i2c-designware-pcidrv.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. i2c_dw_disable(pci_get_drvdata(pdev));
  177. return 0;
  178. }
  179. static int i2c_dw_pci_resume(struct device *dev)
  180. {
  181. struct pci_dev *pdev = to_pci_dev(dev);
  182. return i2c_dw_init(pci_get_drvdata(pdev));
  183. }
  184. #endif
  185. static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
  186. i2c_dw_pci_resume, NULL);
  187. static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
  188. {
  189. return dev->controller->clk_khz;
  190. }
  191. static int i2c_dw_pci_probe(struct pci_dev *pdev,
  192. const struct pci_device_id *id)
  193. {
  194. struct dw_i2c_dev *dev;
  195. struct i2c_adapter *adap;
  196. int r;
  197. struct dw_pci_controller *controller;
  198. struct dw_scl_sda_cfg *cfg;
  199. if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
  200. dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
  201. id->driver_data);
  202. return -EINVAL;
  203. }
  204. controller = &dw_pci_controllers[id->driver_data];
  205. r = pcim_enable_device(pdev);
  206. if (r) {
  207. dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
  208. r);
  209. return r;
  210. }
  211. r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
  212. if (r) {
  213. dev_err(&pdev->dev, "I/O memory remapping failed\n");
  214. return r;
  215. }
  216. dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
  217. if (!dev)
  218. return -ENOMEM;
  219. dev->clk = NULL;
  220. dev->controller = controller;
  221. dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
  222. dev->base = pcim_iomap_table(pdev)[0];
  223. dev->dev = &pdev->dev;
  224. dev->irq = pdev->irq;
  225. dev->flags |= controller->flags;
  226. if (controller->setup) {
  227. r = controller->setup(pdev, controller);
  228. if (r)
  229. return r;
  230. }
  231. dev->functionality = controller->functionality |
  232. DW_IC_DEFAULT_FUNCTIONALITY;
  233. dev->master_cfg = controller->bus_cfg;
  234. if (controller->scl_sda_cfg) {
  235. cfg = controller->scl_sda_cfg;
  236. dev->ss_hcnt = cfg->ss_hcnt;
  237. dev->fs_hcnt = cfg->fs_hcnt;
  238. dev->ss_lcnt = cfg->ss_lcnt;
  239. dev->fs_lcnt = cfg->fs_lcnt;
  240. dev->sda_hold_time = cfg->sda_hold;
  241. }
  242. pci_set_drvdata(pdev, dev);
  243. dev->tx_fifo_depth = controller->tx_fifo_depth;
  244. dev->rx_fifo_depth = controller->rx_fifo_depth;
  245. adap = &dev->adapter;
  246. adap->owner = THIS_MODULE;
  247. adap->class = 0;
  248. ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
  249. adap->nr = controller->bus_num;
  250. r = i2c_dw_probe(dev);
  251. if (r)
  252. return r;
  253. pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
  254. pm_runtime_use_autosuspend(&pdev->dev);
  255. pm_runtime_put_autosuspend(&pdev->dev);
  256. pm_runtime_allow(&pdev->dev);
  257. return 0;
  258. }
  259. static void i2c_dw_pci_remove(struct pci_dev *pdev)
  260. {
  261. struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
  262. i2c_dw_disable(dev);
  263. pm_runtime_forbid(&pdev->dev);
  264. pm_runtime_get_noresume(&pdev->dev);
  265. i2c_del_adapter(&dev->adapter);
  266. }
  267. /* work with hotplug and coldplug */
  268. MODULE_ALIAS("i2c_designware-pci");
  269. static const struct pci_device_id i2_designware_pci_ids[] = {
  270. /* Medfield */
  271. { PCI_VDEVICE(INTEL, 0x0817), medfield },
  272. { PCI_VDEVICE(INTEL, 0x0818), medfield },
  273. { PCI_VDEVICE(INTEL, 0x0819), medfield },
  274. { PCI_VDEVICE(INTEL, 0x082C), medfield },
  275. { PCI_VDEVICE(INTEL, 0x082D), medfield },
  276. { PCI_VDEVICE(INTEL, 0x082E), medfield },
  277. /* Merrifield */
  278. { PCI_VDEVICE(INTEL, 0x1195), merrifield },
  279. { PCI_VDEVICE(INTEL, 0x1196), merrifield },
  280. /* Baytrail */
  281. { PCI_VDEVICE(INTEL, 0x0F41), baytrail },
  282. { PCI_VDEVICE(INTEL, 0x0F42), baytrail },
  283. { PCI_VDEVICE(INTEL, 0x0F43), baytrail },
  284. { PCI_VDEVICE(INTEL, 0x0F44), baytrail },
  285. { PCI_VDEVICE(INTEL, 0x0F45), baytrail },
  286. { PCI_VDEVICE(INTEL, 0x0F46), baytrail },
  287. { PCI_VDEVICE(INTEL, 0x0F47), baytrail },
  288. /* Haswell */
  289. { PCI_VDEVICE(INTEL, 0x9c61), haswell },
  290. { PCI_VDEVICE(INTEL, 0x9c62), haswell },
  291. /* Braswell / Cherrytrail */
  292. { PCI_VDEVICE(INTEL, 0x22C1), cherrytrail },
  293. { PCI_VDEVICE(INTEL, 0x22C2), cherrytrail },
  294. { PCI_VDEVICE(INTEL, 0x22C3), cherrytrail },
  295. { PCI_VDEVICE(INTEL, 0x22C4), cherrytrail },
  296. { PCI_VDEVICE(INTEL, 0x22C5), cherrytrail },
  297. { PCI_VDEVICE(INTEL, 0x22C6), cherrytrail },
  298. { PCI_VDEVICE(INTEL, 0x22C7), cherrytrail },
  299. { 0,}
  300. };
  301. MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
  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,
  306. .remove = i2c_dw_pci_remove,
  307. .driver = {
  308. .pm = &i2c_dw_pm_ops,
  309. },
  310. };
  311. module_pci_driver(dw_i2c_driver);
  312. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  313. MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
  314. MODULE_LICENSE("GPL");