i2c-xlr.c 6.8 KB

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