introduce-path_put-unionfs
[linux-2.6/linux-trees-mm.git] / lib / gpiolib.c
blob32f0f57f20db8489090ffda9d83904532bc07212
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/irq.h>
4 #include <linux/spinlock.h>
6 #include <asm/gpio.h>
9 /* Optional implementation infrastructure for GPIO interfaces.
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.
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.
22 /* When debugging, extend minimal trust to callers and platform code.
23 * Otherwise, minimize overhead in what may be bitbanging codepaths.
25 #ifdef CONFIG_DEBUG_GPIO
26 #define extra_checks 1
27 #else
28 #define extra_checks 0
29 #endif
31 /* gpio_lock protects the table of chips and gpio_chip->requested.
32 * While any gpio is requested, its gpio_chip is not removable. It's
33 * a raw spinlock to ensure safe access from hardirq contexts, and to
34 * shrink bitbang overhead: per-bit preemption would be very wrong.
36 static raw_spinlock_t gpio_lock = __RAW_SPIN_LOCK_UNLOCKED;
37 static struct gpio_chip *chips[DIV_ROUND_UP(ARCH_NR_GPIOS,
38 ARCH_GPIOS_PER_CHIP)];
41 /* Warn when drivers omit gpio_request() calls -- legal but
42 * ill-advised when setting direction, and otherwise illegal.
44 static void gpio_ensure_requested(struct gpio_chip *chip, unsigned offset)
46 int requested;
48 #ifdef CONFIG_DEBUG_FS
49 requested = (int) chip->requested[offset];
50 if (!requested)
51 chip->requested[offset] = "[auto]";
52 #else
53 requested = test_and_set_bit(offset, chip->requested);
54 #endif
56 if (!requested)
57 printk(KERN_DEBUG "GPIO-%d autorequested\n",
58 chip->base + offset);
61 /* caller holds gpio_lock */
62 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
64 return chips[gpio / ARCH_GPIOS_PER_CHIP];
67 /**
68 * gpiochip_add() - register a gpio_chip
69 * @chip: the chip to register, with chip->base initialized
70 * Context: potentially before irqs or kmalloc will work
72 * Returns a negative errno if the chip can't be registered, such as
73 * because the chip->base is invalid or already associated with a
74 * different chip. Otherwise it returns zero as a success code.
76 int gpiochip_add(struct gpio_chip *chip)
78 unsigned long flags;
79 int status = 0;
80 unsigned id;
82 if (chip->base < 0 || (chip->base % ARCH_GPIOS_PER_CHIP) != 0)
83 return -EINVAL;
84 if ((chip->base + chip->ngpio) >= ARCH_NR_GPIOS)
85 return -EINVAL;
86 if (chip->ngpio > ARCH_GPIOS_PER_CHIP)
87 return -EINVAL;
89 local_irq_save(flags);
90 __raw_spin_lock(&gpio_lock);
92 id = chip->base / ARCH_GPIOS_PER_CHIP;
93 if (chips[id] == NULL)
94 chips[id] = chip;
95 else
96 status = -EBUSY;
98 __raw_spin_unlock(&gpio_lock);
99 local_irq_restore(flags);
100 return status;
102 EXPORT_SYMBOL_GPL(gpiochip_add);
105 * gpiochip_remove() - unregister a gpio_chip
106 * @chip: the chip to unregister
108 * A gpio_chip with any GPIOs still requested may not be removed.
110 int gpiochip_remove(struct gpio_chip *chip)
112 unsigned long flags;
113 int status = 0;
114 int offset;
115 unsigned id = chip->base / ARCH_GPIOS_PER_CHIP;
117 local_irq_save(flags);
118 __raw_spin_lock(&gpio_lock);
120 for (offset = 0; offset < chip->ngpio; offset++) {
121 if (gpiochip_is_requested(chip, offset)) {
122 status = -EBUSY;
123 break;
127 if (status == 0) {
128 if (chips[id] == chip)
129 chips[id] = NULL;
130 else
131 status = -EINVAL;
134 __raw_spin_unlock(&gpio_lock);
135 local_irq_restore(flags);
136 return status;
138 EXPORT_SYMBOL_GPL(gpiochip_remove);
141 /* These "optional" allocation calls help prevent drivers from stomping
142 * on each other, and help provide better diagnostics in debugfs.
143 * They're called even less than the "set direction" calls.
145 int gpio_request(unsigned gpio, const char *label)
147 struct gpio_chip *chip;
148 int status = -EINVAL;
149 unsigned long flags;
151 local_irq_save(flags);
152 __raw_spin_lock(&gpio_lock);
154 chip = gpio_to_chip(gpio);
155 if (!chip)
156 goto done;
157 gpio -= chip->base;
158 if (gpio >= chip->ngpio)
159 goto done;
161 /* NOTE: gpio_request() can be called in early boot,
162 * before IRQs are enabled.
165 status = 0;
166 #ifdef CONFIG_DEBUG_FS
167 if (!label)
168 label = "?";
169 if (chip->requested[gpio])
170 status = -EBUSY;
171 else
172 chip->requested[gpio] = label;
173 #else
174 if (test_and_set_bit(gpio, chip->requested))
175 status = -EBUSY;
176 #endif
178 done:
179 __raw_spin_unlock(&gpio_lock);
180 local_irq_restore(flags);
181 return status;
183 EXPORT_SYMBOL_GPL(gpio_request);
185 void gpio_free(unsigned gpio)
187 unsigned long flags;
188 struct gpio_chip *chip;
190 local_irq_save(flags);
191 __raw_spin_lock(&gpio_lock);
193 chip = gpio_to_chip(gpio);
194 if (!chip)
195 goto done;
196 gpio -= chip->base;
197 if (gpio >= chip->ngpio)
198 goto done;
200 #ifdef CONFIG_DEBUG_FS
201 if (chip->requested[gpio])
202 chip->requested[gpio] = NULL;
203 else
204 chip = NULL;
205 #else
206 if (!test_and_clear_bit(gpio, chip->requested))
207 chip = NULL;
208 #endif
209 WARN_ON(extra_checks && chip == NULL);
210 done:
211 __raw_spin_unlock(&gpio_lock);
212 local_irq_restore(flags);
214 EXPORT_SYMBOL_GPL(gpio_free);
218 /* Drivers MUST make configuration calls before get/set calls
220 * As a rule these aren't called more than once (except for drivers
221 * using the open-drain emulation idiom) so these are natural places
222 * to accumulate extra debugging checks. Note that we can't rely on
223 * gpio_request() having been called beforehand.
226 int gpio_direction_input(unsigned gpio)
228 unsigned long flags;
229 struct gpio_chip *chip;
230 int status = -EINVAL;
232 local_irq_save(flags);
233 __raw_spin_lock(&gpio_lock);
235 chip = gpio_to_chip(gpio);
236 if (!chip || !chip->get || !chip->direction_input)
237 goto fail;
238 gpio -= chip->base;
239 if (gpio >= chip->ngpio)
240 goto fail;
241 gpio_ensure_requested(chip, gpio);
243 /* now we know the gpio is valid and chip won't vanish */
245 __raw_spin_unlock(&gpio_lock);
246 local_irq_restore(flags);
248 might_sleep_if(extra_checks && chip->can_sleep);
250 status = chip->direction_input(chip, gpio);
251 if (status == 0)
252 clear_bit(gpio, chip->is_out);
253 return status;
254 fail:
255 __raw_spin_unlock(&gpio_lock);
256 local_irq_restore(flags);
257 return status;
259 EXPORT_SYMBOL_GPL(gpio_direction_input);
261 int gpio_direction_output(unsigned gpio, int value)
263 unsigned long flags;
264 struct gpio_chip *chip;
265 int status = -EINVAL;
267 local_irq_save(flags);
268 __raw_spin_lock(&gpio_lock);
270 chip = gpio_to_chip(gpio);
271 if (!chip || !chip->set || !chip->direction_output)
272 goto fail;
273 gpio -= chip->base;
274 if (gpio >= chip->ngpio)
275 goto fail;
276 gpio_ensure_requested(chip, gpio);
278 /* now we know the gpio is valid and chip won't vanish */
280 __raw_spin_unlock(&gpio_lock);
281 local_irq_restore(flags);
283 might_sleep_if(extra_checks && chip->can_sleep);
285 status = chip->direction_output(chip, gpio, value);
286 if (status == 0)
287 set_bit(gpio, chip->is_out);
288 return status;
289 fail:
290 __raw_spin_unlock(&gpio_lock);
291 local_irq_restore(flags);
292 return status;
294 EXPORT_SYMBOL_GPL(gpio_direction_output);
297 /* I/O calls are only valid after configuration completed; the relevant
298 * "is this a valid GPIO" error checks should already have been done.
300 * "Get" operations are often inlinable as reading a pin value register,
301 * and masking the relevant bit in that register.
303 * When "set" operations are inlinable, they involve writing that mask to
304 * one register to set a low value, or a different register to set it high.
305 * Otherwise locking is needed, so there may be little value to inlining.
307 int __gpio_get_value(unsigned gpio)
309 unsigned long flags;
310 struct gpio_chip *chip;
312 local_irq_save(flags);
313 __raw_spin_lock(&gpio_lock);
315 chip = gpio_to_chip(gpio);
316 if (extra_checks)
317 gpio_ensure_requested(chip, gpio - chip->base);
319 __raw_spin_unlock(&gpio_lock);
320 local_irq_restore(flags);
322 WARN_ON(extra_checks && chip->can_sleep);
323 return chip->get(chip, gpio - chip->base);
325 EXPORT_SYMBOL_GPL(__gpio_get_value);
327 void __gpio_set_value(unsigned gpio, int value)
329 unsigned long flags;
330 struct gpio_chip *chip;
332 local_irq_save(flags);
333 __raw_spin_lock(&gpio_lock);
335 chip = gpio_to_chip(gpio);
336 if (extra_checks)
337 gpio_ensure_requested(chip, gpio - chip->base);
339 __raw_spin_unlock(&gpio_lock);
340 local_irq_restore(flags);
342 WARN_ON(extra_checks && chip->can_sleep);
343 chip->set(chip, gpio - chip->base, value);
345 EXPORT_SYMBOL_GPL(__gpio_set_value);
347 int __gpio_cansleep(unsigned gpio)
349 unsigned long flags;
350 struct gpio_chip *chip;
352 local_irq_save(flags);
353 __raw_spin_lock(&gpio_lock);
355 chip = gpio_to_chip(gpio);
356 if (extra_checks)
357 gpio_ensure_requested(chip, gpio - chip->base);
359 __raw_spin_unlock(&gpio_lock);
360 local_irq_restore(flags);
362 return chip->can_sleep;
364 EXPORT_SYMBOL_GPL(__gpio_cansleep);
368 /* There's no value in inlining GPIO calls that may sleep.
369 * Common examples include ones connected to I2C or SPI chips.
372 int gpio_get_value_cansleep(unsigned gpio)
374 unsigned long flags;
375 struct gpio_chip *chip;
377 might_sleep_if(extra_checks);
379 local_irq_save(flags);
380 __raw_spin_lock(&gpio_lock);
382 chip = gpio_to_chip(gpio);
383 if (extra_checks)
384 gpio_ensure_requested(chip, gpio - chip->base);
386 __raw_spin_unlock(&gpio_lock);
387 local_irq_restore(flags);
389 return chip->get(chip, gpio - chip->base);
391 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
393 void gpio_set_value_cansleep(unsigned gpio, int value)
395 unsigned long flags;
396 struct gpio_chip *chip;
398 might_sleep_if(extra_checks);
400 local_irq_save(flags);
401 __raw_spin_lock(&gpio_lock);
403 chip = gpio_to_chip(gpio);
404 if (extra_checks)
405 gpio_ensure_requested(chip, gpio - chip->base);
407 __raw_spin_unlock(&gpio_lock);
408 local_irq_restore(flags);
410 chip->set(chip, gpio - chip->base, value);
412 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
415 #ifdef CONFIG_DEBUG_FS
417 #include <linux/debugfs.h>
418 #include <linux/seq_file.h>
421 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
423 unsigned i;
425 for (i = 0; i < chip->ngpio; i++) {
426 unsigned gpio;
427 int is_out;
429 if (!chip->requested[i])
430 continue;
432 gpio = chip->base + i;
433 is_out = test_bit(i, chip->is_out);
435 seq_printf(s, " gpio-%-3d (%-12s) %s %s",
436 gpio, chip->requested[i],
437 is_out ? "out" : "in ",
438 chip->get
439 ? (chip->get(chip, i) ? "hi" : "lo")
440 : "? ");
442 if (!is_out) {
443 int irq = gpio_to_irq(gpio);
444 struct irq_desc *desc = irq_desc + irq;
446 /* This races with request_irq(), set_irq_type(),
447 * and set_irq_wake() ... but those are "rare".
449 * More significantly, trigger type flags aren't
450 * currently maintained by genirq.
452 if (irq >= 0 && desc->action) {
453 char *trigger;
455 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
456 case IRQ_TYPE_NONE:
457 trigger = "(default)";
458 break;
459 case IRQ_TYPE_EDGE_FALLING:
460 trigger = "edge-falling";
461 break;
462 case IRQ_TYPE_EDGE_RISING:
463 trigger = "edge-rising";
464 break;
465 case IRQ_TYPE_EDGE_BOTH:
466 trigger = "edge-both";
467 break;
468 case IRQ_TYPE_LEVEL_HIGH:
469 trigger = "level-high";
470 break;
471 case IRQ_TYPE_LEVEL_LOW:
472 trigger = "level-low";
473 break;
474 default:
475 trigger = "?trigger?";
476 break;
479 seq_printf(s, " irq-%d %s%s",
480 irq, trigger,
481 (desc->status & IRQ_WAKEUP)
482 ? " wakeup" : "");
486 seq_printf(s, "\n");
490 static int gpiolib_show(struct seq_file *s, void *unused)
492 struct gpio_chip *chip;
493 unsigned id;
494 int started = 0;
496 /* REVISIT this isn't locked against gpio_chip removal ... */
498 for (id = 0; id < ARRAY_SIZE(chips); id++) {
499 chip = chips[id];
500 if (!chip)
501 continue;
503 seq_printf(s, "%sGPIOs %d-%d, %s%s:\n",
504 started ? "\n" : "",
505 chip->base, chip->base + chip->ngpio - 1,
506 chip->label ? : "generic",
507 chip->can_sleep ? ", can sleep" : "");
508 started = 1;
509 if (chip->dbg_show)
510 chip->dbg_show(s, chip);
511 else
512 gpiolib_dbg_show(s, chip);
514 return 0;
517 static int gpiolib_open(struct inode *inode, struct file *file)
519 return single_open(file, gpiolib_show, NULL);
522 static struct file_operations gpiolib_operations = {
523 .open = gpiolib_open,
524 .read = seq_read,
525 .llseek = seq_lseek,
526 .release = single_release,
529 static int __init gpiolib_debugfs_init(void)
531 /* /sys/kernel/debug/gpio */
532 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
533 NULL, NULL, &gpiolib_operations);
534 return 0;
536 subsys_initcall(gpiolib_debugfs_init);
538 #endif /* DEBUG_FS */