blob: aec7bd86ae7eaab69d4849f2c862301b7d3d7eb8 [file] [log] [blame]
Vladimir Zapolskiy27038c32018-11-22 15:59:01 +02001// SPDX-License-Identifier: GPL-2.0+
Anton Vorontsov863fbf42008-04-11 23:06:45 +10002/*
3 * OF helpers for the GPIO API
4 *
5 * Copyright (c) 2007-2008 MontaVista Software, Inc.
6 *
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
Anton Vorontsov863fbf42008-04-11 23:06:45 +10008 */
9
Grant Likely2e13cba2010-07-05 16:11:55 -060010#include <linux/device.h>
Sachin Kamatbea4dbe2014-01-27 12:15:08 +053011#include <linux/err.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100012#include <linux/errno.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060013#include <linux/module.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100014#include <linux/io.h>
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070015#include <linux/gpio/consumer.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100016#include <linux/of.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060017#include <linux/of_address.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100018#include <linux/of_gpio.h>
Shiraz Hashimf23f1512012-10-27 15:21:36 +053019#include <linux/pinctrl/pinctrl.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060020#include <linux/slab.h>
Benoit Parrotf625d462015-02-02 11:44:44 -060021#include <linux/gpio/machine.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100022
Alexandre Courbot1bd6b602014-07-22 16:17:41 +090023#include "gpiolib.h"
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070024
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090025static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
Grant Likely3d0f7cf2012-05-17 13:54:40 -060026{
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090027 struct of_phandle_args *gpiospec = data;
28
29 return chip->gpiodev->dev.of_node == gpiospec->np &&
Vincent Whitchurchd49b48f2018-08-31 09:04:18 +020030 chip->of_xlate &&
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090031 chip->of_xlate(chip, gpiospec, NULL) >= 0;
Masahiro Yamada762c2e42016-06-14 19:07:06 +090032}
Grant Likely3d0f7cf2012-05-17 13:54:40 -060033
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090034static struct gpio_chip *of_find_gpiochip_by_xlate(
35 struct of_phandle_args *gpiospec)
Masahiro Yamada762c2e42016-06-14 19:07:06 +090036{
Masahiro Yamadac7e9d392016-10-25 10:47:44 +090037 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
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
Linus Walleija603a2b2017-12-30 16:26:36 +010056static void of_gpio_flags_quirks(struct device_node *np,
Linus Walleij89a5e152018-12-17 22:36:25 +010057 const char *propname,
Linus Walleij6953c572018-09-04 11:01:58 +020058 enum of_gpio_flags *flags,
59 int index)
Linus Walleija603a2b2017-12-30 16:26:36 +010060{
61 /*
Linus Walleij81c85ec2018-11-26 14:51:23 +010062 * Handle MMC "cd-inverted" and "wp-inverted" semantics.
63 */
64 if (IS_ENABLED(CONFIG_MMC)) {
Linus Walleij89a5e152018-12-17 22:36:25 +010065 /*
66 * Active low is the default according to the
67 * SDHCI specification and the device tree
68 * bindings. However the code in the current
69 * kernel was written such that the phandle
70 * flags were always respected, and "cd-inverted"
71 * would invert the flag from the device phandle.
72 */
73 if (!strcmp(propname, "cd-gpios")) {
74 if (of_property_read_bool(np, "cd-inverted"))
75 *flags ^= OF_GPIO_ACTIVE_LOW;
Linus Walleij81c85ec2018-11-26 14:51:23 +010076 }
Linus Walleij89a5e152018-12-17 22:36:25 +010077 if (!strcmp(propname, "wp-gpios")) {
78 if (of_property_read_bool(np, "wp-inverted"))
79 *flags ^= OF_GPIO_ACTIVE_LOW;
Linus Walleij81c85ec2018-11-26 14:51:23 +010080 }
81 }
82 /*
Linus Walleija603a2b2017-12-30 16:26:36 +010083 * Some GPIO fixed regulator quirks.
84 * Note that active low is the default.
85 */
86 if (IS_ENABLED(CONFIG_REGULATOR) &&
Linus Walleij906402a2018-06-11 15:11:41 +020087 (of_device_is_compatible(np, "regulator-fixed") ||
88 of_device_is_compatible(np, "reg-fixed-voltage") ||
Geert Uytterhoevena71a81e2019-03-29 09:42:29 +010089 (!(strcmp(propname, "enable-gpio") &&
90 strcmp(propname, "enable-gpios")) &&
91 of_device_is_compatible(np, "regulator-gpio")))) {
Linus Walleija603a2b2017-12-30 16:26:36 +010092 /*
93 * The regulator GPIO handles are specified such that the
94 * presence or absence of "enable-active-high" solely controls
95 * the polarity of the GPIO line. Any phandle flags must
96 * be actively ignored.
97 */
98 if (*flags & OF_GPIO_ACTIVE_LOW) {
99 pr_warn("%s GPIO handle specifies active low - ignored\n",
100 of_node_full_name(np));
101 *flags &= ~OF_GPIO_ACTIVE_LOW;
102 }
103 if (!of_property_read_bool(np, "enable-active-high"))
104 *flags |= OF_GPIO_ACTIVE_LOW;
105 }
106 /*
107 * Legacy open drain handling for fixed voltage regulators.
108 */
109 if (IS_ENABLED(CONFIG_REGULATOR) &&
110 of_device_is_compatible(np, "reg-fixed-voltage") &&
111 of_property_read_bool(np, "gpio-open-drain")) {
112 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
113 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
114 of_node_full_name(np));
115 }
Linus Walleij6953c572018-09-04 11:01:58 +0200116
117 /*
118 * Legacy handling of SPI active high chip select. If we have a
119 * property named "cs-gpios" we need to inspect the child node
120 * to determine if the flags should have inverted semantics.
121 */
Geert Uytterhoevena71a81e2019-03-29 09:42:29 +0100122 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
123 of_property_read_bool(np, "cs-gpios")) {
Linus Walleij6953c572018-09-04 11:01:58 +0200124 struct device_node *child;
125 u32 cs;
126 int ret;
127
128 for_each_child_of_node(np, child) {
129 ret = of_property_read_u32(child, "reg", &cs);
Linus Walleijc1c04ce2019-01-16 09:21:10 +0100130 if (ret)
Linus Walleij6953c572018-09-04 11:01:58 +0200131 continue;
132 if (cs == index) {
133 /*
134 * SPI children have active low chip selects
135 * by default. This can be specified negatively
136 * by just omitting "spi-cs-high" in the
137 * device node, or actively by tagging on
138 * GPIO_ACTIVE_LOW as flag in the device
139 * tree. If the line is simultaneously
140 * tagged as active low in the device tree
141 * and has the "spi-cs-high" set, we get a
142 * conflict and the "spi-cs-high" flag will
143 * take precedence.
144 */
Andrey Smirnov7ce402772019-03-25 23:32:09 -0700145 if (of_property_read_bool(child, "spi-cs-high")) {
Linus Walleij6953c572018-09-04 11:01:58 +0200146 if (*flags & OF_GPIO_ACTIVE_LOW) {
147 pr_warn("%s GPIO handle specifies active low - ignored\n",
Andrey Smirnov7ce402772019-03-25 23:32:09 -0700148 of_node_full_name(child));
Linus Walleij6953c572018-09-04 11:01:58 +0200149 *flags &= ~OF_GPIO_ACTIVE_LOW;
150 }
151 } else {
152 if (!(*flags & OF_GPIO_ACTIVE_LOW))
153 pr_info("%s enforce active low on chipselect handle\n",
Andrey Smirnov7ce402772019-03-25 23:32:09 -0700154 of_node_full_name(child));
Linus Walleij6953c572018-09-04 11:01:58 +0200155 *flags |= OF_GPIO_ACTIVE_LOW;
156 }
157 break;
158 }
159 }
160 }
Linus Walleija603a2b2017-12-30 16:26:36 +0100161}
162
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000163/**
Alexandre Courbotaf8b6372013-10-17 10:21:37 -0700164 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000165 * @np: device node to get GPIO from
John Bonesioa6b09192011-06-27 16:49:57 -0700166 * @propname: property name containing gpio specifier(s)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000167 * @index: index of the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +0000168 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000169 *
Alexandre Courbotaf8b6372013-10-17 10:21:37 -0700170 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
Anton Vorontsovb908b532008-12-01 06:30:04 +0000171 * value on the error condition. If @flags is not NULL the function also fills
172 * in flags for the GPIO.
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000173 */
Alexandre Courbotaf8b6372013-10-17 10:21:37 -0700174struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
175 const char *propname, int index, enum of_gpio_flags *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000176{
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900177 struct of_phandle_args gpiospec;
178 struct gpio_chip *chip;
179 struct gpio_desc *desc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000180 int ret;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000181
Stephen Boydc11e6f02018-01-30 18:36:18 -0800182 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
183 &gpiospec);
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600184 if (ret) {
Rob Herring7eb6ce22017-07-18 16:43:03 -0500185 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
186 __func__, propname, np, index);
Alexandre Courbotaf8b6372013-10-17 10:21:37 -0700187 return ERR_PTR(ret);
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600188 }
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000189
Masahiro Yamadac7e9d392016-10-25 10:47:44 +0900190 chip = of_find_gpiochip_by_xlate(&gpiospec);
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900191 if (!chip) {
192 desc = ERR_PTR(-EPROBE_DEFER);
193 goto out;
194 }
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600195
Masahiro Yamada99468c12016-06-14 19:07:07 +0900196 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900197 if (IS_ERR(desc))
198 goto out;
199
Linus Walleij605f2d32018-01-16 23:44:46 +0100200 if (flags)
Linus Walleij89a5e152018-12-17 22:36:25 +0100201 of_gpio_flags_quirks(np, propname, flags, index);
Linus Walleija603a2b2017-12-30 16:26:36 +0100202
Rob Herring7eb6ce22017-07-18 16:43:03 -0500203 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
204 __func__, propname, np, index,
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900205 PTR_ERR_OR_ZERO(desc));
206
207out:
208 of_node_put(gpiospec.np);
209
210 return desc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000211}
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000212
Alexandre Courbotf01d9072014-05-17 14:54:50 +0900213int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
214 int index, enum of_gpio_flags *flags)
215{
216 struct gpio_desc *desc;
217
218 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
219
220 if (IS_ERR(desc))
221 return PTR_ERR(desc);
222 else
223 return desc_to_gpio(desc);
224}
225EXPORT_SYMBOL(of_get_named_gpio_flags);
226
Linus Walleijc8582332017-12-27 16:37:44 +0100227/*
228 * The SPI GPIO bindings happened before we managed to establish that GPIO
229 * properties should be named "foo-gpios" so we have this special kludge for
230 * them.
231 */
232static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
233 enum of_gpio_flags *of_flags)
234{
235 char prop_name[32]; /* 32 is max size of property name */
236 struct device_node *np = dev->of_node;
237 struct gpio_desc *desc;
238
239 /*
240 * Hopefully the compiler stubs the rest of the function if this
241 * is false.
242 */
243 if (!IS_ENABLED(CONFIG_SPI_MASTER))
244 return ERR_PTR(-ENOENT);
245
246 /* Allow this specifically for "spi-gpio" devices */
247 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
248 return ERR_PTR(-ENOENT);
249
250 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
251 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
252
253 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
254 return desc;
255}
256
Linus Walleij6a537d42017-12-27 16:37:44 +0100257/*
258 * Some regulator bindings happened before we managed to establish that GPIO
259 * properties should be named "foo-gpios" so we have this special kludge for
260 * them.
261 */
262static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
263 enum of_gpio_flags *of_flags)
264{
265 /* These are the connection IDs we accept as legacy GPIO phandles */
266 const char *whitelist[] = {
267 "wlf,ldoena", /* Arizona */
268 "wlf,ldo1ena", /* WM8994 */
269 "wlf,ldo2ena", /* WM8994 */
270 };
271 struct device_node *np = dev->of_node;
272 struct gpio_desc *desc;
273 int i;
274
275 if (!IS_ENABLED(CONFIG_REGULATOR))
276 return ERR_PTR(-ENOENT);
277
278 if (!con_id)
279 return ERR_PTR(-ENOENT);
280
Andy Shevchenko4b21f942018-05-03 20:14:46 +0300281 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
282 if (i < 0)
Linus Walleij6a537d42017-12-27 16:37:44 +0100283 return ERR_PTR(-ENOENT);
284
285 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
286 return desc;
287}
288
Linus Walleijea713bc2016-10-03 10:09:40 +0200289struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300290 unsigned int idx, unsigned long *flags)
Linus Walleijea713bc2016-10-03 10:09:40 +0200291{
292 char prop_name[32]; /* 32 is max size of property name */
293 enum of_gpio_flags of_flags;
294 struct gpio_desc *desc;
295 unsigned int i;
296
Linus Walleijc8582332017-12-27 16:37:44 +0100297 /* Try GPIO property "foo-gpios" and "foo-gpio" */
Linus Walleijea713bc2016-10-03 10:09:40 +0200298 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
299 if (con_id)
300 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
301 gpio_suffixes[i]);
302 else
303 snprintf(prop_name, sizeof(prop_name), "%s",
304 gpio_suffixes[i]);
305
306 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
307 &of_flags);
Maxime Ripard6662ae62018-02-21 09:11:00 +0100308 /*
309 * -EPROBE_DEFER in our case means that we found a
310 * valid GPIO property, but no controller has been
311 * registered so far.
312 *
313 * This means we don't need to look any further for
314 * alternate name conventions, and we should really
315 * preserve the return code for our user to be able to
316 * retry probing later.
317 */
318 if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
319 return desc;
320
Linus Walleijea713bc2016-10-03 10:09:40 +0200321 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
322 break;
323 }
324
Linus Walleijc8582332017-12-27 16:37:44 +0100325 /* Special handling for SPI GPIOs if used */
326 if (IS_ERR(desc))
327 desc = of_find_spi_gpio(dev, con_id, &of_flags);
328
Linus Walleij6a537d42017-12-27 16:37:44 +0100329 /* Special handling for regulator GPIOs if used */
Chen-Yu Tsaice27fb22018-02-13 14:08:14 +0800330 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
Linus Walleij6a537d42017-12-27 16:37:44 +0100331 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
332
Linus Walleijea713bc2016-10-03 10:09:40 +0200333 if (IS_ERR(desc))
334 return desc;
335
336 if (of_flags & OF_GPIO_ACTIVE_LOW)
337 *flags |= GPIO_ACTIVE_LOW;
338
339 if (of_flags & OF_GPIO_SINGLE_ENDED) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +0530340 if (of_flags & OF_GPIO_OPEN_DRAIN)
Linus Walleijea713bc2016-10-03 10:09:40 +0200341 *flags |= GPIO_OPEN_DRAIN;
342 else
343 *flags |= GPIO_OPEN_SOURCE;
344 }
345
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030346 if (of_flags & OF_GPIO_TRANSITORY)
347 *flags |= GPIO_TRANSITORY;
Charles Keepax05f479b2017-05-23 15:47:29 +0100348
Thomas Petazzonid4499912019-02-07 17:28:58 +0100349 if (of_flags & OF_GPIO_PULL_UP)
350 *flags |= GPIO_PULL_UP;
351 if (of_flags & OF_GPIO_PULL_DOWN)
352 *flags |= GPIO_PULL_DOWN;
353
Linus Walleijea713bc2016-10-03 10:09:40 +0200354 return desc;
355}
356
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000357/**
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200358 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
Benoit Parrotf625d462015-02-02 11:44:44 -0600359 * @np: device node to get GPIO from
Masahiro Yamadabe715342016-06-14 19:07:04 +0900360 * @chip: GPIO chip whose hog is parsed
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100361 * @idx: Index of the GPIO to parse
Benoit Parrotf625d462015-02-02 11:44:44 -0600362 * @name: GPIO line name
Andy Shevchenkofed70262019-04-10 18:39:16 +0300363 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
364 * of_find_gpio() or of_parse_own_gpio()
Benoit Parrotf625d462015-02-02 11:44:44 -0600365 * @dflags: gpiod_flags - optional GPIO initialization flags
366 *
367 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
368 * value on the error condition.
369 */
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200370static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
Masahiro Yamadabe715342016-06-14 19:07:04 +0900371 struct gpio_chip *chip,
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100372 unsigned int idx, const char **name,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300373 unsigned long *lflags,
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200374 enum gpiod_flags *dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -0600375{
376 struct device_node *chip_np;
377 enum of_gpio_flags xlate_flags;
Masahiro Yamadabe715342016-06-14 19:07:04 +0900378 struct of_phandle_args gpiospec;
379 struct gpio_desc *desc;
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100380 unsigned int i;
Benoit Parrotf625d462015-02-02 11:44:44 -0600381 u32 tmp;
Masahiro Yamada3f9547e2016-06-14 19:07:03 +0900382 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600383
Masahiro Yamadabe715342016-06-14 19:07:04 +0900384 chip_np = chip->of_node;
Benoit Parrotf625d462015-02-02 11:44:44 -0600385 if (!chip_np)
386 return ERR_PTR(-EINVAL);
387
388 xlate_flags = 0;
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +0300389 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Benoit Parrotf625d462015-02-02 11:44:44 -0600390 *dflags = 0;
391
392 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
393 if (ret)
394 return ERR_PTR(ret);
395
Masahiro Yamadabe715342016-06-14 19:07:04 +0900396 gpiospec.np = chip_np;
397 gpiospec.args_count = tmp;
Benoit Parrotf625d462015-02-02 11:44:44 -0600398
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100399 for (i = 0; i < tmp; i++) {
400 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
401 &gpiospec.args[i]);
402 if (ret)
403 return ERR_PTR(ret);
404 }
Benoit Parrotf625d462015-02-02 11:44:44 -0600405
Masahiro Yamada99468c12016-06-14 19:07:07 +0900406 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
Masahiro Yamadabe715342016-06-14 19:07:04 +0900407 if (IS_ERR(desc))
408 return desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600409
410 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
411 *lflags |= GPIO_ACTIVE_LOW;
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030412 if (xlate_flags & OF_GPIO_TRANSITORY)
413 *lflags |= GPIO_TRANSITORY;
Benoit Parrotf625d462015-02-02 11:44:44 -0600414
415 if (of_property_read_bool(np, "input"))
416 *dflags |= GPIOD_IN;
417 else if (of_property_read_bool(np, "output-low"))
418 *dflags |= GPIOD_OUT_LOW;
419 else if (of_property_read_bool(np, "output-high"))
420 *dflags |= GPIOD_OUT_HIGH;
421 else {
Rob Herring62cdcb62018-08-27 20:52:19 -0500422 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
423 desc_to_gpio(desc), np);
Benoit Parrotf625d462015-02-02 11:44:44 -0600424 return ERR_PTR(-EINVAL);
425 }
426
427 if (name && of_property_read_string(np, "line-name", name))
428 *name = np->name;
429
Masahiro Yamadabe715342016-06-14 19:07:04 +0900430 return desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600431}
432
433/**
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200434 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
Benoit Parrotf625d462015-02-02 11:44:44 -0600435 * @chip: gpio chip to act on
436 *
437 * This is only used by of_gpiochip_add to request/set GPIO initial
438 * configuration.
Geert Uytterhoevenead066e2017-06-01 12:30:01 +0200439 * It returns error if it fails otherwise 0 on success.
Benoit Parrotf625d462015-02-02 11:44:44 -0600440 */
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530441static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
Benoit Parrotf625d462015-02-02 11:44:44 -0600442{
443 struct gpio_desc *desc = NULL;
444 struct device_node *np;
445 const char *name;
Andy Shevchenkofed70262019-04-10 18:39:16 +0300446 unsigned long lflags;
Benoit Parrotf625d462015-02-02 11:44:44 -0600447 enum gpiod_flags dflags;
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100448 unsigned int i;
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530449 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600450
Laxman Dewangand1279d92016-03-11 19:13:20 +0530451 for_each_available_child_of_node(chip->of_node, np) {
Benoit Parrotf625d462015-02-02 11:44:44 -0600452 if (!of_property_read_bool(np, "gpio-hog"))
453 continue;
454
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100455 for (i = 0;; i++) {
456 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
457 &dflags);
458 if (IS_ERR(desc))
459 break;
Benoit Parrotf625d462015-02-02 11:44:44 -0600460
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100461 ret = gpiod_hog(desc, name, lflags, dflags);
462 if (ret < 0) {
463 of_node_put(np);
464 return ret;
465 }
Wei Yongjun09e258a2016-10-29 16:13:52 +0000466 }
Benoit Parrotf625d462015-02-02 11:44:44 -0600467 }
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530468
469 return 0;
Benoit Parrotf625d462015-02-02 11:44:44 -0600470}
471
472/**
Thierry Reding67049c52017-07-24 16:57:23 +0200473 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600474 * @gc: pointer to the gpio_chip structure
Thierry Reding67049c52017-07-24 16:57:23 +0200475 * @gpiospec: GPIO specifier as found in the device tree
Anton Vorontsovb908b532008-12-01 06:30:04 +0000476 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000477 *
478 * This is simple translation function, suitable for the most 1:1 mapped
Thierry Reding67049c52017-07-24 16:57:23 +0200479 * GPIO chips. This function performs only one sanity check: whether GPIO
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000480 * is less than ngpios (that is specified in the gpio_chip).
481 */
Grant Likely15c9a0a2011-12-12 09:25:57 -0700482int of_gpio_simple_xlate(struct gpio_chip *gc,
483 const struct of_phandle_args *gpiospec, u32 *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000484{
Anton Vorontsovb908b532008-12-01 06:30:04 +0000485 /*
486 * We're discouraging gpio_cells < 2, since that way you'll have to
Colin Cronin20a8a962015-05-18 11:41:43 -0700487 * write your own xlate function (that will have to retrieve the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +0000488 * number and the flags from a single gpio cell -- this is possible,
489 * but not recommended).
490 */
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600491 if (gc->of_gpio_n_cells < 2) {
Anton Vorontsovb908b532008-12-01 06:30:04 +0000492 WARN_ON(1);
493 return -EINVAL;
494 }
495
Grant Likely15c9a0a2011-12-12 09:25:57 -0700496 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
497 return -EINVAL;
498
Roland Stigge6270d832012-04-04 02:02:58 +0200499 if (gpiospec->args[0] >= gc->ngpio)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000500 return -EINVAL;
501
Anton Vorontsovb908b532008-12-01 06:30:04 +0000502 if (flags)
Grant Likely15c9a0a2011-12-12 09:25:57 -0700503 *flags = gpiospec->args[1];
Anton Vorontsovb908b532008-12-01 06:30:04 +0000504
Grant Likely15c9a0a2011-12-12 09:25:57 -0700505 return gpiospec->args[0];
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000506}
Jamie Iles3038bbd2011-07-28 16:25:41 +0100507EXPORT_SYMBOL(of_gpio_simple_xlate);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000508
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000509/**
Linus Walleij3208b0f2015-12-04 15:13:53 +0100510 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000511 * @np: device node of the GPIO chip
512 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
Linus Walleij3208b0f2015-12-04 15:13:53 +0100513 * @data: driver data to store in the struct gpio_chip
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000514 *
515 * To use this function you should allocate and fill mm_gc with:
516 *
517 * 1) In the gpio_chip structure:
518 * - all the callbacks
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600519 * - of_gpio_n_cells
520 * - of_xlate callback (optional)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000521 *
522 * 3) In the of_mm_gpio_chip structure:
523 * - save_regs callback (optional)
524 *
525 * If succeeded, this function will map bank's memory and will
526 * do all necessary work for you. Then you'll able to use .regs
527 * to manage GPIOs from the callbacks.
528 */
Linus Walleij3208b0f2015-12-04 15:13:53 +0100529int of_mm_gpiochip_add_data(struct device_node *np,
530 struct of_mm_gpio_chip *mm_gc,
531 void *data)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000532{
533 int ret = -ENOMEM;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600534 struct gpio_chip *gc = &mm_gc->gc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000535
Rob Herring7eb6ce22017-07-18 16:43:03 -0500536 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000537 if (!gc->label)
538 goto err0;
539
540 mm_gc->regs = of_iomap(np, 0);
541 if (!mm_gc->regs)
542 goto err1;
543
Anton Vorontsov21451152008-04-30 00:05:24 +1000544 gc->base = -1;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000545
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000546 if (mm_gc->save_regs)
547 mm_gc->save_regs(mm_gc);
548
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600549 mm_gc->gc.of_node = np;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000550
Linus Walleij3208b0f2015-12-04 15:13:53 +0100551 ret = gpiochip_add_data(gc, data);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000552 if (ret)
553 goto err2;
554
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000555 return 0;
556err2:
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000557 iounmap(mm_gc->regs);
558err1:
559 kfree(gc->label);
560err0:
Rob Herring7eb6ce22017-07-18 16:43:03 -0500561 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000562 return ret;
563}
Linus Walleij3208b0f2015-12-04 15:13:53 +0100564EXPORT_SYMBOL(of_mm_gpiochip_add_data);
Grant Likely594fa262010-06-08 07:48:16 -0600565
Ricardo Ribalda Delgadod621e8b2014-12-17 16:51:13 +0100566/**
567 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
568 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
569 */
570void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
571{
572 struct gpio_chip *gc = &mm_gc->gc;
573
574 if (!mm_gc)
575 return;
576
577 gpiochip_remove(gc);
578 iounmap(mm_gc->regs);
579 kfree(gc->label);
580}
581EXPORT_SYMBOL(of_mm_gpiochip_remove);
582
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700583static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
584{
585 int len, i;
586 u32 start, count;
587 struct device_node *np = chip->of_node;
588
589 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
590 if (len < 0 || len % 2 != 0)
591 return;
592
593 for (i = 0; i < len; i += 2) {
594 of_property_read_u32_index(np, "gpio-reserved-ranges",
595 i, &start);
596 of_property_read_u32_index(np, "gpio-reserved-ranges",
597 i + 1, &count);
598 if (start >= chip->ngpio || start + count >= chip->ngpio)
599 continue;
600
601 bitmap_clear(chip->valid_mask, start, count);
602 }
603};
604
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530605#ifdef CONFIG_PINCTRL
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200606static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530607{
608 struct device_node *np = chip->of_node;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530609 struct of_phandle_args pinspec;
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100610 struct pinctrl_dev *pctldev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530611 int index = 0, ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200612 const char *name;
613 static const char group_names_propname[] = "gpio-ranges-group-names";
614 struct property *group_names;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530615
616 if (!np)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200617 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530618
Christian Ruppert586a87e2013-10-15 15:37:54 +0200619 group_names = of_find_property(np, group_names_propname, NULL);
620
Haojian Zhuangad4e1a72013-02-17 19:42:48 +0800621 for (;; index++) {
Stephen Warrend9fe0032013-08-14 15:27:12 -0600622 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
623 index, &pinspec);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530624 if (ret)
625 break;
626
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100627 pctldev = of_pinctrl_get(pinspec.np);
Masahiro Yamada602cf632016-05-23 10:52:09 +0900628 of_node_put(pinspec.np);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100629 if (!pctldev)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200630 return -EPROBE_DEFER;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530631
Christian Ruppert586a87e2013-10-15 15:37:54 +0200632 if (pinspec.args[2]) {
633 if (group_names) {
Laurent Navet72858602015-07-07 22:22:15 +0200634 of_property_read_string_index(np,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200635 group_names_propname,
636 index, &name);
637 if (strlen(name)) {
Rob Herring7eb6ce22017-07-18 16:43:03 -0500638 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
639 np);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200640 break;
641 }
642 }
643 /* npins != 0: linear range */
644 ret = gpiochip_add_pin_range(chip,
645 pinctrl_dev_get_devname(pctldev),
646 pinspec.args[0],
647 pinspec.args[1],
648 pinspec.args[2]);
649 if (ret)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200650 return ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200651 } else {
652 /* npins == 0: special range */
653 if (pinspec.args[1]) {
Rob Herring7eb6ce22017-07-18 16:43:03 -0500654 pr_err("%pOF: Illegal gpio-range format.\n",
655 np);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200656 break;
657 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530658
Christian Ruppert586a87e2013-10-15 15:37:54 +0200659 if (!group_names) {
Rob Herring7eb6ce22017-07-18 16:43:03 -0500660 pr_err("%pOF: GPIO group range requested but no %s property.\n",
661 np, group_names_propname);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200662 break;
663 }
664
665 ret = of_property_read_string_index(np,
666 group_names_propname,
667 index, &name);
668 if (ret)
669 break;
670
671 if (!strlen(name)) {
Rob Herring7eb6ce22017-07-18 16:43:03 -0500672 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
673 np);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200674 break;
675 }
676
677 ret = gpiochip_add_pingroup_range(chip, pctldev,
678 pinspec.args[0], name);
679 if (ret)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200680 return ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200681 }
Haojian Zhuangad4e1a72013-02-17 19:42:48 +0800682 }
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200683
684 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530685}
686
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530687#else
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200688static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530689#endif
690
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200691int of_gpiochip_add(struct gpio_chip *chip)
Anton Vorontsov391c9702010-06-08 07:48:17 -0600692{
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200693 int status;
694
Anton Vorontsov391c9702010-06-08 07:48:17 -0600695 if (!chip->of_node)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200696 return 0;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600697
698 if (!chip->of_xlate) {
699 chip->of_gpio_n_cells = 2;
700 chip->of_xlate = of_gpio_simple_xlate;
701 }
702
Masahiro Yamada1020dfd12016-06-14 19:07:05 +0900703 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
704 return -EINVAL;
705
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700706 of_gpiochip_init_valid_mask(chip);
707
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200708 status = of_gpiochip_add_pin_range(chip);
709 if (status)
710 return status;
711
Linus Walleijfd9c5532016-04-19 15:26:26 +0200712 /* If the chip defines names itself, these take precedence */
713 if (!chip->names)
Christophe Leroy82270332017-12-15 15:02:33 +0100714 devprop_gpiochip_set_names(chip,
715 of_fwnode_handle(chip->of_node));
Linus Walleijfd9c5532016-04-19 15:26:26 +0200716
Anton Vorontsov391c9702010-06-08 07:48:17 -0600717 of_node_get(chip->of_node);
Benoit Parrotf625d462015-02-02 11:44:44 -0600718
Geert Uytterhoevenf7299d42019-03-28 14:13:47 +0100719 status = of_gpiochip_scan_gpios(chip);
720 if (status) {
721 of_node_put(chip->of_node);
722 gpiochip_remove_pin_ranges(chip);
723 }
724
725 return status;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600726}
727
728void of_gpiochip_remove(struct gpio_chip *chip)
729{
Linus Walleije93fa3f2012-11-06 15:03:47 +0100730 gpiochip_remove_pin_ranges(chip);
Julia Lawall8a691552014-08-08 12:07:51 +0200731 of_node_put(chip->of_node);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600732}