Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Software Node helpers for the GPIO API
4 *
5 * Copyright 2022 Google LLC
6 */
7
8#define pr_fmt(fmt) "gpiolib: swnode: " fmt
9
10#include <linux/err.h>
11#include <linux/errno.h>
12#include <linux/export.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/printk.h>
16#include <linux/property.h>
17#include <linux/string.h>
18
19#include <linux/gpio/consumer.h>
20#include <linux/gpio/driver.h>
21
22#include "gpiolib.h"
23#include "gpiolib-swnode.h"
24
25#define GPIOLIB_SWNODE_UNDEFINED_NAME "swnode-gpio-undefined"
26
27static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
28{
29 const struct software_node *gdev_node;
30 struct gpio_device *gdev;
31
32 gdev_node = to_software_node(fwnode);
33 if (!gdev_node || !gdev_node->name)
34 goto fwnode_lookup;
35
36 /*
37 * Check for a special node that identifies undefined GPIOs, this is
38 * primarily used as a key for internal chip selects in SPI bindings.
39 */
40 if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) &&
41 !strcmp(gdev_node->name, GPIOLIB_SWNODE_UNDEFINED_NAME))
42 return ERR_PTR(-ENOENT);
43
44fwnode_lookup:
45 gdev = gpio_device_find_by_fwnode(fwnode);
46 return gdev ?: ERR_PTR(-EPROBE_DEFER);
47}
48
49static int swnode_gpio_get_reference(const struct fwnode_handle *fwnode,
50 const char *propname, unsigned int idx,
51 struct fwnode_reference_args *args)
52{
53 /*
54 * We expect all swnode-described GPIOs have GPIO number and
55 * polarity arguments, hence nargs is set to 2.
56 */
57 return fwnode_property_get_reference_args(fwnode, propname, NULL, 2, idx, args);
58}
59
60struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
61 const char *con_id, unsigned int idx,
62 unsigned long *flags)
63{
64 const struct software_node *swnode;
65 struct fwnode_reference_args args;
66 struct gpio_desc *desc;
67 char propname[32]; /* 32 is max size of property name */
68 int ret = 0;
69
70 swnode = to_software_node(fwnode);
71 if (!swnode)
72 return ERR_PTR(-EINVAL);
73
74 for_each_gpio_property_name(propname, con_id) {
75 ret = swnode_gpio_get_reference(fwnode, propname, idx, &args);
76 if (ret == 0)
77 break;
78 }
79 if (ret) {
80 pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n",
81 __func__, propname, fwnode, idx);
82 return ERR_PTR(ret);
83 }
84
85 struct gpio_device *gdev __free(gpio_device_put) =
86 swnode_get_gpio_device(args.fwnode);
87 fwnode_handle_put(args.fwnode);
88 if (IS_ERR(gdev))
89 return ERR_CAST(gdev);
90
91 /*
92 * FIXME: The GPIO device reference is put at return but the descriptor
93 * is passed on. Find a proper solution.
94 */
95 desc = gpio_device_get_desc(gdev, args.args[0]);
96 *flags = args.args[1]; /* We expect native GPIO flags */
97
98 pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n",
99 __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc));
100
101 return desc;
102}
103
104/**
105 * swnode_gpio_count - count the GPIOs associated with a device / function
106 * @fwnode: firmware node of the GPIO consumer, can be %NULL for
107 * system-global GPIOs
108 * @con_id: function within the GPIO consumer
109 *
110 * Returns:
111 * The number of GPIOs associated with a device / function or %-ENOENT,
112 * if no GPIO has been assigned to the requested function.
113 */
114int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
115{
116 struct fwnode_reference_args args;
117 char propname[32];
118 int count;
119
120 /*
121 * This is not very efficient, but GPIO lists usually have only
122 * 1 or 2 entries.
123 */
124 for_each_gpio_property_name(propname, con_id) {
125 count = 0;
126 while (swnode_gpio_get_reference(fwnode, propname, count, &args) == 0) {
127 fwnode_handle_put(args.fwnode);
128 count++;
129 }
130 if (count)
131 return count;
132 }
133
134 return -ENOENT;
135}
136
137#if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED)
138/*
139 * A special node that identifies undefined GPIOs, this is primarily used as
140 * a key for internal chip selects in SPI bindings.
141 */
142const struct software_node swnode_gpio_undefined = {
143 .name = GPIOLIB_SWNODE_UNDEFINED_NAME,
144};
145EXPORT_SYMBOL_NS_GPL(swnode_gpio_undefined, "GPIO_SWNODE");
146
147static int __init swnode_gpio_init(void)
148{
149 int ret;
150
151 ret = software_node_register(&swnode_gpio_undefined);
152 if (ret < 0)
153 pr_err("failed to register swnode: %d\n", ret);
154
155 return ret;
156}
157subsys_initcall(swnode_gpio_init);
158
159static void __exit swnode_gpio_cleanup(void)
160{
161 software_node_unregister(&swnode_gpio_undefined);
162}
163__exitcall(swnode_gpio_cleanup);
164#endif