xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision a79fead5)
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
163a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
164f625d460SBenoit Parrot  * @name:	GPIO line name
165f625d460SBenoit Parrot  * @lflags:	gpio_lookup_flags - returned from of_find_gpio() or
166fd7337fdSMarkus Pargmann  *		of_parse_own_gpio()
167f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
168f625d460SBenoit Parrot  *
169f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
170f625d460SBenoit Parrot  * value on the error condition.
171f625d460SBenoit Parrot  */
172fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
173be715343SMasahiro Yamada 					   struct gpio_chip *chip,
174a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
175f625d460SBenoit Parrot 					   enum gpio_lookup_flags *lflags,
176f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
177f625d460SBenoit Parrot {
178f625d460SBenoit Parrot 	struct device_node *chip_np;
179f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
180be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
181be715343SMasahiro Yamada 	struct gpio_desc *desc;
182a79fead5SGeert Uytterhoeven 	unsigned int i;
183f625d460SBenoit Parrot 	u32 tmp;
1843f9547e1SMasahiro Yamada 	int ret;
185f625d460SBenoit Parrot 
186be715343SMasahiro Yamada 	chip_np = chip->of_node;
187f625d460SBenoit Parrot 	if (!chip_np)
188f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
189f625d460SBenoit Parrot 
190f625d460SBenoit Parrot 	xlate_flags = 0;
191f625d460SBenoit Parrot 	*lflags = 0;
192f625d460SBenoit Parrot 	*dflags = 0;
193f625d460SBenoit Parrot 
194f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
195f625d460SBenoit Parrot 	if (ret)
196f625d460SBenoit Parrot 		return ERR_PTR(ret);
197f625d460SBenoit Parrot 
198be715343SMasahiro Yamada 	gpiospec.np = chip_np;
199be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
200f625d460SBenoit Parrot 
201a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
202a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
203a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
204f625d460SBenoit Parrot 		if (ret)
205f625d460SBenoit Parrot 			return ERR_PTR(ret);
206a79fead5SGeert Uytterhoeven 	}
207f625d460SBenoit Parrot 
20899468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
209be715343SMasahiro Yamada 	if (IS_ERR(desc))
210be715343SMasahiro Yamada 		return desc;
211f625d460SBenoit Parrot 
212f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
213f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
214f625d460SBenoit Parrot 
215f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
216f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
217f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
218f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
219f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
220f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
221f625d460SBenoit Parrot 	else {
222f625d460SBenoit Parrot 		pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
223be715343SMasahiro Yamada 			desc_to_gpio(desc), np->name);
224f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
225f625d460SBenoit Parrot 	}
226f625d460SBenoit Parrot 
227f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
228f625d460SBenoit Parrot 		*name = np->name;
229f625d460SBenoit Parrot 
230be715343SMasahiro Yamada 	return desc;
231f625d460SBenoit Parrot }
232f625d460SBenoit Parrot 
233f625d460SBenoit Parrot /**
234fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
235f625d460SBenoit Parrot  * @chip:	gpio chip to act on
236f625d460SBenoit Parrot  *
237f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
238f625d460SBenoit Parrot  * configuration.
239dfbd379bSLaxman Dewangan  * It retures error if it fails otherwise 0 on success.
240f625d460SBenoit Parrot  */
241dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
242f625d460SBenoit Parrot {
243f625d460SBenoit Parrot 	struct gpio_desc *desc = NULL;
244f625d460SBenoit Parrot 	struct device_node *np;
245f625d460SBenoit Parrot 	const char *name;
246f625d460SBenoit Parrot 	enum gpio_lookup_flags lflags;
247f625d460SBenoit Parrot 	enum gpiod_flags dflags;
248a79fead5SGeert Uytterhoeven 	unsigned int i;
249dfbd379bSLaxman Dewangan 	int ret;
250f625d460SBenoit Parrot 
251d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
252f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
253f625d460SBenoit Parrot 			continue;
254f625d460SBenoit Parrot 
255a79fead5SGeert Uytterhoeven 		for (i = 0;; i++) {
256a79fead5SGeert Uytterhoeven 			desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
257a79fead5SGeert Uytterhoeven 						 &dflags);
258f625d460SBenoit Parrot 			if (IS_ERR(desc))
259a79fead5SGeert Uytterhoeven 				break;
260f625d460SBenoit Parrot 
261dfbd379bSLaxman Dewangan 			ret = gpiod_hog(desc, name, lflags, dflags);
26209e258afSWei Yongjun 			if (ret < 0) {
26309e258afSWei Yongjun 				of_node_put(np);
264dfbd379bSLaxman Dewangan 				return ret;
265f625d460SBenoit Parrot 			}
26609e258afSWei Yongjun 		}
267a79fead5SGeert Uytterhoeven 	}
268dfbd379bSLaxman Dewangan 
269dfbd379bSLaxman Dewangan 	return 0;
270f625d460SBenoit Parrot }
271f625d460SBenoit Parrot 
272f625d460SBenoit Parrot /**
273f141ed65SGrant Likely  * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
274f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
275f141ed65SGrant Likely  * @np:		device node of the GPIO chip
276f141ed65SGrant Likely  * @gpio_spec:	gpio specifier as found in the device tree
277f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
278f141ed65SGrant Likely  *
279f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
280f141ed65SGrant Likely  * gpio chips. This function performs only one sanity check: whether gpio
281f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
282f141ed65SGrant Likely  */
283f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
284f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
285f141ed65SGrant Likely {
286f141ed65SGrant Likely 	/*
287f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
28820a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
289f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
290f141ed65SGrant Likely 	 * but not recommended).
291f141ed65SGrant Likely 	 */
292f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
293f141ed65SGrant Likely 		WARN_ON(1);
294f141ed65SGrant Likely 		return -EINVAL;
295f141ed65SGrant Likely 	}
296f141ed65SGrant Likely 
297f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
298f141ed65SGrant Likely 		return -EINVAL;
299f141ed65SGrant Likely 
3007b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
301f141ed65SGrant Likely 		return -EINVAL;
302f141ed65SGrant Likely 
303f141ed65SGrant Likely 	if (flags)
304f141ed65SGrant Likely 		*flags = gpiospec->args[1];
305f141ed65SGrant Likely 
306f141ed65SGrant Likely 	return gpiospec->args[0];
307f141ed65SGrant Likely }
308f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
309f141ed65SGrant Likely 
310f141ed65SGrant Likely /**
3113208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
312f141ed65SGrant Likely  * @np:		device node of the GPIO chip
313f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
3143208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
315f141ed65SGrant Likely  *
316f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
317f141ed65SGrant Likely  *
318f141ed65SGrant Likely  * 1) In the gpio_chip structure:
319f141ed65SGrant Likely  *    - all the callbacks
320f141ed65SGrant Likely  *    - of_gpio_n_cells
321f141ed65SGrant Likely  *    - of_xlate callback (optional)
322f141ed65SGrant Likely  *
323f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
324f141ed65SGrant Likely  *    - save_regs callback (optional)
325f141ed65SGrant Likely  *
326f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
327f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
328f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
329f141ed65SGrant Likely  */
3303208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
3313208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
3323208b0f0SLinus Walleij 			    void *data)
333f141ed65SGrant Likely {
334f141ed65SGrant Likely 	int ret = -ENOMEM;
335f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
336f141ed65SGrant Likely 
337f141ed65SGrant Likely 	gc->label = kstrdup(np->full_name, GFP_KERNEL);
338f141ed65SGrant Likely 	if (!gc->label)
339f141ed65SGrant Likely 		goto err0;
340f141ed65SGrant Likely 
341f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
342f141ed65SGrant Likely 	if (!mm_gc->regs)
343f141ed65SGrant Likely 		goto err1;
344f141ed65SGrant Likely 
345f141ed65SGrant Likely 	gc->base = -1;
346f141ed65SGrant Likely 
347f141ed65SGrant Likely 	if (mm_gc->save_regs)
348f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
349f141ed65SGrant Likely 
350f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
351f141ed65SGrant Likely 
3523208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
353f141ed65SGrant Likely 	if (ret)
354f141ed65SGrant Likely 		goto err2;
355f141ed65SGrant Likely 
356f141ed65SGrant Likely 	return 0;
357f141ed65SGrant Likely err2:
358f141ed65SGrant Likely 	iounmap(mm_gc->regs);
359f141ed65SGrant Likely err1:
360f141ed65SGrant Likely 	kfree(gc->label);
361f141ed65SGrant Likely err0:
362f141ed65SGrant Likely 	pr_err("%s: GPIO chip registration failed with status %d\n",
363f141ed65SGrant Likely 	       np->full_name, ret);
364f141ed65SGrant Likely 	return ret;
365f141ed65SGrant Likely }
3663208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data);
367f141ed65SGrant Likely 
368d621e8baSRicardo Ribalda Delgado /**
369d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
370d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
371d621e8baSRicardo Ribalda Delgado  */
372d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
373d621e8baSRicardo Ribalda Delgado {
374d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
375d621e8baSRicardo Ribalda Delgado 
376d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
377d621e8baSRicardo Ribalda Delgado 		return;
378d621e8baSRicardo Ribalda Delgado 
379d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
380d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
381d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
382d621e8baSRicardo Ribalda Delgado }
383d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove);
384d621e8baSRicardo Ribalda Delgado 
385f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
38628355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
387f23f1516SShiraz Hashim {
388f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
389f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
3901e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
391f23f1516SShiraz Hashim 	int index = 0, ret;
392586a87e6SChristian Ruppert 	const char *name;
393586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
394586a87e6SChristian Ruppert 	struct property *group_names;
395f23f1516SShiraz Hashim 
396f23f1516SShiraz Hashim 	if (!np)
39728355f81STomeu Vizoso 		return 0;
398f23f1516SShiraz Hashim 
399586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
400586a87e6SChristian Ruppert 
401ad4e1a7cSHaojian Zhuang 	for (;; index++) {
402d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
403d9fe0039SStephen Warren 				index, &pinspec);
404f23f1516SShiraz Hashim 		if (ret)
405f23f1516SShiraz Hashim 			break;
406f23f1516SShiraz Hashim 
4071e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
408602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
4091e63d7b9SLinus Walleij 		if (!pctldev)
41028355f81STomeu Vizoso 			return -EPROBE_DEFER;
411f23f1516SShiraz Hashim 
412586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
413586a87e6SChristian Ruppert 			if (group_names) {
41472858602SLaurent Navet 				of_property_read_string_index(np,
415586a87e6SChristian Ruppert 						group_names_propname,
416586a87e6SChristian Ruppert 						index, &name);
417586a87e6SChristian Ruppert 				if (strlen(name)) {
418586a87e6SChristian Ruppert 					pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
419586a87e6SChristian Ruppert 						np->full_name);
420586a87e6SChristian Ruppert 					break;
421586a87e6SChristian Ruppert 				}
422586a87e6SChristian Ruppert 			}
423586a87e6SChristian Ruppert 			/* npins != 0: linear range */
4241e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
425ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
4261e63d7b9SLinus Walleij 					pinspec.args[0],
42786853c83SHaojian Zhuang 					pinspec.args[1],
42886853c83SHaojian Zhuang 					pinspec.args[2]);
4291e63d7b9SLinus Walleij 			if (ret)
43028355f81STomeu Vizoso 				return ret;
431586a87e6SChristian Ruppert 		} else {
432586a87e6SChristian Ruppert 			/* npins == 0: special range */
433586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
434586a87e6SChristian Ruppert 				pr_err("%s: Illegal gpio-range format.\n",
435586a87e6SChristian Ruppert 					np->full_name);
436586a87e6SChristian Ruppert 				break;
437586a87e6SChristian Ruppert 			}
438586a87e6SChristian Ruppert 
439586a87e6SChristian Ruppert 			if (!group_names) {
440586a87e6SChristian Ruppert 				pr_err("%s: GPIO group range requested but no %s property.\n",
441586a87e6SChristian Ruppert 					np->full_name, group_names_propname);
442586a87e6SChristian Ruppert 				break;
443586a87e6SChristian Ruppert 			}
444586a87e6SChristian Ruppert 
445586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
446586a87e6SChristian Ruppert 						group_names_propname,
447586a87e6SChristian Ruppert 						index, &name);
448586a87e6SChristian Ruppert 			if (ret)
449586a87e6SChristian Ruppert 				break;
450586a87e6SChristian Ruppert 
451586a87e6SChristian Ruppert 			if (!strlen(name)) {
452586a87e6SChristian Ruppert 				pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
453586a87e6SChristian Ruppert 				np->full_name);
454586a87e6SChristian Ruppert 				break;
455586a87e6SChristian Ruppert 			}
456586a87e6SChristian Ruppert 
457586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
458586a87e6SChristian Ruppert 						pinspec.args[0], name);
459586a87e6SChristian Ruppert 			if (ret)
46028355f81STomeu Vizoso 				return ret;
461586a87e6SChristian Ruppert 		}
462ad4e1a7cSHaojian Zhuang 	}
46328355f81STomeu Vizoso 
46428355f81STomeu Vizoso 	return 0;
465f23f1516SShiraz Hashim }
466f23f1516SShiraz Hashim 
467f23f1516SShiraz Hashim #else
46828355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
469f23f1516SShiraz Hashim #endif
470f23f1516SShiraz Hashim 
47128355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
472f141ed65SGrant Likely {
47328355f81STomeu Vizoso 	int status;
47428355f81STomeu Vizoso 
47558383c78SLinus Walleij 	if ((!chip->of_node) && (chip->parent))
47658383c78SLinus Walleij 		chip->of_node = chip->parent->of_node;
477f141ed65SGrant Likely 
478f141ed65SGrant Likely 	if (!chip->of_node)
47928355f81STomeu Vizoso 		return 0;
480f141ed65SGrant Likely 
481f141ed65SGrant Likely 	if (!chip->of_xlate) {
482f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
483f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
484f141ed65SGrant Likely 	}
485f141ed65SGrant Likely 
4861020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
4871020dfd1SMasahiro Yamada 		return -EINVAL;
4881020dfd1SMasahiro Yamada 
48928355f81STomeu Vizoso 	status = of_gpiochip_add_pin_range(chip);
49028355f81STomeu Vizoso 	if (status)
49128355f81STomeu Vizoso 		return status;
49228355f81STomeu Vizoso 
493fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
494fd9c5531SLinus Walleij 	if (!chip->names)
4959427ecbeSMika Westerberg 		devprop_gpiochip_set_names(chip);
496fd9c5531SLinus Walleij 
497f141ed65SGrant Likely 	of_node_get(chip->of_node);
498f625d460SBenoit Parrot 
499dfbd379bSLaxman Dewangan 	return of_gpiochip_scan_gpios(chip);
500f141ed65SGrant Likely }
501f141ed65SGrant Likely 
502f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
503f141ed65SGrant Likely {
504e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
505f141ed65SGrant Likely 	of_node_put(chip->of_node);
506f141ed65SGrant Likely }
507