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 <linux/pm_runtime.h>
42 #include "i2c-designware-core.h"
44 #define DRIVER_NAME "i2c-designware-pci"
46 enum dw_pci_ctl_id_t
{
62 struct dw_scl_sda_cfg
{
70 struct dw_pci_controller
{
77 struct dw_scl_sda_cfg
*scl_sda_cfg
;
80 #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
81 DW_IC_CON_SLAVE_DISABLE | \
84 #define DW_DEFAULT_FUNCTIONALITY (I2C_FUNC_I2C | \
85 I2C_FUNC_SMBUS_BYTE | \
86 I2C_FUNC_SMBUS_BYTE_DATA | \
87 I2C_FUNC_SMBUS_WORD_DATA | \
88 I2C_FUNC_SMBUS_I2C_BLOCK)
90 /* BayTrail HCNT/LCNT/SDA hold time */
91 static struct dw_scl_sda_cfg byt_config
= {
99 /* Haswell HCNT/LCNT/SDA hold time */
100 static struct dw_scl_sda_cfg hsw_config
= {
108 static struct dw_pci_controller dw_pci_controllers
[] = {
111 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
118 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
125 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
132 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
139 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
146 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
153 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_STD
,
160 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
167 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
174 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
178 .functionality
= I2C_FUNC_10BIT_ADDR
,
179 .scl_sda_cfg
= &byt_config
,
183 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
187 .functionality
= I2C_FUNC_10BIT_ADDR
,
188 .scl_sda_cfg
= &hsw_config
,
192 static struct i2c_algorithm i2c_dw_algo
= {
193 .master_xfer
= i2c_dw_xfer
,
194 .functionality
= i2c_dw_func
,
198 static int i2c_dw_pci_suspend(struct device
*dev
)
200 struct pci_dev
*pdev
= container_of(dev
, struct pci_dev
, dev
);
202 i2c_dw_disable(pci_get_drvdata(pdev
));
206 static int i2c_dw_pci_resume(struct device
*dev
)
208 struct pci_dev
*pdev
= container_of(dev
, struct pci_dev
, dev
);
210 return i2c_dw_init(pci_get_drvdata(pdev
));
214 static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops
, i2c_dw_pci_suspend
,
215 i2c_dw_pci_resume
, NULL
);
217 static u32
i2c_dw_get_clk_rate_khz(struct dw_i2c_dev
*dev
)
219 return dev
->controller
->clk_khz
;
222 static int i2c_dw_pci_probe(struct pci_dev
*pdev
,
223 const struct pci_device_id
*id
)
225 struct dw_i2c_dev
*dev
;
226 struct i2c_adapter
*adap
;
228 struct dw_pci_controller
*controller
;
229 struct dw_scl_sda_cfg
*cfg
;
231 if (id
->driver_data
>= ARRAY_SIZE(dw_pci_controllers
)) {
232 dev_err(&pdev
->dev
, "%s: invalid driver data %ld\n", __func__
,
237 controller
= &dw_pci_controllers
[id
->driver_data
];
239 r
= pcim_enable_device(pdev
);
241 dev_err(&pdev
->dev
, "Failed to enable I2C PCI device (%d)\n",
246 r
= pcim_iomap_regions(pdev
, 1 << 0, pci_name(pdev
));
248 dev_err(&pdev
->dev
, "I/O memory remapping failed\n");
252 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct dw_i2c_dev
), GFP_KERNEL
);
256 init_completion(&dev
->cmd_complete
);
257 mutex_init(&dev
->lock
);
259 dev
->controller
= controller
;
260 dev
->get_clk_rate_khz
= i2c_dw_get_clk_rate_khz
;
261 dev
->base
= pcim_iomap_table(pdev
)[0];
262 dev
->dev
= &pdev
->dev
;
263 dev
->functionality
= controller
->functionality
|
264 DW_DEFAULT_FUNCTIONALITY
;
266 dev
->master_cfg
= controller
->bus_cfg
;
267 if (controller
->scl_sda_cfg
) {
268 cfg
= controller
->scl_sda_cfg
;
269 dev
->ss_hcnt
= cfg
->ss_hcnt
;
270 dev
->fs_hcnt
= cfg
->fs_hcnt
;
271 dev
->ss_lcnt
= cfg
->ss_lcnt
;
272 dev
->fs_lcnt
= cfg
->fs_lcnt
;
273 dev
->sda_hold_time
= cfg
->sda_hold
;
276 pci_set_drvdata(pdev
, dev
);
278 dev
->tx_fifo_depth
= controller
->tx_fifo_depth
;
279 dev
->rx_fifo_depth
= controller
->rx_fifo_depth
;
280 r
= i2c_dw_init(dev
);
284 adap
= &dev
->adapter
;
285 i2c_set_adapdata(adap
, dev
);
286 adap
->owner
= THIS_MODULE
;
288 adap
->algo
= &i2c_dw_algo
;
289 adap
->dev
.parent
= &pdev
->dev
;
290 adap
->nr
= controller
->bus_num
;
292 snprintf(adap
->name
, sizeof(adap
->name
), "i2c-designware-pci");
294 r
= devm_request_irq(&pdev
->dev
, pdev
->irq
, i2c_dw_isr
, IRQF_SHARED
,
297 dev_err(&pdev
->dev
, "failure requesting irq %i\n", dev
->irq
);
301 i2c_dw_disable_int(dev
);
302 i2c_dw_clear_int(dev
);
303 r
= i2c_add_numbered_adapter(adap
);
305 dev_err(&pdev
->dev
, "failure adding adapter\n");
309 pm_runtime_set_autosuspend_delay(&pdev
->dev
, 1000);
310 pm_runtime_use_autosuspend(&pdev
->dev
);
311 pm_runtime_put_autosuspend(&pdev
->dev
);
312 pm_runtime_allow(&pdev
->dev
);
317 static void i2c_dw_pci_remove(struct pci_dev
*pdev
)
319 struct dw_i2c_dev
*dev
= pci_get_drvdata(pdev
);
322 pm_runtime_forbid(&pdev
->dev
);
323 pm_runtime_get_noresume(&pdev
->dev
);
325 i2c_del_adapter(&dev
->adapter
);
328 /* work with hotplug and coldplug */
329 MODULE_ALIAS("i2c_designware-pci");
331 static const struct pci_device_id i2_designware_pci_ids
[] = {
333 { PCI_VDEVICE(INTEL
, 0x0802), moorestown_0
},
334 { PCI_VDEVICE(INTEL
, 0x0803), moorestown_1
},
335 { PCI_VDEVICE(INTEL
, 0x0804), moorestown_2
},
337 { PCI_VDEVICE(INTEL
, 0x0817), medfield_3
,},
338 { PCI_VDEVICE(INTEL
, 0x0818), medfield_4
},
339 { PCI_VDEVICE(INTEL
, 0x0819), medfield_5
},
340 { PCI_VDEVICE(INTEL
, 0x082C), medfield_0
},
341 { PCI_VDEVICE(INTEL
, 0x082D), medfield_1
},
342 { PCI_VDEVICE(INTEL
, 0x082E), medfield_2
},
344 { PCI_VDEVICE(INTEL
, 0x0F41), baytrail
},
345 { PCI_VDEVICE(INTEL
, 0x0F42), baytrail
},
346 { PCI_VDEVICE(INTEL
, 0x0F43), baytrail
},
347 { PCI_VDEVICE(INTEL
, 0x0F44), baytrail
},
348 { PCI_VDEVICE(INTEL
, 0x0F45), baytrail
},
349 { PCI_VDEVICE(INTEL
, 0x0F46), baytrail
},
350 { PCI_VDEVICE(INTEL
, 0x0F47), baytrail
},
352 { PCI_VDEVICE(INTEL
, 0x9c61), haswell
},
353 { PCI_VDEVICE(INTEL
, 0x9c62), haswell
},
354 /* Braswell / Cherrytrail */
355 { PCI_VDEVICE(INTEL
, 0x22C1), baytrail
,},
356 { PCI_VDEVICE(INTEL
, 0x22C2), baytrail
},
357 { PCI_VDEVICE(INTEL
, 0x22C3), baytrail
},
358 { PCI_VDEVICE(INTEL
, 0x22C4), baytrail
},
359 { PCI_VDEVICE(INTEL
, 0x22C5), baytrail
},
360 { PCI_VDEVICE(INTEL
, 0x22C6), baytrail
},
361 { PCI_VDEVICE(INTEL
, 0x22C7), baytrail
},
364 MODULE_DEVICE_TABLE(pci
, i2_designware_pci_ids
);
366 static struct pci_driver dw_i2c_driver
= {
368 .id_table
= i2_designware_pci_ids
,
369 .probe
= i2c_dw_pci_probe
,
370 .remove
= i2c_dw_pci_remove
,
372 .pm
= &i2c_dw_pm_ops
,
376 module_pci_driver(dw_i2c_driver
);
378 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
379 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
380 MODULE_LICENSE("GPL");