blob: d6ed4e891b3483e2b14122aa73d11104b5140123 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * OF helpers for the GPIO API
3 *
4 * Copyright (c) 2007-2008 MontaVista Software, Inc.
5 *
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/device.h>
15#include <linux/err.h>
16#include <linux/errno.h>
17#include <linux/module.h>
18#include <linux/io.h>
19#include <linux/gpio/consumer.h>
20#include <linux/of.h>
21#include <linux/of_address.h>
22#include <linux/of_gpio.h>
23#include <linux/pinctrl/pinctrl.h>
24#include <linux/slab.h>
25#include <linux/gpio/machine.h>
26
27#include "gpiolib.h"
28
29static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
30{
31 struct of_phandle_args *gpiospec = data;
32
33 return chip->gpiodev->dev.of_node == gpiospec->np &&
34 chip->of_xlate &&
35 chip->of_xlate(chip, gpiospec, NULL) >= 0;
36}
37
38static struct gpio_chip *of_find_gpiochip_by_xlate(
39 struct of_phandle_args *gpiospec)
40{
41 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
42}
43
44static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
45 struct of_phandle_args *gpiospec,
46 enum of_gpio_flags *flags)
47{
48 int ret;
49
50 if (chip->of_gpio_n_cells != gpiospec->args_count)
51 return ERR_PTR(-EINVAL);
52
53 ret = chip->of_xlate(chip, gpiospec, flags);
54 if (ret < 0)
55 return ERR_PTR(ret);
56
57 return gpiochip_get_desc(chip, ret);
58}
59
60/**
61 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
62 * @np: device node to get GPIO from
63 * @propname: property name containing gpio specifier(s)
64 * @index: index of the GPIO
65 * @flags: a flags pointer to fill in
66 *
67 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
68 * value on the error condition. If @flags is not NULL the function also fills
69 * in flags for the GPIO.
70 */
71struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
72 const char *propname, int index, enum of_gpio_flags *flags)
73{
74 struct of_phandle_args gpiospec;
75 struct gpio_chip *chip;
76 struct gpio_desc *desc;
77 int ret;
78
79 ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
80 &gpiospec);
81 if (ret) {
82 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
83 __func__, propname, np, index);
84 return ERR_PTR(ret);
85 }
86
87 chip = of_find_gpiochip_by_xlate(&gpiospec);
88 if (!chip) {
89 desc = ERR_PTR(-EPROBE_DEFER);
90 goto out;
91 }
92
93 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
94 if (IS_ERR(desc))
95 goto out;
96
97 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
98 __func__, propname, np, index,
99 PTR_ERR_OR_ZERO(desc));
100
101out:
102 of_node_put(gpiospec.np);
103
104 return desc;
105}
106
107int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
108 int index, enum of_gpio_flags *flags)
109{
110 struct gpio_desc *desc;
111
112 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
113
114 if (IS_ERR(desc))
115 return PTR_ERR(desc);
116 else
117 return desc_to_gpio(desc);
118}
119EXPORT_SYMBOL(of_get_named_gpio_flags);
120
121struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
122 unsigned int idx,
123 enum gpio_lookup_flags *flags)
124{
125 char prop_name[32]; /* 32 is max size of property name */
126 enum of_gpio_flags of_flags;
127 struct gpio_desc *desc;
128 unsigned int i;
129
130 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
131 if (con_id)
132 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
133 gpio_suffixes[i]);
134 else
135 snprintf(prop_name, sizeof(prop_name), "%s",
136 gpio_suffixes[i]);
137
138 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
139 &of_flags);
140 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
141 break;
142 }
143
144 if (IS_ERR(desc))
145 return desc;
146
147 if (of_flags & OF_GPIO_ACTIVE_LOW)
148 *flags |= GPIO_ACTIVE_LOW;
149
150 if (of_flags & OF_GPIO_SINGLE_ENDED) {
151 if (of_flags & OF_GPIO_OPEN_DRAIN)
152 *flags |= GPIO_OPEN_DRAIN;
153 else
154 *flags |= GPIO_OPEN_SOURCE;
155 }
156
157 if (of_flags & OF_GPIO_SLEEP_MAY_LOOSE_VALUE)
158 *flags |= GPIO_SLEEP_MAY_LOOSE_VALUE;
159
160 return desc;
161}
162
163/**
164 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
165 * @np: device node to get GPIO from
166 * @chip: GPIO chip whose hog is parsed
167 * @idx: Index of the GPIO to parse
168 * @name: GPIO line name
169 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
170 * of_parse_own_gpio()
171 * @dflags: gpiod_flags - optional GPIO initialization flags
172 *
173 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
174 * value on the error condition.
175 */
176static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
177 struct gpio_chip *chip,
178 unsigned int idx, const char **name,
179 enum gpio_lookup_flags *lflags,
180 enum gpiod_flags *dflags)
181{
182 struct device_node *chip_np;
183 enum of_gpio_flags xlate_flags;
184 struct of_phandle_args gpiospec;
185 struct gpio_desc *desc;
186 unsigned int i;
187 u32 tmp;
188 int ret;
189
190 chip_np = chip->of_node;
191 if (!chip_np)
192 return ERR_PTR(-EINVAL);
193
194 xlate_flags = 0;
195 *lflags = 0;
196 *dflags = 0;
197
198 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
199 if (ret)
200 return ERR_PTR(ret);
201
202 gpiospec.np = chip_np;
203 gpiospec.args_count = tmp;
204
205 for (i = 0; i < tmp; i++) {
206 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
207 &gpiospec.args[i]);
208 if (ret)
209 return ERR_PTR(ret);
210 }
211
212 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
213 if (IS_ERR(desc))
214 return desc;
215
216 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
217 *lflags |= GPIO_ACTIVE_LOW;
218
219 if (of_property_read_bool(np, "input"))
220 *dflags |= GPIOD_IN;
221 else if (of_property_read_bool(np, "output-low"))
222 *dflags |= GPIOD_OUT_LOW;
223 else if (of_property_read_bool(np, "output-high"))
224 *dflags |= GPIOD_OUT_HIGH;
225 else {
226 pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
227 desc_to_gpio(desc), np->name);
228 return ERR_PTR(-EINVAL);
229 }
230
231 if (name && of_property_read_string(np, "line-name", name))
232 *name = np->name;
233
234 return desc;
235}
236
237/**
238 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
239 * @chip: gpio chip to act on
240 *
241 * This is only used by of_gpiochip_add to request/set GPIO initial
242 * configuration.
243 * It returns error if it fails otherwise 0 on success.
244 */
245static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
246{
247 struct gpio_desc *desc = NULL;
248 struct device_node *np;
249 const char *name;
250 enum gpio_lookup_flags lflags;
251 enum gpiod_flags dflags;
252 unsigned int i;
253 int ret;
254
255 for_each_available_child_of_node(chip->of_node, np) {
256 if (!of_property_read_bool(np, "gpio-hog"))
257 continue;
258
259 for (i = 0;; i++) {
260 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
261 &dflags);
262 if (IS_ERR(desc))
263 break;
264
265 ret = gpiod_hog(desc, name, lflags, dflags);
266 if (ret < 0) {
267 of_node_put(np);
268 return ret;
269 }
270 }
271 }
272
273 return 0;
274}
275
276/**
277 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
278 * @gc: pointer to the gpio_chip structure
279 * @gpiospec: GPIO specifier as found in the device tree
280 * @flags: a flags pointer to fill in
281 *
282 * This is simple translation function, suitable for the most 1:1 mapped
283 * GPIO chips. This function performs only one sanity check: whether GPIO
284 * is less than ngpios (that is specified in the gpio_chip).
285 */
286int of_gpio_simple_xlate(struct gpio_chip *gc,
287 const struct of_phandle_args *gpiospec, u32 *flags)
288{
289 /*
290 * We're discouraging gpio_cells < 2, since that way you'll have to
291 * write your own xlate function (that will have to retrieve the GPIO
292 * number and the flags from a single gpio cell -- this is possible,
293 * but not recommended).
294 */
295 if (gc->of_gpio_n_cells < 2) {
296 WARN_ON(1);
297 return -EINVAL;
298 }
299
300 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
301 return -EINVAL;
302
303 if (gpiospec->args[0] >= gc->ngpio)
304 return -EINVAL;
305
306 if (flags)
307 *flags = gpiospec->args[1];
308
309 return gpiospec->args[0];
310}
311EXPORT_SYMBOL(of_gpio_simple_xlate);
312
313/**
314 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
315 * @np: device node of the GPIO chip
316 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
317 * @data: driver data to store in the struct gpio_chip
318 *
319 * To use this function you should allocate and fill mm_gc with:
320 *
321 * 1) In the gpio_chip structure:
322 * - all the callbacks
323 * - of_gpio_n_cells
324 * - of_xlate callback (optional)
325 *
326 * 3) In the of_mm_gpio_chip structure:
327 * - save_regs callback (optional)
328 *
329 * If succeeded, this function will map bank's memory and will
330 * do all necessary work for you. Then you'll able to use .regs
331 * to manage GPIOs from the callbacks.
332 */
333int of_mm_gpiochip_add_data(struct device_node *np,
334 struct of_mm_gpio_chip *mm_gc,
335 void *data)
336{
337 int ret = -ENOMEM;
338 struct gpio_chip *gc = &mm_gc->gc;
339
340 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
341 if (!gc->label)
342 goto err0;
343
344 mm_gc->regs = of_iomap(np, 0);
345 if (!mm_gc->regs)
346 goto err1;
347
348 gc->base = -1;
349
350 if (mm_gc->save_regs)
351 mm_gc->save_regs(mm_gc);
352
353 mm_gc->gc.of_node = np;
354
355 ret = gpiochip_add_data(gc, data);
356 if (ret)
357 goto err2;
358
359 return 0;
360err2:
361 iounmap(mm_gc->regs);
362err1:
363 kfree(gc->label);
364err0:
365 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
366 return ret;
367}
368EXPORT_SYMBOL(of_mm_gpiochip_add_data);
369
370/**
371 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
372 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
373 */
374void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
375{
376 struct gpio_chip *gc = &mm_gc->gc;
377
378 if (!mm_gc)
379 return;
380
381 gpiochip_remove(gc);
382 iounmap(mm_gc->regs);
383 kfree(gc->label);
384}
385EXPORT_SYMBOL(of_mm_gpiochip_remove);
386
387#ifdef CONFIG_PINCTRL
388static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
389{
390 struct device_node *np = chip->of_node;
391 struct of_phandle_args pinspec;
392 struct pinctrl_dev *pctldev;
393 int index = 0, ret;
394 const char *name;
395 static const char group_names_propname[] = "gpio-ranges-group-names";
396 struct property *group_names;
397
398 if (!np)
399 return 0;
400
401 group_names = of_find_property(np, group_names_propname, NULL);
402
403 for (;; index++) {
404 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
405 index, &pinspec);
406 if (ret)
407 break;
408
409 pctldev = of_pinctrl_get(pinspec.np);
410 of_node_put(pinspec.np);
411 if (!pctldev)
412 return -EPROBE_DEFER;
413
414 if (pinspec.args[2]) {
415 if (group_names) {
416 of_property_read_string_index(np,
417 group_names_propname,
418 index, &name);
419 if (strlen(name)) {
420 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
421 np);
422 break;
423 }
424 }
425 /* npins != 0: linear range */
426 ret = gpiochip_add_pin_range(chip,
427 pinctrl_dev_get_devname(pctldev),
428 pinspec.args[0],
429 pinspec.args[1],
430 pinspec.args[2]);
431 if (ret)
432 return ret;
433 } else {
434 /* npins == 0: special range */
435 if (pinspec.args[1]) {
436 pr_err("%pOF: Illegal gpio-range format.\n",
437 np);
438 break;
439 }
440
441 if (!group_names) {
442 pr_err("%pOF: GPIO group range requested but no %s property.\n",
443 np, group_names_propname);
444 break;
445 }
446
447 ret = of_property_read_string_index(np,
448 group_names_propname,
449 index, &name);
450 if (ret)
451 break;
452
453 if (!strlen(name)) {
454 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
455 np);
456 break;
457 }
458
459 ret = gpiochip_add_pingroup_range(chip, pctldev,
460 pinspec.args[0], name);
461 if (ret)
462 return ret;
463 }
464 }
465
466 return 0;
467}
468
469#else
470static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
471#endif
472
473int of_gpiochip_add(struct gpio_chip *chip)
474{
475 int status;
476
477 if ((!chip->of_node) && (chip->parent))
478 chip->of_node = chip->parent->of_node;
479
480 if (!chip->of_node)
481 return 0;
482
483 if (!chip->of_xlate) {
484 chip->of_gpio_n_cells = 2;
485 chip->of_xlate = of_gpio_simple_xlate;
486 }
487
488 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
489 return -EINVAL;
490
491 status = of_gpiochip_add_pin_range(chip);
492 if (status)
493 return status;
494
495 /* If the chip defines names itself, these take precedence */
496 if (!chip->names)
497 devprop_gpiochip_set_names(chip,
498 of_fwnode_handle(chip->of_node));
499
500 of_node_get(chip->of_node);
501
502 status = of_gpiochip_scan_gpios(chip);
503 if (status) {
504 of_node_put(chip->of_node);
505 gpiochip_remove_pin_ranges(chip);
506 }
507
508 return status;
509}
510
511void of_gpiochip_remove(struct gpio_chip *chip)
512{
513 gpiochip_remove_pin_ranges(chip);
514 of_node_put(chip->of_node);
515}