i2c-qup.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. /*
  337. * The QUP block will issue a NACK and STOP on the bus when reaching
  338. * the end of the read, the length of the read is specified as one byte
  339. * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
  340. */
  341. if (msg->len > QUP_READ_LIMIT) {
  342. dev_err(qup->dev, "HW not capable of reads over %d bytes\n",
  343. QUP_READ_LIMIT);
  344. return -EINVAL;
  345. }
  346. qup->msg = msg;
  347. qup->pos = 0;
  348. enable_irq(qup->irq);
  349. qup_i2c_set_read_mode(qup, msg->len);
  350. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  351. if (ret)
  352. goto err;
  353. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  354. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  355. if (ret)
  356. goto err;
  357. qup_i2c_issue_read(qup, msg);
  358. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  359. if (ret)
  360. goto err;
  361. do {
  362. left = wait_for_completion_timeout(&qup->xfer, HZ);
  363. if (!left) {
  364. writel(1, qup->base + QUP_SW_RESET);
  365. ret = -ETIMEDOUT;
  366. goto err;
  367. }
  368. if (qup->bus_err || qup->qup_err) {
  369. if (qup->bus_err & QUP_I2C_NACK_FLAG)
  370. dev_err(qup->dev, "NACK from %x\n", msg->addr);
  371. ret = -EIO;
  372. goto err;
  373. }
  374. qup_i2c_read_fifo(qup, msg);
  375. } while (qup->pos < msg->len);
  376. err:
  377. disable_irq(qup->irq);
  378. qup->msg = NULL;
  379. return ret;
  380. }
  381. static int qup_i2c_xfer(struct i2c_adapter *adap,
  382. struct i2c_msg msgs[],
  383. int num)
  384. {
  385. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  386. int ret, idx;
  387. ret = pm_runtime_get_sync(qup->dev);
  388. if (ret < 0)
  389. goto out;
  390. writel(1, qup->base + QUP_SW_RESET);
  391. ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
  392. if (ret)
  393. goto out;
  394. /* Configure QUP as I2C mini core */
  395. writel(I2C_MINI_CORE | I2C_N_VAL, qup->base + QUP_CONFIG);
  396. for (idx = 0; idx < num; idx++) {
  397. if (msgs[idx].len == 0) {
  398. ret = -EINVAL;
  399. goto out;
  400. }
  401. if (qup_i2c_poll_state_i2c_master(qup)) {
  402. ret = -EIO;
  403. goto out;
  404. }
  405. if (msgs[idx].flags & I2C_M_RD)
  406. ret = qup_i2c_read_one(qup, &msgs[idx]);
  407. else
  408. ret = qup_i2c_write_one(qup, &msgs[idx]);
  409. if (ret)
  410. break;
  411. ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
  412. if (ret)
  413. break;
  414. }
  415. if (ret == 0)
  416. ret = num;
  417. out:
  418. pm_runtime_mark_last_busy(qup->dev);
  419. pm_runtime_put_autosuspend(qup->dev);
  420. return ret;
  421. }
  422. static u32 qup_i2c_func(struct i2c_adapter *adap)
  423. {
  424. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  425. }
  426. static const struct i2c_algorithm qup_i2c_algo = {
  427. .master_xfer = qup_i2c_xfer,
  428. .functionality = qup_i2c_func,
  429. };
  430. static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
  431. {
  432. clk_prepare_enable(qup->clk);
  433. clk_prepare_enable(qup->pclk);
  434. }
  435. static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
  436. {
  437. u32 config;
  438. qup_i2c_change_state(qup, QUP_RESET_STATE);
  439. clk_disable_unprepare(qup->clk);
  440. config = readl(qup->base + QUP_CONFIG);
  441. config |= QUP_CLOCK_AUTO_GATE;
  442. writel(config, qup->base + QUP_CONFIG);
  443. clk_disable_unprepare(qup->pclk);
  444. }
  445. static int qup_i2c_probe(struct platform_device *pdev)
  446. {
  447. static const int blk_sizes[] = {4, 16, 32};
  448. struct device_node *node = pdev->dev.of_node;
  449. struct qup_i2c_dev *qup;
  450. unsigned long one_bit_t;
  451. struct resource *res;
  452. u32 io_mode, hw_ver, size;
  453. int ret, fs_div, hs_div;
  454. int src_clk_freq;
  455. u32 clk_freq = 100000;
  456. qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
  457. if (!qup)
  458. return -ENOMEM;
  459. qup->dev = &pdev->dev;
  460. init_completion(&qup->xfer);
  461. platform_set_drvdata(pdev, qup);
  462. of_property_read_u32(node, "clock-frequency", &clk_freq);
  463. /* We support frequencies up to FAST Mode (400KHz) */
  464. if (!clk_freq || clk_freq > 400000) {
  465. dev_err(qup->dev, "clock frequency not supported %d\n",
  466. clk_freq);
  467. return -EINVAL;
  468. }
  469. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  470. qup->base = devm_ioremap_resource(qup->dev, res);
  471. if (IS_ERR(qup->base))
  472. return PTR_ERR(qup->base);
  473. qup->irq = platform_get_irq(pdev, 0);
  474. if (qup->irq < 0) {
  475. dev_err(qup->dev, "No IRQ defined\n");
  476. return qup->irq;
  477. }
  478. qup->clk = devm_clk_get(qup->dev, "core");
  479. if (IS_ERR(qup->clk)) {
  480. dev_err(qup->dev, "Could not get core clock\n");
  481. return PTR_ERR(qup->clk);
  482. }
  483. qup->pclk = devm_clk_get(qup->dev, "iface");
  484. if (IS_ERR(qup->pclk)) {
  485. dev_err(qup->dev, "Could not get iface clock\n");
  486. return PTR_ERR(qup->pclk);
  487. }
  488. qup_i2c_enable_clocks(qup);
  489. /*
  490. * Bootloaders might leave a pending interrupt on certain QUP's,
  491. * so we reset the core before registering for interrupts.
  492. */
  493. writel(1, qup->base + QUP_SW_RESET);
  494. ret = qup_i2c_poll_state_valid(qup);
  495. if (ret)
  496. goto fail;
  497. ret = devm_request_irq(qup->dev, qup->irq, qup_i2c_interrupt,
  498. IRQF_TRIGGER_HIGH, "i2c_qup", qup);
  499. if (ret) {
  500. dev_err(qup->dev, "Request %d IRQ failed\n", qup->irq);
  501. goto fail;
  502. }
  503. disable_irq(qup->irq);
  504. hw_ver = readl(qup->base + QUP_HW_VERSION);
  505. dev_dbg(qup->dev, "Revision %x\n", hw_ver);
  506. io_mode = readl(qup->base + QUP_IO_MODE);
  507. /*
  508. * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
  509. * associated with each byte written/received
  510. */
  511. size = QUP_OUTPUT_BLOCK_SIZE(io_mode);
  512. if (size >= ARRAY_SIZE(blk_sizes))
  513. return -EIO;
  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. return -EIO;
  518. qup->in_blk_sz = blk_sizes[size] / 2;
  519. size = QUP_OUTPUT_FIFO_SIZE(io_mode);
  520. qup->out_fifo_sz = qup->out_blk_sz * (2 << size);
  521. size = QUP_INPUT_FIFO_SIZE(io_mode);
  522. qup->in_fifo_sz = qup->in_blk_sz * (2 << size);
  523. src_clk_freq = clk_get_rate(qup->clk);
  524. fs_div = ((src_clk_freq / clk_freq) / 2) - 3;
  525. hs_div = 3;
  526. qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff);
  527. /*
  528. * Time it takes for a byte to be clocked out on the bus.
  529. * Each byte takes 9 clock cycles (8 bits + 1 ack).
  530. */
  531. one_bit_t = (USEC_PER_SEC / clk_freq) + 1;
  532. qup->one_byte_t = one_bit_t * 9;
  533. dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
  534. qup->in_blk_sz, qup->in_fifo_sz,
  535. qup->out_blk_sz, qup->out_fifo_sz);
  536. i2c_set_adapdata(&qup->adap, qup);
  537. qup->adap.algo = &qup_i2c_algo;
  538. qup->adap.dev.parent = qup->dev;
  539. qup->adap.dev.of_node = pdev->dev.of_node;
  540. strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
  541. ret = i2c_add_adapter(&qup->adap);
  542. if (ret)
  543. goto fail;
  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. return 0;
  549. fail:
  550. qup_i2c_disable_clocks(qup);
  551. return ret;
  552. }
  553. static int qup_i2c_remove(struct platform_device *pdev)
  554. {
  555. struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
  556. disable_irq(qup->irq);
  557. qup_i2c_disable_clocks(qup);
  558. i2c_del_adapter(&qup->adap);
  559. pm_runtime_disable(qup->dev);
  560. pm_runtime_set_suspended(qup->dev);
  561. return 0;
  562. }
  563. #ifdef CONFIG_PM
  564. static int qup_i2c_pm_suspend_runtime(struct device *device)
  565. {
  566. struct qup_i2c_dev *qup = dev_get_drvdata(device);
  567. dev_dbg(device, "pm_runtime: suspending...\n");
  568. qup_i2c_disable_clocks(qup);
  569. return 0;
  570. }
  571. static int qup_i2c_pm_resume_runtime(struct device *device)
  572. {
  573. struct qup_i2c_dev *qup = dev_get_drvdata(device);
  574. dev_dbg(device, "pm_runtime: resuming...\n");
  575. qup_i2c_enable_clocks(qup);
  576. return 0;
  577. }
  578. #endif
  579. #ifdef CONFIG_PM_SLEEP
  580. static int qup_i2c_suspend(struct device *device)
  581. {
  582. qup_i2c_pm_suspend_runtime(device);
  583. return 0;
  584. }
  585. static int qup_i2c_resume(struct device *device)
  586. {
  587. qup_i2c_pm_resume_runtime(device);
  588. pm_runtime_mark_last_busy(device);
  589. pm_request_autosuspend(device);
  590. return 0;
  591. }
  592. #endif
  593. static const struct dev_pm_ops qup_i2c_qup_pm_ops = {
  594. SET_SYSTEM_SLEEP_PM_OPS(
  595. qup_i2c_suspend,
  596. qup_i2c_resume)
  597. SET_RUNTIME_PM_OPS(
  598. qup_i2c_pm_suspend_runtime,
  599. qup_i2c_pm_resume_runtime,
  600. NULL)
  601. };
  602. static const struct of_device_id qup_i2c_dt_match[] = {
  603. { .compatible = "qcom,i2c-qup-v1.1.1" },
  604. { .compatible = "qcom,i2c-qup-v2.1.1" },
  605. { .compatible = "qcom,i2c-qup-v2.2.1" },
  606. {}
  607. };
  608. MODULE_DEVICE_TABLE(of, qup_i2c_dt_match);
  609. static struct platform_driver qup_i2c_driver = {
  610. .probe = qup_i2c_probe,
  611. .remove = qup_i2c_remove,
  612. .driver = {
  613. .name = "i2c_qup",
  614. .owner = THIS_MODULE,
  615. .pm = &qup_i2c_qup_pm_ops,
  616. .of_match_table = qup_i2c_dt_match,
  617. },
  618. };
  619. module_platform_driver(qup_i2c_driver);
  620. MODULE_LICENSE("GPL v2");
  621. MODULE_ALIAS("platform:i2c_qup");