1 // SPDX-License-Identifier: GPL-2.0
3 * Marvell Berlin SoC pinctrl core driver
5 * Copyright (C) 2014 Marvell Technology Group Ltd.
7 * Antoine Ténart <antoine.tenart@free-electrons.com>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/of_device.h>
16 #include <linux/pinctrl/pinctrl.h>
17 #include <linux/pinctrl/pinmux.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
23 #include "../pinctrl-utils.h"
26 struct berlin_pinctrl
{
27 struct regmap
*regmap
;
29 const struct berlin_pinctrl_desc
*desc
;
30 struct pinfunction
*functions
;
32 struct pinctrl_dev
*pctrl_dev
;
35 static int berlin_pinctrl_get_group_count(struct pinctrl_dev
*pctrl_dev
)
37 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
39 return pctrl
->desc
->ngroups
;
42 static const char *berlin_pinctrl_get_group_name(struct pinctrl_dev
*pctrl_dev
,
45 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
47 return pctrl
->desc
->groups
[group
].name
;
50 static int berlin_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctrl_dev
,
51 struct device_node
*node
,
52 struct pinctrl_map
**map
,
55 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
56 struct property
*prop
;
57 const char *function_name
, *group_name
;
58 unsigned reserved_maps
= 0;
64 ret
= of_property_read_string(node
, "function", &function_name
);
67 "missing function property in node %pOFn\n", node
);
71 ngroups
= of_property_count_strings(node
, "groups");
74 "missing groups property in node %pOFn\n", node
);
78 ret
= pinctrl_utils_reserve_map(pctrl_dev
, map
, &reserved_maps
,
81 dev_err(pctrl
->dev
, "can't reserve map: %d\n", ret
);
85 of_property_for_each_string(node
, "groups", prop
, group_name
) {
86 ret
= pinctrl_utils_add_map_mux(pctrl_dev
, map
, &reserved_maps
,
90 dev_err(pctrl
->dev
, "can't add map: %d\n", ret
);
98 static const struct pinctrl_ops berlin_pinctrl_ops
= {
99 .get_groups_count
= berlin_pinctrl_get_group_count
,
100 .get_group_name
= berlin_pinctrl_get_group_name
,
101 .dt_node_to_map
= berlin_pinctrl_dt_node_to_map
,
102 .dt_free_map
= pinctrl_utils_free_map
,
105 static int berlin_pinmux_get_functions_count(struct pinctrl_dev
*pctrl_dev
)
107 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
109 return pctrl
->nfunctions
;
112 static const char *berlin_pinmux_get_function_name(struct pinctrl_dev
*pctrl_dev
,
115 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
117 return pctrl
->functions
[function
].name
;
120 static int berlin_pinmux_get_function_groups(struct pinctrl_dev
*pctrl_dev
,
122 const char * const **groups
,
123 unsigned * const ngroups
)
125 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
127 *groups
= pctrl
->functions
[function
].groups
;
128 *ngroups
= pctrl
->functions
[function
].ngroups
;
133 static struct berlin_desc_function
*
134 berlin_pinctrl_find_function_by_name(struct berlin_pinctrl
*pctrl
,
135 const struct berlin_desc_group
*group
,
138 struct berlin_desc_function
*function
= group
->functions
;
140 while (function
->name
) {
141 if (!strcmp(function
->name
, fname
))
150 static int berlin_pinmux_set(struct pinctrl_dev
*pctrl_dev
,
154 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
155 const struct berlin_desc_group
*group_desc
= pctrl
->desc
->groups
+ group
;
156 struct pinfunction
*func
= pctrl
->functions
+ function
;
157 struct berlin_desc_function
*function_desc
=
158 berlin_pinctrl_find_function_by_name(pctrl
, group_desc
,
165 mask
= GENMASK(group_desc
->lsb
+ group_desc
->bit_width
- 1,
167 val
= function_desc
->muxval
<< group_desc
->lsb
;
168 regmap_update_bits(pctrl
->regmap
, group_desc
->offset
, mask
, val
);
173 static const struct pinmux_ops berlin_pinmux_ops
= {
174 .get_functions_count
= &berlin_pinmux_get_functions_count
,
175 .get_function_name
= &berlin_pinmux_get_function_name
,
176 .get_function_groups
= &berlin_pinmux_get_function_groups
,
177 .set_mux
= &berlin_pinmux_set
,
180 static int berlin_pinctrl_add_function(struct berlin_pinctrl
*pctrl
,
183 struct pinfunction
*function
= pctrl
->functions
;
185 while (function
->name
) {
186 if (!strcmp(function
->name
, name
)) {
193 function
->name
= name
;
194 function
->ngroups
= 1;
201 static int berlin_pinctrl_build_state(struct platform_device
*pdev
)
203 struct berlin_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
204 const struct berlin_desc_group
*desc_group
;
205 const struct berlin_desc_function
*desc_function
;
206 int i
, max_functions
= 0;
208 pctrl
->nfunctions
= 0;
210 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
211 desc_group
= pctrl
->desc
->groups
+ i
;
212 /* compute the maximum number of functions a group can have */
213 max_functions
+= 1 << (desc_group
->bit_width
+ 1);
216 /* we will reallocate later */
217 pctrl
->functions
= kcalloc(max_functions
, sizeof(*pctrl
->functions
), GFP_KERNEL
);
218 if (!pctrl
->functions
)
221 /* register all functions */
222 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
223 desc_group
= pctrl
->desc
->groups
+ i
;
224 desc_function
= desc_group
->functions
;
226 while (desc_function
->name
) {
227 berlin_pinctrl_add_function(pctrl
, desc_function
->name
);
232 pctrl
->functions
= krealloc(pctrl
->functions
,
233 pctrl
->nfunctions
* sizeof(*pctrl
->functions
),
235 if (!pctrl
->functions
)
238 /* map functions to theirs groups */
239 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
240 desc_group
= pctrl
->desc
->groups
+ i
;
241 desc_function
= desc_group
->functions
;
243 while (desc_function
->name
) {
244 struct pinfunction
*function
= pctrl
->functions
;
248 while (function
->name
) {
249 if (!strcmp(desc_function
->name
, function
->name
)) {
257 kfree(pctrl
->functions
);
261 if (!function
->groups
) {
263 devm_kcalloc(&pdev
->dev
,
265 sizeof(*function
->groups
),
267 if (!function
->groups
) {
268 kfree(pctrl
->functions
);
273 groups
= (const char **)function
->groups
;
277 *groups
= desc_group
->name
;
286 static struct pinctrl_desc berlin_pctrl_desc
= {
287 .name
= "berlin-pinctrl",
288 .pctlops
= &berlin_pinctrl_ops
,
289 .pmxops
= &berlin_pinmux_ops
,
290 .owner
= THIS_MODULE
,
293 int berlin_pinctrl_probe_regmap(struct platform_device
*pdev
,
294 const struct berlin_pinctrl_desc
*desc
,
295 struct regmap
*regmap
)
297 struct device
*dev
= &pdev
->dev
;
298 struct berlin_pinctrl
*pctrl
;
301 pctrl
= devm_kzalloc(dev
, sizeof(*pctrl
), GFP_KERNEL
);
305 platform_set_drvdata(pdev
, pctrl
);
307 pctrl
->regmap
= regmap
;
308 pctrl
->dev
= &pdev
->dev
;
311 ret
= berlin_pinctrl_build_state(pdev
);
313 dev_err(dev
, "cannot build driver state: %d\n", ret
);
317 pctrl
->pctrl_dev
= devm_pinctrl_register(dev
, &berlin_pctrl_desc
,
319 if (IS_ERR(pctrl
->pctrl_dev
)) {
320 dev_err(dev
, "failed to register pinctrl driver\n");
321 return PTR_ERR(pctrl
->pctrl_dev
);
327 int berlin_pinctrl_probe(struct platform_device
*pdev
,
328 const struct berlin_pinctrl_desc
*desc
)
330 struct device
*dev
= &pdev
->dev
;
331 struct device_node
*parent_np
= of_get_parent(dev
->of_node
);
332 struct regmap
*regmap
= syscon_node_to_regmap(parent_np
);
334 of_node_put(parent_np
);
336 return PTR_ERR(regmap
);
338 return berlin_pinctrl_probe_regmap(pdev
, desc
, regmap
);