Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 1 | /* 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 Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 12 | #include <i2c.h> |
Simon Glass | 711fd98 | 2020-07-07 13:11:49 -0600 | [diff] [blame] | 13 | #include <spi.h> |
Simon Glass | 8965cc9 | 2020-07-07 13:11:40 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
| 15 | |
Simon Glass | 3d39c13 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 16 | struct acpi_ctx; |
Simon Glass | 4fa6331 | 2020-07-07 13:11:46 -0600 | [diff] [blame] | 17 | struct gpio_desc; |
Simon Glass | 3d39c13 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 18 | struct irq; |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 19 | struct udevice; |
| 20 | |
Simon Glass | b24cbf4 | 2020-07-07 13:11:41 -0600 | [diff] [blame] | 21 | /* 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 Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 27 | /* Length of a full path to an ACPI device */ |
| 28 | #define ACPI_PATH_MAX 30 |
| 29 | |
Simon Glass | 8965cc9 | 2020-07-07 13:11:40 -0600 | [diff] [blame] | 30 | /* Values that can be returned for ACPI device _STA method */ |
| 31 | enum 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 Glass | b24cbf4 | 2020-07-07 13:11:41 -0600 | [diff] [blame] | 45 | /** enum acpi_irq_mode - edge/level trigger mode */ |
| 46 | enum 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 | */ |
| 58 | enum 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 | */ |
| 70 | enum 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 */ |
| 76 | enum 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 | */ |
| 90 | struct 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 Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 98 | /** |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 99 | * enum acpi_gpio_type - type of the descriptor |
| 100 | * |
| 101 | * @ACPI_GPIO_TYPE_INTERRUPT: GpioInterrupt |
| 102 | * @ACPI_GPIO_TYPE_IO: GpioIo |
| 103 | */ |
| 104 | enum 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 | */ |
| 117 | enum 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 | */ |
| 132 | enum 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 */ |
| 140 | enum 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 | */ |
| 168 | struct 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 Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 188 | /* ACPI Descriptors for Serial Bus interfaces */ |
| 189 | #define ACPI_SERIAL_BUS_TYPE_I2C 1 |
Simon Glass | 711fd98 | 2020-07-07 13:11:49 -0600 | [diff] [blame] | 190 | #define ACPI_SERIAL_BUS_TYPE_SPI 2 |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 191 | #define ACPI_I2C_SERIAL_BUS_REVISION_ID 1 /* TODO: upgrade to 2 */ |
| 192 | #define ACPI_I2C_TYPE_SPECIFIC_REVISION_ID 1 |
Simon Glass | 711fd98 | 2020-07-07 13:11:49 -0600 | [diff] [blame] | 193 | #define ACPI_SPI_SERIAL_BUS_REVISION_ID 1 |
| 194 | #define ACPI_SPI_TYPE_SPECIFIC_REVISION_ID 1 |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 195 | |
| 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 | */ |
| 204 | struct 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 Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 211 | /** |
Simon Glass | 711fd98 | 2020-07-07 13:11:49 -0600 | [diff] [blame] | 212 | * 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 | */ |
| 223 | struct 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 Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 235 | * 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 | */ |
| 245 | int 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 | */ |
| 259 | int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen); |
| 260 | |
Simon Glass | 8965cc9 | 2020-07-07 13:11:40 -0600 | [diff] [blame] | 261 | /** |
| 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 | */ |
| 270 | enum acpi_dev_status acpi_device_status(const struct udevice *dev); |
| 271 | |
Simon Glass | 3d39c13 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 272 | /** |
| 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 | */ |
| 282 | int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx, |
| 283 | const struct irq *req_irq); |
| 284 | |
Simon Glass | 4fa6331 | 2020-07-07 13:11:46 -0600 | [diff] [blame] | 285 | /** |
| 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 | */ |
| 291 | int 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 | */ |
| 303 | int acpi_device_write_gpio_desc(struct acpi_ctx *ctx, |
| 304 | const struct gpio_desc *desc); |
| 305 | |
Simon Glass | e2bbb13 | 2020-07-07 13:11:47 -0600 | [diff] [blame] | 306 | /** |
| 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 | */ |
| 318 | int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx, |
| 319 | struct udevice *dev, const char *prop); |
| 320 | |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 321 | /** |
| 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 | */ |
| 331 | int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev); |
| 332 | |
Simon Glass | 711fd98 | 2020-07-07 13:11:49 -0600 | [diff] [blame] | 333 | /** |
| 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 | */ |
| 343 | int acpi_device_write_spi_dev(struct acpi_ctx *ctx, const struct udevice *dev); |
| 344 | |
Simon Glass | cec3af0 | 2020-07-07 13:12:02 -0600 | [diff] [blame] | 345 | /** |
| 346 | * acpi_device_add_power_res() - Add a basic PowerResource block for a device |
| 347 | * |
| 348 | * This includes GPIOs to control enable, reset and stop operation of the |
| 349 | * device. Each GPIO is optional, but at least one must be provided. |
| 350 | * This can be applied to any device that has power control, so is fairly |
| 351 | * generic. |
| 352 | * |
| 353 | * Reset - Put the device into / take the device out of reset. |
| 354 | * Enable - Enable / disable power to device. |
| 355 | * Stop - Stop / start operation of device. |
| 356 | * |
| 357 | * @ctx: ACPI context pointer |
| 358 | * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g. |
| 359 | * PAD_CFG0_TX_STATE |
| 360 | * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0" |
| 361 | * @dw0_write: Name to use to read dw0, e.g. "\\_SB.SPC0" |
| 362 | * @reset_gpio: GPIO used to take device out of reset or to put it into reset |
| 363 | * @reset_delay_ms: Delay to be inserted after device is taken out of reset |
| 364 | * (_ON method delay) |
| 365 | * @reset_off_delay_ms: Delay to be inserted after device is put into reset |
| 366 | * (_OFF method delay) |
| 367 | * @enable_gpio: GPIO used to enable device |
| 368 | * @enable_delay_ms: Delay to be inserted after device is enabled |
| 369 | * @enable_off_delay_ms: Delay to be inserted after device is disabled |
| 370 | * (_OFF method delay) |
| 371 | * @stop_gpio: GPIO used to stop operation of device |
| 372 | * @stop_delay_ms: Delay to be inserted after disabling stop (_ON method delay) |
| 373 | * @stop_off_delay_ms: Delay to be inserted after enabling stop. |
| 374 | * (_OFF method delay) |
| 375 | * |
| 376 | * @return 0 if OK, -ve if at least one GPIO is not provided |
| 377 | */ |
| 378 | int acpi_device_add_power_res(struct acpi_ctx *ctx, u32 tx_state_val, |
| 379 | const char *dw0_read, const char *dw0_write, |
| 380 | const struct gpio_desc *reset_gpio, |
| 381 | uint reset_delay_ms, uint reset_off_delay_ms, |
| 382 | const struct gpio_desc *enable_gpio, |
| 383 | uint enable_delay_ms, uint enable_off_delay_ms, |
| 384 | const struct gpio_desc *stop_gpio, |
| 385 | uint stop_delay_ms, uint stop_off_delay_ms); |
| 386 | |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 387 | #endif |