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.
22 * ----------------------------------------------------------------------------
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/delay.h>
29 #include <linux/i2c.h>
30 #include <linux/errno.h>
31 #include <linux/sched.h>
32 #include <linux/err.h>
33 #include <linux/interrupt.h>
35 #include <linux/slab.h>
36 #include <linux/pci.h>
37 #include <linux/pm_runtime.h>
38 #include "i2c-designware-core.h"
40 #define DRIVER_NAME "i2c-designware-pci"
42 enum dw_pci_ctl_id_t {
55 struct dw_pci_controller {
63 #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
64 DW_IC_CON_SLAVE_DISABLE | \
67 static struct dw_pci_controller dw_pci_controllers[] = {
70 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
77 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
84 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
91 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
98 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
105 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
112 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_STD,
119 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
126 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
132 static struct i2c_algorithm i2c_dw_algo = {
133 .master_xfer = i2c_dw_xfer,
134 .functionality = i2c_dw_func,
137 static int i2c_dw_pci_suspend(struct device *dev)
139 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
140 struct dw_i2c_dev *i2c = pci_get_drvdata(pdev);
146 err = pci_save_state(pdev);
148 dev_err(&pdev->dev, "pci_save_state failed\n");
152 err = pci_set_power_state(pdev, PCI_D3hot);
154 dev_err(&pdev->dev, "pci_set_power_state failed\n");
161 static int i2c_dw_pci_resume(struct device *dev)
163 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
164 struct dw_i2c_dev *i2c = pci_get_drvdata(pdev);
168 enabled = i2c_dw_is_enabled(i2c);
172 err = pci_set_power_state(pdev, PCI_D0);
174 dev_err(&pdev->dev, "pci_set_power_state() failed\n");
178 pci_restore_state(pdev);
184 static int i2c_dw_pci_runtime_idle(struct device *dev)
186 int err = pm_schedule_suspend(dev, 500);
187 dev_dbg(dev, "runtime_idle called\n");
194 static const struct dev_pm_ops i2c_dw_pm_ops = {
195 .resume = i2c_dw_pci_resume,
196 .suspend = i2c_dw_pci_suspend,
197 SET_RUNTIME_PM_OPS(i2c_dw_pci_suspend, i2c_dw_pci_resume,
198 i2c_dw_pci_runtime_idle)
201 static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
203 return dev->controller->clk_khz;
206 static int i2c_dw_pci_probe(struct pci_dev *pdev,
207 const struct pci_device_id *id)
209 struct dw_i2c_dev *dev;
210 struct i2c_adapter *adap;
212 struct dw_pci_controller *controller;
214 if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
215 dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
220 controller = &dw_pci_controllers[id->driver_data];
222 r = pcim_enable_device(pdev);
224 dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
229 r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
231 dev_err(&pdev->dev, "I/O memory remapping failed\n");
235 dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
239 init_completion(&dev->cmd_complete);
240 mutex_init(&dev->lock);
242 dev->controller = controller;
243 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
244 dev->base = pcim_iomap_table(pdev)[0];
245 dev->dev = &pdev->dev;
248 I2C_FUNC_SMBUS_BYTE |
249 I2C_FUNC_SMBUS_BYTE_DATA |
250 I2C_FUNC_SMBUS_WORD_DATA |
251 I2C_FUNC_SMBUS_I2C_BLOCK;
252 dev->master_cfg = controller->bus_cfg;
254 pci_set_drvdata(pdev, dev);
256 dev->tx_fifo_depth = controller->tx_fifo_depth;
257 dev->rx_fifo_depth = controller->rx_fifo_depth;
258 r = i2c_dw_init(dev);
262 adap = &dev->adapter;
263 i2c_set_adapdata(adap, dev);
264 adap->owner = THIS_MODULE;
266 adap->algo = &i2c_dw_algo;
267 adap->dev.parent = &pdev->dev;
268 adap->nr = controller->bus_num;
269 snprintf(adap->name, sizeof(adap->name), "i2c-designware-pci-%d",
272 r = devm_request_irq(&pdev->dev, pdev->irq, i2c_dw_isr, IRQF_SHARED,
275 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
279 i2c_dw_disable_int(dev);
280 i2c_dw_clear_int(dev);
281 r = i2c_add_numbered_adapter(adap);
283 dev_err(&pdev->dev, "failure adding adapter\n");
287 pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
288 pm_runtime_use_autosuspend(&pdev->dev);
289 pm_runtime_allow(&pdev->dev);
294 static void i2c_dw_pci_remove(struct pci_dev *pdev)
296 struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
299 pm_runtime_forbid(&pdev->dev);
300 pm_runtime_get_noresume(&pdev->dev);
302 i2c_del_adapter(&dev->adapter);
305 /* work with hotplug and coldplug */
306 MODULE_ALIAS("i2c_designware-pci");
308 static const struct pci_device_id i2_designware_pci_ids[] = {
310 { PCI_VDEVICE(INTEL, 0x0802), moorestown_0 },
311 { PCI_VDEVICE(INTEL, 0x0803), moorestown_1 },
312 { PCI_VDEVICE(INTEL, 0x0804), moorestown_2 },
314 { PCI_VDEVICE(INTEL, 0x0817), medfield_3,},
315 { PCI_VDEVICE(INTEL, 0x0818), medfield_4 },
316 { PCI_VDEVICE(INTEL, 0x0819), medfield_5 },
317 { PCI_VDEVICE(INTEL, 0x082C), medfield_0 },
318 { PCI_VDEVICE(INTEL, 0x082D), medfield_1 },
319 { PCI_VDEVICE(INTEL, 0x082E), medfield_2 },
322 MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
324 static struct pci_driver dw_i2c_driver = {
326 .id_table = i2_designware_pci_ids,
327 .probe = i2c_dw_pci_probe,
328 .remove = i2c_dw_pci_remove,
330 .pm = &i2c_dw_pm_ops,
334 module_pci_driver(dw_i2c_driver);
336 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
337 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
338 MODULE_LICENSE("GPL");