blob: 62b1295201c7b6c3078cb2a3cd15341bf0b8a022 [file] [log] [blame]
Simon Glassebb2e832020-07-07 13:11:39 -06001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Generation of tables for particular device types
4 *
5 * Copyright 2019 Google LLC
6 * Mostly taken from coreboot file of the same name
7 */
8
9#ifndef __ACPI_DEVICE_H
10#define __ACPI_DEVICE_H
11
Simon Glass39ab8672020-07-07 13:11:48 -060012#include <i2c.h>
Simon Glass711fd982020-07-07 13:11:49 -060013#include <spi.h>
Simon Glass8965cc92020-07-07 13:11:40 -060014#include <linux/bitops.h>
15
Simon Glass3d39c132020-07-07 13:11:43 -060016struct acpi_ctx;
Simon Glass4fa63312020-07-07 13:11:46 -060017struct gpio_desc;
Simon Glass3d39c132020-07-07 13:11:43 -060018struct irq;
Simon Glassebb2e832020-07-07 13:11:39 -060019struct udevice;
20
Simon Glassb24cbf42020-07-07 13:11:41 -060021/* ACPI descriptor values for common descriptors: SERIAL_BUS means I2C */
22#define ACPI_DESCRIPTOR_LARGE BIT(7)
23#define ACPI_DESCRIPTOR_INTERRUPT (ACPI_DESCRIPTOR_LARGE | 9)
24#define ACPI_DESCRIPTOR_GPIO (ACPI_DESCRIPTOR_LARGE | 12)
25#define ACPI_DESCRIPTOR_SERIAL_BUS (ACPI_DESCRIPTOR_LARGE | 14)
26
Simon Glassebb2e832020-07-07 13:11:39 -060027/* Length of a full path to an ACPI device */
28#define ACPI_PATH_MAX 30
29
Simon Glass8965cc92020-07-07 13:11:40 -060030/* Values that can be returned for ACPI device _STA method */
31enum acpi_dev_status {
32 ACPI_DSTATUS_PRESENT = BIT(0),
33 ACPI_DSTATUS_ENABLED = BIT(1),
34 ACPI_DSTATUS_SHOW_IN_UI = BIT(2),
35 ACPI_DSTATUS_OK = BIT(3),
36 ACPI_DSTATUS_HAS_BATTERY = BIT(4),
37
38 ACPI_DSTATUS_ALL_OFF = 0,
39 ACPI_DSTATUS_HIDDEN_ON = ACPI_DSTATUS_PRESENT | ACPI_DSTATUS_ENABLED |
40 ACPI_DSTATUS_OK,
41 ACPI_DSTATUS_ALL_ON = ACPI_DSTATUS_HIDDEN_ON |
42 ACPI_DSTATUS_SHOW_IN_UI,
43};
44
Simon Glassb24cbf42020-07-07 13:11:41 -060045/** enum acpi_irq_mode - edge/level trigger mode */
46enum acpi_irq_mode {
47 ACPI_IRQ_EDGE_TRIGGERED,
48 ACPI_IRQ_LEVEL_TRIGGERED,
49};
50
51/**
52 * enum acpi_irq_polarity - polarity of interrupt
53 *
54 * @ACPI_IRQ_ACTIVE_LOW - for ACPI_IRQ_EDGE_TRIGGERED this means falling edge
55 * @ACPI_IRQ_ACTIVE_HIGH - for ACPI_IRQ_EDGE_TRIGGERED this means rising edge
56 * @ACPI_IRQ_ACTIVE_BOTH - not meaningful for ACPI_IRQ_EDGE_TRIGGERED
57 */
58enum acpi_irq_polarity {
59 ACPI_IRQ_ACTIVE_LOW,
60 ACPI_IRQ_ACTIVE_HIGH,
61 ACPI_IRQ_ACTIVE_BOTH,
62};
63
64/**
65 * enum acpi_irq_shared - whether interrupt is shared or not
66 *
67 * @ACPI_IRQ_EXCLUSIVE: only this device uses the interrupt
68 * @ACPI_IRQ_SHARED: other devices may use this interrupt
69 */
70enum acpi_irq_shared {
71 ACPI_IRQ_EXCLUSIVE,
72 ACPI_IRQ_SHARED,
73};
74
75/** enum acpi_irq_wake - indicates whether this interrupt can wake the device */
76enum acpi_irq_wake {
77 ACPI_IRQ_NO_WAKE,
78 ACPI_IRQ_WAKE,
79};
80
81/**
82 * struct acpi_irq - representation of an ACPI interrupt
83 *
84 * @pin: ACPI pin that is monitored for the interrupt
85 * @mode: Edge/level triggering
86 * @polarity: Interrupt polarity
87 * @shared: Whether interrupt is shared or not
88 * @wake: Whether interrupt can wake the device from sleep
89 */
90struct acpi_irq {
91 unsigned int pin;
92 enum acpi_irq_mode mode;
93 enum acpi_irq_polarity polarity;
94 enum acpi_irq_shared shared;
95 enum acpi_irq_wake wake;
96};
97
Simon Glassebb2e832020-07-07 13:11:39 -060098/**
Simon Glass3176b6c2020-07-07 13:11:44 -060099 * enum acpi_gpio_type - type of the descriptor
100 *
101 * @ACPI_GPIO_TYPE_INTERRUPT: GpioInterrupt
102 * @ACPI_GPIO_TYPE_IO: GpioIo
103 */
104enum acpi_gpio_type {
105 ACPI_GPIO_TYPE_INTERRUPT,
106 ACPI_GPIO_TYPE_IO,
107};
108
109/**
110 * enum acpi_gpio_pull - pull direction
111 *
112 * @ACPI_GPIO_PULL_DEFAULT: Use default value for pin
113 * @ACPI_GPIO_PULL_UP: Pull up
114 * @ACPI_GPIO_PULL_DOWN: Pull down
115 * @ACPI_GPIO_PULL_NONE: No pullup/pulldown
116 */
117enum acpi_gpio_pull {
118 ACPI_GPIO_PULL_DEFAULT,
119 ACPI_GPIO_PULL_UP,
120 ACPI_GPIO_PULL_DOWN,
121 ACPI_GPIO_PULL_NONE,
122};
123
124/**
125 * enum acpi_gpio_io_restrict - controls input/output of pin
126 *
127 * @ACPI_GPIO_IO_RESTRICT_NONE: no restrictions
128 * @ACPI_GPIO_IO_RESTRICT_INPUT: input only (no output)
129 * @ACPI_GPIO_IO_RESTRICT_OUTPUT: output only (no input)
130 * @ACPI_GPIO_IO_RESTRICT_PRESERVE: preserve settings when driver not active
131 */
132enum acpi_gpio_io_restrict {
133 ACPI_GPIO_IO_RESTRICT_NONE,
134 ACPI_GPIO_IO_RESTRICT_INPUT,
135 ACPI_GPIO_IO_RESTRICT_OUTPUT,
136 ACPI_GPIO_IO_RESTRICT_PRESERVE,
137};
138
139/** enum acpi_gpio_polarity - controls the GPIO polarity */
140enum acpi_gpio_polarity {
141 ACPI_GPIO_ACTIVE_HIGH = 0,
142 ACPI_GPIO_ACTIVE_LOW = 1,
143};
144
145#define ACPI_GPIO_REVISION_ID 1
146#define ACPI_GPIO_MAX_PINS 2
147
148/**
149 * struct acpi_gpio - representation of an ACPI GPIO
150 *
151 * @pin_count: Number of pins represented
152 * @pins: List of pins
153 * @pin0_addr: Address in memory of the control registers for pin 0. This is
154 * used when generating ACPI tables
155 * @type: GPIO type
156 * @pull: Pullup/pulldown setting
157 * @resource: Resource name for this GPIO controller
158 * For GpioInt:
159 * @interrupt_debounce_timeout: Debounce timeout in units of 10us
160 * @irq: Interrupt
161 *
162 * For GpioIo:
163 * @output_drive_strength: Drive strength in units of 10uA
164 * @io_shared; true if GPIO is shared
165 * @io_restrict: I/O restriction setting
166 * @polarity: GPIO polarity
167 */
168struct acpi_gpio {
169 int pin_count;
170 u16 pins[ACPI_GPIO_MAX_PINS];
171 ulong pin0_addr;
172
173 enum acpi_gpio_type type;
174 enum acpi_gpio_pull pull;
175 char resource[ACPI_PATH_MAX];
176
177 /* GpioInt */
178 u16 interrupt_debounce_timeout;
179 struct acpi_irq irq;
180
181 /* GpioIo */
182 u16 output_drive_strength;
183 bool io_shared;
184 enum acpi_gpio_io_restrict io_restrict;
185 enum acpi_gpio_polarity polarity;
186};
187
Simon Glass39ab8672020-07-07 13:11:48 -0600188/* ACPI Descriptors for Serial Bus interfaces */
189#define ACPI_SERIAL_BUS_TYPE_I2C 1
Simon Glass711fd982020-07-07 13:11:49 -0600190#define ACPI_SERIAL_BUS_TYPE_SPI 2
Simon Glass39ab8672020-07-07 13:11:48 -0600191#define ACPI_I2C_SERIAL_BUS_REVISION_ID 1 /* TODO: upgrade to 2 */
192#define ACPI_I2C_TYPE_SPECIFIC_REVISION_ID 1
Simon Glass711fd982020-07-07 13:11:49 -0600193#define ACPI_SPI_SERIAL_BUS_REVISION_ID 1
194#define ACPI_SPI_TYPE_SPECIFIC_REVISION_ID 1
Simon Glass39ab8672020-07-07 13:11:48 -0600195
196/**
197 * struct acpi_i2c - representation of an ACPI I2C device
198 *
199 * @address: 7-bit or 10-bit I2C address
200 * @mode_10bit: Which address size is used
201 * @speed: Bus speed in Hz
202 * @resource: Resource name for the I2C controller
203 */
204struct acpi_i2c {
205 u16 address;
206 enum i2c_address_mode mode_10bit;
207 enum i2c_speed_rate speed;
208 const char *resource;
209};
210
Simon Glass3176b6c2020-07-07 13:11:44 -0600211/**
Simon Glass711fd982020-07-07 13:11:49 -0600212 * struct acpi_spi - representation of an ACPI SPI device
213 *
214 * @device_select: Chip select used by this device (typically 0)
215 * @device_select_polarity: Polarity for the device
216 * @wire_mode: Number of wires used for SPI
217 * @speed: Bus speed in Hz
218 * @data_bit_length: Word length for SPI (typically 8)
219 * @clock_phase: Clock phase to capture data
220 * @clock_polarity: Bus polarity
221 * @resource: Resource name for the SPI controller
222 */
223struct acpi_spi {
224 u16 device_select;
225 enum spi_polarity device_select_polarity;
226 enum spi_wire_mode wire_mode;
227 unsigned int speed;
228 u8 data_bit_length;
229 enum spi_clock_phase clock_phase;
230 enum spi_polarity clock_polarity;
231 const char *resource;
232};
233
234/**
Simon Glassebb2e832020-07-07 13:11:39 -0600235 * acpi_device_path() - Get the full path to an ACPI device
236 *
237 * This gets the full path in the form XXXX.YYYY.ZZZZ where XXXX is the root
238 * and ZZZZ is the device. All parent devices are added to the path.
239 *
240 * @dev: Device to check
241 * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
242 * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
243 * @return 0 if OK, -ve on error
244 */
245int acpi_device_path(const struct udevice *dev, char *buf, int maxlen);
246
247/**
248 * acpi_device_scope() - Get the scope of an ACPI device
249 *
250 * This gets the scope which is the full path of the parent device, as per
251 * acpi_device_path().
252 *
253 * @dev: Device to check
254 * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
255 * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
256 * @return 0 if OK, -EINVAL if the device has no parent, other -ve on other
257 * error
258 */
259int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen);
260
Simon Glass8965cc92020-07-07 13:11:40 -0600261/**
262 * acpi_device_status() - Get the status of a device
263 *
264 * This currently just returns ACPI_DSTATUS_ALL_ON. It does not support
265 * inactive or hidden devices.
266 *
267 * @dev: Device to check
268 * @return device status, as ACPI_DSTATUS_...
269 */
270enum acpi_dev_status acpi_device_status(const struct udevice *dev);
271
Simon Glass3d39c132020-07-07 13:11:43 -0600272/**
273 * acpi_device_write_interrupt_irq() - Write an interrupt descriptor
274 *
275 * This writes an ACPI interrupt descriptor for the given interrupt, converting
276 * fields as needed.
277 *
278 * @ctx: ACPI context pointer
279 * @req_irq: Interrupt to output
280 * @return IRQ pin number if OK, -ve on error
281 */
282int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
283 const struct irq *req_irq);
284
Simon Glass4fa63312020-07-07 13:11:46 -0600285/**
286 * acpi_device_write_gpio() - Write GpioIo() or GpioInt() descriptor
287 *
288 * @gpio: GPIO information to write
289 * @return GPIO pin number of first GPIO if OK, -ve on error
290 */
291int acpi_device_write_gpio(struct acpi_ctx *ctx, const struct acpi_gpio *gpio);
292
293/**
294 * acpi_device_write_gpio_desc() - Write a GPIO to ACPI
295 *
296 * This creates a GPIO descriptor for a GPIO, including information ACPI needs
297 * to use it.
298 *
299 * @ctx: ACPI context pointer
300 * @desc: GPIO to write
301 * @return 0 if OK, -ve on error
302 */
303int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
304 const struct gpio_desc *desc);
305
Simon Glasse2bbb132020-07-07 13:11:47 -0600306/**
307 * acpi_device_write_interrupt_or_gpio() - Write interrupt or GPIO to ACPI
308 *
309 * This reads an interrupt from the device tree "interrupts-extended" property,
310 * if available. If not it reads the first GPIO with the name @prop.
311 *
312 * If an interrupt is found, an ACPI interrupt descriptor is written to the ACPI
313 * output. If not, but if a GPIO is found, a GPIO descriptor is written.
314 *
315 * @return irq or GPIO pin number if OK, -ve if neither an interrupt nor a GPIO
316 * could be found, or some other error occurred
317 */
318int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
319 struct udevice *dev, const char *prop);
320
Simon Glass39ab8672020-07-07 13:11:48 -0600321/**
322 * acpi_device_write_i2c_dev() - Write an I2C device to ACPI
323 *
324 * This creates a I2cSerialBus descriptor for an I2C device, including
325 * information ACPI needs to use it.
326 *
327 * @ctx: ACPI context pointer
328 * @dev: I2C device to write
329 * @return I2C address of device if OK, -ve on error
330 */
331int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev);
332
Simon Glass711fd982020-07-07 13:11:49 -0600333/**
334 * acpi_device_write_spi_dev() - Write a SPI device to ACPI
335 *
336 * This writes a serial bus descriptor for the SPI device so that ACPI can use
337 * it
338 *
339 * @ctx: ACPI context pointer
340 * @dev: SPI device to write
341 * @return 0 if OK, -ve on error
342 */
343int acpi_device_write_spi_dev(struct acpi_ctx *ctx, const struct udevice *dev);
344
Simon Glassebb2e832020-07-07 13:11:39 -0600345#endif