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