1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * NOTE: This header *must not* be included.
4 *
5 * This is the LEGACY GPIO bulk include file, including legacy APIs. It is
6 * used for GPIO drivers still referencing the global GPIO numberspace,
7 * and should not be included in new code.
8 *
9 * If you're implementing a GPIO driver, only include <linux/gpio/driver.h>
10 * If you're implementing a GPIO consumer, only include <linux/gpio/consumer.h>
11 */
12#ifndef __LINUX_GPIO_H
13#define __LINUX_GPIO_H
14
15#include <linux/types.h>
16#ifdef CONFIG_GPIOLIB
17#include <linux/gpio/consumer.h>
18#endif
19
20#ifdef CONFIG_GPIOLIB_LEGACY
21
22struct device;
23
24/* make these flag values available regardless of GPIO kconfig options */
25#define GPIOF_IN ((1 << 0))
26#define GPIOF_OUT_INIT_LOW ((0 << 0) | (0 << 1))
27#define GPIOF_OUT_INIT_HIGH ((0 << 0) | (1 << 1))
28
29#ifdef CONFIG_GPIOLIB
30/*
31 * "valid" GPIO numbers are nonnegative and may be passed to
32 * setup routines like gpio_request(). Only some valid numbers
33 * can successfully be requested and used.
34 *
35 * Invalid GPIO numbers are useful for indicating no-such-GPIO in
36 * platform data and other tables.
37 */
38static inline bool gpio_is_valid(int number)
39{
40 /* only non-negative numbers are valid */
41 return number >= 0;
42}
43
44/*
45 * Platforms may implement their GPIO interface with library code,
46 * at a small performance cost for non-inlined operations and some
47 * extra memory (for code and for per-GPIO table entries).
48 */
49
50/* Always use the library code for GPIO management calls,
51 * or when sleeping may be involved.
52 */
53int gpio_request(unsigned gpio, const char *label);
54void gpio_free(unsigned gpio);
55
56static inline int gpio_direction_input(unsigned gpio)
57{
58 return gpiod_direction_input(gpio_to_desc(gpio));
59}
60static inline int gpio_direction_output(unsigned gpio, int value)
61{
62 return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
63}
64
65static inline int gpio_get_value_cansleep(unsigned gpio)
66{
67 return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
68}
69static inline void gpio_set_value_cansleep(unsigned gpio, int value)
70{
71 gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
72}
73
74static inline int gpio_get_value(unsigned gpio)
75{
76 return gpiod_get_raw_value(gpio_to_desc(gpio));
77}
78static inline void gpio_set_value(unsigned gpio, int value)
79{
80 gpiod_set_raw_value(gpio_to_desc(gpio), value);
81}
82
83static inline int gpio_to_irq(unsigned gpio)
84{
85 return gpiod_to_irq(gpio_to_desc(gpio));
86}
87
88int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
89
90int devm_gpio_request_one(struct device *dev, unsigned gpio,
91 unsigned long flags, const char *label);
92
93#else /* ! CONFIG_GPIOLIB */
94
95#include <linux/kernel.h>
96
97#include <asm/bug.h>
98#include <asm/errno.h>
99
100static inline bool gpio_is_valid(int number)
101{
102 return false;
103}
104
105static inline int gpio_request(unsigned gpio, const char *label)
106{
107 return -ENOSYS;
108}
109
110static inline int gpio_request_one(unsigned gpio,
111 unsigned long flags, const char *label)
112{
113 return -ENOSYS;
114}
115
116static inline void gpio_free(unsigned gpio)
117{
118 might_sleep();
119
120 /* GPIO can never have been requested */
121 WARN_ON(1);
122}
123
124static inline int gpio_direction_input(unsigned gpio)
125{
126 return -ENOSYS;
127}
128
129static inline int gpio_direction_output(unsigned gpio, int value)
130{
131 return -ENOSYS;
132}
133
134static inline int gpio_get_value(unsigned gpio)
135{
136 /* GPIO can never have been requested or set as {in,out}put */
137 WARN_ON(1);
138 return 0;
139}
140
141static inline void gpio_set_value(unsigned gpio, int value)
142{
143 /* GPIO can never have been requested or set as output */
144 WARN_ON(1);
145}
146
147static inline int gpio_get_value_cansleep(unsigned gpio)
148{
149 /* GPIO can never have been requested or set as {in,out}put */
150 WARN_ON(1);
151 return 0;
152}
153
154static inline void gpio_set_value_cansleep(unsigned gpio, int value)
155{
156 /* GPIO can never have been requested or set as output */
157 WARN_ON(1);
158}
159
160static inline int gpio_to_irq(unsigned gpio)
161{
162 /* GPIO can never have been requested or set as input */
163 WARN_ON(1);
164 return -EINVAL;
165}
166
167static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
168 unsigned long flags, const char *label)
169{
170 WARN_ON(1);
171 return -EINVAL;
172}
173
174#endif /* ! CONFIG_GPIOLIB */
175#endif /* CONFIG_GPIOLIB_LEGACY */
176#endif /* __LINUX_GPIO_H */
177