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