blob: 51f8a388b917a991eb1cba5ca2d1c08973ed4076 [file] [log] [blame]
Haojian Zhuangf4e66982012-01-04 10:26:33 +08001/*
2 * linux/drivers/pinctrl/pinctrl-pxa3xx.c
3 *
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.
7 *
8 * Copyright (C) 2011, Marvell Technology Group Ltd.
9 *
10 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/io.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include "pinctrl-pxa3xx.h"
20
21static struct pinctrl_gpio_range pxa3xx_pinctrl_gpio_range = {
22 .name = "PXA3xx GPIO",
23 .id = 0,
24 .base = 0,
25 .pin_base = 0,
26};
27
Viresh Kumard1e90e92012-03-30 11:25:40 +053028static int pxa3xx_get_groups_count(struct pinctrl_dev *pctrldev)
Haojian Zhuangf4e66982012-01-04 10:26:33 +080029{
30 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +053031
32 return info->num_grps;
Haojian Zhuangf4e66982012-01-04 10:26:33 +080033}
34
35static const char *pxa3xx_get_group_name(struct pinctrl_dev *pctrldev,
36 unsigned selector)
37{
38 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +053039
Haojian Zhuangf4e66982012-01-04 10:26:33 +080040 return info->grps[selector].name;
41}
42
43static int pxa3xx_get_group_pins(struct pinctrl_dev *pctrldev,
44 unsigned selector,
45 const unsigned **pins,
46 unsigned *num_pins)
47{
48 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +053049
Haojian Zhuangf4e66982012-01-04 10:26:33 +080050 *pins = info->grps[selector].pins;
51 *num_pins = info->grps[selector].npins;
52 return 0;
53}
54
55static struct pinctrl_ops pxa3xx_pctrl_ops = {
Viresh Kumard1e90e92012-03-30 11:25:40 +053056 .get_groups_count = pxa3xx_get_groups_count,
Haojian Zhuangf4e66982012-01-04 10:26:33 +080057 .get_group_name = pxa3xx_get_group_name,
58 .get_group_pins = pxa3xx_get_group_pins,
59};
60
Viresh Kumard1e90e92012-03-30 11:25:40 +053061static int pxa3xx_pmx_get_funcs_count(struct pinctrl_dev *pctrldev)
Haojian Zhuangf4e66982012-01-04 10:26:33 +080062{
63 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
Viresh Kumard1e90e92012-03-30 11:25:40 +053064
65 return info->num_funcs;
Haojian Zhuangf4e66982012-01-04 10:26:33 +080066}
67
68static const char *pxa3xx_pmx_get_func_name(struct pinctrl_dev *pctrldev,
69 unsigned func)
70{
71 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
72 return info->funcs[func].name;
73}
74
75static int pxa3xx_pmx_get_groups(struct pinctrl_dev *pctrldev, unsigned func,
76 const char * const **groups,
77 unsigned * const num_groups)
78{
79 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
80 *groups = info->funcs[func].groups;
81 *num_groups = info->funcs[func].num_groups;
82 return 0;
83}
84
85/* Return function number. If failure, return negative value. */
86static int match_mux(struct pxa3xx_mfp_pin *mfp, unsigned mux)
87{
88 int i;
89 for (i = 0; i < PXA3xx_MAX_MUX; i++) {
90 if (mfp->func[i] == mux)
91 break;
92 }
93 if (i >= PXA3xx_MAX_MUX)
94 return -EINVAL;
95 return i;
96}
97
98/* check whether current pin configuration is valid. Negative for failure */
99static int match_group_mux(struct pxa3xx_pin_group *grp,
100 struct pxa3xx_pinmux_info *info,
101 unsigned mux)
102{
103 int i, pin, ret = 0;
104 for (i = 0; i < grp->npins; i++) {
105 pin = grp->pins[i];
106 ret = match_mux(&info->mfp[pin], mux);
107 if (ret < 0) {
108 dev_err(info->dev, "Can't find mux %d on pin%d\n",
109 mux, pin);
110 break;
111 }
112 }
113 return ret;
114}
115
116static int pxa3xx_pmx_enable(struct pinctrl_dev *pctrldev, unsigned func,
117 unsigned group)
118{
119 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
120 struct pxa3xx_pin_group *pin_grp = &info->grps[group];
121 unsigned int data;
122 int i, mfpr, pin, pin_func;
123
124 if (!pin_grp->npins ||
125 (match_group_mux(pin_grp, info, pin_grp->mux) < 0)) {
126 dev_err(info->dev, "Failed to set the pin group: %d\n", group);
127 return -EINVAL;
128 }
129 for (i = 0; i < pin_grp->npins; i++) {
130 pin = pin_grp->pins[i];
131 pin_func = match_mux(&info->mfp[pin], pin_grp->mux);
132 mfpr = info->mfp[pin].mfpr;
133 data = readl_relaxed(info->virt_base + mfpr);
134 data &= ~MFPR_FUNC_MASK;
135 data |= pin_func;
136 writel_relaxed(data, info->virt_base + mfpr);
137 }
138 return 0;
139}
140
Haojian Zhuangf4e66982012-01-04 10:26:33 +0800141static int pxa3xx_pmx_request_gpio(struct pinctrl_dev *pctrldev,
142 struct pinctrl_gpio_range *range,
143 unsigned pin)
144{
145 struct pxa3xx_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
146 unsigned int data;
147 int pin_func, mfpr;
148
149 pin_func = match_mux(&info->mfp[pin], PXA3xx_MUX_GPIO);
150 if (pin_func < 0) {
151 dev_err(info->dev, "No GPIO function on pin%d (%s)\n",
152 pin, info->pads[pin].name);
153 return -EINVAL;
154 }
155 mfpr = info->mfp[pin].mfpr;
156 /* write gpio function into mfpr register */
157 data = readl_relaxed(info->virt_base + mfpr) & ~MFPR_FUNC_MASK;
158 data |= pin_func;
159 writel_relaxed(data, info->virt_base + mfpr);
160 return 0;
161}
162
163static struct pinmux_ops pxa3xx_pmx_ops = {
Viresh Kumard1e90e92012-03-30 11:25:40 +0530164 .get_functions_count = pxa3xx_pmx_get_funcs_count,
Haojian Zhuangf4e66982012-01-04 10:26:33 +0800165 .get_function_name = pxa3xx_pmx_get_func_name,
166 .get_function_groups = pxa3xx_pmx_get_groups,
167 .enable = pxa3xx_pmx_enable,
Haojian Zhuangf4e66982012-01-04 10:26:33 +0800168 .gpio_request_enable = pxa3xx_pmx_request_gpio,
169};
170
171int pxa3xx_pinctrl_register(struct platform_device *pdev,
172 struct pxa3xx_pinmux_info *info)
173{
174 struct pinctrl_desc *desc;
175 struct resource *res;
Haojian Zhuangf4e66982012-01-04 10:26:33 +0800176
177 if (!info || !info->cputype)
178 return -EINVAL;
179 desc = info->desc;
180 desc->pins = info->pads;
181 desc->npins = info->num_pads;
182 desc->pctlops = &pxa3xx_pctrl_ops;
183 desc->pmxops = &pxa3xx_pmx_ops;
184 info->dev = &pdev->dev;
185 pxa3xx_pinctrl_gpio_range.npins = info->num_gpio;
186
187 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
188 if (!res)
189 return -ENOENT;
Axel Lin93877602012-11-12 14:45:50 +0800190 info->virt_base = devm_request_and_ioremap(&pdev->dev, res);
Haojian Zhuangf4e66982012-01-04 10:26:33 +0800191 if (!info->virt_base)
192 return -ENOMEM;
193 info->pctrl = pinctrl_register(desc, &pdev->dev, info);
194 if (!info->pctrl) {
195 dev_err(&pdev->dev, "failed to register PXA pinmux driver\n");
Axel Lin93877602012-11-12 14:45:50 +0800196 return -EINVAL;
Haojian Zhuangf4e66982012-01-04 10:26:33 +0800197 }
198 pinctrl_add_gpio_range(info->pctrl, &pxa3xx_pinctrl_gpio_range);
199 platform_set_drvdata(pdev, info);
200 return 0;
Haojian Zhuangf4e66982012-01-04 10:26:33 +0800201}
202
203int pxa3xx_pinctrl_unregister(struct platform_device *pdev)
204{
205 struct pxa3xx_pinmux_info *info = platform_get_drvdata(pdev);
206
207 pinctrl_unregister(info->pctrl);
Haojian Zhuangf4e66982012-01-04 10:26:33 +0800208 platform_set_drvdata(pdev, NULL);
209 return 0;
210}
211
212static int __init pxa3xx_pinctrl_init(void)
213{
214 pr_info("pxa3xx-pinctrl: PXA3xx pinctrl driver initializing\n");
215 return 0;
216}
217core_initcall_sync(pxa3xx_pinctrl_init);
218
219static void __exit pxa3xx_pinctrl_exit(void)
220{
221}
222module_exit(pxa3xx_pinctrl_exit);
223
224MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
225MODULE_DESCRIPTION("PXA3xx pin control driver");
226MODULE_LICENSE("GPL v2");