pinctrl: add pinctrl-mxs support
[pandora-kernel.git] / drivers / pinctrl / pinctrl-mxs.c
1 /*
2  * Copyright 2012 Freescale Semiconductor, Inc.
3  *
4  * The code contained herein is licensed under the GNU General Public
5  * License. You may obtain a copy of the GNU General Public License
6  * Version 2 or later at the following locations:
7  *
8  * http://www.opensource.org/licenses/gpl-license.html
9  * http://www.gnu.org/copyleft/gpl.html
10  */
11
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/pinctrl/machine.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/pinctrl/pinctrl.h>
21 #include <linux/pinctrl/pinmux.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include "core.h"
25 #include "pinctrl-mxs.h"
26
27 #define SUFFIX_LEN      4
28
29 struct mxs_pinctrl_data {
30         struct device *dev;
31         struct pinctrl_dev *pctl;
32         void __iomem *base;
33         struct mxs_pinctrl_soc_data *soc;
34 };
35
36 static int mxs_get_groups_count(struct pinctrl_dev *pctldev)
37 {
38         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
39
40         return d->soc->ngroups;
41 }
42
43 static const char *mxs_get_group_name(struct pinctrl_dev *pctldev,
44                                       unsigned group)
45 {
46         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
47
48         return d->soc->groups[group].name;
49 }
50
51 static int mxs_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
52                               const unsigned **pins, unsigned *num_pins)
53 {
54         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
55
56         *pins = d->soc->groups[group].pins;
57         *num_pins = d->soc->groups[group].npins;
58
59         return 0;
60 }
61
62 static void mxs_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
63                              unsigned offset)
64 {
65         seq_printf(s, " %s", dev_name(pctldev->dev));
66 }
67
68 static int mxs_dt_node_to_map(struct pinctrl_dev *pctldev,
69                               struct device_node *np,
70                               struct pinctrl_map **map, unsigned *num_maps)
71 {
72         struct pinctrl_map *new_map;
73         char *group;
74         unsigned new_num;
75         unsigned long config = 0;
76         unsigned long *pconfig;
77         int length = strlen(np->name) + SUFFIX_LEN;
78         u32 val;
79         int ret;
80
81         ret = of_property_read_u32(np, "fsl,drive-strength", &val);
82         if (!ret)
83                 config = val | MA_PRESENT;
84         ret = of_property_read_u32(np, "fsl,voltage", &val);
85         if (!ret)
86                 config |= val << VOL_SHIFT | VOL_PRESENT;
87         ret = of_property_read_u32(np, "fsl,pull-up", &val);
88         if (!ret)
89                 config |= val << PULL_SHIFT | PULL_PRESENT;
90
91         new_num = config ? 2 : 1;
92         new_map = kzalloc(sizeof(*new_map) * new_num, GFP_KERNEL);
93         if (!new_map)
94                 return -ENOMEM;
95
96         new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
97         new_map[0].data.mux.function = np->name;
98
99         /* Compose group name */
100         group = kzalloc(length, GFP_KERNEL);
101         if (!group)
102                 return -ENOMEM;
103         of_property_read_u32(np, "reg", &val);
104         snprintf(group, length, "%s.%d", np->name, val);
105         new_map[0].data.mux.group = group;
106
107         if (config) {
108                 pconfig = kmemdup(&config, sizeof(config), GFP_KERNEL);
109                 if (!pconfig) {
110                         ret = -ENOMEM;
111                         goto free;
112                 }
113
114                 new_map[1].type = PIN_MAP_TYPE_CONFIGS_GROUP;
115                 new_map[1].data.configs.group_or_pin = group;
116                 new_map[1].data.configs.configs = pconfig;
117                 new_map[1].data.configs.num_configs = 1;
118         }
119
120         *map = new_map;
121         *num_maps = new_num;
122
123         return 0;
124
125 free:
126         kfree(new_map);
127         return ret;
128 }
129
130 static void mxs_dt_free_map(struct pinctrl_dev *pctldev,
131                             struct pinctrl_map *map, unsigned num_maps)
132 {
133         int i;
134
135         for (i = 0; i < num_maps; i++) {
136                 if (map[i].type == PIN_MAP_TYPE_MUX_GROUP)
137                         kfree(map[i].data.mux.group);
138                 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
139                         kfree(map[i].data.configs.configs);
140         }
141
142         kfree(map);
143 }
144
145 static struct pinctrl_ops mxs_pinctrl_ops = {
146         .get_groups_count = mxs_get_groups_count,
147         .get_group_name = mxs_get_group_name,
148         .get_group_pins = mxs_get_group_pins,
149         .pin_dbg_show = mxs_pin_dbg_show,
150         .dt_node_to_map = mxs_dt_node_to_map,
151         .dt_free_map = mxs_dt_free_map,
152 };
153
154 static int mxs_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
155 {
156         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
157
158         return d->soc->nfunctions;
159 }
160
161 static const char *mxs_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
162                                              unsigned function)
163 {
164         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
165
166         return d->soc->functions[function].name;
167 }
168
169 static int mxs_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
170                                        unsigned group,
171                                        const char * const **groups,
172                                        unsigned * const num_groups)
173 {
174         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
175
176         *groups = d->soc->functions[group].groups;
177         *num_groups = d->soc->functions[group].ngroups;
178
179         return 0;
180 }
181
182 static int mxs_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned selector,
183                               unsigned group)
184 {
185         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
186         struct mxs_group *g = &d->soc->groups[group];
187         void __iomem *reg;
188         u8 bank, shift;
189         u16 pin;
190         int i;
191
192         for (i = 0; i < g->npins; i++) {
193                 bank = PINID_TO_BANK(g->pins[i]);
194                 pin = PINID_TO_PIN(g->pins[i]);
195                 reg = d->base + d->soc->regs->muxsel;
196                 reg += bank * 0x20 + pin / 16 * 0x10;
197                 shift = pin % 16 * 2;
198
199                 writel(0x3 << shift, reg + CLR);
200                 writel(g->muxsel[i] << shift, reg + SET);
201         }
202
203         return 0;
204 }
205
206 static void mxs_pinctrl_disable(struct pinctrl_dev *pctldev,
207                                 unsigned function, unsigned group)
208 {
209         /* Nothing to do here */
210 }
211
212 static struct pinmux_ops mxs_pinmux_ops = {
213         .get_functions_count = mxs_pinctrl_get_funcs_count,
214         .get_function_name = mxs_pinctrl_get_func_name,
215         .get_function_groups = mxs_pinctrl_get_func_groups,
216         .enable = mxs_pinctrl_enable,
217         .disable = mxs_pinctrl_disable,
218 };
219
220 static int mxs_pinconf_get(struct pinctrl_dev *pctldev,
221                            unsigned pin, unsigned long *config)
222 {
223         return -ENOTSUPP;
224 }
225
226 static int mxs_pinconf_set(struct pinctrl_dev *pctldev,
227                            unsigned pin, unsigned long config)
228 {
229         return -ENOTSUPP;
230 }
231
232 static int mxs_pinconf_group_get(struct pinctrl_dev *pctldev,
233                                  unsigned group, unsigned long *config)
234 {
235         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
236
237         *config = d->soc->groups[group].config;
238
239         return 0;
240 }
241
242 static int mxs_pinconf_group_set(struct pinctrl_dev *pctldev,
243                                  unsigned group, unsigned long config)
244 {
245         struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
246         struct mxs_group *g = &d->soc->groups[group];
247         void __iomem *reg;
248         u8 ma, vol, pull, bank, shift;
249         u16 pin;
250         int i;
251
252         ma = CONFIG_TO_MA(config);
253         vol = CONFIG_TO_VOL(config);
254         pull = CONFIG_TO_PULL(config);
255
256         for (i = 0; i < g->npins; i++) {
257                 bank = PINID_TO_BANK(g->pins[i]);
258                 pin = PINID_TO_PIN(g->pins[i]);
259
260                 /* drive */
261                 reg = d->base + d->soc->regs->drive;
262                 reg += bank * 0x40 + pin / 8 * 0x10;
263
264                 /* mA */
265                 if (config & MA_PRESENT) {
266                         shift = pin % 8 * 4;
267                         writel(0x3 << shift, reg + CLR);
268                         writel(ma << shift, reg + SET);
269                 }
270
271                 /* vol */
272                 if (config & VOL_PRESENT) {
273                         shift = pin % 8 * 4 + 2;
274                         if (vol)
275                                 writel(1 << shift, reg + SET);
276                         else
277                                 writel(1 << shift, reg + CLR);
278                 }
279
280                 /* pull */
281                 if (config & PULL_PRESENT) {
282                         reg = d->base + d->soc->regs->pull;
283                         reg += bank * 0x10;
284                         shift = pin;
285                         if (pull)
286                                 writel(1 << shift, reg + SET);
287                         else
288                                 writel(1 << shift, reg + CLR);
289                 }
290         }
291
292         /* cache the config value for mxs_pinconf_group_get() */
293         g->config = config;
294
295         return 0;
296 }
297
298 static void mxs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
299                                  struct seq_file *s, unsigned pin)
300 {
301         /* Not support */
302 }
303
304 static void mxs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
305                                        struct seq_file *s, unsigned group)
306 {
307         unsigned long config;
308
309         if (!mxs_pinconf_group_get(pctldev, group, &config))
310                 seq_printf(s, "0x%lx", config);
311 }
312
313 struct pinconf_ops mxs_pinconf_ops = {
314         .pin_config_get = mxs_pinconf_get,
315         .pin_config_set = mxs_pinconf_set,
316         .pin_config_group_get = mxs_pinconf_group_get,
317         .pin_config_group_set = mxs_pinconf_group_set,
318         .pin_config_dbg_show = mxs_pinconf_dbg_show,
319         .pin_config_group_dbg_show = mxs_pinconf_group_dbg_show,
320 };
321
322 static struct pinctrl_desc mxs_pinctrl_desc = {
323         .pctlops = &mxs_pinctrl_ops,
324         .pmxops = &mxs_pinmux_ops,
325         .confops = &mxs_pinconf_ops,
326         .owner = THIS_MODULE,
327 };
328
329 static int __devinit mxs_pinctrl_parse_group(struct platform_device *pdev,
330                                              struct device_node *np, int idx,
331                                              const char **out_name)
332 {
333         struct mxs_pinctrl_data *d = platform_get_drvdata(pdev);
334         struct mxs_group *g = &d->soc->groups[idx];
335         struct property *prop;
336         const char *propname = "fsl,pinmux-ids";
337         char *group;
338         int length = strlen(np->name) + SUFFIX_LEN;
339         int i;
340         u32 val;
341
342         group = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
343         if (!group)
344                 return -ENOMEM;
345         of_property_read_u32(np, "reg", &val);
346         snprintf(group, length, "%s.%d", np->name, val);
347         g->name = group;
348
349         prop = of_find_property(np, propname, &length);
350         if (!prop)
351                 return -EINVAL;
352         g->npins = length / sizeof(u32);
353
354         g->pins = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->pins),
355                                GFP_KERNEL);
356         if (!g->pins)
357                 return -ENOMEM;
358
359         g->muxsel = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->muxsel),
360                                  GFP_KERNEL);
361         if (!g->muxsel)
362                 return -ENOMEM;
363
364         of_property_read_u32_array(np, propname, g->pins, g->npins);
365         for (i = 0; i < g->npins; i++) {
366                 g->muxsel[i] = MUXID_TO_MUXSEL(g->pins[i]);
367                 g->pins[i] = MUXID_TO_PINID(g->pins[i]);
368         }
369
370         *out_name = g->name;
371
372         return 0;
373 }
374
375 static int __devinit mxs_pinctrl_probe_dt(struct platform_device *pdev,
376                                           struct mxs_pinctrl_data *d)
377 {
378         struct mxs_pinctrl_soc_data *soc = d->soc;
379         struct device_node *np = pdev->dev.of_node;
380         struct device_node *child;
381         struct mxs_function *f;
382         const char *fn, *fnull = "";
383         int i = 0, idxf = 0, idxg = 0;
384         int ret;
385         u32 val;
386
387         child = of_get_next_child(np, NULL);
388         if (!child) {
389                 dev_err(&pdev->dev, "no group is defined\n");
390                 return -ENOENT;
391         }
392
393         /* Count total functions and groups */
394         fn = fnull;
395         for_each_child_of_node(np, child) {
396                 /* Skip pure pinconf node */
397                 if (of_property_read_u32(child, "reg", &val))
398                         continue;
399                 if (strcmp(fn, child->name)) {
400                         fn = child->name;
401                         soc->nfunctions++;
402                 }
403                 soc->ngroups++;
404         }
405
406         soc->functions = devm_kzalloc(&pdev->dev, soc->nfunctions *
407                                       sizeof(*soc->functions), GFP_KERNEL);
408         if (!soc->functions)
409                 return -ENOMEM;
410
411         soc->groups = devm_kzalloc(&pdev->dev, soc->ngroups *
412                                    sizeof(*soc->groups), GFP_KERNEL);
413         if (!soc->groups)
414                 return -ENOMEM;
415
416         /* Count groups for each function */
417         fn = fnull;
418         f = &soc->functions[idxf];
419         for_each_child_of_node(np, child) {
420                 if (of_property_read_u32(child, "reg", &val))
421                         continue;
422                 if (strcmp(fn, child->name)) {
423                         f = &soc->functions[idxf++];
424                         f->name = fn = child->name;
425                 }
426                 f->ngroups++;
427         };
428
429         /* Get groups for each function */
430         idxf = 0;
431         fn = fnull;
432         for_each_child_of_node(np, child) {
433                 if (of_property_read_u32(child, "reg", &val))
434                         continue;
435                 if (strcmp(fn, child->name)) {
436                         f = &soc->functions[idxf++];
437                         f->groups = devm_kzalloc(&pdev->dev, f->ngroups *
438                                                  sizeof(*f->groups),
439                                                  GFP_KERNEL);
440                         if (!f->groups)
441                                 return -ENOMEM;
442                         fn = child->name;
443                         i = 0;
444                 }
445                 ret = mxs_pinctrl_parse_group(pdev, child, idxg++,
446                                               &f->groups[i++]);
447                 if (ret)
448                         return ret;
449         }
450
451         return 0;
452 }
453
454 int __devinit mxs_pinctrl_probe(struct platform_device *pdev,
455                                 struct mxs_pinctrl_soc_data *soc)
456 {
457         struct device_node *np = pdev->dev.of_node;
458         struct mxs_pinctrl_data *d;
459         int ret;
460
461         d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
462         if (!d)
463                 return -ENOMEM;
464
465         d->dev = &pdev->dev;
466         d->soc = soc;
467
468         d->base = of_iomap(np, 0);
469         if (!d->base)
470                 return -EADDRNOTAVAIL;
471
472         mxs_pinctrl_desc.pins = d->soc->pins;
473         mxs_pinctrl_desc.npins = d->soc->npins;
474         mxs_pinctrl_desc.name = dev_name(&pdev->dev);
475
476         platform_set_drvdata(pdev, d);
477
478         ret = mxs_pinctrl_probe_dt(pdev, d);
479         if (ret) {
480                 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
481                 goto err;
482         }
483
484         d->pctl = pinctrl_register(&mxs_pinctrl_desc, &pdev->dev, d);
485         if (!d->pctl) {
486                 dev_err(&pdev->dev, "Couldn't register MXS pinctrl driver\n");
487                 ret = -EINVAL;
488                 goto err;
489         }
490
491         return 0;
492
493 err:
494         iounmap(d->base);
495         return ret;
496 }
497 EXPORT_SYMBOL_GPL(mxs_pinctrl_probe);
498
499 int __devexit mxs_pinctrl_remove(struct platform_device *pdev)
500 {
501         struct mxs_pinctrl_data *d = platform_get_drvdata(pdev);
502
503         pinctrl_unregister(d->pctl);
504         iounmap(d->base);
505
506         return 0;
507 }
508 EXPORT_SYMBOL_GPL(mxs_pinctrl_remove);