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