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