i2c-mxs.c 11 KB

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