blob: d2223b5f922b5dba8d4d78c7b2762a9b706637d5 [file] [log] [blame]
Dirk Brandewiefe20ff52011-10-06 11:26:35 -07001/*
2 * Synopsys DesignWare I2C adapter driver (master only).
3 *
4 * Based on the TI DAVINCI I2C adapter driver.
5 *
6 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd.
9 * Copyright (C) 2011 Intel corporation.
10 *
11 * ----------------------------------------------------------------------------
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * ----------------------------------------------------------------------------
27 *
28 */
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/delay.h>
33#include <linux/i2c.h>
34#include <linux/errno.h>
35#include <linux/sched.h>
36#include <linux/err.h>
37#include <linux/interrupt.h>
38#include <linux/io.h>
39#include <linux/slab.h>
40#include <linux/pci.h>
Dirk Brandewie18dbdda2011-10-06 11:26:36 -070041#include <linux/pm_runtime.h>
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070042#include "i2c-designware-core.h"
43
44#define DRIVER_NAME "i2c-designware-pci"
45
46enum dw_pci_ctl_id_t {
47 moorestown_0,
48 moorestown_1,
49 moorestown_2,
50
51 medfield_0,
52 medfield_1,
53 medfield_2,
54 medfield_3,
55 medfield_4,
56 medfield_5,
57};
58
59struct dw_pci_controller {
60 u32 bus_num;
61 u32 bus_cfg;
62 u32 tx_fifo_depth;
63 u32 rx_fifo_depth;
64 u32 clk_khz;
65};
66
67#define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
68 DW_IC_CON_SLAVE_DISABLE | \
69 DW_IC_CON_RESTART_EN)
70
71static struct dw_pci_controller dw_pci_controllers[] = {
72 [moorestown_0] = {
73 .bus_num = 0,
74 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
75 .tx_fifo_depth = 32,
76 .rx_fifo_depth = 32,
77 .clk_khz = 25000,
78 },
79 [moorestown_1] = {
80 .bus_num = 1,
81 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
82 .tx_fifo_depth = 32,
83 .rx_fifo_depth = 32,
84 .clk_khz = 25000,
85 },
86 [moorestown_2] = {
87 .bus_num = 2,
88 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
89 .tx_fifo_depth = 32,
90 .rx_fifo_depth = 32,
91 .clk_khz = 25000,
92 },
93 [medfield_0] = {
94 .bus_num = 0,
95 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
96 .tx_fifo_depth = 32,
97 .rx_fifo_depth = 32,
98 .clk_khz = 25000,
99 },
100 [medfield_1] = {
101 .bus_num = 1,
102 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
103 .tx_fifo_depth = 32,
104 .rx_fifo_depth = 32,
105 .clk_khz = 25000,
106 },
107 [medfield_2] = {
108 .bus_num = 2,
109 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
110 .tx_fifo_depth = 32,
111 .rx_fifo_depth = 32,
112 .clk_khz = 25000,
113 },
114 [medfield_3] = {
115 .bus_num = 3,
116 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_STD,
117 .tx_fifo_depth = 32,
118 .rx_fifo_depth = 32,
119 .clk_khz = 25000,
120 },
121 [medfield_4] = {
122 .bus_num = 4,
123 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
124 .tx_fifo_depth = 32,
125 .rx_fifo_depth = 32,
126 .clk_khz = 25000,
127 },
128 [medfield_5] = {
129 .bus_num = 5,
130 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
131 .tx_fifo_depth = 32,
132 .rx_fifo_depth = 32,
133 .clk_khz = 25000,
134 },
135};
136static struct i2c_algorithm i2c_dw_algo = {
137 .master_xfer = i2c_dw_xfer,
138 .functionality = i2c_dw_func,
139};
140
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700141static int i2c_dw_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
142{
143 struct dw_i2c_dev *i2c = pci_get_drvdata(pdev);
144 int err;
145
146
147 i2c_dw_disable(i2c);
148
149 err = pci_save_state(pdev);
150 if (err) {
151 dev_err(&pdev->dev, "pci_save_state failed\n");
152 return err;
153 }
154
155 err = pci_set_power_state(pdev, PCI_D3hot);
156 if (err) {
157 dev_err(&pdev->dev, "pci_set_power_state failed\n");
158 return err;
159 }
160
161 return 0;
162}
163
164static int i2c_dw_pci_runtime_suspend(struct device *dev)
165{
166 struct pci_dev *pdev = to_pci_dev(dev);
167 dev_dbg(dev, "PCI suspend called\n");
168 return i2c_dw_pci_suspend(pdev, PMSG_SUSPEND);
169}
170
171static int i2c_dw_pci_resume(struct pci_dev *pdev)
172{
173 struct dw_i2c_dev *i2c = pci_get_drvdata(pdev);
174 int err;
175 u32 enabled;
176
177 enabled = i2c_dw_is_enabled(i2c);
178 if (enabled)
179 return 0;
180
181 err = pci_set_power_state(pdev, PCI_D0);
182 if (err) {
183 dev_err(&pdev->dev, "pci_set_power_state() failed\n");
184 return err;
185 }
186
187 pci_restore_state(pdev);
188
189 i2c_dw_init(i2c);
190 i2c_dw_enable(i2c);
191 return 0;
192}
193
194static int i2c_dw_pci_runtime_resume(struct device *dev)
195{
196 struct pci_dev *pdev = to_pci_dev(dev);
197 dev_dbg(dev, "runtime_resume called\n");
198 return i2c_dw_pci_resume(pdev);
199}
200
201static int i2c_dw_pci_runtime_idle(struct device *dev)
202{
203 int err = pm_schedule_suspend(dev, 500);
204 dev_dbg(dev, "runtime_idle called\n");
205
206 if (err != 0)
207 return 0;
208 return -EBUSY;
209}
210
211static const struct dev_pm_ops i2c_dw_pm_ops = {
212 .runtime_suspend = i2c_dw_pci_runtime_suspend,
213 .runtime_resume = i2c_dw_pci_runtime_resume,
214 .runtime_idle = i2c_dw_pci_runtime_idle,
215};
216
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700217static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
218{
219 return dev->controller->clk_khz;
220}
221
222static int __devinit i2c_dw_pci_probe(struct pci_dev *pdev,
223const struct pci_device_id *id)
224{
225 struct dw_i2c_dev *dev;
226 struct i2c_adapter *adap;
227 unsigned long start, len;
228 void __iomem *base;
229 int r;
230 struct dw_pci_controller *controller;
231
232 if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
233 printk(KERN_ERR "dw_i2c_pci_probe: invalid driver data %ld\n",
234 id->driver_data);
235 return -EINVAL;
236 }
237
238 controller = &dw_pci_controllers[id->driver_data];
239
240 r = pci_enable_device(pdev);
241 if (r) {
242 dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
243 r);
244 goto exit;
245 }
246
247 /* Determine the address of the I2C area */
248 start = pci_resource_start(pdev, 0);
249 len = pci_resource_len(pdev, 0);
250 if (!start || len == 0) {
251 dev_err(&pdev->dev, "base address not set\n");
252 r = -ENODEV;
253 goto exit;
254 }
255
256 r = pci_request_region(pdev, 0, DRIVER_NAME);
257 if (r) {
258 dev_err(&pdev->dev, "failed to request I2C region "
259 "0x%lx-0x%lx\n", start,
260 (unsigned long)pci_resource_end(pdev, 0));
261 goto exit;
262 }
263
264 base = ioremap_nocache(start, len);
265 if (!base) {
266 dev_err(&pdev->dev, "I/O memory remapping failed\n");
267 r = -ENOMEM;
268 goto err_release_region;
269 }
270
271
272 dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
273 if (!dev) {
274 r = -ENOMEM;
275 goto err_release_region;
276 }
277
278 init_completion(&dev->cmd_complete);
279 mutex_init(&dev->lock);
280 dev->clk = NULL;
281 dev->controller = controller;
282 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
283 dev->base = base;
284 dev->dev = get_device(&pdev->dev);
285 dev->functionality =
286 I2C_FUNC_I2C |
287 I2C_FUNC_SMBUS_BYTE |
288 I2C_FUNC_SMBUS_BYTE_DATA |
289 I2C_FUNC_SMBUS_WORD_DATA |
290 I2C_FUNC_SMBUS_I2C_BLOCK;
291 dev->master_cfg = controller->bus_cfg;
292
293 pci_set_drvdata(pdev, dev);
294
295 dev->tx_fifo_depth = controller->tx_fifo_depth;
296 dev->rx_fifo_depth = controller->rx_fifo_depth;
297 r = i2c_dw_init(dev);
298 if (r)
299 goto err_iounmap;
300
301 adap = &dev->adapter;
302 i2c_set_adapdata(adap, dev);
303 adap->owner = THIS_MODULE;
304 adap->class = 0;
305 adap->algo = &i2c_dw_algo;
306 adap->dev.parent = &pdev->dev;
307 adap->nr = controller->bus_num;
308 snprintf(adap->name, sizeof(adap->name), "i2c-designware-pci-%d",
309 adap->nr);
310
311 r = request_irq(pdev->irq, i2c_dw_isr, IRQF_SHARED, adap->name, dev);
312 if (r) {
313 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
314 goto err_iounmap;
315 }
316
317 i2c_dw_disable_int(dev);
318 i2c_dw_clear_int(dev);
319 r = i2c_add_numbered_adapter(adap);
320 if (r) {
321 dev_err(&pdev->dev, "failure adding adapter\n");
322 goto err_free_irq;
323 }
324
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700325 pm_runtime_put_noidle(&pdev->dev);
326 pm_runtime_allow(&pdev->dev);
327
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700328 return 0;
329
330err_free_irq:
331 free_irq(pdev->irq, dev);
332err_iounmap:
333 iounmap(dev->base);
334 pci_set_drvdata(pdev, NULL);
335 put_device(&pdev->dev);
336 kfree(dev);
337err_release_region:
338 pci_release_region(pdev, 0);
339exit:
340 return r;
341}
342
343static void __devexit i2c_dw_pci_remove(struct pci_dev *pdev)
344{
345 struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
346
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700347 i2c_dw_disable(dev);
348 pm_runtime_forbid(&pdev->dev);
349 pm_runtime_get_noresume(&pdev->dev);
350
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700351 pci_set_drvdata(pdev, NULL);
352 i2c_del_adapter(&dev->adapter);
353 put_device(&pdev->dev);
354
355 free_irq(dev->irq, dev);
356 kfree(dev);
357 pci_release_region(pdev, 0);
358}
359
360/* work with hotplug and coldplug */
361MODULE_ALIAS("i2c_designware-pci");
362
363DEFINE_PCI_DEVICE_TABLE(i2_designware_pci_ids) = {
364 /* Moorestown */
365 { PCI_VDEVICE(INTEL, 0x0802), moorestown_0 },
366 { PCI_VDEVICE(INTEL, 0x0803), moorestown_1 },
367 { PCI_VDEVICE(INTEL, 0x0804), moorestown_2 },
368 /* Medfield */
369 { PCI_VDEVICE(INTEL, 0x0817), medfield_3,},
370 { PCI_VDEVICE(INTEL, 0x0818), medfield_4 },
371 { PCI_VDEVICE(INTEL, 0x0819), medfield_5 },
372 { PCI_VDEVICE(INTEL, 0x082C), medfield_0 },
373 { PCI_VDEVICE(INTEL, 0x082D), medfield_1 },
374 { PCI_VDEVICE(INTEL, 0x082E), medfield_2 },
375 { 0,}
376};
377MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
378
379static struct pci_driver dw_i2c_driver = {
380 .name = DRIVER_NAME,
381 .id_table = i2_designware_pci_ids,
382 .probe = i2c_dw_pci_probe,
383 .remove = __devexit_p(i2c_dw_pci_remove),
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700384 .resume = i2c_dw_pci_resume,
385 .suspend = i2c_dw_pci_suspend,
386 .driver = {
387 .pm = &i2c_dw_pm_ops,
388 },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700389};
390
391static int __init dw_i2c_init_driver(void)
392{
393 return pci_register_driver(&dw_i2c_driver);
394}
395module_init(dw_i2c_init_driver);
396
397static void __exit dw_i2c_exit_driver(void)
398{
399 pci_unregister_driver(&dw_i2c_driver);
400}
401module_exit(dw_i2c_exit_driver);
402
403MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
404MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
405MODULE_LICENSE("GPL");