1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/idr.h>
20 #include <linux/cdev.h>
22 #include <linux/uaccess.h>
23 #include <uapi/linux/gpio.h>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/gpio.h>
30 /* Implementation infrastructure for GPIO interfaces.
32 * The GPIO programming interface allows for inlining speed-critical
33 * get/set operations for common cases, so that access to SOC-integrated
34 * GPIOs can sometimes cost only an instruction or two per bit.
38 /* When debugging, extend minimal trust to callers and platform code.
39 * Also emit diagnostic messages that may help initial bringup, when
40 * board setup or driver bugs are most common.
42 * Otherwise, minimize overhead in what may be bitbanging codepaths.
45 #define extra_checks 1
47 #define extra_checks 0
50 /* Device and char device-related information */
51 static DEFINE_IDA(gpio_ida
);
52 static dev_t gpio_devt
;
53 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
54 static struct bus_type gpio_bus_type
= {
58 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
59 * While any GPIO is requested, its gpio_chip is not removable;
60 * each GPIO's "requested" flag serves as a lock and refcount.
62 DEFINE_SPINLOCK(gpio_lock
);
64 static DEFINE_MUTEX(gpio_lookup_lock
);
65 static LIST_HEAD(gpio_lookup_list
);
66 LIST_HEAD(gpio_devices
);
68 static void gpiochip_free_hogs(struct gpio_chip
*chip
);
69 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
);
72 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
78 * Convert a GPIO number to its descriptor
80 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
82 struct gpio_device
*gdev
;
85 spin_lock_irqsave(&gpio_lock
, flags
);
87 list_for_each_entry(gdev
, &gpio_devices
, list
) {
88 if (gdev
->base
<= gpio
&&
89 gdev
->base
+ gdev
->ngpio
> gpio
) {
90 spin_unlock_irqrestore(&gpio_lock
, flags
);
91 return &gdev
->descs
[gpio
- gdev
->base
];
95 spin_unlock_irqrestore(&gpio_lock
, flags
);
97 if (!gpio_is_valid(gpio
))
98 WARN(1, "invalid GPIO %d\n", gpio
);
102 EXPORT_SYMBOL_GPL(gpio_to_desc
);
105 * Get the GPIO descriptor corresponding to the given hw number for this chip.
107 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*chip
,
110 struct gpio_device
*gdev
= chip
->gpiodev
;
112 if (hwnum
>= gdev
->ngpio
)
113 return ERR_PTR(-EINVAL
);
115 return &gdev
->descs
[hwnum
];
119 * Convert a GPIO descriptor to the integer namespace.
120 * This should disappear in the future but is needed since we still
121 * use GPIO numbers for error messages and sysfs nodes
123 int desc_to_gpio(const struct gpio_desc
*desc
)
125 return desc
->gdev
->base
+ (desc
- &desc
->gdev
->descs
[0]);
127 EXPORT_SYMBOL_GPL(desc_to_gpio
);
131 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
132 * @desc: descriptor to return the chip of
134 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
136 if (!desc
|| !desc
->gdev
|| !desc
->gdev
->chip
)
138 return desc
->gdev
->chip
;
140 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
142 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
143 static int gpiochip_find_base(int ngpio
)
145 struct gpio_device
*gdev
;
146 int base
= ARCH_NR_GPIOS
- ngpio
;
148 list_for_each_entry_reverse(gdev
, &gpio_devices
, list
) {
149 /* found a free space? */
150 if (gdev
->base
+ gdev
->ngpio
<= base
)
153 /* nope, check the space right before the chip */
154 base
= gdev
->base
- ngpio
;
157 if (gpio_is_valid(base
)) {
158 pr_debug("%s: found new base at %d\n", __func__
, base
);
161 pr_err("%s: cannot find free range\n", __func__
);
167 * gpiod_get_direction - return the current direction of a GPIO
168 * @desc: GPIO to get the direction of
170 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
172 * This function may sleep if gpiod_cansleep() is true.
174 int gpiod_get_direction(struct gpio_desc
*desc
)
176 struct gpio_chip
*chip
;
178 int status
= -EINVAL
;
180 chip
= gpiod_to_chip(desc
);
181 offset
= gpio_chip_hwgpio(desc
);
183 if (!chip
->get_direction
)
186 status
= chip
->get_direction(chip
, offset
);
188 /* GPIOF_DIR_IN, or other positive */
190 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
194 set_bit(FLAG_IS_OUT
, &desc
->flags
);
198 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
201 * Add a new chip to the global chips list, keeping the list of chips sorted
202 * by range(means [base, base + ngpio - 1]) order.
204 * Return -EBUSY if the new chip overlaps with some other chip's integer
207 static int gpiodev_add_to_list(struct gpio_device
*gdev
)
209 struct gpio_device
*prev
, *next
;
211 if (list_empty(&gpio_devices
)) {
212 /* initial entry in list */
213 list_add_tail(&gdev
->list
, &gpio_devices
);
217 next
= list_entry(gpio_devices
.next
, struct gpio_device
, list
);
218 if (gdev
->base
+ gdev
->ngpio
<= next
->base
) {
219 /* add before first entry */
220 list_add(&gdev
->list
, &gpio_devices
);
224 prev
= list_entry(gpio_devices
.prev
, struct gpio_device
, list
);
225 if (prev
->base
+ prev
->ngpio
<= gdev
->base
) {
226 /* add behind last entry */
227 list_add_tail(&gdev
->list
, &gpio_devices
);
231 list_for_each_entry_safe(prev
, next
, &gpio_devices
, list
) {
232 /* at the end of the list */
233 if (&next
->list
== &gpio_devices
)
236 /* add between prev and next */
237 if (prev
->base
+ prev
->ngpio
<= gdev
->base
238 && gdev
->base
+ gdev
->ngpio
<= next
->base
) {
239 list_add(&gdev
->list
, &prev
->list
);
244 dev_err(&gdev
->dev
, "GPIO integer space overlap, cannot add chip\n");
249 * Convert a GPIO name to its descriptor
251 static struct gpio_desc
*gpio_name_to_desc(const char * const name
)
253 struct gpio_device
*gdev
;
256 spin_lock_irqsave(&gpio_lock
, flags
);
258 list_for_each_entry(gdev
, &gpio_devices
, list
) {
261 for (i
= 0; i
!= gdev
->ngpio
; ++i
) {
262 struct gpio_desc
*desc
= &gdev
->descs
[i
];
264 if (!desc
->name
|| !name
)
267 if (!strcmp(desc
->name
, name
)) {
268 spin_unlock_irqrestore(&gpio_lock
, flags
);
274 spin_unlock_irqrestore(&gpio_lock
, flags
);
280 * Takes the names from gc->names and checks if they are all unique. If they
281 * are, they are assigned to their gpio descriptors.
283 * Warning if one of the names is already used for a different GPIO.
285 static int gpiochip_set_desc_names(struct gpio_chip
*gc
)
287 struct gpio_device
*gdev
= gc
->gpiodev
;
293 /* First check all names if they are unique */
294 for (i
= 0; i
!= gc
->ngpio
; ++i
) {
295 struct gpio_desc
*gpio
;
297 gpio
= gpio_name_to_desc(gc
->names
[i
]);
300 "Detected name collision for GPIO name '%s'\n",
304 /* Then add all names to the GPIO descriptors */
305 for (i
= 0; i
!= gc
->ngpio
; ++i
)
306 gdev
->descs
[i
].name
= gc
->names
[i
];
312 * gpio_ioctl() - ioctl handler for the GPIO chardev
314 static long gpio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
316 struct gpio_device
*gdev
= filp
->private_data
;
317 struct gpio_chip
*chip
= gdev
->chip
;
318 int __user
*ip
= (int __user
*)arg
;
320 /* We fail any subsequent ioctl():s when the chip is gone */
324 /* Fill in the struct and pass to userspace */
325 if (cmd
== GPIO_GET_CHIPINFO_IOCTL
) {
326 struct gpiochip_info chipinfo
;
328 strncpy(chipinfo
.name
, dev_name(&gdev
->dev
),
329 sizeof(chipinfo
.name
));
330 chipinfo
.name
[sizeof(chipinfo
.name
)-1] = '\0';
331 strncpy(chipinfo
.label
, gdev
->label
,
332 sizeof(chipinfo
.label
));
333 chipinfo
.label
[sizeof(chipinfo
.label
)-1] = '\0';
334 chipinfo
.lines
= gdev
->ngpio
;
335 if (copy_to_user(ip
, &chipinfo
, sizeof(chipinfo
)))
338 } else if (cmd
== GPIO_GET_LINEINFO_IOCTL
) {
339 struct gpioline_info lineinfo
;
340 struct gpio_desc
*desc
;
342 if (copy_from_user(&lineinfo
, ip
, sizeof(lineinfo
)))
344 if (lineinfo
.line_offset
> gdev
->ngpio
)
347 desc
= &gdev
->descs
[lineinfo
.line_offset
];
349 strncpy(lineinfo
.name
, desc
->name
,
350 sizeof(lineinfo
.name
));
351 lineinfo
.name
[sizeof(lineinfo
.name
)-1] = '\0';
353 lineinfo
.name
[0] = '\0';
356 strncpy(lineinfo
.consumer
, desc
->label
,
357 sizeof(lineinfo
.consumer
));
358 lineinfo
.consumer
[sizeof(lineinfo
.consumer
)-1] = '\0';
360 lineinfo
.consumer
[0] = '\0';
364 * Userspace only need to know that the kernel is using
365 * this GPIO so it can't use it.
368 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
369 test_bit(FLAG_IS_HOGGED
, &desc
->flags
) ||
370 test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
) ||
371 test_bit(FLAG_EXPORT
, &desc
->flags
) ||
372 test_bit(FLAG_SYSFS
, &desc
->flags
))
373 lineinfo
.flags
|= GPIOLINE_FLAG_KERNEL
;
374 if (test_bit(FLAG_IS_OUT
, &desc
->flags
))
375 lineinfo
.flags
|= GPIOLINE_FLAG_IS_OUT
;
376 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
377 lineinfo
.flags
|= GPIOLINE_FLAG_ACTIVE_LOW
;
378 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
379 lineinfo
.flags
|= GPIOLINE_FLAG_OPEN_DRAIN
;
380 if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
381 lineinfo
.flags
|= GPIOLINE_FLAG_OPEN_SOURCE
;
383 if (copy_to_user(ip
, &lineinfo
, sizeof(lineinfo
)))
391 * gpio_chrdev_open() - open the chardev for ioctl operations
392 * @inode: inode for this chardev
393 * @filp: file struct for storing private data
394 * Returns 0 on success
396 static int gpio_chrdev_open(struct inode
*inode
, struct file
*filp
)
398 struct gpio_device
*gdev
= container_of(inode
->i_cdev
,
399 struct gpio_device
, chrdev
);
401 /* Fail on open if the backing gpiochip is gone */
402 if (!gdev
|| !gdev
->chip
)
404 get_device(&gdev
->dev
);
405 filp
->private_data
= gdev
;
410 * gpio_chrdev_release() - close chardev after ioctl operations
411 * @inode: inode for this chardev
412 * @filp: file struct for storing private data
413 * Returns 0 on success
415 static int gpio_chrdev_release(struct inode
*inode
, struct file
*filp
)
417 struct gpio_device
*gdev
= container_of(inode
->i_cdev
,
418 struct gpio_device
, chrdev
);
422 put_device(&gdev
->dev
);
427 static const struct file_operations gpio_fileops
= {
428 .release
= gpio_chrdev_release
,
429 .open
= gpio_chrdev_open
,
430 .owner
= THIS_MODULE
,
431 .llseek
= noop_llseek
,
432 .unlocked_ioctl
= gpio_ioctl
,
433 .compat_ioctl
= gpio_ioctl
,
436 static void gpiodevice_release(struct device
*dev
)
438 struct gpio_device
*gdev
= dev_get_drvdata(dev
);
440 cdev_del(&gdev
->chrdev
);
441 list_del(&gdev
->list
);
442 ida_simple_remove(&gpio_ida
, gdev
->id
);
447 * gpiochip_add_data() - register a gpio_chip
448 * @chip: the chip to register, with chip->base initialized
449 * Context: potentially before irqs will work
451 * Returns a negative errno if the chip can't be registered, such as
452 * because the chip->base is invalid or already associated with a
453 * different chip. Otherwise it returns zero as a success code.
455 * When gpiochip_add_data() is called very early during boot, so that GPIOs
456 * can be freely used, the chip->parent device must be registered before
457 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
458 * for GPIOs will fail rudely.
460 * If chip->base is negative, this requests dynamic assignment of
461 * a range of valid GPIOs.
463 int gpiochip_add_data(struct gpio_chip
*chip
, void *data
)
468 int base
= chip
->base
;
469 struct gpio_device
*gdev
;
472 * First: allocate and populate the internal stat container, and
473 * set up the struct device.
475 gdev
= kzalloc(sizeof(*gdev
), GFP_KERNEL
);
478 gdev
->dev
.bus
= &gpio_bus_type
;
480 chip
->gpiodev
= gdev
;
482 gdev
->dev
.parent
= chip
->parent
;
483 gdev
->dev
.of_node
= chip
->parent
->of_node
;
485 #ifdef CONFIG_OF_GPIO
486 /* If the gpiochip has an assigned OF node this takes precedence */
488 gdev
->dev
.of_node
= chip
->of_node
;
491 gdev
->id
= ida_simple_get(&gpio_ida
, 0, 0, GFP_KERNEL
);
496 dev_set_name(&gdev
->dev
, "gpiochip%d", gdev
->id
);
497 device_initialize(&gdev
->dev
);
498 dev_set_drvdata(&gdev
->dev
, gdev
);
499 if (chip
->parent
&& chip
->parent
->driver
)
500 gdev
->owner
= chip
->parent
->driver
->owner
;
501 else if (chip
->owner
)
502 /* TODO: remove chip->owner */
503 gdev
->owner
= chip
->owner
;
505 gdev
->owner
= THIS_MODULE
;
507 gdev
->descs
= devm_kcalloc(&gdev
->dev
, chip
->ngpio
,
508 sizeof(gdev
->descs
[0]), GFP_KERNEL
);
514 if (chip
->ngpio
== 0) {
515 chip_err(chip
, "tried to insert a GPIO chip with zero lines\n");
521 gdev
->label
= devm_kstrdup(&gdev
->dev
, chip
->label
, GFP_KERNEL
);
523 gdev
->label
= devm_kstrdup(&gdev
->dev
, "unknown", GFP_KERNEL
);
529 gdev
->ngpio
= chip
->ngpio
;
532 spin_lock_irqsave(&gpio_lock
, flags
);
535 * TODO: this allocates a Linux GPIO number base in the global
536 * GPIO numberspace for this chip. In the long run we want to
537 * get *rid* of this numberspace and use only descriptors, but
538 * it may be a pipe dream. It will not happen before we get rid
539 * of the sysfs interface anyways.
542 base
= gpiochip_find_base(chip
->ngpio
);
545 spin_unlock_irqrestore(&gpio_lock
, flags
);
549 * TODO: it should not be necessary to reflect the assigned
550 * base outside of the GPIO subsystem. Go over drivers and
551 * see if anyone makes use of this, else drop this and assign
558 status
= gpiodev_add_to_list(gdev
);
560 spin_unlock_irqrestore(&gpio_lock
, flags
);
564 for (i
= 0; i
< chip
->ngpio
; i
++) {
565 struct gpio_desc
*desc
= &gdev
->descs
[i
];
569 /* REVISIT: most hardware initializes GPIOs as inputs (often
570 * with pullups enabled) so power usage is minimized. Linux
571 * code should set the gpio direction first thing; but until
572 * it does, and in case chip->get_direction is not set, we may
573 * expose the wrong direction in sysfs.
575 desc
->flags
= !chip
->direction_input
? (1 << FLAG_IS_OUT
) : 0;
578 spin_unlock_irqrestore(&gpio_lock
, flags
);
580 #ifdef CONFIG_PINCTRL
581 INIT_LIST_HEAD(&gdev
->pin_ranges
);
584 status
= gpiochip_set_desc_names(chip
);
586 goto err_remove_from_list
;
588 status
= of_gpiochip_add(chip
);
590 goto err_remove_chip
;
592 acpi_gpiochip_add(chip
);
595 * By first adding the chardev, and then adding the device,
596 * we get a device node entry in sysfs under
597 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
598 * coldplug of device nodes and other udev business.
600 cdev_init(&gdev
->chrdev
, &gpio_fileops
);
601 gdev
->chrdev
.owner
= THIS_MODULE
;
602 gdev
->chrdev
.kobj
.parent
= &gdev
->dev
.kobj
;
603 gdev
->dev
.devt
= MKDEV(MAJOR(gpio_devt
), gdev
->id
);
604 status
= cdev_add(&gdev
->chrdev
, gdev
->dev
.devt
, 1);
606 chip_warn(chip
, "failed to add char device %d:%d\n",
607 MAJOR(gpio_devt
), gdev
->id
);
609 chip_dbg(chip
, "added GPIO chardev (%d:%d)\n",
610 MAJOR(gpio_devt
), gdev
->id
);
611 status
= device_add(&gdev
->dev
);
613 goto err_remove_chardev
;
615 status
= gpiochip_sysfs_register(gdev
);
617 goto err_remove_device
;
619 /* From this point, the .release() function cleans up gpio_device */
620 gdev
->dev
.release
= gpiodevice_release
;
621 get_device(&gdev
->dev
);
622 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
623 __func__
, gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1,
624 dev_name(&gdev
->dev
), chip
->label
? : "generic");
629 device_del(&gdev
->dev
);
631 cdev_del(&gdev
->chrdev
);
633 acpi_gpiochip_remove(chip
);
634 gpiochip_free_hogs(chip
);
635 of_gpiochip_remove(chip
);
636 err_remove_from_list
:
637 spin_lock_irqsave(&gpio_lock
, flags
);
638 list_del(&gdev
->list
);
639 spin_unlock_irqrestore(&gpio_lock
, flags
);
641 ida_simple_remove(&gpio_ida
, gdev
->id
);
642 /* failures here can mean systems won't boot... */
643 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__
,
644 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1,
645 chip
->label
? : "generic");
649 EXPORT_SYMBOL_GPL(gpiochip_add_data
);
652 * gpiochip_get_data() - get per-subdriver data for the chip
654 void *gpiochip_get_data(struct gpio_chip
*chip
)
656 return chip
->gpiodev
->data
;
658 EXPORT_SYMBOL_GPL(gpiochip_get_data
);
661 * gpiochip_remove() - unregister a gpio_chip
662 * @chip: the chip to unregister
664 * A gpio_chip with any GPIOs still requested may not be removed.
666 void gpiochip_remove(struct gpio_chip
*chip
)
668 struct gpio_device
*gdev
= chip
->gpiodev
;
669 struct gpio_desc
*desc
;
672 bool requested
= false;
674 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
675 gpiochip_sysfs_unregister(gdev
);
676 /* Numb the device, cancelling all outstanding operations */
678 gpiochip_irqchip_remove(chip
);
679 acpi_gpiochip_remove(chip
);
680 gpiochip_remove_pin_ranges(chip
);
681 gpiochip_free_hogs(chip
);
682 of_gpiochip_remove(chip
);
684 * We accept no more calls into the driver from this point, so
685 * NULL the driver data pointer
689 spin_lock_irqsave(&gpio_lock
, flags
);
690 for (i
= 0; i
< gdev
->ngpio
; i
++) {
691 desc
= &gdev
->descs
[i
];
692 if (test_bit(FLAG_REQUESTED
, &desc
->flags
))
695 spin_unlock_irqrestore(&gpio_lock
, flags
);
699 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
702 * The gpiochip side puts its use of the device to rest here:
703 * if there are no userspace clients, the chardev and device will
704 * be removed, else it will be dangling until the last user is
707 put_device(&gdev
->dev
);
709 EXPORT_SYMBOL_GPL(gpiochip_remove
);
711 static void devm_gpio_chip_release(struct device
*dev
, void *res
)
713 struct gpio_chip
*chip
= *(struct gpio_chip
**)res
;
715 gpiochip_remove(chip
);
718 static int devm_gpio_chip_match(struct device
*dev
, void *res
, void *data
)
721 struct gpio_chip
**r
= res
;
732 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
733 * @dev: the device pointer on which irq_chip belongs to.
734 * @chip: the chip to register, with chip->base initialized
735 * Context: potentially before irqs will work
737 * Returns a negative errno if the chip can't be registered, such as
738 * because the chip->base is invalid or already associated with a
739 * different chip. Otherwise it returns zero as a success code.
741 * The gpio chip automatically be released when the device is unbound.
743 int devm_gpiochip_add_data(struct device
*dev
, struct gpio_chip
*chip
,
746 struct gpio_chip
**ptr
;
749 ptr
= devres_alloc(devm_gpio_chip_release
, sizeof(*ptr
),
754 ret
= gpiochip_add_data(chip
, data
);
761 devres_add(dev
, ptr
);
765 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data
);
768 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
769 * @dev: device for which which resource was allocated
770 * @chip: the chip to remove
772 * A gpio_chip with any GPIOs still requested may not be removed.
774 void devm_gpiochip_remove(struct device
*dev
, struct gpio_chip
*chip
)
778 ret
= devres_release(dev
, devm_gpio_chip_release
,
779 devm_gpio_chip_match
, chip
);
783 EXPORT_SYMBOL_GPL(devm_gpiochip_remove
);
786 * gpiochip_find() - iterator for locating a specific gpio_chip
787 * @data: data to pass to match function
788 * @callback: Callback function to check gpio_chip
790 * Similar to bus_find_device. It returns a reference to a gpio_chip as
791 * determined by a user supplied @match callback. The callback should return
792 * 0 if the device doesn't match and non-zero if it does. If the callback is
793 * non-zero, this function will return to the caller and not iterate over any
796 struct gpio_chip
*gpiochip_find(void *data
,
797 int (*match
)(struct gpio_chip
*chip
,
800 struct gpio_device
*gdev
;
801 struct gpio_chip
*chip
;
804 spin_lock_irqsave(&gpio_lock
, flags
);
805 list_for_each_entry(gdev
, &gpio_devices
, list
)
806 if (match(gdev
->chip
, data
))
810 if (&gdev
->list
== &gpio_devices
)
815 spin_unlock_irqrestore(&gpio_lock
, flags
);
819 EXPORT_SYMBOL_GPL(gpiochip_find
);
821 static int gpiochip_match_name(struct gpio_chip
*chip
, void *data
)
823 const char *name
= data
;
825 return !strcmp(chip
->label
, name
);
828 static struct gpio_chip
*find_chip_by_name(const char *name
)
830 return gpiochip_find((void *)name
, gpiochip_match_name
);
833 #ifdef CONFIG_GPIOLIB_IRQCHIP
836 * The following is irqchip helper code for gpiochips.
840 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
841 * @gpiochip: the gpiochip to set the irqchip chain to
842 * @irqchip: the irqchip to chain to the gpiochip
843 * @parent_irq: the irq number corresponding to the parent IRQ for this
845 * @parent_handler: the parent interrupt handler for the accumulated IRQ
846 * coming out of the gpiochip. If the interrupt is nested rather than
847 * cascaded, pass NULL in this handler argument
849 void gpiochip_set_chained_irqchip(struct gpio_chip
*gpiochip
,
850 struct irq_chip
*irqchip
,
852 irq_flow_handler_t parent_handler
)
856 if (!gpiochip
->irqdomain
) {
857 chip_err(gpiochip
, "called %s before setting up irqchip\n",
862 if (parent_handler
) {
863 if (gpiochip
->can_sleep
) {
865 "you cannot have chained interrupts on a "
866 "chip that may sleep\n");
870 * The parent irqchip is already using the chip_data for this
871 * irqchip, so our callbacks simply use the handler_data.
873 irq_set_chained_handler_and_data(parent_irq
, parent_handler
,
876 gpiochip
->irq_parent
= parent_irq
;
879 /* Set the parent IRQ for all affected IRQs */
880 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
881 irq_set_parent(irq_find_mapping(gpiochip
->irqdomain
, offset
),
884 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip
);
887 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
888 * @d: the irqdomain used by this irqchip
889 * @irq: the global irq number used by this GPIO irqchip irq
890 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
892 * This function will set up the mapping for a certain IRQ line on a
893 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
894 * stored inside the gpiochip.
896 static int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
897 irq_hw_number_t hwirq
)
899 struct gpio_chip
*chip
= d
->host_data
;
901 irq_set_chip_data(irq
, chip
);
903 * This lock class tells lockdep that GPIO irqs are in a different
904 * category than their parents, so it won't report false recursion.
906 irq_set_lockdep_class(irq
, chip
->lock_key
);
907 irq_set_chip_and_handler(irq
, chip
->irqchip
, chip
->irq_handler
);
908 /* Chips that can sleep need nested thread handlers */
909 if (chip
->can_sleep
&& !chip
->irq_not_threaded
)
910 irq_set_nested_thread(irq
, 1);
911 irq_set_noprobe(irq
);
914 * No set-up of the hardware will happen if IRQ_TYPE_NONE
915 * is passed as default type.
917 if (chip
->irq_default_type
!= IRQ_TYPE_NONE
)
918 irq_set_irq_type(irq
, chip
->irq_default_type
);
923 static void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
925 struct gpio_chip
*chip
= d
->host_data
;
928 irq_set_nested_thread(irq
, 0);
929 irq_set_chip_and_handler(irq
, NULL
, NULL
);
930 irq_set_chip_data(irq
, NULL
);
933 static const struct irq_domain_ops gpiochip_domain_ops
= {
934 .map
= gpiochip_irq_map
,
935 .unmap
= gpiochip_irq_unmap
,
936 /* Virtually all GPIO irqchips are twocell:ed */
937 .xlate
= irq_domain_xlate_twocell
,
940 static int gpiochip_irq_reqres(struct irq_data
*d
)
942 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
944 if (!try_module_get(chip
->gpiodev
->owner
))
947 if (gpiochip_lock_as_irq(chip
, d
->hwirq
)) {
949 "unable to lock HW IRQ %lu for IRQ\n",
951 module_put(chip
->gpiodev
->owner
);
957 static void gpiochip_irq_relres(struct irq_data
*d
)
959 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
961 gpiochip_unlock_as_irq(chip
, d
->hwirq
);
962 module_put(chip
->gpiodev
->owner
);
965 static int gpiochip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
967 return irq_find_mapping(chip
->irqdomain
, offset
);
971 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
972 * @gpiochip: the gpiochip to remove the irqchip from
974 * This is called only from gpiochip_remove()
976 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
)
980 acpi_gpiochip_free_interrupts(gpiochip
);
982 if (gpiochip
->irq_parent
) {
983 irq_set_chained_handler(gpiochip
->irq_parent
, NULL
);
984 irq_set_handler_data(gpiochip
->irq_parent
, NULL
);
987 /* Remove all IRQ mappings and delete the domain */
988 if (gpiochip
->irqdomain
) {
989 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
991 irq_find_mapping(gpiochip
->irqdomain
, offset
));
992 irq_domain_remove(gpiochip
->irqdomain
);
995 if (gpiochip
->irqchip
) {
996 gpiochip
->irqchip
->irq_request_resources
= NULL
;
997 gpiochip
->irqchip
->irq_release_resources
= NULL
;
998 gpiochip
->irqchip
= NULL
;
1003 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1004 * @gpiochip: the gpiochip to add the irqchip to
1005 * @irqchip: the irqchip to add to the gpiochip
1006 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1007 * allocate gpiochip irqs from
1008 * @handler: the irq handler to use (often a predefined irq core function)
1009 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1010 * to have the core avoid setting up any default type in the hardware.
1011 * @lock_key: lockdep class
1013 * This function closely associates a certain irqchip with a certain
1014 * gpiochip, providing an irq domain to translate the local IRQs to
1015 * global irqs in the gpiolib core, and making sure that the gpiochip
1016 * is passed as chip data to all related functions. Driver callbacks
1017 * need to use gpiochip_get_data() to get their local state containers back
1018 * from the gpiochip passed as chip data. An irqdomain will be stored
1019 * in the gpiochip that shall be used by the driver to handle IRQ number
1020 * translation. The gpiochip will need to be initialized and registered
1021 * before calling this function.
1023 * This function will handle two cell:ed simple IRQs and assumes all
1024 * the pins on the gpiochip can generate a unique IRQ. Everything else
1025 * need to be open coded.
1027 int _gpiochip_irqchip_add(struct gpio_chip
*gpiochip
,
1028 struct irq_chip
*irqchip
,
1029 unsigned int first_irq
,
1030 irq_flow_handler_t handler
,
1032 struct lock_class_key
*lock_key
)
1034 struct device_node
*of_node
;
1035 unsigned int offset
;
1036 unsigned irq_base
= 0;
1038 if (!gpiochip
|| !irqchip
)
1041 if (!gpiochip
->parent
) {
1042 pr_err("missing gpiochip .dev parent pointer\n");
1045 of_node
= gpiochip
->parent
->of_node
;
1046 #ifdef CONFIG_OF_GPIO
1048 * If the gpiochip has an assigned OF node this takes precedence
1049 * FIXME: get rid of this and use gpiochip->parent->of_node
1052 if (gpiochip
->of_node
)
1053 of_node
= gpiochip
->of_node
;
1055 gpiochip
->irqchip
= irqchip
;
1056 gpiochip
->irq_handler
= handler
;
1057 gpiochip
->irq_default_type
= type
;
1058 gpiochip
->to_irq
= gpiochip_to_irq
;
1059 gpiochip
->lock_key
= lock_key
;
1060 gpiochip
->irqdomain
= irq_domain_add_simple(of_node
,
1061 gpiochip
->ngpio
, first_irq
,
1062 &gpiochip_domain_ops
, gpiochip
);
1063 if (!gpiochip
->irqdomain
) {
1064 gpiochip
->irqchip
= NULL
;
1069 * It is possible for a driver to override this, but only if the
1070 * alternative functions are both implemented.
1072 if (!irqchip
->irq_request_resources
&&
1073 !irqchip
->irq_release_resources
) {
1074 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
1075 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
1079 * Prepare the mapping since the irqchip shall be orthogonal to
1080 * any gpiochip calls. If the first_irq was zero, this is
1081 * necessary to allocate descriptors for all IRQs.
1083 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++) {
1084 irq_base
= irq_create_mapping(gpiochip
->irqdomain
, offset
);
1087 * Store the base into the gpiochip to be used when
1088 * unmapping the irqs.
1090 gpiochip
->irq_base
= irq_base
;
1093 acpi_gpiochip_request_interrupts(gpiochip
);
1097 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add
);
1099 #else /* CONFIG_GPIOLIB_IRQCHIP */
1101 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
) {}
1103 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1106 * gpiochip_generic_request() - request the gpio function for a pin
1107 * @chip: the gpiochip owning the GPIO
1108 * @offset: the offset of the GPIO to request for GPIO function
1110 int gpiochip_generic_request(struct gpio_chip
*chip
, unsigned offset
)
1112 return pinctrl_request_gpio(chip
->gpiodev
->base
+ offset
);
1114 EXPORT_SYMBOL_GPL(gpiochip_generic_request
);
1117 * gpiochip_generic_free() - free the gpio function from a pin
1118 * @chip: the gpiochip to request the gpio function for
1119 * @offset: the offset of the GPIO to free from GPIO function
1121 void gpiochip_generic_free(struct gpio_chip
*chip
, unsigned offset
)
1123 pinctrl_free_gpio(chip
->gpiodev
->base
+ offset
);
1125 EXPORT_SYMBOL_GPL(gpiochip_generic_free
);
1127 #ifdef CONFIG_PINCTRL
1130 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1131 * @chip: the gpiochip to add the range for
1132 * @pctldev: the pin controller to map to
1133 * @gpio_offset: the start offset in the current gpio_chip number space
1134 * @pin_group: name of the pin group inside the pin controller
1136 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
1137 struct pinctrl_dev
*pctldev
,
1138 unsigned int gpio_offset
, const char *pin_group
)
1140 struct gpio_pin_range
*pin_range
;
1141 struct gpio_device
*gdev
= chip
->gpiodev
;
1144 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1146 chip_err(chip
, "failed to allocate pin ranges\n");
1150 /* Use local offset as range ID */
1151 pin_range
->range
.id
= gpio_offset
;
1152 pin_range
->range
.gc
= chip
;
1153 pin_range
->range
.name
= chip
->label
;
1154 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
1155 pin_range
->pctldev
= pctldev
;
1157 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
1158 &pin_range
->range
.pins
,
1159 &pin_range
->range
.npins
);
1165 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
1167 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1168 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
1169 pinctrl_dev_get_devname(pctldev
), pin_group
);
1171 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
1175 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
1178 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1179 * @chip: the gpiochip to add the range for
1180 * @pinctrl_name: the dev_name() of the pin controller to map to
1181 * @gpio_offset: the start offset in the current gpio_chip number space
1182 * @pin_offset: the start offset in the pin controller number space
1183 * @npins: the number of pins from the offset of each pin space (GPIO and
1184 * pin controller) to accumulate in this range
1186 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
1187 unsigned int gpio_offset
, unsigned int pin_offset
,
1190 struct gpio_pin_range
*pin_range
;
1191 struct gpio_device
*gdev
= chip
->gpiodev
;
1194 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1196 chip_err(chip
, "failed to allocate pin ranges\n");
1200 /* Use local offset as range ID */
1201 pin_range
->range
.id
= gpio_offset
;
1202 pin_range
->range
.gc
= chip
;
1203 pin_range
->range
.name
= chip
->label
;
1204 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
1205 pin_range
->range
.pin_base
= pin_offset
;
1206 pin_range
->range
.npins
= npins
;
1207 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
1209 if (IS_ERR(pin_range
->pctldev
)) {
1210 ret
= PTR_ERR(pin_range
->pctldev
);
1211 chip_err(chip
, "could not create pin range\n");
1215 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1216 gpio_offset
, gpio_offset
+ npins
- 1,
1218 pin_offset
, pin_offset
+ npins
- 1);
1220 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
1224 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
1227 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1228 * @chip: the chip to remove all the mappings for
1230 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
1232 struct gpio_pin_range
*pin_range
, *tmp
;
1233 struct gpio_device
*gdev
= chip
->gpiodev
;
1235 list_for_each_entry_safe(pin_range
, tmp
, &gdev
->pin_ranges
, node
) {
1236 list_del(&pin_range
->node
);
1237 pinctrl_remove_gpio_range(pin_range
->pctldev
,
1242 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
1244 #endif /* CONFIG_PINCTRL */
1246 /* These "optional" allocation calls help prevent drivers from stomping
1247 * on each other, and help provide better diagnostics in debugfs.
1248 * They're called even less than the "set direction" calls.
1250 static int __gpiod_request(struct gpio_desc
*desc
, const char *label
)
1252 struct gpio_chip
*chip
= desc
->gdev
->chip
;
1254 unsigned long flags
;
1256 spin_lock_irqsave(&gpio_lock
, flags
);
1258 /* NOTE: gpio_request() can be called in early boot,
1259 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1262 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1263 desc_set_label(desc
, label
? : "?");
1270 if (chip
->request
) {
1271 /* chip->request may sleep */
1272 spin_unlock_irqrestore(&gpio_lock
, flags
);
1273 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
1274 spin_lock_irqsave(&gpio_lock
, flags
);
1277 desc_set_label(desc
, NULL
);
1278 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1282 if (chip
->get_direction
) {
1283 /* chip->get_direction may sleep */
1284 spin_unlock_irqrestore(&gpio_lock
, flags
);
1285 gpiod_get_direction(desc
);
1286 spin_lock_irqsave(&gpio_lock
, flags
);
1290 /* Clear flags that might have been set by the caller before
1291 * requesting the GPIO.
1293 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1294 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1295 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1297 spin_unlock_irqrestore(&gpio_lock
, flags
);
1302 * This descriptor validation needs to be inserted verbatim into each
1303 * function taking a descriptor, so we need to use a preprocessor
1304 * macro to avoid endless duplication.
1306 #define VALIDATE_DESC(desc) do { \
1307 if (!desc || !desc->gdev) { \
1308 pr_warn("%s: invalid GPIO\n", __func__); \
1311 if ( !desc->gdev->chip ) { \
1312 dev_warn(&desc->gdev->dev, \
1313 "%s: backing chip is gone\n", __func__); \
1317 #define VALIDATE_DESC_VOID(desc) do { \
1318 if (!desc || !desc->gdev) { \
1319 pr_warn("%s: invalid GPIO\n", __func__); \
1322 if (!desc->gdev->chip) { \
1323 dev_warn(&desc->gdev->dev, \
1324 "%s: backing chip is gone\n", __func__); \
1329 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
1331 int status
= -EPROBE_DEFER
;
1332 struct gpio_device
*gdev
;
1334 VALIDATE_DESC(desc
);
1337 if (try_module_get(gdev
->owner
)) {
1338 status
= __gpiod_request(desc
, label
);
1340 module_put(gdev
->owner
);
1342 get_device(&gdev
->dev
);
1346 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1351 static bool __gpiod_free(struct gpio_desc
*desc
)
1354 unsigned long flags
;
1355 struct gpio_chip
*chip
;
1359 gpiod_unexport(desc
);
1361 spin_lock_irqsave(&gpio_lock
, flags
);
1363 chip
= desc
->gdev
->chip
;
1364 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1366 spin_unlock_irqrestore(&gpio_lock
, flags
);
1367 might_sleep_if(chip
->can_sleep
);
1368 chip
->free(chip
, gpio_chip_hwgpio(desc
));
1369 spin_lock_irqsave(&gpio_lock
, flags
);
1371 desc_set_label(desc
, NULL
);
1372 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1373 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1374 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1375 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1376 clear_bit(FLAG_IS_HOGGED
, &desc
->flags
);
1380 spin_unlock_irqrestore(&gpio_lock
, flags
);
1384 void gpiod_free(struct gpio_desc
*desc
)
1386 if (desc
&& desc
->gdev
&& __gpiod_free(desc
)) {
1387 module_put(desc
->gdev
->owner
);
1388 put_device(&desc
->gdev
->dev
);
1390 WARN_ON(extra_checks
);
1395 * gpiochip_is_requested - return string iff signal was requested
1396 * @chip: controller managing the signal
1397 * @offset: of signal within controller's 0..(ngpio - 1) range
1399 * Returns NULL if the GPIO is not currently requested, else a string.
1400 * The string returned is the label passed to gpio_request(); if none has been
1401 * passed it is a meaningless, non-NULL constant.
1403 * This function is for use by GPIO controller drivers. The label can
1404 * help with diagnostics, and knowing that the signal is used as a GPIO
1405 * can help avoid accidentally multiplexing it to another controller.
1407 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1409 struct gpio_desc
*desc
;
1411 if (offset
>= chip
->ngpio
)
1414 desc
= &chip
->gpiodev
->descs
[offset
];
1416 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
1420 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1423 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1424 * @desc: GPIO descriptor to request
1425 * @label: label for the GPIO
1427 * Function allows GPIO chip drivers to request and use their own GPIO
1428 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1429 * function will not increase reference count of the GPIO chip module. This
1430 * allows the GPIO chip module to be unloaded as needed (we assume that the
1431 * GPIO chip driver handles freeing the GPIOs it has requested).
1433 struct gpio_desc
*gpiochip_request_own_desc(struct gpio_chip
*chip
, u16 hwnum
,
1436 struct gpio_desc
*desc
= gpiochip_get_desc(chip
, hwnum
);
1440 chip_err(chip
, "failed to get GPIO descriptor\n");
1444 err
= __gpiod_request(desc
, label
);
1446 return ERR_PTR(err
);
1450 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc
);
1453 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1454 * @desc: GPIO descriptor to free
1456 * Function frees the given GPIO requested previously with
1457 * gpiochip_request_own_desc().
1459 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
1464 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc
);
1467 * Drivers MUST set GPIO direction before making get/set calls. In
1468 * some cases this is done in early boot, before IRQs are enabled.
1470 * As a rule these aren't called more than once (except for drivers
1471 * using the open-drain emulation idiom) so these are natural places
1472 * to accumulate extra debugging checks. Note that we can't (yet)
1473 * rely on gpio_request() having been called beforehand.
1477 * gpiod_direction_input - set the GPIO direction to input
1478 * @desc: GPIO to set to input
1480 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1481 * be called safely on it.
1483 * Return 0 in case of success, else an error code.
1485 int gpiod_direction_input(struct gpio_desc
*desc
)
1487 struct gpio_chip
*chip
;
1488 int status
= -EINVAL
;
1490 VALIDATE_DESC(desc
);
1491 chip
= desc
->gdev
->chip
;
1493 if (!chip
->get
|| !chip
->direction_input
) {
1495 "%s: missing get() or direction_input() operations\n",
1500 status
= chip
->direction_input(chip
, gpio_chip_hwgpio(desc
));
1502 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1504 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1508 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
1510 static int _gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1512 struct gpio_chip
*gc
= desc
->gdev
->chip
;
1515 /* GPIOs used for IRQs shall not be set as output */
1516 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
1518 "%s: tried to set a GPIO tied to an IRQ as output\n",
1523 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
)) {
1524 /* First see if we can enable open drain in hardware */
1525 if (gc
->set_single_ended
) {
1526 ret
= gc
->set_single_ended(gc
, gpio_chip_hwgpio(desc
),
1527 LINE_MODE_OPEN_DRAIN
);
1529 goto set_output_value
;
1531 /* Emulate open drain by not actively driving the line high */
1533 return gpiod_direction_input(desc
);
1535 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
)) {
1536 if (gc
->set_single_ended
) {
1537 ret
= gc
->set_single_ended(gc
, gpio_chip_hwgpio(desc
),
1538 LINE_MODE_OPEN_SOURCE
);
1540 goto set_output_value
;
1542 /* Emulate open source by not actively driving the line low */
1544 return gpiod_direction_input(desc
);
1546 /* Make sure to disable open drain/source hardware, if any */
1547 if (gc
->set_single_ended
)
1548 gc
->set_single_ended(gc
,
1549 gpio_chip_hwgpio(desc
),
1550 LINE_MODE_PUSH_PULL
);
1554 if (!gc
->set
|| !gc
->direction_output
) {
1556 "%s: missing set() or direction_output() operations\n",
1561 ret
= gc
->direction_output(gc
, gpio_chip_hwgpio(desc
), value
);
1563 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1564 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1565 trace_gpio_direction(desc_to_gpio(desc
), 0, ret
);
1570 * gpiod_direction_output_raw - set the GPIO direction to output
1571 * @desc: GPIO to set to output
1572 * @value: initial output value of the GPIO
1574 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1575 * be called safely on it. The initial value of the output must be specified
1576 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1578 * Return 0 in case of success, else an error code.
1580 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1582 VALIDATE_DESC(desc
);
1583 return _gpiod_direction_output_raw(desc
, value
);
1585 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
1588 * gpiod_direction_output - set the GPIO direction to output
1589 * @desc: GPIO to set to output
1590 * @value: initial output value of the GPIO
1592 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1593 * be called safely on it. The initial value of the output must be specified
1594 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1597 * Return 0 in case of success, else an error code.
1599 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1601 VALIDATE_DESC(desc
);
1602 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1604 return _gpiod_direction_output_raw(desc
, value
);
1606 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1609 * gpiod_set_debounce - sets @debounce time for a @gpio
1610 * @gpio: the gpio to set debounce time
1611 * @debounce: debounce time is microseconds
1613 * returns -ENOTSUPP if the controller does not support setting
1616 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1618 struct gpio_chip
*chip
;
1620 VALIDATE_DESC(desc
);
1621 chip
= desc
->gdev
->chip
;
1622 if (!chip
->set
|| !chip
->set_debounce
) {
1624 "%s: missing set() or set_debounce() operations\n",
1629 return chip
->set_debounce(chip
, gpio_chip_hwgpio(desc
), debounce
);
1631 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1634 * gpiod_is_active_low - test whether a GPIO is active-low or not
1635 * @desc: the gpio descriptor to test
1637 * Returns 1 if the GPIO is active-low, 0 otherwise.
1639 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1641 VALIDATE_DESC(desc
);
1642 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1644 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1646 /* I/O calls are only valid after configuration completed; the relevant
1647 * "is this a valid GPIO" error checks should already have been done.
1649 * "Get" operations are often inlinable as reading a pin value register,
1650 * and masking the relevant bit in that register.
1652 * When "set" operations are inlinable, they involve writing that mask to
1653 * one register to set a low value, or a different register to set it high.
1654 * Otherwise locking is needed, so there may be little value to inlining.
1656 *------------------------------------------------------------------------
1658 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1659 * have requested the GPIO. That can include implicit requesting by
1660 * a direction setting call. Marking a gpio as requested locks its chip
1661 * in memory, guaranteeing that these table lookups need no more locking
1662 * and that gpiochip_remove() will fail.
1664 * REVISIT when debugging, consider adding some instrumentation to ensure
1665 * that the GPIO was actually requested.
1668 static int _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1670 struct gpio_chip
*chip
;
1674 chip
= desc
->gdev
->chip
;
1675 offset
= gpio_chip_hwgpio(desc
);
1676 value
= chip
->get
? chip
->get(chip
, offset
) : -EIO
;
1677 value
= value
< 0 ? value
: !!value
;
1678 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1683 * gpiod_get_raw_value() - return a gpio's raw value
1684 * @desc: gpio whose value will be returned
1686 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1687 * its ACTIVE_LOW status, or negative errno on failure.
1689 * This function should be called from contexts where we cannot sleep, and will
1690 * complain if the GPIO chip functions potentially sleep.
1692 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
1694 VALIDATE_DESC(desc
);
1695 /* Should be using gpio_get_value_cansleep() */
1696 WARN_ON(desc
->gdev
->chip
->can_sleep
);
1697 return _gpiod_get_raw_value(desc
);
1699 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1702 * gpiod_get_value() - return a gpio's value
1703 * @desc: gpio whose value will be returned
1705 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1706 * account, or negative errno on failure.
1708 * This function should be called from contexts where we cannot sleep, and will
1709 * complain if the GPIO chip functions potentially sleep.
1711 int gpiod_get_value(const struct gpio_desc
*desc
)
1715 VALIDATE_DESC(desc
);
1716 /* Should be using gpio_get_value_cansleep() */
1717 WARN_ON(desc
->gdev
->chip
->can_sleep
);
1719 value
= _gpiod_get_raw_value(desc
);
1723 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1728 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1731 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1732 * @desc: gpio descriptor whose state need to be set.
1733 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1735 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, bool value
)
1738 struct gpio_chip
*chip
= desc
->gdev
->chip
;
1739 int offset
= gpio_chip_hwgpio(desc
);
1742 err
= chip
->direction_input(chip
, offset
);
1744 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1746 err
= chip
->direction_output(chip
, offset
, 0);
1748 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1750 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1753 "%s: Error in set_value for open drain err %d\n",
1758 * _gpio_set_open_source_value() - Set the open source gpio's value.
1759 * @desc: gpio descriptor whose state need to be set.
1760 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1762 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, bool value
)
1765 struct gpio_chip
*chip
= desc
->gdev
->chip
;
1766 int offset
= gpio_chip_hwgpio(desc
);
1769 err
= chip
->direction_output(chip
, offset
, 1);
1771 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1773 err
= chip
->direction_input(chip
, offset
);
1775 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1777 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
1780 "%s: Error in set_value for open source err %d\n",
1784 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, bool value
)
1786 struct gpio_chip
*chip
;
1788 chip
= desc
->gdev
->chip
;
1789 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1790 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1791 _gpio_set_open_drain_value(desc
, value
);
1792 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1793 _gpio_set_open_source_value(desc
, value
);
1795 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
1799 * set multiple outputs on the same chip;
1800 * use the chip's set_multiple function if available;
1801 * otherwise set the outputs sequentially;
1802 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1803 * defines which outputs are to be changed
1804 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1805 * defines the values the outputs specified by mask are to be set to
1807 static void gpio_chip_set_multiple(struct gpio_chip
*chip
,
1808 unsigned long *mask
, unsigned long *bits
)
1810 if (chip
->set_multiple
) {
1811 chip
->set_multiple(chip
, mask
, bits
);
1814 for (i
= 0; i
< chip
->ngpio
; i
++) {
1815 if (mask
[BIT_WORD(i
)] == 0) {
1816 /* no more set bits in this mask word;
1817 * skip ahead to the next word */
1818 i
= (BIT_WORD(i
) + 1) * BITS_PER_LONG
- 1;
1821 /* set outputs if the corresponding mask bit is set */
1822 if (__test_and_clear_bit(i
, mask
))
1823 chip
->set(chip
, i
, test_bit(i
, bits
));
1828 static void gpiod_set_array_value_priv(bool raw
, bool can_sleep
,
1829 unsigned int array_size
,
1830 struct gpio_desc
**desc_array
,
1835 while (i
< array_size
) {
1836 struct gpio_chip
*chip
= desc_array
[i
]->gdev
->chip
;
1837 unsigned long mask
[BITS_TO_LONGS(chip
->ngpio
)];
1838 unsigned long bits
[BITS_TO_LONGS(chip
->ngpio
)];
1842 WARN_ON(chip
->can_sleep
);
1844 memset(mask
, 0, sizeof(mask
));
1846 struct gpio_desc
*desc
= desc_array
[i
];
1847 int hwgpio
= gpio_chip_hwgpio(desc
);
1848 int value
= value_array
[i
];
1850 if (!raw
&& test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1852 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1854 * collect all normal outputs belonging to the same chip
1855 * open drain and open source outputs are set individually
1857 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
)) {
1858 _gpio_set_open_drain_value(desc
, value
);
1859 } else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
)) {
1860 _gpio_set_open_source_value(desc
, value
);
1862 __set_bit(hwgpio
, mask
);
1864 __set_bit(hwgpio
, bits
);
1866 __clear_bit(hwgpio
, bits
);
1870 } while ((i
< array_size
) &&
1871 (desc_array
[i
]->gdev
->chip
== chip
));
1872 /* push collected bits to outputs */
1874 gpio_chip_set_multiple(chip
, mask
, bits
);
1879 * gpiod_set_raw_value() - assign a gpio's raw value
1880 * @desc: gpio whose value will be assigned
1881 * @value: value to assign
1883 * Set the raw value of the GPIO, i.e. the value of its physical line without
1884 * regard for its ACTIVE_LOW status.
1886 * This function should be called from contexts where we cannot sleep, and will
1887 * complain if the GPIO chip functions potentially sleep.
1889 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
1891 VALIDATE_DESC_VOID(desc
);
1892 /* Should be using gpiod_set_value_cansleep() */
1893 WARN_ON(desc
->gdev
->chip
->can_sleep
);
1894 _gpiod_set_raw_value(desc
, value
);
1896 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
1899 * gpiod_set_value() - assign a gpio's value
1900 * @desc: gpio whose value will be assigned
1901 * @value: value to assign
1903 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1906 * This function should be called from contexts where we cannot sleep, and will
1907 * complain if the GPIO chip functions potentially sleep.
1909 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
1911 VALIDATE_DESC_VOID(desc
);
1912 /* Should be using gpiod_set_value_cansleep() */
1913 WARN_ON(desc
->gdev
->chip
->can_sleep
);
1914 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1916 _gpiod_set_raw_value(desc
, value
);
1918 EXPORT_SYMBOL_GPL(gpiod_set_value
);
1921 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1922 * @array_size: number of elements in the descriptor / value arrays
1923 * @desc_array: array of GPIO descriptors whose values will be assigned
1924 * @value_array: array of values to assign
1926 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1927 * without regard for their ACTIVE_LOW status.
1929 * This function should be called from contexts where we cannot sleep, and will
1930 * complain if the GPIO chip functions potentially sleep.
1932 void gpiod_set_raw_array_value(unsigned int array_size
,
1933 struct gpio_desc
**desc_array
, int *value_array
)
1937 gpiod_set_array_value_priv(true, false, array_size
, desc_array
,
1940 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value
);
1943 * gpiod_set_array_value() - assign values to an array of GPIOs
1944 * @array_size: number of elements in the descriptor / value arrays
1945 * @desc_array: array of GPIO descriptors whose values will be assigned
1946 * @value_array: array of values to assign
1948 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1951 * This function should be called from contexts where we cannot sleep, and will
1952 * complain if the GPIO chip functions potentially sleep.
1954 void gpiod_set_array_value(unsigned int array_size
,
1955 struct gpio_desc
**desc_array
, int *value_array
)
1959 gpiod_set_array_value_priv(false, false, array_size
, desc_array
,
1962 EXPORT_SYMBOL_GPL(gpiod_set_array_value
);
1965 * gpiod_cansleep() - report whether gpio value access may sleep
1966 * @desc: gpio to check
1969 int gpiod_cansleep(const struct gpio_desc
*desc
)
1971 VALIDATE_DESC(desc
);
1972 return desc
->gdev
->chip
->can_sleep
;
1974 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
1977 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1978 * @desc: gpio whose IRQ will be returned (already requested)
1980 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1983 int gpiod_to_irq(const struct gpio_desc
*desc
)
1985 struct gpio_chip
*chip
;
1988 VALIDATE_DESC(desc
);
1989 chip
= desc
->gdev
->chip
;
1990 offset
= gpio_chip_hwgpio(desc
);
1991 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
1993 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
1996 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1997 * @chip: the chip the GPIO to lock belongs to
1998 * @offset: the offset of the GPIO to lock as IRQ
2000 * This is used directly by GPIO drivers that want to lock down
2001 * a certain GPIO line to be used for IRQs.
2003 int gpiochip_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
2005 if (offset
>= chip
->ngpio
)
2008 if (test_bit(FLAG_IS_OUT
, &chip
->gpiodev
->descs
[offset
].flags
)) {
2010 "%s: tried to flag a GPIO set as output for IRQ\n",
2015 set_bit(FLAG_USED_AS_IRQ
, &chip
->gpiodev
->descs
[offset
].flags
);
2018 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq
);
2021 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
2022 * @chip: the chip the GPIO to lock belongs to
2023 * @offset: the offset of the GPIO to lock as IRQ
2025 * This is used directly by GPIO drivers that want to indicate
2026 * that a certain GPIO is no longer used exclusively for IRQ.
2028 void gpiochip_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
2030 if (offset
>= chip
->ngpio
)
2033 clear_bit(FLAG_USED_AS_IRQ
, &chip
->gpiodev
->descs
[offset
].flags
);
2035 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq
);
2037 bool gpiochip_line_is_irq(struct gpio_chip
*chip
, unsigned int offset
)
2039 if (offset
>= chip
->ngpio
)
2042 return test_bit(FLAG_USED_AS_IRQ
, &chip
->gpiodev
->descs
[offset
].flags
);
2044 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq
);
2046 bool gpiochip_line_is_open_drain(struct gpio_chip
*chip
, unsigned int offset
)
2048 if (offset
>= chip
->ngpio
)
2051 return test_bit(FLAG_OPEN_DRAIN
, &chip
->gpiodev
->descs
[offset
].flags
);
2053 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain
);
2055 bool gpiochip_line_is_open_source(struct gpio_chip
*chip
, unsigned int offset
)
2057 if (offset
>= chip
->ngpio
)
2060 return test_bit(FLAG_OPEN_SOURCE
, &chip
->gpiodev
->descs
[offset
].flags
);
2062 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source
);
2065 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2066 * @desc: gpio whose value will be returned
2068 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2069 * its ACTIVE_LOW status, or negative errno on failure.
2071 * This function is to be called from contexts that can sleep.
2073 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
2075 might_sleep_if(extra_checks
);
2076 VALIDATE_DESC(desc
);
2077 return _gpiod_get_raw_value(desc
);
2079 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
2082 * gpiod_get_value_cansleep() - return a gpio's value
2083 * @desc: gpio whose value will be returned
2085 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2086 * account, or negative errno on failure.
2088 * This function is to be called from contexts that can sleep.
2090 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
2094 might_sleep_if(extra_checks
);
2095 VALIDATE_DESC(desc
);
2096 value
= _gpiod_get_raw_value(desc
);
2100 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2105 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
2108 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2109 * @desc: gpio whose value will be assigned
2110 * @value: value to assign
2112 * Set the raw value of the GPIO, i.e. the value of its physical line without
2113 * regard for its ACTIVE_LOW status.
2115 * This function is to be called from contexts that can sleep.
2117 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
2119 might_sleep_if(extra_checks
);
2120 VALIDATE_DESC_VOID(desc
);
2121 _gpiod_set_raw_value(desc
, value
);
2123 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
2126 * gpiod_set_value_cansleep() - assign a gpio's value
2127 * @desc: gpio whose value will be assigned
2128 * @value: value to assign
2130 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2133 * This function is to be called from contexts that can sleep.
2135 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
2137 might_sleep_if(extra_checks
);
2138 VALIDATE_DESC_VOID(desc
);
2139 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2141 _gpiod_set_raw_value(desc
, value
);
2143 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
2146 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
2147 * @array_size: number of elements in the descriptor / value arrays
2148 * @desc_array: array of GPIO descriptors whose values will be assigned
2149 * @value_array: array of values to assign
2151 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2152 * without regard for their ACTIVE_LOW status.
2154 * This function is to be called from contexts that can sleep.
2156 void gpiod_set_raw_array_value_cansleep(unsigned int array_size
,
2157 struct gpio_desc
**desc_array
,
2160 might_sleep_if(extra_checks
);
2163 gpiod_set_array_value_priv(true, true, array_size
, desc_array
,
2166 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep
);
2169 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
2170 * @array_size: number of elements in the descriptor / value arrays
2171 * @desc_array: array of GPIO descriptors whose values will be assigned
2172 * @value_array: array of values to assign
2174 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2177 * This function is to be called from contexts that can sleep.
2179 void gpiod_set_array_value_cansleep(unsigned int array_size
,
2180 struct gpio_desc
**desc_array
,
2183 might_sleep_if(extra_checks
);
2186 gpiod_set_array_value_priv(false, true, array_size
, desc_array
,
2189 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep
);
2192 * gpiod_add_lookup_table() - register GPIO device consumers
2193 * @table: table of consumers to register
2195 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
2197 mutex_lock(&gpio_lookup_lock
);
2199 list_add_tail(&table
->list
, &gpio_lookup_list
);
2201 mutex_unlock(&gpio_lookup_lock
);
2205 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2206 * @table: table of consumers to unregister
2208 void gpiod_remove_lookup_table(struct gpiod_lookup_table
*table
)
2210 mutex_lock(&gpio_lookup_lock
);
2212 list_del(&table
->list
);
2214 mutex_unlock(&gpio_lookup_lock
);
2217 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
2219 enum gpio_lookup_flags
*flags
)
2221 char prop_name
[32]; /* 32 is max size of property name */
2222 enum of_gpio_flags of_flags
;
2223 struct gpio_desc
*desc
;
2226 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
2228 snprintf(prop_name
, sizeof(prop_name
), "%s-%s", con_id
,
2231 snprintf(prop_name
, sizeof(prop_name
), "%s",
2234 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
2236 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
2243 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
2244 *flags
|= GPIO_ACTIVE_LOW
;
2246 if (of_flags
& OF_GPIO_SINGLE_ENDED
) {
2247 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
2248 *flags
|= GPIO_OPEN_DRAIN
;
2250 *flags
|= GPIO_OPEN_SOURCE
;
2256 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
2258 enum gpio_lookup_flags
*flags
)
2260 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
2261 struct acpi_gpio_info info
;
2262 struct gpio_desc
*desc
;
2266 /* Try first from _DSD */
2267 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
2268 if (con_id
&& strcmp(con_id
, "gpios")) {
2269 snprintf(propname
, sizeof(propname
), "%s-%s",
2270 con_id
, gpio_suffixes
[i
]);
2272 snprintf(propname
, sizeof(propname
), "%s",
2276 desc
= acpi_get_gpiod_by_index(adev
, propname
, idx
, &info
);
2277 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
2281 /* Then from plain _CRS GPIOs */
2283 if (!acpi_can_fallback_to_crs(adev
, con_id
))
2284 return ERR_PTR(-ENOENT
);
2286 desc
= acpi_get_gpiod_by_index(adev
, NULL
, idx
, &info
);
2291 if (info
.polarity
== GPIO_ACTIVE_LOW
)
2292 *flags
|= GPIO_ACTIVE_LOW
;
2297 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
2299 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
2300 struct gpiod_lookup_table
*table
;
2302 mutex_lock(&gpio_lookup_lock
);
2304 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
2305 if (table
->dev_id
&& dev_id
) {
2307 * Valid strings on both ends, must be identical to have
2310 if (!strcmp(table
->dev_id
, dev_id
))
2314 * One of the pointers is NULL, so both must be to have
2317 if (dev_id
== table
->dev_id
)
2324 mutex_unlock(&gpio_lookup_lock
);
2328 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
2330 enum gpio_lookup_flags
*flags
)
2332 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
2333 struct gpiod_lookup_table
*table
;
2334 struct gpiod_lookup
*p
;
2336 table
= gpiod_find_lookup_table(dev
);
2340 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
2341 struct gpio_chip
*chip
;
2343 /* idx must always match exactly */
2347 /* If the lookup entry has a con_id, require exact match */
2348 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
2351 chip
= find_chip_by_name(p
->chip_label
);
2354 dev_err(dev
, "cannot find GPIO chip %s\n",
2356 return ERR_PTR(-ENODEV
);
2359 if (chip
->ngpio
<= p
->chip_hwnum
) {
2361 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2362 idx
, chip
->ngpio
, chip
->label
);
2363 return ERR_PTR(-EINVAL
);
2366 desc
= gpiochip_get_desc(chip
, p
->chip_hwnum
);
2375 static int dt_gpio_count(struct device
*dev
, const char *con_id
)
2381 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
2383 snprintf(propname
, sizeof(propname
), "%s-%s",
2384 con_id
, gpio_suffixes
[i
]);
2386 snprintf(propname
, sizeof(propname
), "%s",
2389 ret
= of_gpio_named_count(dev
->of_node
, propname
);
2396 static int platform_gpio_count(struct device
*dev
, const char *con_id
)
2398 struct gpiod_lookup_table
*table
;
2399 struct gpiod_lookup
*p
;
2400 unsigned int count
= 0;
2402 table
= gpiod_find_lookup_table(dev
);
2406 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
2407 if ((con_id
&& p
->con_id
&& !strcmp(con_id
, p
->con_id
)) ||
2408 (!con_id
&& !p
->con_id
))
2418 * gpiod_count - return the number of GPIOs associated with a device / function
2419 * or -ENOENT if no GPIO has been assigned to the requested function
2420 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2421 * @con_id: function within the GPIO consumer
2423 int gpiod_count(struct device
*dev
, const char *con_id
)
2425 int count
= -ENOENT
;
2427 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
)
2428 count
= dt_gpio_count(dev
, con_id
);
2429 else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
))
2430 count
= acpi_gpio_count(dev
, con_id
);
2433 count
= platform_gpio_count(dev
, con_id
);
2437 EXPORT_SYMBOL_GPL(gpiod_count
);
2440 * gpiod_get - obtain a GPIO for a given GPIO function
2441 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2442 * @con_id: function within the GPIO consumer
2443 * @flags: optional GPIO initialization flags
2445 * Return the GPIO descriptor corresponding to the function con_id of device
2446 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2447 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
2449 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
,
2450 enum gpiod_flags flags
)
2452 return gpiod_get_index(dev
, con_id
, 0, flags
);
2454 EXPORT_SYMBOL_GPL(gpiod_get
);
2457 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2458 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2459 * @con_id: function within the GPIO consumer
2460 * @flags: optional GPIO initialization flags
2462 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2463 * the requested function it will return NULL. This is convenient for drivers
2464 * that need to handle optional GPIOs.
2466 struct gpio_desc
*__must_check
gpiod_get_optional(struct device
*dev
,
2468 enum gpiod_flags flags
)
2470 return gpiod_get_index_optional(dev
, con_id
, 0, flags
);
2472 EXPORT_SYMBOL_GPL(gpiod_get_optional
);
2475 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2476 * @desc: gpio to be setup
2477 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2480 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2482 static void gpiod_parse_flags(struct gpio_desc
*desc
, unsigned long lflags
)
2484 if (lflags
& GPIO_ACTIVE_LOW
)
2485 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2486 if (lflags
& GPIO_OPEN_DRAIN
)
2487 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
2488 if (lflags
& GPIO_OPEN_SOURCE
)
2489 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
2493 * gpiod_configure_flags - helper function to configure a given GPIO
2494 * @desc: gpio whose value will be assigned
2495 * @con_id: function within the GPIO consumer
2496 * @dflags: gpiod_flags - optional GPIO initialization flags
2498 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2499 * requested function and/or index, or another IS_ERR() code if an error
2500 * occurred while trying to acquire the GPIO.
2502 static int gpiod_configure_flags(struct gpio_desc
*desc
, const char *con_id
,
2503 enum gpiod_flags dflags
)
2507 /* No particular flag request, return here... */
2508 if (!(dflags
& GPIOD_FLAGS_BIT_DIR_SET
)) {
2509 pr_debug("no flags found for %s\n", con_id
);
2514 if (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
)
2515 status
= gpiod_direction_output(desc
,
2516 dflags
& GPIOD_FLAGS_BIT_DIR_VAL
);
2518 status
= gpiod_direction_input(desc
);
2524 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2525 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2526 * @con_id: function within the GPIO consumer
2527 * @idx: index of the GPIO to obtain in the consumer
2528 * @flags: optional GPIO initialization flags
2530 * This variant of gpiod_get() allows to access GPIOs other than the first
2531 * defined one for functions that define several GPIOs.
2533 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2534 * requested function and/or index, or another IS_ERR() code if an error
2535 * occurred while trying to acquire the GPIO.
2537 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
2540 enum gpiod_flags flags
)
2542 struct gpio_desc
*desc
= NULL
;
2544 enum gpio_lookup_flags lookupflags
= 0;
2546 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
2549 /* Using device tree? */
2550 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
2551 dev_dbg(dev
, "using device tree for GPIO lookup\n");
2552 desc
= of_find_gpio(dev
, con_id
, idx
, &lookupflags
);
2553 } else if (ACPI_COMPANION(dev
)) {
2554 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
2555 desc
= acpi_find_gpio(dev
, con_id
, idx
, &lookupflags
);
2560 * Either we are not using DT or ACPI, or their lookup did not return
2561 * a result. In that case, use platform lookup as a fallback.
2563 if (!desc
|| desc
== ERR_PTR(-ENOENT
)) {
2564 dev_dbg(dev
, "using lookup tables for GPIO lookup\n");
2565 desc
= gpiod_find(dev
, con_id
, idx
, &lookupflags
);
2569 dev_dbg(dev
, "lookup for GPIO %s failed\n", con_id
);
2573 gpiod_parse_flags(desc
, lookupflags
);
2575 status
= gpiod_request(desc
, con_id
);
2577 return ERR_PTR(status
);
2579 status
= gpiod_configure_flags(desc
, con_id
, flags
);
2581 dev_dbg(dev
, "setup of GPIO %s failed\n", con_id
);
2583 return ERR_PTR(status
);
2588 EXPORT_SYMBOL_GPL(gpiod_get_index
);
2591 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2592 * @fwnode: handle of the firmware node
2593 * @propname: name of the firmware property representing the GPIO
2595 * This function can be used for drivers that get their configuration
2598 * Function properly finds the corresponding GPIO using whatever is the
2599 * underlying firmware interface and then makes sure that the GPIO
2600 * descriptor is requested before it is returned to the caller.
2602 * In case of error an ERR_PTR() is returned.
2604 struct gpio_desc
*fwnode_get_named_gpiod(struct fwnode_handle
*fwnode
,
2605 const char *propname
)
2607 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
2608 bool active_low
= false;
2609 bool single_ended
= false;
2613 return ERR_PTR(-EINVAL
);
2615 if (is_of_node(fwnode
)) {
2616 enum of_gpio_flags flags
;
2618 desc
= of_get_named_gpiod_flags(to_of_node(fwnode
), propname
, 0,
2620 if (!IS_ERR(desc
)) {
2621 active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
2622 single_ended
= flags
& OF_GPIO_SINGLE_ENDED
;
2624 } else if (is_acpi_node(fwnode
)) {
2625 struct acpi_gpio_info info
;
2627 desc
= acpi_node_get_gpiod(fwnode
, propname
, 0, &info
);
2629 active_low
= info
.polarity
== GPIO_ACTIVE_LOW
;
2636 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2640 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
2642 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
2645 ret
= gpiod_request(desc
, NULL
);
2647 return ERR_PTR(ret
);
2651 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod
);
2654 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2656 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2657 * @con_id: function within the GPIO consumer
2658 * @index: index of the GPIO to obtain in the consumer
2659 * @flags: optional GPIO initialization flags
2661 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2662 * specified index was assigned to the requested function it will return NULL.
2663 * This is convenient for drivers that need to handle optional GPIOs.
2665 struct gpio_desc
*__must_check
gpiod_get_index_optional(struct device
*dev
,
2668 enum gpiod_flags flags
)
2670 struct gpio_desc
*desc
;
2672 desc
= gpiod_get_index(dev
, con_id
, index
, flags
);
2674 if (PTR_ERR(desc
) == -ENOENT
)
2680 EXPORT_SYMBOL_GPL(gpiod_get_index_optional
);
2683 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2684 * @desc: gpio whose value will be assigned
2685 * @name: gpio line name
2686 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2688 * @dflags: gpiod_flags - optional GPIO initialization flags
2690 int gpiod_hog(struct gpio_desc
*desc
, const char *name
,
2691 unsigned long lflags
, enum gpiod_flags dflags
)
2693 struct gpio_chip
*chip
;
2694 struct gpio_desc
*local_desc
;
2698 chip
= gpiod_to_chip(desc
);
2699 hwnum
= gpio_chip_hwgpio(desc
);
2701 gpiod_parse_flags(desc
, lflags
);
2703 local_desc
= gpiochip_request_own_desc(chip
, hwnum
, name
);
2704 if (IS_ERR(local_desc
)) {
2705 status
= PTR_ERR(local_desc
);
2706 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
2707 name
, chip
->label
, hwnum
, status
);
2711 status
= gpiod_configure_flags(desc
, name
, dflags
);
2713 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
2714 name
, chip
->label
, hwnum
, status
);
2715 gpiochip_free_own_desc(desc
);
2719 /* Mark GPIO as hogged so it can be identified and removed later */
2720 set_bit(FLAG_IS_HOGGED
, &desc
->flags
);
2722 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2723 desc_to_gpio(desc
), name
,
2724 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ? "output" : "input",
2725 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ?
2726 (dflags
&GPIOD_FLAGS_BIT_DIR_VAL
) ? "/high" : "/low":"");
2732 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2733 * @chip: gpio chip to act on
2735 * This is only used by of_gpiochip_remove to free hogged gpios
2737 static void gpiochip_free_hogs(struct gpio_chip
*chip
)
2741 for (id
= 0; id
< chip
->ngpio
; id
++) {
2742 if (test_bit(FLAG_IS_HOGGED
, &chip
->gpiodev
->descs
[id
].flags
))
2743 gpiochip_free_own_desc(&chip
->gpiodev
->descs
[id
]);
2748 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2749 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2750 * @con_id: function within the GPIO consumer
2751 * @flags: optional GPIO initialization flags
2753 * This function acquires all the GPIOs defined under a given function.
2755 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2756 * no GPIO has been assigned to the requested function, or another IS_ERR()
2757 * code if an error occurred while trying to acquire the GPIOs.
2759 struct gpio_descs
*__must_check
gpiod_get_array(struct device
*dev
,
2761 enum gpiod_flags flags
)
2763 struct gpio_desc
*desc
;
2764 struct gpio_descs
*descs
;
2767 count
= gpiod_count(dev
, con_id
);
2769 return ERR_PTR(count
);
2771 descs
= kzalloc(sizeof(*descs
) + sizeof(descs
->desc
[0]) * count
,
2774 return ERR_PTR(-ENOMEM
);
2776 for (descs
->ndescs
= 0; descs
->ndescs
< count
; ) {
2777 desc
= gpiod_get_index(dev
, con_id
, descs
->ndescs
, flags
);
2779 gpiod_put_array(descs
);
2780 return ERR_CAST(desc
);
2782 descs
->desc
[descs
->ndescs
] = desc
;
2787 EXPORT_SYMBOL_GPL(gpiod_get_array
);
2790 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2792 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2793 * @con_id: function within the GPIO consumer
2794 * @flags: optional GPIO initialization flags
2796 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2797 * assigned to the requested function it will return NULL.
2799 struct gpio_descs
*__must_check
gpiod_get_array_optional(struct device
*dev
,
2801 enum gpiod_flags flags
)
2803 struct gpio_descs
*descs
;
2805 descs
= gpiod_get_array(dev
, con_id
, flags
);
2806 if (IS_ERR(descs
) && (PTR_ERR(descs
) == -ENOENT
))
2811 EXPORT_SYMBOL_GPL(gpiod_get_array_optional
);
2814 * gpiod_put - dispose of a GPIO descriptor
2815 * @desc: GPIO descriptor to dispose of
2817 * No descriptor can be used after gpiod_put() has been called on it.
2819 void gpiod_put(struct gpio_desc
*desc
)
2823 EXPORT_SYMBOL_GPL(gpiod_put
);
2826 * gpiod_put_array - dispose of multiple GPIO descriptors
2827 * @descs: struct gpio_descs containing an array of descriptors
2829 void gpiod_put_array(struct gpio_descs
*descs
)
2833 for (i
= 0; i
< descs
->ndescs
; i
++)
2834 gpiod_put(descs
->desc
[i
]);
2838 EXPORT_SYMBOL_GPL(gpiod_put_array
);
2840 static int __init
gpiolib_dev_init(void)
2844 /* Register GPIO sysfs bus */
2845 ret
= bus_register(&gpio_bus_type
);
2847 pr_err("gpiolib: could not register GPIO bus type\n");
2851 ret
= alloc_chrdev_region(&gpio_devt
, 0, GPIO_DEV_MAX
, "gpiochip");
2853 pr_err("gpiolib: failed to allocate char dev region\n");
2854 bus_unregister(&gpio_bus_type
);
2858 core_initcall(gpiolib_dev_init
);
2860 #ifdef CONFIG_DEBUG_FS
2862 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_device
*gdev
)
2865 struct gpio_chip
*chip
= gdev
->chip
;
2866 unsigned gpio
= gdev
->base
;
2867 struct gpio_desc
*gdesc
= &gdev
->descs
[0];
2871 for (i
= 0; i
< gdev
->ngpio
; i
++, gpio
++, gdesc
++) {
2872 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
)) {
2874 seq_printf(s
, " gpio-%-3d (%-20.20s)\n",
2880 gpiod_get_direction(gdesc
);
2881 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
2882 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
2883 seq_printf(s
, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2884 gpio
, gdesc
->name
? gdesc
->name
: "", gdesc
->label
,
2885 is_out
? "out" : "in ",
2887 ? (chip
->get(chip
, i
) ? "hi" : "lo")
2889 is_irq
? "IRQ" : " ");
2890 seq_printf(s
, "\n");
2894 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
2896 unsigned long flags
;
2897 struct gpio_device
*gdev
= NULL
;
2898 loff_t index
= *pos
;
2902 spin_lock_irqsave(&gpio_lock
, flags
);
2903 list_for_each_entry(gdev
, &gpio_devices
, list
)
2905 spin_unlock_irqrestore(&gpio_lock
, flags
);
2908 spin_unlock_irqrestore(&gpio_lock
, flags
);
2913 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2915 unsigned long flags
;
2916 struct gpio_device
*gdev
= v
;
2919 spin_lock_irqsave(&gpio_lock
, flags
);
2920 if (list_is_last(&gdev
->list
, &gpio_devices
))
2923 ret
= list_entry(gdev
->list
.next
, struct gpio_device
, list
);
2924 spin_unlock_irqrestore(&gpio_lock
, flags
);
2932 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
2936 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
2938 struct gpio_device
*gdev
= v
;
2939 struct gpio_chip
*chip
= gdev
->chip
;
2940 struct device
*parent
;
2943 seq_printf(s
, "%s%s: (dangling chip)", (char *)s
->private,
2944 dev_name(&gdev
->dev
));
2948 seq_printf(s
, "%s%s: GPIOs %d-%d", (char *)s
->private,
2949 dev_name(&gdev
->dev
),
2950 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1);
2951 parent
= chip
->parent
;
2953 seq_printf(s
, ", parent: %s/%s",
2954 parent
->bus
? parent
->bus
->name
: "no-bus",
2957 seq_printf(s
, ", %s", chip
->label
);
2958 if (chip
->can_sleep
)
2959 seq_printf(s
, ", can sleep");
2960 seq_printf(s
, ":\n");
2963 chip
->dbg_show(s
, chip
);
2965 gpiolib_dbg_show(s
, gdev
);
2970 static const struct seq_operations gpiolib_seq_ops
= {
2971 .start
= gpiolib_seq_start
,
2972 .next
= gpiolib_seq_next
,
2973 .stop
= gpiolib_seq_stop
,
2974 .show
= gpiolib_seq_show
,
2977 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
2979 return seq_open(file
, &gpiolib_seq_ops
);
2982 static const struct file_operations gpiolib_operations
= {
2983 .owner
= THIS_MODULE
,
2984 .open
= gpiolib_open
,
2986 .llseek
= seq_lseek
,
2987 .release
= seq_release
,
2990 static int __init
gpiolib_debugfs_init(void)
2992 /* /sys/kernel/debug/gpio */
2993 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
2994 NULL
, NULL
, &gpiolib_operations
);
2997 subsys_initcall(gpiolib_debugfs_init
);
2999 #endif /* DEBUG_FS */