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 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()
293 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
295 * @propname: name of the DT property representing the GPIO
296 * @index: index of the GPIO to obtain for the consumer
297 * @dflags: GPIO initialization flags
298 * @label: label to attach to the requested GPIO
301 * On successful request the GPIO pin is configured in accordance with
333 if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) in gpiod_get_from_of_node()
368 * The SPI GPIO bindings happened before we managed to establish that GPIO
369 * properties should be named "foo-gpios" so we have this special kludge for
376 struct device_node *np = dev->of_node; in of_find_spi_gpio()
384 return ERR_PTR(-ENOENT); in of_find_spi_gpio()
386 /* Allow this specifically for "spi-gpio" devices */ in of_find_spi_gpio()
387 if (!of_device_is_compatible(np, "spi-gpio") || !con_id) in of_find_spi_gpio()
388 return ERR_PTR(-ENOENT); in of_find_spi_gpio()
390 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ in of_find_spi_gpio()
391 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
407 struct device_node *np = dev->of_node; in of_find_spi_cs_gpio()
410 return ERR_PTR(-ENOENT); in of_find_spi_cs_gpio()
415 !of_device_is_compatible(np, "ibm,ppc4xx-spi")) in of_find_spi_cs_gpio()
416 return ERR_PTR(-ENOENT); in of_find_spi_cs_gpio()
417 /* Allow only if asking for "cs-gpios" */ in of_find_spi_cs_gpio()
419 return ERR_PTR(-ENOENT); in of_find_spi_cs_gpio()
422 * While all other SPI controllers use "cs-gpios" the Freescale in of_find_spi_cs_gpio()
423 * uses just "gpios" so translate to that when "cs-gpios" is in of_find_spi_cs_gpio()
430 * Some regulator bindings happened before we managed to establish that GPIO
431 * 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()
443 struct device_node *np = dev->of_node; in of_find_regulator_gpio()
448 return ERR_PTR(-ENOENT); in of_find_regulator_gpio()
451 return ERR_PTR(-ENOENT); in of_find_regulator_gpio()
455 return ERR_PTR(-ENOENT); in of_find_regulator_gpio()
466 return ERR_PTR(-ENOENT); in of_find_arizona_gpio()
469 return ERR_PTR(-ENOENT); in of_find_arizona_gpio()
471 return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags); in of_find_arizona_gpio()
480 * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios" in of_find_usb_gpio()
484 return ERR_PTR(-ENOENT); in of_find_usb_gpio()
487 return ERR_PTR(-ENOENT); in of_find_usb_gpio()
489 return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags); in of_find_usb_gpio()
500 /* Try GPIO property "foo-gpios" and "foo-gpio" */ in of_find_gpio()
503 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, in of_find_gpio()
509 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, in of_find_gpio()
512 if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT) in of_find_gpio()
516 if (PTR_ERR(desc) == -ENOENT) { in of_find_gpio()
521 if (PTR_ERR(desc) == -ENOENT) { in of_find_gpio()
528 if (PTR_ERR(desc) == -ENOENT) { in of_find_gpio()
533 if (PTR_ERR(desc) == -ENOENT) in of_find_gpio()
536 if (PTR_ERR(desc) == -ENOENT) in of_find_gpio()
564 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
565 * @np: device node to get GPIO from
566 * @chip: GPIO chip whose hog is parsed
567 * @idx: Index of the GPIO to parse
568 * @name: GPIO line name
569 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
571 * @dflags: gpiod_flags - optional GPIO initialization flags
573 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
590 chip_np = chip->of_node; in of_parse_own_gpio()
592 return ERR_PTR(-EINVAL); in of_parse_own_gpio()
598 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); in of_parse_own_gpio()
627 else if (of_property_read_bool(np, "output-low")) in of_parse_own_gpio()
629 else if (of_property_read_bool(np, "output-high")) in of_parse_own_gpio()
632 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", in of_parse_own_gpio()
634 return ERR_PTR(-EINVAL); in of_parse_own_gpio()
637 if (name && of_property_read_string(np, "line-name", name)) in of_parse_own_gpio()
638 *name = np->name; in of_parse_own_gpio()
644 * of_gpiochip_add_hog - Add all hogs in a hog device node
645 * @chip: gpio chip to act on
646 * @hog: device node describing the hogs
650 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) in of_gpiochip_add_hog() argument
660 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags); in of_gpiochip_add_hog()
669 desc->hog = hog; in of_gpiochip_add_hog()
677 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
678 * @chip: gpio chip to act on
680 * This is only used by of_gpiochip_add to request/set GPIO initial
689 for_each_available_child_of_node(chip->of_node, np) { in of_gpiochip_scan_gpios()
690 if (!of_property_read_bool(np, "gpio-hog")) in of_gpiochip_scan_gpios()
707 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
708 * @chip: gpio chip to act on
709 * @hog: device node describing the hogs
712 struct device_node *hog) in of_gpiochip_remove_hog() argument
714 struct gpio_desc *descs = chip->gpiodev->descs; in of_gpiochip_remove_hog()
717 for (i = 0; i < chip->ngpio; i++) { in of_gpiochip_remove_hog()
719 descs[i].hog == hog) in of_gpiochip_remove_hog()
726 return chip->gpiodev->dev.of_node == data; in of_gpiochip_match_node()
742 * This only supports adding and removing complete gpio-hog nodes. in of_gpio_notify()
743 * Modifying an existing gpio-hog node is not supported (except for in of_gpio_notify()
749 if (!of_property_read_bool(rd->dn, "gpio-hog")) in of_gpio_notify()
752 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) in of_gpio_notify()
755 chip = of_find_gpiochip_by_node(rd->dn->parent); in of_gpio_notify()
759 ret = of_gpiochip_add_hog(chip, rd->dn); in of_gpio_notify()
762 rd->dn); in of_gpio_notify()
763 of_node_clear_flag(rd->dn, OF_POPULATED); in of_gpio_notify()
769 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_gpio_notify()
772 chip = of_find_gpiochip_by_node(rd->dn->parent); in of_gpio_notify()
776 of_gpiochip_remove_hog(chip, rd->dn); in of_gpio_notify()
777 of_node_clear_flag(rd->dn, OF_POPULATED); in of_gpio_notify()
790 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
792 * @gpiospec: GPIO specifier as found in the device tree
796 * GPIO chips. This function performs only one sanity check: whether GPIO
805 * write your own xlate function (that will have to retrieve the GPIO in of_gpio_simple_xlate()
806 * number and the flags from a single gpio cell -- this is possible, in of_gpio_simple_xlate()
809 if (gc->of_gpio_n_cells < 2) { in of_gpio_simple_xlate()
811 return -EINVAL; in of_gpio_simple_xlate()
814 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) in of_gpio_simple_xlate()
815 return -EINVAL; in of_gpio_simple_xlate()
817 if (gpiospec->args[0] >= gc->ngpio) in of_gpio_simple_xlate()
818 return -EINVAL; in of_gpio_simple_xlate()
821 *flags = gpiospec->args[1]; in of_gpio_simple_xlate()
823 return gpiospec->args[0]; in of_gpio_simple_xlate()
827 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
828 * @np: device node of the GPIO chip
835 * - all the callbacks
836 * - of_gpio_n_cells
837 * - of_xlate callback (optional)
840 * - save_regs callback (optional)
850 int ret = -ENOMEM; in of_mm_gpiochip_add_data()
851 struct gpio_chip *gc = &mm_gc->gc; in of_mm_gpiochip_add_data()
853 gc->label = kasprintf(GFP_KERNEL, "%pOF", np); in of_mm_gpiochip_add_data()
854 if (!gc->label) in of_mm_gpiochip_add_data()
857 mm_gc->regs = of_iomap(np, 0); in of_mm_gpiochip_add_data()
858 if (!mm_gc->regs) in of_mm_gpiochip_add_data()
861 gc->base = -1; in of_mm_gpiochip_add_data()
863 if (mm_gc->save_regs) in of_mm_gpiochip_add_data()
864 mm_gc->save_regs(mm_gc); in of_mm_gpiochip_add_data()
866 mm_gc->gc.of_node = np; in of_mm_gpiochip_add_data()
874 iounmap(mm_gc->regs); in of_mm_gpiochip_add_data()
876 kfree(gc->label); in of_mm_gpiochip_add_data()
878 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); in of_mm_gpiochip_add_data()
884 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
889 struct gpio_chip *gc = &mm_gc->gc; in of_mm_gpiochip_remove()
895 iounmap(mm_gc->regs); in of_mm_gpiochip_remove()
896 kfree(gc->label); in of_mm_gpiochip_remove()
904 struct device_node *np = chip->of_node; in of_gpiochip_init_valid_mask()
906 len = of_property_count_u32_elems(np, "gpio-reserved-ranges"); in of_gpiochip_init_valid_mask()
911 of_property_read_u32_index(np, "gpio-reserved-ranges", in of_gpiochip_init_valid_mask()
913 of_property_read_u32_index(np, "gpio-reserved-ranges", in of_gpiochip_init_valid_mask()
915 if (start >= chip->ngpio || start + count >= chip->ngpio) in of_gpiochip_init_valid_mask()
918 bitmap_clear(chip->valid_mask, start, count); in of_gpiochip_init_valid_mask()
925 struct device_node *np = chip->of_node; in of_gpiochip_add_pin_range()
930 static const char group_names_propname[] = "gpio-ranges-group-names"; in of_gpiochip_add_pin_range()
939 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, in of_gpiochip_add_pin_range()
947 return -EPROBE_DEFER; in of_gpiochip_add_pin_range()
955 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", in of_gpiochip_add_pin_range()
971 pr_err("%pOF: Illegal gpio-range format.\n", in of_gpiochip_add_pin_range()
977 pr_err("%pOF: GPIO group range requested but no %s property.\n", in of_gpiochip_add_pin_range()
989 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", in of_gpiochip_add_pin_range()
1012 if (!chip->of_node) in of_gpiochip_add()
1015 if (!chip->of_xlate) { in of_gpiochip_add()
1016 chip->of_gpio_n_cells = 2; in of_gpiochip_add()
1017 chip->of_xlate = of_gpio_simple_xlate; in of_gpiochip_add()
1020 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) in of_gpiochip_add()
1021 return -EINVAL; in of_gpiochip_add()
1029 of_node_get(chip->of_node); in of_gpiochip_add()
1033 of_node_put(chip->of_node); in of_gpiochip_add()
1040 of_node_put(chip->of_node); in of_gpiochip_remove()