linux/include/asm-generic/gpio.h
<<
>>
Prefs
   1#ifndef _ASM_GENERIC_GPIO_H
   2#define _ASM_GENERIC_GPIO_H
   3
   4#include <linux/kernel.h>
   5#include <linux/types.h>
   6#include <linux/errno.h>
   7
   8#ifdef CONFIG_GPIOLIB
   9
  10#include <linux/compiler.h>
  11
  12/* Platforms may implement their GPIO interface with library code,
  13 * at a small performance cost for non-inlined operations and some
  14 * extra memory (for code and for per-GPIO table entries).
  15 *
  16 * While the GPIO programming interface defines valid GPIO numbers
  17 * to be in the range 0..MAX_INT, this library restricts them to the
  18 * smaller range 0..ARCH_NR_GPIOS-1.
  19 */
  20
  21#ifndef ARCH_NR_GPIOS
  22#define ARCH_NR_GPIOS           256
  23#endif
  24
  25static inline int gpio_is_valid(int number)
  26{
  27        /* only some non-negative numbers are valid */
  28        return ((unsigned)number) < ARCH_NR_GPIOS;
  29}
  30
  31struct seq_file;
  32struct module;
  33
  34/**
  35 * struct gpio_chip - abstract a GPIO controller
  36 * @label: for diagnostics
  37 * @dev: optional device providing the GPIOs
  38 * @owner: helps prevent removal of modules exporting active GPIOs
  39 * @request: optional hook for chip-specific activation, such as
  40 *      enabling module power and clock; may sleep
  41 * @free: optional hook for chip-specific deactivation, such as
  42 *      disabling module power and clock; may sleep
  43 * @direction_input: configures signal "offset" as input, or returns error
  44 * @get: returns value for signal "offset"; for output signals this
  45 *      returns either the value actually sensed, or zero
  46 * @direction_output: configures signal "offset" as output, or returns error
  47 * @set: assigns output value for signal "offset"
  48 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  49 *      implementation may not sleep
  50 * @dbg_show: optional routine to show contents in debugfs; default code
  51 *      will be used when this is omitted, but custom code can show extra
  52 *      state (such as pullup/pulldown configuration).
  53 * @base: identifies the first GPIO number handled by this chip; or, if
  54 *      negative during registration, requests dynamic ID allocation.
  55 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  56 *      handled is (base + ngpio - 1).
  57 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  58 *      must while accessing GPIO expander chips over I2C or SPI
  59 * @names: if set, must be an array of strings to use as alternative
  60 *      names for the GPIOs in this chip. Any entry in the array
  61 *      may be NULL if there is no alias for the GPIO, however the
  62 *      array must be @ngpio entries long.
  63 *
  64 * A gpio_chip can help platforms abstract various sources of GPIOs so
  65 * they can all be accessed through a common programing interface.
  66 * Example sources would be SOC controllers, FPGAs, multifunction
  67 * chips, dedicated GPIO expanders, and so on.
  68 *
  69 * Each chip controls a number of signals, identified in method calls
  70 * by "offset" values in the range 0..(@ngpio - 1).  When those signals
  71 * are referenced through calls like gpio_get_value(gpio), the offset
  72 * is calculated by subtracting @base from the gpio number.
  73 */
  74struct gpio_chip {
  75        const char              *label;
  76        struct device           *dev;
  77        struct module           *owner;
  78
  79        int                     (*request)(struct gpio_chip *chip,
  80                                                unsigned offset);
  81        void                    (*free)(struct gpio_chip *chip,
  82                                                unsigned offset);
  83
  84        int                     (*direction_input)(struct gpio_chip *chip,
  85                                                unsigned offset);
  86        int                     (*get)(struct gpio_chip *chip,
  87                                                unsigned offset);
  88        int                     (*direction_output)(struct gpio_chip *chip,
  89                                                unsigned offset, int value);
  90        void                    (*set)(struct gpio_chip *chip,
  91                                                unsigned offset, int value);
  92
  93        int                     (*to_irq)(struct gpio_chip *chip,
  94                                                unsigned offset);
  95
  96        void                    (*dbg_show)(struct seq_file *s,
  97                                                struct gpio_chip *chip);
  98        int                     base;
  99        u16                     ngpio;
 100        char                    **names;
 101        unsigned                can_sleep:1;
 102        unsigned                exported:1;
 103};
 104
 105extern const char *gpiochip_is_requested(struct gpio_chip *chip,
 106                        unsigned offset);
 107extern int __must_check gpiochip_reserve(int start, int ngpio);
 108
 109/* add/remove chips */
 110extern int gpiochip_add(struct gpio_chip *chip);
 111extern int __must_check gpiochip_remove(struct gpio_chip *chip);
 112
 113
 114/* Always use the library code for GPIO management calls,
 115 * or when sleeping may be involved.
 116 */
 117extern int gpio_request(unsigned gpio, const char *label);
 118extern void gpio_free(unsigned gpio);
 119
 120extern int gpio_direction_input(unsigned gpio);
 121extern int gpio_direction_output(unsigned gpio, int value);
 122
 123extern int gpio_get_value_cansleep(unsigned gpio);
 124extern void gpio_set_value_cansleep(unsigned gpio, int value);
 125
 126
 127/* A platform's <asm/gpio.h> code may want to inline the I/O calls when
 128 * the GPIO is constant and refers to some always-present controller,
 129 * giving direct access to chip registers and tight bitbanging loops.
 130 */
 131extern int __gpio_get_value(unsigned gpio);
 132extern void __gpio_set_value(unsigned gpio, int value);
 133
 134extern int __gpio_cansleep(unsigned gpio);
 135
 136extern int __gpio_to_irq(unsigned gpio);
 137
 138#ifdef CONFIG_GPIO_SYSFS
 139
 140/*
 141 * A sysfs interface can be exported by individual drivers if they want,
 142 * but more typically is configured entirely from userspace.
 143 */
 144extern int gpio_export(unsigned gpio, bool direction_may_change);
 145extern int gpio_export_link(struct device *dev, const char *name,
 146                        unsigned gpio);
 147extern void gpio_unexport(unsigned gpio);
 148
 149#endif  /* CONFIG_GPIO_SYSFS */
 150
 151#else   /* !CONFIG_HAVE_GPIO_LIB */
 152
 153static inline int gpio_is_valid(int number)
 154{
 155        /* only non-negative numbers are valid */
 156        return number >= 0;
 157}
 158
 159/* platforms that don't directly support access to GPIOs through I2C, SPI,
 160 * or other blocking infrastructure can use these wrappers.
 161 */
 162
 163static inline int gpio_cansleep(unsigned gpio)
 164{
 165        return 0;
 166}
 167
 168static inline int gpio_get_value_cansleep(unsigned gpio)
 169{
 170        might_sleep();
 171        return gpio_get_value(gpio);
 172}
 173
 174static inline void gpio_set_value_cansleep(unsigned gpio, int value)
 175{
 176        might_sleep();
 177        gpio_set_value(gpio, value);
 178}
 179
 180#endif /* !CONFIG_HAVE_GPIO_LIB */
 181
 182#ifndef CONFIG_GPIO_SYSFS
 183
 184/* sysfs support is only available with gpiolib, where it's optional */
 185
 186static inline int gpio_export(unsigned gpio, bool direction_may_change)
 187{
 188        return -ENOSYS;
 189}
 190
 191static inline int gpio_export_link(struct device *dev, const char *name,
 192                                unsigned gpio)
 193{
 194        return -ENOSYS;
 195}
 196
 197static inline void gpio_unexport(unsigned gpio)
 198{
 199}
 200#endif  /* CONFIG_GPIO_SYSFS */
 201
 202#endif /* _ASM_GENERIC_GPIO_H */
 203