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]");
74 /* caller holds gpio_lock *OR* gpio is marked as requested */
75 static inline struct gpio_chip
*gpio_to_chip(unsigned gpio
)
77 return gpio_desc
[gpio
].chip
;
81 * gpiochip_add() - register a gpio_chip
82 * @chip: the chip to register, with chip->base initialized
83 * Context: potentially before irqs or kmalloc will work
85 * Returns a negative errno if the chip can't be registered, such as
86 * because the chip->base is invalid or already associated with a
87 * different chip. Otherwise it returns zero as a success code.
89 int gpiochip_add(struct gpio_chip
*chip
)
95 /* NOTE chip->base negative is reserved to mean a request for
96 * dynamic allocation. We don't currently support that.
99 if (chip
->base
< 0 || (chip
->base
+ chip
->ngpio
) >= ARCH_NR_GPIOS
) {
104 spin_lock_irqsave(&gpio_lock
, flags
);
106 /* these GPIO numbers must not be managed by another gpio_chip */
107 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
108 if (gpio_desc
[id
].chip
!= NULL
) {
114 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
115 gpio_desc
[id
].chip
= chip
;
116 gpio_desc
[id
].flags
= 0;
120 spin_unlock_irqrestore(&gpio_lock
, flags
);
122 /* failures here can mean systems won't boot... */
124 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
125 chip
->base
, chip
->base
+ chip
->ngpio
,
126 chip
->label
? : "generic");
129 EXPORT_SYMBOL_GPL(gpiochip_add
);
132 * gpiochip_remove() - unregister a gpio_chip
133 * @chip: the chip to unregister
135 * A gpio_chip with any GPIOs still requested may not be removed.
137 int gpiochip_remove(struct gpio_chip
*chip
)
143 spin_lock_irqsave(&gpio_lock
, flags
);
145 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
146 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[id
].flags
)) {
152 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++)
153 gpio_desc
[id
].chip
= NULL
;
156 spin_unlock_irqrestore(&gpio_lock
, flags
);
159 EXPORT_SYMBOL_GPL(gpiochip_remove
);
162 /* These "optional" allocation calls help prevent drivers from stomping
163 * on each other, and help provide better diagnostics in debugfs.
164 * They're called even less than the "set direction" calls.
166 int gpio_request(unsigned gpio
, const char *label
)
168 struct gpio_desc
*desc
;
169 int status
= -EINVAL
;
172 spin_lock_irqsave(&gpio_lock
, flags
);
174 if (gpio
>= ARCH_NR_GPIOS
)
176 desc
= &gpio_desc
[gpio
];
177 if (desc
->chip
== NULL
)
180 /* NOTE: gpio_request() can be called in early boot,
181 * before IRQs are enabled.
184 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
185 desc_set_label(desc
, label
? : "?");
192 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
193 gpio
, label
? : "?", status
);
194 spin_unlock_irqrestore(&gpio_lock
, flags
);
197 EXPORT_SYMBOL_GPL(gpio_request
);
199 void gpio_free(unsigned gpio
)
202 struct gpio_desc
*desc
;
204 if (gpio
>= ARCH_NR_GPIOS
) {
205 WARN_ON(extra_checks
);
209 spin_lock_irqsave(&gpio_lock
, flags
);
211 desc
= &gpio_desc
[gpio
];
212 if (desc
->chip
&& test_and_clear_bit(FLAG_REQUESTED
, &desc
->flags
))
213 desc_set_label(desc
, NULL
);
215 WARN_ON(extra_checks
);
217 spin_unlock_irqrestore(&gpio_lock
, flags
);
219 EXPORT_SYMBOL_GPL(gpio_free
);
223 * gpiochip_is_requested - return string iff signal was requested
224 * @chip: controller managing the signal
225 * @offset: of signal within controller's 0..(ngpio - 1) range
227 * Returns NULL if the GPIO is not currently requested, else a string.
228 * If debugfs support is enabled, the string returned is the label passed
229 * to gpio_request(); otherwise it is a meaningless constant.
231 * This function is for use by GPIO controller drivers. The label can
232 * help with diagnostics, and knowing that the signal is used as a GPIO
233 * can help avoid accidentally multiplexing it to another controller.
235 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
237 unsigned gpio
= chip
->base
+ offset
;
239 if (gpio
>= ARCH_NR_GPIOS
|| gpio_desc
[gpio
].chip
!= chip
)
241 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[gpio
].flags
) == 0)
243 #ifdef CONFIG_DEBUG_FS
244 return gpio_desc
[gpio
].label
;
249 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
252 /* Drivers MUST set GPIO direction before making get/set calls. In
253 * some cases this is done in early boot, before IRQs are enabled.
255 * As a rule these aren't called more than once (except for drivers
256 * using the open-drain emulation idiom) so these are natural places
257 * to accumulate extra debugging checks. Note that we can't (yet)
258 * rely on gpio_request() having been called beforehand.
261 int gpio_direction_input(unsigned gpio
)
264 struct gpio_chip
*chip
;
265 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
266 int status
= -EINVAL
;
268 spin_lock_irqsave(&gpio_lock
, flags
);
270 if (gpio
>= ARCH_NR_GPIOS
)
273 if (!chip
|| !chip
->get
|| !chip
->direction_input
)
276 if (gpio
>= chip
->ngpio
)
278 gpio_ensure_requested(desc
);
280 /* now we know the gpio is valid and chip won't vanish */
282 spin_unlock_irqrestore(&gpio_lock
, flags
);
284 might_sleep_if(extra_checks
&& chip
->can_sleep
);
286 status
= chip
->direction_input(chip
, gpio
);
288 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
291 spin_unlock_irqrestore(&gpio_lock
, flags
);
293 pr_debug("%s: gpio-%d status %d\n",
294 __FUNCTION__
, gpio
, status
);
297 EXPORT_SYMBOL_GPL(gpio_direction_input
);
299 int gpio_direction_output(unsigned gpio
, int value
)
302 struct gpio_chip
*chip
;
303 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
304 int status
= -EINVAL
;
306 spin_lock_irqsave(&gpio_lock
, flags
);
308 if (gpio
>= ARCH_NR_GPIOS
)
311 if (!chip
|| !chip
->set
|| !chip
->direction_output
)
314 if (gpio
>= chip
->ngpio
)
316 gpio_ensure_requested(desc
);
318 /* now we know the gpio is valid and chip won't vanish */
320 spin_unlock_irqrestore(&gpio_lock
, flags
);
322 might_sleep_if(extra_checks
&& chip
->can_sleep
);
324 status
= chip
->direction_output(chip
, gpio
, value
);
326 set_bit(FLAG_IS_OUT
, &desc
->flags
);
329 spin_unlock_irqrestore(&gpio_lock
, flags
);
331 pr_debug("%s: gpio-%d status %d\n",
332 __FUNCTION__
, gpio
, status
);
335 EXPORT_SYMBOL_GPL(gpio_direction_output
);
338 /* I/O calls are only valid after configuration completed; the relevant
339 * "is this a valid GPIO" error checks should already have been done.
341 * "Get" operations are often inlinable as reading a pin value register,
342 * and masking the relevant bit in that register.
344 * When "set" operations are inlinable, they involve writing that mask to
345 * one register to set a low value, or a different register to set it high.
346 * Otherwise locking is needed, so there may be little value to inlining.
348 *------------------------------------------------------------------------
350 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
351 * have requested the GPIO. That can include implicit requesting by
352 * a direction setting call. Marking a gpio as requested locks its chip
353 * in memory, guaranteeing that these table lookups need no more locking
354 * and that gpiochip_remove() will fail.
356 * REVISIT when debugging, consider adding some instrumentation to ensure
357 * that the GPIO was actually requested.
361 * __gpio_get_value() - return a gpio's value
362 * @gpio: gpio whose value will be returned
365 * This is used directly or indirectly to implement gpio_get_value().
366 * It returns the zero or nonzero value provided by the associated
367 * gpio_chip.get() method; or zero if no such method is provided.
369 int __gpio_get_value(unsigned gpio
)
371 struct gpio_chip
*chip
;
373 chip
= gpio_to_chip(gpio
);
374 WARN_ON(extra_checks
&& chip
->can_sleep
);
375 return chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
377 EXPORT_SYMBOL_GPL(__gpio_get_value
);
380 * __gpio_set_value() - assign a gpio's value
381 * @gpio: gpio whose value will be assigned
382 * @value: value to assign
385 * This is used directly or indirectly to implement gpio_set_value().
386 * It invokes the associated gpio_chip.set() method.
388 void __gpio_set_value(unsigned gpio
, int value
)
390 struct gpio_chip
*chip
;
392 chip
= gpio_to_chip(gpio
);
393 WARN_ON(extra_checks
&& chip
->can_sleep
);
394 chip
->set(chip
, gpio
- chip
->base
, value
);
396 EXPORT_SYMBOL_GPL(__gpio_set_value
);
399 * __gpio_cansleep() - report whether gpio value access will sleep
400 * @gpio: gpio in question
403 * This is used directly or indirectly to implement gpio_cansleep(). It
404 * returns nonzero if access reading or writing the GPIO value can sleep.
406 int __gpio_cansleep(unsigned gpio
)
408 struct gpio_chip
*chip
;
410 /* only call this on GPIOs that are valid! */
411 chip
= gpio_to_chip(gpio
);
413 return chip
->can_sleep
;
415 EXPORT_SYMBOL_GPL(__gpio_cansleep
);
419 /* There's no value in making it easy to inline GPIO calls that may sleep.
420 * Common examples include ones connected to I2C or SPI chips.
423 int gpio_get_value_cansleep(unsigned gpio
)
425 struct gpio_chip
*chip
;
427 might_sleep_if(extra_checks
);
428 chip
= gpio_to_chip(gpio
);
429 return chip
->get(chip
, gpio
- chip
->base
);
431 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep
);
433 void gpio_set_value_cansleep(unsigned gpio
, int value
)
435 struct gpio_chip
*chip
;
437 might_sleep_if(extra_checks
);
438 chip
= gpio_to_chip(gpio
);
439 chip
->set(chip
, gpio
- chip
->base
, value
);
441 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep
);
444 #ifdef CONFIG_DEBUG_FS
446 #include <linux/debugfs.h>
447 #include <linux/seq_file.h>
450 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
453 unsigned gpio
= chip
->base
;
454 struct gpio_desc
*gdesc
= &gpio_desc
[gpio
];
457 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
458 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
461 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
462 seq_printf(s
, " gpio-%-3d (%-12s) %s %s",
464 is_out
? "out" : "in ",
466 ? (chip
->get(chip
, i
) ? "hi" : "lo")
470 int irq
= gpio_to_irq(gpio
);
471 struct irq_desc
*desc
= irq_desc
+ irq
;
473 /* This races with request_irq(), set_irq_type(),
474 * and set_irq_wake() ... but those are "rare".
476 * More significantly, trigger type flags aren't
477 * currently maintained by genirq.
479 if (irq
>= 0 && desc
->action
) {
482 switch (desc
->status
& IRQ_TYPE_SENSE_MASK
) {
484 trigger
= "(default)";
486 case IRQ_TYPE_EDGE_FALLING
:
487 trigger
= "edge-falling";
489 case IRQ_TYPE_EDGE_RISING
:
490 trigger
= "edge-rising";
492 case IRQ_TYPE_EDGE_BOTH
:
493 trigger
= "edge-both";
495 case IRQ_TYPE_LEVEL_HIGH
:
496 trigger
= "level-high";
498 case IRQ_TYPE_LEVEL_LOW
:
499 trigger
= "level-low";
502 trigger
= "?trigger?";
506 seq_printf(s
, " irq-%d %s%s",
508 (desc
->status
& IRQ_WAKEUP
)
517 static int gpiolib_show(struct seq_file
*s
, void *unused
)
519 struct gpio_chip
*chip
= NULL
;
523 /* REVISIT this isn't locked against gpio_chip removal ... */
525 for (gpio
= 0; gpio
< ARCH_NR_GPIOS
; gpio
++) {
526 if (chip
== gpio_desc
[gpio
].chip
)
528 chip
= gpio_desc
[gpio
].chip
;
532 seq_printf(s
, "%sGPIOs %d-%d, %s%s:\n",
534 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
535 chip
->label
? : "generic",
536 chip
->can_sleep
? ", can sleep" : "");
539 chip
->dbg_show(s
, chip
);
541 gpiolib_dbg_show(s
, chip
);
546 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
548 return single_open(file
, gpiolib_show
, NULL
);
551 static struct file_operations gpiolib_operations
= {
552 .open
= gpiolib_open
,
555 .release
= single_release
,
558 static int __init
gpiolib_debugfs_init(void)
560 /* /sys/kernel/debug/gpio */
561 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
562 NULL
, NULL
, &gpiolib_operations
);
565 subsys_initcall(gpiolib_debugfs_init
);
567 #endif /* DEBUG_FS */