blob: f9483ae42726da68ed5d088e70270194b39ee737 [file] [log] [blame]
Viresh Kumardeda8282012-03-28 22:27:07 +05301/*
2 * Driver for the ST Microelectronics SPEAr pinmux
3 *
4 * Copyright (C) 2012 ST Microelectronics
Viresh Kumar10d89352012-06-20 12:53:02 -07005 * Viresh Kumar <viresh.linux@gmail.com>
Viresh Kumardeda8282012-03-28 22:27:07 +05306 *
7 * Inspired from:
8 * - U300 Pinctl drivers
9 * - Tegra Pinctl drivers
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
16#include <linux/err.h>
Viresh Kumardeda8282012-03-28 22:27:07 +053017#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_address.h>
Viresh Kumarf4f8e562012-10-27 15:21:38 +053020#include <linux/of_gpio.h>
Viresh Kumardeda8282012-03-28 22:27:07 +053021#include <linux/pinctrl/machine.h>
22#include <linux/pinctrl/pinctrl.h>
23#include <linux/pinctrl/pinmux.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
27#include "pinctrl-spear.h"
28
29#define DRIVER_NAME "spear-pinmux"
30
Viresh Kumarf4f8e562012-10-27 15:21:38 +053031static void muxregs_endisable(struct spear_pmx *pmx,
32 struct spear_muxreg *muxregs, u8 count, bool enable)
33{
34 struct spear_muxreg *muxreg;
35 u32 val, temp, j;
36
37 for (j = 0; j < count; j++) {
38 muxreg = &muxregs[j];
39
40 val = pmx_readl(pmx, muxreg->reg);
41 val &= ~muxreg->mask;
42
43 if (enable)
44 temp = muxreg->val;
45 else
46 temp = ~muxreg->val;
47
48 val |= muxreg->mask & temp;
49 pmx_writel(pmx, val, muxreg->reg);
50 }
51}
52
Viresh Kumardeda8282012-03-28 22:27:07 +053053static int set_mode(struct spear_pmx *pmx, int mode)
54{
55 struct spear_pmx_mode *pmx_mode = NULL;
56 int i;
57 u32 val;
58
59 if (!pmx->machdata->pmx_modes || !pmx->machdata->npmx_modes)
60 return -EINVAL;
61
62 for (i = 0; i < pmx->machdata->npmx_modes; i++) {
63 if (pmx->machdata->pmx_modes[i]->mode == (1 << mode)) {
64 pmx_mode = pmx->machdata->pmx_modes[i];
65 break;
66 }
67 }
68
69 if (!pmx_mode)
70 return -EINVAL;
71
72 val = pmx_readl(pmx, pmx_mode->reg);
73 val &= ~pmx_mode->mask;
74 val |= pmx_mode->val;
75 pmx_writel(pmx, val, pmx_mode->reg);
76
77 pmx->machdata->mode = pmx_mode->mode;
78 dev_info(pmx->dev, "Configured Mode: %s with id: %x\n\n",
79 pmx_mode->name ? pmx_mode->name : "no_name",
80 pmx_mode->reg);
81
82 return 0;
83}
84
Viresh Kumarf4f8e562012-10-27 15:21:38 +053085void __devinit
86pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup *gpio_pingroup,
87 unsigned count, u16 reg)
88{
89 int i = 0, j = 0;
90
91 for (; i < count; i++)
92 for (; j < gpio_pingroup[i].nmuxregs; j++)
93 gpio_pingroup[i].muxregs[j].reg = reg;
94}
95
Viresh Kumardeda8282012-03-28 22:27:07 +053096void __devinit pmx_init_addr(struct spear_pinctrl_machdata *machdata, u16 reg)
97{
98 struct spear_pingroup *pgroup;
99 struct spear_modemux *modemux;
100 int i, j, group;
101
102 for (group = 0; group < machdata->ngroups; group++) {
103 pgroup = machdata->groups[group];
104
105 for (i = 0; i < pgroup->nmodemuxs; i++) {
106 modemux = &pgroup->modemuxs[i];
107
108 for (j = 0; j < modemux->nmuxregs; j++)
109 if (modemux->muxregs[j].reg == 0xFFFF)
110 modemux->muxregs[j].reg = reg;
111 }
112 }
113}
114
115static int spear_pinctrl_get_groups_cnt(struct pinctrl_dev *pctldev)
116{
117 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
118
119 return pmx->machdata->ngroups;
120}
121
122static const char *spear_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
123 unsigned group)
124{
125 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
126
127 return pmx->machdata->groups[group]->name;
128}
129
130static int spear_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
131 unsigned group, const unsigned **pins, unsigned *num_pins)
132{
133 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
134
135 *pins = pmx->machdata->groups[group]->pins;
136 *num_pins = pmx->machdata->groups[group]->npins;
137
138 return 0;
139}
140
141static void spear_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
142 struct seq_file *s, unsigned offset)
143{
144 seq_printf(s, " " DRIVER_NAME);
145}
146
147int spear_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
148 struct device_node *np_config,
149 struct pinctrl_map **map, unsigned *num_maps)
150{
151 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
152 struct device_node *np;
153 struct property *prop;
154 const char *function, *group;
155 int ret, index = 0, count = 0;
156
157 /* calculate number of maps required */
158 for_each_child_of_node(np_config, np) {
159 ret = of_property_read_string(np, "st,function", &function);
160 if (ret < 0)
161 return ret;
162
163 ret = of_property_count_strings(np, "st,pins");
164 if (ret < 0)
165 return ret;
166
167 count += ret;
168 }
169
170 if (!count) {
171 dev_err(pmx->dev, "No child nodes passed via DT\n");
172 return -ENODEV;
173 }
174
175 *map = kzalloc(sizeof(**map) * count, GFP_KERNEL);
176 if (!*map)
177 return -ENOMEM;
178
179 for_each_child_of_node(np_config, np) {
180 of_property_read_string(np, "st,function", &function);
181 of_property_for_each_string(np, "st,pins", prop, group) {
182 (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
183 (*map)[index].data.mux.group = group;
184 (*map)[index].data.mux.function = function;
185 index++;
186 }
187 }
188
189 *num_maps = count;
190
191 return 0;
192}
193
194void spear_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
195 struct pinctrl_map *map, unsigned num_maps)
196{
197 kfree(map);
198}
199
200static struct pinctrl_ops spear_pinctrl_ops = {
201 .get_groups_count = spear_pinctrl_get_groups_cnt,
202 .get_group_name = spear_pinctrl_get_group_name,
203 .get_group_pins = spear_pinctrl_get_group_pins,
204 .pin_dbg_show = spear_pinctrl_pin_dbg_show,
205 .dt_node_to_map = spear_pinctrl_dt_node_to_map,
206 .dt_free_map = spear_pinctrl_dt_free_map,
207};
208
209static int spear_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
210{
211 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
212
213 return pmx->machdata->nfunctions;
214}
215
216static const char *spear_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
217 unsigned function)
218{
219 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
220
221 return pmx->machdata->functions[function]->name;
222}
223
224static int spear_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
225 unsigned function, const char *const **groups,
226 unsigned * const ngroups)
227{
228 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
229
230 *groups = pmx->machdata->functions[function]->groups;
231 *ngroups = pmx->machdata->functions[function]->ngroups;
232
233 return 0;
234}
235
236static int spear_pinctrl_endisable(struct pinctrl_dev *pctldev,
237 unsigned function, unsigned group, bool enable)
238{
239 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
240 const struct spear_pingroup *pgroup;
241 const struct spear_modemux *modemux;
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530242 int i;
Viresh Kumardeda8282012-03-28 22:27:07 +0530243 bool found = false;
244
245 pgroup = pmx->machdata->groups[group];
246
247 for (i = 0; i < pgroup->nmodemuxs; i++) {
248 modemux = &pgroup->modemuxs[i];
249
250 /* SoC have any modes */
251 if (pmx->machdata->modes_supported) {
252 if (!(pmx->machdata->mode & modemux->modes))
253 continue;
254 }
255
256 found = true;
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530257 muxregs_endisable(pmx, modemux->muxregs, modemux->nmuxregs,
258 enable);
Viresh Kumardeda8282012-03-28 22:27:07 +0530259 }
260
261 if (!found) {
262 dev_err(pmx->dev, "pinmux group: %s not supported\n",
263 pgroup->name);
264 return -ENODEV;
265 }
266
267 return 0;
268}
269
270static int spear_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned function,
271 unsigned group)
272{
273 return spear_pinctrl_endisable(pctldev, function, group, true);
274}
275
276static void spear_pinctrl_disable(struct pinctrl_dev *pctldev,
277 unsigned function, unsigned group)
278{
279 spear_pinctrl_endisable(pctldev, function, group, false);
280}
281
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530282/* gpio with pinmux */
283static struct spear_gpio_pingroup *get_gpio_pingroup(struct spear_pmx *pmx,
284 unsigned pin)
285{
286 struct spear_gpio_pingroup *gpio_pingroup;
287 int i = 0, j;
288
289 if (!pmx->machdata->gpio_pingroups)
290 return NULL;
291
292 for (; i < pmx->machdata->ngpio_pingroups; i++) {
293 gpio_pingroup = &pmx->machdata->gpio_pingroups[i];
294
295 for (j = 0; j < gpio_pingroup->npins; j++) {
296 if (gpio_pingroup->pins[j] == pin)
297 return gpio_pingroup;
298 }
299 }
300
301 return ERR_PTR(-EINVAL);
302}
303
304static int gpio_request_endisable(struct pinctrl_dev *pctldev,
305 struct pinctrl_gpio_range *range, unsigned offset, bool enable)
306{
307 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
Shiraz Hashim826d6ca2012-11-07 20:07:25 +0530308 struct spear_pinctrl_machdata *machdata = pmx->machdata;
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530309 struct spear_gpio_pingroup *gpio_pingroup;
310
Shiraz Hashim826d6ca2012-11-07 20:07:25 +0530311 /*
312 * Some SoC have configuration options applicable to group of pins,
313 * rather than a single pin.
314 */
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530315 gpio_pingroup = get_gpio_pingroup(pmx, offset);
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530316 if (gpio_pingroup)
317 muxregs_endisable(pmx, gpio_pingroup->muxregs,
318 gpio_pingroup->nmuxregs, enable);
319
Shiraz Hashim826d6ca2012-11-07 20:07:25 +0530320 /*
321 * SoC may need some extra configurations, or configurations for single
322 * pin
323 */
324 if (machdata->gpio_request_endisable)
325 machdata->gpio_request_endisable(pmx, offset, enable);
326
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530327 return 0;
328}
329
330static int gpio_request_enable(struct pinctrl_dev *pctldev,
331 struct pinctrl_gpio_range *range, unsigned offset)
332{
333 return gpio_request_endisable(pctldev, range, offset, true);
334}
335
336static void gpio_disable_free(struct pinctrl_dev *pctldev,
337 struct pinctrl_gpio_range *range, unsigned offset)
338{
339 gpio_request_endisable(pctldev, range, offset, false);
340}
341
Viresh Kumardeda8282012-03-28 22:27:07 +0530342static struct pinmux_ops spear_pinmux_ops = {
343 .get_functions_count = spear_pinctrl_get_funcs_count,
344 .get_function_name = spear_pinctrl_get_func_name,
345 .get_function_groups = spear_pinctrl_get_func_groups,
346 .enable = spear_pinctrl_enable,
347 .disable = spear_pinctrl_disable,
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530348 .gpio_request_enable = gpio_request_enable,
349 .gpio_disable_free = gpio_disable_free,
Viresh Kumardeda8282012-03-28 22:27:07 +0530350};
351
352static struct pinctrl_desc spear_pinctrl_desc = {
353 .name = DRIVER_NAME,
354 .pctlops = &spear_pinctrl_ops,
355 .pmxops = &spear_pinmux_ops,
356 .owner = THIS_MODULE,
357};
358
359int __devinit spear_pinctrl_probe(struct platform_device *pdev,
360 struct spear_pinctrl_machdata *machdata)
361{
362 struct device_node *np = pdev->dev.of_node;
363 struct resource *res;
364 struct spear_pmx *pmx;
365
366 if (!machdata)
367 return -ENODEV;
368
369 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
370 if (!res)
371 return -EINVAL;
372
373 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
374 if (!pmx) {
375 dev_err(&pdev->dev, "Can't alloc spear_pmx\n");
376 return -ENOMEM;
377 }
378
379 pmx->vbase = devm_ioremap(&pdev->dev, res->start, resource_size(res));
380 if (!pmx->vbase) {
381 dev_err(&pdev->dev, "Couldn't ioremap at index 0\n");
382 return -ENODEV;
383 }
384
385 pmx->dev = &pdev->dev;
386 pmx->machdata = machdata;
387
388 /* configure mode, if supported by SoC */
389 if (machdata->modes_supported) {
390 int mode = 0;
391
392 if (of_property_read_u32(np, "st,pinmux-mode", &mode)) {
393 dev_err(&pdev->dev, "OF: pinmux mode not passed\n");
394 return -EINVAL;
395 }
396
397 if (set_mode(pmx, mode)) {
398 dev_err(&pdev->dev, "OF: Couldn't configure mode: %x\n",
399 mode);
400 return -EINVAL;
401 }
402 }
403
404 platform_set_drvdata(pdev, pmx);
405
406 spear_pinctrl_desc.pins = machdata->pins;
407 spear_pinctrl_desc.npins = machdata->npins;
408
409 pmx->pctl = pinctrl_register(&spear_pinctrl_desc, &pdev->dev, pmx);
Devendra Naga8003ae32012-06-18 23:16:04 +0530410 if (!pmx->pctl) {
Viresh Kumardeda8282012-03-28 22:27:07 +0530411 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Devendra Naga8003ae32012-06-18 23:16:04 +0530412 return -ENODEV;
Viresh Kumardeda8282012-03-28 22:27:07 +0530413 }
414
415 return 0;
416}
417
418int __devexit spear_pinctrl_remove(struct platform_device *pdev)
419{
420 struct spear_pmx *pmx = platform_get_drvdata(pdev);
421
422 pinctrl_unregister(pmx->pctl);
423
424 return 0;
425}