2 * Copyright 2012 Freescale Semiconductor, Inc.
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:
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
12 #include <linux/err.h>
13 #include <linux/init.h>
15 #include <linux/module.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>
25 #include "pinctrl-mxs.h"
29 struct mxs_pinctrl_data {
31 struct pinctrl_dev *pctl;
33 struct mxs_pinctrl_soc_data *soc;
36 static int mxs_get_groups_count(struct pinctrl_dev *pctldev)
38 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
40 return d->soc->ngroups;
43 static const char *mxs_get_group_name(struct pinctrl_dev *pctldev,
46 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
48 return d->soc->groups[group].name;
51 static int mxs_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
52 const unsigned **pins, unsigned *num_pins)
54 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
56 *pins = d->soc->groups[group].pins;
57 *num_pins = d->soc->groups[group].npins;
62 static void mxs_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
65 seq_printf(s, " %s", dev_name(pctldev->dev));
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)
72 struct pinctrl_map *new_map;
75 unsigned long config = 0;
76 unsigned long *pconfig;
77 int length = strlen(np->name) + SUFFIX_LEN;
81 ret = of_property_read_u32(np, "fsl,drive-strength", &val);
83 config = val | MA_PRESENT;
84 ret = of_property_read_u32(np, "fsl,voltage", &val);
86 config |= val << VOL_SHIFT | VOL_PRESENT;
87 ret = of_property_read_u32(np, "fsl,pull-up", &val);
89 config |= val << PULL_SHIFT | PULL_PRESENT;
91 new_num = config ? 2 : 1;
92 new_map = kzalloc(sizeof(*new_map) * new_num, GFP_KERNEL);
96 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
97 new_map[0].data.mux.function = np->name;
99 /* Compose group name */
100 group = kzalloc(length, GFP_KERNEL);
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;
108 pconfig = kmemdup(&config, sizeof(config), GFP_KERNEL);
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;
130 static void mxs_dt_free_map(struct pinctrl_dev *pctldev,
131 struct pinctrl_map *map, unsigned num_maps)
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);
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,
154 static int mxs_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
156 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
158 return d->soc->nfunctions;
161 static const char *mxs_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
164 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
166 return d->soc->functions[function].name;
169 static int mxs_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
171 const char * const **groups,
172 unsigned * const num_groups)
174 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
176 *groups = d->soc->functions[group].groups;
177 *num_groups = d->soc->functions[group].ngroups;
182 static int mxs_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned selector,
185 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
186 struct mxs_group *g = &d->soc->groups[group];
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;
199 writel(0x3 << shift, reg + CLR);
200 writel(g->muxsel[i] << shift, reg + SET);
206 static void mxs_pinctrl_disable(struct pinctrl_dev *pctldev,
207 unsigned function, unsigned group)
209 /* Nothing to do here */
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,
220 static int mxs_pinconf_get(struct pinctrl_dev *pctldev,
221 unsigned pin, unsigned long *config)
226 static int mxs_pinconf_set(struct pinctrl_dev *pctldev,
227 unsigned pin, unsigned long config)
232 static int mxs_pinconf_group_get(struct pinctrl_dev *pctldev,
233 unsigned group, unsigned long *config)
235 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
237 *config = d->soc->groups[group].config;
242 static int mxs_pinconf_group_set(struct pinctrl_dev *pctldev,
243 unsigned group, unsigned long config)
245 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
246 struct mxs_group *g = &d->soc->groups[group];
248 u8 ma, vol, pull, bank, shift;
252 ma = CONFIG_TO_MA(config);
253 vol = CONFIG_TO_VOL(config);
254 pull = CONFIG_TO_PULL(config);
256 for (i = 0; i < g->npins; i++) {
257 bank = PINID_TO_BANK(g->pins[i]);
258 pin = PINID_TO_PIN(g->pins[i]);
261 reg = d->base + d->soc->regs->drive;
262 reg += bank * 0x40 + pin / 8 * 0x10;
265 if (config & MA_PRESENT) {
267 writel(0x3 << shift, reg + CLR);
268 writel(ma << shift, reg + SET);
272 if (config & VOL_PRESENT) {
273 shift = pin % 8 * 4 + 2;
275 writel(1 << shift, reg + SET);
277 writel(1 << shift, reg + CLR);
281 if (config & PULL_PRESENT) {
282 reg = d->base + d->soc->regs->pull;
286 writel(1 << shift, reg + SET);
288 writel(1 << shift, reg + CLR);
292 /* cache the config value for mxs_pinconf_group_get() */
298 static void mxs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
299 struct seq_file *s, unsigned pin)
304 static void mxs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
305 struct seq_file *s, unsigned group)
307 unsigned long config;
309 if (!mxs_pinconf_group_get(pctldev, group, &config))
310 seq_printf(s, "0x%lx", config);
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,
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,
329 static int __devinit mxs_pinctrl_parse_group(struct platform_device *pdev,
330 struct device_node *np, int idx,
331 const char **out_name)
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";
338 int length = strlen(np->name) + SUFFIX_LEN;
342 group = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
345 of_property_read_u32(np, "reg", &val);
346 snprintf(group, length, "%s.%d", np->name, val);
349 prop = of_find_property(np, propname, &length);
352 g->npins = length / sizeof(u32);
354 g->pins = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->pins),
359 g->muxsel = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->muxsel),
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]);
375 static int __devinit mxs_pinctrl_probe_dt(struct platform_device *pdev,
376 struct mxs_pinctrl_data *d)
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;
387 child = of_get_next_child(np, NULL);
389 dev_err(&pdev->dev, "no group is defined\n");
393 /* Count total functions and groups */
395 for_each_child_of_node(np, child) {
396 /* Skip pure pinconf node */
397 if (of_property_read_u32(child, "reg", &val))
399 if (strcmp(fn, child->name)) {
406 soc->functions = devm_kzalloc(&pdev->dev, soc->nfunctions *
407 sizeof(*soc->functions), GFP_KERNEL);
411 soc->groups = devm_kzalloc(&pdev->dev, soc->ngroups *
412 sizeof(*soc->groups), GFP_KERNEL);
416 /* Count groups for each function */
418 f = &soc->functions[idxf];
419 for_each_child_of_node(np, child) {
420 if (of_property_read_u32(child, "reg", &val))
422 if (strcmp(fn, child->name)) {
423 f = &soc->functions[idxf++];
424 f->name = fn = child->name;
429 /* Get groups for each function */
432 for_each_child_of_node(np, child) {
433 if (of_property_read_u32(child, "reg", &val))
435 if (strcmp(fn, child->name)) {
436 f = &soc->functions[idxf++];
437 f->groups = devm_kzalloc(&pdev->dev, f->ngroups *
445 ret = mxs_pinctrl_parse_group(pdev, child, idxg++,
454 int __devinit mxs_pinctrl_probe(struct platform_device *pdev,
455 struct mxs_pinctrl_soc_data *soc)
457 struct device_node *np = pdev->dev.of_node;
458 struct mxs_pinctrl_data *d;
461 d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
468 d->base = of_iomap(np, 0);
470 return -EADDRNOTAVAIL;
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);
476 platform_set_drvdata(pdev, d);
478 ret = mxs_pinctrl_probe_dt(pdev, d);
480 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
484 d->pctl = pinctrl_register(&mxs_pinctrl_desc, &pdev->dev, d);
486 dev_err(&pdev->dev, "Couldn't register MXS pinctrl driver\n");
497 EXPORT_SYMBOL_GPL(mxs_pinctrl_probe);
499 int __devexit mxs_pinctrl_remove(struct platform_device *pdev)
501 struct mxs_pinctrl_data *d = platform_get_drvdata(pdev);
503 pinctrl_unregister(d->pctl);
508 EXPORT_SYMBOL_GPL(mxs_pinctrl_remove);