blob: d31d313ab4f7694564012d3ad6d570a0c9f5f3a7 [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.
9 * Copyright (C) 2011 Intel corporation.
10 *
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.
22 *
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 * ----------------------------------------------------------------------------
27 *
28 */
29
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>
38#include <linux/io.h>
39#include <linux/slab.h>
40#include <linux/pci.h>
Dirk Brandewie18dbdda2011-10-06 11:26:36 -070041#include <linux/pm_runtime.h>
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070042#include "i2c-designware-core.h"
43
44#define DRIVER_NAME "i2c-designware-pci"
45
46enum dw_pci_ctl_id_t {
47 moorestown_0,
48 moorestown_1,
49 moorestown_2,
50
51 medfield_0,
52 medfield_1,
53 medfield_2,
54 medfield_3,
55 medfield_4,
56 medfield_5,
Mika Westerberg089c7292014-02-19 16:10:29 +020057
58 baytrail,
Mika Westerberg157a8012014-05-15 17:37:24 +030059 haswell,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070060};
61
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080062struct dw_scl_sda_cfg {
63 u32 ss_hcnt;
64 u32 fs_hcnt;
65 u32 ss_lcnt;
66 u32 fs_lcnt;
67 u32 sda_hold;
68};
69
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070070struct dw_pci_controller {
71 u32 bus_num;
72 u32 bus_cfg;
73 u32 tx_fifo_depth;
74 u32 rx_fifo_depth;
75 u32 clk_khz;
Chew, Chiau Eececcd292014-03-07 22:12:50 +080076 u32 functionality;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080077 struct dw_scl_sda_cfg *scl_sda_cfg;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070078};
79
80#define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
81 DW_IC_CON_SLAVE_DISABLE | \
82 DW_IC_CON_RESTART_EN)
83
Chew, Chiau Eececcd292014-03-07 22:12:50 +080084#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)
89
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080090/* BayTrail HCNT/LCNT/SDA hold time */
91static struct dw_scl_sda_cfg byt_config = {
92 .ss_hcnt = 0x200,
93 .fs_hcnt = 0x55,
94 .ss_lcnt = 0x200,
95 .fs_lcnt = 0x99,
96 .sda_hold = 0x6,
97};
98
Mika Westerberg157a8012014-05-15 17:37:24 +030099/* Haswell HCNT/LCNT/SDA hold time */
100static struct dw_scl_sda_cfg hsw_config = {
101 .ss_hcnt = 0x01b0,
102 .fs_hcnt = 0x48,
103 .ss_lcnt = 0x01fb,
104 .fs_lcnt = 0xa0,
105 .sda_hold = 0x9,
106};
107
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700108static struct dw_pci_controller dw_pci_controllers[] = {
109 [moorestown_0] = {
110 .bus_num = 0,
111 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
112 .tx_fifo_depth = 32,
113 .rx_fifo_depth = 32,
114 .clk_khz = 25000,
115 },
116 [moorestown_1] = {
117 .bus_num = 1,
118 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
119 .tx_fifo_depth = 32,
120 .rx_fifo_depth = 32,
121 .clk_khz = 25000,
122 },
123 [moorestown_2] = {
124 .bus_num = 2,
125 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
126 .tx_fifo_depth = 32,
127 .rx_fifo_depth = 32,
128 .clk_khz = 25000,
129 },
130 [medfield_0] = {
131 .bus_num = 0,
132 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
133 .tx_fifo_depth = 32,
134 .rx_fifo_depth = 32,
135 .clk_khz = 25000,
136 },
137 [medfield_1] = {
138 .bus_num = 1,
139 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
140 .tx_fifo_depth = 32,
141 .rx_fifo_depth = 32,
142 .clk_khz = 25000,
143 },
144 [medfield_2] = {
145 .bus_num = 2,
146 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
147 .tx_fifo_depth = 32,
148 .rx_fifo_depth = 32,
149 .clk_khz = 25000,
150 },
151 [medfield_3] = {
152 .bus_num = 3,
153 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_STD,
154 .tx_fifo_depth = 32,
155 .rx_fifo_depth = 32,
156 .clk_khz = 25000,
157 },
158 [medfield_4] = {
159 .bus_num = 4,
160 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
161 .tx_fifo_depth = 32,
162 .rx_fifo_depth = 32,
163 .clk_khz = 25000,
164 },
165 [medfield_5] = {
166 .bus_num = 5,
167 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
168 .tx_fifo_depth = 32,
169 .rx_fifo_depth = 32,
170 .clk_khz = 25000,
171 },
Mika Westerberg089c7292014-02-19 16:10:29 +0200172 [baytrail] = {
173 .bus_num = -1,
174 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
175 .tx_fifo_depth = 32,
176 .rx_fifo_depth = 32,
177 .clk_khz = 100000,
Chew, Chiau Eececcd292014-03-07 22:12:50 +0800178 .functionality = I2C_FUNC_10BIT_ADDR,
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800179 .scl_sda_cfg = &byt_config,
Mika Westerberg089c7292014-02-19 16:10:29 +0200180 },
Mika Westerberg157a8012014-05-15 17:37:24 +0300181 [haswell] = {
182 .bus_num = -1,
183 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
184 .tx_fifo_depth = 32,
185 .rx_fifo_depth = 32,
186 .clk_khz = 100000,
187 .functionality = I2C_FUNC_10BIT_ADDR,
188 .scl_sda_cfg = &hsw_config,
189 },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700190};
Alan Cox04095162014-07-23 13:06:57 +0100191
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700192static struct i2c_algorithm i2c_dw_algo = {
193 .master_xfer = i2c_dw_xfer,
194 .functionality = i2c_dw_func,
195};
196
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200197#ifdef CONFIG_PM
Octavian Purdila52c28432011-10-06 11:26:37 -0700198static int i2c_dw_pci_suspend(struct device *dev)
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700199{
Octavian Purdila52c28432011-10-06 11:26:37 -0700200 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700201
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200202 i2c_dw_disable(pci_get_drvdata(pdev));
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700203 return 0;
204}
205
Octavian Purdila52c28432011-10-06 11:26:37 -0700206static int i2c_dw_pci_resume(struct device *dev)
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700207{
Octavian Purdila52c28432011-10-06 11:26:37 -0700208 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700209
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200210 return i2c_dw_init(pci_get_drvdata(pdev));
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700211}
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200212#endif
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700213
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200214static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
215 i2c_dw_pci_resume, NULL);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700216
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700217static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
218{
219 return dev->controller->clk_khz;
220}
221
Bill Pemberton0b255e92012-11-27 15:59:38 -0500222static int i2c_dw_pci_probe(struct pci_dev *pdev,
Andy Shevchenkoca0c1ff2013-04-10 00:36:37 +0000223 const struct pci_device_id *id)
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700224{
225 struct dw_i2c_dev *dev;
226 struct i2c_adapter *adap;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700227 int r;
228 struct dw_pci_controller *controller;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800229 struct dw_scl_sda_cfg *cfg;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700230
231 if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
Andy Shevchenkoca0c1ff2013-04-10 00:36:37 +0000232 dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700233 id->driver_data);
234 return -EINVAL;
235 }
236
237 controller = &dw_pci_controllers[id->driver_data];
238
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000239 r = pcim_enable_device(pdev);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700240 if (r) {
241 dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
242 r);
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000243 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700244 }
245
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000246 r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700247 if (r) {
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700248 dev_err(&pdev->dev, "I/O memory remapping failed\n");
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000249 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700250 }
251
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000252 dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
253 if (!dev)
254 return -ENOMEM;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700255
256 init_completion(&dev->cmd_complete);
257 mutex_init(&dev->lock);
258 dev->clk = NULL;
259 dev->controller = controller;
260 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000261 dev->base = pcim_iomap_table(pdev)[0];
262 dev->dev = &pdev->dev;
Chew, Chiau Eececcd292014-03-07 22:12:50 +0800263 dev->functionality = controller->functionality |
264 DW_DEFAULT_FUNCTIONALITY;
265
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700266 dev->master_cfg = controller->bus_cfg;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800267 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;
274 }
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700275
276 pci_set_drvdata(pdev, dev);
277
278 dev->tx_fifo_depth = controller->tx_fifo_depth;
279 dev->rx_fifo_depth = controller->rx_fifo_depth;
280 r = i2c_dw_init(dev);
281 if (r)
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000282 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700283
284 adap = &dev->adapter;
285 i2c_set_adapdata(adap, dev);
286 adap->owner = THIS_MODULE;
287 adap->class = 0;
288 adap->algo = &i2c_dw_algo;
289 adap->dev.parent = &pdev->dev;
290 adap->nr = controller->bus_num;
Mika Westerberg089c7292014-02-19 16:10:29 +0200291
292 snprintf(adap->name, sizeof(adap->name), "i2c-designware-pci");
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700293
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000294 r = devm_request_irq(&pdev->dev, pdev->irq, i2c_dw_isr, IRQF_SHARED,
295 adap->name, dev);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700296 if (r) {
297 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000298 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700299 }
300
301 i2c_dw_disable_int(dev);
302 i2c_dw_clear_int(dev);
303 r = i2c_add_numbered_adapter(adap);
304 if (r) {
305 dev_err(&pdev->dev, "failure adding adapter\n");
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000306 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700307 }
308
Mika Westerberg43452332013-04-10 00:36:42 +0000309 pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
310 pm_runtime_use_autosuspend(&pdev->dev);
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200311 pm_runtime_put_autosuspend(&pdev->dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700312 pm_runtime_allow(&pdev->dev);
313
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700314 return 0;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700315}
316
Bill Pemberton0b255e92012-11-27 15:59:38 -0500317static void i2c_dw_pci_remove(struct pci_dev *pdev)
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700318{
319 struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
320
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700321 i2c_dw_disable(dev);
322 pm_runtime_forbid(&pdev->dev);
323 pm_runtime_get_noresume(&pdev->dev);
324
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700325 i2c_del_adapter(&dev->adapter);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700326}
327
328/* work with hotplug and coldplug */
329MODULE_ALIAS("i2c_designware-pci");
330
Jingoo Han392debf2013-12-03 08:11:20 +0900331static const struct pci_device_id i2_designware_pci_ids[] = {
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700332 /* Moorestown */
333 { PCI_VDEVICE(INTEL, 0x0802), moorestown_0 },
334 { PCI_VDEVICE(INTEL, 0x0803), moorestown_1 },
335 { PCI_VDEVICE(INTEL, 0x0804), moorestown_2 },
336 /* Medfield */
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 },
Mika Westerberg089c7292014-02-19 16:10:29 +0200343 /* Baytrail */
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 },
Mika Westerberg157a8012014-05-15 17:37:24 +0300351 /* Haswell */
352 { PCI_VDEVICE(INTEL, 0x9c61), haswell },
353 { PCI_VDEVICE(INTEL, 0x9c62), haswell },
Alan Cox04095162014-07-23 13:06:57 +0100354 /* 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 },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700362 { 0,}
363};
364MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
365
366static struct pci_driver dw_i2c_driver = {
367 .name = DRIVER_NAME,
368 .id_table = i2_designware_pci_ids,
369 .probe = i2c_dw_pci_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500370 .remove = i2c_dw_pci_remove,
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700371 .driver = {
372 .pm = &i2c_dw_pm_ops,
373 },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700374};
375
Axel Lin56f21782012-07-24 14:13:56 +0200376module_pci_driver(dw_i2c_driver);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700377
378MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
379MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
380MODULE_LICENSE("GPL");