1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 *
4 * Copyright (C) 2012 ARM Limited
5 */
6
7 #include <linux/gpio/driver.h>
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/mfd/core.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
15 #include <linux/slab.h>
16 #include <linux/stat.h>
17
18 #define SYS_ID 0x000
19 #define SYS_SW 0x004
20 #define SYS_LED 0x008
21 #define SYS_100HZ 0x024
22 #define SYS_FLAGSSET 0x030
23 #define SYS_FLAGSCLR 0x034
24 #define SYS_NVFLAGS 0x038
25 #define SYS_NVFLAGSSET 0x038
26 #define SYS_NVFLAGSCLR 0x03c
27 #define SYS_MCI 0x048
28 #define SYS_FLASH 0x04c
29 #define SYS_CFGSW 0x058
30 #define SYS_24MHZ 0x05c
31 #define SYS_MISC 0x060
32 #define SYS_DMA 0x064
33 #define SYS_PROCID0 0x084
34 #define SYS_PROCID1 0x088
35 #define SYS_CFGDATA 0x0a0
36 #define SYS_CFGCTRL 0x0a4
37 #define SYS_CFGSTAT 0x0a8
38
39 /* The sysreg block is just a random collection of various functions... */
40
41 static const struct property_entry vexpress_sysreg_sys_led_props[] = {
42 PROPERTY_ENTRY_STRING("label", "sys_led"),
43 PROPERTY_ENTRY_U32("ngpios", 8),
44 { }
45 };
46
47 static const struct software_node vexpress_sysreg_sys_led_swnode = {
48 .properties = vexpress_sysreg_sys_led_props,
49 };
50
51 static const struct property_entry vexpress_sysreg_sys_mci_props[] = {
52 PROPERTY_ENTRY_STRING("label", "sys_mci"),
53 PROPERTY_ENTRY_U32("ngpios", 2),
54 { }
55 };
56
57 static const struct software_node vexpress_sysreg_sys_mci_swnode = {
58 .properties = vexpress_sysreg_sys_mci_props,
59 };
60
61 static const struct property_entry vexpress_sysreg_sys_flash_props[] = {
62 PROPERTY_ENTRY_STRING("label", "sys_flash"),
63 PROPERTY_ENTRY_U32("ngpios", 1),
64 { }
65 };
66
67 static const struct software_node vexpress_sysreg_sys_flash_swnode = {
68 .properties = vexpress_sysreg_sys_flash_props,
69 };
70
71 static struct mfd_cell vexpress_sysreg_cells[] = {
72 {
73 .name = "basic-mmio-gpio",
74 .of_compatible = "arm,vexpress-sysreg,sys_led",
75 .num_resources = 1,
76 .resources = &DEFINE_RES_MEM_NAMED(SYS_LED, 0x4, "dat"),
77 .swnode = &vexpress_sysreg_sys_led_swnode,
78 }, {
79 .name = "basic-mmio-gpio",
80 .of_compatible = "arm,vexpress-sysreg,sys_mci",
81 .num_resources = 1,
82 .resources = &DEFINE_RES_MEM_NAMED(SYS_MCI, 0x4, "dat"),
83 .swnode = &vexpress_sysreg_sys_mci_swnode,
84 }, {
85 .name = "basic-mmio-gpio",
86 .of_compatible = "arm,vexpress-sysreg,sys_flash",
87 .num_resources = 1,
88 .resources = &DEFINE_RES_MEM_NAMED(SYS_FLASH, 0x4, "dat"),
89 .swnode = &vexpress_sysreg_sys_flash_swnode,
90 }, {
91 .name = "vexpress-syscfg",
92 .num_resources = 1,
93 .resources = &DEFINE_RES_MEM(SYS_MISC, 0x4c),
94 }
95 };
96
vexpress_sysreg_probe(struct platform_device * pdev)97 static int vexpress_sysreg_probe(struct platform_device *pdev)
98 {
99 struct resource *mem;
100 void __iomem *base;
101 struct gpio_chip *mmc_gpio_chip;
102
103 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
104 if (!mem)
105 return -EINVAL;
106
107 base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
108 if (!base)
109 return -ENOMEM;
110
111 /*
112 * Duplicated SYS_MCI pseudo-GPIO controller for compatibility with
113 * older trees using sysreg node for MMC control lines.
114 */
115 mmc_gpio_chip = devm_kzalloc(&pdev->dev, sizeof(*mmc_gpio_chip),
116 GFP_KERNEL);
117 if (!mmc_gpio_chip)
118 return -ENOMEM;
119 bgpio_init(mmc_gpio_chip, &pdev->dev, 0x4, base + SYS_MCI,
120 NULL, NULL, NULL, NULL, 0);
121 mmc_gpio_chip->ngpio = 2;
122 devm_gpiochip_add_data(&pdev->dev, mmc_gpio_chip, NULL);
123
124 return devm_mfd_add_devices(&pdev->dev, PLATFORM_DEVID_AUTO,
125 vexpress_sysreg_cells,
126 ARRAY_SIZE(vexpress_sysreg_cells), mem, 0, NULL);
127 }
128
129 static const struct of_device_id vexpress_sysreg_match[] = {
130 { .compatible = "arm,vexpress-sysreg", },
131 {},
132 };
133 MODULE_DEVICE_TABLE(of, vexpress_sysreg_match);
134
135 static struct platform_driver vexpress_sysreg_driver = {
136 .driver = {
137 .name = "vexpress-sysreg",
138 .of_match_table = vexpress_sysreg_match,
139 },
140 .probe = vexpress_sysreg_probe,
141 };
142
143 module_platform_driver(vexpress_sysreg_driver);
144 MODULE_DESCRIPTION("Versatile Express system registers driver");
145 MODULE_LICENSE("GPL v2");
146