i2c-qup.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2014, Sony Mobile Communications AB.
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/i2c.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. /* QUP Registers */
  27. #define QUP_CONFIG 0x000
  28. #define QUP_STATE 0x004
  29. #define QUP_IO_MODE 0x008
  30. #define QUP_SW_RESET 0x00c
  31. #define QUP_OPERATIONAL 0x018
  32. #define QUP_ERROR_FLAGS 0x01c
  33. #define QUP_ERROR_FLAGS_EN 0x020
  34. #define QUP_HW_VERSION 0x030
  35. #define QUP_MX_OUTPUT_CNT 0x100
  36. #define QUP_OUT_FIFO_BASE 0x110
  37. #define QUP_MX_WRITE_CNT 0x150
  38. #define QUP_MX_INPUT_CNT 0x200
  39. #define QUP_MX_READ_CNT 0x208
  40. #define QUP_IN_FIFO_BASE 0x218
  41. #define QUP_I2C_CLK_CTL 0x400
  42. #define QUP_I2C_STATUS 0x404
  43. /* QUP States and reset values */
  44. #define QUP_RESET_STATE 0
  45. #define QUP_RUN_STATE 1
  46. #define QUP_PAUSE_STATE 3
  47. #define QUP_STATE_MASK 3
  48. #define QUP_STATE_VALID BIT(2)
  49. #define QUP_I2C_MAST_GEN BIT(4)
  50. #define QUP_OPERATIONAL_RESET 0x000ff0
  51. #define QUP_I2C_STATUS_RESET 0xfffffc
  52. /* QUP OPERATIONAL FLAGS */
  53. #define QUP_I2C_NACK_FLAG BIT(3)
  54. #define QUP_OUT_NOT_EMPTY BIT(4)
  55. #define QUP_IN_NOT_EMPTY BIT(5)
  56. #define QUP_OUT_FULL BIT(6)
  57. #define QUP_OUT_SVC_FLAG BIT(8)
  58. #define QUP_IN_SVC_FLAG BIT(9)
  59. #define QUP_MX_OUTPUT_DONE BIT(10)
  60. #define QUP_MX_INPUT_DONE BIT(11)
  61. /* I2C mini core related values */
  62. #define QUP_CLOCK_AUTO_GATE BIT(13)
  63. #define I2C_MINI_CORE (2 << 8)
  64. #define I2C_N_VAL 15
  65. /* Most significant word offset in FIFO port */
  66. #define QUP_MSW_SHIFT (I2C_N_VAL + 1)
  67. /* Packing/Unpacking words in FIFOs, and IO modes */
  68. #define QUP_OUTPUT_BLK_MODE (1 << 10)
  69. #define QUP_INPUT_BLK_MODE (1 << 12)
  70. #define QUP_UNPACK_EN BIT(14)
  71. #define QUP_PACK_EN BIT(15)
  72. #define QUP_REPACK_EN (QUP_UNPACK_EN | QUP_PACK_EN)
  73. #define QUP_OUTPUT_BLOCK_SIZE(x)(((x) >> 0) & 0x03)
  74. #define QUP_OUTPUT_FIFO_SIZE(x) (((x) >> 2) & 0x07)
  75. #define QUP_INPUT_BLOCK_SIZE(x) (((x) >> 5) & 0x03)
  76. #define QUP_INPUT_FIFO_SIZE(x) (((x) >> 7) & 0x07)
  77. /* QUP tags */
  78. #define QUP_TAG_START (1 << 8)
  79. #define QUP_TAG_DATA (2 << 8)
  80. #define QUP_TAG_STOP (3 << 8)
  81. #define QUP_TAG_REC (4 << 8)
  82. /* Status, Error flags */
  83. #define I2C_STATUS_WR_BUFFER_FULL BIT(0)
  84. #define I2C_STATUS_BUS_ACTIVE BIT(8)
  85. #define I2C_STATUS_ERROR_MASK 0x38000fc
  86. #define QUP_STATUS_ERROR_FLAGS 0x7c
  87. #define QUP_READ_LIMIT 256
  88. struct qup_i2c_dev {
  89. struct device *dev;
  90. void __iomem *base;
  91. int irq;
  92. struct clk *clk;
  93. struct clk *pclk;
  94. struct i2c_adapter adap;
  95. int clk_ctl;
  96. int out_fifo_sz;
  97. int in_fifo_sz;
  98. int out_blk_sz;
  99. int in_blk_sz;
  100. unsigned long one_byte_t;
  101. struct i2c_msg *msg;
  102. /* Current posion in user message buffer */
  103. int pos;
  104. /* I2C protocol errors */
  105. u32 bus_err;
  106. /* QUP core errors */
  107. u32 qup_err;
  108. struct completion xfer;
  109. };
  110. static irqreturn_t qup_i2c_interrupt(int irq, void *dev)
  111. {
  112. struct qup_i2c_dev *qup = dev;
  113. u32 bus_err;
  114. u32 qup_err;
  115. u32 opflags;
  116. bus_err = readl(qup->base + QUP_I2C_STATUS);
  117. qup_err = readl(qup->base + QUP_ERROR_FLAGS);
  118. opflags = readl(qup->base + QUP_OPERATIONAL);
  119. if (!qup->msg) {
  120. /* Clear Error interrupt */
  121. writel(QUP_RESET_STATE, qup->base + QUP_STATE);
  122. return IRQ_HANDLED;
  123. }
  124. bus_err &= I2C_STATUS_ERROR_MASK;
  125. qup_err &= QUP_STATUS_ERROR_FLAGS;
  126. if (qup_err) {
  127. /* Clear Error interrupt */
  128. writel(qup_err, qup->base + QUP_ERROR_FLAGS);
  129. goto done;
  130. }
  131. if (bus_err) {
  132. /* Clear Error interrupt */
  133. writel(QUP_RESET_STATE, qup->base + QUP_STATE);
  134. goto done;
  135. }
  136. if (opflags & QUP_IN_SVC_FLAG)
  137. writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL);
  138. if (opflags & QUP_OUT_SVC_FLAG)
  139. writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
  140. done:
  141. qup->qup_err = qup_err;
  142. qup->bus_err = bus_err;
  143. complete(&qup->xfer);
  144. return IRQ_HANDLED;
  145. }
  146. static int qup_i2c_poll_state_mask(struct qup_i2c_dev *qup,
  147. u32 req_state, u32 req_mask)
  148. {
  149. int retries = 1;
  150. u32 state;
  151. /*
  152. * State transition takes 3 AHB clocks cycles + 3 I2C master clock
  153. * cycles. So retry once after a 1uS delay.
  154. */
  155. do {
  156. state = readl(qup->base + QUP_STATE);
  157. if (state & QUP_STATE_VALID &&
  158. (state & req_mask) == req_state)
  159. return 0;
  160. udelay(1);
  161. } while (retries--);
  162. return -ETIMEDOUT;
  163. }
  164. static int qup_i2c_poll_state(struct qup_i2c_dev *qup, u32 req_state)
  165. {
  166. return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK);
  167. }
  168. static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup)
  169. {
  170. return qup_i2c_poll_state_mask(qup, 0, 0);
  171. }
  172. static int qup_i2c_poll_state_i2c_master(struct qup_i2c_dev *qup)
  173. {
  174. return qup_i2c_poll_state_mask(qup, QUP_I2C_MAST_GEN, QUP_I2C_MAST_GEN);
  175. }
  176. static int qup_i2c_change_state(struct qup_i2c_dev *qup, u32 state)
  177. {
  178. if (qup_i2c_poll_state_valid(qup) != 0)
  179. return -EIO;
  180. writel(state, qup->base + QUP_STATE);
  181. if (qup_i2c_poll_state(qup, state) != 0)
  182. return -EIO;
  183. return 0;
  184. }
  185. static int qup_i2c_wait_writeready(struct qup_i2c_dev *qup)
  186. {
  187. unsigned long timeout;
  188. u32 opflags;
  189. u32 status;
  190. timeout = jiffies + HZ;
  191. for (;;) {
  192. opflags = readl(qup->base + QUP_OPERATIONAL);
  193. status = readl(qup->base + QUP_I2C_STATUS);
  194. if (!(opflags & QUP_OUT_NOT_EMPTY) &&
  195. !(status & I2C_STATUS_BUS_ACTIVE))
  196. return 0;
  197. if (time_after(jiffies, timeout))
  198. return -ETIMEDOUT;
  199. usleep_range(qup->one_byte_t, qup->one_byte_t * 2);
  200. }
  201. }
  202. static void qup_i2c_set_write_mode(struct qup_i2c_dev *qup, struct i2c_msg *msg)
  203. {
  204. /* Number of entries to shift out, including the start */
  205. int total = msg->len + 1;
  206. if (total < qup->out_fifo_sz) {
  207. /* FIFO mode */
  208. writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
  209. writel(total, qup->base + QUP_MX_WRITE_CNT);
  210. } else {
  211. /* BLOCK mode (transfer data on chunks) */
  212. writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
  213. qup->base + QUP_IO_MODE);
  214. writel(total, qup->base + QUP_MX_OUTPUT_CNT);
  215. }
  216. }
  217. static void qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
  218. {
  219. u32 addr = msg->addr << 1;
  220. u32 qup_tag;
  221. u32 opflags;
  222. int idx;
  223. u32 val;
  224. if (qup->pos == 0) {
  225. val = QUP_TAG_START | addr;
  226. idx = 1;
  227. } else {
  228. val = 0;
  229. idx = 0;
  230. }
  231. while (qup->pos < msg->len) {
  232. /* Check that there's space in the FIFO for our pair */
  233. opflags = readl(qup->base + QUP_OPERATIONAL);
  234. if (opflags & QUP_OUT_FULL)
  235. break;
  236. if (qup->pos == msg->len - 1)
  237. qup_tag = QUP_TAG_STOP;
  238. else
  239. qup_tag = QUP_TAG_DATA;
  240. if (idx & 1)
  241. val |= (qup_tag | msg->buf[qup->pos]) << QUP_MSW_SHIFT;
  242. else
  243. val = qup_tag | msg->buf[qup->pos];
  244. /* Write out the pair and the last odd value */
  245. if (idx & 1 || qup->pos == msg->len - 1)
  246. writel(val, qup->base + QUP_OUT_FIFO_BASE);
  247. qup->pos++;
  248. idx++;
  249. }
  250. }
  251. static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
  252. {
  253. unsigned long left;
  254. int ret;
  255. qup->msg = msg;
  256. qup->pos = 0;
  257. enable_irq(qup->irq);
  258. qup_i2c_set_write_mode(qup, msg);
  259. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  260. if (ret)
  261. goto err;
  262. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  263. do {
  264. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  265. if (ret)
  266. goto err;
  267. qup_i2c_issue_write(qup, msg);
  268. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  269. if (ret)
  270. goto err;
  271. left = wait_for_completion_timeout(&qup->xfer, HZ);
  272. if (!left) {
  273. writel(1, qup->base + QUP_SW_RESET);
  274. ret = -ETIMEDOUT;
  275. goto err;
  276. }
  277. if (qup->bus_err || qup->qup_err) {
  278. if (qup->bus_err & QUP_I2C_NACK_FLAG)
  279. dev_err(qup->dev, "NACK from %x\n", msg->addr);
  280. ret = -EIO;
  281. goto err;
  282. }
  283. } while (qup->pos < msg->len);
  284. /* Wait for the outstanding data in the fifo to drain */
  285. ret = qup_i2c_wait_writeready(qup);
  286. err:
  287. disable_irq(qup->irq);
  288. qup->msg = NULL;
  289. return ret;
  290. }
  291. static void qup_i2c_set_read_mode(struct qup_i2c_dev *qup, int len)
  292. {
  293. if (len < qup->in_fifo_sz) {
  294. /* FIFO mode */
  295. writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
  296. writel(len, qup->base + QUP_MX_READ_CNT);
  297. } else {
  298. /* BLOCK mode (transfer data on chunks) */
  299. writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
  300. qup->base + QUP_IO_MODE);
  301. writel(len, qup->base + QUP_MX_INPUT_CNT);
  302. }
  303. }
  304. static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg)
  305. {
  306. u32 addr, len, val;
  307. addr = (msg->addr << 1) | 1;
  308. /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
  309. len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
  310. val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
  311. writel(val, qup->base + QUP_OUT_FIFO_BASE);
  312. }
  313. static void qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg)
  314. {
  315. u32 opflags;
  316. u32 val = 0;
  317. int idx;
  318. for (idx = 0; qup->pos < msg->len; idx++) {
  319. if ((idx & 1) == 0) {
  320. /* Check that FIFO have data */
  321. opflags = readl(qup->base + QUP_OPERATIONAL);
  322. if (!(opflags & QUP_IN_NOT_EMPTY))
  323. break;
  324. /* Reading 2 words at time */
  325. val = readl(qup->base + QUP_IN_FIFO_BASE);
  326. msg->buf[qup->pos++] = val & 0xFF;
  327. } else {
  328. msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
  329. }
  330. }
  331. }
  332. static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
  333. {
  334. unsigned long left;
  335. int ret;
  336. qup->msg = msg;
  337. qup->pos = 0;
  338. enable_irq(qup->irq);
  339. qup_i2c_set_read_mode(qup, msg->len);
  340. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  341. if (ret)
  342. goto err;
  343. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  344. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  345. if (ret)
  346. goto err;
  347. qup_i2c_issue_read(qup, msg);
  348. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  349. if (ret)
  350. goto err;
  351. do {
  352. left = wait_for_completion_timeout(&qup->xfer, HZ);
  353. if (!left) {
  354. writel(1, qup->base + QUP_SW_RESET);
  355. ret = -ETIMEDOUT;
  356. goto err;
  357. }
  358. if (qup->bus_err || qup->qup_err) {
  359. if (qup->bus_err & QUP_I2C_NACK_FLAG)
  360. dev_err(qup->dev, "NACK from %x\n", msg->addr);
  361. ret = -EIO;
  362. goto err;
  363. }
  364. qup_i2c_read_fifo(qup, msg);
  365. } while (qup->pos < msg->len);
  366. err:
  367. disable_irq(qup->irq);
  368. qup->msg = NULL;
  369. return ret;
  370. }
  371. static int qup_i2c_xfer(struct i2c_adapter *adap,
  372. struct i2c_msg msgs[],
  373. int num)
  374. {
  375. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  376. int ret, idx;
  377. ret = pm_runtime_get_sync(qup->dev);
  378. if (ret < 0)
  379. goto out;
  380. writel(1, qup->base + QUP_SW_RESET);
  381. ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
  382. if (ret)
  383. goto out;
  384. /* Configure QUP as I2C mini core */
  385. writel(I2C_MINI_CORE | I2C_N_VAL, qup->base + QUP_CONFIG);
  386. for (idx = 0; idx < num; idx++) {
  387. if (msgs[idx].len == 0) {
  388. ret = -EINVAL;
  389. goto out;
  390. }
  391. if (qup_i2c_poll_state_i2c_master(qup)) {
  392. ret = -EIO;
  393. goto out;
  394. }
  395. if (msgs[idx].flags & I2C_M_RD)
  396. ret = qup_i2c_read_one(qup, &msgs[idx]);
  397. else
  398. ret = qup_i2c_write_one(qup, &msgs[idx]);
  399. if (ret)
  400. break;
  401. ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
  402. if (ret)
  403. break;
  404. }
  405. if (ret == 0)
  406. ret = num;
  407. out:
  408. pm_runtime_mark_last_busy(qup->dev);
  409. pm_runtime_put_autosuspend(qup->dev);
  410. return ret;
  411. }
  412. static u32 qup_i2c_func(struct i2c_adapter *adap)
  413. {
  414. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  415. }
  416. static const struct i2c_algorithm qup_i2c_algo = {
  417. .master_xfer = qup_i2c_xfer,
  418. .functionality = qup_i2c_func,
  419. };
  420. /*
  421. * The QUP block will issue a NACK and STOP on the bus when reaching
  422. * the end of the read, the length of the read is specified as one byte
  423. * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
  424. */
  425. static struct i2c_adapter_quirks qup_i2c_quirks = {
  426. .max_read_len = QUP_READ_LIMIT,
  427. };
  428. static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
  429. {
  430. clk_prepare_enable(qup->clk);
  431. clk_prepare_enable(qup->pclk);
  432. }
  433. static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
  434. {
  435. u32 config;
  436. qup_i2c_change_state(qup, QUP_RESET_STATE);
  437. clk_disable_unprepare(qup->clk);
  438. config = readl(qup->base + QUP_CONFIG);
  439. config |= QUP_CLOCK_AUTO_GATE;
  440. writel(config, qup->base + QUP_CONFIG);
  441. clk_disable_unprepare(qup->pclk);
  442. }
  443. static int qup_i2c_probe(struct platform_device *pdev)
  444. {
  445. static const int blk_sizes[] = {4, 16, 32};
  446. struct device_node *node = pdev->dev.of_node;
  447. struct qup_i2c_dev *qup;
  448. unsigned long one_bit_t;
  449. struct resource *res;
  450. u32 io_mode, hw_ver, size;
  451. int ret, fs_div, hs_div;
  452. int src_clk_freq;
  453. u32 clk_freq = 100000;
  454. qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
  455. if (!qup)
  456. return -ENOMEM;
  457. qup->dev = &pdev->dev;
  458. init_completion(&qup->xfer);
  459. platform_set_drvdata(pdev, qup);
  460. of_property_read_u32(node, "clock-frequency", &clk_freq);
  461. /* We support frequencies up to FAST Mode (400KHz) */
  462. if (!clk_freq || clk_freq > 400000) {
  463. dev_err(qup->dev, "clock frequency not supported %d\n",
  464. clk_freq);
  465. return -EINVAL;
  466. }
  467. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  468. qup->base = devm_ioremap_resource(qup->dev, res);
  469. if (IS_ERR(qup->base))
  470. return PTR_ERR(qup->base);
  471. qup->irq = platform_get_irq(pdev, 0);
  472. if (qup->irq < 0) {
  473. dev_err(qup->dev, "No IRQ defined\n");
  474. return qup->irq;
  475. }
  476. qup->clk = devm_clk_get(qup->dev, "core");
  477. if (IS_ERR(qup->clk)) {
  478. dev_err(qup->dev, "Could not get core clock\n");
  479. return PTR_ERR(qup->clk);
  480. }
  481. qup->pclk = devm_clk_get(qup->dev, "iface");
  482. if (IS_ERR(qup->pclk)) {
  483. dev_err(qup->dev, "Could not get iface clock\n");
  484. return PTR_ERR(qup->pclk);
  485. }
  486. qup_i2c_enable_clocks(qup);
  487. /*
  488. * Bootloaders might leave a pending interrupt on certain QUP's,
  489. * so we reset the core before registering for interrupts.
  490. */
  491. writel(1, qup->base + QUP_SW_RESET);
  492. ret = qup_i2c_poll_state_valid(qup);
  493. if (ret)
  494. goto fail;
  495. ret = devm_request_irq(qup->dev, qup->irq, qup_i2c_interrupt,
  496. IRQF_TRIGGER_HIGH, "i2c_qup", qup);
  497. if (ret) {
  498. dev_err(qup->dev, "Request %d IRQ failed\n", qup->irq);
  499. goto fail;
  500. }
  501. disable_irq(qup->irq);
  502. hw_ver = readl(qup->base + QUP_HW_VERSION);
  503. dev_dbg(qup->dev, "Revision %x\n", hw_ver);
  504. io_mode = readl(qup->base + QUP_IO_MODE);
  505. /*
  506. * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
  507. * associated with each byte written/received
  508. */
  509. size = QUP_OUTPUT_BLOCK_SIZE(io_mode);
  510. if (size >= ARRAY_SIZE(blk_sizes)) {
  511. ret = -EIO;
  512. goto fail;
  513. }
  514. qup->out_blk_sz = blk_sizes[size] / 2;
  515. size = QUP_INPUT_BLOCK_SIZE(io_mode);
  516. if (size >= ARRAY_SIZE(blk_sizes)) {
  517. ret = -EIO;
  518. goto fail;
  519. }
  520. qup->in_blk_sz = blk_sizes[size] / 2;
  521. size = QUP_OUTPUT_FIFO_SIZE(io_mode);
  522. qup->out_fifo_sz = qup->out_blk_sz * (2 << size);
  523. size = QUP_INPUT_FIFO_SIZE(io_mode);
  524. qup->in_fifo_sz = qup->in_blk_sz * (2 << size);
  525. src_clk_freq = clk_get_rate(qup->clk);
  526. fs_div = ((src_clk_freq / clk_freq) / 2) - 3;
  527. hs_div = 3;
  528. qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff);
  529. /*
  530. * Time it takes for a byte to be clocked out on the bus.
  531. * Each byte takes 9 clock cycles (8 bits + 1 ack).
  532. */
  533. one_bit_t = (USEC_PER_SEC / clk_freq) + 1;
  534. qup->one_byte_t = one_bit_t * 9;
  535. dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
  536. qup->in_blk_sz, qup->in_fifo_sz,
  537. qup->out_blk_sz, qup->out_fifo_sz);
  538. i2c_set_adapdata(&qup->adap, qup);
  539. qup->adap.algo = &qup_i2c_algo;
  540. qup->adap.quirks = &qup_i2c_quirks;
  541. qup->adap.dev.parent = qup->dev;
  542. qup->adap.dev.of_node = pdev->dev.of_node;
  543. strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
  544. pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC);
  545. pm_runtime_use_autosuspend(qup->dev);
  546. pm_runtime_set_active(qup->dev);
  547. pm_runtime_enable(qup->dev);
  548. ret = i2c_add_adapter(&qup->adap);
  549. if (ret)
  550. goto fail_runtime;
  551. return 0;
  552. fail_runtime:
  553. pm_runtime_disable(qup->dev);
  554. pm_runtime_set_suspended(qup->dev);
  555. fail:
  556. qup_i2c_disable_clocks(qup);
  557. return ret;
  558. }
  559. static int qup_i2c_remove(struct platform_device *pdev)
  560. {
  561. struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
  562. disable_irq(qup->irq);
  563. qup_i2c_disable_clocks(qup);
  564. i2c_del_adapter(&qup->adap);
  565. pm_runtime_disable(qup->dev);
  566. pm_runtime_set_suspended(qup->dev);
  567. return 0;
  568. }
  569. #ifdef CONFIG_PM
  570. static int qup_i2c_pm_suspend_runtime(struct device *device)
  571. {
  572. struct qup_i2c_dev *qup = dev_get_drvdata(device);
  573. dev_dbg(device, "pm_runtime: suspending...\n");
  574. qup_i2c_disable_clocks(qup);
  575. return 0;
  576. }
  577. static int qup_i2c_pm_resume_runtime(struct device *device)
  578. {
  579. struct qup_i2c_dev *qup = dev_get_drvdata(device);
  580. dev_dbg(device, "pm_runtime: resuming...\n");
  581. qup_i2c_enable_clocks(qup);
  582. return 0;
  583. }
  584. #endif
  585. #ifdef CONFIG_PM_SLEEP
  586. static int qup_i2c_suspend(struct device *device)
  587. {
  588. if (!pm_runtime_suspended(device))
  589. return qup_i2c_pm_suspend_runtime(device);
  590. return 0;
  591. }
  592. static int qup_i2c_resume(struct device *device)
  593. {
  594. qup_i2c_pm_resume_runtime(device);
  595. pm_runtime_mark_last_busy(device);
  596. pm_request_autosuspend(device);
  597. return 0;
  598. }
  599. #endif
  600. static const struct dev_pm_ops qup_i2c_qup_pm_ops = {
  601. SET_SYSTEM_SLEEP_PM_OPS(
  602. qup_i2c_suspend,
  603. qup_i2c_resume)
  604. SET_RUNTIME_PM_OPS(
  605. qup_i2c_pm_suspend_runtime,
  606. qup_i2c_pm_resume_runtime,
  607. NULL)
  608. };
  609. static const struct of_device_id qup_i2c_dt_match[] = {
  610. { .compatible = "qcom,i2c-qup-v1.1.1" },
  611. { .compatible = "qcom,i2c-qup-v2.1.1" },
  612. { .compatible = "qcom,i2c-qup-v2.2.1" },
  613. {}
  614. };
  615. MODULE_DEVICE_TABLE(of, qup_i2c_dt_match);
  616. static struct platform_driver qup_i2c_driver = {
  617. .probe = qup_i2c_probe,
  618. .remove = qup_i2c_remove,
  619. .driver = {
  620. .name = "i2c_qup",
  621. .pm = &qup_i2c_qup_pm_ops,
  622. .of_match_table = qup_i2c_dt_match,
  623. },
  624. };
  625. module_platform_driver(qup_i2c_driver);
  626. MODULE_LICENSE("GPL v2");
  627. MODULE_ALIAS("platform:i2c_qup");