i2c-mxs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Freescale MXS I2C bus driver
  3. *
  4. * Copyright (C) 2011 Wolfram Sang, Pengutronix e.K.
  5. *
  6. * based on a (non-working) driver which was:
  7. *
  8. * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
  9. *
  10. * TODO: add dma-support if platform-support for it is available
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/device.h>
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/err.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/completion.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/io.h>
  28. #include <mach/common.h>
  29. #define DRIVER_NAME "mxs-i2c"
  30. #define MXS_I2C_CTRL0 (0x00)
  31. #define MXS_I2C_CTRL0_SET (0x04)
  32. #define MXS_I2C_CTRL0_SFTRST 0x80000000
  33. #define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
  34. #define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
  35. #define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
  36. #define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
  37. #define MXS_I2C_CTRL0_MASTER_MODE 0x00020000
  38. #define MXS_I2C_CTRL0_DIRECTION 0x00010000
  39. #define MXS_I2C_CTRL0_XFER_COUNT(v) ((v) & 0x0000FFFF)
  40. #define MXS_I2C_CTRL1 (0x40)
  41. #define MXS_I2C_CTRL1_SET (0x44)
  42. #define MXS_I2C_CTRL1_CLR (0x48)
  43. #define MXS_I2C_CTRL1_BUS_FREE_IRQ 0x80
  44. #define MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ 0x40
  45. #define MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ 0x20
  46. #define MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ 0x10
  47. #define MXS_I2C_CTRL1_EARLY_TERM_IRQ 0x08
  48. #define MXS_I2C_CTRL1_MASTER_LOSS_IRQ 0x04
  49. #define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
  50. #define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
  51. #define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
  52. MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
  53. MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
  54. MXS_I2C_CTRL1_MASTER_LOSS_IRQ | \
  55. MXS_I2C_CTRL1_SLAVE_STOP_IRQ | \
  56. MXS_I2C_CTRL1_SLAVE_IRQ)
  57. #define MXS_I2C_QUEUECTRL (0x60)
  58. #define MXS_I2C_QUEUECTRL_SET (0x64)
  59. #define MXS_I2C_QUEUECTRL_CLR (0x68)
  60. #define MXS_I2C_QUEUECTRL_QUEUE_RUN 0x20
  61. #define MXS_I2C_QUEUECTRL_PIO_QUEUE_MODE 0x04
  62. #define MXS_I2C_QUEUESTAT (0x70)
  63. #define MXS_I2C_QUEUESTAT_RD_QUEUE_EMPTY 0x00002000
  64. #define MXS_I2C_QUEUECMD (0x80)
  65. #define MXS_I2C_QUEUEDATA (0x90)
  66. #define MXS_I2C_DATA (0xa0)
  67. #define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
  68. MXS_I2C_CTRL0_PRE_SEND_START | \
  69. MXS_I2C_CTRL0_MASTER_MODE | \
  70. MXS_I2C_CTRL0_DIRECTION | \
  71. MXS_I2C_CTRL0_XFER_COUNT(1))
  72. #define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
  73. MXS_I2C_CTRL0_MASTER_MODE | \
  74. MXS_I2C_CTRL0_DIRECTION)
  75. #define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
  76. MXS_I2C_CTRL0_MASTER_MODE)
  77. /**
  78. * struct mxs_i2c_dev - per device, private MXS-I2C data
  79. *
  80. * @dev: driver model device node
  81. * @regs: IO registers pointer
  82. * @cmd_complete: completion object for transaction wait
  83. * @cmd_err: error code for last transaction
  84. * @adapter: i2c subsystem adapter node
  85. */
  86. struct mxs_i2c_dev {
  87. struct device *dev;
  88. void __iomem *regs;
  89. struct completion cmd_complete;
  90. u32 cmd_err;
  91. struct i2c_adapter adapter;
  92. };
  93. /*
  94. * TODO: check if calls to here are really needed. If not, we could get rid of
  95. * mxs_reset_block and the mach-dependency. Needs an I2C analyzer, probably.
  96. */
  97. static void mxs_i2c_reset(struct mxs_i2c_dev *i2c)
  98. {
  99. mxs_reset_block(i2c->regs);
  100. writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
  101. writel(MXS_I2C_QUEUECTRL_PIO_QUEUE_MODE,
  102. i2c->regs + MXS_I2C_QUEUECTRL_SET);
  103. }
  104. static void mxs_i2c_pioq_setup_read(struct mxs_i2c_dev *i2c, u8 addr, int len,
  105. int flags)
  106. {
  107. u32 data;
  108. writel(MXS_CMD_I2C_SELECT, i2c->regs + MXS_I2C_QUEUECMD);
  109. data = (addr << 1) | I2C_SMBUS_READ;
  110. writel(data, i2c->regs + MXS_I2C_DATA);
  111. data = MXS_CMD_I2C_READ | MXS_I2C_CTRL0_XFER_COUNT(len) | flags;
  112. writel(data, i2c->regs + MXS_I2C_QUEUECMD);
  113. }
  114. static void mxs_i2c_pioq_setup_write(struct mxs_i2c_dev *i2c,
  115. u8 addr, u8 *buf, int len, int flags)
  116. {
  117. u32 data;
  118. int i, shifts_left;
  119. data = MXS_CMD_I2C_WRITE | MXS_I2C_CTRL0_XFER_COUNT(len + 1) | flags;
  120. writel(data, i2c->regs + MXS_I2C_QUEUECMD);
  121. /*
  122. * We have to copy the slave address (u8) and buffer (arbitrary number
  123. * of u8) into the data register (u32). To achieve that, the u8 are put
  124. * into the MSBs of 'data' which is then shifted for the next u8. When
  125. * apropriate, 'data' is written to MXS_I2C_DATA. So, the first u32
  126. * looks like this:
  127. *
  128. * 3 2 1 0
  129. * 10987654|32109876|54321098|76543210
  130. * --------+--------+--------+--------
  131. * buffer+2|buffer+1|buffer+0|slave_addr
  132. */
  133. data = ((addr << 1) | I2C_SMBUS_WRITE) << 24;
  134. for (i = 0; i < len; i++) {
  135. data >>= 8;
  136. data |= buf[i] << 24;
  137. if ((i & 3) == 2)
  138. writel(data, i2c->regs + MXS_I2C_DATA);
  139. }
  140. /* Write out the remaining bytes if any */
  141. shifts_left = 24 - (i & 3) * 8;
  142. if (shifts_left)
  143. writel(data >> shifts_left, i2c->regs + MXS_I2C_DATA);
  144. }
  145. /*
  146. * TODO: should be replaceable with a waitqueue and RD_QUEUE_IRQ (setting the
  147. * rd_threshold to 1). Couldn't get this to work, though.
  148. */
  149. static int mxs_i2c_wait_for_data(struct mxs_i2c_dev *i2c)
  150. {
  151. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  152. while (readl(i2c->regs + MXS_I2C_QUEUESTAT)
  153. & MXS_I2C_QUEUESTAT_RD_QUEUE_EMPTY) {
  154. if (time_after(jiffies, timeout))
  155. return -ETIMEDOUT;
  156. cond_resched();
  157. }
  158. return 0;
  159. }
  160. static int mxs_i2c_finish_read(struct mxs_i2c_dev *i2c, u8 *buf, int len)
  161. {
  162. u32 data;
  163. int i;
  164. for (i = 0; i < len; i++) {
  165. if ((i & 3) == 0) {
  166. if (mxs_i2c_wait_for_data(i2c))
  167. return -ETIMEDOUT;
  168. data = readl(i2c->regs + MXS_I2C_QUEUEDATA);
  169. }
  170. buf[i] = data & 0xff;
  171. data >>= 8;
  172. }
  173. return 0;
  174. }
  175. /*
  176. * Low level master read/write transaction.
  177. */
  178. static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
  179. int stop)
  180. {
  181. struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
  182. int ret;
  183. int flags;
  184. init_completion(&i2c->cmd_complete);
  185. dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
  186. msg->addr, msg->len, msg->flags, stop);
  187. if (msg->len == 0)
  188. return -EINVAL;
  189. flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
  190. if (msg->flags & I2C_M_RD)
  191. mxs_i2c_pioq_setup_read(i2c, msg->addr, msg->len, flags);
  192. else
  193. mxs_i2c_pioq_setup_write(i2c, msg->addr, msg->buf, msg->len,
  194. flags);
  195. writel(MXS_I2C_QUEUECTRL_QUEUE_RUN,
  196. i2c->regs + MXS_I2C_QUEUECTRL_SET);
  197. ret = wait_for_completion_timeout(&i2c->cmd_complete,
  198. msecs_to_jiffies(1000));
  199. if (ret == 0)
  200. goto timeout;
  201. if ((!i2c->cmd_err) && (msg->flags & I2C_M_RD)) {
  202. ret = mxs_i2c_finish_read(i2c, msg->buf, msg->len);
  203. if (ret)
  204. goto timeout;
  205. }
  206. if (i2c->cmd_err == -ENXIO)
  207. mxs_i2c_reset(i2c);
  208. dev_dbg(i2c->dev, "Done with err=%d\n", i2c->cmd_err);
  209. return i2c->cmd_err;
  210. timeout:
  211. dev_dbg(i2c->dev, "Timeout!\n");
  212. mxs_i2c_reset(i2c);
  213. return -ETIMEDOUT;
  214. }
  215. static int mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  216. int num)
  217. {
  218. int i;
  219. int err;
  220. for (i = 0; i < num; i++) {
  221. err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
  222. if (err)
  223. return err;
  224. }
  225. return num;
  226. }
  227. static u32 mxs_i2c_func(struct i2c_adapter *adap)
  228. {
  229. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  230. }
  231. static irqreturn_t mxs_i2c_isr(int this_irq, void *dev_id)
  232. {
  233. struct mxs_i2c_dev *i2c = dev_id;
  234. u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK;
  235. if (!stat)
  236. return IRQ_NONE;
  237. if (stat & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
  238. i2c->cmd_err = -ENXIO;
  239. else if (stat & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
  240. MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
  241. MXS_I2C_CTRL1_SLAVE_STOP_IRQ | MXS_I2C_CTRL1_SLAVE_IRQ))
  242. /* MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ is only for slaves */
  243. i2c->cmd_err = -EIO;
  244. else
  245. i2c->cmd_err = 0;
  246. complete(&i2c->cmd_complete);
  247. writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR);
  248. return IRQ_HANDLED;
  249. }
  250. static const struct i2c_algorithm mxs_i2c_algo = {
  251. .master_xfer = mxs_i2c_xfer,
  252. .functionality = mxs_i2c_func,
  253. };
  254. static int __devinit mxs_i2c_probe(struct platform_device *pdev)
  255. {
  256. struct device *dev = &pdev->dev;
  257. struct mxs_i2c_dev *i2c;
  258. struct i2c_adapter *adap;
  259. struct resource *res;
  260. resource_size_t res_size;
  261. int err, irq;
  262. i2c = devm_kzalloc(dev, sizeof(struct mxs_i2c_dev), GFP_KERNEL);
  263. if (!i2c)
  264. return -ENOMEM;
  265. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  266. if (!res)
  267. return -ENOENT;
  268. res_size = resource_size(res);
  269. if (!devm_request_mem_region(dev, res->start, res_size, res->name))
  270. return -EBUSY;
  271. i2c->regs = devm_ioremap_nocache(dev, res->start, res_size);
  272. if (!i2c->regs)
  273. return -EBUSY;
  274. irq = platform_get_irq(pdev, 0);
  275. if (irq < 0)
  276. return irq;
  277. err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
  278. if (err)
  279. return err;
  280. i2c->dev = dev;
  281. platform_set_drvdata(pdev, i2c);
  282. /* Do reset to enforce correct startup after pinmuxing */
  283. mxs_i2c_reset(i2c);
  284. adap = &i2c->adapter;
  285. strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
  286. adap->owner = THIS_MODULE;
  287. adap->algo = &mxs_i2c_algo;
  288. adap->dev.parent = dev;
  289. adap->nr = pdev->id;
  290. i2c_set_adapdata(adap, i2c);
  291. err = i2c_add_numbered_adapter(adap);
  292. if (err) {
  293. dev_err(dev, "Failed to add adapter (%d)\n", err);
  294. writel(MXS_I2C_CTRL0_SFTRST,
  295. i2c->regs + MXS_I2C_CTRL0_SET);
  296. return err;
  297. }
  298. return 0;
  299. }
  300. static int __devexit mxs_i2c_remove(struct platform_device *pdev)
  301. {
  302. struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
  303. int ret;
  304. ret = i2c_del_adapter(&i2c->adapter);
  305. if (ret)
  306. return -EBUSY;
  307. writel(MXS_I2C_QUEUECTRL_QUEUE_RUN,
  308. i2c->regs + MXS_I2C_QUEUECTRL_CLR);
  309. writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
  310. platform_set_drvdata(pdev, NULL);
  311. return 0;
  312. }
  313. static struct platform_driver mxs_i2c_driver = {
  314. .driver = {
  315. .name = DRIVER_NAME,
  316. .owner = THIS_MODULE,
  317. },
  318. .remove = __devexit_p(mxs_i2c_remove),
  319. };
  320. static int __init mxs_i2c_init(void)
  321. {
  322. return platform_driver_probe(&mxs_i2c_driver, mxs_i2c_probe);
  323. }
  324. subsys_initcall(mxs_i2c_init);
  325. static void __exit mxs_i2c_exit(void)
  326. {
  327. platform_driver_unregister(&mxs_i2c_driver);
  328. }
  329. module_exit(mxs_i2c_exit);
  330. MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
  331. MODULE_DESCRIPTION("MXS I2C Bus Driver");
  332. MODULE_LICENSE("GPL");
  333. MODULE_ALIAS("platform:" DRIVER_NAME);