blob: 83cbc34e3a763a79559f1a15b2f8a104243a81fd [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 Brownelld8f388d82008-07-25 01:46:07 -07006#include <linux/device.h>
7#include <linux/err.h>
8#include <linux/debugfs.h>
9#include <linux/seq_file.h>
10#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060011#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070012#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080014
15
16/* Optional implementation infrastructure for GPIO interfaces.
17 *
18 * Platforms may want to use this if they tend to use very many GPIOs
19 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
20 *
21 * When kernel footprint or instruction count is an issue, simpler
22 * implementations may be preferred. The GPIO programming interface
23 * allows for inlining speed-critical get/set operations for common
24 * cases, so that access to SOC-integrated GPIOs can sometimes cost
25 * only an instruction or two per bit.
26 */
27
28
29/* When debugging, extend minimal trust to callers and platform code.
30 * Also emit diagnostic messages that may help initial bringup, when
31 * board setup or driver bugs are most common.
32 *
33 * Otherwise, minimize overhead in what may be bitbanging codepaths.
34 */
35#ifdef DEBUG
36#define extra_checks 1
37#else
38#define extra_checks 0
39#endif
40
41/* gpio_lock prevents conflicts during gpio_desc[] table updates.
42 * While any GPIO is requested, its gpio_chip is not removable;
43 * each GPIO's "requested" flag serves as a lock and refcount.
44 */
45static DEFINE_SPINLOCK(gpio_lock);
46
47struct gpio_desc {
48 struct gpio_chip *chip;
49 unsigned long flags;
50/* flag symbols are bit numbers */
51#define FLAG_REQUESTED 0
52#define FLAG_IS_OUT 1
Anton Vorontsov169b6a72008-04-28 02:14:47 -070053#define FLAG_RESERVED 2
David Brownelld8f388d82008-07-25 01:46:07 -070054#define FLAG_EXPORT 3 /* protected by sysfs_lock */
55#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070056#define FLAG_TRIG_FALL 5 /* trigger on falling edge */
57#define FLAG_TRIG_RISE 6 /* trigger on rising edge */
Jani Nikula07697462009-12-15 16:46:20 -080058#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070059
60#define PDESC_ID_SHIFT 16 /* add new flags before this one */
61
62#define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
63#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080064
65#ifdef CONFIG_DEBUG_FS
66 const char *label;
67#endif
68};
69static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
70
Daniel Glöcknerff77c352009-09-22 16:46:38 -070071#ifdef CONFIG_GPIO_SYSFS
72struct poll_desc {
73 struct work_struct work;
74 struct sysfs_dirent *value_sd;
75};
76
77static struct idr pdesc_idr;
78#endif
79
David Brownelld2876d02008-02-04 22:28:20 -080080static inline void desc_set_label(struct gpio_desc *d, const char *label)
81{
82#ifdef CONFIG_DEBUG_FS
83 d->label = label;
84#endif
85}
86
87/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
88 * when setting direction, and otherwise illegal. Until board setup code
89 * and drivers use explicit requests everywhere (which won't happen when
90 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -070091 * message should motivate switching to explicit requests... so should
92 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -070093 *
94 * NOTE: the autorequest mechanism is going away; at this point it's
95 * only "legal" in the sense that (old) code using it won't break yet,
96 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -080097 */
David Brownell35e8bb52008-10-15 22:03:16 -070098static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
David Brownelld2876d02008-02-04 22:28:20 -080099{
David Brownell8a0cecf2009-04-02 16:57:06 -0700100 const struct gpio_chip *chip = desc->chip;
101 const int gpio = chip->base + offset;
David Brownell35e8bb52008-10-15 22:03:16 -0700102
David Brownell8a0cecf2009-04-02 16:57:06 -0700103 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
104 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700105 if (!try_module_get(chip->owner)) {
106 pr_err("GPIO-%d: module can't be gotten \n", gpio);
107 clear_bit(FLAG_REQUESTED, &desc->flags);
108 /* lose */
109 return -EIO;
110 }
David Brownelld2876d02008-02-04 22:28:20 -0800111 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700112 /* caller must chip->request() w/o spinlock */
113 if (chip->request)
114 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800115 }
David Brownell35e8bb52008-10-15 22:03:16 -0700116 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800117}
118
119/* caller holds gpio_lock *OR* gpio is marked as requested */
120static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
121{
122 return gpio_desc[gpio].chip;
123}
124
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700125/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
126static int gpiochip_find_base(int ngpio)
127{
128 int i;
129 int spare = 0;
130 int base = -ENOSPC;
131
132 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700133 struct gpio_desc *desc = &gpio_desc[i];
134 struct gpio_chip *chip = desc->chip;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700135
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700136 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700137 spare++;
138 if (spare == ngpio) {
139 base = i;
140 break;
141 }
142 } else {
143 spare = 0;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700144 if (chip)
145 i -= chip->ngpio - 1;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700146 }
147 }
148
149 if (gpio_is_valid(base))
150 pr_debug("%s: found new base at %d\n", __func__, base);
151 return base;
152}
153
David Brownelld2876d02008-02-04 22:28:20 -0800154/**
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700155 * gpiochip_reserve() - reserve range of gpios to use with platform code only
156 * @start: starting gpio number
157 * @ngpio: number of gpios to reserve
158 * Context: platform init, potentially before irqs or kmalloc will work
159 *
160 * Returns a negative errno if any gpio within the range is already reserved
161 * or registered, else returns zero as a success code. Use this function
162 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
163 * for example because its driver support is not yet loaded.
164 */
165int __init gpiochip_reserve(int start, int ngpio)
166{
167 int ret = 0;
168 unsigned long flags;
169 int i;
170
Trent Piephobff5fda2008-05-23 13:04:44 -0700171 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700172 return -EINVAL;
173
174 spin_lock_irqsave(&gpio_lock, flags);
175
176 for (i = start; i < start + ngpio; i++) {
177 struct gpio_desc *desc = &gpio_desc[i];
178
179 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
180 ret = -EBUSY;
181 goto err;
182 }
183
184 set_bit(FLAG_RESERVED, &desc->flags);
185 }
186
187 pr_debug("%s: reserved gpios from %d to %d\n",
188 __func__, start, start + ngpio - 1);
189err:
190 spin_unlock_irqrestore(&gpio_lock, flags);
191
192 return ret;
193}
194
David Brownelld8f388d82008-07-25 01:46:07 -0700195#ifdef CONFIG_GPIO_SYSFS
196
197/* lock protects against unexport_gpio() being called while
198 * sysfs files are active.
199 */
200static DEFINE_MUTEX(sysfs_lock);
201
202/*
203 * /sys/class/gpio/gpioN... only for GPIOs that are exported
204 * /direction
205 * * MAY BE OMITTED if kernel won't allow direction changes
206 * * is read/write as "in" or "out"
207 * * may also be written as "high" or "low", initializing
208 * output value as specified ("out" implies "low")
209 * /value
210 * * always readable, subject to hardware behavior
211 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700212 * /edge
213 * * configures behavior of poll(2) on /value
214 * * available only if pin can generate IRQs on input
215 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800216 * /active_low
217 * * configures polarity of /value
218 * * is read/write as zero/nonzero
219 * * also affects existing and subsequent "falling" and "rising"
220 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700221 */
222
223static ssize_t gpio_direction_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 const struct gpio_desc *desc = dev_get_drvdata(dev);
227 ssize_t status;
228
229 mutex_lock(&sysfs_lock);
230
231 if (!test_bit(FLAG_EXPORT, &desc->flags))
232 status = -EIO;
233 else
234 status = sprintf(buf, "%s\n",
235 test_bit(FLAG_IS_OUT, &desc->flags)
236 ? "out" : "in");
237
238 mutex_unlock(&sysfs_lock);
239 return status;
240}
241
242static ssize_t gpio_direction_store(struct device *dev,
243 struct device_attribute *attr, const char *buf, size_t size)
244{
245 const struct gpio_desc *desc = dev_get_drvdata(dev);
246 unsigned gpio = desc - gpio_desc;
247 ssize_t status;
248
249 mutex_lock(&sysfs_lock);
250
251 if (!test_bit(FLAG_EXPORT, &desc->flags))
252 status = -EIO;
253 else if (sysfs_streq(buf, "high"))
254 status = gpio_direction_output(gpio, 1);
255 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
256 status = gpio_direction_output(gpio, 0);
257 else if (sysfs_streq(buf, "in"))
258 status = gpio_direction_input(gpio);
259 else
260 status = -EINVAL;
261
262 mutex_unlock(&sysfs_lock);
263 return status ? : size;
264}
265
Jani Nikula07697462009-12-15 16:46:20 -0800266static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700267 gpio_direction_show, gpio_direction_store);
268
269static ssize_t gpio_value_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
271{
272 const struct gpio_desc *desc = dev_get_drvdata(dev);
273 unsigned gpio = desc - gpio_desc;
274 ssize_t status;
275
276 mutex_lock(&sysfs_lock);
277
Jani Nikula07697462009-12-15 16:46:20 -0800278 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700279 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800280 } else {
281 int value;
282
283 value = !!gpio_get_value_cansleep(gpio);
284 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
285 value = !value;
286
287 status = sprintf(buf, "%d\n", value);
288 }
David Brownelld8f388d82008-07-25 01:46:07 -0700289
290 mutex_unlock(&sysfs_lock);
291 return status;
292}
293
294static ssize_t gpio_value_store(struct device *dev,
295 struct device_attribute *attr, const char *buf, size_t size)
296{
297 const struct gpio_desc *desc = dev_get_drvdata(dev);
298 unsigned gpio = desc - gpio_desc;
299 ssize_t status;
300
301 mutex_lock(&sysfs_lock);
302
303 if (!test_bit(FLAG_EXPORT, &desc->flags))
304 status = -EIO;
305 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
306 status = -EPERM;
307 else {
308 long value;
309
310 status = strict_strtol(buf, 0, &value);
311 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800312 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
313 value = !value;
David Brownelld8f388d82008-07-25 01:46:07 -0700314 gpio_set_value_cansleep(gpio, value != 0);
315 status = size;
316 }
317 }
318
319 mutex_unlock(&sysfs_lock);
320 return status;
321}
322
Jani Nikula07697462009-12-15 16:46:20 -0800323static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700324 gpio_value_show, gpio_value_store);
325
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700326static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
327{
328 struct work_struct *work = priv;
329
330 schedule_work(work);
331 return IRQ_HANDLED;
332}
333
334static void gpio_notify_sysfs(struct work_struct *work)
335{
336 struct poll_desc *pdesc;
337
338 pdesc = container_of(work, struct poll_desc, work);
339 sysfs_notify_dirent(pdesc->value_sd);
340}
341
342static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
343 unsigned long gpio_flags)
344{
345 struct poll_desc *pdesc;
346 unsigned long irq_flags;
347 int ret, irq, id;
348
349 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
350 return 0;
351
352 irq = gpio_to_irq(desc - gpio_desc);
353 if (irq < 0)
354 return -EIO;
355
356 id = desc->flags >> PDESC_ID_SHIFT;
357 pdesc = idr_find(&pdesc_idr, id);
358 if (pdesc) {
359 free_irq(irq, &pdesc->work);
360 cancel_work_sync(&pdesc->work);
361 }
362
363 desc->flags &= ~GPIO_TRIGGER_MASK;
364
365 if (!gpio_flags) {
366 ret = 0;
367 goto free_sd;
368 }
369
370 irq_flags = IRQF_SHARED;
371 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800372 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
373 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700374 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800375 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
376 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700377
378 if (!pdesc) {
379 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
380 if (!pdesc) {
381 ret = -ENOMEM;
382 goto err_out;
383 }
384
385 do {
386 ret = -ENOMEM;
387 if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
388 ret = idr_get_new_above(&pdesc_idr,
389 pdesc, 1, &id);
390 } while (ret == -EAGAIN);
391
392 if (ret)
393 goto free_mem;
394
395 desc->flags &= GPIO_FLAGS_MASK;
396 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
397
398 if (desc->flags >> PDESC_ID_SHIFT != id) {
399 ret = -ERANGE;
400 goto free_id;
401 }
402
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700403 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700404 if (!pdesc->value_sd) {
405 ret = -ENODEV;
406 goto free_id;
407 }
408 INIT_WORK(&pdesc->work, gpio_notify_sysfs);
409 }
410
411 ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
412 "gpiolib", &pdesc->work);
413 if (ret)
414 goto free_sd;
415
416 desc->flags |= gpio_flags;
417 return 0;
418
419free_sd:
Dan Carpenter3913fd52010-04-27 14:12:03 -0700420 if (pdesc)
421 sysfs_put(pdesc->value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700422free_id:
423 idr_remove(&pdesc_idr, id);
424 desc->flags &= GPIO_FLAGS_MASK;
425free_mem:
426 kfree(pdesc);
427err_out:
428 return ret;
429}
430
431static const struct {
432 const char *name;
433 unsigned long flags;
434} trigger_types[] = {
435 { "none", 0 },
436 { "falling", BIT(FLAG_TRIG_FALL) },
437 { "rising", BIT(FLAG_TRIG_RISE) },
438 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
439};
440
441static ssize_t gpio_edge_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
443{
444 const struct gpio_desc *desc = dev_get_drvdata(dev);
445 ssize_t status;
446
447 mutex_lock(&sysfs_lock);
448
449 if (!test_bit(FLAG_EXPORT, &desc->flags))
450 status = -EIO;
451 else {
452 int i;
453
454 status = 0;
455 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
456 if ((desc->flags & GPIO_TRIGGER_MASK)
457 == trigger_types[i].flags) {
458 status = sprintf(buf, "%s\n",
459 trigger_types[i].name);
460 break;
461 }
462 }
463
464 mutex_unlock(&sysfs_lock);
465 return status;
466}
467
468static ssize_t gpio_edge_store(struct device *dev,
469 struct device_attribute *attr, const char *buf, size_t size)
470{
471 struct gpio_desc *desc = dev_get_drvdata(dev);
472 ssize_t status;
473 int i;
474
475 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
476 if (sysfs_streq(trigger_types[i].name, buf))
477 goto found;
478 return -EINVAL;
479
480found:
481 mutex_lock(&sysfs_lock);
482
483 if (!test_bit(FLAG_EXPORT, &desc->flags))
484 status = -EIO;
485 else {
486 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
487 if (!status)
488 status = size;
489 }
490
491 mutex_unlock(&sysfs_lock);
492
493 return status;
494}
495
496static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
497
Jani Nikula07697462009-12-15 16:46:20 -0800498static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
499 int value)
500{
501 int status = 0;
502
503 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
504 return 0;
505
506 if (value)
507 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
508 else
509 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
510
511 /* reconfigure poll(2) support if enabled on one edge only */
512 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
513 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
514 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
515
516 gpio_setup_irq(desc, dev, 0);
517 status = gpio_setup_irq(desc, dev, trigger_flags);
518 }
519
520 return status;
521}
522
523static ssize_t gpio_active_low_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
526 const struct gpio_desc *desc = dev_get_drvdata(dev);
527 ssize_t status;
528
529 mutex_lock(&sysfs_lock);
530
531 if (!test_bit(FLAG_EXPORT, &desc->flags))
532 status = -EIO;
533 else
534 status = sprintf(buf, "%d\n",
535 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
536
537 mutex_unlock(&sysfs_lock);
538
539 return status;
540}
541
542static ssize_t gpio_active_low_store(struct device *dev,
543 struct device_attribute *attr, const char *buf, size_t size)
544{
545 struct gpio_desc *desc = dev_get_drvdata(dev);
546 ssize_t status;
547
548 mutex_lock(&sysfs_lock);
549
550 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
551 status = -EIO;
552 } else {
553 long value;
554
555 status = strict_strtol(buf, 0, &value);
556 if (status == 0)
557 status = sysfs_set_active_low(desc, dev, value != 0);
558 }
559
560 mutex_unlock(&sysfs_lock);
561
562 return status ? : size;
563}
564
565static const DEVICE_ATTR(active_low, 0644,
566 gpio_active_low_show, gpio_active_low_store);
567
David Brownelld8f388d82008-07-25 01:46:07 -0700568static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700569 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800570 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700571 NULL,
572};
573
574static const struct attribute_group gpio_attr_group = {
575 .attrs = (struct attribute **) gpio_attrs,
576};
577
578/*
579 * /sys/class/gpio/gpiochipN/
580 * /base ... matching gpio_chip.base (N)
581 * /label ... matching gpio_chip.label
582 * /ngpio ... matching gpio_chip.ngpio
583 */
584
585static ssize_t chip_base_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
587{
588 const struct gpio_chip *chip = dev_get_drvdata(dev);
589
590 return sprintf(buf, "%d\n", chip->base);
591}
592static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
593
594static ssize_t chip_label_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
597 const struct gpio_chip *chip = dev_get_drvdata(dev);
598
599 return sprintf(buf, "%s\n", chip->label ? : "");
600}
601static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
602
603static ssize_t chip_ngpio_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
605{
606 const struct gpio_chip *chip = dev_get_drvdata(dev);
607
608 return sprintf(buf, "%u\n", chip->ngpio);
609}
610static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
611
612static const struct attribute *gpiochip_attrs[] = {
613 &dev_attr_base.attr,
614 &dev_attr_label.attr,
615 &dev_attr_ngpio.attr,
616 NULL,
617};
618
619static const struct attribute_group gpiochip_attr_group = {
620 .attrs = (struct attribute **) gpiochip_attrs,
621};
622
623/*
624 * /sys/class/gpio/export ... write-only
625 * integer N ... number of GPIO to export (full access)
626 * /sys/class/gpio/unexport ... write-only
627 * integer N ... number of GPIO to unexport
628 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100629static ssize_t export_store(struct class *class,
630 struct class_attribute *attr,
631 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700632{
633 long gpio;
634 int status;
635
636 status = strict_strtol(buf, 0, &gpio);
637 if (status < 0)
638 goto done;
639
640 /* No extra locking here; FLAG_SYSFS just signifies that the
641 * request and export were done by on behalf of userspace, so
642 * they may be undone on its behalf too.
643 */
644
645 status = gpio_request(gpio, "sysfs");
646 if (status < 0)
647 goto done;
648
649 status = gpio_export(gpio, true);
650 if (status < 0)
651 gpio_free(gpio);
652 else
653 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
654
655done:
656 if (status)
657 pr_debug("%s: status %d\n", __func__, status);
658 return status ? : len;
659}
660
Andi Kleen28812fe2010-01-05 12:48:07 +0100661static ssize_t unexport_store(struct class *class,
662 struct class_attribute *attr,
663 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700664{
665 long gpio;
666 int status;
667
668 status = strict_strtol(buf, 0, &gpio);
669 if (status < 0)
670 goto done;
671
672 status = -EINVAL;
673
674 /* reject bogus commands (gpio_unexport ignores them) */
675 if (!gpio_is_valid(gpio))
676 goto done;
677
678 /* No extra locking here; FLAG_SYSFS just signifies that the
679 * request and export were done by on behalf of userspace, so
680 * they may be undone on its behalf too.
681 */
682 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
683 status = 0;
684 gpio_free(gpio);
685 }
686done:
687 if (status)
688 pr_debug("%s: status %d\n", __func__, status);
689 return status ? : len;
690}
691
692static struct class_attribute gpio_class_attrs[] = {
693 __ATTR(export, 0200, NULL, export_store),
694 __ATTR(unexport, 0200, NULL, unexport_store),
695 __ATTR_NULL,
696};
697
698static struct class gpio_class = {
699 .name = "gpio",
700 .owner = THIS_MODULE,
701
702 .class_attrs = gpio_class_attrs,
703};
704
705
706/**
707 * gpio_export - export a GPIO through sysfs
708 * @gpio: gpio to make available, already requested
709 * @direction_may_change: true if userspace may change gpio direction
710 * Context: arch_initcall or later
711 *
712 * When drivers want to make a GPIO accessible to userspace after they
713 * have requested it -- perhaps while debugging, or as part of their
714 * public interface -- they may use this routine. If the GPIO can
715 * change direction (some can't) and the caller allows it, userspace
716 * will see "direction" sysfs attribute which may be used to change
717 * the gpio's direction. A "value" attribute will always be provided.
718 *
719 * Returns zero on success, else an error.
720 */
721int gpio_export(unsigned gpio, bool direction_may_change)
722{
723 unsigned long flags;
724 struct gpio_desc *desc;
725 int status = -EINVAL;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700726 const char *ioname = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700727
728 /* can't export until sysfs is available ... */
729 if (!gpio_class.p) {
730 pr_debug("%s: called too early!\n", __func__);
731 return -ENOENT;
732 }
733
734 if (!gpio_is_valid(gpio))
735 goto done;
736
737 mutex_lock(&sysfs_lock);
738
739 spin_lock_irqsave(&gpio_lock, flags);
740 desc = &gpio_desc[gpio];
741 if (test_bit(FLAG_REQUESTED, &desc->flags)
742 && !test_bit(FLAG_EXPORT, &desc->flags)) {
743 status = 0;
744 if (!desc->chip->direction_input
745 || !desc->chip->direction_output)
746 direction_may_change = false;
747 }
748 spin_unlock_irqrestore(&gpio_lock, flags);
749
Daniel Silverstone926b6632009-04-02 16:57:05 -0700750 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
751 ioname = desc->chip->names[gpio - desc->chip->base];
752
David Brownelld8f388d82008-07-25 01:46:07 -0700753 if (status == 0) {
754 struct device *dev;
755
756 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Uwe Kleine-König7cfe1392010-05-26 14:42:18 -0700757 desc, ioname ? ioname : "gpio%u", gpio);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800758 if (!IS_ERR(dev)) {
Jani Nikula07697462009-12-15 16:46:20 -0800759 status = sysfs_create_group(&dev->kobj,
David Brownelld8f388d82008-07-25 01:46:07 -0700760 &gpio_attr_group);
Jani Nikula07697462009-12-15 16:46:20 -0800761
762 if (!status && direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700763 status = device_create_file(dev,
Jani Nikula07697462009-12-15 16:46:20 -0800764 &dev_attr_direction);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700765
766 if (!status && gpio_to_irq(gpio) >= 0
767 && (direction_may_change
768 || !test_bit(FLAG_IS_OUT,
769 &desc->flags)))
770 status = device_create_file(dev,
771 &dev_attr_edge);
772
David Brownelld8f388d82008-07-25 01:46:07 -0700773 if (status != 0)
774 device_unregister(dev);
775 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800776 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700777 if (status == 0)
778 set_bit(FLAG_EXPORT, &desc->flags);
779 }
780
781 mutex_unlock(&sysfs_lock);
782
783done:
784 if (status)
785 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
786
787 return status;
788}
789EXPORT_SYMBOL_GPL(gpio_export);
790
791static int match_export(struct device *dev, void *data)
792{
793 return dev_get_drvdata(dev) == data;
794}
795
796/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700797 * gpio_export_link - create a sysfs link to an exported GPIO node
798 * @dev: device under which to create symlink
799 * @name: name of the symlink
800 * @gpio: gpio to create symlink to, already exported
801 *
802 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
803 * node. Caller is responsible for unlinking.
804 *
805 * Returns zero on success, else an error.
806 */
807int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
808{
809 struct gpio_desc *desc;
810 int status = -EINVAL;
811
812 if (!gpio_is_valid(gpio))
813 goto done;
814
815 mutex_lock(&sysfs_lock);
816
817 desc = &gpio_desc[gpio];
818
819 if (test_bit(FLAG_EXPORT, &desc->flags)) {
820 struct device *tdev;
821
822 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
823 if (tdev != NULL) {
824 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
825 name);
826 } else {
827 status = -ENODEV;
828 }
829 }
830
831 mutex_unlock(&sysfs_lock);
832
833done:
834 if (status)
835 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
836
837 return status;
838}
839EXPORT_SYMBOL_GPL(gpio_export_link);
840
Jani Nikula07697462009-12-15 16:46:20 -0800841
842/**
843 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
844 * @gpio: gpio to change
845 * @value: non-zero to use active low, i.e. inverted values
846 *
847 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
848 * The GPIO does not have to be exported yet. If poll(2) support has
849 * been enabled for either rising or falling edge, it will be
850 * reconfigured to follow the new polarity.
851 *
852 * Returns zero on success, else an error.
853 */
854int gpio_sysfs_set_active_low(unsigned gpio, int value)
855{
856 struct gpio_desc *desc;
857 struct device *dev = NULL;
858 int status = -EINVAL;
859
860 if (!gpio_is_valid(gpio))
861 goto done;
862
863 mutex_lock(&sysfs_lock);
864
865 desc = &gpio_desc[gpio];
866
867 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800868 dev = class_find_device(&gpio_class, NULL, desc, match_export);
869 if (dev == NULL) {
870 status = -ENODEV;
871 goto unlock;
872 }
873 }
874
875 status = sysfs_set_active_low(desc, dev, value);
876
877unlock:
878 mutex_unlock(&sysfs_lock);
879
880done:
881 if (status)
882 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
883
884 return status;
885}
886EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
887
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700888/**
David Brownelld8f388d82008-07-25 01:46:07 -0700889 * gpio_unexport - reverse effect of gpio_export()
890 * @gpio: gpio to make unavailable
891 *
892 * This is implicit on gpio_free().
893 */
894void gpio_unexport(unsigned gpio)
895{
896 struct gpio_desc *desc;
897 int status = -EINVAL;
898
899 if (!gpio_is_valid(gpio))
900 goto done;
901
902 mutex_lock(&sysfs_lock);
903
904 desc = &gpio_desc[gpio];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700905
David Brownelld8f388d82008-07-25 01:46:07 -0700906 if (test_bit(FLAG_EXPORT, &desc->flags)) {
907 struct device *dev = NULL;
908
909 dev = class_find_device(&gpio_class, NULL, desc, match_export);
910 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700911 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700912 clear_bit(FLAG_EXPORT, &desc->flags);
913 put_device(dev);
914 device_unregister(dev);
915 status = 0;
916 } 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 Brownelld8f388d82008-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 Brownelld8f388d82008-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 Brownelld8f388d82008-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 Brownelld8f388d82008-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 Brownelld8f388d82008-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 Brownelld8f388d82008-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
Anton Vorontsov391c9702010-06-08 07:48:17 -06001103 of_gpiochip_add(chip);
1104
David Brownelld8f388d82008-07-25 01:46:07 -07001105unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001106 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001107
1108 if (status)
1109 goto fail;
1110
1111 status = gpiochip_export(chip);
1112 if (status)
1113 goto fail;
1114
1115 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001116fail:
1117 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001118 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1119 chip->base, chip->base + chip->ngpio - 1,
1120 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001121 return status;
1122}
1123EXPORT_SYMBOL_GPL(gpiochip_add);
1124
1125/**
1126 * gpiochip_remove() - unregister a gpio_chip
1127 * @chip: the chip to unregister
1128 *
1129 * A gpio_chip with any GPIOs still requested may not be removed.
1130 */
1131int gpiochip_remove(struct gpio_chip *chip)
1132{
1133 unsigned long flags;
1134 int status = 0;
1135 unsigned id;
1136
1137 spin_lock_irqsave(&gpio_lock, flags);
1138
Anton Vorontsov391c9702010-06-08 07:48:17 -06001139 of_gpiochip_remove(chip);
1140
David Brownelld2876d02008-02-04 22:28:20 -08001141 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1142 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1143 status = -EBUSY;
1144 break;
1145 }
1146 }
1147 if (status == 0) {
1148 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1149 gpio_desc[id].chip = NULL;
1150 }
1151
1152 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001153
1154 if (status == 0)
1155 gpiochip_unexport(chip);
1156
David Brownelld2876d02008-02-04 22:28:20 -08001157 return status;
1158}
1159EXPORT_SYMBOL_GPL(gpiochip_remove);
1160
Grant Likely594fa262010-06-08 07:48:16 -06001161/**
1162 * gpiochip_find() - iterator for locating a specific gpio_chip
1163 * @data: data to pass to match function
1164 * @callback: Callback function to check gpio_chip
1165 *
1166 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1167 * determined by a user supplied @match callback. The callback should return
1168 * 0 if the device doesn't match and non-zero if it does. If the callback is
1169 * non-zero, this function will return to the caller and not iterate over any
1170 * more gpio_chips.
1171 */
1172struct gpio_chip *gpiochip_find(void *data,
1173 int (*match)(struct gpio_chip *chip, void *data))
1174{
1175 struct gpio_chip *chip = NULL;
1176 unsigned long flags;
1177 int i;
1178
1179 spin_lock_irqsave(&gpio_lock, flags);
1180 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1181 if (!gpio_desc[i].chip)
1182 continue;
1183
1184 if (match(gpio_desc[i].chip, data)) {
1185 chip = gpio_desc[i].chip;
1186 break;
1187 }
1188 }
1189 spin_unlock_irqrestore(&gpio_lock, flags);
1190
1191 return chip;
1192}
David Brownelld2876d02008-02-04 22:28:20 -08001193
1194/* These "optional" allocation calls help prevent drivers from stomping
1195 * on each other, and help provide better diagnostics in debugfs.
1196 * They're called even less than the "set direction" calls.
1197 */
1198int gpio_request(unsigned gpio, const char *label)
1199{
1200 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001201 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001202 int status = -EINVAL;
1203 unsigned long flags;
1204
1205 spin_lock_irqsave(&gpio_lock, flags);
1206
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001207 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001208 goto done;
1209 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001210 chip = desc->chip;
1211 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001212 goto done;
1213
David Brownell35e8bb52008-10-15 22:03:16 -07001214 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001215 goto done;
1216
David Brownelld2876d02008-02-04 22:28:20 -08001217 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001218 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001219 */
1220
1221 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1222 desc_set_label(desc, label ? : "?");
1223 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001224 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001225 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001226 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001227 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001228 }
1229
1230 if (chip->request) {
1231 /* chip->request may sleep */
1232 spin_unlock_irqrestore(&gpio_lock, flags);
1233 status = chip->request(chip, gpio - chip->base);
1234 spin_lock_irqsave(&gpio_lock, flags);
1235
1236 if (status < 0) {
1237 desc_set_label(desc, NULL);
1238 module_put(chip->owner);
1239 clear_bit(FLAG_REQUESTED, &desc->flags);
1240 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001241 }
David Brownelld2876d02008-02-04 22:28:20 -08001242
1243done:
1244 if (status)
1245 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1246 gpio, label ? : "?", status);
1247 spin_unlock_irqrestore(&gpio_lock, flags);
1248 return status;
1249}
1250EXPORT_SYMBOL_GPL(gpio_request);
1251
1252void gpio_free(unsigned gpio)
1253{
1254 unsigned long flags;
1255 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001256 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001257
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001258 might_sleep();
1259
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001260 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -08001261 WARN_ON(extra_checks);
1262 return;
1263 }
1264
David Brownelld8f388d82008-07-25 01:46:07 -07001265 gpio_unexport(gpio);
1266
David Brownelld2876d02008-02-04 22:28:20 -08001267 spin_lock_irqsave(&gpio_lock, flags);
1268
1269 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001270 chip = desc->chip;
1271 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1272 if (chip->free) {
1273 spin_unlock_irqrestore(&gpio_lock, flags);
1274 might_sleep_if(extra_checks && chip->can_sleep);
1275 chip->free(chip, gpio - chip->base);
1276 spin_lock_irqsave(&gpio_lock, flags);
1277 }
David Brownelld2876d02008-02-04 22:28:20 -08001278 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001279 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001280 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001281 clear_bit(FLAG_REQUESTED, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001282 } else
David Brownelld2876d02008-02-04 22:28:20 -08001283 WARN_ON(extra_checks);
1284
1285 spin_unlock_irqrestore(&gpio_lock, flags);
1286}
1287EXPORT_SYMBOL_GPL(gpio_free);
1288
Eric Miao3e45f1d2010-03-05 13:44:35 -08001289/**
1290 * gpio_request_one - request a single GPIO with initial configuration
1291 * @gpio: the GPIO number
1292 * @flags: GPIO configuration as specified by GPIOF_*
1293 * @label: a literal description string of this GPIO
1294 */
1295int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1296{
1297 int err;
1298
1299 err = gpio_request(gpio, label);
1300 if (err)
1301 return err;
1302
1303 if (flags & GPIOF_DIR_IN)
1304 err = gpio_direction_input(gpio);
1305 else
1306 err = gpio_direction_output(gpio,
1307 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1308
1309 return err;
1310}
1311EXPORT_SYMBOL_GPL(gpio_request_one);
1312
1313/**
1314 * gpio_request_array - request multiple GPIOs in a single call
1315 * @array: array of the 'struct gpio'
1316 * @num: how many GPIOs in the array
1317 */
1318int gpio_request_array(struct gpio *array, size_t num)
1319{
1320 int i, err;
1321
1322 for (i = 0; i < num; i++, array++) {
1323 err = gpio_request_one(array->gpio, array->flags, array->label);
1324 if (err)
1325 goto err_free;
1326 }
1327 return 0;
1328
1329err_free:
1330 while (i--)
1331 gpio_free((--array)->gpio);
1332 return err;
1333}
1334EXPORT_SYMBOL_GPL(gpio_request_array);
1335
1336/**
1337 * gpio_free_array - release multiple GPIOs in a single call
1338 * @array: array of the 'struct gpio'
1339 * @num: how many GPIOs in the array
1340 */
1341void gpio_free_array(struct gpio *array, size_t num)
1342{
1343 while (num--)
1344 gpio_free((array++)->gpio);
1345}
1346EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001347
1348/**
1349 * gpiochip_is_requested - return string iff signal was requested
1350 * @chip: controller managing the signal
1351 * @offset: of signal within controller's 0..(ngpio - 1) range
1352 *
1353 * Returns NULL if the GPIO is not currently requested, else a string.
1354 * If debugfs support is enabled, the string returned is the label passed
1355 * to gpio_request(); otherwise it is a meaningless constant.
1356 *
1357 * This function is for use by GPIO controller drivers. The label can
1358 * help with diagnostics, and knowing that the signal is used as a GPIO
1359 * can help avoid accidentally multiplexing it to another controller.
1360 */
1361const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1362{
1363 unsigned gpio = chip->base + offset;
1364
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001365 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001366 return NULL;
1367 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1368 return NULL;
1369#ifdef CONFIG_DEBUG_FS
1370 return gpio_desc[gpio].label;
1371#else
1372 return "?";
1373#endif
1374}
1375EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1376
1377
1378/* Drivers MUST set GPIO direction before making get/set calls. In
1379 * some cases this is done in early boot, before IRQs are enabled.
1380 *
1381 * As a rule these aren't called more than once (except for drivers
1382 * using the open-drain emulation idiom) so these are natural places
1383 * to accumulate extra debugging checks. Note that we can't (yet)
1384 * rely on gpio_request() having been called beforehand.
1385 */
1386
1387int gpio_direction_input(unsigned gpio)
1388{
1389 unsigned long flags;
1390 struct gpio_chip *chip;
1391 struct gpio_desc *desc = &gpio_desc[gpio];
1392 int status = -EINVAL;
1393
1394 spin_lock_irqsave(&gpio_lock, flags);
1395
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001396 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001397 goto fail;
1398 chip = desc->chip;
1399 if (!chip || !chip->get || !chip->direction_input)
1400 goto fail;
1401 gpio -= chip->base;
1402 if (gpio >= chip->ngpio)
1403 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001404 status = gpio_ensure_requested(desc, gpio);
1405 if (status < 0)
1406 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001407
1408 /* now we know the gpio is valid and chip won't vanish */
1409
1410 spin_unlock_irqrestore(&gpio_lock, flags);
1411
1412 might_sleep_if(extra_checks && chip->can_sleep);
1413
David Brownell35e8bb52008-10-15 22:03:16 -07001414 if (status) {
1415 status = chip->request(chip, gpio);
1416 if (status < 0) {
1417 pr_debug("GPIO-%d: chip request fail, %d\n",
1418 chip->base + gpio, status);
1419 /* and it's not available to anyone else ...
1420 * gpio_request() is the fully clean solution.
1421 */
1422 goto lose;
1423 }
1424 }
1425
David Brownelld2876d02008-02-04 22:28:20 -08001426 status = chip->direction_input(chip, gpio);
1427 if (status == 0)
1428 clear_bit(FLAG_IS_OUT, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001429lose:
David Brownelld2876d02008-02-04 22:28:20 -08001430 return status;
1431fail:
1432 spin_unlock_irqrestore(&gpio_lock, flags);
1433 if (status)
1434 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001435 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001436 return status;
1437}
1438EXPORT_SYMBOL_GPL(gpio_direction_input);
1439
1440int gpio_direction_output(unsigned gpio, int value)
1441{
1442 unsigned long flags;
1443 struct gpio_chip *chip;
1444 struct gpio_desc *desc = &gpio_desc[gpio];
1445 int status = -EINVAL;
1446
1447 spin_lock_irqsave(&gpio_lock, flags);
1448
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001449 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001450 goto fail;
1451 chip = desc->chip;
1452 if (!chip || !chip->set || !chip->direction_output)
1453 goto fail;
1454 gpio -= chip->base;
1455 if (gpio >= chip->ngpio)
1456 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001457 status = gpio_ensure_requested(desc, gpio);
1458 if (status < 0)
1459 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001460
1461 /* now we know the gpio is valid and chip won't vanish */
1462
1463 spin_unlock_irqrestore(&gpio_lock, flags);
1464
1465 might_sleep_if(extra_checks && chip->can_sleep);
1466
David Brownell35e8bb52008-10-15 22:03:16 -07001467 if (status) {
1468 status = chip->request(chip, gpio);
1469 if (status < 0) {
1470 pr_debug("GPIO-%d: chip request fail, %d\n",
1471 chip->base + gpio, status);
1472 /* and it's not available to anyone else ...
1473 * gpio_request() is the fully clean solution.
1474 */
1475 goto lose;
1476 }
1477 }
1478
David Brownelld2876d02008-02-04 22:28:20 -08001479 status = chip->direction_output(chip, gpio, value);
1480 if (status == 0)
1481 set_bit(FLAG_IS_OUT, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001482lose:
David Brownelld2876d02008-02-04 22:28:20 -08001483 return status;
1484fail:
1485 spin_unlock_irqrestore(&gpio_lock, flags);
1486 if (status)
1487 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001488 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001489 return status;
1490}
1491EXPORT_SYMBOL_GPL(gpio_direction_output);
1492
Felipe Balbic4b5be92010-05-26 14:42:23 -07001493/**
1494 * gpio_set_debounce - sets @debounce time for a @gpio
1495 * @gpio: the gpio to set debounce time
1496 * @debounce: debounce time is microseconds
1497 */
1498int gpio_set_debounce(unsigned gpio, unsigned debounce)
1499{
1500 unsigned long flags;
1501 struct gpio_chip *chip;
1502 struct gpio_desc *desc = &gpio_desc[gpio];
1503 int status = -EINVAL;
1504
1505 spin_lock_irqsave(&gpio_lock, flags);
1506
1507 if (!gpio_is_valid(gpio))
1508 goto fail;
1509 chip = desc->chip;
1510 if (!chip || !chip->set || !chip->set_debounce)
1511 goto fail;
1512 gpio -= chip->base;
1513 if (gpio >= chip->ngpio)
1514 goto fail;
1515 status = gpio_ensure_requested(desc, gpio);
1516 if (status < 0)
1517 goto fail;
1518
1519 /* now we know the gpio is valid and chip won't vanish */
1520
1521 spin_unlock_irqrestore(&gpio_lock, flags);
1522
1523 might_sleep_if(extra_checks && chip->can_sleep);
1524
1525 return chip->set_debounce(chip, gpio, debounce);
1526
1527fail:
1528 spin_unlock_irqrestore(&gpio_lock, flags);
1529 if (status)
1530 pr_debug("%s: gpio-%d status %d\n",
1531 __func__, gpio, status);
1532
1533 return status;
1534}
1535EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001536
1537/* I/O calls are only valid after configuration completed; the relevant
1538 * "is this a valid GPIO" error checks should already have been done.
1539 *
1540 * "Get" operations are often inlinable as reading a pin value register,
1541 * and masking the relevant bit in that register.
1542 *
1543 * When "set" operations are inlinable, they involve writing that mask to
1544 * one register to set a low value, or a different register to set it high.
1545 * Otherwise locking is needed, so there may be little value to inlining.
1546 *
1547 *------------------------------------------------------------------------
1548 *
1549 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1550 * have requested the GPIO. That can include implicit requesting by
1551 * a direction setting call. Marking a gpio as requested locks its chip
1552 * in memory, guaranteeing that these table lookups need no more locking
1553 * and that gpiochip_remove() will fail.
1554 *
1555 * REVISIT when debugging, consider adding some instrumentation to ensure
1556 * that the GPIO was actually requested.
1557 */
1558
1559/**
1560 * __gpio_get_value() - return a gpio's value
1561 * @gpio: gpio whose value will be returned
1562 * Context: any
1563 *
1564 * This is used directly or indirectly to implement gpio_get_value().
1565 * It returns the zero or nonzero value provided by the associated
1566 * gpio_chip.get() method; or zero if no such method is provided.
1567 */
1568int __gpio_get_value(unsigned gpio)
1569{
1570 struct gpio_chip *chip;
1571
1572 chip = gpio_to_chip(gpio);
1573 WARN_ON(extra_checks && chip->can_sleep);
1574 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1575}
1576EXPORT_SYMBOL_GPL(__gpio_get_value);
1577
1578/**
1579 * __gpio_set_value() - assign a gpio's value
1580 * @gpio: gpio whose value will be assigned
1581 * @value: value to assign
1582 * Context: any
1583 *
1584 * This is used directly or indirectly to implement gpio_set_value().
1585 * It invokes the associated gpio_chip.set() method.
1586 */
1587void __gpio_set_value(unsigned gpio, int value)
1588{
1589 struct gpio_chip *chip;
1590
1591 chip = gpio_to_chip(gpio);
1592 WARN_ON(extra_checks && chip->can_sleep);
1593 chip->set(chip, gpio - chip->base, value);
1594}
1595EXPORT_SYMBOL_GPL(__gpio_set_value);
1596
1597/**
1598 * __gpio_cansleep() - report whether gpio value access will sleep
1599 * @gpio: gpio in question
1600 * Context: any
1601 *
1602 * This is used directly or indirectly to implement gpio_cansleep(). It
1603 * returns nonzero if access reading or writing the GPIO value can sleep.
1604 */
1605int __gpio_cansleep(unsigned gpio)
1606{
1607 struct gpio_chip *chip;
1608
1609 /* only call this on GPIOs that are valid! */
1610 chip = gpio_to_chip(gpio);
1611
1612 return chip->can_sleep;
1613}
1614EXPORT_SYMBOL_GPL(__gpio_cansleep);
1615
David Brownell0f6d5042008-10-15 22:03:14 -07001616/**
1617 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1618 * @gpio: gpio whose IRQ will be returned (already requested)
1619 * Context: any
1620 *
1621 * This is used directly or indirectly to implement gpio_to_irq().
1622 * It returns the number of the IRQ signaled by this (input) GPIO,
1623 * or a negative errno.
1624 */
1625int __gpio_to_irq(unsigned gpio)
1626{
1627 struct gpio_chip *chip;
1628
1629 chip = gpio_to_chip(gpio);
1630 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1631}
1632EXPORT_SYMBOL_GPL(__gpio_to_irq);
1633
David Brownelld2876d02008-02-04 22:28:20 -08001634
1635
1636/* There's no value in making it easy to inline GPIO calls that may sleep.
1637 * Common examples include ones connected to I2C or SPI chips.
1638 */
1639
1640int gpio_get_value_cansleep(unsigned gpio)
1641{
1642 struct gpio_chip *chip;
1643
1644 might_sleep_if(extra_checks);
1645 chip = gpio_to_chip(gpio);
David Brownell978ccaa2008-10-18 20:27:49 -07001646 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001647}
1648EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1649
1650void gpio_set_value_cansleep(unsigned gpio, int value)
1651{
1652 struct gpio_chip *chip;
1653
1654 might_sleep_if(extra_checks);
1655 chip = gpio_to_chip(gpio);
1656 chip->set(chip, gpio - chip->base, value);
1657}
1658EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1659
1660
1661#ifdef CONFIG_DEBUG_FS
1662
David Brownelld2876d02008-02-04 22:28:20 -08001663static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1664{
1665 unsigned i;
1666 unsigned gpio = chip->base;
1667 struct gpio_desc *gdesc = &gpio_desc[gpio];
1668 int is_out;
1669
1670 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1671 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1672 continue;
1673
1674 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08001675 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001676 gpio, gdesc->label,
1677 is_out ? "out" : "in ",
1678 chip->get
1679 ? (chip->get(chip, i) ? "hi" : "lo")
1680 : "? ");
1681
1682 if (!is_out) {
1683 int irq = gpio_to_irq(gpio);
Yinghai Lu08678b02008-08-19 20:50:05 -07001684 struct irq_desc *desc = irq_to_desc(irq);
David Brownelld2876d02008-02-04 22:28:20 -08001685
1686 /* This races with request_irq(), set_irq_type(),
1687 * and set_irq_wake() ... but those are "rare".
1688 *
1689 * More significantly, trigger type flags aren't
1690 * currently maintained by genirq.
1691 */
1692 if (irq >= 0 && desc->action) {
1693 char *trigger;
1694
1695 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1696 case IRQ_TYPE_NONE:
1697 trigger = "(default)";
1698 break;
1699 case IRQ_TYPE_EDGE_FALLING:
1700 trigger = "edge-falling";
1701 break;
1702 case IRQ_TYPE_EDGE_RISING:
1703 trigger = "edge-rising";
1704 break;
1705 case IRQ_TYPE_EDGE_BOTH:
1706 trigger = "edge-both";
1707 break;
1708 case IRQ_TYPE_LEVEL_HIGH:
1709 trigger = "level-high";
1710 break;
1711 case IRQ_TYPE_LEVEL_LOW:
1712 trigger = "level-low";
1713 break;
1714 default:
1715 trigger = "?trigger?";
1716 break;
1717 }
1718
1719 seq_printf(s, " irq-%d %s%s",
1720 irq, trigger,
1721 (desc->status & IRQ_WAKEUP)
1722 ? " wakeup" : "");
1723 }
1724 }
1725
1726 seq_printf(s, "\n");
1727 }
1728}
1729
1730static int gpiolib_show(struct seq_file *s, void *unused)
1731{
1732 struct gpio_chip *chip = NULL;
1733 unsigned gpio;
1734 int started = 0;
1735
1736 /* REVISIT this isn't locked against gpio_chip removal ... */
1737
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001738 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
David Brownelld8f388d82008-07-25 01:46:07 -07001739 struct device *dev;
1740
David Brownelld2876d02008-02-04 22:28:20 -08001741 if (chip == gpio_desc[gpio].chip)
1742 continue;
1743 chip = gpio_desc[gpio].chip;
1744 if (!chip)
1745 continue;
1746
David Brownelld8f388d82008-07-25 01:46:07 -07001747 seq_printf(s, "%sGPIOs %d-%d",
David Brownelld2876d02008-02-04 22:28:20 -08001748 started ? "\n" : "",
David Brownelld8f388d82008-07-25 01:46:07 -07001749 chip->base, chip->base + chip->ngpio - 1);
1750 dev = chip->dev;
1751 if (dev)
1752 seq_printf(s, ", %s/%s",
1753 dev->bus ? dev->bus->name : "no-bus",
Kay Sievers14ab3092009-01-06 10:44:42 -08001754 dev_name(dev));
David Brownelld8f388d82008-07-25 01:46:07 -07001755 if (chip->label)
1756 seq_printf(s, ", %s", chip->label);
1757 if (chip->can_sleep)
1758 seq_printf(s, ", can sleep");
1759 seq_printf(s, ":\n");
1760
David Brownelld2876d02008-02-04 22:28:20 -08001761 started = 1;
1762 if (chip->dbg_show)
1763 chip->dbg_show(s, chip);
1764 else
1765 gpiolib_dbg_show(s, chip);
1766 }
1767 return 0;
1768}
1769
1770static int gpiolib_open(struct inode *inode, struct file *file)
1771{
1772 return single_open(file, gpiolib_show, NULL);
1773}
1774
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001775static const struct file_operations gpiolib_operations = {
David Brownelld2876d02008-02-04 22:28:20 -08001776 .open = gpiolib_open,
1777 .read = seq_read,
1778 .llseek = seq_lseek,
1779 .release = single_release,
1780};
1781
1782static int __init gpiolib_debugfs_init(void)
1783{
1784 /* /sys/kernel/debug/gpio */
1785 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1786 NULL, NULL, &gpiolib_operations);
1787 return 0;
1788}
1789subsys_initcall(gpiolib_debugfs_init);
1790
1791#endif /* DEBUG_FS */