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