blob: 1b838fcb8576b1d08d42fc40bfa5f282c08efd8b [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 Glass31cc5b42020-09-22 12:45:01 -060013#include <irq.h>
Simon Glass711fd982020-07-07 13:11:49 -060014#include <spi.h>
Simon Glass31cc5b42020-09-22 12:45:01 -060015#include <asm-generic/gpio.h>
Simon Glass8965cc92020-07-07 13:11:40 -060016#include <linux/bitops.h>
17
Simon Glass3d39c132020-07-07 13:11:43 -060018struct acpi_ctx;
Simon Glass4fa63312020-07-07 13:11:46 -060019struct gpio_desc;
Simon Glass3d39c132020-07-07 13:11:43 -060020struct irq;
Simon Glassebb2e832020-07-07 13:11:39 -060021struct udevice;
22
Simon Glassb24cbf42020-07-07 13:11:41 -060023/* ACPI descriptor values for common descriptors: SERIAL_BUS means I2C */
24#define ACPI_DESCRIPTOR_LARGE BIT(7)
Simon Glass837127f2020-07-07 21:32:11 -060025#define ACPI_DESCRIPTOR_REGISTER (ACPI_DESCRIPTOR_LARGE | 2)
Simon Glassb24cbf42020-07-07 13:11:41 -060026#define ACPI_DESCRIPTOR_INTERRUPT (ACPI_DESCRIPTOR_LARGE | 9)
27#define ACPI_DESCRIPTOR_GPIO (ACPI_DESCRIPTOR_LARGE | 12)
28#define ACPI_DESCRIPTOR_SERIAL_BUS (ACPI_DESCRIPTOR_LARGE | 14)
29
Simon Glassebb2e832020-07-07 13:11:39 -060030/* Length of a full path to an ACPI device */
31#define ACPI_PATH_MAX 30
32
Simon Glass4c8cbbd2020-09-22 12:44:58 -060033/* UUID for an I2C _DSM method */
34#define ACPI_DSM_I2C_HID_UUID "3cdff6f7-4267-4555-ad05-b30a3d8938de"
35
Simon Glass8965cc92020-07-07 13:11:40 -060036/* Values that can be returned for ACPI device _STA method */
37enum acpi_dev_status {
38 ACPI_DSTATUS_PRESENT = BIT(0),
39 ACPI_DSTATUS_ENABLED = BIT(1),
40 ACPI_DSTATUS_SHOW_IN_UI = BIT(2),
41 ACPI_DSTATUS_OK = BIT(3),
42 ACPI_DSTATUS_HAS_BATTERY = BIT(4),
43
44 ACPI_DSTATUS_ALL_OFF = 0,
45 ACPI_DSTATUS_HIDDEN_ON = ACPI_DSTATUS_PRESENT | ACPI_DSTATUS_ENABLED |
46 ACPI_DSTATUS_OK,
47 ACPI_DSTATUS_ALL_ON = ACPI_DSTATUS_HIDDEN_ON |
48 ACPI_DSTATUS_SHOW_IN_UI,
49};
50
Simon Glassb24cbf42020-07-07 13:11:41 -060051/** enum acpi_irq_mode - edge/level trigger mode */
52enum acpi_irq_mode {
53 ACPI_IRQ_EDGE_TRIGGERED,
54 ACPI_IRQ_LEVEL_TRIGGERED,
55};
56
57/**
58 * enum acpi_irq_polarity - polarity of interrupt
59 *
60 * @ACPI_IRQ_ACTIVE_LOW - for ACPI_IRQ_EDGE_TRIGGERED this means falling edge
61 * @ACPI_IRQ_ACTIVE_HIGH - for ACPI_IRQ_EDGE_TRIGGERED this means rising edge
62 * @ACPI_IRQ_ACTIVE_BOTH - not meaningful for ACPI_IRQ_EDGE_TRIGGERED
63 */
64enum acpi_irq_polarity {
65 ACPI_IRQ_ACTIVE_LOW,
66 ACPI_IRQ_ACTIVE_HIGH,
67 ACPI_IRQ_ACTIVE_BOTH,
68};
69
70/**
71 * enum acpi_irq_shared - whether interrupt is shared or not
72 *
73 * @ACPI_IRQ_EXCLUSIVE: only this device uses the interrupt
74 * @ACPI_IRQ_SHARED: other devices may use this interrupt
75 */
76enum acpi_irq_shared {
77 ACPI_IRQ_EXCLUSIVE,
78 ACPI_IRQ_SHARED,
79};
80
81/** enum acpi_irq_wake - indicates whether this interrupt can wake the device */
82enum acpi_irq_wake {
83 ACPI_IRQ_NO_WAKE,
84 ACPI_IRQ_WAKE,
85};
86
87/**
88 * struct acpi_irq - representation of an ACPI interrupt
89 *
90 * @pin: ACPI pin that is monitored for the interrupt
91 * @mode: Edge/level triggering
92 * @polarity: Interrupt polarity
93 * @shared: Whether interrupt is shared or not
94 * @wake: Whether interrupt can wake the device from sleep
95 */
96struct acpi_irq {
97 unsigned int pin;
98 enum acpi_irq_mode mode;
99 enum acpi_irq_polarity polarity;
100 enum acpi_irq_shared shared;
101 enum acpi_irq_wake wake;
102};
103
Simon Glassebb2e832020-07-07 13:11:39 -0600104/**
Simon Glass3176b6c2020-07-07 13:11:44 -0600105 * enum acpi_gpio_type - type of the descriptor
106 *
107 * @ACPI_GPIO_TYPE_INTERRUPT: GpioInterrupt
108 * @ACPI_GPIO_TYPE_IO: GpioIo
109 */
110enum acpi_gpio_type {
111 ACPI_GPIO_TYPE_INTERRUPT,
112 ACPI_GPIO_TYPE_IO,
113};
114
115/**
116 * enum acpi_gpio_pull - pull direction
117 *
118 * @ACPI_GPIO_PULL_DEFAULT: Use default value for pin
119 * @ACPI_GPIO_PULL_UP: Pull up
120 * @ACPI_GPIO_PULL_DOWN: Pull down
121 * @ACPI_GPIO_PULL_NONE: No pullup/pulldown
122 */
123enum acpi_gpio_pull {
124 ACPI_GPIO_PULL_DEFAULT,
125 ACPI_GPIO_PULL_UP,
126 ACPI_GPIO_PULL_DOWN,
127 ACPI_GPIO_PULL_NONE,
128};
129
130/**
131 * enum acpi_gpio_io_restrict - controls input/output of pin
132 *
133 * @ACPI_GPIO_IO_RESTRICT_NONE: no restrictions
134 * @ACPI_GPIO_IO_RESTRICT_INPUT: input only (no output)
135 * @ACPI_GPIO_IO_RESTRICT_OUTPUT: output only (no input)
136 * @ACPI_GPIO_IO_RESTRICT_PRESERVE: preserve settings when driver not active
137 */
138enum acpi_gpio_io_restrict {
139 ACPI_GPIO_IO_RESTRICT_NONE,
140 ACPI_GPIO_IO_RESTRICT_INPUT,
141 ACPI_GPIO_IO_RESTRICT_OUTPUT,
142 ACPI_GPIO_IO_RESTRICT_PRESERVE,
143};
144
145/** enum acpi_gpio_polarity - controls the GPIO polarity */
146enum acpi_gpio_polarity {
147 ACPI_GPIO_ACTIVE_HIGH = 0,
148 ACPI_GPIO_ACTIVE_LOW = 1,
149};
150
151#define ACPI_GPIO_REVISION_ID 1
152#define ACPI_GPIO_MAX_PINS 2
153
154/**
155 * struct acpi_gpio - representation of an ACPI GPIO
156 *
157 * @pin_count: Number of pins represented
158 * @pins: List of pins
159 * @pin0_addr: Address in memory of the control registers for pin 0. This is
160 * used when generating ACPI tables
161 * @type: GPIO type
162 * @pull: Pullup/pulldown setting
163 * @resource: Resource name for this GPIO controller
164 * For GpioInt:
165 * @interrupt_debounce_timeout: Debounce timeout in units of 10us
166 * @irq: Interrupt
167 *
168 * For GpioIo:
169 * @output_drive_strength: Drive strength in units of 10uA
170 * @io_shared; true if GPIO is shared
171 * @io_restrict: I/O restriction setting
172 * @polarity: GPIO polarity
173 */
174struct acpi_gpio {
175 int pin_count;
176 u16 pins[ACPI_GPIO_MAX_PINS];
177 ulong pin0_addr;
178
179 enum acpi_gpio_type type;
180 enum acpi_gpio_pull pull;
181 char resource[ACPI_PATH_MAX];
182
183 /* GpioInt */
184 u16 interrupt_debounce_timeout;
185 struct acpi_irq irq;
186
187 /* GpioIo */
188 u16 output_drive_strength;
189 bool io_shared;
190 enum acpi_gpio_io_restrict io_restrict;
191 enum acpi_gpio_polarity polarity;
192};
193
Simon Glass39ab8672020-07-07 13:11:48 -0600194/* ACPI Descriptors for Serial Bus interfaces */
195#define ACPI_SERIAL_BUS_TYPE_I2C 1
Simon Glass711fd982020-07-07 13:11:49 -0600196#define ACPI_SERIAL_BUS_TYPE_SPI 2
Simon Glass39ab8672020-07-07 13:11:48 -0600197#define ACPI_I2C_SERIAL_BUS_REVISION_ID 1 /* TODO: upgrade to 2 */
198#define ACPI_I2C_TYPE_SPECIFIC_REVISION_ID 1
Simon Glass711fd982020-07-07 13:11:49 -0600199#define ACPI_SPI_SERIAL_BUS_REVISION_ID 1
200#define ACPI_SPI_TYPE_SPECIFIC_REVISION_ID 1
Simon Glass39ab8672020-07-07 13:11:48 -0600201
202/**
203 * struct acpi_i2c - representation of an ACPI I2C device
204 *
205 * @address: 7-bit or 10-bit I2C address
206 * @mode_10bit: Which address size is used
207 * @speed: Bus speed in Hz
208 * @resource: Resource name for the I2C controller
209 */
210struct acpi_i2c {
211 u16 address;
212 enum i2c_address_mode mode_10bit;
213 enum i2c_speed_rate speed;
214 const char *resource;
215};
216
Simon Glass3176b6c2020-07-07 13:11:44 -0600217/**
Simon Glass711fd982020-07-07 13:11:49 -0600218 * struct acpi_spi - representation of an ACPI SPI device
219 *
220 * @device_select: Chip select used by this device (typically 0)
221 * @device_select_polarity: Polarity for the device
222 * @wire_mode: Number of wires used for SPI
223 * @speed: Bus speed in Hz
224 * @data_bit_length: Word length for SPI (typically 8)
225 * @clock_phase: Clock phase to capture data
226 * @clock_polarity: Bus polarity
227 * @resource: Resource name for the SPI controller
228 */
229struct acpi_spi {
230 u16 device_select;
231 enum spi_polarity device_select_polarity;
232 enum spi_wire_mode wire_mode;
233 unsigned int speed;
234 u8 data_bit_length;
235 enum spi_clock_phase clock_phase;
236 enum spi_polarity clock_polarity;
237 const char *resource;
238};
239
240/**
Simon Glass31cc5b42020-09-22 12:45:01 -0600241 * struct acpi_i2c_priv - Information read from device tree
242 *
243 * This is used by devices which want to specify various pieces of ACPI
244 * information, including power control. It allows a generic function to
245 * generate the information for ACPI, based on device-tree properties.
246 *
247 * @disable_gpio_export_in_crs: Don't export GPIOs in the CRS
248 * @reset_gpio: GPIO used to assert reset to the device
249 * @enable_gpio: GPIO used to enable the device
250 * @stop_gpio: GPIO used to stop the device
251 * @irq_gpio: GPIO used for interrupt (if @irq is not used)
252 * @irq: IRQ used for interrupt (if @irq_gpio is not used)
253 * @hid: _HID value for device (required)
254 * @uid: _UID value for device
255 * @desc: _DDN value for device
256 * @wake: Wake event, e.g. GPE0_DW1_15; 0 if none
257 * @property_count: Number of other DSD properties (currently always 0)
258 * @probed: true set set 'linux,probed' property
259 * @compat_string: Device tree compatible string to report through ACPI
260 * @has_power_resource: true if this device has a power resource
261 * @reset_delay_ms: Delay after de-asserting reset, in ms
262 * @reset_off_delay_ms: Delay after asserting reset (during power off)
263 * @enable_delay_ms: Delay after asserting enable
264 * @enable_off_delay_ms: Delay after de-asserting enable (during power off)
265 * @stop_delay_ms: Delay after de-aserting stop
266 * @stop_off_delay_ms: Delay after asserting stop (during power off)
267 * @hid_desc_reg_offset: HID register offset (for Human Interface Devices)
268 */
269struct acpi_i2c_priv {
270 bool disable_gpio_export_in_crs;
271 struct gpio_desc reset_gpio;
272 struct gpio_desc enable_gpio;
273 struct gpio_desc irq_gpio;
274 struct gpio_desc stop_gpio;
275 struct irq irq;
276 const char *hid;
277 u32 uid;
278 const char *desc;
279 u32 wake;
280 u32 property_count;
281 bool probed;
282 const char *compat_string;
283 bool has_power_resource;
284 u32 reset_delay_ms;
285 u32 reset_off_delay_ms;
286 u32 enable_delay_ms;
287 u32 enable_off_delay_ms;
288 u32 stop_delay_ms;
289 u32 stop_off_delay_ms;
290 u32 hid_desc_reg_offset;
291};
292
293/**
Simon Glassebb2e832020-07-07 13:11:39 -0600294 * acpi_device_path() - Get the full path to an ACPI device
295 *
296 * This gets the full path in the form XXXX.YYYY.ZZZZ where XXXX is the root
297 * and ZZZZ is the device. All parent devices are added to the path.
298 *
299 * @dev: Device to check
300 * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
301 * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
302 * @return 0 if OK, -ve on error
303 */
304int acpi_device_path(const struct udevice *dev, char *buf, int maxlen);
305
306/**
307 * acpi_device_scope() - Get the scope of an ACPI device
308 *
309 * This gets the scope which is the full path of the parent device, as per
310 * acpi_device_path().
311 *
312 * @dev: Device to check
313 * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
314 * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
315 * @return 0 if OK, -EINVAL if the device has no parent, other -ve on other
316 * error
317 */
318int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen);
319
Simon Glass8965cc92020-07-07 13:11:40 -0600320/**
321 * acpi_device_status() - Get the status of a device
322 *
323 * This currently just returns ACPI_DSTATUS_ALL_ON. It does not support
324 * inactive or hidden devices.
325 *
326 * @dev: Device to check
327 * @return device status, as ACPI_DSTATUS_...
328 */
329enum acpi_dev_status acpi_device_status(const struct udevice *dev);
330
Simon Glass3d39c132020-07-07 13:11:43 -0600331/**
332 * acpi_device_write_interrupt_irq() - Write an interrupt descriptor
333 *
334 * This writes an ACPI interrupt descriptor for the given interrupt, converting
335 * fields as needed.
336 *
337 * @ctx: ACPI context pointer
338 * @req_irq: Interrupt to output
339 * @return IRQ pin number if OK, -ve on error
340 */
341int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
342 const struct irq *req_irq);
343
Simon Glass4fa63312020-07-07 13:11:46 -0600344/**
345 * acpi_device_write_gpio() - Write GpioIo() or GpioInt() descriptor
346 *
347 * @gpio: GPIO information to write
348 * @return GPIO pin number of first GPIO if OK, -ve on error
349 */
350int acpi_device_write_gpio(struct acpi_ctx *ctx, const struct acpi_gpio *gpio);
351
352/**
353 * acpi_device_write_gpio_desc() - Write a GPIO to ACPI
354 *
355 * This creates a GPIO descriptor for a GPIO, including information ACPI needs
356 * to use it.
357 *
358 * @ctx: ACPI context pointer
359 * @desc: GPIO to write
360 * @return 0 if OK, -ve on error
361 */
362int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
363 const struct gpio_desc *desc);
364
Simon Glasse2bbb132020-07-07 13:11:47 -0600365/**
366 * acpi_device_write_interrupt_or_gpio() - Write interrupt or GPIO to ACPI
367 *
368 * This reads an interrupt from the device tree "interrupts-extended" property,
369 * if available. If not it reads the first GPIO with the name @prop.
370 *
371 * If an interrupt is found, an ACPI interrupt descriptor is written to the ACPI
372 * output. If not, but if a GPIO is found, a GPIO descriptor is written.
373 *
374 * @return irq or GPIO pin number if OK, -ve if neither an interrupt nor a GPIO
375 * could be found, or some other error occurred
376 */
377int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
378 struct udevice *dev, const char *prop);
379
Simon Glass39ab8672020-07-07 13:11:48 -0600380/**
Simon Glass4c8cbbd2020-09-22 12:44:58 -0600381 * acpi_device_write_dsm_i2c_hid() - Write a device-specific method for HID
382 *
383 * This writes a DSM for an I2C Human-Interface Device based on the config
384 * provided
385 *
386 * @hid_desc_reg_offset: HID register offset
387 */
388int acpi_device_write_dsm_i2c_hid(struct acpi_ctx *ctx,
389 int hid_desc_reg_offset);
390
391/**
Simon Glass39ab8672020-07-07 13:11:48 -0600392 * acpi_device_write_i2c_dev() - Write an I2C device to ACPI
393 *
394 * This creates a I2cSerialBus descriptor for an I2C device, including
395 * information ACPI needs to use it.
396 *
397 * @ctx: ACPI context pointer
398 * @dev: I2C device to write
399 * @return I2C address of device if OK, -ve on error
400 */
401int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev);
402
Simon Glass711fd982020-07-07 13:11:49 -0600403/**
404 * acpi_device_write_spi_dev() - Write a SPI device to ACPI
405 *
406 * This writes a serial bus descriptor for the SPI device so that ACPI can use
407 * it
408 *
409 * @ctx: ACPI context pointer
410 * @dev: SPI device to write
411 * @return 0 if OK, -ve on error
412 */
413int acpi_device_write_spi_dev(struct acpi_ctx *ctx, const struct udevice *dev);
414
Simon Glasscec3af02020-07-07 13:12:02 -0600415/**
416 * acpi_device_add_power_res() - Add a basic PowerResource block for a device
417 *
418 * This includes GPIOs to control enable, reset and stop operation of the
419 * device. Each GPIO is optional, but at least one must be provided.
420 * This can be applied to any device that has power control, so is fairly
421 * generic.
422 *
423 * Reset - Put the device into / take the device out of reset.
424 * Enable - Enable / disable power to device.
425 * Stop - Stop / start operation of device.
426 *
427 * @ctx: ACPI context pointer
428 * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
429 * PAD_CFG0_TX_STATE
430 * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0"
431 * @dw0_write: Name to use to read dw0, e.g. "\\_SB.SPC0"
432 * @reset_gpio: GPIO used to take device out of reset or to put it into reset
433 * @reset_delay_ms: Delay to be inserted after device is taken out of reset
434 * (_ON method delay)
435 * @reset_off_delay_ms: Delay to be inserted after device is put into reset
436 * (_OFF method delay)
437 * @enable_gpio: GPIO used to enable device
438 * @enable_delay_ms: Delay to be inserted after device is enabled
439 * @enable_off_delay_ms: Delay to be inserted after device is disabled
440 * (_OFF method delay)
441 * @stop_gpio: GPIO used to stop operation of device
442 * @stop_delay_ms: Delay to be inserted after disabling stop (_ON method delay)
443 * @stop_off_delay_ms: Delay to be inserted after enabling stop.
444 * (_OFF method delay)
445 *
446 * @return 0 if OK, -ve if at least one GPIO is not provided
447 */
448int acpi_device_add_power_res(struct acpi_ctx *ctx, u32 tx_state_val,
449 const char *dw0_read, const char *dw0_write,
450 const struct gpio_desc *reset_gpio,
451 uint reset_delay_ms, uint reset_off_delay_ms,
452 const struct gpio_desc *enable_gpio,
453 uint enable_delay_ms, uint enable_off_delay_ms,
454 const struct gpio_desc *stop_gpio,
455 uint stop_delay_ms, uint stop_off_delay_ms);
456
Simon Glass09642392020-07-07 13:12:11 -0600457/**
458 * acpi_device_infer_name() - Infer the name from its uclass or parent
459 *
460 * Many ACPI devices have a standard name that can be inferred from the uclass
461 * they are in, or the uclass of their parent. These rules are implemented in
462 * this function. It attempts to produce a name for a device based on these
463 * rules.
464 *
465 * NOTE: This currently supports only x86 devices. Feel free to enhance it for
466 * other architectures as needed.
467 *
468 * @dev: Device to check
469 * @out_name: Place to put the name (must hold ACPI_NAME_MAX bytes)
470 * @return 0 if a name was found, -ENOENT if not found, -ENXIO if the device
471 * sequence number could not be determined
472 */
473int acpi_device_infer_name(const struct udevice *dev, char *out_name);
474
Simon Glassebb2e832020-07-07 13:11:39 -0600475#endif