Commit | Line | Data |
---|---|---|
27038c3e | 1 | // SPDX-License-Identifier: GPL-2.0+ |
863fbf49 AV |
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> | |
863fbf49 AV |
8 | */ |
9 | ||
2e13cba8 | 10 | #include <linux/device.h> |
bea4dbee | 11 | #include <linux/err.h> |
863fbf49 | 12 | #include <linux/errno.h> |
2e13cba8 | 13 | #include <linux/module.h> |
863fbf49 | 14 | #include <linux/io.h> |
af8b6375 | 15 | #include <linux/gpio/consumer.h> |
863fbf49 | 16 | #include <linux/of.h> |
2e13cba8 | 17 | #include <linux/of_address.h> |
863fbf49 | 18 | #include <linux/of_gpio.h> |
f23f1516 | 19 | #include <linux/pinctrl/pinctrl.h> |
2e13cba8 | 20 | #include <linux/slab.h> |
f625d460 | 21 | #include <linux/gpio/machine.h> |
863fbf49 | 22 | |
1bd6b601 | 23 | #include "gpiolib.h" |
f626d6df LW |
24 | #include "gpiolib-of.h" |
25 | ||
eed5a3bf AS |
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 | ||
c7835652 DT |
41 | /** |
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 | ||
71b8f600 LW |
67 | /** |
68 | * of_gpio_spi_cs_get_count() - special GPIO counting for SPI | |
c5a66b97 LJ |
69 | * @dev: Consuming device |
70 | * @con_id: Function within the GPIO consumer | |
71 | * | |
71b8f600 | 72 | * Some elder GPIO controllers need special quirks. Currently we handle |
47267732 | 73 | * the Freescale and PPC GPIO controller with bindings that doesn't use the |
71b8f600 LW |
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 | */ | |
a1f4c96b | 79 | static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id) |
71b8f600 LW |
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") && | |
47267732 LW |
88 | !of_device_is_compatible(np, "aeroflexgaisler,spictrl") && |
89 | !of_device_is_compatible(np, "ibm,ppc4xx-spi")) | |
71b8f600 LW |
90 | return 0; |
91 | return of_gpio_named_count(np, "gpios"); | |
92 | } | |
93 | ||
f626d6df LW |
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 | ||
71b8f600 LW |
100 | ret = of_gpio_spi_cs_get_count(dev, con_id); |
101 | if (ret > 0) | |
102 | return ret; | |
103 | ||
f626d6df LW |
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 | } | |
af8b6375 | 118 | |
c7e9d398 | 119 | static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) |
3d0f7cf0 | 120 | { |
c7e9d398 MY |
121 | struct of_phandle_args *gpiospec = data; |
122 | ||
d59fdbc7 | 123 | return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) && |
d49b48f0 | 124 | chip->of_xlate && |
c7e9d398 | 125 | chip->of_xlate(chip, gpiospec, NULL) >= 0; |
762c2e46 | 126 | } |
3d0f7cf0 | 127 | |
c7e9d398 MY |
128 | static struct gpio_chip *of_find_gpiochip_by_xlate( |
129 | struct of_phandle_args *gpiospec) | |
762c2e46 | 130 | { |
c7e9d398 | 131 | return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); |
3d0f7cf0 | 132 | } |
3d0f7cf0 | 133 | |
99468c1a MY |
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) | |
3d0f7cf0 | 137 | { |
3d0f7cf0 GL |
138 | int ret; |
139 | ||
99468c1a MY |
140 | if (chip->of_gpio_n_cells != gpiospec->args_count) |
141 | return ERR_PTR(-EINVAL); | |
142 | ||
143 | ret = chip->of_xlate(chip, gpiospec, flags); | |
144 | if (ret < 0) | |
145 | return ERR_PTR(ret); | |
146 | ||
147 | return gpiochip_get_desc(chip, ret); | |
3d0f7cf0 GL |
148 | } |
149 | ||
e3186e36 DT |
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 | ||
99d18d42 DT |
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 | ||
34cb9352 DT |
206 | static void of_gpio_set_polarity_by_property(const struct device_node *np, |
207 | const char *propname, | |
208 | enum of_gpio_flags *flags) | |
a603a2b8 | 209 | { |
34cb9352 DT |
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 | |
b8b80348 DT |
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 | |
99d18d42 | 237 | |
a603a2b8 LW |
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 | */ | |
34cb9352 DT |
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 | } | |
a603a2b8 | 266 | } |
34cb9352 DT |
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 | ||
a603a2b8 LW |
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 | } | |
6953c57a LW |
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 | */ | |
da7f1349 | 293 | if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && |
a71a81e7 | 294 | of_property_read_bool(np, "cs-gpios")) { |
6953c57a LW |
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); | |
c1c04cea | 301 | if (ret) |
6953c57a LW |
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 | */ | |
e3186e36 DT |
316 | bool active_high = of_property_read_bool(child, |
317 | "spi-cs-high"); | |
318 | of_gpio_quirk_polarity(child, active_high, | |
319 | flags); | |
89fea04c | 320 | of_node_put(child); |
6953c57a LW |
321 | break; |
322 | } | |
323 | } | |
324 | } | |
edc1ef3f MB |
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; | |
a603a2b8 LW |
331 | } |
332 | ||
863fbf49 | 333 | /** |
af8b6375 | 334 | * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API |
863fbf49 | 335 | * @np: device node to get GPIO from |
a6b09191 | 336 | * @propname: property name containing gpio specifier(s) |
863fbf49 | 337 | * @index: index of the GPIO |
b908b53d | 338 | * @flags: a flags pointer to fill in |
863fbf49 | 339 | * |
af8b6375 | 340 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
b908b53d AV |
341 | * value on the error condition. If @flags is not NULL the function also fills |
342 | * in flags for the GPIO. | |
863fbf49 | 343 | */ |
e6ae9a83 | 344 | static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np, |
af8b6375 | 345 | const char *propname, int index, enum of_gpio_flags *flags) |
863fbf49 | 346 | { |
762c2e46 MY |
347 | struct of_phandle_args gpiospec; |
348 | struct gpio_chip *chip; | |
349 | struct gpio_desc *desc; | |
64b60e09 | 350 | int ret; |
3d0f7cf0 | 351 | |
c11e6f0f SB |
352 | ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, |
353 | &gpiospec); | |
64b60e09 | 354 | if (ret) { |
7eb6ce2f RH |
355 | pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", |
356 | __func__, propname, np, index); | |
af8b6375 | 357 | return ERR_PTR(ret); |
863fbf49 AV |
358 | } |
359 | ||
c7e9d398 | 360 | chip = of_find_gpiochip_by_xlate(&gpiospec); |
762c2e46 MY |
361 | if (!chip) { |
362 | desc = ERR_PTR(-EPROBE_DEFER); | |
363 | goto out; | |
364 | } | |
762c2e46 | 365 | |
99468c1a | 366 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); |
762c2e46 MY |
367 | if (IS_ERR(desc)) |
368 | goto out; | |
863fbf49 | 369 | |
605f2d34 | 370 | if (flags) |
89a5e15b | 371 | of_gpio_flags_quirks(np, propname, flags, index); |
a603a2b8 | 372 | |
7eb6ce2f RH |
373 | pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", |
374 | __func__, propname, np, index, | |
762c2e46 MY |
375 | PTR_ERR_OR_ZERO(desc)); |
376 | ||
377 | out: | |
378 | of_node_put(gpiospec.np); | |
379 | ||
380 | return desc; | |
863fbf49 | 381 | } |
863fbf49 | 382 | |
40fc56ee DT |
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) | |
f01d9075 AC |
394 | { |
395 | struct gpio_desc *desc; | |
396 | ||
40fc56ee | 397 | desc = of_get_named_gpiod_flags(np, propname, index, NULL); |
f01d9075 AC |
398 | |
399 | if (IS_ERR(desc)) | |
400 | return PTR_ERR(desc); | |
401 | else | |
402 | return desc_to_gpio(desc); | |
403 | } | |
40fc56ee | 404 | EXPORT_SYMBOL_GPL(of_get_named_gpio); |
f01d9075 | 405 | |
d9e7f0e3 DT |
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 | ||
b311c5cb | 436 | static struct gpio_desc *of_find_gpio_rename(struct device_node *np, |
e3023bf8 LW |
437 | const char *con_id, |
438 | unsigned int idx, | |
984914ec | 439 | enum of_gpio_flags *of_flags) |
e3023bf8 | 440 | { |
b311c5cb DT |
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[] = { | |
fbbbcd17 DT |
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 | |
b311c5cb DT |
458 | #if IS_ENABLED(CONFIG_MFD_ARIZONA) |
459 | { "wlf,reset", NULL, NULL }, | |
460 | #endif | |
eaf1a296 DT |
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 | |
9c2cc717 DT |
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 | |
fbbbcd17 DT |
476 | #if !IS_ENABLED(CONFIG_PCI_LANTIQ) |
477 | /* MIPS Lantiq PCI */ | |
478 | { "reset", "gpios-reset", "lantiq,pci-xway" }, | |
479 | #endif | |
307c593b | 480 | |
b311c5cb DT |
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 | */ | |
307c593b | 486 | #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1) |
b311c5cb | 487 | { "wlf,ldoena", NULL, NULL }, /* Arizona */ |
307c593b DT |
488 | #endif |
489 | #if IS_ENABLED(CONFIG_REGULATOR_WM8994) | |
b311c5cb DT |
490 | { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */ |
491 | { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */ | |
492 | #endif | |
e3023bf8 | 493 | |
944004eb DT |
494 | #if IS_ENABLED(CONFIG_SND_SOC_CS42L56) |
495 | { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" }, | |
496 | #endif | |
fbbbcd17 DT |
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 | |
307c593b | 504 | #if IS_ENABLED(CONFIG_SPI_GPIO) |
b311c5cb DT |
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" }, | |
307c593b | 513 | #endif |
e3023bf8 | 514 | |
b311c5cb DT |
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 | */ | |
307c593b | 521 | #if IS_ENABLED(CONFIG_SPI_FSL_SPI) |
b311c5cb DT |
522 | { "cs", "gpios", "fsl,spi" }, |
523 | { "cs", "gpios", "aeroflexgaisler,spictrl" }, | |
307c593b DT |
524 | #endif |
525 | #if IS_ENABLED(CONFIG_SPI_PPC4xx) | |
b311c5cb DT |
526 | { "cs", "gpios", "ibm,ppc4xx-spi" }, |
527 | #endif | |
307c593b | 528 | |
b311c5cb DT |
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 | |
6a537d48 | 536 | }; |
b311c5cb DT |
537 | struct gpio_desc *desc; |
538 | const char *legacy_id; | |
539 | unsigned int i; | |
6a537d48 LW |
540 | |
541 | if (!con_id) | |
542 | return ERR_PTR(-ENOENT); | |
543 | ||
b311c5cb DT |
544 | for (i = 0; i < ARRAY_SIZE(gpios); i++) { |
545 | if (strcmp(con_id, gpios[i].con_id)) | |
546 | continue; | |
6a537d48 | 547 | |
b311c5cb DT |
548 | if (gpios[i].compatible && |
549 | !of_device_is_compatible(np, gpios[i].compatible)) | |
550 | continue; | |
11c43bb0 | 551 | |
b311c5cb DT |
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 | } | |
6e24826d | 560 | |
b311c5cb | 561 | return ERR_PTR(-ENOENT); |
6e24826d LW |
562 | } |
563 | ||
326c3753 DT |
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 | ||
a2b5e207 DT |
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[] = { | |
b311c5cb | 601 | of_find_gpio_rename, |
326c3753 | 602 | of_find_mt2701_gpio, |
8b10ca2f | 603 | NULL |
a2b5e207 DT |
604 | }; |
605 | ||
07445ae1 | 606 | struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id, |
fed7026a | 607 | unsigned int idx, unsigned long *flags) |
ea713bc4 LW |
608 | { |
609 | char prop_name[32]; /* 32 is max size of property name */ | |
610 | enum of_gpio_flags of_flags; | |
a2b5e207 | 611 | const of_find_gpio_quirk *q; |
ea713bc4 LW |
612 | struct gpio_desc *desc; |
613 | unsigned int i; | |
614 | ||
c8582339 | 615 | /* Try GPIO property "foo-gpios" and "foo-gpio" */ |
ea713bc4 LW |
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 | ||
07445ae1 | 624 | desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags); |
6662ae6a | 625 | |
7b58696d | 626 | if (!gpiod_not_found(desc)) |
ea713bc4 LW |
627 | break; |
628 | } | |
629 | ||
a2b5e207 DT |
630 | /* Properly named GPIO was not found, try workarounds */ |
631 | for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++) | |
07445ae1 | 632 | desc = (*q)(np, con_id, idx, &of_flags); |
6e24826d | 633 | |
ea713bc4 LW |
634 | if (IS_ERR(desc)) |
635 | return desc; | |
636 | ||
d9e7f0e3 | 637 | *flags = of_convert_gpio_flags(of_flags); |
d449991c | 638 | |
ea713bc4 LW |
639 | return desc; |
640 | } | |
641 | ||
f625d460 | 642 | /** |
fd7337fd | 643 | * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API |
f625d460 | 644 | * @np: device node to get GPIO from |
be715343 | 645 | * @chip: GPIO chip whose hog is parsed |
a79fead5 | 646 | * @idx: Index of the GPIO to parse |
f625d460 | 647 | * @name: GPIO line name |
fed7026a AS |
648 | * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
649 | * of_find_gpio() or of_parse_own_gpio() | |
f625d460 BP |
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 | */ | |
fd7337fd | 655 | static struct gpio_desc *of_parse_own_gpio(struct device_node *np, |
be715343 | 656 | struct gpio_chip *chip, |
a79fead5 | 657 | unsigned int idx, const char **name, |
fed7026a | 658 | unsigned long *lflags, |
fd7337fd | 659 | enum gpiod_flags *dflags) |
f625d460 BP |
660 | { |
661 | struct device_node *chip_np; | |
662 | enum of_gpio_flags xlate_flags; | |
be715343 MY |
663 | struct of_phandle_args gpiospec; |
664 | struct gpio_desc *desc; | |
a79fead5 | 665 | unsigned int i; |
f625d460 | 666 | u32 tmp; |
3f9547e1 | 667 | int ret; |
f625d460 | 668 | |
70d0fc42 | 669 | chip_np = dev_of_node(&chip->gpiodev->dev); |
f625d460 BP |
670 | if (!chip_np) |
671 | return ERR_PTR(-EINVAL); | |
672 | ||
673 | xlate_flags = 0; | |
2d6c06f5 | 674 | *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
40941954 | 675 | *dflags = GPIOD_ASIS; |
f625d460 BP |
676 | |
677 | ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); | |
678 | if (ret) | |
679 | return ERR_PTR(ret); | |
680 | ||
be715343 MY |
681 | gpiospec.np = chip_np; |
682 | gpiospec.args_count = tmp; | |
f625d460 | 683 | |
a79fead5 GU |
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 | } | |
f625d460 | 690 | |
99468c1a | 691 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); |
be715343 MY |
692 | if (IS_ERR(desc)) |
693 | return desc; | |
f625d460 | 694 | |
d9e7f0e3 | 695 | *lflags = of_convert_gpio_flags(xlate_flags); |
f625d460 BP |
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 { | |
62cdcb6c RH |
704 | pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", |
705 | desc_to_gpio(desc), np); | |
f625d460 BP |
706 | return ERR_PTR(-EINVAL); |
707 | } | |
708 | ||
709 | if (name && of_property_read_string(np, "line-name", name)) | |
710 | *name = np->name; | |
711 | ||
be715343 | 712 | return desc; |
f625d460 BP |
713 | } |
714 | ||
bc21077e GU |
715 | /** |
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; | |
63636d95 GU |
739 | |
740 | #ifdef CONFIG_OF_DYNAMIC | |
741 | desc->hog = hog; | |
742 | #endif | |
bc21077e GU |
743 | } |
744 | ||
745 | return 0; | |
746 | } | |
747 | ||
f625d460 | 748 | /** |
fd7337fd | 749 | * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions |
f625d460 BP |
750 | * @chip: gpio chip to act on |
751 | * | |
752 | * This is only used by of_gpiochip_add to request/set GPIO initial | |
753 | * configuration. | |
ead066e6 | 754 | * It returns error if it fails otherwise 0 on success. |
f625d460 | 755 | */ |
dfbd379b | 756 | static int of_gpiochip_scan_gpios(struct gpio_chip *chip) |
f625d460 | 757 | { |
f625d460 | 758 | struct device_node *np; |
dfbd379b | 759 | int ret; |
f625d460 | 760 | |
70d0fc42 | 761 | for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) { |
f625d460 BP |
762 | if (!of_property_read_bool(np, "gpio-hog")) |
763 | continue; | |
764 | ||
bc21077e GU |
765 | ret = of_gpiochip_add_hog(chip, np); |
766 | if (ret < 0) { | |
767 | of_node_put(np); | |
768 | return ret; | |
09e258af | 769 | } |
63636d95 GU |
770 | |
771 | of_node_set_flag(np, OF_POPULATED); | |
f625d460 | 772 | } |
dfbd379b LD |
773 | |
774 | return 0; | |
f625d460 BP |
775 | } |
776 | ||
63636d95 GU |
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 | { | |
80c78fbe | 786 | struct gpio_desc *desc; |
63636d95 | 787 | |
57017edd | 788 | for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED) |
80c78fbe AS |
789 | if (desc->hog == hog) |
790 | gpiochip_free_own_desc(desc); | |
63636d95 GU |
791 | } |
792 | ||
793 | static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) | |
794 | { | |
597a8a88 | 795 | return device_match_of_node(&chip->gpiodev->dev, data); |
63636d95 GU |
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 | ||
863fbf49 | 858 | /** |
67049c50 | 859 | * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags |
a19e3da5 | 860 | * @gc: pointer to the gpio_chip structure |
67049c50 | 861 | * @gpiospec: GPIO specifier as found in the device tree |
b908b53d | 862 | * @flags: a flags pointer to fill in |
863fbf49 AV |
863 | * |
864 | * This is simple translation function, suitable for the most 1:1 mapped | |
67049c50 | 865 | * GPIO chips. This function performs only one sanity check: whether GPIO |
863fbf49 AV |
866 | * is less than ngpios (that is specified in the gpio_chip). |
867 | */ | |
b0c7e73b GU |
868 | static int of_gpio_simple_xlate(struct gpio_chip *gc, |
869 | const struct of_phandle_args *gpiospec, | |
870 | u32 *flags) | |
863fbf49 | 871 | { |
b908b53d AV |
872 | /* |
873 | * We're discouraging gpio_cells < 2, since that way you'll have to | |
20a8a968 | 874 | * write your own xlate function (that will have to retrieve the GPIO |
b908b53d AV |
875 | * number and the flags from a single gpio cell -- this is possible, |
876 | * but not recommended). | |
877 | */ | |
a19e3da5 | 878 | if (gc->of_gpio_n_cells < 2) { |
b908b53d AV |
879 | WARN_ON(1); |
880 | return -EINVAL; | |
881 | } | |
882 | ||
15c9a0ac GL |
883 | if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) |
884 | return -EINVAL; | |
885 | ||
6270d830 | 886 | if (gpiospec->args[0] >= gc->ngpio) |
863fbf49 AV |
887 | return -EINVAL; |
888 | ||
b908b53d | 889 | if (flags) |
15c9a0ac | 890 | *flags = gpiospec->args[1]; |
b908b53d | 891 | |
15c9a0ac | 892 | return gpiospec->args[0]; |
863fbf49 | 893 | } |
863fbf49 | 894 | |
a99cc668 AB |
895 | #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP) |
896 | #include <linux/gpio/legacy-of-mm-gpiochip.h> | |
863fbf49 | 897 | /** |
3208b0f0 | 898 | * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) |
863fbf49 AV |
899 | * @np: device node of the GPIO chip |
900 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure | |
3208b0f0 | 901 | * @data: driver data to store in the struct gpio_chip |
863fbf49 AV |
902 | * |
903 | * To use this function you should allocate and fill mm_gc with: | |
904 | * | |
905 | * 1) In the gpio_chip structure: | |
906 | * - all the callbacks | |
a19e3da5 AV |
907 | * - of_gpio_n_cells |
908 | * - of_xlate callback (optional) | |
863fbf49 AV |
909 | * |
910 | * 3) In the of_mm_gpio_chip structure: | |
911 | * - save_regs callback (optional) | |
912 | * | |
913 | * If succeeded, this function will map bank's memory and will | |
914 | * do all necessary work for you. Then you'll able to use .regs | |
915 | * to manage GPIOs from the callbacks. | |
916 | */ | |
3208b0f0 LW |
917 | int of_mm_gpiochip_add_data(struct device_node *np, |
918 | struct of_mm_gpio_chip *mm_gc, | |
919 | void *data) | |
863fbf49 AV |
920 | { |
921 | int ret = -ENOMEM; | |
a19e3da5 | 922 | struct gpio_chip *gc = &mm_gc->gc; |
863fbf49 | 923 | |
7eb6ce2f | 924 | gc->label = kasprintf(GFP_KERNEL, "%pOF", np); |
863fbf49 AV |
925 | if (!gc->label) |
926 | goto err0; | |
927 | ||
928 | mm_gc->regs = of_iomap(np, 0); | |
929 | if (!mm_gc->regs) | |
930 | goto err1; | |
931 | ||
21451155 | 932 | gc->base = -1; |
863fbf49 | 933 | |
863fbf49 AV |
934 | if (mm_gc->save_regs) |
935 | mm_gc->save_regs(mm_gc); | |
936 | ||
77289b2f AS |
937 | fwnode_handle_put(mm_gc->gc.fwnode); |
938 | mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np)); | |
863fbf49 | 939 | |
3208b0f0 | 940 | ret = gpiochip_add_data(gc, data); |
863fbf49 AV |
941 | if (ret) |
942 | goto err2; | |
943 | ||
863fbf49 AV |
944 | return 0; |
945 | err2: | |
5d07a692 | 946 | of_node_put(np); |
863fbf49 AV |
947 | iounmap(mm_gc->regs); |
948 | err1: | |
949 | kfree(gc->label); | |
950 | err0: | |
7eb6ce2f | 951 | pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); |
863fbf49 AV |
952 | return ret; |
953 | } | |
6d662455 | 954 | EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data); |
594fa265 | 955 | |
d621e8ba RRD |
956 | /** |
957 | * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) | |
958 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure | |
959 | */ | |
960 | void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) | |
961 | { | |
962 | struct gpio_chip *gc = &mm_gc->gc; | |
963 | ||
d621e8ba RRD |
964 | gpiochip_remove(gc); |
965 | iounmap(mm_gc->regs); | |
966 | kfree(gc->label); | |
967 | } | |
6d662455 | 968 | EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove); |
a99cc668 | 969 | #endif |
d621e8ba | 970 | |
f23f1516 | 971 | #ifdef CONFIG_PINCTRL |
28355f81 | 972 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) |
f23f1516 | 973 | { |
f23f1516 | 974 | struct of_phandle_args pinspec; |
1e63d7b9 | 975 | struct pinctrl_dev *pctldev; |
70d0fc42 | 976 | struct device_node *np; |
f23f1516 | 977 | int index = 0, ret; |
586a87e6 CR |
978 | const char *name; |
979 | static const char group_names_propname[] = "gpio-ranges-group-names"; | |
980 | struct property *group_names; | |
f23f1516 | 981 | |
70d0fc42 | 982 | np = dev_of_node(&chip->gpiodev->dev); |
f23f1516 | 983 | if (!np) |
28355f81 | 984 | return 0; |
f23f1516 | 985 | |
586a87e6 CR |
986 | group_names = of_find_property(np, group_names_propname, NULL); |
987 | ||
ad4e1a7c | 988 | for (;; index++) { |
d9fe0039 SW |
989 | ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, |
990 | index, &pinspec); | |
f23f1516 SH |
991 | if (ret) |
992 | break; | |
993 | ||
1e63d7b9 | 994 | pctldev = of_pinctrl_get(pinspec.np); |
602cf638 | 995 | of_node_put(pinspec.np); |
1e63d7b9 | 996 | if (!pctldev) |
28355f81 | 997 | return -EPROBE_DEFER; |
f23f1516 | 998 | |
586a87e6 CR |
999 | if (pinspec.args[2]) { |
1000 | if (group_names) { | |
72858602 | 1001 | of_property_read_string_index(np, |
586a87e6 CR |
1002 | group_names_propname, |
1003 | index, &name); | |
1004 | if (strlen(name)) { | |
7eb6ce2f RH |
1005 | pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", |
1006 | np); | |
586a87e6 CR |
1007 | break; |
1008 | } | |
1009 | } | |
1010 | /* npins != 0: linear range */ | |
1011 | ret = gpiochip_add_pin_range(chip, | |
1012 | pinctrl_dev_get_devname(pctldev), | |
1013 | pinspec.args[0], | |
1014 | pinspec.args[1], | |
1015 | pinspec.args[2]); | |
1016 | if (ret) | |
28355f81 | 1017 | return ret; |
586a87e6 CR |
1018 | } else { |
1019 | /* npins == 0: special range */ | |
1020 | if (pinspec.args[1]) { | |
7eb6ce2f RH |
1021 | pr_err("%pOF: Illegal gpio-range format.\n", |
1022 | np); | |
586a87e6 CR |
1023 | break; |
1024 | } | |
1025 | ||
1026 | if (!group_names) { | |
7eb6ce2f RH |
1027 | pr_err("%pOF: GPIO group range requested but no %s property.\n", |
1028 | np, group_names_propname); | |
586a87e6 CR |
1029 | break; |
1030 | } | |
1031 | ||
1032 | ret = of_property_read_string_index(np, | |
1033 | group_names_propname, | |
1034 | index, &name); | |
1035 | if (ret) | |
1036 | break; | |
1037 | ||
1038 | if (!strlen(name)) { | |
7eb6ce2f RH |
1039 | pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", |
1040 | np); | |
586a87e6 CR |
1041 | break; |
1042 | } | |
1043 | ||
1044 | ret = gpiochip_add_pingroup_range(chip, pctldev, | |
1045 | pinspec.args[0], name); | |
1046 | if (ret) | |
28355f81 | 1047 | return ret; |
586a87e6 | 1048 | } |
ad4e1a7c | 1049 | } |
28355f81 TV |
1050 | |
1051 | return 0; | |
f23f1516 SH |
1052 | } |
1053 | ||
f23f1516 | 1054 | #else |
28355f81 | 1055 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } |
f23f1516 SH |
1056 | #endif |
1057 | ||
28355f81 | 1058 | int of_gpiochip_add(struct gpio_chip *chip) |
391c970c | 1059 | { |
8afe8255 | 1060 | struct device_node *np; |
f0d1ab05 | 1061 | int ret; |
28355f81 | 1062 | |
70d0fc42 | 1063 | np = dev_of_node(&chip->gpiodev->dev); |
8afe8255 | 1064 | if (!np) |
28355f81 | 1065 | return 0; |
391c970c AV |
1066 | |
1067 | if (!chip->of_xlate) { | |
1068 | chip->of_gpio_n_cells = 2; | |
1069 | chip->of_xlate = of_gpio_simple_xlate; | |
1070 | } | |
1071 | ||
1020dfd1 MY |
1072 | if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) |
1073 | return -EINVAL; | |
1074 | ||
f0d1ab05 LW |
1075 | ret = of_gpiochip_add_pin_range(chip); |
1076 | if (ret) | |
1077 | return ret; | |
28355f81 | 1078 | |
8afe8255 | 1079 | fwnode_handle_get(chip->fwnode); |
f625d460 | 1080 | |
f0d1ab05 | 1081 | ret = of_gpiochip_scan_gpios(chip); |
2f4133bb | 1082 | if (ret) |
8afe8255 | 1083 | fwnode_handle_put(chip->fwnode); |
f7299d44 | 1084 | |
f0d1ab05 | 1085 | return ret; |
391c970c AV |
1086 | } |
1087 | ||
1088 | void of_gpiochip_remove(struct gpio_chip *chip) | |
1089 | { | |
8afe8255 | 1090 | fwnode_handle_put(chip->fwnode); |
391c970c | 1091 | } |