blob: be953f3aea9ce05b3999abef07b27a6109afc5b4 [file] [log] [blame]
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001/*
2 * Silicon Image SiI8620 HDMI/MHL bridge driver
3 *
4 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
5 * Andrzej Hajda <a.hajda@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Andrzej Hajdae19e9c62017-02-01 08:47:34 +010012#include <asm/unaligned.h>
13
Andrzej Hajdace6e1532016-10-10 09:39:17 +020014#include <drm/bridge/mhl.h>
15#include <drm/drm_crtc.h>
16#include <drm/drm_edid.h>
17
18#include <linux/clk.h>
19#include <linux/delay.h>
20#include <linux/gpio/consumer.h>
21#include <linux/i2c.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
24#include <linux/kernel.h>
25#include <linux/list.h>
26#include <linux/module.h>
27#include <linux/mutex.h>
28#include <linux/regulator/consumer.h>
29#include <linux/slab.h>
30
31#include "sil-sii8620.h"
32
Andrzej Hajdae19e9c62017-02-01 08:47:34 +010033#define SII8620_BURST_BUF_LEN 288
34#define VAL_RX_HDMI_CTRL2_DEFVAL VAL_RX_HDMI_CTRL2_IDLE_CNT(3)
Andrzej Hajdace6e1532016-10-10 09:39:17 +020035
36enum sii8620_mode {
37 CM_DISCONNECTED,
38 CM_DISCOVERY,
39 CM_MHL1,
40 CM_MHL3,
41 CM_ECBUS_S
42};
43
44enum sii8620_sink_type {
45 SINK_NONE,
46 SINK_HDMI,
47 SINK_DVI
48};
49
50enum sii8620_mt_state {
51 MT_STATE_READY,
52 MT_STATE_BUSY,
53 MT_STATE_DONE
54};
55
56struct sii8620 {
57 struct drm_bridge bridge;
58 struct device *dev;
59 struct clk *clk_xtal;
60 struct gpio_desc *gpio_reset;
61 struct gpio_desc *gpio_int;
62 struct regulator_bulk_data supplies[2];
63 struct mutex lock; /* context lock, protects fields below */
64 int error;
65 enum sii8620_mode mode;
66 enum sii8620_sink_type sink_type;
67 u8 cbus_status;
68 u8 stat[MHL_DST_SIZE];
69 u8 xstat[MHL_XDS_SIZE];
70 u8 devcap[MHL_DCAP_SIZE];
71 u8 xdevcap[MHL_XDC_SIZE];
72 u8 avif[19];
73 struct edid *edid;
74 unsigned int gen2_write_burst:1;
75 enum sii8620_mt_state mt_state;
76 struct list_head mt_queue;
Andrzej Hajdae19e9c62017-02-01 08:47:34 +010077 struct {
78 int r_size;
79 int r_count;
80 int rx_ack;
81 int rx_count;
82 u8 rx_buf[32];
83 int tx_count;
84 u8 tx_buf[32];
85 } burst;
Andrzej Hajdace6e1532016-10-10 09:39:17 +020086};
87
88struct sii8620_mt_msg;
89
90typedef void (*sii8620_mt_msg_cb)(struct sii8620 *ctx,
91 struct sii8620_mt_msg *msg);
92
Andrzej Hajda0c2d1872017-02-01 08:47:31 +010093typedef void (*sii8620_cb)(struct sii8620 *ctx, int ret);
94
Andrzej Hajdace6e1532016-10-10 09:39:17 +020095struct sii8620_mt_msg {
96 struct list_head node;
97 u8 reg[4];
98 u8 ret;
99 sii8620_mt_msg_cb send;
100 sii8620_mt_msg_cb recv;
Andrzej Hajda0c2d1872017-02-01 08:47:31 +0100101 sii8620_cb continuation;
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200102};
103
104static const u8 sii8620_i2c_page[] = {
105 0x39, /* Main System */
106 0x3d, /* TDM and HSIC */
107 0x49, /* TMDS Receiver, MHL EDID */
108 0x4d, /* eMSC, HDCP, HSIC */
109 0x5d, /* MHL Spec */
110 0x64, /* MHL CBUS */
111 0x59, /* Hardware TPI (Transmitter Programming Interface) */
112 0x61, /* eCBUS-S, eCBUS-D */
113};
114
115static void sii8620_fetch_edid(struct sii8620 *ctx);
116static void sii8620_set_upstream_edid(struct sii8620 *ctx);
117static void sii8620_enable_hpd(struct sii8620 *ctx);
118static void sii8620_mhl_disconnected(struct sii8620 *ctx);
Andrzej Hajda2c8fb852017-02-01 08:47:32 +0100119static void sii8620_disconnect(struct sii8620 *ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200120
121static int sii8620_clear_error(struct sii8620 *ctx)
122{
123 int ret = ctx->error;
124
125 ctx->error = 0;
126 return ret;
127}
128
129static void sii8620_read_buf(struct sii8620 *ctx, u16 addr, u8 *buf, int len)
130{
131 struct device *dev = ctx->dev;
132 struct i2c_client *client = to_i2c_client(dev);
133 u8 data = addr;
134 struct i2c_msg msg[] = {
135 {
136 .addr = sii8620_i2c_page[addr >> 8],
137 .flags = client->flags,
138 .len = 1,
139 .buf = &data
140 },
141 {
142 .addr = sii8620_i2c_page[addr >> 8],
143 .flags = client->flags | I2C_M_RD,
144 .len = len,
145 .buf = buf
146 },
147 };
148 int ret;
149
150 if (ctx->error)
151 return;
152
153 ret = i2c_transfer(client->adapter, msg, 2);
154 dev_dbg(dev, "read at %04x: %*ph, %d\n", addr, len, buf, ret);
155
156 if (ret != 2) {
157 dev_err(dev, "Read at %#06x of %d bytes failed with code %d.\n",
158 addr, len, ret);
159 ctx->error = ret < 0 ? ret : -EIO;
160 }
161}
162
163static u8 sii8620_readb(struct sii8620 *ctx, u16 addr)
164{
165 u8 ret;
166
167 sii8620_read_buf(ctx, addr, &ret, 1);
168 return ret;
169}
170
171static void sii8620_write_buf(struct sii8620 *ctx, u16 addr, const u8 *buf,
172 int len)
173{
174 struct device *dev = ctx->dev;
175 struct i2c_client *client = to_i2c_client(dev);
176 u8 data[2];
177 struct i2c_msg msg = {
178 .addr = sii8620_i2c_page[addr >> 8],
179 .flags = client->flags,
180 .len = len + 1,
181 };
182 int ret;
183
184 if (ctx->error)
185 return;
186
187 if (len > 1) {
188 msg.buf = kmalloc(len + 1, GFP_KERNEL);
189 if (!msg.buf) {
190 ctx->error = -ENOMEM;
191 return;
192 }
193 memcpy(msg.buf + 1, buf, len);
194 } else {
195 msg.buf = data;
196 msg.buf[1] = *buf;
197 }
198
199 msg.buf[0] = addr;
200
201 ret = i2c_transfer(client->adapter, &msg, 1);
202 dev_dbg(dev, "write at %04x: %*ph, %d\n", addr, len, buf, ret);
203
204 if (ret != 1) {
205 dev_err(dev, "Write at %#06x of %*ph failed with code %d.\n",
206 addr, len, buf, ret);
207 ctx->error = ret ?: -EIO;
208 }
209
210 if (len > 1)
211 kfree(msg.buf);
212}
213
214#define sii8620_write(ctx, addr, arr...) \
215({\
216 u8 d[] = { arr }; \
217 sii8620_write_buf(ctx, addr, d, ARRAY_SIZE(d)); \
218})
219
220static void __sii8620_write_seq(struct sii8620 *ctx, const u16 *seq, int len)
221{
222 int i;
223
224 for (i = 0; i < len; i += 2)
225 sii8620_write(ctx, seq[i], seq[i + 1]);
226}
227
228#define sii8620_write_seq(ctx, seq...) \
229({\
230 const u16 d[] = { seq }; \
231 __sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
232})
233
234#define sii8620_write_seq_static(ctx, seq...) \
235({\
236 static const u16 d[] = { seq }; \
237 __sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
238})
239
240static void sii8620_setbits(struct sii8620 *ctx, u16 addr, u8 mask, u8 val)
241{
242 val = (val & mask) | (sii8620_readb(ctx, addr) & ~mask);
243 sii8620_write(ctx, addr, val);
244}
245
Andrzej Hajdabb4954c2017-02-01 08:47:29 +0100246static inline bool sii8620_is_mhl3(struct sii8620 *ctx)
247{
248 return ctx->mode >= CM_MHL3;
249}
250
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200251static void sii8620_mt_cleanup(struct sii8620 *ctx)
252{
253 struct sii8620_mt_msg *msg, *n;
254
255 list_for_each_entry_safe(msg, n, &ctx->mt_queue, node) {
256 list_del(&msg->node);
257 kfree(msg);
258 }
259 ctx->mt_state = MT_STATE_READY;
260}
261
262static void sii8620_mt_work(struct sii8620 *ctx)
263{
264 struct sii8620_mt_msg *msg;
265
266 if (ctx->error)
267 return;
268 if (ctx->mt_state == MT_STATE_BUSY || list_empty(&ctx->mt_queue))
269 return;
270
271 if (ctx->mt_state == MT_STATE_DONE) {
272 ctx->mt_state = MT_STATE_READY;
273 msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg,
274 node);
275 if (msg->recv)
276 msg->recv(ctx, msg);
Andrzej Hajda0c2d1872017-02-01 08:47:31 +0100277 if (msg->continuation)
278 msg->continuation(ctx, msg->ret);
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200279 list_del(&msg->node);
280 kfree(msg);
281 }
282
283 if (ctx->mt_state != MT_STATE_READY || list_empty(&ctx->mt_queue))
284 return;
285
286 ctx->mt_state = MT_STATE_BUSY;
287 msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
288 if (msg->send)
289 msg->send(ctx, msg);
290}
291
292static void sii8620_mt_msc_cmd_send(struct sii8620 *ctx,
293 struct sii8620_mt_msg *msg)
294{
295 switch (msg->reg[0]) {
296 case MHL_WRITE_STAT:
297 case MHL_SET_INT:
298 sii8620_write_buf(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg + 1, 2);
299 sii8620_write(ctx, REG_MSC_COMMAND_START,
300 BIT_MSC_COMMAND_START_WRITE_STAT);
301 break;
302 case MHL_MSC_MSG:
303 sii8620_write_buf(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg, 3);
304 sii8620_write(ctx, REG_MSC_COMMAND_START,
305 BIT_MSC_COMMAND_START_MSC_MSG);
306 break;
Andrzej Hajdae9c6da22017-02-01 08:47:30 +0100307 case MHL_READ_DEVCAP_REG:
308 case MHL_READ_XDEVCAP_REG:
309 sii8620_write(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg[1]);
310 sii8620_write(ctx, REG_MSC_COMMAND_START,
311 BIT_MSC_COMMAND_START_READ_DEVCAP);
312 break;
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200313 default:
314 dev_err(ctx->dev, "%s: command %#x not supported\n", __func__,
315 msg->reg[0]);
316 }
317}
318
319static struct sii8620_mt_msg *sii8620_mt_msg_new(struct sii8620 *ctx)
320{
321 struct sii8620_mt_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
322
323 if (!msg)
324 ctx->error = -ENOMEM;
325 else
326 list_add_tail(&msg->node, &ctx->mt_queue);
327
328 return msg;
329}
330
Andrzej Hajda0c2d1872017-02-01 08:47:31 +0100331static void sii8620_mt_set_cont(struct sii8620 *ctx, sii8620_cb cont)
332{
333 struct sii8620_mt_msg *msg;
334
335 if (ctx->error)
336 return;
337
338 if (list_empty(&ctx->mt_queue)) {
339 ctx->error = -EINVAL;
340 return;
341 }
342 msg = list_last_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
343 msg->continuation = cont;
344}
345
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200346static void sii8620_mt_msc_cmd(struct sii8620 *ctx, u8 cmd, u8 arg1, u8 arg2)
347{
348 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
349
350 if (!msg)
351 return;
352
353 msg->reg[0] = cmd;
354 msg->reg[1] = arg1;
355 msg->reg[2] = arg2;
356 msg->send = sii8620_mt_msc_cmd_send;
357}
358
359static void sii8620_mt_write_stat(struct sii8620 *ctx, u8 reg, u8 val)
360{
361 sii8620_mt_msc_cmd(ctx, MHL_WRITE_STAT, reg, val);
362}
363
364static inline void sii8620_mt_set_int(struct sii8620 *ctx, u8 irq, u8 mask)
365{
366 sii8620_mt_msc_cmd(ctx, MHL_SET_INT, irq, mask);
367}
368
369static void sii8620_mt_msc_msg(struct sii8620 *ctx, u8 cmd, u8 data)
370{
371 sii8620_mt_msc_cmd(ctx, MHL_MSC_MSG, cmd, data);
372}
373
374static void sii8620_mt_rap(struct sii8620 *ctx, u8 code)
375{
376 sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RAP, code);
377}
378
379static void sii8620_mt_read_devcap_send(struct sii8620 *ctx,
380 struct sii8620_mt_msg *msg)
381{
382 u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
383 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
384 | BIT_EDID_CTRL_EDID_MODE_EN;
385
386 if (msg->reg[0] == MHL_READ_XDEVCAP)
387 ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
388
389 sii8620_write_seq(ctx,
390 REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE,
391 REG_EDID_CTRL, ctrl,
392 REG_TPI_CBUS_START, BIT_TPI_CBUS_START_GET_DEVCAP_START
393 );
394}
395
396/* copy src to dst and set changed bits in src */
397static void sii8620_update_array(u8 *dst, u8 *src, int count)
398{
399 while (--count >= 0) {
400 *src ^= *dst;
401 *dst++ ^= *src++;
402 }
403}
404
405static void sii8620_mr_devcap(struct sii8620 *ctx)
406{
407 static const char * const sink_str[] = {
408 [SINK_NONE] = "NONE",
409 [SINK_HDMI] = "HDMI",
410 [SINK_DVI] = "DVI"
411 };
412
413 u8 dcap[MHL_DCAP_SIZE];
414 char sink_name[20];
415 struct device *dev = ctx->dev;
416
417 sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, dcap, MHL_DCAP_SIZE);
418 if (ctx->error < 0)
419 return;
420
421 dev_info(dev, "dcap: %*ph\n", MHL_DCAP_SIZE, dcap);
422 dev_info(dev, "detected dongle MHL %d.%d, ChipID %02x%02x:%02x%02x\n",
423 dcap[MHL_DCAP_MHL_VERSION] / 16,
424 dcap[MHL_DCAP_MHL_VERSION] % 16, dcap[MHL_DCAP_ADOPTER_ID_H],
425 dcap[MHL_DCAP_ADOPTER_ID_L], dcap[MHL_DCAP_DEVICE_ID_H],
426 dcap[MHL_DCAP_DEVICE_ID_L]);
427 sii8620_update_array(ctx->devcap, dcap, MHL_DCAP_SIZE);
428
429 if (!(dcap[MHL_DCAP_CAT] & MHL_DCAP_CAT_SINK))
430 return;
431
432 sii8620_fetch_edid(ctx);
433 if (!ctx->edid) {
434 dev_err(ctx->dev, "Cannot fetch EDID\n");
435 sii8620_mhl_disconnected(ctx);
436 return;
437 }
438
439 if (drm_detect_hdmi_monitor(ctx->edid))
440 ctx->sink_type = SINK_HDMI;
441 else
442 ctx->sink_type = SINK_DVI;
443
444 drm_edid_get_monitor_name(ctx->edid, sink_name, ARRAY_SIZE(sink_name));
445
446 dev_info(dev, "detected sink(type: %s): %s\n",
447 sink_str[ctx->sink_type], sink_name);
448 sii8620_set_upstream_edid(ctx);
449 sii8620_enable_hpd(ctx);
450}
451
452static void sii8620_mr_xdevcap(struct sii8620 *ctx)
453{
454 sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, ctx->xdevcap,
455 MHL_XDC_SIZE);
456
457 sii8620_mt_write_stat(ctx, MHL_XDS_REG(CURR_ECBUS_MODE),
458 MHL_XDS_ECBUS_S | MHL_XDS_SLOT_MODE_8BIT);
459 sii8620_mt_rap(ctx, MHL_RAP_CBUS_MODE_UP);
460}
461
462static void sii8620_mt_read_devcap_recv(struct sii8620 *ctx,
463 struct sii8620_mt_msg *msg)
464{
465 u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
466 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
467 | BIT_EDID_CTRL_EDID_MODE_EN;
468
469 if (msg->reg[0] == MHL_READ_XDEVCAP)
470 ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
471
472 sii8620_write_seq(ctx,
473 REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE | BIT_INTR9_EDID_DONE
474 | BIT_INTR9_EDID_ERROR,
475 REG_EDID_CTRL, ctrl,
476 REG_EDID_FIFO_ADDR, 0
477 );
478
479 if (msg->reg[0] == MHL_READ_XDEVCAP)
480 sii8620_mr_xdevcap(ctx);
481 else
482 sii8620_mr_devcap(ctx);
483}
484
485static void sii8620_mt_read_devcap(struct sii8620 *ctx, bool xdevcap)
486{
487 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
488
489 if (!msg)
490 return;
491
492 msg->reg[0] = xdevcap ? MHL_READ_XDEVCAP : MHL_READ_DEVCAP;
493 msg->send = sii8620_mt_read_devcap_send;
494 msg->recv = sii8620_mt_read_devcap_recv;
495}
496
Andrzej Hajdae9c6da22017-02-01 08:47:30 +0100497static void sii8620_mt_read_devcap_reg_recv(struct sii8620 *ctx,
498 struct sii8620_mt_msg *msg)
499{
500 u8 reg = msg->reg[0] & 0x7f;
501
502 if (msg->reg[0] & 0x80)
503 ctx->xdevcap[reg] = msg->ret;
504 else
505 ctx->devcap[reg] = msg->ret;
506}
507
508static void sii8620_mt_read_devcap_reg(struct sii8620 *ctx, u8 reg)
509{
510 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
511
512 if (!msg)
513 return;
514
515 msg->reg[0] = (reg & 0x80) ? MHL_READ_XDEVCAP_REG : MHL_READ_DEVCAP_REG;
516 msg->reg[1] = reg;
517 msg->send = sii8620_mt_msc_cmd_send;
518 msg->recv = sii8620_mt_read_devcap_reg_recv;
519}
520
521static inline void sii8620_mt_read_xdevcap_reg(struct sii8620 *ctx, u8 reg)
522{
523 sii8620_mt_read_devcap_reg(ctx, reg | 0x80);
524}
525
Andrzej Hajdae19e9c62017-02-01 08:47:34 +0100526static void *sii8620_burst_get_tx_buf(struct sii8620 *ctx, int len)
527{
528 u8 *buf = &ctx->burst.tx_buf[ctx->burst.tx_count];
529 int size = len + 2;
530
531 if (ctx->burst.tx_count + size > ARRAY_SIZE(ctx->burst.tx_buf)) {
532 dev_err(ctx->dev, "TX-BLK buffer exhausted\n");
533 ctx->error = -EINVAL;
534 return NULL;
535 }
536
537 ctx->burst.tx_count += size;
538 buf[1] = len;
539
540 return buf + 2;
541}
542
543static u8 *sii8620_burst_get_rx_buf(struct sii8620 *ctx, int len)
544{
545 u8 *buf = &ctx->burst.rx_buf[ctx->burst.rx_count];
546 int size = len + 1;
547
548 if (ctx->burst.tx_count + size > ARRAY_SIZE(ctx->burst.tx_buf)) {
549 dev_err(ctx->dev, "RX-BLK buffer exhausted\n");
550 ctx->error = -EINVAL;
551 return NULL;
552 }
553
554 ctx->burst.rx_count += size;
555 buf[0] = len;
556
557 return buf + 1;
558}
559
560static void sii8620_burst_send(struct sii8620 *ctx)
561{
562 int tx_left = ctx->burst.tx_count;
563 u8 *d = ctx->burst.tx_buf;
564
565 while (tx_left > 0) {
566 int len = d[1] + 2;
567
568 if (ctx->burst.r_count + len > ctx->burst.r_size)
569 break;
570 d[0] = min(ctx->burst.rx_ack, 255);
571 ctx->burst.rx_ack -= d[0];
572 sii8620_write_buf(ctx, REG_EMSC_XMIT_WRITE_PORT, d, len);
573 ctx->burst.r_count += len;
574 tx_left -= len;
575 d += len;
576 }
577
578 ctx->burst.tx_count = tx_left;
579
580 while (ctx->burst.rx_ack > 0) {
581 u8 b[2] = { min(ctx->burst.rx_ack, 255), 0 };
582
583 if (ctx->burst.r_count + 2 > ctx->burst.r_size)
584 break;
585 ctx->burst.rx_ack -= b[0];
586 sii8620_write_buf(ctx, REG_EMSC_XMIT_WRITE_PORT, b, 2);
587 ctx->burst.r_count += 2;
588 }
589}
590
591static void sii8620_burst_receive(struct sii8620 *ctx)
592{
593 u8 buf[3], *d;
594 int count;
595
596 sii8620_read_buf(ctx, REG_EMSCRFIFOBCNTL, buf, 2);
597 count = get_unaligned_le16(buf);
598 while (count > 0) {
599 int len = min(count, 3);
600
601 sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, buf, len);
602 count -= len;
603 ctx->burst.rx_ack += len - 1;
604 ctx->burst.r_count -= buf[1];
605 if (ctx->burst.r_count < 0)
606 ctx->burst.r_count = 0;
607
608 if (len < 3 || !buf[2])
609 continue;
610
611 len = buf[2];
612 d = sii8620_burst_get_rx_buf(ctx, len);
613 if (!d)
614 continue;
615 sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, d, len);
616 count -= len;
617 ctx->burst.rx_ack += len;
618 }
619}
620
621static void sii8620_burst_tx_rbuf_info(struct sii8620 *ctx, int size)
622{
623 struct mhl_burst_blk_rcv_buffer_info *d =
624 sii8620_burst_get_tx_buf(ctx, sizeof(*d));
625 if (!d)
626 return;
627
628 d->id = cpu_to_be16(MHL_BURST_ID_BLK_RCV_BUFFER_INFO);
629 d->size = cpu_to_le16(size);
630}
631
632static void sii8620_burst_rx_all(struct sii8620 *ctx)
633{
634 u8 *d = ctx->burst.rx_buf;
635 int count = ctx->burst.rx_count;
636
637 while (count-- > 0) {
638 int len = *d++;
639 int id = get_unaligned_be16(&d[0]);
640
641 switch (id) {
642 case MHL_BURST_ID_BLK_RCV_BUFFER_INFO:
643 ctx->burst.r_size = get_unaligned_le16(&d[2]);
644 break;
645 default:
646 break;
647 }
648 count -= len;
649 d += len;
650 }
651 ctx->burst.rx_count = 0;
652}
653
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200654static void sii8620_fetch_edid(struct sii8620 *ctx)
655{
656 u8 lm_ddc, ddc_cmd, int3, cbus;
657 int fetched, i;
658 int edid_len = EDID_LENGTH;
659 u8 *edid;
660
661 sii8620_readb(ctx, REG_CBUS_STATUS);
662 lm_ddc = sii8620_readb(ctx, REG_LM_DDC);
663 ddc_cmd = sii8620_readb(ctx, REG_DDC_CMD);
664
665 sii8620_write_seq(ctx,
666 REG_INTR9_MASK, 0,
667 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
668 REG_HDCP2X_POLL_CS, 0x71,
669 REG_HDCP2X_CTRL_0, BIT_HDCP2X_CTRL_0_HDCP2X_HDCPTX,
670 REG_LM_DDC, lm_ddc | BIT_LM_DDC_SW_TPI_EN_DISABLED,
671 );
672
673 for (i = 0; i < 256; ++i) {
674 u8 ddc_stat = sii8620_readb(ctx, REG_DDC_STATUS);
675
676 if (!(ddc_stat & BIT_DDC_STATUS_DDC_I2C_IN_PROG))
677 break;
678 sii8620_write(ctx, REG_DDC_STATUS,
679 BIT_DDC_STATUS_DDC_FIFO_EMPTY);
680 }
681
682 sii8620_write(ctx, REG_DDC_ADDR, 0x50 << 1);
683
684 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
685 if (!edid) {
686 ctx->error = -ENOMEM;
687 return;
688 }
689
690#define FETCH_SIZE 16
691 for (fetched = 0; fetched < edid_len; fetched += FETCH_SIZE) {
692 sii8620_readb(ctx, REG_DDC_STATUS);
693 sii8620_write_seq(ctx,
694 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_ABORT,
695 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_CLEAR_FIFO,
696 REG_DDC_STATUS, BIT_DDC_STATUS_DDC_FIFO_EMPTY
697 );
698 sii8620_write_seq(ctx,
699 REG_DDC_SEGM, fetched >> 8,
700 REG_DDC_OFFSET, fetched & 0xff,
701 REG_DDC_DIN_CNT1, FETCH_SIZE,
702 REG_DDC_DIN_CNT2, 0,
703 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_ENH_DDC_READ_NO_ACK
704 );
705
706 do {
707 int3 = sii8620_readb(ctx, REG_INTR3);
708 cbus = sii8620_readb(ctx, REG_CBUS_STATUS);
709
710 if (int3 & BIT_DDC_CMD_DONE)
711 break;
712
713 if (!(cbus & BIT_CBUS_STATUS_CBUS_CONNECTED)) {
714 kfree(edid);
715 edid = NULL;
716 goto end;
717 }
718 } while (1);
719
720 sii8620_readb(ctx, REG_DDC_STATUS);
721 while (sii8620_readb(ctx, REG_DDC_DOUT_CNT) < FETCH_SIZE)
722 usleep_range(10, 20);
723
724 sii8620_read_buf(ctx, REG_DDC_DATA, edid + fetched, FETCH_SIZE);
725 if (fetched + FETCH_SIZE == EDID_LENGTH) {
726 u8 ext = ((struct edid *)edid)->extensions;
727
728 if (ext) {
729 u8 *new_edid;
730
731 edid_len += ext * EDID_LENGTH;
732 new_edid = krealloc(edid, edid_len, GFP_KERNEL);
733 if (!new_edid) {
734 kfree(edid);
735 ctx->error = -ENOMEM;
736 return;
737 }
738 edid = new_edid;
739 }
740 }
741
742 if (fetched + FETCH_SIZE == edid_len)
743 sii8620_write(ctx, REG_INTR3, int3);
744 }
745
746 sii8620_write(ctx, REG_LM_DDC, lm_ddc);
747
748end:
749 kfree(ctx->edid);
750 ctx->edid = (struct edid *)edid;
751}
752
753static void sii8620_set_upstream_edid(struct sii8620 *ctx)
754{
755 sii8620_setbits(ctx, REG_DPD, BIT_DPD_PDNRX12 | BIT_DPD_PDIDCK_N
756 | BIT_DPD_PD_MHL_CLK_N, 0xff);
757
758 sii8620_write_seq_static(ctx,
759 REG_RX_HDMI_CTRL3, 0x00,
760 REG_PKT_FILTER_0, 0xFF,
761 REG_PKT_FILTER_1, 0xFF,
762 REG_ALICE0_BW_I2C, 0x06
763 );
764
765 sii8620_setbits(ctx, REG_RX_HDMI_CLR_BUFFER,
766 BIT_RX_HDMI_CLR_BUFFER_VSI_CLR_EN, 0xff);
767
768 sii8620_write_seq_static(ctx,
769 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
770 | BIT_EDID_CTRL_EDID_MODE_EN,
771 REG_EDID_FIFO_ADDR, 0,
772 );
773
774 sii8620_write_buf(ctx, REG_EDID_FIFO_WR_DATA, (u8 *)ctx->edid,
775 (ctx->edid->extensions + 1) * EDID_LENGTH);
776
777 sii8620_write_seq_static(ctx,
778 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID
779 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
780 | BIT_EDID_CTRL_EDID_MODE_EN,
781 REG_INTR5_MASK, BIT_INTR_SCDT_CHANGE,
782 REG_INTR9_MASK, 0
783 );
784}
785
786static void sii8620_xtal_set_rate(struct sii8620 *ctx)
787{
788 static const struct {
789 unsigned int rate;
790 u8 div;
791 u8 tp1;
792 } rates[] = {
793 { 19200, 0x04, 0x53 },
794 { 20000, 0x04, 0x62 },
795 { 24000, 0x05, 0x75 },
796 { 30000, 0x06, 0x92 },
797 { 38400, 0x0c, 0xbc },
798 };
799 unsigned long rate = clk_get_rate(ctx->clk_xtal) / 1000;
800 int i;
801
802 for (i = 0; i < ARRAY_SIZE(rates) - 1; ++i)
803 if (rate <= rates[i].rate)
804 break;
805
806 if (rate != rates[i].rate)
807 dev_err(ctx->dev, "xtal clock rate(%lukHz) not supported, setting MHL for %ukHz.\n",
808 rate, rates[i].rate);
809
810 sii8620_write(ctx, REG_DIV_CTL_MAIN, rates[i].div);
811 sii8620_write(ctx, REG_HDCP2X_TP1, rates[i].tp1);
812}
813
814static int sii8620_hw_on(struct sii8620 *ctx)
815{
816 int ret;
817
818 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
819 if (ret)
820 return ret;
821 usleep_range(10000, 20000);
822 return clk_prepare_enable(ctx->clk_xtal);
823}
824
825static int sii8620_hw_off(struct sii8620 *ctx)
826{
827 clk_disable_unprepare(ctx->clk_xtal);
828 gpiod_set_value(ctx->gpio_reset, 1);
829 return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
830}
831
832static void sii8620_hw_reset(struct sii8620 *ctx)
833{
834 usleep_range(10000, 20000);
835 gpiod_set_value(ctx->gpio_reset, 0);
836 usleep_range(5000, 20000);
837 gpiod_set_value(ctx->gpio_reset, 1);
838 usleep_range(10000, 20000);
839 gpiod_set_value(ctx->gpio_reset, 0);
840 msleep(300);
841}
842
843static void sii8620_cbus_reset(struct sii8620 *ctx)
844{
845 sii8620_write_seq_static(ctx,
846 REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST
847 | BIT_PWD_SRST_CBUS_RST_SW_EN,
848 REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN
849 );
850}
851
852static void sii8620_set_auto_zone(struct sii8620 *ctx)
853{
854 if (ctx->mode != CM_MHL1) {
855 sii8620_write_seq_static(ctx,
856 REG_TX_ZONE_CTL1, 0x0,
857 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
858 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
859 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
860 );
861 } else {
862 sii8620_write_seq_static(ctx,
863 REG_TX_ZONE_CTL1, VAL_TX_ZONE_CTL1_TX_ZONE_CTRL_MODE,
864 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
865 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
866 );
867 }
868}
869
870static void sii8620_stop_video(struct sii8620 *ctx)
871{
872 u8 uninitialized_var(val);
873
874 sii8620_write_seq_static(ctx,
875 REG_TPI_INTR_EN, 0,
876 REG_HDCP2X_INTR0_MASK, 0,
877 REG_TPI_COPP_DATA2, 0,
878 REG_TPI_INTR_ST0, ~0,
879 );
880
881 switch (ctx->sink_type) {
882 case SINK_DVI:
883 val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
884 | BIT_TPI_SC_TPI_AV_MUTE;
885 break;
886 case SINK_HDMI:
887 val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
888 | BIT_TPI_SC_TPI_AV_MUTE
889 | BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI;
890 break;
891 default:
892 return;
893 }
894
895 sii8620_write(ctx, REG_TPI_SC, val);
896}
897
898static void sii8620_start_hdmi(struct sii8620 *ctx)
899{
900 sii8620_write_seq_static(ctx,
901 REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL
902 | BIT_RX_HDMI_CTRL2_USE_AV_MUTE,
903 REG_VID_OVRRD, BIT_VID_OVRRD_PP_AUTO_DISABLE
904 | BIT_VID_OVRRD_M1080P_OVRRD,
905 REG_VID_MODE, 0,
906 REG_MHL_TOP_CTL, 0x1,
907 REG_MHLTX_CTL6, 0xa0,
908 REG_TPI_INPUT, VAL_TPI_FORMAT(RGB, FULL),
909 REG_TPI_OUTPUT, VAL_TPI_FORMAT(RGB, FULL),
910 );
911
912 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
913 MHL_DST_LM_CLK_MODE_NORMAL |
914 MHL_DST_LM_PATH_ENABLED);
915
916 sii8620_set_auto_zone(ctx);
917
918 sii8620_write(ctx, REG_TPI_SC, BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI);
919
920 sii8620_write_buf(ctx, REG_TPI_AVI_CHSUM, ctx->avif,
921 ARRAY_SIZE(ctx->avif));
922
923 sii8620_write(ctx, REG_PKT_FILTER_0, 0xa1, 0x2);
924}
925
926static void sii8620_start_video(struct sii8620 *ctx)
927{
Andrzej Hajdabb4954c2017-02-01 08:47:29 +0100928 if (!sii8620_is_mhl3(ctx))
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200929 sii8620_stop_video(ctx);
930
931 switch (ctx->sink_type) {
932 case SINK_HDMI:
933 sii8620_start_hdmi(ctx);
934 break;
935 case SINK_DVI:
936 default:
937 break;
938 }
939}
940
941static void sii8620_disable_hpd(struct sii8620 *ctx)
942{
943 sii8620_setbits(ctx, REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID, 0);
944 sii8620_write_seq_static(ctx,
945 REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN,
946 REG_INTR8_MASK, 0
947 );
948}
949
950static void sii8620_enable_hpd(struct sii8620 *ctx)
951{
952 sii8620_setbits(ctx, REG_TMDS_CSTAT_P3,
953 BIT_TMDS_CSTAT_P3_SCDT_CLR_AVI_DIS
954 | BIT_TMDS_CSTAT_P3_CLR_AVI, ~0);
955 sii8620_write_seq_static(ctx,
956 REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN
957 | BIT_HPD_CTRL_HPD_HIGH,
958 );
959}
960
961static void sii8620_enable_gen2_write_burst(struct sii8620 *ctx)
962{
963 if (ctx->gen2_write_burst)
964 return;
965
966 sii8620_write_seq_static(ctx,
967 REG_MDT_RCV_TIMEOUT, 100,
968 REG_MDT_RCV_CTRL, BIT_MDT_RCV_CTRL_MDT_RCV_EN
969 );
970 ctx->gen2_write_burst = 1;
971}
972
973static void sii8620_disable_gen2_write_burst(struct sii8620 *ctx)
974{
975 if (!ctx->gen2_write_burst)
976 return;
977
978 sii8620_write_seq_static(ctx,
979 REG_MDT_XMIT_CTRL, 0,
980 REG_MDT_RCV_CTRL, 0
981 );
982 ctx->gen2_write_burst = 0;
983}
984
985static void sii8620_start_gen2_write_burst(struct sii8620 *ctx)
986{
987 sii8620_write_seq_static(ctx,
988 REG_MDT_INT_1_MASK, BIT_MDT_RCV_TIMEOUT
989 | BIT_MDT_RCV_SM_ABORT_PKT_RCVD | BIT_MDT_RCV_SM_ERROR
990 | BIT_MDT_XMIT_TIMEOUT | BIT_MDT_XMIT_SM_ABORT_PKT_RCVD
991 | BIT_MDT_XMIT_SM_ERROR,
992 REG_MDT_INT_0_MASK, BIT_MDT_XFIFO_EMPTY
993 | BIT_MDT_IDLE_AFTER_HAWB_DISABLE
994 | BIT_MDT_RFIFO_DATA_RDY
995 );
996 sii8620_enable_gen2_write_burst(ctx);
997}
998
999static void sii8620_mhl_discover(struct sii8620 *ctx)
1000{
1001 sii8620_write_seq_static(ctx,
1002 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1003 | BIT_DISC_CTRL9_DISC_PULSE_PROCEED,
1004 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_5K, VAL_PUP_20K),
1005 REG_CBUS_DISC_INTR0_MASK, BIT_MHL3_EST_INT
1006 | BIT_MHL_EST_INT
1007 | BIT_NOT_MHL_EST_INT
1008 | BIT_CBUS_MHL3_DISCON_INT
1009 | BIT_CBUS_MHL12_DISCON_INT
1010 | BIT_RGND_READY_INT,
1011 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1012 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1013 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
1014 REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
1015 | BIT_MHL_DP_CTL0_TX_OE_OVR,
1016 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
1017 REG_MHL_DP_CTL1, 0xA2,
1018 REG_MHL_DP_CTL2, 0x03,
1019 REG_MHL_DP_CTL3, 0x35,
1020 REG_MHL_DP_CTL5, 0x02,
1021 REG_MHL_DP_CTL6, 0x02,
1022 REG_MHL_DP_CTL7, 0x03,
1023 REG_COC_CTLC, 0xFF,
1024 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
1025 | BIT_DPD_OSC_EN | BIT_DPD_PWRON_HSIC,
1026 REG_COC_INTR_MASK, BIT_COC_PLL_LOCK_STATUS_CHANGE
1027 | BIT_COC_CALIBRATION_DONE,
1028 REG_CBUS_INT_1_MASK, BIT_CBUS_MSC_ABORT_RCVD
1029 | BIT_CBUS_CMD_ABORT,
1030 REG_CBUS_INT_0_MASK, BIT_CBUS_MSC_MT_DONE
1031 | BIT_CBUS_HPD_CHG
1032 | BIT_CBUS_MSC_MR_WRITE_STAT
1033 | BIT_CBUS_MSC_MR_MSC_MSG
1034 | BIT_CBUS_MSC_MR_WRITE_BURST
1035 | BIT_CBUS_MSC_MR_SET_INT
1036 | BIT_CBUS_MSC_MT_DONE_NACK
1037 );
1038}
1039
1040static void sii8620_peer_specific_init(struct sii8620 *ctx)
1041{
Andrzej Hajdabb4954c2017-02-01 08:47:29 +01001042 if (sii8620_is_mhl3(ctx))
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001043 sii8620_write_seq_static(ctx,
1044 REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD,
1045 REG_EMSCINTRMASK1,
1046 BIT_EMSCINTR1_EMSC_TRAINING_COMMA_ERR
1047 );
1048 else
1049 sii8620_write_seq_static(ctx,
1050 REG_HDCP2X_INTR0_MASK, 0x00,
1051 REG_EMSCINTRMASK1, 0x00,
1052 REG_HDCP2X_INTR0, 0xFF,
1053 REG_INTR1, 0xFF,
1054 REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD
1055 | BIT_SYS_CTRL1_TX_CTRL_HDMI
1056 );
1057}
1058
1059#define SII8620_MHL_VERSION 0x32
1060#define SII8620_SCRATCHPAD_SIZE 16
1061#define SII8620_INT_STAT_SIZE 0x33
1062
1063static void sii8620_set_dev_cap(struct sii8620 *ctx)
1064{
1065 static const u8 devcap[MHL_DCAP_SIZE] = {
1066 [MHL_DCAP_MHL_VERSION] = SII8620_MHL_VERSION,
1067 [MHL_DCAP_CAT] = MHL_DCAP_CAT_SOURCE | MHL_DCAP_CAT_POWER,
1068 [MHL_DCAP_ADOPTER_ID_H] = 0x01,
1069 [MHL_DCAP_ADOPTER_ID_L] = 0x41,
1070 [MHL_DCAP_VID_LINK_MODE] = MHL_DCAP_VID_LINK_RGB444
1071 | MHL_DCAP_VID_LINK_PPIXEL
1072 | MHL_DCAP_VID_LINK_16BPP,
1073 [MHL_DCAP_AUD_LINK_MODE] = MHL_DCAP_AUD_LINK_2CH,
1074 [MHL_DCAP_VIDEO_TYPE] = MHL_DCAP_VT_GRAPHICS,
1075 [MHL_DCAP_LOG_DEV_MAP] = MHL_DCAP_LD_GUI,
1076 [MHL_DCAP_BANDWIDTH] = 0x0f,
1077 [MHL_DCAP_FEATURE_FLAG] = MHL_DCAP_FEATURE_RCP_SUPPORT
1078 | MHL_DCAP_FEATURE_RAP_SUPPORT
1079 | MHL_DCAP_FEATURE_SP_SUPPORT,
1080 [MHL_DCAP_SCRATCHPAD_SIZE] = SII8620_SCRATCHPAD_SIZE,
1081 [MHL_DCAP_INT_STAT_SIZE] = SII8620_INT_STAT_SIZE,
1082 };
1083 static const u8 xdcap[MHL_XDC_SIZE] = {
1084 [MHL_XDC_ECBUS_SPEEDS] = MHL_XDC_ECBUS_S_075
1085 | MHL_XDC_ECBUS_S_8BIT,
1086 [MHL_XDC_TMDS_SPEEDS] = MHL_XDC_TMDS_150
1087 | MHL_XDC_TMDS_300 | MHL_XDC_TMDS_600,
1088 [MHL_XDC_ECBUS_ROLES] = MHL_XDC_DEV_HOST,
1089 [MHL_XDC_LOG_DEV_MAPX] = MHL_XDC_LD_PHONE,
1090 };
1091
1092 sii8620_write_buf(ctx, REG_MHL_DEVCAP_0, devcap, ARRAY_SIZE(devcap));
1093 sii8620_write_buf(ctx, REG_MHL_EXTDEVCAP_0, xdcap, ARRAY_SIZE(xdcap));
1094}
1095
1096static void sii8620_mhl_init(struct sii8620 *ctx)
1097{
1098 sii8620_write_seq_static(ctx,
1099 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1100 REG_CBUS_MSC_COMPAT_CTRL,
1101 BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN,
1102 );
1103
1104 sii8620_peer_specific_init(ctx);
1105
1106 sii8620_disable_hpd(ctx);
1107
1108 sii8620_write_seq_static(ctx,
1109 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
1110 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1111 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1112 REG_TMDS0_CCTRL1, 0x90,
1113 REG_TMDS_CLK_EN, 0x01,
1114 REG_TMDS_CH_EN, 0x11,
1115 REG_BGR_BIAS, 0x87,
1116 REG_ALICE0_ZONE_CTRL, 0xE8,
1117 REG_ALICE0_MODE_CTRL, 0x04,
1118 );
1119 sii8620_setbits(ctx, REG_LM_DDC, BIT_LM_DDC_SW_TPI_EN_DISABLED, 0);
1120 sii8620_write_seq_static(ctx,
1121 REG_TPI_HW_OPT3, 0x76,
1122 REG_TMDS_CCTRL, BIT_TMDS_CCTRL_TMDS_OE,
1123 REG_TPI_DTD_B2, 79,
1124 );
1125 sii8620_set_dev_cap(ctx);
1126 sii8620_write_seq_static(ctx,
1127 REG_MDT_XMIT_TIMEOUT, 100,
1128 REG_MDT_XMIT_CTRL, 0x03,
1129 REG_MDT_XFIFO_STAT, 0x00,
1130 REG_MDT_RCV_TIMEOUT, 100,
1131 REG_CBUS_LINK_CTRL_8, 0x1D,
1132 );
1133
1134 sii8620_start_gen2_write_burst(ctx);
1135 sii8620_write_seq_static(ctx,
1136 REG_BIST_CTRL, 0x00,
1137 REG_COC_CTL1, 0x10,
1138 REG_COC_CTL2, 0x18,
1139 REG_COC_CTLF, 0x07,
1140 REG_COC_CTL11, 0xF8,
1141 REG_COC_CTL17, 0x61,
1142 REG_COC_CTL18, 0x46,
1143 REG_COC_CTL19, 0x15,
1144 REG_COC_CTL1A, 0x01,
1145 REG_MHL_COC_CTL3, BIT_MHL_COC_CTL3_COC_AECHO_EN,
1146 REG_MHL_COC_CTL4, 0x2D,
1147 REG_MHL_COC_CTL5, 0xF9,
1148 REG_MSC_HEARTBEAT_CTRL, 0x27,
1149 );
1150 sii8620_disable_gen2_write_burst(ctx);
1151
1152 /* currently MHL3 is not supported, so we force version to 0 */
1153 sii8620_mt_write_stat(ctx, MHL_DST_REG(VERSION), 0);
1154 sii8620_mt_write_stat(ctx, MHL_DST_REG(CONNECTED_RDY),
1155 MHL_DST_CONN_DCAP_RDY | MHL_DST_CONN_XDEVCAPP_SUPP
1156 | MHL_DST_CONN_POW_STAT);
1157 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE), MHL_INT_RC_DCAP_CHG);
1158}
1159
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001160static void sii8620_emsc_enable(struct sii8620 *ctx)
1161{
1162 u8 reg;
1163
1164 sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_EMSC_EN
1165 | BIT_GENCTL_CLR_EMSC_RFIFO
1166 | BIT_GENCTL_CLR_EMSC_XFIFO, ~0);
1167 sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_CLR_EMSC_RFIFO
1168 | BIT_GENCTL_CLR_EMSC_XFIFO, 0);
1169 sii8620_setbits(ctx, REG_COMMECNT, BIT_COMMECNT_I2C_TO_EMSC_EN, ~0);
1170 reg = sii8620_readb(ctx, REG_EMSCINTR);
1171 sii8620_write(ctx, REG_EMSCINTR, reg);
1172 sii8620_write(ctx, REG_EMSCINTRMASK, BIT_EMSCINTR_SPI_DVLD);
1173}
1174
1175static int sii8620_wait_for_fsm_state(struct sii8620 *ctx, u8 state)
1176{
1177 int i;
1178
1179 for (i = 0; i < 10; ++i) {
1180 u8 s = sii8620_readb(ctx, REG_COC_STAT_0);
1181
1182 if ((s & MSK_COC_STAT_0_FSM_STATE) == state)
1183 return 0;
1184 if (!(s & BIT_COC_STAT_0_PLL_LOCKED))
1185 return -EBUSY;
1186 usleep_range(4000, 6000);
1187 }
1188 return -ETIMEDOUT;
1189}
1190
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001191static void sii8620_set_mode(struct sii8620 *ctx, enum sii8620_mode mode)
1192{
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001193 int ret;
1194
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001195 if (ctx->mode == mode)
1196 return;
1197
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001198 switch (mode) {
1199 case CM_MHL1:
1200 sii8620_write_seq_static(ctx,
1201 REG_CBUS_MSC_COMPAT_CTRL, 0x02,
1202 REG_M3_CTRL, VAL_M3_CTRL_MHL1_2_VALUE,
1203 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
1204 | BIT_DPD_OSC_EN,
1205 REG_COC_INTR_MASK, 0
1206 );
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001207 ctx->mode = mode;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001208 break;
1209 case CM_MHL3:
Andrzej Hajdadd123122017-02-01 08:47:28 +01001210 sii8620_write(ctx, REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE);
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001211 ctx->mode = mode;
1212 return;
1213 case CM_ECBUS_S:
1214 sii8620_emsc_enable(ctx);
1215 sii8620_write_seq_static(ctx,
1216 REG_TTXSPINUMS, 4,
1217 REG_TRXSPINUMS, 4,
1218 REG_TTXHSICNUMS, 0x14,
1219 REG_TRXHSICNUMS, 0x14,
1220 REG_TTXTOTNUMS, 0x18,
1221 REG_TRXTOTNUMS, 0x18,
1222 REG_PWD_SRST, BIT_PWD_SRST_COC_DOC_RST
1223 | BIT_PWD_SRST_CBUS_RST_SW_EN,
1224 REG_MHL_COC_CTL1, 0xbd,
1225 REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN,
1226 REG_COC_CTLB, 0x01,
1227 REG_COC_CTL0, 0x5c,
1228 REG_COC_CTL14, 0x03,
1229 REG_COC_CTL15, 0x80,
1230 REG_MHL_DP_CTL6, BIT_MHL_DP_CTL6_DP_TAP1_SGN
1231 | BIT_MHL_DP_CTL6_DP_TAP1_EN
1232 | BIT_MHL_DP_CTL6_DT_PREDRV_FEEDCAP_EN,
1233 REG_MHL_DP_CTL8, 0x03
1234 );
1235 ret = sii8620_wait_for_fsm_state(ctx, 0x03);
1236 sii8620_write_seq_static(ctx,
1237 REG_COC_CTL14, 0x00,
1238 REG_COC_CTL15, 0x80
1239 );
1240 if (!ret)
1241 sii8620_write(ctx, REG_CBUS3_CNVT, 0x85);
1242 else
1243 sii8620_disconnect(ctx);
Andrzej Hajdadd123122017-02-01 08:47:28 +01001244 return;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001245 case CM_DISCONNECTED:
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001246 ctx->mode = mode;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001247 break;
1248 default:
1249 dev_err(ctx->dev, "%s mode %d not supported\n", __func__, mode);
1250 break;
kbuild test robot3a81e962016-10-27 00:58:36 +08001251 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001252
1253 sii8620_set_auto_zone(ctx);
1254
1255 if (mode != CM_MHL1)
1256 return;
1257
1258 sii8620_write_seq_static(ctx,
1259 REG_MHL_DP_CTL0, 0xBC,
1260 REG_MHL_DP_CTL1, 0xBB,
1261 REG_MHL_DP_CTL3, 0x48,
1262 REG_MHL_DP_CTL5, 0x39,
1263 REG_MHL_DP_CTL2, 0x2A,
1264 REG_MHL_DP_CTL6, 0x2A,
1265 REG_MHL_DP_CTL7, 0x08
1266 );
1267}
1268
1269static void sii8620_disconnect(struct sii8620 *ctx)
1270{
1271 sii8620_disable_gen2_write_burst(ctx);
1272 sii8620_stop_video(ctx);
1273 msleep(50);
1274 sii8620_cbus_reset(ctx);
1275 sii8620_set_mode(ctx, CM_DISCONNECTED);
1276 sii8620_write_seq_static(ctx,
1277 REG_COC_CTL0, 0x40,
1278 REG_CBUS3_CNVT, 0x84,
1279 REG_COC_CTL14, 0x00,
1280 REG_COC_CTL0, 0x40,
1281 REG_HRXCTRL3, 0x07,
1282 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1283 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1284 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
1285 REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
1286 | BIT_MHL_DP_CTL0_TX_OE_OVR,
1287 REG_MHL_DP_CTL1, 0xBB,
1288 REG_MHL_DP_CTL3, 0x48,
1289 REG_MHL_DP_CTL5, 0x3F,
1290 REG_MHL_DP_CTL2, 0x2F,
1291 REG_MHL_DP_CTL6, 0x2A,
1292 REG_MHL_DP_CTL7, 0x03
1293 );
1294 sii8620_disable_hpd(ctx);
1295 sii8620_write_seq_static(ctx,
1296 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
1297 REG_MHL_COC_CTL1, 0x07,
1298 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1299 REG_DISC_CTRL8, 0x00,
1300 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1301 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1302 REG_INT_CTRL, 0x00,
1303 REG_MSC_HEARTBEAT_CTRL, 0x27,
1304 REG_DISC_CTRL1, 0x25,
1305 REG_CBUS_DISC_INTR0, (u8)~BIT_RGND_READY_INT,
1306 REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT,
1307 REG_MDT_INT_1, 0xff,
1308 REG_MDT_INT_1_MASK, 0x00,
1309 REG_MDT_INT_0, 0xff,
1310 REG_MDT_INT_0_MASK, 0x00,
1311 REG_COC_INTR, 0xff,
1312 REG_COC_INTR_MASK, 0x00,
1313 REG_TRXINTH, 0xff,
1314 REG_TRXINTMH, 0x00,
1315 REG_CBUS_INT_0, 0xff,
1316 REG_CBUS_INT_0_MASK, 0x00,
1317 REG_CBUS_INT_1, 0xff,
1318 REG_CBUS_INT_1_MASK, 0x00,
1319 REG_EMSCINTR, 0xff,
1320 REG_EMSCINTRMASK, 0x00,
1321 REG_EMSCINTR1, 0xff,
1322 REG_EMSCINTRMASK1, 0x00,
1323 REG_INTR8, 0xff,
1324 REG_INTR8_MASK, 0x00,
1325 REG_TPI_INTR_ST0, 0xff,
1326 REG_TPI_INTR_EN, 0x00,
1327 REG_HDCP2X_INTR0, 0xff,
1328 REG_HDCP2X_INTR0_MASK, 0x00,
1329 REG_INTR9, 0xff,
1330 REG_INTR9_MASK, 0x00,
1331 REG_INTR3, 0xff,
1332 REG_INTR3_MASK, 0x00,
1333 REG_INTR5, 0xff,
1334 REG_INTR5_MASK, 0x00,
1335 REG_INTR2, 0xff,
1336 REG_INTR2_MASK, 0x00,
1337 );
1338 memset(ctx->stat, 0, sizeof(ctx->stat));
1339 memset(ctx->xstat, 0, sizeof(ctx->xstat));
1340 memset(ctx->devcap, 0, sizeof(ctx->devcap));
1341 memset(ctx->xdevcap, 0, sizeof(ctx->xdevcap));
1342 ctx->cbus_status = 0;
1343 ctx->sink_type = SINK_NONE;
1344 kfree(ctx->edid);
1345 ctx->edid = NULL;
1346 sii8620_mt_cleanup(ctx);
1347}
1348
1349static void sii8620_mhl_disconnected(struct sii8620 *ctx)
1350{
1351 sii8620_write_seq_static(ctx,
1352 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1353 REG_CBUS_MSC_COMPAT_CTRL,
1354 BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN
1355 );
1356 sii8620_disconnect(ctx);
1357}
1358
1359static void sii8620_irq_disc(struct sii8620 *ctx)
1360{
1361 u8 stat = sii8620_readb(ctx, REG_CBUS_DISC_INTR0);
1362
1363 if (stat & VAL_CBUS_MHL_DISCON)
1364 sii8620_mhl_disconnected(ctx);
1365
1366 if (stat & BIT_RGND_READY_INT) {
1367 u8 stat2 = sii8620_readb(ctx, REG_DISC_STAT2);
1368
1369 if ((stat2 & MSK_DISC_STAT2_RGND) == VAL_RGND_1K) {
1370 sii8620_mhl_discover(ctx);
1371 } else {
1372 sii8620_write_seq_static(ctx,
1373 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1374 | BIT_DISC_CTRL9_NOMHL_EST
1375 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1376 REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT
1377 | BIT_CBUS_MHL3_DISCON_INT
1378 | BIT_CBUS_MHL12_DISCON_INT
1379 | BIT_NOT_MHL_EST_INT
1380 );
1381 }
1382 }
1383 if (stat & BIT_MHL_EST_INT)
1384 sii8620_mhl_init(ctx);
1385
1386 sii8620_write(ctx, REG_CBUS_DISC_INTR0, stat);
1387}
1388
1389static void sii8620_irq_g2wb(struct sii8620 *ctx)
1390{
1391 u8 stat = sii8620_readb(ctx, REG_MDT_INT_0);
1392
1393 if (stat & BIT_MDT_IDLE_AFTER_HAWB_DISABLE)
1394 dev_dbg(ctx->dev, "HAWB idle\n");
1395
1396 sii8620_write(ctx, REG_MDT_INT_0, stat);
1397}
1398
1399static void sii8620_status_changed_dcap(struct sii8620 *ctx)
1400{
1401 if (ctx->stat[MHL_DST_CONNECTED_RDY] & MHL_DST_CONN_DCAP_RDY) {
1402 sii8620_set_mode(ctx, CM_MHL1);
1403 sii8620_peer_specific_init(ctx);
1404 sii8620_write(ctx, REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE
1405 | BIT_INTR9_EDID_DONE | BIT_INTR9_EDID_ERROR);
1406 }
1407}
1408
1409static void sii8620_status_changed_path(struct sii8620 *ctx)
1410{
1411 if (ctx->stat[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED) {
1412 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1413 MHL_DST_LM_CLK_MODE_NORMAL
1414 | MHL_DST_LM_PATH_ENABLED);
Andrzej Hajdae3a65482017-02-01 08:47:36 +01001415 if (!sii8620_is_mhl3(ctx))
1416 sii8620_mt_read_devcap(ctx, false);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001417 } else {
1418 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1419 MHL_DST_LM_CLK_MODE_NORMAL);
1420 }
1421}
1422
1423static void sii8620_msc_mr_write_stat(struct sii8620 *ctx)
1424{
1425 u8 st[MHL_DST_SIZE], xst[MHL_XDS_SIZE];
1426
1427 sii8620_read_buf(ctx, REG_MHL_STAT_0, st, MHL_DST_SIZE);
1428 sii8620_read_buf(ctx, REG_MHL_EXTSTAT_0, xst, MHL_XDS_SIZE);
1429
1430 sii8620_update_array(ctx->stat, st, MHL_DST_SIZE);
1431 sii8620_update_array(ctx->xstat, xst, MHL_XDS_SIZE);
1432
1433 if (st[MHL_DST_CONNECTED_RDY] & MHL_DST_CONN_DCAP_RDY)
1434 sii8620_status_changed_dcap(ctx);
1435
1436 if (st[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED)
1437 sii8620_status_changed_path(ctx);
1438}
1439
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001440static void sii8620_ecbus_up(struct sii8620 *ctx, int ret)
1441{
1442 if (ret < 0)
1443 return;
1444
1445 sii8620_set_mode(ctx, CM_ECBUS_S);
1446}
1447
1448static void sii8620_got_ecbus_speed(struct sii8620 *ctx, int ret)
1449{
1450 if (ret < 0)
1451 return;
1452
1453 sii8620_mt_write_stat(ctx, MHL_XDS_REG(CURR_ECBUS_MODE),
1454 MHL_XDS_ECBUS_S | MHL_XDS_SLOT_MODE_8BIT);
1455 sii8620_mt_rap(ctx, MHL_RAP_CBUS_MODE_UP);
1456 sii8620_mt_set_cont(ctx, sii8620_ecbus_up);
1457}
1458
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001459static void sii8620_msc_mr_set_int(struct sii8620 *ctx)
1460{
1461 u8 ints[MHL_INT_SIZE];
1462
1463 sii8620_read_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
1464 sii8620_write_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001465
1466 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_DCAP_CHG) {
1467 switch (ctx->mode) {
1468 case CM_MHL3:
1469 sii8620_mt_read_xdevcap_reg(ctx, MHL_XDC_ECBUS_SPEEDS);
1470 sii8620_mt_set_cont(ctx, sii8620_got_ecbus_speed);
1471 break;
1472 case CM_ECBUS_S:
1473 sii8620_mt_read_devcap(ctx, true);
1474 break;
1475 default:
1476 break;
1477 }
1478 }
Andrzej Hajda4a368882017-02-01 08:47:35 +01001479 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_REQ) {
1480 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
1481 MHL_INT_RC_FEAT_COMPLETE);
1482 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001483}
1484
1485static struct sii8620_mt_msg *sii8620_msc_msg_first(struct sii8620 *ctx)
1486{
1487 struct device *dev = ctx->dev;
1488
1489 if (list_empty(&ctx->mt_queue)) {
1490 dev_err(dev, "unexpected MSC MT response\n");
1491 return NULL;
1492 }
1493
1494 return list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
1495}
1496
1497static void sii8620_msc_mt_done(struct sii8620 *ctx)
1498{
1499 struct sii8620_mt_msg *msg = sii8620_msc_msg_first(ctx);
1500
1501 if (!msg)
1502 return;
1503
1504 msg->ret = sii8620_readb(ctx, REG_MSC_MT_RCVD_DATA0);
1505 ctx->mt_state = MT_STATE_DONE;
1506}
1507
1508static void sii8620_msc_mr_msc_msg(struct sii8620 *ctx)
1509{
1510 struct sii8620_mt_msg *msg = sii8620_msc_msg_first(ctx);
1511 u8 buf[2];
1512
1513 if (!msg)
1514 return;
1515
1516 sii8620_read_buf(ctx, REG_MSC_MR_MSC_MSG_RCVD_1ST_DATA, buf, 2);
1517
1518 switch (buf[0]) {
1519 case MHL_MSC_MSG_RAPK:
1520 msg->ret = buf[1];
1521 ctx->mt_state = MT_STATE_DONE;
1522 break;
1523 default:
1524 dev_err(ctx->dev, "%s message type %d,%d not supported",
1525 __func__, buf[0], buf[1]);
1526 }
1527}
1528
1529static void sii8620_irq_msc(struct sii8620 *ctx)
1530{
1531 u8 stat = sii8620_readb(ctx, REG_CBUS_INT_0);
1532
1533 if (stat & ~BIT_CBUS_HPD_CHG)
1534 sii8620_write(ctx, REG_CBUS_INT_0, stat & ~BIT_CBUS_HPD_CHG);
1535
1536 if (stat & BIT_CBUS_HPD_CHG) {
1537 u8 cbus_stat = sii8620_readb(ctx, REG_CBUS_STATUS);
1538
1539 if ((cbus_stat ^ ctx->cbus_status) & BIT_CBUS_STATUS_CBUS_HPD) {
1540 sii8620_write(ctx, REG_CBUS_INT_0, BIT_CBUS_HPD_CHG);
1541 } else {
1542 stat ^= BIT_CBUS_STATUS_CBUS_HPD;
1543 cbus_stat ^= BIT_CBUS_STATUS_CBUS_HPD;
1544 }
1545 ctx->cbus_status = cbus_stat;
1546 }
1547
1548 if (stat & BIT_CBUS_MSC_MR_WRITE_STAT)
1549 sii8620_msc_mr_write_stat(ctx);
1550
1551 if (stat & BIT_CBUS_MSC_MR_SET_INT)
1552 sii8620_msc_mr_set_int(ctx);
1553
1554 if (stat & BIT_CBUS_MSC_MT_DONE)
1555 sii8620_msc_mt_done(ctx);
1556
1557 if (stat & BIT_CBUS_MSC_MR_MSC_MSG)
1558 sii8620_msc_mr_msc_msg(ctx);
1559}
1560
1561static void sii8620_irq_coc(struct sii8620 *ctx)
1562{
1563 u8 stat = sii8620_readb(ctx, REG_COC_INTR);
1564
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001565 if (stat & BIT_COC_CALIBRATION_DONE) {
1566 u8 cstat = sii8620_readb(ctx, REG_COC_STAT_0);
1567
1568 cstat &= BIT_COC_STAT_0_PLL_LOCKED | MSK_COC_STAT_0_FSM_STATE;
1569 if (cstat == (BIT_COC_STAT_0_PLL_LOCKED | 0x02)) {
1570 sii8620_write_seq_static(ctx,
1571 REG_COC_CTLB, 0,
1572 REG_TRXINTMH, BIT_TDM_INTR_SYNC_DATA
1573 | BIT_TDM_INTR_SYNC_WAIT
1574 );
1575 }
1576 }
1577
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001578 sii8620_write(ctx, REG_COC_INTR, stat);
1579}
1580
1581static void sii8620_irq_merr(struct sii8620 *ctx)
1582{
1583 u8 stat = sii8620_readb(ctx, REG_CBUS_INT_1);
1584
1585 sii8620_write(ctx, REG_CBUS_INT_1, stat);
1586}
1587
1588static void sii8620_irq_edid(struct sii8620 *ctx)
1589{
1590 u8 stat = sii8620_readb(ctx, REG_INTR9);
1591
1592 sii8620_write(ctx, REG_INTR9, stat);
1593
1594 if (stat & BIT_INTR9_DEVCAP_DONE)
1595 ctx->mt_state = MT_STATE_DONE;
1596}
1597
1598static void sii8620_scdt_high(struct sii8620 *ctx)
1599{
1600 sii8620_write_seq_static(ctx,
1601 REG_INTR8_MASK, BIT_CEA_NEW_AVI | BIT_CEA_NEW_VSI,
1602 REG_TPI_SC, BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI,
1603 );
1604}
1605
1606static void sii8620_scdt_low(struct sii8620 *ctx)
1607{
1608 sii8620_write(ctx, REG_TMDS_CSTAT_P3,
1609 BIT_TMDS_CSTAT_P3_SCDT_CLR_AVI_DIS |
1610 BIT_TMDS_CSTAT_P3_CLR_AVI);
1611
1612 sii8620_stop_video(ctx);
1613
1614 sii8620_write(ctx, REG_INTR8_MASK, 0);
1615}
1616
1617static void sii8620_irq_scdt(struct sii8620 *ctx)
1618{
1619 u8 stat = sii8620_readb(ctx, REG_INTR5);
1620
1621 if (stat & BIT_INTR_SCDT_CHANGE) {
1622 u8 cstat = sii8620_readb(ctx, REG_TMDS_CSTAT_P3);
1623
1624 if (cstat & BIT_TMDS_CSTAT_P3_SCDT)
1625 sii8620_scdt_high(ctx);
1626 else
1627 sii8620_scdt_low(ctx);
1628 }
1629
1630 sii8620_write(ctx, REG_INTR5, stat);
1631}
1632
1633static void sii8620_new_vsi(struct sii8620 *ctx)
1634{
1635 u8 vsif[11];
1636
1637 sii8620_write(ctx, REG_RX_HDMI_CTRL2,
1638 VAL_RX_HDMI_CTRL2_DEFVAL |
1639 BIT_RX_HDMI_CTRL2_VSI_MON_SEL_VSI);
1640 sii8620_read_buf(ctx, REG_RX_HDMI_MON_PKT_HEADER1, vsif,
1641 ARRAY_SIZE(vsif));
1642}
1643
1644static void sii8620_new_avi(struct sii8620 *ctx)
1645{
1646 sii8620_write(ctx, REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL);
1647 sii8620_read_buf(ctx, REG_RX_HDMI_MON_PKT_HEADER1, ctx->avif,
1648 ARRAY_SIZE(ctx->avif));
1649}
1650
1651static void sii8620_irq_infr(struct sii8620 *ctx)
1652{
1653 u8 stat = sii8620_readb(ctx, REG_INTR8)
1654 & (BIT_CEA_NEW_VSI | BIT_CEA_NEW_AVI);
1655
1656 sii8620_write(ctx, REG_INTR8, stat);
1657
1658 if (stat & BIT_CEA_NEW_VSI)
1659 sii8620_new_vsi(ctx);
1660
1661 if (stat & BIT_CEA_NEW_AVI)
1662 sii8620_new_avi(ctx);
1663
1664 if (stat & (BIT_CEA_NEW_VSI | BIT_CEA_NEW_AVI))
1665 sii8620_start_video(ctx);
1666}
1667
Andrzej Hajdae3a65482017-02-01 08:47:36 +01001668static void sii8620_got_xdevcap(struct sii8620 *ctx, int ret)
1669{
1670 if (ret < 0)
1671 return;
1672
1673 sii8620_mt_read_devcap(ctx, false);
1674}
1675
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001676static void sii8620_irq_tdm(struct sii8620 *ctx)
1677{
1678 u8 stat = sii8620_readb(ctx, REG_TRXINTH);
1679 u8 tdm = sii8620_readb(ctx, REG_TRXSTA2);
1680
1681 if ((tdm & MSK_TDM_SYNCHRONIZED) == VAL_TDM_SYNCHRONIZED) {
1682 ctx->mode = CM_ECBUS_S;
1683 ctx->burst.rx_ack = 0;
1684 ctx->burst.r_size = SII8620_BURST_BUF_LEN;
1685 sii8620_burst_tx_rbuf_info(ctx, SII8620_BURST_BUF_LEN);
1686 sii8620_mt_read_devcap(ctx, true);
Andrzej Hajdae3a65482017-02-01 08:47:36 +01001687 sii8620_mt_set_cont(ctx, sii8620_got_xdevcap);
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001688 } else {
1689 sii8620_write_seq_static(ctx,
1690 REG_MHL_PLL_CTL2, 0,
1691 REG_MHL_PLL_CTL2, BIT_MHL_PLL_CTL2_CLKDETECT_EN
1692 );
1693 }
1694
1695 sii8620_write(ctx, REG_TRXINTH, stat);
1696}
1697
1698static void sii8620_irq_block(struct sii8620 *ctx)
1699{
1700 u8 stat = sii8620_readb(ctx, REG_EMSCINTR);
1701
1702 if (stat & BIT_EMSCINTR_SPI_DVLD) {
1703 u8 bstat = sii8620_readb(ctx, REG_SPIBURSTSTAT);
1704
1705 if (bstat & BIT_SPIBURSTSTAT_EMSC_NORMAL_MODE)
1706 sii8620_burst_receive(ctx);
1707 }
1708
1709 sii8620_write(ctx, REG_EMSCINTR, stat);
1710}
1711
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001712/* endian agnostic, non-volatile version of test_bit */
1713static bool sii8620_test_bit(unsigned int nr, const u8 *addr)
1714{
1715 return 1 & (addr[nr / BITS_PER_BYTE] >> (nr % BITS_PER_BYTE));
1716}
1717
1718static irqreturn_t sii8620_irq_thread(int irq, void *data)
1719{
1720 static const struct {
1721 int bit;
1722 void (*handler)(struct sii8620 *ctx);
1723 } irq_vec[] = {
1724 { BIT_FAST_INTR_STAT_DISC, sii8620_irq_disc },
1725 { BIT_FAST_INTR_STAT_G2WB, sii8620_irq_g2wb },
1726 { BIT_FAST_INTR_STAT_COC, sii8620_irq_coc },
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001727 { BIT_FAST_INTR_STAT_TDM, sii8620_irq_tdm },
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001728 { BIT_FAST_INTR_STAT_MSC, sii8620_irq_msc },
1729 { BIT_FAST_INTR_STAT_MERR, sii8620_irq_merr },
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001730 { BIT_FAST_INTR_STAT_BLOCK, sii8620_irq_block },
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001731 { BIT_FAST_INTR_STAT_EDID, sii8620_irq_edid },
1732 { BIT_FAST_INTR_STAT_SCDT, sii8620_irq_scdt },
1733 { BIT_FAST_INTR_STAT_INFR, sii8620_irq_infr },
1734 };
1735 struct sii8620 *ctx = data;
1736 u8 stats[LEN_FAST_INTR_STAT];
1737 int i, ret;
1738
1739 mutex_lock(&ctx->lock);
1740
1741 sii8620_read_buf(ctx, REG_FAST_INTR_STAT, stats, ARRAY_SIZE(stats));
1742 for (i = 0; i < ARRAY_SIZE(irq_vec); ++i)
1743 if (sii8620_test_bit(irq_vec[i].bit, stats))
1744 irq_vec[i].handler(ctx);
1745
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001746 sii8620_burst_rx_all(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001747 sii8620_mt_work(ctx);
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001748 sii8620_burst_send(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001749
1750 ret = sii8620_clear_error(ctx);
1751 if (ret) {
1752 dev_err(ctx->dev, "Error during IRQ handling, %d.\n", ret);
1753 sii8620_mhl_disconnected(ctx);
1754 }
1755 mutex_unlock(&ctx->lock);
1756
1757 return IRQ_HANDLED;
1758}
1759
1760static void sii8620_cable_in(struct sii8620 *ctx)
1761{
1762 struct device *dev = ctx->dev;
1763 u8 ver[5];
1764 int ret;
1765
1766 ret = sii8620_hw_on(ctx);
1767 if (ret) {
1768 dev_err(dev, "Error powering on, %d.\n", ret);
1769 return;
1770 }
1771 sii8620_hw_reset(ctx);
1772
1773 sii8620_read_buf(ctx, REG_VND_IDL, ver, ARRAY_SIZE(ver));
1774 ret = sii8620_clear_error(ctx);
1775 if (ret) {
1776 dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
1777 return;
1778 }
1779
1780 dev_info(dev, "ChipID %02x%02x:%02x%02x rev %02x.\n", ver[1], ver[0],
1781 ver[3], ver[2], ver[4]);
1782
1783 sii8620_write(ctx, REG_DPD,
1784 BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN);
1785
1786 sii8620_xtal_set_rate(ctx);
1787 sii8620_disconnect(ctx);
1788
1789 sii8620_write_seq_static(ctx,
1790 REG_MHL_CBUS_CTL0, VAL_MHL_CBUS_CTL0_CBUS_DRV_SEL_STRONG
1791 | VAL_MHL_CBUS_CTL0_CBUS_RGND_VBIAS_734,
1792 REG_MHL_CBUS_CTL1, VAL_MHL_CBUS_CTL1_1115_OHM,
1793 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN,
1794 );
1795
1796 ret = sii8620_clear_error(ctx);
1797 if (ret) {
1798 dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
1799 return;
1800 }
1801
1802 enable_irq(to_i2c_client(ctx->dev)->irq);
1803}
1804
1805static inline struct sii8620 *bridge_to_sii8620(struct drm_bridge *bridge)
1806{
1807 return container_of(bridge, struct sii8620, bridge);
1808}
1809
1810static bool sii8620_mode_fixup(struct drm_bridge *bridge,
1811 const struct drm_display_mode *mode,
1812 struct drm_display_mode *adjusted_mode)
1813{
1814 struct sii8620 *ctx = bridge_to_sii8620(bridge);
1815 bool ret = false;
1816 int max_clock = 74250;
1817
1818 mutex_lock(&ctx->lock);
1819
1820 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1821 goto out;
1822
1823 if (ctx->devcap[MHL_DCAP_VID_LINK_MODE] & MHL_DCAP_VID_LINK_PPIXEL)
1824 max_clock = 300000;
1825
1826 ret = mode->clock <= max_clock;
1827
1828out:
1829 mutex_unlock(&ctx->lock);
1830
1831 return ret;
1832}
1833
1834static const struct drm_bridge_funcs sii8620_bridge_funcs = {
1835 .mode_fixup = sii8620_mode_fixup,
1836};
1837
1838static int sii8620_probe(struct i2c_client *client,
1839 const struct i2c_device_id *id)
1840{
1841 struct device *dev = &client->dev;
1842 struct sii8620 *ctx;
1843 int ret;
1844
1845 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1846 if (!ctx)
1847 return -ENOMEM;
1848
1849 ctx->dev = dev;
1850 mutex_init(&ctx->lock);
1851 INIT_LIST_HEAD(&ctx->mt_queue);
1852
1853 ctx->clk_xtal = devm_clk_get(dev, "xtal");
1854 if (IS_ERR(ctx->clk_xtal)) {
1855 dev_err(dev, "failed to get xtal clock from DT\n");
1856 return PTR_ERR(ctx->clk_xtal);
1857 }
1858
1859 if (!client->irq) {
1860 dev_err(dev, "no irq provided\n");
1861 return -EINVAL;
1862 }
1863 irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
1864 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1865 sii8620_irq_thread,
1866 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1867 "sii8620", ctx);
1868
1869 ctx->gpio_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1870 if (IS_ERR(ctx->gpio_reset)) {
1871 dev_err(dev, "failed to get reset gpio from DT\n");
1872 return PTR_ERR(ctx->gpio_reset);
1873 }
1874
1875 ctx->supplies[0].supply = "cvcc10";
1876 ctx->supplies[1].supply = "iovcc18";
1877 ret = devm_regulator_bulk_get(dev, 2, ctx->supplies);
1878 if (ret)
1879 return ret;
1880
1881 i2c_set_clientdata(client, ctx);
1882
1883 ctx->bridge.funcs = &sii8620_bridge_funcs;
1884 ctx->bridge.of_node = dev->of_node;
1885 drm_bridge_add(&ctx->bridge);
1886
1887 sii8620_cable_in(ctx);
1888
1889 return 0;
1890}
1891
1892static int sii8620_remove(struct i2c_client *client)
1893{
1894 struct sii8620 *ctx = i2c_get_clientdata(client);
1895
1896 disable_irq(to_i2c_client(ctx->dev)->irq);
1897 drm_bridge_remove(&ctx->bridge);
1898 sii8620_hw_off(ctx);
1899
1900 return 0;
1901}
1902
1903static const struct of_device_id sii8620_dt_match[] = {
1904 { .compatible = "sil,sii8620" },
1905 { },
1906};
1907MODULE_DEVICE_TABLE(of, sii8620_dt_match);
1908
1909static const struct i2c_device_id sii8620_id[] = {
1910 { "sii8620", 0 },
1911 { },
1912};
1913
1914MODULE_DEVICE_TABLE(i2c, sii8620_id);
1915static struct i2c_driver sii8620_driver = {
1916 .driver = {
1917 .name = "sii8620",
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001918 .of_match_table = of_match_ptr(sii8620_dt_match),
1919 },
1920 .probe = sii8620_probe,
1921 .remove = sii8620_remove,
1922 .id_table = sii8620_id,
1923};
1924
1925module_i2c_driver(sii8620_driver);
1926MODULE_LICENSE("GPL v2");