2 * Marvell Berlin SoC pinctrl core driver
4 * Copyright (C) 2014 Marvell Technology Group Ltd.
6 * Antoine Ténart <antoine.tenart@free-electrons.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/slab.h>
26 #include "../pinctrl-utils.h"
29 struct berlin_pinctrl
{
30 struct regmap
*regmap
;
32 const struct berlin_pinctrl_desc
*desc
;
33 struct berlin_pinctrl_function
*functions
;
35 struct pinctrl_dev
*pctrl_dev
;
38 static int berlin_pinctrl_get_group_count(struct pinctrl_dev
*pctrl_dev
)
40 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
42 return pctrl
->desc
->ngroups
;
45 static const char *berlin_pinctrl_get_group_name(struct pinctrl_dev
*pctrl_dev
,
48 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
50 return pctrl
->desc
->groups
[group
].name
;
53 static int berlin_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctrl_dev
,
54 struct device_node
*node
,
55 struct pinctrl_map
**map
,
58 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
59 struct property
*prop
;
60 const char *function_name
, *group_name
;
61 unsigned reserved_maps
= 0;
67 ret
= of_property_read_string(node
, "function", &function_name
);
70 "missing function property in node %s\n",
75 ngroups
= of_property_count_strings(node
, "groups");
78 "missing groups property in node %s\n",
83 ret
= pinctrl_utils_reserve_map(pctrl_dev
, map
, &reserved_maps
,
86 dev_err(pctrl
->dev
, "can't reserve map: %d\n", ret
);
90 of_property_for_each_string(node
, "groups", prop
, group_name
) {
91 ret
= pinctrl_utils_add_map_mux(pctrl_dev
, map
, &reserved_maps
,
95 dev_err(pctrl
->dev
, "can't add map: %d\n", ret
);
103 static const struct pinctrl_ops berlin_pinctrl_ops
= {
104 .get_groups_count
= &berlin_pinctrl_get_group_count
,
105 .get_group_name
= &berlin_pinctrl_get_group_name
,
106 .dt_node_to_map
= &berlin_pinctrl_dt_node_to_map
,
107 .dt_free_map
= &pinctrl_utils_dt_free_map
,
110 static int berlin_pinmux_get_functions_count(struct pinctrl_dev
*pctrl_dev
)
112 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
114 return pctrl
->nfunctions
;
117 static const char *berlin_pinmux_get_function_name(struct pinctrl_dev
*pctrl_dev
,
120 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
122 return pctrl
->functions
[function
].name
;
125 static int berlin_pinmux_get_function_groups(struct pinctrl_dev
*pctrl_dev
,
127 const char * const **groups
,
128 unsigned * const num_groups
)
130 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
132 *groups
= pctrl
->functions
[function
].groups
;
133 *num_groups
= pctrl
->functions
[function
].ngroups
;
138 static struct berlin_desc_function
*
139 berlin_pinctrl_find_function_by_name(struct berlin_pinctrl
*pctrl
,
140 const struct berlin_desc_group
*group
,
143 struct berlin_desc_function
*function
= group
->functions
;
145 while (function
->name
) {
146 if (!strcmp(function
->name
, fname
))
155 static int berlin_pinmux_set(struct pinctrl_dev
*pctrl_dev
,
159 struct berlin_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
160 const struct berlin_desc_group
*group_desc
= pctrl
->desc
->groups
+ group
;
161 struct berlin_pinctrl_function
*func
= pctrl
->functions
+ function
;
162 struct berlin_desc_function
*function_desc
=
163 berlin_pinctrl_find_function_by_name(pctrl
, group_desc
,
170 mask
= GENMASK(group_desc
->lsb
+ group_desc
->bit_width
- 1,
172 val
= function_desc
->muxval
<< group_desc
->lsb
;
173 regmap_update_bits(pctrl
->regmap
, group_desc
->offset
, mask
, val
);
178 static const struct pinmux_ops berlin_pinmux_ops
= {
179 .get_functions_count
= &berlin_pinmux_get_functions_count
,
180 .get_function_name
= &berlin_pinmux_get_function_name
,
181 .get_function_groups
= &berlin_pinmux_get_function_groups
,
182 .set_mux
= &berlin_pinmux_set
,
185 static int berlin_pinctrl_add_function(struct berlin_pinctrl
*pctrl
,
188 struct berlin_pinctrl_function
*function
= pctrl
->functions
;
190 while (function
->name
) {
191 if (!strcmp(function
->name
, name
)) {
198 function
->name
= name
;
199 function
->ngroups
= 1;
206 static int berlin_pinctrl_build_state(struct platform_device
*pdev
)
208 struct berlin_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
209 struct berlin_desc_group
const *desc_group
;
210 struct berlin_desc_function
const *desc_function
;
211 int i
, max_functions
= 0;
213 pctrl
->nfunctions
= 0;
215 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
216 desc_group
= pctrl
->desc
->groups
+ i
;
217 /* compute the maxiumum number of functions a group can have */
218 max_functions
+= 1 << (desc_group
->bit_width
+ 1);
221 /* we will reallocate later */
222 pctrl
->functions
= devm_kzalloc(&pdev
->dev
,
223 max_functions
* sizeof(*pctrl
->functions
),
225 if (!pctrl
->functions
)
228 /* register all functions */
229 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
230 desc_group
= pctrl
->desc
->groups
+ i
;
231 desc_function
= desc_group
->functions
;
233 while (desc_function
->name
) {
234 berlin_pinctrl_add_function(pctrl
, desc_function
->name
);
239 pctrl
->functions
= krealloc(pctrl
->functions
,
240 pctrl
->nfunctions
* sizeof(*pctrl
->functions
),
243 /* map functions to theirs groups */
244 for (i
= 0; i
< pctrl
->desc
->ngroups
; i
++) {
245 desc_group
= pctrl
->desc
->groups
+ i
;
246 desc_function
= desc_group
->functions
;
248 while (desc_function
->name
) {
249 struct berlin_pinctrl_function
250 *function
= pctrl
->functions
;
254 while (function
->name
) {
255 if (!strcmp(desc_function
->name
, function
->name
)) {
265 if (!function
->groups
) {
267 devm_kzalloc(&pdev
->dev
,
268 function
->ngroups
* sizeof(char *),
271 if (!function
->groups
)
275 groups
= function
->groups
;
279 *groups
= desc_group
->name
;
288 static struct pinctrl_desc berlin_pctrl_desc
= {
289 .name
= "berlin-pinctrl",
290 .pctlops
= &berlin_pinctrl_ops
,
291 .pmxops
= &berlin_pinmux_ops
,
292 .owner
= THIS_MODULE
,
295 int berlin_pinctrl_probe_regmap(struct platform_device
*pdev
,
296 const struct berlin_pinctrl_desc
*desc
,
297 struct regmap
*regmap
)
299 struct device
*dev
= &pdev
->dev
;
300 struct berlin_pinctrl
*pctrl
;
303 pctrl
= devm_kzalloc(dev
, sizeof(*pctrl
), GFP_KERNEL
);
307 platform_set_drvdata(pdev
, pctrl
);
309 pctrl
->regmap
= regmap
;
310 pctrl
->dev
= &pdev
->dev
;
313 ret
= berlin_pinctrl_build_state(pdev
);
315 dev_err(dev
, "cannot build driver state: %d\n", ret
);
319 pctrl
->pctrl_dev
= pinctrl_register(&berlin_pctrl_desc
, dev
, pctrl
);
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
);