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
11 #define DRV_NAME "sh-pfc"
12 #define pr_fmt(fmt) KBUILD_MODNAME " pinctrl: " fmt
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/pinctrl/pinconf-generic.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
28 struct sh_pfc_pinctrl
{
29 struct pinctrl_dev
*pctl
;
30 struct pinctrl_desc pctl_desc
;
31 struct pinctrl_gpio_range range
;
35 struct pinmux_gpio
**functions
;
36 unsigned int nr_functions
;
38 struct pinctrl_pin_desc
*pads
;
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 const 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 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
->info
->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
->info
->gpios
[offset
].flags
&= ~PINMUX_FLAG_TYPE
;
176 pfc
->info
->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
->info
->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
:
211 case PINMUX_TYPE_INPUT
:
212 case PINMUX_TYPE_OUTPUT
:
215 pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type
);
223 spin_unlock_irqrestore(&pfc
->lock
, flags
);
228 static void sh_pfc_gpio_disable_free(struct pinctrl_dev
*pctldev
,
229 struct pinctrl_gpio_range
*range
,
232 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
233 struct sh_pfc
*pfc
= pmx
->pfc
;
237 spin_lock_irqsave(&pfc
->lock
, flags
);
239 pinmux_type
= pfc
->info
->gpios
[offset
].flags
& PINMUX_FLAG_TYPE
;
241 sh_pfc_config_gpio(pfc
, offset
, pinmux_type
, GPIO_CFG_FREE
);
243 spin_unlock_irqrestore(&pfc
->lock
, flags
);
246 static int sh_pfc_gpio_set_direction(struct pinctrl_dev
*pctldev
,
247 struct pinctrl_gpio_range
*range
,
248 unsigned offset
, bool input
)
250 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
251 int type
= input
? PINMUX_TYPE_INPUT
: PINMUX_TYPE_OUTPUT
;
253 return sh_pfc_reconfig_pin(pmx
->pfc
, offset
, type
);
256 static const struct pinmux_ops sh_pfc_pinmux_ops
= {
257 .get_functions_count
= sh_pfc_get_functions_count
,
258 .get_function_name
= sh_pfc_get_function_name
,
259 .get_function_groups
= sh_pfc_get_function_groups
,
260 .enable
= sh_pfc_noop_enable
,
261 .disable
= sh_pfc_noop_disable
,
262 .gpio_request_enable
= sh_pfc_gpio_request_enable
,
263 .gpio_disable_free
= sh_pfc_gpio_disable_free
,
264 .gpio_set_direction
= sh_pfc_gpio_set_direction
,
267 static int sh_pfc_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
268 unsigned long *config
)
270 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
271 struct sh_pfc
*pfc
= pmx
->pfc
;
273 *config
= pfc
->info
->gpios
[pin
].flags
& PINMUX_FLAG_TYPE
;
278 static int sh_pfc_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
279 unsigned long config
)
281 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
283 /* Validate the new type */
284 if (config
>= PINMUX_FLAG_TYPE
)
287 return sh_pfc_reconfig_pin(pmx
->pfc
, pin
, config
);
290 static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
291 struct seq_file
*s
, unsigned pin
)
293 const char *pinmux_type_str
[] = {
294 [PINMUX_TYPE_NONE
] = "none",
295 [PINMUX_TYPE_FUNCTION
] = "function",
296 [PINMUX_TYPE_GPIO
] = "gpio",
297 [PINMUX_TYPE_OUTPUT
] = "output",
298 [PINMUX_TYPE_INPUT
] = "input",
299 [PINMUX_TYPE_INPUT_PULLUP
] = "input bias pull up",
300 [PINMUX_TYPE_INPUT_PULLDOWN
] = "input bias pull down",
302 unsigned long config
;
305 rc
= sh_pfc_pinconf_get(pctldev
, pin
, &config
);
306 if (unlikely(rc
!= 0))
309 seq_printf(s
, " %s", pinmux_type_str
[config
]);
312 static const struct pinconf_ops sh_pfc_pinconf_ops
= {
313 .pin_config_get
= sh_pfc_pinconf_get
,
314 .pin_config_set
= sh_pfc_pinconf_set
,
315 .pin_config_dbg_show
= sh_pfc_pinconf_dbg_show
,
318 /* pinmux ranges -> pinctrl pin descs */
319 static int sh_pfc_map_gpios(struct sh_pfc
*pfc
, struct sh_pfc_pinctrl
*pmx
)
323 pmx
->nr_pads
= pfc
->info
->nr_gpios
;
325 pmx
->pads
= devm_kzalloc(pfc
->dev
, sizeof(*pmx
->pads
) * pmx
->nr_pads
,
327 if (unlikely(!pmx
->pads
)) {
332 for (i
= 0; i
< pmx
->nr_pads
; i
++) {
333 struct pinctrl_pin_desc
*pin
= pmx
->pads
+ i
;
334 struct pinmux_gpio
*gpio
= pfc
->info
->gpios
+ i
;
337 pin
->name
= gpio
->name
;
340 if (unlikely(!gpio
->enum_id
))
343 if ((gpio
->flags
& PINMUX_FLAG_TYPE
) == PINMUX_TYPE_FUNCTION
)
350 static int sh_pfc_map_functions(struct sh_pfc
*pfc
, struct sh_pfc_pinctrl
*pmx
)
354 pmx
->functions
= devm_kzalloc(pfc
->dev
, pmx
->nr_functions
*
355 sizeof(*pmx
->functions
), GFP_KERNEL
);
356 if (unlikely(!pmx
->functions
))
359 for (i
= fn
= 0; i
< pmx
->nr_pads
; i
++) {
360 struct pinmux_gpio
*gpio
= pfc
->info
->gpios
+ i
;
362 if ((gpio
->flags
& PINMUX_FLAG_TYPE
) == PINMUX_TYPE_FUNCTION
)
363 pmx
->functions
[fn
++] = gpio
;
369 int sh_pfc_register_pinctrl(struct sh_pfc
*pfc
)
371 struct sh_pfc_pinctrl
*pmx
;
374 pmx
= devm_kzalloc(pfc
->dev
, sizeof(*pmx
), GFP_KERNEL
);
381 ret
= sh_pfc_map_gpios(pfc
, pmx
);
382 if (unlikely(ret
!= 0))
385 ret
= sh_pfc_map_functions(pfc
, pmx
);
386 if (unlikely(ret
!= 0))
389 pmx
->pctl_desc
.name
= DRV_NAME
;
390 pmx
->pctl_desc
.owner
= THIS_MODULE
;
391 pmx
->pctl_desc
.pctlops
= &sh_pfc_pinctrl_ops
;
392 pmx
->pctl_desc
.pmxops
= &sh_pfc_pinmux_ops
;
393 pmx
->pctl_desc
.confops
= &sh_pfc_pinconf_ops
;
394 pmx
->pctl_desc
.pins
= pmx
->pads
;
395 pmx
->pctl_desc
.npins
= pmx
->nr_pads
;
397 pmx
->pctl
= pinctrl_register(&pmx
->pctl_desc
, pfc
->dev
, pmx
);
398 if (IS_ERR(pmx
->pctl
))
399 return PTR_ERR(pmx
->pctl
);
401 pmx
->range
.name
= DRV_NAME
,
403 pmx
->range
.npins
= pfc
->info
->nr_gpios
;
405 pmx
->range
.pin_base
= 0;
407 pinctrl_add_gpio_range(pmx
->pctl
, &pmx
->range
);
412 int sh_pfc_unregister_pinctrl(struct sh_pfc
*pfc
)
414 struct sh_pfc_pinctrl
*pmx
= pfc
->pinctrl
;
416 pinctrl_unregister(pmx
->pctl
);