i2c-zx2967.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Copyright (C) 2017 Sanechips Technology Co., Ltd.
  3. * Copyright 2017 Linaro Ltd.
  4. *
  5. * Author: Baoyou Xie <baoyou.xie@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/i2c.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #define REG_CMD 0x04
  18. #define REG_DEVADDR_H 0x0C
  19. #define REG_DEVADDR_L 0x10
  20. #define REG_CLK_DIV_FS 0x14
  21. #define REG_CLK_DIV_HS 0x18
  22. #define REG_WRCONF 0x1C
  23. #define REG_RDCONF 0x20
  24. #define REG_DATA 0x24
  25. #define REG_STAT 0x28
  26. #define I2C_STOP 0
  27. #define I2C_MASTER BIT(0)
  28. #define I2C_ADDR_MODE_TEN BIT(1)
  29. #define I2C_IRQ_MSK_ENABLE BIT(3)
  30. #define I2C_RW_READ BIT(4)
  31. #define I2C_CMB_RW_EN BIT(5)
  32. #define I2C_START BIT(6)
  33. #define I2C_ADDR_LOW_MASK GENMASK(6, 0)
  34. #define I2C_ADDR_LOW_SHIFT 0
  35. #define I2C_ADDR_HI_MASK GENMASK(2, 0)
  36. #define I2C_ADDR_HI_SHIFT 7
  37. #define I2C_WFIFO_RESET BIT(7)
  38. #define I2C_RFIFO_RESET BIT(7)
  39. #define I2C_IRQ_ACK_CLEAR BIT(7)
  40. #define I2C_INT_MASK GENMASK(6, 0)
  41. #define I2C_TRANS_DONE BIT(0)
  42. #define I2C_SR_EDEVICE BIT(1)
  43. #define I2C_SR_EDATA BIT(2)
  44. #define I2C_FIFO_MAX 16
  45. #define I2C_TIMEOUT msecs_to_jiffies(1000)
  46. #define DEV(i2c) ((i2c)->adap.dev.parent)
  47. struct zx2967_i2c {
  48. struct i2c_adapter adap;
  49. struct clk *clk;
  50. struct completion complete;
  51. u32 clk_freq;
  52. void __iomem *reg_base;
  53. size_t residue;
  54. int irq;
  55. int msg_rd;
  56. u8 *cur_trans;
  57. u8 access_cnt;
  58. bool is_suspended;
  59. int error;
  60. };
  61. static void zx2967_i2c_writel(struct zx2967_i2c *i2c,
  62. u32 val, unsigned long reg)
  63. {
  64. writel_relaxed(val, i2c->reg_base + reg);
  65. }
  66. static u32 zx2967_i2c_readl(struct zx2967_i2c *i2c, unsigned long reg)
  67. {
  68. return readl_relaxed(i2c->reg_base + reg);
  69. }
  70. static void zx2967_i2c_writesb(struct zx2967_i2c *i2c,
  71. void *data, unsigned long reg, int len)
  72. {
  73. writesb(i2c->reg_base + reg, data, len);
  74. }
  75. static void zx2967_i2c_readsb(struct zx2967_i2c *i2c,
  76. void *data, unsigned long reg, int len)
  77. {
  78. readsb(i2c->reg_base + reg, data, len);
  79. }
  80. static void zx2967_i2c_start_ctrl(struct zx2967_i2c *i2c)
  81. {
  82. u32 status;
  83. u32 ctl;
  84. status = zx2967_i2c_readl(i2c, REG_STAT);
  85. status |= I2C_IRQ_ACK_CLEAR;
  86. zx2967_i2c_writel(i2c, status, REG_STAT);
  87. ctl = zx2967_i2c_readl(i2c, REG_CMD);
  88. if (i2c->msg_rd)
  89. ctl |= I2C_RW_READ;
  90. else
  91. ctl &= ~I2C_RW_READ;
  92. ctl &= ~I2C_CMB_RW_EN;
  93. ctl |= I2C_START;
  94. zx2967_i2c_writel(i2c, ctl, REG_CMD);
  95. }
  96. static void zx2967_i2c_flush_fifos(struct zx2967_i2c *i2c)
  97. {
  98. u32 offset;
  99. u32 val;
  100. if (i2c->msg_rd) {
  101. offset = REG_RDCONF;
  102. val = I2C_RFIFO_RESET;
  103. } else {
  104. offset = REG_WRCONF;
  105. val = I2C_WFIFO_RESET;
  106. }
  107. val |= zx2967_i2c_readl(i2c, offset);
  108. zx2967_i2c_writel(i2c, val, offset);
  109. }
  110. static int zx2967_i2c_empty_rx_fifo(struct zx2967_i2c *i2c, u32 size)
  111. {
  112. u8 val[I2C_FIFO_MAX] = {0};
  113. int i;
  114. if (size > I2C_FIFO_MAX) {
  115. dev_err(DEV(i2c), "fifo size %d over the max value %d\n",
  116. size, I2C_FIFO_MAX);
  117. return -EINVAL;
  118. }
  119. zx2967_i2c_readsb(i2c, val, REG_DATA, size);
  120. for (i = 0; i < size; i++) {
  121. *i2c->cur_trans++ = val[i];
  122. i2c->residue--;
  123. }
  124. barrier();
  125. return 0;
  126. }
  127. static int zx2967_i2c_fill_tx_fifo(struct zx2967_i2c *i2c)
  128. {
  129. size_t residue = i2c->residue;
  130. u8 *buf = i2c->cur_trans;
  131. if (residue == 0) {
  132. dev_err(DEV(i2c), "residue is %d\n", (int)residue);
  133. return -EINVAL;
  134. }
  135. if (residue <= I2C_FIFO_MAX) {
  136. zx2967_i2c_writesb(i2c, buf, REG_DATA, residue);
  137. /* Again update before writing to FIFO to make sure isr sees. */
  138. i2c->residue = 0;
  139. i2c->cur_trans = NULL;
  140. } else {
  141. zx2967_i2c_writesb(i2c, buf, REG_DATA, I2C_FIFO_MAX);
  142. i2c->residue -= I2C_FIFO_MAX;
  143. i2c->cur_trans += I2C_FIFO_MAX;
  144. }
  145. barrier();
  146. return 0;
  147. }
  148. static int zx2967_i2c_reset_hardware(struct zx2967_i2c *i2c)
  149. {
  150. u32 val;
  151. u32 clk_div;
  152. val = I2C_MASTER | I2C_IRQ_MSK_ENABLE;
  153. zx2967_i2c_writel(i2c, val, REG_CMD);
  154. clk_div = clk_get_rate(i2c->clk) / i2c->clk_freq - 1;
  155. zx2967_i2c_writel(i2c, clk_div, REG_CLK_DIV_FS);
  156. zx2967_i2c_writel(i2c, clk_div, REG_CLK_DIV_HS);
  157. zx2967_i2c_writel(i2c, I2C_FIFO_MAX - 1, REG_WRCONF);
  158. zx2967_i2c_writel(i2c, I2C_FIFO_MAX - 1, REG_RDCONF);
  159. zx2967_i2c_writel(i2c, 1, REG_RDCONF);
  160. zx2967_i2c_flush_fifos(i2c);
  161. return 0;
  162. }
  163. static void zx2967_i2c_isr_clr(struct zx2967_i2c *i2c)
  164. {
  165. u32 status;
  166. status = zx2967_i2c_readl(i2c, REG_STAT);
  167. status |= I2C_IRQ_ACK_CLEAR;
  168. zx2967_i2c_writel(i2c, status, REG_STAT);
  169. }
  170. static irqreturn_t zx2967_i2c_isr(int irq, void *dev_id)
  171. {
  172. u32 status;
  173. struct zx2967_i2c *i2c = (struct zx2967_i2c *)dev_id;
  174. status = zx2967_i2c_readl(i2c, REG_STAT) & I2C_INT_MASK;
  175. zx2967_i2c_isr_clr(i2c);
  176. if (status & I2C_SR_EDEVICE)
  177. i2c->error = -ENXIO;
  178. else if (status & I2C_SR_EDATA)
  179. i2c->error = -EIO;
  180. else if (status & I2C_TRANS_DONE)
  181. i2c->error = 0;
  182. else
  183. goto done;
  184. complete(&i2c->complete);
  185. done:
  186. return IRQ_HANDLED;
  187. }
  188. static void zx2967_set_addr(struct zx2967_i2c *i2c, u16 addr)
  189. {
  190. u16 val;
  191. val = (addr >> I2C_ADDR_LOW_SHIFT) & I2C_ADDR_LOW_MASK;
  192. zx2967_i2c_writel(i2c, val, REG_DEVADDR_L);
  193. val = (addr >> I2C_ADDR_HI_SHIFT) & I2C_ADDR_HI_MASK;
  194. zx2967_i2c_writel(i2c, val, REG_DEVADDR_H);
  195. if (val)
  196. val = zx2967_i2c_readl(i2c, REG_CMD) | I2C_ADDR_MODE_TEN;
  197. else
  198. val = zx2967_i2c_readl(i2c, REG_CMD) & ~I2C_ADDR_MODE_TEN;
  199. zx2967_i2c_writel(i2c, val, REG_CMD);
  200. }
  201. static int zx2967_i2c_xfer_bytes(struct zx2967_i2c *i2c, u32 bytes)
  202. {
  203. unsigned long time_left;
  204. int rd = i2c->msg_rd;
  205. int ret;
  206. reinit_completion(&i2c->complete);
  207. if (rd) {
  208. zx2967_i2c_writel(i2c, bytes - 1, REG_RDCONF);
  209. } else {
  210. ret = zx2967_i2c_fill_tx_fifo(i2c);
  211. if (ret)
  212. return ret;
  213. }
  214. zx2967_i2c_start_ctrl(i2c);
  215. time_left = wait_for_completion_timeout(&i2c->complete,
  216. I2C_TIMEOUT);
  217. if (time_left == 0)
  218. return -ETIMEDOUT;
  219. if (i2c->error)
  220. return i2c->error;
  221. return rd ? zx2967_i2c_empty_rx_fifo(i2c, bytes) : 0;
  222. }
  223. static int zx2967_i2c_xfer_msg(struct zx2967_i2c *i2c,
  224. struct i2c_msg *msg)
  225. {
  226. int ret;
  227. int i;
  228. zx2967_i2c_flush_fifos(i2c);
  229. i2c->cur_trans = msg->buf;
  230. i2c->residue = msg->len;
  231. i2c->access_cnt = msg->len / I2C_FIFO_MAX;
  232. i2c->msg_rd = msg->flags & I2C_M_RD;
  233. for (i = 0; i < i2c->access_cnt; i++) {
  234. ret = zx2967_i2c_xfer_bytes(i2c, I2C_FIFO_MAX);
  235. if (ret)
  236. return ret;
  237. }
  238. if (i2c->residue > 0) {
  239. ret = zx2967_i2c_xfer_bytes(i2c, i2c->residue);
  240. if (ret)
  241. return ret;
  242. }
  243. i2c->residue = 0;
  244. i2c->access_cnt = 0;
  245. return 0;
  246. }
  247. static int zx2967_i2c_xfer(struct i2c_adapter *adap,
  248. struct i2c_msg *msgs, int num)
  249. {
  250. struct zx2967_i2c *i2c = i2c_get_adapdata(adap);
  251. int ret;
  252. int i;
  253. if (i2c->is_suspended)
  254. return -EBUSY;
  255. zx2967_set_addr(i2c, msgs->addr);
  256. for (i = 0; i < num; i++) {
  257. ret = zx2967_i2c_xfer_msg(i2c, &msgs[i]);
  258. if (ret)
  259. return ret;
  260. }
  261. return num;
  262. }
  263. static void
  264. zx2967_smbus_xfer_prepare(struct zx2967_i2c *i2c, u16 addr,
  265. char read_write, u8 command, int size,
  266. union i2c_smbus_data *data)
  267. {
  268. u32 val;
  269. val = zx2967_i2c_readl(i2c, REG_RDCONF);
  270. val |= I2C_RFIFO_RESET;
  271. zx2967_i2c_writel(i2c, val, REG_RDCONF);
  272. zx2967_set_addr(i2c, addr);
  273. val = zx2967_i2c_readl(i2c, REG_CMD);
  274. val &= ~I2C_RW_READ;
  275. zx2967_i2c_writel(i2c, val, REG_CMD);
  276. switch (size) {
  277. case I2C_SMBUS_BYTE:
  278. zx2967_i2c_writel(i2c, command, REG_DATA);
  279. break;
  280. case I2C_SMBUS_BYTE_DATA:
  281. zx2967_i2c_writel(i2c, command, REG_DATA);
  282. if (read_write == I2C_SMBUS_WRITE)
  283. zx2967_i2c_writel(i2c, data->byte, REG_DATA);
  284. break;
  285. case I2C_SMBUS_WORD_DATA:
  286. zx2967_i2c_writel(i2c, command, REG_DATA);
  287. if (read_write == I2C_SMBUS_WRITE) {
  288. zx2967_i2c_writel(i2c, (data->word >> 8), REG_DATA);
  289. zx2967_i2c_writel(i2c, (data->word & 0xff),
  290. REG_DATA);
  291. }
  292. break;
  293. }
  294. }
  295. static int zx2967_smbus_xfer_read(struct zx2967_i2c *i2c, int size,
  296. union i2c_smbus_data *data)
  297. {
  298. unsigned long time_left;
  299. u8 buf[2];
  300. u32 val;
  301. reinit_completion(&i2c->complete);
  302. val = zx2967_i2c_readl(i2c, REG_CMD);
  303. val |= I2C_CMB_RW_EN;
  304. zx2967_i2c_writel(i2c, val, REG_CMD);
  305. val = zx2967_i2c_readl(i2c, REG_CMD);
  306. val |= I2C_START;
  307. zx2967_i2c_writel(i2c, val, REG_CMD);
  308. time_left = wait_for_completion_timeout(&i2c->complete,
  309. I2C_TIMEOUT);
  310. if (time_left == 0)
  311. return -ETIMEDOUT;
  312. if (i2c->error)
  313. return i2c->error;
  314. switch (size) {
  315. case I2C_SMBUS_BYTE:
  316. case I2C_SMBUS_BYTE_DATA:
  317. val = zx2967_i2c_readl(i2c, REG_DATA);
  318. data->byte = val;
  319. break;
  320. case I2C_SMBUS_WORD_DATA:
  321. case I2C_SMBUS_PROC_CALL:
  322. buf[0] = zx2967_i2c_readl(i2c, REG_DATA);
  323. buf[1] = zx2967_i2c_readl(i2c, REG_DATA);
  324. data->word = (buf[0] << 8) | buf[1];
  325. break;
  326. default:
  327. return -EOPNOTSUPP;
  328. }
  329. return 0;
  330. }
  331. static int zx2967_smbus_xfer_write(struct zx2967_i2c *i2c)
  332. {
  333. unsigned long time_left;
  334. u32 val;
  335. reinit_completion(&i2c->complete);
  336. val = zx2967_i2c_readl(i2c, REG_CMD);
  337. val |= I2C_START;
  338. zx2967_i2c_writel(i2c, val, REG_CMD);
  339. time_left = wait_for_completion_timeout(&i2c->complete,
  340. I2C_TIMEOUT);
  341. if (time_left == 0)
  342. return -ETIMEDOUT;
  343. if (i2c->error)
  344. return i2c->error;
  345. return 0;
  346. }
  347. static int zx2967_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  348. unsigned short flags, char read_write,
  349. u8 command, int size, union i2c_smbus_data *data)
  350. {
  351. struct zx2967_i2c *i2c = i2c_get_adapdata(adap);
  352. if (size == I2C_SMBUS_QUICK)
  353. read_write = I2C_SMBUS_WRITE;
  354. switch (size) {
  355. case I2C_SMBUS_QUICK:
  356. case I2C_SMBUS_BYTE:
  357. case I2C_SMBUS_BYTE_DATA:
  358. case I2C_SMBUS_WORD_DATA:
  359. zx2967_smbus_xfer_prepare(i2c, addr, read_write,
  360. command, size, data);
  361. break;
  362. default:
  363. return -EOPNOTSUPP;
  364. }
  365. if (read_write == I2C_SMBUS_READ)
  366. return zx2967_smbus_xfer_read(i2c, size, data);
  367. return zx2967_smbus_xfer_write(i2c);
  368. }
  369. static u32 zx2967_i2c_func(struct i2c_adapter *adap)
  370. {
  371. return I2C_FUNC_I2C |
  372. I2C_FUNC_SMBUS_QUICK |
  373. I2C_FUNC_SMBUS_BYTE |
  374. I2C_FUNC_SMBUS_BYTE_DATA |
  375. I2C_FUNC_SMBUS_WORD_DATA |
  376. I2C_FUNC_SMBUS_BLOCK_DATA |
  377. I2C_FUNC_SMBUS_PROC_CALL |
  378. I2C_FUNC_SMBUS_I2C_BLOCK;
  379. }
  380. static int __maybe_unused zx2967_i2c_suspend(struct device *dev)
  381. {
  382. struct zx2967_i2c *i2c = dev_get_drvdata(dev);
  383. i2c->is_suspended = true;
  384. clk_disable_unprepare(i2c->clk);
  385. return 0;
  386. }
  387. static int __maybe_unused zx2967_i2c_resume(struct device *dev)
  388. {
  389. struct zx2967_i2c *i2c = dev_get_drvdata(dev);
  390. i2c->is_suspended = false;
  391. clk_prepare_enable(i2c->clk);
  392. return 0;
  393. }
  394. static SIMPLE_DEV_PM_OPS(zx2967_i2c_dev_pm_ops,
  395. zx2967_i2c_suspend, zx2967_i2c_resume);
  396. static const struct i2c_algorithm zx2967_i2c_algo = {
  397. .master_xfer = zx2967_i2c_xfer,
  398. .smbus_xfer = zx2967_smbus_xfer,
  399. .functionality = zx2967_i2c_func,
  400. };
  401. static const struct i2c_adapter_quirks zx2967_i2c_quirks = {
  402. .flags = I2C_AQ_NO_ZERO_LEN,
  403. };
  404. static const struct of_device_id zx2967_i2c_of_match[] = {
  405. { .compatible = "zte,zx296718-i2c", },
  406. { },
  407. };
  408. MODULE_DEVICE_TABLE(of, zx2967_i2c_of_match);
  409. static int zx2967_i2c_probe(struct platform_device *pdev)
  410. {
  411. struct zx2967_i2c *i2c;
  412. void __iomem *reg_base;
  413. struct resource *res;
  414. struct clk *clk;
  415. int ret;
  416. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  417. if (!i2c)
  418. return -ENOMEM;
  419. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  420. reg_base = devm_ioremap_resource(&pdev->dev, res);
  421. if (IS_ERR(reg_base))
  422. return PTR_ERR(reg_base);
  423. clk = devm_clk_get(&pdev->dev, NULL);
  424. if (IS_ERR(clk)) {
  425. dev_err(&pdev->dev, "missing controller clock");
  426. return PTR_ERR(clk);
  427. }
  428. ret = clk_prepare_enable(clk);
  429. if (ret) {
  430. dev_err(&pdev->dev, "failed to enable i2c_clk\n");
  431. return ret;
  432. }
  433. ret = device_property_read_u32(&pdev->dev, "clock-frequency",
  434. &i2c->clk_freq);
  435. if (ret) {
  436. dev_err(&pdev->dev, "missing clock-frequency");
  437. return ret;
  438. }
  439. ret = platform_get_irq(pdev, 0);
  440. if (ret < 0)
  441. return ret;
  442. i2c->irq = ret;
  443. i2c->reg_base = reg_base;
  444. i2c->clk = clk;
  445. init_completion(&i2c->complete);
  446. platform_set_drvdata(pdev, i2c);
  447. ret = zx2967_i2c_reset_hardware(i2c);
  448. if (ret) {
  449. dev_err(&pdev->dev, "failed to initialize i2c controller\n");
  450. goto err_clk_unprepare;
  451. }
  452. ret = devm_request_irq(&pdev->dev, i2c->irq,
  453. zx2967_i2c_isr, 0, dev_name(&pdev->dev), i2c);
  454. if (ret) {
  455. dev_err(&pdev->dev, "failed to request irq %i\n", i2c->irq);
  456. goto err_clk_unprepare;
  457. }
  458. i2c_set_adapdata(&i2c->adap, i2c);
  459. strlcpy(i2c->adap.name, "zx2967 i2c adapter",
  460. sizeof(i2c->adap.name));
  461. i2c->adap.algo = &zx2967_i2c_algo;
  462. i2c->adap.quirks = &zx2967_i2c_quirks;
  463. i2c->adap.nr = pdev->id;
  464. i2c->adap.dev.parent = &pdev->dev;
  465. i2c->adap.dev.of_node = pdev->dev.of_node;
  466. ret = i2c_add_numbered_adapter(&i2c->adap);
  467. if (ret)
  468. goto err_clk_unprepare;
  469. return 0;
  470. err_clk_unprepare:
  471. clk_disable_unprepare(i2c->clk);
  472. return ret;
  473. }
  474. static int zx2967_i2c_remove(struct platform_device *pdev)
  475. {
  476. struct zx2967_i2c *i2c = platform_get_drvdata(pdev);
  477. i2c_del_adapter(&i2c->adap);
  478. clk_disable_unprepare(i2c->clk);
  479. return 0;
  480. }
  481. static struct platform_driver zx2967_i2c_driver = {
  482. .probe = zx2967_i2c_probe,
  483. .remove = zx2967_i2c_remove,
  484. .driver = {
  485. .name = "zx2967_i2c",
  486. .of_match_table = zx2967_i2c_of_match,
  487. .pm = &zx2967_i2c_dev_pm_ops,
  488. },
  489. };
  490. module_platform_driver(zx2967_i2c_driver);
  491. MODULE_AUTHOR("Baoyou Xie <baoyou.xie@linaro.org>");
  492. MODULE_DESCRIPTION("ZTE ZX2967 I2C Bus Controller driver");
  493. MODULE_LICENSE("GPL v2");