gpio: remove gpio_ensure_requested()
[linux-2.6-block.git] / drivers / gpio / gpiolib.c
CommitLineData
d2876d08
DB
1#include <linux/kernel.h>
2#include <linux/module.h>
ff77c352 3#include <linux/interrupt.h>
d2876d08
DB
4#include <linux/irq.h>
5#include <linux/spinlock.h>
1a989d0f 6#include <linux/list.h>
d8f388d8
DB
7#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
391c970c 12#include <linux/of_gpio.h>
ff77c352 13#include <linux/idr.h>
5a0e3ad6 14#include <linux/slab.h>
7b199811 15#include <linux/acpi.h>
53e7cac3 16#include <linux/gpio/driver.h>
d2876d08 17
664e3e5a
MW
18#include "gpiolib.h"
19
3f397c21
UKK
20#define CREATE_TRACE_POINTS
21#include <trace/events/gpio.h>
d2876d08 22
79a9becd 23/* Implementation infrastructure for GPIO interfaces.
d2876d08 24 *
79a9becd
AC
25 * The GPIO programming interface allows for inlining speed-critical
26 * get/set operations for common cases, so that access to SOC-integrated
27 * GPIOs can sometimes cost only an instruction or two per bit.
d2876d08
DB
28 */
29
30
31/* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
34 *
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
36 */
37#ifdef DEBUG
38#define extra_checks 1
39#else
40#define extra_checks 0
41#endif
42
43/* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
46 */
0eb4c6c2 47DEFINE_SPINLOCK(gpio_lock);
d2876d08 48
d2876d08
DB
49static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
50
6c0b4e6c
AC
51#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
52
bae48da2
AC
53static DEFINE_MUTEX(gpio_lookup_lock);
54static LIST_HEAD(gpio_lookup_list);
0eb4c6c2 55LIST_HEAD(gpio_chips);
1a2a99c6 56
d2876d08
DB
57static inline void desc_set_label(struct gpio_desc *d, const char *label)
58{
d2876d08 59 d->label = label;
d2876d08
DB
60}
61
372e722e
AC
62/**
63 * Convert a GPIO number to its descriptor
64 */
79a9becd 65struct gpio_desc *gpio_to_desc(unsigned gpio)
372e722e
AC
66{
67 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
68 return NULL;
69 else
70 return &gpio_desc[gpio];
71}
79a9becd 72EXPORT_SYMBOL_GPL(gpio_to_desc);
372e722e 73
d468bf9e 74/**
bb1e88cc 75 * Get the GPIO descriptor corresponding to the given hw number for this chip.
d468bf9e 76 */
bb1e88cc
AC
77struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
78 u16 hwnum)
d468bf9e 79{
bb1e88cc 80 if (hwnum >= chip->ngpio)
b7d0a28a 81 return ERR_PTR(-EINVAL);
d468bf9e 82
bb1e88cc 83 return &chip->desc[hwnum];
d468bf9e 84}
372e722e
AC
85
86/**
87 * Convert a GPIO descriptor to the integer namespace.
88 * This should disappear in the future but is needed since we still
89 * use GPIO numbers for error messages and sysfs nodes
90 */
79a9becd 91int desc_to_gpio(const struct gpio_desc *desc)
372e722e 92{
8c0fca81 93 return desc - &gpio_desc[0];
372e722e 94}
79a9becd 95EXPORT_SYMBOL_GPL(desc_to_gpio);
372e722e
AC
96
97
79a9becd
AC
98/**
99 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
100 * @desc: descriptor to return the chip of
101 */
102struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
372e722e 103{
bcabdef1 104 return desc ? desc->chip : NULL;
372e722e 105}
79a9becd 106EXPORT_SYMBOL_GPL(gpiod_to_chip);
d2876d08 107
8d0aab2f
AV
108/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
109static int gpiochip_find_base(int ngpio)
110{
83cabe33
AC
111 struct gpio_chip *chip;
112 int base = ARCH_NR_GPIOS - ngpio;
8d0aab2f 113
83cabe33
AC
114 list_for_each_entry_reverse(chip, &gpio_chips, list) {
115 /* found a free space? */
116 if (chip->base + chip->ngpio <= base)
117 break;
118 else
119 /* nope, check the space right before the chip */
120 base = chip->base - ngpio;
8d0aab2f
AV
121 }
122
83cabe33 123 if (gpio_is_valid(base)) {
8d0aab2f 124 pr_debug("%s: found new base at %d\n", __func__, base);
83cabe33
AC
125 return base;
126 } else {
127 pr_err("%s: cannot find free range\n", __func__);
128 return -ENOSPC;
169b6a7a 129 }
169b6a7a
AV
130}
131
79a9becd
AC
132/**
133 * gpiod_get_direction - return the current direction of a GPIO
134 * @desc: GPIO to get the direction of
135 *
136 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
137 *
138 * This function may sleep if gpiod_cansleep() is true.
139 */
140int gpiod_get_direction(const struct gpio_desc *desc)
80b0a602
MN
141{
142 struct gpio_chip *chip;
372e722e 143 unsigned offset;
80b0a602
MN
144 int status = -EINVAL;
145
372e722e
AC
146 chip = gpiod_to_chip(desc);
147 offset = gpio_chip_hwgpio(desc);
80b0a602
MN
148
149 if (!chip->get_direction)
150 return status;
151
372e722e 152 status = chip->get_direction(chip, offset);
80b0a602
MN
153 if (status > 0) {
154 /* GPIOF_DIR_IN, or other positive */
155 status = 1;
def63433
AC
156 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
157 * so it does not affect constness per se */
158 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
80b0a602
MN
159 }
160 if (status == 0) {
161 /* GPIOF_DIR_OUT */
def63433 162 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
80b0a602
MN
163 }
164 return status;
165}
79a9becd 166EXPORT_SYMBOL_GPL(gpiod_get_direction);
80b0a602 167
1a989d0f
AC
168/*
169 * Add a new chip to the global chips list, keeping the list of chips sorted
170 * by base order.
171 *
172 * Return -EBUSY if the new chip overlaps with some other chip's integer
173 * space.
174 */
175static int gpiochip_add_to_list(struct gpio_chip *chip)
176{
177 struct list_head *pos = &gpio_chips;
178 struct gpio_chip *_chip;
179 int err = 0;
180
181 /* find where to insert our chip */
182 list_for_each(pos, &gpio_chips) {
183 _chip = list_entry(pos, struct gpio_chip, list);
184 /* shall we insert before _chip? */
185 if (_chip->base >= chip->base + chip->ngpio)
186 break;
187 }
188
189 /* are we stepping on the chip right before? */
190 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
191 _chip = list_entry(pos->prev, struct gpio_chip, list);
192 if (_chip->base + _chip->ngpio > chip->base) {
193 dev_err(chip->dev,
194 "GPIO integer space overlap, cannot add chip\n");
195 err = -EBUSY;
196 }
197 }
198
199 if (!err)
200 list_add_tail(&chip->list, pos);
201
202 return err;
203}
204
d2876d08
DB
205/**
206 * gpiochip_add() - register a gpio_chip
207 * @chip: the chip to register, with chip->base initialized
208 * Context: potentially before irqs or kmalloc will work
209 *
210 * Returns a negative errno if the chip can't be registered, such as
211 * because the chip->base is invalid or already associated with a
212 * different chip. Otherwise it returns zero as a success code.
8d0aab2f 213 *
d8f388d8
DB
214 * When gpiochip_add() is called very early during boot, so that GPIOs
215 * can be freely used, the chip->dev device must be registered before
216 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
217 * for GPIOs will fail rudely.
218 *
8d0aab2f
AV
219 * If chip->base is negative, this requests dynamic assignment of
220 * a range of valid GPIOs.
d2876d08
DB
221 */
222int gpiochip_add(struct gpio_chip *chip)
223{
224 unsigned long flags;
225 int status = 0;
226 unsigned id;
8d0aab2f 227 int base = chip->base;
d2876d08 228
bff5fda9 229 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
8d0aab2f 230 && base >= 0) {
d2876d08
DB
231 status = -EINVAL;
232 goto fail;
233 }
234
235 spin_lock_irqsave(&gpio_lock, flags);
236
8d0aab2f
AV
237 if (base < 0) {
238 base = gpiochip_find_base(chip->ngpio);
239 if (base < 0) {
240 status = base;
d8f388d8 241 goto unlock;
8d0aab2f
AV
242 }
243 chip->base = base;
244 }
245
1a989d0f
AC
246 status = gpiochip_add_to_list(chip);
247
d2876d08 248 if (status == 0) {
6c0b4e6c
AC
249 chip->desc = &gpio_desc[chip->base];
250
251 for (id = 0; id < chip->ngpio; id++) {
252 struct gpio_desc *desc = &chip->desc[id];
253 desc->chip = chip;
d8f388d8
DB
254
255 /* REVISIT: most hardware initializes GPIOs as
256 * inputs (often with pullups enabled) so power
257 * usage is minimized. Linux code should set the
258 * gpio direction first thing; but until it does,
80b0a602 259 * and in case chip->get_direction is not set,
d8f388d8
DB
260 * we may expose the wrong direction in sysfs.
261 */
6c0b4e6c 262 desc->flags = !chip->direction_input
d8f388d8
DB
263 ? (1 << FLAG_IS_OUT)
264 : 0;
d2876d08
DB
265 }
266 }
267
3bae4811
ZG
268 spin_unlock_irqrestore(&gpio_lock, flags);
269
f23f1516
SH
270#ifdef CONFIG_PINCTRL
271 INIT_LIST_HEAD(&chip->pin_ranges);
272#endif
273
391c970c 274 of_gpiochip_add(chip);
664e3e5a 275 acpi_gpiochip_add(chip);
391c970c 276
cedb1881
AV
277 if (status)
278 goto fail;
279
280 status = gpiochip_export(chip);
281 if (status)
282 goto fail;
283
7589e59f 284 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
64842aad
GL
285 chip->base, chip->base + chip->ngpio - 1,
286 chip->label ? : "generic");
287
cedb1881 288 return 0;
3bae4811
ZG
289
290unlock:
291 spin_unlock_irqrestore(&gpio_lock, flags);
d2876d08
DB
292fail:
293 /* failures here can mean systems won't boot... */
7589e59f 294 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
cedb1881
AV
295 chip->base, chip->base + chip->ngpio - 1,
296 chip->label ? : "generic");
d2876d08
DB
297 return status;
298}
299EXPORT_SYMBOL_GPL(gpiochip_add);
300
14250520
LW
301/* Forward-declaration */
302static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
303
d2876d08
DB
304/**
305 * gpiochip_remove() - unregister a gpio_chip
306 * @chip: the chip to unregister
307 *
308 * A gpio_chip with any GPIOs still requested may not be removed.
309 */
310int gpiochip_remove(struct gpio_chip *chip)
311{
312 unsigned long flags;
313 int status = 0;
314 unsigned id;
315
6072b9dc
MW
316 acpi_gpiochip_remove(chip);
317
d2876d08
DB
318 spin_lock_irqsave(&gpio_lock, flags);
319
14250520 320 gpiochip_irqchip_remove(chip);
9ef0d6f7 321 gpiochip_remove_pin_ranges(chip);
391c970c
AV
322 of_gpiochip_remove(chip);
323
6c0b4e6c
AC
324 for (id = 0; id < chip->ngpio; id++) {
325 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
d2876d08
DB
326 status = -EBUSY;
327 break;
328 }
329 }
330 if (status == 0) {
6c0b4e6c
AC
331 for (id = 0; id < chip->ngpio; id++)
332 chip->desc[id].chip = NULL;
1a989d0f
AC
333
334 list_del(&chip->list);
d2876d08
DB
335 }
336
337 spin_unlock_irqrestore(&gpio_lock, flags);
d8f388d8
DB
338
339 if (status == 0)
340 gpiochip_unexport(chip);
341
d2876d08
DB
342 return status;
343}
344EXPORT_SYMBOL_GPL(gpiochip_remove);
345
594fa265
GL
346/**
347 * gpiochip_find() - iterator for locating a specific gpio_chip
348 * @data: data to pass to match function
349 * @callback: Callback function to check gpio_chip
350 *
351 * Similar to bus_find_device. It returns a reference to a gpio_chip as
352 * determined by a user supplied @match callback. The callback should return
353 * 0 if the device doesn't match and non-zero if it does. If the callback is
354 * non-zero, this function will return to the caller and not iterate over any
355 * more gpio_chips.
356 */
07ce8ec7 357struct gpio_chip *gpiochip_find(void *data,
6e2cf651 358 int (*match)(struct gpio_chip *chip,
3d0f7cf0 359 void *data))
594fa265 360{
125eef96 361 struct gpio_chip *chip;
594fa265 362 unsigned long flags;
594fa265
GL
363
364 spin_lock_irqsave(&gpio_lock, flags);
125eef96
AC
365 list_for_each_entry(chip, &gpio_chips, list)
366 if (match(chip, data))
594fa265 367 break;
125eef96
AC
368
369 /* No match? */
370 if (&chip->list == &gpio_chips)
371 chip = NULL;
594fa265
GL
372 spin_unlock_irqrestore(&gpio_lock, flags);
373
374 return chip;
375}
8fa0c9bf 376EXPORT_SYMBOL_GPL(gpiochip_find);
d2876d08 377
79697ef9
AC
378static int gpiochip_match_name(struct gpio_chip *chip, void *data)
379{
380 const char *name = data;
381
382 return !strcmp(chip->label, name);
383}
384
385static struct gpio_chip *find_chip_by_name(const char *name)
386{
387 return gpiochip_find((void *)name, gpiochip_match_name);
388}
389
14250520
LW
390#ifdef CONFIG_GPIOLIB_IRQCHIP
391
392/*
393 * The following is irqchip helper code for gpiochips.
394 */
395
396/**
397 * gpiochip_add_chained_irqchip() - adds a chained irqchip to a gpiochip
398 * @gpiochip: the gpiochip to add the irqchip to
399 * @irqchip: the irqchip to add to the gpiochip
400 * @parent_irq: the irq number corresponding to the parent IRQ for this
401 * chained irqchip
402 * @parent_handler: the parent interrupt handler for the accumulated IRQ
403 * coming out of the gpiochip
404 */
405void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
406 struct irq_chip *irqchip,
407 int parent_irq,
408 irq_flow_handler_t parent_handler)
409{
1c8732bb
LW
410 if (gpiochip->can_sleep) {
411 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
412 return;
413 }
414
14250520
LW
415 irq_set_chained_handler(parent_irq, parent_handler);
416 /*
417 * The parent irqchip is already using the chip_data for this
418 * irqchip, so our callbacks simply use the handler_data.
419 */
420 irq_set_handler_data(parent_irq, gpiochip);
421}
422EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
423
e45d1c80
LW
424/*
425 * This lock class tells lockdep that GPIO irqs are in a different
426 * category than their parents, so it won't report false recursion.
427 */
428static struct lock_class_key gpiochip_irq_lock_class;
429
14250520
LW
430/**
431 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
432 * @d: the irqdomain used by this irqchip
433 * @irq: the global irq number used by this GPIO irqchip irq
434 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
435 *
436 * This function will set up the mapping for a certain IRQ line on a
437 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
438 * stored inside the gpiochip.
439 */
440static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
441 irq_hw_number_t hwirq)
442{
443 struct gpio_chip *chip = d->host_data;
444
14250520 445 irq_set_chip_data(irq, chip);
e45d1c80 446 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
7633fb95 447 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
1c8732bb
LW
448 /* Chips that can sleep need nested thread handlers */
449 if (chip->can_sleep)
450 irq_set_nested_thread(irq, 1);
14250520
LW
451#ifdef CONFIG_ARM
452 set_irq_flags(irq, IRQF_VALID);
453#else
454 irq_set_noprobe(irq);
455#endif
1333b90f
LW
456 /*
457 * No set-up of the hardware will happen if IRQ_TYPE_NONE
458 * is passed as default type.
459 */
460 if (chip->irq_default_type != IRQ_TYPE_NONE)
461 irq_set_irq_type(irq, chip->irq_default_type);
14250520
LW
462
463 return 0;
464}
465
c3626fde
LW
466static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
467{
1c8732bb
LW
468 struct gpio_chip *chip = d->host_data;
469
c3626fde
LW
470#ifdef CONFIG_ARM
471 set_irq_flags(irq, 0);
472#endif
1c8732bb
LW
473 if (chip->can_sleep)
474 irq_set_nested_thread(irq, 0);
c3626fde
LW
475 irq_set_chip_and_handler(irq, NULL, NULL);
476 irq_set_chip_data(irq, NULL);
477}
478
14250520
LW
479static const struct irq_domain_ops gpiochip_domain_ops = {
480 .map = gpiochip_irq_map,
c3626fde 481 .unmap = gpiochip_irq_unmap,
14250520
LW
482 /* Virtually all GPIO irqchips are twocell:ed */
483 .xlate = irq_domain_xlate_twocell,
484};
485
486static int gpiochip_irq_reqres(struct irq_data *d)
487{
488 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
489
490 if (gpio_lock_as_irq(chip, d->hwirq)) {
491 chip_err(chip,
492 "unable to lock HW IRQ %lu for IRQ\n",
493 d->hwirq);
494 return -EINVAL;
495 }
496 return 0;
497}
498
499static void gpiochip_irq_relres(struct irq_data *d)
500{
501 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
502
503 gpio_unlock_as_irq(chip, d->hwirq);
504}
505
506static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
507{
508 return irq_find_mapping(chip->irqdomain, offset);
509}
510
511/**
512 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
513 * @gpiochip: the gpiochip to remove the irqchip from
514 *
515 * This is called only from gpiochip_remove()
516 */
517static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
518{
c3626fde
LW
519 unsigned int offset;
520
521 /* Remove all IRQ mappings and delete the domain */
522 if (gpiochip->irqdomain) {
523 for (offset = 0; offset < gpiochip->ngpio; offset++)
524 irq_dispose_mapping(gpiochip->irq_base + offset);
14250520 525 irq_domain_remove(gpiochip->irqdomain);
c3626fde 526 }
14250520
LW
527
528 if (gpiochip->irqchip) {
529 gpiochip->irqchip->irq_request_resources = NULL;
530 gpiochip->irqchip->irq_release_resources = NULL;
531 gpiochip->irqchip = NULL;
532 }
533}
534
535/**
536 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
537 * @gpiochip: the gpiochip to add the irqchip to
538 * @irqchip: the irqchip to add to the gpiochip
539 * @first_irq: if not dynamically assigned, the base (first) IRQ to
540 * allocate gpiochip irqs from
541 * @handler: the irq handler to use (often a predefined irq core function)
1333b90f
LW
542 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
543 * to have the core avoid setting up any default type in the hardware.
14250520
LW
544 *
545 * This function closely associates a certain irqchip with a certain
546 * gpiochip, providing an irq domain to translate the local IRQs to
547 * global irqs in the gpiolib core, and making sure that the gpiochip
548 * is passed as chip data to all related functions. Driver callbacks
549 * need to use container_of() to get their local state containers back
550 * from the gpiochip passed as chip data. An irqdomain will be stored
551 * in the gpiochip that shall be used by the driver to handle IRQ number
552 * translation. The gpiochip will need to be initialized and registered
553 * before calling this function.
554 *
c3626fde
LW
555 * This function will handle two cell:ed simple IRQs and assumes all
556 * the pins on the gpiochip can generate a unique IRQ. Everything else
14250520
LW
557 * need to be open coded.
558 */
559int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
560 struct irq_chip *irqchip,
561 unsigned int first_irq,
562 irq_flow_handler_t handler,
563 unsigned int type)
564{
565 struct device_node *of_node;
566 unsigned int offset;
c3626fde 567 unsigned irq_base = 0;
14250520
LW
568
569 if (!gpiochip || !irqchip)
570 return -EINVAL;
571
572 if (!gpiochip->dev) {
573 pr_err("missing gpiochip .dev parent pointer\n");
574 return -EINVAL;
575 }
576 of_node = gpiochip->dev->of_node;
577#ifdef CONFIG_OF_GPIO
578 /*
579 * If the gpiochip has an assigned OF node this takes precendence
580 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
581 */
582 if (gpiochip->of_node)
583 of_node = gpiochip->of_node;
584#endif
585 gpiochip->irqchip = irqchip;
586 gpiochip->irq_handler = handler;
587 gpiochip->irq_default_type = type;
588 gpiochip->to_irq = gpiochip_to_irq;
589 gpiochip->irqdomain = irq_domain_add_simple(of_node,
590 gpiochip->ngpio, first_irq,
591 &gpiochip_domain_ops, gpiochip);
592 if (!gpiochip->irqdomain) {
593 gpiochip->irqchip = NULL;
594 return -EINVAL;
595 }
596 irqchip->irq_request_resources = gpiochip_irq_reqres;
597 irqchip->irq_release_resources = gpiochip_irq_relres;
598
599 /*
600 * Prepare the mapping since the irqchip shall be orthogonal to
601 * any gpiochip calls. If the first_irq was zero, this is
602 * necessary to allocate descriptors for all IRQs.
603 */
c3626fde
LW
604 for (offset = 0; offset < gpiochip->ngpio; offset++) {
605 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
606 if (offset == 0)
607 /*
608 * Store the base into the gpiochip to be used when
609 * unmapping the irqs.
610 */
611 gpiochip->irq_base = irq_base;
612 }
14250520
LW
613
614 return 0;
615}
616EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
617
618#else /* CONFIG_GPIOLIB_IRQCHIP */
619
620static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
621
622#endif /* CONFIG_GPIOLIB_IRQCHIP */
623
f23f1516 624#ifdef CONFIG_PINCTRL
165adc9c 625
586a87e6
CR
626/**
627 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
628 * @chip: the gpiochip to add the range for
629 * @pinctrl: the dev_name() of the pin controller to map to
630 * @gpio_offset: the start offset in the current gpio_chip number space
631 * @pin_group: name of the pin group inside the pin controller
632 */
633int gpiochip_add_pingroup_range(struct gpio_chip *chip,
634 struct pinctrl_dev *pctldev,
635 unsigned int gpio_offset, const char *pin_group)
636{
637 struct gpio_pin_range *pin_range;
638 int ret;
639
640 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
641 if (!pin_range) {
1a2a99c6 642 chip_err(chip, "failed to allocate pin ranges\n");
586a87e6
CR
643 return -ENOMEM;
644 }
645
646 /* Use local offset as range ID */
647 pin_range->range.id = gpio_offset;
648 pin_range->range.gc = chip;
649 pin_range->range.name = chip->label;
650 pin_range->range.base = chip->base + gpio_offset;
651 pin_range->pctldev = pctldev;
652
653 ret = pinctrl_get_group_pins(pctldev, pin_group,
654 &pin_range->range.pins,
655 &pin_range->range.npins);
61c6375d
MN
656 if (ret < 0) {
657 kfree(pin_range);
586a87e6 658 return ret;
61c6375d 659 }
586a87e6
CR
660
661 pinctrl_add_gpio_range(pctldev, &pin_range->range);
662
1a2a99c6
AS
663 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
664 gpio_offset, gpio_offset + pin_range->range.npins - 1,
586a87e6
CR
665 pinctrl_dev_get_devname(pctldev), pin_group);
666
667 list_add_tail(&pin_range->node, &chip->pin_ranges);
668
669 return 0;
670}
671EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
672
3f0f8670
LW
673/**
674 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
675 * @chip: the gpiochip to add the range for
676 * @pinctrl_name: the dev_name() of the pin controller to map to
316511c0
LW
677 * @gpio_offset: the start offset in the current gpio_chip number space
678 * @pin_offset: the start offset in the pin controller number space
3f0f8670
LW
679 * @npins: the number of pins from the offset of each pin space (GPIO and
680 * pin controller) to accumulate in this range
681 */
1e63d7b9 682int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
316511c0 683 unsigned int gpio_offset, unsigned int pin_offset,
3f0f8670 684 unsigned int npins)
f23f1516
SH
685{
686 struct gpio_pin_range *pin_range;
b4d4b1f0 687 int ret;
f23f1516 688
3f0f8670 689 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
f23f1516 690 if (!pin_range) {
1a2a99c6 691 chip_err(chip, "failed to allocate pin ranges\n");
1e63d7b9 692 return -ENOMEM;
f23f1516
SH
693 }
694
3f0f8670 695 /* Use local offset as range ID */
316511c0 696 pin_range->range.id = gpio_offset;
3f0f8670 697 pin_range->range.gc = chip;
f23f1516 698 pin_range->range.name = chip->label;
316511c0
LW
699 pin_range->range.base = chip->base + gpio_offset;
700 pin_range->range.pin_base = pin_offset;
f23f1516 701 pin_range->range.npins = npins;
192c369c 702 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
f23f1516 703 &pin_range->range);
8f23ca1a 704 if (IS_ERR(pin_range->pctldev)) {
b4d4b1f0 705 ret = PTR_ERR(pin_range->pctldev);
1a2a99c6 706 chip_err(chip, "could not create pin range\n");
3f0f8670 707 kfree(pin_range);
b4d4b1f0 708 return ret;
3f0f8670 709 }
1a2a99c6
AS
710 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
711 gpio_offset, gpio_offset + npins - 1,
316511c0
LW
712 pinctl_name,
713 pin_offset, pin_offset + npins - 1);
f23f1516
SH
714
715 list_add_tail(&pin_range->node, &chip->pin_ranges);
1e63d7b9
LW
716
717 return 0;
f23f1516 718}
165adc9c 719EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
f23f1516 720
3f0f8670
LW
721/**
722 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
723 * @chip: the chip to remove all the mappings for
724 */
f23f1516
SH
725void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
726{
727 struct gpio_pin_range *pin_range, *tmp;
728
729 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
730 list_del(&pin_range->node);
731 pinctrl_remove_gpio_range(pin_range->pctldev,
732 &pin_range->range);
3f0f8670 733 kfree(pin_range);
f23f1516
SH
734 }
735}
165adc9c
LW
736EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
737
738#endif /* CONFIG_PINCTRL */
f23f1516 739
d2876d08
DB
740/* These "optional" allocation calls help prevent drivers from stomping
741 * on each other, and help provide better diagnostics in debugfs.
742 * They're called even less than the "set direction" calls.
743 */
77c2d792 744static int __gpiod_request(struct gpio_desc *desc, const char *label)
d2876d08 745{
77c2d792
MW
746 struct gpio_chip *chip = desc->chip;
747 int status;
d2876d08
DB
748 unsigned long flags;
749
bcabdef1
AC
750 spin_lock_irqsave(&gpio_lock, flags);
751
d2876d08 752 /* NOTE: gpio_request() can be called in early boot,
35e8bb51 753 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
d2876d08
DB
754 */
755
756 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
757 desc_set_label(desc, label ? : "?");
758 status = 0;
438d8908 759 } else {
d2876d08 760 status = -EBUSY;
7460db56 761 goto done;
35e8bb51
DB
762 }
763
764 if (chip->request) {
765 /* chip->request may sleep */
766 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 767 status = chip->request(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
768 spin_lock_irqsave(&gpio_lock, flags);
769
770 if (status < 0) {
771 desc_set_label(desc, NULL);
35e8bb51 772 clear_bit(FLAG_REQUESTED, &desc->flags);
80b0a602 773 goto done;
35e8bb51 774 }
438d8908 775 }
80b0a602
MN
776 if (chip->get_direction) {
777 /* chip->get_direction may sleep */
778 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 779 gpiod_get_direction(desc);
80b0a602
MN
780 spin_lock_irqsave(&gpio_lock, flags);
781 }
77c2d792
MW
782done:
783 spin_unlock_irqrestore(&gpio_lock, flags);
784 return status;
785}
786
0eb4c6c2 787int gpiod_request(struct gpio_desc *desc, const char *label)
77c2d792
MW
788{
789 int status = -EPROBE_DEFER;
790 struct gpio_chip *chip;
791
792 if (!desc) {
793 pr_warn("%s: invalid GPIO\n", __func__);
794 return -EINVAL;
795 }
796
797 chip = desc->chip;
798 if (!chip)
799 goto done;
800
801 if (try_module_get(chip->owner)) {
802 status = __gpiod_request(desc, label);
803 if (status < 0)
804 module_put(chip->owner);
805 }
806
d2876d08
DB
807done:
808 if (status)
7589e59f 809 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
77c2d792 810
d2876d08
DB
811 return status;
812}
372e722e 813
77c2d792 814static bool __gpiod_free(struct gpio_desc *desc)
d2876d08 815{
77c2d792 816 bool ret = false;
d2876d08 817 unsigned long flags;
35e8bb51 818 struct gpio_chip *chip;
d2876d08 819
3d599d1c
UKK
820 might_sleep();
821
372e722e 822 gpiod_unexport(desc);
d8f388d8 823
d2876d08
DB
824 spin_lock_irqsave(&gpio_lock, flags);
825
35e8bb51
DB
826 chip = desc->chip;
827 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
828 if (chip->free) {
829 spin_unlock_irqrestore(&gpio_lock, flags);
9c4ba946 830 might_sleep_if(chip->can_sleep);
372e722e 831 chip->free(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
832 spin_lock_irqsave(&gpio_lock, flags);
833 }
d2876d08 834 desc_set_label(desc, NULL);
07697461 835 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
35e8bb51 836 clear_bit(FLAG_REQUESTED, &desc->flags);
aca5ce14 837 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
25553ff0 838 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
77c2d792
MW
839 ret = true;
840 }
d2876d08
DB
841
842 spin_unlock_irqrestore(&gpio_lock, flags);
77c2d792
MW
843 return ret;
844}
845
0eb4c6c2 846void gpiod_free(struct gpio_desc *desc)
77c2d792
MW
847{
848 if (desc && __gpiod_free(desc))
849 module_put(desc->chip->owner);
850 else
851 WARN_ON(extra_checks);
d2876d08 852}
372e722e 853
d2876d08
DB
854/**
855 * gpiochip_is_requested - return string iff signal was requested
856 * @chip: controller managing the signal
857 * @offset: of signal within controller's 0..(ngpio - 1) range
858 *
859 * Returns NULL if the GPIO is not currently requested, else a string.
9c8318ff
AC
860 * The string returned is the label passed to gpio_request(); if none has been
861 * passed it is a meaningless, non-NULL constant.
d2876d08
DB
862 *
863 * This function is for use by GPIO controller drivers. The label can
864 * help with diagnostics, and knowing that the signal is used as a GPIO
865 * can help avoid accidentally multiplexing it to another controller.
866 */
867const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
868{
6c0b4e6c 869 struct gpio_desc *desc;
d2876d08 870
6c0b4e6c 871 if (!GPIO_OFFSET_VALID(chip, offset))
d2876d08 872 return NULL;
6c0b4e6c
AC
873
874 desc = &chip->desc[offset];
875
372e722e 876 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
d2876d08 877 return NULL;
372e722e 878 return desc->label;
d2876d08
DB
879}
880EXPORT_SYMBOL_GPL(gpiochip_is_requested);
881
77c2d792
MW
882/**
883 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
884 * @desc: GPIO descriptor to request
885 * @label: label for the GPIO
886 *
887 * Function allows GPIO chip drivers to request and use their own GPIO
888 * descriptors via gpiolib API. Difference to gpiod_request() is that this
889 * function will not increase reference count of the GPIO chip module. This
890 * allows the GPIO chip module to be unloaded as needed (we assume that the
891 * GPIO chip driver handles freeing the GPIOs it has requested).
892 */
893int gpiochip_request_own_desc(struct gpio_desc *desc, const char *label)
894{
895 if (!desc || !desc->chip)
896 return -EINVAL;
897
898 return __gpiod_request(desc, label);
899}
f7d4ad98 900EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
77c2d792
MW
901
902/**
903 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
904 * @desc: GPIO descriptor to free
905 *
906 * Function frees the given GPIO requested previously with
907 * gpiochip_request_own_desc().
908 */
909void gpiochip_free_own_desc(struct gpio_desc *desc)
910{
911 if (desc)
912 __gpiod_free(desc);
913}
f7d4ad98 914EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
d2876d08
DB
915
916/* Drivers MUST set GPIO direction before making get/set calls. In
917 * some cases this is done in early boot, before IRQs are enabled.
918 *
919 * As a rule these aren't called more than once (except for drivers
920 * using the open-drain emulation idiom) so these are natural places
921 * to accumulate extra debugging checks. Note that we can't (yet)
922 * rely on gpio_request() having been called beforehand.
923 */
924
79a9becd
AC
925/**
926 * gpiod_direction_input - set the GPIO direction to input
927 * @desc: GPIO to set to input
928 *
929 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
930 * be called safely on it.
931 *
932 * Return 0 in case of success, else an error code.
933 */
934int gpiod_direction_input(struct gpio_desc *desc)
d2876d08 935{
d2876d08 936 struct gpio_chip *chip;
d2876d08
DB
937 int status = -EINVAL;
938
be1a4b13 939 if (!desc || !desc->chip) {
bcabdef1
AC
940 pr_warn("%s: invalid GPIO\n", __func__);
941 return -EINVAL;
942 }
943
be1a4b13
LW
944 chip = desc->chip;
945 if (!chip->get || !chip->direction_input) {
6424de5a
MB
946 gpiod_warn(desc,
947 "%s: missing get() or direction_input() operations\n",
7589e59f 948 __func__);
be1a4b13
LW
949 return -EIO;
950 }
951
d82da797 952 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
d2876d08
DB
953 if (status == 0)
954 clear_bit(FLAG_IS_OUT, &desc->flags);
3f397c21 955
372e722e 956 trace_gpio_direction(desc_to_gpio(desc), 1, status);
d82da797 957
d2876d08
DB
958 return status;
959}
79a9becd 960EXPORT_SYMBOL_GPL(gpiod_direction_input);
372e722e 961
ef70bbe1 962static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
d2876d08 963{
d2876d08 964 struct gpio_chip *chip;
d2876d08
DB
965 int status = -EINVAL;
966
d468bf9e
LW
967 /* GPIOs used for IRQs shall not be set as output */
968 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
969 gpiod_err(desc,
970 "%s: tried to set a GPIO tied to an IRQ as output\n",
971 __func__);
972 return -EIO;
973 }
974
aca5ce14
LD
975 /* Open drain pin should not be driven to 1 */
976 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
372e722e 977 return gpiod_direction_input(desc);
aca5ce14 978
25553ff0
LD
979 /* Open source pin should not be driven to 0 */
980 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
372e722e 981 return gpiod_direction_input(desc);
25553ff0 982
be1a4b13
LW
983 chip = desc->chip;
984 if (!chip->set || !chip->direction_output) {
6424de5a
MB
985 gpiod_warn(desc,
986 "%s: missing set() or direction_output() operations\n",
987 __func__);
be1a4b13
LW
988 return -EIO;
989 }
990
d82da797 991 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
d2876d08
DB
992 if (status == 0)
993 set_bit(FLAG_IS_OUT, &desc->flags);
372e722e
AC
994 trace_gpio_value(desc_to_gpio(desc), 0, value);
995 trace_gpio_direction(desc_to_gpio(desc), 0, status);
d2876d08
DB
996 return status;
997}
ef70bbe1
PZ
998
999/**
1000 * gpiod_direction_output_raw - set the GPIO direction to output
1001 * @desc: GPIO to set to output
1002 * @value: initial output value of the GPIO
1003 *
1004 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1005 * be called safely on it. The initial value of the output must be specified
1006 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1007 *
1008 * Return 0 in case of success, else an error code.
1009 */
1010int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1011{
1012 if (!desc || !desc->chip) {
1013 pr_warn("%s: invalid GPIO\n", __func__);
1014 return -EINVAL;
1015 }
1016 return _gpiod_direction_output_raw(desc, value);
1017}
1018EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1019
1020/**
90df4fe0 1021 * gpiod_direction_output - set the GPIO direction to output
ef70bbe1
PZ
1022 * @desc: GPIO to set to output
1023 * @value: initial output value of the GPIO
1024 *
1025 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1026 * be called safely on it. The initial value of the output must be specified
1027 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1028 * account.
1029 *
1030 * Return 0 in case of success, else an error code.
1031 */
1032int gpiod_direction_output(struct gpio_desc *desc, int value)
1033{
1034 if (!desc || !desc->chip) {
1035 pr_warn("%s: invalid GPIO\n", __func__);
1036 return -EINVAL;
1037 }
1038 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1039 value = !value;
1040 return _gpiod_direction_output_raw(desc, value);
1041}
79a9becd 1042EXPORT_SYMBOL_GPL(gpiod_direction_output);
d2876d08 1043
c4b5be98 1044/**
79a9becd 1045 * gpiod_set_debounce - sets @debounce time for a @gpio
c4b5be98
FB
1046 * @gpio: the gpio to set debounce time
1047 * @debounce: debounce time is microseconds
65d87656
LW
1048 *
1049 * returns -ENOTSUPP if the controller does not support setting
1050 * debounce.
c4b5be98 1051 */
79a9becd 1052int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
c4b5be98 1053{
c4b5be98 1054 struct gpio_chip *chip;
c4b5be98 1055
be1a4b13 1056 if (!desc || !desc->chip) {
bcabdef1
AC
1057 pr_warn("%s: invalid GPIO\n", __func__);
1058 return -EINVAL;
1059 }
1060
c4b5be98 1061 chip = desc->chip;
be1a4b13 1062 if (!chip->set || !chip->set_debounce) {
6424de5a
MB
1063 gpiod_dbg(desc,
1064 "%s: missing set() or set_debounce() operations\n",
1065 __func__);
65d87656 1066 return -ENOTSUPP;
be1a4b13
LW
1067 }
1068
d82da797 1069 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
c4b5be98 1070}
79a9becd 1071EXPORT_SYMBOL_GPL(gpiod_set_debounce);
372e722e 1072
79a9becd
AC
1073/**
1074 * gpiod_is_active_low - test whether a GPIO is active-low or not
1075 * @desc: the gpio descriptor to test
1076 *
1077 * Returns 1 if the GPIO is active-low, 0 otherwise.
1078 */
1079int gpiod_is_active_low(const struct gpio_desc *desc)
372e722e 1080{
79a9becd 1081 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
372e722e 1082}
79a9becd 1083EXPORT_SYMBOL_GPL(gpiod_is_active_low);
d2876d08
DB
1084
1085/* I/O calls are only valid after configuration completed; the relevant
1086 * "is this a valid GPIO" error checks should already have been done.
1087 *
1088 * "Get" operations are often inlinable as reading a pin value register,
1089 * and masking the relevant bit in that register.
1090 *
1091 * When "set" operations are inlinable, they involve writing that mask to
1092 * one register to set a low value, or a different register to set it high.
1093 * Otherwise locking is needed, so there may be little value to inlining.
1094 *
1095 *------------------------------------------------------------------------
1096 *
1097 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1098 * have requested the GPIO. That can include implicit requesting by
1099 * a direction setting call. Marking a gpio as requested locks its chip
1100 * in memory, guaranteeing that these table lookups need no more locking
1101 * and that gpiochip_remove() will fail.
1102 *
1103 * REVISIT when debugging, consider adding some instrumentation to ensure
1104 * that the GPIO was actually requested.
1105 */
1106
23600969 1107static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08
DB
1108{
1109 struct gpio_chip *chip;
23600969 1110 bool value;
372e722e 1111 int offset;
d2876d08 1112
372e722e
AC
1113 chip = desc->chip;
1114 offset = gpio_chip_hwgpio(desc);
23600969 1115 value = chip->get ? chip->get(chip, offset) : false;
372e722e 1116 trace_gpio_value(desc_to_gpio(desc), 1, value);
3f397c21 1117 return value;
d2876d08 1118}
372e722e 1119
d2876d08 1120/**
79a9becd
AC
1121 * gpiod_get_raw_value() - return a gpio's raw value
1122 * @desc: gpio whose value will be returned
d2876d08 1123 *
79a9becd
AC
1124 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1125 * its ACTIVE_LOW status.
1126 *
1127 * This function should be called from contexts where we cannot sleep, and will
1128 * complain if the GPIO chip functions potentially sleep.
d2876d08 1129 */
79a9becd 1130int gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08 1131{
bcabdef1
AC
1132 if (!desc)
1133 return 0;
e4e449e8 1134 /* Should be using gpio_get_value_cansleep() */
d8e0ac08 1135 WARN_ON(desc->chip->can_sleep);
79a9becd 1136 return _gpiod_get_raw_value(desc);
d2876d08 1137}
79a9becd 1138EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
372e722e 1139
79a9becd
AC
1140/**
1141 * gpiod_get_value() - return a gpio's value
1142 * @desc: gpio whose value will be returned
1143 *
1144 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1145 * account.
1146 *
1147 * This function should be called from contexts where we cannot sleep, and will
1148 * complain if the GPIO chip functions potentially sleep.
1149 */
1150int gpiod_get_value(const struct gpio_desc *desc)
372e722e 1151{
79a9becd
AC
1152 int value;
1153 if (!desc)
1154 return 0;
1155 /* Should be using gpio_get_value_cansleep() */
1156 WARN_ON(desc->chip->can_sleep);
1157
1158 value = _gpiod_get_raw_value(desc);
1159 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1160 value = !value;
1161
1162 return value;
372e722e 1163}
79a9becd 1164EXPORT_SYMBOL_GPL(gpiod_get_value);
d2876d08 1165
aca5ce14
LD
1166/*
1167 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
79a9becd 1168 * @desc: gpio descriptor whose state need to be set.
aca5ce14
LD
1169 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1170 */
23600969 1171static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
aca5ce14
LD
1172{
1173 int err = 0;
372e722e
AC
1174 struct gpio_chip *chip = desc->chip;
1175 int offset = gpio_chip_hwgpio(desc);
1176
aca5ce14 1177 if (value) {
372e722e 1178 err = chip->direction_input(chip, offset);
aca5ce14 1179 if (!err)
372e722e 1180 clear_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 1181 } else {
372e722e 1182 err = chip->direction_output(chip, offset, 0);
aca5ce14 1183 if (!err)
372e722e 1184 set_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 1185 }
372e722e 1186 trace_gpio_direction(desc_to_gpio(desc), value, err);
aca5ce14 1187 if (err < 0)
6424de5a
MB
1188 gpiod_err(desc,
1189 "%s: Error in set_value for open drain err %d\n",
1190 __func__, err);
aca5ce14
LD
1191}
1192
25553ff0 1193/*
79a9becd
AC
1194 * _gpio_set_open_source_value() - Set the open source gpio's value.
1195 * @desc: gpio descriptor whose state need to be set.
25553ff0
LD
1196 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1197 */
23600969 1198static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
25553ff0
LD
1199{
1200 int err = 0;
372e722e
AC
1201 struct gpio_chip *chip = desc->chip;
1202 int offset = gpio_chip_hwgpio(desc);
1203
25553ff0 1204 if (value) {
372e722e 1205 err = chip->direction_output(chip, offset, 1);
25553ff0 1206 if (!err)
372e722e 1207 set_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 1208 } else {
372e722e 1209 err = chip->direction_input(chip, offset);
25553ff0 1210 if (!err)
372e722e 1211 clear_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 1212 }
372e722e 1213 trace_gpio_direction(desc_to_gpio(desc), !value, err);
25553ff0 1214 if (err < 0)
6424de5a
MB
1215 gpiod_err(desc,
1216 "%s: Error in set_value for open source err %d\n",
1217 __func__, err);
25553ff0
LD
1218}
1219
23600969 1220static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
d2876d08
DB
1221{
1222 struct gpio_chip *chip;
1223
372e722e 1224 chip = desc->chip;
372e722e
AC
1225 trace_gpio_value(desc_to_gpio(desc), 0, value);
1226 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1227 _gpio_set_open_drain_value(desc, value);
1228 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1229 _gpio_set_open_source_value(desc, value);
aca5ce14 1230 else
372e722e
AC
1231 chip->set(chip, gpio_chip_hwgpio(desc), value);
1232}
1233
d2876d08 1234/**
79a9becd
AC
1235 * gpiod_set_raw_value() - assign a gpio's raw value
1236 * @desc: gpio whose value will be assigned
d2876d08 1237 * @value: value to assign
d2876d08 1238 *
79a9becd
AC
1239 * Set the raw value of the GPIO, i.e. the value of its physical line without
1240 * regard for its ACTIVE_LOW status.
1241 *
1242 * This function should be called from contexts where we cannot sleep, and will
1243 * complain if the GPIO chip functions potentially sleep.
d2876d08 1244 */
79a9becd 1245void gpiod_set_raw_value(struct gpio_desc *desc, int value)
372e722e 1246{
bcabdef1
AC
1247 if (!desc)
1248 return;
e4e449e8 1249 /* Should be using gpio_set_value_cansleep() */
d8e0ac08 1250 WARN_ON(desc->chip->can_sleep);
79a9becd 1251 _gpiod_set_raw_value(desc, value);
d2876d08 1252}
79a9becd 1253EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
d2876d08
DB
1254
1255/**
79a9becd
AC
1256 * gpiod_set_value() - assign a gpio's value
1257 * @desc: gpio whose value will be assigned
1258 * @value: value to assign
1259 *
1260 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1261 * account
d2876d08 1262 *
79a9becd
AC
1263 * This function should be called from contexts where we cannot sleep, and will
1264 * complain if the GPIO chip functions potentially sleep.
d2876d08 1265 */
79a9becd 1266void gpiod_set_value(struct gpio_desc *desc, int value)
d2876d08 1267{
bcabdef1 1268 if (!desc)
79a9becd
AC
1269 return;
1270 /* Should be using gpio_set_value_cansleep() */
1271 WARN_ON(desc->chip->can_sleep);
1272 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1273 value = !value;
1274 _gpiod_set_raw_value(desc, value);
372e722e 1275}
79a9becd 1276EXPORT_SYMBOL_GPL(gpiod_set_value);
d2876d08 1277
d2876d08 1278/**
79a9becd
AC
1279 * gpiod_cansleep() - report whether gpio value access may sleep
1280 * @desc: gpio to check
d2876d08 1281 *
d2876d08 1282 */
79a9becd 1283int gpiod_cansleep(const struct gpio_desc *desc)
372e722e 1284{
bcabdef1
AC
1285 if (!desc)
1286 return 0;
372e722e 1287 return desc->chip->can_sleep;
d2876d08 1288}
79a9becd 1289EXPORT_SYMBOL_GPL(gpiod_cansleep);
d2876d08 1290
0f6d504e 1291/**
79a9becd
AC
1292 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1293 * @desc: gpio whose IRQ will be returned (already requested)
0f6d504e 1294 *
79a9becd
AC
1295 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1296 * error.
0f6d504e 1297 */
79a9becd 1298int gpiod_to_irq(const struct gpio_desc *desc)
0f6d504e
DB
1299{
1300 struct gpio_chip *chip;
372e722e 1301 int offset;
0f6d504e 1302
bcabdef1
AC
1303 if (!desc)
1304 return -EINVAL;
372e722e
AC
1305 chip = desc->chip;
1306 offset = gpio_chip_hwgpio(desc);
1307 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
0f6d504e 1308}
79a9becd 1309EXPORT_SYMBOL_GPL(gpiod_to_irq);
0f6d504e 1310
d468bf9e 1311/**
d74be6df
AC
1312 * gpio_lock_as_irq() - lock a GPIO to be used as IRQ
1313 * @chip: the chip the GPIO to lock belongs to
1314 * @offset: the offset of the GPIO to lock as IRQ
d468bf9e
LW
1315 *
1316 * This is used directly by GPIO drivers that want to lock down
f438acdf 1317 * a certain GPIO line to be used for IRQs.
d468bf9e 1318 */
d74be6df 1319int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
372e722e 1320{
d74be6df 1321 if (offset >= chip->ngpio)
d468bf9e
LW
1322 return -EINVAL;
1323
d74be6df
AC
1324 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1325 chip_err(chip,
d468bf9e
LW
1326 "%s: tried to flag a GPIO set as output for IRQ\n",
1327 __func__);
1328 return -EIO;
1329 }
1330
d74be6df 1331 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
d468bf9e 1332 return 0;
372e722e 1333}
d74be6df 1334EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
d2876d08 1335
d468bf9e 1336/**
d74be6df
AC
1337 * gpio_unlock_as_irq() - unlock a GPIO used as IRQ
1338 * @chip: the chip the GPIO to lock belongs to
1339 * @offset: the offset of the GPIO to lock as IRQ
d468bf9e
LW
1340 *
1341 * This is used directly by GPIO drivers that want to indicate
1342 * that a certain GPIO is no longer used exclusively for IRQ.
d2876d08 1343 */
d74be6df 1344void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
d468bf9e 1345{
d74be6df 1346 if (offset >= chip->ngpio)
d468bf9e 1347 return;
d2876d08 1348
d74be6df 1349 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
d468bf9e 1350}
d74be6df 1351EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
d468bf9e 1352
79a9becd
AC
1353/**
1354 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1355 * @desc: gpio whose value will be returned
1356 *
1357 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1358 * its ACTIVE_LOW status.
1359 *
1360 * This function is to be called from contexts that can sleep.
d2876d08 1361 */
79a9becd 1362int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
d2876d08 1363{
d2876d08 1364 might_sleep_if(extra_checks);
bcabdef1
AC
1365 if (!desc)
1366 return 0;
79a9becd 1367 return _gpiod_get_raw_value(desc);
d2876d08 1368}
79a9becd 1369EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
372e722e 1370
79a9becd
AC
1371/**
1372 * gpiod_get_value_cansleep() - return a gpio's value
1373 * @desc: gpio whose value will be returned
1374 *
1375 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1376 * account.
1377 *
1378 * This function is to be called from contexts that can sleep.
1379 */
1380int gpiod_get_value_cansleep(const struct gpio_desc *desc)
d2876d08 1381{
3f397c21 1382 int value;
d2876d08
DB
1383
1384 might_sleep_if(extra_checks);
bcabdef1
AC
1385 if (!desc)
1386 return 0;
79a9becd
AC
1387
1388 value = _gpiod_get_raw_value(desc);
1389 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1390 value = !value;
1391
3f397c21 1392 return value;
d2876d08 1393}
79a9becd 1394EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
372e722e 1395
79a9becd
AC
1396/**
1397 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1398 * @desc: gpio whose value will be assigned
1399 * @value: value to assign
1400 *
1401 * Set the raw value of the GPIO, i.e. the value of its physical line without
1402 * regard for its ACTIVE_LOW status.
1403 *
1404 * This function is to be called from contexts that can sleep.
1405 */
1406void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
372e722e 1407{
d2876d08 1408 might_sleep_if(extra_checks);
bcabdef1
AC
1409 if (!desc)
1410 return;
79a9becd 1411 _gpiod_set_raw_value(desc, value);
372e722e 1412}
79a9becd 1413EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
d2876d08 1414
79a9becd
AC
1415/**
1416 * gpiod_set_value_cansleep() - assign a gpio's value
1417 * @desc: gpio whose value will be assigned
1418 * @value: value to assign
1419 *
1420 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1421 * account
1422 *
1423 * This function is to be called from contexts that can sleep.
1424 */
1425void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
d2876d08 1426{
d2876d08 1427 might_sleep_if(extra_checks);
bcabdef1
AC
1428 if (!desc)
1429 return;
79a9becd
AC
1430
1431 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1432 value = !value;
1433 _gpiod_set_raw_value(desc, value);
372e722e 1434}
79a9becd 1435EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
d2876d08 1436
bae48da2 1437/**
ad824783
AC
1438 * gpiod_add_lookup_table() - register GPIO device consumers
1439 * @table: table of consumers to register
bae48da2 1440 */
ad824783 1441void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
bae48da2
AC
1442{
1443 mutex_lock(&gpio_lookup_lock);
1444
ad824783 1445 list_add_tail(&table->list, &gpio_lookup_list);
bae48da2
AC
1446
1447 mutex_unlock(&gpio_lookup_lock);
1448}
1449
bae48da2 1450static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
53e7cac3
AC
1451 unsigned int idx,
1452 enum gpio_lookup_flags *flags)
bae48da2 1453{
dd34c37a 1454 static const char *suffixes[] = { "gpios", "gpio" };
bae48da2
AC
1455 char prop_name[32]; /* 32 is max size of property name */
1456 enum of_gpio_flags of_flags;
1457 struct gpio_desc *desc;
dd34c37a 1458 unsigned int i;
bae48da2 1459
dd34c37a
TR
1460 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1461 if (con_id)
1462 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1463 else
1464 snprintf(prop_name, 32, "%s", suffixes[i]);
bae48da2 1465
dd34c37a
TR
1466 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1467 &of_flags);
06fc3b70 1468 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
dd34c37a
TR
1469 break;
1470 }
bae48da2
AC
1471
1472 if (IS_ERR(desc))
1473 return desc;
1474
1475 if (of_flags & OF_GPIO_ACTIVE_LOW)
53e7cac3 1476 *flags |= GPIO_ACTIVE_LOW;
bae48da2
AC
1477
1478 return desc;
1479}
d2876d08 1480
81f59e9d 1481static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
53e7cac3
AC
1482 unsigned int idx,
1483 enum gpio_lookup_flags *flags)
372e722e 1484{
e01f440a
MW
1485 struct acpi_gpio_info info;
1486 struct gpio_desc *desc;
1487
1488 desc = acpi_get_gpiod_by_index(dev, idx, &info);
1489 if (IS_ERR(desc))
1490 return desc;
1491
1492 if (info.gpioint && info.active_low)
53e7cac3 1493 *flags |= GPIO_ACTIVE_LOW;
e01f440a
MW
1494
1495 return desc;
81f59e9d
MW
1496}
1497
ad824783 1498static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
bae48da2
AC
1499{
1500 const char *dev_id = dev ? dev_name(dev) : NULL;
ad824783 1501 struct gpiod_lookup_table *table;
bae48da2
AC
1502
1503 mutex_lock(&gpio_lookup_lock);
1504
ad824783
AC
1505 list_for_each_entry(table, &gpio_lookup_list, list) {
1506 if (table->dev_id && dev_id) {
1507 /*
1508 * Valid strings on both ends, must be identical to have
1509 * a match
1510 */
1511 if (!strcmp(table->dev_id, dev_id))
1512 goto found;
1513 } else {
1514 /*
1515 * One of the pointers is NULL, so both must be to have
1516 * a match
1517 */
1518 if (dev_id == table->dev_id)
1519 goto found;
1520 }
1521 }
1522 table = NULL;
bae48da2 1523
ad824783
AC
1524found:
1525 mutex_unlock(&gpio_lookup_lock);
1526 return table;
1527}
bae48da2 1528
ad824783
AC
1529static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1530 unsigned int idx,
1531 enum gpio_lookup_flags *flags)
1532{
2a3cf6a3 1533 struct gpio_desc *desc = ERR_PTR(-ENOENT);
ad824783
AC
1534 struct gpiod_lookup_table *table;
1535 struct gpiod_lookup *p;
bae48da2 1536
ad824783
AC
1537 table = gpiod_find_lookup_table(dev);
1538 if (!table)
1539 return desc;
bae48da2 1540
ad824783
AC
1541 for (p = &table->table[0]; p->chip_label; p++) {
1542 struct gpio_chip *chip;
bae48da2 1543
ad824783 1544 /* idx must always match exactly */
bae48da2
AC
1545 if (p->idx != idx)
1546 continue;
1547
ad824783
AC
1548 /* If the lookup entry has a con_id, require exact match */
1549 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1550 continue;
bae48da2 1551
ad824783 1552 chip = find_chip_by_name(p->chip_label);
bae48da2 1553
ad824783 1554 if (!chip) {
2a3cf6a3
AC
1555 dev_err(dev, "cannot find GPIO chip %s\n",
1556 p->chip_label);
1557 return ERR_PTR(-ENODEV);
ad824783 1558 }
bae48da2 1559
ad824783 1560 if (chip->ngpio <= p->chip_hwnum) {
2a3cf6a3
AC
1561 dev_err(dev,
1562 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1563 idx, chip->ngpio, chip->label);
1564 return ERR_PTR(-EINVAL);
bae48da2 1565 }
bae48da2 1566
bb1e88cc 1567 desc = gpiochip_get_desc(chip, p->chip_hwnum);
ad824783 1568 *flags = p->flags;
bae48da2 1569
2a3cf6a3 1570 return desc;
bae48da2
AC
1571 }
1572
bae48da2
AC
1573 return desc;
1574}
1575
1576/**
0879162f 1577 * gpiod_get - obtain a GPIO for a given GPIO function
ad824783 1578 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2
AC
1579 * @con_id: function within the GPIO consumer
1580 *
1581 * Return the GPIO descriptor corresponding to the function con_id of device
2a3cf6a3
AC
1582 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1583 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
bae48da2
AC
1584 */
1585struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
1586{
1587 return gpiod_get_index(dev, con_id, 0);
1588}
1589EXPORT_SYMBOL_GPL(gpiod_get);
1590
29a1f233
TR
1591/**
1592 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1593 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1594 * @con_id: function within the GPIO consumer
1595 *
1596 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1597 * the requested function it will return NULL. This is convenient for drivers
1598 * that need to handle optional GPIOs.
1599 */
1600struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
1601 const char *con_id)
1602{
1603 return gpiod_get_index_optional(dev, con_id, 0);
1604}
1605EXPORT_SYMBOL_GPL(gpiod_get_optional);
1606
bae48da2
AC
1607/**
1608 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
fdd6a5fe 1609 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2
AC
1610 * @con_id: function within the GPIO consumer
1611 * @idx: index of the GPIO to obtain in the consumer
1612 *
1613 * This variant of gpiod_get() allows to access GPIOs other than the first
1614 * defined one for functions that define several GPIOs.
1615 *
2a3cf6a3
AC
1616 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1617 * requested function and/or index, or another IS_ERR() code if an error
1618 * occured while trying to acquire the GPIO.
bae48da2
AC
1619 */
1620struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
1621 const char *con_id,
1622 unsigned int idx)
1623{
35c5d7fd 1624 struct gpio_desc *desc = NULL;
bae48da2 1625 int status;
53e7cac3 1626 enum gpio_lookup_flags flags = 0;
bae48da2
AC
1627
1628 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1629
1630 /* Using device tree? */
1631 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1632 dev_dbg(dev, "using device tree for GPIO lookup\n");
1633 desc = of_find_gpio(dev, con_id, idx, &flags);
81f59e9d
MW
1634 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1635 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1636 desc = acpi_find_gpio(dev, con_id, idx, &flags);
35c5d7fd
AC
1637 }
1638
1639 /*
1640 * Either we are not using DT or ACPI, or their lookup did not return
1641 * a result. In that case, use platform lookup as a fallback.
1642 */
2a3cf6a3 1643 if (!desc || desc == ERR_PTR(-ENOENT)) {
bae48da2 1644 dev_dbg(dev, "using lookup tables for GPIO lookup");
2a3cf6a3 1645 desc = gpiod_find(dev, con_id, idx, &flags);
bae48da2
AC
1646 }
1647
1648 if (IS_ERR(desc)) {
351cfe0f 1649 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
bae48da2
AC
1650 return desc;
1651 }
1652
1653 status = gpiod_request(desc, con_id);
1654
1655 if (status < 0)
1656 return ERR_PTR(status);
1657
53e7cac3 1658 if (flags & GPIO_ACTIVE_LOW)
bae48da2 1659 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
53e7cac3
AC
1660 if (flags & GPIO_OPEN_DRAIN)
1661 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1662 if (flags & GPIO_OPEN_SOURCE)
1663 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
bae48da2
AC
1664
1665 return desc;
1666}
1667EXPORT_SYMBOL_GPL(gpiod_get_index);
1668
29a1f233
TR
1669/**
1670 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1671 * function
1672 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1673 * @con_id: function within the GPIO consumer
1674 * @index: index of the GPIO to obtain in the consumer
1675 *
1676 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1677 * specified index was assigned to the requested function it will return NULL.
1678 * This is convenient for drivers that need to handle optional GPIOs.
1679 */
1680struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
1681 const char *con_id,
1682 unsigned int index)
1683{
1684 struct gpio_desc *desc;
1685
1686 desc = gpiod_get_index(dev, con_id, index);
1687 if (IS_ERR(desc)) {
1688 if (PTR_ERR(desc) == -ENOENT)
1689 return NULL;
1690 }
1691
1692 return desc;
1693}
1694EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
1695
bae48da2
AC
1696/**
1697 * gpiod_put - dispose of a GPIO descriptor
1698 * @desc: GPIO descriptor to dispose of
1699 *
1700 * No descriptor can be used after gpiod_put() has been called on it.
1701 */
1702void gpiod_put(struct gpio_desc *desc)
1703{
1704 gpiod_free(desc);
372e722e 1705}
bae48da2 1706EXPORT_SYMBOL_GPL(gpiod_put);
d2876d08
DB
1707
1708#ifdef CONFIG_DEBUG_FS
1709
d2876d08
DB
1710static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1711{
1712 unsigned i;
1713 unsigned gpio = chip->base;
6c0b4e6c 1714 struct gpio_desc *gdesc = &chip->desc[0];
d2876d08 1715 int is_out;
d468bf9e 1716 int is_irq;
d2876d08
DB
1717
1718 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1719 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1720 continue;
1721
372e722e 1722 gpiod_get_direction(gdesc);
d2876d08 1723 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
d468bf9e
LW
1724 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
1725 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
d2876d08
DB
1726 gpio, gdesc->label,
1727 is_out ? "out" : "in ",
1728 chip->get
1729 ? (chip->get(chip, i) ? "hi" : "lo")
d468bf9e
LW
1730 : "? ",
1731 is_irq ? "IRQ" : " ");
d2876d08
DB
1732 seq_printf(s, "\n");
1733 }
1734}
1735
f9c4a31f 1736static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
d2876d08 1737{
362432ae 1738 unsigned long flags;
f9c4a31f 1739 struct gpio_chip *chip = NULL;
cb1650d4 1740 loff_t index = *pos;
d2876d08 1741
f9c4a31f 1742 s->private = "";
d2876d08 1743
362432ae 1744 spin_lock_irqsave(&gpio_lock, flags);
cb1650d4 1745 list_for_each_entry(chip, &gpio_chips, list)
362432ae
GL
1746 if (index-- == 0) {
1747 spin_unlock_irqrestore(&gpio_lock, flags);
cb1650d4 1748 return chip;
f9c4a31f 1749 }
362432ae 1750 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f 1751
cb1650d4 1752 return NULL;
f9c4a31f
TR
1753}
1754
1755static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1756{
362432ae 1757 unsigned long flags;
f9c4a31f 1758 struct gpio_chip *chip = v;
f9c4a31f
TR
1759 void *ret = NULL;
1760
362432ae 1761 spin_lock_irqsave(&gpio_lock, flags);
cb1650d4
AC
1762 if (list_is_last(&chip->list, &gpio_chips))
1763 ret = NULL;
1764 else
1765 ret = list_entry(chip->list.next, struct gpio_chip, list);
362432ae 1766 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f
TR
1767
1768 s->private = "\n";
1769 ++*pos;
1770
1771 return ret;
1772}
1773
1774static void gpiolib_seq_stop(struct seq_file *s, void *v)
1775{
1776}
1777
1778static int gpiolib_seq_show(struct seq_file *s, void *v)
1779{
1780 struct gpio_chip *chip = v;
1781 struct device *dev;
1782
1783 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1784 chip->base, chip->base + chip->ngpio - 1);
1785 dev = chip->dev;
1786 if (dev)
1787 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1788 dev_name(dev));
1789 if (chip->label)
1790 seq_printf(s, ", %s", chip->label);
1791 if (chip->can_sleep)
1792 seq_printf(s, ", can sleep");
1793 seq_printf(s, ":\n");
1794
1795 if (chip->dbg_show)
1796 chip->dbg_show(s, chip);
1797 else
1798 gpiolib_dbg_show(s, chip);
1799
d2876d08
DB
1800 return 0;
1801}
1802
f9c4a31f
TR
1803static const struct seq_operations gpiolib_seq_ops = {
1804 .start = gpiolib_seq_start,
1805 .next = gpiolib_seq_next,
1806 .stop = gpiolib_seq_stop,
1807 .show = gpiolib_seq_show,
1808};
1809
d2876d08
DB
1810static int gpiolib_open(struct inode *inode, struct file *file)
1811{
f9c4a31f 1812 return seq_open(file, &gpiolib_seq_ops);
d2876d08
DB
1813}
1814
828c0950 1815static const struct file_operations gpiolib_operations = {
f9c4a31f 1816 .owner = THIS_MODULE,
d2876d08
DB
1817 .open = gpiolib_open,
1818 .read = seq_read,
1819 .llseek = seq_lseek,
f9c4a31f 1820 .release = seq_release,
d2876d08
DB
1821};
1822
1823static int __init gpiolib_debugfs_init(void)
1824{
1825 /* /sys/kernel/debug/gpio */
1826 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1827 NULL, NULL, &gpiolib_operations);
1828 return 0;
1829}
1830subsys_initcall(gpiolib_debugfs_init);
1831
1832#endif /* DEBUG_FS */