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