i2c-ocores.c 7.8 KB

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