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"
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/sh_pfc.h>
18 #include <linux/module.h>
19 #include <linux/err.h>
21 #include <linux/bitops.h>
22 #include <linux/slab.h>
23 #include <linux/ioport.h>
24 #include <linux/pinctrl/machine.h>
25 #include <linux/platform_device.h>
29 static void pfc_iounmap(struct sh_pfc
*pfc
)
33 for (k
= 0; k
< pfc
->pdata
->num_resources
; k
++)
34 if (pfc
->window
[k
].virt
)
35 iounmap(pfc
->window
[k
].virt
);
38 static int pfc_ioremap(struct sh_pfc
*pfc
)
43 if (!pfc
->pdata
->num_resources
)
46 pfc
->window
= devm_kzalloc(pfc
->dev
, pfc
->pdata
->num_resources
*
47 sizeof(*pfc
->window
), GFP_NOWAIT
);
51 for (k
= 0; k
< pfc
->pdata
->num_resources
; k
++) {
52 res
= pfc
->pdata
->resource
+ k
;
53 WARN_ON(resource_type(res
) != IORESOURCE_MEM
);
54 pfc
->window
[k
].phys
= res
->start
;
55 pfc
->window
[k
].size
= resource_size(res
);
56 pfc
->window
[k
].virt
= ioremap_nocache(res
->start
,
58 if (!pfc
->window
[k
].virt
) {
67 static void __iomem
*pfc_phys_to_virt(struct sh_pfc
*pfc
,
68 unsigned long address
)
70 struct pfc_window
*window
;
73 /* scan through physical windows and convert address */
74 for (k
= 0; k
< pfc
->pdata
->num_resources
; k
++) {
75 window
= pfc
->window
+ k
;
77 if (address
< window
->phys
)
80 if (address
>= (window
->phys
+ window
->size
))
83 return window
->virt
+ (address
- window
->phys
);
86 /* no windows defined, register must be 1:1 mapped virt:phys */
87 return (void __iomem
*)address
;
90 static int enum_in_range(pinmux_enum_t enum_id
, struct pinmux_range
*r
)
92 if (enum_id
< r
->begin
)
101 static unsigned long gpio_read_raw_reg(void __iomem
*mapped_reg
,
102 unsigned long reg_width
)
106 return ioread8(mapped_reg
);
108 return ioread16(mapped_reg
);
110 return ioread32(mapped_reg
);
117 static void gpio_write_raw_reg(void __iomem
*mapped_reg
,
118 unsigned long reg_width
,
123 iowrite8(data
, mapped_reg
);
126 iowrite16(data
, mapped_reg
);
129 iowrite32(data
, mapped_reg
);
136 int sh_pfc_read_bit(struct pinmux_data_reg
*dr
, unsigned long in_pos
)
140 pos
= dr
->reg_width
- (in_pos
+ 1);
142 pr_debug("read_bit: addr = %lx, pos = %ld, "
143 "r_width = %ld\n", dr
->reg
, pos
, dr
->reg_width
);
145 return (gpio_read_raw_reg(dr
->mapped_reg
, dr
->reg_width
) >> pos
) & 1;
148 void sh_pfc_write_bit(struct pinmux_data_reg
*dr
, unsigned long in_pos
,
153 pos
= dr
->reg_width
- (in_pos
+ 1);
155 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
157 dr
->reg
, !!value
, pos
, dr
->reg_width
);
160 set_bit(pos
, &dr
->reg_shadow
);
162 clear_bit(pos
, &dr
->reg_shadow
);
164 gpio_write_raw_reg(dr
->mapped_reg
, dr
->reg_width
, dr
->reg_shadow
);
167 static void config_reg_helper(struct sh_pfc
*pfc
,
168 struct pinmux_cfg_reg
*crp
,
169 unsigned long in_pos
,
170 void __iomem
**mapped_regp
,
171 unsigned long *maskp
,
176 *mapped_regp
= pfc_phys_to_virt(pfc
, crp
->reg
);
178 if (crp
->field_width
) {
179 *maskp
= (1 << crp
->field_width
) - 1;
180 *posp
= crp
->reg_width
- ((in_pos
+ 1) * crp
->field_width
);
182 *maskp
= (1 << crp
->var_field_width
[in_pos
]) - 1;
183 *posp
= crp
->reg_width
;
184 for (k
= 0; k
<= in_pos
; k
++)
185 *posp
-= crp
->var_field_width
[k
];
189 static int read_config_reg(struct sh_pfc
*pfc
,
190 struct pinmux_cfg_reg
*crp
,
193 void __iomem
*mapped_reg
;
194 unsigned long mask
, pos
;
196 config_reg_helper(pfc
, crp
, field
, &mapped_reg
, &mask
, &pos
);
198 pr_debug("read_reg: addr = %lx, field = %ld, "
199 "r_width = %ld, f_width = %ld\n",
200 crp
->reg
, field
, crp
->reg_width
, crp
->field_width
);
202 return (gpio_read_raw_reg(mapped_reg
, crp
->reg_width
) >> pos
) & mask
;
205 static void write_config_reg(struct sh_pfc
*pfc
,
206 struct pinmux_cfg_reg
*crp
,
207 unsigned long field
, unsigned long value
)
209 void __iomem
*mapped_reg
;
210 unsigned long mask
, pos
, data
;
212 config_reg_helper(pfc
, crp
, field
, &mapped_reg
, &mask
, &pos
);
214 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
215 "r_width = %ld, f_width = %ld\n",
216 crp
->reg
, value
, field
, crp
->reg_width
, crp
->field_width
);
218 mask
= ~(mask
<< pos
);
219 value
= value
<< pos
;
221 data
= gpio_read_raw_reg(mapped_reg
, crp
->reg_width
);
225 if (pfc
->pdata
->unlock_reg
)
226 gpio_write_raw_reg(pfc_phys_to_virt(pfc
, pfc
->pdata
->unlock_reg
),
229 gpio_write_raw_reg(mapped_reg
, crp
->reg_width
, data
);
232 static int setup_data_reg(struct sh_pfc
*pfc
, unsigned gpio
)
234 struct pinmux_gpio
*gpiop
= &pfc
->pdata
->gpios
[gpio
];
235 struct pinmux_data_reg
*data_reg
;
238 if (!enum_in_range(gpiop
->enum_id
, &pfc
->pdata
->data
))
243 data_reg
= pfc
->pdata
->data_regs
+ k
;
245 if (!data_reg
->reg_width
)
248 data_reg
->mapped_reg
= pfc_phys_to_virt(pfc
, data_reg
->reg
);
250 for (n
= 0; n
< data_reg
->reg_width
; n
++) {
251 if (data_reg
->enum_ids
[n
] == gpiop
->enum_id
) {
252 gpiop
->flags
&= ~PINMUX_FLAG_DREG
;
253 gpiop
->flags
|= (k
<< PINMUX_FLAG_DREG_SHIFT
);
254 gpiop
->flags
&= ~PINMUX_FLAG_DBIT
;
255 gpiop
->flags
|= (n
<< PINMUX_FLAG_DBIT_SHIFT
);
267 static void setup_data_regs(struct sh_pfc
*pfc
)
269 struct pinmux_data_reg
*drp
;
272 for (k
= pfc
->pdata
->first_gpio
; k
<= pfc
->pdata
->last_gpio
; k
++)
273 setup_data_reg(pfc
, k
);
277 drp
= pfc
->pdata
->data_regs
+ k
;
282 drp
->reg_shadow
= gpio_read_raw_reg(drp
->mapped_reg
,
288 int sh_pfc_get_data_reg(struct sh_pfc
*pfc
, unsigned gpio
,
289 struct pinmux_data_reg
**drp
, int *bitp
)
291 struct pinmux_gpio
*gpiop
= &pfc
->pdata
->gpios
[gpio
];
294 if (!enum_in_range(gpiop
->enum_id
, &pfc
->pdata
->data
))
297 k
= (gpiop
->flags
& PINMUX_FLAG_DREG
) >> PINMUX_FLAG_DREG_SHIFT
;
298 n
= (gpiop
->flags
& PINMUX_FLAG_DBIT
) >> PINMUX_FLAG_DBIT_SHIFT
;
299 *drp
= pfc
->pdata
->data_regs
+ k
;
304 static int get_config_reg(struct sh_pfc
*pfc
, pinmux_enum_t enum_id
,
305 struct pinmux_cfg_reg
**crp
,
306 int *fieldp
, int *valuep
,
307 unsigned long **cntp
)
309 struct pinmux_cfg_reg
*config_reg
;
310 unsigned long r_width
, f_width
, curr_width
, ncomb
;
311 int k
, m
, n
, pos
, bit_pos
;
315 config_reg
= pfc
->pdata
->cfg_regs
+ k
;
317 r_width
= config_reg
->reg_width
;
318 f_width
= config_reg
->field_width
;
325 for (bit_pos
= 0; bit_pos
< r_width
; bit_pos
+= curr_width
) {
327 curr_width
= f_width
;
329 curr_width
= config_reg
->var_field_width
[m
];
331 ncomb
= 1 << curr_width
;
332 for (n
= 0; n
< ncomb
; n
++) {
333 if (config_reg
->enum_ids
[pos
+ n
] == enum_id
) {
337 *cntp
= &config_reg
->cnt
[m
];
350 int sh_pfc_gpio_to_enum(struct sh_pfc
*pfc
, unsigned gpio
, int pos
,
351 pinmux_enum_t
*enum_idp
)
353 pinmux_enum_t enum_id
= pfc
->pdata
->gpios
[gpio
].enum_id
;
354 pinmux_enum_t
*data
= pfc
->pdata
->gpio_data
;
357 if (!enum_in_range(enum_id
, &pfc
->pdata
->data
)) {
358 if (!enum_in_range(enum_id
, &pfc
->pdata
->mark
)) {
359 pr_err("non data/mark enum_id for gpio %d\n", gpio
);
365 *enum_idp
= data
[pos
+ 1];
369 for (k
= 0; k
< pfc
->pdata
->gpio_data_size
; k
++) {
370 if (data
[k
] == enum_id
) {
371 *enum_idp
= data
[k
+ 1];
376 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio
);
380 int sh_pfc_config_gpio(struct sh_pfc
*pfc
, unsigned gpio
, int pinmux_type
,
383 struct pinmux_cfg_reg
*cr
= NULL
;
384 pinmux_enum_t enum_id
;
385 struct pinmux_range
*range
;
386 int in_range
, pos
, field
, value
;
389 switch (pinmux_type
) {
391 case PINMUX_TYPE_FUNCTION
:
395 case PINMUX_TYPE_OUTPUT
:
396 range
= &pfc
->pdata
->output
;
399 case PINMUX_TYPE_INPUT
:
400 range
= &pfc
->pdata
->input
;
403 case PINMUX_TYPE_INPUT_PULLUP
:
404 range
= &pfc
->pdata
->input_pu
;
407 case PINMUX_TYPE_INPUT_PULLDOWN
:
408 range
= &pfc
->pdata
->input_pd
;
420 pos
= sh_pfc_gpio_to_enum(pfc
, gpio
, pos
, &enum_id
);
427 /* first check if this is a function enum */
428 in_range
= enum_in_range(enum_id
, &pfc
->pdata
->function
);
430 /* not a function enum */
433 * other range exists, so this pin is
434 * a regular GPIO pin that now is being
435 * bound to a specific direction.
437 * for this case we only allow function enums
438 * and the enums that match the other range.
440 in_range
= enum_in_range(enum_id
, range
);
443 * special case pass through for fixed
444 * input-only or output-only pins without
445 * function enum register association.
447 if (in_range
&& enum_id
== range
->force
)
451 * no other range exists, so this pin
452 * must then be of the function type.
454 * allow function type pins to select
455 * any combination of function/in/out
456 * in their MARK lists.
465 if (get_config_reg(pfc
, enum_id
, &cr
,
466 &field
, &value
, &cntp
) != 0)
470 case GPIO_CFG_DRYRUN
:
472 (read_config_reg(pfc
, cr
, field
) != value
))
477 write_config_reg(pfc
, cr
, field
, value
);
492 static int sh_pfc_probe(struct platform_device
*pdev
)
494 struct sh_pfc_platform_data
*pdata
= pdev
->dev
.platform_data
;
499 * Ensure that the type encoding fits
501 BUILD_BUG_ON(PINMUX_FLAG_TYPE
> ((1 << PINMUX_FLAG_DBIT_SHIFT
) - 1));
506 pfc
= devm_kzalloc(&pdev
->dev
, sizeof(pfc
), GFP_KERNEL
);
511 pfc
->dev
= &pdev
->dev
;
513 ret
= pfc_ioremap(pfc
);
514 if (unlikely(ret
< 0))
517 spin_lock_init(&pfc
->lock
);
519 pinctrl_provide_dummies();
520 setup_data_regs(pfc
);
523 * Initialize pinctrl bindings first
525 ret
= sh_pfc_register_pinctrl(pfc
);
526 if (unlikely(ret
!= 0))
529 #ifdef CONFIG_GPIO_SH_PFC
533 ret
= sh_pfc_register_gpiochip(pfc
);
534 if (unlikely(ret
!= 0)) {
536 * If the GPIO chip fails to come up we still leave the
537 * PFC state as it is, given that there are already
538 * extant users of it that have succeeded by this point.
540 pr_notice("failed to init GPIO chip, ignoring...\n");
544 platform_set_drvdata(pdev
, pfc
);
546 pr_info("%s support registered\n", pdata
->name
);
555 static int sh_pfc_remove(struct platform_device
*pdev
)
557 struct sh_pfc
*pfc
= platform_get_drvdata(pdev
);
559 #ifdef CONFIG_GPIO_SH_PFC
560 sh_pfc_unregister_gpiochip(pfc
);
562 sh_pfc_unregister_pinctrl(pfc
);
566 platform_set_drvdata(pdev
, NULL
);
571 static const struct platform_device_id sh_pfc_id_table
[] = {
575 MODULE_DEVICE_TABLE(platform
, sh_pfc_id_table
);
577 static struct platform_driver sh_pfc_driver
= {
578 .probe
= sh_pfc_probe
,
579 .remove
= sh_pfc_remove
,
580 .id_table
= sh_pfc_id_table
,
583 .owner
= THIS_MODULE
,
587 static struct platform_device sh_pfc_device
= {
592 int __init
register_sh_pfc(struct sh_pfc_platform_data
*pdata
)
596 sh_pfc_device
.dev
.platform_data
= pdata
;
598 rc
= platform_driver_register(&sh_pfc_driver
);
600 rc
= platform_device_register(&sh_pfc_device
);
602 platform_driver_unregister(&sh_pfc_driver
);
608 static void __exit
sh_pfc_exit(void)
610 platform_driver_unregister(&sh_pfc_driver
);
612 module_exit(sh_pfc_exit
);
614 MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
615 MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
616 MODULE_LICENSE("GPL v2");