Linux-libre 3.18.62-gnu
[librecmc/linux-libre.git] / drivers / gpio / gpiolib-legacy.c
1 #include <linux/gpio/consumer.h>
2 #include <linux/gpio/driver.h>
3
4 #include <linux/gpio.h>
5
6 #include "gpiolib.h"
7
8 void gpio_free(unsigned gpio)
9 {
10         gpiod_free(gpio_to_desc(gpio));
11 }
12 EXPORT_SYMBOL_GPL(gpio_free);
13
14 /**
15  * gpio_request_one - request a single GPIO with initial configuration
16  * @gpio:       the GPIO number
17  * @flags:      GPIO configuration as specified by GPIOF_*
18  * @label:      a literal description string of this GPIO
19  */
20 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
21 {
22         struct gpio_desc *desc;
23         int err;
24
25         desc = gpio_to_desc(gpio);
26
27         err = gpiod_request(desc, label);
28         if (err)
29                 return err;
30
31         if (flags & GPIOF_OPEN_DRAIN)
32                 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
33
34         if (flags & GPIOF_OPEN_SOURCE)
35                 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
36
37         if (flags & GPIOF_ACTIVE_LOW)
38                 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
39
40         if (flags & GPIOF_DIR_IN)
41                 err = gpiod_direction_input(desc);
42         else
43                 err = gpiod_direction_output_raw(desc,
44                                 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
45
46         if (err)
47                 goto free_gpio;
48
49         if (flags & GPIOF_EXPORT) {
50                 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
51                 if (err)
52                         goto free_gpio;
53         }
54
55         return 0;
56
57  free_gpio:
58         gpiod_free(desc);
59         return err;
60 }
61 EXPORT_SYMBOL_GPL(gpio_request_one);
62
63 int gpio_request(unsigned gpio, const char *label)
64 {
65         return gpiod_request(gpio_to_desc(gpio), label);
66 }
67 EXPORT_SYMBOL_GPL(gpio_request);
68
69 /**
70  * gpio_request_array - request multiple GPIOs in a single call
71  * @array:      array of the 'struct gpio'
72  * @num:        how many GPIOs in the array
73  */
74 int gpio_request_array(const struct gpio *array, size_t num)
75 {
76         int i, err;
77
78         for (i = 0; i < num; i++, array++) {
79                 err = gpio_request_one(array->gpio, array->flags, array->label);
80                 if (err)
81                         goto err_free;
82         }
83         return 0;
84
85 err_free:
86         while (i--)
87                 gpio_free((--array)->gpio);
88         return err;
89 }
90 EXPORT_SYMBOL_GPL(gpio_request_array);
91
92 /**
93  * gpio_free_array - release multiple GPIOs in a single call
94  * @array:      array of the 'struct gpio'
95  * @num:        how many GPIOs in the array
96  */
97 void gpio_free_array(const struct gpio *array, size_t num)
98 {
99         while (num--)
100                 gpio_free((array++)->gpio);
101 }
102 EXPORT_SYMBOL_GPL(gpio_free_array);