Commit | Line | Data |
---|---|---|
e29482e8 MN |
1 | /* |
2 | * ACPI helpers for GPIO API | |
3 | * | |
4 | * Copyright (C) 2012, Intel Corporation | |
5 | * Authors: Mathias Nyman <mathias.nyman@linux.intel.com> | |
6 | * Mika Westerberg <mika.westerberg@linux.intel.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | #include <linux/errno.h> | |
354567e6 | 14 | #include <linux/gpio.h> |
936e15dd | 15 | #include <linux/gpio/consumer.h> |
5ccff852 | 16 | #include <linux/gpio/driver.h> |
031ba28a | 17 | #include <linux/gpio/machine.h> |
e29482e8 | 18 | #include <linux/export.h> |
e29482e8 | 19 | #include <linux/acpi.h> |
0d1c28a4 | 20 | #include <linux/interrupt.h> |
473ed7be | 21 | #include <linux/mutex.h> |
354567e6 | 22 | #include <linux/pinctrl/pinctrl.h> |
e29482e8 | 23 | |
5ccff852 MW |
24 | #include "gpiolib.h" |
25 | ||
4b01a14b | 26 | struct acpi_gpio_event { |
7fc7acb9 | 27 | struct list_head node; |
4b01a14b | 28 | acpi_handle handle; |
7fc7acb9 RW |
29 | unsigned int pin; |
30 | unsigned int irq; | |
e46cf32c | 31 | struct gpio_desc *desc; |
7fc7acb9 RW |
32 | }; |
33 | ||
473ed7be MW |
34 | struct acpi_gpio_connection { |
35 | struct list_head node; | |
e46cf32c | 36 | unsigned int pin; |
473ed7be MW |
37 | struct gpio_desc *desc; |
38 | }; | |
39 | ||
aa92b6f6 | 40 | struct acpi_gpio_chip { |
473ed7be MW |
41 | /* |
42 | * ACPICA requires that the first field of the context parameter | |
43 | * passed to acpi_install_address_space_handler() is large enough | |
44 | * to hold struct acpi_connection_info. | |
45 | */ | |
46 | struct acpi_connection_info conn_info; | |
47 | struct list_head conns; | |
48 | struct mutex conn_lock; | |
aa92b6f6 | 49 | struct gpio_chip *chip; |
4b01a14b | 50 | struct list_head events; |
aa92b6f6 MW |
51 | }; |
52 | ||
e29482e8 MN |
53 | static int acpi_gpiochip_find(struct gpio_chip *gc, void *data) |
54 | { | |
58383c78 | 55 | if (!gc->parent) |
e29482e8 MN |
56 | return false; |
57 | ||
58383c78 | 58 | return ACPI_HANDLE(gc->parent) == data; |
e29482e8 MN |
59 | } |
60 | ||
354567e6 MW |
61 | #ifdef CONFIG_PINCTRL |
62 | /** | |
63 | * acpi_gpiochip_pin_to_gpio_offset() - translates ACPI GPIO to Linux GPIO | |
64 | * @chip: GPIO chip | |
65 | * @pin: ACPI GPIO pin number from GpioIo/GpioInt resource | |
66 | * | |
67 | * Function takes ACPI GpioIo/GpioInt pin number as a parameter and | |
68 | * translates it to a corresponding offset suitable to be passed to a | |
69 | * GPIO controller driver. | |
70 | * | |
71 | * Typically the returned offset is same as @pin, but if the GPIO | |
e1c05067 | 72 | * controller uses pin controller and the mapping is not contiguous the |
354567e6 MW |
73 | * offset might be different. |
74 | */ | |
20ec3e39 | 75 | static int acpi_gpiochip_pin_to_gpio_offset(struct gpio_device *gdev, int pin) |
354567e6 MW |
76 | { |
77 | struct gpio_pin_range *pin_range; | |
78 | ||
79 | /* If there are no ranges in this chip, use 1:1 mapping */ | |
20ec3e39 | 80 | if (list_empty(&gdev->pin_ranges)) |
354567e6 MW |
81 | return pin; |
82 | ||
20ec3e39 | 83 | list_for_each_entry(pin_range, &gdev->pin_ranges, node) { |
354567e6 MW |
84 | const struct pinctrl_gpio_range *range = &pin_range->range; |
85 | int i; | |
86 | ||
87 | if (range->pins) { | |
88 | for (i = 0; i < range->npins; i++) { | |
89 | if (range->pins[i] == pin) | |
20ec3e39 | 90 | return range->base + i - gdev->base; |
354567e6 MW |
91 | } |
92 | } else { | |
93 | if (pin >= range->pin_base && | |
94 | pin < range->pin_base + range->npins) { | |
95 | unsigned gpio_base; | |
96 | ||
20ec3e39 | 97 | gpio_base = range->base - gdev->base; |
354567e6 MW |
98 | return gpio_base + pin - range->pin_base; |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | return -EINVAL; | |
104 | } | |
105 | #else | |
20ec3e39 | 106 | static inline int acpi_gpiochip_pin_to_gpio_offset(struct gpio_device *gdev, |
354567e6 MW |
107 | int pin) |
108 | { | |
109 | return pin; | |
110 | } | |
111 | #endif | |
112 | ||
e29482e8 | 113 | /** |
936e15dd | 114 | * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API |
e29482e8 MN |
115 | * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1") |
116 | * @pin: ACPI GPIO pin number (0-based, controller-relative) | |
117 | * | |
f35bbf61 MW |
118 | * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR |
119 | * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO | |
120 | * controller does not have gpiochip registered at the moment. This is to | |
121 | * support probe deferral. | |
e29482e8 | 122 | */ |
936e15dd | 123 | static struct gpio_desc *acpi_get_gpiod(char *path, int pin) |
e29482e8 MN |
124 | { |
125 | struct gpio_chip *chip; | |
126 | acpi_handle handle; | |
127 | acpi_status status; | |
354567e6 | 128 | int offset; |
e29482e8 MN |
129 | |
130 | status = acpi_get_handle(NULL, path, &handle); | |
131 | if (ACPI_FAILURE(status)) | |
936e15dd | 132 | return ERR_PTR(-ENODEV); |
e29482e8 MN |
133 | |
134 | chip = gpiochip_find(handle, acpi_gpiochip_find); | |
135 | if (!chip) | |
f35bbf61 | 136 | return ERR_PTR(-EPROBE_DEFER); |
e29482e8 | 137 | |
20ec3e39 | 138 | offset = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, pin); |
354567e6 MW |
139 | if (offset < 0) |
140 | return ERR_PTR(offset); | |
e29482e8 | 141 | |
354567e6 | 142 | return gpiochip_get_desc(chip, offset); |
e29482e8 | 143 | } |
0d1c28a4 | 144 | |
0d1c28a4 MN |
145 | static irqreturn_t acpi_gpio_irq_handler(int irq, void *data) |
146 | { | |
6072b9dc | 147 | struct acpi_gpio_event *event = data; |
0d1c28a4 | 148 | |
6072b9dc | 149 | acpi_evaluate_object(event->handle, NULL, NULL, NULL); |
0d1c28a4 MN |
150 | |
151 | return IRQ_HANDLED; | |
152 | } | |
153 | ||
7fc7acb9 RW |
154 | static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data) |
155 | { | |
4b01a14b | 156 | struct acpi_gpio_event *event = data; |
7fc7acb9 | 157 | |
4b01a14b | 158 | acpi_execute_simple_method(event->handle, NULL, event->pin); |
7fc7acb9 RW |
159 | |
160 | return IRQ_HANDLED; | |
161 | } | |
162 | ||
aa92b6f6 | 163 | static void acpi_gpio_chip_dh(acpi_handle handle, void *data) |
7fc7acb9 RW |
164 | { |
165 | /* The address of this function is used as a key. */ | |
166 | } | |
167 | ||
6072b9dc MW |
168 | static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares, |
169 | void *context) | |
0d1c28a4 | 170 | { |
6072b9dc | 171 | struct acpi_gpio_chip *acpi_gpio = context; |
aa92b6f6 | 172 | struct gpio_chip *chip = acpi_gpio->chip; |
6072b9dc | 173 | struct acpi_resource_gpio *agpio; |
7fc7acb9 | 174 | acpi_handle handle, evt_handle; |
6072b9dc MW |
175 | struct acpi_gpio_event *event; |
176 | irq_handler_t handler = NULL; | |
177 | struct gpio_desc *desc; | |
178 | unsigned long irqflags; | |
179 | int ret, pin, irq; | |
0d1c28a4 | 180 | |
6072b9dc MW |
181 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) |
182 | return AE_OK; | |
183 | ||
184 | agpio = &ares->data.gpio; | |
185 | if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT) | |
186 | return AE_OK; | |
0d1c28a4 | 187 | |
58383c78 | 188 | handle = ACPI_HANDLE(chip->parent); |
6072b9dc MW |
189 | pin = agpio->pin_table[0]; |
190 | ||
191 | if (pin <= 255) { | |
192 | char ev_name[5]; | |
193 | sprintf(ev_name, "_%c%02X", | |
194 | agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', | |
195 | pin); | |
196 | if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle))) | |
197 | handler = acpi_gpio_irq_handler; | |
198 | } | |
199 | if (!handler) { | |
200 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle))) | |
201 | handler = acpi_gpio_irq_handler_evt; | |
202 | } | |
203 | if (!handler) | |
204 | return AE_BAD_PARAMETER; | |
0d1c28a4 | 205 | |
20ec3e39 | 206 | pin = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, pin); |
4de60970 | 207 | if (pin < 0) |
208 | return AE_BAD_PARAMETER; | |
209 | ||
abdc08a3 | 210 | desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event"); |
6072b9dc | 211 | if (IS_ERR(desc)) { |
58383c78 | 212 | dev_err(chip->parent, "Failed to request GPIO\n"); |
6072b9dc MW |
213 | return AE_ERROR; |
214 | } | |
0d1c28a4 | 215 | |
6072b9dc | 216 | gpiod_direction_input(desc); |
0d1c28a4 | 217 | |
e3a2e878 | 218 | ret = gpiochip_lock_as_irq(chip, pin); |
6072b9dc | 219 | if (ret) { |
58383c78 | 220 | dev_err(chip->parent, "Failed to lock GPIO as interrupt\n"); |
6072b9dc MW |
221 | goto fail_free_desc; |
222 | } | |
0d1c28a4 | 223 | |
6072b9dc MW |
224 | irq = gpiod_to_irq(desc); |
225 | if (irq < 0) { | |
58383c78 | 226 | dev_err(chip->parent, "Failed to translate GPIO to IRQ\n"); |
6072b9dc MW |
227 | goto fail_unlock_irq; |
228 | } | |
7fc7acb9 | 229 | |
6072b9dc MW |
230 | irqflags = IRQF_ONESHOT; |
231 | if (agpio->triggering == ACPI_LEVEL_SENSITIVE) { | |
232 | if (agpio->polarity == ACPI_ACTIVE_HIGH) | |
233 | irqflags |= IRQF_TRIGGER_HIGH; | |
234 | else | |
235 | irqflags |= IRQF_TRIGGER_LOW; | |
236 | } else { | |
237 | switch (agpio->polarity) { | |
238 | case ACPI_ACTIVE_HIGH: | |
239 | irqflags |= IRQF_TRIGGER_RISING; | |
240 | break; | |
241 | case ACPI_ACTIVE_LOW: | |
242 | irqflags |= IRQF_TRIGGER_FALLING; | |
243 | break; | |
244 | default: | |
245 | irqflags |= IRQF_TRIGGER_RISING | | |
246 | IRQF_TRIGGER_FALLING; | |
247 | break; | |
7fc7acb9 | 248 | } |
6072b9dc | 249 | } |
aa92b6f6 | 250 | |
6072b9dc MW |
251 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
252 | if (!event) | |
253 | goto fail_unlock_irq; | |
7fc7acb9 | 254 | |
6072b9dc MW |
255 | event->handle = evt_handle; |
256 | event->irq = irq; | |
257 | event->pin = pin; | |
e46cf32c | 258 | event->desc = desc; |
7fc7acb9 | 259 | |
6072b9dc MW |
260 | ret = request_threaded_irq(event->irq, NULL, handler, irqflags, |
261 | "ACPI:Event", event); | |
262 | if (ret) { | |
58383c78 LW |
263 | dev_err(chip->parent, |
264 | "Failed to setup interrupt handler for %d\n", | |
6072b9dc MW |
265 | event->irq); |
266 | goto fail_free_event; | |
0d1c28a4 | 267 | } |
6072b9dc | 268 | |
8a146fbe HG |
269 | if (agpio->wake_capable == ACPI_WAKE_CAPABLE) |
270 | enable_irq_wake(irq); | |
271 | ||
6072b9dc MW |
272 | list_add_tail(&event->node, &acpi_gpio->events); |
273 | return AE_OK; | |
274 | ||
275 | fail_free_event: | |
276 | kfree(event); | |
277 | fail_unlock_irq: | |
e3a2e878 | 278 | gpiochip_unlock_as_irq(chip, pin); |
6072b9dc MW |
279 | fail_free_desc: |
280 | gpiochip_free_own_desc(desc); | |
281 | ||
282 | return AE_ERROR; | |
0d1c28a4 | 283 | } |
7fc7acb9 | 284 | |
70b53411 | 285 | /** |
6072b9dc | 286 | * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events |
afa82fab | 287 | * @chip: GPIO chip |
70b53411 | 288 | * |
6072b9dc MW |
289 | * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are |
290 | * handled by ACPI event methods which need to be called from the GPIO | |
291 | * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which | |
292 | * gpio pins have acpi event methods and assigns interrupt handlers that calls | |
293 | * the acpi event methods for those pins. | |
294 | */ | |
afa82fab | 295 | void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) |
6072b9dc | 296 | { |
afa82fab MW |
297 | struct acpi_gpio_chip *acpi_gpio; |
298 | acpi_handle handle; | |
299 | acpi_status status; | |
300 | ||
58383c78 | 301 | if (!chip->parent || !chip->to_irq) |
afa82fab | 302 | return; |
6072b9dc | 303 | |
58383c78 | 304 | handle = ACPI_HANDLE(chip->parent); |
afa82fab MW |
305 | if (!handle) |
306 | return; | |
307 | ||
308 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); | |
309 | if (ACPI_FAILURE(status)) | |
6072b9dc MW |
310 | return; |
311 | ||
1ecb016e | 312 | acpi_walk_resources(handle, "_AEI", |
6072b9dc MW |
313 | acpi_gpiochip_request_interrupt, acpi_gpio); |
314 | } | |
2b528fff | 315 | EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts); |
6072b9dc MW |
316 | |
317 | /** | |
318 | * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts. | |
afa82fab | 319 | * @chip: GPIO chip |
70b53411 | 320 | * |
6072b9dc MW |
321 | * Free interrupts associated with GPIO ACPI event method for the given |
322 | * GPIO chip. | |
70b53411 | 323 | */ |
afa82fab | 324 | void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) |
70b53411 | 325 | { |
afa82fab | 326 | struct acpi_gpio_chip *acpi_gpio; |
4b01a14b | 327 | struct acpi_gpio_event *event, *ep; |
afa82fab MW |
328 | acpi_handle handle; |
329 | acpi_status status; | |
330 | ||
58383c78 | 331 | if (!chip->parent || !chip->to_irq) |
afa82fab | 332 | return; |
70b53411 | 333 | |
58383c78 | 334 | handle = ACPI_HANDLE(chip->parent); |
afa82fab MW |
335 | if (!handle) |
336 | return; | |
337 | ||
338 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); | |
339 | if (ACPI_FAILURE(status)) | |
70b53411 MW |
340 | return; |
341 | ||
4b01a14b | 342 | list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) { |
6072b9dc MW |
343 | struct gpio_desc *desc; |
344 | ||
8a146fbe HG |
345 | if (irqd_is_wakeup_set(irq_get_irq_data(event->irq))) |
346 | disable_irq_wake(event->irq); | |
347 | ||
6072b9dc | 348 | free_irq(event->irq, event); |
e46cf32c | 349 | desc = event->desc; |
6072b9dc MW |
350 | if (WARN_ON(IS_ERR(desc))) |
351 | continue; | |
e3a2e878 | 352 | gpiochip_unlock_as_irq(chip, event->pin); |
6072b9dc | 353 | gpiochip_free_own_desc(desc); |
4b01a14b MW |
354 | list_del(&event->node); |
355 | kfree(event); | |
70b53411 | 356 | } |
70b53411 | 357 | } |
2b528fff | 358 | EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts); |
70b53411 | 359 | |
f028d524 RW |
360 | int acpi_dev_add_driver_gpios(struct acpi_device *adev, |
361 | const struct acpi_gpio_mapping *gpios) | |
362 | { | |
363 | if (adev && gpios) { | |
364 | adev->driver_gpios = gpios; | |
365 | return 0; | |
366 | } | |
367 | return -EINVAL; | |
368 | } | |
369 | EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios); | |
370 | ||
85c73d50 AS |
371 | static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res) |
372 | { | |
373 | acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev)); | |
374 | } | |
375 | ||
376 | int devm_acpi_dev_add_driver_gpios(struct device *dev, | |
377 | const struct acpi_gpio_mapping *gpios) | |
378 | { | |
379 | void *res; | |
380 | int ret; | |
381 | ||
382 | res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL); | |
383 | if (!res) | |
384 | return -ENOMEM; | |
385 | ||
386 | ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios); | |
387 | if (ret) { | |
388 | devres_free(res); | |
389 | return ret; | |
390 | } | |
391 | devres_add(dev, res); | |
392 | return 0; | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios); | |
395 | ||
396 | void devm_acpi_dev_remove_driver_gpios(struct device *dev) | |
397 | { | |
398 | WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL)); | |
399 | } | |
400 | EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios); | |
401 | ||
f028d524 RW |
402 | static bool acpi_get_driver_gpio_data(struct acpi_device *adev, |
403 | const char *name, int index, | |
404 | struct acpi_reference_args *args) | |
405 | { | |
406 | const struct acpi_gpio_mapping *gm; | |
407 | ||
408 | if (!adev->driver_gpios) | |
409 | return false; | |
410 | ||
411 | for (gm = adev->driver_gpios; gm->name; gm++) | |
412 | if (!strcmp(name, gm->name) && gm->data && index < gm->size) { | |
413 | const struct acpi_gpio_params *par = gm->data + index; | |
414 | ||
415 | args->adev = adev; | |
416 | args->args[0] = par->crs_entry_index; | |
417 | args->args[1] = par->line_index; | |
418 | args->args[2] = par->active_low; | |
419 | args->nargs = 3; | |
420 | return true; | |
421 | } | |
422 | ||
423 | return false; | |
424 | } | |
425 | ||
2eca25af AS |
426 | static enum gpiod_flags |
427 | acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio) | |
428 | { | |
429 | bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP; | |
430 | ||
431 | switch (agpio->io_restriction) { | |
432 | case ACPI_IO_RESTRICT_INPUT: | |
433 | return GPIOD_IN; | |
434 | case ACPI_IO_RESTRICT_OUTPUT: | |
435 | /* | |
436 | * ACPI GPIO resources don't contain an initial value for the | |
437 | * GPIO. Therefore we deduce that value from the pull field | |
438 | * instead. If the pin is pulled up we assume default to be | |
439 | * high, otherwise low. | |
440 | */ | |
441 | return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; | |
442 | default: | |
443 | /* | |
444 | * Assume that the BIOS has configured the direction and pull | |
445 | * accordingly. | |
446 | */ | |
447 | return GPIOD_ASIS; | |
448 | } | |
449 | } | |
450 | ||
12028d2d MW |
451 | struct acpi_gpio_lookup { |
452 | struct acpi_gpio_info info; | |
453 | int index; | |
0d9a693c | 454 | int pin_index; |
d079524a RW |
455 | bool active_low; |
456 | struct acpi_device *adev; | |
936e15dd | 457 | struct gpio_desc *desc; |
12028d2d MW |
458 | int n; |
459 | }; | |
460 | ||
031ba28a | 461 | static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data) |
12028d2d MW |
462 | { |
463 | struct acpi_gpio_lookup *lookup = data; | |
464 | ||
465 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) | |
466 | return 1; | |
467 | ||
936e15dd | 468 | if (lookup->n++ == lookup->index && !lookup->desc) { |
12028d2d | 469 | const struct acpi_resource_gpio *agpio = &ares->data.gpio; |
0d9a693c MW |
470 | int pin_index = lookup->pin_index; |
471 | ||
472 | if (pin_index >= agpio->pin_table_length) | |
473 | return 1; | |
12028d2d | 474 | |
936e15dd | 475 | lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr, |
0d9a693c | 476 | agpio->pin_table[pin_index]); |
12028d2d MW |
477 | lookup->info.gpioint = |
478 | agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; | |
0d9a693c MW |
479 | |
480 | /* | |
e567c35f AS |
481 | * Polarity and triggering are only specified for GpioInt |
482 | * resource. | |
52044723 CR |
483 | * Note: we expect here: |
484 | * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW | |
485 | * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH | |
0d9a693c | 486 | */ |
52044723 CR |
487 | if (lookup->info.gpioint) { |
488 | lookup->info.polarity = agpio->polarity; | |
489 | lookup->info.triggering = agpio->triggering; | |
490 | } | |
491 | ||
12028d2d MW |
492 | } |
493 | ||
494 | return 1; | |
495 | } | |
496 | ||
d079524a RW |
497 | static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup, |
498 | struct acpi_gpio_info *info) | |
499 | { | |
500 | struct list_head res_list; | |
501 | int ret; | |
502 | ||
503 | INIT_LIST_HEAD(&res_list); | |
504 | ||
031ba28a LW |
505 | ret = acpi_dev_get_resources(lookup->adev, &res_list, |
506 | acpi_populate_gpio_lookup, | |
d079524a RW |
507 | lookup); |
508 | if (ret < 0) | |
509 | return ret; | |
510 | ||
511 | acpi_dev_free_resource_list(&res_list); | |
512 | ||
513 | if (!lookup->desc) | |
514 | return -ENOENT; | |
515 | ||
516 | if (info) { | |
517 | *info = lookup->info; | |
518 | if (lookup->active_low) | |
52044723 | 519 | info->polarity = lookup->active_low; |
d079524a RW |
520 | } |
521 | return 0; | |
522 | } | |
523 | ||
504a3374 | 524 | static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, |
d079524a RW |
525 | const char *propname, int index, |
526 | struct acpi_gpio_lookup *lookup) | |
527 | { | |
528 | struct acpi_reference_args args; | |
529 | int ret; | |
530 | ||
531 | memset(&args, 0, sizeof(args)); | |
6f7194a1 MW |
532 | ret = __acpi_node_get_property_reference(fwnode, propname, index, 3, |
533 | &args); | |
504a3374 RW |
534 | if (ret) { |
535 | struct acpi_device *adev = to_acpi_device_node(fwnode); | |
d079524a | 536 | |
504a3374 RW |
537 | if (!adev) |
538 | return ret; | |
539 | ||
540 | if (!acpi_get_driver_gpio_data(adev, propname, index, &args)) | |
541 | return ret; | |
542 | } | |
d079524a RW |
543 | /* |
544 | * The property was found and resolved, so need to lookup the GPIO based | |
545 | * on returned args. | |
546 | */ | |
547 | lookup->adev = args.adev; | |
6f7194a1 MW |
548 | if (args.nargs != 3) |
549 | return -EPROTO; | |
550 | ||
551 | lookup->index = args.args[0]; | |
552 | lookup->pin_index = args.args[1]; | |
553 | lookup->active_low = !!args.args[2]; | |
554 | ||
d079524a RW |
555 | return 0; |
556 | } | |
557 | ||
12028d2d | 558 | /** |
936e15dd | 559 | * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources |
0d9a693c MW |
560 | * @adev: pointer to a ACPI device to get GPIO from |
561 | * @propname: Property name of the GPIO (optional) | |
12028d2d MW |
562 | * @index: index of GpioIo/GpioInt resource (starting from %0) |
563 | * @info: info pointer to fill in (optional) | |
564 | * | |
0d9a693c | 565 | * Function goes through ACPI resources for @adev and based on @index looks |
936e15dd | 566 | * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor, |
12028d2d MW |
567 | * and returns it. @index matches GpioIo/GpioInt resources only so if there |
568 | * are total %3 GPIO resources, the index goes from %0 to %2. | |
569 | * | |
0d9a693c MW |
570 | * If @propname is specified the GPIO is looked using device property. In |
571 | * that case @index is used to select the GPIO entry in the property value | |
572 | * (in case of multiple). | |
573 | * | |
936e15dd | 574 | * If the GPIO cannot be translated or there is an error an ERR_PTR is |
12028d2d MW |
575 | * returned. |
576 | * | |
577 | * Note: if the GPIO resource has multiple entries in the pin list, this | |
578 | * function only returns the first. | |
579 | */ | |
031ba28a | 580 | static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev, |
0d9a693c | 581 | const char *propname, int index, |
936e15dd | 582 | struct acpi_gpio_info *info) |
12028d2d MW |
583 | { |
584 | struct acpi_gpio_lookup lookup; | |
12028d2d MW |
585 | int ret; |
586 | ||
0d9a693c | 587 | if (!adev) |
936e15dd | 588 | return ERR_PTR(-ENODEV); |
12028d2d MW |
589 | |
590 | memset(&lookup, 0, sizeof(lookup)); | |
591 | lookup.index = index; | |
12028d2d | 592 | |
0d9a693c | 593 | if (propname) { |
0d9a693c MW |
594 | dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname); |
595 | ||
504a3374 RW |
596 | ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), |
597 | propname, index, &lookup); | |
d079524a RW |
598 | if (ret) |
599 | return ERR_PTR(ret); | |
0d9a693c | 600 | |
d079524a RW |
601 | dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n", |
602 | dev_name(&lookup.adev->dev), lookup.index, | |
603 | lookup.pin_index, lookup.active_low); | |
0d9a693c MW |
604 | } else { |
605 | dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index); | |
d079524a | 606 | lookup.adev = adev; |
0d9a693c MW |
607 | } |
608 | ||
d079524a RW |
609 | ret = acpi_gpio_resource_lookup(&lookup, info); |
610 | return ret ? ERR_PTR(ret) : lookup.desc; | |
12028d2d | 611 | } |
664e3e5a | 612 | |
031ba28a LW |
613 | struct gpio_desc *acpi_find_gpio(struct device *dev, |
614 | const char *con_id, | |
615 | unsigned int idx, | |
616 | enum gpiod_flags flags, | |
617 | enum gpio_lookup_flags *lookupflags) | |
618 | { | |
619 | struct acpi_device *adev = ACPI_COMPANION(dev); | |
620 | struct acpi_gpio_info info; | |
621 | struct gpio_desc *desc; | |
622 | char propname[32]; | |
623 | int i; | |
624 | ||
625 | /* Try first from _DSD */ | |
626 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | |
9e66504a | 627 | if (con_id) { |
031ba28a LW |
628 | snprintf(propname, sizeof(propname), "%s-%s", |
629 | con_id, gpio_suffixes[i]); | |
630 | } else { | |
631 | snprintf(propname, sizeof(propname), "%s", | |
632 | gpio_suffixes[i]); | |
633 | } | |
634 | ||
635 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); | |
693bdaa1 | 636 | if (!IS_ERR(desc)) |
031ba28a | 637 | break; |
693bdaa1 DT |
638 | if (PTR_ERR(desc) == -EPROBE_DEFER) |
639 | return ERR_CAST(desc); | |
031ba28a LW |
640 | } |
641 | ||
642 | /* Then from plain _CRS GPIOs */ | |
643 | if (IS_ERR(desc)) { | |
644 | if (!acpi_can_fallback_to_crs(adev, con_id)) | |
645 | return ERR_PTR(-ENOENT); | |
646 | ||
647 | desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); | |
648 | if (IS_ERR(desc)) | |
649 | return desc; | |
fe06b56c | 650 | } |
031ba28a | 651 | |
fe06b56c AS |
652 | if (info.gpioint && |
653 | (flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH)) { | |
654 | dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n"); | |
655 | return ERR_PTR(-ENOENT); | |
031ba28a LW |
656 | } |
657 | ||
658 | if (info.polarity == GPIO_ACTIVE_LOW) | |
659 | *lookupflags |= GPIO_ACTIVE_LOW; | |
660 | ||
661 | return desc; | |
662 | } | |
663 | ||
504a3374 RW |
664 | /** |
665 | * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources | |
666 | * @fwnode: pointer to an ACPI firmware node to get the GPIO information from | |
667 | * @propname: Property name of the GPIO | |
668 | * @index: index of GpioIo/GpioInt resource (starting from %0) | |
669 | * @info: info pointer to fill in (optional) | |
670 | * | |
671 | * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it. | |
672 | * Otherwise (ie. it is a data-only non-device object), use the property-based | |
673 | * GPIO lookup to get to the GPIO resource with the relevant information and use | |
674 | * that to obtain the GPIO descriptor to return. | |
675 | */ | |
676 | struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, | |
677 | const char *propname, int index, | |
678 | struct acpi_gpio_info *info) | |
679 | { | |
680 | struct acpi_gpio_lookup lookup; | |
681 | struct acpi_device *adev; | |
682 | int ret; | |
12028d2d | 683 | |
504a3374 RW |
684 | adev = to_acpi_device_node(fwnode); |
685 | if (adev) | |
686 | return acpi_get_gpiod_by_index(adev, propname, index, info); | |
12028d2d | 687 | |
504a3374 RW |
688 | if (!is_acpi_data_node(fwnode)) |
689 | return ERR_PTR(-ENODEV); | |
690 | ||
691 | if (!propname) | |
692 | return ERR_PTR(-EINVAL); | |
693 | ||
694 | memset(&lookup, 0, sizeof(lookup)); | |
695 | lookup.index = index; | |
696 | ||
697 | ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup); | |
698 | if (ret) | |
699 | return ERR_PTR(ret); | |
12028d2d | 700 | |
504a3374 RW |
701 | ret = acpi_gpio_resource_lookup(&lookup, info); |
702 | return ret ? ERR_PTR(ret) : lookup.desc; | |
12028d2d | 703 | } |
664e3e5a | 704 | |
c884fbd4 MW |
705 | /** |
706 | * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number | |
707 | * @adev: pointer to a ACPI device to get IRQ from | |
708 | * @index: index of GpioInt resource (starting from %0) | |
709 | * | |
710 | * If the device has one or more GpioInt resources, this function can be | |
711 | * used to translate from the GPIO offset in the resource to the Linux IRQ | |
712 | * number. | |
713 | * | |
714 | * Return: Linux IRQ number (>%0) on success, negative errno on failure. | |
715 | */ | |
716 | int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index) | |
717 | { | |
718 | int idx, i; | |
52044723 | 719 | unsigned int irq_flags; |
c884fbd4 MW |
720 | |
721 | for (i = 0, idx = 0; idx <= index; i++) { | |
722 | struct acpi_gpio_info info; | |
723 | struct gpio_desc *desc; | |
724 | ||
725 | desc = acpi_get_gpiod_by_index(adev, NULL, i, &info); | |
6798d727 HG |
726 | |
727 | /* Ignore -EPROBE_DEFER, it only matters if idx matches */ | |
728 | if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) | |
729 | return PTR_ERR(desc); | |
730 | ||
52044723 | 731 | if (info.gpioint && idx++ == index) { |
6798d727 | 732 | int irq; |
52044723 | 733 | |
6798d727 HG |
734 | if (IS_ERR(desc)) |
735 | return PTR_ERR(desc); | |
52044723 | 736 | |
6798d727 | 737 | irq = gpiod_to_irq(desc); |
52044723 CR |
738 | if (irq < 0) |
739 | return irq; | |
740 | ||
741 | irq_flags = acpi_dev_get_irq_type(info.triggering, | |
742 | info.polarity); | |
743 | ||
744 | /* Set type if specified and different than the current one */ | |
745 | if (irq_flags != IRQ_TYPE_NONE && | |
746 | irq_flags != irq_get_trigger_type(irq)) | |
747 | irq_set_irq_type(irq, irq_flags); | |
748 | ||
749 | return irq; | |
750 | } | |
751 | ||
c884fbd4 | 752 | } |
6798d727 | 753 | return -ENOENT; |
c884fbd4 MW |
754 | } |
755 | EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get); | |
756 | ||
473ed7be MW |
757 | static acpi_status |
758 | acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address, | |
759 | u32 bits, u64 *value, void *handler_context, | |
760 | void *region_context) | |
761 | { | |
762 | struct acpi_gpio_chip *achip = region_context; | |
763 | struct gpio_chip *chip = achip->chip; | |
764 | struct acpi_resource_gpio *agpio; | |
765 | struct acpi_resource *ares; | |
c15d821d | 766 | int pin_index = (int)address; |
473ed7be | 767 | acpi_status status; |
c15d821d | 768 | int length; |
473ed7be MW |
769 | int i; |
770 | ||
771 | status = acpi_buffer_to_resource(achip->conn_info.connection, | |
772 | achip->conn_info.length, &ares); | |
773 | if (ACPI_FAILURE(status)) | |
774 | return status; | |
775 | ||
776 | if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) { | |
777 | ACPI_FREE(ares); | |
778 | return AE_BAD_PARAMETER; | |
779 | } | |
780 | ||
781 | agpio = &ares->data.gpio; | |
473ed7be MW |
782 | |
783 | if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT && | |
784 | function == ACPI_WRITE)) { | |
785 | ACPI_FREE(ares); | |
786 | return AE_BAD_PARAMETER; | |
787 | } | |
788 | ||
c15d821d SP |
789 | length = min(agpio->pin_table_length, (u16)(pin_index + bits)); |
790 | for (i = pin_index; i < length; ++i) { | |
a4811622 | 791 | int pin = agpio->pin_table[i]; |
473ed7be MW |
792 | struct acpi_gpio_connection *conn; |
793 | struct gpio_desc *desc; | |
794 | bool found; | |
795 | ||
20ec3e39 | 796 | pin = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, pin); |
4de60970 | 797 | if (pin < 0) { |
798 | status = AE_BAD_PARAMETER; | |
799 | goto out; | |
800 | } | |
801 | ||
473ed7be MW |
802 | mutex_lock(&achip->conn_lock); |
803 | ||
804 | found = false; | |
805 | list_for_each_entry(conn, &achip->conns, node) { | |
e46cf32c | 806 | if (conn->pin == pin) { |
473ed7be | 807 | found = true; |
e46cf32c | 808 | desc = conn->desc; |
473ed7be MW |
809 | break; |
810 | } | |
811 | } | |
c103a10f MW |
812 | |
813 | /* | |
814 | * The same GPIO can be shared between operation region and | |
815 | * event but only if the access here is ACPI_READ. In that | |
816 | * case we "borrow" the event GPIO instead. | |
817 | */ | |
818 | if (!found && agpio->sharable == ACPI_SHARED && | |
819 | function == ACPI_READ) { | |
820 | struct acpi_gpio_event *event; | |
821 | ||
822 | list_for_each_entry(event, &achip->events, node) { | |
823 | if (event->pin == pin) { | |
824 | desc = event->desc; | |
825 | found = true; | |
826 | break; | |
827 | } | |
828 | } | |
829 | } | |
830 | ||
473ed7be | 831 | if (!found) { |
2eca25af AS |
832 | enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio); |
833 | const char *label = "ACPI:OpRegion"; | |
834 | int err; | |
835 | ||
836 | desc = gpiochip_request_own_desc(chip, pin, label); | |
e46cf32c | 837 | if (IS_ERR(desc)) { |
473ed7be MW |
838 | status = AE_ERROR; |
839 | mutex_unlock(&achip->conn_lock); | |
840 | goto out; | |
841 | } | |
842 | ||
2eca25af AS |
843 | err = gpiod_configure_flags(desc, label, 0, flags); |
844 | if (err < 0) { | |
845 | status = AE_NOT_CONFIGURED; | |
846 | gpiochip_free_own_desc(desc); | |
847 | mutex_unlock(&achip->conn_lock); | |
848 | goto out; | |
473ed7be MW |
849 | } |
850 | ||
851 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); | |
852 | if (!conn) { | |
853 | status = AE_NO_MEMORY; | |
854 | gpiochip_free_own_desc(desc); | |
855 | mutex_unlock(&achip->conn_lock); | |
856 | goto out; | |
857 | } | |
858 | ||
e46cf32c | 859 | conn->pin = pin; |
473ed7be MW |
860 | conn->desc = desc; |
861 | list_add_tail(&conn->node, &achip->conns); | |
862 | } | |
863 | ||
864 | mutex_unlock(&achip->conn_lock); | |
865 | ||
866 | if (function == ACPI_WRITE) | |
dc62b56a AL |
867 | gpiod_set_raw_value_cansleep(desc, |
868 | !!((1 << i) & *value)); | |
473ed7be | 869 | else |
dc62b56a | 870 | *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i; |
473ed7be MW |
871 | } |
872 | ||
873 | out: | |
874 | ACPI_FREE(ares); | |
875 | return status; | |
876 | } | |
877 | ||
878 | static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip) | |
879 | { | |
880 | struct gpio_chip *chip = achip->chip; | |
58383c78 | 881 | acpi_handle handle = ACPI_HANDLE(chip->parent); |
473ed7be MW |
882 | acpi_status status; |
883 | ||
884 | INIT_LIST_HEAD(&achip->conns); | |
885 | mutex_init(&achip->conn_lock); | |
886 | status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, | |
887 | acpi_gpio_adr_space_handler, | |
888 | NULL, achip); | |
889 | if (ACPI_FAILURE(status)) | |
58383c78 LW |
890 | dev_err(chip->parent, |
891 | "Failed to install GPIO OpRegion handler\n"); | |
473ed7be MW |
892 | } |
893 | ||
894 | static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip) | |
895 | { | |
896 | struct gpio_chip *chip = achip->chip; | |
58383c78 | 897 | acpi_handle handle = ACPI_HANDLE(chip->parent); |
473ed7be MW |
898 | struct acpi_gpio_connection *conn, *tmp; |
899 | acpi_status status; | |
900 | ||
901 | status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, | |
902 | acpi_gpio_adr_space_handler); | |
903 | if (ACPI_FAILURE(status)) { | |
58383c78 LW |
904 | dev_err(chip->parent, |
905 | "Failed to remove GPIO OpRegion handler\n"); | |
473ed7be MW |
906 | return; |
907 | } | |
908 | ||
909 | list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) { | |
910 | gpiochip_free_own_desc(conn->desc); | |
911 | list_del(&conn->node); | |
912 | kfree(conn); | |
913 | } | |
914 | } | |
915 | ||
550a9532 WY |
916 | static struct gpio_desc *acpi_gpiochip_parse_own_gpio( |
917 | struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode, | |
918 | const char **name, unsigned int *lflags, unsigned int *dflags) | |
c80f1ba7 MW |
919 | { |
920 | struct gpio_chip *chip = achip->chip; | |
921 | struct gpio_desc *desc; | |
922 | u32 gpios[2]; | |
923 | int ret; | |
924 | ||
c82064f2 AB |
925 | *lflags = 0; |
926 | *dflags = 0; | |
927 | *name = NULL; | |
928 | ||
c80f1ba7 MW |
929 | ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, |
930 | ARRAY_SIZE(gpios)); | |
931 | if (ret < 0) | |
932 | return ERR_PTR(ret); | |
933 | ||
934 | ret = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, gpios[0]); | |
935 | if (ret < 0) | |
936 | return ERR_PTR(ret); | |
937 | ||
938 | desc = gpiochip_get_desc(chip, ret); | |
939 | if (IS_ERR(desc)) | |
940 | return desc; | |
941 | ||
c80f1ba7 MW |
942 | if (gpios[1]) |
943 | *lflags |= GPIO_ACTIVE_LOW; | |
944 | ||
945 | if (fwnode_property_present(fwnode, "input")) | |
946 | *dflags |= GPIOD_IN; | |
947 | else if (fwnode_property_present(fwnode, "output-low")) | |
948 | *dflags |= GPIOD_OUT_LOW; | |
949 | else if (fwnode_property_present(fwnode, "output-high")) | |
950 | *dflags |= GPIOD_OUT_HIGH; | |
951 | else | |
952 | return ERR_PTR(-EINVAL); | |
953 | ||
954 | fwnode_property_read_string(fwnode, "line-name", name); | |
955 | ||
956 | return desc; | |
957 | } | |
958 | ||
959 | static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip) | |
960 | { | |
961 | struct gpio_chip *chip = achip->chip; | |
962 | struct fwnode_handle *fwnode; | |
963 | ||
964 | device_for_each_child_node(chip->parent, fwnode) { | |
965 | unsigned int lflags, dflags; | |
966 | struct gpio_desc *desc; | |
967 | const char *name; | |
968 | int ret; | |
969 | ||
970 | if (!fwnode_property_present(fwnode, "gpio-hog")) | |
971 | continue; | |
972 | ||
973 | desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name, | |
974 | &lflags, &dflags); | |
975 | if (IS_ERR(desc)) | |
976 | continue; | |
977 | ||
978 | ret = gpiod_hog(desc, name, lflags, dflags); | |
979 | if (ret) { | |
980 | dev_err(chip->parent, "Failed to hog GPIO\n"); | |
1b6998c9 | 981 | fwnode_handle_put(fwnode); |
c80f1ba7 MW |
982 | return; |
983 | } | |
984 | } | |
985 | } | |
986 | ||
664e3e5a MW |
987 | void acpi_gpiochip_add(struct gpio_chip *chip) |
988 | { | |
aa92b6f6 MW |
989 | struct acpi_gpio_chip *acpi_gpio; |
990 | acpi_handle handle; | |
991 | acpi_status status; | |
992 | ||
58383c78 | 993 | if (!chip || !chip->parent) |
e9595f84 MW |
994 | return; |
995 | ||
58383c78 | 996 | handle = ACPI_HANDLE(chip->parent); |
aa92b6f6 MW |
997 | if (!handle) |
998 | return; | |
999 | ||
1000 | acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL); | |
1001 | if (!acpi_gpio) { | |
58383c78 | 1002 | dev_err(chip->parent, |
aa92b6f6 MW |
1003 | "Failed to allocate memory for ACPI GPIO chip\n"); |
1004 | return; | |
1005 | } | |
1006 | ||
1007 | acpi_gpio->chip = chip; | |
c103a10f | 1008 | INIT_LIST_HEAD(&acpi_gpio->events); |
aa92b6f6 MW |
1009 | |
1010 | status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio); | |
1011 | if (ACPI_FAILURE(status)) { | |
58383c78 | 1012 | dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n"); |
aa92b6f6 MW |
1013 | kfree(acpi_gpio); |
1014 | return; | |
1015 | } | |
1016 | ||
4035cc15 MW |
1017 | if (!chip->names) |
1018 | devprop_gpiochip_set_names(chip); | |
1019 | ||
473ed7be | 1020 | acpi_gpiochip_request_regions(acpi_gpio); |
c80f1ba7 | 1021 | acpi_gpiochip_scan_gpios(acpi_gpio); |
3f86a635 | 1022 | acpi_walk_dep_device_list(handle); |
664e3e5a MW |
1023 | } |
1024 | ||
1025 | void acpi_gpiochip_remove(struct gpio_chip *chip) | |
1026 | { | |
aa92b6f6 MW |
1027 | struct acpi_gpio_chip *acpi_gpio; |
1028 | acpi_handle handle; | |
1029 | acpi_status status; | |
1030 | ||
58383c78 | 1031 | if (!chip || !chip->parent) |
e9595f84 MW |
1032 | return; |
1033 | ||
58383c78 | 1034 | handle = ACPI_HANDLE(chip->parent); |
aa92b6f6 MW |
1035 | if (!handle) |
1036 | return; | |
1037 | ||
1038 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); | |
1039 | if (ACPI_FAILURE(status)) { | |
58383c78 | 1040 | dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n"); |
aa92b6f6 MW |
1041 | return; |
1042 | } | |
1043 | ||
473ed7be | 1044 | acpi_gpiochip_free_regions(acpi_gpio); |
aa92b6f6 MW |
1045 | |
1046 | acpi_detach_data(handle, acpi_gpio_chip_dh); | |
1047 | kfree(acpi_gpio); | |
664e3e5a | 1048 | } |
66858527 | 1049 | |
6f7194a1 | 1050 | static int acpi_gpio_package_count(const union acpi_object *obj) |
66858527 RI |
1051 | { |
1052 | const union acpi_object *element = obj->package.elements; | |
1053 | const union acpi_object *end = element + obj->package.count; | |
1054 | unsigned int count = 0; | |
1055 | ||
1056 | while (element < end) { | |
6f7194a1 MW |
1057 | switch (element->type) { |
1058 | case ACPI_TYPE_LOCAL_REFERENCE: | |
1059 | element += 3; | |
1060 | /* Fallthrough */ | |
1061 | case ACPI_TYPE_INTEGER: | |
1062 | element++; | |
66858527 | 1063 | count++; |
6f7194a1 | 1064 | break; |
66858527 | 1065 | |
6f7194a1 MW |
1066 | default: |
1067 | return -EPROTO; | |
1068 | } | |
66858527 | 1069 | } |
6f7194a1 | 1070 | |
66858527 RI |
1071 | return count; |
1072 | } | |
1073 | ||
1074 | static int acpi_find_gpio_count(struct acpi_resource *ares, void *data) | |
1075 | { | |
1076 | unsigned int *count = data; | |
1077 | ||
1078 | if (ares->type == ACPI_RESOURCE_TYPE_GPIO) | |
1079 | *count += ares->data.gpio.pin_table_length; | |
1080 | ||
1081 | return 1; | |
1082 | } | |
1083 | ||
1084 | /** | |
1085 | * acpi_gpio_count - return the number of GPIOs associated with a | |
1086 | * device / function or -ENOENT if no GPIO has been | |
1087 | * assigned to the requested function. | |
1088 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
1089 | * @con_id: function within the GPIO consumer | |
1090 | */ | |
1091 | int acpi_gpio_count(struct device *dev, const char *con_id) | |
1092 | { | |
1093 | struct acpi_device *adev = ACPI_COMPANION(dev); | |
1094 | const union acpi_object *obj; | |
1095 | const struct acpi_gpio_mapping *gm; | |
1096 | int count = -ENOENT; | |
1097 | int ret; | |
1098 | char propname[32]; | |
1099 | unsigned int i; | |
1100 | ||
1101 | /* Try first from _DSD */ | |
1102 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | |
9e66504a | 1103 | if (con_id) |
66858527 RI |
1104 | snprintf(propname, sizeof(propname), "%s-%s", |
1105 | con_id, gpio_suffixes[i]); | |
1106 | else | |
1107 | snprintf(propname, sizeof(propname), "%s", | |
1108 | gpio_suffixes[i]); | |
1109 | ||
1110 | ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY, | |
1111 | &obj); | |
1112 | if (ret == 0) { | |
1113 | if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) | |
1114 | count = 1; | |
1115 | else if (obj->type == ACPI_TYPE_PACKAGE) | |
1116 | count = acpi_gpio_package_count(obj); | |
1117 | } else if (adev->driver_gpios) { | |
1118 | for (gm = adev->driver_gpios; gm->name; gm++) | |
1119 | if (strcmp(propname, gm->name) == 0) { | |
1120 | count = gm->size; | |
1121 | break; | |
1122 | } | |
1123 | } | |
4ed55016 | 1124 | if (count > 0) |
66858527 RI |
1125 | break; |
1126 | } | |
1127 | ||
1128 | /* Then from plain _CRS GPIOs */ | |
1129 | if (count < 0) { | |
1130 | struct list_head resource_list; | |
1131 | unsigned int crs_count = 0; | |
1132 | ||
6fe9da42 AS |
1133 | if (!acpi_can_fallback_to_crs(adev, con_id)) |
1134 | return count; | |
1135 | ||
66858527 RI |
1136 | INIT_LIST_HEAD(&resource_list); |
1137 | acpi_dev_get_resources(adev, &resource_list, | |
1138 | acpi_find_gpio_count, &crs_count); | |
1139 | acpi_dev_free_resource_list(&resource_list); | |
1140 | if (crs_count > 0) | |
1141 | count = crs_count; | |
1142 | } | |
4ed55016 | 1143 | return count ? count : -ENOENT; |
66858527 | 1144 | } |
10cf4899 | 1145 | |
10cf4899 DT |
1146 | bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id) |
1147 | { | |
10cf4899 DT |
1148 | /* Never allow fallback if the device has properties */ |
1149 | if (adev->data.properties || adev->driver_gpios) | |
1150 | return false; | |
1151 | ||
f10e4bf6 | 1152 | return con_id == NULL; |
10cf4899 | 1153 | } |