i2c-meson.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * I2C bus driver for Amlogic Meson SoCs
  4. *
  5. * Copyright (C) 2014 Beniamino Galvani <[email protected]>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/completion.h>
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.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 GENMASK(21, REG_CTRL_CLKDIV_SHIFT)
  36. #define REG_CTRL_CLKDIVEXT_SHIFT 28
  37. #define REG_CTRL_CLKDIVEXT_MASK GENMASK(29, REG_CTRL_CLKDIVEXT_SHIFT)
  38. #define REG_SLV_ADDR_MASK GENMASK(7, 0)
  39. #define REG_SLV_SDA_FILTER_MASK GENMASK(10, 8)
  40. #define REG_SLV_SCL_FILTER_MASK GENMASK(13, 11)
  41. #define REG_SLV_SCL_LOW_SHIFT 16
  42. #define REG_SLV_SCL_LOW_MASK GENMASK(27, REG_SLV_SCL_LOW_SHIFT)
  43. #define REG_SLV_SCL_LOW_EN BIT(28)
  44. #define I2C_TIMEOUT_MS 500
  45. #define FILTER_DELAY 15
  46. enum {
  47. TOKEN_END = 0,
  48. TOKEN_START,
  49. TOKEN_SLAVE_ADDR_WRITE,
  50. TOKEN_SLAVE_ADDR_READ,
  51. TOKEN_DATA,
  52. TOKEN_DATA_LAST,
  53. TOKEN_STOP,
  54. };
  55. enum {
  56. STATE_IDLE,
  57. STATE_READ,
  58. STATE_WRITE,
  59. };
  60. /**
  61. * struct meson_i2c - Meson I2C device private data
  62. *
  63. * @adap: I2C adapter instance
  64. * @dev: Pointer to device structure
  65. * @regs: Base address of the device memory mapped registers
  66. * @clk: Pointer to clock structure
  67. * @msg: Pointer to the current I2C message
  68. * @state: Current state in the driver state machine
  69. * @last: Flag set for the last message in the transfer
  70. * @count: Number of bytes to be sent/received in current transfer
  71. * @pos: Current position in the send/receive buffer
  72. * @error: Flag set when an error is received
  73. * @lock: To avoid race conditions between irq handler and xfer code
  74. * @done: Completion used to wait for transfer termination
  75. * @tokens: Sequence of tokens to be written to the device
  76. * @num_tokens: Number of tokens
  77. * @data: Pointer to the controller's platform data
  78. */
  79. struct meson_i2c {
  80. struct i2c_adapter adap;
  81. struct device *dev;
  82. void __iomem *regs;
  83. struct clk *clk;
  84. struct i2c_msg *msg;
  85. int state;
  86. bool last;
  87. int count;
  88. int pos;
  89. int error;
  90. spinlock_t lock;
  91. struct completion done;
  92. u32 tokens[2];
  93. int num_tokens;
  94. const struct meson_i2c_data *data;
  95. };
  96. struct meson_i2c_data {
  97. void (*set_clk_div)(struct meson_i2c *i2c, unsigned int freq);
  98. };
  99. static void meson_i2c_set_mask(struct meson_i2c *i2c, int reg, u32 mask,
  100. u32 val)
  101. {
  102. u32 data;
  103. data = readl(i2c->regs + reg);
  104. data &= ~mask;
  105. data |= val & mask;
  106. writel(data, i2c->regs + reg);
  107. }
  108. static void meson_i2c_reset_tokens(struct meson_i2c *i2c)
  109. {
  110. i2c->tokens[0] = 0;
  111. i2c->tokens[1] = 0;
  112. i2c->num_tokens = 0;
  113. }
  114. static void meson_i2c_add_token(struct meson_i2c *i2c, int token)
  115. {
  116. if (i2c->num_tokens < 8)
  117. i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4);
  118. else
  119. i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4);
  120. i2c->num_tokens++;
  121. }
  122. static void meson_gxbb_axg_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq)
  123. {
  124. unsigned long clk_rate = clk_get_rate(i2c->clk);
  125. unsigned int div_h, div_l;
  126. /* According to I2C-BUS Spec 2.1, in FAST-MODE, the minimum LOW period is 1.3uS, and
  127. * minimum HIGH is least 0.6us.
  128. * For 400000 freq, the period is 2.5us. To keep within the specs, give 40% of period to
  129. * HIGH and 60% to LOW. This means HIGH at 1.0us and LOW 1.5us.
  130. * The same applies for Fast-mode plus, where LOW is 0.5us and HIGH is 0.26us.
  131. * Duty = H/(H + L) = 2/5
  132. */
  133. if (freq <= I2C_MAX_STANDARD_MODE_FREQ) {
  134. div_h = DIV_ROUND_UP(clk_rate, freq);
  135. div_l = DIV_ROUND_UP(div_h, 4);
  136. div_h = DIV_ROUND_UP(div_h, 2) - FILTER_DELAY;
  137. } else {
  138. div_h = DIV_ROUND_UP(clk_rate * 2, freq * 5) - FILTER_DELAY;
  139. div_l = DIV_ROUND_UP(clk_rate * 3, freq * 5 * 2);
  140. }
  141. /* clock divider has 12 bits */
  142. if (div_h > GENMASK(11, 0)) {
  143. dev_err(i2c->dev, "requested bus frequency too low\n");
  144. div_h = GENMASK(11, 0);
  145. }
  146. if (div_l > GENMASK(11, 0)) {
  147. dev_err(i2c->dev, "requested bus frequency too low\n");
  148. div_l = GENMASK(11, 0);
  149. }
  150. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK,
  151. FIELD_PREP(REG_CTRL_CLKDIV_MASK, div_h & GENMASK(9, 0)));
  152. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK,
  153. FIELD_PREP(REG_CTRL_CLKDIVEXT_MASK, div_h >> 10));
  154. /* set SCL low delay */
  155. meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_MASK,
  156. FIELD_PREP(REG_SLV_SCL_LOW_MASK, div_l));
  157. /* Enable HIGH/LOW mode */
  158. meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, REG_SLV_SCL_LOW_EN);
  159. dev_dbg(i2c->dev, "%s: clk %lu, freq %u, divh %u, divl %u\n", __func__,
  160. clk_rate, freq, div_h, div_l);
  161. }
  162. static void meson6_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq)
  163. {
  164. unsigned long clk_rate = clk_get_rate(i2c->clk);
  165. unsigned int div;
  166. div = DIV_ROUND_UP(clk_rate, freq);
  167. div -= FILTER_DELAY;
  168. div = DIV_ROUND_UP(div, 4);
  169. /* clock divider has 12 bits */
  170. if (div > GENMASK(11, 0)) {
  171. dev_err(i2c->dev, "requested bus frequency too low\n");
  172. div = GENMASK(11, 0);
  173. }
  174. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK,
  175. FIELD_PREP(REG_CTRL_CLKDIV_MASK, div & GENMASK(9, 0)));
  176. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK,
  177. FIELD_PREP(REG_CTRL_CLKDIVEXT_MASK, div >> 10));
  178. /* Disable HIGH/LOW mode */
  179. meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, 0);
  180. dev_dbg(i2c->dev, "%s: clk %lu, freq %u, div %u\n", __func__,
  181. clk_rate, freq, div);
  182. }
  183. static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len)
  184. {
  185. u32 rdata0, rdata1;
  186. int i;
  187. rdata0 = readl(i2c->regs + REG_TOK_RDATA0);
  188. rdata1 = readl(i2c->regs + REG_TOK_RDATA1);
  189. dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
  190. rdata0, rdata1, len);
  191. for (i = 0; i < min(4, len); i++)
  192. *buf++ = (rdata0 >> i * 8) & 0xff;
  193. for (i = 4; i < min(8, len); i++)
  194. *buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
  195. }
  196. static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len)
  197. {
  198. u32 wdata0 = 0, wdata1 = 0;
  199. int i;
  200. for (i = 0; i < min(4, len); i++)
  201. wdata0 |= *buf++ << (i * 8);
  202. for (i = 4; i < min(8, len); i++)
  203. wdata1 |= *buf++ << ((i - 4) * 8);
  204. writel(wdata0, i2c->regs + REG_TOK_WDATA0);
  205. writel(wdata1, i2c->regs + REG_TOK_WDATA1);
  206. dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
  207. wdata0, wdata1, len);
  208. }
  209. static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
  210. {
  211. bool write = !(i2c->msg->flags & I2C_M_RD);
  212. int i;
  213. i2c->count = min(i2c->msg->len - i2c->pos, 8);
  214. for (i = 0; i < i2c->count - 1; i++)
  215. meson_i2c_add_token(i2c, TOKEN_DATA);
  216. if (i2c->count) {
  217. if (write || i2c->pos + i2c->count < i2c->msg->len)
  218. meson_i2c_add_token(i2c, TOKEN_DATA);
  219. else
  220. meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
  221. }
  222. if (write)
  223. meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
  224. if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len)
  225. meson_i2c_add_token(i2c, TOKEN_STOP);
  226. writel(i2c->tokens[0], i2c->regs + REG_TOK_LIST0);
  227. writel(i2c->tokens[1], i2c->regs + REG_TOK_LIST1);
  228. }
  229. static void meson_i2c_transfer_complete(struct meson_i2c *i2c, u32 ctrl)
  230. {
  231. if (ctrl & REG_CTRL_ERROR) {
  232. /*
  233. * The bit is set when the IGNORE_NAK bit is cleared
  234. * and the device didn't respond. In this case, the
  235. * I2C controller automatically generates a STOP
  236. * condition.
  237. */
  238. dev_dbg(i2c->dev, "error bit set\n");
  239. i2c->error = -ENXIO;
  240. i2c->state = STATE_IDLE;
  241. } else {
  242. if (i2c->state == STATE_READ && i2c->count)
  243. meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos,
  244. i2c->count);
  245. i2c->pos += i2c->count;
  246. if (i2c->pos >= i2c->msg->len)
  247. i2c->state = STATE_IDLE;
  248. }
  249. }
  250. static irqreturn_t meson_i2c_irq(int irqno, void *dev_id)
  251. {
  252. struct meson_i2c *i2c = dev_id;
  253. unsigned int ctrl;
  254. spin_lock(&i2c->lock);
  255. meson_i2c_reset_tokens(i2c);
  256. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
  257. ctrl = readl(i2c->regs + REG_CTRL);
  258. dev_dbg(i2c->dev, "irq: state %d, pos %d, count %d, ctrl %08x\n",
  259. i2c->state, i2c->pos, i2c->count, ctrl);
  260. if (i2c->state == STATE_IDLE) {
  261. spin_unlock(&i2c->lock);
  262. return IRQ_NONE;
  263. }
  264. meson_i2c_transfer_complete(i2c, ctrl);
  265. if (i2c->state == STATE_IDLE) {
  266. complete(&i2c->done);
  267. goto out;
  268. }
  269. /* Restart the processing */
  270. meson_i2c_prepare_xfer(i2c);
  271. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
  272. out:
  273. spin_unlock(&i2c->lock);
  274. return IRQ_HANDLED;
  275. }
  276. static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
  277. {
  278. int token;
  279. token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
  280. TOKEN_SLAVE_ADDR_WRITE;
  281. meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_ADDR_MASK,
  282. FIELD_PREP(REG_SLV_ADDR_MASK, msg->addr << 1));
  283. meson_i2c_add_token(i2c, TOKEN_START);
  284. meson_i2c_add_token(i2c, token);
  285. }
  286. static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
  287. int last, bool atomic)
  288. {
  289. unsigned long time_left, flags;
  290. int ret = 0;
  291. u32 ctrl;
  292. i2c->msg = msg;
  293. i2c->last = last;
  294. i2c->pos = 0;
  295. i2c->count = 0;
  296. i2c->error = 0;
  297. meson_i2c_reset_tokens(i2c);
  298. flags = (msg->flags & I2C_M_IGNORE_NAK) ? REG_CTRL_ACK_IGNORE : 0;
  299. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_ACK_IGNORE, flags);
  300. if (!(msg->flags & I2C_M_NOSTART))
  301. meson_i2c_do_start(i2c, msg);
  302. i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  303. meson_i2c_prepare_xfer(i2c);
  304. if (!atomic)
  305. reinit_completion(&i2c->done);
  306. /* Start the transfer */
  307. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
  308. if (atomic) {
  309. ret = readl_poll_timeout_atomic(i2c->regs + REG_CTRL, ctrl,
  310. !(ctrl & REG_CTRL_STATUS),
  311. 10, I2C_TIMEOUT_MS * 1000);
  312. } else {
  313. time_left = msecs_to_jiffies(I2C_TIMEOUT_MS);
  314. time_left = wait_for_completion_timeout(&i2c->done, time_left);
  315. if (!time_left)
  316. ret = -ETIMEDOUT;
  317. }
  318. /*
  319. * Protect access to i2c struct and registers from interrupt
  320. * handlers triggered by a transfer terminated after the
  321. * timeout period
  322. */
  323. spin_lock_irqsave(&i2c->lock, flags);
  324. if (atomic && !ret)
  325. meson_i2c_transfer_complete(i2c, ctrl);
  326. /* Abort any active operation */
  327. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
  328. if (ret)
  329. i2c->state = STATE_IDLE;
  330. if (i2c->error)
  331. ret = i2c->error;
  332. spin_unlock_irqrestore(&i2c->lock, flags);
  333. return ret;
  334. }
  335. static int meson_i2c_xfer_messages(struct i2c_adapter *adap,
  336. struct i2c_msg *msgs, int num, bool atomic)
  337. {
  338. struct meson_i2c *i2c = adap->algo_data;
  339. int i, ret = 0;
  340. for (i = 0; i < num; i++) {
  341. ret = meson_i2c_xfer_msg(i2c, msgs + i, i == num - 1, atomic);
  342. if (ret)
  343. break;
  344. }
  345. return ret ?: i;
  346. }
  347. static int meson_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  348. int num)
  349. {
  350. return meson_i2c_xfer_messages(adap, msgs, num, false);
  351. }
  352. static int meson_i2c_xfer_atomic(struct i2c_adapter *adap,
  353. struct i2c_msg *msgs, int num)
  354. {
  355. return meson_i2c_xfer_messages(adap, msgs, num, true);
  356. }
  357. static u32 meson_i2c_func(struct i2c_adapter *adap)
  358. {
  359. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  360. }
  361. static const struct i2c_algorithm meson_i2c_algorithm = {
  362. .master_xfer = meson_i2c_xfer,
  363. .master_xfer_atomic = meson_i2c_xfer_atomic,
  364. .functionality = meson_i2c_func,
  365. };
  366. static int meson_i2c_probe(struct platform_device *pdev)
  367. {
  368. struct device_node *np = pdev->dev.of_node;
  369. struct meson_i2c *i2c;
  370. struct i2c_timings timings;
  371. int irq, ret = 0;
  372. i2c = devm_kzalloc(&pdev->dev, sizeof(struct meson_i2c), GFP_KERNEL);
  373. if (!i2c)
  374. return -ENOMEM;
  375. i2c_parse_fw_timings(&pdev->dev, &timings, true);
  376. i2c->dev = &pdev->dev;
  377. platform_set_drvdata(pdev, i2c);
  378. spin_lock_init(&i2c->lock);
  379. init_completion(&i2c->done);
  380. i2c->data = (const struct meson_i2c_data *)
  381. of_device_get_match_data(&pdev->dev);
  382. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  383. if (IS_ERR(i2c->clk)) {
  384. dev_err(&pdev->dev, "can't get device clock\n");
  385. return PTR_ERR(i2c->clk);
  386. }
  387. i2c->regs = devm_platform_ioremap_resource(pdev, 0);
  388. if (IS_ERR(i2c->regs))
  389. return PTR_ERR(i2c->regs);
  390. irq = platform_get_irq(pdev, 0);
  391. if (irq < 0)
  392. return irq;
  393. ret = devm_request_irq(&pdev->dev, irq, meson_i2c_irq, 0, NULL, i2c);
  394. if (ret < 0) {
  395. dev_err(&pdev->dev, "can't request IRQ\n");
  396. return ret;
  397. }
  398. ret = clk_prepare_enable(i2c->clk);
  399. if (ret < 0) {
  400. dev_err(&pdev->dev, "can't prepare clock\n");
  401. return ret;
  402. }
  403. strscpy(i2c->adap.name, "Meson I2C adapter",
  404. sizeof(i2c->adap.name));
  405. i2c->adap.owner = THIS_MODULE;
  406. i2c->adap.algo = &meson_i2c_algorithm;
  407. i2c->adap.dev.parent = &pdev->dev;
  408. i2c->adap.dev.of_node = np;
  409. i2c->adap.algo_data = i2c;
  410. /*
  411. * A transfer is triggered when START bit changes from 0 to 1.
  412. * Ensure that the bit is set to 0 after probe
  413. */
  414. meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
  415. /* Disable filtering */
  416. meson_i2c_set_mask(i2c, REG_SLAVE_ADDR,
  417. REG_SLV_SDA_FILTER_MASK | REG_SLV_SCL_FILTER_MASK, 0);
  418. if (!i2c->data->set_clk_div) {
  419. clk_disable_unprepare(i2c->clk);
  420. return -EINVAL;
  421. }
  422. i2c->data->set_clk_div(i2c, timings.bus_freq_hz);
  423. ret = i2c_add_adapter(&i2c->adap);
  424. if (ret < 0) {
  425. clk_disable_unprepare(i2c->clk);
  426. return ret;
  427. }
  428. return 0;
  429. }
  430. static int meson_i2c_remove(struct platform_device *pdev)
  431. {
  432. struct meson_i2c *i2c = platform_get_drvdata(pdev);
  433. i2c_del_adapter(&i2c->adap);
  434. clk_disable_unprepare(i2c->clk);
  435. return 0;
  436. }
  437. static const struct meson_i2c_data i2c_meson6_data = {
  438. .set_clk_div = meson6_i2c_set_clk_div,
  439. };
  440. static const struct meson_i2c_data i2c_gxbb_data = {
  441. .set_clk_div = meson_gxbb_axg_i2c_set_clk_div,
  442. };
  443. static const struct meson_i2c_data i2c_axg_data = {
  444. .set_clk_div = meson_gxbb_axg_i2c_set_clk_div,
  445. };
  446. static const struct of_device_id meson_i2c_match[] = {
  447. { .compatible = "amlogic,meson6-i2c", .data = &i2c_meson6_data },
  448. { .compatible = "amlogic,meson-gxbb-i2c", .data = &i2c_gxbb_data },
  449. { .compatible = "amlogic,meson-axg-i2c", .data = &i2c_axg_data },
  450. {},
  451. };
  452. MODULE_DEVICE_TABLE(of, meson_i2c_match);
  453. static struct platform_driver meson_i2c_driver = {
  454. .probe = meson_i2c_probe,
  455. .remove = meson_i2c_remove,
  456. .driver = {
  457. .name = "meson-i2c",
  458. .of_match_table = meson_i2c_match,
  459. },
  460. };
  461. module_platform_driver(meson_i2c_driver);
  462. MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");
  463. MODULE_AUTHOR("Beniamino Galvani <[email protected]>");
  464. MODULE_LICENSE("GPL v2");