perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / gpio / gpiolib-of.c
blob7f1260c78270b57ebd60daee22bbbec939125234
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 enum of_gpio_flags *flags,
58 int index)
61 * Some GPIO fixed regulator quirks.
62 * Note that active low is the default.
64 if (IS_ENABLED(CONFIG_REGULATOR) &&
65 (of_device_is_compatible(np, "regulator-fixed") ||
66 of_device_is_compatible(np, "reg-fixed-voltage") ||
67 of_device_is_compatible(np, "regulator-gpio"))) {
69 * The regulator GPIO handles are specified such that the
70 * presence or absence of "enable-active-high" solely controls
71 * the polarity of the GPIO line. Any phandle flags must
72 * be actively ignored.
74 if (*flags & OF_GPIO_ACTIVE_LOW) {
75 pr_warn("%s GPIO handle specifies active low - ignored\n",
76 of_node_full_name(np));
77 *flags &= ~OF_GPIO_ACTIVE_LOW;
79 if (!of_property_read_bool(np, "enable-active-high"))
80 *flags |= OF_GPIO_ACTIVE_LOW;
83 * Legacy open drain handling for fixed voltage regulators.
85 if (IS_ENABLED(CONFIG_REGULATOR) &&
86 of_device_is_compatible(np, "reg-fixed-voltage") &&
87 of_property_read_bool(np, "gpio-open-drain")) {
88 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
89 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
90 of_node_full_name(np));
94 * Legacy handling of SPI active high chip select. If we have a
95 * property named "cs-gpios" we need to inspect the child node
96 * to determine if the flags should have inverted semantics.
98 if (IS_ENABLED(CONFIG_SPI_MASTER) &&
99 of_property_read_bool(np, "cs-gpios")) {
100 struct device_node *child;
101 u32 cs;
102 int ret;
104 for_each_child_of_node(np, child) {
105 ret = of_property_read_u32(child, "reg", &cs);
106 if (!ret)
107 continue;
108 if (cs == index) {
110 * SPI children have active low chip selects
111 * by default. This can be specified negatively
112 * by just omitting "spi-cs-high" in the
113 * device node, or actively by tagging on
114 * GPIO_ACTIVE_LOW as flag in the device
115 * tree. If the line is simultaneously
116 * tagged as active low in the device tree
117 * and has the "spi-cs-high" set, we get a
118 * conflict and the "spi-cs-high" flag will
119 * take precedence.
121 if (of_property_read_bool(np, "spi-cs-high")) {
122 if (*flags & OF_GPIO_ACTIVE_LOW) {
123 pr_warn("%s GPIO handle specifies active low - ignored\n",
124 of_node_full_name(np));
125 *flags &= ~OF_GPIO_ACTIVE_LOW;
127 } else {
128 if (!(*flags & OF_GPIO_ACTIVE_LOW))
129 pr_info("%s enforce active low on chipselect handle\n",
130 of_node_full_name(np));
131 *flags |= OF_GPIO_ACTIVE_LOW;
133 break;
140 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
141 * @np: device node to get GPIO from
142 * @propname: property name containing gpio specifier(s)
143 * @index: index of the GPIO
144 * @flags: a flags pointer to fill in
146 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
147 * value on the error condition. If @flags is not NULL the function also fills
148 * in flags for the GPIO.
150 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
151 const char *propname, int index, enum of_gpio_flags *flags)
153 struct of_phandle_args gpiospec;
154 struct gpio_chip *chip;
155 struct gpio_desc *desc;
156 int ret;
158 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
159 &gpiospec);
160 if (ret) {
161 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
162 __func__, propname, np, index);
163 return ERR_PTR(ret);
166 chip = of_find_gpiochip_by_xlate(&gpiospec);
167 if (!chip) {
168 desc = ERR_PTR(-EPROBE_DEFER);
169 goto out;
172 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
173 if (IS_ERR(desc))
174 goto out;
176 if (flags)
177 of_gpio_flags_quirks(np, flags, index);
179 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
180 __func__, propname, np, index,
181 PTR_ERR_OR_ZERO(desc));
183 out:
184 of_node_put(gpiospec.np);
186 return desc;
189 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
190 int index, enum of_gpio_flags *flags)
192 struct gpio_desc *desc;
194 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
196 if (IS_ERR(desc))
197 return PTR_ERR(desc);
198 else
199 return desc_to_gpio(desc);
201 EXPORT_SYMBOL(of_get_named_gpio_flags);
204 * The SPI GPIO bindings happened before we managed to establish that GPIO
205 * properties should be named "foo-gpios" so we have this special kludge for
206 * them.
208 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
209 enum of_gpio_flags *of_flags)
211 char prop_name[32]; /* 32 is max size of property name */
212 struct device_node *np = dev->of_node;
213 struct gpio_desc *desc;
216 * Hopefully the compiler stubs the rest of the function if this
217 * is false.
219 if (!IS_ENABLED(CONFIG_SPI_MASTER))
220 return ERR_PTR(-ENOENT);
222 /* Allow this specifically for "spi-gpio" devices */
223 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
224 return ERR_PTR(-ENOENT);
226 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
227 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
229 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
230 return desc;
234 * Some regulator bindings happened before we managed to establish that GPIO
235 * properties should be named "foo-gpios" so we have this special kludge for
236 * them.
238 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
239 enum of_gpio_flags *of_flags)
241 /* These are the connection IDs we accept as legacy GPIO phandles */
242 const char *whitelist[] = {
243 "wlf,ldoena", /* Arizona */
244 "wlf,ldo1ena", /* WM8994 */
245 "wlf,ldo2ena", /* WM8994 */
247 struct device_node *np = dev->of_node;
248 struct gpio_desc *desc;
249 int i;
251 if (!IS_ENABLED(CONFIG_REGULATOR))
252 return ERR_PTR(-ENOENT);
254 if (!con_id)
255 return ERR_PTR(-ENOENT);
257 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
258 if (i < 0)
259 return ERR_PTR(-ENOENT);
261 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
262 return desc;
265 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
266 unsigned int idx,
267 enum gpio_lookup_flags *flags)
269 char prop_name[32]; /* 32 is max size of property name */
270 enum of_gpio_flags of_flags;
271 struct gpio_desc *desc;
272 unsigned int i;
274 /* Try GPIO property "foo-gpios" and "foo-gpio" */
275 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
276 if (con_id)
277 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
278 gpio_suffixes[i]);
279 else
280 snprintf(prop_name, sizeof(prop_name), "%s",
281 gpio_suffixes[i]);
283 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
284 &of_flags);
286 * -EPROBE_DEFER in our case means that we found a
287 * valid GPIO property, but no controller has been
288 * registered so far.
290 * This means we don't need to look any further for
291 * alternate name conventions, and we should really
292 * preserve the return code for our user to be able to
293 * retry probing later.
295 if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
296 return desc;
298 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
299 break;
302 /* Special handling for SPI GPIOs if used */
303 if (IS_ERR(desc))
304 desc = of_find_spi_gpio(dev, con_id, &of_flags);
306 /* Special handling for regulator GPIOs if used */
307 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
308 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
310 if (IS_ERR(desc))
311 return desc;
313 if (of_flags & OF_GPIO_ACTIVE_LOW)
314 *flags |= GPIO_ACTIVE_LOW;
316 if (of_flags & OF_GPIO_SINGLE_ENDED) {
317 if (of_flags & OF_GPIO_OPEN_DRAIN)
318 *flags |= GPIO_OPEN_DRAIN;
319 else
320 *flags |= GPIO_OPEN_SOURCE;
323 if (of_flags & OF_GPIO_TRANSITORY)
324 *flags |= GPIO_TRANSITORY;
326 return desc;
330 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
331 * @np: device node to get GPIO from
332 * @chip: GPIO chip whose hog is parsed
333 * @idx: Index of the GPIO to parse
334 * @name: GPIO line name
335 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
336 * of_parse_own_gpio()
337 * @dflags: gpiod_flags - optional GPIO initialization flags
339 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
340 * value on the error condition.
342 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
343 struct gpio_chip *chip,
344 unsigned int idx, const char **name,
345 enum gpio_lookup_flags *lflags,
346 enum gpiod_flags *dflags)
348 struct device_node *chip_np;
349 enum of_gpio_flags xlate_flags;
350 struct of_phandle_args gpiospec;
351 struct gpio_desc *desc;
352 unsigned int i;
353 u32 tmp;
354 int ret;
356 chip_np = chip->of_node;
357 if (!chip_np)
358 return ERR_PTR(-EINVAL);
360 xlate_flags = 0;
361 *lflags = 0;
362 *dflags = 0;
364 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
365 if (ret)
366 return ERR_PTR(ret);
368 gpiospec.np = chip_np;
369 gpiospec.args_count = tmp;
371 for (i = 0; i < tmp; i++) {
372 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
373 &gpiospec.args[i]);
374 if (ret)
375 return ERR_PTR(ret);
378 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
379 if (IS_ERR(desc))
380 return desc;
382 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
383 *lflags |= GPIO_ACTIVE_LOW;
384 if (xlate_flags & OF_GPIO_TRANSITORY)
385 *lflags |= GPIO_TRANSITORY;
387 if (of_property_read_bool(np, "input"))
388 *dflags |= GPIOD_IN;
389 else if (of_property_read_bool(np, "output-low"))
390 *dflags |= GPIOD_OUT_LOW;
391 else if (of_property_read_bool(np, "output-high"))
392 *dflags |= GPIOD_OUT_HIGH;
393 else {
394 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
395 desc_to_gpio(desc), np);
396 return ERR_PTR(-EINVAL);
399 if (name && of_property_read_string(np, "line-name", name))
400 *name = np->name;
402 return desc;
406 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
407 * @chip: gpio chip to act on
409 * This is only used by of_gpiochip_add to request/set GPIO initial
410 * configuration.
411 * It returns error if it fails otherwise 0 on success.
413 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
415 struct gpio_desc *desc = NULL;
416 struct device_node *np;
417 const char *name;
418 enum gpio_lookup_flags lflags;
419 enum gpiod_flags dflags;
420 unsigned int i;
421 int ret;
423 for_each_available_child_of_node(chip->of_node, np) {
424 if (!of_property_read_bool(np, "gpio-hog"))
425 continue;
427 for (i = 0;; i++) {
428 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
429 &dflags);
430 if (IS_ERR(desc))
431 break;
433 ret = gpiod_hog(desc, name, lflags, dflags);
434 if (ret < 0) {
435 of_node_put(np);
436 return ret;
441 return 0;
445 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
446 * @gc: pointer to the gpio_chip structure
447 * @gpiospec: GPIO specifier as found in the device tree
448 * @flags: a flags pointer to fill in
450 * This is simple translation function, suitable for the most 1:1 mapped
451 * GPIO chips. This function performs only one sanity check: whether GPIO
452 * is less than ngpios (that is specified in the gpio_chip).
454 int of_gpio_simple_xlate(struct gpio_chip *gc,
455 const struct of_phandle_args *gpiospec, u32 *flags)
458 * We're discouraging gpio_cells < 2, since that way you'll have to
459 * write your own xlate function (that will have to retrieve the GPIO
460 * number and the flags from a single gpio cell -- this is possible,
461 * but not recommended).
463 if (gc->of_gpio_n_cells < 2) {
464 WARN_ON(1);
465 return -EINVAL;
468 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
469 return -EINVAL;
471 if (gpiospec->args[0] >= gc->ngpio)
472 return -EINVAL;
474 if (flags)
475 *flags = gpiospec->args[1];
477 return gpiospec->args[0];
479 EXPORT_SYMBOL(of_gpio_simple_xlate);
482 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
483 * @np: device node of the GPIO chip
484 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
485 * @data: driver data to store in the struct gpio_chip
487 * To use this function you should allocate and fill mm_gc with:
489 * 1) In the gpio_chip structure:
490 * - all the callbacks
491 * - of_gpio_n_cells
492 * - of_xlate callback (optional)
494 * 3) In the of_mm_gpio_chip structure:
495 * - save_regs callback (optional)
497 * If succeeded, this function will map bank's memory and will
498 * do all necessary work for you. Then you'll able to use .regs
499 * to manage GPIOs from the callbacks.
501 int of_mm_gpiochip_add_data(struct device_node *np,
502 struct of_mm_gpio_chip *mm_gc,
503 void *data)
505 int ret = -ENOMEM;
506 struct gpio_chip *gc = &mm_gc->gc;
508 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
509 if (!gc->label)
510 goto err0;
512 mm_gc->regs = of_iomap(np, 0);
513 if (!mm_gc->regs)
514 goto err1;
516 gc->base = -1;
518 if (mm_gc->save_regs)
519 mm_gc->save_regs(mm_gc);
521 mm_gc->gc.of_node = np;
523 ret = gpiochip_add_data(gc, data);
524 if (ret)
525 goto err2;
527 return 0;
528 err2:
529 iounmap(mm_gc->regs);
530 err1:
531 kfree(gc->label);
532 err0:
533 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
534 return ret;
536 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
539 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
540 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
542 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
544 struct gpio_chip *gc = &mm_gc->gc;
546 if (!mm_gc)
547 return;
549 gpiochip_remove(gc);
550 iounmap(mm_gc->regs);
551 kfree(gc->label);
553 EXPORT_SYMBOL(of_mm_gpiochip_remove);
555 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
557 int len, i;
558 u32 start, count;
559 struct device_node *np = chip->of_node;
561 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
562 if (len < 0 || len % 2 != 0)
563 return;
565 for (i = 0; i < len; i += 2) {
566 of_property_read_u32_index(np, "gpio-reserved-ranges",
567 i, &start);
568 of_property_read_u32_index(np, "gpio-reserved-ranges",
569 i + 1, &count);
570 if (start >= chip->ngpio || start + count >= chip->ngpio)
571 continue;
573 bitmap_clear(chip->valid_mask, start, count);
577 #ifdef CONFIG_PINCTRL
578 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
580 struct device_node *np = chip->of_node;
581 struct of_phandle_args pinspec;
582 struct pinctrl_dev *pctldev;
583 int index = 0, ret;
584 const char *name;
585 static const char group_names_propname[] = "gpio-ranges-group-names";
586 struct property *group_names;
588 if (!np)
589 return 0;
591 group_names = of_find_property(np, group_names_propname, NULL);
593 for (;; index++) {
594 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
595 index, &pinspec);
596 if (ret)
597 break;
599 pctldev = of_pinctrl_get(pinspec.np);
600 of_node_put(pinspec.np);
601 if (!pctldev)
602 return -EPROBE_DEFER;
604 if (pinspec.args[2]) {
605 if (group_names) {
606 of_property_read_string_index(np,
607 group_names_propname,
608 index, &name);
609 if (strlen(name)) {
610 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
611 np);
612 break;
615 /* npins != 0: linear range */
616 ret = gpiochip_add_pin_range(chip,
617 pinctrl_dev_get_devname(pctldev),
618 pinspec.args[0],
619 pinspec.args[1],
620 pinspec.args[2]);
621 if (ret)
622 return ret;
623 } else {
624 /* npins == 0: special range */
625 if (pinspec.args[1]) {
626 pr_err("%pOF: Illegal gpio-range format.\n",
627 np);
628 break;
631 if (!group_names) {
632 pr_err("%pOF: GPIO group range requested but no %s property.\n",
633 np, group_names_propname);
634 break;
637 ret = of_property_read_string_index(np,
638 group_names_propname,
639 index, &name);
640 if (ret)
641 break;
643 if (!strlen(name)) {
644 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
645 np);
646 break;
649 ret = gpiochip_add_pingroup_range(chip, pctldev,
650 pinspec.args[0], name);
651 if (ret)
652 return ret;
656 return 0;
659 #else
660 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
661 #endif
663 int of_gpiochip_add(struct gpio_chip *chip)
665 int status;
667 if (!chip->of_node)
668 return 0;
670 if (!chip->of_xlate) {
671 chip->of_gpio_n_cells = 2;
672 chip->of_xlate = of_gpio_simple_xlate;
675 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
676 return -EINVAL;
678 of_gpiochip_init_valid_mask(chip);
680 status = of_gpiochip_add_pin_range(chip);
681 if (status)
682 return status;
684 /* If the chip defines names itself, these take precedence */
685 if (!chip->names)
686 devprop_gpiochip_set_names(chip,
687 of_fwnode_handle(chip->of_node));
689 of_node_get(chip->of_node);
691 return of_gpiochip_scan_gpios(chip);
694 void of_gpiochip_remove(struct gpio_chip *chip)
696 gpiochip_remove_pin_ranges(chip);
697 of_node_put(chip->of_node);