1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/errno.h>
14#include <linux/gpio.h>
15#include <linux/gpio/consumer.h>
16#include <linux/gpio/driver.h>
17#include <linux/gpio/machine.h>
18#include <linux/export.h>
19#include <linux/acpi.h>
20#include <linux/interrupt.h>
21#include <linux/mutex.h>
22#include <linux/pinctrl/pinctrl.h>
23
24#include "gpiolib.h"
25
26struct acpi_gpio_event {
27 struct list_head node;
28 struct list_head initial_sync_list;
29 acpi_handle handle;
30 unsigned int pin;
31 unsigned int irq;
32 struct gpio_desc *desc;
33};
34
35struct acpi_gpio_connection {
36 struct list_head node;
37 unsigned int pin;
38 struct gpio_desc *desc;
39};
40
41struct acpi_gpio_chip {
42
43
44
45
46
47 struct acpi_connection_info conn_info;
48 struct list_head conns;
49 struct mutex conn_lock;
50 struct gpio_chip *chip;
51 struct list_head events;
52};
53
54static LIST_HEAD(acpi_gpio_initial_sync_list);
55static DEFINE_MUTEX(acpi_gpio_initial_sync_list_lock);
56
57static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
58{
59 if (!gc->parent)
60 return false;
61
62 return ACPI_HANDLE(gc->parent) == data;
63}
64
65
66
67
68
69
70
71
72
73
74
75static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
76{
77 struct gpio_chip *chip;
78 acpi_handle handle;
79 acpi_status status;
80
81 status = acpi_get_handle(NULL, path, &handle);
82 if (ACPI_FAILURE(status))
83 return ERR_PTR(-ENODEV);
84
85 chip = gpiochip_find(handle, acpi_gpiochip_find);
86 if (!chip)
87 return ERR_PTR(-EPROBE_DEFER);
88
89 return gpiochip_get_desc(chip, pin);
90}
91
92static void acpi_gpio_add_to_initial_sync_list(struct acpi_gpio_event *event)
93{
94 mutex_lock(&acpi_gpio_initial_sync_list_lock);
95 list_add(&event->initial_sync_list, &acpi_gpio_initial_sync_list);
96 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
97}
98
99static void acpi_gpio_del_from_initial_sync_list(struct acpi_gpio_event *event)
100{
101 mutex_lock(&acpi_gpio_initial_sync_list_lock);
102 if (!list_empty(&event->initial_sync_list))
103 list_del_init(&event->initial_sync_list);
104 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
105}
106
107static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
108{
109 struct acpi_gpio_event *event = data;
110
111 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
112
113 return IRQ_HANDLED;
114}
115
116static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
117{
118 struct acpi_gpio_event *event = data;
119
120 acpi_execute_simple_method(event->handle, NULL, event->pin);
121
122 return IRQ_HANDLED;
123}
124
125static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
126{
127
128}
129
130bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
131 struct acpi_resource_gpio **agpio)
132{
133 struct acpi_resource_gpio *gpio;
134
135 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
136 return false;
137
138 gpio = &ares->data.gpio;
139 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
140 return false;
141
142 *agpio = gpio;
143 return true;
144}
145EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
146
147static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
148 void *context)
149{
150 struct acpi_gpio_chip *acpi_gpio = context;
151 struct gpio_chip *chip = acpi_gpio->chip;
152 struct acpi_resource_gpio *agpio;
153 acpi_handle handle, evt_handle;
154 struct acpi_gpio_event *event;
155 irq_handler_t handler = NULL;
156 struct gpio_desc *desc;
157 unsigned long irqflags;
158 int ret, pin, irq, value;
159
160 if (!acpi_gpio_get_irq_resource(ares, &agpio))
161 return AE_OK;
162
163 handle = ACPI_HANDLE(chip->parent);
164 pin = agpio->pin_table[0];
165
166 if (pin <= 255) {
167 char ev_name[5];
168 sprintf(ev_name, "_%c%02hhX",
169 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
170 pin);
171 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
172 handler = acpi_gpio_irq_handler;
173 }
174 if (!handler) {
175 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
176 handler = acpi_gpio_irq_handler_evt;
177 }
178 if (!handler)
179 return AE_OK;
180
181 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
182 if (IS_ERR(desc)) {
183 dev_err(chip->parent, "Failed to request GPIO\n");
184 return AE_ERROR;
185 }
186
187 gpiod_direction_input(desc);
188
189 value = gpiod_get_value(desc);
190
191 ret = gpiochip_lock_as_irq(chip, pin);
192 if (ret) {
193 dev_err(chip->parent, "Failed to lock GPIO as interrupt\n");
194 goto fail_free_desc;
195 }
196
197 irq = gpiod_to_irq(desc);
198 if (irq < 0) {
199 dev_err(chip->parent, "Failed to translate GPIO to IRQ\n");
200 goto fail_unlock_irq;
201 }
202
203 irqflags = IRQF_ONESHOT;
204 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
205 if (agpio->polarity == ACPI_ACTIVE_HIGH)
206 irqflags |= IRQF_TRIGGER_HIGH;
207 else
208 irqflags |= IRQF_TRIGGER_LOW;
209 } else {
210 switch (agpio->polarity) {
211 case ACPI_ACTIVE_HIGH:
212 irqflags |= IRQF_TRIGGER_RISING;
213 break;
214 case ACPI_ACTIVE_LOW:
215 irqflags |= IRQF_TRIGGER_FALLING;
216 break;
217 default:
218 irqflags |= IRQF_TRIGGER_RISING |
219 IRQF_TRIGGER_FALLING;
220 break;
221 }
222 }
223
224 event = kzalloc(sizeof(*event), GFP_KERNEL);
225 if (!event)
226 goto fail_unlock_irq;
227
228 event->handle = evt_handle;
229 event->irq = irq;
230 event->pin = pin;
231 event->desc = desc;
232 INIT_LIST_HEAD(&event->initial_sync_list);
233
234 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
235 "ACPI:Event", event);
236 if (ret) {
237 dev_err(chip->parent,
238 "Failed to setup interrupt handler for %d\n",
239 event->irq);
240 goto fail_free_event;
241 }
242
243 if (agpio->wake_capable == ACPI_WAKE_CAPABLE)
244 enable_irq_wake(irq);
245
246 list_add_tail(&event->node, &acpi_gpio->events);
247
248
249
250
251
252
253
254 if (handler == acpi_gpio_irq_handler &&
255 (((irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
256 ((irqflags & IRQF_TRIGGER_FALLING) && value == 0)))
257 acpi_gpio_add_to_initial_sync_list(event);
258
259 return AE_OK;
260
261fail_free_event:
262 kfree(event);
263fail_unlock_irq:
264 gpiochip_unlock_as_irq(chip, pin);
265fail_free_desc:
266 gpiochip_free_own_desc(desc);
267
268 return AE_ERROR;
269}
270
271
272
273
274
275
276
277
278
279
280
281void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
282{
283 struct acpi_gpio_chip *acpi_gpio;
284 acpi_handle handle;
285 acpi_status status;
286
287 if (!chip->parent || !chip->to_irq)
288 return;
289
290 handle = ACPI_HANDLE(chip->parent);
291 if (!handle)
292 return;
293
294 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
295 if (ACPI_FAILURE(status))
296 return;
297
298 acpi_walk_resources(handle, "_AEI",
299 acpi_gpiochip_request_interrupt, acpi_gpio);
300}
301EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
302
303
304
305
306
307
308
309
310void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
311{
312 struct acpi_gpio_chip *acpi_gpio;
313 struct acpi_gpio_event *event, *ep;
314 acpi_handle handle;
315 acpi_status status;
316
317 if (!chip->parent || !chip->to_irq)
318 return;
319
320 handle = ACPI_HANDLE(chip->parent);
321 if (!handle)
322 return;
323
324 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
325 if (ACPI_FAILURE(status))
326 return;
327
328 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
329 struct gpio_desc *desc;
330
331 acpi_gpio_del_from_initial_sync_list(event);
332
333 if (irqd_is_wakeup_set(irq_get_irq_data(event->irq)))
334 disable_irq_wake(event->irq);
335
336 free_irq(event->irq, event);
337 desc = event->desc;
338 if (WARN_ON(IS_ERR(desc)))
339 continue;
340 gpiochip_unlock_as_irq(chip, event->pin);
341 gpiochip_free_own_desc(desc);
342 list_del(&event->node);
343 kfree(event);
344 }
345}
346EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
347
348int acpi_dev_add_driver_gpios(struct acpi_device *adev,
349 const struct acpi_gpio_mapping *gpios)
350{
351 if (adev && gpios) {
352 adev->driver_gpios = gpios;
353 return 0;
354 }
355 return -EINVAL;
356}
357EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
358
359static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
360{
361 acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
362}
363
364int devm_acpi_dev_add_driver_gpios(struct device *dev,
365 const struct acpi_gpio_mapping *gpios)
366{
367 void *res;
368 int ret;
369
370 res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
371 if (!res)
372 return -ENOMEM;
373
374 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
375 if (ret) {
376 devres_free(res);
377 return ret;
378 }
379 devres_add(dev, res);
380 return 0;
381}
382EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
383
384void devm_acpi_dev_remove_driver_gpios(struct device *dev)
385{
386 WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
387}
388EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
389
390static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
391 const char *name, int index,
392 struct fwnode_reference_args *args,
393 unsigned int *quirks)
394{
395 const struct acpi_gpio_mapping *gm;
396
397 if (!adev->driver_gpios)
398 return false;
399
400 for (gm = adev->driver_gpios; gm->name; gm++)
401 if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
402 const struct acpi_gpio_params *par = gm->data + index;
403
404 args->fwnode = acpi_fwnode_handle(adev);
405 args->args[0] = par->crs_entry_index;
406 args->args[1] = par->line_index;
407 args->args[2] = par->active_low;
408 args->nargs = 3;
409
410 *quirks = gm->quirks;
411 return true;
412 }
413
414 return false;
415}
416
417static enum gpiod_flags
418acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
419{
420 bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
421
422 switch (agpio->io_restriction) {
423 case ACPI_IO_RESTRICT_INPUT:
424 return GPIOD_IN;
425 case ACPI_IO_RESTRICT_OUTPUT:
426
427
428
429
430
431
432 return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
433 default:
434
435
436
437
438 return GPIOD_ASIS;
439 }
440}
441
442static int
443__acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
444{
445 int ret = 0;
446
447
448
449
450
451
452 if (update & GPIOD_FLAGS_BIT_DIR_SET) {
453 enum gpiod_flags diff = *flags ^ update;
454
455
456
457
458
459
460
461
462 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
463 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
464 ret = -EINVAL;
465 *flags = update;
466 }
467 return ret;
468}
469
470int
471acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
472{
473 struct device *dev = &info->adev->dev;
474 enum gpiod_flags old = *flags;
475 int ret;
476
477 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
478 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
479 if (ret)
480 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
481 } else {
482 if (ret)
483 dev_dbg(dev, "Override GPIO initialization flags\n");
484 *flags = old;
485 }
486
487 return ret;
488}
489
490struct acpi_gpio_lookup {
491 struct acpi_gpio_info info;
492 int index;
493 int pin_index;
494 bool active_low;
495 struct gpio_desc *desc;
496 int n;
497};
498
499static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
500{
501 struct acpi_gpio_lookup *lookup = data;
502
503 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
504 return 1;
505
506 if (!lookup->desc) {
507 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
508 bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
509 int pin_index;
510
511 if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
512 lookup->index++;
513
514 if (lookup->n++ != lookup->index)
515 return 1;
516
517 pin_index = lookup->pin_index;
518 if (pin_index >= agpio->pin_table_length)
519 return 1;
520
521 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
522 agpio->pin_table[pin_index]);
523 lookup->info.gpioint = gpioint;
524
525
526
527
528
529
530
531
532 if (lookup->info.gpioint) {
533 lookup->info.flags = GPIOD_IN;
534 lookup->info.polarity = agpio->polarity;
535 lookup->info.triggering = agpio->triggering;
536 } else {
537 lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
538 lookup->info.polarity = lookup->active_low;
539 }
540 }
541
542 return 1;
543}
544
545static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
546 struct acpi_gpio_info *info)
547{
548 struct acpi_device *adev = lookup->info.adev;
549 struct list_head res_list;
550 int ret;
551
552 INIT_LIST_HEAD(&res_list);
553
554 ret = acpi_dev_get_resources(adev, &res_list,
555 acpi_populate_gpio_lookup,
556 lookup);
557 if (ret < 0)
558 return ret;
559
560 acpi_dev_free_resource_list(&res_list);
561
562 if (!lookup->desc)
563 return -ENOENT;
564
565 if (info)
566 *info = lookup->info;
567 return 0;
568}
569
570static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
571 const char *propname, int index,
572 struct acpi_gpio_lookup *lookup)
573{
574 struct fwnode_reference_args args;
575 unsigned int quirks = 0;
576 int ret;
577
578 memset(&args, 0, sizeof(args));
579 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
580 &args);
581 if (ret) {
582 struct acpi_device *adev = to_acpi_device_node(fwnode);
583
584 if (!adev)
585 return ret;
586
587 if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
588 &quirks))
589 return ret;
590 }
591
592
593
594
595 if (!to_acpi_device_node(args.fwnode))
596 return -EINVAL;
597 if (args.nargs != 3)
598 return -EPROTO;
599
600 lookup->index = args.args[0];
601 lookup->pin_index = args.args[1];
602 lookup->active_low = !!args.args[2];
603
604 lookup->info.adev = to_acpi_device_node(args.fwnode);
605 lookup->info.quirks = quirks;
606
607 return 0;
608}
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
633 const char *propname, int index,
634 struct acpi_gpio_info *info)
635{
636 struct acpi_gpio_lookup lookup;
637 int ret;
638
639 if (!adev)
640 return ERR_PTR(-ENODEV);
641
642 memset(&lookup, 0, sizeof(lookup));
643 lookup.index = index;
644
645 if (propname) {
646 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
647
648 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
649 propname, index, &lookup);
650 if (ret)
651 return ERR_PTR(ret);
652
653 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
654 dev_name(&lookup.info.adev->dev), lookup.index,
655 lookup.pin_index, lookup.active_low);
656 } else {
657 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
658 lookup.info.adev = adev;
659 }
660
661 ret = acpi_gpio_resource_lookup(&lookup, info);
662 return ret ? ERR_PTR(ret) : lookup.desc;
663}
664
665struct gpio_desc *acpi_find_gpio(struct device *dev,
666 const char *con_id,
667 unsigned int idx,
668 enum gpiod_flags *dflags,
669 enum gpio_lookup_flags *lookupflags)
670{
671 struct acpi_device *adev = ACPI_COMPANION(dev);
672 struct acpi_gpio_info info;
673 struct gpio_desc *desc;
674 char propname[32];
675 int i;
676
677
678 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
679 if (con_id) {
680 snprintf(propname, sizeof(propname), "%s-%s",
681 con_id, gpio_suffixes[i]);
682 } else {
683 snprintf(propname, sizeof(propname), "%s",
684 gpio_suffixes[i]);
685 }
686
687 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
688 if (!IS_ERR(desc))
689 break;
690 if (PTR_ERR(desc) == -EPROBE_DEFER)
691 return ERR_CAST(desc);
692 }
693
694
695 if (IS_ERR(desc)) {
696 if (!acpi_can_fallback_to_crs(adev, con_id))
697 return ERR_PTR(-ENOENT);
698
699 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
700 if (IS_ERR(desc))
701 return desc;
702 }
703
704 if (info.gpioint &&
705 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
706 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
707 return ERR_PTR(-ENOENT);
708 }
709
710 if (info.polarity == GPIO_ACTIVE_LOW)
711 *lookupflags |= GPIO_ACTIVE_LOW;
712
713 acpi_gpio_update_gpiod_flags(dflags, &info);
714 return desc;
715}
716
717
718
719
720
721
722
723
724
725
726
727
728
729struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
730 const char *propname, int index,
731 struct acpi_gpio_info *info)
732{
733 struct acpi_gpio_lookup lookup;
734 struct acpi_device *adev;
735 int ret;
736
737 adev = to_acpi_device_node(fwnode);
738 if (adev)
739 return acpi_get_gpiod_by_index(adev, propname, index, info);
740
741 if (!is_acpi_data_node(fwnode))
742 return ERR_PTR(-ENODEV);
743
744 if (!propname)
745 return ERR_PTR(-EINVAL);
746
747 memset(&lookup, 0, sizeof(lookup));
748 lookup.index = index;
749
750 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
751 if (ret)
752 return ERR_PTR(ret);
753
754 ret = acpi_gpio_resource_lookup(&lookup, info);
755 return ret ? ERR_PTR(ret) : lookup.desc;
756}
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
773{
774 int idx, i;
775 unsigned int irq_flags;
776 int ret;
777
778 for (i = 0, idx = 0; idx <= index; i++) {
779 struct acpi_gpio_info info;
780 struct gpio_desc *desc;
781
782 desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
783
784
785 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
786 return PTR_ERR(desc);
787
788 if (info.gpioint && idx++ == index) {
789 char label[32];
790 int irq;
791
792 if (IS_ERR(desc))
793 return PTR_ERR(desc);
794
795 irq = gpiod_to_irq(desc);
796 if (irq < 0)
797 return irq;
798
799 snprintf(label, sizeof(label), "GpioInt() %d", index);
800 ret = gpiod_configure_flags(desc, label, 0, info.flags);
801 if (ret < 0)
802 return ret;
803
804 irq_flags = acpi_dev_get_irq_type(info.triggering,
805 info.polarity);
806
807
808 if (irq_flags != IRQ_TYPE_NONE &&
809 irq_flags != irq_get_trigger_type(irq))
810 irq_set_irq_type(irq, irq_flags);
811
812 return irq;
813 }
814
815 }
816 return -ENOENT;
817}
818EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
819
820static acpi_status
821acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
822 u32 bits, u64 *value, void *handler_context,
823 void *region_context)
824{
825 struct acpi_gpio_chip *achip = region_context;
826 struct gpio_chip *chip = achip->chip;
827 struct acpi_resource_gpio *agpio;
828 struct acpi_resource *ares;
829 int pin_index = (int)address;
830 acpi_status status;
831 int length;
832 int i;
833
834 status = acpi_buffer_to_resource(achip->conn_info.connection,
835 achip->conn_info.length, &ares);
836 if (ACPI_FAILURE(status))
837 return status;
838
839 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
840 ACPI_FREE(ares);
841 return AE_BAD_PARAMETER;
842 }
843
844 agpio = &ares->data.gpio;
845
846 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
847 function == ACPI_WRITE)) {
848 ACPI_FREE(ares);
849 return AE_BAD_PARAMETER;
850 }
851
852 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
853 for (i = pin_index; i < length; ++i) {
854 int pin = agpio->pin_table[i];
855 struct acpi_gpio_connection *conn;
856 struct gpio_desc *desc;
857 bool found;
858
859 mutex_lock(&achip->conn_lock);
860
861 found = false;
862 list_for_each_entry(conn, &achip->conns, node) {
863 if (conn->pin == pin) {
864 found = true;
865 desc = conn->desc;
866 break;
867 }
868 }
869
870
871
872
873
874
875 if (!found && agpio->shareable == ACPI_SHARED &&
876 function == ACPI_READ) {
877 struct acpi_gpio_event *event;
878
879 list_for_each_entry(event, &achip->events, node) {
880 if (event->pin == pin) {
881 desc = event->desc;
882 found = true;
883 break;
884 }
885 }
886 }
887
888 if (!found) {
889 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
890 const char *label = "ACPI:OpRegion";
891 int err;
892
893 desc = gpiochip_request_own_desc(chip, pin, label);
894 if (IS_ERR(desc)) {
895 status = AE_ERROR;
896 mutex_unlock(&achip->conn_lock);
897 goto out;
898 }
899
900 err = gpiod_configure_flags(desc, label, 0, flags);
901 if (err < 0) {
902 status = AE_NOT_CONFIGURED;
903 gpiochip_free_own_desc(desc);
904 mutex_unlock(&achip->conn_lock);
905 goto out;
906 }
907
908 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
909 if (!conn) {
910 status = AE_NO_MEMORY;
911 gpiochip_free_own_desc(desc);
912 mutex_unlock(&achip->conn_lock);
913 goto out;
914 }
915
916 conn->pin = pin;
917 conn->desc = desc;
918 list_add_tail(&conn->node, &achip->conns);
919 }
920
921 mutex_unlock(&achip->conn_lock);
922
923 if (function == ACPI_WRITE)
924 gpiod_set_raw_value_cansleep(desc,
925 !!((1 << i) & *value));
926 else
927 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
928 }
929
930out:
931 ACPI_FREE(ares);
932 return status;
933}
934
935static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
936{
937 struct gpio_chip *chip = achip->chip;
938 acpi_handle handle = ACPI_HANDLE(chip->parent);
939 acpi_status status;
940
941 INIT_LIST_HEAD(&achip->conns);
942 mutex_init(&achip->conn_lock);
943 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
944 acpi_gpio_adr_space_handler,
945 NULL, achip);
946 if (ACPI_FAILURE(status))
947 dev_err(chip->parent,
948 "Failed to install GPIO OpRegion handler\n");
949}
950
951static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
952{
953 struct gpio_chip *chip = achip->chip;
954 acpi_handle handle = ACPI_HANDLE(chip->parent);
955 struct acpi_gpio_connection *conn, *tmp;
956 acpi_status status;
957
958 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
959 acpi_gpio_adr_space_handler);
960 if (ACPI_FAILURE(status)) {
961 dev_err(chip->parent,
962 "Failed to remove GPIO OpRegion handler\n");
963 return;
964 }
965
966 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
967 gpiochip_free_own_desc(conn->desc);
968 list_del(&conn->node);
969 kfree(conn);
970 }
971}
972
973static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
974 struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
975 const char **name, unsigned int *lflags, unsigned int *dflags)
976{
977 struct gpio_chip *chip = achip->chip;
978 struct gpio_desc *desc;
979 u32 gpios[2];
980 int ret;
981
982 *lflags = 0;
983 *dflags = 0;
984 *name = NULL;
985
986 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
987 ARRAY_SIZE(gpios));
988 if (ret < 0)
989 return ERR_PTR(ret);
990
991 desc = gpiochip_get_desc(chip, gpios[0]);
992 if (IS_ERR(desc))
993 return desc;
994
995 if (gpios[1])
996 *lflags |= GPIO_ACTIVE_LOW;
997
998 if (fwnode_property_present(fwnode, "input"))
999 *dflags |= GPIOD_IN;
1000 else if (fwnode_property_present(fwnode, "output-low"))
1001 *dflags |= GPIOD_OUT_LOW;
1002 else if (fwnode_property_present(fwnode, "output-high"))
1003 *dflags |= GPIOD_OUT_HIGH;
1004 else
1005 return ERR_PTR(-EINVAL);
1006
1007 fwnode_property_read_string(fwnode, "line-name", name);
1008
1009 return desc;
1010}
1011
1012static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1013{
1014 struct gpio_chip *chip = achip->chip;
1015 struct fwnode_handle *fwnode;
1016
1017 device_for_each_child_node(chip->parent, fwnode) {
1018 unsigned int lflags, dflags;
1019 struct gpio_desc *desc;
1020 const char *name;
1021 int ret;
1022
1023 if (!fwnode_property_present(fwnode, "gpio-hog"))
1024 continue;
1025
1026 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1027 &lflags, &dflags);
1028 if (IS_ERR(desc))
1029 continue;
1030
1031 ret = gpiod_hog(desc, name, lflags, dflags);
1032 if (ret) {
1033 dev_err(chip->parent, "Failed to hog GPIO\n");
1034 fwnode_handle_put(fwnode);
1035 return;
1036 }
1037 }
1038}
1039
1040void acpi_gpiochip_add(struct gpio_chip *chip)
1041{
1042 struct acpi_gpio_chip *acpi_gpio;
1043 acpi_handle handle;
1044 acpi_status status;
1045
1046 if (!chip || !chip->parent)
1047 return;
1048
1049 handle = ACPI_HANDLE(chip->parent);
1050 if (!handle)
1051 return;
1052
1053 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1054 if (!acpi_gpio) {
1055 dev_err(chip->parent,
1056 "Failed to allocate memory for ACPI GPIO chip\n");
1057 return;
1058 }
1059
1060 acpi_gpio->chip = chip;
1061 INIT_LIST_HEAD(&acpi_gpio->events);
1062
1063 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
1064 if (ACPI_FAILURE(status)) {
1065 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
1066 kfree(acpi_gpio);
1067 return;
1068 }
1069
1070 if (!chip->names)
1071 devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
1072
1073 acpi_gpiochip_request_regions(acpi_gpio);
1074 acpi_gpiochip_scan_gpios(acpi_gpio);
1075 acpi_walk_dep_device_list(handle);
1076}
1077
1078void acpi_gpiochip_remove(struct gpio_chip *chip)
1079{
1080 struct acpi_gpio_chip *acpi_gpio;
1081 acpi_handle handle;
1082 acpi_status status;
1083
1084 if (!chip || !chip->parent)
1085 return;
1086
1087 handle = ACPI_HANDLE(chip->parent);
1088 if (!handle)
1089 return;
1090
1091 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1092 if (ACPI_FAILURE(status)) {
1093 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
1094 return;
1095 }
1096
1097 acpi_gpiochip_free_regions(acpi_gpio);
1098
1099 acpi_detach_data(handle, acpi_gpio_chip_dh);
1100 kfree(acpi_gpio);
1101}
1102
1103static int acpi_gpio_package_count(const union acpi_object *obj)
1104{
1105 const union acpi_object *element = obj->package.elements;
1106 const union acpi_object *end = element + obj->package.count;
1107 unsigned int count = 0;
1108
1109 while (element < end) {
1110 switch (element->type) {
1111 case ACPI_TYPE_LOCAL_REFERENCE:
1112 element += 3;
1113
1114 case ACPI_TYPE_INTEGER:
1115 element++;
1116 count++;
1117 break;
1118
1119 default:
1120 return -EPROTO;
1121 }
1122 }
1123
1124 return count;
1125}
1126
1127static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1128{
1129 unsigned int *count = data;
1130
1131 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1132 *count += ares->data.gpio.pin_table_length;
1133
1134 return 1;
1135}
1136
1137
1138
1139
1140
1141
1142
1143
1144int acpi_gpio_count(struct device *dev, const char *con_id)
1145{
1146 struct acpi_device *adev = ACPI_COMPANION(dev);
1147 const union acpi_object *obj;
1148 const struct acpi_gpio_mapping *gm;
1149 int count = -ENOENT;
1150 int ret;
1151 char propname[32];
1152 unsigned int i;
1153
1154
1155 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1156 if (con_id)
1157 snprintf(propname, sizeof(propname), "%s-%s",
1158 con_id, gpio_suffixes[i]);
1159 else
1160 snprintf(propname, sizeof(propname), "%s",
1161 gpio_suffixes[i]);
1162
1163 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1164 &obj);
1165 if (ret == 0) {
1166 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1167 count = 1;
1168 else if (obj->type == ACPI_TYPE_PACKAGE)
1169 count = acpi_gpio_package_count(obj);
1170 } else if (adev->driver_gpios) {
1171 for (gm = adev->driver_gpios; gm->name; gm++)
1172 if (strcmp(propname, gm->name) == 0) {
1173 count = gm->size;
1174 break;
1175 }
1176 }
1177 if (count > 0)
1178 break;
1179 }
1180
1181
1182 if (count < 0) {
1183 struct list_head resource_list;
1184 unsigned int crs_count = 0;
1185
1186 if (!acpi_can_fallback_to_crs(adev, con_id))
1187 return count;
1188
1189 INIT_LIST_HEAD(&resource_list);
1190 acpi_dev_get_resources(adev, &resource_list,
1191 acpi_find_gpio_count, &crs_count);
1192 acpi_dev_free_resource_list(&resource_list);
1193 if (crs_count > 0)
1194 count = crs_count;
1195 }
1196 return count ? count : -ENOENT;
1197}
1198
1199bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
1200{
1201
1202 if (acpi_dev_has_props(adev) || adev->driver_gpios)
1203 return false;
1204
1205 return con_id == NULL;
1206}
1207
1208
1209static int acpi_gpio_initial_sync(void)
1210{
1211 struct acpi_gpio_event *event, *ep;
1212
1213 mutex_lock(&acpi_gpio_initial_sync_list_lock);
1214 list_for_each_entry_safe(event, ep, &acpi_gpio_initial_sync_list,
1215 initial_sync_list) {
1216 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
1217 list_del_init(&event->initial_sync_list);
1218 }
1219 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
1220
1221 return 0;
1222}
1223
1224late_initcall_sync(acpi_gpio_initial_sync);
1225