hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/gpio/gpiolib-of.c
....@@ -1,14 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0+
12 /*
23 * OF helpers for the GPIO API
34 *
45 * Copyright (c) 2007-2008 MontaVista Software, Inc.
56 *
67 * 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.
128 */
139
1410 #include <linux/device.h>
....@@ -25,6 +21,65 @@
2521 #include <linux/gpio/machine.h>
2622
2723 #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
+}
2883
2984 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
3085 {
....@@ -57,8 +112,28 @@
57112 return gpiochip_get_desc(chip, ret);
58113 }
59114
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
+
60133 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)
62137 {
63138 /*
64139 * Some GPIO fixed regulator quirks.
....@@ -67,19 +142,23 @@
67142 if (IS_ENABLED(CONFIG_REGULATOR) &&
68143 (of_device_is_compatible(np, "regulator-fixed") ||
69144 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");
71150 /*
72151 * The regulator GPIO handles are specified such that the
73152 * presence or absence of "enable-active-high" solely controls
74153 * the polarity of the GPIO line. Any phandle flags must
75154 * be actively ignored.
76155 */
77
- if (*flags & OF_GPIO_ACTIVE_LOW) {
156
+ if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
78157 pr_warn("%s GPIO handle specifies active low - ignored\n",
79158 of_node_full_name(np));
80159 *flags &= ~OF_GPIO_ACTIVE_LOW;
81160 }
82
- if (!of_property_read_bool(np, "enable-active-high"))
161
+ if (active_low)
83162 *flags |= OF_GPIO_ACTIVE_LOW;
84163 }
85164 /*
....@@ -92,6 +171,58 @@
92171 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
93172 of_node_full_name(np));
94173 }
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;
95226 }
96227
97228 /**
....@@ -105,7 +236,7 @@
105236 * value on the error condition. If @flags is not NULL the function also fills
106237 * in flags for the GPIO.
107238 */
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,
109240 const char *propname, int index, enum of_gpio_flags *flags)
110241 {
111242 struct of_phandle_args gpiospec;
....@@ -132,7 +263,7 @@
132263 goto out;
133264
134265 if (flags)
135
- of_gpio_flags_quirks(np, flags);
266
+ of_gpio_flags_quirks(np, propname, flags, index);
136267
137268 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
138269 __func__, propname, np, index,
....@@ -156,7 +287,82 @@
156287 else
157288 return desc_to_gpio(desc);
158289 }
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);
160366
161367 /*
162368 * The SPI GPIO bindings happened before we managed to establish that GPIO
....@@ -186,6 +392,38 @@
186392
187393 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
188394 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);
189427 }
190428
191429 /*
....@@ -220,9 +458,39 @@
220458 return desc;
221459 }
222460
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
+
223492 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)
226494 {
227495 char prop_name[32]; /* 32 is max size of property name */
228496 enum of_gpio_flags of_flags;
....@@ -240,30 +508,33 @@
240508
241509 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
242510 &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;
255511
256
- if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
512
+ if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
257513 break;
258514 }
259515
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 */
262518 desc = of_find_spi_gpio(dev, con_id, &of_flags);
519
+ }
263520
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 */
266530 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);
267538
268539 if (IS_ERR(desc))
269540 return desc;
....@@ -281,6 +552,11 @@
281552 if (of_flags & OF_GPIO_TRANSITORY)
282553 *flags |= GPIO_TRANSITORY;
283554
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
+
284560 return desc;
285561 }
286562
....@@ -290,8 +566,8 @@
290566 * @chip: GPIO chip whose hog is parsed
291567 * @idx: Index of the GPIO to parse
292568 * @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()
295571 * @dflags: gpiod_flags - optional GPIO initialization flags
296572 *
297573 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
....@@ -300,7 +576,7 @@
300576 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
301577 struct gpio_chip *chip,
302578 unsigned int idx, const char **name,
303
- enum gpio_lookup_flags *lflags,
579
+ unsigned long *lflags,
304580 enum gpiod_flags *dflags)
305581 {
306582 struct device_node *chip_np;
....@@ -316,7 +592,7 @@
316592 return ERR_PTR(-EINVAL);
317593
318594 xlate_flags = 0;
319
- *lflags = 0;
595
+ *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
320596 *dflags = 0;
321597
322598 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
....@@ -341,6 +617,10 @@
341617 *lflags |= GPIO_ACTIVE_LOW;
342618 if (xlate_flags & OF_GPIO_TRANSITORY)
343619 *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;
344624
345625 if (of_property_read_bool(np, "input"))
346626 *dflags |= GPIOD_IN;
....@@ -349,8 +629,8 @@
349629 else if (of_property_read_bool(np, "output-high"))
350630 *dflags |= GPIOD_OUT_HIGH;
351631 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);
354634 return ERR_PTR(-EINVAL);
355635 }
356636
....@@ -358,6 +638,39 @@
358638 *name = np->name;
359639
360640 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;
361674 }
362675
363676 /**
....@@ -370,34 +683,108 @@
370683 */
371684 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
372685 {
373
- struct gpio_desc *desc = NULL;
374686 struct device_node *np;
375
- const char *name;
376
- enum gpio_lookup_flags lflags;
377
- enum gpiod_flags dflags;
378
- unsigned int i;
379687 int ret;
380688
381689 for_each_available_child_of_node(chip->of_node, np) {
382690 if (!of_property_read_bool(np, "gpio-hog"))
383691 continue;
384692
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;
396697 }
698
+
699
+ of_node_set_flag(np, OF_POPULATED);
397700 }
398701
399702 return 0;
400703 }
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 */
401788
402789 /**
403790 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
....@@ -409,8 +796,9 @@
409796 * GPIO chips. This function performs only one sanity check: whether GPIO
410797 * is less than ngpios (that is specified in the gpio_chip).
411798 */
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)
414802 {
415803 /*
416804 * We're discouraging gpio_cells < 2, since that way you'll have to
....@@ -434,7 +822,6 @@
434822
435823 return gpiospec->args[0];
436824 }
437
-EXPORT_SYMBOL(of_gpio_simple_xlate);
438825
439826 /**
440827 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
....@@ -476,7 +863,8 @@
476863 if (mm_gc->save_regs)
477864 mm_gc->save_regs(mm_gc);
478865
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);
480868
481869 ret = gpiochip_add_data(gc, data);
482870 if (ret)
....@@ -484,6 +872,7 @@
484872
485873 return 0;
486874 err2:
875
+ of_node_put(np);
487876 iounmap(mm_gc->regs);
488877 err1:
489878 kfree(gc->label);
....@@ -491,7 +880,7 @@
491880 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
492881 return ret;
493882 }
494
-EXPORT_SYMBOL(of_mm_gpiochip_add_data);
883
+EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
495884
496885 /**
497886 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
....@@ -508,7 +897,7 @@
508897 iounmap(mm_gc->regs);
509898 kfree(gc->label);
510899 }
511
-EXPORT_SYMBOL(of_mm_gpiochip_remove);
900
+EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
512901
513902 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
514903 {
....@@ -525,7 +914,7 @@
525914 i, &start);
526915 of_property_read_u32_index(np, "gpio-reserved-ranges",
527916 i + 1, &count);
528
- if (start >= chip->ngpio || start + count >= chip->ngpio)
917
+ if (start >= chip->ngpio || start + count > chip->ngpio)
529918 continue;
530919
531920 bitmap_clear(chip->valid_mask, start, count);
....@@ -620,7 +1009,7 @@
6201009
6211010 int of_gpiochip_add(struct gpio_chip *chip)
6221011 {
623
- int status;
1012
+ int ret;
6241013
6251014 if (!chip->of_node)
6261015 return 0;
....@@ -635,28 +1024,31 @@
6351024
6361025 of_gpiochip_init_valid_mask(chip);
6371026
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;
6461030
6471031 of_node_get(chip->of_node);
6481032
649
- status = of_gpiochip_scan_gpios(chip);
650
- if (status) {
1033
+ ret = of_gpiochip_scan_gpios(chip);
1034
+ if (ret)
6511035 of_node_put(chip->of_node);
652
- gpiochip_remove_pin_ranges(chip);
653
- }
6541036
655
- return status;
1037
+ return ret;
6561038 }
6571039
6581040 void of_gpiochip_remove(struct gpio_chip *chip)
6591041 {
660
- gpiochip_remove_pin_ranges(chip);
6611042 of_node_put(chip->of_node);
6621043 }
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
+}