blob: 96f8230cd2d3352fa52678bc4be9f6e2f39d397f [file] [log] [blame]
Dirk Brandewiefe20ff52011-10-06 11:26:35 -07001/*
2 * Synopsys DesignWare I2C adapter driver (master only).
3 *
4 * Based on the TI DAVINCI I2C adapter driver.
5 *
6 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd.
Andy Shevchenko45bc35e2016-06-15 18:05:07 +03009 * Copyright (C) 2011, 2015, 2016 Intel Corporation.
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070010 *
11 * ----------------------------------------------------------------------------
12 *
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.
17 *
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.
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070022 * ----------------------------------------------------------------------------
23 *
24 */
25
Andy Shevchenko45bc35e2016-06-15 18:05:07 +030026#include <linux/acpi.h>
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070027#include <linux/delay.h>
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070028#include <linux/err.h>
Andy Shevchenko45bc35e2016-06-15 18:05:07 +030029#include <linux/errno.h>
30#include <linux/i2c.h>
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070031#include <linux/interrupt.h>
32#include <linux/io.h>
Andy Shevchenko45bc35e2016-06-15 18:05:07 +030033#include <linux/kernel.h>
34#include <linux/module.h>
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070035#include <linux/pci.h>
Dirk Brandewie18dbdda2011-10-06 11:26:36 -070036#include <linux/pm_runtime.h>
Andy Shevchenko45bc35e2016-06-15 18:05:07 +030037#include <linux/sched.h>
38#include <linux/slab.h>
39
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070040#include "i2c-designware-core.h"
41
42#define DRIVER_NAME "i2c-designware-pci"
43
44enum dw_pci_ctl_id_t {
Andy Shevchenkoed1bf032016-06-15 18:05:05 +030045 medfield,
Andy Shevchenkob20551c2016-06-15 18:05:06 +030046 merrifield,
Mika Westerberg089c7292014-02-19 16:10:29 +020047 baytrail,
Mika Westerberg157a8012014-05-15 17:37:24 +030048 haswell,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070049};
50
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080051struct dw_scl_sda_cfg {
52 u32 ss_hcnt;
53 u32 fs_hcnt;
54 u32 ss_lcnt;
55 u32 fs_lcnt;
56 u32 sda_hold;
57};
58
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070059struct dw_pci_controller {
60 u32 bus_num;
61 u32 bus_cfg;
62 u32 tx_fifo_depth;
63 u32 rx_fifo_depth;
64 u32 clk_khz;
Chew, Chiau Eececcd292014-03-07 22:12:50 +080065 u32 functionality;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080066 struct dw_scl_sda_cfg *scl_sda_cfg;
Andy Shevchenkoed1bf032016-06-15 18:05:05 +030067 int (*setup)(struct pci_dev *pdev, struct dw_pci_controller *c);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070068};
69
70#define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
71 DW_IC_CON_SLAVE_DISABLE | \
72 DW_IC_CON_RESTART_EN)
73
Chew, Chiau Eececcd292014-03-07 22:12:50 +080074#define DW_DEFAULT_FUNCTIONALITY (I2C_FUNC_I2C | \
75 I2C_FUNC_SMBUS_BYTE | \
76 I2C_FUNC_SMBUS_BYTE_DATA | \
77 I2C_FUNC_SMBUS_WORD_DATA | \
78 I2C_FUNC_SMBUS_I2C_BLOCK)
79
Andy Shevchenkob20551c2016-06-15 18:05:06 +030080/* Merrifield HCNT/LCNT/SDA hold time */
81static struct dw_scl_sda_cfg mrfld_config = {
82 .ss_hcnt = 0x2f8,
83 .fs_hcnt = 0x87,
84 .ss_lcnt = 0x37b,
85 .fs_lcnt = 0x10a,
86};
87
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080088/* BayTrail HCNT/LCNT/SDA hold time */
89static struct dw_scl_sda_cfg byt_config = {
90 .ss_hcnt = 0x200,
91 .fs_hcnt = 0x55,
92 .ss_lcnt = 0x200,
93 .fs_lcnt = 0x99,
94 .sda_hold = 0x6,
95};
96
Mika Westerberg157a8012014-05-15 17:37:24 +030097/* Haswell HCNT/LCNT/SDA hold time */
98static struct dw_scl_sda_cfg hsw_config = {
99 .ss_hcnt = 0x01b0,
100 .fs_hcnt = 0x48,
101 .ss_lcnt = 0x01fb,
102 .fs_lcnt = 0xa0,
103 .sda_hold = 0x9,
104};
105
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300106static int mfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
107{
108 switch (pdev->device) {
109 case 0x0817:
110 c->bus_cfg &= ~DW_IC_CON_SPEED_MASK;
111 c->bus_cfg |= DW_IC_CON_SPEED_STD;
112 case 0x0818:
113 case 0x0819:
114 c->bus_num = pdev->device - 0x817 + 3;
115 return 0;
116 case 0x082C:
117 case 0x082D:
118 case 0x082E:
119 c->bus_num = pdev->device - 0x82C + 0;
120 return 0;
121 }
122 return -ENODEV;
123}
124
Andy Shevchenkob20551c2016-06-15 18:05:06 +0300125static int mrfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
126{
127 /*
Andy Shevchenkoec2790e2016-06-20 11:58:09 +0300128 * On Intel Merrifield the user visible i2c busses are enumerated
129 * [1..7]. So, we add 1 to shift the default range. Besides that the
130 * first PCI slot provides 4 functions, that's why we have to add 0 to
131 * the first slot and 4 to the next one.
Andy Shevchenkob20551c2016-06-15 18:05:06 +0300132 */
133 switch (PCI_SLOT(pdev->devfn)) {
134 case 8:
135 c->bus_num = PCI_FUNC(pdev->devfn) + 0 + 1;
136 return 0;
137 case 9:
138 c->bus_num = PCI_FUNC(pdev->devfn) + 4 + 1;
139 return 0;
140 }
141 return -ENODEV;
142}
143
Andy Shevchenkoa93ac572015-02-06 13:47:02 +0200144static struct dw_pci_controller dw_pci_controllers[] = {
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300145 [medfield] = {
146 .bus_num = -1,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700147 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
148 .tx_fifo_depth = 32,
149 .rx_fifo_depth = 32,
150 .clk_khz = 25000,
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300151 .setup = mfld_setup,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700152 },
Andy Shevchenkob20551c2016-06-15 18:05:06 +0300153 [merrifield] = {
154 .bus_num = -1,
155 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
156 .tx_fifo_depth = 64,
157 .rx_fifo_depth = 64,
158 .scl_sda_cfg = &mrfld_config,
159 .setup = mrfld_setup,
160 },
Mika Westerberg089c7292014-02-19 16:10:29 +0200161 [baytrail] = {
162 .bus_num = -1,
163 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
164 .tx_fifo_depth = 32,
165 .rx_fifo_depth = 32,
Chew, Chiau Eececcd292014-03-07 22:12:50 +0800166 .functionality = I2C_FUNC_10BIT_ADDR,
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800167 .scl_sda_cfg = &byt_config,
Mika Westerberg089c7292014-02-19 16:10:29 +0200168 },
Mika Westerberg157a8012014-05-15 17:37:24 +0300169 [haswell] = {
170 .bus_num = -1,
171 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
172 .tx_fifo_depth = 32,
173 .rx_fifo_depth = 32,
Mika Westerberg157a8012014-05-15 17:37:24 +0300174 .functionality = I2C_FUNC_10BIT_ADDR,
175 .scl_sda_cfg = &hsw_config,
176 },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700177};
Alan Cox04095162014-07-23 13:06:57 +0100178
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200179#ifdef CONFIG_PM
Octavian Purdila52c28432011-10-06 11:26:37 -0700180static int i2c_dw_pci_suspend(struct device *dev)
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700181{
Geliang Tang238c44a2015-12-27 18:45:59 +0800182 struct pci_dev *pdev = to_pci_dev(dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700183
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200184 i2c_dw_disable(pci_get_drvdata(pdev));
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700185 return 0;
186}
187
Octavian Purdila52c28432011-10-06 11:26:37 -0700188static int i2c_dw_pci_resume(struct device *dev)
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700189{
Geliang Tang238c44a2015-12-27 18:45:59 +0800190 struct pci_dev *pdev = to_pci_dev(dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700191
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200192 return i2c_dw_init(pci_get_drvdata(pdev));
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700193}
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200194#endif
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700195
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200196static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
197 i2c_dw_pci_resume, NULL);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700198
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700199static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
200{
201 return dev->controller->clk_khz;
202}
203
Bill Pemberton0b255e92012-11-27 15:59:38 -0500204static int i2c_dw_pci_probe(struct pci_dev *pdev,
Andy Shevchenkoca0c1ff2013-04-10 00:36:37 +0000205 const struct pci_device_id *id)
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700206{
207 struct dw_i2c_dev *dev;
208 struct i2c_adapter *adap;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700209 int r;
Andy Shevchenko45bc35e2016-06-15 18:05:07 +0300210 struct dw_pci_controller *controller;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800211 struct dw_scl_sda_cfg *cfg;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700212
213 if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
Andy Shevchenkoca0c1ff2013-04-10 00:36:37 +0000214 dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700215 id->driver_data);
216 return -EINVAL;
217 }
218
219 controller = &dw_pci_controllers[id->driver_data];
220
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000221 r = pcim_enable_device(pdev);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700222 if (r) {
223 dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
224 r);
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000225 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700226 }
227
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000228 r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700229 if (r) {
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700230 dev_err(&pdev->dev, "I/O memory remapping failed\n");
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000231 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700232 }
233
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000234 dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
235 if (!dev)
236 return -ENOMEM;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700237
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700238 dev->clk = NULL;
239 dev->controller = controller;
240 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000241 dev->base = pcim_iomap_table(pdev)[0];
242 dev->dev = &pdev->dev;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300243 dev->irq = pdev->irq;
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300244
245 if (controller->setup) {
246 r = controller->setup(pdev, controller);
247 if (r)
248 return r;
249 }
250
Chew, Chiau Eececcd292014-03-07 22:12:50 +0800251 dev->functionality = controller->functionality |
252 DW_DEFAULT_FUNCTIONALITY;
253
Andy Shevchenkoa93ac572015-02-06 13:47:02 +0200254 dev->master_cfg = controller->bus_cfg;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800255 if (controller->scl_sda_cfg) {
256 cfg = controller->scl_sda_cfg;
257 dev->ss_hcnt = cfg->ss_hcnt;
258 dev->fs_hcnt = cfg->fs_hcnt;
259 dev->ss_lcnt = cfg->ss_lcnt;
260 dev->fs_lcnt = cfg->fs_lcnt;
261 dev->sda_hold_time = cfg->sda_hold;
262 }
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700263
264 pci_set_drvdata(pdev, dev);
265
266 dev->tx_fifo_depth = controller->tx_fifo_depth;
267 dev->rx_fifo_depth = controller->rx_fifo_depth;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700268
269 adap = &dev->adapter;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700270 adap->owner = THIS_MODULE;
271 adap->class = 0;
Dustin Byford8eb5c872015-10-23 12:27:07 -0700272 ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700273 adap->nr = controller->bus_num;
Mika Westerberg089c7292014-02-19 16:10:29 +0200274
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300275 r = i2c_dw_probe(dev);
276 if (r)
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000277 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700278
Mika Westerberg43452332013-04-10 00:36:42 +0000279 pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
280 pm_runtime_use_autosuspend(&pdev->dev);
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200281 pm_runtime_put_autosuspend(&pdev->dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700282 pm_runtime_allow(&pdev->dev);
283
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700284 return 0;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700285}
286
Bill Pemberton0b255e92012-11-27 15:59:38 -0500287static void i2c_dw_pci_remove(struct pci_dev *pdev)
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700288{
289 struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
290
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700291 i2c_dw_disable(dev);
292 pm_runtime_forbid(&pdev->dev);
293 pm_runtime_get_noresume(&pdev->dev);
294
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700295 i2c_del_adapter(&dev->adapter);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700296}
297
298/* work with hotplug and coldplug */
299MODULE_ALIAS("i2c_designware-pci");
300
Jingoo Han392debf2013-12-03 08:11:20 +0900301static const struct pci_device_id i2_designware_pci_ids[] = {
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700302 /* Medfield */
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300303 { PCI_VDEVICE(INTEL, 0x0817), medfield },
304 { PCI_VDEVICE(INTEL, 0x0818), medfield },
305 { PCI_VDEVICE(INTEL, 0x0819), medfield },
306 { PCI_VDEVICE(INTEL, 0x082C), medfield },
307 { PCI_VDEVICE(INTEL, 0x082D), medfield },
308 { PCI_VDEVICE(INTEL, 0x082E), medfield },
Andy Shevchenkob20551c2016-06-15 18:05:06 +0300309 /* Merrifield */
310 { PCI_VDEVICE(INTEL, 0x1195), merrifield },
311 { PCI_VDEVICE(INTEL, 0x1196), merrifield },
Mika Westerberg089c7292014-02-19 16:10:29 +0200312 /* Baytrail */
313 { PCI_VDEVICE(INTEL, 0x0F41), baytrail },
314 { PCI_VDEVICE(INTEL, 0x0F42), baytrail },
315 { PCI_VDEVICE(INTEL, 0x0F43), baytrail },
316 { PCI_VDEVICE(INTEL, 0x0F44), baytrail },
317 { PCI_VDEVICE(INTEL, 0x0F45), baytrail },
318 { PCI_VDEVICE(INTEL, 0x0F46), baytrail },
319 { PCI_VDEVICE(INTEL, 0x0F47), baytrail },
Mika Westerberg157a8012014-05-15 17:37:24 +0300320 /* Haswell */
321 { PCI_VDEVICE(INTEL, 0x9c61), haswell },
322 { PCI_VDEVICE(INTEL, 0x9c62), haswell },
Alan Cox04095162014-07-23 13:06:57 +0100323 /* Braswell / Cherrytrail */
Andy Shevchenkoa93ac572015-02-06 13:47:02 +0200324 { PCI_VDEVICE(INTEL, 0x22C1), baytrail },
Alan Cox04095162014-07-23 13:06:57 +0100325 { PCI_VDEVICE(INTEL, 0x22C2), baytrail },
326 { PCI_VDEVICE(INTEL, 0x22C3), baytrail },
327 { PCI_VDEVICE(INTEL, 0x22C4), baytrail },
328 { PCI_VDEVICE(INTEL, 0x22C5), baytrail },
329 { PCI_VDEVICE(INTEL, 0x22C6), baytrail },
330 { PCI_VDEVICE(INTEL, 0x22C7), baytrail },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700331 { 0,}
332};
333MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
334
335static struct pci_driver dw_i2c_driver = {
336 .name = DRIVER_NAME,
337 .id_table = i2_designware_pci_ids,
338 .probe = i2c_dw_pci_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500339 .remove = i2c_dw_pci_remove,
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700340 .driver = {
341 .pm = &i2c_dw_pm_ops,
342 },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700343};
344
Axel Lin56f21782012-07-24 14:13:56 +0200345module_pci_driver(dw_i2c_driver);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700346
347MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
348MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
349MODULE_LICENSE("GPL");