i2c-ocores.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
  3. * (http://www.opencores.org/projects.cgi/web/i2c/overview).
  4. *
  5. * Peter Korsgaard <jacmet@sunsite.dk>
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/wait.h>
  19. #include <linux/i2c-ocores.h>
  20. #include <asm/io.h>
  21. struct ocores_i2c {
  22. void __iomem *base;
  23. int regstep;
  24. wait_queue_head_t wait;
  25. struct i2c_adapter adap;
  26. struct i2c_msg *msg;
  27. int pos;
  28. int nmsgs;
  29. int state; /* see STATE_ */
  30. };
  31. /* registers */
  32. #define OCI2C_PRELOW 0
  33. #define OCI2C_PREHIGH 1
  34. #define OCI2C_CONTROL 2
  35. #define OCI2C_DATA 3
  36. #define OCI2C_CMD 4 /* write only */
  37. #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
  38. #define OCI2C_CTRL_IEN 0x40
  39. #define OCI2C_CTRL_EN 0x80
  40. #define OCI2C_CMD_START 0x91
  41. #define OCI2C_CMD_STOP 0x41
  42. #define OCI2C_CMD_READ 0x21
  43. #define OCI2C_CMD_WRITE 0x11
  44. #define OCI2C_CMD_READ_ACK 0x21
  45. #define OCI2C_CMD_READ_NACK 0x29
  46. #define OCI2C_CMD_IACK 0x01
  47. #define OCI2C_STAT_IF 0x01
  48. #define OCI2C_STAT_TIP 0x02
  49. #define OCI2C_STAT_ARBLOST 0x20
  50. #define OCI2C_STAT_BUSY 0x40
  51. #define OCI2C_STAT_NACK 0x80
  52. #define STATE_DONE 0
  53. #define STATE_START 1
  54. #define STATE_WRITE 2
  55. #define STATE_READ 3
  56. #define STATE_ERROR 4
  57. static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
  58. {
  59. iowrite8(value, i2c->base + reg * i2c->regstep);
  60. }
  61. static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
  62. {
  63. return ioread8(i2c->base + reg * i2c->regstep);
  64. }
  65. static void ocores_process(struct ocores_i2c *i2c)
  66. {
  67. struct i2c_msg *msg = i2c->msg;
  68. u8 stat = oc_getreg(i2c, OCI2C_STATUS);
  69. if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
  70. /* stop has been sent */
  71. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  72. wake_up(&i2c->wait);
  73. return;
  74. }
  75. /* error? */
  76. if (stat & OCI2C_STAT_ARBLOST) {
  77. i2c->state = STATE_ERROR;
  78. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  79. return;
  80. }
  81. if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
  82. i2c->state =
  83. (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  84. if (stat & OCI2C_STAT_NACK) {
  85. i2c->state = STATE_ERROR;
  86. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  87. return;
  88. }
  89. } else
  90. msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
  91. /* end of msg? */
  92. if (i2c->pos == msg->len) {
  93. i2c->nmsgs--;
  94. i2c->msg++;
  95. i2c->pos = 0;
  96. msg = i2c->msg;
  97. if (i2c->nmsgs) { /* end? */
  98. /* send start? */
  99. if (!(msg->flags & I2C_M_NOSTART)) {
  100. u8 addr = (msg->addr << 1);
  101. if (msg->flags & I2C_M_RD)
  102. addr |= 1;
  103. i2c->state = STATE_START;
  104. oc_setreg(i2c, OCI2C_DATA, addr);
  105. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  106. return;
  107. } else
  108. i2c->state = (msg->flags & I2C_M_RD)
  109. ? STATE_READ : STATE_WRITE;
  110. } else {
  111. i2c->state = STATE_DONE;
  112. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  113. return;
  114. }
  115. }
  116. if (i2c->state == STATE_READ) {
  117. oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
  118. OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
  119. } else {
  120. oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
  121. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
  122. }
  123. }
  124. static irqreturn_t ocores_isr(int irq, void *dev_id)
  125. {
  126. struct ocores_i2c *i2c = dev_id;
  127. ocores_process(i2c);
  128. return IRQ_HANDLED;
  129. }
  130. static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  131. {
  132. struct ocores_i2c *i2c = i2c_get_adapdata(adap);
  133. i2c->msg = msgs;
  134. i2c->pos = 0;
  135. i2c->nmsgs = num;
  136. i2c->state = STATE_START;
  137. oc_setreg(i2c, OCI2C_DATA,
  138. (i2c->msg->addr << 1) |
  139. ((i2c->msg->flags & I2C_M_RD) ? 1:0));
  140. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  141. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  142. (i2c->state == STATE_DONE), HZ))
  143. return (i2c->state == STATE_DONE) ? num : -EIO;
  144. else
  145. return -ETIMEDOUT;
  146. }
  147. static void ocores_init(struct ocores_i2c *i2c,
  148. struct ocores_i2c_platform_data *pdata)
  149. {
  150. int prescale;
  151. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  152. /* make sure the device is disabled */
  153. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  154. prescale = (pdata->clock_khz / (5*100)) - 1;
  155. oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
  156. oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
  157. /* Init the device */
  158. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  159. oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
  160. }
  161. static u32 ocores_func(struct i2c_adapter *adap)
  162. {
  163. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  164. }
  165. static const struct i2c_algorithm ocores_algorithm = {
  166. .master_xfer = ocores_xfer,
  167. .functionality = ocores_func,
  168. };
  169. static struct i2c_adapter ocores_adapter = {
  170. .owner = THIS_MODULE,
  171. .name = "i2c-ocores",
  172. .class = I2C_CLASS_HWMON,
  173. .algo = &ocores_algorithm,
  174. };
  175. static int __devinit ocores_i2c_probe(struct platform_device *pdev)
  176. {
  177. struct ocores_i2c *i2c;
  178. struct ocores_i2c_platform_data *pdata;
  179. struct resource *res, *res2;
  180. int ret;
  181. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  182. if (!res)
  183. return -ENODEV;
  184. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  185. if (!res2)
  186. return -ENODEV;
  187. pdata = (struct ocores_i2c_platform_data*) pdev->dev.platform_data;
  188. if (!pdata)
  189. return -ENODEV;
  190. i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
  191. if (!i2c)
  192. return -ENOMEM;
  193. if (!request_mem_region(res->start, res->end - res->start + 1,
  194. pdev->name)) {
  195. dev_err(&pdev->dev, "Memory region busy\n");
  196. ret = -EBUSY;
  197. goto request_mem_failed;
  198. }
  199. i2c->base = ioremap(res->start, res->end - res->start + 1);
  200. if (!i2c->base) {
  201. dev_err(&pdev->dev, "Unable to map registers\n");
  202. ret = -EIO;
  203. goto map_failed;
  204. }
  205. i2c->regstep = pdata->regstep;
  206. ocores_init(i2c, pdata);
  207. init_waitqueue_head(&i2c->wait);
  208. ret = request_irq(res2->start, ocores_isr, 0, pdev->name, i2c);
  209. if (ret) {
  210. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  211. goto request_irq_failed;
  212. }
  213. /* hook up driver to tree */
  214. platform_set_drvdata(pdev, i2c);
  215. i2c->adap = ocores_adapter;
  216. i2c_set_adapdata(&i2c->adap, i2c);
  217. i2c->adap.dev.parent = &pdev->dev;
  218. /* add i2c adapter to i2c tree */
  219. ret = i2c_add_adapter(&i2c->adap);
  220. if (ret) {
  221. dev_err(&pdev->dev, "Failed to add adapter\n");
  222. goto add_adapter_failed;
  223. }
  224. return 0;
  225. add_adapter_failed:
  226. free_irq(res2->start, i2c);
  227. request_irq_failed:
  228. iounmap(i2c->base);
  229. map_failed:
  230. release_mem_region(res->start, res->end - res->start + 1);
  231. request_mem_failed:
  232. kfree(i2c);
  233. return ret;
  234. }
  235. static int __devexit ocores_i2c_remove(struct platform_device* pdev)
  236. {
  237. struct ocores_i2c *i2c = platform_get_drvdata(pdev);
  238. struct resource *res;
  239. /* disable i2c logic */
  240. oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
  241. & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  242. /* remove adapter & data */
  243. i2c_del_adapter(&i2c->adap);
  244. platform_set_drvdata(pdev, NULL);
  245. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  246. if (res)
  247. free_irq(res->start, i2c);
  248. iounmap(i2c->base);
  249. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  250. if (res)
  251. release_mem_region(res->start, res->end - res->start + 1);
  252. kfree(i2c);
  253. return 0;
  254. }
  255. static struct platform_driver ocores_i2c_driver = {
  256. .probe = ocores_i2c_probe,
  257. .remove = __devexit_p(ocores_i2c_remove),
  258. .driver = {
  259. .owner = THIS_MODULE,
  260. .name = "ocores-i2c",
  261. },
  262. };
  263. static int __init ocores_i2c_init(void)
  264. {
  265. return platform_driver_register(&ocores_i2c_driver);
  266. }
  267. static void __exit ocores_i2c_exit(void)
  268. {
  269. platform_driver_unregister(&ocores_i2c_driver);
  270. }
  271. module_init(ocores_i2c_init);
  272. module_exit(ocores_i2c_exit);
  273. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  274. MODULE_DESCRIPTION("OpenCores I2C bus driver");
  275. MODULE_LICENSE("GPL");