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/lockdep.h>
11#include <linux/pinctrl/pinctrl.h>
12#include <linux/kconfig.h>
13
14struct gpio_desc;
15struct of_phandle_args;
16struct device_node;
17struct seq_file;
18struct gpio_device;
19
20#ifdef CONFIG_GPIOLIB
21
22
23
24
25
26
27
28enum single_ended_mode {
29 LINE_MODE_PUSH_PULL,
30 LINE_MODE_OPEN_DRAIN,
31 LINE_MODE_OPEN_SOURCE,
32};
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127struct gpio_chip {
128 const char *label;
129 struct gpio_device *gpiodev;
130 struct device *parent;
131 struct module *owner;
132
133 int (*request)(struct gpio_chip *chip,
134 unsigned offset);
135 void (*free)(struct gpio_chip *chip,
136 unsigned offset);
137 int (*get_direction)(struct gpio_chip *chip,
138 unsigned offset);
139 int (*direction_input)(struct gpio_chip *chip,
140 unsigned offset);
141 int (*direction_output)(struct gpio_chip *chip,
142 unsigned offset, int value);
143 int (*get)(struct gpio_chip *chip,
144 unsigned offset);
145 void (*set)(struct gpio_chip *chip,
146 unsigned offset, int value);
147 void (*set_multiple)(struct gpio_chip *chip,
148 unsigned long *mask,
149 unsigned long *bits);
150 int (*set_debounce)(struct gpio_chip *chip,
151 unsigned offset,
152 unsigned debounce);
153 int (*set_single_ended)(struct gpio_chip *chip,
154 unsigned offset,
155 enum single_ended_mode mode);
156
157 int (*to_irq)(struct gpio_chip *chip,
158 unsigned offset);
159
160 void (*dbg_show)(struct seq_file *s,
161 struct gpio_chip *chip);
162 int base;
163 u16 ngpio;
164 const char *const *names;
165 bool can_sleep;
166 bool irq_not_threaded;
167
168#if IS_ENABLED(CONFIG_GPIO_GENERIC)
169 unsigned long (*read_reg)(void __iomem *reg);
170 void (*write_reg)(void __iomem *reg, unsigned long data);
171 unsigned long (*pin2mask)(struct gpio_chip *gc, unsigned int pin);
172 void __iomem *reg_dat;
173 void __iomem *reg_set;
174 void __iomem *reg_clr;
175 void __iomem *reg_dir;
176 int bgpio_bits;
177 spinlock_t bgpio_lock;
178 unsigned long bgpio_data;
179 unsigned long bgpio_dir;
180#endif
181
182#ifdef CONFIG_GPIOLIB_IRQCHIP
183
184
185
186
187 struct irq_chip *irqchip;
188 struct irq_domain *irqdomain;
189 unsigned int irq_base;
190 irq_flow_handler_t irq_handler;
191 unsigned int irq_default_type;
192 int irq_parent;
193 struct lock_class_key *lock_key;
194#endif
195
196#if defined(CONFIG_OF_GPIO)
197
198
199
200
201 struct device_node *of_node;
202 int of_gpio_n_cells;
203 int (*of_xlate)(struct gpio_chip *gc,
204 const struct of_phandle_args *gpiospec, u32 *flags);
205#endif
206};
207
208extern const char *gpiochip_is_requested(struct gpio_chip *chip,
209 unsigned offset);
210
211
212extern int gpiochip_add_data(struct gpio_chip *chip, void *data);
213static inline int gpiochip_add(struct gpio_chip *chip)
214{
215 return gpiochip_add_data(chip, NULL);
216}
217extern void gpiochip_remove(struct gpio_chip *chip);
218extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
219 void *data);
220extern void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip);
221
222extern struct gpio_chip *gpiochip_find(void *data,
223 int (*match)(struct gpio_chip *chip, void *data));
224
225
226int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
227void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
228bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset);
229
230
231bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset);
232bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset);
233
234
235void *gpiochip_get_data(struct gpio_chip *chip);
236
237struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
238
239struct bgpio_pdata {
240 const char *label;
241 int base;
242 int ngpio;
243};
244
245#if IS_ENABLED(CONFIG_GPIO_GENERIC)
246
247int bgpio_init(struct gpio_chip *gc, struct device *dev,
248 unsigned long sz, void __iomem *dat, void __iomem *set,
249 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
250 unsigned long flags);
251
252#define BGPIOF_BIG_ENDIAN BIT(0)
253#define BGPIOF_UNREADABLE_REG_SET BIT(1)
254#define BGPIOF_UNREADABLE_REG_DIR BIT(2)
255#define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3)
256#define BGPIOF_READ_OUTPUT_REG_SET BIT(4)
257#define BGPIOF_NO_OUTPUT BIT(5)
258
259#endif
260
261#ifdef CONFIG_GPIOLIB_IRQCHIP
262
263void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
264 struct irq_chip *irqchip,
265 int parent_irq,
266 irq_flow_handler_t parent_handler);
267
268int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
269 struct irq_chip *irqchip,
270 unsigned int first_irq,
271 irq_flow_handler_t handler,
272 unsigned int type,
273 struct lock_class_key *lock_key);
274
275#ifdef CONFIG_LOCKDEP
276#define gpiochip_irqchip_add(...) \
277( \
278 ({ \
279 static struct lock_class_key _key; \
280 _gpiochip_irqchip_add(__VA_ARGS__, &_key); \
281 }) \
282)
283#else
284#define gpiochip_irqchip_add(...) \
285 _gpiochip_irqchip_add(__VA_ARGS__, NULL)
286#endif
287
288#endif
289
290int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset);
291void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset);
292
293#ifdef CONFIG_PINCTRL
294
295
296
297
298
299
300
301
302struct gpio_pin_range {
303 struct list_head node;
304 struct pinctrl_dev *pctldev;
305 struct pinctrl_gpio_range range;
306};
307
308int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
309 unsigned int gpio_offset, unsigned int pin_offset,
310 unsigned int npins);
311int gpiochip_add_pingroup_range(struct gpio_chip *chip,
312 struct pinctrl_dev *pctldev,
313 unsigned int gpio_offset, const char *pin_group);
314void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
315
316#else
317
318static inline int
319gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
320 unsigned int gpio_offset, unsigned int pin_offset,
321 unsigned int npins)
322{
323 return 0;
324}
325static inline int
326gpiochip_add_pingroup_range(struct gpio_chip *chip,
327 struct pinctrl_dev *pctldev,
328 unsigned int gpio_offset, const char *pin_group)
329{
330 return 0;
331}
332
333static inline void
334gpiochip_remove_pin_ranges(struct gpio_chip *chip)
335{
336}
337
338#endif
339
340struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
341 const char *label);
342void gpiochip_free_own_desc(struct gpio_desc *desc);
343
344#else
345
346static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
347{
348
349 WARN_ON(1);
350 return ERR_PTR(-ENODEV);
351}
352
353#endif
354
355#endif
356