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