Lines Matching +full:gpio +full:- +full:hog

1 // SPDX-License-Identifier: GPL-2.0+
3 * OF helpers for the GPIO API
5 * Copyright (c) 2007-2008 MontaVista Software, Inc.
15 #include <linux/gpio/consumer.h>
21 #include <linux/gpio/machine.h>
24 #include "gpiolib-of.h"
27 * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
29 * @con_id: Function within the GPIO consumer
31 * Some elder GPIO controllers need special quirks. Currently we handle
32 * the Freescale and PPC GPIO controller with bindings that doesn't use the
33 * established "cs-gpios" for chip selects but instead rely on
35 * the counting of "cs-gpios" to count "gpios" transparent to the
40 struct device_node *np = dev->of_node; in of_gpio_spi_cs_get_count()
48 !of_device_is_compatible(np, "ibm,ppc4xx-spi")) in of_gpio_spi_cs_get_count()
56 * FIXME: get rid of those external users by converting them to GPIO
71 snprintf(propname, sizeof(propname), "%s-%s", in of_gpio_get_count()
77 ret = of_gpio_named_count(dev->of_node, propname); in of_gpio_get_count()
81 return ret ? ret : -ENOENT; in of_gpio_get_count()
88 return chip->gpiodev->dev.of_node == gpiospec->np && in of_gpiochip_match_node_and_xlate()
89 chip->of_xlate && in of_gpiochip_match_node_and_xlate()
90 chip->of_xlate(chip, gpiospec, NULL) >= 0; in of_gpiochip_match_node_and_xlate()
105 if (chip->of_gpio_n_cells != gpiospec->args_count) in of_xlate_and_get_gpiod_flags()
106 return ERR_PTR(-EINVAL); in of_xlate_and_get_gpiod_flags()
108 ret = chip->of_xlate(chip, gpiospec, flags); in of_xlate_and_get_gpiod_flags()
116 * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
125 const struct device_node *np = gc->of_node; in of_gpio_need_valid_mask()
127 size = of_property_count_u32_elems(np, "gpio-reserved-ranges"); in of_gpio_need_valid_mask()
139 * Some GPIO fixed regulator quirks. in of_gpio_flags_quirks()
143 (of_device_is_compatible(np, "regulator-fixed") || in of_gpio_flags_quirks()
144 of_device_is_compatible(np, "reg-fixed-voltage") || in of_gpio_flags_quirks()
145 (!(strcmp(propname, "enable-gpio") && in of_gpio_flags_quirks()
146 strcmp(propname, "enable-gpios")) && in of_gpio_flags_quirks()
147 of_device_is_compatible(np, "regulator-gpio")))) { in of_gpio_flags_quirks()
149 "enable-active-high"); in of_gpio_flags_quirks()
151 * The regulator GPIO handles are specified such that the in of_gpio_flags_quirks()
152 * presence or absence of "enable-active-high" solely controls in of_gpio_flags_quirks()
153 * the polarity of the GPIO line. Any phandle flags must in of_gpio_flags_quirks()
157 pr_warn("%s GPIO handle specifies active low - ignored\n", in of_gpio_flags_quirks()
168 of_device_is_compatible(np, "reg-fixed-voltage") && in of_gpio_flags_quirks()
169 of_property_read_bool(np, "gpio-open-drain")) { in of_gpio_flags_quirks()
171 pr_info("%s uses legacy open drain flag - update the DTS if you can\n", in of_gpio_flags_quirks()
177 * property named "cs-gpios" we need to inspect the child node in of_gpio_flags_quirks()
180 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && in of_gpio_flags_quirks()
181 of_property_read_bool(np, "cs-gpios")) { in of_gpio_flags_quirks()
194 * by just omitting "spi-cs-high" in the in of_gpio_flags_quirks()
199 * and has the "spi-cs-high" set, we get a in of_gpio_flags_quirks()
200 * conflict and the "spi-cs-high" flag will in of_gpio_flags_quirks()
203 if (of_property_read_bool(child, "spi-cs-high")) { in of_gpio_flags_quirks()
205 pr_warn("%s GPIO handle specifies active low - ignored\n", in of_gpio_flags_quirks()
221 /* Legacy handling of stmmac's active-low PHY reset line */ in of_gpio_flags_quirks()
223 !strcmp(propname, "snps,reset-gpio") && in of_gpio_flags_quirks()
224 of_property_read_bool(np, "snps,reset-active-low")) in of_gpio_flags_quirks()
229 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
230 * @np: device node to get GPIO from
231 * @propname: property name containing gpio specifier(s)
232 * @index: index of the GPIO
235 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
237 * in flags for the GPIO.
247 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, in of_get_named_gpiod_flags()
257 desc = ERR_PTR(-EPROBE_DEFER); in of_get_named_gpiod_flags()
268 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", in of_get_named_gpiod_flags()
323 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
325 * @propname: name of the DT property representing the GPIO
326 * @index: index of the GPIO to obtain for the consumer
327 * @dflags: GPIO initialization flags
328 * @label: label to attach to the requested GPIO
331 * On successful request the GPIO pin is configured in accordance with
351 if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) in gpiod_get_from_of_node()
369 * The SPI GPIO bindings happened before we managed to establish that GPIO
370 * properties should be named "foo-gpios" so we have this special kludge for
385 return ERR_PTR(-ENOENT); in of_find_spi_gpio()
387 /* Allow this specifically for "spi-gpio" devices */ in of_find_spi_gpio()
388 if (!of_device_is_compatible(np, "spi-gpio") || !con_id) in of_find_spi_gpio()
389 return ERR_PTR(-ENOENT); in of_find_spi_gpio()
391 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ in of_find_spi_gpio()
392 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id); in of_find_spi_gpio()
399 * lines rather than "cs-gpios" like all other SPI hardware. Account for this
408 return ERR_PTR(-ENOENT); in of_find_spi_cs_gpio()
413 !of_device_is_compatible(np, "ibm,ppc4xx-spi")) in of_find_spi_cs_gpio()
414 return ERR_PTR(-ENOENT); in of_find_spi_cs_gpio()
415 /* Allow only if asking for "cs-gpios" */ in of_find_spi_cs_gpio()
417 return ERR_PTR(-ENOENT); in of_find_spi_cs_gpio()
420 * While all other SPI controllers use "cs-gpios" the Freescale in of_find_spi_cs_gpio()
421 * uses just "gpios" so translate to that when "cs-gpios" is in of_find_spi_cs_gpio()
428 * Some regulator bindings happened before we managed to establish that GPIO
429 * properties should be named "foo-gpios" so we have this special kludge for
437 /* These are the connection IDs we accept as legacy GPIO phandles */ in of_find_regulator_gpio()
446 return ERR_PTR(-ENOENT); in of_find_regulator_gpio()
449 return ERR_PTR(-ENOENT); in of_find_regulator_gpio()
453 return ERR_PTR(-ENOENT); in of_find_regulator_gpio()
464 return ERR_PTR(-ENOENT); in of_find_arizona_gpio()
467 return ERR_PTR(-ENOENT); in of_find_arizona_gpio()
479 * which is using an undocumented DT GPIO line named "fcs,int_n" in of_find_usb_gpio()
480 * without the compulsory "-gpios" suffix. in of_find_usb_gpio()
483 return ERR_PTR(-ENOENT); in of_find_usb_gpio()
486 return ERR_PTR(-ENOENT); in of_find_usb_gpio()
513 /* Try GPIO property "foo-gpios" and "foo-gpio" */ in of_find_gpio()
516 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, in of_find_gpio()
522 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, in of_find_gpio()
529 /* Properly named GPIO was not found, try workarounds */ in of_find_gpio()
531 desc = (*q)(dev->of_node, con_id, idx, &of_flags); in of_find_gpio()
542 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
543 * @np: device node to get GPIO from
544 * @chip: GPIO chip whose hog is parsed
545 * @idx: Index of the GPIO to parse
546 * @name: GPIO line name
547 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
549 * @dflags: gpiod_flags - optional GPIO initialization flags
551 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
568 chip_np = chip->of_node; in of_parse_own_gpio()
570 return ERR_PTR(-EINVAL); in of_parse_own_gpio()
576 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); in of_parse_own_gpio()
598 else if (of_property_read_bool(np, "output-low")) in of_parse_own_gpio()
600 else if (of_property_read_bool(np, "output-high")) in of_parse_own_gpio()
603 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", in of_parse_own_gpio()
605 return ERR_PTR(-EINVAL); in of_parse_own_gpio()
608 if (name && of_property_read_string(np, "line-name", name)) in of_parse_own_gpio()
609 *name = np->name; in of_parse_own_gpio()
615 * of_gpiochip_add_hog - Add all hogs in a hog device node
616 * @chip: gpio chip to act on
617 * @hog: device node describing the hogs
621 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) in of_gpiochip_add_hog() argument
631 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags); in of_gpiochip_add_hog()
640 desc->hog = hog; in of_gpiochip_add_hog()
648 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
649 * @chip: gpio chip to act on
651 * This is only used by of_gpiochip_add to request/set GPIO initial
660 for_each_available_child_of_node(chip->of_node, np) { in of_gpiochip_scan_gpios()
661 if (!of_property_read_bool(np, "gpio-hog")) in of_gpiochip_scan_gpios()
678 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
679 * @chip: gpio chip to act on
680 * @hog: device node describing the hogs
683 struct device_node *hog) in of_gpiochip_remove_hog() argument
688 if (desc->hog == hog) in of_gpiochip_remove_hog()
694 return device_match_of_node(&chip->gpiodev->dev, data); in of_gpiochip_match_node()
710 * This only supports adding and removing complete gpio-hog nodes. in of_gpio_notify()
711 * Modifying an existing gpio-hog node is not supported (except for in of_gpio_notify()
717 if (!of_property_read_bool(rd->dn, "gpio-hog")) in of_gpio_notify()
720 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) in of_gpio_notify()
723 chip = of_find_gpiochip_by_node(rd->dn->parent); in of_gpio_notify()
727 ret = of_gpiochip_add_hog(chip, rd->dn); in of_gpio_notify()
730 rd->dn); in of_gpio_notify()
731 of_node_clear_flag(rd->dn, OF_POPULATED); in of_gpio_notify()
737 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_gpio_notify()
740 chip = of_find_gpiochip_by_node(rd->dn->parent); in of_gpio_notify()
744 of_gpiochip_remove_hog(chip, rd->dn); in of_gpio_notify()
745 of_node_clear_flag(rd->dn, OF_POPULATED); in of_gpio_notify()
758 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
760 * @gpiospec: GPIO specifier as found in the device tree
764 * GPIO chips. This function performs only one sanity check: whether GPIO
773 * write your own xlate function (that will have to retrieve the GPIO in of_gpio_simple_xlate()
774 * number and the flags from a single gpio cell -- this is possible, in of_gpio_simple_xlate()
777 if (gc->of_gpio_n_cells < 2) { in of_gpio_simple_xlate()
779 return -EINVAL; in of_gpio_simple_xlate()
782 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) in of_gpio_simple_xlate()
783 return -EINVAL; in of_gpio_simple_xlate()
785 if (gpiospec->args[0] >= gc->ngpio) in of_gpio_simple_xlate()
786 return -EINVAL; in of_gpio_simple_xlate()
789 *flags = gpiospec->args[1]; in of_gpio_simple_xlate()
791 return gpiospec->args[0]; in of_gpio_simple_xlate()
795 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
796 * @np: device node of the GPIO chip
803 * - all the callbacks
804 * - of_gpio_n_cells
805 * - of_xlate callback (optional)
808 * - save_regs callback (optional)
818 int ret = -ENOMEM; in of_mm_gpiochip_add_data()
819 struct gpio_chip *gc = &mm_gc->gc; in of_mm_gpiochip_add_data()
821 gc->label = kasprintf(GFP_KERNEL, "%pOF", np); in of_mm_gpiochip_add_data()
822 if (!gc->label) in of_mm_gpiochip_add_data()
825 mm_gc->regs = of_iomap(np, 0); in of_mm_gpiochip_add_data()
826 if (!mm_gc->regs) in of_mm_gpiochip_add_data()
829 gc->base = -1; in of_mm_gpiochip_add_data()
831 if (mm_gc->save_regs) in of_mm_gpiochip_add_data()
832 mm_gc->save_regs(mm_gc); in of_mm_gpiochip_add_data()
834 of_node_put(mm_gc->gc.of_node); in of_mm_gpiochip_add_data()
835 mm_gc->gc.of_node = of_node_get(np); in of_mm_gpiochip_add_data()
844 iounmap(mm_gc->regs); in of_mm_gpiochip_add_data()
846 kfree(gc->label); in of_mm_gpiochip_add_data()
848 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); in of_mm_gpiochip_add_data()
854 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
859 struct gpio_chip *gc = &mm_gc->gc; in of_mm_gpiochip_remove()
865 iounmap(mm_gc->regs); in of_mm_gpiochip_remove()
866 kfree(gc->label); in of_mm_gpiochip_remove()
874 struct device_node *np = chip->of_node; in of_gpiochip_init_valid_mask()
876 len = of_property_count_u32_elems(np, "gpio-reserved-ranges"); in of_gpiochip_init_valid_mask()
881 of_property_read_u32_index(np, "gpio-reserved-ranges", in of_gpiochip_init_valid_mask()
883 of_property_read_u32_index(np, "gpio-reserved-ranges", in of_gpiochip_init_valid_mask()
885 if (start >= chip->ngpio || start + count > chip->ngpio) in of_gpiochip_init_valid_mask()
888 bitmap_clear(chip->valid_mask, start, count); in of_gpiochip_init_valid_mask()
895 struct device_node *np = chip->of_node; in of_gpiochip_add_pin_range()
900 static const char group_names_propname[] = "gpio-ranges-group-names"; in of_gpiochip_add_pin_range()
906 if (!of_property_read_bool(np, "gpio-ranges") && in of_gpiochip_add_pin_range()
907 chip->of_gpio_ranges_fallback) { in of_gpiochip_add_pin_range()
908 return chip->of_gpio_ranges_fallback(chip, np); in of_gpiochip_add_pin_range()
914 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, in of_gpiochip_add_pin_range()
922 return -EPROBE_DEFER; in of_gpiochip_add_pin_range()
930 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", in of_gpiochip_add_pin_range()
946 pr_err("%pOF: Illegal gpio-range format.\n", in of_gpiochip_add_pin_range()
952 pr_err("%pOF: GPIO group range requested but no %s property.\n", in of_gpiochip_add_pin_range()
964 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", in of_gpiochip_add_pin_range()
987 if (!chip->of_node) in of_gpiochip_add()
990 if (!chip->of_xlate) { in of_gpiochip_add()
991 chip->of_gpio_n_cells = 2; in of_gpiochip_add()
992 chip->of_xlate = of_gpio_simple_xlate; in of_gpiochip_add()
995 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) in of_gpiochip_add()
996 return -EINVAL; in of_gpiochip_add()
1004 of_node_get(chip->of_node); in of_gpiochip_add()
1008 of_node_put(chip->of_node); in of_gpiochip_add()
1015 of_node_put(chip->of_node); in of_gpiochip_remove()
1021 if (gc->parent) in of_gpio_dev_init()
1022 gdev->dev.of_node = gc->parent->of_node; in of_gpio_dev_init()
1024 if (gc->fwnode) in of_gpio_dev_init()
1025 gc->of_node = to_of_node(gc->fwnode); in of_gpio_dev_init()
1028 if (gc->of_node) in of_gpio_dev_init()
1029 gdev->dev.of_node = gc->of_node; in of_gpio_dev_init()
1031 gc->of_node = gdev->dev.of_node; in of_gpio_dev_init()