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
{
52 struct dw_scl_sda_cfg
{
60 struct dw_pci_controller
{
68 struct dw_scl_sda_cfg
*scl_sda_cfg
;
69 int (*setup
)(struct pci_dev
*pdev
, struct dw_pci_controller
*c
);
72 #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
73 DW_IC_CON_SLAVE_DISABLE | \
76 /* Merrifield HCNT/LCNT/SDA hold time */
77 static struct dw_scl_sda_cfg mrfld_config
= {
84 /* BayTrail HCNT/LCNT/SDA hold time */
85 static struct dw_scl_sda_cfg byt_config
= {
93 /* Haswell HCNT/LCNT/SDA hold time */
94 static struct dw_scl_sda_cfg hsw_config
= {
102 static int mfld_setup(struct pci_dev
*pdev
, struct dw_pci_controller
*c
)
104 switch (pdev
->device
) {
106 c
->bus_cfg
&= ~DW_IC_CON_SPEED_MASK
;
107 c
->bus_cfg
|= DW_IC_CON_SPEED_STD
;
110 c
->bus_num
= pdev
->device
- 0x817 + 3;
115 c
->bus_num
= pdev
->device
- 0x82C + 0;
121 static int mrfld_setup(struct pci_dev
*pdev
, struct dw_pci_controller
*c
)
124 * On Intel Merrifield the user visible i2c busses are enumerated
125 * [1..7]. So, we add 1 to shift the default range. Besides that the
126 * first PCI slot provides 4 functions, that's why we have to add 0 to
127 * the first slot and 4 to the next one.
129 switch (PCI_SLOT(pdev
->devfn
)) {
131 c
->bus_num
= PCI_FUNC(pdev
->devfn
) + 0 + 1;
134 c
->bus_num
= PCI_FUNC(pdev
->devfn
) + 4 + 1;
140 static struct dw_pci_controller dw_pci_controllers
[] = {
143 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
146 .functionality
= I2C_FUNC_10BIT_ADDR
,
152 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
155 .functionality
= I2C_FUNC_10BIT_ADDR
,
156 .scl_sda_cfg
= &mrfld_config
,
157 .setup
= mrfld_setup
,
161 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
164 .functionality
= I2C_FUNC_10BIT_ADDR
,
165 .scl_sda_cfg
= &byt_config
,
169 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
172 .functionality
= I2C_FUNC_10BIT_ADDR
,
173 .scl_sda_cfg
= &hsw_config
,
177 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
180 .functionality
= I2C_FUNC_10BIT_ADDR
,
181 .flags
= MODEL_CHERRYTRAIL
,
182 .scl_sda_cfg
= &byt_config
,
187 static int i2c_dw_pci_suspend(struct device
*dev
)
189 struct pci_dev
*pdev
= to_pci_dev(dev
);
190 struct dw_i2c_dev
*i_dev
= pci_get_drvdata(pdev
);
192 i_dev
->disable(i_dev
);
197 static int i2c_dw_pci_resume(struct device
*dev
)
199 struct pci_dev
*pdev
= to_pci_dev(dev
);
200 struct dw_i2c_dev
*i_dev
= pci_get_drvdata(pdev
);
202 return i_dev
->init(i_dev
);
206 static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops
, i2c_dw_pci_suspend
,
207 i2c_dw_pci_resume
, NULL
);
209 static u32
i2c_dw_get_clk_rate_khz(struct dw_i2c_dev
*dev
)
211 return dev
->controller
->clk_khz
;
214 static int i2c_dw_pci_probe(struct pci_dev
*pdev
,
215 const struct pci_device_id
*id
)
217 struct dw_i2c_dev
*dev
;
218 struct i2c_adapter
*adap
;
220 struct dw_pci_controller
*controller
;
221 struct dw_scl_sda_cfg
*cfg
;
223 if (id
->driver_data
>= ARRAY_SIZE(dw_pci_controllers
)) {
224 dev_err(&pdev
->dev
, "%s: invalid driver data %ld\n", __func__
,
229 controller
= &dw_pci_controllers
[id
->driver_data
];
231 r
= pcim_enable_device(pdev
);
233 dev_err(&pdev
->dev
, "Failed to enable I2C PCI device (%d)\n",
238 r
= pcim_iomap_regions(pdev
, 1 << 0, pci_name(pdev
));
240 dev_err(&pdev
->dev
, "I/O memory remapping failed\n");
244 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct dw_i2c_dev
), GFP_KERNEL
);
249 dev
->controller
= controller
;
250 dev
->get_clk_rate_khz
= i2c_dw_get_clk_rate_khz
;
251 dev
->base
= pcim_iomap_table(pdev
)[0];
252 dev
->dev
= &pdev
->dev
;
253 dev
->irq
= pdev
->irq
;
254 dev
->flags
|= controller
->flags
;
256 if (controller
->setup
) {
257 r
= controller
->setup(pdev
, controller
);
262 dev
->functionality
= controller
->functionality
|
263 DW_IC_DEFAULT_FUNCTIONALITY
;
265 dev
->master_cfg
= controller
->bus_cfg
;
266 if (controller
->scl_sda_cfg
) {
267 cfg
= controller
->scl_sda_cfg
;
268 dev
->ss_hcnt
= cfg
->ss_hcnt
;
269 dev
->fs_hcnt
= cfg
->fs_hcnt
;
270 dev
->ss_lcnt
= cfg
->ss_lcnt
;
271 dev
->fs_lcnt
= cfg
->fs_lcnt
;
272 dev
->sda_hold_time
= cfg
->sda_hold
;
275 pci_set_drvdata(pdev
, dev
);
277 dev
->tx_fifo_depth
= controller
->tx_fifo_depth
;
278 dev
->rx_fifo_depth
= controller
->rx_fifo_depth
;
280 adap
= &dev
->adapter
;
281 adap
->owner
= THIS_MODULE
;
283 ACPI_COMPANION_SET(&adap
->dev
, ACPI_COMPANION(&pdev
->dev
));
284 adap
->nr
= controller
->bus_num
;
286 r
= i2c_dw_probe(dev
);
290 pm_runtime_set_autosuspend_delay(&pdev
->dev
, 1000);
291 pm_runtime_use_autosuspend(&pdev
->dev
);
292 pm_runtime_put_autosuspend(&pdev
->dev
);
293 pm_runtime_allow(&pdev
->dev
);
298 static void i2c_dw_pci_remove(struct pci_dev
*pdev
)
300 struct dw_i2c_dev
*dev
= pci_get_drvdata(pdev
);
303 pm_runtime_forbid(&pdev
->dev
);
304 pm_runtime_get_noresume(&pdev
->dev
);
306 i2c_del_adapter(&dev
->adapter
);
309 /* work with hotplug and coldplug */
310 MODULE_ALIAS("i2c_designware-pci");
312 static const struct pci_device_id i2_designware_pci_ids
[] = {
314 { PCI_VDEVICE(INTEL
, 0x0817), medfield
},
315 { PCI_VDEVICE(INTEL
, 0x0818), medfield
},
316 { PCI_VDEVICE(INTEL
, 0x0819), medfield
},
317 { PCI_VDEVICE(INTEL
, 0x082C), medfield
},
318 { PCI_VDEVICE(INTEL
, 0x082D), medfield
},
319 { PCI_VDEVICE(INTEL
, 0x082E), medfield
},
321 { PCI_VDEVICE(INTEL
, 0x1195), merrifield
},
322 { PCI_VDEVICE(INTEL
, 0x1196), merrifield
},
324 { PCI_VDEVICE(INTEL
, 0x0F41), baytrail
},
325 { PCI_VDEVICE(INTEL
, 0x0F42), baytrail
},
326 { PCI_VDEVICE(INTEL
, 0x0F43), baytrail
},
327 { PCI_VDEVICE(INTEL
, 0x0F44), baytrail
},
328 { PCI_VDEVICE(INTEL
, 0x0F45), baytrail
},
329 { PCI_VDEVICE(INTEL
, 0x0F46), baytrail
},
330 { PCI_VDEVICE(INTEL
, 0x0F47), baytrail
},
332 { PCI_VDEVICE(INTEL
, 0x9c61), haswell
},
333 { PCI_VDEVICE(INTEL
, 0x9c62), haswell
},
334 /* Braswell / Cherrytrail */
335 { PCI_VDEVICE(INTEL
, 0x22C1), cherrytrail
},
336 { PCI_VDEVICE(INTEL
, 0x22C2), cherrytrail
},
337 { PCI_VDEVICE(INTEL
, 0x22C3), cherrytrail
},
338 { PCI_VDEVICE(INTEL
, 0x22C4), cherrytrail
},
339 { PCI_VDEVICE(INTEL
, 0x22C5), cherrytrail
},
340 { PCI_VDEVICE(INTEL
, 0x22C6), cherrytrail
},
341 { PCI_VDEVICE(INTEL
, 0x22C7), cherrytrail
},
344 MODULE_DEVICE_TABLE(pci
, i2_designware_pci_ids
);
346 static struct pci_driver dw_i2c_driver
= {
348 .id_table
= i2_designware_pci_ids
,
349 .probe
= i2c_dw_pci_probe
,
350 .remove
= i2c_dw_pci_remove
,
352 .pm
= &i2c_dw_pm_ops
,
356 module_pci_driver(dw_i2c_driver
);
358 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
359 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
360 MODULE_LICENSE("GPL");