.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
---|
1 | 2 | /* |
---|
2 | 3 | * OF helpers for the GPIO API |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (c) 2007-2008 MontaVista Software, Inc. |
---|
5 | 6 | * |
---|
6 | 7 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
---|
7 | | - * |
---|
8 | | - * This program is free software; you can redistribute it and/or modify |
---|
9 | | - * it under the terms of the GNU General Public License as published by |
---|
10 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
11 | | - * (at your option) any later version. |
---|
12 | 8 | */ |
---|
13 | 9 | |
---|
14 | 10 | #include <linux/device.h> |
---|
.. | .. |
---|
25 | 21 | #include <linux/gpio/machine.h> |
---|
26 | 22 | |
---|
27 | 23 | #include "gpiolib.h" |
---|
| 24 | +#include "gpiolib-of.h" |
---|
| 25 | + |
---|
| 26 | +/** |
---|
| 27 | + * of_gpio_spi_cs_get_count() - special GPIO counting for SPI |
---|
| 28 | + * @dev: Consuming device |
---|
| 29 | + * @con_id: Function within the GPIO consumer |
---|
| 30 | + * |
---|
| 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 |
---|
| 34 | + * "gpios" for the chip select lines. If we detect this, we redirect |
---|
| 35 | + * the counting of "cs-gpios" to count "gpios" transparent to the |
---|
| 36 | + * driver. |
---|
| 37 | + */ |
---|
| 38 | +static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id) |
---|
| 39 | +{ |
---|
| 40 | + struct device_node *np = dev->of_node; |
---|
| 41 | + |
---|
| 42 | + if (!IS_ENABLED(CONFIG_SPI_MASTER)) |
---|
| 43 | + return 0; |
---|
| 44 | + if (!con_id || strcmp(con_id, "cs")) |
---|
| 45 | + return 0; |
---|
| 46 | + if (!of_device_is_compatible(np, "fsl,spi") && |
---|
| 47 | + !of_device_is_compatible(np, "aeroflexgaisler,spictrl") && |
---|
| 48 | + !of_device_is_compatible(np, "ibm,ppc4xx-spi")) |
---|
| 49 | + return 0; |
---|
| 50 | + return of_gpio_named_count(np, "gpios"); |
---|
| 51 | +} |
---|
| 52 | + |
---|
| 53 | +/* |
---|
| 54 | + * This is used by external users of of_gpio_count() from <linux/of_gpio.h> |
---|
| 55 | + * |
---|
| 56 | + * FIXME: get rid of those external users by converting them to GPIO |
---|
| 57 | + * descriptors and let them all use gpiod_count() |
---|
| 58 | + */ |
---|
| 59 | +int of_gpio_get_count(struct device *dev, const char *con_id) |
---|
| 60 | +{ |
---|
| 61 | + int ret; |
---|
| 62 | + char propname[32]; |
---|
| 63 | + unsigned int i; |
---|
| 64 | + |
---|
| 65 | + ret = of_gpio_spi_cs_get_count(dev, con_id); |
---|
| 66 | + if (ret > 0) |
---|
| 67 | + return ret; |
---|
| 68 | + |
---|
| 69 | + for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
---|
| 70 | + if (con_id) |
---|
| 71 | + snprintf(propname, sizeof(propname), "%s-%s", |
---|
| 72 | + con_id, gpio_suffixes[i]); |
---|
| 73 | + else |
---|
| 74 | + snprintf(propname, sizeof(propname), "%s", |
---|
| 75 | + gpio_suffixes[i]); |
---|
| 76 | + |
---|
| 77 | + ret = of_gpio_named_count(dev->of_node, propname); |
---|
| 78 | + if (ret > 0) |
---|
| 79 | + break; |
---|
| 80 | + } |
---|
| 81 | + return ret ? ret : -ENOENT; |
---|
| 82 | +} |
---|
28 | 83 | |
---|
29 | 84 | static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) |
---|
30 | 85 | { |
---|
.. | .. |
---|
57 | 112 | return gpiochip_get_desc(chip, ret); |
---|
58 | 113 | } |
---|
59 | 114 | |
---|
| 115 | +/** |
---|
| 116 | + * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs |
---|
| 117 | + * to set the .valid_mask |
---|
| 118 | + * @gc: the target gpio_chip |
---|
| 119 | + * |
---|
| 120 | + * Return: true if the valid mask needs to be set |
---|
| 121 | + */ |
---|
| 122 | +bool of_gpio_need_valid_mask(const struct gpio_chip *gc) |
---|
| 123 | +{ |
---|
| 124 | + int size; |
---|
| 125 | + struct device_node *np = gc->of_node; |
---|
| 126 | + |
---|
| 127 | + size = of_property_count_u32_elems(np, "gpio-reserved-ranges"); |
---|
| 128 | + if (size > 0 && size % 2 == 0) |
---|
| 129 | + return true; |
---|
| 130 | + return false; |
---|
| 131 | +} |
---|
| 132 | + |
---|
60 | 133 | static void of_gpio_flags_quirks(struct device_node *np, |
---|
61 | | - enum of_gpio_flags *flags) |
---|
| 134 | + const char *propname, |
---|
| 135 | + enum of_gpio_flags *flags, |
---|
| 136 | + int index) |
---|
62 | 137 | { |
---|
63 | 138 | /* |
---|
64 | 139 | * Some GPIO fixed regulator quirks. |
---|
.. | .. |
---|
67 | 142 | if (IS_ENABLED(CONFIG_REGULATOR) && |
---|
68 | 143 | (of_device_is_compatible(np, "regulator-fixed") || |
---|
69 | 144 | of_device_is_compatible(np, "reg-fixed-voltage") || |
---|
70 | | - of_device_is_compatible(np, "regulator-gpio"))) { |
---|
| 145 | + (!(strcmp(propname, "enable-gpio") && |
---|
| 146 | + strcmp(propname, "enable-gpios")) && |
---|
| 147 | + of_device_is_compatible(np, "regulator-gpio")))) { |
---|
| 148 | + bool active_low = !of_property_read_bool(np, |
---|
| 149 | + "enable-active-high"); |
---|
71 | 150 | /* |
---|
72 | 151 | * The regulator GPIO handles are specified such that the |
---|
73 | 152 | * presence or absence of "enable-active-high" solely controls |
---|
74 | 153 | * the polarity of the GPIO line. Any phandle flags must |
---|
75 | 154 | * be actively ignored. |
---|
76 | 155 | */ |
---|
77 | | - if (*flags & OF_GPIO_ACTIVE_LOW) { |
---|
| 156 | + if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) { |
---|
78 | 157 | pr_warn("%s GPIO handle specifies active low - ignored\n", |
---|
79 | 158 | of_node_full_name(np)); |
---|
80 | 159 | *flags &= ~OF_GPIO_ACTIVE_LOW; |
---|
81 | 160 | } |
---|
82 | | - if (!of_property_read_bool(np, "enable-active-high")) |
---|
| 161 | + if (active_low) |
---|
83 | 162 | *flags |= OF_GPIO_ACTIVE_LOW; |
---|
84 | 163 | } |
---|
85 | 164 | /* |
---|
.. | .. |
---|
92 | 171 | pr_info("%s uses legacy open drain flag - update the DTS if you can\n", |
---|
93 | 172 | of_node_full_name(np)); |
---|
94 | 173 | } |
---|
| 174 | + |
---|
| 175 | + /* |
---|
| 176 | + * Legacy handling of SPI active high chip select. If we have a |
---|
| 177 | + * property named "cs-gpios" we need to inspect the child node |
---|
| 178 | + * to determine if the flags should have inverted semantics. |
---|
| 179 | + */ |
---|
| 180 | + if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && |
---|
| 181 | + of_property_read_bool(np, "cs-gpios")) { |
---|
| 182 | + struct device_node *child; |
---|
| 183 | + u32 cs; |
---|
| 184 | + int ret; |
---|
| 185 | + |
---|
| 186 | + for_each_child_of_node(np, child) { |
---|
| 187 | + ret = of_property_read_u32(child, "reg", &cs); |
---|
| 188 | + if (ret) |
---|
| 189 | + continue; |
---|
| 190 | + if (cs == index) { |
---|
| 191 | + /* |
---|
| 192 | + * SPI children have active low chip selects |
---|
| 193 | + * by default. This can be specified negatively |
---|
| 194 | + * by just omitting "spi-cs-high" in the |
---|
| 195 | + * device node, or actively by tagging on |
---|
| 196 | + * GPIO_ACTIVE_LOW as flag in the device |
---|
| 197 | + * tree. If the line is simultaneously |
---|
| 198 | + * tagged as active low in the device tree |
---|
| 199 | + * and has the "spi-cs-high" set, we get a |
---|
| 200 | + * conflict and the "spi-cs-high" flag will |
---|
| 201 | + * take precedence. |
---|
| 202 | + */ |
---|
| 203 | + if (of_property_read_bool(child, "spi-cs-high")) { |
---|
| 204 | + if (*flags & OF_GPIO_ACTIVE_LOW) { |
---|
| 205 | + pr_warn("%s GPIO handle specifies active low - ignored\n", |
---|
| 206 | + of_node_full_name(child)); |
---|
| 207 | + *flags &= ~OF_GPIO_ACTIVE_LOW; |
---|
| 208 | + } |
---|
| 209 | + } else { |
---|
| 210 | + if (!(*flags & OF_GPIO_ACTIVE_LOW)) |
---|
| 211 | + pr_info("%s enforce active low on chipselect handle\n", |
---|
| 212 | + of_node_full_name(child)); |
---|
| 213 | + *flags |= OF_GPIO_ACTIVE_LOW; |
---|
| 214 | + } |
---|
| 215 | + of_node_put(child); |
---|
| 216 | + break; |
---|
| 217 | + } |
---|
| 218 | + } |
---|
| 219 | + } |
---|
| 220 | + |
---|
| 221 | + /* Legacy handling of stmmac's active-low PHY reset line */ |
---|
| 222 | + if (IS_ENABLED(CONFIG_STMMAC_ETH) && |
---|
| 223 | + !strcmp(propname, "snps,reset-gpio") && |
---|
| 224 | + of_property_read_bool(np, "snps,reset-active-low")) |
---|
| 225 | + *flags |= OF_GPIO_ACTIVE_LOW; |
---|
95 | 226 | } |
---|
96 | 227 | |
---|
97 | 228 | /** |
---|
.. | .. |
---|
105 | 236 | * value on the error condition. If @flags is not NULL the function also fills |
---|
106 | 237 | * in flags for the GPIO. |
---|
107 | 238 | */ |
---|
108 | | -struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, |
---|
| 239 | +static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, |
---|
109 | 240 | const char *propname, int index, enum of_gpio_flags *flags) |
---|
110 | 241 | { |
---|
111 | 242 | struct of_phandle_args gpiospec; |
---|
.. | .. |
---|
132 | 263 | goto out; |
---|
133 | 264 | |
---|
134 | 265 | if (flags) |
---|
135 | | - of_gpio_flags_quirks(np, flags); |
---|
| 266 | + of_gpio_flags_quirks(np, propname, flags, index); |
---|
136 | 267 | |
---|
137 | 268 | pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", |
---|
138 | 269 | __func__, propname, np, index, |
---|
.. | .. |
---|
156 | 287 | else |
---|
157 | 288 | return desc_to_gpio(desc); |
---|
158 | 289 | } |
---|
159 | | -EXPORT_SYMBOL(of_get_named_gpio_flags); |
---|
| 290 | +EXPORT_SYMBOL_GPL(of_get_named_gpio_flags); |
---|
| 291 | + |
---|
| 292 | +/** |
---|
| 293 | + * gpiod_get_from_of_node() - obtain a GPIO from an OF node |
---|
| 294 | + * @node: handle of the 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 |
---|
| 299 | + * |
---|
| 300 | + * Returns: |
---|
| 301 | + * On successful request the GPIO pin is configured in accordance with |
---|
| 302 | + * provided @dflags. |
---|
| 303 | + * |
---|
| 304 | + * In case of error an ERR_PTR() is returned. |
---|
| 305 | + */ |
---|
| 306 | +struct gpio_desc *gpiod_get_from_of_node(struct device_node *node, |
---|
| 307 | + const char *propname, int index, |
---|
| 308 | + enum gpiod_flags dflags, |
---|
| 309 | + const char *label) |
---|
| 310 | +{ |
---|
| 311 | + unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
---|
| 312 | + struct gpio_desc *desc; |
---|
| 313 | + enum of_gpio_flags flags; |
---|
| 314 | + bool active_low = false; |
---|
| 315 | + bool single_ended = false; |
---|
| 316 | + bool open_drain = false; |
---|
| 317 | + bool transitory = false; |
---|
| 318 | + int ret; |
---|
| 319 | + |
---|
| 320 | + desc = of_get_named_gpiod_flags(node, propname, |
---|
| 321 | + index, &flags); |
---|
| 322 | + |
---|
| 323 | + if (!desc || IS_ERR(desc)) { |
---|
| 324 | + return desc; |
---|
| 325 | + } |
---|
| 326 | + |
---|
| 327 | + active_low = flags & OF_GPIO_ACTIVE_LOW; |
---|
| 328 | + single_ended = flags & OF_GPIO_SINGLE_ENDED; |
---|
| 329 | + open_drain = flags & OF_GPIO_OPEN_DRAIN; |
---|
| 330 | + transitory = flags & OF_GPIO_TRANSITORY; |
---|
| 331 | + |
---|
| 332 | + ret = gpiod_request(desc, label); |
---|
| 333 | + if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) |
---|
| 334 | + return desc; |
---|
| 335 | + if (ret) |
---|
| 336 | + return ERR_PTR(ret); |
---|
| 337 | + |
---|
| 338 | + if (active_low) |
---|
| 339 | + lflags |= GPIO_ACTIVE_LOW; |
---|
| 340 | + |
---|
| 341 | + if (single_ended) { |
---|
| 342 | + if (open_drain) |
---|
| 343 | + lflags |= GPIO_OPEN_DRAIN; |
---|
| 344 | + else |
---|
| 345 | + lflags |= GPIO_OPEN_SOURCE; |
---|
| 346 | + } |
---|
| 347 | + |
---|
| 348 | + if (transitory) |
---|
| 349 | + lflags |= GPIO_TRANSITORY; |
---|
| 350 | + |
---|
| 351 | + if (flags & OF_GPIO_PULL_UP) |
---|
| 352 | + lflags |= GPIO_PULL_UP; |
---|
| 353 | + |
---|
| 354 | + if (flags & OF_GPIO_PULL_DOWN) |
---|
| 355 | + lflags |= GPIO_PULL_DOWN; |
---|
| 356 | + |
---|
| 357 | + ret = gpiod_configure_flags(desc, propname, lflags, dflags); |
---|
| 358 | + if (ret < 0) { |
---|
| 359 | + gpiod_put(desc); |
---|
| 360 | + return ERR_PTR(ret); |
---|
| 361 | + } |
---|
| 362 | + |
---|
| 363 | + return desc; |
---|
| 364 | +} |
---|
| 365 | +EXPORT_SYMBOL_GPL(gpiod_get_from_of_node); |
---|
160 | 366 | |
---|
161 | 367 | /* |
---|
162 | 368 | * The SPI GPIO bindings happened before we managed to establish that GPIO |
---|
.. | .. |
---|
186 | 392 | |
---|
187 | 393 | desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags); |
---|
188 | 394 | return desc; |
---|
| 395 | +} |
---|
| 396 | + |
---|
| 397 | +/* |
---|
| 398 | + * The old Freescale bindings use simply "gpios" as name for the chip select |
---|
| 399 | + * lines rather than "cs-gpios" like all other SPI hardware. Account for this |
---|
| 400 | + * with a special quirk. |
---|
| 401 | + */ |
---|
| 402 | +static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev, |
---|
| 403 | + const char *con_id, |
---|
| 404 | + unsigned int idx, |
---|
| 405 | + unsigned long *flags) |
---|
| 406 | +{ |
---|
| 407 | + struct device_node *np = dev->of_node; |
---|
| 408 | + |
---|
| 409 | + if (!IS_ENABLED(CONFIG_SPI_MASTER)) |
---|
| 410 | + return ERR_PTR(-ENOENT); |
---|
| 411 | + |
---|
| 412 | + /* Allow this specifically for Freescale and PPC devices */ |
---|
| 413 | + if (!of_device_is_compatible(np, "fsl,spi") && |
---|
| 414 | + !of_device_is_compatible(np, "aeroflexgaisler,spictrl") && |
---|
| 415 | + !of_device_is_compatible(np, "ibm,ppc4xx-spi")) |
---|
| 416 | + return ERR_PTR(-ENOENT); |
---|
| 417 | + /* Allow only if asking for "cs-gpios" */ |
---|
| 418 | + if (!con_id || strcmp(con_id, "cs")) |
---|
| 419 | + return ERR_PTR(-ENOENT); |
---|
| 420 | + |
---|
| 421 | + /* |
---|
| 422 | + * While all other SPI controllers use "cs-gpios" the Freescale |
---|
| 423 | + * uses just "gpios" so translate to that when "cs-gpios" is |
---|
| 424 | + * requested. |
---|
| 425 | + */ |
---|
| 426 | + return of_find_gpio(dev, NULL, idx, flags); |
---|
189 | 427 | } |
---|
190 | 428 | |
---|
191 | 429 | /* |
---|
.. | .. |
---|
220 | 458 | return desc; |
---|
221 | 459 | } |
---|
222 | 460 | |
---|
| 461 | +static struct gpio_desc *of_find_arizona_gpio(struct device *dev, |
---|
| 462 | + const char *con_id, |
---|
| 463 | + enum of_gpio_flags *of_flags) |
---|
| 464 | +{ |
---|
| 465 | + if (!IS_ENABLED(CONFIG_MFD_ARIZONA)) |
---|
| 466 | + return ERR_PTR(-ENOENT); |
---|
| 467 | + |
---|
| 468 | + if (!con_id || strcmp(con_id, "wlf,reset")) |
---|
| 469 | + return ERR_PTR(-ENOENT); |
---|
| 470 | + |
---|
| 471 | + return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags); |
---|
| 472 | +} |
---|
| 473 | + |
---|
| 474 | +static struct gpio_desc *of_find_usb_gpio(struct device *dev, |
---|
| 475 | + const char *con_id, |
---|
| 476 | + enum of_gpio_flags *of_flags) |
---|
| 477 | +{ |
---|
| 478 | + /* |
---|
| 479 | + * Currently this USB quirk is only for the Fairchild FUSB302 host which is using |
---|
| 480 | + * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios" |
---|
| 481 | + * suffix. |
---|
| 482 | + */ |
---|
| 483 | + if (!IS_ENABLED(CONFIG_TYPEC_FUSB302)) |
---|
| 484 | + return ERR_PTR(-ENOENT); |
---|
| 485 | + |
---|
| 486 | + if (!con_id || strcmp(con_id, "fcs,int_n")) |
---|
| 487 | + return ERR_PTR(-ENOENT); |
---|
| 488 | + |
---|
| 489 | + return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags); |
---|
| 490 | +} |
---|
| 491 | + |
---|
223 | 492 | struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
---|
224 | | - unsigned int idx, |
---|
225 | | - enum gpio_lookup_flags *flags) |
---|
| 493 | + unsigned int idx, unsigned long *flags) |
---|
226 | 494 | { |
---|
227 | 495 | char prop_name[32]; /* 32 is max size of property name */ |
---|
228 | 496 | enum of_gpio_flags of_flags; |
---|
.. | .. |
---|
240 | 508 | |
---|
241 | 509 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
---|
242 | 510 | &of_flags); |
---|
243 | | - /* |
---|
244 | | - * -EPROBE_DEFER in our case means that we found a |
---|
245 | | - * valid GPIO property, but no controller has been |
---|
246 | | - * registered so far. |
---|
247 | | - * |
---|
248 | | - * This means we don't need to look any further for |
---|
249 | | - * alternate name conventions, and we should really |
---|
250 | | - * preserve the return code for our user to be able to |
---|
251 | | - * retry probing later. |
---|
252 | | - */ |
---|
253 | | - if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER) |
---|
254 | | - return desc; |
---|
255 | 511 | |
---|
256 | | - if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) |
---|
| 512 | + if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT) |
---|
257 | 513 | break; |
---|
258 | 514 | } |
---|
259 | 515 | |
---|
260 | | - /* Special handling for SPI GPIOs if used */ |
---|
261 | | - if (IS_ERR(desc)) |
---|
| 516 | + if (PTR_ERR(desc) == -ENOENT) { |
---|
| 517 | + /* Special handling for SPI GPIOs if used */ |
---|
262 | 518 | desc = of_find_spi_gpio(dev, con_id, &of_flags); |
---|
| 519 | + } |
---|
263 | 520 | |
---|
264 | | - /* Special handling for regulator GPIOs if used */ |
---|
265 | | - if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) |
---|
| 521 | + if (PTR_ERR(desc) == -ENOENT) { |
---|
| 522 | + /* This quirk looks up flags and all */ |
---|
| 523 | + desc = of_find_spi_cs_gpio(dev, con_id, idx, flags); |
---|
| 524 | + if (!IS_ERR(desc)) |
---|
| 525 | + return desc; |
---|
| 526 | + } |
---|
| 527 | + |
---|
| 528 | + if (PTR_ERR(desc) == -ENOENT) { |
---|
| 529 | + /* Special handling for regulator GPIOs if used */ |
---|
266 | 530 | desc = of_find_regulator_gpio(dev, con_id, &of_flags); |
---|
| 531 | + } |
---|
| 532 | + |
---|
| 533 | + if (PTR_ERR(desc) == -ENOENT) |
---|
| 534 | + desc = of_find_arizona_gpio(dev, con_id, &of_flags); |
---|
| 535 | + |
---|
| 536 | + if (PTR_ERR(desc) == -ENOENT) |
---|
| 537 | + desc = of_find_usb_gpio(dev, con_id, &of_flags); |
---|
267 | 538 | |
---|
268 | 539 | if (IS_ERR(desc)) |
---|
269 | 540 | return desc; |
---|
.. | .. |
---|
281 | 552 | if (of_flags & OF_GPIO_TRANSITORY) |
---|
282 | 553 | *flags |= GPIO_TRANSITORY; |
---|
283 | 554 | |
---|
| 555 | + if (of_flags & OF_GPIO_PULL_UP) |
---|
| 556 | + *flags |= GPIO_PULL_UP; |
---|
| 557 | + if (of_flags & OF_GPIO_PULL_DOWN) |
---|
| 558 | + *flags |= GPIO_PULL_DOWN; |
---|
| 559 | + |
---|
284 | 560 | return desc; |
---|
285 | 561 | } |
---|
286 | 562 | |
---|
.. | .. |
---|
290 | 566 | * @chip: GPIO chip whose hog is parsed |
---|
291 | 567 | * @idx: Index of the GPIO to parse |
---|
292 | 568 | * @name: GPIO line name |
---|
293 | | - * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
---|
294 | | - * of_parse_own_gpio() |
---|
| 569 | + * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
---|
| 570 | + * of_find_gpio() or of_parse_own_gpio() |
---|
295 | 571 | * @dflags: gpiod_flags - optional GPIO initialization flags |
---|
296 | 572 | * |
---|
297 | 573 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
---|
.. | .. |
---|
300 | 576 | static struct gpio_desc *of_parse_own_gpio(struct device_node *np, |
---|
301 | 577 | struct gpio_chip *chip, |
---|
302 | 578 | unsigned int idx, const char **name, |
---|
303 | | - enum gpio_lookup_flags *lflags, |
---|
| 579 | + unsigned long *lflags, |
---|
304 | 580 | enum gpiod_flags *dflags) |
---|
305 | 581 | { |
---|
306 | 582 | struct device_node *chip_np; |
---|
.. | .. |
---|
316 | 592 | return ERR_PTR(-EINVAL); |
---|
317 | 593 | |
---|
318 | 594 | xlate_flags = 0; |
---|
319 | | - *lflags = 0; |
---|
| 595 | + *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
---|
320 | 596 | *dflags = 0; |
---|
321 | 597 | |
---|
322 | 598 | ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); |
---|
.. | .. |
---|
341 | 617 | *lflags |= GPIO_ACTIVE_LOW; |
---|
342 | 618 | if (xlate_flags & OF_GPIO_TRANSITORY) |
---|
343 | 619 | *lflags |= GPIO_TRANSITORY; |
---|
| 620 | + if (xlate_flags & OF_GPIO_PULL_UP) |
---|
| 621 | + *lflags |= GPIO_PULL_UP; |
---|
| 622 | + if (xlate_flags & OF_GPIO_PULL_DOWN) |
---|
| 623 | + *lflags |= GPIO_PULL_DOWN; |
---|
344 | 624 | |
---|
345 | 625 | if (of_property_read_bool(np, "input")) |
---|
346 | 626 | *dflags |= GPIOD_IN; |
---|
.. | .. |
---|
349 | 629 | else if (of_property_read_bool(np, "output-high")) |
---|
350 | 630 | *dflags |= GPIOD_OUT_HIGH; |
---|
351 | 631 | else { |
---|
352 | | - pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n", |
---|
353 | | - desc_to_gpio(desc), np->name); |
---|
| 632 | + pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", |
---|
| 633 | + desc_to_gpio(desc), np); |
---|
354 | 634 | return ERR_PTR(-EINVAL); |
---|
355 | 635 | } |
---|
356 | 636 | |
---|
.. | .. |
---|
358 | 638 | *name = np->name; |
---|
359 | 639 | |
---|
360 | 640 | return desc; |
---|
| 641 | +} |
---|
| 642 | + |
---|
| 643 | +/** |
---|
| 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 |
---|
| 647 | + * |
---|
| 648 | + * Returns error if it fails otherwise 0 on success. |
---|
| 649 | + */ |
---|
| 650 | +static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) |
---|
| 651 | +{ |
---|
| 652 | + enum gpiod_flags dflags; |
---|
| 653 | + struct gpio_desc *desc; |
---|
| 654 | + unsigned long lflags; |
---|
| 655 | + const char *name; |
---|
| 656 | + unsigned int i; |
---|
| 657 | + int ret; |
---|
| 658 | + |
---|
| 659 | + for (i = 0;; i++) { |
---|
| 660 | + desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags); |
---|
| 661 | + if (IS_ERR(desc)) |
---|
| 662 | + break; |
---|
| 663 | + |
---|
| 664 | + ret = gpiod_hog(desc, name, lflags, dflags); |
---|
| 665 | + if (ret < 0) |
---|
| 666 | + return ret; |
---|
| 667 | + |
---|
| 668 | +#ifdef CONFIG_OF_DYNAMIC |
---|
| 669 | + desc->hog = hog; |
---|
| 670 | +#endif |
---|
| 671 | + } |
---|
| 672 | + |
---|
| 673 | + return 0; |
---|
361 | 674 | } |
---|
362 | 675 | |
---|
363 | 676 | /** |
---|
.. | .. |
---|
370 | 683 | */ |
---|
371 | 684 | static int of_gpiochip_scan_gpios(struct gpio_chip *chip) |
---|
372 | 685 | { |
---|
373 | | - struct gpio_desc *desc = NULL; |
---|
374 | 686 | struct device_node *np; |
---|
375 | | - const char *name; |
---|
376 | | - enum gpio_lookup_flags lflags; |
---|
377 | | - enum gpiod_flags dflags; |
---|
378 | | - unsigned int i; |
---|
379 | 687 | int ret; |
---|
380 | 688 | |
---|
381 | 689 | for_each_available_child_of_node(chip->of_node, np) { |
---|
382 | 690 | if (!of_property_read_bool(np, "gpio-hog")) |
---|
383 | 691 | continue; |
---|
384 | 692 | |
---|
385 | | - for (i = 0;; i++) { |
---|
386 | | - desc = of_parse_own_gpio(np, chip, i, &name, &lflags, |
---|
387 | | - &dflags); |
---|
388 | | - if (IS_ERR(desc)) |
---|
389 | | - break; |
---|
390 | | - |
---|
391 | | - ret = gpiod_hog(desc, name, lflags, dflags); |
---|
392 | | - if (ret < 0) { |
---|
393 | | - of_node_put(np); |
---|
394 | | - return ret; |
---|
395 | | - } |
---|
| 693 | + ret = of_gpiochip_add_hog(chip, np); |
---|
| 694 | + if (ret < 0) { |
---|
| 695 | + of_node_put(np); |
---|
| 696 | + return ret; |
---|
396 | 697 | } |
---|
| 698 | + |
---|
| 699 | + of_node_set_flag(np, OF_POPULATED); |
---|
397 | 700 | } |
---|
398 | 701 | |
---|
399 | 702 | return 0; |
---|
400 | 703 | } |
---|
| 704 | + |
---|
| 705 | +#ifdef CONFIG_OF_DYNAMIC |
---|
| 706 | +/** |
---|
| 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 |
---|
| 710 | + */ |
---|
| 711 | +static void of_gpiochip_remove_hog(struct gpio_chip *chip, |
---|
| 712 | + struct device_node *hog) |
---|
| 713 | +{ |
---|
| 714 | + struct gpio_desc *descs = chip->gpiodev->descs; |
---|
| 715 | + unsigned int i; |
---|
| 716 | + |
---|
| 717 | + for (i = 0; i < chip->ngpio; i++) { |
---|
| 718 | + if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) && |
---|
| 719 | + descs[i].hog == hog) |
---|
| 720 | + gpiochip_free_own_desc(&descs[i]); |
---|
| 721 | + } |
---|
| 722 | +} |
---|
| 723 | + |
---|
| 724 | +static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) |
---|
| 725 | +{ |
---|
| 726 | + return chip->gpiodev->dev.of_node == data; |
---|
| 727 | +} |
---|
| 728 | + |
---|
| 729 | +static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np) |
---|
| 730 | +{ |
---|
| 731 | + return gpiochip_find(np, of_gpiochip_match_node); |
---|
| 732 | +} |
---|
| 733 | + |
---|
| 734 | +static int of_gpio_notify(struct notifier_block *nb, unsigned long action, |
---|
| 735 | + void *arg) |
---|
| 736 | +{ |
---|
| 737 | + struct of_reconfig_data *rd = arg; |
---|
| 738 | + struct gpio_chip *chip; |
---|
| 739 | + int ret; |
---|
| 740 | + |
---|
| 741 | + /* |
---|
| 742 | + * This only supports adding and removing complete gpio-hog nodes. |
---|
| 743 | + * Modifying an existing gpio-hog node is not supported (except for |
---|
| 744 | + * changing its "status" property, which is treated the same as |
---|
| 745 | + * addition/removal). |
---|
| 746 | + */ |
---|
| 747 | + switch (of_reconfig_get_state_change(action, arg)) { |
---|
| 748 | + case OF_RECONFIG_CHANGE_ADD: |
---|
| 749 | + if (!of_property_read_bool(rd->dn, "gpio-hog")) |
---|
| 750 | + return NOTIFY_OK; /* not for us */ |
---|
| 751 | + |
---|
| 752 | + if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) |
---|
| 753 | + return NOTIFY_OK; |
---|
| 754 | + |
---|
| 755 | + chip = of_find_gpiochip_by_node(rd->dn->parent); |
---|
| 756 | + if (chip == NULL) |
---|
| 757 | + return NOTIFY_OK; /* not for us */ |
---|
| 758 | + |
---|
| 759 | + ret = of_gpiochip_add_hog(chip, rd->dn); |
---|
| 760 | + if (ret < 0) { |
---|
| 761 | + pr_err("%s: failed to add hogs for %pOF\n", __func__, |
---|
| 762 | + rd->dn); |
---|
| 763 | + of_node_clear_flag(rd->dn, OF_POPULATED); |
---|
| 764 | + return notifier_from_errno(ret); |
---|
| 765 | + } |
---|
| 766 | + break; |
---|
| 767 | + |
---|
| 768 | + case OF_RECONFIG_CHANGE_REMOVE: |
---|
| 769 | + if (!of_node_check_flag(rd->dn, OF_POPULATED)) |
---|
| 770 | + return NOTIFY_OK; /* already depopulated */ |
---|
| 771 | + |
---|
| 772 | + chip = of_find_gpiochip_by_node(rd->dn->parent); |
---|
| 773 | + if (chip == NULL) |
---|
| 774 | + return NOTIFY_OK; /* not for us */ |
---|
| 775 | + |
---|
| 776 | + of_gpiochip_remove_hog(chip, rd->dn); |
---|
| 777 | + of_node_clear_flag(rd->dn, OF_POPULATED); |
---|
| 778 | + break; |
---|
| 779 | + } |
---|
| 780 | + |
---|
| 781 | + return NOTIFY_OK; |
---|
| 782 | +} |
---|
| 783 | + |
---|
| 784 | +struct notifier_block gpio_of_notifier = { |
---|
| 785 | + .notifier_call = of_gpio_notify, |
---|
| 786 | +}; |
---|
| 787 | +#endif /* CONFIG_OF_DYNAMIC */ |
---|
401 | 788 | |
---|
402 | 789 | /** |
---|
403 | 790 | * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags |
---|
.. | .. |
---|
409 | 796 | * GPIO chips. This function performs only one sanity check: whether GPIO |
---|
410 | 797 | * is less than ngpios (that is specified in the gpio_chip). |
---|
411 | 798 | */ |
---|
412 | | -int of_gpio_simple_xlate(struct gpio_chip *gc, |
---|
413 | | - const struct of_phandle_args *gpiospec, u32 *flags) |
---|
| 799 | +static int of_gpio_simple_xlate(struct gpio_chip *gc, |
---|
| 800 | + const struct of_phandle_args *gpiospec, |
---|
| 801 | + u32 *flags) |
---|
414 | 802 | { |
---|
415 | 803 | /* |
---|
416 | 804 | * We're discouraging gpio_cells < 2, since that way you'll have to |
---|
.. | .. |
---|
434 | 822 | |
---|
435 | 823 | return gpiospec->args[0]; |
---|
436 | 824 | } |
---|
437 | | -EXPORT_SYMBOL(of_gpio_simple_xlate); |
---|
438 | 825 | |
---|
439 | 826 | /** |
---|
440 | 827 | * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) |
---|
.. | .. |
---|
476 | 863 | if (mm_gc->save_regs) |
---|
477 | 864 | mm_gc->save_regs(mm_gc); |
---|
478 | 865 | |
---|
479 | | - mm_gc->gc.of_node = np; |
---|
| 866 | + of_node_put(mm_gc->gc.of_node); |
---|
| 867 | + mm_gc->gc.of_node = of_node_get(np); |
---|
480 | 868 | |
---|
481 | 869 | ret = gpiochip_add_data(gc, data); |
---|
482 | 870 | if (ret) |
---|
.. | .. |
---|
484 | 872 | |
---|
485 | 873 | return 0; |
---|
486 | 874 | err2: |
---|
| 875 | + of_node_put(np); |
---|
487 | 876 | iounmap(mm_gc->regs); |
---|
488 | 877 | err1: |
---|
489 | 878 | kfree(gc->label); |
---|
.. | .. |
---|
491 | 880 | pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); |
---|
492 | 881 | return ret; |
---|
493 | 882 | } |
---|
494 | | -EXPORT_SYMBOL(of_mm_gpiochip_add_data); |
---|
| 883 | +EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data); |
---|
495 | 884 | |
---|
496 | 885 | /** |
---|
497 | 886 | * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) |
---|
.. | .. |
---|
508 | 897 | iounmap(mm_gc->regs); |
---|
509 | 898 | kfree(gc->label); |
---|
510 | 899 | } |
---|
511 | | -EXPORT_SYMBOL(of_mm_gpiochip_remove); |
---|
| 900 | +EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove); |
---|
512 | 901 | |
---|
513 | 902 | static void of_gpiochip_init_valid_mask(struct gpio_chip *chip) |
---|
514 | 903 | { |
---|
.. | .. |
---|
525 | 914 | i, &start); |
---|
526 | 915 | of_property_read_u32_index(np, "gpio-reserved-ranges", |
---|
527 | 916 | i + 1, &count); |
---|
528 | | - if (start >= chip->ngpio || start + count >= chip->ngpio) |
---|
| 917 | + if (start >= chip->ngpio || start + count > chip->ngpio) |
---|
529 | 918 | continue; |
---|
530 | 919 | |
---|
531 | 920 | bitmap_clear(chip->valid_mask, start, count); |
---|
.. | .. |
---|
620 | 1009 | |
---|
621 | 1010 | int of_gpiochip_add(struct gpio_chip *chip) |
---|
622 | 1011 | { |
---|
623 | | - int status; |
---|
| 1012 | + int ret; |
---|
624 | 1013 | |
---|
625 | 1014 | if (!chip->of_node) |
---|
626 | 1015 | return 0; |
---|
.. | .. |
---|
635 | 1024 | |
---|
636 | 1025 | of_gpiochip_init_valid_mask(chip); |
---|
637 | 1026 | |
---|
638 | | - status = of_gpiochip_add_pin_range(chip); |
---|
639 | | - if (status) |
---|
640 | | - return status; |
---|
641 | | - |
---|
642 | | - /* If the chip defines names itself, these take precedence */ |
---|
643 | | - if (!chip->names) |
---|
644 | | - devprop_gpiochip_set_names(chip, |
---|
645 | | - of_fwnode_handle(chip->of_node)); |
---|
| 1027 | + ret = of_gpiochip_add_pin_range(chip); |
---|
| 1028 | + if (ret) |
---|
| 1029 | + return ret; |
---|
646 | 1030 | |
---|
647 | 1031 | of_node_get(chip->of_node); |
---|
648 | 1032 | |
---|
649 | | - status = of_gpiochip_scan_gpios(chip); |
---|
650 | | - if (status) { |
---|
| 1033 | + ret = of_gpiochip_scan_gpios(chip); |
---|
| 1034 | + if (ret) |
---|
651 | 1035 | of_node_put(chip->of_node); |
---|
652 | | - gpiochip_remove_pin_ranges(chip); |
---|
653 | | - } |
---|
654 | 1036 | |
---|
655 | | - return status; |
---|
| 1037 | + return ret; |
---|
656 | 1038 | } |
---|
657 | 1039 | |
---|
658 | 1040 | void of_gpiochip_remove(struct gpio_chip *chip) |
---|
659 | 1041 | { |
---|
660 | | - gpiochip_remove_pin_ranges(chip); |
---|
661 | 1042 | of_node_put(chip->of_node); |
---|
662 | 1043 | } |
---|
| 1044 | + |
---|
| 1045 | +void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) |
---|
| 1046 | +{ |
---|
| 1047 | + /* If the gpiochip has an assigned OF node this takes precedence */ |
---|
| 1048 | + if (gc->of_node) |
---|
| 1049 | + gdev->dev.of_node = gc->of_node; |
---|
| 1050 | + else |
---|
| 1051 | + gc->of_node = gdev->dev.of_node; |
---|
| 1052 | + if (gdev->dev.of_node) |
---|
| 1053 | + gdev->dev.fwnode = of_fwnode_handle(gdev->dev.of_node); |
---|
| 1054 | +} |
---|