xref: /openbmc/linux/include/linux/of_gpio.h (revision 74982092)
1863fbf49SAnton Vorontsov /*
2863fbf49SAnton Vorontsov  * OF helpers for the GPIO API
3863fbf49SAnton Vorontsov  *
4863fbf49SAnton Vorontsov  * Copyright (c) 2007-2008  MontaVista Software, Inc.
5863fbf49SAnton Vorontsov  *
6863fbf49SAnton Vorontsov  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7863fbf49SAnton Vorontsov  *
8863fbf49SAnton Vorontsov  * This program is free software; you can redistribute it and/or modify
9863fbf49SAnton Vorontsov  * it under the terms of the GNU General Public License as published by
10863fbf49SAnton Vorontsov  * the Free Software Foundation; either version 2 of the License, or
11863fbf49SAnton Vorontsov  * (at your option) any later version.
12863fbf49SAnton Vorontsov  */
13863fbf49SAnton Vorontsov 
14863fbf49SAnton Vorontsov #ifndef __LINUX_OF_GPIO_H
15863fbf49SAnton Vorontsov #define __LINUX_OF_GPIO_H
16863fbf49SAnton Vorontsov 
17b908b53dSAnton Vorontsov #include <linux/compiler.h>
18b908b53dSAnton Vorontsov #include <linux/kernel.h>
19863fbf49SAnton Vorontsov #include <linux/errno.h>
2018ad7a61SWolfgang Grandegger #include <linux/gpio.h>
21863fbf49SAnton Vorontsov 
22b908b53dSAnton Vorontsov struct device_node;
23b908b53dSAnton Vorontsov 
24b908b53dSAnton Vorontsov /*
25b908b53dSAnton Vorontsov  * This is Linux-specific flags. By default controllers' and Linux' mapping
26b908b53dSAnton Vorontsov  * match, but GPIO controllers are free to translate their own flags to
27b908b53dSAnton Vorontsov  * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
28b908b53dSAnton Vorontsov  */
29b908b53dSAnton Vorontsov enum of_gpio_flags {
30b908b53dSAnton Vorontsov 	OF_GPIO_ACTIVE_LOW = 0x1,
31b908b53dSAnton Vorontsov };
32b908b53dSAnton Vorontsov 
33863fbf49SAnton Vorontsov #ifdef CONFIG_OF_GPIO
34863fbf49SAnton Vorontsov 
35863fbf49SAnton Vorontsov /*
36863fbf49SAnton Vorontsov  * Generic OF GPIO chip
37863fbf49SAnton Vorontsov  */
38863fbf49SAnton Vorontsov struct of_gpio_chip {
39863fbf49SAnton Vorontsov 	struct gpio_chip gc;
40863fbf49SAnton Vorontsov 	int gpio_cells;
41863fbf49SAnton Vorontsov 	int (*xlate)(struct of_gpio_chip *of_gc, struct device_node *np,
42b908b53dSAnton Vorontsov 		     const void *gpio_spec, enum of_gpio_flags *flags);
43863fbf49SAnton Vorontsov };
44863fbf49SAnton Vorontsov 
45863fbf49SAnton Vorontsov static inline struct of_gpio_chip *to_of_gpio_chip(struct gpio_chip *gc)
46863fbf49SAnton Vorontsov {
47863fbf49SAnton Vorontsov 	return container_of(gc, struct of_gpio_chip, gc);
48863fbf49SAnton Vorontsov }
49863fbf49SAnton Vorontsov 
50863fbf49SAnton Vorontsov /*
51863fbf49SAnton Vorontsov  * OF GPIO chip for memory mapped banks
52863fbf49SAnton Vorontsov  */
53863fbf49SAnton Vorontsov struct of_mm_gpio_chip {
54863fbf49SAnton Vorontsov 	struct of_gpio_chip of_gc;
55863fbf49SAnton Vorontsov 	void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
56863fbf49SAnton Vorontsov 	void __iomem *regs;
57863fbf49SAnton Vorontsov };
58863fbf49SAnton Vorontsov 
59863fbf49SAnton Vorontsov static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
60863fbf49SAnton Vorontsov {
61863fbf49SAnton Vorontsov 	struct of_gpio_chip *of_gc = to_of_gpio_chip(gc);
62863fbf49SAnton Vorontsov 
63863fbf49SAnton Vorontsov 	return container_of(of_gc, struct of_mm_gpio_chip, of_gc);
64863fbf49SAnton Vorontsov }
65863fbf49SAnton Vorontsov 
66b908b53dSAnton Vorontsov extern int of_get_gpio_flags(struct device_node *np, int index,
67b908b53dSAnton Vorontsov 			     enum of_gpio_flags *flags);
6874982092SAnton Vorontsov extern unsigned int of_gpio_count(struct device_node *np);
69b908b53dSAnton Vorontsov 
70863fbf49SAnton Vorontsov extern int of_mm_gpiochip_add(struct device_node *np,
71863fbf49SAnton Vorontsov 			      struct of_mm_gpio_chip *mm_gc);
72863fbf49SAnton Vorontsov extern int of_gpio_simple_xlate(struct of_gpio_chip *of_gc,
73863fbf49SAnton Vorontsov 				struct device_node *np,
74b908b53dSAnton Vorontsov 				const void *gpio_spec,
75b908b53dSAnton Vorontsov 				enum of_gpio_flags *flags);
76863fbf49SAnton Vorontsov #else
77863fbf49SAnton Vorontsov 
78863fbf49SAnton Vorontsov /* Drivers may not strictly depend on the GPIO support, so let them link. */
79b908b53dSAnton Vorontsov static inline int of_get_gpio_flags(struct device_node *np, int index,
80b908b53dSAnton Vorontsov 				    enum of_gpio_flags *flags)
81863fbf49SAnton Vorontsov {
82863fbf49SAnton Vorontsov 	return -ENOSYS;
83863fbf49SAnton Vorontsov }
84863fbf49SAnton Vorontsov 
8574982092SAnton Vorontsov static inline unsigned int of_gpio_count(struct device_node *np)
8674982092SAnton Vorontsov {
8774982092SAnton Vorontsov 	return 0;
8874982092SAnton Vorontsov }
8974982092SAnton Vorontsov 
90863fbf49SAnton Vorontsov #endif /* CONFIG_OF_GPIO */
91863fbf49SAnton Vorontsov 
92b908b53dSAnton Vorontsov /**
93b908b53dSAnton Vorontsov  * of_get_gpio - Get a GPIO number to use with GPIO API
94b908b53dSAnton Vorontsov  * @np:		device node to get GPIO from
95b908b53dSAnton Vorontsov  * @index:	index of the GPIO
96b908b53dSAnton Vorontsov  *
97b908b53dSAnton Vorontsov  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
98b908b53dSAnton Vorontsov  * value on the error condition.
99b908b53dSAnton Vorontsov  */
100b908b53dSAnton Vorontsov static inline int of_get_gpio(struct device_node *np, int index)
101b908b53dSAnton Vorontsov {
102b908b53dSAnton Vorontsov 	return of_get_gpio_flags(np, index, NULL);
103b908b53dSAnton Vorontsov }
104b908b53dSAnton Vorontsov 
105863fbf49SAnton Vorontsov #endif /* __LINUX_OF_GPIO_H */
106