Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_GENERIC_GPIO_H
0003 #define _ASM_GENERIC_GPIO_H
0004 
0005 #include <linux/types.h>
0006 #include <linux/errno.h>
0007 
0008 #ifdef CONFIG_GPIOLIB
0009 
0010 #include <linux/compiler.h>
0011 #include <linux/gpio/driver.h>
0012 #include <linux/gpio/consumer.h>
0013 
0014 /* Platforms may implement their GPIO interface with library code,
0015  * at a small performance cost for non-inlined operations and some
0016  * extra memory (for code and for per-GPIO table entries).
0017  *
0018  * While the GPIO programming interface defines valid GPIO numbers
0019  * to be in the range 0..MAX_INT, this library restricts them to the
0020  * smaller range 0..ARCH_NR_GPIOS-1.
0021  *
0022  * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of
0023  * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is
0024  * actually an estimate of a board-specific value.
0025  */
0026 
0027 #ifndef ARCH_NR_GPIOS
0028 #if defined(CONFIG_ARCH_NR_GPIO) && CONFIG_ARCH_NR_GPIO > 0
0029 #define ARCH_NR_GPIOS CONFIG_ARCH_NR_GPIO
0030 #else
0031 #define ARCH_NR_GPIOS       512
0032 #endif
0033 #endif
0034 
0035 /*
0036  * "valid" GPIO numbers are nonnegative and may be passed to
0037  * setup routines like gpio_request().  only some valid numbers
0038  * can successfully be requested and used.
0039  *
0040  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
0041  * platform data and other tables.
0042  */
0043 
0044 static inline bool gpio_is_valid(int number)
0045 {
0046     return number >= 0 && number < ARCH_NR_GPIOS;
0047 }
0048 
0049 struct device;
0050 struct gpio;
0051 struct seq_file;
0052 struct module;
0053 struct device_node;
0054 struct gpio_desc;
0055 
0056 /* caller holds gpio_lock *OR* gpio is marked as requested */
0057 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
0058 {
0059     return gpiod_to_chip(gpio_to_desc(gpio));
0060 }
0061 
0062 /* Always use the library code for GPIO management calls,
0063  * or when sleeping may be involved.
0064  */
0065 extern int gpio_request(unsigned gpio, const char *label);
0066 extern void gpio_free(unsigned gpio);
0067 
0068 static inline int gpio_direction_input(unsigned gpio)
0069 {
0070     return gpiod_direction_input(gpio_to_desc(gpio));
0071 }
0072 static inline int gpio_direction_output(unsigned gpio, int value)
0073 {
0074     return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
0075 }
0076 
0077 static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
0078 {
0079     return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
0080 }
0081 
0082 static inline int gpio_get_value_cansleep(unsigned gpio)
0083 {
0084     return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
0085 }
0086 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
0087 {
0088     return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
0089 }
0090 
0091 
0092 /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
0093  * the GPIO is constant and refers to some always-present controller,
0094  * giving direct access to chip registers and tight bitbanging loops.
0095  */
0096 static inline int __gpio_get_value(unsigned gpio)
0097 {
0098     return gpiod_get_raw_value(gpio_to_desc(gpio));
0099 }
0100 static inline void __gpio_set_value(unsigned gpio, int value)
0101 {
0102     return gpiod_set_raw_value(gpio_to_desc(gpio), value);
0103 }
0104 
0105 static inline int __gpio_cansleep(unsigned gpio)
0106 {
0107     return gpiod_cansleep(gpio_to_desc(gpio));
0108 }
0109 
0110 static inline int __gpio_to_irq(unsigned gpio)
0111 {
0112     return gpiod_to_irq(gpio_to_desc(gpio));
0113 }
0114 
0115 extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
0116 extern int gpio_request_array(const struct gpio *array, size_t num);
0117 extern void gpio_free_array(const struct gpio *array, size_t num);
0118 
0119 /*
0120  * A sysfs interface can be exported by individual drivers if they want,
0121  * but more typically is configured entirely from userspace.
0122  */
0123 static inline int gpio_export(unsigned gpio, bool direction_may_change)
0124 {
0125     return gpiod_export(gpio_to_desc(gpio), direction_may_change);
0126 }
0127 
0128 static inline int gpio_export_link(struct device *dev, const char *name,
0129                    unsigned gpio)
0130 {
0131     return gpiod_export_link(dev, name, gpio_to_desc(gpio));
0132 }
0133 
0134 static inline void gpio_unexport(unsigned gpio)
0135 {
0136     gpiod_unexport(gpio_to_desc(gpio));
0137 }
0138 
0139 #else   /* !CONFIG_GPIOLIB */
0140 
0141 #include <linux/kernel.h>
0142 
0143 static inline bool gpio_is_valid(int number)
0144 {
0145     /* only non-negative numbers are valid */
0146     return number >= 0;
0147 }
0148 
0149 /* platforms that don't directly support access to GPIOs through I2C, SPI,
0150  * or other blocking infrastructure can use these wrappers.
0151  */
0152 
0153 static inline int gpio_cansleep(unsigned gpio)
0154 {
0155     return 0;
0156 }
0157 
0158 static inline int gpio_get_value_cansleep(unsigned gpio)
0159 {
0160     might_sleep();
0161     return __gpio_get_value(gpio);
0162 }
0163 
0164 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
0165 {
0166     might_sleep();
0167     __gpio_set_value(gpio, value);
0168 }
0169 
0170 #endif /* !CONFIG_GPIOLIB */
0171 
0172 #endif /* _ASM_GENERIC_GPIO_H */