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
292
293
294
295
296 bool need_valid_mask;
297
298
299
300
301
302
303
304 unsigned long *valid_mask;
305
306#if defined(CONFIG_OF_GPIO)
307
308
309
310
311
312
313
314
315
316
317 struct device_node *of_node;
318
319
320
321
322
323
324 unsigned int of_gpio_n_cells;
325
326
327
328
329
330
331
332 int (*of_xlate)(struct gpio_chip *gc,
333 const struct of_phandle_args *gpiospec, u32 *flags);
334#endif
335};
336
337extern const char *gpiochip_is_requested(struct gpio_chip *chip,
338 unsigned offset);
339
340
341extern int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
342 struct lock_class_key *lock_key,
343 struct lock_class_key *request_key);
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368#ifdef CONFIG_LOCKDEP
369#define gpiochip_add_data(chip, data) ({ \
370 static struct lock_class_key lock_key; \
371 static struct lock_class_key request_key; \
372 gpiochip_add_data_with_key(chip, data, &lock_key, \
373 &request_key); \
374 })
375#else
376#define gpiochip_add_data(chip, data) gpiochip_add_data_with_key(chip, data, NULL, NULL)
377#endif
378
379static inline int gpiochip_add(struct gpio_chip *chip)
380{
381 return gpiochip_add_data(chip, NULL);
382}
383extern void gpiochip_remove(struct gpio_chip *chip);
384extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
385 void *data);
386extern void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip);
387
388extern struct gpio_chip *gpiochip_find(void *data,
389 int (*match)(struct gpio_chip *chip, void *data));
390
391
392int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
393void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
394bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset);
395
396
397bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset);
398bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset);
399
400
401bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset);
402bool gpiochip_line_is_valid(const struct gpio_chip *chip, unsigned int offset);
403
404
405void *gpiochip_get_data(struct gpio_chip *chip);
406
407struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
408
409struct bgpio_pdata {
410 const char *label;
411 int base;
412 int ngpio;
413};
414
415#if IS_ENABLED(CONFIG_GPIO_GENERIC)
416
417int bgpio_init(struct gpio_chip *gc, struct device *dev,
418 unsigned long sz, void __iomem *dat, void __iomem *set,
419 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
420 unsigned long flags);
421
422#define BGPIOF_BIG_ENDIAN BIT(0)
423#define BGPIOF_UNREADABLE_REG_SET BIT(1)
424#define BGPIOF_UNREADABLE_REG_DIR BIT(2)
425#define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3)
426#define BGPIOF_READ_OUTPUT_REG_SET BIT(4)
427#define BGPIOF_NO_OUTPUT BIT(5)
428
429#endif
430
431#ifdef CONFIG_GPIOLIB_IRQCHIP
432
433int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
434 irq_hw_number_t hwirq);
435void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq);
436
437void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
438 struct irq_chip *irqchip,
439 unsigned int parent_irq,
440 irq_flow_handler_t parent_handler);
441
442void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
443 struct irq_chip *irqchip,
444 unsigned int parent_irq);
445
446int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
447 struct irq_chip *irqchip,
448 unsigned int first_irq,
449 irq_flow_handler_t handler,
450 unsigned int type,
451 bool threaded,
452 struct lock_class_key *lock_key,
453 struct lock_class_key *request_key);
454
455bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
456 unsigned int offset);
457
458#ifdef CONFIG_LOCKDEP
459
460
461
462
463
464
465
466static inline int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
467 struct irq_chip *irqchip,
468 unsigned int first_irq,
469 irq_flow_handler_t handler,
470 unsigned int type)
471{
472 static struct lock_class_key lock_key;
473 static struct lock_class_key request_key;
474
475 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
476 handler, type, false,
477 &lock_key, &request_key);
478}
479
480static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gpiochip,
481 struct irq_chip *irqchip,
482 unsigned int first_irq,
483 irq_flow_handler_t handler,
484 unsigned int type)
485{
486
487 static struct lock_class_key lock_key;
488 static struct lock_class_key request_key;
489
490 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
491 handler, type, true,
492 &lock_key, &request_key);
493}
494#else
495static inline int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
496 struct irq_chip *irqchip,
497 unsigned int first_irq,
498 irq_flow_handler_t handler,
499 unsigned int type)
500{
501 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
502 handler, type, false, NULL, NULL);
503}
504
505static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gpiochip,
506 struct irq_chip *irqchip,
507 unsigned int first_irq,
508 irq_flow_handler_t handler,
509 unsigned int type)
510{
511 return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
512 handler, type, true, NULL, NULL);
513}
514#endif
515
516#endif
517
518int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset);
519void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset);
520int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
521 unsigned long config);
522
523#ifdef CONFIG_PINCTRL
524
525
526
527
528
529
530
531struct gpio_pin_range {
532 struct list_head node;
533 struct pinctrl_dev *pctldev;
534 struct pinctrl_gpio_range range;
535};
536
537int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
538 unsigned int gpio_offset, unsigned int pin_offset,
539 unsigned int npins);
540int gpiochip_add_pingroup_range(struct gpio_chip *chip,
541 struct pinctrl_dev *pctldev,
542 unsigned int gpio_offset, const char *pin_group);
543void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
544
545#else
546
547static inline int
548gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
549 unsigned int gpio_offset, unsigned int pin_offset,
550 unsigned int npins)
551{
552 return 0;
553}
554static inline int
555gpiochip_add_pingroup_range(struct gpio_chip *chip,
556 struct pinctrl_dev *pctldev,
557 unsigned int gpio_offset, const char *pin_group)
558{
559 return 0;
560}
561
562static inline void
563gpiochip_remove_pin_ranges(struct gpio_chip *chip)
564{
565}
566
567#endif
568
569struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
570 const char *label);
571void gpiochip_free_own_desc(struct gpio_desc *desc);
572
573#else
574
575static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
576{
577
578 WARN_ON(1);
579 return ERR_PTR(-ENODEV);
580}
581
582#endif
583
584#endif
585