i2c-simtec.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2005 Simtec Electronics
  3. * Ben Dooks <ben@simtec.co.uk>
  4. *
  5. * Simtec Generic I2C Controller
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c-algo-bit.h>
  27. #include <asm/io.h>
  28. struct simtec_i2c_data {
  29. struct resource *ioarea;
  30. void __iomem *reg;
  31. struct i2c_adapter adap;
  32. struct i2c_algo_bit_data bit;
  33. };
  34. #define CMD_SET_SDA (1<<2)
  35. #define CMD_SET_SCL (1<<3)
  36. #define STATE_SDA (1<<0)
  37. #define STATE_SCL (1<<1)
  38. /* i2c bit-bus functions */
  39. static void simtec_i2c_setsda(void *pw, int state)
  40. {
  41. struct simtec_i2c_data *pd = pw;
  42. writeb(CMD_SET_SDA | (state ? STATE_SDA : 0), pd->reg);
  43. }
  44. static void simtec_i2c_setscl(void *pw, int state)
  45. {
  46. struct simtec_i2c_data *pd = pw;
  47. writeb(CMD_SET_SCL | (state ? STATE_SCL : 0), pd->reg);
  48. }
  49. static int simtec_i2c_getsda(void *pw)
  50. {
  51. struct simtec_i2c_data *pd = pw;
  52. return readb(pd->reg) & STATE_SDA ? 1 : 0;
  53. }
  54. static int simtec_i2c_getscl(void *pw)
  55. {
  56. struct simtec_i2c_data *pd = pw;
  57. return readb(pd->reg) & STATE_SCL ? 1 : 0;
  58. }
  59. /* device registration */
  60. static int simtec_i2c_probe(struct platform_device *dev)
  61. {
  62. struct simtec_i2c_data *pd;
  63. struct resource *res;
  64. int size;
  65. int ret;
  66. pd = kzalloc(sizeof(struct simtec_i2c_data), GFP_KERNEL);
  67. if (pd == NULL) {
  68. dev_err(&dev->dev, "cannot allocate private data\n");
  69. return -ENOMEM;
  70. }
  71. platform_set_drvdata(dev, pd);
  72. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  73. if (res == NULL) {
  74. dev_err(&dev->dev, "cannot find IO resource\n");
  75. ret = -ENOENT;
  76. goto err;
  77. }
  78. size = (res->end-res->start)+1;
  79. pd->ioarea = request_mem_region(res->start, size, dev->name);
  80. if (pd->ioarea == NULL) {
  81. dev_err(&dev->dev, "cannot request IO\n");
  82. ret = -ENXIO;
  83. goto err;
  84. }
  85. pd->reg = ioremap(res->start, size);
  86. if (pd->reg == NULL) {
  87. dev_err(&dev->dev, "cannot map IO\n");
  88. ret = -ENXIO;
  89. goto err_res;
  90. }
  91. /* setup the private data */
  92. pd->adap.owner = THIS_MODULE;
  93. pd->adap.algo_data = &pd->bit;
  94. pd->adap.dev.parent = &dev->dev;
  95. strlcpy(pd->adap.name, "Simtec I2C", sizeof(pd->adap.name));
  96. pd->bit.data = pd;
  97. pd->bit.setsda = simtec_i2c_setsda;
  98. pd->bit.setscl = simtec_i2c_setscl;
  99. pd->bit.getsda = simtec_i2c_getsda;
  100. pd->bit.getscl = simtec_i2c_getscl;
  101. pd->bit.timeout = HZ;
  102. pd->bit.udelay = 20;
  103. ret = i2c_bit_add_bus(&pd->adap);
  104. if (ret)
  105. goto err_all;
  106. return 0;
  107. err_all:
  108. iounmap(pd->reg);
  109. err_res:
  110. release_resource(pd->ioarea);
  111. kfree(pd->ioarea);
  112. err:
  113. kfree(pd);
  114. return ret;
  115. }
  116. static int simtec_i2c_remove(struct platform_device *dev)
  117. {
  118. struct simtec_i2c_data *pd = platform_get_drvdata(dev);
  119. i2c_del_adapter(&pd->adap);
  120. iounmap(pd->reg);
  121. release_resource(pd->ioarea);
  122. kfree(pd->ioarea);
  123. kfree(pd);
  124. return 0;
  125. }
  126. /* device driver */
  127. /* work with hotplug and coldplug */
  128. MODULE_ALIAS("platform:simtec-i2c");
  129. static struct platform_driver simtec_i2c_driver = {
  130. .driver = {
  131. .name = "simtec-i2c",
  132. .owner = THIS_MODULE,
  133. },
  134. .probe = simtec_i2c_probe,
  135. .remove = simtec_i2c_remove,
  136. };
  137. static int __init i2c_adap_simtec_init(void)
  138. {
  139. return platform_driver_register(&simtec_i2c_driver);
  140. }
  141. static void __exit i2c_adap_simtec_exit(void)
  142. {
  143. platform_driver_unregister(&simtec_i2c_driver);
  144. }
  145. module_init(i2c_adap_simtec_init);
  146. module_exit(i2c_adap_simtec_exit);
  147. MODULE_DESCRIPTION("Simtec Generic I2C Bus driver");
  148. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  149. MODULE_LICENSE("GPL");