Merge tag 'v3.10.69' into update
[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/list.h>
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15
16 #define CREATE_TRACE_POINTS
17 #include <trace/events/gpio.h>
18
19 /* Optional implementation infrastructure for GPIO interfaces.
20 *
21 * Platforms may want to use this if they tend to use very many GPIOs
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
23 *
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
29 */
30
31
32 /* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38 #ifdef DEBUG
39 #define extra_checks 1
40 #else
41 #define extra_checks 0
42 #endif
43
44 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
48 static DEFINE_SPINLOCK(gpio_lock);
49
50 struct gpio_desc {
51 struct gpio_chip *chip;
52 unsigned long flags;
53 /* flag symbols are bit numbers */
54 #define FLAG_REQUESTED 0
55 #define FLAG_IS_OUT 1
56 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
57 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58 #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59 #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60 #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
61 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
63
64 #define ID_SHIFT 16 /* add new flags before this one */
65
66 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
67 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
68
69 #ifdef CONFIG_DEBUG_FS
70 const char *label;
71 #endif
72 };
73 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74
75 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
76
77 static LIST_HEAD(gpio_chips);
78
79 #ifdef CONFIG_GPIO_SYSFS
80 static DEFINE_IDR(dirent_idr);
81 #endif
82
83 /*
84 * Internal gpiod_* API using descriptors instead of the integer namespace.
85 * Most of this should eventually go public.
86 */
87 static int gpiod_request(struct gpio_desc *desc, const char *label);
88 static void gpiod_free(struct gpio_desc *desc);
89 static int gpiod_direction_input(struct gpio_desc *desc);
90 static int gpiod_direction_output(struct gpio_desc *desc, int value);
91 static int gpiod_get_direction(const struct gpio_desc *desc);
92 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
93 static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
94 static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
95 static int gpiod_get_value(const struct gpio_desc *desc);
96 static void gpiod_set_value(struct gpio_desc *desc, int value);
97 static int gpiod_cansleep(const struct gpio_desc *desc);
98 static int gpiod_to_irq(const struct gpio_desc *desc);
99 static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
100 static int gpiod_export_link(struct device *dev, const char *name,
101 struct gpio_desc *desc);
102 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
103 static void gpiod_unexport(struct gpio_desc *desc);
104
105
106 static inline void desc_set_label(struct gpio_desc *d, const char *label)
107 {
108 #ifdef CONFIG_DEBUG_FS
109 d->label = label;
110 #endif
111 }
112
113 /*
114 * Return the GPIO number of the passed descriptor relative to its chip
115 */
116 static int gpio_chip_hwgpio(const struct gpio_desc *desc)
117 {
118 return desc - &desc->chip->desc[0];
119 }
120
121 /**
122 * Convert a GPIO number to its descriptor
123 */
124 static struct gpio_desc *gpio_to_desc(unsigned gpio)
125 {
126 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
127 return NULL;
128 else
129 return &gpio_desc[gpio];
130 }
131
132 /**
133 * Convert a GPIO descriptor to the integer namespace.
134 * This should disappear in the future but is needed since we still
135 * use GPIO numbers for error messages and sysfs nodes
136 */
137 static int desc_to_gpio(const struct gpio_desc *desc)
138 {
139 return desc - &gpio_desc[0];
140 }
141
142
143 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
144 * when setting direction, and otherwise illegal. Until board setup code
145 * and drivers use explicit requests everywhere (which won't happen when
146 * those calls have no teeth) we can't avoid autorequesting. This nag
147 * message should motivate switching to explicit requests... so should
148 * the weaker cleanup after faults, compared to gpio_request().
149 *
150 * NOTE: the autorequest mechanism is going away; at this point it's
151 * only "legal" in the sense that (old) code using it won't break yet,
152 * but instead only triggers a WARN() stack dump.
153 */
154 static int gpio_ensure_requested(struct gpio_desc *desc)
155 {
156 const struct gpio_chip *chip = desc->chip;
157 const int gpio = desc_to_gpio(desc);
158
159 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
160 "autorequest GPIO-%d\n", gpio)) {
161 if (!try_module_get(chip->owner)) {
162 pr_err("GPIO-%d: module can't be gotten \n", gpio);
163 clear_bit(FLAG_REQUESTED, &desc->flags);
164 /* lose */
165 return -EIO;
166 }
167 desc_set_label(desc, "[auto]");
168 /* caller must chip->request() w/o spinlock */
169 if (chip->request)
170 return 1;
171 }
172 return 0;
173 }
174
175 static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
176 {
177 return desc ? desc->chip : NULL;
178 }
179
180 /* caller holds gpio_lock *OR* gpio is marked as requested */
181 struct gpio_chip *gpio_to_chip(unsigned gpio)
182 {
183 return gpiod_to_chip(gpio_to_desc(gpio));
184 }
185
186 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
187 static int gpiochip_find_base(int ngpio)
188 {
189 struct gpio_chip *chip;
190 int base = ARCH_NR_GPIOS - ngpio;
191
192 list_for_each_entry_reverse(chip, &gpio_chips, list) {
193 /* found a free space? */
194 if (chip->base + chip->ngpio <= base)
195 break;
196 else
197 /* nope, check the space right before the chip */
198 base = chip->base - ngpio;
199 }
200
201 if (gpio_is_valid(base)) {
202 pr_debug("%s: found new base at %d\n", __func__, base);
203 return base;
204 } else {
205 pr_err("%s: cannot find free range\n", __func__);
206 return -ENOSPC;
207 }
208 }
209
210 /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
211 static int gpiod_get_direction(const struct gpio_desc *desc)
212 {
213 struct gpio_chip *chip;
214 unsigned offset;
215 int status = -EINVAL;
216
217 chip = gpiod_to_chip(desc);
218 offset = gpio_chip_hwgpio(desc);
219
220 if (!chip->get_direction)
221 return status;
222
223 status = chip->get_direction(chip, offset);
224 if (status > 0) {
225 /* GPIOF_DIR_IN, or other positive */
226 status = 1;
227 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
228 * so it does not affect constness per se */
229 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
230 }
231 if (status == 0) {
232 /* GPIOF_DIR_OUT */
233 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
234 }
235 return status;
236 }
237
238 #ifdef CONFIG_GPIO_SYSFS
239
240 /* lock protects against unexport_gpio() being called while
241 * sysfs files are active.
242 */
243 static DEFINE_MUTEX(sysfs_lock);
244
245 /*
246 * /sys/class/gpio/gpioN... only for GPIOs that are exported
247 * /direction
248 * * MAY BE OMITTED if kernel won't allow direction changes
249 * * is read/write as "in" or "out"
250 * * may also be written as "high" or "low", initializing
251 * output value as specified ("out" implies "low")
252 * /value
253 * * always readable, subject to hardware behavior
254 * * may be writable, as zero/nonzero
255 * /edge
256 * * configures behavior of poll(2) on /value
257 * * available only if pin can generate IRQs on input
258 * * is read/write as "none", "falling", "rising", or "both"
259 * /active_low
260 * * configures polarity of /value
261 * * is read/write as zero/nonzero
262 * * also affects existing and subsequent "falling" and "rising"
263 * /edge configuration
264 */
265
266 static ssize_t gpio_direction_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268 {
269 const struct gpio_desc *desc = dev_get_drvdata(dev);
270 ssize_t status;
271
272 mutex_lock(&sysfs_lock);
273
274 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
275 status = -EIO;
276 } else {
277 gpiod_get_direction(desc);
278 status = sprintf(buf, "%s\n",
279 test_bit(FLAG_IS_OUT, &desc->flags)
280 ? "out" : "in");
281 }
282
283 mutex_unlock(&sysfs_lock);
284 return status;
285 }
286
287 static ssize_t gpio_direction_store(struct device *dev,
288 struct device_attribute *attr, const char *buf, size_t size)
289 {
290 struct gpio_desc *desc = dev_get_drvdata(dev);
291 ssize_t status;
292
293 mutex_lock(&sysfs_lock);
294
295 if (!test_bit(FLAG_EXPORT, &desc->flags))
296 status = -EIO;
297 else if (sysfs_streq(buf, "high"))
298 status = gpiod_direction_output(desc, 1);
299 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
300 status = gpiod_direction_output(desc, 0);
301 else if (sysfs_streq(buf, "in"))
302 status = gpiod_direction_input(desc);
303 else
304 status = -EINVAL;
305
306 mutex_unlock(&sysfs_lock);
307 return status ? : size;
308 }
309
310 static /* const */ DEVICE_ATTR(direction, 0644,
311 gpio_direction_show, gpio_direction_store);
312
313 static ssize_t gpio_value_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
315 {
316 struct gpio_desc *desc = dev_get_drvdata(dev);
317 ssize_t status;
318
319 mutex_lock(&sysfs_lock);
320
321 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
322 status = -EIO;
323 } else {
324 int value;
325
326 value = !!gpiod_get_value_cansleep(desc);
327 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
328 value = !value;
329
330 status = sprintf(buf, "%d\n", value);
331 }
332
333 mutex_unlock(&sysfs_lock);
334 return status;
335 }
336
337 static ssize_t gpio_value_store(struct device *dev,
338 struct device_attribute *attr, const char *buf, size_t size)
339 {
340 struct gpio_desc *desc = dev_get_drvdata(dev);
341 ssize_t status;
342
343 mutex_lock(&sysfs_lock);
344
345 if (!test_bit(FLAG_EXPORT, &desc->flags))
346 status = -EIO;
347 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
348 status = -EPERM;
349 else {
350 long value;
351
352 status = strict_strtol(buf, 0, &value);
353 if (status == 0) {
354 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
355 value = !value;
356 gpiod_set_value_cansleep(desc, value != 0);
357 status = size;
358 }
359 }
360
361 mutex_unlock(&sysfs_lock);
362 return status;
363 }
364
365 static DEVICE_ATTR(value, 0644,
366 gpio_value_show, gpio_value_store);
367
368 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
369 {
370 struct sysfs_dirent *value_sd = priv;
371
372 sysfs_notify_dirent(value_sd);
373 return IRQ_HANDLED;
374 }
375
376 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
377 unsigned long gpio_flags)
378 {
379 struct sysfs_dirent *value_sd;
380 unsigned long irq_flags;
381 int ret, irq, id;
382
383 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
384 return 0;
385
386 irq = gpiod_to_irq(desc);
387 if (irq < 0)
388 return -EIO;
389
390 id = desc->flags >> ID_SHIFT;
391 value_sd = idr_find(&dirent_idr, id);
392 if (value_sd)
393 free_irq(irq, value_sd);
394
395 desc->flags &= ~GPIO_TRIGGER_MASK;
396
397 if (!gpio_flags) {
398 ret = 0;
399 goto free_id;
400 }
401
402 irq_flags = IRQF_SHARED;
403 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
404 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
405 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
406 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
407 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
408 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
409
410 if (!value_sd) {
411 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
412 if (!value_sd) {
413 ret = -ENODEV;
414 goto err_out;
415 }
416
417 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
418 if (ret < 0)
419 goto free_sd;
420 id = ret;
421
422 desc->flags &= GPIO_FLAGS_MASK;
423 desc->flags |= (unsigned long)id << ID_SHIFT;
424
425 if (desc->flags >> ID_SHIFT != id) {
426 ret = -ERANGE;
427 goto free_id;
428 }
429 }
430
431 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
432 "gpiolib", value_sd);
433 if (ret < 0)
434 goto free_id;
435
436 desc->flags |= gpio_flags;
437 return 0;
438
439 free_id:
440 idr_remove(&dirent_idr, id);
441 desc->flags &= GPIO_FLAGS_MASK;
442 free_sd:
443 if (value_sd)
444 sysfs_put(value_sd);
445 err_out:
446 return ret;
447 }
448
449 static const struct {
450 const char *name;
451 unsigned long flags;
452 } trigger_types[] = {
453 { "none", 0 },
454 { "falling", BIT(FLAG_TRIG_FALL) },
455 { "rising", BIT(FLAG_TRIG_RISE) },
456 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
457 };
458
459 static ssize_t gpio_edge_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461 {
462 const struct gpio_desc *desc = dev_get_drvdata(dev);
463 ssize_t status;
464
465 mutex_lock(&sysfs_lock);
466
467 if (!test_bit(FLAG_EXPORT, &desc->flags))
468 status = -EIO;
469 else {
470 int i;
471
472 status = 0;
473 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
474 if ((desc->flags & GPIO_TRIGGER_MASK)
475 == trigger_types[i].flags) {
476 status = sprintf(buf, "%s\n",
477 trigger_types[i].name);
478 break;
479 }
480 }
481
482 mutex_unlock(&sysfs_lock);
483 return status;
484 }
485
486 static ssize_t gpio_edge_store(struct device *dev,
487 struct device_attribute *attr, const char *buf, size_t size)
488 {
489 struct gpio_desc *desc = dev_get_drvdata(dev);
490 ssize_t status;
491 int i;
492
493 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
494 if (sysfs_streq(trigger_types[i].name, buf))
495 goto found;
496 return -EINVAL;
497
498 found:
499 mutex_lock(&sysfs_lock);
500
501 if (!test_bit(FLAG_EXPORT, &desc->flags))
502 status = -EIO;
503 else {
504 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
505 if (!status)
506 status = size;
507 }
508
509 mutex_unlock(&sysfs_lock);
510
511 return status;
512 }
513
514 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
515
516 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
517 int value)
518 {
519 int status = 0;
520
521 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
522 return 0;
523
524 if (value)
525 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
526 else
527 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
528
529 /* reconfigure poll(2) support if enabled on one edge only */
530 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
531 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
532 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
533
534 gpio_setup_irq(desc, dev, 0);
535 status = gpio_setup_irq(desc, dev, trigger_flags);
536 }
537
538 return status;
539 }
540
541 static ssize_t gpio_active_low_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543 {
544 const struct gpio_desc *desc = dev_get_drvdata(dev);
545 ssize_t status;
546
547 mutex_lock(&sysfs_lock);
548
549 if (!test_bit(FLAG_EXPORT, &desc->flags))
550 status = -EIO;
551 else
552 status = sprintf(buf, "%d\n",
553 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
554
555 mutex_unlock(&sysfs_lock);
556
557 return status;
558 }
559
560 static ssize_t gpio_active_low_store(struct device *dev,
561 struct device_attribute *attr, const char *buf, size_t size)
562 {
563 struct gpio_desc *desc = dev_get_drvdata(dev);
564 ssize_t status;
565
566 mutex_lock(&sysfs_lock);
567
568 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
569 status = -EIO;
570 } else {
571 long value;
572
573 status = strict_strtol(buf, 0, &value);
574 if (status == 0)
575 status = sysfs_set_active_low(desc, dev, value != 0);
576 }
577
578 mutex_unlock(&sysfs_lock);
579
580 return status ? : size;
581 }
582
583 static DEVICE_ATTR(active_low, 0644,
584 gpio_active_low_show, gpio_active_low_store);
585
586 static struct attribute *gpio_attrs[] = {
587 &dev_attr_value.attr,
588 &dev_attr_active_low.attr,
589 NULL,
590 };
591
592 static const struct attribute_group gpio_attr_group = {
593 .attrs = gpio_attrs,
594 };
595
596 /*
597 * /sys/class/gpio/gpiochipN/
598 * /base ... matching gpio_chip.base (N)
599 * /label ... matching gpio_chip.label
600 * /ngpio ... matching gpio_chip.ngpio
601 */
602
603 static ssize_t chip_base_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
605 {
606 const struct gpio_chip *chip = dev_get_drvdata(dev);
607
608 return sprintf(buf, "%d\n", chip->base);
609 }
610 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
611
612 static ssize_t chip_label_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614 {
615 const struct gpio_chip *chip = dev_get_drvdata(dev);
616
617 return sprintf(buf, "%s\n", chip->label ? : "");
618 }
619 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
620
621 static ssize_t chip_ngpio_show(struct device *dev,
622 struct device_attribute *attr, char *buf)
623 {
624 const struct gpio_chip *chip = dev_get_drvdata(dev);
625
626 return sprintf(buf, "%u\n", chip->ngpio);
627 }
628 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
629
630 static struct attribute *gpiochip_attrs[] = {
631 &dev_attr_base.attr,
632 &dev_attr_label.attr,
633 &dev_attr_ngpio.attr,
634 NULL,
635 };
636
637 static const struct attribute_group gpiochip_attr_group = {
638 .attrs = gpiochip_attrs,
639 };
640
641 /*
642 * /sys/class/gpio/export ... write-only
643 * integer N ... number of GPIO to export (full access)
644 * /sys/class/gpio/unexport ... write-only
645 * integer N ... number of GPIO to unexport
646 */
647 static ssize_t export_store(struct class *class,
648 struct class_attribute *attr,
649 const char *buf, size_t len)
650 {
651 long gpio;
652 struct gpio_desc *desc;
653 int status;
654
655 status = strict_strtol(buf, 0, &gpio);
656 if (status < 0)
657 goto done;
658
659 desc = gpio_to_desc(gpio);
660 /* reject invalid GPIOs */
661 if (!desc) {
662 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
663 return -EINVAL;
664 }
665
666 /* No extra locking here; FLAG_SYSFS just signifies that the
667 * request and export were done by on behalf of userspace, so
668 * they may be undone on its behalf too.
669 */
670
671 status = gpiod_request(desc, "sysfs");
672 if (status < 0) {
673 if (status == -EPROBE_DEFER)
674 status = -ENODEV;
675 goto done;
676 }
677 status = gpiod_export(desc, true);
678 if (status < 0)
679 gpiod_free(desc);
680 else
681 set_bit(FLAG_SYSFS, &desc->flags);
682
683 done:
684 if (status)
685 pr_debug("%s: status %d\n", __func__, status);
686 return status ? : len;
687 }
688
689 static ssize_t unexport_store(struct class *class,
690 struct class_attribute *attr,
691 const char *buf, size_t len)
692 {
693 long gpio;
694 struct gpio_desc *desc;
695 int status;
696
697 status = strict_strtol(buf, 0, &gpio);
698 if (status < 0)
699 goto done;
700
701 desc = gpio_to_desc(gpio);
702 /* reject bogus commands (gpio_unexport ignores them) */
703 if (!desc) {
704 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
705 return -EINVAL;
706 }
707
708 status = -EINVAL;
709
710 /* No extra locking here; FLAG_SYSFS just signifies that the
711 * request and export were done by on behalf of userspace, so
712 * they may be undone on its behalf too.
713 */
714 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
715 status = 0;
716 gpiod_free(desc);
717 }
718 done:
719 if (status)
720 pr_debug("%s: status %d\n", __func__, status);
721 return status ? : len;
722 }
723
724 static struct class_attribute gpio_class_attrs[] = {
725 __ATTR(export, 0200, NULL, export_store),
726 __ATTR(unexport, 0200, NULL, unexport_store),
727 __ATTR_NULL,
728 };
729
730 static struct class gpio_class = {
731 .name = "gpio",
732 .owner = THIS_MODULE,
733
734 .class_attrs = gpio_class_attrs,
735 };
736
737
738 /**
739 * gpio_export - export a GPIO through sysfs
740 * @gpio: gpio to make available, already requested
741 * @direction_may_change: true if userspace may change gpio direction
742 * Context: arch_initcall or later
743 *
744 * When drivers want to make a GPIO accessible to userspace after they
745 * have requested it -- perhaps while debugging, or as part of their
746 * public interface -- they may use this routine. If the GPIO can
747 * change direction (some can't) and the caller allows it, userspace
748 * will see "direction" sysfs attribute which may be used to change
749 * the gpio's direction. A "value" attribute will always be provided.
750 *
751 * Returns zero on success, else an error.
752 */
753 static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
754 {
755 unsigned long flags;
756 int status;
757 const char *ioname = NULL;
758 struct device *dev;
759 int offset;
760
761 /* can't export until sysfs is available ... */
762 if (!gpio_class.p) {
763 pr_debug("%s: called too early!\n", __func__);
764 return -ENOENT;
765 }
766
767 if (!desc) {
768 pr_debug("%s: invalid gpio descriptor\n", __func__);
769 return -EINVAL;
770 }
771
772 mutex_lock(&sysfs_lock);
773
774 spin_lock_irqsave(&gpio_lock, flags);
775 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
776 test_bit(FLAG_EXPORT, &desc->flags)) {
777 spin_unlock_irqrestore(&gpio_lock, flags);
778 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
779 __func__, desc_to_gpio(desc),
780 test_bit(FLAG_REQUESTED, &desc->flags),
781 test_bit(FLAG_EXPORT, &desc->flags));
782 status = -EPERM;
783 goto fail_unlock;
784 }
785
786 if (!desc->chip->direction_input || !desc->chip->direction_output)
787 direction_may_change = false;
788 spin_unlock_irqrestore(&gpio_lock, flags);
789
790 offset = gpio_chip_hwgpio(desc);
791 if (desc->chip->names && desc->chip->names[offset])
792 ioname = desc->chip->names[offset];
793
794 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
795 desc, ioname ? ioname : "gpio%u",
796 desc_to_gpio(desc));
797 if (IS_ERR(dev)) {
798 status = PTR_ERR(dev);
799 goto fail_unlock;
800 }
801
802 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
803 if (status)
804 goto fail_unregister_device;
805
806 if (direction_may_change) {
807 status = device_create_file(dev, &dev_attr_direction);
808 if (status)
809 goto fail_remove_attr_group;
810 }
811
812 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
813 !test_bit(FLAG_IS_OUT, &desc->flags))) {
814 status = device_create_file(dev, &dev_attr_edge);
815 if (status)
816 goto fail_remove_attr_direction;
817 }
818
819 set_bit(FLAG_EXPORT, &desc->flags);
820 mutex_unlock(&sysfs_lock);
821 return 0;
822
823 fail_remove_attr_direction:
824 device_remove_file(dev, &dev_attr_direction);
825 fail_remove_attr_group:
826 sysfs_remove_group(&dev->kobj, &gpio_attr_group);
827 fail_unregister_device:
828 device_unregister(dev);
829 fail_unlock:
830 mutex_unlock(&sysfs_lock);
831 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
832 status);
833 return status;
834 }
835
836 int gpio_export(unsigned gpio, bool direction_may_change)
837 {
838 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
839 }
840 EXPORT_SYMBOL_GPL(gpio_export);
841
842 static int match_export(struct device *dev, const void *data)
843 {
844 return dev_get_drvdata(dev) == data;
845 }
846
847 /**
848 * gpio_export_link - create a sysfs link to an exported GPIO node
849 * @dev: device under which to create symlink
850 * @name: name of the symlink
851 * @gpio: gpio to create symlink to, already exported
852 *
853 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
854 * node. Caller is responsible for unlinking.
855 *
856 * Returns zero on success, else an error.
857 */
858 static int gpiod_export_link(struct device *dev, const char *name,
859 struct gpio_desc *desc)
860 {
861 int status = -EINVAL;
862
863 if (!desc) {
864 pr_warn("%s: invalid GPIO\n", __func__);
865 return -EINVAL;
866 }
867
868 mutex_lock(&sysfs_lock);
869
870 if (test_bit(FLAG_EXPORT, &desc->flags)) {
871 struct device *tdev;
872
873 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
874 if (tdev != NULL) {
875 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
876 name);
877 put_device(tdev);
878 } else {
879 status = -ENODEV;
880 }
881 }
882
883 mutex_unlock(&sysfs_lock);
884
885 if (status)
886 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
887 status);
888
889 return status;
890 }
891
892 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
893 {
894 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
895 }
896 EXPORT_SYMBOL_GPL(gpio_export_link);
897
898 /**
899 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
900 * @gpio: gpio to change
901 * @value: non-zero to use active low, i.e. inverted values
902 *
903 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
904 * The GPIO does not have to be exported yet. If poll(2) support has
905 * been enabled for either rising or falling edge, it will be
906 * reconfigured to follow the new polarity.
907 *
908 * Returns zero on success, else an error.
909 */
910 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
911 {
912 struct device *dev = NULL;
913 int status = -EINVAL;
914
915 if (!desc) {
916 pr_warn("%s: invalid GPIO\n", __func__);
917 return -EINVAL;
918 }
919
920 mutex_lock(&sysfs_lock);
921
922 if (test_bit(FLAG_EXPORT, &desc->flags)) {
923 dev = class_find_device(&gpio_class, NULL, desc, match_export);
924 if (dev == NULL) {
925 status = -ENODEV;
926 goto unlock;
927 }
928 }
929
930 status = sysfs_set_active_low(desc, dev, value);
931 put_device(dev);
932 unlock:
933 mutex_unlock(&sysfs_lock);
934
935 if (status)
936 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
937 status);
938
939 return status;
940 }
941
942 int gpio_sysfs_set_active_low(unsigned gpio, int value)
943 {
944 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
945 }
946 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
947
948 /**
949 * gpio_unexport - reverse effect of gpio_export()
950 * @gpio: gpio to make unavailable
951 *
952 * This is implicit on gpio_free().
953 */
954 static void gpiod_unexport(struct gpio_desc *desc)
955 {
956 int status = 0;
957 struct device *dev = NULL;
958
959 if (!desc) {
960 pr_warn("%s: invalid GPIO\n", __func__);
961 return;
962 }
963
964 mutex_lock(&sysfs_lock);
965
966 if (test_bit(FLAG_EXPORT, &desc->flags)) {
967
968 dev = class_find_device(&gpio_class, NULL, desc, match_export);
969 if (dev) {
970 gpio_setup_irq(desc, dev, 0);
971 clear_bit(FLAG_EXPORT, &desc->flags);
972 } else
973 status = -ENODEV;
974 }
975
976 mutex_unlock(&sysfs_lock);
977
978 if (dev) {
979 device_remove_file(dev, &dev_attr_edge);
980 device_remove_file(dev, &dev_attr_direction);
981 sysfs_remove_group(&dev->kobj, &gpio_attr_group);
982 device_unregister(dev);
983 put_device(dev);
984 }
985
986 if (status)
987 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
988 status);
989 }
990
991 void gpio_unexport(unsigned gpio)
992 {
993 gpiod_unexport(gpio_to_desc(gpio));
994 }
995 EXPORT_SYMBOL_GPL(gpio_unexport);
996
997 static int gpiochip_export(struct gpio_chip *chip)
998 {
999 int status;
1000 struct device *dev;
1001
1002 /* Many systems register gpio chips for SOC support very early,
1003 * before driver model support is available. In those cases we
1004 * export this later, in gpiolib_sysfs_init() ... here we just
1005 * verify that _some_ field of gpio_class got initialized.
1006 */
1007 if (!gpio_class.p)
1008 return 0;
1009
1010 /* use chip->base for the ID; it's already known to be unique */
1011 mutex_lock(&sysfs_lock);
1012 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1013 "gpiochip%d", chip->base);
1014 if (!IS_ERR(dev)) {
1015 status = sysfs_create_group(&dev->kobj,
1016 &gpiochip_attr_group);
1017 } else
1018 status = PTR_ERR(dev);
1019 chip->exported = (status == 0);
1020 mutex_unlock(&sysfs_lock);
1021
1022 if (status) {
1023 unsigned long flags;
1024 unsigned gpio;
1025
1026 spin_lock_irqsave(&gpio_lock, flags);
1027 gpio = 0;
1028 while (gpio < chip->ngpio)
1029 chip->desc[gpio++].chip = NULL;
1030 spin_unlock_irqrestore(&gpio_lock, flags);
1031
1032 pr_debug("%s: chip %s status %d\n", __func__,
1033 chip->label, status);
1034 }
1035
1036 return status;
1037 }
1038
1039 static void gpiochip_unexport(struct gpio_chip *chip)
1040 {
1041 int status;
1042 struct device *dev;
1043
1044 mutex_lock(&sysfs_lock);
1045 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1046 if (dev) {
1047 sysfs_remove_group(&dev->kobj, &gpiochip_attr_group);
1048 put_device(dev);
1049 device_unregister(dev);
1050 chip->exported = 0;
1051 status = 0;
1052 } else
1053 status = -ENODEV;
1054 mutex_unlock(&sysfs_lock);
1055
1056 if (status)
1057 pr_debug("%s: chip %s status %d\n", __func__,
1058 chip->label, status);
1059 }
1060
1061 static int __init gpiolib_sysfs_init(void)
1062 {
1063 int status;
1064 unsigned long flags;
1065 struct gpio_chip *chip;
1066
1067 status = class_register(&gpio_class);
1068 if (status < 0)
1069 return status;
1070
1071 /* Scan and register the gpio_chips which registered very
1072 * early (e.g. before the class_register above was called).
1073 *
1074 * We run before arch_initcall() so chip->dev nodes can have
1075 * registered, and so arch_initcall() can always gpio_export().
1076 */
1077 spin_lock_irqsave(&gpio_lock, flags);
1078 list_for_each_entry(chip, &gpio_chips, list) {
1079 if (!chip || chip->exported)
1080 continue;
1081
1082 spin_unlock_irqrestore(&gpio_lock, flags);
1083 status = gpiochip_export(chip);
1084 spin_lock_irqsave(&gpio_lock, flags);
1085 }
1086 spin_unlock_irqrestore(&gpio_lock, flags);
1087
1088
1089 return status;
1090 }
1091 postcore_initcall(gpiolib_sysfs_init);
1092
1093 #else
1094 static inline int gpiochip_export(struct gpio_chip *chip)
1095 {
1096 return 0;
1097 }
1098
1099 static inline void gpiochip_unexport(struct gpio_chip *chip)
1100 {
1101 }
1102
1103 static inline int gpiod_export(struct gpio_desc *desc,
1104 bool direction_may_change)
1105 {
1106 return -ENOSYS;
1107 }
1108
1109 static inline int gpiod_export_link(struct device *dev, const char *name,
1110 struct gpio_desc *desc)
1111 {
1112 return -ENOSYS;
1113 }
1114
1115 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1116 {
1117 return -ENOSYS;
1118 }
1119
1120 static inline void gpiod_unexport(struct gpio_desc *desc)
1121 {
1122 }
1123
1124 #endif /* CONFIG_GPIO_SYSFS */
1125
1126 /*
1127 * Add a new chip to the global chips list, keeping the list of chips sorted
1128 * by base order.
1129 *
1130 * Return -EBUSY if the new chip overlaps with some other chip's integer
1131 * space.
1132 */
1133 static int gpiochip_add_to_list(struct gpio_chip *chip)
1134 {
1135 struct list_head *pos = &gpio_chips;
1136 struct gpio_chip *_chip;
1137 int err = 0;
1138
1139 /* find where to insert our chip */
1140 list_for_each(pos, &gpio_chips) {
1141 _chip = list_entry(pos, struct gpio_chip, list);
1142 /* shall we insert before _chip? */
1143 if (_chip->base >= chip->base + chip->ngpio)
1144 break;
1145 }
1146
1147 /* are we stepping on the chip right before? */
1148 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1149 _chip = list_entry(pos->prev, struct gpio_chip, list);
1150 if (_chip->base + _chip->ngpio > chip->base) {
1151 dev_err(chip->dev,
1152 "GPIO integer space overlap, cannot add chip\n");
1153 err = -EBUSY;
1154 }
1155 }
1156
1157 if (!err)
1158 list_add_tail(&chip->list, pos);
1159
1160 return err;
1161 }
1162
1163 /**
1164 * gpiochip_add() - register a gpio_chip
1165 * @chip: the chip to register, with chip->base initialized
1166 * Context: potentially before irqs or kmalloc will work
1167 *
1168 * Returns a negative errno if the chip can't be registered, such as
1169 * because the chip->base is invalid or already associated with a
1170 * different chip. Otherwise it returns zero as a success code.
1171 *
1172 * When gpiochip_add() is called very early during boot, so that GPIOs
1173 * can be freely used, the chip->dev device must be registered before
1174 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1175 * for GPIOs will fail rudely.
1176 *
1177 * If chip->base is negative, this requests dynamic assignment of
1178 * a range of valid GPIOs.
1179 */
1180 int gpiochip_add(struct gpio_chip *chip)
1181 {
1182 unsigned long flags;
1183 int status = 0;
1184 unsigned id;
1185 int base = chip->base;
1186
1187 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1188 && base >= 0) {
1189 status = -EINVAL;
1190 goto fail;
1191 }
1192
1193 spin_lock_irqsave(&gpio_lock, flags);
1194
1195 if (base < 0) {
1196 base = gpiochip_find_base(chip->ngpio);
1197 if (base < 0) {
1198 status = base;
1199 goto unlock;
1200 }
1201 chip->base = base;
1202 }
1203
1204 status = gpiochip_add_to_list(chip);
1205
1206 if (status == 0) {
1207 chip->desc = &gpio_desc[chip->base];
1208
1209 for (id = 0; id < chip->ngpio; id++) {
1210 struct gpio_desc *desc = &chip->desc[id];
1211 desc->chip = chip;
1212
1213 /* REVISIT: most hardware initializes GPIOs as
1214 * inputs (often with pullups enabled) so power
1215 * usage is minimized. Linux code should set the
1216 * gpio direction first thing; but until it does,
1217 * and in case chip->get_direction is not set,
1218 * we may expose the wrong direction in sysfs.
1219 */
1220 desc->flags = !chip->direction_input
1221 ? (1 << FLAG_IS_OUT)
1222 : 0;
1223 }
1224 }
1225
1226 spin_unlock_irqrestore(&gpio_lock, flags);
1227
1228 #ifdef CONFIG_PINCTRL
1229 INIT_LIST_HEAD(&chip->pin_ranges);
1230 #endif
1231
1232 of_gpiochip_add(chip);
1233
1234 if (status)
1235 goto fail;
1236
1237 status = gpiochip_export(chip);
1238 if (status)
1239 goto fail;
1240
1241 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1242 chip->base, chip->base + chip->ngpio - 1,
1243 chip->label ? : "generic");
1244
1245 return 0;
1246
1247 unlock:
1248 spin_unlock_irqrestore(&gpio_lock, flags);
1249 fail:
1250 /* failures here can mean systems won't boot... */
1251 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1252 chip->base, chip->base + chip->ngpio - 1,
1253 chip->label ? : "generic");
1254 return status;
1255 }
1256 EXPORT_SYMBOL_GPL(gpiochip_add);
1257
1258 /**
1259 * gpiochip_remove() - unregister a gpio_chip
1260 * @chip: the chip to unregister
1261 *
1262 * A gpio_chip with any GPIOs still requested may not be removed.
1263 */
1264 int gpiochip_remove(struct gpio_chip *chip)
1265 {
1266 unsigned long flags;
1267 int status = 0;
1268 unsigned id;
1269
1270 spin_lock_irqsave(&gpio_lock, flags);
1271
1272 gpiochip_remove_pin_ranges(chip);
1273 of_gpiochip_remove(chip);
1274
1275 for (id = 0; id < chip->ngpio; id++) {
1276 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
1277 status = -EBUSY;
1278 break;
1279 }
1280 }
1281 if (status == 0) {
1282 for (id = 0; id < chip->ngpio; id++)
1283 chip->desc[id].chip = NULL;
1284
1285 list_del(&chip->list);
1286 }
1287
1288 spin_unlock_irqrestore(&gpio_lock, flags);
1289
1290 if (status == 0)
1291 gpiochip_unexport(chip);
1292
1293 return status;
1294 }
1295 EXPORT_SYMBOL_GPL(gpiochip_remove);
1296
1297 /**
1298 * gpiochip_find() - iterator for locating a specific gpio_chip
1299 * @data: data to pass to match function
1300 * @callback: Callback function to check gpio_chip
1301 *
1302 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1303 * determined by a user supplied @match callback. The callback should return
1304 * 0 if the device doesn't match and non-zero if it does. If the callback is
1305 * non-zero, this function will return to the caller and not iterate over any
1306 * more gpio_chips.
1307 */
1308 struct gpio_chip *gpiochip_find(void *data,
1309 int (*match)(struct gpio_chip *chip,
1310 void *data))
1311 {
1312 struct gpio_chip *chip;
1313 unsigned long flags;
1314
1315 spin_lock_irqsave(&gpio_lock, flags);
1316 list_for_each_entry(chip, &gpio_chips, list)
1317 if (match(chip, data))
1318 break;
1319
1320 /* No match? */
1321 if (&chip->list == &gpio_chips)
1322 chip = NULL;
1323 spin_unlock_irqrestore(&gpio_lock, flags);
1324
1325 return chip;
1326 }
1327 EXPORT_SYMBOL_GPL(gpiochip_find);
1328
1329 #ifdef CONFIG_PINCTRL
1330
1331 /**
1332 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1333 * @chip: the gpiochip to add the range for
1334 * @pinctrl_name: the dev_name() of the pin controller to map to
1335 * @gpio_offset: the start offset in the current gpio_chip number space
1336 * @pin_offset: the start offset in the pin controller number space
1337 * @npins: the number of pins from the offset of each pin space (GPIO and
1338 * pin controller) to accumulate in this range
1339 */
1340 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1341 unsigned int gpio_offset, unsigned int pin_offset,
1342 unsigned int npins)
1343 {
1344 struct gpio_pin_range *pin_range;
1345 int ret;
1346
1347 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1348 if (!pin_range) {
1349 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1350 chip->label);
1351 return -ENOMEM;
1352 }
1353
1354 /* Use local offset as range ID */
1355 pin_range->range.id = gpio_offset;
1356 pin_range->range.gc = chip;
1357 pin_range->range.name = chip->label;
1358 pin_range->range.base = chip->base + gpio_offset;
1359 pin_range->range.pin_base = pin_offset;
1360 pin_range->range.npins = npins;
1361 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1362 &pin_range->range);
1363 if (IS_ERR(pin_range->pctldev)) {
1364 ret = PTR_ERR(pin_range->pctldev);
1365 pr_err("%s: GPIO chip: could not create pin range\n",
1366 chip->label);
1367 kfree(pin_range);
1368 return ret;
1369 }
1370 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1371 chip->label, gpio_offset, gpio_offset + npins - 1,
1372 pinctl_name,
1373 pin_offset, pin_offset + npins - 1);
1374
1375 list_add_tail(&pin_range->node, &chip->pin_ranges);
1376
1377 return 0;
1378 }
1379 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1380
1381 /**
1382 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1383 * @chip: the chip to remove all the mappings for
1384 */
1385 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1386 {
1387 struct gpio_pin_range *pin_range, *tmp;
1388
1389 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1390 list_del(&pin_range->node);
1391 pinctrl_remove_gpio_range(pin_range->pctldev,
1392 &pin_range->range);
1393 kfree(pin_range);
1394 }
1395 }
1396 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1397
1398 #endif /* CONFIG_PINCTRL */
1399
1400 /* These "optional" allocation calls help prevent drivers from stomping
1401 * on each other, and help provide better diagnostics in debugfs.
1402 * They're called even less than the "set direction" calls.
1403 */
1404 static int gpiod_request(struct gpio_desc *desc, const char *label)
1405 {
1406 struct gpio_chip *chip;
1407 int status = -EPROBE_DEFER;
1408 unsigned long flags;
1409
1410 if (!desc) {
1411 pr_warn("%s: invalid GPIO\n", __func__);
1412 return -EINVAL;
1413 }
1414
1415 spin_lock_irqsave(&gpio_lock, flags);
1416
1417 chip = desc->chip;
1418 if (chip == NULL)
1419 goto done;
1420
1421 if (!try_module_get(chip->owner))
1422 goto done;
1423
1424 /* NOTE: gpio_request() can be called in early boot,
1425 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1426 */
1427
1428 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1429 desc_set_label(desc, label ? : "?");
1430 status = 0;
1431 } else {
1432 status = -EBUSY;
1433 module_put(chip->owner);
1434 goto done;
1435 }
1436
1437 if (chip->request) {
1438 /* chip->request may sleep */
1439 spin_unlock_irqrestore(&gpio_lock, flags);
1440 status = chip->request(chip, gpio_chip_hwgpio(desc));
1441 spin_lock_irqsave(&gpio_lock, flags);
1442
1443 if (status < 0) {
1444 desc_set_label(desc, NULL);
1445 module_put(chip->owner);
1446 clear_bit(FLAG_REQUESTED, &desc->flags);
1447 goto done;
1448 }
1449 }
1450 if (chip->get_direction) {
1451 /* chip->get_direction may sleep */
1452 spin_unlock_irqrestore(&gpio_lock, flags);
1453 gpiod_get_direction(desc);
1454 spin_lock_irqsave(&gpio_lock, flags);
1455 }
1456 done:
1457 if (status)
1458 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1459 desc_to_gpio(desc), label ? : "?", status);
1460 spin_unlock_irqrestore(&gpio_lock, flags);
1461 return status;
1462 }
1463
1464 int gpio_request(unsigned gpio, const char *label)
1465 {
1466 return gpiod_request(gpio_to_desc(gpio), label);
1467 }
1468 EXPORT_SYMBOL_GPL(gpio_request);
1469
1470 static void gpiod_free(struct gpio_desc *desc)
1471 {
1472 unsigned long flags;
1473 struct gpio_chip *chip;
1474
1475 might_sleep();
1476
1477 if (!desc) {
1478 WARN_ON(extra_checks);
1479 return;
1480 }
1481
1482 gpiod_unexport(desc);
1483
1484 spin_lock_irqsave(&gpio_lock, flags);
1485
1486 chip = desc->chip;
1487 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1488 if (chip->free) {
1489 spin_unlock_irqrestore(&gpio_lock, flags);
1490 might_sleep_if(chip->can_sleep);
1491 chip->free(chip, gpio_chip_hwgpio(desc));
1492 spin_lock_irqsave(&gpio_lock, flags);
1493 }
1494 desc_set_label(desc, NULL);
1495 module_put(desc->chip->owner);
1496 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1497 clear_bit(FLAG_REQUESTED, &desc->flags);
1498 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1499 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1500 } else
1501 WARN_ON(extra_checks);
1502
1503 spin_unlock_irqrestore(&gpio_lock, flags);
1504 }
1505
1506 void gpio_free(unsigned gpio)
1507 {
1508 gpiod_free(gpio_to_desc(gpio));
1509 }
1510 EXPORT_SYMBOL_GPL(gpio_free);
1511
1512 /**
1513 * gpio_request_one - request a single GPIO with initial configuration
1514 * @gpio: the GPIO number
1515 * @flags: GPIO configuration as specified by GPIOF_*
1516 * @label: a literal description string of this GPIO
1517 */
1518 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1519 {
1520 struct gpio_desc *desc;
1521 int err;
1522
1523 desc = gpio_to_desc(gpio);
1524
1525 err = gpiod_request(desc, label);
1526 if (err)
1527 return err;
1528
1529 if (flags & GPIOF_OPEN_DRAIN)
1530 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1531
1532 if (flags & GPIOF_OPEN_SOURCE)
1533 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1534
1535 if (flags & GPIOF_DIR_IN)
1536 err = gpiod_direction_input(desc);
1537 else
1538 err = gpiod_direction_output(desc,
1539 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1540
1541 if (err)
1542 goto free_gpio;
1543
1544 if (flags & GPIOF_EXPORT) {
1545 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
1546 if (err)
1547 goto free_gpio;
1548 }
1549
1550 return 0;
1551
1552 free_gpio:
1553 gpiod_free(desc);
1554 return err;
1555 }
1556 EXPORT_SYMBOL_GPL(gpio_request_one);
1557
1558 /**
1559 * gpio_request_array - request multiple GPIOs in a single call
1560 * @array: array of the 'struct gpio'
1561 * @num: how many GPIOs in the array
1562 */
1563 int gpio_request_array(const struct gpio *array, size_t num)
1564 {
1565 int i, err;
1566
1567 for (i = 0; i < num; i++, array++) {
1568 err = gpio_request_one(array->gpio, array->flags, array->label);
1569 if (err)
1570 goto err_free;
1571 }
1572 return 0;
1573
1574 err_free:
1575 while (i--)
1576 gpio_free((--array)->gpio);
1577 return err;
1578 }
1579 EXPORT_SYMBOL_GPL(gpio_request_array);
1580
1581 /**
1582 * gpio_free_array - release multiple GPIOs in a single call
1583 * @array: array of the 'struct gpio'
1584 * @num: how many GPIOs in the array
1585 */
1586 void gpio_free_array(const struct gpio *array, size_t num)
1587 {
1588 while (num--)
1589 gpio_free((array++)->gpio);
1590 }
1591 EXPORT_SYMBOL_GPL(gpio_free_array);
1592
1593 /**
1594 * gpiochip_is_requested - return string iff signal was requested
1595 * @chip: controller managing the signal
1596 * @offset: of signal within controller's 0..(ngpio - 1) range
1597 *
1598 * Returns NULL if the GPIO is not currently requested, else a string.
1599 * If debugfs support is enabled, the string returned is the label passed
1600 * to gpio_request(); otherwise it is a meaningless constant.
1601 *
1602 * This function is for use by GPIO controller drivers. The label can
1603 * help with diagnostics, and knowing that the signal is used as a GPIO
1604 * can help avoid accidentally multiplexing it to another controller.
1605 */
1606 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1607 {
1608 struct gpio_desc *desc;
1609
1610 if (!GPIO_OFFSET_VALID(chip, offset))
1611 return NULL;
1612
1613 desc = &chip->desc[offset];
1614
1615 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1616 return NULL;
1617 #ifdef CONFIG_DEBUG_FS
1618 return desc->label;
1619 #else
1620 return "?";
1621 #endif
1622 }
1623 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1624
1625
1626 /* Drivers MUST set GPIO direction before making get/set calls. In
1627 * some cases this is done in early boot, before IRQs are enabled.
1628 *
1629 * As a rule these aren't called more than once (except for drivers
1630 * using the open-drain emulation idiom) so these are natural places
1631 * to accumulate extra debugging checks. Note that we can't (yet)
1632 * rely on gpio_request() having been called beforehand.
1633 */
1634
1635 static int gpiod_direction_input(struct gpio_desc *desc)
1636 {
1637 unsigned long flags;
1638 struct gpio_chip *chip;
1639 int status = -EINVAL;
1640 int offset;
1641
1642 if (!desc) {
1643 pr_warn("%s: invalid GPIO\n", __func__);
1644 return -EINVAL;
1645 }
1646
1647 spin_lock_irqsave(&gpio_lock, flags);
1648
1649 chip = desc->chip;
1650 if (!chip || !chip->get || !chip->direction_input)
1651 goto fail;
1652 status = gpio_ensure_requested(desc);
1653 if (status < 0)
1654 goto fail;
1655
1656 /* now we know the gpio is valid and chip won't vanish */
1657
1658 spin_unlock_irqrestore(&gpio_lock, flags);
1659
1660 might_sleep_if(chip->can_sleep);
1661
1662 offset = gpio_chip_hwgpio(desc);
1663 if (status) {
1664 status = chip->request(chip, offset);
1665 if (status < 0) {
1666 pr_debug("GPIO-%d: chip request fail, %d\n",
1667 desc_to_gpio(desc), status);
1668 /* and it's not available to anyone else ...
1669 * gpio_request() is the fully clean solution.
1670 */
1671 goto lose;
1672 }
1673 }
1674
1675 status = chip->direction_input(chip, offset);
1676 if (status == 0)
1677 clear_bit(FLAG_IS_OUT, &desc->flags);
1678
1679 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1680 lose:
1681 return status;
1682 fail:
1683 spin_unlock_irqrestore(&gpio_lock, flags);
1684 if (status)
1685 pr_debug("%s: gpio-%d status %d\n", __func__,
1686 desc_to_gpio(desc), status);
1687 return status;
1688 }
1689
1690 int gpio_direction_input(unsigned gpio)
1691 {
1692 return gpiod_direction_input(gpio_to_desc(gpio));
1693 }
1694 EXPORT_SYMBOL_GPL(gpio_direction_input);
1695
1696 static int gpiod_direction_output(struct gpio_desc *desc, int value)
1697 {
1698 unsigned long flags;
1699 struct gpio_chip *chip;
1700 int status = -EINVAL;
1701 int offset;
1702
1703 if (!desc) {
1704 pr_warn("%s: invalid GPIO\n", __func__);
1705 return -EINVAL;
1706 }
1707
1708 /* Open drain pin should not be driven to 1 */
1709 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1710 return gpiod_direction_input(desc);
1711
1712 /* Open source pin should not be driven to 0 */
1713 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1714 return gpiod_direction_input(desc);
1715
1716 spin_lock_irqsave(&gpio_lock, flags);
1717
1718 chip = desc->chip;
1719 if (!chip || !chip->set || !chip->direction_output)
1720 goto fail;
1721 status = gpio_ensure_requested(desc);
1722 if (status < 0)
1723 goto fail;
1724
1725 /* now we know the gpio is valid and chip won't vanish */
1726
1727 spin_unlock_irqrestore(&gpio_lock, flags);
1728
1729 might_sleep_if(chip->can_sleep);
1730
1731 offset = gpio_chip_hwgpio(desc);
1732 if (status) {
1733 status = chip->request(chip, offset);
1734 if (status < 0) {
1735 pr_debug("GPIO-%d: chip request fail, %d\n",
1736 desc_to_gpio(desc), status);
1737 /* and it's not available to anyone else ...
1738 * gpio_request() is the fully clean solution.
1739 */
1740 goto lose;
1741 }
1742 }
1743
1744 status = chip->direction_output(chip, offset, value);
1745 if (status == 0)
1746 set_bit(FLAG_IS_OUT, &desc->flags);
1747 trace_gpio_value(desc_to_gpio(desc), 0, value);
1748 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1749 lose:
1750 return status;
1751 fail:
1752 spin_unlock_irqrestore(&gpio_lock, flags);
1753 if (status)
1754 pr_debug("%s: gpio-%d status %d\n", __func__,
1755 desc_to_gpio(desc), status);
1756 return status;
1757 }
1758
1759 int gpio_direction_output(unsigned gpio, int value)
1760 {
1761 return gpiod_direction_output(gpio_to_desc(gpio), value);
1762 }
1763 EXPORT_SYMBOL_GPL(gpio_direction_output);
1764
1765 /**
1766 * gpio_set_debounce - sets @debounce time for a @gpio
1767 * @gpio: the gpio to set debounce time
1768 * @debounce: debounce time is microseconds
1769 */
1770 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1771 {
1772 unsigned long flags;
1773 struct gpio_chip *chip;
1774 int status = -EINVAL;
1775 int offset;
1776
1777 if (!desc) {
1778 pr_warn("%s: invalid GPIO\n", __func__);
1779 return -EINVAL;
1780 }
1781
1782 spin_lock_irqsave(&gpio_lock, flags);
1783
1784 chip = desc->chip;
1785 if (!chip || !chip->set || !chip->set_debounce)
1786 goto fail;
1787
1788 status = gpio_ensure_requested(desc);
1789 if (status < 0)
1790 goto fail;
1791
1792 /* now we know the gpio is valid and chip won't vanish */
1793
1794 spin_unlock_irqrestore(&gpio_lock, flags);
1795
1796 might_sleep_if(chip->can_sleep);
1797
1798 offset = gpio_chip_hwgpio(desc);
1799 return chip->set_debounce(chip, offset, debounce);
1800
1801 fail:
1802 spin_unlock_irqrestore(&gpio_lock, flags);
1803 if (status)
1804 pr_debug("%s: gpio-%d status %d\n", __func__,
1805 desc_to_gpio(desc), status);
1806
1807 return status;
1808 }
1809
1810 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1811 {
1812 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1813 }
1814 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1815
1816 /* I/O calls are only valid after configuration completed; the relevant
1817 * "is this a valid GPIO" error checks should already have been done.
1818 *
1819 * "Get" operations are often inlinable as reading a pin value register,
1820 * and masking the relevant bit in that register.
1821 *
1822 * When "set" operations are inlinable, they involve writing that mask to
1823 * one register to set a low value, or a different register to set it high.
1824 * Otherwise locking is needed, so there may be little value to inlining.
1825 *
1826 *------------------------------------------------------------------------
1827 *
1828 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1829 * have requested the GPIO. That can include implicit requesting by
1830 * a direction setting call. Marking a gpio as requested locks its chip
1831 * in memory, guaranteeing that these table lookups need no more locking
1832 * and that gpiochip_remove() will fail.
1833 *
1834 * REVISIT when debugging, consider adding some instrumentation to ensure
1835 * that the GPIO was actually requested.
1836 */
1837
1838 /**
1839 * __gpio_get_value() - return a gpio's value
1840 * @gpio: gpio whose value will be returned
1841 * Context: any
1842 *
1843 * This is used directly or indirectly to implement gpio_get_value().
1844 * It returns the zero or nonzero value provided by the associated
1845 * gpio_chip.get() method; or zero if no such method is provided.
1846 */
1847 static int gpiod_get_value(const struct gpio_desc *desc)
1848 {
1849 struct gpio_chip *chip;
1850 int value;
1851 int offset;
1852
1853 if (!desc)
1854 return 0;
1855 chip = desc->chip;
1856 offset = gpio_chip_hwgpio(desc);
1857 /* Should be using gpio_get_value_cansleep() */
1858 WARN_ON(chip->can_sleep);
1859 value = chip->get ? chip->get(chip, offset) : 0;
1860 trace_gpio_value(desc_to_gpio(desc), 1, value);
1861 return value;
1862 }
1863
1864 int __gpio_get_value(unsigned gpio)
1865 {
1866 return gpiod_get_value(gpio_to_desc(gpio));
1867 }
1868 EXPORT_SYMBOL_GPL(__gpio_get_value);
1869
1870 /*
1871 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1872 * @gpio: Gpio whose state need to be set.
1873 * @chip: Gpio chip.
1874 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1875 */
1876 static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
1877 {
1878 int err = 0;
1879 struct gpio_chip *chip = desc->chip;
1880 int offset = gpio_chip_hwgpio(desc);
1881
1882 if (value) {
1883 err = chip->direction_input(chip, offset);
1884 if (!err)
1885 clear_bit(FLAG_IS_OUT, &desc->flags);
1886 } else {
1887 err = chip->direction_output(chip, offset, 0);
1888 if (!err)
1889 set_bit(FLAG_IS_OUT, &desc->flags);
1890 }
1891 trace_gpio_direction(desc_to_gpio(desc), value, err);
1892 if (err < 0)
1893 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1894 __func__, desc_to_gpio(desc), err);
1895 }
1896
1897 /*
1898 * _gpio_set_open_source() - Set the open source gpio's value.
1899 * @gpio: Gpio whose state need to be set.
1900 * @chip: Gpio chip.
1901 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1902 */
1903 static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
1904 {
1905 int err = 0;
1906 struct gpio_chip *chip = desc->chip;
1907 int offset = gpio_chip_hwgpio(desc);
1908
1909 if (value) {
1910 err = chip->direction_output(chip, offset, 1);
1911 if (!err)
1912 set_bit(FLAG_IS_OUT, &desc->flags);
1913 } else {
1914 err = chip->direction_input(chip, offset);
1915 if (!err)
1916 clear_bit(FLAG_IS_OUT, &desc->flags);
1917 }
1918 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1919 if (err < 0)
1920 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1921 __func__, desc_to_gpio(desc), err);
1922 }
1923
1924 /**
1925 * __gpio_set_value() - assign a gpio's value
1926 * @gpio: gpio whose value will be assigned
1927 * @value: value to assign
1928 * Context: any
1929 *
1930 * This is used directly or indirectly to implement gpio_set_value().
1931 * It invokes the associated gpio_chip.set() method.
1932 */
1933 static void gpiod_set_value(struct gpio_desc *desc, int value)
1934 {
1935 struct gpio_chip *chip;
1936
1937 if (!desc)
1938 return;
1939 chip = desc->chip;
1940 /* Should be using gpio_set_value_cansleep() */
1941 WARN_ON(chip->can_sleep);
1942 trace_gpio_value(desc_to_gpio(desc), 0, value);
1943 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1944 _gpio_set_open_drain_value(desc, value);
1945 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1946 _gpio_set_open_source_value(desc, value);
1947 else
1948 chip->set(chip, gpio_chip_hwgpio(desc), value);
1949 }
1950
1951 void __gpio_set_value(unsigned gpio, int value)
1952 {
1953 return gpiod_set_value(gpio_to_desc(gpio), value);
1954 }
1955 EXPORT_SYMBOL_GPL(__gpio_set_value);
1956
1957 /**
1958 * __gpio_cansleep() - report whether gpio value access will sleep
1959 * @gpio: gpio in question
1960 * Context: any
1961 *
1962 * This is used directly or indirectly to implement gpio_cansleep(). It
1963 * returns nonzero if access reading or writing the GPIO value can sleep.
1964 */
1965 static int gpiod_cansleep(const struct gpio_desc *desc)
1966 {
1967 if (!desc)
1968 return 0;
1969 /* only call this on GPIOs that are valid! */
1970 return desc->chip->can_sleep;
1971 }
1972
1973 int __gpio_cansleep(unsigned gpio)
1974 {
1975 return gpiod_cansleep(gpio_to_desc(gpio));
1976 }
1977 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1978
1979 /**
1980 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1981 * @gpio: gpio whose IRQ will be returned (already requested)
1982 * Context: any
1983 *
1984 * This is used directly or indirectly to implement gpio_to_irq().
1985 * It returns the number of the IRQ signaled by this (input) GPIO,
1986 * or a negative errno.
1987 */
1988 static int gpiod_to_irq(const struct gpio_desc *desc)
1989 {
1990 struct gpio_chip *chip;
1991 int offset;
1992
1993 if (!desc)
1994 return -EINVAL;
1995 chip = desc->chip;
1996 offset = gpio_chip_hwgpio(desc);
1997 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1998 }
1999
2000 int __gpio_to_irq(unsigned gpio)
2001 {
2002 return gpiod_to_irq(gpio_to_desc(gpio));
2003 }
2004 EXPORT_SYMBOL_GPL(__gpio_to_irq);
2005
2006
2007 /* There's no value in making it easy to inline GPIO calls that may sleep.
2008 * Common examples include ones connected to I2C or SPI chips.
2009 */
2010
2011 static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2012 {
2013 struct gpio_chip *chip;
2014 int value;
2015 int offset;
2016
2017 might_sleep_if(extra_checks);
2018 if (!desc)
2019 return 0;
2020 chip = desc->chip;
2021 offset = gpio_chip_hwgpio(desc);
2022 value = chip->get ? chip->get(chip, offset) : 0;
2023 trace_gpio_value(desc_to_gpio(desc), 1, value);
2024 return value;
2025 }
2026
2027 int gpio_get_value_cansleep(unsigned gpio)
2028 {
2029 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2030 }
2031 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2032
2033 static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2034 {
2035 struct gpio_chip *chip;
2036
2037 might_sleep_if(extra_checks);
2038 if (!desc)
2039 return;
2040 chip = desc->chip;
2041 trace_gpio_value(desc_to_gpio(desc), 0, value);
2042 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2043 _gpio_set_open_drain_value(desc, value);
2044 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2045 _gpio_set_open_source_value(desc, value);
2046 else
2047 chip->set(chip, gpio_chip_hwgpio(desc), value);
2048 }
2049
2050 void gpio_set_value_cansleep(unsigned gpio, int value)
2051 {
2052 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
2053 }
2054 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2055
2056 #ifdef CONFIG_DEBUG_FS
2057
2058 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2059 {
2060 unsigned i;
2061 unsigned gpio = chip->base;
2062 struct gpio_desc *gdesc = &chip->desc[0];
2063 int is_out;
2064
2065 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2066 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2067 continue;
2068
2069 gpiod_get_direction(gdesc);
2070 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2071 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
2072 gpio, gdesc->label,
2073 is_out ? "out" : "in ",
2074 chip->get
2075 ? (chip->get(chip, i) ? "hi" : "lo")
2076 : "? ");
2077 seq_printf(s, "\n");
2078 }
2079 }
2080
2081 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2082 {
2083 unsigned long flags;
2084 struct gpio_chip *chip = NULL;
2085 loff_t index = *pos;
2086
2087 s->private = "";
2088
2089 spin_lock_irqsave(&gpio_lock, flags);
2090 list_for_each_entry(chip, &gpio_chips, list)
2091 if (index-- == 0) {
2092 spin_unlock_irqrestore(&gpio_lock, flags);
2093 return chip;
2094 }
2095 spin_unlock_irqrestore(&gpio_lock, flags);
2096
2097 return NULL;
2098 }
2099
2100 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2101 {
2102 unsigned long flags;
2103 struct gpio_chip *chip = v;
2104 void *ret = NULL;
2105
2106 spin_lock_irqsave(&gpio_lock, flags);
2107 if (list_is_last(&chip->list, &gpio_chips))
2108 ret = NULL;
2109 else
2110 ret = list_entry(chip->list.next, struct gpio_chip, list);
2111 spin_unlock_irqrestore(&gpio_lock, flags);
2112
2113 s->private = "\n";
2114 ++*pos;
2115
2116 return ret;
2117 }
2118
2119 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2120 {
2121 }
2122
2123 static int gpiolib_seq_show(struct seq_file *s, void *v)
2124 {
2125 struct gpio_chip *chip = v;
2126 struct device *dev;
2127
2128 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2129 chip->base, chip->base + chip->ngpio - 1);
2130 dev = chip->dev;
2131 if (dev)
2132 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2133 dev_name(dev));
2134 if (chip->label)
2135 seq_printf(s, ", %s", chip->label);
2136 if (chip->can_sleep)
2137 seq_printf(s, ", can sleep");
2138 seq_printf(s, ":\n");
2139
2140 if (chip->dbg_show)
2141 chip->dbg_show(s, chip);
2142 else
2143 gpiolib_dbg_show(s, chip);
2144
2145 return 0;
2146 }
2147
2148 static const struct seq_operations gpiolib_seq_ops = {
2149 .start = gpiolib_seq_start,
2150 .next = gpiolib_seq_next,
2151 .stop = gpiolib_seq_stop,
2152 .show = gpiolib_seq_show,
2153 };
2154
2155 static int gpiolib_open(struct inode *inode, struct file *file)
2156 {
2157 return seq_open(file, &gpiolib_seq_ops);
2158 }
2159
2160 static const struct file_operations gpiolib_operations = {
2161 .owner = THIS_MODULE,
2162 .open = gpiolib_open,
2163 .read = seq_read,
2164 .llseek = seq_lseek,
2165 .release = seq_release,
2166 };
2167
2168 static int __init gpiolib_debugfs_init(void)
2169 {
2170 /* /sys/kernel/debug/gpio */
2171 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2172 NULL, NULL, &gpiolib_operations);
2173 return 0;
2174 }
2175 subsys_initcall(gpiolib_debugfs_init);
2176
2177 #endif /* DEBUG_FS */