i2c-simtec.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/slab.h>
  26. #include <linux/io.h>
  27. #include <linux/i2c.h>
  28. #include <linux/i2c-algo-bit.h>
  29. struct simtec_i2c_data {
  30. struct resource *ioarea;
  31. void __iomem *reg;
  32. struct i2c_adapter adap;
  33. struct i2c_algo_bit_data bit;
  34. };
  35. #define CMD_SET_SDA (1<<2)
  36. #define CMD_SET_SCL (1<<3)
  37. #define STATE_SDA (1<<0)
  38. #define STATE_SCL (1<<1)
  39. /* i2c bit-bus functions */
  40. static void simtec_i2c_setsda(void *pw, int state)
  41. {
  42. struct simtec_i2c_data *pd = pw;
  43. writeb(CMD_SET_SDA | (state ? STATE_SDA : 0), pd->reg);
  44. }
  45. static void simtec_i2c_setscl(void *pw, int state)
  46. {
  47. struct simtec_i2c_data *pd = pw;
  48. writeb(CMD_SET_SCL | (state ? STATE_SCL : 0), pd->reg);
  49. }
  50. static int simtec_i2c_getsda(void *pw)
  51. {
  52. struct simtec_i2c_data *pd = pw;
  53. return readb(pd->reg) & STATE_SDA ? 1 : 0;
  54. }
  55. static int simtec_i2c_getscl(void *pw)
  56. {
  57. struct simtec_i2c_data *pd = pw;
  58. return readb(pd->reg) & STATE_SCL ? 1 : 0;
  59. }
  60. /* device registration */
  61. static int simtec_i2c_probe(struct platform_device *dev)
  62. {
  63. struct simtec_i2c_data *pd;
  64. struct resource *res;
  65. int size;
  66. int ret;
  67. pd = kzalloc(sizeof(struct simtec_i2c_data), GFP_KERNEL);
  68. if (pd == NULL) {
  69. dev_err(&dev->dev, "cannot allocate private data\n");
  70. return -ENOMEM;
  71. }
  72. platform_set_drvdata(dev, pd);
  73. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  74. if (res == NULL) {
  75. dev_err(&dev->dev, "cannot find IO resource\n");
  76. ret = -ENOENT;
  77. goto err;
  78. }
  79. size = resource_size(res);
  80. pd->ioarea = request_mem_region(res->start, size, dev->name);
  81. if (pd->ioarea == NULL) {
  82. dev_err(&dev->dev, "cannot request IO\n");
  83. ret = -ENXIO;
  84. goto err;
  85. }
  86. pd->reg = ioremap(res->start, size);
  87. if (pd->reg == NULL) {
  88. dev_err(&dev->dev, "cannot map IO\n");
  89. ret = -ENXIO;
  90. goto err_res;
  91. }
  92. /* setup the private data */
  93. pd->adap.owner = THIS_MODULE;
  94. pd->adap.algo_data = &pd->bit;
  95. pd->adap.dev.parent = &dev->dev;
  96. strlcpy(pd->adap.name, "Simtec I2C", sizeof(pd->adap.name));
  97. pd->bit.data = pd;
  98. pd->bit.setsda = simtec_i2c_setsda;
  99. pd->bit.setscl = simtec_i2c_setscl;
  100. pd->bit.getsda = simtec_i2c_getsda;
  101. pd->bit.getscl = simtec_i2c_getscl;
  102. pd->bit.timeout = HZ;
  103. pd->bit.udelay = 20;
  104. ret = i2c_bit_add_bus(&pd->adap);
  105. if (ret)
  106. goto err_all;
  107. return 0;
  108. err_all:
  109. iounmap(pd->reg);
  110. err_res:
  111. release_resource(pd->ioarea);
  112. kfree(pd->ioarea);
  113. err:
  114. kfree(pd);
  115. return ret;
  116. }
  117. static int simtec_i2c_remove(struct platform_device *dev)
  118. {
  119. struct simtec_i2c_data *pd = platform_get_drvdata(dev);
  120. i2c_del_adapter(&pd->adap);
  121. iounmap(pd->reg);
  122. release_resource(pd->ioarea);
  123. kfree(pd->ioarea);
  124. kfree(pd);
  125. return 0;
  126. }
  127. /* device driver */
  128. static struct platform_driver simtec_i2c_driver = {
  129. .driver = {
  130. .name = "simtec-i2c",
  131. .owner = THIS_MODULE,
  132. },
  133. .probe = simtec_i2c_probe,
  134. .remove = simtec_i2c_remove,
  135. };
  136. module_platform_driver(simtec_i2c_driver);
  137. MODULE_DESCRIPTION("Simtec Generic I2C Bus driver");
  138. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  139. MODULE_LICENSE("GPL");
  140. MODULE_ALIAS("platform:simtec-i2c");