mm-only debug patch...
[mmotm.git] / drivers / gpio / gpiolib.c
blobc7cb7b478aae91690b532eba3508e3bff2ca6004
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/device.h>
7 #include <linux/err.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/gpio.h>
11 #include <linux/idr.h>
14 /* Optional implementation infrastructure for GPIO interfaces.
16 * Platforms may want to use this if they tend to use very many GPIOs
17 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
19 * When kernel footprint or instruction count is an issue, simpler
20 * implementations may be preferred. The GPIO programming interface
21 * allows for inlining speed-critical get/set operations for common
22 * cases, so that access to SOC-integrated GPIOs can sometimes cost
23 * only an instruction or two per bit.
27 /* When debugging, extend minimal trust to callers and platform code.
28 * Also emit diagnostic messages that may help initial bringup, when
29 * board setup or driver bugs are most common.
31 * Otherwise, minimize overhead in what may be bitbanging codepaths.
33 #ifdef DEBUG
34 #define extra_checks 1
35 #else
36 #define extra_checks 0
37 #endif
39 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
40 * While any GPIO is requested, its gpio_chip is not removable;
41 * each GPIO's "requested" flag serves as a lock and refcount.
43 static DEFINE_SPINLOCK(gpio_lock);
45 struct gpio_desc {
46 struct gpio_chip *chip;
47 unsigned long flags;
48 /* flag symbols are bit numbers */
49 #define FLAG_REQUESTED 0
50 #define FLAG_IS_OUT 1
51 #define FLAG_RESERVED 2
52 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
53 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
54 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
55 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
57 #define PDESC_ID_SHIFT 16 /* add new flags before this one */
59 #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
60 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
62 #ifdef CONFIG_DEBUG_FS
63 const char *label;
64 #endif
66 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
68 #ifdef CONFIG_GPIO_SYSFS
69 struct poll_desc {
70 struct work_struct work;
71 struct sysfs_dirent *value_sd;
74 static struct idr pdesc_idr;
75 #endif
77 static inline void desc_set_label(struct gpio_desc *d, const char *label)
79 #ifdef CONFIG_DEBUG_FS
80 d->label = label;
81 #endif
84 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
85 * when setting direction, and otherwise illegal. Until board setup code
86 * and drivers use explicit requests everywhere (which won't happen when
87 * those calls have no teeth) we can't avoid autorequesting. This nag
88 * message should motivate switching to explicit requests... so should
89 * the weaker cleanup after faults, compared to gpio_request().
91 * NOTE: the autorequest mechanism is going away; at this point it's
92 * only "legal" in the sense that (old) code using it won't break yet,
93 * but instead only triggers a WARN() stack dump.
95 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
97 const struct gpio_chip *chip = desc->chip;
98 const int gpio = chip->base + offset;
100 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
101 "autorequest GPIO-%d\n", gpio)) {
102 if (!try_module_get(chip->owner)) {
103 pr_err("GPIO-%d: module can't be gotten \n", gpio);
104 clear_bit(FLAG_REQUESTED, &desc->flags);
105 /* lose */
106 return -EIO;
108 desc_set_label(desc, "[auto]");
109 /* caller must chip->request() w/o spinlock */
110 if (chip->request)
111 return 1;
113 return 0;
116 /* caller holds gpio_lock *OR* gpio is marked as requested */
117 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
119 return gpio_desc[gpio].chip;
122 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
123 static int gpiochip_find_base(int ngpio)
125 int i;
126 int spare = 0;
127 int base = -ENOSPC;
129 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
130 struct gpio_desc *desc = &gpio_desc[i];
131 struct gpio_chip *chip = desc->chip;
133 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
134 spare++;
135 if (spare == ngpio) {
136 base = i;
137 break;
139 } else {
140 spare = 0;
141 if (chip)
142 i -= chip->ngpio - 1;
146 if (gpio_is_valid(base))
147 pr_debug("%s: found new base at %d\n", __func__, base);
148 return base;
152 * gpiochip_reserve() - reserve range of gpios to use with platform code only
153 * @start: starting gpio number
154 * @ngpio: number of gpios to reserve
155 * Context: platform init, potentially before irqs or kmalloc will work
157 * Returns a negative errno if any gpio within the range is already reserved
158 * or registered, else returns zero as a success code. Use this function
159 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
160 * for example because its driver support is not yet loaded.
162 int __init gpiochip_reserve(int start, int ngpio)
164 int ret = 0;
165 unsigned long flags;
166 int i;
168 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
169 return -EINVAL;
171 spin_lock_irqsave(&gpio_lock, flags);
173 for (i = start; i < start + ngpio; i++) {
174 struct gpio_desc *desc = &gpio_desc[i];
176 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
177 ret = -EBUSY;
178 goto err;
181 set_bit(FLAG_RESERVED, &desc->flags);
184 pr_debug("%s: reserved gpios from %d to %d\n",
185 __func__, start, start + ngpio - 1);
186 err:
187 spin_unlock_irqrestore(&gpio_lock, flags);
189 return ret;
192 #ifdef CONFIG_GPIO_SYSFS
194 /* lock protects against unexport_gpio() being called while
195 * sysfs files are active.
197 static DEFINE_MUTEX(sysfs_lock);
200 * /sys/class/gpio/gpioN... only for GPIOs that are exported
201 * /direction
202 * * MAY BE OMITTED if kernel won't allow direction changes
203 * * is read/write as "in" or "out"
204 * * may also be written as "high" or "low", initializing
205 * output value as specified ("out" implies "low")
206 * /value
207 * * always readable, subject to hardware behavior
208 * * may be writable, as zero/nonzero
209 * /edge
210 * * configures behavior of poll(2) on /value
211 * * available only if pin can generate IRQs on input
212 * * is read/write as "none", "falling", "rising", or "both"
215 static ssize_t gpio_direction_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
218 const struct gpio_desc *desc = dev_get_drvdata(dev);
219 ssize_t status;
221 mutex_lock(&sysfs_lock);
223 if (!test_bit(FLAG_EXPORT, &desc->flags))
224 status = -EIO;
225 else
226 status = sprintf(buf, "%s\n",
227 test_bit(FLAG_IS_OUT, &desc->flags)
228 ? "out" : "in");
230 mutex_unlock(&sysfs_lock);
231 return status;
234 static ssize_t gpio_direction_store(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t size)
237 const struct gpio_desc *desc = dev_get_drvdata(dev);
238 unsigned gpio = desc - gpio_desc;
239 ssize_t status;
241 mutex_lock(&sysfs_lock);
243 if (!test_bit(FLAG_EXPORT, &desc->flags))
244 status = -EIO;
245 else if (sysfs_streq(buf, "high"))
246 status = gpio_direction_output(gpio, 1);
247 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
248 status = gpio_direction_output(gpio, 0);
249 else if (sysfs_streq(buf, "in"))
250 status = gpio_direction_input(gpio);
251 else
252 status = -EINVAL;
254 mutex_unlock(&sysfs_lock);
255 return status ? : size;
258 static const DEVICE_ATTR(direction, 0644,
259 gpio_direction_show, gpio_direction_store);
261 static ssize_t gpio_value_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
264 const struct gpio_desc *desc = dev_get_drvdata(dev);
265 unsigned gpio = desc - gpio_desc;
266 ssize_t status;
268 mutex_lock(&sysfs_lock);
270 if (!test_bit(FLAG_EXPORT, &desc->flags))
271 status = -EIO;
272 else
273 status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio));
275 mutex_unlock(&sysfs_lock);
276 return status;
279 static ssize_t gpio_value_store(struct device *dev,
280 struct device_attribute *attr, const char *buf, size_t size)
282 const struct gpio_desc *desc = dev_get_drvdata(dev);
283 unsigned gpio = desc - gpio_desc;
284 ssize_t status;
286 mutex_lock(&sysfs_lock);
288 if (!test_bit(FLAG_EXPORT, &desc->flags))
289 status = -EIO;
290 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
291 status = -EPERM;
292 else {
293 long value;
295 status = strict_strtol(buf, 0, &value);
296 if (status == 0) {
297 gpio_set_value_cansleep(gpio, value != 0);
298 status = size;
302 mutex_unlock(&sysfs_lock);
303 return status;
306 static /*const*/ DEVICE_ATTR(value, 0644,
307 gpio_value_show, gpio_value_store);
309 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
311 struct work_struct *work = priv;
313 schedule_work(work);
314 return IRQ_HANDLED;
317 static void gpio_notify_sysfs(struct work_struct *work)
319 struct poll_desc *pdesc;
321 pdesc = container_of(work, struct poll_desc, work);
322 sysfs_notify_dirent(pdesc->value_sd);
325 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
326 unsigned long gpio_flags)
328 struct poll_desc *pdesc;
329 unsigned long irq_flags;
330 int ret, irq, id;
332 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
333 return 0;
335 irq = gpio_to_irq(desc - gpio_desc);
336 if (irq < 0)
337 return -EIO;
339 id = desc->flags >> PDESC_ID_SHIFT;
340 pdesc = idr_find(&pdesc_idr, id);
341 if (pdesc) {
342 free_irq(irq, &pdesc->work);
343 cancel_work_sync(&pdesc->work);
346 desc->flags &= ~GPIO_TRIGGER_MASK;
348 if (!gpio_flags) {
349 ret = 0;
350 goto free_sd;
353 irq_flags = IRQF_SHARED;
354 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
355 irq_flags |= IRQF_TRIGGER_FALLING;
356 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
357 irq_flags |= IRQF_TRIGGER_RISING;
359 if (!pdesc) {
360 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
361 if (!pdesc) {
362 ret = -ENOMEM;
363 goto err_out;
366 do {
367 ret = -ENOMEM;
368 if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
369 ret = idr_get_new_above(&pdesc_idr,
370 pdesc, 1, &id);
371 } while (ret == -EAGAIN);
373 if (ret)
374 goto free_mem;
376 desc->flags &= GPIO_FLAGS_MASK;
377 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
379 if (desc->flags >> PDESC_ID_SHIFT != id) {
380 ret = -ERANGE;
381 goto free_id;
384 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
385 if (!pdesc->value_sd) {
386 ret = -ENODEV;
387 goto free_id;
389 INIT_WORK(&pdesc->work, gpio_notify_sysfs);
392 ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
393 "gpiolib", &pdesc->work);
394 if (ret)
395 goto free_sd;
397 desc->flags |= gpio_flags;
398 return 0;
400 free_sd:
401 sysfs_put(pdesc->value_sd);
402 free_id:
403 idr_remove(&pdesc_idr, id);
404 desc->flags &= GPIO_FLAGS_MASK;
405 free_mem:
406 kfree(pdesc);
407 err_out:
408 return ret;
411 static const struct {
412 const char *name;
413 unsigned long flags;
414 } trigger_types[] = {
415 { "none", 0 },
416 { "falling", BIT(FLAG_TRIG_FALL) },
417 { "rising", BIT(FLAG_TRIG_RISE) },
418 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
421 static ssize_t gpio_edge_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
424 const struct gpio_desc *desc = dev_get_drvdata(dev);
425 ssize_t status;
427 mutex_lock(&sysfs_lock);
429 if (!test_bit(FLAG_EXPORT, &desc->flags))
430 status = -EIO;
431 else {
432 int i;
434 status = 0;
435 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
436 if ((desc->flags & GPIO_TRIGGER_MASK)
437 == trigger_types[i].flags) {
438 status = sprintf(buf, "%s\n",
439 trigger_types[i].name);
440 break;
444 mutex_unlock(&sysfs_lock);
445 return status;
448 static ssize_t gpio_edge_store(struct device *dev,
449 struct device_attribute *attr, const char *buf, size_t size)
451 struct gpio_desc *desc = dev_get_drvdata(dev);
452 ssize_t status;
453 int i;
455 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
456 if (sysfs_streq(trigger_types[i].name, buf))
457 goto found;
458 return -EINVAL;
460 found:
461 mutex_lock(&sysfs_lock);
463 if (!test_bit(FLAG_EXPORT, &desc->flags))
464 status = -EIO;
465 else {
466 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
467 if (!status)
468 status = size;
471 mutex_unlock(&sysfs_lock);
473 return status;
476 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
478 static const struct attribute *gpio_attrs[] = {
479 &dev_attr_direction.attr,
480 &dev_attr_value.attr,
481 NULL,
484 static const struct attribute_group gpio_attr_group = {
485 .attrs = (struct attribute **) gpio_attrs,
489 * /sys/class/gpio/gpiochipN/
490 * /base ... matching gpio_chip.base (N)
491 * /label ... matching gpio_chip.label
492 * /ngpio ... matching gpio_chip.ngpio
493 * /names ... matching gpio_chip.names
496 static ssize_t chip_base_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
499 const struct gpio_chip *chip = dev_get_drvdata(dev);
501 return sprintf(buf, "%d\n", chip->base);
503 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
505 static ssize_t chip_label_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
508 const struct gpio_chip *chip = dev_get_drvdata(dev);
510 return sprintf(buf, "%s\n", chip->label ? : "");
512 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
514 static ssize_t chip_ngpio_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
517 const struct gpio_chip *chip = dev_get_drvdata(dev);
519 return sprintf(buf, "%u\n", chip->ngpio);
521 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
523 static ssize_t chip_names_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
526 const struct gpio_chip *chip = dev_get_drvdata(dev);
527 char **names = chip->names;
528 int ptr = 0;
529 int name;
531 if (!names)
532 return -EINVAL;
534 for (name = 0; name < chip->ngpio && ptr < PAGE_SIZE; name++)
535 ptr += snprintf(buf + ptr, PAGE_SIZE - ptr,
536 "%s\n", names[name] ? names[name] : "");
538 return ptr;
540 static DEVICE_ATTR(names, 0444, chip_names_show, NULL);
542 static const struct attribute *gpiochip_attrs[] = {
543 &dev_attr_base.attr,
544 &dev_attr_label.attr,
545 &dev_attr_ngpio.attr,
546 &dev_attr_names.attr,
547 NULL,
550 static const struct attribute_group gpiochip_attr_group = {
551 .attrs = (struct attribute **) gpiochip_attrs,
555 * /sys/class/gpio/export ... write-only
556 * integer N ... number of GPIO to export (full access)
557 * /sys/class/gpio/unexport ... write-only
558 * integer N ... number of GPIO to unexport
560 static ssize_t export_store(struct class *class, const char *buf, size_t len)
562 long gpio;
563 int status;
565 status = strict_strtol(buf, 0, &gpio);
566 if (status < 0)
567 goto done;
569 /* No extra locking here; FLAG_SYSFS just signifies that the
570 * request and export were done by on behalf of userspace, so
571 * they may be undone on its behalf too.
574 status = gpio_request(gpio, "sysfs");
575 if (status < 0)
576 goto done;
578 status = gpio_export(gpio, true);
579 if (status < 0)
580 gpio_free(gpio);
581 else
582 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
584 done:
585 if (status)
586 pr_debug("%s: status %d\n", __func__, status);
587 return status ? : len;
590 static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
592 long gpio;
593 int status;
595 status = strict_strtol(buf, 0, &gpio);
596 if (status < 0)
597 goto done;
599 status = -EINVAL;
601 /* reject bogus commands (gpio_unexport ignores them) */
602 if (!gpio_is_valid(gpio))
603 goto done;
605 /* No extra locking here; FLAG_SYSFS just signifies that the
606 * request and export were done by on behalf of userspace, so
607 * they may be undone on its behalf too.
609 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
610 status = 0;
611 gpio_free(gpio);
613 done:
614 if (status)
615 pr_debug("%s: status %d\n", __func__, status);
616 return status ? : len;
619 static struct class_attribute gpio_class_attrs[] = {
620 __ATTR(export, 0200, NULL, export_store),
621 __ATTR(unexport, 0200, NULL, unexport_store),
622 __ATTR_NULL,
625 static struct class gpio_class = {
626 .name = "gpio",
627 .owner = THIS_MODULE,
629 .class_attrs = gpio_class_attrs,
634 * gpio_export - export a GPIO through sysfs
635 * @gpio: gpio to make available, already requested
636 * @direction_may_change: true if userspace may change gpio direction
637 * Context: arch_initcall or later
639 * When drivers want to make a GPIO accessible to userspace after they
640 * have requested it -- perhaps while debugging, or as part of their
641 * public interface -- they may use this routine. If the GPIO can
642 * change direction (some can't) and the caller allows it, userspace
643 * will see "direction" sysfs attribute which may be used to change
644 * the gpio's direction. A "value" attribute will always be provided.
646 * Returns zero on success, else an error.
648 int gpio_export(unsigned gpio, bool direction_may_change)
650 unsigned long flags;
651 struct gpio_desc *desc;
652 int status = -EINVAL;
653 char *ioname = NULL;
655 /* can't export until sysfs is available ... */
656 if (!gpio_class.p) {
657 pr_debug("%s: called too early!\n", __func__);
658 return -ENOENT;
661 if (!gpio_is_valid(gpio))
662 goto done;
664 mutex_lock(&sysfs_lock);
666 spin_lock_irqsave(&gpio_lock, flags);
667 desc = &gpio_desc[gpio];
668 if (test_bit(FLAG_REQUESTED, &desc->flags)
669 && !test_bit(FLAG_EXPORT, &desc->flags)) {
670 status = 0;
671 if (!desc->chip->direction_input
672 || !desc->chip->direction_output)
673 direction_may_change = false;
675 spin_unlock_irqrestore(&gpio_lock, flags);
677 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
678 ioname = desc->chip->names[gpio - desc->chip->base];
680 if (status == 0) {
681 struct device *dev;
683 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
684 desc, ioname ? ioname : "gpio%d", gpio);
685 if (dev) {
686 if (direction_may_change)
687 status = sysfs_create_group(&dev->kobj,
688 &gpio_attr_group);
689 else
690 status = device_create_file(dev,
691 &dev_attr_value);
693 if (!status && gpio_to_irq(gpio) >= 0
694 && (direction_may_change
695 || !test_bit(FLAG_IS_OUT,
696 &desc->flags)))
697 status = device_create_file(dev,
698 &dev_attr_edge);
700 if (status != 0)
701 device_unregister(dev);
702 } else
703 status = -ENODEV;
704 if (status == 0)
705 set_bit(FLAG_EXPORT, &desc->flags);
708 mutex_unlock(&sysfs_lock);
710 done:
711 if (status)
712 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
714 return status;
716 EXPORT_SYMBOL_GPL(gpio_export);
718 static int match_export(struct device *dev, void *data)
720 return dev_get_drvdata(dev) == data;
724 * gpio_export_link - create a sysfs link to an exported GPIO node
725 * @dev: device under which to create symlink
726 * @name: name of the symlink
727 * @gpio: gpio to create symlink to, already exported
729 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
730 * node. Caller is responsible for unlinking.
732 * Returns zero on success, else an error.
734 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
736 struct gpio_desc *desc;
737 int status = -EINVAL;
739 if (!gpio_is_valid(gpio))
740 goto done;
742 mutex_lock(&sysfs_lock);
744 desc = &gpio_desc[gpio];
746 if (test_bit(FLAG_EXPORT, &desc->flags)) {
747 struct device *tdev;
749 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
750 if (tdev != NULL) {
751 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
752 name);
753 } else {
754 status = -ENODEV;
758 mutex_unlock(&sysfs_lock);
760 done:
761 if (status)
762 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
764 return status;
766 EXPORT_SYMBOL_GPL(gpio_export_link);
769 * gpio_unexport - reverse effect of gpio_export()
770 * @gpio: gpio to make unavailable
772 * This is implicit on gpio_free().
774 void gpio_unexport(unsigned gpio)
776 struct gpio_desc *desc;
777 int status = -EINVAL;
779 if (!gpio_is_valid(gpio))
780 goto done;
782 mutex_lock(&sysfs_lock);
784 desc = &gpio_desc[gpio];
786 if (test_bit(FLAG_EXPORT, &desc->flags)) {
787 struct device *dev = NULL;
789 dev = class_find_device(&gpio_class, NULL, desc, match_export);
790 if (dev) {
791 gpio_setup_irq(desc, dev, 0);
792 clear_bit(FLAG_EXPORT, &desc->flags);
793 put_device(dev);
794 device_unregister(dev);
795 status = 0;
796 } else
797 status = -ENODEV;
800 mutex_unlock(&sysfs_lock);
801 done:
802 if (status)
803 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
805 EXPORT_SYMBOL_GPL(gpio_unexport);
807 static int gpiochip_export(struct gpio_chip *chip)
809 int status;
810 struct device *dev;
812 /* Many systems register gpio chips for SOC support very early,
813 * before driver model support is available. In those cases we
814 * export this later, in gpiolib_sysfs_init() ... here we just
815 * verify that _some_ field of gpio_class got initialized.
817 if (!gpio_class.p)
818 return 0;
820 /* use chip->base for the ID; it's already known to be unique */
821 mutex_lock(&sysfs_lock);
822 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
823 "gpiochip%d", chip->base);
824 if (dev) {
825 status = sysfs_create_group(&dev->kobj,
826 &gpiochip_attr_group);
827 } else
828 status = -ENODEV;
829 chip->exported = (status == 0);
830 mutex_unlock(&sysfs_lock);
832 if (status) {
833 unsigned long flags;
834 unsigned gpio;
836 spin_lock_irqsave(&gpio_lock, flags);
837 gpio = chip->base;
838 while (gpio_desc[gpio].chip == chip)
839 gpio_desc[gpio++].chip = NULL;
840 spin_unlock_irqrestore(&gpio_lock, flags);
842 pr_debug("%s: chip %s status %d\n", __func__,
843 chip->label, status);
846 return status;
849 static void gpiochip_unexport(struct gpio_chip *chip)
851 int status;
852 struct device *dev;
854 mutex_lock(&sysfs_lock);
855 dev = class_find_device(&gpio_class, NULL, chip, match_export);
856 if (dev) {
857 put_device(dev);
858 device_unregister(dev);
859 chip->exported = 0;
860 status = 0;
861 } else
862 status = -ENODEV;
863 mutex_unlock(&sysfs_lock);
865 if (status)
866 pr_debug("%s: chip %s status %d\n", __func__,
867 chip->label, status);
870 static int __init gpiolib_sysfs_init(void)
872 int status;
873 unsigned long flags;
874 unsigned gpio;
876 idr_init(&pdesc_idr);
878 status = class_register(&gpio_class);
879 if (status < 0)
880 return status;
882 /* Scan and register the gpio_chips which registered very
883 * early (e.g. before the class_register above was called).
885 * We run before arch_initcall() so chip->dev nodes can have
886 * registered, and so arch_initcall() can always gpio_export().
888 spin_lock_irqsave(&gpio_lock, flags);
889 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
890 struct gpio_chip *chip;
892 chip = gpio_desc[gpio].chip;
893 if (!chip || chip->exported)
894 continue;
896 spin_unlock_irqrestore(&gpio_lock, flags);
897 status = gpiochip_export(chip);
898 spin_lock_irqsave(&gpio_lock, flags);
900 spin_unlock_irqrestore(&gpio_lock, flags);
903 return status;
905 postcore_initcall(gpiolib_sysfs_init);
907 #else
908 static inline int gpiochip_export(struct gpio_chip *chip)
910 return 0;
913 static inline void gpiochip_unexport(struct gpio_chip *chip)
917 #endif /* CONFIG_GPIO_SYSFS */
920 * gpiochip_add() - register a gpio_chip
921 * @chip: the chip to register, with chip->base initialized
922 * Context: potentially before irqs or kmalloc will work
924 * Returns a negative errno if the chip can't be registered, such as
925 * because the chip->base is invalid or already associated with a
926 * different chip. Otherwise it returns zero as a success code.
928 * When gpiochip_add() is called very early during boot, so that GPIOs
929 * can be freely used, the chip->dev device must be registered before
930 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
931 * for GPIOs will fail rudely.
933 * If chip->base is negative, this requests dynamic assignment of
934 * a range of valid GPIOs.
936 int gpiochip_add(struct gpio_chip *chip)
938 unsigned long flags;
939 int status = 0;
940 unsigned id;
941 int base = chip->base;
943 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
944 && base >= 0) {
945 status = -EINVAL;
946 goto fail;
949 spin_lock_irqsave(&gpio_lock, flags);
951 if (base < 0) {
952 base = gpiochip_find_base(chip->ngpio);
953 if (base < 0) {
954 status = base;
955 goto unlock;
957 chip->base = base;
960 /* these GPIO numbers must not be managed by another gpio_chip */
961 for (id = base; id < base + chip->ngpio; id++) {
962 if (gpio_desc[id].chip != NULL) {
963 status = -EBUSY;
964 break;
967 if (status == 0) {
968 for (id = base; id < base + chip->ngpio; id++) {
969 gpio_desc[id].chip = chip;
971 /* REVISIT: most hardware initializes GPIOs as
972 * inputs (often with pullups enabled) so power
973 * usage is minimized. Linux code should set the
974 * gpio direction first thing; but until it does,
975 * we may expose the wrong direction in sysfs.
977 gpio_desc[id].flags = !chip->direction_input
978 ? (1 << FLAG_IS_OUT)
979 : 0;
983 unlock:
984 spin_unlock_irqrestore(&gpio_lock, flags);
985 if (status == 0)
986 status = gpiochip_export(chip);
987 fail:
988 /* failures here can mean systems won't boot... */
989 if (status)
990 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
991 chip->base, chip->base + chip->ngpio - 1,
992 chip->label ? : "generic");
993 return status;
995 EXPORT_SYMBOL_GPL(gpiochip_add);
998 * gpiochip_remove() - unregister a gpio_chip
999 * @chip: the chip to unregister
1001 * A gpio_chip with any GPIOs still requested may not be removed.
1003 int gpiochip_remove(struct gpio_chip *chip)
1005 unsigned long flags;
1006 int status = 0;
1007 unsigned id;
1009 spin_lock_irqsave(&gpio_lock, flags);
1011 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1012 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1013 status = -EBUSY;
1014 break;
1017 if (status == 0) {
1018 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1019 gpio_desc[id].chip = NULL;
1022 spin_unlock_irqrestore(&gpio_lock, flags);
1024 if (status == 0)
1025 gpiochip_unexport(chip);
1027 return status;
1029 EXPORT_SYMBOL_GPL(gpiochip_remove);
1032 /* These "optional" allocation calls help prevent drivers from stomping
1033 * on each other, and help provide better diagnostics in debugfs.
1034 * They're called even less than the "set direction" calls.
1036 int gpio_request(unsigned gpio, const char *label)
1038 struct gpio_desc *desc;
1039 struct gpio_chip *chip;
1040 int status = -EINVAL;
1041 unsigned long flags;
1043 spin_lock_irqsave(&gpio_lock, flags);
1045 if (!gpio_is_valid(gpio))
1046 goto done;
1047 desc = &gpio_desc[gpio];
1048 chip = desc->chip;
1049 if (chip == NULL)
1050 goto done;
1052 if (!try_module_get(chip->owner))
1053 goto done;
1055 /* NOTE: gpio_request() can be called in early boot,
1056 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1059 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1060 desc_set_label(desc, label ? : "?");
1061 status = 0;
1062 } else {
1063 status = -EBUSY;
1064 module_put(chip->owner);
1065 goto done;
1068 if (chip->request) {
1069 /* chip->request may sleep */
1070 spin_unlock_irqrestore(&gpio_lock, flags);
1071 status = chip->request(chip, gpio - chip->base);
1072 spin_lock_irqsave(&gpio_lock, flags);
1074 if (status < 0) {
1075 desc_set_label(desc, NULL);
1076 module_put(chip->owner);
1077 clear_bit(FLAG_REQUESTED, &desc->flags);
1081 done:
1082 if (status)
1083 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1084 gpio, label ? : "?", status);
1085 spin_unlock_irqrestore(&gpio_lock, flags);
1086 return status;
1088 EXPORT_SYMBOL_GPL(gpio_request);
1090 void gpio_free(unsigned gpio)
1092 unsigned long flags;
1093 struct gpio_desc *desc;
1094 struct gpio_chip *chip;
1096 might_sleep();
1098 if (!gpio_is_valid(gpio)) {
1099 WARN_ON(extra_checks);
1100 return;
1103 gpio_unexport(gpio);
1105 spin_lock_irqsave(&gpio_lock, flags);
1107 desc = &gpio_desc[gpio];
1108 chip = desc->chip;
1109 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1110 if (chip->free) {
1111 spin_unlock_irqrestore(&gpio_lock, flags);
1112 might_sleep_if(extra_checks && chip->can_sleep);
1113 chip->free(chip, gpio - chip->base);
1114 spin_lock_irqsave(&gpio_lock, flags);
1116 desc_set_label(desc, NULL);
1117 module_put(desc->chip->owner);
1118 clear_bit(FLAG_REQUESTED, &desc->flags);
1119 } else
1120 WARN_ON(extra_checks);
1122 spin_unlock_irqrestore(&gpio_lock, flags);
1124 EXPORT_SYMBOL_GPL(gpio_free);
1128 * gpiochip_is_requested - return string iff signal was requested
1129 * @chip: controller managing the signal
1130 * @offset: of signal within controller's 0..(ngpio - 1) range
1132 * Returns NULL if the GPIO is not currently requested, else a string.
1133 * If debugfs support is enabled, the string returned is the label passed
1134 * to gpio_request(); otherwise it is a meaningless constant.
1136 * This function is for use by GPIO controller drivers. The label can
1137 * help with diagnostics, and knowing that the signal is used as a GPIO
1138 * can help avoid accidentally multiplexing it to another controller.
1140 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1142 unsigned gpio = chip->base + offset;
1144 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1145 return NULL;
1146 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1147 return NULL;
1148 #ifdef CONFIG_DEBUG_FS
1149 return gpio_desc[gpio].label;
1150 #else
1151 return "?";
1152 #endif
1154 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1157 /* Drivers MUST set GPIO direction before making get/set calls. In
1158 * some cases this is done in early boot, before IRQs are enabled.
1160 * As a rule these aren't called more than once (except for drivers
1161 * using the open-drain emulation idiom) so these are natural places
1162 * to accumulate extra debugging checks. Note that we can't (yet)
1163 * rely on gpio_request() having been called beforehand.
1166 int gpio_direction_input(unsigned gpio)
1168 unsigned long flags;
1169 struct gpio_chip *chip;
1170 struct gpio_desc *desc = &gpio_desc[gpio];
1171 int status = -EINVAL;
1173 spin_lock_irqsave(&gpio_lock, flags);
1175 if (!gpio_is_valid(gpio))
1176 goto fail;
1177 chip = desc->chip;
1178 if (!chip || !chip->get || !chip->direction_input)
1179 goto fail;
1180 gpio -= chip->base;
1181 if (gpio >= chip->ngpio)
1182 goto fail;
1183 status = gpio_ensure_requested(desc, gpio);
1184 if (status < 0)
1185 goto fail;
1187 /* now we know the gpio is valid and chip won't vanish */
1189 spin_unlock_irqrestore(&gpio_lock, flags);
1191 might_sleep_if(extra_checks && chip->can_sleep);
1193 if (status) {
1194 status = chip->request(chip, gpio);
1195 if (status < 0) {
1196 pr_debug("GPIO-%d: chip request fail, %d\n",
1197 chip->base + gpio, status);
1198 /* and it's not available to anyone else ...
1199 * gpio_request() is the fully clean solution.
1201 goto lose;
1205 status = chip->direction_input(chip, gpio);
1206 if (status == 0)
1207 clear_bit(FLAG_IS_OUT, &desc->flags);
1208 lose:
1209 return status;
1210 fail:
1211 spin_unlock_irqrestore(&gpio_lock, flags);
1212 if (status)
1213 pr_debug("%s: gpio-%d status %d\n",
1214 __func__, gpio, status);
1215 return status;
1217 EXPORT_SYMBOL_GPL(gpio_direction_input);
1219 int gpio_direction_output(unsigned gpio, int value)
1221 unsigned long flags;
1222 struct gpio_chip *chip;
1223 struct gpio_desc *desc = &gpio_desc[gpio];
1224 int status = -EINVAL;
1226 spin_lock_irqsave(&gpio_lock, flags);
1228 if (!gpio_is_valid(gpio))
1229 goto fail;
1230 chip = desc->chip;
1231 if (!chip || !chip->set || !chip->direction_output)
1232 goto fail;
1233 gpio -= chip->base;
1234 if (gpio >= chip->ngpio)
1235 goto fail;
1236 status = gpio_ensure_requested(desc, gpio);
1237 if (status < 0)
1238 goto fail;
1240 /* now we know the gpio is valid and chip won't vanish */
1242 spin_unlock_irqrestore(&gpio_lock, flags);
1244 might_sleep_if(extra_checks && chip->can_sleep);
1246 if (status) {
1247 status = chip->request(chip, gpio);
1248 if (status < 0) {
1249 pr_debug("GPIO-%d: chip request fail, %d\n",
1250 chip->base + gpio, status);
1251 /* and it's not available to anyone else ...
1252 * gpio_request() is the fully clean solution.
1254 goto lose;
1258 status = chip->direction_output(chip, gpio, value);
1259 if (status == 0)
1260 set_bit(FLAG_IS_OUT, &desc->flags);
1261 lose:
1262 return status;
1263 fail:
1264 spin_unlock_irqrestore(&gpio_lock, flags);
1265 if (status)
1266 pr_debug("%s: gpio-%d status %d\n",
1267 __func__, gpio, status);
1268 return status;
1270 EXPORT_SYMBOL_GPL(gpio_direction_output);
1273 /* I/O calls are only valid after configuration completed; the relevant
1274 * "is this a valid GPIO" error checks should already have been done.
1276 * "Get" operations are often inlinable as reading a pin value register,
1277 * and masking the relevant bit in that register.
1279 * When "set" operations are inlinable, they involve writing that mask to
1280 * one register to set a low value, or a different register to set it high.
1281 * Otherwise locking is needed, so there may be little value to inlining.
1283 *------------------------------------------------------------------------
1285 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1286 * have requested the GPIO. That can include implicit requesting by
1287 * a direction setting call. Marking a gpio as requested locks its chip
1288 * in memory, guaranteeing that these table lookups need no more locking
1289 * and that gpiochip_remove() will fail.
1291 * REVISIT when debugging, consider adding some instrumentation to ensure
1292 * that the GPIO was actually requested.
1296 * __gpio_get_value() - return a gpio's value
1297 * @gpio: gpio whose value will be returned
1298 * Context: any
1300 * This is used directly or indirectly to implement gpio_get_value().
1301 * It returns the zero or nonzero value provided by the associated
1302 * gpio_chip.get() method; or zero if no such method is provided.
1304 int __gpio_get_value(unsigned gpio)
1306 struct gpio_chip *chip;
1308 chip = gpio_to_chip(gpio);
1309 WARN_ON(extra_checks && chip->can_sleep);
1310 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1312 EXPORT_SYMBOL_GPL(__gpio_get_value);
1315 * __gpio_set_value() - assign a gpio's value
1316 * @gpio: gpio whose value will be assigned
1317 * @value: value to assign
1318 * Context: any
1320 * This is used directly or indirectly to implement gpio_set_value().
1321 * It invokes the associated gpio_chip.set() method.
1323 void __gpio_set_value(unsigned gpio, int value)
1325 struct gpio_chip *chip;
1327 chip = gpio_to_chip(gpio);
1328 WARN_ON(extra_checks && chip->can_sleep);
1329 chip->set(chip, gpio - chip->base, value);
1331 EXPORT_SYMBOL_GPL(__gpio_set_value);
1334 * __gpio_cansleep() - report whether gpio value access will sleep
1335 * @gpio: gpio in question
1336 * Context: any
1338 * This is used directly or indirectly to implement gpio_cansleep(). It
1339 * returns nonzero if access reading or writing the GPIO value can sleep.
1341 int __gpio_cansleep(unsigned gpio)
1343 struct gpio_chip *chip;
1345 /* only call this on GPIOs that are valid! */
1346 chip = gpio_to_chip(gpio);
1348 return chip->can_sleep;
1350 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1353 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1354 * @gpio: gpio whose IRQ will be returned (already requested)
1355 * Context: any
1357 * This is used directly or indirectly to implement gpio_to_irq().
1358 * It returns the number of the IRQ signaled by this (input) GPIO,
1359 * or a negative errno.
1361 int __gpio_to_irq(unsigned gpio)
1363 struct gpio_chip *chip;
1365 chip = gpio_to_chip(gpio);
1366 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1368 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1372 /* There's no value in making it easy to inline GPIO calls that may sleep.
1373 * Common examples include ones connected to I2C or SPI chips.
1376 int gpio_get_value_cansleep(unsigned gpio)
1378 struct gpio_chip *chip;
1380 might_sleep_if(extra_checks);
1381 chip = gpio_to_chip(gpio);
1382 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1384 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1386 void gpio_set_value_cansleep(unsigned gpio, int value)
1388 struct gpio_chip *chip;
1390 might_sleep_if(extra_checks);
1391 chip = gpio_to_chip(gpio);
1392 chip->set(chip, gpio - chip->base, value);
1394 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1397 #ifdef CONFIG_DEBUG_FS
1399 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1401 unsigned i;
1402 unsigned gpio = chip->base;
1403 struct gpio_desc *gdesc = &gpio_desc[gpio];
1404 int is_out;
1406 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1407 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1408 continue;
1410 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1411 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1412 gpio, gdesc->label,
1413 is_out ? "out" : "in ",
1414 chip->get
1415 ? (chip->get(chip, i) ? "hi" : "lo")
1416 : "? ");
1418 if (!is_out) {
1419 int irq = gpio_to_irq(gpio);
1420 struct irq_desc *desc = irq_to_desc(irq);
1422 /* This races with request_irq(), set_irq_type(),
1423 * and set_irq_wake() ... but those are "rare".
1425 * More significantly, trigger type flags aren't
1426 * currently maintained by genirq.
1428 if (irq >= 0 && desc->action) {
1429 char *trigger;
1431 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1432 case IRQ_TYPE_NONE:
1433 trigger = "(default)";
1434 break;
1435 case IRQ_TYPE_EDGE_FALLING:
1436 trigger = "edge-falling";
1437 break;
1438 case IRQ_TYPE_EDGE_RISING:
1439 trigger = "edge-rising";
1440 break;
1441 case IRQ_TYPE_EDGE_BOTH:
1442 trigger = "edge-both";
1443 break;
1444 case IRQ_TYPE_LEVEL_HIGH:
1445 trigger = "level-high";
1446 break;
1447 case IRQ_TYPE_LEVEL_LOW:
1448 trigger = "level-low";
1449 break;
1450 default:
1451 trigger = "?trigger?";
1452 break;
1455 seq_printf(s, " irq-%d %s%s",
1456 irq, trigger,
1457 (desc->status & IRQ_WAKEUP)
1458 ? " wakeup" : "");
1462 seq_printf(s, "\n");
1466 static int gpiolib_show(struct seq_file *s, void *unused)
1468 struct gpio_chip *chip = NULL;
1469 unsigned gpio;
1470 int started = 0;
1472 /* REVISIT this isn't locked against gpio_chip removal ... */
1474 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1475 struct device *dev;
1477 if (chip == gpio_desc[gpio].chip)
1478 continue;
1479 chip = gpio_desc[gpio].chip;
1480 if (!chip)
1481 continue;
1483 seq_printf(s, "%sGPIOs %d-%d",
1484 started ? "\n" : "",
1485 chip->base, chip->base + chip->ngpio - 1);
1486 dev = chip->dev;
1487 if (dev)
1488 seq_printf(s, ", %s/%s",
1489 dev->bus ? dev->bus->name : "no-bus",
1490 dev_name(dev));
1491 if (chip->label)
1492 seq_printf(s, ", %s", chip->label);
1493 if (chip->can_sleep)
1494 seq_printf(s, ", can sleep");
1495 seq_printf(s, ":\n");
1497 started = 1;
1498 if (chip->dbg_show)
1499 chip->dbg_show(s, chip);
1500 else
1501 gpiolib_dbg_show(s, chip);
1503 return 0;
1506 static int gpiolib_open(struct inode *inode, struct file *file)
1508 return single_open(file, gpiolib_show, NULL);
1511 static const struct file_operations gpiolib_operations = {
1512 .open = gpiolib_open,
1513 .read = seq_read,
1514 .llseek = seq_lseek,
1515 .release = single_release,
1518 static int __init gpiolib_debugfs_init(void)
1520 /* /sys/kernel/debug/gpio */
1521 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1522 NULL, NULL, &gpiolib_operations);
1523 return 0;
1525 subsys_initcall(gpiolib_debugfs_init);
1527 #endif /* DEBUG_FS */