i2c-xlr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2011, Netlogic Microsystems Inc.
  3. * Copyright 2004, Matt Porter <mporter@kernel.crashing.org>
  4. *
  5. * This file is licensed under the terms of the GNU General Public
  6. * License version 2. This program is licensed "as is" without any
  7. * warranty of any kind, whether express or implied.
  8. */
  9. #include <linux/err.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/ioport.h>
  14. #include <linux/delay.h>
  15. #include <linux/errno.h>
  16. #include <linux/i2c.h>
  17. #include <linux/io.h>
  18. #include <linux/platform_device.h>
  19. /* XLR I2C REGISTERS */
  20. #define XLR_I2C_CFG 0x00
  21. #define XLR_I2C_CLKDIV 0x01
  22. #define XLR_I2C_DEVADDR 0x02
  23. #define XLR_I2C_ADDR 0x03
  24. #define XLR_I2C_DATAOUT 0x04
  25. #define XLR_I2C_DATAIN 0x05
  26. #define XLR_I2C_STATUS 0x06
  27. #define XLR_I2C_STARTXFR 0x07
  28. #define XLR_I2C_BYTECNT 0x08
  29. #define XLR_I2C_HDSTATIM 0x09
  30. /* XLR I2C REGISTERS FLAGS */
  31. #define XLR_I2C_BUS_BUSY 0x01
  32. #define XLR_I2C_SDOEMPTY 0x02
  33. #define XLR_I2C_RXRDY 0x04
  34. #define XLR_I2C_ACK_ERR 0x08
  35. #define XLR_I2C_ARB_STARTERR 0x30
  36. /* Register Values */
  37. #define XLR_I2C_CFG_ADDR 0xF8
  38. #define XLR_I2C_CFG_NOADDR 0xFA
  39. #define XLR_I2C_STARTXFR_ND 0x02 /* No Data */
  40. #define XLR_I2C_STARTXFR_RD 0x01 /* Read */
  41. #define XLR_I2C_STARTXFR_WR 0x00 /* Write */
  42. #define XLR_I2C_TIMEOUT 10 /* timeout per byte in msec */
  43. /*
  44. * On XLR/XLS, we need to use __raw_ IO to read the I2C registers
  45. * because they are in the big-endian MMIO area on the SoC.
  46. *
  47. * The readl/writel implementation on XLR/XLS byteswaps, because
  48. * those are for its little-endian PCI space (see arch/mips/Kconfig).
  49. */
  50. static inline void xlr_i2c_wreg(u32 __iomem *base, unsigned int reg, u32 val)
  51. {
  52. __raw_writel(val, base + reg);
  53. }
  54. static inline u32 xlr_i2c_rdreg(u32 __iomem *base, unsigned int reg)
  55. {
  56. return __raw_readl(base + reg);
  57. }
  58. struct xlr_i2c_private {
  59. struct i2c_adapter adap;
  60. u32 __iomem *iobase;
  61. };
  62. static int xlr_i2c_tx(struct xlr_i2c_private *priv, u16 len,
  63. u8 *buf, u16 addr)
  64. {
  65. struct i2c_adapter *adap = &priv->adap;
  66. unsigned long timeout, stoptime, checktime;
  67. u32 i2c_status;
  68. int pos, timedout;
  69. u8 offset, byte;
  70. offset = buf[0];
  71. xlr_i2c_wreg(priv->iobase, XLR_I2C_ADDR, offset);
  72. xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr);
  73. xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG, XLR_I2C_CFG_ADDR);
  74. xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 1);
  75. timeout = msecs_to_jiffies(XLR_I2C_TIMEOUT);
  76. stoptime = jiffies + timeout;
  77. timedout = 0;
  78. pos = 1;
  79. retry:
  80. if (len == 1) {
  81. xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR,
  82. XLR_I2C_STARTXFR_ND);
  83. } else {
  84. xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT, buf[pos]);
  85. xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR,
  86. XLR_I2C_STARTXFR_WR);
  87. }
  88. while (!timedout) {
  89. checktime = jiffies;
  90. i2c_status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS);
  91. if (i2c_status & XLR_I2C_SDOEMPTY) {
  92. pos++;
  93. /* need to do a empty dataout after the last byte */
  94. byte = (pos < len) ? buf[pos] : 0;
  95. xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT, byte);
  96. /* reset timeout on successful xmit */
  97. stoptime = jiffies + timeout;
  98. }
  99. timedout = time_after(checktime, stoptime);
  100. if (i2c_status & XLR_I2C_ARB_STARTERR) {
  101. if (timedout)
  102. break;
  103. goto retry;
  104. }
  105. if (i2c_status & XLR_I2C_ACK_ERR)
  106. return -EIO;
  107. if ((i2c_status & XLR_I2C_BUS_BUSY) == 0 && pos >= len)
  108. return 0;
  109. }
  110. dev_err(&adap->dev, "I2C transmit timeout\n");
  111. return -ETIMEDOUT;
  112. }
  113. static int xlr_i2c_rx(struct xlr_i2c_private *priv, u16 len, u8 *buf, u16 addr)
  114. {
  115. struct i2c_adapter *adap = &priv->adap;
  116. u32 i2c_status;
  117. unsigned long timeout, stoptime, checktime;
  118. int nbytes, timedout;
  119. u8 byte;
  120. xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG, XLR_I2C_CFG_NOADDR);
  121. xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len);
  122. xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr);
  123. timeout = msecs_to_jiffies(XLR_I2C_TIMEOUT);
  124. stoptime = jiffies + timeout;
  125. timedout = 0;
  126. nbytes = 0;
  127. retry:
  128. xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR, XLR_I2C_STARTXFR_RD);
  129. while (!timedout) {
  130. checktime = jiffies;
  131. i2c_status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS);
  132. if (i2c_status & XLR_I2C_RXRDY) {
  133. if (nbytes > len)
  134. return -EIO; /* should not happen */
  135. /* we need to do a dummy datain when nbytes == len */
  136. byte = xlr_i2c_rdreg(priv->iobase, XLR_I2C_DATAIN);
  137. if (nbytes < len)
  138. buf[nbytes] = byte;
  139. nbytes++;
  140. /* reset timeout on successful read */
  141. stoptime = jiffies + timeout;
  142. }
  143. timedout = time_after(checktime, stoptime);
  144. if (i2c_status & XLR_I2C_ARB_STARTERR) {
  145. if (timedout)
  146. break;
  147. goto retry;
  148. }
  149. if (i2c_status & XLR_I2C_ACK_ERR)
  150. return -EIO;
  151. if ((i2c_status & XLR_I2C_BUS_BUSY) == 0)
  152. return 0;
  153. }
  154. dev_err(&adap->dev, "I2C receive timeout\n");
  155. return -ETIMEDOUT;
  156. }
  157. static int xlr_i2c_xfer(struct i2c_adapter *adap,
  158. struct i2c_msg *msgs, int num)
  159. {
  160. struct i2c_msg *msg;
  161. int i;
  162. int ret = 0;
  163. struct xlr_i2c_private *priv = i2c_get_adapdata(adap);
  164. for (i = 0; ret == 0 && i < num; i++) {
  165. msg = &msgs[i];
  166. if (msg->flags & I2C_M_RD)
  167. ret = xlr_i2c_rx(priv, msg->len, &msg->buf[0],
  168. msg->addr);
  169. else
  170. ret = xlr_i2c_tx(priv, msg->len, &msg->buf[0],
  171. msg->addr);
  172. }
  173. return (ret != 0) ? ret : num;
  174. }
  175. static u32 xlr_func(struct i2c_adapter *adap)
  176. {
  177. /* Emulate SMBUS over I2C */
  178. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  179. }
  180. static struct i2c_algorithm xlr_i2c_algo = {
  181. .master_xfer = xlr_i2c_xfer,
  182. .functionality = xlr_func,
  183. };
  184. static int xlr_i2c_probe(struct platform_device *pdev)
  185. {
  186. struct xlr_i2c_private *priv;
  187. struct resource *res;
  188. int ret;
  189. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  190. if (!priv)
  191. return -ENOMEM;
  192. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  193. priv->iobase = devm_ioremap_resource(&pdev->dev, res);
  194. if (IS_ERR(priv->iobase))
  195. return PTR_ERR(priv->iobase);
  196. priv->adap.dev.parent = &pdev->dev;
  197. priv->adap.owner = THIS_MODULE;
  198. priv->adap.algo_data = priv;
  199. priv->adap.algo = &xlr_i2c_algo;
  200. priv->adap.nr = pdev->id;
  201. priv->adap.class = I2C_CLASS_HWMON;
  202. snprintf(priv->adap.name, sizeof(priv->adap.name), "xlr-i2c");
  203. i2c_set_adapdata(&priv->adap, priv);
  204. ret = i2c_add_numbered_adapter(&priv->adap);
  205. if (ret < 0) {
  206. dev_err(&priv->adap.dev, "Failed to add i2c bus.\n");
  207. return ret;
  208. }
  209. platform_set_drvdata(pdev, priv);
  210. dev_info(&priv->adap.dev, "Added I2C Bus.\n");
  211. return 0;
  212. }
  213. static int xlr_i2c_remove(struct platform_device *pdev)
  214. {
  215. struct xlr_i2c_private *priv;
  216. priv = platform_get_drvdata(pdev);
  217. i2c_del_adapter(&priv->adap);
  218. return 0;
  219. }
  220. static struct platform_driver xlr_i2c_driver = {
  221. .probe = xlr_i2c_probe,
  222. .remove = xlr_i2c_remove,
  223. .driver = {
  224. .name = "xlr-i2cbus",
  225. },
  226. };
  227. module_platform_driver(xlr_i2c_driver);
  228. MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@netlogicmicro.com>");
  229. MODULE_DESCRIPTION("XLR/XLS SoC I2C Controller driver");
  230. MODULE_LICENSE("GPL v2");
  231. MODULE_ALIAS("platform:xlr-i2cbus");