2 * SuperH Pin Function Controller support.
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
12 #define DRV_NAME "sh-pfc"
14 #include <linux/bitops.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
18 #include <linux/ioport.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/pinctrl/machine.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
29 static int sh_pfc_ioremap(struct sh_pfc *pfc, struct platform_device *pdev)
34 if (pdev->num_resources == 0)
37 pfc->window = devm_kzalloc(pfc->dev, pdev->num_resources *
38 sizeof(*pfc->window), GFP_NOWAIT);
42 pfc->num_windows = pdev->num_resources;
44 for (k = 0, res = pdev->resource; k < pdev->num_resources; k++, res++) {
45 WARN_ON(resource_type(res) != IORESOURCE_MEM);
46 pfc->window[k].phys = res->start;
47 pfc->window[k].size = resource_size(res);
48 pfc->window[k].virt = devm_ioremap_nocache(pfc->dev, res->start,
50 if (!pfc->window[k].virt)
57 static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
58 unsigned long address)
60 struct sh_pfc_window *window;
63 /* scan through physical windows and convert address */
64 for (i = 0; i < pfc->num_windows; i++) {
65 window = pfc->window + i;
67 if (address < window->phys)
70 if (address >= (window->phys + window->size))
73 return window->virt + (address - window->phys);
80 int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
85 if (pfc->info->ranges == NULL)
88 for (i = 0, offset = 0; i < pfc->info->nr_ranges; ++i) {
89 const struct pinmux_range *range = &pfc->info->ranges[i];
91 if (pin <= range->end)
92 return pin >= range->begin
93 ? offset + pin - range->begin : -1;
95 offset += range->end - range->begin + 1;
101 static int sh_pfc_enum_in_range(pinmux_enum_t enum_id,
102 const struct pinmux_range *r)
104 if (enum_id < r->begin)
107 if (enum_id > r->end)
113 unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
114 unsigned long reg_width)
118 return ioread8(mapped_reg);
120 return ioread16(mapped_reg);
122 return ioread32(mapped_reg);
129 void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned long reg_width,
134 iowrite8(data, mapped_reg);
137 iowrite16(data, mapped_reg);
140 iowrite32(data, mapped_reg);
147 static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
148 const struct pinmux_cfg_reg *crp,
149 unsigned long in_pos,
150 void __iomem **mapped_regp,
151 unsigned long *maskp,
156 *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
158 if (crp->field_width) {
159 *maskp = (1 << crp->field_width) - 1;
160 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
162 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
163 *posp = crp->reg_width;
164 for (k = 0; k <= in_pos; k++)
165 *posp -= crp->var_field_width[k];
169 static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
170 const struct pinmux_cfg_reg *crp,
171 unsigned long field, unsigned long value)
173 void __iomem *mapped_reg;
174 unsigned long mask, pos, data;
176 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
178 dev_dbg(pfc->dev, "write_reg addr = %lx, value = %ld, field = %ld, "
179 "r_width = %ld, f_width = %ld\n",
180 crp->reg, value, field, crp->reg_width, crp->field_width);
182 mask = ~(mask << pos);
183 value = value << pos;
185 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
189 if (pfc->info->unlock_reg)
190 sh_pfc_write_raw_reg(
191 sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
194 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
197 static int sh_pfc_get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
198 const struct pinmux_cfg_reg **crp, int *fieldp,
201 const struct pinmux_cfg_reg *config_reg;
202 unsigned long r_width, f_width, curr_width, ncomb;
203 int k, m, n, pos, bit_pos;
207 config_reg = pfc->info->cfg_regs + k;
209 r_width = config_reg->reg_width;
210 f_width = config_reg->field_width;
217 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
219 curr_width = f_width;
221 curr_width = config_reg->var_field_width[m];
223 ncomb = 1 << curr_width;
224 for (n = 0; n < ncomb; n++) {
225 if (config_reg->enum_ids[pos + n] == enum_id) {
241 static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, pinmux_enum_t mark, int pos,
242 pinmux_enum_t *enum_idp)
244 const pinmux_enum_t *data = pfc->info->gpio_data;
248 *enum_idp = data[pos + 1];
252 for (k = 0; k < pfc->info->gpio_data_size; k++) {
253 if (data[k] == mark) {
254 *enum_idp = data[k + 1];
259 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
264 int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
266 const struct pinmux_cfg_reg *cr = NULL;
267 pinmux_enum_t enum_id;
268 const struct pinmux_range *range;
269 int in_range, pos, field, value;
272 switch (pinmux_type) {
273 case PINMUX_TYPE_GPIO:
274 case PINMUX_TYPE_FUNCTION:
278 case PINMUX_TYPE_OUTPUT:
279 range = &pfc->info->output;
282 case PINMUX_TYPE_INPUT:
283 range = &pfc->info->input;
295 /* Iterate over all the configuration fields we need to update. */
297 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
304 /* Check if the configuration field selects a function. If it
305 * doesn't, skip the field if it's not applicable to the
306 * requested pinmux type.
308 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
310 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
311 /* Functions are allowed to modify all
315 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
316 /* Input/output types can only modify fields
317 * that correspond to their respective ranges.
319 in_range = sh_pfc_enum_in_range(enum_id, range);
322 * special case pass through for fixed
323 * input-only or output-only pins without
324 * function enum register association.
326 if (in_range && enum_id == range->force)
329 /* GPIOs are only allowed to modify function fields. */
335 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
339 sh_pfc_write_config_reg(pfc, cr, field, value);
346 static const struct of_device_id sh_pfc_of_table[] = {
347 #ifdef CONFIG_PINCTRL_PFC_R8A73A4
349 .compatible = "renesas,pfc-r8a73a4",
350 .data = &r8a73a4_pinmux_info,
353 #ifdef CONFIG_PINCTRL_PFC_R8A7740
355 .compatible = "renesas,pfc-r8a7740",
356 .data = &r8a7740_pinmux_info,
359 #ifdef CONFIG_PINCTRL_PFC_R8A7778
361 .compatible = "renesas,pfc-r8a7778",
362 .data = &r8a7778_pinmux_info,
365 #ifdef CONFIG_PINCTRL_PFC_R8A7779
367 .compatible = "renesas,pfc-r8a7779",
368 .data = &r8a7779_pinmux_info,
371 #ifdef CONFIG_PINCTRL_PFC_R8A7790
373 .compatible = "renesas,pfc-r8a7790",
374 .data = &r8a7790_pinmux_info,
377 #ifdef CONFIG_PINCTRL_PFC_SH7372
379 .compatible = "renesas,pfc-sh7372",
380 .data = &sh7372_pinmux_info,
383 #ifdef CONFIG_PINCTRL_PFC_SH73A0
385 .compatible = "renesas,pfc-sh73a0",
386 .data = &sh73a0_pinmux_info,
391 MODULE_DEVICE_TABLE(of, sh_pfc_of_table);
394 static int sh_pfc_probe(struct platform_device *pdev)
396 const struct platform_device_id *platid = platform_get_device_id(pdev);
398 struct device_node *np = pdev->dev.of_node;
400 const struct sh_pfc_soc_info *info;
406 info = of_match_device(sh_pfc_of_table, &pdev->dev)->data;
409 info = platid ? (const void *)platid->driver_data : NULL;
414 pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
419 pfc->dev = &pdev->dev;
421 ret = sh_pfc_ioremap(pfc, pdev);
422 if (unlikely(ret < 0))
425 spin_lock_init(&pfc->lock);
427 if (info->ops && info->ops->init) {
428 ret = info->ops->init(pfc);
433 pinctrl_provide_dummies();
436 * Initialize pinctrl bindings first
438 ret = sh_pfc_register_pinctrl(pfc);
439 if (unlikely(ret != 0))
442 #ifdef CONFIG_GPIO_SH_PFC
446 ret = sh_pfc_register_gpiochip(pfc);
447 if (unlikely(ret != 0)) {
449 * If the GPIO chip fails to come up we still leave the
450 * PFC state as it is, given that there are already
451 * extant users of it that have succeeded by this point.
453 dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
457 platform_set_drvdata(pdev, pfc);
459 dev_info(pfc->dev, "%s support registered\n", info->name);
464 if (info->ops && info->ops->exit)
465 info->ops->exit(pfc);
469 static int sh_pfc_remove(struct platform_device *pdev)
471 struct sh_pfc *pfc = platform_get_drvdata(pdev);
473 #ifdef CONFIG_GPIO_SH_PFC
474 sh_pfc_unregister_gpiochip(pfc);
476 sh_pfc_unregister_pinctrl(pfc);
478 if (pfc->info->ops && pfc->info->ops->exit)
479 pfc->info->ops->exit(pfc);
481 platform_set_drvdata(pdev, NULL);
486 static const struct platform_device_id sh_pfc_id_table[] = {
487 #ifdef CONFIG_PINCTRL_PFC_R8A73A4
488 { "pfc-r8a73a4", (kernel_ulong_t)&r8a73a4_pinmux_info },
490 #ifdef CONFIG_PINCTRL_PFC_R8A7740
491 { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
493 #ifdef CONFIG_PINCTRL_PFC_R8A7778
494 { "pfc-r8a7778", (kernel_ulong_t)&r8a7778_pinmux_info },
496 #ifdef CONFIG_PINCTRL_PFC_R8A7779
497 { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
499 #ifdef CONFIG_PINCTRL_PFC_R8A7790
500 { "pfc-r8a7790", (kernel_ulong_t)&r8a7790_pinmux_info },
502 #ifdef CONFIG_PINCTRL_PFC_SH7203
503 { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
505 #ifdef CONFIG_PINCTRL_PFC_SH7264
506 { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
508 #ifdef CONFIG_PINCTRL_PFC_SH7269
509 { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
511 #ifdef CONFIG_PINCTRL_PFC_SH7372
512 { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
514 #ifdef CONFIG_PINCTRL_PFC_SH73A0
515 { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
517 #ifdef CONFIG_PINCTRL_PFC_SH7720
518 { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
520 #ifdef CONFIG_PINCTRL_PFC_SH7722
521 { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
523 #ifdef CONFIG_PINCTRL_PFC_SH7723
524 { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
526 #ifdef CONFIG_PINCTRL_PFC_SH7724
527 { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
529 #ifdef CONFIG_PINCTRL_PFC_SH7734
530 { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
532 #ifdef CONFIG_PINCTRL_PFC_SH7757
533 { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
535 #ifdef CONFIG_PINCTRL_PFC_SH7785
536 { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
538 #ifdef CONFIG_PINCTRL_PFC_SH7786
539 { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
541 #ifdef CONFIG_PINCTRL_PFC_SHX3
542 { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
547 MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
549 static struct platform_driver sh_pfc_driver = {
550 .probe = sh_pfc_probe,
551 .remove = sh_pfc_remove,
552 .id_table = sh_pfc_id_table,
555 .owner = THIS_MODULE,
556 .of_match_table = of_match_ptr(sh_pfc_of_table),
560 static int __init sh_pfc_init(void)
562 return platform_driver_register(&sh_pfc_driver);
564 postcore_initcall(sh_pfc_init);
566 static void __exit sh_pfc_exit(void)
568 platform_driver_unregister(&sh_pfc_driver);
570 module_exit(sh_pfc_exit);
572 MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
573 MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
574 MODULE_LICENSE("GPL v2");