linux/include/linux/gpio/driver.h
<<
>>
Prefs
   1#ifndef __LINUX_GPIO_DRIVER_H
   2#define __LINUX_GPIO_DRIVER_H
   3
   4#include <linux/device.h>
   5#include <linux/types.h>
   6#include <linux/module.h>
   7#include <linux/irq.h>
   8#include <linux/irqchip/chained_irq.h>
   9#include <linux/irqdomain.h>
  10#include <linux/pinctrl/pinctrl.h>
  11#include <linux/pinctrl/pinconf-generic.h>
  12
  13struct gpio_desc;
  14struct of_phandle_args;
  15struct device_node;
  16struct seq_file;
  17struct gpio_device;
  18
  19/**
  20 * struct gpio_chip - abstract a GPIO controller
  21 * @label: for diagnostics
  22 * @gpiodev: the internal state holder, opaque struct
  23 * @dev: optional device providing the GPIOs
  24 * @owner: helps prevent removal of modules exporting active GPIOs
  25 * @request: optional hook for chip-specific activation, such as
  26 *      enabling module power and clock; may sleep
  27 * @free: optional hook for chip-specific deactivation, such as
  28 *      disabling module power and clock; may sleep
  29 * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  30 *      (same as GPIOF_DIR_XXX), or negative error
  31 * @direction_input: configures signal "offset" as input, or returns error
  32 * @direction_output: configures signal "offset" as output, or returns error
  33 * @get: returns value for signal "offset"; for output signals this
  34 *      returns either the value actually sensed, or zero
  35 * @set: assigns output value for signal "offset"
  36 * @set_config: optional hook for all kinds of settings. Uses the same
  37 *      packed config format as generic pinconf.
  38 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  39 *      implementation may not sleep
  40 * @dbg_show: optional routine to show contents in debugfs; default code
  41 *      will be used when this is omitted, but custom code can show extra
  42 *      state (such as pullup/pulldown configuration).
  43 * @base: identifies the first GPIO number handled by this chip; or, if
  44 *      negative during registration, requests dynamic ID allocation.
  45 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  46 *      handled is (base + ngpio - 1).
  47 * @desc: array of ngpio descriptors. Private.
  48 * @names: if set, must be an array of strings to use as alternative
  49 *      names for the GPIOs in this chip. Any entry in the array
  50 *      may be NULL if there is no alias for the GPIO, however the
  51 *      array must be @ngpio entries long.  A name can include a single printk
  52 *      format specifier for an unsigned int.  It is substituted by the actual
  53 *      number of the gpio.
  54 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  55 *      must while accessing GPIO expander chips over I2C or SPI. This
  56 *      implies that if the chip supports IRQs, these IRQs need to be threaded
  57 *      as the chip access may sleep when e.g. reading out the IRQ status
  58 *      registers.
  59 * @exported: flags if the gpiochip is exported for use from sysfs. Private.
  60 *
  61 * A gpio_chip can help platforms abstract various sources of GPIOs so
  62 * they can all be accessed through a common programing interface.
  63 * Example sources would be SOC controllers, FPGAs, multifunction
  64 * chips, dedicated GPIO expanders, and so on.
  65 *
  66 * Each chip controls a number of signals, identified in method calls
  67 * by "offset" values in the range 0..(@ngpio - 1).  When those signals
  68 * are referenced through calls like gpio_get_value(gpio), the offset
  69 * is calculated by subtracting @base from the gpio number.
  70 */
  71struct gpio_chip {
  72        const char              *label;
  73        struct gpio_device      *gpiodev;
  74        struct device           *dev;
  75        struct module           *owner;
  76        void                    *data;
  77
  78        int                     (*request)(struct gpio_chip *chip,
  79                                                unsigned offset);
  80        void                    (*free)(struct gpio_chip *chip,
  81                                                unsigned offset);
  82        int                     (*get_direction)(struct gpio_chip *chip,
  83                                                unsigned offset);
  84        int                     (*direction_input)(struct gpio_chip *chip,
  85                                                unsigned offset);
  86        int                     (*direction_output)(struct gpio_chip *chip,
  87                                                unsigned offset, int value);
  88        int                     (*get)(struct gpio_chip *chip,
  89                                                unsigned offset);
  90        void                    (*set)(struct gpio_chip *chip,
  91                                                unsigned offset, int value);
  92        int                     (*set_config)(struct gpio_chip *chip,
  93                                              unsigned offset,
  94                                              unsigned long config);
  95        int                     (*to_irq)(struct gpio_chip *chip,
  96                                                unsigned offset);
  97
  98        void                    (*dbg_show)(struct seq_file *s,
  99                                                struct gpio_chip *chip);
 100        int                     base;
 101        u16                     ngpio;
 102        struct gpio_desc        *desc;
 103        const char              *const *names;
 104        bool                    can_sleep;
 105        bool                    exported;
 106
 107#ifdef CONFIG_GPIOLIB_IRQCHIP
 108        /*
 109         * With CONFIG_GPIO_IRQCHIP we get an irqchip inside the gpiolib
 110         * to handle IRQs for most practical cases.
 111         */
 112        struct irq_chip         *irqchip;
 113        struct irq_domain       *irqdomain;
 114        unsigned int            irq_base;
 115        irq_flow_handler_t      irq_handler;
 116        unsigned int            irq_default_type;
 117#endif
 118
 119#if defined(CONFIG_OF_GPIO)
 120        /*
 121         * If CONFIG_OF is enabled, then all GPIO controllers described in the
 122         * device tree automatically may have an OF translation
 123         */
 124        struct device_node *of_node;
 125        int of_gpio_n_cells;
 126        int (*of_xlate)(struct gpio_chip *gc,
 127                        const struct of_phandle_args *gpiospec, u32 *flags);
 128#endif
 129#ifdef CONFIG_PINCTRL
 130        /*
 131         * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
 132         * describe the actual pin range which they serve in an SoC. This
 133         * information would be used by pinctrl subsystem to configure
 134         * corresponding pins for gpio usage.
 135         */
 136        struct list_head pin_ranges;
 137#endif
 138};
 139
 140extern const char *gpiochip_is_requested(struct gpio_chip *chip,
 141                        unsigned offset);
 142
 143/* add/remove chips */
 144extern int gpiochip_add_data(struct gpio_chip *chip, void *data);
 145static inline int gpiochip_add(struct gpio_chip *chip)
 146{
 147        return gpiochip_add_data(chip, NULL);
 148}
 149extern int gpiochip_remove(struct gpio_chip *chip);
 150extern struct gpio_chip *gpiochip_find(void *data,
 151                              int (*match)(struct gpio_chip *chip, void *data));
 152
 153/* lock/unlock as IRQ */
 154int gpiod_lock_as_irq(struct gpio_desc *desc);
 155void gpiod_unlock_as_irq(struct gpio_desc *desc);
 156bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset);
 157
 158/* get driver data */
 159static inline void *gpiochip_get_data(struct gpio_chip *chip)
 160{
 161        return chip->data;
 162}
 163
 164enum gpio_lookup_flags {
 165        GPIO_ACTIVE_HIGH = (0 << 0),
 166        GPIO_ACTIVE_LOW = (1 << 0),
 167        GPIO_OPEN_DRAIN = (1 << 1),
 168        GPIO_OPEN_SOURCE = (1 << 2),
 169};
 170
 171/**
 172 * struct gpiod_lookup - lookup table
 173 * @chip_label: name of the chip the GPIO belongs to
 174 * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO
 175 * @con_id: name of the GPIO from the device's point of view
 176 * @idx: index of the GPIO in case several GPIOs share the same name
 177 * @flags: mask of GPIO_* values
 178 *
 179 * gpiod_lookup is a lookup table for associating GPIOs to specific devices and
 180 * functions using platform data.
 181 */
 182struct gpiod_lookup {
 183        const char *chip_label;
 184        u16 chip_hwnum;
 185        const char *con_id;
 186        unsigned int idx;
 187        enum gpio_lookup_flags flags;
 188};
 189
 190struct gpiod_lookup_table {
 191        struct list_head list;
 192        const char *dev_id;
 193        struct gpiod_lookup table[];
 194};
 195
 196/*
 197 * Simple definition of a single GPIO under a con_id
 198 */
 199#define GPIO_LOOKUP(_chip_label, _chip_hwnum, _con_id, _flags) \
 200        GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, 0, _flags)
 201
 202/*
 203 * Use this macro if you need to have several GPIOs under the same con_id.
 204 * Each GPIO needs to use a different index and can be accessed using
 205 * gpiod_get_index()
 206 */
 207#define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, _idx, _flags)  \
 208{                                                                         \
 209        .chip_label = _chip_label,                                        \
 210        .chip_hwnum = _chip_hwnum,                                        \
 211        .con_id = _con_id,                                                \
 212        .idx = _idx,                                                      \
 213        .flags = _flags,                                                  \
 214}
 215
 216void gpiod_add_lookup_table(struct gpiod_lookup_table *table);
 217
 218#ifdef CONFIG_GPIOLIB_IRQCHIP
 219
 220void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
 221                struct irq_chip *irqchip,
 222                int parent_irq,
 223                irq_flow_handler_t parent_handler);
 224
 225int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
 226                struct irq_chip *irqchip,
 227                unsigned int first_irq,
 228                irq_flow_handler_t handler,
 229                unsigned int type);
 230
 231#endif /* CONFIG_GPIO_IRQCHIP */
 232
 233int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
 234                            unsigned long config);
 235
 236#endif
 237