1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Synopsys DesignWare I2C adapter driver (master only).
5 * Based on the TI DAVINCI I2C adapter driver.
7 * Copyright (C) 2006 Texas Instruments.
8 * Copyright (C) 2007 MontaVista Software Inc.
9 * Copyright (C) 2009 Provigent Ltd.
10 * Copyright (C) 2011, 2015, 2016 Intel Corporation.
12 #include <linux/acpi.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
26 #include "i2c-designware-core.h"
28 #define DRIVER_NAME "i2c-designware-pci"
30 enum dw_pci_ctl_id_t
{
39 struct dw_scl_sda_cfg
{
47 struct dw_pci_controller
{
55 struct dw_scl_sda_cfg
*scl_sda_cfg
;
56 int (*setup
)(struct pci_dev
*pdev
, struct dw_pci_controller
*c
);
59 #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
60 DW_IC_CON_SLAVE_DISABLE | \
63 /* Merrifield HCNT/LCNT/SDA hold time */
64 static struct dw_scl_sda_cfg mrfld_config
= {
71 /* BayTrail HCNT/LCNT/SDA hold time */
72 static struct dw_scl_sda_cfg byt_config
= {
80 /* Haswell HCNT/LCNT/SDA hold time */
81 static struct dw_scl_sda_cfg hsw_config
= {
89 static int mfld_setup(struct pci_dev
*pdev
, struct dw_pci_controller
*c
)
91 switch (pdev
->device
) {
93 c
->bus_cfg
&= ~DW_IC_CON_SPEED_MASK
;
94 c
->bus_cfg
|= DW_IC_CON_SPEED_STD
;
98 c
->bus_num
= pdev
->device
- 0x817 + 3;
103 c
->bus_num
= pdev
->device
- 0x82C + 0;
109 static int mrfld_setup(struct pci_dev
*pdev
, struct dw_pci_controller
*c
)
112 * On Intel Merrifield the user visible i2c busses are enumerated
113 * [1..7]. So, we add 1 to shift the default range. Besides that the
114 * first PCI slot provides 4 functions, that's why we have to add 0 to
115 * the first slot and 4 to the next one.
117 switch (PCI_SLOT(pdev
->devfn
)) {
119 c
->bus_num
= PCI_FUNC(pdev
->devfn
) + 0 + 1;
122 c
->bus_num
= PCI_FUNC(pdev
->devfn
) + 4 + 1;
128 static struct dw_pci_controller dw_pci_controllers
[] = {
131 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
134 .functionality
= I2C_FUNC_10BIT_ADDR
,
140 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
143 .functionality
= I2C_FUNC_10BIT_ADDR
,
144 .scl_sda_cfg
= &mrfld_config
,
145 .setup
= mrfld_setup
,
149 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
152 .functionality
= I2C_FUNC_10BIT_ADDR
,
153 .scl_sda_cfg
= &byt_config
,
157 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
160 .functionality
= I2C_FUNC_10BIT_ADDR
,
161 .scl_sda_cfg
= &hsw_config
,
165 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
168 .functionality
= I2C_FUNC_10BIT_ADDR
,
169 .flags
= MODEL_CHERRYTRAIL
,
170 .scl_sda_cfg
= &byt_config
,
174 .bus_cfg
= INTEL_MID_STD_CFG
| DW_IC_CON_SPEED_FAST
,
177 .functionality
= I2C_FUNC_10BIT_ADDR
,
183 static int i2c_dw_pci_suspend(struct device
*dev
)
185 struct dw_i2c_dev
*i_dev
= dev_get_drvdata(dev
);
187 i_dev
->suspended
= true;
188 i_dev
->disable(i_dev
);
193 static int i2c_dw_pci_resume(struct device
*dev
)
195 struct dw_i2c_dev
*i_dev
= dev_get_drvdata(dev
);
198 ret
= i_dev
->init(i_dev
);
199 i_dev
->suspended
= false;
205 static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops
, i2c_dw_pci_suspend
,
206 i2c_dw_pci_resume
, NULL
);
208 static u32
i2c_dw_get_clk_rate_khz(struct dw_i2c_dev
*dev
)
210 return dev
->controller
->clk_khz
;
213 static int i2c_dw_pci_probe(struct pci_dev
*pdev
,
214 const struct pci_device_id
*id
)
216 struct dw_i2c_dev
*dev
;
217 struct i2c_adapter
*adap
;
219 struct dw_pci_controller
*controller
;
220 struct dw_scl_sda_cfg
*cfg
;
222 if (id
->driver_data
>= ARRAY_SIZE(dw_pci_controllers
)) {
223 dev_err(&pdev
->dev
, "%s: invalid driver data %ld\n", __func__
,
228 controller
= &dw_pci_controllers
[id
->driver_data
];
230 r
= pcim_enable_device(pdev
);
232 dev_err(&pdev
->dev
, "Failed to enable I2C PCI device (%d)\n",
237 pci_set_master(pdev
);
239 r
= pcim_iomap_regions(pdev
, 1 << 0, pci_name(pdev
));
241 dev_err(&pdev
->dev
, "I/O memory remapping failed\n");
245 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct dw_i2c_dev
), GFP_KERNEL
);
249 r
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_ALL_TYPES
);
254 dev
->controller
= controller
;
255 dev
->get_clk_rate_khz
= i2c_dw_get_clk_rate_khz
;
256 dev
->base
= pcim_iomap_table(pdev
)[0];
257 dev
->dev
= &pdev
->dev
;
258 dev
->irq
= pci_irq_vector(pdev
, 0);
259 dev
->flags
|= controller
->flags
;
261 if (controller
->setup
) {
262 r
= controller
->setup(pdev
, controller
);
264 pci_free_irq_vectors(pdev
);
269 dev
->functionality
= controller
->functionality
|
270 DW_IC_DEFAULT_FUNCTIONALITY
;
272 dev
->master_cfg
= controller
->bus_cfg
;
273 if (controller
->scl_sda_cfg
) {
274 cfg
= controller
->scl_sda_cfg
;
275 dev
->ss_hcnt
= cfg
->ss_hcnt
;
276 dev
->fs_hcnt
= cfg
->fs_hcnt
;
277 dev
->ss_lcnt
= cfg
->ss_lcnt
;
278 dev
->fs_lcnt
= cfg
->fs_lcnt
;
279 dev
->sda_hold_time
= cfg
->sda_hold
;
282 pci_set_drvdata(pdev
, dev
);
284 dev
->tx_fifo_depth
= controller
->tx_fifo_depth
;
285 dev
->rx_fifo_depth
= controller
->rx_fifo_depth
;
287 adap
= &dev
->adapter
;
288 adap
->owner
= THIS_MODULE
;
290 ACPI_COMPANION_SET(&adap
->dev
, ACPI_COMPANION(&pdev
->dev
));
291 adap
->nr
= controller
->bus_num
;
293 r
= i2c_dw_probe(dev
);
295 pci_free_irq_vectors(pdev
);
299 pm_runtime_set_autosuspend_delay(&pdev
->dev
, 1000);
300 pm_runtime_use_autosuspend(&pdev
->dev
);
301 pm_runtime_put_autosuspend(&pdev
->dev
);
302 pm_runtime_allow(&pdev
->dev
);
307 static void i2c_dw_pci_remove(struct pci_dev
*pdev
)
309 struct dw_i2c_dev
*dev
= pci_get_drvdata(pdev
);
312 pm_runtime_forbid(&pdev
->dev
);
313 pm_runtime_get_noresume(&pdev
->dev
);
315 i2c_del_adapter(&dev
->adapter
);
316 pci_free_irq_vectors(pdev
);
319 /* work with hotplug and coldplug */
320 MODULE_ALIAS("i2c_designware-pci");
322 static const struct pci_device_id i2_designware_pci_ids
[] = {
324 { PCI_VDEVICE(INTEL
, 0x0817), medfield
},
325 { PCI_VDEVICE(INTEL
, 0x0818), medfield
},
326 { PCI_VDEVICE(INTEL
, 0x0819), medfield
},
327 { PCI_VDEVICE(INTEL
, 0x082C), medfield
},
328 { PCI_VDEVICE(INTEL
, 0x082D), medfield
},
329 { PCI_VDEVICE(INTEL
, 0x082E), medfield
},
331 { PCI_VDEVICE(INTEL
, 0x1195), merrifield
},
332 { PCI_VDEVICE(INTEL
, 0x1196), merrifield
},
334 { PCI_VDEVICE(INTEL
, 0x0F41), baytrail
},
335 { PCI_VDEVICE(INTEL
, 0x0F42), baytrail
},
336 { PCI_VDEVICE(INTEL
, 0x0F43), baytrail
},
337 { PCI_VDEVICE(INTEL
, 0x0F44), baytrail
},
338 { PCI_VDEVICE(INTEL
, 0x0F45), baytrail
},
339 { PCI_VDEVICE(INTEL
, 0x0F46), baytrail
},
340 { PCI_VDEVICE(INTEL
, 0x0F47), baytrail
},
342 { PCI_VDEVICE(INTEL
, 0x9c61), haswell
},
343 { PCI_VDEVICE(INTEL
, 0x9c62), haswell
},
344 /* Braswell / Cherrytrail */
345 { PCI_VDEVICE(INTEL
, 0x22C1), cherrytrail
},
346 { PCI_VDEVICE(INTEL
, 0x22C2), cherrytrail
},
347 { PCI_VDEVICE(INTEL
, 0x22C3), cherrytrail
},
348 { PCI_VDEVICE(INTEL
, 0x22C4), cherrytrail
},
349 { PCI_VDEVICE(INTEL
, 0x22C5), cherrytrail
},
350 { PCI_VDEVICE(INTEL
, 0x22C6), cherrytrail
},
351 { PCI_VDEVICE(INTEL
, 0x22C7), cherrytrail
},
352 /* Elkhart Lake (PSE I2C) */
353 { PCI_VDEVICE(INTEL
, 0x4bb9), elkhartlake
},
354 { PCI_VDEVICE(INTEL
, 0x4bba), elkhartlake
},
355 { PCI_VDEVICE(INTEL
, 0x4bbb), elkhartlake
},
356 { PCI_VDEVICE(INTEL
, 0x4bbc), elkhartlake
},
357 { PCI_VDEVICE(INTEL
, 0x4bbd), elkhartlake
},
358 { PCI_VDEVICE(INTEL
, 0x4bbe), elkhartlake
},
359 { PCI_VDEVICE(INTEL
, 0x4bbf), elkhartlake
},
360 { PCI_VDEVICE(INTEL
, 0x4bc0), elkhartlake
},
363 MODULE_DEVICE_TABLE(pci
, i2_designware_pci_ids
);
365 static struct pci_driver dw_i2c_driver
= {
367 .id_table
= i2_designware_pci_ids
,
368 .probe
= i2c_dw_pci_probe
,
369 .remove
= i2c_dw_pci_remove
,
371 .pm
= &i2c_dw_pm_ops
,
375 module_pci_driver(dw_i2c_driver
);
377 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
378 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
379 MODULE_LICENSE("GPL");