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 *gc,
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_alloc_arg)(struct gpio_chip *gc,
106 unsigned int parent_hwirq,
107 unsigned int parent_type);
108
109
110
111
112
113
114
115
116
117 unsigned int (*child_offset_to_irq)(struct gpio_chip *gc,
118 unsigned int pin);
119
120
121
122
123
124
125
126
127
128 struct irq_domain_ops child_irq_domain_ops;
129#endif
130
131
132
133
134
135
136
137 irq_flow_handler_t handler;
138
139
140
141
142
143
144
145 unsigned int default_type;
146
147
148
149
150
151
152 struct lock_class_key *lock_key;
153
154
155
156
157
158
159 struct lock_class_key *request_key;
160
161
162
163
164
165
166
167 irq_flow_handler_t parent_handler;
168
169
170
171
172
173
174
175 void *parent_handler_data;
176
177
178
179
180
181
182 unsigned int num_parents;
183
184
185
186
187
188
189
190 unsigned int *parents;
191
192
193
194
195
196
197 unsigned int *map;
198
199
200
201
202
203
204 bool threaded;
205
206
207
208
209
210
211
212 int (*init_hw)(struct gpio_chip *gc);
213
214
215
216
217
218
219
220
221
222
223 void (*init_valid_mask)(struct gpio_chip *gc,
224 unsigned long *valid_mask,
225 unsigned int ngpios);
226
227
228
229
230
231
232
233 unsigned long *valid_mask;
234
235
236
237
238
239
240
241 unsigned int first;
242
243
244
245
246
247
248 void (*irq_enable)(struct irq_data *data);
249
250
251
252
253
254
255 void (*irq_disable)(struct irq_data *data);
256
257
258
259
260
261 void (*irq_unmask)(struct irq_data *data);
262
263
264
265
266
267
268 void (*irq_mask)(struct irq_data *data);
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
346
347
348
349
350
351
352
353
354
355
356
357
358struct gpio_chip {
359 const char *label;
360 struct gpio_device *gpiodev;
361 struct device *parent;
362 struct module *owner;
363
364 int (*request)(struct gpio_chip *gc,
365 unsigned int offset);
366 void (*free)(struct gpio_chip *gc,
367 unsigned int offset);
368 int (*get_direction)(struct gpio_chip *gc,
369 unsigned int offset);
370 int (*direction_input)(struct gpio_chip *gc,
371 unsigned int offset);
372 int (*direction_output)(struct gpio_chip *gc,
373 unsigned int offset, int value);
374 int (*get)(struct gpio_chip *gc,
375 unsigned int offset);
376 int (*get_multiple)(struct gpio_chip *gc,
377 unsigned long *mask,
378 unsigned long *bits);
379 void (*set)(struct gpio_chip *gc,
380 unsigned int offset, int value);
381 void (*set_multiple)(struct gpio_chip *gc,
382 unsigned long *mask,
383 unsigned long *bits);
384 int (*set_config)(struct gpio_chip *gc,
385 unsigned int offset,
386 unsigned long config);
387 int (*to_irq)(struct gpio_chip *gc,
388 unsigned int offset);
389
390 void (*dbg_show)(struct seq_file *s,
391 struct gpio_chip *gc);
392
393 int (*init_valid_mask)(struct gpio_chip *gc,
394 unsigned long *valid_mask,
395 unsigned int ngpios);
396
397 int (*add_pin_ranges)(struct gpio_chip *gc);
398
399 int base;
400 u16 ngpio;
401 const char *const *names;
402 bool can_sleep;
403
404#if IS_ENABLED(CONFIG_GPIO_GENERIC)
405 unsigned long (*read_reg)(void __iomem *reg);
406 void (*write_reg)(void __iomem *reg, unsigned long data);
407 bool be_bits;
408 void __iomem *reg_dat;
409 void __iomem *reg_set;
410 void __iomem *reg_clr;
411 void __iomem *reg_dir_out;
412 void __iomem *reg_dir_in;
413 bool bgpio_dir_unreadable;
414 int bgpio_bits;
415 spinlock_t bgpio_lock;
416 unsigned long bgpio_data;
417 unsigned long bgpio_dir;
418#endif
419
420#ifdef CONFIG_GPIOLIB_IRQCHIP
421
422
423
424
425
426
427
428
429
430
431
432 struct gpio_irq_chip irq;
433#endif
434
435
436
437
438
439
440
441 unsigned long *valid_mask;
442
443#if defined(CONFIG_OF_GPIO)
444
445
446
447
448
449
450
451
452
453
454 struct device_node *of_node;
455
456
457
458
459
460
461 unsigned int of_gpio_n_cells;
462
463
464
465
466
467
468
469 int (*of_xlate)(struct gpio_chip *gc,
470 const struct of_phandle_args *gpiospec, u32 *flags);
471#endif
472};
473
474extern const char *gpiochip_is_requested(struct gpio_chip *gc,
475 unsigned int offset);
476
477
478
479
480
481
482
483
484
485#define for_each_requested_gpio_in_range(chip, i, base, size, label) \
486 for (i = 0; i < size; i++) \
487 if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else
488
489
490#define for_each_requested_gpio(chip, i, label) \
491 for_each_requested_gpio_in_range(chip, i, 0, chip->ngpio, label)
492
493
494extern int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
495 struct lock_class_key *lock_key,
496 struct lock_class_key *request_key);
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521#ifdef CONFIG_LOCKDEP
522#define gpiochip_add_data(gc, data) ({ \
523 static struct lock_class_key lock_key; \
524 static struct lock_class_key request_key; \
525 gpiochip_add_data_with_key(gc, data, &lock_key, \
526 &request_key); \
527 })
528#define devm_gpiochip_add_data(dev, gc, data) ({ \
529 static struct lock_class_key lock_key; \
530 static struct lock_class_key request_key; \
531 devm_gpiochip_add_data_with_key(dev, gc, data, &lock_key, \
532 &request_key); \
533 })
534#else
535#define gpiochip_add_data(gc, data) gpiochip_add_data_with_key(gc, data, NULL, NULL)
536#define devm_gpiochip_add_data(dev, gc, data) \
537 devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL)
538#endif
539
540static inline int gpiochip_add(struct gpio_chip *gc)
541{
542 return gpiochip_add_data(gc, NULL);
543}
544extern void gpiochip_remove(struct gpio_chip *gc);
545extern int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
546 struct lock_class_key *lock_key,
547 struct lock_class_key *request_key);
548
549extern struct gpio_chip *gpiochip_find(void *data,
550 int (*match)(struct gpio_chip *gc, void *data));
551
552bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset);
553int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset);
554void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset);
555void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset);
556void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset);
557
558
559bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset);
560bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset);
561
562
563bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset);
564bool gpiochip_line_is_valid(const struct gpio_chip *gc, unsigned int offset);
565
566
567void *gpiochip_get_data(struct gpio_chip *gc);
568
569struct bgpio_pdata {
570 const char *label;
571 int base;
572 int ngpio;
573};
574
575#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
576
577void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
578 unsigned int parent_hwirq,
579 unsigned int parent_type);
580void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
581 unsigned int parent_hwirq,
582 unsigned int parent_type);
583
584#else
585
586static inline void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
587 unsigned int parent_hwirq,
588 unsigned int parent_type)
589{
590 return NULL;
591}
592
593static inline void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
594 unsigned int parent_hwirq,
595 unsigned int parent_type)
596{
597 return NULL;
598}
599
600#endif
601
602int bgpio_init(struct gpio_chip *gc, struct device *dev,
603 unsigned long sz, void __iomem *dat, void __iomem *set,
604 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
605 unsigned long flags);
606
607#define BGPIOF_BIG_ENDIAN BIT(0)
608#define BGPIOF_UNREADABLE_REG_SET BIT(1)
609#define BGPIOF_UNREADABLE_REG_DIR BIT(2)
610#define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3)
611#define BGPIOF_READ_OUTPUT_REG_SET BIT(4)
612#define BGPIOF_NO_OUTPUT BIT(5)
613#define BGPIOF_NO_SET_ON_INPUT BIT(6)
614
615int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
616 irq_hw_number_t hwirq);
617void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq);
618
619int gpiochip_irq_domain_activate(struct irq_domain *domain,
620 struct irq_data *data, bool reserve);
621void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
622 struct irq_data *data);
623
624void gpiochip_set_nested_irqchip(struct gpio_chip *gc,
625 struct irq_chip *irqchip,
626 unsigned int parent_irq);
627
628int gpiochip_irqchip_add_key(struct gpio_chip *gc,
629 struct irq_chip *irqchip,
630 unsigned int first_irq,
631 irq_flow_handler_t handler,
632 unsigned int type,
633 bool threaded,
634 struct lock_class_key *lock_key,
635 struct lock_class_key *request_key);
636
637bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
638 unsigned int offset);
639
640int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
641 struct irq_domain *domain);
642
643#ifdef CONFIG_LOCKDEP
644
645
646
647
648
649
650
651static inline int gpiochip_irqchip_add(struct gpio_chip *gc,
652 struct irq_chip *irqchip,
653 unsigned int first_irq,
654 irq_flow_handler_t handler,
655 unsigned int type)
656{
657 static struct lock_class_key lock_key;
658 static struct lock_class_key request_key;
659
660 return gpiochip_irqchip_add_key(gc, irqchip, first_irq,
661 handler, type, false,
662 &lock_key, &request_key);
663}
664
665static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gc,
666 struct irq_chip *irqchip,
667 unsigned int first_irq,
668 irq_flow_handler_t handler,
669 unsigned int type)
670{
671
672 static struct lock_class_key lock_key;
673 static struct lock_class_key request_key;
674
675 return gpiochip_irqchip_add_key(gc, irqchip, first_irq,
676 handler, type, true,
677 &lock_key, &request_key);
678}
679#else
680static inline int gpiochip_irqchip_add(struct gpio_chip *gc,
681 struct irq_chip *irqchip,
682 unsigned int first_irq,
683 irq_flow_handler_t handler,
684 unsigned int type)
685{
686 return gpiochip_irqchip_add_key(gc, irqchip, first_irq,
687 handler, type, false, NULL, NULL);
688}
689
690static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gc,
691 struct irq_chip *irqchip,
692 unsigned int first_irq,
693 irq_flow_handler_t handler,
694 unsigned int type)
695{
696 return gpiochip_irqchip_add_key(gc, irqchip, first_irq,
697 handler, type, true, NULL, NULL);
698}
699#endif
700
701int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset);
702void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset);
703int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
704 unsigned long config);
705
706
707
708
709
710
711
712struct gpio_pin_range {
713 struct list_head node;
714 struct pinctrl_dev *pctldev;
715 struct pinctrl_gpio_range range;
716};
717
718#ifdef CONFIG_PINCTRL
719
720int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
721 unsigned int gpio_offset, unsigned int pin_offset,
722 unsigned int npins);
723int gpiochip_add_pingroup_range(struct gpio_chip *gc,
724 struct pinctrl_dev *pctldev,
725 unsigned int gpio_offset, const char *pin_group);
726void gpiochip_remove_pin_ranges(struct gpio_chip *gc);
727
728#else
729
730static inline int
731gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
732 unsigned int gpio_offset, unsigned int pin_offset,
733 unsigned int npins)
734{
735 return 0;
736}
737static inline int
738gpiochip_add_pingroup_range(struct gpio_chip *gc,
739 struct pinctrl_dev *pctldev,
740 unsigned int gpio_offset, const char *pin_group)
741{
742 return 0;
743}
744
745static inline void
746gpiochip_remove_pin_ranges(struct gpio_chip *gc)
747{
748}
749
750#endif
751
752struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
753 unsigned int hwnum,
754 const char *label,
755 enum gpio_lookup_flags lflags,
756 enum gpiod_flags dflags);
757void gpiochip_free_own_desc(struct gpio_desc *desc);
758
759#ifdef CONFIG_GPIOLIB
760
761
762int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset);
763void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset);
764
765
766struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
767
768#else
769
770static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
771{
772
773 WARN_ON(1);
774 return ERR_PTR(-ENODEV);
775}
776
777static inline int gpiochip_lock_as_irq(struct gpio_chip *gc,
778 unsigned int offset)
779{
780 WARN_ON(1);
781 return -EINVAL;
782}
783
784static inline void gpiochip_unlock_as_irq(struct gpio_chip *gc,
785 unsigned int offset)
786{
787 WARN_ON(1);
788}
789#endif
790
791#endif
792