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
;
286 case PINMUX_TYPE_INPUT_PULLUP
:
287 range
= &pfc
->info
->input_pu
;
290 case PINMUX_TYPE_INPUT_PULLDOWN
:
291 range
= &pfc
->info
->input_pd
;
303 /* Iterate over all the configuration fields we need to update. */
305 pos
= sh_pfc_mark_to_enum(pfc
, mark
, pos
, &enum_id
);
312 /* Check if the configuration field selects a function. If it
313 * doesn't, skip the field if it's not applicable to the
314 * requested pinmux type.
316 in_range
= sh_pfc_enum_in_range(enum_id
, &pfc
->info
->function
);
318 if (pinmux_type
== PINMUX_TYPE_FUNCTION
) {
319 /* Functions are allowed to modify all
323 } else if (pinmux_type
!= PINMUX_TYPE_GPIO
) {
324 /* Input/output types can only modify fields
325 * that correspond to their respective ranges.
327 in_range
= sh_pfc_enum_in_range(enum_id
, range
);
330 * special case pass through for fixed
331 * input-only or output-only pins without
332 * function enum register association.
334 if (in_range
&& enum_id
== range
->force
)
337 /* GPIOs are only allowed to modify function fields. */
343 ret
= sh_pfc_get_config_reg(pfc
, enum_id
, &cr
, &field
, &value
);
347 sh_pfc_write_config_reg(pfc
, cr
, field
, value
);
354 static const struct of_device_id sh_pfc_of_table
[] = {
355 #ifdef CONFIG_PINCTRL_PFC_R8A73A4
357 .compatible
= "renesas,pfc-r8a73a4",
358 .data
= &r8a73a4_pinmux_info
,
361 #ifdef CONFIG_PINCTRL_PFC_R8A7740
363 .compatible
= "renesas,pfc-r8a7740",
364 .data
= &r8a7740_pinmux_info
,
367 #ifdef CONFIG_PINCTRL_PFC_R8A7778
369 .compatible
= "renesas,pfc-r8a7778",
370 .data
= &r8a7778_pinmux_info
,
373 #ifdef CONFIG_PINCTRL_PFC_R8A7779
375 .compatible
= "renesas,pfc-r8a7779",
376 .data
= &r8a7779_pinmux_info
,
379 #ifdef CONFIG_PINCTRL_PFC_R8A7790
381 .compatible
= "renesas,pfc-r8a7790",
382 .data
= &r8a7790_pinmux_info
,
385 #ifdef CONFIG_PINCTRL_PFC_SH7372
387 .compatible
= "renesas,pfc-sh7372",
388 .data
= &sh7372_pinmux_info
,
391 #ifdef CONFIG_PINCTRL_PFC_SH73A0
393 .compatible
= "renesas,pfc-sh73a0",
394 .data
= &sh73a0_pinmux_info
,
399 MODULE_DEVICE_TABLE(of
, sh_pfc_of_table
);
402 static int sh_pfc_probe(struct platform_device
*pdev
)
404 const struct platform_device_id
*platid
= platform_get_device_id(pdev
);
406 struct device_node
*np
= pdev
->dev
.of_node
;
408 const struct sh_pfc_soc_info
*info
;
414 info
= of_match_device(sh_pfc_of_table
, &pdev
->dev
)->data
;
417 info
= platid
? (const void *)platid
->driver_data
: NULL
;
422 pfc
= devm_kzalloc(&pdev
->dev
, sizeof(*pfc
), GFP_KERNEL
);
427 pfc
->dev
= &pdev
->dev
;
429 ret
= sh_pfc_ioremap(pfc
, pdev
);
430 if (unlikely(ret
< 0))
433 spin_lock_init(&pfc
->lock
);
435 if (info
->ops
&& info
->ops
->init
) {
436 ret
= info
->ops
->init(pfc
);
441 pinctrl_provide_dummies();
444 * Initialize pinctrl bindings first
446 ret
= sh_pfc_register_pinctrl(pfc
);
447 if (unlikely(ret
!= 0))
450 #ifdef CONFIG_GPIO_SH_PFC
454 ret
= sh_pfc_register_gpiochip(pfc
);
455 if (unlikely(ret
!= 0)) {
457 * If the GPIO chip fails to come up we still leave the
458 * PFC state as it is, given that there are already
459 * extant users of it that have succeeded by this point.
461 dev_notice(pfc
->dev
, "failed to init GPIO chip, ignoring...\n");
465 platform_set_drvdata(pdev
, pfc
);
467 dev_info(pfc
->dev
, "%s support registered\n", info
->name
);
472 if (info
->ops
&& info
->ops
->exit
)
473 info
->ops
->exit(pfc
);
477 static int sh_pfc_remove(struct platform_device
*pdev
)
479 struct sh_pfc
*pfc
= platform_get_drvdata(pdev
);
481 #ifdef CONFIG_GPIO_SH_PFC
482 sh_pfc_unregister_gpiochip(pfc
);
484 sh_pfc_unregister_pinctrl(pfc
);
486 if (pfc
->info
->ops
&& pfc
->info
->ops
->exit
)
487 pfc
->info
->ops
->exit(pfc
);
489 platform_set_drvdata(pdev
, NULL
);
494 static const struct platform_device_id sh_pfc_id_table
[] = {
495 #ifdef CONFIG_PINCTRL_PFC_R8A73A4
496 { "pfc-r8a73a4", (kernel_ulong_t
)&r8a73a4_pinmux_info
},
498 #ifdef CONFIG_PINCTRL_PFC_R8A7740
499 { "pfc-r8a7740", (kernel_ulong_t
)&r8a7740_pinmux_info
},
501 #ifdef CONFIG_PINCTRL_PFC_R8A7778
502 { "pfc-r8a7778", (kernel_ulong_t
)&r8a7778_pinmux_info
},
504 #ifdef CONFIG_PINCTRL_PFC_R8A7779
505 { "pfc-r8a7779", (kernel_ulong_t
)&r8a7779_pinmux_info
},
507 #ifdef CONFIG_PINCTRL_PFC_R8A7790
508 { "pfc-r8a7790", (kernel_ulong_t
)&r8a7790_pinmux_info
},
510 #ifdef CONFIG_PINCTRL_PFC_SH7203
511 { "pfc-sh7203", (kernel_ulong_t
)&sh7203_pinmux_info
},
513 #ifdef CONFIG_PINCTRL_PFC_SH7264
514 { "pfc-sh7264", (kernel_ulong_t
)&sh7264_pinmux_info
},
516 #ifdef CONFIG_PINCTRL_PFC_SH7269
517 { "pfc-sh7269", (kernel_ulong_t
)&sh7269_pinmux_info
},
519 #ifdef CONFIG_PINCTRL_PFC_SH7372
520 { "pfc-sh7372", (kernel_ulong_t
)&sh7372_pinmux_info
},
522 #ifdef CONFIG_PINCTRL_PFC_SH73A0
523 { "pfc-sh73a0", (kernel_ulong_t
)&sh73a0_pinmux_info
},
525 #ifdef CONFIG_PINCTRL_PFC_SH7720
526 { "pfc-sh7720", (kernel_ulong_t
)&sh7720_pinmux_info
},
528 #ifdef CONFIG_PINCTRL_PFC_SH7722
529 { "pfc-sh7722", (kernel_ulong_t
)&sh7722_pinmux_info
},
531 #ifdef CONFIG_PINCTRL_PFC_SH7723
532 { "pfc-sh7723", (kernel_ulong_t
)&sh7723_pinmux_info
},
534 #ifdef CONFIG_PINCTRL_PFC_SH7724
535 { "pfc-sh7724", (kernel_ulong_t
)&sh7724_pinmux_info
},
537 #ifdef CONFIG_PINCTRL_PFC_SH7734
538 { "pfc-sh7734", (kernel_ulong_t
)&sh7734_pinmux_info
},
540 #ifdef CONFIG_PINCTRL_PFC_SH7757
541 { "pfc-sh7757", (kernel_ulong_t
)&sh7757_pinmux_info
},
543 #ifdef CONFIG_PINCTRL_PFC_SH7785
544 { "pfc-sh7785", (kernel_ulong_t
)&sh7785_pinmux_info
},
546 #ifdef CONFIG_PINCTRL_PFC_SH7786
547 { "pfc-sh7786", (kernel_ulong_t
)&sh7786_pinmux_info
},
549 #ifdef CONFIG_PINCTRL_PFC_SHX3
550 { "pfc-shx3", (kernel_ulong_t
)&shx3_pinmux_info
},
555 MODULE_DEVICE_TABLE(platform
, sh_pfc_id_table
);
557 static struct platform_driver sh_pfc_driver
= {
558 .probe
= sh_pfc_probe
,
559 .remove
= sh_pfc_remove
,
560 .id_table
= sh_pfc_id_table
,
563 .owner
= THIS_MODULE
,
564 .of_match_table
= of_match_ptr(sh_pfc_of_table
),
568 static int __init
sh_pfc_init(void)
570 return platform_driver_register(&sh_pfc_driver
);
572 postcore_initcall(sh_pfc_init
);
574 static void __exit
sh_pfc_exit(void)
576 platform_driver_unregister(&sh_pfc_driver
);
578 module_exit(sh_pfc_exit
);
580 MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
581 MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
582 MODULE_LICENSE("GPL v2");