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