1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/pinctrl/pinctrl-lantiq.c
4 * based on linux/drivers/pinctrl/pinctrl-pxa3xx.c
5 *
6 * Copyright (C) 2012 John Crispin <john@phrozen.org>
7 */
8
9 #include <linux/device.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16
17 #include "pinctrl-lantiq.h"
18
ltq_get_group_count(struct pinctrl_dev * pctrldev)19 static int ltq_get_group_count(struct pinctrl_dev *pctrldev)
20 {
21 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
22 return info->num_grps;
23 }
24
ltq_get_group_name(struct pinctrl_dev * pctrldev,unsigned selector)25 static const char *ltq_get_group_name(struct pinctrl_dev *pctrldev,
26 unsigned selector)
27 {
28 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
29 if (selector >= info->num_grps)
30 return NULL;
31 return info->grps[selector].name;
32 }
33
ltq_get_group_pins(struct pinctrl_dev * pctrldev,unsigned selector,const unsigned ** pins,unsigned * num_pins)34 static int ltq_get_group_pins(struct pinctrl_dev *pctrldev,
35 unsigned selector,
36 const unsigned **pins,
37 unsigned *num_pins)
38 {
39 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
40 if (selector >= info->num_grps)
41 return -EINVAL;
42 *pins = info->grps[selector].pins;
43 *num_pins = info->grps[selector].npins;
44 return 0;
45 }
46
ltq_pinctrl_dt_free_map(struct pinctrl_dev * pctldev,struct pinctrl_map * map,unsigned num_maps)47 static void ltq_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
48 struct pinctrl_map *map, unsigned num_maps)
49 {
50 int i;
51
52 for (i = 0; i < num_maps; i++)
53 if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN ||
54 map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
55 kfree(map[i].data.configs.configs);
56 kfree(map);
57 }
58
ltq_pinctrl_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned offset)59 static void ltq_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
60 struct seq_file *s,
61 unsigned offset)
62 {
63 seq_printf(s, " %s", dev_name(pctldev->dev));
64 }
65
ltq_pinctrl_dt_subnode_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** map)66 static void ltq_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
67 struct device_node *np,
68 struct pinctrl_map **map)
69 {
70 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctldev);
71 struct property *pins = of_find_property(np, "lantiq,pins", NULL);
72 struct property *groups = of_find_property(np, "lantiq,groups", NULL);
73 unsigned long configs[3];
74 unsigned num_configs = 0;
75 struct property *prop;
76 const char *group, *pin;
77 const char *function;
78 int ret, i;
79
80 if (!pins && !groups) {
81 dev_err(pctldev->dev, "%pOFn defines neither pins nor groups\n",
82 np);
83 return;
84 }
85
86 if (pins && groups) {
87 dev_err(pctldev->dev, "%pOFn defines both pins and groups\n",
88 np);
89 return;
90 }
91
92 ret = of_property_read_string(np, "lantiq,function", &function);
93 if (groups && !ret) {
94 of_property_for_each_string(np, "lantiq,groups", prop, group) {
95 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
96 (*map)->name = function;
97 (*map)->data.mux.group = group;
98 (*map)->data.mux.function = function;
99 (*map)++;
100 }
101 }
102
103 for (i = 0; i < info->num_params; i++) {
104 u32 val;
105 int ret = of_property_read_u32(np,
106 info->params[i].property, &val);
107 if (!ret)
108 configs[num_configs++] =
109 LTQ_PINCONF_PACK(info->params[i].param,
110 val);
111 }
112
113 if (!num_configs)
114 return;
115
116 of_property_for_each_string(np, "lantiq,pins", prop, pin) {
117 (*map)->data.configs.configs = kmemdup(configs,
118 num_configs * sizeof(unsigned long),
119 GFP_KERNEL);
120 (*map)->type = PIN_MAP_TYPE_CONFIGS_PIN;
121 (*map)->name = pin;
122 (*map)->data.configs.group_or_pin = pin;
123 (*map)->data.configs.num_configs = num_configs;
124 (*map)++;
125 }
126 of_property_for_each_string(np, "lantiq,groups", prop, group) {
127 (*map)->data.configs.configs = kmemdup(configs,
128 num_configs * sizeof(unsigned long),
129 GFP_KERNEL);
130 (*map)->type = PIN_MAP_TYPE_CONFIGS_GROUP;
131 (*map)->name = group;
132 (*map)->data.configs.group_or_pin = group;
133 (*map)->data.configs.num_configs = num_configs;
134 (*map)++;
135 }
136 }
137
ltq_pinctrl_dt_subnode_size(struct device_node * np)138 static int ltq_pinctrl_dt_subnode_size(struct device_node *np)
139 {
140 int ret;
141
142 ret = of_property_count_strings(np, "lantiq,groups");
143 if (ret < 0)
144 ret = of_property_count_strings(np, "lantiq,pins");
145 return ret;
146 }
147
ltq_pinctrl_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np_config,struct pinctrl_map ** map,unsigned * num_maps)148 static int ltq_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
149 struct device_node *np_config,
150 struct pinctrl_map **map,
151 unsigned *num_maps)
152 {
153 struct pinctrl_map *tmp;
154 struct device_node *np;
155 int max_maps = 0;
156
157 for_each_child_of_node(np_config, np)
158 max_maps += ltq_pinctrl_dt_subnode_size(np);
159 *map = kzalloc(array3_size(max_maps, sizeof(struct pinctrl_map), 2),
160 GFP_KERNEL);
161 if (!*map)
162 return -ENOMEM;
163 tmp = *map;
164
165 for_each_child_of_node(np_config, np)
166 ltq_pinctrl_dt_subnode_to_map(pctldev, np, &tmp);
167 *num_maps = ((int)(tmp - *map));
168
169 return 0;
170 }
171
172 static const struct pinctrl_ops ltq_pctrl_ops = {
173 .get_groups_count = ltq_get_group_count,
174 .get_group_name = ltq_get_group_name,
175 .get_group_pins = ltq_get_group_pins,
176 .pin_dbg_show = ltq_pinctrl_pin_dbg_show,
177 .dt_node_to_map = ltq_pinctrl_dt_node_to_map,
178 .dt_free_map = ltq_pinctrl_dt_free_map,
179 };
180
ltq_pmx_func_count(struct pinctrl_dev * pctrldev)181 static int ltq_pmx_func_count(struct pinctrl_dev *pctrldev)
182 {
183 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
184
185 return info->num_funcs;
186 }
187
ltq_pmx_func_name(struct pinctrl_dev * pctrldev,unsigned selector)188 static const char *ltq_pmx_func_name(struct pinctrl_dev *pctrldev,
189 unsigned selector)
190 {
191 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
192
193 if (selector >= info->num_funcs)
194 return NULL;
195
196 return info->funcs[selector].name;
197 }
198
ltq_pmx_get_groups(struct pinctrl_dev * pctrldev,unsigned func,const char * const ** groups,unsigned * const num_groups)199 static int ltq_pmx_get_groups(struct pinctrl_dev *pctrldev,
200 unsigned func,
201 const char * const **groups,
202 unsigned * const num_groups)
203 {
204 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
205
206 *groups = info->funcs[func].groups;
207 *num_groups = info->funcs[func].num_groups;
208
209 return 0;
210 }
211
212 /* Return function number. If failure, return negative value. */
match_mux(const struct ltq_mfp_pin * mfp,unsigned mux)213 static int match_mux(const struct ltq_mfp_pin *mfp, unsigned mux)
214 {
215 int i;
216 for (i = 0; i < LTQ_MAX_MUX; i++) {
217 if (mfp->func[i] == mux)
218 break;
219 }
220 if (i >= LTQ_MAX_MUX)
221 return -EINVAL;
222 return i;
223 }
224
225 /* don't assume .mfp is linearly mapped. find the mfp with the correct .pin */
match_mfp(const struct ltq_pinmux_info * info,int pin)226 static int match_mfp(const struct ltq_pinmux_info *info, int pin)
227 {
228 int i;
229 for (i = 0; i < info->num_mfp; i++) {
230 if (info->mfp[i].pin == pin)
231 return i;
232 }
233 return -1;
234 }
235
236 /* check whether current pin configuration is valid. Negative for failure */
match_group_mux(const struct ltq_pin_group * grp,const struct ltq_pinmux_info * info,unsigned mux)237 static int match_group_mux(const struct ltq_pin_group *grp,
238 const struct ltq_pinmux_info *info,
239 unsigned mux)
240 {
241 int i, pin, ret = 0;
242 for (i = 0; i < grp->npins; i++) {
243 pin = match_mfp(info, grp->pins[i]);
244 if (pin < 0) {
245 dev_err(info->dev, "could not find mfp for pin %d\n",
246 grp->pins[i]);
247 return -EINVAL;
248 }
249 ret = match_mux(&info->mfp[pin], mux);
250 if (ret < 0) {
251 dev_err(info->dev, "Can't find mux %d on pin%d\n",
252 mux, pin);
253 break;
254 }
255 }
256 return ret;
257 }
258
ltq_pmx_set(struct pinctrl_dev * pctrldev,unsigned func,unsigned group)259 static int ltq_pmx_set(struct pinctrl_dev *pctrldev,
260 unsigned func,
261 unsigned group)
262 {
263 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
264 const struct ltq_pin_group *pin_grp = &info->grps[group];
265 int i, pin, pin_func, ret;
266
267 if (!pin_grp->npins ||
268 (match_group_mux(pin_grp, info, pin_grp->mux) < 0)) {
269 dev_err(info->dev, "Failed to set the pin group: %s\n",
270 info->grps[group].name);
271 return -EINVAL;
272 }
273 for (i = 0; i < pin_grp->npins; i++) {
274 pin = match_mfp(info, pin_grp->pins[i]);
275 if (pin < 0) {
276 dev_err(info->dev, "could not find mfp for pin %d\n",
277 pin_grp->pins[i]);
278 return -EINVAL;
279 }
280 pin_func = match_mux(&info->mfp[pin], pin_grp->mux);
281 ret = info->apply_mux(pctrldev, pin, pin_func);
282 if (ret) {
283 dev_err(info->dev,
284 "failed to apply mux %d for pin %d\n",
285 pin_func, pin);
286 return ret;
287 }
288 }
289 return 0;
290 }
291
ltq_pmx_gpio_request_enable(struct pinctrl_dev * pctrldev,struct pinctrl_gpio_range * range,unsigned pin)292 static int ltq_pmx_gpio_request_enable(struct pinctrl_dev *pctrldev,
293 struct pinctrl_gpio_range *range,
294 unsigned pin)
295 {
296 struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
297 int mfp = match_mfp(info, pin);
298 int pin_func;
299
300 if (mfp < 0) {
301 dev_err(info->dev, "could not find mfp for pin %d\n", pin);
302 return -EINVAL;
303 }
304
305 pin_func = match_mux(&info->mfp[mfp], 0);
306 if (pin_func < 0) {
307 dev_err(info->dev, "No GPIO function on pin%d\n", mfp);
308 return -EINVAL;
309 }
310
311 return info->apply_mux(pctrldev, mfp, pin_func);
312 }
313
314 static const struct pinmux_ops ltq_pmx_ops = {
315 .get_functions_count = ltq_pmx_func_count,
316 .get_function_name = ltq_pmx_func_name,
317 .get_function_groups = ltq_pmx_get_groups,
318 .set_mux = ltq_pmx_set,
319 .gpio_request_enable = ltq_pmx_gpio_request_enable,
320 };
321
322 /*
323 * allow different socs to register with the generic part of the lanti
324 * pinctrl code
325 */
ltq_pinctrl_register(struct platform_device * pdev,struct ltq_pinmux_info * info)326 int ltq_pinctrl_register(struct platform_device *pdev,
327 struct ltq_pinmux_info *info)
328 {
329 struct pinctrl_desc *desc;
330
331 if (!info)
332 return -EINVAL;
333 desc = info->desc;
334 desc->pctlops = <q_pctrl_ops;
335 desc->pmxops = <q_pmx_ops;
336 info->dev = &pdev->dev;
337
338 info->pctrl = devm_pinctrl_register(&pdev->dev, desc, info);
339 if (IS_ERR(info->pctrl)) {
340 dev_err(&pdev->dev, "failed to register LTQ pinmux driver\n");
341 return PTR_ERR(info->pctrl);
342 }
343 platform_set_drvdata(pdev, info);
344 return 0;
345 }
346