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