blob: 7946d6e38b87201e3904f3247d2807c2ed174ca4 [file] [log] [blame]
Raymond Tan60ae5b92015-02-02 10:52:51 +08001/*
2 * Intel Quark MFD PCI driver for I2C & GPIO
3 *
4 * Copyright(c) 2014 Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * Intel Quark PCI device for I2C and GPIO controller sharing the same
16 * PCI function. This PCI driver will split the 2 devices into their
17 * respective drivers.
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/mfd/core.h>
24#include <linux/clkdev.h>
25#include <linux/clk-provider.h>
26#include <linux/dmi.h>
27#include <linux/platform_data/gpio-dwapb.h>
28#include <linux/platform_data/i2c-designware.h>
29
30/* PCI BAR for register base address */
31#define MFD_I2C_BAR 0
32#define MFD_GPIO_BAR 1
33
Andy Shevchenko918fe702015-10-23 12:16:43 +030034/* ACPI _ADR value to match the child node */
35#define MFD_ACPI_MATCH_GPIO 0ULL
36#define MFD_ACPI_MATCH_I2C 1ULL
37
Raymond Tan60ae5b92015-02-02 10:52:51 +080038/* The base GPIO number under GPIOLIB framework */
39#define INTEL_QUARK_MFD_GPIO_BASE 8
40
41/* The default number of South-Cluster GPIO on Quark. */
42#define INTEL_QUARK_MFD_NGPIO 8
43
44/* The DesignWare GPIO ports on Quark. */
45#define INTEL_QUARK_GPIO_NPORTS 1
46
47#define INTEL_QUARK_IORES_MEM 0
48#define INTEL_QUARK_IORES_IRQ 1
49
50#define INTEL_QUARK_I2C_CONTROLLER_CLK "i2c_designware.0"
51
52/* The Quark I2C controller source clock */
53#define INTEL_QUARK_I2C_CLK_HZ 33000000
54
Raymond Tan60ae5b92015-02-02 10:52:51 +080055struct intel_quark_mfd {
Andy Shevchenko9caac882016-02-19 10:42:11 +020056 struct device *dev;
Raymond Tan60ae5b92015-02-02 10:52:51 +080057 struct clk *i2c_clk;
58 struct clk_lookup *i2c_clk_lookup;
59};
60
61struct i2c_mode_info {
62 const char *name;
63 unsigned int i2c_scl_freq;
64};
65
66static const struct i2c_mode_info platform_i2c_mode_info[] = {
67 {
68 .name = "Galileo",
69 .i2c_scl_freq = 100000,
70 },
71 {
72 .name = "GalileoGen2",
73 .i2c_scl_freq = 400000,
74 },
Andy Shevchenkobafc1fa2015-03-25 20:03:55 +020075 {}
Raymond Tan60ae5b92015-02-02 10:52:51 +080076};
77
78static struct resource intel_quark_i2c_res[] = {
79 [INTEL_QUARK_IORES_MEM] = {
80 .flags = IORESOURCE_MEM,
81 },
82 [INTEL_QUARK_IORES_IRQ] = {
83 .flags = IORESOURCE_IRQ,
84 },
85};
86
Andy Shevchenko918fe702015-10-23 12:16:43 +030087static struct mfd_cell_acpi_match intel_quark_acpi_match_i2c = {
88 .adr = MFD_ACPI_MATCH_I2C,
89};
90
Raymond Tan60ae5b92015-02-02 10:52:51 +080091static struct resource intel_quark_gpio_res[] = {
92 [INTEL_QUARK_IORES_MEM] = {
93 .flags = IORESOURCE_MEM,
94 },
95};
96
Andy Shevchenko918fe702015-10-23 12:16:43 +030097static struct mfd_cell_acpi_match intel_quark_acpi_match_gpio = {
98 .adr = MFD_ACPI_MATCH_GPIO,
99};
100
Raymond Tan60ae5b92015-02-02 10:52:51 +0800101static struct mfd_cell intel_quark_mfd_cells[] = {
102 {
Raymond Tan60ae5b92015-02-02 10:52:51 +0800103 .id = MFD_GPIO_BAR,
104 .name = "gpio-dwapb",
Andy Shevchenko918fe702015-10-23 12:16:43 +0300105 .acpi_match = &intel_quark_acpi_match_gpio,
Raymond Tan60ae5b92015-02-02 10:52:51 +0800106 .num_resources = ARRAY_SIZE(intel_quark_gpio_res),
107 .resources = intel_quark_gpio_res,
108 .ignore_resource_conflicts = true,
109 },
Andy Shevchenko236fd462015-10-23 12:16:42 +0300110 {
111 .id = MFD_I2C_BAR,
112 .name = "i2c_designware",
Andy Shevchenko918fe702015-10-23 12:16:43 +0300113 .acpi_match = &intel_quark_acpi_match_i2c,
Andy Shevchenko236fd462015-10-23 12:16:42 +0300114 .num_resources = ARRAY_SIZE(intel_quark_i2c_res),
115 .resources = intel_quark_i2c_res,
116 .ignore_resource_conflicts = true,
117 },
Raymond Tan60ae5b92015-02-02 10:52:51 +0800118};
119
120static const struct pci_device_id intel_quark_mfd_ids[] = {
121 { PCI_VDEVICE(INTEL, 0x0934), },
122 {},
123};
124MODULE_DEVICE_TABLE(pci, intel_quark_mfd_ids);
125
Andy Shevchenko9caac882016-02-19 10:42:11 +0200126static int intel_quark_register_i2c_clk(struct device *dev)
Raymond Tan60ae5b92015-02-02 10:52:51 +0800127{
Andy Shevchenko9caac882016-02-19 10:42:11 +0200128 struct intel_quark_mfd *quark_mfd = dev_get_drvdata(dev);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800129 struct clk *i2c_clk;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800130
Andy Shevchenko9caac882016-02-19 10:42:11 +0200131 i2c_clk = clk_register_fixed_rate(dev,
Raymond Tan60ae5b92015-02-02 10:52:51 +0800132 INTEL_QUARK_I2C_CONTROLLER_CLK, NULL,
Stephen Boyd36a0c082016-04-19 18:19:20 -0700133 0, INTEL_QUARK_I2C_CLK_HZ);
Stephen Boydc4726ab2016-02-08 17:45:28 -0800134 if (IS_ERR(i2c_clk))
135 return PTR_ERR(i2c_clk);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800136
Raymond Tan60ae5b92015-02-02 10:52:51 +0800137 quark_mfd->i2c_clk = i2c_clk;
Stephen Boydc4726ab2016-02-08 17:45:28 -0800138 quark_mfd->i2c_clk_lookup = clkdev_create(i2c_clk, NULL,
139 INTEL_QUARK_I2C_CONTROLLER_CLK);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800140
Stephen Boydc4726ab2016-02-08 17:45:28 -0800141 if (!quark_mfd->i2c_clk_lookup) {
Andy Shevchenko7f0c5ae2016-02-19 10:42:10 +0200142 clk_unregister(quark_mfd->i2c_clk);
Andy Shevchenko9caac882016-02-19 10:42:11 +0200143 dev_err(dev, "Fixed clk register failed\n");
Stephen Boydc4726ab2016-02-08 17:45:28 -0800144 return -ENOMEM;
145 }
Raymond Tan60ae5b92015-02-02 10:52:51 +0800146
Stephen Boydc4726ab2016-02-08 17:45:28 -0800147 return 0;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800148}
149
Andy Shevchenko9caac882016-02-19 10:42:11 +0200150static void intel_quark_unregister_i2c_clk(struct device *dev)
Raymond Tan60ae5b92015-02-02 10:52:51 +0800151{
Andy Shevchenko9caac882016-02-19 10:42:11 +0200152 struct intel_quark_mfd *quark_mfd = dev_get_drvdata(dev);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800153
Andy Shevchenko7f0c5ae2016-02-19 10:42:10 +0200154 if (!quark_mfd->i2c_clk_lookup)
Raymond Tan60ae5b92015-02-02 10:52:51 +0800155 return;
156
157 clkdev_drop(quark_mfd->i2c_clk_lookup);
158 clk_unregister(quark_mfd->i2c_clk);
159}
160
161static int intel_quark_i2c_setup(struct pci_dev *pdev, struct mfd_cell *cell)
162{
163 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
Andy Shevchenkobafc1fa2015-03-25 20:03:55 +0200164 const struct i2c_mode_info *info;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800165 struct dw_i2c_platform_data *pdata;
166 struct resource *res = (struct resource *)cell->resources;
167 struct device *dev = &pdev->dev;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800168
169 res[INTEL_QUARK_IORES_MEM].start =
170 pci_resource_start(pdev, MFD_I2C_BAR);
171 res[INTEL_QUARK_IORES_MEM].end =
172 pci_resource_end(pdev, MFD_I2C_BAR);
173
174 res[INTEL_QUARK_IORES_IRQ].start = pdev->irq;
175 res[INTEL_QUARK_IORES_IRQ].end = pdev->irq;
176
177 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
178 if (!pdata)
179 return -ENOMEM;
180
Andy Shevchenkobafc1fa2015-03-25 20:03:55 +0200181 /* Normal mode by default */
182 pdata->i2c_scl_freq = 100000;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800183
Andy Shevchenkobafc1fa2015-03-25 20:03:55 +0200184 if (board_name) {
185 for (info = platform_i2c_mode_info; info->name; info++) {
186 if (!strcmp(board_name, info->name)) {
187 pdata->i2c_scl_freq = info->i2c_scl_freq;
188 break;
189 }
190 }
191 }
Raymond Tan60ae5b92015-02-02 10:52:51 +0800192
193 cell->platform_data = pdata;
194 cell->pdata_size = sizeof(*pdata);
195
196 return 0;
197}
198
199static int intel_quark_gpio_setup(struct pci_dev *pdev, struct mfd_cell *cell)
200{
201 struct dwapb_platform_data *pdata;
202 struct resource *res = (struct resource *)cell->resources;
203 struct device *dev = &pdev->dev;
204
205 res[INTEL_QUARK_IORES_MEM].start =
206 pci_resource_start(pdev, MFD_GPIO_BAR);
207 res[INTEL_QUARK_IORES_MEM].end =
208 pci_resource_end(pdev, MFD_GPIO_BAR);
209
210 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
211 if (!pdata)
212 return -ENOMEM;
213
214 /* For intel quark x1000, it has only one port: portA */
215 pdata->nports = INTEL_QUARK_GPIO_NPORTS;
216 pdata->properties = devm_kcalloc(dev, pdata->nports,
217 sizeof(*pdata->properties),
218 GFP_KERNEL);
219 if (!pdata->properties)
220 return -ENOMEM;
221
222 /* Set the properties for portA */
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800223 pdata->properties->fwnode = NULL;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800224 pdata->properties->idx = 0;
225 pdata->properties->ngpio = INTEL_QUARK_MFD_NGPIO;
226 pdata->properties->gpio_base = INTEL_QUARK_MFD_GPIO_BASE;
227 pdata->properties->irq = pdev->irq;
228 pdata->properties->irq_shared = true;
229
230 cell->platform_data = pdata;
231 cell->pdata_size = sizeof(*pdata);
232
233 return 0;
234}
235
236static int intel_quark_mfd_probe(struct pci_dev *pdev,
237 const struct pci_device_id *id)
238{
239 struct intel_quark_mfd *quark_mfd;
240 int ret;
241
242 ret = pcim_enable_device(pdev);
243 if (ret)
244 return ret;
245
246 quark_mfd = devm_kzalloc(&pdev->dev, sizeof(*quark_mfd), GFP_KERNEL);
247 if (!quark_mfd)
248 return -ENOMEM;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800249
Andy Shevchenko9caac882016-02-19 10:42:11 +0200250 quark_mfd->dev = &pdev->dev;
Andy Shevchenko7f0c5ae2016-02-19 10:42:10 +0200251 dev_set_drvdata(&pdev->dev, quark_mfd);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800252
Andy Shevchenko9caac882016-02-19 10:42:11 +0200253 ret = intel_quark_register_i2c_clk(&pdev->dev);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800254 if (ret)
255 return ret;
256
Andy Shevchenko236fd462015-10-23 12:16:42 +0300257 ret = intel_quark_i2c_setup(pdev, &intel_quark_mfd_cells[1]);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800258 if (ret)
Andy Shevchenko7f0c5ae2016-02-19 10:42:10 +0200259 goto err_unregister_i2c_clk;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800260
Andy Shevchenko236fd462015-10-23 12:16:42 +0300261 ret = intel_quark_gpio_setup(pdev, &intel_quark_mfd_cells[0]);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800262 if (ret)
Andy Shevchenko7f0c5ae2016-02-19 10:42:10 +0200263 goto err_unregister_i2c_clk;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800264
Andy Shevchenko7f0c5ae2016-02-19 10:42:10 +0200265 ret = mfd_add_devices(&pdev->dev, 0, intel_quark_mfd_cells,
266 ARRAY_SIZE(intel_quark_mfd_cells), NULL, 0,
267 NULL);
268 if (ret)
269 goto err_unregister_i2c_clk;
270
271 return 0;
272
273err_unregister_i2c_clk:
Andy Shevchenko9caac882016-02-19 10:42:11 +0200274 intel_quark_unregister_i2c_clk(&pdev->dev);
Andy Shevchenko7f0c5ae2016-02-19 10:42:10 +0200275 return ret;
Raymond Tan60ae5b92015-02-02 10:52:51 +0800276}
277
278static void intel_quark_mfd_remove(struct pci_dev *pdev)
279{
Andy Shevchenko9caac882016-02-19 10:42:11 +0200280 intel_quark_unregister_i2c_clk(&pdev->dev);
Raymond Tan60ae5b92015-02-02 10:52:51 +0800281 mfd_remove_devices(&pdev->dev);
282}
283
284static struct pci_driver intel_quark_mfd_driver = {
285 .name = "intel_quark_mfd_i2c_gpio",
286 .id_table = intel_quark_mfd_ids,
287 .probe = intel_quark_mfd_probe,
288 .remove = intel_quark_mfd_remove,
289};
290
291module_pci_driver(intel_quark_mfd_driver);
292
293MODULE_AUTHOR("Raymond Tan <raymond.tan@intel.com>");
294MODULE_DESCRIPTION("Intel Quark MFD PCI driver for I2C & GPIO");
295MODULE_LICENSE("GPL v2");