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