i2c-lpc2k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (C) 2011 NXP Semiconductors
  3. *
  4. * Code portions referenced from the i2x-pxa and i2c-pnx drivers
  5. *
  6. * Make SMBus byte and word transactions work on LPC178x/7x
  7. * Copyright (c) 2012
  8. * Alexander Potashev, Emcraft Systems, aspotashev@emcraft.com
  9. * Anton Protopopov, Emcraft Systems, antonp@emcraft.com
  10. *
  11. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/errno.h>
  21. #include <linux/i2c.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/sched.h>
  30. #include <linux/time.h>
  31. /* LPC24xx register offsets and bits */
  32. #define LPC24XX_I2CONSET 0x00
  33. #define LPC24XX_I2STAT 0x04
  34. #define LPC24XX_I2DAT 0x08
  35. #define LPC24XX_I2ADDR 0x0c
  36. #define LPC24XX_I2SCLH 0x10
  37. #define LPC24XX_I2SCLL 0x14
  38. #define LPC24XX_I2CONCLR 0x18
  39. #define LPC24XX_AA BIT(2)
  40. #define LPC24XX_SI BIT(3)
  41. #define LPC24XX_STO BIT(4)
  42. #define LPC24XX_STA BIT(5)
  43. #define LPC24XX_I2EN BIT(6)
  44. #define LPC24XX_STO_AA (LPC24XX_STO | LPC24XX_AA)
  45. #define LPC24XX_CLEAR_ALL (LPC24XX_AA | LPC24XX_SI | LPC24XX_STO | \
  46. LPC24XX_STA | LPC24XX_I2EN)
  47. /* I2C SCL clock has different duty cycle depending on mode */
  48. #define I2C_STD_MODE_DUTY 46
  49. #define I2C_FAST_MODE_DUTY 36
  50. #define I2C_FAST_MODE_PLUS_DUTY 38
  51. /*
  52. * 26 possible I2C status codes, but codes applicable only
  53. * to master are listed here and used in this driver
  54. */
  55. enum {
  56. M_BUS_ERROR = 0x00,
  57. M_START = 0x08,
  58. M_REPSTART = 0x10,
  59. MX_ADDR_W_ACK = 0x18,
  60. MX_ADDR_W_NACK = 0x20,
  61. MX_DATA_W_ACK = 0x28,
  62. MX_DATA_W_NACK = 0x30,
  63. M_DATA_ARB_LOST = 0x38,
  64. MR_ADDR_R_ACK = 0x40,
  65. MR_ADDR_R_NACK = 0x48,
  66. MR_DATA_R_ACK = 0x50,
  67. MR_DATA_R_NACK = 0x58,
  68. M_I2C_IDLE = 0xf8,
  69. };
  70. struct lpc2k_i2c {
  71. void __iomem *base;
  72. struct clk *clk;
  73. int irq;
  74. wait_queue_head_t wait;
  75. struct i2c_adapter adap;
  76. struct i2c_msg *msg;
  77. int msg_idx;
  78. int msg_status;
  79. int is_last;
  80. };
  81. static void i2c_lpc2k_reset(struct lpc2k_i2c *i2c)
  82. {
  83. /* Will force clear all statuses */
  84. writel(LPC24XX_CLEAR_ALL, i2c->base + LPC24XX_I2CONCLR);
  85. writel(0, i2c->base + LPC24XX_I2ADDR);
  86. writel(LPC24XX_I2EN, i2c->base + LPC24XX_I2CONSET);
  87. }
  88. static int i2c_lpc2k_clear_arb(struct lpc2k_i2c *i2c)
  89. {
  90. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  91. /*
  92. * If the transfer needs to abort for some reason, we'll try to
  93. * force a stop condition to clear any pending bus conditions
  94. */
  95. writel(LPC24XX_STO, i2c->base + LPC24XX_I2CONSET);
  96. /* Wait for status change */
  97. while (readl(i2c->base + LPC24XX_I2STAT) != M_I2C_IDLE) {
  98. if (time_after(jiffies, timeout)) {
  99. /* Bus was not idle, try to reset adapter */
  100. i2c_lpc2k_reset(i2c);
  101. return -EBUSY;
  102. }
  103. cpu_relax();
  104. }
  105. return 0;
  106. }
  107. static void i2c_lpc2k_pump_msg(struct lpc2k_i2c *i2c)
  108. {
  109. unsigned char data;
  110. u32 status;
  111. /*
  112. * I2C in the LPC2xxx series is basically a state machine.
  113. * Just run through the steps based on the current status.
  114. */
  115. status = readl(i2c->base + LPC24XX_I2STAT);
  116. switch (status) {
  117. case M_START:
  118. case M_REPSTART:
  119. /* Start bit was just sent out, send out addr and dir */
  120. data = i2c->msg->addr << 1;
  121. if (i2c->msg->flags & I2C_M_RD)
  122. data |= 1;
  123. writel(data, i2c->base + LPC24XX_I2DAT);
  124. writel(LPC24XX_STA, i2c->base + LPC24XX_I2CONCLR);
  125. break;
  126. case MX_ADDR_W_ACK:
  127. case MX_DATA_W_ACK:
  128. /*
  129. * Address or data was sent out with an ACK. If there is more
  130. * data to send, send it now
  131. */
  132. if (i2c->msg_idx < i2c->msg->len) {
  133. writel(i2c->msg->buf[i2c->msg_idx],
  134. i2c->base + LPC24XX_I2DAT);
  135. } else if (i2c->is_last) {
  136. /* Last message, send stop */
  137. writel(LPC24XX_STO_AA, i2c->base + LPC24XX_I2CONSET);
  138. writel(LPC24XX_SI, i2c->base + LPC24XX_I2CONCLR);
  139. i2c->msg_status = 0;
  140. disable_irq_nosync(i2c->irq);
  141. } else {
  142. i2c->msg_status = 0;
  143. disable_irq_nosync(i2c->irq);
  144. }
  145. i2c->msg_idx++;
  146. break;
  147. case MR_ADDR_R_ACK:
  148. /* Receive first byte from slave */
  149. if (i2c->msg->len == 1) {
  150. /* Last byte, return NACK */
  151. writel(LPC24XX_AA, i2c->base + LPC24XX_I2CONCLR);
  152. } else {
  153. /* Not last byte, return ACK */
  154. writel(LPC24XX_AA, i2c->base + LPC24XX_I2CONSET);
  155. }
  156. writel(LPC24XX_STA, i2c->base + LPC24XX_I2CONCLR);
  157. break;
  158. case MR_DATA_R_NACK:
  159. /*
  160. * The I2C shows NACK status on reads, so we need to accept
  161. * the NACK as an ACK here. This should be ok, as the real
  162. * BACK would of been caught on the address write.
  163. */
  164. case MR_DATA_R_ACK:
  165. /* Data was received */
  166. if (i2c->msg_idx < i2c->msg->len) {
  167. i2c->msg->buf[i2c->msg_idx] =
  168. readl(i2c->base + LPC24XX_I2DAT);
  169. }
  170. /* If transfer is done, send STOP */
  171. if (i2c->msg_idx >= i2c->msg->len - 1 && i2c->is_last) {
  172. writel(LPC24XX_STO_AA, i2c->base + LPC24XX_I2CONSET);
  173. writel(LPC24XX_SI, i2c->base + LPC24XX_I2CONCLR);
  174. i2c->msg_status = 0;
  175. }
  176. /* Message is done */
  177. if (i2c->msg_idx >= i2c->msg->len - 1) {
  178. i2c->msg_status = 0;
  179. disable_irq_nosync(i2c->irq);
  180. }
  181. /*
  182. * One pre-last data input, send NACK to tell the slave that
  183. * this is going to be the last data byte to be transferred.
  184. */
  185. if (i2c->msg_idx >= i2c->msg->len - 2) {
  186. /* One byte left to receive - NACK */
  187. writel(LPC24XX_AA, i2c->base + LPC24XX_I2CONCLR);
  188. } else {
  189. /* More than one byte left to receive - ACK */
  190. writel(LPC24XX_AA, i2c->base + LPC24XX_I2CONSET);
  191. }
  192. writel(LPC24XX_STA, i2c->base + LPC24XX_I2CONCLR);
  193. i2c->msg_idx++;
  194. break;
  195. case MX_ADDR_W_NACK:
  196. case MX_DATA_W_NACK:
  197. case MR_ADDR_R_NACK:
  198. /* NACK processing is done */
  199. writel(LPC24XX_STO_AA, i2c->base + LPC24XX_I2CONSET);
  200. i2c->msg_status = -ENXIO;
  201. disable_irq_nosync(i2c->irq);
  202. break;
  203. case M_DATA_ARB_LOST:
  204. /* Arbitration lost */
  205. i2c->msg_status = -EAGAIN;
  206. /* Release the I2C bus */
  207. writel(LPC24XX_STA | LPC24XX_STO, i2c->base + LPC24XX_I2CONCLR);
  208. disable_irq_nosync(i2c->irq);
  209. break;
  210. default:
  211. /* Unexpected statuses */
  212. i2c->msg_status = -EIO;
  213. disable_irq_nosync(i2c->irq);
  214. break;
  215. }
  216. /* Exit on failure or all bytes transferred */
  217. if (i2c->msg_status != -EBUSY)
  218. wake_up(&i2c->wait);
  219. /*
  220. * If `msg_status` is zero, then `lpc2k_process_msg()`
  221. * is responsible for clearing the SI flag.
  222. */
  223. if (i2c->msg_status != 0)
  224. writel(LPC24XX_SI, i2c->base + LPC24XX_I2CONCLR);
  225. }
  226. static int lpc2k_process_msg(struct lpc2k_i2c *i2c, int msgidx)
  227. {
  228. /* A new transfer is kicked off by initiating a start condition */
  229. if (!msgidx) {
  230. writel(LPC24XX_STA, i2c->base + LPC24XX_I2CONSET);
  231. } else {
  232. /*
  233. * A multi-message I2C transfer continues where the
  234. * previous I2C transfer left off and uses the
  235. * current condition of the I2C adapter.
  236. */
  237. if (unlikely(i2c->msg->flags & I2C_M_NOSTART)) {
  238. WARN_ON(i2c->msg->len == 0);
  239. if (!(i2c->msg->flags & I2C_M_RD)) {
  240. /* Start transmit of data */
  241. writel(i2c->msg->buf[0],
  242. i2c->base + LPC24XX_I2DAT);
  243. i2c->msg_idx++;
  244. }
  245. } else {
  246. /* Start or repeated start */
  247. writel(LPC24XX_STA, i2c->base + LPC24XX_I2CONSET);
  248. }
  249. writel(LPC24XX_SI, i2c->base + LPC24XX_I2CONCLR);
  250. }
  251. enable_irq(i2c->irq);
  252. /* Wait for transfer completion */
  253. if (wait_event_timeout(i2c->wait, i2c->msg_status != -EBUSY,
  254. msecs_to_jiffies(1000)) == 0) {
  255. disable_irq_nosync(i2c->irq);
  256. return -ETIMEDOUT;
  257. }
  258. return i2c->msg_status;
  259. }
  260. static int i2c_lpc2k_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  261. int msg_num)
  262. {
  263. struct lpc2k_i2c *i2c = i2c_get_adapdata(adap);
  264. int ret, i;
  265. u32 stat;
  266. /* Check for bus idle condition */
  267. stat = readl(i2c->base + LPC24XX_I2STAT);
  268. if (stat != M_I2C_IDLE) {
  269. /* Something is holding the bus, try to clear it */
  270. return i2c_lpc2k_clear_arb(i2c);
  271. }
  272. /* Process a single message at a time */
  273. for (i = 0; i < msg_num; i++) {
  274. /* Save message pointer and current message data index */
  275. i2c->msg = &msgs[i];
  276. i2c->msg_idx = 0;
  277. i2c->msg_status = -EBUSY;
  278. i2c->is_last = (i == (msg_num - 1));
  279. ret = lpc2k_process_msg(i2c, i);
  280. if (ret)
  281. return ret;
  282. }
  283. return msg_num;
  284. }
  285. static irqreturn_t i2c_lpc2k_handler(int irq, void *dev_id)
  286. {
  287. struct lpc2k_i2c *i2c = dev_id;
  288. if (readl(i2c->base + LPC24XX_I2CONSET) & LPC24XX_SI) {
  289. i2c_lpc2k_pump_msg(i2c);
  290. return IRQ_HANDLED;
  291. }
  292. return IRQ_NONE;
  293. }
  294. static u32 i2c_lpc2k_functionality(struct i2c_adapter *adap)
  295. {
  296. /* Only emulated SMBus for now */
  297. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  298. }
  299. static const struct i2c_algorithm i2c_lpc2k_algorithm = {
  300. .master_xfer = i2c_lpc2k_xfer,
  301. .functionality = i2c_lpc2k_functionality,
  302. };
  303. static int i2c_lpc2k_probe(struct platform_device *pdev)
  304. {
  305. struct lpc2k_i2c *i2c;
  306. struct resource *res;
  307. u32 bus_clk_rate;
  308. u32 scl_high;
  309. u32 clkrate;
  310. int ret;
  311. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  312. if (!i2c)
  313. return -ENOMEM;
  314. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  315. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  316. if (IS_ERR(i2c->base))
  317. return PTR_ERR(i2c->base);
  318. i2c->irq = platform_get_irq(pdev, 0);
  319. if (i2c->irq < 0) {
  320. dev_err(&pdev->dev, "can't get interrupt resource\n");
  321. return i2c->irq;
  322. }
  323. init_waitqueue_head(&i2c->wait);
  324. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  325. if (IS_ERR(i2c->clk)) {
  326. dev_err(&pdev->dev, "error getting clock\n");
  327. return PTR_ERR(i2c->clk);
  328. }
  329. ret = clk_prepare_enable(i2c->clk);
  330. if (ret) {
  331. dev_err(&pdev->dev, "unable to enable clock.\n");
  332. return ret;
  333. }
  334. ret = devm_request_irq(&pdev->dev, i2c->irq, i2c_lpc2k_handler, 0,
  335. dev_name(&pdev->dev), i2c);
  336. if (ret < 0) {
  337. dev_err(&pdev->dev, "can't request interrupt.\n");
  338. goto fail_clk;
  339. }
  340. disable_irq_nosync(i2c->irq);
  341. /* Place controller is a known state */
  342. i2c_lpc2k_reset(i2c);
  343. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  344. &bus_clk_rate);
  345. if (ret)
  346. bus_clk_rate = 100000; /* 100 kHz default clock rate */
  347. clkrate = clk_get_rate(i2c->clk);
  348. if (clkrate == 0) {
  349. dev_err(&pdev->dev, "can't get I2C base clock\n");
  350. ret = -EINVAL;
  351. goto fail_clk;
  352. }
  353. /* Setup I2C dividers to generate clock with proper duty cycle */
  354. clkrate = clkrate / bus_clk_rate;
  355. if (bus_clk_rate <= 100000)
  356. scl_high = (clkrate * I2C_STD_MODE_DUTY) / 100;
  357. else if (bus_clk_rate <= 400000)
  358. scl_high = (clkrate * I2C_FAST_MODE_DUTY) / 100;
  359. else
  360. scl_high = (clkrate * I2C_FAST_MODE_PLUS_DUTY) / 100;
  361. writel(scl_high, i2c->base + LPC24XX_I2SCLH);
  362. writel(clkrate - scl_high, i2c->base + LPC24XX_I2SCLL);
  363. platform_set_drvdata(pdev, i2c);
  364. i2c_set_adapdata(&i2c->adap, i2c);
  365. i2c->adap.owner = THIS_MODULE;
  366. strlcpy(i2c->adap.name, "LPC2K I2C adapter", sizeof(i2c->adap.name));
  367. i2c->adap.algo = &i2c_lpc2k_algorithm;
  368. i2c->adap.dev.parent = &pdev->dev;
  369. i2c->adap.dev.of_node = pdev->dev.of_node;
  370. ret = i2c_add_adapter(&i2c->adap);
  371. if (ret < 0) {
  372. dev_err(&pdev->dev, "failed to add adapter!\n");
  373. goto fail_clk;
  374. }
  375. dev_info(&pdev->dev, "LPC2K I2C adapter\n");
  376. return 0;
  377. fail_clk:
  378. clk_disable_unprepare(i2c->clk);
  379. return ret;
  380. }
  381. static int i2c_lpc2k_remove(struct platform_device *dev)
  382. {
  383. struct lpc2k_i2c *i2c = platform_get_drvdata(dev);
  384. i2c_del_adapter(&i2c->adap);
  385. clk_disable_unprepare(i2c->clk);
  386. return 0;
  387. }
  388. #ifdef CONFIG_PM
  389. static int i2c_lpc2k_suspend(struct device *dev)
  390. {
  391. struct platform_device *pdev = to_platform_device(dev);
  392. struct lpc2k_i2c *i2c = platform_get_drvdata(pdev);
  393. clk_disable(i2c->clk);
  394. return 0;
  395. }
  396. static int i2c_lpc2k_resume(struct device *dev)
  397. {
  398. struct platform_device *pdev = to_platform_device(dev);
  399. struct lpc2k_i2c *i2c = platform_get_drvdata(pdev);
  400. clk_enable(i2c->clk);
  401. i2c_lpc2k_reset(i2c);
  402. return 0;
  403. }
  404. static const struct dev_pm_ops i2c_lpc2k_dev_pm_ops = {
  405. .suspend_noirq = i2c_lpc2k_suspend,
  406. .resume_noirq = i2c_lpc2k_resume,
  407. };
  408. #define I2C_LPC2K_DEV_PM_OPS (&i2c_lpc2k_dev_pm_ops)
  409. #else
  410. #define I2C_LPC2K_DEV_PM_OPS NULL
  411. #endif
  412. static const struct of_device_id lpc2k_i2c_match[] = {
  413. { .compatible = "nxp,lpc1788-i2c" },
  414. {},
  415. };
  416. MODULE_DEVICE_TABLE(of, lpc2k_i2c_match);
  417. static struct platform_driver i2c_lpc2k_driver = {
  418. .probe = i2c_lpc2k_probe,
  419. .remove = i2c_lpc2k_remove,
  420. .driver = {
  421. .name = "lpc2k-i2c",
  422. .pm = I2C_LPC2K_DEV_PM_OPS,
  423. .of_match_table = lpc2k_i2c_match,
  424. },
  425. };
  426. module_platform_driver(i2c_lpc2k_driver);
  427. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  428. MODULE_DESCRIPTION("I2C driver for LPC2xxx devices");
  429. MODULE_LICENSE("GPL");
  430. MODULE_ALIAS("platform:lpc2k-i2c");