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