Patrick Georgi | 11f0079 | 2020-03-04 15:10:45 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 2 | |
Furquan Shaikh | d1130af | 2020-04-23 12:51:42 -0700 | [diff] [blame] | 3 | #include <assert.h> |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 4 | #include <string.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 5 | #include <acpi/acpi.h> |
| 6 | #include <acpi/acpi_device.h> |
| 7 | #include <acpi/acpigen.h> |
Tim Wawrzynczak | d40a4c2 | 2021-02-25 13:14:49 -0700 | [diff] [blame] | 8 | #include <acpi/acpigen_pci.h> |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 9 | #include <device/device.h> |
| 10 | #include <device/path.h> |
Elyes HAOUAS | 70a03dd | 2019-12-02 20:47:50 +0100 | [diff] [blame] | 11 | #include <stdlib.h> |
Elyes HAOUAS | a83a7db | 2020-07-22 11:42:53 +0200 | [diff] [blame] | 12 | #include <types.h> |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 13 | #include <crc_byte.h> |
| 14 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 15 | #if CONFIG(GENERIC_GPIO_LIB) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 16 | #include <gpio.h> |
| 17 | #endif |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 18 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 19 | #define ACPI_DP_UUID "daffd814-6eba-4d8c-8a91-bc9bbf4aa301" |
| 20 | #define ACPI_DP_CHILD_UUID "dbb8e3e6-5886-4ba6-8795-1319f52a966b" |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 21 | |
Kapil Porwal | 7543627 | 2022-11-28 17:25:48 +0530 | [diff] [blame] | 22 | /* |
| 23 | * Below properties are defined at |
| 24 | * https://docs.microsoft.com/en-us/windows-hardware/drivers/pci/dsd-for-pcie-root-ports |
| 25 | */ |
| 26 | #define ACPI_DSD_EXTERNAL_FACING_PORT_UUID "EFCC06CC-73AC-4BC3-BFF0-76143807C389" |
| 27 | #define ACPI_DSD_EXTERNAL_FACING_PORT_NAME "ExternalFacingPort" |
| 28 | |
| 29 | #define ACPI_DSD_HOTPLUG_IN_D3_UUID "6211E2C0-58A3-4AF3-90E1-927A4E0C55A4" |
| 30 | #define ACPI_DSD_HOTPLUG_IN_D3_NAME "HotPlugSupportInD3" |
| 31 | |
| 32 | /* ID for the DmaProperty _DSD */ |
| 33 | #define ACPI_DSD_DMA_PROPERTY_UUID "70D24161-6DD5-4C9E-8070-705531292865" |
| 34 | #define ACPI_DSD_DMA_PROPERTY_NAME "DmaProperty" |
| 35 | |
| 36 | /* |
| 37 | * Below properties are defined at |
| 38 | * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/power-management-for-storage-hardware-devices-intro |
| 39 | */ |
| 40 | #define ACPI_DSD_STORAGE_D3_UUID "5025030F-842F-4AB4-A561-99A5189762D0" |
| 41 | #define ACPI_DSD_STORAGE_D3_NAME "StorageD3Enable" |
| 42 | |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 43 | /* Write empty word value and return pointer to it */ |
| 44 | static void *acpi_device_write_zero_len(void) |
| 45 | { |
| 46 | char *p = acpigen_get_current(); |
| 47 | acpigen_emit_word(0); |
| 48 | return p; |
| 49 | } |
| 50 | |
| 51 | /* Fill in length value from start to current at specified location */ |
| 52 | static void acpi_device_fill_from_len(char *ptr, char *start) |
| 53 | { |
| 54 | uint16_t len = acpigen_get_current() - start; |
| 55 | ptr[0] = len & 0xff; |
| 56 | ptr[1] = (len >> 8) & 0xff; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Fill in the length field with the value calculated from after |
| 61 | * the 16bit field to acpigen current as this length value does |
| 62 | * not include the length field itself. |
| 63 | */ |
| 64 | static void acpi_device_fill_len(void *ptr) |
| 65 | { |
| 66 | acpi_device_fill_from_len(ptr, ptr + sizeof(uint16_t)); |
| 67 | } |
| 68 | |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 69 | /* Locate and return the ACPI name for this device */ |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 70 | const char *acpi_device_name(const struct device *dev) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 71 | { |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 72 | const struct device *pdev = dev; |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 73 | const char *name = NULL; |
| 74 | |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 75 | if (!dev) |
| 76 | return NULL; |
| 77 | |
| 78 | /* Check for device specific handler */ |
CoolStar | 42be899 | 2023-08-24 02:00:50 -0700 | [diff] [blame] | 79 | if (dev->ops && dev->ops->acpi_name) { |
| 80 | name = dev->ops->acpi_name(dev); |
| 81 | if (name) |
| 82 | return name; |
| 83 | } |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 84 | |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 85 | /* Walk up the tree to find if any parent can identify this device */ |
| 86 | while (pdev->bus) { |
| 87 | pdev = pdev->bus->dev; |
| 88 | if (!pdev) |
| 89 | break; |
| 90 | if (pdev->path.type == DEVICE_PATH_ROOT) |
| 91 | break; |
| 92 | if (pdev->ops && pdev->ops->acpi_name) |
| 93 | name = pdev->ops->acpi_name(dev); |
| 94 | if (name) |
| 95 | return name; |
| 96 | } |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 97 | |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Patrick Rudolph | eeb8e74 | 2019-08-20 08:20:01 +0200 | [diff] [blame] | 101 | /* Locate and return the ACPI _HID (Hardware ID) for this device */ |
| 102 | const char *acpi_device_hid(const struct device *dev) |
| 103 | { |
| 104 | if (!dev) |
| 105 | return NULL; |
| 106 | |
| 107 | /* Check for device specific handler */ |
| 108 | if (dev->ops->acpi_hid) |
| 109 | return dev->ops->acpi_hid(dev); |
| 110 | |
| 111 | /* |
| 112 | * Don't walk up the tree to find any parent that can identify this device, as |
| 113 | * PNP devices are hard to identify. |
| 114 | */ |
| 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 119 | /* |
| 120 | * Generate unique ID based on the ACPI path. |
| 121 | * Collisions on the same _HID are possible but very unlikely. |
| 122 | */ |
Furquan Shaikh | d14d03a | 2020-04-24 21:27:29 -0700 | [diff] [blame] | 123 | uint32_t acpi_device_uid(const struct device *dev) |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 124 | { |
| 125 | const char *path = acpi_device_path(dev); |
| 126 | if (!path) |
| 127 | return 0; |
| 128 | |
| 129 | return CRC(path, strlen(path), crc32_byte); |
| 130 | } |
| 131 | |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 132 | /* Recursive function to find the root device and print a path from there */ |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 133 | static ssize_t acpi_device_path_fill(const struct device *dev, char *buf, |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 134 | size_t buf_len, size_t cur) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 135 | { |
| 136 | const char *name = acpi_device_name(dev); |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 137 | ssize_t next = 0; |
| 138 | |
| 139 | if (!name) |
| 140 | return -1; |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Make sure this name segment will fit, including the path segment |
| 144 | * separator and possible NUL terminator if this is the last segment. |
| 145 | */ |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 146 | if (!dev || (cur + strlen(name) + 2) > buf_len) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 147 | return cur; |
| 148 | |
| 149 | /* Walk up the tree to the root device */ |
| 150 | if (dev->path.type != DEVICE_PATH_ROOT && dev->bus && dev->bus->dev) |
| 151 | next = acpi_device_path_fill(dev->bus->dev, buf, buf_len, cur); |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 152 | if (next < 0) |
| 153 | return next; |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 154 | |
| 155 | /* Fill in the path from the root device */ |
| 156 | next += snprintf(buf + next, buf_len - next, "%s%s", |
Timothy Pearson | aeb6101 | 2017-04-13 17:06:16 -0500 | [diff] [blame] | 157 | (dev->path.type == DEVICE_PATH_ROOT |
| 158 | || (strlen(name) == 0)) ? |
| 159 | "" : ".", name); |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 160 | |
| 161 | return next; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Warning: just as with dev_path() this uses a static buffer |
Martin Roth | 0949e73 | 2021-10-01 14:28:22 -0600 | [diff] [blame] | 166 | * so should not be called multiple times in one statement |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 167 | */ |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 168 | const char *acpi_device_path(const struct device *dev) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 169 | { |
| 170 | static char buf[DEVICE_PATH_MAX] = {}; |
| 171 | |
| 172 | if (!dev) |
| 173 | return NULL; |
| 174 | |
| 175 | if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0) |
| 176 | return NULL; |
| 177 | |
| 178 | return buf; |
| 179 | } |
| 180 | |
| 181 | /* Return the path of the parent device as the ACPI Scope for this device */ |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 182 | const char *acpi_device_scope(const struct device *dev) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 183 | { |
Duncan Laurie | d02685b | 2016-06-30 14:37:37 -0700 | [diff] [blame] | 184 | static char buf[DEVICE_PATH_MAX] = {}; |
| 185 | |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 186 | if (!dev || !dev->bus || !dev->bus->dev) |
| 187 | return NULL; |
| 188 | |
Duncan Laurie | d02685b | 2016-06-30 14:37:37 -0700 | [diff] [blame] | 189 | if (acpi_device_path_fill(dev->bus->dev, buf, sizeof(buf), 0) <= 0) |
| 190 | return NULL; |
| 191 | |
| 192 | return buf; |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Elyes HAOUAS | 6716bab | 2020-01-09 21:29:25 +0100 | [diff] [blame] | 195 | /* Concatenate the device path and provided name suffix */ |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 196 | const char *acpi_device_path_join(const struct device *dev, const char *name) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 197 | { |
| 198 | static char buf[DEVICE_PATH_MAX] = {}; |
Jacob Garber | f2ba2d9 | 2019-07-02 10:50:10 -0600 | [diff] [blame] | 199 | ssize_t len; |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 200 | |
| 201 | if (!dev) |
| 202 | return NULL; |
| 203 | |
| 204 | /* Build the path of this device */ |
| 205 | len = acpi_device_path_fill(dev, buf, sizeof(buf), 0); |
| 206 | if (len <= 0) |
| 207 | return NULL; |
| 208 | |
| 209 | /* Ensure there is room for the added name, separator, and NUL */ |
| 210 | if ((len + strlen(name) + 2) > sizeof(buf)) |
| 211 | return NULL; |
| 212 | snprintf(buf + len, sizeof(buf) - len, ".%s", name); |
| 213 | |
| 214 | return buf; |
| 215 | } |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 216 | |
Hung-Te Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame] | 217 | int acpi_device_status(const struct device *dev) |
| 218 | { |
| 219 | if (!dev->enabled) |
| 220 | return ACPI_STATUS_DEVICE_ALL_OFF; |
| 221 | if (dev->hidden) |
| 222 | return ACPI_STATUS_DEVICE_HIDDEN_ON; |
| 223 | return ACPI_STATUS_DEVICE_ALL_ON; |
| 224 | } |
| 225 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 226 | /* Write the unique _UID based on ACPI device path. */ |
Furquan Shaikh | d14d03a | 2020-04-24 21:27:29 -0700 | [diff] [blame] | 227 | void acpi_device_write_uid(const struct device *dev) |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 228 | { |
| 229 | acpigen_write_name_integer("_UID", acpi_device_uid(dev)); |
| 230 | } |
| 231 | |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 232 | /* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */ |
| 233 | void acpi_device_write_interrupt(const struct acpi_irq *irq) |
| 234 | { |
| 235 | void *desc_length; |
| 236 | uint8_t flags; |
| 237 | |
| 238 | if (!irq || !irq->pin) |
| 239 | return; |
| 240 | |
| 241 | /* This is supported by GpioInt() but not Interrupt() */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 242 | if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 243 | return; |
| 244 | |
| 245 | /* Byte 0: Descriptor Type */ |
| 246 | acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT); |
| 247 | |
| 248 | /* Byte 1-2: Length (filled in later) */ |
| 249 | desc_length = acpi_device_write_zero_len(); |
| 250 | |
| 251 | /* |
| 252 | * Byte 3: Flags |
| 253 | * [7:5]: Reserved |
| 254 | * [4]: Wake (0=NO_WAKE 1=WAKE) |
| 255 | * [3]: Sharing (0=EXCLUSIVE 1=SHARED) |
| 256 | * [2]: Polarity (0=HIGH 1=LOW) |
| 257 | * [1]: Mode (0=LEVEL 1=EDGE) |
| 258 | * [0]: Resource (0=PRODUCER 1=CONSUMER) |
| 259 | */ |
| 260 | flags = 1 << 0; /* ResourceConsumer */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 261 | if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 262 | flags |= 1 << 1; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 263 | if (irq->polarity == ACPI_IRQ_ACTIVE_LOW) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 264 | flags |= 1 << 2; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 265 | if (irq->shared == ACPI_IRQ_SHARED) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 266 | flags |= 1 << 3; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 267 | if (irq->wake == ACPI_IRQ_WAKE) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 268 | flags |= 1 << 4; |
| 269 | acpigen_emit_byte(flags); |
| 270 | |
| 271 | /* Byte 4: Interrupt Table Entry Count */ |
| 272 | acpigen_emit_byte(1); |
| 273 | |
| 274 | /* Byte 5-8: Interrupt Number */ |
| 275 | acpigen_emit_dword(irq->pin); |
| 276 | |
| 277 | /* Fill in Descriptor Length (account for len word) */ |
| 278 | acpi_device_fill_len(desc_length); |
| 279 | } |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 280 | |
| 281 | /* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */ |
| 282 | void acpi_device_write_gpio(const struct acpi_gpio *gpio) |
| 283 | { |
| 284 | void *start, *desc_length; |
| 285 | void *pin_table_offset, *vendor_data_offset, *resource_offset; |
| 286 | uint16_t flags = 0; |
| 287 | int pin; |
| 288 | |
| 289 | if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO) |
| 290 | return; |
| 291 | |
| 292 | start = acpigen_get_current(); |
| 293 | |
| 294 | /* Byte 0: Descriptor Type */ |
| 295 | acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO); |
| 296 | |
| 297 | /* Byte 1-2: Length (fill in later) */ |
| 298 | desc_length = acpi_device_write_zero_len(); |
| 299 | |
| 300 | /* Byte 3: Revision ID */ |
| 301 | acpigen_emit_byte(ACPI_GPIO_REVISION_ID); |
| 302 | |
| 303 | /* Byte 4: GpioIo or GpioInt */ |
| 304 | acpigen_emit_byte(gpio->type); |
| 305 | |
| 306 | /* |
| 307 | * Byte 5-6: General Flags |
| 308 | * [15:1]: 0 => Reserved |
| 309 | * [0]: 1 => ResourceConsumer |
| 310 | */ |
| 311 | acpigen_emit_word(1 << 0); |
| 312 | |
| 313 | switch (gpio->type) { |
| 314 | case ACPI_GPIO_TYPE_INTERRUPT: |
| 315 | /* |
| 316 | * Byte 7-8: GPIO Interrupt Flags |
| 317 | * [15:5]: 0 => Reserved |
| 318 | * [4]: Wake (0=NO_WAKE 1=WAKE) |
| 319 | * [3]: Sharing (0=EXCLUSIVE 1=SHARED) |
| 320 | * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH) |
| 321 | * [0]: Mode (0=LEVEL 1=EDGE) |
| 322 | */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 323 | if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 324 | flags |= 1 << 0; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 325 | if (gpio->irq.shared == ACPI_IRQ_SHARED) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 326 | flags |= 1 << 3; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 327 | if (gpio->irq.wake == ACPI_IRQ_WAKE) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 328 | flags |= 1 << 4; |
| 329 | |
| 330 | switch (gpio->irq.polarity) { |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 331 | case ACPI_IRQ_ACTIVE_HIGH: |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 332 | flags |= 0 << 1; |
| 333 | break; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 334 | case ACPI_IRQ_ACTIVE_LOW: |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 335 | flags |= 1 << 1; |
| 336 | break; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 337 | case ACPI_IRQ_ACTIVE_BOTH: |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 338 | flags |= 2 << 1; |
| 339 | break; |
| 340 | } |
| 341 | break; |
| 342 | |
| 343 | case ACPI_GPIO_TYPE_IO: |
| 344 | /* |
| 345 | * Byte 7-8: GPIO IO Flags |
| 346 | * [15:4]: 0 => Reserved |
| 347 | * [3]: Sharing (0=EXCLUSIVE 1=SHARED) |
| 348 | * [2]: 0 => Reserved |
| 349 | * [1:0]: IO Restriction |
| 350 | * 0 => IoRestrictionNone |
| 351 | * 1 => IoRestrictionInputOnly |
| 352 | * 2 => IoRestrictionOutputOnly |
| 353 | * 3 => IoRestrictionNoneAndPreserve |
| 354 | */ |
| 355 | flags |= gpio->io_restrict & 3; |
| 356 | if (gpio->io_shared) |
| 357 | flags |= 1 << 3; |
| 358 | break; |
| 359 | } |
| 360 | acpigen_emit_word(flags); |
| 361 | |
| 362 | /* |
| 363 | * Byte 9: Pin Configuration |
| 364 | * 0x01 => Default (no configuration applied) |
| 365 | * 0x02 => Pull-up |
| 366 | * 0x03 => Pull-down |
| 367 | * 0x04-0x7F => Reserved |
| 368 | * 0x80-0xff => Vendor defined |
| 369 | */ |
| 370 | acpigen_emit_byte(gpio->pull); |
| 371 | |
| 372 | /* Byte 10-11: Output Drive Strength in 1/100 mA */ |
| 373 | acpigen_emit_word(gpio->output_drive_strength); |
| 374 | |
| 375 | /* Byte 12-13: Debounce Timeout in 1/100 ms */ |
| 376 | acpigen_emit_word(gpio->interrupt_debounce_timeout); |
| 377 | |
| 378 | /* Byte 14-15: Pin Table Offset, relative to start */ |
| 379 | pin_table_offset = acpi_device_write_zero_len(); |
| 380 | |
| 381 | /* Byte 16: Reserved */ |
| 382 | acpigen_emit_byte(0); |
| 383 | |
| 384 | /* Byte 17-18: Resource Source Name Offset, relative to start */ |
| 385 | resource_offset = acpi_device_write_zero_len(); |
| 386 | |
| 387 | /* Byte 19-20: Vendor Data Offset, relative to start */ |
| 388 | vendor_data_offset = acpi_device_write_zero_len(); |
| 389 | |
| 390 | /* Byte 21-22: Vendor Data Length */ |
| 391 | acpigen_emit_word(0); |
| 392 | |
| 393 | /* Fill in Pin Table Offset */ |
| 394 | acpi_device_fill_from_len(pin_table_offset, start); |
| 395 | |
| 396 | /* Pin Table, one word for each pin */ |
Duncan Laurie | 5b6c28c | 2016-06-29 10:47:22 -0700 | [diff] [blame] | 397 | for (pin = 0; pin < gpio->pin_count; pin++) { |
| 398 | uint16_t acpi_pin = gpio->pins[pin]; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 399 | #if CONFIG(GENERIC_GPIO_LIB) |
Duncan Laurie | 5b6c28c | 2016-06-29 10:47:22 -0700 | [diff] [blame] | 400 | acpi_pin = gpio_acpi_pin(acpi_pin); |
| 401 | #endif |
| 402 | acpigen_emit_word(acpi_pin); |
| 403 | } |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 404 | |
| 405 | /* Fill in Resource Source Name Offset */ |
| 406 | acpi_device_fill_from_len(resource_offset, start); |
| 407 | |
| 408 | /* Resource Source Name String */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 409 | #if CONFIG(GENERIC_GPIO_LIB) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 410 | acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0])); |
| 411 | #else |
| 412 | acpigen_emit_string(gpio->resource); |
| 413 | #endif |
| 414 | |
| 415 | /* Fill in Vendor Data Offset */ |
| 416 | acpi_device_fill_from_len(vendor_data_offset, start); |
| 417 | |
| 418 | /* Fill in GPIO Descriptor Length (account for len word) */ |
| 419 | acpi_device_fill_len(desc_length); |
| 420 | } |
Duncan Laurie | 1010b4a | 2016-05-09 20:10:47 -0700 | [diff] [blame] | 421 | |
| 422 | /* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */ |
| 423 | void acpi_device_write_i2c(const struct acpi_i2c *i2c) |
| 424 | { |
| 425 | void *desc_length, *type_length; |
| 426 | |
| 427 | /* Byte 0: Descriptor Type */ |
| 428 | acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS); |
| 429 | |
| 430 | /* Byte 1+2: Length (filled in later) */ |
| 431 | desc_length = acpi_device_write_zero_len(); |
| 432 | |
| 433 | /* Byte 3: Revision ID */ |
Elyes HAOUAS | 34564ed | 2019-04-16 08:12:10 +0200 | [diff] [blame] | 434 | acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID); |
Duncan Laurie | 1010b4a | 2016-05-09 20:10:47 -0700 | [diff] [blame] | 435 | |
| 436 | /* Byte 4: Resource Source Index is Reserved */ |
| 437 | acpigen_emit_byte(0); |
| 438 | |
| 439 | /* Byte 5: Serial Bus Type is I2C */ |
| 440 | acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C); |
| 441 | |
| 442 | /* |
| 443 | * Byte 6: Flags |
| 444 | * [7:2]: 0 => Reserved |
| 445 | * [1]: 1 => ResourceConsumer |
| 446 | * [0]: 0 => ControllerInitiated |
| 447 | */ |
| 448 | acpigen_emit_byte(1 << 1); |
| 449 | |
| 450 | /* |
| 451 | * Byte 7-8: Type Specific Flags |
| 452 | * [15:1]: 0 => Reserved |
| 453 | * [0]: 0 => 7bit, 1 => 10bit |
| 454 | */ |
| 455 | acpigen_emit_word(i2c->mode_10bit); |
| 456 | |
| 457 | /* Byte 9: Type Specific Revision ID */ |
Elyes HAOUAS | 34564ed | 2019-04-16 08:12:10 +0200 | [diff] [blame] | 458 | acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID); |
Duncan Laurie | 1010b4a | 2016-05-09 20:10:47 -0700 | [diff] [blame] | 459 | |
| 460 | /* Byte 10-11: I2C Type Data Length */ |
| 461 | type_length = acpi_device_write_zero_len(); |
| 462 | |
| 463 | /* Byte 12-15: I2C Bus Speed */ |
| 464 | acpigen_emit_dword(i2c->speed); |
| 465 | |
| 466 | /* Byte 16-17: I2C Slave Address */ |
| 467 | acpigen_emit_word(i2c->address); |
| 468 | |
| 469 | /* Fill in Type Data Length */ |
| 470 | acpi_device_fill_len(type_length); |
| 471 | |
| 472 | /* Byte 18+: ResourceSource */ |
| 473 | acpigen_emit_string(i2c->resource); |
| 474 | |
| 475 | /* Fill in I2C Descriptor Length */ |
| 476 | acpi_device_fill_len(desc_length); |
| 477 | } |
Duncan Laurie | 70c86d9 | 2016-05-10 07:26:34 -0700 | [diff] [blame] | 478 | |
| 479 | /* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */ |
| 480 | void acpi_device_write_spi(const struct acpi_spi *spi) |
| 481 | { |
| 482 | void *desc_length, *type_length; |
| 483 | uint16_t flags = 0; |
| 484 | |
| 485 | /* Byte 0: Descriptor Type */ |
| 486 | acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS); |
| 487 | |
| 488 | /* Byte 1+2: Length (filled in later) */ |
| 489 | desc_length = acpi_device_write_zero_len(); |
| 490 | |
| 491 | /* Byte 3: Revision ID */ |
Elyes HAOUAS | 34564ed | 2019-04-16 08:12:10 +0200 | [diff] [blame] | 492 | acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID); |
Duncan Laurie | 70c86d9 | 2016-05-10 07:26:34 -0700 | [diff] [blame] | 493 | |
| 494 | /* Byte 4: Resource Source Index is Reserved */ |
| 495 | acpigen_emit_byte(0); |
| 496 | |
| 497 | /* Byte 5: Serial Bus Type is SPI */ |
| 498 | acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI); |
| 499 | |
| 500 | /* |
| 501 | * Byte 6: Flags |
| 502 | * [7:2]: 0 => Reserved |
| 503 | * [1]: 1 => ResourceConsumer |
| 504 | * [0]: 0 => ControllerInitiated |
| 505 | */ |
| 506 | acpigen_emit_byte(1 << 1); |
| 507 | |
| 508 | /* |
| 509 | * Byte 7-8: Type Specific Flags |
| 510 | * [15:2]: 0 => Reserved |
| 511 | * [1]: 0 => ActiveLow, 1 => ActiveHigh |
| 512 | * [0]: 0 => FourWire, 1 => ThreeWire |
| 513 | */ |
| 514 | if (spi->wire_mode == SPI_3_WIRE_MODE) |
| 515 | flags |= 1 << 0; |
| 516 | if (spi->device_select_polarity == SPI_POLARITY_HIGH) |
| 517 | flags |= 1 << 1; |
| 518 | acpigen_emit_word(flags); |
| 519 | |
| 520 | /* Byte 9: Type Specific Revision ID */ |
Elyes HAOUAS | 34564ed | 2019-04-16 08:12:10 +0200 | [diff] [blame] | 521 | acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID); |
Duncan Laurie | 70c86d9 | 2016-05-10 07:26:34 -0700 | [diff] [blame] | 522 | |
| 523 | /* Byte 10-11: SPI Type Data Length */ |
| 524 | type_length = acpi_device_write_zero_len(); |
| 525 | |
| 526 | /* Byte 12-15: Connection Speed */ |
| 527 | acpigen_emit_dword(spi->speed); |
| 528 | |
| 529 | /* Byte 16: Data Bit Length */ |
| 530 | acpigen_emit_byte(spi->data_bit_length); |
| 531 | |
| 532 | /* Byte 17: Clock Phase */ |
| 533 | acpigen_emit_byte(spi->clock_phase); |
| 534 | |
| 535 | /* Byte 18: Clock Polarity */ |
| 536 | acpigen_emit_byte(spi->clock_polarity); |
| 537 | |
| 538 | /* Byte 19-20: Device Selection */ |
| 539 | acpigen_emit_word(spi->device_select); |
| 540 | |
| 541 | /* Fill in Type Data Length */ |
| 542 | acpi_device_fill_len(type_length); |
| 543 | |
| 544 | /* Byte 21+: ResourceSource String */ |
| 545 | acpigen_emit_string(spi->resource); |
| 546 | |
| 547 | /* Fill in SPI Descriptor Length */ |
| 548 | acpi_device_fill_len(desc_length); |
| 549 | } |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 550 | |
Duncan Laurie | dccef0d | 2020-05-27 12:29:51 -0700 | [diff] [blame] | 551 | /* UART Serial Bus - UARTSerialBusV2() */ |
| 552 | void acpi_device_write_uart(const struct acpi_uart *uart) |
| 553 | { |
| 554 | void *desc_length, *type_length; |
| 555 | uint16_t flags; |
| 556 | |
| 557 | /* Byte 0: Descriptor Type */ |
| 558 | acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS); |
| 559 | |
| 560 | /* Byte 1+2: Length (filled in later) */ |
| 561 | desc_length = acpi_device_write_zero_len(); |
| 562 | |
| 563 | /* Byte 3: Revision ID */ |
| 564 | acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID); |
| 565 | |
| 566 | /* Byte 4: Resource Source Index is Reserved */ |
| 567 | acpigen_emit_byte(0); |
| 568 | |
| 569 | /* Byte 5: Serial Bus Type is UART */ |
| 570 | acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART); |
| 571 | |
| 572 | /* |
| 573 | * Byte 6: Flags |
| 574 | * [7:2]: 0 => Reserved |
| 575 | * [1]: 1 => ResourceConsumer |
| 576 | * [0]: 0 => ControllerInitiated |
| 577 | */ |
| 578 | acpigen_emit_byte(BIT(1)); |
| 579 | |
| 580 | /* |
| 581 | * Byte 7-8: Type Specific Flags |
| 582 | * [15:8]: 0 => Reserved |
| 583 | * [7]: 0 => Little Endian, 1 => Big Endian |
| 584 | * [6:4]: Data bits |
| 585 | * [3:2]: Stop bits |
| 586 | * [1:0]: Flow control |
| 587 | */ |
| 588 | flags = uart->flow_control & 3; |
| 589 | flags |= (uart->stop_bits & 3) << 2; |
| 590 | flags |= (uart->data_bits & 7) << 4; |
| 591 | flags |= (uart->endian & 1) << 7; |
| 592 | acpigen_emit_word(flags); |
| 593 | |
| 594 | /* Byte 9: Type Specific Revision ID */ |
| 595 | acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID); |
| 596 | |
| 597 | /* Byte 10-11: Type Data Length */ |
| 598 | type_length = acpi_device_write_zero_len(); |
| 599 | |
| 600 | /* Byte 12-15: Initial Baud Rate */ |
| 601 | acpigen_emit_dword(uart->initial_baud_rate); |
| 602 | |
| 603 | /* Byte 16-17: RX FIFO size */ |
| 604 | acpigen_emit_word(uart->rx_fifo_bytes); |
| 605 | |
| 606 | /* Byte 18-19: TX FIFO size */ |
| 607 | acpigen_emit_word(uart->tx_fifo_bytes); |
| 608 | |
| 609 | /* Byte 20: Parity */ |
| 610 | acpigen_emit_byte(uart->parity); |
| 611 | |
| 612 | /* Byte 21: Lines Enabled */ |
| 613 | acpigen_emit_byte(uart->lines_in_use); |
| 614 | |
| 615 | /* Fill in Type Data Length */ |
| 616 | acpi_device_fill_len(type_length); |
| 617 | |
| 618 | /* Byte 22+: ResourceSource */ |
| 619 | acpigen_emit_string(uart->resource); |
| 620 | |
| 621 | /* Fill in Descriptor Length */ |
| 622 | acpi_device_fill_len(desc_length); |
| 623 | } |
| 624 | |
Raul E Rangel | 6d2bc00 | 2021-05-27 15:04:21 -0600 | [diff] [blame] | 625 | #define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP |
| 626 | #define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP |
| 627 | |
| 628 | /** |
| 629 | * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO |
| 630 | * state does not match the active parameter. |
| 631 | */ |
| 632 | static void acpigen_write_gpio_STA(const struct acpi_gpio *gpio, bool active) |
| 633 | { |
| 634 | if (!gpio || !gpio->pin_count) |
| 635 | return; |
| 636 | |
| 637 | /* Read current GPIO status into Local0. */ |
| 638 | acpigen_get_tx_gpio(gpio); |
| 639 | |
| 640 | /* |
| 641 | * If (!Local0) |
| 642 | * { |
| 643 | * Return (Zero) |
| 644 | * } |
| 645 | */ |
| 646 | acpigen_write_if(); |
| 647 | if (active) |
| 648 | acpigen_emit_byte(LNOT_OP); |
| 649 | acpigen_emit_byte(LOCAL0_OP); |
| 650 | acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_OFF_OP); |
| 651 | acpigen_write_if_end(); |
| 652 | } |
| 653 | |
| 654 | static void acpigen_write_power_res_STA(const struct acpi_power_res_params *params) |
| 655 | { |
| 656 | acpigen_write_method_serialized("_STA", 0); |
| 657 | |
| 658 | /* Verify all the GPIOs are in the ON state, otherwise return 0 */ |
| 659 | acpigen_write_gpio_STA(params->enable_gpio, true); |
| 660 | acpigen_write_gpio_STA(params->reset_gpio, false); |
| 661 | acpigen_write_gpio_STA(params->stop_gpio, false); |
| 662 | |
| 663 | /* All GPIOs are in the ON state */ |
| 664 | acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP); |
| 665 | |
| 666 | acpigen_pop_len(); /* Method */ |
| 667 | } |
| 668 | |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 669 | /* PowerResource() with Enable and/or Reset control */ |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 670 | void acpi_device_add_power_res(const struct acpi_power_res_params *params) |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 671 | { |
Furquan Shaikh | d47946e | 2021-09-14 15:54:41 -0700 | [diff] [blame] | 672 | static uint8_t id; |
Furquan Shaikh | dc78275 | 2020-04-30 22:49:39 -0700 | [diff] [blame] | 673 | static const char * const power_res_dev_states[] = { "_PR0", "_PR3" }; |
Edward O'Callaghan | 7e26255 | 2020-01-23 10:32:33 +1100 | [diff] [blame] | 674 | unsigned int reset_gpio = params->reset_gpio ? params->reset_gpio->pins[0] : 0; |
| 675 | unsigned int enable_gpio = params->enable_gpio ? params->enable_gpio->pins[0] : 0; |
| 676 | unsigned int stop_gpio = params->stop_gpio ? params->stop_gpio->pins[0] : 0; |
Furquan Shaikh | d47946e | 2021-09-14 15:54:41 -0700 | [diff] [blame] | 677 | char pr_name[ACPI_NAME_BUFFER_SIZE]; |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 678 | |
Furquan Shaikh | edf459f | 2017-08-28 17:20:49 -0700 | [diff] [blame] | 679 | if (!reset_gpio && !enable_gpio && !stop_gpio) |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 680 | return; |
| 681 | |
Furquan Shaikh | d47946e | 2021-09-14 15:54:41 -0700 | [diff] [blame] | 682 | snprintf(pr_name, sizeof(pr_name), "PR%02X", id++); |
| 683 | |
| 684 | /* PowerResource (PR##, 0, 0) */ |
| 685 | acpigen_write_power_res(pr_name, 0, 0, power_res_dev_states, |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 686 | ARRAY_SIZE(power_res_dev_states)); |
| 687 | |
Raul E Rangel | 6d2bc00 | 2021-05-27 15:04:21 -0600 | [diff] [blame] | 688 | if (params->use_gpio_for_status) { |
| 689 | acpigen_write_power_res_STA(params); |
| 690 | } else { |
| 691 | /* Method (_STA, 0, NotSerialized) { Return (0x1) } */ |
| 692 | acpigen_write_STA(ACPI_POWER_RESOURCE_STATUS_ON_OP); |
| 693 | } |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 694 | |
| 695 | /* Method (_ON, 0, Serialized) */ |
| 696 | acpigen_write_method_serialized("_ON", 0); |
Tim Van Patten | 3d4665c | 2022-04-13 11:53:20 -0600 | [diff] [blame] | 697 | /* Call _STA and early return if the device is already enabled, since the Linux |
| 698 | kernel doesn't check the device status before calling _ON. This avoids |
| 699 | unnecessary delays while booting. */ |
| 700 | if (params->use_gpio_for_status) { |
| 701 | /* Local0 = _STA () */ |
| 702 | acpigen_write_store(); |
| 703 | acpigen_emit_namestring("_STA"); |
| 704 | acpigen_emit_byte(LOCAL0_OP); |
| 705 | /* If (( Local0 == ACPI_POWER_RESOURCE_STATUS_ON_OP)) */ |
| 706 | acpigen_write_if_lequal_op_op(LOCAL0_OP, ACPI_POWER_RESOURCE_STATUS_ON_OP); |
| 707 | acpigen_write_return_op(ZERO_OP); |
| 708 | acpigen_write_if_end(); |
| 709 | } |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 710 | if (reset_gpio) |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 711 | acpigen_enable_tx_gpio(params->reset_gpio); |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 712 | if (enable_gpio) { |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 713 | acpigen_enable_tx_gpio(params->enable_gpio); |
| 714 | if (params->enable_delay_ms) |
| 715 | acpigen_write_sleep(params->enable_delay_ms); |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 716 | } |
| 717 | if (reset_gpio) { |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 718 | acpigen_disable_tx_gpio(params->reset_gpio); |
| 719 | if (params->reset_delay_ms) |
| 720 | acpigen_write_sleep(params->reset_delay_ms); |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 721 | } |
Furquan Shaikh | edf459f | 2017-08-28 17:20:49 -0700 | [diff] [blame] | 722 | if (stop_gpio) { |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 723 | acpigen_disable_tx_gpio(params->stop_gpio); |
| 724 | if (params->stop_delay_ms) |
| 725 | acpigen_write_sleep(params->stop_delay_ms); |
Furquan Shaikh | edf459f | 2017-08-28 17:20:49 -0700 | [diff] [blame] | 726 | } |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 727 | acpigen_pop_len(); /* _ON method */ |
| 728 | |
| 729 | /* Method (_OFF, 0, Serialized) */ |
| 730 | acpigen_write_method_serialized("_OFF", 0); |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 731 | if (stop_gpio) { |
| 732 | acpigen_enable_tx_gpio(params->stop_gpio); |
| 733 | if (params->stop_off_delay_ms) |
| 734 | acpigen_write_sleep(params->stop_off_delay_ms); |
| 735 | } |
| 736 | if (reset_gpio) { |
| 737 | acpigen_enable_tx_gpio(params->reset_gpio); |
| 738 | if (params->reset_off_delay_ms) |
| 739 | acpigen_write_sleep(params->reset_off_delay_ms); |
| 740 | } |
| 741 | if (enable_gpio) { |
| 742 | acpigen_disable_tx_gpio(params->enable_gpio); |
| 743 | if (params->enable_off_delay_ms) |
| 744 | acpigen_write_sleep(params->enable_off_delay_ms); |
| 745 | } |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 746 | acpigen_pop_len(); /* _OFF method */ |
| 747 | |
Furquan Shaikh | d47946e | 2021-09-14 15:54:41 -0700 | [diff] [blame] | 748 | acpigen_pop_len(); /* PowerResource PR## */ |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 751 | static void acpi_dp_write_array(const struct acpi_dp *array); |
| 752 | static void acpi_dp_write_value(const struct acpi_dp *prop) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 753 | { |
| 754 | switch (prop->type) { |
| 755 | case ACPI_DP_TYPE_INTEGER: |
| 756 | acpigen_write_integer(prop->integer); |
| 757 | break; |
| 758 | case ACPI_DP_TYPE_STRING: |
Harsha Priya | 3a96ac4 | 2016-07-15 17:31:43 -0700 | [diff] [blame] | 759 | case ACPI_DP_TYPE_CHILD: |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 760 | acpigen_write_string(prop->string); |
| 761 | break; |
| 762 | case ACPI_DP_TYPE_REFERENCE: |
| 763 | acpigen_emit_namestring(prop->string); |
| 764 | break; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 765 | case ACPI_DP_TYPE_ARRAY: |
| 766 | acpi_dp_write_array(prop->array); |
| 767 | break; |
| 768 | default: |
| 769 | break; |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 773 | /* Package (2) { "prop->name", VALUE } */ |
| 774 | static void acpi_dp_write_property(const struct acpi_dp *prop) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 775 | { |
| 776 | acpigen_write_package(2); |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 777 | acpigen_write_string(prop->name); |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 778 | acpi_dp_write_value(prop); |
| 779 | acpigen_pop_len(); |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* Write array of Device Properties */ |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 783 | static void acpi_dp_write_array(const struct acpi_dp *array) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 784 | { |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 785 | const struct acpi_dp *dp; |
| 786 | char *pkg_count; |
| 787 | |
| 788 | /* Package element count determined as it is populated */ |
| 789 | pkg_count = acpigen_write_package(0); |
| 790 | |
Furquan Shaikh | 35c01bc | 2016-10-03 23:30:14 -0700 | [diff] [blame] | 791 | /* |
| 792 | * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array. |
| 793 | * DP_TYPE_TABLE does not have a value to be written. Thus, start |
| 794 | * the loop from next type in the array. |
| 795 | */ |
| 796 | for (dp = array->next; dp; dp = dp->next) { |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 797 | acpi_dp_write_value(dp); |
| 798 | (*pkg_count)++; |
| 799 | } |
| 800 | |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 801 | acpigen_pop_len(); |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 804 | static void acpi_dp_free(struct acpi_dp *dp) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 805 | { |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 806 | while (dp) { |
| 807 | struct acpi_dp *p = dp->next; |
| 808 | |
| 809 | switch (dp->type) { |
| 810 | case ACPI_DP_TYPE_CHILD: |
| 811 | acpi_dp_free(dp->child); |
| 812 | break; |
| 813 | case ACPI_DP_TYPE_ARRAY: |
| 814 | acpi_dp_free(dp->array); |
| 815 | break; |
| 816 | default: |
| 817 | break; |
| 818 | } |
| 819 | |
| 820 | free(dp); |
| 821 | dp = p; |
| 822 | } |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Duncan Laurie | 84fac41 | 2020-06-03 12:36:51 -0700 | [diff] [blame] | 825 | static bool acpi_dp_write_properties(struct acpi_dp *prop, const char *uuid) |
| 826 | { |
| 827 | struct acpi_dp *dp; |
| 828 | char *prop_count = NULL; |
| 829 | |
| 830 | /* Print base properties */ |
| 831 | for (dp = prop; dp; dp = dp->next) { |
| 832 | if (dp->type == ACPI_DP_TYPE_TABLE || |
| 833 | dp->type == ACPI_DP_TYPE_CHILD || |
| 834 | dp->type == ACPI_DP_TYPE_PACKAGE) |
| 835 | continue; |
| 836 | |
| 837 | /* |
| 838 | * The UUID and package is only added when |
| 839 | * we come across the first property. This |
| 840 | * is to avoid creating a zero-length package |
| 841 | * in situations where there are only children. |
| 842 | */ |
| 843 | if (!prop_count) { |
| 844 | /* ToUUID (dp->uuid) */ |
| 845 | acpigen_write_uuid(uuid); |
| 846 | /* |
| 847 | * Package (PROP), element count determined as |
| 848 | * it is populated |
| 849 | */ |
| 850 | prop_count = acpigen_write_package(0); |
| 851 | } |
| 852 | (*prop_count)++; |
| 853 | acpi_dp_write_property(dp); |
| 854 | } |
| 855 | if (prop_count) { |
| 856 | /* Package (PROP) length, if a package was written */ |
| 857 | acpigen_pop_len(); |
| 858 | return true; |
| 859 | } |
| 860 | return false; |
| 861 | } |
| 862 | |
Simon Glass | 8bfa51e | 2020-06-29 16:18:37 -0600 | [diff] [blame] | 863 | static void acpi_dp_write_(struct acpi_dp *table) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 864 | { |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 865 | struct acpi_dp *dp, *prop; |
Duncan Laurie | 84fac41 | 2020-06-03 12:36:51 -0700 | [diff] [blame] | 866 | char *dp_count; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 867 | int child_count = 0; |
| 868 | |
Duncan Laurie | c1adeb6 | 2020-04-29 00:04:14 -0700 | [diff] [blame] | 869 | if (!table || table->type != ACPI_DP_TYPE_TABLE || !table->next) |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 870 | return; |
| 871 | |
| 872 | /* Name (name) */ |
| 873 | acpigen_write_name(table->name); |
| 874 | |
| 875 | /* Device Property list starts with the next entry */ |
| 876 | prop = table->next; |
| 877 | |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 878 | /* Package (DP), default to assuming no properties or children */ |
| 879 | dp_count = acpigen_write_package(0); |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 880 | |
| 881 | /* Print base properties */ |
Duncan Laurie | 84fac41 | 2020-06-03 12:36:51 -0700 | [diff] [blame] | 882 | if (acpi_dp_write_properties(prop, table->uuid)) |
| 883 | *dp_count += 2; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 884 | |
Duncan Laurie | 84fac41 | 2020-06-03 12:36:51 -0700 | [diff] [blame] | 885 | /* Count child properties */ |
| 886 | for (dp = prop; dp; dp = dp->next) |
| 887 | if (dp->type == ACPI_DP_TYPE_CHILD) |
| 888 | child_count++; |
| 889 | |
| 890 | /* Add child properties to the base table */ |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 891 | if (child_count) { |
Duncan Laurie | 84fac41 | 2020-06-03 12:36:51 -0700 | [diff] [blame] | 892 | /* Update DP package count */ |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 893 | *dp_count += 2; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 894 | /* ToUUID (ACPI_DP_CHILD_UUID) */ |
| 895 | acpigen_write_uuid(ACPI_DP_CHILD_UUID); |
| 896 | |
| 897 | /* Print child pointer properties */ |
| 898 | acpigen_write_package(child_count); |
| 899 | |
| 900 | for (dp = prop; dp; dp = dp->next) |
| 901 | if (dp->type == ACPI_DP_TYPE_CHILD) |
| 902 | acpi_dp_write_property(dp); |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 903 | /* Package (CHILD) length */ |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 904 | acpigen_pop_len(); |
| 905 | } |
| 906 | |
Duncan Laurie | 84fac41 | 2020-06-03 12:36:51 -0700 | [diff] [blame] | 907 | /* Write packages of properties with unique UUID */ |
| 908 | for (dp = prop; dp; dp = dp->next) |
| 909 | if (dp->type == ACPI_DP_TYPE_PACKAGE) |
| 910 | if (acpi_dp_write_properties(dp->child, dp->uuid)) |
| 911 | *dp_count += 2; |
| 912 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 913 | /* Package (DP) length */ |
| 914 | acpigen_pop_len(); |
| 915 | |
| 916 | /* Recursively parse children into separate tables */ |
| 917 | for (dp = prop; dp; dp = dp->next) |
| 918 | if (dp->type == ACPI_DP_TYPE_CHILD) |
Simon Glass | 8bfa51e | 2020-06-29 16:18:37 -0600 | [diff] [blame] | 919 | acpi_dp_write_(dp->child); |
| 920 | } |
| 921 | |
| 922 | void acpi_dp_write(struct acpi_dp *table) |
| 923 | { |
| 924 | acpi_dp_write_(table); |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 925 | |
| 926 | /* Clean up */ |
| 927 | acpi_dp_free(table); |
| 928 | } |
| 929 | |
| 930 | static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type, |
| 931 | const char *name) |
| 932 | { |
| 933 | struct acpi_dp *new; |
| 934 | |
| 935 | new = malloc(sizeof(struct acpi_dp)); |
| 936 | if (!new) |
| 937 | return NULL; |
| 938 | |
| 939 | memset(new, 0, sizeof(*new)); |
| 940 | new->type = type; |
| 941 | new->name = name; |
Duncan Laurie | 84fac41 | 2020-06-03 12:36:51 -0700 | [diff] [blame] | 942 | new->uuid = ACPI_DP_UUID; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 943 | |
| 944 | if (dp) { |
| 945 | /* Add to end of property list */ |
| 946 | while (dp->next) |
| 947 | dp = dp->next; |
| 948 | dp->next = new; |
| 949 | } |
| 950 | |
| 951 | return new; |
| 952 | } |
| 953 | |
| 954 | struct acpi_dp *acpi_dp_new_table(const char *name) |
| 955 | { |
| 956 | return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name); |
| 957 | } |
| 958 | |
Duncan Laurie | b3023b6 | 2017-08-29 08:26:50 -0700 | [diff] [blame] | 959 | size_t acpi_dp_add_property_list(struct acpi_dp *dp, |
| 960 | const struct acpi_dp *property_list, |
| 961 | size_t property_count) |
| 962 | { |
| 963 | const struct acpi_dp *prop; |
| 964 | size_t i, properties_added = 0; |
| 965 | |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 966 | if (!dp || !property_list) |
| 967 | return 0; |
| 968 | |
Duncan Laurie | b3023b6 | 2017-08-29 08:26:50 -0700 | [diff] [blame] | 969 | for (i = 0; i < property_count; i++) { |
| 970 | prop = &property_list[i]; |
| 971 | |
| 972 | if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name) |
| 973 | continue; |
| 974 | |
| 975 | switch (prop->type) { |
| 976 | case ACPI_DP_TYPE_INTEGER: |
| 977 | acpi_dp_add_integer(dp, prop->name, prop->integer); |
| 978 | break; |
| 979 | case ACPI_DP_TYPE_STRING: |
| 980 | acpi_dp_add_string(dp, prop->name, prop->string); |
| 981 | break; |
| 982 | case ACPI_DP_TYPE_REFERENCE: |
| 983 | acpi_dp_add_reference(dp, prop->name, prop->string); |
| 984 | break; |
| 985 | case ACPI_DP_TYPE_ARRAY: |
| 986 | acpi_dp_add_array(dp, prop->array); |
| 987 | break; |
| 988 | case ACPI_DP_TYPE_CHILD: |
| 989 | acpi_dp_add_child(dp, prop->name, prop->child); |
| 990 | break; |
| 991 | default: |
| 992 | continue; |
| 993 | } |
| 994 | |
| 995 | ++properties_added; |
| 996 | } |
| 997 | |
| 998 | return properties_added; |
| 999 | } |
| 1000 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1001 | struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name, |
| 1002 | uint64_t value) |
| 1003 | { |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 1004 | if (!dp) |
| 1005 | return NULL; |
| 1006 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1007 | struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name); |
| 1008 | |
| 1009 | if (new) |
| 1010 | new->integer = value; |
| 1011 | |
| 1012 | return new; |
| 1013 | } |
| 1014 | |
| 1015 | struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name, |
| 1016 | const char *string) |
| 1017 | { |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 1018 | if (!dp) |
| 1019 | return NULL; |
| 1020 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1021 | struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name); |
| 1022 | |
| 1023 | if (new) |
| 1024 | new->string = string; |
| 1025 | |
| 1026 | return new; |
| 1027 | } |
| 1028 | |
| 1029 | struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name, |
| 1030 | const char *reference) |
| 1031 | { |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 1032 | if (!dp) |
| 1033 | return NULL; |
| 1034 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1035 | struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name); |
| 1036 | |
| 1037 | if (new) |
| 1038 | new->string = reference; |
| 1039 | |
| 1040 | return new; |
| 1041 | } |
| 1042 | |
| 1043 | struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name, |
| 1044 | struct acpi_dp *child) |
| 1045 | { |
| 1046 | struct acpi_dp *new; |
| 1047 | |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 1048 | if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE) |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1049 | return NULL; |
| 1050 | |
| 1051 | new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name); |
| 1052 | if (new) { |
| 1053 | new->child = child; |
| 1054 | new->string = child->name; |
| 1055 | } |
| 1056 | |
| 1057 | return new; |
| 1058 | } |
| 1059 | |
Duncan Laurie | 84fac41 | 2020-06-03 12:36:51 -0700 | [diff] [blame] | 1060 | struct acpi_dp *acpi_dp_add_package(struct acpi_dp *dp, struct acpi_dp *package) |
| 1061 | { |
| 1062 | struct acpi_dp *new; |
| 1063 | |
| 1064 | if (!dp || !package || package->type != ACPI_DP_TYPE_TABLE) |
| 1065 | return NULL; |
| 1066 | |
| 1067 | new = acpi_dp_new(dp, ACPI_DP_TYPE_PACKAGE, NULL); |
| 1068 | if (new) { |
| 1069 | new->uuid = package->name; |
| 1070 | new->child = package; |
| 1071 | } |
| 1072 | |
| 1073 | return new; |
| 1074 | } |
| 1075 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1076 | struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array) |
| 1077 | { |
| 1078 | struct acpi_dp *new; |
| 1079 | |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 1080 | if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE) |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1081 | return NULL; |
| 1082 | |
| 1083 | new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name); |
| 1084 | if (new) |
| 1085 | new->array = array; |
| 1086 | |
| 1087 | return new; |
| 1088 | } |
| 1089 | |
| 1090 | struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name, |
Duncan Laurie | ed6eb27 | 2020-04-29 11:39:08 -0700 | [diff] [blame] | 1091 | const uint64_t *array, int len) |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1092 | { |
| 1093 | struct acpi_dp *dp_array; |
| 1094 | int i; |
| 1095 | |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 1096 | if (!dp || len <= 0) |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1097 | return NULL; |
| 1098 | |
| 1099 | dp_array = acpi_dp_new_table(name); |
| 1100 | if (!dp_array) |
| 1101 | return NULL; |
| 1102 | |
| 1103 | for (i = 0; i < len; i++) |
| 1104 | if (!acpi_dp_add_integer(dp_array, NULL, array[i])) |
| 1105 | break; |
| 1106 | |
| 1107 | acpi_dp_add_array(dp, dp_array); |
| 1108 | |
| 1109 | return dp_array; |
| 1110 | } |
| 1111 | |
Karthikeyan Ramasubramanian | 685dbe1 | 2020-10-05 10:44:50 -0600 | [diff] [blame] | 1112 | struct acpi_dp *acpi_dp_add_gpio_array(struct acpi_dp *dp, const char *name, |
| 1113 | const struct acpi_gpio_res_params *params, |
| 1114 | size_t param_count) |
| 1115 | { |
| 1116 | struct acpi_dp *gpio; |
| 1117 | uint32_t i; |
| 1118 | |
| 1119 | if (!dp || !param_count) |
| 1120 | return NULL; |
| 1121 | |
| 1122 | gpio = acpi_dp_new_table(name); |
| 1123 | if (!gpio) |
| 1124 | return NULL; |
| 1125 | |
| 1126 | /* |
| 1127 | * Generate ACPI identifiers as follows: |
| 1128 | * Package () { |
| 1129 | * name, // e.g. cs-gpios |
| 1130 | * Package() { |
| 1131 | * ref, index, pin, active_low, // GPIO-0 (params[0]) |
| 1132 | * ref, index, pin, active_low, // GPIO-1 (params[1]) |
| 1133 | * ... |
| 1134 | * } |
| 1135 | * } |
| 1136 | */ |
| 1137 | for (i = 0; i < param_count; i++, params++) { |
| 1138 | /* |
| 1139 | * If refs is NULL, leave a hole in the gpio array. This can be used in |
| 1140 | * conditions where some controllers use both GPIOs and native signals. |
| 1141 | */ |
| 1142 | if (!params->ref) { |
| 1143 | acpi_dp_add_integer(gpio, NULL, 0); |
| 1144 | continue; |
| 1145 | } |
| 1146 | |
| 1147 | /* The device that has _CRS containing GpioIO()/GpioInt() */ |
| 1148 | acpi_dp_add_reference(gpio, NULL, params->ref); |
| 1149 | |
| 1150 | /* Index of the GPIO resource in _CRS starting from zero */ |
| 1151 | acpi_dp_add_integer(gpio, NULL, params->index); |
| 1152 | |
| 1153 | /* Pin in the GPIO resource, typically zero */ |
| 1154 | acpi_dp_add_integer(gpio, NULL, params->pin); |
| 1155 | |
| 1156 | /* Set if pin is active low */ |
| 1157 | acpi_dp_add_integer(gpio, NULL, params->active_low); |
| 1158 | } |
| 1159 | acpi_dp_add_array(dp, gpio); |
| 1160 | |
| 1161 | return gpio; |
| 1162 | |
| 1163 | } |
| 1164 | |
| 1165 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 1166 | struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name, |
| 1167 | const char *ref, int index, int pin, |
| 1168 | int active_low) |
| 1169 | { |
Karthikeyan Ramasubramanian | 685dbe1 | 2020-10-05 10:44:50 -0600 | [diff] [blame] | 1170 | struct acpi_gpio_res_params param = { |
| 1171 | .ref = ref, |
| 1172 | .index = index, |
| 1173 | .pin = pin, |
| 1174 | .active_low = active_low, |
| 1175 | }; |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 1176 | |
Karthikeyan Ramasubramanian | 685dbe1 | 2020-10-05 10:44:50 -0600 | [diff] [blame] | 1177 | return acpi_dp_add_gpio_array(dp, name, ¶m, 1); |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 1178 | } |
Furquan Shaikh | d1130af | 2020-04-23 12:51:42 -0700 | [diff] [blame] | 1179 | |
| 1180 | /* |
| 1181 | * This function writes a PCI device with _ADR object: |
| 1182 | * Example: |
| 1183 | * Scope (\_SB.PCI0) |
| 1184 | * { |
| 1185 | * Device (IGFX) |
| 1186 | * { |
| 1187 | * Name (_ADR, 0x0000000000000000) |
| 1188 | * Method (_STA, 0, NotSerialized) { Return (status) } |
| 1189 | * } |
| 1190 | * } |
| 1191 | */ |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 1192 | void acpi_device_write_pci_dev(const struct device *dev) |
Furquan Shaikh | d1130af | 2020-04-23 12:51:42 -0700 | [diff] [blame] | 1193 | { |
| 1194 | const char *scope = acpi_device_scope(dev); |
| 1195 | const char *name = acpi_device_name(dev); |
| 1196 | |
| 1197 | assert(dev->path.type == DEVICE_PATH_PCI); |
| 1198 | assert(name); |
| 1199 | assert(scope); |
| 1200 | |
| 1201 | acpigen_write_scope(scope); |
| 1202 | acpigen_write_device(name); |
| 1203 | |
| 1204 | acpigen_write_ADR_pci_device(dev); |
| 1205 | acpigen_write_STA(acpi_device_status(dev)); |
| 1206 | |
| 1207 | acpigen_pop_len(); /* Device */ |
| 1208 | acpigen_pop_len(); /* Scope */ |
| 1209 | } |
Kapil Porwal | ddc52a6 | 2022-11-26 19:10:57 +0530 | [diff] [blame] | 1210 | |
Kapil Porwal | 7543627 | 2022-11-28 17:25:48 +0530 | [diff] [blame] | 1211 | /* |
| 1212 | * Helper function to add given integer property with an UUID to _DSD in the current scope. |
| 1213 | * |
| 1214 | * dsd - Pointer to a _DSD object. |
| 1215 | * Append to existing _DSD object if not NULL. |
| 1216 | * Create new _DSD object and flush it if NULL. |
| 1217 | * uuid - Pointer to the UUID string. |
| 1218 | * name - Pointer to the property name string. |
| 1219 | * value - Value of the integer property. |
| 1220 | */ |
| 1221 | static void acpi_device_add_integer_property_with_uuid(struct acpi_dp *dsd, |
| 1222 | const char *uuid, |
| 1223 | const char *name, |
| 1224 | uint64_t value) |
Kapil Porwal | ddc52a6 | 2022-11-26 19:10:57 +0530 | [diff] [blame] | 1225 | { |
| 1226 | struct acpi_dp *prev_dsd = dsd, *pkg; |
| 1227 | if (prev_dsd == NULL) |
| 1228 | dsd = acpi_dp_new_table("_DSD"); |
Kapil Porwal | 7543627 | 2022-11-28 17:25:48 +0530 | [diff] [blame] | 1229 | pkg = acpi_dp_new_table(uuid); |
| 1230 | acpi_dp_add_integer(pkg, name, value); |
Kapil Porwal | ddc52a6 | 2022-11-26 19:10:57 +0530 | [diff] [blame] | 1231 | acpi_dp_add_package(dsd, pkg); |
| 1232 | if (prev_dsd == NULL) |
| 1233 | acpi_dp_write(dsd); |
| 1234 | } |
Kapil Porwal | 7543627 | 2022-11-28 17:25:48 +0530 | [diff] [blame] | 1235 | |
| 1236 | /* _DSD with ExternalFacingPort */ |
| 1237 | void acpi_device_add_external_facing_port(struct acpi_dp *dsd) |
| 1238 | { |
| 1239 | acpi_device_add_integer_property_with_uuid(dsd, |
| 1240 | ACPI_DSD_EXTERNAL_FACING_PORT_UUID, |
| 1241 | ACPI_DSD_EXTERNAL_FACING_PORT_NAME, |
| 1242 | 1); |
| 1243 | } |
| 1244 | |
| 1245 | /* _DSD with HotPlugSupportInD3 */ |
| 1246 | void acpi_device_add_hotplug_support_in_d3(struct acpi_dp *dsd) |
| 1247 | { |
| 1248 | acpi_device_add_integer_property_with_uuid(dsd, |
| 1249 | ACPI_DSD_HOTPLUG_IN_D3_UUID, |
| 1250 | ACPI_DSD_HOTPLUG_IN_D3_NAME, |
| 1251 | 1); |
| 1252 | } |
| 1253 | |
| 1254 | /* _DSD with DmaProperty */ |
| 1255 | void acpi_device_add_dma_property(struct acpi_dp *dsd) |
| 1256 | { |
| 1257 | acpi_device_add_integer_property_with_uuid(dsd, |
| 1258 | ACPI_DSD_DMA_PROPERTY_UUID, |
| 1259 | ACPI_DSD_DMA_PROPERTY_NAME, |
| 1260 | 1); |
| 1261 | } |
| 1262 | |
| 1263 | /* _DSD with StorageD3Enable */ |
| 1264 | void acpi_device_add_storage_d3_enable(struct acpi_dp *dsd) |
| 1265 | { |
| 1266 | acpi_device_add_integer_property_with_uuid(dsd, |
| 1267 | ACPI_DSD_STORAGE_D3_UUID, |
| 1268 | ACPI_DSD_STORAGE_D3_NAME, |
| 1269 | 1); |
| 1270 | } |