i2c-sprd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Copyright (C) 2017 Spreadtrum Communications Inc.
  3. *
  4. * SPDX-License-Identifier: (GPL-2.0+ OR MIT)
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #define I2C_CTL 0x00
  19. #define I2C_ADDR_CFG 0x04
  20. #define I2C_COUNT 0x08
  21. #define I2C_RX 0x0c
  22. #define I2C_TX 0x10
  23. #define I2C_STATUS 0x14
  24. #define I2C_HSMODE_CFG 0x18
  25. #define I2C_VERSION 0x1c
  26. #define ADDR_DVD0 0x20
  27. #define ADDR_DVD1 0x24
  28. #define ADDR_STA0_DVD 0x28
  29. #define ADDR_RST 0x2c
  30. /* I2C_CTL */
  31. #define STP_EN BIT(20)
  32. #define FIFO_AF_LVL_MASK GENMASK(19, 16)
  33. #define FIFO_AF_LVL 16
  34. #define FIFO_AE_LVL_MASK GENMASK(15, 12)
  35. #define FIFO_AE_LVL 12
  36. #define I2C_DMA_EN BIT(11)
  37. #define FULL_INTEN BIT(10)
  38. #define EMPTY_INTEN BIT(9)
  39. #define I2C_DVD_OPT BIT(8)
  40. #define I2C_OUT_OPT BIT(7)
  41. #define I2C_TRIM_OPT BIT(6)
  42. #define I2C_HS_MODE BIT(4)
  43. #define I2C_MODE BIT(3)
  44. #define I2C_EN BIT(2)
  45. #define I2C_INT_EN BIT(1)
  46. #define I2C_START BIT(0)
  47. /* I2C_STATUS */
  48. #define SDA_IN BIT(21)
  49. #define SCL_IN BIT(20)
  50. #define FIFO_FULL BIT(4)
  51. #define FIFO_EMPTY BIT(3)
  52. #define I2C_INT BIT(2)
  53. #define I2C_RX_ACK BIT(1)
  54. #define I2C_BUSY BIT(0)
  55. /* ADDR_RST */
  56. #define I2C_RST BIT(0)
  57. #define I2C_FIFO_DEEP 12
  58. #define I2C_FIFO_FULL_THLD 15
  59. #define I2C_FIFO_EMPTY_THLD 4
  60. #define I2C_DATA_STEP 8
  61. #define I2C_ADDR_DVD0_CALC(high, low) \
  62. ((((high) & GENMASK(15, 0)) << 16) | ((low) & GENMASK(15, 0)))
  63. #define I2C_ADDR_DVD1_CALC(high, low) \
  64. (((high) & GENMASK(31, 16)) | (((low) & GENMASK(31, 16)) >> 16))
  65. /* timeout (ms) for pm runtime autosuspend */
  66. #define SPRD_I2C_PM_TIMEOUT 1000
  67. /* SPRD i2c data structure */
  68. struct sprd_i2c {
  69. struct i2c_adapter adap;
  70. struct device *dev;
  71. void __iomem *base;
  72. struct i2c_msg *msg;
  73. struct clk *clk;
  74. u32 src_clk;
  75. u32 bus_freq;
  76. struct completion complete;
  77. u8 *buf;
  78. u32 count;
  79. int irq;
  80. int err;
  81. bool is_suspended;
  82. };
  83. static void sprd_i2c_set_count(struct sprd_i2c *i2c_dev, u32 count)
  84. {
  85. writel(count, i2c_dev->base + I2C_COUNT);
  86. }
  87. static void sprd_i2c_send_stop(struct sprd_i2c *i2c_dev, int stop)
  88. {
  89. u32 tmp = readl(i2c_dev->base + I2C_CTL);
  90. if (stop)
  91. writel(tmp & ~STP_EN, i2c_dev->base + I2C_CTL);
  92. else
  93. writel(tmp | STP_EN, i2c_dev->base + I2C_CTL);
  94. }
  95. static void sprd_i2c_clear_start(struct sprd_i2c *i2c_dev)
  96. {
  97. u32 tmp = readl(i2c_dev->base + I2C_CTL);
  98. writel(tmp & ~I2C_START, i2c_dev->base + I2C_CTL);
  99. }
  100. static void sprd_i2c_clear_ack(struct sprd_i2c *i2c_dev)
  101. {
  102. u32 tmp = readl(i2c_dev->base + I2C_STATUS);
  103. writel(tmp & ~I2C_RX_ACK, i2c_dev->base + I2C_STATUS);
  104. }
  105. static void sprd_i2c_clear_irq(struct sprd_i2c *i2c_dev)
  106. {
  107. u32 tmp = readl(i2c_dev->base + I2C_STATUS);
  108. writel(tmp & ~I2C_INT, i2c_dev->base + I2C_STATUS);
  109. }
  110. static void sprd_i2c_reset_fifo(struct sprd_i2c *i2c_dev)
  111. {
  112. writel(I2C_RST, i2c_dev->base + ADDR_RST);
  113. }
  114. static void sprd_i2c_set_devaddr(struct sprd_i2c *i2c_dev, struct i2c_msg *m)
  115. {
  116. writel(m->addr << 1, i2c_dev->base + I2C_ADDR_CFG);
  117. }
  118. static void sprd_i2c_write_bytes(struct sprd_i2c *i2c_dev, u8 *buf, u32 len)
  119. {
  120. u32 i;
  121. for (i = 0; i < len; i++)
  122. writeb(buf[i], i2c_dev->base + I2C_TX);
  123. }
  124. static void sprd_i2c_read_bytes(struct sprd_i2c *i2c_dev, u8 *buf, u32 len)
  125. {
  126. u32 i;
  127. for (i = 0; i < len; i++)
  128. buf[i] = readb(i2c_dev->base + I2C_RX);
  129. }
  130. static void sprd_i2c_set_full_thld(struct sprd_i2c *i2c_dev, u32 full_thld)
  131. {
  132. u32 tmp = readl(i2c_dev->base + I2C_CTL);
  133. tmp &= ~FIFO_AF_LVL_MASK;
  134. tmp |= full_thld << FIFO_AF_LVL;
  135. writel(tmp, i2c_dev->base + I2C_CTL);
  136. };
  137. static void sprd_i2c_set_empty_thld(struct sprd_i2c *i2c_dev, u32 empty_thld)
  138. {
  139. u32 tmp = readl(i2c_dev->base + I2C_CTL);
  140. tmp &= ~FIFO_AE_LVL_MASK;
  141. tmp |= empty_thld << FIFO_AE_LVL;
  142. writel(tmp, i2c_dev->base + I2C_CTL);
  143. };
  144. static void sprd_i2c_set_fifo_full_int(struct sprd_i2c *i2c_dev, int enable)
  145. {
  146. u32 tmp = readl(i2c_dev->base + I2C_CTL);
  147. if (enable)
  148. tmp |= FULL_INTEN;
  149. else
  150. tmp &= ~FULL_INTEN;
  151. writel(tmp, i2c_dev->base + I2C_CTL);
  152. };
  153. static void sprd_i2c_set_fifo_empty_int(struct sprd_i2c *i2c_dev, int enable)
  154. {
  155. u32 tmp = readl(i2c_dev->base + I2C_CTL);
  156. if (enable)
  157. tmp |= EMPTY_INTEN;
  158. else
  159. tmp &= ~EMPTY_INTEN;
  160. writel(tmp, i2c_dev->base + I2C_CTL);
  161. };
  162. static void sprd_i2c_opt_start(struct sprd_i2c *i2c_dev)
  163. {
  164. u32 tmp = readl(i2c_dev->base + I2C_CTL);
  165. writel(tmp | I2C_START, i2c_dev->base + I2C_CTL);
  166. }
  167. static void sprd_i2c_opt_mode(struct sprd_i2c *i2c_dev, int rw)
  168. {
  169. u32 cmd = readl(i2c_dev->base + I2C_CTL) & ~I2C_MODE;
  170. writel(cmd | rw << 3, i2c_dev->base + I2C_CTL);
  171. }
  172. static void sprd_i2c_data_transfer(struct sprd_i2c *i2c_dev)
  173. {
  174. u32 i2c_count = i2c_dev->count;
  175. u32 need_tran = i2c_count <= I2C_FIFO_DEEP ? i2c_count : I2C_FIFO_DEEP;
  176. struct i2c_msg *msg = i2c_dev->msg;
  177. if (msg->flags & I2C_M_RD) {
  178. sprd_i2c_read_bytes(i2c_dev, i2c_dev->buf, I2C_FIFO_FULL_THLD);
  179. i2c_dev->count -= I2C_FIFO_FULL_THLD;
  180. i2c_dev->buf += I2C_FIFO_FULL_THLD;
  181. /*
  182. * If the read data count is larger than rx fifo full threshold,
  183. * we should enable the rx fifo full interrupt to read data
  184. * again.
  185. */
  186. if (i2c_dev->count >= I2C_FIFO_FULL_THLD)
  187. sprd_i2c_set_fifo_full_int(i2c_dev, 1);
  188. } else {
  189. sprd_i2c_write_bytes(i2c_dev, i2c_dev->buf, need_tran);
  190. i2c_dev->buf += need_tran;
  191. i2c_dev->count -= need_tran;
  192. /*
  193. * If the write data count is arger than tx fifo depth which
  194. * means we can not write all data in one time, then we should
  195. * enable the tx fifo empty interrupt to write again.
  196. */
  197. if (i2c_count > I2C_FIFO_DEEP)
  198. sprd_i2c_set_fifo_empty_int(i2c_dev, 1);
  199. }
  200. }
  201. static int sprd_i2c_handle_msg(struct i2c_adapter *i2c_adap,
  202. struct i2c_msg *msg, bool is_last_msg)
  203. {
  204. struct sprd_i2c *i2c_dev = i2c_adap->algo_data;
  205. i2c_dev->msg = msg;
  206. i2c_dev->buf = msg->buf;
  207. i2c_dev->count = msg->len;
  208. reinit_completion(&i2c_dev->complete);
  209. sprd_i2c_reset_fifo(i2c_dev);
  210. sprd_i2c_set_devaddr(i2c_dev, msg);
  211. sprd_i2c_set_count(i2c_dev, msg->len);
  212. if (msg->flags & I2C_M_RD) {
  213. sprd_i2c_opt_mode(i2c_dev, 1);
  214. sprd_i2c_send_stop(i2c_dev, 1);
  215. } else {
  216. sprd_i2c_opt_mode(i2c_dev, 0);
  217. sprd_i2c_send_stop(i2c_dev, !!is_last_msg);
  218. }
  219. /*
  220. * We should enable rx fifo full interrupt to get data when receiving
  221. * full data.
  222. */
  223. if (msg->flags & I2C_M_RD)
  224. sprd_i2c_set_fifo_full_int(i2c_dev, 1);
  225. else
  226. sprd_i2c_data_transfer(i2c_dev);
  227. sprd_i2c_opt_start(i2c_dev);
  228. wait_for_completion(&i2c_dev->complete);
  229. return i2c_dev->err;
  230. }
  231. static int sprd_i2c_master_xfer(struct i2c_adapter *i2c_adap,
  232. struct i2c_msg *msgs, int num)
  233. {
  234. struct sprd_i2c *i2c_dev = i2c_adap->algo_data;
  235. int im, ret;
  236. if (i2c_dev->is_suspended)
  237. return -EBUSY;
  238. ret = pm_runtime_get_sync(i2c_dev->dev);
  239. if (ret < 0)
  240. return ret;
  241. for (im = 0; im < num - 1; im++) {
  242. ret = sprd_i2c_handle_msg(i2c_adap, &msgs[im], 0);
  243. if (ret)
  244. goto err_msg;
  245. }
  246. ret = sprd_i2c_handle_msg(i2c_adap, &msgs[im++], 1);
  247. err_msg:
  248. pm_runtime_mark_last_busy(i2c_dev->dev);
  249. pm_runtime_put_autosuspend(i2c_dev->dev);
  250. return ret < 0 ? ret : im;
  251. }
  252. static u32 sprd_i2c_func(struct i2c_adapter *adap)
  253. {
  254. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  255. }
  256. static const struct i2c_algorithm sprd_i2c_algo = {
  257. .master_xfer = sprd_i2c_master_xfer,
  258. .functionality = sprd_i2c_func,
  259. };
  260. static void sprd_i2c_set_clk(struct sprd_i2c *i2c_dev, u32 freq)
  261. {
  262. u32 apb_clk = i2c_dev->src_clk;
  263. /*
  264. * From I2C databook, the prescale calculation formula:
  265. * prescale = freq_i2c / (4 * freq_scl) - 1;
  266. */
  267. u32 i2c_dvd = apb_clk / (4 * freq) - 1;
  268. /*
  269. * From I2C databook, the high period of SCL clock is recommended as
  270. * 40% (2/5), and the low period of SCL clock is recommended as 60%
  271. * (3/5), then the formula should be:
  272. * high = (prescale * 2 * 2) / 5
  273. * low = (prescale * 2 * 3) / 5
  274. */
  275. u32 high = ((i2c_dvd << 1) * 2) / 5;
  276. u32 low = ((i2c_dvd << 1) * 3) / 5;
  277. u32 div0 = I2C_ADDR_DVD0_CALC(high, low);
  278. u32 div1 = I2C_ADDR_DVD1_CALC(high, low);
  279. writel(div0, i2c_dev->base + ADDR_DVD0);
  280. writel(div1, i2c_dev->base + ADDR_DVD1);
  281. /* Start hold timing = hold time(us) * source clock */
  282. if (freq == 400000)
  283. writel((6 * apb_clk) / 10000000, i2c_dev->base + ADDR_STA0_DVD);
  284. else if (freq == 100000)
  285. writel((4 * apb_clk) / 1000000, i2c_dev->base + ADDR_STA0_DVD);
  286. }
  287. static void sprd_i2c_enable(struct sprd_i2c *i2c_dev)
  288. {
  289. u32 tmp = I2C_DVD_OPT;
  290. writel(tmp, i2c_dev->base + I2C_CTL);
  291. sprd_i2c_set_full_thld(i2c_dev, I2C_FIFO_FULL_THLD);
  292. sprd_i2c_set_empty_thld(i2c_dev, I2C_FIFO_EMPTY_THLD);
  293. sprd_i2c_set_clk(i2c_dev, i2c_dev->bus_freq);
  294. sprd_i2c_reset_fifo(i2c_dev);
  295. sprd_i2c_clear_irq(i2c_dev);
  296. tmp = readl(i2c_dev->base + I2C_CTL);
  297. writel(tmp | I2C_EN | I2C_INT_EN, i2c_dev->base + I2C_CTL);
  298. }
  299. static irqreturn_t sprd_i2c_isr_thread(int irq, void *dev_id)
  300. {
  301. struct sprd_i2c *i2c_dev = dev_id;
  302. struct i2c_msg *msg = i2c_dev->msg;
  303. bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK);
  304. u32 i2c_tran;
  305. if (msg->flags & I2C_M_RD)
  306. i2c_tran = i2c_dev->count >= I2C_FIFO_FULL_THLD;
  307. else
  308. i2c_tran = i2c_dev->count;
  309. /*
  310. * If we got one ACK from slave when writing data, and we did not
  311. * finish this transmission (i2c_tran is not zero), then we should
  312. * continue to write data.
  313. *
  314. * For reading data, ack is always true, if i2c_tran is not 0 which
  315. * means we still need to contine to read data from slave.
  316. */
  317. if (i2c_tran && ack) {
  318. sprd_i2c_data_transfer(i2c_dev);
  319. return IRQ_HANDLED;
  320. }
  321. i2c_dev->err = 0;
  322. /*
  323. * If we did not get one ACK from slave when writing data, we should
  324. * return -EIO to notify users.
  325. */
  326. if (!ack)
  327. i2c_dev->err = -EIO;
  328. else if (msg->flags & I2C_M_RD && i2c_dev->count)
  329. sprd_i2c_read_bytes(i2c_dev, i2c_dev->buf, i2c_dev->count);
  330. /* Transmission is done and clear ack and start operation */
  331. sprd_i2c_clear_ack(i2c_dev);
  332. sprd_i2c_clear_start(i2c_dev);
  333. complete(&i2c_dev->complete);
  334. return IRQ_HANDLED;
  335. }
  336. static irqreturn_t sprd_i2c_isr(int irq, void *dev_id)
  337. {
  338. struct sprd_i2c *i2c_dev = dev_id;
  339. struct i2c_msg *msg = i2c_dev->msg;
  340. bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK);
  341. u32 i2c_tran;
  342. if (msg->flags & I2C_M_RD)
  343. i2c_tran = i2c_dev->count >= I2C_FIFO_FULL_THLD;
  344. else
  345. i2c_tran = i2c_dev->count;
  346. /*
  347. * If we did not get one ACK from slave when writing data, then we
  348. * should finish this transmission since we got some errors.
  349. *
  350. * When writing data, if i2c_tran == 0 which means we have writen
  351. * done all data, then we can finish this transmission.
  352. *
  353. * When reading data, if conut < rx fifo full threshold, which
  354. * means we can read all data in one time, then we can finish this
  355. * transmission too.
  356. */
  357. if (!i2c_tran || !ack) {
  358. sprd_i2c_clear_start(i2c_dev);
  359. sprd_i2c_clear_irq(i2c_dev);
  360. }
  361. sprd_i2c_set_fifo_empty_int(i2c_dev, 0);
  362. sprd_i2c_set_fifo_full_int(i2c_dev, 0);
  363. return IRQ_WAKE_THREAD;
  364. }
  365. static int sprd_i2c_clk_init(struct sprd_i2c *i2c_dev)
  366. {
  367. struct clk *clk_i2c, *clk_parent;
  368. clk_i2c = devm_clk_get(i2c_dev->dev, "i2c");
  369. if (IS_ERR(clk_i2c)) {
  370. dev_warn(i2c_dev->dev, "i2c%d can't get the i2c clock\n",
  371. i2c_dev->adap.nr);
  372. clk_i2c = NULL;
  373. }
  374. clk_parent = devm_clk_get(i2c_dev->dev, "source");
  375. if (IS_ERR(clk_parent)) {
  376. dev_warn(i2c_dev->dev, "i2c%d can't get the source clock\n",
  377. i2c_dev->adap.nr);
  378. clk_parent = NULL;
  379. }
  380. if (clk_set_parent(clk_i2c, clk_parent))
  381. i2c_dev->src_clk = clk_get_rate(clk_i2c);
  382. else
  383. i2c_dev->src_clk = 26000000;
  384. dev_dbg(i2c_dev->dev, "i2c%d set source clock is %d\n",
  385. i2c_dev->adap.nr, i2c_dev->src_clk);
  386. i2c_dev->clk = devm_clk_get(i2c_dev->dev, "enable");
  387. if (IS_ERR(i2c_dev->clk)) {
  388. dev_warn(i2c_dev->dev, "i2c%d can't get the enable clock\n",
  389. i2c_dev->adap.nr);
  390. i2c_dev->clk = NULL;
  391. }
  392. return 0;
  393. }
  394. static int sprd_i2c_probe(struct platform_device *pdev)
  395. {
  396. struct device *dev = &pdev->dev;
  397. struct sprd_i2c *i2c_dev;
  398. struct resource *res;
  399. u32 prop;
  400. int ret;
  401. pdev->id = of_alias_get_id(dev->of_node, "i2c");
  402. i2c_dev = devm_kzalloc(dev, sizeof(struct sprd_i2c), GFP_KERNEL);
  403. if (!i2c_dev)
  404. return -ENOMEM;
  405. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  406. i2c_dev->base = devm_ioremap_resource(dev, res);
  407. if (IS_ERR(i2c_dev->base))
  408. return PTR_ERR(i2c_dev->base);
  409. i2c_dev->irq = platform_get_irq(pdev, 0);
  410. if (i2c_dev->irq < 0) {
  411. dev_err(&pdev->dev, "failed to get irq resource\n");
  412. return i2c_dev->irq;
  413. }
  414. i2c_set_adapdata(&i2c_dev->adap, i2c_dev);
  415. init_completion(&i2c_dev->complete);
  416. snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name),
  417. "%s", "sprd-i2c");
  418. i2c_dev->bus_freq = 100000;
  419. i2c_dev->adap.owner = THIS_MODULE;
  420. i2c_dev->dev = dev;
  421. i2c_dev->adap.retries = 3;
  422. i2c_dev->adap.algo = &sprd_i2c_algo;
  423. i2c_dev->adap.algo_data = i2c_dev;
  424. i2c_dev->adap.dev.parent = dev;
  425. i2c_dev->adap.nr = pdev->id;
  426. i2c_dev->adap.dev.of_node = dev->of_node;
  427. if (!of_property_read_u32(dev->of_node, "clock-frequency", &prop))
  428. i2c_dev->bus_freq = prop;
  429. /* We only support 100k and 400k now, otherwise will return error. */
  430. if (i2c_dev->bus_freq != 100000 && i2c_dev->bus_freq != 400000)
  431. return -EINVAL;
  432. sprd_i2c_clk_init(i2c_dev);
  433. platform_set_drvdata(pdev, i2c_dev);
  434. ret = clk_prepare_enable(i2c_dev->clk);
  435. if (ret)
  436. return ret;
  437. sprd_i2c_enable(i2c_dev);
  438. pm_runtime_set_autosuspend_delay(i2c_dev->dev, SPRD_I2C_PM_TIMEOUT);
  439. pm_runtime_use_autosuspend(i2c_dev->dev);
  440. pm_runtime_set_active(i2c_dev->dev);
  441. pm_runtime_enable(i2c_dev->dev);
  442. ret = pm_runtime_get_sync(i2c_dev->dev);
  443. if (ret < 0)
  444. goto err_rpm_put;
  445. ret = devm_request_threaded_irq(dev, i2c_dev->irq,
  446. sprd_i2c_isr, sprd_i2c_isr_thread,
  447. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  448. pdev->name, i2c_dev);
  449. if (ret) {
  450. dev_err(&pdev->dev, "failed to request irq %d\n", i2c_dev->irq);
  451. goto err_rpm_put;
  452. }
  453. ret = i2c_add_numbered_adapter(&i2c_dev->adap);
  454. if (ret) {
  455. dev_err(&pdev->dev, "add adapter failed\n");
  456. goto err_rpm_put;
  457. }
  458. pm_runtime_mark_last_busy(i2c_dev->dev);
  459. pm_runtime_put_autosuspend(i2c_dev->dev);
  460. return 0;
  461. err_rpm_put:
  462. pm_runtime_put_noidle(i2c_dev->dev);
  463. pm_runtime_disable(i2c_dev->dev);
  464. clk_disable_unprepare(i2c_dev->clk);
  465. return ret;
  466. }
  467. static int sprd_i2c_remove(struct platform_device *pdev)
  468. {
  469. struct sprd_i2c *i2c_dev = platform_get_drvdata(pdev);
  470. int ret;
  471. ret = pm_runtime_get_sync(i2c_dev->dev);
  472. if (ret < 0)
  473. return ret;
  474. i2c_del_adapter(&i2c_dev->adap);
  475. clk_disable_unprepare(i2c_dev->clk);
  476. pm_runtime_put_noidle(i2c_dev->dev);
  477. pm_runtime_disable(i2c_dev->dev);
  478. return 0;
  479. }
  480. static int __maybe_unused sprd_i2c_suspend_noirq(struct device *pdev)
  481. {
  482. struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev);
  483. i2c_lock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
  484. i2c_dev->is_suspended = true;
  485. i2c_unlock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
  486. return pm_runtime_force_suspend(pdev);
  487. }
  488. static int __maybe_unused sprd_i2c_resume_noirq(struct device *pdev)
  489. {
  490. struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev);
  491. i2c_lock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
  492. i2c_dev->is_suspended = false;
  493. i2c_unlock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
  494. return pm_runtime_force_resume(pdev);
  495. }
  496. static int __maybe_unused sprd_i2c_runtime_suspend(struct device *pdev)
  497. {
  498. struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev);
  499. clk_disable_unprepare(i2c_dev->clk);
  500. return 0;
  501. }
  502. static int __maybe_unused sprd_i2c_runtime_resume(struct device *pdev)
  503. {
  504. struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev);
  505. int ret;
  506. ret = clk_prepare_enable(i2c_dev->clk);
  507. if (ret)
  508. return ret;
  509. sprd_i2c_enable(i2c_dev);
  510. return 0;
  511. }
  512. static const struct dev_pm_ops sprd_i2c_pm_ops = {
  513. SET_RUNTIME_PM_OPS(sprd_i2c_runtime_suspend,
  514. sprd_i2c_runtime_resume, NULL)
  515. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sprd_i2c_suspend_noirq,
  516. sprd_i2c_resume_noirq)
  517. };
  518. static const struct of_device_id sprd_i2c_of_match[] = {
  519. { .compatible = "sprd,sc9860-i2c", },
  520. {},
  521. };
  522. static struct platform_driver sprd_i2c_driver = {
  523. .probe = sprd_i2c_probe,
  524. .remove = sprd_i2c_remove,
  525. .driver = {
  526. .name = "sprd-i2c",
  527. .of_match_table = sprd_i2c_of_match,
  528. .pm = &sprd_i2c_pm_ops,
  529. },
  530. };
  531. static int sprd_i2c_init(void)
  532. {
  533. return platform_driver_register(&sprd_i2c_driver);
  534. }
  535. arch_initcall_sync(sprd_i2c_init);