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 void berlin_pinctrl_dt_free_map(struct pinctrl_dev *pctrl_dev,
103 struct pinctrl_map *map,
108 for (i = 0; i < nmaps; i++) {
109 if (map[i].type == PIN_MAP_TYPE_MUX_GROUP) {
110 kfree(map[i].data.mux.group);
112 /* a function can be applied to multiple groups */
114 kfree(map[i].data.mux.function);
121 static const struct pinctrl_ops berlin_pinctrl_ops = {
122 .get_groups_count = &berlin_pinctrl_get_group_count,
123 .get_group_name = &berlin_pinctrl_get_group_name,
124 .dt_node_to_map = &berlin_pinctrl_dt_node_to_map,
125 .dt_free_map = &berlin_pinctrl_dt_free_map,
128 static int berlin_pinmux_get_functions_count(struct pinctrl_dev *pctrl_dev)
130 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
132 return pctrl->nfunctions;
135 static const char *berlin_pinmux_get_function_name(struct pinctrl_dev *pctrl_dev,
138 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
140 return pctrl->functions[function].name;
143 static int berlin_pinmux_get_function_groups(struct pinctrl_dev *pctrl_dev,
145 const char * const **groups,
146 unsigned * const num_groups)
148 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
150 *groups = pctrl->functions[function].groups;
151 *num_groups = pctrl->functions[function].ngroups;
156 static struct berlin_desc_function *
157 berlin_pinctrl_find_function_by_name(struct berlin_pinctrl *pctrl,
158 const struct berlin_desc_group *group,
161 struct berlin_desc_function *function = group->functions;
163 while (function->name) {
164 if (!strcmp(function->name, fname))
173 static int berlin_pinmux_enable(struct pinctrl_dev *pctrl_dev,
177 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
178 const struct berlin_desc_group *group_desc = pctrl->desc->groups + group;
179 struct berlin_pinctrl_function *func = pctrl->functions + function;
180 struct berlin_desc_function *function_desc =
181 berlin_pinctrl_find_function_by_name(pctrl, group_desc,
188 mask = GENMASK(group_desc->lsb + group_desc->bit_width - 1,
190 val = function_desc->muxval << group_desc->lsb;
191 regmap_update_bits(pctrl->regmap, group_desc->offset, mask, val);
196 static const struct pinmux_ops berlin_pinmux_ops = {
197 .get_functions_count = &berlin_pinmux_get_functions_count,
198 .get_function_name = &berlin_pinmux_get_function_name,
199 .get_function_groups = &berlin_pinmux_get_function_groups,
200 .enable = &berlin_pinmux_enable,
203 static int berlin_pinctrl_add_function(struct berlin_pinctrl *pctrl,
206 struct berlin_pinctrl_function *function = pctrl->functions;
208 while (function->name) {
209 if (!strcmp(function->name, name)) {
216 function->name = name;
217 function->ngroups = 1;
224 static int berlin_pinctrl_build_state(struct platform_device *pdev)
226 struct berlin_pinctrl *pctrl = platform_get_drvdata(pdev);
227 struct berlin_desc_group const *desc_group;
228 struct berlin_desc_function const *desc_function;
229 int i, max_functions = 0;
231 pctrl->nfunctions = 0;
233 for (i = 0; i < pctrl->desc->ngroups; i++) {
234 desc_group = pctrl->desc->groups + i;
235 /* compute the maxiumum number of functions a group can have */
236 max_functions += 1 << (desc_group->bit_width + 1);
239 /* we will reallocate later */
240 pctrl->functions = devm_kzalloc(&pdev->dev,
241 max_functions * sizeof(*pctrl->functions),
243 if (!pctrl->functions)
246 /* register all functions */
247 for (i = 0; i < pctrl->desc->ngroups; i++) {
248 desc_group = pctrl->desc->groups + i;
249 desc_function = desc_group->functions;
251 while (desc_function->name) {
252 berlin_pinctrl_add_function(pctrl, desc_function->name);
257 pctrl->functions = krealloc(pctrl->functions,
258 pctrl->nfunctions * sizeof(*pctrl->functions),
261 /* map functions to theirs groups */
262 for (i = 0; i < pctrl->desc->ngroups; i++) {
263 desc_group = pctrl->desc->groups + i;
264 desc_function = desc_group->functions;
266 while (desc_function->name) {
267 struct berlin_pinctrl_function
268 *function = pctrl->functions;
272 while (function->name) {
273 if (!strcmp(desc_function->name, function->name)) {
283 if (!function->groups) {
285 devm_kzalloc(&pdev->dev,
286 function->ngroups * sizeof(char *),
289 if (!function->groups)
293 groups = function->groups;
297 *groups = desc_group->name;
306 static struct pinctrl_desc berlin_pctrl_desc = {
307 .name = "berlin-pinctrl",
308 .pctlops = &berlin_pinctrl_ops,
309 .pmxops = &berlin_pinmux_ops,
310 .owner = THIS_MODULE,
313 int berlin_pinctrl_probe(struct platform_device *pdev,
314 const struct berlin_pinctrl_desc *desc)
316 struct device *dev = &pdev->dev;
317 struct berlin_pinctrl *pctrl;
318 struct regmap *regmap;
321 regmap = dev_get_regmap(&pdev->dev, NULL);
325 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
329 platform_set_drvdata(pdev, pctrl);
331 pctrl->regmap = regmap;
332 pctrl->dev = &pdev->dev;
335 ret = berlin_pinctrl_build_state(pdev);
337 dev_err(dev, "cannot build driver state: %d\n", ret);
341 pctrl->pctrl_dev = pinctrl_register(&berlin_pctrl_desc, dev, pctrl);
342 if (!pctrl->pctrl_dev) {
343 dev_err(dev, "failed to register pinctrl driver\n");