blob: c27e52b56d66a90e6b39c257814e7c5b2c15986b [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
David Brownelld8f388d2008-07-25 01:46:07 -07006#include <linux/device.h>
7#include <linux/err.h>
8#include <linux/debugfs.h>
9#include <linux/seq_file.h>
10#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060011#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070012#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080014
15
16/* Optional implementation infrastructure for GPIO interfaces.
17 *
18 * Platforms may want to use this if they tend to use very many GPIOs
19 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
20 *
21 * When kernel footprint or instruction count is an issue, simpler
22 * implementations may be preferred. The GPIO programming interface
23 * allows for inlining speed-critical get/set operations for common
24 * cases, so that access to SOC-integrated GPIOs can sometimes cost
25 * only an instruction or two per bit.
26 */
27
28
29/* When debugging, extend minimal trust to callers and platform code.
30 * Also emit diagnostic messages that may help initial bringup, when
31 * board setup or driver bugs are most common.
32 *
33 * Otherwise, minimize overhead in what may be bitbanging codepaths.
34 */
35#ifdef DEBUG
36#define extra_checks 1
37#else
38#define extra_checks 0
39#endif
40
41/* gpio_lock prevents conflicts during gpio_desc[] table updates.
42 * While any GPIO is requested, its gpio_chip is not removable;
43 * each GPIO's "requested" flag serves as a lock and refcount.
44 */
45static DEFINE_SPINLOCK(gpio_lock);
46
47struct gpio_desc {
48 struct gpio_chip *chip;
49 unsigned long flags;
50/* flag symbols are bit numbers */
51#define FLAG_REQUESTED 0
52#define FLAG_IS_OUT 1
Anton Vorontsov169b6a72008-04-28 02:14:47 -070053#define FLAG_RESERVED 2
David Brownelld8f388d2008-07-25 01:46:07 -070054#define FLAG_EXPORT 3 /* protected by sysfs_lock */
55#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070056#define FLAG_TRIG_FALL 5 /* trigger on falling edge */
57#define FLAG_TRIG_RISE 6 /* trigger on rising edge */
Jani Nikula07697462009-12-15 16:46:20 -080058#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070059
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070060#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070061
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070062#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070063#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080064
65#ifdef CONFIG_DEBUG_FS
66 const char *label;
67#endif
68};
69static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
70
Daniel Glöcknerff77c352009-09-22 16:46:38 -070071#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070072static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070073#endif
74
David Brownelld2876d02008-02-04 22:28:20 -080075static inline void desc_set_label(struct gpio_desc *d, const char *label)
76{
77#ifdef CONFIG_DEBUG_FS
78 d->label = label;
79#endif
80}
81
82/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
83 * when setting direction, and otherwise illegal. Until board setup code
84 * and drivers use explicit requests everywhere (which won't happen when
85 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -070086 * message should motivate switching to explicit requests... so should
87 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -070088 *
89 * NOTE: the autorequest mechanism is going away; at this point it's
90 * only "legal" in the sense that (old) code using it won't break yet,
91 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -080092 */
David Brownell35e8bb52008-10-15 22:03:16 -070093static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
David Brownelld2876d02008-02-04 22:28:20 -080094{
David Brownell8a0cecf2009-04-02 16:57:06 -070095 const struct gpio_chip *chip = desc->chip;
96 const int gpio = chip->base + offset;
David Brownell35e8bb52008-10-15 22:03:16 -070097
David Brownell8a0cecf2009-04-02 16:57:06 -070098 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
99 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700100 if (!try_module_get(chip->owner)) {
101 pr_err("GPIO-%d: module can't be gotten \n", gpio);
102 clear_bit(FLAG_REQUESTED, &desc->flags);
103 /* lose */
104 return -EIO;
105 }
David Brownelld2876d02008-02-04 22:28:20 -0800106 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700107 /* caller must chip->request() w/o spinlock */
108 if (chip->request)
109 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800110 }
David Brownell35e8bb52008-10-15 22:03:16 -0700111 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800112}
113
114/* caller holds gpio_lock *OR* gpio is marked as requested */
115static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
116{
117 return gpio_desc[gpio].chip;
118}
119
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700120/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
121static int gpiochip_find_base(int ngpio)
122{
123 int i;
124 int spare = 0;
125 int base = -ENOSPC;
126
127 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700128 struct gpio_desc *desc = &gpio_desc[i];
129 struct gpio_chip *chip = desc->chip;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700130
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700131 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700132 spare++;
133 if (spare == ngpio) {
134 base = i;
135 break;
136 }
137 } else {
138 spare = 0;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700139 if (chip)
140 i -= chip->ngpio - 1;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700141 }
142 }
143
144 if (gpio_is_valid(base))
145 pr_debug("%s: found new base at %d\n", __func__, base);
146 return base;
147}
148
David Brownelld2876d02008-02-04 22:28:20 -0800149/**
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700150 * gpiochip_reserve() - reserve range of gpios to use with platform code only
151 * @start: starting gpio number
152 * @ngpio: number of gpios to reserve
153 * Context: platform init, potentially before irqs or kmalloc will work
154 *
155 * Returns a negative errno if any gpio within the range is already reserved
156 * or registered, else returns zero as a success code. Use this function
157 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
158 * for example because its driver support is not yet loaded.
159 */
160int __init gpiochip_reserve(int start, int ngpio)
161{
162 int ret = 0;
163 unsigned long flags;
164 int i;
165
Trent Piephobff5fda2008-05-23 13:04:44 -0700166 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700167 return -EINVAL;
168
169 spin_lock_irqsave(&gpio_lock, flags);
170
171 for (i = start; i < start + ngpio; i++) {
172 struct gpio_desc *desc = &gpio_desc[i];
173
174 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
175 ret = -EBUSY;
176 goto err;
177 }
178
179 set_bit(FLAG_RESERVED, &desc->flags);
180 }
181
182 pr_debug("%s: reserved gpios from %d to %d\n",
183 __func__, start, start + ngpio - 1);
184err:
185 spin_unlock_irqrestore(&gpio_lock, flags);
186
187 return ret;
188}
189
David Brownelld8f388d2008-07-25 01:46:07 -0700190#ifdef CONFIG_GPIO_SYSFS
191
192/* lock protects against unexport_gpio() being called while
193 * sysfs files are active.
194 */
195static DEFINE_MUTEX(sysfs_lock);
196
197/*
198 * /sys/class/gpio/gpioN... only for GPIOs that are exported
199 * /direction
200 * * MAY BE OMITTED if kernel won't allow direction changes
201 * * is read/write as "in" or "out"
202 * * may also be written as "high" or "low", initializing
203 * output value as specified ("out" implies "low")
204 * /value
205 * * always readable, subject to hardware behavior
206 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700207 * /edge
208 * * configures behavior of poll(2) on /value
209 * * available only if pin can generate IRQs on input
210 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800211 * /active_low
212 * * configures polarity of /value
213 * * is read/write as zero/nonzero
214 * * also affects existing and subsequent "falling" and "rising"
215 * /edge configuration
David Brownelld8f388d2008-07-25 01:46:07 -0700216 */
217
218static ssize_t gpio_direction_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
220{
221 const struct gpio_desc *desc = dev_get_drvdata(dev);
222 ssize_t status;
223
224 mutex_lock(&sysfs_lock);
225
226 if (!test_bit(FLAG_EXPORT, &desc->flags))
227 status = -EIO;
228 else
229 status = sprintf(buf, "%s\n",
230 test_bit(FLAG_IS_OUT, &desc->flags)
231 ? "out" : "in");
232
233 mutex_unlock(&sysfs_lock);
234 return status;
235}
236
237static ssize_t gpio_direction_store(struct device *dev,
238 struct device_attribute *attr, const char *buf, size_t size)
239{
240 const struct gpio_desc *desc = dev_get_drvdata(dev);
241 unsigned gpio = desc - gpio_desc;
242 ssize_t status;
243
244 mutex_lock(&sysfs_lock);
245
246 if (!test_bit(FLAG_EXPORT, &desc->flags))
247 status = -EIO;
248 else if (sysfs_streq(buf, "high"))
249 status = gpio_direction_output(gpio, 1);
250 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
251 status = gpio_direction_output(gpio, 0);
252 else if (sysfs_streq(buf, "in"))
253 status = gpio_direction_input(gpio);
254 else
255 status = -EINVAL;
256
257 mutex_unlock(&sysfs_lock);
258 return status ? : size;
259}
260
Jani Nikula07697462009-12-15 16:46:20 -0800261static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700262 gpio_direction_show, gpio_direction_store);
263
264static ssize_t gpio_value_show(struct device *dev,
265 struct device_attribute *attr, char *buf)
266{
267 const struct gpio_desc *desc = dev_get_drvdata(dev);
268 unsigned gpio = desc - gpio_desc;
269 ssize_t status;
270
271 mutex_lock(&sysfs_lock);
272
Jani Nikula07697462009-12-15 16:46:20 -0800273 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700274 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800275 } else {
276 int value;
277
278 value = !!gpio_get_value_cansleep(gpio);
279 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
280 value = !value;
281
282 status = sprintf(buf, "%d\n", value);
283 }
David Brownelld8f388d2008-07-25 01:46:07 -0700284
285 mutex_unlock(&sysfs_lock);
286 return status;
287}
288
289static ssize_t gpio_value_store(struct device *dev,
290 struct device_attribute *attr, const char *buf, size_t size)
291{
292 const struct gpio_desc *desc = dev_get_drvdata(dev);
293 unsigned gpio = desc - gpio_desc;
294 ssize_t status;
295
296 mutex_lock(&sysfs_lock);
297
298 if (!test_bit(FLAG_EXPORT, &desc->flags))
299 status = -EIO;
300 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
301 status = -EPERM;
302 else {
303 long value;
304
305 status = strict_strtol(buf, 0, &value);
306 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800307 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
308 value = !value;
David Brownelld8f388d2008-07-25 01:46:07 -0700309 gpio_set_value_cansleep(gpio, value != 0);
310 status = size;
311 }
312 }
313
314 mutex_unlock(&sysfs_lock);
315 return status;
316}
317
Jani Nikula07697462009-12-15 16:46:20 -0800318static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700319 gpio_value_show, gpio_value_store);
320
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700321static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
322{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700323 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700324
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700325 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700326 return IRQ_HANDLED;
327}
328
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700329static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
330 unsigned long gpio_flags)
331{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700332 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700333 unsigned long irq_flags;
334 int ret, irq, id;
335
336 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
337 return 0;
338
339 irq = gpio_to_irq(desc - gpio_desc);
340 if (irq < 0)
341 return -EIO;
342
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700343 id = desc->flags >> ID_SHIFT;
344 value_sd = idr_find(&dirent_idr, id);
345 if (value_sd)
346 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700347
348 desc->flags &= ~GPIO_TRIGGER_MASK;
349
350 if (!gpio_flags) {
351 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700352 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700353 }
354
355 irq_flags = IRQF_SHARED;
356 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800357 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
358 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700359 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800360 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
361 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700362
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700363 if (!value_sd) {
364 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
365 if (!value_sd) {
366 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700367 goto err_out;
368 }
369
370 do {
371 ret = -ENOMEM;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700372 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
373 ret = idr_get_new_above(&dirent_idr, value_sd,
374 1, &id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700375 } while (ret == -EAGAIN);
376
377 if (ret)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700378 goto free_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700379
380 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700381 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700382
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700383 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700384 ret = -ERANGE;
385 goto free_id;
386 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700387 }
388
Daniel Gl?ckner364fadb2010-08-10 18:02:26 -0700389 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700390 "gpiolib", value_sd);
Daniel Gl?ckner364fadb2010-08-10 18:02:26 -0700391 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700392 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700393
394 desc->flags |= gpio_flags;
395 return 0;
396
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700397free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700398 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700399 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700400free_sd:
401 if (value_sd)
402 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700403err_out:
404 return ret;
405}
406
407static const struct {
408 const char *name;
409 unsigned long flags;
410} trigger_types[] = {
411 { "none", 0 },
412 { "falling", BIT(FLAG_TRIG_FALL) },
413 { "rising", BIT(FLAG_TRIG_RISE) },
414 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
415};
416
417static ssize_t gpio_edge_show(struct device *dev,
418 struct device_attribute *attr, char *buf)
419{
420 const struct gpio_desc *desc = dev_get_drvdata(dev);
421 ssize_t status;
422
423 mutex_lock(&sysfs_lock);
424
425 if (!test_bit(FLAG_EXPORT, &desc->flags))
426 status = -EIO;
427 else {
428 int i;
429
430 status = 0;
431 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
432 if ((desc->flags & GPIO_TRIGGER_MASK)
433 == trigger_types[i].flags) {
434 status = sprintf(buf, "%s\n",
435 trigger_types[i].name);
436 break;
437 }
438 }
439
440 mutex_unlock(&sysfs_lock);
441 return status;
442}
443
444static ssize_t gpio_edge_store(struct device *dev,
445 struct device_attribute *attr, const char *buf, size_t size)
446{
447 struct gpio_desc *desc = dev_get_drvdata(dev);
448 ssize_t status;
449 int i;
450
451 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
452 if (sysfs_streq(trigger_types[i].name, buf))
453 goto found;
454 return -EINVAL;
455
456found:
457 mutex_lock(&sysfs_lock);
458
459 if (!test_bit(FLAG_EXPORT, &desc->flags))
460 status = -EIO;
461 else {
462 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
463 if (!status)
464 status = size;
465 }
466
467 mutex_unlock(&sysfs_lock);
468
469 return status;
470}
471
472static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
473
Jani Nikula07697462009-12-15 16:46:20 -0800474static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
475 int value)
476{
477 int status = 0;
478
479 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
480 return 0;
481
482 if (value)
483 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
484 else
485 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
486
487 /* reconfigure poll(2) support if enabled on one edge only */
488 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
489 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
490 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
491
492 gpio_setup_irq(desc, dev, 0);
493 status = gpio_setup_irq(desc, dev, trigger_flags);
494 }
495
496 return status;
497}
498
499static ssize_t gpio_active_low_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
502 const struct gpio_desc *desc = dev_get_drvdata(dev);
503 ssize_t status;
504
505 mutex_lock(&sysfs_lock);
506
507 if (!test_bit(FLAG_EXPORT, &desc->flags))
508 status = -EIO;
509 else
510 status = sprintf(buf, "%d\n",
511 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
512
513 mutex_unlock(&sysfs_lock);
514
515 return status;
516}
517
518static ssize_t gpio_active_low_store(struct device *dev,
519 struct device_attribute *attr, const char *buf, size_t size)
520{
521 struct gpio_desc *desc = dev_get_drvdata(dev);
522 ssize_t status;
523
524 mutex_lock(&sysfs_lock);
525
526 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
527 status = -EIO;
528 } else {
529 long value;
530
531 status = strict_strtol(buf, 0, &value);
532 if (status == 0)
533 status = sysfs_set_active_low(desc, dev, value != 0);
534 }
535
536 mutex_unlock(&sysfs_lock);
537
538 return status ? : size;
539}
540
541static const DEVICE_ATTR(active_low, 0644,
542 gpio_active_low_show, gpio_active_low_store);
543
David Brownelld8f388d2008-07-25 01:46:07 -0700544static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d2008-07-25 01:46:07 -0700545 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800546 &dev_attr_active_low.attr,
David Brownelld8f388d2008-07-25 01:46:07 -0700547 NULL,
548};
549
550static const struct attribute_group gpio_attr_group = {
551 .attrs = (struct attribute **) gpio_attrs,
552};
553
554/*
555 * /sys/class/gpio/gpiochipN/
556 * /base ... matching gpio_chip.base (N)
557 * /label ... matching gpio_chip.label
558 * /ngpio ... matching gpio_chip.ngpio
559 */
560
561static ssize_t chip_base_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
564 const struct gpio_chip *chip = dev_get_drvdata(dev);
565
566 return sprintf(buf, "%d\n", chip->base);
567}
568static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
569
570static ssize_t chip_label_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 const struct gpio_chip *chip = dev_get_drvdata(dev);
574
575 return sprintf(buf, "%s\n", chip->label ? : "");
576}
577static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
578
579static ssize_t chip_ngpio_show(struct device *dev,
580 struct device_attribute *attr, char *buf)
581{
582 const struct gpio_chip *chip = dev_get_drvdata(dev);
583
584 return sprintf(buf, "%u\n", chip->ngpio);
585}
586static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
587
588static const struct attribute *gpiochip_attrs[] = {
589 &dev_attr_base.attr,
590 &dev_attr_label.attr,
591 &dev_attr_ngpio.attr,
592 NULL,
593};
594
595static const struct attribute_group gpiochip_attr_group = {
596 .attrs = (struct attribute **) gpiochip_attrs,
597};
598
599/*
600 * /sys/class/gpio/export ... write-only
601 * integer N ... number of GPIO to export (full access)
602 * /sys/class/gpio/unexport ... write-only
603 * integer N ... number of GPIO to unexport
604 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100605static ssize_t export_store(struct class *class,
606 struct class_attribute *attr,
607 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700608{
609 long gpio;
610 int status;
611
612 status = strict_strtol(buf, 0, &gpio);
613 if (status < 0)
614 goto done;
615
616 /* No extra locking here; FLAG_SYSFS just signifies that the
617 * request and export were done by on behalf of userspace, so
618 * they may be undone on its behalf too.
619 */
620
621 status = gpio_request(gpio, "sysfs");
622 if (status < 0)
623 goto done;
624
625 status = gpio_export(gpio, true);
626 if (status < 0)
627 gpio_free(gpio);
628 else
629 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
630
631done:
632 if (status)
633 pr_debug("%s: status %d\n", __func__, status);
634 return status ? : len;
635}
636
Andi Kleen28812fe2010-01-05 12:48:07 +0100637static ssize_t unexport_store(struct class *class,
638 struct class_attribute *attr,
639 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700640{
641 long gpio;
642 int status;
643
644 status = strict_strtol(buf, 0, &gpio);
645 if (status < 0)
646 goto done;
647
648 status = -EINVAL;
649
650 /* reject bogus commands (gpio_unexport ignores them) */
651 if (!gpio_is_valid(gpio))
652 goto done;
653
654 /* No extra locking here; FLAG_SYSFS just signifies that the
655 * request and export were done by on behalf of userspace, so
656 * they may be undone on its behalf too.
657 */
658 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
659 status = 0;
660 gpio_free(gpio);
661 }
662done:
663 if (status)
664 pr_debug("%s: status %d\n", __func__, status);
665 return status ? : len;
666}
667
668static struct class_attribute gpio_class_attrs[] = {
669 __ATTR(export, 0200, NULL, export_store),
670 __ATTR(unexport, 0200, NULL, unexport_store),
671 __ATTR_NULL,
672};
673
674static struct class gpio_class = {
675 .name = "gpio",
676 .owner = THIS_MODULE,
677
678 .class_attrs = gpio_class_attrs,
679};
680
681
682/**
683 * gpio_export - export a GPIO through sysfs
684 * @gpio: gpio to make available, already requested
685 * @direction_may_change: true if userspace may change gpio direction
686 * Context: arch_initcall or later
687 *
688 * When drivers want to make a GPIO accessible to userspace after they
689 * have requested it -- perhaps while debugging, or as part of their
690 * public interface -- they may use this routine. If the GPIO can
691 * change direction (some can't) and the caller allows it, userspace
692 * will see "direction" sysfs attribute which may be used to change
693 * the gpio's direction. A "value" attribute will always be provided.
694 *
695 * Returns zero on success, else an error.
696 */
697int gpio_export(unsigned gpio, bool direction_may_change)
698{
699 unsigned long flags;
700 struct gpio_desc *desc;
701 int status = -EINVAL;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700702 const char *ioname = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -0700703
704 /* can't export until sysfs is available ... */
705 if (!gpio_class.p) {
706 pr_debug("%s: called too early!\n", __func__);
707 return -ENOENT;
708 }
709
710 if (!gpio_is_valid(gpio))
711 goto done;
712
713 mutex_lock(&sysfs_lock);
714
715 spin_lock_irqsave(&gpio_lock, flags);
716 desc = &gpio_desc[gpio];
717 if (test_bit(FLAG_REQUESTED, &desc->flags)
718 && !test_bit(FLAG_EXPORT, &desc->flags)) {
719 status = 0;
720 if (!desc->chip->direction_input
721 || !desc->chip->direction_output)
722 direction_may_change = false;
723 }
724 spin_unlock_irqrestore(&gpio_lock, flags);
725
Daniel Silverstone926b6632009-04-02 16:57:05 -0700726 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
727 ioname = desc->chip->names[gpio - desc->chip->base];
728
David Brownelld8f388d2008-07-25 01:46:07 -0700729 if (status == 0) {
730 struct device *dev;
731
732 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Uwe Kleine-König7cfe1392010-05-26 14:42:18 -0700733 desc, ioname ? ioname : "gpio%u", gpio);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800734 if (!IS_ERR(dev)) {
Jani Nikula07697462009-12-15 16:46:20 -0800735 status = sysfs_create_group(&dev->kobj,
David Brownelld8f388d2008-07-25 01:46:07 -0700736 &gpio_attr_group);
Jani Nikula07697462009-12-15 16:46:20 -0800737
738 if (!status && direction_may_change)
David Brownelld8f388d2008-07-25 01:46:07 -0700739 status = device_create_file(dev,
Jani Nikula07697462009-12-15 16:46:20 -0800740 &dev_attr_direction);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700741
742 if (!status && gpio_to_irq(gpio) >= 0
743 && (direction_may_change
744 || !test_bit(FLAG_IS_OUT,
745 &desc->flags)))
746 status = device_create_file(dev,
747 &dev_attr_edge);
748
David Brownelld8f388d2008-07-25 01:46:07 -0700749 if (status != 0)
750 device_unregister(dev);
751 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800752 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700753 if (status == 0)
754 set_bit(FLAG_EXPORT, &desc->flags);
755 }
756
757 mutex_unlock(&sysfs_lock);
758
759done:
760 if (status)
761 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
762
763 return status;
764}
765EXPORT_SYMBOL_GPL(gpio_export);
766
767static int match_export(struct device *dev, void *data)
768{
769 return dev_get_drvdata(dev) == data;
770}
771
772/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700773 * gpio_export_link - create a sysfs link to an exported GPIO node
774 * @dev: device under which to create symlink
775 * @name: name of the symlink
776 * @gpio: gpio to create symlink to, already exported
777 *
778 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
779 * node. Caller is responsible for unlinking.
780 *
781 * Returns zero on success, else an error.
782 */
783int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
784{
785 struct gpio_desc *desc;
786 int status = -EINVAL;
787
788 if (!gpio_is_valid(gpio))
789 goto done;
790
791 mutex_lock(&sysfs_lock);
792
793 desc = &gpio_desc[gpio];
794
795 if (test_bit(FLAG_EXPORT, &desc->flags)) {
796 struct device *tdev;
797
798 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
799 if (tdev != NULL) {
800 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
801 name);
802 } else {
803 status = -ENODEV;
804 }
805 }
806
807 mutex_unlock(&sysfs_lock);
808
809done:
810 if (status)
811 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
812
813 return status;
814}
815EXPORT_SYMBOL_GPL(gpio_export_link);
816
Jani Nikula07697462009-12-15 16:46:20 -0800817
818/**
819 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
820 * @gpio: gpio to change
821 * @value: non-zero to use active low, i.e. inverted values
822 *
823 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
824 * The GPIO does not have to be exported yet. If poll(2) support has
825 * been enabled for either rising or falling edge, it will be
826 * reconfigured to follow the new polarity.
827 *
828 * Returns zero on success, else an error.
829 */
830int gpio_sysfs_set_active_low(unsigned gpio, int value)
831{
832 struct gpio_desc *desc;
833 struct device *dev = NULL;
834 int status = -EINVAL;
835
836 if (!gpio_is_valid(gpio))
837 goto done;
838
839 mutex_lock(&sysfs_lock);
840
841 desc = &gpio_desc[gpio];
842
843 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800844 dev = class_find_device(&gpio_class, NULL, desc, match_export);
845 if (dev == NULL) {
846 status = -ENODEV;
847 goto unlock;
848 }
849 }
850
851 status = sysfs_set_active_low(desc, dev, value);
852
853unlock:
854 mutex_unlock(&sysfs_lock);
855
856done:
857 if (status)
858 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
859
860 return status;
861}
862EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
863
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700864/**
David Brownelld8f388d2008-07-25 01:46:07 -0700865 * gpio_unexport - reverse effect of gpio_export()
866 * @gpio: gpio to make unavailable
867 *
868 * This is implicit on gpio_free().
869 */
870void gpio_unexport(unsigned gpio)
871{
872 struct gpio_desc *desc;
Jon Povey6a99ad42010-07-27 13:18:06 -0700873 int status = 0;
David Brownelld8f388d2008-07-25 01:46:07 -0700874
Jon Povey6a99ad42010-07-27 13:18:06 -0700875 if (!gpio_is_valid(gpio)) {
876 status = -EINVAL;
David Brownelld8f388d2008-07-25 01:46:07 -0700877 goto done;
Jon Povey6a99ad42010-07-27 13:18:06 -0700878 }
David Brownelld8f388d2008-07-25 01:46:07 -0700879
880 mutex_lock(&sysfs_lock);
881
882 desc = &gpio_desc[gpio];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700883
David Brownelld8f388d2008-07-25 01:46:07 -0700884 if (test_bit(FLAG_EXPORT, &desc->flags)) {
885 struct device *dev = NULL;
886
887 dev = class_find_device(&gpio_class, NULL, desc, match_export);
888 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700889 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700890 clear_bit(FLAG_EXPORT, &desc->flags);
891 put_device(dev);
892 device_unregister(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700893 } else
894 status = -ENODEV;
895 }
896
897 mutex_unlock(&sysfs_lock);
898done:
899 if (status)
900 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
901}
902EXPORT_SYMBOL_GPL(gpio_unexport);
903
904static int gpiochip_export(struct gpio_chip *chip)
905{
906 int status;
907 struct device *dev;
908
909 /* Many systems register gpio chips for SOC support very early,
910 * before driver model support is available. In those cases we
911 * export this later, in gpiolib_sysfs_init() ... here we just
912 * verify that _some_ field of gpio_class got initialized.
913 */
914 if (!gpio_class.p)
915 return 0;
916
917 /* use chip->base for the ID; it's already known to be unique */
918 mutex_lock(&sysfs_lock);
919 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
920 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800921 if (!IS_ERR(dev)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700922 status = sysfs_create_group(&dev->kobj,
923 &gpiochip_attr_group);
924 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800925 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700926 chip->exported = (status == 0);
927 mutex_unlock(&sysfs_lock);
928
929 if (status) {
930 unsigned long flags;
931 unsigned gpio;
932
933 spin_lock_irqsave(&gpio_lock, flags);
934 gpio = chip->base;
935 while (gpio_desc[gpio].chip == chip)
936 gpio_desc[gpio++].chip = NULL;
937 spin_unlock_irqrestore(&gpio_lock, flags);
938
939 pr_debug("%s: chip %s status %d\n", __func__,
940 chip->label, status);
941 }
942
943 return status;
944}
945
946static void gpiochip_unexport(struct gpio_chip *chip)
947{
948 int status;
949 struct device *dev;
950
951 mutex_lock(&sysfs_lock);
952 dev = class_find_device(&gpio_class, NULL, chip, match_export);
953 if (dev) {
954 put_device(dev);
955 device_unregister(dev);
956 chip->exported = 0;
957 status = 0;
958 } else
959 status = -ENODEV;
960 mutex_unlock(&sysfs_lock);
961
962 if (status)
963 pr_debug("%s: chip %s status %d\n", __func__,
964 chip->label, status);
965}
966
967static int __init gpiolib_sysfs_init(void)
968{
969 int status;
970 unsigned long flags;
971 unsigned gpio;
972
973 status = class_register(&gpio_class);
974 if (status < 0)
975 return status;
976
977 /* Scan and register the gpio_chips which registered very
978 * early (e.g. before the class_register above was called).
979 *
980 * We run before arch_initcall() so chip->dev nodes can have
981 * registered, and so arch_initcall() can always gpio_export().
982 */
983 spin_lock_irqsave(&gpio_lock, flags);
984 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
985 struct gpio_chip *chip;
986
987 chip = gpio_desc[gpio].chip;
988 if (!chip || chip->exported)
989 continue;
990
991 spin_unlock_irqrestore(&gpio_lock, flags);
992 status = gpiochip_export(chip);
993 spin_lock_irqsave(&gpio_lock, flags);
994 }
995 spin_unlock_irqrestore(&gpio_lock, flags);
996
997
998 return status;
999}
1000postcore_initcall(gpiolib_sysfs_init);
1001
1002#else
1003static inline int gpiochip_export(struct gpio_chip *chip)
1004{
1005 return 0;
1006}
1007
1008static inline void gpiochip_unexport(struct gpio_chip *chip)
1009{
1010}
1011
1012#endif /* CONFIG_GPIO_SYSFS */
1013
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001014/**
David Brownelld2876d02008-02-04 22:28:20 -08001015 * gpiochip_add() - register a gpio_chip
1016 * @chip: the chip to register, with chip->base initialized
1017 * Context: potentially before irqs or kmalloc will work
1018 *
1019 * Returns a negative errno if the chip can't be registered, such as
1020 * because the chip->base is invalid or already associated with a
1021 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001022 *
David Brownelld8f388d2008-07-25 01:46:07 -07001023 * When gpiochip_add() is called very early during boot, so that GPIOs
1024 * can be freely used, the chip->dev device must be registered before
1025 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1026 * for GPIOs will fail rudely.
1027 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001028 * If chip->base is negative, this requests dynamic assignment of
1029 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001030 */
1031int gpiochip_add(struct gpio_chip *chip)
1032{
1033 unsigned long flags;
1034 int status = 0;
1035 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001036 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001037
Trent Piephobff5fda2008-05-23 13:04:44 -07001038 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001039 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001040 status = -EINVAL;
1041 goto fail;
1042 }
1043
1044 spin_lock_irqsave(&gpio_lock, flags);
1045
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001046 if (base < 0) {
1047 base = gpiochip_find_base(chip->ngpio);
1048 if (base < 0) {
1049 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -07001050 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001051 }
1052 chip->base = base;
1053 }
1054
David Brownelld2876d02008-02-04 22:28:20 -08001055 /* these GPIO numbers must not be managed by another gpio_chip */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001056 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001057 if (gpio_desc[id].chip != NULL) {
1058 status = -EBUSY;
1059 break;
1060 }
1061 }
1062 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001063 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001064 gpio_desc[id].chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001065
1066 /* REVISIT: most hardware initializes GPIOs as
1067 * inputs (often with pullups enabled) so power
1068 * usage is minimized. Linux code should set the
1069 * gpio direction first thing; but until it does,
1070 * we may expose the wrong direction in sysfs.
1071 */
1072 gpio_desc[id].flags = !chip->direction_input
1073 ? (1 << FLAG_IS_OUT)
1074 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001075 }
1076 }
1077
Anton Vorontsov391c9702010-06-08 07:48:17 -06001078 of_gpiochip_add(chip);
1079
David Brownelld8f388d2008-07-25 01:46:07 -07001080unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001081 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001082
1083 if (status)
1084 goto fail;
1085
1086 status = gpiochip_export(chip);
1087 if (status)
1088 goto fail;
1089
1090 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001091fail:
1092 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001093 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1094 chip->base, chip->base + chip->ngpio - 1,
1095 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001096 return status;
1097}
1098EXPORT_SYMBOL_GPL(gpiochip_add);
1099
1100/**
1101 * gpiochip_remove() - unregister a gpio_chip
1102 * @chip: the chip to unregister
1103 *
1104 * A gpio_chip with any GPIOs still requested may not be removed.
1105 */
1106int gpiochip_remove(struct gpio_chip *chip)
1107{
1108 unsigned long flags;
1109 int status = 0;
1110 unsigned id;
1111
1112 spin_lock_irqsave(&gpio_lock, flags);
1113
Anton Vorontsov391c9702010-06-08 07:48:17 -06001114 of_gpiochip_remove(chip);
1115
David Brownelld2876d02008-02-04 22:28:20 -08001116 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1117 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1118 status = -EBUSY;
1119 break;
1120 }
1121 }
1122 if (status == 0) {
1123 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1124 gpio_desc[id].chip = NULL;
1125 }
1126
1127 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001128
1129 if (status == 0)
1130 gpiochip_unexport(chip);
1131
David Brownelld2876d02008-02-04 22:28:20 -08001132 return status;
1133}
1134EXPORT_SYMBOL_GPL(gpiochip_remove);
1135
Grant Likely594fa262010-06-08 07:48:16 -06001136/**
1137 * gpiochip_find() - iterator for locating a specific gpio_chip
1138 * @data: data to pass to match function
1139 * @callback: Callback function to check gpio_chip
1140 *
1141 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1142 * determined by a user supplied @match callback. The callback should return
1143 * 0 if the device doesn't match and non-zero if it does. If the callback is
1144 * non-zero, this function will return to the caller and not iterate over any
1145 * more gpio_chips.
1146 */
1147struct gpio_chip *gpiochip_find(void *data,
1148 int (*match)(struct gpio_chip *chip, void *data))
1149{
1150 struct gpio_chip *chip = NULL;
1151 unsigned long flags;
1152 int i;
1153
1154 spin_lock_irqsave(&gpio_lock, flags);
1155 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1156 if (!gpio_desc[i].chip)
1157 continue;
1158
1159 if (match(gpio_desc[i].chip, data)) {
1160 chip = gpio_desc[i].chip;
1161 break;
1162 }
1163 }
1164 spin_unlock_irqrestore(&gpio_lock, flags);
1165
1166 return chip;
1167}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001168EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001169
1170/* These "optional" allocation calls help prevent drivers from stomping
1171 * on each other, and help provide better diagnostics in debugfs.
1172 * They're called even less than the "set direction" calls.
1173 */
1174int gpio_request(unsigned gpio, const char *label)
1175{
1176 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001177 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001178 int status = -EINVAL;
1179 unsigned long flags;
1180
1181 spin_lock_irqsave(&gpio_lock, flags);
1182
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001183 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001184 goto done;
1185 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001186 chip = desc->chip;
1187 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001188 goto done;
1189
David Brownell35e8bb52008-10-15 22:03:16 -07001190 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001191 goto done;
1192
David Brownelld2876d02008-02-04 22:28:20 -08001193 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001194 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001195 */
1196
1197 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1198 desc_set_label(desc, label ? : "?");
1199 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001200 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001201 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001202 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001203 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001204 }
1205
1206 if (chip->request) {
1207 /* chip->request may sleep */
1208 spin_unlock_irqrestore(&gpio_lock, flags);
1209 status = chip->request(chip, gpio - chip->base);
1210 spin_lock_irqsave(&gpio_lock, flags);
1211
1212 if (status < 0) {
1213 desc_set_label(desc, NULL);
1214 module_put(chip->owner);
1215 clear_bit(FLAG_REQUESTED, &desc->flags);
1216 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001217 }
David Brownelld2876d02008-02-04 22:28:20 -08001218
1219done:
1220 if (status)
1221 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1222 gpio, label ? : "?", status);
1223 spin_unlock_irqrestore(&gpio_lock, flags);
1224 return status;
1225}
1226EXPORT_SYMBOL_GPL(gpio_request);
1227
1228void gpio_free(unsigned gpio)
1229{
1230 unsigned long flags;
1231 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001232 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001233
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001234 might_sleep();
1235
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001236 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -08001237 WARN_ON(extra_checks);
1238 return;
1239 }
1240
David Brownelld8f388d2008-07-25 01:46:07 -07001241 gpio_unexport(gpio);
1242
David Brownelld2876d02008-02-04 22:28:20 -08001243 spin_lock_irqsave(&gpio_lock, flags);
1244
1245 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001246 chip = desc->chip;
1247 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1248 if (chip->free) {
1249 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001250 might_sleep_if(chip->can_sleep);
David Brownell35e8bb52008-10-15 22:03:16 -07001251 chip->free(chip, gpio - chip->base);
1252 spin_lock_irqsave(&gpio_lock, flags);
1253 }
David Brownelld2876d02008-02-04 22:28:20 -08001254 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001255 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001256 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001257 clear_bit(FLAG_REQUESTED, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001258 } else
David Brownelld2876d02008-02-04 22:28:20 -08001259 WARN_ON(extra_checks);
1260
1261 spin_unlock_irqrestore(&gpio_lock, flags);
1262}
1263EXPORT_SYMBOL_GPL(gpio_free);
1264
Eric Miao3e45f1d2010-03-05 13:44:35 -08001265/**
1266 * gpio_request_one - request a single GPIO with initial configuration
1267 * @gpio: the GPIO number
1268 * @flags: GPIO configuration as specified by GPIOF_*
1269 * @label: a literal description string of this GPIO
1270 */
1271int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1272{
1273 int err;
1274
1275 err = gpio_request(gpio, label);
1276 if (err)
1277 return err;
1278
1279 if (flags & GPIOF_DIR_IN)
1280 err = gpio_direction_input(gpio);
1281 else
1282 err = gpio_direction_output(gpio,
1283 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1284
Aaro Koskinene2548112010-12-21 17:24:22 -08001285 if (err)
1286 gpio_free(gpio);
1287
Eric Miao3e45f1d2010-03-05 13:44:35 -08001288 return err;
1289}
1290EXPORT_SYMBOL_GPL(gpio_request_one);
1291
1292/**
1293 * gpio_request_array - request multiple GPIOs in a single call
1294 * @array: array of the 'struct gpio'
1295 * @num: how many GPIOs in the array
1296 */
1297int gpio_request_array(struct gpio *array, size_t num)
1298{
1299 int i, err;
1300
1301 for (i = 0; i < num; i++, array++) {
1302 err = gpio_request_one(array->gpio, array->flags, array->label);
1303 if (err)
1304 goto err_free;
1305 }
1306 return 0;
1307
1308err_free:
1309 while (i--)
1310 gpio_free((--array)->gpio);
1311 return err;
1312}
1313EXPORT_SYMBOL_GPL(gpio_request_array);
1314
1315/**
1316 * gpio_free_array - release multiple GPIOs in a single call
1317 * @array: array of the 'struct gpio'
1318 * @num: how many GPIOs in the array
1319 */
1320void gpio_free_array(struct gpio *array, size_t num)
1321{
1322 while (num--)
1323 gpio_free((array++)->gpio);
1324}
1325EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001326
1327/**
1328 * gpiochip_is_requested - return string iff signal was requested
1329 * @chip: controller managing the signal
1330 * @offset: of signal within controller's 0..(ngpio - 1) range
1331 *
1332 * Returns NULL if the GPIO is not currently requested, else a string.
1333 * If debugfs support is enabled, the string returned is the label passed
1334 * to gpio_request(); otherwise it is a meaningless constant.
1335 *
1336 * This function is for use by GPIO controller drivers. The label can
1337 * help with diagnostics, and knowing that the signal is used as a GPIO
1338 * can help avoid accidentally multiplexing it to another controller.
1339 */
1340const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1341{
1342 unsigned gpio = chip->base + offset;
1343
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001344 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001345 return NULL;
1346 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1347 return NULL;
1348#ifdef CONFIG_DEBUG_FS
1349 return gpio_desc[gpio].label;
1350#else
1351 return "?";
1352#endif
1353}
1354EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1355
1356
1357/* Drivers MUST set GPIO direction before making get/set calls. In
1358 * some cases this is done in early boot, before IRQs are enabled.
1359 *
1360 * As a rule these aren't called more than once (except for drivers
1361 * using the open-drain emulation idiom) so these are natural places
1362 * to accumulate extra debugging checks. Note that we can't (yet)
1363 * rely on gpio_request() having been called beforehand.
1364 */
1365
1366int gpio_direction_input(unsigned gpio)
1367{
1368 unsigned long flags;
1369 struct gpio_chip *chip;
1370 struct gpio_desc *desc = &gpio_desc[gpio];
1371 int status = -EINVAL;
1372
1373 spin_lock_irqsave(&gpio_lock, flags);
1374
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001375 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001376 goto fail;
1377 chip = desc->chip;
1378 if (!chip || !chip->get || !chip->direction_input)
1379 goto fail;
1380 gpio -= chip->base;
1381 if (gpio >= chip->ngpio)
1382 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001383 status = gpio_ensure_requested(desc, gpio);
1384 if (status < 0)
1385 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001386
1387 /* now we know the gpio is valid and chip won't vanish */
1388
1389 spin_unlock_irqrestore(&gpio_lock, flags);
1390
David Brownell9c4ba942010-08-10 18:02:24 -07001391 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001392
David Brownell35e8bb52008-10-15 22:03:16 -07001393 if (status) {
1394 status = chip->request(chip, gpio);
1395 if (status < 0) {
1396 pr_debug("GPIO-%d: chip request fail, %d\n",
1397 chip->base + gpio, status);
1398 /* and it's not available to anyone else ...
1399 * gpio_request() is the fully clean solution.
1400 */
1401 goto lose;
1402 }
1403 }
1404
David Brownelld2876d02008-02-04 22:28:20 -08001405 status = chip->direction_input(chip, gpio);
1406 if (status == 0)
1407 clear_bit(FLAG_IS_OUT, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001408lose:
David Brownelld2876d02008-02-04 22:28:20 -08001409 return status;
1410fail:
1411 spin_unlock_irqrestore(&gpio_lock, flags);
1412 if (status)
1413 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001414 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001415 return status;
1416}
1417EXPORT_SYMBOL_GPL(gpio_direction_input);
1418
1419int gpio_direction_output(unsigned gpio, int value)
1420{
1421 unsigned long flags;
1422 struct gpio_chip *chip;
1423 struct gpio_desc *desc = &gpio_desc[gpio];
1424 int status = -EINVAL;
1425
1426 spin_lock_irqsave(&gpio_lock, flags);
1427
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001428 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001429 goto fail;
1430 chip = desc->chip;
1431 if (!chip || !chip->set || !chip->direction_output)
1432 goto fail;
1433 gpio -= chip->base;
1434 if (gpio >= chip->ngpio)
1435 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001436 status = gpio_ensure_requested(desc, gpio);
1437 if (status < 0)
1438 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001439
1440 /* now we know the gpio is valid and chip won't vanish */
1441
1442 spin_unlock_irqrestore(&gpio_lock, flags);
1443
David Brownell9c4ba942010-08-10 18:02:24 -07001444 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001445
David Brownell35e8bb52008-10-15 22:03:16 -07001446 if (status) {
1447 status = chip->request(chip, gpio);
1448 if (status < 0) {
1449 pr_debug("GPIO-%d: chip request fail, %d\n",
1450 chip->base + gpio, status);
1451 /* and it's not available to anyone else ...
1452 * gpio_request() is the fully clean solution.
1453 */
1454 goto lose;
1455 }
1456 }
1457
David Brownelld2876d02008-02-04 22:28:20 -08001458 status = chip->direction_output(chip, gpio, value);
1459 if (status == 0)
1460 set_bit(FLAG_IS_OUT, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001461lose:
David Brownelld2876d02008-02-04 22:28:20 -08001462 return status;
1463fail:
1464 spin_unlock_irqrestore(&gpio_lock, flags);
1465 if (status)
1466 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001467 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001468 return status;
1469}
1470EXPORT_SYMBOL_GPL(gpio_direction_output);
1471
Felipe Balbic4b5be92010-05-26 14:42:23 -07001472/**
1473 * gpio_set_debounce - sets @debounce time for a @gpio
1474 * @gpio: the gpio to set debounce time
1475 * @debounce: debounce time is microseconds
1476 */
1477int gpio_set_debounce(unsigned gpio, unsigned debounce)
1478{
1479 unsigned long flags;
1480 struct gpio_chip *chip;
1481 struct gpio_desc *desc = &gpio_desc[gpio];
1482 int status = -EINVAL;
1483
1484 spin_lock_irqsave(&gpio_lock, flags);
1485
1486 if (!gpio_is_valid(gpio))
1487 goto fail;
1488 chip = desc->chip;
1489 if (!chip || !chip->set || !chip->set_debounce)
1490 goto fail;
1491 gpio -= chip->base;
1492 if (gpio >= chip->ngpio)
1493 goto fail;
1494 status = gpio_ensure_requested(desc, gpio);
1495 if (status < 0)
1496 goto fail;
1497
1498 /* now we know the gpio is valid and chip won't vanish */
1499
1500 spin_unlock_irqrestore(&gpio_lock, flags);
1501
David Brownell9c4ba942010-08-10 18:02:24 -07001502 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001503
1504 return chip->set_debounce(chip, gpio, debounce);
1505
1506fail:
1507 spin_unlock_irqrestore(&gpio_lock, flags);
1508 if (status)
1509 pr_debug("%s: gpio-%d status %d\n",
1510 __func__, gpio, status);
1511
1512 return status;
1513}
1514EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001515
1516/* I/O calls are only valid after configuration completed; the relevant
1517 * "is this a valid GPIO" error checks should already have been done.
1518 *
1519 * "Get" operations are often inlinable as reading a pin value register,
1520 * and masking the relevant bit in that register.
1521 *
1522 * When "set" operations are inlinable, they involve writing that mask to
1523 * one register to set a low value, or a different register to set it high.
1524 * Otherwise locking is needed, so there may be little value to inlining.
1525 *
1526 *------------------------------------------------------------------------
1527 *
1528 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1529 * have requested the GPIO. That can include implicit requesting by
1530 * a direction setting call. Marking a gpio as requested locks its chip
1531 * in memory, guaranteeing that these table lookups need no more locking
1532 * and that gpiochip_remove() will fail.
1533 *
1534 * REVISIT when debugging, consider adding some instrumentation to ensure
1535 * that the GPIO was actually requested.
1536 */
1537
1538/**
1539 * __gpio_get_value() - return a gpio's value
1540 * @gpio: gpio whose value will be returned
1541 * Context: any
1542 *
1543 * This is used directly or indirectly to implement gpio_get_value().
1544 * It returns the zero or nonzero value provided by the associated
1545 * gpio_chip.get() method; or zero if no such method is provided.
1546 */
1547int __gpio_get_value(unsigned gpio)
1548{
1549 struct gpio_chip *chip;
1550
1551 chip = gpio_to_chip(gpio);
David Brownell9c4ba942010-08-10 18:02:24 -07001552 WARN_ON(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001553 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1554}
1555EXPORT_SYMBOL_GPL(__gpio_get_value);
1556
1557/**
1558 * __gpio_set_value() - assign a gpio's value
1559 * @gpio: gpio whose value will be assigned
1560 * @value: value to assign
1561 * Context: any
1562 *
1563 * This is used directly or indirectly to implement gpio_set_value().
1564 * It invokes the associated gpio_chip.set() method.
1565 */
1566void __gpio_set_value(unsigned gpio, int value)
1567{
1568 struct gpio_chip *chip;
1569
1570 chip = gpio_to_chip(gpio);
David Brownell9c4ba942010-08-10 18:02:24 -07001571 WARN_ON(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001572 chip->set(chip, gpio - chip->base, value);
1573}
1574EXPORT_SYMBOL_GPL(__gpio_set_value);
1575
1576/**
1577 * __gpio_cansleep() - report whether gpio value access will sleep
1578 * @gpio: gpio in question
1579 * Context: any
1580 *
1581 * This is used directly or indirectly to implement gpio_cansleep(). It
1582 * returns nonzero if access reading or writing the GPIO value can sleep.
1583 */
1584int __gpio_cansleep(unsigned gpio)
1585{
1586 struct gpio_chip *chip;
1587
1588 /* only call this on GPIOs that are valid! */
1589 chip = gpio_to_chip(gpio);
1590
1591 return chip->can_sleep;
1592}
1593EXPORT_SYMBOL_GPL(__gpio_cansleep);
1594
David Brownell0f6d5042008-10-15 22:03:14 -07001595/**
1596 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1597 * @gpio: gpio whose IRQ will be returned (already requested)
1598 * Context: any
1599 *
1600 * This is used directly or indirectly to implement gpio_to_irq().
1601 * It returns the number of the IRQ signaled by this (input) GPIO,
1602 * or a negative errno.
1603 */
1604int __gpio_to_irq(unsigned gpio)
1605{
1606 struct gpio_chip *chip;
1607
1608 chip = gpio_to_chip(gpio);
1609 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1610}
1611EXPORT_SYMBOL_GPL(__gpio_to_irq);
1612
David Brownelld2876d02008-02-04 22:28:20 -08001613
1614
1615/* There's no value in making it easy to inline GPIO calls that may sleep.
1616 * Common examples include ones connected to I2C or SPI chips.
1617 */
1618
1619int gpio_get_value_cansleep(unsigned gpio)
1620{
1621 struct gpio_chip *chip;
1622
1623 might_sleep_if(extra_checks);
1624 chip = gpio_to_chip(gpio);
David Brownell978ccaa2008-10-18 20:27:49 -07001625 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001626}
1627EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1628
1629void gpio_set_value_cansleep(unsigned gpio, int value)
1630{
1631 struct gpio_chip *chip;
1632
1633 might_sleep_if(extra_checks);
1634 chip = gpio_to_chip(gpio);
1635 chip->set(chip, gpio - chip->base, value);
1636}
1637EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1638
1639
1640#ifdef CONFIG_DEBUG_FS
1641
David Brownelld2876d02008-02-04 22:28:20 -08001642static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1643{
1644 unsigned i;
1645 unsigned gpio = chip->base;
1646 struct gpio_desc *gdesc = &gpio_desc[gpio];
1647 int is_out;
1648
1649 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1650 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1651 continue;
1652
1653 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08001654 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001655 gpio, gdesc->label,
1656 is_out ? "out" : "in ",
1657 chip->get
1658 ? (chip->get(chip, i) ? "hi" : "lo")
1659 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08001660 seq_printf(s, "\n");
1661 }
1662}
1663
1664static int gpiolib_show(struct seq_file *s, void *unused)
1665{
1666 struct gpio_chip *chip = NULL;
1667 unsigned gpio;
1668 int started = 0;
1669
1670 /* REVISIT this isn't locked against gpio_chip removal ... */
1671
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001672 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
David Brownelld8f388d2008-07-25 01:46:07 -07001673 struct device *dev;
1674
David Brownelld2876d02008-02-04 22:28:20 -08001675 if (chip == gpio_desc[gpio].chip)
1676 continue;
1677 chip = gpio_desc[gpio].chip;
1678 if (!chip)
1679 continue;
1680
David Brownelld8f388d2008-07-25 01:46:07 -07001681 seq_printf(s, "%sGPIOs %d-%d",
David Brownelld2876d02008-02-04 22:28:20 -08001682 started ? "\n" : "",
David Brownelld8f388d2008-07-25 01:46:07 -07001683 chip->base, chip->base + chip->ngpio - 1);
1684 dev = chip->dev;
1685 if (dev)
1686 seq_printf(s, ", %s/%s",
1687 dev->bus ? dev->bus->name : "no-bus",
Kay Sievers14ab3092009-01-06 10:44:42 -08001688 dev_name(dev));
David Brownelld8f388d2008-07-25 01:46:07 -07001689 if (chip->label)
1690 seq_printf(s, ", %s", chip->label);
1691 if (chip->can_sleep)
1692 seq_printf(s, ", can sleep");
1693 seq_printf(s, ":\n");
1694
David Brownelld2876d02008-02-04 22:28:20 -08001695 started = 1;
1696 if (chip->dbg_show)
1697 chip->dbg_show(s, chip);
1698 else
1699 gpiolib_dbg_show(s, chip);
1700 }
1701 return 0;
1702}
1703
1704static int gpiolib_open(struct inode *inode, struct file *file)
1705{
1706 return single_open(file, gpiolib_show, NULL);
1707}
1708
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001709static const struct file_operations gpiolib_operations = {
David Brownelld2876d02008-02-04 22:28:20 -08001710 .open = gpiolib_open,
1711 .read = seq_read,
1712 .llseek = seq_lseek,
1713 .release = single_release,
1714};
1715
1716static int __init gpiolib_debugfs_init(void)
1717{
1718 /* /sys/kernel/debug/gpio */
1719 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1720 NULL, NULL, &gpiolib_operations);
1721 return 0;
1722}
1723subsys_initcall(gpiolib_debugfs_init);
1724
1725#endif /* DEBUG_FS */