blob: f9009d6312f113b19241c6ce0dfa84404a9d8177 [file] [log] [blame]
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001/*
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
17#include <linux/clk.h>
18#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/i2c.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/module.h>
24#include <linux/of.h>
25#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
27
28/* QUP Registers */
29#define QUP_CONFIG 0x000
30#define QUP_STATE 0x004
31#define QUP_IO_MODE 0x008
32#define QUP_SW_RESET 0x00c
33#define QUP_OPERATIONAL 0x018
34#define QUP_ERROR_FLAGS 0x01c
35#define QUP_ERROR_FLAGS_EN 0x020
36#define QUP_HW_VERSION 0x030
37#define QUP_MX_OUTPUT_CNT 0x100
38#define QUP_OUT_FIFO_BASE 0x110
39#define QUP_MX_WRITE_CNT 0x150
40#define QUP_MX_INPUT_CNT 0x200
41#define QUP_MX_READ_CNT 0x208
42#define QUP_IN_FIFO_BASE 0x218
43#define QUP_I2C_CLK_CTL 0x400
44#define QUP_I2C_STATUS 0x404
Sricharan R191424b2016-01-19 15:32:42 +053045#define QUP_I2C_MASTER_GEN 0x408
Bjorn Andersson10c5a842014-03-13 19:07:43 -070046
47/* QUP States and reset values */
48#define QUP_RESET_STATE 0
49#define QUP_RUN_STATE 1
50#define QUP_PAUSE_STATE 3
51#define QUP_STATE_MASK 3
52
53#define QUP_STATE_VALID BIT(2)
54#define QUP_I2C_MAST_GEN BIT(4)
55
56#define QUP_OPERATIONAL_RESET 0x000ff0
57#define QUP_I2C_STATUS_RESET 0xfffffc
58
59/* QUP OPERATIONAL FLAGS */
60#define QUP_I2C_NACK_FLAG BIT(3)
61#define QUP_OUT_NOT_EMPTY BIT(4)
62#define QUP_IN_NOT_EMPTY BIT(5)
63#define QUP_OUT_FULL BIT(6)
64#define QUP_OUT_SVC_FLAG BIT(8)
65#define QUP_IN_SVC_FLAG BIT(9)
66#define QUP_MX_OUTPUT_DONE BIT(10)
67#define QUP_MX_INPUT_DONE BIT(11)
68
69/* I2C mini core related values */
70#define QUP_CLOCK_AUTO_GATE BIT(13)
71#define I2C_MINI_CORE (2 << 8)
72#define I2C_N_VAL 15
Sricharan R191424b2016-01-19 15:32:42 +053073#define I2C_N_VAL_V2 7
74
Bjorn Andersson10c5a842014-03-13 19:07:43 -070075/* Most significant word offset in FIFO port */
76#define QUP_MSW_SHIFT (I2C_N_VAL + 1)
77
78/* Packing/Unpacking words in FIFOs, and IO modes */
79#define QUP_OUTPUT_BLK_MODE (1 << 10)
80#define QUP_INPUT_BLK_MODE (1 << 12)
81#define QUP_UNPACK_EN BIT(14)
82#define QUP_PACK_EN BIT(15)
83
84#define QUP_REPACK_EN (QUP_UNPACK_EN | QUP_PACK_EN)
Sricharan R191424b2016-01-19 15:32:42 +053085#define QUP_V2_TAGS_EN 1
Bjorn Andersson10c5a842014-03-13 19:07:43 -070086
87#define QUP_OUTPUT_BLOCK_SIZE(x)(((x) >> 0) & 0x03)
88#define QUP_OUTPUT_FIFO_SIZE(x) (((x) >> 2) & 0x07)
89#define QUP_INPUT_BLOCK_SIZE(x) (((x) >> 5) & 0x03)
90#define QUP_INPUT_FIFO_SIZE(x) (((x) >> 7) & 0x07)
91
92/* QUP tags */
93#define QUP_TAG_START (1 << 8)
94#define QUP_TAG_DATA (2 << 8)
95#define QUP_TAG_STOP (3 << 8)
96#define QUP_TAG_REC (4 << 8)
97
Sricharan R191424b2016-01-19 15:32:42 +053098/* QUP v2 tags */
99#define QUP_TAG_V2_START 0x81
100#define QUP_TAG_V2_DATAWR 0x82
101#define QUP_TAG_V2_DATAWR_STOP 0x83
102#define QUP_TAG_V2_DATARD 0x85
103#define QUP_TAG_V2_DATARD_STOP 0x87
104
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700105/* Status, Error flags */
106#define I2C_STATUS_WR_BUFFER_FULL BIT(0)
107#define I2C_STATUS_BUS_ACTIVE BIT(8)
108#define I2C_STATUS_ERROR_MASK 0x38000fc
109#define QUP_STATUS_ERROR_FLAGS 0x7c
110
111#define QUP_READ_LIMIT 256
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530112#define SET_BIT 0x1
113#define RESET_BIT 0x0
114#define ONE_BYTE 0x1
Sricharan Rf7418792016-01-19 15:32:43 +0530115#define QUP_I2C_MX_CONFIG_DURING_RUN BIT(31)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700116
Sricharan R191424b2016-01-19 15:32:42 +0530117struct qup_i2c_block {
118 int count;
119 int pos;
120 int tx_tag_len;
121 int rx_tag_len;
122 int data_len;
123 u8 tags[6];
124};
125
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700126struct qup_i2c_dev {
127 struct device *dev;
128 void __iomem *base;
129 int irq;
130 struct clk *clk;
131 struct clk *pclk;
132 struct i2c_adapter adap;
133
134 int clk_ctl;
135 int out_fifo_sz;
136 int in_fifo_sz;
137 int out_blk_sz;
138 int in_blk_sz;
139
140 unsigned long one_byte_t;
Sricharan R191424b2016-01-19 15:32:42 +0530141 struct qup_i2c_block blk;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700142
143 struct i2c_msg *msg;
144 /* Current posion in user message buffer */
145 int pos;
146 /* I2C protocol errors */
147 u32 bus_err;
148 /* QUP core errors */
149 u32 qup_err;
150
Sricharan Rf7418792016-01-19 15:32:43 +0530151 /* To check if this is the last msg */
152 bool is_last;
153
154 /* To configure when bus is in run state */
155 int config_run;
156
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700157 struct completion xfer;
158};
159
160static irqreturn_t qup_i2c_interrupt(int irq, void *dev)
161{
162 struct qup_i2c_dev *qup = dev;
163 u32 bus_err;
164 u32 qup_err;
165 u32 opflags;
166
167 bus_err = readl(qup->base + QUP_I2C_STATUS);
168 qup_err = readl(qup->base + QUP_ERROR_FLAGS);
169 opflags = readl(qup->base + QUP_OPERATIONAL);
170
171 if (!qup->msg) {
172 /* Clear Error interrupt */
173 writel(QUP_RESET_STATE, qup->base + QUP_STATE);
174 return IRQ_HANDLED;
175 }
176
177 bus_err &= I2C_STATUS_ERROR_MASK;
178 qup_err &= QUP_STATUS_ERROR_FLAGS;
179
180 if (qup_err) {
181 /* Clear Error interrupt */
182 writel(qup_err, qup->base + QUP_ERROR_FLAGS);
183 goto done;
184 }
185
186 if (bus_err) {
187 /* Clear Error interrupt */
188 writel(QUP_RESET_STATE, qup->base + QUP_STATE);
189 goto done;
190 }
191
192 if (opflags & QUP_IN_SVC_FLAG)
193 writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL);
194
195 if (opflags & QUP_OUT_SVC_FLAG)
196 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
197
198done:
199 qup->qup_err = qup_err;
200 qup->bus_err = bus_err;
201 complete(&qup->xfer);
202 return IRQ_HANDLED;
203}
204
205static int qup_i2c_poll_state_mask(struct qup_i2c_dev *qup,
206 u32 req_state, u32 req_mask)
207{
208 int retries = 1;
209 u32 state;
210
211 /*
212 * State transition takes 3 AHB clocks cycles + 3 I2C master clock
213 * cycles. So retry once after a 1uS delay.
214 */
215 do {
216 state = readl(qup->base + QUP_STATE);
217
218 if (state & QUP_STATE_VALID &&
219 (state & req_mask) == req_state)
220 return 0;
221
222 udelay(1);
223 } while (retries--);
224
225 return -ETIMEDOUT;
226}
227
228static int qup_i2c_poll_state(struct qup_i2c_dev *qup, u32 req_state)
229{
230 return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK);
231}
232
233static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup)
234{
235 return qup_i2c_poll_state_mask(qup, 0, 0);
236}
237
238static int qup_i2c_poll_state_i2c_master(struct qup_i2c_dev *qup)
239{
240 return qup_i2c_poll_state_mask(qup, QUP_I2C_MAST_GEN, QUP_I2C_MAST_GEN);
241}
242
243static int qup_i2c_change_state(struct qup_i2c_dev *qup, u32 state)
244{
245 if (qup_i2c_poll_state_valid(qup) != 0)
246 return -EIO;
247
248 writel(state, qup->base + QUP_STATE);
249
250 if (qup_i2c_poll_state(qup, state) != 0)
251 return -EIO;
252 return 0;
253}
254
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530255/**
256 * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path
257 * @qup: The qup_i2c_dev device
258 * @op: The bit/event to wait on
259 * @val: value of the bit to wait on, 0 or 1
260 * @len: The length the bytes to be transferred
261 */
262static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val,
263 int len)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700264{
265 unsigned long timeout;
266 u32 opflags;
267 u32 status;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530268 u32 shift = __ffs(op);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700269
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530270 len *= qup->one_byte_t;
271 /* timeout after a wait of twice the max time */
272 timeout = jiffies + len * 4;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700273
274 for (;;) {
275 opflags = readl(qup->base + QUP_OPERATIONAL);
276 status = readl(qup->base + QUP_I2C_STATUS);
277
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530278 if (((opflags & op) >> shift) == val) {
Sricharan Rf7418792016-01-19 15:32:43 +0530279 if ((op == QUP_OUT_NOT_EMPTY) && qup->is_last) {
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530280 if (!(status & I2C_STATUS_BUS_ACTIVE))
281 return 0;
282 } else {
283 return 0;
284 }
285 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700286
287 if (time_after(jiffies, timeout))
288 return -ETIMEDOUT;
289
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530290 usleep_range(len, len * 2);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700291 }
292}
293
Sricharan R191424b2016-01-19 15:32:42 +0530294static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup,
295 struct i2c_msg *msg)
296{
297 /* Number of entries to shift out, including the tags */
298 int total = msg->len + qup->blk.tx_tag_len;
299
Sricharan Rf7418792016-01-19 15:32:43 +0530300 total |= qup->config_run;
301
Sricharan R191424b2016-01-19 15:32:42 +0530302 if (total < qup->out_fifo_sz) {
303 /* FIFO mode */
304 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
305 writel(total, qup->base + QUP_MX_WRITE_CNT);
306 } else {
307 /* BLOCK mode (transfer data on chunks) */
308 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
309 qup->base + QUP_IO_MODE);
310 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
311 }
312}
313
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700314static void qup_i2c_set_write_mode(struct qup_i2c_dev *qup, struct i2c_msg *msg)
315{
316 /* Number of entries to shift out, including the start */
317 int total = msg->len + 1;
318
319 if (total < qup->out_fifo_sz) {
320 /* FIFO mode */
321 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
322 writel(total, qup->base + QUP_MX_WRITE_CNT);
323 } else {
324 /* BLOCK mode (transfer data on chunks) */
325 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
326 qup->base + QUP_IO_MODE);
327 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
328 }
329}
330
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530331static int qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700332{
333 u32 addr = msg->addr << 1;
334 u32 qup_tag;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700335 int idx;
336 u32 val;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530337 int ret = 0;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700338
339 if (qup->pos == 0) {
340 val = QUP_TAG_START | addr;
341 idx = 1;
342 } else {
343 val = 0;
344 idx = 0;
345 }
346
347 while (qup->pos < msg->len) {
348 /* Check that there's space in the FIFO for our pair */
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530349 ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL, RESET_BIT,
350 4 * ONE_BYTE);
351 if (ret)
352 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700353
354 if (qup->pos == msg->len - 1)
355 qup_tag = QUP_TAG_STOP;
356 else
357 qup_tag = QUP_TAG_DATA;
358
359 if (idx & 1)
360 val |= (qup_tag | msg->buf[qup->pos]) << QUP_MSW_SHIFT;
361 else
362 val = qup_tag | msg->buf[qup->pos];
363
364 /* Write out the pair and the last odd value */
365 if (idx & 1 || qup->pos == msg->len - 1)
366 writel(val, qup->base + QUP_OUT_FIFO_BASE);
367
368 qup->pos++;
369 idx++;
370 }
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530371
372 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700373}
374
Sricharan R191424b2016-01-19 15:32:42 +0530375static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
376 struct i2c_msg *msg)
377{
378 memset(&qup->blk, 0, sizeof(qup->blk));
379
380 qup->blk.data_len = msg->len;
381 qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT;
382
383 /* 4 bytes for first block and 2 writes for rest */
384 qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2;
385
386 /* There are 2 tag bytes that are read in to fifo for every block */
387 if (msg->flags & I2C_M_RD)
388 qup->blk.rx_tag_len = qup->blk.count * 2;
389}
390
391static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf,
392 int dlen, u8 *dbuf)
393{
394 u32 val = 0, idx = 0, pos = 0, i = 0, t;
395 int len = tlen + dlen;
396 u8 *buf = tbuf;
397 int ret = 0;
398
399 while (len > 0) {
400 ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL,
401 RESET_BIT, 4 * ONE_BYTE);
402 if (ret) {
403 dev_err(qup->dev, "timeout for fifo out full");
404 return ret;
405 }
406
407 t = (len >= 4) ? 4 : len;
408
409 while (idx < t) {
410 if (!i && (pos >= tlen)) {
411 buf = dbuf;
412 pos = 0;
413 i = 1;
414 }
415 val |= buf[pos++] << (idx++ * 8);
416 }
417
418 writel(val, qup->base + QUP_OUT_FIFO_BASE);
419 idx = 0;
420 val = 0;
421 len -= 4;
422 }
423
424 return ret;
425}
426
427static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
428{
429 int data_len;
430
431 if (qup->blk.data_len > QUP_READ_LIMIT)
432 data_len = QUP_READ_LIMIT;
433 else
434 data_len = qup->blk.data_len;
435
436 return data_len;
437}
438
439static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup,
440 struct i2c_msg *msg)
441{
442 u16 addr = (msg->addr << 1) | ((msg->flags & I2C_M_RD) == I2C_M_RD);
443 int len = 0;
444 int data_len;
445
446 if (qup->blk.pos == 0) {
447 tags[len++] = QUP_TAG_V2_START;
448 tags[len++] = addr & 0xff;
449
450 if (msg->flags & I2C_M_TEN)
451 tags[len++] = addr >> 8;
452 }
453
454 /* Send _STOP commands for the last block */
Sricharan Rf7418792016-01-19 15:32:43 +0530455 if ((qup->blk.pos == (qup->blk.count - 1)) && qup->is_last) {
Sricharan R191424b2016-01-19 15:32:42 +0530456 if (msg->flags & I2C_M_RD)
457 tags[len++] = QUP_TAG_V2_DATARD_STOP;
458 else
459 tags[len++] = QUP_TAG_V2_DATAWR_STOP;
460 } else {
461 if (msg->flags & I2C_M_RD)
462 tags[len++] = QUP_TAG_V2_DATARD;
463 else
464 tags[len++] = QUP_TAG_V2_DATAWR;
465 }
466
467 data_len = qup_i2c_get_data_len(qup);
468
469 /* 0 implies 256 bytes */
470 if (data_len == QUP_READ_LIMIT)
471 tags[len++] = 0;
472 else
473 tags[len++] = data_len;
474
475 return len;
476}
477
478static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
479{
480 int data_len = 0, tag_len, index;
481 int ret;
482
483 tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg);
484 index = msg->len - qup->blk.data_len;
485
486 /* only tags are written for read */
487 if (!(msg->flags & I2C_M_RD))
488 data_len = qup_i2c_get_data_len(qup);
489
490 ret = qup_i2c_send_data(qup, tag_len, qup->blk.tags,
491 data_len, &msg->buf[index]);
492 qup->blk.data_len -= data_len;
493
494 return ret;
495}
496
497static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
498 struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700499{
500 unsigned long left;
Sricharan R191424b2016-01-19 15:32:42 +0530501 int ret = 0;
502
503 left = wait_for_completion_timeout(&qup->xfer, HZ);
504 if (!left) {
505 writel(1, qup->base + QUP_SW_RESET);
506 ret = -ETIMEDOUT;
507 }
508
509 if (qup->bus_err || qup->qup_err) {
510 if (qup->bus_err & QUP_I2C_NACK_FLAG) {
511 dev_err(qup->dev, "NACK from %x\n", msg->addr);
512 ret = -EIO;
513 }
514 }
515
516 return ret;
517}
518
519static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
520{
521 int ret = 0;
522
523 qup->msg = msg;
524 qup->pos = 0;
525 enable_irq(qup->irq);
526 qup_i2c_set_blk_data(qup, msg);
527 qup_i2c_set_write_mode_v2(qup, msg);
528
529 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
530 if (ret)
531 goto err;
532
533 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
534
535 do {
536 ret = qup_i2c_issue_xfer_v2(qup, msg);
537 if (ret)
538 goto err;
539
540 ret = qup_i2c_wait_for_complete(qup, msg);
541 if (ret)
542 goto err;
543
544 qup->blk.pos++;
545 } while (qup->blk.pos < qup->blk.count);
546
547 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
548
549err:
550 disable_irq(qup->irq);
551 qup->msg = NULL;
552
553 return ret;
554}
555
556static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
557{
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700558 int ret;
559
560 qup->msg = msg;
561 qup->pos = 0;
562
563 enable_irq(qup->irq);
564
565 qup_i2c_set_write_mode(qup, msg);
566
567 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
568 if (ret)
569 goto err;
570
571 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
572
573 do {
574 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
575 if (ret)
576 goto err;
577
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530578 ret = qup_i2c_issue_write(qup, msg);
579 if (ret)
580 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700581
582 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
583 if (ret)
584 goto err;
585
Sricharan R191424b2016-01-19 15:32:42 +0530586 ret = qup_i2c_wait_for_complete(qup, msg);
587 if (ret)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700588 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700589 } while (qup->pos < msg->len);
590
591 /* Wait for the outstanding data in the fifo to drain */
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530592 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700593err:
594 disable_irq(qup->irq);
595 qup->msg = NULL;
596
597 return ret;
598}
599
600static void qup_i2c_set_read_mode(struct qup_i2c_dev *qup, int len)
601{
602 if (len < qup->in_fifo_sz) {
603 /* FIFO mode */
604 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
605 writel(len, qup->base + QUP_MX_READ_CNT);
606 } else {
607 /* BLOCK mode (transfer data on chunks) */
608 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
609 qup->base + QUP_IO_MODE);
610 writel(len, qup->base + QUP_MX_INPUT_CNT);
611 }
612}
613
Sricharan R191424b2016-01-19 15:32:42 +0530614static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
615{
616 int tx_len = qup->blk.tx_tag_len;
617
618 len += qup->blk.rx_tag_len;
Sricharan Rf7418792016-01-19 15:32:43 +0530619 len |= qup->config_run;
620 tx_len |= qup->config_run;
Sricharan R191424b2016-01-19 15:32:42 +0530621
622 if (len < qup->in_fifo_sz) {
623 /* FIFO mode */
624 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
Sricharan R191424b2016-01-19 15:32:42 +0530625 writel(tx_len, qup->base + QUP_MX_WRITE_CNT);
Sricharan Rf7418792016-01-19 15:32:43 +0530626 writel(len, qup->base + QUP_MX_READ_CNT);
Sricharan R191424b2016-01-19 15:32:42 +0530627 } else {
628 /* BLOCK mode (transfer data on chunks) */
629 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
630 qup->base + QUP_IO_MODE);
Sricharan R191424b2016-01-19 15:32:42 +0530631 writel(tx_len, qup->base + QUP_MX_OUTPUT_CNT);
Sricharan Rf7418792016-01-19 15:32:43 +0530632 writel(len, qup->base + QUP_MX_INPUT_CNT);
Sricharan R191424b2016-01-19 15:32:42 +0530633 }
634}
635
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700636static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg)
637{
638 u32 addr, len, val;
639
640 addr = (msg->addr << 1) | 1;
641
642 /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
643 len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
644
645 val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
646 writel(val, qup->base + QUP_OUT_FIFO_BASE);
647}
648
649
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530650static int qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700651{
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700652 u32 val = 0;
653 int idx;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530654 int ret = 0;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700655
656 for (idx = 0; qup->pos < msg->len; idx++) {
657 if ((idx & 1) == 0) {
658 /* Check that FIFO have data */
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530659 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
660 SET_BIT, 4 * ONE_BYTE);
661 if (ret)
662 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700663
664 /* Reading 2 words at time */
665 val = readl(qup->base + QUP_IN_FIFO_BASE);
666
667 msg->buf[qup->pos++] = val & 0xFF;
668 } else {
669 msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
670 }
671 }
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530672
673 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700674}
675
Sricharan R191424b2016-01-19 15:32:42 +0530676static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
677 struct i2c_msg *msg)
678{
679 u32 val;
680 int idx, pos = 0, ret = 0, total;
681
682 total = qup_i2c_get_data_len(qup);
683
684 /* 2 extra bytes for read tags */
685 while (pos < (total + 2)) {
686 /* Check that FIFO have data */
687 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
688 SET_BIT, 4 * ONE_BYTE);
689 if (ret) {
690 dev_err(qup->dev, "timeout for fifo not empty");
691 return ret;
692 }
693 val = readl(qup->base + QUP_IN_FIFO_BASE);
694
695 for (idx = 0; idx < 4; idx++, val >>= 8, pos++) {
696 /* first 2 bytes are tag bytes */
697 if (pos < 2)
698 continue;
699
700 if (pos >= (total + 2))
701 goto out;
702
703 msg->buf[qup->pos++] = val & 0xff;
704 }
705 }
706
707out:
708 qup->blk.data_len -= total;
709
710 return ret;
711}
712
713static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
714{
715 int ret = 0;
716
717 qup->msg = msg;
718 qup->pos = 0;
719 enable_irq(qup->irq);
720 qup_i2c_set_blk_data(qup, msg);
721 qup_i2c_set_read_mode_v2(qup, msg->len);
722
723 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
724 if (ret)
725 goto err;
726
727 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
728
729 do {
730 ret = qup_i2c_issue_xfer_v2(qup, msg);
731 if (ret)
732 goto err;
733
734 ret = qup_i2c_wait_for_complete(qup, msg);
735 if (ret)
736 goto err;
737
738 ret = qup_i2c_read_fifo_v2(qup, msg);
739 if (ret)
740 goto err;
741
742 qup->blk.pos++;
743 } while (qup->blk.pos < qup->blk.count);
744
745err:
746 disable_irq(qup->irq);
747 qup->msg = NULL;
748
749 return ret;
750}
751
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700752static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
753{
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700754 int ret;
755
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700756 qup->msg = msg;
757 qup->pos = 0;
758
759 enable_irq(qup->irq);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700760 qup_i2c_set_read_mode(qup, msg->len);
761
762 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
763 if (ret)
764 goto err;
765
766 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
767
768 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
769 if (ret)
770 goto err;
771
772 qup_i2c_issue_read(qup, msg);
773
774 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
775 if (ret)
776 goto err;
777
778 do {
Sricharan R191424b2016-01-19 15:32:42 +0530779 ret = qup_i2c_wait_for_complete(qup, msg);
780 if (ret)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700781 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700782
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530783 ret = qup_i2c_read_fifo(qup, msg);
784 if (ret)
785 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700786 } while (qup->pos < msg->len);
787
788err:
789 disable_irq(qup->irq);
790 qup->msg = NULL;
791
792 return ret;
793}
794
795static int qup_i2c_xfer(struct i2c_adapter *adap,
796 struct i2c_msg msgs[],
797 int num)
798{
799 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
800 int ret, idx;
801
802 ret = pm_runtime_get_sync(qup->dev);
Andy Grossfa01d092014-05-02 20:54:29 -0500803 if (ret < 0)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700804 goto out;
805
806 writel(1, qup->base + QUP_SW_RESET);
807 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
808 if (ret)
809 goto out;
810
811 /* Configure QUP as I2C mini core */
812 writel(I2C_MINI_CORE | I2C_N_VAL, qup->base + QUP_CONFIG);
813
814 for (idx = 0; idx < num; idx++) {
815 if (msgs[idx].len == 0) {
816 ret = -EINVAL;
817 goto out;
818 }
819
820 if (qup_i2c_poll_state_i2c_master(qup)) {
821 ret = -EIO;
822 goto out;
823 }
824
825 if (msgs[idx].flags & I2C_M_RD)
826 ret = qup_i2c_read_one(qup, &msgs[idx]);
827 else
828 ret = qup_i2c_write_one(qup, &msgs[idx]);
829
830 if (ret)
831 break;
832
833 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
834 if (ret)
835 break;
836 }
837
838 if (ret == 0)
839 ret = num;
840out:
841
842 pm_runtime_mark_last_busy(qup->dev);
843 pm_runtime_put_autosuspend(qup->dev);
844
845 return ret;
846}
847
Sricharan R191424b2016-01-19 15:32:42 +0530848static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
849 struct i2c_msg msgs[],
850 int num)
851{
852 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
853 int ret, idx;
854
855 ret = pm_runtime_get_sync(qup->dev);
856 if (ret < 0)
857 goto out;
858
859 writel(1, qup->base + QUP_SW_RESET);
860 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
861 if (ret)
862 goto out;
863
864 /* Configure QUP as I2C mini core */
865 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
866 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
867
868 for (idx = 0; idx < num; idx++) {
869 if (msgs[idx].len == 0) {
870 ret = -EINVAL;
871 goto out;
872 }
873
874 if (qup_i2c_poll_state_i2c_master(qup)) {
875 ret = -EIO;
876 goto out;
877 }
878
Sricharan Rf7418792016-01-19 15:32:43 +0530879 qup->is_last = (idx == (num - 1));
880 if (idx)
881 qup->config_run = QUP_I2C_MX_CONFIG_DURING_RUN;
882 else
883 qup->config_run = 0;
884
Sricharan R191424b2016-01-19 15:32:42 +0530885 reinit_completion(&qup->xfer);
886
887 if (msgs[idx].flags & I2C_M_RD)
888 ret = qup_i2c_read_one_v2(qup, &msgs[idx]);
889 else
890 ret = qup_i2c_write_one_v2(qup, &msgs[idx]);
891
Sricharan R191424b2016-01-19 15:32:42 +0530892 if (ret)
893 break;
894 }
895
Sricharan Rf7418792016-01-19 15:32:43 +0530896 if (!ret)
897 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
898
Sricharan R191424b2016-01-19 15:32:42 +0530899 if (ret == 0)
900 ret = num;
901out:
902 pm_runtime_mark_last_busy(qup->dev);
903 pm_runtime_put_autosuspend(qup->dev);
904
905 return ret;
906}
907
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700908static u32 qup_i2c_func(struct i2c_adapter *adap)
909{
910 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
911}
912
913static const struct i2c_algorithm qup_i2c_algo = {
914 .master_xfer = qup_i2c_xfer,
915 .functionality = qup_i2c_func,
916};
917
Sricharan R191424b2016-01-19 15:32:42 +0530918static const struct i2c_algorithm qup_i2c_algo_v2 = {
919 .master_xfer = qup_i2c_xfer_v2,
920 .functionality = qup_i2c_func,
921};
922
Wolfram Sang994647d2015-01-07 12:24:10 +0100923/*
924 * The QUP block will issue a NACK and STOP on the bus when reaching
925 * the end of the read, the length of the read is specified as one byte
926 * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
927 */
928static struct i2c_adapter_quirks qup_i2c_quirks = {
929 .max_read_len = QUP_READ_LIMIT,
930};
931
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700932static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
933{
934 clk_prepare_enable(qup->clk);
935 clk_prepare_enable(qup->pclk);
936}
937
938static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
939{
940 u32 config;
941
942 qup_i2c_change_state(qup, QUP_RESET_STATE);
943 clk_disable_unprepare(qup->clk);
944 config = readl(qup->base + QUP_CONFIG);
945 config |= QUP_CLOCK_AUTO_GATE;
946 writel(config, qup->base + QUP_CONFIG);
947 clk_disable_unprepare(qup->pclk);
948}
949
950static int qup_i2c_probe(struct platform_device *pdev)
951{
952 static const int blk_sizes[] = {4, 16, 32};
953 struct device_node *node = pdev->dev.of_node;
954 struct qup_i2c_dev *qup;
955 unsigned long one_bit_t;
956 struct resource *res;
957 u32 io_mode, hw_ver, size;
958 int ret, fs_div, hs_div;
959 int src_clk_freq;
Wolfram Sangcf23e332014-04-03 11:30:33 +0200960 u32 clk_freq = 100000;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700961
962 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
963 if (!qup)
964 return -ENOMEM;
965
966 qup->dev = &pdev->dev;
967 init_completion(&qup->xfer);
968 platform_set_drvdata(pdev, qup);
969
970 of_property_read_u32(node, "clock-frequency", &clk_freq);
971
Sricharan R191424b2016-01-19 15:32:42 +0530972 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
973 qup->adap.algo = &qup_i2c_algo;
974 qup->adap.quirks = &qup_i2c_quirks;
975 } else {
976 qup->adap.algo = &qup_i2c_algo_v2;
977 }
978
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700979 /* We support frequencies up to FAST Mode (400KHz) */
980 if (!clk_freq || clk_freq > 400000) {
981 dev_err(qup->dev, "clock frequency not supported %d\n",
982 clk_freq);
983 return -EINVAL;
984 }
985
986 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
987 qup->base = devm_ioremap_resource(qup->dev, res);
988 if (IS_ERR(qup->base))
989 return PTR_ERR(qup->base);
990
991 qup->irq = platform_get_irq(pdev, 0);
992 if (qup->irq < 0) {
993 dev_err(qup->dev, "No IRQ defined\n");
994 return qup->irq;
995 }
996
997 qup->clk = devm_clk_get(qup->dev, "core");
998 if (IS_ERR(qup->clk)) {
999 dev_err(qup->dev, "Could not get core clock\n");
1000 return PTR_ERR(qup->clk);
1001 }
1002
1003 qup->pclk = devm_clk_get(qup->dev, "iface");
1004 if (IS_ERR(qup->pclk)) {
1005 dev_err(qup->dev, "Could not get iface clock\n");
1006 return PTR_ERR(qup->pclk);
1007 }
1008
1009 qup_i2c_enable_clocks(qup);
1010
1011 /*
1012 * Bootloaders might leave a pending interrupt on certain QUP's,
1013 * so we reset the core before registering for interrupts.
1014 */
1015 writel(1, qup->base + QUP_SW_RESET);
1016 ret = qup_i2c_poll_state_valid(qup);
1017 if (ret)
1018 goto fail;
1019
1020 ret = devm_request_irq(qup->dev, qup->irq, qup_i2c_interrupt,
1021 IRQF_TRIGGER_HIGH, "i2c_qup", qup);
1022 if (ret) {
1023 dev_err(qup->dev, "Request %d IRQ failed\n", qup->irq);
1024 goto fail;
1025 }
1026 disable_irq(qup->irq);
1027
1028 hw_ver = readl(qup->base + QUP_HW_VERSION);
1029 dev_dbg(qup->dev, "Revision %x\n", hw_ver);
1030
1031 io_mode = readl(qup->base + QUP_IO_MODE);
1032
1033 /*
1034 * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
1035 * associated with each byte written/received
1036 */
1037 size = QUP_OUTPUT_BLOCK_SIZE(io_mode);
Pramod Gurav3cf357d2014-08-06 18:03:25 +05301038 if (size >= ARRAY_SIZE(blk_sizes)) {
1039 ret = -EIO;
1040 goto fail;
1041 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001042 qup->out_blk_sz = blk_sizes[size] / 2;
1043
1044 size = QUP_INPUT_BLOCK_SIZE(io_mode);
Pramod Gurav3cf357d2014-08-06 18:03:25 +05301045 if (size >= ARRAY_SIZE(blk_sizes)) {
1046 ret = -EIO;
1047 goto fail;
1048 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001049 qup->in_blk_sz = blk_sizes[size] / 2;
1050
1051 size = QUP_OUTPUT_FIFO_SIZE(io_mode);
1052 qup->out_fifo_sz = qup->out_blk_sz * (2 << size);
1053
1054 size = QUP_INPUT_FIFO_SIZE(io_mode);
1055 qup->in_fifo_sz = qup->in_blk_sz * (2 << size);
1056
1057 src_clk_freq = clk_get_rate(qup->clk);
1058 fs_div = ((src_clk_freq / clk_freq) / 2) - 3;
1059 hs_div = 3;
1060 qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff);
1061
1062 /*
1063 * Time it takes for a byte to be clocked out on the bus.
1064 * Each byte takes 9 clock cycles (8 bits + 1 ack).
1065 */
1066 one_bit_t = (USEC_PER_SEC / clk_freq) + 1;
1067 qup->one_byte_t = one_bit_t * 9;
1068
1069 dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
1070 qup->in_blk_sz, qup->in_fifo_sz,
1071 qup->out_blk_sz, qup->out_fifo_sz);
1072
1073 i2c_set_adapdata(&qup->adap, qup);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001074 qup->adap.dev.parent = qup->dev;
1075 qup->adap.dev.of_node = pdev->dev.of_node;
Sricharan Rf7418792016-01-19 15:32:43 +05301076 qup->is_last = 1;
1077
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001078 strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
1079
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001080 pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC);
1081 pm_runtime_use_autosuspend(qup->dev);
1082 pm_runtime_set_active(qup->dev);
1083 pm_runtime_enable(qup->dev);
Andy Gross86b59bb2014-09-29 17:00:51 -05001084
1085 ret = i2c_add_adapter(&qup->adap);
1086 if (ret)
1087 goto fail_runtime;
1088
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001089 return 0;
1090
Andy Gross86b59bb2014-09-29 17:00:51 -05001091fail_runtime:
1092 pm_runtime_disable(qup->dev);
1093 pm_runtime_set_suspended(qup->dev);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001094fail:
1095 qup_i2c_disable_clocks(qup);
1096 return ret;
1097}
1098
1099static int qup_i2c_remove(struct platform_device *pdev)
1100{
1101 struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
1102
1103 disable_irq(qup->irq);
1104 qup_i2c_disable_clocks(qup);
1105 i2c_del_adapter(&qup->adap);
1106 pm_runtime_disable(qup->dev);
1107 pm_runtime_set_suspended(qup->dev);
1108 return 0;
1109}
1110
1111#ifdef CONFIG_PM
1112static int qup_i2c_pm_suspend_runtime(struct device *device)
1113{
1114 struct qup_i2c_dev *qup = dev_get_drvdata(device);
1115
1116 dev_dbg(device, "pm_runtime: suspending...\n");
1117 qup_i2c_disable_clocks(qup);
1118 return 0;
1119}
1120
1121static int qup_i2c_pm_resume_runtime(struct device *device)
1122{
1123 struct qup_i2c_dev *qup = dev_get_drvdata(device);
1124
1125 dev_dbg(device, "pm_runtime: resuming...\n");
1126 qup_i2c_enable_clocks(qup);
1127 return 0;
1128}
1129#endif
1130
1131#ifdef CONFIG_PM_SLEEP
1132static int qup_i2c_suspend(struct device *device)
1133{
1134 qup_i2c_pm_suspend_runtime(device);
1135 return 0;
1136}
1137
1138static int qup_i2c_resume(struct device *device)
1139{
1140 qup_i2c_pm_resume_runtime(device);
1141 pm_runtime_mark_last_busy(device);
1142 pm_request_autosuspend(device);
1143 return 0;
1144}
1145#endif
1146
1147static const struct dev_pm_ops qup_i2c_qup_pm_ops = {
1148 SET_SYSTEM_SLEEP_PM_OPS(
1149 qup_i2c_suspend,
1150 qup_i2c_resume)
1151 SET_RUNTIME_PM_OPS(
1152 qup_i2c_pm_suspend_runtime,
1153 qup_i2c_pm_resume_runtime,
1154 NULL)
1155};
1156
1157static const struct of_device_id qup_i2c_dt_match[] = {
1158 { .compatible = "qcom,i2c-qup-v1.1.1" },
1159 { .compatible = "qcom,i2c-qup-v2.1.1" },
1160 { .compatible = "qcom,i2c-qup-v2.2.1" },
1161 {}
1162};
1163MODULE_DEVICE_TABLE(of, qup_i2c_dt_match);
1164
1165static struct platform_driver qup_i2c_driver = {
1166 .probe = qup_i2c_probe,
1167 .remove = qup_i2c_remove,
1168 .driver = {
1169 .name = "i2c_qup",
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001170 .pm = &qup_i2c_qup_pm_ops,
1171 .of_match_table = qup_i2c_dt_match,
1172 },
1173};
1174
1175module_platform_driver(qup_i2c_driver);
1176
1177MODULE_LICENSE("GPL v2");
1178MODULE_ALIAS("platform:i2c_qup");