Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/bitops.h>
3#include <linux/device.h>
4#include <linux/errno.h>
5#include <linux/export.h>
6#include <linux/gfp.h>
7
8#include <linux/gpio/consumer.h>
9#include <linux/gpio/driver.h>
10
11#include <linux/gpio.h>
12
13#include "gpiolib.h"
14
15/*
16 * **DEPRECATED** This function is deprecated and must not be used in new code.
17 */
18void gpio_free(unsigned gpio)
19{
20 gpiod_free(gpio_to_desc(gpio));
21}
22EXPORT_SYMBOL_GPL(gpio_free);
23
24/**
25 * gpio_request_one - request a single GPIO with initial configuration
26 * @gpio: the GPIO number
27 * @flags: GPIO configuration as specified by GPIOF_*
28 * @label: a literal description string of this GPIO
29 *
30 * **DEPRECATED** This function is deprecated and must not be used in new code.
31 *
32 * Returns:
33 * 0 on success, or negative errno on failure.
34 */
35int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
36{
37 struct gpio_desc *desc;
38 int err;
39
40 /* Compatibility: assume unavailable "valid" GPIOs will appear later */
41 desc = gpio_to_desc(gpio);
42 if (!desc)
43 return -EPROBE_DEFER;
44
45 err = gpiod_request(desc, label);
46 if (err)
47 return err;
48
49 if (flags & GPIOF_IN)
50 err = gpiod_direction_input(desc);
51 else
52 err = gpiod_direction_output_raw(desc, !!(flags & GPIOF_OUT_INIT_HIGH));
53
54 if (err)
55 goto free_gpio;
56
57 return 0;
58
59 free_gpio:
60 gpiod_free(desc);
61 return err;
62}
63EXPORT_SYMBOL_GPL(gpio_request_one);
64
65/*
66 * **DEPRECATED** This function is deprecated and must not be used in new code.
67 */
68int gpio_request(unsigned gpio, const char *label)
69{
70 struct gpio_desc *desc;
71
72 /* Compatibility: assume unavailable "valid" GPIOs will appear later */
73 desc = gpio_to_desc(gpio);
74 if (!desc)
75 return -EPROBE_DEFER;
76
77 return gpiod_request(desc, label);
78}
79EXPORT_SYMBOL_GPL(gpio_request);
80
81static void devm_gpio_release(struct device *dev, void *res)
82{
83 unsigned *gpio = res;
84
85 gpio_free(*gpio);
86}
87
88/**
89 * devm_gpio_request - request a GPIO for a managed device
90 * @dev: device to request the GPIO for
91 * @gpio: GPIO to allocate
92 * @label: the name of the requested GPIO
93 *
94 * Except for the extra @dev argument, this function takes the
95 * same arguments and performs the same function as gpio_request().
96 * GPIOs requested with this function will be automatically freed
97 * on driver detach.
98 *
99 * **DEPRECATED** This function is deprecated and must not be used in new code.
100 *
101 * Returns:
102 * 0 on success, or negative errno on failure.
103 */
104int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
105{
106 unsigned *dr;
107 int rc;
108
109 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
110 if (!dr)
111 return -ENOMEM;
112
113 rc = gpio_request(gpio, label);
114 if (rc) {
115 devres_free(dr);
116 return rc;
117 }
118
119 *dr = gpio;
120 devres_add(dev, dr);
121
122 return 0;
123}
124EXPORT_SYMBOL_GPL(devm_gpio_request);
125
126/**
127 * devm_gpio_request_one - request a single GPIO with initial setup
128 * @dev: device to request for
129 * @gpio: the GPIO number
130 * @flags: GPIO configuration as specified by GPIOF_*
131 * @label: a literal description string of this GPIO
132 *
133 * **DEPRECATED** This function is deprecated and must not be used in new code.
134 *
135 * Returns:
136 * 0 on success, or negative errno on failure.
137 */
138int devm_gpio_request_one(struct device *dev, unsigned gpio,
139 unsigned long flags, const char *label)
140{
141 unsigned *dr;
142 int rc;
143
144 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
145 if (!dr)
146 return -ENOMEM;
147
148 rc = gpio_request_one(gpio, flags, label);
149 if (rc) {
150 devres_free(dr);
151 return rc;
152 }
153
154 *dr = gpio;
155 devres_add(dev, dr);
156
157 return 0;
158}
159EXPORT_SYMBOL_GPL(devm_gpio_request_one);
1#include <linux/gpio/consumer.h>
2#include <linux/gpio/driver.h>
3
4#include <linux/gpio.h>
5
6#include "gpiolib.h"
7
8void gpio_free(unsigned gpio)
9{
10 gpiod_free(gpio_to_desc(gpio));
11}
12EXPORT_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 */
20int 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 /* Compatibility: assume unavailable "valid" GPIOs will appear later */
28 if (!desc && gpio_is_valid(gpio))
29 return -EPROBE_DEFER;
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 err = gpiod_request(desc, label);
41 if (err)
42 return err;
43
44 if (flags & GPIOF_DIR_IN)
45 err = gpiod_direction_input(desc);
46 else
47 err = gpiod_direction_output_raw(desc,
48 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
49
50 if (err)
51 goto free_gpio;
52
53 if (flags & GPIOF_EXPORT) {
54 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
55 if (err)
56 goto free_gpio;
57 }
58
59 return 0;
60
61 free_gpio:
62 gpiod_free(desc);
63 return err;
64}
65EXPORT_SYMBOL_GPL(gpio_request_one);
66
67int gpio_request(unsigned gpio, const char *label)
68{
69 struct gpio_desc *desc = gpio_to_desc(gpio);
70
71 /* Compatibility: assume unavailable "valid" GPIOs will appear later */
72 if (!desc && gpio_is_valid(gpio))
73 return -EPROBE_DEFER;
74
75 return gpiod_request(desc, label);
76}
77EXPORT_SYMBOL_GPL(gpio_request);
78
79/**
80 * gpio_request_array - request multiple GPIOs in a single call
81 * @array: array of the 'struct gpio'
82 * @num: how many GPIOs in the array
83 */
84int gpio_request_array(const struct gpio *array, size_t num)
85{
86 int i, err;
87
88 for (i = 0; i < num; i++, array++) {
89 err = gpio_request_one(array->gpio, array->flags, array->label);
90 if (err)
91 goto err_free;
92 }
93 return 0;
94
95err_free:
96 while (i--)
97 gpio_free((--array)->gpio);
98 return err;
99}
100EXPORT_SYMBOL_GPL(gpio_request_array);
101
102/**
103 * gpio_free_array - release multiple GPIOs in a single call
104 * @array: array of the 'struct gpio'
105 * @num: how many GPIOs in the array
106 */
107void gpio_free_array(const struct gpio *array, size_t num)
108{
109 while (num--)
110 gpio_free((array++)->gpio);
111}
112EXPORT_SYMBOL_GPL(gpio_free_array);