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