1 #include <linux/kernel.h>
2 #include <linux/module.h>
4 #include <linux/spinlock.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
28 #define extra_checks 0
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
)
48 #ifdef CONFIG_DEBUG_FS
49 requested
= (int) chip
->requested
[offset
];
51 chip
->requested
[offset
] = "[auto]";
53 requested
= test_and_set_bit(offset
, chip
->requested
);
57 printk(KERN_DEBUG
"GPIO-%d autorequested\n",
61 /* caller holds gpio_lock */
62 static inline struct gpio_chip
*gpio_to_chip(unsigned gpio
)
64 return chips
[gpio
/ ARCH_GPIOS_PER_CHIP
];
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
)
82 if (chip
->base
< 0 || (chip
->base
% ARCH_GPIOS_PER_CHIP
) != 0)
84 if ((chip
->base
+ chip
->ngpio
) >= ARCH_NR_GPIOS
)
86 if (chip
->ngpio
> ARCH_GPIOS_PER_CHIP
)
89 local_irq_save(flags
);
90 __raw_spin_lock(&gpio_lock
);
92 id
= chip
->base
/ ARCH_GPIOS_PER_CHIP
;
93 if (chips
[id
] == NULL
)
98 __raw_spin_unlock(&gpio_lock
);
99 local_irq_restore(flags
);
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
)
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
)) {
128 if (chips
[id
] == chip
)
134 __raw_spin_unlock(&gpio_lock
);
135 local_irq_restore(flags
);
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
;
151 local_irq_save(flags
);
152 __raw_spin_lock(&gpio_lock
);
154 chip
= gpio_to_chip(gpio
);
158 if (gpio
>= chip
->ngpio
)
161 /* NOTE: gpio_request() can be called in early boot,
162 * before IRQs are enabled.
166 #ifdef CONFIG_DEBUG_FS
169 if (chip
->requested
[gpio
])
172 chip
->requested
[gpio
] = label
;
174 if (test_and_set_bit(gpio
, chip
->requested
))
179 __raw_spin_unlock(&gpio_lock
);
180 local_irq_restore(flags
);
183 EXPORT_SYMBOL_GPL(gpio_request
);
185 void gpio_free(unsigned gpio
)
188 struct gpio_chip
*chip
;
190 local_irq_save(flags
);
191 __raw_spin_lock(&gpio_lock
);
193 chip
= gpio_to_chip(gpio
);
197 if (gpio
>= chip
->ngpio
)
200 #ifdef CONFIG_DEBUG_FS
201 if (chip
->requested
[gpio
])
202 chip
->requested
[gpio
] = NULL
;
206 if (!test_and_clear_bit(gpio
, chip
->requested
))
209 WARN_ON(extra_checks
&& chip
== NULL
);
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
)
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
)
239 if (gpio
>= chip
->ngpio
)
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
);
252 clear_bit(gpio
, chip
->is_out
);
255 __raw_spin_unlock(&gpio_lock
);
256 local_irq_restore(flags
);
259 EXPORT_SYMBOL_GPL(gpio_direction_input
);
261 int gpio_direction_output(unsigned gpio
, int value
)
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
)
274 if (gpio
>= chip
->ngpio
)
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
);
287 set_bit(gpio
, chip
->is_out
);
290 __raw_spin_unlock(&gpio_lock
);
291 local_irq_restore(flags
);
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
)
310 struct gpio_chip
*chip
;
312 local_irq_save(flags
);
313 __raw_spin_lock(&gpio_lock
);
315 chip
= gpio_to_chip(gpio
);
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
)
330 struct gpio_chip
*chip
;
332 local_irq_save(flags
);
333 __raw_spin_lock(&gpio_lock
);
335 chip
= gpio_to_chip(gpio
);
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
)
350 struct gpio_chip
*chip
;
352 local_irq_save(flags
);
353 __raw_spin_lock(&gpio_lock
);
355 chip
= gpio_to_chip(gpio
);
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
)
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
);
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
)
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
);
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
)
425 for (i
= 0; i
< chip
->ngpio
; i
++) {
429 if (!chip
->requested
[i
])
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 ",
439 ? (chip
->get(chip
, i
) ? "hi" : "lo")
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
) {
455 switch (desc
->status
& IRQ_TYPE_SENSE_MASK
) {
457 trigger
= "(default)";
459 case IRQ_TYPE_EDGE_FALLING
:
460 trigger
= "edge-falling";
462 case IRQ_TYPE_EDGE_RISING
:
463 trigger
= "edge-rising";
465 case IRQ_TYPE_EDGE_BOTH
:
466 trigger
= "edge-both";
468 case IRQ_TYPE_LEVEL_HIGH
:
469 trigger
= "level-high";
471 case IRQ_TYPE_LEVEL_LOW
:
472 trigger
= "level-low";
475 trigger
= "?trigger?";
479 seq_printf(s
, " irq-%d %s%s",
481 (desc
->status
& IRQ_WAKEUP
)
490 static int gpiolib_show(struct seq_file
*s
, void *unused
)
492 struct gpio_chip
*chip
;
496 /* REVISIT this isn't locked against gpio_chip removal ... */
498 for (id
= 0; id
< ARRAY_SIZE(chips
); id
++) {
503 seq_printf(s
, "%sGPIOs %d-%d, %s%s:\n",
505 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
506 chip
->label
? : "generic",
507 chip
->can_sleep
? ", can sleep" : "");
510 chip
->dbg_show(s
, chip
);
512 gpiolib_dbg_show(s
, chip
);
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
,
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
);
536 subsys_initcall(gpiolib_debugfs_init
);
538 #endif /* DEBUG_FS */