of/gpio: stop using device_node data pointer to find gpio_chip
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpio / gpiolib.c
CommitLineData
d2876d08
DB
1#include <linux/kernel.h>
2#include <linux/module.h>
ff77c352 3#include <linux/interrupt.h>
d2876d08
DB
4#include <linux/irq.h>
5#include <linux/spinlock.h>
d8f388d8
DB
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>
ff77c352 11#include <linux/idr.h>
5a0e3ad6 12#include <linux/slab.h>
d2876d08
DB
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 */
44static DEFINE_SPINLOCK(gpio_lock);
45
46struct 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
169b6a7a 52#define FLAG_RESERVED 2
d8f388d8
DB
53#define FLAG_EXPORT 3 /* protected by sysfs_lock */
54#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
ff77c352
DG
55#define FLAG_TRIG_FALL 5 /* trigger on falling edge */
56#define FLAG_TRIG_RISE 6 /* trigger on rising edge */
07697461 57#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
ff77c352
DG
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))
d2876d08
DB
63
64#ifdef CONFIG_DEBUG_FS
65 const char *label;
66#endif
67};
68static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
69
ff77c352
DG
70#ifdef CONFIG_GPIO_SYSFS
71struct poll_desc {
72 struct work_struct work;
73 struct sysfs_dirent *value_sd;
74};
75
76static struct idr pdesc_idr;
77#endif
78
d2876d08
DB
79static 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
35e8bb51
DB
90 * message should motivate switching to explicit requests... so should
91 * the weaker cleanup after faults, compared to gpio_request().
8a0cecff
DB
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.
d2876d08 96 */
35e8bb51 97static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
d2876d08 98{
8a0cecff
DB
99 const struct gpio_chip *chip = desc->chip;
100 const int gpio = chip->base + offset;
35e8bb51 101
8a0cecff
DB
102 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
103 "autorequest GPIO-%d\n", gpio)) {
35e8bb51
DB
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 }
d2876d08 110 desc_set_label(desc, "[auto]");
35e8bb51
DB
111 /* caller must chip->request() w/o spinlock */
112 if (chip->request)
113 return 1;
d2876d08 114 }
35e8bb51 115 return 0;
d2876d08
DB
116}
117
118/* caller holds gpio_lock *OR* gpio is marked as requested */
119static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
120{
121 return gpio_desc[gpio].chip;
122}
123
8d0aab2f
AV
124/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
125static 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--) {
169b6a7a
AV
132 struct gpio_desc *desc = &gpio_desc[i];
133 struct gpio_chip *chip = desc->chip;
8d0aab2f 134
169b6a7a 135 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
8d0aab2f
AV
136 spare++;
137 if (spare == ngpio) {
138 base = i;
139 break;
140 }
141 } else {
142 spare = 0;
169b6a7a
AV
143 if (chip)
144 i -= chip->ngpio - 1;
8d0aab2f
AV
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
169b6a7a
AV
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 */
164int __init gpiochip_reserve(int start, int ngpio)
165{
166 int ret = 0;
167 unsigned long flags;
168 int i;
169
bff5fda9 170 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
169b6a7a
AV
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);
188err:
189 spin_unlock_irqrestore(&gpio_lock, flags);
190
191 return ret;
192}
193
d8f388d8
DB
194#ifdef CONFIG_GPIO_SYSFS
195
196/* lock protects against unexport_gpio() being called while
197 * sysfs files are active.
198 */
199static 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
ff77c352
DG
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"
07697461
JN
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
d8f388d8
DB
220 */
221
222static 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
241static 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
07697461 265static /* const */ DEVICE_ATTR(direction, 0644,
d8f388d8
DB
266 gpio_direction_show, gpio_direction_store);
267
268static 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
07697461 277 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
d8f388d8 278 status = -EIO;
07697461
JN
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 }
d8f388d8
DB
288
289 mutex_unlock(&sysfs_lock);
290 return status;
291}
292
293static 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) {
07697461
JN
311 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
312 value = !value;
d8f388d8
DB
313 gpio_set_value_cansleep(gpio, value != 0);
314 status = size;
315 }
316 }
317
318 mutex_unlock(&sysfs_lock);
319 return status;
320}
321
07697461 322static const DEVICE_ATTR(value, 0644,
d8f388d8
DB
323 gpio_value_show, gpio_value_store);
324
ff77c352
DG
325static 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
333static 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
341static 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))
07697461
JN
371 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
372 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
ff77c352 373 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
07697461
JN
374 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
375 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
ff77c352
DG
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
3ff195b0 402 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
ff77c352
DG
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
418free_sd:
3913fd5e
DC
419 if (pdesc)
420 sysfs_put(pdesc->value_sd);
ff77c352
DG
421free_id:
422 idr_remove(&pdesc_idr, id);
423 desc->flags &= GPIO_FLAGS_MASK;
424free_mem:
425 kfree(pdesc);
426err_out:
427 return ret;
428}
429
430static const struct {
431 const char *name;
432 unsigned long flags;
433} trigger_types[] = {
434 { "none", 0 },
435 { "falling", BIT(FLAG_TRIG_FALL) },
436 { "rising", BIT(FLAG_TRIG_RISE) },
437 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
438};
439
440static ssize_t gpio_edge_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
443 const struct gpio_desc *desc = dev_get_drvdata(dev);
444 ssize_t status;
445
446 mutex_lock(&sysfs_lock);
447
448 if (!test_bit(FLAG_EXPORT, &desc->flags))
449 status = -EIO;
450 else {
451 int i;
452
453 status = 0;
454 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
455 if ((desc->flags & GPIO_TRIGGER_MASK)
456 == trigger_types[i].flags) {
457 status = sprintf(buf, "%s\n",
458 trigger_types[i].name);
459 break;
460 }
461 }
462
463 mutex_unlock(&sysfs_lock);
464 return status;
465}
466
467static ssize_t gpio_edge_store(struct device *dev,
468 struct device_attribute *attr, const char *buf, size_t size)
469{
470 struct gpio_desc *desc = dev_get_drvdata(dev);
471 ssize_t status;
472 int i;
473
474 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
475 if (sysfs_streq(trigger_types[i].name, buf))
476 goto found;
477 return -EINVAL;
478
479found:
480 mutex_lock(&sysfs_lock);
481
482 if (!test_bit(FLAG_EXPORT, &desc->flags))
483 status = -EIO;
484 else {
485 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
486 if (!status)
487 status = size;
488 }
489
490 mutex_unlock(&sysfs_lock);
491
492 return status;
493}
494
495static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
496
07697461
JN
497static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
498 int value)
499{
500 int status = 0;
501
502 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
503 return 0;
504
505 if (value)
506 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
507 else
508 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
509
510 /* reconfigure poll(2) support if enabled on one edge only */
511 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
512 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
513 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
514
515 gpio_setup_irq(desc, dev, 0);
516 status = gpio_setup_irq(desc, dev, trigger_flags);
517 }
518
519 return status;
520}
521
522static ssize_t gpio_active_low_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
525 const struct gpio_desc *desc = dev_get_drvdata(dev);
526 ssize_t status;
527
528 mutex_lock(&sysfs_lock);
529
530 if (!test_bit(FLAG_EXPORT, &desc->flags))
531 status = -EIO;
532 else
533 status = sprintf(buf, "%d\n",
534 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
535
536 mutex_unlock(&sysfs_lock);
537
538 return status;
539}
540
541static ssize_t gpio_active_low_store(struct device *dev,
542 struct device_attribute *attr, const char *buf, size_t size)
543{
544 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 long value;
553
554 status = strict_strtol(buf, 0, &value);
555 if (status == 0)
556 status = sysfs_set_active_low(desc, dev, value != 0);
557 }
558
559 mutex_unlock(&sysfs_lock);
560
561 return status ? : size;
562}
563
564static const DEVICE_ATTR(active_low, 0644,
565 gpio_active_low_show, gpio_active_low_store);
566
d8f388d8 567static const struct attribute *gpio_attrs[] = {
d8f388d8 568 &dev_attr_value.attr,
07697461 569 &dev_attr_active_low.attr,
d8f388d8
DB
570 NULL,
571};
572
573static const struct attribute_group gpio_attr_group = {
574 .attrs = (struct attribute **) gpio_attrs,
575};
576
577/*
578 * /sys/class/gpio/gpiochipN/
579 * /base ... matching gpio_chip.base (N)
580 * /label ... matching gpio_chip.label
581 * /ngpio ... matching gpio_chip.ngpio
582 */
583
584static ssize_t chip_base_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
586{
587 const struct gpio_chip *chip = dev_get_drvdata(dev);
588
589 return sprintf(buf, "%d\n", chip->base);
590}
591static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
592
593static ssize_t chip_label_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
595{
596 const struct gpio_chip *chip = dev_get_drvdata(dev);
597
598 return sprintf(buf, "%s\n", chip->label ? : "");
599}
600static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
601
602static ssize_t chip_ngpio_show(struct device *dev,
603 struct device_attribute *attr, char *buf)
604{
605 const struct gpio_chip *chip = dev_get_drvdata(dev);
606
607 return sprintf(buf, "%u\n", chip->ngpio);
608}
609static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
610
611static const struct attribute *gpiochip_attrs[] = {
612 &dev_attr_base.attr,
613 &dev_attr_label.attr,
614 &dev_attr_ngpio.attr,
615 NULL,
616};
617
618static const struct attribute_group gpiochip_attr_group = {
619 .attrs = (struct attribute **) gpiochip_attrs,
620};
621
622/*
623 * /sys/class/gpio/export ... write-only
624 * integer N ... number of GPIO to export (full access)
625 * /sys/class/gpio/unexport ... write-only
626 * integer N ... number of GPIO to unexport
627 */
28812fe1
AK
628static ssize_t export_store(struct class *class,
629 struct class_attribute *attr,
630 const char *buf, size_t len)
d8f388d8
DB
631{
632 long gpio;
633 int status;
634
635 status = strict_strtol(buf, 0, &gpio);
636 if (status < 0)
637 goto done;
638
639 /* No extra locking here; FLAG_SYSFS just signifies that the
640 * request and export were done by on behalf of userspace, so
641 * they may be undone on its behalf too.
642 */
643
644 status = gpio_request(gpio, "sysfs");
645 if (status < 0)
646 goto done;
647
648 status = gpio_export(gpio, true);
649 if (status < 0)
650 gpio_free(gpio);
651 else
652 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
653
654done:
655 if (status)
656 pr_debug("%s: status %d\n", __func__, status);
657 return status ? : len;
658}
659
28812fe1
AK
660static ssize_t unexport_store(struct class *class,
661 struct class_attribute *attr,
662 const char *buf, size_t len)
d8f388d8
DB
663{
664 long gpio;
665 int status;
666
667 status = strict_strtol(buf, 0, &gpio);
668 if (status < 0)
669 goto done;
670
671 status = -EINVAL;
672
673 /* reject bogus commands (gpio_unexport ignores them) */
674 if (!gpio_is_valid(gpio))
675 goto done;
676
677 /* No extra locking here; FLAG_SYSFS just signifies that the
678 * request and export were done by on behalf of userspace, so
679 * they may be undone on its behalf too.
680 */
681 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
682 status = 0;
683 gpio_free(gpio);
684 }
685done:
686 if (status)
687 pr_debug("%s: status %d\n", __func__, status);
688 return status ? : len;
689}
690
691static struct class_attribute gpio_class_attrs[] = {
692 __ATTR(export, 0200, NULL, export_store),
693 __ATTR(unexport, 0200, NULL, unexport_store),
694 __ATTR_NULL,
695};
696
697static struct class gpio_class = {
698 .name = "gpio",
699 .owner = THIS_MODULE,
700
701 .class_attrs = gpio_class_attrs,
702};
703
704
705/**
706 * gpio_export - export a GPIO through sysfs
707 * @gpio: gpio to make available, already requested
708 * @direction_may_change: true if userspace may change gpio direction
709 * Context: arch_initcall or later
710 *
711 * When drivers want to make a GPIO accessible to userspace after they
712 * have requested it -- perhaps while debugging, or as part of their
713 * public interface -- they may use this routine. If the GPIO can
714 * change direction (some can't) and the caller allows it, userspace
715 * will see "direction" sysfs attribute which may be used to change
716 * the gpio's direction. A "value" attribute will always be provided.
717 *
718 * Returns zero on success, else an error.
719 */
720int gpio_export(unsigned gpio, bool direction_may_change)
721{
722 unsigned long flags;
723 struct gpio_desc *desc;
724 int status = -EINVAL;
62154991 725 const char *ioname = NULL;
d8f388d8
DB
726
727 /* can't export until sysfs is available ... */
728 if (!gpio_class.p) {
729 pr_debug("%s: called too early!\n", __func__);
730 return -ENOENT;
731 }
732
733 if (!gpio_is_valid(gpio))
734 goto done;
735
736 mutex_lock(&sysfs_lock);
737
738 spin_lock_irqsave(&gpio_lock, flags);
739 desc = &gpio_desc[gpio];
740 if (test_bit(FLAG_REQUESTED, &desc->flags)
741 && !test_bit(FLAG_EXPORT, &desc->flags)) {
742 status = 0;
743 if (!desc->chip->direction_input
744 || !desc->chip->direction_output)
745 direction_may_change = false;
746 }
747 spin_unlock_irqrestore(&gpio_lock, flags);
748
926b663c
DS
749 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
750 ioname = desc->chip->names[gpio - desc->chip->base];
751
d8f388d8
DB
752 if (status == 0) {
753 struct device *dev;
754
755 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
7cfe1395 756 desc, ioname ? ioname : "gpio%u", gpio);
d62668e1 757 if (!IS_ERR(dev)) {
07697461 758 status = sysfs_create_group(&dev->kobj,
d8f388d8 759 &gpio_attr_group);
07697461
JN
760
761 if (!status && direction_may_change)
d8f388d8 762 status = device_create_file(dev,
07697461 763 &dev_attr_direction);
ff77c352
DG
764
765 if (!status && gpio_to_irq(gpio) >= 0
766 && (direction_may_change
767 || !test_bit(FLAG_IS_OUT,
768 &desc->flags)))
769 status = device_create_file(dev,
770 &dev_attr_edge);
771
d8f388d8
DB
772 if (status != 0)
773 device_unregister(dev);
774 } else
d62668e1 775 status = PTR_ERR(dev);
d8f388d8
DB
776 if (status == 0)
777 set_bit(FLAG_EXPORT, &desc->flags);
778 }
779
780 mutex_unlock(&sysfs_lock);
781
782done:
783 if (status)
784 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
785
786 return status;
787}
788EXPORT_SYMBOL_GPL(gpio_export);
789
790static int match_export(struct device *dev, void *data)
791{
792 return dev_get_drvdata(dev) == data;
793}
794
a4177ee7
JN
795/**
796 * gpio_export_link - create a sysfs link to an exported GPIO node
797 * @dev: device under which to create symlink
798 * @name: name of the symlink
799 * @gpio: gpio to create symlink to, already exported
800 *
801 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
802 * node. Caller is responsible for unlinking.
803 *
804 * Returns zero on success, else an error.
805 */
806int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
807{
808 struct gpio_desc *desc;
809 int status = -EINVAL;
810
811 if (!gpio_is_valid(gpio))
812 goto done;
813
814 mutex_lock(&sysfs_lock);
815
816 desc = &gpio_desc[gpio];
817
818 if (test_bit(FLAG_EXPORT, &desc->flags)) {
819 struct device *tdev;
820
821 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
822 if (tdev != NULL) {
823 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
824 name);
825 } else {
826 status = -ENODEV;
827 }
828 }
829
830 mutex_unlock(&sysfs_lock);
831
832done:
833 if (status)
834 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
835
836 return status;
837}
838EXPORT_SYMBOL_GPL(gpio_export_link);
839
07697461
JN
840
841/**
842 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
843 * @gpio: gpio to change
844 * @value: non-zero to use active low, i.e. inverted values
845 *
846 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
847 * The GPIO does not have to be exported yet. If poll(2) support has
848 * been enabled for either rising or falling edge, it will be
849 * reconfigured to follow the new polarity.
850 *
851 * Returns zero on success, else an error.
852 */
853int gpio_sysfs_set_active_low(unsigned gpio, int value)
854{
855 struct gpio_desc *desc;
856 struct device *dev = NULL;
857 int status = -EINVAL;
858
859 if (!gpio_is_valid(gpio))
860 goto done;
861
862 mutex_lock(&sysfs_lock);
863
864 desc = &gpio_desc[gpio];
865
866 if (test_bit(FLAG_EXPORT, &desc->flags)) {
07697461
JN
867 dev = class_find_device(&gpio_class, NULL, desc, match_export);
868 if (dev == NULL) {
869 status = -ENODEV;
870 goto unlock;
871 }
872 }
873
874 status = sysfs_set_active_low(desc, dev, value);
875
876unlock:
877 mutex_unlock(&sysfs_lock);
878
879done:
880 if (status)
881 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
882
883 return status;
884}
885EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
886
d8f388d8
DB
887/**
888 * gpio_unexport - reverse effect of gpio_export()
889 * @gpio: gpio to make unavailable
890 *
891 * This is implicit on gpio_free().
892 */
893void gpio_unexport(unsigned gpio)
894{
895 struct gpio_desc *desc;
896 int status = -EINVAL;
897
898 if (!gpio_is_valid(gpio))
899 goto done;
900
901 mutex_lock(&sysfs_lock);
902
903 desc = &gpio_desc[gpio];
926b663c 904
d8f388d8
DB
905 if (test_bit(FLAG_EXPORT, &desc->flags)) {
906 struct device *dev = NULL;
907
908 dev = class_find_device(&gpio_class, NULL, desc, match_export);
909 if (dev) {
ff77c352 910 gpio_setup_irq(desc, dev, 0);
d8f388d8
DB
911 clear_bit(FLAG_EXPORT, &desc->flags);
912 put_device(dev);
913 device_unregister(dev);
914 status = 0;
915 } else
916 status = -ENODEV;
917 }
918
919 mutex_unlock(&sysfs_lock);
920done:
921 if (status)
922 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
923}
924EXPORT_SYMBOL_GPL(gpio_unexport);
925
926static int gpiochip_export(struct gpio_chip *chip)
927{
928 int status;
929 struct device *dev;
930
931 /* Many systems register gpio chips for SOC support very early,
932 * before driver model support is available. In those cases we
933 * export this later, in gpiolib_sysfs_init() ... here we just
934 * verify that _some_ field of gpio_class got initialized.
935 */
936 if (!gpio_class.p)
937 return 0;
938
939 /* use chip->base for the ID; it's already known to be unique */
940 mutex_lock(&sysfs_lock);
941 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
942 "gpiochip%d", chip->base);
d62668e1 943 if (!IS_ERR(dev)) {
d8f388d8
DB
944 status = sysfs_create_group(&dev->kobj,
945 &gpiochip_attr_group);
946 } else
d62668e1 947 status = PTR_ERR(dev);
d8f388d8
DB
948 chip->exported = (status == 0);
949 mutex_unlock(&sysfs_lock);
950
951 if (status) {
952 unsigned long flags;
953 unsigned gpio;
954
955 spin_lock_irqsave(&gpio_lock, flags);
956 gpio = chip->base;
957 while (gpio_desc[gpio].chip == chip)
958 gpio_desc[gpio++].chip = NULL;
959 spin_unlock_irqrestore(&gpio_lock, flags);
960
961 pr_debug("%s: chip %s status %d\n", __func__,
962 chip->label, status);
963 }
964
965 return status;
966}
967
968static void gpiochip_unexport(struct gpio_chip *chip)
969{
970 int status;
971 struct device *dev;
972
973 mutex_lock(&sysfs_lock);
974 dev = class_find_device(&gpio_class, NULL, chip, match_export);
975 if (dev) {
976 put_device(dev);
977 device_unregister(dev);
978 chip->exported = 0;
979 status = 0;
980 } else
981 status = -ENODEV;
982 mutex_unlock(&sysfs_lock);
983
984 if (status)
985 pr_debug("%s: chip %s status %d\n", __func__,
986 chip->label, status);
987}
988
989static int __init gpiolib_sysfs_init(void)
990{
991 int status;
992 unsigned long flags;
993 unsigned gpio;
994
ff77c352
DG
995 idr_init(&pdesc_idr);
996
d8f388d8
DB
997 status = class_register(&gpio_class);
998 if (status < 0)
999 return status;
1000
1001 /* Scan and register the gpio_chips which registered very
1002 * early (e.g. before the class_register above was called).
1003 *
1004 * We run before arch_initcall() so chip->dev nodes can have
1005 * registered, and so arch_initcall() can always gpio_export().
1006 */
1007 spin_lock_irqsave(&gpio_lock, flags);
1008 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
1009 struct gpio_chip *chip;
1010
1011 chip = gpio_desc[gpio].chip;
1012 if (!chip || chip->exported)
1013 continue;
1014
1015 spin_unlock_irqrestore(&gpio_lock, flags);
1016 status = gpiochip_export(chip);
1017 spin_lock_irqsave(&gpio_lock, flags);
1018 }
1019 spin_unlock_irqrestore(&gpio_lock, flags);
1020
1021
1022 return status;
1023}
1024postcore_initcall(gpiolib_sysfs_init);
1025
1026#else
1027static inline int gpiochip_export(struct gpio_chip *chip)
1028{
1029 return 0;
1030}
1031
1032static inline void gpiochip_unexport(struct gpio_chip *chip)
1033{
1034}
1035
1036#endif /* CONFIG_GPIO_SYSFS */
1037
d2876d08
DB
1038/**
1039 * gpiochip_add() - register a gpio_chip
1040 * @chip: the chip to register, with chip->base initialized
1041 * Context: potentially before irqs or kmalloc will work
1042 *
1043 * Returns a negative errno if the chip can't be registered, such as
1044 * because the chip->base is invalid or already associated with a
1045 * different chip. Otherwise it returns zero as a success code.
8d0aab2f 1046 *
d8f388d8
DB
1047 * When gpiochip_add() is called very early during boot, so that GPIOs
1048 * can be freely used, the chip->dev device must be registered before
1049 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1050 * for GPIOs will fail rudely.
1051 *
8d0aab2f
AV
1052 * If chip->base is negative, this requests dynamic assignment of
1053 * a range of valid GPIOs.
d2876d08
DB
1054 */
1055int gpiochip_add(struct gpio_chip *chip)
1056{
1057 unsigned long flags;
1058 int status = 0;
1059 unsigned id;
8d0aab2f 1060 int base = chip->base;
d2876d08 1061
bff5fda9 1062 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
8d0aab2f 1063 && base >= 0) {
d2876d08
DB
1064 status = -EINVAL;
1065 goto fail;
1066 }
1067
1068 spin_lock_irqsave(&gpio_lock, flags);
1069
8d0aab2f
AV
1070 if (base < 0) {
1071 base = gpiochip_find_base(chip->ngpio);
1072 if (base < 0) {
1073 status = base;
d8f388d8 1074 goto unlock;
8d0aab2f
AV
1075 }
1076 chip->base = base;
1077 }
1078
d2876d08 1079 /* these GPIO numbers must not be managed by another gpio_chip */
8d0aab2f 1080 for (id = base; id < base + chip->ngpio; id++) {
d2876d08
DB
1081 if (gpio_desc[id].chip != NULL) {
1082 status = -EBUSY;
1083 break;
1084 }
1085 }
1086 if (status == 0) {
8d0aab2f 1087 for (id = base; id < base + chip->ngpio; id++) {
d2876d08 1088 gpio_desc[id].chip = chip;
d8f388d8
DB
1089
1090 /* REVISIT: most hardware initializes GPIOs as
1091 * inputs (often with pullups enabled) so power
1092 * usage is minimized. Linux code should set the
1093 * gpio direction first thing; but until it does,
1094 * we may expose the wrong direction in sysfs.
1095 */
1096 gpio_desc[id].flags = !chip->direction_input
1097 ? (1 << FLAG_IS_OUT)
1098 : 0;
d2876d08
DB
1099 }
1100 }
1101
d8f388d8 1102unlock:
d2876d08 1103 spin_unlock_irqrestore(&gpio_lock, flags);
cedb1881
AV
1104
1105 if (status)
1106 goto fail;
1107
1108 status = gpiochip_export(chip);
1109 if (status)
1110 goto fail;
1111
1112 return 0;
d2876d08
DB
1113fail:
1114 /* failures here can mean systems won't boot... */
cedb1881
AV
1115 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1116 chip->base, chip->base + chip->ngpio - 1,
1117 chip->label ? : "generic");
d2876d08
DB
1118 return status;
1119}
1120EXPORT_SYMBOL_GPL(gpiochip_add);
1121
1122/**
1123 * gpiochip_remove() - unregister a gpio_chip
1124 * @chip: the chip to unregister
1125 *
1126 * A gpio_chip with any GPIOs still requested may not be removed.
1127 */
1128int gpiochip_remove(struct gpio_chip *chip)
1129{
1130 unsigned long flags;
1131 int status = 0;
1132 unsigned id;
1133
1134 spin_lock_irqsave(&gpio_lock, flags);
1135
1136 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1137 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1138 status = -EBUSY;
1139 break;
1140 }
1141 }
1142 if (status == 0) {
1143 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1144 gpio_desc[id].chip = NULL;
1145 }
1146
1147 spin_unlock_irqrestore(&gpio_lock, flags);
d8f388d8
DB
1148
1149 if (status == 0)
1150 gpiochip_unexport(chip);
1151
d2876d08
DB
1152 return status;
1153}
1154EXPORT_SYMBOL_GPL(gpiochip_remove);
1155
594fa265
GL
1156/**
1157 * gpiochip_find() - iterator for locating a specific gpio_chip
1158 * @data: data to pass to match function
1159 * @callback: Callback function to check gpio_chip
1160 *
1161 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1162 * determined by a user supplied @match callback. The callback should return
1163 * 0 if the device doesn't match and non-zero if it does. If the callback is
1164 * non-zero, this function will return to the caller and not iterate over any
1165 * more gpio_chips.
1166 */
1167struct gpio_chip *gpiochip_find(void *data,
1168 int (*match)(struct gpio_chip *chip, void *data))
1169{
1170 struct gpio_chip *chip = NULL;
1171 unsigned long flags;
1172 int i;
1173
1174 spin_lock_irqsave(&gpio_lock, flags);
1175 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1176 if (!gpio_desc[i].chip)
1177 continue;
1178
1179 if (match(gpio_desc[i].chip, data)) {
1180 chip = gpio_desc[i].chip;
1181 break;
1182 }
1183 }
1184 spin_unlock_irqrestore(&gpio_lock, flags);
1185
1186 return chip;
1187}
d2876d08
DB
1188
1189/* These "optional" allocation calls help prevent drivers from stomping
1190 * on each other, and help provide better diagnostics in debugfs.
1191 * They're called even less than the "set direction" calls.
1192 */
1193int gpio_request(unsigned gpio, const char *label)
1194{
1195 struct gpio_desc *desc;
35e8bb51 1196 struct gpio_chip *chip;
d2876d08
DB
1197 int status = -EINVAL;
1198 unsigned long flags;
1199
1200 spin_lock_irqsave(&gpio_lock, flags);
1201
e6de1808 1202 if (!gpio_is_valid(gpio))
d2876d08
DB
1203 goto done;
1204 desc = &gpio_desc[gpio];
35e8bb51
DB
1205 chip = desc->chip;
1206 if (chip == NULL)
d2876d08
DB
1207 goto done;
1208
35e8bb51 1209 if (!try_module_get(chip->owner))
438d8908
GL
1210 goto done;
1211
d2876d08 1212 /* NOTE: gpio_request() can be called in early boot,
35e8bb51 1213 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
d2876d08
DB
1214 */
1215
1216 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1217 desc_set_label(desc, label ? : "?");
1218 status = 0;
438d8908 1219 } else {
d2876d08 1220 status = -EBUSY;
35e8bb51 1221 module_put(chip->owner);
7460db56 1222 goto done;
35e8bb51
DB
1223 }
1224
1225 if (chip->request) {
1226 /* chip->request may sleep */
1227 spin_unlock_irqrestore(&gpio_lock, flags);
1228 status = chip->request(chip, gpio - chip->base);
1229 spin_lock_irqsave(&gpio_lock, flags);
1230
1231 if (status < 0) {
1232 desc_set_label(desc, NULL);
1233 module_put(chip->owner);
1234 clear_bit(FLAG_REQUESTED, &desc->flags);
1235 }
438d8908 1236 }
d2876d08
DB
1237
1238done:
1239 if (status)
1240 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1241 gpio, label ? : "?", status);
1242 spin_unlock_irqrestore(&gpio_lock, flags);
1243 return status;
1244}
1245EXPORT_SYMBOL_GPL(gpio_request);
1246
1247void gpio_free(unsigned gpio)
1248{
1249 unsigned long flags;
1250 struct gpio_desc *desc;
35e8bb51 1251 struct gpio_chip *chip;
d2876d08 1252
3d599d1c
UKK
1253 might_sleep();
1254
e6de1808 1255 if (!gpio_is_valid(gpio)) {
d2876d08
DB
1256 WARN_ON(extra_checks);
1257 return;
1258 }
1259
d8f388d8
DB
1260 gpio_unexport(gpio);
1261
d2876d08
DB
1262 spin_lock_irqsave(&gpio_lock, flags);
1263
1264 desc = &gpio_desc[gpio];
35e8bb51
DB
1265 chip = desc->chip;
1266 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1267 if (chip->free) {
1268 spin_unlock_irqrestore(&gpio_lock, flags);
1269 might_sleep_if(extra_checks && chip->can_sleep);
1270 chip->free(chip, gpio - chip->base);
1271 spin_lock_irqsave(&gpio_lock, flags);
1272 }
d2876d08 1273 desc_set_label(desc, NULL);
438d8908 1274 module_put(desc->chip->owner);
07697461 1275 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
35e8bb51 1276 clear_bit(FLAG_REQUESTED, &desc->flags);
438d8908 1277 } else
d2876d08
DB
1278 WARN_ON(extra_checks);
1279
1280 spin_unlock_irqrestore(&gpio_lock, flags);
1281}
1282EXPORT_SYMBOL_GPL(gpio_free);
1283
3e45f1d1
EM
1284/**
1285 * gpio_request_one - request a single GPIO with initial configuration
1286 * @gpio: the GPIO number
1287 * @flags: GPIO configuration as specified by GPIOF_*
1288 * @label: a literal description string of this GPIO
1289 */
1290int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1291{
1292 int err;
1293
1294 err = gpio_request(gpio, label);
1295 if (err)
1296 return err;
1297
1298 if (flags & GPIOF_DIR_IN)
1299 err = gpio_direction_input(gpio);
1300 else
1301 err = gpio_direction_output(gpio,
1302 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1303
1304 return err;
1305}
1306EXPORT_SYMBOL_GPL(gpio_request_one);
1307
1308/**
1309 * gpio_request_array - request multiple GPIOs in a single call
1310 * @array: array of the 'struct gpio'
1311 * @num: how many GPIOs in the array
1312 */
1313int gpio_request_array(struct gpio *array, size_t num)
1314{
1315 int i, err;
1316
1317 for (i = 0; i < num; i++, array++) {
1318 err = gpio_request_one(array->gpio, array->flags, array->label);
1319 if (err)
1320 goto err_free;
1321 }
1322 return 0;
1323
1324err_free:
1325 while (i--)
1326 gpio_free((--array)->gpio);
1327 return err;
1328}
1329EXPORT_SYMBOL_GPL(gpio_request_array);
1330
1331/**
1332 * gpio_free_array - release multiple GPIOs in a single call
1333 * @array: array of the 'struct gpio'
1334 * @num: how many GPIOs in the array
1335 */
1336void gpio_free_array(struct gpio *array, size_t num)
1337{
1338 while (num--)
1339 gpio_free((array++)->gpio);
1340}
1341EXPORT_SYMBOL_GPL(gpio_free_array);
d2876d08
DB
1342
1343/**
1344 * gpiochip_is_requested - return string iff signal was requested
1345 * @chip: controller managing the signal
1346 * @offset: of signal within controller's 0..(ngpio - 1) range
1347 *
1348 * Returns NULL if the GPIO is not currently requested, else a string.
1349 * If debugfs support is enabled, the string returned is the label passed
1350 * to gpio_request(); otherwise it is a meaningless constant.
1351 *
1352 * This function is for use by GPIO controller drivers. The label can
1353 * help with diagnostics, and knowing that the signal is used as a GPIO
1354 * can help avoid accidentally multiplexing it to another controller.
1355 */
1356const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1357{
1358 unsigned gpio = chip->base + offset;
1359
e6de1808 1360 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
d2876d08
DB
1361 return NULL;
1362 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1363 return NULL;
1364#ifdef CONFIG_DEBUG_FS
1365 return gpio_desc[gpio].label;
1366#else
1367 return "?";
1368#endif
1369}
1370EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1371
1372
1373/* Drivers MUST set GPIO direction before making get/set calls. In
1374 * some cases this is done in early boot, before IRQs are enabled.
1375 *
1376 * As a rule these aren't called more than once (except for drivers
1377 * using the open-drain emulation idiom) so these are natural places
1378 * to accumulate extra debugging checks. Note that we can't (yet)
1379 * rely on gpio_request() having been called beforehand.
1380 */
1381
1382int gpio_direction_input(unsigned gpio)
1383{
1384 unsigned long flags;
1385 struct gpio_chip *chip;
1386 struct gpio_desc *desc = &gpio_desc[gpio];
1387 int status = -EINVAL;
1388
1389 spin_lock_irqsave(&gpio_lock, flags);
1390
e6de1808 1391 if (!gpio_is_valid(gpio))
d2876d08
DB
1392 goto fail;
1393 chip = desc->chip;
1394 if (!chip || !chip->get || !chip->direction_input)
1395 goto fail;
1396 gpio -= chip->base;
1397 if (gpio >= chip->ngpio)
1398 goto fail;
35e8bb51
DB
1399 status = gpio_ensure_requested(desc, gpio);
1400 if (status < 0)
1401 goto fail;
d2876d08
DB
1402
1403 /* now we know the gpio is valid and chip won't vanish */
1404
1405 spin_unlock_irqrestore(&gpio_lock, flags);
1406
1407 might_sleep_if(extra_checks && chip->can_sleep);
1408
35e8bb51
DB
1409 if (status) {
1410 status = chip->request(chip, gpio);
1411 if (status < 0) {
1412 pr_debug("GPIO-%d: chip request fail, %d\n",
1413 chip->base + gpio, status);
1414 /* and it's not available to anyone else ...
1415 * gpio_request() is the fully clean solution.
1416 */
1417 goto lose;
1418 }
1419 }
1420
d2876d08
DB
1421 status = chip->direction_input(chip, gpio);
1422 if (status == 0)
1423 clear_bit(FLAG_IS_OUT, &desc->flags);
35e8bb51 1424lose:
d2876d08
DB
1425 return status;
1426fail:
1427 spin_unlock_irqrestore(&gpio_lock, flags);
1428 if (status)
1429 pr_debug("%s: gpio-%d status %d\n",
145980a0 1430 __func__, gpio, status);
d2876d08
DB
1431 return status;
1432}
1433EXPORT_SYMBOL_GPL(gpio_direction_input);
1434
1435int gpio_direction_output(unsigned gpio, int value)
1436{
1437 unsigned long flags;
1438 struct gpio_chip *chip;
1439 struct gpio_desc *desc = &gpio_desc[gpio];
1440 int status = -EINVAL;
1441
1442 spin_lock_irqsave(&gpio_lock, flags);
1443
e6de1808 1444 if (!gpio_is_valid(gpio))
d2876d08
DB
1445 goto fail;
1446 chip = desc->chip;
1447 if (!chip || !chip->set || !chip->direction_output)
1448 goto fail;
1449 gpio -= chip->base;
1450 if (gpio >= chip->ngpio)
1451 goto fail;
35e8bb51
DB
1452 status = gpio_ensure_requested(desc, gpio);
1453 if (status < 0)
1454 goto fail;
d2876d08
DB
1455
1456 /* now we know the gpio is valid and chip won't vanish */
1457
1458 spin_unlock_irqrestore(&gpio_lock, flags);
1459
1460 might_sleep_if(extra_checks && chip->can_sleep);
1461
35e8bb51
DB
1462 if (status) {
1463 status = chip->request(chip, gpio);
1464 if (status < 0) {
1465 pr_debug("GPIO-%d: chip request fail, %d\n",
1466 chip->base + gpio, status);
1467 /* and it's not available to anyone else ...
1468 * gpio_request() is the fully clean solution.
1469 */
1470 goto lose;
1471 }
1472 }
1473
d2876d08
DB
1474 status = chip->direction_output(chip, gpio, value);
1475 if (status == 0)
1476 set_bit(FLAG_IS_OUT, &desc->flags);
35e8bb51 1477lose:
d2876d08
DB
1478 return status;
1479fail:
1480 spin_unlock_irqrestore(&gpio_lock, flags);
1481 if (status)
1482 pr_debug("%s: gpio-%d status %d\n",
145980a0 1483 __func__, gpio, status);
d2876d08
DB
1484 return status;
1485}
1486EXPORT_SYMBOL_GPL(gpio_direction_output);
1487
c4b5be98
FB
1488/**
1489 * gpio_set_debounce - sets @debounce time for a @gpio
1490 * @gpio: the gpio to set debounce time
1491 * @debounce: debounce time is microseconds
1492 */
1493int gpio_set_debounce(unsigned gpio, unsigned debounce)
1494{
1495 unsigned long flags;
1496 struct gpio_chip *chip;
1497 struct gpio_desc *desc = &gpio_desc[gpio];
1498 int status = -EINVAL;
1499
1500 spin_lock_irqsave(&gpio_lock, flags);
1501
1502 if (!gpio_is_valid(gpio))
1503 goto fail;
1504 chip = desc->chip;
1505 if (!chip || !chip->set || !chip->set_debounce)
1506 goto fail;
1507 gpio -= chip->base;
1508 if (gpio >= chip->ngpio)
1509 goto fail;
1510 status = gpio_ensure_requested(desc, gpio);
1511 if (status < 0)
1512 goto fail;
1513
1514 /* now we know the gpio is valid and chip won't vanish */
1515
1516 spin_unlock_irqrestore(&gpio_lock, flags);
1517
1518 might_sleep_if(extra_checks && chip->can_sleep);
1519
1520 return chip->set_debounce(chip, gpio, debounce);
1521
1522fail:
1523 spin_unlock_irqrestore(&gpio_lock, flags);
1524 if (status)
1525 pr_debug("%s: gpio-%d status %d\n",
1526 __func__, gpio, status);
1527
1528 return status;
1529}
1530EXPORT_SYMBOL_GPL(gpio_set_debounce);
d2876d08
DB
1531
1532/* I/O calls are only valid after configuration completed; the relevant
1533 * "is this a valid GPIO" error checks should already have been done.
1534 *
1535 * "Get" operations are often inlinable as reading a pin value register,
1536 * and masking the relevant bit in that register.
1537 *
1538 * When "set" operations are inlinable, they involve writing that mask to
1539 * one register to set a low value, or a different register to set it high.
1540 * Otherwise locking is needed, so there may be little value to inlining.
1541 *
1542 *------------------------------------------------------------------------
1543 *
1544 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1545 * have requested the GPIO. That can include implicit requesting by
1546 * a direction setting call. Marking a gpio as requested locks its chip
1547 * in memory, guaranteeing that these table lookups need no more locking
1548 * and that gpiochip_remove() will fail.
1549 *
1550 * REVISIT when debugging, consider adding some instrumentation to ensure
1551 * that the GPIO was actually requested.
1552 */
1553
1554/**
1555 * __gpio_get_value() - return a gpio's value
1556 * @gpio: gpio whose value will be returned
1557 * Context: any
1558 *
1559 * This is used directly or indirectly to implement gpio_get_value().
1560 * It returns the zero or nonzero value provided by the associated
1561 * gpio_chip.get() method; or zero if no such method is provided.
1562 */
1563int __gpio_get_value(unsigned gpio)
1564{
1565 struct gpio_chip *chip;
1566
1567 chip = gpio_to_chip(gpio);
1568 WARN_ON(extra_checks && chip->can_sleep);
1569 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1570}
1571EXPORT_SYMBOL_GPL(__gpio_get_value);
1572
1573/**
1574 * __gpio_set_value() - assign a gpio's value
1575 * @gpio: gpio whose value will be assigned
1576 * @value: value to assign
1577 * Context: any
1578 *
1579 * This is used directly or indirectly to implement gpio_set_value().
1580 * It invokes the associated gpio_chip.set() method.
1581 */
1582void __gpio_set_value(unsigned gpio, int value)
1583{
1584 struct gpio_chip *chip;
1585
1586 chip = gpio_to_chip(gpio);
1587 WARN_ON(extra_checks && chip->can_sleep);
1588 chip->set(chip, gpio - chip->base, value);
1589}
1590EXPORT_SYMBOL_GPL(__gpio_set_value);
1591
1592/**
1593 * __gpio_cansleep() - report whether gpio value access will sleep
1594 * @gpio: gpio in question
1595 * Context: any
1596 *
1597 * This is used directly or indirectly to implement gpio_cansleep(). It
1598 * returns nonzero if access reading or writing the GPIO value can sleep.
1599 */
1600int __gpio_cansleep(unsigned gpio)
1601{
1602 struct gpio_chip *chip;
1603
1604 /* only call this on GPIOs that are valid! */
1605 chip = gpio_to_chip(gpio);
1606
1607 return chip->can_sleep;
1608}
1609EXPORT_SYMBOL_GPL(__gpio_cansleep);
1610
0f6d504e
DB
1611/**
1612 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1613 * @gpio: gpio whose IRQ will be returned (already requested)
1614 * Context: any
1615 *
1616 * This is used directly or indirectly to implement gpio_to_irq().
1617 * It returns the number of the IRQ signaled by this (input) GPIO,
1618 * or a negative errno.
1619 */
1620int __gpio_to_irq(unsigned gpio)
1621{
1622 struct gpio_chip *chip;
1623
1624 chip = gpio_to_chip(gpio);
1625 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1626}
1627EXPORT_SYMBOL_GPL(__gpio_to_irq);
1628
d2876d08
DB
1629
1630
1631/* There's no value in making it easy to inline GPIO calls that may sleep.
1632 * Common examples include ones connected to I2C or SPI chips.
1633 */
1634
1635int gpio_get_value_cansleep(unsigned gpio)
1636{
1637 struct gpio_chip *chip;
1638
1639 might_sleep_if(extra_checks);
1640 chip = gpio_to_chip(gpio);
978ccaa8 1641 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
d2876d08
DB
1642}
1643EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1644
1645void gpio_set_value_cansleep(unsigned gpio, int value)
1646{
1647 struct gpio_chip *chip;
1648
1649 might_sleep_if(extra_checks);
1650 chip = gpio_to_chip(gpio);
1651 chip->set(chip, gpio - chip->base, value);
1652}
1653EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1654
1655
1656#ifdef CONFIG_DEBUG_FS
1657
d2876d08
DB
1658static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1659{
1660 unsigned i;
1661 unsigned gpio = chip->base;
1662 struct gpio_desc *gdesc = &gpio_desc[gpio];
1663 int is_out;
1664
1665 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1666 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1667 continue;
1668
1669 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
6e8ba729 1670 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
d2876d08
DB
1671 gpio, gdesc->label,
1672 is_out ? "out" : "in ",
1673 chip->get
1674 ? (chip->get(chip, i) ? "hi" : "lo")
1675 : "? ");
1676
1677 if (!is_out) {
1678 int irq = gpio_to_irq(gpio);
08678b08 1679 struct irq_desc *desc = irq_to_desc(irq);
d2876d08
DB
1680
1681 /* This races with request_irq(), set_irq_type(),
1682 * and set_irq_wake() ... but those are "rare".
1683 *
1684 * More significantly, trigger type flags aren't
1685 * currently maintained by genirq.
1686 */
1687 if (irq >= 0 && desc->action) {
1688 char *trigger;
1689
1690 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1691 case IRQ_TYPE_NONE:
1692 trigger = "(default)";
1693 break;
1694 case IRQ_TYPE_EDGE_FALLING:
1695 trigger = "edge-falling";
1696 break;
1697 case IRQ_TYPE_EDGE_RISING:
1698 trigger = "edge-rising";
1699 break;
1700 case IRQ_TYPE_EDGE_BOTH:
1701 trigger = "edge-both";
1702 break;
1703 case IRQ_TYPE_LEVEL_HIGH:
1704 trigger = "level-high";
1705 break;
1706 case IRQ_TYPE_LEVEL_LOW:
1707 trigger = "level-low";
1708 break;
1709 default:
1710 trigger = "?trigger?";
1711 break;
1712 }
1713
1714 seq_printf(s, " irq-%d %s%s",
1715 irq, trigger,
1716 (desc->status & IRQ_WAKEUP)
1717 ? " wakeup" : "");
1718 }
1719 }
1720
1721 seq_printf(s, "\n");
1722 }
1723}
1724
1725static int gpiolib_show(struct seq_file *s, void *unused)
1726{
1727 struct gpio_chip *chip = NULL;
1728 unsigned gpio;
1729 int started = 0;
1730
1731 /* REVISIT this isn't locked against gpio_chip removal ... */
1732
e6de1808 1733 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
d8f388d8
DB
1734 struct device *dev;
1735
d2876d08
DB
1736 if (chip == gpio_desc[gpio].chip)
1737 continue;
1738 chip = gpio_desc[gpio].chip;
1739 if (!chip)
1740 continue;
1741
d8f388d8 1742 seq_printf(s, "%sGPIOs %d-%d",
d2876d08 1743 started ? "\n" : "",
d8f388d8
DB
1744 chip->base, chip->base + chip->ngpio - 1);
1745 dev = chip->dev;
1746 if (dev)
1747 seq_printf(s, ", %s/%s",
1748 dev->bus ? dev->bus->name : "no-bus",
14ab309d 1749 dev_name(dev));
d8f388d8
DB
1750 if (chip->label)
1751 seq_printf(s, ", %s", chip->label);
1752 if (chip->can_sleep)
1753 seq_printf(s, ", can sleep");
1754 seq_printf(s, ":\n");
1755
d2876d08
DB
1756 started = 1;
1757 if (chip->dbg_show)
1758 chip->dbg_show(s, chip);
1759 else
1760 gpiolib_dbg_show(s, chip);
1761 }
1762 return 0;
1763}
1764
1765static int gpiolib_open(struct inode *inode, struct file *file)
1766{
1767 return single_open(file, gpiolib_show, NULL);
1768}
1769
828c0950 1770static const struct file_operations gpiolib_operations = {
d2876d08
DB
1771 .open = gpiolib_open,
1772 .read = seq_read,
1773 .llseek = seq_lseek,
1774 .release = single_release,
1775};
1776
1777static int __init gpiolib_debugfs_init(void)
1778{
1779 /* /sys/kernel/debug/gpio */
1780 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1781 NULL, NULL, &gpiolib_operations);
1782 return 0;
1783}
1784subsys_initcall(gpiolib_debugfs_init);
1785
1786#endif /* DEBUG_FS */