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