i2c-designware-pcidrv.c 8.9 KB

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