blob: 3ece59185d372b7d6b67e23ecfd3f425b9a1bde2 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0+
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9
10#include <linux/device.h>
11#include <linux/err.h>
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/io.h>
15#include <linux/gpio/consumer.h>
16#include <linux/of.h>
17#include <linux/of_address.h>
18#include <linux/of_gpio.h>
19#include <linux/pinctrl/pinctrl.h>
20#include <linux/slab.h>
21#include <linux/gpio/machine.h>
22
23#include "gpiolib.h"
David Brazdil0f672f62019-12-10 10:32:29 +000024#include "gpiolib-of.h"
25
Olivier Deprez0e641232021-09-23 10:07:05 +020026/**
27 * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
28 * Some elder GPIO controllers need special quirks. Currently we handle
29 * the Freescale GPIO controller with bindings that doesn't use the
30 * established "cs-gpios" for chip selects but instead rely on
31 * "gpios" for the chip select lines. If we detect this, we redirect
32 * the counting of "cs-gpios" to count "gpios" transparent to the
33 * driver.
34 */
35int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
36{
37 struct device_node *np = dev->of_node;
38
39 if (!IS_ENABLED(CONFIG_SPI_MASTER))
40 return 0;
41 if (!con_id || strcmp(con_id, "cs"))
42 return 0;
43 if (!of_device_is_compatible(np, "fsl,spi") &&
44 !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
45 return 0;
46 return of_gpio_named_count(np, "gpios");
47}
48
David Brazdil0f672f62019-12-10 10:32:29 +000049/*
50 * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
51 *
52 * FIXME: get rid of those external users by converting them to GPIO
53 * descriptors and let them all use gpiod_get_count()
54 */
55int of_gpio_get_count(struct device *dev, const char *con_id)
56{
57 int ret;
58 char propname[32];
59 unsigned int i;
60
Olivier Deprez0e641232021-09-23 10:07:05 +020061 ret = of_gpio_spi_cs_get_count(dev, con_id);
62 if (ret > 0)
63 return ret;
64
David Brazdil0f672f62019-12-10 10:32:29 +000065 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
66 if (con_id)
67 snprintf(propname, sizeof(propname), "%s-%s",
68 con_id, gpio_suffixes[i]);
69 else
70 snprintf(propname, sizeof(propname), "%s",
71 gpio_suffixes[i]);
72
73 ret = of_gpio_named_count(dev->of_node, propname);
74 if (ret > 0)
75 break;
76 }
77 return ret ? ret : -ENOENT;
78}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079
80static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
81{
82 struct of_phandle_args *gpiospec = data;
83
84 return chip->gpiodev->dev.of_node == gpiospec->np &&
85 chip->of_xlate &&
86 chip->of_xlate(chip, gpiospec, NULL) >= 0;
87}
88
89static struct gpio_chip *of_find_gpiochip_by_xlate(
90 struct of_phandle_args *gpiospec)
91{
92 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
93}
94
95static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
96 struct of_phandle_args *gpiospec,
97 enum of_gpio_flags *flags)
98{
99 int ret;
100
101 if (chip->of_gpio_n_cells != gpiospec->args_count)
102 return ERR_PTR(-EINVAL);
103
104 ret = chip->of_xlate(chip, gpiospec, flags);
105 if (ret < 0)
106 return ERR_PTR(ret);
107
108 return gpiochip_get_desc(chip, ret);
109}
110
David Brazdil0f672f62019-12-10 10:32:29 +0000111/**
112 * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
113 * to set the .valid_mask
114 * @dev: the device for the GPIO provider
115 * @return: true if the valid mask needs to be set
116 */
117bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118{
David Brazdil0f672f62019-12-10 10:32:29 +0000119 int size;
120 struct device_node *np = gc->of_node;
121
122 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
123 if (size > 0 && size % 2 == 0)
124 return true;
125 return false;
126}
127
128static void of_gpio_flags_quirks(struct device_node *np,
129 const char *propname,
130 enum of_gpio_flags *flags,
131 int index)
132{
133 /*
134 * Handle MMC "cd-inverted" and "wp-inverted" semantics.
135 */
136 if (IS_ENABLED(CONFIG_MMC)) {
137 /*
138 * Active low is the default according to the
139 * SDHCI specification and the device tree
140 * bindings. However the code in the current
141 * kernel was written such that the phandle
142 * flags were always respected, and "cd-inverted"
143 * would invert the flag from the device phandle.
144 */
145 if (!strcmp(propname, "cd-gpios")) {
146 if (of_property_read_bool(np, "cd-inverted"))
147 *flags ^= OF_GPIO_ACTIVE_LOW;
148 }
David Brazdil0f672f62019-12-10 10:32:29 +0000149 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150 /*
151 * Some GPIO fixed regulator quirks.
152 * Note that active low is the default.
153 */
154 if (IS_ENABLED(CONFIG_REGULATOR) &&
155 (of_device_is_compatible(np, "regulator-fixed") ||
156 of_device_is_compatible(np, "reg-fixed-voltage") ||
David Brazdil0f672f62019-12-10 10:32:29 +0000157 (!(strcmp(propname, "enable-gpio") &&
158 strcmp(propname, "enable-gpios")) &&
159 of_device_is_compatible(np, "regulator-gpio")))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160 /*
161 * The regulator GPIO handles are specified such that the
162 * presence or absence of "enable-active-high" solely controls
163 * the polarity of the GPIO line. Any phandle flags must
164 * be actively ignored.
165 */
166 if (*flags & OF_GPIO_ACTIVE_LOW) {
167 pr_warn("%s GPIO handle specifies active low - ignored\n",
168 of_node_full_name(np));
169 *flags &= ~OF_GPIO_ACTIVE_LOW;
170 }
171 if (!of_property_read_bool(np, "enable-active-high"))
172 *flags |= OF_GPIO_ACTIVE_LOW;
173 }
174 /*
175 * Legacy open drain handling for fixed voltage regulators.
176 */
177 if (IS_ENABLED(CONFIG_REGULATOR) &&
178 of_device_is_compatible(np, "reg-fixed-voltage") &&
179 of_property_read_bool(np, "gpio-open-drain")) {
180 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
181 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
182 of_node_full_name(np));
183 }
David Brazdil0f672f62019-12-10 10:32:29 +0000184
185 /*
186 * Legacy handling of SPI active high chip select. If we have a
187 * property named "cs-gpios" we need to inspect the child node
188 * to determine if the flags should have inverted semantics.
189 */
190 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
191 of_property_read_bool(np, "cs-gpios")) {
192 struct device_node *child;
193 u32 cs;
194 int ret;
195
196 for_each_child_of_node(np, child) {
197 ret = of_property_read_u32(child, "reg", &cs);
198 if (ret)
199 continue;
200 if (cs == index) {
201 /*
202 * SPI children have active low chip selects
203 * by default. This can be specified negatively
204 * by just omitting "spi-cs-high" in the
205 * device node, or actively by tagging on
206 * GPIO_ACTIVE_LOW as flag in the device
207 * tree. If the line is simultaneously
208 * tagged as active low in the device tree
209 * and has the "spi-cs-high" set, we get a
210 * conflict and the "spi-cs-high" flag will
211 * take precedence.
212 */
213 if (of_property_read_bool(child, "spi-cs-high")) {
214 if (*flags & OF_GPIO_ACTIVE_LOW) {
215 pr_warn("%s GPIO handle specifies active low - ignored\n",
216 of_node_full_name(child));
217 *flags &= ~OF_GPIO_ACTIVE_LOW;
218 }
219 } else {
220 if (!(*flags & OF_GPIO_ACTIVE_LOW))
221 pr_info("%s enforce active low on chipselect handle\n",
222 of_node_full_name(child));
223 *flags |= OF_GPIO_ACTIVE_LOW;
224 }
225 of_node_put(child);
226 break;
227 }
228 }
229 }
230
231 /* Legacy handling of stmmac's active-low PHY reset line */
232 if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
233 !strcmp(propname, "snps,reset-gpio") &&
234 of_property_read_bool(np, "snps,reset-active-low"))
235 *flags |= OF_GPIO_ACTIVE_LOW;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000236}
237
238/**
239 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
240 * @np: device node to get GPIO from
241 * @propname: property name containing gpio specifier(s)
242 * @index: index of the GPIO
243 * @flags: a flags pointer to fill in
244 *
245 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
246 * value on the error condition. If @flags is not NULL the function also fills
247 * in flags for the GPIO.
248 */
David Brazdil0f672f62019-12-10 10:32:29 +0000249static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250 const char *propname, int index, enum of_gpio_flags *flags)
251{
252 struct of_phandle_args gpiospec;
253 struct gpio_chip *chip;
254 struct gpio_desc *desc;
255 int ret;
256
257 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
258 &gpiospec);
259 if (ret) {
260 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
261 __func__, propname, np, index);
262 return ERR_PTR(ret);
263 }
264
265 chip = of_find_gpiochip_by_xlate(&gpiospec);
266 if (!chip) {
267 desc = ERR_PTR(-EPROBE_DEFER);
268 goto out;
269 }
270
271 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
272 if (IS_ERR(desc))
273 goto out;
274
275 if (flags)
David Brazdil0f672f62019-12-10 10:32:29 +0000276 of_gpio_flags_quirks(np, propname, flags, index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000277
278 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
279 __func__, propname, np, index,
280 PTR_ERR_OR_ZERO(desc));
281
282out:
283 of_node_put(gpiospec.np);
284
285 return desc;
286}
287
288int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
289 int index, enum of_gpio_flags *flags)
290{
291 struct gpio_desc *desc;
292
293 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
294
295 if (IS_ERR(desc))
296 return PTR_ERR(desc);
297 else
298 return desc_to_gpio(desc);
299}
David Brazdil0f672f62019-12-10 10:32:29 +0000300EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
301
302/**
303 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
304 * @node: handle of the OF node
305 * @propname: name of the DT property representing the GPIO
306 * @index: index of the GPIO to obtain for the consumer
307 * @dflags: GPIO initialization flags
308 * @label: label to attach to the requested GPIO
309 *
310 * Returns:
311 * On successful request the GPIO pin is configured in accordance with
312 * provided @dflags.
313 *
314 * In case of error an ERR_PTR() is returned.
315 */
316struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
317 const char *propname, int index,
318 enum gpiod_flags dflags,
319 const char *label)
320{
321 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
322 struct gpio_desc *desc;
323 enum of_gpio_flags flags;
324 bool active_low = false;
325 bool single_ended = false;
326 bool open_drain = false;
327 bool transitory = false;
328 int ret;
329
330 desc = of_get_named_gpiod_flags(node, propname,
331 index, &flags);
332
333 if (!desc || IS_ERR(desc)) {
334 return desc;
335 }
336
337 active_low = flags & OF_GPIO_ACTIVE_LOW;
338 single_ended = flags & OF_GPIO_SINGLE_ENDED;
339 open_drain = flags & OF_GPIO_OPEN_DRAIN;
340 transitory = flags & OF_GPIO_TRANSITORY;
341
342 ret = gpiod_request(desc, label);
343 if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
344 return desc;
345 if (ret)
346 return ERR_PTR(ret);
347
348 if (active_low)
349 lflags |= GPIO_ACTIVE_LOW;
350
351 if (single_ended) {
352 if (open_drain)
353 lflags |= GPIO_OPEN_DRAIN;
354 else
355 lflags |= GPIO_OPEN_SOURCE;
356 }
357
358 if (transitory)
359 lflags |= GPIO_TRANSITORY;
360
361 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
362 if (ret < 0) {
363 gpiod_put(desc);
364 return ERR_PTR(ret);
365 }
366
367 return desc;
368}
369EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000370
371/*
372 * The SPI GPIO bindings happened before we managed to establish that GPIO
373 * properties should be named "foo-gpios" so we have this special kludge for
374 * them.
375 */
376static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
377 enum of_gpio_flags *of_flags)
378{
379 char prop_name[32]; /* 32 is max size of property name */
380 struct device_node *np = dev->of_node;
381 struct gpio_desc *desc;
382
383 /*
384 * Hopefully the compiler stubs the rest of the function if this
385 * is false.
386 */
387 if (!IS_ENABLED(CONFIG_SPI_MASTER))
388 return ERR_PTR(-ENOENT);
389
390 /* Allow this specifically for "spi-gpio" devices */
391 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
392 return ERR_PTR(-ENOENT);
393
394 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
395 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
396
397 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
398 return desc;
399}
400
401/*
David Brazdil0f672f62019-12-10 10:32:29 +0000402 * The old Freescale bindings use simply "gpios" as name for the chip select
403 * lines rather than "cs-gpios" like all other SPI hardware. Account for this
404 * with a special quirk.
405 */
406static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
407 const char *con_id,
408 unsigned int idx,
409 unsigned long *flags)
410{
411 struct device_node *np = dev->of_node;
412
413 if (!IS_ENABLED(CONFIG_SPI_MASTER))
414 return ERR_PTR(-ENOENT);
415
416 /* Allow this specifically for Freescale devices */
417 if (!of_device_is_compatible(np, "fsl,spi") &&
418 !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
419 return ERR_PTR(-ENOENT);
420 /* Allow only if asking for "cs-gpios" */
421 if (!con_id || strcmp(con_id, "cs"))
422 return ERR_PTR(-ENOENT);
423
424 /*
425 * While all other SPI controllers use "cs-gpios" the Freescale
426 * uses just "gpios" so translate to that when "cs-gpios" is
427 * requested.
428 */
429 return of_find_gpio(dev, NULL, idx, flags);
430}
431
432/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000433 * Some regulator bindings happened before we managed to establish that GPIO
434 * properties should be named "foo-gpios" so we have this special kludge for
435 * them.
436 */
437static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
438 enum of_gpio_flags *of_flags)
439{
440 /* These are the connection IDs we accept as legacy GPIO phandles */
441 const char *whitelist[] = {
442 "wlf,ldoena", /* Arizona */
443 "wlf,ldo1ena", /* WM8994 */
444 "wlf,ldo2ena", /* WM8994 */
445 };
446 struct device_node *np = dev->of_node;
447 struct gpio_desc *desc;
448 int i;
449
450 if (!IS_ENABLED(CONFIG_REGULATOR))
451 return ERR_PTR(-ENOENT);
452
453 if (!con_id)
454 return ERR_PTR(-ENOENT);
455
456 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
457 if (i < 0)
458 return ERR_PTR(-ENOENT);
459
460 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
461 return desc;
462}
463
David Brazdil0f672f62019-12-10 10:32:29 +0000464static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
465 const char *con_id,
466 enum of_gpio_flags *of_flags)
467{
468 if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
469 return ERR_PTR(-ENOENT);
470
471 if (!con_id || strcmp(con_id, "wlf,reset"))
472 return ERR_PTR(-ENOENT);
473
474 return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
475}
476
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
David Brazdil0f672f62019-12-10 10:32:29 +0000478 unsigned int idx, unsigned long *flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479{
480 char prop_name[32]; /* 32 is max size of property name */
481 enum of_gpio_flags of_flags;
482 struct gpio_desc *desc;
483 unsigned int i;
484
485 /* Try GPIO property "foo-gpios" and "foo-gpio" */
486 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
487 if (con_id)
488 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
489 gpio_suffixes[i]);
490 else
491 snprintf(prop_name, sizeof(prop_name), "%s",
492 gpio_suffixes[i]);
493
494 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
495 &of_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000496
David Brazdil0f672f62019-12-10 10:32:29 +0000497 if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000498 break;
499 }
500
David Brazdil0f672f62019-12-10 10:32:29 +0000501 if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
502 /* Special handling for SPI GPIOs if used */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503 desc = of_find_spi_gpio(dev, con_id, &of_flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000504 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505
David Brazdil0f672f62019-12-10 10:32:29 +0000506 if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
507 /* This quirk looks up flags and all */
508 desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
509 if (!IS_ERR(desc))
510 return desc;
511 }
512
513 if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
514 /* Special handling for regulator GPIOs if used */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000515 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000516 }
517
518 if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
519 desc = of_find_arizona_gpio(dev, con_id, &of_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520
521 if (IS_ERR(desc))
522 return desc;
523
524 if (of_flags & OF_GPIO_ACTIVE_LOW)
525 *flags |= GPIO_ACTIVE_LOW;
526
527 if (of_flags & OF_GPIO_SINGLE_ENDED) {
528 if (of_flags & OF_GPIO_OPEN_DRAIN)
529 *flags |= GPIO_OPEN_DRAIN;
530 else
531 *flags |= GPIO_OPEN_SOURCE;
532 }
533
534 if (of_flags & OF_GPIO_TRANSITORY)
535 *flags |= GPIO_TRANSITORY;
536
David Brazdil0f672f62019-12-10 10:32:29 +0000537 if (of_flags & OF_GPIO_PULL_UP)
538 *flags |= GPIO_PULL_UP;
539 if (of_flags & OF_GPIO_PULL_DOWN)
540 *flags |= GPIO_PULL_DOWN;
541
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000542 return desc;
543}
544
545/**
546 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
547 * @np: device node to get GPIO from
548 * @chip: GPIO chip whose hog is parsed
549 * @idx: Index of the GPIO to parse
550 * @name: GPIO line name
David Brazdil0f672f62019-12-10 10:32:29 +0000551 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
552 * of_find_gpio() or of_parse_own_gpio()
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000553 * @dflags: gpiod_flags - optional GPIO initialization flags
554 *
555 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
556 * value on the error condition.
557 */
558static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
559 struct gpio_chip *chip,
560 unsigned int idx, const char **name,
David Brazdil0f672f62019-12-10 10:32:29 +0000561 unsigned long *lflags,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562 enum gpiod_flags *dflags)
563{
564 struct device_node *chip_np;
565 enum of_gpio_flags xlate_flags;
566 struct of_phandle_args gpiospec;
567 struct gpio_desc *desc;
568 unsigned int i;
569 u32 tmp;
570 int ret;
571
572 chip_np = chip->of_node;
573 if (!chip_np)
574 return ERR_PTR(-EINVAL);
575
576 xlate_flags = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000577 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000578 *dflags = 0;
579
580 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
581 if (ret)
582 return ERR_PTR(ret);
583
584 gpiospec.np = chip_np;
585 gpiospec.args_count = tmp;
586
587 for (i = 0; i < tmp; i++) {
588 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
589 &gpiospec.args[i]);
590 if (ret)
591 return ERR_PTR(ret);
592 }
593
594 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
595 if (IS_ERR(desc))
596 return desc;
597
598 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
599 *lflags |= GPIO_ACTIVE_LOW;
600 if (xlate_flags & OF_GPIO_TRANSITORY)
601 *lflags |= GPIO_TRANSITORY;
602
603 if (of_property_read_bool(np, "input"))
604 *dflags |= GPIOD_IN;
605 else if (of_property_read_bool(np, "output-low"))
606 *dflags |= GPIOD_OUT_LOW;
607 else if (of_property_read_bool(np, "output-high"))
608 *dflags |= GPIOD_OUT_HIGH;
609 else {
David Brazdil0f672f62019-12-10 10:32:29 +0000610 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
611 desc_to_gpio(desc), np);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000612 return ERR_PTR(-EINVAL);
613 }
614
615 if (name && of_property_read_string(np, "line-name", name))
616 *name = np->name;
617
618 return desc;
619}
620
621/**
622 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
623 * @chip: gpio chip to act on
624 *
625 * This is only used by of_gpiochip_add to request/set GPIO initial
626 * configuration.
627 * It returns error if it fails otherwise 0 on success.
628 */
629static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
630{
631 struct gpio_desc *desc = NULL;
632 struct device_node *np;
633 const char *name;
David Brazdil0f672f62019-12-10 10:32:29 +0000634 unsigned long lflags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635 enum gpiod_flags dflags;
636 unsigned int i;
637 int ret;
638
639 for_each_available_child_of_node(chip->of_node, np) {
640 if (!of_property_read_bool(np, "gpio-hog"))
641 continue;
642
643 for (i = 0;; i++) {
644 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
645 &dflags);
646 if (IS_ERR(desc))
647 break;
648
649 ret = gpiod_hog(desc, name, lflags, dflags);
650 if (ret < 0) {
651 of_node_put(np);
652 return ret;
653 }
654 }
655 }
656
657 return 0;
658}
659
660/**
661 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
662 * @gc: pointer to the gpio_chip structure
663 * @gpiospec: GPIO specifier as found in the device tree
664 * @flags: a flags pointer to fill in
665 *
666 * This is simple translation function, suitable for the most 1:1 mapped
667 * GPIO chips. This function performs only one sanity check: whether GPIO
668 * is less than ngpios (that is specified in the gpio_chip).
669 */
David Brazdil0f672f62019-12-10 10:32:29 +0000670static int of_gpio_simple_xlate(struct gpio_chip *gc,
671 const struct of_phandle_args *gpiospec,
672 u32 *flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000673{
674 /*
675 * We're discouraging gpio_cells < 2, since that way you'll have to
676 * write your own xlate function (that will have to retrieve the GPIO
677 * number and the flags from a single gpio cell -- this is possible,
678 * but not recommended).
679 */
680 if (gc->of_gpio_n_cells < 2) {
681 WARN_ON(1);
682 return -EINVAL;
683 }
684
685 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
686 return -EINVAL;
687
688 if (gpiospec->args[0] >= gc->ngpio)
689 return -EINVAL;
690
691 if (flags)
692 *flags = gpiospec->args[1];
693
694 return gpiospec->args[0];
695}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000696
697/**
698 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
699 * @np: device node of the GPIO chip
700 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
701 * @data: driver data to store in the struct gpio_chip
702 *
703 * To use this function you should allocate and fill mm_gc with:
704 *
705 * 1) In the gpio_chip structure:
706 * - all the callbacks
707 * - of_gpio_n_cells
708 * - of_xlate callback (optional)
709 *
710 * 3) In the of_mm_gpio_chip structure:
711 * - save_regs callback (optional)
712 *
713 * If succeeded, this function will map bank's memory and will
714 * do all necessary work for you. Then you'll able to use .regs
715 * to manage GPIOs from the callbacks.
716 */
717int of_mm_gpiochip_add_data(struct device_node *np,
718 struct of_mm_gpio_chip *mm_gc,
719 void *data)
720{
721 int ret = -ENOMEM;
722 struct gpio_chip *gc = &mm_gc->gc;
723
724 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
725 if (!gc->label)
726 goto err0;
727
728 mm_gc->regs = of_iomap(np, 0);
729 if (!mm_gc->regs)
730 goto err1;
731
732 gc->base = -1;
733
734 if (mm_gc->save_regs)
735 mm_gc->save_regs(mm_gc);
736
737 mm_gc->gc.of_node = np;
738
739 ret = gpiochip_add_data(gc, data);
740 if (ret)
741 goto err2;
742
743 return 0;
744err2:
745 iounmap(mm_gc->regs);
746err1:
747 kfree(gc->label);
748err0:
749 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
750 return ret;
751}
David Brazdil0f672f62019-12-10 10:32:29 +0000752EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000753
754/**
755 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
756 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
757 */
758void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
759{
760 struct gpio_chip *gc = &mm_gc->gc;
761
762 if (!mm_gc)
763 return;
764
765 gpiochip_remove(gc);
766 iounmap(mm_gc->regs);
767 kfree(gc->label);
768}
David Brazdil0f672f62019-12-10 10:32:29 +0000769EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770
771static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
772{
773 int len, i;
774 u32 start, count;
775 struct device_node *np = chip->of_node;
776
777 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
778 if (len < 0 || len % 2 != 0)
779 return;
780
781 for (i = 0; i < len; i += 2) {
782 of_property_read_u32_index(np, "gpio-reserved-ranges",
783 i, &start);
784 of_property_read_u32_index(np, "gpio-reserved-ranges",
785 i + 1, &count);
786 if (start >= chip->ngpio || start + count >= chip->ngpio)
787 continue;
788
789 bitmap_clear(chip->valid_mask, start, count);
790 }
791};
792
793#ifdef CONFIG_PINCTRL
794static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
795{
796 struct device_node *np = chip->of_node;
797 struct of_phandle_args pinspec;
798 struct pinctrl_dev *pctldev;
799 int index = 0, ret;
800 const char *name;
801 static const char group_names_propname[] = "gpio-ranges-group-names";
802 struct property *group_names;
803
804 if (!np)
805 return 0;
806
807 group_names = of_find_property(np, group_names_propname, NULL);
808
809 for (;; index++) {
810 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
811 index, &pinspec);
812 if (ret)
813 break;
814
815 pctldev = of_pinctrl_get(pinspec.np);
816 of_node_put(pinspec.np);
817 if (!pctldev)
818 return -EPROBE_DEFER;
819
820 if (pinspec.args[2]) {
821 if (group_names) {
822 of_property_read_string_index(np,
823 group_names_propname,
824 index, &name);
825 if (strlen(name)) {
826 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
827 np);
828 break;
829 }
830 }
831 /* npins != 0: linear range */
832 ret = gpiochip_add_pin_range(chip,
833 pinctrl_dev_get_devname(pctldev),
834 pinspec.args[0],
835 pinspec.args[1],
836 pinspec.args[2]);
837 if (ret)
838 return ret;
839 } else {
840 /* npins == 0: special range */
841 if (pinspec.args[1]) {
842 pr_err("%pOF: Illegal gpio-range format.\n",
843 np);
844 break;
845 }
846
847 if (!group_names) {
848 pr_err("%pOF: GPIO group range requested but no %s property.\n",
849 np, group_names_propname);
850 break;
851 }
852
853 ret = of_property_read_string_index(np,
854 group_names_propname,
855 index, &name);
856 if (ret)
857 break;
858
859 if (!strlen(name)) {
860 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
861 np);
862 break;
863 }
864
865 ret = gpiochip_add_pingroup_range(chip, pctldev,
866 pinspec.args[0], name);
867 if (ret)
868 return ret;
869 }
870 }
871
872 return 0;
873}
874
875#else
876static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
877#endif
878
879int of_gpiochip_add(struct gpio_chip *chip)
880{
David Brazdil0f672f62019-12-10 10:32:29 +0000881 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000882
883 if (!chip->of_node)
884 return 0;
885
886 if (!chip->of_xlate) {
887 chip->of_gpio_n_cells = 2;
888 chip->of_xlate = of_gpio_simple_xlate;
889 }
890
891 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
892 return -EINVAL;
893
894 of_gpiochip_init_valid_mask(chip);
895
David Brazdil0f672f62019-12-10 10:32:29 +0000896 ret = of_gpiochip_add_pin_range(chip);
897 if (ret)
898 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899
900 /* If the chip defines names itself, these take precedence */
901 if (!chip->names)
902 devprop_gpiochip_set_names(chip,
903 of_fwnode_handle(chip->of_node));
904
905 of_node_get(chip->of_node);
906
David Brazdil0f672f62019-12-10 10:32:29 +0000907 ret = of_gpiochip_scan_gpios(chip);
Olivier Deprez0e641232021-09-23 10:07:05 +0200908 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000909 of_node_put(chip->of_node);
David Brazdil0f672f62019-12-10 10:32:29 +0000910
911 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000912}
913
914void of_gpiochip_remove(struct gpio_chip *chip)
915{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000916 of_node_put(chip->of_node);
917}