at v6.2 159 lines 4.1 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _ASM_GENERIC_GPIO_H 3#define _ASM_GENERIC_GPIO_H 4 5#include <linux/types.h> 6#include <linux/errno.h> 7 8#ifdef CONFIG_GPIOLIB 9 10#include <linux/compiler.h> 11#include <linux/gpio/driver.h> 12#include <linux/gpio/consumer.h> 13 14/* 15 * Platforms may implement their GPIO interface with library code, 16 * at a small performance cost for non-inlined operations and some 17 * extra memory (for code and for per-GPIO table entries). 18 */ 19 20/* 21 * At the end we want all GPIOs to be dynamically allocated from 0. 22 * However, some legacy drivers still perform fixed allocation. 23 * Until they are all fixed, leave 0-512 space for them. 24 */ 25#define GPIO_DYNAMIC_BASE 512 26 27struct device; 28struct gpio; 29struct seq_file; 30struct module; 31struct device_node; 32struct gpio_desc; 33 34/* caller holds gpio_lock *OR* gpio is marked as requested */ 35static inline struct gpio_chip *gpio_to_chip(unsigned gpio) 36{ 37 return gpiod_to_chip(gpio_to_desc(gpio)); 38} 39 40/* Always use the library code for GPIO management calls, 41 * or when sleeping may be involved. 42 */ 43extern int gpio_request(unsigned gpio, const char *label); 44extern void gpio_free(unsigned gpio); 45 46static inline int gpio_direction_input(unsigned gpio) 47{ 48 return gpiod_direction_input(gpio_to_desc(gpio)); 49} 50static inline int gpio_direction_output(unsigned gpio, int value) 51{ 52 return gpiod_direction_output_raw(gpio_to_desc(gpio), value); 53} 54 55static inline int gpio_set_debounce(unsigned gpio, unsigned debounce) 56{ 57 return gpiod_set_debounce(gpio_to_desc(gpio), debounce); 58} 59 60static inline int gpio_get_value_cansleep(unsigned gpio) 61{ 62 return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio)); 63} 64static inline void gpio_set_value_cansleep(unsigned gpio, int value) 65{ 66 return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value); 67} 68 69 70/* A platform's <asm/gpio.h> code may want to inline the I/O calls when 71 * the GPIO is constant and refers to some always-present controller, 72 * giving direct access to chip registers and tight bitbanging loops. 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 return gpiod_set_raw_value(gpio_to_desc(gpio), value); 81} 82 83static inline int __gpio_cansleep(unsigned gpio) 84{ 85 return gpiod_cansleep(gpio_to_desc(gpio)); 86} 87 88static inline int __gpio_to_irq(unsigned gpio) 89{ 90 return gpiod_to_irq(gpio_to_desc(gpio)); 91} 92 93extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label); 94extern int gpio_request_array(const struct gpio *array, size_t num); 95extern void gpio_free_array(const struct gpio *array, size_t num); 96 97/* 98 * A sysfs interface can be exported by individual drivers if they want, 99 * but more typically is configured entirely from userspace. 100 */ 101static inline int gpio_export(unsigned gpio, bool direction_may_change) 102{ 103 return gpiod_export(gpio_to_desc(gpio), direction_may_change); 104} 105 106static inline int gpio_export_link(struct device *dev, const char *name, 107 unsigned gpio) 108{ 109 return gpiod_export_link(dev, name, gpio_to_desc(gpio)); 110} 111 112static inline void gpio_unexport(unsigned gpio) 113{ 114 gpiod_unexport(gpio_to_desc(gpio)); 115} 116 117#else /* !CONFIG_GPIOLIB */ 118 119#include <linux/kernel.h> 120 121/* platforms that don't directly support access to GPIOs through I2C, SPI, 122 * or other blocking infrastructure can use these wrappers. 123 */ 124 125static inline int gpio_cansleep(unsigned gpio) 126{ 127 return 0; 128} 129 130static inline int gpio_get_value_cansleep(unsigned gpio) 131{ 132 might_sleep(); 133 return __gpio_get_value(gpio); 134} 135 136static inline void gpio_set_value_cansleep(unsigned gpio, int value) 137{ 138 might_sleep(); 139 __gpio_set_value(gpio, value); 140} 141 142#endif /* !CONFIG_GPIOLIB */ 143 144/* 145 * "valid" GPIO numbers are nonnegative and may be passed to 146 * setup routines like gpio_request(). only some valid numbers 147 * can successfully be requested and used. 148 * 149 * Invalid GPIO numbers are useful for indicating no-such-GPIO in 150 * platform data and other tables. 151 */ 152 153static inline bool gpio_is_valid(int number) 154{ 155 /* only non-negative numbers are valid */ 156 return number >= 0; 157} 158 159#endif /* _ASM_GENERIC_GPIO_H */