2 * linux/drivers/pinctrl/pinctrl-pxa3xx.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * publishhed by the Free Software Foundation.
8 * Copyright (C) 2011, Marvell Technology Group Ltd.
10 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
14 #include <linux/module.h>
15 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include "pinctrl-pxa3xx.h"
21 static struct pinctrl_gpio_range pxa3xx_pinctrl_gpio_range = {
22 .name = "PXA3xx GPIO",
28 static int pxa3xx_list_groups(struct pinctrl_dev *pctrldev, unsigned selector)
30 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
31 if (selector >= info->num_grps)
36 static const char *pxa3xx_get_group_name(struct pinctrl_dev *pctrldev,
39 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
40 if (selector >= info->num_grps)
42 return info->grps[selector].name;
45 static int pxa3xx_get_group_pins(struct pinctrl_dev *pctrldev,
47 const unsigned **pins,
50 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
51 if (selector >= info->num_grps)
53 *pins = info->grps[selector].pins;
54 *num_pins = info->grps[selector].npins;
58 static struct pinctrl_ops pxa3xx_pctrl_ops = {
59 .list_groups = pxa3xx_list_groups,
60 .get_group_name = pxa3xx_get_group_name,
61 .get_group_pins = pxa3xx_get_group_pins,
64 static int pxa3xx_pmx_list_func(struct pinctrl_dev *pctrldev, unsigned func)
66 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
67 if (func >= info->num_funcs)
72 static const char *pxa3xx_pmx_get_func_name(struct pinctrl_dev *pctrldev,
75 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
76 return info->funcs[func].name;
79 static int pxa3xx_pmx_get_groups(struct pinctrl_dev *pctrldev, unsigned func,
80 const char * const **groups,
81 unsigned * const num_groups)
83 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
84 *groups = info->funcs[func].groups;
85 *num_groups = info->funcs[func].num_groups;
89 /* Return function number. If failure, return negative value. */
90 static int match_mux(struct pxa3xx_mfp_pin *mfp, unsigned mux)
93 for (i = 0; i < PXA3xx_MAX_MUX; i++) {
94 if (mfp->func[i] == mux)
97 if (i >= PXA3xx_MAX_MUX)
102 /* check whether current pin configuration is valid. Negative for failure */
103 static int match_group_mux(struct pxa3xx_pin_group *grp,
104 struct pxa3xx_pinmux_info *info,
108 for (i = 0; i < grp->npins; i++) {
110 ret = match_mux(&info->mfp[pin], mux);
112 dev_err(info->dev, "Can't find mux %d on pin%d\n",
120 static int pxa3xx_pmx_enable(struct pinctrl_dev *pctrldev, unsigned func,
123 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
124 struct pxa3xx_pin_group *pin_grp = &info->grps[group];
126 int i, mfpr, pin, pin_func;
128 if (!pin_grp->npins ||
129 (match_group_mux(pin_grp, info, pin_grp->mux) < 0)) {
130 dev_err(info->dev, "Failed to set the pin group: %d\n", group);
133 for (i = 0; i < pin_grp->npins; i++) {
134 pin = pin_grp->pins[i];
135 pin_func = match_mux(&info->mfp[pin], pin_grp->mux);
136 mfpr = info->mfp[pin].mfpr;
137 data = readl_relaxed(info->virt_base + mfpr);
138 data &= ~MFPR_FUNC_MASK;
140 writel_relaxed(data, info->virt_base + mfpr);
145 static void pxa3xx_pmx_disable(struct pinctrl_dev *pctrldev, unsigned func,
150 static int pxa3xx_pmx_request_gpio(struct pinctrl_dev *pctrldev,
151 struct pinctrl_gpio_range *range,
154 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
158 pin_func = match_mux(&info->mfp[pin], PXA3xx_MUX_GPIO);
160 dev_err(info->dev, "No GPIO function on pin%d (%s)\n",
161 pin, info->pads[pin].name);
164 mfpr = info->mfp[pin].mfpr;
165 /* write gpio function into mfpr register */
166 data = readl_relaxed(info->virt_base + mfpr) & ~MFPR_FUNC_MASK;
168 writel_relaxed(data, info->virt_base + mfpr);
172 static struct pinmux_ops pxa3xx_pmx_ops = {
173 .list_functions = pxa3xx_pmx_list_func,
174 .get_function_name = pxa3xx_pmx_get_func_name,
175 .get_function_groups = pxa3xx_pmx_get_groups,
176 .enable = pxa3xx_pmx_enable,
177 .disable = pxa3xx_pmx_disable,
178 .gpio_request_enable = pxa3xx_pmx_request_gpio,
181 int pxa3xx_pinctrl_register(struct platform_device *pdev,
182 struct pxa3xx_pinmux_info *info)
184 struct pinctrl_desc *desc;
185 struct resource *res;
188 if (!info || !info->cputype)
191 desc->pins = info->pads;
192 desc->npins = info->num_pads;
193 desc->pctlops = &pxa3xx_pctrl_ops;
194 desc->pmxops = &pxa3xx_pmx_ops;
195 info->dev = &pdev->dev;
196 pxa3xx_pinctrl_gpio_range.npins = info->num_gpio;
198 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
201 info->phy_base = res->start;
202 info->phy_size = resource_size(res);
203 info->virt_base = ioremap(info->phy_base, info->phy_size);
204 if (!info->virt_base)
206 info->pctrl = pinctrl_register(desc, &pdev->dev, info);
208 dev_err(&pdev->dev, "failed to register PXA pinmux driver\n");
212 pinctrl_add_gpio_range(info->pctrl, &pxa3xx_pinctrl_gpio_range);
213 platform_set_drvdata(pdev, info);
216 iounmap(info->virt_base);
220 int pxa3xx_pinctrl_unregister(struct platform_device *pdev)
222 struct pxa3xx_pinmux_info *info = platform_get_drvdata(pdev);
224 pinctrl_unregister(info->pctrl);
225 iounmap(info->virt_base);
226 platform_set_drvdata(pdev, NULL);
230 static int __init pxa3xx_pinctrl_init(void)
232 pr_info("pxa3xx-pinctrl: PXA3xx pinctrl driver initializing\n");
235 core_initcall_sync(pxa3xx_pinctrl_init);
237 static void __exit pxa3xx_pinctrl_exit(void)
240 module_exit(pxa3xx_pinctrl_exit);
242 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
243 MODULE_DESCRIPTION("PXA3xx pin control driver");
244 MODULE_LICENSE("GPL v2");