1 // SPDX-License-Identifier: GPL-2.0
3 * Pin Control and GPIO driver for SuperH Pin Function Controller.
5 * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
7 * Copyright (C) 2008 Magnus Damm
8 * Copyright (C) 2009 - 2012 Paul Mundt
11 #define DRV_NAME "sh-pfc"
13 #include <linux/bitops.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
18 #include <linux/ioport.h>
19 #include <linux/kernel.h>
20 #include <linux/math.h>
22 #include <linux/of_device.h>
23 #include <linux/pinctrl/machine.h>
24 #include <linux/platform_device.h>
25 #include <linux/psci.h>
26 #include <linux/slab.h>
27 #include <linux/sys_soc.h>
31 static int sh_pfc_map_resources(struct sh_pfc *pfc,
32 struct platform_device *pdev)
34 struct sh_pfc_window *windows;
35 unsigned int *irqs = NULL;
36 unsigned int num_windows;
41 /* Count the MEM and IRQ resources. */
42 for (num_windows = 0;; num_windows++) {
43 res = platform_get_resource(pdev, IORESOURCE_MEM, num_windows);
50 num_irqs = platform_irq_count(pdev);
54 /* Allocate memory windows and IRQs arrays. */
55 windows = devm_kcalloc(pfc->dev, num_windows, sizeof(*windows),
60 pfc->num_windows = num_windows;
61 pfc->windows = windows;
64 irqs = devm_kcalloc(pfc->dev, num_irqs, sizeof(*irqs),
69 pfc->num_irqs = num_irqs;
74 for (i = 0; i < num_windows; i++) {
75 windows->virt = devm_platform_get_and_ioremap_resource(pdev, i, &res);
76 if (IS_ERR(windows->virt))
78 windows->phys = res->start;
79 windows->size = resource_size(res);
82 for (i = 0; i < num_irqs; i++)
83 *irqs++ = platform_get_irq(pdev, i);
88 static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc, u32 reg)
90 struct sh_pfc_window *window;
91 phys_addr_t address = reg;
94 /* scan through physical windows and convert address */
95 for (i = 0; i < pfc->num_windows; i++) {
96 window = pfc->windows + i;
98 if (address < window->phys)
101 if (address >= (window->phys + window->size))
104 return window->virt + (address - window->phys);
111 int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
116 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
117 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
119 if (pin <= range->end)
120 return pin >= range->start
121 ? offset + pin - range->start : -1;
123 offset += range->end - range->start + 1;
129 static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
131 if (enum_id < r->begin)
134 if (enum_id > r->end)
140 u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
144 return ioread8(mapped_reg);
146 return ioread16(mapped_reg);
148 return ioread32(mapped_reg);
155 void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
160 iowrite8(data, mapped_reg);
163 iowrite16(data, mapped_reg);
166 iowrite32(data, mapped_reg);
173 u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
175 return sh_pfc_read_raw_reg(sh_pfc_phys_to_virt(pfc, reg), 32);
178 static void sh_pfc_unlock_reg(struct sh_pfc *pfc, u32 reg, u32 data)
182 if (!pfc->info->unlock_reg)
185 if (pfc->info->unlock_reg >= 0x80000000UL)
186 unlock = pfc->info->unlock_reg;
188 /* unlock_reg is a mask */
189 unlock = reg & ~pfc->info->unlock_reg;
191 sh_pfc_write_raw_reg(sh_pfc_phys_to_virt(pfc, unlock), 32, ~data);
194 void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
196 sh_pfc_unlock_reg(pfc, reg, data);
197 sh_pfc_write_raw_reg(sh_pfc_phys_to_virt(pfc, reg), 32, data);
200 static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
201 const struct pinmux_cfg_reg *crp,
203 void __iomem **mapped_regp, u32 *maskp,
208 *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
210 if (crp->field_width) {
211 *maskp = (1 << crp->field_width) - 1;
212 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
214 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
215 *posp = crp->reg_width;
216 for (k = 0; k <= in_pos; k++)
217 *posp -= abs(crp->var_field_width[k]);
221 static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
222 const struct pinmux_cfg_reg *crp,
223 unsigned int field, u32 value)
225 void __iomem *mapped_reg;
229 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
231 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
232 "r_width = %u, f_width = %u\n",
233 crp->reg, value, field, crp->reg_width, hweight32(mask));
235 mask = ~(mask << pos);
236 value = value << pos;
238 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
242 sh_pfc_unlock_reg(pfc, crp->reg, data);
243 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
246 static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
247 const struct pinmux_cfg_reg **crp,
248 unsigned int *fieldp, u32 *valuep)
253 const struct pinmux_cfg_reg *config_reg =
254 pfc->info->cfg_regs + k;
255 unsigned int r_width = config_reg->reg_width;
256 unsigned int f_width = config_reg->field_width;
257 unsigned int curr_width;
258 unsigned int bit_pos;
259 unsigned int pos = 0;
265 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width, m++) {
270 curr_width = f_width;
272 curr_width = abs(config_reg->var_field_width[m]);
273 if (config_reg->var_field_width[m] < 0)
277 ncomb = 1 << curr_width;
278 for (n = 0; n < ncomb; n++) {
279 if (config_reg->enum_ids[pos + n] == enum_id) {
294 static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
297 const u16 *data = pfc->info->pinmux_data;
301 *enum_idp = data[pos + 1];
305 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
306 if (data[k] == mark) {
307 *enum_idp = data[k + 1];
312 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
317 int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
319 const struct pinmux_range *range;
322 switch (pinmux_type) {
323 case PINMUX_TYPE_GPIO:
324 case PINMUX_TYPE_FUNCTION:
328 #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
329 case PINMUX_TYPE_OUTPUT:
330 range = &pfc->info->output;
333 case PINMUX_TYPE_INPUT:
334 range = &pfc->info->input;
336 #endif /* CONFIG_PINCTRL_SH_PFC_GPIO */
342 /* Iterate over all the configuration fields we need to update. */
344 const struct pinmux_cfg_reg *cr;
351 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
358 /* Check if the configuration field selects a function. If it
359 * doesn't, skip the field if it's not applicable to the
360 * requested pinmux type.
362 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
364 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
365 /* Functions are allowed to modify all
369 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
370 /* Input/output types can only modify fields
371 * that correspond to their respective ranges.
373 in_range = sh_pfc_enum_in_range(enum_id, range);
376 * special case pass through for fixed
377 * input-only or output-only pins without
378 * function enum register association.
380 if (in_range && enum_id == range->force)
383 /* GPIOs are only allowed to modify function fields. */
389 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
393 sh_pfc_write_config_reg(pfc, cr, field, value);
399 static int sh_pfc_init_ranges(struct sh_pfc *pfc)
401 struct sh_pfc_pin_range *range;
402 unsigned int nr_ranges;
405 if (pfc->info->pins[0].pin == (u16)-1) {
406 /* Pin number -1 denotes that the SoC doesn't report pin numbers
407 * in its pin arrays yet. Consider the pin numbers range as
408 * continuous and allocate a single range.
411 pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
413 if (pfc->ranges == NULL)
416 pfc->ranges->start = 0;
417 pfc->ranges->end = pfc->info->nr_pins - 1;
418 pfc->nr_gpio_pins = pfc->info->nr_pins;
423 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
424 * be sorted by pin numbers, and pins without a GPIO port must come
427 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
428 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
432 pfc->nr_ranges = nr_ranges;
433 pfc->ranges = devm_kcalloc(pfc->dev, nr_ranges, sizeof(*pfc->ranges),
435 if (pfc->ranges == NULL)
439 range->start = pfc->info->pins[0].pin;
441 for (i = 1; i < pfc->info->nr_pins; ++i) {
442 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
445 range->end = pfc->info->pins[i-1].pin;
446 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
447 pfc->nr_gpio_pins = range->end + 1;
450 range->start = pfc->info->pins[i].pin;
453 range->end = pfc->info->pins[i-1].pin;
454 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
455 pfc->nr_gpio_pins = range->end + 1;
461 static const struct of_device_id sh_pfc_of_table[] = {
462 #ifdef CONFIG_PINCTRL_PFC_EMEV2
464 .compatible = "renesas,pfc-emev2",
465 .data = &emev2_pinmux_info,
468 #ifdef CONFIG_PINCTRL_PFC_R8A73A4
470 .compatible = "renesas,pfc-r8a73a4",
471 .data = &r8a73a4_pinmux_info,
474 #ifdef CONFIG_PINCTRL_PFC_R8A7740
476 .compatible = "renesas,pfc-r8a7740",
477 .data = &r8a7740_pinmux_info,
480 #ifdef CONFIG_PINCTRL_PFC_R8A7742
482 .compatible = "renesas,pfc-r8a7742",
483 .data = &r8a7742_pinmux_info,
486 #ifdef CONFIG_PINCTRL_PFC_R8A7743
488 .compatible = "renesas,pfc-r8a7743",
489 .data = &r8a7743_pinmux_info,
492 #ifdef CONFIG_PINCTRL_PFC_R8A7744
494 .compatible = "renesas,pfc-r8a7744",
495 .data = &r8a7744_pinmux_info,
498 #ifdef CONFIG_PINCTRL_PFC_R8A7745
500 .compatible = "renesas,pfc-r8a7745",
501 .data = &r8a7745_pinmux_info,
504 #ifdef CONFIG_PINCTRL_PFC_R8A77470
506 .compatible = "renesas,pfc-r8a77470",
507 .data = &r8a77470_pinmux_info,
510 #ifdef CONFIG_PINCTRL_PFC_R8A774A1
512 .compatible = "renesas,pfc-r8a774a1",
513 .data = &r8a774a1_pinmux_info,
516 #ifdef CONFIG_PINCTRL_PFC_R8A774B1
518 .compatible = "renesas,pfc-r8a774b1",
519 .data = &r8a774b1_pinmux_info,
522 #ifdef CONFIG_PINCTRL_PFC_R8A774C0
524 .compatible = "renesas,pfc-r8a774c0",
525 .data = &r8a774c0_pinmux_info,
528 #ifdef CONFIG_PINCTRL_PFC_R8A774E1
530 .compatible = "renesas,pfc-r8a774e1",
531 .data = &r8a774e1_pinmux_info,
534 #ifdef CONFIG_PINCTRL_PFC_R8A7778
536 .compatible = "renesas,pfc-r8a7778",
537 .data = &r8a7778_pinmux_info,
540 #ifdef CONFIG_PINCTRL_PFC_R8A7779
542 .compatible = "renesas,pfc-r8a7779",
543 .data = &r8a7779_pinmux_info,
546 #ifdef CONFIG_PINCTRL_PFC_R8A7790
548 .compatible = "renesas,pfc-r8a7790",
549 .data = &r8a7790_pinmux_info,
552 #ifdef CONFIG_PINCTRL_PFC_R8A7791
554 .compatible = "renesas,pfc-r8a7791",
555 .data = &r8a7791_pinmux_info,
558 #ifdef CONFIG_PINCTRL_PFC_R8A7792
560 .compatible = "renesas,pfc-r8a7792",
561 .data = &r8a7792_pinmux_info,
564 #ifdef CONFIG_PINCTRL_PFC_R8A7793
566 .compatible = "renesas,pfc-r8a7793",
567 .data = &r8a7793_pinmux_info,
570 #ifdef CONFIG_PINCTRL_PFC_R8A7794
572 .compatible = "renesas,pfc-r8a7794",
573 .data = &r8a7794_pinmux_info,
577 * Both r8a7795 entries must be present to make sanity checks work, but only
578 * the first entry is actually used.
579 * R-Car H3 ES1.x is matched using soc_device_match() instead.
581 #ifdef CONFIG_PINCTRL_PFC_R8A77951
583 .compatible = "renesas,pfc-r8a7795",
584 .data = &r8a77951_pinmux_info,
587 #ifdef CONFIG_PINCTRL_PFC_R8A77950
589 .compatible = "renesas,pfc-r8a7795",
590 .data = &r8a77950_pinmux_info,
593 #ifdef CONFIG_PINCTRL_PFC_R8A77960
595 .compatible = "renesas,pfc-r8a7796",
596 .data = &r8a77960_pinmux_info,
599 #ifdef CONFIG_PINCTRL_PFC_R8A77961
601 .compatible = "renesas,pfc-r8a77961",
602 .data = &r8a77961_pinmux_info,
605 #ifdef CONFIG_PINCTRL_PFC_R8A77965
607 .compatible = "renesas,pfc-r8a77965",
608 .data = &r8a77965_pinmux_info,
611 #ifdef CONFIG_PINCTRL_PFC_R8A77970
613 .compatible = "renesas,pfc-r8a77970",
614 .data = &r8a77970_pinmux_info,
617 #ifdef CONFIG_PINCTRL_PFC_R8A77980
619 .compatible = "renesas,pfc-r8a77980",
620 .data = &r8a77980_pinmux_info,
623 #ifdef CONFIG_PINCTRL_PFC_R8A77990
625 .compatible = "renesas,pfc-r8a77990",
626 .data = &r8a77990_pinmux_info,
629 #ifdef CONFIG_PINCTRL_PFC_R8A77995
631 .compatible = "renesas,pfc-r8a77995",
632 .data = &r8a77995_pinmux_info,
635 #ifdef CONFIG_PINCTRL_PFC_R8A779A0
637 .compatible = "renesas,pfc-r8a779a0",
638 .data = &r8a779a0_pinmux_info,
641 #ifdef CONFIG_PINCTRL_PFC_R8A779F0
643 .compatible = "renesas,pfc-r8a779f0",
644 .data = &r8a779f0_pinmux_info,
647 #ifdef CONFIG_PINCTRL_PFC_R8A779G0
649 .compatible = "renesas,pfc-r8a779g0",
650 .data = &r8a779g0_pinmux_info,
653 #ifdef CONFIG_PINCTRL_PFC_SH73A0
655 .compatible = "renesas,pfc-sh73a0",
656 .data = &sh73a0_pinmux_info,
663 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM_PSCI_FW)
664 static void sh_pfc_nop_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
668 static void sh_pfc_save_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
670 pfc->saved_regs[idx] = sh_pfc_read(pfc, reg);
673 static void sh_pfc_restore_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
675 sh_pfc_write(pfc, reg, pfc->saved_regs[idx]);
678 static unsigned int sh_pfc_walk_regs(struct sh_pfc *pfc,
679 void (*do_reg)(struct sh_pfc *pfc, u32 reg, unsigned int idx))
681 unsigned int i, n = 0;
683 if (pfc->info->cfg_regs)
684 for (i = 0; pfc->info->cfg_regs[i].reg; i++)
685 do_reg(pfc, pfc->info->cfg_regs[i].reg, n++);
687 if (pfc->info->drive_regs)
688 for (i = 0; pfc->info->drive_regs[i].reg; i++)
689 do_reg(pfc, pfc->info->drive_regs[i].reg, n++);
691 if (pfc->info->bias_regs)
692 for (i = 0; pfc->info->bias_regs[i].puen ||
693 pfc->info->bias_regs[i].pud; i++) {
694 if (pfc->info->bias_regs[i].puen)
695 do_reg(pfc, pfc->info->bias_regs[i].puen, n++);
696 if (pfc->info->bias_regs[i].pud)
697 do_reg(pfc, pfc->info->bias_regs[i].pud, n++);
700 if (pfc->info->ioctrl_regs)
701 for (i = 0; pfc->info->ioctrl_regs[i].reg; i++)
702 do_reg(pfc, pfc->info->ioctrl_regs[i].reg, n++);
707 static int sh_pfc_suspend_init(struct sh_pfc *pfc)
711 /* This is the best we can do to check for the presence of PSCI */
712 if (!psci_ops.cpu_suspend)
715 n = sh_pfc_walk_regs(pfc, sh_pfc_nop_reg);
719 pfc->saved_regs = devm_kmalloc_array(pfc->dev, n,
720 sizeof(*pfc->saved_regs),
722 if (!pfc->saved_regs)
725 dev_dbg(pfc->dev, "Allocated space to save %u regs\n", n);
729 static int sh_pfc_suspend_noirq(struct device *dev)
731 struct sh_pfc *pfc = dev_get_drvdata(dev);
734 sh_pfc_walk_regs(pfc, sh_pfc_save_reg);
738 static int sh_pfc_resume_noirq(struct device *dev)
740 struct sh_pfc *pfc = dev_get_drvdata(dev);
743 sh_pfc_walk_regs(pfc, sh_pfc_restore_reg);
747 static const struct dev_pm_ops sh_pfc_pm = {
748 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sh_pfc_suspend_noirq, sh_pfc_resume_noirq)
750 #define DEV_PM_OPS &sh_pfc_pm
752 static int sh_pfc_suspend_init(struct sh_pfc *pfc) { return 0; }
753 #define DEV_PM_OPS NULL
754 #endif /* CONFIG_PM_SLEEP && CONFIG_ARM_PSCI_FW */
757 #define SH_PFC_MAX_REGS 300
758 #define SH_PFC_MAX_ENUMS 5000
760 static unsigned int sh_pfc_errors __initdata;
761 static unsigned int sh_pfc_warnings __initdata;
762 static bool sh_pfc_bias_done __initdata;
763 static bool sh_pfc_drive_done __initdata;
764 static bool sh_pfc_power_done __initdata;
768 } *sh_pfc_regs __initdata;
769 static u32 sh_pfc_num_regs __initdata;
770 static u16 *sh_pfc_enums __initdata;
771 static u32 sh_pfc_num_enums __initdata;
773 #define sh_pfc_err(fmt, ...) \
775 pr_err("%s: " fmt, drvname, ##__VA_ARGS__); \
779 #define sh_pfc_err_once(type, fmt, ...) \
781 if (!sh_pfc_ ## type ## _done) { \
782 sh_pfc_ ## type ## _done = true; \
783 sh_pfc_err(fmt, ##__VA_ARGS__); \
787 #define sh_pfc_warn(fmt, ...) \
789 pr_warn("%s: " fmt, drvname, ##__VA_ARGS__); \
793 static bool __init is0s(const u16 *enum_ids, unsigned int n)
797 for (i = 0; i < n; i++)
804 static bool __init same_name(const char *a, const char *b)
806 return a && b && !strcmp(a, b);
809 static void __init sh_pfc_check_reg(const char *drvname, u32 reg, u32 bits)
813 for (i = 0; i < sh_pfc_num_regs; i++) {
814 if (reg != sh_pfc_regs[i].reg)
817 if (bits & sh_pfc_regs[i].bits)
818 sh_pfc_err("reg 0x%x: bits 0x%x conflict\n", reg,
819 bits & sh_pfc_regs[i].bits);
821 sh_pfc_regs[i].bits |= bits;
825 if (sh_pfc_num_regs == SH_PFC_MAX_REGS) {
826 pr_warn_once("%s: Please increase SH_PFC_MAX_REGS\n", drvname);
830 sh_pfc_regs[sh_pfc_num_regs].reg = reg;
831 sh_pfc_regs[sh_pfc_num_regs].bits = bits;
835 static int __init sh_pfc_check_enum(const char *drvname, u16 enum_id)
839 for (i = 0; i < sh_pfc_num_enums; i++) {
840 if (enum_id == sh_pfc_enums[i])
844 if (sh_pfc_num_enums == SH_PFC_MAX_ENUMS) {
845 pr_warn_once("%s: Please increase SH_PFC_MAX_ENUMS\n", drvname);
849 sh_pfc_enums[sh_pfc_num_enums++] = enum_id;
853 static void __init sh_pfc_check_reg_enums(const char *drvname, u32 reg,
854 const u16 *enums, unsigned int n)
858 for (i = 0; i < n; i++) {
859 if (enums[i] && sh_pfc_check_enum(drvname, enums[i]))
860 sh_pfc_err("reg 0x%x enum_id %u conflict\n", reg,
865 static const struct sh_pfc_pin __init *sh_pfc_find_pin(
866 const struct sh_pfc_soc_info *info, u32 reg, unsigned int pin)
868 const char *drvname = info->name;
871 if (pin == SH_PFC_PIN_NONE)
874 for (i = 0; i < info->nr_pins; i++) {
875 if (pin == info->pins[i].pin)
876 return &info->pins[i];
879 sh_pfc_err("reg 0x%x: pin %u not found\n", reg, pin);
883 static void __init sh_pfc_check_cfg_reg(const char *drvname,
884 const struct pinmux_cfg_reg *cfg_reg)
886 unsigned int i, n, rw, r;
889 sh_pfc_check_reg(drvname, cfg_reg->reg,
890 GENMASK(cfg_reg->reg_width - 1, 0));
892 if (cfg_reg->field_width) {
893 fw = cfg_reg->field_width;
894 n = (cfg_reg->reg_width / fw) << fw;
895 for (i = 0, r = 0; i < n; i += 1 << fw) {
896 if (is0s(&cfg_reg->enum_ids[i], 1 << fw))
900 if ((r << fw) * sizeof(u16) > cfg_reg->reg_width / fw)
901 sh_pfc_warn("reg 0x%x can be described with variable-width reserved fields\n",
904 /* Skip field checks (done at build time) */
908 for (i = 0, n = 0, rw = 0; (fw = cfg_reg->var_field_width[i]); i++) {
912 if (is0s(&cfg_reg->enum_ids[n], 1 << fw))
913 sh_pfc_warn("reg 0x%x: field [%u:%u] can be described as reserved\n",
914 cfg_reg->reg, rw, rw + fw - 1);
920 if (rw != cfg_reg->reg_width)
921 sh_pfc_err("reg 0x%x: var_field_width declares %u instead of %u bits\n",
922 cfg_reg->reg, rw, cfg_reg->reg_width);
924 if (n != cfg_reg->nr_enum_ids)
925 sh_pfc_err("reg 0x%x: enum_ids[] has %u instead of %u values\n",
926 cfg_reg->reg, cfg_reg->nr_enum_ids, n);
929 sh_pfc_check_reg_enums(drvname, cfg_reg->reg, cfg_reg->enum_ids, n);
932 static void __init sh_pfc_check_drive_reg(const struct sh_pfc_soc_info *info,
933 const struct pinmux_drive_reg *drive)
935 const char *drvname = info->name;
936 const struct sh_pfc_pin *pin;
939 for (i = 0; i < ARRAY_SIZE(drive->fields); i++) {
940 const struct pinmux_drive_reg_field *field = &drive->fields[i];
942 if (!field->pin && !field->offset && !field->size)
945 sh_pfc_check_reg(info->name, drive->reg,
946 GENMASK(field->offset + field->size - 1,
949 pin = sh_pfc_find_pin(info, drive->reg, field->pin);
950 if (pin && !(pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH))
951 sh_pfc_err("drive_reg 0x%x: field %u: pin %s lacks SH_PFC_PIN_CFG_DRIVE_STRENGTH flag\n",
952 drive->reg, i, pin->name);
956 static void __init sh_pfc_check_bias_reg(const struct sh_pfc_soc_info *info,
957 const struct pinmux_bias_reg *bias)
959 const char *drvname = info->name;
960 const struct sh_pfc_pin *pin;
964 for (i = 0, bits = 0; i < ARRAY_SIZE(bias->pins); i++)
965 if (bias->pins[i] != SH_PFC_PIN_NONE)
969 sh_pfc_check_reg(info->name, bias->puen, bits);
971 sh_pfc_check_reg(info->name, bias->pud, bits);
972 for (i = 0; i < ARRAY_SIZE(bias->pins); i++) {
973 pin = sh_pfc_find_pin(info, bias->puen, bias->pins[i]);
977 if (bias->puen && bias->pud) {
979 * Pull-enable and pull-up/down control registers
980 * As some SoCs have pins that support only pull-up
981 * or pull-down, we just check for one of them
983 if (!(pin->configs & SH_PFC_PIN_CFG_PULL_UP_DOWN))
984 sh_pfc_err("bias_reg 0x%x:%u: pin %s lacks one or more SH_PFC_PIN_CFG_PULL_* flags\n",
985 bias->puen, i, pin->name);
986 } else if (bias->puen) {
987 /* Pull-up control register only */
988 if (!(pin->configs & SH_PFC_PIN_CFG_PULL_UP))
989 sh_pfc_err("bias_reg 0x%x:%u: pin %s lacks SH_PFC_PIN_CFG_PULL_UP flag\n",
990 bias->puen, i, pin->name);
991 } else if (bias->pud) {
992 /* Pull-down control register only */
993 if (!(pin->configs & SH_PFC_PIN_CFG_PULL_DOWN))
994 sh_pfc_err("bias_reg 0x%x:%u: pin %s lacks SH_PFC_PIN_CFG_PULL_DOWN flag\n",
995 bias->pud, i, pin->name);
1000 static void __init sh_pfc_compare_groups(const char *drvname,
1001 const struct sh_pfc_pin_group *a,
1002 const struct sh_pfc_pin_group *b)
1007 if (same_name(a->name, b->name))
1008 sh_pfc_err("group %s: name conflict\n", a->name);
1010 if (a->nr_pins > b->nr_pins)
1013 len = a->nr_pins * sizeof(a->pins[0]);
1014 for (i = 0; i <= b->nr_pins - a->nr_pins; i++) {
1015 if (a->pins == b->pins + i || a->mux == b->mux + i ||
1016 memcmp(a->pins, b->pins + i, len) ||
1017 memcmp(a->mux, b->mux + i, len))
1020 if (a->nr_pins == b->nr_pins)
1021 sh_pfc_warn("group %s can be an alias for %s\n",
1024 sh_pfc_warn("group %s is a subset of %s\n", a->name,
1029 static void __init sh_pfc_check_info(const struct sh_pfc_soc_info *info)
1031 const struct pinmux_drive_reg *drive_regs = info->drive_regs;
1032 #define drive_nfields ARRAY_SIZE(drive_regs->fields)
1033 #define drive_ofs(i) drive_regs[(i) / drive_nfields]
1034 #define drive_reg(i) drive_ofs(i).reg
1035 #define drive_bit(i) ((i) % drive_nfields)
1036 #define drive_field(i) drive_ofs(i).fields[drive_bit(i)]
1037 const struct pinmux_bias_reg *bias_regs = info->bias_regs;
1038 #define bias_npins ARRAY_SIZE(bias_regs->pins)
1039 #define bias_ofs(i) bias_regs[(i) / bias_npins]
1040 #define bias_puen(i) bias_ofs(i).puen
1041 #define bias_pud(i) bias_ofs(i).pud
1042 #define bias_bit(i) ((i) % bias_npins)
1043 #define bias_pin(i) bias_ofs(i).pins[bias_bit(i)]
1044 const char *drvname = info->name;
1045 unsigned int *refcnts;
1046 unsigned int i, j, k;
1048 pr_info("sh_pfc: Checking %s\n", drvname);
1049 sh_pfc_num_regs = 0;
1050 sh_pfc_num_enums = 0;
1051 sh_pfc_bias_done = false;
1052 sh_pfc_drive_done = false;
1053 sh_pfc_power_done = false;
1056 for (i = 0; i < info->nr_pins; i++) {
1057 const struct sh_pfc_pin *pin = &info->pins[i];
1061 sh_pfc_err("empty pin %u\n", i);
1064 for (j = 0; j < i; j++) {
1065 const struct sh_pfc_pin *pin2 = &info->pins[j];
1067 if (same_name(pin->name, pin2->name))
1068 sh_pfc_err("pin %s: name conflict\n",
1071 if (pin->pin != (u16)-1 && pin->pin == pin2->pin)
1072 sh_pfc_err("pin %s/%s: pin %u conflict\n",
1073 pin->name, pin2->name, pin->pin);
1075 if (pin->enum_id && pin->enum_id == pin2->enum_id)
1076 sh_pfc_err("pin %s/%s: enum_id %u conflict\n",
1077 pin->name, pin2->name,
1081 if (pin->configs & SH_PFC_PIN_CFG_PULL_UP_DOWN) {
1082 if (!info->ops || !info->ops->get_bias ||
1083 !info->ops->set_bias)
1084 sh_pfc_err_once(bias, "SH_PFC_PIN_CFG_PULL_* flag set but .[gs]et_bias() not implemented\n");
1087 (!info->ops || !info->ops->pin_to_portcr))
1088 sh_pfc_err_once(bias, "SH_PFC_PIN_CFG_PULL_UP flag set but no bias_regs defined and .pin_to_portcr() not implemented\n");
1091 if ((pin->configs & SH_PFC_PIN_CFG_PULL_UP_DOWN) && bias_regs) {
1092 const struct pinmux_bias_reg *bias_reg =
1093 rcar_pin_to_bias_reg(info, pin->pin, &x);
1096 ((pin->configs & SH_PFC_PIN_CFG_PULL_UP) &&
1098 sh_pfc_err("pin %s: SH_PFC_PIN_CFG_PULL_UP flag set but pin not in bias_regs\n",
1102 ((pin->configs & SH_PFC_PIN_CFG_PULL_DOWN) &&
1104 sh_pfc_err("pin %s: SH_PFC_PIN_CFG_PULL_DOWN flag set but pin not in bias_regs\n",
1108 if (pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH) {
1110 sh_pfc_err_once(drive, "SH_PFC_PIN_CFG_DRIVE_STRENGTH flag set but drive_regs missing\n");
1112 for (j = 0; drive_reg(j); j++) {
1113 if (!drive_field(j).pin &&
1114 !drive_field(j).offset &&
1115 !drive_field(j).size)
1118 if (drive_field(j).pin == pin->pin)
1123 sh_pfc_err("pin %s: SH_PFC_PIN_CFG_DRIVE_STRENGTH flag set but not in drive_regs\n",
1128 if (pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE) {
1129 if (!info->ops || !info->ops->pin_to_pocctrl)
1130 sh_pfc_err_once(power, "SH_PFC_PIN_CFG_IO_VOLTAGE flag set but .pin_to_pocctrl() not implemented\n");
1131 else if (info->ops->pin_to_pocctrl(pin->pin, &x) < 0)
1132 sh_pfc_err("pin %s: SH_PFC_PIN_CFG_IO_VOLTAGE set but invalid pin_to_pocctrl()\n",
1134 } else if (info->ops && info->ops->pin_to_pocctrl &&
1135 info->ops->pin_to_pocctrl(pin->pin, &x) >= 0) {
1136 sh_pfc_warn("pin %s: SH_PFC_PIN_CFG_IO_VOLTAGE not set but valid pin_to_pocctrl()\n",
1141 /* Check groups and functions */
1142 refcnts = kcalloc(info->nr_groups, sizeof(*refcnts), GFP_KERNEL);
1146 for (i = 0; i < info->nr_functions; i++) {
1147 const struct sh_pfc_function *func = &info->functions[i];
1150 sh_pfc_err("empty function %u\n", i);
1153 for (j = 0; j < i; j++) {
1154 if (same_name(func->name, info->functions[j].name))
1155 sh_pfc_err("function %s: name conflict\n",
1158 for (j = 0; j < func->nr_groups; j++) {
1159 for (k = 0; k < info->nr_groups; k++) {
1160 if (same_name(func->groups[j],
1161 info->groups[k].name)) {
1167 if (k == info->nr_groups)
1168 sh_pfc_err("function %s: group %s not found\n",
1169 func->name, func->groups[j]);
1173 for (i = 0; i < info->nr_groups; i++) {
1174 const struct sh_pfc_pin_group *group = &info->groups[i];
1177 sh_pfc_err("empty group %u\n", i);
1180 for (j = 0; j < i; j++)
1181 sh_pfc_compare_groups(drvname, group, &info->groups[j]);
1184 sh_pfc_err("orphan group %s\n", group->name);
1185 else if (refcnts[i] > 1)
1186 sh_pfc_warn("group %s referenced by %u functions\n",
1187 group->name, refcnts[i]);
1192 /* Check config register descriptions */
1193 for (i = 0; info->cfg_regs && info->cfg_regs[i].reg; i++)
1194 sh_pfc_check_cfg_reg(drvname, &info->cfg_regs[i]);
1196 /* Check drive strength registers */
1197 for (i = 0; drive_regs && drive_regs[i].reg; i++)
1198 sh_pfc_check_drive_reg(info, &drive_regs[i]);
1200 for (i = 0; drive_regs && drive_reg(i); i++) {
1201 if (!drive_field(i).pin && !drive_field(i).offset &&
1202 !drive_field(i).size)
1205 for (j = 0; j < i; j++) {
1206 if (drive_field(i).pin == drive_field(j).pin &&
1207 drive_field(j).offset && drive_field(j).size) {
1208 sh_pfc_err("drive_reg 0x%x:%zu/0x%x:%zu: pin conflict\n",
1209 drive_reg(i), drive_bit(i),
1210 drive_reg(j), drive_bit(j));
1215 /* Check bias registers */
1216 for (i = 0; bias_regs && (bias_regs[i].puen || bias_regs[i].pud); i++)
1217 sh_pfc_check_bias_reg(info, &bias_regs[i]);
1219 for (i = 0; bias_regs && (bias_puen(i) || bias_pud(i)); i++) {
1220 if (bias_pin(i) == SH_PFC_PIN_NONE)
1223 for (j = 0; j < i; j++) {
1224 if (bias_pin(i) != bias_pin(j))
1227 if (bias_puen(i) && bias_puen(j))
1228 sh_pfc_err("bias_reg 0x%x:%zu/0x%x:%zu: pin conflict\n",
1229 bias_puen(i), bias_bit(i),
1230 bias_puen(j), bias_bit(j));
1231 if (bias_pud(i) && bias_pud(j))
1232 sh_pfc_err("bias_reg 0x%x:%zu/0x%x:%zu: pin conflict\n",
1233 bias_pud(i), bias_bit(i),
1234 bias_pud(j), bias_bit(j));
1238 /* Check ioctrl registers */
1239 for (i = 0; info->ioctrl_regs && info->ioctrl_regs[i].reg; i++)
1240 sh_pfc_check_reg(drvname, info->ioctrl_regs[i].reg, U32_MAX);
1242 /* Check data registers */
1243 for (i = 0; info->data_regs && info->data_regs[i].reg; i++) {
1244 sh_pfc_check_reg(drvname, info->data_regs[i].reg,
1245 GENMASK(info->data_regs[i].reg_width - 1, 0));
1246 sh_pfc_check_reg_enums(drvname, info->data_regs[i].reg,
1247 info->data_regs[i].enum_ids,
1248 info->data_regs[i].reg_width);
1251 #ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
1252 /* Check function GPIOs */
1253 for (i = 0; i < info->nr_func_gpios; i++) {
1254 const struct pinmux_func *func = &info->func_gpios[i];
1257 sh_pfc_err("empty function gpio %u\n", i);
1260 for (j = 0; j < i; j++) {
1261 if (same_name(func->name, info->func_gpios[j].name))
1262 sh_pfc_err("func_gpio %s: name conflict\n",
1265 if (sh_pfc_check_enum(drvname, func->enum_id))
1266 sh_pfc_err("%s enum_id %u conflict\n", func->name,
1272 static void __init sh_pfc_check_driver(const struct platform_driver *pdrv)
1276 if (!IS_ENABLED(CONFIG_SUPERH) &&
1277 !of_find_matching_node(NULL, pdrv->driver.of_match_table))
1280 sh_pfc_regs = kcalloc(SH_PFC_MAX_REGS, sizeof(*sh_pfc_regs),
1285 sh_pfc_enums = kcalloc(SH_PFC_MAX_ENUMS, sizeof(*sh_pfc_enums),
1290 pr_warn("sh_pfc: Checking builtin pinmux tables\n");
1292 for (i = 0; pdrv->id_table[i].name[0]; i++)
1293 sh_pfc_check_info((void *)pdrv->id_table[i].driver_data);
1296 for (i = 0; pdrv->driver.of_match_table[i].compatible[0]; i++)
1297 sh_pfc_check_info(pdrv->driver.of_match_table[i].data);
1300 pr_warn("sh_pfc: Detected %u errors and %u warnings\n", sh_pfc_errors,
1303 kfree(sh_pfc_enums);
1309 static inline void sh_pfc_check_driver(struct platform_driver *pdrv) {}
1313 static const void *sh_pfc_quirk_match(void)
1315 #ifdef CONFIG_PINCTRL_PFC_R8A77950
1316 const struct soc_device_attribute *match;
1317 static const struct soc_device_attribute quirks[] = {
1319 .soc_id = "r8a7795", .revision = "ES1.*",
1320 .data = &r8a77950_pinmux_info,
1325 match = soc_device_match(quirks);
1328 #endif /* CONFIG_PINCTRL_PFC_R8A77950 */
1332 #endif /* CONFIG_OF */
1334 static int sh_pfc_probe(struct platform_device *pdev)
1336 const struct sh_pfc_soc_info *info;
1341 if (pdev->dev.of_node) {
1342 info = sh_pfc_quirk_match();
1344 info = of_device_get_match_data(&pdev->dev);
1347 info = (const void *)platform_get_device_id(pdev)->driver_data;
1349 pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
1354 pfc->dev = &pdev->dev;
1356 ret = sh_pfc_map_resources(pfc, pdev);
1357 if (unlikely(ret < 0))
1360 spin_lock_init(&pfc->lock);
1362 if (info->ops && info->ops->init) {
1363 ret = info->ops->init(pfc);
1367 /* .init() may have overridden pfc->info */
1371 ret = sh_pfc_suspend_init(pfc);
1375 /* Enable dummy states for those platforms without pinctrl support */
1376 if (!of_have_populated_dt())
1377 pinctrl_provide_dummies();
1379 ret = sh_pfc_init_ranges(pfc);
1384 * Initialize pinctrl bindings first
1386 ret = sh_pfc_register_pinctrl(pfc);
1387 if (unlikely(ret != 0))
1390 #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
1392 * Then the GPIO chip
1394 ret = sh_pfc_register_gpiochip(pfc);
1395 if (unlikely(ret != 0)) {
1397 * If the GPIO chip fails to come up we still leave the
1398 * PFC state as it is, given that there are already
1399 * extant users of it that have succeeded by this point.
1401 dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
1405 platform_set_drvdata(pdev, pfc);
1407 dev_info(pfc->dev, "%s support registered\n", info->name);
1412 static const struct platform_device_id sh_pfc_id_table[] = {
1413 #ifdef CONFIG_PINCTRL_PFC_SH7203
1414 { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
1416 #ifdef CONFIG_PINCTRL_PFC_SH7264
1417 { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
1419 #ifdef CONFIG_PINCTRL_PFC_SH7269
1420 { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
1422 #ifdef CONFIG_PINCTRL_PFC_SH7720
1423 { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
1425 #ifdef CONFIG_PINCTRL_PFC_SH7722
1426 { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
1428 #ifdef CONFIG_PINCTRL_PFC_SH7723
1429 { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
1431 #ifdef CONFIG_PINCTRL_PFC_SH7724
1432 { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
1434 #ifdef CONFIG_PINCTRL_PFC_SH7734
1435 { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
1437 #ifdef CONFIG_PINCTRL_PFC_SH7757
1438 { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
1440 #ifdef CONFIG_PINCTRL_PFC_SH7785
1441 { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
1443 #ifdef CONFIG_PINCTRL_PFC_SH7786
1444 { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
1446 #ifdef CONFIG_PINCTRL_PFC_SHX3
1447 { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
1452 static struct platform_driver sh_pfc_driver = {
1453 .probe = sh_pfc_probe,
1454 .id_table = sh_pfc_id_table,
1457 .of_match_table = of_match_ptr(sh_pfc_of_table),
1462 static int __init sh_pfc_init(void)
1464 sh_pfc_check_driver(&sh_pfc_driver);
1465 return platform_driver_register(&sh_pfc_driver);
1467 postcore_initcall(sh_pfc_init);