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 int pfc_ioremap(struct sh_pfc
*pfc
)
34 if (!pfc
->pdata
->num_resources
)
37 pfc
->window
= devm_kzalloc(pfc
->dev
, pfc
->pdata
->num_resources
*
38 sizeof(*pfc
->window
), GFP_NOWAIT
);
42 for (k
= 0; k
< pfc
->pdata
->num_resources
; k
++) {
43 res
= pfc
->pdata
->resource
+ k
;
44 WARN_ON(resource_type(res
) != IORESOURCE_MEM
);
45 pfc
->window
[k
].phys
= res
->start
;
46 pfc
->window
[k
].size
= resource_size(res
);
47 pfc
->window
[k
].virt
= devm_ioremap_nocache(pfc
->dev
, res
->start
,
49 if (!pfc
->window
[k
].virt
)
56 static void __iomem
*pfc_phys_to_virt(struct sh_pfc
*pfc
,
57 unsigned long address
)
59 struct pfc_window
*window
;
62 /* scan through physical windows and convert address */
63 for (k
= 0; k
< pfc
->pdata
->num_resources
; k
++) {
64 window
= pfc
->window
+ k
;
66 if (address
< window
->phys
)
69 if (address
>= (window
->phys
+ window
->size
))
72 return window
->virt
+ (address
- window
->phys
);
75 /* no windows defined, register must be 1:1 mapped virt:phys */
76 return (void __iomem
*)address
;
79 static int enum_in_range(pinmux_enum_t enum_id
, struct pinmux_range
*r
)
81 if (enum_id
< r
->begin
)
90 static unsigned long gpio_read_raw_reg(void __iomem
*mapped_reg
,
91 unsigned long reg_width
)
95 return ioread8(mapped_reg
);
97 return ioread16(mapped_reg
);
99 return ioread32(mapped_reg
);
106 static void gpio_write_raw_reg(void __iomem
*mapped_reg
,
107 unsigned long reg_width
,
112 iowrite8(data
, mapped_reg
);
115 iowrite16(data
, mapped_reg
);
118 iowrite32(data
, mapped_reg
);
125 int sh_pfc_read_bit(struct pinmux_data_reg
*dr
, unsigned long in_pos
)
129 pos
= dr
->reg_width
- (in_pos
+ 1);
131 pr_debug("read_bit: addr = %lx, pos = %ld, "
132 "r_width = %ld\n", dr
->reg
, pos
, dr
->reg_width
);
134 return (gpio_read_raw_reg(dr
->mapped_reg
, dr
->reg_width
) >> pos
) & 1;
137 void sh_pfc_write_bit(struct pinmux_data_reg
*dr
, unsigned long in_pos
,
142 pos
= dr
->reg_width
- (in_pos
+ 1);
144 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
146 dr
->reg
, !!value
, pos
, dr
->reg_width
);
149 set_bit(pos
, &dr
->reg_shadow
);
151 clear_bit(pos
, &dr
->reg_shadow
);
153 gpio_write_raw_reg(dr
->mapped_reg
, dr
->reg_width
, dr
->reg_shadow
);
156 static void config_reg_helper(struct sh_pfc
*pfc
,
157 struct pinmux_cfg_reg
*crp
,
158 unsigned long in_pos
,
159 void __iomem
**mapped_regp
,
160 unsigned long *maskp
,
165 *mapped_regp
= pfc_phys_to_virt(pfc
, crp
->reg
);
167 if (crp
->field_width
) {
168 *maskp
= (1 << crp
->field_width
) - 1;
169 *posp
= crp
->reg_width
- ((in_pos
+ 1) * crp
->field_width
);
171 *maskp
= (1 << crp
->var_field_width
[in_pos
]) - 1;
172 *posp
= crp
->reg_width
;
173 for (k
= 0; k
<= in_pos
; k
++)
174 *posp
-= crp
->var_field_width
[k
];
178 static int read_config_reg(struct sh_pfc
*pfc
,
179 struct pinmux_cfg_reg
*crp
,
182 void __iomem
*mapped_reg
;
183 unsigned long mask
, pos
;
185 config_reg_helper(pfc
, crp
, field
, &mapped_reg
, &mask
, &pos
);
187 pr_debug("read_reg: addr = %lx, field = %ld, "
188 "r_width = %ld, f_width = %ld\n",
189 crp
->reg
, field
, crp
->reg_width
, crp
->field_width
);
191 return (gpio_read_raw_reg(mapped_reg
, crp
->reg_width
) >> pos
) & mask
;
194 static void write_config_reg(struct sh_pfc
*pfc
,
195 struct pinmux_cfg_reg
*crp
,
196 unsigned long field
, unsigned long value
)
198 void __iomem
*mapped_reg
;
199 unsigned long mask
, pos
, data
;
201 config_reg_helper(pfc
, crp
, field
, &mapped_reg
, &mask
, &pos
);
203 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
204 "r_width = %ld, f_width = %ld\n",
205 crp
->reg
, value
, field
, crp
->reg_width
, crp
->field_width
);
207 mask
= ~(mask
<< pos
);
208 value
= value
<< pos
;
210 data
= gpio_read_raw_reg(mapped_reg
, crp
->reg_width
);
214 if (pfc
->pdata
->unlock_reg
)
215 gpio_write_raw_reg(pfc_phys_to_virt(pfc
, pfc
->pdata
->unlock_reg
),
218 gpio_write_raw_reg(mapped_reg
, crp
->reg_width
, data
);
221 static int setup_data_reg(struct sh_pfc
*pfc
, unsigned gpio
)
223 struct pinmux_gpio
*gpiop
= &pfc
->pdata
->gpios
[gpio
];
224 struct pinmux_data_reg
*data_reg
;
227 if (!enum_in_range(gpiop
->enum_id
, &pfc
->pdata
->data
))
232 data_reg
= pfc
->pdata
->data_regs
+ k
;
234 if (!data_reg
->reg_width
)
237 data_reg
->mapped_reg
= pfc_phys_to_virt(pfc
, data_reg
->reg
);
239 for (n
= 0; n
< data_reg
->reg_width
; n
++) {
240 if (data_reg
->enum_ids
[n
] == gpiop
->enum_id
) {
241 gpiop
->flags
&= ~PINMUX_FLAG_DREG
;
242 gpiop
->flags
|= (k
<< PINMUX_FLAG_DREG_SHIFT
);
243 gpiop
->flags
&= ~PINMUX_FLAG_DBIT
;
244 gpiop
->flags
|= (n
<< PINMUX_FLAG_DBIT_SHIFT
);
256 static void setup_data_regs(struct sh_pfc
*pfc
)
258 struct pinmux_data_reg
*drp
;
261 for (k
= pfc
->pdata
->first_gpio
; k
<= pfc
->pdata
->last_gpio
; k
++)
262 setup_data_reg(pfc
, k
);
266 drp
= pfc
->pdata
->data_regs
+ k
;
271 drp
->reg_shadow
= gpio_read_raw_reg(drp
->mapped_reg
,
277 int sh_pfc_get_data_reg(struct sh_pfc
*pfc
, unsigned gpio
,
278 struct pinmux_data_reg
**drp
, int *bitp
)
280 struct pinmux_gpio
*gpiop
= &pfc
->pdata
->gpios
[gpio
];
283 if (!enum_in_range(gpiop
->enum_id
, &pfc
->pdata
->data
))
286 k
= (gpiop
->flags
& PINMUX_FLAG_DREG
) >> PINMUX_FLAG_DREG_SHIFT
;
287 n
= (gpiop
->flags
& PINMUX_FLAG_DBIT
) >> PINMUX_FLAG_DBIT_SHIFT
;
288 *drp
= pfc
->pdata
->data_regs
+ k
;
293 static int get_config_reg(struct sh_pfc
*pfc
, pinmux_enum_t enum_id
,
294 struct pinmux_cfg_reg
**crp
,
295 int *fieldp
, int *valuep
,
296 unsigned long **cntp
)
298 struct pinmux_cfg_reg
*config_reg
;
299 unsigned long r_width
, f_width
, curr_width
, ncomb
;
300 int k
, m
, n
, pos
, bit_pos
;
304 config_reg
= pfc
->pdata
->cfg_regs
+ k
;
306 r_width
= config_reg
->reg_width
;
307 f_width
= config_reg
->field_width
;
314 for (bit_pos
= 0; bit_pos
< r_width
; bit_pos
+= curr_width
) {
316 curr_width
= f_width
;
318 curr_width
= config_reg
->var_field_width
[m
];
320 ncomb
= 1 << curr_width
;
321 for (n
= 0; n
< ncomb
; n
++) {
322 if (config_reg
->enum_ids
[pos
+ n
] == enum_id
) {
326 *cntp
= &config_reg
->cnt
[m
];
339 int sh_pfc_gpio_to_enum(struct sh_pfc
*pfc
, unsigned gpio
, int pos
,
340 pinmux_enum_t
*enum_idp
)
342 pinmux_enum_t enum_id
= pfc
->pdata
->gpios
[gpio
].enum_id
;
343 pinmux_enum_t
*data
= pfc
->pdata
->gpio_data
;
346 if (!enum_in_range(enum_id
, &pfc
->pdata
->data
)) {
347 if (!enum_in_range(enum_id
, &pfc
->pdata
->mark
)) {
348 pr_err("non data/mark enum_id for gpio %d\n", gpio
);
354 *enum_idp
= data
[pos
+ 1];
358 for (k
= 0; k
< pfc
->pdata
->gpio_data_size
; k
++) {
359 if (data
[k
] == enum_id
) {
360 *enum_idp
= data
[k
+ 1];
365 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio
);
369 int sh_pfc_config_gpio(struct sh_pfc
*pfc
, unsigned gpio
, int pinmux_type
,
372 struct pinmux_cfg_reg
*cr
= NULL
;
373 pinmux_enum_t enum_id
;
374 struct pinmux_range
*range
;
375 int in_range
, pos
, field
, value
;
378 switch (pinmux_type
) {
380 case PINMUX_TYPE_FUNCTION
:
384 case PINMUX_TYPE_OUTPUT
:
385 range
= &pfc
->pdata
->output
;
388 case PINMUX_TYPE_INPUT
:
389 range
= &pfc
->pdata
->input
;
392 case PINMUX_TYPE_INPUT_PULLUP
:
393 range
= &pfc
->pdata
->input_pu
;
396 case PINMUX_TYPE_INPUT_PULLDOWN
:
397 range
= &pfc
->pdata
->input_pd
;
409 pos
= sh_pfc_gpio_to_enum(pfc
, gpio
, pos
, &enum_id
);
416 /* first check if this is a function enum */
417 in_range
= enum_in_range(enum_id
, &pfc
->pdata
->function
);
419 /* not a function enum */
422 * other range exists, so this pin is
423 * a regular GPIO pin that now is being
424 * bound to a specific direction.
426 * for this case we only allow function enums
427 * and the enums that match the other range.
429 in_range
= enum_in_range(enum_id
, range
);
432 * special case pass through for fixed
433 * input-only or output-only pins without
434 * function enum register association.
436 if (in_range
&& enum_id
== range
->force
)
440 * no other range exists, so this pin
441 * must then be of the function type.
443 * allow function type pins to select
444 * any combination of function/in/out
445 * in their MARK lists.
454 if (get_config_reg(pfc
, enum_id
, &cr
,
455 &field
, &value
, &cntp
) != 0)
459 case GPIO_CFG_DRYRUN
:
461 (read_config_reg(pfc
, cr
, field
) != value
))
466 write_config_reg(pfc
, cr
, field
, value
);
481 static int sh_pfc_probe(struct platform_device
*pdev
)
483 struct sh_pfc_platform_data
*pdata
= pdev
->dev
.platform_data
;
488 * Ensure that the type encoding fits
490 BUILD_BUG_ON(PINMUX_FLAG_TYPE
> ((1 << PINMUX_FLAG_DBIT_SHIFT
) - 1));
495 pfc
= devm_kzalloc(&pdev
->dev
, sizeof(pfc
), GFP_KERNEL
);
500 pfc
->dev
= &pdev
->dev
;
502 ret
= pfc_ioremap(pfc
);
503 if (unlikely(ret
< 0))
506 spin_lock_init(&pfc
->lock
);
508 pinctrl_provide_dummies();
509 setup_data_regs(pfc
);
512 * Initialize pinctrl bindings first
514 ret
= sh_pfc_register_pinctrl(pfc
);
515 if (unlikely(ret
!= 0))
518 #ifdef CONFIG_GPIO_SH_PFC
522 ret
= sh_pfc_register_gpiochip(pfc
);
523 if (unlikely(ret
!= 0)) {
525 * If the GPIO chip fails to come up we still leave the
526 * PFC state as it is, given that there are already
527 * extant users of it that have succeeded by this point.
529 pr_notice("failed to init GPIO chip, ignoring...\n");
533 platform_set_drvdata(pdev
, pfc
);
535 pr_info("%s support registered\n", pdata
->name
);
540 static int sh_pfc_remove(struct platform_device
*pdev
)
542 struct sh_pfc
*pfc
= platform_get_drvdata(pdev
);
544 #ifdef CONFIG_GPIO_SH_PFC
545 sh_pfc_unregister_gpiochip(pfc
);
547 sh_pfc_unregister_pinctrl(pfc
);
549 platform_set_drvdata(pdev
, NULL
);
554 static const struct platform_device_id sh_pfc_id_table
[] = {
558 MODULE_DEVICE_TABLE(platform
, sh_pfc_id_table
);
560 static struct platform_driver sh_pfc_driver
= {
561 .probe
= sh_pfc_probe
,
562 .remove
= sh_pfc_remove
,
563 .id_table
= sh_pfc_id_table
,
566 .owner
= THIS_MODULE
,
570 static struct platform_device sh_pfc_device
= {
575 int __init
register_sh_pfc(struct sh_pfc_platform_data
*pdata
)
579 sh_pfc_device
.dev
.platform_data
= pdata
;
581 rc
= platform_driver_register(&sh_pfc_driver
);
583 rc
= platform_device_register(&sh_pfc_device
);
585 platform_driver_unregister(&sh_pfc_driver
);
591 static void __exit
sh_pfc_exit(void)
593 platform_driver_unregister(&sh_pfc_driver
);
595 module_exit(sh_pfc_exit
);
597 MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
598 MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
599 MODULE_LICENSE("GPL v2");