1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// SPDX-License-Identifier: GPL-2.0-only
#define pr_fmt(fmt) "generic pinconfig core: " fmt
#include <linux/array_size.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/slab.h>
#include <linux/pinctrl/pinconf-generic.h>
#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinctrl.h>
#include "core.h"
#include "pinconf.h"
#include "pinctrl-utils.h"
#include "pinmux.h"
int pinctrl_generic_to_map(struct pinctrl_dev *pctldev, struct device_node *parent,
struct device_node *np, struct pinctrl_map **maps,
unsigned int *num_maps, unsigned int *num_reserved_maps,
const char **group_names, unsigned int ngroups,
void *data, unsigned int *pins, unsigned int npins)
{
struct device *dev = pctldev->dev;
unsigned int num_configs;
const char *group_name;
unsigned long *configs;
int ret, reserve = 1;
group_name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn.%pOFn", parent, np);
if (!group_name)
return -ENOMEM;
group_names[ngroups] = group_name;
ret = pinctrl_utils_reserve_map(pctldev, maps, num_reserved_maps, num_maps, reserve);
if (ret)
return ret;
ret = pinctrl_utils_add_map_mux(pctldev, maps, num_reserved_maps, num_maps, group_name,
parent->name);
if (ret < 0)
return ret;
ret = pinctrl_generic_add_group(pctldev, group_name, pins, npins, data);
if (ret < 0)
return dev_err_probe(dev, ret, "failed to add group %s: %d\n",
group_name, ret);
ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &num_configs);
if (ret)
return dev_err_probe(dev, ret, "failed to parse pin config of group %s\n",
group_name);
if (num_configs == 0)
return 0;
ret = pinctrl_utils_reserve_map(pctldev, maps, num_reserved_maps, num_maps, reserve);
if (ret)
return ret;
ret = pinctrl_utils_add_map_configs(pctldev, maps, num_reserved_maps, num_maps, group_name,
configs,
num_configs, PIN_MAP_TYPE_CONFIGS_GROUP);
kfree(configs);
if (ret)
return ret;
return 0;
};
EXPORT_SYMBOL_GPL(pinctrl_generic_to_map);
static int pinctrl_generic_pins_function_dt_subnode_to_map(struct pinctrl_dev *pctldev,
struct device_node *parent,
struct device_node *np,
struct pinctrl_map **maps,
unsigned int *num_maps,
unsigned int *num_reserved_maps,
const char **group_names,
unsigned int ngroups)
{
struct device *dev = pctldev->dev;
unsigned int pin, *pins;
const char **functions;
int npins, ret;
npins = of_property_count_u32_elems(np, "pins");
if (npins < 1) {
dev_err(dev, "invalid pinctrl group %pOFn.%pOFn %d\n",
parent, np, npins);
return npins;
}
pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL);
if (!pins)
return -ENOMEM;
functions = devm_kcalloc(dev, npins, sizeof(*functions), GFP_KERNEL);
if (!functions)
return -ENOMEM;
for (int i = 0; i < npins; i++) {
ret = of_property_read_u32_index(np, "pins", i, &pin);
if (ret)
return ret;
pins[i] = pin;
ret = of_property_read_string(np, "function", &functions[i]);
if (ret)
return ret;
}
return pinctrl_generic_to_map(pctldev, parent, np, maps, num_maps,
num_reserved_maps, group_names, ngroups,
functions, pins, npins);
}
static int pinctrl_generic_pinmux_dt_subnode_to_map(struct pinctrl_dev *pctldev,
struct device_node *parent,
struct device_node *np,
struct pinctrl_map **maps,
unsigned int *num_maps,
unsigned int *num_reserved_maps,
const char **group_names,
unsigned int ngroups)
{
struct device *dev = pctldev->dev;
unsigned int *pins, *muxes;
int npins, ret;
npins = of_property_count_u32_elems(np, "pinmux");
if (npins < 1) {
dev_err(dev, "invalid pinctrl group %pOFn.%pOFn %d\n",
parent, np, npins);
return npins;
}
pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL);
if (!pins)
return -ENOMEM;
muxes = devm_kcalloc(dev, npins, sizeof(*muxes), GFP_KERNEL);
if (!muxes)
return -ENOMEM;
for (int i = 0; i < npins; i++) {
unsigned int pinmux;
ret = of_property_read_u32_index(np, "pinmux", i, &pinmux);
if (ret)
return ret;
pins[i] = pinmux >> 16;
muxes[i] = pinmux & GENMASK(15, 0);
}
return pinctrl_generic_to_map(pctldev, parent, np, maps, num_maps,
num_reserved_maps, group_names, ngroups,
muxes, pins, npins);
}
static int pinctrl_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
struct device_node *np,
struct pinctrl_map **maps,
unsigned int *num_maps,
int (dt_subnode_to_map)(
struct pinctrl_dev *,
struct device_node *,
struct device_node *,
struct pinctrl_map **,
unsigned int *,
unsigned int *,
const char **,
unsigned int))
{
struct device *dev = pctldev->dev;
struct device_node *child_np;
const char **group_names;
unsigned int num_reserved_maps = 0;
int ngroups = 0;
int ret;
*maps = NULL;
*num_maps = 0;
/*
* Check if this is actually the pins node, or a parent containing
* multiple pins nodes.
*/
if (!of_property_present(np, "pins"))
goto parent;
group_names = devm_kcalloc(dev, 1, sizeof(*group_names), GFP_KERNEL);
if (!group_names)
return -ENOMEM;
ret = dt_subnode_to_map(pctldev, np, np, maps, num_maps,
&num_reserved_maps, group_names, ngroups);
if (ret) {
pinctrl_utils_free_map(pctldev, *maps, *num_maps);
return dev_err_probe(dev, ret, "error figuring out mappings for %s\n", np->name);
}
ret = pinmux_generic_add_function(pctldev, np->name, group_names, 1, NULL);
if (ret < 0) {
pinctrl_utils_free_map(pctldev, *maps, *num_maps);
return dev_err_probe(dev, ret, "error adding function %s\n", np->name);
}
return 0;
parent:
for_each_available_child_of_node(np, child_np)
ngroups += 1;
group_names = devm_kcalloc(dev, ngroups, sizeof(*group_names), GFP_KERNEL);
if (!group_names)
return -ENOMEM;
ngroups = 0;
for_each_available_child_of_node_scoped(np, child_np) {
ret = dt_subnode_to_map(pctldev, np, child_np, maps, num_maps,
&num_reserved_maps, group_names, ngroups);
if (ret) {
pinctrl_utils_free_map(pctldev, *maps, *num_maps);
return dev_err_probe(dev, ret, "error figuring out mappings for %s\n",
np->name);
}
ngroups++;
}
ret = pinmux_generic_add_function(pctldev, np->name, group_names, ngroups, NULL);
if (ret < 0) {
pinctrl_utils_free_map(pctldev, *maps, *num_maps);
return dev_err_probe(dev, ret, "error adding function %s\n", np->name);
}
return 0;
}
/*
* For platforms that do not define groups or functions in the driver, but
* instead use the devicetree to describe them. This function will, unlike
* pinconf_generic_dt_node_to_map() etc which rely on driver defined groups
* and functions, create them in addition to parsing pinconf properties and
* adding mappings.
*/
int pinctrl_generic_pins_function_dt_node_to_map(struct pinctrl_dev *pctldev,
struct device_node *np,
struct pinctrl_map **maps,
unsigned int *num_maps)
{
return pinctrl_generic_dt_node_to_map(pctldev, np, maps, num_maps,
&pinctrl_generic_pins_function_dt_subnode_to_map);
}
EXPORT_SYMBOL_GPL(pinctrl_generic_pins_function_dt_node_to_map);
/*
* For platforms that do not define groups or functions in the driver, but
* instead use the devicetree to describe them. This function will, unlike
* pinconf_generic_dt_node_to_map() etc which rely on driver defined groups
* and functions, create them in addition to parsing pinconf properties and
* adding mappings.
*
* It assumes that the upper 16 bits of the pinmux items contain the pin
* and the lower 16 the mux setting.
*/
int pinctrl_generic_pinmux_dt_node_to_map(struct pinctrl_dev *pctldev,
struct device_node *np,
struct pinctrl_map **maps,
unsigned int *num_maps)
{
return pinctrl_generic_dt_node_to_map(pctldev, np, maps, num_maps,
&pinctrl_generic_pinmux_dt_subnode_to_map);
};
EXPORT_SYMBOL_GPL(pinctrl_generic_pinmux_dt_node_to_map);
|