dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / pinctrl / berlin / berlin.c
blob46f2b4818da3ae3a1c736b172916ab000252d3a2
1 /*
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.
13 #include <linux/io.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of.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>
25 #include "../core.h"
26 #include "../pinctrl-utils.h"
27 #include "berlin.h"
29 struct berlin_pinctrl {
30 struct regmap *regmap;
31 struct device *dev;
32 const struct berlin_pinctrl_desc *desc;
33 struct berlin_pinctrl_function *functions;
34 unsigned nfunctions;
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,
46 unsigned group)
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,
56 unsigned *num_maps)
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;
62 int ret, ngroups;
64 *map = NULL;
65 *num_maps = 0;
67 ret = of_property_read_string(node, "function", &function_name);
68 if (ret) {
69 dev_err(pctrl->dev,
70 "missing function property in node %s\n",
71 node->name);
72 return -EINVAL;
75 ngroups = of_property_count_strings(node, "groups");
76 if (ngroups < 0) {
77 dev_err(pctrl->dev,
78 "missing groups property in node %s\n",
79 node->name);
80 return -EINVAL;
83 ret = pinctrl_utils_reserve_map(pctrl_dev, map, &reserved_maps,
84 num_maps, ngroups);
85 if (ret) {
86 dev_err(pctrl->dev, "can't reserve map: %d\n", ret);
87 return ret;
90 of_property_for_each_string(node, "groups", prop, group_name) {
91 ret = pinctrl_utils_add_map_mux(pctrl_dev, map, &reserved_maps,
92 num_maps, group_name,
93 function_name);
94 if (ret) {
95 dev_err(pctrl->dev, "can't add map: %d\n", ret);
96 return ret;
100 return 0;
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,
118 unsigned function)
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,
126 unsigned function,
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;
135 return 0;
138 static struct berlin_desc_function *
139 berlin_pinctrl_find_function_by_name(struct berlin_pinctrl *pctrl,
140 const struct berlin_desc_group *group,
141 const char *fname)
143 struct berlin_desc_function *function = group->functions;
145 while (function->name) {
146 if (!strcmp(function->name, fname))
147 return function;
149 function++;
152 return NULL;
155 static int berlin_pinmux_set(struct pinctrl_dev *pctrl_dev,
156 unsigned function,
157 unsigned group)
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,
164 func->name);
165 u32 mask, val;
167 if (!function_desc)
168 return -EINVAL;
170 mask = GENMASK(group_desc->lsb + group_desc->bit_width - 1,
171 group_desc->lsb);
172 val = function_desc->muxval << group_desc->lsb;
173 regmap_update_bits(pctrl->regmap, group_desc->offset, mask, val);
175 return 0;
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,
186 const char *name)
188 struct berlin_pinctrl_function *function = pctrl->functions;
190 while (function->name) {
191 if (!strcmp(function->name, name)) {
192 function->ngroups++;
193 return -EEXIST;
195 function++;
198 function->name = name;
199 function->ngroups = 1;
201 pctrl->nfunctions++;
203 return 0;
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),
224 GFP_KERNEL);
225 if (!pctrl->functions)
226 return -ENOMEM;
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);
235 desc_function++;
239 pctrl->functions = krealloc(pctrl->functions,
240 pctrl->nfunctions * sizeof(*pctrl->functions),
241 GFP_KERNEL);
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;
251 const char **groups;
252 bool found = false;
254 while (function->name) {
255 if (!strcmp(desc_function->name, function->name)) {
256 found = true;
257 break;
259 function++;
262 if (!found)
263 return -EINVAL;
265 if (!function->groups) {
266 function->groups =
267 devm_kzalloc(&pdev->dev,
268 function->ngroups * sizeof(char *),
269 GFP_KERNEL);
271 if (!function->groups)
272 return -ENOMEM;
275 groups = function->groups;
276 while (*groups)
277 groups++;
279 *groups = desc_group->name;
281 desc_function++;
285 return 0;
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;
301 int ret;
303 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
304 if (!pctrl)
305 return -ENOMEM;
307 platform_set_drvdata(pdev, pctrl);
309 pctrl->regmap = regmap;
310 pctrl->dev = &pdev->dev;
311 pctrl->desc = desc;
313 ret = berlin_pinctrl_build_state(pdev);
314 if (ret) {
315 dev_err(dev, "cannot build driver state: %d\n", ret);
316 return 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);
325 return 0;
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);
336 if (IS_ERR(regmap))
337 return PTR_ERR(regmap);
339 return berlin_pinctrl_probe_regmap(pdev, desc, regmap);