1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ACPI helpers for GPIO API
4 *
5 * Copyright (C) 2012, Intel Corporation
6 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 */
9
10 #include <linux/acpi.h>
11 #include <linux/dmi.h>
12 #include <linux/errno.h>
13 #include <linux/export.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/mutex.h>
17 #include <linux/pinctrl/pinctrl.h>
18
19 #include <linux/gpio/consumer.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/gpio/machine.h>
22
23 #include "gpiolib.h"
24 #include "gpiolib-acpi.h"
25
26 /**
27 * struct acpi_gpio_event - ACPI GPIO event handler data
28 *
29 * @node: list-entry of the events list of the struct acpi_gpio_chip
30 * @handle: handle of ACPI method to execute when the IRQ triggers
31 * @handler: handler function to pass to request_irq() when requesting the IRQ
32 * @pin: GPIO pin number on the struct gpio_chip
33 * @irq: Linux IRQ number for the event, for request_irq() / free_irq()
34 * @irqflags: flags to pass to request_irq() when requesting the IRQ
35 * @irq_is_wake: If the ACPI flags indicate the IRQ is a wakeup source
36 * @irq_requested:True if request_irq() has been done
37 * @desc: struct gpio_desc for the GPIO pin for this event
38 */
39 struct acpi_gpio_event {
40 struct list_head node;
41 acpi_handle handle;
42 irq_handler_t handler;
43 unsigned int pin;
44 unsigned int irq;
45 unsigned long irqflags;
46 bool irq_is_wake;
47 bool irq_requested;
48 struct gpio_desc *desc;
49 };
50
51 struct acpi_gpio_connection {
52 struct list_head node;
53 unsigned int pin;
54 struct gpio_desc *desc;
55 };
56
57 struct acpi_gpio_chip {
58 /*
59 * ACPICA requires that the first field of the context parameter
60 * passed to acpi_install_address_space_handler() is large enough
61 * to hold struct acpi_connection_info.
62 */
63 struct acpi_connection_info conn_info;
64 struct list_head conns;
65 struct mutex conn_lock;
66 struct gpio_chip *chip;
67 struct list_head events;
68 struct list_head deferred_req_irqs_list_entry;
69 };
70
71 /**
72 * struct acpi_gpio_info - ACPI GPIO specific information
73 * @adev: reference to ACPI device which consumes GPIO resource
74 * @flags: GPIO initialization flags
75 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
76 * @wake_capable: wake capability as provided by ACPI
77 * @pin_config: pin bias as provided by ACPI
78 * @polarity: interrupt polarity as provided by ACPI
79 * @triggering: triggering type as provided by ACPI
80 * @debounce: debounce timeout as provided by ACPI
81 * @quirks: Linux specific quirks as provided by struct acpi_gpio_mapping
82 */
83 struct acpi_gpio_info {
84 struct acpi_device *adev;
85 enum gpiod_flags flags;
86 bool gpioint;
87 bool wake_capable;
88 int pin_config;
89 int polarity;
90 int triggering;
91 unsigned int debounce;
92 unsigned int quirks;
93 };
94
acpi_gpiochip_find(struct gpio_chip * gc,const void * data)95 static int acpi_gpiochip_find(struct gpio_chip *gc, const void *data)
96 {
97 /* First check the actual GPIO device */
98 if (device_match_acpi_handle(&gc->gpiodev->dev, data))
99 return true;
100
101 /*
102 * When the ACPI device is artificially split to the banks of GPIOs,
103 * where each of them is represented by a separate GPIO device,
104 * the firmware node of the physical device may not be shared among
105 * the banks as they may require different values for the same property,
106 * e.g., number of GPIOs in a certain bank. In such case the ACPI handle
107 * of a GPIO device is NULL and can not be used. Hence we have to check
108 * the parent device to be sure that there is no match before bailing
109 * out.
110 */
111 if (gc->parent)
112 return device_match_acpi_handle(gc->parent, data);
113
114 return false;
115 }
116
117 /**
118 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
119 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
120 * @pin: ACPI GPIO pin number (0-based, controller-relative)
121 *
122 * Returns:
123 * GPIO descriptor to use with Linux generic GPIO API.
124 * If the GPIO cannot be translated or there is an error an ERR_PTR is
125 * returned.
126 *
127 * Specifically returns %-EPROBE_DEFER if the referenced GPIO
128 * controller does not have GPIO chip registered at the moment. This is to
129 * support probe deferral.
130 */
acpi_get_gpiod(char * path,unsigned int pin)131 static struct gpio_desc *acpi_get_gpiod(char *path, unsigned int pin)
132 {
133 acpi_handle handle;
134 acpi_status status;
135
136 status = acpi_get_handle(NULL, path, &handle);
137 if (ACPI_FAILURE(status))
138 return ERR_PTR(-ENODEV);
139
140 struct gpio_device *gdev __free(gpio_device_put) =
141 gpio_device_find(handle, acpi_gpiochip_find);
142 if (!gdev)
143 return ERR_PTR(-EPROBE_DEFER);
144
145 return gpio_device_get_desc(gdev, pin);
146 }
147
acpi_gpio_irq_handler(int irq,void * data)148 static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
149 {
150 struct acpi_gpio_event *event = data;
151
152 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
153
154 return IRQ_HANDLED;
155 }
156
acpi_gpio_irq_handler_evt(int irq,void * data)157 static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
158 {
159 struct acpi_gpio_event *event = data;
160
161 acpi_execute_simple_method(event->handle, NULL, event->pin);
162
163 return IRQ_HANDLED;
164 }
165
acpi_gpio_chip_dh(acpi_handle handle,void * data)166 static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
167 {
168 /* The address of this function is used as a key. */
169 }
170
acpi_gpio_get_irq_resource(struct acpi_resource * ares,struct acpi_resource_gpio ** agpio)171 bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
172 struct acpi_resource_gpio **agpio)
173 {
174 struct acpi_resource_gpio *gpio;
175
176 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
177 return false;
178
179 gpio = &ares->data.gpio;
180 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
181 return false;
182
183 *agpio = gpio;
184 return true;
185 }
186 EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
187
188 /**
189 * acpi_gpio_get_io_resource - Fetch details of an ACPI resource if it is a GPIO
190 * I/O resource or return False if not.
191 * @ares: Pointer to the ACPI resource to fetch
192 * @agpio: Pointer to a &struct acpi_resource_gpio to store the output pointer
193 *
194 * Returns:
195 * %true if GpioIo resource is found, %false otherwise.
196 */
acpi_gpio_get_io_resource(struct acpi_resource * ares,struct acpi_resource_gpio ** agpio)197 bool acpi_gpio_get_io_resource(struct acpi_resource *ares,
198 struct acpi_resource_gpio **agpio)
199 {
200 struct acpi_resource_gpio *gpio;
201
202 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
203 return false;
204
205 gpio = &ares->data.gpio;
206 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_IO)
207 return false;
208
209 *agpio = gpio;
210 return true;
211 }
212 EXPORT_SYMBOL_GPL(acpi_gpio_get_io_resource);
213
acpi_gpiochip_request_irq(struct acpi_gpio_chip * acpi_gpio,struct acpi_gpio_event * event)214 static void acpi_gpiochip_request_irq(struct acpi_gpio_chip *acpi_gpio,
215 struct acpi_gpio_event *event)
216 {
217 struct device *parent = acpi_gpio->chip->parent;
218 int ret, value;
219
220 ret = request_threaded_irq(event->irq, NULL, event->handler,
221 event->irqflags | IRQF_ONESHOT, "ACPI:Event", event);
222 if (ret) {
223 dev_err(parent, "Failed to setup interrupt handler for %d\n", event->irq);
224 return;
225 }
226
227 if (event->irq_is_wake)
228 enable_irq_wake(event->irq);
229
230 event->irq_requested = true;
231
232 /*
233 * Make sure we trigger the initial state of ActiveBoth IRQs.
234 *
235 * According to the Microsoft GPIO documentation, triggering GPIO
236 * interrupts marked as ActiveBoth during initialization is correct
237 * as long as the associated GPIO line is already "asserted"
238 * (logic level low). We should not trigger edge-based GPIO
239 * interrupts not marked as ActiveBoth.
240 *
241 * See: https://learn.microsoft.com/en-us/windows-hardware/drivers/bringup/general-purpose-i-o--gpio-
242 * Section: "GPIO controllers and ActiveBoth interrupts"
243 */
244 if (acpi_gpio_need_run_edge_events_on_boot() &&
245 ((event->irqflags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) ==
246 (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))) {
247 value = gpiod_get_raw_value_cansleep(event->desc);
248 if (value == 0)
249 event->handler(event->irq, event);
250 }
251 }
252
acpi_gpiochip_request_irqs(struct acpi_gpio_chip * acpi_gpio)253 static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio)
254 {
255 struct acpi_gpio_event *event;
256
257 list_for_each_entry(event, &acpi_gpio->events, node)
258 acpi_gpiochip_request_irq(acpi_gpio, event);
259 }
260
261 static enum gpiod_flags
acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio * agpio,int polarity)262 acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio, int polarity)
263 {
264 /* GpioInt() implies input configuration */
265 if (agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
266 return GPIOD_IN;
267
268 switch (agpio->io_restriction) {
269 case ACPI_IO_RESTRICT_INPUT:
270 return GPIOD_IN;
271 case ACPI_IO_RESTRICT_OUTPUT:
272 /*
273 * ACPI GPIO resources don't contain an initial value for the
274 * GPIO. Therefore we deduce that value from the pull field
275 * and the polarity instead. If the pin is pulled up we assume
276 * default to be high, if it is pulled down we assume default
277 * to be low, otherwise we leave pin untouched. For active low
278 * polarity values will be switched. See also
279 * Documentation/firmware-guide/acpi/gpio-properties.rst.
280 */
281 switch (agpio->pin_config) {
282 case ACPI_PIN_CONFIG_PULLUP:
283 return polarity == GPIO_ACTIVE_LOW ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH;
284 case ACPI_PIN_CONFIG_PULLDOWN:
285 return polarity == GPIO_ACTIVE_LOW ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
286 default:
287 break;
288 }
289 break;
290 default:
291 break;
292 }
293
294 /*
295 * Assume that the BIOS has configured the direction and pull
296 * accordingly.
297 */
298 return GPIOD_ASIS;
299 }
300
acpi_gpio_set_debounce_timeout(struct gpio_desc * desc,unsigned int acpi_debounce)301 static void acpi_gpio_set_debounce_timeout(struct gpio_desc *desc,
302 unsigned int acpi_debounce)
303 {
304 int ret;
305
306 /* ACPI uses hundredths of milliseconds units */
307 acpi_debounce *= 10;
308 ret = gpio_set_debounce_timeout(desc, acpi_debounce);
309 if (ret)
310 gpiod_warn(desc, "Failed to set debounce-timeout %u: %d\n",
311 acpi_debounce, ret);
312 }
313
acpi_request_own_gpiod(struct gpio_chip * chip,struct acpi_resource_gpio * agpio,unsigned int index,const char * label)314 static struct gpio_desc *acpi_request_own_gpiod(struct gpio_chip *chip,
315 struct acpi_resource_gpio *agpio,
316 unsigned int index,
317 const char *label)
318 {
319 enum gpiod_flags flags;
320 struct gpio_desc *desc;
321 unsigned int pin;
322 int polarity;
323
324 if (index >= agpio->pin_table_length)
325 return ERR_PTR(-EINVAL);
326
327 pin = agpio->pin_table[index];
328 polarity = GPIO_ACTIVE_HIGH;
329 flags = acpi_gpio_to_gpiod_flags(agpio, polarity);
330
331 desc = gpiochip_request_own_desc(chip, pin, label, polarity, flags);
332 if (IS_ERR(desc))
333 return desc;
334
335 acpi_gpio_set_debounce_timeout(desc, agpio->debounce_timeout);
336
337 return desc;
338 }
339
acpi_gpio_irq_is_wake(struct device * parent,const struct acpi_resource_gpio * agpio)340 static bool acpi_gpio_irq_is_wake(struct device *parent,
341 const struct acpi_resource_gpio *agpio)
342 {
343 unsigned int pin;
344
345 if (agpio->pin_table_length == 0)
346 return false;
347
348 pin = agpio->pin_table[0];
349
350 if (agpio->wake_capable != ACPI_WAKE_CAPABLE)
351 return false;
352
353 if (acpi_gpio_in_ignore_list(ACPI_GPIO_IGNORE_WAKE, dev_name(parent), pin)) {
354 dev_info(parent, "Ignoring wakeup on pin %u\n", pin);
355 return false;
356 }
357
358 return true;
359 }
360
361 /* Always returns AE_OK so that we keep looping over the resources */
acpi_gpiochip_alloc_event(struct acpi_resource * ares,void * context)362 static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
363 void *context)
364 {
365 struct acpi_gpio_chip *acpi_gpio = context;
366 struct gpio_chip *chip = acpi_gpio->chip;
367 struct acpi_resource_gpio *agpio;
368 acpi_handle handle, evt_handle;
369 struct acpi_gpio_event *event;
370 irq_handler_t handler = NULL;
371 struct gpio_desc *desc;
372 unsigned int pin;
373 int ret, irq;
374
375 if (!acpi_gpio_get_irq_resource(ares, &agpio))
376 return AE_OK;
377
378 if (agpio->pin_table_length == 0)
379 return AE_OK;
380
381 handle = ACPI_HANDLE(chip->parent);
382 pin = agpio->pin_table[0];
383
384 if (pin <= 255) {
385 char ev_name[8];
386 sprintf(ev_name, "_%c%02X",
387 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
388 pin);
389 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
390 handler = acpi_gpio_irq_handler;
391 }
392 if (!handler) {
393 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
394 handler = acpi_gpio_irq_handler_evt;
395 }
396 if (!handler)
397 return AE_OK;
398
399 if (acpi_gpio_in_ignore_list(ACPI_GPIO_IGNORE_INTERRUPT, dev_name(chip->parent), pin)) {
400 dev_info(chip->parent, "Ignoring interrupt on pin %u\n", pin);
401 return AE_OK;
402 }
403
404 desc = acpi_request_own_gpiod(chip, agpio, 0, "ACPI:Event");
405 if (IS_ERR(desc)) {
406 dev_err(chip->parent,
407 "Failed to request GPIO for pin 0x%04X, err %pe\n",
408 pin, desc);
409 return AE_OK;
410 }
411
412 ret = gpiochip_lock_as_irq(chip, pin);
413 if (ret) {
414 dev_err(chip->parent,
415 "Failed to lock GPIO pin 0x%04X as interrupt, err %d\n",
416 pin, ret);
417 goto fail_free_desc;
418 }
419
420 irq = gpiod_to_irq(desc);
421 if (irq < 0) {
422 dev_err(chip->parent,
423 "Failed to translate GPIO pin 0x%04X to IRQ, err %d\n",
424 pin, irq);
425 goto fail_unlock_irq;
426 }
427
428 event = kzalloc_obj(*event);
429 if (!event)
430 goto fail_unlock_irq;
431
432 event->irqflags = IRQF_ONESHOT;
433 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
434 if (agpio->polarity == ACPI_ACTIVE_HIGH)
435 event->irqflags |= IRQF_TRIGGER_HIGH;
436 else
437 event->irqflags |= IRQF_TRIGGER_LOW;
438 } else {
439 switch (agpio->polarity) {
440 case ACPI_ACTIVE_HIGH:
441 event->irqflags |= IRQF_TRIGGER_RISING;
442 break;
443 case ACPI_ACTIVE_LOW:
444 event->irqflags |= IRQF_TRIGGER_FALLING;
445 break;
446 default:
447 event->irqflags |= IRQF_TRIGGER_RISING |
448 IRQF_TRIGGER_FALLING;
449 break;
450 }
451 }
452
453 event->handle = evt_handle;
454 event->handler = handler;
455 event->irq = irq;
456 event->irq_is_wake = acpi_gpio_irq_is_wake(chip->parent, agpio);
457 event->pin = pin;
458 event->desc = desc;
459
460 list_add_tail(&event->node, &acpi_gpio->events);
461
462 return AE_OK;
463
464 fail_unlock_irq:
465 gpiochip_unlock_as_irq(chip, pin);
466 fail_free_desc:
467 gpiochip_free_own_desc(desc);
468
469 return AE_OK;
470 }
471
472 /**
473 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
474 * @chip: GPIO chip
475 *
476 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
477 * handled by ACPI event methods which need to be called from the GPIO
478 * chip's interrupt handler. acpi_gpiochip_request_interrupts() finds out which
479 * GPIO pins have ACPI event methods and assigns interrupt handlers that calls
480 * the ACPI event methods for those pins.
481 */
acpi_gpiochip_request_interrupts(struct gpio_chip * chip)482 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
483 {
484 struct acpi_gpio_chip *acpi_gpio;
485 acpi_handle handle;
486 acpi_status status;
487
488 if (!chip->parent || !chip->to_irq)
489 return;
490
491 handle = ACPI_HANDLE(chip->parent);
492 if (!handle)
493 return;
494
495 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
496 if (ACPI_FAILURE(status))
497 return;
498
499 if (acpi_quirk_skip_gpio_event_handlers())
500 return;
501
502 acpi_walk_resources(handle, METHOD_NAME__AEI,
503 acpi_gpiochip_alloc_event, acpi_gpio);
504
505 if (acpi_gpio_add_to_deferred_list(&acpi_gpio->deferred_req_irqs_list_entry))
506 return;
507
508 acpi_gpiochip_request_irqs(acpi_gpio);
509 }
510 EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
511
512 /**
513 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
514 * @chip: GPIO chip
515 *
516 * Free interrupts associated with GPIO ACPI event method for the given
517 * GPIO chip.
518 */
acpi_gpiochip_free_interrupts(struct gpio_chip * chip)519 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
520 {
521 struct acpi_gpio_chip *acpi_gpio;
522 struct acpi_gpio_event *event, *ep;
523 acpi_handle handle;
524 acpi_status status;
525
526 if (!chip->parent || !chip->to_irq)
527 return;
528
529 handle = ACPI_HANDLE(chip->parent);
530 if (!handle)
531 return;
532
533 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
534 if (ACPI_FAILURE(status))
535 return;
536
537 acpi_gpio_remove_from_deferred_list(&acpi_gpio->deferred_req_irqs_list_entry);
538
539 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
540 if (event->irq_requested) {
541 if (event->irq_is_wake)
542 disable_irq_wake(event->irq);
543
544 free_irq(event->irq, event);
545 }
546
547 gpiochip_unlock_as_irq(chip, event->pin);
548 gpiochip_free_own_desc(event->desc);
549 list_del(&event->node);
550 kfree(event);
551 }
552 }
553 EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
554
acpi_gpio_process_deferred_list(struct list_head * list)555 void __init acpi_gpio_process_deferred_list(struct list_head *list)
556 {
557 struct acpi_gpio_chip *acpi_gpio, *tmp;
558
559 list_for_each_entry_safe(acpi_gpio, tmp, list, deferred_req_irqs_list_entry)
560 acpi_gpiochip_request_irqs(acpi_gpio);
561 }
562
acpi_dev_add_driver_gpios(struct acpi_device * adev,const struct acpi_gpio_mapping * gpios)563 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
564 const struct acpi_gpio_mapping *gpios)
565 {
566 if (adev && gpios) {
567 adev->driver_gpios = gpios;
568 return 0;
569 }
570 return -EINVAL;
571 }
572 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
573
acpi_dev_remove_driver_gpios(struct acpi_device * adev)574 void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
575 {
576 if (adev)
577 adev->driver_gpios = NULL;
578 }
579 EXPORT_SYMBOL_GPL(acpi_dev_remove_driver_gpios);
580
acpi_dev_release_driver_gpios(void * adev)581 static void acpi_dev_release_driver_gpios(void *adev)
582 {
583 acpi_dev_remove_driver_gpios(adev);
584 }
585
devm_acpi_dev_add_driver_gpios(struct device * dev,const struct acpi_gpio_mapping * gpios)586 int devm_acpi_dev_add_driver_gpios(struct device *dev,
587 const struct acpi_gpio_mapping *gpios)
588 {
589 struct acpi_device *adev = ACPI_COMPANION(dev);
590 int ret;
591
592 ret = acpi_dev_add_driver_gpios(adev, gpios);
593 if (ret)
594 return ret;
595
596 return devm_add_action_or_reset(dev, acpi_dev_release_driver_gpios, adev);
597 }
598 EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
599
acpi_get_driver_gpio_data(struct acpi_device * adev,const char * name,int index,struct fwnode_reference_args * args,unsigned int * quirks)600 static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
601 const char *name, int index,
602 struct fwnode_reference_args *args,
603 unsigned int *quirks)
604 {
605 const struct acpi_gpio_mapping *gm;
606
607 if (!adev || !adev->driver_gpios)
608 return false;
609
610 for (gm = adev->driver_gpios; gm->name; gm++)
611 if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
612 const struct acpi_gpio_params *params = gm->data + index;
613
614 args->fwnode = acpi_fwnode_handle(adev);
615 args->args[0] = params->crs_entry_index;
616 args->args[1] = params->line_index;
617 args->args[2] = params->active_low;
618 args->nargs = 3;
619
620 *quirks = gm->quirks;
621 return true;
622 }
623
624 return false;
625 }
626
627 static int
__acpi_gpio_update_gpiod_flags(enum gpiod_flags * flags,enum gpiod_flags update)628 __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
629 {
630 const enum gpiod_flags mask =
631 GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
632 GPIOD_FLAGS_BIT_DIR_VAL;
633 int ret = 0;
634
635 /*
636 * Check if the BIOS has IoRestriction with explicitly set direction
637 * and update @flags accordingly. Otherwise use whatever caller asked
638 * for.
639 */
640 if (update & GPIOD_FLAGS_BIT_DIR_SET) {
641 enum gpiod_flags diff = *flags ^ update;
642
643 /*
644 * Check if caller supplied incompatible GPIO initialization
645 * flags.
646 *
647 * Return %-EINVAL to notify that firmware has different
648 * settings and we are going to use them.
649 */
650 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
651 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
652 ret = -EINVAL;
653 *flags = (*flags & ~mask) | (update & mask);
654 }
655 return ret;
656 }
657
acpi_gpio_update_gpiod_flags(enum gpiod_flags * flags,struct acpi_gpio_info * info)658 static int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
659 struct acpi_gpio_info *info)
660 {
661 struct device *dev = &info->adev->dev;
662 enum gpiod_flags old = *flags;
663 int ret;
664
665 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
666 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
667 if (ret)
668 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
669 } else {
670 if (ret)
671 dev_dbg(dev, "Override GPIO initialization flags\n");
672 *flags = old;
673 }
674
675 return ret;
676 }
677
acpi_gpio_update_gpiod_lookup_flags(unsigned long * lookupflags,struct acpi_gpio_info * info)678 static int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
679 struct acpi_gpio_info *info)
680 {
681 switch (info->pin_config) {
682 case ACPI_PIN_CONFIG_PULLUP:
683 *lookupflags |= GPIO_PULL_UP;
684 break;
685 case ACPI_PIN_CONFIG_PULLDOWN:
686 *lookupflags |= GPIO_PULL_DOWN;
687 break;
688 case ACPI_PIN_CONFIG_NOPULL:
689 *lookupflags |= GPIO_PULL_DISABLE;
690 break;
691 default:
692 break;
693 }
694
695 if (info->polarity == GPIO_ACTIVE_LOW)
696 *lookupflags |= GPIO_ACTIVE_LOW;
697
698 return 0;
699 }
700
701 struct acpi_gpio_lookup {
702 struct acpi_gpio_params params;
703 struct acpi_gpio_info *info;
704 struct gpio_desc *desc;
705 int n;
706 };
707
acpi_populate_gpio_lookup(struct acpi_resource * ares,void * data)708 static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
709 {
710 struct acpi_gpio_lookup *lookup = data;
711 struct acpi_gpio_params *params = &lookup->params;
712 struct acpi_gpio_info *info = lookup->info;
713
714 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
715 return 1;
716
717 if (!lookup->desc) {
718 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
719 bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
720 struct gpio_desc *desc;
721 u16 pin_index;
722
723 if (info->quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
724 params->crs_entry_index++;
725
726 if (lookup->n++ != params->crs_entry_index)
727 return 1;
728
729 pin_index = params->line_index;
730 if (pin_index >= agpio->pin_table_length)
731 return 1;
732
733 if (info->quirks & ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER)
734 desc = gpio_to_desc(agpio->pin_table[pin_index]);
735 else
736 desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
737 agpio->pin_table[pin_index]);
738 lookup->desc = desc;
739 info->pin_config = agpio->pin_config;
740 info->debounce = agpio->debounce_timeout;
741 info->gpioint = gpioint;
742 info->wake_capable = acpi_gpio_irq_is_wake(&info->adev->dev, agpio);
743
744 /*
745 * Polarity and triggering are only specified for GpioInt
746 * resource.
747 * Note: we expect here:
748 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
749 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
750 */
751 if (info->gpioint) {
752 info->polarity = agpio->polarity;
753 info->triggering = agpio->triggering;
754 } else {
755 info->polarity = params->active_low;
756 }
757
758 info->flags = acpi_gpio_to_gpiod_flags(agpio, info->polarity);
759 }
760
761 return 1;
762 }
763
acpi_gpio_resource_lookup(struct acpi_gpio_lookup * lookup)764 static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup)
765 {
766 struct acpi_gpio_info *info = lookup->info;
767 struct acpi_device *adev = info->adev;
768 struct list_head res_list;
769 int ret;
770
771 INIT_LIST_HEAD(&res_list);
772
773 ret = acpi_dev_get_resources(adev, &res_list,
774 acpi_populate_gpio_lookup,
775 lookup);
776 if (ret < 0)
777 return ret;
778
779 acpi_dev_free_resource_list(&res_list);
780
781 if (!lookup->desc)
782 return -ENOENT;
783
784 return 0;
785 }
786
acpi_gpio_property_lookup(struct fwnode_handle * fwnode,const char * propname,struct acpi_gpio_lookup * lookup)787 static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, const char *propname,
788 struct acpi_gpio_lookup *lookup)
789 {
790 struct fwnode_reference_args args;
791 struct acpi_gpio_params *params = &lookup->params;
792 struct acpi_gpio_info *info = lookup->info;
793 unsigned int index = params->crs_entry_index;
794 unsigned int quirks = 0;
795 int ret;
796
797 memset(&args, 0, sizeof(args));
798
799 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3, &args);
800 if (ret) {
801 struct acpi_device *adev;
802
803 adev = to_acpi_device_node(fwnode);
804 if (!acpi_get_driver_gpio_data(adev, propname, index, &args, &quirks))
805 return ret;
806 }
807 /*
808 * The property was found and resolved, so need to lookup the GPIO based
809 * on returned args.
810 */
811 if (!to_acpi_device_node(args.fwnode))
812 return -EINVAL;
813 if (args.nargs != 3)
814 return -EPROTO;
815
816 params->crs_entry_index = args.args[0];
817 params->line_index = args.args[1];
818 params->active_low = !!args.args[2];
819
820 info->adev = to_acpi_device_node(args.fwnode);
821 info->quirks = quirks;
822
823 return 0;
824 }
825
826 /**
827 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
828 * @adev: pointer to a ACPI device to get GPIO from
829 * @propname: Property name of the GPIO (optional)
830 * @lookup: pointer to struct acpi_gpio_lookup to fill in
831 *
832 * Function goes through ACPI resources for @adev and based on @lookup.index looks
833 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
834 * and returns it. @lookup.index matches GpioIo/GpioInt resources only so if there
835 * are total 3 GPIO resources, the index goes from 0 to 2.
836 *
837 * If @propname is specified the GPIO is looked using device property. In
838 * that case @index is used to select the GPIO entry in the property value
839 * (in case of multiple).
840 *
841 * Returns:
842 * 0 on success, negative errno on failure.
843 *
844 * The @lookup is filled with GPIO descriptor to use with Linux generic GPIO API.
845 * If the GPIO cannot be translated an error will be returned.
846 *
847 * Note: if the GPIO resource has multiple entries in the pin list, this
848 * function only returns the first.
849 */
acpi_get_gpiod_by_index(struct acpi_device * adev,const char * propname,struct acpi_gpio_lookup * lookup)850 static int acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
851 struct acpi_gpio_lookup *lookup)
852 {
853 struct acpi_gpio_params *params = &lookup->params;
854 struct acpi_gpio_info *info = lookup->info;
855 int ret;
856
857 if (propname) {
858 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
859
860 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), propname, lookup);
861 if (ret)
862 return ret;
863
864 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %u %u %u\n",
865 dev_name(&info->adev->dev),
866 params->crs_entry_index, params->line_index, params->active_low);
867 } else {
868 dev_dbg(&adev->dev, "GPIO: looking up %u in _CRS\n", params->crs_entry_index);
869 info->adev = adev;
870 }
871
872 return acpi_gpio_resource_lookup(lookup);
873 }
874
875 /**
876 * acpi_get_gpiod_from_data() - get a GPIO descriptor from ACPI data node
877 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
878 * @propname: Property name of the GPIO
879 * @lookup: pointer to struct acpi_gpio_lookup to fill in
880 *
881 * This function uses the property-based GPIO lookup to get to the GPIO
882 * resource with the relevant information from a data-only ACPI firmware node
883 * and uses that to obtain the GPIO descriptor to return.
884 *
885 * Returns:
886 * 0 on success, negative errno on failure.
887 *
888 * The @lookup is filled with GPIO descriptor to use with Linux generic GPIO API.
889 * If the GPIO cannot be translated an error will be returned.
890 */
acpi_get_gpiod_from_data(struct fwnode_handle * fwnode,const char * propname,struct acpi_gpio_lookup * lookup)891 static int acpi_get_gpiod_from_data(struct fwnode_handle *fwnode, const char *propname,
892 struct acpi_gpio_lookup *lookup)
893 {
894 int ret;
895
896 if (!is_acpi_data_node(fwnode))
897 return -ENODEV;
898
899 if (!propname)
900 return -EINVAL;
901
902 ret = acpi_gpio_property_lookup(fwnode, propname, lookup);
903 if (ret)
904 return ret;
905
906 return acpi_gpio_resource_lookup(lookup);
907 }
908
acpi_can_fallback_to_crs(struct acpi_device * adev,const char * con_id)909 static bool acpi_can_fallback_to_crs(struct acpi_device *adev,
910 const char *con_id)
911 {
912 /* If there is no ACPI device, there is no _CRS to fall back to */
913 if (!adev)
914 return false;
915
916 /* Never allow fallback if the device has properties */
917 if (acpi_dev_has_props(adev) || adev->driver_gpios)
918 return false;
919
920 return con_id == NULL;
921 }
922
923 static struct gpio_desc *
__acpi_find_gpio(struct fwnode_handle * fwnode,const char * con_id,unsigned int idx,bool can_fallback,struct acpi_gpio_info * info)924 __acpi_find_gpio(struct fwnode_handle *fwnode, const char *con_id, unsigned int idx,
925 bool can_fallback, struct acpi_gpio_info *info)
926 {
927 struct acpi_device *adev = to_acpi_device_node(fwnode);
928 struct acpi_gpio_lookup lookup;
929 struct gpio_desc *desc;
930 char propname[32];
931 int ret;
932
933 memset(&lookup, 0, sizeof(lookup));
934 lookup.params.crs_entry_index = idx;
935 lookup.info = info;
936
937 /* Try first from _DSD */
938 for_each_gpio_property_name(propname, con_id) {
939 if (adev)
940 ret = acpi_get_gpiod_by_index(adev, propname, &lookup);
941 else
942 ret = acpi_get_gpiod_from_data(fwnode, propname, &lookup);
943 if (ret)
944 continue;
945
946 desc = lookup.desc;
947 if (PTR_ERR(desc) == -EPROBE_DEFER)
948 return desc;
949
950 if (!IS_ERR(desc))
951 return desc;
952 }
953
954 /* Then from plain _CRS GPIOs */
955 if (can_fallback) {
956 ret = acpi_get_gpiod_by_index(adev, NULL, &lookup);
957 if (ret)
958 return ERR_PTR(ret);
959
960 return lookup.desc;
961 }
962
963 return ERR_PTR(-ENOENT);
964 }
965
acpi_find_gpio(struct fwnode_handle * fwnode,const char * con_id,unsigned int idx,enum gpiod_flags * dflags,unsigned long * lookupflags)966 struct gpio_desc *acpi_find_gpio(struct fwnode_handle *fwnode,
967 const char *con_id,
968 unsigned int idx,
969 enum gpiod_flags *dflags,
970 unsigned long *lookupflags)
971 {
972 struct acpi_device *adev = to_acpi_device_node(fwnode);
973 bool can_fallback = acpi_can_fallback_to_crs(adev, con_id);
974 struct acpi_gpio_info info = {};
975 struct gpio_desc *desc;
976
977 desc = __acpi_find_gpio(fwnode, con_id, idx, can_fallback, &info);
978 if (IS_ERR(desc))
979 return desc;
980
981 if (info.gpioint &&
982 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
983 dev_dbg(&adev->dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
984 return ERR_PTR(-ENOENT);
985 }
986
987 acpi_gpio_update_gpiod_flags(dflags, &info);
988 acpi_gpio_update_gpiod_lookup_flags(lookupflags, &info);
989
990 acpi_gpio_set_debounce_timeout(desc, info.debounce);
991
992 return desc;
993 }
994
995 /**
996 * acpi_dev_gpio_irq_wake_get_by() - Find GpioInt and translate it to Linux IRQ number
997 * @adev: pointer to a ACPI device to get IRQ from
998 * @con_id: optional name of GpioInt resource
999 * @index: index of GpioInt resource (starting from %0)
1000 * @wake_capable: Set to true if the IRQ is wake capable
1001 *
1002 * If the device has one or more GpioInt resources, this function can be
1003 * used to translate from the GPIO offset in the resource to the Linux IRQ
1004 * number.
1005 *
1006 * The function is idempotent, though each time it runs it will configure GPIO
1007 * pin direction according to the flags in GpioInt resource.
1008 *
1009 * The function takes optional @con_id parameter. If the resource has
1010 * a @con_id in a property, then only those will be taken into account.
1011 *
1012 * The GPIO is considered wake capable if the GpioInt resource specifies
1013 * SharedAndWake or ExclusiveAndWake.
1014 *
1015 * Returns:
1016 * Linux IRQ number (> 0) on success, negative errno on failure.
1017 */
acpi_dev_gpio_irq_wake_get_by(struct acpi_device * adev,const char * con_id,int index,bool * wake_capable)1018 int acpi_dev_gpio_irq_wake_get_by(struct acpi_device *adev, const char *con_id, int index,
1019 bool *wake_capable)
1020 {
1021 struct fwnode_handle *fwnode = acpi_fwnode_handle(adev);
1022 int idx, i;
1023 unsigned int irq_flags;
1024 int ret;
1025
1026 for (i = 0, idx = 0; idx <= index; i++) {
1027 struct acpi_gpio_info info = {};
1028 struct gpio_desc *desc;
1029
1030 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
1031 desc = __acpi_find_gpio(fwnode, con_id, i, true, &info);
1032 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
1033 return PTR_ERR(desc);
1034
1035 if (info.gpioint && idx++ == index) {
1036 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
1037 enum gpiod_flags dflags = GPIOD_ASIS;
1038 char label[32];
1039 int irq;
1040
1041 if (IS_ERR(desc))
1042 return PTR_ERR(desc);
1043
1044 irq = gpiod_to_irq(desc);
1045 if (irq < 0)
1046 return irq;
1047
1048 acpi_gpio_update_gpiod_flags(&dflags, &info);
1049 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
1050
1051 snprintf(label, sizeof(label), "%pfwP GpioInt(%d)", fwnode, index);
1052 ret = gpiod_set_consumer_name(desc, con_id ?: label);
1053 if (ret)
1054 return ret;
1055
1056 ret = gpiod_configure_flags(desc, label, lflags, dflags);
1057 if (ret < 0)
1058 return ret;
1059
1060 /* ACPI uses hundredths of milliseconds units */
1061 ret = gpio_set_debounce_timeout(desc, info.debounce * 10);
1062 if (ret)
1063 return ret;
1064
1065 irq_flags = acpi_dev_get_irq_type(info.triggering,
1066 info.polarity);
1067
1068 /*
1069 * If the IRQ is not already in use then set type
1070 * if specified and different than the current one.
1071 */
1072 if (can_request_irq(irq, irq_flags)) {
1073 if (irq_flags != IRQ_TYPE_NONE &&
1074 irq_flags != irq_get_trigger_type(irq))
1075 irq_set_irq_type(irq, irq_flags);
1076 } else {
1077 dev_dbg(&adev->dev, "IRQ %d already in use\n", irq);
1078 }
1079
1080 /* avoid suspend issues with GPIOs when systems are using S3 */
1081 if (wake_capable && acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
1082 *wake_capable = info.wake_capable;
1083
1084 return irq;
1085 }
1086
1087 }
1088 return -ENOENT;
1089 }
1090 EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_wake_get_by);
1091
1092 static acpi_status
acpi_gpio_adr_space_handler(u32 function,acpi_physical_address address,u32 bits,u64 * value,void * handler_context,void * region_context)1093 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
1094 u32 bits, u64 *value, void *handler_context,
1095 void *region_context)
1096 {
1097 struct acpi_gpio_chip *achip = region_context;
1098 struct gpio_chip *chip = achip->chip;
1099 struct acpi_resource_gpio *agpio;
1100 struct acpi_resource *ares;
1101 unsigned int length;
1102 acpi_status status;
1103 unsigned int i;
1104 u16 pin_index;
1105
1106 status = acpi_buffer_to_resource(achip->conn_info.connection,
1107 achip->conn_info.length, &ares);
1108 if (ACPI_FAILURE(status))
1109 return status;
1110
1111 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
1112 ACPI_FREE(ares);
1113 return AE_BAD_PARAMETER;
1114 }
1115
1116 agpio = &ares->data.gpio;
1117
1118 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
1119 function == ACPI_WRITE)) {
1120 ACPI_FREE(ares);
1121 return AE_BAD_PARAMETER;
1122 }
1123
1124 /* address represents GPIO pin index in connection table */
1125 if (address >= agpio->pin_table_length) {
1126 ACPI_FREE(ares);
1127 return AE_BAD_PARAMETER;
1128 }
1129
1130 pin_index = address;
1131 length = min_t(unsigned int, agpio->pin_table_length, pin_index + bits);
1132 for (i = pin_index; i < length; ++i) {
1133 unsigned int pin = agpio->pin_table[i];
1134 struct acpi_gpio_connection *conn;
1135 struct gpio_desc *desc;
1136 u16 word, shift;
1137 bool found;
1138
1139 mutex_lock(&achip->conn_lock);
1140
1141 found = false;
1142 list_for_each_entry(conn, &achip->conns, node) {
1143 if (conn->pin == pin) {
1144 found = true;
1145 desc = conn->desc;
1146 break;
1147 }
1148 }
1149
1150 /*
1151 * The same GPIO can be shared between operation region and
1152 * event but only if the access here is ACPI_READ. In that
1153 * case we "borrow" the event GPIO instead.
1154 */
1155 if (!found && agpio->shareable == ACPI_SHARED &&
1156 function == ACPI_READ) {
1157 struct acpi_gpio_event *event;
1158
1159 list_for_each_entry(event, &achip->events, node) {
1160 if (event->pin == pin) {
1161 desc = event->desc;
1162 found = true;
1163 break;
1164 }
1165 }
1166 }
1167
1168 if (!found) {
1169 desc = acpi_request_own_gpiod(chip, agpio, i, "ACPI:OpRegion");
1170 if (IS_ERR(desc)) {
1171 mutex_unlock(&achip->conn_lock);
1172 status = AE_ERROR;
1173 goto out;
1174 }
1175
1176 conn = kzalloc_obj(*conn);
1177 if (!conn) {
1178 gpiochip_free_own_desc(desc);
1179 mutex_unlock(&achip->conn_lock);
1180 status = AE_NO_MEMORY;
1181 goto out;
1182 }
1183
1184 conn->pin = pin;
1185 conn->desc = desc;
1186 list_add_tail(&conn->node, &achip->conns);
1187 }
1188
1189 mutex_unlock(&achip->conn_lock);
1190
1191 /*
1192 * For the cases when OperationRegion() consists of more than
1193 * 64 bits calculate the word and bit shift to use that one to
1194 * access the value.
1195 */
1196 word = i / 64;
1197 shift = i % 64;
1198
1199 if (function == ACPI_WRITE) {
1200 gpiod_set_raw_value_cansleep(desc, value[word] & BIT_ULL(shift));
1201 } else {
1202 if (gpiod_get_raw_value_cansleep(desc))
1203 value[word] |= BIT_ULL(shift);
1204 else
1205 value[word] &= ~BIT_ULL(shift);
1206 }
1207 }
1208
1209 out:
1210 ACPI_FREE(ares);
1211 return status;
1212 }
1213
acpi_gpiochip_request_regions(struct acpi_gpio_chip * achip)1214 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
1215 {
1216 struct gpio_chip *chip = achip->chip;
1217 acpi_handle handle = ACPI_HANDLE(chip->parent);
1218 acpi_status status;
1219
1220 INIT_LIST_HEAD(&achip->conns);
1221 mutex_init(&achip->conn_lock);
1222 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1223 acpi_gpio_adr_space_handler,
1224 NULL, achip);
1225 if (ACPI_FAILURE(status))
1226 dev_err(chip->parent,
1227 "Failed to install GPIO OpRegion handler\n");
1228 }
1229
acpi_gpiochip_free_regions(struct acpi_gpio_chip * achip)1230 static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
1231 {
1232 struct gpio_chip *chip = achip->chip;
1233 acpi_handle handle = ACPI_HANDLE(chip->parent);
1234 struct acpi_gpio_connection *conn, *tmp;
1235 acpi_status status;
1236
1237 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1238 acpi_gpio_adr_space_handler);
1239 if (ACPI_FAILURE(status)) {
1240 dev_err(chip->parent,
1241 "Failed to remove GPIO OpRegion handler\n");
1242 return;
1243 }
1244
1245 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
1246 gpiochip_free_own_desc(conn->desc);
1247 list_del(&conn->node);
1248 kfree(conn);
1249 }
1250 }
1251
acpi_gpiochip_add(struct gpio_chip * chip)1252 void acpi_gpiochip_add(struct gpio_chip *chip)
1253 {
1254 struct acpi_gpio_chip *acpi_gpio;
1255 struct acpi_device *adev;
1256 acpi_status status;
1257
1258 if (!chip || !chip->parent)
1259 return;
1260
1261 adev = ACPI_COMPANION(chip->parent);
1262 if (!adev)
1263 return;
1264
1265 acpi_gpio = kzalloc_obj(*acpi_gpio);
1266 if (!acpi_gpio) {
1267 dev_err(chip->parent,
1268 "Failed to allocate memory for ACPI GPIO chip\n");
1269 return;
1270 }
1271
1272 acpi_gpio->chip = chip;
1273 INIT_LIST_HEAD(&acpi_gpio->events);
1274 INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
1275
1276 status = acpi_attach_data(adev->handle, acpi_gpio_chip_dh, acpi_gpio);
1277 if (ACPI_FAILURE(status)) {
1278 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
1279 kfree(acpi_gpio);
1280 return;
1281 }
1282
1283 acpi_gpiochip_request_regions(acpi_gpio);
1284 acpi_dev_clear_dependencies(adev);
1285 }
1286
acpi_gpiochip_remove(struct gpio_chip * chip)1287 void acpi_gpiochip_remove(struct gpio_chip *chip)
1288 {
1289 struct acpi_gpio_chip *acpi_gpio;
1290 acpi_handle handle;
1291 acpi_status status;
1292
1293 if (!chip || !chip->parent)
1294 return;
1295
1296 handle = ACPI_HANDLE(chip->parent);
1297 if (!handle)
1298 return;
1299
1300 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1301 if (ACPI_FAILURE(status)) {
1302 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
1303 return;
1304 }
1305
1306 acpi_gpiochip_free_regions(acpi_gpio);
1307
1308 acpi_detach_data(handle, acpi_gpio_chip_dh);
1309 kfree(acpi_gpio);
1310 }
1311
acpi_gpio_package_count(const union acpi_object * obj)1312 static int acpi_gpio_package_count(const union acpi_object *obj)
1313 {
1314 const union acpi_object *element = obj->package.elements;
1315 const union acpi_object *end = element + obj->package.count;
1316 unsigned int count = 0;
1317
1318 while (element < end) {
1319 switch (element->type) {
1320 case ACPI_TYPE_LOCAL_REFERENCE:
1321 case ACPI_TYPE_STRING:
1322 element += 3;
1323 fallthrough;
1324 case ACPI_TYPE_INTEGER:
1325 element++;
1326 count++;
1327 break;
1328
1329 default:
1330 return -EPROTO;
1331 }
1332 }
1333
1334 return count;
1335 }
1336
acpi_find_gpio_count(struct acpi_resource * ares,void * data)1337 static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1338 {
1339 unsigned int *count = data;
1340
1341 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1342 *count += ares->data.gpio.pin_table_length;
1343
1344 return 1;
1345 }
1346
1347 /**
1348 * acpi_gpio_count - count the GPIOs associated with a firmware node / function
1349 * @fwnode: firmware node of the GPIO consumer
1350 * @con_id: function within the GPIO consumer
1351 *
1352 * Returns:
1353 * The number of GPIOs associated with a firmware node / function or %-ENOENT,
1354 * if no GPIO has been assigned to the requested function.
1355 */
acpi_gpio_count(const struct fwnode_handle * fwnode,const char * con_id)1356 int acpi_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
1357 {
1358 struct acpi_device *adev = to_acpi_device_node(fwnode);
1359 const union acpi_object *obj;
1360 const struct acpi_gpio_mapping *gm;
1361 int count = -ENOENT;
1362 int ret;
1363 char propname[32];
1364
1365 /* Try first from _DSD */
1366 for_each_gpio_property_name(propname, con_id) {
1367 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY, &obj);
1368 if (ret == 0) {
1369 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1370 count = 1;
1371 else if (obj->type == ACPI_TYPE_PACKAGE)
1372 count = acpi_gpio_package_count(obj);
1373 } else if (adev->driver_gpios) {
1374 for (gm = adev->driver_gpios; gm->name; gm++)
1375 if (strcmp(propname, gm->name) == 0) {
1376 count = gm->size;
1377 break;
1378 }
1379 }
1380 if (count > 0)
1381 break;
1382 }
1383
1384 /* Then from plain _CRS GPIOs */
1385 if (count < 0) {
1386 struct list_head resource_list;
1387 unsigned int crs_count = 0;
1388
1389 if (!acpi_can_fallback_to_crs(adev, con_id))
1390 return count;
1391
1392 INIT_LIST_HEAD(&resource_list);
1393 acpi_dev_get_resources(adev, &resource_list,
1394 acpi_find_gpio_count, &crs_count);
1395 acpi_dev_free_resource_list(&resource_list);
1396 if (crs_count > 0)
1397 count = crs_count;
1398 }
1399 return count ? count : -ENOENT;
1400 }
1401