Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 1 | /* |
| 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 Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 41 | #include <linux/pm_runtime.h> |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 42 | #include "i2c-designware-core.h" |
| 43 | |
| 44 | #define DRIVER_NAME "i2c-designware-pci" |
| 45 | |
| 46 | enum 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 Westerberg | 089c729 | 2014-02-19 16:10:29 +0200 | [diff] [blame^] | 57 | |
| 58 | baytrail, |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | struct dw_pci_controller { |
| 62 | u32 bus_num; |
| 63 | u32 bus_cfg; |
| 64 | u32 tx_fifo_depth; |
| 65 | u32 rx_fifo_depth; |
| 66 | u32 clk_khz; |
| 67 | }; |
| 68 | |
| 69 | #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \ |
| 70 | DW_IC_CON_SLAVE_DISABLE | \ |
| 71 | DW_IC_CON_RESTART_EN) |
| 72 | |
| 73 | static struct dw_pci_controller dw_pci_controllers[] = { |
| 74 | [moorestown_0] = { |
| 75 | .bus_num = 0, |
| 76 | .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST, |
| 77 | .tx_fifo_depth = 32, |
| 78 | .rx_fifo_depth = 32, |
| 79 | .clk_khz = 25000, |
| 80 | }, |
| 81 | [moorestown_1] = { |
| 82 | .bus_num = 1, |
| 83 | .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST, |
| 84 | .tx_fifo_depth = 32, |
| 85 | .rx_fifo_depth = 32, |
| 86 | .clk_khz = 25000, |
| 87 | }, |
| 88 | [moorestown_2] = { |
| 89 | .bus_num = 2, |
| 90 | .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST, |
| 91 | .tx_fifo_depth = 32, |
| 92 | .rx_fifo_depth = 32, |
| 93 | .clk_khz = 25000, |
| 94 | }, |
| 95 | [medfield_0] = { |
| 96 | .bus_num = 0, |
| 97 | .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST, |
| 98 | .tx_fifo_depth = 32, |
| 99 | .rx_fifo_depth = 32, |
| 100 | .clk_khz = 25000, |
| 101 | }, |
| 102 | [medfield_1] = { |
| 103 | .bus_num = 1, |
| 104 | .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST, |
| 105 | .tx_fifo_depth = 32, |
| 106 | .rx_fifo_depth = 32, |
| 107 | .clk_khz = 25000, |
| 108 | }, |
| 109 | [medfield_2] = { |
| 110 | .bus_num = 2, |
| 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 | [medfield_3] = { |
| 117 | .bus_num = 3, |
| 118 | .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_STD, |
| 119 | .tx_fifo_depth = 32, |
| 120 | .rx_fifo_depth = 32, |
| 121 | .clk_khz = 25000, |
| 122 | }, |
| 123 | [medfield_4] = { |
| 124 | .bus_num = 4, |
| 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_5] = { |
| 131 | .bus_num = 5, |
| 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 | }, |
Mika Westerberg | 089c729 | 2014-02-19 16:10:29 +0200 | [diff] [blame^] | 137 | [baytrail] = { |
| 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 = 100000, |
| 143 | }, |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 144 | }; |
| 145 | static struct i2c_algorithm i2c_dw_algo = { |
| 146 | .master_xfer = i2c_dw_xfer, |
| 147 | .functionality = i2c_dw_func, |
| 148 | }; |
| 149 | |
Mika Westerberg | be58eda | 2014-02-04 14:37:07 +0200 | [diff] [blame] | 150 | #ifdef CONFIG_PM |
Octavian Purdila | 52c2843 | 2011-10-06 11:26:37 -0700 | [diff] [blame] | 151 | static int i2c_dw_pci_suspend(struct device *dev) |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 152 | { |
Octavian Purdila | 52c2843 | 2011-10-06 11:26:37 -0700 | [diff] [blame] | 153 | struct pci_dev *pdev = container_of(dev, struct pci_dev, dev); |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 154 | |
Mika Westerberg | be58eda | 2014-02-04 14:37:07 +0200 | [diff] [blame] | 155 | i2c_dw_disable(pci_get_drvdata(pdev)); |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Octavian Purdila | 52c2843 | 2011-10-06 11:26:37 -0700 | [diff] [blame] | 159 | static int i2c_dw_pci_resume(struct device *dev) |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 160 | { |
Octavian Purdila | 52c2843 | 2011-10-06 11:26:37 -0700 | [diff] [blame] | 161 | struct pci_dev *pdev = container_of(dev, struct pci_dev, dev); |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 162 | |
Mika Westerberg | be58eda | 2014-02-04 14:37:07 +0200 | [diff] [blame] | 163 | return i2c_dw_init(pci_get_drvdata(pdev)); |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 164 | } |
Mika Westerberg | be58eda | 2014-02-04 14:37:07 +0200 | [diff] [blame] | 165 | #endif |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 166 | |
Mika Westerberg | be58eda | 2014-02-04 14:37:07 +0200 | [diff] [blame] | 167 | static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend, |
| 168 | i2c_dw_pci_resume, NULL); |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 169 | |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 170 | static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev) |
| 171 | { |
| 172 | return dev->controller->clk_khz; |
| 173 | } |
| 174 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 175 | static int i2c_dw_pci_probe(struct pci_dev *pdev, |
Andy Shevchenko | ca0c1ff | 2013-04-10 00:36:37 +0000 | [diff] [blame] | 176 | const struct pci_device_id *id) |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 177 | { |
| 178 | struct dw_i2c_dev *dev; |
| 179 | struct i2c_adapter *adap; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 180 | int r; |
| 181 | struct dw_pci_controller *controller; |
| 182 | |
| 183 | if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) { |
Andy Shevchenko | ca0c1ff | 2013-04-10 00:36:37 +0000 | [diff] [blame] | 184 | dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__, |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 185 | id->driver_data); |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | |
| 189 | controller = &dw_pci_controllers[id->driver_data]; |
| 190 | |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 191 | r = pcim_enable_device(pdev); |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 192 | if (r) { |
| 193 | dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n", |
| 194 | r); |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 195 | return r; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 198 | r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev)); |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 199 | if (r) { |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 200 | dev_err(&pdev->dev, "I/O memory remapping failed\n"); |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 201 | return r; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 204 | dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL); |
| 205 | if (!dev) |
| 206 | return -ENOMEM; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 207 | |
| 208 | init_completion(&dev->cmd_complete); |
| 209 | mutex_init(&dev->lock); |
| 210 | dev->clk = NULL; |
| 211 | dev->controller = controller; |
| 212 | dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz; |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 213 | dev->base = pcim_iomap_table(pdev)[0]; |
| 214 | dev->dev = &pdev->dev; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 215 | dev->functionality = |
| 216 | I2C_FUNC_I2C | |
| 217 | I2C_FUNC_SMBUS_BYTE | |
| 218 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 219 | I2C_FUNC_SMBUS_WORD_DATA | |
| 220 | I2C_FUNC_SMBUS_I2C_BLOCK; |
| 221 | dev->master_cfg = controller->bus_cfg; |
| 222 | |
| 223 | pci_set_drvdata(pdev, dev); |
| 224 | |
| 225 | dev->tx_fifo_depth = controller->tx_fifo_depth; |
| 226 | dev->rx_fifo_depth = controller->rx_fifo_depth; |
| 227 | r = i2c_dw_init(dev); |
| 228 | if (r) |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 229 | return r; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 230 | |
| 231 | adap = &dev->adapter; |
| 232 | i2c_set_adapdata(adap, dev); |
| 233 | adap->owner = THIS_MODULE; |
| 234 | adap->class = 0; |
| 235 | adap->algo = &i2c_dw_algo; |
| 236 | adap->dev.parent = &pdev->dev; |
| 237 | adap->nr = controller->bus_num; |
Mika Westerberg | 089c729 | 2014-02-19 16:10:29 +0200 | [diff] [blame^] | 238 | |
| 239 | snprintf(adap->name, sizeof(adap->name), "i2c-designware-pci"); |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 240 | |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 241 | r = devm_request_irq(&pdev->dev, pdev->irq, i2c_dw_isr, IRQF_SHARED, |
| 242 | adap->name, dev); |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 243 | if (r) { |
| 244 | dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 245 | return r; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | i2c_dw_disable_int(dev); |
| 249 | i2c_dw_clear_int(dev); |
| 250 | r = i2c_add_numbered_adapter(adap); |
| 251 | if (r) { |
| 252 | dev_err(&pdev->dev, "failure adding adapter\n"); |
Andy Shevchenko | 76cf3fc | 2013-04-10 00:36:38 +0000 | [diff] [blame] | 253 | return r; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Mika Westerberg | 4345233 | 2013-04-10 00:36:42 +0000 | [diff] [blame] | 256 | pm_runtime_set_autosuspend_delay(&pdev->dev, 1000); |
| 257 | pm_runtime_use_autosuspend(&pdev->dev); |
Mika Westerberg | be58eda | 2014-02-04 14:37:07 +0200 | [diff] [blame] | 258 | pm_runtime_put_autosuspend(&pdev->dev); |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 259 | pm_runtime_allow(&pdev->dev); |
| 260 | |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 261 | return 0; |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 264 | static void i2c_dw_pci_remove(struct pci_dev *pdev) |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 265 | { |
| 266 | struct dw_i2c_dev *dev = pci_get_drvdata(pdev); |
| 267 | |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 268 | i2c_dw_disable(dev); |
| 269 | pm_runtime_forbid(&pdev->dev); |
| 270 | pm_runtime_get_noresume(&pdev->dev); |
| 271 | |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 272 | i2c_del_adapter(&dev->adapter); |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* work with hotplug and coldplug */ |
| 276 | MODULE_ALIAS("i2c_designware-pci"); |
| 277 | |
Axel Lin | 3527bd5 | 2012-01-12 20:32:04 +0100 | [diff] [blame] | 278 | static DEFINE_PCI_DEVICE_TABLE(i2_designware_pci_ids) = { |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 279 | /* Moorestown */ |
| 280 | { PCI_VDEVICE(INTEL, 0x0802), moorestown_0 }, |
| 281 | { PCI_VDEVICE(INTEL, 0x0803), moorestown_1 }, |
| 282 | { PCI_VDEVICE(INTEL, 0x0804), moorestown_2 }, |
| 283 | /* Medfield */ |
| 284 | { PCI_VDEVICE(INTEL, 0x0817), medfield_3,}, |
| 285 | { PCI_VDEVICE(INTEL, 0x0818), medfield_4 }, |
| 286 | { PCI_VDEVICE(INTEL, 0x0819), medfield_5 }, |
| 287 | { PCI_VDEVICE(INTEL, 0x082C), medfield_0 }, |
| 288 | { PCI_VDEVICE(INTEL, 0x082D), medfield_1 }, |
| 289 | { PCI_VDEVICE(INTEL, 0x082E), medfield_2 }, |
Mika Westerberg | 089c729 | 2014-02-19 16:10:29 +0200 | [diff] [blame^] | 290 | /* Baytrail */ |
| 291 | { PCI_VDEVICE(INTEL, 0x0F41), baytrail }, |
| 292 | { PCI_VDEVICE(INTEL, 0x0F42), baytrail }, |
| 293 | { PCI_VDEVICE(INTEL, 0x0F43), baytrail }, |
| 294 | { PCI_VDEVICE(INTEL, 0x0F44), baytrail }, |
| 295 | { PCI_VDEVICE(INTEL, 0x0F45), baytrail }, |
| 296 | { PCI_VDEVICE(INTEL, 0x0F46), baytrail }, |
| 297 | { PCI_VDEVICE(INTEL, 0x0F47), baytrail }, |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 298 | { 0,} |
| 299 | }; |
| 300 | MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids); |
| 301 | |
| 302 | static struct pci_driver dw_i2c_driver = { |
| 303 | .name = DRIVER_NAME, |
| 304 | .id_table = i2_designware_pci_ids, |
| 305 | .probe = i2c_dw_pci_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 306 | .remove = i2c_dw_pci_remove, |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 307 | .driver = { |
| 308 | .pm = &i2c_dw_pm_ops, |
| 309 | }, |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
Axel Lin | 56f2178 | 2012-07-24 14:13:56 +0200 | [diff] [blame] | 312 | module_pci_driver(dw_i2c_driver); |
Dirk Brandewie | fe20ff5 | 2011-10-06 11:26:35 -0700 | [diff] [blame] | 313 | |
| 314 | MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>"); |
| 315 | MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter"); |
| 316 | MODULE_LICENSE("GPL"); |