xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision c7e9d398)
1f141ed65SGrant Likely /*
2f141ed65SGrant Likely  * OF helpers for the GPIO API
3f141ed65SGrant Likely  *
4f141ed65SGrant Likely  * Copyright (c) 2007-2008  MontaVista Software, Inc.
5f141ed65SGrant Likely  *
6f141ed65SGrant Likely  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7f141ed65SGrant Likely  *
8f141ed65SGrant Likely  * This program is free software; you can redistribute it and/or modify
9f141ed65SGrant Likely  * it under the terms of the GNU General Public License as published by
10f141ed65SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
11f141ed65SGrant Likely  * (at your option) any later version.
12f141ed65SGrant Likely  */
13f141ed65SGrant Likely 
14f141ed65SGrant Likely #include <linux/device.h>
15bea4dbeeSSachin Kamat #include <linux/err.h>
16f141ed65SGrant Likely #include <linux/errno.h>
17f141ed65SGrant Likely #include <linux/module.h>
18f141ed65SGrant Likely #include <linux/io.h>
19af8b6375SAlexandre Courbot #include <linux/gpio/consumer.h>
20f141ed65SGrant Likely #include <linux/of.h>
21f141ed65SGrant Likely #include <linux/of_address.h>
22f141ed65SGrant Likely #include <linux/of_gpio.h>
23f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h>
24f141ed65SGrant Likely #include <linux/slab.h>
25f625d460SBenoit Parrot #include <linux/gpio/machine.h>
26f141ed65SGrant Likely 
271bd6b601SAlexandre Courbot #include "gpiolib.h"
28af8b6375SAlexandre Courbot 
29c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
303d0f7cf0SGrant Likely {
31c7e9d398SMasahiro Yamada 	struct of_phandle_args *gpiospec = data;
32c7e9d398SMasahiro Yamada 
33c7e9d398SMasahiro Yamada 	return chip->gpiodev->dev.of_node == gpiospec->np &&
34c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
357b8792bbSHans Holmberg }
363d0f7cf0SGrant Likely 
37c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
38c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
39762c2e46SMasahiro Yamada {
40c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
413d0f7cf0SGrant Likely }
423d0f7cf0SGrant Likely 
4399468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
4499468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
4599468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
4699468c1aSMasahiro Yamada {
4799468c1aSMasahiro Yamada 	int ret;
4899468c1aSMasahiro Yamada 
4999468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
5099468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
5199468c1aSMasahiro Yamada 
5299468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
5399468c1aSMasahiro Yamada 	if (ret < 0)
5499468c1aSMasahiro Yamada 		return ERR_PTR(ret);
5599468c1aSMasahiro Yamada 
5699468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
57f141ed65SGrant Likely }
58f141ed65SGrant Likely 
59f141ed65SGrant Likely /**
60af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
61f141ed65SGrant Likely  * @np:		device node to get GPIO from
62f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
63f141ed65SGrant Likely  * @index:	index of the GPIO
64f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
65f141ed65SGrant Likely  *
66af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
67f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
68f141ed65SGrant Likely  * in flags for the GPIO.
69f141ed65SGrant Likely  */
70af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
71af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
72f141ed65SGrant Likely {
73762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
74762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
75762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
76f141ed65SGrant Likely 	int ret;
77f141ed65SGrant Likely 
783d0f7cf0SGrant Likely 	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
79762c2e46SMasahiro Yamada 					 &gpiospec);
803d0f7cf0SGrant Likely 	if (ret) {
8185ea29acSTushar Behera 		pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
8285ea29acSTushar Behera 			__func__, propname, np->full_name, index);
83af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
843d0f7cf0SGrant Likely 	}
85f141ed65SGrant Likely 
86c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
87762c2e46SMasahiro Yamada 	if (!chip) {
88762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
89762c2e46SMasahiro Yamada 		goto out;
90762c2e46SMasahiro Yamada 	}
913d0f7cf0SGrant Likely 
9299468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
93762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
94762c2e46SMasahiro Yamada 		goto out;
95762c2e46SMasahiro Yamada 
9685ea29acSTushar Behera 	pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
9785ea29acSTushar Behera 		 __func__, propname, np->full_name, index,
98762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
99762c2e46SMasahiro Yamada 
100762c2e46SMasahiro Yamada out:
101762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
102762c2e46SMasahiro Yamada 
103762c2e46SMasahiro Yamada 	return desc;
104f141ed65SGrant Likely }
105f141ed65SGrant Likely 
106f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
107f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
108f01d9075SAlexandre Courbot {
109f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
110f01d9075SAlexandre Courbot 
111f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
112f01d9075SAlexandre Courbot 
113f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
114f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
115f01d9075SAlexandre Courbot 	else
116f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
117f01d9075SAlexandre Courbot }
118f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags);
119f01d9075SAlexandre Courbot 
120ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
121ea713bc4SLinus Walleij 			       unsigned int idx,
122ea713bc4SLinus Walleij 			       enum gpio_lookup_flags *flags)
123ea713bc4SLinus Walleij {
124ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
125ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
126ea713bc4SLinus Walleij 	struct gpio_desc *desc;
127ea713bc4SLinus Walleij 	unsigned int i;
128ea713bc4SLinus Walleij 
129ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
130ea713bc4SLinus Walleij 		if (con_id)
131ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
132ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
133ea713bc4SLinus Walleij 		else
134ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
135ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
136ea713bc4SLinus Walleij 
137ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
138ea713bc4SLinus Walleij 						&of_flags);
139ea713bc4SLinus Walleij 		if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
140ea713bc4SLinus Walleij 			break;
141ea713bc4SLinus Walleij 	}
142ea713bc4SLinus Walleij 
143ea713bc4SLinus Walleij 	if (IS_ERR(desc))
144ea713bc4SLinus Walleij 		return desc;
145ea713bc4SLinus Walleij 
146ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_ACTIVE_LOW)
147ea713bc4SLinus Walleij 		*flags |= GPIO_ACTIVE_LOW;
148ea713bc4SLinus Walleij 
149ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
150ea713bc4SLinus Walleij 		if (of_flags & OF_GPIO_ACTIVE_LOW)
151ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_DRAIN;
152ea713bc4SLinus Walleij 		else
153ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_SOURCE;
154ea713bc4SLinus Walleij 	}
155ea713bc4SLinus Walleij 
156ea713bc4SLinus Walleij 	return desc;
157ea713bc4SLinus Walleij }
158ea713bc4SLinus Walleij 
159f141ed65SGrant Likely /**
160fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
161f625d460SBenoit Parrot  * @np:		device node to get GPIO from
162be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
163f625d460SBenoit Parrot  * @name:	GPIO line name
164f625d460SBenoit Parrot  * @lflags:	gpio_lookup_flags - returned from of_find_gpio() or
165fd7337fdSMarkus Pargmann  *		of_parse_own_gpio()
166f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
167f625d460SBenoit Parrot  *
168f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
169f625d460SBenoit Parrot  * value on the error condition.
170f625d460SBenoit Parrot  */
171fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
172be715343SMasahiro Yamada 					   struct gpio_chip *chip,
173f625d460SBenoit Parrot 					   const char **name,
174f625d460SBenoit Parrot 					   enum gpio_lookup_flags *lflags,
175f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
176f625d460SBenoit Parrot {
177f625d460SBenoit Parrot 	struct device_node *chip_np;
178f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
179be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
180be715343SMasahiro Yamada 	struct gpio_desc *desc;
181f625d460SBenoit Parrot 	u32 tmp;
1823f9547e1SMasahiro Yamada 	int ret;
183f625d460SBenoit Parrot 
184be715343SMasahiro Yamada 	chip_np = chip->of_node;
185f625d460SBenoit Parrot 	if (!chip_np)
186f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
187f625d460SBenoit Parrot 
188f625d460SBenoit Parrot 	xlate_flags = 0;
189f625d460SBenoit Parrot 	*lflags = 0;
190f625d460SBenoit Parrot 	*dflags = 0;
191f625d460SBenoit Parrot 
192f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
193f625d460SBenoit Parrot 	if (ret)
194f625d460SBenoit Parrot 		return ERR_PTR(ret);
195f625d460SBenoit Parrot 
196be715343SMasahiro Yamada 	gpiospec.np = chip_np;
197be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
198f625d460SBenoit Parrot 
199be715343SMasahiro Yamada 	ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp);
200f625d460SBenoit Parrot 	if (ret)
201f625d460SBenoit Parrot 		return ERR_PTR(ret);
202f625d460SBenoit Parrot 
20399468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
204be715343SMasahiro Yamada 	if (IS_ERR(desc))
205be715343SMasahiro Yamada 		return desc;
206f625d460SBenoit Parrot 
207f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
208f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
209f625d460SBenoit Parrot 
210f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
211f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
212f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
213f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
214f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
215f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
216f625d460SBenoit Parrot 	else {
217f625d460SBenoit Parrot 		pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
218be715343SMasahiro Yamada 			desc_to_gpio(desc), np->name);
219f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
220f625d460SBenoit Parrot 	}
221f625d460SBenoit Parrot 
222f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
223f625d460SBenoit Parrot 		*name = np->name;
224f625d460SBenoit Parrot 
225be715343SMasahiro Yamada 	return desc;
226f625d460SBenoit Parrot }
227f625d460SBenoit Parrot 
228f625d460SBenoit Parrot /**
229fd9c5531SLinus Walleij  * of_gpiochip_set_names() - set up the names of the lines
230fd9c5531SLinus Walleij  * @chip: GPIO chip whose lines should be named, if possible
231fd9c5531SLinus Walleij  */
232fd9c5531SLinus Walleij static void of_gpiochip_set_names(struct gpio_chip *gc)
233fd9c5531SLinus Walleij {
234fd9c5531SLinus Walleij 	struct gpio_device *gdev = gc->gpiodev;
235fd9c5531SLinus Walleij 	struct device_node *np = gc->of_node;
236fd9c5531SLinus Walleij 	int i;
237fd9c5531SLinus Walleij 	int nstrings;
238fd9c5531SLinus Walleij 
239fd9c5531SLinus Walleij 	nstrings = of_property_count_strings(np, "gpio-line-names");
240fd9c5531SLinus Walleij 	if (nstrings <= 0)
241fd9c5531SLinus Walleij 		/* Lines names not present */
242fd9c5531SLinus Walleij 		return;
243fd9c5531SLinus Walleij 
244fd9c5531SLinus Walleij 	/* This is normally not what you want */
245fd9c5531SLinus Walleij 	if (gdev->ngpio != nstrings)
246fd9c5531SLinus Walleij 		dev_info(&gdev->dev, "gpio-line-names specifies %d line "
247fd9c5531SLinus Walleij 			 "names but there are %d lines on the chip\n",
248fd9c5531SLinus Walleij 			 nstrings, gdev->ngpio);
249fd9c5531SLinus Walleij 
250fd9c5531SLinus Walleij 	/*
251fd9c5531SLinus Walleij 	 * Make sure to not index beyond the end of the number of descriptors
252fd9c5531SLinus Walleij 	 * of the GPIO device.
253fd9c5531SLinus Walleij 	 */
254fd9c5531SLinus Walleij 	for (i = 0; i < gdev->ngpio; i++) {
255fd9c5531SLinus Walleij 		const char *name;
256fd9c5531SLinus Walleij 		int ret;
257fd9c5531SLinus Walleij 
258fd9c5531SLinus Walleij 		ret = of_property_read_string_index(np,
259fd9c5531SLinus Walleij 						    "gpio-line-names",
260fd9c5531SLinus Walleij 						    i,
261fd9c5531SLinus Walleij 						    &name);
262fd9c5531SLinus Walleij 		if (ret) {
263fd9c5531SLinus Walleij 			if (ret != -ENODATA)
264fd9c5531SLinus Walleij                                 dev_err(&gdev->dev,
265fd9c5531SLinus Walleij                                         "unable to name line %d: %d\n",
266fd9c5531SLinus Walleij                                         i, ret);
267fd9c5531SLinus Walleij 			break;
268fd9c5531SLinus Walleij 		}
269fd9c5531SLinus Walleij 		gdev->descs[i].name = name;
270fd9c5531SLinus Walleij 	}
271fd9c5531SLinus Walleij }
272fd9c5531SLinus Walleij 
273fd9c5531SLinus Walleij /**
274fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
275f625d460SBenoit Parrot  * @chip:	gpio chip to act on
276f625d460SBenoit Parrot  *
277f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
278f625d460SBenoit Parrot  * configuration.
279dfbd379bSLaxman Dewangan  * It retures error if it fails otherwise 0 on success.
280f625d460SBenoit Parrot  */
281dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
282f625d460SBenoit Parrot {
283f625d460SBenoit Parrot 	struct gpio_desc *desc = NULL;
284f625d460SBenoit Parrot 	struct device_node *np;
285f625d460SBenoit Parrot 	const char *name;
286f625d460SBenoit Parrot 	enum gpio_lookup_flags lflags;
287f625d460SBenoit Parrot 	enum gpiod_flags dflags;
288dfbd379bSLaxman Dewangan 	int ret;
289f625d460SBenoit Parrot 
290d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
291f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
292f625d460SBenoit Parrot 			continue;
293f625d460SBenoit Parrot 
294be715343SMasahiro Yamada 		desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags);
295f625d460SBenoit Parrot 		if (IS_ERR(desc))
296f625d460SBenoit Parrot 			continue;
297f625d460SBenoit Parrot 
298dfbd379bSLaxman Dewangan 		ret = gpiod_hog(desc, name, lflags, dflags);
299dfbd379bSLaxman Dewangan 		if (ret < 0)
300dfbd379bSLaxman Dewangan 			return ret;
301f625d460SBenoit Parrot 	}
302dfbd379bSLaxman Dewangan 
303dfbd379bSLaxman Dewangan 	return 0;
304f625d460SBenoit Parrot }
305f625d460SBenoit Parrot 
306f625d460SBenoit Parrot /**
307f141ed65SGrant Likely  * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
308f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
309f141ed65SGrant Likely  * @np:		device node of the GPIO chip
310f141ed65SGrant Likely  * @gpio_spec:	gpio specifier as found in the device tree
311f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
312f141ed65SGrant Likely  *
313f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
314f141ed65SGrant Likely  * gpio chips. This function performs only one sanity check: whether gpio
315f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
316f141ed65SGrant Likely  */
317f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
318f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
319f141ed65SGrant Likely {
320f141ed65SGrant Likely 	/*
321f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
32220a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
323f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
324f141ed65SGrant Likely 	 * but not recommended).
325f141ed65SGrant Likely 	 */
326f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
327f141ed65SGrant Likely 		WARN_ON(1);
328f141ed65SGrant Likely 		return -EINVAL;
329f141ed65SGrant Likely 	}
330f141ed65SGrant Likely 
331f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
332f141ed65SGrant Likely 		return -EINVAL;
333f141ed65SGrant Likely 
3347b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
335f141ed65SGrant Likely 		return -EINVAL;
336f141ed65SGrant Likely 
337f141ed65SGrant Likely 	if (flags)
338f141ed65SGrant Likely 		*flags = gpiospec->args[1];
339f141ed65SGrant Likely 
340f141ed65SGrant Likely 	return gpiospec->args[0];
341f141ed65SGrant Likely }
342f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
343f141ed65SGrant Likely 
344f141ed65SGrant Likely /**
3453208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
346f141ed65SGrant Likely  * @np:		device node of the GPIO chip
347f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
3483208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
349f141ed65SGrant Likely  *
350f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
351f141ed65SGrant Likely  *
352f141ed65SGrant Likely  * 1) In the gpio_chip structure:
353f141ed65SGrant Likely  *    - all the callbacks
354f141ed65SGrant Likely  *    - of_gpio_n_cells
355f141ed65SGrant Likely  *    - of_xlate callback (optional)
356f141ed65SGrant Likely  *
357f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
358f141ed65SGrant Likely  *    - save_regs callback (optional)
359f141ed65SGrant Likely  *
360f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
361f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
362f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
363f141ed65SGrant Likely  */
3643208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
3653208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
3663208b0f0SLinus Walleij 			    void *data)
367f141ed65SGrant Likely {
368f141ed65SGrant Likely 	int ret = -ENOMEM;
369f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
370f141ed65SGrant Likely 
371f141ed65SGrant Likely 	gc->label = kstrdup(np->full_name, GFP_KERNEL);
372f141ed65SGrant Likely 	if (!gc->label)
373f141ed65SGrant Likely 		goto err0;
374f141ed65SGrant Likely 
375f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
376f141ed65SGrant Likely 	if (!mm_gc->regs)
377f141ed65SGrant Likely 		goto err1;
378f141ed65SGrant Likely 
379f141ed65SGrant Likely 	gc->base = -1;
380f141ed65SGrant Likely 
381f141ed65SGrant Likely 	if (mm_gc->save_regs)
382f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
383f141ed65SGrant Likely 
384f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
385f141ed65SGrant Likely 
3863208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
387f141ed65SGrant Likely 	if (ret)
388f141ed65SGrant Likely 		goto err2;
389f141ed65SGrant Likely 
390f141ed65SGrant Likely 	return 0;
391f141ed65SGrant Likely err2:
392f141ed65SGrant Likely 	iounmap(mm_gc->regs);
393f141ed65SGrant Likely err1:
394f141ed65SGrant Likely 	kfree(gc->label);
395f141ed65SGrant Likely err0:
396f141ed65SGrant Likely 	pr_err("%s: GPIO chip registration failed with status %d\n",
397f141ed65SGrant Likely 	       np->full_name, ret);
398f141ed65SGrant Likely 	return ret;
399f141ed65SGrant Likely }
4003208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data);
401f141ed65SGrant Likely 
402d621e8baSRicardo Ribalda Delgado /**
403d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
404d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
405d621e8baSRicardo Ribalda Delgado  */
406d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
407d621e8baSRicardo Ribalda Delgado {
408d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
409d621e8baSRicardo Ribalda Delgado 
410d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
411d621e8baSRicardo Ribalda Delgado 		return;
412d621e8baSRicardo Ribalda Delgado 
413d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
414d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
415d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
416d621e8baSRicardo Ribalda Delgado }
417d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove);
418d621e8baSRicardo Ribalda Delgado 
419f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
42028355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
421f23f1516SShiraz Hashim {
422f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
423f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
4241e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
425f23f1516SShiraz Hashim 	int index = 0, ret;
426586a87e6SChristian Ruppert 	const char *name;
427586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
428586a87e6SChristian Ruppert 	struct property *group_names;
429f23f1516SShiraz Hashim 
430f23f1516SShiraz Hashim 	if (!np)
43128355f81STomeu Vizoso 		return 0;
432f23f1516SShiraz Hashim 
433586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
434586a87e6SChristian Ruppert 
435ad4e1a7cSHaojian Zhuang 	for (;; index++) {
436d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
437d9fe0039SStephen Warren 				index, &pinspec);
438f23f1516SShiraz Hashim 		if (ret)
439f23f1516SShiraz Hashim 			break;
440f23f1516SShiraz Hashim 
4411e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
442602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
4431e63d7b9SLinus Walleij 		if (!pctldev)
44428355f81STomeu Vizoso 			return -EPROBE_DEFER;
445f23f1516SShiraz Hashim 
446586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
447586a87e6SChristian Ruppert 			if (group_names) {
44872858602SLaurent Navet 				of_property_read_string_index(np,
449586a87e6SChristian Ruppert 						group_names_propname,
450586a87e6SChristian Ruppert 						index, &name);
451586a87e6SChristian Ruppert 				if (strlen(name)) {
452586a87e6SChristian Ruppert 					pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
453586a87e6SChristian Ruppert 						np->full_name);
454586a87e6SChristian Ruppert 					break;
455586a87e6SChristian Ruppert 				}
456586a87e6SChristian Ruppert 			}
457586a87e6SChristian Ruppert 			/* npins != 0: linear range */
4581e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
459ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
4601e63d7b9SLinus Walleij 					pinspec.args[0],
46186853c83SHaojian Zhuang 					pinspec.args[1],
46286853c83SHaojian Zhuang 					pinspec.args[2]);
4631e63d7b9SLinus Walleij 			if (ret)
46428355f81STomeu Vizoso 				return ret;
465586a87e6SChristian Ruppert 		} else {
466586a87e6SChristian Ruppert 			/* npins == 0: special range */
467586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
468586a87e6SChristian Ruppert 				pr_err("%s: Illegal gpio-range format.\n",
469586a87e6SChristian Ruppert 					np->full_name);
470586a87e6SChristian Ruppert 				break;
471586a87e6SChristian Ruppert 			}
472586a87e6SChristian Ruppert 
473586a87e6SChristian Ruppert 			if (!group_names) {
474586a87e6SChristian Ruppert 				pr_err("%s: GPIO group range requested but no %s property.\n",
475586a87e6SChristian Ruppert 					np->full_name, group_names_propname);
476586a87e6SChristian Ruppert 				break;
477586a87e6SChristian Ruppert 			}
478586a87e6SChristian Ruppert 
479586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
480586a87e6SChristian Ruppert 						group_names_propname,
481586a87e6SChristian Ruppert 						index, &name);
482586a87e6SChristian Ruppert 			if (ret)
483586a87e6SChristian Ruppert 				break;
484586a87e6SChristian Ruppert 
485586a87e6SChristian Ruppert 			if (!strlen(name)) {
486586a87e6SChristian Ruppert 				pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
487586a87e6SChristian Ruppert 				np->full_name);
488586a87e6SChristian Ruppert 				break;
489586a87e6SChristian Ruppert 			}
490586a87e6SChristian Ruppert 
491586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
492586a87e6SChristian Ruppert 						pinspec.args[0], name);
493586a87e6SChristian Ruppert 			if (ret)
49428355f81STomeu Vizoso 				return ret;
495586a87e6SChristian Ruppert 		}
496ad4e1a7cSHaojian Zhuang 	}
49728355f81STomeu Vizoso 
49828355f81STomeu Vizoso 	return 0;
499f23f1516SShiraz Hashim }
500f23f1516SShiraz Hashim 
501f23f1516SShiraz Hashim #else
50228355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
503f23f1516SShiraz Hashim #endif
504f23f1516SShiraz Hashim 
50528355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
506f141ed65SGrant Likely {
50728355f81STomeu Vizoso 	int status;
50828355f81STomeu Vizoso 
50958383c78SLinus Walleij 	if ((!chip->of_node) && (chip->parent))
51058383c78SLinus Walleij 		chip->of_node = chip->parent->of_node;
511f141ed65SGrant Likely 
512f141ed65SGrant Likely 	if (!chip->of_node)
51328355f81STomeu Vizoso 		return 0;
514f141ed65SGrant Likely 
515f141ed65SGrant Likely 	if (!chip->of_xlate) {
516f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
517f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
518f141ed65SGrant Likely 	}
519f141ed65SGrant Likely 
5201020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
5211020dfd1SMasahiro Yamada 		return -EINVAL;
5221020dfd1SMasahiro Yamada 
52328355f81STomeu Vizoso 	status = of_gpiochip_add_pin_range(chip);
52428355f81STomeu Vizoso 	if (status)
52528355f81STomeu Vizoso 		return status;
52628355f81STomeu Vizoso 
527fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
528fd9c5531SLinus Walleij 	if (!chip->names)
529fd9c5531SLinus Walleij 		of_gpiochip_set_names(chip);
530fd9c5531SLinus Walleij 
531f141ed65SGrant Likely 	of_node_get(chip->of_node);
532f625d460SBenoit Parrot 
533dfbd379bSLaxman Dewangan 	return of_gpiochip_scan_gpios(chip);
534f141ed65SGrant Likely }
535f141ed65SGrant Likely 
536f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
537f141ed65SGrant Likely {
538e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
539f141ed65SGrant Likely 	of_node_put(chip->of_node);
540f141ed65SGrant Likely }
541