#ifndef _ASM_GENERIC_GPIO_H1#define _ASM_GENERIC_GPIO_H23#include <linux/kernel.h>4#include <linux/types.h>5#include <linux/errno.h>67#ifdef CONFIG_GPIOLIB89#include <linux/compiler.h>1011/* Platforms may implement their GPIO interface with library code,12* at a small performance cost for non-inlined operations and some13* extra memory (for code and for per-GPIO table entries).14*15* While the GPIO programming interface defines valid GPIO numbers16* to be in the range 0..MAX_INT, this library restricts them to the17* smaller range 0..ARCH_NR_GPIOS-1.18*19* ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of20* builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is21* actually an estimate of a board-specific value.22*/2324#ifndef ARCH_NR_GPIOS25#define ARCH_NR_GPIOS 25626#endif2728/*29* "valid" GPIO numbers are nonnegative and may be passed to30* setup routines like gpio_request(). only some valid numbers31* can successfully be requested and used.32*33* Invalid GPIO numbers are useful for indicating no-such-GPIO in34* platform data and other tables.35*/3637static inline bool gpio_is_valid(int number)38{39return number >= 0 && number < ARCH_NR_GPIOS;40}4142struct device;43struct seq_file;44struct module;45struct device_node;4647/**48* struct gpio_chip - abstract a GPIO controller49* @label: for diagnostics50* @dev: optional device providing the GPIOs51* @owner: helps prevent removal of modules exporting active GPIOs52* @request: optional hook for chip-specific activation, such as53* enabling module power and clock; may sleep54* @free: optional hook for chip-specific deactivation, such as55* disabling module power and clock; may sleep56* @direction_input: configures signal "offset" as input, or returns error57* @get: returns value for signal "offset"; for output signals this58* returns either the value actually sensed, or zero59* @direction_output: configures signal "offset" as output, or returns error60* @set: assigns output value for signal "offset"61* @to_irq: optional hook supporting non-static gpio_to_irq() mappings;62* implementation may not sleep63* @dbg_show: optional routine to show contents in debugfs; default code64* will be used when this is omitted, but custom code can show extra65* state (such as pullup/pulldown configuration).66* @base: identifies the first GPIO number handled by this chip; or, if67* negative during registration, requests dynamic ID allocation.68* @ngpio: the number of GPIOs handled by this controller; the last GPIO69* handled is (base + ngpio - 1).70* @can_sleep: flag must be set iff get()/set() methods sleep, as they71* must while accessing GPIO expander chips over I2C or SPI72* @names: if set, must be an array of strings to use as alternative73* names for the GPIOs in this chip. Any entry in the array74* may be NULL if there is no alias for the GPIO, however the75* array must be @ngpio entries long. A name can include a single printk76* format specifier for an unsigned int. It is substituted by the actual77* number of the gpio.78*79* A gpio_chip can help platforms abstract various sources of GPIOs so80* they can all be accessed through a common programing interface.81* Example sources would be SOC controllers, FPGAs, multifunction82* chips, dedicated GPIO expanders, and so on.83*84* Each chip controls a number of signals, identified in method calls85* by "offset" values in the range 0..(@ngpio - 1). When those signals86* are referenced through calls like gpio_get_value(gpio), the offset87* is calculated by subtracting @base from the gpio number.88*/89struct gpio_chip {90const char *label;91struct device *dev;92struct module *owner;9394int (*request)(struct gpio_chip *chip,95unsigned offset);96void (*free)(struct gpio_chip *chip,97unsigned offset);9899int (*direction_input)(struct gpio_chip *chip,100unsigned offset);101int (*get)(struct gpio_chip *chip,102unsigned offset);103int (*direction_output)(struct gpio_chip *chip,104unsigned offset, int value);105int (*set_debounce)(struct gpio_chip *chip,106unsigned offset, unsigned debounce);107108void (*set)(struct gpio_chip *chip,109unsigned offset, int value);110111int (*to_irq)(struct gpio_chip *chip,112unsigned offset);113114void (*dbg_show)(struct seq_file *s,115struct gpio_chip *chip);116int base;117u16 ngpio;118const char *const *names;119unsigned can_sleep:1;120unsigned exported:1;121122#if defined(CONFIG_OF_GPIO)123/*124* If CONFIG_OF is enabled, then all GPIO controllers described in the125* device tree automatically may have an OF translation126*/127struct device_node *of_node;128int of_gpio_n_cells;129int (*of_xlate)(struct gpio_chip *gc, struct device_node *np,130const void *gpio_spec, u32 *flags);131#endif132};133134extern const char *gpiochip_is_requested(struct gpio_chip *chip,135unsigned offset);136extern int __must_check gpiochip_reserve(int start, int ngpio);137138/* add/remove chips */139extern int gpiochip_add(struct gpio_chip *chip);140extern int __must_check gpiochip_remove(struct gpio_chip *chip);141extern struct gpio_chip *gpiochip_find(void *data,142int (*match)(struct gpio_chip *chip,143void *data));144145146/* Always use the library code for GPIO management calls,147* or when sleeping may be involved.148*/149extern int gpio_request(unsigned gpio, const char *label);150extern void gpio_free(unsigned gpio);151152extern int gpio_direction_input(unsigned gpio);153extern int gpio_direction_output(unsigned gpio, int value);154155extern int gpio_set_debounce(unsigned gpio, unsigned debounce);156157extern int gpio_get_value_cansleep(unsigned gpio);158extern void gpio_set_value_cansleep(unsigned gpio, int value);159160161/* A platform's <asm/gpio.h> code may want to inline the I/O calls when162* the GPIO is constant and refers to some always-present controller,163* giving direct access to chip registers and tight bitbanging loops.164*/165extern int __gpio_get_value(unsigned gpio);166extern void __gpio_set_value(unsigned gpio, int value);167168extern int __gpio_cansleep(unsigned gpio);169170extern int __gpio_to_irq(unsigned gpio);171172/**173* struct gpio - a structure describing a GPIO with configuration174* @gpio: the GPIO number175* @flags: GPIO configuration as specified by GPIOF_*176* @label: a literal description string of this GPIO177*/178struct gpio {179unsigned gpio;180unsigned long flags;181const char *label;182};183184extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);185extern int gpio_request_array(const struct gpio *array, size_t num);186extern void gpio_free_array(const struct gpio *array, size_t num);187188#ifdef CONFIG_GPIO_SYSFS189190/*191* A sysfs interface can be exported by individual drivers if they want,192* but more typically is configured entirely from userspace.193*/194extern int gpio_export(unsigned gpio, bool direction_may_change);195extern int gpio_export_link(struct device *dev, const char *name,196unsigned gpio);197extern int gpio_sysfs_set_active_low(unsigned gpio, int value);198extern void gpio_unexport(unsigned gpio);199200#endif /* CONFIG_GPIO_SYSFS */201202#else /* !CONFIG_GPIOLIB */203204static inline bool gpio_is_valid(int number)205{206/* only non-negative numbers are valid */207return number >= 0;208}209210/* platforms that don't directly support access to GPIOs through I2C, SPI,211* or other blocking infrastructure can use these wrappers.212*/213214static inline int gpio_cansleep(unsigned gpio)215{216return 0;217}218219static inline int gpio_get_value_cansleep(unsigned gpio)220{221might_sleep();222return gpio_get_value(gpio);223}224225static inline void gpio_set_value_cansleep(unsigned gpio, int value)226{227might_sleep();228gpio_set_value(gpio, value);229}230231#endif /* !CONFIG_GPIOLIB */232233#ifndef CONFIG_GPIO_SYSFS234235struct device;236237/* sysfs support is only available with gpiolib, where it's optional */238239static inline int gpio_export(unsigned gpio, bool direction_may_change)240{241return -ENOSYS;242}243244static inline int gpio_export_link(struct device *dev, const char *name,245unsigned gpio)246{247return -ENOSYS;248}249250static inline int gpio_sysfs_set_active_low(unsigned gpio, int value)251{252return -ENOSYS;253}254255static inline void gpio_unexport(unsigned gpio)256{257}258#endif /* CONFIG_GPIO_SYSFS */259260#endif /* _ASM_GENERIC_GPIO_H */261262263