Vladimir Zapolskiy | 27038c3 | 2018-11-22 15:59:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 2 | /* |
| 3 | * OF helpers for the GPIO API |
| 4 | * |
| 5 | * Copyright (c) 2007-2008 MontaVista Software, Inc. |
| 6 | * |
| 7 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 8 | */ |
| 9 | |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 10 | #include <linux/device.h> |
Sachin Kamat | bea4dbe | 2014-01-27 12:15:08 +0530 | [diff] [blame] | 11 | #include <linux/err.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 12 | #include <linux/errno.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 13 | #include <linux/module.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 14 | #include <linux/io.h> |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 15 | #include <linux/gpio/consumer.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 16 | #include <linux/of.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 17 | #include <linux/of_address.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 18 | #include <linux/of_gpio.h> |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 19 | #include <linux/pinctrl/pinctrl.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 20 | #include <linux/slab.h> |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 21 | #include <linux/gpio/machine.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 22 | |
Alexandre Courbot | 1bd6b60 | 2014-07-22 16:17:41 +0900 | [diff] [blame] | 23 | #include "gpiolib.h" |
Linus Walleij | f626d6d | 2019-07-17 09:10:01 +0200 | [diff] [blame] | 24 | #include "gpiolib-of.h" |
| 25 | |
Andy Shevchenko | eed5a3b | 2023-01-12 16:51:40 +0200 | [diff] [blame] | 26 | /* |
| 27 | * This is Linux-specific flags. By default controllers' and Linux' mapping |
| 28 | * match, but GPIO controllers are free to translate their own flags to |
| 29 | * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended. |
| 30 | */ |
| 31 | enum of_gpio_flags { |
| 32 | OF_GPIO_ACTIVE_LOW = 0x1, |
| 33 | OF_GPIO_SINGLE_ENDED = 0x2, |
| 34 | OF_GPIO_OPEN_DRAIN = 0x4, |
| 35 | OF_GPIO_TRANSITORY = 0x8, |
| 36 | OF_GPIO_PULL_UP = 0x10, |
| 37 | OF_GPIO_PULL_DOWN = 0x20, |
| 38 | OF_GPIO_PULL_DISABLE = 0x40, |
| 39 | }; |
| 40 | |
Linus Walleij | 71b8f60 | 2019-11-28 09:37:17 +0100 | [diff] [blame] | 41 | /** |
Dmitry Torokhov | c783565 | 2022-12-19 11:20:13 -0800 | [diff] [blame] | 42 | * of_gpio_named_count() - Count GPIOs for a device |
| 43 | * @np: device node to count GPIOs for |
| 44 | * @propname: property name containing gpio specifier(s) |
| 45 | * |
| 46 | * The function returns the count of GPIOs specified for a node. |
| 47 | * Note that the empty GPIO specifiers count too. Returns either |
| 48 | * Number of gpios defined in property, |
| 49 | * -EINVAL for an incorrectly formed gpios property, or |
| 50 | * -ENOENT for a missing gpios property |
| 51 | * |
| 52 | * Example: |
| 53 | * gpios = <0 |
| 54 | * &gpio1 1 2 |
| 55 | * 0 |
| 56 | * &gpio2 3 4>; |
| 57 | * |
| 58 | * The above example defines four GPIOs, two of which are not specified. |
| 59 | * This function will return '4' |
| 60 | */ |
| 61 | static int of_gpio_named_count(const struct device_node *np, |
| 62 | const char *propname) |
| 63 | { |
| 64 | return of_count_phandle_with_args(np, propname, "#gpio-cells"); |
| 65 | } |
| 66 | |
| 67 | /** |
Linus Walleij | 71b8f60 | 2019-11-28 09:37:17 +0100 | [diff] [blame] | 68 | * of_gpio_spi_cs_get_count() - special GPIO counting for SPI |
Lee Jones | c5a66b9 | 2020-06-30 14:33:36 +0100 | [diff] [blame] | 69 | * @dev: Consuming device |
| 70 | * @con_id: Function within the GPIO consumer |
| 71 | * |
Linus Walleij | 71b8f60 | 2019-11-28 09:37:17 +0100 | [diff] [blame] | 72 | * Some elder GPIO controllers need special quirks. Currently we handle |
Linus Walleij | 4726773 | 2020-07-14 09:22:26 +0200 | [diff] [blame] | 73 | * the Freescale and PPC GPIO controller with bindings that doesn't use the |
Linus Walleij | 71b8f60 | 2019-11-28 09:37:17 +0100 | [diff] [blame] | 74 | * established "cs-gpios" for chip selects but instead rely on |
| 75 | * "gpios" for the chip select lines. If we detect this, we redirect |
| 76 | * the counting of "cs-gpios" to count "gpios" transparent to the |
| 77 | * driver. |
| 78 | */ |
YueHaibing | a1f4c96 | 2019-11-30 09:28:28 +0800 | [diff] [blame] | 79 | static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id) |
Linus Walleij | 71b8f60 | 2019-11-28 09:37:17 +0100 | [diff] [blame] | 80 | { |
| 81 | struct device_node *np = dev->of_node; |
| 82 | |
| 83 | if (!IS_ENABLED(CONFIG_SPI_MASTER)) |
| 84 | return 0; |
| 85 | if (!con_id || strcmp(con_id, "cs")) |
| 86 | return 0; |
| 87 | if (!of_device_is_compatible(np, "fsl,spi") && |
Linus Walleij | 4726773 | 2020-07-14 09:22:26 +0200 | [diff] [blame] | 88 | !of_device_is_compatible(np, "aeroflexgaisler,spictrl") && |
| 89 | !of_device_is_compatible(np, "ibm,ppc4xx-spi")) |
Linus Walleij | 71b8f60 | 2019-11-28 09:37:17 +0100 | [diff] [blame] | 90 | return 0; |
| 91 | return of_gpio_named_count(np, "gpios"); |
| 92 | } |
| 93 | |
Linus Walleij | f626d6d | 2019-07-17 09:10:01 +0200 | [diff] [blame] | 94 | int of_gpio_get_count(struct device *dev, const char *con_id) |
| 95 | { |
| 96 | int ret; |
| 97 | char propname[32]; |
| 98 | unsigned int i; |
| 99 | |
Linus Walleij | 71b8f60 | 2019-11-28 09:37:17 +0100 | [diff] [blame] | 100 | ret = of_gpio_spi_cs_get_count(dev, con_id); |
| 101 | if (ret > 0) |
| 102 | return ret; |
| 103 | |
Linus Walleij | f626d6d | 2019-07-17 09:10:01 +0200 | [diff] [blame] | 104 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
| 105 | if (con_id) |
| 106 | snprintf(propname, sizeof(propname), "%s-%s", |
| 107 | con_id, gpio_suffixes[i]); |
| 108 | else |
| 109 | snprintf(propname, sizeof(propname), "%s", |
| 110 | gpio_suffixes[i]); |
| 111 | |
| 112 | ret = of_gpio_named_count(dev->of_node, propname); |
| 113 | if (ret > 0) |
| 114 | break; |
| 115 | } |
| 116 | return ret ? ret : -ENOENT; |
| 117 | } |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 118 | |
Masahiro Yamada | c7e9d39 | 2016-10-25 10:47:44 +0900 | [diff] [blame] | 119 | static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) |
Grant Likely | 3d0f7cf0 | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 120 | { |
Masahiro Yamada | c7e9d39 | 2016-10-25 10:47:44 +0900 | [diff] [blame] | 121 | struct of_phandle_args *gpiospec = data; |
| 122 | |
Andy Shevchenko | d59fdbc | 2022-11-03 22:30:05 +0200 | [diff] [blame] | 123 | return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) && |
Vincent Whitchurch | d49b48f | 2018-08-31 09:04:18 +0200 | [diff] [blame] | 124 | chip->of_xlate && |
Masahiro Yamada | c7e9d39 | 2016-10-25 10:47:44 +0900 | [diff] [blame] | 125 | chip->of_xlate(chip, gpiospec, NULL) >= 0; |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 126 | } |
Grant Likely | 3d0f7cf0 | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 127 | |
Masahiro Yamada | c7e9d39 | 2016-10-25 10:47:44 +0900 | [diff] [blame] | 128 | static struct gpio_chip *of_find_gpiochip_by_xlate( |
| 129 | struct of_phandle_args *gpiospec) |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 130 | { |
Masahiro Yamada | c7e9d39 | 2016-10-25 10:47:44 +0900 | [diff] [blame] | 131 | return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); |
Grant Likely | 3d0f7cf0 | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 132 | } |
| 133 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 134 | static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, |
| 135 | struct of_phandle_args *gpiospec, |
| 136 | enum of_gpio_flags *flags) |
| 137 | { |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 138 | int ret; |
| 139 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 140 | if (chip->of_gpio_n_cells != gpiospec->args_count) |
| 141 | return ERR_PTR(-EINVAL); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 142 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 143 | ret = chip->of_xlate(chip, gpiospec, flags); |
| 144 | if (ret < 0) |
| 145 | return ERR_PTR(ret); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 146 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 147 | return gpiochip_get_desc(chip, ret); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 148 | } |
| 149 | |
Dmitry Torokhov | e3186e3 | 2022-10-17 22:41:09 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Overrides stated polarity of a gpio line and warns when there is a |
| 152 | * discrepancy. |
| 153 | */ |
| 154 | static void of_gpio_quirk_polarity(const struct device_node *np, |
| 155 | bool active_high, |
| 156 | enum of_gpio_flags *flags) |
| 157 | { |
| 158 | if (active_high) { |
| 159 | if (*flags & OF_GPIO_ACTIVE_LOW) { |
| 160 | pr_warn("%s GPIO handle specifies active low - ignored\n", |
| 161 | of_node_full_name(np)); |
| 162 | *flags &= ~OF_GPIO_ACTIVE_LOW; |
| 163 | } |
| 164 | } else { |
| 165 | if (!(*flags & OF_GPIO_ACTIVE_LOW)) |
| 166 | pr_info("%s enforce active low on GPIO handle\n", |
| 167 | of_node_full_name(np)); |
| 168 | *flags |= OF_GPIO_ACTIVE_LOW; |
| 169 | } |
| 170 | } |
| 171 | |
Dmitry Torokhov | 99d18d4 | 2022-10-17 22:41:11 -0700 | [diff] [blame] | 172 | /* |
| 173 | * This quirk does static polarity overrides in cases where existing |
| 174 | * DTS specified incorrect polarity. |
| 175 | */ |
| 176 | static void of_gpio_try_fixup_polarity(const struct device_node *np, |
| 177 | const char *propname, |
| 178 | enum of_gpio_flags *flags) |
| 179 | { |
| 180 | static const struct { |
| 181 | const char *compatible; |
| 182 | const char *propname; |
| 183 | bool active_high; |
| 184 | } gpios[] = { |
| 185 | #if !IS_ENABLED(CONFIG_LCD_HX8357) |
| 186 | /* |
| 187 | * Himax LCD controllers used incorrectly named |
| 188 | * "gpios-reset" property and also specified wrong |
| 189 | * polarity. |
| 190 | */ |
| 191 | { "himax,hx8357", "gpios-reset", false }, |
| 192 | { "himax,hx8369", "gpios-reset", false }, |
| 193 | #endif |
| 194 | }; |
| 195 | unsigned int i; |
| 196 | |
| 197 | for (i = 0; i < ARRAY_SIZE(gpios); i++) { |
| 198 | if (of_device_is_compatible(np, gpios[i].compatible) && |
| 199 | !strcmp(propname, gpios[i].propname)) { |
| 200 | of_gpio_quirk_polarity(np, gpios[i].active_high, flags); |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Dmitry Torokhov | 34cb935 | 2022-10-29 21:40:46 -0700 | [diff] [blame] | 206 | static void of_gpio_set_polarity_by_property(const struct device_node *np, |
| 207 | const char *propname, |
| 208 | enum of_gpio_flags *flags) |
Linus Walleij | a603a2b | 2017-12-30 16:26:36 +0100 | [diff] [blame] | 209 | { |
Dmitry Torokhov | 34cb935 | 2022-10-29 21:40:46 -0700 | [diff] [blame] | 210 | static const struct { |
| 211 | const char *compatible; |
| 212 | const char *gpio_propname; |
| 213 | const char *polarity_propname; |
| 214 | } gpios[] = { |
| 215 | #if IS_ENABLED(CONFIG_FEC) |
| 216 | /* Freescale Fast Ethernet Controller */ |
| 217 | { "fsl,imx25-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 218 | { "fsl,imx27-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 219 | { "fsl,imx28-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 220 | { "fsl,imx6q-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 221 | { "fsl,mvf600-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 222 | { "fsl,imx6sx-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 223 | { "fsl,imx6ul-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 224 | { "fsl,imx8mq-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 225 | { "fsl,imx8qm-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 226 | { "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" }, |
| 227 | #endif |
Dmitry Torokhov | b8b8034 | 2022-10-29 21:40:47 -0700 | [diff] [blame] | 228 | #if IS_ENABLED(CONFIG_PCI_IMX6) |
| 229 | { "fsl,imx6q-pcie", "reset-gpio", "reset-gpio-active-high" }, |
| 230 | { "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" }, |
| 231 | { "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" }, |
| 232 | { "fsl,imx7d-pcie", "reset-gpio", "reset-gpio-active-high" }, |
| 233 | { "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" }, |
| 234 | { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" }, |
| 235 | { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" }, |
| 236 | #endif |
Dmitry Torokhov | 99d18d4 | 2022-10-17 22:41:11 -0700 | [diff] [blame] | 237 | |
Linus Walleij | a603a2b | 2017-12-30 16:26:36 +0100 | [diff] [blame] | 238 | /* |
| 239 | * The regulator GPIO handles are specified such that the |
| 240 | * presence or absence of "enable-active-high" solely controls |
| 241 | * the polarity of the GPIO line. Any phandle flags must |
| 242 | * be actively ignored. |
| 243 | */ |
Dmitry Torokhov | 34cb935 | 2022-10-29 21:40:46 -0700 | [diff] [blame] | 244 | #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE) |
| 245 | { "regulator-fixed", "gpios", "enable-active-high" }, |
| 246 | { "regulator-fixed", "gpio", "enable-active-high" }, |
| 247 | { "reg-fixed-voltage", "gpios", "enable-active-high" }, |
| 248 | { "reg-fixed-voltage", "gpio", "enable-active-high" }, |
| 249 | #endif |
| 250 | #if IS_ENABLED(CONFIG_REGULATOR_GPIO) |
| 251 | { "regulator-gpio", "enable-gpio", "enable-active-high" }, |
| 252 | { "regulator-gpio", "enable-gpios", "enable-active-high" }, |
| 253 | #endif |
| 254 | }; |
| 255 | unsigned int i; |
| 256 | bool active_high; |
| 257 | |
| 258 | for (i = 0; i < ARRAY_SIZE(gpios); i++) { |
| 259 | if (of_device_is_compatible(np, gpios[i].compatible) && |
| 260 | !strcmp(propname, gpios[i].gpio_propname)) { |
| 261 | active_high = of_property_read_bool(np, |
| 262 | gpios[i].polarity_propname); |
| 263 | of_gpio_quirk_polarity(np, active_high, flags); |
| 264 | break; |
| 265 | } |
Linus Walleij | a603a2b | 2017-12-30 16:26:36 +0100 | [diff] [blame] | 266 | } |
Dmitry Torokhov | 34cb935 | 2022-10-29 21:40:46 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void of_gpio_flags_quirks(const struct device_node *np, |
| 270 | const char *propname, |
| 271 | enum of_gpio_flags *flags, |
| 272 | int index) |
| 273 | { |
| 274 | of_gpio_try_fixup_polarity(np, propname, flags); |
| 275 | of_gpio_set_polarity_by_property(np, propname, flags); |
| 276 | |
Linus Walleij | a603a2b | 2017-12-30 16:26:36 +0100 | [diff] [blame] | 277 | /* |
| 278 | * Legacy open drain handling for fixed voltage regulators. |
| 279 | */ |
| 280 | if (IS_ENABLED(CONFIG_REGULATOR) && |
| 281 | of_device_is_compatible(np, "reg-fixed-voltage") && |
| 282 | of_property_read_bool(np, "gpio-open-drain")) { |
| 283 | *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); |
| 284 | pr_info("%s uses legacy open drain flag - update the DTS if you can\n", |
| 285 | of_node_full_name(np)); |
| 286 | } |
Linus Walleij | 6953c57 | 2018-09-04 11:01:58 +0200 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * Legacy handling of SPI active high chip select. If we have a |
| 290 | * property named "cs-gpios" we need to inspect the child node |
| 291 | * to determine if the flags should have inverted semantics. |
| 292 | */ |
Linus Walleij | da7f134 | 2019-07-15 22:45:29 +0200 | [diff] [blame] | 293 | if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && |
Geert Uytterhoeven | a71a81e | 2019-03-29 09:42:29 +0100 | [diff] [blame] | 294 | of_property_read_bool(np, "cs-gpios")) { |
Linus Walleij | 6953c57 | 2018-09-04 11:01:58 +0200 | [diff] [blame] | 295 | struct device_node *child; |
| 296 | u32 cs; |
| 297 | int ret; |
| 298 | |
| 299 | for_each_child_of_node(np, child) { |
| 300 | ret = of_property_read_u32(child, "reg", &cs); |
Linus Walleij | c1c04ce | 2019-01-16 09:21:10 +0100 | [diff] [blame] | 301 | if (ret) |
Linus Walleij | 6953c57 | 2018-09-04 11:01:58 +0200 | [diff] [blame] | 302 | continue; |
| 303 | if (cs == index) { |
| 304 | /* |
| 305 | * SPI children have active low chip selects |
| 306 | * by default. This can be specified negatively |
| 307 | * by just omitting "spi-cs-high" in the |
| 308 | * device node, or actively by tagging on |
| 309 | * GPIO_ACTIVE_LOW as flag in the device |
| 310 | * tree. If the line is simultaneously |
| 311 | * tagged as active low in the device tree |
| 312 | * and has the "spi-cs-high" set, we get a |
| 313 | * conflict and the "spi-cs-high" flag will |
| 314 | * take precedence. |
| 315 | */ |
Dmitry Torokhov | e3186e3 | 2022-10-17 22:41:09 -0700 | [diff] [blame] | 316 | bool active_high = of_property_read_bool(child, |
| 317 | "spi-cs-high"); |
| 318 | of_gpio_quirk_polarity(child, active_high, |
| 319 | flags); |
Nishka Dasgupta | 89fea04 | 2019-07-06 19:04:22 +0530 | [diff] [blame] | 320 | of_node_put(child); |
Linus Walleij | 6953c57 | 2018-09-04 11:01:58 +0200 | [diff] [blame] | 321 | break; |
| 322 | } |
| 323 | } |
| 324 | } |
Martin Blumenstingl | edc1ef3 | 2019-06-10 19:05:23 +0200 | [diff] [blame] | 325 | |
| 326 | /* Legacy handling of stmmac's active-low PHY reset line */ |
| 327 | if (IS_ENABLED(CONFIG_STMMAC_ETH) && |
| 328 | !strcmp(propname, "snps,reset-gpio") && |
| 329 | of_property_read_bool(np, "snps,reset-active-low")) |
| 330 | *flags |= OF_GPIO_ACTIVE_LOW; |
Linus Walleij | a603a2b | 2017-12-30 16:26:36 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 333 | /** |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 334 | * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 335 | * @np: device node to get GPIO from |
John Bonesio | a6b0919 | 2011-06-27 16:49:57 -0700 | [diff] [blame] | 336 | * @propname: property name containing gpio specifier(s) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 337 | * @index: index of the GPIO |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 338 | * @flags: a flags pointer to fill in |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 339 | * |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 340 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 341 | * value on the error condition. If @flags is not NULL the function also fills |
| 342 | * in flags for the GPIO. |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 343 | */ |
Krzysztof Kozlowski | e6ae9a8 | 2021-07-28 16:42:28 +0200 | [diff] [blame] | 344 | static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np, |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 345 | const char *propname, int index, enum of_gpio_flags *flags) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 346 | { |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 347 | struct of_phandle_args gpiospec; |
| 348 | struct gpio_chip *chip; |
| 349 | struct gpio_desc *desc; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 350 | int ret; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 351 | |
Stephen Boyd | c11e6f0 | 2018-01-30 18:36:18 -0800 | [diff] [blame] | 352 | ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, |
| 353 | &gpiospec); |
Grant Likely | 3d0f7cf0 | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 354 | if (ret) { |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 355 | pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", |
| 356 | __func__, propname, np, index); |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 357 | return ERR_PTR(ret); |
Grant Likely | 3d0f7cf0 | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 358 | } |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 359 | |
Masahiro Yamada | c7e9d39 | 2016-10-25 10:47:44 +0900 | [diff] [blame] | 360 | chip = of_find_gpiochip_by_xlate(&gpiospec); |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 361 | if (!chip) { |
| 362 | desc = ERR_PTR(-EPROBE_DEFER); |
| 363 | goto out; |
| 364 | } |
Grant Likely | 3d0f7cf0 | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 365 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 366 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 367 | if (IS_ERR(desc)) |
| 368 | goto out; |
| 369 | |
Linus Walleij | 605f2d3 | 2018-01-16 23:44:46 +0100 | [diff] [blame] | 370 | if (flags) |
Linus Walleij | 89a5e15 | 2018-12-17 22:36:25 +0100 | [diff] [blame] | 371 | of_gpio_flags_quirks(np, propname, flags, index); |
Linus Walleij | a603a2b | 2017-12-30 16:26:36 +0100 | [diff] [blame] | 372 | |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 373 | pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", |
| 374 | __func__, propname, np, index, |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 375 | PTR_ERR_OR_ZERO(desc)); |
| 376 | |
| 377 | out: |
| 378 | of_node_put(gpiospec.np); |
| 379 | |
| 380 | return desc; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 381 | } |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 382 | |
Dmitry Torokhov | 40fc56e | 2022-12-19 11:20:15 -0800 | [diff] [blame] | 383 | /** |
| 384 | * of_get_named_gpio() - Get a GPIO number to use with GPIO API |
| 385 | * @np: device node to get GPIO from |
| 386 | * @propname: Name of property containing gpio specifier(s) |
| 387 | * @index: index of the GPIO |
| 388 | * |
| 389 | * Returns GPIO number to use with Linux generic GPIO API, or one of the errno |
| 390 | * value on the error condition. |
| 391 | */ |
| 392 | int of_get_named_gpio(const struct device_node *np, const char *propname, |
| 393 | int index) |
Alexandre Courbot | f01d907 | 2014-05-17 14:54:50 +0900 | [diff] [blame] | 394 | { |
| 395 | struct gpio_desc *desc; |
| 396 | |
Dmitry Torokhov | 40fc56e | 2022-12-19 11:20:15 -0800 | [diff] [blame] | 397 | desc = of_get_named_gpiod_flags(np, propname, index, NULL); |
Alexandre Courbot | f01d907 | 2014-05-17 14:54:50 +0900 | [diff] [blame] | 398 | |
| 399 | if (IS_ERR(desc)) |
| 400 | return PTR_ERR(desc); |
| 401 | else |
| 402 | return desc_to_gpio(desc); |
| 403 | } |
Dmitry Torokhov | 40fc56e | 2022-12-19 11:20:15 -0800 | [diff] [blame] | 404 | EXPORT_SYMBOL_GPL(of_get_named_gpio); |
Alexandre Courbot | f01d907 | 2014-05-17 14:54:50 +0900 | [diff] [blame] | 405 | |
Dmitry Torokhov | d9e7f0e | 2022-09-07 22:39:49 -0700 | [diff] [blame] | 406 | /* Converts gpio_lookup_flags into bitmask of GPIO_* values */ |
| 407 | static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags) |
| 408 | { |
| 409 | unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
| 410 | |
| 411 | if (flags & OF_GPIO_ACTIVE_LOW) |
| 412 | lflags |= GPIO_ACTIVE_LOW; |
| 413 | |
| 414 | if (flags & OF_GPIO_SINGLE_ENDED) { |
| 415 | if (flags & OF_GPIO_OPEN_DRAIN) |
| 416 | lflags |= GPIO_OPEN_DRAIN; |
| 417 | else |
| 418 | lflags |= GPIO_OPEN_SOURCE; |
| 419 | } |
| 420 | |
| 421 | if (flags & OF_GPIO_TRANSITORY) |
| 422 | lflags |= GPIO_TRANSITORY; |
| 423 | |
| 424 | if (flags & OF_GPIO_PULL_UP) |
| 425 | lflags |= GPIO_PULL_UP; |
| 426 | |
| 427 | if (flags & OF_GPIO_PULL_DOWN) |
| 428 | lflags |= GPIO_PULL_DOWN; |
| 429 | |
| 430 | if (flags & OF_GPIO_PULL_DISABLE) |
| 431 | lflags |= GPIO_PULL_DISABLE; |
| 432 | |
| 433 | return lflags; |
| 434 | } |
| 435 | |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 436 | static struct gpio_desc *of_find_gpio_rename(struct device_node *np, |
Linus Walleij | e3023bf | 2018-12-13 15:22:47 +0100 | [diff] [blame] | 437 | const char *con_id, |
| 438 | unsigned int idx, |
Dmitry Torokhov | 984914e | 2022-09-07 22:39:47 -0700 | [diff] [blame] | 439 | enum of_gpio_flags *of_flags) |
Linus Walleij | e3023bf | 2018-12-13 15:22:47 +0100 | [diff] [blame] | 440 | { |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 441 | static const struct of_rename_gpio { |
| 442 | const char *con_id; |
| 443 | const char *legacy_id; /* NULL - same as con_id */ |
| 444 | /* |
| 445 | * Compatible string can be set to NULL in case where |
| 446 | * matching to a particular compatible is not practical, |
| 447 | * but it should only be done for gpio names that have |
| 448 | * vendor prefix to reduce risk of false positives. |
| 449 | * Addition of such entries is strongly discouraged. |
| 450 | */ |
| 451 | const char *compatible; |
| 452 | } gpios[] = { |
Dmitry Torokhov | fbbbcd1 | 2022-10-17 22:41:05 -0700 | [diff] [blame] | 453 | #if !IS_ENABLED(CONFIG_LCD_HX8357) |
| 454 | /* Himax LCD controllers used "gpios-reset" */ |
| 455 | { "reset", "gpios-reset", "himax,hx8357" }, |
| 456 | { "reset", "gpios-reset", "himax,hx8369" }, |
| 457 | #endif |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 458 | #if IS_ENABLED(CONFIG_MFD_ARIZONA) |
| 459 | { "wlf,reset", NULL, NULL }, |
| 460 | #endif |
Dmitry Torokhov | eaf1a29 | 2022-10-17 22:41:08 -0700 | [diff] [blame] | 461 | #if IS_ENABLED(CONFIG_RTC_DRV_MOXART) |
| 462 | { "rtc-data", "gpio-rtc-data", "moxa,moxart-rtc" }, |
| 463 | { "rtc-sclk", "gpio-rtc-sclk", "moxa,moxart-rtc" }, |
| 464 | { "rtc-reset", "gpio-rtc-reset", "moxa,moxart-rtc" }, |
| 465 | #endif |
Dmitry Torokhov | 9c2cc717 | 2022-10-17 22:41:06 -0700 | [diff] [blame] | 466 | #if IS_ENABLED(CONFIG_NFC_MRVL_I2C) |
| 467 | { "reset", "reset-n-io", "marvell,nfc-i2c" }, |
| 468 | #endif |
| 469 | #if IS_ENABLED(CONFIG_NFC_MRVL_SPI) |
| 470 | { "reset", "reset-n-io", "marvell,nfc-spi" }, |
| 471 | #endif |
| 472 | #if IS_ENABLED(CONFIG_NFC_MRVL_UART) |
| 473 | { "reset", "reset-n-io", "marvell,nfc-uart" }, |
| 474 | { "reset", "reset-n-io", "mrvl,nfc-uart" }, |
| 475 | #endif |
Dmitry Torokhov | fbbbcd1 | 2022-10-17 22:41:05 -0700 | [diff] [blame] | 476 | #if !IS_ENABLED(CONFIG_PCI_LANTIQ) |
| 477 | /* MIPS Lantiq PCI */ |
| 478 | { "reset", "gpios-reset", "lantiq,pci-xway" }, |
| 479 | #endif |
Dmitry Torokhov | 307c593 | 2022-10-17 22:41:04 -0700 | [diff] [blame] | 480 | |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 481 | /* |
| 482 | * Some regulator bindings happened before we managed to |
| 483 | * establish that GPIO properties should be named |
| 484 | * "foo-gpios" so we have this special kludge for them. |
| 485 | */ |
Dmitry Torokhov | 307c593 | 2022-10-17 22:41:04 -0700 | [diff] [blame] | 486 | #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1) |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 487 | { "wlf,ldoena", NULL, NULL }, /* Arizona */ |
Dmitry Torokhov | 307c593 | 2022-10-17 22:41:04 -0700 | [diff] [blame] | 488 | #endif |
| 489 | #if IS_ENABLED(CONFIG_REGULATOR_WM8994) |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 490 | { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */ |
| 491 | { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */ |
| 492 | #endif |
Linus Walleij | e3023bf | 2018-12-13 15:22:47 +0100 | [diff] [blame] | 493 | |
Dmitry Torokhov | 944004e | 2022-10-17 22:41:07 -0700 | [diff] [blame] | 494 | #if IS_ENABLED(CONFIG_SND_SOC_CS42L56) |
| 495 | { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" }, |
| 496 | #endif |
Dmitry Torokhov | fbbbcd1 | 2022-10-17 22:41:05 -0700 | [diff] [blame] | 497 | #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X) |
| 498 | { "reset", "gpio-reset", "ti,tlv320aic3x" }, |
| 499 | { "reset", "gpio-reset", "ti,tlv320aic33" }, |
| 500 | { "reset", "gpio-reset", "ti,tlv320aic3007" }, |
| 501 | { "reset", "gpio-reset", "ti,tlv320aic3104" }, |
| 502 | { "reset", "gpio-reset", "ti,tlv320aic3106" }, |
| 503 | #endif |
Dmitry Torokhov | 307c593 | 2022-10-17 22:41:04 -0700 | [diff] [blame] | 504 | #if IS_ENABLED(CONFIG_SPI_GPIO) |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 505 | /* |
| 506 | * The SPI GPIO bindings happened before we managed to |
| 507 | * establish that GPIO properties should be named |
| 508 | * "foo-gpios" so we have this special kludge for them. |
| 509 | */ |
| 510 | { "miso", "gpio-miso", "spi-gpio" }, |
| 511 | { "mosi", "gpio-mosi", "spi-gpio" }, |
| 512 | { "sck", "gpio-sck", "spi-gpio" }, |
Dmitry Torokhov | 307c593 | 2022-10-17 22:41:04 -0700 | [diff] [blame] | 513 | #endif |
Linus Walleij | e3023bf | 2018-12-13 15:22:47 +0100 | [diff] [blame] | 514 | |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 515 | /* |
| 516 | * The old Freescale bindings use simply "gpios" as name |
| 517 | * for the chip select lines rather than "cs-gpios" like |
| 518 | * all other SPI hardware. Allow this specifically for |
| 519 | * Freescale and PPC devices. |
| 520 | */ |
Dmitry Torokhov | 307c593 | 2022-10-17 22:41:04 -0700 | [diff] [blame] | 521 | #if IS_ENABLED(CONFIG_SPI_FSL_SPI) |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 522 | { "cs", "gpios", "fsl,spi" }, |
| 523 | { "cs", "gpios", "aeroflexgaisler,spictrl" }, |
Dmitry Torokhov | 307c593 | 2022-10-17 22:41:04 -0700 | [diff] [blame] | 524 | #endif |
| 525 | #if IS_ENABLED(CONFIG_SPI_PPC4xx) |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 526 | { "cs", "gpios", "ibm,ppc4xx-spi" }, |
| 527 | #endif |
Dmitry Torokhov | 307c593 | 2022-10-17 22:41:04 -0700 | [diff] [blame] | 528 | |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 529 | #if IS_ENABLED(CONFIG_TYPEC_FUSB302) |
| 530 | /* |
| 531 | * Fairchild FUSB302 host is using undocumented "fcs,int_n" |
| 532 | * property without the compulsory "-gpios" suffix. |
| 533 | */ |
| 534 | { "fcs,int_n", NULL, "fcs,fusb302" }, |
| 535 | #endif |
Linus Walleij | 6a537d4 | 2017-12-27 16:37:44 +0100 | [diff] [blame] | 536 | }; |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 537 | struct gpio_desc *desc; |
| 538 | const char *legacy_id; |
| 539 | unsigned int i; |
Linus Walleij | 6a537d4 | 2017-12-27 16:37:44 +0100 | [diff] [blame] | 540 | |
| 541 | if (!con_id) |
| 542 | return ERR_PTR(-ENOENT); |
| 543 | |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 544 | for (i = 0; i < ARRAY_SIZE(gpios); i++) { |
| 545 | if (strcmp(con_id, gpios[i].con_id)) |
| 546 | continue; |
Linus Walleij | 6a537d4 | 2017-12-27 16:37:44 +0100 | [diff] [blame] | 547 | |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 548 | if (gpios[i].compatible && |
| 549 | !of_device_is_compatible(np, gpios[i].compatible)) |
| 550 | continue; |
Linus Walleij | 6a537d4 | 2017-12-27 16:37:44 +0100 | [diff] [blame] | 551 | |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 552 | legacy_id = gpios[i].legacy_id ?: gpios[i].con_id; |
| 553 | desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags); |
| 554 | if (!gpiod_not_found(desc)) { |
| 555 | pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n", |
| 556 | of_node_full_name(np), legacy_id, con_id); |
| 557 | return desc; |
| 558 | } |
| 559 | } |
Dmitry Torokhov | 11c43bb | 2019-09-11 00:52:05 -0700 | [diff] [blame] | 560 | |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 561 | return ERR_PTR(-ENOENT); |
Linus Walleij | 6e24826 | 2020-04-15 21:24:48 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Dmitry Torokhov | 326c375 | 2022-10-17 22:41:02 -0700 | [diff] [blame] | 564 | static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np, |
| 565 | const char *con_id, |
| 566 | unsigned int idx, |
| 567 | enum of_gpio_flags *of_flags) |
| 568 | { |
| 569 | struct gpio_desc *desc; |
| 570 | const char *legacy_id; |
| 571 | |
| 572 | if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)) |
| 573 | return ERR_PTR(-ENOENT); |
| 574 | |
| 575 | if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine")) |
| 576 | return ERR_PTR(-ENOENT); |
| 577 | |
| 578 | if (!con_id || strcmp(con_id, "i2s1-in-sel")) |
| 579 | return ERR_PTR(-ENOENT); |
| 580 | |
| 581 | if (idx == 0) |
| 582 | legacy_id = "i2s1-in-sel-gpio1"; |
| 583 | else if (idx == 1) |
| 584 | legacy_id = "i2s1-in-sel-gpio2"; |
| 585 | else |
| 586 | return ERR_PTR(-ENOENT); |
| 587 | |
| 588 | desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags); |
| 589 | if (!gpiod_not_found(desc)) |
| 590 | pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n", |
| 591 | of_node_full_name(np), legacy_id, con_id); |
| 592 | |
| 593 | return desc; |
| 594 | } |
| 595 | |
Dmitry Torokhov | a2b5e20 | 2022-09-07 22:39:48 -0700 | [diff] [blame] | 596 | typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np, |
| 597 | const char *con_id, |
| 598 | unsigned int idx, |
| 599 | enum of_gpio_flags *of_flags); |
| 600 | static const of_find_gpio_quirk of_find_gpio_quirks[] = { |
Dmitry Torokhov | b311c5c | 2022-10-17 22:41:03 -0700 | [diff] [blame] | 601 | of_find_gpio_rename, |
Dmitry Torokhov | 326c375 | 2022-10-17 22:41:02 -0700 | [diff] [blame] | 602 | of_find_mt2701_gpio, |
Michael Walle | 8b10ca2 | 2022-09-16 13:01:18 +0200 | [diff] [blame] | 603 | NULL |
Dmitry Torokhov | a2b5e20 | 2022-09-07 22:39:48 -0700 | [diff] [blame] | 604 | }; |
| 605 | |
Dmitry Torokhov | 07445ae | 2022-11-15 11:16:24 +0100 | [diff] [blame] | 606 | struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id, |
Andy Shevchenko | fed7026 | 2019-04-10 18:39:16 +0300 | [diff] [blame] | 607 | unsigned int idx, unsigned long *flags) |
Linus Walleij | ea713bc | 2016-10-03 10:09:40 +0200 | [diff] [blame] | 608 | { |
| 609 | char prop_name[32]; /* 32 is max size of property name */ |
| 610 | enum of_gpio_flags of_flags; |
Dmitry Torokhov | a2b5e20 | 2022-09-07 22:39:48 -0700 | [diff] [blame] | 611 | const of_find_gpio_quirk *q; |
Linus Walleij | ea713bc | 2016-10-03 10:09:40 +0200 | [diff] [blame] | 612 | struct gpio_desc *desc; |
| 613 | unsigned int i; |
| 614 | |
Linus Walleij | c858233 | 2017-12-27 16:37:44 +0100 | [diff] [blame] | 615 | /* Try GPIO property "foo-gpios" and "foo-gpio" */ |
Linus Walleij | ea713bc | 2016-10-03 10:09:40 +0200 | [diff] [blame] | 616 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
| 617 | if (con_id) |
| 618 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, |
| 619 | gpio_suffixes[i]); |
| 620 | else |
| 621 | snprintf(prop_name, sizeof(prop_name), "%s", |
| 622 | gpio_suffixes[i]); |
| 623 | |
Dmitry Torokhov | 07445ae | 2022-11-15 11:16:24 +0100 | [diff] [blame] | 624 | desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags); |
Maxime Ripard | 6662ae6 | 2018-02-21 09:11:00 +0100 | [diff] [blame] | 625 | |
Andy Shevchenko | 7b58696 | 2020-10-21 17:25:28 +0300 | [diff] [blame] | 626 | if (!gpiod_not_found(desc)) |
Linus Walleij | ea713bc | 2016-10-03 10:09:40 +0200 | [diff] [blame] | 627 | break; |
| 628 | } |
| 629 | |
Dmitry Torokhov | a2b5e20 | 2022-09-07 22:39:48 -0700 | [diff] [blame] | 630 | /* Properly named GPIO was not found, try workarounds */ |
| 631 | for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++) |
Dmitry Torokhov | 07445ae | 2022-11-15 11:16:24 +0100 | [diff] [blame] | 632 | desc = (*q)(np, con_id, idx, &of_flags); |
Linus Walleij | 6e24826 | 2020-04-15 21:24:48 +0200 | [diff] [blame] | 633 | |
Linus Walleij | ea713bc | 2016-10-03 10:09:40 +0200 | [diff] [blame] | 634 | if (IS_ERR(desc)) |
| 635 | return desc; |
| 636 | |
Dmitry Torokhov | d9e7f0e | 2022-09-07 22:39:49 -0700 | [diff] [blame] | 637 | *flags = of_convert_gpio_flags(of_flags); |
Thomas Petazzoni | d449991 | 2019-02-07 17:28:58 +0100 | [diff] [blame] | 638 | |
Linus Walleij | ea713bc | 2016-10-03 10:09:40 +0200 | [diff] [blame] | 639 | return desc; |
| 640 | } |
| 641 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 642 | /** |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 643 | * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 644 | * @np: device node to get GPIO from |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 645 | * @chip: GPIO chip whose hog is parsed |
Geert Uytterhoeven | a79fead | 2016-12-19 19:21:34 +0100 | [diff] [blame] | 646 | * @idx: Index of the GPIO to parse |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 647 | * @name: GPIO line name |
Andy Shevchenko | fed7026 | 2019-04-10 18:39:16 +0300 | [diff] [blame] | 648 | * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
| 649 | * of_find_gpio() or of_parse_own_gpio() |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 650 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 651 | * |
| 652 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
| 653 | * value on the error condition. |
| 654 | */ |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 655 | static struct gpio_desc *of_parse_own_gpio(struct device_node *np, |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 656 | struct gpio_chip *chip, |
Geert Uytterhoeven | a79fead | 2016-12-19 19:21:34 +0100 | [diff] [blame] | 657 | unsigned int idx, const char **name, |
Andy Shevchenko | fed7026 | 2019-04-10 18:39:16 +0300 | [diff] [blame] | 658 | unsigned long *lflags, |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 659 | enum gpiod_flags *dflags) |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 660 | { |
| 661 | struct device_node *chip_np; |
| 662 | enum of_gpio_flags xlate_flags; |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 663 | struct of_phandle_args gpiospec; |
| 664 | struct gpio_desc *desc; |
Geert Uytterhoeven | a79fead | 2016-12-19 19:21:34 +0100 | [diff] [blame] | 665 | unsigned int i; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 666 | u32 tmp; |
Masahiro Yamada | 3f9547e | 2016-06-14 19:07:03 +0900 | [diff] [blame] | 667 | int ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 668 | |
Andy Shevchenko | 70d0fc4 | 2022-12-28 11:20:44 +0200 | [diff] [blame] | 669 | chip_np = dev_of_node(&chip->gpiodev->dev); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 670 | if (!chip_np) |
| 671 | return ERR_PTR(-EINVAL); |
| 672 | |
| 673 | xlate_flags = 0; |
Andy Shevchenko | 2d6c06f | 2019-04-10 18:39:17 +0300 | [diff] [blame] | 674 | *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
Andy Shevchenko | 4094195 | 2020-10-22 19:58:47 +0300 | [diff] [blame] | 675 | *dflags = GPIOD_ASIS; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 676 | |
| 677 | ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); |
| 678 | if (ret) |
| 679 | return ERR_PTR(ret); |
| 680 | |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 681 | gpiospec.np = chip_np; |
| 682 | gpiospec.args_count = tmp; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 683 | |
Geert Uytterhoeven | a79fead | 2016-12-19 19:21:34 +0100 | [diff] [blame] | 684 | for (i = 0; i < tmp; i++) { |
| 685 | ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, |
| 686 | &gpiospec.args[i]); |
| 687 | if (ret) |
| 688 | return ERR_PTR(ret); |
| 689 | } |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 690 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 691 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 692 | if (IS_ERR(desc)) |
| 693 | return desc; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 694 | |
Dmitry Torokhov | d9e7f0e | 2022-09-07 22:39:49 -0700 | [diff] [blame] | 695 | *lflags = of_convert_gpio_flags(xlate_flags); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 696 | |
| 697 | if (of_property_read_bool(np, "input")) |
| 698 | *dflags |= GPIOD_IN; |
| 699 | else if (of_property_read_bool(np, "output-low")) |
| 700 | *dflags |= GPIOD_OUT_LOW; |
| 701 | else if (of_property_read_bool(np, "output-high")) |
| 702 | *dflags |= GPIOD_OUT_HIGH; |
| 703 | else { |
Rob Herring | 62cdcb6 | 2018-08-27 20:52:19 -0500 | [diff] [blame] | 704 | pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", |
| 705 | desc_to_gpio(desc), np); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 706 | return ERR_PTR(-EINVAL); |
| 707 | } |
| 708 | |
| 709 | if (name && of_property_read_string(np, "line-name", name)) |
| 710 | *name = np->name; |
| 711 | |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 712 | return desc; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /** |
Geert Uytterhoeven | bc21077 | 2020-02-20 14:01:48 +0100 | [diff] [blame] | 716 | * of_gpiochip_add_hog - Add all hogs in a hog device node |
| 717 | * @chip: gpio chip to act on |
| 718 | * @hog: device node describing the hogs |
| 719 | * |
| 720 | * Returns error if it fails otherwise 0 on success. |
| 721 | */ |
| 722 | static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) |
| 723 | { |
| 724 | enum gpiod_flags dflags; |
| 725 | struct gpio_desc *desc; |
| 726 | unsigned long lflags; |
| 727 | const char *name; |
| 728 | unsigned int i; |
| 729 | int ret; |
| 730 | |
| 731 | for (i = 0;; i++) { |
| 732 | desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags); |
| 733 | if (IS_ERR(desc)) |
| 734 | break; |
| 735 | |
| 736 | ret = gpiod_hog(desc, name, lflags, dflags); |
| 737 | if (ret < 0) |
| 738 | return ret; |
Geert Uytterhoeven | 63636d9 | 2020-02-20 14:01:49 +0100 | [diff] [blame] | 739 | |
| 740 | #ifdef CONFIG_OF_DYNAMIC |
| 741 | desc->hog = hog; |
| 742 | #endif |
Geert Uytterhoeven | bc21077 | 2020-02-20 14:01:48 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | /** |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 749 | * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 750 | * @chip: gpio chip to act on |
| 751 | * |
| 752 | * This is only used by of_gpiochip_add to request/set GPIO initial |
| 753 | * configuration. |
Geert Uytterhoeven | ead066e | 2017-06-01 12:30:01 +0200 | [diff] [blame] | 754 | * It returns error if it fails otherwise 0 on success. |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 755 | */ |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 756 | static int of_gpiochip_scan_gpios(struct gpio_chip *chip) |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 757 | { |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 758 | struct device_node *np; |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 759 | int ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 760 | |
Andy Shevchenko | 70d0fc4 | 2022-12-28 11:20:44 +0200 | [diff] [blame] | 761 | for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) { |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 762 | if (!of_property_read_bool(np, "gpio-hog")) |
| 763 | continue; |
| 764 | |
Geert Uytterhoeven | bc21077 | 2020-02-20 14:01:48 +0100 | [diff] [blame] | 765 | ret = of_gpiochip_add_hog(chip, np); |
| 766 | if (ret < 0) { |
| 767 | of_node_put(np); |
| 768 | return ret; |
Wei Yongjun | 09e258a | 2016-10-29 16:13:52 +0000 | [diff] [blame] | 769 | } |
Geert Uytterhoeven | 63636d9 | 2020-02-20 14:01:49 +0100 | [diff] [blame] | 770 | |
| 771 | of_node_set_flag(np, OF_POPULATED); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 772 | } |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 773 | |
| 774 | return 0; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 775 | } |
| 776 | |
Geert Uytterhoeven | 63636d9 | 2020-02-20 14:01:49 +0100 | [diff] [blame] | 777 | #ifdef CONFIG_OF_DYNAMIC |
| 778 | /** |
| 779 | * of_gpiochip_remove_hog - Remove all hogs in a hog device node |
| 780 | * @chip: gpio chip to act on |
| 781 | * @hog: device node describing the hogs |
| 782 | */ |
| 783 | static void of_gpiochip_remove_hog(struct gpio_chip *chip, |
| 784 | struct device_node *hog) |
| 785 | { |
Andy Shevchenko | 80c78fb | 2022-02-01 17:27:56 +0200 | [diff] [blame] | 786 | struct gpio_desc *desc; |
Geert Uytterhoeven | 63636d9 | 2020-02-20 14:01:49 +0100 | [diff] [blame] | 787 | |
Andy Shevchenko | 57017edd | 2022-04-08 21:18:50 +0300 | [diff] [blame] | 788 | for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED) |
Andy Shevchenko | 80c78fb | 2022-02-01 17:27:56 +0200 | [diff] [blame] | 789 | if (desc->hog == hog) |
| 790 | gpiochip_free_own_desc(desc); |
Geert Uytterhoeven | 63636d9 | 2020-02-20 14:01:49 +0100 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) |
| 794 | { |
Andy Shevchenko | 597a8a8 | 2022-06-29 14:30:58 +0300 | [diff] [blame] | 795 | return device_match_of_node(&chip->gpiodev->dev, data); |
Geert Uytterhoeven | 63636d9 | 2020-02-20 14:01:49 +0100 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np) |
| 799 | { |
| 800 | return gpiochip_find(np, of_gpiochip_match_node); |
| 801 | } |
| 802 | |
| 803 | static int of_gpio_notify(struct notifier_block *nb, unsigned long action, |
| 804 | void *arg) |
| 805 | { |
| 806 | struct of_reconfig_data *rd = arg; |
| 807 | struct gpio_chip *chip; |
| 808 | int ret; |
| 809 | |
| 810 | /* |
| 811 | * This only supports adding and removing complete gpio-hog nodes. |
| 812 | * Modifying an existing gpio-hog node is not supported (except for |
| 813 | * changing its "status" property, which is treated the same as |
| 814 | * addition/removal). |
| 815 | */ |
| 816 | switch (of_reconfig_get_state_change(action, arg)) { |
| 817 | case OF_RECONFIG_CHANGE_ADD: |
| 818 | if (!of_property_read_bool(rd->dn, "gpio-hog")) |
| 819 | return NOTIFY_OK; /* not for us */ |
| 820 | |
| 821 | if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) |
| 822 | return NOTIFY_OK; |
| 823 | |
| 824 | chip = of_find_gpiochip_by_node(rd->dn->parent); |
| 825 | if (chip == NULL) |
| 826 | return NOTIFY_OK; /* not for us */ |
| 827 | |
| 828 | ret = of_gpiochip_add_hog(chip, rd->dn); |
| 829 | if (ret < 0) { |
| 830 | pr_err("%s: failed to add hogs for %pOF\n", __func__, |
| 831 | rd->dn); |
| 832 | of_node_clear_flag(rd->dn, OF_POPULATED); |
| 833 | return notifier_from_errno(ret); |
| 834 | } |
| 835 | break; |
| 836 | |
| 837 | case OF_RECONFIG_CHANGE_REMOVE: |
| 838 | if (!of_node_check_flag(rd->dn, OF_POPULATED)) |
| 839 | return NOTIFY_OK; /* already depopulated */ |
| 840 | |
| 841 | chip = of_find_gpiochip_by_node(rd->dn->parent); |
| 842 | if (chip == NULL) |
| 843 | return NOTIFY_OK; /* not for us */ |
| 844 | |
| 845 | of_gpiochip_remove_hog(chip, rd->dn); |
| 846 | of_node_clear_flag(rd->dn, OF_POPULATED); |
| 847 | break; |
| 848 | } |
| 849 | |
| 850 | return NOTIFY_OK; |
| 851 | } |
| 852 | |
| 853 | struct notifier_block gpio_of_notifier = { |
| 854 | .notifier_call = of_gpio_notify, |
| 855 | }; |
| 856 | #endif /* CONFIG_OF_DYNAMIC */ |
| 857 | |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 858 | /** |
Thierry Reding | 67049c5 | 2017-07-24 16:57:23 +0200 | [diff] [blame] | 859 | * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 860 | * @gc: pointer to the gpio_chip structure |
Thierry Reding | 67049c5 | 2017-07-24 16:57:23 +0200 | [diff] [blame] | 861 | * @gpiospec: GPIO specifier as found in the device tree |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 862 | * @flags: a flags pointer to fill in |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 863 | * |
| 864 | * This is simple translation function, suitable for the most 1:1 mapped |
Thierry Reding | 67049c5 | 2017-07-24 16:57:23 +0200 | [diff] [blame] | 865 | * GPIO chips. This function performs only one sanity check: whether GPIO |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 866 | * is less than ngpios (that is specified in the gpio_chip). |
| 867 | */ |
Geert Uytterhoeven | b0c7e73 | 2019-09-06 10:45:37 +0200 | [diff] [blame] | 868 | static int of_gpio_simple_xlate(struct gpio_chip *gc, |
| 869 | const struct of_phandle_args *gpiospec, |
| 870 | u32 *flags) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 871 | { |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 872 | /* |
| 873 | * We're discouraging gpio_cells < 2, since that way you'll have to |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 874 | * write your own xlate function (that will have to retrieve the GPIO |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 875 | * number and the flags from a single gpio cell -- this is possible, |
| 876 | * but not recommended). |
| 877 | */ |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 878 | if (gc->of_gpio_n_cells < 2) { |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 879 | WARN_ON(1); |
| 880 | return -EINVAL; |
| 881 | } |
| 882 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 883 | if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) |
| 884 | return -EINVAL; |
| 885 | |
Roland Stigge | 6270d83 | 2012-04-04 02:02:58 +0200 | [diff] [blame] | 886 | if (gpiospec->args[0] >= gc->ngpio) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 887 | return -EINVAL; |
| 888 | |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 889 | if (flags) |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 890 | *flags = gpiospec->args[1]; |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 891 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 892 | return gpiospec->args[0]; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 893 | } |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 894 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 895 | /** |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 896 | * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 897 | * @np: device node of the GPIO chip |
| 898 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 899 | * @data: driver data to store in the struct gpio_chip |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 900 | * |
| 901 | * To use this function you should allocate and fill mm_gc with: |
| 902 | * |
| 903 | * 1) In the gpio_chip structure: |
| 904 | * - all the callbacks |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 905 | * - of_gpio_n_cells |
| 906 | * - of_xlate callback (optional) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 907 | * |
| 908 | * 3) In the of_mm_gpio_chip structure: |
| 909 | * - save_regs callback (optional) |
| 910 | * |
| 911 | * If succeeded, this function will map bank's memory and will |
| 912 | * do all necessary work for you. Then you'll able to use .regs |
| 913 | * to manage GPIOs from the callbacks. |
| 914 | */ |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 915 | int of_mm_gpiochip_add_data(struct device_node *np, |
| 916 | struct of_mm_gpio_chip *mm_gc, |
| 917 | void *data) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 918 | { |
| 919 | int ret = -ENOMEM; |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 920 | struct gpio_chip *gc = &mm_gc->gc; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 921 | |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 922 | gc->label = kasprintf(GFP_KERNEL, "%pOF", np); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 923 | if (!gc->label) |
| 924 | goto err0; |
| 925 | |
| 926 | mm_gc->regs = of_iomap(np, 0); |
| 927 | if (!mm_gc->regs) |
| 928 | goto err1; |
| 929 | |
Anton Vorontsov | 2145115 | 2008-04-30 00:05:24 +1000 | [diff] [blame] | 930 | gc->base = -1; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 931 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 932 | if (mm_gc->save_regs) |
| 933 | mm_gc->save_regs(mm_gc); |
| 934 | |
Andy Shevchenko | 77289b2 | 2022-11-14 20:46:25 +0200 | [diff] [blame] | 935 | fwnode_handle_put(mm_gc->gc.fwnode); |
| 936 | mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np)); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 937 | |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 938 | ret = gpiochip_add_data(gc, data); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 939 | if (ret) |
| 940 | goto err2; |
| 941 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 942 | return 0; |
| 943 | err2: |
Liang He | 5d07a69 | 2022-07-11 20:52:38 +0800 | [diff] [blame] | 944 | of_node_put(np); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 945 | iounmap(mm_gc->regs); |
| 946 | err1: |
| 947 | kfree(gc->label); |
| 948 | err0: |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 949 | pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 950 | return ret; |
| 951 | } |
Geert Uytterhoeven | 6d66245 | 2019-09-06 10:45:38 +0200 | [diff] [blame] | 952 | EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data); |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 953 | |
Ricardo Ribalda Delgado | d621e8b | 2014-12-17 16:51:13 +0100 | [diff] [blame] | 954 | /** |
| 955 | * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) |
| 956 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure |
| 957 | */ |
| 958 | void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) |
| 959 | { |
| 960 | struct gpio_chip *gc = &mm_gc->gc; |
| 961 | |
Ricardo Ribalda Delgado | d621e8b | 2014-12-17 16:51:13 +0100 | [diff] [blame] | 962 | gpiochip_remove(gc); |
| 963 | iounmap(mm_gc->regs); |
| 964 | kfree(gc->label); |
| 965 | } |
Geert Uytterhoeven | 6d66245 | 2019-09-06 10:45:38 +0200 | [diff] [blame] | 966 | EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove); |
Ricardo Ribalda Delgado | d621e8b | 2014-12-17 16:51:13 +0100 | [diff] [blame] | 967 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 968 | #ifdef CONFIG_PINCTRL |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 969 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 970 | { |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 971 | struct of_phandle_args pinspec; |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 972 | struct pinctrl_dev *pctldev; |
Andy Shevchenko | 70d0fc4 | 2022-12-28 11:20:44 +0200 | [diff] [blame] | 973 | struct device_node *np; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 974 | int index = 0, ret; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 975 | const char *name; |
| 976 | static const char group_names_propname[] = "gpio-ranges-group-names"; |
| 977 | struct property *group_names; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 978 | |
Andy Shevchenko | 70d0fc4 | 2022-12-28 11:20:44 +0200 | [diff] [blame] | 979 | np = dev_of_node(&chip->gpiodev->dev); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 980 | if (!np) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 981 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 982 | |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 983 | group_names = of_find_property(np, group_names_propname, NULL); |
| 984 | |
Haojian Zhuang | ad4e1a7 | 2013-02-17 19:42:48 +0800 | [diff] [blame] | 985 | for (;; index++) { |
Stephen Warren | d9fe003 | 2013-08-14 15:27:12 -0600 | [diff] [blame] | 986 | ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, |
| 987 | index, &pinspec); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 988 | if (ret) |
| 989 | break; |
| 990 | |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 991 | pctldev = of_pinctrl_get(pinspec.np); |
Masahiro Yamada | 602cf63 | 2016-05-23 10:52:09 +0900 | [diff] [blame] | 992 | of_node_put(pinspec.np); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 993 | if (!pctldev) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 994 | return -EPROBE_DEFER; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 995 | |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 996 | if (pinspec.args[2]) { |
| 997 | if (group_names) { |
Laurent Navet | 7285860 | 2015-07-07 22:22:15 +0200 | [diff] [blame] | 998 | of_property_read_string_index(np, |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 999 | group_names_propname, |
| 1000 | index, &name); |
| 1001 | if (strlen(name)) { |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 1002 | pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", |
| 1003 | np); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1004 | break; |
| 1005 | } |
| 1006 | } |
| 1007 | /* npins != 0: linear range */ |
| 1008 | ret = gpiochip_add_pin_range(chip, |
| 1009 | pinctrl_dev_get_devname(pctldev), |
| 1010 | pinspec.args[0], |
| 1011 | pinspec.args[1], |
| 1012 | pinspec.args[2]); |
| 1013 | if (ret) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1014 | return ret; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1015 | } else { |
| 1016 | /* npins == 0: special range */ |
| 1017 | if (pinspec.args[1]) { |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 1018 | pr_err("%pOF: Illegal gpio-range format.\n", |
| 1019 | np); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1020 | break; |
| 1021 | } |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1022 | |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1023 | if (!group_names) { |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 1024 | pr_err("%pOF: GPIO group range requested but no %s property.\n", |
| 1025 | np, group_names_propname); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1026 | break; |
| 1027 | } |
| 1028 | |
| 1029 | ret = of_property_read_string_index(np, |
| 1030 | group_names_propname, |
| 1031 | index, &name); |
| 1032 | if (ret) |
| 1033 | break; |
| 1034 | |
| 1035 | if (!strlen(name)) { |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 1036 | pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", |
| 1037 | np); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | ret = gpiochip_add_pingroup_range(chip, pctldev, |
| 1042 | pinspec.args[0], name); |
| 1043 | if (ret) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1044 | return ret; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1045 | } |
Haojian Zhuang | ad4e1a7 | 2013-02-17 19:42:48 +0800 | [diff] [blame] | 1046 | } |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1047 | |
| 1048 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1049 | } |
| 1050 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1051 | #else |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1052 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1053 | #endif |
| 1054 | |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1055 | int of_gpiochip_add(struct gpio_chip *chip) |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1056 | { |
Andy Shevchenko | 8afe825 | 2022-11-08 15:38:52 +0200 | [diff] [blame] | 1057 | struct device_node *np; |
Linus Walleij | f0d1ab0 | 2019-07-16 13:58:54 +0200 | [diff] [blame] | 1058 | int ret; |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1059 | |
Andy Shevchenko | 70d0fc4 | 2022-12-28 11:20:44 +0200 | [diff] [blame] | 1060 | np = dev_of_node(&chip->gpiodev->dev); |
Andy Shevchenko | 8afe825 | 2022-11-08 15:38:52 +0200 | [diff] [blame] | 1061 | if (!np) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1062 | return 0; |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1063 | |
| 1064 | if (!chip->of_xlate) { |
| 1065 | chip->of_gpio_n_cells = 2; |
| 1066 | chip->of_xlate = of_gpio_simple_xlate; |
| 1067 | } |
| 1068 | |
Masahiro Yamada | 1020dfd1 | 2016-06-14 19:07:05 +0900 | [diff] [blame] | 1069 | if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) |
| 1070 | return -EINVAL; |
| 1071 | |
Linus Walleij | f0d1ab0 | 2019-07-16 13:58:54 +0200 | [diff] [blame] | 1072 | ret = of_gpiochip_add_pin_range(chip); |
| 1073 | if (ret) |
| 1074 | return ret; |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1075 | |
Andy Shevchenko | 8afe825 | 2022-11-08 15:38:52 +0200 | [diff] [blame] | 1076 | fwnode_handle_get(chip->fwnode); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 1077 | |
Linus Walleij | f0d1ab0 | 2019-07-16 13:58:54 +0200 | [diff] [blame] | 1078 | ret = of_gpiochip_scan_gpios(chip); |
Andy Shevchenko | 2f4133b | 2019-11-05 20:06:54 +0200 | [diff] [blame] | 1079 | if (ret) |
Andy Shevchenko | 8afe825 | 2022-11-08 15:38:52 +0200 | [diff] [blame] | 1080 | fwnode_handle_put(chip->fwnode); |
Geert Uytterhoeven | f7299d4 | 2019-03-28 14:13:47 +0100 | [diff] [blame] | 1081 | |
Linus Walleij | f0d1ab0 | 2019-07-16 13:58:54 +0200 | [diff] [blame] | 1082 | return ret; |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | void of_gpiochip_remove(struct gpio_chip *chip) |
| 1086 | { |
Andy Shevchenko | 8afe825 | 2022-11-08 15:38:52 +0200 | [diff] [blame] | 1087 | fwnode_handle_put(chip->fwnode); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1088 | } |