Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/gpio/consumer.h>
3#include <linux/gpio/driver.h>
4
5#include <linux/gpio.h>
6
7#include "gpiolib.h"
8
9void gpio_free(unsigned gpio)
10{
11 gpiod_free(gpio_to_desc(gpio));
12}
13EXPORT_SYMBOL_GPL(gpio_free);
14
15/**
16 * gpio_request_one - request a single GPIO with initial configuration
17 * @gpio: the GPIO number
18 * @flags: GPIO configuration as specified by GPIOF_*
19 * @label: a literal description string of this GPIO
20 */
21int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
22{
23 struct gpio_desc *desc;
24 int err;
25
26 desc = gpio_to_desc(gpio);
27
28 /* Compatibility: assume unavailable "valid" GPIOs will appear later */
29 if (!desc && gpio_is_valid(gpio))
30 return -EPROBE_DEFER;
31
32 err = gpiod_request(desc, label);
33 if (err)
34 return err;
35
36 if (flags & GPIOF_OPEN_DRAIN)
37 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
38
39 if (flags & GPIOF_OPEN_SOURCE)
40 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
41
42 if (flags & GPIOF_ACTIVE_LOW)
43 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
44
45 if (flags & GPIOF_DIR_IN)
46 err = gpiod_direction_input(desc);
47 else
48 err = gpiod_direction_output_raw(desc,
49 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
50
51 if (err)
52 goto free_gpio;
53
54 if (flags & GPIOF_EXPORT) {
55 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
56 if (err)
57 goto free_gpio;
58 }
59
60 return 0;
61
62 free_gpio:
63 gpiod_free(desc);
64 return err;
65}
66EXPORT_SYMBOL_GPL(gpio_request_one);
67
68int gpio_request(unsigned gpio, const char *label)
69{
70 struct gpio_desc *desc = gpio_to_desc(gpio);
71
72 /* Compatibility: assume unavailable "valid" GPIOs will appear later */
73 if (!desc && gpio_is_valid(gpio))
74 return -EPROBE_DEFER;
75
76 return gpiod_request(desc, label);
77}
78EXPORT_SYMBOL_GPL(gpio_request);
79
80/**
81 * gpio_request_array - request multiple GPIOs in a single call
82 * @array: array of the 'struct gpio'
83 * @num: how many GPIOs in the array
84 */
85int gpio_request_array(const struct gpio *array, size_t num)
86{
87 int i, err;
88
89 for (i = 0; i < num; i++, array++) {
90 err = gpio_request_one(array->gpio, array->flags, array->label);
91 if (err)
92 goto err_free;
93 }
94 return 0;
95
96err_free:
97 while (i--)
98 gpio_free((--array)->gpio);
99 return err;
100}
101EXPORT_SYMBOL_GPL(gpio_request_array);
102
103/**
104 * gpio_free_array - release multiple GPIOs in a single call
105 * @array: array of the 'struct gpio'
106 * @num: how many GPIOs in the array
107 */
108void gpio_free_array(const struct gpio *array, size_t num)
109{
110 while (num--)
111 gpio_free((array++)->gpio);
112}
113EXPORT_SYMBOL_GPL(gpio_free_array);
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/gpio/consumer.h>
3#include <linux/gpio/driver.h>
4
5#include <linux/gpio.h>
6
7#include "gpiolib.h"
8
9/*
10 * **DEPRECATED** This function is deprecated and must not be used in new code.
11 */
12void gpio_free(unsigned gpio)
13{
14 gpiod_free(gpio_to_desc(gpio));
15}
16EXPORT_SYMBOL_GPL(gpio_free);
17
18/**
19 * gpio_request_one - request a single GPIO with initial configuration
20 * @gpio: the GPIO number
21 * @flags: GPIO configuration as specified by GPIOF_*
22 * @label: a literal description string of this GPIO
23 *
24 * **DEPRECATED** This function is deprecated and must not be used in new code.
25 */
26int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
27{
28 struct gpio_desc *desc;
29 int err;
30
31 desc = gpio_to_desc(gpio);
32
33 /* Compatibility: assume unavailable "valid" GPIOs will appear later */
34 if (!desc && gpio_is_valid(gpio))
35 return -EPROBE_DEFER;
36
37 err = gpiod_request(desc, label);
38 if (err)
39 return err;
40
41 if (flags & GPIOF_ACTIVE_LOW)
42 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
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 return 0;
54
55 free_gpio:
56 gpiod_free(desc);
57 return err;
58}
59EXPORT_SYMBOL_GPL(gpio_request_one);
60
61/*
62 * **DEPRECATED** This function is deprecated and must not be used in new code.
63 */
64int gpio_request(unsigned gpio, const char *label)
65{
66 struct gpio_desc *desc = gpio_to_desc(gpio);
67
68 /* Compatibility: assume unavailable "valid" GPIOs will appear later */
69 if (!desc && gpio_is_valid(gpio))
70 return -EPROBE_DEFER;
71
72 return gpiod_request(desc, label);
73}
74EXPORT_SYMBOL_GPL(gpio_request);
75
76/**
77 * gpio_request_array - request multiple GPIOs in a single call
78 * @array: array of the 'struct gpio'
79 * @num: how many GPIOs in the array
80 *
81 * **DEPRECATED** This function is deprecated and must not be used in new code.
82 */
83int gpio_request_array(const struct gpio *array, size_t num)
84{
85 int i, err;
86
87 for (i = 0; i < num; i++, array++) {
88 err = gpio_request_one(array->gpio, array->flags, array->label);
89 if (err)
90 goto err_free;
91 }
92 return 0;
93
94err_free:
95 while (i--)
96 gpio_free((--array)->gpio);
97 return err;
98}
99EXPORT_SYMBOL_GPL(gpio_request_array);
100
101/**
102 * gpio_free_array - release multiple GPIOs in a single call
103 * @array: array of the 'struct gpio'
104 * @num: how many GPIOs in the array
105 *
106 * **DEPRECATED** This function is deprecated and must not be used in new code.
107 */
108void gpio_free_array(const struct gpio *array, size_t num)
109{
110 while (num--)
111 gpio_free((array++)->gpio);
112}
113EXPORT_SYMBOL_GPL(gpio_free_array);