i2c-meson.c 11 KB

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