blob: 4e51fe3c1fc4f97594d96fcb9a4e719fa40b8edc [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>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070011#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080013
14
15/* Optional implementation infrastructure for GPIO interfaces.
16 *
17 * Platforms may want to use this if they tend to use very many GPIOs
18 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
19 *
20 * When kernel footprint or instruction count is an issue, simpler
21 * implementations may be preferred. The GPIO programming interface
22 * allows for inlining speed-critical get/set operations for common
23 * cases, so that access to SOC-integrated GPIOs can sometimes cost
24 * only an instruction or two per bit.
25 */
26
27
28/* When debugging, extend minimal trust to callers and platform code.
29 * Also emit diagnostic messages that may help initial bringup, when
30 * board setup or driver bugs are most common.
31 *
32 * Otherwise, minimize overhead in what may be bitbanging codepaths.
33 */
34#ifdef DEBUG
35#define extra_checks 1
36#else
37#define extra_checks 0
38#endif
39
40/* gpio_lock prevents conflicts during gpio_desc[] table updates.
41 * While any GPIO is requested, its gpio_chip is not removable;
42 * each GPIO's "requested" flag serves as a lock and refcount.
43 */
44static DEFINE_SPINLOCK(gpio_lock);
45
46struct gpio_desc {
47 struct gpio_chip *chip;
48 unsigned long flags;
49/* flag symbols are bit numbers */
50#define FLAG_REQUESTED 0
51#define FLAG_IS_OUT 1
Anton Vorontsov169b6a72008-04-28 02:14:47 -070052#define FLAG_RESERVED 2
David Brownelld8f388d2008-07-25 01:46:07 -070053#define FLAG_EXPORT 3 /* protected by sysfs_lock */
54#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070055#define FLAG_TRIG_FALL 5 /* trigger on falling edge */
56#define FLAG_TRIG_RISE 6 /* trigger on rising edge */
Jani Nikula07697462009-12-15 16:46:20 -080057#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070058
59#define PDESC_ID_SHIFT 16 /* add new flags before this one */
60
61#define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
62#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080063
64#ifdef CONFIG_DEBUG_FS
65 const char *label;
66#endif
67};
68static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
69
Daniel Glöcknerff77c352009-09-22 16:46:38 -070070#ifdef CONFIG_GPIO_SYSFS
71struct poll_desc {
72 struct work_struct work;
73 struct sysfs_dirent *value_sd;
74};
75
76static struct idr pdesc_idr;
77#endif
78
David Brownelld2876d02008-02-04 22:28:20 -080079static inline void desc_set_label(struct gpio_desc *d, const char *label)
80{
81#ifdef CONFIG_DEBUG_FS
82 d->label = label;
83#endif
84}
85
86/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
87 * when setting direction, and otherwise illegal. Until board setup code
88 * and drivers use explicit requests everywhere (which won't happen when
89 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -070090 * message should motivate switching to explicit requests... so should
91 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -070092 *
93 * NOTE: the autorequest mechanism is going away; at this point it's
94 * only "legal" in the sense that (old) code using it won't break yet,
95 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -080096 */
David Brownell35e8bb52008-10-15 22:03:16 -070097static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
David Brownelld2876d02008-02-04 22:28:20 -080098{
David Brownell8a0cecf2009-04-02 16:57:06 -070099 const struct gpio_chip *chip = desc->chip;
100 const int gpio = chip->base + offset;
David Brownell35e8bb52008-10-15 22:03:16 -0700101
David Brownell8a0cecf2009-04-02 16:57:06 -0700102 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
103 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700104 if (!try_module_get(chip->owner)) {
105 pr_err("GPIO-%d: module can't be gotten \n", gpio);
106 clear_bit(FLAG_REQUESTED, &desc->flags);
107 /* lose */
108 return -EIO;
109 }
David Brownelld2876d02008-02-04 22:28:20 -0800110 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700111 /* caller must chip->request() w/o spinlock */
112 if (chip->request)
113 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800114 }
David Brownell35e8bb52008-10-15 22:03:16 -0700115 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800116}
117
118/* caller holds gpio_lock *OR* gpio is marked as requested */
119static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
120{
121 return gpio_desc[gpio].chip;
122}
123
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700124/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
125static int gpiochip_find_base(int ngpio)
126{
127 int i;
128 int spare = 0;
129 int base = -ENOSPC;
130
131 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700132 struct gpio_desc *desc = &gpio_desc[i];
133 struct gpio_chip *chip = desc->chip;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700134
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700135 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700136 spare++;
137 if (spare == ngpio) {
138 base = i;
139 break;
140 }
141 } else {
142 spare = 0;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700143 if (chip)
144 i -= chip->ngpio - 1;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700145 }
146 }
147
148 if (gpio_is_valid(base))
149 pr_debug("%s: found new base at %d\n", __func__, base);
150 return base;
151}
152
David Brownelld2876d02008-02-04 22:28:20 -0800153/**
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700154 * gpiochip_reserve() - reserve range of gpios to use with platform code only
155 * @start: starting gpio number
156 * @ngpio: number of gpios to reserve
157 * Context: platform init, potentially before irqs or kmalloc will work
158 *
159 * Returns a negative errno if any gpio within the range is already reserved
160 * or registered, else returns zero as a success code. Use this function
161 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
162 * for example because its driver support is not yet loaded.
163 */
164int __init gpiochip_reserve(int start, int ngpio)
165{
166 int ret = 0;
167 unsigned long flags;
168 int i;
169
Trent Piephobff5fda2008-05-23 13:04:44 -0700170 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700171 return -EINVAL;
172
173 spin_lock_irqsave(&gpio_lock, flags);
174
175 for (i = start; i < start + ngpio; i++) {
176 struct gpio_desc *desc = &gpio_desc[i];
177
178 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
179 ret = -EBUSY;
180 goto err;
181 }
182
183 set_bit(FLAG_RESERVED, &desc->flags);
184 }
185
186 pr_debug("%s: reserved gpios from %d to %d\n",
187 __func__, start, start + ngpio - 1);
188err:
189 spin_unlock_irqrestore(&gpio_lock, flags);
190
191 return ret;
192}
193
David Brownelld8f388d2008-07-25 01:46:07 -0700194#ifdef CONFIG_GPIO_SYSFS
195
196/* lock protects against unexport_gpio() being called while
197 * sysfs files are active.
198 */
199static DEFINE_MUTEX(sysfs_lock);
200
201/*
202 * /sys/class/gpio/gpioN... only for GPIOs that are exported
203 * /direction
204 * * MAY BE OMITTED if kernel won't allow direction changes
205 * * is read/write as "in" or "out"
206 * * may also be written as "high" or "low", initializing
207 * output value as specified ("out" implies "low")
208 * /value
209 * * always readable, subject to hardware behavior
210 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700211 * /edge
212 * * configures behavior of poll(2) on /value
213 * * available only if pin can generate IRQs on input
214 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800215 * /active_low
216 * * configures polarity of /value
217 * * is read/write as zero/nonzero
218 * * also affects existing and subsequent "falling" and "rising"
219 * /edge configuration
David Brownelld8f388d2008-07-25 01:46:07 -0700220 */
221
222static ssize_t gpio_direction_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
224{
225 const struct gpio_desc *desc = dev_get_drvdata(dev);
226 ssize_t status;
227
228 mutex_lock(&sysfs_lock);
229
230 if (!test_bit(FLAG_EXPORT, &desc->flags))
231 status = -EIO;
232 else
233 status = sprintf(buf, "%s\n",
234 test_bit(FLAG_IS_OUT, &desc->flags)
235 ? "out" : "in");
236
237 mutex_unlock(&sysfs_lock);
238 return status;
239}
240
241static ssize_t gpio_direction_store(struct device *dev,
242 struct device_attribute *attr, const char *buf, size_t size)
243{
244 const struct gpio_desc *desc = dev_get_drvdata(dev);
245 unsigned gpio = desc - gpio_desc;
246 ssize_t status;
247
248 mutex_lock(&sysfs_lock);
249
250 if (!test_bit(FLAG_EXPORT, &desc->flags))
251 status = -EIO;
252 else if (sysfs_streq(buf, "high"))
253 status = gpio_direction_output(gpio, 1);
254 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
255 status = gpio_direction_output(gpio, 0);
256 else if (sysfs_streq(buf, "in"))
257 status = gpio_direction_input(gpio);
258 else
259 status = -EINVAL;
260
261 mutex_unlock(&sysfs_lock);
262 return status ? : size;
263}
264
Jani Nikula07697462009-12-15 16:46:20 -0800265static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700266 gpio_direction_show, gpio_direction_store);
267
268static ssize_t gpio_value_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
270{
271 const struct gpio_desc *desc = dev_get_drvdata(dev);
272 unsigned gpio = desc - gpio_desc;
273 ssize_t status;
274
275 mutex_lock(&sysfs_lock);
276
Jani Nikula07697462009-12-15 16:46:20 -0800277 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700278 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800279 } else {
280 int value;
281
282 value = !!gpio_get_value_cansleep(gpio);
283 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
284 value = !value;
285
286 status = sprintf(buf, "%d\n", value);
287 }
David Brownelld8f388d2008-07-25 01:46:07 -0700288
289 mutex_unlock(&sysfs_lock);
290 return status;
291}
292
293static ssize_t gpio_value_store(struct device *dev,
294 struct device_attribute *attr, const char *buf, size_t size)
295{
296 const struct gpio_desc *desc = dev_get_drvdata(dev);
297 unsigned gpio = desc - gpio_desc;
298 ssize_t status;
299
300 mutex_lock(&sysfs_lock);
301
302 if (!test_bit(FLAG_EXPORT, &desc->flags))
303 status = -EIO;
304 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
305 status = -EPERM;
306 else {
307 long value;
308
309 status = strict_strtol(buf, 0, &value);
310 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800311 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
312 value = !value;
David Brownelld8f388d2008-07-25 01:46:07 -0700313 gpio_set_value_cansleep(gpio, value != 0);
314 status = size;
315 }
316 }
317
318 mutex_unlock(&sysfs_lock);
319 return status;
320}
321
Jani Nikula07697462009-12-15 16:46:20 -0800322static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700323 gpio_value_show, gpio_value_store);
324
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700325static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
326{
327 struct work_struct *work = priv;
328
329 schedule_work(work);
330 return IRQ_HANDLED;
331}
332
333static void gpio_notify_sysfs(struct work_struct *work)
334{
335 struct poll_desc *pdesc;
336
337 pdesc = container_of(work, struct poll_desc, work);
338 sysfs_notify_dirent(pdesc->value_sd);
339}
340
341static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
342 unsigned long gpio_flags)
343{
344 struct poll_desc *pdesc;
345 unsigned long irq_flags;
346 int ret, irq, id;
347
348 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
349 return 0;
350
351 irq = gpio_to_irq(desc - gpio_desc);
352 if (irq < 0)
353 return -EIO;
354
355 id = desc->flags >> PDESC_ID_SHIFT;
356 pdesc = idr_find(&pdesc_idr, id);
357 if (pdesc) {
358 free_irq(irq, &pdesc->work);
359 cancel_work_sync(&pdesc->work);
360 }
361
362 desc->flags &= ~GPIO_TRIGGER_MASK;
363
364 if (!gpio_flags) {
365 ret = 0;
366 goto free_sd;
367 }
368
369 irq_flags = IRQF_SHARED;
370 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800371 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
372 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700373 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800374 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
375 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700376
377 if (!pdesc) {
378 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
379 if (!pdesc) {
380 ret = -ENOMEM;
381 goto err_out;
382 }
383
384 do {
385 ret = -ENOMEM;
386 if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
387 ret = idr_get_new_above(&pdesc_idr,
388 pdesc, 1, &id);
389 } while (ret == -EAGAIN);
390
391 if (ret)
392 goto free_mem;
393
394 desc->flags &= GPIO_FLAGS_MASK;
395 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
396
397 if (desc->flags >> PDESC_ID_SHIFT != id) {
398 ret = -ERANGE;
399 goto free_id;
400 }
401
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700402 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700403 if (!pdesc->value_sd) {
404 ret = -ENODEV;
405 goto free_id;
406 }
407 INIT_WORK(&pdesc->work, gpio_notify_sysfs);
408 }
409
410 ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
411 "gpiolib", &pdesc->work);
412 if (ret)
413 goto free_sd;
414
415 desc->flags |= gpio_flags;
416 return 0;
417
418free_sd:
Dan Carpenter3913fd52010-04-27 14:12:03 -0700419 if (pdesc)
420 sysfs_put(pdesc->value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700421free_id:
422 idr_remove(&pdesc_idr, id);
423 desc->flags &= GPIO_FLAGS_MASK;
424free_mem:
425 kfree(pdesc);
426err_out:
427 return ret;
428}
429
430static const struct {
431 const char *name;
432 unsigned long flags;
433} trigger_types[] = {
434 { "none", 0 },
435 { "falling", BIT(FLAG_TRIG_FALL) },
436 { "rising", BIT(FLAG_TRIG_RISE) },
437 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
438};
439
440static ssize_t gpio_edge_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
443 const struct gpio_desc *desc = dev_get_drvdata(dev);
444 ssize_t status;
445
446 mutex_lock(&sysfs_lock);
447
448 if (!test_bit(FLAG_EXPORT, &desc->flags))
449 status = -EIO;
450 else {
451 int i;
452
453 status = 0;
454 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
455 if ((desc->flags & GPIO_TRIGGER_MASK)
456 == trigger_types[i].flags) {
457 status = sprintf(buf, "%s\n",
458 trigger_types[i].name);
459 break;
460 }
461 }
462
463 mutex_unlock(&sysfs_lock);
464 return status;
465}
466
467static ssize_t gpio_edge_store(struct device *dev,
468 struct device_attribute *attr, const char *buf, size_t size)
469{
470 struct gpio_desc *desc = dev_get_drvdata(dev);
471 ssize_t status;
472 int i;
473
474 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
475 if (sysfs_streq(trigger_types[i].name, buf))
476 goto found;
477 return -EINVAL;
478
479found:
480 mutex_lock(&sysfs_lock);
481
482 if (!test_bit(FLAG_EXPORT, &desc->flags))
483 status = -EIO;
484 else {
485 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
486 if (!status)
487 status = size;
488 }
489
490 mutex_unlock(&sysfs_lock);
491
492 return status;
493}
494
495static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
496
Jani Nikula07697462009-12-15 16:46:20 -0800497static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
498 int value)
499{
500 int status = 0;
501
502 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
503 return 0;
504
505 if (value)
506 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
507 else
508 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
509
510 /* reconfigure poll(2) support if enabled on one edge only */
511 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
512 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
513 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
514
515 gpio_setup_irq(desc, dev, 0);
516 status = gpio_setup_irq(desc, dev, trigger_flags);
517 }
518
519 return status;
520}
521
522static ssize_t gpio_active_low_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
525 const struct gpio_desc *desc = dev_get_drvdata(dev);
526 ssize_t status;
527
528 mutex_lock(&sysfs_lock);
529
530 if (!test_bit(FLAG_EXPORT, &desc->flags))
531 status = -EIO;
532 else
533 status = sprintf(buf, "%d\n",
534 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
535
536 mutex_unlock(&sysfs_lock);
537
538 return status;
539}
540
541static ssize_t gpio_active_low_store(struct device *dev,
542 struct device_attribute *attr, const char *buf, size_t size)
543{
544 struct gpio_desc *desc = dev_get_drvdata(dev);
545 ssize_t status;
546
547 mutex_lock(&sysfs_lock);
548
549 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
550 status = -EIO;
551 } else {
552 long value;
553
554 status = strict_strtol(buf, 0, &value);
555 if (status == 0)
556 status = sysfs_set_active_low(desc, dev, value != 0);
557 }
558
559 mutex_unlock(&sysfs_lock);
560
561 return status ? : size;
562}
563
564static const DEVICE_ATTR(active_low, 0644,
565 gpio_active_low_show, gpio_active_low_store);
566
David Brownelld8f388d2008-07-25 01:46:07 -0700567static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d2008-07-25 01:46:07 -0700568 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800569 &dev_attr_active_low.attr,
David Brownelld8f388d2008-07-25 01:46:07 -0700570 NULL,
571};
572
573static const struct attribute_group gpio_attr_group = {
574 .attrs = (struct attribute **) gpio_attrs,
575};
576
577/*
578 * /sys/class/gpio/gpiochipN/
579 * /base ... matching gpio_chip.base (N)
580 * /label ... matching gpio_chip.label
581 * /ngpio ... matching gpio_chip.ngpio
582 */
583
584static ssize_t chip_base_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
586{
587 const struct gpio_chip *chip = dev_get_drvdata(dev);
588
589 return sprintf(buf, "%d\n", chip->base);
590}
591static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
592
593static ssize_t chip_label_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
595{
596 const struct gpio_chip *chip = dev_get_drvdata(dev);
597
598 return sprintf(buf, "%s\n", chip->label ? : "");
599}
600static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
601
602static ssize_t chip_ngpio_show(struct device *dev,
603 struct device_attribute *attr, char *buf)
604{
605 const struct gpio_chip *chip = dev_get_drvdata(dev);
606
607 return sprintf(buf, "%u\n", chip->ngpio);
608}
609static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
610
611static const struct attribute *gpiochip_attrs[] = {
612 &dev_attr_base.attr,
613 &dev_attr_label.attr,
614 &dev_attr_ngpio.attr,
615 NULL,
616};
617
618static const struct attribute_group gpiochip_attr_group = {
619 .attrs = (struct attribute **) gpiochip_attrs,
620};
621
622/*
623 * /sys/class/gpio/export ... write-only
624 * integer N ... number of GPIO to export (full access)
625 * /sys/class/gpio/unexport ... write-only
626 * integer N ... number of GPIO to unexport
627 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100628static ssize_t export_store(struct class *class,
629 struct class_attribute *attr,
630 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700631{
632 long gpio;
633 int status;
634
635 status = strict_strtol(buf, 0, &gpio);
636 if (status < 0)
637 goto done;
638
639 /* No extra locking here; FLAG_SYSFS just signifies that the
640 * request and export were done by on behalf of userspace, so
641 * they may be undone on its behalf too.
642 */
643
644 status = gpio_request(gpio, "sysfs");
645 if (status < 0)
646 goto done;
647
648 status = gpio_export(gpio, true);
649 if (status < 0)
650 gpio_free(gpio);
651 else
652 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
653
654done:
655 if (status)
656 pr_debug("%s: status %d\n", __func__, status);
657 return status ? : len;
658}
659
Andi Kleen28812fe2010-01-05 12:48:07 +0100660static ssize_t unexport_store(struct class *class,
661 struct class_attribute *attr,
662 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700663{
664 long gpio;
665 int status;
666
667 status = strict_strtol(buf, 0, &gpio);
668 if (status < 0)
669 goto done;
670
671 status = -EINVAL;
672
673 /* reject bogus commands (gpio_unexport ignores them) */
674 if (!gpio_is_valid(gpio))
675 goto done;
676
677 /* No extra locking here; FLAG_SYSFS just signifies that the
678 * request and export were done by on behalf of userspace, so
679 * they may be undone on its behalf too.
680 */
681 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
682 status = 0;
683 gpio_free(gpio);
684 }
685done:
686 if (status)
687 pr_debug("%s: status %d\n", __func__, status);
688 return status ? : len;
689}
690
691static struct class_attribute gpio_class_attrs[] = {
692 __ATTR(export, 0200, NULL, export_store),
693 __ATTR(unexport, 0200, NULL, unexport_store),
694 __ATTR_NULL,
695};
696
697static struct class gpio_class = {
698 .name = "gpio",
699 .owner = THIS_MODULE,
700
701 .class_attrs = gpio_class_attrs,
702};
703
704
705/**
706 * gpio_export - export a GPIO through sysfs
707 * @gpio: gpio to make available, already requested
708 * @direction_may_change: true if userspace may change gpio direction
709 * Context: arch_initcall or later
710 *
711 * When drivers want to make a GPIO accessible to userspace after they
712 * have requested it -- perhaps while debugging, or as part of their
713 * public interface -- they may use this routine. If the GPIO can
714 * change direction (some can't) and the caller allows it, userspace
715 * will see "direction" sysfs attribute which may be used to change
716 * the gpio's direction. A "value" attribute will always be provided.
717 *
718 * Returns zero on success, else an error.
719 */
720int gpio_export(unsigned gpio, bool direction_may_change)
721{
722 unsigned long flags;
723 struct gpio_desc *desc;
724 int status = -EINVAL;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700725 const char *ioname = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -0700726
727 /* can't export until sysfs is available ... */
728 if (!gpio_class.p) {
729 pr_debug("%s: called too early!\n", __func__);
730 return -ENOENT;
731 }
732
733 if (!gpio_is_valid(gpio))
734 goto done;
735
736 mutex_lock(&sysfs_lock);
737
738 spin_lock_irqsave(&gpio_lock, flags);
739 desc = &gpio_desc[gpio];
740 if (test_bit(FLAG_REQUESTED, &desc->flags)
741 && !test_bit(FLAG_EXPORT, &desc->flags)) {
742 status = 0;
743 if (!desc->chip->direction_input
744 || !desc->chip->direction_output)
745 direction_may_change = false;
746 }
747 spin_unlock_irqrestore(&gpio_lock, flags);
748
Daniel Silverstone926b6632009-04-02 16:57:05 -0700749 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
750 ioname = desc->chip->names[gpio - desc->chip->base];
751
David Brownelld8f388d2008-07-25 01:46:07 -0700752 if (status == 0) {
753 struct device *dev;
754
755 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Uwe Kleine-König7cfe1392010-05-26 14:42:18 -0700756 desc, ioname ? ioname : "gpio%u", gpio);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800757 if (!IS_ERR(dev)) {
Jani Nikula07697462009-12-15 16:46:20 -0800758 status = sysfs_create_group(&dev->kobj,
David Brownelld8f388d2008-07-25 01:46:07 -0700759 &gpio_attr_group);
Jani Nikula07697462009-12-15 16:46:20 -0800760
761 if (!status && direction_may_change)
David Brownelld8f388d2008-07-25 01:46:07 -0700762 status = device_create_file(dev,
Jani Nikula07697462009-12-15 16:46:20 -0800763 &dev_attr_direction);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700764
765 if (!status && gpio_to_irq(gpio) >= 0
766 && (direction_may_change
767 || !test_bit(FLAG_IS_OUT,
768 &desc->flags)))
769 status = device_create_file(dev,
770 &dev_attr_edge);
771
David Brownelld8f388d2008-07-25 01:46:07 -0700772 if (status != 0)
773 device_unregister(dev);
774 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800775 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700776 if (status == 0)
777 set_bit(FLAG_EXPORT, &desc->flags);
778 }
779
780 mutex_unlock(&sysfs_lock);
781
782done:
783 if (status)
784 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
785
786 return status;
787}
788EXPORT_SYMBOL_GPL(gpio_export);
789
790static int match_export(struct device *dev, void *data)
791{
792 return dev_get_drvdata(dev) == data;
793}
794
795/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700796 * gpio_export_link - create a sysfs link to an exported GPIO node
797 * @dev: device under which to create symlink
798 * @name: name of the symlink
799 * @gpio: gpio to create symlink to, already exported
800 *
801 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
802 * node. Caller is responsible for unlinking.
803 *
804 * Returns zero on success, else an error.
805 */
806int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
807{
808 struct gpio_desc *desc;
809 int status = -EINVAL;
810
811 if (!gpio_is_valid(gpio))
812 goto done;
813
814 mutex_lock(&sysfs_lock);
815
816 desc = &gpio_desc[gpio];
817
818 if (test_bit(FLAG_EXPORT, &desc->flags)) {
819 struct device *tdev;
820
821 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
822 if (tdev != NULL) {
823 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
824 name);
825 } else {
826 status = -ENODEV;
827 }
828 }
829
830 mutex_unlock(&sysfs_lock);
831
832done:
833 if (status)
834 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
835
836 return status;
837}
838EXPORT_SYMBOL_GPL(gpio_export_link);
839
Jani Nikula07697462009-12-15 16:46:20 -0800840
841/**
842 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
843 * @gpio: gpio to change
844 * @value: non-zero to use active low, i.e. inverted values
845 *
846 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
847 * The GPIO does not have to be exported yet. If poll(2) support has
848 * been enabled for either rising or falling edge, it will be
849 * reconfigured to follow the new polarity.
850 *
851 * Returns zero on success, else an error.
852 */
853int gpio_sysfs_set_active_low(unsigned gpio, int value)
854{
855 struct gpio_desc *desc;
856 struct device *dev = NULL;
857 int status = -EINVAL;
858
859 if (!gpio_is_valid(gpio))
860 goto done;
861
862 mutex_lock(&sysfs_lock);
863
864 desc = &gpio_desc[gpio];
865
866 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800867 dev = class_find_device(&gpio_class, NULL, desc, match_export);
868 if (dev == NULL) {
869 status = -ENODEV;
870 goto unlock;
871 }
872 }
873
874 status = sysfs_set_active_low(desc, dev, value);
875
876unlock:
877 mutex_unlock(&sysfs_lock);
878
879done:
880 if (status)
881 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
882
883 return status;
884}
885EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
886
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700887/**
David Brownelld8f388d2008-07-25 01:46:07 -0700888 * gpio_unexport - reverse effect of gpio_export()
889 * @gpio: gpio to make unavailable
890 *
891 * This is implicit on gpio_free().
892 */
893void gpio_unexport(unsigned gpio)
894{
895 struct gpio_desc *desc;
Jon Povey6a99ad42010-07-27 13:18:06 -0700896 int status = 0;
David Brownelld8f388d2008-07-25 01:46:07 -0700897
Jon Povey6a99ad42010-07-27 13:18:06 -0700898 if (!gpio_is_valid(gpio)) {
899 status = -EINVAL;
David Brownelld8f388d2008-07-25 01:46:07 -0700900 goto done;
Jon Povey6a99ad42010-07-27 13:18:06 -0700901 }
David Brownelld8f388d2008-07-25 01:46:07 -0700902
903 mutex_lock(&sysfs_lock);
904
905 desc = &gpio_desc[gpio];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700906
David Brownelld8f388d2008-07-25 01:46:07 -0700907 if (test_bit(FLAG_EXPORT, &desc->flags)) {
908 struct device *dev = NULL;
909
910 dev = class_find_device(&gpio_class, NULL, desc, match_export);
911 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700912 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700913 clear_bit(FLAG_EXPORT, &desc->flags);
914 put_device(dev);
915 device_unregister(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700916 } else
917 status = -ENODEV;
918 }
919
920 mutex_unlock(&sysfs_lock);
921done:
922 if (status)
923 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
924}
925EXPORT_SYMBOL_GPL(gpio_unexport);
926
927static int gpiochip_export(struct gpio_chip *chip)
928{
929 int status;
930 struct device *dev;
931
932 /* Many systems register gpio chips for SOC support very early,
933 * before driver model support is available. In those cases we
934 * export this later, in gpiolib_sysfs_init() ... here we just
935 * verify that _some_ field of gpio_class got initialized.
936 */
937 if (!gpio_class.p)
938 return 0;
939
940 /* use chip->base for the ID; it's already known to be unique */
941 mutex_lock(&sysfs_lock);
942 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
943 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800944 if (!IS_ERR(dev)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700945 status = sysfs_create_group(&dev->kobj,
946 &gpiochip_attr_group);
947 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800948 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700949 chip->exported = (status == 0);
950 mutex_unlock(&sysfs_lock);
951
952 if (status) {
953 unsigned long flags;
954 unsigned gpio;
955
956 spin_lock_irqsave(&gpio_lock, flags);
957 gpio = chip->base;
958 while (gpio_desc[gpio].chip == chip)
959 gpio_desc[gpio++].chip = NULL;
960 spin_unlock_irqrestore(&gpio_lock, flags);
961
962 pr_debug("%s: chip %s status %d\n", __func__,
963 chip->label, status);
964 }
965
966 return status;
967}
968
969static void gpiochip_unexport(struct gpio_chip *chip)
970{
971 int status;
972 struct device *dev;
973
974 mutex_lock(&sysfs_lock);
975 dev = class_find_device(&gpio_class, NULL, chip, match_export);
976 if (dev) {
977 put_device(dev);
978 device_unregister(dev);
979 chip->exported = 0;
980 status = 0;
981 } else
982 status = -ENODEV;
983 mutex_unlock(&sysfs_lock);
984
985 if (status)
986 pr_debug("%s: chip %s status %d\n", __func__,
987 chip->label, status);
988}
989
990static int __init gpiolib_sysfs_init(void)
991{
992 int status;
993 unsigned long flags;
994 unsigned gpio;
995
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700996 idr_init(&pdesc_idr);
997
David Brownelld8f388d2008-07-25 01:46:07 -0700998 status = class_register(&gpio_class);
999 if (status < 0)
1000 return status;
1001
1002 /* Scan and register the gpio_chips which registered very
1003 * early (e.g. before the class_register above was called).
1004 *
1005 * We run before arch_initcall() so chip->dev nodes can have
1006 * registered, and so arch_initcall() can always gpio_export().
1007 */
1008 spin_lock_irqsave(&gpio_lock, flags);
1009 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
1010 struct gpio_chip *chip;
1011
1012 chip = gpio_desc[gpio].chip;
1013 if (!chip || chip->exported)
1014 continue;
1015
1016 spin_unlock_irqrestore(&gpio_lock, flags);
1017 status = gpiochip_export(chip);
1018 spin_lock_irqsave(&gpio_lock, flags);
1019 }
1020 spin_unlock_irqrestore(&gpio_lock, flags);
1021
1022
1023 return status;
1024}
1025postcore_initcall(gpiolib_sysfs_init);
1026
1027#else
1028static inline int gpiochip_export(struct gpio_chip *chip)
1029{
1030 return 0;
1031}
1032
1033static inline void gpiochip_unexport(struct gpio_chip *chip)
1034{
1035}
1036
1037#endif /* CONFIG_GPIO_SYSFS */
1038
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001039/**
David Brownelld2876d02008-02-04 22:28:20 -08001040 * gpiochip_add() - register a gpio_chip
1041 * @chip: the chip to register, with chip->base initialized
1042 * Context: potentially before irqs or kmalloc will work
1043 *
1044 * Returns a negative errno if the chip can't be registered, such as
1045 * because the chip->base is invalid or already associated with a
1046 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001047 *
David Brownelld8f388d2008-07-25 01:46:07 -07001048 * When gpiochip_add() is called very early during boot, so that GPIOs
1049 * can be freely used, the chip->dev device must be registered before
1050 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1051 * for GPIOs will fail rudely.
1052 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001053 * If chip->base is negative, this requests dynamic assignment of
1054 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001055 */
1056int gpiochip_add(struct gpio_chip *chip)
1057{
1058 unsigned long flags;
1059 int status = 0;
1060 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001061 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001062
Trent Piephobff5fda2008-05-23 13:04:44 -07001063 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001064 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001065 status = -EINVAL;
1066 goto fail;
1067 }
1068
1069 spin_lock_irqsave(&gpio_lock, flags);
1070
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001071 if (base < 0) {
1072 base = gpiochip_find_base(chip->ngpio);
1073 if (base < 0) {
1074 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -07001075 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001076 }
1077 chip->base = base;
1078 }
1079
David Brownelld2876d02008-02-04 22:28:20 -08001080 /* these GPIO numbers must not be managed by another gpio_chip */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001081 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001082 if (gpio_desc[id].chip != NULL) {
1083 status = -EBUSY;
1084 break;
1085 }
1086 }
1087 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001088 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001089 gpio_desc[id].chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001090
1091 /* REVISIT: most hardware initializes GPIOs as
1092 * inputs (often with pullups enabled) so power
1093 * usage is minimized. Linux code should set the
1094 * gpio direction first thing; but until it does,
1095 * we may expose the wrong direction in sysfs.
1096 */
1097 gpio_desc[id].flags = !chip->direction_input
1098 ? (1 << FLAG_IS_OUT)
1099 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001100 }
1101 }
1102
David Brownelld8f388d2008-07-25 01:46:07 -07001103unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001104 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001105 if (status == 0)
1106 status = gpiochip_export(chip);
David Brownelld2876d02008-02-04 22:28:20 -08001107fail:
1108 /* failures here can mean systems won't boot... */
1109 if (status)
Ben Dooks796a8e42010-05-26 14:42:21 -07001110 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
Trent Piephobff5fda2008-05-23 13:04:44 -07001111 chip->base, chip->base + chip->ngpio - 1,
David Brownelld2876d02008-02-04 22:28:20 -08001112 chip->label ? : "generic");
1113 return status;
1114}
1115EXPORT_SYMBOL_GPL(gpiochip_add);
1116
1117/**
1118 * gpiochip_remove() - unregister a gpio_chip
1119 * @chip: the chip to unregister
1120 *
1121 * A gpio_chip with any GPIOs still requested may not be removed.
1122 */
1123int gpiochip_remove(struct gpio_chip *chip)
1124{
1125 unsigned long flags;
1126 int status = 0;
1127 unsigned id;
1128
1129 spin_lock_irqsave(&gpio_lock, flags);
1130
1131 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1132 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1133 status = -EBUSY;
1134 break;
1135 }
1136 }
1137 if (status == 0) {
1138 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1139 gpio_desc[id].chip = NULL;
1140 }
1141
1142 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001143
1144 if (status == 0)
1145 gpiochip_unexport(chip);
1146
David Brownelld2876d02008-02-04 22:28:20 -08001147 return status;
1148}
1149EXPORT_SYMBOL_GPL(gpiochip_remove);
1150
1151
1152/* These "optional" allocation calls help prevent drivers from stomping
1153 * on each other, and help provide better diagnostics in debugfs.
1154 * They're called even less than the "set direction" calls.
1155 */
1156int gpio_request(unsigned gpio, const char *label)
1157{
1158 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001159 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001160 int status = -EINVAL;
1161 unsigned long flags;
1162
1163 spin_lock_irqsave(&gpio_lock, flags);
1164
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001165 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001166 goto done;
1167 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001168 chip = desc->chip;
1169 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001170 goto done;
1171
David Brownell35e8bb52008-10-15 22:03:16 -07001172 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001173 goto done;
1174
David Brownelld2876d02008-02-04 22:28:20 -08001175 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001176 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001177 */
1178
1179 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1180 desc_set_label(desc, label ? : "?");
1181 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001182 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001183 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001184 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001185 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001186 }
1187
1188 if (chip->request) {
1189 /* chip->request may sleep */
1190 spin_unlock_irqrestore(&gpio_lock, flags);
1191 status = chip->request(chip, gpio - chip->base);
1192 spin_lock_irqsave(&gpio_lock, flags);
1193
1194 if (status < 0) {
1195 desc_set_label(desc, NULL);
1196 module_put(chip->owner);
1197 clear_bit(FLAG_REQUESTED, &desc->flags);
1198 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001199 }
David Brownelld2876d02008-02-04 22:28:20 -08001200
1201done:
1202 if (status)
1203 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1204 gpio, label ? : "?", status);
1205 spin_unlock_irqrestore(&gpio_lock, flags);
1206 return status;
1207}
1208EXPORT_SYMBOL_GPL(gpio_request);
1209
1210void gpio_free(unsigned gpio)
1211{
1212 unsigned long flags;
1213 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001214 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001215
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001216 might_sleep();
1217
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001218 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -08001219 WARN_ON(extra_checks);
1220 return;
1221 }
1222
David Brownelld8f388d2008-07-25 01:46:07 -07001223 gpio_unexport(gpio);
1224
David Brownelld2876d02008-02-04 22:28:20 -08001225 spin_lock_irqsave(&gpio_lock, flags);
1226
1227 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001228 chip = desc->chip;
1229 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1230 if (chip->free) {
1231 spin_unlock_irqrestore(&gpio_lock, flags);
1232 might_sleep_if(extra_checks && chip->can_sleep);
1233 chip->free(chip, gpio - chip->base);
1234 spin_lock_irqsave(&gpio_lock, flags);
1235 }
David Brownelld2876d02008-02-04 22:28:20 -08001236 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001237 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001238 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001239 clear_bit(FLAG_REQUESTED, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001240 } else
David Brownelld2876d02008-02-04 22:28:20 -08001241 WARN_ON(extra_checks);
1242
1243 spin_unlock_irqrestore(&gpio_lock, flags);
1244}
1245EXPORT_SYMBOL_GPL(gpio_free);
1246
Eric Miao3e45f1d2010-03-05 13:44:35 -08001247/**
1248 * gpio_request_one - request a single GPIO with initial configuration
1249 * @gpio: the GPIO number
1250 * @flags: GPIO configuration as specified by GPIOF_*
1251 * @label: a literal description string of this GPIO
1252 */
1253int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1254{
1255 int err;
1256
1257 err = gpio_request(gpio, label);
1258 if (err)
1259 return err;
1260
1261 if (flags & GPIOF_DIR_IN)
1262 err = gpio_direction_input(gpio);
1263 else
1264 err = gpio_direction_output(gpio,
1265 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1266
1267 return err;
1268}
1269EXPORT_SYMBOL_GPL(gpio_request_one);
1270
1271/**
1272 * gpio_request_array - request multiple GPIOs in a single call
1273 * @array: array of the 'struct gpio'
1274 * @num: how many GPIOs in the array
1275 */
1276int gpio_request_array(struct gpio *array, size_t num)
1277{
1278 int i, err;
1279
1280 for (i = 0; i < num; i++, array++) {
1281 err = gpio_request_one(array->gpio, array->flags, array->label);
1282 if (err)
1283 goto err_free;
1284 }
1285 return 0;
1286
1287err_free:
1288 while (i--)
1289 gpio_free((--array)->gpio);
1290 return err;
1291}
1292EXPORT_SYMBOL_GPL(gpio_request_array);
1293
1294/**
1295 * gpio_free_array - release multiple GPIOs in a single call
1296 * @array: array of the 'struct gpio'
1297 * @num: how many GPIOs in the array
1298 */
1299void gpio_free_array(struct gpio *array, size_t num)
1300{
1301 while (num--)
1302 gpio_free((array++)->gpio);
1303}
1304EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001305
1306/**
1307 * gpiochip_is_requested - return string iff signal was requested
1308 * @chip: controller managing the signal
1309 * @offset: of signal within controller's 0..(ngpio - 1) range
1310 *
1311 * Returns NULL if the GPIO is not currently requested, else a string.
1312 * If debugfs support is enabled, the string returned is the label passed
1313 * to gpio_request(); otherwise it is a meaningless constant.
1314 *
1315 * This function is for use by GPIO controller drivers. The label can
1316 * help with diagnostics, and knowing that the signal is used as a GPIO
1317 * can help avoid accidentally multiplexing it to another controller.
1318 */
1319const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1320{
1321 unsigned gpio = chip->base + offset;
1322
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001323 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001324 return NULL;
1325 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1326 return NULL;
1327#ifdef CONFIG_DEBUG_FS
1328 return gpio_desc[gpio].label;
1329#else
1330 return "?";
1331#endif
1332}
1333EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1334
1335
1336/* Drivers MUST set GPIO direction before making get/set calls. In
1337 * some cases this is done in early boot, before IRQs are enabled.
1338 *
1339 * As a rule these aren't called more than once (except for drivers
1340 * using the open-drain emulation idiom) so these are natural places
1341 * to accumulate extra debugging checks. Note that we can't (yet)
1342 * rely on gpio_request() having been called beforehand.
1343 */
1344
1345int gpio_direction_input(unsigned gpio)
1346{
1347 unsigned long flags;
1348 struct gpio_chip *chip;
1349 struct gpio_desc *desc = &gpio_desc[gpio];
1350 int status = -EINVAL;
1351
1352 spin_lock_irqsave(&gpio_lock, flags);
1353
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001354 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001355 goto fail;
1356 chip = desc->chip;
1357 if (!chip || !chip->get || !chip->direction_input)
1358 goto fail;
1359 gpio -= chip->base;
1360 if (gpio >= chip->ngpio)
1361 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001362 status = gpio_ensure_requested(desc, gpio);
1363 if (status < 0)
1364 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001365
1366 /* now we know the gpio is valid and chip won't vanish */
1367
1368 spin_unlock_irqrestore(&gpio_lock, flags);
1369
1370 might_sleep_if(extra_checks && chip->can_sleep);
1371
David Brownell35e8bb52008-10-15 22:03:16 -07001372 if (status) {
1373 status = chip->request(chip, gpio);
1374 if (status < 0) {
1375 pr_debug("GPIO-%d: chip request fail, %d\n",
1376 chip->base + gpio, status);
1377 /* and it's not available to anyone else ...
1378 * gpio_request() is the fully clean solution.
1379 */
1380 goto lose;
1381 }
1382 }
1383
David Brownelld2876d02008-02-04 22:28:20 -08001384 status = chip->direction_input(chip, gpio);
1385 if (status == 0)
1386 clear_bit(FLAG_IS_OUT, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001387lose:
David Brownelld2876d02008-02-04 22:28:20 -08001388 return status;
1389fail:
1390 spin_unlock_irqrestore(&gpio_lock, flags);
1391 if (status)
1392 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001393 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001394 return status;
1395}
1396EXPORT_SYMBOL_GPL(gpio_direction_input);
1397
1398int gpio_direction_output(unsigned gpio, int value)
1399{
1400 unsigned long flags;
1401 struct gpio_chip *chip;
1402 struct gpio_desc *desc = &gpio_desc[gpio];
1403 int status = -EINVAL;
1404
1405 spin_lock_irqsave(&gpio_lock, flags);
1406
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001407 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001408 goto fail;
1409 chip = desc->chip;
1410 if (!chip || !chip->set || !chip->direction_output)
1411 goto fail;
1412 gpio -= chip->base;
1413 if (gpio >= chip->ngpio)
1414 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001415 status = gpio_ensure_requested(desc, gpio);
1416 if (status < 0)
1417 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001418
1419 /* now we know the gpio is valid and chip won't vanish */
1420
1421 spin_unlock_irqrestore(&gpio_lock, flags);
1422
1423 might_sleep_if(extra_checks && chip->can_sleep);
1424
David Brownell35e8bb52008-10-15 22:03:16 -07001425 if (status) {
1426 status = chip->request(chip, gpio);
1427 if (status < 0) {
1428 pr_debug("GPIO-%d: chip request fail, %d\n",
1429 chip->base + gpio, status);
1430 /* and it's not available to anyone else ...
1431 * gpio_request() is the fully clean solution.
1432 */
1433 goto lose;
1434 }
1435 }
1436
David Brownelld2876d02008-02-04 22:28:20 -08001437 status = chip->direction_output(chip, gpio, value);
1438 if (status == 0)
1439 set_bit(FLAG_IS_OUT, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001440lose:
David Brownelld2876d02008-02-04 22:28:20 -08001441 return status;
1442fail:
1443 spin_unlock_irqrestore(&gpio_lock, flags);
1444 if (status)
1445 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001446 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001447 return status;
1448}
1449EXPORT_SYMBOL_GPL(gpio_direction_output);
1450
Felipe Balbic4b5be92010-05-26 14:42:23 -07001451/**
1452 * gpio_set_debounce - sets @debounce time for a @gpio
1453 * @gpio: the gpio to set debounce time
1454 * @debounce: debounce time is microseconds
1455 */
1456int gpio_set_debounce(unsigned gpio, unsigned debounce)
1457{
1458 unsigned long flags;
1459 struct gpio_chip *chip;
1460 struct gpio_desc *desc = &gpio_desc[gpio];
1461 int status = -EINVAL;
1462
1463 spin_lock_irqsave(&gpio_lock, flags);
1464
1465 if (!gpio_is_valid(gpio))
1466 goto fail;
1467 chip = desc->chip;
1468 if (!chip || !chip->set || !chip->set_debounce)
1469 goto fail;
1470 gpio -= chip->base;
1471 if (gpio >= chip->ngpio)
1472 goto fail;
1473 status = gpio_ensure_requested(desc, gpio);
1474 if (status < 0)
1475 goto fail;
1476
1477 /* now we know the gpio is valid and chip won't vanish */
1478
1479 spin_unlock_irqrestore(&gpio_lock, flags);
1480
1481 might_sleep_if(extra_checks && chip->can_sleep);
1482
1483 return chip->set_debounce(chip, gpio, debounce);
1484
1485fail:
1486 spin_unlock_irqrestore(&gpio_lock, flags);
1487 if (status)
1488 pr_debug("%s: gpio-%d status %d\n",
1489 __func__, gpio, status);
1490
1491 return status;
1492}
1493EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001494
1495/* I/O calls are only valid after configuration completed; the relevant
1496 * "is this a valid GPIO" error checks should already have been done.
1497 *
1498 * "Get" operations are often inlinable as reading a pin value register,
1499 * and masking the relevant bit in that register.
1500 *
1501 * When "set" operations are inlinable, they involve writing that mask to
1502 * one register to set a low value, or a different register to set it high.
1503 * Otherwise locking is needed, so there may be little value to inlining.
1504 *
1505 *------------------------------------------------------------------------
1506 *
1507 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1508 * have requested the GPIO. That can include implicit requesting by
1509 * a direction setting call. Marking a gpio as requested locks its chip
1510 * in memory, guaranteeing that these table lookups need no more locking
1511 * and that gpiochip_remove() will fail.
1512 *
1513 * REVISIT when debugging, consider adding some instrumentation to ensure
1514 * that the GPIO was actually requested.
1515 */
1516
1517/**
1518 * __gpio_get_value() - return a gpio's value
1519 * @gpio: gpio whose value will be returned
1520 * Context: any
1521 *
1522 * This is used directly or indirectly to implement gpio_get_value().
1523 * It returns the zero or nonzero value provided by the associated
1524 * gpio_chip.get() method; or zero if no such method is provided.
1525 */
1526int __gpio_get_value(unsigned gpio)
1527{
1528 struct gpio_chip *chip;
1529
1530 chip = gpio_to_chip(gpio);
1531 WARN_ON(extra_checks && chip->can_sleep);
1532 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1533}
1534EXPORT_SYMBOL_GPL(__gpio_get_value);
1535
1536/**
1537 * __gpio_set_value() - assign a gpio's value
1538 * @gpio: gpio whose value will be assigned
1539 * @value: value to assign
1540 * Context: any
1541 *
1542 * This is used directly or indirectly to implement gpio_set_value().
1543 * It invokes the associated gpio_chip.set() method.
1544 */
1545void __gpio_set_value(unsigned gpio, int value)
1546{
1547 struct gpio_chip *chip;
1548
1549 chip = gpio_to_chip(gpio);
1550 WARN_ON(extra_checks && chip->can_sleep);
1551 chip->set(chip, gpio - chip->base, value);
1552}
1553EXPORT_SYMBOL_GPL(__gpio_set_value);
1554
1555/**
1556 * __gpio_cansleep() - report whether gpio value access will sleep
1557 * @gpio: gpio in question
1558 * Context: any
1559 *
1560 * This is used directly or indirectly to implement gpio_cansleep(). It
1561 * returns nonzero if access reading or writing the GPIO value can sleep.
1562 */
1563int __gpio_cansleep(unsigned gpio)
1564{
1565 struct gpio_chip *chip;
1566
1567 /* only call this on GPIOs that are valid! */
1568 chip = gpio_to_chip(gpio);
1569
1570 return chip->can_sleep;
1571}
1572EXPORT_SYMBOL_GPL(__gpio_cansleep);
1573
David Brownell0f6d5042008-10-15 22:03:14 -07001574/**
1575 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1576 * @gpio: gpio whose IRQ will be returned (already requested)
1577 * Context: any
1578 *
1579 * This is used directly or indirectly to implement gpio_to_irq().
1580 * It returns the number of the IRQ signaled by this (input) GPIO,
1581 * or a negative errno.
1582 */
1583int __gpio_to_irq(unsigned gpio)
1584{
1585 struct gpio_chip *chip;
1586
1587 chip = gpio_to_chip(gpio);
1588 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1589}
1590EXPORT_SYMBOL_GPL(__gpio_to_irq);
1591
David Brownelld2876d02008-02-04 22:28:20 -08001592
1593
1594/* There's no value in making it easy to inline GPIO calls that may sleep.
1595 * Common examples include ones connected to I2C or SPI chips.
1596 */
1597
1598int gpio_get_value_cansleep(unsigned gpio)
1599{
1600 struct gpio_chip *chip;
1601
1602 might_sleep_if(extra_checks);
1603 chip = gpio_to_chip(gpio);
David Brownell978ccaa2008-10-18 20:27:49 -07001604 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001605}
1606EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1607
1608void gpio_set_value_cansleep(unsigned gpio, int value)
1609{
1610 struct gpio_chip *chip;
1611
1612 might_sleep_if(extra_checks);
1613 chip = gpio_to_chip(gpio);
1614 chip->set(chip, gpio - chip->base, value);
1615}
1616EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1617
1618
1619#ifdef CONFIG_DEBUG_FS
1620
David Brownelld2876d02008-02-04 22:28:20 -08001621static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1622{
1623 unsigned i;
1624 unsigned gpio = chip->base;
1625 struct gpio_desc *gdesc = &gpio_desc[gpio];
1626 int is_out;
1627
1628 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1629 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1630 continue;
1631
1632 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08001633 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001634 gpio, gdesc->label,
1635 is_out ? "out" : "in ",
1636 chip->get
1637 ? (chip->get(chip, i) ? "hi" : "lo")
1638 : "? ");
1639
1640 if (!is_out) {
1641 int irq = gpio_to_irq(gpio);
Yinghai Lu08678b02008-08-19 20:50:05 -07001642 struct irq_desc *desc = irq_to_desc(irq);
David Brownelld2876d02008-02-04 22:28:20 -08001643
1644 /* This races with request_irq(), set_irq_type(),
1645 * and set_irq_wake() ... but those are "rare".
1646 *
1647 * More significantly, trigger type flags aren't
1648 * currently maintained by genirq.
1649 */
1650 if (irq >= 0 && desc->action) {
1651 char *trigger;
1652
1653 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1654 case IRQ_TYPE_NONE:
1655 trigger = "(default)";
1656 break;
1657 case IRQ_TYPE_EDGE_FALLING:
1658 trigger = "edge-falling";
1659 break;
1660 case IRQ_TYPE_EDGE_RISING:
1661 trigger = "edge-rising";
1662 break;
1663 case IRQ_TYPE_EDGE_BOTH:
1664 trigger = "edge-both";
1665 break;
1666 case IRQ_TYPE_LEVEL_HIGH:
1667 trigger = "level-high";
1668 break;
1669 case IRQ_TYPE_LEVEL_LOW:
1670 trigger = "level-low";
1671 break;
1672 default:
1673 trigger = "?trigger?";
1674 break;
1675 }
1676
1677 seq_printf(s, " irq-%d %s%s",
1678 irq, trigger,
1679 (desc->status & IRQ_WAKEUP)
1680 ? " wakeup" : "");
1681 }
1682 }
1683
1684 seq_printf(s, "\n");
1685 }
1686}
1687
1688static int gpiolib_show(struct seq_file *s, void *unused)
1689{
1690 struct gpio_chip *chip = NULL;
1691 unsigned gpio;
1692 int started = 0;
1693
1694 /* REVISIT this isn't locked against gpio_chip removal ... */
1695
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001696 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
David Brownelld8f388d2008-07-25 01:46:07 -07001697 struct device *dev;
1698
David Brownelld2876d02008-02-04 22:28:20 -08001699 if (chip == gpio_desc[gpio].chip)
1700 continue;
1701 chip = gpio_desc[gpio].chip;
1702 if (!chip)
1703 continue;
1704
David Brownelld8f388d2008-07-25 01:46:07 -07001705 seq_printf(s, "%sGPIOs %d-%d",
David Brownelld2876d02008-02-04 22:28:20 -08001706 started ? "\n" : "",
David Brownelld8f388d2008-07-25 01:46:07 -07001707 chip->base, chip->base + chip->ngpio - 1);
1708 dev = chip->dev;
1709 if (dev)
1710 seq_printf(s, ", %s/%s",
1711 dev->bus ? dev->bus->name : "no-bus",
Kay Sievers14ab3092009-01-06 10:44:42 -08001712 dev_name(dev));
David Brownelld8f388d2008-07-25 01:46:07 -07001713 if (chip->label)
1714 seq_printf(s, ", %s", chip->label);
1715 if (chip->can_sleep)
1716 seq_printf(s, ", can sleep");
1717 seq_printf(s, ":\n");
1718
David Brownelld2876d02008-02-04 22:28:20 -08001719 started = 1;
1720 if (chip->dbg_show)
1721 chip->dbg_show(s, chip);
1722 else
1723 gpiolib_dbg_show(s, chip);
1724 }
1725 return 0;
1726}
1727
1728static int gpiolib_open(struct inode *inode, struct file *file)
1729{
1730 return single_open(file, gpiolib_show, NULL);
1731}
1732
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001733static const struct file_operations gpiolib_operations = {
David Brownelld2876d02008-02-04 22:28:20 -08001734 .open = gpiolib_open,
1735 .read = seq_read,
1736 .llseek = seq_lseek,
1737 .release = single_release,
1738};
1739
1740static int __init gpiolib_debugfs_init(void)
1741{
1742 /* /sys/kernel/debug/gpio */
1743 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1744 NULL, NULL, &gpiolib_operations);
1745 return 0;
1746}
1747subsys_initcall(gpiolib_debugfs_init);
1748
1749#endif /* DEBUG_FS */