2 * Synopsys DesignWare I2C adapter driver (master only).
4 * Based on the TI DAVINCI I2C adapter driver.
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.
11 * ----------------------------------------------------------------------------
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.
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.
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 * ----------------------------------------------------------------------------
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>
39 #include <linux/slab.h>
40 #include <linux/pci.h>
41 #include "i2c-designware-core.h"
43 #define DRIVER_NAME "i2c-designware-pci"
45 enum dw_pci_ctl_id_t {
58 struct dw_pci_controller {
66 #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
67 DW_IC_CON_SLAVE_DISABLE | \
70 static struct dw_pci_controller dw_pci_controllers[] = {
73 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
80 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
87 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
94 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
101 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
108 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
115 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_STD,
122 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
129 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
135 static struct i2c_algorithm i2c_dw_algo = {
136 .master_xfer = i2c_dw_xfer,
137 .functionality = i2c_dw_func,
140 static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
142 return dev->controller->clk_khz;
145 static int __devinit i2c_dw_pci_probe(struct pci_dev *pdev,
146 const struct pci_device_id *id)
148 struct dw_i2c_dev *dev;
149 struct i2c_adapter *adap;
150 unsigned long start, len;
153 struct dw_pci_controller *controller;
155 if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
156 printk(KERN_ERR "dw_i2c_pci_probe: invalid driver data %ld\n",
161 controller = &dw_pci_controllers[id->driver_data];
163 r = pci_enable_device(pdev);
165 dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
170 /* Determine the address of the I2C area */
171 start = pci_resource_start(pdev, 0);
172 len = pci_resource_len(pdev, 0);
173 if (!start || len == 0) {
174 dev_err(&pdev->dev, "base address not set\n");
179 r = pci_request_region(pdev, 0, DRIVER_NAME);
181 dev_err(&pdev->dev, "failed to request I2C region "
182 "0x%lx-0x%lx\n", start,
183 (unsigned long)pci_resource_end(pdev, 0));
187 base = ioremap_nocache(start, len);
189 dev_err(&pdev->dev, "I/O memory remapping failed\n");
191 goto err_release_region;
195 dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
198 goto err_release_region;
201 init_completion(&dev->cmd_complete);
202 mutex_init(&dev->lock);
204 dev->controller = controller;
205 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
207 dev->dev = get_device(&pdev->dev);
210 I2C_FUNC_SMBUS_BYTE |
211 I2C_FUNC_SMBUS_BYTE_DATA |
212 I2C_FUNC_SMBUS_WORD_DATA |
213 I2C_FUNC_SMBUS_I2C_BLOCK;
214 dev->master_cfg = controller->bus_cfg;
216 pci_set_drvdata(pdev, dev);
218 dev->tx_fifo_depth = controller->tx_fifo_depth;
219 dev->rx_fifo_depth = controller->rx_fifo_depth;
220 r = i2c_dw_init(dev);
224 adap = &dev->adapter;
225 i2c_set_adapdata(adap, dev);
226 adap->owner = THIS_MODULE;
228 adap->algo = &i2c_dw_algo;
229 adap->dev.parent = &pdev->dev;
230 adap->nr = controller->bus_num;
231 snprintf(adap->name, sizeof(adap->name), "i2c-designware-pci-%d",
234 r = request_irq(pdev->irq, i2c_dw_isr, IRQF_SHARED, adap->name, dev);
236 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
240 i2c_dw_disable_int(dev);
241 i2c_dw_clear_int(dev);
242 r = i2c_add_numbered_adapter(adap);
244 dev_err(&pdev->dev, "failure adding adapter\n");
251 free_irq(pdev->irq, dev);
254 pci_set_drvdata(pdev, NULL);
255 put_device(&pdev->dev);
258 pci_release_region(pdev, 0);
263 static void __devexit i2c_dw_pci_remove(struct pci_dev *pdev)
265 struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
267 pci_set_drvdata(pdev, NULL);
268 i2c_del_adapter(&dev->adapter);
269 put_device(&pdev->dev);
271 free_irq(dev->irq, dev);
273 pci_release_region(pdev, 0);
276 /* work with hotplug and coldplug */
277 MODULE_ALIAS("i2c_designware-pci");
279 DEFINE_PCI_DEVICE_TABLE(i2_designware_pci_ids) = {
281 { PCI_VDEVICE(INTEL, 0x0802), moorestown_0 },
282 { PCI_VDEVICE(INTEL, 0x0803), moorestown_1 },
283 { PCI_VDEVICE(INTEL, 0x0804), moorestown_2 },
285 { PCI_VDEVICE(INTEL, 0x0817), medfield_3,},
286 { PCI_VDEVICE(INTEL, 0x0818), medfield_4 },
287 { PCI_VDEVICE(INTEL, 0x0819), medfield_5 },
288 { PCI_VDEVICE(INTEL, 0x082C), medfield_0 },
289 { PCI_VDEVICE(INTEL, 0x082D), medfield_1 },
290 { PCI_VDEVICE(INTEL, 0x082E), medfield_2 },
293 MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
295 static struct pci_driver dw_i2c_driver = {
297 .id_table = i2_designware_pci_ids,
298 .probe = i2c_dw_pci_probe,
299 .remove = __devexit_p(i2c_dw_pci_remove),
302 static int __init dw_i2c_init_driver(void)
304 return pci_register_driver(&dw_i2c_driver);
306 module_init(dw_i2c_init_driver);
308 static void __exit dw_i2c_exit_driver(void)
310 pci_unregister_driver(&dw_i2c_driver);
312 module_exit(dw_i2c_exit_driver);
314 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
315 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
316 MODULE_LICENSE("GPL");