blob: a25ad284a272799d2b4f91634fa8d985e41a885c [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>
David Brownelld2876d02008-02-04 22:28:20 -080012
13
14/* Optional implementation infrastructure for GPIO interfaces.
15 *
16 * Platforms may want to use this if they tend to use very many GPIOs
17 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
18 *
19 * When kernel footprint or instruction count is an issue, simpler
20 * implementations may be preferred. The GPIO programming interface
21 * allows for inlining speed-critical get/set operations for common
22 * cases, so that access to SOC-integrated GPIOs can sometimes cost
23 * only an instruction or two per bit.
24 */
25
26
27/* When debugging, extend minimal trust to callers and platform code.
28 * Also emit diagnostic messages that may help initial bringup, when
29 * board setup or driver bugs are most common.
30 *
31 * Otherwise, minimize overhead in what may be bitbanging codepaths.
32 */
33#ifdef DEBUG
34#define extra_checks 1
35#else
36#define extra_checks 0
37#endif
38
39/* gpio_lock prevents conflicts during gpio_desc[] table updates.
40 * While any GPIO is requested, its gpio_chip is not removable;
41 * each GPIO's "requested" flag serves as a lock and refcount.
42 */
43static DEFINE_SPINLOCK(gpio_lock);
44
45struct gpio_desc {
46 struct gpio_chip *chip;
47 unsigned long flags;
48/* flag symbols are bit numbers */
49#define FLAG_REQUESTED 0
50#define FLAG_IS_OUT 1
Anton Vorontsov169b6a72008-04-28 02:14:47 -070051#define FLAG_RESERVED 2
David Brownelld8f388d2008-07-25 01:46:07 -070052#define FLAG_EXPORT 3 /* protected by sysfs_lock */
53#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070054#define FLAG_TRIG_FALL 5 /* trigger on falling edge */
55#define FLAG_TRIG_RISE 6 /* trigger on rising edge */
Jani Nikula07697462009-12-15 16:46:20 -080056#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070057
58#define PDESC_ID_SHIFT 16 /* add new flags before this one */
59
60#define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
61#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080062
63#ifdef CONFIG_DEBUG_FS
64 const char *label;
65#endif
66};
67static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
68
Daniel Glöcknerff77c352009-09-22 16:46:38 -070069#ifdef CONFIG_GPIO_SYSFS
70struct poll_desc {
71 struct work_struct work;
72 struct sysfs_dirent *value_sd;
73};
74
75static struct idr pdesc_idr;
76#endif
77
David Brownelld2876d02008-02-04 22:28:20 -080078static inline void desc_set_label(struct gpio_desc *d, const char *label)
79{
80#ifdef CONFIG_DEBUG_FS
81 d->label = label;
82#endif
83}
84
85/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
86 * when setting direction, and otherwise illegal. Until board setup code
87 * and drivers use explicit requests everywhere (which won't happen when
88 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -070089 * message should motivate switching to explicit requests... so should
90 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -070091 *
92 * NOTE: the autorequest mechanism is going away; at this point it's
93 * only "legal" in the sense that (old) code using it won't break yet,
94 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -080095 */
David Brownell35e8bb52008-10-15 22:03:16 -070096static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
David Brownelld2876d02008-02-04 22:28:20 -080097{
David Brownell8a0cecf2009-04-02 16:57:06 -070098 const struct gpio_chip *chip = desc->chip;
99 const int gpio = chip->base + offset;
David Brownell35e8bb52008-10-15 22:03:16 -0700100
David Brownell8a0cecf2009-04-02 16:57:06 -0700101 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
102 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700103 if (!try_module_get(chip->owner)) {
104 pr_err("GPIO-%d: module can't be gotten \n", gpio);
105 clear_bit(FLAG_REQUESTED, &desc->flags);
106 /* lose */
107 return -EIO;
108 }
David Brownelld2876d02008-02-04 22:28:20 -0800109 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700110 /* caller must chip->request() w/o spinlock */
111 if (chip->request)
112 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800113 }
David Brownell35e8bb52008-10-15 22:03:16 -0700114 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800115}
116
117/* caller holds gpio_lock *OR* gpio is marked as requested */
118static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
119{
120 return gpio_desc[gpio].chip;
121}
122
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700123/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
124static int gpiochip_find_base(int ngpio)
125{
126 int i;
127 int spare = 0;
128 int base = -ENOSPC;
129
130 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700131 struct gpio_desc *desc = &gpio_desc[i];
132 struct gpio_chip *chip = desc->chip;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700133
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700134 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700135 spare++;
136 if (spare == ngpio) {
137 base = i;
138 break;
139 }
140 } else {
141 spare = 0;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700142 if (chip)
143 i -= chip->ngpio - 1;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700144 }
145 }
146
147 if (gpio_is_valid(base))
148 pr_debug("%s: found new base at %d\n", __func__, base);
149 return base;
150}
151
David Brownelld2876d02008-02-04 22:28:20 -0800152/**
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700153 * gpiochip_reserve() - reserve range of gpios to use with platform code only
154 * @start: starting gpio number
155 * @ngpio: number of gpios to reserve
156 * Context: platform init, potentially before irqs or kmalloc will work
157 *
158 * Returns a negative errno if any gpio within the range is already reserved
159 * or registered, else returns zero as a success code. Use this function
160 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
161 * for example because its driver support is not yet loaded.
162 */
163int __init gpiochip_reserve(int start, int ngpio)
164{
165 int ret = 0;
166 unsigned long flags;
167 int i;
168
Trent Piephobff5fda2008-05-23 13:04:44 -0700169 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700170 return -EINVAL;
171
172 spin_lock_irqsave(&gpio_lock, flags);
173
174 for (i = start; i < start + ngpio; i++) {
175 struct gpio_desc *desc = &gpio_desc[i];
176
177 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
178 ret = -EBUSY;
179 goto err;
180 }
181
182 set_bit(FLAG_RESERVED, &desc->flags);
183 }
184
185 pr_debug("%s: reserved gpios from %d to %d\n",
186 __func__, start, start + ngpio - 1);
187err:
188 spin_unlock_irqrestore(&gpio_lock, flags);
189
190 return ret;
191}
192
David Brownelld8f388d2008-07-25 01:46:07 -0700193#ifdef CONFIG_GPIO_SYSFS
194
195/* lock protects against unexport_gpio() being called while
196 * sysfs files are active.
197 */
198static DEFINE_MUTEX(sysfs_lock);
199
200/*
201 * /sys/class/gpio/gpioN... only for GPIOs that are exported
202 * /direction
203 * * MAY BE OMITTED if kernel won't allow direction changes
204 * * is read/write as "in" or "out"
205 * * may also be written as "high" or "low", initializing
206 * output value as specified ("out" implies "low")
207 * /value
208 * * always readable, subject to hardware behavior
209 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700210 * /edge
211 * * configures behavior of poll(2) on /value
212 * * available only if pin can generate IRQs on input
213 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800214 * /active_low
215 * * configures polarity of /value
216 * * is read/write as zero/nonzero
217 * * also affects existing and subsequent "falling" and "rising"
218 * /edge configuration
David Brownelld8f388d2008-07-25 01:46:07 -0700219 */
220
221static ssize_t gpio_direction_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
224 const struct gpio_desc *desc = dev_get_drvdata(dev);
225 ssize_t status;
226
227 mutex_lock(&sysfs_lock);
228
229 if (!test_bit(FLAG_EXPORT, &desc->flags))
230 status = -EIO;
231 else
232 status = sprintf(buf, "%s\n",
233 test_bit(FLAG_IS_OUT, &desc->flags)
234 ? "out" : "in");
235
236 mutex_unlock(&sysfs_lock);
237 return status;
238}
239
240static ssize_t gpio_direction_store(struct device *dev,
241 struct device_attribute *attr, const char *buf, size_t size)
242{
243 const struct gpio_desc *desc = dev_get_drvdata(dev);
244 unsigned gpio = desc - gpio_desc;
245 ssize_t status;
246
247 mutex_lock(&sysfs_lock);
248
249 if (!test_bit(FLAG_EXPORT, &desc->flags))
250 status = -EIO;
251 else if (sysfs_streq(buf, "high"))
252 status = gpio_direction_output(gpio, 1);
253 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
254 status = gpio_direction_output(gpio, 0);
255 else if (sysfs_streq(buf, "in"))
256 status = gpio_direction_input(gpio);
257 else
258 status = -EINVAL;
259
260 mutex_unlock(&sysfs_lock);
261 return status ? : size;
262}
263
Jani Nikula07697462009-12-15 16:46:20 -0800264static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700265 gpio_direction_show, gpio_direction_store);
266
267static ssize_t gpio_value_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
269{
270 const struct gpio_desc *desc = dev_get_drvdata(dev);
271 unsigned gpio = desc - gpio_desc;
272 ssize_t status;
273
274 mutex_lock(&sysfs_lock);
275
Jani Nikula07697462009-12-15 16:46:20 -0800276 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700277 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800278 } else {
279 int value;
280
281 value = !!gpio_get_value_cansleep(gpio);
282 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
283 value = !value;
284
285 status = sprintf(buf, "%d\n", value);
286 }
David Brownelld8f388d2008-07-25 01:46:07 -0700287
288 mutex_unlock(&sysfs_lock);
289 return status;
290}
291
292static ssize_t gpio_value_store(struct device *dev,
293 struct device_attribute *attr, const char *buf, size_t size)
294{
295 const struct gpio_desc *desc = dev_get_drvdata(dev);
296 unsigned gpio = desc - gpio_desc;
297 ssize_t status;
298
299 mutex_lock(&sysfs_lock);
300
301 if (!test_bit(FLAG_EXPORT, &desc->flags))
302 status = -EIO;
303 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
304 status = -EPERM;
305 else {
306 long value;
307
308 status = strict_strtol(buf, 0, &value);
309 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800310 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
311 value = !value;
David Brownelld8f388d2008-07-25 01:46:07 -0700312 gpio_set_value_cansleep(gpio, value != 0);
313 status = size;
314 }
315 }
316
317 mutex_unlock(&sysfs_lock);
318 return status;
319}
320
Jani Nikula07697462009-12-15 16:46:20 -0800321static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700322 gpio_value_show, gpio_value_store);
323
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700324static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
325{
326 struct work_struct *work = priv;
327
328 schedule_work(work);
329 return IRQ_HANDLED;
330}
331
332static void gpio_notify_sysfs(struct work_struct *work)
333{
334 struct poll_desc *pdesc;
335
336 pdesc = container_of(work, struct poll_desc, work);
337 sysfs_notify_dirent(pdesc->value_sd);
338}
339
340static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
341 unsigned long gpio_flags)
342{
343 struct poll_desc *pdesc;
344 unsigned long irq_flags;
345 int ret, irq, id;
346
347 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
348 return 0;
349
350 irq = gpio_to_irq(desc - gpio_desc);
351 if (irq < 0)
352 return -EIO;
353
354 id = desc->flags >> PDESC_ID_SHIFT;
355 pdesc = idr_find(&pdesc_idr, id);
356 if (pdesc) {
357 free_irq(irq, &pdesc->work);
358 cancel_work_sync(&pdesc->work);
359 }
360
361 desc->flags &= ~GPIO_TRIGGER_MASK;
362
363 if (!gpio_flags) {
364 ret = 0;
365 goto free_sd;
366 }
367
368 irq_flags = IRQF_SHARED;
369 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800370 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
371 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700372 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800373 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
374 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700375
376 if (!pdesc) {
377 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
378 if (!pdesc) {
379 ret = -ENOMEM;
380 goto err_out;
381 }
382
383 do {
384 ret = -ENOMEM;
385 if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
386 ret = idr_get_new_above(&pdesc_idr,
387 pdesc, 1, &id);
388 } while (ret == -EAGAIN);
389
390 if (ret)
391 goto free_mem;
392
393 desc->flags &= GPIO_FLAGS_MASK;
394 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
395
396 if (desc->flags >> PDESC_ID_SHIFT != id) {
397 ret = -ERANGE;
398 goto free_id;
399 }
400
401 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
402 if (!pdesc->value_sd) {
403 ret = -ENODEV;
404 goto free_id;
405 }
406 INIT_WORK(&pdesc->work, gpio_notify_sysfs);
407 }
408
409 ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
410 "gpiolib", &pdesc->work);
411 if (ret)
412 goto free_sd;
413
414 desc->flags |= gpio_flags;
415 return 0;
416
417free_sd:
418 sysfs_put(pdesc->value_sd);
419free_id:
420 idr_remove(&pdesc_idr, id);
421 desc->flags &= GPIO_FLAGS_MASK;
422free_mem:
423 kfree(pdesc);
424err_out:
425 return ret;
426}
427
428static const struct {
429 const char *name;
430 unsigned long flags;
431} trigger_types[] = {
432 { "none", 0 },
433 { "falling", BIT(FLAG_TRIG_FALL) },
434 { "rising", BIT(FLAG_TRIG_RISE) },
435 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
436};
437
438static ssize_t gpio_edge_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
441 const struct gpio_desc *desc = dev_get_drvdata(dev);
442 ssize_t status;
443
444 mutex_lock(&sysfs_lock);
445
446 if (!test_bit(FLAG_EXPORT, &desc->flags))
447 status = -EIO;
448 else {
449 int i;
450
451 status = 0;
452 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
453 if ((desc->flags & GPIO_TRIGGER_MASK)
454 == trigger_types[i].flags) {
455 status = sprintf(buf, "%s\n",
456 trigger_types[i].name);
457 break;
458 }
459 }
460
461 mutex_unlock(&sysfs_lock);
462 return status;
463}
464
465static ssize_t gpio_edge_store(struct device *dev,
466 struct device_attribute *attr, const char *buf, size_t size)
467{
468 struct gpio_desc *desc = dev_get_drvdata(dev);
469 ssize_t status;
470 int i;
471
472 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
473 if (sysfs_streq(trigger_types[i].name, buf))
474 goto found;
475 return -EINVAL;
476
477found:
478 mutex_lock(&sysfs_lock);
479
480 if (!test_bit(FLAG_EXPORT, &desc->flags))
481 status = -EIO;
482 else {
483 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
484 if (!status)
485 status = size;
486 }
487
488 mutex_unlock(&sysfs_lock);
489
490 return status;
491}
492
493static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
494
Jani Nikula07697462009-12-15 16:46:20 -0800495static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
496 int value)
497{
498 int status = 0;
499
500 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
501 return 0;
502
503 if (value)
504 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
505 else
506 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
507
508 /* reconfigure poll(2) support if enabled on one edge only */
509 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
510 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
511 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
512
513 gpio_setup_irq(desc, dev, 0);
514 status = gpio_setup_irq(desc, dev, trigger_flags);
515 }
516
517 return status;
518}
519
520static ssize_t gpio_active_low_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
522{
523 const struct gpio_desc *desc = dev_get_drvdata(dev);
524 ssize_t status;
525
526 mutex_lock(&sysfs_lock);
527
528 if (!test_bit(FLAG_EXPORT, &desc->flags))
529 status = -EIO;
530 else
531 status = sprintf(buf, "%d\n",
532 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
533
534 mutex_unlock(&sysfs_lock);
535
536 return status;
537}
538
539static ssize_t gpio_active_low_store(struct device *dev,
540 struct device_attribute *attr, const char *buf, size_t size)
541{
542 struct gpio_desc *desc = dev_get_drvdata(dev);
543 ssize_t status;
544
545 mutex_lock(&sysfs_lock);
546
547 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
548 status = -EIO;
549 } else {
550 long value;
551
552 status = strict_strtol(buf, 0, &value);
553 if (status == 0)
554 status = sysfs_set_active_low(desc, dev, value != 0);
555 }
556
557 mutex_unlock(&sysfs_lock);
558
559 return status ? : size;
560}
561
562static const DEVICE_ATTR(active_low, 0644,
563 gpio_active_low_show, gpio_active_low_store);
564
David Brownelld8f388d2008-07-25 01:46:07 -0700565static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d2008-07-25 01:46:07 -0700566 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800567 &dev_attr_active_low.attr,
David Brownelld8f388d2008-07-25 01:46:07 -0700568 NULL,
569};
570
571static const struct attribute_group gpio_attr_group = {
572 .attrs = (struct attribute **) gpio_attrs,
573};
574
575/*
576 * /sys/class/gpio/gpiochipN/
577 * /base ... matching gpio_chip.base (N)
578 * /label ... matching gpio_chip.label
579 * /ngpio ... matching gpio_chip.ngpio
580 */
581
582static ssize_t chip_base_show(struct device *dev,
583 struct device_attribute *attr, char *buf)
584{
585 const struct gpio_chip *chip = dev_get_drvdata(dev);
586
587 return sprintf(buf, "%d\n", chip->base);
588}
589static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
590
591static ssize_t chip_label_show(struct device *dev,
592 struct device_attribute *attr, char *buf)
593{
594 const struct gpio_chip *chip = dev_get_drvdata(dev);
595
596 return sprintf(buf, "%s\n", chip->label ? : "");
597}
598static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
599
600static ssize_t chip_ngpio_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
603 const struct gpio_chip *chip = dev_get_drvdata(dev);
604
605 return sprintf(buf, "%u\n", chip->ngpio);
606}
607static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
608
609static const struct attribute *gpiochip_attrs[] = {
610 &dev_attr_base.attr,
611 &dev_attr_label.attr,
612 &dev_attr_ngpio.attr,
613 NULL,
614};
615
616static const struct attribute_group gpiochip_attr_group = {
617 .attrs = (struct attribute **) gpiochip_attrs,
618};
619
620/*
621 * /sys/class/gpio/export ... write-only
622 * integer N ... number of GPIO to export (full access)
623 * /sys/class/gpio/unexport ... write-only
624 * integer N ... number of GPIO to unexport
625 */
626static ssize_t export_store(struct class *class, const char *buf, size_t len)
627{
628 long gpio;
629 int status;
630
631 status = strict_strtol(buf, 0, &gpio);
632 if (status < 0)
633 goto done;
634
635 /* No extra locking here; FLAG_SYSFS just signifies that the
636 * request and export were done by on behalf of userspace, so
637 * they may be undone on its behalf too.
638 */
639
640 status = gpio_request(gpio, "sysfs");
641 if (status < 0)
642 goto done;
643
644 status = gpio_export(gpio, true);
645 if (status < 0)
646 gpio_free(gpio);
647 else
648 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
649
650done:
651 if (status)
652 pr_debug("%s: status %d\n", __func__, status);
653 return status ? : len;
654}
655
656static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
657{
658 long gpio;
659 int status;
660
661 status = strict_strtol(buf, 0, &gpio);
662 if (status < 0)
663 goto done;
664
665 status = -EINVAL;
666
667 /* reject bogus commands (gpio_unexport ignores them) */
668 if (!gpio_is_valid(gpio))
669 goto done;
670
671 /* No extra locking here; FLAG_SYSFS just signifies that the
672 * request and export were done by on behalf of userspace, so
673 * they may be undone on its behalf too.
674 */
675 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
676 status = 0;
677 gpio_free(gpio);
678 }
679done:
680 if (status)
681 pr_debug("%s: status %d\n", __func__, status);
682 return status ? : len;
683}
684
685static struct class_attribute gpio_class_attrs[] = {
686 __ATTR(export, 0200, NULL, export_store),
687 __ATTR(unexport, 0200, NULL, unexport_store),
688 __ATTR_NULL,
689};
690
691static struct class gpio_class = {
692 .name = "gpio",
693 .owner = THIS_MODULE,
694
695 .class_attrs = gpio_class_attrs,
696};
697
698
699/**
700 * gpio_export - export a GPIO through sysfs
701 * @gpio: gpio to make available, already requested
702 * @direction_may_change: true if userspace may change gpio direction
703 * Context: arch_initcall or later
704 *
705 * When drivers want to make a GPIO accessible to userspace after they
706 * have requested it -- perhaps while debugging, or as part of their
707 * public interface -- they may use this routine. If the GPIO can
708 * change direction (some can't) and the caller allows it, userspace
709 * will see "direction" sysfs attribute which may be used to change
710 * the gpio's direction. A "value" attribute will always be provided.
711 *
712 * Returns zero on success, else an error.
713 */
714int gpio_export(unsigned gpio, bool direction_may_change)
715{
716 unsigned long flags;
717 struct gpio_desc *desc;
718 int status = -EINVAL;
Daniel Silverstone926b6632009-04-02 16:57:05 -0700719 char *ioname = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -0700720
721 /* can't export until sysfs is available ... */
722 if (!gpio_class.p) {
723 pr_debug("%s: called too early!\n", __func__);
724 return -ENOENT;
725 }
726
727 if (!gpio_is_valid(gpio))
728 goto done;
729
730 mutex_lock(&sysfs_lock);
731
732 spin_lock_irqsave(&gpio_lock, flags);
733 desc = &gpio_desc[gpio];
734 if (test_bit(FLAG_REQUESTED, &desc->flags)
735 && !test_bit(FLAG_EXPORT, &desc->flags)) {
736 status = 0;
737 if (!desc->chip->direction_input
738 || !desc->chip->direction_output)
739 direction_may_change = false;
740 }
741 spin_unlock_irqrestore(&gpio_lock, flags);
742
Daniel Silverstone926b6632009-04-02 16:57:05 -0700743 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
744 ioname = desc->chip->names[gpio - desc->chip->base];
745
David Brownelld8f388d2008-07-25 01:46:07 -0700746 if (status == 0) {
747 struct device *dev;
748
749 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700750 desc, ioname ? ioname : "gpio%d", gpio);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800751 if (!IS_ERR(dev)) {
Jani Nikula07697462009-12-15 16:46:20 -0800752 status = sysfs_create_group(&dev->kobj,
David Brownelld8f388d2008-07-25 01:46:07 -0700753 &gpio_attr_group);
Jani Nikula07697462009-12-15 16:46:20 -0800754
755 if (!status && direction_may_change)
David Brownelld8f388d2008-07-25 01:46:07 -0700756 status = device_create_file(dev,
Jani Nikula07697462009-12-15 16:46:20 -0800757 &dev_attr_direction);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700758
759 if (!status && gpio_to_irq(gpio) >= 0
760 && (direction_may_change
761 || !test_bit(FLAG_IS_OUT,
762 &desc->flags)))
763 status = device_create_file(dev,
764 &dev_attr_edge);
765
David Brownelld8f388d2008-07-25 01:46:07 -0700766 if (status != 0)
767 device_unregister(dev);
768 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800769 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700770 if (status == 0)
771 set_bit(FLAG_EXPORT, &desc->flags);
772 }
773
774 mutex_unlock(&sysfs_lock);
775
776done:
777 if (status)
778 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
779
780 return status;
781}
782EXPORT_SYMBOL_GPL(gpio_export);
783
784static int match_export(struct device *dev, void *data)
785{
786 return dev_get_drvdata(dev) == data;
787}
788
789/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700790 * gpio_export_link - create a sysfs link to an exported GPIO node
791 * @dev: device under which to create symlink
792 * @name: name of the symlink
793 * @gpio: gpio to create symlink to, already exported
794 *
795 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
796 * node. Caller is responsible for unlinking.
797 *
798 * Returns zero on success, else an error.
799 */
800int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
801{
802 struct gpio_desc *desc;
803 int status = -EINVAL;
804
805 if (!gpio_is_valid(gpio))
806 goto done;
807
808 mutex_lock(&sysfs_lock);
809
810 desc = &gpio_desc[gpio];
811
812 if (test_bit(FLAG_EXPORT, &desc->flags)) {
813 struct device *tdev;
814
815 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
816 if (tdev != NULL) {
817 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
818 name);
819 } else {
820 status = -ENODEV;
821 }
822 }
823
824 mutex_unlock(&sysfs_lock);
825
826done:
827 if (status)
828 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
829
830 return status;
831}
832EXPORT_SYMBOL_GPL(gpio_export_link);
833
Jani Nikula07697462009-12-15 16:46:20 -0800834
835/**
836 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
837 * @gpio: gpio to change
838 * @value: non-zero to use active low, i.e. inverted values
839 *
840 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
841 * The GPIO does not have to be exported yet. If poll(2) support has
842 * been enabled for either rising or falling edge, it will be
843 * reconfigured to follow the new polarity.
844 *
845 * Returns zero on success, else an error.
846 */
847int gpio_sysfs_set_active_low(unsigned gpio, int value)
848{
849 struct gpio_desc *desc;
850 struct device *dev = NULL;
851 int status = -EINVAL;
852
853 if (!gpio_is_valid(gpio))
854 goto done;
855
856 mutex_lock(&sysfs_lock);
857
858 desc = &gpio_desc[gpio];
859
860 if (test_bit(FLAG_EXPORT, &desc->flags)) {
861 struct device *dev;
862
863 dev = class_find_device(&gpio_class, NULL, desc, match_export);
864 if (dev == NULL) {
865 status = -ENODEV;
866 goto unlock;
867 }
868 }
869
870 status = sysfs_set_active_low(desc, dev, value);
871
872unlock:
873 mutex_unlock(&sysfs_lock);
874
875done:
876 if (status)
877 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
878
879 return status;
880}
881EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
882
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700883/**
David Brownelld8f388d2008-07-25 01:46:07 -0700884 * gpio_unexport - reverse effect of gpio_export()
885 * @gpio: gpio to make unavailable
886 *
887 * This is implicit on gpio_free().
888 */
889void gpio_unexport(unsigned gpio)
890{
891 struct gpio_desc *desc;
892 int status = -EINVAL;
893
894 if (!gpio_is_valid(gpio))
895 goto done;
896
897 mutex_lock(&sysfs_lock);
898
899 desc = &gpio_desc[gpio];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700900
David Brownelld8f388d2008-07-25 01:46:07 -0700901 if (test_bit(FLAG_EXPORT, &desc->flags)) {
902 struct device *dev = NULL;
903
904 dev = class_find_device(&gpio_class, NULL, desc, match_export);
905 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700906 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700907 clear_bit(FLAG_EXPORT, &desc->flags);
908 put_device(dev);
909 device_unregister(dev);
910 status = 0;
911 } else
912 status = -ENODEV;
913 }
914
915 mutex_unlock(&sysfs_lock);
916done:
917 if (status)
918 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
919}
920EXPORT_SYMBOL_GPL(gpio_unexport);
921
922static int gpiochip_export(struct gpio_chip *chip)
923{
924 int status;
925 struct device *dev;
926
927 /* Many systems register gpio chips for SOC support very early,
928 * before driver model support is available. In those cases we
929 * export this later, in gpiolib_sysfs_init() ... here we just
930 * verify that _some_ field of gpio_class got initialized.
931 */
932 if (!gpio_class.p)
933 return 0;
934
935 /* use chip->base for the ID; it's already known to be unique */
936 mutex_lock(&sysfs_lock);
937 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
938 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800939 if (!IS_ERR(dev)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700940 status = sysfs_create_group(&dev->kobj,
941 &gpiochip_attr_group);
942 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800943 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700944 chip->exported = (status == 0);
945 mutex_unlock(&sysfs_lock);
946
947 if (status) {
948 unsigned long flags;
949 unsigned gpio;
950
951 spin_lock_irqsave(&gpio_lock, flags);
952 gpio = chip->base;
953 while (gpio_desc[gpio].chip == chip)
954 gpio_desc[gpio++].chip = NULL;
955 spin_unlock_irqrestore(&gpio_lock, flags);
956
957 pr_debug("%s: chip %s status %d\n", __func__,
958 chip->label, status);
959 }
960
961 return status;
962}
963
964static void gpiochip_unexport(struct gpio_chip *chip)
965{
966 int status;
967 struct device *dev;
968
969 mutex_lock(&sysfs_lock);
970 dev = class_find_device(&gpio_class, NULL, chip, match_export);
971 if (dev) {
972 put_device(dev);
973 device_unregister(dev);
974 chip->exported = 0;
975 status = 0;
976 } else
977 status = -ENODEV;
978 mutex_unlock(&sysfs_lock);
979
980 if (status)
981 pr_debug("%s: chip %s status %d\n", __func__,
982 chip->label, status);
983}
984
985static int __init gpiolib_sysfs_init(void)
986{
987 int status;
988 unsigned long flags;
989 unsigned gpio;
990
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700991 idr_init(&pdesc_idr);
992
David Brownelld8f388d2008-07-25 01:46:07 -0700993 status = class_register(&gpio_class);
994 if (status < 0)
995 return status;
996
997 /* Scan and register the gpio_chips which registered very
998 * early (e.g. before the class_register above was called).
999 *
1000 * We run before arch_initcall() so chip->dev nodes can have
1001 * registered, and so arch_initcall() can always gpio_export().
1002 */
1003 spin_lock_irqsave(&gpio_lock, flags);
1004 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
1005 struct gpio_chip *chip;
1006
1007 chip = gpio_desc[gpio].chip;
1008 if (!chip || chip->exported)
1009 continue;
1010
1011 spin_unlock_irqrestore(&gpio_lock, flags);
1012 status = gpiochip_export(chip);
1013 spin_lock_irqsave(&gpio_lock, flags);
1014 }
1015 spin_unlock_irqrestore(&gpio_lock, flags);
1016
1017
1018 return status;
1019}
1020postcore_initcall(gpiolib_sysfs_init);
1021
1022#else
1023static inline int gpiochip_export(struct gpio_chip *chip)
1024{
1025 return 0;
1026}
1027
1028static inline void gpiochip_unexport(struct gpio_chip *chip)
1029{
1030}
1031
1032#endif /* CONFIG_GPIO_SYSFS */
1033
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001034/**
David Brownelld2876d02008-02-04 22:28:20 -08001035 * gpiochip_add() - register a gpio_chip
1036 * @chip: the chip to register, with chip->base initialized
1037 * Context: potentially before irqs or kmalloc will work
1038 *
1039 * Returns a negative errno if the chip can't be registered, such as
1040 * because the chip->base is invalid or already associated with a
1041 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001042 *
David Brownelld8f388d2008-07-25 01:46:07 -07001043 * When gpiochip_add() is called very early during boot, so that GPIOs
1044 * can be freely used, the chip->dev device must be registered before
1045 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1046 * for GPIOs will fail rudely.
1047 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001048 * If chip->base is negative, this requests dynamic assignment of
1049 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001050 */
1051int gpiochip_add(struct gpio_chip *chip)
1052{
1053 unsigned long flags;
1054 int status = 0;
1055 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001056 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001057
Trent Piephobff5fda2008-05-23 13:04:44 -07001058 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001059 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001060 status = -EINVAL;
1061 goto fail;
1062 }
1063
1064 spin_lock_irqsave(&gpio_lock, flags);
1065
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001066 if (base < 0) {
1067 base = gpiochip_find_base(chip->ngpio);
1068 if (base < 0) {
1069 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -07001070 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001071 }
1072 chip->base = base;
1073 }
1074
David Brownelld2876d02008-02-04 22:28:20 -08001075 /* these GPIO numbers must not be managed by another gpio_chip */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001076 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001077 if (gpio_desc[id].chip != NULL) {
1078 status = -EBUSY;
1079 break;
1080 }
1081 }
1082 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001083 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001084 gpio_desc[id].chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001085
1086 /* REVISIT: most hardware initializes GPIOs as
1087 * inputs (often with pullups enabled) so power
1088 * usage is minimized. Linux code should set the
1089 * gpio direction first thing; but until it does,
1090 * we may expose the wrong direction in sysfs.
1091 */
1092 gpio_desc[id].flags = !chip->direction_input
1093 ? (1 << FLAG_IS_OUT)
1094 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001095 }
1096 }
1097
David Brownelld8f388d2008-07-25 01:46:07 -07001098unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001099 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001100 if (status == 0)
1101 status = gpiochip_export(chip);
David Brownelld2876d02008-02-04 22:28:20 -08001102fail:
1103 /* failures here can mean systems won't boot... */
1104 if (status)
1105 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
Trent Piephobff5fda2008-05-23 13:04:44 -07001106 chip->base, chip->base + chip->ngpio - 1,
David Brownelld2876d02008-02-04 22:28:20 -08001107 chip->label ? : "generic");
1108 return status;
1109}
1110EXPORT_SYMBOL_GPL(gpiochip_add);
1111
1112/**
1113 * gpiochip_remove() - unregister a gpio_chip
1114 * @chip: the chip to unregister
1115 *
1116 * A gpio_chip with any GPIOs still requested may not be removed.
1117 */
1118int gpiochip_remove(struct gpio_chip *chip)
1119{
1120 unsigned long flags;
1121 int status = 0;
1122 unsigned id;
1123
1124 spin_lock_irqsave(&gpio_lock, flags);
1125
1126 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1127 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1128 status = -EBUSY;
1129 break;
1130 }
1131 }
1132 if (status == 0) {
1133 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1134 gpio_desc[id].chip = NULL;
1135 }
1136
1137 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001138
1139 if (status == 0)
1140 gpiochip_unexport(chip);
1141
David Brownelld2876d02008-02-04 22:28:20 -08001142 return status;
1143}
1144EXPORT_SYMBOL_GPL(gpiochip_remove);
1145
1146
1147/* These "optional" allocation calls help prevent drivers from stomping
1148 * on each other, and help provide better diagnostics in debugfs.
1149 * They're called even less than the "set direction" calls.
1150 */
1151int gpio_request(unsigned gpio, const char *label)
1152{
1153 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001154 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001155 int status = -EINVAL;
1156 unsigned long flags;
1157
1158 spin_lock_irqsave(&gpio_lock, flags);
1159
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001160 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001161 goto done;
1162 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001163 chip = desc->chip;
1164 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001165 goto done;
1166
David Brownell35e8bb52008-10-15 22:03:16 -07001167 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001168 goto done;
1169
David Brownelld2876d02008-02-04 22:28:20 -08001170 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001171 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001172 */
1173
1174 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1175 desc_set_label(desc, label ? : "?");
1176 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001177 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001178 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001179 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001180 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001181 }
1182
1183 if (chip->request) {
1184 /* chip->request may sleep */
1185 spin_unlock_irqrestore(&gpio_lock, flags);
1186 status = chip->request(chip, gpio - chip->base);
1187 spin_lock_irqsave(&gpio_lock, flags);
1188
1189 if (status < 0) {
1190 desc_set_label(desc, NULL);
1191 module_put(chip->owner);
1192 clear_bit(FLAG_REQUESTED, &desc->flags);
1193 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001194 }
David Brownelld2876d02008-02-04 22:28:20 -08001195
1196done:
1197 if (status)
1198 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1199 gpio, label ? : "?", status);
1200 spin_unlock_irqrestore(&gpio_lock, flags);
1201 return status;
1202}
1203EXPORT_SYMBOL_GPL(gpio_request);
1204
1205void gpio_free(unsigned gpio)
1206{
1207 unsigned long flags;
1208 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001209 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001210
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001211 might_sleep();
1212
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001213 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -08001214 WARN_ON(extra_checks);
1215 return;
1216 }
1217
David Brownelld8f388d2008-07-25 01:46:07 -07001218 gpio_unexport(gpio);
1219
David Brownelld2876d02008-02-04 22:28:20 -08001220 spin_lock_irqsave(&gpio_lock, flags);
1221
1222 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001223 chip = desc->chip;
1224 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1225 if (chip->free) {
1226 spin_unlock_irqrestore(&gpio_lock, flags);
1227 might_sleep_if(extra_checks && chip->can_sleep);
1228 chip->free(chip, gpio - chip->base);
1229 spin_lock_irqsave(&gpio_lock, flags);
1230 }
David Brownelld2876d02008-02-04 22:28:20 -08001231 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001232 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001233 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001234 clear_bit(FLAG_REQUESTED, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001235 } else
David Brownelld2876d02008-02-04 22:28:20 -08001236 WARN_ON(extra_checks);
1237
1238 spin_unlock_irqrestore(&gpio_lock, flags);
1239}
1240EXPORT_SYMBOL_GPL(gpio_free);
1241
1242
1243/**
1244 * gpiochip_is_requested - return string iff signal was requested
1245 * @chip: controller managing the signal
1246 * @offset: of signal within controller's 0..(ngpio - 1) range
1247 *
1248 * Returns NULL if the GPIO is not currently requested, else a string.
1249 * If debugfs support is enabled, the string returned is the label passed
1250 * to gpio_request(); otherwise it is a meaningless constant.
1251 *
1252 * This function is for use by GPIO controller drivers. The label can
1253 * help with diagnostics, and knowing that the signal is used as a GPIO
1254 * can help avoid accidentally multiplexing it to another controller.
1255 */
1256const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1257{
1258 unsigned gpio = chip->base + offset;
1259
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001260 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001261 return NULL;
1262 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1263 return NULL;
1264#ifdef CONFIG_DEBUG_FS
1265 return gpio_desc[gpio].label;
1266#else
1267 return "?";
1268#endif
1269}
1270EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1271
1272
1273/* Drivers MUST set GPIO direction before making get/set calls. In
1274 * some cases this is done in early boot, before IRQs are enabled.
1275 *
1276 * As a rule these aren't called more than once (except for drivers
1277 * using the open-drain emulation idiom) so these are natural places
1278 * to accumulate extra debugging checks. Note that we can't (yet)
1279 * rely on gpio_request() having been called beforehand.
1280 */
1281
1282int gpio_direction_input(unsigned gpio)
1283{
1284 unsigned long flags;
1285 struct gpio_chip *chip;
1286 struct gpio_desc *desc = &gpio_desc[gpio];
1287 int status = -EINVAL;
1288
1289 spin_lock_irqsave(&gpio_lock, flags);
1290
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001291 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001292 goto fail;
1293 chip = desc->chip;
1294 if (!chip || !chip->get || !chip->direction_input)
1295 goto fail;
1296 gpio -= chip->base;
1297 if (gpio >= chip->ngpio)
1298 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001299 status = gpio_ensure_requested(desc, gpio);
1300 if (status < 0)
1301 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001302
1303 /* now we know the gpio is valid and chip won't vanish */
1304
1305 spin_unlock_irqrestore(&gpio_lock, flags);
1306
1307 might_sleep_if(extra_checks && chip->can_sleep);
1308
David Brownell35e8bb52008-10-15 22:03:16 -07001309 if (status) {
1310 status = chip->request(chip, gpio);
1311 if (status < 0) {
1312 pr_debug("GPIO-%d: chip request fail, %d\n",
1313 chip->base + gpio, status);
1314 /* and it's not available to anyone else ...
1315 * gpio_request() is the fully clean solution.
1316 */
1317 goto lose;
1318 }
1319 }
1320
David Brownelld2876d02008-02-04 22:28:20 -08001321 status = chip->direction_input(chip, gpio);
1322 if (status == 0)
1323 clear_bit(FLAG_IS_OUT, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001324lose:
David Brownelld2876d02008-02-04 22:28:20 -08001325 return status;
1326fail:
1327 spin_unlock_irqrestore(&gpio_lock, flags);
1328 if (status)
1329 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001330 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001331 return status;
1332}
1333EXPORT_SYMBOL_GPL(gpio_direction_input);
1334
1335int gpio_direction_output(unsigned gpio, int value)
1336{
1337 unsigned long flags;
1338 struct gpio_chip *chip;
1339 struct gpio_desc *desc = &gpio_desc[gpio];
1340 int status = -EINVAL;
1341
1342 spin_lock_irqsave(&gpio_lock, flags);
1343
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001344 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001345 goto fail;
1346 chip = desc->chip;
1347 if (!chip || !chip->set || !chip->direction_output)
1348 goto fail;
1349 gpio -= chip->base;
1350 if (gpio >= chip->ngpio)
1351 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001352 status = gpio_ensure_requested(desc, gpio);
1353 if (status < 0)
1354 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001355
1356 /* now we know the gpio is valid and chip won't vanish */
1357
1358 spin_unlock_irqrestore(&gpio_lock, flags);
1359
1360 might_sleep_if(extra_checks && chip->can_sleep);
1361
David Brownell35e8bb52008-10-15 22:03:16 -07001362 if (status) {
1363 status = chip->request(chip, gpio);
1364 if (status < 0) {
1365 pr_debug("GPIO-%d: chip request fail, %d\n",
1366 chip->base + gpio, status);
1367 /* and it's not available to anyone else ...
1368 * gpio_request() is the fully clean solution.
1369 */
1370 goto lose;
1371 }
1372 }
1373
David Brownelld2876d02008-02-04 22:28:20 -08001374 status = chip->direction_output(chip, gpio, value);
1375 if (status == 0)
1376 set_bit(FLAG_IS_OUT, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001377lose:
David Brownelld2876d02008-02-04 22:28:20 -08001378 return status;
1379fail:
1380 spin_unlock_irqrestore(&gpio_lock, flags);
1381 if (status)
1382 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001383 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001384 return status;
1385}
1386EXPORT_SYMBOL_GPL(gpio_direction_output);
1387
1388
1389/* I/O calls are only valid after configuration completed; the relevant
1390 * "is this a valid GPIO" error checks should already have been done.
1391 *
1392 * "Get" operations are often inlinable as reading a pin value register,
1393 * and masking the relevant bit in that register.
1394 *
1395 * When "set" operations are inlinable, they involve writing that mask to
1396 * one register to set a low value, or a different register to set it high.
1397 * Otherwise locking is needed, so there may be little value to inlining.
1398 *
1399 *------------------------------------------------------------------------
1400 *
1401 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1402 * have requested the GPIO. That can include implicit requesting by
1403 * a direction setting call. Marking a gpio as requested locks its chip
1404 * in memory, guaranteeing that these table lookups need no more locking
1405 * and that gpiochip_remove() will fail.
1406 *
1407 * REVISIT when debugging, consider adding some instrumentation to ensure
1408 * that the GPIO was actually requested.
1409 */
1410
1411/**
1412 * __gpio_get_value() - return a gpio's value
1413 * @gpio: gpio whose value will be returned
1414 * Context: any
1415 *
1416 * This is used directly or indirectly to implement gpio_get_value().
1417 * It returns the zero or nonzero value provided by the associated
1418 * gpio_chip.get() method; or zero if no such method is provided.
1419 */
1420int __gpio_get_value(unsigned gpio)
1421{
1422 struct gpio_chip *chip;
1423
1424 chip = gpio_to_chip(gpio);
1425 WARN_ON(extra_checks && chip->can_sleep);
1426 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1427}
1428EXPORT_SYMBOL_GPL(__gpio_get_value);
1429
1430/**
1431 * __gpio_set_value() - assign a gpio's value
1432 * @gpio: gpio whose value will be assigned
1433 * @value: value to assign
1434 * Context: any
1435 *
1436 * This is used directly or indirectly to implement gpio_set_value().
1437 * It invokes the associated gpio_chip.set() method.
1438 */
1439void __gpio_set_value(unsigned gpio, int value)
1440{
1441 struct gpio_chip *chip;
1442
1443 chip = gpio_to_chip(gpio);
1444 WARN_ON(extra_checks && chip->can_sleep);
1445 chip->set(chip, gpio - chip->base, value);
1446}
1447EXPORT_SYMBOL_GPL(__gpio_set_value);
1448
1449/**
1450 * __gpio_cansleep() - report whether gpio value access will sleep
1451 * @gpio: gpio in question
1452 * Context: any
1453 *
1454 * This is used directly or indirectly to implement gpio_cansleep(). It
1455 * returns nonzero if access reading or writing the GPIO value can sleep.
1456 */
1457int __gpio_cansleep(unsigned gpio)
1458{
1459 struct gpio_chip *chip;
1460
1461 /* only call this on GPIOs that are valid! */
1462 chip = gpio_to_chip(gpio);
1463
1464 return chip->can_sleep;
1465}
1466EXPORT_SYMBOL_GPL(__gpio_cansleep);
1467
David Brownell0f6d5042008-10-15 22:03:14 -07001468/**
1469 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1470 * @gpio: gpio whose IRQ will be returned (already requested)
1471 * Context: any
1472 *
1473 * This is used directly or indirectly to implement gpio_to_irq().
1474 * It returns the number of the IRQ signaled by this (input) GPIO,
1475 * or a negative errno.
1476 */
1477int __gpio_to_irq(unsigned gpio)
1478{
1479 struct gpio_chip *chip;
1480
1481 chip = gpio_to_chip(gpio);
1482 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1483}
1484EXPORT_SYMBOL_GPL(__gpio_to_irq);
1485
David Brownelld2876d02008-02-04 22:28:20 -08001486
1487
1488/* There's no value in making it easy to inline GPIO calls that may sleep.
1489 * Common examples include ones connected to I2C or SPI chips.
1490 */
1491
1492int gpio_get_value_cansleep(unsigned gpio)
1493{
1494 struct gpio_chip *chip;
1495
1496 might_sleep_if(extra_checks);
1497 chip = gpio_to_chip(gpio);
David Brownell978ccaa2008-10-18 20:27:49 -07001498 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001499}
1500EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1501
1502void gpio_set_value_cansleep(unsigned gpio, int value)
1503{
1504 struct gpio_chip *chip;
1505
1506 might_sleep_if(extra_checks);
1507 chip = gpio_to_chip(gpio);
1508 chip->set(chip, gpio - chip->base, value);
1509}
1510EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1511
1512
1513#ifdef CONFIG_DEBUG_FS
1514
David Brownelld2876d02008-02-04 22:28:20 -08001515static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1516{
1517 unsigned i;
1518 unsigned gpio = chip->base;
1519 struct gpio_desc *gdesc = &gpio_desc[gpio];
1520 int is_out;
1521
1522 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1523 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1524 continue;
1525
1526 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08001527 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001528 gpio, gdesc->label,
1529 is_out ? "out" : "in ",
1530 chip->get
1531 ? (chip->get(chip, i) ? "hi" : "lo")
1532 : "? ");
1533
1534 if (!is_out) {
1535 int irq = gpio_to_irq(gpio);
Yinghai Lu08678b02008-08-19 20:50:05 -07001536 struct irq_desc *desc = irq_to_desc(irq);
David Brownelld2876d02008-02-04 22:28:20 -08001537
1538 /* This races with request_irq(), set_irq_type(),
1539 * and set_irq_wake() ... but those are "rare".
1540 *
1541 * More significantly, trigger type flags aren't
1542 * currently maintained by genirq.
1543 */
1544 if (irq >= 0 && desc->action) {
1545 char *trigger;
1546
1547 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1548 case IRQ_TYPE_NONE:
1549 trigger = "(default)";
1550 break;
1551 case IRQ_TYPE_EDGE_FALLING:
1552 trigger = "edge-falling";
1553 break;
1554 case IRQ_TYPE_EDGE_RISING:
1555 trigger = "edge-rising";
1556 break;
1557 case IRQ_TYPE_EDGE_BOTH:
1558 trigger = "edge-both";
1559 break;
1560 case IRQ_TYPE_LEVEL_HIGH:
1561 trigger = "level-high";
1562 break;
1563 case IRQ_TYPE_LEVEL_LOW:
1564 trigger = "level-low";
1565 break;
1566 default:
1567 trigger = "?trigger?";
1568 break;
1569 }
1570
1571 seq_printf(s, " irq-%d %s%s",
1572 irq, trigger,
1573 (desc->status & IRQ_WAKEUP)
1574 ? " wakeup" : "");
1575 }
1576 }
1577
1578 seq_printf(s, "\n");
1579 }
1580}
1581
1582static int gpiolib_show(struct seq_file *s, void *unused)
1583{
1584 struct gpio_chip *chip = NULL;
1585 unsigned gpio;
1586 int started = 0;
1587
1588 /* REVISIT this isn't locked against gpio_chip removal ... */
1589
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001590 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
David Brownelld8f388d2008-07-25 01:46:07 -07001591 struct device *dev;
1592
David Brownelld2876d02008-02-04 22:28:20 -08001593 if (chip == gpio_desc[gpio].chip)
1594 continue;
1595 chip = gpio_desc[gpio].chip;
1596 if (!chip)
1597 continue;
1598
David Brownelld8f388d2008-07-25 01:46:07 -07001599 seq_printf(s, "%sGPIOs %d-%d",
David Brownelld2876d02008-02-04 22:28:20 -08001600 started ? "\n" : "",
David Brownelld8f388d2008-07-25 01:46:07 -07001601 chip->base, chip->base + chip->ngpio - 1);
1602 dev = chip->dev;
1603 if (dev)
1604 seq_printf(s, ", %s/%s",
1605 dev->bus ? dev->bus->name : "no-bus",
Kay Sievers14ab3092009-01-06 10:44:42 -08001606 dev_name(dev));
David Brownelld8f388d2008-07-25 01:46:07 -07001607 if (chip->label)
1608 seq_printf(s, ", %s", chip->label);
1609 if (chip->can_sleep)
1610 seq_printf(s, ", can sleep");
1611 seq_printf(s, ":\n");
1612
David Brownelld2876d02008-02-04 22:28:20 -08001613 started = 1;
1614 if (chip->dbg_show)
1615 chip->dbg_show(s, chip);
1616 else
1617 gpiolib_dbg_show(s, chip);
1618 }
1619 return 0;
1620}
1621
1622static int gpiolib_open(struct inode *inode, struct file *file)
1623{
1624 return single_open(file, gpiolib_show, NULL);
1625}
1626
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001627static const struct file_operations gpiolib_operations = {
David Brownelld2876d02008-02-04 22:28:20 -08001628 .open = gpiolib_open,
1629 .read = seq_read,
1630 .llseek = seq_lseek,
1631 .release = single_release,
1632};
1633
1634static int __init gpiolib_debugfs_init(void)
1635{
1636 /* /sys/kernel/debug/gpio */
1637 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1638 NULL, NULL, &gpiolib_operations);
1639 return 0;
1640}
1641subsys_initcall(gpiolib_debugfs_init);
1642
1643#endif /* DEBUG_FS */