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/delay.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/power_supply.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
27 #include "i2c-designware-core.h"
28 #include "i2c-ccgx-ucsi.h"
30 #define DRIVER_NAME "i2c-designware-pci"
32 enum dw_pci_ctl_id_t
{
43 * This is a legacy structure to describe the hardware counters
44 * to configure signal timings on the bus. For Device Tree platforms
45 * one should use the respective properties and for ACPI there is
46 * a set of ACPI methods that provide these counters. No new
47 * platform should use this structure.
49 struct dw_scl_sda_cfg
{
57 struct dw_pci_controller
{
60 struct dw_scl_sda_cfg
*scl_sda_cfg
;
61 int (*setup
)(struct pci_dev
*pdev
, struct dw_pci_controller
*c
);
62 u32 (*get_clk_rate_khz
)(struct dw_i2c_dev
*dev
);
65 /* Merrifield HCNT/LCNT/SDA hold time */
66 static struct dw_scl_sda_cfg mrfld_config
= {
73 /* BayTrail HCNT/LCNT/SDA hold time */
74 static struct dw_scl_sda_cfg byt_config
= {
82 /* Haswell HCNT/LCNT/SDA hold time */
83 static struct dw_scl_sda_cfg hsw_config
= {
91 /* NAVI-AMD HCNT/LCNT/SDA hold time */
92 static struct dw_scl_sda_cfg navi_amd_config
= {
98 static u32
mfld_get_clk_rate_khz(struct dw_i2c_dev
*dev
)
103 static int mfld_setup(struct pci_dev
*pdev
, struct dw_pci_controller
*c
)
105 struct dw_i2c_dev
*dev
= pci_get_drvdata(pdev
);
107 switch (pdev
->device
) {
109 dev
->timings
.bus_freq_hz
= I2C_MAX_STANDARD_MODE_FREQ
;
113 c
->bus_num
= pdev
->device
- 0x817 + 3;
118 c
->bus_num
= pdev
->device
- 0x82C + 0;
124 static int mrfld_setup(struct pci_dev
*pdev
, struct dw_pci_controller
*c
)
127 * On Intel Merrifield the user visible i2c buses are enumerated
128 * [1..7]. So, we add 1 to shift the default range. Besides that the
129 * first PCI slot provides 4 functions, that's why we have to add 0 to
130 * the first slot and 4 to the next one.
132 switch (PCI_SLOT(pdev
->devfn
)) {
134 c
->bus_num
= PCI_FUNC(pdev
->devfn
) + 0 + 1;
137 c
->bus_num
= PCI_FUNC(pdev
->devfn
) + 4 + 1;
143 static u32
ehl_get_clk_rate_khz(struct dw_i2c_dev
*dev
)
148 static u32
navi_amd_get_clk_rate_khz(struct dw_i2c_dev
*dev
)
153 static int navi_amd_setup(struct pci_dev
*pdev
, struct dw_pci_controller
*c
)
155 struct dw_i2c_dev
*dev
= pci_get_drvdata(pdev
);
157 dev
->flags
|= MODEL_AMD_NAVI_GPU
| ACCESS_POLLING
;
158 dev
->timings
.bus_freq_hz
= I2C_MAX_STANDARD_MODE_FREQ
;
162 static struct dw_pci_controller dw_pci_controllers
[] = {
166 .get_clk_rate_khz
= mfld_get_clk_rate_khz
,
170 .scl_sda_cfg
= &mrfld_config
,
171 .setup
= mrfld_setup
,
175 .scl_sda_cfg
= &byt_config
,
179 .scl_sda_cfg
= &hsw_config
,
183 .scl_sda_cfg
= &byt_config
,
187 .get_clk_rate_khz
= ehl_get_clk_rate_khz
,
191 .scl_sda_cfg
= &navi_amd_config
,
192 .setup
= navi_amd_setup
,
193 .get_clk_rate_khz
= navi_amd_get_clk_rate_khz
,
197 static const struct property_entry dgpu_properties
[] = {
198 /* USB-C doesn't power the system */
199 PROPERTY_ENTRY_U8("scope", POWER_SUPPLY_SCOPE_DEVICE
),
203 static const struct software_node dgpu_node
= {
204 .properties
= dgpu_properties
,
207 static int i2c_dw_pci_probe(struct pci_dev
*pdev
,
208 const struct pci_device_id
*id
)
210 struct device
*device
= &pdev
->dev
;
211 struct dw_i2c_dev
*dev
;
212 struct i2c_adapter
*adap
;
214 struct dw_pci_controller
*controller
;
215 struct dw_scl_sda_cfg
*cfg
;
217 if (id
->driver_data
>= ARRAY_SIZE(dw_pci_controllers
))
218 return dev_err_probe(device
, -EINVAL
, "Invalid driver data %ld\n",
221 controller
= &dw_pci_controllers
[id
->driver_data
];
223 r
= pcim_enable_device(pdev
);
225 return dev_err_probe(device
, r
, "Failed to enable I2C PCI device\n");
227 pci_set_master(pdev
);
229 r
= pcim_iomap_regions(pdev
, 1 << 0, pci_name(pdev
));
231 return dev_err_probe(device
, r
, "I/O memory remapping failed\n");
233 dev
= devm_kzalloc(device
, sizeof(*dev
), GFP_KERNEL
);
237 r
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_ALL_TYPES
);
241 dev
->get_clk_rate_khz
= controller
->get_clk_rate_khz
;
242 dev
->base
= pcim_iomap_table(pdev
)[0];
244 dev
->irq
= pci_irq_vector(pdev
, 0);
245 dev
->flags
|= controller
->flags
;
247 pci_set_drvdata(pdev
, dev
);
249 if (controller
->setup
) {
250 r
= controller
->setup(pdev
, controller
);
255 r
= i2c_dw_fw_parse_and_configure(dev
);
259 i2c_dw_configure(dev
);
261 if (controller
->scl_sda_cfg
) {
262 cfg
= controller
->scl_sda_cfg
;
263 dev
->ss_hcnt
= cfg
->ss_hcnt
;
264 dev
->fs_hcnt
= cfg
->fs_hcnt
;
265 dev
->ss_lcnt
= cfg
->ss_lcnt
;
266 dev
->fs_lcnt
= cfg
->fs_lcnt
;
267 dev
->sda_hold_time
= cfg
->sda_hold_time
;
270 adap
= &dev
->adapter
;
271 adap
->owner
= THIS_MODULE
;
273 adap
->nr
= controller
->bus_num
;
275 r
= i2c_dw_probe(dev
);
279 if ((dev
->flags
& MODEL_MASK
) == MODEL_AMD_NAVI_GPU
) {
280 dev
->slave
= i2c_new_ccgx_ucsi(&dev
->adapter
, dev
->irq
, &dgpu_node
);
281 if (IS_ERR(dev
->slave
))
282 return dev_err_probe(device
, PTR_ERR(dev
->slave
),
283 "register UCSI failed\n");
286 pm_runtime_set_autosuspend_delay(device
, 1000);
287 pm_runtime_use_autosuspend(device
);
288 pm_runtime_put_autosuspend(device
);
289 pm_runtime_allow(device
);
294 static void i2c_dw_pci_remove(struct pci_dev
*pdev
)
296 struct dw_i2c_dev
*dev
= pci_get_drvdata(pdev
);
297 struct device
*device
= &pdev
->dev
;
301 pm_runtime_forbid(device
);
302 pm_runtime_get_noresume(device
);
304 i2c_del_adapter(&dev
->adapter
);
307 static const struct pci_device_id i2c_designware_pci_ids
[] = {
309 { PCI_VDEVICE(INTEL
, 0x0817), medfield
},
310 { PCI_VDEVICE(INTEL
, 0x0818), medfield
},
311 { PCI_VDEVICE(INTEL
, 0x0819), medfield
},
312 { PCI_VDEVICE(INTEL
, 0x082C), medfield
},
313 { PCI_VDEVICE(INTEL
, 0x082D), medfield
},
314 { PCI_VDEVICE(INTEL
, 0x082E), medfield
},
316 { PCI_VDEVICE(INTEL
, 0x1195), merrifield
},
317 { PCI_VDEVICE(INTEL
, 0x1196), merrifield
},
319 { PCI_VDEVICE(INTEL
, 0x0F41), baytrail
},
320 { PCI_VDEVICE(INTEL
, 0x0F42), baytrail
},
321 { PCI_VDEVICE(INTEL
, 0x0F43), baytrail
},
322 { PCI_VDEVICE(INTEL
, 0x0F44), baytrail
},
323 { PCI_VDEVICE(INTEL
, 0x0F45), baytrail
},
324 { PCI_VDEVICE(INTEL
, 0x0F46), baytrail
},
325 { PCI_VDEVICE(INTEL
, 0x0F47), baytrail
},
327 { PCI_VDEVICE(INTEL
, 0x9c61), haswell
},
328 { PCI_VDEVICE(INTEL
, 0x9c62), haswell
},
329 /* Braswell / Cherrytrail */
330 { PCI_VDEVICE(INTEL
, 0x22C1), cherrytrail
},
331 { PCI_VDEVICE(INTEL
, 0x22C2), cherrytrail
},
332 { PCI_VDEVICE(INTEL
, 0x22C3), cherrytrail
},
333 { PCI_VDEVICE(INTEL
, 0x22C4), cherrytrail
},
334 { PCI_VDEVICE(INTEL
, 0x22C5), cherrytrail
},
335 { PCI_VDEVICE(INTEL
, 0x22C6), cherrytrail
},
336 { PCI_VDEVICE(INTEL
, 0x22C7), cherrytrail
},
337 /* Elkhart Lake (PSE I2C) */
338 { PCI_VDEVICE(INTEL
, 0x4bb9), elkhartlake
},
339 { PCI_VDEVICE(INTEL
, 0x4bba), elkhartlake
},
340 { PCI_VDEVICE(INTEL
, 0x4bbb), elkhartlake
},
341 { PCI_VDEVICE(INTEL
, 0x4bbc), elkhartlake
},
342 { PCI_VDEVICE(INTEL
, 0x4bbd), elkhartlake
},
343 { PCI_VDEVICE(INTEL
, 0x4bbe), elkhartlake
},
344 { PCI_VDEVICE(INTEL
, 0x4bbf), elkhartlake
},
345 { PCI_VDEVICE(INTEL
, 0x4bc0), elkhartlake
},
347 { PCI_VDEVICE(ATI
, 0x7314), navi_amd
},
348 { PCI_VDEVICE(ATI
, 0x73a4), navi_amd
},
349 { PCI_VDEVICE(ATI
, 0x73e4), navi_amd
},
350 { PCI_VDEVICE(ATI
, 0x73c4), navi_amd
},
351 { PCI_VDEVICE(ATI
, 0x7444), navi_amd
},
352 { PCI_VDEVICE(ATI
, 0x7464), navi_amd
},
355 MODULE_DEVICE_TABLE(pci
, i2c_designware_pci_ids
);
357 static struct pci_driver dw_i2c_driver
= {
359 .probe
= i2c_dw_pci_probe
,
360 .remove
= i2c_dw_pci_remove
,
362 .pm
= pm_ptr(&i2c_dw_dev_pm_ops
),
364 .id_table
= i2c_designware_pci_ids
,
366 module_pci_driver(dw_i2c_driver
);
368 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
369 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
370 MODULE_LICENSE("GPL");
371 MODULE_IMPORT_NS("I2C_DW");
372 MODULE_IMPORT_NS("I2C_DW_COMMON");