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;
82 /* Check for pin config node which has no 'reg' property */
83 if (of_property_read_u32(np, "reg", ®))
86 ret = of_property_read_u32(np, "fsl,drive-strength", &val);
88 config = val | MA_PRESENT;
89 ret = of_property_read_u32(np, "fsl,voltage", &val);
91 config |= val << VOL_SHIFT | VOL_PRESENT;
92 ret = of_property_read_u32(np, "fsl,pull-up", &val);
94 config |= val << PULL_SHIFT | PULL_PRESENT;
96 /* Check for group node which has both mux and config settings */
97 if (!purecfg && config)
100 new_map = kzalloc(sizeof(*new_map) * new_num, GFP_KERNEL);
105 new_map[i].type = PIN_MAP_TYPE_MUX_GROUP;
106 new_map[i].data.mux.function = np->name;
108 /* Compose group name */
109 group = kzalloc(length, GFP_KERNEL);
112 snprintf(group, length, "%s.%d", np->name, reg);
113 new_map[i].data.mux.group = group;
118 pconfig = kmemdup(&config, sizeof(config), GFP_KERNEL);
124 new_map[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
125 new_map[i].data.configs.group_or_pin = purecfg ? np->name :
127 new_map[i].data.configs.configs = pconfig;
128 new_map[i].data.configs.num_configs = 1;
141 static void mxs_dt_free_map(struct pinctrl_dev *pctldev,
142 struct pinctrl_map *map, unsigned num_maps)
146 for (i = 0; i < num_maps; i++) {
147 if (map[i].type == PIN_MAP_TYPE_MUX_GROUP)
148 kfree(map[i].data.mux.group);
149 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
150 kfree(map[i].data.configs.configs);
156 static struct pinctrl_ops mxs_pinctrl_ops = {
157 .get_groups_count = mxs_get_groups_count,
158 .get_group_name = mxs_get_group_name,
159 .get_group_pins = mxs_get_group_pins,
160 .pin_dbg_show = mxs_pin_dbg_show,
161 .dt_node_to_map = mxs_dt_node_to_map,
162 .dt_free_map = mxs_dt_free_map,
165 static int mxs_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
167 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
169 return d->soc->nfunctions;
172 static const char *mxs_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
175 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
177 return d->soc->functions[function].name;
180 static int mxs_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
182 const char * const **groups,
183 unsigned * const num_groups)
185 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
187 *groups = d->soc->functions[group].groups;
188 *num_groups = d->soc->functions[group].ngroups;
193 static int mxs_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned selector,
196 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
197 struct mxs_group *g = &d->soc->groups[group];
203 for (i = 0; i < g->npins; i++) {
204 bank = PINID_TO_BANK(g->pins[i]);
205 pin = PINID_TO_PIN(g->pins[i]);
206 reg = d->base + d->soc->regs->muxsel;
207 reg += bank * 0x20 + pin / 16 * 0x10;
208 shift = pin % 16 * 2;
210 writel(0x3 << shift, reg + CLR);
211 writel(g->muxsel[i] << shift, reg + SET);
217 static struct pinmux_ops mxs_pinmux_ops = {
218 .get_functions_count = mxs_pinctrl_get_funcs_count,
219 .get_function_name = mxs_pinctrl_get_func_name,
220 .get_function_groups = mxs_pinctrl_get_func_groups,
221 .enable = mxs_pinctrl_enable,
224 static int mxs_pinconf_get(struct pinctrl_dev *pctldev,
225 unsigned pin, unsigned long *config)
230 static int mxs_pinconf_set(struct pinctrl_dev *pctldev,
231 unsigned pin, unsigned long config)
236 static int mxs_pinconf_group_get(struct pinctrl_dev *pctldev,
237 unsigned group, unsigned long *config)
239 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
241 *config = d->soc->groups[group].config;
246 static int mxs_pinconf_group_set(struct pinctrl_dev *pctldev,
247 unsigned group, unsigned long config)
249 struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
250 struct mxs_group *g = &d->soc->groups[group];
252 u8 ma, vol, pull, bank, shift;
256 ma = CONFIG_TO_MA(config);
257 vol = CONFIG_TO_VOL(config);
258 pull = CONFIG_TO_PULL(config);
260 for (i = 0; i < g->npins; i++) {
261 bank = PINID_TO_BANK(g->pins[i]);
262 pin = PINID_TO_PIN(g->pins[i]);
265 reg = d->base + d->soc->regs->drive;
266 reg += bank * 0x40 + pin / 8 * 0x10;
269 if (config & MA_PRESENT) {
271 writel(0x3 << shift, reg + CLR);
272 writel(ma << shift, reg + SET);
276 if (config & VOL_PRESENT) {
277 shift = pin % 8 * 4 + 2;
279 writel(1 << shift, reg + SET);
281 writel(1 << shift, reg + CLR);
285 if (config & PULL_PRESENT) {
286 reg = d->base + d->soc->regs->pull;
290 writel(1 << shift, reg + SET);
292 writel(1 << shift, reg + CLR);
296 /* cache the config value for mxs_pinconf_group_get() */
302 static void mxs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
303 struct seq_file *s, unsigned pin)
308 static void mxs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
309 struct seq_file *s, unsigned group)
311 unsigned long config;
313 if (!mxs_pinconf_group_get(pctldev, group, &config))
314 seq_printf(s, "0x%lx", config);
317 struct pinconf_ops mxs_pinconf_ops = {
318 .pin_config_get = mxs_pinconf_get,
319 .pin_config_set = mxs_pinconf_set,
320 .pin_config_group_get = mxs_pinconf_group_get,
321 .pin_config_group_set = mxs_pinconf_group_set,
322 .pin_config_dbg_show = mxs_pinconf_dbg_show,
323 .pin_config_group_dbg_show = mxs_pinconf_group_dbg_show,
326 static struct pinctrl_desc mxs_pinctrl_desc = {
327 .pctlops = &mxs_pinctrl_ops,
328 .pmxops = &mxs_pinmux_ops,
329 .confops = &mxs_pinconf_ops,
330 .owner = THIS_MODULE,
333 static int __devinit mxs_pinctrl_parse_group(struct platform_device *pdev,
334 struct device_node *np, int idx,
335 const char **out_name)
337 struct mxs_pinctrl_data *d = platform_get_drvdata(pdev);
338 struct mxs_group *g = &d->soc->groups[idx];
339 struct property *prop;
340 const char *propname = "fsl,pinmux-ids";
342 int length = strlen(np->name) + SUFFIX_LEN;
346 group = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
349 if (of_property_read_u32(np, "reg", &val))
350 snprintf(group, length, "%s", np->name);
352 snprintf(group, length, "%s.%d", np->name, val);
355 prop = of_find_property(np, propname, &length);
358 g->npins = length / sizeof(u32);
360 g->pins = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->pins),
365 g->muxsel = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->muxsel),
370 of_property_read_u32_array(np, propname, g->pins, g->npins);
371 for (i = 0; i < g->npins; i++) {
372 g->muxsel[i] = MUXID_TO_MUXSEL(g->pins[i]);
373 g->pins[i] = MUXID_TO_PINID(g->pins[i]);
382 static int __devinit mxs_pinctrl_probe_dt(struct platform_device *pdev,
383 struct mxs_pinctrl_data *d)
385 struct mxs_pinctrl_soc_data *soc = d->soc;
386 struct device_node *np = pdev->dev.of_node;
387 struct device_node *child;
388 struct mxs_function *f;
389 const char *gpio_compat = "fsl,mxs-gpio";
390 const char *fn, *fnull = "";
391 int i = 0, idxf = 0, idxg = 0;
395 child = of_get_next_child(np, NULL);
397 dev_err(&pdev->dev, "no group is defined\n");
401 /* Count total functions and groups */
403 for_each_child_of_node(np, child) {
404 if (of_device_is_compatible(child, gpio_compat))
407 /* Skip pure pinconf node */
408 if (of_property_read_u32(child, "reg", &val))
410 if (strcmp(fn, child->name)) {
416 soc->functions = devm_kzalloc(&pdev->dev, soc->nfunctions *
417 sizeof(*soc->functions), GFP_KERNEL);
421 soc->groups = devm_kzalloc(&pdev->dev, soc->ngroups *
422 sizeof(*soc->groups), GFP_KERNEL);
426 /* Count groups for each function */
428 f = &soc->functions[idxf];
429 for_each_child_of_node(np, child) {
430 if (of_device_is_compatible(child, gpio_compat))
432 if (of_property_read_u32(child, "reg", &val))
434 if (strcmp(fn, child->name)) {
435 f = &soc->functions[idxf++];
436 f->name = fn = child->name;
441 /* Get groups for each function */
444 for_each_child_of_node(np, child) {
445 if (of_device_is_compatible(child, gpio_compat))
447 if (of_property_read_u32(child, "reg", &val)) {
448 ret = mxs_pinctrl_parse_group(pdev, child,
455 if (strcmp(fn, child->name)) {
456 f = &soc->functions[idxf++];
457 f->groups = devm_kzalloc(&pdev->dev, f->ngroups *
465 ret = mxs_pinctrl_parse_group(pdev, child, idxg++,
474 int __devinit mxs_pinctrl_probe(struct platform_device *pdev,
475 struct mxs_pinctrl_soc_data *soc)
477 struct device_node *np = pdev->dev.of_node;
478 struct mxs_pinctrl_data *d;
481 d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
488 d->base = of_iomap(np, 0);
490 return -EADDRNOTAVAIL;
492 mxs_pinctrl_desc.pins = d->soc->pins;
493 mxs_pinctrl_desc.npins = d->soc->npins;
494 mxs_pinctrl_desc.name = dev_name(&pdev->dev);
496 platform_set_drvdata(pdev, d);
498 ret = mxs_pinctrl_probe_dt(pdev, d);
500 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
504 d->pctl = pinctrl_register(&mxs_pinctrl_desc, &pdev->dev, d);
506 dev_err(&pdev->dev, "Couldn't register MXS pinctrl driver\n");
517 EXPORT_SYMBOL_GPL(mxs_pinctrl_probe);
519 int __devexit mxs_pinctrl_remove(struct platform_device *pdev)
521 struct mxs_pinctrl_data *d = platform_get_drvdata(pdev);
523 pinctrl_unregister(d->pctl);
528 EXPORT_SYMBOL_GPL(mxs_pinctrl_remove);