i2c-mxs.c 11 KB

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