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