1 // SPDX-License-Identifier: GPL-2.0+
3 * OF helpers for the GPIO API
5 * Copyright (c) 2007-2008 MontaVista Software, Inc.
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
15 #include <linux/gpio/consumer.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>
24 #include "gpiolib-of.h"
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 and PPC 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
35 static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
37 struct device_node *np = dev->of_node;
39 if (!IS_ENABLED(CONFIG_SPI_MASTER))
41 if (!con_id || strcmp(con_id, "cs"))
43 if (!of_device_is_compatible(np, "fsl,spi") &&
44 !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
45 !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
47 return of_gpio_named_count(np, "gpios");
51 * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
53 * FIXME: get rid of those external users by converting them to GPIO
54 * descriptors and let them all use gpiod_count()
56 int of_gpio_get_count(struct device *dev, const char *con_id)
62 ret = of_gpio_spi_cs_get_count(dev, con_id);
66 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
68 snprintf(propname, sizeof(propname), "%s-%s",
69 con_id, gpio_suffixes[i]);
71 snprintf(propname, sizeof(propname), "%s",
74 ret = of_gpio_named_count(dev->of_node, propname);
78 return ret ? ret : -ENOENT;
81 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
83 struct of_phandle_args *gpiospec = data;
85 return chip->gpiodev->dev.of_node == gpiospec->np &&
87 chip->of_xlate(chip, gpiospec, NULL) >= 0;
90 static struct gpio_chip *of_find_gpiochip_by_xlate(
91 struct of_phandle_args *gpiospec)
93 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
96 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
97 struct of_phandle_args *gpiospec,
98 enum of_gpio_flags *flags)
102 if (chip->of_gpio_n_cells != gpiospec->args_count)
103 return ERR_PTR(-EINVAL);
105 ret = chip->of_xlate(chip, gpiospec, flags);
109 return gpiochip_get_desc(chip, ret);
113 * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
114 * to set the .valid_mask
115 * @gc: the target gpio_chip
117 * Return: true if the valid mask needs to be set
119 bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
122 struct device_node *np = gc->of_node;
124 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
125 if (size > 0 && size % 2 == 0)
130 static void of_gpio_flags_quirks(struct device_node *np,
131 const char *propname,
132 enum of_gpio_flags *flags,
136 * Some GPIO fixed regulator quirks.
137 * Note that active low is the default.
139 if (IS_ENABLED(CONFIG_REGULATOR) &&
140 (of_device_is_compatible(np, "regulator-fixed") ||
141 of_device_is_compatible(np, "reg-fixed-voltage") ||
142 (!(strcmp(propname, "enable-gpio") &&
143 strcmp(propname, "enable-gpios")) &&
144 of_device_is_compatible(np, "regulator-gpio")))) {
145 bool active_low = !of_property_read_bool(np,
146 "enable-active-high");
148 * The regulator GPIO handles are specified such that the
149 * presence or absence of "enable-active-high" solely controls
150 * the polarity of the GPIO line. Any phandle flags must
151 * be actively ignored.
153 if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
154 pr_warn("%s GPIO handle specifies active low - ignored\n",
155 of_node_full_name(np));
156 *flags &= ~OF_GPIO_ACTIVE_LOW;
159 *flags |= OF_GPIO_ACTIVE_LOW;
162 * Legacy open drain handling for fixed voltage regulators.
164 if (IS_ENABLED(CONFIG_REGULATOR) &&
165 of_device_is_compatible(np, "reg-fixed-voltage") &&
166 of_property_read_bool(np, "gpio-open-drain")) {
167 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
168 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
169 of_node_full_name(np));
173 * Legacy handling of SPI active high chip select. If we have a
174 * property named "cs-gpios" we need to inspect the child node
175 * to determine if the flags should have inverted semantics.
177 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
178 of_property_read_bool(np, "cs-gpios")) {
179 struct device_node *child;
183 for_each_child_of_node(np, child) {
184 ret = of_property_read_u32(child, "reg", &cs);
189 * SPI children have active low chip selects
190 * by default. This can be specified negatively
191 * by just omitting "spi-cs-high" in the
192 * device node, or actively by tagging on
193 * GPIO_ACTIVE_LOW as flag in the device
194 * tree. If the line is simultaneously
195 * tagged as active low in the device tree
196 * and has the "spi-cs-high" set, we get a
197 * conflict and the "spi-cs-high" flag will
200 if (of_property_read_bool(child, "spi-cs-high")) {
201 if (*flags & OF_GPIO_ACTIVE_LOW) {
202 pr_warn("%s GPIO handle specifies active low - ignored\n",
203 of_node_full_name(child));
204 *flags &= ~OF_GPIO_ACTIVE_LOW;
207 if (!(*flags & OF_GPIO_ACTIVE_LOW))
208 pr_info("%s enforce active low on chipselect handle\n",
209 of_node_full_name(child));
210 *flags |= OF_GPIO_ACTIVE_LOW;
218 /* Legacy handling of stmmac's active-low PHY reset line */
219 if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
220 !strcmp(propname, "snps,reset-gpio") &&
221 of_property_read_bool(np, "snps,reset-active-low"))
222 *flags |= OF_GPIO_ACTIVE_LOW;
226 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
227 * @np: device node to get GPIO from
228 * @propname: property name containing gpio specifier(s)
229 * @index: index of the GPIO
230 * @flags: a flags pointer to fill in
232 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
233 * value on the error condition. If @flags is not NULL the function also fills
234 * in flags for the GPIO.
236 static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
237 const char *propname, int index, enum of_gpio_flags *flags)
239 struct of_phandle_args gpiospec;
240 struct gpio_chip *chip;
241 struct gpio_desc *desc;
244 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
247 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
248 __func__, propname, np, index);
252 chip = of_find_gpiochip_by_xlate(&gpiospec);
254 desc = ERR_PTR(-EPROBE_DEFER);
258 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
263 of_gpio_flags_quirks(np, propname, flags, index);
265 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
266 __func__, propname, np, index,
267 PTR_ERR_OR_ZERO(desc));
270 of_node_put(gpiospec.np);
275 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
276 int index, enum of_gpio_flags *flags)
278 struct gpio_desc *desc;
280 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
283 return PTR_ERR(desc);
285 return desc_to_gpio(desc);
287 EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
290 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
291 * @node: handle of the OF node
292 * @propname: name of the DT property representing the GPIO
293 * @index: index of the GPIO to obtain for the consumer
294 * @dflags: GPIO initialization flags
295 * @label: label to attach to the requested GPIO
298 * On successful request the GPIO pin is configured in accordance with
301 * In case of error an ERR_PTR() is returned.
303 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
304 const char *propname, int index,
305 enum gpiod_flags dflags,
308 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
309 struct gpio_desc *desc;
310 enum of_gpio_flags flags;
311 bool active_low = false;
312 bool single_ended = false;
313 bool open_drain = false;
314 bool transitory = false;
317 desc = of_get_named_gpiod_flags(node, propname,
320 if (!desc || IS_ERR(desc)) {
324 active_low = flags & OF_GPIO_ACTIVE_LOW;
325 single_ended = flags & OF_GPIO_SINGLE_ENDED;
326 open_drain = flags & OF_GPIO_OPEN_DRAIN;
327 transitory = flags & OF_GPIO_TRANSITORY;
329 ret = gpiod_request(desc, label);
330 if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
336 lflags |= GPIO_ACTIVE_LOW;
340 lflags |= GPIO_OPEN_DRAIN;
342 lflags |= GPIO_OPEN_SOURCE;
346 lflags |= GPIO_TRANSITORY;
348 if (flags & OF_GPIO_PULL_UP)
349 lflags |= GPIO_PULL_UP;
351 if (flags & OF_GPIO_PULL_DOWN)
352 lflags |= GPIO_PULL_DOWN;
354 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
362 EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
365 * The SPI GPIO bindings happened before we managed to establish that GPIO
366 * properties should be named "foo-gpios" so we have this special kludge for
369 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
370 enum of_gpio_flags *of_flags)
372 char prop_name[32]; /* 32 is max size of property name */
373 struct device_node *np = dev->of_node;
374 struct gpio_desc *desc;
377 * Hopefully the compiler stubs the rest of the function if this
380 if (!IS_ENABLED(CONFIG_SPI_MASTER))
381 return ERR_PTR(-ENOENT);
383 /* Allow this specifically for "spi-gpio" devices */
384 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
385 return ERR_PTR(-ENOENT);
387 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
388 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
390 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
395 * The old Freescale bindings use simply "gpios" as name for the chip select
396 * lines rather than "cs-gpios" like all other SPI hardware. Account for this
397 * with a special quirk.
399 static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
402 unsigned long *flags)
404 struct device_node *np = dev->of_node;
406 if (!IS_ENABLED(CONFIG_SPI_MASTER))
407 return ERR_PTR(-ENOENT);
409 /* Allow this specifically for Freescale and PPC devices */
410 if (!of_device_is_compatible(np, "fsl,spi") &&
411 !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
412 !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
413 return ERR_PTR(-ENOENT);
414 /* Allow only if asking for "cs-gpios" */
415 if (!con_id || strcmp(con_id, "cs"))
416 return ERR_PTR(-ENOENT);
419 * While all other SPI controllers use "cs-gpios" the Freescale
420 * uses just "gpios" so translate to that when "cs-gpios" is
423 return of_find_gpio(dev, NULL, idx, flags);
427 * Some regulator bindings happened before we managed to establish that GPIO
428 * properties should be named "foo-gpios" so we have this special kludge for
431 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
432 enum of_gpio_flags *of_flags)
434 /* These are the connection IDs we accept as legacy GPIO phandles */
435 const char *whitelist[] = {
436 "wlf,ldoena", /* Arizona */
437 "wlf,ldo1ena", /* WM8994 */
438 "wlf,ldo2ena", /* WM8994 */
440 struct device_node *np = dev->of_node;
441 struct gpio_desc *desc;
444 if (!IS_ENABLED(CONFIG_REGULATOR))
445 return ERR_PTR(-ENOENT);
448 return ERR_PTR(-ENOENT);
450 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
452 return ERR_PTR(-ENOENT);
454 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
458 static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
460 enum of_gpio_flags *of_flags)
462 if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
463 return ERR_PTR(-ENOENT);
465 if (!con_id || strcmp(con_id, "wlf,reset"))
466 return ERR_PTR(-ENOENT);
468 return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
471 static struct gpio_desc *of_find_usb_gpio(struct device *dev,
473 enum of_gpio_flags *of_flags)
476 * Currently this USB quirk is only for the Fairchild FUSB302 host which is using
477 * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios"
480 if (!IS_ENABLED(CONFIG_TYPEC_FUSB302))
481 return ERR_PTR(-ENOENT);
483 if (!con_id || strcmp(con_id, "fcs,int_n"))
484 return ERR_PTR(-ENOENT);
486 return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
489 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
490 unsigned int idx, unsigned long *flags)
492 char prop_name[32]; /* 32 is max size of property name */
493 enum of_gpio_flags of_flags;
494 struct gpio_desc *desc;
497 /* Try GPIO property "foo-gpios" and "foo-gpio" */
498 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
500 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
503 snprintf(prop_name, sizeof(prop_name), "%s",
506 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
509 if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
513 if (PTR_ERR(desc) == -ENOENT) {
514 /* Special handling for SPI GPIOs if used */
515 desc = of_find_spi_gpio(dev, con_id, &of_flags);
518 if (PTR_ERR(desc) == -ENOENT) {
519 /* This quirk looks up flags and all */
520 desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
525 if (PTR_ERR(desc) == -ENOENT) {
526 /* Special handling for regulator GPIOs if used */
527 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
530 if (PTR_ERR(desc) == -ENOENT)
531 desc = of_find_arizona_gpio(dev, con_id, &of_flags);
533 if (PTR_ERR(desc) == -ENOENT)
534 desc = of_find_usb_gpio(dev, con_id, &of_flags);
539 if (of_flags & OF_GPIO_ACTIVE_LOW)
540 *flags |= GPIO_ACTIVE_LOW;
542 if (of_flags & OF_GPIO_SINGLE_ENDED) {
543 if (of_flags & OF_GPIO_OPEN_DRAIN)
544 *flags |= GPIO_OPEN_DRAIN;
546 *flags |= GPIO_OPEN_SOURCE;
549 if (of_flags & OF_GPIO_TRANSITORY)
550 *flags |= GPIO_TRANSITORY;
552 if (of_flags & OF_GPIO_PULL_UP)
553 *flags |= GPIO_PULL_UP;
554 if (of_flags & OF_GPIO_PULL_DOWN)
555 *flags |= GPIO_PULL_DOWN;
561 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
562 * @np: device node to get GPIO from
563 * @chip: GPIO chip whose hog is parsed
564 * @idx: Index of the GPIO to parse
565 * @name: GPIO line name
566 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
567 * of_find_gpio() or of_parse_own_gpio()
568 * @dflags: gpiod_flags - optional GPIO initialization flags
570 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
571 * value on the error condition.
573 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
574 struct gpio_chip *chip,
575 unsigned int idx, const char **name,
576 unsigned long *lflags,
577 enum gpiod_flags *dflags)
579 struct device_node *chip_np;
580 enum of_gpio_flags xlate_flags;
581 struct of_phandle_args gpiospec;
582 struct gpio_desc *desc;
587 chip_np = chip->of_node;
589 return ERR_PTR(-EINVAL);
592 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
595 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
599 gpiospec.np = chip_np;
600 gpiospec.args_count = tmp;
602 for (i = 0; i < tmp; i++) {
603 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
609 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
613 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
614 *lflags |= GPIO_ACTIVE_LOW;
615 if (xlate_flags & OF_GPIO_TRANSITORY)
616 *lflags |= GPIO_TRANSITORY;
617 if (xlate_flags & OF_GPIO_PULL_UP)
618 *lflags |= GPIO_PULL_UP;
619 if (xlate_flags & OF_GPIO_PULL_DOWN)
620 *lflags |= GPIO_PULL_DOWN;
622 if (of_property_read_bool(np, "input"))
624 else if (of_property_read_bool(np, "output-low"))
625 *dflags |= GPIOD_OUT_LOW;
626 else if (of_property_read_bool(np, "output-high"))
627 *dflags |= GPIOD_OUT_HIGH;
629 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
630 desc_to_gpio(desc), np);
631 return ERR_PTR(-EINVAL);
634 if (name && of_property_read_string(np, "line-name", name))
641 * of_gpiochip_add_hog - Add all hogs in a hog device node
642 * @chip: gpio chip to act on
643 * @hog: device node describing the hogs
645 * Returns error if it fails otherwise 0 on success.
647 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
649 enum gpiod_flags dflags;
650 struct gpio_desc *desc;
651 unsigned long lflags;
657 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
661 ret = gpiod_hog(desc, name, lflags, dflags);
665 #ifdef CONFIG_OF_DYNAMIC
674 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
675 * @chip: gpio chip to act on
677 * This is only used by of_gpiochip_add to request/set GPIO initial
679 * It returns error if it fails otherwise 0 on success.
681 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
683 struct device_node *np;
686 for_each_available_child_of_node(chip->of_node, np) {
687 if (!of_property_read_bool(np, "gpio-hog"))
690 ret = of_gpiochip_add_hog(chip, np);
696 of_node_set_flag(np, OF_POPULATED);
702 #ifdef CONFIG_OF_DYNAMIC
704 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
705 * @chip: gpio chip to act on
706 * @hog: device node describing the hogs
708 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
709 struct device_node *hog)
711 struct gpio_desc *descs = chip->gpiodev->descs;
714 for (i = 0; i < chip->ngpio; i++) {
715 if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) &&
717 gpiochip_free_own_desc(&descs[i]);
721 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
723 return chip->gpiodev->dev.of_node == data;
726 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
728 return gpiochip_find(np, of_gpiochip_match_node);
731 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
734 struct of_reconfig_data *rd = arg;
735 struct gpio_chip *chip;
739 * This only supports adding and removing complete gpio-hog nodes.
740 * Modifying an existing gpio-hog node is not supported (except for
741 * changing its "status" property, which is treated the same as
744 switch (of_reconfig_get_state_change(action, arg)) {
745 case OF_RECONFIG_CHANGE_ADD:
746 if (!of_property_read_bool(rd->dn, "gpio-hog"))
747 return NOTIFY_OK; /* not for us */
749 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
752 chip = of_find_gpiochip_by_node(rd->dn->parent);
754 return NOTIFY_OK; /* not for us */
756 ret = of_gpiochip_add_hog(chip, rd->dn);
758 pr_err("%s: failed to add hogs for %pOF\n", __func__,
760 of_node_clear_flag(rd->dn, OF_POPULATED);
761 return notifier_from_errno(ret);
765 case OF_RECONFIG_CHANGE_REMOVE:
766 if (!of_node_check_flag(rd->dn, OF_POPULATED))
767 return NOTIFY_OK; /* already depopulated */
769 chip = of_find_gpiochip_by_node(rd->dn->parent);
771 return NOTIFY_OK; /* not for us */
773 of_gpiochip_remove_hog(chip, rd->dn);
774 of_node_clear_flag(rd->dn, OF_POPULATED);
781 struct notifier_block gpio_of_notifier = {
782 .notifier_call = of_gpio_notify,
784 #endif /* CONFIG_OF_DYNAMIC */
787 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
788 * @gc: pointer to the gpio_chip structure
789 * @gpiospec: GPIO specifier as found in the device tree
790 * @flags: a flags pointer to fill in
792 * This is simple translation function, suitable for the most 1:1 mapped
793 * GPIO chips. This function performs only one sanity check: whether GPIO
794 * is less than ngpios (that is specified in the gpio_chip).
796 static int of_gpio_simple_xlate(struct gpio_chip *gc,
797 const struct of_phandle_args *gpiospec,
801 * We're discouraging gpio_cells < 2, since that way you'll have to
802 * write your own xlate function (that will have to retrieve the GPIO
803 * number and the flags from a single gpio cell -- this is possible,
804 * but not recommended).
806 if (gc->of_gpio_n_cells < 2) {
811 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
814 if (gpiospec->args[0] >= gc->ngpio)
818 *flags = gpiospec->args[1];
820 return gpiospec->args[0];
824 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
825 * @np: device node of the GPIO chip
826 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
827 * @data: driver data to store in the struct gpio_chip
829 * To use this function you should allocate and fill mm_gc with:
831 * 1) In the gpio_chip structure:
832 * - all the callbacks
834 * - of_xlate callback (optional)
836 * 3) In the of_mm_gpio_chip structure:
837 * - save_regs callback (optional)
839 * If succeeded, this function will map bank's memory and will
840 * do all necessary work for you. Then you'll able to use .regs
841 * to manage GPIOs from the callbacks.
843 int of_mm_gpiochip_add_data(struct device_node *np,
844 struct of_mm_gpio_chip *mm_gc,
848 struct gpio_chip *gc = &mm_gc->gc;
850 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
854 mm_gc->regs = of_iomap(np, 0);
860 if (mm_gc->save_regs)
861 mm_gc->save_regs(mm_gc);
863 mm_gc->gc.of_node = np;
865 ret = gpiochip_add_data(gc, data);
871 iounmap(mm_gc->regs);
875 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
878 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
881 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
882 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
884 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
886 struct gpio_chip *gc = &mm_gc->gc;
892 iounmap(mm_gc->regs);
895 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
897 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
901 struct device_node *np = chip->of_node;
903 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
904 if (len < 0 || len % 2 != 0)
907 for (i = 0; i < len; i += 2) {
908 of_property_read_u32_index(np, "gpio-reserved-ranges",
910 of_property_read_u32_index(np, "gpio-reserved-ranges",
912 if (start >= chip->ngpio || start + count >= chip->ngpio)
915 bitmap_clear(chip->valid_mask, start, count);
919 #ifdef CONFIG_PINCTRL
920 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
922 struct device_node *np = chip->of_node;
923 struct of_phandle_args pinspec;
924 struct pinctrl_dev *pctldev;
927 static const char group_names_propname[] = "gpio-ranges-group-names";
928 struct property *group_names;
933 group_names = of_find_property(np, group_names_propname, NULL);
936 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
941 pctldev = of_pinctrl_get(pinspec.np);
942 of_node_put(pinspec.np);
944 return -EPROBE_DEFER;
946 if (pinspec.args[2]) {
948 of_property_read_string_index(np,
949 group_names_propname,
952 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
957 /* npins != 0: linear range */
958 ret = gpiochip_add_pin_range(chip,
959 pinctrl_dev_get_devname(pctldev),
966 /* npins == 0: special range */
967 if (pinspec.args[1]) {
968 pr_err("%pOF: Illegal gpio-range format.\n",
974 pr_err("%pOF: GPIO group range requested but no %s property.\n",
975 np, group_names_propname);
979 ret = of_property_read_string_index(np,
980 group_names_propname,
986 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
991 ret = gpiochip_add_pingroup_range(chip, pctldev,
992 pinspec.args[0], name);
1002 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1005 int of_gpiochip_add(struct gpio_chip *chip)
1012 if (!chip->of_xlate) {
1013 chip->of_gpio_n_cells = 2;
1014 chip->of_xlate = of_gpio_simple_xlate;
1017 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1020 of_gpiochip_init_valid_mask(chip);
1022 ret = of_gpiochip_add_pin_range(chip);
1026 /* If the chip defines names itself, these take precedence */
1028 devprop_gpiochip_set_names(chip,
1029 of_fwnode_handle(chip->of_node));
1031 of_node_get(chip->of_node);
1033 ret = of_gpiochip_scan_gpios(chip);
1035 of_node_put(chip->of_node);
1040 void of_gpiochip_remove(struct gpio_chip *chip)
1042 of_node_put(chip->of_node);