1
2#ifndef __LINUX_GPIO_DRIVER_H
3#define __LINUX_GPIO_DRIVER_H
4
5#include <linux/device.h>
6#include <linux/types.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/pinctrl/pinconf-generic.h>
13
14struct gpio_desc;
15struct of_phandle_args;
16struct device_node;
17struct seq_file;
18struct gpio_device;
19struct module;
20
21#ifdef CONFIG_GPIOLIB
22
23#ifdef CONFIG_GPIOLIB_IRQCHIP
24
25
26
27struct gpio_irq_chip {
28
29
30
31
32
33 struct irq_chip *chip;
34
35
36
37
38
39
40
41 struct irq_domain *domain;
42
43
44
45
46
47
48 const struct irq_domain_ops *domain_ops;
49
50
51
52
53
54
55
56 irq_flow_handler_t handler;
57
58
59
60
61
62
63
64 unsigned int default_type;
65
66
67
68
69
70
71 struct lock_class_key *lock_key;
72 struct lock_class_key *request_key;
73
74
75
76
77
78
79
80 irq_flow_handler_t parent_handler;
81
82
83
84
85
86
87
88 void *parent_handler_data;
89
90
91
92
93
94
95 unsigned int num_parents;
96
97
98
99
100
101
102
103 unsigned int *parents;
104
105
106
107
108
109
110 unsigned int *map;
111
112
113
114
115
116
117 bool threaded;
118
119
120
121
122
123
124 bool need_valid_mask;
125
126
127
128
129
130
131
132 unsigned long *valid_mask;
133
134
135
136
137
138
139
140 unsigned int first;
141};
142
143static inline struct gpio_irq_chip *to_gpio_irq_chip(struct irq_chip *chip)
144{
145 return container_of(chip, struct gpio_irq_chip, chip);
146}
147#endif
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223struct gpio_chip {
224 const char *label;
225 struct gpio_device *gpiodev;
226 struct device *parent;
227 struct module *owner;
228
229 int (*request)(struct gpio_chip *chip,
230 unsigned offset);
231 void (*free)(struct gpio_chip *chip,
232 unsigned offset);
233 int (*get_direction)(struct gpio_chip *chip,
234 unsigned offset);
235 int (*direction_input)(struct gpio_chip *chip,
236 unsigned offset);
237 int (*direction_output)(struct gpio_chip *chip,
238 unsigned offset, int value);
239 int (*get)(struct gpio_chip *chip,
240 unsigned offset);
241 int (*get_multiple)(struct gpio_chip *chip,
242 unsigned long *mask,
243 unsigned long *bits);
244 void (*set)(struct gpio_chip *chip,
245 unsigned offset, int value);
246 void (*set_multiple)(struct gpio_chip *chip,
247 unsigned long *mask,
248 unsigned long *bits);
249 int (*set_config)(struct gpio_chip *chip,
250 unsigned offset,
251 unsigned long config);
252 int (*to_irq)(struct gpio_chip *chip,
253 unsigned offset);
254
255 void (*dbg_show)(struct seq_file *s,
256 struct gpio_chip *chip);
257 int base;
258 u16 ngpio;
259 const char *const *names;
260 bool can_sleep;
261
262#if IS_ENABLED(CONFIG_GPIO_GENERIC)
263 unsigned long (*read_reg)(void __iomem *reg);
264 void (*write_reg)(void __iomem *reg, unsigned long data);
265 bool be_bits;
266 void __iomem *reg_dat;
267 void __iomem *reg_set;
268 void __iomem *reg_clr;
269 void __iomem *reg_dir;
270 int bgpio_bits;
271 spinlock_t bgpio_lock;
272 unsigned long bgpio_data;
273 unsigned long bgpio_dir;
274#endif
275
276#ifdef CONFIG_GPIOLIB_IRQCHIP
277
278
279
280
281
282
283
284
285
286
287
288 struct gpio_irq_chip irq;
289#endif
290
291#if defined(CONFIG_OF_GPIO)
292
293
294
295
296
297
298
299
300
301
302 struct device_node *of_node;
303
304
305
306
307
308
309 unsigned int of_gpio_n_cells;
310
311
312
313
314
315
316
317 int (*of_xlate)(struct gpio_chip *gc,
318 const struct of_phandle_args *gpiospec, u32 *flags);
319#endif
320};
321
322extern const char *gpiochip_is_requested(struct gpio_chip *chip,
323 unsigned offset);
324
325
326extern int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
327 struct lock_class_key *lock_key,
328 struct lock_class_key *request_key);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353#ifdef CONFIG_LOCKDEP
354#define gpiochip_add_data(chip, data) ({ \
355 static struct lock_class_key lock_key; \
356 static struct lock_class_key request_key; \
357 gpiochip_add_data_with_key(chip, data, &lock_key, \
358 &request_key); \
359 })
360#else
361#define gpiochip_add_data(chip, data) gpiochip_add_data_with_key(chip, data, NULL, NULL)
362#endif
363
364static inline int gpiochip_add(struct gpio_chip *chip)
365{
366 return gpiochip_add_data(chip, NULL);
367}
368extern void gpiochip_remove(struct gpio_chip *chip);
369extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
370 void *data);
371extern void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip);
372
373extern struct gpio_chip *gpiochip_find(void *data,
374 int (*match)(struct gpio_chip *chip, void *data));
375
376
377int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
378void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
379bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset);
380
381
382bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset);
383bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset);
384
385
386bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset);
387
388
389void *gpiochip_get_data(struct gpio_chip *chip);
390
391struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
392
393struct bgpio_pdata {
394 const char *label;
395 int base;
396 int ngpio;
397};
398
399#if IS_ENABLED(CONFIG_GPIO_GENERIC)
400
401int bgpio_init(struct gpio_chip *gc, struct device *dev,
402 unsigned long sz, void __iomem *dat, void __iomem *set,
403 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
404 unsigned long flags);
405
406#define BGPIOF_BIG_ENDIAN BIT(0)
407#define BGPIOF_UNREADABLE_REG_SET BIT(1)
408#define BGPIOF_UNREADABLE_REG_DIR BIT(2)
409#define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3)
410#define BGPIOF_READ_OUTPUT_REG_SET BIT(4)
411#define BGPIOF_NO_OUTPUT BIT(5)
412
413#endif
414
415#ifdef CONFIG_GPIOLIB_IRQCHIP
416
417int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
418 irq_hw_number_t hwirq);
419void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq);
420
421void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
422 struct irq_chip *irqchip,
423 unsigned int parent_irq,
424 irq_flow_handler_t parent_handler);
425
426void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
427 struct irq_chip *irqchip,
428 unsigned int parent_irq);
429
430int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
431 struct irq_chip *irqchip,
432 unsigned int first_irq,
433 irq_flow_handler_t handler,
434 unsigned int type,
435 bool threaded,
436 struct lock_class_key *lock_key,
437 struct lock_class_key *request_key);
438
439bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
440 unsigned int offset);
441
442#ifdef CONFIG_LOCKDEP
443
444
445
446
447
448
449
450static inline int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
451 struct irq_chip *irqchip,
452 unsigned int first_irq,
453 irq_flow_handler_t handler,
454 unsigned int type)
455{
456 static struct lock_class_key lock_key;
457 static struct lock_class_key request_key;
458
459 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
460 handler, type, false,
461 &lock_key, &request_key);
462}
463
464static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gpiochip,
465 struct irq_chip *irqchip,
466 unsigned int first_irq,
467 irq_flow_handler_t handler,
468 unsigned int type)
469{
470
471 static struct lock_class_key lock_key;
472 static struct lock_class_key request_key;
473
474 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
475 handler, type, true,
476 &lock_key, &request_key);
477}
478#else
479static inline int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
480 struct irq_chip *irqchip,
481 unsigned int first_irq,
482 irq_flow_handler_t handler,
483 unsigned int type)
484{
485 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
486 handler, type, false, NULL, NULL);
487}
488
489static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gpiochip,
490 struct irq_chip *irqchip,
491 unsigned int first_irq,
492 irq_flow_handler_t handler,
493 unsigned int type)
494{
495 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
496 handler, type, true, NULL, NULL);
497}
498#endif
499
500#endif
501
502int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset);
503void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset);
504int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
505 unsigned long config);
506
507#ifdef CONFIG_PINCTRL
508
509
510
511
512
513
514
515struct gpio_pin_range {
516 struct list_head node;
517 struct pinctrl_dev *pctldev;
518 struct pinctrl_gpio_range range;
519};
520
521int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
522 unsigned int gpio_offset, unsigned int pin_offset,
523 unsigned int npins);
524int gpiochip_add_pingroup_range(struct gpio_chip *chip,
525 struct pinctrl_dev *pctldev,
526 unsigned int gpio_offset, const char *pin_group);
527void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
528
529#else
530
531static inline int
532gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
533 unsigned int gpio_offset, unsigned int pin_offset,
534 unsigned int npins)
535{
536 return 0;
537}
538static inline int
539gpiochip_add_pingroup_range(struct gpio_chip *chip,
540 struct pinctrl_dev *pctldev,
541 unsigned int gpio_offset, const char *pin_group)
542{
543 return 0;
544}
545
546static inline void
547gpiochip_remove_pin_ranges(struct gpio_chip *chip)
548{
549}
550
551#endif
552
553struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
554 const char *label);
555void gpiochip_free_own_desc(struct gpio_desc *desc);
556
557#else
558
559static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
560{
561
562 WARN_ON(1);
563 return ERR_PTR(-ENODEV);
564}
565
566#endif
567
568#endif
569