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