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> |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 9 | #include <linux/lockdep.h> |
Linus Walleij | 964cb34 | 2015-03-18 01:56:17 +0100 | [diff] [blame] | 10 | #include <linux/pinctrl/pinctrl.h> |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 11 | |
| 12 | struct device; |
| 13 | struct gpio_desc; |
Alexandre Courbot | c9a9972 | 2013-11-25 18:34:24 +0900 | [diff] [blame] | 14 | struct of_phandle_args; |
| 15 | struct device_node; |
Stephen Rothwell | f3ed0b6 | 2013-10-29 01:06:23 +1100 | [diff] [blame] | 16 | struct seq_file; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 17 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 18 | #ifdef CONFIG_GPIOLIB |
| 19 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 20 | /** |
| 21 | * struct gpio_chip - abstract a GPIO controller |
| 22 | * @label: for diagnostics |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 23 | * @parent: optional parent device providing the GPIOs |
Johan Hovold | 6a4b6b0 | 2015-05-04 17:10:31 +0200 | [diff] [blame] | 24 | * @cdev: class device used by sysfs interface (may be NULL) |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 25 | * @owner: helps prevent removal of modules exporting active GPIOs |
Linus Walleij | b08ea35 | 2015-12-03 15:14:13 +0100 | [diff] [blame^] | 26 | * @data: per-instance data assigned by the driver |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 27 | * @list: links gpio_chips together for traversal |
| 28 | * @request: optional hook for chip-specific activation, such as |
| 29 | * enabling module power and clock; may sleep |
| 30 | * @free: optional hook for chip-specific deactivation, such as |
| 31 | * disabling module power and clock; may sleep |
| 32 | * @get_direction: returns direction for signal "offset", 0=out, 1=in, |
| 33 | * (same as GPIOF_DIR_XXX), or negative error |
| 34 | * @direction_input: configures signal "offset" as input, or returns error |
| 35 | * @direction_output: configures signal "offset" as output, or returns error |
Vladimir Zapolskiy | 60befd2 | 2015-12-22 16:37:28 +0200 | [diff] [blame] | 36 | * @get: returns value for signal "offset", 0=low, 1=high, or negative error |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 37 | * @set: assigns output value for signal "offset" |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 38 | * @set_multiple: assigns output values for multiple signals defined by "mask" |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 39 | * @set_debounce: optional hook for setting debounce time for specified gpio in |
| 40 | * interrupt triggered gpio chips |
| 41 | * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; |
| 42 | * implementation may not sleep |
| 43 | * @dbg_show: optional routine to show contents in debugfs; default code |
| 44 | * will be used when this is omitted, but custom code can show extra |
| 45 | * state (such as pullup/pulldown configuration). |
Linus Walleij | af6c235 | 2015-05-13 13:03:21 +0200 | [diff] [blame] | 46 | * @base: identifies the first GPIO number handled by this chip; |
| 47 | * or, if negative during registration, requests dynamic ID allocation. |
| 48 | * DEPRECATION: providing anything non-negative and nailing the base |
Geert Uytterhoeven | 30bb6fb | 2015-06-15 13:31:33 +0200 | [diff] [blame] | 49 | * offset of GPIO chips is deprecated. Please pass -1 as base to |
Linus Walleij | af6c235 | 2015-05-13 13:03:21 +0200 | [diff] [blame] | 50 | * let gpiolib select the chip base in all possible cases. We want to |
| 51 | * get rid of the static GPIO number space in the long run. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 52 | * @ngpio: the number of GPIOs handled by this controller; the last GPIO |
| 53 | * handled is (base + ngpio - 1). |
| 54 | * @desc: array of ngpio descriptors. Private. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 55 | * @names: if set, must be an array of strings to use as alternative |
| 56 | * names for the GPIOs in this chip. Any entry in the array |
| 57 | * may be NULL if there is no alias for the GPIO, however the |
| 58 | * array must be @ngpio entries long. A name can include a single printk |
| 59 | * format specifier for an unsigned int. It is substituted by the actual |
| 60 | * number of the gpio. |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 61 | * @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] | 62 | * must while accessing GPIO expander chips over I2C or SPI. This |
| 63 | * implies that if the chip supports IRQs, these IRQs need to be threaded |
| 64 | * as the chip access may sleep when e.g. reading out the IRQ status |
| 65 | * registers. |
Octavian Purdila | 295494a | 2014-09-19 23:22:44 +0300 | [diff] [blame] | 66 | * @irq_not_threaded: flag must be set if @can_sleep is set but the |
| 67 | * IRQs don't need to be threaded |
Grygorii Strashko | 41d6bb4 | 2015-08-17 15:35:24 +0300 | [diff] [blame] | 68 | * @irqchip: GPIO IRQ chip impl, provided by GPIO driver |
| 69 | * @irqdomain: Interrupt translation domain; responsible for mapping |
| 70 | * between GPIO hwirq number and linux irq number |
| 71 | * @irq_base: first linux IRQ number assigned to GPIO IRQ chip (deprecated) |
| 72 | * @irq_handler: the irq handler to use (often a predefined irq core function) |
| 73 | * for GPIO IRQs, provided by GPIO driver |
| 74 | * @irq_default_type: default IRQ triggering type applied during GPIO driver |
| 75 | * initialization, provided by GPIO driver |
| 76 | * @irq_parent: GPIO IRQ chip parent/bank linux irq number, |
| 77 | * provided by GPIO driver |
| 78 | * @lock_key: per GPIO IRQ chip lockdep class |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 79 | * |
| 80 | * A gpio_chip can help platforms abstract various sources of GPIOs so |
| 81 | * they can all be accessed through a common programing interface. |
| 82 | * Example sources would be SOC controllers, FPGAs, multifunction |
| 83 | * chips, dedicated GPIO expanders, and so on. |
| 84 | * |
| 85 | * Each chip controls a number of signals, identified in method calls |
| 86 | * by "offset" values in the range 0..(@ngpio - 1). When those signals |
| 87 | * are referenced through calls like gpio_get_value(gpio), the offset |
| 88 | * is calculated by subtracting @base from the gpio number. |
| 89 | */ |
| 90 | struct gpio_chip { |
| 91 | const char *label; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 92 | struct device *parent; |
Johan Hovold | 6a4b6b0 | 2015-05-04 17:10:31 +0200 | [diff] [blame] | 93 | struct device *cdev; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 94 | struct module *owner; |
Linus Walleij | b08ea35 | 2015-12-03 15:14:13 +0100 | [diff] [blame^] | 95 | void *data; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 96 | struct list_head list; |
| 97 | |
| 98 | int (*request)(struct gpio_chip *chip, |
| 99 | unsigned offset); |
| 100 | void (*free)(struct gpio_chip *chip, |
| 101 | unsigned offset); |
| 102 | int (*get_direction)(struct gpio_chip *chip, |
| 103 | unsigned offset); |
| 104 | int (*direction_input)(struct gpio_chip *chip, |
| 105 | unsigned offset); |
| 106 | int (*direction_output)(struct gpio_chip *chip, |
| 107 | unsigned offset, int value); |
| 108 | int (*get)(struct gpio_chip *chip, |
| 109 | unsigned offset); |
| 110 | void (*set)(struct gpio_chip *chip, |
| 111 | unsigned offset, int value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 112 | void (*set_multiple)(struct gpio_chip *chip, |
| 113 | unsigned long *mask, |
| 114 | unsigned long *bits); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 115 | int (*set_debounce)(struct gpio_chip *chip, |
| 116 | unsigned offset, |
| 117 | unsigned debounce); |
| 118 | |
| 119 | int (*to_irq)(struct gpio_chip *chip, |
| 120 | unsigned offset); |
| 121 | |
| 122 | void (*dbg_show)(struct seq_file *s, |
| 123 | struct gpio_chip *chip); |
| 124 | int base; |
| 125 | u16 ngpio; |
| 126 | struct gpio_desc *desc; |
| 127 | const char *const *names; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 128 | bool can_sleep; |
Octavian Purdila | 295494a | 2014-09-19 23:22:44 +0300 | [diff] [blame] | 129 | bool irq_not_threaded; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 130 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 131 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 132 | /* |
Paul Bolle | 7d75a87 | 2014-09-05 13:09:25 +0200 | [diff] [blame] | 133 | * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 134 | * to handle IRQs for most practical cases. |
| 135 | */ |
| 136 | struct irq_chip *irqchip; |
| 137 | struct irq_domain *irqdomain; |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 138 | unsigned int irq_base; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 139 | irq_flow_handler_t irq_handler; |
| 140 | unsigned int irq_default_type; |
Dmitry Eremin-Solenikov | 25e4fe9 | 2015-05-12 20:12:23 +0300 | [diff] [blame] | 141 | int irq_parent; |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 142 | struct lock_class_key *lock_key; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 143 | #endif |
| 144 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 145 | #if defined(CONFIG_OF_GPIO) |
| 146 | /* |
| 147 | * If CONFIG_OF is enabled, then all GPIO controllers described in the |
| 148 | * device tree automatically may have an OF translation |
| 149 | */ |
| 150 | struct device_node *of_node; |
| 151 | int of_gpio_n_cells; |
| 152 | int (*of_xlate)(struct gpio_chip *gc, |
| 153 | const struct of_phandle_args *gpiospec, u32 *flags); |
| 154 | #endif |
| 155 | #ifdef CONFIG_PINCTRL |
| 156 | /* |
| 157 | * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally |
| 158 | * describe the actual pin range which they serve in an SoC. This |
| 159 | * information would be used by pinctrl subsystem to configure |
| 160 | * corresponding pins for gpio usage. |
| 161 | */ |
| 162 | struct list_head pin_ranges; |
| 163 | #endif |
| 164 | }; |
| 165 | |
| 166 | extern const char *gpiochip_is_requested(struct gpio_chip *chip, |
| 167 | unsigned offset); |
| 168 | |
| 169 | /* add/remove chips */ |
Linus Walleij | b08ea35 | 2015-12-03 15:14:13 +0100 | [diff] [blame^] | 170 | extern int gpiochip_add_data(struct gpio_chip *chip, void *data); |
| 171 | static inline int gpiochip_add(struct gpio_chip *chip) |
| 172 | { |
| 173 | return gpiochip_add_data(chip, NULL); |
| 174 | } |
abdoulaye berthe | e1db170 | 2014-07-05 18:28:50 +0200 | [diff] [blame] | 175 | extern void gpiochip_remove(struct gpio_chip *chip); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 176 | extern struct gpio_chip *gpiochip_find(void *data, |
| 177 | int (*match)(struct gpio_chip *chip, void *data)); |
| 178 | |
| 179 | /* lock/unlock as IRQ */ |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 180 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset); |
| 181 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 182 | |
Linus Walleij | b08ea35 | 2015-12-03 15:14:13 +0100 | [diff] [blame^] | 183 | /* get driver data */ |
| 184 | static inline void *gpiochip_get_data(struct gpio_chip *chip) |
| 185 | { |
| 186 | return chip->data; |
| 187 | } |
| 188 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 189 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc); |
| 190 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 191 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 192 | |
| 193 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, |
| 194 | struct irq_chip *irqchip, |
| 195 | int parent_irq, |
| 196 | irq_flow_handler_t parent_handler); |
| 197 | |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 198 | int _gpiochip_irqchip_add(struct gpio_chip *gpiochip, |
| 199 | struct irq_chip *irqchip, |
| 200 | unsigned int first_irq, |
| 201 | irq_flow_handler_t handler, |
| 202 | unsigned int type, |
| 203 | struct lock_class_key *lock_key); |
| 204 | |
| 205 | #ifdef CONFIG_LOCKDEP |
| 206 | #define gpiochip_irqchip_add(...) \ |
| 207 | ( \ |
| 208 | ({ \ |
| 209 | static struct lock_class_key _key; \ |
| 210 | _gpiochip_irqchip_add(__VA_ARGS__, &_key); \ |
| 211 | }) \ |
| 212 | ) |
| 213 | #else |
| 214 | #define gpiochip_irqchip_add(...) \ |
| 215 | _gpiochip_irqchip_add(__VA_ARGS__, NULL) |
| 216 | #endif |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 217 | |
Paul Bolle | 7d75a87 | 2014-09-05 13:09:25 +0200 | [diff] [blame] | 218 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 219 | |
Jonas Gorski | c771c2f | 2015-10-11 17:34:15 +0200 | [diff] [blame] | 220 | int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset); |
| 221 | void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset); |
| 222 | |
Linus Walleij | 964cb34 | 2015-03-18 01:56:17 +0100 | [diff] [blame] | 223 | #ifdef CONFIG_PINCTRL |
| 224 | |
| 225 | /** |
| 226 | * struct gpio_pin_range - pin range controlled by a gpio chip |
| 227 | * @head: list for maintaining set of pin ranges, used internally |
| 228 | * @pctldev: pinctrl device which handles corresponding pins |
| 229 | * @range: actual range of pins controlled by a gpio controller |
| 230 | */ |
| 231 | |
| 232 | struct gpio_pin_range { |
| 233 | struct list_head node; |
| 234 | struct pinctrl_dev *pctldev; |
| 235 | struct pinctrl_gpio_range range; |
| 236 | }; |
| 237 | |
| 238 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
| 239 | unsigned int gpio_offset, unsigned int pin_offset, |
| 240 | unsigned int npins); |
| 241 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 242 | struct pinctrl_dev *pctldev, |
| 243 | unsigned int gpio_offset, const char *pin_group); |
| 244 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip); |
| 245 | |
| 246 | #else |
| 247 | |
| 248 | static inline int |
| 249 | gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
| 250 | unsigned int gpio_offset, unsigned int pin_offset, |
| 251 | unsigned int npins) |
| 252 | { |
| 253 | return 0; |
| 254 | } |
| 255 | static inline int |
| 256 | gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 257 | struct pinctrl_dev *pctldev, |
| 258 | unsigned int gpio_offset, const char *pin_group) |
| 259 | { |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static inline void |
| 264 | gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | #endif /* CONFIG_PINCTRL */ |
| 269 | |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 270 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
| 271 | const char *label); |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 272 | void gpiochip_free_own_desc(struct gpio_desc *desc); |
| 273 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 274 | #else /* CONFIG_GPIOLIB */ |
| 275 | |
| 276 | static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
| 277 | { |
| 278 | /* GPIO can never have been requested */ |
| 279 | WARN_ON(1); |
| 280 | return ERR_PTR(-ENODEV); |
| 281 | } |
| 282 | |
| 283 | #endif /* CONFIG_GPIOLIB */ |
| 284 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 285 | #endif |