i2c-simtec.c 3.5 KB

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