blob: 75e7b3919ea7756f0d417eee62855145ca678957 [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>
Linus Walleij7d4defe2016-06-08 10:58:20 +020019#include <linux/io-mapping.h>
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070020#include <linux/gpio/consumer.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100021#include <linux/of.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060022#include <linux/of_address.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100023#include <linux/of_gpio.h>
Shiraz Hashimf23f1512012-10-27 15:21:36 +053024#include <linux/pinctrl/pinctrl.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060025#include <linux/slab.h>
Benoit Parrotf625d462015-02-02 11:44:44 -060026#include <linux/gpio/machine.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100027
Alexandre Courbot1bd6b602014-07-22 16:17:41 +090028#include "gpiolib.h"
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070029
Masahiro Yamada762c2e42016-06-14 19:07:06 +090030static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
Grant Likely3d0f7cf2012-05-17 13:54:40 -060031{
Masahiro Yamada762c2e42016-06-14 19:07:06 +090032 return chip->gpiodev->dev.of_node == data;
33}
Grant Likely3d0f7cf2012-05-17 13:54:40 -060034
Masahiro Yamada762c2e42016-06-14 19:07:06 +090035static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
36{
37 return gpiochip_find(np, of_gpiochip_match_node);
Grant Likely3d0f7cf2012-05-17 13:54:40 -060038}
39
Masahiro Yamada99468c12016-06-14 19:07:07 +090040static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
41 struct of_phandle_args *gpiospec,
42 enum of_gpio_flags *flags)
43{
Anton Vorontsov863fbf42008-04-11 23:06:45 +100044 int ret;
45
Masahiro Yamada99468c12016-06-14 19:07:07 +090046 if (chip->of_gpio_n_cells != gpiospec->args_count)
47 return ERR_PTR(-EINVAL);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100048
Masahiro Yamada99468c12016-06-14 19:07:07 +090049 ret = chip->of_xlate(chip, gpiospec, flags);
50 if (ret < 0)
51 return ERR_PTR(ret);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100052
Masahiro Yamada99468c12016-06-14 19:07:07 +090053 return gpiochip_get_desc(chip, ret);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100054}
55
Anton Vorontsovb908b532008-12-01 06:30:04 +000056/**
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070057 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
Anton Vorontsov863fbf42008-04-11 23:06:45 +100058 * @np: device node to get GPIO from
John Bonesioa6b09192011-06-27 16:49:57 -070059 * @propname: property name containing gpio specifier(s)
Anton Vorontsov863fbf42008-04-11 23:06:45 +100060 * @index: index of the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +000061 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +100062 *
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070063 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
Anton Vorontsovb908b532008-12-01 06:30:04 +000064 * value on the error condition. If @flags is not NULL the function also fills
65 * in flags for the GPIO.
Anton Vorontsov863fbf42008-04-11 23:06:45 +100066 */
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070067struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
68 const char *propname, int index, enum of_gpio_flags *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +100069{
Masahiro Yamada762c2e42016-06-14 19:07:06 +090070 struct of_phandle_args gpiospec;
71 struct gpio_chip *chip;
72 struct gpio_desc *desc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +000073 int ret;
Anton Vorontsov863fbf42008-04-11 23:06:45 +100074
Grant Likely3d0f7cf2012-05-17 13:54:40 -060075 ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
Masahiro Yamada762c2e42016-06-14 19:07:06 +090076 &gpiospec);
Grant Likely3d0f7cf2012-05-17 13:54:40 -060077 if (ret) {
Tushar Behera85ea29a2014-07-04 15:22:09 +053078 pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
79 __func__, propname, np->full_name, index);
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070080 return ERR_PTR(ret);
Grant Likely3d0f7cf2012-05-17 13:54:40 -060081 }
Anton Vorontsov863fbf42008-04-11 23:06:45 +100082
Masahiro Yamada762c2e42016-06-14 19:07:06 +090083 chip = of_find_gpiochip_by_node(gpiospec.np);
84 if (!chip) {
85 desc = ERR_PTR(-EPROBE_DEFER);
86 goto out;
87 }
Grant Likely3d0f7cf2012-05-17 13:54:40 -060088
Masahiro Yamada99468c12016-06-14 19:07:07 +090089 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
Masahiro Yamada762c2e42016-06-14 19:07:06 +090090 if (IS_ERR(desc))
91 goto out;
92
Tushar Behera85ea29a2014-07-04 15:22:09 +053093 pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
94 __func__, propname, np->full_name, index,
Masahiro Yamada762c2e42016-06-14 19:07:06 +090095 PTR_ERR_OR_ZERO(desc));
96
97out:
98 of_node_put(gpiospec.np);
99
100 return desc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000101}
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000102
Alexandre Courbotf01d9072014-05-17 14:54:50 +0900103int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
104 int index, enum of_gpio_flags *flags)
105{
106 struct gpio_desc *desc;
107
108 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
109
110 if (IS_ERR(desc))
111 return PTR_ERR(desc);
112 else
113 return desc_to_gpio(desc);
114}
115EXPORT_SYMBOL(of_get_named_gpio_flags);
116
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000117/**
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200118 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
Benoit Parrotf625d462015-02-02 11:44:44 -0600119 * @np: device node to get GPIO from
Masahiro Yamadabe715342016-06-14 19:07:04 +0900120 * @chip: GPIO chip whose hog is parsed
Benoit Parrotf625d462015-02-02 11:44:44 -0600121 * @name: GPIO line name
122 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200123 * of_parse_own_gpio()
Benoit Parrotf625d462015-02-02 11:44:44 -0600124 * @dflags: gpiod_flags - optional GPIO initialization flags
125 *
126 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
127 * value on the error condition.
128 */
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200129static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
Masahiro Yamadabe715342016-06-14 19:07:04 +0900130 struct gpio_chip *chip,
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200131 const char **name,
132 enum gpio_lookup_flags *lflags,
133 enum gpiod_flags *dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -0600134{
135 struct device_node *chip_np;
136 enum of_gpio_flags xlate_flags;
Masahiro Yamadabe715342016-06-14 19:07:04 +0900137 struct of_phandle_args gpiospec;
138 struct gpio_desc *desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600139 u32 tmp;
Masahiro Yamada3f9547e2016-06-14 19:07:03 +0900140 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600141
Masahiro Yamadabe715342016-06-14 19:07:04 +0900142 chip_np = chip->of_node;
Benoit Parrotf625d462015-02-02 11:44:44 -0600143 if (!chip_np)
144 return ERR_PTR(-EINVAL);
145
146 xlate_flags = 0;
147 *lflags = 0;
148 *dflags = 0;
149
150 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
151 if (ret)
152 return ERR_PTR(ret);
153
Masahiro Yamadabe715342016-06-14 19:07:04 +0900154 gpiospec.np = chip_np;
155 gpiospec.args_count = tmp;
Benoit Parrotf625d462015-02-02 11:44:44 -0600156
Masahiro Yamadabe715342016-06-14 19:07:04 +0900157 ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp);
Masahiro Yamada3f9547e2016-06-14 19:07:03 +0900158 if (ret)
159 return ERR_PTR(ret);
Benoit Parrotf625d462015-02-02 11:44:44 -0600160
Masahiro Yamada99468c12016-06-14 19:07:07 +0900161 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
Masahiro Yamadabe715342016-06-14 19:07:04 +0900162 if (IS_ERR(desc))
163 return desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600164
165 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
166 *lflags |= GPIO_ACTIVE_LOW;
167
168 if (of_property_read_bool(np, "input"))
169 *dflags |= GPIOD_IN;
170 else if (of_property_read_bool(np, "output-low"))
171 *dflags |= GPIOD_OUT_LOW;
172 else if (of_property_read_bool(np, "output-high"))
173 *dflags |= GPIOD_OUT_HIGH;
174 else {
175 pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
Masahiro Yamadabe715342016-06-14 19:07:04 +0900176 desc_to_gpio(desc), np->name);
Benoit Parrotf625d462015-02-02 11:44:44 -0600177 return ERR_PTR(-EINVAL);
178 }
179
180 if (name && of_property_read_string(np, "line-name", name))
181 *name = np->name;
182
Masahiro Yamadabe715342016-06-14 19:07:04 +0900183 return desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600184}
185
186/**
Linus Walleijfd9c5532016-04-19 15:26:26 +0200187 * of_gpiochip_set_names() - set up the names of the lines
188 * @chip: GPIO chip whose lines should be named, if possible
189 */
190static void of_gpiochip_set_names(struct gpio_chip *gc)
191{
192 struct gpio_device *gdev = gc->gpiodev;
193 struct device_node *np = gc->of_node;
194 int i;
195 int nstrings;
196
197 nstrings = of_property_count_strings(np, "gpio-line-names");
198 if (nstrings <= 0)
199 /* Lines names not present */
200 return;
201
202 /* This is normally not what you want */
203 if (gdev->ngpio != nstrings)
204 dev_info(&gdev->dev, "gpio-line-names specifies %d line "
205 "names but there are %d lines on the chip\n",
206 nstrings, gdev->ngpio);
207
208 /*
209 * Make sure to not index beyond the end of the number of descriptors
210 * of the GPIO device.
211 */
212 for (i = 0; i < gdev->ngpio; i++) {
213 const char *name;
214 int ret;
215
216 ret = of_property_read_string_index(np,
217 "gpio-line-names",
218 i,
219 &name);
220 if (ret) {
221 if (ret != -ENODATA)
222 dev_err(&gdev->dev,
223 "unable to name line %d: %d\n",
224 i, ret);
225 break;
226 }
227 gdev->descs[i].name = name;
228 }
229}
230
231/**
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200232 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
Benoit Parrotf625d462015-02-02 11:44:44 -0600233 * @chip: gpio chip to act on
234 *
235 * This is only used by of_gpiochip_add to request/set GPIO initial
236 * configuration.
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530237 * It retures error if it fails otherwise 0 on success.
Benoit Parrotf625d462015-02-02 11:44:44 -0600238 */
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530239static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
Benoit Parrotf625d462015-02-02 11:44:44 -0600240{
241 struct gpio_desc *desc = NULL;
242 struct device_node *np;
243 const char *name;
244 enum gpio_lookup_flags lflags;
245 enum gpiod_flags dflags;
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530246 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600247
Laxman Dewangand1279d92016-03-11 19:13:20 +0530248 for_each_available_child_of_node(chip->of_node, np) {
Benoit Parrotf625d462015-02-02 11:44:44 -0600249 if (!of_property_read_bool(np, "gpio-hog"))
250 continue;
251
Masahiro Yamadabe715342016-06-14 19:07:04 +0900252 desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600253 if (IS_ERR(desc))
254 continue;
255
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530256 ret = gpiod_hog(desc, name, lflags, dflags);
257 if (ret < 0)
258 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600259 }
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530260
261 return 0;
Benoit Parrotf625d462015-02-02 11:44:44 -0600262}
263
264/**
Anton Vorontsovb908b532008-12-01 06:30:04 +0000265 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600266 * @gc: pointer to the gpio_chip structure
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000267 * @np: device node of the GPIO chip
268 * @gpio_spec: gpio specifier as found in the device tree
Anton Vorontsovb908b532008-12-01 06:30:04 +0000269 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000270 *
271 * This is simple translation function, suitable for the most 1:1 mapped
272 * gpio chips. This function performs only one sanity check: whether gpio
273 * is less than ngpios (that is specified in the gpio_chip).
274 */
Grant Likely15c9a0a2011-12-12 09:25:57 -0700275int of_gpio_simple_xlate(struct gpio_chip *gc,
276 const struct of_phandle_args *gpiospec, u32 *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000277{
Anton Vorontsovb908b532008-12-01 06:30:04 +0000278 /*
279 * We're discouraging gpio_cells < 2, since that way you'll have to
Colin Cronin20a8a962015-05-18 11:41:43 -0700280 * write your own xlate function (that will have to retrieve the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +0000281 * number and the flags from a single gpio cell -- this is possible,
282 * but not recommended).
283 */
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600284 if (gc->of_gpio_n_cells < 2) {
Anton Vorontsovb908b532008-12-01 06:30:04 +0000285 WARN_ON(1);
286 return -EINVAL;
287 }
288
Grant Likely15c9a0a2011-12-12 09:25:57 -0700289 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
290 return -EINVAL;
291
Roland Stigge6270d832012-04-04 02:02:58 +0200292 if (gpiospec->args[0] >= gc->ngpio)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000293 return -EINVAL;
294
Anton Vorontsovb908b532008-12-01 06:30:04 +0000295 if (flags)
Grant Likely15c9a0a2011-12-12 09:25:57 -0700296 *flags = gpiospec->args[1];
Anton Vorontsovb908b532008-12-01 06:30:04 +0000297
Grant Likely15c9a0a2011-12-12 09:25:57 -0700298 return gpiospec->args[0];
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000299}
Jamie Iles3038bbd2011-07-28 16:25:41 +0100300EXPORT_SYMBOL(of_gpio_simple_xlate);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000301
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000302/**
Linus Walleij3208b0f2015-12-04 15:13:53 +0100303 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000304 * @np: device node of the GPIO chip
305 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
Linus Walleij3208b0f2015-12-04 15:13:53 +0100306 * @data: driver data to store in the struct gpio_chip
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000307 *
308 * To use this function you should allocate and fill mm_gc with:
309 *
310 * 1) In the gpio_chip structure:
311 * - all the callbacks
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600312 * - of_gpio_n_cells
313 * - of_xlate callback (optional)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000314 *
315 * 3) In the of_mm_gpio_chip structure:
316 * - save_regs callback (optional)
317 *
318 * If succeeded, this function will map bank's memory and will
319 * do all necessary work for you. Then you'll able to use .regs
320 * to manage GPIOs from the callbacks.
321 */
Linus Walleij3208b0f2015-12-04 15:13:53 +0100322int of_mm_gpiochip_add_data(struct device_node *np,
323 struct of_mm_gpio_chip *mm_gc,
324 void *data)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000325{
326 int ret = -ENOMEM;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600327 struct gpio_chip *gc = &mm_gc->gc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000328
329 gc->label = kstrdup(np->full_name, GFP_KERNEL);
330 if (!gc->label)
331 goto err0;
332
333 mm_gc->regs = of_iomap(np, 0);
334 if (!mm_gc->regs)
335 goto err1;
336
Anton Vorontsov21451152008-04-30 00:05:24 +1000337 gc->base = -1;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000338
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000339 if (mm_gc->save_regs)
340 mm_gc->save_regs(mm_gc);
341
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600342 mm_gc->gc.of_node = np;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000343
Linus Walleij3208b0f2015-12-04 15:13:53 +0100344 ret = gpiochip_add_data(gc, data);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000345 if (ret)
346 goto err2;
347
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000348 return 0;
349err2:
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000350 iounmap(mm_gc->regs);
351err1:
352 kfree(gc->label);
353err0:
354 pr_err("%s: GPIO chip registration failed with status %d\n",
355 np->full_name, ret);
356 return ret;
357}
Linus Walleij3208b0f2015-12-04 15:13:53 +0100358EXPORT_SYMBOL(of_mm_gpiochip_add_data);
Grant Likely594fa262010-06-08 07:48:16 -0600359
Ricardo Ribalda Delgadod621e8b2014-12-17 16:51:13 +0100360/**
361 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
362 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
363 */
364void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
365{
366 struct gpio_chip *gc = &mm_gc->gc;
367
368 if (!mm_gc)
369 return;
370
371 gpiochip_remove(gc);
372 iounmap(mm_gc->regs);
373 kfree(gc->label);
374}
375EXPORT_SYMBOL(of_mm_gpiochip_remove);
376
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530377#ifdef CONFIG_PINCTRL
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200378static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530379{
380 struct device_node *np = chip->of_node;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530381 struct of_phandle_args pinspec;
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100382 struct pinctrl_dev *pctldev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530383 int index = 0, ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200384 const char *name;
385 static const char group_names_propname[] = "gpio-ranges-group-names";
386 struct property *group_names;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530387
388 if (!np)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200389 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530390
Christian Ruppert586a87e2013-10-15 15:37:54 +0200391 group_names = of_find_property(np, group_names_propname, NULL);
392
Haojian Zhuangad4e1a72013-02-17 19:42:48 +0800393 for (;; index++) {
Stephen Warrend9fe0032013-08-14 15:27:12 -0600394 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
395 index, &pinspec);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530396 if (ret)
397 break;
398
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100399 pctldev = of_pinctrl_get(pinspec.np);
Masahiro Yamada602cf632016-05-23 10:52:09 +0900400 of_node_put(pinspec.np);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100401 if (!pctldev)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200402 return -EPROBE_DEFER;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530403
Christian Ruppert586a87e2013-10-15 15:37:54 +0200404 if (pinspec.args[2]) {
405 if (group_names) {
Laurent Navet72858602015-07-07 22:22:15 +0200406 of_property_read_string_index(np,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200407 group_names_propname,
408 index, &name);
409 if (strlen(name)) {
410 pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
411 np->full_name);
412 break;
413 }
414 }
415 /* npins != 0: linear range */
416 ret = gpiochip_add_pin_range(chip,
417 pinctrl_dev_get_devname(pctldev),
418 pinspec.args[0],
419 pinspec.args[1],
420 pinspec.args[2]);
421 if (ret)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200422 return ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200423 } else {
424 /* npins == 0: special range */
425 if (pinspec.args[1]) {
426 pr_err("%s: Illegal gpio-range format.\n",
427 np->full_name);
428 break;
429 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530430
Christian Ruppert586a87e2013-10-15 15:37:54 +0200431 if (!group_names) {
432 pr_err("%s: GPIO group range requested but no %s property.\n",
433 np->full_name, group_names_propname);
434 break;
435 }
436
437 ret = of_property_read_string_index(np,
438 group_names_propname,
439 index, &name);
440 if (ret)
441 break;
442
443 if (!strlen(name)) {
444 pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
445 np->full_name);
446 break;
447 }
448
449 ret = gpiochip_add_pingroup_range(chip, pctldev,
450 pinspec.args[0], name);
451 if (ret)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200452 return ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200453 }
Haojian Zhuangad4e1a72013-02-17 19:42:48 +0800454 }
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200455
456 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530457}
458
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530459#else
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200460static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530461#endif
462
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200463int of_gpiochip_add(struct gpio_chip *chip)
Anton Vorontsov391c9702010-06-08 07:48:17 -0600464{
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200465 int status;
466
Linus Walleij58383c72015-11-04 09:56:26 +0100467 if ((!chip->of_node) && (chip->parent))
468 chip->of_node = chip->parent->of_node;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600469
470 if (!chip->of_node)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200471 return 0;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600472
473 if (!chip->of_xlate) {
474 chip->of_gpio_n_cells = 2;
475 chip->of_xlate = of_gpio_simple_xlate;
476 }
477
Masahiro Yamada1020dfd2016-06-14 19:07:05 +0900478 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
479 return -EINVAL;
480
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200481 status = of_gpiochip_add_pin_range(chip);
482 if (status)
483 return status;
484
Linus Walleijfd9c5532016-04-19 15:26:26 +0200485 /* If the chip defines names itself, these take precedence */
486 if (!chip->names)
487 of_gpiochip_set_names(chip);
488
Anton Vorontsov391c9702010-06-08 07:48:17 -0600489 of_node_get(chip->of_node);
Benoit Parrotf625d462015-02-02 11:44:44 -0600490
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530491 return of_gpiochip_scan_gpios(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600492}
493
494void of_gpiochip_remove(struct gpio_chip *chip)
495{
Linus Walleije93fa3f2012-11-06 15:03:47 +0100496 gpiochip_remove_pin_ranges(chip);
Julia Lawall8a691552014-08-08 12:07:51 +0200497 of_node_put(chip->of_node);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600498}