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 * Also emit diagnostic messages that may help initial bringup, when
24 * board setup or driver bugs are most common.
26 * Otherwise, minimize overhead in what may be bitbanging codepaths.
29 #define extra_checks 1
31 #define extra_checks 0
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.
38 static DEFINE_SPINLOCK(gpio_lock
);
41 struct gpio_chip
*chip
;
43 /* flag symbols are bit numbers */
44 #define FLAG_REQUESTED 0
47 #ifdef CONFIG_DEBUG_FS
51 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
53 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
55 #ifdef CONFIG_DEBUG_FS
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...
66 static void gpio_ensure_requested(struct gpio_desc
*desc
)
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]");
71 if (!try_module_get(desc
->chip
->owner
))
72 pr_err("GPIO-%d: module can't be gotten \n",
73 (int)(desc
- gpio_desc
));
77 /* caller holds gpio_lock *OR* gpio is marked as requested */
78 static inline struct gpio_chip
*gpio_to_chip(unsigned gpio
)
80 return gpio_desc
[gpio
].chip
;
84 * gpiochip_add() - register a gpio_chip
85 * @chip: the chip to register, with chip->base initialized
86 * Context: potentially before irqs or kmalloc will work
88 * Returns a negative errno if the chip can't be registered, such as
89 * because the chip->base is invalid or already associated with a
90 * different chip. Otherwise it returns zero as a success code.
92 int gpiochip_add(struct gpio_chip
*chip
)
98 /* NOTE chip->base negative is reserved to mean a request for
99 * dynamic allocation. We don't currently support that.
102 if (chip
->base
< 0 || (chip
->base
+ chip
->ngpio
) >= ARCH_NR_GPIOS
) {
107 spin_lock_irqsave(&gpio_lock
, flags
);
109 /* these GPIO numbers must not be managed by another gpio_chip */
110 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
111 if (gpio_desc
[id
].chip
!= NULL
) {
117 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
118 gpio_desc
[id
].chip
= chip
;
119 gpio_desc
[id
].flags
= 0;
123 spin_unlock_irqrestore(&gpio_lock
, flags
);
125 /* failures here can mean systems won't boot... */
127 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
128 chip
->base
, chip
->base
+ chip
->ngpio
,
129 chip
->label
? : "generic");
132 EXPORT_SYMBOL_GPL(gpiochip_add
);
135 * gpiochip_remove() - unregister a gpio_chip
136 * @chip: the chip to unregister
138 * A gpio_chip with any GPIOs still requested may not be removed.
140 int gpiochip_remove(struct gpio_chip
*chip
)
146 spin_lock_irqsave(&gpio_lock
, flags
);
148 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
149 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[id
].flags
)) {
155 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++)
156 gpio_desc
[id
].chip
= NULL
;
159 spin_unlock_irqrestore(&gpio_lock
, flags
);
162 EXPORT_SYMBOL_GPL(gpiochip_remove
);
165 /* These "optional" allocation calls help prevent drivers from stomping
166 * on each other, and help provide better diagnostics in debugfs.
167 * They're called even less than the "set direction" calls.
169 int gpio_request(unsigned gpio
, const char *label
)
171 struct gpio_desc
*desc
;
172 int status
= -EINVAL
;
175 spin_lock_irqsave(&gpio_lock
, flags
);
177 if (gpio
>= ARCH_NR_GPIOS
)
179 desc
= &gpio_desc
[gpio
];
180 if (desc
->chip
== NULL
)
183 if (!try_module_get(desc
->chip
->owner
))
186 /* NOTE: gpio_request() can be called in early boot,
187 * before IRQs are enabled.
190 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
191 desc_set_label(desc
, label
? : "?");
195 module_put(desc
->chip
->owner
);
200 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
201 gpio
, label
? : "?", status
);
202 spin_unlock_irqrestore(&gpio_lock
, flags
);
205 EXPORT_SYMBOL_GPL(gpio_request
);
207 void gpio_free(unsigned gpio
)
210 struct gpio_desc
*desc
;
212 if (gpio
>= ARCH_NR_GPIOS
) {
213 WARN_ON(extra_checks
);
217 spin_lock_irqsave(&gpio_lock
, flags
);
219 desc
= &gpio_desc
[gpio
];
220 if (desc
->chip
&& test_and_clear_bit(FLAG_REQUESTED
, &desc
->flags
)) {
221 desc_set_label(desc
, NULL
);
222 module_put(desc
->chip
->owner
);
224 WARN_ON(extra_checks
);
226 spin_unlock_irqrestore(&gpio_lock
, flags
);
228 EXPORT_SYMBOL_GPL(gpio_free
);
232 * gpiochip_is_requested - return string iff signal was requested
233 * @chip: controller managing the signal
234 * @offset: of signal within controller's 0..(ngpio - 1) range
236 * Returns NULL if the GPIO is not currently requested, else a string.
237 * If debugfs support is enabled, the string returned is the label passed
238 * to gpio_request(); otherwise it is a meaningless constant.
240 * This function is for use by GPIO controller drivers. The label can
241 * help with diagnostics, and knowing that the signal is used as a GPIO
242 * can help avoid accidentally multiplexing it to another controller.
244 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
246 unsigned gpio
= chip
->base
+ offset
;
248 if (gpio
>= ARCH_NR_GPIOS
|| gpio_desc
[gpio
].chip
!= chip
)
250 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[gpio
].flags
) == 0)
252 #ifdef CONFIG_DEBUG_FS
253 return gpio_desc
[gpio
].label
;
258 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
261 /* Drivers MUST set GPIO direction before making get/set calls. In
262 * some cases this is done in early boot, before IRQs are enabled.
264 * As a rule these aren't called more than once (except for drivers
265 * using the open-drain emulation idiom) so these are natural places
266 * to accumulate extra debugging checks. Note that we can't (yet)
267 * rely on gpio_request() having been called beforehand.
270 int gpio_direction_input(unsigned gpio
)
273 struct gpio_chip
*chip
;
274 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
275 int status
= -EINVAL
;
277 spin_lock_irqsave(&gpio_lock
, flags
);
279 if (gpio
>= ARCH_NR_GPIOS
)
282 if (!chip
|| !chip
->get
|| !chip
->direction_input
)
285 if (gpio
>= chip
->ngpio
)
287 gpio_ensure_requested(desc
);
289 /* now we know the gpio is valid and chip won't vanish */
291 spin_unlock_irqrestore(&gpio_lock
, flags
);
293 might_sleep_if(extra_checks
&& chip
->can_sleep
);
295 status
= chip
->direction_input(chip
, gpio
);
297 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
300 spin_unlock_irqrestore(&gpio_lock
, flags
);
302 pr_debug("%s: gpio-%d status %d\n",
303 __FUNCTION__
, gpio
, status
);
306 EXPORT_SYMBOL_GPL(gpio_direction_input
);
308 int gpio_direction_output(unsigned gpio
, int value
)
311 struct gpio_chip
*chip
;
312 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
313 int status
= -EINVAL
;
315 spin_lock_irqsave(&gpio_lock
, flags
);
317 if (gpio
>= ARCH_NR_GPIOS
)
320 if (!chip
|| !chip
->set
|| !chip
->direction_output
)
323 if (gpio
>= chip
->ngpio
)
325 gpio_ensure_requested(desc
);
327 /* now we know the gpio is valid and chip won't vanish */
329 spin_unlock_irqrestore(&gpio_lock
, flags
);
331 might_sleep_if(extra_checks
&& chip
->can_sleep
);
333 status
= chip
->direction_output(chip
, gpio
, value
);
335 set_bit(FLAG_IS_OUT
, &desc
->flags
);
338 spin_unlock_irqrestore(&gpio_lock
, flags
);
340 pr_debug("%s: gpio-%d status %d\n",
341 __FUNCTION__
, gpio
, status
);
344 EXPORT_SYMBOL_GPL(gpio_direction_output
);
347 /* I/O calls are only valid after configuration completed; the relevant
348 * "is this a valid GPIO" error checks should already have been done.
350 * "Get" operations are often inlinable as reading a pin value register,
351 * and masking the relevant bit in that register.
353 * When "set" operations are inlinable, they involve writing that mask to
354 * one register to set a low value, or a different register to set it high.
355 * Otherwise locking is needed, so there may be little value to inlining.
357 *------------------------------------------------------------------------
359 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
360 * have requested the GPIO. That can include implicit requesting by
361 * a direction setting call. Marking a gpio as requested locks its chip
362 * in memory, guaranteeing that these table lookups need no more locking
363 * and that gpiochip_remove() will fail.
365 * REVISIT when debugging, consider adding some instrumentation to ensure
366 * that the GPIO was actually requested.
370 * __gpio_get_value() - return a gpio's value
371 * @gpio: gpio whose value will be returned
374 * This is used directly or indirectly to implement gpio_get_value().
375 * It returns the zero or nonzero value provided by the associated
376 * gpio_chip.get() method; or zero if no such method is provided.
378 int __gpio_get_value(unsigned gpio
)
380 struct gpio_chip
*chip
;
382 chip
= gpio_to_chip(gpio
);
383 WARN_ON(extra_checks
&& chip
->can_sleep
);
384 return chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
386 EXPORT_SYMBOL_GPL(__gpio_get_value
);
389 * __gpio_set_value() - assign a gpio's value
390 * @gpio: gpio whose value will be assigned
391 * @value: value to assign
394 * This is used directly or indirectly to implement gpio_set_value().
395 * It invokes the associated gpio_chip.set() method.
397 void __gpio_set_value(unsigned gpio
, int value
)
399 struct gpio_chip
*chip
;
401 chip
= gpio_to_chip(gpio
);
402 WARN_ON(extra_checks
&& chip
->can_sleep
);
403 chip
->set(chip
, gpio
- chip
->base
, value
);
405 EXPORT_SYMBOL_GPL(__gpio_set_value
);
408 * __gpio_cansleep() - report whether gpio value access will sleep
409 * @gpio: gpio in question
412 * This is used directly or indirectly to implement gpio_cansleep(). It
413 * returns nonzero if access reading or writing the GPIO value can sleep.
415 int __gpio_cansleep(unsigned gpio
)
417 struct gpio_chip
*chip
;
419 /* only call this on GPIOs that are valid! */
420 chip
= gpio_to_chip(gpio
);
422 return chip
->can_sleep
;
424 EXPORT_SYMBOL_GPL(__gpio_cansleep
);
428 /* There's no value in making it easy to inline GPIO calls that may sleep.
429 * Common examples include ones connected to I2C or SPI chips.
432 int gpio_get_value_cansleep(unsigned gpio
)
434 struct gpio_chip
*chip
;
436 might_sleep_if(extra_checks
);
437 chip
= gpio_to_chip(gpio
);
438 return chip
->get(chip
, gpio
- chip
->base
);
440 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep
);
442 void gpio_set_value_cansleep(unsigned gpio
, int value
)
444 struct gpio_chip
*chip
;
446 might_sleep_if(extra_checks
);
447 chip
= gpio_to_chip(gpio
);
448 chip
->set(chip
, gpio
- chip
->base
, value
);
450 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep
);
453 #ifdef CONFIG_DEBUG_FS
455 #include <linux/debugfs.h>
456 #include <linux/seq_file.h>
459 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
462 unsigned gpio
= chip
->base
;
463 struct gpio_desc
*gdesc
= &gpio_desc
[gpio
];
466 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
467 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
470 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
471 seq_printf(s
, " gpio-%-3d (%-12s) %s %s",
473 is_out
? "out" : "in ",
475 ? (chip
->get(chip
, i
) ? "hi" : "lo")
479 int irq
= gpio_to_irq(gpio
);
480 struct irq_desc
*desc
= irq_desc
+ irq
;
482 /* This races with request_irq(), set_irq_type(),
483 * and set_irq_wake() ... but those are "rare".
485 * More significantly, trigger type flags aren't
486 * currently maintained by genirq.
488 if (irq
>= 0 && desc
->action
) {
491 switch (desc
->status
& IRQ_TYPE_SENSE_MASK
) {
493 trigger
= "(default)";
495 case IRQ_TYPE_EDGE_FALLING
:
496 trigger
= "edge-falling";
498 case IRQ_TYPE_EDGE_RISING
:
499 trigger
= "edge-rising";
501 case IRQ_TYPE_EDGE_BOTH
:
502 trigger
= "edge-both";
504 case IRQ_TYPE_LEVEL_HIGH
:
505 trigger
= "level-high";
507 case IRQ_TYPE_LEVEL_LOW
:
508 trigger
= "level-low";
511 trigger
= "?trigger?";
515 seq_printf(s
, " irq-%d %s%s",
517 (desc
->status
& IRQ_WAKEUP
)
526 static int gpiolib_show(struct seq_file
*s
, void *unused
)
528 struct gpio_chip
*chip
= NULL
;
532 /* REVISIT this isn't locked against gpio_chip removal ... */
534 for (gpio
= 0; gpio
< ARCH_NR_GPIOS
; gpio
++) {
535 if (chip
== gpio_desc
[gpio
].chip
)
537 chip
= gpio_desc
[gpio
].chip
;
541 seq_printf(s
, "%sGPIOs %d-%d, %s%s:\n",
543 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
544 chip
->label
? : "generic",
545 chip
->can_sleep
? ", can sleep" : "");
548 chip
->dbg_show(s
, chip
);
550 gpiolib_dbg_show(s
, chip
);
555 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
557 return single_open(file
, gpiolib_show
, NULL
);
560 static struct file_operations gpiolib_operations
= {
561 .open
= gpiolib_open
,
564 .release
= single_release
,
567 static int __init
gpiolib_debugfs_init(void)
569 /* /sys/kernel/debug/gpio */
570 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
571 NULL
, NULL
, &gpiolib_operations
);
574 subsys_initcall(gpiolib_debugfs_init
);
576 #endif /* DEBUG_FS */