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