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