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