i2c-designware-pcidrv.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/delay.h>
  32. #include <linux/i2c.h>
  33. #include <linux/errno.h>
  34. #include <linux/sched.h>
  35. #include <linux/err.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/io.h>
  38. #include <linux/slab.h>
  39. #include <linux/pci.h>
  40. #include <linux/pm_runtime.h>
  41. #include "i2c-designware-core.h"
  42. #define DRIVER_NAME "i2c-designware-pci"
  43. enum dw_pci_ctl_id_t {
  44. moorestown_0,
  45. moorestown_1,
  46. moorestown_2,
  47. medfield_0,
  48. medfield_1,
  49. medfield_2,
  50. medfield_3,
  51. medfield_4,
  52. medfield_5,
  53. baytrail,
  54. haswell,
  55. };
  56. struct dw_scl_sda_cfg {
  57. u32 ss_hcnt;
  58. u32 fs_hcnt;
  59. u32 ss_lcnt;
  60. u32 fs_lcnt;
  61. u32 sda_hold;
  62. };
  63. struct dw_pci_controller {
  64. u32 bus_num;
  65. u32 bus_cfg;
  66. u32 tx_fifo_depth;
  67. u32 rx_fifo_depth;
  68. u32 clk_khz;
  69. u32 functionality;
  70. struct dw_scl_sda_cfg *scl_sda_cfg;
  71. };
  72. #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
  73. DW_IC_CON_SLAVE_DISABLE | \
  74. DW_IC_CON_RESTART_EN)
  75. #define DW_DEFAULT_FUNCTIONALITY (I2C_FUNC_I2C | \
  76. I2C_FUNC_SMBUS_BYTE | \
  77. I2C_FUNC_SMBUS_BYTE_DATA | \
  78. I2C_FUNC_SMBUS_WORD_DATA | \
  79. I2C_FUNC_SMBUS_I2C_BLOCK)
  80. /* BayTrail HCNT/LCNT/SDA hold time */
  81. static struct dw_scl_sda_cfg byt_config = {
  82. .ss_hcnt = 0x200,
  83. .fs_hcnt = 0x55,
  84. .ss_lcnt = 0x200,
  85. .fs_lcnt = 0x99,
  86. .sda_hold = 0x6,
  87. };
  88. /* Haswell HCNT/LCNT/SDA hold time */
  89. static struct dw_scl_sda_cfg hsw_config = {
  90. .ss_hcnt = 0x01b0,
  91. .fs_hcnt = 0x48,
  92. .ss_lcnt = 0x01fb,
  93. .fs_lcnt = 0xa0,
  94. .sda_hold = 0x9,
  95. };
  96. static struct dw_pci_controller dw_pci_controllers[] = {
  97. [moorestown_0] = {
  98. .bus_num = 0,
  99. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  100. .tx_fifo_depth = 32,
  101. .rx_fifo_depth = 32,
  102. .clk_khz = 25000,
  103. },
  104. [moorestown_1] = {
  105. .bus_num = 1,
  106. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  107. .tx_fifo_depth = 32,
  108. .rx_fifo_depth = 32,
  109. .clk_khz = 25000,
  110. },
  111. [moorestown_2] = {
  112. .bus_num = 2,
  113. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  114. .tx_fifo_depth = 32,
  115. .rx_fifo_depth = 32,
  116. .clk_khz = 25000,
  117. },
  118. [medfield_0] = {
  119. .bus_num = 0,
  120. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  121. .tx_fifo_depth = 32,
  122. .rx_fifo_depth = 32,
  123. .clk_khz = 25000,
  124. },
  125. [medfield_1] = {
  126. .bus_num = 1,
  127. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  128. .tx_fifo_depth = 32,
  129. .rx_fifo_depth = 32,
  130. .clk_khz = 25000,
  131. },
  132. [medfield_2] = {
  133. .bus_num = 2,
  134. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  135. .tx_fifo_depth = 32,
  136. .rx_fifo_depth = 32,
  137. .clk_khz = 25000,
  138. },
  139. [medfield_3] = {
  140. .bus_num = 3,
  141. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_STD,
  142. .tx_fifo_depth = 32,
  143. .rx_fifo_depth = 32,
  144. .clk_khz = 25000,
  145. },
  146. [medfield_4] = {
  147. .bus_num = 4,
  148. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  149. .tx_fifo_depth = 32,
  150. .rx_fifo_depth = 32,
  151. .clk_khz = 25000,
  152. },
  153. [medfield_5] = {
  154. .bus_num = 5,
  155. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  156. .tx_fifo_depth = 32,
  157. .rx_fifo_depth = 32,
  158. .clk_khz = 25000,
  159. },
  160. [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,
  165. .clk_khz = 100000,
  166. .functionality = I2C_FUNC_10BIT_ADDR,
  167. .scl_sda_cfg = &byt_config,
  168. },
  169. [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,
  174. .clk_khz = 100000,
  175. .functionality = I2C_FUNC_10BIT_ADDR,
  176. .scl_sda_cfg = &hsw_config,
  177. },
  178. };
  179. static struct i2c_algorithm i2c_dw_algo = {
  180. .master_xfer = i2c_dw_xfer,
  181. .functionality = i2c_dw_func,
  182. };
  183. #ifdef CONFIG_PM
  184. static int i2c_dw_pci_suspend(struct device *dev)
  185. {
  186. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  187. i2c_dw_disable(pci_get_drvdata(pdev));
  188. return 0;
  189. }
  190. static int i2c_dw_pci_resume(struct device *dev)
  191. {
  192. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  193. return i2c_dw_init(pci_get_drvdata(pdev));
  194. }
  195. #endif
  196. static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
  197. i2c_dw_pci_resume, NULL);
  198. static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
  199. {
  200. return dev->controller->clk_khz;
  201. }
  202. static int i2c_dw_pci_probe(struct pci_dev *pdev,
  203. const struct pci_device_id *id)
  204. {
  205. struct dw_i2c_dev *dev;
  206. struct i2c_adapter *adap;
  207. int r;
  208. struct dw_pci_controller *controller;
  209. struct dw_scl_sda_cfg *cfg;
  210. if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
  211. dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
  212. id->driver_data);
  213. return -EINVAL;
  214. }
  215. controller = &dw_pci_controllers[id->driver_data];
  216. r = pcim_enable_device(pdev);
  217. if (r) {
  218. dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
  219. r);
  220. return r;
  221. }
  222. r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
  223. if (r) {
  224. dev_err(&pdev->dev, "I/O memory remapping failed\n");
  225. return r;
  226. }
  227. dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
  228. if (!dev)
  229. return -ENOMEM;
  230. init_completion(&dev->cmd_complete);
  231. mutex_init(&dev->lock);
  232. dev->clk = NULL;
  233. dev->controller = controller;
  234. dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
  235. dev->base = pcim_iomap_table(pdev)[0];
  236. dev->dev = &pdev->dev;
  237. dev->functionality = controller->functionality |
  238. DW_DEFAULT_FUNCTIONALITY;
  239. dev->master_cfg = controller->bus_cfg;
  240. if (controller->scl_sda_cfg) {
  241. cfg = controller->scl_sda_cfg;
  242. dev->ss_hcnt = cfg->ss_hcnt;
  243. dev->fs_hcnt = cfg->fs_hcnt;
  244. dev->ss_lcnt = cfg->ss_lcnt;
  245. dev->fs_lcnt = cfg->fs_lcnt;
  246. dev->sda_hold_time = cfg->sda_hold;
  247. }
  248. pci_set_drvdata(pdev, dev);
  249. dev->tx_fifo_depth = controller->tx_fifo_depth;
  250. dev->rx_fifo_depth = controller->rx_fifo_depth;
  251. r = i2c_dw_init(dev);
  252. if (r)
  253. return r;
  254. adap = &dev->adapter;
  255. i2c_set_adapdata(adap, dev);
  256. adap->owner = THIS_MODULE;
  257. adap->class = 0;
  258. adap->algo = &i2c_dw_algo;
  259. adap->dev.parent = &pdev->dev;
  260. adap->nr = controller->bus_num;
  261. snprintf(adap->name, sizeof(adap->name), "i2c-designware-pci");
  262. r = devm_request_irq(&pdev->dev, pdev->irq, i2c_dw_isr, IRQF_SHARED,
  263. adap->name, dev);
  264. if (r) {
  265. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  266. return r;
  267. }
  268. i2c_dw_disable_int(dev);
  269. i2c_dw_clear_int(dev);
  270. r = i2c_add_numbered_adapter(adap);
  271. if (r) {
  272. dev_err(&pdev->dev, "failure adding adapter\n");
  273. return r;
  274. }
  275. pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
  276. pm_runtime_use_autosuspend(&pdev->dev);
  277. pm_runtime_put_autosuspend(&pdev->dev);
  278. pm_runtime_allow(&pdev->dev);
  279. return 0;
  280. }
  281. static void i2c_dw_pci_remove(struct pci_dev *pdev)
  282. {
  283. struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
  284. i2c_dw_disable(dev);
  285. pm_runtime_forbid(&pdev->dev);
  286. pm_runtime_get_noresume(&pdev->dev);
  287. i2c_del_adapter(&dev->adapter);
  288. }
  289. /* work with hotplug and coldplug */
  290. MODULE_ALIAS("i2c_designware-pci");
  291. static const struct pci_device_id i2_designware_pci_ids[] = {
  292. /* Moorestown */
  293. { PCI_VDEVICE(INTEL, 0x0802), moorestown_0 },
  294. { PCI_VDEVICE(INTEL, 0x0803), moorestown_1 },
  295. { PCI_VDEVICE(INTEL, 0x0804), moorestown_2 },
  296. /* Medfield */
  297. { PCI_VDEVICE(INTEL, 0x0817), medfield_3,},
  298. { PCI_VDEVICE(INTEL, 0x0818), medfield_4 },
  299. { PCI_VDEVICE(INTEL, 0x0819), medfield_5 },
  300. { PCI_VDEVICE(INTEL, 0x082C), medfield_0 },
  301. { PCI_VDEVICE(INTEL, 0x082D), medfield_1 },
  302. { PCI_VDEVICE(INTEL, 0x082E), medfield_2 },
  303. /* Baytrail */
  304. { PCI_VDEVICE(INTEL, 0x0F41), baytrail },
  305. { PCI_VDEVICE(INTEL, 0x0F42), baytrail },
  306. { PCI_VDEVICE(INTEL, 0x0F43), baytrail },
  307. { PCI_VDEVICE(INTEL, 0x0F44), baytrail },
  308. { PCI_VDEVICE(INTEL, 0x0F45), baytrail },
  309. { PCI_VDEVICE(INTEL, 0x0F46), baytrail },
  310. { PCI_VDEVICE(INTEL, 0x0F47), baytrail },
  311. /* Haswell */
  312. { PCI_VDEVICE(INTEL, 0x9c61), haswell },
  313. { PCI_VDEVICE(INTEL, 0x9c62), haswell },
  314. /* Braswell / Cherrytrail */
  315. { PCI_VDEVICE(INTEL, 0x22C1), baytrail,},
  316. { PCI_VDEVICE(INTEL, 0x22C2), baytrail },
  317. { PCI_VDEVICE(INTEL, 0x22C3), baytrail },
  318. { PCI_VDEVICE(INTEL, 0x22C4), baytrail },
  319. { PCI_VDEVICE(INTEL, 0x22C5), baytrail },
  320. { PCI_VDEVICE(INTEL, 0x22C6), baytrail },
  321. { PCI_VDEVICE(INTEL, 0x22C7), baytrail },
  322. { 0,}
  323. };
  324. MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
  325. static struct pci_driver dw_i2c_driver = {
  326. .name = DRIVER_NAME,
  327. .id_table = i2_designware_pci_ids,
  328. .probe = i2c_dw_pci_probe,
  329. .remove = i2c_dw_pci_remove,
  330. .driver = {
  331. .pm = &i2c_dw_pm_ops,
  332. },
  333. };
  334. module_pci_driver(dw_i2c_driver);
  335. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  336. MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
  337. MODULE_LICENSE("GPL");