1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/pinctrl/pinctrl-lantiq.c
4 * based on linux/drivers/pinctrl/pinctrl-pxa3xx.c
6 * Copyright (C) 2012 John Crispin <john@phrozen.org>
9 #include <linux/device.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
17 #include "pinctrl-lantiq.h"
19 static int ltq_get_group_count(struct pinctrl_dev
*pctrldev
)
21 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
22 return info
->num_grps
;
25 static const char *ltq_get_group_name(struct pinctrl_dev
*pctrldev
,
28 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
29 if (selector
>= info
->num_grps
)
31 return info
->grps
[selector
].name
;
34 static int ltq_get_group_pins(struct pinctrl_dev
*pctrldev
,
36 const unsigned **pins
,
39 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
40 if (selector
>= info
->num_grps
)
42 *pins
= info
->grps
[selector
].pins
;
43 *num_pins
= info
->grps
[selector
].npins
;
47 static void ltq_pinctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
48 struct pinctrl_map
*map
, unsigned num_maps
)
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
);
59 static void ltq_pinctrl_pin_dbg_show(struct pinctrl_dev
*pctldev
,
63 seq_printf(s
, " %s", dev_name(pctldev
->dev
));
66 static void ltq_pinctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
67 struct device_node
*np
,
68 struct pinctrl_map
**map
)
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
;
80 if (!pins
&& !groups
) {
81 dev_err(pctldev
->dev
, "%pOFn defines neither pins nor groups\n",
87 dev_err(pctldev
->dev
, "%pOFn defines both pins and groups\n",
92 ret
= of_property_read_string(np
, "lantiq,function", &function
);
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
;
103 for (i
= 0; i
< info
->num_params
; i
++) {
105 int ret
= of_property_read_u32(np
,
106 info
->params
[i
].property
, &val
);
108 configs
[num_configs
++] =
109 LTQ_PINCONF_PACK(info
->params
[i
].param
,
116 of_property_for_each_string(np
, "lantiq,pins", prop
, pin
) {
117 (*map
)->data
.configs
.configs
= kmemdup(configs
,
118 num_configs
* sizeof(unsigned long),
120 (*map
)->type
= PIN_MAP_TYPE_CONFIGS_PIN
;
122 (*map
)->data
.configs
.group_or_pin
= pin
;
123 (*map
)->data
.configs
.num_configs
= num_configs
;
126 of_property_for_each_string(np
, "lantiq,groups", prop
, group
) {
127 (*map
)->data
.configs
.configs
= kmemdup(configs
,
128 num_configs
* sizeof(unsigned long),
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
;
138 static int ltq_pinctrl_dt_subnode_size(struct device_node
*np
)
142 ret
= of_property_count_strings(np
, "lantiq,groups");
144 ret
= of_property_count_strings(np
, "lantiq,pins");
148 static int ltq_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
149 struct device_node
*np_config
,
150 struct pinctrl_map
**map
,
153 struct pinctrl_map
*tmp
;
154 struct device_node
*np
;
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),
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
));
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
,
181 static int ltq_pmx_func_count(struct pinctrl_dev
*pctrldev
)
183 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
185 return info
->num_funcs
;
188 static const char *ltq_pmx_func_name(struct pinctrl_dev
*pctrldev
,
191 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
193 if (selector
>= info
->num_funcs
)
196 return info
->funcs
[selector
].name
;
199 static int ltq_pmx_get_groups(struct pinctrl_dev
*pctrldev
,
201 const char * const **groups
,
202 unsigned * const num_groups
)
204 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
206 *groups
= info
->funcs
[func
].groups
;
207 *num_groups
= info
->funcs
[func
].num_groups
;
212 /* Return function number. If failure, return negative value. */
213 static int match_mux(const struct ltq_mfp_pin
*mfp
, unsigned mux
)
216 for (i
= 0; i
< LTQ_MAX_MUX
; i
++) {
217 if (mfp
->func
[i
] == mux
)
220 if (i
>= LTQ_MAX_MUX
)
225 /* don't assume .mfp is linearly mapped. find the mfp with the correct .pin */
226 static int match_mfp(const struct ltq_pinmux_info
*info
, int pin
)
229 for (i
= 0; i
< info
->num_mfp
; i
++) {
230 if (info
->mfp
[i
].pin
== pin
)
236 /* check whether current pin configuration is valid. Negative for failure */
237 static int match_group_mux(const struct ltq_pin_group
*grp
,
238 const struct ltq_pinmux_info
*info
,
242 for (i
= 0; i
< grp
->npins
; i
++) {
243 pin
= match_mfp(info
, grp
->pins
[i
]);
245 dev_err(info
->dev
, "could not find mfp for pin %d\n",
249 ret
= match_mux(&info
->mfp
[pin
], mux
);
251 dev_err(info
->dev
, "Can't find mux %d on pin%d\n",
259 static int ltq_pmx_set(struct pinctrl_dev
*pctrldev
,
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
;
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
);
273 for (i
= 0; i
< pin_grp
->npins
; i
++) {
274 pin
= match_mfp(info
, pin_grp
->pins
[i
]);
276 dev_err(info
->dev
, "could not find mfp for pin %d\n",
280 pin_func
= match_mux(&info
->mfp
[pin
], pin_grp
->mux
);
281 ret
= info
->apply_mux(pctrldev
, pin
, pin_func
);
284 "failed to apply mux %d for pin %d\n",
292 static int ltq_pmx_gpio_request_enable(struct pinctrl_dev
*pctrldev
,
293 struct pinctrl_gpio_range
*range
,
296 struct ltq_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
297 int mfp
= match_mfp(info
, pin
);
301 dev_err(info
->dev
, "could not find mfp for pin %d\n", pin
);
305 pin_func
= match_mux(&info
->mfp
[mfp
], 0);
307 dev_err(info
->dev
, "No GPIO function on pin%d\n", mfp
);
311 return info
->apply_mux(pctrldev
, mfp
, pin_func
);
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
,
323 * allow different socs to register with the generic part of the lanti
326 int ltq_pinctrl_register(struct platform_device
*pdev
,
327 struct ltq_pinmux_info
*info
)
329 struct pinctrl_desc
*desc
;
334 desc
->pctlops
= <q_pctrl_ops
;
335 desc
->pmxops
= <q_pmx_ops
;
336 info
->dev
= &pdev
->dev
;
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
);
343 platform_set_drvdata(pdev
, info
);