]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/asm-generic/gpio.h
macvlan: do not assume mac_header is set in macvlan_broadcast()
[mirror_ubuntu-bionic-kernel.git] / include / asm-generic / gpio.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
4c20386c
DB
2#ifndef _ASM_GENERIC_GPIO_H
3#define _ASM_GENERIC_GPIO_H
4
b3db4a8a 5#include <linux/kernel.h>
6ea0205b 6#include <linux/types.h>
25947d5a 7#include <linux/errno.h>
15c9a0ac 8#include <linux/of.h>
6ea0205b 9
7444a72e 10#ifdef CONFIG_GPIOLIB
d2876d08 11
6ea0205b 12#include <linux/compiler.h>
79a9becd
AC
13#include <linux/gpio/driver.h>
14#include <linux/gpio/consumer.h>
6ea0205b 15
d2876d08
DB
16/* Platforms may implement their GPIO interface with library code,
17 * at a small performance cost for non-inlined operations and some
18 * extra memory (for code and for per-GPIO table entries).
19 *
20 * While the GPIO programming interface defines valid GPIO numbers
21 * to be in the range 0..MAX_INT, this library restricts them to the
6a9436d0 22 * smaller range 0..ARCH_NR_GPIOS-1.
c956126c
DB
23 *
24 * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of
25 * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is
26 * actually an estimate of a board-specific value.
d2876d08
DB
27 */
28
29#ifndef ARCH_NR_GPIOS
aa6aedb5
AB
30#if defined(CONFIG_ARCH_NR_GPIO) && CONFIG_ARCH_NR_GPIO > 0
31#define ARCH_NR_GPIOS CONFIG_ARCH_NR_GPIO
32#else
7ca267fa 33#define ARCH_NR_GPIOS 512
d2876d08 34#endif
aa6aedb5 35#endif
d2876d08 36
c956126c
DB
37/*
38 * "valid" GPIO numbers are nonnegative and may be passed to
39 * setup routines like gpio_request(). only some valid numbers
40 * can successfully be requested and used.
41 *
42 * Invalid GPIO numbers are useful for indicating no-such-GPIO in
43 * platform data and other tables.
44 */
45
3474cb3c 46static inline bool gpio_is_valid(int number)
e6de1808 47{
3474cb3c 48 return number >= 0 && number < ARCH_NR_GPIOS;
e6de1808
GL
49}
50
1f018c8d 51struct device;
feb83699 52struct gpio;
d2876d08 53struct seq_file;
438d8908 54struct module;
a19e3da5 55struct device_node;
6c0b4e6c 56struct gpio_desc;
d2876d08 57
79a9becd
AC
58/* caller holds gpio_lock *OR* gpio is marked as requested */
59static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
60{
61 return gpiod_to_chip(gpio_to_desc(gpio));
62}
d2876d08
DB
63
64/* Always use the library code for GPIO management calls,
65 * or when sleeping may be involved.
66 */
d8a3515e 67extern int gpio_request(unsigned gpio, const char *label);
d2876d08
DB
68extern void gpio_free(unsigned gpio);
69
c7caf868
AC
70static inline int gpio_direction_input(unsigned gpio)
71{
72 return gpiod_direction_input(gpio_to_desc(gpio));
73}
74static inline int gpio_direction_output(unsigned gpio, int value)
75{
76 return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
77}
d2876d08 78
c7caf868
AC
79static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
80{
81 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
82}
c4b5be98 83
79a9becd
AC
84static inline int gpio_get_value_cansleep(unsigned gpio)
85{
86 return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
87}
88static inline void gpio_set_value_cansleep(unsigned gpio, int value)
89{
90 return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
91}
d2876d08
DB
92
93
94/* A platform's <asm/gpio.h> code may want to inline the I/O calls when
95 * the GPIO is constant and refers to some always-present controller,
96 * giving direct access to chip registers and tight bitbanging loops.
97 */
79a9becd
AC
98static inline int __gpio_get_value(unsigned gpio)
99{
100 return gpiod_get_raw_value(gpio_to_desc(gpio));
101}
102static inline void __gpio_set_value(unsigned gpio, int value)
103{
104 return gpiod_set_raw_value(gpio_to_desc(gpio), value);
105}
d2876d08 106
79a9becd
AC
107static inline int __gpio_cansleep(unsigned gpio)
108{
109 return gpiod_cansleep(gpio_to_desc(gpio));
110}
d2876d08 111
79a9becd
AC
112static inline int __gpio_to_irq(unsigned gpio)
113{
114 return gpiod_to_irq(gpio_to_desc(gpio));
115}
d2876d08 116
d8a3515e 117extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
7c295975
LPC
118extern int gpio_request_array(const struct gpio *array, size_t num);
119extern void gpio_free_array(const struct gpio *array, size_t num);
3e45f1d1 120
d8f388d8
DB
121/*
122 * A sysfs interface can be exported by individual drivers if they want,
123 * but more typically is configured entirely from userspace.
124 */
79a9becd
AC
125static inline int gpio_export(unsigned gpio, bool direction_may_change)
126{
127 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
128}
d8f388d8 129
79a9becd
AC
130static inline int gpio_export_link(struct device *dev, const char *name,
131 unsigned gpio)
132{
133 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
134}
135
79a9becd
AC
136static inline void gpio_unexport(unsigned gpio)
137{
138 gpiod_unexport(gpio_to_desc(gpio));
139}
d8f388d8 140
09cd9527 141#else /* !CONFIG_GPIOLIB */
d2876d08 142
3474cb3c 143static inline bool gpio_is_valid(int number)
e6de1808
GL
144{
145 /* only non-negative numbers are valid */
146 return number >= 0;
147}
148
4c20386c
DB
149/* platforms that don't directly support access to GPIOs through I2C, SPI,
150 * or other blocking infrastructure can use these wrappers.
151 */
152
153static inline int gpio_cansleep(unsigned gpio)
154{
155 return 0;
156}
157
158static inline int gpio_get_value_cansleep(unsigned gpio)
159{
160 might_sleep();
eb9ae7f2 161 return __gpio_get_value(gpio);
4c20386c
DB
162}
163
164static inline void gpio_set_value_cansleep(unsigned gpio, int value)
165{
166 might_sleep();
eb9ae7f2 167 __gpio_set_value(gpio, value);
4c20386c
DB
168}
169
09cd9527 170#endif /* !CONFIG_GPIOLIB */
d8f388d8 171
4c20386c 172#endif /* _ASM_GENERIC_GPIO_H */