i2c-mxs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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_QUEUESTAT_WRITE_QUEUE_CNT_MASK 0x0000001F
  65. #define MXS_I2C_QUEUECMD (0x80)
  66. #define MXS_I2C_QUEUEDATA (0x90)
  67. #define MXS_I2C_DATA (0xa0)
  68. #define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
  69. MXS_I2C_CTRL0_PRE_SEND_START | \
  70. MXS_I2C_CTRL0_MASTER_MODE | \
  71. MXS_I2C_CTRL0_DIRECTION | \
  72. MXS_I2C_CTRL0_XFER_COUNT(1))
  73. #define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
  74. MXS_I2C_CTRL0_MASTER_MODE | \
  75. MXS_I2C_CTRL0_DIRECTION)
  76. #define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
  77. MXS_I2C_CTRL0_MASTER_MODE)
  78. /**
  79. * struct mxs_i2c_dev - per device, private MXS-I2C data
  80. *
  81. * @dev: driver model device node
  82. * @regs: IO registers pointer
  83. * @cmd_complete: completion object for transaction wait
  84. * @cmd_err: error code for last transaction
  85. * @adapter: i2c subsystem adapter node
  86. */
  87. struct mxs_i2c_dev {
  88. struct device *dev;
  89. void __iomem *regs;
  90. struct completion cmd_complete;
  91. u32 cmd_err;
  92. struct i2c_adapter adapter;
  93. };
  94. /*
  95. * TODO: check if calls to here are really needed. If not, we could get rid of
  96. * mxs_reset_block and the mach-dependency. Needs an I2C analyzer, probably.
  97. */
  98. static void mxs_i2c_reset(struct mxs_i2c_dev *i2c)
  99. {
  100. mxs_reset_block(i2c->regs);
  101. writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
  102. writel(MXS_I2C_QUEUECTRL_PIO_QUEUE_MODE,
  103. i2c->regs + MXS_I2C_QUEUECTRL_SET);
  104. }
  105. static void mxs_i2c_pioq_setup_read(struct mxs_i2c_dev *i2c, u8 addr, int len,
  106. int flags)
  107. {
  108. u32 data;
  109. writel(MXS_CMD_I2C_SELECT, i2c->regs + MXS_I2C_QUEUECMD);
  110. data = (addr << 1) | I2C_SMBUS_READ;
  111. writel(data, i2c->regs + MXS_I2C_DATA);
  112. data = MXS_CMD_I2C_READ | MXS_I2C_CTRL0_XFER_COUNT(len) | flags;
  113. writel(data, i2c->regs + MXS_I2C_QUEUECMD);
  114. }
  115. static void mxs_i2c_pioq_setup_write(struct mxs_i2c_dev *i2c,
  116. u8 addr, u8 *buf, int len, int flags)
  117. {
  118. u32 data;
  119. int i, shifts_left;
  120. data = MXS_CMD_I2C_WRITE | MXS_I2C_CTRL0_XFER_COUNT(len + 1) | flags;
  121. writel(data, i2c->regs + MXS_I2C_QUEUECMD);
  122. /*
  123. * We have to copy the slave address (u8) and buffer (arbitrary number
  124. * of u8) into the data register (u32). To achieve that, the u8 are put
  125. * into the MSBs of 'data' which is then shifted for the next u8. When
  126. * appropriate, 'data' is written to MXS_I2C_DATA. So, the first u32
  127. * looks like this:
  128. *
  129. * 3 2 1 0
  130. * 10987654|32109876|54321098|76543210
  131. * --------+--------+--------+--------
  132. * buffer+2|buffer+1|buffer+0|slave_addr
  133. */
  134. data = ((addr << 1) | I2C_SMBUS_WRITE) << 24;
  135. for (i = 0; i < len; i++) {
  136. data >>= 8;
  137. data |= buf[i] << 24;
  138. if ((i & 3) == 2)
  139. writel(data, i2c->regs + MXS_I2C_DATA);
  140. }
  141. /* Write out the remaining bytes if any */
  142. shifts_left = 24 - (i & 3) * 8;
  143. if (shifts_left)
  144. writel(data >> shifts_left, i2c->regs + MXS_I2C_DATA);
  145. }
  146. /*
  147. * TODO: should be replaceable with a waitqueue and RD_QUEUE_IRQ (setting the
  148. * rd_threshold to 1). Couldn't get this to work, though.
  149. */
  150. static int mxs_i2c_wait_for_data(struct mxs_i2c_dev *i2c)
  151. {
  152. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  153. while (readl(i2c->regs + MXS_I2C_QUEUESTAT)
  154. & MXS_I2C_QUEUESTAT_RD_QUEUE_EMPTY) {
  155. if (time_after(jiffies, timeout))
  156. return -ETIMEDOUT;
  157. cond_resched();
  158. }
  159. return 0;
  160. }
  161. static int mxs_i2c_finish_read(struct mxs_i2c_dev *i2c, u8 *buf, int len)
  162. {
  163. u32 data;
  164. int i;
  165. for (i = 0; i < len; i++) {
  166. if ((i & 3) == 0) {
  167. if (mxs_i2c_wait_for_data(i2c))
  168. return -ETIMEDOUT;
  169. data = readl(i2c->regs + MXS_I2C_QUEUEDATA);
  170. }
  171. buf[i] = data & 0xff;
  172. data >>= 8;
  173. }
  174. return 0;
  175. }
  176. /*
  177. * Low level master read/write transaction.
  178. */
  179. static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
  180. int stop)
  181. {
  182. struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
  183. int ret;
  184. int flags;
  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. init_completion(&i2c->cmd_complete);
  190. i2c->cmd_err = 0;
  191. flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
  192. if (msg->flags & I2C_M_RD)
  193. mxs_i2c_pioq_setup_read(i2c, msg->addr, msg->len, flags);
  194. else
  195. mxs_i2c_pioq_setup_write(i2c, msg->addr, msg->buf, msg->len,
  196. flags);
  197. writel(MXS_I2C_QUEUECTRL_QUEUE_RUN,
  198. i2c->regs + MXS_I2C_QUEUECTRL_SET);
  199. ret = wait_for_completion_timeout(&i2c->cmd_complete,
  200. msecs_to_jiffies(1000));
  201. if (ret == 0)
  202. goto timeout;
  203. if ((!i2c->cmd_err) && (msg->flags & I2C_M_RD)) {
  204. ret = mxs_i2c_finish_read(i2c, msg->buf, msg->len);
  205. if (ret)
  206. goto timeout;
  207. }
  208. if (i2c->cmd_err == -ENXIO)
  209. mxs_i2c_reset(i2c);
  210. else
  211. writel(MXS_I2C_QUEUECTRL_QUEUE_RUN,
  212. i2c->regs + MXS_I2C_QUEUECTRL_CLR);
  213. dev_dbg(i2c->dev, "Done with err=%d\n", i2c->cmd_err);
  214. return i2c->cmd_err;
  215. timeout:
  216. dev_dbg(i2c->dev, "Timeout!\n");
  217. mxs_i2c_reset(i2c);
  218. return -ETIMEDOUT;
  219. }
  220. static int mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  221. int num)
  222. {
  223. int i;
  224. int err;
  225. for (i = 0; i < num; i++) {
  226. err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
  227. if (err)
  228. return err;
  229. }
  230. return num;
  231. }
  232. static u32 mxs_i2c_func(struct i2c_adapter *adap)
  233. {
  234. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  235. }
  236. static irqreturn_t mxs_i2c_isr(int this_irq, void *dev_id)
  237. {
  238. struct mxs_i2c_dev *i2c = dev_id;
  239. u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK;
  240. bool is_last_cmd;
  241. if (!stat)
  242. return IRQ_NONE;
  243. if (stat & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
  244. i2c->cmd_err = -ENXIO;
  245. else if (stat & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
  246. MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
  247. MXS_I2C_CTRL1_SLAVE_STOP_IRQ | MXS_I2C_CTRL1_SLAVE_IRQ))
  248. /* MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ is only for slaves */
  249. i2c->cmd_err = -EIO;
  250. is_last_cmd = (readl(i2c->regs + MXS_I2C_QUEUESTAT) &
  251. MXS_I2C_QUEUESTAT_WRITE_QUEUE_CNT_MASK) == 0;
  252. if (is_last_cmd || i2c->cmd_err)
  253. complete(&i2c->cmd_complete);
  254. writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR);
  255. return IRQ_HANDLED;
  256. }
  257. static const struct i2c_algorithm mxs_i2c_algo = {
  258. .master_xfer = mxs_i2c_xfer,
  259. .functionality = mxs_i2c_func,
  260. };
  261. static int __devinit mxs_i2c_probe(struct platform_device *pdev)
  262. {
  263. struct device *dev = &pdev->dev;
  264. struct mxs_i2c_dev *i2c;
  265. struct i2c_adapter *adap;
  266. struct resource *res;
  267. resource_size_t res_size;
  268. int err, irq;
  269. i2c = devm_kzalloc(dev, sizeof(struct mxs_i2c_dev), GFP_KERNEL);
  270. if (!i2c)
  271. return -ENOMEM;
  272. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  273. if (!res)
  274. return -ENOENT;
  275. res_size = resource_size(res);
  276. if (!devm_request_mem_region(dev, res->start, res_size, res->name))
  277. return -EBUSY;
  278. i2c->regs = devm_ioremap_nocache(dev, res->start, res_size);
  279. if (!i2c->regs)
  280. return -EBUSY;
  281. irq = platform_get_irq(pdev, 0);
  282. if (irq < 0)
  283. return irq;
  284. err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
  285. if (err)
  286. return err;
  287. i2c->dev = dev;
  288. platform_set_drvdata(pdev, i2c);
  289. /* Do reset to enforce correct startup after pinmuxing */
  290. mxs_i2c_reset(i2c);
  291. adap = &i2c->adapter;
  292. strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
  293. adap->owner = THIS_MODULE;
  294. adap->algo = &mxs_i2c_algo;
  295. adap->dev.parent = dev;
  296. adap->nr = pdev->id;
  297. i2c_set_adapdata(adap, i2c);
  298. err = i2c_add_numbered_adapter(adap);
  299. if (err) {
  300. dev_err(dev, "Failed to add adapter (%d)\n", err);
  301. writel(MXS_I2C_CTRL0_SFTRST,
  302. i2c->regs + MXS_I2C_CTRL0_SET);
  303. return err;
  304. }
  305. return 0;
  306. }
  307. static int __devexit mxs_i2c_remove(struct platform_device *pdev)
  308. {
  309. struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
  310. int ret;
  311. ret = i2c_del_adapter(&i2c->adapter);
  312. if (ret)
  313. return -EBUSY;
  314. writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
  315. platform_set_drvdata(pdev, NULL);
  316. return 0;
  317. }
  318. static struct platform_driver mxs_i2c_driver = {
  319. .driver = {
  320. .name = DRIVER_NAME,
  321. .owner = THIS_MODULE,
  322. },
  323. .remove = __devexit_p(mxs_i2c_remove),
  324. };
  325. static int __init mxs_i2c_init(void)
  326. {
  327. return platform_driver_probe(&mxs_i2c_driver, mxs_i2c_probe);
  328. }
  329. subsys_initcall(mxs_i2c_init);
  330. static void __exit mxs_i2c_exit(void)
  331. {
  332. platform_driver_unregister(&mxs_i2c_driver);
  333. }
  334. module_exit(mxs_i2c_exit);
  335. MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
  336. MODULE_DESCRIPTION("MXS I2C Bus Driver");
  337. MODULE_LICENSE("GPL");
  338. MODULE_ALIAS("platform:" DRIVER_NAME);