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, 2015, 2016 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/acpi.h>
27 #include <linux/delay.h>
28 #include <linux/err.h>
29 #include <linux/errno.h>
30 #include <linux/i2c.h>
31 #include <linux/interrupt.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/pci.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
40 #include "i2c-designware-core.h"
42 #define DRIVER_NAME "i2c-designware-pci"
44 enum dw_pci_ctl_id_t {
51 struct dw_scl_sda_cfg {
59 struct dw_pci_controller {
66 struct dw_scl_sda_cfg *scl_sda_cfg;
67 int (*setup)(struct pci_dev *pdev, struct dw_pci_controller *c);
70 #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
71 DW_IC_CON_SLAVE_DISABLE | \
74 /* Merrifield HCNT/LCNT/SDA hold time */
75 static struct dw_scl_sda_cfg mrfld_config = {
82 /* BayTrail HCNT/LCNT/SDA hold time */
83 static struct dw_scl_sda_cfg byt_config = {
91 /* Haswell HCNT/LCNT/SDA hold time */
92 static struct dw_scl_sda_cfg hsw_config = {
100 static int mfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
102 switch (pdev->device) {
104 c->bus_cfg &= ~DW_IC_CON_SPEED_MASK;
105 c->bus_cfg |= DW_IC_CON_SPEED_STD;
108 c->bus_num = pdev->device - 0x817 + 3;
113 c->bus_num = pdev->device - 0x82C + 0;
119 static int mrfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
122 * On Intel Merrifield the user visible i2c busses are enumerated
123 * [1..7]. So, we add 1 to shift the default range. Besides that the
124 * first PCI slot provides 4 functions, that's why we have to add 0 to
125 * the first slot and 4 to the next one.
127 switch (PCI_SLOT(pdev->devfn)) {
129 c->bus_num = PCI_FUNC(pdev->devfn) + 0 + 1;
132 c->bus_num = PCI_FUNC(pdev->devfn) + 4 + 1;
138 static struct dw_pci_controller dw_pci_controllers[] = {
141 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
144 .functionality = I2C_FUNC_10BIT_ADDR,
150 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
153 .functionality = I2C_FUNC_10BIT_ADDR,
154 .scl_sda_cfg = &mrfld_config,
155 .setup = mrfld_setup,
159 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
162 .functionality = I2C_FUNC_10BIT_ADDR,
163 .scl_sda_cfg = &byt_config,
167 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
170 .functionality = I2C_FUNC_10BIT_ADDR,
171 .scl_sda_cfg = &hsw_config,
176 static int i2c_dw_pci_suspend(struct device *dev)
178 struct pci_dev *pdev = to_pci_dev(dev);
180 i2c_dw_disable(pci_get_drvdata(pdev));
184 static int i2c_dw_pci_resume(struct device *dev)
186 struct pci_dev *pdev = to_pci_dev(dev);
188 return i2c_dw_init(pci_get_drvdata(pdev));
192 static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
193 i2c_dw_pci_resume, NULL);
195 static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
197 return dev->controller->clk_khz;
200 static int i2c_dw_pci_probe(struct pci_dev *pdev,
201 const struct pci_device_id *id)
203 struct dw_i2c_dev *dev;
204 struct i2c_adapter *adap;
206 struct dw_pci_controller *controller;
207 struct dw_scl_sda_cfg *cfg;
209 if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
210 dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
215 controller = &dw_pci_controllers[id->driver_data];
217 r = pcim_enable_device(pdev);
219 dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
224 r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
226 dev_err(&pdev->dev, "I/O memory remapping failed\n");
230 dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
235 dev->controller = controller;
236 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
237 dev->base = pcim_iomap_table(pdev)[0];
238 dev->dev = &pdev->dev;
239 dev->irq = pdev->irq;
241 if (controller->setup) {
242 r = controller->setup(pdev, controller);
247 dev->functionality = controller->functionality |
248 DW_IC_DEFAULT_FUNCTIONALITY;
250 dev->master_cfg = controller->bus_cfg;
251 if (controller->scl_sda_cfg) {
252 cfg = controller->scl_sda_cfg;
253 dev->ss_hcnt = cfg->ss_hcnt;
254 dev->fs_hcnt = cfg->fs_hcnt;
255 dev->ss_lcnt = cfg->ss_lcnt;
256 dev->fs_lcnt = cfg->fs_lcnt;
257 dev->sda_hold_time = cfg->sda_hold;
260 pci_set_drvdata(pdev, dev);
262 dev->tx_fifo_depth = controller->tx_fifo_depth;
263 dev->rx_fifo_depth = controller->rx_fifo_depth;
265 adap = &dev->adapter;
266 adap->owner = THIS_MODULE;
268 ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
269 adap->nr = controller->bus_num;
271 r = i2c_dw_probe(dev);
275 pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
276 pm_runtime_use_autosuspend(&pdev->dev);
277 pm_runtime_put_autosuspend(&pdev->dev);
278 pm_runtime_allow(&pdev->dev);
283 static void i2c_dw_pci_remove(struct pci_dev *pdev)
285 struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
288 pm_runtime_forbid(&pdev->dev);
289 pm_runtime_get_noresume(&pdev->dev);
291 i2c_del_adapter(&dev->adapter);
294 /* work with hotplug and coldplug */
295 MODULE_ALIAS("i2c_designware-pci");
297 static const struct pci_device_id i2_designware_pci_ids[] = {
299 { PCI_VDEVICE(INTEL, 0x0817), medfield },
300 { PCI_VDEVICE(INTEL, 0x0818), medfield },
301 { PCI_VDEVICE(INTEL, 0x0819), medfield },
302 { PCI_VDEVICE(INTEL, 0x082C), medfield },
303 { PCI_VDEVICE(INTEL, 0x082D), medfield },
304 { PCI_VDEVICE(INTEL, 0x082E), medfield },
306 { PCI_VDEVICE(INTEL, 0x1195), merrifield },
307 { PCI_VDEVICE(INTEL, 0x1196), merrifield },
309 { PCI_VDEVICE(INTEL, 0x0F41), baytrail },
310 { PCI_VDEVICE(INTEL, 0x0F42), baytrail },
311 { PCI_VDEVICE(INTEL, 0x0F43), baytrail },
312 { PCI_VDEVICE(INTEL, 0x0F44), baytrail },
313 { PCI_VDEVICE(INTEL, 0x0F45), baytrail },
314 { PCI_VDEVICE(INTEL, 0x0F46), baytrail },
315 { PCI_VDEVICE(INTEL, 0x0F47), baytrail },
317 { PCI_VDEVICE(INTEL, 0x9c61), haswell },
318 { PCI_VDEVICE(INTEL, 0x9c62), haswell },
319 /* Braswell / Cherrytrail */
320 { PCI_VDEVICE(INTEL, 0x22C1), baytrail },
321 { PCI_VDEVICE(INTEL, 0x22C2), baytrail },
322 { PCI_VDEVICE(INTEL, 0x22C3), baytrail },
323 { PCI_VDEVICE(INTEL, 0x22C4), baytrail },
324 { PCI_VDEVICE(INTEL, 0x22C5), baytrail },
325 { PCI_VDEVICE(INTEL, 0x22C6), baytrail },
326 { PCI_VDEVICE(INTEL, 0x22C7), baytrail },
329 MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
331 static struct pci_driver dw_i2c_driver = {
333 .id_table = i2_designware_pci_ids,
334 .probe = i2c_dw_pci_probe,
335 .remove = i2c_dw_pci_remove,
337 .pm = &i2c_dw_pm_ops,
341 module_pci_driver(dw_i2c_driver);
343 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
344 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
345 MODULE_LICENSE("GPL");