gpio: add userspace ABI for GPIO line information
[deliverable/linux.git] / drivers / gpio / gpiolib.c
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
4 #include <linux/irq.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
8 #include <linux/err.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>
21 #include <linux/fs.h>
22 #include <linux/uaccess.h>
23 #include <uapi/linux/gpio.h>
24
25 #include "gpiolib.h"
26
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/gpio.h>
29
30 /* Implementation infrastructure for GPIO interfaces.
31 *
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.
35 */
36
37
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.
41 *
42 * Otherwise, minimize overhead in what may be bitbanging codepaths.
43 */
44 #ifdef DEBUG
45 #define extra_checks 1
46 #else
47 #define extra_checks 0
48 #endif
49
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 = {
55 .name = "gpio",
56 };
57
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.
61 */
62 DEFINE_SPINLOCK(gpio_lock);
63
64 static DEFINE_MUTEX(gpio_lookup_lock);
65 static LIST_HEAD(gpio_lookup_list);
66 LIST_HEAD(gpio_devices);
67
68 static void gpiochip_free_hogs(struct gpio_chip *chip);
69 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
70
71
72 static inline void desc_set_label(struct gpio_desc *d, const char *label)
73 {
74 d->label = label;
75 }
76
77 /**
78 * Convert a GPIO number to its descriptor
79 */
80 struct gpio_desc *gpio_to_desc(unsigned gpio)
81 {
82 struct gpio_device *gdev;
83 unsigned long flags;
84
85 spin_lock_irqsave(&gpio_lock, flags);
86
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];
92 }
93 }
94
95 spin_unlock_irqrestore(&gpio_lock, flags);
96
97 if (!gpio_is_valid(gpio))
98 WARN(1, "invalid GPIO %d\n", gpio);
99
100 return NULL;
101 }
102 EXPORT_SYMBOL_GPL(gpio_to_desc);
103
104 /**
105 * Get the GPIO descriptor corresponding to the given hw number for this chip.
106 */
107 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
108 u16 hwnum)
109 {
110 struct gpio_device *gdev = chip->gpiodev;
111
112 if (hwnum >= gdev->ngpio)
113 return ERR_PTR(-EINVAL);
114
115 return &gdev->descs[hwnum];
116 }
117
118 /**
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
122 */
123 int desc_to_gpio(const struct gpio_desc *desc)
124 {
125 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
126 }
127 EXPORT_SYMBOL_GPL(desc_to_gpio);
128
129
130 /**
131 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
132 * @desc: descriptor to return the chip of
133 */
134 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
135 {
136 if (!desc || !desc->gdev || !desc->gdev->chip)
137 return NULL;
138 return desc->gdev->chip;
139 }
140 EXPORT_SYMBOL_GPL(gpiod_to_chip);
141
142 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
143 static int gpiochip_find_base(int ngpio)
144 {
145 struct gpio_device *gdev;
146 int base = ARCH_NR_GPIOS - ngpio;
147
148 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
149 /* found a free space? */
150 if (gdev->base + gdev->ngpio <= base)
151 break;
152 else
153 /* nope, check the space right before the chip */
154 base = gdev->base - ngpio;
155 }
156
157 if (gpio_is_valid(base)) {
158 pr_debug("%s: found new base at %d\n", __func__, base);
159 return base;
160 } else {
161 pr_err("%s: cannot find free range\n", __func__);
162 return -ENOSPC;
163 }
164 }
165
166 /**
167 * gpiod_get_direction - return the current direction of a GPIO
168 * @desc: GPIO to get the direction of
169 *
170 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
171 *
172 * This function may sleep if gpiod_cansleep() is true.
173 */
174 int gpiod_get_direction(struct gpio_desc *desc)
175 {
176 struct gpio_chip *chip;
177 unsigned offset;
178 int status = -EINVAL;
179
180 chip = gpiod_to_chip(desc);
181 offset = gpio_chip_hwgpio(desc);
182
183 if (!chip->get_direction)
184 return status;
185
186 status = chip->get_direction(chip, offset);
187 if (status > 0) {
188 /* GPIOF_DIR_IN, or other positive */
189 status = 1;
190 clear_bit(FLAG_IS_OUT, &desc->flags);
191 }
192 if (status == 0) {
193 /* GPIOF_DIR_OUT */
194 set_bit(FLAG_IS_OUT, &desc->flags);
195 }
196 return status;
197 }
198 EXPORT_SYMBOL_GPL(gpiod_get_direction);
199
200 /*
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.
203 *
204 * Return -EBUSY if the new chip overlaps with some other chip's integer
205 * space.
206 */
207 static int gpiodev_add_to_list(struct gpio_device *gdev)
208 {
209 struct gpio_device *iterator;
210 struct gpio_device *previous = NULL;
211
212 if (!gdev->chip)
213 return -EINVAL;
214
215 if (list_empty(&gpio_devices)) {
216 list_add_tail(&gdev->list, &gpio_devices);
217 return 0;
218 }
219
220 list_for_each_entry(iterator, &gpio_devices, list) {
221 if (iterator->base >= gdev->base + gdev->ngpio) {
222 /*
223 * Iterator is the first GPIO chip so there is no
224 * previous one
225 */
226 if (!previous) {
227 goto found;
228 } else {
229 /*
230 * We found a valid range(means
231 * [base, base + ngpio - 1]) between previous
232 * and iterator chip.
233 */
234 if (previous->base + previous->ngpio
235 <= gdev->base)
236 goto found;
237 }
238 }
239 previous = iterator;
240 }
241
242 /*
243 * We are beyond the last chip in the list and iterator now
244 * points to the head.
245 * Let iterator point to the last chip in the list.
246 */
247
248 iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
249 if (iterator->base + iterator->ngpio <= gdev->base) {
250 list_add(&gdev->list, &iterator->list);
251 return 0;
252 }
253
254 dev_err(&gdev->dev,
255 "GPIO integer space overlap, cannot add chip\n");
256 return -EBUSY;
257
258 found:
259 list_add_tail(&gdev->list, &iterator->list);
260 return 0;
261 }
262
263 /**
264 * Convert a GPIO name to its descriptor
265 */
266 static struct gpio_desc *gpio_name_to_desc(const char * const name)
267 {
268 struct gpio_device *gdev;
269 unsigned long flags;
270
271 spin_lock_irqsave(&gpio_lock, flags);
272
273 list_for_each_entry(gdev, &gpio_devices, list) {
274 int i;
275
276 for (i = 0; i != gdev->ngpio; ++i) {
277 struct gpio_desc *desc = &gdev->descs[i];
278
279 if (!desc->name || !name)
280 continue;
281
282 if (!strcmp(desc->name, name)) {
283 spin_unlock_irqrestore(&gpio_lock, flags);
284 return desc;
285 }
286 }
287 }
288
289 spin_unlock_irqrestore(&gpio_lock, flags);
290
291 return NULL;
292 }
293
294 /*
295 * Takes the names from gc->names and checks if they are all unique. If they
296 * are, they are assigned to their gpio descriptors.
297 *
298 * Warning if one of the names is already used for a different GPIO.
299 */
300 static int gpiochip_set_desc_names(struct gpio_chip *gc)
301 {
302 struct gpio_device *gdev = gc->gpiodev;
303 int i;
304
305 if (!gc->names)
306 return 0;
307
308 /* First check all names if they are unique */
309 for (i = 0; i != gc->ngpio; ++i) {
310 struct gpio_desc *gpio;
311
312 gpio = gpio_name_to_desc(gc->names[i]);
313 if (gpio)
314 dev_warn(&gdev->dev,
315 "Detected name collision for GPIO name '%s'\n",
316 gc->names[i]);
317 }
318
319 /* Then add all names to the GPIO descriptors */
320 for (i = 0; i != gc->ngpio; ++i)
321 gdev->descs[i].name = gc->names[i];
322
323 return 0;
324 }
325
326 /**
327 * gpio_ioctl() - ioctl handler for the GPIO chardev
328 */
329 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
330 {
331 struct gpio_device *gdev = filp->private_data;
332 struct gpio_chip *chip = gdev->chip;
333 int __user *ip = (int __user *)arg;
334
335 /* We fail any subsequent ioctl():s when the chip is gone */
336 if (!chip)
337 return -ENODEV;
338
339 /* Fill in the struct and pass to userspace */
340 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
341 struct gpiochip_info chipinfo;
342
343 strncpy(chipinfo.name, dev_name(&gdev->dev),
344 sizeof(chipinfo.name));
345 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
346 strncpy(chipinfo.label, gdev->label,
347 sizeof(chipinfo.label));
348 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
349 chipinfo.lines = gdev->ngpio;
350 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
351 return -EFAULT;
352 return 0;
353 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
354 struct gpioline_info lineinfo;
355 struct gpio_desc *desc;
356
357 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
358 return -EFAULT;
359 if (lineinfo.line_offset > gdev->ngpio)
360 return -EINVAL;
361
362 desc = &gdev->descs[lineinfo.line_offset];
363 if (desc->name) {
364 strncpy(lineinfo.name, desc->name,
365 sizeof(lineinfo.name));
366 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
367 } else {
368 lineinfo.name[0] = '\0';
369 }
370 if (desc->label) {
371 strncpy(lineinfo.label, desc->label,
372 sizeof(lineinfo.label));
373 lineinfo.label[sizeof(lineinfo.label)-1] = '\0';
374 } else {
375 lineinfo.label[0] = '\0';
376 }
377
378 /*
379 * Userspace only need to know that the kernel is using
380 * this GPIO so it can't use it.
381 */
382 lineinfo.flags = 0;
383 if (desc->flags & (FLAG_REQUESTED | FLAG_IS_HOGGED |
384 FLAG_USED_AS_IRQ | FLAG_EXPORT |
385 FLAG_SYSFS))
386 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
387 if (desc->flags & FLAG_IS_OUT)
388 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
389 if (desc->flags & FLAG_ACTIVE_LOW)
390 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
391 if (desc->flags & FLAG_OPEN_DRAIN)
392 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
393 if (desc->flags & FLAG_OPEN_SOURCE)
394 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
395
396 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
397 return -EFAULT;
398 return 0;
399 }
400 return -EINVAL;
401 }
402
403 /**
404 * gpio_chrdev_open() - open the chardev for ioctl operations
405 * @inode: inode for this chardev
406 * @filp: file struct for storing private data
407 * Returns 0 on success
408 */
409 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
410 {
411 struct gpio_device *gdev = container_of(inode->i_cdev,
412 struct gpio_device, chrdev);
413
414 /* Fail on open if the backing gpiochip is gone */
415 if (!gdev || !gdev->chip)
416 return -ENODEV;
417 get_device(&gdev->dev);
418 filp->private_data = gdev;
419 return 0;
420 }
421
422 /**
423 * gpio_chrdev_release() - close chardev after ioctl operations
424 * @inode: inode for this chardev
425 * @filp: file struct for storing private data
426 * Returns 0 on success
427 */
428 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
429 {
430 struct gpio_device *gdev = container_of(inode->i_cdev,
431 struct gpio_device, chrdev);
432
433 if (!gdev)
434 return -ENODEV;
435 put_device(&gdev->dev);
436 return 0;
437 }
438
439
440 static const struct file_operations gpio_fileops = {
441 .release = gpio_chrdev_release,
442 .open = gpio_chrdev_open,
443 .owner = THIS_MODULE,
444 .llseek = noop_llseek,
445 .unlocked_ioctl = gpio_ioctl,
446 .compat_ioctl = gpio_ioctl,
447 };
448
449 static void gpiodevice_release(struct device *dev)
450 {
451 struct gpio_device *gdev = dev_get_drvdata(dev);
452
453 cdev_del(&gdev->chrdev);
454 list_del(&gdev->list);
455 ida_simple_remove(&gpio_ida, gdev->id);
456 kfree(gdev);
457 }
458
459 /**
460 * gpiochip_add_data() - register a gpio_chip
461 * @chip: the chip to register, with chip->base initialized
462 * Context: potentially before irqs will work
463 *
464 * Returns a negative errno if the chip can't be registered, such as
465 * because the chip->base is invalid or already associated with a
466 * different chip. Otherwise it returns zero as a success code.
467 *
468 * When gpiochip_add_data() is called very early during boot, so that GPIOs
469 * can be freely used, the chip->parent device must be registered before
470 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
471 * for GPIOs will fail rudely.
472 *
473 * If chip->base is negative, this requests dynamic assignment of
474 * a range of valid GPIOs.
475 */
476 int gpiochip_add_data(struct gpio_chip *chip, void *data)
477 {
478 unsigned long flags;
479 int status = 0;
480 unsigned i;
481 int base = chip->base;
482 struct gpio_device *gdev;
483
484 /*
485 * First: allocate and populate the internal stat container, and
486 * set up the struct device.
487 */
488 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
489 if (!gdev)
490 return -ENOMEM;
491 gdev->dev.bus = &gpio_bus_type;
492 gdev->chip = chip;
493 chip->gpiodev = gdev;
494 if (chip->parent) {
495 gdev->dev.parent = chip->parent;
496 gdev->dev.of_node = chip->parent->of_node;
497 } else {
498 #ifdef CONFIG_OF_GPIO
499 /* If the gpiochip has an assigned OF node this takes precedence */
500 if (chip->of_node)
501 gdev->dev.of_node = chip->of_node;
502 #endif
503 }
504 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
505 if (gdev->id < 0) {
506 status = gdev->id;
507 goto err_free_gdev;
508 }
509 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
510 device_initialize(&gdev->dev);
511 dev_set_drvdata(&gdev->dev, gdev);
512 if (chip->parent && chip->parent->driver)
513 gdev->owner = chip->parent->driver->owner;
514 else if (chip->owner)
515 /* TODO: remove chip->owner */
516 gdev->owner = chip->owner;
517 else
518 gdev->owner = THIS_MODULE;
519
520 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
521 sizeof(gdev->descs[0]), GFP_KERNEL);
522 if (!gdev->descs) {
523 status = -ENOMEM;
524 goto err_free_gdev;
525 }
526
527 if (chip->ngpio == 0) {
528 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
529 status = -EINVAL;
530 goto err_free_gdev;
531 }
532
533 if (chip->label)
534 gdev->label = devm_kstrdup(&gdev->dev, chip->label, GFP_KERNEL);
535 else
536 gdev->label = devm_kstrdup(&gdev->dev, "unknown", GFP_KERNEL);
537 if (!gdev->label) {
538 status = -ENOMEM;
539 goto err_free_gdev;
540 }
541
542 gdev->ngpio = chip->ngpio;
543 gdev->data = data;
544
545 spin_lock_irqsave(&gpio_lock, flags);
546
547 /*
548 * TODO: this allocates a Linux GPIO number base in the global
549 * GPIO numberspace for this chip. In the long run we want to
550 * get *rid* of this numberspace and use only descriptors, but
551 * it may be a pipe dream. It will not happen before we get rid
552 * of the sysfs interface anyways.
553 */
554 if (base < 0) {
555 base = gpiochip_find_base(chip->ngpio);
556 if (base < 0) {
557 status = base;
558 spin_unlock_irqrestore(&gpio_lock, flags);
559 goto err_free_gdev;
560 }
561 /*
562 * TODO: it should not be necessary to reflect the assigned
563 * base outside of the GPIO subsystem. Go over drivers and
564 * see if anyone makes use of this, else drop this and assign
565 * a poison instead.
566 */
567 chip->base = base;
568 }
569 gdev->base = base;
570
571 status = gpiodev_add_to_list(gdev);
572 if (status) {
573 spin_unlock_irqrestore(&gpio_lock, flags);
574 goto err_free_gdev;
575 }
576
577 for (i = 0; i < chip->ngpio; i++) {
578 struct gpio_desc *desc = &gdev->descs[i];
579
580 desc->gdev = gdev;
581
582 /* REVISIT: most hardware initializes GPIOs as inputs (often
583 * with pullups enabled) so power usage is minimized. Linux
584 * code should set the gpio direction first thing; but until
585 * it does, and in case chip->get_direction is not set, we may
586 * expose the wrong direction in sysfs.
587 */
588 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
589 }
590
591 spin_unlock_irqrestore(&gpio_lock, flags);
592
593 #ifdef CONFIG_PINCTRL
594 INIT_LIST_HEAD(&gdev->pin_ranges);
595 #endif
596
597 status = gpiochip_set_desc_names(chip);
598 if (status)
599 goto err_remove_from_list;
600
601 status = of_gpiochip_add(chip);
602 if (status)
603 goto err_remove_chip;
604
605 acpi_gpiochip_add(chip);
606
607 /*
608 * By first adding the chardev, and then adding the device,
609 * we get a device node entry in sysfs under
610 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
611 * coldplug of device nodes and other udev business.
612 */
613 cdev_init(&gdev->chrdev, &gpio_fileops);
614 gdev->chrdev.owner = THIS_MODULE;
615 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
616 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
617 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
618 if (status < 0)
619 chip_warn(chip, "failed to add char device %d:%d\n",
620 MAJOR(gpio_devt), gdev->id);
621 else
622 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
623 MAJOR(gpio_devt), gdev->id);
624 status = device_add(&gdev->dev);
625 if (status)
626 goto err_remove_chardev;
627
628 status = gpiochip_sysfs_register(gdev);
629 if (status)
630 goto err_remove_device;
631
632 /* From this point, the .release() function cleans up gpio_device */
633 gdev->dev.release = gpiodevice_release;
634 get_device(&gdev->dev);
635 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
636 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
637 dev_name(&gdev->dev), chip->label ? : "generic");
638
639 return 0;
640
641 err_remove_device:
642 device_del(&gdev->dev);
643 err_remove_chardev:
644 cdev_del(&gdev->chrdev);
645 err_remove_chip:
646 acpi_gpiochip_remove(chip);
647 gpiochip_free_hogs(chip);
648 of_gpiochip_remove(chip);
649 err_remove_from_list:
650 spin_lock_irqsave(&gpio_lock, flags);
651 list_del(&gdev->list);
652 spin_unlock_irqrestore(&gpio_lock, flags);
653 err_free_gdev:
654 ida_simple_remove(&gpio_ida, gdev->id);
655 /* failures here can mean systems won't boot... */
656 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
657 gdev->base, gdev->base + gdev->ngpio - 1,
658 chip->label ? : "generic");
659 kfree(gdev);
660 return status;
661 }
662 EXPORT_SYMBOL_GPL(gpiochip_add_data);
663
664 /**
665 * gpiochip_get_data() - get per-subdriver data for the chip
666 */
667 void *gpiochip_get_data(struct gpio_chip *chip)
668 {
669 return chip->gpiodev->data;
670 }
671 EXPORT_SYMBOL_GPL(gpiochip_get_data);
672
673 /**
674 * gpiochip_remove() - unregister a gpio_chip
675 * @chip: the chip to unregister
676 *
677 * A gpio_chip with any GPIOs still requested may not be removed.
678 */
679 void gpiochip_remove(struct gpio_chip *chip)
680 {
681 struct gpio_device *gdev = chip->gpiodev;
682 struct gpio_desc *desc;
683 unsigned long flags;
684 unsigned i;
685 bool requested = false;
686
687 /* Numb the device, cancelling all outstanding operations */
688 gdev->chip = NULL;
689
690 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
691 gpiochip_sysfs_unregister(gdev);
692 gpiochip_irqchip_remove(chip);
693 acpi_gpiochip_remove(chip);
694 gpiochip_remove_pin_ranges(chip);
695 gpiochip_free_hogs(chip);
696 of_gpiochip_remove(chip);
697 /*
698 * We accept no more calls into the driver from this point, so
699 * NULL the driver data pointer
700 */
701 gdev->data = NULL;
702
703 spin_lock_irqsave(&gpio_lock, flags);
704 for (i = 0; i < gdev->ngpio; i++) {
705 desc = &gdev->descs[i];
706 if (test_bit(FLAG_REQUESTED, &desc->flags))
707 requested = true;
708 }
709 spin_unlock_irqrestore(&gpio_lock, flags);
710
711 if (requested)
712 dev_crit(&gdev->dev,
713 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
714
715 /*
716 * The gpiochip side puts its use of the device to rest here:
717 * if there are no userspace clients, the chardev and device will
718 * be removed, else it will be dangling until the last user is
719 * gone.
720 */
721 put_device(&gdev->dev);
722 }
723 EXPORT_SYMBOL_GPL(gpiochip_remove);
724
725 /**
726 * gpiochip_find() - iterator for locating a specific gpio_chip
727 * @data: data to pass to match function
728 * @callback: Callback function to check gpio_chip
729 *
730 * Similar to bus_find_device. It returns a reference to a gpio_chip as
731 * determined by a user supplied @match callback. The callback should return
732 * 0 if the device doesn't match and non-zero if it does. If the callback is
733 * non-zero, this function will return to the caller and not iterate over any
734 * more gpio_chips.
735 */
736 struct gpio_chip *gpiochip_find(void *data,
737 int (*match)(struct gpio_chip *chip,
738 void *data))
739 {
740 struct gpio_device *gdev;
741 struct gpio_chip *chip;
742 unsigned long flags;
743
744 spin_lock_irqsave(&gpio_lock, flags);
745 list_for_each_entry(gdev, &gpio_devices, list)
746 if (match(gdev->chip, data))
747 break;
748
749 /* No match? */
750 if (&gdev->list == &gpio_devices)
751 chip = NULL;
752 else
753 chip = gdev->chip;
754
755 spin_unlock_irqrestore(&gpio_lock, flags);
756
757 return chip;
758 }
759 EXPORT_SYMBOL_GPL(gpiochip_find);
760
761 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
762 {
763 const char *name = data;
764
765 return !strcmp(chip->label, name);
766 }
767
768 static struct gpio_chip *find_chip_by_name(const char *name)
769 {
770 return gpiochip_find((void *)name, gpiochip_match_name);
771 }
772
773 #ifdef CONFIG_GPIOLIB_IRQCHIP
774
775 /*
776 * The following is irqchip helper code for gpiochips.
777 */
778
779 /**
780 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
781 * @gpiochip: the gpiochip to set the irqchip chain to
782 * @irqchip: the irqchip to chain to the gpiochip
783 * @parent_irq: the irq number corresponding to the parent IRQ for this
784 * chained irqchip
785 * @parent_handler: the parent interrupt handler for the accumulated IRQ
786 * coming out of the gpiochip. If the interrupt is nested rather than
787 * cascaded, pass NULL in this handler argument
788 */
789 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
790 struct irq_chip *irqchip,
791 int parent_irq,
792 irq_flow_handler_t parent_handler)
793 {
794 unsigned int offset;
795
796 if (!gpiochip->irqdomain) {
797 chip_err(gpiochip, "called %s before setting up irqchip\n",
798 __func__);
799 return;
800 }
801
802 if (parent_handler) {
803 if (gpiochip->can_sleep) {
804 chip_err(gpiochip,
805 "you cannot have chained interrupts on a "
806 "chip that may sleep\n");
807 return;
808 }
809 /*
810 * The parent irqchip is already using the chip_data for this
811 * irqchip, so our callbacks simply use the handler_data.
812 */
813 irq_set_chained_handler_and_data(parent_irq, parent_handler,
814 gpiochip);
815
816 gpiochip->irq_parent = parent_irq;
817 }
818
819 /* Set the parent IRQ for all affected IRQs */
820 for (offset = 0; offset < gpiochip->ngpio; offset++)
821 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
822 parent_irq);
823 }
824 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
825
826 /**
827 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
828 * @d: the irqdomain used by this irqchip
829 * @irq: the global irq number used by this GPIO irqchip irq
830 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
831 *
832 * This function will set up the mapping for a certain IRQ line on a
833 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
834 * stored inside the gpiochip.
835 */
836 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
837 irq_hw_number_t hwirq)
838 {
839 struct gpio_chip *chip = d->host_data;
840
841 irq_set_chip_data(irq, chip);
842 /*
843 * This lock class tells lockdep that GPIO irqs are in a different
844 * category than their parents, so it won't report false recursion.
845 */
846 irq_set_lockdep_class(irq, chip->lock_key);
847 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
848 /* Chips that can sleep need nested thread handlers */
849 if (chip->can_sleep && !chip->irq_not_threaded)
850 irq_set_nested_thread(irq, 1);
851 irq_set_noprobe(irq);
852
853 /*
854 * No set-up of the hardware will happen if IRQ_TYPE_NONE
855 * is passed as default type.
856 */
857 if (chip->irq_default_type != IRQ_TYPE_NONE)
858 irq_set_irq_type(irq, chip->irq_default_type);
859
860 return 0;
861 }
862
863 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
864 {
865 struct gpio_chip *chip = d->host_data;
866
867 if (chip->can_sleep)
868 irq_set_nested_thread(irq, 0);
869 irq_set_chip_and_handler(irq, NULL, NULL);
870 irq_set_chip_data(irq, NULL);
871 }
872
873 static const struct irq_domain_ops gpiochip_domain_ops = {
874 .map = gpiochip_irq_map,
875 .unmap = gpiochip_irq_unmap,
876 /* Virtually all GPIO irqchips are twocell:ed */
877 .xlate = irq_domain_xlate_twocell,
878 };
879
880 static int gpiochip_irq_reqres(struct irq_data *d)
881 {
882 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
883
884 if (!try_module_get(chip->gpiodev->owner))
885 return -ENODEV;
886
887 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
888 chip_err(chip,
889 "unable to lock HW IRQ %lu for IRQ\n",
890 d->hwirq);
891 module_put(chip->gpiodev->owner);
892 return -EINVAL;
893 }
894 return 0;
895 }
896
897 static void gpiochip_irq_relres(struct irq_data *d)
898 {
899 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
900
901 gpiochip_unlock_as_irq(chip, d->hwirq);
902 module_put(chip->gpiodev->owner);
903 }
904
905 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
906 {
907 return irq_find_mapping(chip->irqdomain, offset);
908 }
909
910 /**
911 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
912 * @gpiochip: the gpiochip to remove the irqchip from
913 *
914 * This is called only from gpiochip_remove()
915 */
916 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
917 {
918 unsigned int offset;
919
920 acpi_gpiochip_free_interrupts(gpiochip);
921
922 if (gpiochip->irq_parent) {
923 irq_set_chained_handler(gpiochip->irq_parent, NULL);
924 irq_set_handler_data(gpiochip->irq_parent, NULL);
925 }
926
927 /* Remove all IRQ mappings and delete the domain */
928 if (gpiochip->irqdomain) {
929 for (offset = 0; offset < gpiochip->ngpio; offset++)
930 irq_dispose_mapping(
931 irq_find_mapping(gpiochip->irqdomain, offset));
932 irq_domain_remove(gpiochip->irqdomain);
933 }
934
935 if (gpiochip->irqchip) {
936 gpiochip->irqchip->irq_request_resources = NULL;
937 gpiochip->irqchip->irq_release_resources = NULL;
938 gpiochip->irqchip = NULL;
939 }
940 }
941
942 /**
943 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
944 * @gpiochip: the gpiochip to add the irqchip to
945 * @irqchip: the irqchip to add to the gpiochip
946 * @first_irq: if not dynamically assigned, the base (first) IRQ to
947 * allocate gpiochip irqs from
948 * @handler: the irq handler to use (often a predefined irq core function)
949 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
950 * to have the core avoid setting up any default type in the hardware.
951 * @lock_key: lockdep class
952 *
953 * This function closely associates a certain irqchip with a certain
954 * gpiochip, providing an irq domain to translate the local IRQs to
955 * global irqs in the gpiolib core, and making sure that the gpiochip
956 * is passed as chip data to all related functions. Driver callbacks
957 * need to use gpiochip_get_data() to get their local state containers back
958 * from the gpiochip passed as chip data. An irqdomain will be stored
959 * in the gpiochip that shall be used by the driver to handle IRQ number
960 * translation. The gpiochip will need to be initialized and registered
961 * before calling this function.
962 *
963 * This function will handle two cell:ed simple IRQs and assumes all
964 * the pins on the gpiochip can generate a unique IRQ. Everything else
965 * need to be open coded.
966 */
967 int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
968 struct irq_chip *irqchip,
969 unsigned int first_irq,
970 irq_flow_handler_t handler,
971 unsigned int type,
972 struct lock_class_key *lock_key)
973 {
974 struct device_node *of_node;
975 unsigned int offset;
976 unsigned irq_base = 0;
977
978 if (!gpiochip || !irqchip)
979 return -EINVAL;
980
981 if (!gpiochip->parent) {
982 pr_err("missing gpiochip .dev parent pointer\n");
983 return -EINVAL;
984 }
985 of_node = gpiochip->parent->of_node;
986 #ifdef CONFIG_OF_GPIO
987 /*
988 * If the gpiochip has an assigned OF node this takes precedence
989 * FIXME: get rid of this and use gpiochip->parent->of_node
990 * everywhere
991 */
992 if (gpiochip->of_node)
993 of_node = gpiochip->of_node;
994 #endif
995 gpiochip->irqchip = irqchip;
996 gpiochip->irq_handler = handler;
997 gpiochip->irq_default_type = type;
998 gpiochip->to_irq = gpiochip_to_irq;
999 gpiochip->lock_key = lock_key;
1000 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1001 gpiochip->ngpio, first_irq,
1002 &gpiochip_domain_ops, gpiochip);
1003 if (!gpiochip->irqdomain) {
1004 gpiochip->irqchip = NULL;
1005 return -EINVAL;
1006 }
1007
1008 /*
1009 * It is possible for a driver to override this, but only if the
1010 * alternative functions are both implemented.
1011 */
1012 if (!irqchip->irq_request_resources &&
1013 !irqchip->irq_release_resources) {
1014 irqchip->irq_request_resources = gpiochip_irq_reqres;
1015 irqchip->irq_release_resources = gpiochip_irq_relres;
1016 }
1017
1018 /*
1019 * Prepare the mapping since the irqchip shall be orthogonal to
1020 * any gpiochip calls. If the first_irq was zero, this is
1021 * necessary to allocate descriptors for all IRQs.
1022 */
1023 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1024 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1025 if (offset == 0)
1026 /*
1027 * Store the base into the gpiochip to be used when
1028 * unmapping the irqs.
1029 */
1030 gpiochip->irq_base = irq_base;
1031 }
1032
1033 acpi_gpiochip_request_interrupts(gpiochip);
1034
1035 return 0;
1036 }
1037 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
1038
1039 #else /* CONFIG_GPIOLIB_IRQCHIP */
1040
1041 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1042
1043 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1044
1045 /**
1046 * gpiochip_generic_request() - request the gpio function for a pin
1047 * @chip: the gpiochip owning the GPIO
1048 * @offset: the offset of the GPIO to request for GPIO function
1049 */
1050 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1051 {
1052 return pinctrl_request_gpio(chip->gpiodev->base + offset);
1053 }
1054 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1055
1056 /**
1057 * gpiochip_generic_free() - free the gpio function from a pin
1058 * @chip: the gpiochip to request the gpio function for
1059 * @offset: the offset of the GPIO to free from GPIO function
1060 */
1061 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1062 {
1063 pinctrl_free_gpio(chip->gpiodev->base + offset);
1064 }
1065 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1066
1067 #ifdef CONFIG_PINCTRL
1068
1069 /**
1070 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1071 * @chip: the gpiochip to add the range for
1072 * @pctldev: the pin controller to map to
1073 * @gpio_offset: the start offset in the current gpio_chip number space
1074 * @pin_group: name of the pin group inside the pin controller
1075 */
1076 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1077 struct pinctrl_dev *pctldev,
1078 unsigned int gpio_offset, const char *pin_group)
1079 {
1080 struct gpio_pin_range *pin_range;
1081 struct gpio_device *gdev = chip->gpiodev;
1082 int ret;
1083
1084 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1085 if (!pin_range) {
1086 chip_err(chip, "failed to allocate pin ranges\n");
1087 return -ENOMEM;
1088 }
1089
1090 /* Use local offset as range ID */
1091 pin_range->range.id = gpio_offset;
1092 pin_range->range.gc = chip;
1093 pin_range->range.name = chip->label;
1094 pin_range->range.base = gdev->base + gpio_offset;
1095 pin_range->pctldev = pctldev;
1096
1097 ret = pinctrl_get_group_pins(pctldev, pin_group,
1098 &pin_range->range.pins,
1099 &pin_range->range.npins);
1100 if (ret < 0) {
1101 kfree(pin_range);
1102 return ret;
1103 }
1104
1105 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1106
1107 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1108 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1109 pinctrl_dev_get_devname(pctldev), pin_group);
1110
1111 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1112
1113 return 0;
1114 }
1115 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1116
1117 /**
1118 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1119 * @chip: the gpiochip to add the range for
1120 * @pinctrl_name: the dev_name() of the pin controller to map to
1121 * @gpio_offset: the start offset in the current gpio_chip number space
1122 * @pin_offset: the start offset in the pin controller number space
1123 * @npins: the number of pins from the offset of each pin space (GPIO and
1124 * pin controller) to accumulate in this range
1125 */
1126 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1127 unsigned int gpio_offset, unsigned int pin_offset,
1128 unsigned int npins)
1129 {
1130 struct gpio_pin_range *pin_range;
1131 struct gpio_device *gdev = chip->gpiodev;
1132 int ret;
1133
1134 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1135 if (!pin_range) {
1136 chip_err(chip, "failed to allocate pin ranges\n");
1137 return -ENOMEM;
1138 }
1139
1140 /* Use local offset as range ID */
1141 pin_range->range.id = gpio_offset;
1142 pin_range->range.gc = chip;
1143 pin_range->range.name = chip->label;
1144 pin_range->range.base = gdev->base + gpio_offset;
1145 pin_range->range.pin_base = pin_offset;
1146 pin_range->range.npins = npins;
1147 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1148 &pin_range->range);
1149 if (IS_ERR(pin_range->pctldev)) {
1150 ret = PTR_ERR(pin_range->pctldev);
1151 chip_err(chip, "could not create pin range\n");
1152 kfree(pin_range);
1153 return ret;
1154 }
1155 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1156 gpio_offset, gpio_offset + npins - 1,
1157 pinctl_name,
1158 pin_offset, pin_offset + npins - 1);
1159
1160 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1161
1162 return 0;
1163 }
1164 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1165
1166 /**
1167 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1168 * @chip: the chip to remove all the mappings for
1169 */
1170 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1171 {
1172 struct gpio_pin_range *pin_range, *tmp;
1173 struct gpio_device *gdev = chip->gpiodev;
1174
1175 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1176 list_del(&pin_range->node);
1177 pinctrl_remove_gpio_range(pin_range->pctldev,
1178 &pin_range->range);
1179 kfree(pin_range);
1180 }
1181 }
1182 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1183
1184 #endif /* CONFIG_PINCTRL */
1185
1186 /* These "optional" allocation calls help prevent drivers from stomping
1187 * on each other, and help provide better diagnostics in debugfs.
1188 * They're called even less than the "set direction" calls.
1189 */
1190 static int __gpiod_request(struct gpio_desc *desc, const char *label)
1191 {
1192 struct gpio_chip *chip = desc->gdev->chip;
1193 int status;
1194 unsigned long flags;
1195
1196 spin_lock_irqsave(&gpio_lock, flags);
1197
1198 /* NOTE: gpio_request() can be called in early boot,
1199 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1200 */
1201
1202 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1203 desc_set_label(desc, label ? : "?");
1204 status = 0;
1205 } else {
1206 status = -EBUSY;
1207 goto done;
1208 }
1209
1210 if (chip->request) {
1211 /* chip->request may sleep */
1212 spin_unlock_irqrestore(&gpio_lock, flags);
1213 status = chip->request(chip, gpio_chip_hwgpio(desc));
1214 spin_lock_irqsave(&gpio_lock, flags);
1215
1216 if (status < 0) {
1217 desc_set_label(desc, NULL);
1218 clear_bit(FLAG_REQUESTED, &desc->flags);
1219 goto done;
1220 }
1221 }
1222 if (chip->get_direction) {
1223 /* chip->get_direction may sleep */
1224 spin_unlock_irqrestore(&gpio_lock, flags);
1225 gpiod_get_direction(desc);
1226 spin_lock_irqsave(&gpio_lock, flags);
1227 }
1228 done:
1229 if (status < 0) {
1230 /* Clear flags that might have been set by the caller before
1231 * requesting the GPIO.
1232 */
1233 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1234 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1235 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1236 }
1237 spin_unlock_irqrestore(&gpio_lock, flags);
1238 return status;
1239 }
1240
1241 /*
1242 * This descriptor validation needs to be inserted verbatim into each
1243 * function taking a descriptor, so we need to use a preprocessor
1244 * macro to avoid endless duplication.
1245 */
1246 #define VALIDATE_DESC(desc) do { \
1247 if (!desc || !desc->gdev) { \
1248 pr_warn("%s: invalid GPIO\n", __func__); \
1249 return -EINVAL; \
1250 } \
1251 if ( !desc->gdev->chip ) { \
1252 dev_warn(&desc->gdev->dev, \
1253 "%s: backing chip is gone\n", __func__); \
1254 return 0; \
1255 } } while (0)
1256
1257 #define VALIDATE_DESC_VOID(desc) do { \
1258 if (!desc || !desc->gdev) { \
1259 pr_warn("%s: invalid GPIO\n", __func__); \
1260 return; \
1261 } \
1262 if (!desc->gdev->chip) { \
1263 dev_warn(&desc->gdev->dev, \
1264 "%s: backing chip is gone\n", __func__); \
1265 return; \
1266 } } while (0)
1267
1268
1269 int gpiod_request(struct gpio_desc *desc, const char *label)
1270 {
1271 int status = -EPROBE_DEFER;
1272 struct gpio_device *gdev;
1273
1274 VALIDATE_DESC(desc);
1275 gdev = desc->gdev;
1276
1277 if (try_module_get(gdev->owner)) {
1278 status = __gpiod_request(desc, label);
1279 if (status < 0)
1280 module_put(gdev->owner);
1281 else
1282 get_device(&gdev->dev);
1283 }
1284
1285 if (status)
1286 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1287
1288 return status;
1289 }
1290
1291 static bool __gpiod_free(struct gpio_desc *desc)
1292 {
1293 bool ret = false;
1294 unsigned long flags;
1295 struct gpio_chip *chip;
1296
1297 might_sleep();
1298
1299 gpiod_unexport(desc);
1300
1301 spin_lock_irqsave(&gpio_lock, flags);
1302
1303 chip = desc->gdev->chip;
1304 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1305 if (chip->free) {
1306 spin_unlock_irqrestore(&gpio_lock, flags);
1307 might_sleep_if(chip->can_sleep);
1308 chip->free(chip, gpio_chip_hwgpio(desc));
1309 spin_lock_irqsave(&gpio_lock, flags);
1310 }
1311 desc_set_label(desc, NULL);
1312 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1313 clear_bit(FLAG_REQUESTED, &desc->flags);
1314 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1315 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1316 clear_bit(FLAG_IS_HOGGED, &desc->flags);
1317 ret = true;
1318 }
1319
1320 spin_unlock_irqrestore(&gpio_lock, flags);
1321 return ret;
1322 }
1323
1324 void gpiod_free(struct gpio_desc *desc)
1325 {
1326 if (desc && desc->gdev && __gpiod_free(desc)) {
1327 module_put(desc->gdev->owner);
1328 put_device(&desc->gdev->dev);
1329 } else {
1330 WARN_ON(extra_checks);
1331 }
1332 }
1333
1334 /**
1335 * gpiochip_is_requested - return string iff signal was requested
1336 * @chip: controller managing the signal
1337 * @offset: of signal within controller's 0..(ngpio - 1) range
1338 *
1339 * Returns NULL if the GPIO is not currently requested, else a string.
1340 * The string returned is the label passed to gpio_request(); if none has been
1341 * passed it is a meaningless, non-NULL constant.
1342 *
1343 * This function is for use by GPIO controller drivers. The label can
1344 * help with diagnostics, and knowing that the signal is used as a GPIO
1345 * can help avoid accidentally multiplexing it to another controller.
1346 */
1347 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1348 {
1349 struct gpio_desc *desc;
1350
1351 if (offset >= chip->ngpio)
1352 return NULL;
1353
1354 desc = &chip->gpiodev->descs[offset];
1355
1356 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1357 return NULL;
1358 return desc->label;
1359 }
1360 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1361
1362 /**
1363 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1364 * @desc: GPIO descriptor to request
1365 * @label: label for the GPIO
1366 *
1367 * Function allows GPIO chip drivers to request and use their own GPIO
1368 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1369 * function will not increase reference count of the GPIO chip module. This
1370 * allows the GPIO chip module to be unloaded as needed (we assume that the
1371 * GPIO chip driver handles freeing the GPIOs it has requested).
1372 */
1373 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1374 const char *label)
1375 {
1376 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1377 int err;
1378
1379 if (IS_ERR(desc)) {
1380 chip_err(chip, "failed to get GPIO descriptor\n");
1381 return desc;
1382 }
1383
1384 err = __gpiod_request(desc, label);
1385 if (err < 0)
1386 return ERR_PTR(err);
1387
1388 return desc;
1389 }
1390 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
1391
1392 /**
1393 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1394 * @desc: GPIO descriptor to free
1395 *
1396 * Function frees the given GPIO requested previously with
1397 * gpiochip_request_own_desc().
1398 */
1399 void gpiochip_free_own_desc(struct gpio_desc *desc)
1400 {
1401 if (desc)
1402 __gpiod_free(desc);
1403 }
1404 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
1405
1406 /*
1407 * Drivers MUST set GPIO direction before making get/set calls. In
1408 * some cases this is done in early boot, before IRQs are enabled.
1409 *
1410 * As a rule these aren't called more than once (except for drivers
1411 * using the open-drain emulation idiom) so these are natural places
1412 * to accumulate extra debugging checks. Note that we can't (yet)
1413 * rely on gpio_request() having been called beforehand.
1414 */
1415
1416 /**
1417 * gpiod_direction_input - set the GPIO direction to input
1418 * @desc: GPIO to set to input
1419 *
1420 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1421 * be called safely on it.
1422 *
1423 * Return 0 in case of success, else an error code.
1424 */
1425 int gpiod_direction_input(struct gpio_desc *desc)
1426 {
1427 struct gpio_chip *chip;
1428 int status = -EINVAL;
1429
1430 VALIDATE_DESC(desc);
1431 chip = desc->gdev->chip;
1432
1433 if (!chip->get || !chip->direction_input) {
1434 gpiod_warn(desc,
1435 "%s: missing get() or direction_input() operations\n",
1436 __func__);
1437 return -EIO;
1438 }
1439
1440 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
1441 if (status == 0)
1442 clear_bit(FLAG_IS_OUT, &desc->flags);
1443
1444 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1445
1446 return status;
1447 }
1448 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1449
1450 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1451 {
1452 struct gpio_chip *chip;
1453 int status = -EINVAL;
1454
1455 /* GPIOs used for IRQs shall not be set as output */
1456 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1457 gpiod_err(desc,
1458 "%s: tried to set a GPIO tied to an IRQ as output\n",
1459 __func__);
1460 return -EIO;
1461 }
1462
1463 /* Open drain pin should not be driven to 1 */
1464 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1465 return gpiod_direction_input(desc);
1466
1467 /* Open source pin should not be driven to 0 */
1468 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1469 return gpiod_direction_input(desc);
1470
1471 chip = desc->gdev->chip;
1472 if (!chip->set || !chip->direction_output) {
1473 gpiod_warn(desc,
1474 "%s: missing set() or direction_output() operations\n",
1475 __func__);
1476 return -EIO;
1477 }
1478
1479 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
1480 if (status == 0)
1481 set_bit(FLAG_IS_OUT, &desc->flags);
1482 trace_gpio_value(desc_to_gpio(desc), 0, value);
1483 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1484 return status;
1485 }
1486
1487 /**
1488 * gpiod_direction_output_raw - set the GPIO direction to output
1489 * @desc: GPIO to set to output
1490 * @value: initial output value of the GPIO
1491 *
1492 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1493 * be called safely on it. The initial value of the output must be specified
1494 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1495 *
1496 * Return 0 in case of success, else an error code.
1497 */
1498 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1499 {
1500 VALIDATE_DESC(desc);
1501 return _gpiod_direction_output_raw(desc, value);
1502 }
1503 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1504
1505 /**
1506 * gpiod_direction_output - set the GPIO direction to output
1507 * @desc: GPIO to set to output
1508 * @value: initial output value of the GPIO
1509 *
1510 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1511 * be called safely on it. The initial value of the output must be specified
1512 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1513 * account.
1514 *
1515 * Return 0 in case of success, else an error code.
1516 */
1517 int gpiod_direction_output(struct gpio_desc *desc, int value)
1518 {
1519 VALIDATE_DESC(desc);
1520 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1521 value = !value;
1522 return _gpiod_direction_output_raw(desc, value);
1523 }
1524 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1525
1526 /**
1527 * gpiod_set_debounce - sets @debounce time for a @gpio
1528 * @gpio: the gpio to set debounce time
1529 * @debounce: debounce time is microseconds
1530 *
1531 * returns -ENOTSUPP if the controller does not support setting
1532 * debounce.
1533 */
1534 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1535 {
1536 struct gpio_chip *chip;
1537
1538 VALIDATE_DESC(desc);
1539 chip = desc->gdev->chip;
1540 if (!chip->set || !chip->set_debounce) {
1541 gpiod_dbg(desc,
1542 "%s: missing set() or set_debounce() operations\n",
1543 __func__);
1544 return -ENOTSUPP;
1545 }
1546
1547 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
1548 }
1549 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1550
1551 /**
1552 * gpiod_is_active_low - test whether a GPIO is active-low or not
1553 * @desc: the gpio descriptor to test
1554 *
1555 * Returns 1 if the GPIO is active-low, 0 otherwise.
1556 */
1557 int gpiod_is_active_low(const struct gpio_desc *desc)
1558 {
1559 VALIDATE_DESC(desc);
1560 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1561 }
1562 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1563
1564 /* I/O calls are only valid after configuration completed; the relevant
1565 * "is this a valid GPIO" error checks should already have been done.
1566 *
1567 * "Get" operations are often inlinable as reading a pin value register,
1568 * and masking the relevant bit in that register.
1569 *
1570 * When "set" operations are inlinable, they involve writing that mask to
1571 * one register to set a low value, or a different register to set it high.
1572 * Otherwise locking is needed, so there may be little value to inlining.
1573 *
1574 *------------------------------------------------------------------------
1575 *
1576 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1577 * have requested the GPIO. That can include implicit requesting by
1578 * a direction setting call. Marking a gpio as requested locks its chip
1579 * in memory, guaranteeing that these table lookups need no more locking
1580 * and that gpiochip_remove() will fail.
1581 *
1582 * REVISIT when debugging, consider adding some instrumentation to ensure
1583 * that the GPIO was actually requested.
1584 */
1585
1586 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
1587 {
1588 struct gpio_chip *chip;
1589 int offset;
1590 int value;
1591
1592 chip = desc->gdev->chip;
1593 offset = gpio_chip_hwgpio(desc);
1594 value = chip->get ? chip->get(chip, offset) : -EIO;
1595 value = value < 0 ? value : !!value;
1596 trace_gpio_value(desc_to_gpio(desc), 1, value);
1597 return value;
1598 }
1599
1600 /**
1601 * gpiod_get_raw_value() - return a gpio's raw value
1602 * @desc: gpio whose value will be returned
1603 *
1604 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1605 * its ACTIVE_LOW status, or negative errno on failure.
1606 *
1607 * This function should be called from contexts where we cannot sleep, and will
1608 * complain if the GPIO chip functions potentially sleep.
1609 */
1610 int gpiod_get_raw_value(const struct gpio_desc *desc)
1611 {
1612 VALIDATE_DESC(desc);
1613 /* Should be using gpio_get_value_cansleep() */
1614 WARN_ON(desc->gdev->chip->can_sleep);
1615 return _gpiod_get_raw_value(desc);
1616 }
1617 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1618
1619 /**
1620 * gpiod_get_value() - return a gpio's value
1621 * @desc: gpio whose value will be returned
1622 *
1623 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1624 * account, or negative errno on failure.
1625 *
1626 * This function should be called from contexts where we cannot sleep, and will
1627 * complain if the GPIO chip functions potentially sleep.
1628 */
1629 int gpiod_get_value(const struct gpio_desc *desc)
1630 {
1631 int value;
1632
1633 VALIDATE_DESC(desc);
1634 /* Should be using gpio_get_value_cansleep() */
1635 WARN_ON(desc->gdev->chip->can_sleep);
1636
1637 value = _gpiod_get_raw_value(desc);
1638 if (value < 0)
1639 return value;
1640
1641 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1642 value = !value;
1643
1644 return value;
1645 }
1646 EXPORT_SYMBOL_GPL(gpiod_get_value);
1647
1648 /*
1649 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1650 * @desc: gpio descriptor whose state need to be set.
1651 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1652 */
1653 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
1654 {
1655 int err = 0;
1656 struct gpio_chip *chip = desc->gdev->chip;
1657 int offset = gpio_chip_hwgpio(desc);
1658
1659 if (value) {
1660 err = chip->direction_input(chip, offset);
1661 if (!err)
1662 clear_bit(FLAG_IS_OUT, &desc->flags);
1663 } else {
1664 err = chip->direction_output(chip, offset, 0);
1665 if (!err)
1666 set_bit(FLAG_IS_OUT, &desc->flags);
1667 }
1668 trace_gpio_direction(desc_to_gpio(desc), value, err);
1669 if (err < 0)
1670 gpiod_err(desc,
1671 "%s: Error in set_value for open drain err %d\n",
1672 __func__, err);
1673 }
1674
1675 /*
1676 * _gpio_set_open_source_value() - Set the open source gpio's value.
1677 * @desc: gpio descriptor whose state need to be set.
1678 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1679 */
1680 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
1681 {
1682 int err = 0;
1683 struct gpio_chip *chip = desc->gdev->chip;
1684 int offset = gpio_chip_hwgpio(desc);
1685
1686 if (value) {
1687 err = chip->direction_output(chip, offset, 1);
1688 if (!err)
1689 set_bit(FLAG_IS_OUT, &desc->flags);
1690 } else {
1691 err = chip->direction_input(chip, offset);
1692 if (!err)
1693 clear_bit(FLAG_IS_OUT, &desc->flags);
1694 }
1695 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1696 if (err < 0)
1697 gpiod_err(desc,
1698 "%s: Error in set_value for open source err %d\n",
1699 __func__, err);
1700 }
1701
1702 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1703 {
1704 struct gpio_chip *chip;
1705
1706 chip = desc->gdev->chip;
1707 trace_gpio_value(desc_to_gpio(desc), 0, value);
1708 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1709 _gpio_set_open_drain_value(desc, value);
1710 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1711 _gpio_set_open_source_value(desc, value);
1712 else
1713 chip->set(chip, gpio_chip_hwgpio(desc), value);
1714 }
1715
1716 /*
1717 * set multiple outputs on the same chip;
1718 * use the chip's set_multiple function if available;
1719 * otherwise set the outputs sequentially;
1720 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1721 * defines which outputs are to be changed
1722 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1723 * defines the values the outputs specified by mask are to be set to
1724 */
1725 static void gpio_chip_set_multiple(struct gpio_chip *chip,
1726 unsigned long *mask, unsigned long *bits)
1727 {
1728 if (chip->set_multiple) {
1729 chip->set_multiple(chip, mask, bits);
1730 } else {
1731 int i;
1732 for (i = 0; i < chip->ngpio; i++) {
1733 if (mask[BIT_WORD(i)] == 0) {
1734 /* no more set bits in this mask word;
1735 * skip ahead to the next word */
1736 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1737 continue;
1738 }
1739 /* set outputs if the corresponding mask bit is set */
1740 if (__test_and_clear_bit(i, mask))
1741 chip->set(chip, i, test_bit(i, bits));
1742 }
1743 }
1744 }
1745
1746 static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1747 unsigned int array_size,
1748 struct gpio_desc **desc_array,
1749 int *value_array)
1750 {
1751 int i = 0;
1752
1753 while (i < array_size) {
1754 struct gpio_chip *chip = desc_array[i]->gdev->chip;
1755 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1756 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1757 int count = 0;
1758
1759 if (!can_sleep)
1760 WARN_ON(chip->can_sleep);
1761
1762 memset(mask, 0, sizeof(mask));
1763 do {
1764 struct gpio_desc *desc = desc_array[i];
1765 int hwgpio = gpio_chip_hwgpio(desc);
1766 int value = value_array[i];
1767
1768 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1769 value = !value;
1770 trace_gpio_value(desc_to_gpio(desc), 0, value);
1771 /*
1772 * collect all normal outputs belonging to the same chip
1773 * open drain and open source outputs are set individually
1774 */
1775 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1776 _gpio_set_open_drain_value(desc, value);
1777 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1778 _gpio_set_open_source_value(desc, value);
1779 } else {
1780 __set_bit(hwgpio, mask);
1781 if (value)
1782 __set_bit(hwgpio, bits);
1783 else
1784 __clear_bit(hwgpio, bits);
1785 count++;
1786 }
1787 i++;
1788 } while ((i < array_size) &&
1789 (desc_array[i]->gdev->chip == chip));
1790 /* push collected bits to outputs */
1791 if (count != 0)
1792 gpio_chip_set_multiple(chip, mask, bits);
1793 }
1794 }
1795
1796 /**
1797 * gpiod_set_raw_value() - assign a gpio's raw value
1798 * @desc: gpio whose value will be assigned
1799 * @value: value to assign
1800 *
1801 * Set the raw value of the GPIO, i.e. the value of its physical line without
1802 * regard for its ACTIVE_LOW status.
1803 *
1804 * This function should be called from contexts where we cannot sleep, and will
1805 * complain if the GPIO chip functions potentially sleep.
1806 */
1807 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
1808 {
1809 VALIDATE_DESC_VOID(desc);
1810 /* Should be using gpio_set_value_cansleep() */
1811 WARN_ON(desc->gdev->chip->can_sleep);
1812 _gpiod_set_raw_value(desc, value);
1813 }
1814 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
1815
1816 /**
1817 * gpiod_set_value() - assign a gpio's value
1818 * @desc: gpio whose value will be assigned
1819 * @value: value to assign
1820 *
1821 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1822 * account
1823 *
1824 * This function should be called from contexts where we cannot sleep, and will
1825 * complain if the GPIO chip functions potentially sleep.
1826 */
1827 void gpiod_set_value(struct gpio_desc *desc, int value)
1828 {
1829 VALIDATE_DESC_VOID(desc);
1830 /* Should be using gpio_set_value_cansleep() */
1831 WARN_ON(desc->gdev->chip->can_sleep);
1832 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1833 value = !value;
1834 _gpiod_set_raw_value(desc, value);
1835 }
1836 EXPORT_SYMBOL_GPL(gpiod_set_value);
1837
1838 /**
1839 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1840 * @array_size: number of elements in the descriptor / value arrays
1841 * @desc_array: array of GPIO descriptors whose values will be assigned
1842 * @value_array: array of values to assign
1843 *
1844 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1845 * without regard for their ACTIVE_LOW status.
1846 *
1847 * This function should be called from contexts where we cannot sleep, and will
1848 * complain if the GPIO chip functions potentially sleep.
1849 */
1850 void gpiod_set_raw_array_value(unsigned int array_size,
1851 struct gpio_desc **desc_array, int *value_array)
1852 {
1853 if (!desc_array)
1854 return;
1855 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1856 value_array);
1857 }
1858 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
1859
1860 /**
1861 * gpiod_set_array_value() - assign values to an array of GPIOs
1862 * @array_size: number of elements in the descriptor / value arrays
1863 * @desc_array: array of GPIO descriptors whose values will be assigned
1864 * @value_array: array of values to assign
1865 *
1866 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1867 * into account.
1868 *
1869 * This function should be called from contexts where we cannot sleep, and will
1870 * complain if the GPIO chip functions potentially sleep.
1871 */
1872 void gpiod_set_array_value(unsigned int array_size,
1873 struct gpio_desc **desc_array, int *value_array)
1874 {
1875 if (!desc_array)
1876 return;
1877 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1878 value_array);
1879 }
1880 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
1881
1882 /**
1883 * gpiod_cansleep() - report whether gpio value access may sleep
1884 * @desc: gpio to check
1885 *
1886 */
1887 int gpiod_cansleep(const struct gpio_desc *desc)
1888 {
1889 VALIDATE_DESC(desc);
1890 return desc->gdev->chip->can_sleep;
1891 }
1892 EXPORT_SYMBOL_GPL(gpiod_cansleep);
1893
1894 /**
1895 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1896 * @desc: gpio whose IRQ will be returned (already requested)
1897 *
1898 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1899 * error.
1900 */
1901 int gpiod_to_irq(const struct gpio_desc *desc)
1902 {
1903 struct gpio_chip *chip;
1904 int offset;
1905
1906 VALIDATE_DESC(desc);
1907 chip = desc->gdev->chip;
1908 offset = gpio_chip_hwgpio(desc);
1909 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1910 }
1911 EXPORT_SYMBOL_GPL(gpiod_to_irq);
1912
1913 /**
1914 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1915 * @chip: the chip the GPIO to lock belongs to
1916 * @offset: the offset of the GPIO to lock as IRQ
1917 *
1918 * This is used directly by GPIO drivers that want to lock down
1919 * a certain GPIO line to be used for IRQs.
1920 */
1921 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1922 {
1923 if (offset >= chip->ngpio)
1924 return -EINVAL;
1925
1926 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
1927 chip_err(chip,
1928 "%s: tried to flag a GPIO set as output for IRQ\n",
1929 __func__);
1930 return -EIO;
1931 }
1932
1933 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
1934 return 0;
1935 }
1936 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1937
1938 /**
1939 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1940 * @chip: the chip the GPIO to lock belongs to
1941 * @offset: the offset of the GPIO to lock as IRQ
1942 *
1943 * This is used directly by GPIO drivers that want to indicate
1944 * that a certain GPIO is no longer used exclusively for IRQ.
1945 */
1946 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1947 {
1948 if (offset >= chip->ngpio)
1949 return;
1950
1951 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
1952 }
1953 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1954
1955 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
1956 {
1957 if (offset >= chip->ngpio)
1958 return false;
1959
1960 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
1961 }
1962 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
1963
1964 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
1965 {
1966 if (offset >= chip->ngpio)
1967 return false;
1968
1969 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
1970 }
1971 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
1972
1973 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
1974 {
1975 if (offset >= chip->ngpio)
1976 return false;
1977
1978 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
1979 }
1980 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
1981
1982 /**
1983 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1984 * @desc: gpio whose value will be returned
1985 *
1986 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1987 * its ACTIVE_LOW status, or negative errno on failure.
1988 *
1989 * This function is to be called from contexts that can sleep.
1990 */
1991 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
1992 {
1993 might_sleep_if(extra_checks);
1994 VALIDATE_DESC(desc);
1995 return _gpiod_get_raw_value(desc);
1996 }
1997 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
1998
1999 /**
2000 * gpiod_get_value_cansleep() - return a gpio's value
2001 * @desc: gpio whose value will be returned
2002 *
2003 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2004 * account, or negative errno on failure.
2005 *
2006 * This function is to be called from contexts that can sleep.
2007 */
2008 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2009 {
2010 int value;
2011
2012 might_sleep_if(extra_checks);
2013 VALIDATE_DESC(desc);
2014 value = _gpiod_get_raw_value(desc);
2015 if (value < 0)
2016 return value;
2017
2018 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2019 value = !value;
2020
2021 return value;
2022 }
2023 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2024
2025 /**
2026 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2027 * @desc: gpio whose value will be assigned
2028 * @value: value to assign
2029 *
2030 * Set the raw value of the GPIO, i.e. the value of its physical line without
2031 * regard for its ACTIVE_LOW status.
2032 *
2033 * This function is to be called from contexts that can sleep.
2034 */
2035 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2036 {
2037 might_sleep_if(extra_checks);
2038 VALIDATE_DESC_VOID(desc);
2039 _gpiod_set_raw_value(desc, value);
2040 }
2041 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2042
2043 /**
2044 * gpiod_set_value_cansleep() - assign a gpio's value
2045 * @desc: gpio whose value will be assigned
2046 * @value: value to assign
2047 *
2048 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2049 * account
2050 *
2051 * This function is to be called from contexts that can sleep.
2052 */
2053 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2054 {
2055 might_sleep_if(extra_checks);
2056 VALIDATE_DESC_VOID(desc);
2057 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2058 value = !value;
2059 _gpiod_set_raw_value(desc, value);
2060 }
2061 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2062
2063 /**
2064 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
2065 * @array_size: number of elements in the descriptor / value arrays
2066 * @desc_array: array of GPIO descriptors whose values will be assigned
2067 * @value_array: array of values to assign
2068 *
2069 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2070 * without regard for their ACTIVE_LOW status.
2071 *
2072 * This function is to be called from contexts that can sleep.
2073 */
2074 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2075 struct gpio_desc **desc_array,
2076 int *value_array)
2077 {
2078 might_sleep_if(extra_checks);
2079 if (!desc_array)
2080 return;
2081 gpiod_set_array_value_priv(true, true, array_size, desc_array,
2082 value_array);
2083 }
2084 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
2085
2086 /**
2087 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
2088 * @array_size: number of elements in the descriptor / value arrays
2089 * @desc_array: array of GPIO descriptors whose values will be assigned
2090 * @value_array: array of values to assign
2091 *
2092 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2093 * into account.
2094 *
2095 * This function is to be called from contexts that can sleep.
2096 */
2097 void gpiod_set_array_value_cansleep(unsigned int array_size,
2098 struct gpio_desc **desc_array,
2099 int *value_array)
2100 {
2101 might_sleep_if(extra_checks);
2102 if (!desc_array)
2103 return;
2104 gpiod_set_array_value_priv(false, true, array_size, desc_array,
2105 value_array);
2106 }
2107 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
2108
2109 /**
2110 * gpiod_add_lookup_table() - register GPIO device consumers
2111 * @table: table of consumers to register
2112 */
2113 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
2114 {
2115 mutex_lock(&gpio_lookup_lock);
2116
2117 list_add_tail(&table->list, &gpio_lookup_list);
2118
2119 mutex_unlock(&gpio_lookup_lock);
2120 }
2121
2122 /**
2123 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2124 * @table: table of consumers to unregister
2125 */
2126 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2127 {
2128 mutex_lock(&gpio_lookup_lock);
2129
2130 list_del(&table->list);
2131
2132 mutex_unlock(&gpio_lookup_lock);
2133 }
2134
2135 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2136 unsigned int idx,
2137 enum gpio_lookup_flags *flags)
2138 {
2139 char prop_name[32]; /* 32 is max size of property name */
2140 enum of_gpio_flags of_flags;
2141 struct gpio_desc *desc;
2142 unsigned int i;
2143
2144 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2145 if (con_id)
2146 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
2147 gpio_suffixes[i]);
2148 else
2149 snprintf(prop_name, sizeof(prop_name), "%s",
2150 gpio_suffixes[i]);
2151
2152 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2153 &of_flags);
2154 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2155 break;
2156 }
2157
2158 if (IS_ERR(desc))
2159 return desc;
2160
2161 if (of_flags & OF_GPIO_ACTIVE_LOW)
2162 *flags |= GPIO_ACTIVE_LOW;
2163
2164 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2165 if (of_flags & OF_GPIO_ACTIVE_LOW)
2166 *flags |= GPIO_OPEN_DRAIN;
2167 else
2168 *flags |= GPIO_OPEN_SOURCE;
2169 }
2170
2171 return desc;
2172 }
2173
2174 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2175 unsigned int idx,
2176 enum gpio_lookup_flags *flags)
2177 {
2178 struct acpi_device *adev = ACPI_COMPANION(dev);
2179 struct acpi_gpio_info info;
2180 struct gpio_desc *desc;
2181 char propname[32];
2182 int i;
2183
2184 /* Try first from _DSD */
2185 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2186 if (con_id && strcmp(con_id, "gpios")) {
2187 snprintf(propname, sizeof(propname), "%s-%s",
2188 con_id, gpio_suffixes[i]);
2189 } else {
2190 snprintf(propname, sizeof(propname), "%s",
2191 gpio_suffixes[i]);
2192 }
2193
2194 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2195 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2196 break;
2197 }
2198
2199 /* Then from plain _CRS GPIOs */
2200 if (IS_ERR(desc)) {
2201 if (!acpi_can_fallback_to_crs(adev, con_id))
2202 return ERR_PTR(-ENOENT);
2203
2204 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2205 if (IS_ERR(desc))
2206 return desc;
2207 }
2208
2209 if (info.polarity == GPIO_ACTIVE_LOW)
2210 *flags |= GPIO_ACTIVE_LOW;
2211
2212 return desc;
2213 }
2214
2215 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2216 {
2217 const char *dev_id = dev ? dev_name(dev) : NULL;
2218 struct gpiod_lookup_table *table;
2219
2220 mutex_lock(&gpio_lookup_lock);
2221
2222 list_for_each_entry(table, &gpio_lookup_list, list) {
2223 if (table->dev_id && dev_id) {
2224 /*
2225 * Valid strings on both ends, must be identical to have
2226 * a match
2227 */
2228 if (!strcmp(table->dev_id, dev_id))
2229 goto found;
2230 } else {
2231 /*
2232 * One of the pointers is NULL, so both must be to have
2233 * a match
2234 */
2235 if (dev_id == table->dev_id)
2236 goto found;
2237 }
2238 }
2239 table = NULL;
2240
2241 found:
2242 mutex_unlock(&gpio_lookup_lock);
2243 return table;
2244 }
2245
2246 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2247 unsigned int idx,
2248 enum gpio_lookup_flags *flags)
2249 {
2250 struct gpio_desc *desc = ERR_PTR(-ENOENT);
2251 struct gpiod_lookup_table *table;
2252 struct gpiod_lookup *p;
2253
2254 table = gpiod_find_lookup_table(dev);
2255 if (!table)
2256 return desc;
2257
2258 for (p = &table->table[0]; p->chip_label; p++) {
2259 struct gpio_chip *chip;
2260
2261 /* idx must always match exactly */
2262 if (p->idx != idx)
2263 continue;
2264
2265 /* If the lookup entry has a con_id, require exact match */
2266 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2267 continue;
2268
2269 chip = find_chip_by_name(p->chip_label);
2270
2271 if (!chip) {
2272 dev_err(dev, "cannot find GPIO chip %s\n",
2273 p->chip_label);
2274 return ERR_PTR(-ENODEV);
2275 }
2276
2277 if (chip->ngpio <= p->chip_hwnum) {
2278 dev_err(dev,
2279 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2280 idx, chip->ngpio, chip->label);
2281 return ERR_PTR(-EINVAL);
2282 }
2283
2284 desc = gpiochip_get_desc(chip, p->chip_hwnum);
2285 *flags = p->flags;
2286
2287 return desc;
2288 }
2289
2290 return desc;
2291 }
2292
2293 static int dt_gpio_count(struct device *dev, const char *con_id)
2294 {
2295 int ret;
2296 char propname[32];
2297 unsigned int i;
2298
2299 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2300 if (con_id)
2301 snprintf(propname, sizeof(propname), "%s-%s",
2302 con_id, gpio_suffixes[i]);
2303 else
2304 snprintf(propname, sizeof(propname), "%s",
2305 gpio_suffixes[i]);
2306
2307 ret = of_gpio_named_count(dev->of_node, propname);
2308 if (ret >= 0)
2309 break;
2310 }
2311 return ret;
2312 }
2313
2314 static int platform_gpio_count(struct device *dev, const char *con_id)
2315 {
2316 struct gpiod_lookup_table *table;
2317 struct gpiod_lookup *p;
2318 unsigned int count = 0;
2319
2320 table = gpiod_find_lookup_table(dev);
2321 if (!table)
2322 return -ENOENT;
2323
2324 for (p = &table->table[0]; p->chip_label; p++) {
2325 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2326 (!con_id && !p->con_id))
2327 count++;
2328 }
2329 if (!count)
2330 return -ENOENT;
2331
2332 return count;
2333 }
2334
2335 /**
2336 * gpiod_count - return the number of GPIOs associated with a device / function
2337 * or -ENOENT if no GPIO has been assigned to the requested function
2338 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2339 * @con_id: function within the GPIO consumer
2340 */
2341 int gpiod_count(struct device *dev, const char *con_id)
2342 {
2343 int count = -ENOENT;
2344
2345 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2346 count = dt_gpio_count(dev, con_id);
2347 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2348 count = acpi_gpio_count(dev, con_id);
2349
2350 if (count < 0)
2351 count = platform_gpio_count(dev, con_id);
2352
2353 return count;
2354 }
2355 EXPORT_SYMBOL_GPL(gpiod_count);
2356
2357 /**
2358 * gpiod_get - obtain a GPIO for a given GPIO function
2359 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2360 * @con_id: function within the GPIO consumer
2361 * @flags: optional GPIO initialization flags
2362 *
2363 * Return the GPIO descriptor corresponding to the function con_id of device
2364 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2365 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
2366 */
2367 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
2368 enum gpiod_flags flags)
2369 {
2370 return gpiod_get_index(dev, con_id, 0, flags);
2371 }
2372 EXPORT_SYMBOL_GPL(gpiod_get);
2373
2374 /**
2375 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2376 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2377 * @con_id: function within the GPIO consumer
2378 * @flags: optional GPIO initialization flags
2379 *
2380 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2381 * the requested function it will return NULL. This is convenient for drivers
2382 * that need to handle optional GPIOs.
2383 */
2384 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
2385 const char *con_id,
2386 enum gpiod_flags flags)
2387 {
2388 return gpiod_get_index_optional(dev, con_id, 0, flags);
2389 }
2390 EXPORT_SYMBOL_GPL(gpiod_get_optional);
2391
2392 /**
2393 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2394 * @desc: gpio to be setup
2395 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2396 * of_get_gpio_hog()
2397 *
2398 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2399 */
2400 static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2401 {
2402 if (lflags & GPIO_ACTIVE_LOW)
2403 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2404 if (lflags & GPIO_OPEN_DRAIN)
2405 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2406 if (lflags & GPIO_OPEN_SOURCE)
2407 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2408 }
2409
2410 /**
2411 * gpiod_configure_flags - helper function to configure a given GPIO
2412 * @desc: gpio whose value will be assigned
2413 * @con_id: function within the GPIO consumer
2414 * @dflags: gpiod_flags - optional GPIO initialization flags
2415 *
2416 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2417 * requested function and/or index, or another IS_ERR() code if an error
2418 * occurred while trying to acquire the GPIO.
2419 */
2420 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2421 enum gpiod_flags dflags)
2422 {
2423 int status;
2424
2425 /* No particular flag request, return here... */
2426 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2427 pr_debug("no flags found for %s\n", con_id);
2428 return 0;
2429 }
2430
2431 /* Process flags */
2432 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2433 status = gpiod_direction_output(desc,
2434 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2435 else
2436 status = gpiod_direction_input(desc);
2437
2438 return status;
2439 }
2440
2441 /**
2442 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2443 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2444 * @con_id: function within the GPIO consumer
2445 * @idx: index of the GPIO to obtain in the consumer
2446 * @flags: optional GPIO initialization flags
2447 *
2448 * This variant of gpiod_get() allows to access GPIOs other than the first
2449 * defined one for functions that define several GPIOs.
2450 *
2451 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2452 * requested function and/or index, or another IS_ERR() code if an error
2453 * occurred while trying to acquire the GPIO.
2454 */
2455 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2456 const char *con_id,
2457 unsigned int idx,
2458 enum gpiod_flags flags)
2459 {
2460 struct gpio_desc *desc = NULL;
2461 int status;
2462 enum gpio_lookup_flags lookupflags = 0;
2463
2464 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2465
2466 if (dev) {
2467 /* Using device tree? */
2468 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2469 dev_dbg(dev, "using device tree for GPIO lookup\n");
2470 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2471 } else if (ACPI_COMPANION(dev)) {
2472 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2473 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2474 }
2475 }
2476
2477 /*
2478 * Either we are not using DT or ACPI, or their lookup did not return
2479 * a result. In that case, use platform lookup as a fallback.
2480 */
2481 if (!desc || desc == ERR_PTR(-ENOENT)) {
2482 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
2483 desc = gpiod_find(dev, con_id, idx, &lookupflags);
2484 }
2485
2486 if (IS_ERR(desc)) {
2487 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
2488 return desc;
2489 }
2490
2491 gpiod_parse_flags(desc, lookupflags);
2492
2493 status = gpiod_request(desc, con_id);
2494 if (status < 0)
2495 return ERR_PTR(status);
2496
2497 status = gpiod_configure_flags(desc, con_id, flags);
2498 if (status < 0) {
2499 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2500 gpiod_put(desc);
2501 return ERR_PTR(status);
2502 }
2503
2504 return desc;
2505 }
2506 EXPORT_SYMBOL_GPL(gpiod_get_index);
2507
2508 /**
2509 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2510 * @fwnode: handle of the firmware node
2511 * @propname: name of the firmware property representing the GPIO
2512 *
2513 * This function can be used for drivers that get their configuration
2514 * from firmware.
2515 *
2516 * Function properly finds the corresponding GPIO using whatever is the
2517 * underlying firmware interface and then makes sure that the GPIO
2518 * descriptor is requested before it is returned to the caller.
2519 *
2520 * In case of error an ERR_PTR() is returned.
2521 */
2522 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2523 const char *propname)
2524 {
2525 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2526 bool active_low = false;
2527 bool single_ended = false;
2528 int ret;
2529
2530 if (!fwnode)
2531 return ERR_PTR(-EINVAL);
2532
2533 if (is_of_node(fwnode)) {
2534 enum of_gpio_flags flags;
2535
2536 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
2537 &flags);
2538 if (!IS_ERR(desc)) {
2539 active_low = flags & OF_GPIO_ACTIVE_LOW;
2540 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2541 }
2542 } else if (is_acpi_node(fwnode)) {
2543 struct acpi_gpio_info info;
2544
2545 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
2546 if (!IS_ERR(desc))
2547 active_low = info.polarity == GPIO_ACTIVE_LOW;
2548 }
2549
2550 if (IS_ERR(desc))
2551 return desc;
2552
2553 if (active_low)
2554 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2555
2556 if (single_ended) {
2557 if (active_low)
2558 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2559 else
2560 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2561 }
2562
2563 ret = gpiod_request(desc, NULL);
2564 if (ret)
2565 return ERR_PTR(ret);
2566
2567 return desc;
2568 }
2569 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2570
2571 /**
2572 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2573 * function
2574 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2575 * @con_id: function within the GPIO consumer
2576 * @index: index of the GPIO to obtain in the consumer
2577 * @flags: optional GPIO initialization flags
2578 *
2579 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2580 * specified index was assigned to the requested function it will return NULL.
2581 * This is convenient for drivers that need to handle optional GPIOs.
2582 */
2583 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
2584 const char *con_id,
2585 unsigned int index,
2586 enum gpiod_flags flags)
2587 {
2588 struct gpio_desc *desc;
2589
2590 desc = gpiod_get_index(dev, con_id, index, flags);
2591 if (IS_ERR(desc)) {
2592 if (PTR_ERR(desc) == -ENOENT)
2593 return NULL;
2594 }
2595
2596 return desc;
2597 }
2598 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
2599
2600 /**
2601 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2602 * @desc: gpio whose value will be assigned
2603 * @name: gpio line name
2604 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2605 * of_get_gpio_hog()
2606 * @dflags: gpiod_flags - optional GPIO initialization flags
2607 */
2608 int gpiod_hog(struct gpio_desc *desc, const char *name,
2609 unsigned long lflags, enum gpiod_flags dflags)
2610 {
2611 struct gpio_chip *chip;
2612 struct gpio_desc *local_desc;
2613 int hwnum;
2614 int status;
2615
2616 chip = gpiod_to_chip(desc);
2617 hwnum = gpio_chip_hwgpio(desc);
2618
2619 gpiod_parse_flags(desc, lflags);
2620
2621 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2622 if (IS_ERR(local_desc)) {
2623 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2624 name, chip->label, hwnum);
2625 return PTR_ERR(local_desc);
2626 }
2627
2628 status = gpiod_configure_flags(desc, name, dflags);
2629 if (status < 0) {
2630 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2631 name, chip->label, hwnum);
2632 gpiochip_free_own_desc(desc);
2633 return status;
2634 }
2635
2636 /* Mark GPIO as hogged so it can be identified and removed later */
2637 set_bit(FLAG_IS_HOGGED, &desc->flags);
2638
2639 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2640 desc_to_gpio(desc), name,
2641 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2642 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2643 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2644
2645 return 0;
2646 }
2647
2648 /**
2649 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2650 * @chip: gpio chip to act on
2651 *
2652 * This is only used by of_gpiochip_remove to free hogged gpios
2653 */
2654 static void gpiochip_free_hogs(struct gpio_chip *chip)
2655 {
2656 int id;
2657
2658 for (id = 0; id < chip->ngpio; id++) {
2659 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2660 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
2661 }
2662 }
2663
2664 /**
2665 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2666 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2667 * @con_id: function within the GPIO consumer
2668 * @flags: optional GPIO initialization flags
2669 *
2670 * This function acquires all the GPIOs defined under a given function.
2671 *
2672 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2673 * no GPIO has been assigned to the requested function, or another IS_ERR()
2674 * code if an error occurred while trying to acquire the GPIOs.
2675 */
2676 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2677 const char *con_id,
2678 enum gpiod_flags flags)
2679 {
2680 struct gpio_desc *desc;
2681 struct gpio_descs *descs;
2682 int count;
2683
2684 count = gpiod_count(dev, con_id);
2685 if (count < 0)
2686 return ERR_PTR(count);
2687
2688 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2689 GFP_KERNEL);
2690 if (!descs)
2691 return ERR_PTR(-ENOMEM);
2692
2693 for (descs->ndescs = 0; descs->ndescs < count; ) {
2694 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2695 if (IS_ERR(desc)) {
2696 gpiod_put_array(descs);
2697 return ERR_CAST(desc);
2698 }
2699 descs->desc[descs->ndescs] = desc;
2700 descs->ndescs++;
2701 }
2702 return descs;
2703 }
2704 EXPORT_SYMBOL_GPL(gpiod_get_array);
2705
2706 /**
2707 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2708 * function
2709 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2710 * @con_id: function within the GPIO consumer
2711 * @flags: optional GPIO initialization flags
2712 *
2713 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2714 * assigned to the requested function it will return NULL.
2715 */
2716 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2717 const char *con_id,
2718 enum gpiod_flags flags)
2719 {
2720 struct gpio_descs *descs;
2721
2722 descs = gpiod_get_array(dev, con_id, flags);
2723 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2724 return NULL;
2725
2726 return descs;
2727 }
2728 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2729
2730 /**
2731 * gpiod_put - dispose of a GPIO descriptor
2732 * @desc: GPIO descriptor to dispose of
2733 *
2734 * No descriptor can be used after gpiod_put() has been called on it.
2735 */
2736 void gpiod_put(struct gpio_desc *desc)
2737 {
2738 gpiod_free(desc);
2739 }
2740 EXPORT_SYMBOL_GPL(gpiod_put);
2741
2742 /**
2743 * gpiod_put_array - dispose of multiple GPIO descriptors
2744 * @descs: struct gpio_descs containing an array of descriptors
2745 */
2746 void gpiod_put_array(struct gpio_descs *descs)
2747 {
2748 unsigned int i;
2749
2750 for (i = 0; i < descs->ndescs; i++)
2751 gpiod_put(descs->desc[i]);
2752
2753 kfree(descs);
2754 }
2755 EXPORT_SYMBOL_GPL(gpiod_put_array);
2756
2757 static int __init gpiolib_dev_init(void)
2758 {
2759 int ret;
2760
2761 /* Register GPIO sysfs bus */
2762 ret = bus_register(&gpio_bus_type);
2763 if (ret < 0) {
2764 pr_err("gpiolib: could not register GPIO bus type\n");
2765 return ret;
2766 }
2767
2768 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2769 if (ret < 0) {
2770 pr_err("gpiolib: failed to allocate char dev region\n");
2771 bus_unregister(&gpio_bus_type);
2772 }
2773 return ret;
2774 }
2775 core_initcall(gpiolib_dev_init);
2776
2777 #ifdef CONFIG_DEBUG_FS
2778
2779 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
2780 {
2781 unsigned i;
2782 struct gpio_chip *chip = gdev->chip;
2783 unsigned gpio = gdev->base;
2784 struct gpio_desc *gdesc = &gdev->descs[0];
2785 int is_out;
2786 int is_irq;
2787
2788 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
2789 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2790 if (gdesc->name) {
2791 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2792 gpio, gdesc->name);
2793 }
2794 continue;
2795 }
2796
2797 gpiod_get_direction(gdesc);
2798 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2799 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2800 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2801 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
2802 is_out ? "out" : "in ",
2803 chip->get
2804 ? (chip->get(chip, i) ? "hi" : "lo")
2805 : "? ",
2806 is_irq ? "IRQ" : " ");
2807 seq_printf(s, "\n");
2808 }
2809 }
2810
2811 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2812 {
2813 unsigned long flags;
2814 struct gpio_device *gdev = NULL;
2815 loff_t index = *pos;
2816
2817 s->private = "";
2818
2819 spin_lock_irqsave(&gpio_lock, flags);
2820 list_for_each_entry(gdev, &gpio_devices, list)
2821 if (index-- == 0) {
2822 spin_unlock_irqrestore(&gpio_lock, flags);
2823 return gdev;
2824 }
2825 spin_unlock_irqrestore(&gpio_lock, flags);
2826
2827 return NULL;
2828 }
2829
2830 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2831 {
2832 unsigned long flags;
2833 struct gpio_device *gdev = v;
2834 void *ret = NULL;
2835
2836 spin_lock_irqsave(&gpio_lock, flags);
2837 if (list_is_last(&gdev->list, &gpio_devices))
2838 ret = NULL;
2839 else
2840 ret = list_entry(gdev->list.next, struct gpio_device, list);
2841 spin_unlock_irqrestore(&gpio_lock, flags);
2842
2843 s->private = "\n";
2844 ++*pos;
2845
2846 return ret;
2847 }
2848
2849 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2850 {
2851 }
2852
2853 static int gpiolib_seq_show(struct seq_file *s, void *v)
2854 {
2855 struct gpio_device *gdev = v;
2856 struct gpio_chip *chip = gdev->chip;
2857 struct device *parent;
2858
2859 if (!chip) {
2860 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2861 dev_name(&gdev->dev));
2862 return 0;
2863 }
2864
2865 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2866 dev_name(&gdev->dev),
2867 gdev->base, gdev->base + gdev->ngpio - 1);
2868 parent = chip->parent;
2869 if (parent)
2870 seq_printf(s, ", parent: %s/%s",
2871 parent->bus ? parent->bus->name : "no-bus",
2872 dev_name(parent));
2873 if (chip->label)
2874 seq_printf(s, ", %s", chip->label);
2875 if (chip->can_sleep)
2876 seq_printf(s, ", can sleep");
2877 seq_printf(s, ":\n");
2878
2879 if (chip->dbg_show)
2880 chip->dbg_show(s, chip);
2881 else
2882 gpiolib_dbg_show(s, gdev);
2883
2884 return 0;
2885 }
2886
2887 static const struct seq_operations gpiolib_seq_ops = {
2888 .start = gpiolib_seq_start,
2889 .next = gpiolib_seq_next,
2890 .stop = gpiolib_seq_stop,
2891 .show = gpiolib_seq_show,
2892 };
2893
2894 static int gpiolib_open(struct inode *inode, struct file *file)
2895 {
2896 return seq_open(file, &gpiolib_seq_ops);
2897 }
2898
2899 static const struct file_operations gpiolib_operations = {
2900 .owner = THIS_MODULE,
2901 .open = gpiolib_open,
2902 .read = seq_read,
2903 .llseek = seq_lseek,
2904 .release = seq_release,
2905 };
2906
2907 static int __init gpiolib_debugfs_init(void)
2908 {
2909 /* /sys/kernel/debug/gpio */
2910 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2911 NULL, NULL, &gpiolib_operations);
2912 return 0;
2913 }
2914 subsys_initcall(gpiolib_debugfs_init);
2915
2916 #endif /* DEBUG_FS */
This page took 0.146949 seconds and 5 git commands to generate.