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