i2c-hix5hd2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. #define HIX5I2C_READ_OPERATION 0x01
  70. enum hix5hd2_i2c_state {
  71. HIX5I2C_STAT_RW_ERR = -1,
  72. HIX5I2C_STAT_INIT,
  73. HIX5I2C_STAT_RW,
  74. HIX5I2C_STAT_SND_STOP,
  75. HIX5I2C_STAT_RW_SUCCESS,
  76. };
  77. struct hix5hd2_i2c_priv {
  78. struct i2c_adapter adap;
  79. struct i2c_msg *msg;
  80. struct completion msg_complete;
  81. unsigned int msg_idx;
  82. unsigned int msg_len;
  83. int stop;
  84. void __iomem *regs;
  85. struct clk *clk;
  86. struct device *dev;
  87. spinlock_t lock; /* IRQ synchronization */
  88. int err;
  89. unsigned int freq;
  90. enum hix5hd2_i2c_state state;
  91. };
  92. static u32 hix5hd2_i2c_clr_pend_irq(struct hix5hd2_i2c_priv *priv)
  93. {
  94. u32 val = readl_relaxed(priv->regs + HIX5I2C_SR);
  95. writel_relaxed(val, priv->regs + HIX5I2C_ICR);
  96. return val;
  97. }
  98. static void hix5hd2_i2c_clr_all_irq(struct hix5hd2_i2c_priv *priv)
  99. {
  100. writel_relaxed(I2C_CLEAR_ALL, priv->regs + HIX5I2C_ICR);
  101. }
  102. static void hix5hd2_i2c_disable_irq(struct hix5hd2_i2c_priv *priv)
  103. {
  104. writel_relaxed(0, priv->regs + HIX5I2C_CTRL);
  105. }
  106. static void hix5hd2_i2c_enable_irq(struct hix5hd2_i2c_priv *priv)
  107. {
  108. writel_relaxed(I2C_ENABLE | I2C_UNMASK_TOTAL | I2C_UNMASK_ALL,
  109. priv->regs + HIX5I2C_CTRL);
  110. }
  111. static void hix5hd2_i2c_drv_setrate(struct hix5hd2_i2c_priv *priv)
  112. {
  113. u32 rate, val;
  114. u32 scl, sysclock;
  115. /* close all i2c interrupt */
  116. val = readl_relaxed(priv->regs + HIX5I2C_CTRL);
  117. writel_relaxed(val & (~I2C_UNMASK_TOTAL), priv->regs + HIX5I2C_CTRL);
  118. rate = priv->freq;
  119. sysclock = clk_get_rate(priv->clk);
  120. scl = (sysclock / (rate * 2)) / 2 - 1;
  121. writel_relaxed(scl, priv->regs + HIX5I2C_SCL_H);
  122. writel_relaxed(scl, priv->regs + HIX5I2C_SCL_L);
  123. /* restore original interrupt*/
  124. writel_relaxed(val, priv->regs + HIX5I2C_CTRL);
  125. dev_dbg(priv->dev, "%s: sysclock=%d, rate=%d, scl=%d\n",
  126. __func__, sysclock, rate, scl);
  127. }
  128. static void hix5hd2_i2c_init(struct hix5hd2_i2c_priv *priv)
  129. {
  130. hix5hd2_i2c_disable_irq(priv);
  131. hix5hd2_i2c_drv_setrate(priv);
  132. hix5hd2_i2c_clr_all_irq(priv);
  133. hix5hd2_i2c_enable_irq(priv);
  134. }
  135. static void hix5hd2_i2c_reset(struct hix5hd2_i2c_priv *priv)
  136. {
  137. clk_disable_unprepare(priv->clk);
  138. msleep(20);
  139. clk_prepare_enable(priv->clk);
  140. hix5hd2_i2c_init(priv);
  141. }
  142. static int hix5hd2_i2c_wait_bus_idle(struct hix5hd2_i2c_priv *priv)
  143. {
  144. unsigned long stop_time;
  145. u32 int_status;
  146. /* wait for 100 milli seconds for the bus to be idle */
  147. stop_time = jiffies + msecs_to_jiffies(100);
  148. do {
  149. int_status = hix5hd2_i2c_clr_pend_irq(priv);
  150. if (!(int_status & I2C_BUSY))
  151. return 0;
  152. usleep_range(50, 200);
  153. } while (time_before(jiffies, stop_time));
  154. return -EBUSY;
  155. }
  156. static void hix5hd2_rw_over(struct hix5hd2_i2c_priv *priv)
  157. {
  158. if (priv->state == HIX5I2C_STAT_SND_STOP)
  159. dev_dbg(priv->dev, "%s: rw and send stop over\n", __func__);
  160. else
  161. dev_dbg(priv->dev, "%s: have not data to send\n", __func__);
  162. priv->state = HIX5I2C_STAT_RW_SUCCESS;
  163. priv->err = 0;
  164. }
  165. static void hix5hd2_rw_handle_stop(struct hix5hd2_i2c_priv *priv)
  166. {
  167. if (priv->stop) {
  168. priv->state = HIX5I2C_STAT_SND_STOP;
  169. writel_relaxed(I2C_STOP, priv->regs + HIX5I2C_COM);
  170. } else {
  171. hix5hd2_rw_over(priv);
  172. }
  173. }
  174. static void hix5hd2_read_handle(struct hix5hd2_i2c_priv *priv)
  175. {
  176. if (priv->msg_len == 1) {
  177. /* the last byte don't need send ACK */
  178. writel_relaxed(I2C_READ | I2C_NO_ACK, priv->regs + HIX5I2C_COM);
  179. } else if (priv->msg_len > 1) {
  180. /* if i2c master receive data will send ACK */
  181. writel_relaxed(I2C_READ, priv->regs + HIX5I2C_COM);
  182. } else {
  183. hix5hd2_rw_handle_stop(priv);
  184. }
  185. }
  186. static void hix5hd2_write_handle(struct hix5hd2_i2c_priv *priv)
  187. {
  188. u8 data;
  189. if (priv->msg_len > 0) {
  190. data = priv->msg->buf[priv->msg_idx++];
  191. writel_relaxed(data, priv->regs + HIX5I2C_TXR);
  192. writel_relaxed(I2C_WRITE, priv->regs + HIX5I2C_COM);
  193. } else {
  194. hix5hd2_rw_handle_stop(priv);
  195. }
  196. }
  197. static int hix5hd2_rw_preprocess(struct hix5hd2_i2c_priv *priv)
  198. {
  199. u8 data;
  200. if (priv->state == HIX5I2C_STAT_INIT) {
  201. priv->state = HIX5I2C_STAT_RW;
  202. } else if (priv->state == HIX5I2C_STAT_RW) {
  203. if (priv->msg->flags & I2C_M_RD) {
  204. data = readl_relaxed(priv->regs + HIX5I2C_RXR);
  205. priv->msg->buf[priv->msg_idx++] = data;
  206. }
  207. priv->msg_len--;
  208. } else {
  209. dev_dbg(priv->dev, "%s: error: priv->state = %d, msg_len = %d\n",
  210. __func__, priv->state, priv->msg_len);
  211. return -EAGAIN;
  212. }
  213. return 0;
  214. }
  215. static irqreturn_t hix5hd2_i2c_irq(int irqno, void *dev_id)
  216. {
  217. struct hix5hd2_i2c_priv *priv = dev_id;
  218. u32 int_status;
  219. int ret;
  220. spin_lock(&priv->lock);
  221. int_status = hix5hd2_i2c_clr_pend_irq(priv);
  222. /* handle error */
  223. if (int_status & I2C_ARBITRATE_INTR) {
  224. /* bus error */
  225. dev_dbg(priv->dev, "ARB bus loss\n");
  226. priv->err = -EAGAIN;
  227. priv->state = HIX5I2C_STAT_RW_ERR;
  228. goto stop;
  229. } else if (int_status & I2C_ACK_INTR) {
  230. /* ack error */
  231. dev_dbg(priv->dev, "No ACK from device\n");
  232. priv->err = -ENXIO;
  233. priv->state = HIX5I2C_STAT_RW_ERR;
  234. goto stop;
  235. }
  236. if (int_status & I2C_OVER_INTR) {
  237. if (priv->msg_len > 0) {
  238. ret = hix5hd2_rw_preprocess(priv);
  239. if (ret) {
  240. priv->err = ret;
  241. priv->state = HIX5I2C_STAT_RW_ERR;
  242. goto stop;
  243. }
  244. if (priv->msg->flags & I2C_M_RD)
  245. hix5hd2_read_handle(priv);
  246. else
  247. hix5hd2_write_handle(priv);
  248. } else {
  249. hix5hd2_rw_over(priv);
  250. }
  251. }
  252. stop:
  253. if ((priv->state == HIX5I2C_STAT_RW_SUCCESS &&
  254. priv->msg->len == priv->msg_idx) ||
  255. (priv->state == HIX5I2C_STAT_RW_ERR)) {
  256. hix5hd2_i2c_disable_irq(priv);
  257. hix5hd2_i2c_clr_pend_irq(priv);
  258. complete(&priv->msg_complete);
  259. }
  260. spin_unlock(&priv->lock);
  261. return IRQ_HANDLED;
  262. }
  263. static void hix5hd2_i2c_message_start(struct hix5hd2_i2c_priv *priv, int stop)
  264. {
  265. unsigned long flags;
  266. spin_lock_irqsave(&priv->lock, flags);
  267. hix5hd2_i2c_clr_all_irq(priv);
  268. hix5hd2_i2c_enable_irq(priv);
  269. if (priv->msg->flags & I2C_M_RD)
  270. writel_relaxed((priv->msg->addr << 1) | HIX5I2C_READ_OPERATION,
  271. priv->regs + HIX5I2C_TXR);
  272. else
  273. writel_relaxed(priv->msg->addr << 1,
  274. priv->regs + HIX5I2C_TXR);
  275. writel_relaxed(I2C_WRITE | I2C_START, priv->regs + HIX5I2C_COM);
  276. spin_unlock_irqrestore(&priv->lock, flags);
  277. }
  278. static int hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv *priv,
  279. struct i2c_msg *msgs, int stop)
  280. {
  281. unsigned long timeout;
  282. int ret;
  283. priv->msg = msgs;
  284. priv->msg_idx = 0;
  285. priv->msg_len = priv->msg->len;
  286. priv->stop = stop;
  287. priv->err = 0;
  288. priv->state = HIX5I2C_STAT_INIT;
  289. reinit_completion(&priv->msg_complete);
  290. hix5hd2_i2c_message_start(priv, stop);
  291. timeout = wait_for_completion_timeout(&priv->msg_complete,
  292. priv->adap.timeout);
  293. if (timeout == 0) {
  294. priv->state = HIX5I2C_STAT_RW_ERR;
  295. priv->err = -ETIMEDOUT;
  296. dev_warn(priv->dev, "%s timeout=%d\n",
  297. msgs->flags & I2C_M_RD ? "rx" : "tx",
  298. priv->adap.timeout);
  299. }
  300. ret = priv->state;
  301. /*
  302. * If this is the last message to be transfered (stop == 1)
  303. * Then check if the bus can be brought back to idle.
  304. */
  305. if (priv->state == HIX5I2C_STAT_RW_SUCCESS && stop)
  306. ret = hix5hd2_i2c_wait_bus_idle(priv);
  307. if (ret < 0)
  308. hix5hd2_i2c_reset(priv);
  309. return priv->err;
  310. }
  311. static int hix5hd2_i2c_xfer(struct i2c_adapter *adap,
  312. struct i2c_msg *msgs, int num)
  313. {
  314. struct hix5hd2_i2c_priv *priv = i2c_get_adapdata(adap);
  315. int i, ret, stop;
  316. pm_runtime_get_sync(priv->dev);
  317. for (i = 0; i < num; i++, msgs++) {
  318. stop = (i == num - 1);
  319. ret = hix5hd2_i2c_xfer_msg(priv, msgs, stop);
  320. if (ret < 0)
  321. goto out;
  322. }
  323. if (i == num) {
  324. ret = num;
  325. } else {
  326. /* Only one message, cannot access the device */
  327. if (i == 1)
  328. ret = -EREMOTEIO;
  329. else
  330. ret = i;
  331. dev_warn(priv->dev, "xfer message failed\n");
  332. }
  333. out:
  334. pm_runtime_mark_last_busy(priv->dev);
  335. pm_runtime_put_autosuspend(priv->dev);
  336. return ret;
  337. }
  338. static u32 hix5hd2_i2c_func(struct i2c_adapter *adap)
  339. {
  340. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  341. }
  342. static const struct i2c_algorithm hix5hd2_i2c_algorithm = {
  343. .master_xfer = hix5hd2_i2c_xfer,
  344. .functionality = hix5hd2_i2c_func,
  345. };
  346. static int hix5hd2_i2c_probe(struct platform_device *pdev)
  347. {
  348. struct device_node *np = pdev->dev.of_node;
  349. struct hix5hd2_i2c_priv *priv;
  350. struct resource *mem;
  351. unsigned int freq;
  352. int irq, ret;
  353. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  354. if (!priv)
  355. return -ENOMEM;
  356. if (of_property_read_u32(np, "clock-frequency", &freq)) {
  357. /* use 100k as default value */
  358. priv->freq = 100000;
  359. } else {
  360. if (freq > HIX5I2C_MAX_FREQ) {
  361. priv->freq = HIX5I2C_MAX_FREQ;
  362. dev_warn(priv->dev, "use max freq %d instead\n",
  363. HIX5I2C_MAX_FREQ);
  364. } else {
  365. priv->freq = freq;
  366. }
  367. }
  368. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  369. priv->regs = devm_ioremap_resource(&pdev->dev, mem);
  370. if (IS_ERR(priv->regs))
  371. return PTR_ERR(priv->regs);
  372. irq = platform_get_irq(pdev, 0);
  373. if (irq <= 0) {
  374. dev_err(&pdev->dev, "cannot find HS-I2C IRQ\n");
  375. return irq;
  376. }
  377. priv->clk = devm_clk_get(&pdev->dev, NULL);
  378. if (IS_ERR(priv->clk)) {
  379. dev_err(&pdev->dev, "cannot get clock\n");
  380. return PTR_ERR(priv->clk);
  381. }
  382. clk_prepare_enable(priv->clk);
  383. strlcpy(priv->adap.name, "hix5hd2-i2c", sizeof(priv->adap.name));
  384. priv->dev = &pdev->dev;
  385. priv->adap.owner = THIS_MODULE;
  386. priv->adap.algo = &hix5hd2_i2c_algorithm;
  387. priv->adap.retries = 3;
  388. priv->adap.dev.of_node = np;
  389. priv->adap.algo_data = priv;
  390. priv->adap.dev.parent = &pdev->dev;
  391. i2c_set_adapdata(&priv->adap, priv);
  392. platform_set_drvdata(pdev, priv);
  393. spin_lock_init(&priv->lock);
  394. init_completion(&priv->msg_complete);
  395. hix5hd2_i2c_init(priv);
  396. ret = devm_request_irq(&pdev->dev, irq, hix5hd2_i2c_irq,
  397. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  398. dev_name(&pdev->dev), priv);
  399. if (ret != 0) {
  400. dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", irq);
  401. goto err_clk;
  402. }
  403. pm_suspend_ignore_children(&pdev->dev, true);
  404. pm_runtime_set_autosuspend_delay(priv->dev, MSEC_PER_SEC);
  405. pm_runtime_use_autosuspend(priv->dev);
  406. pm_runtime_set_active(priv->dev);
  407. pm_runtime_enable(priv->dev);
  408. ret = i2c_add_adapter(&priv->adap);
  409. if (ret < 0) {
  410. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  411. goto err_runtime;
  412. }
  413. return ret;
  414. err_runtime:
  415. pm_runtime_disable(priv->dev);
  416. pm_runtime_set_suspended(priv->dev);
  417. err_clk:
  418. clk_disable_unprepare(priv->clk);
  419. return ret;
  420. }
  421. static int hix5hd2_i2c_remove(struct platform_device *pdev)
  422. {
  423. struct hix5hd2_i2c_priv *priv = platform_get_drvdata(pdev);
  424. i2c_del_adapter(&priv->adap);
  425. pm_runtime_disable(priv->dev);
  426. pm_runtime_set_suspended(priv->dev);
  427. return 0;
  428. }
  429. #ifdef CONFIG_PM
  430. static int hix5hd2_i2c_runtime_suspend(struct device *dev)
  431. {
  432. struct platform_device *pdev = to_platform_device(dev);
  433. struct hix5hd2_i2c_priv *priv = platform_get_drvdata(pdev);
  434. clk_disable_unprepare(priv->clk);
  435. return 0;
  436. }
  437. static int hix5hd2_i2c_runtime_resume(struct device *dev)
  438. {
  439. struct platform_device *pdev = to_platform_device(dev);
  440. struct hix5hd2_i2c_priv *priv = platform_get_drvdata(pdev);
  441. clk_prepare_enable(priv->clk);
  442. hix5hd2_i2c_init(priv);
  443. return 0;
  444. }
  445. #endif
  446. static const struct dev_pm_ops hix5hd2_i2c_pm_ops = {
  447. SET_RUNTIME_PM_OPS(hix5hd2_i2c_runtime_suspend,
  448. hix5hd2_i2c_runtime_resume,
  449. NULL)
  450. };
  451. static const struct of_device_id hix5hd2_i2c_match[] = {
  452. { .compatible = "hisilicon,hix5hd2-i2c" },
  453. {},
  454. };
  455. MODULE_DEVICE_TABLE(of, hix5hd2_i2c_match);
  456. static struct platform_driver hix5hd2_i2c_driver = {
  457. .probe = hix5hd2_i2c_probe,
  458. .remove = hix5hd2_i2c_remove,
  459. .driver = {
  460. .name = "hix5hd2-i2c",
  461. .pm = &hix5hd2_i2c_pm_ops,
  462. .of_match_table = hix5hd2_i2c_match,
  463. },
  464. };
  465. module_platform_driver(hix5hd2_i2c_driver);
  466. MODULE_DESCRIPTION("Hix5hd2 I2C Bus driver");
  467. MODULE_AUTHOR("Wei Yan <sledge.yanwei@huawei.com>");
  468. MODULE_LICENSE("GPL");
  469. MODULE_ALIAS("platform:hix5hd2-i2c");