i2c-puv3.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * I2C driver for PKUnity-v3 SoC
  3. * Code specific to PKUnity SoC and UniCore ISA
  4. *
  5. * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
  6. * Copyright (C) 2001-2010 Guan Xuetao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  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/clk.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #include <mach/hardware.h>
  23. /*
  24. * Poll the i2c status register until the specified bit is set.
  25. * Returns 0 if timed out (100 msec).
  26. */
  27. static short poll_status(unsigned long bit)
  28. {
  29. int loop_cntr = 1000;
  30. if (bit & I2C_STATUS_TFNF) {
  31. do {
  32. udelay(10);
  33. } while (!(readl(I2C_STATUS) & bit) && (--loop_cntr > 0));
  34. } else {
  35. /* RXRDY handler */
  36. do {
  37. if (readl(I2C_TAR) == I2C_TAR_EEPROM)
  38. msleep(20);
  39. else
  40. udelay(10);
  41. } while (!(readl(I2C_RXFLR) & 0xf) && (--loop_cntr > 0));
  42. }
  43. return (loop_cntr > 0);
  44. }
  45. static int xfer_read(struct i2c_adapter *adap, unsigned char *buf, int length)
  46. {
  47. int i2c_reg = *buf;
  48. /* Read data */
  49. while (length--) {
  50. if (!poll_status(I2C_STATUS_TFNF)) {
  51. dev_dbg(&adap->dev, "Tx FIFO Not Full timeout\n");
  52. return -ETIMEDOUT;
  53. }
  54. /* send addr */
  55. writel(i2c_reg | I2C_DATACMD_WRITE, I2C_DATACMD);
  56. /* get ready to next write */
  57. i2c_reg++;
  58. /* send read CMD */
  59. writel(I2C_DATACMD_READ, I2C_DATACMD);
  60. /* wait until the Rx FIFO have available */
  61. if (!poll_status(I2C_STATUS_RFNE)) {
  62. dev_dbg(&adap->dev, "RXRDY timeout\n");
  63. return -ETIMEDOUT;
  64. }
  65. /* read the data to buf */
  66. *buf = (readl(I2C_DATACMD) & I2C_DATACMD_DAT_MASK);
  67. buf++;
  68. }
  69. return 0;
  70. }
  71. static int xfer_write(struct i2c_adapter *adap, unsigned char *buf, int length)
  72. {
  73. int i2c_reg = *buf;
  74. /* Do nothing but storing the reg_num to a static variable */
  75. if (i2c_reg == -1) {
  76. printk(KERN_WARNING "Error i2c reg\n");
  77. return -ETIMEDOUT;
  78. }
  79. if (length == 1)
  80. return 0;
  81. buf++;
  82. length--;
  83. while (length--) {
  84. /* send addr */
  85. writel(i2c_reg | I2C_DATACMD_WRITE, I2C_DATACMD);
  86. /* send write CMD */
  87. writel(*buf | I2C_DATACMD_WRITE, I2C_DATACMD);
  88. /* wait until the Rx FIFO have available */
  89. msleep(20);
  90. /* read the data to buf */
  91. i2c_reg++;
  92. buf++;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Generic i2c master transfer entrypoint.
  98. *
  99. */
  100. static int puv3_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *pmsg,
  101. int num)
  102. {
  103. int i, ret;
  104. unsigned char swap;
  105. /* Disable i2c */
  106. writel(I2C_ENABLE_DISABLE, I2C_ENABLE);
  107. /* Set the work mode and speed*/
  108. writel(I2C_CON_MASTER | I2C_CON_SPEED_STD | I2C_CON_SLAVEDISABLE, I2C_CON);
  109. writel(pmsg->addr, I2C_TAR);
  110. /* Enable i2c */
  111. writel(I2C_ENABLE_ENABLE, I2C_ENABLE);
  112. dev_dbg(&adap->dev, "puv3_i2c_xfer: processing %d messages:\n", num);
  113. for (i = 0; i < num; i++) {
  114. dev_dbg(&adap->dev, " #%d: %sing %d byte%s %s 0x%02x\n", i,
  115. pmsg->flags & I2C_M_RD ? "read" : "writ",
  116. pmsg->len, pmsg->len > 1 ? "s" : "",
  117. pmsg->flags & I2C_M_RD ? "from" : "to", pmsg->addr);
  118. if (pmsg->len && pmsg->buf) { /* sanity check */
  119. if (pmsg->flags & I2C_M_RD)
  120. ret = xfer_read(adap, pmsg->buf, pmsg->len);
  121. else
  122. ret = xfer_write(adap, pmsg->buf, pmsg->len);
  123. if (ret)
  124. return ret;
  125. }
  126. dev_dbg(&adap->dev, "transfer complete\n");
  127. pmsg++; /* next message */
  128. }
  129. /* XXX: fixup be16_to_cpu in bq27x00_battery.c */
  130. if (pmsg->addr == I2C_TAR_PWIC) {
  131. swap = pmsg->buf[0];
  132. pmsg->buf[0] = pmsg->buf[1];
  133. pmsg->buf[1] = swap;
  134. }
  135. return i;
  136. }
  137. /*
  138. * Return list of supported functionality.
  139. */
  140. static u32 puv3_i2c_func(struct i2c_adapter *adapter)
  141. {
  142. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  143. }
  144. static struct i2c_algorithm puv3_i2c_algorithm = {
  145. .master_xfer = puv3_i2c_xfer,
  146. .functionality = puv3_i2c_func,
  147. };
  148. /*
  149. * Main initialization routine.
  150. */
  151. static int puv3_i2c_probe(struct platform_device *pdev)
  152. {
  153. struct i2c_adapter *adapter;
  154. struct resource *mem;
  155. int rc;
  156. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  157. if (!mem)
  158. return -ENODEV;
  159. if (!request_mem_region(mem->start, resource_size(mem), "puv3_i2c"))
  160. return -EBUSY;
  161. adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  162. if (adapter == NULL) {
  163. dev_err(&pdev->dev, "can't allocate interface!\n");
  164. rc = -ENOMEM;
  165. goto fail_nomem;
  166. }
  167. snprintf(adapter->name, sizeof(adapter->name), "PUV3-I2C at 0x%08x",
  168. mem->start);
  169. adapter->algo = &puv3_i2c_algorithm;
  170. adapter->class = I2C_CLASS_HWMON;
  171. adapter->dev.parent = &pdev->dev;
  172. platform_set_drvdata(pdev, adapter);
  173. adapter->nr = pdev->id;
  174. rc = i2c_add_numbered_adapter(adapter);
  175. if (rc) {
  176. dev_err(&pdev->dev, "Adapter '%s' registration failed\n",
  177. adapter->name);
  178. goto fail_add_adapter;
  179. }
  180. dev_info(&pdev->dev, "PKUnity v3 i2c bus adapter.\n");
  181. return 0;
  182. fail_add_adapter:
  183. kfree(adapter);
  184. fail_nomem:
  185. release_mem_region(mem->start, resource_size(mem));
  186. return rc;
  187. }
  188. static int puv3_i2c_remove(struct platform_device *pdev)
  189. {
  190. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  191. struct resource *mem;
  192. i2c_del_adapter(adapter);
  193. put_device(&pdev->dev);
  194. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  195. release_mem_region(mem->start, resource_size(mem));
  196. return 0;
  197. }
  198. #ifdef CONFIG_PM_SLEEP
  199. static int puv3_i2c_suspend(struct device *dev)
  200. {
  201. int poll_count;
  202. /* Disable the IIC */
  203. writel(I2C_ENABLE_DISABLE, I2C_ENABLE);
  204. for (poll_count = 0; poll_count < 50; poll_count++) {
  205. if (readl(I2C_ENSTATUS) & I2C_ENSTATUS_ENABLE)
  206. udelay(25);
  207. }
  208. return 0;
  209. }
  210. static SIMPLE_DEV_PM_OPS(puv3_i2c_pm, puv3_i2c_suspend, NULL);
  211. #define PUV3_I2C_PM (&puv3_i2c_pm)
  212. #else
  213. #define PUV3_I2C_PM NULL
  214. #endif
  215. static struct platform_driver puv3_i2c_driver = {
  216. .probe = puv3_i2c_probe,
  217. .remove = puv3_i2c_remove,
  218. .driver = {
  219. .name = "PKUnity-v3-I2C",
  220. .pm = PUV3_I2C_PM,
  221. }
  222. };
  223. module_platform_driver(puv3_i2c_driver);
  224. MODULE_DESCRIPTION("PKUnity v3 I2C driver");
  225. MODULE_LICENSE("GPL v2");
  226. MODULE_ALIAS("platform:puv3_i2c");