gpiolib: fix poll(2) support reconfigure on sysfs polarity change
[safe/jmp/linux-2.6] / 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/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>
12
13
14 /* Optional implementation infrastructure for GPIO interfaces.
15  *
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.
18  *
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.
24  */
25
26
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.
30  *
31  * Otherwise, minimize overhead in what may be bitbanging codepaths.
32  */
33 #ifdef  DEBUG
34 #define extra_checks    1
35 #else
36 #define extra_checks    0
37 #endif
38
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.
42  */
43 static DEFINE_SPINLOCK(gpio_lock);
44
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 */
56 #define FLAG_ACTIVE_LOW 7       /* sysfs value has active low */
57
58 #define PDESC_ID_SHIFT  16      /* add new flags before this one */
59
60 #define GPIO_FLAGS_MASK         ((1 << PDESC_ID_SHIFT) - 1)
61 #define GPIO_TRIGGER_MASK       (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
62
63 #ifdef CONFIG_DEBUG_FS
64         const char              *label;
65 #endif
66 };
67 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
68
69 #ifdef CONFIG_GPIO_SYSFS
70 struct poll_desc {
71         struct work_struct      work;
72         struct sysfs_dirent     *value_sd;
73 };
74
75 static struct idr pdesc_idr;
76 #endif
77
78 static inline void desc_set_label(struct gpio_desc *d, const char *label)
79 {
80 #ifdef CONFIG_DEBUG_FS
81         d->label = label;
82 #endif
83 }
84
85 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
86  * when setting direction, and otherwise illegal.  Until board setup code
87  * and drivers use explicit requests everywhere (which won't happen when
88  * those calls have no teeth) we can't avoid autorequesting.  This nag
89  * message should motivate switching to explicit requests... so should
90  * the weaker cleanup after faults, compared to gpio_request().
91  *
92  * NOTE: the autorequest mechanism is going away; at this point it's
93  * only "legal" in the sense that (old) code using it won't break yet,
94  * but instead only triggers a WARN() stack dump.
95  */
96 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
97 {
98         const struct gpio_chip *chip = desc->chip;
99         const int gpio = chip->base + offset;
100
101         if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
102                         "autorequest GPIO-%d\n", gpio)) {
103                 if (!try_module_get(chip->owner)) {
104                         pr_err("GPIO-%d: module can't be gotten \n", gpio);
105                         clear_bit(FLAG_REQUESTED, &desc->flags);
106                         /* lose */
107                         return -EIO;
108                 }
109                 desc_set_label(desc, "[auto]");
110                 /* caller must chip->request() w/o spinlock */
111                 if (chip->request)
112                         return 1;
113         }
114         return 0;
115 }
116
117 /* caller holds gpio_lock *OR* gpio is marked as requested */
118 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
119 {
120         return gpio_desc[gpio].chip;
121 }
122
123 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
124 static int gpiochip_find_base(int ngpio)
125 {
126         int i;
127         int spare = 0;
128         int base = -ENOSPC;
129
130         for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
131                 struct gpio_desc *desc = &gpio_desc[i];
132                 struct gpio_chip *chip = desc->chip;
133
134                 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
135                         spare++;
136                         if (spare == ngpio) {
137                                 base = i;
138                                 break;
139                         }
140                 } else {
141                         spare = 0;
142                         if (chip)
143                                 i -= chip->ngpio - 1;
144                 }
145         }
146
147         if (gpio_is_valid(base))
148                 pr_debug("%s: found new base at %d\n", __func__, base);
149         return base;
150 }
151
152 /**
153  * gpiochip_reserve() - reserve range of gpios to use with platform code only
154  * @start: starting gpio number
155  * @ngpio: number of gpios to reserve
156  * Context: platform init, potentially before irqs or kmalloc will work
157  *
158  * Returns a negative errno if any gpio within the range is already reserved
159  * or registered, else returns zero as a success code.  Use this function
160  * to mark a range of gpios as unavailable for dynamic gpio number allocation,
161  * for example because its driver support is not yet loaded.
162  */
163 int __init gpiochip_reserve(int start, int ngpio)
164 {
165         int ret = 0;
166         unsigned long flags;
167         int i;
168
169         if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
170                 return -EINVAL;
171
172         spin_lock_irqsave(&gpio_lock, flags);
173
174         for (i = start; i < start + ngpio; i++) {
175                 struct gpio_desc *desc = &gpio_desc[i];
176
177                 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
178                         ret = -EBUSY;
179                         goto err;
180                 }
181
182                 set_bit(FLAG_RESERVED, &desc->flags);
183         }
184
185         pr_debug("%s: reserved gpios from %d to %d\n",
186                  __func__, start, start + ngpio - 1);
187 err:
188         spin_unlock_irqrestore(&gpio_lock, flags);
189
190         return ret;
191 }
192
193 #ifdef CONFIG_GPIO_SYSFS
194
195 /* lock protects against unexport_gpio() being called while
196  * sysfs files are active.
197  */
198 static DEFINE_MUTEX(sysfs_lock);
199
200 /*
201  * /sys/class/gpio/gpioN... only for GPIOs that are exported
202  *   /direction
203  *      * MAY BE OMITTED if kernel won't allow direction changes
204  *      * is read/write as "in" or "out"
205  *      * may also be written as "high" or "low", initializing
206  *        output value as specified ("out" implies "low")
207  *   /value
208  *      * always readable, subject to hardware behavior
209  *      * may be writable, as zero/nonzero
210  *   /edge
211  *      * configures behavior of poll(2) on /value
212  *      * available only if pin can generate IRQs on input
213  *      * is read/write as "none", "falling", "rising", or "both"
214  *   /active_low
215  *      * configures polarity of /value
216  *      * is read/write as zero/nonzero
217  *      * also affects existing and subsequent "falling" and "rising"
218  *        /edge configuration
219  */
220
221 static ssize_t gpio_direction_show(struct device *dev,
222                 struct device_attribute *attr, char *buf)
223 {
224         const struct gpio_desc  *desc = dev_get_drvdata(dev);
225         ssize_t                 status;
226
227         mutex_lock(&sysfs_lock);
228
229         if (!test_bit(FLAG_EXPORT, &desc->flags))
230                 status = -EIO;
231         else
232                 status = sprintf(buf, "%s\n",
233                         test_bit(FLAG_IS_OUT, &desc->flags)
234                                 ? "out" : "in");
235
236         mutex_unlock(&sysfs_lock);
237         return status;
238 }
239
240 static ssize_t gpio_direction_store(struct device *dev,
241                 struct device_attribute *attr, const char *buf, size_t size)
242 {
243         const struct gpio_desc  *desc = dev_get_drvdata(dev);
244         unsigned                gpio = desc - gpio_desc;
245         ssize_t                 status;
246
247         mutex_lock(&sysfs_lock);
248
249         if (!test_bit(FLAG_EXPORT, &desc->flags))
250                 status = -EIO;
251         else if (sysfs_streq(buf, "high"))
252                 status = gpio_direction_output(gpio, 1);
253         else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
254                 status = gpio_direction_output(gpio, 0);
255         else if (sysfs_streq(buf, "in"))
256                 status = gpio_direction_input(gpio);
257         else
258                 status = -EINVAL;
259
260         mutex_unlock(&sysfs_lock);
261         return status ? : size;
262 }
263
264 static /* const */ DEVICE_ATTR(direction, 0644,
265                 gpio_direction_show, gpio_direction_store);
266
267 static ssize_t gpio_value_show(struct device *dev,
268                 struct device_attribute *attr, char *buf)
269 {
270         const struct gpio_desc  *desc = dev_get_drvdata(dev);
271         unsigned                gpio = desc - gpio_desc;
272         ssize_t                 status;
273
274         mutex_lock(&sysfs_lock);
275
276         if (!test_bit(FLAG_EXPORT, &desc->flags)) {
277                 status = -EIO;
278         } else {
279                 int value;
280
281                 value = !!gpio_get_value_cansleep(gpio);
282                 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
283                         value = !value;
284
285                 status = sprintf(buf, "%d\n", value);
286         }
287
288         mutex_unlock(&sysfs_lock);
289         return status;
290 }
291
292 static ssize_t gpio_value_store(struct device *dev,
293                 struct device_attribute *attr, const char *buf, size_t size)
294 {
295         const struct gpio_desc  *desc = dev_get_drvdata(dev);
296         unsigned                gpio = desc - gpio_desc;
297         ssize_t                 status;
298
299         mutex_lock(&sysfs_lock);
300
301         if (!test_bit(FLAG_EXPORT, &desc->flags))
302                 status = -EIO;
303         else if (!test_bit(FLAG_IS_OUT, &desc->flags))
304                 status = -EPERM;
305         else {
306                 long            value;
307
308                 status = strict_strtol(buf, 0, &value);
309                 if (status == 0) {
310                         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
311                                 value = !value;
312                         gpio_set_value_cansleep(gpio, value != 0);
313                         status = size;
314                 }
315         }
316
317         mutex_unlock(&sysfs_lock);
318         return status;
319 }
320
321 static const DEVICE_ATTR(value, 0644,
322                 gpio_value_show, gpio_value_store);
323
324 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
325 {
326         struct work_struct      *work = priv;
327
328         schedule_work(work);
329         return IRQ_HANDLED;
330 }
331
332 static void gpio_notify_sysfs(struct work_struct *work)
333 {
334         struct poll_desc        *pdesc;
335
336         pdesc = container_of(work, struct poll_desc, work);
337         sysfs_notify_dirent(pdesc->value_sd);
338 }
339
340 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
341                 unsigned long gpio_flags)
342 {
343         struct poll_desc        *pdesc;
344         unsigned long           irq_flags;
345         int                     ret, irq, id;
346
347         if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
348                 return 0;
349
350         irq = gpio_to_irq(desc - gpio_desc);
351         if (irq < 0)
352                 return -EIO;
353
354         id = desc->flags >> PDESC_ID_SHIFT;
355         pdesc = idr_find(&pdesc_idr, id);
356         if (pdesc) {
357                 free_irq(irq, &pdesc->work);
358                 cancel_work_sync(&pdesc->work);
359         }
360
361         desc->flags &= ~GPIO_TRIGGER_MASK;
362
363         if (!gpio_flags) {
364                 ret = 0;
365                 goto free_sd;
366         }
367
368         irq_flags = IRQF_SHARED;
369         if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
370                 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
371                         IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
372         if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
373                 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
374                         IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
375
376         if (!pdesc) {
377                 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
378                 if (!pdesc) {
379                         ret = -ENOMEM;
380                         goto err_out;
381                 }
382
383                 do {
384                         ret = -ENOMEM;
385                         if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
386                                 ret = idr_get_new_above(&pdesc_idr,
387                                                 pdesc, 1, &id);
388                 } while (ret == -EAGAIN);
389
390                 if (ret)
391                         goto free_mem;
392
393                 desc->flags &= GPIO_FLAGS_MASK;
394                 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
395
396                 if (desc->flags >> PDESC_ID_SHIFT != id) {
397                         ret = -ERANGE;
398                         goto free_id;
399                 }
400
401                 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
402                 if (!pdesc->value_sd) {
403                         ret = -ENODEV;
404                         goto free_id;
405                 }
406                 INIT_WORK(&pdesc->work, gpio_notify_sysfs);
407         }
408
409         ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
410                         "gpiolib", &pdesc->work);
411         if (ret)
412                 goto free_sd;
413
414         desc->flags |= gpio_flags;
415         return 0;
416
417 free_sd:
418         sysfs_put(pdesc->value_sd);
419 free_id:
420         idr_remove(&pdesc_idr, id);
421         desc->flags &= GPIO_FLAGS_MASK;
422 free_mem:
423         kfree(pdesc);
424 err_out:
425         return ret;
426 }
427
428 static const struct {
429         const char *name;
430         unsigned long flags;
431 } trigger_types[] = {
432         { "none",    0 },
433         { "falling", BIT(FLAG_TRIG_FALL) },
434         { "rising",  BIT(FLAG_TRIG_RISE) },
435         { "both",    BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
436 };
437
438 static ssize_t gpio_edge_show(struct device *dev,
439                 struct device_attribute *attr, char *buf)
440 {
441         const struct gpio_desc  *desc = dev_get_drvdata(dev);
442         ssize_t                 status;
443
444         mutex_lock(&sysfs_lock);
445
446         if (!test_bit(FLAG_EXPORT, &desc->flags))
447                 status = -EIO;
448         else {
449                 int i;
450
451                 status = 0;
452                 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
453                         if ((desc->flags & GPIO_TRIGGER_MASK)
454                                         == trigger_types[i].flags) {
455                                 status = sprintf(buf, "%s\n",
456                                                  trigger_types[i].name);
457                                 break;
458                         }
459         }
460
461         mutex_unlock(&sysfs_lock);
462         return status;
463 }
464
465 static ssize_t gpio_edge_store(struct device *dev,
466                 struct device_attribute *attr, const char *buf, size_t size)
467 {
468         struct gpio_desc        *desc = dev_get_drvdata(dev);
469         ssize_t                 status;
470         int                     i;
471
472         for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
473                 if (sysfs_streq(trigger_types[i].name, buf))
474                         goto found;
475         return -EINVAL;
476
477 found:
478         mutex_lock(&sysfs_lock);
479
480         if (!test_bit(FLAG_EXPORT, &desc->flags))
481                 status = -EIO;
482         else {
483                 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
484                 if (!status)
485                         status = size;
486         }
487
488         mutex_unlock(&sysfs_lock);
489
490         return status;
491 }
492
493 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
494
495 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
496                                 int value)
497 {
498         int                     status = 0;
499
500         if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
501                 return 0;
502
503         if (value)
504                 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
505         else
506                 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
507
508         /* reconfigure poll(2) support if enabled on one edge only */
509         if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
510                                 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
511                 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
512
513                 gpio_setup_irq(desc, dev, 0);
514                 status = gpio_setup_irq(desc, dev, trigger_flags);
515         }
516
517         return status;
518 }
519
520 static ssize_t gpio_active_low_show(struct device *dev,
521                 struct device_attribute *attr, char *buf)
522 {
523         const struct gpio_desc  *desc = dev_get_drvdata(dev);
524         ssize_t                 status;
525
526         mutex_lock(&sysfs_lock);
527
528         if (!test_bit(FLAG_EXPORT, &desc->flags))
529                 status = -EIO;
530         else
531                 status = sprintf(buf, "%d\n",
532                                 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
533
534         mutex_unlock(&sysfs_lock);
535
536         return status;
537 }
538
539 static ssize_t gpio_active_low_store(struct device *dev,
540                 struct device_attribute *attr, const char *buf, size_t size)
541 {
542         struct gpio_desc        *desc = dev_get_drvdata(dev);
543         ssize_t                 status;
544
545         mutex_lock(&sysfs_lock);
546
547         if (!test_bit(FLAG_EXPORT, &desc->flags)) {
548                 status = -EIO;
549         } else {
550                 long            value;
551
552                 status = strict_strtol(buf, 0, &value);
553                 if (status == 0)
554                         status = sysfs_set_active_low(desc, dev, value != 0);
555         }
556
557         mutex_unlock(&sysfs_lock);
558
559         return status ? : size;
560 }
561
562 static const DEVICE_ATTR(active_low, 0644,
563                 gpio_active_low_show, gpio_active_low_store);
564
565 static const struct attribute *gpio_attrs[] = {
566         &dev_attr_value.attr,
567         &dev_attr_active_low.attr,
568         NULL,
569 };
570
571 static const struct attribute_group gpio_attr_group = {
572         .attrs = (struct attribute **) gpio_attrs,
573 };
574
575 /*
576  * /sys/class/gpio/gpiochipN/
577  *   /base ... matching gpio_chip.base (N)
578  *   /label ... matching gpio_chip.label
579  *   /ngpio ... matching gpio_chip.ngpio
580  */
581
582 static ssize_t chip_base_show(struct device *dev,
583                                struct device_attribute *attr, char *buf)
584 {
585         const struct gpio_chip  *chip = dev_get_drvdata(dev);
586
587         return sprintf(buf, "%d\n", chip->base);
588 }
589 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
590
591 static ssize_t chip_label_show(struct device *dev,
592                                struct device_attribute *attr, char *buf)
593 {
594         const struct gpio_chip  *chip = dev_get_drvdata(dev);
595
596         return sprintf(buf, "%s\n", chip->label ? : "");
597 }
598 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
599
600 static ssize_t chip_ngpio_show(struct device *dev,
601                                struct device_attribute *attr, char *buf)
602 {
603         const struct gpio_chip  *chip = dev_get_drvdata(dev);
604
605         return sprintf(buf, "%u\n", chip->ngpio);
606 }
607 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
608
609 static const struct attribute *gpiochip_attrs[] = {
610         &dev_attr_base.attr,
611         &dev_attr_label.attr,
612         &dev_attr_ngpio.attr,
613         NULL,
614 };
615
616 static const struct attribute_group gpiochip_attr_group = {
617         .attrs = (struct attribute **) gpiochip_attrs,
618 };
619
620 /*
621  * /sys/class/gpio/export ... write-only
622  *      integer N ... number of GPIO to export (full access)
623  * /sys/class/gpio/unexport ... write-only
624  *      integer N ... number of GPIO to unexport
625  */
626 static ssize_t export_store(struct class *class, const char *buf, size_t len)
627 {
628         long    gpio;
629         int     status;
630
631         status = strict_strtol(buf, 0, &gpio);
632         if (status < 0)
633                 goto done;
634
635         /* No extra locking here; FLAG_SYSFS just signifies that the
636          * request and export were done by on behalf of userspace, so
637          * they may be undone on its behalf too.
638          */
639
640         status = gpio_request(gpio, "sysfs");
641         if (status < 0)
642                 goto done;
643
644         status = gpio_export(gpio, true);
645         if (status < 0)
646                 gpio_free(gpio);
647         else
648                 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
649
650 done:
651         if (status)
652                 pr_debug("%s: status %d\n", __func__, status);
653         return status ? : len;
654 }
655
656 static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
657 {
658         long    gpio;
659         int     status;
660
661         status = strict_strtol(buf, 0, &gpio);
662         if (status < 0)
663                 goto done;
664
665         status = -EINVAL;
666
667         /* reject bogus commands (gpio_unexport ignores them) */
668         if (!gpio_is_valid(gpio))
669                 goto done;
670
671         /* No extra locking here; FLAG_SYSFS just signifies that the
672          * request and export were done by on behalf of userspace, so
673          * they may be undone on its behalf too.
674          */
675         if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
676                 status = 0;
677                 gpio_free(gpio);
678         }
679 done:
680         if (status)
681                 pr_debug("%s: status %d\n", __func__, status);
682         return status ? : len;
683 }
684
685 static struct class_attribute gpio_class_attrs[] = {
686         __ATTR(export, 0200, NULL, export_store),
687         __ATTR(unexport, 0200, NULL, unexport_store),
688         __ATTR_NULL,
689 };
690
691 static struct class gpio_class = {
692         .name =         "gpio",
693         .owner =        THIS_MODULE,
694
695         .class_attrs =  gpio_class_attrs,
696 };
697
698
699 /**
700  * gpio_export - export a GPIO through sysfs
701  * @gpio: gpio to make available, already requested
702  * @direction_may_change: true if userspace may change gpio direction
703  * Context: arch_initcall or later
704  *
705  * When drivers want to make a GPIO accessible to userspace after they
706  * have requested it -- perhaps while debugging, or as part of their
707  * public interface -- they may use this routine.  If the GPIO can
708  * change direction (some can't) and the caller allows it, userspace
709  * will see "direction" sysfs attribute which may be used to change
710  * the gpio's direction.  A "value" attribute will always be provided.
711  *
712  * Returns zero on success, else an error.
713  */
714 int gpio_export(unsigned gpio, bool direction_may_change)
715 {
716         unsigned long           flags;
717         struct gpio_desc        *desc;
718         int                     status = -EINVAL;
719         char                    *ioname = NULL;
720
721         /* can't export until sysfs is available ... */
722         if (!gpio_class.p) {
723                 pr_debug("%s: called too early!\n", __func__);
724                 return -ENOENT;
725         }
726
727         if (!gpio_is_valid(gpio))
728                 goto done;
729
730         mutex_lock(&sysfs_lock);
731
732         spin_lock_irqsave(&gpio_lock, flags);
733         desc = &gpio_desc[gpio];
734         if (test_bit(FLAG_REQUESTED, &desc->flags)
735                         && !test_bit(FLAG_EXPORT, &desc->flags)) {
736                 status = 0;
737                 if (!desc->chip->direction_input
738                                 || !desc->chip->direction_output)
739                         direction_may_change = false;
740         }
741         spin_unlock_irqrestore(&gpio_lock, flags);
742
743         if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
744                 ioname = desc->chip->names[gpio - desc->chip->base];
745
746         if (status == 0) {
747                 struct device   *dev;
748
749                 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
750                                 desc, ioname ? ioname : "gpio%d", gpio);
751                 if (!IS_ERR(dev)) {
752                         status = sysfs_create_group(&dev->kobj,
753                                                 &gpio_attr_group);
754
755                         if (!status && direction_may_change)
756                                 status = device_create_file(dev,
757                                                 &dev_attr_direction);
758
759                         if (!status && gpio_to_irq(gpio) >= 0
760                                         && (direction_may_change
761                                                 || !test_bit(FLAG_IS_OUT,
762                                                         &desc->flags)))
763                                 status = device_create_file(dev,
764                                                 &dev_attr_edge);
765
766                         if (status != 0)
767                                 device_unregister(dev);
768                 } else
769                         status = PTR_ERR(dev);
770                 if (status == 0)
771                         set_bit(FLAG_EXPORT, &desc->flags);
772         }
773
774         mutex_unlock(&sysfs_lock);
775
776 done:
777         if (status)
778                 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
779
780         return status;
781 }
782 EXPORT_SYMBOL_GPL(gpio_export);
783
784 static int match_export(struct device *dev, void *data)
785 {
786         return dev_get_drvdata(dev) == data;
787 }
788
789 /**
790  * gpio_export_link - create a sysfs link to an exported GPIO node
791  * @dev: device under which to create symlink
792  * @name: name of the symlink
793  * @gpio: gpio to create symlink to, already exported
794  *
795  * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
796  * node. Caller is responsible for unlinking.
797  *
798  * Returns zero on success, else an error.
799  */
800 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
801 {
802         struct gpio_desc        *desc;
803         int                     status = -EINVAL;
804
805         if (!gpio_is_valid(gpio))
806                 goto done;
807
808         mutex_lock(&sysfs_lock);
809
810         desc = &gpio_desc[gpio];
811
812         if (test_bit(FLAG_EXPORT, &desc->flags)) {
813                 struct device *tdev;
814
815                 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
816                 if (tdev != NULL) {
817                         status = sysfs_create_link(&dev->kobj, &tdev->kobj,
818                                                 name);
819                 } else {
820                         status = -ENODEV;
821                 }
822         }
823
824         mutex_unlock(&sysfs_lock);
825
826 done:
827         if (status)
828                 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
829
830         return status;
831 }
832 EXPORT_SYMBOL_GPL(gpio_export_link);
833
834
835 /**
836  * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
837  * @gpio: gpio to change
838  * @value: non-zero to use active low, i.e. inverted values
839  *
840  * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
841  * The GPIO does not have to be exported yet.  If poll(2) support has
842  * been enabled for either rising or falling edge, it will be
843  * reconfigured to follow the new polarity.
844  *
845  * Returns zero on success, else an error.
846  */
847 int gpio_sysfs_set_active_low(unsigned gpio, int value)
848 {
849         struct gpio_desc        *desc;
850         struct device           *dev = NULL;
851         int                     status = -EINVAL;
852
853         if (!gpio_is_valid(gpio))
854                 goto done;
855
856         mutex_lock(&sysfs_lock);
857
858         desc = &gpio_desc[gpio];
859
860         if (test_bit(FLAG_EXPORT, &desc->flags)) {
861                 dev = class_find_device(&gpio_class, NULL, desc, match_export);
862                 if (dev == NULL) {
863                         status = -ENODEV;
864                         goto unlock;
865                 }
866         }
867
868         status = sysfs_set_active_low(desc, dev, value);
869
870 unlock:
871         mutex_unlock(&sysfs_lock);
872
873 done:
874         if (status)
875                 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
876
877         return status;
878 }
879 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
880
881 /**
882  * gpio_unexport - reverse effect of gpio_export()
883  * @gpio: gpio to make unavailable
884  *
885  * This is implicit on gpio_free().
886  */
887 void gpio_unexport(unsigned gpio)
888 {
889         struct gpio_desc        *desc;
890         int                     status = -EINVAL;
891
892         if (!gpio_is_valid(gpio))
893                 goto done;
894
895         mutex_lock(&sysfs_lock);
896
897         desc = &gpio_desc[gpio];
898
899         if (test_bit(FLAG_EXPORT, &desc->flags)) {
900                 struct device   *dev = NULL;
901
902                 dev = class_find_device(&gpio_class, NULL, desc, match_export);
903                 if (dev) {
904                         gpio_setup_irq(desc, dev, 0);
905                         clear_bit(FLAG_EXPORT, &desc->flags);
906                         put_device(dev);
907                         device_unregister(dev);
908                         status = 0;
909                 } else
910                         status = -ENODEV;
911         }
912
913         mutex_unlock(&sysfs_lock);
914 done:
915         if (status)
916                 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
917 }
918 EXPORT_SYMBOL_GPL(gpio_unexport);
919
920 static int gpiochip_export(struct gpio_chip *chip)
921 {
922         int             status;
923         struct device   *dev;
924
925         /* Many systems register gpio chips for SOC support very early,
926          * before driver model support is available.  In those cases we
927          * export this later, in gpiolib_sysfs_init() ... here we just
928          * verify that _some_ field of gpio_class got initialized.
929          */
930         if (!gpio_class.p)
931                 return 0;
932
933         /* use chip->base for the ID; it's already known to be unique */
934         mutex_lock(&sysfs_lock);
935         dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
936                                 "gpiochip%d", chip->base);
937         if (!IS_ERR(dev)) {
938                 status = sysfs_create_group(&dev->kobj,
939                                 &gpiochip_attr_group);
940         } else
941                 status = PTR_ERR(dev);
942         chip->exported = (status == 0);
943         mutex_unlock(&sysfs_lock);
944
945         if (status) {
946                 unsigned long   flags;
947                 unsigned        gpio;
948
949                 spin_lock_irqsave(&gpio_lock, flags);
950                 gpio = chip->base;
951                 while (gpio_desc[gpio].chip == chip)
952                         gpio_desc[gpio++].chip = NULL;
953                 spin_unlock_irqrestore(&gpio_lock, flags);
954
955                 pr_debug("%s: chip %s status %d\n", __func__,
956                                 chip->label, status);
957         }
958
959         return status;
960 }
961
962 static void gpiochip_unexport(struct gpio_chip *chip)
963 {
964         int                     status;
965         struct device           *dev;
966
967         mutex_lock(&sysfs_lock);
968         dev = class_find_device(&gpio_class, NULL, chip, match_export);
969         if (dev) {
970                 put_device(dev);
971                 device_unregister(dev);
972                 chip->exported = 0;
973                 status = 0;
974         } else
975                 status = -ENODEV;
976         mutex_unlock(&sysfs_lock);
977
978         if (status)
979                 pr_debug("%s: chip %s status %d\n", __func__,
980                                 chip->label, status);
981 }
982
983 static int __init gpiolib_sysfs_init(void)
984 {
985         int             status;
986         unsigned long   flags;
987         unsigned        gpio;
988
989         idr_init(&pdesc_idr);
990
991         status = class_register(&gpio_class);
992         if (status < 0)
993                 return status;
994
995         /* Scan and register the gpio_chips which registered very
996          * early (e.g. before the class_register above was called).
997          *
998          * We run before arch_initcall() so chip->dev nodes can have
999          * registered, and so arch_initcall() can always gpio_export().
1000          */
1001         spin_lock_irqsave(&gpio_lock, flags);
1002         for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
1003                 struct gpio_chip        *chip;
1004
1005                 chip = gpio_desc[gpio].chip;
1006                 if (!chip || chip->exported)
1007                         continue;
1008
1009                 spin_unlock_irqrestore(&gpio_lock, flags);
1010                 status = gpiochip_export(chip);
1011                 spin_lock_irqsave(&gpio_lock, flags);
1012         }
1013         spin_unlock_irqrestore(&gpio_lock, flags);
1014
1015
1016         return status;
1017 }
1018 postcore_initcall(gpiolib_sysfs_init);
1019
1020 #else
1021 static inline int gpiochip_export(struct gpio_chip *chip)
1022 {
1023         return 0;
1024 }
1025
1026 static inline void gpiochip_unexport(struct gpio_chip *chip)
1027 {
1028 }
1029
1030 #endif /* CONFIG_GPIO_SYSFS */
1031
1032 /**
1033  * gpiochip_add() - register a gpio_chip
1034  * @chip: the chip to register, with chip->base initialized
1035  * Context: potentially before irqs or kmalloc will work
1036  *
1037  * Returns a negative errno if the chip can't be registered, such as
1038  * because the chip->base is invalid or already associated with a
1039  * different chip.  Otherwise it returns zero as a success code.
1040  *
1041  * When gpiochip_add() is called very early during boot, so that GPIOs
1042  * can be freely used, the chip->dev device must be registered before
1043  * the gpio framework's arch_initcall().  Otherwise sysfs initialization
1044  * for GPIOs will fail rudely.
1045  *
1046  * If chip->base is negative, this requests dynamic assignment of
1047  * a range of valid GPIOs.
1048  */
1049 int gpiochip_add(struct gpio_chip *chip)
1050 {
1051         unsigned long   flags;
1052         int             status = 0;
1053         unsigned        id;
1054         int             base = chip->base;
1055
1056         if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1057                         && base >= 0) {
1058                 status = -EINVAL;
1059                 goto fail;
1060         }
1061
1062         spin_lock_irqsave(&gpio_lock, flags);
1063
1064         if (base < 0) {
1065                 base = gpiochip_find_base(chip->ngpio);
1066                 if (base < 0) {
1067                         status = base;
1068                         goto unlock;
1069                 }
1070                 chip->base = base;
1071         }
1072
1073         /* these GPIO numbers must not be managed by another gpio_chip */
1074         for (id = base; id < base + chip->ngpio; id++) {
1075                 if (gpio_desc[id].chip != NULL) {
1076                         status = -EBUSY;
1077                         break;
1078                 }
1079         }
1080         if (status == 0) {
1081                 for (id = base; id < base + chip->ngpio; id++) {
1082                         gpio_desc[id].chip = chip;
1083
1084                         /* REVISIT:  most hardware initializes GPIOs as
1085                          * inputs (often with pullups enabled) so power
1086                          * usage is minimized.  Linux code should set the
1087                          * gpio direction first thing; but until it does,
1088                          * we may expose the wrong direction in sysfs.
1089                          */
1090                         gpio_desc[id].flags = !chip->direction_input
1091                                 ? (1 << FLAG_IS_OUT)
1092                                 : 0;
1093                 }
1094         }
1095
1096 unlock:
1097         spin_unlock_irqrestore(&gpio_lock, flags);
1098         if (status == 0)
1099                 status = gpiochip_export(chip);
1100 fail:
1101         /* failures here can mean systems won't boot... */
1102         if (status)
1103                 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
1104                         chip->base, chip->base + chip->ngpio - 1,
1105                         chip->label ? : "generic");
1106         return status;
1107 }
1108 EXPORT_SYMBOL_GPL(gpiochip_add);
1109
1110 /**
1111  * gpiochip_remove() - unregister a gpio_chip
1112  * @chip: the chip to unregister
1113  *
1114  * A gpio_chip with any GPIOs still requested may not be removed.
1115  */
1116 int gpiochip_remove(struct gpio_chip *chip)
1117 {
1118         unsigned long   flags;
1119         int             status = 0;
1120         unsigned        id;
1121
1122         spin_lock_irqsave(&gpio_lock, flags);
1123
1124         for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1125                 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1126                         status = -EBUSY;
1127                         break;
1128                 }
1129         }
1130         if (status == 0) {
1131                 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1132                         gpio_desc[id].chip = NULL;
1133         }
1134
1135         spin_unlock_irqrestore(&gpio_lock, flags);
1136
1137         if (status == 0)
1138                 gpiochip_unexport(chip);
1139
1140         return status;
1141 }
1142 EXPORT_SYMBOL_GPL(gpiochip_remove);
1143
1144
1145 /* These "optional" allocation calls help prevent drivers from stomping
1146  * on each other, and help provide better diagnostics in debugfs.
1147  * They're called even less than the "set direction" calls.
1148  */
1149 int gpio_request(unsigned gpio, const char *label)
1150 {
1151         struct gpio_desc        *desc;
1152         struct gpio_chip        *chip;
1153         int                     status = -EINVAL;
1154         unsigned long           flags;
1155
1156         spin_lock_irqsave(&gpio_lock, flags);
1157
1158         if (!gpio_is_valid(gpio))
1159                 goto done;
1160         desc = &gpio_desc[gpio];
1161         chip = desc->chip;
1162         if (chip == NULL)
1163                 goto done;
1164
1165         if (!try_module_get(chip->owner))
1166                 goto done;
1167
1168         /* NOTE:  gpio_request() can be called in early boot,
1169          * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1170          */
1171
1172         if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1173                 desc_set_label(desc, label ? : "?");
1174                 status = 0;
1175         } else {
1176                 status = -EBUSY;
1177                 module_put(chip->owner);
1178                 goto done;
1179         }
1180
1181         if (chip->request) {
1182                 /* chip->request may sleep */
1183                 spin_unlock_irqrestore(&gpio_lock, flags);
1184                 status = chip->request(chip, gpio - chip->base);
1185                 spin_lock_irqsave(&gpio_lock, flags);
1186
1187                 if (status < 0) {
1188                         desc_set_label(desc, NULL);
1189                         module_put(chip->owner);
1190                         clear_bit(FLAG_REQUESTED, &desc->flags);
1191                 }
1192         }
1193
1194 done:
1195         if (status)
1196                 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1197                         gpio, label ? : "?", status);
1198         spin_unlock_irqrestore(&gpio_lock, flags);
1199         return status;
1200 }
1201 EXPORT_SYMBOL_GPL(gpio_request);
1202
1203 void gpio_free(unsigned gpio)
1204 {
1205         unsigned long           flags;
1206         struct gpio_desc        *desc;
1207         struct gpio_chip        *chip;
1208
1209         might_sleep();
1210
1211         if (!gpio_is_valid(gpio)) {
1212                 WARN_ON(extra_checks);
1213                 return;
1214         }
1215
1216         gpio_unexport(gpio);
1217
1218         spin_lock_irqsave(&gpio_lock, flags);
1219
1220         desc = &gpio_desc[gpio];
1221         chip = desc->chip;
1222         if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1223                 if (chip->free) {
1224                         spin_unlock_irqrestore(&gpio_lock, flags);
1225                         might_sleep_if(extra_checks && chip->can_sleep);
1226                         chip->free(chip, gpio - chip->base);
1227                         spin_lock_irqsave(&gpio_lock, flags);
1228                 }
1229                 desc_set_label(desc, NULL);
1230                 module_put(desc->chip->owner);
1231                 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1232                 clear_bit(FLAG_REQUESTED, &desc->flags);
1233         } else
1234                 WARN_ON(extra_checks);
1235
1236         spin_unlock_irqrestore(&gpio_lock, flags);
1237 }
1238 EXPORT_SYMBOL_GPL(gpio_free);
1239
1240
1241 /**
1242  * gpiochip_is_requested - return string iff signal was requested
1243  * @chip: controller managing the signal
1244  * @offset: of signal within controller's 0..(ngpio - 1) range
1245  *
1246  * Returns NULL if the GPIO is not currently requested, else a string.
1247  * If debugfs support is enabled, the string returned is the label passed
1248  * to gpio_request(); otherwise it is a meaningless constant.
1249  *
1250  * This function is for use by GPIO controller drivers.  The label can
1251  * help with diagnostics, and knowing that the signal is used as a GPIO
1252  * can help avoid accidentally multiplexing it to another controller.
1253  */
1254 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1255 {
1256         unsigned gpio = chip->base + offset;
1257
1258         if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1259                 return NULL;
1260         if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1261                 return NULL;
1262 #ifdef CONFIG_DEBUG_FS
1263         return gpio_desc[gpio].label;
1264 #else
1265         return "?";
1266 #endif
1267 }
1268 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1269
1270
1271 /* Drivers MUST set GPIO direction before making get/set calls.  In
1272  * some cases this is done in early boot, before IRQs are enabled.
1273  *
1274  * As a rule these aren't called more than once (except for drivers
1275  * using the open-drain emulation idiom) so these are natural places
1276  * to accumulate extra debugging checks.  Note that we can't (yet)
1277  * rely on gpio_request() having been called beforehand.
1278  */
1279
1280 int gpio_direction_input(unsigned gpio)
1281 {
1282         unsigned long           flags;
1283         struct gpio_chip        *chip;
1284         struct gpio_desc        *desc = &gpio_desc[gpio];
1285         int                     status = -EINVAL;
1286
1287         spin_lock_irqsave(&gpio_lock, flags);
1288
1289         if (!gpio_is_valid(gpio))
1290                 goto fail;
1291         chip = desc->chip;
1292         if (!chip || !chip->get || !chip->direction_input)
1293                 goto fail;
1294         gpio -= chip->base;
1295         if (gpio >= chip->ngpio)
1296                 goto fail;
1297         status = gpio_ensure_requested(desc, gpio);
1298         if (status < 0)
1299                 goto fail;
1300
1301         /* now we know the gpio is valid and chip won't vanish */
1302
1303         spin_unlock_irqrestore(&gpio_lock, flags);
1304
1305         might_sleep_if(extra_checks && chip->can_sleep);
1306
1307         if (status) {
1308                 status = chip->request(chip, gpio);
1309                 if (status < 0) {
1310                         pr_debug("GPIO-%d: chip request fail, %d\n",
1311                                 chip->base + gpio, status);
1312                         /* and it's not available to anyone else ...
1313                          * gpio_request() is the fully clean solution.
1314                          */
1315                         goto lose;
1316                 }
1317         }
1318
1319         status = chip->direction_input(chip, gpio);
1320         if (status == 0)
1321                 clear_bit(FLAG_IS_OUT, &desc->flags);
1322 lose:
1323         return status;
1324 fail:
1325         spin_unlock_irqrestore(&gpio_lock, flags);
1326         if (status)
1327                 pr_debug("%s: gpio-%d status %d\n",
1328                         __func__, gpio, status);
1329         return status;
1330 }
1331 EXPORT_SYMBOL_GPL(gpio_direction_input);
1332
1333 int gpio_direction_output(unsigned gpio, int value)
1334 {
1335         unsigned long           flags;
1336         struct gpio_chip        *chip;
1337         struct gpio_desc        *desc = &gpio_desc[gpio];
1338         int                     status = -EINVAL;
1339
1340         spin_lock_irqsave(&gpio_lock, flags);
1341
1342         if (!gpio_is_valid(gpio))
1343                 goto fail;
1344         chip = desc->chip;
1345         if (!chip || !chip->set || !chip->direction_output)
1346                 goto fail;
1347         gpio -= chip->base;
1348         if (gpio >= chip->ngpio)
1349                 goto fail;
1350         status = gpio_ensure_requested(desc, gpio);
1351         if (status < 0)
1352                 goto fail;
1353
1354         /* now we know the gpio is valid and chip won't vanish */
1355
1356         spin_unlock_irqrestore(&gpio_lock, flags);
1357
1358         might_sleep_if(extra_checks && chip->can_sleep);
1359
1360         if (status) {
1361                 status = chip->request(chip, gpio);
1362                 if (status < 0) {
1363                         pr_debug("GPIO-%d: chip request fail, %d\n",
1364                                 chip->base + gpio, status);
1365                         /* and it's not available to anyone else ...
1366                          * gpio_request() is the fully clean solution.
1367                          */
1368                         goto lose;
1369                 }
1370         }
1371
1372         status = chip->direction_output(chip, gpio, value);
1373         if (status == 0)
1374                 set_bit(FLAG_IS_OUT, &desc->flags);
1375 lose:
1376         return status;
1377 fail:
1378         spin_unlock_irqrestore(&gpio_lock, flags);
1379         if (status)
1380                 pr_debug("%s: gpio-%d status %d\n",
1381                         __func__, gpio, status);
1382         return status;
1383 }
1384 EXPORT_SYMBOL_GPL(gpio_direction_output);
1385
1386
1387 /* I/O calls are only valid after configuration completed; the relevant
1388  * "is this a valid GPIO" error checks should already have been done.
1389  *
1390  * "Get" operations are often inlinable as reading a pin value register,
1391  * and masking the relevant bit in that register.
1392  *
1393  * When "set" operations are inlinable, they involve writing that mask to
1394  * one register to set a low value, or a different register to set it high.
1395  * Otherwise locking is needed, so there may be little value to inlining.
1396  *
1397  *------------------------------------------------------------------------
1398  *
1399  * IMPORTANT!!!  The hot paths -- get/set value -- assume that callers
1400  * have requested the GPIO.  That can include implicit requesting by
1401  * a direction setting call.  Marking a gpio as requested locks its chip
1402  * in memory, guaranteeing that these table lookups need no more locking
1403  * and that gpiochip_remove() will fail.
1404  *
1405  * REVISIT when debugging, consider adding some instrumentation to ensure
1406  * that the GPIO was actually requested.
1407  */
1408
1409 /**
1410  * __gpio_get_value() - return a gpio's value
1411  * @gpio: gpio whose value will be returned
1412  * Context: any
1413  *
1414  * This is used directly or indirectly to implement gpio_get_value().
1415  * It returns the zero or nonzero value provided by the associated
1416  * gpio_chip.get() method; or zero if no such method is provided.
1417  */
1418 int __gpio_get_value(unsigned gpio)
1419 {
1420         struct gpio_chip        *chip;
1421
1422         chip = gpio_to_chip(gpio);
1423         WARN_ON(extra_checks && chip->can_sleep);
1424         return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1425 }
1426 EXPORT_SYMBOL_GPL(__gpio_get_value);
1427
1428 /**
1429  * __gpio_set_value() - assign a gpio's value
1430  * @gpio: gpio whose value will be assigned
1431  * @value: value to assign
1432  * Context: any
1433  *
1434  * This is used directly or indirectly to implement gpio_set_value().
1435  * It invokes the associated gpio_chip.set() method.
1436  */
1437 void __gpio_set_value(unsigned gpio, int value)
1438 {
1439         struct gpio_chip        *chip;
1440
1441         chip = gpio_to_chip(gpio);
1442         WARN_ON(extra_checks && chip->can_sleep);
1443         chip->set(chip, gpio - chip->base, value);
1444 }
1445 EXPORT_SYMBOL_GPL(__gpio_set_value);
1446
1447 /**
1448  * __gpio_cansleep() - report whether gpio value access will sleep
1449  * @gpio: gpio in question
1450  * Context: any
1451  *
1452  * This is used directly or indirectly to implement gpio_cansleep().  It
1453  * returns nonzero if access reading or writing the GPIO value can sleep.
1454  */
1455 int __gpio_cansleep(unsigned gpio)
1456 {
1457         struct gpio_chip        *chip;
1458
1459         /* only call this on GPIOs that are valid! */
1460         chip = gpio_to_chip(gpio);
1461
1462         return chip->can_sleep;
1463 }
1464 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1465
1466 /**
1467  * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1468  * @gpio: gpio whose IRQ will be returned (already requested)
1469  * Context: any
1470  *
1471  * This is used directly or indirectly to implement gpio_to_irq().
1472  * It returns the number of the IRQ signaled by this (input) GPIO,
1473  * or a negative errno.
1474  */
1475 int __gpio_to_irq(unsigned gpio)
1476 {
1477         struct gpio_chip        *chip;
1478
1479         chip = gpio_to_chip(gpio);
1480         return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1481 }
1482 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1483
1484
1485
1486 /* There's no value in making it easy to inline GPIO calls that may sleep.
1487  * Common examples include ones connected to I2C or SPI chips.
1488  */
1489
1490 int gpio_get_value_cansleep(unsigned gpio)
1491 {
1492         struct gpio_chip        *chip;
1493
1494         might_sleep_if(extra_checks);
1495         chip = gpio_to_chip(gpio);
1496         return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1497 }
1498 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1499
1500 void gpio_set_value_cansleep(unsigned gpio, int value)
1501 {
1502         struct gpio_chip        *chip;
1503
1504         might_sleep_if(extra_checks);
1505         chip = gpio_to_chip(gpio);
1506         chip->set(chip, gpio - chip->base, value);
1507 }
1508 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1509
1510
1511 #ifdef CONFIG_DEBUG_FS
1512
1513 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1514 {
1515         unsigned                i;
1516         unsigned                gpio = chip->base;
1517         struct gpio_desc        *gdesc = &gpio_desc[gpio];
1518         int                     is_out;
1519
1520         for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1521                 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1522                         continue;
1523
1524                 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1525                 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1526                         gpio, gdesc->label,
1527                         is_out ? "out" : "in ",
1528                         chip->get
1529                                 ? (chip->get(chip, i) ? "hi" : "lo")
1530                                 : "?  ");
1531
1532                 if (!is_out) {
1533                         int             irq = gpio_to_irq(gpio);
1534                         struct irq_desc *desc = irq_to_desc(irq);
1535
1536                         /* This races with request_irq(), set_irq_type(),
1537                          * and set_irq_wake() ... but those are "rare".
1538                          *
1539                          * More significantly, trigger type flags aren't
1540                          * currently maintained by genirq.
1541                          */
1542                         if (irq >= 0 && desc->action) {
1543                                 char *trigger;
1544
1545                                 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1546                                 case IRQ_TYPE_NONE:
1547                                         trigger = "(default)";
1548                                         break;
1549                                 case IRQ_TYPE_EDGE_FALLING:
1550                                         trigger = "edge-falling";
1551                                         break;
1552                                 case IRQ_TYPE_EDGE_RISING:
1553                                         trigger = "edge-rising";
1554                                         break;
1555                                 case IRQ_TYPE_EDGE_BOTH:
1556                                         trigger = "edge-both";
1557                                         break;
1558                                 case IRQ_TYPE_LEVEL_HIGH:
1559                                         trigger = "level-high";
1560                                         break;
1561                                 case IRQ_TYPE_LEVEL_LOW:
1562                                         trigger = "level-low";
1563                                         break;
1564                                 default:
1565                                         trigger = "?trigger?";
1566                                         break;
1567                                 }
1568
1569                                 seq_printf(s, " irq-%d %s%s",
1570                                         irq, trigger,
1571                                         (desc->status & IRQ_WAKEUP)
1572                                                 ? " wakeup" : "");
1573                         }
1574                 }
1575
1576                 seq_printf(s, "\n");
1577         }
1578 }
1579
1580 static int gpiolib_show(struct seq_file *s, void *unused)
1581 {
1582         struct gpio_chip        *chip = NULL;
1583         unsigned                gpio;
1584         int                     started = 0;
1585
1586         /* REVISIT this isn't locked against gpio_chip removal ... */
1587
1588         for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1589                 struct device *dev;
1590
1591                 if (chip == gpio_desc[gpio].chip)
1592                         continue;
1593                 chip = gpio_desc[gpio].chip;
1594                 if (!chip)
1595                         continue;
1596
1597                 seq_printf(s, "%sGPIOs %d-%d",
1598                                 started ? "\n" : "",
1599                                 chip->base, chip->base + chip->ngpio - 1);
1600                 dev = chip->dev;
1601                 if (dev)
1602                         seq_printf(s, ", %s/%s",
1603                                 dev->bus ? dev->bus->name : "no-bus",
1604                                 dev_name(dev));
1605                 if (chip->label)
1606                         seq_printf(s, ", %s", chip->label);
1607                 if (chip->can_sleep)
1608                         seq_printf(s, ", can sleep");
1609                 seq_printf(s, ":\n");
1610
1611                 started = 1;
1612                 if (chip->dbg_show)
1613                         chip->dbg_show(s, chip);
1614                 else
1615                         gpiolib_dbg_show(s, chip);
1616         }
1617         return 0;
1618 }
1619
1620 static int gpiolib_open(struct inode *inode, struct file *file)
1621 {
1622         return single_open(file, gpiolib_show, NULL);
1623 }
1624
1625 static const struct file_operations gpiolib_operations = {
1626         .open           = gpiolib_open,
1627         .read           = seq_read,
1628         .llseek         = seq_lseek,
1629         .release        = single_release,
1630 };
1631
1632 static int __init gpiolib_debugfs_init(void)
1633 {
1634         /* /sys/kernel/debug/gpio */
1635         (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1636                                 NULL, NULL, &gpiolib_operations);
1637         return 0;
1638 }
1639 subsys_initcall(gpiolib_debugfs_init);
1640
1641 #endif  /* DEBUG_FS */