i2c-sprd.c 16 KB

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