blob: 2ba6127c4fae5f2c7dcc8af3b39d021cc7a25c95 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/irq.h>
4#include <linux/spinlock.h>
5
6#include <asm/gpio.h>
7
8
9/* Optional implementation infrastructure for GPIO interfaces.
10 *
11 * Platforms may want to use this if they tend to use very many GPIOs
12 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
13 *
14 * When kernel footprint or instruction count is an issue, simpler
15 * implementations may be preferred. The GPIO programming interface
16 * allows for inlining speed-critical get/set operations for common
17 * cases, so that access to SOC-integrated GPIOs can sometimes cost
18 * only an instruction or two per bit.
19 */
20
21
22/* When debugging, extend minimal trust to callers and platform code.
23 * Also emit diagnostic messages that may help initial bringup, when
24 * board setup or driver bugs are most common.
25 *
26 * Otherwise, minimize overhead in what may be bitbanging codepaths.
27 */
28#ifdef DEBUG
29#define extra_checks 1
30#else
31#define extra_checks 0
32#endif
33
34/* gpio_lock prevents conflicts during gpio_desc[] table updates.
35 * While any GPIO is requested, its gpio_chip is not removable;
36 * each GPIO's "requested" flag serves as a lock and refcount.
37 */
38static DEFINE_SPINLOCK(gpio_lock);
39
40struct gpio_desc {
41 struct gpio_chip *chip;
42 unsigned long flags;
43/* flag symbols are bit numbers */
44#define FLAG_REQUESTED 0
45#define FLAG_IS_OUT 1
46
47#ifdef CONFIG_DEBUG_FS
48 const char *label;
49#endif
50};
51static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
52
53static inline void desc_set_label(struct gpio_desc *d, const char *label)
54{
55#ifdef CONFIG_DEBUG_FS
56 d->label = label;
57#endif
58}
59
60/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
61 * when setting direction, and otherwise illegal. Until board setup code
62 * and drivers use explicit requests everywhere (which won't happen when
63 * those calls have no teeth) we can't avoid autorequesting. This nag
64 * message should motivate switching to explicit requests...
65 */
66static void gpio_ensure_requested(struct gpio_desc *desc)
67{
68 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
69 pr_warning("GPIO-%d autorequested\n", (int)(desc - gpio_desc));
70 desc_set_label(desc, "[auto]");
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -070071 if (!try_module_get(desc->chip->owner))
72 pr_err("GPIO-%d: module can't be gotten \n",
73 (int)(desc - gpio_desc));
David Brownelld2876d02008-02-04 22:28:20 -080074 }
75}
76
77/* caller holds gpio_lock *OR* gpio is marked as requested */
78static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
79{
80 return gpio_desc[gpio].chip;
81}
82
Anton Vorontsov8d0aab22008-04-28 02:14:46 -070083/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
84static int gpiochip_find_base(int ngpio)
85{
86 int i;
87 int spare = 0;
88 int base = -ENOSPC;
89
90 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
91 struct gpio_chip *chip = gpio_desc[i].chip;
92
93 if (!chip) {
94 spare++;
95 if (spare == ngpio) {
96 base = i;
97 break;
98 }
99 } else {
100 spare = 0;
101 i -= chip->ngpio - 1;
102 }
103 }
104
105 if (gpio_is_valid(base))
106 pr_debug("%s: found new base at %d\n", __func__, base);
107 return base;
108}
109
David Brownelld2876d02008-02-04 22:28:20 -0800110/**
111 * gpiochip_add() - register a gpio_chip
112 * @chip: the chip to register, with chip->base initialized
113 * Context: potentially before irqs or kmalloc will work
114 *
115 * Returns a negative errno if the chip can't be registered, such as
116 * because the chip->base is invalid or already associated with a
117 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700118 *
119 * If chip->base is negative, this requests dynamic assignment of
120 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800121 */
122int gpiochip_add(struct gpio_chip *chip)
123{
124 unsigned long flags;
125 int status = 0;
126 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700127 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -0800128
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700129 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio))
130 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -0800131 status = -EINVAL;
132 goto fail;
133 }
134
135 spin_lock_irqsave(&gpio_lock, flags);
136
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700137 if (base < 0) {
138 base = gpiochip_find_base(chip->ngpio);
139 if (base < 0) {
140 status = base;
141 goto fail_unlock;
142 }
143 chip->base = base;
144 }
145
David Brownelld2876d02008-02-04 22:28:20 -0800146 /* these GPIO numbers must not be managed by another gpio_chip */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700147 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -0800148 if (gpio_desc[id].chip != NULL) {
149 status = -EBUSY;
150 break;
151 }
152 }
153 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700154 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -0800155 gpio_desc[id].chip = chip;
156 gpio_desc[id].flags = 0;
157 }
158 }
159
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700160fail_unlock:
David Brownelld2876d02008-02-04 22:28:20 -0800161 spin_unlock_irqrestore(&gpio_lock, flags);
162fail:
163 /* failures here can mean systems won't boot... */
164 if (status)
165 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
166 chip->base, chip->base + chip->ngpio,
167 chip->label ? : "generic");
168 return status;
169}
170EXPORT_SYMBOL_GPL(gpiochip_add);
171
172/**
173 * gpiochip_remove() - unregister a gpio_chip
174 * @chip: the chip to unregister
175 *
176 * A gpio_chip with any GPIOs still requested may not be removed.
177 */
178int gpiochip_remove(struct gpio_chip *chip)
179{
180 unsigned long flags;
181 int status = 0;
182 unsigned id;
183
184 spin_lock_irqsave(&gpio_lock, flags);
185
186 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
187 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
188 status = -EBUSY;
189 break;
190 }
191 }
192 if (status == 0) {
193 for (id = chip->base; id < chip->base + chip->ngpio; id++)
194 gpio_desc[id].chip = NULL;
195 }
196
197 spin_unlock_irqrestore(&gpio_lock, flags);
198 return status;
199}
200EXPORT_SYMBOL_GPL(gpiochip_remove);
201
202
203/* These "optional" allocation calls help prevent drivers from stomping
204 * on each other, and help provide better diagnostics in debugfs.
205 * They're called even less than the "set direction" calls.
206 */
207int gpio_request(unsigned gpio, const char *label)
208{
209 struct gpio_desc *desc;
210 int status = -EINVAL;
211 unsigned long flags;
212
213 spin_lock_irqsave(&gpio_lock, flags);
214
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700215 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -0800216 goto done;
217 desc = &gpio_desc[gpio];
218 if (desc->chip == NULL)
219 goto done;
220
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700221 if (!try_module_get(desc->chip->owner))
222 goto done;
223
David Brownelld2876d02008-02-04 22:28:20 -0800224 /* NOTE: gpio_request() can be called in early boot,
225 * before IRQs are enabled.
226 */
227
228 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
229 desc_set_label(desc, label ? : "?");
230 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700231 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800232 status = -EBUSY;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700233 module_put(desc->chip->owner);
234 }
David Brownelld2876d02008-02-04 22:28:20 -0800235
236done:
237 if (status)
238 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
239 gpio, label ? : "?", status);
240 spin_unlock_irqrestore(&gpio_lock, flags);
241 return status;
242}
243EXPORT_SYMBOL_GPL(gpio_request);
244
245void gpio_free(unsigned gpio)
246{
247 unsigned long flags;
248 struct gpio_desc *desc;
249
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700250 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -0800251 WARN_ON(extra_checks);
252 return;
253 }
254
255 spin_lock_irqsave(&gpio_lock, flags);
256
257 desc = &gpio_desc[gpio];
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700258 if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags)) {
David Brownelld2876d02008-02-04 22:28:20 -0800259 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700260 module_put(desc->chip->owner);
261 } else
David Brownelld2876d02008-02-04 22:28:20 -0800262 WARN_ON(extra_checks);
263
264 spin_unlock_irqrestore(&gpio_lock, flags);
265}
266EXPORT_SYMBOL_GPL(gpio_free);
267
268
269/**
270 * gpiochip_is_requested - return string iff signal was requested
271 * @chip: controller managing the signal
272 * @offset: of signal within controller's 0..(ngpio - 1) range
273 *
274 * Returns NULL if the GPIO is not currently requested, else a string.
275 * If debugfs support is enabled, the string returned is the label passed
276 * to gpio_request(); otherwise it is a meaningless constant.
277 *
278 * This function is for use by GPIO controller drivers. The label can
279 * help with diagnostics, and knowing that the signal is used as a GPIO
280 * can help avoid accidentally multiplexing it to another controller.
281 */
282const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
283{
284 unsigned gpio = chip->base + offset;
285
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700286 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -0800287 return NULL;
288 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
289 return NULL;
290#ifdef CONFIG_DEBUG_FS
291 return gpio_desc[gpio].label;
292#else
293 return "?";
294#endif
295}
296EXPORT_SYMBOL_GPL(gpiochip_is_requested);
297
298
299/* Drivers MUST set GPIO direction before making get/set calls. In
300 * some cases this is done in early boot, before IRQs are enabled.
301 *
302 * As a rule these aren't called more than once (except for drivers
303 * using the open-drain emulation idiom) so these are natural places
304 * to accumulate extra debugging checks. Note that we can't (yet)
305 * rely on gpio_request() having been called beforehand.
306 */
307
308int gpio_direction_input(unsigned gpio)
309{
310 unsigned long flags;
311 struct gpio_chip *chip;
312 struct gpio_desc *desc = &gpio_desc[gpio];
313 int status = -EINVAL;
314
315 spin_lock_irqsave(&gpio_lock, flags);
316
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700317 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -0800318 goto fail;
319 chip = desc->chip;
320 if (!chip || !chip->get || !chip->direction_input)
321 goto fail;
322 gpio -= chip->base;
323 if (gpio >= chip->ngpio)
324 goto fail;
325 gpio_ensure_requested(desc);
326
327 /* now we know the gpio is valid and chip won't vanish */
328
329 spin_unlock_irqrestore(&gpio_lock, flags);
330
331 might_sleep_if(extra_checks && chip->can_sleep);
332
333 status = chip->direction_input(chip, gpio);
334 if (status == 0)
335 clear_bit(FLAG_IS_OUT, &desc->flags);
336 return status;
337fail:
338 spin_unlock_irqrestore(&gpio_lock, flags);
339 if (status)
340 pr_debug("%s: gpio-%d status %d\n",
341 __FUNCTION__, gpio, status);
342 return status;
343}
344EXPORT_SYMBOL_GPL(gpio_direction_input);
345
346int gpio_direction_output(unsigned gpio, int value)
347{
348 unsigned long flags;
349 struct gpio_chip *chip;
350 struct gpio_desc *desc = &gpio_desc[gpio];
351 int status = -EINVAL;
352
353 spin_lock_irqsave(&gpio_lock, flags);
354
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700355 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -0800356 goto fail;
357 chip = desc->chip;
358 if (!chip || !chip->set || !chip->direction_output)
359 goto fail;
360 gpio -= chip->base;
361 if (gpio >= chip->ngpio)
362 goto fail;
363 gpio_ensure_requested(desc);
364
365 /* now we know the gpio is valid and chip won't vanish */
366
367 spin_unlock_irqrestore(&gpio_lock, flags);
368
369 might_sleep_if(extra_checks && chip->can_sleep);
370
371 status = chip->direction_output(chip, gpio, value);
372 if (status == 0)
373 set_bit(FLAG_IS_OUT, &desc->flags);
374 return status;
375fail:
376 spin_unlock_irqrestore(&gpio_lock, flags);
377 if (status)
378 pr_debug("%s: gpio-%d status %d\n",
379 __FUNCTION__, gpio, status);
380 return status;
381}
382EXPORT_SYMBOL_GPL(gpio_direction_output);
383
384
385/* I/O calls are only valid after configuration completed; the relevant
386 * "is this a valid GPIO" error checks should already have been done.
387 *
388 * "Get" operations are often inlinable as reading a pin value register,
389 * and masking the relevant bit in that register.
390 *
391 * When "set" operations are inlinable, they involve writing that mask to
392 * one register to set a low value, or a different register to set it high.
393 * Otherwise locking is needed, so there may be little value to inlining.
394 *
395 *------------------------------------------------------------------------
396 *
397 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
398 * have requested the GPIO. That can include implicit requesting by
399 * a direction setting call. Marking a gpio as requested locks its chip
400 * in memory, guaranteeing that these table lookups need no more locking
401 * and that gpiochip_remove() will fail.
402 *
403 * REVISIT when debugging, consider adding some instrumentation to ensure
404 * that the GPIO was actually requested.
405 */
406
407/**
408 * __gpio_get_value() - return a gpio's value
409 * @gpio: gpio whose value will be returned
410 * Context: any
411 *
412 * This is used directly or indirectly to implement gpio_get_value().
413 * It returns the zero or nonzero value provided by the associated
414 * gpio_chip.get() method; or zero if no such method is provided.
415 */
416int __gpio_get_value(unsigned gpio)
417{
418 struct gpio_chip *chip;
419
420 chip = gpio_to_chip(gpio);
421 WARN_ON(extra_checks && chip->can_sleep);
422 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
423}
424EXPORT_SYMBOL_GPL(__gpio_get_value);
425
426/**
427 * __gpio_set_value() - assign a gpio's value
428 * @gpio: gpio whose value will be assigned
429 * @value: value to assign
430 * Context: any
431 *
432 * This is used directly or indirectly to implement gpio_set_value().
433 * It invokes the associated gpio_chip.set() method.
434 */
435void __gpio_set_value(unsigned gpio, int value)
436{
437 struct gpio_chip *chip;
438
439 chip = gpio_to_chip(gpio);
440 WARN_ON(extra_checks && chip->can_sleep);
441 chip->set(chip, gpio - chip->base, value);
442}
443EXPORT_SYMBOL_GPL(__gpio_set_value);
444
445/**
446 * __gpio_cansleep() - report whether gpio value access will sleep
447 * @gpio: gpio in question
448 * Context: any
449 *
450 * This is used directly or indirectly to implement gpio_cansleep(). It
451 * returns nonzero if access reading or writing the GPIO value can sleep.
452 */
453int __gpio_cansleep(unsigned gpio)
454{
455 struct gpio_chip *chip;
456
457 /* only call this on GPIOs that are valid! */
458 chip = gpio_to_chip(gpio);
459
460 return chip->can_sleep;
461}
462EXPORT_SYMBOL_GPL(__gpio_cansleep);
463
464
465
466/* There's no value in making it easy to inline GPIO calls that may sleep.
467 * Common examples include ones connected to I2C or SPI chips.
468 */
469
470int gpio_get_value_cansleep(unsigned gpio)
471{
472 struct gpio_chip *chip;
473
474 might_sleep_if(extra_checks);
475 chip = gpio_to_chip(gpio);
476 return chip->get(chip, gpio - chip->base);
477}
478EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
479
480void gpio_set_value_cansleep(unsigned gpio, int value)
481{
482 struct gpio_chip *chip;
483
484 might_sleep_if(extra_checks);
485 chip = gpio_to_chip(gpio);
486 chip->set(chip, gpio - chip->base, value);
487}
488EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
489
490
491#ifdef CONFIG_DEBUG_FS
492
493#include <linux/debugfs.h>
494#include <linux/seq_file.h>
495
496
497static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
498{
499 unsigned i;
500 unsigned gpio = chip->base;
501 struct gpio_desc *gdesc = &gpio_desc[gpio];
502 int is_out;
503
504 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
505 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
506 continue;
507
508 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
509 seq_printf(s, " gpio-%-3d (%-12s) %s %s",
510 gpio, gdesc->label,
511 is_out ? "out" : "in ",
512 chip->get
513 ? (chip->get(chip, i) ? "hi" : "lo")
514 : "? ");
515
516 if (!is_out) {
517 int irq = gpio_to_irq(gpio);
518 struct irq_desc *desc = irq_desc + irq;
519
520 /* This races with request_irq(), set_irq_type(),
521 * and set_irq_wake() ... but those are "rare".
522 *
523 * More significantly, trigger type flags aren't
524 * currently maintained by genirq.
525 */
526 if (irq >= 0 && desc->action) {
527 char *trigger;
528
529 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
530 case IRQ_TYPE_NONE:
531 trigger = "(default)";
532 break;
533 case IRQ_TYPE_EDGE_FALLING:
534 trigger = "edge-falling";
535 break;
536 case IRQ_TYPE_EDGE_RISING:
537 trigger = "edge-rising";
538 break;
539 case IRQ_TYPE_EDGE_BOTH:
540 trigger = "edge-both";
541 break;
542 case IRQ_TYPE_LEVEL_HIGH:
543 trigger = "level-high";
544 break;
545 case IRQ_TYPE_LEVEL_LOW:
546 trigger = "level-low";
547 break;
548 default:
549 trigger = "?trigger?";
550 break;
551 }
552
553 seq_printf(s, " irq-%d %s%s",
554 irq, trigger,
555 (desc->status & IRQ_WAKEUP)
556 ? " wakeup" : "");
557 }
558 }
559
560 seq_printf(s, "\n");
561 }
562}
563
564static int gpiolib_show(struct seq_file *s, void *unused)
565{
566 struct gpio_chip *chip = NULL;
567 unsigned gpio;
568 int started = 0;
569
570 /* REVISIT this isn't locked against gpio_chip removal ... */
571
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700572 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
David Brownelld2876d02008-02-04 22:28:20 -0800573 if (chip == gpio_desc[gpio].chip)
574 continue;
575 chip = gpio_desc[gpio].chip;
576 if (!chip)
577 continue;
578
579 seq_printf(s, "%sGPIOs %d-%d, %s%s:\n",
580 started ? "\n" : "",
581 chip->base, chip->base + chip->ngpio - 1,
582 chip->label ? : "generic",
583 chip->can_sleep ? ", can sleep" : "");
584 started = 1;
585 if (chip->dbg_show)
586 chip->dbg_show(s, chip);
587 else
588 gpiolib_dbg_show(s, chip);
589 }
590 return 0;
591}
592
593static int gpiolib_open(struct inode *inode, struct file *file)
594{
595 return single_open(file, gpiolib_show, NULL);
596}
597
598static struct file_operations gpiolib_operations = {
599 .open = gpiolib_open,
600 .read = seq_read,
601 .llseek = seq_lseek,
602 .release = single_release,
603};
604
605static int __init gpiolib_debugfs_init(void)
606{
607 /* /sys/kernel/debug/gpio */
608 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
609 NULL, NULL, &gpiolib_operations);
610 return 0;
611}
612subsys_initcall(gpiolib_debugfs_init);
613
614#endif /* DEBUG_FS */