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