i2c-designware-pcidrv.c 8.6 KB

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