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. return 0;
  168. }
  169. static int i2c_dw_pci_runtime_idle(struct device *dev)
  170. {
  171. int err = pm_schedule_suspend(dev, 500);
  172. dev_dbg(dev, "runtime_idle called\n");
  173. if (err != 0)
  174. return 0;
  175. return -EBUSY;
  176. }
  177. static const struct dev_pm_ops i2c_dw_pm_ops = {
  178. .resume = i2c_dw_pci_resume,
  179. .suspend = i2c_dw_pci_suspend,
  180. SET_RUNTIME_PM_OPS(i2c_dw_pci_suspend, i2c_dw_pci_resume,
  181. i2c_dw_pci_runtime_idle)
  182. };
  183. static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
  184. {
  185. return dev->controller->clk_khz;
  186. }
  187. static int __devinit i2c_dw_pci_probe(struct pci_dev *pdev,
  188. const struct pci_device_id *id)
  189. {
  190. struct dw_i2c_dev *dev;
  191. struct i2c_adapter *adap;
  192. unsigned long start, len;
  193. void __iomem *base;
  194. int r;
  195. struct dw_pci_controller *controller;
  196. if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
  197. printk(KERN_ERR "dw_i2c_pci_probe: invalid driver data %ld\n",
  198. id->driver_data);
  199. return -EINVAL;
  200. }
  201. controller = &dw_pci_controllers[id->driver_data];
  202. r = pci_enable_device(pdev);
  203. if (r) {
  204. dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
  205. r);
  206. goto exit;
  207. }
  208. /* Determine the address of the I2C area */
  209. start = pci_resource_start(pdev, 0);
  210. len = pci_resource_len(pdev, 0);
  211. if (!start || len == 0) {
  212. dev_err(&pdev->dev, "base address not set\n");
  213. r = -ENODEV;
  214. goto exit;
  215. }
  216. r = pci_request_region(pdev, 0, DRIVER_NAME);
  217. if (r) {
  218. dev_err(&pdev->dev, "failed to request I2C region "
  219. "0x%lx-0x%lx\n", start,
  220. (unsigned long)pci_resource_end(pdev, 0));
  221. goto exit;
  222. }
  223. base = ioremap_nocache(start, len);
  224. if (!base) {
  225. dev_err(&pdev->dev, "I/O memory remapping failed\n");
  226. r = -ENOMEM;
  227. goto err_release_region;
  228. }
  229. dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
  230. if (!dev) {
  231. r = -ENOMEM;
  232. goto err_release_region;
  233. }
  234. init_completion(&dev->cmd_complete);
  235. mutex_init(&dev->lock);
  236. dev->clk = NULL;
  237. dev->controller = controller;
  238. dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
  239. dev->base = base;
  240. dev->dev = get_device(&pdev->dev);
  241. dev->functionality =
  242. I2C_FUNC_I2C |
  243. I2C_FUNC_SMBUS_BYTE |
  244. I2C_FUNC_SMBUS_BYTE_DATA |
  245. I2C_FUNC_SMBUS_WORD_DATA |
  246. I2C_FUNC_SMBUS_I2C_BLOCK;
  247. dev->master_cfg = controller->bus_cfg;
  248. pci_set_drvdata(pdev, dev);
  249. dev->tx_fifo_depth = controller->tx_fifo_depth;
  250. dev->rx_fifo_depth = controller->rx_fifo_depth;
  251. r = i2c_dw_init(dev);
  252. if (r)
  253. goto err_iounmap;
  254. adap = &dev->adapter;
  255. i2c_set_adapdata(adap, dev);
  256. adap->owner = THIS_MODULE;
  257. adap->class = 0;
  258. adap->algo = &i2c_dw_algo;
  259. adap->dev.parent = &pdev->dev;
  260. adap->nr = controller->bus_num;
  261. snprintf(adap->name, sizeof(adap->name), "i2c-designware-pci-%d",
  262. adap->nr);
  263. r = request_irq(pdev->irq, i2c_dw_isr, IRQF_SHARED, adap->name, dev);
  264. if (r) {
  265. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  266. goto err_iounmap;
  267. }
  268. i2c_dw_disable_int(dev);
  269. i2c_dw_clear_int(dev);
  270. r = i2c_add_numbered_adapter(adap);
  271. if (r) {
  272. dev_err(&pdev->dev, "failure adding adapter\n");
  273. goto err_free_irq;
  274. }
  275. pm_runtime_put_noidle(&pdev->dev);
  276. pm_runtime_allow(&pdev->dev);
  277. return 0;
  278. err_free_irq:
  279. free_irq(pdev->irq, dev);
  280. err_iounmap:
  281. iounmap(dev->base);
  282. pci_set_drvdata(pdev, NULL);
  283. put_device(&pdev->dev);
  284. kfree(dev);
  285. err_release_region:
  286. pci_release_region(pdev, 0);
  287. exit:
  288. return r;
  289. }
  290. static void __devexit i2c_dw_pci_remove(struct pci_dev *pdev)
  291. {
  292. struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
  293. i2c_dw_disable(dev);
  294. pm_runtime_forbid(&pdev->dev);
  295. pm_runtime_get_noresume(&pdev->dev);
  296. pci_set_drvdata(pdev, NULL);
  297. i2c_del_adapter(&dev->adapter);
  298. put_device(&pdev->dev);
  299. free_irq(dev->irq, dev);
  300. kfree(dev);
  301. pci_release_region(pdev, 0);
  302. }
  303. /* work with hotplug and coldplug */
  304. MODULE_ALIAS("i2c_designware-pci");
  305. static DEFINE_PCI_DEVICE_TABLE(i2_designware_pci_ids) = {
  306. /* Moorestown */
  307. { PCI_VDEVICE(INTEL, 0x0802), moorestown_0 },
  308. { PCI_VDEVICE(INTEL, 0x0803), moorestown_1 },
  309. { PCI_VDEVICE(INTEL, 0x0804), moorestown_2 },
  310. /* Medfield */
  311. { PCI_VDEVICE(INTEL, 0x0817), medfield_3,},
  312. { PCI_VDEVICE(INTEL, 0x0818), medfield_4 },
  313. { PCI_VDEVICE(INTEL, 0x0819), medfield_5 },
  314. { PCI_VDEVICE(INTEL, 0x082C), medfield_0 },
  315. { PCI_VDEVICE(INTEL, 0x082D), medfield_1 },
  316. { PCI_VDEVICE(INTEL, 0x082E), medfield_2 },
  317. { 0,}
  318. };
  319. MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
  320. static struct pci_driver dw_i2c_driver = {
  321. .name = DRIVER_NAME,
  322. .id_table = i2_designware_pci_ids,
  323. .probe = i2c_dw_pci_probe,
  324. .remove = __devexit_p(i2c_dw_pci_remove),
  325. .driver = {
  326. .pm = &i2c_dw_pm_ops,
  327. },
  328. };
  329. static int __init dw_i2c_init_driver(void)
  330. {
  331. return pci_register_driver(&dw_i2c_driver);
  332. }
  333. module_init(dw_i2c_init_driver);
  334. static void __exit dw_i2c_exit_driver(void)
  335. {
  336. pci_unregister_driver(&dw_i2c_driver);
  337. }
  338. module_exit(dw_i2c_exit_driver);
  339. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  340. MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
  341. MODULE_LICENSE("GPL");