blob: 96a678842cdeef7e0d4200329565351e20278410 [file] [log] [blame]
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001#ifndef __LINUX_GPIO_DRIVER_H
2#define __LINUX_GPIO_DRIVER_H
3
4#include <linux/types.h>
Alexandre Courbotc9a99722013-11-25 18:34:24 +09005#include <linux/module.h>
Linus Walleij14250522014-03-25 10:40:18 +01006#include <linux/irq.h>
7#include <linux/irqchip/chained_irq.h>
8#include <linux/irqdomain.h>
Linus Walleij964cb342015-03-18 01:56:17 +01009#include <linux/pinctrl/pinctrl.h>
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070010
11struct device;
12struct gpio_desc;
Alexandre Courbotc9a99722013-11-25 18:34:24 +090013struct of_phandle_args;
14struct device_node;
Stephen Rothwellf3ed0b62013-10-29 01:06:23 +110015struct seq_file;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070016
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090017#ifdef CONFIG_GPIOLIB
18
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070019/**
20 * struct gpio_chip - abstract a GPIO controller
21 * @label: for diagnostics
22 * @dev: optional device providing the GPIOs
Johan Hovold6a4b6b02015-05-04 17:10:31 +020023 * @cdev: class device used by sysfs interface (may be NULL)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070024 * @owner: helps prevent removal of modules exporting active GPIOs
25 * @list: links gpio_chips together for traversal
26 * @request: optional hook for chip-specific activation, such as
27 * enabling module power and clock; may sleep
28 * @free: optional hook for chip-specific deactivation, such as
29 * disabling module power and clock; may sleep
30 * @get_direction: returns direction for signal "offset", 0=out, 1=in,
31 * (same as GPIOF_DIR_XXX), or negative error
32 * @direction_input: configures signal "offset" as input, or returns error
33 * @direction_output: configures signal "offset" as output, or returns error
34 * @get: returns value for signal "offset"; for output signals this
35 * returns either the value actually sensed, or zero
36 * @set: assigns output value for signal "offset"
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +010037 * @set_multiple: assigns output values for multiple signals defined by "mask"
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038 * @set_debounce: optional hook for setting debounce time for specified gpio in
39 * interrupt triggered gpio chips
40 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
41 * implementation may not sleep
42 * @dbg_show: optional routine to show contents in debugfs; default code
43 * will be used when this is omitted, but custom code can show extra
44 * state (such as pullup/pulldown configuration).
45 * @base: identifies the first GPIO number handled by this chip; or, if
46 * negative during registration, requests dynamic ID allocation.
47 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
48 * handled is (base + ngpio - 1).
49 * @desc: array of ngpio descriptors. Private.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070050 * @names: if set, must be an array of strings to use as alternative
51 * names for the GPIOs in this chip. Any entry in the array
52 * may be NULL if there is no alias for the GPIO, however the
53 * array must be @ngpio entries long. A name can include a single printk
54 * format specifier for an unsigned int. It is substituted by the actual
55 * number of the gpio.
Linus Walleij9fb1f392013-12-04 14:42:46 +010056 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
Linus Walleij1c8732b2014-04-09 13:34:39 +020057 * must while accessing GPIO expander chips over I2C or SPI. This
58 * implies that if the chip supports IRQs, these IRQs need to be threaded
59 * as the chip access may sleep when e.g. reading out the IRQ status
60 * registers.
Octavian Purdila295494a2014-09-19 23:22:44 +030061 * @irq_not_threaded: flag must be set if @can_sleep is set but the
62 * IRQs don't need to be threaded
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070063 *
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;
Johan Hovold6a4b6b02015-05-04 17:10:31 +020077 struct device *cdev;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070078 struct module *owner;
79 struct list_head list;
80
81 int (*request)(struct gpio_chip *chip,
82 unsigned offset);
83 void (*free)(struct gpio_chip *chip,
84 unsigned offset);
85 int (*get_direction)(struct gpio_chip *chip,
86 unsigned offset);
87 int (*direction_input)(struct gpio_chip *chip,
88 unsigned offset);
89 int (*direction_output)(struct gpio_chip *chip,
90 unsigned offset, int value);
91 int (*get)(struct gpio_chip *chip,
92 unsigned offset);
93 void (*set)(struct gpio_chip *chip,
94 unsigned offset, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +010095 void (*set_multiple)(struct gpio_chip *chip,
96 unsigned long *mask,
97 unsigned long *bits);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070098 int (*set_debounce)(struct gpio_chip *chip,
99 unsigned offset,
100 unsigned debounce);
101
102 int (*to_irq)(struct gpio_chip *chip,
103 unsigned offset);
104
105 void (*dbg_show)(struct seq_file *s,
106 struct gpio_chip *chip);
107 int base;
108 u16 ngpio;
109 struct gpio_desc *desc;
110 const char *const *names;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100111 bool can_sleep;
Octavian Purdila295494a2014-09-19 23:22:44 +0300112 bool irq_not_threaded;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700113
Linus Walleij14250522014-03-25 10:40:18 +0100114#ifdef CONFIG_GPIOLIB_IRQCHIP
115 /*
Paul Bolle7d75a872014-09-05 13:09:25 +0200116 * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
Linus Walleij14250522014-03-25 10:40:18 +0100117 * to handle IRQs for most practical cases.
118 */
119 struct irq_chip *irqchip;
120 struct irq_domain *irqdomain;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100121 unsigned int irq_base;
Linus Walleij14250522014-03-25 10:40:18 +0100122 irq_flow_handler_t irq_handler;
123 unsigned int irq_default_type;
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300124 int irq_parent;
Linus Walleij14250522014-03-25 10:40:18 +0100125#endif
126
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700127#if defined(CONFIG_OF_GPIO)
128 /*
129 * If CONFIG_OF is enabled, then all GPIO controllers described in the
130 * device tree automatically may have an OF translation
131 */
132 struct device_node *of_node;
133 int of_gpio_n_cells;
134 int (*of_xlate)(struct gpio_chip *gc,
135 const struct of_phandle_args *gpiospec, u32 *flags);
136#endif
137#ifdef CONFIG_PINCTRL
138 /*
139 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
140 * describe the actual pin range which they serve in an SoC. This
141 * information would be used by pinctrl subsystem to configure
142 * corresponding pins for gpio usage.
143 */
144 struct list_head pin_ranges;
145#endif
146};
147
148extern const char *gpiochip_is_requested(struct gpio_chip *chip,
149 unsigned offset);
150
151/* add/remove chips */
152extern int gpiochip_add(struct gpio_chip *chip);
abdoulaye berthee1db1702014-07-05 18:28:50 +0200153extern void gpiochip_remove(struct gpio_chip *chip);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700154extern struct gpio_chip *gpiochip_find(void *data,
155 int (*match)(struct gpio_chip *chip, void *data));
156
157/* lock/unlock as IRQ */
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900158int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
159void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700160
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900161struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
162
Linus Walleij14250522014-03-25 10:40:18 +0100163#ifdef CONFIG_GPIOLIB_IRQCHIP
164
165void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
166 struct irq_chip *irqchip,
167 int parent_irq,
168 irq_flow_handler_t parent_handler);
169
170int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
171 struct irq_chip *irqchip,
172 unsigned int first_irq,
173 irq_flow_handler_t handler,
174 unsigned int type);
175
Paul Bolle7d75a872014-09-05 13:09:25 +0200176#endif /* CONFIG_GPIOLIB_IRQCHIP */
Linus Walleij14250522014-03-25 10:40:18 +0100177
Linus Walleij964cb342015-03-18 01:56:17 +0100178#ifdef CONFIG_PINCTRL
179
180/**
181 * struct gpio_pin_range - pin range controlled by a gpio chip
182 * @head: list for maintaining set of pin ranges, used internally
183 * @pctldev: pinctrl device which handles corresponding pins
184 * @range: actual range of pins controlled by a gpio controller
185 */
186
187struct gpio_pin_range {
188 struct list_head node;
189 struct pinctrl_dev *pctldev;
190 struct pinctrl_gpio_range range;
191};
192
193int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
194 unsigned int gpio_offset, unsigned int pin_offset,
195 unsigned int npins);
196int gpiochip_add_pingroup_range(struct gpio_chip *chip,
197 struct pinctrl_dev *pctldev,
198 unsigned int gpio_offset, const char *pin_group);
199void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
200
201#else
202
203static inline int
204gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
205 unsigned int gpio_offset, unsigned int pin_offset,
206 unsigned int npins)
207{
208 return 0;
209}
210static inline int
211gpiochip_add_pingroup_range(struct gpio_chip *chip,
212 struct pinctrl_dev *pctldev,
213 unsigned int gpio_offset, const char *pin_group)
214{
215 return 0;
216}
217
218static inline void
219gpiochip_remove_pin_ranges(struct gpio_chip *chip)
220{
221}
222
223#endif /* CONFIG_PINCTRL */
224
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700225struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
226 const char *label);
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700227void gpiochip_free_own_desc(struct gpio_desc *desc);
228
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900229#else /* CONFIG_GPIOLIB */
230
231static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
232{
233 /* GPIO can never have been requested */
234 WARN_ON(1);
235 return ERR_PTR(-ENODEV);
236}
237
238#endif /* CONFIG_GPIOLIB */
239
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700240#endif