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
23struct gpio_chip;
24
25#define GPIO_LINE_DIRECTION_IN 1
26#define GPIO_LINE_DIRECTION_OUT 0
27
28
29
30
31struct gpio_irq_chip {
32
33
34
35
36
37 struct irq_chip *chip;
38
39
40
41
42
43
44
45 struct irq_domain *domain;
46
47
48
49
50
51
52 const struct irq_domain_ops *domain_ops;
53
54#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
55
56
57
58
59
60
61 struct fwnode_handle *fwnode;
62
63
64
65
66
67
68
69
70
71 struct irq_domain *parent_domain;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 int (*child_to_parent_hwirq)(struct gpio_chip *chip,
91 unsigned int child_hwirq,
92 unsigned int child_type,
93 unsigned int *parent_hwirq,
94 unsigned int *parent_type);
95
96
97
98
99
100
101
102
103
104
105 void (*populate_parent_fwspec)(struct gpio_chip *chip,
106 struct irq_fwspec *fwspec,
107 unsigned int parent_hwirq,
108 unsigned int parent_type);
109
110
111
112
113
114
115
116
117
118 unsigned int (*child_offset_to_irq)(struct gpio_chip *chip,
119 unsigned int pin);
120
121
122
123
124
125
126
127
128
129 struct irq_domain_ops child_irq_domain_ops;
130#endif
131
132
133
134
135
136
137
138 irq_flow_handler_t handler;
139
140
141
142
143
144
145
146 unsigned int default_type;
147
148
149
150
151
152
153 struct lock_class_key *lock_key;
154
155
156
157
158
159
160 struct lock_class_key *request_key;
161
162
163
164
165
166
167
168 irq_flow_handler_t parent_handler;
169
170
171
172
173
174
175
176 void *parent_handler_data;
177
178
179
180
181
182
183 unsigned int num_parents;
184
185
186
187
188
189
190
191 unsigned int *parents;
192
193
194
195
196
197
198 unsigned int *map;
199
200
201
202
203
204
205 bool threaded;
206
207
208
209
210
211
212
213 int (*init_hw)(struct gpio_chip *chip);
214
215
216
217
218
219
220
221
222
223
224 void (*init_valid_mask)(struct gpio_chip *chip,
225 unsigned long *valid_mask,
226 unsigned int ngpios);
227
228
229
230
231
232
233
234 unsigned long *valid_mask;
235
236
237
238
239
240
241
242 unsigned int first;
243
244
245
246
247
248
249 void (*irq_enable)(struct irq_data *data);
250
251
252
253
254
255
256 void (*irq_disable)(struct irq_data *data);
257};
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346struct gpio_chip {
347 const char *label;
348 struct gpio_device *gpiodev;
349 struct device *parent;
350 struct module *owner;
351
352 int (*request)(struct gpio_chip *chip,
353 unsigned offset);
354 void (*free)(struct gpio_chip *chip,
355 unsigned offset);
356 int (*get_direction)(struct gpio_chip *chip,
357 unsigned offset);
358 int (*direction_input)(struct gpio_chip *chip,
359 unsigned offset);
360 int (*direction_output)(struct gpio_chip *chip,
361 unsigned offset, int value);
362 int (*get)(struct gpio_chip *chip,
363 unsigned offset);
364 int (*get_multiple)(struct gpio_chip *chip,
365 unsigned long *mask,
366 unsigned long *bits);
367 void (*set)(struct gpio_chip *chip,
368 unsigned offset, int value);
369 void (*set_multiple)(struct gpio_chip *chip,
370 unsigned long *mask,
371 unsigned long *bits);
372 int (*set_config)(struct gpio_chip *chip,
373 unsigned offset,
374 unsigned long config);
375 int (*to_irq)(struct gpio_chip *chip,
376 unsigned offset);
377
378 void (*dbg_show)(struct seq_file *s,
379 struct gpio_chip *chip);
380
381 int (*init_valid_mask)(struct gpio_chip *chip,
382 unsigned long *valid_mask,
383 unsigned int ngpios);
384
385 int (*add_pin_ranges)(struct gpio_chip *chip);
386
387 int base;
388 u16 ngpio;
389 const char *const *names;
390 bool can_sleep;
391
392#if IS_ENABLED(CONFIG_GPIO_GENERIC)
393 unsigned long (*read_reg)(void __iomem *reg);
394 void (*write_reg)(void __iomem *reg, unsigned long data);
395 bool be_bits;
396 void __iomem *reg_dat;
397 void __iomem *reg_set;
398 void __iomem *reg_clr;
399 void __iomem *reg_dir_out;
400 void __iomem *reg_dir_in;
401 bool bgpio_dir_unreadable;
402 int bgpio_bits;
403 spinlock_t bgpio_lock;
404 unsigned long bgpio_data;
405 unsigned long bgpio_dir;
406#endif
407
408#ifdef CONFIG_GPIOLIB_IRQCHIP
409
410
411
412
413
414
415
416
417
418
419
420 struct gpio_irq_chip irq;
421#endif
422
423
424
425
426
427
428
429 unsigned long *valid_mask;
430
431#if defined(CONFIG_OF_GPIO)
432
433
434
435
436
437
438
439
440
441
442 struct device_node *of_node;
443
444
445
446
447
448
449 unsigned int of_gpio_n_cells;
450
451
452
453
454
455
456
457 int (*of_xlate)(struct gpio_chip *gc,
458 const struct of_phandle_args *gpiospec, u32 *flags);
459#endif
460};
461
462extern const char *gpiochip_is_requested(struct gpio_chip *chip,
463 unsigned offset);
464
465
466extern int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
467 struct lock_class_key *lock_key,
468 struct lock_class_key *request_key);
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493#ifdef CONFIG_LOCKDEP
494#define gpiochip_add_data(chip, data) ({ \
495 static struct lock_class_key lock_key; \
496 static struct lock_class_key request_key; \
497 gpiochip_add_data_with_key(chip, data, &lock_key, \
498 &request_key); \
499 })
500#else
501#define gpiochip_add_data(chip, data) gpiochip_add_data_with_key(chip, data, NULL, NULL)
502#endif
503
504static inline int gpiochip_add(struct gpio_chip *chip)
505{
506 return gpiochip_add_data(chip, NULL);
507}
508extern void gpiochip_remove(struct gpio_chip *chip);
509extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
510 void *data);
511
512extern struct gpio_chip *gpiochip_find(void *data,
513 int (*match)(struct gpio_chip *chip, void *data));
514
515bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset);
516int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset);
517void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset);
518void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset);
519void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset);
520
521
522bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset);
523bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset);
524
525
526bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset);
527bool gpiochip_line_is_valid(const struct gpio_chip *chip, unsigned int offset);
528
529
530void *gpiochip_get_data(struct gpio_chip *chip);
531
532struct bgpio_pdata {
533 const char *label;
534 int base;
535 int ngpio;
536};
537
538#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
539
540void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
541 struct irq_fwspec *fwspec,
542 unsigned int parent_hwirq,
543 unsigned int parent_type);
544void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
545 struct irq_fwspec *fwspec,
546 unsigned int parent_hwirq,
547 unsigned int parent_type);
548
549#else
550
551static inline void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
552 struct irq_fwspec *fwspec,
553 unsigned int parent_hwirq,
554 unsigned int parent_type)
555{
556}
557
558static inline void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
559 struct irq_fwspec *fwspec,
560 unsigned int parent_hwirq,
561 unsigned int parent_type)
562{
563}
564
565#endif
566
567int bgpio_init(struct gpio_chip *gc, struct device *dev,
568 unsigned long sz, void __iomem *dat, void __iomem *set,
569 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
570 unsigned long flags);
571
572#define BGPIOF_BIG_ENDIAN BIT(0)
573#define BGPIOF_UNREADABLE_REG_SET BIT(1)
574#define BGPIOF_UNREADABLE_REG_DIR BIT(2)
575#define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3)
576#define BGPIOF_READ_OUTPUT_REG_SET BIT(4)
577#define BGPIOF_NO_OUTPUT BIT(5)
578
579int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
580 irq_hw_number_t hwirq);
581void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq);
582
583int gpiochip_irq_domain_activate(struct irq_domain *domain,
584 struct irq_data *data, bool reserve);
585void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
586 struct irq_data *data);
587
588void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
589 struct irq_chip *irqchip,
590 unsigned int parent_irq,
591 irq_flow_handler_t parent_handler);
592
593void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
594 struct irq_chip *irqchip,
595 unsigned int parent_irq);
596
597int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
598 struct irq_chip *irqchip,
599 unsigned int first_irq,
600 irq_flow_handler_t handler,
601 unsigned int type,
602 bool threaded,
603 struct lock_class_key *lock_key,
604 struct lock_class_key *request_key);
605
606bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
607 unsigned int offset);
608
609#ifdef CONFIG_LOCKDEP
610
611
612
613
614
615
616
617static inline int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
618 struct irq_chip *irqchip,
619 unsigned int first_irq,
620 irq_flow_handler_t handler,
621 unsigned int type)
622{
623 static struct lock_class_key lock_key;
624 static struct lock_class_key request_key;
625
626 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
627 handler, type, false,
628 &lock_key, &request_key);
629}
630
631static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gpiochip,
632 struct irq_chip *irqchip,
633 unsigned int first_irq,
634 irq_flow_handler_t handler,
635 unsigned int type)
636{
637
638 static struct lock_class_key lock_key;
639 static struct lock_class_key request_key;
640
641 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
642 handler, type, true,
643 &lock_key, &request_key);
644}
645#else
646static inline int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
647 struct irq_chip *irqchip,
648 unsigned int first_irq,
649 irq_flow_handler_t handler,
650 unsigned int type)
651{
652 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
653 handler, type, false, NULL, NULL);
654}
655
656static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gpiochip,
657 struct irq_chip *irqchip,
658 unsigned int first_irq,
659 irq_flow_handler_t handler,
660 unsigned int type)
661{
662 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
663 handler, type, true, NULL, NULL);
664}
665#endif
666
667int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset);
668void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset);
669int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
670 unsigned long config);
671
672
673
674
675
676
677
678struct gpio_pin_range {
679 struct list_head node;
680 struct pinctrl_dev *pctldev;
681 struct pinctrl_gpio_range range;
682};
683
684#ifdef CONFIG_PINCTRL
685
686int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
687 unsigned int gpio_offset, unsigned int pin_offset,
688 unsigned int npins);
689int gpiochip_add_pingroup_range(struct gpio_chip *chip,
690 struct pinctrl_dev *pctldev,
691 unsigned int gpio_offset, const char *pin_group);
692void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
693
694#else
695
696static inline int
697gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
698 unsigned int gpio_offset, unsigned int pin_offset,
699 unsigned int npins)
700{
701 return 0;
702}
703static inline int
704gpiochip_add_pingroup_range(struct gpio_chip *chip,
705 struct pinctrl_dev *pctldev,
706 unsigned int gpio_offset, const char *pin_group)
707{
708 return 0;
709}
710
711static inline void
712gpiochip_remove_pin_ranges(struct gpio_chip *chip)
713{
714}
715
716#endif
717
718struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
719 const char *label,
720 enum gpio_lookup_flags lflags,
721 enum gpiod_flags dflags);
722void gpiochip_free_own_desc(struct gpio_desc *desc);
723
724void devprop_gpiochip_set_names(struct gpio_chip *chip,
725 const struct fwnode_handle *fwnode);
726
727#ifdef CONFIG_GPIOLIB
728
729
730int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
731void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
732
733
734struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
735
736#else
737
738static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
739{
740
741 WARN_ON(1);
742 return ERR_PTR(-ENODEV);
743}
744
745static inline int gpiochip_lock_as_irq(struct gpio_chip *chip,
746 unsigned int offset)
747{
748 WARN_ON(1);
749 return -EINVAL;
750}
751
752static inline void gpiochip_unlock_as_irq(struct gpio_chip *chip,
753 unsigned int offset)
754{
755 WARN_ON(1);
756}
757#endif
758
759#endif
760