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