aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pinctrl/berlin
diff options
context:
space:
mode:
authorAntoine Tenart <antoine.tenart@free-electrons.com>2014-05-19 13:36:29 -0400
committerLinus Walleij <linus.walleij@linaro.org>2014-05-22 18:05:00 -0400
commit3de68d331c24e8136da147801c2d090c3038eb32 (patch)
treec12a4211b62a6db0dbd8a87b602451317fd40762 /drivers/pinctrl/berlin
parent3ff95885ed7c974ecf308c1d4d27baaa218e32dc (diff)
pinctrl: berlin: add the core pinctrl driver for Marvell Berlin SoCs
The Marvell Berlin boards have a group based pinmuxing mechanism. This adds the core driver support. We actually do not need any information about the pins here and only have the definition of the groups. Let's take the example of the uart0 pinmuxing on the BG2Q. Balls BK4 and BH6 are muxed to respectively UART0 RX and TX if the group GSM12 is set to mode 0: Group Modes Offset Base Offset LSB Bit Width GSM12 3 sm_base 0x40 0x10 0x2 Ball Group Mode 0 Mode 1 Mode 2 BK4 GSM12 UART0_RX IrDA0_RX GPIO9 BH6 GSM12 UART0_TX IrDA0_TX GPIO10 So in order to configure BK4 -> UART0_TX and BH6 -> UART0_RX, we need to set (sm_base + 0x40 + 0x10) &= ff3fffff. As pin control registers are part of either chip control or system control registers, that deal with a bunch of other functions we rely on a regmap instead of exclusively remapping any resources. Signed-off-by: Antoine Tenart <antoine.tenart@free-electrons.com> Acked-by: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers/pinctrl/berlin')
-rw-r--r--drivers/pinctrl/berlin/Kconfig8
-rw-r--r--drivers/pinctrl/berlin/Makefile1
-rw-r--r--drivers/pinctrl/berlin/berlin.c348
-rw-r--r--drivers/pinctrl/berlin/berlin.h61
4 files changed, 418 insertions, 0 deletions
diff --git a/drivers/pinctrl/berlin/Kconfig b/drivers/pinctrl/berlin/Kconfig
new file mode 100644
index 000000000000..d81c1bb13697
--- /dev/null
+++ b/drivers/pinctrl/berlin/Kconfig
@@ -0,0 +1,8 @@
1if ARCH_BERLIN
2
3config PINCTRL_BERLIN
4 bool
5 select PINMUX
6 select REGMAP_MMIO
7
8endif
diff --git a/drivers/pinctrl/berlin/Makefile b/drivers/pinctrl/berlin/Makefile
new file mode 100644
index 000000000000..251a2b4e1057
--- /dev/null
+++ b/drivers/pinctrl/berlin/Makefile
@@ -0,0 +1 @@
obj-$(CONFIG_PINCTRL_BERLIN) += berlin.o
diff --git a/drivers/pinctrl/berlin/berlin.c b/drivers/pinctrl/berlin/berlin.c
new file mode 100644
index 000000000000..edf5d2fd2b22
--- /dev/null
+++ b/drivers/pinctrl/berlin/berlin.c
@@ -0,0 +1,348 @@
1/*
2 * Marvell Berlin SoC pinctrl core driver
3 *
4 * Copyright (C) 2014 Marvell Technology Group Ltd.
5 *
6 * Antoine Ténart <antoine.tenart@free-electrons.com>
7 *
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.
11 */
12
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/of.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>
23
24#include "../core.h"
25#include "../pinctrl-utils.h"
26#include "berlin.h"
27
28struct berlin_pinctrl {
29 struct regmap *regmap;
30 struct device *dev;
31 const struct berlin_pinctrl_desc *desc;
32 struct berlin_pinctrl_function *functions;
33 unsigned nfunctions;
34 struct pinctrl_dev *pctrl_dev;
35};
36
37static int berlin_pinctrl_get_group_count(struct pinctrl_dev *pctrl_dev)
38{
39 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
40
41 return pctrl->desc->ngroups;
42}
43
44static const char *berlin_pinctrl_get_group_name(struct pinctrl_dev *pctrl_dev,
45 unsigned group)
46{
47 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
48
49 return pctrl->desc->groups[group].name;
50}
51
52static int berlin_pinctrl_dt_node_to_map(struct pinctrl_dev *pctrl_dev,
53 struct device_node *node,
54 struct pinctrl_map **map,
55 unsigned *num_maps)
56{
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;
61 int ret, ngroups;
62
63 *map = NULL;
64 *num_maps = 0;
65
66 ret = of_property_read_string(node, "function", &function_name);
67 if (ret) {
68 dev_err(pctrl->dev,
69 "missing function property in node %s\n",
70 node->name);
71 return -EINVAL;
72 }
73
74 ngroups = of_property_count_strings(node, "groups");
75 if (ngroups < 0) {
76 dev_err(pctrl->dev,
77 "missing groups property in node %s\n",
78 node->name);
79 return -EINVAL;
80 }
81
82 ret = pinctrl_utils_reserve_map(pctrl_dev, map, &reserved_maps,
83 num_maps, ngroups);
84 if (ret) {
85 dev_err(pctrl->dev, "can't reserve map: %d\n", ret);
86 return ret;
87 }
88
89 of_property_for_each_string(node, "groups", prop, group_name) {
90 ret = pinctrl_utils_add_map_mux(pctrl_dev, map, &reserved_maps,
91 num_maps, group_name,
92 function_name);
93 if (ret) {
94 dev_err(pctrl->dev, "can't add map: %d\n", ret);
95 return ret;
96 }
97 }
98
99 return 0;
100}
101
102static void berlin_pinctrl_dt_free_map(struct pinctrl_dev *pctrl_dev,
103 struct pinctrl_map *map,
104 unsigned nmaps)
105{
106 int i;
107
108 for (i = 0; i < nmaps; i++) {
109 if (map[i].type == PIN_MAP_TYPE_MUX_GROUP) {
110 kfree(map[i].data.mux.group);
111
112 /* a function can be applied to multiple groups */
113 if (i == 0)
114 kfree(map[i].data.mux.function);
115 }
116 }
117
118 kfree(map);
119}
120
121static 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,
126};
127
128static int berlin_pinmux_get_functions_count(struct pinctrl_dev *pctrl_dev)
129{
130 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
131
132 return pctrl->nfunctions;
133}
134
135static const char *berlin_pinmux_get_function_name(struct pinctrl_dev *pctrl_dev,
136 unsigned function)
137{
138 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
139
140 return pctrl->functions[function].name;
141}
142
143static int berlin_pinmux_get_function_groups(struct pinctrl_dev *pctrl_dev,
144 unsigned function,
145 const char * const **groups,
146 unsigned * const num_groups)
147{
148 struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
149
150 *groups = pctrl->functions[function].groups;
151 *num_groups = pctrl->functions[function].ngroups;
152
153 return 0;
154}
155
156static struct berlin_desc_function *
157berlin_pinctrl_find_function_by_name(struct berlin_pinctrl *pctrl,
158 const struct berlin_desc_group *group,
159 const char *fname)
160{
161 struct berlin_desc_function *function = group->functions;
162
163 while (function->name) {
164 if (!strcmp(function->name, fname))
165 return function;
166
167 function++;
168 }
169
170 return NULL;
171}
172
173static int berlin_pinmux_enable(struct pinctrl_dev *pctrl_dev,
174 unsigned function,
175 unsigned group)
176{
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,
182 func->name);
183 u32 mask, val;
184
185 if (!function_desc)
186 return -EINVAL;
187
188 mask = GENMASK(group_desc->lsb + group_desc->bit_width - 1,
189 group_desc->lsb);
190 val = function_desc->muxval << group_desc->lsb;
191 regmap_update_bits(pctrl->regmap, group_desc->offset, mask, val);
192
193 return 0;
194}
195
196static 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,
201};
202
203static int berlin_pinctrl_add_function(struct berlin_pinctrl *pctrl,
204 const char *name)
205{
206 struct berlin_pinctrl_function *function = pctrl->functions;
207
208 while (function->name) {
209 if (!strcmp(function->name, name)) {
210 function->ngroups++;
211 return -EEXIST;
212 }
213 function++;
214 }
215
216 function->name = name;
217 function->ngroups = 1;
218
219 pctrl->nfunctions++;
220
221 return 0;
222}
223
224static int berlin_pinctrl_build_state(struct platform_device *pdev)
225{
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;
230
231 pctrl->nfunctions = 0;
232
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);
237 }
238
239 /* we will reallocate later */
240 pctrl->functions = devm_kzalloc(&pdev->dev,
241 max_functions * sizeof(*pctrl->functions),
242 GFP_KERNEL);
243 if (!pctrl->functions)
244 return -ENOMEM;
245
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;
250
251 while (desc_function->name) {
252 berlin_pinctrl_add_function(pctrl, desc_function->name);
253 desc_function++;
254 }
255 }
256
257 pctrl->functions = krealloc(pctrl->functions,
258 pctrl->nfunctions * sizeof(*pctrl->functions),
259 GFP_KERNEL);
260
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;
265
266 while (desc_function->name) {
267 struct berlin_pinctrl_function
268 *function = pctrl->functions;
269 const char **groups;
270 bool found = false;
271
272 while (function->name) {
273 if (!strcmp(desc_function->name, function->name)) {
274 found = true;
275 break;
276 }
277 function++;
278 }
279
280 if (!found)
281 return -EINVAL;
282
283 if (!function->groups) {
284 function->groups =
285 devm_kzalloc(&pdev->dev,
286 function->ngroups * sizeof(char *),
287 GFP_KERNEL);
288
289 if (!function->groups)
290 return -ENOMEM;
291 }
292
293 groups = function->groups;
294 while (*groups)
295 groups++;
296
297 *groups = desc_group->name;
298
299 desc_function++;
300 }
301 }
302
303 return 0;
304}
305
306static struct pinctrl_desc berlin_pctrl_desc = {
307 .name = "berlin-pinctrl",
308 .pctlops = &berlin_pinctrl_ops,
309 .pmxops = &berlin_pinmux_ops,
310 .owner = THIS_MODULE,
311};
312
313int berlin_pinctrl_probe(struct platform_device *pdev,
314 const struct berlin_pinctrl_desc *desc)
315{
316 struct device *dev = &pdev->dev;
317 struct berlin_pinctrl *pctrl;
318 struct regmap *regmap;
319 int ret;
320
321 regmap = dev_get_regmap(&pdev->dev, NULL);
322 if (!regmap)
323 return PTR_ERR(regmap);
324
325 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
326 if (!pctrl)
327 return -ENOMEM;
328
329 platform_set_drvdata(pdev, pctrl);
330
331 pctrl->regmap = regmap;
332 pctrl->dev = &pdev->dev;
333 pctrl->desc = desc;
334
335 ret = berlin_pinctrl_build_state(pdev);
336 if (ret) {
337 dev_err(dev, "cannot build driver state: %d\n", ret);
338 return ret;
339 }
340
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");
344 return -EINVAL;
345 }
346
347 return 0;
348}
diff --git a/drivers/pinctrl/berlin/berlin.h b/drivers/pinctrl/berlin/berlin.h
new file mode 100644
index 000000000000..e1aa84145194
--- /dev/null
+++ b/drivers/pinctrl/berlin/berlin.h
@@ -0,0 +1,61 @@
1/*
2 * Marvell Berlin SoC pinctrl driver.
3 *
4 * Copyright (C) 2014 Marvell Technology Group Ltd.
5 *
6 * Antoine Ténart <antoine.tenart@free-electrons.com>
7 *
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.
11 */
12
13#ifndef __PINCTRL_BERLIN_H
14#define __PINCTRL_BERLIN_H
15
16struct berlin_desc_function {
17 const char *name;
18 u8 muxval;
19};
20
21struct berlin_desc_group {
22 const char *name;
23 u8 offset;
24 u8 bit_width;
25 u8 lsb;
26 struct berlin_desc_function *functions;
27};
28
29struct berlin_pinctrl_desc {
30 const struct berlin_desc_group *groups;
31 unsigned ngroups;
32};
33
34struct berlin_pinctrl_function {
35 const char *name;
36 const char **groups;
37 unsigned ngroups;
38};
39
40#define BERLIN_PINCTRL_GROUP(_name, _offset, _width, _lsb, ...) \
41 { \
42 .name = _name, \
43 .offset = _offset, \
44 .bit_width = _width, \
45 .lsb = _lsb, \
46 .functions = (struct berlin_desc_function[]){ \
47 __VA_ARGS__, { } }, \
48 }
49
50#define BERLIN_PINCTRL_FUNCTION(_muxval, _name) \
51 { \
52 .name = _name, \
53 .muxval = _muxval, \
54 }
55
56#define BERLIN_PINCTRL_FUNCTION_UNKNOWN {}
57
58int berlin_pinctrl_probe(struct platform_device *pdev,
59 const struct berlin_pinctrl_desc *desc);
60
61#endif /* __PINCTRL_BERLIN_H */