blob: 2b085b61a64ca67dd2e4816d5a443d1ec52095b3 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Lauried9af3ce2016-05-08 18:15:25 -07002
Furquan Shaikhd1130af2020-04-23 12:51:42 -07003#include <assert.h>
Duncan Lauried9af3ce2016-05-08 18:15:25 -07004#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
6#include <acpi/acpi_device.h>
7#include <acpi/acpigen.h>
Tim Wawrzynczakd40a4c22021-02-25 13:14:49 -07008#include <acpi/acpigen_pci.h>
Duncan Lauried9af3ce2016-05-08 18:15:25 -07009#include <device/device.h>
10#include <device/path.h>
Elyes HAOUAS70a03dd2019-12-02 20:47:50 +010011#include <stdlib.h>
Elyes HAOUASa83a7db2020-07-22 11:42:53 +020012#include <types.h>
Patrick Rudolphc83bab62019-12-13 12:16:06 +010013#include <crc_byte.h>
14
Julius Wernercd49cce2019-03-05 16:53:33 -080015#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -070016#include <gpio.h>
17#endif
Duncan Lauried9af3ce2016-05-08 18:15:25 -070018
Duncan Laurieffc99902016-07-02 19:56:06 -070019#define ACPI_DP_UUID "daffd814-6eba-4d8c-8a91-bc9bbf4aa301"
20#define ACPI_DP_CHILD_UUID "dbb8e3e6-5886-4ba6-8795-1319f52a966b"
Duncan Laurie559e9472016-05-10 13:18:17 -070021
Duncan Laurie6b7c1f62016-05-09 15:38:36 -070022/* Write empty word value and return pointer to it */
23static void *acpi_device_write_zero_len(void)
24{
25 char *p = acpigen_get_current();
26 acpigen_emit_word(0);
27 return p;
28}
29
30/* Fill in length value from start to current at specified location */
31static void acpi_device_fill_from_len(char *ptr, char *start)
32{
33 uint16_t len = acpigen_get_current() - start;
34 ptr[0] = len & 0xff;
35 ptr[1] = (len >> 8) & 0xff;
36}
37
38/*
39 * Fill in the length field with the value calculated from after
40 * the 16bit field to acpigen current as this length value does
41 * not include the length field itself.
42 */
43static void acpi_device_fill_len(void *ptr)
44{
45 acpi_device_fill_from_len(ptr, ptr + sizeof(uint16_t));
46}
47
Duncan Lauried9af3ce2016-05-08 18:15:25 -070048/* Locate and return the ACPI name for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +053049const char *acpi_device_name(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -070050{
Aamir Bohrae825d3f2019-07-30 10:11:41 +053051 const struct device *pdev = dev;
Duncan Laurie47029142018-05-07 14:28:53 -070052 const char *name = NULL;
53
Duncan Lauried9af3ce2016-05-08 18:15:25 -070054 if (!dev)
55 return NULL;
56
57 /* Check for device specific handler */
Duncan Laurie84fac412020-06-03 12:36:51 -070058 if (dev->ops && dev->ops->acpi_name)
Duncan Lauried9af3ce2016-05-08 18:15:25 -070059 return dev->ops->acpi_name(dev);
60
Duncan Laurie47029142018-05-07 14:28:53 -070061 /* Walk up the tree to find if any parent can identify this device */
62 while (pdev->bus) {
63 pdev = pdev->bus->dev;
64 if (!pdev)
65 break;
66 if (pdev->path.type == DEVICE_PATH_ROOT)
67 break;
68 if (pdev->ops && pdev->ops->acpi_name)
69 name = pdev->ops->acpi_name(dev);
70 if (name)
71 return name;
72 }
Duncan Lauried9af3ce2016-05-08 18:15:25 -070073
74 return NULL;
75}
76
Patrick Rudolpheeb8e742019-08-20 08:20:01 +020077/* Locate and return the ACPI _HID (Hardware ID) for this device */
78const char *acpi_device_hid(const struct device *dev)
79{
80 if (!dev)
81 return NULL;
82
83 /* Check for device specific handler */
84 if (dev->ops->acpi_hid)
85 return dev->ops->acpi_hid(dev);
86
87 /*
88 * Don't walk up the tree to find any parent that can identify this device, as
89 * PNP devices are hard to identify.
90 */
91
92 return NULL;
93}
94
Patrick Rudolphc83bab62019-12-13 12:16:06 +010095/*
96 * Generate unique ID based on the ACPI path.
97 * Collisions on the same _HID are possible but very unlikely.
98 */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -070099uint32_t acpi_device_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100100{
101 const char *path = acpi_device_path(dev);
102 if (!path)
103 return 0;
104
105 return CRC(path, strlen(path), crc32_byte);
106}
107
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700108/* Recursive function to find the root device and print a path from there */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530109static ssize_t acpi_device_path_fill(const struct device *dev, char *buf,
Duncan Laurie47029142018-05-07 14:28:53 -0700110 size_t buf_len, size_t cur)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700111{
112 const char *name = acpi_device_name(dev);
Duncan Laurie47029142018-05-07 14:28:53 -0700113 ssize_t next = 0;
114
115 if (!name)
116 return -1;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700117
118 /*
119 * Make sure this name segment will fit, including the path segment
120 * separator and possible NUL terminator if this is the last segment.
121 */
Duncan Laurie47029142018-05-07 14:28:53 -0700122 if (!dev || (cur + strlen(name) + 2) > buf_len)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700123 return cur;
124
125 /* Walk up the tree to the root device */
126 if (dev->path.type != DEVICE_PATH_ROOT && dev->bus && dev->bus->dev)
127 next = acpi_device_path_fill(dev->bus->dev, buf, buf_len, cur);
Duncan Laurie47029142018-05-07 14:28:53 -0700128 if (next < 0)
129 return next;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700130
131 /* Fill in the path from the root device */
132 next += snprintf(buf + next, buf_len - next, "%s%s",
Timothy Pearsonaeb61012017-04-13 17:06:16 -0500133 (dev->path.type == DEVICE_PATH_ROOT
134 || (strlen(name) == 0)) ?
135 "" : ".", name);
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700136
137 return next;
138}
139
140/*
141 * Warning: just as with dev_path() this uses a static buffer
Martin Roth0949e732021-10-01 14:28:22 -0600142 * so should not be called multiple times in one statement
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700143 */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530144const char *acpi_device_path(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700145{
146 static char buf[DEVICE_PATH_MAX] = {};
147
148 if (!dev)
149 return NULL;
150
151 if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0)
152 return NULL;
153
154 return buf;
155}
156
157/* Return the path of the parent device as the ACPI Scope for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530158const char *acpi_device_scope(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700159{
Duncan Lauried02685b2016-06-30 14:37:37 -0700160 static char buf[DEVICE_PATH_MAX] = {};
161
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700162 if (!dev || !dev->bus || !dev->bus->dev)
163 return NULL;
164
Duncan Lauried02685b2016-06-30 14:37:37 -0700165 if (acpi_device_path_fill(dev->bus->dev, buf, sizeof(buf), 0) <= 0)
166 return NULL;
167
168 return buf;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700169}
170
Elyes HAOUAS6716bab2020-01-09 21:29:25 +0100171/* Concatenate the device path and provided name suffix */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530172const char *acpi_device_path_join(const struct device *dev, const char *name)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700173{
174 static char buf[DEVICE_PATH_MAX] = {};
Jacob Garberf2ba2d92019-07-02 10:50:10 -0600175 ssize_t len;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700176
177 if (!dev)
178 return NULL;
179
180 /* Build the path of this device */
181 len = acpi_device_path_fill(dev, buf, sizeof(buf), 0);
182 if (len <= 0)
183 return NULL;
184
185 /* Ensure there is room for the added name, separator, and NUL */
186 if ((len + strlen(name) + 2) > sizeof(buf))
187 return NULL;
188 snprintf(buf + len, sizeof(buf) - len, ".%s", name);
189
190 return buf;
191}
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700192
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800193int acpi_device_status(const struct device *dev)
194{
195 if (!dev->enabled)
196 return ACPI_STATUS_DEVICE_ALL_OFF;
197 if (dev->hidden)
198 return ACPI_STATUS_DEVICE_HIDDEN_ON;
199 return ACPI_STATUS_DEVICE_ALL_ON;
200}
201
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100202/* Write the unique _UID based on ACPI device path. */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -0700203void acpi_device_write_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100204{
205 acpigen_write_name_integer("_UID", acpi_device_uid(dev));
206}
207
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700208/* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
209void acpi_device_write_interrupt(const struct acpi_irq *irq)
210{
211 void *desc_length;
212 uint8_t flags;
213
214 if (!irq || !irq->pin)
215 return;
216
217 /* This is supported by GpioInt() but not Interrupt() */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800218 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700219 return;
220
221 /* Byte 0: Descriptor Type */
222 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT);
223
224 /* Byte 1-2: Length (filled in later) */
225 desc_length = acpi_device_write_zero_len();
226
227 /*
228 * Byte 3: Flags
229 * [7:5]: Reserved
230 * [4]: Wake (0=NO_WAKE 1=WAKE)
231 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
232 * [2]: Polarity (0=HIGH 1=LOW)
233 * [1]: Mode (0=LEVEL 1=EDGE)
234 * [0]: Resource (0=PRODUCER 1=CONSUMER)
235 */
236 flags = 1 << 0; /* ResourceConsumer */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800237 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700238 flags |= 1 << 1;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800239 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700240 flags |= 1 << 2;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800241 if (irq->shared == ACPI_IRQ_SHARED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700242 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800243 if (irq->wake == ACPI_IRQ_WAKE)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700244 flags |= 1 << 4;
245 acpigen_emit_byte(flags);
246
247 /* Byte 4: Interrupt Table Entry Count */
248 acpigen_emit_byte(1);
249
250 /* Byte 5-8: Interrupt Number */
251 acpigen_emit_dword(irq->pin);
252
253 /* Fill in Descriptor Length (account for len word) */
254 acpi_device_fill_len(desc_length);
255}
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700256
257/* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
258void acpi_device_write_gpio(const struct acpi_gpio *gpio)
259{
260 void *start, *desc_length;
261 void *pin_table_offset, *vendor_data_offset, *resource_offset;
262 uint16_t flags = 0;
263 int pin;
264
265 if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO)
266 return;
267
268 start = acpigen_get_current();
269
270 /* Byte 0: Descriptor Type */
271 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO);
272
273 /* Byte 1-2: Length (fill in later) */
274 desc_length = acpi_device_write_zero_len();
275
276 /* Byte 3: Revision ID */
277 acpigen_emit_byte(ACPI_GPIO_REVISION_ID);
278
279 /* Byte 4: GpioIo or GpioInt */
280 acpigen_emit_byte(gpio->type);
281
282 /*
283 * Byte 5-6: General Flags
284 * [15:1]: 0 => Reserved
285 * [0]: 1 => ResourceConsumer
286 */
287 acpigen_emit_word(1 << 0);
288
289 switch (gpio->type) {
290 case ACPI_GPIO_TYPE_INTERRUPT:
291 /*
292 * Byte 7-8: GPIO Interrupt Flags
293 * [15:5]: 0 => Reserved
294 * [4]: Wake (0=NO_WAKE 1=WAKE)
295 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
296 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
297 * [0]: Mode (0=LEVEL 1=EDGE)
298 */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800299 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700300 flags |= 1 << 0;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800301 if (gpio->irq.shared == ACPI_IRQ_SHARED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700302 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800303 if (gpio->irq.wake == ACPI_IRQ_WAKE)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700304 flags |= 1 << 4;
305
306 switch (gpio->irq.polarity) {
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800307 case ACPI_IRQ_ACTIVE_HIGH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700308 flags |= 0 << 1;
309 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800310 case ACPI_IRQ_ACTIVE_LOW:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700311 flags |= 1 << 1;
312 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800313 case ACPI_IRQ_ACTIVE_BOTH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700314 flags |= 2 << 1;
315 break;
316 }
317 break;
318
319 case ACPI_GPIO_TYPE_IO:
320 /*
321 * Byte 7-8: GPIO IO Flags
322 * [15:4]: 0 => Reserved
323 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
324 * [2]: 0 => Reserved
325 * [1:0]: IO Restriction
326 * 0 => IoRestrictionNone
327 * 1 => IoRestrictionInputOnly
328 * 2 => IoRestrictionOutputOnly
329 * 3 => IoRestrictionNoneAndPreserve
330 */
331 flags |= gpio->io_restrict & 3;
332 if (gpio->io_shared)
333 flags |= 1 << 3;
334 break;
335 }
336 acpigen_emit_word(flags);
337
338 /*
339 * Byte 9: Pin Configuration
340 * 0x01 => Default (no configuration applied)
341 * 0x02 => Pull-up
342 * 0x03 => Pull-down
343 * 0x04-0x7F => Reserved
344 * 0x80-0xff => Vendor defined
345 */
346 acpigen_emit_byte(gpio->pull);
347
348 /* Byte 10-11: Output Drive Strength in 1/100 mA */
349 acpigen_emit_word(gpio->output_drive_strength);
350
351 /* Byte 12-13: Debounce Timeout in 1/100 ms */
352 acpigen_emit_word(gpio->interrupt_debounce_timeout);
353
354 /* Byte 14-15: Pin Table Offset, relative to start */
355 pin_table_offset = acpi_device_write_zero_len();
356
357 /* Byte 16: Reserved */
358 acpigen_emit_byte(0);
359
360 /* Byte 17-18: Resource Source Name Offset, relative to start */
361 resource_offset = acpi_device_write_zero_len();
362
363 /* Byte 19-20: Vendor Data Offset, relative to start */
364 vendor_data_offset = acpi_device_write_zero_len();
365
366 /* Byte 21-22: Vendor Data Length */
367 acpigen_emit_word(0);
368
369 /* Fill in Pin Table Offset */
370 acpi_device_fill_from_len(pin_table_offset, start);
371
372 /* Pin Table, one word for each pin */
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700373 for (pin = 0; pin < gpio->pin_count; pin++) {
374 uint16_t acpi_pin = gpio->pins[pin];
Julius Wernercd49cce2019-03-05 16:53:33 -0800375#if CONFIG(GENERIC_GPIO_LIB)
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700376 acpi_pin = gpio_acpi_pin(acpi_pin);
377#endif
378 acpigen_emit_word(acpi_pin);
379 }
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700380
381 /* Fill in Resource Source Name Offset */
382 acpi_device_fill_from_len(resource_offset, start);
383
384 /* Resource Source Name String */
Julius Wernercd49cce2019-03-05 16:53:33 -0800385#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700386 acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0]));
387#else
388 acpigen_emit_string(gpio->resource);
389#endif
390
391 /* Fill in Vendor Data Offset */
392 acpi_device_fill_from_len(vendor_data_offset, start);
393
394 /* Fill in GPIO Descriptor Length (account for len word) */
395 acpi_device_fill_len(desc_length);
396}
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700397
398/* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
399void acpi_device_write_i2c(const struct acpi_i2c *i2c)
400{
401 void *desc_length, *type_length;
402
403 /* Byte 0: Descriptor Type */
404 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
405
406 /* Byte 1+2: Length (filled in later) */
407 desc_length = acpi_device_write_zero_len();
408
409 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200410 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700411
412 /* Byte 4: Resource Source Index is Reserved */
413 acpigen_emit_byte(0);
414
415 /* Byte 5: Serial Bus Type is I2C */
416 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C);
417
418 /*
419 * Byte 6: Flags
420 * [7:2]: 0 => Reserved
421 * [1]: 1 => ResourceConsumer
422 * [0]: 0 => ControllerInitiated
423 */
424 acpigen_emit_byte(1 << 1);
425
426 /*
427 * Byte 7-8: Type Specific Flags
428 * [15:1]: 0 => Reserved
429 * [0]: 0 => 7bit, 1 => 10bit
430 */
431 acpigen_emit_word(i2c->mode_10bit);
432
433 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200434 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700435
436 /* Byte 10-11: I2C Type Data Length */
437 type_length = acpi_device_write_zero_len();
438
439 /* Byte 12-15: I2C Bus Speed */
440 acpigen_emit_dword(i2c->speed);
441
442 /* Byte 16-17: I2C Slave Address */
443 acpigen_emit_word(i2c->address);
444
445 /* Fill in Type Data Length */
446 acpi_device_fill_len(type_length);
447
448 /* Byte 18+: ResourceSource */
449 acpigen_emit_string(i2c->resource);
450
451 /* Fill in I2C Descriptor Length */
452 acpi_device_fill_len(desc_length);
453}
Duncan Laurie70c86d92016-05-10 07:26:34 -0700454
455/* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
456void acpi_device_write_spi(const struct acpi_spi *spi)
457{
458 void *desc_length, *type_length;
459 uint16_t flags = 0;
460
461 /* Byte 0: Descriptor Type */
462 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
463
464 /* Byte 1+2: Length (filled in later) */
465 desc_length = acpi_device_write_zero_len();
466
467 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200468 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700469
470 /* Byte 4: Resource Source Index is Reserved */
471 acpigen_emit_byte(0);
472
473 /* Byte 5: Serial Bus Type is SPI */
474 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI);
475
476 /*
477 * Byte 6: Flags
478 * [7:2]: 0 => Reserved
479 * [1]: 1 => ResourceConsumer
480 * [0]: 0 => ControllerInitiated
481 */
482 acpigen_emit_byte(1 << 1);
483
484 /*
485 * Byte 7-8: Type Specific Flags
486 * [15:2]: 0 => Reserved
487 * [1]: 0 => ActiveLow, 1 => ActiveHigh
488 * [0]: 0 => FourWire, 1 => ThreeWire
489 */
490 if (spi->wire_mode == SPI_3_WIRE_MODE)
491 flags |= 1 << 0;
492 if (spi->device_select_polarity == SPI_POLARITY_HIGH)
493 flags |= 1 << 1;
494 acpigen_emit_word(flags);
495
496 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200497 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700498
499 /* Byte 10-11: SPI Type Data Length */
500 type_length = acpi_device_write_zero_len();
501
502 /* Byte 12-15: Connection Speed */
503 acpigen_emit_dword(spi->speed);
504
505 /* Byte 16: Data Bit Length */
506 acpigen_emit_byte(spi->data_bit_length);
507
508 /* Byte 17: Clock Phase */
509 acpigen_emit_byte(spi->clock_phase);
510
511 /* Byte 18: Clock Polarity */
512 acpigen_emit_byte(spi->clock_polarity);
513
514 /* Byte 19-20: Device Selection */
515 acpigen_emit_word(spi->device_select);
516
517 /* Fill in Type Data Length */
518 acpi_device_fill_len(type_length);
519
520 /* Byte 21+: ResourceSource String */
521 acpigen_emit_string(spi->resource);
522
523 /* Fill in SPI Descriptor Length */
524 acpi_device_fill_len(desc_length);
525}
Duncan Laurie559e9472016-05-10 13:18:17 -0700526
Duncan Lauriedccef0d2020-05-27 12:29:51 -0700527/* UART Serial Bus - UARTSerialBusV2() */
528void acpi_device_write_uart(const struct acpi_uart *uart)
529{
530 void *desc_length, *type_length;
531 uint16_t flags;
532
533 /* Byte 0: Descriptor Type */
534 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
535
536 /* Byte 1+2: Length (filled in later) */
537 desc_length = acpi_device_write_zero_len();
538
539 /* Byte 3: Revision ID */
540 acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID);
541
542 /* Byte 4: Resource Source Index is Reserved */
543 acpigen_emit_byte(0);
544
545 /* Byte 5: Serial Bus Type is UART */
546 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART);
547
548 /*
549 * Byte 6: Flags
550 * [7:2]: 0 => Reserved
551 * [1]: 1 => ResourceConsumer
552 * [0]: 0 => ControllerInitiated
553 */
554 acpigen_emit_byte(BIT(1));
555
556 /*
557 * Byte 7-8: Type Specific Flags
558 * [15:8]: 0 => Reserved
559 * [7]: 0 => Little Endian, 1 => Big Endian
560 * [6:4]: Data bits
561 * [3:2]: Stop bits
562 * [1:0]: Flow control
563 */
564 flags = uart->flow_control & 3;
565 flags |= (uart->stop_bits & 3) << 2;
566 flags |= (uart->data_bits & 7) << 4;
567 flags |= (uart->endian & 1) << 7;
568 acpigen_emit_word(flags);
569
570 /* Byte 9: Type Specific Revision ID */
571 acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID);
572
573 /* Byte 10-11: Type Data Length */
574 type_length = acpi_device_write_zero_len();
575
576 /* Byte 12-15: Initial Baud Rate */
577 acpigen_emit_dword(uart->initial_baud_rate);
578
579 /* Byte 16-17: RX FIFO size */
580 acpigen_emit_word(uart->rx_fifo_bytes);
581
582 /* Byte 18-19: TX FIFO size */
583 acpigen_emit_word(uart->tx_fifo_bytes);
584
585 /* Byte 20: Parity */
586 acpigen_emit_byte(uart->parity);
587
588 /* Byte 21: Lines Enabled */
589 acpigen_emit_byte(uart->lines_in_use);
590
591 /* Fill in Type Data Length */
592 acpi_device_fill_len(type_length);
593
594 /* Byte 22+: ResourceSource */
595 acpigen_emit_string(uart->resource);
596
597 /* Fill in Descriptor Length */
598 acpi_device_fill_len(desc_length);
599}
600
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600601#define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP
602#define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP
603
604/**
605 * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO
606 * state does not match the active parameter.
607 */
608static void acpigen_write_gpio_STA(const struct acpi_gpio *gpio, bool active)
609{
610 if (!gpio || !gpio->pin_count)
611 return;
612
613 /* Read current GPIO status into Local0. */
614 acpigen_get_tx_gpio(gpio);
615
616 /*
617 * If (!Local0)
618 * {
619 * Return (Zero)
620 * }
621 */
622 acpigen_write_if();
623 if (active)
624 acpigen_emit_byte(LNOT_OP);
625 acpigen_emit_byte(LOCAL0_OP);
626 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_OFF_OP);
627 acpigen_write_if_end();
628}
629
630static void acpigen_write_power_res_STA(const struct acpi_power_res_params *params)
631{
632 acpigen_write_method_serialized("_STA", 0);
633
634 /* Verify all the GPIOs are in the ON state, otherwise return 0 */
635 acpigen_write_gpio_STA(params->enable_gpio, true);
636 acpigen_write_gpio_STA(params->reset_gpio, false);
637 acpigen_write_gpio_STA(params->stop_gpio, false);
638
639 /* All GPIOs are in the ON state */
640 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP);
641
642 acpigen_pop_len(); /* Method */
643}
644
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800645/* PowerResource() with Enable and/or Reset control */
Shelley Chena0603392018-04-26 13:52:30 -0700646void acpi_device_add_power_res(const struct acpi_power_res_params *params)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800647{
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700648 static uint8_t id;
Furquan Shaikhdc782752020-04-30 22:49:39 -0700649 static const char * const power_res_dev_states[] = { "_PR0", "_PR3" };
Edward O'Callaghan7e262552020-01-23 10:32:33 +1100650 unsigned int reset_gpio = params->reset_gpio ? params->reset_gpio->pins[0] : 0;
651 unsigned int enable_gpio = params->enable_gpio ? params->enable_gpio->pins[0] : 0;
652 unsigned int stop_gpio = params->stop_gpio ? params->stop_gpio->pins[0] : 0;
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700653 char pr_name[ACPI_NAME_BUFFER_SIZE];
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800654
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700655 if (!reset_gpio && !enable_gpio && !stop_gpio)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800656 return;
657
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700658 snprintf(pr_name, sizeof(pr_name), "PR%02X", id++);
659
660 /* PowerResource (PR##, 0, 0) */
661 acpigen_write_power_res(pr_name, 0, 0, power_res_dev_states,
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800662 ARRAY_SIZE(power_res_dev_states));
663
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600664 if (params->use_gpio_for_status) {
665 acpigen_write_power_res_STA(params);
666 } else {
667 /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
668 acpigen_write_STA(ACPI_POWER_RESOURCE_STATUS_ON_OP);
669 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800670
671 /* Method (_ON, 0, Serialized) */
672 acpigen_write_method_serialized("_ON", 0);
Tim Van Patten3d4665c2022-04-13 11:53:20 -0600673 /* Call _STA and early return if the device is already enabled, since the Linux
674 kernel doesn't check the device status before calling _ON. This avoids
675 unnecessary delays while booting. */
676 if (params->use_gpio_for_status) {
677 /* Local0 = _STA () */
678 acpigen_write_store();
679 acpigen_emit_namestring("_STA");
680 acpigen_emit_byte(LOCAL0_OP);
681 /* If (( Local0 == ACPI_POWER_RESOURCE_STATUS_ON_OP)) */
682 acpigen_write_if_lequal_op_op(LOCAL0_OP, ACPI_POWER_RESOURCE_STATUS_ON_OP);
683 acpigen_write_return_op(ZERO_OP);
684 acpigen_write_if_end();
685 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800686 if (reset_gpio)
Shelley Chena0603392018-04-26 13:52:30 -0700687 acpigen_enable_tx_gpio(params->reset_gpio);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800688 if (enable_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700689 acpigen_enable_tx_gpio(params->enable_gpio);
690 if (params->enable_delay_ms)
691 acpigen_write_sleep(params->enable_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800692 }
693 if (reset_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700694 acpigen_disable_tx_gpio(params->reset_gpio);
695 if (params->reset_delay_ms)
696 acpigen_write_sleep(params->reset_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800697 }
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700698 if (stop_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700699 acpigen_disable_tx_gpio(params->stop_gpio);
700 if (params->stop_delay_ms)
701 acpigen_write_sleep(params->stop_delay_ms);
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700702 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800703 acpigen_pop_len(); /* _ON method */
704
705 /* Method (_OFF, 0, Serialized) */
706 acpigen_write_method_serialized("_OFF", 0);
Shelley Chena0603392018-04-26 13:52:30 -0700707 if (stop_gpio) {
708 acpigen_enable_tx_gpio(params->stop_gpio);
709 if (params->stop_off_delay_ms)
710 acpigen_write_sleep(params->stop_off_delay_ms);
711 }
712 if (reset_gpio) {
713 acpigen_enable_tx_gpio(params->reset_gpio);
714 if (params->reset_off_delay_ms)
715 acpigen_write_sleep(params->reset_off_delay_ms);
716 }
717 if (enable_gpio) {
718 acpigen_disable_tx_gpio(params->enable_gpio);
719 if (params->enable_off_delay_ms)
720 acpigen_write_sleep(params->enable_off_delay_ms);
721 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800722 acpigen_pop_len(); /* _OFF method */
723
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700724 acpigen_pop_len(); /* PowerResource PR## */
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800725}
726
Duncan Laurieffc99902016-07-02 19:56:06 -0700727static void acpi_dp_write_array(const struct acpi_dp *array);
728static void acpi_dp_write_value(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700729{
730 switch (prop->type) {
731 case ACPI_DP_TYPE_INTEGER:
732 acpigen_write_integer(prop->integer);
733 break;
734 case ACPI_DP_TYPE_STRING:
Harsha Priya3a96ac42016-07-15 17:31:43 -0700735 case ACPI_DP_TYPE_CHILD:
Duncan Laurie559e9472016-05-10 13:18:17 -0700736 acpigen_write_string(prop->string);
737 break;
738 case ACPI_DP_TYPE_REFERENCE:
739 acpigen_emit_namestring(prop->string);
740 break;
Duncan Laurieffc99902016-07-02 19:56:06 -0700741 case ACPI_DP_TYPE_ARRAY:
742 acpi_dp_write_array(prop->array);
743 break;
744 default:
745 break;
Duncan Laurie559e9472016-05-10 13:18:17 -0700746 }
747}
748
Duncan Laurieffc99902016-07-02 19:56:06 -0700749/* Package (2) { "prop->name", VALUE } */
750static void acpi_dp_write_property(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700751{
752 acpigen_write_package(2);
Duncan Laurieffc99902016-07-02 19:56:06 -0700753 acpigen_write_string(prop->name);
Duncan Laurie559e9472016-05-10 13:18:17 -0700754 acpi_dp_write_value(prop);
755 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700756}
757
758/* Write array of Device Properties */
Duncan Laurieffc99902016-07-02 19:56:06 -0700759static void acpi_dp_write_array(const struct acpi_dp *array)
Duncan Laurie559e9472016-05-10 13:18:17 -0700760{
Duncan Laurieffc99902016-07-02 19:56:06 -0700761 const struct acpi_dp *dp;
762 char *pkg_count;
763
764 /* Package element count determined as it is populated */
765 pkg_count = acpigen_write_package(0);
766
Furquan Shaikh35c01bc2016-10-03 23:30:14 -0700767 /*
768 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
769 * DP_TYPE_TABLE does not have a value to be written. Thus, start
770 * the loop from next type in the array.
771 */
772 for (dp = array->next; dp; dp = dp->next) {
Duncan Laurieffc99902016-07-02 19:56:06 -0700773 acpi_dp_write_value(dp);
774 (*pkg_count)++;
775 }
776
Duncan Laurie559e9472016-05-10 13:18:17 -0700777 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700778}
779
Duncan Laurieffc99902016-07-02 19:56:06 -0700780static void acpi_dp_free(struct acpi_dp *dp)
Duncan Laurie559e9472016-05-10 13:18:17 -0700781{
Duncan Laurieffc99902016-07-02 19:56:06 -0700782 while (dp) {
783 struct acpi_dp *p = dp->next;
784
785 switch (dp->type) {
786 case ACPI_DP_TYPE_CHILD:
787 acpi_dp_free(dp->child);
788 break;
789 case ACPI_DP_TYPE_ARRAY:
790 acpi_dp_free(dp->array);
791 break;
792 default:
793 break;
794 }
795
796 free(dp);
797 dp = p;
798 }
Duncan Laurie559e9472016-05-10 13:18:17 -0700799}
800
Duncan Laurie84fac412020-06-03 12:36:51 -0700801static bool acpi_dp_write_properties(struct acpi_dp *prop, const char *uuid)
802{
803 struct acpi_dp *dp;
804 char *prop_count = NULL;
805
806 /* Print base properties */
807 for (dp = prop; dp; dp = dp->next) {
808 if (dp->type == ACPI_DP_TYPE_TABLE ||
809 dp->type == ACPI_DP_TYPE_CHILD ||
810 dp->type == ACPI_DP_TYPE_PACKAGE)
811 continue;
812
813 /*
814 * The UUID and package is only added when
815 * we come across the first property. This
816 * is to avoid creating a zero-length package
817 * in situations where there are only children.
818 */
819 if (!prop_count) {
820 /* ToUUID (dp->uuid) */
821 acpigen_write_uuid(uuid);
822 /*
823 * Package (PROP), element count determined as
824 * it is populated
825 */
826 prop_count = acpigen_write_package(0);
827 }
828 (*prop_count)++;
829 acpi_dp_write_property(dp);
830 }
831 if (prop_count) {
832 /* Package (PROP) length, if a package was written */
833 acpigen_pop_len();
834 return true;
835 }
836 return false;
837}
838
Simon Glass8bfa51e2020-06-29 16:18:37 -0600839static void acpi_dp_write_(struct acpi_dp *table)
Duncan Laurie559e9472016-05-10 13:18:17 -0700840{
Duncan Laurieffc99902016-07-02 19:56:06 -0700841 struct acpi_dp *dp, *prop;
Duncan Laurie84fac412020-06-03 12:36:51 -0700842 char *dp_count;
Duncan Laurieffc99902016-07-02 19:56:06 -0700843 int child_count = 0;
844
Duncan Lauriec1adeb62020-04-29 00:04:14 -0700845 if (!table || table->type != ACPI_DP_TYPE_TABLE || !table->next)
Duncan Laurieffc99902016-07-02 19:56:06 -0700846 return;
847
848 /* Name (name) */
849 acpigen_write_name(table->name);
850
851 /* Device Property list starts with the next entry */
852 prop = table->next;
853
Matt Delco08258882019-01-30 11:16:08 -0800854 /* Package (DP), default to assuming no properties or children */
855 dp_count = acpigen_write_package(0);
Duncan Laurieffc99902016-07-02 19:56:06 -0700856
857 /* Print base properties */
Duncan Laurie84fac412020-06-03 12:36:51 -0700858 if (acpi_dp_write_properties(prop, table->uuid))
859 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700860
Duncan Laurie84fac412020-06-03 12:36:51 -0700861 /* Count child properties */
862 for (dp = prop; dp; dp = dp->next)
863 if (dp->type == ACPI_DP_TYPE_CHILD)
864 child_count++;
865
866 /* Add child properties to the base table */
Duncan Laurieffc99902016-07-02 19:56:06 -0700867 if (child_count) {
Duncan Laurie84fac412020-06-03 12:36:51 -0700868 /* Update DP package count */
Matt Delco08258882019-01-30 11:16:08 -0800869 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700870 /* ToUUID (ACPI_DP_CHILD_UUID) */
871 acpigen_write_uuid(ACPI_DP_CHILD_UUID);
872
873 /* Print child pointer properties */
874 acpigen_write_package(child_count);
875
876 for (dp = prop; dp; dp = dp->next)
877 if (dp->type == ACPI_DP_TYPE_CHILD)
878 acpi_dp_write_property(dp);
Matt Delco08258882019-01-30 11:16:08 -0800879 /* Package (CHILD) length */
Duncan Laurieffc99902016-07-02 19:56:06 -0700880 acpigen_pop_len();
881 }
882
Duncan Laurie84fac412020-06-03 12:36:51 -0700883 /* Write packages of properties with unique UUID */
884 for (dp = prop; dp; dp = dp->next)
885 if (dp->type == ACPI_DP_TYPE_PACKAGE)
886 if (acpi_dp_write_properties(dp->child, dp->uuid))
887 *dp_count += 2;
888
Duncan Laurieffc99902016-07-02 19:56:06 -0700889 /* Package (DP) length */
890 acpigen_pop_len();
891
892 /* Recursively parse children into separate tables */
893 for (dp = prop; dp; dp = dp->next)
894 if (dp->type == ACPI_DP_TYPE_CHILD)
Simon Glass8bfa51e2020-06-29 16:18:37 -0600895 acpi_dp_write_(dp->child);
896}
897
898void acpi_dp_write(struct acpi_dp *table)
899{
900 acpi_dp_write_(table);
Duncan Laurieffc99902016-07-02 19:56:06 -0700901
902 /* Clean up */
903 acpi_dp_free(table);
904}
905
906static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
907 const char *name)
908{
909 struct acpi_dp *new;
910
911 new = malloc(sizeof(struct acpi_dp));
912 if (!new)
913 return NULL;
914
915 memset(new, 0, sizeof(*new));
916 new->type = type;
917 new->name = name;
Duncan Laurie84fac412020-06-03 12:36:51 -0700918 new->uuid = ACPI_DP_UUID;
Duncan Laurieffc99902016-07-02 19:56:06 -0700919
920 if (dp) {
921 /* Add to end of property list */
922 while (dp->next)
923 dp = dp->next;
924 dp->next = new;
925 }
926
927 return new;
928}
929
930struct acpi_dp *acpi_dp_new_table(const char *name)
931{
932 return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
933}
934
Duncan Laurieb3023b62017-08-29 08:26:50 -0700935size_t acpi_dp_add_property_list(struct acpi_dp *dp,
936 const struct acpi_dp *property_list,
937 size_t property_count)
938{
939 const struct acpi_dp *prop;
940 size_t i, properties_added = 0;
941
Jacob Garberc30e5902019-05-23 14:34:58 -0600942 if (!dp || !property_list)
943 return 0;
944
Duncan Laurieb3023b62017-08-29 08:26:50 -0700945 for (i = 0; i < property_count; i++) {
946 prop = &property_list[i];
947
948 if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name)
949 continue;
950
951 switch (prop->type) {
952 case ACPI_DP_TYPE_INTEGER:
953 acpi_dp_add_integer(dp, prop->name, prop->integer);
954 break;
955 case ACPI_DP_TYPE_STRING:
956 acpi_dp_add_string(dp, prop->name, prop->string);
957 break;
958 case ACPI_DP_TYPE_REFERENCE:
959 acpi_dp_add_reference(dp, prop->name, prop->string);
960 break;
961 case ACPI_DP_TYPE_ARRAY:
962 acpi_dp_add_array(dp, prop->array);
963 break;
964 case ACPI_DP_TYPE_CHILD:
965 acpi_dp_add_child(dp, prop->name, prop->child);
966 break;
967 default:
968 continue;
969 }
970
971 ++properties_added;
972 }
973
974 return properties_added;
975}
976
Duncan Laurieffc99902016-07-02 19:56:06 -0700977struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
978 uint64_t value)
979{
Jacob Garberc30e5902019-05-23 14:34:58 -0600980 if (!dp)
981 return NULL;
982
Duncan Laurieffc99902016-07-02 19:56:06 -0700983 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);
984
985 if (new)
986 new->integer = value;
987
988 return new;
989}
990
991struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
992 const char *string)
993{
Jacob Garberc30e5902019-05-23 14:34:58 -0600994 if (!dp)
995 return NULL;
996
Duncan Laurieffc99902016-07-02 19:56:06 -0700997 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
998
999 if (new)
1000 new->string = string;
1001
1002 return new;
1003}
1004
1005struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
1006 const char *reference)
1007{
Jacob Garberc30e5902019-05-23 14:34:58 -06001008 if (!dp)
1009 return NULL;
1010
Duncan Laurieffc99902016-07-02 19:56:06 -07001011 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
1012
1013 if (new)
1014 new->string = reference;
1015
1016 return new;
1017}
1018
1019struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
1020 struct acpi_dp *child)
1021{
1022 struct acpi_dp *new;
1023
Jacob Garberc30e5902019-05-23 14:34:58 -06001024 if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001025 return NULL;
1026
1027 new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
1028 if (new) {
1029 new->child = child;
1030 new->string = child->name;
1031 }
1032
1033 return new;
1034}
1035
Duncan Laurie84fac412020-06-03 12:36:51 -07001036struct acpi_dp *acpi_dp_add_package(struct acpi_dp *dp, struct acpi_dp *package)
1037{
1038 struct acpi_dp *new;
1039
1040 if (!dp || !package || package->type != ACPI_DP_TYPE_TABLE)
1041 return NULL;
1042
1043 new = acpi_dp_new(dp, ACPI_DP_TYPE_PACKAGE, NULL);
1044 if (new) {
1045 new->uuid = package->name;
1046 new->child = package;
1047 }
1048
1049 return new;
1050}
1051
Duncan Laurieffc99902016-07-02 19:56:06 -07001052struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
1053{
1054 struct acpi_dp *new;
1055
Jacob Garberc30e5902019-05-23 14:34:58 -06001056 if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001057 return NULL;
1058
1059 new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
1060 if (new)
1061 new->array = array;
1062
1063 return new;
1064}
1065
1066struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
Duncan Laurieed6eb272020-04-29 11:39:08 -07001067 const uint64_t *array, int len)
Duncan Laurieffc99902016-07-02 19:56:06 -07001068{
1069 struct acpi_dp *dp_array;
1070 int i;
1071
Jacob Garberc30e5902019-05-23 14:34:58 -06001072 if (!dp || len <= 0)
Duncan Laurieffc99902016-07-02 19:56:06 -07001073 return NULL;
1074
1075 dp_array = acpi_dp_new_table(name);
1076 if (!dp_array)
1077 return NULL;
1078
1079 for (i = 0; i < len; i++)
1080 if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
1081 break;
1082
1083 acpi_dp_add_array(dp, dp_array);
1084
1085 return dp_array;
1086}
1087
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001088struct acpi_dp *acpi_dp_add_gpio_array(struct acpi_dp *dp, const char *name,
1089 const struct acpi_gpio_res_params *params,
1090 size_t param_count)
1091{
1092 struct acpi_dp *gpio;
1093 uint32_t i;
1094
1095 if (!dp || !param_count)
1096 return NULL;
1097
1098 gpio = acpi_dp_new_table(name);
1099 if (!gpio)
1100 return NULL;
1101
1102 /*
1103 * Generate ACPI identifiers as follows:
1104 * Package () {
1105 * name, // e.g. cs-gpios
1106 * Package() {
1107 * ref, index, pin, active_low, // GPIO-0 (params[0])
1108 * ref, index, pin, active_low, // GPIO-1 (params[1])
1109 * ...
1110 * }
1111 * }
1112 */
1113 for (i = 0; i < param_count; i++, params++) {
1114 /*
1115 * If refs is NULL, leave a hole in the gpio array. This can be used in
1116 * conditions where some controllers use both GPIOs and native signals.
1117 */
1118 if (!params->ref) {
1119 acpi_dp_add_integer(gpio, NULL, 0);
1120 continue;
1121 }
1122
1123 /* The device that has _CRS containing GpioIO()/GpioInt() */
1124 acpi_dp_add_reference(gpio, NULL, params->ref);
1125
1126 /* Index of the GPIO resource in _CRS starting from zero */
1127 acpi_dp_add_integer(gpio, NULL, params->index);
1128
1129 /* Pin in the GPIO resource, typically zero */
1130 acpi_dp_add_integer(gpio, NULL, params->pin);
1131
1132 /* Set if pin is active low */
1133 acpi_dp_add_integer(gpio, NULL, params->active_low);
1134 }
1135 acpi_dp_add_array(dp, gpio);
1136
1137 return gpio;
1138
1139}
1140
1141
Duncan Laurieffc99902016-07-02 19:56:06 -07001142struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
1143 const char *ref, int index, int pin,
1144 int active_low)
1145{
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001146 struct acpi_gpio_res_params param = {
1147 .ref = ref,
1148 .index = index,
1149 .pin = pin,
1150 .active_low = active_low,
1151 };
Jacob Garberc30e5902019-05-23 14:34:58 -06001152
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001153 return acpi_dp_add_gpio_array(dp, name, &param, 1);
Duncan Laurie559e9472016-05-10 13:18:17 -07001154}
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001155
1156/*
1157 * This function writes a PCI device with _ADR object:
1158 * Example:
1159 * Scope (\_SB.PCI0)
1160 * {
1161 * Device (IGFX)
1162 * {
1163 * Name (_ADR, 0x0000000000000000)
1164 * Method (_STA, 0, NotSerialized) { Return (status) }
1165 * }
1166 * }
1167 */
Furquan Shaikh7536a392020-04-24 21:59:21 -07001168void acpi_device_write_pci_dev(const struct device *dev)
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001169{
1170 const char *scope = acpi_device_scope(dev);
1171 const char *name = acpi_device_name(dev);
1172
1173 assert(dev->path.type == DEVICE_PATH_PCI);
1174 assert(name);
1175 assert(scope);
1176
1177 acpigen_write_scope(scope);
1178 acpigen_write_device(name);
1179
1180 acpigen_write_ADR_pci_device(dev);
1181 acpigen_write_STA(acpi_device_status(dev));
1182
1183 acpigen_pop_len(); /* Device */
1184 acpigen_pop_len(); /* Scope */
1185}