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/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
25 #include "../pinctrl-utils.h"
28 struct berlin_pinctrl {
29 struct regmap *regmap;
31 const struct berlin_pinctrl_desc *desc;
32 struct berlin_pinctrl_function *functions;
34 struct pinctrl_dev *pctrl_dev;
37 static int berlin_pinctrl_get_group_count(struct pinctrl_dev *pctrl_dev)
39 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
41 return pctrl->desc->ngroups;
44 static const char *berlin_pinctrl_get_group_name(struct pinctrl_dev *pctrl_dev,
47 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
49 return pctrl->desc->groups[group].name;
52 static int berlin_pinctrl_dt_node_to_map(struct pinctrl_dev *pctrl_dev,
53 struct device_node *node,
54 struct pinctrl_map **map,
57 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
58 struct property *prop;
59 const char *function_name, *group_name;
60 unsigned reserved_maps = 0;
66 ret = of_property_read_string(node, "function", &function_name);
69 "missing function property in node %s\n",
74 ngroups = of_property_count_strings(node, "groups");
77 "missing groups property in node %s\n",
82 ret = pinctrl_utils_reserve_map(pctrl_dev, map, &reserved_maps,
85 dev_err(pctrl->dev, "can't reserve map: %d\n", ret);
89 of_property_for_each_string(node, "groups", prop, group_name) {
90 ret = pinctrl_utils_add_map_mux(pctrl_dev, map, &reserved_maps,
94 dev_err(pctrl->dev, "can't add map: %d\n", ret);
102 static const struct pinctrl_ops berlin_pinctrl_ops = {
103 .get_groups_count = &berlin_pinctrl_get_group_count,
104 .get_group_name = &berlin_pinctrl_get_group_name,
105 .dt_node_to_map = &berlin_pinctrl_dt_node_to_map,
106 .dt_free_map = &pinctrl_utils_dt_free_map,
109 static int berlin_pinmux_get_functions_count(struct pinctrl_dev *pctrl_dev)
111 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
113 return pctrl->nfunctions;
116 static const char *berlin_pinmux_get_function_name(struct pinctrl_dev *pctrl_dev,
119 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
121 return pctrl->functions[function].name;
124 static int berlin_pinmux_get_function_groups(struct pinctrl_dev *pctrl_dev,
126 const char * const **groups,
127 unsigned * const num_groups)
129 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
131 *groups = pctrl->functions[function].groups;
132 *num_groups = pctrl->functions[function].ngroups;
137 static struct berlin_desc_function *
138 berlin_pinctrl_find_function_by_name(struct berlin_pinctrl *pctrl,
139 const struct berlin_desc_group *group,
142 struct berlin_desc_function *function = group->functions;
144 while (function->name) {
145 if (!strcmp(function->name, fname))
154 static int berlin_pinmux_set(struct pinctrl_dev *pctrl_dev,
158 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
159 const struct berlin_desc_group *group_desc = pctrl->desc->groups + group;
160 struct berlin_pinctrl_function *func = pctrl->functions + function;
161 struct berlin_desc_function *function_desc =
162 berlin_pinctrl_find_function_by_name(pctrl, group_desc,
169 mask = GENMASK(group_desc->lsb + group_desc->bit_width - 1,
171 val = function_desc->muxval << group_desc->lsb;
172 regmap_update_bits(pctrl->regmap, group_desc->offset, mask, val);
177 static const struct pinmux_ops berlin_pinmux_ops = {
178 .get_functions_count = &berlin_pinmux_get_functions_count,
179 .get_function_name = &berlin_pinmux_get_function_name,
180 .get_function_groups = &berlin_pinmux_get_function_groups,
181 .set_mux = &berlin_pinmux_set,
184 static int berlin_pinctrl_add_function(struct berlin_pinctrl *pctrl,
187 struct berlin_pinctrl_function *function = pctrl->functions;
189 while (function->name) {
190 if (!strcmp(function->name, name)) {
197 function->name = name;
198 function->ngroups = 1;
205 static int berlin_pinctrl_build_state(struct platform_device *pdev)
207 struct berlin_pinctrl *pctrl = platform_get_drvdata(pdev);
208 struct berlin_desc_group const *desc_group;
209 struct berlin_desc_function const *desc_function;
210 int i, max_functions = 0;
212 pctrl->nfunctions = 0;
214 for (i = 0; i < pctrl->desc->ngroups; i++) {
215 desc_group = pctrl->desc->groups + i;
216 /* compute the maxiumum number of functions a group can have */
217 max_functions += 1 << (desc_group->bit_width + 1);
220 /* we will reallocate later */
221 pctrl->functions = devm_kzalloc(&pdev->dev,
222 max_functions * sizeof(*pctrl->functions),
224 if (!pctrl->functions)
227 /* register all functions */
228 for (i = 0; i < pctrl->desc->ngroups; i++) {
229 desc_group = pctrl->desc->groups + i;
230 desc_function = desc_group->functions;
232 while (desc_function->name) {
233 berlin_pinctrl_add_function(pctrl, desc_function->name);
238 pctrl->functions = krealloc(pctrl->functions,
239 pctrl->nfunctions * sizeof(*pctrl->functions),
242 /* map functions to theirs groups */
243 for (i = 0; i < pctrl->desc->ngroups; i++) {
244 desc_group = pctrl->desc->groups + i;
245 desc_function = desc_group->functions;
247 while (desc_function->name) {
248 struct berlin_pinctrl_function
249 *function = pctrl->functions;
253 while (function->name) {
254 if (!strcmp(desc_function->name, function->name)) {
264 if (!function->groups) {
266 devm_kzalloc(&pdev->dev,
267 function->ngroups * sizeof(char *),
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(struct platform_device *pdev,
295 const struct berlin_pinctrl_desc *desc)
297 struct device *dev = &pdev->dev;
298 struct berlin_pinctrl *pctrl;
299 struct regmap *regmap;
302 regmap = dev_get_regmap(&pdev->dev, NULL);
306 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
310 platform_set_drvdata(pdev, pctrl);
312 pctrl->regmap = regmap;
313 pctrl->dev = &pdev->dev;
316 ret = berlin_pinctrl_build_state(pdev);
318 dev_err(dev, "cannot build driver state: %d\n", ret);
322 pctrl->pctrl_dev = pinctrl_register(&berlin_pctrl_desc, dev, pctrl);
323 if (!pctrl->pctrl_dev) {
324 dev_err(dev, "failed to register pinctrl driver\n");