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