1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel Quark MFD PCI driver for I2C & GPIO
4  *
5  * Copyright(c) 2014 Intel Corporation.
6  *
7  * Intel Quark PCI device for I2C and GPIO controller sharing the same
8  * PCI function. This PCI driver will split the 2 devices into their
9  * respective drivers.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/mfd/core.h>
16 #include <linux/clkdev.h>
17 #include <linux/clk-provider.h>
18 #include <linux/dmi.h>
19 #include <linux/platform_data/gpio-dwapb.h>
20 #include <linux/platform_data/i2c-designware.h>
21 
22 /* PCI BAR for register base address */
23 #define MFD_I2C_BAR		0
24 #define MFD_GPIO_BAR		1
25 
26 /* ACPI _ADR value to match the child node */
27 #define MFD_ACPI_MATCH_GPIO	0ULL
28 #define MFD_ACPI_MATCH_I2C	1ULL
29 
30 /* The base GPIO number under GPIOLIB framework */
31 #define INTEL_QUARK_MFD_GPIO_BASE	8
32 
33 /* The default number of South-Cluster GPIO on Quark. */
34 #define INTEL_QUARK_MFD_NGPIO		8
35 
36 /* The DesignWare GPIO ports on Quark. */
37 #define INTEL_QUARK_GPIO_NPORTS	1
38 
39 #define INTEL_QUARK_IORES_MEM	0
40 #define INTEL_QUARK_IORES_IRQ	1
41 
42 #define INTEL_QUARK_I2C_CONTROLLER_CLK "i2c_designware.0"
43 
44 /* The Quark I2C controller source clock */
45 #define INTEL_QUARK_I2C_CLK_HZ	33000000
46 
47 struct intel_quark_mfd {
48 	struct device		*dev;
49 	struct clk		*i2c_clk;
50 	struct clk_lookup	*i2c_clk_lookup;
51 };
52 
53 static const struct dmi_system_id dmi_platform_info[] = {
54 	{
55 		.matches = {
56 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Galileo"),
57 		},
58 		.driver_data = (void *)100000,
59 	},
60 	{
61 		.matches = {
62 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "GalileoGen2"),
63 		},
64 		.driver_data = (void *)400000,
65 	},
66 	{
67 		.matches = {
68 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
69 		},
70 		.driver_data = (void *)400000,
71 	},
72 	{}
73 };
74 
75 /* This is used as a place holder and will be modified at run-time */
76 static struct resource intel_quark_i2c_res[] = {
77 	[INTEL_QUARK_IORES_MEM] = {
78 		.flags = IORESOURCE_MEM,
79 	},
80 	[INTEL_QUARK_IORES_IRQ] = {
81 		.flags = IORESOURCE_IRQ,
82 	},
83 };
84 
85 static struct mfd_cell_acpi_match intel_quark_acpi_match_i2c = {
86 	.adr = MFD_ACPI_MATCH_I2C,
87 };
88 
89 /* This is used as a place holder and will be modified at run-time */
90 static struct resource intel_quark_gpio_res[] = {
91 	[INTEL_QUARK_IORES_MEM] = {
92 		.flags = IORESOURCE_MEM,
93 	},
94 };
95 
96 static struct mfd_cell_acpi_match intel_quark_acpi_match_gpio = {
97 	.adr = MFD_ACPI_MATCH_GPIO,
98 };
99 
100 static struct mfd_cell intel_quark_mfd_cells[] = {
101 	{
102 		.id = MFD_GPIO_BAR,
103 		.name = "gpio-dwapb",
104 		.acpi_match = &intel_quark_acpi_match_gpio,
105 		.num_resources = ARRAY_SIZE(intel_quark_gpio_res),
106 		.resources = intel_quark_gpio_res,
107 		.ignore_resource_conflicts = true,
108 	},
109 	{
110 		.id = MFD_I2C_BAR,
111 		.name = "i2c_designware",
112 		.acpi_match = &intel_quark_acpi_match_i2c,
113 		.num_resources = ARRAY_SIZE(intel_quark_i2c_res),
114 		.resources = intel_quark_i2c_res,
115 		.ignore_resource_conflicts = true,
116 	},
117 };
118 
119 static const struct pci_device_id intel_quark_mfd_ids[] = {
120 	{ PCI_VDEVICE(INTEL, 0x0934), },
121 	{},
122 };
123 MODULE_DEVICE_TABLE(pci, intel_quark_mfd_ids);
124 
125 static int intel_quark_register_i2c_clk(struct device *dev)
126 {
127 	struct intel_quark_mfd *quark_mfd = dev_get_drvdata(dev);
128 	struct clk *i2c_clk;
129 
130 	i2c_clk = clk_register_fixed_rate(dev,
131 					  INTEL_QUARK_I2C_CONTROLLER_CLK, NULL,
132 					  0, INTEL_QUARK_I2C_CLK_HZ);
133 	if (IS_ERR(i2c_clk))
134 		return PTR_ERR(i2c_clk);
135 
136 	quark_mfd->i2c_clk = i2c_clk;
137 	quark_mfd->i2c_clk_lookup = clkdev_create(i2c_clk, NULL,
138 						INTEL_QUARK_I2C_CONTROLLER_CLK);
139 
140 	if (!quark_mfd->i2c_clk_lookup) {
141 		clk_unregister(quark_mfd->i2c_clk);
142 		dev_err(dev, "Fixed clk register failed\n");
143 		return -ENOMEM;
144 	}
145 
146 	return 0;
147 }
148 
149 static void intel_quark_unregister_i2c_clk(struct device *dev)
150 {
151 	struct intel_quark_mfd *quark_mfd = dev_get_drvdata(dev);
152 
153 	if (!quark_mfd->i2c_clk_lookup)
154 		return;
155 
156 	clkdev_drop(quark_mfd->i2c_clk_lookup);
157 	clk_unregister(quark_mfd->i2c_clk);
158 }
159 
160 static int intel_quark_i2c_setup(struct pci_dev *pdev, struct mfd_cell *cell)
161 {
162 	const struct dmi_system_id *dmi_id;
163 	struct dw_i2c_platform_data *pdata;
164 	struct resource *res = (struct resource *)cell->resources;
165 	struct device *dev = &pdev->dev;
166 
167 	res[INTEL_QUARK_IORES_MEM].start =
168 		pci_resource_start(pdev, MFD_I2C_BAR);
169 	res[INTEL_QUARK_IORES_MEM].end =
170 		pci_resource_end(pdev, MFD_I2C_BAR);
171 
172 	res[INTEL_QUARK_IORES_IRQ].start = pdev->irq;
173 	res[INTEL_QUARK_IORES_IRQ].end = pdev->irq;
174 
175 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
176 	if (!pdata)
177 		return -ENOMEM;
178 
179 	/* Normal mode by default */
180 	pdata->i2c_scl_freq = 100000;
181 
182 	dmi_id = dmi_first_match(dmi_platform_info);
183 	if (dmi_id)
184 		pdata->i2c_scl_freq = (uintptr_t)dmi_id->driver_data;
185 
186 	cell->platform_data = pdata;
187 	cell->pdata_size = sizeof(*pdata);
188 
189 	return 0;
190 }
191 
192 static int intel_quark_gpio_setup(struct pci_dev *pdev, struct mfd_cell *cell)
193 {
194 	struct dwapb_platform_data *pdata;
195 	struct resource *res = (struct resource *)cell->resources;
196 	struct device *dev = &pdev->dev;
197 
198 	res[INTEL_QUARK_IORES_MEM].start =
199 		pci_resource_start(pdev, MFD_GPIO_BAR);
200 	res[INTEL_QUARK_IORES_MEM].end =
201 		pci_resource_end(pdev, MFD_GPIO_BAR);
202 
203 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
204 	if (!pdata)
205 		return -ENOMEM;
206 
207 	/* For intel quark x1000, it has only one port: portA */
208 	pdata->nports = INTEL_QUARK_GPIO_NPORTS;
209 	pdata->properties = devm_kcalloc(dev, pdata->nports,
210 					 sizeof(*pdata->properties),
211 					 GFP_KERNEL);
212 	if (!pdata->properties)
213 		return -ENOMEM;
214 
215 	/* Set the properties for portA */
216 	pdata->properties->fwnode	= NULL;
217 	pdata->properties->idx		= 0;
218 	pdata->properties->ngpio	= INTEL_QUARK_MFD_NGPIO;
219 	pdata->properties->gpio_base	= INTEL_QUARK_MFD_GPIO_BASE;
220 	pdata->properties->irq[0]	= pdev->irq;
221 	pdata->properties->irq_shared	= true;
222 
223 	cell->platform_data = pdata;
224 	cell->pdata_size = sizeof(*pdata);
225 
226 	return 0;
227 }
228 
229 static int intel_quark_mfd_probe(struct pci_dev *pdev,
230 				 const struct pci_device_id *id)
231 {
232 	struct intel_quark_mfd *quark_mfd;
233 	int ret;
234 
235 	ret = pcim_enable_device(pdev);
236 	if (ret)
237 		return ret;
238 
239 	quark_mfd = devm_kzalloc(&pdev->dev, sizeof(*quark_mfd), GFP_KERNEL);
240 	if (!quark_mfd)
241 		return -ENOMEM;
242 
243 	quark_mfd->dev = &pdev->dev;
244 	dev_set_drvdata(&pdev->dev, quark_mfd);
245 
246 	ret = intel_quark_register_i2c_clk(&pdev->dev);
247 	if (ret)
248 		return ret;
249 
250 	ret = intel_quark_i2c_setup(pdev, &intel_quark_mfd_cells[1]);
251 	if (ret)
252 		goto err_unregister_i2c_clk;
253 
254 	ret = intel_quark_gpio_setup(pdev, &intel_quark_mfd_cells[0]);
255 	if (ret)
256 		goto err_unregister_i2c_clk;
257 
258 	ret = mfd_add_devices(&pdev->dev, 0, intel_quark_mfd_cells,
259 			      ARRAY_SIZE(intel_quark_mfd_cells), NULL, 0,
260 			      NULL);
261 	if (ret)
262 		goto err_unregister_i2c_clk;
263 
264 	return 0;
265 
266 err_unregister_i2c_clk:
267 	intel_quark_unregister_i2c_clk(&pdev->dev);
268 	return ret;
269 }
270 
271 static void intel_quark_mfd_remove(struct pci_dev *pdev)
272 {
273 	intel_quark_unregister_i2c_clk(&pdev->dev);
274 	mfd_remove_devices(&pdev->dev);
275 }
276 
277 static struct pci_driver intel_quark_mfd_driver = {
278 	.name		= "intel_quark_mfd_i2c_gpio",
279 	.id_table	= intel_quark_mfd_ids,
280 	.probe		= intel_quark_mfd_probe,
281 	.remove		= intel_quark_mfd_remove,
282 };
283 
284 module_pci_driver(intel_quark_mfd_driver);
285 
286 MODULE_AUTHOR("Raymond Tan <raymond.tan@intel.com>");
287 MODULE_DESCRIPTION("Intel Quark MFD PCI driver for I2C & GPIO");
288 MODULE_LICENSE("GPL v2");
289