i2c-meson.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * I2C bus driver for Amlogic Meson SoCs
  3. *
  4. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/completion.h>
  12. #include <linux/i2c.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/types.h>
  20. /* Meson I2C register map */
  21. #define REG_CTRL 0x00
  22. #define REG_SLAVE_ADDR 0x04
  23. #define REG_TOK_LIST0 0x08
  24. #define REG_TOK_LIST1 0x0c
  25. #define REG_TOK_WDATA0 0x10
  26. #define REG_TOK_WDATA1 0x14
  27. #define REG_TOK_RDATA0 0x18
  28. #define REG_TOK_RDATA1 0x1c
  29. /* Control register fields */
  30. #define REG_CTRL_START BIT(0)
  31. #define REG_CTRL_ACK_IGNORE BIT(1)
  32. #define REG_CTRL_STATUS BIT(2)
  33. #define REG_CTRL_ERROR BIT(3)
  34. #define REG_CTRL_CLKDIV_SHIFT 12
  35. #define REG_CTRL_CLKDIV_MASK ((BIT(10) - 1) << REG_CTRL_CLKDIV_SHIFT)
  36. #define I2C_TIMEOUT_MS 500
  37. #define DEFAULT_FREQ 100000
  38. enum {
  39. TOKEN_END = 0,
  40. TOKEN_START,
  41. TOKEN_SLAVE_ADDR_WRITE,
  42. TOKEN_SLAVE_ADDR_READ,
  43. TOKEN_DATA,
  44. TOKEN_DATA_LAST,
  45. TOKEN_STOP,
  46. };
  47. enum {
  48. STATE_IDLE,
  49. STATE_READ,
  50. STATE_WRITE,
  51. STATE_STOP,
  52. };
  53. /**
  54. * struct meson_i2c - Meson I2C device private data
  55. *
  56. * @adap: I2C adapter instance
  57. * @dev: Pointer to device structure
  58. * @regs: Base address of the device memory mapped registers
  59. * @clk: Pointer to clock structure
  60. * @irq: IRQ number
  61. * @msg: Pointer to the current I2C message
  62. * @state: Current state in the driver state machine
  63. * @last: Flag set for the last message in the transfer
  64. * @count: Number of bytes to be sent/received in current transfer
  65. * @pos: Current position in the send/receive buffer
  66. * @error: Flag set when an error is received
  67. * @lock: To avoid race conditions between irq handler and xfer code
  68. * @done: Completion used to wait for transfer termination
  69. * @frequency: Operating frequency of I2C bus clock
  70. * @tokens: Sequence of tokens to be written to the device
  71. * @num_tokens: Number of tokens
  72. */
  73. struct meson_i2c {
  74. struct i2c_adapter adap;
  75. struct device *dev;
  76. void __iomem *regs;
  77. struct clk *clk;
  78. int irq;
  79. struct i2c_msg *msg;
  80. int state;
  81. bool last;
  82. int count;
  83. int pos;
  84. int error;
  85. spinlock_t lock;
  86. struct completion done;
  87. unsigned int frequency;
  88. u32 tokens[2];
  89. int num_tokens;
  90. };
  91. static void meson_i2c_set_mask(struct meson_i2c *i2c, int reg, u32 mask,
  92. u32 val)
  93. {
  94. u32 data;
  95. data = readl(i2c->regs + reg);
  96. data &= ~mask;
  97. data |= val & mask;
  98. writel(data, i2c->regs + reg);
  99. }
  100. static void meson_i2c_reset_tokens(struct meson_i2c *i2c)
  101. {
  102. i2c->tokens[0] = 0;
  103. i2c->tokens[1] = 0;
  104. i2c->num_tokens = 0;
  105. }
  106. static void meson_i2c_add_token(struct meson_i2c *i2c, int token)
  107. {
  108. if (i2c->num_tokens < 8)
  109. i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4);
  110. else
  111. i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4);
  112. i2c->num_tokens++;
  113. }
  114. static void meson_i2c_write_tokens(struct meson_i2c *i2c)
  115. {
  116. writel(i2c->tokens[0], i2c->regs + REG_TOK_LIST0);
  117. writel(i2c->tokens[1], i2c->regs + REG_TOK_LIST1);
  118. }
  119. static void meson_i2c_set_clk_div(struct meson_i2c *i2c)
  120. {
  121. unsigned long clk_rate = clk_get_rate(i2c->clk);
  122. unsigned int div;
  123. div = DIV_ROUND_UP(clk_rate, i2c->frequency * 4);
  124. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK,
  125. div << REG_CTRL_CLKDIV_SHIFT);
  126. dev_dbg(i2c->dev, "%s: clk %lu, freq %u, div %u\n", __func__,
  127. clk_rate, i2c->frequency, div);
  128. }
  129. static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len)
  130. {
  131. u32 rdata0, rdata1;
  132. int i;
  133. rdata0 = readl(i2c->regs + REG_TOK_RDATA0);
  134. rdata1 = readl(i2c->regs + REG_TOK_RDATA1);
  135. dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
  136. rdata0, rdata1, len);
  137. for (i = 0; i < min_t(int, 4, len); i++)
  138. *buf++ = (rdata0 >> i * 8) & 0xff;
  139. for (i = 4; i < min_t(int, 8, len); i++)
  140. *buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
  141. }
  142. static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len)
  143. {
  144. u32 wdata0 = 0, wdata1 = 0;
  145. int i;
  146. for (i = 0; i < min_t(int, 4, len); i++)
  147. wdata0 |= *buf++ << (i * 8);
  148. for (i = 4; i < min_t(int, 8, len); i++)
  149. wdata1 |= *buf++ << ((i - 4) * 8);
  150. writel(wdata0, i2c->regs + REG_TOK_WDATA0);
  151. writel(wdata1, i2c->regs + REG_TOK_WDATA1);
  152. dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
  153. wdata0, wdata1, len);
  154. }
  155. static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
  156. {
  157. bool write = !(i2c->msg->flags & I2C_M_RD);
  158. int i;
  159. i2c->count = min_t(int, i2c->msg->len - i2c->pos, 8);
  160. for (i = 0; i < i2c->count - 1; i++)
  161. meson_i2c_add_token(i2c, TOKEN_DATA);
  162. if (i2c->count) {
  163. if (write || i2c->pos + i2c->count < i2c->msg->len)
  164. meson_i2c_add_token(i2c, TOKEN_DATA);
  165. else
  166. meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
  167. }
  168. if (write)
  169. meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
  170. }
  171. static void meson_i2c_stop(struct meson_i2c *i2c)
  172. {
  173. dev_dbg(i2c->dev, "%s: last %d\n", __func__, i2c->last);
  174. if (i2c->last) {
  175. i2c->state = STATE_STOP;
  176. meson_i2c_add_token(i2c, TOKEN_STOP);
  177. } else {
  178. i2c->state = STATE_IDLE;
  179. complete_all(&i2c->done);
  180. }
  181. }
  182. static irqreturn_t meson_i2c_irq(int irqno, void *dev_id)
  183. {
  184. struct meson_i2c *i2c = dev_id;
  185. unsigned int ctrl;
  186. spin_lock(&i2c->lock);
  187. meson_i2c_reset_tokens(i2c);
  188. ctrl = readl(i2c->regs + REG_CTRL);
  189. dev_dbg(i2c->dev, "irq: state %d, pos %d, count %d, ctrl %08x\n",
  190. i2c->state, i2c->pos, i2c->count, ctrl);
  191. if (ctrl & REG_CTRL_ERROR && i2c->state != STATE_IDLE) {
  192. /*
  193. * The bit is set when the IGNORE_NAK bit is cleared
  194. * and the device didn't respond. In this case, the
  195. * I2C controller automatically generates a STOP
  196. * condition.
  197. */
  198. dev_dbg(i2c->dev, "error bit set\n");
  199. i2c->error = -ENXIO;
  200. i2c->state = STATE_IDLE;
  201. complete_all(&i2c->done);
  202. goto out;
  203. }
  204. switch (i2c->state) {
  205. case STATE_READ:
  206. if (i2c->count > 0) {
  207. meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos,
  208. i2c->count);
  209. i2c->pos += i2c->count;
  210. }
  211. if (i2c->pos >= i2c->msg->len) {
  212. meson_i2c_stop(i2c);
  213. break;
  214. }
  215. meson_i2c_prepare_xfer(i2c);
  216. break;
  217. case STATE_WRITE:
  218. i2c->pos += i2c->count;
  219. if (i2c->pos >= i2c->msg->len) {
  220. meson_i2c_stop(i2c);
  221. break;
  222. }
  223. meson_i2c_prepare_xfer(i2c);
  224. break;
  225. case STATE_STOP:
  226. i2c->state = STATE_IDLE;
  227. complete_all(&i2c->done);
  228. break;
  229. case STATE_IDLE:
  230. break;
  231. }
  232. out:
  233. if (i2c->state != STATE_IDLE) {
  234. /* Restart the processing */
  235. meson_i2c_write_tokens(i2c);
  236. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
  237. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START,
  238. REG_CTRL_START);
  239. }
  240. spin_unlock(&i2c->lock);
  241. return IRQ_HANDLED;
  242. }
  243. static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
  244. {
  245. int token;
  246. token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
  247. TOKEN_SLAVE_ADDR_WRITE;
  248. writel(msg->addr << 1, i2c->regs + REG_SLAVE_ADDR);
  249. meson_i2c_add_token(i2c, TOKEN_START);
  250. meson_i2c_add_token(i2c, token);
  251. }
  252. static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
  253. int last)
  254. {
  255. unsigned long time_left, flags;
  256. int ret = 0;
  257. i2c->msg = msg;
  258. i2c->last = last;
  259. i2c->pos = 0;
  260. i2c->count = 0;
  261. i2c->error = 0;
  262. meson_i2c_reset_tokens(i2c);
  263. flags = (msg->flags & I2C_M_IGNORE_NAK) ? REG_CTRL_ACK_IGNORE : 0;
  264. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_ACK_IGNORE, flags);
  265. if (!(msg->flags & I2C_M_NOSTART))
  266. meson_i2c_do_start(i2c, msg);
  267. i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  268. meson_i2c_prepare_xfer(i2c);
  269. meson_i2c_write_tokens(i2c);
  270. reinit_completion(&i2c->done);
  271. /* Start the transfer */
  272. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
  273. time_left = msecs_to_jiffies(I2C_TIMEOUT_MS);
  274. time_left = wait_for_completion_timeout(&i2c->done, time_left);
  275. /*
  276. * Protect access to i2c struct and registers from interrupt
  277. * handlers triggered by a transfer terminated after the
  278. * timeout period
  279. */
  280. spin_lock_irqsave(&i2c->lock, flags);
  281. /* Abort any active operation */
  282. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
  283. if (!time_left) {
  284. i2c->state = STATE_IDLE;
  285. ret = -ETIMEDOUT;
  286. }
  287. if (i2c->error)
  288. ret = i2c->error;
  289. spin_unlock_irqrestore(&i2c->lock, flags);
  290. return ret;
  291. }
  292. static int meson_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  293. int num)
  294. {
  295. struct meson_i2c *i2c = adap->algo_data;
  296. int i, ret = 0, count = 0;
  297. clk_enable(i2c->clk);
  298. meson_i2c_set_clk_div(i2c);
  299. for (i = 0; i < num; i++) {
  300. ret = meson_i2c_xfer_msg(i2c, msgs + i, i == num - 1);
  301. if (ret)
  302. break;
  303. count++;
  304. }
  305. clk_disable(i2c->clk);
  306. return ret ? ret : count;
  307. }
  308. static u32 meson_i2c_func(struct i2c_adapter *adap)
  309. {
  310. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  311. }
  312. static const struct i2c_algorithm meson_i2c_algorithm = {
  313. .master_xfer = meson_i2c_xfer,
  314. .functionality = meson_i2c_func,
  315. };
  316. static int meson_i2c_probe(struct platform_device *pdev)
  317. {
  318. struct device_node *np = pdev->dev.of_node;
  319. struct meson_i2c *i2c;
  320. struct resource *mem;
  321. int ret = 0;
  322. i2c = devm_kzalloc(&pdev->dev, sizeof(struct meson_i2c), GFP_KERNEL);
  323. if (!i2c)
  324. return -ENOMEM;
  325. if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  326. &i2c->frequency))
  327. i2c->frequency = DEFAULT_FREQ;
  328. i2c->dev = &pdev->dev;
  329. platform_set_drvdata(pdev, i2c);
  330. spin_lock_init(&i2c->lock);
  331. init_completion(&i2c->done);
  332. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  333. if (IS_ERR(i2c->clk)) {
  334. dev_err(&pdev->dev, "can't get device clock\n");
  335. return PTR_ERR(i2c->clk);
  336. }
  337. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  338. i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
  339. if (IS_ERR(i2c->regs))
  340. return PTR_ERR(i2c->regs);
  341. i2c->irq = platform_get_irq(pdev, 0);
  342. if (i2c->irq < 0) {
  343. dev_err(&pdev->dev, "can't find IRQ\n");
  344. return i2c->irq;
  345. }
  346. ret = devm_request_irq(&pdev->dev, i2c->irq, meson_i2c_irq,
  347. 0, dev_name(&pdev->dev), i2c);
  348. if (ret < 0) {
  349. dev_err(&pdev->dev, "can't request IRQ\n");
  350. return ret;
  351. }
  352. ret = clk_prepare(i2c->clk);
  353. if (ret < 0) {
  354. dev_err(&pdev->dev, "can't prepare clock\n");
  355. return ret;
  356. }
  357. strlcpy(i2c->adap.name, "Meson I2C adapter",
  358. sizeof(i2c->adap.name));
  359. i2c->adap.owner = THIS_MODULE;
  360. i2c->adap.algo = &meson_i2c_algorithm;
  361. i2c->adap.dev.parent = &pdev->dev;
  362. i2c->adap.dev.of_node = np;
  363. i2c->adap.algo_data = i2c;
  364. /*
  365. * A transfer is triggered when START bit changes from 0 to 1.
  366. * Ensure that the bit is set to 0 after probe
  367. */
  368. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
  369. ret = i2c_add_adapter(&i2c->adap);
  370. if (ret < 0) {
  371. dev_err(&pdev->dev, "can't register adapter\n");
  372. clk_unprepare(i2c->clk);
  373. return ret;
  374. }
  375. return 0;
  376. }
  377. static int meson_i2c_remove(struct platform_device *pdev)
  378. {
  379. struct meson_i2c *i2c = platform_get_drvdata(pdev);
  380. i2c_del_adapter(&i2c->adap);
  381. clk_unprepare(i2c->clk);
  382. return 0;
  383. }
  384. static const struct of_device_id meson_i2c_match[] = {
  385. { .compatible = "amlogic,meson6-i2c" },
  386. { },
  387. };
  388. MODULE_DEVICE_TABLE(of, meson_i2c_match);
  389. static struct platform_driver meson_i2c_driver = {
  390. .probe = meson_i2c_probe,
  391. .remove = meson_i2c_remove,
  392. .driver = {
  393. .name = "meson-i2c",
  394. .of_match_table = meson_i2c_match,
  395. },
  396. };
  397. module_platform_driver(meson_i2c_driver);
  398. MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");
  399. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  400. MODULE_LICENSE("GPL v2");