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 pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt
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>
17 #include <linux/spinlock.h>
18 #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>
25 struct sh_pfc_pinctrl
{
26 struct pinctrl_dev
*pctl
;
29 struct pinmux_gpio
**functions
;
30 unsigned int nr_functions
;
32 struct pinctrl_pin_desc
*pads
;
38 static struct sh_pfc_pinctrl
*sh_pfc_pmx
;
41 * No group support yet
43 static int sh_pfc_get_noop_count(struct pinctrl_dev
*pctldev
)
48 static const char *sh_pfc_get_noop_name(struct pinctrl_dev
*pctldev
,
54 static int sh_pfc_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned group
,
55 const unsigned **pins
, unsigned *num_pins
)
60 static 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
,
66 static int sh_pfc_get_functions_count(struct pinctrl_dev
*pctldev
)
68 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
70 return pmx
->nr_functions
;
73 static const char *sh_pfc_get_function_name(struct pinctrl_dev
*pctldev
,
76 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
78 return pmx
->functions
[selector
]->name
;
81 static int sh_pfc_get_function_groups(struct pinctrl_dev
*pctldev
, unsigned func
,
82 const char * const **groups
,
83 unsigned * const num_groups
)
85 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
87 *groups
= &pmx
->functions
[func
]->name
;
93 static int sh_pfc_noop_enable(struct pinctrl_dev
*pctldev
, unsigned func
,
99 static void sh_pfc_noop_disable(struct pinctrl_dev
*pctldev
, unsigned func
,
104 static inline int sh_pfc_config_function(struct sh_pfc
*pfc
, unsigned offset
)
106 if (sh_pfc_config_gpio(pfc
, offset
,
107 PINMUX_TYPE_FUNCTION
,
108 GPIO_CFG_DRYRUN
) != 0)
111 if (sh_pfc_config_gpio(pfc
, offset
,
112 PINMUX_TYPE_FUNCTION
,
119 static int sh_pfc_gpio_request_enable(struct pinctrl_dev
*pctldev
,
120 struct pinctrl_gpio_range
*range
,
123 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
124 struct sh_pfc
*pfc
= pmx
->pfc
;
126 int ret
, pinmux_type
;
128 spin_lock_irqsave(&pfc
->lock
, flags
);
130 pinmux_type
= pfc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
132 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");
137 ret
= sh_pfc_config_function(pfc
, offset
);
138 if (unlikely(ret
< 0))
142 case PINMUX_TYPE_GPIO
:
145 pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type
);
152 spin_unlock_irqrestore(&pfc
->lock
, flags
);
157 static void sh_pfc_gpio_disable_free(struct pinctrl_dev
*pctldev
,
158 struct pinctrl_gpio_range
*range
,
161 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
162 struct sh_pfc
*pfc
= pmx
->pfc
;
166 spin_lock_irqsave(&pfc
->lock
, flags
);
168 pinmux_type
= pfc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
170 sh_pfc_config_gpio(pfc
, offset
, pinmux_type
, GPIO_CFG_FREE
);
172 spin_unlock_irqrestore(&pfc
->lock
, flags
);
175 static int sh_pfc_gpio_set_direction(struct pinctrl_dev
*pctldev
,
176 struct pinctrl_gpio_range
*range
,
177 unsigned offset
, bool input
)
179 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
180 struct sh_pfc
*pfc
= pmx
->pfc
;
182 int pinmux_type
, new_pinmux_type
;
185 new_pinmux_type
= input
? PINMUX_TYPE_INPUT
: PINMUX_TYPE_OUTPUT
;
187 spin_lock_irqsave(&pfc
->lock
, flags
);
189 pinmux_type
= pfc
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
191 switch (pinmux_type
) {
192 case PINMUX_TYPE_GPIO
:
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
);
204 if (sh_pfc_config_gpio(pfc
, offset
,
206 GPIO_CFG_DRYRUN
) != 0)
209 if (sh_pfc_config_gpio(pfc
, offset
,
214 pfc
->gpios
[offset
].flags
&= ~PINMUX_FLAG_TYPE
;
215 pfc
->gpios
[offset
].flags
|= new_pinmux_type
;
220 spin_unlock_irqrestore(&pfc
->lock
, flags
);
225 static struct pinmux_ops sh_pfc_pinmux_ops
= {
226 .get_functions_count
= sh_pfc_get_functions_count
,
227 .get_function_name
= sh_pfc_get_function_name
,
228 .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
,
236 static int sh_pfc_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
237 unsigned long *config
)
239 enum pin_config_param param
= (enum pin_config_param
)(*config
);
249 static int sh_pfc_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
250 unsigned long config
)
255 static struct pinconf_ops sh_pfc_pinconf_ops
= {
257 .pin_config_get
= sh_pfc_pinconf_get
,
258 .pin_config_set
= sh_pfc_pinconf_set
,
261 static struct pinctrl_gpio_range sh_pfc_gpio_range
= {
262 .name
= KBUILD_MODNAME
,
266 static 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
,
274 int sh_pfc_register_pinctrl(struct sh_pfc
*pfc
)
276 sh_pfc_pmx
= kzalloc(sizeof(struct sh_pfc_pinctrl
), GFP_KERNEL
);
277 if (unlikely(!sh_pfc_pmx
))
280 spin_lock_init(&sh_pfc_pmx
->lock
);
282 sh_pfc_pmx
->pfc
= pfc
;
286 EXPORT_SYMBOL_GPL(sh_pfc_register_pinctrl
);
288 static inline void __devinit
sh_pfc_map_one_gpio(struct sh_pfc
*pfc
,
289 struct sh_pfc_pinctrl
*pmx
,
290 struct pinmux_gpio
*gpio
,
293 struct pinmux_data_reg
*dummy
;
297 gpio
->flags
&= ~PINMUX_FLAG_TYPE
;
299 if (sh_pfc_get_data_reg(pfc
, offset
, &dummy
, &bit
) == 0)
300 gpio
->flags
|= PINMUX_TYPE_GPIO
;
302 gpio
->flags
|= PINMUX_TYPE_FUNCTION
;
304 spin_lock_irqsave(&pmx
->lock
, flags
);
306 spin_unlock_irqrestore(&pmx
->lock
, flags
);
310 /* pinmux ranges -> pinctrl pin descs */
311 static int __devinit
sh_pfc_map_gpios(struct sh_pfc
*pfc
,
312 struct sh_pfc_pinctrl
*pmx
)
317 pmx
->nr_pads
= pfc
->last_gpio
- pfc
->first_gpio
+ 1;
319 pmx
->pads
= kmalloc(sizeof(struct pinctrl_pin_desc
) * pmx
->nr_pads
,
321 if (unlikely(!pmx
->pads
)) {
326 spin_lock_irqsave(&pfc
->lock
, flags
);
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.
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
;
338 pin
->number
= pfc
->first_gpio
+ i
;
339 pin
->name
= gpio
->name
;
342 if (unlikely(!gpio
->enum_id
))
345 sh_pfc_map_one_gpio(pfc
, pmx
, gpio
, i
);
348 spin_unlock_irqrestore(&pfc
->lock
, flags
);
350 sh_pfc_pinctrl_desc
.pins
= pmx
->pads
;
351 sh_pfc_pinctrl_desc
.npins
= pmx
->nr_pads
;
356 static int __devinit
sh_pfc_map_functions(struct sh_pfc
*pfc
,
357 struct sh_pfc_pinctrl
*pmx
)
362 pmx
->functions
= kzalloc(pmx
->nr_functions
* sizeof(void *),
364 if (unlikely(!pmx
->functions
))
367 spin_lock_irqsave(&pmx
->lock
, flags
);
369 for (i
= fn
= 0; i
< pmx
->nr_pads
; i
++) {
370 struct pinmux_gpio
*gpio
= pfc
->gpios
+ i
;
372 if ((gpio
->flags
& PINMUX_FLAG_TYPE
) == PINMUX_TYPE_FUNCTION
)
373 pmx
->functions
[fn
++] = gpio
;
376 spin_unlock_irqrestore(&pmx
->lock
, flags
);
381 static int __devinit
sh_pfc_pinctrl_probe(struct platform_device
*pdev
)
386 if (unlikely(!sh_pfc_pmx
))
389 pfc
= sh_pfc_pmx
->pfc
;
391 ret
= sh_pfc_map_gpios(pfc
, sh_pfc_pmx
);
392 if (unlikely(ret
!= 0))
395 ret
= sh_pfc_map_functions(pfc
, sh_pfc_pmx
);
396 if (unlikely(ret
!= 0))
399 sh_pfc_pmx
->pctl
= pinctrl_register(&sh_pfc_pinctrl_desc
, &pdev
->dev
,
401 if (IS_ERR(sh_pfc_pmx
->pctl
)) {
402 ret
= PTR_ERR(sh_pfc_pmx
->pctl
);
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
;
410 pinctrl_add_gpio_range(sh_pfc_pmx
->pctl
, &sh_pfc_gpio_range
);
412 platform_set_drvdata(pdev
, sh_pfc_pmx
);
417 kfree(sh_pfc_pmx
->functions
);
419 kfree(sh_pfc_pmx
->pads
);
425 static int __devexit
sh_pfc_pinctrl_remove(struct platform_device
*pdev
)
427 struct sh_pfc_pinctrl
*pmx
= platform_get_drvdata(pdev
);
429 pinctrl_remove_gpio_range(pmx
->pctl
, &sh_pfc_gpio_range
);
430 pinctrl_unregister(pmx
->pctl
);
432 platform_set_drvdata(pdev
, NULL
);
434 kfree(sh_pfc_pmx
->functions
);
435 kfree(sh_pfc_pmx
->pads
);
441 static struct platform_driver sh_pfc_pinctrl_driver
= {
442 .probe
= sh_pfc_pinctrl_probe
,
443 .remove
= __devexit_p(sh_pfc_pinctrl_remove
),
445 .name
= KBUILD_MODNAME
,
446 .owner
= THIS_MODULE
,
450 static struct platform_device sh_pfc_pinctrl_device
= {
451 .name
= KBUILD_MODNAME
,
455 static int __init
sh_pfc_pinctrl_init(void)
459 rc
= platform_driver_register(&sh_pfc_pinctrl_driver
);
461 rc
= platform_device_register(&sh_pfc_pinctrl_device
);
463 platform_driver_unregister(&sh_pfc_pinctrl_driver
);
469 static void __exit
sh_pfc_pinctrl_exit(void)
471 platform_driver_unregister(&sh_pfc_pinctrl_driver
);
474 subsys_initcall(sh_pfc_pinctrl_init
);
475 module_exit(sh_pfc_pinctrl_exit
);