1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * OF helpers for the GPIO API
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2007-2008 MontaVista Software, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/err.h>
12*4882a593Smuzhiyun #include <linux/errno.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/io.h>
15*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
16*4882a593Smuzhiyun #include <linux/of.h>
17*4882a593Smuzhiyun #include <linux/of_address.h>
18*4882a593Smuzhiyun #include <linux/of_gpio.h>
19*4882a593Smuzhiyun #include <linux/pinctrl/pinctrl.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/gpio/machine.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "gpiolib.h"
24*4882a593Smuzhiyun #include "gpiolib-of.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /**
27*4882a593Smuzhiyun * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
28*4882a593Smuzhiyun * @dev: Consuming device
29*4882a593Smuzhiyun * @con_id: Function within the GPIO consumer
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * Some elder GPIO controllers need special quirks. Currently we handle
32*4882a593Smuzhiyun * the Freescale and PPC GPIO controller with bindings that doesn't use the
33*4882a593Smuzhiyun * established "cs-gpios" for chip selects but instead rely on
34*4882a593Smuzhiyun * "gpios" for the chip select lines. If we detect this, we redirect
35*4882a593Smuzhiyun * the counting of "cs-gpios" to count "gpios" transparent to the
36*4882a593Smuzhiyun * driver.
37*4882a593Smuzhiyun */
of_gpio_spi_cs_get_count(struct device * dev,const char * con_id)38*4882a593Smuzhiyun static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun struct device_node *np = dev->of_node;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_SPI_MASTER))
43*4882a593Smuzhiyun return 0;
44*4882a593Smuzhiyun if (!con_id || strcmp(con_id, "cs"))
45*4882a593Smuzhiyun return 0;
46*4882a593Smuzhiyun if (!of_device_is_compatible(np, "fsl,spi") &&
47*4882a593Smuzhiyun !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
48*4882a593Smuzhiyun !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
49*4882a593Smuzhiyun return 0;
50*4882a593Smuzhiyun return of_gpio_named_count(np, "gpios");
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * FIXME: get rid of those external users by converting them to GPIO
57*4882a593Smuzhiyun * descriptors and let them all use gpiod_count()
58*4882a593Smuzhiyun */
of_gpio_get_count(struct device * dev,const char * con_id)59*4882a593Smuzhiyun int of_gpio_get_count(struct device *dev, const char *con_id)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun int ret;
62*4882a593Smuzhiyun char propname[32];
63*4882a593Smuzhiyun unsigned int i;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun ret = of_gpio_spi_cs_get_count(dev, con_id);
66*4882a593Smuzhiyun if (ret > 0)
67*4882a593Smuzhiyun return ret;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
70*4882a593Smuzhiyun if (con_id)
71*4882a593Smuzhiyun snprintf(propname, sizeof(propname), "%s-%s",
72*4882a593Smuzhiyun con_id, gpio_suffixes[i]);
73*4882a593Smuzhiyun else
74*4882a593Smuzhiyun snprintf(propname, sizeof(propname), "%s",
75*4882a593Smuzhiyun gpio_suffixes[i]);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun ret = of_gpio_named_count(dev->of_node, propname);
78*4882a593Smuzhiyun if (ret > 0)
79*4882a593Smuzhiyun break;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun return ret ? ret : -ENOENT;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
of_gpiochip_match_node_and_xlate(struct gpio_chip * chip,void * data)84*4882a593Smuzhiyun static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun struct of_phandle_args *gpiospec = data;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return chip->gpiodev->dev.of_node == gpiospec->np &&
89*4882a593Smuzhiyun chip->of_xlate &&
90*4882a593Smuzhiyun chip->of_xlate(chip, gpiospec, NULL) >= 0;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
of_find_gpiochip_by_xlate(struct of_phandle_args * gpiospec)93*4882a593Smuzhiyun static struct gpio_chip *of_find_gpiochip_by_xlate(
94*4882a593Smuzhiyun struct of_phandle_args *gpiospec)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
of_xlate_and_get_gpiod_flags(struct gpio_chip * chip,struct of_phandle_args * gpiospec,enum of_gpio_flags * flags)99*4882a593Smuzhiyun static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
100*4882a593Smuzhiyun struct of_phandle_args *gpiospec,
101*4882a593Smuzhiyun enum of_gpio_flags *flags)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun int ret;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun if (chip->of_gpio_n_cells != gpiospec->args_count)
106*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun ret = chip->of_xlate(chip, gpiospec, flags);
109*4882a593Smuzhiyun if (ret < 0)
110*4882a593Smuzhiyun return ERR_PTR(ret);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return gpiochip_get_desc(chip, ret);
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
117*4882a593Smuzhiyun * to set the .valid_mask
118*4882a593Smuzhiyun * @gc: the target gpio_chip
119*4882a593Smuzhiyun *
120*4882a593Smuzhiyun * Return: true if the valid mask needs to be set
121*4882a593Smuzhiyun */
of_gpio_need_valid_mask(const struct gpio_chip * gc)122*4882a593Smuzhiyun bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun int size;
125*4882a593Smuzhiyun struct device_node *np = gc->of_node;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
128*4882a593Smuzhiyun if (size > 0 && size % 2 == 0)
129*4882a593Smuzhiyun return true;
130*4882a593Smuzhiyun return false;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
of_gpio_flags_quirks(struct device_node * np,const char * propname,enum of_gpio_flags * flags,int index)133*4882a593Smuzhiyun static void of_gpio_flags_quirks(struct device_node *np,
134*4882a593Smuzhiyun const char *propname,
135*4882a593Smuzhiyun enum of_gpio_flags *flags,
136*4882a593Smuzhiyun int index)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun * Some GPIO fixed regulator quirks.
140*4882a593Smuzhiyun * Note that active low is the default.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_REGULATOR) &&
143*4882a593Smuzhiyun (of_device_is_compatible(np, "regulator-fixed") ||
144*4882a593Smuzhiyun of_device_is_compatible(np, "reg-fixed-voltage") ||
145*4882a593Smuzhiyun (!(strcmp(propname, "enable-gpio") &&
146*4882a593Smuzhiyun strcmp(propname, "enable-gpios")) &&
147*4882a593Smuzhiyun of_device_is_compatible(np, "regulator-gpio")))) {
148*4882a593Smuzhiyun bool active_low = !of_property_read_bool(np,
149*4882a593Smuzhiyun "enable-active-high");
150*4882a593Smuzhiyun /*
151*4882a593Smuzhiyun * The regulator GPIO handles are specified such that the
152*4882a593Smuzhiyun * presence or absence of "enable-active-high" solely controls
153*4882a593Smuzhiyun * the polarity of the GPIO line. Any phandle flags must
154*4882a593Smuzhiyun * be actively ignored.
155*4882a593Smuzhiyun */
156*4882a593Smuzhiyun if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
157*4882a593Smuzhiyun pr_warn("%s GPIO handle specifies active low - ignored\n",
158*4882a593Smuzhiyun of_node_full_name(np));
159*4882a593Smuzhiyun *flags &= ~OF_GPIO_ACTIVE_LOW;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun if (active_low)
162*4882a593Smuzhiyun *flags |= OF_GPIO_ACTIVE_LOW;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun * Legacy open drain handling for fixed voltage regulators.
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_REGULATOR) &&
168*4882a593Smuzhiyun of_device_is_compatible(np, "reg-fixed-voltage") &&
169*4882a593Smuzhiyun of_property_read_bool(np, "gpio-open-drain")) {
170*4882a593Smuzhiyun *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
171*4882a593Smuzhiyun pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
172*4882a593Smuzhiyun of_node_full_name(np));
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /*
176*4882a593Smuzhiyun * Legacy handling of SPI active high chip select. If we have a
177*4882a593Smuzhiyun * property named "cs-gpios" we need to inspect the child node
178*4882a593Smuzhiyun * to determine if the flags should have inverted semantics.
179*4882a593Smuzhiyun */
180*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
181*4882a593Smuzhiyun of_property_read_bool(np, "cs-gpios")) {
182*4882a593Smuzhiyun struct device_node *child;
183*4882a593Smuzhiyun u32 cs;
184*4882a593Smuzhiyun int ret;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun for_each_child_of_node(np, child) {
187*4882a593Smuzhiyun ret = of_property_read_u32(child, "reg", &cs);
188*4882a593Smuzhiyun if (ret)
189*4882a593Smuzhiyun continue;
190*4882a593Smuzhiyun if (cs == index) {
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun * SPI children have active low chip selects
193*4882a593Smuzhiyun * by default. This can be specified negatively
194*4882a593Smuzhiyun * by just omitting "spi-cs-high" in the
195*4882a593Smuzhiyun * device node, or actively by tagging on
196*4882a593Smuzhiyun * GPIO_ACTIVE_LOW as flag in the device
197*4882a593Smuzhiyun * tree. If the line is simultaneously
198*4882a593Smuzhiyun * tagged as active low in the device tree
199*4882a593Smuzhiyun * and has the "spi-cs-high" set, we get a
200*4882a593Smuzhiyun * conflict and the "spi-cs-high" flag will
201*4882a593Smuzhiyun * take precedence.
202*4882a593Smuzhiyun */
203*4882a593Smuzhiyun if (of_property_read_bool(child, "spi-cs-high")) {
204*4882a593Smuzhiyun if (*flags & OF_GPIO_ACTIVE_LOW) {
205*4882a593Smuzhiyun pr_warn("%s GPIO handle specifies active low - ignored\n",
206*4882a593Smuzhiyun of_node_full_name(child));
207*4882a593Smuzhiyun *flags &= ~OF_GPIO_ACTIVE_LOW;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun } else {
210*4882a593Smuzhiyun if (!(*flags & OF_GPIO_ACTIVE_LOW))
211*4882a593Smuzhiyun pr_info("%s enforce active low on chipselect handle\n",
212*4882a593Smuzhiyun of_node_full_name(child));
213*4882a593Smuzhiyun *flags |= OF_GPIO_ACTIVE_LOW;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun of_node_put(child);
216*4882a593Smuzhiyun break;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* Legacy handling of stmmac's active-low PHY reset line */
222*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
223*4882a593Smuzhiyun !strcmp(propname, "snps,reset-gpio") &&
224*4882a593Smuzhiyun of_property_read_bool(np, "snps,reset-active-low"))
225*4882a593Smuzhiyun *flags |= OF_GPIO_ACTIVE_LOW;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /**
229*4882a593Smuzhiyun * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
230*4882a593Smuzhiyun * @np: device node to get GPIO from
231*4882a593Smuzhiyun * @propname: property name containing gpio specifier(s)
232*4882a593Smuzhiyun * @index: index of the GPIO
233*4882a593Smuzhiyun * @flags: a flags pointer to fill in
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
236*4882a593Smuzhiyun * value on the error condition. If @flags is not NULL the function also fills
237*4882a593Smuzhiyun * in flags for the GPIO.
238*4882a593Smuzhiyun */
of_get_named_gpiod_flags(struct device_node * np,const char * propname,int index,enum of_gpio_flags * flags)239*4882a593Smuzhiyun static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
240*4882a593Smuzhiyun const char *propname, int index, enum of_gpio_flags *flags)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun struct of_phandle_args gpiospec;
243*4882a593Smuzhiyun struct gpio_chip *chip;
244*4882a593Smuzhiyun struct gpio_desc *desc;
245*4882a593Smuzhiyun int ret;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
248*4882a593Smuzhiyun &gpiospec);
249*4882a593Smuzhiyun if (ret) {
250*4882a593Smuzhiyun pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
251*4882a593Smuzhiyun __func__, propname, np, index);
252*4882a593Smuzhiyun return ERR_PTR(ret);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun chip = of_find_gpiochip_by_xlate(&gpiospec);
256*4882a593Smuzhiyun if (!chip) {
257*4882a593Smuzhiyun desc = ERR_PTR(-EPROBE_DEFER);
258*4882a593Smuzhiyun goto out;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
262*4882a593Smuzhiyun if (IS_ERR(desc))
263*4882a593Smuzhiyun goto out;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun if (flags)
266*4882a593Smuzhiyun of_gpio_flags_quirks(np, propname, flags, index);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
269*4882a593Smuzhiyun __func__, propname, np, index,
270*4882a593Smuzhiyun PTR_ERR_OR_ZERO(desc));
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun out:
273*4882a593Smuzhiyun of_node_put(gpiospec.np);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun return desc;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
of_get_named_gpio_flags(struct device_node * np,const char * list_name,int index,enum of_gpio_flags * flags)278*4882a593Smuzhiyun int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
279*4882a593Smuzhiyun int index, enum of_gpio_flags *flags)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun struct gpio_desc *desc;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun desc = of_get_named_gpiod_flags(np, list_name, index, flags);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun if (IS_ERR(desc))
286*4882a593Smuzhiyun return PTR_ERR(desc);
287*4882a593Smuzhiyun else
288*4882a593Smuzhiyun return desc_to_gpio(desc);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /**
293*4882a593Smuzhiyun * gpiod_get_from_of_node() - obtain a GPIO from an OF node
294*4882a593Smuzhiyun * @node: handle of the OF node
295*4882a593Smuzhiyun * @propname: name of the DT property representing the GPIO
296*4882a593Smuzhiyun * @index: index of the GPIO to obtain for the consumer
297*4882a593Smuzhiyun * @dflags: GPIO initialization flags
298*4882a593Smuzhiyun * @label: label to attach to the requested GPIO
299*4882a593Smuzhiyun *
300*4882a593Smuzhiyun * Returns:
301*4882a593Smuzhiyun * On successful request the GPIO pin is configured in accordance with
302*4882a593Smuzhiyun * provided @dflags.
303*4882a593Smuzhiyun *
304*4882a593Smuzhiyun * In case of error an ERR_PTR() is returned.
305*4882a593Smuzhiyun */
gpiod_get_from_of_node(struct device_node * node,const char * propname,int index,enum gpiod_flags dflags,const char * label)306*4882a593Smuzhiyun struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
307*4882a593Smuzhiyun const char *propname, int index,
308*4882a593Smuzhiyun enum gpiod_flags dflags,
309*4882a593Smuzhiyun const char *label)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
312*4882a593Smuzhiyun struct gpio_desc *desc;
313*4882a593Smuzhiyun enum of_gpio_flags flags;
314*4882a593Smuzhiyun bool active_low = false;
315*4882a593Smuzhiyun bool single_ended = false;
316*4882a593Smuzhiyun bool open_drain = false;
317*4882a593Smuzhiyun bool transitory = false;
318*4882a593Smuzhiyun int ret;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun desc = of_get_named_gpiod_flags(node, propname,
321*4882a593Smuzhiyun index, &flags);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (!desc || IS_ERR(desc)) {
324*4882a593Smuzhiyun return desc;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun active_low = flags & OF_GPIO_ACTIVE_LOW;
328*4882a593Smuzhiyun single_ended = flags & OF_GPIO_SINGLE_ENDED;
329*4882a593Smuzhiyun open_drain = flags & OF_GPIO_OPEN_DRAIN;
330*4882a593Smuzhiyun transitory = flags & OF_GPIO_TRANSITORY;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun ret = gpiod_request(desc, label);
333*4882a593Smuzhiyun if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
334*4882a593Smuzhiyun return desc;
335*4882a593Smuzhiyun if (ret)
336*4882a593Smuzhiyun return ERR_PTR(ret);
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun if (active_low)
339*4882a593Smuzhiyun lflags |= GPIO_ACTIVE_LOW;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun if (single_ended) {
342*4882a593Smuzhiyun if (open_drain)
343*4882a593Smuzhiyun lflags |= GPIO_OPEN_DRAIN;
344*4882a593Smuzhiyun else
345*4882a593Smuzhiyun lflags |= GPIO_OPEN_SOURCE;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun if (transitory)
349*4882a593Smuzhiyun lflags |= GPIO_TRANSITORY;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun if (flags & OF_GPIO_PULL_UP)
352*4882a593Smuzhiyun lflags |= GPIO_PULL_UP;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun if (flags & OF_GPIO_PULL_DOWN)
355*4882a593Smuzhiyun lflags |= GPIO_PULL_DOWN;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun ret = gpiod_configure_flags(desc, propname, lflags, dflags);
358*4882a593Smuzhiyun if (ret < 0) {
359*4882a593Smuzhiyun gpiod_put(desc);
360*4882a593Smuzhiyun return ERR_PTR(ret);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun return desc;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun * The SPI GPIO bindings happened before we managed to establish that GPIO
369*4882a593Smuzhiyun * properties should be named "foo-gpios" so we have this special kludge for
370*4882a593Smuzhiyun * them.
371*4882a593Smuzhiyun */
of_find_spi_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)372*4882a593Smuzhiyun static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
373*4882a593Smuzhiyun enum of_gpio_flags *of_flags)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun char prop_name[32]; /* 32 is max size of property name */
376*4882a593Smuzhiyun struct device_node *np = dev->of_node;
377*4882a593Smuzhiyun struct gpio_desc *desc;
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /*
380*4882a593Smuzhiyun * Hopefully the compiler stubs the rest of the function if this
381*4882a593Smuzhiyun * is false.
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_SPI_MASTER))
384*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /* Allow this specifically for "spi-gpio" devices */
387*4882a593Smuzhiyun if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
388*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
391*4882a593Smuzhiyun snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
394*4882a593Smuzhiyun return desc;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /*
398*4882a593Smuzhiyun * The old Freescale bindings use simply "gpios" as name for the chip select
399*4882a593Smuzhiyun * lines rather than "cs-gpios" like all other SPI hardware. Account for this
400*4882a593Smuzhiyun * with a special quirk.
401*4882a593Smuzhiyun */
of_find_spi_cs_gpio(struct device * dev,const char * con_id,unsigned int idx,unsigned long * flags)402*4882a593Smuzhiyun static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
403*4882a593Smuzhiyun const char *con_id,
404*4882a593Smuzhiyun unsigned int idx,
405*4882a593Smuzhiyun unsigned long *flags)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun struct device_node *np = dev->of_node;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_SPI_MASTER))
410*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun /* Allow this specifically for Freescale and PPC devices */
413*4882a593Smuzhiyun if (!of_device_is_compatible(np, "fsl,spi") &&
414*4882a593Smuzhiyun !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
415*4882a593Smuzhiyun !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
416*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
417*4882a593Smuzhiyun /* Allow only if asking for "cs-gpios" */
418*4882a593Smuzhiyun if (!con_id || strcmp(con_id, "cs"))
419*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /*
422*4882a593Smuzhiyun * While all other SPI controllers use "cs-gpios" the Freescale
423*4882a593Smuzhiyun * uses just "gpios" so translate to that when "cs-gpios" is
424*4882a593Smuzhiyun * requested.
425*4882a593Smuzhiyun */
426*4882a593Smuzhiyun return of_find_gpio(dev, NULL, idx, flags);
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun /*
430*4882a593Smuzhiyun * Some regulator bindings happened before we managed to establish that GPIO
431*4882a593Smuzhiyun * properties should be named "foo-gpios" so we have this special kludge for
432*4882a593Smuzhiyun * them.
433*4882a593Smuzhiyun */
of_find_regulator_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)434*4882a593Smuzhiyun static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
435*4882a593Smuzhiyun enum of_gpio_flags *of_flags)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun /* These are the connection IDs we accept as legacy GPIO phandles */
438*4882a593Smuzhiyun const char *whitelist[] = {
439*4882a593Smuzhiyun "wlf,ldoena", /* Arizona */
440*4882a593Smuzhiyun "wlf,ldo1ena", /* WM8994 */
441*4882a593Smuzhiyun "wlf,ldo2ena", /* WM8994 */
442*4882a593Smuzhiyun };
443*4882a593Smuzhiyun struct device_node *np = dev->of_node;
444*4882a593Smuzhiyun struct gpio_desc *desc;
445*4882a593Smuzhiyun int i;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_REGULATOR))
448*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun if (!con_id)
451*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
454*4882a593Smuzhiyun if (i < 0)
455*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
458*4882a593Smuzhiyun return desc;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun
of_find_arizona_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)461*4882a593Smuzhiyun static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
462*4882a593Smuzhiyun const char *con_id,
463*4882a593Smuzhiyun enum of_gpio_flags *of_flags)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
466*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun if (!con_id || strcmp(con_id, "wlf,reset"))
469*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
of_find_usb_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)474*4882a593Smuzhiyun static struct gpio_desc *of_find_usb_gpio(struct device *dev,
475*4882a593Smuzhiyun const char *con_id,
476*4882a593Smuzhiyun enum of_gpio_flags *of_flags)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun /*
479*4882a593Smuzhiyun * Currently this USB quirk is only for the Fairchild FUSB302 host which is using
480*4882a593Smuzhiyun * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios"
481*4882a593Smuzhiyun * suffix.
482*4882a593Smuzhiyun */
483*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_TYPEC_FUSB302))
484*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun if (!con_id || strcmp(con_id, "fcs,int_n"))
487*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
of_find_gpio(struct device * dev,const char * con_id,unsigned int idx,unsigned long * flags)492*4882a593Smuzhiyun struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
493*4882a593Smuzhiyun unsigned int idx, unsigned long *flags)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun char prop_name[32]; /* 32 is max size of property name */
496*4882a593Smuzhiyun enum of_gpio_flags of_flags;
497*4882a593Smuzhiyun struct gpio_desc *desc;
498*4882a593Smuzhiyun unsigned int i;
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /* Try GPIO property "foo-gpios" and "foo-gpio" */
501*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
502*4882a593Smuzhiyun if (con_id)
503*4882a593Smuzhiyun snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
504*4882a593Smuzhiyun gpio_suffixes[i]);
505*4882a593Smuzhiyun else
506*4882a593Smuzhiyun snprintf(prop_name, sizeof(prop_name), "%s",
507*4882a593Smuzhiyun gpio_suffixes[i]);
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
510*4882a593Smuzhiyun &of_flags);
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
513*4882a593Smuzhiyun break;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun if (PTR_ERR(desc) == -ENOENT) {
517*4882a593Smuzhiyun /* Special handling for SPI GPIOs if used */
518*4882a593Smuzhiyun desc = of_find_spi_gpio(dev, con_id, &of_flags);
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun if (PTR_ERR(desc) == -ENOENT) {
522*4882a593Smuzhiyun /* This quirk looks up flags and all */
523*4882a593Smuzhiyun desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
524*4882a593Smuzhiyun if (!IS_ERR(desc))
525*4882a593Smuzhiyun return desc;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun if (PTR_ERR(desc) == -ENOENT) {
529*4882a593Smuzhiyun /* Special handling for regulator GPIOs if used */
530*4882a593Smuzhiyun desc = of_find_regulator_gpio(dev, con_id, &of_flags);
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun if (PTR_ERR(desc) == -ENOENT)
534*4882a593Smuzhiyun desc = of_find_arizona_gpio(dev, con_id, &of_flags);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun if (PTR_ERR(desc) == -ENOENT)
537*4882a593Smuzhiyun desc = of_find_usb_gpio(dev, con_id, &of_flags);
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun if (IS_ERR(desc))
540*4882a593Smuzhiyun return desc;
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun if (of_flags & OF_GPIO_ACTIVE_LOW)
543*4882a593Smuzhiyun *flags |= GPIO_ACTIVE_LOW;
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun if (of_flags & OF_GPIO_SINGLE_ENDED) {
546*4882a593Smuzhiyun if (of_flags & OF_GPIO_OPEN_DRAIN)
547*4882a593Smuzhiyun *flags |= GPIO_OPEN_DRAIN;
548*4882a593Smuzhiyun else
549*4882a593Smuzhiyun *flags |= GPIO_OPEN_SOURCE;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun if (of_flags & OF_GPIO_TRANSITORY)
553*4882a593Smuzhiyun *flags |= GPIO_TRANSITORY;
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun if (of_flags & OF_GPIO_PULL_UP)
556*4882a593Smuzhiyun *flags |= GPIO_PULL_UP;
557*4882a593Smuzhiyun if (of_flags & OF_GPIO_PULL_DOWN)
558*4882a593Smuzhiyun *flags |= GPIO_PULL_DOWN;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun return desc;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun /**
564*4882a593Smuzhiyun * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
565*4882a593Smuzhiyun * @np: device node to get GPIO from
566*4882a593Smuzhiyun * @chip: GPIO chip whose hog is parsed
567*4882a593Smuzhiyun * @idx: Index of the GPIO to parse
568*4882a593Smuzhiyun * @name: GPIO line name
569*4882a593Smuzhiyun * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
570*4882a593Smuzhiyun * of_find_gpio() or of_parse_own_gpio()
571*4882a593Smuzhiyun * @dflags: gpiod_flags - optional GPIO initialization flags
572*4882a593Smuzhiyun *
573*4882a593Smuzhiyun * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
574*4882a593Smuzhiyun * value on the error condition.
575*4882a593Smuzhiyun */
of_parse_own_gpio(struct device_node * np,struct gpio_chip * chip,unsigned int idx,const char ** name,unsigned long * lflags,enum gpiod_flags * dflags)576*4882a593Smuzhiyun static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
577*4882a593Smuzhiyun struct gpio_chip *chip,
578*4882a593Smuzhiyun unsigned int idx, const char **name,
579*4882a593Smuzhiyun unsigned long *lflags,
580*4882a593Smuzhiyun enum gpiod_flags *dflags)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun struct device_node *chip_np;
583*4882a593Smuzhiyun enum of_gpio_flags xlate_flags;
584*4882a593Smuzhiyun struct of_phandle_args gpiospec;
585*4882a593Smuzhiyun struct gpio_desc *desc;
586*4882a593Smuzhiyun unsigned int i;
587*4882a593Smuzhiyun u32 tmp;
588*4882a593Smuzhiyun int ret;
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun chip_np = chip->of_node;
591*4882a593Smuzhiyun if (!chip_np)
592*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun xlate_flags = 0;
595*4882a593Smuzhiyun *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
596*4882a593Smuzhiyun *dflags = 0;
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
599*4882a593Smuzhiyun if (ret)
600*4882a593Smuzhiyun return ERR_PTR(ret);
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun gpiospec.np = chip_np;
603*4882a593Smuzhiyun gpiospec.args_count = tmp;
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun for (i = 0; i < tmp; i++) {
606*4882a593Smuzhiyun ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
607*4882a593Smuzhiyun &gpiospec.args[i]);
608*4882a593Smuzhiyun if (ret)
609*4882a593Smuzhiyun return ERR_PTR(ret);
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
613*4882a593Smuzhiyun if (IS_ERR(desc))
614*4882a593Smuzhiyun return desc;
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun if (xlate_flags & OF_GPIO_ACTIVE_LOW)
617*4882a593Smuzhiyun *lflags |= GPIO_ACTIVE_LOW;
618*4882a593Smuzhiyun if (xlate_flags & OF_GPIO_TRANSITORY)
619*4882a593Smuzhiyun *lflags |= GPIO_TRANSITORY;
620*4882a593Smuzhiyun if (xlate_flags & OF_GPIO_PULL_UP)
621*4882a593Smuzhiyun *lflags |= GPIO_PULL_UP;
622*4882a593Smuzhiyun if (xlate_flags & OF_GPIO_PULL_DOWN)
623*4882a593Smuzhiyun *lflags |= GPIO_PULL_DOWN;
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun if (of_property_read_bool(np, "input"))
626*4882a593Smuzhiyun *dflags |= GPIOD_IN;
627*4882a593Smuzhiyun else if (of_property_read_bool(np, "output-low"))
628*4882a593Smuzhiyun *dflags |= GPIOD_OUT_LOW;
629*4882a593Smuzhiyun else if (of_property_read_bool(np, "output-high"))
630*4882a593Smuzhiyun *dflags |= GPIOD_OUT_HIGH;
631*4882a593Smuzhiyun else {
632*4882a593Smuzhiyun pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
633*4882a593Smuzhiyun desc_to_gpio(desc), np);
634*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun if (name && of_property_read_string(np, "line-name", name))
638*4882a593Smuzhiyun *name = np->name;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun return desc;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun /**
644*4882a593Smuzhiyun * of_gpiochip_add_hog - Add all hogs in a hog device node
645*4882a593Smuzhiyun * @chip: gpio chip to act on
646*4882a593Smuzhiyun * @hog: device node describing the hogs
647*4882a593Smuzhiyun *
648*4882a593Smuzhiyun * Returns error if it fails otherwise 0 on success.
649*4882a593Smuzhiyun */
of_gpiochip_add_hog(struct gpio_chip * chip,struct device_node * hog)650*4882a593Smuzhiyun static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun enum gpiod_flags dflags;
653*4882a593Smuzhiyun struct gpio_desc *desc;
654*4882a593Smuzhiyun unsigned long lflags;
655*4882a593Smuzhiyun const char *name;
656*4882a593Smuzhiyun unsigned int i;
657*4882a593Smuzhiyun int ret;
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun for (i = 0;; i++) {
660*4882a593Smuzhiyun desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
661*4882a593Smuzhiyun if (IS_ERR(desc))
662*4882a593Smuzhiyun break;
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun ret = gpiod_hog(desc, name, lflags, dflags);
665*4882a593Smuzhiyun if (ret < 0)
666*4882a593Smuzhiyun return ret;
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun #ifdef CONFIG_OF_DYNAMIC
669*4882a593Smuzhiyun desc->hog = hog;
670*4882a593Smuzhiyun #endif
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun return 0;
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun /**
677*4882a593Smuzhiyun * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
678*4882a593Smuzhiyun * @chip: gpio chip to act on
679*4882a593Smuzhiyun *
680*4882a593Smuzhiyun * This is only used by of_gpiochip_add to request/set GPIO initial
681*4882a593Smuzhiyun * configuration.
682*4882a593Smuzhiyun * It returns error if it fails otherwise 0 on success.
683*4882a593Smuzhiyun */
of_gpiochip_scan_gpios(struct gpio_chip * chip)684*4882a593Smuzhiyun static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun struct device_node *np;
687*4882a593Smuzhiyun int ret;
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun for_each_available_child_of_node(chip->of_node, np) {
690*4882a593Smuzhiyun if (!of_property_read_bool(np, "gpio-hog"))
691*4882a593Smuzhiyun continue;
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun ret = of_gpiochip_add_hog(chip, np);
694*4882a593Smuzhiyun if (ret < 0) {
695*4882a593Smuzhiyun of_node_put(np);
696*4882a593Smuzhiyun return ret;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun of_node_set_flag(np, OF_POPULATED);
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun return 0;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun #ifdef CONFIG_OF_DYNAMIC
706*4882a593Smuzhiyun /**
707*4882a593Smuzhiyun * of_gpiochip_remove_hog - Remove all hogs in a hog device node
708*4882a593Smuzhiyun * @chip: gpio chip to act on
709*4882a593Smuzhiyun * @hog: device node describing the hogs
710*4882a593Smuzhiyun */
of_gpiochip_remove_hog(struct gpio_chip * chip,struct device_node * hog)711*4882a593Smuzhiyun static void of_gpiochip_remove_hog(struct gpio_chip *chip,
712*4882a593Smuzhiyun struct device_node *hog)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun struct gpio_desc *descs = chip->gpiodev->descs;
715*4882a593Smuzhiyun unsigned int i;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun for (i = 0; i < chip->ngpio; i++) {
718*4882a593Smuzhiyun if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) &&
719*4882a593Smuzhiyun descs[i].hog == hog)
720*4882a593Smuzhiyun gpiochip_free_own_desc(&descs[i]);
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun
of_gpiochip_match_node(struct gpio_chip * chip,void * data)724*4882a593Smuzhiyun static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun return chip->gpiodev->dev.of_node == data;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun
of_find_gpiochip_by_node(struct device_node * np)729*4882a593Smuzhiyun static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun return gpiochip_find(np, of_gpiochip_match_node);
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun
of_gpio_notify(struct notifier_block * nb,unsigned long action,void * arg)734*4882a593Smuzhiyun static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
735*4882a593Smuzhiyun void *arg)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun struct of_reconfig_data *rd = arg;
738*4882a593Smuzhiyun struct gpio_chip *chip;
739*4882a593Smuzhiyun int ret;
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun /*
742*4882a593Smuzhiyun * This only supports adding and removing complete gpio-hog nodes.
743*4882a593Smuzhiyun * Modifying an existing gpio-hog node is not supported (except for
744*4882a593Smuzhiyun * changing its "status" property, which is treated the same as
745*4882a593Smuzhiyun * addition/removal).
746*4882a593Smuzhiyun */
747*4882a593Smuzhiyun switch (of_reconfig_get_state_change(action, arg)) {
748*4882a593Smuzhiyun case OF_RECONFIG_CHANGE_ADD:
749*4882a593Smuzhiyun if (!of_property_read_bool(rd->dn, "gpio-hog"))
750*4882a593Smuzhiyun return NOTIFY_OK; /* not for us */
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
753*4882a593Smuzhiyun return NOTIFY_OK;
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun chip = of_find_gpiochip_by_node(rd->dn->parent);
756*4882a593Smuzhiyun if (chip == NULL)
757*4882a593Smuzhiyun return NOTIFY_OK; /* not for us */
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun ret = of_gpiochip_add_hog(chip, rd->dn);
760*4882a593Smuzhiyun if (ret < 0) {
761*4882a593Smuzhiyun pr_err("%s: failed to add hogs for %pOF\n", __func__,
762*4882a593Smuzhiyun rd->dn);
763*4882a593Smuzhiyun of_node_clear_flag(rd->dn, OF_POPULATED);
764*4882a593Smuzhiyun return notifier_from_errno(ret);
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun break;
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun case OF_RECONFIG_CHANGE_REMOVE:
769*4882a593Smuzhiyun if (!of_node_check_flag(rd->dn, OF_POPULATED))
770*4882a593Smuzhiyun return NOTIFY_OK; /* already depopulated */
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun chip = of_find_gpiochip_by_node(rd->dn->parent);
773*4882a593Smuzhiyun if (chip == NULL)
774*4882a593Smuzhiyun return NOTIFY_OK; /* not for us */
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun of_gpiochip_remove_hog(chip, rd->dn);
777*4882a593Smuzhiyun of_node_clear_flag(rd->dn, OF_POPULATED);
778*4882a593Smuzhiyun break;
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun return NOTIFY_OK;
782*4882a593Smuzhiyun }
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun struct notifier_block gpio_of_notifier = {
785*4882a593Smuzhiyun .notifier_call = of_gpio_notify,
786*4882a593Smuzhiyun };
787*4882a593Smuzhiyun #endif /* CONFIG_OF_DYNAMIC */
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun /**
790*4882a593Smuzhiyun * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
791*4882a593Smuzhiyun * @gc: pointer to the gpio_chip structure
792*4882a593Smuzhiyun * @gpiospec: GPIO specifier as found in the device tree
793*4882a593Smuzhiyun * @flags: a flags pointer to fill in
794*4882a593Smuzhiyun *
795*4882a593Smuzhiyun * This is simple translation function, suitable for the most 1:1 mapped
796*4882a593Smuzhiyun * GPIO chips. This function performs only one sanity check: whether GPIO
797*4882a593Smuzhiyun * is less than ngpios (that is specified in the gpio_chip).
798*4882a593Smuzhiyun */
of_gpio_simple_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)799*4882a593Smuzhiyun static int of_gpio_simple_xlate(struct gpio_chip *gc,
800*4882a593Smuzhiyun const struct of_phandle_args *gpiospec,
801*4882a593Smuzhiyun u32 *flags)
802*4882a593Smuzhiyun {
803*4882a593Smuzhiyun /*
804*4882a593Smuzhiyun * We're discouraging gpio_cells < 2, since that way you'll have to
805*4882a593Smuzhiyun * write your own xlate function (that will have to retrieve the GPIO
806*4882a593Smuzhiyun * number and the flags from a single gpio cell -- this is possible,
807*4882a593Smuzhiyun * but not recommended).
808*4882a593Smuzhiyun */
809*4882a593Smuzhiyun if (gc->of_gpio_n_cells < 2) {
810*4882a593Smuzhiyun WARN_ON(1);
811*4882a593Smuzhiyun return -EINVAL;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
815*4882a593Smuzhiyun return -EINVAL;
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun if (gpiospec->args[0] >= gc->ngpio)
818*4882a593Smuzhiyun return -EINVAL;
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun if (flags)
821*4882a593Smuzhiyun *flags = gpiospec->args[1];
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun return gpiospec->args[0];
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun /**
827*4882a593Smuzhiyun * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
828*4882a593Smuzhiyun * @np: device node of the GPIO chip
829*4882a593Smuzhiyun * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
830*4882a593Smuzhiyun * @data: driver data to store in the struct gpio_chip
831*4882a593Smuzhiyun *
832*4882a593Smuzhiyun * To use this function you should allocate and fill mm_gc with:
833*4882a593Smuzhiyun *
834*4882a593Smuzhiyun * 1) In the gpio_chip structure:
835*4882a593Smuzhiyun * - all the callbacks
836*4882a593Smuzhiyun * - of_gpio_n_cells
837*4882a593Smuzhiyun * - of_xlate callback (optional)
838*4882a593Smuzhiyun *
839*4882a593Smuzhiyun * 3) In the of_mm_gpio_chip structure:
840*4882a593Smuzhiyun * - save_regs callback (optional)
841*4882a593Smuzhiyun *
842*4882a593Smuzhiyun * If succeeded, this function will map bank's memory and will
843*4882a593Smuzhiyun * do all necessary work for you. Then you'll able to use .regs
844*4882a593Smuzhiyun * to manage GPIOs from the callbacks.
845*4882a593Smuzhiyun */
of_mm_gpiochip_add_data(struct device_node * np,struct of_mm_gpio_chip * mm_gc,void * data)846*4882a593Smuzhiyun int of_mm_gpiochip_add_data(struct device_node *np,
847*4882a593Smuzhiyun struct of_mm_gpio_chip *mm_gc,
848*4882a593Smuzhiyun void *data)
849*4882a593Smuzhiyun {
850*4882a593Smuzhiyun int ret = -ENOMEM;
851*4882a593Smuzhiyun struct gpio_chip *gc = &mm_gc->gc;
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
854*4882a593Smuzhiyun if (!gc->label)
855*4882a593Smuzhiyun goto err0;
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun mm_gc->regs = of_iomap(np, 0);
858*4882a593Smuzhiyun if (!mm_gc->regs)
859*4882a593Smuzhiyun goto err1;
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun gc->base = -1;
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun if (mm_gc->save_regs)
864*4882a593Smuzhiyun mm_gc->save_regs(mm_gc);
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun of_node_put(mm_gc->gc.of_node);
867*4882a593Smuzhiyun mm_gc->gc.of_node = of_node_get(np);
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun ret = gpiochip_add_data(gc, data);
870*4882a593Smuzhiyun if (ret)
871*4882a593Smuzhiyun goto err2;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun return 0;
874*4882a593Smuzhiyun err2:
875*4882a593Smuzhiyun of_node_put(np);
876*4882a593Smuzhiyun iounmap(mm_gc->regs);
877*4882a593Smuzhiyun err1:
878*4882a593Smuzhiyun kfree(gc->label);
879*4882a593Smuzhiyun err0:
880*4882a593Smuzhiyun pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
881*4882a593Smuzhiyun return ret;
882*4882a593Smuzhiyun }
883*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun /**
886*4882a593Smuzhiyun * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
887*4882a593Smuzhiyun * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
888*4882a593Smuzhiyun */
of_mm_gpiochip_remove(struct of_mm_gpio_chip * mm_gc)889*4882a593Smuzhiyun void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
890*4882a593Smuzhiyun {
891*4882a593Smuzhiyun struct gpio_chip *gc = &mm_gc->gc;
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun if (!mm_gc)
894*4882a593Smuzhiyun return;
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun gpiochip_remove(gc);
897*4882a593Smuzhiyun iounmap(mm_gc->regs);
898*4882a593Smuzhiyun kfree(gc->label);
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
901*4882a593Smuzhiyun
of_gpiochip_init_valid_mask(struct gpio_chip * chip)902*4882a593Smuzhiyun static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun int len, i;
905*4882a593Smuzhiyun u32 start, count;
906*4882a593Smuzhiyun struct device_node *np = chip->of_node;
907*4882a593Smuzhiyun
908*4882a593Smuzhiyun len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
909*4882a593Smuzhiyun if (len < 0 || len % 2 != 0)
910*4882a593Smuzhiyun return;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun for (i = 0; i < len; i += 2) {
913*4882a593Smuzhiyun of_property_read_u32_index(np, "gpio-reserved-ranges",
914*4882a593Smuzhiyun i, &start);
915*4882a593Smuzhiyun of_property_read_u32_index(np, "gpio-reserved-ranges",
916*4882a593Smuzhiyun i + 1, &count);
917*4882a593Smuzhiyun if (start >= chip->ngpio || start + count > chip->ngpio)
918*4882a593Smuzhiyun continue;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun bitmap_clear(chip->valid_mask, start, count);
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun };
923*4882a593Smuzhiyun
924*4882a593Smuzhiyun #ifdef CONFIG_PINCTRL
of_gpiochip_add_pin_range(struct gpio_chip * chip)925*4882a593Smuzhiyun static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
926*4882a593Smuzhiyun {
927*4882a593Smuzhiyun struct device_node *np = chip->of_node;
928*4882a593Smuzhiyun struct of_phandle_args pinspec;
929*4882a593Smuzhiyun struct pinctrl_dev *pctldev;
930*4882a593Smuzhiyun int index = 0, ret;
931*4882a593Smuzhiyun const char *name;
932*4882a593Smuzhiyun static const char group_names_propname[] = "gpio-ranges-group-names";
933*4882a593Smuzhiyun struct property *group_names;
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun if (!np)
936*4882a593Smuzhiyun return 0;
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun group_names = of_find_property(np, group_names_propname, NULL);
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun for (;; index++) {
941*4882a593Smuzhiyun ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
942*4882a593Smuzhiyun index, &pinspec);
943*4882a593Smuzhiyun if (ret)
944*4882a593Smuzhiyun break;
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun pctldev = of_pinctrl_get(pinspec.np);
947*4882a593Smuzhiyun of_node_put(pinspec.np);
948*4882a593Smuzhiyun if (!pctldev)
949*4882a593Smuzhiyun return -EPROBE_DEFER;
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun if (pinspec.args[2]) {
952*4882a593Smuzhiyun if (group_names) {
953*4882a593Smuzhiyun of_property_read_string_index(np,
954*4882a593Smuzhiyun group_names_propname,
955*4882a593Smuzhiyun index, &name);
956*4882a593Smuzhiyun if (strlen(name)) {
957*4882a593Smuzhiyun pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
958*4882a593Smuzhiyun np);
959*4882a593Smuzhiyun break;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun /* npins != 0: linear range */
963*4882a593Smuzhiyun ret = gpiochip_add_pin_range(chip,
964*4882a593Smuzhiyun pinctrl_dev_get_devname(pctldev),
965*4882a593Smuzhiyun pinspec.args[0],
966*4882a593Smuzhiyun pinspec.args[1],
967*4882a593Smuzhiyun pinspec.args[2]);
968*4882a593Smuzhiyun if (ret)
969*4882a593Smuzhiyun return ret;
970*4882a593Smuzhiyun } else {
971*4882a593Smuzhiyun /* npins == 0: special range */
972*4882a593Smuzhiyun if (pinspec.args[1]) {
973*4882a593Smuzhiyun pr_err("%pOF: Illegal gpio-range format.\n",
974*4882a593Smuzhiyun np);
975*4882a593Smuzhiyun break;
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun if (!group_names) {
979*4882a593Smuzhiyun pr_err("%pOF: GPIO group range requested but no %s property.\n",
980*4882a593Smuzhiyun np, group_names_propname);
981*4882a593Smuzhiyun break;
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun ret = of_property_read_string_index(np,
985*4882a593Smuzhiyun group_names_propname,
986*4882a593Smuzhiyun index, &name);
987*4882a593Smuzhiyun if (ret)
988*4882a593Smuzhiyun break;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun if (!strlen(name)) {
991*4882a593Smuzhiyun pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
992*4882a593Smuzhiyun np);
993*4882a593Smuzhiyun break;
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun ret = gpiochip_add_pingroup_range(chip, pctldev,
997*4882a593Smuzhiyun pinspec.args[0], name);
998*4882a593Smuzhiyun if (ret)
999*4882a593Smuzhiyun return ret;
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun return 0;
1004*4882a593Smuzhiyun }
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun #else
of_gpiochip_add_pin_range(struct gpio_chip * chip)1007*4882a593Smuzhiyun static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1008*4882a593Smuzhiyun #endif
1009*4882a593Smuzhiyun
of_gpiochip_add(struct gpio_chip * chip)1010*4882a593Smuzhiyun int of_gpiochip_add(struct gpio_chip *chip)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun int ret;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun if (!chip->of_node)
1015*4882a593Smuzhiyun return 0;
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun if (!chip->of_xlate) {
1018*4882a593Smuzhiyun chip->of_gpio_n_cells = 2;
1019*4882a593Smuzhiyun chip->of_xlate = of_gpio_simple_xlate;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1023*4882a593Smuzhiyun return -EINVAL;
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun of_gpiochip_init_valid_mask(chip);
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun ret = of_gpiochip_add_pin_range(chip);
1028*4882a593Smuzhiyun if (ret)
1029*4882a593Smuzhiyun return ret;
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun of_node_get(chip->of_node);
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun ret = of_gpiochip_scan_gpios(chip);
1034*4882a593Smuzhiyun if (ret)
1035*4882a593Smuzhiyun of_node_put(chip->of_node);
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun return ret;
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun
of_gpiochip_remove(struct gpio_chip * chip)1040*4882a593Smuzhiyun void of_gpiochip_remove(struct gpio_chip *chip)
1041*4882a593Smuzhiyun {
1042*4882a593Smuzhiyun of_node_put(chip->of_node);
1043*4882a593Smuzhiyun }
1044*4882a593Smuzhiyun
of_gpio_dev_init(struct gpio_chip * gc,struct gpio_device * gdev)1045*4882a593Smuzhiyun void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun /* If the gpiochip has an assigned OF node this takes precedence */
1048*4882a593Smuzhiyun if (gc->of_node)
1049*4882a593Smuzhiyun gdev->dev.of_node = gc->of_node;
1050*4882a593Smuzhiyun else
1051*4882a593Smuzhiyun gc->of_node = gdev->dev.of_node;
1052*4882a593Smuzhiyun if (gdev->dev.of_node)
1053*4882a593Smuzhiyun gdev->dev.fwnode = of_fwnode_handle(gdev->dev.of_node);
1054*4882a593Smuzhiyun }
1055