i2c-designware-platdrv.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/io.h>
  40. #include <linux/slab.h>
  41. #include "i2c-designware-core.h"
  42. static struct i2c_algorithm i2c_dw_algo = {
  43. .master_xfer = i2c_dw_xfer,
  44. .functionality = i2c_dw_func,
  45. };
  46. static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
  47. {
  48. return clk_get_rate(dev->clk)/1000;
  49. }
  50. static int __devinit dw_i2c_probe(struct platform_device *pdev)
  51. {
  52. struct dw_i2c_dev *dev;
  53. struct i2c_adapter *adap;
  54. struct resource *mem, *ioarea;
  55. int irq, r;
  56. /* NOTE: driver uses the static register mapping */
  57. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  58. if (!mem) {
  59. dev_err(&pdev->dev, "no mem resource?\n");
  60. return -EINVAL;
  61. }
  62. irq = platform_get_irq(pdev, 0);
  63. if (irq < 0) {
  64. dev_err(&pdev->dev, "no irq resource?\n");
  65. return irq; /* -ENXIO */
  66. }
  67. ioarea = request_mem_region(mem->start, resource_size(mem),
  68. pdev->name);
  69. if (!ioarea) {
  70. dev_err(&pdev->dev, "I2C region already claimed\n");
  71. return -EBUSY;
  72. }
  73. dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
  74. if (!dev) {
  75. r = -ENOMEM;
  76. goto err_release_region;
  77. }
  78. init_completion(&dev->cmd_complete);
  79. mutex_init(&dev->lock);
  80. dev->dev = get_device(&pdev->dev);
  81. dev->irq = irq;
  82. platform_set_drvdata(pdev, dev);
  83. dev->clk = clk_get(&pdev->dev, NULL);
  84. dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
  85. if (IS_ERR(dev->clk)) {
  86. r = -ENODEV;
  87. goto err_free_mem;
  88. }
  89. clk_enable(dev->clk);
  90. dev->functionality =
  91. I2C_FUNC_I2C |
  92. I2C_FUNC_10BIT_ADDR |
  93. I2C_FUNC_SMBUS_BYTE |
  94. I2C_FUNC_SMBUS_BYTE_DATA |
  95. I2C_FUNC_SMBUS_WORD_DATA |
  96. I2C_FUNC_SMBUS_I2C_BLOCK;
  97. dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  98. DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
  99. dev->base = ioremap(mem->start, resource_size(mem));
  100. if (dev->base == NULL) {
  101. dev_err(&pdev->dev, "failure mapping io resources\n");
  102. r = -EBUSY;
  103. goto err_unuse_clocks;
  104. }
  105. {
  106. u32 param1 = i2c_dw_read_comp_param(dev);
  107. dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
  108. dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
  109. }
  110. r = i2c_dw_init(dev);
  111. if (r)
  112. goto err_iounmap;
  113. i2c_dw_disable_int(dev);
  114. r = request_irq(dev->irq, i2c_dw_isr, IRQF_DISABLED, pdev->name, dev);
  115. if (r) {
  116. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  117. goto err_iounmap;
  118. }
  119. adap = &dev->adapter;
  120. i2c_set_adapdata(adap, dev);
  121. adap->owner = THIS_MODULE;
  122. adap->class = I2C_CLASS_HWMON;
  123. strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
  124. sizeof(adap->name));
  125. adap->algo = &i2c_dw_algo;
  126. adap->dev.parent = &pdev->dev;
  127. adap->dev.of_node = pdev->dev.of_node;
  128. adap->nr = pdev->id;
  129. r = i2c_add_numbered_adapter(adap);
  130. if (r) {
  131. dev_err(&pdev->dev, "failure adding adapter\n");
  132. goto err_free_irq;
  133. }
  134. of_i2c_register_devices(adap);
  135. return 0;
  136. err_free_irq:
  137. free_irq(dev->irq, dev);
  138. err_iounmap:
  139. iounmap(dev->base);
  140. err_unuse_clocks:
  141. clk_disable(dev->clk);
  142. clk_put(dev->clk);
  143. dev->clk = NULL;
  144. err_free_mem:
  145. platform_set_drvdata(pdev, NULL);
  146. put_device(&pdev->dev);
  147. kfree(dev);
  148. err_release_region:
  149. release_mem_region(mem->start, resource_size(mem));
  150. return r;
  151. }
  152. static int __devexit dw_i2c_remove(struct platform_device *pdev)
  153. {
  154. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  155. struct resource *mem;
  156. platform_set_drvdata(pdev, NULL);
  157. i2c_del_adapter(&dev->adapter);
  158. put_device(&pdev->dev);
  159. clk_disable(dev->clk);
  160. clk_put(dev->clk);
  161. dev->clk = NULL;
  162. i2c_dw_disable(dev);
  163. free_irq(dev->irq, dev);
  164. kfree(dev);
  165. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  166. release_mem_region(mem->start, resource_size(mem));
  167. return 0;
  168. }
  169. #ifdef CONFIG_OF
  170. static const struct of_device_id dw_i2c_of_match[] = {
  171. { .compatible = "snps,designware-i2c", },
  172. {},
  173. };
  174. MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
  175. #endif
  176. /* work with hotplug and coldplug */
  177. MODULE_ALIAS("platform:i2c_designware");
  178. static struct platform_driver dw_i2c_driver = {
  179. .remove = __devexit_p(dw_i2c_remove),
  180. .driver = {
  181. .name = "i2c_designware",
  182. .owner = THIS_MODULE,
  183. .of_match_table = of_match_ptr(dw_i2c_of_match),
  184. },
  185. };
  186. static int __init dw_i2c_init_driver(void)
  187. {
  188. return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
  189. }
  190. subsys_initcall(dw_i2c_init_driver);
  191. static void __exit dw_i2c_exit_driver(void)
  192. {
  193. platform_driver_unregister(&dw_i2c_driver);
  194. }
  195. module_exit(dw_i2c_exit_driver);
  196. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  197. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
  198. MODULE_LICENSE("GPL");