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