i2c-designware-platdrv.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. * ----------------------------------------------------------------------------
  26. *
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/delay.h>
  31. #include <linux/i2c.h>
  32. #include <linux/clk.h>
  33. #include <linux/errno.h>
  34. #include <linux/sched.h>
  35. #include <linux/err.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/of_i2c.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/pm.h>
  40. #include <linux/io.h>
  41. #include <linux/slab.h>
  42. #include "i2c-designware-core.h"
  43. static struct i2c_algorithm i2c_dw_algo = {
  44. .master_xfer = i2c_dw_xfer,
  45. .functionality = i2c_dw_func,
  46. };
  47. static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
  48. {
  49. return clk_get_rate(dev->clk)/1000;
  50. }
  51. static int __devinit dw_i2c_probe(struct platform_device *pdev)
  52. {
  53. struct dw_i2c_dev *dev;
  54. struct i2c_adapter *adap;
  55. struct resource *mem, *ioarea;
  56. int irq, r;
  57. /* NOTE: driver uses the static register mapping */
  58. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  59. if (!mem) {
  60. dev_err(&pdev->dev, "no mem resource?\n");
  61. return -EINVAL;
  62. }
  63. irq = platform_get_irq(pdev, 0);
  64. if (irq < 0) {
  65. dev_err(&pdev->dev, "no irq resource?\n");
  66. return irq; /* -ENXIO */
  67. }
  68. ioarea = request_mem_region(mem->start, resource_size(mem),
  69. pdev->name);
  70. if (!ioarea) {
  71. dev_err(&pdev->dev, "I2C region already claimed\n");
  72. return -EBUSY;
  73. }
  74. dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
  75. if (!dev) {
  76. r = -ENOMEM;
  77. goto err_release_region;
  78. }
  79. init_completion(&dev->cmd_complete);
  80. mutex_init(&dev->lock);
  81. dev->dev = get_device(&pdev->dev);
  82. dev->irq = irq;
  83. platform_set_drvdata(pdev, dev);
  84. dev->clk = clk_get(&pdev->dev, NULL);
  85. dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
  86. if (IS_ERR(dev->clk)) {
  87. r = -ENODEV;
  88. goto err_free_mem;
  89. }
  90. clk_prepare_enable(dev->clk);
  91. dev->functionality =
  92. I2C_FUNC_I2C |
  93. I2C_FUNC_10BIT_ADDR |
  94. I2C_FUNC_SMBUS_BYTE |
  95. I2C_FUNC_SMBUS_BYTE_DATA |
  96. I2C_FUNC_SMBUS_WORD_DATA |
  97. I2C_FUNC_SMBUS_I2C_BLOCK;
  98. dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  99. DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
  100. dev->base = ioremap(mem->start, resource_size(mem));
  101. if (dev->base == NULL) {
  102. dev_err(&pdev->dev, "failure mapping io resources\n");
  103. r = -EBUSY;
  104. goto err_unuse_clocks;
  105. }
  106. {
  107. u32 param1 = i2c_dw_read_comp_param(dev);
  108. dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
  109. dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
  110. }
  111. r = i2c_dw_init(dev);
  112. if (r)
  113. goto err_iounmap;
  114. i2c_dw_disable_int(dev);
  115. r = request_irq(dev->irq, i2c_dw_isr, IRQF_DISABLED, pdev->name, dev);
  116. if (r) {
  117. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  118. goto err_iounmap;
  119. }
  120. adap = &dev->adapter;
  121. i2c_set_adapdata(adap, dev);
  122. adap->owner = THIS_MODULE;
  123. adap->class = I2C_CLASS_HWMON;
  124. strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
  125. sizeof(adap->name));
  126. adap->algo = &i2c_dw_algo;
  127. adap->dev.parent = &pdev->dev;
  128. adap->dev.of_node = pdev->dev.of_node;
  129. adap->nr = pdev->id;
  130. r = i2c_add_numbered_adapter(adap);
  131. if (r) {
  132. dev_err(&pdev->dev, "failure adding adapter\n");
  133. goto err_free_irq;
  134. }
  135. of_i2c_register_devices(adap);
  136. return 0;
  137. err_free_irq:
  138. free_irq(dev->irq, dev);
  139. err_iounmap:
  140. iounmap(dev->base);
  141. err_unuse_clocks:
  142. clk_disable_unprepare(dev->clk);
  143. clk_put(dev->clk);
  144. dev->clk = NULL;
  145. err_free_mem:
  146. platform_set_drvdata(pdev, NULL);
  147. put_device(&pdev->dev);
  148. kfree(dev);
  149. err_release_region:
  150. release_mem_region(mem->start, resource_size(mem));
  151. return r;
  152. }
  153. static int __devexit dw_i2c_remove(struct platform_device *pdev)
  154. {
  155. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  156. struct resource *mem;
  157. platform_set_drvdata(pdev, NULL);
  158. i2c_del_adapter(&dev->adapter);
  159. put_device(&pdev->dev);
  160. clk_disable_unprepare(dev->clk);
  161. clk_put(dev->clk);
  162. dev->clk = NULL;
  163. i2c_dw_disable(dev);
  164. free_irq(dev->irq, dev);
  165. kfree(dev);
  166. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  167. release_mem_region(mem->start, resource_size(mem));
  168. return 0;
  169. }
  170. #ifdef CONFIG_OF
  171. static const struct of_device_id dw_i2c_of_match[] = {
  172. { .compatible = "snps,designware-i2c", },
  173. {},
  174. };
  175. MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
  176. #endif
  177. #ifdef CONFIG_PM
  178. static int dw_i2c_suspend(struct device *dev)
  179. {
  180. struct platform_device *pdev = to_platform_device(dev);
  181. struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
  182. clk_disable_unprepare(i_dev->clk);
  183. return 0;
  184. }
  185. static int dw_i2c_resume(struct device *dev)
  186. {
  187. struct platform_device *pdev = to_platform_device(dev);
  188. struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
  189. clk_prepare_enable(i_dev->clk);
  190. i2c_dw_init(i_dev);
  191. return 0;
  192. }
  193. #endif
  194. static SIMPLE_DEV_PM_OPS(dw_i2c_dev_pm_ops, dw_i2c_suspend, dw_i2c_resume);
  195. /* work with hotplug and coldplug */
  196. MODULE_ALIAS("platform:i2c_designware");
  197. static struct platform_driver dw_i2c_driver = {
  198. .remove = __devexit_p(dw_i2c_remove),
  199. .driver = {
  200. .name = "i2c_designware",
  201. .owner = THIS_MODULE,
  202. .of_match_table = of_match_ptr(dw_i2c_of_match),
  203. .pm = &dw_i2c_dev_pm_ops,
  204. },
  205. };
  206. static int __init dw_i2c_init_driver(void)
  207. {
  208. return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
  209. }
  210. subsys_initcall(dw_i2c_init_driver);
  211. static void __exit dw_i2c_exit_driver(void)
  212. {
  213. platform_driver_unregister(&dw_i2c_driver);
  214. }
  215. module_exit(dw_i2c_exit_driver);
  216. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  217. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
  218. MODULE_LICENSE("GPL");