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