blob: 0afaf79a4e5175f99233726d07ea086758e72623 [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 Kumarda899472015-07-17 16:23:50 -07005 * Viresh Kumar <vireshk@kernel.org>
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
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -080085void pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup *gpio_pingroup,
86 unsigned count, u16 reg)
Viresh Kumarf4f8e562012-10-27 15:21:38 +053087{
Axel Lin4484d0b2012-11-13 15:31:41 +080088 int i, j;
Viresh Kumarf4f8e562012-10-27 15:21:38 +053089
Axel Lin4484d0b2012-11-13 15:31:41 +080090 for (i = 0; i < count; i++)
91 for (j = 0; j < gpio_pingroup[i].nmuxregs; j++)
Viresh Kumarf4f8e562012-10-27 15:21:38 +053092 gpio_pingroup[i].muxregs[j].reg = reg;
93}
94
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -080095void pmx_init_addr(struct spear_pinctrl_machdata *machdata, u16 reg)
Viresh Kumardeda8282012-03-28 22:27:07 +053096{
97 struct spear_pingroup *pgroup;
98 struct spear_modemux *modemux;
99 int i, j, group;
100
101 for (group = 0; group < machdata->ngroups; group++) {
102 pgroup = machdata->groups[group];
103
104 for (i = 0; i < pgroup->nmodemuxs; i++) {
105 modemux = &pgroup->modemuxs[i];
106
107 for (j = 0; j < modemux->nmuxregs; j++)
108 if (modemux->muxregs[j].reg == 0xFFFF)
109 modemux->muxregs[j].reg = reg;
110 }
111 }
112}
113
114static int spear_pinctrl_get_groups_cnt(struct pinctrl_dev *pctldev)
115{
116 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
117
118 return pmx->machdata->ngroups;
119}
120
121static const char *spear_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
122 unsigned group)
123{
124 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
125
126 return pmx->machdata->groups[group]->name;
127}
128
129static int spear_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
130 unsigned group, const unsigned **pins, unsigned *num_pins)
131{
132 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
133
134 *pins = pmx->machdata->groups[group]->pins;
135 *num_pins = pmx->machdata->groups[group]->npins;
136
137 return 0;
138}
139
140static void spear_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
141 struct seq_file *s, unsigned offset)
142{
143 seq_printf(s, " " DRIVER_NAME);
144}
145
Axel Lin9b2b1742012-11-11 10:29:40 +0800146static int spear_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
147 struct device_node *np_config,
148 struct pinctrl_map **map,
149 unsigned *num_maps)
Viresh Kumardeda8282012-03-28 22:27:07 +0530150{
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
Axel Lin9b2b1742012-11-11 10:29:40 +0800194static void spear_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
195 struct pinctrl_map *map,
196 unsigned num_maps)
Viresh Kumardeda8282012-03-28 22:27:07 +0530197{
198 kfree(map);
199}
200
Laurent Pinchart022ab142013-02-16 10:25:07 +0100201static const struct pinctrl_ops spear_pinctrl_ops = {
Viresh Kumardeda8282012-03-28 22:27:07 +0530202 .get_groups_count = spear_pinctrl_get_groups_cnt,
203 .get_group_name = spear_pinctrl_get_group_name,
204 .get_group_pins = spear_pinctrl_get_group_pins,
205 .pin_dbg_show = spear_pinctrl_pin_dbg_show,
206 .dt_node_to_map = spear_pinctrl_dt_node_to_map,
207 .dt_free_map = spear_pinctrl_dt_free_map,
208};
209
210static int spear_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
211{
212 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
213
214 return pmx->machdata->nfunctions;
215}
216
217static const char *spear_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
218 unsigned function)
219{
220 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
221
222 return pmx->machdata->functions[function]->name;
223}
224
225static int spear_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
226 unsigned function, const char *const **groups,
227 unsigned * const ngroups)
228{
229 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
230
231 *groups = pmx->machdata->functions[function]->groups;
232 *ngroups = pmx->machdata->functions[function]->ngroups;
233
234 return 0;
235}
236
237static int spear_pinctrl_endisable(struct pinctrl_dev *pctldev,
238 unsigned function, unsigned group, bool enable)
239{
240 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
241 const struct spear_pingroup *pgroup;
242 const struct spear_modemux *modemux;
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530243 int i;
Viresh Kumardeda8282012-03-28 22:27:07 +0530244 bool found = false;
245
246 pgroup = pmx->machdata->groups[group];
247
248 for (i = 0; i < pgroup->nmodemuxs; i++) {
249 modemux = &pgroup->modemuxs[i];
250
251 /* SoC have any modes */
252 if (pmx->machdata->modes_supported) {
253 if (!(pmx->machdata->mode & modemux->modes))
254 continue;
255 }
256
257 found = true;
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530258 muxregs_endisable(pmx, modemux->muxregs, modemux->nmuxregs,
259 enable);
Viresh Kumardeda8282012-03-28 22:27:07 +0530260 }
261
262 if (!found) {
263 dev_err(pmx->dev, "pinmux group: %s not supported\n",
264 pgroup->name);
265 return -ENODEV;
266 }
267
268 return 0;
269}
270
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200271static int spear_pinctrl_set_mux(struct pinctrl_dev *pctldev, unsigned function,
Viresh Kumardeda8282012-03-28 22:27:07 +0530272 unsigned group)
273{
274 return spear_pinctrl_endisable(pctldev, function, group, true);
275}
276
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530277/* gpio with pinmux */
278static struct spear_gpio_pingroup *get_gpio_pingroup(struct spear_pmx *pmx,
279 unsigned pin)
280{
281 struct spear_gpio_pingroup *gpio_pingroup;
Axel Lin6f37b1b2012-11-13 20:20:50 +0800282 int i, j;
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530283
284 if (!pmx->machdata->gpio_pingroups)
285 return NULL;
286
Axel Lin6f37b1b2012-11-13 20:20:50 +0800287 for (i = 0; i < pmx->machdata->ngpio_pingroups; i++) {
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530288 gpio_pingroup = &pmx->machdata->gpio_pingroups[i];
289
290 for (j = 0; j < gpio_pingroup->npins; j++) {
291 if (gpio_pingroup->pins[j] == pin)
292 return gpio_pingroup;
293 }
294 }
295
Axel Lin6f37b1b2012-11-13 20:20:50 +0800296 return NULL;
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530297}
298
299static int gpio_request_endisable(struct pinctrl_dev *pctldev,
300 struct pinctrl_gpio_range *range, unsigned offset, bool enable)
301{
302 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
Shiraz Hashim826d6ca2012-11-07 20:07:25 +0530303 struct spear_pinctrl_machdata *machdata = pmx->machdata;
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530304 struct spear_gpio_pingroup *gpio_pingroup;
305
Shiraz Hashim826d6ca2012-11-07 20:07:25 +0530306 /*
307 * Some SoC have configuration options applicable to group of pins,
308 * rather than a single pin.
309 */
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530310 gpio_pingroup = get_gpio_pingroup(pmx, offset);
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530311 if (gpio_pingroup)
312 muxregs_endisable(pmx, gpio_pingroup->muxregs,
313 gpio_pingroup->nmuxregs, enable);
314
Shiraz Hashim826d6ca2012-11-07 20:07:25 +0530315 /*
316 * SoC may need some extra configurations, or configurations for single
317 * pin
318 */
319 if (machdata->gpio_request_endisable)
320 machdata->gpio_request_endisable(pmx, offset, enable);
321
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530322 return 0;
323}
324
325static int gpio_request_enable(struct pinctrl_dev *pctldev,
326 struct pinctrl_gpio_range *range, unsigned offset)
327{
328 return gpio_request_endisable(pctldev, range, offset, true);
329}
330
331static void gpio_disable_free(struct pinctrl_dev *pctldev,
332 struct pinctrl_gpio_range *range, unsigned offset)
333{
334 gpio_request_endisable(pctldev, range, offset, false);
335}
336
Laurent Pinchart022ab142013-02-16 10:25:07 +0100337static const struct pinmux_ops spear_pinmux_ops = {
Viresh Kumardeda8282012-03-28 22:27:07 +0530338 .get_functions_count = spear_pinctrl_get_funcs_count,
339 .get_function_name = spear_pinctrl_get_func_name,
340 .get_function_groups = spear_pinctrl_get_func_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200341 .set_mux = spear_pinctrl_set_mux,
Viresh Kumarf4f8e562012-10-27 15:21:38 +0530342 .gpio_request_enable = gpio_request_enable,
343 .gpio_disable_free = gpio_disable_free,
Viresh Kumardeda8282012-03-28 22:27:07 +0530344};
345
346static struct pinctrl_desc spear_pinctrl_desc = {
347 .name = DRIVER_NAME,
348 .pctlops = &spear_pinctrl_ops,
349 .pmxops = &spear_pinmux_ops,
350 .owner = THIS_MODULE,
351};
352
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800353int spear_pinctrl_probe(struct platform_device *pdev,
354 struct spear_pinctrl_machdata *machdata)
Viresh Kumardeda8282012-03-28 22:27:07 +0530355{
356 struct device_node *np = pdev->dev.of_node;
357 struct resource *res;
358 struct spear_pmx *pmx;
359
360 if (!machdata)
361 return -ENODEV;
362
Viresh Kumardeda8282012-03-28 22:27:07 +0530363 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
364 if (!pmx) {
365 dev_err(&pdev->dev, "Can't alloc spear_pmx\n");
366 return -ENOMEM;
367 }
368
Axel Lindff5a992013-08-26 08:20:45 +0800369 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
370 pmx->vbase = devm_ioremap_resource(&pdev->dev, res);
371 if (IS_ERR(pmx->vbase))
372 return PTR_ERR(pmx->vbase);
Viresh Kumardeda8282012-03-28 22:27:07 +0530373
374 pmx->dev = &pdev->dev;
375 pmx->machdata = machdata;
376
377 /* configure mode, if supported by SoC */
378 if (machdata->modes_supported) {
379 int mode = 0;
380
381 if (of_property_read_u32(np, "st,pinmux-mode", &mode)) {
382 dev_err(&pdev->dev, "OF: pinmux mode not passed\n");
383 return -EINVAL;
384 }
385
386 if (set_mode(pmx, mode)) {
387 dev_err(&pdev->dev, "OF: Couldn't configure mode: %x\n",
388 mode);
389 return -EINVAL;
390 }
391 }
392
393 platform_set_drvdata(pdev, pmx);
394
395 spear_pinctrl_desc.pins = machdata->pins;
396 spear_pinctrl_desc.npins = machdata->npins;
397
398 pmx->pctl = pinctrl_register(&spear_pinctrl_desc, &pdev->dev, pmx);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900399 if (IS_ERR(pmx->pctl)) {
Viresh Kumardeda8282012-03-28 22:27:07 +0530400 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900401 return PTR_ERR(pmx->pctl);
Viresh Kumardeda8282012-03-28 22:27:07 +0530402 }
403
404 return 0;
405}
406
Bill Pembertonf90f54b2012-11-19 13:26:06 -0500407int spear_pinctrl_remove(struct platform_device *pdev)
Viresh Kumardeda8282012-03-28 22:27:07 +0530408{
409 struct spear_pmx *pmx = platform_get_drvdata(pdev);
410
411 pinctrl_unregister(pmx->pctl);
412
413 return 0;
414}