blob: 266352b1a966bc7079be3418e92633f08d5bbf0d [file] [log] [blame]
Vladimir Zapolskiy27038c32018-11-22 15:59:01 +02001// SPDX-License-Identifier: GPL-2.0+
Anton Vorontsov863fbf42008-04-11 23:06:45 +10002/*
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 Vorontsov863fbf42008-04-11 23:06:45 +10008 */
9
Grant Likely2e13cba2010-07-05 16:11:55 -060010#include <linux/device.h>
Sachin Kamatbea4dbe2014-01-27 12:15:08 +053011#include <linux/err.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100012#include <linux/errno.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060013#include <linux/module.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100014#include <linux/io.h>
Alexandre Courbotaf8b6372013-10-17 10:21:37 -070015#include <linux/gpio/consumer.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100016#include <linux/of.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060017#include <linux/of_address.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100018#include <linux/of_gpio.h>
Shiraz Hashimf23f1512012-10-27 15:21:36 +053019#include <linux/pinctrl/pinctrl.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060020#include <linux/slab.h>
Benoit Parrotf625d462015-02-02 11:44:44 -060021#include <linux/gpio/machine.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100022
Alexandre Courbot1bd6b602014-07-22 16:17:41 +090023#include "gpiolib.h"
Linus Walleijf626d6d2019-07-17 09:10:01 +020024#include "gpiolib-of.h"
25
Andy Shevchenkoeed5a3b2023-01-12 16:51:40 +020026/*
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 */
31enum 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 Walleij71b8f602019-11-28 09:37:17 +010041/**
Dmitry Torokhovc7835652022-12-19 11:20:13 -080042 * 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 */
61static 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 Walleij71b8f602019-11-28 09:37:17 +010068 * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
Lee Jonesc5a66b92020-06-30 14:33:36 +010069 * @dev: Consuming device
70 * @con_id: Function within the GPIO consumer
71 *
Linus Walleij71b8f602019-11-28 09:37:17 +010072 * Some elder GPIO controllers need special quirks. Currently we handle
Linus Walleij47267732020-07-14 09:22:26 +020073 * the Freescale and PPC GPIO controller with bindings that doesn't use the
Linus Walleij71b8f602019-11-28 09:37:17 +010074 * 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 */
YueHaibinga1f4c962019-11-30 09:28:28 +080079static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
Linus Walleij71b8f602019-11-28 09:37:17 +010080{
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 Walleij47267732020-07-14 09:22:26 +020088 !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
89 !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
Linus Walleij71b8f602019-11-28 09:37:17 +010090 return 0;
91 return of_gpio_named_count(np, "gpios");
92}
93
Linus Walleijf626d6d2019-07-17 09:10:01 +020094int 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 Walleij71b8f602019-11-28 09:37:17 +0100100 ret = of_gpio_spi_cs_get_count(dev, con_id);
101 if (ret > 0)
102 return ret;
103
Linus Walleijf626d6d2019-07-17 09:10:01 +0200104 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 Courbotaf8b6372013-10-17 10:21:37 -0700118
Masahiro Yamadac7e9d392016-10-25 10:47:44 +0900119static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
Grant Likely3d0f7cf02012-05-17 13:54:40 -0600120{
Masahiro Yamadac7e9d392016-10-25 10:47:44 +0900121 struct of_phandle_args *gpiospec = data;
122
Andy Shevchenkod59fdbc2022-11-03 22:30:05 +0200123 return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) &&
Vincent Whitchurchd49b48f2018-08-31 09:04:18 +0200124 chip->of_xlate &&
Masahiro Yamadac7e9d392016-10-25 10:47:44 +0900125 chip->of_xlate(chip, gpiospec, NULL) >= 0;
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900126}
Grant Likely3d0f7cf02012-05-17 13:54:40 -0600127
Masahiro Yamadac7e9d392016-10-25 10:47:44 +0900128static struct gpio_chip *of_find_gpiochip_by_xlate(
129 struct of_phandle_args *gpiospec)
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900130{
Masahiro Yamadac7e9d392016-10-25 10:47:44 +0900131 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
Grant Likely3d0f7cf02012-05-17 13:54:40 -0600132}
133
Masahiro Yamada99468c12016-06-14 19:07:07 +0900134static 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 Vorontsov863fbf42008-04-11 23:06:45 +1000138 int ret;
139
Masahiro Yamada99468c12016-06-14 19:07:07 +0900140 if (chip->of_gpio_n_cells != gpiospec->args_count)
141 return ERR_PTR(-EINVAL);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000142
Masahiro Yamada99468c12016-06-14 19:07:07 +0900143 ret = chip->of_xlate(chip, gpiospec, flags);
144 if (ret < 0)
145 return ERR_PTR(ret);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000146
Masahiro Yamada99468c12016-06-14 19:07:07 +0900147 return gpiochip_get_desc(chip, ret);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000148}
149
Dmitry Torokhove3186e32022-10-17 22:41:09 -0700150/*
151 * Overrides stated polarity of a gpio line and warns when there is a
152 * discrepancy.
153 */
154static 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 Torokhov99d18d42022-10-17 22:41:11 -0700172/*
173 * This quirk does static polarity overrides in cases where existing
174 * DTS specified incorrect polarity.
175 */
176static 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 Torokhov34cb9352022-10-29 21:40:46 -0700206static void of_gpio_set_polarity_by_property(const struct device_node *np,
207 const char *propname,
208 enum of_gpio_flags *flags)
Linus Walleija603a2b2017-12-30 16:26:36 +0100209{
Dmitry Torokhov34cb9352022-10-29 21:40:46 -0700210 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 Torokhovb8b80342022-10-29 21:40:47 -0700228#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 Torokhov99d18d42022-10-17 22:41:11 -0700237
Linus Walleija603a2b2017-12-30 16:26:36 +0100238 /*
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 Torokhov34cb9352022-10-29 21:40:46 -0700244#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 Walleija603a2b2017-12-30 16:26:36 +0100266 }
Dmitry Torokhov34cb9352022-10-29 21:40:46 -0700267}
268
269static 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 Walleija603a2b2017-12-30 16:26:36 +0100277 /*
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 Walleij6953c572018-09-04 11:01:58 +0200287
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 Walleijda7f1342019-07-15 22:45:29 +0200293 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
Geert Uytterhoevena71a81e2019-03-29 09:42:29 +0100294 of_property_read_bool(np, "cs-gpios")) {
Linus Walleij6953c572018-09-04 11:01:58 +0200295 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 Walleijc1c04ce2019-01-16 09:21:10 +0100301 if (ret)
Linus Walleij6953c572018-09-04 11:01:58 +0200302 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 Torokhove3186e32022-10-17 22:41:09 -0700316 bool active_high = of_property_read_bool(child,
317 "spi-cs-high");
318 of_gpio_quirk_polarity(child, active_high,
319 flags);
Nishka Dasgupta89fea042019-07-06 19:04:22 +0530320 of_node_put(child);
Linus Walleij6953c572018-09-04 11:01:58 +0200321 break;
322 }
323 }
324 }
Martin Blumenstingledc1ef32019-06-10 19:05:23 +0200325
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 Walleija603a2b2017-12-30 16:26:36 +0100331}
332
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000333/**
Alexandre Courbotaf8b6372013-10-17 10:21:37 -0700334 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000335 * @np: device node to get GPIO from
John Bonesioa6b09192011-06-27 16:49:57 -0700336 * @propname: property name containing gpio specifier(s)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000337 * @index: index of the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +0000338 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000339 *
Alexandre Courbotaf8b6372013-10-17 10:21:37 -0700340 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
Anton Vorontsovb908b532008-12-01 06:30:04 +0000341 * value on the error condition. If @flags is not NULL the function also fills
342 * in flags for the GPIO.
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000343 */
Krzysztof Kozlowskie6ae9a82021-07-28 16:42:28 +0200344static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
Alexandre Courbotaf8b6372013-10-17 10:21:37 -0700345 const char *propname, int index, enum of_gpio_flags *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000346{
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900347 struct of_phandle_args gpiospec;
348 struct gpio_chip *chip;
349 struct gpio_desc *desc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000350 int ret;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000351
Stephen Boydc11e6f02018-01-30 18:36:18 -0800352 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
353 &gpiospec);
Grant Likely3d0f7cf02012-05-17 13:54:40 -0600354 if (ret) {
Rob Herring7eb6ce22017-07-18 16:43:03 -0500355 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
356 __func__, propname, np, index);
Alexandre Courbotaf8b6372013-10-17 10:21:37 -0700357 return ERR_PTR(ret);
Grant Likely3d0f7cf02012-05-17 13:54:40 -0600358 }
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000359
Masahiro Yamadac7e9d392016-10-25 10:47:44 +0900360 chip = of_find_gpiochip_by_xlate(&gpiospec);
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900361 if (!chip) {
362 desc = ERR_PTR(-EPROBE_DEFER);
363 goto out;
364 }
Grant Likely3d0f7cf02012-05-17 13:54:40 -0600365
Masahiro Yamada99468c12016-06-14 19:07:07 +0900366 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900367 if (IS_ERR(desc))
368 goto out;
369
Linus Walleij605f2d32018-01-16 23:44:46 +0100370 if (flags)
Linus Walleij89a5e152018-12-17 22:36:25 +0100371 of_gpio_flags_quirks(np, propname, flags, index);
Linus Walleija603a2b2017-12-30 16:26:36 +0100372
Rob Herring7eb6ce22017-07-18 16:43:03 -0500373 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
374 __func__, propname, np, index,
Masahiro Yamada762c2e42016-06-14 19:07:06 +0900375 PTR_ERR_OR_ZERO(desc));
376
377out:
378 of_node_put(gpiospec.np);
379
380 return desc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000381}
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000382
Dmitry Torokhov40fc56e2022-12-19 11:20:15 -0800383/**
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 */
392int of_get_named_gpio(const struct device_node *np, const char *propname,
393 int index)
Alexandre Courbotf01d9072014-05-17 14:54:50 +0900394{
395 struct gpio_desc *desc;
396
Dmitry Torokhov40fc56e2022-12-19 11:20:15 -0800397 desc = of_get_named_gpiod_flags(np, propname, index, NULL);
Alexandre Courbotf01d9072014-05-17 14:54:50 +0900398
399 if (IS_ERR(desc))
400 return PTR_ERR(desc);
401 else
402 return desc_to_gpio(desc);
403}
Dmitry Torokhov40fc56e2022-12-19 11:20:15 -0800404EXPORT_SYMBOL_GPL(of_get_named_gpio);
Alexandre Courbotf01d9072014-05-17 14:54:50 +0900405
Dmitry Torokhovd9e7f0e2022-09-07 22:39:49 -0700406/* Converts gpio_lookup_flags into bitmask of GPIO_* values */
407static 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 Torokhovb311c5c2022-10-17 22:41:03 -0700436static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
Linus Walleije3023bf2018-12-13 15:22:47 +0100437 const char *con_id,
438 unsigned int idx,
Dmitry Torokhov984914e2022-09-07 22:39:47 -0700439 enum of_gpio_flags *of_flags)
Linus Walleije3023bf2018-12-13 15:22:47 +0100440{
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700441 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 Torokhovfbbbcd12022-10-17 22:41:05 -0700453#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 Torokhovb311c5c2022-10-17 22:41:03 -0700458#if IS_ENABLED(CONFIG_MFD_ARIZONA)
459 { "wlf,reset", NULL, NULL },
460#endif
Dmitry Torokhoveaf1a292022-10-17 22:41:08 -0700461#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 Torokhov9c2cc7172022-10-17 22:41:06 -0700466#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 Torokhovfbbbcd12022-10-17 22:41:05 -0700476#if !IS_ENABLED(CONFIG_PCI_LANTIQ)
477 /* MIPS Lantiq PCI */
478 { "reset", "gpios-reset", "lantiq,pci-xway" },
479#endif
Dmitry Torokhov307c5932022-10-17 22:41:04 -0700480
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700481 /*
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 Torokhov307c5932022-10-17 22:41:04 -0700486#if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700487 { "wlf,ldoena", NULL, NULL }, /* Arizona */
Dmitry Torokhov307c5932022-10-17 22:41:04 -0700488#endif
489#if IS_ENABLED(CONFIG_REGULATOR_WM8994)
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700490 { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */
491 { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */
492#endif
Linus Walleije3023bf2018-12-13 15:22:47 +0100493
Dmitry Torokhov944004e2022-10-17 22:41:07 -0700494#if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
495 { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" },
496#endif
Dmitry Torokhovfbbbcd12022-10-17 22:41:05 -0700497#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 Torokhov307c5932022-10-17 22:41:04 -0700504#if IS_ENABLED(CONFIG_SPI_GPIO)
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700505 /*
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 Torokhov307c5932022-10-17 22:41:04 -0700513#endif
Linus Walleije3023bf2018-12-13 15:22:47 +0100514
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700515 /*
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 Torokhov307c5932022-10-17 22:41:04 -0700521#if IS_ENABLED(CONFIG_SPI_FSL_SPI)
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700522 { "cs", "gpios", "fsl,spi" },
523 { "cs", "gpios", "aeroflexgaisler,spictrl" },
Dmitry Torokhov307c5932022-10-17 22:41:04 -0700524#endif
525#if IS_ENABLED(CONFIG_SPI_PPC4xx)
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700526 { "cs", "gpios", "ibm,ppc4xx-spi" },
527#endif
Dmitry Torokhov307c5932022-10-17 22:41:04 -0700528
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700529#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 Walleij6a537d42017-12-27 16:37:44 +0100536 };
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700537 struct gpio_desc *desc;
538 const char *legacy_id;
539 unsigned int i;
Linus Walleij6a537d42017-12-27 16:37:44 +0100540
541 if (!con_id)
542 return ERR_PTR(-ENOENT);
543
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700544 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
545 if (strcmp(con_id, gpios[i].con_id))
546 continue;
Linus Walleij6a537d42017-12-27 16:37:44 +0100547
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700548 if (gpios[i].compatible &&
549 !of_device_is_compatible(np, gpios[i].compatible))
550 continue;
Linus Walleij6a537d42017-12-27 16:37:44 +0100551
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700552 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 Torokhov11c43bb2019-09-11 00:52:05 -0700560
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700561 return ERR_PTR(-ENOENT);
Linus Walleij6e248262020-04-15 21:24:48 +0200562}
563
Dmitry Torokhov326c3752022-10-17 22:41:02 -0700564static 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 Torokhova2b5e202022-09-07 22:39:48 -0700596typedef 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);
600static const of_find_gpio_quirk of_find_gpio_quirks[] = {
Dmitry Torokhovb311c5c2022-10-17 22:41:03 -0700601 of_find_gpio_rename,
Dmitry Torokhov326c3752022-10-17 22:41:02 -0700602 of_find_mt2701_gpio,
Michael Walle8b10ca22022-09-16 13:01:18 +0200603 NULL
Dmitry Torokhova2b5e202022-09-07 22:39:48 -0700604};
605
Dmitry Torokhov07445ae2022-11-15 11:16:24 +0100606struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300607 unsigned int idx, unsigned long *flags)
Linus Walleijea713bc2016-10-03 10:09:40 +0200608{
609 char prop_name[32]; /* 32 is max size of property name */
610 enum of_gpio_flags of_flags;
Dmitry Torokhova2b5e202022-09-07 22:39:48 -0700611 const of_find_gpio_quirk *q;
Linus Walleijea713bc2016-10-03 10:09:40 +0200612 struct gpio_desc *desc;
613 unsigned int i;
614
Linus Walleijc8582332017-12-27 16:37:44 +0100615 /* Try GPIO property "foo-gpios" and "foo-gpio" */
Linus Walleijea713bc2016-10-03 10:09:40 +0200616 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 Torokhov07445ae2022-11-15 11:16:24 +0100624 desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags);
Maxime Ripard6662ae62018-02-21 09:11:00 +0100625
Andy Shevchenko7b586962020-10-21 17:25:28 +0300626 if (!gpiod_not_found(desc))
Linus Walleijea713bc2016-10-03 10:09:40 +0200627 break;
628 }
629
Dmitry Torokhova2b5e202022-09-07 22:39:48 -0700630 /* Properly named GPIO was not found, try workarounds */
631 for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
Dmitry Torokhov07445ae2022-11-15 11:16:24 +0100632 desc = (*q)(np, con_id, idx, &of_flags);
Linus Walleij6e248262020-04-15 21:24:48 +0200633
Linus Walleijea713bc2016-10-03 10:09:40 +0200634 if (IS_ERR(desc))
635 return desc;
636
Dmitry Torokhovd9e7f0e2022-09-07 22:39:49 -0700637 *flags = of_convert_gpio_flags(of_flags);
Thomas Petazzonid4499912019-02-07 17:28:58 +0100638
Linus Walleijea713bc2016-10-03 10:09:40 +0200639 return desc;
640}
641
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000642/**
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200643 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
Benoit Parrotf625d462015-02-02 11:44:44 -0600644 * @np: device node to get GPIO from
Masahiro Yamadabe715342016-06-14 19:07:04 +0900645 * @chip: GPIO chip whose hog is parsed
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100646 * @idx: Index of the GPIO to parse
Benoit Parrotf625d462015-02-02 11:44:44 -0600647 * @name: GPIO line name
Andy Shevchenkofed70262019-04-10 18:39:16 +0300648 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
649 * of_find_gpio() or of_parse_own_gpio()
Benoit Parrotf625d462015-02-02 11:44:44 -0600650 * @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 Pargmannfd7337f2015-08-14 16:10:58 +0200655static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
Masahiro Yamadabe715342016-06-14 19:07:04 +0900656 struct gpio_chip *chip,
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100657 unsigned int idx, const char **name,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300658 unsigned long *lflags,
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200659 enum gpiod_flags *dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -0600660{
661 struct device_node *chip_np;
662 enum of_gpio_flags xlate_flags;
Masahiro Yamadabe715342016-06-14 19:07:04 +0900663 struct of_phandle_args gpiospec;
664 struct gpio_desc *desc;
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100665 unsigned int i;
Benoit Parrotf625d462015-02-02 11:44:44 -0600666 u32 tmp;
Masahiro Yamada3f9547e2016-06-14 19:07:03 +0900667 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600668
Andy Shevchenko70d0fc42022-12-28 11:20:44 +0200669 chip_np = dev_of_node(&chip->gpiodev->dev);
Benoit Parrotf625d462015-02-02 11:44:44 -0600670 if (!chip_np)
671 return ERR_PTR(-EINVAL);
672
673 xlate_flags = 0;
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +0300674 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Andy Shevchenko40941952020-10-22 19:58:47 +0300675 *dflags = GPIOD_ASIS;
Benoit Parrotf625d462015-02-02 11:44:44 -0600676
677 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
678 if (ret)
679 return ERR_PTR(ret);
680
Masahiro Yamadabe715342016-06-14 19:07:04 +0900681 gpiospec.np = chip_np;
682 gpiospec.args_count = tmp;
Benoit Parrotf625d462015-02-02 11:44:44 -0600683
Geert Uytterhoevena79fead2016-12-19 19:21:34 +0100684 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 Parrotf625d462015-02-02 11:44:44 -0600690
Masahiro Yamada99468c12016-06-14 19:07:07 +0900691 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
Masahiro Yamadabe715342016-06-14 19:07:04 +0900692 if (IS_ERR(desc))
693 return desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600694
Dmitry Torokhovd9e7f0e2022-09-07 22:39:49 -0700695 *lflags = of_convert_gpio_flags(xlate_flags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600696
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 Herring62cdcb62018-08-27 20:52:19 -0500704 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
705 desc_to_gpio(desc), np);
Benoit Parrotf625d462015-02-02 11:44:44 -0600706 return ERR_PTR(-EINVAL);
707 }
708
709 if (name && of_property_read_string(np, "line-name", name))
710 *name = np->name;
711
Masahiro Yamadabe715342016-06-14 19:07:04 +0900712 return desc;
Benoit Parrotf625d462015-02-02 11:44:44 -0600713}
714
715/**
Geert Uytterhoevenbc210772020-02-20 14:01:48 +0100716 * 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 */
722static 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 Uytterhoeven63636d92020-02-20 14:01:49 +0100739
740#ifdef CONFIG_OF_DYNAMIC
741 desc->hog = hog;
742#endif
Geert Uytterhoevenbc210772020-02-20 14:01:48 +0100743 }
744
745 return 0;
746}
747
748/**
Markus Pargmannfd7337f2015-08-14 16:10:58 +0200749 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
Benoit Parrotf625d462015-02-02 11:44:44 -0600750 * @chip: gpio chip to act on
751 *
752 * This is only used by of_gpiochip_add to request/set GPIO initial
753 * configuration.
Geert Uytterhoevenead066e2017-06-01 12:30:01 +0200754 * It returns error if it fails otherwise 0 on success.
Benoit Parrotf625d462015-02-02 11:44:44 -0600755 */
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530756static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
Benoit Parrotf625d462015-02-02 11:44:44 -0600757{
Benoit Parrotf625d462015-02-02 11:44:44 -0600758 struct device_node *np;
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530759 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -0600760
Andy Shevchenko70d0fc42022-12-28 11:20:44 +0200761 for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) {
Benoit Parrotf625d462015-02-02 11:44:44 -0600762 if (!of_property_read_bool(np, "gpio-hog"))
763 continue;
764
Geert Uytterhoevenbc210772020-02-20 14:01:48 +0100765 ret = of_gpiochip_add_hog(chip, np);
766 if (ret < 0) {
767 of_node_put(np);
768 return ret;
Wei Yongjun09e258a2016-10-29 16:13:52 +0000769 }
Geert Uytterhoeven63636d92020-02-20 14:01:49 +0100770
771 of_node_set_flag(np, OF_POPULATED);
Benoit Parrotf625d462015-02-02 11:44:44 -0600772 }
Laxman Dewangandfbd3792016-03-11 19:13:22 +0530773
774 return 0;
Benoit Parrotf625d462015-02-02 11:44:44 -0600775}
776
Geert Uytterhoeven63636d92020-02-20 14:01:49 +0100777#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 */
783static void of_gpiochip_remove_hog(struct gpio_chip *chip,
784 struct device_node *hog)
785{
Andy Shevchenko80c78fb2022-02-01 17:27:56 +0200786 struct gpio_desc *desc;
Geert Uytterhoeven63636d92020-02-20 14:01:49 +0100787
Andy Shevchenko57017edd2022-04-08 21:18:50 +0300788 for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
Andy Shevchenko80c78fb2022-02-01 17:27:56 +0200789 if (desc->hog == hog)
790 gpiochip_free_own_desc(desc);
Geert Uytterhoeven63636d92020-02-20 14:01:49 +0100791}
792
793static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
794{
Andy Shevchenko597a8a82022-06-29 14:30:58 +0300795 return device_match_of_node(&chip->gpiodev->dev, data);
Geert Uytterhoeven63636d92020-02-20 14:01:49 +0100796}
797
798static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
799{
800 return gpiochip_find(np, of_gpiochip_match_node);
801}
802
803static 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
853struct notifier_block gpio_of_notifier = {
854 .notifier_call = of_gpio_notify,
855};
856#endif /* CONFIG_OF_DYNAMIC */
857
Benoit Parrotf625d462015-02-02 11:44:44 -0600858/**
Thierry Reding67049c52017-07-24 16:57:23 +0200859 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600860 * @gc: pointer to the gpio_chip structure
Thierry Reding67049c52017-07-24 16:57:23 +0200861 * @gpiospec: GPIO specifier as found in the device tree
Anton Vorontsovb908b532008-12-01 06:30:04 +0000862 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000863 *
864 * This is simple translation function, suitable for the most 1:1 mapped
Thierry Reding67049c52017-07-24 16:57:23 +0200865 * GPIO chips. This function performs only one sanity check: whether GPIO
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000866 * is less than ngpios (that is specified in the gpio_chip).
867 */
Geert Uytterhoevenb0c7e732019-09-06 10:45:37 +0200868static int of_gpio_simple_xlate(struct gpio_chip *gc,
869 const struct of_phandle_args *gpiospec,
870 u32 *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000871{
Anton Vorontsovb908b532008-12-01 06:30:04 +0000872 /*
873 * We're discouraging gpio_cells < 2, since that way you'll have to
Colin Cronin20a8a962015-05-18 11:41:43 -0700874 * write your own xlate function (that will have to retrieve the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +0000875 * number and the flags from a single gpio cell -- this is possible,
876 * but not recommended).
877 */
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600878 if (gc->of_gpio_n_cells < 2) {
Anton Vorontsovb908b532008-12-01 06:30:04 +0000879 WARN_ON(1);
880 return -EINVAL;
881 }
882
Grant Likely15c9a0a2011-12-12 09:25:57 -0700883 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
884 return -EINVAL;
885
Roland Stigge6270d832012-04-04 02:02:58 +0200886 if (gpiospec->args[0] >= gc->ngpio)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000887 return -EINVAL;
888
Anton Vorontsovb908b532008-12-01 06:30:04 +0000889 if (flags)
Grant Likely15c9a0a2011-12-12 09:25:57 -0700890 *flags = gpiospec->args[1];
Anton Vorontsovb908b532008-12-01 06:30:04 +0000891
Grant Likely15c9a0a2011-12-12 09:25:57 -0700892 return gpiospec->args[0];
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000893}
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000894
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000895/**
Linus Walleij3208b0f2015-12-04 15:13:53 +0100896 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000897 * @np: device node of the GPIO chip
898 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
Linus Walleij3208b0f2015-12-04 15:13:53 +0100899 * @data: driver data to store in the struct gpio_chip
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000900 *
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 Vorontsova19e3da2010-06-08 07:48:16 -0600905 * - of_gpio_n_cells
906 * - of_xlate callback (optional)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000907 *
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 Walleij3208b0f2015-12-04 15:13:53 +0100915int of_mm_gpiochip_add_data(struct device_node *np,
916 struct of_mm_gpio_chip *mm_gc,
917 void *data)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000918{
919 int ret = -ENOMEM;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600920 struct gpio_chip *gc = &mm_gc->gc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000921
Rob Herring7eb6ce22017-07-18 16:43:03 -0500922 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000923 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 Vorontsov21451152008-04-30 00:05:24 +1000930 gc->base = -1;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000931
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000932 if (mm_gc->save_regs)
933 mm_gc->save_regs(mm_gc);
934
Andy Shevchenko77289b22022-11-14 20:46:25 +0200935 fwnode_handle_put(mm_gc->gc.fwnode);
936 mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000937
Linus Walleij3208b0f2015-12-04 15:13:53 +0100938 ret = gpiochip_add_data(gc, data);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000939 if (ret)
940 goto err2;
941
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000942 return 0;
943err2:
Liang He5d07a692022-07-11 20:52:38 +0800944 of_node_put(np);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000945 iounmap(mm_gc->regs);
946err1:
947 kfree(gc->label);
948err0:
Rob Herring7eb6ce22017-07-18 16:43:03 -0500949 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000950 return ret;
951}
Geert Uytterhoeven6d662452019-09-06 10:45:38 +0200952EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
Grant Likely594fa262010-06-08 07:48:16 -0600953
Ricardo Ribalda Delgadod621e8b2014-12-17 16:51:13 +0100954/**
955 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
956 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
957 */
958void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
959{
960 struct gpio_chip *gc = &mm_gc->gc;
961
Ricardo Ribalda Delgadod621e8b2014-12-17 16:51:13 +0100962 gpiochip_remove(gc);
963 iounmap(mm_gc->regs);
964 kfree(gc->label);
965}
Geert Uytterhoeven6d662452019-09-06 10:45:38 +0200966EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
Ricardo Ribalda Delgadod621e8b2014-12-17 16:51:13 +0100967
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530968#ifdef CONFIG_PINCTRL
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200969static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530970{
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530971 struct of_phandle_args pinspec;
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100972 struct pinctrl_dev *pctldev;
Andy Shevchenko70d0fc42022-12-28 11:20:44 +0200973 struct device_node *np;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530974 int index = 0, ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +0200975 const char *name;
976 static const char group_names_propname[] = "gpio-ranges-group-names";
977 struct property *group_names;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530978
Andy Shevchenko70d0fc42022-12-28 11:20:44 +0200979 np = dev_of_node(&chip->gpiodev->dev);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530980 if (!np)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200981 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530982
Christian Ruppert586a87e2013-10-15 15:37:54 +0200983 group_names = of_find_property(np, group_names_propname, NULL);
984
Haojian Zhuangad4e1a72013-02-17 19:42:48 +0800985 for (;; index++) {
Stephen Warrend9fe0032013-08-14 15:27:12 -0600986 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
987 index, &pinspec);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530988 if (ret)
989 break;
990
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100991 pctldev = of_pinctrl_get(pinspec.np);
Masahiro Yamada602cf632016-05-23 10:52:09 +0900992 of_node_put(pinspec.np);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100993 if (!pctldev)
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200994 return -EPROBE_DEFER;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530995
Christian Ruppert586a87e2013-10-15 15:37:54 +0200996 if (pinspec.args[2]) {
997 if (group_names) {
Laurent Navet72858602015-07-07 22:22:15 +0200998 of_property_read_string_index(np,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200999 group_names_propname,
1000 index, &name);
1001 if (strlen(name)) {
Rob Herring7eb6ce22017-07-18 16:43:03 -05001002 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
1003 np);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001004 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 Vizoso28355f82015-07-14 10:29:54 +02001014 return ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001015 } else {
1016 /* npins == 0: special range */
1017 if (pinspec.args[1]) {
Rob Herring7eb6ce22017-07-18 16:43:03 -05001018 pr_err("%pOF: Illegal gpio-range format.\n",
1019 np);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001020 break;
1021 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301022
Christian Ruppert586a87e2013-10-15 15:37:54 +02001023 if (!group_names) {
Rob Herring7eb6ce22017-07-18 16:43:03 -05001024 pr_err("%pOF: GPIO group range requested but no %s property.\n",
1025 np, group_names_propname);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001026 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 Herring7eb6ce22017-07-18 16:43:03 -05001036 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1037 np);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001038 break;
1039 }
1040
1041 ret = gpiochip_add_pingroup_range(chip, pctldev,
1042 pinspec.args[0], name);
1043 if (ret)
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001044 return ret;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001045 }
Haojian Zhuangad4e1a72013-02-17 19:42:48 +08001046 }
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001047
1048 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301049}
1050
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301051#else
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001052static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301053#endif
1054
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001055int of_gpiochip_add(struct gpio_chip *chip)
Anton Vorontsov391c9702010-06-08 07:48:17 -06001056{
Andy Shevchenko8afe8252022-11-08 15:38:52 +02001057 struct device_node *np;
Linus Walleijf0d1ab02019-07-16 13:58:54 +02001058 int ret;
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001059
Andy Shevchenko70d0fc42022-12-28 11:20:44 +02001060 np = dev_of_node(&chip->gpiodev->dev);
Andy Shevchenko8afe8252022-11-08 15:38:52 +02001061 if (!np)
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001062 return 0;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001063
1064 if (!chip->of_xlate) {
1065 chip->of_gpio_n_cells = 2;
1066 chip->of_xlate = of_gpio_simple_xlate;
1067 }
1068
Masahiro Yamada1020dfd12016-06-14 19:07:05 +09001069 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1070 return -EINVAL;
1071
Linus Walleijf0d1ab02019-07-16 13:58:54 +02001072 ret = of_gpiochip_add_pin_range(chip);
1073 if (ret)
1074 return ret;
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001075
Andy Shevchenko8afe8252022-11-08 15:38:52 +02001076 fwnode_handle_get(chip->fwnode);
Benoit Parrotf625d462015-02-02 11:44:44 -06001077
Linus Walleijf0d1ab02019-07-16 13:58:54 +02001078 ret = of_gpiochip_scan_gpios(chip);
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001079 if (ret)
Andy Shevchenko8afe8252022-11-08 15:38:52 +02001080 fwnode_handle_put(chip->fwnode);
Geert Uytterhoevenf7299d42019-03-28 14:13:47 +01001081
Linus Walleijf0d1ab02019-07-16 13:58:54 +02001082 return ret;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001083}
1084
1085void of_gpiochip_remove(struct gpio_chip *chip)
1086{
Andy Shevchenko8afe8252022-11-08 15:38:52 +02001087 fwnode_handle_put(chip->fwnode);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001088}