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 berlin_pinctrl_function
*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 %s\n",
72 ngroups
= of_property_count_strings(node
, "groups");
75 "missing groups property in node %s\n",
80 ret
= pinctrl_utils_reserve_map(pctrl_dev
, map
, &reserved_maps
,
83 dev_err(pctrl
->dev
, "can't reserve map: %d\n", ret
);
87 of_property_for_each_string(node
, "groups", prop
, group_name
) {
88 ret
= pinctrl_utils_add_map_mux(pctrl_dev
, map
, &reserved_maps
,
92 dev_err(pctrl
->dev
, "can't add map: %d\n", ret
);
100 static const struct pinctrl_ops berlin_pinctrl_ops
= {
101 .get_groups_count
= &berlin_pinctrl_get_group_count
,
102 .get_group_name
= &berlin_pinctrl_get_group_name
,
103 .dt_node_to_map
= &berlin_pinctrl_dt_node_to_map
,
104 .dt_free_map
= &pinctrl_utils_free_map
,
107 static int berlin_pinmux_get_functions_count(struct pinctrl_dev
*pctrl_dev
)
109 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
111 return pctrl
->nfunctions
;
114 static const char *berlin_pinmux_get_function_name(struct pinctrl_dev
*pctrl_dev
,
117 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
119 return pctrl
->functions
[function
].name
;
122 static int berlin_pinmux_get_function_groups(struct pinctrl_dev
*pctrl_dev
,
124 const char * const **groups
,
125 unsigned * const num_groups
)
127 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
129 *groups
= pctrl
->functions
[function
].groups
;
130 *num_groups
= pctrl
->functions
[function
].ngroups
;
135 static struct berlin_desc_function
*
136 berlin_pinctrl_find_function_by_name(struct berlin_pinctrl
*pctrl
,
137 const struct berlin_desc_group
*group
,
140 struct berlin_desc_function
*function
= group
->functions
;
142 while (function
->name
) {
143 if (!strcmp(function
->name
, fname
))
152 static int berlin_pinmux_set(struct pinctrl_dev
*pctrl_dev
,
156 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
157 const struct berlin_desc_group
*group_desc
= pctrl
->desc
->groups
+ group
;
158 struct berlin_pinctrl_function
*func
= pctrl
->functions
+ function
;
159 struct berlin_desc_function
*function_desc
=
160 berlin_pinctrl_find_function_by_name(pctrl
, group_desc
,
167 mask
= GENMASK(group_desc
->lsb
+ group_desc
->bit_width
- 1,
169 val
= function_desc
->muxval
<< group_desc
->lsb
;
170 regmap_update_bits(pctrl
->regmap
, group_desc
->offset
, mask
, val
);
175 static const struct pinmux_ops berlin_pinmux_ops
= {
176 .get_functions_count
= &berlin_pinmux_get_functions_count
,
177 .get_function_name
= &berlin_pinmux_get_function_name
,
178 .get_function_groups
= &berlin_pinmux_get_function_groups
,
179 .set_mux
= &berlin_pinmux_set
,
182 static int berlin_pinctrl_add_function(struct berlin_pinctrl
*pctrl
,
185 struct berlin_pinctrl_function
*function
= pctrl
->functions
;
187 while (function
->name
) {
188 if (!strcmp(function
->name
, name
)) {
195 function
->name
= name
;
196 function
->ngroups
= 1;
203 static int berlin_pinctrl_build_state(struct platform_device
*pdev
)
205 struct berlin_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
206 const struct berlin_desc_group
*desc_group
;
207 const struct berlin_desc_function
*desc_function
;
208 int i
, max_functions
= 0;
210 pctrl
->nfunctions
= 0;
212 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
213 desc_group
= pctrl
->desc
->groups
+ i
;
214 /* compute the maxiumum number of functions a group can have */
215 max_functions
+= 1 << (desc_group
->bit_width
+ 1);
218 /* we will reallocate later */
219 pctrl
->functions
= devm_kcalloc(&pdev
->dev
,
221 sizeof(*pctrl
->functions
),
223 if (!pctrl
->functions
)
226 /* register all functions */
227 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
228 desc_group
= pctrl
->desc
->groups
+ i
;
229 desc_function
= desc_group
->functions
;
231 while (desc_function
->name
) {
232 berlin_pinctrl_add_function(pctrl
, desc_function
->name
);
237 pctrl
->functions
= krealloc(pctrl
->functions
,
238 pctrl
->nfunctions
* sizeof(*pctrl
->functions
),
241 /* map functions to theirs groups */
242 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
243 desc_group
= pctrl
->desc
->groups
+ i
;
244 desc_function
= desc_group
->functions
;
246 while (desc_function
->name
) {
247 struct berlin_pinctrl_function
248 *function
= pctrl
->functions
;
252 while (function
->name
) {
253 if (!strcmp(desc_function
->name
, function
->name
)) {
263 if (!function
->groups
) {
265 devm_kcalloc(&pdev
->dev
,
270 if (!function
->groups
)
274 groups
= function
->groups
;
278 *groups
= desc_group
->name
;
287 static struct pinctrl_desc berlin_pctrl_desc
= {
288 .name
= "berlin-pinctrl",
289 .pctlops
= &berlin_pinctrl_ops
,
290 .pmxops
= &berlin_pinmux_ops
,
291 .owner
= THIS_MODULE
,
294 int berlin_pinctrl_probe_regmap(struct platform_device
*pdev
,
295 const struct berlin_pinctrl_desc
*desc
,
296 struct regmap
*regmap
)
298 struct device
*dev
= &pdev
->dev
;
299 struct berlin_pinctrl
*pctrl
;
302 pctrl
= devm_kzalloc(dev
, sizeof(*pctrl
), GFP_KERNEL
);
306 platform_set_drvdata(pdev
, pctrl
);
308 pctrl
->regmap
= regmap
;
309 pctrl
->dev
= &pdev
->dev
;
312 ret
= berlin_pinctrl_build_state(pdev
);
314 dev_err(dev
, "cannot build driver state: %d\n", ret
);
318 pctrl
->pctrl_dev
= devm_pinctrl_register(dev
, &berlin_pctrl_desc
,
320 if (IS_ERR(pctrl
->pctrl_dev
)) {
321 dev_err(dev
, "failed to register pinctrl driver\n");
322 return PTR_ERR(pctrl
->pctrl_dev
);
328 int berlin_pinctrl_probe(struct platform_device
*pdev
,
329 const struct berlin_pinctrl_desc
*desc
)
331 struct device
*dev
= &pdev
->dev
;
332 struct device_node
*parent_np
= of_get_parent(dev
->of_node
);
333 struct regmap
*regmap
= syscon_node_to_regmap(parent_np
);
335 of_node_put(parent_np
);
337 return PTR_ERR(regmap
);
339 return berlin_pinctrl_probe_regmap(pdev
, desc
, regmap
);