i2c-hix5hd2.c 13 KB

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