i2c-hix5hd2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2014 Linaro Ltd.
  4. * Copyright (c) 2014 HiSilicon Limited.
  5. *
  6. * Now only support 7 bit address.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/i2c.h>
  11. #include <linux/io.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. /* Register Map */
  18. #define HIX5I2C_CTRL 0x00
  19. #define HIX5I2C_COM 0x04
  20. #define HIX5I2C_ICR 0x08
  21. #define HIX5I2C_SR 0x0c
  22. #define HIX5I2C_SCL_H 0x10
  23. #define HIX5I2C_SCL_L 0x14
  24. #define HIX5I2C_TXR 0x18
  25. #define HIX5I2C_RXR 0x1c
  26. /* I2C_CTRL_REG */
  27. #define I2C_ENABLE BIT(8)
  28. #define I2C_UNMASK_TOTAL BIT(7)
  29. #define I2C_UNMASK_START BIT(6)
  30. #define I2C_UNMASK_END BIT(5)
  31. #define I2C_UNMASK_SEND BIT(4)
  32. #define I2C_UNMASK_RECEIVE BIT(3)
  33. #define I2C_UNMASK_ACK BIT(2)
  34. #define I2C_UNMASK_ARBITRATE BIT(1)
  35. #define I2C_UNMASK_OVER BIT(0)
  36. #define I2C_UNMASK_ALL (I2C_UNMASK_ACK | I2C_UNMASK_OVER)
  37. /* I2C_COM_REG */
  38. #define I2C_NO_ACK BIT(4)
  39. #define I2C_START BIT(3)
  40. #define I2C_READ BIT(2)
  41. #define I2C_WRITE BIT(1)
  42. #define I2C_STOP BIT(0)
  43. /* I2C_ICR_REG */
  44. #define I2C_CLEAR_START BIT(6)
  45. #define I2C_CLEAR_END BIT(5)
  46. #define I2C_CLEAR_SEND BIT(4)
  47. #define I2C_CLEAR_RECEIVE BIT(3)
  48. #define I2C_CLEAR_ACK BIT(2)
  49. #define I2C_CLEAR_ARBITRATE BIT(1)
  50. #define I2C_CLEAR_OVER BIT(0)
  51. #define I2C_CLEAR_ALL (I2C_CLEAR_START | I2C_CLEAR_END | \
  52. I2C_CLEAR_SEND | I2C_CLEAR_RECEIVE | \
  53. I2C_CLEAR_ACK | I2C_CLEAR_ARBITRATE | \
  54. I2C_CLEAR_OVER)
  55. /* I2C_SR_REG */
  56. #define I2C_BUSY BIT(7)
  57. #define I2C_START_INTR BIT(6)
  58. #define I2C_END_INTR BIT(5)
  59. #define I2C_SEND_INTR BIT(4)
  60. #define I2C_RECEIVE_INTR BIT(3)
  61. #define I2C_ACK_INTR BIT(2)
  62. #define I2C_ARBITRATE_INTR BIT(1)
  63. #define I2C_OVER_INTR BIT(0)
  64. enum hix5hd2_i2c_state {
  65. HIX5I2C_STAT_RW_ERR = -1,
  66. HIX5I2C_STAT_INIT,
  67. HIX5I2C_STAT_RW,
  68. HIX5I2C_STAT_SND_STOP,
  69. HIX5I2C_STAT_RW_SUCCESS,
  70. };
  71. struct hix5hd2_i2c_priv {
  72. struct i2c_adapter adap;
  73. struct i2c_msg *msg;
  74. struct completion msg_complete;
  75. unsigned int msg_idx;
  76. unsigned int msg_len;
  77. int stop;
  78. void __iomem *regs;
  79. struct clk *clk;
  80. struct device *dev;
  81. spinlock_t lock; /* IRQ synchronization */
  82. int err;
  83. unsigned int freq;
  84. enum hix5hd2_i2c_state state;
  85. };
  86. static u32 hix5hd2_i2c_clr_pend_irq(struct hix5hd2_i2c_priv *priv)
  87. {
  88. u32 val = readl_relaxed(priv->regs + HIX5I2C_SR);
  89. writel_relaxed(val, priv->regs + HIX5I2C_ICR);
  90. return val;
  91. }
  92. static void hix5hd2_i2c_clr_all_irq(struct hix5hd2_i2c_priv *priv)
  93. {
  94. writel_relaxed(I2C_CLEAR_ALL, priv->regs + HIX5I2C_ICR);
  95. }
  96. static void hix5hd2_i2c_disable_irq(struct hix5hd2_i2c_priv *priv)
  97. {
  98. writel_relaxed(0, priv->regs + HIX5I2C_CTRL);
  99. }
  100. static void hix5hd2_i2c_enable_irq(struct hix5hd2_i2c_priv *priv)
  101. {
  102. writel_relaxed(I2C_ENABLE | I2C_UNMASK_TOTAL | I2C_UNMASK_ALL,
  103. priv->regs + HIX5I2C_CTRL);
  104. }
  105. static void hix5hd2_i2c_drv_setrate(struct hix5hd2_i2c_priv *priv)
  106. {
  107. u32 rate, val;
  108. u32 scl, sysclock;
  109. /* close all i2c interrupt */
  110. val = readl_relaxed(priv->regs + HIX5I2C_CTRL);
  111. writel_relaxed(val & (~I2C_UNMASK_TOTAL), priv->regs + HIX5I2C_CTRL);
  112. rate = priv->freq;
  113. sysclock = clk_get_rate(priv->clk);
  114. scl = (sysclock / (rate * 2)) / 2 - 1;
  115. writel_relaxed(scl, priv->regs + HIX5I2C_SCL_H);
  116. writel_relaxed(scl, priv->regs + HIX5I2C_SCL_L);
  117. /* restore original interrupt*/
  118. writel_relaxed(val, priv->regs + HIX5I2C_CTRL);
  119. dev_dbg(priv->dev, "%s: sysclock=%d, rate=%d, scl=%d\n",
  120. __func__, sysclock, rate, scl);
  121. }
  122. static void hix5hd2_i2c_init(struct hix5hd2_i2c_priv *priv)
  123. {
  124. hix5hd2_i2c_disable_irq(priv);
  125. hix5hd2_i2c_drv_setrate(priv);
  126. hix5hd2_i2c_clr_all_irq(priv);
  127. hix5hd2_i2c_enable_irq(priv);
  128. }
  129. static void hix5hd2_i2c_reset(struct hix5hd2_i2c_priv *priv)
  130. {
  131. clk_disable_unprepare(priv->clk);
  132. msleep(20);
  133. clk_prepare_enable(priv->clk);
  134. hix5hd2_i2c_init(priv);
  135. }
  136. static int hix5hd2_i2c_wait_bus_idle(struct hix5hd2_i2c_priv *priv)
  137. {
  138. unsigned long stop_time;
  139. u32 int_status;
  140. /* wait for 100 milli seconds for the bus to be idle */
  141. stop_time = jiffies + msecs_to_jiffies(100);
  142. do {
  143. int_status = hix5hd2_i2c_clr_pend_irq(priv);
  144. if (!(int_status & I2C_BUSY))
  145. return 0;
  146. usleep_range(50, 200);
  147. } while (time_before(jiffies, stop_time));
  148. return -EBUSY;
  149. }
  150. static void hix5hd2_rw_over(struct hix5hd2_i2c_priv *priv)
  151. {
  152. if (priv->state == HIX5I2C_STAT_SND_STOP)
  153. dev_dbg(priv->dev, "%s: rw and send stop over\n", __func__);
  154. else
  155. dev_dbg(priv->dev, "%s: have not data to send\n", __func__);
  156. priv->state = HIX5I2C_STAT_RW_SUCCESS;
  157. priv->err = 0;
  158. }
  159. static void hix5hd2_rw_handle_stop(struct hix5hd2_i2c_priv *priv)
  160. {
  161. if (priv->stop) {
  162. priv->state = HIX5I2C_STAT_SND_STOP;
  163. writel_relaxed(I2C_STOP, priv->regs + HIX5I2C_COM);
  164. } else {
  165. hix5hd2_rw_over(priv);
  166. }
  167. }
  168. static void hix5hd2_read_handle(struct hix5hd2_i2c_priv *priv)
  169. {
  170. if (priv->msg_len == 1) {
  171. /* the last byte don't need send ACK */
  172. writel_relaxed(I2C_READ | I2C_NO_ACK, priv->regs + HIX5I2C_COM);
  173. } else if (priv->msg_len > 1) {
  174. /* if i2c master receive data will send ACK */
  175. writel_relaxed(I2C_READ, priv->regs + HIX5I2C_COM);
  176. } else {
  177. hix5hd2_rw_handle_stop(priv);
  178. }
  179. }
  180. static void hix5hd2_write_handle(struct hix5hd2_i2c_priv *priv)
  181. {
  182. u8 data;
  183. if (priv->msg_len > 0) {
  184. data = priv->msg->buf[priv->msg_idx++];
  185. writel_relaxed(data, priv->regs + HIX5I2C_TXR);
  186. writel_relaxed(I2C_WRITE, priv->regs + HIX5I2C_COM);
  187. } else {
  188. hix5hd2_rw_handle_stop(priv);
  189. }
  190. }
  191. static int hix5hd2_rw_preprocess(struct hix5hd2_i2c_priv *priv)
  192. {
  193. u8 data;
  194. if (priv->state == HIX5I2C_STAT_INIT) {
  195. priv->state = HIX5I2C_STAT_RW;
  196. } else if (priv->state == HIX5I2C_STAT_RW) {
  197. if (priv->msg->flags & I2C_M_RD) {
  198. data = readl_relaxed(priv->regs + HIX5I2C_RXR);
  199. priv->msg->buf[priv->msg_idx++] = data;
  200. }
  201. priv->msg_len--;
  202. } else {
  203. dev_dbg(priv->dev, "%s: error: priv->state = %d, msg_len = %d\n",
  204. __func__, priv->state, priv->msg_len);
  205. return -EAGAIN;
  206. }
  207. return 0;
  208. }
  209. static irqreturn_t hix5hd2_i2c_irq(int irqno, void *dev_id)
  210. {
  211. struct hix5hd2_i2c_priv *priv = dev_id;
  212. u32 int_status;
  213. int ret;
  214. spin_lock(&priv->lock);
  215. int_status = hix5hd2_i2c_clr_pend_irq(priv);
  216. /* handle error */
  217. if (int_status & I2C_ARBITRATE_INTR) {
  218. /* bus error */
  219. dev_dbg(priv->dev, "ARB bus loss\n");
  220. priv->err = -EAGAIN;
  221. priv->state = HIX5I2C_STAT_RW_ERR;
  222. goto stop;
  223. } else if (int_status & I2C_ACK_INTR) {
  224. /* ack error */
  225. dev_dbg(priv->dev, "No ACK from device\n");
  226. priv->err = -ENXIO;
  227. priv->state = HIX5I2C_STAT_RW_ERR;
  228. goto stop;
  229. }
  230. if (int_status & I2C_OVER_INTR) {
  231. if (priv->msg_len > 0) {
  232. ret = hix5hd2_rw_preprocess(priv);
  233. if (ret) {
  234. priv->err = ret;
  235. priv->state = HIX5I2C_STAT_RW_ERR;
  236. goto stop;
  237. }
  238. if (priv->msg->flags & I2C_M_RD)
  239. hix5hd2_read_handle(priv);
  240. else
  241. hix5hd2_write_handle(priv);
  242. } else {
  243. hix5hd2_rw_over(priv);
  244. }
  245. }
  246. stop:
  247. if ((priv->state == HIX5I2C_STAT_RW_SUCCESS &&
  248. priv->msg->len == priv->msg_idx) ||
  249. (priv->state == HIX5I2C_STAT_RW_ERR)) {
  250. hix5hd2_i2c_disable_irq(priv);
  251. hix5hd2_i2c_clr_pend_irq(priv);
  252. complete(&priv->msg_complete);
  253. }
  254. spin_unlock(&priv->lock);
  255. return IRQ_HANDLED;
  256. }
  257. static void hix5hd2_i2c_message_start(struct hix5hd2_i2c_priv *priv, int stop)
  258. {
  259. unsigned long flags;
  260. spin_lock_irqsave(&priv->lock, flags);
  261. hix5hd2_i2c_clr_all_irq(priv);
  262. hix5hd2_i2c_enable_irq(priv);
  263. writel_relaxed(i2c_8bit_addr_from_msg(priv->msg),
  264. priv->regs + HIX5I2C_TXR);
  265. writel_relaxed(I2C_WRITE | I2C_START, priv->regs + HIX5I2C_COM);
  266. spin_unlock_irqrestore(&priv->lock, flags);
  267. }
  268. static int hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv *priv,
  269. struct i2c_msg *msgs, int stop)
  270. {
  271. unsigned long timeout;
  272. int ret;
  273. priv->msg = msgs;
  274. priv->msg_idx = 0;
  275. priv->msg_len = priv->msg->len;
  276. priv->stop = stop;
  277. priv->err = 0;
  278. priv->state = HIX5I2C_STAT_INIT;
  279. reinit_completion(&priv->msg_complete);
  280. hix5hd2_i2c_message_start(priv, stop);
  281. timeout = wait_for_completion_timeout(&priv->msg_complete,
  282. priv->adap.timeout);
  283. if (timeout == 0) {
  284. priv->state = HIX5I2C_STAT_RW_ERR;
  285. priv->err = -ETIMEDOUT;
  286. dev_warn(priv->dev, "%s timeout=%d\n",
  287. msgs->flags & I2C_M_RD ? "rx" : "tx",
  288. priv->adap.timeout);
  289. }
  290. ret = priv->state;
  291. /*
  292. * If this is the last message to be transfered (stop == 1)
  293. * Then check if the bus can be brought back to idle.
  294. */
  295. if (priv->state == HIX5I2C_STAT_RW_SUCCESS && stop)
  296. ret = hix5hd2_i2c_wait_bus_idle(priv);
  297. if (ret < 0)
  298. hix5hd2_i2c_reset(priv);
  299. return priv->err;
  300. }
  301. static int hix5hd2_i2c_xfer(struct i2c_adapter *adap,
  302. struct i2c_msg *msgs, int num)
  303. {
  304. struct hix5hd2_i2c_priv *priv = i2c_get_adapdata(adap);
  305. int i, ret, stop;
  306. pm_runtime_get_sync(priv->dev);
  307. for (i = 0; i < num; i++, msgs++) {
  308. stop = (i == num - 1);
  309. ret = hix5hd2_i2c_xfer_msg(priv, msgs, stop);
  310. if (ret < 0)
  311. goto out;
  312. }
  313. ret = num;
  314. out:
  315. pm_runtime_mark_last_busy(priv->dev);
  316. pm_runtime_put_autosuspend(priv->dev);
  317. return ret;
  318. }
  319. static u32 hix5hd2_i2c_func(struct i2c_adapter *adap)
  320. {
  321. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  322. }
  323. static const struct i2c_algorithm hix5hd2_i2c_algorithm = {
  324. .master_xfer = hix5hd2_i2c_xfer,
  325. .functionality = hix5hd2_i2c_func,
  326. };
  327. static int hix5hd2_i2c_probe(struct platform_device *pdev)
  328. {
  329. struct device_node *np = pdev->dev.of_node;
  330. struct hix5hd2_i2c_priv *priv;
  331. unsigned int freq;
  332. int irq, ret;
  333. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  334. if (!priv)
  335. return -ENOMEM;
  336. if (of_property_read_u32(np, "clock-frequency", &freq)) {
  337. /* use 100k as default value */
  338. priv->freq = I2C_MAX_STANDARD_MODE_FREQ;
  339. } else {
  340. if (freq > I2C_MAX_FAST_MODE_FREQ) {
  341. priv->freq = I2C_MAX_FAST_MODE_FREQ;
  342. dev_warn(priv->dev, "use max freq %d instead\n",
  343. I2C_MAX_FAST_MODE_FREQ);
  344. } else {
  345. priv->freq = freq;
  346. }
  347. }
  348. priv->regs = devm_platform_ioremap_resource(pdev, 0);
  349. if (IS_ERR(priv->regs))
  350. return PTR_ERR(priv->regs);
  351. irq = platform_get_irq(pdev, 0);
  352. if (irq < 0)
  353. return irq;
  354. priv->clk = devm_clk_get(&pdev->dev, NULL);
  355. if (IS_ERR(priv->clk)) {
  356. dev_err(&pdev->dev, "cannot get clock\n");
  357. return PTR_ERR(priv->clk);
  358. }
  359. clk_prepare_enable(priv->clk);
  360. strscpy(priv->adap.name, "hix5hd2-i2c", sizeof(priv->adap.name));
  361. priv->dev = &pdev->dev;
  362. priv->adap.owner = THIS_MODULE;
  363. priv->adap.algo = &hix5hd2_i2c_algorithm;
  364. priv->adap.retries = 3;
  365. priv->adap.dev.of_node = np;
  366. priv->adap.algo_data = priv;
  367. priv->adap.dev.parent = &pdev->dev;
  368. i2c_set_adapdata(&priv->adap, priv);
  369. platform_set_drvdata(pdev, priv);
  370. spin_lock_init(&priv->lock);
  371. init_completion(&priv->msg_complete);
  372. hix5hd2_i2c_init(priv);
  373. ret = devm_request_irq(&pdev->dev, irq, hix5hd2_i2c_irq,
  374. IRQF_NO_SUSPEND, dev_name(&pdev->dev), priv);
  375. if (ret != 0) {
  376. dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", irq);
  377. goto err_clk;
  378. }
  379. pm_runtime_set_autosuspend_delay(priv->dev, MSEC_PER_SEC);
  380. pm_runtime_use_autosuspend(priv->dev);
  381. pm_runtime_set_active(priv->dev);
  382. pm_runtime_enable(priv->dev);
  383. ret = i2c_add_adapter(&priv->adap);
  384. if (ret < 0)
  385. goto err_runtime;
  386. return ret;
  387. err_runtime:
  388. pm_runtime_disable(priv->dev);
  389. pm_runtime_set_suspended(priv->dev);
  390. err_clk:
  391. clk_disable_unprepare(priv->clk);
  392. return ret;
  393. }
  394. static int hix5hd2_i2c_remove(struct platform_device *pdev)
  395. {
  396. struct hix5hd2_i2c_priv *priv = platform_get_drvdata(pdev);
  397. i2c_del_adapter(&priv->adap);
  398. pm_runtime_disable(priv->dev);
  399. pm_runtime_set_suspended(priv->dev);
  400. clk_disable_unprepare(priv->clk);
  401. return 0;
  402. }
  403. #ifdef CONFIG_PM
  404. static int hix5hd2_i2c_runtime_suspend(struct device *dev)
  405. {
  406. struct hix5hd2_i2c_priv *priv = dev_get_drvdata(dev);
  407. clk_disable_unprepare(priv->clk);
  408. return 0;
  409. }
  410. static int hix5hd2_i2c_runtime_resume(struct device *dev)
  411. {
  412. struct hix5hd2_i2c_priv *priv = dev_get_drvdata(dev);
  413. clk_prepare_enable(priv->clk);
  414. hix5hd2_i2c_init(priv);
  415. return 0;
  416. }
  417. #endif
  418. static const struct dev_pm_ops hix5hd2_i2c_pm_ops = {
  419. SET_RUNTIME_PM_OPS(hix5hd2_i2c_runtime_suspend,
  420. hix5hd2_i2c_runtime_resume,
  421. NULL)
  422. };
  423. static const struct of_device_id hix5hd2_i2c_match[] = {
  424. { .compatible = "hisilicon,hix5hd2-i2c" },
  425. {},
  426. };
  427. MODULE_DEVICE_TABLE(of, hix5hd2_i2c_match);
  428. static struct platform_driver hix5hd2_i2c_driver = {
  429. .probe = hix5hd2_i2c_probe,
  430. .remove = hix5hd2_i2c_remove,
  431. .driver = {
  432. .name = "hix5hd2-i2c",
  433. .pm = &hix5hd2_i2c_pm_ops,
  434. .of_match_table = hix5hd2_i2c_match,
  435. },
  436. };
  437. module_platform_driver(hix5hd2_i2c_driver);
  438. MODULE_DESCRIPTION("Hix5hd2 I2C Bus driver");
  439. MODULE_AUTHOR("Wei Yan <[email protected]>");
  440. MODULE_LICENSE("GPL");
  441. MODULE_ALIAS("platform:hix5hd2-i2c");