1863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov/*
2863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov * OF helpers for the GPIO API
3863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov *
4863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov * Copyright (c) 2007-2008  MontaVista Software, Inc.
5863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov *
6863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov *
8863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov * This program is free software; you can redistribute it and/or modify
9863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov * it under the terms of the GNU General Public License as published by
10863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov * the Free Software Foundation; either version 2 of the License, or
11863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov * (at your option) any later version.
12863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov */
13863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
14863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov#ifndef __LINUX_OF_GPIO_H
15863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov#define __LINUX_OF_GPIO_H
16863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
17b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov#include <linux/compiler.h>
18b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov#include <linux/kernel.h>
19863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov#include <linux/errno.h>
2018ad7a61e1b700dfe177fabf1c350b4f5d4da8acWolfgang Grandegger#include <linux/gpio.h>
2115c9a0acc3f7873db4b7d35d016729b2dc229b49Grant Likely#include <linux/of.h>
22863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
23b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsovstruct device_node;
24b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov
25b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov/*
26b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov * This is Linux-specific flags. By default controllers' and Linux' mapping
27b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov * match, but GPIO controllers are free to translate their own flags to
28b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
29b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov */
30b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsovenum of_gpio_flags {
31b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov	OF_GPIO_ACTIVE_LOW = 0x1,
32b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov};
33b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov
34863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov#ifdef CONFIG_OF_GPIO
35863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
36863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov/*
37863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov * OF GPIO chip for memory mapped banks
38863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov */
39863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsovstruct of_mm_gpio_chip {
40a19e3da5bc5fc6c10ab73f310bea80f3845b4531Anton Vorontsov	struct gpio_chip gc;
41863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov	void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
42863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov	void __iomem *regs;
43863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov};
44863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
45863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsovstatic inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
46863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov{
47a19e3da5bc5fc6c10ab73f310bea80f3845b4531Anton Vorontsov	return container_of(gc, struct of_mm_gpio_chip, gc);
48863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov}
49863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
50a6b0919140b49e0871584362ae0cf1d18c476058John Bonesioextern int of_get_named_gpio_flags(struct device_node *np,
51a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio		const char *list_name, int index, enum of_gpio_flags *flags);
52a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio
53ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARDextern unsigned int of_gpio_named_count(struct device_node *np,
54ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD					const char* propname);
55b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov
56863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsovextern int of_mm_gpiochip_add(struct device_node *np,
57863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov			      struct of_mm_gpio_chip *mm_gc);
58594fa265e084073443390c5b93d5410fd28e9bcdGrant Likely
59391c970c0dd1100e3b9e1681f7d0f20aac35455aAnton Vorontsovextern void of_gpiochip_add(struct gpio_chip *gc);
60391c970c0dd1100e3b9e1681f7d0f20aac35455aAnton Vorontsovextern void of_gpiochip_remove(struct gpio_chip *gc);
61594fa265e084073443390c5b93d5410fd28e9bcdGrant Likelyextern struct gpio_chip *of_node_to_gpiochip(struct device_node *np);
6215c9a0acc3f7873db4b7d35d016729b2dc229b49Grant Likelyextern int of_gpio_simple_xlate(struct gpio_chip *gc,
6315c9a0acc3f7873db4b7d35d016729b2dc229b49Grant Likely				const struct of_phandle_args *gpiospec,
6415c9a0acc3f7873db4b7d35d016729b2dc229b49Grant Likely				u32 *flags);
65594fa265e084073443390c5b93d5410fd28e9bcdGrant Likely
66a19e3da5bc5fc6c10ab73f310bea80f3845b4531Anton Vorontsov#else /* CONFIG_OF_GPIO */
67863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
68863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov/* Drivers may not strictly depend on the GPIO support, so let them link. */
69a6b0919140b49e0871584362ae0cf1d18c476058John Bonesiostatic inline int of_get_named_gpio_flags(struct device_node *np,
70a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio		const char *list_name, int index, enum of_gpio_flags *flags)
71863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov{
72863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov	return -ENOSYS;
73863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov}
74863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
75ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARDstatic inline unsigned int of_gpio_named_count(struct device_node *np,
76ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD					const char* propname)
77749820928a2fd47ff536773d869d2c3f8038b7d1Anton Vorontsov{
78749820928a2fd47ff536773d869d2c3f8038b7d1Anton Vorontsov	return 0;
79749820928a2fd47ff536773d869d2c3f8038b7d1Anton Vorontsov}
80749820928a2fd47ff536773d869d2c3f8038b7d1Anton Vorontsov
813038bbdf7404ae3948385cbde30df946579d4e3aJamie Ilesstatic inline int of_gpio_simple_xlate(struct gpio_chip *gc,
8215c9a0acc3f7873db4b7d35d016729b2dc229b49Grant Likely				       const struct of_phandle_args *gpiospec,
8315c9a0acc3f7873db4b7d35d016729b2dc229b49Grant Likely				       u32 *flags)
843038bbdf7404ae3948385cbde30df946579d4e3aJamie Iles{
853038bbdf7404ae3948385cbde30df946579d4e3aJamie Iles	return -ENOSYS;
863038bbdf7404ae3948385cbde30df946579d4e3aJamie Iles}
873038bbdf7404ae3948385cbde30df946579d4e3aJamie Iles
88391c970c0dd1100e3b9e1681f7d0f20aac35455aAnton Vorontsovstatic inline void of_gpiochip_add(struct gpio_chip *gc) { }
89391c970c0dd1100e3b9e1681f7d0f20aac35455aAnton Vorontsovstatic inline void of_gpiochip_remove(struct gpio_chip *gc) { }
90391c970c0dd1100e3b9e1681f7d0f20aac35455aAnton Vorontsov
91863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov#endif /* CONFIG_OF_GPIO */
92863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov
93b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov/**
94ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD * of_gpio_count - Count GPIOs for a device
95ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD * @np:		device node to count GPIOs for
96ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD *
97ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD * The function returns the count of GPIOs specified for a node.
98ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD *
99ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD * Note that the empty GPIO specifiers counts too. For example,
100ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD *
101ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD * gpios = <0
102ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD *          &pio1 1 2
103ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD *          0
104ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD *          &pio2 3 4>;
105ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD *
106ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD * defines four GPIOs (so this function will return 4), two of which
107ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD * are not specified.
108ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD */
109ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARDstatic inline unsigned int of_gpio_count(struct device_node *np)
110ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD{
111ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD	return of_gpio_named_count(np, "gpios");
112ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD}
113ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD
114ff64abefb6680dfc2aca7ecaa5e695949e7335c9Jean-Christophe PLAGNIOL-VILLARD/**
115a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * of_get_gpio_flags() - Get a GPIO number and flags to use with GPIO API
116a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * @np:		device node to get GPIO from
117a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * @index:	index of the GPIO
118a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * @flags:	a flags pointer to fill in
119a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio *
120a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
121a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * value on the error condition. If @flags is not NULL the function also fills
122a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * in flags for the GPIO.
123a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio */
124a6b0919140b49e0871584362ae0cf1d18c476058John Bonesiostatic inline int of_get_gpio_flags(struct device_node *np, int index,
125a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio		      enum of_gpio_flags *flags)
126a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio{
127a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio	return of_get_named_gpio_flags(np, "gpios", index, flags);
128a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio}
129a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio
130a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio/**
131a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * of_get_named_gpio() - Get a GPIO number to use with GPIO API
132a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * @np:		device node to get GPIO from
133a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * @propname:	Name of property containing gpio specifier(s)
134a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * @index:	index of the GPIO
135a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio *
136a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
137a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * value on the error condition.
138a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio */
139a6b0919140b49e0871584362ae0cf1d18c476058John Bonesiostatic inline int of_get_named_gpio(struct device_node *np,
140a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio                                   const char *propname, int index)
141a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio{
142a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio	return of_get_named_gpio_flags(np, propname, index, NULL);
143a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio}
144a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio
145a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio/**
146a6b0919140b49e0871584362ae0cf1d18c476058John Bonesio * of_get_gpio() - Get a GPIO number to use with GPIO API
147b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov * @np:		device node to get GPIO from
148b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov * @index:	index of the GPIO
149b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov *
150b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
151b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov * value on the error condition.
152b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov */
153b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsovstatic inline int of_get_gpio(struct device_node *np, int index)
154b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov{
155b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov	return of_get_gpio_flags(np, index, NULL);
156b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov}
157b908b53d580c3e9aba81ebe3339c5b7b4fa8031dAnton Vorontsov
158863fbf4966a7ac301a4077e4a04d73e8abfdd7b2Anton Vorontsov#endif /* __LINUX_OF_GPIO_H */
159