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