Merge 5.0-rc6 into driver-core-next
[linux/fpc-iii.git] / drivers / gpio / gpiolib-of.c
bloba6e1891217e26880cb94bd2ec6b08289733f8c42
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * OF helpers for the GPIO API
5 * Copyright (c) 2007-2008 MontaVista Software, Inc.
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8 */
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>
23 #include "gpiolib.h"
25 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
27 struct of_phandle_args *gpiospec = data;
29 return chip->gpiodev->dev.of_node == gpiospec->np &&
30 chip->of_xlate &&
31 chip->of_xlate(chip, gpiospec, NULL) >= 0;
34 static struct gpio_chip *of_find_gpiochip_by_xlate(
35 struct of_phandle_args *gpiospec)
37 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
40 static 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)
44 int ret;
46 if (chip->of_gpio_n_cells != gpiospec->args_count)
47 return ERR_PTR(-EINVAL);
49 ret = chip->of_xlate(chip, gpiospec, flags);
50 if (ret < 0)
51 return ERR_PTR(ret);
53 return gpiochip_get_desc(chip, ret);
56 static void of_gpio_flags_quirks(struct device_node *np,
57 const char *propname,
58 enum of_gpio_flags *flags,
59 int index)
62 * Handle MMC "cd-inverted" and "wp-inverted" semantics.
64 if (IS_ENABLED(CONFIG_MMC)) {
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.
73 if (!strcmp(propname, "cd-gpios")) {
74 if (of_property_read_bool(np, "cd-inverted"))
75 *flags ^= OF_GPIO_ACTIVE_LOW;
77 if (!strcmp(propname, "wp-gpios")) {
78 if (of_property_read_bool(np, "wp-inverted"))
79 *flags ^= OF_GPIO_ACTIVE_LOW;
83 * Some GPIO fixed regulator quirks.
84 * Note that active low is the default.
86 if (IS_ENABLED(CONFIG_REGULATOR) &&
87 (of_device_is_compatible(np, "regulator-fixed") ||
88 of_device_is_compatible(np, "reg-fixed-voltage") ||
89 of_device_is_compatible(np, "regulator-gpio"))) {
91 * The regulator GPIO handles are specified such that the
92 * presence or absence of "enable-active-high" solely controls
93 * the polarity of the GPIO line. Any phandle flags must
94 * be actively ignored.
96 if (*flags & OF_GPIO_ACTIVE_LOW) {
97 pr_warn("%s GPIO handle specifies active low - ignored\n",
98 of_node_full_name(np));
99 *flags &= ~OF_GPIO_ACTIVE_LOW;
101 if (!of_property_read_bool(np, "enable-active-high"))
102 *flags |= OF_GPIO_ACTIVE_LOW;
105 * Legacy open drain handling for fixed voltage regulators.
107 if (IS_ENABLED(CONFIG_REGULATOR) &&
108 of_device_is_compatible(np, "reg-fixed-voltage") &&
109 of_property_read_bool(np, "gpio-open-drain")) {
110 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
111 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
112 of_node_full_name(np));
116 * Legacy handling of SPI active high chip select. If we have a
117 * property named "cs-gpios" we need to inspect the child node
118 * to determine if the flags should have inverted semantics.
120 if (IS_ENABLED(CONFIG_SPI_MASTER) &&
121 of_property_read_bool(np, "cs-gpios")) {
122 struct device_node *child;
123 u32 cs;
124 int ret;
126 for_each_child_of_node(np, child) {
127 ret = of_property_read_u32(child, "reg", &cs);
128 if (!ret)
129 continue;
130 if (cs == index) {
132 * SPI children have active low chip selects
133 * by default. This can be specified negatively
134 * by just omitting "spi-cs-high" in the
135 * device node, or actively by tagging on
136 * GPIO_ACTIVE_LOW as flag in the device
137 * tree. If the line is simultaneously
138 * tagged as active low in the device tree
139 * and has the "spi-cs-high" set, we get a
140 * conflict and the "spi-cs-high" flag will
141 * take precedence.
143 if (of_property_read_bool(np, "spi-cs-high")) {
144 if (*flags & OF_GPIO_ACTIVE_LOW) {
145 pr_warn("%s GPIO handle specifies active low - ignored\n",
146 of_node_full_name(np));
147 *flags &= ~OF_GPIO_ACTIVE_LOW;
149 } else {
150 if (!(*flags & OF_GPIO_ACTIVE_LOW))
151 pr_info("%s enforce active low on chipselect handle\n",
152 of_node_full_name(np));
153 *flags |= OF_GPIO_ACTIVE_LOW;
155 break;
162 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
163 * @np: device node to get GPIO from
164 * @propname: property name containing gpio specifier(s)
165 * @index: index of the GPIO
166 * @flags: a flags pointer to fill in
168 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
169 * value on the error condition. If @flags is not NULL the function also fills
170 * in flags for the GPIO.
172 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
173 const char *propname, int index, enum of_gpio_flags *flags)
175 struct of_phandle_args gpiospec;
176 struct gpio_chip *chip;
177 struct gpio_desc *desc;
178 int ret;
180 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
181 &gpiospec);
182 if (ret) {
183 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
184 __func__, propname, np, index);
185 return ERR_PTR(ret);
188 chip = of_find_gpiochip_by_xlate(&gpiospec);
189 if (!chip) {
190 desc = ERR_PTR(-EPROBE_DEFER);
191 goto out;
194 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
195 if (IS_ERR(desc))
196 goto out;
198 if (flags)
199 of_gpio_flags_quirks(np, propname, flags, index);
201 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
202 __func__, propname, np, index,
203 PTR_ERR_OR_ZERO(desc));
205 out:
206 of_node_put(gpiospec.np);
208 return desc;
211 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
212 int index, enum of_gpio_flags *flags)
214 struct gpio_desc *desc;
216 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
218 if (IS_ERR(desc))
219 return PTR_ERR(desc);
220 else
221 return desc_to_gpio(desc);
223 EXPORT_SYMBOL(of_get_named_gpio_flags);
226 * The SPI GPIO bindings happened before we managed to establish that GPIO
227 * properties should be named "foo-gpios" so we have this special kludge for
228 * them.
230 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
231 enum of_gpio_flags *of_flags)
233 char prop_name[32]; /* 32 is max size of property name */
234 struct device_node *np = dev->of_node;
235 struct gpio_desc *desc;
238 * Hopefully the compiler stubs the rest of the function if this
239 * is false.
241 if (!IS_ENABLED(CONFIG_SPI_MASTER))
242 return ERR_PTR(-ENOENT);
244 /* Allow this specifically for "spi-gpio" devices */
245 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
246 return ERR_PTR(-ENOENT);
248 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
249 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
251 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
252 return desc;
256 * Some regulator bindings happened before we managed to establish that GPIO
257 * properties should be named "foo-gpios" so we have this special kludge for
258 * them.
260 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
261 enum of_gpio_flags *of_flags)
263 /* These are the connection IDs we accept as legacy GPIO phandles */
264 const char *whitelist[] = {
265 "wlf,ldoena", /* Arizona */
266 "wlf,ldo1ena", /* WM8994 */
267 "wlf,ldo2ena", /* WM8994 */
269 struct device_node *np = dev->of_node;
270 struct gpio_desc *desc;
271 int i;
273 if (!IS_ENABLED(CONFIG_REGULATOR))
274 return ERR_PTR(-ENOENT);
276 if (!con_id)
277 return ERR_PTR(-ENOENT);
279 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
280 if (i < 0)
281 return ERR_PTR(-ENOENT);
283 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
284 return desc;
287 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
288 unsigned int idx,
289 enum gpio_lookup_flags *flags)
291 char prop_name[32]; /* 32 is max size of property name */
292 enum of_gpio_flags of_flags;
293 struct gpio_desc *desc;
294 unsigned int i;
296 /* Try GPIO property "foo-gpios" and "foo-gpio" */
297 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
298 if (con_id)
299 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
300 gpio_suffixes[i]);
301 else
302 snprintf(prop_name, sizeof(prop_name), "%s",
303 gpio_suffixes[i]);
305 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
306 &of_flags);
308 * -EPROBE_DEFER in our case means that we found a
309 * valid GPIO property, but no controller has been
310 * registered so far.
312 * This means we don't need to look any further for
313 * alternate name conventions, and we should really
314 * preserve the return code for our user to be able to
315 * retry probing later.
317 if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
318 return desc;
320 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
321 break;
324 /* Special handling for SPI GPIOs if used */
325 if (IS_ERR(desc))
326 desc = of_find_spi_gpio(dev, con_id, &of_flags);
328 /* Special handling for regulator GPIOs if used */
329 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
330 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
332 if (IS_ERR(desc))
333 return desc;
335 if (of_flags & OF_GPIO_ACTIVE_LOW)
336 *flags |= GPIO_ACTIVE_LOW;
338 if (of_flags & OF_GPIO_SINGLE_ENDED) {
339 if (of_flags & OF_GPIO_OPEN_DRAIN)
340 *flags |= GPIO_OPEN_DRAIN;
341 else
342 *flags |= GPIO_OPEN_SOURCE;
345 if (of_flags & OF_GPIO_TRANSITORY)
346 *flags |= GPIO_TRANSITORY;
348 return desc;
352 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
353 * @np: device node to get GPIO from
354 * @chip: GPIO chip whose hog is parsed
355 * @idx: Index of the GPIO to parse
356 * @name: GPIO line name
357 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
358 * of_parse_own_gpio()
359 * @dflags: gpiod_flags - optional GPIO initialization flags
361 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
362 * value on the error condition.
364 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
365 struct gpio_chip *chip,
366 unsigned int idx, const char **name,
367 enum gpio_lookup_flags *lflags,
368 enum gpiod_flags *dflags)
370 struct device_node *chip_np;
371 enum of_gpio_flags xlate_flags;
372 struct of_phandle_args gpiospec;
373 struct gpio_desc *desc;
374 unsigned int i;
375 u32 tmp;
376 int ret;
378 chip_np = chip->of_node;
379 if (!chip_np)
380 return ERR_PTR(-EINVAL);
382 xlate_flags = 0;
383 *lflags = 0;
384 *dflags = 0;
386 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
387 if (ret)
388 return ERR_PTR(ret);
390 gpiospec.np = chip_np;
391 gpiospec.args_count = tmp;
393 for (i = 0; i < tmp; i++) {
394 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
395 &gpiospec.args[i]);
396 if (ret)
397 return ERR_PTR(ret);
400 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
401 if (IS_ERR(desc))
402 return desc;
404 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
405 *lflags |= GPIO_ACTIVE_LOW;
406 if (xlate_flags & OF_GPIO_TRANSITORY)
407 *lflags |= GPIO_TRANSITORY;
409 if (of_property_read_bool(np, "input"))
410 *dflags |= GPIOD_IN;
411 else if (of_property_read_bool(np, "output-low"))
412 *dflags |= GPIOD_OUT_LOW;
413 else if (of_property_read_bool(np, "output-high"))
414 *dflags |= GPIOD_OUT_HIGH;
415 else {
416 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
417 desc_to_gpio(desc), np);
418 return ERR_PTR(-EINVAL);
421 if (name && of_property_read_string(np, "line-name", name))
422 *name = np->name;
424 return desc;
428 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
429 * @chip: gpio chip to act on
431 * This is only used by of_gpiochip_add to request/set GPIO initial
432 * configuration.
433 * It returns error if it fails otherwise 0 on success.
435 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
437 struct gpio_desc *desc = NULL;
438 struct device_node *np;
439 const char *name;
440 enum gpio_lookup_flags lflags;
441 enum gpiod_flags dflags;
442 unsigned int i;
443 int ret;
445 for_each_available_child_of_node(chip->of_node, np) {
446 if (!of_property_read_bool(np, "gpio-hog"))
447 continue;
449 for (i = 0;; i++) {
450 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
451 &dflags);
452 if (IS_ERR(desc))
453 break;
455 ret = gpiod_hog(desc, name, lflags, dflags);
456 if (ret < 0) {
457 of_node_put(np);
458 return ret;
463 return 0;
467 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
468 * @gc: pointer to the gpio_chip structure
469 * @gpiospec: GPIO specifier as found in the device tree
470 * @flags: a flags pointer to fill in
472 * This is simple translation function, suitable for the most 1:1 mapped
473 * GPIO chips. This function performs only one sanity check: whether GPIO
474 * is less than ngpios (that is specified in the gpio_chip).
476 int of_gpio_simple_xlate(struct gpio_chip *gc,
477 const struct of_phandle_args *gpiospec, u32 *flags)
480 * We're discouraging gpio_cells < 2, since that way you'll have to
481 * write your own xlate function (that will have to retrieve the GPIO
482 * number and the flags from a single gpio cell -- this is possible,
483 * but not recommended).
485 if (gc->of_gpio_n_cells < 2) {
486 WARN_ON(1);
487 return -EINVAL;
490 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
491 return -EINVAL;
493 if (gpiospec->args[0] >= gc->ngpio)
494 return -EINVAL;
496 if (flags)
497 *flags = gpiospec->args[1];
499 return gpiospec->args[0];
501 EXPORT_SYMBOL(of_gpio_simple_xlate);
504 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
505 * @np: device node of the GPIO chip
506 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
507 * @data: driver data to store in the struct gpio_chip
509 * To use this function you should allocate and fill mm_gc with:
511 * 1) In the gpio_chip structure:
512 * - all the callbacks
513 * - of_gpio_n_cells
514 * - of_xlate callback (optional)
516 * 3) In the of_mm_gpio_chip structure:
517 * - save_regs callback (optional)
519 * If succeeded, this function will map bank's memory and will
520 * do all necessary work for you. Then you'll able to use .regs
521 * to manage GPIOs from the callbacks.
523 int of_mm_gpiochip_add_data(struct device_node *np,
524 struct of_mm_gpio_chip *mm_gc,
525 void *data)
527 int ret = -ENOMEM;
528 struct gpio_chip *gc = &mm_gc->gc;
530 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
531 if (!gc->label)
532 goto err0;
534 mm_gc->regs = of_iomap(np, 0);
535 if (!mm_gc->regs)
536 goto err1;
538 gc->base = -1;
540 if (mm_gc->save_regs)
541 mm_gc->save_regs(mm_gc);
543 mm_gc->gc.of_node = np;
545 ret = gpiochip_add_data(gc, data);
546 if (ret)
547 goto err2;
549 return 0;
550 err2:
551 iounmap(mm_gc->regs);
552 err1:
553 kfree(gc->label);
554 err0:
555 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
556 return ret;
558 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
561 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
562 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
564 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
566 struct gpio_chip *gc = &mm_gc->gc;
568 if (!mm_gc)
569 return;
571 gpiochip_remove(gc);
572 iounmap(mm_gc->regs);
573 kfree(gc->label);
575 EXPORT_SYMBOL(of_mm_gpiochip_remove);
577 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
579 int len, i;
580 u32 start, count;
581 struct device_node *np = chip->of_node;
583 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
584 if (len < 0 || len % 2 != 0)
585 return;
587 for (i = 0; i < len; i += 2) {
588 of_property_read_u32_index(np, "gpio-reserved-ranges",
589 i, &start);
590 of_property_read_u32_index(np, "gpio-reserved-ranges",
591 i + 1, &count);
592 if (start >= chip->ngpio || start + count >= chip->ngpio)
593 continue;
595 bitmap_clear(chip->valid_mask, start, count);
599 #ifdef CONFIG_PINCTRL
600 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
602 struct device_node *np = chip->of_node;
603 struct of_phandle_args pinspec;
604 struct pinctrl_dev *pctldev;
605 int index = 0, ret;
606 const char *name;
607 static const char group_names_propname[] = "gpio-ranges-group-names";
608 struct property *group_names;
610 if (!np)
611 return 0;
613 group_names = of_find_property(np, group_names_propname, NULL);
615 for (;; index++) {
616 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
617 index, &pinspec);
618 if (ret)
619 break;
621 pctldev = of_pinctrl_get(pinspec.np);
622 of_node_put(pinspec.np);
623 if (!pctldev)
624 return -EPROBE_DEFER;
626 if (pinspec.args[2]) {
627 if (group_names) {
628 of_property_read_string_index(np,
629 group_names_propname,
630 index, &name);
631 if (strlen(name)) {
632 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
633 np);
634 break;
637 /* npins != 0: linear range */
638 ret = gpiochip_add_pin_range(chip,
639 pinctrl_dev_get_devname(pctldev),
640 pinspec.args[0],
641 pinspec.args[1],
642 pinspec.args[2]);
643 if (ret)
644 return ret;
645 } else {
646 /* npins == 0: special range */
647 if (pinspec.args[1]) {
648 pr_err("%pOF: Illegal gpio-range format.\n",
649 np);
650 break;
653 if (!group_names) {
654 pr_err("%pOF: GPIO group range requested but no %s property.\n",
655 np, group_names_propname);
656 break;
659 ret = of_property_read_string_index(np,
660 group_names_propname,
661 index, &name);
662 if (ret)
663 break;
665 if (!strlen(name)) {
666 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
667 np);
668 break;
671 ret = gpiochip_add_pingroup_range(chip, pctldev,
672 pinspec.args[0], name);
673 if (ret)
674 return ret;
678 return 0;
681 #else
682 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
683 #endif
685 int of_gpiochip_add(struct gpio_chip *chip)
687 int status;
689 if (!chip->of_node)
690 return 0;
692 if (!chip->of_xlate) {
693 chip->of_gpio_n_cells = 2;
694 chip->of_xlate = of_gpio_simple_xlate;
697 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
698 return -EINVAL;
700 of_gpiochip_init_valid_mask(chip);
702 status = of_gpiochip_add_pin_range(chip);
703 if (status)
704 return status;
706 /* If the chip defines names itself, these take precedence */
707 if (!chip->names)
708 devprop_gpiochip_set_names(chip,
709 of_fwnode_handle(chip->of_node));
711 of_node_get(chip->of_node);
713 return of_gpiochip_scan_gpios(chip);
716 void of_gpiochip_remove(struct gpio_chip *chip)
718 gpiochip_remove_pin_ranges(chip);
719 of_node_put(chip->of_node);