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