]> Git Repo - linux.git/blame - include/linux/gpio.h
Linux 6.14-rc3
[linux.git] / include / linux / gpio.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
56a46b61 2/*
447e140e 3 * NOTE: This header *must not* be included.
56a46b61
LW
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 */
7560fa60
DB
12#ifndef __LINUX_GPIO_H
13#define __LINUX_GPIO_H
14
eccb7a00 15#include <linux/types.h>
7563bbf8 16
380c7ba3
AS
17struct device;
18
c001fb72 19/* make these flag values available regardless of GPIO kconfig options */
8c045ca5
AS
20#define GPIOF_IN ((1 << 0))
21#define GPIOF_OUT_INIT_LOW ((0 << 0) | (0 << 1))
22#define GPIOF_OUT_INIT_HIGH ((0 << 0) | (1 << 1))
c001fb72 23
feb83699
MB
24/**
25 * struct gpio - a structure describing a GPIO with configuration
26 * @gpio: the GPIO number
27 * @flags: GPIO configuration as specified by GPIOF_*
28 * @label: a literal description string of this GPIO
29 */
30struct gpio {
31 unsigned gpio;
32 unsigned long flags;
33 const char *label;
34};
35
76ec9d18 36#ifdef CONFIG_GPIOLIB
7563bbf8 37
eccb7a00
AB
38#include <linux/gpio/consumer.h>
39
40/*
41 * "valid" GPIO numbers are nonnegative and may be passed to
42 * setup routines like gpio_request(). Only some valid numbers
43 * can successfully be requested and used.
44 *
45 * Invalid GPIO numbers are useful for indicating no-such-GPIO in
46 * platform data and other tables.
47 */
48static inline bool gpio_is_valid(int number)
7563bbf8 49{
eccb7a00
AB
50 /* only non-negative numbers are valid */
51 return number >= 0;
7563bbf8
MB
52}
53
eccb7a00
AB
54/*
55 * Platforms may implement their GPIO interface with library code,
56 * at a small performance cost for non-inlined operations and some
57 * extra memory (for code and for per-GPIO table entries).
58 */
59
60/*
61 * At the end we want all GPIOs to be dynamically allocated from 0.
62 * However, some legacy drivers still perform fixed allocation.
63 * Until they are all fixed, leave 0-512 space for them.
64 */
65#define GPIO_DYNAMIC_BASE 512
8a7a6103
AS
66/*
67 * Define the maximum of the possible GPIO in the global numberspace.
68 * While the GPIO base and numbers are positive, we limit it with signed
69 * maximum as a lot of code is using negative values for special cases.
70 */
71#define GPIO_DYNAMIC_MAX INT_MAX
eccb7a00
AB
72
73/* Always use the library code for GPIO management calls,
74 * or when sleeping may be involved.
75 */
76int gpio_request(unsigned gpio, const char *label);
77void gpio_free(unsigned gpio);
78
79static inline int gpio_direction_input(unsigned gpio)
80{
81 return gpiod_direction_input(gpio_to_desc(gpio));
82}
83static inline int gpio_direction_output(unsigned gpio, int value)
7563bbf8 84{
eccb7a00 85 return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
7563bbf8
MB
86}
87
eccb7a00
AB
88static inline int gpio_get_value_cansleep(unsigned gpio)
89{
90 return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
91}
92static inline void gpio_set_value_cansleep(unsigned gpio, int value)
7563bbf8 93{
eccb7a00
AB
94 return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
95}
96
97static inline int gpio_get_value(unsigned gpio)
98{
99 return gpiod_get_raw_value(gpio_to_desc(gpio));
100}
101static inline void gpio_set_value(unsigned gpio, int value)
102{
103 return gpiod_set_raw_value(gpio_to_desc(gpio), value);
104}
105
eccb7a00
AB
106static inline int gpio_to_irq(unsigned gpio)
107{
108 return gpiod_to_irq(gpio_to_desc(gpio));
109}
110
111int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
eccb7a00 112
403c1d0b
LW
113int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
114int devm_gpio_request_one(struct device *dev, unsigned gpio,
115 unsigned long flags, const char *label);
403c1d0b 116
76ec9d18 117#else /* ! CONFIG_GPIOLIB */
7560fa60 118
3d599d1c 119#include <linux/kernel.h>
6ea0205b 120
380c7ba3
AS
121#include <asm/bug.h>
122#include <asm/errno.h>
a4177ee7 123
3474cb3c 124static inline bool gpio_is_valid(int number)
7560fa60 125{
3474cb3c 126 return false;
7560fa60
DB
127}
128
d8a3515e 129static inline int gpio_request(unsigned gpio, const char *label)
7560fa60
DB
130{
131 return -ENOSYS;
132}
133
323b7fe8 134static inline int gpio_request_one(unsigned gpio,
5f829e40
WS
135 unsigned long flags, const char *label)
136{
137 return -ENOSYS;
138}
139
7560fa60
DB
140static inline void gpio_free(unsigned gpio)
141{
3d599d1c
UKK
142 might_sleep();
143
7560fa60 144 /* GPIO can never have been requested */
2c96922a
MB
145 WARN_ON(1);
146}
147
d8a3515e 148static inline int gpio_direction_input(unsigned gpio)
7560fa60
DB
149{
150 return -ENOSYS;
151}
152
d8a3515e 153static inline int gpio_direction_output(unsigned gpio, int value)
7560fa60
DB
154{
155 return -ENOSYS;
156}
157
158static inline int gpio_get_value(unsigned gpio)
159{
160 /* GPIO can never have been requested or set as {in,out}put */
161 WARN_ON(1);
162 return 0;
163}
164
165static inline void gpio_set_value(unsigned gpio, int value)
166{
167 /* GPIO can never have been requested or set as output */
168 WARN_ON(1);
169}
170
7560fa60
DB
171static inline int gpio_get_value_cansleep(unsigned gpio)
172{
173 /* GPIO can never have been requested or set as {in,out}put */
174 WARN_ON(1);
175 return 0;
176}
177
178static inline void gpio_set_value_cansleep(unsigned gpio, int value)
179{
180 /* GPIO can never have been requested or set as output */
181 WARN_ON(1);
182}
183
184static inline int gpio_to_irq(unsigned gpio)
185{
186 /* GPIO can never have been requested or set as input */
187 WARN_ON(1);
188 return -EINVAL;
189}
190
403c1d0b
LW
191static inline int devm_gpio_request(struct device *dev, unsigned gpio,
192 const char *label)
193{
194 WARN_ON(1);
195 return -EINVAL;
196}
7560fa60 197
403c1d0b
LW
198static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
199 unsigned long flags, const char *label)
200{
201 WARN_ON(1);
202 return -EINVAL;
203}
6a89a314 204
403c1d0b 205#endif /* ! CONFIG_GPIOLIB */
6a89a314 206
7560fa60 207#endif /* __LINUX_GPIO_H */
This page took 1.890796 seconds and 5 git commands to generate.