i2c-meson.c 12 KB

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