blob: 7a65dadbb2f4d364ebf5d539a4c3e149f9e2c130 [file] [log] [blame]
Simon Glass1361a532020-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 Glass31e17872020-07-07 13:11:48 -060012#include <i2c.h>
Simon Glass2715b362020-07-07 13:11:40 -060013#include <linux/bitops.h>
14
Simon Glassff715c62020-07-07 13:11:43 -060015struct acpi_ctx;
Simon Glassa9e0a072020-07-07 13:11:46 -060016struct gpio_desc;
Simon Glassff715c62020-07-07 13:11:43 -060017struct irq;
Simon Glass1361a532020-07-07 13:11:39 -060018struct udevice;
19
Simon Glassf4955132020-07-07 13:11:41 -060020/* ACPI descriptor values for common descriptors: SERIAL_BUS means I2C */
21#define ACPI_DESCRIPTOR_LARGE BIT(7)
22#define ACPI_DESCRIPTOR_INTERRUPT (ACPI_DESCRIPTOR_LARGE | 9)
23#define ACPI_DESCRIPTOR_GPIO (ACPI_DESCRIPTOR_LARGE | 12)
24#define ACPI_DESCRIPTOR_SERIAL_BUS (ACPI_DESCRIPTOR_LARGE | 14)
25
Simon Glass1361a532020-07-07 13:11:39 -060026/* Length of a full path to an ACPI device */
27#define ACPI_PATH_MAX 30
28
Simon Glass2715b362020-07-07 13:11:40 -060029/* Values that can be returned for ACPI device _STA method */
30enum acpi_dev_status {
31 ACPI_DSTATUS_PRESENT = BIT(0),
32 ACPI_DSTATUS_ENABLED = BIT(1),
33 ACPI_DSTATUS_SHOW_IN_UI = BIT(2),
34 ACPI_DSTATUS_OK = BIT(3),
35 ACPI_DSTATUS_HAS_BATTERY = BIT(4),
36
37 ACPI_DSTATUS_ALL_OFF = 0,
38 ACPI_DSTATUS_HIDDEN_ON = ACPI_DSTATUS_PRESENT | ACPI_DSTATUS_ENABLED |
39 ACPI_DSTATUS_OK,
40 ACPI_DSTATUS_ALL_ON = ACPI_DSTATUS_HIDDEN_ON |
41 ACPI_DSTATUS_SHOW_IN_UI,
42};
43
Simon Glassf4955132020-07-07 13:11:41 -060044/** enum acpi_irq_mode - edge/level trigger mode */
45enum acpi_irq_mode {
46 ACPI_IRQ_EDGE_TRIGGERED,
47 ACPI_IRQ_LEVEL_TRIGGERED,
48};
49
50/**
51 * enum acpi_irq_polarity - polarity of interrupt
52 *
53 * @ACPI_IRQ_ACTIVE_LOW - for ACPI_IRQ_EDGE_TRIGGERED this means falling edge
54 * @ACPI_IRQ_ACTIVE_HIGH - for ACPI_IRQ_EDGE_TRIGGERED this means rising edge
55 * @ACPI_IRQ_ACTIVE_BOTH - not meaningful for ACPI_IRQ_EDGE_TRIGGERED
56 */
57enum acpi_irq_polarity {
58 ACPI_IRQ_ACTIVE_LOW,
59 ACPI_IRQ_ACTIVE_HIGH,
60 ACPI_IRQ_ACTIVE_BOTH,
61};
62
63/**
64 * enum acpi_irq_shared - whether interrupt is shared or not
65 *
66 * @ACPI_IRQ_EXCLUSIVE: only this device uses the interrupt
67 * @ACPI_IRQ_SHARED: other devices may use this interrupt
68 */
69enum acpi_irq_shared {
70 ACPI_IRQ_EXCLUSIVE,
71 ACPI_IRQ_SHARED,
72};
73
74/** enum acpi_irq_wake - indicates whether this interrupt can wake the device */
75enum acpi_irq_wake {
76 ACPI_IRQ_NO_WAKE,
77 ACPI_IRQ_WAKE,
78};
79
80/**
81 * struct acpi_irq - representation of an ACPI interrupt
82 *
83 * @pin: ACPI pin that is monitored for the interrupt
84 * @mode: Edge/level triggering
85 * @polarity: Interrupt polarity
86 * @shared: Whether interrupt is shared or not
87 * @wake: Whether interrupt can wake the device from sleep
88 */
89struct acpi_irq {
90 unsigned int pin;
91 enum acpi_irq_mode mode;
92 enum acpi_irq_polarity polarity;
93 enum acpi_irq_shared shared;
94 enum acpi_irq_wake wake;
95};
96
Simon Glass1361a532020-07-07 13:11:39 -060097/**
Simon Glass29126862020-07-07 13:11:44 -060098 * enum acpi_gpio_type - type of the descriptor
99 *
100 * @ACPI_GPIO_TYPE_INTERRUPT: GpioInterrupt
101 * @ACPI_GPIO_TYPE_IO: GpioIo
102 */
103enum acpi_gpio_type {
104 ACPI_GPIO_TYPE_INTERRUPT,
105 ACPI_GPIO_TYPE_IO,
106};
107
108/**
109 * enum acpi_gpio_pull - pull direction
110 *
111 * @ACPI_GPIO_PULL_DEFAULT: Use default value for pin
112 * @ACPI_GPIO_PULL_UP: Pull up
113 * @ACPI_GPIO_PULL_DOWN: Pull down
114 * @ACPI_GPIO_PULL_NONE: No pullup/pulldown
115 */
116enum acpi_gpio_pull {
117 ACPI_GPIO_PULL_DEFAULT,
118 ACPI_GPIO_PULL_UP,
119 ACPI_GPIO_PULL_DOWN,
120 ACPI_GPIO_PULL_NONE,
121};
122
123/**
124 * enum acpi_gpio_io_restrict - controls input/output of pin
125 *
126 * @ACPI_GPIO_IO_RESTRICT_NONE: no restrictions
127 * @ACPI_GPIO_IO_RESTRICT_INPUT: input only (no output)
128 * @ACPI_GPIO_IO_RESTRICT_OUTPUT: output only (no input)
129 * @ACPI_GPIO_IO_RESTRICT_PRESERVE: preserve settings when driver not active
130 */
131enum acpi_gpio_io_restrict {
132 ACPI_GPIO_IO_RESTRICT_NONE,
133 ACPI_GPIO_IO_RESTRICT_INPUT,
134 ACPI_GPIO_IO_RESTRICT_OUTPUT,
135 ACPI_GPIO_IO_RESTRICT_PRESERVE,
136};
137
138/** enum acpi_gpio_polarity - controls the GPIO polarity */
139enum acpi_gpio_polarity {
140 ACPI_GPIO_ACTIVE_HIGH = 0,
141 ACPI_GPIO_ACTIVE_LOW = 1,
142};
143
144#define ACPI_GPIO_REVISION_ID 1
145#define ACPI_GPIO_MAX_PINS 2
146
147/**
148 * struct acpi_gpio - representation of an ACPI GPIO
149 *
150 * @pin_count: Number of pins represented
151 * @pins: List of pins
152 * @pin0_addr: Address in memory of the control registers for pin 0. This is
153 * used when generating ACPI tables
154 * @type: GPIO type
155 * @pull: Pullup/pulldown setting
156 * @resource: Resource name for this GPIO controller
157 * For GpioInt:
158 * @interrupt_debounce_timeout: Debounce timeout in units of 10us
159 * @irq: Interrupt
160 *
161 * For GpioIo:
162 * @output_drive_strength: Drive strength in units of 10uA
163 * @io_shared; true if GPIO is shared
164 * @io_restrict: I/O restriction setting
165 * @polarity: GPIO polarity
166 */
167struct acpi_gpio {
168 int pin_count;
169 u16 pins[ACPI_GPIO_MAX_PINS];
170 ulong pin0_addr;
171
172 enum acpi_gpio_type type;
173 enum acpi_gpio_pull pull;
174 char resource[ACPI_PATH_MAX];
175
176 /* GpioInt */
177 u16 interrupt_debounce_timeout;
178 struct acpi_irq irq;
179
180 /* GpioIo */
181 u16 output_drive_strength;
182 bool io_shared;
183 enum acpi_gpio_io_restrict io_restrict;
184 enum acpi_gpio_polarity polarity;
185};
186
Simon Glass31e17872020-07-07 13:11:48 -0600187/* ACPI Descriptors for Serial Bus interfaces */
188#define ACPI_SERIAL_BUS_TYPE_I2C 1
189#define ACPI_I2C_SERIAL_BUS_REVISION_ID 1 /* TODO: upgrade to 2 */
190#define ACPI_I2C_TYPE_SPECIFIC_REVISION_ID 1
191
192/**
193 * struct acpi_i2c - representation of an ACPI I2C device
194 *
195 * @address: 7-bit or 10-bit I2C address
196 * @mode_10bit: Which address size is used
197 * @speed: Bus speed in Hz
198 * @resource: Resource name for the I2C controller
199 */
200struct acpi_i2c {
201 u16 address;
202 enum i2c_address_mode mode_10bit;
203 enum i2c_speed_rate speed;
204 const char *resource;
205};
206
Simon Glass29126862020-07-07 13:11:44 -0600207/**
Simon Glass1361a532020-07-07 13:11:39 -0600208 * acpi_device_path() - Get the full path to an ACPI device
209 *
210 * This gets the full path in the form XXXX.YYYY.ZZZZ where XXXX is the root
211 * and ZZZZ is the device. All parent devices are added to the path.
212 *
213 * @dev: Device to check
214 * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
215 * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
216 * @return 0 if OK, -ve on error
217 */
218int acpi_device_path(const struct udevice *dev, char *buf, int maxlen);
219
220/**
221 * acpi_device_scope() - Get the scope of an ACPI device
222 *
223 * This gets the scope which is the full path of the parent device, as per
224 * acpi_device_path().
225 *
226 * @dev: Device to check
227 * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
228 * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
229 * @return 0 if OK, -EINVAL if the device has no parent, other -ve on other
230 * error
231 */
232int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen);
233
Simon Glass2715b362020-07-07 13:11:40 -0600234/**
235 * acpi_device_status() - Get the status of a device
236 *
237 * This currently just returns ACPI_DSTATUS_ALL_ON. It does not support
238 * inactive or hidden devices.
239 *
240 * @dev: Device to check
241 * @return device status, as ACPI_DSTATUS_...
242 */
243enum acpi_dev_status acpi_device_status(const struct udevice *dev);
244
Simon Glassff715c62020-07-07 13:11:43 -0600245/**
246 * acpi_device_write_interrupt_irq() - Write an interrupt descriptor
247 *
248 * This writes an ACPI interrupt descriptor for the given interrupt, converting
249 * fields as needed.
250 *
251 * @ctx: ACPI context pointer
252 * @req_irq: Interrupt to output
253 * @return IRQ pin number if OK, -ve on error
254 */
255int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
256 const struct irq *req_irq);
257
Simon Glassa9e0a072020-07-07 13:11:46 -0600258/**
259 * acpi_device_write_gpio() - Write GpioIo() or GpioInt() descriptor
260 *
261 * @gpio: GPIO information to write
262 * @return GPIO pin number of first GPIO if OK, -ve on error
263 */
264int acpi_device_write_gpio(struct acpi_ctx *ctx, const struct acpi_gpio *gpio);
265
266/**
267 * acpi_device_write_gpio_desc() - Write a GPIO to ACPI
268 *
269 * This creates a GPIO descriptor for a GPIO, including information ACPI needs
270 * to use it.
271 *
272 * @ctx: ACPI context pointer
273 * @desc: GPIO to write
274 * @return 0 if OK, -ve on error
275 */
276int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
277 const struct gpio_desc *desc);
278
Simon Glass4ebc9402020-07-07 13:11:47 -0600279/**
280 * acpi_device_write_interrupt_or_gpio() - Write interrupt or GPIO to ACPI
281 *
282 * This reads an interrupt from the device tree "interrupts-extended" property,
283 * if available. If not it reads the first GPIO with the name @prop.
284 *
285 * If an interrupt is found, an ACPI interrupt descriptor is written to the ACPI
286 * output. If not, but if a GPIO is found, a GPIO descriptor is written.
287 *
288 * @return irq or GPIO pin number if OK, -ve if neither an interrupt nor a GPIO
289 * could be found, or some other error occurred
290 */
291int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
292 struct udevice *dev, const char *prop);
293
Simon Glass31e17872020-07-07 13:11:48 -0600294/**
295 * acpi_device_write_i2c_dev() - Write an I2C device to ACPI
296 *
297 * This creates a I2cSerialBus descriptor for an I2C device, including
298 * information ACPI needs to use it.
299 *
300 * @ctx: ACPI context pointer
301 * @dev: I2C device to write
302 * @return I2C address of device if OK, -ve on error
303 */
304int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev);
305
Simon Glass1361a532020-07-07 13:11:39 -0600306#endif