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