blob: 45e80ee707662c55dbd306b671b584ce2628c046 [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
Paul Mundte3f805e2012-07-17 15:48:18 +090040static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
Paul Mundtca5481c62012-07-10 12:08:14 +090041{
Paul Mundte3f805e2012-07-17 15:48:18 +090042 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
43
44 return pmx->nr_pads;
Paul Mundtca5481c62012-07-10 12:08:14 +090045}
46
Paul Mundte3f805e2012-07-17 15:48:18 +090047static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
Paul Mundtca5481c62012-07-10 12:08:14 +090048 unsigned selector)
49{
Paul Mundte3f805e2012-07-17 15:48:18 +090050 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
51
52 return pmx->pads[selector].name;
Paul Mundtca5481c62012-07-10 12:08:14 +090053}
54
55static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
56 const unsigned **pins, unsigned *num_pins)
57{
Paul Mundte3f805e2012-07-17 15:48:18 +090058 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
59
60 *pins = &pmx->pads[group].number;
61 *num_pins = 1;
62
63 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +090064}
65
66static struct pinctrl_ops sh_pfc_pinctrl_ops = {
Paul Mundte3f805e2012-07-17 15:48:18 +090067 .get_groups_count = sh_pfc_get_groups_count,
68 .get_group_name = sh_pfc_get_group_name,
Paul Mundtca5481c62012-07-10 12:08:14 +090069 .get_group_pins = sh_pfc_get_group_pins,
70};
71
Paul Mundtd93a8912012-07-11 17:17:10 +090072static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
73{
74 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtca5481c62012-07-10 12:08:14 +090075
Paul Mundtd93a8912012-07-11 17:17:10 +090076 return pmx->nr_functions;
77}
78
79static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
80 unsigned selector)
81{
82 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
83
84 return pmx->functions[selector]->name;
85}
86
Paul Mundtca5481c62012-07-10 12:08:14 +090087static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, unsigned func,
88 const char * const **groups,
89 unsigned * const num_groups)
90{
Paul Mundtd93a8912012-07-11 17:17:10 +090091 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
92
93 *groups = &pmx->functions[func]->name;
94 *num_groups = 1;
95
Paul Mundtca5481c62012-07-10 12:08:14 +090096 return 0;
97}
98
99static int sh_pfc_noop_enable(struct pinctrl_dev *pctldev, unsigned func,
100 unsigned group)
101{
102 return 0;
103}
104
105static void sh_pfc_noop_disable(struct pinctrl_dev *pctldev, unsigned func,
106 unsigned group)
107{
108}
109
Paul Mundtd93a8912012-07-11 17:17:10 +0900110static inline int sh_pfc_config_function(struct sh_pfc *pfc, unsigned offset)
111{
112 if (sh_pfc_config_gpio(pfc, offset,
113 PINMUX_TYPE_FUNCTION,
114 GPIO_CFG_DRYRUN) != 0)
115 return -EINVAL;
116
117 if (sh_pfc_config_gpio(pfc, offset,
118 PINMUX_TYPE_FUNCTION,
119 GPIO_CFG_REQ) != 0)
120 return -EINVAL;
121
122 return 0;
123}
124
Paul Mundtca5481c62012-07-10 12:08:14 +0900125static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
126 struct pinctrl_gpio_range *range,
127 unsigned offset)
128{
129 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
130 struct sh_pfc *pfc = pmx->pfc;
Paul Mundtca5481c62012-07-10 12:08:14 +0900131 unsigned long flags;
Paul Mundtd93a8912012-07-11 17:17:10 +0900132 int ret, pinmux_type;
Paul Mundtca5481c62012-07-10 12:08:14 +0900133
134 spin_lock_irqsave(&pfc->lock, flags);
135
Paul Mundtd93a8912012-07-11 17:17:10 +0900136 pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
Paul Mundtca5481c62012-07-10 12:08:14 +0900137
Paul Mundtd93a8912012-07-11 17:17:10 +0900138 switch (pinmux_type) {
139 case PINMUX_TYPE_FUNCTION:
140 pr_notice_once("Use of GPIO API for function requests is "
141 "deprecated, convert to pinctrl\n");
142 /* handle for now */
143 ret = sh_pfc_config_function(pfc, offset);
144 if (unlikely(ret < 0))
Paul Mundtca5481c62012-07-10 12:08:14 +0900145 goto err;
146
Paul Mundtd93a8912012-07-11 17:17:10 +0900147 break;
148 case PINMUX_TYPE_GPIO:
149 break;
150 default:
151 pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type);
152 return -ENOTSUPP;
153 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900154
155 ret = 0;
156
157err:
158 spin_unlock_irqrestore(&pfc->lock, flags);
159
160 return ret;
161}
162
163static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
164 struct pinctrl_gpio_range *range,
165 unsigned offset)
166{
167 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
168 struct sh_pfc *pfc = pmx->pfc;
169 unsigned long flags;
170 int pinmux_type;
171
172 spin_lock_irqsave(&pfc->lock, flags);
173
174 pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
175
176 sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
177
Paul Mundtca5481c62012-07-10 12:08:14 +0900178 spin_unlock_irqrestore(&pfc->lock, flags);
179}
180
181static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
182 struct pinctrl_gpio_range *range,
183 unsigned offset, bool input)
184{
185 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
186 struct sh_pfc *pfc = pmx->pfc;
187 unsigned long flags;
188 int pinmux_type, new_pinmux_type;
189 int ret = -EINVAL;
190
191 new_pinmux_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
192
193 spin_lock_irqsave(&pfc->lock, flags);
194
195 pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
196
197 switch (pinmux_type) {
198 case PINMUX_TYPE_GPIO:
199 break;
200 case PINMUX_TYPE_OUTPUT:
201 case PINMUX_TYPE_INPUT:
202 case PINMUX_TYPE_INPUT_PULLUP:
203 case PINMUX_TYPE_INPUT_PULLDOWN:
204 sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
205 break;
206 default:
207 goto err;
208 }
209
210 if (sh_pfc_config_gpio(pfc, offset,
211 new_pinmux_type,
212 GPIO_CFG_DRYRUN) != 0)
213 goto err;
214
215 if (sh_pfc_config_gpio(pfc, offset,
216 new_pinmux_type,
217 GPIO_CFG_REQ) != 0)
218 BUG();
219
220 pfc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
221 pfc->gpios[offset].flags |= new_pinmux_type;
222
223 ret = 0;
224
225err:
226 spin_unlock_irqrestore(&pfc->lock, flags);
227
228 return ret;
229}
230
231static struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900232 .get_functions_count = sh_pfc_get_functions_count,
233 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900234 .get_function_groups = sh_pfc_get_function_groups,
235 .enable = sh_pfc_noop_enable,
236 .disable = sh_pfc_noop_disable,
237 .gpio_request_enable = sh_pfc_gpio_request_enable,
238 .gpio_disable_free = sh_pfc_gpio_disable_free,
239 .gpio_set_direction = sh_pfc_gpio_set_direction,
240};
241
242static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
243 unsigned long *config)
244{
Paul Mundtd93a8912012-07-11 17:17:10 +0900245 enum pin_config_param param = (enum pin_config_param)(*config);
246
247 switch (param) {
248 default:
249 break;
250 }
251
Paul Mundtca5481c62012-07-10 12:08:14 +0900252 return -ENOTSUPP;
253}
254
255static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
256 unsigned long config)
257{
258 return -EINVAL;
259}
260
261static struct pinconf_ops sh_pfc_pinconf_ops = {
262 .is_generic = true,
263 .pin_config_get = sh_pfc_pinconf_get,
264 .pin_config_set = sh_pfc_pinconf_set,
265};
266
267static struct pinctrl_gpio_range sh_pfc_gpio_range = {
268 .name = KBUILD_MODNAME,
269 .id = 0,
270};
271
272static struct pinctrl_desc sh_pfc_pinctrl_desc = {
273 .name = KBUILD_MODNAME,
274 .owner = THIS_MODULE,
275 .pctlops = &sh_pfc_pinctrl_ops,
276 .pmxops = &sh_pfc_pinmux_ops,
277 .confops = &sh_pfc_pinconf_ops,
278};
279
280int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
281{
Paul Mundtd93a8912012-07-11 17:17:10 +0900282 sh_pfc_pmx = kzalloc(sizeof(struct sh_pfc_pinctrl), GFP_KERNEL);
Paul Mundtca5481c62012-07-10 12:08:14 +0900283 if (unlikely(!sh_pfc_pmx))
284 return -ENOMEM;
285
Paul Mundtd93a8912012-07-11 17:17:10 +0900286 spin_lock_init(&sh_pfc_pmx->lock);
287
Paul Mundtca5481c62012-07-10 12:08:14 +0900288 sh_pfc_pmx->pfc = pfc;
289
290 return 0;
291}
Paul Mundt1acbbb4ed2012-07-17 15:21:47 +0900292EXPORT_SYMBOL_GPL(sh_pfc_register_pinctrl);
Paul Mundtca5481c62012-07-10 12:08:14 +0900293
Paul Mundtd93a8912012-07-11 17:17:10 +0900294static inline void __devinit sh_pfc_map_one_gpio(struct sh_pfc *pfc,
295 struct sh_pfc_pinctrl *pmx,
296 struct pinmux_gpio *gpio,
297 unsigned offset)
298{
299 struct pinmux_data_reg *dummy;
300 unsigned long flags;
301 int bit;
302
303 gpio->flags &= ~PINMUX_FLAG_TYPE;
304
305 if (sh_pfc_get_data_reg(pfc, offset, &dummy, &bit) == 0)
306 gpio->flags |= PINMUX_TYPE_GPIO;
307 else {
308 gpio->flags |= PINMUX_TYPE_FUNCTION;
309
310 spin_lock_irqsave(&pmx->lock, flags);
311 pmx->nr_functions++;
312 spin_unlock_irqrestore(&pmx->lock, flags);
313 }
314}
315
Paul Mundtca5481c62012-07-10 12:08:14 +0900316/* pinmux ranges -> pinctrl pin descs */
317static int __devinit sh_pfc_map_gpios(struct sh_pfc *pfc,
318 struct sh_pfc_pinctrl *pmx)
319{
Paul Mundtd93a8912012-07-11 17:17:10 +0900320 unsigned long flags;
Paul Mundtca5481c62012-07-10 12:08:14 +0900321 int i;
322
323 pmx->nr_pads = pfc->last_gpio - pfc->first_gpio + 1;
324
325 pmx->pads = kmalloc(sizeof(struct pinctrl_pin_desc) * pmx->nr_pads,
326 GFP_KERNEL);
327 if (unlikely(!pmx->pads)) {
328 pmx->nr_pads = 0;
329 return -ENOMEM;
330 }
331
Paul Mundtd93a8912012-07-11 17:17:10 +0900332 spin_lock_irqsave(&pfc->lock, flags);
333
Paul Mundtca5481c62012-07-10 12:08:14 +0900334 /*
335 * We don't necessarily have a 1:1 mapping between pin and linux
336 * GPIO number, as the latter maps to the associated enum_id.
337 * Care needs to be taken to translate back to pin space when
338 * dealing with any pin configurations.
339 */
340 for (i = 0; i < pmx->nr_pads; i++) {
341 struct pinctrl_pin_desc *pin = pmx->pads + i;
342 struct pinmux_gpio *gpio = pfc->gpios + i;
343
344 pin->number = pfc->first_gpio + i;
345 pin->name = gpio->name;
Paul Mundtd93a8912012-07-11 17:17:10 +0900346
Paul Mundte3e79452012-07-17 15:23:11 +0900347 /* XXX */
348 if (unlikely(!gpio->enum_id))
349 continue;
350
Paul Mundtd93a8912012-07-11 17:17:10 +0900351 sh_pfc_map_one_gpio(pfc, pmx, gpio, i);
Paul Mundtca5481c62012-07-10 12:08:14 +0900352 }
353
Paul Mundtd93a8912012-07-11 17:17:10 +0900354 spin_unlock_irqrestore(&pfc->lock, flags);
355
Paul Mundtca5481c62012-07-10 12:08:14 +0900356 sh_pfc_pinctrl_desc.pins = pmx->pads;
357 sh_pfc_pinctrl_desc.npins = pmx->nr_pads;
358
359 return 0;
360}
361
Paul Mundtd93a8912012-07-11 17:17:10 +0900362static int __devinit sh_pfc_map_functions(struct sh_pfc *pfc,
363 struct sh_pfc_pinctrl *pmx)
364{
365 unsigned long flags;
366 int i, fn;
367
368 pmx->functions = kzalloc(pmx->nr_functions * sizeof(void *),
369 GFP_KERNEL);
370 if (unlikely(!pmx->functions))
371 return -ENOMEM;
372
373 spin_lock_irqsave(&pmx->lock, flags);
374
375 for (i = fn = 0; i < pmx->nr_pads; i++) {
376 struct pinmux_gpio *gpio = pfc->gpios + i;
377
378 if ((gpio->flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_FUNCTION)
379 pmx->functions[fn++] = gpio;
380 }
381
382 spin_unlock_irqrestore(&pmx->lock, flags);
383
384 return 0;
385}
386
Paul Mundtca5481c62012-07-10 12:08:14 +0900387static int __devinit sh_pfc_pinctrl_probe(struct platform_device *pdev)
388{
389 struct sh_pfc *pfc;
390 int ret;
391
392 if (unlikely(!sh_pfc_pmx))
393 return -ENODEV;
394
395 pfc = sh_pfc_pmx->pfc;
396
397 ret = sh_pfc_map_gpios(pfc, sh_pfc_pmx);
398 if (unlikely(ret != 0))
399 return ret;
400
Paul Mundtd93a8912012-07-11 17:17:10 +0900401 ret = sh_pfc_map_functions(pfc, sh_pfc_pmx);
402 if (unlikely(ret != 0))
403 goto free_pads;
404
Paul Mundtca5481c62012-07-10 12:08:14 +0900405 sh_pfc_pmx->pctl = pinctrl_register(&sh_pfc_pinctrl_desc, &pdev->dev,
406 sh_pfc_pmx);
407 if (IS_ERR(sh_pfc_pmx->pctl)) {
408 ret = PTR_ERR(sh_pfc_pmx->pctl);
Paul Mundtd93a8912012-07-11 17:17:10 +0900409 goto free_functions;
Paul Mundtca5481c62012-07-10 12:08:14 +0900410 }
411
412 sh_pfc_gpio_range.npins = pfc->last_gpio - pfc->first_gpio + 1;
413 sh_pfc_gpio_range.base = pfc->first_gpio;
414 sh_pfc_gpio_range.pin_base = pfc->first_gpio;
415
416 pinctrl_add_gpio_range(sh_pfc_pmx->pctl, &sh_pfc_gpio_range);
417
418 platform_set_drvdata(pdev, sh_pfc_pmx);
419
420 return 0;
421
Paul Mundtd93a8912012-07-11 17:17:10 +0900422free_functions:
423 kfree(sh_pfc_pmx->functions);
424free_pads:
Paul Mundtca5481c62012-07-10 12:08:14 +0900425 kfree(sh_pfc_pmx->pads);
426 kfree(sh_pfc_pmx);
Paul Mundtd93a8912012-07-11 17:17:10 +0900427
Paul Mundtca5481c62012-07-10 12:08:14 +0900428 return ret;
429}
430
431static int __devexit sh_pfc_pinctrl_remove(struct platform_device *pdev)
432{
433 struct sh_pfc_pinctrl *pmx = platform_get_drvdata(pdev);
434
435 pinctrl_remove_gpio_range(pmx->pctl, &sh_pfc_gpio_range);
436 pinctrl_unregister(pmx->pctl);
437
438 platform_set_drvdata(pdev, NULL);
439
Paul Mundtd93a8912012-07-11 17:17:10 +0900440 kfree(sh_pfc_pmx->functions);
Paul Mundtca5481c62012-07-10 12:08:14 +0900441 kfree(sh_pfc_pmx->pads);
442 kfree(sh_pfc_pmx);
443
444 return 0;
445}
446
447static struct platform_driver sh_pfc_pinctrl_driver = {
448 .probe = sh_pfc_pinctrl_probe,
449 .remove = __devexit_p(sh_pfc_pinctrl_remove),
450 .driver = {
451 .name = KBUILD_MODNAME,
452 .owner = THIS_MODULE,
453 },
454};
455
456static struct platform_device sh_pfc_pinctrl_device = {
457 .name = KBUILD_MODNAME,
458 .id = -1,
459};
460
461static int __init sh_pfc_pinctrl_init(void)
462{
463 int rc;
464
465 rc = platform_driver_register(&sh_pfc_pinctrl_driver);
466 if (likely(!rc)) {
467 rc = platform_device_register(&sh_pfc_pinctrl_device);
468 if (unlikely(rc))
469 platform_driver_unregister(&sh_pfc_pinctrl_driver);
470 }
471
472 return rc;
473}
474
475static void __exit sh_pfc_pinctrl_exit(void)
476{
477 platform_driver_unregister(&sh_pfc_pinctrl_driver);
478}
479
480subsys_initcall(sh_pfc_pinctrl_init);
481module_exit(sh_pfc_pinctrl_exit);