2 * SuperH Pin Function Controller pinmux support.
4 * Copyright (C) 2012 Paul Mundt
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
10 #define DRV_NAME "pinctrl-sh_pfc"
12 #define pr_fmt(fmt) DRV_NAME " " KBUILD_MODNAME ": " fmt
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/sh_pfc.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/platform_device.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinmux.h>
25 #include <linux/pinctrl/pinconf-generic.h>
27 struct sh_pfc_pinctrl
{
28 struct pinctrl_dev
*pctl
;
31 struct pinmux_gpio
**functions
;
32 unsigned int nr_functions
;
34 struct pinctrl_pin_desc
*pads
;
40 static struct sh_pfc_pinctrl
*sh_pfc_pmx
;
42 static int sh_pfc_get_groups_count(struct pinctrl_dev
*pctldev
)
44 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
49 static const char *sh_pfc_get_group_name(struct pinctrl_dev
*pctldev
,
52 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
54 return pmx
->pads
[selector
].name
;
57 static int sh_pfc_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned group
,
58 const unsigned **pins
, unsigned *num_pins
)
60 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
62 *pins
= &pmx
->pads
[group
].number
;
68 static void sh_pfc_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
71 seq_printf(s
, "%s", DRV_NAME
);
74 static struct pinctrl_ops sh_pfc_pinctrl_ops
= {
75 .get_groups_count
= sh_pfc_get_groups_count
,
76 .get_group_name
= sh_pfc_get_group_name
,
77 .get_group_pins
= sh_pfc_get_group_pins
,
78 .pin_dbg_show
= sh_pfc_pin_dbg_show
,
81 static int sh_pfc_get_functions_count(struct pinctrl_dev
*pctldev
)
83 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
85 return pmx
->nr_functions
;
88 static const char *sh_pfc_get_function_name(struct pinctrl_dev
*pctldev
,
91 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
93 return pmx
->functions
[selector
]->name
;
96 static int sh_pfc_get_function_groups(struct pinctrl_dev
*pctldev
, unsigned func
,
97 const char * const **groups
,
98 unsigned * const num_groups
)
100 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
102 *groups
= &pmx
->functions
[func
]->name
;
108 static int sh_pfc_noop_enable(struct pinctrl_dev
*pctldev
, unsigned func
,
114 static void sh_pfc_noop_disable(struct pinctrl_dev
*pctldev
, unsigned func
,
119 static inline int sh_pfc_config_function(struct sh_pfc
*pfc
, unsigned offset
)
121 if (sh_pfc_config_gpio(pfc
, offset
,
122 PINMUX_TYPE_FUNCTION
,
123 GPIO_CFG_DRYRUN
) != 0)
126 if (sh_pfc_config_gpio(pfc
, offset
,
127 PINMUX_TYPE_FUNCTION
,
134 static int sh_pfc_reconfig_pin(struct sh_pfc
*pfc
, unsigned offset
,
141 spin_lock_irqsave(&pfc
->lock
, flags
);
143 pinmux_type
= pfc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
146 * See if the present config needs to first be de-configured.
148 switch (pinmux_type
) {
149 case PINMUX_TYPE_GPIO
:
151 case PINMUX_TYPE_OUTPUT
:
152 case PINMUX_TYPE_INPUT
:
153 case PINMUX_TYPE_INPUT_PULLUP
:
154 case PINMUX_TYPE_INPUT_PULLDOWN
:
155 sh_pfc_config_gpio(pfc
, offset
, pinmux_type
, GPIO_CFG_FREE
);
164 if (sh_pfc_config_gpio(pfc
, offset
, new_type
,
165 GPIO_CFG_DRYRUN
) != 0)
171 if (sh_pfc_config_gpio(pfc
, offset
, new_type
,
175 pfc
->gpios
[offset
].flags
&= ~PINMUX_FLAG_TYPE
;
176 pfc
->gpios
[offset
].flags
|= new_type
;
181 spin_unlock_irqrestore(&pfc
->lock
, flags
);
187 static int sh_pfc_gpio_request_enable(struct pinctrl_dev
*pctldev
,
188 struct pinctrl_gpio_range
*range
,
191 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
192 struct sh_pfc
*pfc
= pmx
->pfc
;
194 int ret
, pinmux_type
;
196 spin_lock_irqsave(&pfc
->lock
, flags
);
198 pinmux_type
= pfc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
200 switch (pinmux_type
) {
201 case PINMUX_TYPE_FUNCTION
:
202 pr_notice_once("Use of GPIO API for function requests is "
203 "deprecated, convert to pinctrl\n");
205 ret
= sh_pfc_config_function(pfc
, offset
);
206 if (unlikely(ret
< 0))
210 case PINMUX_TYPE_GPIO
:
213 pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type
);
221 spin_unlock_irqrestore(&pfc
->lock
, flags
);
226 static void sh_pfc_gpio_disable_free(struct pinctrl_dev
*pctldev
,
227 struct pinctrl_gpio_range
*range
,
230 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
231 struct sh_pfc
*pfc
= pmx
->pfc
;
235 spin_lock_irqsave(&pfc
->lock
, flags
);
237 pinmux_type
= pfc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
239 sh_pfc_config_gpio(pfc
, offset
, pinmux_type
, GPIO_CFG_FREE
);
241 spin_unlock_irqrestore(&pfc
->lock
, flags
);
244 static int sh_pfc_gpio_set_direction(struct pinctrl_dev
*pctldev
,
245 struct pinctrl_gpio_range
*range
,
246 unsigned offset
, bool input
)
248 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
249 int type
= input
? PINMUX_TYPE_INPUT
: PINMUX_TYPE_OUTPUT
;
251 return sh_pfc_reconfig_pin(pmx
->pfc
, offset
, type
);
254 static struct pinmux_ops sh_pfc_pinmux_ops
= {
255 .get_functions_count
= sh_pfc_get_functions_count
,
256 .get_function_name
= sh_pfc_get_function_name
,
257 .get_function_groups
= sh_pfc_get_function_groups
,
258 .enable
= sh_pfc_noop_enable
,
259 .disable
= sh_pfc_noop_disable
,
260 .gpio_request_enable
= sh_pfc_gpio_request_enable
,
261 .gpio_disable_free
= sh_pfc_gpio_disable_free
,
262 .gpio_set_direction
= sh_pfc_gpio_set_direction
,
265 static int sh_pfc_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
266 unsigned long *config
)
268 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
269 struct sh_pfc
*pfc
= pmx
->pfc
;
271 *config
= pfc
->gpios
[pin
].flags
& PINMUX_FLAG_TYPE
;
276 static int sh_pfc_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
277 unsigned long config
)
279 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
281 /* Validate the new type */
282 if (config
>= PINMUX_FLAG_TYPE
)
285 return sh_pfc_reconfig_pin(pmx
->pfc
, pin
, config
);
288 static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
289 struct seq_file
*s
, unsigned pin
)
291 const char *pinmux_type_str
[] = {
292 [PINMUX_TYPE_NONE
] = "none",
293 [PINMUX_TYPE_FUNCTION
] = "function",
294 [PINMUX_TYPE_GPIO
] = "gpio",
295 [PINMUX_TYPE_OUTPUT
] = "output",
296 [PINMUX_TYPE_INPUT
] = "input",
297 [PINMUX_TYPE_INPUT_PULLUP
] = "input bias pull up",
298 [PINMUX_TYPE_INPUT_PULLDOWN
] = "input bias pull down",
300 unsigned long config
;
303 rc
= sh_pfc_pinconf_get(pctldev
, pin
, &config
);
304 if (unlikely(rc
!= 0))
307 seq_printf(s
, " %s", pinmux_type_str
[config
]);
310 static struct pinconf_ops sh_pfc_pinconf_ops
= {
311 .pin_config_get
= sh_pfc_pinconf_get
,
312 .pin_config_set
= sh_pfc_pinconf_set
,
313 .pin_config_dbg_show
= sh_pfc_pinconf_dbg_show
,
316 static struct pinctrl_gpio_range sh_pfc_gpio_range
= {
321 static struct pinctrl_desc sh_pfc_pinctrl_desc
= {
323 .owner
= THIS_MODULE
,
324 .pctlops
= &sh_pfc_pinctrl_ops
,
325 .pmxops
= &sh_pfc_pinmux_ops
,
326 .confops
= &sh_pfc_pinconf_ops
,
329 static inline void __devinit
sh_pfc_map_one_gpio(struct sh_pfc
*pfc
,
330 struct sh_pfc_pinctrl
*pmx
,
331 struct pinmux_gpio
*gpio
,
334 struct pinmux_data_reg
*dummy
;
338 gpio
->flags
&= ~PINMUX_FLAG_TYPE
;
340 if (sh_pfc_get_data_reg(pfc
, offset
, &dummy
, &bit
) == 0)
341 gpio
->flags
|= PINMUX_TYPE_GPIO
;
343 gpio
->flags
|= PINMUX_TYPE_FUNCTION
;
345 spin_lock_irqsave(&pmx
->lock
, flags
);
347 spin_unlock_irqrestore(&pmx
->lock
, flags
);
351 /* pinmux ranges -> pinctrl pin descs */
352 static int __devinit
sh_pfc_map_gpios(struct sh_pfc
*pfc
,
353 struct sh_pfc_pinctrl
*pmx
)
358 pmx
->nr_pads
= pfc
->last_gpio
- pfc
->first_gpio
+ 1;
360 pmx
->pads
= kmalloc(sizeof(struct pinctrl_pin_desc
) * pmx
->nr_pads
,
362 if (unlikely(!pmx
->pads
)) {
367 spin_lock_irqsave(&pfc
->lock
, flags
);
370 * We don't necessarily have a 1:1 mapping between pin and linux
371 * GPIO number, as the latter maps to the associated enum_id.
372 * Care needs to be taken to translate back to pin space when
373 * dealing with any pin configurations.
375 for (i
= 0; i
< pmx
->nr_pads
; i
++) {
376 struct pinctrl_pin_desc
*pin
= pmx
->pads
+ i
;
377 struct pinmux_gpio
*gpio
= pfc
->gpios
+ i
;
379 pin
->number
= pfc
->first_gpio
+ i
;
380 pin
->name
= gpio
->name
;
383 if (unlikely(!gpio
->enum_id
))
386 sh_pfc_map_one_gpio(pfc
, pmx
, gpio
, i
);
389 spin_unlock_irqrestore(&pfc
->lock
, flags
);
391 sh_pfc_pinctrl_desc
.pins
= pmx
->pads
;
392 sh_pfc_pinctrl_desc
.npins
= pmx
->nr_pads
;
397 static int __devinit
sh_pfc_map_functions(struct sh_pfc
*pfc
,
398 struct sh_pfc_pinctrl
*pmx
)
403 pmx
->functions
= kzalloc(pmx
->nr_functions
* sizeof(void *),
405 if (unlikely(!pmx
->functions
))
408 spin_lock_irqsave(&pmx
->lock
, flags
);
410 for (i
= fn
= 0; i
< pmx
->nr_pads
; i
++) {
411 struct pinmux_gpio
*gpio
= pfc
->gpios
+ i
;
413 if ((gpio
->flags
& PINMUX_FLAG_TYPE
) == PINMUX_TYPE_FUNCTION
)
414 pmx
->functions
[fn
++] = gpio
;
417 spin_unlock_irqrestore(&pmx
->lock
, flags
);
422 static int __devinit
sh_pfc_pinctrl_probe(struct platform_device
*pdev
)
427 if (unlikely(!sh_pfc_pmx
))
430 pfc
= sh_pfc_pmx
->pfc
;
432 ret
= sh_pfc_map_gpios(pfc
, sh_pfc_pmx
);
433 if (unlikely(ret
!= 0))
436 ret
= sh_pfc_map_functions(pfc
, sh_pfc_pmx
);
437 if (unlikely(ret
!= 0))
440 sh_pfc_pmx
->pctl
= pinctrl_register(&sh_pfc_pinctrl_desc
, &pdev
->dev
,
442 if (IS_ERR(sh_pfc_pmx
->pctl
)) {
443 ret
= PTR_ERR(sh_pfc_pmx
->pctl
);
447 sh_pfc_gpio_range
.npins
= pfc
->last_gpio
- pfc
->first_gpio
+ 1;
448 sh_pfc_gpio_range
.base
= pfc
->first_gpio
;
449 sh_pfc_gpio_range
.pin_base
= pfc
->first_gpio
;
451 pinctrl_add_gpio_range(sh_pfc_pmx
->pctl
, &sh_pfc_gpio_range
);
453 platform_set_drvdata(pdev
, sh_pfc_pmx
);
458 kfree(sh_pfc_pmx
->functions
);
460 kfree(sh_pfc_pmx
->pads
);
466 static int __devexit
sh_pfc_pinctrl_remove(struct platform_device
*pdev
)
468 struct sh_pfc_pinctrl
*pmx
= platform_get_drvdata(pdev
);
470 pinctrl_unregister(pmx
->pctl
);
472 platform_set_drvdata(pdev
, NULL
);
474 kfree(sh_pfc_pmx
->functions
);
475 kfree(sh_pfc_pmx
->pads
);
481 static struct platform_driver sh_pfc_pinctrl_driver
= {
482 .probe
= sh_pfc_pinctrl_probe
,
483 .remove
= __devexit_p(sh_pfc_pinctrl_remove
),
486 .owner
= THIS_MODULE
,
490 static struct platform_device sh_pfc_pinctrl_device
= {
495 static int sh_pfc_pinctrl_init(void)
499 rc
= platform_driver_register(&sh_pfc_pinctrl_driver
);
501 rc
= platform_device_register(&sh_pfc_pinctrl_device
);
503 platform_driver_unregister(&sh_pfc_pinctrl_driver
);
509 int sh_pfc_register_pinctrl(struct sh_pfc
*pfc
)
511 sh_pfc_pmx
= kzalloc(sizeof(struct sh_pfc_pinctrl
), GFP_KERNEL
);
512 if (unlikely(!sh_pfc_pmx
))
515 spin_lock_init(&sh_pfc_pmx
->lock
);
517 sh_pfc_pmx
->pfc
= pfc
;
519 return sh_pfc_pinctrl_init();
521 EXPORT_SYMBOL_GPL(sh_pfc_register_pinctrl
);
523 static void __exit
sh_pfc_pinctrl_exit(void)
525 platform_driver_unregister(&sh_pfc_pinctrl_driver
);
527 module_exit(sh_pfc_pinctrl_exit
);