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