blob: 8f6903ec7aec187fac187b023b37faa248a6aba9 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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/acpi.h>
18#include <linux/atomic.h>
19#include <linux/clk.h>
20#include <linux/delay.h>
21#include <linux/dmaengine.h>
22#include <linux/dmapool.h>
23#include <linux/dma-mapping.h>
24#include <linux/err.h>
25#include <linux/i2c.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/module.h>
29#include <linux/of.h>
30#include <linux/platform_device.h>
31#include <linux/pm_runtime.h>
32#include <linux/scatterlist.h>
33
34/* QUP Registers */
35#define QUP_CONFIG 0x000
36#define QUP_STATE 0x004
37#define QUP_IO_MODE 0x008
38#define QUP_SW_RESET 0x00c
39#define QUP_OPERATIONAL 0x018
40#define QUP_ERROR_FLAGS 0x01c
41#define QUP_ERROR_FLAGS_EN 0x020
42#define QUP_OPERATIONAL_MASK 0x028
43#define QUP_HW_VERSION 0x030
44#define QUP_MX_OUTPUT_CNT 0x100
45#define QUP_OUT_FIFO_BASE 0x110
46#define QUP_MX_WRITE_CNT 0x150
47#define QUP_MX_INPUT_CNT 0x200
48#define QUP_MX_READ_CNT 0x208
49#define QUP_IN_FIFO_BASE 0x218
50#define QUP_I2C_CLK_CTL 0x400
51#define QUP_I2C_STATUS 0x404
52#define QUP_I2C_MASTER_GEN 0x408
53
54/* QUP States and reset values */
55#define QUP_RESET_STATE 0
56#define QUP_RUN_STATE 1
57#define QUP_PAUSE_STATE 3
58#define QUP_STATE_MASK 3
59
60#define QUP_STATE_VALID BIT(2)
61#define QUP_I2C_MAST_GEN BIT(4)
62#define QUP_I2C_FLUSH BIT(6)
63
64#define QUP_OPERATIONAL_RESET 0x000ff0
65#define QUP_I2C_STATUS_RESET 0xfffffc
66
67/* QUP OPERATIONAL FLAGS */
68#define QUP_I2C_NACK_FLAG BIT(3)
69#define QUP_OUT_NOT_EMPTY BIT(4)
70#define QUP_IN_NOT_EMPTY BIT(5)
71#define QUP_OUT_FULL BIT(6)
72#define QUP_OUT_SVC_FLAG BIT(8)
73#define QUP_IN_SVC_FLAG BIT(9)
74#define QUP_MX_OUTPUT_DONE BIT(10)
75#define QUP_MX_INPUT_DONE BIT(11)
76
77/* I2C mini core related values */
78#define QUP_CLOCK_AUTO_GATE BIT(13)
79#define I2C_MINI_CORE (2 << 8)
80#define I2C_N_VAL 15
81#define I2C_N_VAL_V2 7
82
83/* Most significant word offset in FIFO port */
84#define QUP_MSW_SHIFT (I2C_N_VAL + 1)
85
86/* Packing/Unpacking words in FIFOs, and IO modes */
87#define QUP_OUTPUT_BLK_MODE (1 << 10)
88#define QUP_OUTPUT_BAM_MODE (3 << 10)
89#define QUP_INPUT_BLK_MODE (1 << 12)
90#define QUP_INPUT_BAM_MODE (3 << 12)
91#define QUP_BAM_MODE (QUP_OUTPUT_BAM_MODE | QUP_INPUT_BAM_MODE)
92#define QUP_UNPACK_EN BIT(14)
93#define QUP_PACK_EN BIT(15)
94
95#define QUP_REPACK_EN (QUP_UNPACK_EN | QUP_PACK_EN)
96#define QUP_V2_TAGS_EN 1
97
98#define QUP_OUTPUT_BLOCK_SIZE(x)(((x) >> 0) & 0x03)
99#define QUP_OUTPUT_FIFO_SIZE(x) (((x) >> 2) & 0x07)
100#define QUP_INPUT_BLOCK_SIZE(x) (((x) >> 5) & 0x03)
101#define QUP_INPUT_FIFO_SIZE(x) (((x) >> 7) & 0x07)
102
103/* QUP tags */
104#define QUP_TAG_START (1 << 8)
105#define QUP_TAG_DATA (2 << 8)
106#define QUP_TAG_STOP (3 << 8)
107#define QUP_TAG_REC (4 << 8)
108#define QUP_BAM_INPUT_EOT 0x93
109#define QUP_BAM_FLUSH_STOP 0x96
110
111/* QUP v2 tags */
112#define QUP_TAG_V2_START 0x81
113#define QUP_TAG_V2_DATAWR 0x82
114#define QUP_TAG_V2_DATAWR_STOP 0x83
115#define QUP_TAG_V2_DATARD 0x85
116#define QUP_TAG_V2_DATARD_STOP 0x87
117
118/* Status, Error flags */
119#define I2C_STATUS_WR_BUFFER_FULL BIT(0)
120#define I2C_STATUS_BUS_ACTIVE BIT(8)
121#define I2C_STATUS_ERROR_MASK 0x38000fc
122#define QUP_STATUS_ERROR_FLAGS 0x7c
123
124#define QUP_READ_LIMIT 256
125#define SET_BIT 0x1
126#define RESET_BIT 0x0
127#define ONE_BYTE 0x1
128#define QUP_I2C_MX_CONFIG_DURING_RUN BIT(31)
129
130#define MX_TX_RX_LEN SZ_64K
131#define MX_BLOCKS (MX_TX_RX_LEN / QUP_READ_LIMIT)
132
133/* Max timeout in ms for 32k bytes */
134#define TOUT_MAX 300
135
136/* Default values. Use these if FW query fails */
137#define DEFAULT_CLK_FREQ 100000
138#define DEFAULT_SRC_CLK 20000000
139
140struct qup_i2c_block {
141 int count;
142 int pos;
143 int tx_tag_len;
144 int rx_tag_len;
145 int data_len;
146 u8 tags[6];
147};
148
149struct qup_i2c_tag {
150 u8 *start;
151 dma_addr_t addr;
152};
153
154struct qup_i2c_bam {
155 struct qup_i2c_tag tag;
156 struct dma_chan *dma;
157 struct scatterlist *sg;
158};
159
160struct qup_i2c_dev {
161 struct device *dev;
162 void __iomem *base;
163 int irq;
164 struct clk *clk;
165 struct clk *pclk;
166 struct i2c_adapter adap;
167
168 int clk_ctl;
169 int out_fifo_sz;
170 int in_fifo_sz;
171 int out_blk_sz;
172 int in_blk_sz;
173
174 unsigned long one_byte_t;
175 struct qup_i2c_block blk;
176
177 struct i2c_msg *msg;
178 /* Current posion in user message buffer */
179 int pos;
180 /* I2C protocol errors */
181 u32 bus_err;
182 /* QUP core errors */
183 u32 qup_err;
184
185 /* To check if this is the last msg */
186 bool is_last;
187
188 /* To configure when bus is in run state */
189 int config_run;
190
191 /* dma parameters */
192 bool is_dma;
193 struct dma_pool *dpool;
194 struct qup_i2c_tag start_tag;
195 struct qup_i2c_bam brx;
196 struct qup_i2c_bam btx;
197
198 struct completion xfer;
199};
200
201static irqreturn_t qup_i2c_interrupt(int irq, void *dev)
202{
203 struct qup_i2c_dev *qup = dev;
204 u32 bus_err;
205 u32 qup_err;
206 u32 opflags;
207
208 bus_err = readl(qup->base + QUP_I2C_STATUS);
209 qup_err = readl(qup->base + QUP_ERROR_FLAGS);
210 opflags = readl(qup->base + QUP_OPERATIONAL);
211
212 if (!qup->msg) {
213 /* Clear Error interrupt */
214 writel(QUP_RESET_STATE, qup->base + QUP_STATE);
215 return IRQ_HANDLED;
216 }
217
218 bus_err &= I2C_STATUS_ERROR_MASK;
219 qup_err &= QUP_STATUS_ERROR_FLAGS;
220
221 /* Clear the error bits in QUP_ERROR_FLAGS */
222 if (qup_err)
223 writel(qup_err, qup->base + QUP_ERROR_FLAGS);
224
225 /* Clear the error bits in QUP_I2C_STATUS */
226 if (bus_err)
227 writel(bus_err, qup->base + QUP_I2C_STATUS);
228
229 /* Reset the QUP State in case of error */
230 if (qup_err || bus_err) {
231 writel(QUP_RESET_STATE, qup->base + QUP_STATE);
232 goto done;
233 }
234
235 if (opflags & QUP_IN_SVC_FLAG)
236 writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL);
237
238 if (opflags & QUP_OUT_SVC_FLAG)
239 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
240
241done:
242 qup->qup_err = qup_err;
243 qup->bus_err = bus_err;
244 complete(&qup->xfer);
245 return IRQ_HANDLED;
246}
247
248static int qup_i2c_poll_state_mask(struct qup_i2c_dev *qup,
249 u32 req_state, u32 req_mask)
250{
251 int retries = 1;
252 u32 state;
253
254 /*
255 * State transition takes 3 AHB clocks cycles + 3 I2C master clock
256 * cycles. So retry once after a 1uS delay.
257 */
258 do {
259 state = readl(qup->base + QUP_STATE);
260
261 if (state & QUP_STATE_VALID &&
262 (state & req_mask) == req_state)
263 return 0;
264
265 udelay(1);
266 } while (retries--);
267
268 return -ETIMEDOUT;
269}
270
271static int qup_i2c_poll_state(struct qup_i2c_dev *qup, u32 req_state)
272{
273 return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK);
274}
275
276static void qup_i2c_flush(struct qup_i2c_dev *qup)
277{
278 u32 val = readl(qup->base + QUP_STATE);
279
280 val |= QUP_I2C_FLUSH;
281 writel(val, qup->base + QUP_STATE);
282}
283
284static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup)
285{
286 return qup_i2c_poll_state_mask(qup, 0, 0);
287}
288
289static int qup_i2c_poll_state_i2c_master(struct qup_i2c_dev *qup)
290{
291 return qup_i2c_poll_state_mask(qup, QUP_I2C_MAST_GEN, QUP_I2C_MAST_GEN);
292}
293
294static int qup_i2c_change_state(struct qup_i2c_dev *qup, u32 state)
295{
296 if (qup_i2c_poll_state_valid(qup) != 0)
297 return -EIO;
298
299 writel(state, qup->base + QUP_STATE);
300
301 if (qup_i2c_poll_state(qup, state) != 0)
302 return -EIO;
303 return 0;
304}
305
306/**
307 * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path
308 * @qup: The qup_i2c_dev device
309 * @op: The bit/event to wait on
310 * @val: value of the bit to wait on, 0 or 1
311 * @len: The length the bytes to be transferred
312 */
313static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val,
314 int len)
315{
316 unsigned long timeout;
317 u32 opflags;
318 u32 status;
319 u32 shift = __ffs(op);
320 int ret = 0;
321
322 len *= qup->one_byte_t;
323 /* timeout after a wait of twice the max time */
324 timeout = jiffies + len * 4;
325
326 for (;;) {
327 opflags = readl(qup->base + QUP_OPERATIONAL);
328 status = readl(qup->base + QUP_I2C_STATUS);
329
330 if (((opflags & op) >> shift) == val) {
331 if ((op == QUP_OUT_NOT_EMPTY) && qup->is_last) {
332 if (!(status & I2C_STATUS_BUS_ACTIVE)) {
333 ret = 0;
334 goto done;
335 }
336 } else {
337 ret = 0;
338 goto done;
339 }
340 }
341
342 if (time_after(jiffies, timeout)) {
343 ret = -ETIMEDOUT;
344 goto done;
345 }
346 usleep_range(len, len * 2);
347 }
348
349done:
350 if (qup->bus_err || qup->qup_err)
351 ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
352
353 return ret;
354}
355
356static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup,
357 struct i2c_msg *msg)
358{
359 /* Number of entries to shift out, including the tags */
360 int total = msg->len + qup->blk.tx_tag_len;
361
362 total |= qup->config_run;
363
364 if (total < qup->out_fifo_sz) {
365 /* FIFO mode */
366 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
367 writel(total, qup->base + QUP_MX_WRITE_CNT);
368 } else {
369 /* BLOCK mode (transfer data on chunks) */
370 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
371 qup->base + QUP_IO_MODE);
372 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
373 }
374}
375
376static void qup_i2c_set_write_mode(struct qup_i2c_dev *qup, struct i2c_msg *msg)
377{
378 /* Number of entries to shift out, including the start */
379 int total = msg->len + 1;
380
381 if (total < qup->out_fifo_sz) {
382 /* FIFO mode */
383 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
384 writel(total, qup->base + QUP_MX_WRITE_CNT);
385 } else {
386 /* BLOCK mode (transfer data on chunks) */
387 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
388 qup->base + QUP_IO_MODE);
389 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
390 }
391}
392
393static int check_for_fifo_space(struct qup_i2c_dev *qup)
394{
395 int ret;
396
397 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
398 if (ret)
399 goto out;
400
401 ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL,
402 RESET_BIT, 4 * ONE_BYTE);
403 if (ret) {
404 /* Fifo is full. Drain out the fifo */
405 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
406 if (ret)
407 goto out;
408
409 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY,
410 RESET_BIT, 256 * ONE_BYTE);
411 if (ret) {
412 dev_err(qup->dev, "timeout for fifo out full");
413 goto out;
414 }
415
416 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
417 if (ret)
418 goto out;
419 }
420
421out:
422 return ret;
423}
424
425static int qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
426{
427 u32 addr = msg->addr << 1;
428 u32 qup_tag;
429 int idx;
430 u32 val;
431 int ret = 0;
432
433 if (qup->pos == 0) {
434 val = QUP_TAG_START | addr;
435 idx = 1;
436 } else {
437 val = 0;
438 idx = 0;
439 }
440
441 while (qup->pos < msg->len) {
442 /* Check that there's space in the FIFO for our pair */
443 ret = check_for_fifo_space(qup);
444 if (ret)
445 return ret;
446
447 if (qup->pos == msg->len - 1)
448 qup_tag = QUP_TAG_STOP;
449 else
450 qup_tag = QUP_TAG_DATA;
451
452 if (idx & 1)
453 val |= (qup_tag | msg->buf[qup->pos]) << QUP_MSW_SHIFT;
454 else
455 val = qup_tag | msg->buf[qup->pos];
456
457 /* Write out the pair and the last odd value */
458 if (idx & 1 || qup->pos == msg->len - 1)
459 writel(val, qup->base + QUP_OUT_FIFO_BASE);
460
461 qup->pos++;
462 idx++;
463 }
464
465 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
466
467 return ret;
468}
469
470static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
471 struct i2c_msg *msg)
472{
473 memset(&qup->blk, 0, sizeof(qup->blk));
474
475 qup->blk.data_len = msg->len;
476 qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT;
477
478 /* 4 bytes for first block and 2 writes for rest */
479 qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2;
480
481 /* There are 2 tag bytes that are read in to fifo for every block */
482 if (msg->flags & I2C_M_RD)
483 qup->blk.rx_tag_len = qup->blk.count * 2;
484}
485
486static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf,
487 int dlen, u8 *dbuf)
488{
489 u32 val = 0, idx = 0, pos = 0, i = 0, t;
490 int len = tlen + dlen;
491 u8 *buf = tbuf;
492 int ret = 0;
493
494 while (len > 0) {
495 ret = check_for_fifo_space(qup);
496 if (ret)
497 return ret;
498
499 t = (len >= 4) ? 4 : len;
500
501 while (idx < t) {
502 if (!i && (pos >= tlen)) {
503 buf = dbuf;
504 pos = 0;
505 i = 1;
506 }
507 val |= buf[pos++] << (idx++ * 8);
508 }
509
510 writel(val, qup->base + QUP_OUT_FIFO_BASE);
511 idx = 0;
512 val = 0;
513 len -= 4;
514 }
515
516 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
517
518 return ret;
519}
520
521static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
522{
523 int data_len;
524
525 if (qup->blk.data_len > QUP_READ_LIMIT)
526 data_len = QUP_READ_LIMIT;
527 else
528 data_len = qup->blk.data_len;
529
530 return data_len;
531}
532
533static bool qup_i2c_check_msg_len(struct i2c_msg *msg)
534{
535 return ((msg->flags & I2C_M_RD) && (msg->flags & I2C_M_RECV_LEN));
536}
537
538static int qup_i2c_set_tags_smb(u16 addr, u8 *tags, struct qup_i2c_dev *qup,
539 struct i2c_msg *msg)
540{
541 int len = 0;
542
543 if (msg->len > 1) {
544 tags[len++] = QUP_TAG_V2_DATARD_STOP;
545 tags[len++] = qup_i2c_get_data_len(qup) - 1;
546 } else {
547 tags[len++] = QUP_TAG_V2_START;
548 tags[len++] = addr & 0xff;
549
550 if (msg->flags & I2C_M_TEN)
551 tags[len++] = addr >> 8;
552
553 tags[len++] = QUP_TAG_V2_DATARD;
554 /* Read 1 byte indicating the length of the SMBus message */
555 tags[len++] = 1;
556 }
557 return len;
558}
559
560static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup,
561 struct i2c_msg *msg, int is_dma)
562{
563 u16 addr = i2c_8bit_addr_from_msg(msg);
564 int len = 0;
565 int data_len;
566
567 int last = (qup->blk.pos == (qup->blk.count - 1)) && (qup->is_last);
568
569 /* Handle tags for SMBus block read */
570 if (qup_i2c_check_msg_len(msg))
571 return qup_i2c_set_tags_smb(addr, tags, qup, msg);
572
573 if (qup->blk.pos == 0) {
574 tags[len++] = QUP_TAG_V2_START;
575 tags[len++] = addr & 0xff;
576
577 if (msg->flags & I2C_M_TEN)
578 tags[len++] = addr >> 8;
579 }
580
581 /* Send _STOP commands for the last block */
582 if (last) {
583 if (msg->flags & I2C_M_RD)
584 tags[len++] = QUP_TAG_V2_DATARD_STOP;
585 else
586 tags[len++] = QUP_TAG_V2_DATAWR_STOP;
587 } else {
588 if (msg->flags & I2C_M_RD)
589 tags[len++] = QUP_TAG_V2_DATARD;
590 else
591 tags[len++] = QUP_TAG_V2_DATAWR;
592 }
593
594 data_len = qup_i2c_get_data_len(qup);
595
596 /* 0 implies 256 bytes */
597 if (data_len == QUP_READ_LIMIT)
598 tags[len++] = 0;
599 else
600 tags[len++] = data_len;
601
602 if ((msg->flags & I2C_M_RD) && last && is_dma) {
603 tags[len++] = QUP_BAM_INPUT_EOT;
604 tags[len++] = QUP_BAM_FLUSH_STOP;
605 }
606
607 return len;
608}
609
610static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
611{
612 int data_len = 0, tag_len, index;
613 int ret;
614
615 tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg, 0);
616 index = msg->len - qup->blk.data_len;
617
618 /* only tags are written for read */
619 if (!(msg->flags & I2C_M_RD))
620 data_len = qup_i2c_get_data_len(qup);
621
622 ret = qup_i2c_send_data(qup, tag_len, qup->blk.tags,
623 data_len, &msg->buf[index]);
624 qup->blk.data_len -= data_len;
625
626 return ret;
627}
628
629static void qup_i2c_bam_cb(void *data)
630{
631 struct qup_i2c_dev *qup = data;
632
633 complete(&qup->xfer);
634}
635
636static int qup_sg_set_buf(struct scatterlist *sg, void *buf,
637 unsigned int buflen, struct qup_i2c_dev *qup,
638 int dir)
639{
640 int ret;
641
642 sg_set_buf(sg, buf, buflen);
643 ret = dma_map_sg(qup->dev, sg, 1, dir);
644 if (!ret)
645 return -EINVAL;
646
647 return 0;
648}
649
650static void qup_i2c_rel_dma(struct qup_i2c_dev *qup)
651{
652 if (qup->btx.dma)
653 dma_release_channel(qup->btx.dma);
654 if (qup->brx.dma)
655 dma_release_channel(qup->brx.dma);
656 qup->btx.dma = NULL;
657 qup->brx.dma = NULL;
658}
659
660static int qup_i2c_req_dma(struct qup_i2c_dev *qup)
661{
662 int err;
663
664 if (!qup->btx.dma) {
665 qup->btx.dma = dma_request_slave_channel_reason(qup->dev, "tx");
666 if (IS_ERR(qup->btx.dma)) {
667 err = PTR_ERR(qup->btx.dma);
668 qup->btx.dma = NULL;
669 dev_err(qup->dev, "\n tx channel not available");
670 return err;
671 }
672 }
673
674 if (!qup->brx.dma) {
675 qup->brx.dma = dma_request_slave_channel_reason(qup->dev, "rx");
676 if (IS_ERR(qup->brx.dma)) {
677 dev_err(qup->dev, "\n rx channel not available");
678 err = PTR_ERR(qup->brx.dma);
679 qup->brx.dma = NULL;
680 qup_i2c_rel_dma(qup);
681 return err;
682 }
683 }
684 return 0;
685}
686
687static int qup_i2c_bam_do_xfer(struct qup_i2c_dev *qup, struct i2c_msg *msg,
688 int num)
689{
690 struct dma_async_tx_descriptor *txd, *rxd = NULL;
691 int ret = 0, idx = 0, limit = QUP_READ_LIMIT;
692 dma_cookie_t cookie_rx, cookie_tx;
693 u32 rx_nents = 0, tx_nents = 0, len, blocks, rem;
694 u32 i, tlen, tx_len, tx_buf = 0, rx_buf = 0, off = 0;
695 u8 *tags;
696
697 while (idx < num) {
698 tx_len = 0, len = 0, i = 0;
699
700 qup->is_last = (idx == (num - 1));
701
702 qup_i2c_set_blk_data(qup, msg);
703
704 blocks = qup->blk.count;
705 rem = msg->len - (blocks - 1) * limit;
706
707 if (msg->flags & I2C_M_RD) {
708 rx_nents += (blocks * 2) + 1;
709 tx_nents += 1;
710
711 while (qup->blk.pos < blocks) {
712 tlen = (i == (blocks - 1)) ? rem : limit;
713 tags = &qup->start_tag.start[off + len];
714 len += qup_i2c_set_tags(tags, qup, msg, 1);
715 qup->blk.data_len -= tlen;
716
717 /* scratch buf to read the start and len tags */
718 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
719 &qup->brx.tag.start[0],
720 2, qup, DMA_FROM_DEVICE);
721
722 if (ret)
723 return ret;
724
725 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
726 &msg->buf[limit * i],
727 tlen, qup,
728 DMA_FROM_DEVICE);
729 if (ret)
730 return ret;
731
732 i++;
733 qup->blk.pos = i;
734 }
735 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
736 &qup->start_tag.start[off],
737 len, qup, DMA_TO_DEVICE);
738 if (ret)
739 return ret;
740
741 off += len;
742 /* scratch buf to read the BAM EOT and FLUSH tags */
743 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
744 &qup->brx.tag.start[0],
745 2, qup, DMA_FROM_DEVICE);
746 if (ret)
747 return ret;
748 } else {
749 tx_nents += (blocks * 2);
750
751 while (qup->blk.pos < blocks) {
752 tlen = (i == (blocks - 1)) ? rem : limit;
753 tags = &qup->start_tag.start[off + tx_len];
754 len = qup_i2c_set_tags(tags, qup, msg, 1);
755 qup->blk.data_len -= tlen;
756
757 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
758 tags, len,
759 qup, DMA_TO_DEVICE);
760 if (ret)
761 return ret;
762
763 tx_len += len;
764 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
765 &msg->buf[limit * i],
766 tlen, qup, DMA_TO_DEVICE);
767 if (ret)
768 return ret;
769 i++;
770 qup->blk.pos = i;
771 }
772 off += tx_len;
773
774 if (idx == (num - 1)) {
775 len = 1;
776 if (rx_nents) {
777 qup->btx.tag.start[0] =
778 QUP_BAM_INPUT_EOT;
779 len++;
780 }
781 qup->btx.tag.start[len - 1] =
782 QUP_BAM_FLUSH_STOP;
783 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
784 &qup->btx.tag.start[0],
785 len, qup, DMA_TO_DEVICE);
786 if (ret)
787 return ret;
788 tx_nents += 1;
789 }
790 }
791 idx++;
792 msg++;
793 }
794
795 txd = dmaengine_prep_slave_sg(qup->btx.dma, qup->btx.sg, tx_nents,
796 DMA_MEM_TO_DEV,
797 DMA_PREP_INTERRUPT | DMA_PREP_FENCE);
798 if (!txd) {
799 dev_err(qup->dev, "failed to get tx desc\n");
800 ret = -EINVAL;
801 goto desc_err;
802 }
803
804 if (!rx_nents) {
805 txd->callback = qup_i2c_bam_cb;
806 txd->callback_param = qup;
807 }
808
809 cookie_tx = dmaengine_submit(txd);
810 if (dma_submit_error(cookie_tx)) {
811 ret = -EINVAL;
812 goto desc_err;
813 }
814
815 dma_async_issue_pending(qup->btx.dma);
816
817 if (rx_nents) {
818 rxd = dmaengine_prep_slave_sg(qup->brx.dma, qup->brx.sg,
819 rx_nents, DMA_DEV_TO_MEM,
820 DMA_PREP_INTERRUPT);
821 if (!rxd) {
822 dev_err(qup->dev, "failed to get rx desc\n");
823 ret = -EINVAL;
824
825 /* abort TX descriptors */
826 dmaengine_terminate_all(qup->btx.dma);
827 goto desc_err;
828 }
829
830 rxd->callback = qup_i2c_bam_cb;
831 rxd->callback_param = qup;
832 cookie_rx = dmaengine_submit(rxd);
833 if (dma_submit_error(cookie_rx)) {
834 ret = -EINVAL;
835 goto desc_err;
836 }
837
838 dma_async_issue_pending(qup->brx.dma);
839 }
840
841 if (!wait_for_completion_timeout(&qup->xfer, TOUT_MAX * HZ)) {
842 dev_err(qup->dev, "normal trans timed out\n");
843 ret = -ETIMEDOUT;
844 }
845
846 if (ret || qup->bus_err || qup->qup_err) {
847 reinit_completion(&qup->xfer);
848
849 if (qup_i2c_change_state(qup, QUP_RUN_STATE)) {
850 dev_err(qup->dev, "change to run state timed out");
851 goto desc_err;
852 }
853
854 if (rx_nents)
855 writel(QUP_BAM_INPUT_EOT,
856 qup->base + QUP_OUT_FIFO_BASE);
857
858 writel(QUP_BAM_FLUSH_STOP, qup->base + QUP_OUT_FIFO_BASE);
859
860 qup_i2c_flush(qup);
861
862 /* wait for remaining interrupts to occur */
863 if (!wait_for_completion_timeout(&qup->xfer, HZ))
864 dev_err(qup->dev, "flush timed out\n");
865
866 qup_i2c_rel_dma(qup);
867
868 ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
869 }
870
871desc_err:
872 dma_unmap_sg(qup->dev, qup->btx.sg, tx_nents, DMA_TO_DEVICE);
873
874 if (rx_nents)
875 dma_unmap_sg(qup->dev, qup->brx.sg, rx_nents,
876 DMA_FROM_DEVICE);
877
878 return ret;
879}
880
881static int qup_i2c_bam_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
882 int num)
883{
884 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
885 int ret = 0;
886
887 enable_irq(qup->irq);
888 ret = qup_i2c_req_dma(qup);
889
890 if (ret)
891 goto out;
892
893 writel(0, qup->base + QUP_MX_INPUT_CNT);
894 writel(0, qup->base + QUP_MX_OUTPUT_CNT);
895
896 /* set BAM mode */
897 writel(QUP_REPACK_EN | QUP_BAM_MODE, qup->base + QUP_IO_MODE);
898
899 /* mask fifo irqs */
900 writel((0x3 << 8), qup->base + QUP_OPERATIONAL_MASK);
901
902 /* set RUN STATE */
903 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
904 if (ret)
905 goto out;
906
907 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
908
909 qup->msg = msg;
910 ret = qup_i2c_bam_do_xfer(qup, qup->msg, num);
911out:
912 disable_irq(qup->irq);
913
914 qup->msg = NULL;
915 return ret;
916}
917
918static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
919 struct i2c_msg *msg)
920{
921 unsigned long left;
922 int ret = 0;
923
924 left = wait_for_completion_timeout(&qup->xfer, HZ);
925 if (!left) {
926 writel(1, qup->base + QUP_SW_RESET);
927 ret = -ETIMEDOUT;
928 }
929
930 if (qup->bus_err || qup->qup_err)
931 ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
932
933 return ret;
934}
935
936static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
937{
938 int ret = 0;
939
940 qup->msg = msg;
941 qup->pos = 0;
942 enable_irq(qup->irq);
943 qup_i2c_set_blk_data(qup, msg);
944 qup_i2c_set_write_mode_v2(qup, msg);
945
946 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
947 if (ret)
948 goto err;
949
950 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
951
952 do {
953 ret = qup_i2c_issue_xfer_v2(qup, msg);
954 if (ret)
955 goto err;
956
957 ret = qup_i2c_wait_for_complete(qup, msg);
958 if (ret)
959 goto err;
960
961 qup->blk.pos++;
962 } while (qup->blk.pos < qup->blk.count);
963
964 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
965
966err:
967 disable_irq(qup->irq);
968 qup->msg = NULL;
969
970 return ret;
971}
972
973static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
974{
975 int ret;
976
977 qup->msg = msg;
978 qup->pos = 0;
979
980 enable_irq(qup->irq);
981
982 qup_i2c_set_write_mode(qup, msg);
983
984 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
985 if (ret)
986 goto err;
987
988 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
989
990 do {
991 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
992 if (ret)
993 goto err;
994
995 ret = qup_i2c_issue_write(qup, msg);
996 if (ret)
997 goto err;
998
999 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1000 if (ret)
1001 goto err;
1002
1003 ret = qup_i2c_wait_for_complete(qup, msg);
1004 if (ret)
1005 goto err;
1006 } while (qup->pos < msg->len);
1007
1008 /* Wait for the outstanding data in the fifo to drain */
1009 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
1010err:
1011 disable_irq(qup->irq);
1012 qup->msg = NULL;
1013
1014 return ret;
1015}
1016
1017static void qup_i2c_set_read_mode(struct qup_i2c_dev *qup, int len)
1018{
1019 if (len < qup->in_fifo_sz) {
1020 /* FIFO mode */
1021 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
1022 writel(len, qup->base + QUP_MX_READ_CNT);
1023 } else {
1024 /* BLOCK mode (transfer data on chunks) */
1025 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
1026 qup->base + QUP_IO_MODE);
1027 writel(len, qup->base + QUP_MX_INPUT_CNT);
1028 }
1029}
1030
1031static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
1032{
1033 int tx_len = qup->blk.tx_tag_len;
1034
1035 len += qup->blk.rx_tag_len;
1036 len |= qup->config_run;
1037 tx_len |= qup->config_run;
1038
1039 if (len < qup->in_fifo_sz) {
1040 /* FIFO mode */
1041 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
1042 writel(tx_len, qup->base + QUP_MX_WRITE_CNT);
1043 writel(len, qup->base + QUP_MX_READ_CNT);
1044 } else {
1045 /* BLOCK mode (transfer data on chunks) */
1046 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
1047 qup->base + QUP_IO_MODE);
1048 writel(tx_len, qup->base + QUP_MX_OUTPUT_CNT);
1049 writel(len, qup->base + QUP_MX_INPUT_CNT);
1050 }
1051}
1052
1053static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1054{
1055 u32 addr, len, val;
1056
1057 addr = i2c_8bit_addr_from_msg(msg);
1058
1059 /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
1060 len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
1061
1062 val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
1063 writel(val, qup->base + QUP_OUT_FIFO_BASE);
1064}
1065
1066
1067static int qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1068{
1069 u32 val = 0;
1070 int idx;
1071 int ret = 0;
1072
1073 for (idx = 0; qup->pos < msg->len; idx++) {
1074 if ((idx & 1) == 0) {
1075 /* Check that FIFO have data */
1076 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
1077 SET_BIT, 4 * ONE_BYTE);
1078 if (ret)
1079 return ret;
1080
1081 /* Reading 2 words at time */
1082 val = readl(qup->base + QUP_IN_FIFO_BASE);
1083
1084 msg->buf[qup->pos++] = val & 0xFF;
1085 } else {
1086 msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
1087 }
1088 }
1089
1090 return ret;
1091}
1092
1093static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
1094 struct i2c_msg *msg)
1095{
1096 u32 val;
1097 int idx, pos = 0, ret = 0, total, msg_offset = 0;
1098
1099 /*
1100 * If the message length is already read in
1101 * the first byte of the buffer, account for
1102 * that by setting the offset
1103 */
1104 if (qup_i2c_check_msg_len(msg) && (msg->len > 1))
1105 msg_offset = 1;
1106 total = qup_i2c_get_data_len(qup);
1107 total -= msg_offset;
1108
1109 /* 2 extra bytes for read tags */
1110 while (pos < (total + 2)) {
1111 /* Check that FIFO have data */
1112 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
1113 SET_BIT, 4 * ONE_BYTE);
1114 if (ret) {
1115 dev_err(qup->dev, "timeout for fifo not empty");
1116 return ret;
1117 }
1118 val = readl(qup->base + QUP_IN_FIFO_BASE);
1119
1120 for (idx = 0; idx < 4; idx++, val >>= 8, pos++) {
1121 /* first 2 bytes are tag bytes */
1122 if (pos < 2)
1123 continue;
1124
1125 if (pos >= (total + 2))
1126 goto out;
1127 msg->buf[qup->pos + msg_offset] = val & 0xff;
1128 qup->pos++;
1129 }
1130 }
1131
1132out:
1133 qup->blk.data_len -= total;
1134
1135 return ret;
1136}
1137
1138static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1139{
1140 int ret = 0;
1141
1142 qup->msg = msg;
1143 qup->pos = 0;
1144 enable_irq(qup->irq);
1145 qup_i2c_set_blk_data(qup, msg);
1146 qup_i2c_set_read_mode_v2(qup, msg->len);
1147
1148 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1149 if (ret)
1150 goto err;
1151
1152 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
1153
1154 do {
1155 ret = qup_i2c_issue_xfer_v2(qup, msg);
1156 if (ret)
1157 goto err;
1158
1159 ret = qup_i2c_wait_for_complete(qup, msg);
1160 if (ret)
1161 goto err;
1162
1163 ret = qup_i2c_read_fifo_v2(qup, msg);
1164 if (ret)
1165 goto err;
1166
1167 qup->blk.pos++;
1168
1169 /* Handle SMBus block read length */
1170 if (qup_i2c_check_msg_len(msg) && (msg->len == 1)) {
1171 if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX) {
1172 ret = -EPROTO;
1173 goto err;
1174 }
1175 msg->len += msg->buf[0];
1176 qup->pos = 0;
1177 qup_i2c_set_blk_data(qup, msg);
1178 /* set tag length for block read */
1179 qup->blk.tx_tag_len = 2;
1180 qup_i2c_set_read_mode_v2(qup, msg->buf[0]);
1181 }
1182 } while (qup->blk.pos < qup->blk.count);
1183
1184err:
1185 disable_irq(qup->irq);
1186 qup->msg = NULL;
1187
1188 return ret;
1189}
1190
1191static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1192{
1193 int ret;
1194
1195 qup->msg = msg;
1196 qup->pos = 0;
1197
1198 enable_irq(qup->irq);
1199 qup_i2c_set_read_mode(qup, msg->len);
1200
1201 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1202 if (ret)
1203 goto err;
1204
1205 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
1206
1207 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
1208 if (ret)
1209 goto err;
1210
1211 qup_i2c_issue_read(qup, msg);
1212
1213 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1214 if (ret)
1215 goto err;
1216
1217 do {
1218 ret = qup_i2c_wait_for_complete(qup, msg);
1219 if (ret)
1220 goto err;
1221
1222 ret = qup_i2c_read_fifo(qup, msg);
1223 if (ret)
1224 goto err;
1225 } while (qup->pos < msg->len);
1226
1227err:
1228 disable_irq(qup->irq);
1229 qup->msg = NULL;
1230
1231 return ret;
1232}
1233
1234static int qup_i2c_xfer(struct i2c_adapter *adap,
1235 struct i2c_msg msgs[],
1236 int num)
1237{
1238 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
1239 int ret, idx;
1240
1241 ret = pm_runtime_get_sync(qup->dev);
1242 if (ret < 0)
1243 goto out;
1244
1245 qup->bus_err = 0;
1246 qup->qup_err = 0;
1247
1248 writel(1, qup->base + QUP_SW_RESET);
1249 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
1250 if (ret)
1251 goto out;
1252
1253 /* Configure QUP as I2C mini core */
1254 writel(I2C_MINI_CORE | I2C_N_VAL, qup->base + QUP_CONFIG);
1255
1256 for (idx = 0; idx < num; idx++) {
1257 if (msgs[idx].len == 0) {
1258 ret = -EINVAL;
1259 goto out;
1260 }
1261
1262 if (qup_i2c_poll_state_i2c_master(qup)) {
1263 ret = -EIO;
1264 goto out;
1265 }
1266
1267 if (qup_i2c_check_msg_len(&msgs[idx])) {
1268 ret = -EINVAL;
1269 goto out;
1270 }
1271
1272 if (msgs[idx].flags & I2C_M_RD)
1273 ret = qup_i2c_read_one(qup, &msgs[idx]);
1274 else
1275 ret = qup_i2c_write_one(qup, &msgs[idx]);
1276
1277 if (ret)
1278 break;
1279
1280 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
1281 if (ret)
1282 break;
1283 }
1284
1285 if (ret == 0)
1286 ret = num;
1287out:
1288
1289 pm_runtime_mark_last_busy(qup->dev);
1290 pm_runtime_put_autosuspend(qup->dev);
1291
1292 return ret;
1293}
1294
1295static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
1296 struct i2c_msg msgs[],
1297 int num)
1298{
1299 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
1300 int ret, len, idx = 0, use_dma = 0;
1301
1302 qup->bus_err = 0;
1303 qup->qup_err = 0;
1304
1305 ret = pm_runtime_get_sync(qup->dev);
1306 if (ret < 0)
1307 goto out;
1308
1309 writel(1, qup->base + QUP_SW_RESET);
1310 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
1311 if (ret)
1312 goto out;
1313
1314 /* Configure QUP as I2C mini core */
1315 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
1316 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
1317
1318 if ((qup->is_dma)) {
1319 /* All i2c_msgs should be transferred using either dma or cpu */
1320 for (idx = 0; idx < num; idx++) {
1321 if (msgs[idx].len == 0) {
1322 ret = -EINVAL;
1323 goto out;
1324 }
1325
1326 len = (msgs[idx].len > qup->out_fifo_sz) ||
1327 (msgs[idx].len > qup->in_fifo_sz);
1328
1329 if ((!is_vmalloc_addr(msgs[idx].buf)) && len) {
1330 use_dma = 1;
1331 } else {
1332 use_dma = 0;
1333 break;
1334 }
1335 }
1336 }
1337
1338 idx = 0;
1339
1340 do {
1341 if (msgs[idx].len == 0) {
1342 ret = -EINVAL;
1343 goto out;
1344 }
1345
1346 if (qup_i2c_poll_state_i2c_master(qup)) {
1347 ret = -EIO;
1348 goto out;
1349 }
1350
1351 qup->is_last = (idx == (num - 1));
1352 if (idx)
1353 qup->config_run = QUP_I2C_MX_CONFIG_DURING_RUN;
1354 else
1355 qup->config_run = 0;
1356
1357 reinit_completion(&qup->xfer);
1358
1359 if (use_dma) {
1360 ret = qup_i2c_bam_xfer(adap, &msgs[idx], num);
1361 } else {
1362 if (msgs[idx].flags & I2C_M_RD)
1363 ret = qup_i2c_read_one_v2(qup, &msgs[idx]);
1364 else
1365 ret = qup_i2c_write_one_v2(qup, &msgs[idx]);
1366 }
1367 } while ((idx++ < (num - 1)) && !use_dma && !ret);
1368
1369 if (!ret)
1370 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
1371
1372 if (ret == 0)
1373 ret = num;
1374out:
1375 pm_runtime_mark_last_busy(qup->dev);
1376 pm_runtime_put_autosuspend(qup->dev);
1377
1378 return ret;
1379}
1380
1381static u32 qup_i2c_func(struct i2c_adapter *adap)
1382{
1383 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
1384}
1385
1386static const struct i2c_algorithm qup_i2c_algo = {
1387 .master_xfer = qup_i2c_xfer,
1388 .functionality = qup_i2c_func,
1389};
1390
1391static const struct i2c_algorithm qup_i2c_algo_v2 = {
1392 .master_xfer = qup_i2c_xfer_v2,
1393 .functionality = qup_i2c_func,
1394};
1395
1396/*
1397 * The QUP block will issue a NACK and STOP on the bus when reaching
1398 * the end of the read, the length of the read is specified as one byte
1399 * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
1400 */
1401static const struct i2c_adapter_quirks qup_i2c_quirks = {
1402 .max_read_len = QUP_READ_LIMIT,
1403};
1404
1405static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
1406{
1407 clk_prepare_enable(qup->clk);
1408 clk_prepare_enable(qup->pclk);
1409}
1410
1411static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
1412{
1413 u32 config;
1414
1415 qup_i2c_change_state(qup, QUP_RESET_STATE);
1416 clk_disable_unprepare(qup->clk);
1417 config = readl(qup->base + QUP_CONFIG);
1418 config |= QUP_CLOCK_AUTO_GATE;
1419 writel(config, qup->base + QUP_CONFIG);
1420 clk_disable_unprepare(qup->pclk);
1421}
1422
1423static int qup_i2c_probe(struct platform_device *pdev)
1424{
1425 static const int blk_sizes[] = {4, 16, 32};
1426 struct qup_i2c_dev *qup;
1427 unsigned long one_bit_t;
1428 struct resource *res;
1429 u32 io_mode, hw_ver, size;
1430 int ret, fs_div, hs_div;
1431 u32 src_clk_freq = DEFAULT_SRC_CLK;
1432 u32 clk_freq = DEFAULT_CLK_FREQ;
1433 int blocks;
1434
1435 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
1436 if (!qup)
1437 return -ENOMEM;
1438
1439 qup->dev = &pdev->dev;
1440 init_completion(&qup->xfer);
1441 platform_set_drvdata(pdev, qup);
1442
1443 ret = device_property_read_u32(qup->dev, "clock-frequency", &clk_freq);
1444 if (ret) {
1445 dev_notice(qup->dev, "using default clock-frequency %d",
1446 DEFAULT_CLK_FREQ);
1447 }
1448
1449 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
1450 qup->adap.algo = &qup_i2c_algo;
1451 qup->adap.quirks = &qup_i2c_quirks;
1452 } else {
1453 qup->adap.algo = &qup_i2c_algo_v2;
1454 ret = qup_i2c_req_dma(qup);
1455
1456 if (ret == -EPROBE_DEFER)
1457 goto fail_dma;
1458 else if (ret != 0)
1459 goto nodma;
1460
1461 blocks = (MX_BLOCKS << 1) + 1;
1462 qup->btx.sg = devm_kzalloc(&pdev->dev,
1463 sizeof(*qup->btx.sg) * blocks,
1464 GFP_KERNEL);
1465 if (!qup->btx.sg) {
1466 ret = -ENOMEM;
1467 goto fail_dma;
1468 }
1469 sg_init_table(qup->btx.sg, blocks);
1470
1471 qup->brx.sg = devm_kzalloc(&pdev->dev,
1472 sizeof(*qup->brx.sg) * blocks,
1473 GFP_KERNEL);
1474 if (!qup->brx.sg) {
1475 ret = -ENOMEM;
1476 goto fail_dma;
1477 }
1478 sg_init_table(qup->brx.sg, blocks);
1479
1480 /* 2 tag bytes for each block + 5 for start, stop tags */
1481 size = blocks * 2 + 5;
1482
1483 qup->start_tag.start = devm_kzalloc(&pdev->dev,
1484 size, GFP_KERNEL);
1485 if (!qup->start_tag.start) {
1486 ret = -ENOMEM;
1487 goto fail_dma;
1488 }
1489
1490 qup->brx.tag.start = devm_kzalloc(&pdev->dev, 2, GFP_KERNEL);
1491 if (!qup->brx.tag.start) {
1492 ret = -ENOMEM;
1493 goto fail_dma;
1494 }
1495
1496 qup->btx.tag.start = devm_kzalloc(&pdev->dev, 2, GFP_KERNEL);
1497 if (!qup->btx.tag.start) {
1498 ret = -ENOMEM;
1499 goto fail_dma;
1500 }
1501 qup->is_dma = true;
1502 }
1503
1504nodma:
1505 /* We support frequencies up to FAST Mode (400KHz) */
1506 if (!clk_freq || clk_freq > 400000) {
1507 dev_err(qup->dev, "clock frequency not supported %d\n",
1508 clk_freq);
1509 return -EINVAL;
1510 }
1511
1512 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1513 qup->base = devm_ioremap_resource(qup->dev, res);
1514 if (IS_ERR(qup->base))
1515 return PTR_ERR(qup->base);
1516
1517 qup->irq = platform_get_irq(pdev, 0);
1518 if (qup->irq < 0) {
1519 dev_err(qup->dev, "No IRQ defined\n");
1520 return qup->irq;
1521 }
1522
1523 if (has_acpi_companion(qup->dev)) {
1524 ret = device_property_read_u32(qup->dev,
1525 "src-clock-hz", &src_clk_freq);
1526 if (ret) {
1527 dev_notice(qup->dev, "using default src-clock-hz %d",
1528 DEFAULT_SRC_CLK);
1529 }
1530 ACPI_COMPANION_SET(&qup->adap.dev, ACPI_COMPANION(qup->dev));
1531 } else {
1532 qup->clk = devm_clk_get(qup->dev, "core");
1533 if (IS_ERR(qup->clk)) {
1534 dev_err(qup->dev, "Could not get core clock\n");
1535 return PTR_ERR(qup->clk);
1536 }
1537
1538 qup->pclk = devm_clk_get(qup->dev, "iface");
1539 if (IS_ERR(qup->pclk)) {
1540 dev_err(qup->dev, "Could not get iface clock\n");
1541 return PTR_ERR(qup->pclk);
1542 }
1543 qup_i2c_enable_clocks(qup);
1544 src_clk_freq = clk_get_rate(qup->clk);
1545 }
1546
1547 /*
1548 * Bootloaders might leave a pending interrupt on certain QUP's,
1549 * so we reset the core before registering for interrupts.
1550 */
1551 writel(1, qup->base + QUP_SW_RESET);
1552 ret = qup_i2c_poll_state_valid(qup);
1553 if (ret)
1554 goto fail;
1555
1556 ret = devm_request_irq(qup->dev, qup->irq, qup_i2c_interrupt,
1557 IRQF_TRIGGER_HIGH, "i2c_qup", qup);
1558 if (ret) {
1559 dev_err(qup->dev, "Request %d IRQ failed\n", qup->irq);
1560 goto fail;
1561 }
1562 disable_irq(qup->irq);
1563
1564 hw_ver = readl(qup->base + QUP_HW_VERSION);
1565 dev_dbg(qup->dev, "Revision %x\n", hw_ver);
1566
1567 io_mode = readl(qup->base + QUP_IO_MODE);
1568
1569 /*
1570 * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
1571 * associated with each byte written/received
1572 */
1573 size = QUP_OUTPUT_BLOCK_SIZE(io_mode);
1574 if (size >= ARRAY_SIZE(blk_sizes)) {
1575 ret = -EIO;
1576 goto fail;
1577 }
1578 qup->out_blk_sz = blk_sizes[size] / 2;
1579
1580 size = QUP_INPUT_BLOCK_SIZE(io_mode);
1581 if (size >= ARRAY_SIZE(blk_sizes)) {
1582 ret = -EIO;
1583 goto fail;
1584 }
1585 qup->in_blk_sz = blk_sizes[size] / 2;
1586
1587 size = QUP_OUTPUT_FIFO_SIZE(io_mode);
1588 qup->out_fifo_sz = qup->out_blk_sz * (2 << size);
1589
1590 size = QUP_INPUT_FIFO_SIZE(io_mode);
1591 qup->in_fifo_sz = qup->in_blk_sz * (2 << size);
1592
1593 fs_div = ((src_clk_freq / clk_freq) / 2) - 3;
1594 hs_div = 3;
1595 qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff);
1596
1597 /*
1598 * Time it takes for a byte to be clocked out on the bus.
1599 * Each byte takes 9 clock cycles (8 bits + 1 ack).
1600 */
1601 one_bit_t = (USEC_PER_SEC / clk_freq) + 1;
1602 qup->one_byte_t = one_bit_t * 9;
1603
1604 dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
1605 qup->in_blk_sz, qup->in_fifo_sz,
1606 qup->out_blk_sz, qup->out_fifo_sz);
1607
1608 i2c_set_adapdata(&qup->adap, qup);
1609 qup->adap.dev.parent = qup->dev;
1610 qup->adap.dev.of_node = pdev->dev.of_node;
1611 qup->is_last = true;
1612
1613 strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
1614
1615 pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC);
1616 pm_runtime_use_autosuspend(qup->dev);
1617 pm_runtime_set_active(qup->dev);
1618 pm_runtime_enable(qup->dev);
1619
1620 ret = i2c_add_adapter(&qup->adap);
1621 if (ret)
1622 goto fail_runtime;
1623
1624 return 0;
1625
1626fail_runtime:
1627 pm_runtime_disable(qup->dev);
1628 pm_runtime_set_suspended(qup->dev);
1629fail:
1630 qup_i2c_disable_clocks(qup);
1631fail_dma:
1632 if (qup->btx.dma)
1633 dma_release_channel(qup->btx.dma);
1634 if (qup->brx.dma)
1635 dma_release_channel(qup->brx.dma);
1636 return ret;
1637}
1638
1639static int qup_i2c_remove(struct platform_device *pdev)
1640{
1641 struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
1642
1643 if (qup->is_dma) {
1644 dma_release_channel(qup->btx.dma);
1645 dma_release_channel(qup->brx.dma);
1646 }
1647
1648 disable_irq(qup->irq);
1649 qup_i2c_disable_clocks(qup);
1650 i2c_del_adapter(&qup->adap);
1651 pm_runtime_disable(qup->dev);
1652 pm_runtime_set_suspended(qup->dev);
1653 return 0;
1654}
1655
1656#ifdef CONFIG_PM
1657static int qup_i2c_pm_suspend_runtime(struct device *device)
1658{
1659 struct qup_i2c_dev *qup = dev_get_drvdata(device);
1660
1661 dev_dbg(device, "pm_runtime: suspending...\n");
1662 qup_i2c_disable_clocks(qup);
1663 return 0;
1664}
1665
1666static int qup_i2c_pm_resume_runtime(struct device *device)
1667{
1668 struct qup_i2c_dev *qup = dev_get_drvdata(device);
1669
1670 dev_dbg(device, "pm_runtime: resuming...\n");
1671 qup_i2c_enable_clocks(qup);
1672 return 0;
1673}
1674#endif
1675
1676#ifdef CONFIG_PM_SLEEP
1677static int qup_i2c_suspend(struct device *device)
1678{
1679 if (!pm_runtime_suspended(device))
1680 return qup_i2c_pm_suspend_runtime(device);
1681 return 0;
1682}
1683
1684static int qup_i2c_resume(struct device *device)
1685{
1686 qup_i2c_pm_resume_runtime(device);
1687 pm_runtime_mark_last_busy(device);
1688 pm_request_autosuspend(device);
1689 return 0;
1690}
1691#endif
1692
1693static const struct dev_pm_ops qup_i2c_qup_pm_ops = {
1694 SET_SYSTEM_SLEEP_PM_OPS(
1695 qup_i2c_suspend,
1696 qup_i2c_resume)
1697 SET_RUNTIME_PM_OPS(
1698 qup_i2c_pm_suspend_runtime,
1699 qup_i2c_pm_resume_runtime,
1700 NULL)
1701};
1702
1703static const struct of_device_id qup_i2c_dt_match[] = {
1704 { .compatible = "qcom,i2c-qup-v1.1.1" },
1705 { .compatible = "qcom,i2c-qup-v2.1.1" },
1706 { .compatible = "qcom,i2c-qup-v2.2.1" },
1707 {}
1708};
1709MODULE_DEVICE_TABLE(of, qup_i2c_dt_match);
1710
1711#if IS_ENABLED(CONFIG_ACPI)
1712static const struct acpi_device_id qup_i2c_acpi_match[] = {
1713 { "QCOM8010"},
1714 { },
1715};
1716MODULE_DEVICE_TABLE(acpi, qup_i2c_acpi_match);
1717#endif
1718
1719static struct platform_driver qup_i2c_driver = {
1720 .probe = qup_i2c_probe,
1721 .remove = qup_i2c_remove,
1722 .driver = {
1723 .name = "i2c_qup",
1724 .pm = &qup_i2c_qup_pm_ops,
1725 .of_match_table = qup_i2c_dt_match,
1726 .acpi_match_table = ACPI_PTR(qup_i2c_acpi_match),
1727 },
1728};
1729
1730module_platform_driver(qup_i2c_driver);
1731
1732MODULE_LICENSE("GPL v2");
1733MODULE_ALIAS("platform:i2c_qup");