blob: 715d4d73efd06a2193d6deacca8eb89797fb15b4 [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
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700115
Sricharan R191424b2016-01-19 15:32:42 +0530116struct qup_i2c_block {
117 int count;
118 int pos;
119 int tx_tag_len;
120 int rx_tag_len;
121 int data_len;
122 u8 tags[6];
123};
124
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700125struct qup_i2c_dev {
126 struct device *dev;
127 void __iomem *base;
128 int irq;
129 struct clk *clk;
130 struct clk *pclk;
131 struct i2c_adapter adap;
132
133 int clk_ctl;
134 int out_fifo_sz;
135 int in_fifo_sz;
136 int out_blk_sz;
137 int in_blk_sz;
138
139 unsigned long one_byte_t;
Sricharan R191424b2016-01-19 15:32:42 +0530140 struct qup_i2c_block blk;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700141
142 struct i2c_msg *msg;
143 /* Current posion in user message buffer */
144 int pos;
145 /* I2C protocol errors */
146 u32 bus_err;
147 /* QUP core errors */
148 u32 qup_err;
149
150 struct completion xfer;
151};
152
153static irqreturn_t qup_i2c_interrupt(int irq, void *dev)
154{
155 struct qup_i2c_dev *qup = dev;
156 u32 bus_err;
157 u32 qup_err;
158 u32 opflags;
159
160 bus_err = readl(qup->base + QUP_I2C_STATUS);
161 qup_err = readl(qup->base + QUP_ERROR_FLAGS);
162 opflags = readl(qup->base + QUP_OPERATIONAL);
163
164 if (!qup->msg) {
165 /* Clear Error interrupt */
166 writel(QUP_RESET_STATE, qup->base + QUP_STATE);
167 return IRQ_HANDLED;
168 }
169
170 bus_err &= I2C_STATUS_ERROR_MASK;
171 qup_err &= QUP_STATUS_ERROR_FLAGS;
172
173 if (qup_err) {
174 /* Clear Error interrupt */
175 writel(qup_err, qup->base + QUP_ERROR_FLAGS);
176 goto done;
177 }
178
179 if (bus_err) {
180 /* Clear Error interrupt */
181 writel(QUP_RESET_STATE, qup->base + QUP_STATE);
182 goto done;
183 }
184
185 if (opflags & QUP_IN_SVC_FLAG)
186 writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL);
187
188 if (opflags & QUP_OUT_SVC_FLAG)
189 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
190
191done:
192 qup->qup_err = qup_err;
193 qup->bus_err = bus_err;
194 complete(&qup->xfer);
195 return IRQ_HANDLED;
196}
197
198static int qup_i2c_poll_state_mask(struct qup_i2c_dev *qup,
199 u32 req_state, u32 req_mask)
200{
201 int retries = 1;
202 u32 state;
203
204 /*
205 * State transition takes 3 AHB clocks cycles + 3 I2C master clock
206 * cycles. So retry once after a 1uS delay.
207 */
208 do {
209 state = readl(qup->base + QUP_STATE);
210
211 if (state & QUP_STATE_VALID &&
212 (state & req_mask) == req_state)
213 return 0;
214
215 udelay(1);
216 } while (retries--);
217
218 return -ETIMEDOUT;
219}
220
221static int qup_i2c_poll_state(struct qup_i2c_dev *qup, u32 req_state)
222{
223 return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK);
224}
225
226static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup)
227{
228 return qup_i2c_poll_state_mask(qup, 0, 0);
229}
230
231static int qup_i2c_poll_state_i2c_master(struct qup_i2c_dev *qup)
232{
233 return qup_i2c_poll_state_mask(qup, QUP_I2C_MAST_GEN, QUP_I2C_MAST_GEN);
234}
235
236static int qup_i2c_change_state(struct qup_i2c_dev *qup, u32 state)
237{
238 if (qup_i2c_poll_state_valid(qup) != 0)
239 return -EIO;
240
241 writel(state, qup->base + QUP_STATE);
242
243 if (qup_i2c_poll_state(qup, state) != 0)
244 return -EIO;
245 return 0;
246}
247
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530248/**
249 * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path
250 * @qup: The qup_i2c_dev device
251 * @op: The bit/event to wait on
252 * @val: value of the bit to wait on, 0 or 1
253 * @len: The length the bytes to be transferred
254 */
255static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val,
256 int len)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700257{
258 unsigned long timeout;
259 u32 opflags;
260 u32 status;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530261 u32 shift = __ffs(op);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700262
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530263 len *= qup->one_byte_t;
264 /* timeout after a wait of twice the max time */
265 timeout = jiffies + len * 4;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700266
267 for (;;) {
268 opflags = readl(qup->base + QUP_OPERATIONAL);
269 status = readl(qup->base + QUP_I2C_STATUS);
270
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530271 if (((opflags & op) >> shift) == val) {
272 if (op == QUP_OUT_NOT_EMPTY) {
273 if (!(status & I2C_STATUS_BUS_ACTIVE))
274 return 0;
275 } else {
276 return 0;
277 }
278 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700279
280 if (time_after(jiffies, timeout))
281 return -ETIMEDOUT;
282
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530283 usleep_range(len, len * 2);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700284 }
285}
286
Sricharan R191424b2016-01-19 15:32:42 +0530287static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup,
288 struct i2c_msg *msg)
289{
290 /* Number of entries to shift out, including the tags */
291 int total = msg->len + qup->blk.tx_tag_len;
292
293 if (total < qup->out_fifo_sz) {
294 /* FIFO mode */
295 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
296 writel(total, qup->base + QUP_MX_WRITE_CNT);
297 } else {
298 /* BLOCK mode (transfer data on chunks) */
299 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
300 qup->base + QUP_IO_MODE);
301 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
302 }
303}
304
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700305static void qup_i2c_set_write_mode(struct qup_i2c_dev *qup, struct i2c_msg *msg)
306{
307 /* Number of entries to shift out, including the start */
308 int total = msg->len + 1;
309
310 if (total < qup->out_fifo_sz) {
311 /* FIFO mode */
312 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
313 writel(total, qup->base + QUP_MX_WRITE_CNT);
314 } else {
315 /* BLOCK mode (transfer data on chunks) */
316 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
317 qup->base + QUP_IO_MODE);
318 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
319 }
320}
321
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530322static int qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700323{
324 u32 addr = msg->addr << 1;
325 u32 qup_tag;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700326 int idx;
327 u32 val;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530328 int ret = 0;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700329
330 if (qup->pos == 0) {
331 val = QUP_TAG_START | addr;
332 idx = 1;
333 } else {
334 val = 0;
335 idx = 0;
336 }
337
338 while (qup->pos < msg->len) {
339 /* Check that there's space in the FIFO for our pair */
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530340 ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL, RESET_BIT,
341 4 * ONE_BYTE);
342 if (ret)
343 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700344
345 if (qup->pos == msg->len - 1)
346 qup_tag = QUP_TAG_STOP;
347 else
348 qup_tag = QUP_TAG_DATA;
349
350 if (idx & 1)
351 val |= (qup_tag | msg->buf[qup->pos]) << QUP_MSW_SHIFT;
352 else
353 val = qup_tag | msg->buf[qup->pos];
354
355 /* Write out the pair and the last odd value */
356 if (idx & 1 || qup->pos == msg->len - 1)
357 writel(val, qup->base + QUP_OUT_FIFO_BASE);
358
359 qup->pos++;
360 idx++;
361 }
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530362
363 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700364}
365
Sricharan R191424b2016-01-19 15:32:42 +0530366static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
367 struct i2c_msg *msg)
368{
369 memset(&qup->blk, 0, sizeof(qup->blk));
370
371 qup->blk.data_len = msg->len;
372 qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT;
373
374 /* 4 bytes for first block and 2 writes for rest */
375 qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2;
376
377 /* There are 2 tag bytes that are read in to fifo for every block */
378 if (msg->flags & I2C_M_RD)
379 qup->blk.rx_tag_len = qup->blk.count * 2;
380}
381
382static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf,
383 int dlen, u8 *dbuf)
384{
385 u32 val = 0, idx = 0, pos = 0, i = 0, t;
386 int len = tlen + dlen;
387 u8 *buf = tbuf;
388 int ret = 0;
389
390 while (len > 0) {
391 ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL,
392 RESET_BIT, 4 * ONE_BYTE);
393 if (ret) {
394 dev_err(qup->dev, "timeout for fifo out full");
395 return ret;
396 }
397
398 t = (len >= 4) ? 4 : len;
399
400 while (idx < t) {
401 if (!i && (pos >= tlen)) {
402 buf = dbuf;
403 pos = 0;
404 i = 1;
405 }
406 val |= buf[pos++] << (idx++ * 8);
407 }
408
409 writel(val, qup->base + QUP_OUT_FIFO_BASE);
410 idx = 0;
411 val = 0;
412 len -= 4;
413 }
414
415 return ret;
416}
417
418static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
419{
420 int data_len;
421
422 if (qup->blk.data_len > QUP_READ_LIMIT)
423 data_len = QUP_READ_LIMIT;
424 else
425 data_len = qup->blk.data_len;
426
427 return data_len;
428}
429
430static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup,
431 struct i2c_msg *msg)
432{
433 u16 addr = (msg->addr << 1) | ((msg->flags & I2C_M_RD) == I2C_M_RD);
434 int len = 0;
435 int data_len;
436
437 if (qup->blk.pos == 0) {
438 tags[len++] = QUP_TAG_V2_START;
439 tags[len++] = addr & 0xff;
440
441 if (msg->flags & I2C_M_TEN)
442 tags[len++] = addr >> 8;
443 }
444
445 /* Send _STOP commands for the last block */
446 if (qup->blk.pos == (qup->blk.count - 1)) {
447 if (msg->flags & I2C_M_RD)
448 tags[len++] = QUP_TAG_V2_DATARD_STOP;
449 else
450 tags[len++] = QUP_TAG_V2_DATAWR_STOP;
451 } else {
452 if (msg->flags & I2C_M_RD)
453 tags[len++] = QUP_TAG_V2_DATARD;
454 else
455 tags[len++] = QUP_TAG_V2_DATAWR;
456 }
457
458 data_len = qup_i2c_get_data_len(qup);
459
460 /* 0 implies 256 bytes */
461 if (data_len == QUP_READ_LIMIT)
462 tags[len++] = 0;
463 else
464 tags[len++] = data_len;
465
466 return len;
467}
468
469static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
470{
471 int data_len = 0, tag_len, index;
472 int ret;
473
474 tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg);
475 index = msg->len - qup->blk.data_len;
476
477 /* only tags are written for read */
478 if (!(msg->flags & I2C_M_RD))
479 data_len = qup_i2c_get_data_len(qup);
480
481 ret = qup_i2c_send_data(qup, tag_len, qup->blk.tags,
482 data_len, &msg->buf[index]);
483 qup->blk.data_len -= data_len;
484
485 return ret;
486}
487
488static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
489 struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700490{
491 unsigned long left;
Sricharan R191424b2016-01-19 15:32:42 +0530492 int ret = 0;
493
494 left = wait_for_completion_timeout(&qup->xfer, HZ);
495 if (!left) {
496 writel(1, qup->base + QUP_SW_RESET);
497 ret = -ETIMEDOUT;
498 }
499
500 if (qup->bus_err || qup->qup_err) {
501 if (qup->bus_err & QUP_I2C_NACK_FLAG) {
502 dev_err(qup->dev, "NACK from %x\n", msg->addr);
503 ret = -EIO;
504 }
505 }
506
507 return ret;
508}
509
510static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
511{
512 int ret = 0;
513
514 qup->msg = msg;
515 qup->pos = 0;
516 enable_irq(qup->irq);
517 qup_i2c_set_blk_data(qup, msg);
518 qup_i2c_set_write_mode_v2(qup, msg);
519
520 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
521 if (ret)
522 goto err;
523
524 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
525
526 do {
527 ret = qup_i2c_issue_xfer_v2(qup, msg);
528 if (ret)
529 goto err;
530
531 ret = qup_i2c_wait_for_complete(qup, msg);
532 if (ret)
533 goto err;
534
535 qup->blk.pos++;
536 } while (qup->blk.pos < qup->blk.count);
537
538 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
539
540err:
541 disable_irq(qup->irq);
542 qup->msg = NULL;
543
544 return ret;
545}
546
547static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
548{
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700549 int ret;
550
551 qup->msg = msg;
552 qup->pos = 0;
553
554 enable_irq(qup->irq);
555
556 qup_i2c_set_write_mode(qup, msg);
557
558 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
559 if (ret)
560 goto err;
561
562 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
563
564 do {
565 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
566 if (ret)
567 goto err;
568
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530569 ret = qup_i2c_issue_write(qup, msg);
570 if (ret)
571 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700572
573 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
574 if (ret)
575 goto err;
576
Sricharan R191424b2016-01-19 15:32:42 +0530577 ret = qup_i2c_wait_for_complete(qup, msg);
578 if (ret)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700579 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700580 } while (qup->pos < msg->len);
581
582 /* Wait for the outstanding data in the fifo to drain */
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530583 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700584
585err:
586 disable_irq(qup->irq);
587 qup->msg = NULL;
588
589 return ret;
590}
591
592static void qup_i2c_set_read_mode(struct qup_i2c_dev *qup, int len)
593{
594 if (len < qup->in_fifo_sz) {
595 /* FIFO mode */
596 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
597 writel(len, qup->base + QUP_MX_READ_CNT);
598 } else {
599 /* BLOCK mode (transfer data on chunks) */
600 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
601 qup->base + QUP_IO_MODE);
602 writel(len, qup->base + QUP_MX_INPUT_CNT);
603 }
604}
605
Sricharan R191424b2016-01-19 15:32:42 +0530606static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
607{
608 int tx_len = qup->blk.tx_tag_len;
609
610 len += qup->blk.rx_tag_len;
611
612 if (len < qup->in_fifo_sz) {
613 /* FIFO mode */
614 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
615 writel(len, qup->base + QUP_MX_READ_CNT);
616 writel(tx_len, qup->base + QUP_MX_WRITE_CNT);
617 } else {
618 /* BLOCK mode (transfer data on chunks) */
619 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
620 qup->base + QUP_IO_MODE);
621 writel(len, qup->base + QUP_MX_INPUT_CNT);
622 writel(tx_len, qup->base + QUP_MX_OUTPUT_CNT);
623 }
624}
625
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700626static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg)
627{
628 u32 addr, len, val;
629
630 addr = (msg->addr << 1) | 1;
631
632 /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
633 len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
634
635 val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
636 writel(val, qup->base + QUP_OUT_FIFO_BASE);
637}
638
639
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530640static int qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700641{
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700642 u32 val = 0;
643 int idx;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530644 int ret = 0;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700645
646 for (idx = 0; qup->pos < msg->len; idx++) {
647 if ((idx & 1) == 0) {
648 /* Check that FIFO have data */
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530649 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
650 SET_BIT, 4 * ONE_BYTE);
651 if (ret)
652 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700653
654 /* Reading 2 words at time */
655 val = readl(qup->base + QUP_IN_FIFO_BASE);
656
657 msg->buf[qup->pos++] = val & 0xFF;
658 } else {
659 msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
660 }
661 }
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530662
663 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700664}
665
Sricharan R191424b2016-01-19 15:32:42 +0530666static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
667 struct i2c_msg *msg)
668{
669 u32 val;
670 int idx, pos = 0, ret = 0, total;
671
672 total = qup_i2c_get_data_len(qup);
673
674 /* 2 extra bytes for read tags */
675 while (pos < (total + 2)) {
676 /* Check that FIFO have data */
677 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
678 SET_BIT, 4 * ONE_BYTE);
679 if (ret) {
680 dev_err(qup->dev, "timeout for fifo not empty");
681 return ret;
682 }
683 val = readl(qup->base + QUP_IN_FIFO_BASE);
684
685 for (idx = 0; idx < 4; idx++, val >>= 8, pos++) {
686 /* first 2 bytes are tag bytes */
687 if (pos < 2)
688 continue;
689
690 if (pos >= (total + 2))
691 goto out;
692
693 msg->buf[qup->pos++] = val & 0xff;
694 }
695 }
696
697out:
698 qup->blk.data_len -= total;
699
700 return ret;
701}
702
703static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
704{
705 int ret = 0;
706
707 qup->msg = msg;
708 qup->pos = 0;
709 enable_irq(qup->irq);
710 qup_i2c_set_blk_data(qup, msg);
711 qup_i2c_set_read_mode_v2(qup, msg->len);
712
713 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
714 if (ret)
715 goto err;
716
717 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
718
719 do {
720 ret = qup_i2c_issue_xfer_v2(qup, msg);
721 if (ret)
722 goto err;
723
724 ret = qup_i2c_wait_for_complete(qup, msg);
725 if (ret)
726 goto err;
727
728 ret = qup_i2c_read_fifo_v2(qup, msg);
729 if (ret)
730 goto err;
731
732 qup->blk.pos++;
733 } while (qup->blk.pos < qup->blk.count);
734
735err:
736 disable_irq(qup->irq);
737 qup->msg = NULL;
738
739 return ret;
740}
741
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700742static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
743{
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700744 int ret;
745
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700746 qup->msg = msg;
747 qup->pos = 0;
748
749 enable_irq(qup->irq);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700750 qup_i2c_set_read_mode(qup, msg->len);
751
752 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
753 if (ret)
754 goto err;
755
756 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
757
758 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
759 if (ret)
760 goto err;
761
762 qup_i2c_issue_read(qup, msg);
763
764 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
765 if (ret)
766 goto err;
767
768 do {
Sricharan R191424b2016-01-19 15:32:42 +0530769 ret = qup_i2c_wait_for_complete(qup, msg);
770 if (ret)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700771 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700772
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530773 ret = qup_i2c_read_fifo(qup, msg);
774 if (ret)
775 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700776 } while (qup->pos < msg->len);
777
778err:
779 disable_irq(qup->irq);
780 qup->msg = NULL;
781
782 return ret;
783}
784
785static int qup_i2c_xfer(struct i2c_adapter *adap,
786 struct i2c_msg msgs[],
787 int num)
788{
789 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
790 int ret, idx;
791
792 ret = pm_runtime_get_sync(qup->dev);
Andy Grossfa01d092014-05-02 20:54:29 -0500793 if (ret < 0)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700794 goto out;
795
796 writel(1, qup->base + QUP_SW_RESET);
797 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
798 if (ret)
799 goto out;
800
801 /* Configure QUP as I2C mini core */
802 writel(I2C_MINI_CORE | I2C_N_VAL, qup->base + QUP_CONFIG);
803
804 for (idx = 0; idx < num; idx++) {
805 if (msgs[idx].len == 0) {
806 ret = -EINVAL;
807 goto out;
808 }
809
810 if (qup_i2c_poll_state_i2c_master(qup)) {
811 ret = -EIO;
812 goto out;
813 }
814
815 if (msgs[idx].flags & I2C_M_RD)
816 ret = qup_i2c_read_one(qup, &msgs[idx]);
817 else
818 ret = qup_i2c_write_one(qup, &msgs[idx]);
819
820 if (ret)
821 break;
822
823 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
824 if (ret)
825 break;
826 }
827
828 if (ret == 0)
829 ret = num;
830out:
831
832 pm_runtime_mark_last_busy(qup->dev);
833 pm_runtime_put_autosuspend(qup->dev);
834
835 return ret;
836}
837
Sricharan R191424b2016-01-19 15:32:42 +0530838static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
839 struct i2c_msg msgs[],
840 int num)
841{
842 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
843 int ret, idx;
844
845 ret = pm_runtime_get_sync(qup->dev);
846 if (ret < 0)
847 goto out;
848
849 writel(1, qup->base + QUP_SW_RESET);
850 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
851 if (ret)
852 goto out;
853
854 /* Configure QUP as I2C mini core */
855 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
856 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
857
858 for (idx = 0; idx < num; idx++) {
859 if (msgs[idx].len == 0) {
860 ret = -EINVAL;
861 goto out;
862 }
863
864 if (qup_i2c_poll_state_i2c_master(qup)) {
865 ret = -EIO;
866 goto out;
867 }
868
869 reinit_completion(&qup->xfer);
870
871 if (msgs[idx].flags & I2C_M_RD)
872 ret = qup_i2c_read_one_v2(qup, &msgs[idx]);
873 else
874 ret = qup_i2c_write_one_v2(qup, &msgs[idx]);
875
876 if (!ret)
877 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
878
879 if (ret)
880 break;
881 }
882
883 if (ret == 0)
884 ret = num;
885out:
886 pm_runtime_mark_last_busy(qup->dev);
887 pm_runtime_put_autosuspend(qup->dev);
888
889 return ret;
890}
891
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700892static u32 qup_i2c_func(struct i2c_adapter *adap)
893{
894 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
895}
896
897static const struct i2c_algorithm qup_i2c_algo = {
898 .master_xfer = qup_i2c_xfer,
899 .functionality = qup_i2c_func,
900};
901
Sricharan R191424b2016-01-19 15:32:42 +0530902static const struct i2c_algorithm qup_i2c_algo_v2 = {
903 .master_xfer = qup_i2c_xfer_v2,
904 .functionality = qup_i2c_func,
905};
906
Wolfram Sang994647d2015-01-07 12:24:10 +0100907/*
908 * The QUP block will issue a NACK and STOP on the bus when reaching
909 * the end of the read, the length of the read is specified as one byte
910 * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
911 */
912static struct i2c_adapter_quirks qup_i2c_quirks = {
913 .max_read_len = QUP_READ_LIMIT,
914};
915
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700916static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
917{
918 clk_prepare_enable(qup->clk);
919 clk_prepare_enable(qup->pclk);
920}
921
922static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
923{
924 u32 config;
925
926 qup_i2c_change_state(qup, QUP_RESET_STATE);
927 clk_disable_unprepare(qup->clk);
928 config = readl(qup->base + QUP_CONFIG);
929 config |= QUP_CLOCK_AUTO_GATE;
930 writel(config, qup->base + QUP_CONFIG);
931 clk_disable_unprepare(qup->pclk);
932}
933
934static int qup_i2c_probe(struct platform_device *pdev)
935{
936 static const int blk_sizes[] = {4, 16, 32};
937 struct device_node *node = pdev->dev.of_node;
938 struct qup_i2c_dev *qup;
939 unsigned long one_bit_t;
940 struct resource *res;
941 u32 io_mode, hw_ver, size;
942 int ret, fs_div, hs_div;
943 int src_clk_freq;
Wolfram Sangcf23e332014-04-03 11:30:33 +0200944 u32 clk_freq = 100000;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700945
946 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
947 if (!qup)
948 return -ENOMEM;
949
950 qup->dev = &pdev->dev;
951 init_completion(&qup->xfer);
952 platform_set_drvdata(pdev, qup);
953
954 of_property_read_u32(node, "clock-frequency", &clk_freq);
955
Sricharan R191424b2016-01-19 15:32:42 +0530956 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
957 qup->adap.algo = &qup_i2c_algo;
958 qup->adap.quirks = &qup_i2c_quirks;
959 } else {
960 qup->adap.algo = &qup_i2c_algo_v2;
961 }
962
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700963 /* We support frequencies up to FAST Mode (400KHz) */
964 if (!clk_freq || clk_freq > 400000) {
965 dev_err(qup->dev, "clock frequency not supported %d\n",
966 clk_freq);
967 return -EINVAL;
968 }
969
970 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
971 qup->base = devm_ioremap_resource(qup->dev, res);
972 if (IS_ERR(qup->base))
973 return PTR_ERR(qup->base);
974
975 qup->irq = platform_get_irq(pdev, 0);
976 if (qup->irq < 0) {
977 dev_err(qup->dev, "No IRQ defined\n");
978 return qup->irq;
979 }
980
981 qup->clk = devm_clk_get(qup->dev, "core");
982 if (IS_ERR(qup->clk)) {
983 dev_err(qup->dev, "Could not get core clock\n");
984 return PTR_ERR(qup->clk);
985 }
986
987 qup->pclk = devm_clk_get(qup->dev, "iface");
988 if (IS_ERR(qup->pclk)) {
989 dev_err(qup->dev, "Could not get iface clock\n");
990 return PTR_ERR(qup->pclk);
991 }
992
993 qup_i2c_enable_clocks(qup);
994
995 /*
996 * Bootloaders might leave a pending interrupt on certain QUP's,
997 * so we reset the core before registering for interrupts.
998 */
999 writel(1, qup->base + QUP_SW_RESET);
1000 ret = qup_i2c_poll_state_valid(qup);
1001 if (ret)
1002 goto fail;
1003
1004 ret = devm_request_irq(qup->dev, qup->irq, qup_i2c_interrupt,
1005 IRQF_TRIGGER_HIGH, "i2c_qup", qup);
1006 if (ret) {
1007 dev_err(qup->dev, "Request %d IRQ failed\n", qup->irq);
1008 goto fail;
1009 }
1010 disable_irq(qup->irq);
1011
1012 hw_ver = readl(qup->base + QUP_HW_VERSION);
1013 dev_dbg(qup->dev, "Revision %x\n", hw_ver);
1014
1015 io_mode = readl(qup->base + QUP_IO_MODE);
1016
1017 /*
1018 * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
1019 * associated with each byte written/received
1020 */
1021 size = QUP_OUTPUT_BLOCK_SIZE(io_mode);
Pramod Gurav3cf357d2014-08-06 18:03:25 +05301022 if (size >= ARRAY_SIZE(blk_sizes)) {
1023 ret = -EIO;
1024 goto fail;
1025 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001026 qup->out_blk_sz = blk_sizes[size] / 2;
1027
1028 size = QUP_INPUT_BLOCK_SIZE(io_mode);
Pramod Gurav3cf357d2014-08-06 18:03:25 +05301029 if (size >= ARRAY_SIZE(blk_sizes)) {
1030 ret = -EIO;
1031 goto fail;
1032 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001033 qup->in_blk_sz = blk_sizes[size] / 2;
1034
1035 size = QUP_OUTPUT_FIFO_SIZE(io_mode);
1036 qup->out_fifo_sz = qup->out_blk_sz * (2 << size);
1037
1038 size = QUP_INPUT_FIFO_SIZE(io_mode);
1039 qup->in_fifo_sz = qup->in_blk_sz * (2 << size);
1040
1041 src_clk_freq = clk_get_rate(qup->clk);
1042 fs_div = ((src_clk_freq / clk_freq) / 2) - 3;
1043 hs_div = 3;
1044 qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff);
1045
1046 /*
1047 * Time it takes for a byte to be clocked out on the bus.
1048 * Each byte takes 9 clock cycles (8 bits + 1 ack).
1049 */
1050 one_bit_t = (USEC_PER_SEC / clk_freq) + 1;
1051 qup->one_byte_t = one_bit_t * 9;
1052
1053 dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
1054 qup->in_blk_sz, qup->in_fifo_sz,
1055 qup->out_blk_sz, qup->out_fifo_sz);
1056
1057 i2c_set_adapdata(&qup->adap, qup);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001058 qup->adap.dev.parent = qup->dev;
1059 qup->adap.dev.of_node = pdev->dev.of_node;
1060 strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
1061
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001062 pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC);
1063 pm_runtime_use_autosuspend(qup->dev);
1064 pm_runtime_set_active(qup->dev);
1065 pm_runtime_enable(qup->dev);
Andy Gross86b59bb2014-09-29 17:00:51 -05001066
1067 ret = i2c_add_adapter(&qup->adap);
1068 if (ret)
1069 goto fail_runtime;
1070
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001071 return 0;
1072
Andy Gross86b59bb2014-09-29 17:00:51 -05001073fail_runtime:
1074 pm_runtime_disable(qup->dev);
1075 pm_runtime_set_suspended(qup->dev);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001076fail:
1077 qup_i2c_disable_clocks(qup);
1078 return ret;
1079}
1080
1081static int qup_i2c_remove(struct platform_device *pdev)
1082{
1083 struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
1084
1085 disable_irq(qup->irq);
1086 qup_i2c_disable_clocks(qup);
1087 i2c_del_adapter(&qup->adap);
1088 pm_runtime_disable(qup->dev);
1089 pm_runtime_set_suspended(qup->dev);
1090 return 0;
1091}
1092
1093#ifdef CONFIG_PM
1094static int qup_i2c_pm_suspend_runtime(struct device *device)
1095{
1096 struct qup_i2c_dev *qup = dev_get_drvdata(device);
1097
1098 dev_dbg(device, "pm_runtime: suspending...\n");
1099 qup_i2c_disable_clocks(qup);
1100 return 0;
1101}
1102
1103static int qup_i2c_pm_resume_runtime(struct device *device)
1104{
1105 struct qup_i2c_dev *qup = dev_get_drvdata(device);
1106
1107 dev_dbg(device, "pm_runtime: resuming...\n");
1108 qup_i2c_enable_clocks(qup);
1109 return 0;
1110}
1111#endif
1112
1113#ifdef CONFIG_PM_SLEEP
1114static int qup_i2c_suspend(struct device *device)
1115{
1116 qup_i2c_pm_suspend_runtime(device);
1117 return 0;
1118}
1119
1120static int qup_i2c_resume(struct device *device)
1121{
1122 qup_i2c_pm_resume_runtime(device);
1123 pm_runtime_mark_last_busy(device);
1124 pm_request_autosuspend(device);
1125 return 0;
1126}
1127#endif
1128
1129static const struct dev_pm_ops qup_i2c_qup_pm_ops = {
1130 SET_SYSTEM_SLEEP_PM_OPS(
1131 qup_i2c_suspend,
1132 qup_i2c_resume)
1133 SET_RUNTIME_PM_OPS(
1134 qup_i2c_pm_suspend_runtime,
1135 qup_i2c_pm_resume_runtime,
1136 NULL)
1137};
1138
1139static const struct of_device_id qup_i2c_dt_match[] = {
1140 { .compatible = "qcom,i2c-qup-v1.1.1" },
1141 { .compatible = "qcom,i2c-qup-v2.1.1" },
1142 { .compatible = "qcom,i2c-qup-v2.2.1" },
1143 {}
1144};
1145MODULE_DEVICE_TABLE(of, qup_i2c_dt_match);
1146
1147static struct platform_driver qup_i2c_driver = {
1148 .probe = qup_i2c_probe,
1149 .remove = qup_i2c_remove,
1150 .driver = {
1151 .name = "i2c_qup",
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001152 .pm = &qup_i2c_qup_pm_ops,
1153 .of_match_table = qup_i2c_dt_match,
1154 },
1155};
1156
1157module_platform_driver(qup_i2c_driver);
1158
1159MODULE_LICENSE("GPL v2");
1160MODULE_ALIAS("platform:i2c_qup");