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>
17 #include <linux/ioport.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
21 #include <linux/of_device.h>
22 #include <linux/pinctrl/machine.h>
23 #include <linux/platform_device.h>
24 #include <linux/psci.h>
25 #include <linux/slab.h>
26 #include <linux/sys_soc.h>
30 static int sh_pfc_map_resources(struct sh_pfc *pfc,
31 struct platform_device *pdev)
33 struct sh_pfc_window *windows;
34 unsigned int *irqs = NULL;
35 unsigned int num_windows;
40 /* Count the MEM and IRQ resources. */
41 for (num_windows = 0;; num_windows++) {
42 res = platform_get_resource(pdev, IORESOURCE_MEM, num_windows);
49 num_irqs = platform_irq_count(pdev);
53 /* Allocate memory windows and IRQs arrays. */
54 windows = devm_kcalloc(pfc->dev, num_windows, sizeof(*windows),
59 pfc->num_windows = num_windows;
60 pfc->windows = windows;
63 irqs = devm_kcalloc(pfc->dev, num_irqs, sizeof(*irqs),
68 pfc->num_irqs = num_irqs;
73 for (i = 0; i < num_windows; i++) {
74 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
75 windows->phys = res->start;
76 windows->size = resource_size(res);
77 windows->virt = devm_ioremap_resource(pfc->dev, res);
78 if (IS_ERR(windows->virt))
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 -= 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) {
270 curr_width = f_width;
272 curr_width = config_reg->var_field_width[m];
274 ncomb = 1 << curr_width;
275 for (n = 0; n < ncomb; n++) {
276 if (config_reg->enum_ids[pos + n] == enum_id) {
292 static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
295 const u16 *data = pfc->info->pinmux_data;
299 *enum_idp = data[pos + 1];
303 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
304 if (data[k] == mark) {
305 *enum_idp = data[k + 1];
310 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
315 int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
317 const struct pinmux_range *range;
320 switch (pinmux_type) {
321 case PINMUX_TYPE_GPIO:
322 case PINMUX_TYPE_FUNCTION:
326 #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
327 case PINMUX_TYPE_OUTPUT:
328 range = &pfc->info->output;
331 case PINMUX_TYPE_INPUT:
332 range = &pfc->info->input;
334 #endif /* CONFIG_PINCTRL_SH_PFC_GPIO */
340 /* Iterate over all the configuration fields we need to update. */
342 const struct pinmux_cfg_reg *cr;
349 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
356 /* Check if the configuration field selects a function. If it
357 * doesn't, skip the field if it's not applicable to the
358 * requested pinmux type.
360 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
362 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
363 /* Functions are allowed to modify all
367 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
368 /* Input/output types can only modify fields
369 * that correspond to their respective ranges.
371 in_range = sh_pfc_enum_in_range(enum_id, range);
374 * special case pass through for fixed
375 * input-only or output-only pins without
376 * function enum register association.
378 if (in_range && enum_id == range->force)
381 /* GPIOs are only allowed to modify function fields. */
387 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
391 sh_pfc_write_config_reg(pfc, cr, field, value);
397 static int sh_pfc_init_ranges(struct sh_pfc *pfc)
399 struct sh_pfc_pin_range *range;
400 unsigned int nr_ranges;
403 if (pfc->info->pins[0].pin == (u16)-1) {
404 /* Pin number -1 denotes that the SoC doesn't report pin numbers
405 * in its pin arrays yet. Consider the pin numbers range as
406 * continuous and allocate a single range.
409 pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
411 if (pfc->ranges == NULL)
414 pfc->ranges->start = 0;
415 pfc->ranges->end = pfc->info->nr_pins - 1;
416 pfc->nr_gpio_pins = pfc->info->nr_pins;
421 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
422 * be sorted by pin numbers, and pins without a GPIO port must come
425 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
426 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
430 pfc->nr_ranges = nr_ranges;
431 pfc->ranges = devm_kcalloc(pfc->dev, nr_ranges, sizeof(*pfc->ranges),
433 if (pfc->ranges == NULL)
437 range->start = pfc->info->pins[0].pin;
439 for (i = 1; i < pfc->info->nr_pins; ++i) {
440 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
443 range->end = pfc->info->pins[i-1].pin;
444 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
445 pfc->nr_gpio_pins = range->end + 1;
448 range->start = pfc->info->pins[i].pin;
451 range->end = pfc->info->pins[i-1].pin;
452 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
453 pfc->nr_gpio_pins = range->end + 1;
459 static const struct of_device_id sh_pfc_of_table[] = {
460 #ifdef CONFIG_PINCTRL_PFC_EMEV2
462 .compatible = "renesas,pfc-emev2",
463 .data = &emev2_pinmux_info,
466 #ifdef CONFIG_PINCTRL_PFC_R8A73A4
468 .compatible = "renesas,pfc-r8a73a4",
469 .data = &r8a73a4_pinmux_info,
472 #ifdef CONFIG_PINCTRL_PFC_R8A7740
474 .compatible = "renesas,pfc-r8a7740",
475 .data = &r8a7740_pinmux_info,
478 #ifdef CONFIG_PINCTRL_PFC_R8A7742
480 .compatible = "renesas,pfc-r8a7742",
481 .data = &r8a7742_pinmux_info,
484 #ifdef CONFIG_PINCTRL_PFC_R8A7743
486 .compatible = "renesas,pfc-r8a7743",
487 .data = &r8a7743_pinmux_info,
490 #ifdef CONFIG_PINCTRL_PFC_R8A7744
492 .compatible = "renesas,pfc-r8a7744",
493 .data = &r8a7744_pinmux_info,
496 #ifdef CONFIG_PINCTRL_PFC_R8A7745
498 .compatible = "renesas,pfc-r8a7745",
499 .data = &r8a7745_pinmux_info,
502 #ifdef CONFIG_PINCTRL_PFC_R8A77470
504 .compatible = "renesas,pfc-r8a77470",
505 .data = &r8a77470_pinmux_info,
508 #ifdef CONFIG_PINCTRL_PFC_R8A774A1
510 .compatible = "renesas,pfc-r8a774a1",
511 .data = &r8a774a1_pinmux_info,
514 #ifdef CONFIG_PINCTRL_PFC_R8A774B1
516 .compatible = "renesas,pfc-r8a774b1",
517 .data = &r8a774b1_pinmux_info,
520 #ifdef CONFIG_PINCTRL_PFC_R8A774C0
522 .compatible = "renesas,pfc-r8a774c0",
523 .data = &r8a774c0_pinmux_info,
526 #ifdef CONFIG_PINCTRL_PFC_R8A774E1
528 .compatible = "renesas,pfc-r8a774e1",
529 .data = &r8a774e1_pinmux_info,
532 #ifdef CONFIG_PINCTRL_PFC_R8A7778
534 .compatible = "renesas,pfc-r8a7778",
535 .data = &r8a7778_pinmux_info,
538 #ifdef CONFIG_PINCTRL_PFC_R8A7779
540 .compatible = "renesas,pfc-r8a7779",
541 .data = &r8a7779_pinmux_info,
544 #ifdef CONFIG_PINCTRL_PFC_R8A7790
546 .compatible = "renesas,pfc-r8a7790",
547 .data = &r8a7790_pinmux_info,
550 #ifdef CONFIG_PINCTRL_PFC_R8A7791
552 .compatible = "renesas,pfc-r8a7791",
553 .data = &r8a7791_pinmux_info,
556 #ifdef CONFIG_PINCTRL_PFC_R8A7792
558 .compatible = "renesas,pfc-r8a7792",
559 .data = &r8a7792_pinmux_info,
562 #ifdef CONFIG_PINCTRL_PFC_R8A7793
564 .compatible = "renesas,pfc-r8a7793",
565 .data = &r8a7793_pinmux_info,
568 #ifdef CONFIG_PINCTRL_PFC_R8A7794
570 .compatible = "renesas,pfc-r8a7794",
571 .data = &r8a7794_pinmux_info,
575 * Both r8a7795 entries must be present to make sanity checks work, but only
576 * the first entry is actually used.
577 * R-Car H3 ES1.x is matched using soc_device_match() instead.
579 #ifdef CONFIG_PINCTRL_PFC_R8A77951
581 .compatible = "renesas,pfc-r8a7795",
582 .data = &r8a77951_pinmux_info,
585 #ifdef CONFIG_PINCTRL_PFC_R8A77950
587 .compatible = "renesas,pfc-r8a7795",
588 .data = &r8a77950_pinmux_info,
591 #ifdef CONFIG_PINCTRL_PFC_R8A77960
593 .compatible = "renesas,pfc-r8a7796",
594 .data = &r8a77960_pinmux_info,
597 #ifdef CONFIG_PINCTRL_PFC_R8A77961
599 .compatible = "renesas,pfc-r8a77961",
600 .data = &r8a77961_pinmux_info,
603 #ifdef CONFIG_PINCTRL_PFC_R8A77965
605 .compatible = "renesas,pfc-r8a77965",
606 .data = &r8a77965_pinmux_info,
609 #ifdef CONFIG_PINCTRL_PFC_R8A77970
611 .compatible = "renesas,pfc-r8a77970",
612 .data = &r8a77970_pinmux_info,
615 #ifdef CONFIG_PINCTRL_PFC_R8A77980
617 .compatible = "renesas,pfc-r8a77980",
618 .data = &r8a77980_pinmux_info,
621 #ifdef CONFIG_PINCTRL_PFC_R8A77990
623 .compatible = "renesas,pfc-r8a77990",
624 .data = &r8a77990_pinmux_info,
627 #ifdef CONFIG_PINCTRL_PFC_R8A77995
629 .compatible = "renesas,pfc-r8a77995",
630 .data = &r8a77995_pinmux_info,
633 #ifdef CONFIG_PINCTRL_PFC_R8A779A0
635 .compatible = "renesas,pfc-r8a779a0",
636 .data = &r8a779a0_pinmux_info,
639 #ifdef CONFIG_PINCTRL_PFC_SH73A0
641 .compatible = "renesas,pfc-sh73a0",
642 .data = &sh73a0_pinmux_info,
649 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM_PSCI_FW)
650 static void sh_pfc_nop_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
654 static void sh_pfc_save_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
656 pfc->saved_regs[idx] = sh_pfc_read(pfc, reg);
659 static void sh_pfc_restore_reg(struct sh_pfc *pfc, u32 reg, unsigned int idx)
661 sh_pfc_write(pfc, reg, pfc->saved_regs[idx]);
664 static unsigned int sh_pfc_walk_regs(struct sh_pfc *pfc,
665 void (*do_reg)(struct sh_pfc *pfc, u32 reg, unsigned int idx))
667 unsigned int i, n = 0;
669 if (pfc->info->cfg_regs)
670 for (i = 0; pfc->info->cfg_regs[i].reg; i++)
671 do_reg(pfc, pfc->info->cfg_regs[i].reg, n++);
673 if (pfc->info->drive_regs)
674 for (i = 0; pfc->info->drive_regs[i].reg; i++)
675 do_reg(pfc, pfc->info->drive_regs[i].reg, n++);
677 if (pfc->info->bias_regs)
678 for (i = 0; pfc->info->bias_regs[i].puen ||
679 pfc->info->bias_regs[i].pud; i++) {
680 if (pfc->info->bias_regs[i].puen)
681 do_reg(pfc, pfc->info->bias_regs[i].puen, n++);
682 if (pfc->info->bias_regs[i].pud)
683 do_reg(pfc, pfc->info->bias_regs[i].pud, n++);
686 if (pfc->info->ioctrl_regs)
687 for (i = 0; pfc->info->ioctrl_regs[i].reg; i++)
688 do_reg(pfc, pfc->info->ioctrl_regs[i].reg, n++);
693 static int sh_pfc_suspend_init(struct sh_pfc *pfc)
697 /* This is the best we can do to check for the presence of PSCI */
698 if (!psci_ops.cpu_suspend)
701 n = sh_pfc_walk_regs(pfc, sh_pfc_nop_reg);
705 pfc->saved_regs = devm_kmalloc_array(pfc->dev, n,
706 sizeof(*pfc->saved_regs),
708 if (!pfc->saved_regs)
711 dev_dbg(pfc->dev, "Allocated space to save %u regs\n", n);
715 static int sh_pfc_suspend_noirq(struct device *dev)
717 struct sh_pfc *pfc = dev_get_drvdata(dev);
720 sh_pfc_walk_regs(pfc, sh_pfc_save_reg);
724 static int sh_pfc_resume_noirq(struct device *dev)
726 struct sh_pfc *pfc = dev_get_drvdata(dev);
729 sh_pfc_walk_regs(pfc, sh_pfc_restore_reg);
733 static const struct dev_pm_ops sh_pfc_pm = {
734 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sh_pfc_suspend_noirq, sh_pfc_resume_noirq)
736 #define DEV_PM_OPS &sh_pfc_pm
738 static int sh_pfc_suspend_init(struct sh_pfc *pfc) { return 0; }
739 #define DEV_PM_OPS NULL
740 #endif /* CONFIG_PM_SLEEP && CONFIG_ARM_PSCI_FW */
743 #define SH_PFC_MAX_REGS 300
744 #define SH_PFC_MAX_ENUMS 3000
746 static unsigned int sh_pfc_errors __initdata;
747 static unsigned int sh_pfc_warnings __initdata;
751 } *sh_pfc_regs __initdata;
752 static u32 sh_pfc_num_regs __initdata;
753 static u16 *sh_pfc_enums __initdata;
754 static u32 sh_pfc_num_enums __initdata;
756 #define sh_pfc_err(fmt, ...) \
758 pr_err("%s: " fmt, drvname, ##__VA_ARGS__); \
761 #define sh_pfc_warn(fmt, ...) \
763 pr_warn("%s: " fmt, drvname, ##__VA_ARGS__); \
767 static bool __init is0s(const u16 *enum_ids, unsigned int n)
771 for (i = 0; i < n; i++)
778 static bool __init same_name(const char *a, const char *b)
780 return a && b && !strcmp(a, b);
783 static void __init sh_pfc_check_reg(const char *drvname, u32 reg, u32 bits)
787 for (i = 0; i < sh_pfc_num_regs; i++) {
788 if (reg != sh_pfc_regs[i].reg)
791 if (bits & sh_pfc_regs[i].bits)
792 sh_pfc_err("reg 0x%x: bits 0x%x conflict\n", reg,
793 bits & sh_pfc_regs[i].bits);
795 sh_pfc_regs[i].bits |= bits;
799 if (sh_pfc_num_regs == SH_PFC_MAX_REGS) {
800 pr_warn_once("%s: Please increase SH_PFC_MAX_REGS\n", drvname);
804 sh_pfc_regs[sh_pfc_num_regs].reg = reg;
805 sh_pfc_regs[sh_pfc_num_regs].bits = bits;
809 static int __init sh_pfc_check_enum(const char *drvname, u16 enum_id)
813 for (i = 0; i < sh_pfc_num_enums; i++) {
814 if (enum_id == sh_pfc_enums[i])
818 if (sh_pfc_num_enums == SH_PFC_MAX_ENUMS) {
819 pr_warn_once("%s: Please increase SH_PFC_MAX_ENUMS\n", drvname);
823 sh_pfc_enums[sh_pfc_num_enums++] = enum_id;
827 static void __init sh_pfc_check_reg_enums(const char *drvname, u32 reg,
828 const u16 *enums, unsigned int n)
832 for (i = 0; i < n; i++) {
833 if (enums[i] && sh_pfc_check_enum(drvname, enums[i]))
834 sh_pfc_err("reg 0x%x enum_id %u conflict\n", reg,
839 static void __init sh_pfc_check_pin(const struct sh_pfc_soc_info *info,
840 u32 reg, unsigned int pin)
842 const char *drvname = info->name;
845 if (pin == SH_PFC_PIN_NONE)
848 for (i = 0; i < info->nr_pins; i++) {
849 if (pin == info->pins[i].pin)
853 sh_pfc_err("reg 0x%x: pin %u not found\n", reg, pin);
856 static void __init sh_pfc_check_cfg_reg(const char *drvname,
857 const struct pinmux_cfg_reg *cfg_reg)
859 unsigned int i, n, rw, fw;
861 sh_pfc_check_reg(drvname, cfg_reg->reg,
862 GENMASK(cfg_reg->reg_width - 1, 0));
864 if (cfg_reg->field_width) {
865 n = cfg_reg->reg_width / cfg_reg->field_width;
866 /* Skip field checks (done at build time) */
870 for (i = 0, n = 0, rw = 0; (fw = cfg_reg->var_field_width[i]); i++) {
871 if (fw > 3 && is0s(&cfg_reg->enum_ids[n], 1 << fw))
872 sh_pfc_warn("reg 0x%x: reserved field [%u:%u] can be split to reduce table size\n",
873 cfg_reg->reg, rw, rw + fw - 1);
878 if (rw != cfg_reg->reg_width)
879 sh_pfc_err("reg 0x%x: var_field_width declares %u instead of %u bits\n",
880 cfg_reg->reg, rw, cfg_reg->reg_width);
882 if (n != cfg_reg->nr_enum_ids)
883 sh_pfc_err("reg 0x%x: enum_ids[] has %u instead of %u values\n",
884 cfg_reg->reg, cfg_reg->nr_enum_ids, n);
887 sh_pfc_check_reg_enums(drvname, cfg_reg->reg, cfg_reg->enum_ids, n);
890 static void __init sh_pfc_check_drive_reg(const struct sh_pfc_soc_info *info,
891 const struct pinmux_drive_reg *drive)
895 for (i = 0; i < ARRAY_SIZE(drive->fields); i++) {
896 const struct pinmux_drive_reg_field *field = &drive->fields[i];
898 if (!field->pin && !field->offset && !field->size)
901 sh_pfc_check_reg(info->name, drive->reg,
902 GENMASK(field->offset + field->size - 1,
905 sh_pfc_check_pin(info, drive->reg, field->pin);
909 static void __init sh_pfc_check_bias_reg(const struct sh_pfc_soc_info *info,
910 const struct pinmux_bias_reg *bias)
915 for (i = 0, bits = 0; i < ARRAY_SIZE(bias->pins); i++)
916 if (bias->pins[i] != SH_PFC_PIN_NONE)
920 sh_pfc_check_reg(info->name, bias->puen, bits);
922 sh_pfc_check_reg(info->name, bias->pud, bits);
923 for (i = 0; i < ARRAY_SIZE(bias->pins); i++)
924 sh_pfc_check_pin(info, bias->puen, bias->pins[i]);
927 static void __init sh_pfc_check_info(const struct sh_pfc_soc_info *info)
929 const struct pinmux_bias_reg *bias_regs = info->bias_regs;
930 const char *drvname = info->name;
931 unsigned int *refcnts;
932 unsigned int i, j, k;
934 pr_info("sh_pfc: Checking %s\n", drvname);
936 sh_pfc_num_enums = 0;
939 for (i = 0; i < info->nr_pins; i++) {
940 const struct sh_pfc_pin *pin = &info->pins[i];
943 sh_pfc_err("empty pin %u\n", i);
946 for (j = 0; j < i; j++) {
947 const struct sh_pfc_pin *pin2 = &info->pins[j];
949 if (same_name(pin->name, pin2->name))
950 sh_pfc_err("pin %s: name conflict\n",
953 if (pin->pin != (u16)-1 && pin->pin == pin2->pin)
954 sh_pfc_err("pin %s/%s: pin %u conflict\n",
955 pin->name, pin2->name, pin->pin);
957 if (pin->enum_id && pin->enum_id == pin2->enum_id)
958 sh_pfc_err("pin %s/%s: enum_id %u conflict\n",
959 pin->name, pin2->name,
964 /* Check groups and functions */
965 refcnts = kcalloc(info->nr_groups, sizeof(*refcnts), GFP_KERNEL);
969 for (i = 0; i < info->nr_functions; i++) {
970 const struct sh_pfc_function *func = &info->functions[i];
973 sh_pfc_err("empty function %u\n", i);
976 for (j = 0; j < i; j++) {
977 if (same_name(func->name, info->functions[j].name))
978 sh_pfc_err("function %s: name conflict\n",
981 for (j = 0; j < func->nr_groups; j++) {
982 for (k = 0; k < info->nr_groups; k++) {
983 if (same_name(func->groups[j],
984 info->groups[k].name)) {
990 if (k == info->nr_groups)
991 sh_pfc_err("function %s: group %s not found\n",
992 func->name, func->groups[j]);
996 for (i = 0; i < info->nr_groups; i++) {
997 const struct sh_pfc_pin_group *group = &info->groups[i];
1000 sh_pfc_err("empty group %u\n", i);
1003 for (j = 0; j < i; j++) {
1004 if (same_name(group->name, info->groups[j].name))
1005 sh_pfc_err("group %s: name conflict\n",
1009 sh_pfc_err("orphan group %s\n", group->name);
1010 else if (refcnts[i] > 1)
1011 sh_pfc_warn("group %s referenced by %u functions\n",
1012 group->name, refcnts[i]);
1017 /* Check config register descriptions */
1018 for (i = 0; info->cfg_regs && info->cfg_regs[i].reg; i++)
1019 sh_pfc_check_cfg_reg(drvname, &info->cfg_regs[i]);
1021 /* Check drive strength registers */
1022 for (i = 0; info->drive_regs && info->drive_regs[i].reg; i++)
1023 sh_pfc_check_drive_reg(info, &info->drive_regs[i]);
1025 /* Check bias registers */
1026 for (i = 0; bias_regs && (bias_regs[i].puen || bias_regs[i].pud); i++)
1027 sh_pfc_check_bias_reg(info, &bias_regs[i]);
1029 /* Check ioctrl registers */
1030 for (i = 0; info->ioctrl_regs && info->ioctrl_regs[i].reg; i++)
1031 sh_pfc_check_reg(drvname, info->ioctrl_regs[i].reg, U32_MAX);
1033 /* Check data registers */
1034 for (i = 0; info->data_regs && info->data_regs[i].reg; i++) {
1035 sh_pfc_check_reg(drvname, info->data_regs[i].reg,
1036 GENMASK(info->data_regs[i].reg_width - 1, 0));
1037 sh_pfc_check_reg_enums(drvname, info->data_regs[i].reg,
1038 info->data_regs[i].enum_ids,
1039 info->data_regs[i].reg_width);
1042 #ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
1043 /* Check function GPIOs */
1044 for (i = 0; i < info->nr_func_gpios; i++) {
1045 const struct pinmux_func *func = &info->func_gpios[i];
1048 sh_pfc_err("empty function gpio %u\n", i);
1051 for (j = 0; j < i; j++) {
1052 if (same_name(func->name, info->func_gpios[j].name))
1053 sh_pfc_err("func_gpio %s: name conflict\n",
1056 if (sh_pfc_check_enum(drvname, func->enum_id))
1057 sh_pfc_err("%s enum_id %u conflict\n", func->name,
1063 static void __init sh_pfc_check_driver(const struct platform_driver *pdrv)
1067 if (!IS_ENABLED(CONFIG_SUPERH) &&
1068 !of_find_matching_node(NULL, pdrv->driver.of_match_table))
1071 sh_pfc_regs = kcalloc(SH_PFC_MAX_REGS, sizeof(*sh_pfc_regs),
1076 sh_pfc_enums = kcalloc(SH_PFC_MAX_ENUMS, sizeof(*sh_pfc_enums),
1081 pr_warn("sh_pfc: Checking builtin pinmux tables\n");
1083 for (i = 0; pdrv->id_table[i].name[0]; i++)
1084 sh_pfc_check_info((void *)pdrv->id_table[i].driver_data);
1087 for (i = 0; pdrv->driver.of_match_table[i].compatible[0]; i++)
1088 sh_pfc_check_info(pdrv->driver.of_match_table[i].data);
1091 pr_warn("sh_pfc: Detected %u errors and %u warnings\n", sh_pfc_errors,
1094 kfree(sh_pfc_enums);
1100 static inline void sh_pfc_check_driver(struct platform_driver *pdrv) {}
1104 static const void *sh_pfc_quirk_match(void)
1106 #ifdef CONFIG_PINCTRL_PFC_R8A77950
1107 const struct soc_device_attribute *match;
1108 static const struct soc_device_attribute quirks[] = {
1110 .soc_id = "r8a7795", .revision = "ES1.*",
1111 .data = &r8a77950_pinmux_info,
1116 match = soc_device_match(quirks);
1119 #endif /* CONFIG_PINCTRL_PFC_R8A77950 */
1123 #endif /* CONFIG_OF */
1125 static int sh_pfc_probe(struct platform_device *pdev)
1127 const struct sh_pfc_soc_info *info;
1132 if (pdev->dev.of_node) {
1133 info = sh_pfc_quirk_match();
1135 info = of_device_get_match_data(&pdev->dev);
1138 info = (const void *)platform_get_device_id(pdev)->driver_data;
1140 pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
1145 pfc->dev = &pdev->dev;
1147 ret = sh_pfc_map_resources(pfc, pdev);
1148 if (unlikely(ret < 0))
1151 spin_lock_init(&pfc->lock);
1153 if (info->ops && info->ops->init) {
1154 ret = info->ops->init(pfc);
1158 /* .init() may have overridden pfc->info */
1162 ret = sh_pfc_suspend_init(pfc);
1166 /* Enable dummy states for those platforms without pinctrl support */
1167 if (!of_have_populated_dt())
1168 pinctrl_provide_dummies();
1170 ret = sh_pfc_init_ranges(pfc);
1175 * Initialize pinctrl bindings first
1177 ret = sh_pfc_register_pinctrl(pfc);
1178 if (unlikely(ret != 0))
1181 #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
1183 * Then the GPIO chip
1185 ret = sh_pfc_register_gpiochip(pfc);
1186 if (unlikely(ret != 0)) {
1188 * If the GPIO chip fails to come up we still leave the
1189 * PFC state as it is, given that there are already
1190 * extant users of it that have succeeded by this point.
1192 dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
1196 platform_set_drvdata(pdev, pfc);
1198 dev_info(pfc->dev, "%s support registered\n", info->name);
1203 static const struct platform_device_id sh_pfc_id_table[] = {
1204 #ifdef CONFIG_PINCTRL_PFC_SH7203
1205 { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
1207 #ifdef CONFIG_PINCTRL_PFC_SH7264
1208 { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
1210 #ifdef CONFIG_PINCTRL_PFC_SH7269
1211 { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
1213 #ifdef CONFIG_PINCTRL_PFC_SH7720
1214 { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
1216 #ifdef CONFIG_PINCTRL_PFC_SH7722
1217 { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
1219 #ifdef CONFIG_PINCTRL_PFC_SH7723
1220 { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
1222 #ifdef CONFIG_PINCTRL_PFC_SH7724
1223 { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
1225 #ifdef CONFIG_PINCTRL_PFC_SH7734
1226 { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
1228 #ifdef CONFIG_PINCTRL_PFC_SH7757
1229 { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
1231 #ifdef CONFIG_PINCTRL_PFC_SH7785
1232 { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
1234 #ifdef CONFIG_PINCTRL_PFC_SH7786
1235 { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
1237 #ifdef CONFIG_PINCTRL_PFC_SHX3
1238 { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
1243 static struct platform_driver sh_pfc_driver = {
1244 .probe = sh_pfc_probe,
1245 .id_table = sh_pfc_id_table,
1248 .of_match_table = of_match_ptr(sh_pfc_of_table),
1253 static int __init sh_pfc_init(void)
1255 sh_pfc_check_driver(&sh_pfc_driver);
1256 return platform_driver_register(&sh_pfc_driver);
1258 postcore_initcall(sh_pfc_init);