i2c-at91.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
  3. Copyright (C) 2004 Rick Bronson
  4. Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
  5. Borrowed heavily from original work by:
  6. Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  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, or
  10. (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/version.h>
  14. #include <linux/kernel.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/delay.h>
  19. #include <linux/i2c.h>
  20. #include <linux/init.h>
  21. #include <linux/clk.h>
  22. #include <linux/platform_device.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/at91_twi.h>
  25. #include <asm/arch/board.h>
  26. #include <asm/arch/cpu.h>
  27. #define TWI_CLOCK 100000 /* Hz. max 400 Kbits/sec */
  28. static struct clk *twi_clk;
  29. static void __iomem *twi_base;
  30. #define at91_twi_read(reg) __raw_readl(twi_base + (reg))
  31. #define at91_twi_write(reg, val) __raw_writel((val), twi_base + (reg))
  32. /*
  33. * Initialize the TWI hardware registers.
  34. */
  35. static void __devinit at91_twi_hwinit(void)
  36. {
  37. unsigned long cdiv, ckdiv;
  38. at91_twi_write(AT91_TWI_IDR, 0xffffffff); /* Disable all interrupts */
  39. at91_twi_write(AT91_TWI_CR, AT91_TWI_SWRST); /* Reset peripheral */
  40. at91_twi_write(AT91_TWI_CR, AT91_TWI_MSEN); /* Set Master mode */
  41. /* Calcuate clock dividers */
  42. cdiv = (clk_get_rate(twi_clk) / (2 * TWI_CLOCK)) - 3;
  43. cdiv = cdiv + 1; /* round up */
  44. ckdiv = 0;
  45. while (cdiv > 255) {
  46. ckdiv++;
  47. cdiv = cdiv >> 1;
  48. }
  49. if (cpu_is_at91rm9200()) { /* AT91RM9200 Errata #22 */
  50. if (ckdiv > 5) {
  51. printk(KERN_ERR "AT91 I2C: Invalid TWI_CLOCK value!\n");
  52. ckdiv = 5;
  53. }
  54. }
  55. at91_twi_write(AT91_TWI_CWGR, (ckdiv << 16) | (cdiv << 8) | cdiv);
  56. }
  57. /*
  58. * Poll the i2c status register until the specified bit is set.
  59. * Returns 0 if timed out (100 msec).
  60. */
  61. static short at91_poll_status(unsigned long bit)
  62. {
  63. int loop_cntr = 10000;
  64. do {
  65. udelay(10);
  66. } while (!(at91_twi_read(AT91_TWI_SR) & bit) && (--loop_cntr > 0));
  67. return (loop_cntr > 0);
  68. }
  69. static int xfer_read(struct i2c_adapter *adap, unsigned char *buf, int length)
  70. {
  71. /* Send Start */
  72. at91_twi_write(AT91_TWI_CR, AT91_TWI_START);
  73. /* Read data */
  74. while (length--) {
  75. if (!length) /* need to send Stop before reading last byte */
  76. at91_twi_write(AT91_TWI_CR, AT91_TWI_STOP);
  77. if (!at91_poll_status(AT91_TWI_RXRDY)) {
  78. dev_dbg(&adap->dev, "RXRDY timeout\n");
  79. return -ETIMEDOUT;
  80. }
  81. *buf++ = (at91_twi_read(AT91_TWI_RHR) & 0xff);
  82. }
  83. return 0;
  84. }
  85. static int xfer_write(struct i2c_adapter *adap, unsigned char *buf, int length)
  86. {
  87. /* Load first byte into transmitter */
  88. at91_twi_write(AT91_TWI_THR, *buf++);
  89. /* Send Start */
  90. at91_twi_write(AT91_TWI_CR, AT91_TWI_START);
  91. do {
  92. if (!at91_poll_status(AT91_TWI_TXRDY)) {
  93. dev_dbg(&adap->dev, "TXRDY timeout\n");
  94. return -ETIMEDOUT;
  95. }
  96. length--; /* byte was transmitted */
  97. if (length > 0) /* more data to send? */
  98. at91_twi_write(AT91_TWI_THR, *buf++);
  99. } while (length);
  100. /* Send Stop */
  101. at91_twi_write(AT91_TWI_CR, AT91_TWI_STOP);
  102. return 0;
  103. }
  104. /*
  105. * Generic i2c master transfer entrypoint.
  106. *
  107. * Note: We do not use Atmel's feature of storing the "internal device address".
  108. * Instead the "internal device address" has to be written using a seperate
  109. * i2c message.
  110. * http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2004-September/024411.html
  111. */
  112. static int at91_xfer(struct i2c_adapter *adap, struct i2c_msg *pmsg, int num)
  113. {
  114. int i, ret;
  115. dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
  116. for (i = 0; i < num; i++) {
  117. dev_dbg(&adap->dev, " #%d: %sing %d byte%s %s 0x%02x\n", i,
  118. pmsg->flags & I2C_M_RD ? "read" : "writ",
  119. pmsg->len, pmsg->len > 1 ? "s" : "",
  120. pmsg->flags & I2C_M_RD ? "from" : "to", pmsg->addr);
  121. at91_twi_write(AT91_TWI_MMR, (pmsg->addr << 16)
  122. | ((pmsg->flags & I2C_M_RD) ? AT91_TWI_MREAD : 0));
  123. if (pmsg->len && pmsg->buf) { /* sanity check */
  124. if (pmsg->flags & I2C_M_RD)
  125. ret = xfer_read(adap, pmsg->buf, pmsg->len);
  126. else
  127. ret = xfer_write(adap, pmsg->buf, pmsg->len);
  128. if (ret)
  129. return ret;
  130. /* Wait until transfer is finished */
  131. if (!at91_poll_status(AT91_TWI_TXCOMP)) {
  132. dev_dbg(&adap->dev, "TXCOMP timeout\n");
  133. return -ETIMEDOUT;
  134. }
  135. }
  136. dev_dbg(&adap->dev, "transfer complete\n");
  137. pmsg++; /* next message */
  138. }
  139. return i;
  140. }
  141. /*
  142. * Return list of supported functionality.
  143. */
  144. static u32 at91_func(struct i2c_adapter *adapter)
  145. {
  146. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  147. }
  148. static struct i2c_algorithm at91_algorithm = {
  149. .master_xfer = at91_xfer,
  150. .functionality = at91_func,
  151. };
  152. /*
  153. * Main initialization routine.
  154. */
  155. static int __devinit at91_i2c_probe(struct platform_device *pdev)
  156. {
  157. struct i2c_adapter *adapter;
  158. struct resource *res;
  159. int rc;
  160. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. if (!res)
  162. return -ENXIO;
  163. if (!request_mem_region(res->start, res->end - res->start + 1, "at91_i2c"))
  164. return -EBUSY;
  165. twi_base = ioremap(res->start, res->end - res->start + 1);
  166. if (!twi_base) {
  167. rc = -ENOMEM;
  168. goto fail0;
  169. }
  170. twi_clk = clk_get(NULL, "twi_clk");
  171. if (IS_ERR(twi_clk)) {
  172. dev_err(&pdev->dev, "no clock defined\n");
  173. rc = -ENODEV;
  174. goto fail1;
  175. }
  176. adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  177. if (adapter == NULL) {
  178. dev_err(&pdev->dev, "can't allocate inteface!\n");
  179. rc = -ENOMEM;
  180. goto fail2;
  181. }
  182. sprintf(adapter->name, "AT91");
  183. adapter->algo = &at91_algorithm;
  184. adapter->class = I2C_CLASS_HWMON;
  185. adapter->dev.parent = &pdev->dev;
  186. /* adapter->id == 0 ... only one TWI controller for now */
  187. platform_set_drvdata(pdev, adapter);
  188. clk_enable(twi_clk); /* enable peripheral clock */
  189. at91_twi_hwinit(); /* initialize TWI controller */
  190. rc = i2c_add_numbered_adapter(adapter);
  191. if (rc) {
  192. dev_err(&pdev->dev, "Adapter %s registration failed\n",
  193. adapter->name);
  194. goto fail3;
  195. }
  196. dev_info(&pdev->dev, "AT91 i2c bus driver.\n");
  197. return 0;
  198. fail3:
  199. platform_set_drvdata(pdev, NULL);
  200. kfree(adapter);
  201. clk_disable(twi_clk);
  202. fail2:
  203. clk_put(twi_clk);
  204. fail1:
  205. iounmap(twi_base);
  206. fail0:
  207. release_mem_region(res->start, res->end - res->start + 1);
  208. return rc;
  209. }
  210. static int __devexit at91_i2c_remove(struct platform_device *pdev)
  211. {
  212. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  213. struct resource *res;
  214. int rc;
  215. rc = i2c_del_adapter(adapter);
  216. platform_set_drvdata(pdev, NULL);
  217. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  218. iounmap(twi_base);
  219. release_mem_region(res->start, res->end - res->start + 1);
  220. clk_disable(twi_clk); /* disable peripheral clock */
  221. clk_put(twi_clk);
  222. return rc;
  223. }
  224. #ifdef CONFIG_PM
  225. /* NOTE: could save a few mA by keeping clock off outside of at91_xfer... */
  226. static int at91_i2c_suspend(struct platform_device *pdev, pm_message_t mesg)
  227. {
  228. clk_disable(twi_clk);
  229. return 0;
  230. }
  231. static int at91_i2c_resume(struct platform_device *pdev)
  232. {
  233. return clk_enable(twi_clk);
  234. }
  235. #else
  236. #define at91_i2c_suspend NULL
  237. #define at91_i2c_resume NULL
  238. #endif
  239. /* work with "modprobe at91_i2c" from hotplugging or coldplugging */
  240. MODULE_ALIAS("at91_i2c");
  241. static struct platform_driver at91_i2c_driver = {
  242. .probe = at91_i2c_probe,
  243. .remove = __devexit_p(at91_i2c_remove),
  244. .suspend = at91_i2c_suspend,
  245. .resume = at91_i2c_resume,
  246. .driver = {
  247. .name = "at91_i2c",
  248. .owner = THIS_MODULE,
  249. },
  250. };
  251. static int __init at91_i2c_init(void)
  252. {
  253. return platform_driver_register(&at91_i2c_driver);
  254. }
  255. static void __exit at91_i2c_exit(void)
  256. {
  257. platform_driver_unregister(&at91_i2c_driver);
  258. }
  259. module_init(at91_i2c_init);
  260. module_exit(at91_i2c_exit);
  261. MODULE_AUTHOR("Rick Bronson");
  262. MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
  263. MODULE_LICENSE("GPL");