blob: 686f262e34f7d9fed9eb49591ce3731ee1b87ef3 [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
Sricharan R9cedf3b2016-02-22 17:38:15 +053017#include <linux/atomic.h>
Bjorn Andersson10c5a842014-03-13 19:07:43 -070018#include <linux/clk.h>
19#include <linux/delay.h>
Sricharan R9cedf3b2016-02-22 17:38:15 +053020#include <linux/dmaengine.h>
21#include <linux/dmapool.h>
22#include <linux/dma-mapping.h>
Bjorn Andersson10c5a842014-03-13 19:07:43 -070023#include <linux/err.h>
24#include <linux/i2c.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/module.h>
28#include <linux/of.h>
29#include <linux/platform_device.h>
30#include <linux/pm_runtime.h>
Sricharan R9cedf3b2016-02-22 17:38:15 +053031#include <linux/scatterlist.h>
Bjorn Andersson10c5a842014-03-13 19:07:43 -070032
33/* QUP Registers */
34#define QUP_CONFIG 0x000
35#define QUP_STATE 0x004
36#define QUP_IO_MODE 0x008
37#define QUP_SW_RESET 0x00c
38#define QUP_OPERATIONAL 0x018
39#define QUP_ERROR_FLAGS 0x01c
40#define QUP_ERROR_FLAGS_EN 0x020
Sricharan R9cedf3b2016-02-22 17:38:15 +053041#define QUP_OPERATIONAL_MASK 0x028
Bjorn Andersson10c5a842014-03-13 19:07:43 -070042#define QUP_HW_VERSION 0x030
43#define QUP_MX_OUTPUT_CNT 0x100
44#define QUP_OUT_FIFO_BASE 0x110
45#define QUP_MX_WRITE_CNT 0x150
46#define QUP_MX_INPUT_CNT 0x200
47#define QUP_MX_READ_CNT 0x208
48#define QUP_IN_FIFO_BASE 0x218
49#define QUP_I2C_CLK_CTL 0x400
50#define QUP_I2C_STATUS 0x404
Sricharan R191424b2016-01-19 15:32:42 +053051#define QUP_I2C_MASTER_GEN 0x408
Bjorn Andersson10c5a842014-03-13 19:07:43 -070052
53/* QUP States and reset values */
54#define QUP_RESET_STATE 0
55#define QUP_RUN_STATE 1
56#define QUP_PAUSE_STATE 3
57#define QUP_STATE_MASK 3
58
59#define QUP_STATE_VALID BIT(2)
60#define QUP_I2C_MAST_GEN BIT(4)
Sricharan R9cedf3b2016-02-22 17:38:15 +053061#define QUP_I2C_FLUSH BIT(6)
Bjorn Andersson10c5a842014-03-13 19:07:43 -070062
63#define QUP_OPERATIONAL_RESET 0x000ff0
64#define QUP_I2C_STATUS_RESET 0xfffffc
65
66/* QUP OPERATIONAL FLAGS */
67#define QUP_I2C_NACK_FLAG BIT(3)
68#define QUP_OUT_NOT_EMPTY BIT(4)
69#define QUP_IN_NOT_EMPTY BIT(5)
70#define QUP_OUT_FULL BIT(6)
71#define QUP_OUT_SVC_FLAG BIT(8)
72#define QUP_IN_SVC_FLAG BIT(9)
73#define QUP_MX_OUTPUT_DONE BIT(10)
74#define QUP_MX_INPUT_DONE BIT(11)
75
76/* I2C mini core related values */
77#define QUP_CLOCK_AUTO_GATE BIT(13)
78#define I2C_MINI_CORE (2 << 8)
79#define I2C_N_VAL 15
Sricharan R191424b2016-01-19 15:32:42 +053080#define I2C_N_VAL_V2 7
81
Bjorn Andersson10c5a842014-03-13 19:07:43 -070082/* Most significant word offset in FIFO port */
83#define QUP_MSW_SHIFT (I2C_N_VAL + 1)
84
85/* Packing/Unpacking words in FIFOs, and IO modes */
86#define QUP_OUTPUT_BLK_MODE (1 << 10)
Sricharan R9cedf3b2016-02-22 17:38:15 +053087#define QUP_OUTPUT_BAM_MODE (3 << 10)
Bjorn Andersson10c5a842014-03-13 19:07:43 -070088#define QUP_INPUT_BLK_MODE (1 << 12)
Sricharan R9cedf3b2016-02-22 17:38:15 +053089#define QUP_INPUT_BAM_MODE (3 << 12)
90#define QUP_BAM_MODE (QUP_OUTPUT_BAM_MODE | QUP_INPUT_BAM_MODE)
Bjorn Andersson10c5a842014-03-13 19:07:43 -070091#define QUP_UNPACK_EN BIT(14)
92#define QUP_PACK_EN BIT(15)
93
94#define QUP_REPACK_EN (QUP_UNPACK_EN | QUP_PACK_EN)
Sricharan R191424b2016-01-19 15:32:42 +053095#define QUP_V2_TAGS_EN 1
Bjorn Andersson10c5a842014-03-13 19:07:43 -070096
97#define QUP_OUTPUT_BLOCK_SIZE(x)(((x) >> 0) & 0x03)
98#define QUP_OUTPUT_FIFO_SIZE(x) (((x) >> 2) & 0x07)
99#define QUP_INPUT_BLOCK_SIZE(x) (((x) >> 5) & 0x03)
100#define QUP_INPUT_FIFO_SIZE(x) (((x) >> 7) & 0x07)
101
102/* QUP tags */
103#define QUP_TAG_START (1 << 8)
104#define QUP_TAG_DATA (2 << 8)
105#define QUP_TAG_STOP (3 << 8)
106#define QUP_TAG_REC (4 << 8)
Sricharan R9cedf3b2016-02-22 17:38:15 +0530107#define QUP_BAM_INPUT_EOT 0x93
108#define QUP_BAM_FLUSH_STOP 0x96
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700109
Sricharan R191424b2016-01-19 15:32:42 +0530110/* QUP v2 tags */
111#define QUP_TAG_V2_START 0x81
112#define QUP_TAG_V2_DATAWR 0x82
113#define QUP_TAG_V2_DATAWR_STOP 0x83
114#define QUP_TAG_V2_DATARD 0x85
115#define QUP_TAG_V2_DATARD_STOP 0x87
116
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700117/* Status, Error flags */
118#define I2C_STATUS_WR_BUFFER_FULL BIT(0)
119#define I2C_STATUS_BUS_ACTIVE BIT(8)
120#define I2C_STATUS_ERROR_MASK 0x38000fc
121#define QUP_STATUS_ERROR_FLAGS 0x7c
122
123#define QUP_READ_LIMIT 256
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530124#define SET_BIT 0x1
125#define RESET_BIT 0x0
126#define ONE_BYTE 0x1
Sricharan Rf7418792016-01-19 15:32:43 +0530127#define QUP_I2C_MX_CONFIG_DURING_RUN BIT(31)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700128
Sricharan R9cedf3b2016-02-22 17:38:15 +0530129#define MX_TX_RX_LEN SZ_64K
130#define MX_BLOCKS (MX_TX_RX_LEN / QUP_READ_LIMIT)
131
132/* Max timeout in ms for 32k bytes */
133#define TOUT_MAX 300
134
Sricharan R191424b2016-01-19 15:32:42 +0530135struct qup_i2c_block {
136 int count;
137 int pos;
138 int tx_tag_len;
139 int rx_tag_len;
140 int data_len;
141 u8 tags[6];
142};
143
Sricharan R9cedf3b2016-02-22 17:38:15 +0530144struct qup_i2c_tag {
145 u8 *start;
146 dma_addr_t addr;
147};
148
149struct qup_i2c_bam {
150 struct qup_i2c_tag tag;
151 struct dma_chan *dma;
152 struct scatterlist *sg;
153};
154
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700155struct qup_i2c_dev {
156 struct device *dev;
157 void __iomem *base;
158 int irq;
159 struct clk *clk;
160 struct clk *pclk;
161 struct i2c_adapter adap;
162
163 int clk_ctl;
164 int out_fifo_sz;
165 int in_fifo_sz;
166 int out_blk_sz;
167 int in_blk_sz;
168
169 unsigned long one_byte_t;
Sricharan R191424b2016-01-19 15:32:42 +0530170 struct qup_i2c_block blk;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700171
172 struct i2c_msg *msg;
173 /* Current posion in user message buffer */
174 int pos;
175 /* I2C protocol errors */
176 u32 bus_err;
177 /* QUP core errors */
178 u32 qup_err;
179
Sricharan Rf7418792016-01-19 15:32:43 +0530180 /* To check if this is the last msg */
181 bool is_last;
182
183 /* To configure when bus is in run state */
184 int config_run;
185
Sricharan R9cedf3b2016-02-22 17:38:15 +0530186 /* dma parameters */
187 bool is_dma;
188 struct dma_pool *dpool;
189 struct qup_i2c_tag start_tag;
190 struct qup_i2c_bam brx;
191 struct qup_i2c_bam btx;
192
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700193 struct completion xfer;
194};
195
196static irqreturn_t qup_i2c_interrupt(int irq, void *dev)
197{
198 struct qup_i2c_dev *qup = dev;
199 u32 bus_err;
200 u32 qup_err;
201 u32 opflags;
202
203 bus_err = readl(qup->base + QUP_I2C_STATUS);
204 qup_err = readl(qup->base + QUP_ERROR_FLAGS);
205 opflags = readl(qup->base + QUP_OPERATIONAL);
206
207 if (!qup->msg) {
208 /* Clear Error interrupt */
209 writel(QUP_RESET_STATE, qup->base + QUP_STATE);
210 return IRQ_HANDLED;
211 }
212
213 bus_err &= I2C_STATUS_ERROR_MASK;
214 qup_err &= QUP_STATUS_ERROR_FLAGS;
215
Abhishek Sahu2b84a4d2016-05-09 18:14:30 +0530216 /* Clear the error bits in QUP_ERROR_FLAGS */
217 if (qup_err)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700218 writel(qup_err, qup->base + QUP_ERROR_FLAGS);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700219
Abhishek Sahu2b84a4d2016-05-09 18:14:30 +0530220 /* Clear the error bits in QUP_I2C_STATUS */
221 if (bus_err)
222 writel(bus_err, qup->base + QUP_I2C_STATUS);
223
224 /* Reset the QUP State in case of error */
225 if (qup_err || bus_err) {
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700226 writel(QUP_RESET_STATE, qup->base + QUP_STATE);
227 goto done;
228 }
229
230 if (opflags & QUP_IN_SVC_FLAG)
231 writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL);
232
233 if (opflags & QUP_OUT_SVC_FLAG)
234 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
235
236done:
237 qup->qup_err = qup_err;
238 qup->bus_err = bus_err;
239 complete(&qup->xfer);
240 return IRQ_HANDLED;
241}
242
243static int qup_i2c_poll_state_mask(struct qup_i2c_dev *qup,
244 u32 req_state, u32 req_mask)
245{
246 int retries = 1;
247 u32 state;
248
249 /*
250 * State transition takes 3 AHB clocks cycles + 3 I2C master clock
251 * cycles. So retry once after a 1uS delay.
252 */
253 do {
254 state = readl(qup->base + QUP_STATE);
255
256 if (state & QUP_STATE_VALID &&
257 (state & req_mask) == req_state)
258 return 0;
259
260 udelay(1);
261 } while (retries--);
262
263 return -ETIMEDOUT;
264}
265
266static int qup_i2c_poll_state(struct qup_i2c_dev *qup, u32 req_state)
267{
268 return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK);
269}
270
Sricharan R9cedf3b2016-02-22 17:38:15 +0530271static void qup_i2c_flush(struct qup_i2c_dev *qup)
272{
273 u32 val = readl(qup->base + QUP_STATE);
274
275 val |= QUP_I2C_FLUSH;
276 writel(val, qup->base + QUP_STATE);
277}
278
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700279static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup)
280{
281 return qup_i2c_poll_state_mask(qup, 0, 0);
282}
283
284static int qup_i2c_poll_state_i2c_master(struct qup_i2c_dev *qup)
285{
286 return qup_i2c_poll_state_mask(qup, QUP_I2C_MAST_GEN, QUP_I2C_MAST_GEN);
287}
288
289static int qup_i2c_change_state(struct qup_i2c_dev *qup, u32 state)
290{
291 if (qup_i2c_poll_state_valid(qup) != 0)
292 return -EIO;
293
294 writel(state, qup->base + QUP_STATE);
295
296 if (qup_i2c_poll_state(qup, state) != 0)
297 return -EIO;
298 return 0;
299}
300
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530301/**
302 * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path
303 * @qup: The qup_i2c_dev device
304 * @op: The bit/event to wait on
305 * @val: value of the bit to wait on, 0 or 1
306 * @len: The length the bytes to be transferred
307 */
308static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val,
309 int len)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700310{
311 unsigned long timeout;
312 u32 opflags;
313 u32 status;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530314 u32 shift = __ffs(op);
Sricharan Rfbf99212016-06-10 23:38:21 +0530315 int ret = 0;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700316
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530317 len *= qup->one_byte_t;
318 /* timeout after a wait of twice the max time */
319 timeout = jiffies + len * 4;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700320
321 for (;;) {
322 opflags = readl(qup->base + QUP_OPERATIONAL);
323 status = readl(qup->base + QUP_I2C_STATUS);
324
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530325 if (((opflags & op) >> shift) == val) {
Sricharan Rf7418792016-01-19 15:32:43 +0530326 if ((op == QUP_OUT_NOT_EMPTY) && qup->is_last) {
Sricharan Rfbf99212016-06-10 23:38:21 +0530327 if (!(status & I2C_STATUS_BUS_ACTIVE)) {
328 ret = 0;
329 goto done;
330 }
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530331 } else {
Sricharan Rfbf99212016-06-10 23:38:21 +0530332 ret = 0;
333 goto done;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530334 }
335 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700336
Sricharan Rfbf99212016-06-10 23:38:21 +0530337 if (time_after(jiffies, timeout)) {
338 ret = -ETIMEDOUT;
339 goto done;
340 }
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530341 usleep_range(len, len * 2);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700342 }
Sricharan Rfbf99212016-06-10 23:38:21 +0530343
344done:
345 if (qup->bus_err || qup->qup_err)
346 ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
347
348 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700349}
350
Sricharan R191424b2016-01-19 15:32:42 +0530351static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup,
352 struct i2c_msg *msg)
353{
354 /* Number of entries to shift out, including the tags */
355 int total = msg->len + qup->blk.tx_tag_len;
356
Sricharan Rf7418792016-01-19 15:32:43 +0530357 total |= qup->config_run;
358
Sricharan R191424b2016-01-19 15:32:42 +0530359 if (total < qup->out_fifo_sz) {
360 /* FIFO mode */
361 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
362 writel(total, qup->base + QUP_MX_WRITE_CNT);
363 } else {
364 /* BLOCK mode (transfer data on chunks) */
365 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
366 qup->base + QUP_IO_MODE);
367 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
368 }
369}
370
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700371static void qup_i2c_set_write_mode(struct qup_i2c_dev *qup, struct i2c_msg *msg)
372{
373 /* Number of entries to shift out, including the start */
374 int total = msg->len + 1;
375
376 if (total < qup->out_fifo_sz) {
377 /* FIFO mode */
378 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
379 writel(total, qup->base + QUP_MX_WRITE_CNT);
380 } else {
381 /* BLOCK mode (transfer data on chunks) */
382 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
383 qup->base + QUP_IO_MODE);
384 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
385 }
386}
387
Sricharan R52db2232016-02-26 21:28:54 +0530388static int check_for_fifo_space(struct qup_i2c_dev *qup)
389{
390 int ret;
391
392 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
393 if (ret)
394 goto out;
395
396 ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL,
397 RESET_BIT, 4 * ONE_BYTE);
398 if (ret) {
399 /* Fifo is full. Drain out the fifo */
400 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
401 if (ret)
402 goto out;
403
404 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY,
405 RESET_BIT, 256 * ONE_BYTE);
406 if (ret) {
407 dev_err(qup->dev, "timeout for fifo out full");
408 goto out;
409 }
410
411 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
412 if (ret)
413 goto out;
414 }
415
416out:
417 return ret;
418}
419
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530420static int qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700421{
422 u32 addr = msg->addr << 1;
423 u32 qup_tag;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700424 int idx;
425 u32 val;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530426 int ret = 0;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700427
428 if (qup->pos == 0) {
429 val = QUP_TAG_START | addr;
430 idx = 1;
431 } else {
432 val = 0;
433 idx = 0;
434 }
435
436 while (qup->pos < msg->len) {
437 /* Check that there's space in the FIFO for our pair */
Sricharan R52db2232016-02-26 21:28:54 +0530438 ret = check_for_fifo_space(qup);
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530439 if (ret)
440 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700441
442 if (qup->pos == msg->len - 1)
443 qup_tag = QUP_TAG_STOP;
444 else
445 qup_tag = QUP_TAG_DATA;
446
447 if (idx & 1)
448 val |= (qup_tag | msg->buf[qup->pos]) << QUP_MSW_SHIFT;
449 else
450 val = qup_tag | msg->buf[qup->pos];
451
452 /* Write out the pair and the last odd value */
453 if (idx & 1 || qup->pos == msg->len - 1)
454 writel(val, qup->base + QUP_OUT_FIFO_BASE);
455
456 qup->pos++;
457 idx++;
458 }
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530459
Sricharan R52db2232016-02-26 21:28:54 +0530460 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
461
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530462 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700463}
464
Sricharan R191424b2016-01-19 15:32:42 +0530465static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
466 struct i2c_msg *msg)
467{
468 memset(&qup->blk, 0, sizeof(qup->blk));
469
470 qup->blk.data_len = msg->len;
471 qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT;
472
473 /* 4 bytes for first block and 2 writes for rest */
474 qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2;
475
476 /* There are 2 tag bytes that are read in to fifo for every block */
477 if (msg->flags & I2C_M_RD)
478 qup->blk.rx_tag_len = qup->blk.count * 2;
479}
480
481static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf,
482 int dlen, u8 *dbuf)
483{
484 u32 val = 0, idx = 0, pos = 0, i = 0, t;
485 int len = tlen + dlen;
486 u8 *buf = tbuf;
487 int ret = 0;
488
489 while (len > 0) {
Sricharan R52db2232016-02-26 21:28:54 +0530490 ret = check_for_fifo_space(qup);
491 if (ret)
Sricharan R191424b2016-01-19 15:32:42 +0530492 return ret;
Sricharan R191424b2016-01-19 15:32:42 +0530493
494 t = (len >= 4) ? 4 : len;
495
496 while (idx < t) {
497 if (!i && (pos >= tlen)) {
498 buf = dbuf;
499 pos = 0;
500 i = 1;
501 }
502 val |= buf[pos++] << (idx++ * 8);
503 }
504
505 writel(val, qup->base + QUP_OUT_FIFO_BASE);
506 idx = 0;
507 val = 0;
508 len -= 4;
509 }
510
Sricharan R52db2232016-02-26 21:28:54 +0530511 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
512
Sricharan R191424b2016-01-19 15:32:42 +0530513 return ret;
514}
515
516static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
517{
518 int data_len;
519
520 if (qup->blk.data_len > QUP_READ_LIMIT)
521 data_len = QUP_READ_LIMIT;
522 else
523 data_len = qup->blk.data_len;
524
525 return data_len;
526}
527
528static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup,
Sricharan R9cedf3b2016-02-22 17:38:15 +0530529 struct i2c_msg *msg, int is_dma)
Sricharan R191424b2016-01-19 15:32:42 +0530530{
Wolfram Sange3c60f32016-04-03 20:44:58 +0200531 u16 addr = i2c_8bit_addr_from_msg(msg);
Sricharan R191424b2016-01-19 15:32:42 +0530532 int len = 0;
533 int data_len;
534
Sricharan R9cedf3b2016-02-22 17:38:15 +0530535 int last = (qup->blk.pos == (qup->blk.count - 1)) && (qup->is_last);
536
Sricharan R191424b2016-01-19 15:32:42 +0530537 if (qup->blk.pos == 0) {
538 tags[len++] = QUP_TAG_V2_START;
539 tags[len++] = addr & 0xff;
540
541 if (msg->flags & I2C_M_TEN)
542 tags[len++] = addr >> 8;
543 }
544
545 /* Send _STOP commands for the last block */
Sricharan R9cedf3b2016-02-22 17:38:15 +0530546 if (last) {
Sricharan R191424b2016-01-19 15:32:42 +0530547 if (msg->flags & I2C_M_RD)
548 tags[len++] = QUP_TAG_V2_DATARD_STOP;
549 else
550 tags[len++] = QUP_TAG_V2_DATAWR_STOP;
551 } else {
552 if (msg->flags & I2C_M_RD)
553 tags[len++] = QUP_TAG_V2_DATARD;
554 else
555 tags[len++] = QUP_TAG_V2_DATAWR;
556 }
557
558 data_len = qup_i2c_get_data_len(qup);
559
560 /* 0 implies 256 bytes */
561 if (data_len == QUP_READ_LIMIT)
562 tags[len++] = 0;
563 else
564 tags[len++] = data_len;
565
Sricharan R9cedf3b2016-02-22 17:38:15 +0530566 if ((msg->flags & I2C_M_RD) && last && is_dma) {
567 tags[len++] = QUP_BAM_INPUT_EOT;
568 tags[len++] = QUP_BAM_FLUSH_STOP;
569 }
570
Sricharan R191424b2016-01-19 15:32:42 +0530571 return len;
572}
573
574static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
575{
576 int data_len = 0, tag_len, index;
577 int ret;
578
Sricharan R9cedf3b2016-02-22 17:38:15 +0530579 tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg, 0);
Sricharan R191424b2016-01-19 15:32:42 +0530580 index = msg->len - qup->blk.data_len;
581
582 /* only tags are written for read */
583 if (!(msg->flags & I2C_M_RD))
584 data_len = qup_i2c_get_data_len(qup);
585
586 ret = qup_i2c_send_data(qup, tag_len, qup->blk.tags,
587 data_len, &msg->buf[index]);
588 qup->blk.data_len -= data_len;
589
590 return ret;
591}
592
Sricharan R9cedf3b2016-02-22 17:38:15 +0530593static void qup_i2c_bam_cb(void *data)
594{
595 struct qup_i2c_dev *qup = data;
596
597 complete(&qup->xfer);
598}
599
600static int qup_sg_set_buf(struct scatterlist *sg, void *buf,
Sricharan R685983f2016-06-10 23:38:19 +0530601 unsigned int buflen, struct qup_i2c_dev *qup,
602 int dir)
Sricharan R9cedf3b2016-02-22 17:38:15 +0530603{
604 int ret;
605
606 sg_set_buf(sg, buf, buflen);
607 ret = dma_map_sg(qup->dev, sg, 1, dir);
608 if (!ret)
609 return -EINVAL;
610
Sricharan R9cedf3b2016-02-22 17:38:15 +0530611 return 0;
612}
613
614static void qup_i2c_rel_dma(struct qup_i2c_dev *qup)
615{
616 if (qup->btx.dma)
617 dma_release_channel(qup->btx.dma);
618 if (qup->brx.dma)
619 dma_release_channel(qup->brx.dma);
620 qup->btx.dma = NULL;
621 qup->brx.dma = NULL;
622}
623
624static int qup_i2c_req_dma(struct qup_i2c_dev *qup)
625{
626 int err;
627
628 if (!qup->btx.dma) {
629 qup->btx.dma = dma_request_slave_channel_reason(qup->dev, "tx");
630 if (IS_ERR(qup->btx.dma)) {
631 err = PTR_ERR(qup->btx.dma);
632 qup->btx.dma = NULL;
633 dev_err(qup->dev, "\n tx channel not available");
634 return err;
635 }
636 }
637
638 if (!qup->brx.dma) {
639 qup->brx.dma = dma_request_slave_channel_reason(qup->dev, "rx");
640 if (IS_ERR(qup->brx.dma)) {
641 dev_err(qup->dev, "\n rx channel not available");
642 err = PTR_ERR(qup->brx.dma);
643 qup->brx.dma = NULL;
644 qup_i2c_rel_dma(qup);
645 return err;
646 }
647 }
648 return 0;
649}
650
651static int qup_i2c_bam_do_xfer(struct qup_i2c_dev *qup, struct i2c_msg *msg,
652 int num)
653{
654 struct dma_async_tx_descriptor *txd, *rxd = NULL;
655 int ret = 0, idx = 0, limit = QUP_READ_LIMIT;
656 dma_cookie_t cookie_rx, cookie_tx;
657 u32 rx_nents = 0, tx_nents = 0, len, blocks, rem;
658 u32 i, tlen, tx_len, tx_buf = 0, rx_buf = 0, off = 0;
659 u8 *tags;
660
661 while (idx < num) {
662 blocks = (msg->len + limit) / limit;
663 rem = msg->len % limit;
664 tx_len = 0, len = 0, i = 0;
665
666 qup->is_last = (idx == (num - 1));
667
668 qup_i2c_set_blk_data(qup, msg);
669
670 if (msg->flags & I2C_M_RD) {
671 rx_nents += (blocks * 2) + 1;
672 tx_nents += 1;
673
674 while (qup->blk.pos < blocks) {
675 /* length set to '0' implies 256 bytes */
676 tlen = (i == (blocks - 1)) ? rem : 0;
677 tags = &qup->start_tag.start[off + len];
678 len += qup_i2c_set_tags(tags, qup, msg, 1);
679
680 /* scratch buf to read the start and len tags */
681 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
682 &qup->brx.tag.start[0],
Sricharan R685983f2016-06-10 23:38:19 +0530683 2, qup, DMA_FROM_DEVICE);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530684
685 if (ret)
686 return ret;
687
688 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
689 &msg->buf[limit * i],
Sricharan R685983f2016-06-10 23:38:19 +0530690 tlen, qup,
691 DMA_FROM_DEVICE);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530692 if (ret)
693 return ret;
694
695 i++;
696 qup->blk.pos = i;
697 }
698 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
699 &qup->start_tag.start[off],
Sricharan R685983f2016-06-10 23:38:19 +0530700 len, qup, DMA_TO_DEVICE);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530701 if (ret)
702 return ret;
703
704 off += len;
705 /* scratch buf to read the BAM EOT and FLUSH tags */
706 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
707 &qup->brx.tag.start[0],
Sricharan R685983f2016-06-10 23:38:19 +0530708 2, qup, DMA_FROM_DEVICE);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530709 if (ret)
710 return ret;
711 } else {
712 tx_nents += (blocks * 2);
713
714 while (qup->blk.pos < blocks) {
715 tlen = (i == (blocks - 1)) ? rem : 0;
716 tags = &qup->start_tag.start[off + tx_len];
717 len = qup_i2c_set_tags(tags, qup, msg, 1);
718
719 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
Sricharan R685983f2016-06-10 23:38:19 +0530720 tags, len,
721 qup, DMA_TO_DEVICE);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530722 if (ret)
723 return ret;
724
725 tx_len += len;
726 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
727 &msg->buf[limit * i],
Sricharan R685983f2016-06-10 23:38:19 +0530728 tlen, qup, DMA_TO_DEVICE);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530729 if (ret)
730 return ret;
731 i++;
732 qup->blk.pos = i;
733 }
734 off += tx_len;
735
736 if (idx == (num - 1)) {
737 len = 1;
738 if (rx_nents) {
739 qup->btx.tag.start[0] =
740 QUP_BAM_INPUT_EOT;
741 len++;
742 }
743 qup->btx.tag.start[len - 1] =
744 QUP_BAM_FLUSH_STOP;
745 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
746 &qup->btx.tag.start[0],
Sricharan R685983f2016-06-10 23:38:19 +0530747 len, qup, DMA_TO_DEVICE);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530748 if (ret)
749 return ret;
750 tx_nents += 1;
751 }
752 }
753 idx++;
754 msg++;
755 }
756
757 txd = dmaengine_prep_slave_sg(qup->btx.dma, qup->btx.sg, tx_nents,
758 DMA_MEM_TO_DEV,
759 DMA_PREP_INTERRUPT | DMA_PREP_FENCE);
760 if (!txd) {
761 dev_err(qup->dev, "failed to get tx desc\n");
762 ret = -EINVAL;
763 goto desc_err;
764 }
765
766 if (!rx_nents) {
767 txd->callback = qup_i2c_bam_cb;
768 txd->callback_param = qup;
769 }
770
771 cookie_tx = dmaengine_submit(txd);
772 if (dma_submit_error(cookie_tx)) {
773 ret = -EINVAL;
774 goto desc_err;
775 }
776
777 dma_async_issue_pending(qup->btx.dma);
778
779 if (rx_nents) {
780 rxd = dmaengine_prep_slave_sg(qup->brx.dma, qup->brx.sg,
781 rx_nents, DMA_DEV_TO_MEM,
782 DMA_PREP_INTERRUPT);
783 if (!rxd) {
784 dev_err(qup->dev, "failed to get rx desc\n");
785 ret = -EINVAL;
786
787 /* abort TX descriptors */
788 dmaengine_terminate_all(qup->btx.dma);
789 goto desc_err;
790 }
791
792 rxd->callback = qup_i2c_bam_cb;
793 rxd->callback_param = qup;
794 cookie_rx = dmaengine_submit(rxd);
795 if (dma_submit_error(cookie_rx)) {
796 ret = -EINVAL;
797 goto desc_err;
798 }
799
800 dma_async_issue_pending(qup->brx.dma);
801 }
802
803 if (!wait_for_completion_timeout(&qup->xfer, TOUT_MAX * HZ)) {
804 dev_err(qup->dev, "normal trans timed out\n");
805 ret = -ETIMEDOUT;
806 }
807
808 if (ret || qup->bus_err || qup->qup_err) {
Sricharan Rfbf99212016-06-10 23:38:21 +0530809 if (qup_i2c_change_state(qup, QUP_RUN_STATE)) {
810 dev_err(qup->dev, "change to run state timed out");
811 goto desc_err;
812 }
Sricharan R9cedf3b2016-02-22 17:38:15 +0530813
Sricharan Rfbf99212016-06-10 23:38:21 +0530814 if (rx_nents)
815 writel(QUP_BAM_INPUT_EOT,
Sricharan R9cedf3b2016-02-22 17:38:15 +0530816 qup->base + QUP_OUT_FIFO_BASE);
817
Sricharan Rfbf99212016-06-10 23:38:21 +0530818 writel(QUP_BAM_FLUSH_STOP, qup->base + QUP_OUT_FIFO_BASE);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530819
Sricharan Rfbf99212016-06-10 23:38:21 +0530820 qup_i2c_flush(qup);
Sricharan R9cedf3b2016-02-22 17:38:15 +0530821
Sricharan Rfbf99212016-06-10 23:38:21 +0530822 /* wait for remaining interrupts to occur */
823 if (!wait_for_completion_timeout(&qup->xfer, HZ))
824 dev_err(qup->dev, "flush timed out\n");
825
826 qup_i2c_rel_dma(qup);
827
828 ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
Sricharan R9cedf3b2016-02-22 17:38:15 +0530829 }
830
Sricharan Rfbf99212016-06-10 23:38:21 +0530831desc_err:
Sricharan R9cedf3b2016-02-22 17:38:15 +0530832 dma_unmap_sg(qup->dev, qup->btx.sg, tx_nents, DMA_TO_DEVICE);
833
834 if (rx_nents)
835 dma_unmap_sg(qup->dev, qup->brx.sg, rx_nents,
836 DMA_FROM_DEVICE);
Sricharan Rfbf99212016-06-10 23:38:21 +0530837
Sricharan R9cedf3b2016-02-22 17:38:15 +0530838 return ret;
839}
840
841static int qup_i2c_bam_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
842 int num)
843{
844 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
845 int ret = 0;
846
847 enable_irq(qup->irq);
848 ret = qup_i2c_req_dma(qup);
849
850 if (ret)
851 goto out;
852
Sricharan R9cedf3b2016-02-22 17:38:15 +0530853 writel(0, qup->base + QUP_MX_INPUT_CNT);
854 writel(0, qup->base + QUP_MX_OUTPUT_CNT);
855
856 /* set BAM mode */
857 writel(QUP_REPACK_EN | QUP_BAM_MODE, qup->base + QUP_IO_MODE);
858
859 /* mask fifo irqs */
860 writel((0x3 << 8), qup->base + QUP_OPERATIONAL_MASK);
861
862 /* set RUN STATE */
863 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
864 if (ret)
865 goto out;
866
867 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
868
869 qup->msg = msg;
870 ret = qup_i2c_bam_do_xfer(qup, qup->msg, num);
871out:
872 disable_irq(qup->irq);
873
874 qup->msg = NULL;
875 return ret;
876}
877
Sricharan R191424b2016-01-19 15:32:42 +0530878static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
879 struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700880{
881 unsigned long left;
Sricharan R191424b2016-01-19 15:32:42 +0530882 int ret = 0;
883
884 left = wait_for_completion_timeout(&qup->xfer, HZ);
885 if (!left) {
886 writel(1, qup->base + QUP_SW_RESET);
887 ret = -ETIMEDOUT;
888 }
889
Sricharan Rfbf99212016-06-10 23:38:21 +0530890 if (qup->bus_err || qup->qup_err)
891 ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
Sricharan R191424b2016-01-19 15:32:42 +0530892
893 return ret;
894}
895
896static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
897{
898 int ret = 0;
899
900 qup->msg = msg;
901 qup->pos = 0;
902 enable_irq(qup->irq);
903 qup_i2c_set_blk_data(qup, msg);
904 qup_i2c_set_write_mode_v2(qup, msg);
905
906 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
907 if (ret)
908 goto err;
909
910 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
911
912 do {
913 ret = qup_i2c_issue_xfer_v2(qup, msg);
914 if (ret)
915 goto err;
916
917 ret = qup_i2c_wait_for_complete(qup, msg);
918 if (ret)
919 goto err;
920
921 qup->blk.pos++;
922 } while (qup->blk.pos < qup->blk.count);
923
924 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
925
926err:
927 disable_irq(qup->irq);
928 qup->msg = NULL;
929
930 return ret;
931}
932
933static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
934{
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700935 int ret;
936
937 qup->msg = msg;
938 qup->pos = 0;
939
940 enable_irq(qup->irq);
941
942 qup_i2c_set_write_mode(qup, msg);
943
944 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
945 if (ret)
946 goto err;
947
948 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
949
950 do {
951 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
952 if (ret)
953 goto err;
954
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530955 ret = qup_i2c_issue_write(qup, msg);
956 if (ret)
957 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700958
959 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
960 if (ret)
961 goto err;
962
Sricharan R191424b2016-01-19 15:32:42 +0530963 ret = qup_i2c_wait_for_complete(qup, msg);
964 if (ret)
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700965 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700966 } while (qup->pos < msg->len);
967
968 /* Wait for the outstanding data in the fifo to drain */
Sricharan Rc4f0c5f2016-01-19 15:32:41 +0530969 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
Bjorn Andersson10c5a842014-03-13 19:07:43 -0700970err:
971 disable_irq(qup->irq);
972 qup->msg = NULL;
973
974 return ret;
975}
976
977static void qup_i2c_set_read_mode(struct qup_i2c_dev *qup, int len)
978{
979 if (len < qup->in_fifo_sz) {
980 /* FIFO mode */
981 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
982 writel(len, qup->base + QUP_MX_READ_CNT);
983 } else {
984 /* BLOCK mode (transfer data on chunks) */
985 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
986 qup->base + QUP_IO_MODE);
987 writel(len, qup->base + QUP_MX_INPUT_CNT);
988 }
989}
990
Sricharan R191424b2016-01-19 15:32:42 +0530991static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
992{
993 int tx_len = qup->blk.tx_tag_len;
994
995 len += qup->blk.rx_tag_len;
Sricharan Rf7418792016-01-19 15:32:43 +0530996 len |= qup->config_run;
997 tx_len |= qup->config_run;
Sricharan R191424b2016-01-19 15:32:42 +0530998
999 if (len < qup->in_fifo_sz) {
1000 /* FIFO mode */
1001 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
Sricharan R191424b2016-01-19 15:32:42 +05301002 writel(tx_len, qup->base + QUP_MX_WRITE_CNT);
Sricharan Rf7418792016-01-19 15:32:43 +05301003 writel(len, qup->base + QUP_MX_READ_CNT);
Sricharan R191424b2016-01-19 15:32:42 +05301004 } else {
1005 /* BLOCK mode (transfer data on chunks) */
1006 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
1007 qup->base + QUP_IO_MODE);
Sricharan R191424b2016-01-19 15:32:42 +05301008 writel(tx_len, qup->base + QUP_MX_OUTPUT_CNT);
Sricharan Rf7418792016-01-19 15:32:43 +05301009 writel(len, qup->base + QUP_MX_INPUT_CNT);
Sricharan R191424b2016-01-19 15:32:42 +05301010 }
1011}
1012
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001013static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1014{
1015 u32 addr, len, val;
1016
Naveen Kaje01309442016-05-05 12:33:17 -06001017 addr = i2c_8bit_addr_from_msg(msg);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001018
1019 /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
1020 len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
1021
1022 val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
1023 writel(val, qup->base + QUP_OUT_FIFO_BASE);
1024}
1025
1026
Sricharan Rc4f0c5f2016-01-19 15:32:41 +05301027static int qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg)
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001028{
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001029 u32 val = 0;
1030 int idx;
Sricharan Rc4f0c5f2016-01-19 15:32:41 +05301031 int ret = 0;
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001032
1033 for (idx = 0; qup->pos < msg->len; idx++) {
1034 if ((idx & 1) == 0) {
1035 /* Check that FIFO have data */
Sricharan Rc4f0c5f2016-01-19 15:32:41 +05301036 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
1037 SET_BIT, 4 * ONE_BYTE);
1038 if (ret)
1039 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001040
1041 /* Reading 2 words at time */
1042 val = readl(qup->base + QUP_IN_FIFO_BASE);
1043
1044 msg->buf[qup->pos++] = val & 0xFF;
1045 } else {
1046 msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
1047 }
1048 }
Sricharan Rc4f0c5f2016-01-19 15:32:41 +05301049
1050 return ret;
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001051}
1052
Sricharan R191424b2016-01-19 15:32:42 +05301053static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
1054 struct i2c_msg *msg)
1055{
1056 u32 val;
1057 int idx, pos = 0, ret = 0, total;
1058
1059 total = qup_i2c_get_data_len(qup);
1060
1061 /* 2 extra bytes for read tags */
1062 while (pos < (total + 2)) {
1063 /* Check that FIFO have data */
1064 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
1065 SET_BIT, 4 * ONE_BYTE);
1066 if (ret) {
1067 dev_err(qup->dev, "timeout for fifo not empty");
1068 return ret;
1069 }
1070 val = readl(qup->base + QUP_IN_FIFO_BASE);
1071
1072 for (idx = 0; idx < 4; idx++, val >>= 8, pos++) {
1073 /* first 2 bytes are tag bytes */
1074 if (pos < 2)
1075 continue;
1076
1077 if (pos >= (total + 2))
1078 goto out;
1079
1080 msg->buf[qup->pos++] = val & 0xff;
1081 }
1082 }
1083
1084out:
1085 qup->blk.data_len -= total;
1086
1087 return ret;
1088}
1089
1090static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1091{
1092 int ret = 0;
1093
1094 qup->msg = msg;
1095 qup->pos = 0;
1096 enable_irq(qup->irq);
1097 qup_i2c_set_blk_data(qup, msg);
1098 qup_i2c_set_read_mode_v2(qup, msg->len);
1099
1100 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1101 if (ret)
1102 goto err;
1103
1104 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
1105
1106 do {
1107 ret = qup_i2c_issue_xfer_v2(qup, msg);
1108 if (ret)
1109 goto err;
1110
1111 ret = qup_i2c_wait_for_complete(qup, msg);
1112 if (ret)
1113 goto err;
1114
1115 ret = qup_i2c_read_fifo_v2(qup, msg);
1116 if (ret)
1117 goto err;
1118
1119 qup->blk.pos++;
1120 } while (qup->blk.pos < qup->blk.count);
1121
1122err:
1123 disable_irq(qup->irq);
1124 qup->msg = NULL;
1125
1126 return ret;
1127}
1128
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001129static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1130{
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001131 int ret;
1132
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001133 qup->msg = msg;
1134 qup->pos = 0;
1135
1136 enable_irq(qup->irq);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001137 qup_i2c_set_read_mode(qup, msg->len);
1138
1139 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1140 if (ret)
1141 goto err;
1142
1143 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
1144
1145 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
1146 if (ret)
1147 goto err;
1148
1149 qup_i2c_issue_read(qup, msg);
1150
1151 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1152 if (ret)
1153 goto err;
1154
1155 do {
Sricharan R191424b2016-01-19 15:32:42 +05301156 ret = qup_i2c_wait_for_complete(qup, msg);
1157 if (ret)
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001158 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001159
Sricharan Rc4f0c5f2016-01-19 15:32:41 +05301160 ret = qup_i2c_read_fifo(qup, msg);
1161 if (ret)
1162 goto err;
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001163 } while (qup->pos < msg->len);
1164
1165err:
1166 disable_irq(qup->irq);
1167 qup->msg = NULL;
1168
1169 return ret;
1170}
1171
1172static int qup_i2c_xfer(struct i2c_adapter *adap,
1173 struct i2c_msg msgs[],
1174 int num)
1175{
1176 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
1177 int ret, idx;
1178
1179 ret = pm_runtime_get_sync(qup->dev);
Andy Grossfa01d092014-05-02 20:54:29 -05001180 if (ret < 0)
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001181 goto out;
1182
Sricharan Rfbf99212016-06-10 23:38:21 +05301183 qup->bus_err = 0;
1184 qup->qup_err = 0;
1185
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001186 writel(1, qup->base + QUP_SW_RESET);
1187 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
1188 if (ret)
1189 goto out;
1190
1191 /* Configure QUP as I2C mini core */
1192 writel(I2C_MINI_CORE | I2C_N_VAL, qup->base + QUP_CONFIG);
1193
1194 for (idx = 0; idx < num; idx++) {
1195 if (msgs[idx].len == 0) {
1196 ret = -EINVAL;
1197 goto out;
1198 }
1199
1200 if (qup_i2c_poll_state_i2c_master(qup)) {
1201 ret = -EIO;
1202 goto out;
1203 }
1204
1205 if (msgs[idx].flags & I2C_M_RD)
1206 ret = qup_i2c_read_one(qup, &msgs[idx]);
1207 else
1208 ret = qup_i2c_write_one(qup, &msgs[idx]);
1209
1210 if (ret)
1211 break;
1212
1213 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
1214 if (ret)
1215 break;
1216 }
1217
1218 if (ret == 0)
1219 ret = num;
1220out:
1221
1222 pm_runtime_mark_last_busy(qup->dev);
1223 pm_runtime_put_autosuspend(qup->dev);
1224
1225 return ret;
1226}
1227
Sricharan R191424b2016-01-19 15:32:42 +05301228static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
1229 struct i2c_msg msgs[],
1230 int num)
1231{
1232 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
Sricharan R9cedf3b2016-02-22 17:38:15 +05301233 int ret, len, idx = 0, use_dma = 0;
Sricharan R191424b2016-01-19 15:32:42 +05301234
Sricharan Rfbf99212016-06-10 23:38:21 +05301235 qup->bus_err = 0;
1236 qup->qup_err = 0;
1237
Sricharan R191424b2016-01-19 15:32:42 +05301238 ret = pm_runtime_get_sync(qup->dev);
1239 if (ret < 0)
1240 goto out;
1241
1242 writel(1, qup->base + QUP_SW_RESET);
1243 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
1244 if (ret)
1245 goto out;
1246
1247 /* Configure QUP as I2C mini core */
1248 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
1249 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
1250
Sricharan R9cedf3b2016-02-22 17:38:15 +05301251 if ((qup->is_dma)) {
1252 /* All i2c_msgs should be transferred using either dma or cpu */
1253 for (idx = 0; idx < num; idx++) {
1254 if (msgs[idx].len == 0) {
1255 ret = -EINVAL;
1256 goto out;
1257 }
1258
1259 len = (msgs[idx].len > qup->out_fifo_sz) ||
1260 (msgs[idx].len > qup->in_fifo_sz);
1261
1262 if ((!is_vmalloc_addr(msgs[idx].buf)) && len) {
1263 use_dma = 1;
1264 } else {
1265 use_dma = 0;
1266 break;
1267 }
1268 }
1269 }
1270
1271 do {
Sricharan R191424b2016-01-19 15:32:42 +05301272 if (msgs[idx].len == 0) {
1273 ret = -EINVAL;
1274 goto out;
1275 }
1276
1277 if (qup_i2c_poll_state_i2c_master(qup)) {
1278 ret = -EIO;
1279 goto out;
1280 }
1281
Sricharan Rf7418792016-01-19 15:32:43 +05301282 qup->is_last = (idx == (num - 1));
1283 if (idx)
1284 qup->config_run = QUP_I2C_MX_CONFIG_DURING_RUN;
1285 else
1286 qup->config_run = 0;
1287
Sricharan R191424b2016-01-19 15:32:42 +05301288 reinit_completion(&qup->xfer);
1289
Sricharan R9cedf3b2016-02-22 17:38:15 +05301290 if (use_dma) {
1291 ret = qup_i2c_bam_xfer(adap, &msgs[idx], num);
1292 } else {
1293 if (msgs[idx].flags & I2C_M_RD)
1294 ret = qup_i2c_read_one_v2(qup, &msgs[idx]);
1295 else
1296 ret = qup_i2c_write_one_v2(qup, &msgs[idx]);
1297 }
1298 } while ((idx++ < (num - 1)) && !use_dma && !ret);
Sricharan R191424b2016-01-19 15:32:42 +05301299
Sricharan Rf7418792016-01-19 15:32:43 +05301300 if (!ret)
1301 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
1302
Sricharan R191424b2016-01-19 15:32:42 +05301303 if (ret == 0)
1304 ret = num;
1305out:
1306 pm_runtime_mark_last_busy(qup->dev);
1307 pm_runtime_put_autosuspend(qup->dev);
1308
1309 return ret;
1310}
1311
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001312static u32 qup_i2c_func(struct i2c_adapter *adap)
1313{
1314 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
1315}
1316
1317static const struct i2c_algorithm qup_i2c_algo = {
1318 .master_xfer = qup_i2c_xfer,
1319 .functionality = qup_i2c_func,
1320};
1321
Sricharan R191424b2016-01-19 15:32:42 +05301322static const struct i2c_algorithm qup_i2c_algo_v2 = {
1323 .master_xfer = qup_i2c_xfer_v2,
1324 .functionality = qup_i2c_func,
1325};
1326
Wolfram Sang994647d2015-01-07 12:24:10 +01001327/*
1328 * The QUP block will issue a NACK and STOP on the bus when reaching
1329 * the end of the read, the length of the read is specified as one byte
1330 * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
1331 */
1332static struct i2c_adapter_quirks qup_i2c_quirks = {
1333 .max_read_len = QUP_READ_LIMIT,
1334};
1335
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001336static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
1337{
1338 clk_prepare_enable(qup->clk);
1339 clk_prepare_enable(qup->pclk);
1340}
1341
1342static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
1343{
1344 u32 config;
1345
1346 qup_i2c_change_state(qup, QUP_RESET_STATE);
1347 clk_disable_unprepare(qup->clk);
1348 config = readl(qup->base + QUP_CONFIG);
1349 config |= QUP_CLOCK_AUTO_GATE;
1350 writel(config, qup->base + QUP_CONFIG);
1351 clk_disable_unprepare(qup->pclk);
1352}
1353
1354static int qup_i2c_probe(struct platform_device *pdev)
1355{
1356 static const int blk_sizes[] = {4, 16, 32};
1357 struct device_node *node = pdev->dev.of_node;
1358 struct qup_i2c_dev *qup;
1359 unsigned long one_bit_t;
1360 struct resource *res;
1361 u32 io_mode, hw_ver, size;
1362 int ret, fs_div, hs_div;
1363 int src_clk_freq;
Wolfram Sangcf23e332014-04-03 11:30:33 +02001364 u32 clk_freq = 100000;
Sricharan R9cedf3b2016-02-22 17:38:15 +05301365 int blocks;
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001366
1367 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
1368 if (!qup)
1369 return -ENOMEM;
1370
1371 qup->dev = &pdev->dev;
1372 init_completion(&qup->xfer);
1373 platform_set_drvdata(pdev, qup);
1374
1375 of_property_read_u32(node, "clock-frequency", &clk_freq);
1376
Sricharan R191424b2016-01-19 15:32:42 +05301377 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
1378 qup->adap.algo = &qup_i2c_algo;
1379 qup->adap.quirks = &qup_i2c_quirks;
1380 } else {
1381 qup->adap.algo = &qup_i2c_algo_v2;
Sricharan R9cedf3b2016-02-22 17:38:15 +05301382 ret = qup_i2c_req_dma(qup);
1383
1384 if (ret == -EPROBE_DEFER)
1385 goto fail_dma;
1386 else if (ret != 0)
1387 goto nodma;
1388
1389 blocks = (MX_BLOCKS << 1) + 1;
1390 qup->btx.sg = devm_kzalloc(&pdev->dev,
1391 sizeof(*qup->btx.sg) * blocks,
1392 GFP_KERNEL);
1393 if (!qup->btx.sg) {
1394 ret = -ENOMEM;
1395 goto fail_dma;
1396 }
1397 sg_init_table(qup->btx.sg, blocks);
1398
1399 qup->brx.sg = devm_kzalloc(&pdev->dev,
1400 sizeof(*qup->brx.sg) * blocks,
1401 GFP_KERNEL);
1402 if (!qup->brx.sg) {
1403 ret = -ENOMEM;
1404 goto fail_dma;
1405 }
1406 sg_init_table(qup->brx.sg, blocks);
1407
1408 /* 2 tag bytes for each block + 5 for start, stop tags */
1409 size = blocks * 2 + 5;
Sricharan R9cedf3b2016-02-22 17:38:15 +05301410
Sricharan R685983f2016-06-10 23:38:19 +05301411 qup->start_tag.start = devm_kzalloc(&pdev->dev,
1412 size, GFP_KERNEL);
Sricharan R9cedf3b2016-02-22 17:38:15 +05301413 if (!qup->start_tag.start) {
1414 ret = -ENOMEM;
1415 goto fail_dma;
1416 }
1417
Sricharan R685983f2016-06-10 23:38:19 +05301418 qup->brx.tag.start = devm_kzalloc(&pdev->dev, 2, GFP_KERNEL);
Sricharan R9cedf3b2016-02-22 17:38:15 +05301419 if (!qup->brx.tag.start) {
1420 ret = -ENOMEM;
1421 goto fail_dma;
1422 }
1423
Sricharan R685983f2016-06-10 23:38:19 +05301424 qup->btx.tag.start = devm_kzalloc(&pdev->dev, 2, GFP_KERNEL);
Sricharan R9cedf3b2016-02-22 17:38:15 +05301425 if (!qup->btx.tag.start) {
1426 ret = -ENOMEM;
1427 goto fail_dma;
1428 }
1429 qup->is_dma = true;
Sricharan R191424b2016-01-19 15:32:42 +05301430 }
1431
Sricharan R9cedf3b2016-02-22 17:38:15 +05301432nodma:
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001433 /* We support frequencies up to FAST Mode (400KHz) */
1434 if (!clk_freq || clk_freq > 400000) {
1435 dev_err(qup->dev, "clock frequency not supported %d\n",
1436 clk_freq);
1437 return -EINVAL;
1438 }
1439
1440 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1441 qup->base = devm_ioremap_resource(qup->dev, res);
1442 if (IS_ERR(qup->base))
1443 return PTR_ERR(qup->base);
1444
1445 qup->irq = platform_get_irq(pdev, 0);
1446 if (qup->irq < 0) {
1447 dev_err(qup->dev, "No IRQ defined\n");
1448 return qup->irq;
1449 }
1450
1451 qup->clk = devm_clk_get(qup->dev, "core");
1452 if (IS_ERR(qup->clk)) {
1453 dev_err(qup->dev, "Could not get core clock\n");
1454 return PTR_ERR(qup->clk);
1455 }
1456
1457 qup->pclk = devm_clk_get(qup->dev, "iface");
1458 if (IS_ERR(qup->pclk)) {
1459 dev_err(qup->dev, "Could not get iface clock\n");
1460 return PTR_ERR(qup->pclk);
1461 }
1462
1463 qup_i2c_enable_clocks(qup);
1464
1465 /*
1466 * Bootloaders might leave a pending interrupt on certain QUP's,
1467 * so we reset the core before registering for interrupts.
1468 */
1469 writel(1, qup->base + QUP_SW_RESET);
1470 ret = qup_i2c_poll_state_valid(qup);
1471 if (ret)
1472 goto fail;
1473
1474 ret = devm_request_irq(qup->dev, qup->irq, qup_i2c_interrupt,
1475 IRQF_TRIGGER_HIGH, "i2c_qup", qup);
1476 if (ret) {
1477 dev_err(qup->dev, "Request %d IRQ failed\n", qup->irq);
1478 goto fail;
1479 }
1480 disable_irq(qup->irq);
1481
1482 hw_ver = readl(qup->base + QUP_HW_VERSION);
1483 dev_dbg(qup->dev, "Revision %x\n", hw_ver);
1484
1485 io_mode = readl(qup->base + QUP_IO_MODE);
1486
1487 /*
1488 * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
1489 * associated with each byte written/received
1490 */
1491 size = QUP_OUTPUT_BLOCK_SIZE(io_mode);
Pramod Gurav3cf357d2014-08-06 18:03:25 +05301492 if (size >= ARRAY_SIZE(blk_sizes)) {
1493 ret = -EIO;
1494 goto fail;
1495 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001496 qup->out_blk_sz = blk_sizes[size] / 2;
1497
1498 size = QUP_INPUT_BLOCK_SIZE(io_mode);
Pramod Gurav3cf357d2014-08-06 18:03:25 +05301499 if (size >= ARRAY_SIZE(blk_sizes)) {
1500 ret = -EIO;
1501 goto fail;
1502 }
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001503 qup->in_blk_sz = blk_sizes[size] / 2;
1504
1505 size = QUP_OUTPUT_FIFO_SIZE(io_mode);
1506 qup->out_fifo_sz = qup->out_blk_sz * (2 << size);
1507
1508 size = QUP_INPUT_FIFO_SIZE(io_mode);
1509 qup->in_fifo_sz = qup->in_blk_sz * (2 << size);
1510
1511 src_clk_freq = clk_get_rate(qup->clk);
1512 fs_div = ((src_clk_freq / clk_freq) / 2) - 3;
1513 hs_div = 3;
1514 qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff);
1515
1516 /*
1517 * Time it takes for a byte to be clocked out on the bus.
1518 * Each byte takes 9 clock cycles (8 bits + 1 ack).
1519 */
1520 one_bit_t = (USEC_PER_SEC / clk_freq) + 1;
1521 qup->one_byte_t = one_bit_t * 9;
1522
1523 dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
1524 qup->in_blk_sz, qup->in_fifo_sz,
1525 qup->out_blk_sz, qup->out_fifo_sz);
1526
1527 i2c_set_adapdata(&qup->adap, qup);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001528 qup->adap.dev.parent = qup->dev;
1529 qup->adap.dev.of_node = pdev->dev.of_node;
Sricharan R9cedf3b2016-02-22 17:38:15 +05301530 qup->is_last = true;
Sricharan Rf7418792016-01-19 15:32:43 +05301531
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001532 strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
1533
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001534 pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC);
1535 pm_runtime_use_autosuspend(qup->dev);
1536 pm_runtime_set_active(qup->dev);
1537 pm_runtime_enable(qup->dev);
Andy Gross86b59bb2014-09-29 17:00:51 -05001538
1539 ret = i2c_add_adapter(&qup->adap);
1540 if (ret)
1541 goto fail_runtime;
1542
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001543 return 0;
1544
Andy Gross86b59bb2014-09-29 17:00:51 -05001545fail_runtime:
1546 pm_runtime_disable(qup->dev);
1547 pm_runtime_set_suspended(qup->dev);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001548fail:
1549 qup_i2c_disable_clocks(qup);
Sricharan R9cedf3b2016-02-22 17:38:15 +05301550fail_dma:
1551 if (qup->btx.dma)
1552 dma_release_channel(qup->btx.dma);
1553 if (qup->brx.dma)
1554 dma_release_channel(qup->brx.dma);
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001555 return ret;
1556}
1557
1558static int qup_i2c_remove(struct platform_device *pdev)
1559{
1560 struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
1561
Sricharan R9cedf3b2016-02-22 17:38:15 +05301562 if (qup->is_dma) {
Sricharan R9cedf3b2016-02-22 17:38:15 +05301563 dma_release_channel(qup->btx.dma);
1564 dma_release_channel(qup->brx.dma);
1565 }
1566
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001567 disable_irq(qup->irq);
1568 qup_i2c_disable_clocks(qup);
1569 i2c_del_adapter(&qup->adap);
1570 pm_runtime_disable(qup->dev);
1571 pm_runtime_set_suspended(qup->dev);
1572 return 0;
1573}
1574
1575#ifdef CONFIG_PM
1576static int qup_i2c_pm_suspend_runtime(struct device *device)
1577{
1578 struct qup_i2c_dev *qup = dev_get_drvdata(device);
1579
1580 dev_dbg(device, "pm_runtime: suspending...\n");
1581 qup_i2c_disable_clocks(qup);
1582 return 0;
1583}
1584
1585static int qup_i2c_pm_resume_runtime(struct device *device)
1586{
1587 struct qup_i2c_dev *qup = dev_get_drvdata(device);
1588
1589 dev_dbg(device, "pm_runtime: resuming...\n");
1590 qup_i2c_enable_clocks(qup);
1591 return 0;
1592}
1593#endif
1594
1595#ifdef CONFIG_PM_SLEEP
1596static int qup_i2c_suspend(struct device *device)
1597{
1598 qup_i2c_pm_suspend_runtime(device);
1599 return 0;
1600}
1601
1602static int qup_i2c_resume(struct device *device)
1603{
1604 qup_i2c_pm_resume_runtime(device);
1605 pm_runtime_mark_last_busy(device);
1606 pm_request_autosuspend(device);
1607 return 0;
1608}
1609#endif
1610
1611static const struct dev_pm_ops qup_i2c_qup_pm_ops = {
1612 SET_SYSTEM_SLEEP_PM_OPS(
1613 qup_i2c_suspend,
1614 qup_i2c_resume)
1615 SET_RUNTIME_PM_OPS(
1616 qup_i2c_pm_suspend_runtime,
1617 qup_i2c_pm_resume_runtime,
1618 NULL)
1619};
1620
1621static const struct of_device_id qup_i2c_dt_match[] = {
1622 { .compatible = "qcom,i2c-qup-v1.1.1" },
1623 { .compatible = "qcom,i2c-qup-v2.1.1" },
1624 { .compatible = "qcom,i2c-qup-v2.2.1" },
1625 {}
1626};
1627MODULE_DEVICE_TABLE(of, qup_i2c_dt_match);
1628
1629static struct platform_driver qup_i2c_driver = {
1630 .probe = qup_i2c_probe,
1631 .remove = qup_i2c_remove,
1632 .driver = {
1633 .name = "i2c_qup",
Bjorn Andersson10c5a842014-03-13 19:07:43 -07001634 .pm = &qup_i2c_qup_pm_ops,
1635 .of_match_table = qup_i2c_dt_match,
1636 },
1637};
1638
1639module_platform_driver(qup_i2c_driver);
1640
1641MODULE_LICENSE("GPL v2");
1642MODULE_ALIAS("platform:i2c_qup");