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