1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/acpi.h>
4 #include <linux/array_size.h>
5 #include <linux/bitmap.h>
6 #include <linux/cleanup.h>
7 #include <linux/compat.h>
8 #include <linux/debugfs.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/file.h>
13 #include <linux/fs.h>
14 #include <linux/idr.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/irqdesc.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/lockdep.h>
21 #include <linux/module.h>
22 #include <linux/nospec.h>
23 #include <linux/of.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/srcu.h>
29 #include <linux/string.h>
30
31 #include <linux/gpio.h>
32 #include <linux/gpio/driver.h>
33 #include <linux/gpio/machine.h>
34
35 #include <uapi/linux/gpio.h>
36
37 #include "gpiolib-acpi.h"
38 #include "gpiolib-cdev.h"
39 #include "gpiolib-of.h"
40 #include "gpiolib-swnode.h"
41 #include "gpiolib-sysfs.h"
42 #include "gpiolib.h"
43
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/gpio.h>
46
47 /* Implementation infrastructure for GPIO interfaces.
48 *
49 * The GPIO programming interface allows for inlining speed-critical
50 * get/set operations for common cases, so that access to SOC-integrated
51 * GPIOs can sometimes cost only an instruction or two per bit.
52 */
53
54 /* Device and char device-related information */
55 static DEFINE_IDA(gpio_ida);
56 static dev_t gpio_devt;
57 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
58
gpio_bus_match(struct device * dev,const struct device_driver * drv)59 static int gpio_bus_match(struct device *dev, const struct device_driver *drv)
60 {
61 struct fwnode_handle *fwnode = dev_fwnode(dev);
62
63 /*
64 * Only match if the fwnode doesn't already have a proper struct device
65 * created for it.
66 */
67 if (fwnode && fwnode->dev != dev)
68 return 0;
69 return 1;
70 }
71
72 static const struct bus_type gpio_bus_type = {
73 .name = "gpio",
74 .match = gpio_bus_match,
75 };
76
77 /*
78 * Number of GPIOs to use for the fast path in set array
79 */
80 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
81
82 static DEFINE_MUTEX(gpio_lookup_lock);
83 static LIST_HEAD(gpio_lookup_list);
84
85 static LIST_HEAD(gpio_devices);
86 /* Protects the GPIO device list against concurrent modifications. */
87 static DEFINE_MUTEX(gpio_devices_lock);
88 /* Ensures coherence during read-only accesses to the list of GPIO devices. */
89 DEFINE_STATIC_SRCU(gpio_devices_srcu);
90
91 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
92 static LIST_HEAD(gpio_machine_hogs);
93
94 const char *const gpio_suffixes[] = { "gpios", "gpio", NULL };
95
96 static void gpiochip_free_hogs(struct gpio_chip *gc);
97 static int gpiochip_add_irqchip(struct gpio_chip *gc,
98 struct lock_class_key *lock_key,
99 struct lock_class_key *request_key);
100 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
101 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
102 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
103 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
104
105 static bool gpiolib_initialized;
106
gpiod_get_label(struct gpio_desc * desc)107 const char *gpiod_get_label(struct gpio_desc *desc)
108 {
109 struct gpio_desc_label *label;
110 unsigned long flags;
111
112 flags = READ_ONCE(desc->flags);
113
114 label = srcu_dereference_check(desc->label, &desc->gdev->desc_srcu,
115 srcu_read_lock_held(&desc->gdev->desc_srcu));
116
117 if (test_bit(FLAG_USED_AS_IRQ, &flags))
118 return label ? label->str : "interrupt";
119
120 if (!test_bit(FLAG_REQUESTED, &flags))
121 return NULL;
122
123 return label ? label->str : NULL;
124 }
125
desc_free_label(struct rcu_head * rh)126 static void desc_free_label(struct rcu_head *rh)
127 {
128 kfree(container_of(rh, struct gpio_desc_label, rh));
129 }
130
desc_set_label(struct gpio_desc * desc,const char * label)131 static int desc_set_label(struct gpio_desc *desc, const char *label)
132 {
133 struct gpio_desc_label *new = NULL, *old;
134
135 if (label) {
136 new = kzalloc(struct_size(new, str, strlen(label) + 1),
137 GFP_KERNEL);
138 if (!new)
139 return -ENOMEM;
140
141 strcpy(new->str, label);
142 }
143
144 old = rcu_replace_pointer(desc->label, new, 1);
145 if (old)
146 call_srcu(&desc->gdev->desc_srcu, &old->rh, desc_free_label);
147
148 return 0;
149 }
150
151 /**
152 * gpio_to_desc - Convert a GPIO number to its descriptor
153 * @gpio: global GPIO number
154 *
155 * Returns:
156 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
157 * with the given number exists in the system.
158 */
gpio_to_desc(unsigned gpio)159 struct gpio_desc *gpio_to_desc(unsigned gpio)
160 {
161 struct gpio_device *gdev;
162
163 scoped_guard(srcu, &gpio_devices_srcu) {
164 list_for_each_entry_srcu(gdev, &gpio_devices, list,
165 srcu_read_lock_held(&gpio_devices_srcu)) {
166 if (gdev->base <= gpio &&
167 gdev->base + gdev->ngpio > gpio)
168 return &gdev->descs[gpio - gdev->base];
169 }
170 }
171
172 return NULL;
173 }
174 EXPORT_SYMBOL_GPL(gpio_to_desc);
175
176 /* This function is deprecated and will be removed soon, don't use. */
gpiochip_get_desc(struct gpio_chip * gc,unsigned int hwnum)177 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
178 unsigned int hwnum)
179 {
180 return gpio_device_get_desc(gc->gpiodev, hwnum);
181 }
182
183 /**
184 * gpio_device_get_desc() - get the GPIO descriptor corresponding to the given
185 * hardware number for this GPIO device
186 * @gdev: GPIO device to get the descriptor from
187 * @hwnum: hardware number of the GPIO for this chip
188 *
189 * Returns:
190 * A pointer to the GPIO descriptor or %EINVAL if no GPIO exists in the given
191 * chip for the specified hardware number or %ENODEV if the underlying chip
192 * already vanished.
193 *
194 * The reference count of struct gpio_device is *NOT* increased like when the
195 * GPIO is being requested for exclusive usage. It's up to the caller to make
196 * sure the GPIO device will stay alive together with the descriptor returned
197 * by this function.
198 */
199 struct gpio_desc *
gpio_device_get_desc(struct gpio_device * gdev,unsigned int hwnum)200 gpio_device_get_desc(struct gpio_device *gdev, unsigned int hwnum)
201 {
202 if (hwnum >= gdev->ngpio)
203 return ERR_PTR(-EINVAL);
204
205 return &gdev->descs[array_index_nospec(hwnum, gdev->ngpio)];
206 }
207 EXPORT_SYMBOL_GPL(gpio_device_get_desc);
208
209 /**
210 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
211 * @desc: GPIO descriptor
212 *
213 * This should disappear in the future but is needed since we still
214 * use GPIO numbers for error messages and sysfs nodes.
215 *
216 * Returns:
217 * The global GPIO number for the GPIO specified by its descriptor.
218 */
desc_to_gpio(const struct gpio_desc * desc)219 int desc_to_gpio(const struct gpio_desc *desc)
220 {
221 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
222 }
223 EXPORT_SYMBOL_GPL(desc_to_gpio);
224
225
226 /**
227 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
228 * @desc: descriptor to return the chip of
229 *
230 * *DEPRECATED*
231 * This function is unsafe and should not be used. Using the chip address
232 * without taking the SRCU read lock may result in dereferencing a dangling
233 * pointer.
234 *
235 * Returns:
236 * Address of the GPIO chip backing this device.
237 */
gpiod_to_chip(const struct gpio_desc * desc)238 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
239 {
240 if (!desc)
241 return NULL;
242
243 return gpio_device_get_chip(desc->gdev);
244 }
245 EXPORT_SYMBOL_GPL(gpiod_to_chip);
246
247 /**
248 * gpiod_to_gpio_device() - Return the GPIO device to which this descriptor
249 * belongs.
250 * @desc: Descriptor for which to return the GPIO device.
251 *
252 * This *DOES NOT* increase the reference count of the GPIO device as it's
253 * expected that the descriptor is requested and the users already holds a
254 * reference to the device.
255 *
256 * Returns:
257 * Address of the GPIO device owning this descriptor.
258 */
gpiod_to_gpio_device(struct gpio_desc * desc)259 struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
260 {
261 if (!desc)
262 return NULL;
263
264 return desc->gdev;
265 }
266 EXPORT_SYMBOL_GPL(gpiod_to_gpio_device);
267
268 /**
269 * gpio_device_get_base() - Get the base GPIO number allocated by this device
270 * @gdev: GPIO device
271 *
272 * Returns:
273 * First GPIO number in the global GPIO numberspace for this device.
274 */
gpio_device_get_base(struct gpio_device * gdev)275 int gpio_device_get_base(struct gpio_device *gdev)
276 {
277 return gdev->base;
278 }
279 EXPORT_SYMBOL_GPL(gpio_device_get_base);
280
281 /**
282 * gpio_device_get_label() - Get the label of this GPIO device
283 * @gdev: GPIO device
284 *
285 * Returns:
286 * Pointer to the string containing the GPIO device label. The string's
287 * lifetime is tied to that of the underlying GPIO device.
288 */
gpio_device_get_label(struct gpio_device * gdev)289 const char *gpio_device_get_label(struct gpio_device *gdev)
290 {
291 return gdev->label;
292 }
293 EXPORT_SYMBOL(gpio_device_get_label);
294
295 /**
296 * gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device
297 * @gdev: GPIO device
298 *
299 * Returns:
300 * Address of the GPIO chip backing this device.
301 *
302 * *DEPRECATED*
303 * Until we can get rid of all non-driver users of struct gpio_chip, we must
304 * provide a way of retrieving the pointer to it from struct gpio_device. This
305 * is *NOT* safe as the GPIO API is considered to be hot-unpluggable and the
306 * chip can dissapear at any moment (unlike reference-counted struct
307 * gpio_device).
308 *
309 * Use at your own risk.
310 */
gpio_device_get_chip(struct gpio_device * gdev)311 struct gpio_chip *gpio_device_get_chip(struct gpio_device *gdev)
312 {
313 return rcu_dereference_check(gdev->chip, 1);
314 }
315 EXPORT_SYMBOL_GPL(gpio_device_get_chip);
316
317 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
gpiochip_find_base_unlocked(u16 ngpio)318 static int gpiochip_find_base_unlocked(u16 ngpio)
319 {
320 unsigned int base = GPIO_DYNAMIC_BASE;
321 struct gpio_device *gdev;
322
323 list_for_each_entry_srcu(gdev, &gpio_devices, list,
324 lockdep_is_held(&gpio_devices_lock)) {
325 /* found a free space? */
326 if (gdev->base >= base + ngpio)
327 break;
328 /* nope, check the space right after the chip */
329 base = gdev->base + gdev->ngpio;
330 if (base < GPIO_DYNAMIC_BASE)
331 base = GPIO_DYNAMIC_BASE;
332 if (base > GPIO_DYNAMIC_MAX - ngpio)
333 break;
334 }
335
336 if (base <= GPIO_DYNAMIC_MAX - ngpio) {
337 pr_debug("%s: found new base at %d\n", __func__, base);
338 return base;
339 } else {
340 pr_err("%s: cannot find free range\n", __func__);
341 return -ENOSPC;
342 }
343 }
344
345 /**
346 * gpiod_get_direction - return the current direction of a GPIO
347 * @desc: GPIO to get the direction of
348 *
349 * Returns:
350 * 0 for output, 1 for input, or an error code in case of error.
351 *
352 * This function may sleep if gpiod_cansleep() is true.
353 */
gpiod_get_direction(struct gpio_desc * desc)354 int gpiod_get_direction(struct gpio_desc *desc)
355 {
356 unsigned long flags;
357 unsigned int offset;
358 int ret;
359
360 /*
361 * We cannot use VALIDATE_DESC() as we must not return 0 for a NULL
362 * descriptor like we usually do.
363 */
364 if (IS_ERR_OR_NULL(desc))
365 return -EINVAL;
366
367 CLASS(gpio_chip_guard, guard)(desc);
368 if (!guard.gc)
369 return -ENODEV;
370
371 offset = gpio_chip_hwgpio(desc);
372 flags = READ_ONCE(desc->flags);
373
374 /*
375 * Open drain emulation using input mode may incorrectly report
376 * input here, fix that up.
377 */
378 if (test_bit(FLAG_OPEN_DRAIN, &flags) &&
379 test_bit(FLAG_IS_OUT, &flags))
380 return 0;
381
382 if (!guard.gc->get_direction)
383 return -ENOTSUPP;
384
385 ret = guard.gc->get_direction(guard.gc, offset);
386 if (ret < 0)
387 return ret;
388
389 /*
390 * GPIO_LINE_DIRECTION_IN or other positive,
391 * otherwise GPIO_LINE_DIRECTION_OUT.
392 */
393 if (ret > 0)
394 ret = 1;
395
396 assign_bit(FLAG_IS_OUT, &flags, !ret);
397 WRITE_ONCE(desc->flags, flags);
398
399 return ret;
400 }
401 EXPORT_SYMBOL_GPL(gpiod_get_direction);
402
403 /*
404 * Add a new chip to the global chips list, keeping the list of chips sorted
405 * by range(means [base, base + ngpio - 1]) order.
406 *
407 * Returns:
408 * -EBUSY if the new chip overlaps with some other chip's integer space.
409 */
gpiodev_add_to_list_unlocked(struct gpio_device * gdev)410 static int gpiodev_add_to_list_unlocked(struct gpio_device *gdev)
411 {
412 struct gpio_device *prev, *next;
413
414 lockdep_assert_held(&gpio_devices_lock);
415
416 if (list_empty(&gpio_devices)) {
417 /* initial entry in list */
418 list_add_tail_rcu(&gdev->list, &gpio_devices);
419 return 0;
420 }
421
422 next = list_first_entry(&gpio_devices, struct gpio_device, list);
423 if (gdev->base + gdev->ngpio <= next->base) {
424 /* add before first entry */
425 list_add_rcu(&gdev->list, &gpio_devices);
426 return 0;
427 }
428
429 prev = list_last_entry(&gpio_devices, struct gpio_device, list);
430 if (prev->base + prev->ngpio <= gdev->base) {
431 /* add behind last entry */
432 list_add_tail_rcu(&gdev->list, &gpio_devices);
433 return 0;
434 }
435
436 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
437 /* at the end of the list */
438 if (&next->list == &gpio_devices)
439 break;
440
441 /* add between prev and next */
442 if (prev->base + prev->ngpio <= gdev->base
443 && gdev->base + gdev->ngpio <= next->base) {
444 list_add_rcu(&gdev->list, &prev->list);
445 return 0;
446 }
447 }
448
449 synchronize_srcu(&gpio_devices_srcu);
450
451 return -EBUSY;
452 }
453
454 /*
455 * Convert a GPIO name to its descriptor
456 * Note that there is no guarantee that GPIO names are globally unique!
457 * Hence this function will return, if it exists, a reference to the first GPIO
458 * line found that matches the given name.
459 */
gpio_name_to_desc(const char * const name)460 static struct gpio_desc *gpio_name_to_desc(const char * const name)
461 {
462 struct gpio_device *gdev;
463 struct gpio_desc *desc;
464 struct gpio_chip *gc;
465
466 if (!name)
467 return NULL;
468
469 guard(srcu)(&gpio_devices_srcu);
470
471 list_for_each_entry_srcu(gdev, &gpio_devices, list,
472 srcu_read_lock_held(&gpio_devices_srcu)) {
473 guard(srcu)(&gdev->srcu);
474
475 gc = srcu_dereference(gdev->chip, &gdev->srcu);
476 if (!gc)
477 continue;
478
479 for_each_gpio_desc(gc, desc) {
480 if (desc->name && !strcmp(desc->name, name))
481 return desc;
482 }
483 }
484
485 return NULL;
486 }
487
488 /*
489 * Take the names from gc->names and assign them to their GPIO descriptors.
490 * Warn if a name is already used for a GPIO line on a different GPIO chip.
491 *
492 * Note that:
493 * 1. Non-unique names are still accepted,
494 * 2. Name collisions within the same GPIO chip are not reported.
495 */
gpiochip_set_desc_names(struct gpio_chip * gc)496 static void gpiochip_set_desc_names(struct gpio_chip *gc)
497 {
498 struct gpio_device *gdev = gc->gpiodev;
499 int i;
500
501 /* First check all names if they are unique */
502 for (i = 0; i != gc->ngpio; ++i) {
503 struct gpio_desc *gpio;
504
505 gpio = gpio_name_to_desc(gc->names[i]);
506 if (gpio)
507 dev_warn(&gdev->dev,
508 "Detected name collision for GPIO name '%s'\n",
509 gc->names[i]);
510 }
511
512 /* Then add all names to the GPIO descriptors */
513 for (i = 0; i != gc->ngpio; ++i)
514 gdev->descs[i].name = gc->names[i];
515 }
516
517 /*
518 * gpiochip_set_names - Set GPIO line names using device properties
519 * @chip: GPIO chip whose lines should be named, if possible
520 *
521 * Looks for device property "gpio-line-names" and if it exists assigns
522 * GPIO line names for the chip. The memory allocated for the assigned
523 * names belong to the underlying firmware node and should not be released
524 * by the caller.
525 */
gpiochip_set_names(struct gpio_chip * chip)526 static int gpiochip_set_names(struct gpio_chip *chip)
527 {
528 struct gpio_device *gdev = chip->gpiodev;
529 struct device *dev = &gdev->dev;
530 const char **names;
531 int ret, i;
532 int count;
533
534 count = device_property_string_array_count(dev, "gpio-line-names");
535 if (count < 0)
536 return 0;
537
538 /*
539 * When offset is set in the driver side we assume the driver internally
540 * is using more than one gpiochip per the same device. We have to stop
541 * setting friendly names if the specified ones with 'gpio-line-names'
542 * are less than the offset in the device itself. This means all the
543 * lines are not present for every single pin within all the internal
544 * gpiochips.
545 */
546 if (count <= chip->offset) {
547 dev_warn(dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
548 count, chip->offset);
549 return 0;
550 }
551
552 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
553 if (!names)
554 return -ENOMEM;
555
556 ret = device_property_read_string_array(dev, "gpio-line-names",
557 names, count);
558 if (ret < 0) {
559 dev_warn(dev, "failed to read GPIO line names\n");
560 kfree(names);
561 return ret;
562 }
563
564 /*
565 * When more that one gpiochip per device is used, 'count' can
566 * contain at most number gpiochips x chip->ngpio. We have to
567 * correctly distribute all defined lines taking into account
568 * chip->offset as starting point from where we will assign
569 * the names to pins from the 'names' array. Since property
570 * 'gpio-line-names' cannot contains gaps, we have to be sure
571 * we only assign those pins that really exists since chip->ngpio
572 * can be different of the chip->offset.
573 */
574 count = (count > chip->offset) ? count - chip->offset : count;
575 if (count > chip->ngpio)
576 count = chip->ngpio;
577
578 for (i = 0; i < count; i++) {
579 /*
580 * Allow overriding "fixed" names provided by the GPIO
581 * provider. The "fixed" names are more often than not
582 * generic and less informative than the names given in
583 * device properties.
584 */
585 if (names[chip->offset + i] && names[chip->offset + i][0])
586 gdev->descs[i].name = names[chip->offset + i];
587 }
588
589 kfree(names);
590
591 return 0;
592 }
593
gpiochip_allocate_mask(struct gpio_chip * gc)594 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
595 {
596 unsigned long *p;
597
598 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
599 if (!p)
600 return NULL;
601
602 /* Assume by default all GPIOs are valid */
603 bitmap_fill(p, gc->ngpio);
604
605 return p;
606 }
607
gpiochip_free_mask(unsigned long ** p)608 static void gpiochip_free_mask(unsigned long **p)
609 {
610 bitmap_free(*p);
611 *p = NULL;
612 }
613
gpiochip_count_reserved_ranges(struct gpio_chip * gc)614 static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
615 {
616 struct device *dev = &gc->gpiodev->dev;
617 int size;
618
619 /* Format is "start, count, ..." */
620 size = device_property_count_u32(dev, "gpio-reserved-ranges");
621 if (size > 0 && size % 2 == 0)
622 return size;
623
624 return 0;
625 }
626
gpiochip_apply_reserved_ranges(struct gpio_chip * gc)627 static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc)
628 {
629 struct device *dev = &gc->gpiodev->dev;
630 unsigned int size;
631 u32 *ranges;
632 int ret;
633
634 size = gpiochip_count_reserved_ranges(gc);
635 if (size == 0)
636 return 0;
637
638 ranges = kmalloc_array(size, sizeof(*ranges), GFP_KERNEL);
639 if (!ranges)
640 return -ENOMEM;
641
642 ret = device_property_read_u32_array(dev, "gpio-reserved-ranges",
643 ranges, size);
644 if (ret) {
645 kfree(ranges);
646 return ret;
647 }
648
649 while (size) {
650 u32 count = ranges[--size];
651 u32 start = ranges[--size];
652
653 if (start >= gc->ngpio || start + count > gc->ngpio)
654 continue;
655
656 bitmap_clear(gc->valid_mask, start, count);
657 }
658
659 kfree(ranges);
660 return 0;
661 }
662
gpiochip_init_valid_mask(struct gpio_chip * gc)663 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
664 {
665 int ret;
666
667 if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
668 return 0;
669
670 gc->valid_mask = gpiochip_allocate_mask(gc);
671 if (!gc->valid_mask)
672 return -ENOMEM;
673
674 ret = gpiochip_apply_reserved_ranges(gc);
675 if (ret)
676 return ret;
677
678 if (gc->init_valid_mask)
679 return gc->init_valid_mask(gc,
680 gc->valid_mask,
681 gc->ngpio);
682
683 return 0;
684 }
685
gpiochip_free_valid_mask(struct gpio_chip * gc)686 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
687 {
688 gpiochip_free_mask(&gc->valid_mask);
689 }
690
gpiochip_add_pin_ranges(struct gpio_chip * gc)691 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
692 {
693 /*
694 * Device Tree platforms are supposed to use "gpio-ranges"
695 * property. This check ensures that the ->add_pin_ranges()
696 * won't be called for them.
697 */
698 if (device_property_present(&gc->gpiodev->dev, "gpio-ranges"))
699 return 0;
700
701 if (gc->add_pin_ranges)
702 return gc->add_pin_ranges(gc);
703
704 return 0;
705 }
706
gpiochip_line_is_valid(const struct gpio_chip * gc,unsigned int offset)707 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
708 unsigned int offset)
709 {
710 /* No mask means all valid */
711 if (likely(!gc->valid_mask))
712 return true;
713 return test_bit(offset, gc->valid_mask);
714 }
715 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
716
gpiod_free_irqs(struct gpio_desc * desc)717 static void gpiod_free_irqs(struct gpio_desc *desc)
718 {
719 int irq = gpiod_to_irq(desc);
720 struct irq_desc *irqd = irq_to_desc(irq);
721 void *cookie;
722
723 for (;;) {
724 /*
725 * Make sure the action doesn't go away while we're
726 * dereferencing it. Retrieve and store the cookie value.
727 * If the irq is freed after we release the lock, that's
728 * alright - the underlying maple tree lookup will return NULL
729 * and nothing will happen in free_irq().
730 */
731 scoped_guard(mutex, &irqd->request_mutex) {
732 if (!irq_desc_has_action(irqd))
733 return;
734
735 cookie = irqd->action->dev_id;
736 }
737
738 free_irq(irq, cookie);
739 }
740 }
741
742 /*
743 * The chip is going away but there may be users who had requested interrupts
744 * on its GPIO lines who have no idea about its removal and have no way of
745 * being notified about it. We need to free any interrupts still in use here or
746 * we'll leak memory and resources (like procfs files).
747 */
gpiochip_free_remaining_irqs(struct gpio_chip * gc)748 static void gpiochip_free_remaining_irqs(struct gpio_chip *gc)
749 {
750 struct gpio_desc *desc;
751
752 for_each_gpio_desc_with_flag(gc, desc, FLAG_USED_AS_IRQ)
753 gpiod_free_irqs(desc);
754 }
755
gpiodev_release(struct device * dev)756 static void gpiodev_release(struct device *dev)
757 {
758 struct gpio_device *gdev = to_gpio_device(dev);
759
760 /* Call pending kfree()s for descriptor labels. */
761 synchronize_srcu(&gdev->desc_srcu);
762 cleanup_srcu_struct(&gdev->desc_srcu);
763
764 ida_free(&gpio_ida, gdev->id);
765 kfree_const(gdev->label);
766 kfree(gdev->descs);
767 cleanup_srcu_struct(&gdev->srcu);
768 kfree(gdev);
769 }
770
771 static const struct device_type gpio_dev_type = {
772 .name = "gpio_chip",
773 .release = gpiodev_release,
774 };
775
776 #ifdef CONFIG_GPIO_CDEV
777 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
778 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
779 #else
780 /*
781 * gpiolib_cdev_register() indirectly calls device_add(), which is still
782 * required even when cdev is not selected.
783 */
784 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
785 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
786 #endif
787
gpiochip_setup_dev(struct gpio_device * gdev)788 static int gpiochip_setup_dev(struct gpio_device *gdev)
789 {
790 struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
791 int ret;
792
793 device_initialize(&gdev->dev);
794
795 /*
796 * If fwnode doesn't belong to another device, it's safe to clear its
797 * initialized flag.
798 */
799 if (fwnode && !fwnode->dev)
800 fwnode_dev_initialized(fwnode, false);
801
802 ret = gcdev_register(gdev, gpio_devt);
803 if (ret)
804 return ret;
805
806 ret = gpiochip_sysfs_register(gdev);
807 if (ret)
808 goto err_remove_device;
809
810 dev_dbg(&gdev->dev, "registered GPIOs %u to %u on %s\n", gdev->base,
811 gdev->base + gdev->ngpio - 1, gdev->label);
812
813 return 0;
814
815 err_remove_device:
816 gcdev_unregister(gdev);
817 return ret;
818 }
819
gpiochip_machine_hog(struct gpio_chip * gc,struct gpiod_hog * hog)820 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
821 {
822 struct gpio_desc *desc;
823 int rv;
824
825 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
826 if (IS_ERR(desc)) {
827 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
828 PTR_ERR(desc));
829 return;
830 }
831
832 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
833 if (rv)
834 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
835 __func__, gc->label, hog->chip_hwnum, rv);
836 }
837
machine_gpiochip_add(struct gpio_chip * gc)838 static void machine_gpiochip_add(struct gpio_chip *gc)
839 {
840 struct gpiod_hog *hog;
841
842 mutex_lock(&gpio_machine_hogs_mutex);
843
844 list_for_each_entry(hog, &gpio_machine_hogs, list) {
845 if (!strcmp(gc->label, hog->chip_label))
846 gpiochip_machine_hog(gc, hog);
847 }
848
849 mutex_unlock(&gpio_machine_hogs_mutex);
850 }
851
gpiochip_setup_devs(void)852 static void gpiochip_setup_devs(void)
853 {
854 struct gpio_device *gdev;
855 int ret;
856
857 guard(srcu)(&gpio_devices_srcu);
858
859 list_for_each_entry_srcu(gdev, &gpio_devices, list,
860 srcu_read_lock_held(&gpio_devices_srcu)) {
861 ret = gpiochip_setup_dev(gdev);
862 if (ret)
863 dev_err(&gdev->dev,
864 "Failed to initialize gpio device (%d)\n", ret);
865 }
866 }
867
gpiochip_set_data(struct gpio_chip * gc,void * data)868 static void gpiochip_set_data(struct gpio_chip *gc, void *data)
869 {
870 gc->gpiodev->data = data;
871 }
872
873 /**
874 * gpiochip_get_data() - get per-subdriver data for the chip
875 * @gc: GPIO chip
876 *
877 * Returns:
878 * The per-subdriver data for the chip.
879 */
gpiochip_get_data(struct gpio_chip * gc)880 void *gpiochip_get_data(struct gpio_chip *gc)
881 {
882 return gc->gpiodev->data;
883 }
884 EXPORT_SYMBOL_GPL(gpiochip_get_data);
885
gpiochip_get_ngpios(struct gpio_chip * gc,struct device * dev)886 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev)
887 {
888 u32 ngpios = gc->ngpio;
889 int ret;
890
891 if (ngpios == 0) {
892 ret = device_property_read_u32(dev, "ngpios", &ngpios);
893 if (ret == -ENODATA)
894 /*
895 * -ENODATA means that there is no property found and
896 * we want to issue the error message to the user.
897 * Besides that, we want to return different error code
898 * to state that supplied value is not valid.
899 */
900 ngpios = 0;
901 else if (ret)
902 return ret;
903
904 gc->ngpio = ngpios;
905 }
906
907 if (gc->ngpio == 0) {
908 dev_err(dev, "tried to insert a GPIO chip with zero lines\n");
909 return -EINVAL;
910 }
911
912 if (gc->ngpio > FASTPATH_NGPIO)
913 dev_warn(dev, "line cnt %u is greater than fast path cnt %u\n",
914 gc->ngpio, FASTPATH_NGPIO);
915
916 return 0;
917 }
918 EXPORT_SYMBOL_GPL(gpiochip_get_ngpios);
919
gpiochip_add_data_with_key(struct gpio_chip * gc,void * data,struct lock_class_key * lock_key,struct lock_class_key * request_key)920 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
921 struct lock_class_key *lock_key,
922 struct lock_class_key *request_key)
923 {
924 struct gpio_device *gdev;
925 unsigned int desc_index;
926 int base = 0;
927 int ret = 0;
928
929 /*
930 * First: allocate and populate the internal stat container, and
931 * set up the struct device.
932 */
933 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
934 if (!gdev)
935 return -ENOMEM;
936
937 gdev->dev.type = &gpio_dev_type;
938 gdev->dev.bus = &gpio_bus_type;
939 gdev->dev.parent = gc->parent;
940 rcu_assign_pointer(gdev->chip, gc);
941
942 gc->gpiodev = gdev;
943 gpiochip_set_data(gc, data);
944
945 /*
946 * If the calling driver did not initialize firmware node,
947 * do it here using the parent device, if any.
948 */
949 if (gc->fwnode)
950 device_set_node(&gdev->dev, gc->fwnode);
951 else if (gc->parent)
952 device_set_node(&gdev->dev, dev_fwnode(gc->parent));
953
954 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
955 if (gdev->id < 0) {
956 ret = gdev->id;
957 goto err_free_gdev;
958 }
959
960 ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
961 if (ret)
962 goto err_free_ida;
963
964 if (gc->parent && gc->parent->driver)
965 gdev->owner = gc->parent->driver->owner;
966 else if (gc->owner)
967 /* TODO: remove chip->owner */
968 gdev->owner = gc->owner;
969 else
970 gdev->owner = THIS_MODULE;
971
972 ret = gpiochip_get_ngpios(gc, &gdev->dev);
973 if (ret)
974 goto err_free_dev_name;
975
976 gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
977 if (!gdev->descs) {
978 ret = -ENOMEM;
979 goto err_free_dev_name;
980 }
981
982 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
983 if (!gdev->label) {
984 ret = -ENOMEM;
985 goto err_free_descs;
986 }
987
988 gdev->ngpio = gc->ngpio;
989 gdev->can_sleep = gc->can_sleep;
990
991 scoped_guard(mutex, &gpio_devices_lock) {
992 /*
993 * TODO: this allocates a Linux GPIO number base in the global
994 * GPIO numberspace for this chip. In the long run we want to
995 * get *rid* of this numberspace and use only descriptors, but
996 * it may be a pipe dream. It will not happen before we get rid
997 * of the sysfs interface anyways.
998 */
999 base = gc->base;
1000 if (base < 0) {
1001 base = gpiochip_find_base_unlocked(gc->ngpio);
1002 if (base < 0) {
1003 ret = base;
1004 base = 0;
1005 goto err_free_label;
1006 }
1007
1008 /*
1009 * TODO: it should not be necessary to reflect the
1010 * assigned base outside of the GPIO subsystem. Go over
1011 * drivers and see if anyone makes use of this, else
1012 * drop this and assign a poison instead.
1013 */
1014 gc->base = base;
1015 } else {
1016 dev_warn(&gdev->dev,
1017 "Static allocation of GPIO base is deprecated, use dynamic allocation.\n");
1018 }
1019
1020 gdev->base = base;
1021
1022 ret = gpiodev_add_to_list_unlocked(gdev);
1023 if (ret) {
1024 chip_err(gc, "GPIO integer space overlap, cannot add chip\n");
1025 goto err_free_label;
1026 }
1027 }
1028
1029 for (desc_index = 0; desc_index < gc->ngpio; desc_index++)
1030 gdev->descs[desc_index].gdev = gdev;
1031
1032 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier);
1033 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier);
1034
1035 ret = init_srcu_struct(&gdev->srcu);
1036 if (ret)
1037 goto err_remove_from_list;
1038
1039 ret = init_srcu_struct(&gdev->desc_srcu);
1040 if (ret)
1041 goto err_cleanup_gdev_srcu;
1042
1043 #ifdef CONFIG_PINCTRL
1044 INIT_LIST_HEAD(&gdev->pin_ranges);
1045 #endif
1046
1047 if (gc->names)
1048 gpiochip_set_desc_names(gc);
1049
1050 ret = gpiochip_set_names(gc);
1051 if (ret)
1052 goto err_cleanup_desc_srcu;
1053
1054 ret = gpiochip_init_valid_mask(gc);
1055 if (ret)
1056 goto err_cleanup_desc_srcu;
1057
1058 for (desc_index = 0; desc_index < gc->ngpio; desc_index++) {
1059 struct gpio_desc *desc = &gdev->descs[desc_index];
1060
1061 if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) {
1062 assign_bit(FLAG_IS_OUT,
1063 &desc->flags, !gc->get_direction(gc, desc_index));
1064 } else {
1065 assign_bit(FLAG_IS_OUT,
1066 &desc->flags, !gc->direction_input);
1067 }
1068 }
1069
1070 ret = of_gpiochip_add(gc);
1071 if (ret)
1072 goto err_free_valid_mask;
1073
1074 ret = gpiochip_add_pin_ranges(gc);
1075 if (ret)
1076 goto err_remove_of_chip;
1077
1078 acpi_gpiochip_add(gc);
1079
1080 machine_gpiochip_add(gc);
1081
1082 ret = gpiochip_irqchip_init_valid_mask(gc);
1083 if (ret)
1084 goto err_free_hogs;
1085
1086 ret = gpiochip_irqchip_init_hw(gc);
1087 if (ret)
1088 goto err_remove_irqchip_mask;
1089
1090 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
1091 if (ret)
1092 goto err_remove_irqchip_mask;
1093
1094 /*
1095 * By first adding the chardev, and then adding the device,
1096 * we get a device node entry in sysfs under
1097 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1098 * coldplug of device nodes and other udev business.
1099 * We can do this only if gpiolib has been initialized.
1100 * Otherwise, defer until later.
1101 */
1102 if (gpiolib_initialized) {
1103 ret = gpiochip_setup_dev(gdev);
1104 if (ret)
1105 goto err_remove_irqchip;
1106 }
1107 return 0;
1108
1109 err_remove_irqchip:
1110 gpiochip_irqchip_remove(gc);
1111 err_remove_irqchip_mask:
1112 gpiochip_irqchip_free_valid_mask(gc);
1113 err_free_hogs:
1114 gpiochip_free_hogs(gc);
1115 acpi_gpiochip_remove(gc);
1116 gpiochip_remove_pin_ranges(gc);
1117 err_remove_of_chip:
1118 of_gpiochip_remove(gc);
1119 err_free_valid_mask:
1120 gpiochip_free_valid_mask(gc);
1121 err_cleanup_desc_srcu:
1122 cleanup_srcu_struct(&gdev->desc_srcu);
1123 err_cleanup_gdev_srcu:
1124 cleanup_srcu_struct(&gdev->srcu);
1125 err_remove_from_list:
1126 scoped_guard(mutex, &gpio_devices_lock)
1127 list_del_rcu(&gdev->list);
1128 synchronize_srcu(&gpio_devices_srcu);
1129 if (gdev->dev.release) {
1130 /* release() has been registered by gpiochip_setup_dev() */
1131 gpio_device_put(gdev);
1132 goto err_print_message;
1133 }
1134 err_free_label:
1135 kfree_const(gdev->label);
1136 err_free_descs:
1137 kfree(gdev->descs);
1138 err_free_dev_name:
1139 kfree(dev_name(&gdev->dev));
1140 err_free_ida:
1141 ida_free(&gpio_ida, gdev->id);
1142 err_free_gdev:
1143 kfree(gdev);
1144 err_print_message:
1145 /* failures here can mean systems won't boot... */
1146 if (ret != -EPROBE_DEFER) {
1147 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
1148 base, base + (int)gc->ngpio - 1,
1149 gc->label ? : "generic", ret);
1150 }
1151 return ret;
1152 }
1153 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1154
1155 /**
1156 * gpiochip_remove() - unregister a gpio_chip
1157 * @gc: the chip to unregister
1158 *
1159 * A gpio_chip with any GPIOs still requested may not be removed.
1160 */
gpiochip_remove(struct gpio_chip * gc)1161 void gpiochip_remove(struct gpio_chip *gc)
1162 {
1163 struct gpio_device *gdev = gc->gpiodev;
1164
1165 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1166 gpiochip_sysfs_unregister(gdev);
1167 gpiochip_free_hogs(gc);
1168 gpiochip_free_remaining_irqs(gc);
1169
1170 scoped_guard(mutex, &gpio_devices_lock)
1171 list_del_rcu(&gdev->list);
1172 synchronize_srcu(&gpio_devices_srcu);
1173
1174 /* Numb the device, cancelling all outstanding operations */
1175 rcu_assign_pointer(gdev->chip, NULL);
1176 synchronize_srcu(&gdev->srcu);
1177 gpiochip_irqchip_remove(gc);
1178 acpi_gpiochip_remove(gc);
1179 of_gpiochip_remove(gc);
1180 gpiochip_remove_pin_ranges(gc);
1181 gpiochip_free_valid_mask(gc);
1182 /*
1183 * We accept no more calls into the driver from this point, so
1184 * NULL the driver data pointer.
1185 */
1186 gpiochip_set_data(gc, NULL);
1187
1188 /*
1189 * The gpiochip side puts its use of the device to rest here:
1190 * if there are no userspace clients, the chardev and device will
1191 * be removed, else it will be dangling until the last user is
1192 * gone.
1193 */
1194 gcdev_unregister(gdev);
1195 gpio_device_put(gdev);
1196 }
1197 EXPORT_SYMBOL_GPL(gpiochip_remove);
1198
1199 /**
1200 * gpio_device_find() - find a specific GPIO device
1201 * @data: data to pass to match function
1202 * @match: Callback function to check gpio_chip
1203 *
1204 * Returns:
1205 * New reference to struct gpio_device.
1206 *
1207 * Similar to bus_find_device(). It returns a reference to a gpio_device as
1208 * determined by a user supplied @match callback. The callback should return
1209 * 0 if the device doesn't match and non-zero if it does. If the callback
1210 * returns non-zero, this function will return to the caller and not iterate
1211 * over any more gpio_devices.
1212 *
1213 * The callback takes the GPIO chip structure as argument. During the execution
1214 * of the callback function the chip is protected from being freed. TODO: This
1215 * actually has yet to be implemented.
1216 *
1217 * If the function returns non-NULL, the returned reference must be freed by
1218 * the caller using gpio_device_put().
1219 */
gpio_device_find(const void * data,int (* match)(struct gpio_chip * gc,const void * data))1220 struct gpio_device *gpio_device_find(const void *data,
1221 int (*match)(struct gpio_chip *gc,
1222 const void *data))
1223 {
1224 struct gpio_device *gdev;
1225 struct gpio_chip *gc;
1226
1227 /*
1228 * Not yet but in the future the spinlock below will become a mutex.
1229 * Annotate this function before anyone tries to use it in interrupt
1230 * context like it happened with gpiochip_find().
1231 */
1232 might_sleep();
1233
1234 guard(srcu)(&gpio_devices_srcu);
1235
1236 list_for_each_entry_srcu(gdev, &gpio_devices, list,
1237 srcu_read_lock_held(&gpio_devices_srcu)) {
1238 if (!device_is_registered(&gdev->dev))
1239 continue;
1240
1241 guard(srcu)(&gdev->srcu);
1242
1243 gc = srcu_dereference(gdev->chip, &gdev->srcu);
1244
1245 if (gc && match(gc, data))
1246 return gpio_device_get(gdev);
1247 }
1248
1249 return NULL;
1250 }
1251 EXPORT_SYMBOL_GPL(gpio_device_find);
1252
gpio_chip_match_by_label(struct gpio_chip * gc,const void * label)1253 static int gpio_chip_match_by_label(struct gpio_chip *gc, const void *label)
1254 {
1255 return gc->label && !strcmp(gc->label, label);
1256 }
1257
1258 /**
1259 * gpio_device_find_by_label() - wrapper around gpio_device_find() finding the
1260 * GPIO device by its backing chip's label
1261 * @label: Label to lookup
1262 *
1263 * Returns:
1264 * Reference to the GPIO device or NULL. Reference must be released with
1265 * gpio_device_put().
1266 */
gpio_device_find_by_label(const char * label)1267 struct gpio_device *gpio_device_find_by_label(const char *label)
1268 {
1269 return gpio_device_find((void *)label, gpio_chip_match_by_label);
1270 }
1271 EXPORT_SYMBOL_GPL(gpio_device_find_by_label);
1272
gpio_chip_match_by_fwnode(struct gpio_chip * gc,const void * fwnode)1273 static int gpio_chip_match_by_fwnode(struct gpio_chip *gc, const void *fwnode)
1274 {
1275 return device_match_fwnode(&gc->gpiodev->dev, fwnode);
1276 }
1277
1278 /**
1279 * gpio_device_find_by_fwnode() - wrapper around gpio_device_find() finding
1280 * the GPIO device by its fwnode
1281 * @fwnode: Firmware node to lookup
1282 *
1283 * Returns:
1284 * Reference to the GPIO device or NULL. Reference must be released with
1285 * gpio_device_put().
1286 */
gpio_device_find_by_fwnode(const struct fwnode_handle * fwnode)1287 struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnode)
1288 {
1289 return gpio_device_find((void *)fwnode, gpio_chip_match_by_fwnode);
1290 }
1291 EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode);
1292
1293 /**
1294 * gpio_device_get() - Increase the reference count of this GPIO device
1295 * @gdev: GPIO device to increase the refcount for
1296 *
1297 * Returns:
1298 * Pointer to @gdev.
1299 */
gpio_device_get(struct gpio_device * gdev)1300 struct gpio_device *gpio_device_get(struct gpio_device *gdev)
1301 {
1302 return to_gpio_device(get_device(&gdev->dev));
1303 }
1304 EXPORT_SYMBOL_GPL(gpio_device_get);
1305
1306 /**
1307 * gpio_device_put() - Decrease the reference count of this GPIO device and
1308 * possibly free all resources associated with it.
1309 * @gdev: GPIO device to decrease the reference count for
1310 */
gpio_device_put(struct gpio_device * gdev)1311 void gpio_device_put(struct gpio_device *gdev)
1312 {
1313 put_device(&gdev->dev);
1314 }
1315 EXPORT_SYMBOL_GPL(gpio_device_put);
1316
1317 /**
1318 * gpio_device_to_device() - Retrieve the address of the underlying struct
1319 * device.
1320 * @gdev: GPIO device for which to return the address.
1321 *
1322 * This does not increase the reference count of the GPIO device nor the
1323 * underlying struct device.
1324 *
1325 * Returns:
1326 * Address of struct device backing this GPIO device.
1327 */
gpio_device_to_device(struct gpio_device * gdev)1328 struct device *gpio_device_to_device(struct gpio_device *gdev)
1329 {
1330 return &gdev->dev;
1331 }
1332 EXPORT_SYMBOL_GPL(gpio_device_to_device);
1333
1334 #ifdef CONFIG_GPIOLIB_IRQCHIP
1335
1336 /*
1337 * The following is irqchip helper code for gpiochips.
1338 */
1339
gpiochip_irqchip_init_hw(struct gpio_chip * gc)1340 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1341 {
1342 struct gpio_irq_chip *girq = &gc->irq;
1343
1344 if (!girq->init_hw)
1345 return 0;
1346
1347 return girq->init_hw(gc);
1348 }
1349
gpiochip_irqchip_init_valid_mask(struct gpio_chip * gc)1350 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1351 {
1352 struct gpio_irq_chip *girq = &gc->irq;
1353
1354 if (!girq->init_valid_mask)
1355 return 0;
1356
1357 girq->valid_mask = gpiochip_allocate_mask(gc);
1358 if (!girq->valid_mask)
1359 return -ENOMEM;
1360
1361 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1362
1363 return 0;
1364 }
1365
gpiochip_irqchip_free_valid_mask(struct gpio_chip * gc)1366 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1367 {
1368 gpiochip_free_mask(&gc->irq.valid_mask);
1369 }
1370
gpiochip_irqchip_irq_valid(const struct gpio_chip * gc,unsigned int offset)1371 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1372 unsigned int offset)
1373 {
1374 if (!gpiochip_line_is_valid(gc, offset))
1375 return false;
1376 /* No mask means all valid */
1377 if (likely(!gc->irq.valid_mask))
1378 return true;
1379 return test_bit(offset, gc->irq.valid_mask);
1380 }
1381
1382 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1383
1384 /**
1385 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1386 * to a gpiochip
1387 * @gc: the gpiochip to set the irqchip hierarchical handler to
1388 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1389 * will then percolate up to the parent
1390 */
gpiochip_set_hierarchical_irqchip(struct gpio_chip * gc,struct irq_chip * irqchip)1391 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1392 struct irq_chip *irqchip)
1393 {
1394 /* DT will deal with mapping each IRQ as we go along */
1395 if (is_of_node(gc->irq.fwnode))
1396 return;
1397
1398 /*
1399 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1400 * irqs upfront instead of dynamically since we don't have the
1401 * dynamic type of allocation that hardware description languages
1402 * provide. Once all GPIO drivers using board files are gone from
1403 * the kernel we can delete this code, but for a transitional period
1404 * it is necessary to keep this around.
1405 */
1406 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1407 int i;
1408 int ret;
1409
1410 for (i = 0; i < gc->ngpio; i++) {
1411 struct irq_fwspec fwspec;
1412 unsigned int parent_hwirq;
1413 unsigned int parent_type;
1414 struct gpio_irq_chip *girq = &gc->irq;
1415
1416 /*
1417 * We call the child to parent translation function
1418 * only to check if the child IRQ is valid or not.
1419 * Just pick the rising edge type here as that is what
1420 * we likely need to support.
1421 */
1422 ret = girq->child_to_parent_hwirq(gc, i,
1423 IRQ_TYPE_EDGE_RISING,
1424 &parent_hwirq,
1425 &parent_type);
1426 if (ret) {
1427 chip_err(gc, "skip set-up on hwirq %d\n",
1428 i);
1429 continue;
1430 }
1431
1432 fwspec.fwnode = gc->irq.fwnode;
1433 /* This is the hwirq for the GPIO line side of things */
1434 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1435 /* Just pick something */
1436 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1437 fwspec.param_count = 2;
1438 ret = irq_domain_alloc_irqs(gc->irq.domain, 1,
1439 NUMA_NO_NODE, &fwspec);
1440 if (ret < 0) {
1441 chip_err(gc,
1442 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1443 i, parent_hwirq,
1444 ret);
1445 }
1446 }
1447 }
1448
1449 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1450
1451 return;
1452 }
1453
gpiochip_hierarchy_irq_domain_translate(struct irq_domain * d,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)1454 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1455 struct irq_fwspec *fwspec,
1456 unsigned long *hwirq,
1457 unsigned int *type)
1458 {
1459 /* We support standard DT translation */
1460 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1461 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1462 }
1463
1464 /* This is for board files and others not using DT */
1465 if (is_fwnode_irqchip(fwspec->fwnode)) {
1466 int ret;
1467
1468 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1469 if (ret)
1470 return ret;
1471 WARN_ON(*type == IRQ_TYPE_NONE);
1472 return 0;
1473 }
1474 return -EINVAL;
1475 }
1476
gpiochip_hierarchy_irq_domain_alloc(struct irq_domain * d,unsigned int irq,unsigned int nr_irqs,void * data)1477 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1478 unsigned int irq,
1479 unsigned int nr_irqs,
1480 void *data)
1481 {
1482 struct gpio_chip *gc = d->host_data;
1483 irq_hw_number_t hwirq;
1484 unsigned int type = IRQ_TYPE_NONE;
1485 struct irq_fwspec *fwspec = data;
1486 union gpio_irq_fwspec gpio_parent_fwspec = {};
1487 unsigned int parent_hwirq;
1488 unsigned int parent_type;
1489 struct gpio_irq_chip *girq = &gc->irq;
1490 int ret;
1491
1492 /*
1493 * The nr_irqs parameter is always one except for PCI multi-MSI
1494 * so this should not happen.
1495 */
1496 WARN_ON(nr_irqs != 1);
1497
1498 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1499 if (ret)
1500 return ret;
1501
1502 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1503
1504 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1505 &parent_hwirq, &parent_type);
1506 if (ret) {
1507 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1508 return ret;
1509 }
1510 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1511
1512 /*
1513 * We set handle_bad_irq because the .set_type() should
1514 * always be invoked and set the right type of handler.
1515 */
1516 irq_domain_set_info(d,
1517 irq,
1518 hwirq,
1519 gc->irq.chip,
1520 gc,
1521 girq->handler,
1522 NULL, NULL);
1523 irq_set_probe(irq);
1524
1525 /* This parent only handles asserted level IRQs */
1526 ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
1527 parent_hwirq, parent_type);
1528 if (ret)
1529 return ret;
1530
1531 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1532 irq, parent_hwirq);
1533 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1534 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
1535 /*
1536 * If the parent irqdomain is msi, the interrupts have already
1537 * been allocated, so the EEXIST is good.
1538 */
1539 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1540 ret = 0;
1541 if (ret)
1542 chip_err(gc,
1543 "failed to allocate parent hwirq %d for hwirq %lu\n",
1544 parent_hwirq, hwirq);
1545
1546 return ret;
1547 }
1548
gpiochip_child_offset_to_irq_noop(struct gpio_chip * gc,unsigned int offset)1549 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1550 unsigned int offset)
1551 {
1552 return offset;
1553 }
1554
1555 /**
1556 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1557 * @domain: The IRQ domain used by this IRQ chip
1558 * @data: Outermost irq_data associated with the IRQ
1559 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1560 *
1561 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1562 * used as the activate function for the &struct irq_domain_ops. The host_data
1563 * for the IRQ domain must be the &struct gpio_chip.
1564 *
1565 * Returns:
1566 * 0 on success, or negative errno on failure.
1567 */
gpiochip_irq_domain_activate(struct irq_domain * domain,struct irq_data * data,bool reserve)1568 static int gpiochip_irq_domain_activate(struct irq_domain *domain,
1569 struct irq_data *data, bool reserve)
1570 {
1571 struct gpio_chip *gc = domain->host_data;
1572 unsigned int hwirq = irqd_to_hwirq(data);
1573
1574 return gpiochip_lock_as_irq(gc, hwirq);
1575 }
1576
1577 /**
1578 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1579 * @domain: The IRQ domain used by this IRQ chip
1580 * @data: Outermost irq_data associated with the IRQ
1581 *
1582 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1583 * be used as the deactivate function for the &struct irq_domain_ops. The
1584 * host_data for the IRQ domain must be the &struct gpio_chip.
1585 */
gpiochip_irq_domain_deactivate(struct irq_domain * domain,struct irq_data * data)1586 static void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1587 struct irq_data *data)
1588 {
1589 struct gpio_chip *gc = domain->host_data;
1590 unsigned int hwirq = irqd_to_hwirq(data);
1591
1592 return gpiochip_unlock_as_irq(gc, hwirq);
1593 }
1594
gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops * ops)1595 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1596 {
1597 ops->activate = gpiochip_irq_domain_activate;
1598 ops->deactivate = gpiochip_irq_domain_deactivate;
1599 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1600
1601 /*
1602 * We only allow overriding the translate() and free() functions for
1603 * hierarchical chips, and this should only be done if the user
1604 * really need something other than 1:1 translation for translate()
1605 * callback and free if user wants to free up any resources which
1606 * were allocated during callbacks, for example populate_parent_alloc_arg.
1607 */
1608 if (!ops->translate)
1609 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1610 if (!ops->free)
1611 ops->free = irq_domain_free_irqs_common;
1612 }
1613
gpiochip_hierarchy_create_domain(struct gpio_chip * gc)1614 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1615 {
1616 struct irq_domain *domain;
1617
1618 if (!gc->irq.child_to_parent_hwirq ||
1619 !gc->irq.fwnode) {
1620 chip_err(gc, "missing irqdomain vital data\n");
1621 return ERR_PTR(-EINVAL);
1622 }
1623
1624 if (!gc->irq.child_offset_to_irq)
1625 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1626
1627 if (!gc->irq.populate_parent_alloc_arg)
1628 gc->irq.populate_parent_alloc_arg =
1629 gpiochip_populate_parent_fwspec_twocell;
1630
1631 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1632
1633 domain = irq_domain_create_hierarchy(
1634 gc->irq.parent_domain,
1635 0,
1636 gc->ngpio,
1637 gc->irq.fwnode,
1638 &gc->irq.child_irq_domain_ops,
1639 gc);
1640
1641 if (!domain)
1642 return ERR_PTR(-ENOMEM);
1643
1644 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1645
1646 return domain;
1647 }
1648
gpiochip_hierarchy_is_hierarchical(struct gpio_chip * gc)1649 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1650 {
1651 return !!gc->irq.parent_domain;
1652 }
1653
gpiochip_populate_parent_fwspec_twocell(struct gpio_chip * gc,union gpio_irq_fwspec * gfwspec,unsigned int parent_hwirq,unsigned int parent_type)1654 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1655 union gpio_irq_fwspec *gfwspec,
1656 unsigned int parent_hwirq,
1657 unsigned int parent_type)
1658 {
1659 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1660
1661 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1662 fwspec->param_count = 2;
1663 fwspec->param[0] = parent_hwirq;
1664 fwspec->param[1] = parent_type;
1665
1666 return 0;
1667 }
1668 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1669
gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip * gc,union gpio_irq_fwspec * gfwspec,unsigned int parent_hwirq,unsigned int parent_type)1670 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1671 union gpio_irq_fwspec *gfwspec,
1672 unsigned int parent_hwirq,
1673 unsigned int parent_type)
1674 {
1675 struct irq_fwspec *fwspec = &gfwspec->fwspec;
1676
1677 fwspec->fwnode = gc->irq.parent_domain->fwnode;
1678 fwspec->param_count = 4;
1679 fwspec->param[0] = 0;
1680 fwspec->param[1] = parent_hwirq;
1681 fwspec->param[2] = 0;
1682 fwspec->param[3] = parent_type;
1683
1684 return 0;
1685 }
1686 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1687
1688 #else
1689
gpiochip_hierarchy_create_domain(struct gpio_chip * gc)1690 static struct irq_domain *gpiochip_hierarchy_create_domain(struct gpio_chip *gc)
1691 {
1692 return ERR_PTR(-EINVAL);
1693 }
1694
gpiochip_hierarchy_is_hierarchical(struct gpio_chip * gc)1695 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1696 {
1697 return false;
1698 }
1699
1700 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1701
1702 /**
1703 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1704 * @d: the irqdomain used by this irqchip
1705 * @irq: the global irq number used by this GPIO irqchip irq
1706 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1707 *
1708 * This function will set up the mapping for a certain IRQ line on a
1709 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1710 * stored inside the gpiochip.
1711 *
1712 * Returns:
1713 * 0 on success, or negative errno on failure.
1714 */
gpiochip_irq_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hwirq)1715 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1716 irq_hw_number_t hwirq)
1717 {
1718 struct gpio_chip *gc = d->host_data;
1719 int ret = 0;
1720
1721 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1722 return -ENXIO;
1723
1724 irq_set_chip_data(irq, gc);
1725 /*
1726 * This lock class tells lockdep that GPIO irqs are in a different
1727 * category than their parents, so it won't report false recursion.
1728 */
1729 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1730 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1731 /* Chips that use nested thread handlers have them marked */
1732 if (gc->irq.threaded)
1733 irq_set_nested_thread(irq, 1);
1734 irq_set_noprobe(irq);
1735
1736 if (gc->irq.num_parents == 1)
1737 ret = irq_set_parent(irq, gc->irq.parents[0]);
1738 else if (gc->irq.map)
1739 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1740
1741 if (ret < 0)
1742 return ret;
1743
1744 /*
1745 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1746 * is passed as default type.
1747 */
1748 if (gc->irq.default_type != IRQ_TYPE_NONE)
1749 irq_set_irq_type(irq, gc->irq.default_type);
1750
1751 return 0;
1752 }
1753
gpiochip_irq_unmap(struct irq_domain * d,unsigned int irq)1754 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1755 {
1756 struct gpio_chip *gc = d->host_data;
1757
1758 if (gc->irq.threaded)
1759 irq_set_nested_thread(irq, 0);
1760 irq_set_chip_and_handler(irq, NULL, NULL);
1761 irq_set_chip_data(irq, NULL);
1762 }
1763
1764 static const struct irq_domain_ops gpiochip_domain_ops = {
1765 .map = gpiochip_irq_map,
1766 .unmap = gpiochip_irq_unmap,
1767 /* Virtually all GPIO irqchips are twocell:ed */
1768 .xlate = irq_domain_xlate_twocell,
1769 };
1770
gpiochip_simple_create_domain(struct gpio_chip * gc)1771 static struct irq_domain *gpiochip_simple_create_domain(struct gpio_chip *gc)
1772 {
1773 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1774 struct irq_domain *domain;
1775
1776 domain = irq_domain_create_simple(fwnode, gc->ngpio, gc->irq.first,
1777 &gpiochip_domain_ops, gc);
1778 if (!domain)
1779 return ERR_PTR(-EINVAL);
1780
1781 return domain;
1782 }
1783
gpiochip_to_irq(struct gpio_chip * gc,unsigned int offset)1784 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1785 {
1786 struct irq_domain *domain = gc->irq.domain;
1787
1788 #ifdef CONFIG_GPIOLIB_IRQCHIP
1789 /*
1790 * Avoid race condition with other code, which tries to lookup
1791 * an IRQ before the irqchip has been properly registered,
1792 * i.e. while gpiochip is still being brought up.
1793 */
1794 if (!gc->irq.initialized)
1795 return -EPROBE_DEFER;
1796 #endif
1797
1798 if (!gpiochip_irqchip_irq_valid(gc, offset))
1799 return -ENXIO;
1800
1801 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1802 if (irq_domain_is_hierarchy(domain)) {
1803 struct irq_fwspec spec;
1804
1805 spec.fwnode = domain->fwnode;
1806 spec.param_count = 2;
1807 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1808 spec.param[1] = IRQ_TYPE_NONE;
1809
1810 return irq_create_fwspec_mapping(&spec);
1811 }
1812 #endif
1813
1814 return irq_create_mapping(domain, offset);
1815 }
1816
gpiochip_irq_reqres(struct irq_data * d)1817 int gpiochip_irq_reqres(struct irq_data *d)
1818 {
1819 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1820 unsigned int hwirq = irqd_to_hwirq(d);
1821
1822 return gpiochip_reqres_irq(gc, hwirq);
1823 }
1824 EXPORT_SYMBOL(gpiochip_irq_reqres);
1825
gpiochip_irq_relres(struct irq_data * d)1826 void gpiochip_irq_relres(struct irq_data *d)
1827 {
1828 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1829 unsigned int hwirq = irqd_to_hwirq(d);
1830
1831 gpiochip_relres_irq(gc, hwirq);
1832 }
1833 EXPORT_SYMBOL(gpiochip_irq_relres);
1834
gpiochip_irq_mask(struct irq_data * d)1835 static void gpiochip_irq_mask(struct irq_data *d)
1836 {
1837 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1838 unsigned int hwirq = irqd_to_hwirq(d);
1839
1840 if (gc->irq.irq_mask)
1841 gc->irq.irq_mask(d);
1842 gpiochip_disable_irq(gc, hwirq);
1843 }
1844
gpiochip_irq_unmask(struct irq_data * d)1845 static void gpiochip_irq_unmask(struct irq_data *d)
1846 {
1847 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1848 unsigned int hwirq = irqd_to_hwirq(d);
1849
1850 gpiochip_enable_irq(gc, hwirq);
1851 if (gc->irq.irq_unmask)
1852 gc->irq.irq_unmask(d);
1853 }
1854
gpiochip_irq_enable(struct irq_data * d)1855 static void gpiochip_irq_enable(struct irq_data *d)
1856 {
1857 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1858 unsigned int hwirq = irqd_to_hwirq(d);
1859
1860 gpiochip_enable_irq(gc, hwirq);
1861 gc->irq.irq_enable(d);
1862 }
1863
gpiochip_irq_disable(struct irq_data * d)1864 static void gpiochip_irq_disable(struct irq_data *d)
1865 {
1866 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1867 unsigned int hwirq = irqd_to_hwirq(d);
1868
1869 gc->irq.irq_disable(d);
1870 gpiochip_disable_irq(gc, hwirq);
1871 }
1872
gpiochip_set_irq_hooks(struct gpio_chip * gc)1873 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1874 {
1875 struct irq_chip *irqchip = gc->irq.chip;
1876
1877 if (irqchip->flags & IRQCHIP_IMMUTABLE)
1878 return;
1879
1880 chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
1881
1882 if (!irqchip->irq_request_resources &&
1883 !irqchip->irq_release_resources) {
1884 irqchip->irq_request_resources = gpiochip_irq_reqres;
1885 irqchip->irq_release_resources = gpiochip_irq_relres;
1886 }
1887 if (WARN_ON(gc->irq.irq_enable))
1888 return;
1889 /* Check if the irqchip already has this hook... */
1890 if (irqchip->irq_enable == gpiochip_irq_enable ||
1891 irqchip->irq_mask == gpiochip_irq_mask) {
1892 /*
1893 * ...and if so, give a gentle warning that this is bad
1894 * practice.
1895 */
1896 chip_info(gc,
1897 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1898 return;
1899 }
1900
1901 if (irqchip->irq_disable) {
1902 gc->irq.irq_disable = irqchip->irq_disable;
1903 irqchip->irq_disable = gpiochip_irq_disable;
1904 } else {
1905 gc->irq.irq_mask = irqchip->irq_mask;
1906 irqchip->irq_mask = gpiochip_irq_mask;
1907 }
1908
1909 if (irqchip->irq_enable) {
1910 gc->irq.irq_enable = irqchip->irq_enable;
1911 irqchip->irq_enable = gpiochip_irq_enable;
1912 } else {
1913 gc->irq.irq_unmask = irqchip->irq_unmask;
1914 irqchip->irq_unmask = gpiochip_irq_unmask;
1915 }
1916 }
1917
gpiochip_irqchip_add_allocated_domain(struct gpio_chip * gc,struct irq_domain * domain,bool allocated_externally)1918 static int gpiochip_irqchip_add_allocated_domain(struct gpio_chip *gc,
1919 struct irq_domain *domain,
1920 bool allocated_externally)
1921 {
1922 if (!domain)
1923 return -EINVAL;
1924
1925 if (gc->to_irq)
1926 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1927
1928 gc->to_irq = gpiochip_to_irq;
1929 gc->irq.domain = domain;
1930 gc->irq.domain_is_allocated_externally = allocated_externally;
1931
1932 /*
1933 * Using barrier() here to prevent compiler from reordering
1934 * gc->irq.initialized before adding irqdomain.
1935 */
1936 barrier();
1937
1938 gc->irq.initialized = true;
1939
1940 return 0;
1941 }
1942
1943 /**
1944 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1945 * @gc: the GPIO chip to add the IRQ chip to
1946 * @lock_key: lockdep class for IRQ lock
1947 * @request_key: lockdep class for IRQ request
1948 *
1949 * Returns:
1950 * 0 on success, or a negative errno on failure.
1951 */
gpiochip_add_irqchip(struct gpio_chip * gc,struct lock_class_key * lock_key,struct lock_class_key * request_key)1952 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1953 struct lock_class_key *lock_key,
1954 struct lock_class_key *request_key)
1955 {
1956 struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1957 struct irq_chip *irqchip = gc->irq.chip;
1958 struct irq_domain *domain;
1959 unsigned int type;
1960 unsigned int i;
1961 int ret;
1962
1963 if (!irqchip)
1964 return 0;
1965
1966 if (gc->irq.parent_handler && gc->can_sleep) {
1967 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1968 return -EINVAL;
1969 }
1970
1971 type = gc->irq.default_type;
1972
1973 /*
1974 * Specifying a default trigger is a terrible idea if DT or ACPI is
1975 * used to configure the interrupts, as you may end up with
1976 * conflicting triggers. Tell the user, and reset to NONE.
1977 */
1978 if (WARN(fwnode && type != IRQ_TYPE_NONE,
1979 "%pfw: Ignoring %u default trigger\n", fwnode, type))
1980 type = IRQ_TYPE_NONE;
1981
1982 gc->irq.default_type = type;
1983 gc->irq.lock_key = lock_key;
1984 gc->irq.request_key = request_key;
1985
1986 /* If a parent irqdomain is provided, let's build a hierarchy */
1987 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1988 domain = gpiochip_hierarchy_create_domain(gc);
1989 } else {
1990 domain = gpiochip_simple_create_domain(gc);
1991 }
1992 if (IS_ERR(domain))
1993 return PTR_ERR(domain);
1994
1995 if (gc->irq.parent_handler) {
1996 for (i = 0; i < gc->irq.num_parents; i++) {
1997 void *data;
1998
1999 if (gc->irq.per_parent_data)
2000 data = gc->irq.parent_handler_data_array[i];
2001 else
2002 data = gc->irq.parent_handler_data ?: gc;
2003
2004 /*
2005 * The parent IRQ chip is already using the chip_data
2006 * for this IRQ chip, so our callbacks simply use the
2007 * handler_data.
2008 */
2009 irq_set_chained_handler_and_data(gc->irq.parents[i],
2010 gc->irq.parent_handler,
2011 data);
2012 }
2013 }
2014
2015 gpiochip_set_irq_hooks(gc);
2016
2017 ret = gpiochip_irqchip_add_allocated_domain(gc, domain, false);
2018 if (ret)
2019 return ret;
2020
2021 acpi_gpiochip_request_interrupts(gc);
2022
2023 return 0;
2024 }
2025
2026 /**
2027 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2028 * @gc: the gpiochip to remove the irqchip from
2029 *
2030 * This is called only from gpiochip_remove()
2031 */
gpiochip_irqchip_remove(struct gpio_chip * gc)2032 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
2033 {
2034 struct irq_chip *irqchip = gc->irq.chip;
2035 unsigned int offset;
2036
2037 acpi_gpiochip_free_interrupts(gc);
2038
2039 if (irqchip && gc->irq.parent_handler) {
2040 struct gpio_irq_chip *irq = &gc->irq;
2041 unsigned int i;
2042
2043 for (i = 0; i < irq->num_parents; i++)
2044 irq_set_chained_handler_and_data(irq->parents[i],
2045 NULL, NULL);
2046 }
2047
2048 /* Remove all IRQ mappings and delete the domain */
2049 if (!gc->irq.domain_is_allocated_externally && gc->irq.domain) {
2050 unsigned int irq;
2051
2052 for (offset = 0; offset < gc->ngpio; offset++) {
2053 if (!gpiochip_irqchip_irq_valid(gc, offset))
2054 continue;
2055
2056 irq = irq_find_mapping(gc->irq.domain, offset);
2057 irq_dispose_mapping(irq);
2058 }
2059
2060 irq_domain_remove(gc->irq.domain);
2061 }
2062
2063 if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
2064 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2065 irqchip->irq_request_resources = NULL;
2066 irqchip->irq_release_resources = NULL;
2067 }
2068 if (irqchip->irq_enable == gpiochip_irq_enable) {
2069 irqchip->irq_enable = gc->irq.irq_enable;
2070 irqchip->irq_disable = gc->irq.irq_disable;
2071 }
2072 }
2073 gc->irq.irq_enable = NULL;
2074 gc->irq.irq_disable = NULL;
2075 gc->irq.chip = NULL;
2076
2077 gpiochip_irqchip_free_valid_mask(gc);
2078 }
2079
2080 /**
2081 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
2082 * @gc: the gpiochip to add the irqchip to
2083 * @domain: the irqdomain to add to the gpiochip
2084 *
2085 * This function adds an IRQ domain to the gpiochip.
2086 *
2087 * Returns:
2088 * 0 on success, or negative errno on failure.
2089 */
gpiochip_irqchip_add_domain(struct gpio_chip * gc,struct irq_domain * domain)2090 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
2091 struct irq_domain *domain)
2092 {
2093 return gpiochip_irqchip_add_allocated_domain(gc, domain, true);
2094 }
2095 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
2096
2097 #else /* CONFIG_GPIOLIB_IRQCHIP */
2098
gpiochip_add_irqchip(struct gpio_chip * gc,struct lock_class_key * lock_key,struct lock_class_key * request_key)2099 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
2100 struct lock_class_key *lock_key,
2101 struct lock_class_key *request_key)
2102 {
2103 return 0;
2104 }
gpiochip_irqchip_remove(struct gpio_chip * gc)2105 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
2106
gpiochip_irqchip_init_hw(struct gpio_chip * gc)2107 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
2108 {
2109 return 0;
2110 }
2111
gpiochip_irqchip_init_valid_mask(struct gpio_chip * gc)2112 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
2113 {
2114 return 0;
2115 }
gpiochip_irqchip_free_valid_mask(struct gpio_chip * gc)2116 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
2117 { }
2118
2119 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2120
2121 /**
2122 * gpiochip_generic_request() - request the gpio function for a pin
2123 * @gc: the gpiochip owning the GPIO
2124 * @offset: the offset of the GPIO to request for GPIO function
2125 *
2126 * Returns:
2127 * 0 on success, or negative errno on failure.
2128 */
gpiochip_generic_request(struct gpio_chip * gc,unsigned int offset)2129 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
2130 {
2131 #ifdef CONFIG_PINCTRL
2132 if (list_empty(&gc->gpiodev->pin_ranges))
2133 return 0;
2134 #endif
2135
2136 return pinctrl_gpio_request(gc, offset);
2137 }
2138 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2139
2140 /**
2141 * gpiochip_generic_free() - free the gpio function from a pin
2142 * @gc: the gpiochip to request the gpio function for
2143 * @offset: the offset of the GPIO to free from GPIO function
2144 */
gpiochip_generic_free(struct gpio_chip * gc,unsigned int offset)2145 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
2146 {
2147 #ifdef CONFIG_PINCTRL
2148 if (list_empty(&gc->gpiodev->pin_ranges))
2149 return;
2150 #endif
2151
2152 pinctrl_gpio_free(gc, offset);
2153 }
2154 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2155
2156 /**
2157 * gpiochip_generic_config() - apply configuration for a pin
2158 * @gc: the gpiochip owning the GPIO
2159 * @offset: the offset of the GPIO to apply the configuration
2160 * @config: the configuration to be applied
2161 *
2162 * Returns:
2163 * 0 on success, or negative errno on failure.
2164 */
gpiochip_generic_config(struct gpio_chip * gc,unsigned int offset,unsigned long config)2165 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
2166 unsigned long config)
2167 {
2168 #ifdef CONFIG_PINCTRL
2169 if (list_empty(&gc->gpiodev->pin_ranges))
2170 return -ENOTSUPP;
2171 #endif
2172
2173 return pinctrl_gpio_set_config(gc, offset, config);
2174 }
2175 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2176
2177 #ifdef CONFIG_PINCTRL
2178
2179 /**
2180 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2181 * @gc: the gpiochip to add the range for
2182 * @pctldev: the pin controller to map to
2183 * @gpio_offset: the start offset in the current gpio_chip number space
2184 * @pin_group: name of the pin group inside the pin controller
2185 *
2186 * Calling this function directly from a DeviceTree-supported
2187 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2188 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2189 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2190 *
2191 * Returns:
2192 * 0 on success, or negative errno on failure.
2193 */
gpiochip_add_pingroup_range(struct gpio_chip * gc,struct pinctrl_dev * pctldev,unsigned int gpio_offset,const char * pin_group)2194 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
2195 struct pinctrl_dev *pctldev,
2196 unsigned int gpio_offset, const char *pin_group)
2197 {
2198 struct gpio_pin_range *pin_range;
2199 struct gpio_device *gdev = gc->gpiodev;
2200 int ret;
2201
2202 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2203 if (!pin_range) {
2204 chip_err(gc, "failed to allocate pin ranges\n");
2205 return -ENOMEM;
2206 }
2207
2208 /* Use local offset as range ID */
2209 pin_range->range.id = gpio_offset;
2210 pin_range->range.gc = gc;
2211 pin_range->range.name = gc->label;
2212 pin_range->range.base = gdev->base + gpio_offset;
2213 pin_range->pctldev = pctldev;
2214
2215 ret = pinctrl_get_group_pins(pctldev, pin_group,
2216 &pin_range->range.pins,
2217 &pin_range->range.npins);
2218 if (ret < 0) {
2219 kfree(pin_range);
2220 return ret;
2221 }
2222
2223 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2224
2225 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2226 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2227 pinctrl_dev_get_devname(pctldev), pin_group);
2228
2229 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2230
2231 return 0;
2232 }
2233 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2234
2235 /**
2236 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2237 * @gc: the gpiochip to add the range for
2238 * @pinctl_name: the dev_name() of the pin controller to map to
2239 * @gpio_offset: the start offset in the current gpio_chip number space
2240 * @pin_offset: the start offset in the pin controller number space
2241 * @npins: the number of pins from the offset of each pin space (GPIO and
2242 * pin controller) to accumulate in this range
2243 *
2244 * Calling this function directly from a DeviceTree-supported
2245 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2246 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2247 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2248 *
2249 * Returns:
2250 * 0 on success, or a negative errno on failure.
2251 */
gpiochip_add_pin_range(struct gpio_chip * gc,const char * pinctl_name,unsigned int gpio_offset,unsigned int pin_offset,unsigned int npins)2252 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
2253 unsigned int gpio_offset, unsigned int pin_offset,
2254 unsigned int npins)
2255 {
2256 struct gpio_pin_range *pin_range;
2257 struct gpio_device *gdev = gc->gpiodev;
2258 int ret;
2259
2260 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2261 if (!pin_range) {
2262 chip_err(gc, "failed to allocate pin ranges\n");
2263 return -ENOMEM;
2264 }
2265
2266 /* Use local offset as range ID */
2267 pin_range->range.id = gpio_offset;
2268 pin_range->range.gc = gc;
2269 pin_range->range.name = gc->label;
2270 pin_range->range.base = gdev->base + gpio_offset;
2271 pin_range->range.pin_base = pin_offset;
2272 pin_range->range.npins = npins;
2273 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2274 &pin_range->range);
2275 if (IS_ERR(pin_range->pctldev)) {
2276 ret = PTR_ERR(pin_range->pctldev);
2277 chip_err(gc, "could not create pin range\n");
2278 kfree(pin_range);
2279 return ret;
2280 }
2281 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2282 gpio_offset, gpio_offset + npins - 1,
2283 pinctl_name,
2284 pin_offset, pin_offset + npins - 1);
2285
2286 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2287
2288 return 0;
2289 }
2290 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2291
2292 /**
2293 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2294 * @gc: the chip to remove all the mappings for
2295 */
gpiochip_remove_pin_ranges(struct gpio_chip * gc)2296 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
2297 {
2298 struct gpio_pin_range *pin_range, *tmp;
2299 struct gpio_device *gdev = gc->gpiodev;
2300
2301 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2302 list_del(&pin_range->node);
2303 pinctrl_remove_gpio_range(pin_range->pctldev,
2304 &pin_range->range);
2305 kfree(pin_range);
2306 }
2307 }
2308 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2309
2310 #endif /* CONFIG_PINCTRL */
2311
2312 /* These "optional" allocation calls help prevent drivers from stomping
2313 * on each other, and help provide better diagnostics in debugfs.
2314 * They're called even less than the "set direction" calls.
2315 */
gpiod_request_commit(struct gpio_desc * desc,const char * label)2316 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2317 {
2318 unsigned int offset;
2319 int ret;
2320
2321 CLASS(gpio_chip_guard, guard)(desc);
2322 if (!guard.gc)
2323 return -ENODEV;
2324
2325 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags))
2326 return -EBUSY;
2327
2328 /* NOTE: gpio_request() can be called in early boot,
2329 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2330 */
2331
2332 if (guard.gc->request) {
2333 offset = gpio_chip_hwgpio(desc);
2334 if (gpiochip_line_is_valid(guard.gc, offset))
2335 ret = guard.gc->request(guard.gc, offset);
2336 else
2337 ret = -EINVAL;
2338 if (ret)
2339 goto out_clear_bit;
2340 }
2341
2342 if (guard.gc->get_direction)
2343 gpiod_get_direction(desc);
2344
2345 ret = desc_set_label(desc, label ? : "?");
2346 if (ret)
2347 goto out_clear_bit;
2348
2349 return 0;
2350
2351 out_clear_bit:
2352 clear_bit(FLAG_REQUESTED, &desc->flags);
2353 return ret;
2354 }
2355
2356 /*
2357 * This descriptor validation needs to be inserted verbatim into each
2358 * function taking a descriptor, so we need to use a preprocessor
2359 * macro to avoid endless duplication. If the desc is NULL it is an
2360 * optional GPIO and calls should just bail out.
2361 */
validate_desc(const struct gpio_desc * desc,const char * func)2362 static int validate_desc(const struct gpio_desc *desc, const char *func)
2363 {
2364 if (!desc)
2365 return 0;
2366
2367 if (IS_ERR(desc)) {
2368 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2369 return PTR_ERR(desc);
2370 }
2371
2372 return 1;
2373 }
2374
2375 #define VALIDATE_DESC(desc) do { \
2376 int __valid = validate_desc(desc, __func__); \
2377 if (__valid <= 0) \
2378 return __valid; \
2379 } while (0)
2380
2381 #define VALIDATE_DESC_VOID(desc) do { \
2382 int __valid = validate_desc(desc, __func__); \
2383 if (__valid <= 0) \
2384 return; \
2385 } while (0)
2386
gpiod_request(struct gpio_desc * desc,const char * label)2387 int gpiod_request(struct gpio_desc *desc, const char *label)
2388 {
2389 int ret = -EPROBE_DEFER;
2390
2391 VALIDATE_DESC(desc);
2392
2393 if (try_module_get(desc->gdev->owner)) {
2394 ret = gpiod_request_commit(desc, label);
2395 if (ret)
2396 module_put(desc->gdev->owner);
2397 else
2398 gpio_device_get(desc->gdev);
2399 }
2400
2401 if (ret)
2402 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2403
2404 return ret;
2405 }
2406
gpiod_free_commit(struct gpio_desc * desc)2407 static void gpiod_free_commit(struct gpio_desc *desc)
2408 {
2409 unsigned long flags;
2410
2411 might_sleep();
2412
2413 CLASS(gpio_chip_guard, guard)(desc);
2414
2415 flags = READ_ONCE(desc->flags);
2416
2417 if (guard.gc && test_bit(FLAG_REQUESTED, &flags)) {
2418 if (guard.gc->free)
2419 guard.gc->free(guard.gc, gpio_chip_hwgpio(desc));
2420
2421 clear_bit(FLAG_ACTIVE_LOW, &flags);
2422 clear_bit(FLAG_REQUESTED, &flags);
2423 clear_bit(FLAG_OPEN_DRAIN, &flags);
2424 clear_bit(FLAG_OPEN_SOURCE, &flags);
2425 clear_bit(FLAG_PULL_UP, &flags);
2426 clear_bit(FLAG_PULL_DOWN, &flags);
2427 clear_bit(FLAG_BIAS_DISABLE, &flags);
2428 clear_bit(FLAG_EDGE_RISING, &flags);
2429 clear_bit(FLAG_EDGE_FALLING, &flags);
2430 clear_bit(FLAG_IS_HOGGED, &flags);
2431 #ifdef CONFIG_OF_DYNAMIC
2432 WRITE_ONCE(desc->hog, NULL);
2433 #endif
2434 desc_set_label(desc, NULL);
2435 WRITE_ONCE(desc->flags, flags);
2436
2437 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED);
2438 }
2439 }
2440
gpiod_free(struct gpio_desc * desc)2441 void gpiod_free(struct gpio_desc *desc)
2442 {
2443 VALIDATE_DESC_VOID(desc);
2444
2445 gpiod_free_commit(desc);
2446 module_put(desc->gdev->owner);
2447 gpio_device_put(desc->gdev);
2448 }
2449
2450 /**
2451 * gpiochip_dup_line_label - Get a copy of the consumer label.
2452 * @gc: GPIO chip controlling this line.
2453 * @offset: Hardware offset of the line.
2454 *
2455 * Returns:
2456 * Pointer to a copy of the consumer label if the line is requested or NULL
2457 * if it's not. If a valid pointer was returned, it must be freed using
2458 * kfree(). In case of a memory allocation error, the function returns %ENOMEM.
2459 *
2460 * Must not be called from atomic context.
2461 */
gpiochip_dup_line_label(struct gpio_chip * gc,unsigned int offset)2462 char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
2463 {
2464 struct gpio_desc *desc;
2465 char *label;
2466
2467 desc = gpiochip_get_desc(gc, offset);
2468 if (IS_ERR(desc))
2469 return NULL;
2470
2471 if (!test_bit(FLAG_REQUESTED, &desc->flags))
2472 return NULL;
2473
2474 guard(srcu)(&desc->gdev->desc_srcu);
2475
2476 label = kstrdup(gpiod_get_label(desc), GFP_KERNEL);
2477 if (!label)
2478 return ERR_PTR(-ENOMEM);
2479
2480 return label;
2481 }
2482 EXPORT_SYMBOL_GPL(gpiochip_dup_line_label);
2483
function_name_or_default(const char * con_id)2484 static inline const char *function_name_or_default(const char *con_id)
2485 {
2486 return con_id ?: "(default)";
2487 }
2488
2489 /**
2490 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2491 * @gc: GPIO chip
2492 * @hwnum: hardware number of the GPIO for which to request the descriptor
2493 * @label: label for the GPIO
2494 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2495 * specify things like line inversion semantics with the machine flags
2496 * such as GPIO_OUT_LOW
2497 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2498 * can be used to specify consumer semantics such as open drain
2499 *
2500 * Function allows GPIO chip drivers to request and use their own GPIO
2501 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2502 * function will not increase reference count of the GPIO chip module. This
2503 * allows the GPIO chip module to be unloaded as needed (we assume that the
2504 * GPIO chip driver handles freeing the GPIOs it has requested).
2505 *
2506 * Returns:
2507 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2508 * code on failure.
2509 */
gpiochip_request_own_desc(struct gpio_chip * gc,unsigned int hwnum,const char * label,enum gpio_lookup_flags lflags,enum gpiod_flags dflags)2510 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2511 unsigned int hwnum,
2512 const char *label,
2513 enum gpio_lookup_flags lflags,
2514 enum gpiod_flags dflags)
2515 {
2516 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2517 const char *name = function_name_or_default(label);
2518 int ret;
2519
2520 if (IS_ERR(desc)) {
2521 chip_err(gc, "failed to get GPIO %s descriptor\n", name);
2522 return desc;
2523 }
2524
2525 ret = gpiod_request_commit(desc, label);
2526 if (ret < 0)
2527 return ERR_PTR(ret);
2528
2529 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2530 if (ret) {
2531 gpiod_free_commit(desc);
2532 chip_err(gc, "setup of own GPIO %s failed\n", name);
2533 return ERR_PTR(ret);
2534 }
2535
2536 return desc;
2537 }
2538 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2539
2540 /**
2541 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2542 * @desc: GPIO descriptor to free
2543 *
2544 * Function frees the given GPIO requested previously with
2545 * gpiochip_request_own_desc().
2546 */
gpiochip_free_own_desc(struct gpio_desc * desc)2547 void gpiochip_free_own_desc(struct gpio_desc *desc)
2548 {
2549 if (desc)
2550 gpiod_free_commit(desc);
2551 }
2552 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2553
2554 /*
2555 * Drivers MUST set GPIO direction before making get/set calls. In
2556 * some cases this is done in early boot, before IRQs are enabled.
2557 *
2558 * As a rule these aren't called more than once (except for drivers
2559 * using the open-drain emulation idiom) so these are natural places
2560 * to accumulate extra debugging checks. Note that we can't (yet)
2561 * rely on gpio_request() having been called beforehand.
2562 */
2563
gpio_do_set_config(struct gpio_chip * gc,unsigned int offset,unsigned long config)2564 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2565 unsigned long config)
2566 {
2567 if (!gc->set_config)
2568 return -ENOTSUPP;
2569
2570 return gc->set_config(gc, offset, config);
2571 }
2572
gpio_set_config_with_argument(struct gpio_desc * desc,enum pin_config_param mode,u32 argument)2573 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2574 enum pin_config_param mode,
2575 u32 argument)
2576 {
2577 unsigned long config;
2578
2579 CLASS(gpio_chip_guard, guard)(desc);
2580 if (!guard.gc)
2581 return -ENODEV;
2582
2583 config = pinconf_to_config_packed(mode, argument);
2584 return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config);
2585 }
2586
gpio_set_config_with_argument_optional(struct gpio_desc * desc,enum pin_config_param mode,u32 argument)2587 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2588 enum pin_config_param mode,
2589 u32 argument)
2590 {
2591 struct device *dev = &desc->gdev->dev;
2592 int gpio = gpio_chip_hwgpio(desc);
2593 int ret;
2594
2595 ret = gpio_set_config_with_argument(desc, mode, argument);
2596 if (ret != -ENOTSUPP)
2597 return ret;
2598
2599 switch (mode) {
2600 case PIN_CONFIG_PERSIST_STATE:
2601 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2602 break;
2603 default:
2604 break;
2605 }
2606
2607 return 0;
2608 }
2609
gpio_set_config(struct gpio_desc * desc,enum pin_config_param mode)2610 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2611 {
2612 return gpio_set_config_with_argument(desc, mode, 0);
2613 }
2614
gpio_set_bias(struct gpio_desc * desc)2615 static int gpio_set_bias(struct gpio_desc *desc)
2616 {
2617 enum pin_config_param bias;
2618 unsigned long flags;
2619 unsigned int arg;
2620
2621 flags = READ_ONCE(desc->flags);
2622
2623 if (test_bit(FLAG_BIAS_DISABLE, &flags))
2624 bias = PIN_CONFIG_BIAS_DISABLE;
2625 else if (test_bit(FLAG_PULL_UP, &flags))
2626 bias = PIN_CONFIG_BIAS_PULL_UP;
2627 else if (test_bit(FLAG_PULL_DOWN, &flags))
2628 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2629 else
2630 return 0;
2631
2632 switch (bias) {
2633 case PIN_CONFIG_BIAS_PULL_DOWN:
2634 case PIN_CONFIG_BIAS_PULL_UP:
2635 arg = 1;
2636 break;
2637
2638 default:
2639 arg = 0;
2640 break;
2641 }
2642
2643 return gpio_set_config_with_argument_optional(desc, bias, arg);
2644 }
2645
2646 /**
2647 * gpio_set_debounce_timeout() - Set debounce timeout
2648 * @desc: GPIO descriptor to set the debounce timeout
2649 * @debounce: Debounce timeout in microseconds
2650 *
2651 * The function calls the certain GPIO driver to set debounce timeout
2652 * in the hardware.
2653 *
2654 * Returns:
2655 * 0 on success, or negative errno on failure.
2656 */
gpio_set_debounce_timeout(struct gpio_desc * desc,unsigned int debounce)2657 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2658 {
2659 return gpio_set_config_with_argument_optional(desc,
2660 PIN_CONFIG_INPUT_DEBOUNCE,
2661 debounce);
2662 }
2663
2664 /**
2665 * gpiod_direction_input - set the GPIO direction to input
2666 * @desc: GPIO to set to input
2667 *
2668 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2669 * be called safely on it.
2670 *
2671 * Returns:
2672 * 0 on success, or negative errno on failure.
2673 */
gpiod_direction_input(struct gpio_desc * desc)2674 int gpiod_direction_input(struct gpio_desc *desc)
2675 {
2676 int ret = 0;
2677
2678 VALIDATE_DESC(desc);
2679
2680 CLASS(gpio_chip_guard, guard)(desc);
2681 if (!guard.gc)
2682 return -ENODEV;
2683
2684 /*
2685 * It is legal to have no .get() and .direction_input() specified if
2686 * the chip is output-only, but you can't specify .direction_input()
2687 * and not support the .get() operation, that doesn't make sense.
2688 */
2689 if (!guard.gc->get && guard.gc->direction_input) {
2690 gpiod_warn(desc,
2691 "%s: missing get() but have direction_input()\n",
2692 __func__);
2693 return -EIO;
2694 }
2695
2696 /*
2697 * If we have a .direction_input() callback, things are simple,
2698 * just call it. Else we are some input-only chip so try to check the
2699 * direction (if .get_direction() is supported) else we silently
2700 * assume we are in input mode after this.
2701 */
2702 if (guard.gc->direction_input) {
2703 ret = guard.gc->direction_input(guard.gc,
2704 gpio_chip_hwgpio(desc));
2705 } else if (guard.gc->get_direction &&
2706 (guard.gc->get_direction(guard.gc,
2707 gpio_chip_hwgpio(desc)) != 1)) {
2708 gpiod_warn(desc,
2709 "%s: missing direction_input() operation and line is output\n",
2710 __func__);
2711 return -EIO;
2712 }
2713 if (ret == 0) {
2714 clear_bit(FLAG_IS_OUT, &desc->flags);
2715 ret = gpio_set_bias(desc);
2716 }
2717
2718 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2719
2720 return ret;
2721 }
2722 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2723
gpiod_direction_output_raw_commit(struct gpio_desc * desc,int value)2724 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2725 {
2726 int val = !!value, ret = 0;
2727
2728 CLASS(gpio_chip_guard, guard)(desc);
2729 if (!guard.gc)
2730 return -ENODEV;
2731
2732 /*
2733 * It's OK not to specify .direction_output() if the gpiochip is
2734 * output-only, but if there is then not even a .set() operation it
2735 * is pretty tricky to drive the output line.
2736 */
2737 if (!guard.gc->set && !guard.gc->direction_output) {
2738 gpiod_warn(desc,
2739 "%s: missing set() and direction_output() operations\n",
2740 __func__);
2741 return -EIO;
2742 }
2743
2744 if (guard.gc->direction_output) {
2745 ret = guard.gc->direction_output(guard.gc,
2746 gpio_chip_hwgpio(desc), val);
2747 } else {
2748 /* Check that we are in output mode if we can */
2749 if (guard.gc->get_direction &&
2750 guard.gc->get_direction(guard.gc, gpio_chip_hwgpio(desc))) {
2751 gpiod_warn(desc,
2752 "%s: missing direction_output() operation\n",
2753 __func__);
2754 return -EIO;
2755 }
2756 /*
2757 * If we can't actively set the direction, we are some
2758 * output-only chip, so just drive the output as desired.
2759 */
2760 guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), val);
2761 }
2762
2763 if (!ret)
2764 set_bit(FLAG_IS_OUT, &desc->flags);
2765 trace_gpio_value(desc_to_gpio(desc), 0, val);
2766 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2767 return ret;
2768 }
2769
2770 /**
2771 * gpiod_direction_output_raw - set the GPIO direction to output
2772 * @desc: GPIO to set to output
2773 * @value: initial output value of the GPIO
2774 *
2775 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2776 * be called safely on it. The initial value of the output must be specified
2777 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2778 *
2779 * Returns:
2780 * 0 on success, or negative errno on failure.
2781 */
gpiod_direction_output_raw(struct gpio_desc * desc,int value)2782 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2783 {
2784 VALIDATE_DESC(desc);
2785 return gpiod_direction_output_raw_commit(desc, value);
2786 }
2787 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2788
2789 /**
2790 * gpiod_direction_output - set the GPIO direction to output
2791 * @desc: GPIO to set to output
2792 * @value: initial output value of the GPIO
2793 *
2794 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2795 * be called safely on it. The initial value of the output must be specified
2796 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2797 * account.
2798 *
2799 * Returns:
2800 * 0 on success, or negative errno on failure.
2801 */
gpiod_direction_output(struct gpio_desc * desc,int value)2802 int gpiod_direction_output(struct gpio_desc *desc, int value)
2803 {
2804 unsigned long flags;
2805 int ret;
2806
2807 VALIDATE_DESC(desc);
2808
2809 flags = READ_ONCE(desc->flags);
2810
2811 if (test_bit(FLAG_ACTIVE_LOW, &flags))
2812 value = !value;
2813 else
2814 value = !!value;
2815
2816 /* GPIOs used for enabled IRQs shall not be set as output */
2817 if (test_bit(FLAG_USED_AS_IRQ, &flags) &&
2818 test_bit(FLAG_IRQ_IS_ENABLED, &flags)) {
2819 gpiod_err(desc,
2820 "%s: tried to set a GPIO tied to an IRQ as output\n",
2821 __func__);
2822 return -EIO;
2823 }
2824
2825 if (test_bit(FLAG_OPEN_DRAIN, &flags)) {
2826 /* First see if we can enable open drain in hardware */
2827 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2828 if (!ret)
2829 goto set_output_value;
2830 /* Emulate open drain by not actively driving the line high */
2831 if (value) {
2832 ret = gpiod_direction_input(desc);
2833 goto set_output_flag;
2834 }
2835 } else if (test_bit(FLAG_OPEN_SOURCE, &flags)) {
2836 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2837 if (!ret)
2838 goto set_output_value;
2839 /* Emulate open source by not actively driving the line low */
2840 if (!value) {
2841 ret = gpiod_direction_input(desc);
2842 goto set_output_flag;
2843 }
2844 } else {
2845 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2846 }
2847
2848 set_output_value:
2849 ret = gpio_set_bias(desc);
2850 if (ret)
2851 return ret;
2852 return gpiod_direction_output_raw_commit(desc, value);
2853
2854 set_output_flag:
2855 /*
2856 * When emulating open-source or open-drain functionalities by not
2857 * actively driving the line (setting mode to input) we still need to
2858 * set the IS_OUT flag or otherwise we won't be able to set the line
2859 * value anymore.
2860 */
2861 if (ret == 0)
2862 set_bit(FLAG_IS_OUT, &desc->flags);
2863 return ret;
2864 }
2865 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2866
2867 /**
2868 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2869 *
2870 * @desc: GPIO to enable.
2871 * @flags: Flags related to GPIO edge.
2872 *
2873 * Returns:
2874 * 0 on success, or negative errno on failure.
2875 */
gpiod_enable_hw_timestamp_ns(struct gpio_desc * desc,unsigned long flags)2876 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2877 {
2878 int ret = 0;
2879
2880 VALIDATE_DESC(desc);
2881
2882 CLASS(gpio_chip_guard, guard)(desc);
2883 if (!guard.gc)
2884 return -ENODEV;
2885
2886 if (!guard.gc->en_hw_timestamp) {
2887 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2888 return -ENOTSUPP;
2889 }
2890
2891 ret = guard.gc->en_hw_timestamp(guard.gc,
2892 gpio_chip_hwgpio(desc), flags);
2893 if (ret)
2894 gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
2895
2896 return ret;
2897 }
2898 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
2899
2900 /**
2901 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2902 *
2903 * @desc: GPIO to disable.
2904 * @flags: Flags related to GPIO edge, same value as used during enable call.
2905 *
2906 * Returns:
2907 * 0 on success, or negative errno on failure.
2908 */
gpiod_disable_hw_timestamp_ns(struct gpio_desc * desc,unsigned long flags)2909 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
2910 {
2911 int ret = 0;
2912
2913 VALIDATE_DESC(desc);
2914
2915 CLASS(gpio_chip_guard, guard)(desc);
2916 if (!guard.gc)
2917 return -ENODEV;
2918
2919 if (!guard.gc->dis_hw_timestamp) {
2920 gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
2921 return -ENOTSUPP;
2922 }
2923
2924 ret = guard.gc->dis_hw_timestamp(guard.gc, gpio_chip_hwgpio(desc),
2925 flags);
2926 if (ret)
2927 gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
2928
2929 return ret;
2930 }
2931 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
2932
2933 /**
2934 * gpiod_set_config - sets @config for a GPIO
2935 * @desc: descriptor of the GPIO for which to set the configuration
2936 * @config: Same packed config format as generic pinconf
2937 *
2938 * Returns:
2939 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2940 * configuration.
2941 */
gpiod_set_config(struct gpio_desc * desc,unsigned long config)2942 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2943 {
2944 VALIDATE_DESC(desc);
2945
2946 CLASS(gpio_chip_guard, guard)(desc);
2947 if (!guard.gc)
2948 return -ENODEV;
2949
2950 return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config);
2951 }
2952 EXPORT_SYMBOL_GPL(gpiod_set_config);
2953
2954 /**
2955 * gpiod_set_debounce - sets @debounce time for a GPIO
2956 * @desc: descriptor of the GPIO for which to set debounce time
2957 * @debounce: debounce time in microseconds
2958 *
2959 * Returns:
2960 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2961 * debounce time.
2962 */
gpiod_set_debounce(struct gpio_desc * desc,unsigned int debounce)2963 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2964 {
2965 unsigned long config;
2966
2967 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2968 return gpiod_set_config(desc, config);
2969 }
2970 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2971
2972 /**
2973 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2974 * @desc: descriptor of the GPIO for which to configure persistence
2975 * @transitory: True to lose state on suspend or reset, false for persistence
2976 *
2977 * Returns:
2978 * 0 on success, otherwise a negative error code.
2979 */
gpiod_set_transitory(struct gpio_desc * desc,bool transitory)2980 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2981 {
2982 VALIDATE_DESC(desc);
2983 /*
2984 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2985 * persistence state.
2986 */
2987 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2988
2989 /* If the driver supports it, set the persistence state now */
2990 return gpio_set_config_with_argument_optional(desc,
2991 PIN_CONFIG_PERSIST_STATE,
2992 !transitory);
2993 }
2994
2995 /**
2996 * gpiod_is_active_low - test whether a GPIO is active-low or not
2997 * @desc: the gpio descriptor to test
2998 *
2999 * Returns:
3000 * 1 if the GPIO is active-low, 0 otherwise.
3001 */
gpiod_is_active_low(const struct gpio_desc * desc)3002 int gpiod_is_active_low(const struct gpio_desc *desc)
3003 {
3004 VALIDATE_DESC(desc);
3005 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
3006 }
3007 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
3008
3009 /**
3010 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
3011 * @desc: the gpio descriptor to change
3012 */
gpiod_toggle_active_low(struct gpio_desc * desc)3013 void gpiod_toggle_active_low(struct gpio_desc *desc)
3014 {
3015 VALIDATE_DESC_VOID(desc);
3016 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
3017 }
3018 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
3019
gpio_chip_get_value(struct gpio_chip * gc,const struct gpio_desc * desc)3020 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
3021 {
3022 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
3023 }
3024
3025 /* I/O calls are only valid after configuration completed; the relevant
3026 * "is this a valid GPIO" error checks should already have been done.
3027 *
3028 * "Get" operations are often inlinable as reading a pin value register,
3029 * and masking the relevant bit in that register.
3030 *
3031 * When "set" operations are inlinable, they involve writing that mask to
3032 * one register to set a low value, or a different register to set it high.
3033 * Otherwise locking is needed, so there may be little value to inlining.
3034 *
3035 *------------------------------------------------------------------------
3036 *
3037 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3038 * have requested the GPIO. That can include implicit requesting by
3039 * a direction setting call. Marking a gpio as requested locks its chip
3040 * in memory, guaranteeing that these table lookups need no more locking
3041 * and that gpiochip_remove() will fail.
3042 *
3043 * REVISIT when debugging, consider adding some instrumentation to ensure
3044 * that the GPIO was actually requested.
3045 */
3046
gpiod_get_raw_value_commit(const struct gpio_desc * desc)3047 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
3048 {
3049 struct gpio_device *gdev;
3050 struct gpio_chip *gc;
3051 int value;
3052
3053 /* FIXME Unable to use gpio_chip_guard due to const desc. */
3054 gdev = desc->gdev;
3055
3056 guard(srcu)(&gdev->srcu);
3057
3058 gc = srcu_dereference(gdev->chip, &gdev->srcu);
3059 if (!gc)
3060 return -ENODEV;
3061
3062 value = gpio_chip_get_value(gc, desc);
3063 value = value < 0 ? value : !!value;
3064 trace_gpio_value(desc_to_gpio(desc), 1, value);
3065 return value;
3066 }
3067
gpio_chip_get_multiple(struct gpio_chip * gc,unsigned long * mask,unsigned long * bits)3068 static int gpio_chip_get_multiple(struct gpio_chip *gc,
3069 unsigned long *mask, unsigned long *bits)
3070 {
3071 if (gc->get_multiple)
3072 return gc->get_multiple(gc, mask, bits);
3073 if (gc->get) {
3074 int i, value;
3075
3076 for_each_set_bit(i, mask, gc->ngpio) {
3077 value = gc->get(gc, i);
3078 if (value < 0)
3079 return value;
3080 __assign_bit(i, bits, value);
3081 }
3082 return 0;
3083 }
3084 return -EIO;
3085 }
3086
3087 /* The 'other' chip must be protected with its GPIO device's SRCU. */
gpio_device_chip_cmp(struct gpio_device * gdev,struct gpio_chip * gc)3088 static bool gpio_device_chip_cmp(struct gpio_device *gdev, struct gpio_chip *gc)
3089 {
3090 guard(srcu)(&gdev->srcu);
3091
3092 return gc == srcu_dereference(gdev->chip, &gdev->srcu);
3093 }
3094
gpiod_get_array_value_complex(bool raw,bool can_sleep,unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)3095 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3096 unsigned int array_size,
3097 struct gpio_desc **desc_array,
3098 struct gpio_array *array_info,
3099 unsigned long *value_bitmap)
3100 {
3101 int ret, i = 0;
3102
3103 /*
3104 * Validate array_info against desc_array and its size.
3105 * It should immediately follow desc_array if both
3106 * have been obtained from the same gpiod_get_array() call.
3107 */
3108 if (array_info && array_info->desc == desc_array &&
3109 array_size <= array_info->size &&
3110 (void *)array_info == desc_array + array_info->size) {
3111 if (!can_sleep)
3112 WARN_ON(array_info->chip->can_sleep);
3113
3114 ret = gpio_chip_get_multiple(array_info->chip,
3115 array_info->get_mask,
3116 value_bitmap);
3117 if (ret)
3118 return ret;
3119
3120 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3121 bitmap_xor(value_bitmap, value_bitmap,
3122 array_info->invert_mask, array_size);
3123
3124 i = find_first_zero_bit(array_info->get_mask, array_size);
3125 if (i == array_size)
3126 return 0;
3127 } else {
3128 array_info = NULL;
3129 }
3130
3131 while (i < array_size) {
3132 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3133 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3134 unsigned long *mask, *bits;
3135 int first, j;
3136
3137 CLASS(gpio_chip_guard, guard)(desc_array[i]);
3138 if (!guard.gc)
3139 return -ENODEV;
3140
3141 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) {
3142 mask = fastpath_mask;
3143 bits = fastpath_bits;
3144 } else {
3145 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3146
3147 mask = bitmap_alloc(guard.gc->ngpio, flags);
3148 if (!mask)
3149 return -ENOMEM;
3150
3151 bits = bitmap_alloc(guard.gc->ngpio, flags);
3152 if (!bits) {
3153 bitmap_free(mask);
3154 return -ENOMEM;
3155 }
3156 }
3157
3158 bitmap_zero(mask, guard.gc->ngpio);
3159
3160 if (!can_sleep)
3161 WARN_ON(guard.gc->can_sleep);
3162
3163 /* collect all inputs belonging to the same chip */
3164 first = i;
3165 do {
3166 const struct gpio_desc *desc = desc_array[i];
3167 int hwgpio = gpio_chip_hwgpio(desc);
3168
3169 __set_bit(hwgpio, mask);
3170 i++;
3171
3172 if (array_info)
3173 i = find_next_zero_bit(array_info->get_mask,
3174 array_size, i);
3175 } while ((i < array_size) &&
3176 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc));
3177
3178 ret = gpio_chip_get_multiple(guard.gc, mask, bits);
3179 if (ret) {
3180 if (mask != fastpath_mask)
3181 bitmap_free(mask);
3182 if (bits != fastpath_bits)
3183 bitmap_free(bits);
3184 return ret;
3185 }
3186
3187 for (j = first; j < i; ) {
3188 const struct gpio_desc *desc = desc_array[j];
3189 int hwgpio = gpio_chip_hwgpio(desc);
3190 int value = test_bit(hwgpio, bits);
3191
3192 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3193 value = !value;
3194 __assign_bit(j, value_bitmap, value);
3195 trace_gpio_value(desc_to_gpio(desc), 1, value);
3196 j++;
3197
3198 if (array_info)
3199 j = find_next_zero_bit(array_info->get_mask, i,
3200 j);
3201 }
3202
3203 if (mask != fastpath_mask)
3204 bitmap_free(mask);
3205 if (bits != fastpath_bits)
3206 bitmap_free(bits);
3207 }
3208 return 0;
3209 }
3210
3211 /**
3212 * gpiod_get_raw_value() - return a gpio's raw value
3213 * @desc: gpio whose value will be returned
3214 *
3215 * Returns:
3216 * The GPIO's raw value, i.e. the value of the physical line disregarding
3217 * its ACTIVE_LOW status, or negative errno on failure.
3218 *
3219 * This function can be called from contexts where we cannot sleep, and will
3220 * complain if the GPIO chip functions potentially sleep.
3221 */
gpiod_get_raw_value(const struct gpio_desc * desc)3222 int gpiod_get_raw_value(const struct gpio_desc *desc)
3223 {
3224 VALIDATE_DESC(desc);
3225 /* Should be using gpiod_get_raw_value_cansleep() */
3226 WARN_ON(desc->gdev->can_sleep);
3227 return gpiod_get_raw_value_commit(desc);
3228 }
3229 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
3230
3231 /**
3232 * gpiod_get_value() - return a gpio's value
3233 * @desc: gpio whose value will be returned
3234 *
3235 * Returns:
3236 * The GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3237 * account, or negative errno on failure.
3238 *
3239 * This function can be called from contexts where we cannot sleep, and will
3240 * complain if the GPIO chip functions potentially sleep.
3241 */
gpiod_get_value(const struct gpio_desc * desc)3242 int gpiod_get_value(const struct gpio_desc *desc)
3243 {
3244 int value;
3245
3246 VALIDATE_DESC(desc);
3247 /* Should be using gpiod_get_value_cansleep() */
3248 WARN_ON(desc->gdev->can_sleep);
3249
3250 value = gpiod_get_raw_value_commit(desc);
3251 if (value < 0)
3252 return value;
3253
3254 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3255 value = !value;
3256
3257 return value;
3258 }
3259 EXPORT_SYMBOL_GPL(gpiod_get_value);
3260
3261 /**
3262 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3263 * @array_size: number of elements in the descriptor array / value bitmap
3264 * @desc_array: array of GPIO descriptors whose values will be read
3265 * @array_info: information on applicability of fast bitmap processing path
3266 * @value_bitmap: bitmap to store the read values
3267 *
3268 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3269 * without regard for their ACTIVE_LOW status.
3270 *
3271 * This function can be called from contexts where we cannot sleep,
3272 * and it will complain if the GPIO chip functions potentially sleep.
3273 *
3274 * Returns:
3275 * 0 on success, or negative errno on failure.
3276 */
gpiod_get_raw_array_value(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)3277 int gpiod_get_raw_array_value(unsigned int array_size,
3278 struct gpio_desc **desc_array,
3279 struct gpio_array *array_info,
3280 unsigned long *value_bitmap)
3281 {
3282 if (!desc_array)
3283 return -EINVAL;
3284 return gpiod_get_array_value_complex(true, false, array_size,
3285 desc_array, array_info,
3286 value_bitmap);
3287 }
3288 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3289
3290 /**
3291 * gpiod_get_array_value() - read values from an array of GPIOs
3292 * @array_size: number of elements in the descriptor array / value bitmap
3293 * @desc_array: array of GPIO descriptors whose values will be read
3294 * @array_info: information on applicability of fast bitmap processing path
3295 * @value_bitmap: bitmap to store the read values
3296 *
3297 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3298 * into account.
3299 *
3300 * This function can be called from contexts where we cannot sleep,
3301 * and it will complain if the GPIO chip functions potentially sleep.
3302 *
3303 * Returns:
3304 * 0 on success, or negative errno on failure.
3305 */
gpiod_get_array_value(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)3306 int gpiod_get_array_value(unsigned int array_size,
3307 struct gpio_desc **desc_array,
3308 struct gpio_array *array_info,
3309 unsigned long *value_bitmap)
3310 {
3311 if (!desc_array)
3312 return -EINVAL;
3313 return gpiod_get_array_value_complex(false, false, array_size,
3314 desc_array, array_info,
3315 value_bitmap);
3316 }
3317 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3318
3319 /*
3320 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3321 * @desc: gpio descriptor whose state need to be set.
3322 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3323 */
gpio_set_open_drain_value_commit(struct gpio_desc * desc,bool value)3324 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
3325 {
3326 int ret = 0, offset = gpio_chip_hwgpio(desc);
3327
3328 CLASS(gpio_chip_guard, guard)(desc);
3329 if (!guard.gc)
3330 return;
3331
3332 if (value) {
3333 ret = guard.gc->direction_input(guard.gc, offset);
3334 } else {
3335 ret = guard.gc->direction_output(guard.gc, offset, 0);
3336 if (!ret)
3337 set_bit(FLAG_IS_OUT, &desc->flags);
3338 }
3339 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3340 if (ret < 0)
3341 gpiod_err(desc,
3342 "%s: Error in set_value for open drain err %d\n",
3343 __func__, ret);
3344 }
3345
3346 /*
3347 * _gpio_set_open_source_value() - Set the open source gpio's value.
3348 * @desc: gpio descriptor whose state need to be set.
3349 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3350 */
gpio_set_open_source_value_commit(struct gpio_desc * desc,bool value)3351 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3352 {
3353 int ret = 0, offset = gpio_chip_hwgpio(desc);
3354
3355 CLASS(gpio_chip_guard, guard)(desc);
3356 if (!guard.gc)
3357 return;
3358
3359 if (value) {
3360 ret = guard.gc->direction_output(guard.gc, offset, 1);
3361 if (!ret)
3362 set_bit(FLAG_IS_OUT, &desc->flags);
3363 } else {
3364 ret = guard.gc->direction_input(guard.gc, offset);
3365 }
3366 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3367 if (ret < 0)
3368 gpiod_err(desc,
3369 "%s: Error in set_value for open source err %d\n",
3370 __func__, ret);
3371 }
3372
gpiod_set_raw_value_commit(struct gpio_desc * desc,bool value)3373 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3374 {
3375 CLASS(gpio_chip_guard, guard)(desc);
3376 if (!guard.gc)
3377 return;
3378
3379 trace_gpio_value(desc_to_gpio(desc), 0, value);
3380 guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), value);
3381 }
3382
3383 /*
3384 * set multiple outputs on the same chip;
3385 * use the chip's set_multiple function if available;
3386 * otherwise set the outputs sequentially;
3387 * @chip: the GPIO chip we operate on
3388 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3389 * defines which outputs are to be changed
3390 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3391 * defines the values the outputs specified by mask are to be set to
3392 */
gpio_chip_set_multiple(struct gpio_chip * gc,unsigned long * mask,unsigned long * bits)3393 static void gpio_chip_set_multiple(struct gpio_chip *gc,
3394 unsigned long *mask, unsigned long *bits)
3395 {
3396 if (gc->set_multiple) {
3397 gc->set_multiple(gc, mask, bits);
3398 } else {
3399 unsigned int i;
3400
3401 /* set outputs if the corresponding mask bit is set */
3402 for_each_set_bit(i, mask, gc->ngpio)
3403 gc->set(gc, i, test_bit(i, bits));
3404 }
3405 }
3406
gpiod_set_array_value_complex(bool raw,bool can_sleep,unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)3407 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3408 unsigned int array_size,
3409 struct gpio_desc **desc_array,
3410 struct gpio_array *array_info,
3411 unsigned long *value_bitmap)
3412 {
3413 int i = 0;
3414
3415 /*
3416 * Validate array_info against desc_array and its size.
3417 * It should immediately follow desc_array if both
3418 * have been obtained from the same gpiod_get_array() call.
3419 */
3420 if (array_info && array_info->desc == desc_array &&
3421 array_size <= array_info->size &&
3422 (void *)array_info == desc_array + array_info->size) {
3423 if (!can_sleep)
3424 WARN_ON(array_info->chip->can_sleep);
3425
3426 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3427 bitmap_xor(value_bitmap, value_bitmap,
3428 array_info->invert_mask, array_size);
3429
3430 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3431 value_bitmap);
3432
3433 i = find_first_zero_bit(array_info->set_mask, array_size);
3434 if (i == array_size)
3435 return 0;
3436 } else {
3437 array_info = NULL;
3438 }
3439
3440 while (i < array_size) {
3441 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
3442 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
3443 unsigned long *mask, *bits;
3444 int count = 0;
3445
3446 CLASS(gpio_chip_guard, guard)(desc_array[i]);
3447 if (!guard.gc)
3448 return -ENODEV;
3449
3450 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) {
3451 mask = fastpath_mask;
3452 bits = fastpath_bits;
3453 } else {
3454 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
3455
3456 mask = bitmap_alloc(guard.gc->ngpio, flags);
3457 if (!mask)
3458 return -ENOMEM;
3459
3460 bits = bitmap_alloc(guard.gc->ngpio, flags);
3461 if (!bits) {
3462 bitmap_free(mask);
3463 return -ENOMEM;
3464 }
3465 }
3466
3467 bitmap_zero(mask, guard.gc->ngpio);
3468
3469 if (!can_sleep)
3470 WARN_ON(guard.gc->can_sleep);
3471
3472 do {
3473 struct gpio_desc *desc = desc_array[i];
3474 int hwgpio = gpio_chip_hwgpio(desc);
3475 int value = test_bit(i, value_bitmap);
3476
3477 /*
3478 * Pins applicable for fast input but not for
3479 * fast output processing may have been already
3480 * inverted inside the fast path, skip them.
3481 */
3482 if (!raw && !(array_info &&
3483 test_bit(i, array_info->invert_mask)) &&
3484 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3485 value = !value;
3486 trace_gpio_value(desc_to_gpio(desc), 0, value);
3487 /*
3488 * collect all normal outputs belonging to the same chip
3489 * open drain and open source outputs are set individually
3490 */
3491 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3492 gpio_set_open_drain_value_commit(desc, value);
3493 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3494 gpio_set_open_source_value_commit(desc, value);
3495 } else {
3496 __set_bit(hwgpio, mask);
3497 __assign_bit(hwgpio, bits, value);
3498 count++;
3499 }
3500 i++;
3501
3502 if (array_info)
3503 i = find_next_zero_bit(array_info->set_mask,
3504 array_size, i);
3505 } while ((i < array_size) &&
3506 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc));
3507 /* push collected bits to outputs */
3508 if (count != 0)
3509 gpio_chip_set_multiple(guard.gc, mask, bits);
3510
3511 if (mask != fastpath_mask)
3512 bitmap_free(mask);
3513 if (bits != fastpath_bits)
3514 bitmap_free(bits);
3515 }
3516 return 0;
3517 }
3518
3519 /**
3520 * gpiod_set_raw_value() - assign a gpio's raw value
3521 * @desc: gpio whose value will be assigned
3522 * @value: value to assign
3523 *
3524 * Set the raw value of the GPIO, i.e. the value of its physical line without
3525 * regard for its ACTIVE_LOW status.
3526 *
3527 * This function can be called from contexts where we cannot sleep, and will
3528 * complain if the GPIO chip functions potentially sleep.
3529 */
gpiod_set_raw_value(struct gpio_desc * desc,int value)3530 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3531 {
3532 VALIDATE_DESC_VOID(desc);
3533 /* Should be using gpiod_set_raw_value_cansleep() */
3534 WARN_ON(desc->gdev->can_sleep);
3535 gpiod_set_raw_value_commit(desc, value);
3536 }
3537 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3538
3539 /**
3540 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3541 * @desc: the descriptor to set the value on
3542 * @value: value to set
3543 *
3544 * This sets the value of a GPIO line backing a descriptor, applying
3545 * different semantic quirks like active low and open drain/source
3546 * handling.
3547 */
gpiod_set_value_nocheck(struct gpio_desc * desc,int value)3548 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3549 {
3550 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3551 value = !value;
3552 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3553 gpio_set_open_drain_value_commit(desc, value);
3554 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3555 gpio_set_open_source_value_commit(desc, value);
3556 else
3557 gpiod_set_raw_value_commit(desc, value);
3558 }
3559
3560 /**
3561 * gpiod_set_value() - assign a gpio's value
3562 * @desc: gpio whose value will be assigned
3563 * @value: value to assign
3564 *
3565 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3566 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3567 *
3568 * This function can be called from contexts where we cannot sleep, and will
3569 * complain if the GPIO chip functions potentially sleep.
3570 */
gpiod_set_value(struct gpio_desc * desc,int value)3571 void gpiod_set_value(struct gpio_desc *desc, int value)
3572 {
3573 VALIDATE_DESC_VOID(desc);
3574 /* Should be using gpiod_set_value_cansleep() */
3575 WARN_ON(desc->gdev->can_sleep);
3576 gpiod_set_value_nocheck(desc, value);
3577 }
3578 EXPORT_SYMBOL_GPL(gpiod_set_value);
3579
3580 /**
3581 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3582 * @array_size: number of elements in the descriptor array / value bitmap
3583 * @desc_array: array of GPIO descriptors whose values will be assigned
3584 * @array_info: information on applicability of fast bitmap processing path
3585 * @value_bitmap: bitmap of values to assign
3586 *
3587 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3588 * without regard for their ACTIVE_LOW status.
3589 *
3590 * This function can be called from contexts where we cannot sleep, and will
3591 * complain if the GPIO chip functions potentially sleep.
3592 *
3593 * Returns:
3594 * 0 on success, or negative errno on failure.
3595 */
gpiod_set_raw_array_value(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)3596 int gpiod_set_raw_array_value(unsigned int array_size,
3597 struct gpio_desc **desc_array,
3598 struct gpio_array *array_info,
3599 unsigned long *value_bitmap)
3600 {
3601 if (!desc_array)
3602 return -EINVAL;
3603 return gpiod_set_array_value_complex(true, false, array_size,
3604 desc_array, array_info, value_bitmap);
3605 }
3606 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3607
3608 /**
3609 * gpiod_set_array_value() - assign values to an array of GPIOs
3610 * @array_size: number of elements in the descriptor array / value bitmap
3611 * @desc_array: array of GPIO descriptors whose values will be assigned
3612 * @array_info: information on applicability of fast bitmap processing path
3613 * @value_bitmap: bitmap of values to assign
3614 *
3615 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3616 * into account.
3617 *
3618 * This function can be called from contexts where we cannot sleep, and will
3619 * complain if the GPIO chip functions potentially sleep.
3620 *
3621 * Returns:
3622 * 0 on success, or negative errno on failure.
3623 */
gpiod_set_array_value(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)3624 int gpiod_set_array_value(unsigned int array_size,
3625 struct gpio_desc **desc_array,
3626 struct gpio_array *array_info,
3627 unsigned long *value_bitmap)
3628 {
3629 if (!desc_array)
3630 return -EINVAL;
3631 return gpiod_set_array_value_complex(false, false, array_size,
3632 desc_array, array_info,
3633 value_bitmap);
3634 }
3635 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3636
3637 /**
3638 * gpiod_cansleep() - report whether gpio value access may sleep
3639 * @desc: gpio to check
3640 *
3641 * Returns:
3642 * 0 for non-sleepable, 1 for sleepable, or an error code in case of error.
3643 */
gpiod_cansleep(const struct gpio_desc * desc)3644 int gpiod_cansleep(const struct gpio_desc *desc)
3645 {
3646 VALIDATE_DESC(desc);
3647 return desc->gdev->can_sleep;
3648 }
3649 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3650
3651 /**
3652 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3653 * @desc: gpio to set the consumer name on
3654 * @name: the new consumer name
3655 *
3656 * Returns:
3657 * 0 on success, or negative errno on failure.
3658 */
gpiod_set_consumer_name(struct gpio_desc * desc,const char * name)3659 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3660 {
3661 VALIDATE_DESC(desc);
3662
3663 return desc_set_label(desc, name);
3664 }
3665 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3666
3667 /**
3668 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3669 * @desc: gpio whose IRQ will be returned (already requested)
3670 *
3671 * Returns:
3672 * The IRQ corresponding to the passed GPIO, or an error code in case of error.
3673 */
gpiod_to_irq(const struct gpio_desc * desc)3674 int gpiod_to_irq(const struct gpio_desc *desc)
3675 {
3676 struct gpio_device *gdev;
3677 struct gpio_chip *gc;
3678 int offset;
3679
3680 /*
3681 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3682 * requires this function to not return zero on an invalid descriptor
3683 * but rather a negative error number.
3684 */
3685 if (IS_ERR_OR_NULL(desc))
3686 return -EINVAL;
3687
3688 gdev = desc->gdev;
3689 /* FIXME Cannot use gpio_chip_guard due to const desc. */
3690 guard(srcu)(&gdev->srcu);
3691 gc = srcu_dereference(gdev->chip, &gdev->srcu);
3692 if (!gc)
3693 return -ENODEV;
3694
3695 offset = gpio_chip_hwgpio(desc);
3696 if (gc->to_irq) {
3697 int retirq = gc->to_irq(gc, offset);
3698
3699 /* Zero means NO_IRQ */
3700 if (!retirq)
3701 return -ENXIO;
3702
3703 return retirq;
3704 }
3705 #ifdef CONFIG_GPIOLIB_IRQCHIP
3706 if (gc->irq.chip) {
3707 /*
3708 * Avoid race condition with other code, which tries to lookup
3709 * an IRQ before the irqchip has been properly registered,
3710 * i.e. while gpiochip is still being brought up.
3711 */
3712 return -EPROBE_DEFER;
3713 }
3714 #endif
3715 return -ENXIO;
3716 }
3717 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3718
3719 /**
3720 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3721 * @gc: the chip the GPIO to lock belongs to
3722 * @offset: the offset of the GPIO to lock as IRQ
3723 *
3724 * This is used directly by GPIO drivers that want to lock down
3725 * a certain GPIO line to be used for IRQs.
3726 *
3727 * Returns:
3728 * 0 on success, or negative errno on failure.
3729 */
gpiochip_lock_as_irq(struct gpio_chip * gc,unsigned int offset)3730 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3731 {
3732 struct gpio_desc *desc;
3733
3734 desc = gpiochip_get_desc(gc, offset);
3735 if (IS_ERR(desc))
3736 return PTR_ERR(desc);
3737
3738 /*
3739 * If it's fast: flush the direction setting if something changed
3740 * behind our back
3741 */
3742 if (!gc->can_sleep && gc->get_direction) {
3743 int dir = gpiod_get_direction(desc);
3744
3745 if (dir < 0) {
3746 chip_err(gc, "%s: cannot get GPIO direction\n",
3747 __func__);
3748 return dir;
3749 }
3750 }
3751
3752 /* To be valid for IRQ the line needs to be input or open drain */
3753 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3754 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3755 chip_err(gc,
3756 "%s: tried to flag a GPIO set as output for IRQ\n",
3757 __func__);
3758 return -EIO;
3759 }
3760
3761 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3762 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3763
3764 return 0;
3765 }
3766 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3767
3768 /**
3769 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3770 * @gc: the chip the GPIO to lock belongs to
3771 * @offset: the offset of the GPIO to lock as IRQ
3772 *
3773 * This is used directly by GPIO drivers that want to indicate
3774 * that a certain GPIO is no longer used exclusively for IRQ.
3775 */
gpiochip_unlock_as_irq(struct gpio_chip * gc,unsigned int offset)3776 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3777 {
3778 struct gpio_desc *desc;
3779
3780 desc = gpiochip_get_desc(gc, offset);
3781 if (IS_ERR(desc))
3782 return;
3783
3784 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3785 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3786 }
3787 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3788
gpiochip_disable_irq(struct gpio_chip * gc,unsigned int offset)3789 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3790 {
3791 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3792
3793 if (!IS_ERR(desc) &&
3794 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3795 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3796 }
3797 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3798
gpiochip_enable_irq(struct gpio_chip * gc,unsigned int offset)3799 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3800 {
3801 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3802
3803 if (!IS_ERR(desc) &&
3804 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3805 /*
3806 * We must not be output when using IRQ UNLESS we are
3807 * open drain.
3808 */
3809 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3810 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3811 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3812 }
3813 }
3814 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3815
gpiochip_line_is_irq(struct gpio_chip * gc,unsigned int offset)3816 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3817 {
3818 if (offset >= gc->ngpio)
3819 return false;
3820
3821 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3822 }
3823 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3824
gpiochip_reqres_irq(struct gpio_chip * gc,unsigned int offset)3825 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3826 {
3827 int ret;
3828
3829 if (!try_module_get(gc->gpiodev->owner))
3830 return -ENODEV;
3831
3832 ret = gpiochip_lock_as_irq(gc, offset);
3833 if (ret) {
3834 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3835 module_put(gc->gpiodev->owner);
3836 return ret;
3837 }
3838 return 0;
3839 }
3840 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3841
gpiochip_relres_irq(struct gpio_chip * gc,unsigned int offset)3842 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3843 {
3844 gpiochip_unlock_as_irq(gc, offset);
3845 module_put(gc->gpiodev->owner);
3846 }
3847 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3848
gpiochip_line_is_open_drain(struct gpio_chip * gc,unsigned int offset)3849 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3850 {
3851 if (offset >= gc->ngpio)
3852 return false;
3853
3854 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3855 }
3856 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3857
gpiochip_line_is_open_source(struct gpio_chip * gc,unsigned int offset)3858 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3859 {
3860 if (offset >= gc->ngpio)
3861 return false;
3862
3863 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3864 }
3865 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3866
gpiochip_line_is_persistent(struct gpio_chip * gc,unsigned int offset)3867 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3868 {
3869 if (offset >= gc->ngpio)
3870 return false;
3871
3872 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3873 }
3874 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3875
3876 /**
3877 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3878 * @desc: gpio whose value will be returned
3879 *
3880 * Returns:
3881 * The GPIO's raw value, i.e. the value of the physical line disregarding
3882 * its ACTIVE_LOW status, or negative errno on failure.
3883 *
3884 * This function is to be called from contexts that can sleep.
3885 */
gpiod_get_raw_value_cansleep(const struct gpio_desc * desc)3886 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3887 {
3888 might_sleep();
3889 VALIDATE_DESC(desc);
3890 return gpiod_get_raw_value_commit(desc);
3891 }
3892 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3893
3894 /**
3895 * gpiod_get_value_cansleep() - return a gpio's value
3896 * @desc: gpio whose value will be returned
3897 *
3898 * Returns:
3899 * The GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3900 * account, or negative errno on failure.
3901 *
3902 * This function is to be called from contexts that can sleep.
3903 */
gpiod_get_value_cansleep(const struct gpio_desc * desc)3904 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3905 {
3906 int value;
3907
3908 might_sleep();
3909 VALIDATE_DESC(desc);
3910 value = gpiod_get_raw_value_commit(desc);
3911 if (value < 0)
3912 return value;
3913
3914 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3915 value = !value;
3916
3917 return value;
3918 }
3919 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3920
3921 /**
3922 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3923 * @array_size: number of elements in the descriptor array / value bitmap
3924 * @desc_array: array of GPIO descriptors whose values will be read
3925 * @array_info: information on applicability of fast bitmap processing path
3926 * @value_bitmap: bitmap to store the read values
3927 *
3928 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3929 * without regard for their ACTIVE_LOW status.
3930 *
3931 * This function is to be called from contexts that can sleep.
3932 *
3933 * Returns:
3934 * 0 on success, or negative errno on failure.
3935 */
gpiod_get_raw_array_value_cansleep(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)3936 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3937 struct gpio_desc **desc_array,
3938 struct gpio_array *array_info,
3939 unsigned long *value_bitmap)
3940 {
3941 might_sleep();
3942 if (!desc_array)
3943 return -EINVAL;
3944 return gpiod_get_array_value_complex(true, true, array_size,
3945 desc_array, array_info,
3946 value_bitmap);
3947 }
3948 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3949
3950 /**
3951 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3952 * @array_size: number of elements in the descriptor array / value bitmap
3953 * @desc_array: array of GPIO descriptors whose values will be read
3954 * @array_info: information on applicability of fast bitmap processing path
3955 * @value_bitmap: bitmap to store the read values
3956 *
3957 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3958 * into account.
3959 *
3960 * This function is to be called from contexts that can sleep.
3961 *
3962 * Returns:
3963 * 0 on success, or negative errno on failure.
3964 */
gpiod_get_array_value_cansleep(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)3965 int gpiod_get_array_value_cansleep(unsigned int array_size,
3966 struct gpio_desc **desc_array,
3967 struct gpio_array *array_info,
3968 unsigned long *value_bitmap)
3969 {
3970 might_sleep();
3971 if (!desc_array)
3972 return -EINVAL;
3973 return gpiod_get_array_value_complex(false, true, array_size,
3974 desc_array, array_info,
3975 value_bitmap);
3976 }
3977 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3978
3979 /**
3980 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3981 * @desc: gpio whose value will be assigned
3982 * @value: value to assign
3983 *
3984 * Set the raw value of the GPIO, i.e. the value of its physical line without
3985 * regard for its ACTIVE_LOW status.
3986 *
3987 * This function is to be called from contexts that can sleep.
3988 */
gpiod_set_raw_value_cansleep(struct gpio_desc * desc,int value)3989 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3990 {
3991 might_sleep();
3992 VALIDATE_DESC_VOID(desc);
3993 gpiod_set_raw_value_commit(desc, value);
3994 }
3995 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3996
3997 /**
3998 * gpiod_set_value_cansleep() - assign a gpio's value
3999 * @desc: gpio whose value will be assigned
4000 * @value: value to assign
4001 *
4002 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4003 * account
4004 *
4005 * This function is to be called from contexts that can sleep.
4006 */
gpiod_set_value_cansleep(struct gpio_desc * desc,int value)4007 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
4008 {
4009 might_sleep();
4010 VALIDATE_DESC_VOID(desc);
4011 gpiod_set_value_nocheck(desc, value);
4012 }
4013 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
4014
4015 /**
4016 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
4017 * @array_size: number of elements in the descriptor array / value bitmap
4018 * @desc_array: array of GPIO descriptors whose values will be assigned
4019 * @array_info: information on applicability of fast bitmap processing path
4020 * @value_bitmap: bitmap of values to assign
4021 *
4022 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4023 * without regard for their ACTIVE_LOW status.
4024 *
4025 * This function is to be called from contexts that can sleep.
4026 *
4027 * Returns:
4028 * 0 on success, or negative errno on failure.
4029 */
gpiod_set_raw_array_value_cansleep(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)4030 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
4031 struct gpio_desc **desc_array,
4032 struct gpio_array *array_info,
4033 unsigned long *value_bitmap)
4034 {
4035 might_sleep();
4036 if (!desc_array)
4037 return -EINVAL;
4038 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
4039 array_info, value_bitmap);
4040 }
4041 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
4042
4043 /**
4044 * gpiod_add_lookup_tables() - register GPIO device consumers
4045 * @tables: list of tables of consumers to register
4046 * @n: number of tables in the list
4047 */
gpiod_add_lookup_tables(struct gpiod_lookup_table ** tables,size_t n)4048 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4049 {
4050 unsigned int i;
4051
4052 mutex_lock(&gpio_lookup_lock);
4053
4054 for (i = 0; i < n; i++)
4055 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4056
4057 mutex_unlock(&gpio_lookup_lock);
4058 }
4059
4060 /**
4061 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
4062 * @array_size: number of elements in the descriptor array / value bitmap
4063 * @desc_array: array of GPIO descriptors whose values will be assigned
4064 * @array_info: information on applicability of fast bitmap processing path
4065 * @value_bitmap: bitmap of values to assign
4066 *
4067 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4068 * into account.
4069 *
4070 * This function is to be called from contexts that can sleep.
4071 *
4072 * Returns:
4073 * 0 on success, or negative errno on failure.
4074 */
gpiod_set_array_value_cansleep(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)4075 int gpiod_set_array_value_cansleep(unsigned int array_size,
4076 struct gpio_desc **desc_array,
4077 struct gpio_array *array_info,
4078 unsigned long *value_bitmap)
4079 {
4080 might_sleep();
4081 if (!desc_array)
4082 return -EINVAL;
4083 return gpiod_set_array_value_complex(false, true, array_size,
4084 desc_array, array_info,
4085 value_bitmap);
4086 }
4087 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
4088
gpiod_line_state_notify(struct gpio_desc * desc,unsigned long action)4089 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action)
4090 {
4091 blocking_notifier_call_chain(&desc->gdev->line_state_notifier,
4092 action, desc);
4093 }
4094
4095 /**
4096 * gpiod_add_lookup_table() - register GPIO device consumers
4097 * @table: table of consumers to register
4098 */
gpiod_add_lookup_table(struct gpiod_lookup_table * table)4099 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
4100 {
4101 gpiod_add_lookup_tables(&table, 1);
4102 }
4103 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
4104
4105 /**
4106 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4107 * @table: table of consumers to unregister
4108 */
gpiod_remove_lookup_table(struct gpiod_lookup_table * table)4109 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4110 {
4111 /* Nothing to remove */
4112 if (!table)
4113 return;
4114
4115 mutex_lock(&gpio_lookup_lock);
4116
4117 list_del(&table->list);
4118
4119 mutex_unlock(&gpio_lookup_lock);
4120 }
4121 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
4122
4123 /**
4124 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4125 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4126 */
gpiod_add_hogs(struct gpiod_hog * hogs)4127 void gpiod_add_hogs(struct gpiod_hog *hogs)
4128 {
4129 struct gpiod_hog *hog;
4130
4131 mutex_lock(&gpio_machine_hogs_mutex);
4132
4133 for (hog = &hogs[0]; hog->chip_label; hog++) {
4134 list_add_tail(&hog->list, &gpio_machine_hogs);
4135
4136 /*
4137 * The chip may have been registered earlier, so check if it
4138 * exists and, if so, try to hog the line now.
4139 */
4140 struct gpio_device *gdev __free(gpio_device_put) =
4141 gpio_device_find_by_label(hog->chip_label);
4142 if (gdev)
4143 gpiochip_machine_hog(gpio_device_get_chip(gdev), hog);
4144 }
4145
4146 mutex_unlock(&gpio_machine_hogs_mutex);
4147 }
4148 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4149
gpiod_remove_hogs(struct gpiod_hog * hogs)4150 void gpiod_remove_hogs(struct gpiod_hog *hogs)
4151 {
4152 struct gpiod_hog *hog;
4153
4154 mutex_lock(&gpio_machine_hogs_mutex);
4155 for (hog = &hogs[0]; hog->chip_label; hog++)
4156 list_del(&hog->list);
4157 mutex_unlock(&gpio_machine_hogs_mutex);
4158 }
4159 EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
4160
gpiod_find_lookup_table(struct device * dev)4161 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4162 {
4163 const char *dev_id = dev ? dev_name(dev) : NULL;
4164 struct gpiod_lookup_table *table;
4165
4166 list_for_each_entry(table, &gpio_lookup_list, list) {
4167 if (table->dev_id && dev_id) {
4168 /*
4169 * Valid strings on both ends, must be identical to have
4170 * a match
4171 */
4172 if (!strcmp(table->dev_id, dev_id))
4173 return table;
4174 } else {
4175 /*
4176 * One of the pointers is NULL, so both must be to have
4177 * a match
4178 */
4179 if (dev_id == table->dev_id)
4180 return table;
4181 }
4182 }
4183
4184 return NULL;
4185 }
4186
gpiod_find(struct device * dev,const char * con_id,unsigned int idx,unsigned long * flags)4187 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
4188 unsigned int idx, unsigned long *flags)
4189 {
4190 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4191 struct gpiod_lookup_table *table;
4192 struct gpiod_lookup *p;
4193 struct gpio_chip *gc;
4194
4195 guard(mutex)(&gpio_lookup_lock);
4196
4197 table = gpiod_find_lookup_table(dev);
4198 if (!table)
4199 return desc;
4200
4201 for (p = &table->table[0]; p->key; p++) {
4202 /* idx must always match exactly */
4203 if (p->idx != idx)
4204 continue;
4205
4206 /* If the lookup entry has a con_id, require exact match */
4207 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4208 continue;
4209
4210 if (p->chip_hwnum == U16_MAX) {
4211 desc = gpio_name_to_desc(p->key);
4212 if (desc) {
4213 *flags = p->flags;
4214 return desc;
4215 }
4216
4217 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
4218 p->key);
4219 return ERR_PTR(-EPROBE_DEFER);
4220 }
4221
4222 struct gpio_device *gdev __free(gpio_device_put) =
4223 gpio_device_find_by_label(p->key);
4224 if (!gdev) {
4225 /*
4226 * As the lookup table indicates a chip with
4227 * p->key should exist, assume it may
4228 * still appear later and let the interested
4229 * consumer be probed again or let the Deferred
4230 * Probe infrastructure handle the error.
4231 */
4232 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4233 p->key);
4234 return ERR_PTR(-EPROBE_DEFER);
4235 }
4236
4237 gc = gpio_device_get_chip(gdev);
4238
4239 if (gc->ngpio <= p->chip_hwnum) {
4240 dev_err(dev,
4241 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4242 idx, p->chip_hwnum, gc->ngpio - 1,
4243 gc->label);
4244 return ERR_PTR(-EINVAL);
4245 }
4246
4247 desc = gpio_device_get_desc(gdev, p->chip_hwnum);
4248 *flags = p->flags;
4249
4250 return desc;
4251 }
4252
4253 return desc;
4254 }
4255
platform_gpio_count(struct device * dev,const char * con_id)4256 static int platform_gpio_count(struct device *dev, const char *con_id)
4257 {
4258 struct gpiod_lookup_table *table;
4259 struct gpiod_lookup *p;
4260 unsigned int count = 0;
4261
4262 scoped_guard(mutex, &gpio_lookup_lock) {
4263 table = gpiod_find_lookup_table(dev);
4264 if (!table)
4265 return -ENOENT;
4266
4267 for (p = &table->table[0]; p->key; p++) {
4268 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4269 (!con_id && !p->con_id))
4270 count++;
4271 }
4272 }
4273
4274 if (!count)
4275 return -ENOENT;
4276
4277 return count;
4278 }
4279
gpiod_find_by_fwnode(struct fwnode_handle * fwnode,struct device * consumer,const char * con_id,unsigned int idx,enum gpiod_flags * flags,unsigned long * lookupflags)4280 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
4281 struct device *consumer,
4282 const char *con_id,
4283 unsigned int idx,
4284 enum gpiod_flags *flags,
4285 unsigned long *lookupflags)
4286 {
4287 const char *name = function_name_or_default(con_id);
4288 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4289
4290 if (is_of_node(fwnode)) {
4291 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n", fwnode, name);
4292 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags);
4293 } else if (is_acpi_node(fwnode)) {
4294 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n", fwnode, name);
4295 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
4296 } else if (is_software_node(fwnode)) {
4297 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n", fwnode, name);
4298 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
4299 }
4300
4301 return desc;
4302 }
4303
gpiod_fwnode_lookup(struct fwnode_handle * fwnode,struct device * consumer,const char * con_id,unsigned int idx,enum gpiod_flags * flags,unsigned long * lookupflags)4304 static struct gpio_desc *gpiod_fwnode_lookup(struct fwnode_handle *fwnode,
4305 struct device *consumer,
4306 const char *con_id,
4307 unsigned int idx,
4308 enum gpiod_flags *flags,
4309 unsigned long *lookupflags)
4310 {
4311 struct gpio_desc *desc;
4312
4313 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, flags, lookupflags);
4314 if (gpiod_not_found(desc) && !IS_ERR_OR_NULL(fwnode))
4315 desc = gpiod_find_by_fwnode(fwnode->secondary, consumer, con_id,
4316 idx, flags, lookupflags);
4317
4318 return desc;
4319 }
4320
gpiod_find_and_request(struct device * consumer,struct fwnode_handle * fwnode,const char * con_id,unsigned int idx,enum gpiod_flags flags,const char * label,bool platform_lookup_allowed)4321 struct gpio_desc *gpiod_find_and_request(struct device *consumer,
4322 struct fwnode_handle *fwnode,
4323 const char *con_id,
4324 unsigned int idx,
4325 enum gpiod_flags flags,
4326 const char *label,
4327 bool platform_lookup_allowed)
4328 {
4329 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4330 const char *name = function_name_or_default(con_id);
4331 /*
4332 * scoped_guard() is implemented as a for loop, meaning static
4333 * analyzers will complain about these two not being initialized.
4334 */
4335 struct gpio_desc *desc = NULL;
4336 int ret = 0;
4337
4338 scoped_guard(srcu, &gpio_devices_srcu) {
4339 desc = gpiod_fwnode_lookup(fwnode, consumer, con_id, idx,
4340 &flags, &lookupflags);
4341 if (gpiod_not_found(desc) && platform_lookup_allowed) {
4342 /*
4343 * Either we are not using DT or ACPI, or their lookup
4344 * did not return a result. In that case, use platform
4345 * lookup as a fallback.
4346 */
4347 dev_dbg(consumer,
4348 "using lookup tables for GPIO lookup\n");
4349 desc = gpiod_find(consumer, con_id, idx, &lookupflags);
4350 }
4351
4352 if (IS_ERR(desc)) {
4353 dev_dbg(consumer, "No GPIO consumer %s found\n", name);
4354 return desc;
4355 }
4356
4357 /*
4358 * If a connection label was passed use that, else attempt to use
4359 * the device name as label
4360 */
4361 ret = gpiod_request(desc, label);
4362 }
4363 if (ret) {
4364 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
4365 return ERR_PTR(ret);
4366
4367 /*
4368 * This happens when there are several consumers for
4369 * the same GPIO line: we just return here without
4370 * further initialization. It is a bit of a hack.
4371 * This is necessary to support fixed regulators.
4372 *
4373 * FIXME: Make this more sane and safe.
4374 */
4375 dev_info(consumer, "nonexclusive access to GPIO for %s\n", name);
4376 return desc;
4377 }
4378
4379 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4380 if (ret < 0) {
4381 gpiod_put(desc);
4382 dev_err(consumer, "setup of GPIO %s failed: %d\n", name, ret);
4383 return ERR_PTR(ret);
4384 }
4385
4386 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_REQUESTED);
4387
4388 return desc;
4389 }
4390
4391 /**
4392 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4393 * @fwnode: handle of the firmware node
4394 * @con_id: function within the GPIO consumer
4395 * @index: index of the GPIO to obtain for the consumer
4396 * @flags: GPIO initialization flags
4397 * @label: label to attach to the requested GPIO
4398 *
4399 * This function can be used for drivers that get their configuration
4400 * from opaque firmware.
4401 *
4402 * The function properly finds the corresponding GPIO using whatever is the
4403 * underlying firmware interface and then makes sure that the GPIO
4404 * descriptor is requested before it is returned to the caller.
4405 *
4406 * Returns:
4407 * On successful request the GPIO pin is configured in accordance with
4408 * provided @flags.
4409 *
4410 * In case of error an ERR_PTR() is returned.
4411 */
fwnode_gpiod_get_index(struct fwnode_handle * fwnode,const char * con_id,int index,enum gpiod_flags flags,const char * label)4412 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4413 const char *con_id,
4414 int index,
4415 enum gpiod_flags flags,
4416 const char *label)
4417 {
4418 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false);
4419 }
4420 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4421
4422 /**
4423 * gpiod_count - return the number of GPIOs associated with a device / function
4424 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4425 * @con_id: function within the GPIO consumer
4426 *
4427 * Returns:
4428 * The number of GPIOs associated with a device / function or -ENOENT if no
4429 * GPIO has been assigned to the requested function.
4430 */
gpiod_count(struct device * dev,const char * con_id)4431 int gpiod_count(struct device *dev, const char *con_id)
4432 {
4433 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4434 int count = -ENOENT;
4435
4436 if (is_of_node(fwnode))
4437 count = of_gpio_count(fwnode, con_id);
4438 else if (is_acpi_node(fwnode))
4439 count = acpi_gpio_count(fwnode, con_id);
4440 else if (is_software_node(fwnode))
4441 count = swnode_gpio_count(fwnode, con_id);
4442
4443 if (count < 0)
4444 count = platform_gpio_count(dev, con_id);
4445
4446 return count;
4447 }
4448 EXPORT_SYMBOL_GPL(gpiod_count);
4449
4450 /**
4451 * gpiod_get - obtain a GPIO for a given GPIO function
4452 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4453 * @con_id: function within the GPIO consumer
4454 * @flags: optional GPIO initialization flags
4455 *
4456 * Returns:
4457 * The GPIO descriptor corresponding to the function @con_id of device
4458 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4459 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4460 */
gpiod_get(struct device * dev,const char * con_id,enum gpiod_flags flags)4461 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4462 enum gpiod_flags flags)
4463 {
4464 return gpiod_get_index(dev, con_id, 0, flags);
4465 }
4466 EXPORT_SYMBOL_GPL(gpiod_get);
4467
4468 /**
4469 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4470 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4471 * @con_id: function within the GPIO consumer
4472 * @flags: optional GPIO initialization flags
4473 *
4474 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4475 * the requested function it will return NULL. This is convenient for drivers
4476 * that need to handle optional GPIOs.
4477 *
4478 * Returns:
4479 * The GPIO descriptor corresponding to the function @con_id of device
4480 * dev, NULL if no GPIO has been assigned to the requested function, or
4481 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4482 */
gpiod_get_optional(struct device * dev,const char * con_id,enum gpiod_flags flags)4483 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4484 const char *con_id,
4485 enum gpiod_flags flags)
4486 {
4487 return gpiod_get_index_optional(dev, con_id, 0, flags);
4488 }
4489 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4490
4491
4492 /**
4493 * gpiod_configure_flags - helper function to configure a given GPIO
4494 * @desc: gpio whose value will be assigned
4495 * @con_id: function within the GPIO consumer
4496 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4497 * of_find_gpio() or of_get_gpio_hog()
4498 * @dflags: gpiod_flags - optional GPIO initialization flags
4499 *
4500 * Returns:
4501 * 0 on success, -ENOENT if no GPIO has been assigned to the
4502 * requested function and/or index, or another IS_ERR() code if an error
4503 * occurred while trying to acquire the GPIO.
4504 */
gpiod_configure_flags(struct gpio_desc * desc,const char * con_id,unsigned long lflags,enum gpiod_flags dflags)4505 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4506 unsigned long lflags, enum gpiod_flags dflags)
4507 {
4508 const char *name = function_name_or_default(con_id);
4509 int ret;
4510
4511 if (lflags & GPIO_ACTIVE_LOW)
4512 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4513
4514 if (lflags & GPIO_OPEN_DRAIN)
4515 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4516 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4517 /*
4518 * This enforces open drain mode from the consumer side.
4519 * This is necessary for some busses like I2C, but the lookup
4520 * should *REALLY* have specified them as open drain in the
4521 * first place, so print a little warning here.
4522 */
4523 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4524 gpiod_warn(desc,
4525 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4526 }
4527
4528 if (lflags & GPIO_OPEN_SOURCE)
4529 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4530
4531 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
4532 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
4533 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
4534 gpiod_err(desc,
4535 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4536 return -EINVAL;
4537 }
4538
4539 if (lflags & GPIO_PULL_UP)
4540 set_bit(FLAG_PULL_UP, &desc->flags);
4541 else if (lflags & GPIO_PULL_DOWN)
4542 set_bit(FLAG_PULL_DOWN, &desc->flags);
4543 else if (lflags & GPIO_PULL_DISABLE)
4544 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
4545
4546 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4547 if (ret < 0)
4548 return ret;
4549
4550 /* No particular flag request, return here... */
4551 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4552 gpiod_dbg(desc, "no flags found for GPIO %s\n", name);
4553 return 0;
4554 }
4555
4556 /* Process flags */
4557 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4558 ret = gpiod_direction_output(desc,
4559 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4560 else
4561 ret = gpiod_direction_input(desc);
4562
4563 return ret;
4564 }
4565
4566 /**
4567 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4568 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4569 * @con_id: function within the GPIO consumer
4570 * @idx: index of the GPIO to obtain in the consumer
4571 * @flags: optional GPIO initialization flags
4572 *
4573 * This variant of gpiod_get() allows to access GPIOs other than the first
4574 * defined one for functions that define several GPIOs.
4575 *
4576 * Returns:
4577 * A valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4578 * requested function and/or index, or another IS_ERR() code if an error
4579 * occurred while trying to acquire the GPIO.
4580 */
gpiod_get_index(struct device * dev,const char * con_id,unsigned int idx,enum gpiod_flags flags)4581 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4582 const char *con_id,
4583 unsigned int idx,
4584 enum gpiod_flags flags)
4585 {
4586 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
4587 const char *devname = dev ? dev_name(dev) : "?";
4588 const char *label = con_id ?: devname;
4589
4590 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true);
4591 }
4592 EXPORT_SYMBOL_GPL(gpiod_get_index);
4593
4594 /**
4595 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4596 * function
4597 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4598 * @con_id: function within the GPIO consumer
4599 * @index: index of the GPIO to obtain in the consumer
4600 * @flags: optional GPIO initialization flags
4601 *
4602 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4603 * specified index was assigned to the requested function it will return NULL.
4604 * This is convenient for drivers that need to handle optional GPIOs.
4605 *
4606 * Returns:
4607 * A valid GPIO descriptor, NULL if no GPIO has been assigned to the
4608 * requested function and/or index, or another IS_ERR() code if an error
4609 * occurred while trying to acquire the GPIO.
4610 */
gpiod_get_index_optional(struct device * dev,const char * con_id,unsigned int index,enum gpiod_flags flags)4611 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4612 const char *con_id,
4613 unsigned int index,
4614 enum gpiod_flags flags)
4615 {
4616 struct gpio_desc *desc;
4617
4618 desc = gpiod_get_index(dev, con_id, index, flags);
4619 if (gpiod_not_found(desc))
4620 return NULL;
4621
4622 return desc;
4623 }
4624 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4625
4626 /**
4627 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4628 * @desc: gpio whose value will be assigned
4629 * @name: gpio line name
4630 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4631 * of_find_gpio() or of_get_gpio_hog()
4632 * @dflags: gpiod_flags - optional GPIO initialization flags
4633 *
4634 * Returns:
4635 * 0 on success, or negative errno on failure.
4636 */
gpiod_hog(struct gpio_desc * desc,const char * name,unsigned long lflags,enum gpiod_flags dflags)4637 int gpiod_hog(struct gpio_desc *desc, const char *name,
4638 unsigned long lflags, enum gpiod_flags dflags)
4639 {
4640 struct gpio_device *gdev = desc->gdev;
4641 struct gpio_desc *local_desc;
4642 int hwnum;
4643 int ret;
4644
4645 CLASS(gpio_chip_guard, guard)(desc);
4646 if (!guard.gc)
4647 return -ENODEV;
4648
4649 if (test_and_set_bit(FLAG_IS_HOGGED, &desc->flags))
4650 return 0;
4651
4652 hwnum = gpio_chip_hwgpio(desc);
4653
4654 local_desc = gpiochip_request_own_desc(guard.gc, hwnum, name,
4655 lflags, dflags);
4656 if (IS_ERR(local_desc)) {
4657 clear_bit(FLAG_IS_HOGGED, &desc->flags);
4658 ret = PTR_ERR(local_desc);
4659 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4660 name, gdev->label, hwnum, ret);
4661 return ret;
4662 }
4663
4664 gpiod_dbg(desc, "hogged as %s%s\n",
4665 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4666 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4667 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4668
4669 return 0;
4670 }
4671
4672 /**
4673 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4674 * @gc: gpio chip to act on
4675 */
gpiochip_free_hogs(struct gpio_chip * gc)4676 static void gpiochip_free_hogs(struct gpio_chip *gc)
4677 {
4678 struct gpio_desc *desc;
4679
4680 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
4681 gpiochip_free_own_desc(desc);
4682 }
4683
4684 /**
4685 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4686 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4687 * @con_id: function within the GPIO consumer
4688 * @flags: optional GPIO initialization flags
4689 *
4690 * This function acquires all the GPIOs defined under a given function.
4691 *
4692 * Returns:
4693 * The GPIO descriptors corresponding to the function @con_id of device
4694 * dev, -ENOENT if no GPIO has been assigned to the requested function,
4695 * or another IS_ERR() code if an error occurred while trying to acquire
4696 * the GPIOs.
4697 */
gpiod_get_array(struct device * dev,const char * con_id,enum gpiod_flags flags)4698 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4699 const char *con_id,
4700 enum gpiod_flags flags)
4701 {
4702 struct gpio_desc *desc;
4703 struct gpio_descs *descs;
4704 struct gpio_array *array_info = NULL;
4705 struct gpio_chip *gc;
4706 int count, bitmap_size;
4707 size_t descs_size;
4708
4709 count = gpiod_count(dev, con_id);
4710 if (count < 0)
4711 return ERR_PTR(count);
4712
4713 descs_size = struct_size(descs, desc, count);
4714 descs = kzalloc(descs_size, GFP_KERNEL);
4715 if (!descs)
4716 return ERR_PTR(-ENOMEM);
4717
4718 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) {
4719 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4720 if (IS_ERR(desc)) {
4721 gpiod_put_array(descs);
4722 return ERR_CAST(desc);
4723 }
4724
4725 descs->desc[descs->ndescs] = desc;
4726
4727 gc = gpiod_to_chip(desc);
4728 /*
4729 * If pin hardware number of array member 0 is also 0, select
4730 * its chip as a candidate for fast bitmap processing path.
4731 */
4732 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4733 struct gpio_descs *array;
4734
4735 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4736 gc->ngpio : count);
4737
4738 array = krealloc(descs, descs_size +
4739 struct_size(array_info, invert_mask, 3 * bitmap_size),
4740 GFP_KERNEL | __GFP_ZERO);
4741 if (!array) {
4742 gpiod_put_array(descs);
4743 return ERR_PTR(-ENOMEM);
4744 }
4745
4746 descs = array;
4747
4748 array_info = (void *)descs + descs_size;
4749 array_info->get_mask = array_info->invert_mask +
4750 bitmap_size;
4751 array_info->set_mask = array_info->get_mask +
4752 bitmap_size;
4753
4754 array_info->desc = descs->desc;
4755 array_info->size = count;
4756 array_info->chip = gc;
4757 bitmap_set(array_info->get_mask, descs->ndescs,
4758 count - descs->ndescs);
4759 bitmap_set(array_info->set_mask, descs->ndescs,
4760 count - descs->ndescs);
4761 descs->info = array_info;
4762 }
4763
4764 /* If there is no cache for fast bitmap processing path, continue */
4765 if (!array_info)
4766 continue;
4767
4768 /* Unmark array members which don't belong to the 'fast' chip */
4769 if (array_info->chip != gc) {
4770 __clear_bit(descs->ndescs, array_info->get_mask);
4771 __clear_bit(descs->ndescs, array_info->set_mask);
4772 }
4773 /*
4774 * Detect array members which belong to the 'fast' chip
4775 * but their pins are not in hardware order.
4776 */
4777 else if (gpio_chip_hwgpio(desc) != descs->ndescs) {
4778 /*
4779 * Don't use fast path if all array members processed so
4780 * far belong to the same chip as this one but its pin
4781 * hardware number is different from its array index.
4782 */
4783 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4784 array_info = NULL;
4785 } else {
4786 __clear_bit(descs->ndescs,
4787 array_info->get_mask);
4788 __clear_bit(descs->ndescs,
4789 array_info->set_mask);
4790 }
4791 } else {
4792 /* Exclude open drain or open source from fast output */
4793 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4794 gpiochip_line_is_open_source(gc, descs->ndescs))
4795 __clear_bit(descs->ndescs,
4796 array_info->set_mask);
4797 /* Identify 'fast' pins which require invertion */
4798 if (gpiod_is_active_low(desc))
4799 __set_bit(descs->ndescs,
4800 array_info->invert_mask);
4801 }
4802 }
4803 if (array_info)
4804 dev_dbg(dev,
4805 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4806 array_info->chip->label, array_info->size,
4807 *array_info->get_mask, *array_info->set_mask,
4808 *array_info->invert_mask);
4809 return descs;
4810 }
4811 EXPORT_SYMBOL_GPL(gpiod_get_array);
4812
4813 /**
4814 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4815 * function
4816 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4817 * @con_id: function within the GPIO consumer
4818 * @flags: optional GPIO initialization flags
4819 *
4820 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4821 * assigned to the requested function it will return NULL.
4822 *
4823 * Returns:
4824 * The GPIO descriptors corresponding to the function @con_id of device
4825 * dev, NULL if no GPIO has been assigned to the requested function,
4826 * or another IS_ERR() code if an error occurred while trying to acquire
4827 * the GPIOs.
4828 */
gpiod_get_array_optional(struct device * dev,const char * con_id,enum gpiod_flags flags)4829 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4830 const char *con_id,
4831 enum gpiod_flags flags)
4832 {
4833 struct gpio_descs *descs;
4834
4835 descs = gpiod_get_array(dev, con_id, flags);
4836 if (gpiod_not_found(descs))
4837 return NULL;
4838
4839 return descs;
4840 }
4841 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4842
4843 /**
4844 * gpiod_put - dispose of a GPIO descriptor
4845 * @desc: GPIO descriptor to dispose of
4846 *
4847 * No descriptor can be used after gpiod_put() has been called on it.
4848 */
gpiod_put(struct gpio_desc * desc)4849 void gpiod_put(struct gpio_desc *desc)
4850 {
4851 if (desc)
4852 gpiod_free(desc);
4853 }
4854 EXPORT_SYMBOL_GPL(gpiod_put);
4855
4856 /**
4857 * gpiod_put_array - dispose of multiple GPIO descriptors
4858 * @descs: struct gpio_descs containing an array of descriptors
4859 */
gpiod_put_array(struct gpio_descs * descs)4860 void gpiod_put_array(struct gpio_descs *descs)
4861 {
4862 unsigned int i;
4863
4864 for (i = 0; i < descs->ndescs; i++)
4865 gpiod_put(descs->desc[i]);
4866
4867 kfree(descs);
4868 }
4869 EXPORT_SYMBOL_GPL(gpiod_put_array);
4870
gpio_stub_drv_probe(struct device * dev)4871 static int gpio_stub_drv_probe(struct device *dev)
4872 {
4873 /*
4874 * The DT node of some GPIO chips have a "compatible" property, but
4875 * never have a struct device added and probed by a driver to register
4876 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4877 * the consumers of the GPIO chip to get probe deferred forever because
4878 * they will be waiting for a device associated with the GPIO chip
4879 * firmware node to get added and bound to a driver.
4880 *
4881 * To allow these consumers to probe, we associate the struct
4882 * gpio_device of the GPIO chip with the firmware node and then simply
4883 * bind it to this stub driver.
4884 */
4885 return 0;
4886 }
4887
4888 static struct device_driver gpio_stub_drv = {
4889 .name = "gpio_stub_drv",
4890 .bus = &gpio_bus_type,
4891 .probe = gpio_stub_drv_probe,
4892 };
4893
gpiolib_dev_init(void)4894 static int __init gpiolib_dev_init(void)
4895 {
4896 int ret;
4897
4898 /* Register GPIO sysfs bus */
4899 ret = bus_register(&gpio_bus_type);
4900 if (ret < 0) {
4901 pr_err("gpiolib: could not register GPIO bus type\n");
4902 return ret;
4903 }
4904
4905 ret = driver_register(&gpio_stub_drv);
4906 if (ret < 0) {
4907 pr_err("gpiolib: could not register GPIO stub driver\n");
4908 bus_unregister(&gpio_bus_type);
4909 return ret;
4910 }
4911
4912 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4913 if (ret < 0) {
4914 pr_err("gpiolib: failed to allocate char dev region\n");
4915 driver_unregister(&gpio_stub_drv);
4916 bus_unregister(&gpio_bus_type);
4917 return ret;
4918 }
4919
4920 gpiolib_initialized = true;
4921 gpiochip_setup_devs();
4922
4923 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4924 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4925 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4926
4927 return ret;
4928 }
4929 core_initcall(gpiolib_dev_init);
4930
4931 #ifdef CONFIG_DEBUG_FS
4932
gpiolib_dbg_show(struct seq_file * s,struct gpio_device * gdev)4933 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4934 {
4935 bool active_low, is_irq, is_out;
4936 unsigned int gpio = gdev->base;
4937 struct gpio_desc *desc;
4938 struct gpio_chip *gc;
4939 int value;
4940
4941 guard(srcu)(&gdev->srcu);
4942
4943 gc = srcu_dereference(gdev->chip, &gdev->srcu);
4944 if (!gc) {
4945 seq_puts(s, "Underlying GPIO chip is gone\n");
4946 return;
4947 }
4948
4949 for_each_gpio_desc(gc, desc) {
4950 guard(srcu)(&desc->gdev->desc_srcu);
4951 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
4952 if (is_irq || test_bit(FLAG_REQUESTED, &desc->flags)) {
4953 gpiod_get_direction(desc);
4954 is_out = test_bit(FLAG_IS_OUT, &desc->flags);
4955 value = gpio_chip_get_value(gc, desc);
4956 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4957 seq_printf(s, " gpio-%-3u (%-20.20s|%-20.20s) %s %s %s%s\n",
4958 gpio, desc->name ?: "", gpiod_get_label(desc),
4959 is_out ? "out" : "in ",
4960 value >= 0 ? (value ? "hi" : "lo") : "? ",
4961 is_irq ? "IRQ " : "",
4962 active_low ? "ACTIVE LOW" : "");
4963 } else if (desc->name) {
4964 seq_printf(s, " gpio-%-3u (%-20.20s)\n", gpio, desc->name);
4965 }
4966
4967 gpio++;
4968 }
4969 }
4970
4971 struct gpiolib_seq_priv {
4972 bool newline;
4973 int idx;
4974 };
4975
gpiolib_seq_start(struct seq_file * s,loff_t * pos)4976 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4977 {
4978 struct gpiolib_seq_priv *priv;
4979 struct gpio_device *gdev;
4980 loff_t index = *pos;
4981
4982 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
4983 if (!priv)
4984 return NULL;
4985
4986 s->private = priv;
4987 if (*pos > 0)
4988 priv->newline = true;
4989 priv->idx = srcu_read_lock(&gpio_devices_srcu);
4990
4991 list_for_each_entry_srcu(gdev, &gpio_devices, list,
4992 srcu_read_lock_held(&gpio_devices_srcu)) {
4993 if (index-- == 0)
4994 return gdev;
4995 }
4996
4997 return NULL;
4998 }
4999
gpiolib_seq_next(struct seq_file * s,void * v,loff_t * pos)5000 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5001 {
5002 struct gpiolib_seq_priv *priv = s->private;
5003 struct gpio_device *gdev = v, *next;
5004
5005 next = list_entry_rcu(gdev->list.next, struct gpio_device, list);
5006 gdev = &next->list == &gpio_devices ? NULL : next;
5007 priv->newline = true;
5008 ++*pos;
5009
5010 return gdev;
5011 }
5012
gpiolib_seq_stop(struct seq_file * s,void * v)5013 static void gpiolib_seq_stop(struct seq_file *s, void *v)
5014 {
5015 struct gpiolib_seq_priv *priv = s->private;
5016
5017 srcu_read_unlock(&gpio_devices_srcu, priv->idx);
5018 kfree(priv);
5019 }
5020
gpiolib_seq_show(struct seq_file * s,void * v)5021 static int gpiolib_seq_show(struct seq_file *s, void *v)
5022 {
5023 struct gpiolib_seq_priv *priv = s->private;
5024 struct gpio_device *gdev = v;
5025 struct gpio_chip *gc;
5026 struct device *parent;
5027
5028 guard(srcu)(&gdev->srcu);
5029
5030 gc = srcu_dereference(gdev->chip, &gdev->srcu);
5031 if (!gc) {
5032 seq_printf(s, "%s%s: (dangling chip)\n",
5033 priv->newline ? "\n" : "",
5034 dev_name(&gdev->dev));
5035 return 0;
5036 }
5037
5038 seq_printf(s, "%s%s: GPIOs %u-%u", priv->newline ? "\n" : "",
5039 dev_name(&gdev->dev),
5040 gdev->base, gdev->base + gdev->ngpio - 1);
5041 parent = gc->parent;
5042 if (parent)
5043 seq_printf(s, ", parent: %s/%s",
5044 parent->bus ? parent->bus->name : "no-bus",
5045 dev_name(parent));
5046 if (gc->label)
5047 seq_printf(s, ", %s", gc->label);
5048 if (gc->can_sleep)
5049 seq_printf(s, ", can sleep");
5050 seq_printf(s, ":\n");
5051
5052 if (gc->dbg_show)
5053 gc->dbg_show(s, gc);
5054 else
5055 gpiolib_dbg_show(s, gdev);
5056
5057 return 0;
5058 }
5059
5060 static const struct seq_operations gpiolib_sops = {
5061 .start = gpiolib_seq_start,
5062 .next = gpiolib_seq_next,
5063 .stop = gpiolib_seq_stop,
5064 .show = gpiolib_seq_show,
5065 };
5066 DEFINE_SEQ_ATTRIBUTE(gpiolib);
5067
gpiolib_debugfs_init(void)5068 static int __init gpiolib_debugfs_init(void)
5069 {
5070 /* /sys/kernel/debug/gpio */
5071 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
5072 return 0;
5073 }
5074 subsys_initcall(gpiolib_debugfs_init);
5075
5076 #endif /* DEBUG_FS */
5077