Patrick Georgi | 11f0079 | 2020-03-04 15:10:45 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 3 | |
| 4 | #include <string.h> |
| 5 | #include <arch/acpi.h> |
| 6 | #include <arch/acpi_device.h> |
| 7 | #include <arch/acpigen.h> |
| 8 | #include <device/device.h> |
| 9 | #include <device/path.h> |
Elyes HAOUAS | 70a03dd | 2019-12-02 20:47:50 +0100 | [diff] [blame] | 10 | #include <stdlib.h> |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 11 | #include <crc_byte.h> |
| 12 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 13 | #if CONFIG(GENERIC_GPIO_LIB) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 14 | #include <gpio.h> |
| 15 | #endif |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 16 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 17 | #define ACPI_DP_UUID "daffd814-6eba-4d8c-8a91-bc9bbf4aa301" |
| 18 | #define ACPI_DP_CHILD_UUID "dbb8e3e6-5886-4ba6-8795-1319f52a966b" |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 19 | |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 20 | /* Write empty word value and return pointer to it */ |
| 21 | static void *acpi_device_write_zero_len(void) |
| 22 | { |
| 23 | char *p = acpigen_get_current(); |
| 24 | acpigen_emit_word(0); |
| 25 | return p; |
| 26 | } |
| 27 | |
| 28 | /* Fill in length value from start to current at specified location */ |
| 29 | static void acpi_device_fill_from_len(char *ptr, char *start) |
| 30 | { |
| 31 | uint16_t len = acpigen_get_current() - start; |
| 32 | ptr[0] = len & 0xff; |
| 33 | ptr[1] = (len >> 8) & 0xff; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Fill in the length field with the value calculated from after |
| 38 | * the 16bit field to acpigen current as this length value does |
| 39 | * not include the length field itself. |
| 40 | */ |
| 41 | static void acpi_device_fill_len(void *ptr) |
| 42 | { |
| 43 | acpi_device_fill_from_len(ptr, ptr + sizeof(uint16_t)); |
| 44 | } |
| 45 | |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 46 | /* Locate and return the ACPI name for this device */ |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 47 | const char *acpi_device_name(const struct device *dev) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 48 | { |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 49 | const struct device *pdev = dev; |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 50 | const char *name = NULL; |
| 51 | |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 52 | if (!dev) |
| 53 | return NULL; |
| 54 | |
| 55 | /* Check for device specific handler */ |
| 56 | if (dev->ops->acpi_name) |
| 57 | return dev->ops->acpi_name(dev); |
| 58 | |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 59 | /* Walk up the tree to find if any parent can identify this device */ |
| 60 | while (pdev->bus) { |
| 61 | pdev = pdev->bus->dev; |
| 62 | if (!pdev) |
| 63 | break; |
| 64 | if (pdev->path.type == DEVICE_PATH_ROOT) |
| 65 | break; |
| 66 | if (pdev->ops && pdev->ops->acpi_name) |
| 67 | name = pdev->ops->acpi_name(dev); |
| 68 | if (name) |
| 69 | return name; |
| 70 | } |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Patrick Rudolph | eeb8e74 | 2019-08-20 08:20:01 +0200 | [diff] [blame] | 75 | /* Locate and return the ACPI _HID (Hardware ID) for this device */ |
| 76 | const char *acpi_device_hid(const struct device *dev) |
| 77 | { |
| 78 | if (!dev) |
| 79 | return NULL; |
| 80 | |
| 81 | /* Check for device specific handler */ |
| 82 | if (dev->ops->acpi_hid) |
| 83 | return dev->ops->acpi_hid(dev); |
| 84 | |
| 85 | /* |
| 86 | * Don't walk up the tree to find any parent that can identify this device, as |
| 87 | * PNP devices are hard to identify. |
| 88 | */ |
| 89 | |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Generate unique ID based on the ACPI path. |
| 95 | * Collisions on the same _HID are possible but very unlikely. |
| 96 | */ |
| 97 | uint32_t acpi_device_uid(struct device *dev) |
| 98 | { |
| 99 | const char *path = acpi_device_path(dev); |
| 100 | if (!path) |
| 101 | return 0; |
| 102 | |
| 103 | return CRC(path, strlen(path), crc32_byte); |
| 104 | } |
| 105 | |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 106 | /* 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] | 107 | 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] | 108 | size_t buf_len, size_t cur) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 109 | { |
| 110 | const char *name = acpi_device_name(dev); |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 111 | ssize_t next = 0; |
| 112 | |
| 113 | if (!name) |
| 114 | return -1; |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Make sure this name segment will fit, including the path segment |
| 118 | * separator and possible NUL terminator if this is the last segment. |
| 119 | */ |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 120 | if (!dev || (cur + strlen(name) + 2) > buf_len) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 121 | return cur; |
| 122 | |
| 123 | /* Walk up the tree to the root device */ |
| 124 | if (dev->path.type != DEVICE_PATH_ROOT && dev->bus && dev->bus->dev) |
| 125 | next = acpi_device_path_fill(dev->bus->dev, buf, buf_len, cur); |
Duncan Laurie | 4702914 | 2018-05-07 14:28:53 -0700 | [diff] [blame] | 126 | if (next < 0) |
| 127 | return next; |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 128 | |
| 129 | /* Fill in the path from the root device */ |
| 130 | next += snprintf(buf + next, buf_len - next, "%s%s", |
Timothy Pearson | aeb6101 | 2017-04-13 17:06:16 -0500 | [diff] [blame] | 131 | (dev->path.type == DEVICE_PATH_ROOT |
| 132 | || (strlen(name) == 0)) ? |
| 133 | "" : ".", name); |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 134 | |
| 135 | return next; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Warning: just as with dev_path() this uses a static buffer |
| 140 | * so should not be called mulitple times in one statement |
| 141 | */ |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 142 | const char *acpi_device_path(const struct device *dev) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 143 | { |
| 144 | static char buf[DEVICE_PATH_MAX] = {}; |
| 145 | |
| 146 | if (!dev) |
| 147 | return NULL; |
| 148 | |
| 149 | if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0) |
| 150 | return NULL; |
| 151 | |
| 152 | return buf; |
| 153 | } |
| 154 | |
| 155 | /* 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] | 156 | const char *acpi_device_scope(const struct device *dev) |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 157 | { |
Duncan Laurie | d02685b | 2016-06-30 14:37:37 -0700 | [diff] [blame] | 158 | static char buf[DEVICE_PATH_MAX] = {}; |
| 159 | |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 160 | if (!dev || !dev->bus || !dev->bus->dev) |
| 161 | return NULL; |
| 162 | |
Duncan Laurie | d02685b | 2016-06-30 14:37:37 -0700 | [diff] [blame] | 163 | if (acpi_device_path_fill(dev->bus->dev, buf, sizeof(buf), 0) <= 0) |
| 164 | return NULL; |
| 165 | |
| 166 | return buf; |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Elyes HAOUAS | 6716bab | 2020-01-09 21:29:25 +0100 | [diff] [blame] | 169 | /* Concatenate the device path and provided name suffix */ |
Aamir Bohra | e825d3f | 2019-07-30 10:11:41 +0530 | [diff] [blame] | 170 | 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] | 171 | { |
| 172 | static char buf[DEVICE_PATH_MAX] = {}; |
Jacob Garber | f2ba2d9 | 2019-07-02 10:50:10 -0600 | [diff] [blame] | 173 | ssize_t len; |
Duncan Laurie | d9af3ce | 2016-05-08 18:15:25 -0700 | [diff] [blame] | 174 | |
| 175 | if (!dev) |
| 176 | return NULL; |
| 177 | |
| 178 | /* Build the path of this device */ |
| 179 | len = acpi_device_path_fill(dev, buf, sizeof(buf), 0); |
| 180 | if (len <= 0) |
| 181 | return NULL; |
| 182 | |
| 183 | /* Ensure there is room for the added name, separator, and NUL */ |
| 184 | if ((len + strlen(name) + 2) > sizeof(buf)) |
| 185 | return NULL; |
| 186 | snprintf(buf + len, sizeof(buf) - len, ".%s", name); |
| 187 | |
| 188 | return buf; |
| 189 | } |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 190 | |
Hung-Te Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame] | 191 | int acpi_device_status(const struct device *dev) |
| 192 | { |
| 193 | if (!dev->enabled) |
| 194 | return ACPI_STATUS_DEVICE_ALL_OFF; |
| 195 | if (dev->hidden) |
| 196 | return ACPI_STATUS_DEVICE_HIDDEN_ON; |
| 197 | return ACPI_STATUS_DEVICE_ALL_ON; |
| 198 | } |
| 199 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 200 | |
| 201 | /* Write the unique _UID based on ACPI device path. */ |
| 202 | void acpi_device_write_uid(struct device *dev) |
| 203 | { |
| 204 | acpigen_write_name_integer("_UID", acpi_device_uid(dev)); |
| 205 | } |
| 206 | |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 207 | /* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */ |
| 208 | void acpi_device_write_interrupt(const struct acpi_irq *irq) |
| 209 | { |
| 210 | void *desc_length; |
| 211 | uint8_t flags; |
| 212 | |
| 213 | if (!irq || !irq->pin) |
| 214 | return; |
| 215 | |
| 216 | /* This is supported by GpioInt() but not Interrupt() */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 217 | if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 218 | return; |
| 219 | |
| 220 | /* Byte 0: Descriptor Type */ |
| 221 | acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT); |
| 222 | |
| 223 | /* Byte 1-2: Length (filled in later) */ |
| 224 | desc_length = acpi_device_write_zero_len(); |
| 225 | |
| 226 | /* |
| 227 | * Byte 3: Flags |
| 228 | * [7:5]: Reserved |
| 229 | * [4]: Wake (0=NO_WAKE 1=WAKE) |
| 230 | * [3]: Sharing (0=EXCLUSIVE 1=SHARED) |
| 231 | * [2]: Polarity (0=HIGH 1=LOW) |
| 232 | * [1]: Mode (0=LEVEL 1=EDGE) |
| 233 | * [0]: Resource (0=PRODUCER 1=CONSUMER) |
| 234 | */ |
| 235 | flags = 1 << 0; /* ResourceConsumer */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 236 | if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 237 | flags |= 1 << 1; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 238 | if (irq->polarity == ACPI_IRQ_ACTIVE_LOW) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 239 | flags |= 1 << 2; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 240 | if (irq->shared == ACPI_IRQ_SHARED) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 241 | flags |= 1 << 3; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 242 | if (irq->wake == ACPI_IRQ_WAKE) |
Duncan Laurie | 6b7c1f6 | 2016-05-09 15:38:36 -0700 | [diff] [blame] | 243 | flags |= 1 << 4; |
| 244 | acpigen_emit_byte(flags); |
| 245 | |
| 246 | /* Byte 4: Interrupt Table Entry Count */ |
| 247 | acpigen_emit_byte(1); |
| 248 | |
| 249 | /* Byte 5-8: Interrupt Number */ |
| 250 | acpigen_emit_dword(irq->pin); |
| 251 | |
| 252 | /* Fill in Descriptor Length (account for len word) */ |
| 253 | acpi_device_fill_len(desc_length); |
| 254 | } |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 255 | |
| 256 | /* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */ |
| 257 | void acpi_device_write_gpio(const struct acpi_gpio *gpio) |
| 258 | { |
| 259 | void *start, *desc_length; |
| 260 | void *pin_table_offset, *vendor_data_offset, *resource_offset; |
| 261 | uint16_t flags = 0; |
| 262 | int pin; |
| 263 | |
| 264 | if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO) |
| 265 | return; |
| 266 | |
| 267 | start = acpigen_get_current(); |
| 268 | |
| 269 | /* Byte 0: Descriptor Type */ |
| 270 | acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO); |
| 271 | |
| 272 | /* Byte 1-2: Length (fill in later) */ |
| 273 | desc_length = acpi_device_write_zero_len(); |
| 274 | |
| 275 | /* Byte 3: Revision ID */ |
| 276 | acpigen_emit_byte(ACPI_GPIO_REVISION_ID); |
| 277 | |
| 278 | /* Byte 4: GpioIo or GpioInt */ |
| 279 | acpigen_emit_byte(gpio->type); |
| 280 | |
| 281 | /* |
| 282 | * Byte 5-6: General Flags |
| 283 | * [15:1]: 0 => Reserved |
| 284 | * [0]: 1 => ResourceConsumer |
| 285 | */ |
| 286 | acpigen_emit_word(1 << 0); |
| 287 | |
| 288 | switch (gpio->type) { |
| 289 | case ACPI_GPIO_TYPE_INTERRUPT: |
| 290 | /* |
| 291 | * Byte 7-8: GPIO Interrupt Flags |
| 292 | * [15:5]: 0 => Reserved |
| 293 | * [4]: Wake (0=NO_WAKE 1=WAKE) |
| 294 | * [3]: Sharing (0=EXCLUSIVE 1=SHARED) |
| 295 | * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH) |
| 296 | * [0]: Mode (0=LEVEL 1=EDGE) |
| 297 | */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 298 | if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 299 | flags |= 1 << 0; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 300 | if (gpio->irq.shared == ACPI_IRQ_SHARED) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 301 | flags |= 1 << 3; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 302 | if (gpio->irq.wake == ACPI_IRQ_WAKE) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 303 | flags |= 1 << 4; |
| 304 | |
| 305 | switch (gpio->irq.polarity) { |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 306 | case ACPI_IRQ_ACTIVE_HIGH: |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 307 | flags |= 0 << 1; |
| 308 | break; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 309 | case ACPI_IRQ_ACTIVE_LOW: |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 310 | flags |= 1 << 1; |
| 311 | break; |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 312 | case ACPI_IRQ_ACTIVE_BOTH: |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 313 | flags |= 2 << 1; |
| 314 | break; |
| 315 | } |
| 316 | break; |
| 317 | |
| 318 | case ACPI_GPIO_TYPE_IO: |
| 319 | /* |
| 320 | * Byte 7-8: GPIO IO Flags |
| 321 | * [15:4]: 0 => Reserved |
| 322 | * [3]: Sharing (0=EXCLUSIVE 1=SHARED) |
| 323 | * [2]: 0 => Reserved |
| 324 | * [1:0]: IO Restriction |
| 325 | * 0 => IoRestrictionNone |
| 326 | * 1 => IoRestrictionInputOnly |
| 327 | * 2 => IoRestrictionOutputOnly |
| 328 | * 3 => IoRestrictionNoneAndPreserve |
| 329 | */ |
| 330 | flags |= gpio->io_restrict & 3; |
| 331 | if (gpio->io_shared) |
| 332 | flags |= 1 << 3; |
| 333 | break; |
| 334 | } |
| 335 | acpigen_emit_word(flags); |
| 336 | |
| 337 | /* |
| 338 | * Byte 9: Pin Configuration |
| 339 | * 0x01 => Default (no configuration applied) |
| 340 | * 0x02 => Pull-up |
| 341 | * 0x03 => Pull-down |
| 342 | * 0x04-0x7F => Reserved |
| 343 | * 0x80-0xff => Vendor defined |
| 344 | */ |
| 345 | acpigen_emit_byte(gpio->pull); |
| 346 | |
| 347 | /* Byte 10-11: Output Drive Strength in 1/100 mA */ |
| 348 | acpigen_emit_word(gpio->output_drive_strength); |
| 349 | |
| 350 | /* Byte 12-13: Debounce Timeout in 1/100 ms */ |
| 351 | acpigen_emit_word(gpio->interrupt_debounce_timeout); |
| 352 | |
| 353 | /* Byte 14-15: Pin Table Offset, relative to start */ |
| 354 | pin_table_offset = acpi_device_write_zero_len(); |
| 355 | |
| 356 | /* Byte 16: Reserved */ |
| 357 | acpigen_emit_byte(0); |
| 358 | |
| 359 | /* Byte 17-18: Resource Source Name Offset, relative to start */ |
| 360 | resource_offset = acpi_device_write_zero_len(); |
| 361 | |
| 362 | /* Byte 19-20: Vendor Data Offset, relative to start */ |
| 363 | vendor_data_offset = acpi_device_write_zero_len(); |
| 364 | |
| 365 | /* Byte 21-22: Vendor Data Length */ |
| 366 | acpigen_emit_word(0); |
| 367 | |
| 368 | /* Fill in Pin Table Offset */ |
| 369 | acpi_device_fill_from_len(pin_table_offset, start); |
| 370 | |
| 371 | /* Pin Table, one word for each pin */ |
Duncan Laurie | 5b6c28c | 2016-06-29 10:47:22 -0700 | [diff] [blame] | 372 | for (pin = 0; pin < gpio->pin_count; pin++) { |
| 373 | uint16_t acpi_pin = gpio->pins[pin]; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 374 | #if CONFIG(GENERIC_GPIO_LIB) |
Duncan Laurie | 5b6c28c | 2016-06-29 10:47:22 -0700 | [diff] [blame] | 375 | acpi_pin = gpio_acpi_pin(acpi_pin); |
| 376 | #endif |
| 377 | acpigen_emit_word(acpi_pin); |
| 378 | } |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 379 | |
| 380 | /* Fill in Resource Source Name Offset */ |
| 381 | acpi_device_fill_from_len(resource_offset, start); |
| 382 | |
| 383 | /* Resource Source Name String */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 384 | #if CONFIG(GENERIC_GPIO_LIB) |
Duncan Laurie | cfb6ea7 | 2016-05-09 17:08:38 -0700 | [diff] [blame] | 385 | acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0])); |
| 386 | #else |
| 387 | acpigen_emit_string(gpio->resource); |
| 388 | #endif |
| 389 | |
| 390 | /* Fill in Vendor Data Offset */ |
| 391 | acpi_device_fill_from_len(vendor_data_offset, start); |
| 392 | |
| 393 | /* Fill in GPIO Descriptor Length (account for len word) */ |
| 394 | acpi_device_fill_len(desc_length); |
| 395 | } |
Duncan Laurie | 1010b4a | 2016-05-09 20:10:47 -0700 | [diff] [blame] | 396 | |
| 397 | /* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */ |
| 398 | void acpi_device_write_i2c(const struct acpi_i2c *i2c) |
| 399 | { |
| 400 | void *desc_length, *type_length; |
| 401 | |
| 402 | /* Byte 0: Descriptor Type */ |
| 403 | acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS); |
| 404 | |
| 405 | /* Byte 1+2: Length (filled in later) */ |
| 406 | desc_length = acpi_device_write_zero_len(); |
| 407 | |
| 408 | /* Byte 3: Revision ID */ |
Elyes HAOUAS | 34564ed | 2019-04-16 08:12:10 +0200 | [diff] [blame] | 409 | acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID); |
Duncan Laurie | 1010b4a | 2016-05-09 20:10:47 -0700 | [diff] [blame] | 410 | |
| 411 | /* Byte 4: Resource Source Index is Reserved */ |
| 412 | acpigen_emit_byte(0); |
| 413 | |
| 414 | /* Byte 5: Serial Bus Type is I2C */ |
| 415 | acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C); |
| 416 | |
| 417 | /* |
| 418 | * Byte 6: Flags |
| 419 | * [7:2]: 0 => Reserved |
| 420 | * [1]: 1 => ResourceConsumer |
| 421 | * [0]: 0 => ControllerInitiated |
| 422 | */ |
| 423 | acpigen_emit_byte(1 << 1); |
| 424 | |
| 425 | /* |
| 426 | * Byte 7-8: Type Specific Flags |
| 427 | * [15:1]: 0 => Reserved |
| 428 | * [0]: 0 => 7bit, 1 => 10bit |
| 429 | */ |
| 430 | acpigen_emit_word(i2c->mode_10bit); |
| 431 | |
| 432 | /* Byte 9: Type Specific Revision ID */ |
Elyes HAOUAS | 34564ed | 2019-04-16 08:12:10 +0200 | [diff] [blame] | 433 | acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID); |
Duncan Laurie | 1010b4a | 2016-05-09 20:10:47 -0700 | [diff] [blame] | 434 | |
| 435 | /* Byte 10-11: I2C Type Data Length */ |
| 436 | type_length = acpi_device_write_zero_len(); |
| 437 | |
| 438 | /* Byte 12-15: I2C Bus Speed */ |
| 439 | acpigen_emit_dword(i2c->speed); |
| 440 | |
| 441 | /* Byte 16-17: I2C Slave Address */ |
| 442 | acpigen_emit_word(i2c->address); |
| 443 | |
| 444 | /* Fill in Type Data Length */ |
| 445 | acpi_device_fill_len(type_length); |
| 446 | |
| 447 | /* Byte 18+: ResourceSource */ |
| 448 | acpigen_emit_string(i2c->resource); |
| 449 | |
| 450 | /* Fill in I2C Descriptor Length */ |
| 451 | acpi_device_fill_len(desc_length); |
| 452 | } |
Duncan Laurie | 70c86d9 | 2016-05-10 07:26:34 -0700 | [diff] [blame] | 453 | |
| 454 | /* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */ |
| 455 | void acpi_device_write_spi(const struct acpi_spi *spi) |
| 456 | { |
| 457 | void *desc_length, *type_length; |
| 458 | uint16_t flags = 0; |
| 459 | |
| 460 | /* Byte 0: Descriptor Type */ |
| 461 | acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS); |
| 462 | |
| 463 | /* Byte 1+2: Length (filled in later) */ |
| 464 | desc_length = acpi_device_write_zero_len(); |
| 465 | |
| 466 | /* Byte 3: Revision ID */ |
Elyes HAOUAS | 34564ed | 2019-04-16 08:12:10 +0200 | [diff] [blame] | 467 | acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID); |
Duncan Laurie | 70c86d9 | 2016-05-10 07:26:34 -0700 | [diff] [blame] | 468 | |
| 469 | /* Byte 4: Resource Source Index is Reserved */ |
| 470 | acpigen_emit_byte(0); |
| 471 | |
| 472 | /* Byte 5: Serial Bus Type is SPI */ |
| 473 | acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI); |
| 474 | |
| 475 | /* |
| 476 | * Byte 6: Flags |
| 477 | * [7:2]: 0 => Reserved |
| 478 | * [1]: 1 => ResourceConsumer |
| 479 | * [0]: 0 => ControllerInitiated |
| 480 | */ |
| 481 | acpigen_emit_byte(1 << 1); |
| 482 | |
| 483 | /* |
| 484 | * Byte 7-8: Type Specific Flags |
| 485 | * [15:2]: 0 => Reserved |
| 486 | * [1]: 0 => ActiveLow, 1 => ActiveHigh |
| 487 | * [0]: 0 => FourWire, 1 => ThreeWire |
| 488 | */ |
| 489 | if (spi->wire_mode == SPI_3_WIRE_MODE) |
| 490 | flags |= 1 << 0; |
| 491 | if (spi->device_select_polarity == SPI_POLARITY_HIGH) |
| 492 | flags |= 1 << 1; |
| 493 | acpigen_emit_word(flags); |
| 494 | |
| 495 | /* Byte 9: Type Specific Revision ID */ |
Elyes HAOUAS | 34564ed | 2019-04-16 08:12:10 +0200 | [diff] [blame] | 496 | acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID); |
Duncan Laurie | 70c86d9 | 2016-05-10 07:26:34 -0700 | [diff] [blame] | 497 | |
| 498 | /* Byte 10-11: SPI Type Data Length */ |
| 499 | type_length = acpi_device_write_zero_len(); |
| 500 | |
| 501 | /* Byte 12-15: Connection Speed */ |
| 502 | acpigen_emit_dword(spi->speed); |
| 503 | |
| 504 | /* Byte 16: Data Bit Length */ |
| 505 | acpigen_emit_byte(spi->data_bit_length); |
| 506 | |
| 507 | /* Byte 17: Clock Phase */ |
| 508 | acpigen_emit_byte(spi->clock_phase); |
| 509 | |
| 510 | /* Byte 18: Clock Polarity */ |
| 511 | acpigen_emit_byte(spi->clock_polarity); |
| 512 | |
| 513 | /* Byte 19-20: Device Selection */ |
| 514 | acpigen_emit_word(spi->device_select); |
| 515 | |
| 516 | /* Fill in Type Data Length */ |
| 517 | acpi_device_fill_len(type_length); |
| 518 | |
| 519 | /* Byte 21+: ResourceSource String */ |
| 520 | acpigen_emit_string(spi->resource); |
| 521 | |
| 522 | /* Fill in SPI Descriptor Length */ |
| 523 | acpi_device_fill_len(desc_length); |
| 524 | } |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 525 | |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 526 | /* PowerResource() with Enable and/or Reset control */ |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 527 | 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] | 528 | { |
Lee Leahy | 0b5678f | 2017-03-16 16:01:40 -0700 | [diff] [blame] | 529 | static const char *power_res_dev_states[] = { "_PR0", "_PR3" }; |
Edward O'Callaghan | 7e26255 | 2020-01-23 10:32:33 +1100 | [diff] [blame] | 530 | unsigned int reset_gpio = params->reset_gpio ? params->reset_gpio->pins[0] : 0; |
| 531 | unsigned int enable_gpio = params->enable_gpio ? params->enable_gpio->pins[0] : 0; |
| 532 | unsigned int stop_gpio = params->stop_gpio ? params->stop_gpio->pins[0] : 0; |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 533 | |
Furquan Shaikh | edf459f | 2017-08-28 17:20:49 -0700 | [diff] [blame] | 534 | if (!reset_gpio && !enable_gpio && !stop_gpio) |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 535 | return; |
| 536 | |
| 537 | /* PowerResource (PRIC, 0, 0) */ |
| 538 | acpigen_write_power_res("PRIC", 0, 0, power_res_dev_states, |
| 539 | ARRAY_SIZE(power_res_dev_states)); |
| 540 | |
| 541 | /* Method (_STA, 0, NotSerialized) { Return (0x1) } */ |
| 542 | acpigen_write_STA(0x1); |
| 543 | |
| 544 | /* Method (_ON, 0, Serialized) */ |
| 545 | acpigen_write_method_serialized("_ON", 0); |
| 546 | if (reset_gpio) |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 547 | acpigen_enable_tx_gpio(params->reset_gpio); |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 548 | if (enable_gpio) { |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 549 | acpigen_enable_tx_gpio(params->enable_gpio); |
| 550 | if (params->enable_delay_ms) |
| 551 | acpigen_write_sleep(params->enable_delay_ms); |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 552 | } |
| 553 | if (reset_gpio) { |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 554 | acpigen_disable_tx_gpio(params->reset_gpio); |
| 555 | if (params->reset_delay_ms) |
| 556 | acpigen_write_sleep(params->reset_delay_ms); |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 557 | } |
Furquan Shaikh | edf459f | 2017-08-28 17:20:49 -0700 | [diff] [blame] | 558 | if (stop_gpio) { |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 559 | acpigen_disable_tx_gpio(params->stop_gpio); |
| 560 | if (params->stop_delay_ms) |
| 561 | acpigen_write_sleep(params->stop_delay_ms); |
Furquan Shaikh | edf459f | 2017-08-28 17:20:49 -0700 | [diff] [blame] | 562 | } |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 563 | acpigen_pop_len(); /* _ON method */ |
| 564 | |
| 565 | /* Method (_OFF, 0, Serialized) */ |
| 566 | acpigen_write_method_serialized("_OFF", 0); |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 567 | if (stop_gpio) { |
| 568 | acpigen_enable_tx_gpio(params->stop_gpio); |
| 569 | if (params->stop_off_delay_ms) |
| 570 | acpigen_write_sleep(params->stop_off_delay_ms); |
| 571 | } |
| 572 | if (reset_gpio) { |
| 573 | acpigen_enable_tx_gpio(params->reset_gpio); |
| 574 | if (params->reset_off_delay_ms) |
| 575 | acpigen_write_sleep(params->reset_off_delay_ms); |
| 576 | } |
| 577 | if (enable_gpio) { |
| 578 | acpigen_disable_tx_gpio(params->enable_gpio); |
| 579 | if (params->enable_off_delay_ms) |
| 580 | acpigen_write_sleep(params->enable_off_delay_ms); |
| 581 | } |
Duncan Laurie | bd73dbb | 2017-02-17 17:05:03 -0800 | [diff] [blame] | 582 | acpigen_pop_len(); /* _OFF method */ |
| 583 | |
| 584 | acpigen_pop_len(); /* PowerResource PRIC */ |
| 585 | } |
| 586 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 587 | static void acpi_dp_write_array(const struct acpi_dp *array); |
| 588 | static void acpi_dp_write_value(const struct acpi_dp *prop) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 589 | { |
| 590 | switch (prop->type) { |
| 591 | case ACPI_DP_TYPE_INTEGER: |
| 592 | acpigen_write_integer(prop->integer); |
| 593 | break; |
| 594 | case ACPI_DP_TYPE_STRING: |
Harsha Priya | 3a96ac4 | 2016-07-15 17:31:43 -0700 | [diff] [blame] | 595 | case ACPI_DP_TYPE_CHILD: |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 596 | acpigen_write_string(prop->string); |
| 597 | break; |
| 598 | case ACPI_DP_TYPE_REFERENCE: |
| 599 | acpigen_emit_namestring(prop->string); |
| 600 | break; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 601 | case ACPI_DP_TYPE_ARRAY: |
| 602 | acpi_dp_write_array(prop->array); |
| 603 | break; |
| 604 | default: |
| 605 | break; |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 609 | /* Package (2) { "prop->name", VALUE } */ |
| 610 | static void acpi_dp_write_property(const struct acpi_dp *prop) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 611 | { |
| 612 | acpigen_write_package(2); |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 613 | acpigen_write_string(prop->name); |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 614 | acpi_dp_write_value(prop); |
| 615 | acpigen_pop_len(); |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* Write array of Device Properties */ |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 619 | static void acpi_dp_write_array(const struct acpi_dp *array) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 620 | { |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 621 | const struct acpi_dp *dp; |
| 622 | char *pkg_count; |
| 623 | |
| 624 | /* Package element count determined as it is populated */ |
| 625 | pkg_count = acpigen_write_package(0); |
| 626 | |
Furquan Shaikh | 35c01bc | 2016-10-03 23:30:14 -0700 | [diff] [blame] | 627 | /* |
| 628 | * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array. |
| 629 | * DP_TYPE_TABLE does not have a value to be written. Thus, start |
| 630 | * the loop from next type in the array. |
| 631 | */ |
| 632 | for (dp = array->next; dp; dp = dp->next) { |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 633 | acpi_dp_write_value(dp); |
| 634 | (*pkg_count)++; |
| 635 | } |
| 636 | |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 637 | acpigen_pop_len(); |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 640 | static void acpi_dp_free(struct acpi_dp *dp) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 641 | { |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 642 | while (dp) { |
| 643 | struct acpi_dp *p = dp->next; |
| 644 | |
| 645 | switch (dp->type) { |
| 646 | case ACPI_DP_TYPE_CHILD: |
| 647 | acpi_dp_free(dp->child); |
| 648 | break; |
| 649 | case ACPI_DP_TYPE_ARRAY: |
| 650 | acpi_dp_free(dp->array); |
| 651 | break; |
| 652 | default: |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | free(dp); |
| 657 | dp = p; |
| 658 | } |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 661 | void acpi_dp_write(struct acpi_dp *table) |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 662 | { |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 663 | struct acpi_dp *dp, *prop; |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 664 | char *dp_count, *prop_count = NULL; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 665 | int child_count = 0; |
| 666 | |
| 667 | if (!table || table->type != ACPI_DP_TYPE_TABLE) |
| 668 | return; |
| 669 | |
| 670 | /* Name (name) */ |
| 671 | acpigen_write_name(table->name); |
| 672 | |
| 673 | /* Device Property list starts with the next entry */ |
| 674 | prop = table->next; |
| 675 | |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 676 | /* Package (DP), default to assuming no properties or children */ |
| 677 | dp_count = acpigen_write_package(0); |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 678 | |
| 679 | /* Print base properties */ |
| 680 | for (dp = prop; dp; dp = dp->next) { |
| 681 | if (dp->type == ACPI_DP_TYPE_CHILD) { |
| 682 | child_count++; |
| 683 | } else { |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 684 | /* |
| 685 | * The UUID and package is only added when |
| 686 | * we come across the first property. This |
| 687 | * is to avoid creating a zero-length package |
| 688 | * in situations where there are only children. |
| 689 | */ |
| 690 | if (!prop_count) { |
| 691 | *dp_count += 2; |
| 692 | /* ToUUID (ACPI_DP_UUID) */ |
| 693 | acpigen_write_uuid(ACPI_DP_UUID); |
| 694 | /* |
| 695 | * Package (PROP), element count determined as |
| 696 | * it is populated |
| 697 | */ |
| 698 | prop_count = acpigen_write_package(0); |
| 699 | } |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 700 | (*prop_count)++; |
| 701 | acpi_dp_write_property(dp); |
| 702 | } |
| 703 | } |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 704 | if (prop_count) { |
| 705 | /* Package (PROP) length, if a package was written */ |
| 706 | acpigen_pop_len(); |
| 707 | } |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 708 | |
| 709 | if (child_count) { |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 710 | /* Update DP package count to 2 or 4 */ |
| 711 | *dp_count += 2; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 712 | /* ToUUID (ACPI_DP_CHILD_UUID) */ |
| 713 | acpigen_write_uuid(ACPI_DP_CHILD_UUID); |
| 714 | |
| 715 | /* Print child pointer properties */ |
| 716 | acpigen_write_package(child_count); |
| 717 | |
| 718 | for (dp = prop; dp; dp = dp->next) |
| 719 | if (dp->type == ACPI_DP_TYPE_CHILD) |
| 720 | acpi_dp_write_property(dp); |
Matt Delco | 0825888 | 2019-01-30 11:16:08 -0800 | [diff] [blame] | 721 | /* Package (CHILD) length */ |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 722 | acpigen_pop_len(); |
| 723 | } |
| 724 | |
| 725 | /* Package (DP) length */ |
| 726 | acpigen_pop_len(); |
| 727 | |
| 728 | /* Recursively parse children into separate tables */ |
| 729 | for (dp = prop; dp; dp = dp->next) |
| 730 | if (dp->type == ACPI_DP_TYPE_CHILD) |
| 731 | acpi_dp_write(dp->child); |
| 732 | |
| 733 | /* Clean up */ |
| 734 | acpi_dp_free(table); |
| 735 | } |
| 736 | |
| 737 | static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type, |
| 738 | const char *name) |
| 739 | { |
| 740 | struct acpi_dp *new; |
| 741 | |
| 742 | new = malloc(sizeof(struct acpi_dp)); |
| 743 | if (!new) |
| 744 | return NULL; |
| 745 | |
| 746 | memset(new, 0, sizeof(*new)); |
| 747 | new->type = type; |
| 748 | new->name = name; |
| 749 | |
| 750 | if (dp) { |
| 751 | /* Add to end of property list */ |
| 752 | while (dp->next) |
| 753 | dp = dp->next; |
| 754 | dp->next = new; |
| 755 | } |
| 756 | |
| 757 | return new; |
| 758 | } |
| 759 | |
| 760 | struct acpi_dp *acpi_dp_new_table(const char *name) |
| 761 | { |
| 762 | return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name); |
| 763 | } |
| 764 | |
Duncan Laurie | b3023b6 | 2017-08-29 08:26:50 -0700 | [diff] [blame] | 765 | size_t acpi_dp_add_property_list(struct acpi_dp *dp, |
| 766 | const struct acpi_dp *property_list, |
| 767 | size_t property_count) |
| 768 | { |
| 769 | const struct acpi_dp *prop; |
| 770 | size_t i, properties_added = 0; |
| 771 | |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 772 | if (!dp || !property_list) |
| 773 | return 0; |
| 774 | |
Duncan Laurie | b3023b6 | 2017-08-29 08:26:50 -0700 | [diff] [blame] | 775 | for (i = 0; i < property_count; i++) { |
| 776 | prop = &property_list[i]; |
| 777 | |
| 778 | if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name) |
| 779 | continue; |
| 780 | |
| 781 | switch (prop->type) { |
| 782 | case ACPI_DP_TYPE_INTEGER: |
| 783 | acpi_dp_add_integer(dp, prop->name, prop->integer); |
| 784 | break; |
| 785 | case ACPI_DP_TYPE_STRING: |
| 786 | acpi_dp_add_string(dp, prop->name, prop->string); |
| 787 | break; |
| 788 | case ACPI_DP_TYPE_REFERENCE: |
| 789 | acpi_dp_add_reference(dp, prop->name, prop->string); |
| 790 | break; |
| 791 | case ACPI_DP_TYPE_ARRAY: |
| 792 | acpi_dp_add_array(dp, prop->array); |
| 793 | break; |
| 794 | case ACPI_DP_TYPE_CHILD: |
| 795 | acpi_dp_add_child(dp, prop->name, prop->child); |
| 796 | break; |
| 797 | default: |
| 798 | continue; |
| 799 | } |
| 800 | |
| 801 | ++properties_added; |
| 802 | } |
| 803 | |
| 804 | return properties_added; |
| 805 | } |
| 806 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 807 | struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name, |
| 808 | uint64_t value) |
| 809 | { |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 810 | if (!dp) |
| 811 | return NULL; |
| 812 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 813 | struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name); |
| 814 | |
| 815 | if (new) |
| 816 | new->integer = value; |
| 817 | |
| 818 | return new; |
| 819 | } |
| 820 | |
| 821 | struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name, |
| 822 | const char *string) |
| 823 | { |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 824 | if (!dp) |
| 825 | return NULL; |
| 826 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 827 | struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name); |
| 828 | |
| 829 | if (new) |
| 830 | new->string = string; |
| 831 | |
| 832 | return new; |
| 833 | } |
| 834 | |
| 835 | struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name, |
| 836 | const char *reference) |
| 837 | { |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 838 | if (!dp) |
| 839 | return NULL; |
| 840 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 841 | struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name); |
| 842 | |
| 843 | if (new) |
| 844 | new->string = reference; |
| 845 | |
| 846 | return new; |
| 847 | } |
| 848 | |
| 849 | struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name, |
| 850 | struct acpi_dp *child) |
| 851 | { |
| 852 | struct acpi_dp *new; |
| 853 | |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 854 | if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE) |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 855 | return NULL; |
| 856 | |
| 857 | new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name); |
| 858 | if (new) { |
| 859 | new->child = child; |
| 860 | new->string = child->name; |
| 861 | } |
| 862 | |
| 863 | return new; |
| 864 | } |
| 865 | |
| 866 | struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array) |
| 867 | { |
| 868 | struct acpi_dp *new; |
| 869 | |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 870 | if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE) |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 871 | return NULL; |
| 872 | |
| 873 | new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name); |
| 874 | if (new) |
| 875 | new->array = array; |
| 876 | |
| 877 | return new; |
| 878 | } |
| 879 | |
| 880 | struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name, |
| 881 | uint64_t *array, int len) |
| 882 | { |
| 883 | struct acpi_dp *dp_array; |
| 884 | int i; |
| 885 | |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 886 | if (!dp || len <= 0) |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 887 | return NULL; |
| 888 | |
| 889 | dp_array = acpi_dp_new_table(name); |
| 890 | if (!dp_array) |
| 891 | return NULL; |
| 892 | |
| 893 | for (i = 0; i < len; i++) |
| 894 | if (!acpi_dp_add_integer(dp_array, NULL, array[i])) |
| 895 | break; |
| 896 | |
| 897 | acpi_dp_add_array(dp, dp_array); |
| 898 | |
| 899 | return dp_array; |
| 900 | } |
| 901 | |
| 902 | struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name, |
| 903 | const char *ref, int index, int pin, |
| 904 | int active_low) |
| 905 | { |
Jacob Garber | c30e590 | 2019-05-23 14:34:58 -0600 | [diff] [blame] | 906 | if (!dp) |
| 907 | return NULL; |
| 908 | |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 909 | struct acpi_dp *gpio = acpi_dp_new_table(name); |
| 910 | |
| 911 | if (!gpio) |
| 912 | return NULL; |
| 913 | |
| 914 | /* The device that has _CRS containing GpioIO()/GpioInt() */ |
| 915 | acpi_dp_add_reference(gpio, NULL, ref); |
| 916 | |
| 917 | /* Index of the GPIO resource in _CRS starting from zero */ |
| 918 | acpi_dp_add_integer(gpio, NULL, index); |
| 919 | |
| 920 | /* Pin in the GPIO resource, typically zero */ |
| 921 | acpi_dp_add_integer(gpio, NULL, pin); |
| 922 | |
| 923 | /* Set if pin is active low */ |
| 924 | acpi_dp_add_integer(gpio, NULL, active_low); |
| 925 | |
| 926 | acpi_dp_add_array(dp, gpio); |
| 927 | |
| 928 | return gpio; |
Duncan Laurie | 559e947 | 2016-05-10 13:18:17 -0700 | [diff] [blame] | 929 | } |