blob: b863386be91130693c5da556a6a62263a3d4737e [file] [log] [blame]
Anton Vorontsov863fbf42008-04-11 23:06:45 +10001/*
2 * OF helpers for the GPIO API
3 *
4 * Copyright (c) 2007-2008 MontaVista Software, Inc.
5 *
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Grant Likely2e13cba2010-07-05 16:11:55 -060014#include <linux/device.h>
Sachin Kamatbea4dbe2014-01-27 12:15:08 +053015#include <linux/err.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100016#include <linux/errno.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060017#include <linux/module.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100018#include <linux/io.h>
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070019#include <linux/gpio/consumer.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100020#include <linux/of.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060021#include <linux/of_address.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100022#include <linux/of_gpio.h>
Shiraz Hashimf23f1512012-10-27 15:21:36 +053023#include <linux/pinctrl/pinctrl.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060024#include <linux/slab.h>
Benoit Parrotf625d462015-02-02 11:44:44 -060025#include <linux/gpio/machine.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100026
Alexandre Courbot1bd6b602014-07-22 16:17:41 +090027#include "gpiolib.h"
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070028
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090029static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
Grant Likely3d0f7cf2012-05-17 13:54:40 -060030{
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090031 struct of_phandle_args *gpiospec = data;
32
33 return chip->gpiodev->dev.of_node == gpiospec->np &&
Vincent Whitchurch3bb227b2018-08-31 09:04:18 +020034 chip->of_xlate &&
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090035 chip->of_xlate(chip, gpiospec, NULL) >= 0;
Masahiro Yamada762c2e42016-06-14 19:07:06 +090036}
Grant Likely3d0f7cf2012-05-17 13:54:40 -060037
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090038static struct gpio_chip *of_find_gpiochip_by_xlate(
39 struct of_phandle_args *gpiospec)
Masahiro Yamada762c2e42016-06-14 19:07:06 +090040{
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090041 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
Grant Likely3d0f7cf2012-05-17 13:54:40 -060042}
43
Masahiro Yamada99468c12016-06-14 19:07:07 +090044static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
45 struct of_phandle_args *gpiospec,
46 enum of_gpio_flags *flags)
47{
Anton Vorontsov863fbf42008-04-11 23:06:45 +100048 int ret;
49
Masahiro Yamada99468c12016-06-14 19:07:07 +090050 if (chip->of_gpio_n_cells != gpiospec->args_count)
51 return ERR_PTR(-EINVAL);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100052
Masahiro Yamada99468c12016-06-14 19:07:07 +090053 ret = chip->of_xlate(chip, gpiospec, flags);
54 if (ret < 0)
55 return ERR_PTR(ret);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100056
Masahiro Yamada99468c12016-06-14 19:07:07 +090057 return gpiochip_get_desc(chip, ret);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100058}
59
Anton Vorontsovb908b532008-12-01 06:30:04 +000060/**
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070061 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
Anton Vorontsov863fbf42008-04-11 23:06:45 +100062 * @np: device node to get GPIO from
John Bonesioa6b09192011-06-27 16:49:57 -070063 * @propname: property name containing gpio specifier(s)
Anton Vorontsov863fbf42008-04-11 23:06:45 +100064 * @index: index of the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +000065 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +100066 *
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070067 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
Anton Vorontsovb908b532008-12-01 06:30:04 +000068 * value on the error condition. If @flags is not NULL the function also fills
69 * in flags for the GPIO.
Anton Vorontsov863fbf42008-04-11 23:06:45 +100070 */
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070071struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
72 const char *propname, int index, enum of_gpio_flags *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +100073{
Masahiro Yamada762c2e42016-06-14 19:07:06 +090074 struct of_phandle_args gpiospec;
75 struct gpio_chip *chip;
76 struct gpio_desc *desc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +000077 int ret;
Anton Vorontsov863fbf42008-04-11 23:06:45 +100078
Grant Likely3d0f7cf2012-05-17 13:54:40 -060079 ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
Masahiro Yamada762c2e42016-06-14 19:07:06 +090080 &gpiospec);
Grant Likely3d0f7cf2012-05-17 13:54:40 -060081 if (ret) {
Tushar Behera85ea29a2014-07-04 15:22:09 +053082 pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
83 __func__, propname, np->full_name, index);
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070084 return ERR_PTR(ret);
Grant Likely3d0f7cf2012-05-17 13:54:40 -060085 }
Anton Vorontsov863fbf42008-04-11 23:06:45 +100086
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090087 chip = of_find_gpiochip_by_xlate(&gpiospec);
Masahiro Yamada762c2e42016-06-14 19:07:06 +090088 if (!chip) {
89 desc = ERR_PTR(-EPROBE_DEFER);
90 goto out;
91 }
Grant Likely3d0f7cf2012-05-17 13:54:40 -060092
Masahiro Yamada99468c12016-06-14 19:07:07 +090093 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
Masahiro Yamada762c2e42016-06-14 19:07:06 +090094 if (IS_ERR(desc))
95 goto out;
96
Tushar Behera85ea29a2014-07-04 15:22:09 +053097 pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
98 __func__, propname, np->full_name, index,
Masahiro Yamada762c2e42016-06-14 19:07:06 +090099 PTR_ERR_OR_ZERO(desc));
100
101out:
102 of_node_put(gpiospec.np);
103
104 return desc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000105}
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000106
Alexandre Courbotf01d9072014-05-17 14:54:50 +0900107int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
108 int index, enum of_gpio_flags *flags)
109{
110 struct gpio_desc *desc;
111
112 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
113
114 if (IS_ERR(desc))
115 return PTR_ERR(desc);
116 else
117 return desc_to_gpio(desc);
118}
119EXPORT_SYMBOL(of_get_named_gpio_flags);
120
Linus Walleijea713bc2016-10-03 10:09:40 +0200121struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
122 unsigned int idx,
123 enum gpio_lookup_flags *flags)
124{
125 char prop_name[32]; /* 32 is max size of property name */
126 enum of_gpio_flags of_flags;
127 struct gpio_desc *desc;
128 unsigned int i;
129
130 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
131 if (con_id)
132 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
133 gpio_suffixes[i]);
134 else
135 snprintf(prop_name, sizeof(prop_name), "%s",
136 gpio_suffixes[i]);
137
138 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
139 &of_flags);
140 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
141 break;
142 }
143
144 if (IS_ERR(desc))
145 return desc;
146
147 if (of_flags & OF_GPIO_ACTIVE_LOW)
148 *flags |= GPIO_ACTIVE_LOW;
149
150 if (of_flags & OF_GPIO_SINGLE_ENDED) {
151 if (of_flags & OF_GPIO_ACTIVE_LOW)
152 *flags |= GPIO_OPEN_DRAIN;
153 else
154 *flags |= GPIO_OPEN_SOURCE;
155 }
156
157 return desc;
158}
159
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000160/**
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200161 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
Benoit Parrotf625d462015-02-02 11:44:44 -0600162 * @np: device node to get GPIO from
Masahiro Yamadabe715342016-06-14 19:07:04 +0900163 * @chip: GPIO chip whose hog is parsed
Benoit Parrotf625d462015-02-02 11:44:44 -0600164 * @name: GPIO line name
165 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200166 * of_parse_own_gpio()
Benoit Parrotf625d462015-02-02 11:44:44 -0600167 * @dflags: gpiod_flags - optional GPIO initialization flags
168 *
169 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
170 * value on the error condition.
171 */
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200172static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
Masahiro Yamadabe715342016-06-14 19:07:04 +0900173 struct gpio_chip *chip,
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200174 const char **name,
175 enum gpio_lookup_flags *lflags,
176 enum gpiod_flags *dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -0600177{
178 struct device_node *chip_np;
179 enum of_gpio_flags xlate_flags;
Masahiro Yamadabe715342016-06-14 19:07:04 +0900180 struct of_phandle_args gpiospec;
181 struct gpio_desc *desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600182 u32 tmp;
Masahiro Yamada3f9547e2016-06-14 19:07:03 +0900183 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600184
Masahiro Yamadabe715342016-06-14 19:07:04 +0900185 chip_np = chip->of_node;
Benoit Parrotf625d462015-02-02 11:44:44 -0600186 if (!chip_np)
187 return ERR_PTR(-EINVAL);
188
189 xlate_flags = 0;
190 *lflags = 0;
191 *dflags = 0;
192
193 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
194 if (ret)
195 return ERR_PTR(ret);
196
Masahiro Yamadabe715342016-06-14 19:07:04 +0900197 gpiospec.np = chip_np;
198 gpiospec.args_count = tmp;
Benoit Parrotf625d462015-02-02 11:44:44 -0600199
Masahiro Yamadabe715342016-06-14 19:07:04 +0900200 ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp);
Masahiro Yamada3f9547e2016-06-14 19:07:03 +0900201 if (ret)
202 return ERR_PTR(ret);
Benoit Parrotf625d462015-02-02 11:44:44 -0600203
Masahiro Yamada99468c12016-06-14 19:07:07 +0900204 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
Masahiro Yamadabe715342016-06-14 19:07:04 +0900205 if (IS_ERR(desc))
206 return desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600207
208 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
209 *lflags |= GPIO_ACTIVE_LOW;
210
211 if (of_property_read_bool(np, "input"))
212 *dflags |= GPIOD_IN;
213 else if (of_property_read_bool(np, "output-low"))
214 *dflags |= GPIOD_OUT_LOW;
215 else if (of_property_read_bool(np, "output-high"))
216 *dflags |= GPIOD_OUT_HIGH;
217 else {
218 pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
Masahiro Yamadabe715342016-06-14 19:07:04 +0900219 desc_to_gpio(desc), np->name);
Benoit Parrotf625d462015-02-02 11:44:44 -0600220 return ERR_PTR(-EINVAL);
221 }
222
223 if (name && of_property_read_string(np, "line-name", name))
224 *name = np->name;
225
Masahiro Yamadabe715342016-06-14 19:07:04 +0900226 return desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600227}
228
229/**
Linus Walleijfd9c5532016-04-19 15:26:26 +0200230 * of_gpiochip_set_names() - set up the names of the lines
231 * @chip: GPIO chip whose lines should be named, if possible
232 */
233static void of_gpiochip_set_names(struct gpio_chip *gc)
234{
235 struct gpio_device *gdev = gc->gpiodev;
236 struct device_node *np = gc->of_node;
237 int i;
238 int nstrings;
239
240 nstrings = of_property_count_strings(np, "gpio-line-names");
241 if (nstrings <= 0)
242 /* Lines names not present */
243 return;
244
245 /* This is normally not what you want */
246 if (gdev->ngpio != nstrings)
247 dev_info(&gdev->dev, "gpio-line-names specifies %d line "
248 "names but there are %d lines on the chip\n",
249 nstrings, gdev->ngpio);
250
251 /*
252 * Make sure to not index beyond the end of the number of descriptors
253 * of the GPIO device.
254 */
255 for (i = 0; i < gdev->ngpio; i++) {
256 const char *name;
257 int ret;
258
259 ret = of_property_read_string_index(np,
260 "gpio-line-names",
261 i,
262 &name);
263 if (ret) {
264 if (ret != -ENODATA)
265 dev_err(&gdev->dev,
266 "unable to name line %d: %d\n",
267 i, ret);
268 break;
269 }
270 gdev->descs[i].name = name;
271 }
272}
273
274/**
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200275 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
Benoit Parrotf625d462015-02-02 11:44:44 -0600276 * @chip: gpio chip to act on
277 *
278 * This is only used by of_gpiochip_add to request/set GPIO initial
279 * configuration.
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530280 * It retures error if it fails otherwise 0 on success.
Benoit Parrotf625d462015-02-02 11:44:44 -0600281 */
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530282static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
Benoit Parrotf625d462015-02-02 11:44:44 -0600283{
284 struct gpio_desc *desc = NULL;
285 struct device_node *np;
286 const char *name;
287 enum gpio_lookup_flags lflags;
288 enum gpiod_flags dflags;
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530289 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600290
Laxman Dewangand1279d92016-03-11 19:13:20 +0530291 for_each_available_child_of_node(chip->of_node, np) {
Benoit Parrotf625d462015-02-02 11:44:44 -0600292 if (!of_property_read_bool(np, "gpio-hog"))
293 continue;
294
Masahiro Yamadabe715342016-06-14 19:07:04 +0900295 desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600296 if (IS_ERR(desc))
297 continue;
298
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530299 ret = gpiod_hog(desc, name, lflags, dflags);
300 if (ret < 0)
301 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600302 }
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530303
304 return 0;
Benoit Parrotf625d462015-02-02 11:44:44 -0600305}
306
307/**
Anton Vorontsovb908b532008-12-01 06:30:04 +0000308 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600309 * @gc: pointer to the gpio_chip structure
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000310 * @np: device node of the GPIO chip
311 * @gpio_spec: gpio specifier as found in the device tree
Anton Vorontsovb908b532008-12-01 06:30:04 +0000312 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000313 *
314 * This is simple translation function, suitable for the most 1:1 mapped
315 * gpio chips. This function performs only one sanity check: whether gpio
316 * is less than ngpios (that is specified in the gpio_chip).
317 */
Grant Likely15c9a0a2011-12-12 09:25:57 -0700318int of_gpio_simple_xlate(struct gpio_chip *gc,
319 const struct of_phandle_args *gpiospec, u32 *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000320{
Anton Vorontsovb908b532008-12-01 06:30:04 +0000321 /*
322 * We're discouraging gpio_cells < 2, since that way you'll have to
Colin Cronin20a8a962015-05-18 11:41:43 -0700323 * write your own xlate function (that will have to retrieve the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +0000324 * number and the flags from a single gpio cell -- this is possible,
325 * but not recommended).
326 */
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600327 if (gc->of_gpio_n_cells < 2) {
Anton Vorontsovb908b532008-12-01 06:30:04 +0000328 WARN_ON(1);
329 return -EINVAL;
330 }
331
Grant Likely15c9a0a2011-12-12 09:25:57 -0700332 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
333 return -EINVAL;
334
Roland Stigge6270d832012-04-04 02:02:58 +0200335 if (gpiospec->args[0] >= gc->ngpio)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000336 return -EINVAL;
337
Anton Vorontsovb908b532008-12-01 06:30:04 +0000338 if (flags)
Grant Likely15c9a0a2011-12-12 09:25:57 -0700339 *flags = gpiospec->args[1];
Anton Vorontsovb908b532008-12-01 06:30:04 +0000340
Grant Likely15c9a0a2011-12-12 09:25:57 -0700341 return gpiospec->args[0];
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000342}
Jamie Iles3038bbd2011-07-28 16:25:41 +0100343EXPORT_SYMBOL(of_gpio_simple_xlate);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000344
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000345/**
Linus Walleij3208b0f2015-12-04 15:13:53 +0100346 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000347 * @np: device node of the GPIO chip
348 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
Linus Walleij3208b0f2015-12-04 15:13:53 +0100349 * @data: driver data to store in the struct gpio_chip
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000350 *
351 * To use this function you should allocate and fill mm_gc with:
352 *
353 * 1) In the gpio_chip structure:
354 * - all the callbacks
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600355 * - of_gpio_n_cells
356 * - of_xlate callback (optional)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000357 *
358 * 3) In the of_mm_gpio_chip structure:
359 * - save_regs callback (optional)
360 *
361 * If succeeded, this function will map bank's memory and will
362 * do all necessary work for you. Then you'll able to use .regs
363 * to manage GPIOs from the callbacks.
364 */
Linus Walleij3208b0f2015-12-04 15:13:53 +0100365int of_mm_gpiochip_add_data(struct device_node *np,
366 struct of_mm_gpio_chip *mm_gc,
367 void *data)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000368{
369 int ret = -ENOMEM;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600370 struct gpio_chip *gc = &mm_gc->gc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000371
372 gc->label = kstrdup(np->full_name, GFP_KERNEL);
373 if (!gc->label)
374 goto err0;
375
376 mm_gc->regs = of_iomap(np, 0);
377 if (!mm_gc->regs)
378 goto err1;
379
Anton Vorontsov21451152008-04-30 00:05:24 +1000380 gc->base = -1;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000381
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000382 if (mm_gc->save_regs)
383 mm_gc->save_regs(mm_gc);
384
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600385 mm_gc->gc.of_node = np;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000386
Linus Walleij3208b0f2015-12-04 15:13:53 +0100387 ret = gpiochip_add_data(gc, data);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000388 if (ret)
389 goto err2;
390
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000391 return 0;
392err2:
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000393 iounmap(mm_gc->regs);
394err1:
395 kfree(gc->label);
396err0:
397 pr_err("%s: GPIO chip registration failed with status %d\n",
398 np->full_name, ret);
399 return ret;
400}
Linus Walleij3208b0f2015-12-04 15:13:53 +0100401EXPORT_SYMBOL(of_mm_gpiochip_add_data);
Grant Likely594fa262010-06-08 07:48:16 -0600402
Ricardo Ribalda Delgadod621e8b2014-12-17 16:51:13 +0100403/**
404 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
405 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
406 */
407void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
408{
409 struct gpio_chip *gc = &mm_gc->gc;
410
411 if (!mm_gc)
412 return;
413
414 gpiochip_remove(gc);
415 iounmap(mm_gc->regs);
416 kfree(gc->label);
417}
418EXPORT_SYMBOL(of_mm_gpiochip_remove);
419
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530420#ifdef CONFIG_PINCTRL
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200421static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530422{
423 struct device_node *np = chip->of_node;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530424 struct of_phandle_args pinspec;
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100425 struct pinctrl_dev *pctldev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530426 int index = 0, ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200427 const char *name;
428 static const char group_names_propname[] = "gpio-ranges-group-names";
429 struct property *group_names;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530430
431 if (!np)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200432 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530433
Christian Ruppert586a87e2013-10-15 15:37:54 +0200434 group_names = of_find_property(np, group_names_propname, NULL);
435
Haojian Zhuangad4e1a72013-02-17 19:42:48 +0800436 for (;; index++) {
Stephen Warrend9fe0032013-08-14 15:27:12 -0600437 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
438 index, &pinspec);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530439 if (ret)
440 break;
441
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100442 pctldev = of_pinctrl_get(pinspec.np);
Masahiro Yamada602cf632016-05-23 10:52:09 +0900443 of_node_put(pinspec.np);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100444 if (!pctldev)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200445 return -EPROBE_DEFER;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530446
Christian Ruppert586a87e2013-10-15 15:37:54 +0200447 if (pinspec.args[2]) {
448 if (group_names) {
Laurent Navet72858602015-07-07 22:22:15 +0200449 of_property_read_string_index(np,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200450 group_names_propname,
451 index, &name);
452 if (strlen(name)) {
453 pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
454 np->full_name);
455 break;
456 }
457 }
458 /* npins != 0: linear range */
459 ret = gpiochip_add_pin_range(chip,
460 pinctrl_dev_get_devname(pctldev),
461 pinspec.args[0],
462 pinspec.args[1],
463 pinspec.args[2]);
464 if (ret)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200465 return ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200466 } else {
467 /* npins == 0: special range */
468 if (pinspec.args[1]) {
469 pr_err("%s: Illegal gpio-range format.\n",
470 np->full_name);
471 break;
472 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530473
Christian Ruppert586a87e2013-10-15 15:37:54 +0200474 if (!group_names) {
475 pr_err("%s: GPIO group range requested but no %s property.\n",
476 np->full_name, group_names_propname);
477 break;
478 }
479
480 ret = of_property_read_string_index(np,
481 group_names_propname,
482 index, &name);
483 if (ret)
484 break;
485
486 if (!strlen(name)) {
487 pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
488 np->full_name);
489 break;
490 }
491
492 ret = gpiochip_add_pingroup_range(chip, pctldev,
493 pinspec.args[0], name);
494 if (ret)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200495 return ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200496 }
Haojian Zhuangad4e1a72013-02-17 19:42:48 +0800497 }
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200498
499 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530500}
501
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530502#else
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200503static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530504#endif
505
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200506int of_gpiochip_add(struct gpio_chip *chip)
Anton Vorontsov391c9702010-06-08 07:48:17 -0600507{
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200508 int status;
509
Linus Walleij58383c72015-11-04 09:56:26 +0100510 if ((!chip->of_node) && (chip->parent))
511 chip->of_node = chip->parent->of_node;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600512
513 if (!chip->of_node)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200514 return 0;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600515
516 if (!chip->of_xlate) {
517 chip->of_gpio_n_cells = 2;
518 chip->of_xlate = of_gpio_simple_xlate;
519 }
520
Masahiro Yamada1020dfd2016-06-14 19:07:05 +0900521 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
522 return -EINVAL;
523
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200524 status = of_gpiochip_add_pin_range(chip);
525 if (status)
526 return status;
527
Linus Walleijfd9c5532016-04-19 15:26:26 +0200528 /* If the chip defines names itself, these take precedence */
529 if (!chip->names)
530 of_gpiochip_set_names(chip);
531
Anton Vorontsov391c9702010-06-08 07:48:17 -0600532 of_node_get(chip->of_node);
Benoit Parrotf625d462015-02-02 11:44:44 -0600533
Geert Uytterhoeven9b972022019-03-28 14:13:47 +0100534 status = of_gpiochip_scan_gpios(chip);
535 if (status) {
536 of_node_put(chip->of_node);
537 gpiochip_remove_pin_ranges(chip);
538 }
539
540 return status;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600541}
542
543void of_gpiochip_remove(struct gpio_chip *chip)
544{
Linus Walleije93fa3f2012-11-06 15:03:47 +0100545 gpiochip_remove_pin_ranges(chip);
Julia Lawall8a691552014-08-08 12:07:51 +0200546 of_node_put(chip->of_node);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600547}