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