i2c-designware-pcidrv.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. };
  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. };
  61. #define INTEL_MID_STD_CFG (DW_IC_CON_MASTER | \
  62. DW_IC_CON_SLAVE_DISABLE | \
  63. DW_IC_CON_RESTART_EN)
  64. static struct dw_pci_controller dw_pci_controllers[] = {
  65. [moorestown_0] = {
  66. .bus_num = 0,
  67. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  68. .tx_fifo_depth = 32,
  69. .rx_fifo_depth = 32,
  70. .clk_khz = 25000,
  71. },
  72. [moorestown_1] = {
  73. .bus_num = 1,
  74. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  75. .tx_fifo_depth = 32,
  76. .rx_fifo_depth = 32,
  77. .clk_khz = 25000,
  78. },
  79. [moorestown_2] = {
  80. .bus_num = 2,
  81. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  82. .tx_fifo_depth = 32,
  83. .rx_fifo_depth = 32,
  84. .clk_khz = 25000,
  85. },
  86. [medfield_0] = {
  87. .bus_num = 0,
  88. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  89. .tx_fifo_depth = 32,
  90. .rx_fifo_depth = 32,
  91. .clk_khz = 25000,
  92. },
  93. [medfield_1] = {
  94. .bus_num = 1,
  95. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  96. .tx_fifo_depth = 32,
  97. .rx_fifo_depth = 32,
  98. .clk_khz = 25000,
  99. },
  100. [medfield_2] = {
  101. .bus_num = 2,
  102. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  103. .tx_fifo_depth = 32,
  104. .rx_fifo_depth = 32,
  105. .clk_khz = 25000,
  106. },
  107. [medfield_3] = {
  108. .bus_num = 3,
  109. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_STD,
  110. .tx_fifo_depth = 32,
  111. .rx_fifo_depth = 32,
  112. .clk_khz = 25000,
  113. },
  114. [medfield_4] = {
  115. .bus_num = 4,
  116. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  117. .tx_fifo_depth = 32,
  118. .rx_fifo_depth = 32,
  119. .clk_khz = 25000,
  120. },
  121. [medfield_5] = {
  122. .bus_num = 5,
  123. .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
  124. .tx_fifo_depth = 32,
  125. .rx_fifo_depth = 32,
  126. .clk_khz = 25000,
  127. },
  128. };
  129. static struct i2c_algorithm i2c_dw_algo = {
  130. .master_xfer = i2c_dw_xfer,
  131. .functionality = i2c_dw_func,
  132. };
  133. static int i2c_dw_pci_suspend(struct device *dev)
  134. {
  135. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  136. struct dw_i2c_dev *i2c = pci_get_drvdata(pdev);
  137. int err;
  138. i2c_dw_disable(i2c);
  139. err = pci_save_state(pdev);
  140. if (err) {
  141. dev_err(&pdev->dev, "pci_save_state failed\n");
  142. return err;
  143. }
  144. err = pci_set_power_state(pdev, PCI_D3hot);
  145. if (err) {
  146. dev_err(&pdev->dev, "pci_set_power_state failed\n");
  147. return err;
  148. }
  149. return 0;
  150. }
  151. static int i2c_dw_pci_resume(struct device *dev)
  152. {
  153. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  154. struct dw_i2c_dev *i2c = pci_get_drvdata(pdev);
  155. int err;
  156. u32 enabled;
  157. enabled = i2c_dw_is_enabled(i2c);
  158. if (enabled)
  159. return 0;
  160. err = pci_set_power_state(pdev, PCI_D0);
  161. if (err) {
  162. dev_err(&pdev->dev, "pci_set_power_state() failed\n");
  163. return err;
  164. }
  165. pci_restore_state(pdev);
  166. i2c_dw_init(i2c);
  167. i2c_dw_enable(i2c);
  168. return 0;
  169. }
  170. static int i2c_dw_pci_runtime_idle(struct device *dev)
  171. {
  172. int err = pm_schedule_suspend(dev, 500);
  173. dev_dbg(dev, "runtime_idle called\n");
  174. if (err != 0)
  175. return 0;
  176. return -EBUSY;
  177. }
  178. static const struct dev_pm_ops i2c_dw_pm_ops = {
  179. .resume = i2c_dw_pci_resume,
  180. .suspend = i2c_dw_pci_suspend,
  181. SET_RUNTIME_PM_OPS(i2c_dw_pci_suspend, i2c_dw_pci_resume,
  182. i2c_dw_pci_runtime_idle)
  183. };
  184. static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
  185. {
  186. return dev->controller->clk_khz;
  187. }
  188. static int __devinit i2c_dw_pci_probe(struct pci_dev *pdev,
  189. const struct pci_device_id *id)
  190. {
  191. struct dw_i2c_dev *dev;
  192. struct i2c_adapter *adap;
  193. unsigned long start, len;
  194. void __iomem *base;
  195. int r;
  196. struct dw_pci_controller *controller;
  197. if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
  198. printk(KERN_ERR "dw_i2c_pci_probe: invalid driver data %ld\n",
  199. id->driver_data);
  200. return -EINVAL;
  201. }
  202. controller = &dw_pci_controllers[id->driver_data];
  203. r = pci_enable_device(pdev);
  204. if (r) {
  205. dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
  206. r);
  207. goto exit;
  208. }
  209. /* Determine the address of the I2C area */
  210. start = pci_resource_start(pdev, 0);
  211. len = pci_resource_len(pdev, 0);
  212. if (!start || len == 0) {
  213. dev_err(&pdev->dev, "base address not set\n");
  214. r = -ENODEV;
  215. goto exit;
  216. }
  217. r = pci_request_region(pdev, 0, DRIVER_NAME);
  218. if (r) {
  219. dev_err(&pdev->dev, "failed to request I2C region "
  220. "0x%lx-0x%lx\n", start,
  221. (unsigned long)pci_resource_end(pdev, 0));
  222. goto exit;
  223. }
  224. base = ioremap_nocache(start, len);
  225. if (!base) {
  226. dev_err(&pdev->dev, "I/O memory remapping failed\n");
  227. r = -ENOMEM;
  228. goto err_release_region;
  229. }
  230. dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
  231. if (!dev) {
  232. r = -ENOMEM;
  233. goto err_release_region;
  234. }
  235. init_completion(&dev->cmd_complete);
  236. mutex_init(&dev->lock);
  237. dev->clk = NULL;
  238. dev->controller = controller;
  239. dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
  240. dev->base = base;
  241. dev->dev = get_device(&pdev->dev);
  242. dev->functionality =
  243. I2C_FUNC_I2C |
  244. I2C_FUNC_SMBUS_BYTE |
  245. I2C_FUNC_SMBUS_BYTE_DATA |
  246. I2C_FUNC_SMBUS_WORD_DATA |
  247. I2C_FUNC_SMBUS_I2C_BLOCK;
  248. dev->master_cfg = controller->bus_cfg;
  249. pci_set_drvdata(pdev, dev);
  250. dev->tx_fifo_depth = controller->tx_fifo_depth;
  251. dev->rx_fifo_depth = controller->rx_fifo_depth;
  252. r = i2c_dw_init(dev);
  253. if (r)
  254. goto err_iounmap;
  255. adap = &dev->adapter;
  256. i2c_set_adapdata(adap, dev);
  257. adap->owner = THIS_MODULE;
  258. adap->class = 0;
  259. adap->algo = &i2c_dw_algo;
  260. adap->dev.parent = &pdev->dev;
  261. adap->nr = controller->bus_num;
  262. snprintf(adap->name, sizeof(adap->name), "i2c-designware-pci-%d",
  263. adap->nr);
  264. r = request_irq(pdev->irq, i2c_dw_isr, IRQF_SHARED, adap->name, dev);
  265. if (r) {
  266. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  267. goto err_iounmap;
  268. }
  269. i2c_dw_disable_int(dev);
  270. i2c_dw_clear_int(dev);
  271. r = i2c_add_numbered_adapter(adap);
  272. if (r) {
  273. dev_err(&pdev->dev, "failure adding adapter\n");
  274. goto err_free_irq;
  275. }
  276. pm_runtime_put_noidle(&pdev->dev);
  277. pm_runtime_allow(&pdev->dev);
  278. return 0;
  279. err_free_irq:
  280. free_irq(pdev->irq, dev);
  281. err_iounmap:
  282. iounmap(dev->base);
  283. pci_set_drvdata(pdev, NULL);
  284. put_device(&pdev->dev);
  285. kfree(dev);
  286. err_release_region:
  287. pci_release_region(pdev, 0);
  288. exit:
  289. return r;
  290. }
  291. static void __devexit i2c_dw_pci_remove(struct pci_dev *pdev)
  292. {
  293. struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
  294. i2c_dw_disable(dev);
  295. pm_runtime_forbid(&pdev->dev);
  296. pm_runtime_get_noresume(&pdev->dev);
  297. pci_set_drvdata(pdev, NULL);
  298. i2c_del_adapter(&dev->adapter);
  299. put_device(&pdev->dev);
  300. free_irq(dev->irq, dev);
  301. kfree(dev);
  302. pci_release_region(pdev, 0);
  303. }
  304. /* work with hotplug and coldplug */
  305. MODULE_ALIAS("i2c_designware-pci");
  306. static DEFINE_PCI_DEVICE_TABLE(i2_designware_pci_ids) = {
  307. /* Moorestown */
  308. { PCI_VDEVICE(INTEL, 0x0802), moorestown_0 },
  309. { PCI_VDEVICE(INTEL, 0x0803), moorestown_1 },
  310. { PCI_VDEVICE(INTEL, 0x0804), moorestown_2 },
  311. /* Medfield */
  312. { PCI_VDEVICE(INTEL, 0x0817), medfield_3,},
  313. { PCI_VDEVICE(INTEL, 0x0818), medfield_4 },
  314. { PCI_VDEVICE(INTEL, 0x0819), medfield_5 },
  315. { PCI_VDEVICE(INTEL, 0x082C), medfield_0 },
  316. { PCI_VDEVICE(INTEL, 0x082D), medfield_1 },
  317. { PCI_VDEVICE(INTEL, 0x082E), medfield_2 },
  318. { 0,}
  319. };
  320. MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
  321. static struct pci_driver dw_i2c_driver = {
  322. .name = DRIVER_NAME,
  323. .id_table = i2_designware_pci_ids,
  324. .probe = i2c_dw_pci_probe,
  325. .remove = __devexit_p(i2c_dw_pci_remove),
  326. .driver = {
  327. .pm = &i2c_dw_pm_ops,
  328. },
  329. };
  330. static int __init dw_i2c_init_driver(void)
  331. {
  332. return pci_register_driver(&dw_i2c_driver);
  333. }
  334. module_init(dw_i2c_init_driver);
  335. static void __exit dw_i2c_exit_driver(void)
  336. {
  337. pci_unregister_driver(&dw_i2c_driver);
  338. }
  339. module_exit(dw_i2c_exit_driver);
  340. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  341. MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
  342. MODULE_LICENSE("GPL");