i2c-designware-pcidrv.c 8.5 KB

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