Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_GPIO_DRIVER_H |
| 2 | #define __LINUX_GPIO_DRIVER_H |
| 3 | |
| 4 | #include <linux/types.h> |
Alexandre Courbot | c9a9972 | 2013-11-25 18:34:24 +0900 | [diff] [blame] | 5 | #include <linux/module.h> |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 6 | #include <linux/irq.h> |
| 7 | #include <linux/irqchip/chained_irq.h> |
| 8 | #include <linux/irqdomain.h> |
Linus Walleij | 964cb34 | 2015-03-18 01:56:17 +0100 | [diff] [blame] | 9 | #include <linux/pinctrl/pinctrl.h> |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 10 | |
| 11 | struct device; |
| 12 | struct gpio_desc; |
Alexandre Courbot | c9a9972 | 2013-11-25 18:34:24 +0900 | [diff] [blame] | 13 | struct of_phandle_args; |
| 14 | struct device_node; |
Stephen Rothwell | f3ed0b6 | 2013-10-29 01:06:23 +1100 | [diff] [blame] | 15 | struct seq_file; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 16 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 17 | #ifdef CONFIG_GPIOLIB |
| 18 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 19 | /** |
| 20 | * struct gpio_chip - abstract a GPIO controller |
| 21 | * @label: for diagnostics |
| 22 | * @dev: optional device providing the GPIOs |
| 23 | * @owner: helps prevent removal of modules exporting active GPIOs |
| 24 | * @list: links gpio_chips together for traversal |
| 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" |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 36 | * @set_multiple: assigns output values for multiple signals defined by "mask" |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 37 | * @set_debounce: optional hook for setting debounce time for specified gpio in |
| 38 | * interrupt triggered gpio chips |
| 39 | * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; |
| 40 | * implementation may not sleep |
| 41 | * @dbg_show: optional routine to show contents in debugfs; default code |
| 42 | * will be used when this is omitted, but custom code can show extra |
| 43 | * state (such as pullup/pulldown configuration). |
| 44 | * @base: identifies the first GPIO number handled by this chip; or, if |
| 45 | * negative during registration, requests dynamic ID allocation. |
| 46 | * @ngpio: the number of GPIOs handled by this controller; the last GPIO |
| 47 | * handled is (base + ngpio - 1). |
| 48 | * @desc: array of ngpio descriptors. Private. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 49 | * @names: if set, must be an array of strings to use as alternative |
| 50 | * names for the GPIOs in this chip. Any entry in the array |
| 51 | * may be NULL if there is no alias for the GPIO, however the |
| 52 | * array must be @ngpio entries long. A name can include a single printk |
| 53 | * format specifier for an unsigned int. It is substituted by the actual |
| 54 | * number of the gpio. |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 55 | * @can_sleep: flag must be set iff get()/set() methods sleep, as they |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 56 | * must while accessing GPIO expander chips over I2C or SPI. This |
| 57 | * implies that if the chip supports IRQs, these IRQs need to be threaded |
| 58 | * as the chip access may sleep when e.g. reading out the IRQ status |
| 59 | * registers. |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 60 | * @exported: flags if the gpiochip is exported for use from sysfs. Private. |
Octavian Purdila | 295494a | 2014-09-19 23:22:44 +0300 | [diff] [blame] | 61 | * @irq_not_threaded: flag must be set if @can_sleep is set but the |
| 62 | * IRQs don't need to be threaded |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 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 | */ |
| 74 | struct gpio_chip { |
| 75 | const char *label; |
| 76 | struct device *dev; |
| 77 | struct module *owner; |
| 78 | struct list_head list; |
| 79 | |
| 80 | int (*request)(struct gpio_chip *chip, |
| 81 | unsigned offset); |
| 82 | void (*free)(struct gpio_chip *chip, |
| 83 | unsigned offset); |
| 84 | int (*get_direction)(struct gpio_chip *chip, |
| 85 | unsigned offset); |
| 86 | int (*direction_input)(struct gpio_chip *chip, |
| 87 | unsigned offset); |
| 88 | int (*direction_output)(struct gpio_chip *chip, |
| 89 | unsigned offset, int value); |
| 90 | int (*get)(struct gpio_chip *chip, |
| 91 | unsigned offset); |
| 92 | void (*set)(struct gpio_chip *chip, |
| 93 | unsigned offset, int value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 94 | void (*set_multiple)(struct gpio_chip *chip, |
| 95 | unsigned long *mask, |
| 96 | unsigned long *bits); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 97 | int (*set_debounce)(struct gpio_chip *chip, |
| 98 | unsigned offset, |
| 99 | unsigned debounce); |
| 100 | |
| 101 | int (*to_irq)(struct gpio_chip *chip, |
| 102 | unsigned offset); |
| 103 | |
| 104 | void (*dbg_show)(struct seq_file *s, |
| 105 | struct gpio_chip *chip); |
| 106 | int base; |
| 107 | u16 ngpio; |
| 108 | struct gpio_desc *desc; |
| 109 | const char *const *names; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 110 | bool can_sleep; |
Octavian Purdila | 295494a | 2014-09-19 23:22:44 +0300 | [diff] [blame] | 111 | bool irq_not_threaded; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 112 | bool exported; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 113 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 114 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 115 | /* |
Paul Bolle | 7d75a87 | 2014-09-05 13:09:25 +0200 | [diff] [blame] | 116 | * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 117 | * to handle IRQs for most practical cases. |
| 118 | */ |
| 119 | struct irq_chip *irqchip; |
| 120 | struct irq_domain *irqdomain; |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 121 | unsigned int irq_base; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 122 | irq_flow_handler_t irq_handler; |
| 123 | unsigned int irq_default_type; |
| 124 | #endif |
| 125 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 126 | #if defined(CONFIG_OF_GPIO) |
| 127 | /* |
| 128 | * If CONFIG_OF is enabled, then all GPIO controllers described in the |
| 129 | * device tree automatically may have an OF translation |
| 130 | */ |
| 131 | struct device_node *of_node; |
| 132 | int of_gpio_n_cells; |
| 133 | int (*of_xlate)(struct gpio_chip *gc, |
| 134 | const struct of_phandle_args *gpiospec, u32 *flags); |
| 135 | #endif |
| 136 | #ifdef CONFIG_PINCTRL |
| 137 | /* |
| 138 | * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally |
| 139 | * describe the actual pin range which they serve in an SoC. This |
| 140 | * information would be used by pinctrl subsystem to configure |
| 141 | * corresponding pins for gpio usage. |
| 142 | */ |
| 143 | struct list_head pin_ranges; |
| 144 | #endif |
| 145 | }; |
| 146 | |
| 147 | extern const char *gpiochip_is_requested(struct gpio_chip *chip, |
| 148 | unsigned offset); |
| 149 | |
| 150 | /* add/remove chips */ |
| 151 | extern int gpiochip_add(struct gpio_chip *chip); |
abdoulaye berthe | e1db170 | 2014-07-05 18:28:50 +0200 | [diff] [blame] | 152 | extern void gpiochip_remove(struct gpio_chip *chip); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 153 | extern struct gpio_chip *gpiochip_find(void *data, |
| 154 | int (*match)(struct gpio_chip *chip, void *data)); |
| 155 | |
| 156 | /* lock/unlock as IRQ */ |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 157 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset); |
| 158 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 159 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 160 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc); |
| 161 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 162 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 163 | |
| 164 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, |
| 165 | struct irq_chip *irqchip, |
| 166 | int parent_irq, |
| 167 | irq_flow_handler_t parent_handler); |
| 168 | |
| 169 | int gpiochip_irqchip_add(struct gpio_chip *gpiochip, |
| 170 | struct irq_chip *irqchip, |
| 171 | unsigned int first_irq, |
| 172 | irq_flow_handler_t handler, |
| 173 | unsigned int type); |
| 174 | |
Paul Bolle | 7d75a87 | 2014-09-05 13:09:25 +0200 | [diff] [blame] | 175 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 176 | |
Linus Walleij | 964cb34 | 2015-03-18 01:56:17 +0100 | [diff] [blame] | 177 | #ifdef CONFIG_PINCTRL |
| 178 | |
| 179 | /** |
| 180 | * struct gpio_pin_range - pin range controlled by a gpio chip |
| 181 | * @head: list for maintaining set of pin ranges, used internally |
| 182 | * @pctldev: pinctrl device which handles corresponding pins |
| 183 | * @range: actual range of pins controlled by a gpio controller |
| 184 | */ |
| 185 | |
| 186 | struct gpio_pin_range { |
| 187 | struct list_head node; |
| 188 | struct pinctrl_dev *pctldev; |
| 189 | struct pinctrl_gpio_range range; |
| 190 | }; |
| 191 | |
| 192 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
| 193 | unsigned int gpio_offset, unsigned int pin_offset, |
| 194 | unsigned int npins); |
| 195 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 196 | struct pinctrl_dev *pctldev, |
| 197 | unsigned int gpio_offset, const char *pin_group); |
| 198 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip); |
| 199 | |
| 200 | #else |
| 201 | |
| 202 | static inline int |
| 203 | gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
| 204 | unsigned int gpio_offset, unsigned int pin_offset, |
| 205 | unsigned int npins) |
| 206 | { |
| 207 | return 0; |
| 208 | } |
| 209 | static inline int |
| 210 | gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 211 | struct pinctrl_dev *pctldev, |
| 212 | unsigned int gpio_offset, const char *pin_group) |
| 213 | { |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static inline void |
| 218 | gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 219 | { |
| 220 | } |
| 221 | |
| 222 | #endif /* CONFIG_PINCTRL */ |
| 223 | |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 224 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
| 225 | const char *label); |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 226 | void gpiochip_free_own_desc(struct gpio_desc *desc); |
| 227 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 228 | #else /* CONFIG_GPIOLIB */ |
| 229 | |
| 230 | static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
| 231 | { |
| 232 | /* GPIO can never have been requested */ |
| 233 | WARN_ON(1); |
| 234 | return ERR_PTR(-ENODEV); |
| 235 | } |
| 236 | |
| 237 | #endif /* CONFIG_GPIOLIB */ |
| 238 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 239 | #endif |