tizen 2.4 release
[profile/mobile/platform/kernel/u-boot-tm1.git] / drivers / gpio / gpiolib.c
1 #include <common.h>
2 //#include <linux/err.h>
3 #include <asm/arch/gpio.h>
4 #include <linux/gpio.h>
5 #include <ubi_uboot.h>
6 #include <asm/bitops.h>
7
8 /* Optional implementation infrastructure for GPIO interfaces.
9  *
10  * Platforms may want to use this if they tend to use very many GPIOs
11  * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
12  *
13  * When kernel footprint or instruction count is an issue, simpler
14  * implementations may be preferred.  The GPIO programming interface
15  * allows for inlining speed-critical get/set operations for common
16  * cases, so that access to SOC-integrated GPIOs can sometimes cost
17  * only an instruction or two per bit.
18  */
19
20
21 /* When debugging, extend minimal trust to callers and platform code.
22  * Also emit diagnostic messages that may help initial bringup, when
23  * board setup or driver bugs are most common.
24  *
25  * Otherwise, minimize overhead in what may be bitbanging codepaths.
26  */
27 #ifdef  DEBUG
28 #define extra_checks    1
29 #else
30 #define extra_checks    0
31 #endif
32
33 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
34  * While any GPIO is requested, its gpio_chip is not removable;
35  * each GPIO's "requested" flag serves as a lock and refcount.
36  */
37 static DEFINE_SPINLOCK(gpio_lock);
38
39 struct gpio_desc {
40         struct gpio_chip        *chip;
41         unsigned long           flags;
42 /* flag symbols are bit numbers */
43 #define FLAG_REQUESTED  0
44 #define FLAG_IS_OUT     1
45 #define FLAG_RESERVED   2
46 #define FLAG_EXPORT     3       /* protected by sysfs_lock */
47 #define FLAG_SYSFS      4       /* exported via /sys/class/gpio/control */
48 #define FLAG_TRIG_FALL  5       /* trigger on falling edge */
49 #define FLAG_TRIG_RISE  6       /* trigger on rising edge */
50 #define FLAG_ACTIVE_LOW 7       /* sysfs value has active low */
51 #define FLAG_OPEN_DRAIN 8       /* Gpio is open drain type */
52 #define FLAG_OPEN_SOURCE 9      /* Gpio is open source type */
53
54 #define ID_SHIFT        16      /* add new flags before this one */
55
56 #define GPIO_FLAGS_MASK         ((1 << ID_SHIFT) - 1)
57 #define GPIO_TRIGGER_MASK       (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
58
59 #ifdef CONFIG_DEBUG_FS
60         const char              *label;
61 #endif
62 };
63 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
64
65 #ifdef CONFIG_GPIO_SYSFS
66 static DEFINE_IDR(dirent_idr);
67 #endif
68
69 static inline void desc_set_label(struct gpio_desc *d, const char *label)
70 {
71 #ifdef CONFIG_DEBUG_FS
72         d->label = label;
73 #endif
74 }
75
76 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
77  * when setting direction, and otherwise illegal.  Until board setup code
78  * and drivers use explicit requests everywhere (which won't happen when
79  * those calls have no teeth) we can't avoid autorequesting.  This nag
80  * message should motivate switching to explicit requests... so should
81  * the weaker cleanup after faults, compared to gpio_request().
82  *
83  * NOTE: the autorequest mechanism is going away; at this point it's
84  * only "legal" in the sense that (old) code using it won't break yet,
85  * but instead only triggers a WARN() stack dump.
86  */
87 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
88 {
89         const struct gpio_chip *chip = desc->chip;
90         const int gpio = chip->base + offset;
91
92         if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
93                         "autorequest GPIO-%d\n", gpio)) {
94                 if (!try_module_get(chip->owner)) {
95                         pr_err("GPIO-%d: module can't be gotten \n", gpio);
96                         clear_bit(FLAG_REQUESTED, &desc->flags);
97                         /* lose */
98                         return -EIO;
99                 }
100                 desc_set_label(desc, "[auto]");
101                 /* caller must chip->request() w/o spinlock */
102                 if (chip->request)
103                         return 1;
104         }
105         return 0;
106 }
107
108 /* caller holds gpio_lock *OR* gpio is marked as requested */
109 struct gpio_chip *gpio_to_chip(unsigned gpio)
110 {
111         return gpio_desc[gpio].chip;
112 }
113
114 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
115 static int gpiochip_find_base(int ngpio)
116 {
117         int i;
118         int spare = 0;
119         int base = -ENOSPC;
120
121         for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
122                 struct gpio_desc *desc = &gpio_desc[i];
123                 struct gpio_chip *chip = desc->chip;
124
125                 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
126                         spare++;
127                         if (spare == ngpio) {
128                                 base = i;
129                                 break;
130                         }
131                 } else {
132                         spare = 0;
133                         if (chip)
134                                 i -= chip->ngpio - 1;
135                 }
136         }
137
138         if (gpio_is_valid(base))
139                 pr_debug("%s: found new base at %d\n", __func__, base);
140         return base;
141 }
142
143 /**
144  * gpiochip_reserve() - reserve range of gpios to use with platform code only
145  * @start: starting gpio number
146  * @ngpio: number of gpios to reserve
147  * Context: platform init, potentially before irqs or kmalloc will work
148  *
149  * Returns a negative errno if any gpio within the range is already reserved
150  * or registered, else returns zero as a success code.  Use this function
151  * to mark a range of gpios as unavailable for dynamic gpio number allocation,
152  * for example because its driver support is not yet loaded.
153  */
154 int __init gpiochip_reserve(int start, int ngpio)
155 {
156         int ret = 0;
157         unsigned long flags;
158         int i;
159
160         if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
161                 return -EINVAL;
162
163         spin_lock_irqsave(&gpio_lock, flags);
164
165         for (i = start; i < start + ngpio; i++) {
166                 struct gpio_desc *desc = &gpio_desc[i];
167
168                 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
169                         ret = -EBUSY;
170                         goto err;
171                 }
172
173                 set_bit(FLAG_RESERVED, &desc->flags);
174         }
175
176         pr_debug("%s: reserved gpios from %d to %d\n",
177                  __func__, start, start + ngpio - 1);
178 err:
179         spin_unlock_irqrestore(&gpio_lock, flags);
180
181         return ret;
182 }
183
184 /**
185  * gpiochip_add() - register a gpio_chip
186  * @chip: the chip to register, with chip->base initialized
187  * Context: potentially before irqs or kmalloc will work
188  *
189  * Returns a negative errno if the chip can't be registered, such as
190  * because the chip->base is invalid or already associated with a
191  * different chip.  Otherwise it returns zero as a success code.
192  *
193  * When gpiochip_add() is called very early during boot, so that GPIOs
194  * can be freely used, the chip->dev device must be registered before
195  * the gpio framework's arch_initcall().  Otherwise sysfs initialization
196  * for GPIOs will fail rudely.
197  *
198  * If chip->base is negative, this requests dynamic assignment of
199  * a range of valid GPIOs.
200  */
201 int gpiochip_add(struct gpio_chip *chip)
202 {
203         unsigned long   flags;
204         int             status = 0;
205         unsigned        id;
206         int             base = chip->base;
207
208         if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
209                         && base >= 0) {
210                 status = -EINVAL;
211                 goto fail;
212         }
213
214         spin_lock_irqsave(&gpio_lock, flags);
215
216         if (base < 0) {
217                 base = gpiochip_find_base(chip->ngpio);
218                 if (base < 0) {
219                         status = base;
220                         goto unlock;
221                 }
222                 chip->base = base;
223         }
224
225         /* these GPIO numbers must not be managed by another gpio_chip */
226         for (id = base; id < base + chip->ngpio; id++) {
227                 if (gpio_desc[id].chip != NULL) {
228                         status = -EBUSY;
229                         break;
230                 }
231         }
232         if (status == 0) {
233                 for (id = base; id < base + chip->ngpio; id++) {
234                         gpio_desc[id].chip = chip;
235
236                         /* REVISIT:  most hardware initializes GPIOs as
237                          * inputs (often with pullups enabled) so power
238                          * usage is minimized.  Linux code should set the
239                          * gpio direction first thing; but until it does,
240                          * we may expose the wrong direction in sysfs.
241                          */
242                         gpio_desc[id].flags = !chip->direction_input
243                                 ? (1 << FLAG_IS_OUT)
244                                 : 0;
245                 }
246         }
247
248 unlock:
249         spin_unlock_irqrestore(&gpio_lock, flags);
250
251         if (status)
252                 goto fail;
253
254         pr_info("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
255                 chip->base, chip->base + chip->ngpio - 1,
256                 chip->label ? : "generic");
257
258         return 0;
259 fail:
260         /* failures here can mean systems won't boot... */
261         pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
262                 chip->base, chip->base + chip->ngpio - 1,
263                 chip->label ? : "generic");
264         return status;
265 }
266 EXPORT_SYMBOL_GPL(gpiochip_add);
267
268 /**
269  * gpiochip_remove() - unregister a gpio_chip
270  * @chip: the chip to unregister
271  *
272  * A gpio_chip with any GPIOs still requested may not be removed.
273  */
274 int gpiochip_remove(struct gpio_chip *chip)
275 {
276         unsigned long   flags;
277         int             status = 0;
278         unsigned        id;
279
280         spin_lock_irqsave(&gpio_lock, flags);
281
282         for (id = chip->base; id < chip->base + chip->ngpio; id++) {
283                 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
284                         status = -EBUSY;
285                         break;
286                 }
287         }
288         if (status == 0) {
289                 for (id = chip->base; id < chip->base + chip->ngpio; id++)
290                         gpio_desc[id].chip = NULL;
291         }
292
293         spin_unlock_irqrestore(&gpio_lock, flags);
294
295         return status;
296 }
297 EXPORT_SYMBOL_GPL(gpiochip_remove);
298
299 /**
300  * gpiochip_find() - iterator for locating a specific gpio_chip
301  * @data: data to pass to match function
302  * @callback: Callback function to check gpio_chip
303  *
304  * Similar to bus_find_device.  It returns a reference to a gpio_chip as
305  * determined by a user supplied @match callback.  The callback should return
306  * 0 if the device doesn't match and non-zero if it does.  If the callback is
307  * non-zero, this function will return to the caller and not iterate over any
308  * more gpio_chips.
309  */
310 struct gpio_chip *gpiochip_find(const void *data,
311                                 int (*match)(struct gpio_chip *chip,
312                                              const void *data))
313 {
314         struct gpio_chip *chip = NULL;
315         unsigned long flags;
316         int i;
317
318         spin_lock_irqsave(&gpio_lock, flags);
319         for (i = 0; i < ARCH_NR_GPIOS; i++) {
320                 if (!gpio_desc[i].chip)
321                         continue;
322
323                 if (match(gpio_desc[i].chip, data)) {
324                         chip = gpio_desc[i].chip;
325                         break;
326                 }
327         }
328         spin_unlock_irqrestore(&gpio_lock, flags);
329
330         return chip;
331 }
332 EXPORT_SYMBOL_GPL(gpiochip_find);
333
334 /* These "optional" allocation calls help prevent drivers from stomping
335  * on each other, and help provide better diagnostics in debugfs.
336  * They're called even less than the "set direction" calls.
337  */
338 int gpio_request(unsigned gpio, const char *label)
339 {
340         struct gpio_desc        *desc;
341         struct gpio_chip        *chip;
342         int                     status = -EINVAL;
343         unsigned long           flags;
344
345         spin_lock_irqsave(&gpio_lock, flags);
346
347         if (!gpio_is_valid(gpio))
348                 goto done;
349         desc = &gpio_desc[gpio];
350         chip = desc->chip;
351         if (chip == NULL)
352                 goto done;
353
354         if (!try_module_get(chip->owner))
355                 goto done;
356
357         /* NOTE:  gpio_request() can be called in early boot,
358          * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
359          */
360
361         if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
362                 desc_set_label(desc, label ? : "?");
363                 status = 0;
364         } else {
365                 status = -EBUSY;
366                 module_put(chip->owner);
367                 goto done;
368         }
369
370         if (chip->request) {
371                 /* chip->request may sleep */
372                 spin_unlock_irqrestore(&gpio_lock, flags);
373                 status = chip->request(chip, gpio - chip->base);
374                 spin_lock_irqsave(&gpio_lock, flags);
375
376                 if (status < 0) {
377                         desc_set_label(desc, NULL);
378                         module_put(chip->owner);
379                         clear_bit(FLAG_REQUESTED, &desc->flags);
380                 }
381         }
382
383 done:
384         if (status)
385                 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
386                         gpio, label ? : "?", status);
387         spin_unlock_irqrestore(&gpio_lock, flags);
388         return status;
389 }
390 EXPORT_SYMBOL_GPL(gpio_request);
391
392 void gpio_free(unsigned gpio)
393 {
394         unsigned long           flags;
395         struct gpio_desc        *desc;
396         struct gpio_chip        *chip;
397
398         if (!gpio_is_valid(gpio)) {
399                 WARN_ON(extra_checks);
400                 return;
401         }
402
403
404         spin_lock_irqsave(&gpio_lock, flags);
405
406         desc = &gpio_desc[gpio];
407         chip = desc->chip;
408         if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
409                 if (chip->free) {
410                         spin_unlock_irqrestore(&gpio_lock, flags);
411                         might_sleep_if(chip->can_sleep);
412                         chip->free(chip, gpio - chip->base);
413                         spin_lock_irqsave(&gpio_lock, flags);
414                 }
415                 desc_set_label(desc, NULL);
416                 module_put(desc->chip->owner);
417                 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
418                 clear_bit(FLAG_REQUESTED, &desc->flags);
419                 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
420                 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
421         } else
422                 WARN_ON(extra_checks);
423
424         spin_unlock_irqrestore(&gpio_lock, flags);
425 }
426 EXPORT_SYMBOL_GPL(gpio_free);
427
428 /**
429  * gpio_request_one - request a single GPIO with initial configuration
430  * @gpio:       the GPIO number
431  * @flags:      GPIO configuration as specified by GPIOF_*
432  * @label:      a literal description string of this GPIO
433  */
434 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
435 {
436         int err;
437
438         err = gpio_request(gpio, label);
439         if (err)
440                 return err;
441
442         if (flags & GPIOF_OPEN_DRAIN)
443                 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
444
445         if (flags & GPIOF_OPEN_SOURCE)
446                 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
447
448         if (flags & GPIOF_DIR_IN)
449                 err = gpio_direction_input(gpio);
450         else
451                 err = gpio_direction_output(gpio,
452                                 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
453
454         if (err)
455                 gpio_free(gpio);
456
457         return err;
458 }
459 EXPORT_SYMBOL_GPL(gpio_request_one);
460
461 /**
462  * gpio_request_array - request multiple GPIOs in a single call
463  * @array:      array of the 'struct gpio'
464  * @num:        how many GPIOs in the array
465  */
466 int gpio_request_array(const struct gpio *array, size_t num)
467 {
468         int i, err;
469
470         for (i = 0; i < num; i++, array++) {
471                 err = gpio_request_one(array->gpio, array->flags, array->label);
472                 if (err)
473                         goto err_free;
474         }
475         return 0;
476
477 err_free:
478         while (i--)
479                 gpio_free((--array)->gpio);
480         return err;
481 }
482 EXPORT_SYMBOL_GPL(gpio_request_array);
483
484 /**
485  * gpio_free_array - release multiple GPIOs in a single call
486  * @array:      array of the 'struct gpio'
487  * @num:        how many GPIOs in the array
488  */
489 void gpio_free_array(const struct gpio *array, size_t num)
490 {
491         while (num--)
492                 gpio_free((array++)->gpio);
493 }
494 EXPORT_SYMBOL_GPL(gpio_free_array);
495
496 /**
497  * gpiochip_is_requested - return string iff signal was requested
498  * @chip: controller managing the signal
499  * @offset: of signal within controller's 0..(ngpio - 1) range
500  *
501  * Returns NULL if the GPIO is not currently requested, else a string.
502  * If debugfs support is enabled, the string returned is the label passed
503  * to gpio_request(); otherwise it is a meaningless constant.
504  *
505  * This function is for use by GPIO controller drivers.  The label can
506  * help with diagnostics, and knowing that the signal is used as a GPIO
507  * can help avoid accidentally multiplexing it to another controller.
508  */
509 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
510 {
511         unsigned gpio = chip->base + offset;
512
513         if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
514                 return NULL;
515         if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
516                 return NULL;
517 #ifdef CONFIG_DEBUG_FS
518         return gpio_desc[gpio].label;
519 #else
520         return "?";
521 #endif
522 }
523 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
524
525
526 /* Drivers MUST set GPIO direction before making get/set calls.  In
527  * some cases this is done in early boot, before IRQs are enabled.
528  *
529  * As a rule these aren't called more than once (except for drivers
530  * using the open-drain emulation idiom) so these are natural places
531  * to accumulate extra debugging checks.  Note that we can't (yet)
532  * rely on gpio_request() having been called beforehand.
533  */
534
535 int gpio_direction_input(unsigned gpio)
536 {
537         unsigned long           flags;
538         struct gpio_chip        *chip;
539         struct gpio_desc        *desc = &gpio_desc[gpio];
540         int                     status = -EINVAL;
541
542         spin_lock_irqsave(&gpio_lock, flags);
543
544         if (!gpio_is_valid(gpio))
545                 goto fail;
546         chip = desc->chip;
547         if (!chip || !chip->get || !chip->direction_input)
548                 goto fail;
549         gpio -= chip->base;
550         if (gpio >= chip->ngpio)
551                 goto fail;
552         status = gpio_ensure_requested(desc, gpio);
553         if (status < 0)
554                 goto fail;
555
556         /* now we know the gpio is valid and chip won't vanish */
557
558         spin_unlock_irqrestore(&gpio_lock, flags);
559
560         might_sleep_if(chip->can_sleep);
561
562         if (status) {
563                 status = chip->request(chip, gpio);
564                 if (status < 0) {
565                         pr_debug("GPIO-%d: chip request fail, %d\n",
566                                 chip->base + gpio, status);
567                         /* and it's not available to anyone else ...
568                          * gpio_request() is the fully clean solution.
569                          */
570                         goto lose;
571                 }
572         }
573
574         status = chip->direction_input(chip, gpio);
575         if (status == 0)
576                 clear_bit(FLAG_IS_OUT, &desc->flags);
577
578         trace_gpio_direction(chip->base + gpio, 1, status);
579 lose:
580         return status;
581 fail:
582         spin_unlock_irqrestore(&gpio_lock, flags);
583         if (status)
584                 pr_debug("%s: gpio-%d status %d\n",
585                         __func__, gpio, status);
586         return status;
587 }
588 EXPORT_SYMBOL_GPL(gpio_direction_input);
589
590 int gpio_direction_output(unsigned gpio, int value)
591 {
592         unsigned long           flags;
593         struct gpio_chip        *chip;
594         struct gpio_desc        *desc = &gpio_desc[gpio];
595         int                     status = -EINVAL;
596
597         /* Open drain pin should not be driven to 1 */
598         if (value && test_bit(FLAG_OPEN_DRAIN,  &desc->flags))
599                 return gpio_direction_input(gpio);
600
601         /* Open source pin should not be driven to 0 */
602         if (!value && test_bit(FLAG_OPEN_SOURCE,  &desc->flags))
603                 return gpio_direction_input(gpio);
604
605         spin_lock_irqsave(&gpio_lock, flags);
606
607         if (!gpio_is_valid(gpio))
608                 goto fail;
609         chip = desc->chip;
610         if (!chip || !chip->set || !chip->direction_output)
611                 goto fail;
612         gpio -= chip->base;
613         if (gpio >= chip->ngpio)
614                 goto fail;
615         status = gpio_ensure_requested(desc, gpio);
616         if (status < 0)
617                 goto fail;
618
619         /* now we know the gpio is valid and chip won't vanish */
620
621         spin_unlock_irqrestore(&gpio_lock, flags);
622
623         might_sleep_if(chip->can_sleep);
624
625         if (status) {
626                 status = chip->request(chip, gpio);
627                 if (status < 0) {
628                         pr_debug("GPIO-%d: chip request fail, %d\n",
629                                 chip->base + gpio, status);
630                         /* and it's not available to anyone else ...
631                          * gpio_request() is the fully clean solution.
632                          */
633                         goto lose;
634                 }
635         }
636
637         status = chip->direction_output(chip, gpio, value);
638         if (status == 0)
639                 set_bit(FLAG_IS_OUT, &desc->flags);
640 lose:
641         return status;
642 fail:
643         spin_unlock_irqrestore(&gpio_lock, flags);
644         if (status)
645                 pr_debug("%s: gpio-%d status %d\n",
646                         __func__, gpio, status);
647         return status;
648 }
649 EXPORT_SYMBOL_GPL(gpio_direction_output);
650
651 /**
652  * gpio_set_debounce - sets @debounce time for a @gpio
653  * @gpio: the gpio to set debounce time
654  * @debounce: debounce time is microseconds
655  */
656 int gpio_set_debounce(unsigned gpio, unsigned debounce)
657 {
658         unsigned long           flags;
659         struct gpio_chip        *chip;
660         struct gpio_desc        *desc = &gpio_desc[gpio];
661         int                     status = -EINVAL;
662
663         spin_lock_irqsave(&gpio_lock, flags);
664
665         if (!gpio_is_valid(gpio))
666                 goto fail;
667         chip = desc->chip;
668         if (!chip || !chip->set || !chip->set_debounce)
669                 goto fail;
670         gpio -= chip->base;
671         if (gpio >= chip->ngpio)
672                 goto fail;
673         status = gpio_ensure_requested(desc, gpio);
674         if (status < 0)
675                 goto fail;
676
677         /* now we know the gpio is valid and chip won't vanish */
678
679         spin_unlock_irqrestore(&gpio_lock, flags);
680
681         might_sleep_if(chip->can_sleep);
682
683         return chip->set_debounce(chip, gpio, debounce);
684
685 fail:
686         spin_unlock_irqrestore(&gpio_lock, flags);
687         if (status)
688                 pr_debug("%s: gpio-%d status %d\n",
689                         __func__, gpio, status);
690
691         return status;
692 }
693 EXPORT_SYMBOL_GPL(gpio_set_debounce);
694
695 /* I/O calls are only valid after configuration completed; the relevant
696  * "is this a valid GPIO" error checks should already have been done.
697  *
698  * "Get" operations are often inlinable as reading a pin value register,
699  * and masking the relevant bit in that register.
700  *
701  * When "set" operations are inlinable, they involve writing that mask to
702  * one register to set a low value, or a different register to set it high.
703  * Otherwise locking is needed, so there may be little value to inlining.
704  *
705  *------------------------------------------------------------------------
706  *
707  * IMPORTANT!!!  The hot paths -- get/set value -- assume that callers
708  * have requested the GPIO.  That can include implicit requesting by
709  * a direction setting call.  Marking a gpio as requested locks its chip
710  * in memory, guaranteeing that these table lookups need no more locking
711  * and that gpiochip_remove() will fail.
712  *
713  * REVISIT when debugging, consider adding some instrumentation to ensure
714  * that the GPIO was actually requested.
715  */
716
717 /**
718  * __gpio_get_value() - return a gpio's value
719  * @gpio: gpio whose value will be returned
720  * Context: any
721  *
722  * This is used directly or indirectly to implement gpio_get_value().
723  * It returns the zero or nonzero value provided by the associated
724  * gpio_chip.get() method; or zero if no such method is provided.
725  */
726 int __gpio_get_value(unsigned gpio)
727 {
728         struct gpio_chip        *chip;
729         int value;
730
731         chip = gpio_to_chip(gpio);
732         /* Should be using gpio_get_value_cansleep() */
733         WARN_ON(chip->can_sleep);
734         value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
735         return value;
736 }
737 EXPORT_SYMBOL_GPL(__gpio_get_value);
738
739 /*
740  *  _gpio_set_open_drain_value() - Set the open drain gpio's value.
741  * @gpio: Gpio whose state need to be set.
742  * @chip: Gpio chip.
743  * @value: Non-zero for setting it HIGH otherise it will set to LOW.
744  */
745 static void _gpio_set_open_drain_value(unsigned gpio,
746                         struct gpio_chip *chip, int value)
747 {
748         int err = 0;
749         if (value) {
750                 err = chip->direction_input(chip, gpio - chip->base);
751                 if (!err)
752                         clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
753         } else {
754                 err = chip->direction_output(chip, gpio - chip->base, 0);
755                 if (!err)
756                         set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
757         }
758         if (err < 0)
759                 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
760                                         __func__, gpio, err);
761 }
762
763 /*
764  *  _gpio_set_open_source() - Set the open source gpio's value.
765  * @gpio: Gpio whose state need to be set.
766  * @chip: Gpio chip.
767  * @value: Non-zero for setting it HIGH otherise it will set to LOW.
768  */
769 static void _gpio_set_open_source_value(unsigned gpio,
770                         struct gpio_chip *chip, int value)
771 {
772         int err = 0;
773         if (value) {
774                 err = chip->direction_output(chip, gpio - chip->base, 1);
775                 if (!err)
776                         set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
777         } else {
778                 err = chip->direction_input(chip, gpio - chip->base);
779                 if (!err)
780                         clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
781         }
782         if (err < 0)
783                 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
784                                         __func__, gpio, err);
785 }
786
787
788 /**
789  * __gpio_set_value() - assign a gpio's value
790  * @gpio: gpio whose value will be assigned
791  * @value: value to assign
792  * Context: any
793  *
794  * This is used directly or indirectly to implement gpio_set_value().
795  * It invokes the associated gpio_chip.set() method.
796  */
797 void __gpio_set_value(unsigned gpio, int value)
798 {
799         struct gpio_chip        *chip;
800
801         chip = gpio_to_chip(gpio);
802         /* Should be using gpio_set_value_cansleep() */
803         WARN_ON(chip->can_sleep);
804         if (test_bit(FLAG_OPEN_DRAIN,  &gpio_desc[gpio].flags))
805                 _gpio_set_open_drain_value(gpio, chip, value);
806         else if (test_bit(FLAG_OPEN_SOURCE,  &gpio_desc[gpio].flags))
807                 _gpio_set_open_source_value(gpio, chip, value);
808         else
809                 chip->set(chip, gpio - chip->base, value);
810 }
811 EXPORT_SYMBOL_GPL(__gpio_set_value);
812
813 /**
814  * __gpio_cansleep() - report whether gpio value access will sleep
815  * @gpio: gpio in question
816  * Context: any
817  *
818  * This is used directly or indirectly to implement gpio_cansleep().  It
819  * returns nonzero if access reading or writing the GPIO value can sleep.
820  */
821 int __gpio_cansleep(unsigned gpio)
822 {
823         struct gpio_chip        *chip;
824
825         /* only call this on GPIOs that are valid! */
826         chip = gpio_to_chip(gpio);
827
828         return chip->can_sleep;
829 }
830 EXPORT_SYMBOL_GPL(__gpio_cansleep);
831
832 /**
833  * __gpio_to_irq() - return the IRQ corresponding to a GPIO
834  * @gpio: gpio whose IRQ will be returned (already requested)
835  * Context: any
836  *
837  * This is used directly or indirectly to implement gpio_to_irq().
838  * It returns the number of the IRQ signaled by this (input) GPIO,
839  * or a negative errno.
840  */
841 int __gpio_to_irq(unsigned gpio)
842 {
843         struct gpio_chip        *chip;
844
845         chip = gpio_to_chip(gpio);
846         return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
847 }
848 EXPORT_SYMBOL_GPL(__gpio_to_irq);
849
850
851
852 /* There's no value in making it easy to inline GPIO calls that may sleep.
853  * Common examples include ones connected to I2C or SPI chips.
854  */
855
856 int gpio_get_value_cansleep(unsigned gpio)
857 {
858         struct gpio_chip        *chip;
859         int value;
860
861         might_sleep_if(extra_checks);
862         chip = gpio_to_chip(gpio);
863         value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
864         trace_gpio_value(gpio, 1, value);
865         return value;
866 }
867 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
868
869 void gpio_set_value_cansleep(unsigned gpio, int value)
870 {
871         struct gpio_chip        *chip;
872
873         might_sleep_if(extra_checks);
874         chip = gpio_to_chip(gpio);
875         trace_gpio_value(gpio, 0, value);
876         if (test_bit(FLAG_OPEN_DRAIN,  &gpio_desc[gpio].flags))
877                 _gpio_set_open_drain_value(gpio, chip, value);
878         else if (test_bit(FLAG_OPEN_SOURCE,  &gpio_desc[gpio].flags))
879                 _gpio_set_open_source_value(gpio, chip, value);
880         else
881                 chip->set(chip, gpio - chip->base, value);
882 }
883 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
884