Lines Matching +full:cs +full:- +full:gpio

1 // SPDX-License-Identifier: GPL-2.0+
3 * OF helpers for the GPIO API
5 * Copyright (c) 2007-2008 MontaVista Software, Inc.
22 #include <linux/gpio/consumer.h>
23 #include <linux/gpio/machine.h>
26 #include "gpiolib-of.h"
29 * This is Linux-specific flags. By default controllers' and Linux' mapping
30 * match, but GPIO controllers are free to translate their own flags to
31 * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
44 * of_gpio_named_count() - Count GPIOs for a device
46 * @propname: property name containing gpio specifier(s)
49 * Note that the empty GPIO specifiers count too. Returns either
51 * -EINVAL for an incorrectly formed gpios property, or
52 * -ENOENT for a missing gpios property
66 return of_count_phandle_with_args(np, propname, "#gpio-cells"); in of_gpio_named_count()
70 * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
72 * @con_id: Function within the GPIO consumer
74 * Some elder GPIO controllers need special quirks. Currently we handle
75 * the Freescale and PPC GPIO controller with bindings that doesn't use the
76 * established "cs-gpios" for chip selects but instead rely on
78 * the counting of "cs-gpios" to count "gpios" transparent to the
83 struct device_node *np = dev->of_node; in of_gpio_spi_cs_get_count()
87 if (!con_id || strcmp(con_id, "cs")) in of_gpio_spi_cs_get_count()
91 !of_device_is_compatible(np, "ibm,ppc4xx-spi")) in of_gpio_spi_cs_get_count()
108 snprintf(propname, sizeof(propname), "%s-%s", in of_gpio_get_count()
114 ret = of_gpio_named_count(dev->of_node, propname); in of_gpio_get_count()
118 return ret ? ret : -ENOENT; in of_gpio_get_count()
125 return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) && in of_gpiochip_match_node_and_xlate()
126 chip->of_xlate && in of_gpiochip_match_node_and_xlate()
127 chip->of_xlate(chip, gpiospec, NULL) >= 0; in of_gpiochip_match_node_and_xlate()
142 if (chip->of_gpio_n_cells != gpiospec->args_count) in of_xlate_and_get_gpiod_flags()
143 return ERR_PTR(-EINVAL); in of_xlate_and_get_gpiod_flags()
145 ret = chip->of_xlate(chip, gpiospec, flags); in of_xlate_and_get_gpiod_flags()
153 * Overrides stated polarity of a gpio line and warns when there is a
162 pr_warn("%s GPIO handle specifies active low - ignored\n", in of_gpio_quirk_polarity()
168 pr_info("%s enforce active low on GPIO handle\n", in of_gpio_quirk_polarity()
190 * "gpios-reset" property and also specified wrong in of_gpio_try_fixup_polarity()
193 { "himax,hx8357", "gpios-reset", false }, in of_gpio_try_fixup_polarity()
194 { "himax,hx8369", "gpios-reset", false }, in of_gpio_try_fixup_polarity()
196 * The rb-gpios semantics was undocumented and qi,lb60 (along with in of_gpio_try_fixup_polarity()
199 * inverter on this board, it should be active-high. Let's fix that in of_gpio_try_fixup_polarity()
200 * here for older DTs so we can re-use the generic nand_gpio_waitrdy() in of_gpio_try_fixup_polarity()
203 { "qi,lb60", "rb-gpios", true }, in of_gpio_try_fixup_polarity()
230 { "fsl,imx25-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
231 { "fsl,imx27-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
232 { "fsl,imx28-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
233 { "fsl,imx6q-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
234 { "fsl,mvf600-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
235 { "fsl,imx6sx-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
236 { "fsl,imx6ul-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
237 { "fsl,imx8mq-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
238 { "fsl,imx8qm-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
239 { "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" }, in of_gpio_set_polarity_by_property()
242 { "fsl,imx6q-pcie", "reset-gpio", "reset-gpio-active-high" }, in of_gpio_set_polarity_by_property()
243 { "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" }, in of_gpio_set_polarity_by_property()
244 { "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" }, in of_gpio_set_polarity_by_property()
245 { "fsl,imx7d-pcie", "reset-gpio", "reset-gpio-active-high" }, in of_gpio_set_polarity_by_property()
246 { "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" }, in of_gpio_set_polarity_by_property()
247 { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" }, in of_gpio_set_polarity_by_property()
248 { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" }, in of_gpio_set_polarity_by_property()
252 * The regulator GPIO handles are specified such that the in of_gpio_set_polarity_by_property()
253 * presence or absence of "enable-active-high" solely controls in of_gpio_set_polarity_by_property()
254 * the polarity of the GPIO line. Any phandle flags must in of_gpio_set_polarity_by_property()
258 { "regulator-fixed", "gpios", "enable-active-high" }, in of_gpio_set_polarity_by_property()
259 { "regulator-fixed", "gpio", "enable-active-high" }, in of_gpio_set_polarity_by_property()
260 { "reg-fixed-voltage", "gpios", "enable-active-high" }, in of_gpio_set_polarity_by_property()
261 { "reg-fixed-voltage", "gpio", "enable-active-high" }, in of_gpio_set_polarity_by_property()
264 { "regulator-gpio", "enable-gpio", "enable-active-high" }, in of_gpio_set_polarity_by_property()
265 { "regulator-gpio", "enable-gpios", "enable-active-high" }, in of_gpio_set_polarity_by_property()
268 { "atmel,hsmci", "cd-gpios", "cd-inverted" }, in of_gpio_set_polarity_by_property()
277 * gpio property in a child node in of_gpio_set_polarity_by_property()
279 if (of_device_is_compatible(np->parent, "atmel,hsmci")) { in of_gpio_set_polarity_by_property()
280 np_compat = np->parent; in of_gpio_set_polarity_by_property()
308 of_device_is_compatible(np, "reg-fixed-voltage") && in of_gpio_flags_quirks()
309 of_property_read_bool(np, "gpio-open-drain")) { in of_gpio_flags_quirks()
311 pr_info("%s uses legacy open drain flag - update the DTS if you can\n", in of_gpio_flags_quirks()
317 * property named "cs-gpios" we need to inspect the child node in of_gpio_flags_quirks()
320 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && in of_gpio_flags_quirks()
321 of_property_read_bool(np, "cs-gpios")) { in of_gpio_flags_quirks()
323 u32 cs; in of_gpio_flags_quirks() local
327 ret = of_property_read_u32(child, "reg", &cs); in of_gpio_flags_quirks()
330 if (cs == index) { in of_gpio_flags_quirks()
334 * by just omitting "spi-cs-high" in the in of_gpio_flags_quirks()
339 * and has the "spi-cs-high" set, we get a in of_gpio_flags_quirks()
340 * conflict and the "spi-cs-high" flag will in of_gpio_flags_quirks()
344 "spi-cs-high"); in of_gpio_flags_quirks()
353 /* Legacy handling of stmmac's active-low PHY reset line */ in of_gpio_flags_quirks()
355 !strcmp(propname, "snps,reset-gpio") && in of_gpio_flags_quirks()
356 of_property_read_bool(np, "snps,reset-active-low")) in of_gpio_flags_quirks()
361 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
362 * @np: device node to get GPIO from
363 * @propname: property name containing gpio specifier(s)
364 * @index: index of the GPIO
367 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
369 * in flags for the GPIO.
378 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, in of_get_named_gpiod_flags()
389 desc = ERR_PTR(-EPROBE_DEFER); in of_get_named_gpiod_flags()
401 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", in of_get_named_gpiod_flags()
412 * of_get_named_gpio() - Get a GPIO number to use with GPIO API
413 * @np: device node to get GPIO from
414 * @propname: Name of property containing gpio specifier(s)
415 * @index: index of the GPIO
417 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
471 const char *legacy_id; /* NULL - same as con_id */ in of_find_gpio_rename()
475 * but it should only be done for gpio names that have in of_find_gpio_rename()
482 /* Himax LCD controllers used "gpios-reset" */ in of_find_gpio_rename()
483 { "reset", "gpios-reset", "himax,hx8357" }, in of_find_gpio_rename()
484 { "reset", "gpios-reset", "himax,hx8369" }, in of_find_gpio_rename()
490 { "rtc-data", "gpio-rtc-data", "moxa,moxart-rtc" }, in of_find_gpio_rename()
491 { "rtc-sclk", "gpio-rtc-sclk", "moxa,moxart-rtc" }, in of_find_gpio_rename()
492 { "rtc-reset", "gpio-rtc-reset", "moxa,moxart-rtc" }, in of_find_gpio_rename()
495 { "reset", "reset-n-io", "marvell,nfc-i2c" }, in of_find_gpio_rename()
498 { "reset", "reset-n-io", "marvell,nfc-spi" }, in of_find_gpio_rename()
501 { "reset", "reset-n-io", "marvell,nfc-uart" }, in of_find_gpio_rename()
502 { "reset", "reset-n-io", "mrvl,nfc-uart" }, in of_find_gpio_rename()
506 { "reset", "gpios-reset", "lantiq,pci-xway" }, in of_find_gpio_rename()
511 * establish that GPIO properties should be named in of_find_gpio_rename()
512 * "foo-gpios" so we have this special kludge for them. in of_find_gpio_rename()
523 { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" }, in of_find_gpio_rename()
526 { "i2s1-in-sel-gpio1", NULL, "mediatek,mt2701-cs42448-machine" }, in of_find_gpio_rename()
527 { "i2s1-in-sel-gpio2", NULL, "mediatek,mt2701-cs42448-machine" }, in of_find_gpio_rename()
530 { "reset", "gpio-reset", "ti,tlv320aic3x" }, in of_find_gpio_rename()
531 { "reset", "gpio-reset", "ti,tlv320aic33" }, in of_find_gpio_rename()
532 { "reset", "gpio-reset", "ti,tlv320aic3007" }, in of_find_gpio_rename()
533 { "reset", "gpio-reset", "ti,tlv320aic3104" }, in of_find_gpio_rename()
534 { "reset", "gpio-reset", "ti,tlv320aic3106" }, in of_find_gpio_rename()
538 * The SPI GPIO bindings happened before we managed to in of_find_gpio_rename()
539 * establish that GPIO properties should be named in of_find_gpio_rename()
540 * "foo-gpios" so we have this special kludge for them. in of_find_gpio_rename()
542 { "miso", "gpio-miso", "spi-gpio" }, in of_find_gpio_rename()
543 { "mosi", "gpio-mosi", "spi-gpio" }, in of_find_gpio_rename()
544 { "sck", "gpio-sck", "spi-gpio" }, in of_find_gpio_rename()
549 * for the chip select lines rather than "cs-gpios" like in of_find_gpio_rename()
554 { "cs", "gpios", "fsl,spi" }, in of_find_gpio_rename()
555 { "cs", "gpios", "aeroflexgaisler,spictrl" }, in of_find_gpio_rename()
558 { "cs", "gpios", "ibm,ppc4xx-spi" }, in of_find_gpio_rename()
564 * property without the compulsory "-gpios" suffix. in of_find_gpio_rename()
574 return ERR_PTR(-ENOENT); in of_find_gpio_rename()
587 pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n", in of_find_gpio_rename()
593 return ERR_PTR(-ENOENT); in of_find_gpio_rename()
605 return ERR_PTR(-ENOENT); in of_find_mt2701_gpio()
607 if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine")) in of_find_mt2701_gpio()
608 return ERR_PTR(-ENOENT); in of_find_mt2701_gpio()
610 if (!con_id || strcmp(con_id, "i2s1-in-sel")) in of_find_mt2701_gpio()
611 return ERR_PTR(-ENOENT); in of_find_mt2701_gpio()
614 legacy_id = "i2s1-in-sel-gpio1"; in of_find_mt2701_gpio()
616 legacy_id = "i2s1-in-sel-gpio2"; in of_find_mt2701_gpio()
618 return ERR_PTR(-ENOENT); in of_find_mt2701_gpio()
622 pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n", in of_find_mt2701_gpio()
629 * Trigger sources are special, they allow us to use any GPIO as a LED trigger
630 * and have the name "trigger-sources" no matter which kind of phandle it is
631 * pointing to, whether to a GPIO, a USB host, a network PHY etc. So in this case
632 * we allow looking something up that is not named "foo-gpios".
642 return ERR_PTR(-ENOENT); in of_find_trigger_gpio()
644 if (!con_id || strcmp(con_id, "trigger-sources")) in of_find_trigger_gpio()
645 return ERR_PTR(-ENOENT); in of_find_trigger_gpio()
675 /* Try GPIO property "foo-gpios" and "foo-gpio" */ in of_find_gpio()
678 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, in of_find_gpio()
690 /* Properly named GPIO was not found, try workarounds */ in of_find_gpio()
703 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
704 * @np: device node to get GPIO from
705 * @chip: GPIO chip whose hog is parsed
706 * @idx: Index of the GPIO to parse
707 * @name: GPIO line name
708 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
710 * @dflags: gpiod_flags - optional GPIO initialization flags
712 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
729 chip_np = dev_of_node(&chip->gpiodev->dev); in of_parse_own_gpio()
731 return ERR_PTR(-EINVAL); in of_parse_own_gpio()
737 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); in of_parse_own_gpio()
759 else if (of_property_read_bool(np, "output-low")) in of_parse_own_gpio()
761 else if (of_property_read_bool(np, "output-high")) in of_parse_own_gpio()
764 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", in of_parse_own_gpio()
766 return ERR_PTR(-EINVAL); in of_parse_own_gpio()
769 if (name && of_property_read_string(np, "line-name", name)) in of_parse_own_gpio()
770 *name = np->name; in of_parse_own_gpio()
776 * of_gpiochip_add_hog - Add all hogs in a hog device node
777 * @chip: gpio chip to act on
801 desc->hog = hog; in of_gpiochip_add_hog()
809 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
810 * @chip: gpio chip to act on
812 * This is only used by of_gpiochip_add to request/set GPIO initial
821 for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) { in of_gpiochip_scan_gpios()
822 if (!of_property_read_bool(np, "gpio-hog")) in of_gpiochip_scan_gpios()
839 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
840 * @chip: gpio chip to act on
849 if (desc->hog == hog) in of_gpiochip_remove_hog()
855 return device_match_of_node(&chip->gpiodev->dev, data); in of_gpiochip_match_node()
871 * This only supports adding and removing complete gpio-hog nodes. in of_gpio_notify()
872 * Modifying an existing gpio-hog node is not supported (except for in of_gpio_notify()
878 if (!of_property_read_bool(rd->dn, "gpio-hog")) in of_gpio_notify()
881 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) in of_gpio_notify()
884 gdev = of_find_gpio_device_by_node(rd->dn->parent); in of_gpio_notify()
888 ret = of_gpiochip_add_hog(gpio_device_get_chip(gdev), rd->dn); in of_gpio_notify()
891 rd->dn); in of_gpio_notify()
892 of_node_clear_flag(rd->dn, OF_POPULATED); in of_gpio_notify()
898 if (!of_node_check_flag(rd->dn, OF_POPULATED)) in of_gpio_notify()
901 gdev = of_find_gpio_device_by_node(rd->dn->parent); in of_gpio_notify()
905 of_gpiochip_remove_hog(gpio_device_get_chip(gdev), rd->dn); in of_gpio_notify()
906 of_node_clear_flag(rd->dn, OF_POPULATED); in of_gpio_notify()
919 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
921 * @gpiospec: GPIO specifier as found in the device tree
925 * GPIO chips. This function performs only one sanity check: whether GPIO
934 * write your own xlate function (that will have to retrieve the GPIO in of_gpio_simple_xlate()
935 * number and the flags from a single gpio cell -- this is possible, in of_gpio_simple_xlate()
938 if (gc->of_gpio_n_cells < 2) { in of_gpio_simple_xlate()
940 return -EINVAL; in of_gpio_simple_xlate()
943 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) in of_gpio_simple_xlate()
944 return -EINVAL; in of_gpio_simple_xlate()
946 if (gpiospec->args[0] >= gc->ngpio) in of_gpio_simple_xlate()
947 return -EINVAL; in of_gpio_simple_xlate()
950 *flags = gpiospec->args[1]; in of_gpio_simple_xlate()
952 return gpiospec->args[0]; in of_gpio_simple_xlate()
956 #include <linux/gpio/legacy-of-mm-gpiochip.h>
958 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
959 * @np: device node of the GPIO chip
966 * - all the callbacks
967 * - of_gpio_n_cells
968 * - of_xlate callback (optional)
971 * - save_regs callback (optional)
981 int ret = -ENOMEM; in of_mm_gpiochip_add_data()
982 struct gpio_chip *gc = &mm_gc->gc; in of_mm_gpiochip_add_data()
984 gc->label = kasprintf(GFP_KERNEL, "%pOF", np); in of_mm_gpiochip_add_data()
985 if (!gc->label) in of_mm_gpiochip_add_data()
988 mm_gc->regs = of_iomap(np, 0); in of_mm_gpiochip_add_data()
989 if (!mm_gc->regs) in of_mm_gpiochip_add_data()
992 gc->base = -1; in of_mm_gpiochip_add_data()
994 if (mm_gc->save_regs) in of_mm_gpiochip_add_data()
995 mm_gc->save_regs(mm_gc); in of_mm_gpiochip_add_data()
997 fwnode_handle_put(mm_gc->gc.fwnode); in of_mm_gpiochip_add_data()
998 mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np)); in of_mm_gpiochip_add_data()
1007 iounmap(mm_gc->regs); in of_mm_gpiochip_add_data()
1009 kfree(gc->label); in of_mm_gpiochip_add_data()
1011 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); in of_mm_gpiochip_add_data()
1017 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
1022 struct gpio_chip *gc = &mm_gc->gc; in of_mm_gpiochip_remove()
1025 iounmap(mm_gc->regs); in of_mm_gpiochip_remove()
1026 kfree(gc->label); in of_mm_gpiochip_remove()
1039 static const char group_names_propname[] = "gpio-ranges-group-names"; in of_gpiochip_add_pin_range()
1042 np = dev_of_node(&chip->gpiodev->dev); in of_gpiochip_add_pin_range()
1049 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, in of_gpiochip_add_pin_range()
1057 return -EPROBE_DEFER; in of_gpiochip_add_pin_range()
1065 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", in of_gpiochip_add_pin_range()
1081 pr_err("%pOF: Illegal gpio-range format.\n", in of_gpiochip_add_pin_range()
1087 pr_err("%pOF: GPIO group range requested but no %s property.\n", in of_gpiochip_add_pin_range()
1099 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", in of_gpiochip_add_pin_range()
1123 np = dev_of_node(&chip->gpiodev->dev); in of_gpiochip_add()
1127 if (!chip->of_xlate) { in of_gpiochip_add()
1128 chip->of_gpio_n_cells = 2; in of_gpiochip_add()
1129 chip->of_xlate = of_gpio_simple_xlate; in of_gpiochip_add()
1132 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) in of_gpiochip_add()
1133 return -EINVAL; in of_gpiochip_add()
1150 of_node_put(dev_of_node(&chip->gpiodev->dev)); in of_gpiochip_remove()