i2c-xlr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. #include <linux/of_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/wait.h>
  23. /* XLR I2C REGISTERS */
  24. #define XLR_I2C_CFG 0x00
  25. #define XLR_I2C_CLKDIV 0x01
  26. #define XLR_I2C_DEVADDR 0x02
  27. #define XLR_I2C_ADDR 0x03
  28. #define XLR_I2C_DATAOUT 0x04
  29. #define XLR_I2C_DATAIN 0x05
  30. #define XLR_I2C_STATUS 0x06
  31. #define XLR_I2C_STARTXFR 0x07
  32. #define XLR_I2C_BYTECNT 0x08
  33. #define XLR_I2C_HDSTATIM 0x09
  34. /* Sigma Designs additional registers */
  35. #define XLR_I2C_INT_EN 0x09
  36. #define XLR_I2C_INT_STAT 0x0a
  37. /* XLR I2C REGISTERS FLAGS */
  38. #define XLR_I2C_BUS_BUSY 0x01
  39. #define XLR_I2C_SDOEMPTY 0x02
  40. #define XLR_I2C_RXRDY 0x04
  41. #define XLR_I2C_ACK_ERR 0x08
  42. #define XLR_I2C_ARB_STARTERR 0x30
  43. /* Register Values */
  44. #define XLR_I2C_CFG_ADDR 0xF8
  45. #define XLR_I2C_CFG_NOADDR 0xFA
  46. #define XLR_I2C_STARTXFR_ND 0x02 /* No Data */
  47. #define XLR_I2C_STARTXFR_RD 0x01 /* Read */
  48. #define XLR_I2C_STARTXFR_WR 0x00 /* Write */
  49. #define XLR_I2C_TIMEOUT 10 /* timeout per byte in msec */
  50. /*
  51. * On XLR/XLS, we need to use __raw_ IO to read the I2C registers
  52. * because they are in the big-endian MMIO area on the SoC.
  53. *
  54. * The readl/writel implementation on XLR/XLS byteswaps, because
  55. * those are for its little-endian PCI space (see arch/mips/Kconfig).
  56. */
  57. static inline void xlr_i2c_wreg(u32 __iomem *base, unsigned int reg, u32 val)
  58. {
  59. __raw_writel(val, base + reg);
  60. }
  61. static inline u32 xlr_i2c_rdreg(u32 __iomem *base, unsigned int reg)
  62. {
  63. return __raw_readl(base + reg);
  64. }
  65. #define XLR_I2C_FLAG_IRQ 1
  66. struct xlr_i2c_config {
  67. u32 flags; /* optional feature support */
  68. u32 status_busy; /* value of STATUS[0] when busy */
  69. u32 cfg_extra; /* extra CFG bits to set */
  70. };
  71. struct xlr_i2c_private {
  72. struct i2c_adapter adap;
  73. u32 __iomem *iobase;
  74. int irq;
  75. int pos;
  76. struct i2c_msg *msg;
  77. const struct xlr_i2c_config *cfg;
  78. wait_queue_head_t wait;
  79. struct clk *clk;
  80. };
  81. static int xlr_i2c_busy(struct xlr_i2c_private *priv, u32 status)
  82. {
  83. return (status & XLR_I2C_BUS_BUSY) == priv->cfg->status_busy;
  84. }
  85. static int xlr_i2c_idle(struct xlr_i2c_private *priv)
  86. {
  87. return !xlr_i2c_busy(priv, xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS));
  88. }
  89. static int xlr_i2c_wait(struct xlr_i2c_private *priv, unsigned long timeout)
  90. {
  91. int status;
  92. int t;
  93. t = wait_event_timeout(priv->wait, xlr_i2c_idle(priv),
  94. msecs_to_jiffies(timeout));
  95. if (!t)
  96. return -ETIMEDOUT;
  97. status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS);
  98. return status & XLR_I2C_ACK_ERR ? -EIO : 0;
  99. }
  100. static void xlr_i2c_tx_irq(struct xlr_i2c_private *priv, u32 status)
  101. {
  102. struct i2c_msg *msg = priv->msg;
  103. if (status & XLR_I2C_SDOEMPTY)
  104. xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT,
  105. msg->buf[priv->pos++]);
  106. }
  107. static void xlr_i2c_rx_irq(struct xlr_i2c_private *priv, u32 status)
  108. {
  109. struct i2c_msg *msg = priv->msg;
  110. if (status & XLR_I2C_RXRDY)
  111. msg->buf[priv->pos++] =
  112. xlr_i2c_rdreg(priv->iobase, XLR_I2C_DATAIN);
  113. }
  114. static irqreturn_t xlr_i2c_irq(int irq, void *dev_id)
  115. {
  116. struct xlr_i2c_private *priv = dev_id;
  117. struct i2c_msg *msg = priv->msg;
  118. u32 int_stat, status;
  119. int_stat = xlr_i2c_rdreg(priv->iobase, XLR_I2C_INT_STAT);
  120. if (!int_stat)
  121. return IRQ_NONE;
  122. xlr_i2c_wreg(priv->iobase, XLR_I2C_INT_STAT, int_stat);
  123. if (!msg)
  124. return IRQ_HANDLED;
  125. status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS);
  126. if (priv->pos < msg->len) {
  127. if (msg->flags & I2C_M_RD)
  128. xlr_i2c_rx_irq(priv, status);
  129. else
  130. xlr_i2c_tx_irq(priv, status);
  131. }
  132. if (!xlr_i2c_busy(priv, status))
  133. wake_up(&priv->wait);
  134. return IRQ_HANDLED;
  135. }
  136. static int xlr_i2c_tx(struct xlr_i2c_private *priv, u16 len,
  137. u8 *buf, u16 addr)
  138. {
  139. struct i2c_adapter *adap = &priv->adap;
  140. unsigned long timeout, stoptime, checktime;
  141. u32 i2c_status;
  142. int pos, timedout;
  143. u8 offset;
  144. u32 xfer;
  145. if (!len)
  146. return -EOPNOTSUPP;
  147. offset = buf[0];
  148. xlr_i2c_wreg(priv->iobase, XLR_I2C_ADDR, offset);
  149. xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr);
  150. xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG,
  151. XLR_I2C_CFG_ADDR | priv->cfg->cfg_extra);
  152. timeout = msecs_to_jiffies(XLR_I2C_TIMEOUT);
  153. stoptime = jiffies + timeout;
  154. timedout = 0;
  155. if (len == 1) {
  156. xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 1);
  157. xfer = XLR_I2C_STARTXFR_ND;
  158. pos = 1;
  159. } else {
  160. xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 2);
  161. xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT, buf[1]);
  162. xfer = XLR_I2C_STARTXFR_WR;
  163. pos = 2;
  164. }
  165. priv->pos = pos;
  166. retry:
  167. /* retry can only happen on the first byte */
  168. xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR, xfer);
  169. if (priv->irq > 0)
  170. return xlr_i2c_wait(priv, XLR_I2C_TIMEOUT * len);
  171. while (!timedout) {
  172. checktime = jiffies;
  173. i2c_status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS);
  174. if ((i2c_status & XLR_I2C_SDOEMPTY) && pos < len) {
  175. xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT, buf[pos++]);
  176. /* reset timeout on successful xmit */
  177. stoptime = jiffies + timeout;
  178. }
  179. timedout = time_after(checktime, stoptime);
  180. if (i2c_status & XLR_I2C_ARB_STARTERR) {
  181. if (timedout)
  182. break;
  183. goto retry;
  184. }
  185. if (i2c_status & XLR_I2C_ACK_ERR)
  186. return -EIO;
  187. if (!xlr_i2c_busy(priv, i2c_status) && pos >= len)
  188. return 0;
  189. }
  190. dev_err(&adap->dev, "I2C transmit timeout\n");
  191. return -ETIMEDOUT;
  192. }
  193. static int xlr_i2c_rx(struct xlr_i2c_private *priv, u16 len, u8 *buf, u16 addr)
  194. {
  195. struct i2c_adapter *adap = &priv->adap;
  196. u32 i2c_status;
  197. unsigned long timeout, stoptime, checktime;
  198. int nbytes, timedout;
  199. if (!len)
  200. return -EOPNOTSUPP;
  201. xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG,
  202. XLR_I2C_CFG_NOADDR | priv->cfg->cfg_extra);
  203. xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 1);
  204. xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr);
  205. priv->pos = 0;
  206. timeout = msecs_to_jiffies(XLR_I2C_TIMEOUT);
  207. stoptime = jiffies + timeout;
  208. timedout = 0;
  209. nbytes = 0;
  210. retry:
  211. xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR, XLR_I2C_STARTXFR_RD);
  212. if (priv->irq > 0)
  213. return xlr_i2c_wait(priv, XLR_I2C_TIMEOUT * len);
  214. while (!timedout) {
  215. checktime = jiffies;
  216. i2c_status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS);
  217. if (i2c_status & XLR_I2C_RXRDY) {
  218. if (nbytes >= len)
  219. return -EIO; /* should not happen */
  220. buf[nbytes++] =
  221. xlr_i2c_rdreg(priv->iobase, XLR_I2C_DATAIN);
  222. /* reset timeout on successful read */
  223. stoptime = jiffies + timeout;
  224. }
  225. timedout = time_after(checktime, stoptime);
  226. if (i2c_status & XLR_I2C_ARB_STARTERR) {
  227. if (timedout)
  228. break;
  229. goto retry;
  230. }
  231. if (i2c_status & XLR_I2C_ACK_ERR)
  232. return -EIO;
  233. if (!xlr_i2c_busy(priv, i2c_status))
  234. return 0;
  235. }
  236. dev_err(&adap->dev, "I2C receive timeout\n");
  237. return -ETIMEDOUT;
  238. }
  239. static int xlr_i2c_xfer(struct i2c_adapter *adap,
  240. struct i2c_msg *msgs, int num)
  241. {
  242. struct i2c_msg *msg;
  243. int i;
  244. int ret = 0;
  245. struct xlr_i2c_private *priv = i2c_get_adapdata(adap);
  246. ret = clk_enable(priv->clk);
  247. if (ret)
  248. return ret;
  249. if (priv->irq)
  250. xlr_i2c_wreg(priv->iobase, XLR_I2C_INT_EN, 0xf);
  251. for (i = 0; ret == 0 && i < num; i++) {
  252. msg = &msgs[i];
  253. priv->msg = msg;
  254. if (msg->flags & I2C_M_RD)
  255. ret = xlr_i2c_rx(priv, msg->len, &msg->buf[0],
  256. msg->addr);
  257. else
  258. ret = xlr_i2c_tx(priv, msg->len, &msg->buf[0],
  259. msg->addr);
  260. }
  261. if (priv->irq)
  262. xlr_i2c_wreg(priv->iobase, XLR_I2C_INT_EN, 0);
  263. clk_disable(priv->clk);
  264. priv->msg = NULL;
  265. return (ret != 0) ? ret : num;
  266. }
  267. static u32 xlr_func(struct i2c_adapter *adap)
  268. {
  269. /* Emulate SMBUS over I2C */
  270. return (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) | I2C_FUNC_I2C;
  271. }
  272. static struct i2c_algorithm xlr_i2c_algo = {
  273. .master_xfer = xlr_i2c_xfer,
  274. .functionality = xlr_func,
  275. };
  276. static const struct xlr_i2c_config xlr_i2c_config_default = {
  277. .status_busy = XLR_I2C_BUS_BUSY,
  278. .cfg_extra = 0,
  279. };
  280. static const struct xlr_i2c_config xlr_i2c_config_tangox = {
  281. .flags = XLR_I2C_FLAG_IRQ,
  282. .status_busy = 0,
  283. .cfg_extra = 1 << 8,
  284. };
  285. static const struct of_device_id xlr_i2c_dt_ids[] = {
  286. {
  287. .compatible = "sigma,smp8642-i2c",
  288. .data = &xlr_i2c_config_tangox,
  289. },
  290. { }
  291. };
  292. MODULE_DEVICE_TABLE(of, xlr_i2c_dt_ids);
  293. static int xlr_i2c_probe(struct platform_device *pdev)
  294. {
  295. const struct of_device_id *match;
  296. struct xlr_i2c_private *priv;
  297. struct resource *res;
  298. struct clk *clk;
  299. unsigned long clk_rate;
  300. unsigned long clk_div;
  301. u32 busfreq;
  302. int irq;
  303. int ret;
  304. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  305. if (!priv)
  306. return -ENOMEM;
  307. match = of_match_device(xlr_i2c_dt_ids, &pdev->dev);
  308. if (match)
  309. priv->cfg = match->data;
  310. else
  311. priv->cfg = &xlr_i2c_config_default;
  312. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  313. priv->iobase = devm_ioremap_resource(&pdev->dev, res);
  314. if (IS_ERR(priv->iobase))
  315. return PTR_ERR(priv->iobase);
  316. irq = platform_get_irq(pdev, 0);
  317. if (irq > 0 && (priv->cfg->flags & XLR_I2C_FLAG_IRQ)) {
  318. priv->irq = irq;
  319. xlr_i2c_wreg(priv->iobase, XLR_I2C_INT_EN, 0);
  320. xlr_i2c_wreg(priv->iobase, XLR_I2C_INT_STAT, 0xf);
  321. ret = devm_request_irq(&pdev->dev, priv->irq, xlr_i2c_irq,
  322. IRQF_SHARED, dev_name(&pdev->dev),
  323. priv);
  324. if (ret)
  325. return ret;
  326. init_waitqueue_head(&priv->wait);
  327. }
  328. if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  329. &busfreq))
  330. busfreq = 100000;
  331. clk = devm_clk_get(&pdev->dev, NULL);
  332. if (!IS_ERR(clk)) {
  333. ret = clk_prepare_enable(clk);
  334. if (ret)
  335. return ret;
  336. clk_rate = clk_get_rate(clk);
  337. clk_div = DIV_ROUND_UP(clk_rate, 2 * busfreq);
  338. xlr_i2c_wreg(priv->iobase, XLR_I2C_CLKDIV, clk_div);
  339. clk_disable(clk);
  340. priv->clk = clk;
  341. }
  342. priv->adap.dev.parent = &pdev->dev;
  343. priv->adap.dev.of_node = pdev->dev.of_node;
  344. priv->adap.owner = THIS_MODULE;
  345. priv->adap.algo_data = priv;
  346. priv->adap.algo = &xlr_i2c_algo;
  347. priv->adap.nr = pdev->id;
  348. priv->adap.class = I2C_CLASS_HWMON;
  349. snprintf(priv->adap.name, sizeof(priv->adap.name), "xlr-i2c");
  350. i2c_set_adapdata(&priv->adap, priv);
  351. ret = i2c_add_numbered_adapter(&priv->adap);
  352. if (ret < 0)
  353. return ret;
  354. platform_set_drvdata(pdev, priv);
  355. dev_info(&priv->adap.dev, "Added I2C Bus.\n");
  356. return 0;
  357. }
  358. static int xlr_i2c_remove(struct platform_device *pdev)
  359. {
  360. struct xlr_i2c_private *priv;
  361. priv = platform_get_drvdata(pdev);
  362. i2c_del_adapter(&priv->adap);
  363. clk_unprepare(priv->clk);
  364. return 0;
  365. }
  366. static struct platform_driver xlr_i2c_driver = {
  367. .probe = xlr_i2c_probe,
  368. .remove = xlr_i2c_remove,
  369. .driver = {
  370. .name = "xlr-i2cbus",
  371. .of_match_table = xlr_i2c_dt_ids,
  372. },
  373. };
  374. module_platform_driver(xlr_i2c_driver);
  375. MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@netlogicmicro.com>");
  376. MODULE_DESCRIPTION("XLR/XLS SoC I2C Controller driver");
  377. MODULE_LICENSE("GPL v2");
  378. MODULE_ALIAS("platform:xlr-i2cbus");