blob: e79e379917fcb8cb68da834b76a7f71f8e7d2980 [file] [log] [blame]
Paul Mundtca5481c62012-07-10 12:08:14 +09001/*
2 * SuperH Pin Function Controller pinmux support.
3 *
4 * Copyright (C) 2012 Paul Mundt
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
Paul Mundta2d3aff2012-07-11 17:21:04 +090010#define pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt
Paul Mundtca5481c62012-07-10 12:08:14 +090011
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/sh_pfc.h>
15#include <linux/err.h>
16#include <linux/slab.h>
Paul Mundtd93a8912012-07-11 17:17:10 +090017#include <linux/spinlock.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090018#include <linux/platform_device.h>
19#include <linux/pinctrl/consumer.h>
20#include <linux/pinctrl/pinctrl.h>
21#include <linux/pinctrl/pinconf.h>
22#include <linux/pinctrl/pinmux.h>
23#include <linux/pinctrl/pinconf-generic.h>
24
25struct sh_pfc_pinctrl {
26 struct pinctrl_dev *pctl;
27 struct sh_pfc *pfc;
28
Paul Mundtd93a8912012-07-11 17:17:10 +090029 struct pinmux_gpio **functions;
30 unsigned int nr_functions;
31
Paul Mundtca5481c62012-07-10 12:08:14 +090032 struct pinctrl_pin_desc *pads;
33 unsigned int nr_pads;
Paul Mundtd93a8912012-07-11 17:17:10 +090034
35 spinlock_t lock;
Paul Mundtca5481c62012-07-10 12:08:14 +090036};
37
38static struct sh_pfc_pinctrl *sh_pfc_pmx;
39
40/*
41 * No group support yet
42 */
43static int sh_pfc_get_noop_count(struct pinctrl_dev *pctldev)
44{
45 return 0;
46}
47
48static const char *sh_pfc_get_noop_name(struct pinctrl_dev *pctldev,
49 unsigned selector)
50{
51 return NULL;
52}
53
54static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
55 const unsigned **pins, unsigned *num_pins)
56{
57 return -ENOTSUPP;
58}
59
60static struct pinctrl_ops sh_pfc_pinctrl_ops = {
61 .get_groups_count = sh_pfc_get_noop_count,
62 .get_group_name = sh_pfc_get_noop_name,
63 .get_group_pins = sh_pfc_get_group_pins,
64};
65
Paul Mundtd93a8912012-07-11 17:17:10 +090066static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
67{
68 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtca5481c62012-07-10 12:08:14 +090069
Paul Mundtd93a8912012-07-11 17:17:10 +090070 return pmx->nr_functions;
71}
72
73static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
74 unsigned selector)
75{
76 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
77
78 return pmx->functions[selector]->name;
79}
80
Paul Mundtca5481c62012-07-10 12:08:14 +090081static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, unsigned func,
82 const char * const **groups,
83 unsigned * const num_groups)
84{
Paul Mundtd93a8912012-07-11 17:17:10 +090085 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
86
87 *groups = &pmx->functions[func]->name;
88 *num_groups = 1;
89
Paul Mundtca5481c62012-07-10 12:08:14 +090090 return 0;
91}
92
93static int sh_pfc_noop_enable(struct pinctrl_dev *pctldev, unsigned func,
94 unsigned group)
95{
96 return 0;
97}
98
99static void sh_pfc_noop_disable(struct pinctrl_dev *pctldev, unsigned func,
100 unsigned group)
101{
102}
103
Paul Mundtd93a8912012-07-11 17:17:10 +0900104static inline int sh_pfc_config_function(struct sh_pfc *pfc, unsigned offset)
105{
106 if (sh_pfc_config_gpio(pfc, offset,
107 PINMUX_TYPE_FUNCTION,
108 GPIO_CFG_DRYRUN) != 0)
109 return -EINVAL;
110
111 if (sh_pfc_config_gpio(pfc, offset,
112 PINMUX_TYPE_FUNCTION,
113 GPIO_CFG_REQ) != 0)
114 return -EINVAL;
115
116 return 0;
117}
118
Paul Mundtca5481c62012-07-10 12:08:14 +0900119static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
120 struct pinctrl_gpio_range *range,
121 unsigned offset)
122{
123 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
124 struct sh_pfc *pfc = pmx->pfc;
Paul Mundtca5481c62012-07-10 12:08:14 +0900125 unsigned long flags;
Paul Mundtd93a8912012-07-11 17:17:10 +0900126 int ret, pinmux_type;
Paul Mundtca5481c62012-07-10 12:08:14 +0900127
128 spin_lock_irqsave(&pfc->lock, flags);
129
Paul Mundtd93a8912012-07-11 17:17:10 +0900130 pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
Paul Mundtca5481c62012-07-10 12:08:14 +0900131
Paul Mundtd93a8912012-07-11 17:17:10 +0900132 switch (pinmux_type) {
133 case PINMUX_TYPE_FUNCTION:
134 pr_notice_once("Use of GPIO API for function requests is "
135 "deprecated, convert to pinctrl\n");
136 /* handle for now */
137 ret = sh_pfc_config_function(pfc, offset);
138 if (unlikely(ret < 0))
Paul Mundtca5481c62012-07-10 12:08:14 +0900139 goto err;
140
Paul Mundtd93a8912012-07-11 17:17:10 +0900141 break;
142 case PINMUX_TYPE_GPIO:
143 break;
144 default:
145 pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type);
146 return -ENOTSUPP;
147 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900148
149 ret = 0;
150
151err:
152 spin_unlock_irqrestore(&pfc->lock, flags);
153
154 return ret;
155}
156
157static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
158 struct pinctrl_gpio_range *range,
159 unsigned offset)
160{
161 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
162 struct sh_pfc *pfc = pmx->pfc;
163 unsigned long flags;
164 int pinmux_type;
165
166 spin_lock_irqsave(&pfc->lock, flags);
167
168 pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
169
170 sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
171
Paul Mundtca5481c62012-07-10 12:08:14 +0900172 spin_unlock_irqrestore(&pfc->lock, flags);
173}
174
175static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
176 struct pinctrl_gpio_range *range,
177 unsigned offset, bool input)
178{
179 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
180 struct sh_pfc *pfc = pmx->pfc;
181 unsigned long flags;
182 int pinmux_type, new_pinmux_type;
183 int ret = -EINVAL;
184
185 new_pinmux_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
186
187 spin_lock_irqsave(&pfc->lock, flags);
188
189 pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
190
191 switch (pinmux_type) {
192 case PINMUX_TYPE_GPIO:
193 break;
194 case PINMUX_TYPE_OUTPUT:
195 case PINMUX_TYPE_INPUT:
196 case PINMUX_TYPE_INPUT_PULLUP:
197 case PINMUX_TYPE_INPUT_PULLDOWN:
198 sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
199 break;
200 default:
201 goto err;
202 }
203
204 if (sh_pfc_config_gpio(pfc, offset,
205 new_pinmux_type,
206 GPIO_CFG_DRYRUN) != 0)
207 goto err;
208
209 if (sh_pfc_config_gpio(pfc, offset,
210 new_pinmux_type,
211 GPIO_CFG_REQ) != 0)
212 BUG();
213
214 pfc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
215 pfc->gpios[offset].flags |= new_pinmux_type;
216
217 ret = 0;
218
219err:
220 spin_unlock_irqrestore(&pfc->lock, flags);
221
222 return ret;
223}
224
225static struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900226 .get_functions_count = sh_pfc_get_functions_count,
227 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900228 .get_function_groups = sh_pfc_get_function_groups,
229 .enable = sh_pfc_noop_enable,
230 .disable = sh_pfc_noop_disable,
231 .gpio_request_enable = sh_pfc_gpio_request_enable,
232 .gpio_disable_free = sh_pfc_gpio_disable_free,
233 .gpio_set_direction = sh_pfc_gpio_set_direction,
234};
235
236static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
237 unsigned long *config)
238{
Paul Mundtd93a8912012-07-11 17:17:10 +0900239 enum pin_config_param param = (enum pin_config_param)(*config);
240
241 switch (param) {
242 default:
243 break;
244 }
245
Paul Mundtca5481c62012-07-10 12:08:14 +0900246 return -ENOTSUPP;
247}
248
249static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
250 unsigned long config)
251{
252 return -EINVAL;
253}
254
255static struct pinconf_ops sh_pfc_pinconf_ops = {
256 .is_generic = true,
257 .pin_config_get = sh_pfc_pinconf_get,
258 .pin_config_set = sh_pfc_pinconf_set,
259};
260
261static struct pinctrl_gpio_range sh_pfc_gpio_range = {
262 .name = KBUILD_MODNAME,
263 .id = 0,
264};
265
266static struct pinctrl_desc sh_pfc_pinctrl_desc = {
267 .name = KBUILD_MODNAME,
268 .owner = THIS_MODULE,
269 .pctlops = &sh_pfc_pinctrl_ops,
270 .pmxops = &sh_pfc_pinmux_ops,
271 .confops = &sh_pfc_pinconf_ops,
272};
273
274int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
275{
Paul Mundtd93a8912012-07-11 17:17:10 +0900276 sh_pfc_pmx = kzalloc(sizeof(struct sh_pfc_pinctrl), GFP_KERNEL);
Paul Mundtca5481c62012-07-10 12:08:14 +0900277 if (unlikely(!sh_pfc_pmx))
278 return -ENOMEM;
279
Paul Mundtd93a8912012-07-11 17:17:10 +0900280 spin_lock_init(&sh_pfc_pmx->lock);
281
Paul Mundtca5481c62012-07-10 12:08:14 +0900282 sh_pfc_pmx->pfc = pfc;
283
284 return 0;
285}
Paul Mundt1acbbb4ed2012-07-17 15:21:47 +0900286EXPORT_SYMBOL_GPL(sh_pfc_register_pinctrl);
Paul Mundtca5481c62012-07-10 12:08:14 +0900287
Paul Mundtd93a8912012-07-11 17:17:10 +0900288static inline void __devinit sh_pfc_map_one_gpio(struct sh_pfc *pfc,
289 struct sh_pfc_pinctrl *pmx,
290 struct pinmux_gpio *gpio,
291 unsigned offset)
292{
293 struct pinmux_data_reg *dummy;
294 unsigned long flags;
295 int bit;
296
297 gpio->flags &= ~PINMUX_FLAG_TYPE;
298
299 if (sh_pfc_get_data_reg(pfc, offset, &dummy, &bit) == 0)
300 gpio->flags |= PINMUX_TYPE_GPIO;
301 else {
302 gpio->flags |= PINMUX_TYPE_FUNCTION;
303
304 spin_lock_irqsave(&pmx->lock, flags);
305 pmx->nr_functions++;
306 spin_unlock_irqrestore(&pmx->lock, flags);
307 }
308}
309
Paul Mundtca5481c62012-07-10 12:08:14 +0900310/* pinmux ranges -> pinctrl pin descs */
311static int __devinit sh_pfc_map_gpios(struct sh_pfc *pfc,
312 struct sh_pfc_pinctrl *pmx)
313{
Paul Mundtd93a8912012-07-11 17:17:10 +0900314 unsigned long flags;
Paul Mundtca5481c62012-07-10 12:08:14 +0900315 int i;
316
317 pmx->nr_pads = pfc->last_gpio - pfc->first_gpio + 1;
318
319 pmx->pads = kmalloc(sizeof(struct pinctrl_pin_desc) * pmx->nr_pads,
320 GFP_KERNEL);
321 if (unlikely(!pmx->pads)) {
322 pmx->nr_pads = 0;
323 return -ENOMEM;
324 }
325
Paul Mundtd93a8912012-07-11 17:17:10 +0900326 spin_lock_irqsave(&pfc->lock, flags);
327
Paul Mundtca5481c62012-07-10 12:08:14 +0900328 /*
329 * We don't necessarily have a 1:1 mapping between pin and linux
330 * GPIO number, as the latter maps to the associated enum_id.
331 * Care needs to be taken to translate back to pin space when
332 * dealing with any pin configurations.
333 */
334 for (i = 0; i < pmx->nr_pads; i++) {
335 struct pinctrl_pin_desc *pin = pmx->pads + i;
336 struct pinmux_gpio *gpio = pfc->gpios + i;
337
338 pin->number = pfc->first_gpio + i;
339 pin->name = gpio->name;
Paul Mundtd93a8912012-07-11 17:17:10 +0900340
Paul Mundte3e79452012-07-17 15:23:11 +0900341 /* XXX */
342 if (unlikely(!gpio->enum_id))
343 continue;
344
Paul Mundtd93a8912012-07-11 17:17:10 +0900345 sh_pfc_map_one_gpio(pfc, pmx, gpio, i);
Paul Mundtca5481c62012-07-10 12:08:14 +0900346 }
347
Paul Mundtd93a8912012-07-11 17:17:10 +0900348 spin_unlock_irqrestore(&pfc->lock, flags);
349
Paul Mundtca5481c62012-07-10 12:08:14 +0900350 sh_pfc_pinctrl_desc.pins = pmx->pads;
351 sh_pfc_pinctrl_desc.npins = pmx->nr_pads;
352
353 return 0;
354}
355
Paul Mundtd93a8912012-07-11 17:17:10 +0900356static int __devinit sh_pfc_map_functions(struct sh_pfc *pfc,
357 struct sh_pfc_pinctrl *pmx)
358{
359 unsigned long flags;
360 int i, fn;
361
362 pmx->functions = kzalloc(pmx->nr_functions * sizeof(void *),
363 GFP_KERNEL);
364 if (unlikely(!pmx->functions))
365 return -ENOMEM;
366
367 spin_lock_irqsave(&pmx->lock, flags);
368
369 for (i = fn = 0; i < pmx->nr_pads; i++) {
370 struct pinmux_gpio *gpio = pfc->gpios + i;
371
372 if ((gpio->flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_FUNCTION)
373 pmx->functions[fn++] = gpio;
374 }
375
376 spin_unlock_irqrestore(&pmx->lock, flags);
377
378 return 0;
379}
380
Paul Mundtca5481c62012-07-10 12:08:14 +0900381static int __devinit sh_pfc_pinctrl_probe(struct platform_device *pdev)
382{
383 struct sh_pfc *pfc;
384 int ret;
385
386 if (unlikely(!sh_pfc_pmx))
387 return -ENODEV;
388
389 pfc = sh_pfc_pmx->pfc;
390
391 ret = sh_pfc_map_gpios(pfc, sh_pfc_pmx);
392 if (unlikely(ret != 0))
393 return ret;
394
Paul Mundtd93a8912012-07-11 17:17:10 +0900395 ret = sh_pfc_map_functions(pfc, sh_pfc_pmx);
396 if (unlikely(ret != 0))
397 goto free_pads;
398
Paul Mundtca5481c62012-07-10 12:08:14 +0900399 sh_pfc_pmx->pctl = pinctrl_register(&sh_pfc_pinctrl_desc, &pdev->dev,
400 sh_pfc_pmx);
401 if (IS_ERR(sh_pfc_pmx->pctl)) {
402 ret = PTR_ERR(sh_pfc_pmx->pctl);
Paul Mundtd93a8912012-07-11 17:17:10 +0900403 goto free_functions;
Paul Mundtca5481c62012-07-10 12:08:14 +0900404 }
405
406 sh_pfc_gpio_range.npins = pfc->last_gpio - pfc->first_gpio + 1;
407 sh_pfc_gpio_range.base = pfc->first_gpio;
408 sh_pfc_gpio_range.pin_base = pfc->first_gpio;
409
410 pinctrl_add_gpio_range(sh_pfc_pmx->pctl, &sh_pfc_gpio_range);
411
412 platform_set_drvdata(pdev, sh_pfc_pmx);
413
414 return 0;
415
Paul Mundtd93a8912012-07-11 17:17:10 +0900416free_functions:
417 kfree(sh_pfc_pmx->functions);
418free_pads:
Paul Mundtca5481c62012-07-10 12:08:14 +0900419 kfree(sh_pfc_pmx->pads);
420 kfree(sh_pfc_pmx);
Paul Mundtd93a8912012-07-11 17:17:10 +0900421
Paul Mundtca5481c62012-07-10 12:08:14 +0900422 return ret;
423}
424
425static int __devexit sh_pfc_pinctrl_remove(struct platform_device *pdev)
426{
427 struct sh_pfc_pinctrl *pmx = platform_get_drvdata(pdev);
428
429 pinctrl_remove_gpio_range(pmx->pctl, &sh_pfc_gpio_range);
430 pinctrl_unregister(pmx->pctl);
431
432 platform_set_drvdata(pdev, NULL);
433
Paul Mundtd93a8912012-07-11 17:17:10 +0900434 kfree(sh_pfc_pmx->functions);
Paul Mundtca5481c62012-07-10 12:08:14 +0900435 kfree(sh_pfc_pmx->pads);
436 kfree(sh_pfc_pmx);
437
438 return 0;
439}
440
441static struct platform_driver sh_pfc_pinctrl_driver = {
442 .probe = sh_pfc_pinctrl_probe,
443 .remove = __devexit_p(sh_pfc_pinctrl_remove),
444 .driver = {
445 .name = KBUILD_MODNAME,
446 .owner = THIS_MODULE,
447 },
448};
449
450static struct platform_device sh_pfc_pinctrl_device = {
451 .name = KBUILD_MODNAME,
452 .id = -1,
453};
454
455static int __init sh_pfc_pinctrl_init(void)
456{
457 int rc;
458
459 rc = platform_driver_register(&sh_pfc_pinctrl_driver);
460 if (likely(!rc)) {
461 rc = platform_device_register(&sh_pfc_pinctrl_device);
462 if (unlikely(rc))
463 platform_driver_unregister(&sh_pfc_pinctrl_driver);
464 }
465
466 return rc;
467}
468
469static void __exit sh_pfc_pinctrl_exit(void)
470{
471 platform_driver_unregister(&sh_pfc_pinctrl_driver);
472}
473
474subsys_initcall(sh_pfc_pinctrl_init);
475module_exit(sh_pfc_pinctrl_exit);