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_pin_config
{
32 struct sh_pfc_pinctrl
{
33 struct pinctrl_dev
*pctl
;
34 struct pinctrl_desc pctl_desc
;
38 struct pinctrl_pin_desc
*pins
;
39 struct sh_pfc_pin_config
*configs
;
42 static int sh_pfc_get_groups_count(struct pinctrl_dev
*pctldev
)
44 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
46 return pmx
->pfc
->info
->nr_groups
;
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
->pfc
->info
->groups
[selector
].name
;
57 static int sh_pfc_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
58 const unsigned **pins
, unsigned *num_pins
)
60 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
62 *pins
= pmx
->pfc
->info
->groups
[selector
].pins
;
63 *num_pins
= pmx
->pfc
->info
->groups
[selector
].nr_pins
;
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
->pfc
->info
->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
->pfc
->info
->functions
[selector
].name
;
96 static int sh_pfc_get_function_groups(struct pinctrl_dev
*pctldev
,
98 const char * const **groups
,
99 unsigned * const num_groups
)
101 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
103 *groups
= pmx
->pfc
->info
->functions
[selector
].groups
;
104 *num_groups
= pmx
->pfc
->info
->functions
[selector
].nr_groups
;
109 static int sh_pfc_func_enable(struct pinctrl_dev
*pctldev
, unsigned selector
,
112 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
113 struct sh_pfc
*pfc
= pmx
->pfc
;
114 const struct sh_pfc_pin_group
*grp
= &pfc
->info
->groups
[group
];
119 spin_lock_irqsave(&pfc
->lock
, flags
);
121 for (i
= 0; i
< grp
->nr_pins
; ++i
) {
122 if (sh_pfc_config_mux(pfc
, grp
->mux
[i
], PINMUX_TYPE_FUNCTION
))
129 spin_unlock_irqrestore(&pfc
->lock
, flags
);
133 static void sh_pfc_func_disable(struct pinctrl_dev
*pctldev
, unsigned selector
,
138 static int sh_pfc_reconfig_pin(struct sh_pfc_pinctrl
*pmx
, unsigned offset
,
141 struct sh_pfc
*pfc
= pmx
->pfc
;
142 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
143 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
144 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
145 unsigned int mark
= pin
->enum_id
;
149 spin_lock_irqsave(&pfc
->lock
, flags
);
152 case PINMUX_TYPE_GPIO
:
153 case PINMUX_TYPE_OUTPUT
:
154 case PINMUX_TYPE_INPUT
:
155 case PINMUX_TYPE_INPUT_PULLUP
:
156 case PINMUX_TYPE_INPUT_PULLDOWN
:
162 if (sh_pfc_config_mux(pfc
, mark
, new_type
) != 0)
165 cfg
->type
= new_type
;
170 spin_unlock_irqrestore(&pfc
->lock
, flags
);
175 static int sh_pfc_gpio_request_enable(struct pinctrl_dev
*pctldev
,
176 struct pinctrl_gpio_range
*range
,
179 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
180 struct sh_pfc
*pfc
= pmx
->pfc
;
181 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
182 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
186 spin_lock_irqsave(&pfc
->lock
, flags
);
189 case PINMUX_TYPE_GPIO
:
190 case PINMUX_TYPE_INPUT
:
191 case PINMUX_TYPE_OUTPUT
:
193 case PINMUX_TYPE_FUNCTION
:
195 pr_err("Unsupported mux type (%d), bailing...\n", cfg
->type
);
203 spin_unlock_irqrestore(&pfc
->lock
, flags
);
208 static void sh_pfc_gpio_disable_free(struct pinctrl_dev
*pctldev
,
209 struct pinctrl_gpio_range
*range
,
214 static int sh_pfc_gpio_set_direction(struct pinctrl_dev
*pctldev
,
215 struct pinctrl_gpio_range
*range
,
216 unsigned offset
, bool input
)
218 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
219 int type
= input
? PINMUX_TYPE_INPUT
: PINMUX_TYPE_OUTPUT
;
221 return sh_pfc_reconfig_pin(pmx
, offset
, type
);
224 static const struct pinmux_ops sh_pfc_pinmux_ops
= {
225 .get_functions_count
= sh_pfc_get_functions_count
,
226 .get_function_name
= sh_pfc_get_function_name
,
227 .get_function_groups
= sh_pfc_get_function_groups
,
228 .enable
= sh_pfc_func_enable
,
229 .disable
= sh_pfc_func_disable
,
230 .gpio_request_enable
= sh_pfc_gpio_request_enable
,
231 .gpio_disable_free
= sh_pfc_gpio_disable_free
,
232 .gpio_set_direction
= sh_pfc_gpio_set_direction
,
235 static int sh_pfc_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned _pin
,
236 unsigned long *config
)
238 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
239 struct sh_pfc
*pfc
= pmx
->pfc
;
240 int idx
= sh_pfc_get_pin_index(pfc
, _pin
);
241 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
248 static int sh_pfc_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
249 unsigned long config
)
251 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
253 /* Validate the new type */
254 if (config
>= PINMUX_FLAG_TYPE
)
257 return sh_pfc_reconfig_pin(pmx
, pin
, config
);
260 static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
261 struct seq_file
*s
, unsigned pin
)
263 const char *pinmux_type_str
[] = {
264 [PINMUX_TYPE_NONE
] = "none",
265 [PINMUX_TYPE_FUNCTION
] = "function",
266 [PINMUX_TYPE_GPIO
] = "gpio",
267 [PINMUX_TYPE_OUTPUT
] = "output",
268 [PINMUX_TYPE_INPUT
] = "input",
269 [PINMUX_TYPE_INPUT_PULLUP
] = "input bias pull up",
270 [PINMUX_TYPE_INPUT_PULLDOWN
] = "input bias pull down",
272 unsigned long config
;
275 rc
= sh_pfc_pinconf_get(pctldev
, pin
, &config
);
276 if (unlikely(rc
!= 0))
279 seq_printf(s
, " %s", pinmux_type_str
[config
]);
282 static const struct pinconf_ops sh_pfc_pinconf_ops
= {
283 .pin_config_get
= sh_pfc_pinconf_get
,
284 .pin_config_set
= sh_pfc_pinconf_set
,
285 .pin_config_dbg_show
= sh_pfc_pinconf_dbg_show
,
288 /* PFC ranges -> pinctrl pin descs */
289 static int sh_pfc_map_pins(struct sh_pfc
*pfc
, struct sh_pfc_pinctrl
*pmx
)
291 const struct pinmux_range
*ranges
;
292 struct pinmux_range def_range
;
293 unsigned int nr_ranges
;
294 unsigned int nr_pins
;
297 if (pfc
->info
->ranges
== NULL
) {
299 def_range
.end
= pfc
->info
->nr_pins
- 1;
303 ranges
= pfc
->info
->ranges
;
304 nr_ranges
= pfc
->info
->nr_ranges
;
307 pmx
->pins
= devm_kzalloc(pfc
->dev
,
308 sizeof(*pmx
->pins
) * pfc
->info
->nr_pins
,
310 if (unlikely(!pmx
->pins
))
313 pmx
->configs
= devm_kzalloc(pfc
->dev
,
314 sizeof(*pmx
->configs
) * pfc
->info
->nr_pins
,
316 if (unlikely(!pmx
->configs
))
319 for (i
= 0, nr_pins
= 0; i
< nr_ranges
; ++i
) {
320 const struct pinmux_range
*range
= &ranges
[i
];
323 for (number
= range
->begin
; number
<= range
->end
;
324 number
++, nr_pins
++) {
325 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[nr_pins
];
326 struct pinctrl_pin_desc
*pin
= &pmx
->pins
[nr_pins
];
327 const struct sh_pfc_pin
*info
=
328 &pfc
->info
->pins
[nr_pins
];
330 pin
->number
= number
;
331 pin
->name
= info
->name
;
332 cfg
->type
= PINMUX_TYPE_GPIO
;
336 pfc
->nr_pins
= ranges
[nr_ranges
-1].end
+ 1;
341 int sh_pfc_register_pinctrl(struct sh_pfc
*pfc
)
343 struct sh_pfc_pinctrl
*pmx
;
346 pmx
= devm_kzalloc(pfc
->dev
, sizeof(*pmx
), GFP_KERNEL
);
353 nr_ranges
= sh_pfc_map_pins(pfc
, pmx
);
354 if (unlikely(nr_ranges
< 0))
357 pmx
->pctl_desc
.name
= DRV_NAME
;
358 pmx
->pctl_desc
.owner
= THIS_MODULE
;
359 pmx
->pctl_desc
.pctlops
= &sh_pfc_pinctrl_ops
;
360 pmx
->pctl_desc
.pmxops
= &sh_pfc_pinmux_ops
;
361 pmx
->pctl_desc
.confops
= &sh_pfc_pinconf_ops
;
362 pmx
->pctl_desc
.pins
= pmx
->pins
;
363 pmx
->pctl_desc
.npins
= pfc
->info
->nr_pins
;
365 pmx
->pctl
= pinctrl_register(&pmx
->pctl_desc
, pfc
->dev
, pmx
);
366 if (pmx
->pctl
== NULL
)
372 int sh_pfc_unregister_pinctrl(struct sh_pfc
*pfc
)
374 struct sh_pfc_pinctrl
*pmx
= pfc
->pinctrl
;
376 pinctrl_unregister(pmx
->pctl
);