blob: a7aab105f73bdc8d55dd6460c69ba78b955402a4 [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 Shevchenkoa93ac572015-02-06 13:47:02 +02009 * Copyright (C) 2011, 2015 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
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/delay.h>
29#include <linux/i2c.h>
30#include <linux/errno.h>
31#include <linux/sched.h>
32#include <linux/err.h>
33#include <linux/interrupt.h>
34#include <linux/io.h>
35#include <linux/slab.h>
36#include <linux/pci.h>
Dirk Brandewie18dbdda2011-10-06 11:26:36 -070037#include <linux/pm_runtime.h>
Dustin Byford8eb5c872015-10-23 12:27:07 -070038#include <linux/acpi.h>
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070039#include "i2c-designware-core.h"
40
41#define DRIVER_NAME "i2c-designware-pci"
42
43enum dw_pci_ctl_id_t {
Andy Shevchenkoed1bf032016-06-15 18:05:05 +030044 medfield,
Andy Shevchenkob20551c2016-06-15 18:05:06 +030045 merrifield,
Mika Westerberg089c7292014-02-19 16:10:29 +020046 baytrail,
Mika Westerberg157a8012014-05-15 17:37:24 +030047 haswell,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070048};
49
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080050struct dw_scl_sda_cfg {
51 u32 ss_hcnt;
52 u32 fs_hcnt;
53 u32 ss_lcnt;
54 u32 fs_lcnt;
55 u32 sda_hold;
56};
57
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070058struct dw_pci_controller {
59 u32 bus_num;
60 u32 bus_cfg;
61 u32 tx_fifo_depth;
62 u32 rx_fifo_depth;
63 u32 clk_khz;
Chew, Chiau Eececcd292014-03-07 22:12:50 +080064 u32 functionality;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080065 struct dw_scl_sda_cfg *scl_sda_cfg;
Andy Shevchenkoed1bf032016-06-15 18:05:05 +030066 int (*setup)(struct pci_dev *pdev, struct dw_pci_controller *c);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -070067};
68
69#define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
70 DW_IC_CON_SLAVE_DISABLE | \
71 DW_IC_CON_RESTART_EN)
72
Chew, Chiau Eececcd292014-03-07 22:12:50 +080073#define DW_DEFAULT_FUNCTIONALITY (I2C_FUNC_I2C | \
74 I2C_FUNC_SMBUS_BYTE | \
75 I2C_FUNC_SMBUS_BYTE_DATA | \
76 I2C_FUNC_SMBUS_WORD_DATA | \
77 I2C_FUNC_SMBUS_I2C_BLOCK)
78
Andy Shevchenkob20551c2016-06-15 18:05:06 +030079/* Merrifield HCNT/LCNT/SDA hold time */
80static struct dw_scl_sda_cfg mrfld_config = {
81 .ss_hcnt = 0x2f8,
82 .fs_hcnt = 0x87,
83 .ss_lcnt = 0x37b,
84 .fs_lcnt = 0x10a,
85};
86
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +080087/* BayTrail HCNT/LCNT/SDA hold time */
88static struct dw_scl_sda_cfg byt_config = {
89 .ss_hcnt = 0x200,
90 .fs_hcnt = 0x55,
91 .ss_lcnt = 0x200,
92 .fs_lcnt = 0x99,
93 .sda_hold = 0x6,
94};
95
Mika Westerberg157a8012014-05-15 17:37:24 +030096/* Haswell HCNT/LCNT/SDA hold time */
97static struct dw_scl_sda_cfg hsw_config = {
98 .ss_hcnt = 0x01b0,
99 .fs_hcnt = 0x48,
100 .ss_lcnt = 0x01fb,
101 .fs_lcnt = 0xa0,
102 .sda_hold = 0x9,
103};
104
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300105static int mfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
106{
107 switch (pdev->device) {
108 case 0x0817:
109 c->bus_cfg &= ~DW_IC_CON_SPEED_MASK;
110 c->bus_cfg |= DW_IC_CON_SPEED_STD;
111 case 0x0818:
112 case 0x0819:
113 c->bus_num = pdev->device - 0x817 + 3;
114 return 0;
115 case 0x082C:
116 case 0x082D:
117 case 0x082E:
118 c->bus_num = pdev->device - 0x82C + 0;
119 return 0;
120 }
121 return -ENODEV;
122}
123
Andy Shevchenkob20551c2016-06-15 18:05:06 +0300124static int mrfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
125{
126 /*
127 * On Intel Merrifield the i2c busses are enumerated [1..7]. So, we add
128 * 1 to shift the default range. Besides that the first PCI slot
129 * provides 4 functions, that's why we have to add 0 to the head slot
130 * and 4 to the tail one.
131 */
132 switch (PCI_SLOT(pdev->devfn)) {
133 case 8:
134 c->bus_num = PCI_FUNC(pdev->devfn) + 0 + 1;
135 return 0;
136 case 9:
137 c->bus_num = PCI_FUNC(pdev->devfn) + 4 + 1;
138 return 0;
139 }
140 return -ENODEV;
141}
142
Andy Shevchenkoa93ac572015-02-06 13:47:02 +0200143static struct dw_pci_controller dw_pci_controllers[] = {
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300144 [medfield] = {
145 .bus_num = -1,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700146 .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,
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300150 .setup = mfld_setup,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700151 },
Andy Shevchenkob20551c2016-06-15 18:05:06 +0300152 [merrifield] = {
153 .bus_num = -1,
154 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
155 .tx_fifo_depth = 64,
156 .rx_fifo_depth = 64,
157 .scl_sda_cfg = &mrfld_config,
158 .setup = mrfld_setup,
159 },
Mika Westerberg089c7292014-02-19 16:10:29 +0200160 [baytrail] = {
161 .bus_num = -1,
162 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
163 .tx_fifo_depth = 32,
164 .rx_fifo_depth = 32,
Chew, Chiau Eececcd292014-03-07 22:12:50 +0800165 .functionality = I2C_FUNC_10BIT_ADDR,
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800166 .scl_sda_cfg = &byt_config,
Mika Westerberg089c7292014-02-19 16:10:29 +0200167 },
Mika Westerberg157a8012014-05-15 17:37:24 +0300168 [haswell] = {
169 .bus_num = -1,
170 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
171 .tx_fifo_depth = 32,
172 .rx_fifo_depth = 32,
Mika Westerberg157a8012014-05-15 17:37:24 +0300173 .functionality = I2C_FUNC_10BIT_ADDR,
174 .scl_sda_cfg = &hsw_config,
175 },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700176};
Alan Cox04095162014-07-23 13:06:57 +0100177
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200178#ifdef CONFIG_PM
Octavian Purdila52c28432011-10-06 11:26:37 -0700179static int i2c_dw_pci_suspend(struct device *dev)
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700180{
Geliang Tang238c44a2015-12-27 18:45:59 +0800181 struct pci_dev *pdev = to_pci_dev(dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700182
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200183 i2c_dw_disable(pci_get_drvdata(pdev));
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700184 return 0;
185}
186
Octavian Purdila52c28432011-10-06 11:26:37 -0700187static int i2c_dw_pci_resume(struct device *dev)
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700188{
Geliang Tang238c44a2015-12-27 18:45:59 +0800189 struct pci_dev *pdev = to_pci_dev(dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700190
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200191 return i2c_dw_init(pci_get_drvdata(pdev));
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700192}
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200193#endif
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700194
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200195static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
196 i2c_dw_pci_resume, NULL);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700197
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700198static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
199{
200 return dev->controller->clk_khz;
201}
202
Bill Pemberton0b255e92012-11-27 15:59:38 -0500203static int i2c_dw_pci_probe(struct pci_dev *pdev,
Andy Shevchenkoca0c1ff2013-04-10 00:36:37 +0000204 const struct pci_device_id *id)
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700205{
206 struct dw_i2c_dev *dev;
207 struct i2c_adapter *adap;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700208 int r;
209 struct dw_pci_controller *controller;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800210 struct dw_scl_sda_cfg *cfg;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700211
212 if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
Andy Shevchenkoca0c1ff2013-04-10 00:36:37 +0000213 dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700214 id->driver_data);
215 return -EINVAL;
216 }
217
218 controller = &dw_pci_controllers[id->driver_data];
219
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000220 r = pcim_enable_device(pdev);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700221 if (r) {
222 dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
223 r);
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000224 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700225 }
226
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000227 r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700228 if (r) {
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700229 dev_err(&pdev->dev, "I/O memory remapping failed\n");
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000230 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700231 }
232
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000233 dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
234 if (!dev)
235 return -ENOMEM;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700236
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700237 dev->clk = NULL;
238 dev->controller = controller;
239 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000240 dev->base = pcim_iomap_table(pdev)[0];
241 dev->dev = &pdev->dev;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300242 dev->irq = pdev->irq;
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300243
244 if (controller->setup) {
245 r = controller->setup(pdev, controller);
246 if (r)
247 return r;
248 }
249
Chew, Chiau Eececcd292014-03-07 22:12:50 +0800250 dev->functionality = controller->functionality |
251 DW_DEFAULT_FUNCTIONALITY;
252
Andy Shevchenkoa93ac572015-02-06 13:47:02 +0200253 dev->master_cfg = controller->bus_cfg;
Chew, Chiau Ee8efd1e92014-03-11 19:33:45 +0800254 if (controller->scl_sda_cfg) {
255 cfg = controller->scl_sda_cfg;
256 dev->ss_hcnt = cfg->ss_hcnt;
257 dev->fs_hcnt = cfg->fs_hcnt;
258 dev->ss_lcnt = cfg->ss_lcnt;
259 dev->fs_lcnt = cfg->fs_lcnt;
260 dev->sda_hold_time = cfg->sda_hold;
261 }
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700262
263 pci_set_drvdata(pdev, dev);
264
265 dev->tx_fifo_depth = controller->tx_fifo_depth;
266 dev->rx_fifo_depth = controller->rx_fifo_depth;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700267
268 adap = &dev->adapter;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700269 adap->owner = THIS_MODULE;
270 adap->class = 0;
Dustin Byford8eb5c872015-10-23 12:27:07 -0700271 ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700272 adap->nr = controller->bus_num;
Mika Westerberg089c7292014-02-19 16:10:29 +0200273
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300274 r = i2c_dw_probe(dev);
275 if (r)
Andy Shevchenko76cf3fc2013-04-10 00:36:38 +0000276 return r;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700277
Mika Westerberg43452332013-04-10 00:36:42 +0000278 pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
279 pm_runtime_use_autosuspend(&pdev->dev);
Mika Westerbergbe58eda2014-02-04 14:37:07 +0200280 pm_runtime_put_autosuspend(&pdev->dev);
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700281 pm_runtime_allow(&pdev->dev);
282
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700283 return 0;
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700284}
285
Bill Pemberton0b255e92012-11-27 15:59:38 -0500286static void i2c_dw_pci_remove(struct pci_dev *pdev)
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700287{
288 struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
289
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700290 i2c_dw_disable(dev);
291 pm_runtime_forbid(&pdev->dev);
292 pm_runtime_get_noresume(&pdev->dev);
293
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700294 i2c_del_adapter(&dev->adapter);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700295}
296
297/* work with hotplug and coldplug */
298MODULE_ALIAS("i2c_designware-pci");
299
Jingoo Han392debf2013-12-03 08:11:20 +0900300static const struct pci_device_id i2_designware_pci_ids[] = {
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700301 /* Medfield */
Andy Shevchenkoed1bf032016-06-15 18:05:05 +0300302 { PCI_VDEVICE(INTEL, 0x0817), medfield },
303 { PCI_VDEVICE(INTEL, 0x0818), medfield },
304 { PCI_VDEVICE(INTEL, 0x0819), medfield },
305 { PCI_VDEVICE(INTEL, 0x082C), medfield },
306 { PCI_VDEVICE(INTEL, 0x082D), medfield },
307 { PCI_VDEVICE(INTEL, 0x082E), medfield },
Andy Shevchenkob20551c2016-06-15 18:05:06 +0300308 /* Merrifield */
309 { PCI_VDEVICE(INTEL, 0x1195), merrifield },
310 { PCI_VDEVICE(INTEL, 0x1196), merrifield },
Mika Westerberg089c7292014-02-19 16:10:29 +0200311 /* Baytrail */
312 { PCI_VDEVICE(INTEL, 0x0F41), baytrail },
313 { PCI_VDEVICE(INTEL, 0x0F42), baytrail },
314 { PCI_VDEVICE(INTEL, 0x0F43), baytrail },
315 { PCI_VDEVICE(INTEL, 0x0F44), baytrail },
316 { PCI_VDEVICE(INTEL, 0x0F45), baytrail },
317 { PCI_VDEVICE(INTEL, 0x0F46), baytrail },
318 { PCI_VDEVICE(INTEL, 0x0F47), baytrail },
Mika Westerberg157a8012014-05-15 17:37:24 +0300319 /* Haswell */
320 { PCI_VDEVICE(INTEL, 0x9c61), haswell },
321 { PCI_VDEVICE(INTEL, 0x9c62), haswell },
Alan Cox04095162014-07-23 13:06:57 +0100322 /* Braswell / Cherrytrail */
Andy Shevchenkoa93ac572015-02-06 13:47:02 +0200323 { PCI_VDEVICE(INTEL, 0x22C1), baytrail },
Alan Cox04095162014-07-23 13:06:57 +0100324 { PCI_VDEVICE(INTEL, 0x22C2), baytrail },
325 { PCI_VDEVICE(INTEL, 0x22C3), baytrail },
326 { PCI_VDEVICE(INTEL, 0x22C4), baytrail },
327 { PCI_VDEVICE(INTEL, 0x22C5), baytrail },
328 { PCI_VDEVICE(INTEL, 0x22C6), baytrail },
329 { PCI_VDEVICE(INTEL, 0x22C7), baytrail },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700330 { 0,}
331};
332MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
333
334static struct pci_driver dw_i2c_driver = {
335 .name = DRIVER_NAME,
336 .id_table = i2_designware_pci_ids,
337 .probe = i2c_dw_pci_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500338 .remove = i2c_dw_pci_remove,
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700339 .driver = {
340 .pm = &i2c_dw_pm_ops,
341 },
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700342};
343
Axel Lin56f21782012-07-24 14:13:56 +0200344module_pci_driver(dw_i2c_driver);
Dirk Brandewiefe20ff52011-10-06 11:26:35 -0700345
346MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
347MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
348MODULE_LICENSE("GPL");