i2c-at91.c 7.4 KB

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