2 * Silicon Image SiI8620 HDMI/MHL bridge driver
4 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
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.
12 #include <asm/unaligned.h>
14 #include <drm/bridge/mhl.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_edid.h>
18 #include <linux/clk.h>
19 #include <linux/delay.h>
20 #include <linux/extcon.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/of_graph.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
33 #include <media/rc-core.h>
35 #include "sil-sii8620.h"
37 #define SII8620_BURST_BUF_LEN 288
38 #define VAL_RX_HDMI_CTRL2_DEFVAL VAL_RX_HDMI_CTRL2_IDLE_CNT(3)
39 #define MHL1_MAX_LCLK 225000
40 #define MHL3_MAX_LCLK 600000
50 enum sii8620_sink_type {
56 enum sii8620_mt_state {
63 struct drm_bridge bridge;
65 struct rc_dev *rc_dev;
67 struct gpio_desc *gpio_reset;
68 struct gpio_desc *gpio_int;
69 struct regulator_bulk_data supplies[2];
70 struct mutex lock; /* context lock, protects fields below */
73 unsigned int use_packed_pixel:1;
75 enum sii8620_mode mode;
76 enum sii8620_sink_type sink_type;
78 u8 stat[MHL_DST_SIZE];
79 u8 xstat[MHL_XDS_SIZE];
80 u8 devcap[MHL_DCAP_SIZE];
81 u8 xdevcap[MHL_XDC_SIZE];
82 u8 avif[HDMI_INFOFRAME_SIZE(AVI)];
84 unsigned int gen2_write_burst:1;
85 enum sii8620_mt_state mt_state;
86 struct extcon_dev *extcon;
87 struct notifier_block extcon_nb;
88 struct work_struct extcon_wq;
90 struct list_head mt_queue;
102 struct sii8620_mt_msg;
104 typedef void (*sii8620_mt_msg_cb)(struct sii8620 *ctx,
105 struct sii8620_mt_msg *msg);
107 typedef void (*sii8620_cb)(struct sii8620 *ctx, int ret);
109 struct sii8620_mt_msg {
110 struct list_head node;
113 sii8620_mt_msg_cb send;
114 sii8620_mt_msg_cb recv;
115 sii8620_cb continuation;
118 static const u8 sii8620_i2c_page[] = {
119 0x39, /* Main System */
120 0x3d, /* TDM and HSIC */
121 0x49, /* TMDS Receiver, MHL EDID */
122 0x4d, /* eMSC, HDCP, HSIC */
125 0x59, /* Hardware TPI (Transmitter Programming Interface) */
126 0x61, /* eCBUS-S, eCBUS-D */
129 static void sii8620_fetch_edid(struct sii8620 *ctx);
130 static void sii8620_set_upstream_edid(struct sii8620 *ctx);
131 static void sii8620_enable_hpd(struct sii8620 *ctx);
132 static void sii8620_mhl_disconnected(struct sii8620 *ctx);
133 static void sii8620_disconnect(struct sii8620 *ctx);
135 static int sii8620_clear_error(struct sii8620 *ctx)
137 int ret = ctx->error;
143 static void sii8620_read_buf(struct sii8620 *ctx, u16 addr, u8 *buf, int len)
145 struct device *dev = ctx->dev;
146 struct i2c_client *client = to_i2c_client(dev);
148 struct i2c_msg msg[] = {
150 .addr = sii8620_i2c_page[addr >> 8],
151 .flags = client->flags,
156 .addr = sii8620_i2c_page[addr >> 8],
157 .flags = client->flags | I2C_M_RD,
167 ret = i2c_transfer(client->adapter, msg, 2);
168 dev_dbg(dev, "read at %04x: %*ph, %d\n", addr, len, buf, ret);
171 dev_err(dev, "Read at %#06x of %d bytes failed with code %d.\n",
173 ctx->error = ret < 0 ? ret : -EIO;
177 static u8 sii8620_readb(struct sii8620 *ctx, u16 addr)
181 sii8620_read_buf(ctx, addr, &ret, 1);
185 static void sii8620_write_buf(struct sii8620 *ctx, u16 addr, const u8 *buf,
188 struct device *dev = ctx->dev;
189 struct i2c_client *client = to_i2c_client(dev);
191 struct i2c_msg msg = {
192 .addr = sii8620_i2c_page[addr >> 8],
193 .flags = client->flags,
202 msg.buf = kmalloc(len + 1, GFP_KERNEL);
204 ctx->error = -ENOMEM;
207 memcpy(msg.buf + 1, buf, len);
215 ret = i2c_transfer(client->adapter, &msg, 1);
216 dev_dbg(dev, "write at %04x: %*ph, %d\n", addr, len, buf, ret);
219 dev_err(dev, "Write at %#06x of %*ph failed with code %d.\n",
220 addr, len, buf, ret);
221 ctx->error = ret ?: -EIO;
228 #define sii8620_write(ctx, addr, arr...) \
231 sii8620_write_buf(ctx, addr, d, ARRAY_SIZE(d)); \
234 static void __sii8620_write_seq(struct sii8620 *ctx, const u16 *seq, int len)
238 for (i = 0; i < len; i += 2)
239 sii8620_write(ctx, seq[i], seq[i + 1]);
242 #define sii8620_write_seq(ctx, seq...) \
244 const u16 d[] = { seq }; \
245 __sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
248 #define sii8620_write_seq_static(ctx, seq...) \
250 static const u16 d[] = { seq }; \
251 __sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
254 static void sii8620_setbits(struct sii8620 *ctx, u16 addr, u8 mask, u8 val)
256 val = (val & mask) | (sii8620_readb(ctx, addr) & ~mask);
257 sii8620_write(ctx, addr, val);
260 static inline bool sii8620_is_mhl3(struct sii8620 *ctx)
262 return ctx->mode >= CM_MHL3;
265 static void sii8620_mt_cleanup(struct sii8620 *ctx)
267 struct sii8620_mt_msg *msg, *n;
269 list_for_each_entry_safe(msg, n, &ctx->mt_queue, node) {
270 list_del(&msg->node);
273 ctx->mt_state = MT_STATE_READY;
276 static void sii8620_mt_work(struct sii8620 *ctx)
278 struct sii8620_mt_msg *msg;
282 if (ctx->mt_state == MT_STATE_BUSY || list_empty(&ctx->mt_queue))
285 if (ctx->mt_state == MT_STATE_DONE) {
286 ctx->mt_state = MT_STATE_READY;
287 msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg,
289 list_del(&msg->node);
292 if (msg->continuation)
293 msg->continuation(ctx, msg->ret);
297 if (ctx->mt_state != MT_STATE_READY || list_empty(&ctx->mt_queue))
300 ctx->mt_state = MT_STATE_BUSY;
301 msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
306 static void sii8620_enable_gen2_write_burst(struct sii8620 *ctx)
308 u8 ctrl = BIT_MDT_RCV_CTRL_MDT_RCV_EN;
310 if (ctx->gen2_write_burst)
313 if (ctx->mode >= CM_MHL1)
314 ctrl |= BIT_MDT_RCV_CTRL_MDT_DELAY_RCV_EN;
316 sii8620_write_seq(ctx,
317 REG_MDT_RCV_TIMEOUT, 100,
318 REG_MDT_RCV_CTRL, ctrl
320 ctx->gen2_write_burst = 1;
323 static void sii8620_disable_gen2_write_burst(struct sii8620 *ctx)
325 if (!ctx->gen2_write_burst)
328 sii8620_write_seq_static(ctx,
329 REG_MDT_XMIT_CTRL, 0,
332 ctx->gen2_write_burst = 0;
335 static void sii8620_start_gen2_write_burst(struct sii8620 *ctx)
337 sii8620_write_seq_static(ctx,
338 REG_MDT_INT_1_MASK, BIT_MDT_RCV_TIMEOUT
339 | BIT_MDT_RCV_SM_ABORT_PKT_RCVD | BIT_MDT_RCV_SM_ERROR
340 | BIT_MDT_XMIT_TIMEOUT | BIT_MDT_XMIT_SM_ABORT_PKT_RCVD
341 | BIT_MDT_XMIT_SM_ERROR,
342 REG_MDT_INT_0_MASK, BIT_MDT_XFIFO_EMPTY
343 | BIT_MDT_IDLE_AFTER_HAWB_DISABLE
344 | BIT_MDT_RFIFO_DATA_RDY
346 sii8620_enable_gen2_write_burst(ctx);
349 static void sii8620_mt_msc_cmd_send(struct sii8620 *ctx,
350 struct sii8620_mt_msg *msg)
352 if (msg->reg[0] == MHL_SET_INT &&
353 msg->reg[1] == MHL_INT_REG(RCHANGE) &&
354 msg->reg[2] == MHL_INT_RC_FEAT_REQ)
355 sii8620_enable_gen2_write_burst(ctx);
357 sii8620_disable_gen2_write_burst(ctx);
359 switch (msg->reg[0]) {
362 sii8620_write_buf(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg + 1, 2);
363 sii8620_write(ctx, REG_MSC_COMMAND_START,
364 BIT_MSC_COMMAND_START_WRITE_STAT);
367 sii8620_write_buf(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg, 3);
368 sii8620_write(ctx, REG_MSC_COMMAND_START,
369 BIT_MSC_COMMAND_START_MSC_MSG);
371 case MHL_READ_DEVCAP_REG:
372 case MHL_READ_XDEVCAP_REG:
373 sii8620_write(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg[1]);
374 sii8620_write(ctx, REG_MSC_COMMAND_START,
375 BIT_MSC_COMMAND_START_READ_DEVCAP);
378 dev_err(ctx->dev, "%s: command %#x not supported\n", __func__,
383 static struct sii8620_mt_msg *sii8620_mt_msg_new(struct sii8620 *ctx)
385 struct sii8620_mt_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
388 ctx->error = -ENOMEM;
390 list_add_tail(&msg->node, &ctx->mt_queue);
395 static void sii8620_mt_set_cont(struct sii8620 *ctx, sii8620_cb cont)
397 struct sii8620_mt_msg *msg;
402 if (list_empty(&ctx->mt_queue)) {
403 ctx->error = -EINVAL;
406 msg = list_last_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
407 msg->continuation = cont;
410 static void sii8620_mt_msc_cmd(struct sii8620 *ctx, u8 cmd, u8 arg1, u8 arg2)
412 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
420 msg->send = sii8620_mt_msc_cmd_send;
423 static void sii8620_mt_write_stat(struct sii8620 *ctx, u8 reg, u8 val)
425 sii8620_mt_msc_cmd(ctx, MHL_WRITE_STAT, reg, val);
428 static inline void sii8620_mt_set_int(struct sii8620 *ctx, u8 irq, u8 mask)
430 sii8620_mt_msc_cmd(ctx, MHL_SET_INT, irq, mask);
433 static void sii8620_mt_msc_msg(struct sii8620 *ctx, u8 cmd, u8 data)
435 sii8620_mt_msc_cmd(ctx, MHL_MSC_MSG, cmd, data);
438 static void sii8620_mt_rap(struct sii8620 *ctx, u8 code)
440 sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RAP, code);
443 static void sii8620_mt_rcpk(struct sii8620 *ctx, u8 code)
445 sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RCPK, code);
448 static void sii8620_mt_rcpe(struct sii8620 *ctx, u8 code)
450 sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RCPE, code);
453 static void sii8620_mt_read_devcap_send(struct sii8620 *ctx,
454 struct sii8620_mt_msg *msg)
456 u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
457 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
458 | BIT_EDID_CTRL_EDID_MODE_EN;
460 if (msg->reg[0] == MHL_READ_XDEVCAP)
461 ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
463 sii8620_write_seq(ctx,
464 REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE,
466 REG_TPI_CBUS_START, BIT_TPI_CBUS_START_GET_DEVCAP_START
470 /* copy src to dst and set changed bits in src */
471 static void sii8620_update_array(u8 *dst, u8 *src, int count)
473 while (--count >= 0) {
479 static void sii8620_sink_detected(struct sii8620 *ctx, int ret)
481 static const char * const sink_str[] = {
482 [SINK_NONE] = "NONE",
483 [SINK_HDMI] = "HDMI",
488 struct device *dev = ctx->dev;
493 sii8620_fetch_edid(ctx);
495 dev_err(ctx->dev, "Cannot fetch EDID\n");
496 sii8620_mhl_disconnected(ctx);
500 if (drm_detect_hdmi_monitor(ctx->edid))
501 ctx->sink_type = SINK_HDMI;
503 ctx->sink_type = SINK_DVI;
505 drm_edid_get_monitor_name(ctx->edid, sink_name, ARRAY_SIZE(sink_name));
507 dev_info(dev, "detected sink(type: %s): %s\n",
508 sink_str[ctx->sink_type], sink_name);
511 static void sii8620_hsic_init(struct sii8620 *ctx)
513 if (!sii8620_is_mhl3(ctx))
516 sii8620_write(ctx, REG_FCGC,
517 BIT_FCGC_HSIC_HOSTMODE | BIT_FCGC_HSIC_ENABLE);
518 sii8620_setbits(ctx, REG_HRXCTRL3,
519 BIT_HRXCTRL3_HRX_STAY_RESET | BIT_HRXCTRL3_STATUS_EN, ~0);
520 sii8620_setbits(ctx, REG_TTXNUMB, MSK_TTXNUMB_TTX_NUMBPS, 4);
521 sii8620_setbits(ctx, REG_TRXCTRL, BIT_TRXCTRL_TRX_FROM_SE_COC, ~0);
522 sii8620_setbits(ctx, REG_HTXCTRL, BIT_HTXCTRL_HTX_DRVCONN1, 0);
523 sii8620_setbits(ctx, REG_KEEPER, MSK_KEEPER_MODE, VAL_KEEPER_MODE_HOST);
524 sii8620_write_seq_static(ctx,
526 REG_UTSRST, BIT_UTSRST_HRX_SRST | BIT_UTSRST_HTX_SRST |
527 BIT_UTSRST_KEEPER_SRST | BIT_UTSRST_FC_SRST,
528 REG_UTSRST, BIT_UTSRST_HRX_SRST | BIT_UTSRST_HTX_SRST,
548 static void sii8620_edid_read(struct sii8620 *ctx, int ret)
553 sii8620_set_upstream_edid(ctx);
554 sii8620_hsic_init(ctx);
555 sii8620_enable_hpd(ctx);
558 static void sii8620_mr_devcap(struct sii8620 *ctx)
560 u8 dcap[MHL_DCAP_SIZE];
561 struct device *dev = ctx->dev;
563 sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, dcap, MHL_DCAP_SIZE);
567 dev_info(dev, "detected dongle MHL %d.%d, ChipID %02x%02x:%02x%02x\n",
568 dcap[MHL_DCAP_MHL_VERSION] / 16,
569 dcap[MHL_DCAP_MHL_VERSION] % 16,
570 dcap[MHL_DCAP_ADOPTER_ID_H], dcap[MHL_DCAP_ADOPTER_ID_L],
571 dcap[MHL_DCAP_DEVICE_ID_H], dcap[MHL_DCAP_DEVICE_ID_L]);
572 sii8620_update_array(ctx->devcap, dcap, MHL_DCAP_SIZE);
575 static void sii8620_mr_xdevcap(struct sii8620 *ctx)
577 sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, ctx->xdevcap,
581 static void sii8620_mt_read_devcap_recv(struct sii8620 *ctx,
582 struct sii8620_mt_msg *msg)
584 u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
585 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
586 | BIT_EDID_CTRL_EDID_MODE_EN;
588 if (msg->reg[0] == MHL_READ_XDEVCAP)
589 ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
591 sii8620_write_seq(ctx,
592 REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE | BIT_INTR9_EDID_DONE
593 | BIT_INTR9_EDID_ERROR,
595 REG_EDID_FIFO_ADDR, 0
598 if (msg->reg[0] == MHL_READ_XDEVCAP)
599 sii8620_mr_xdevcap(ctx);
601 sii8620_mr_devcap(ctx);
604 static void sii8620_mt_read_devcap(struct sii8620 *ctx, bool xdevcap)
606 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
611 msg->reg[0] = xdevcap ? MHL_READ_XDEVCAP : MHL_READ_DEVCAP;
612 msg->send = sii8620_mt_read_devcap_send;
613 msg->recv = sii8620_mt_read_devcap_recv;
616 static void sii8620_mt_read_devcap_reg_recv(struct sii8620 *ctx,
617 struct sii8620_mt_msg *msg)
619 u8 reg = msg->reg[1] & 0x7f;
621 if (msg->reg[1] & 0x80)
622 ctx->xdevcap[reg] = msg->ret;
624 ctx->devcap[reg] = msg->ret;
627 static void sii8620_mt_read_devcap_reg(struct sii8620 *ctx, u8 reg)
629 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
634 msg->reg[0] = (reg & 0x80) ? MHL_READ_XDEVCAP_REG : MHL_READ_DEVCAP_REG;
636 msg->send = sii8620_mt_msc_cmd_send;
637 msg->recv = sii8620_mt_read_devcap_reg_recv;
640 static inline void sii8620_mt_read_xdevcap_reg(struct sii8620 *ctx, u8 reg)
642 sii8620_mt_read_devcap_reg(ctx, reg | 0x80);
645 static void *sii8620_burst_get_tx_buf(struct sii8620 *ctx, int len)
647 u8 *buf = &ctx->burst.tx_buf[ctx->burst.tx_count];
650 if (ctx->burst.tx_count + size > ARRAY_SIZE(ctx->burst.tx_buf)) {
651 dev_err(ctx->dev, "TX-BLK buffer exhausted\n");
652 ctx->error = -EINVAL;
656 ctx->burst.tx_count += size;
662 static u8 *sii8620_burst_get_rx_buf(struct sii8620 *ctx, int len)
664 u8 *buf = &ctx->burst.rx_buf[ctx->burst.rx_count];
667 if (ctx->burst.tx_count + size > ARRAY_SIZE(ctx->burst.tx_buf)) {
668 dev_err(ctx->dev, "RX-BLK buffer exhausted\n");
669 ctx->error = -EINVAL;
673 ctx->burst.rx_count += size;
679 static void sii8620_burst_send(struct sii8620 *ctx)
681 int tx_left = ctx->burst.tx_count;
682 u8 *d = ctx->burst.tx_buf;
684 while (tx_left > 0) {
687 if (ctx->burst.r_count + len > ctx->burst.r_size)
689 d[0] = min(ctx->burst.rx_ack, 255);
690 ctx->burst.rx_ack -= d[0];
691 sii8620_write_buf(ctx, REG_EMSC_XMIT_WRITE_PORT, d, len);
692 ctx->burst.r_count += len;
697 ctx->burst.tx_count = tx_left;
699 while (ctx->burst.rx_ack > 0) {
700 u8 b[2] = { min(ctx->burst.rx_ack, 255), 0 };
702 if (ctx->burst.r_count + 2 > ctx->burst.r_size)
704 ctx->burst.rx_ack -= b[0];
705 sii8620_write_buf(ctx, REG_EMSC_XMIT_WRITE_PORT, b, 2);
706 ctx->burst.r_count += 2;
710 static void sii8620_burst_receive(struct sii8620 *ctx)
715 sii8620_read_buf(ctx, REG_EMSCRFIFOBCNTL, buf, 2);
716 count = get_unaligned_le16(buf);
718 int len = min(count, 3);
720 sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, buf, len);
722 ctx->burst.rx_ack += len - 1;
723 ctx->burst.r_count -= buf[1];
724 if (ctx->burst.r_count < 0)
725 ctx->burst.r_count = 0;
727 if (len < 3 || !buf[2])
731 d = sii8620_burst_get_rx_buf(ctx, len);
734 sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, d, len);
736 ctx->burst.rx_ack += len;
740 static void sii8620_burst_tx_rbuf_info(struct sii8620 *ctx, int size)
742 struct mhl_burst_blk_rcv_buffer_info *d =
743 sii8620_burst_get_tx_buf(ctx, sizeof(*d));
747 d->id = cpu_to_be16(MHL_BURST_ID_BLK_RCV_BUFFER_INFO);
748 d->size = cpu_to_le16(size);
751 static u8 sii8620_checksum(void *ptr, int size)
753 u8 *d = ptr, sum = 0;
761 static void sii8620_mhl_burst_hdr_set(struct mhl3_burst_header *h,
762 enum mhl_burst_id id)
764 h->id = cpu_to_be16(id);
765 h->total_entries = 1;
766 h->sequence_index = 1;
769 static void sii8620_burst_tx_bits_per_pixel_fmt(struct sii8620 *ctx, u8 fmt)
771 struct mhl_burst_bits_per_pixel_fmt *d;
772 const int size = sizeof(*d) + sizeof(d->desc[0]);
774 d = sii8620_burst_get_tx_buf(ctx, size);
778 sii8620_mhl_burst_hdr_set(&d->hdr, MHL_BURST_ID_BITS_PER_PIXEL_FMT);
780 d->desc[0].stream_id = 0;
781 d->desc[0].pixel_format = fmt;
782 d->hdr.checksum -= sii8620_checksum(d, size);
785 static void sii8620_burst_rx_all(struct sii8620 *ctx)
787 u8 *d = ctx->burst.rx_buf;
788 int count = ctx->burst.rx_count;
790 while (count-- > 0) {
792 int id = get_unaligned_be16(&d[0]);
795 case MHL_BURST_ID_BLK_RCV_BUFFER_INFO:
796 ctx->burst.r_size = get_unaligned_le16(&d[2]);
804 ctx->burst.rx_count = 0;
807 static void sii8620_fetch_edid(struct sii8620 *ctx)
809 u8 lm_ddc, ddc_cmd, int3, cbus;
811 int edid_len = EDID_LENGTH;
814 sii8620_readb(ctx, REG_CBUS_STATUS);
815 lm_ddc = sii8620_readb(ctx, REG_LM_DDC);
816 ddc_cmd = sii8620_readb(ctx, REG_DDC_CMD);
818 sii8620_write_seq(ctx,
820 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
821 REG_HDCP2X_POLL_CS, 0x71,
822 REG_HDCP2X_CTRL_0, BIT_HDCP2X_CTRL_0_HDCP2X_HDCPTX,
823 REG_LM_DDC, lm_ddc | BIT_LM_DDC_SW_TPI_EN_DISABLED,
826 for (i = 0; i < 256; ++i) {
827 u8 ddc_stat = sii8620_readb(ctx, REG_DDC_STATUS);
829 if (!(ddc_stat & BIT_DDC_STATUS_DDC_I2C_IN_PROG))
831 sii8620_write(ctx, REG_DDC_STATUS,
832 BIT_DDC_STATUS_DDC_FIFO_EMPTY);
835 sii8620_write(ctx, REG_DDC_ADDR, 0x50 << 1);
837 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
839 ctx->error = -ENOMEM;
843 #define FETCH_SIZE 16
844 for (fetched = 0; fetched < edid_len; fetched += FETCH_SIZE) {
845 sii8620_readb(ctx, REG_DDC_STATUS);
846 sii8620_write_seq(ctx,
847 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_ABORT,
848 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_CLEAR_FIFO,
849 REG_DDC_STATUS, BIT_DDC_STATUS_DDC_FIFO_EMPTY
851 sii8620_write_seq(ctx,
852 REG_DDC_SEGM, fetched >> 8,
853 REG_DDC_OFFSET, fetched & 0xff,
854 REG_DDC_DIN_CNT1, FETCH_SIZE,
856 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_ENH_DDC_READ_NO_ACK
860 int3 = sii8620_readb(ctx, REG_INTR3);
861 cbus = sii8620_readb(ctx, REG_CBUS_STATUS);
863 if (int3 & BIT_DDC_CMD_DONE)
866 if (!(cbus & BIT_CBUS_STATUS_CBUS_CONNECTED)) {
873 sii8620_readb(ctx, REG_DDC_STATUS);
874 while (sii8620_readb(ctx, REG_DDC_DOUT_CNT) < FETCH_SIZE)
875 usleep_range(10, 20);
877 sii8620_read_buf(ctx, REG_DDC_DATA, edid + fetched, FETCH_SIZE);
878 if (fetched + FETCH_SIZE == EDID_LENGTH) {
879 u8 ext = ((struct edid *)edid)->extensions;
884 edid_len += ext * EDID_LENGTH;
885 new_edid = krealloc(edid, edid_len, GFP_KERNEL);
888 ctx->error = -ENOMEM;
896 sii8620_write_seq(ctx,
897 REG_INTR3_MASK, BIT_DDC_CMD_DONE,
903 ctx->edid = (struct edid *)edid;
906 static void sii8620_set_upstream_edid(struct sii8620 *ctx)
908 sii8620_setbits(ctx, REG_DPD, BIT_DPD_PDNRX12 | BIT_DPD_PDIDCK_N
909 | BIT_DPD_PD_MHL_CLK_N, 0xff);
911 sii8620_write_seq_static(ctx,
912 REG_RX_HDMI_CTRL3, 0x00,
913 REG_PKT_FILTER_0, 0xFF,
914 REG_PKT_FILTER_1, 0xFF,
915 REG_ALICE0_BW_I2C, 0x06
918 sii8620_setbits(ctx, REG_RX_HDMI_CLR_BUFFER,
919 BIT_RX_HDMI_CLR_BUFFER_VSI_CLR_EN, 0xff);
921 sii8620_write_seq_static(ctx,
922 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
923 | BIT_EDID_CTRL_EDID_MODE_EN,
924 REG_EDID_FIFO_ADDR, 0,
927 sii8620_write_buf(ctx, REG_EDID_FIFO_WR_DATA, (u8 *)ctx->edid,
928 (ctx->edid->extensions + 1) * EDID_LENGTH);
930 sii8620_write_seq_static(ctx,
931 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID
932 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
933 | BIT_EDID_CTRL_EDID_MODE_EN,
934 REG_INTR5_MASK, BIT_INTR_SCDT_CHANGE,
939 static void sii8620_xtal_set_rate(struct sii8620 *ctx)
941 static const struct {
946 { 19200, 0x04, 0x53 },
947 { 20000, 0x04, 0x62 },
948 { 24000, 0x05, 0x75 },
949 { 30000, 0x06, 0x92 },
950 { 38400, 0x0c, 0xbc },
952 unsigned long rate = clk_get_rate(ctx->clk_xtal) / 1000;
955 for (i = 0; i < ARRAY_SIZE(rates) - 1; ++i)
956 if (rate <= rates[i].rate)
959 if (rate != rates[i].rate)
960 dev_err(ctx->dev, "xtal clock rate(%lukHz) not supported, setting MHL for %ukHz.\n",
961 rate, rates[i].rate);
963 sii8620_write(ctx, REG_DIV_CTL_MAIN, rates[i].div);
964 sii8620_write(ctx, REG_HDCP2X_TP1, rates[i].tp1);
967 static int sii8620_hw_on(struct sii8620 *ctx)
971 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
974 usleep_range(10000, 20000);
975 return clk_prepare_enable(ctx->clk_xtal);
978 static int sii8620_hw_off(struct sii8620 *ctx)
980 clk_disable_unprepare(ctx->clk_xtal);
981 gpiod_set_value(ctx->gpio_reset, 1);
982 return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
985 static void sii8620_hw_reset(struct sii8620 *ctx)
987 usleep_range(10000, 20000);
988 gpiod_set_value(ctx->gpio_reset, 0);
989 usleep_range(5000, 20000);
990 gpiod_set_value(ctx->gpio_reset, 1);
991 usleep_range(10000, 20000);
992 gpiod_set_value(ctx->gpio_reset, 0);
996 static void sii8620_cbus_reset(struct sii8620 *ctx)
998 sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST
999 | BIT_PWD_SRST_CBUS_RST_SW_EN);
1000 usleep_range(10000, 20000);
1001 sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN);
1004 static void sii8620_set_auto_zone(struct sii8620 *ctx)
1006 if (ctx->mode != CM_MHL1) {
1007 sii8620_write_seq_static(ctx,
1008 REG_TX_ZONE_CTL1, 0x0,
1009 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1010 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1011 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
1014 sii8620_write_seq_static(ctx,
1015 REG_TX_ZONE_CTL1, VAL_TX_ZONE_CTL1_TX_ZONE_CTRL_MODE,
1016 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1017 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
1022 static void sii8620_stop_video(struct sii8620 *ctx)
1024 u8 uninitialized_var(val);
1026 sii8620_write_seq_static(ctx,
1028 REG_HDCP2X_INTR0_MASK, 0,
1029 REG_TPI_COPP_DATA2, 0,
1030 REG_TPI_INTR_ST0, ~0,
1033 switch (ctx->sink_type) {
1035 val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
1036 | BIT_TPI_SC_TPI_AV_MUTE;
1040 val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
1041 | BIT_TPI_SC_TPI_AV_MUTE
1042 | BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI;
1046 sii8620_write(ctx, REG_TPI_SC, val);
1049 static void sii8620_set_format(struct sii8620 *ctx)
1053 if (sii8620_is_mhl3(ctx)) {
1054 sii8620_setbits(ctx, REG_M3_P0CTRL,
1055 BIT_M3_P0CTRL_MHL3_P0_PIXEL_MODE_PACKED,
1056 ctx->use_packed_pixel ? ~0 : 0);
1058 if (ctx->use_packed_pixel)
1059 sii8620_write_seq_static(ctx,
1060 REG_VID_MODE, BIT_VID_MODE_M1080P,
1061 REG_MHL_TOP_CTL, BIT_MHL_TOP_CTL_MHL_PP_SEL | 1,
1062 REG_MHLTX_CTL6, 0x60
1065 sii8620_write_seq_static(ctx,
1068 REG_MHLTX_CTL6, 0xa0
1072 if (ctx->use_packed_pixel)
1073 out_fmt = VAL_TPI_FORMAT(YCBCR422, FULL) |
1074 BIT_TPI_OUTPUT_CSCMODE709;
1076 out_fmt = VAL_TPI_FORMAT(RGB, FULL);
1078 sii8620_write_seq(ctx,
1079 REG_TPI_INPUT, VAL_TPI_FORMAT(RGB, FULL),
1080 REG_TPI_OUTPUT, out_fmt,
1084 static int mhl3_infoframe_init(struct mhl3_infoframe *frame)
1086 memset(frame, 0, sizeof(*frame));
1089 frame->hev_format = -1;
1093 static ssize_t mhl3_infoframe_pack(struct mhl3_infoframe *frame,
1094 void *buffer, size_t size)
1096 const int frm_len = HDMI_INFOFRAME_HEADER_SIZE + MHL3_INFOFRAME_SIZE;
1102 memset(buffer, 0, size);
1103 ptr[0] = HDMI_INFOFRAME_TYPE_VENDOR;
1104 ptr[1] = frame->version;
1105 ptr[2] = MHL3_INFOFRAME_SIZE;
1106 ptr[4] = MHL3_IEEE_OUI & 0xff;
1107 ptr[5] = (MHL3_IEEE_OUI >> 8) & 0xff;
1108 ptr[6] = (MHL3_IEEE_OUI >> 16) & 0xff;
1109 ptr[7] = frame->video_format & 0x3;
1110 ptr[7] |= (frame->format_type & 0x7) << 2;
1111 ptr[7] |= frame->sep_audio ? BIT(5) : 0;
1112 if (frame->hev_format >= 0) {
1114 ptr[10] = (frame->hev_format >> 8) & 0xff;
1115 ptr[11] = frame->hev_format & 0xff;
1117 if (frame->av_delay) {
1118 bool sign = frame->av_delay < 0;
1119 int delay = sign ? -frame->av_delay : frame->av_delay;
1121 ptr[12] = (delay >> 16) & 0xf;
1124 ptr[13] = (delay >> 8) & 0xff;
1125 ptr[14] = delay & 0xff;
1127 ptr[3] -= sii8620_checksum(buffer, frm_len);
1131 static void sii8620_set_infoframes(struct sii8620 *ctx)
1133 struct mhl3_infoframe mhl_frm;
1134 union hdmi_infoframe frm;
1138 if (!sii8620_is_mhl3(ctx) || !ctx->use_packed_pixel) {
1139 sii8620_write(ctx, REG_TPI_SC,
1140 BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI);
1141 sii8620_write_buf(ctx, REG_TPI_AVI_CHSUM, ctx->avif + 3,
1142 ARRAY_SIZE(ctx->avif) - 3);
1143 sii8620_write(ctx, REG_PKT_FILTER_0,
1144 BIT_PKT_FILTER_0_DROP_CEA_GAMUT_PKT |
1145 BIT_PKT_FILTER_0_DROP_MPEG_PKT |
1146 BIT_PKT_FILTER_0_DROP_GCP_PKT,
1147 BIT_PKT_FILTER_1_DROP_GEN_PKT);
1151 ret = hdmi_avi_infoframe_init(&frm.avi);
1152 frm.avi.colorspace = HDMI_COLORSPACE_YUV422;
1153 frm.avi.active_aspect = HDMI_ACTIVE_ASPECT_PICTURE;
1154 frm.avi.picture_aspect = HDMI_PICTURE_ASPECT_16_9;
1155 frm.avi.colorimetry = HDMI_COLORIMETRY_ITU_709;
1156 frm.avi.video_code = ctx->video_code;
1158 ret = hdmi_avi_infoframe_pack(&frm.avi, buf, ARRAY_SIZE(buf));
1160 sii8620_write_buf(ctx, REG_TPI_AVI_CHSUM, buf + 3, ret - 3);
1161 sii8620_write(ctx, REG_PKT_FILTER_0,
1162 BIT_PKT_FILTER_0_DROP_CEA_GAMUT_PKT |
1163 BIT_PKT_FILTER_0_DROP_MPEG_PKT |
1164 BIT_PKT_FILTER_0_DROP_AVI_PKT |
1165 BIT_PKT_FILTER_0_DROP_GCP_PKT,
1166 BIT_PKT_FILTER_1_VSI_OVERRIDE_DIS |
1167 BIT_PKT_FILTER_1_DROP_GEN_PKT |
1168 BIT_PKT_FILTER_1_DROP_VSIF_PKT);
1170 sii8620_write(ctx, REG_TPI_INFO_FSEL, BIT_TPI_INFO_FSEL_EN
1171 | BIT_TPI_INFO_FSEL_RPT | VAL_TPI_INFO_FSEL_VSI);
1172 ret = mhl3_infoframe_init(&mhl_frm);
1174 ret = mhl3_infoframe_pack(&mhl_frm, buf, ARRAY_SIZE(buf));
1175 sii8620_write_buf(ctx, REG_TPI_INFO_B0, buf, ret);
1178 static void sii8620_start_video(struct sii8620 *ctx)
1180 if (!sii8620_is_mhl3(ctx))
1181 sii8620_stop_video(ctx);
1183 if (ctx->sink_type == SINK_DVI && !sii8620_is_mhl3(ctx)) {
1184 sii8620_write(ctx, REG_RX_HDMI_CTRL2,
1185 VAL_RX_HDMI_CTRL2_DEFVAL);
1186 sii8620_write(ctx, REG_TPI_SC, 0);
1190 sii8620_write_seq_static(ctx,
1191 REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL
1192 | BIT_RX_HDMI_CTRL2_USE_AV_MUTE,
1193 REG_VID_OVRRD, BIT_VID_OVRRD_PP_AUTO_DISABLE
1194 | BIT_VID_OVRRD_M1080P_OVRRD);
1195 sii8620_set_format(ctx);
1197 if (!sii8620_is_mhl3(ctx)) {
1198 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1199 MHL_DST_LM_CLK_MODE_NORMAL | MHL_DST_LM_PATH_ENABLED);
1200 sii8620_set_auto_zone(ctx);
1202 static const struct {
1208 { 150000, VAL_TX_ZONE_CTL3_TX_ZONE_1_5GBPS,
1209 MHL_XDS_LINK_RATE_1_5_GBPS, 0x38 },
1210 { 300000, VAL_TX_ZONE_CTL3_TX_ZONE_3GBPS,
1211 MHL_XDS_LINK_RATE_3_0_GBPS, 0x40 },
1212 { 600000, VAL_TX_ZONE_CTL3_TX_ZONE_6GBPS,
1213 MHL_XDS_LINK_RATE_6_0_GBPS, 0x40 },
1215 u8 p0_ctrl = BIT_M3_P0CTRL_MHL3_P0_PORT_EN;
1216 int clk = ctx->pixel_clock * (ctx->use_packed_pixel ? 2 : 3);
1219 for (i = 0; i < ARRAY_SIZE(clk_spec); ++i)
1220 if (clk < clk_spec[i].max_clk)
1223 if (100 * clk >= 98 * clk_spec[i].max_clk)
1224 p0_ctrl |= BIT_M3_P0CTRL_MHL3_P0_UNLIMIT_EN;
1226 sii8620_burst_tx_bits_per_pixel_fmt(ctx, ctx->use_packed_pixel);
1227 sii8620_burst_send(ctx);
1228 sii8620_write_seq(ctx,
1229 REG_MHL_DP_CTL0, 0xf0,
1230 REG_MHL3_TX_ZONE_CTL, clk_spec[i].zone);
1231 sii8620_setbits(ctx, REG_M3_P0CTRL,
1232 BIT_M3_P0CTRL_MHL3_P0_PORT_EN
1233 | BIT_M3_P0CTRL_MHL3_P0_UNLIMIT_EN, p0_ctrl);
1234 sii8620_setbits(ctx, REG_M3_POSTM, MSK_M3_POSTM_RRP_DECODE,
1235 clk_spec[i].rrp_decode);
1236 sii8620_write_seq_static(ctx,
1237 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE
1238 | BIT_M3_CTRL_H2M_SWRST,
1239 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE
1241 sii8620_mt_write_stat(ctx, MHL_XDS_REG(AVLINK_MODE_CONTROL),
1242 clk_spec[i].link_rate);
1245 sii8620_set_infoframes(ctx);
1248 static void sii8620_disable_hpd(struct sii8620 *ctx)
1250 sii8620_setbits(ctx, REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID, 0);
1251 sii8620_write_seq_static(ctx,
1252 REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN,
1257 static void sii8620_enable_hpd(struct sii8620 *ctx)
1259 sii8620_setbits(ctx, REG_TMDS_CSTAT_P3,
1260 BIT_TMDS_CSTAT_P3_SCDT_CLR_AVI_DIS
1261 | BIT_TMDS_CSTAT_P3_CLR_AVI, ~0);
1262 sii8620_write_seq_static(ctx,
1263 REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN
1264 | BIT_HPD_CTRL_HPD_HIGH,
1268 static void sii8620_mhl_discover(struct sii8620 *ctx)
1270 sii8620_write_seq_static(ctx,
1271 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1272 | BIT_DISC_CTRL9_DISC_PULSE_PROCEED,
1273 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_5K, VAL_PUP_20K),
1274 REG_CBUS_DISC_INTR0_MASK, BIT_MHL3_EST_INT
1276 | BIT_NOT_MHL_EST_INT
1277 | BIT_CBUS_MHL3_DISCON_INT
1278 | BIT_CBUS_MHL12_DISCON_INT
1279 | BIT_RGND_READY_INT,
1280 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1281 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1282 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
1283 REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
1284 | BIT_MHL_DP_CTL0_TX_OE_OVR,
1285 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
1286 REG_MHL_DP_CTL1, 0xA2,
1287 REG_MHL_DP_CTL2, 0x03,
1288 REG_MHL_DP_CTL3, 0x35,
1289 REG_MHL_DP_CTL5, 0x02,
1290 REG_MHL_DP_CTL6, 0x02,
1291 REG_MHL_DP_CTL7, 0x03,
1293 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
1294 | BIT_DPD_OSC_EN | BIT_DPD_PWRON_HSIC,
1295 REG_COC_INTR_MASK, BIT_COC_PLL_LOCK_STATUS_CHANGE
1296 | BIT_COC_CALIBRATION_DONE,
1297 REG_CBUS_INT_1_MASK, BIT_CBUS_MSC_ABORT_RCVD
1298 | BIT_CBUS_CMD_ABORT,
1299 REG_CBUS_INT_0_MASK, BIT_CBUS_MSC_MT_DONE
1301 | BIT_CBUS_MSC_MR_WRITE_STAT
1302 | BIT_CBUS_MSC_MR_MSC_MSG
1303 | BIT_CBUS_MSC_MR_WRITE_BURST
1304 | BIT_CBUS_MSC_MR_SET_INT
1305 | BIT_CBUS_MSC_MT_DONE_NACK
1309 static void sii8620_peer_specific_init(struct sii8620 *ctx)
1311 if (sii8620_is_mhl3(ctx))
1312 sii8620_write_seq_static(ctx,
1313 REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD,
1315 BIT_EMSCINTR1_EMSC_TRAINING_COMMA_ERR
1318 sii8620_write_seq_static(ctx,
1319 REG_HDCP2X_INTR0_MASK, 0x00,
1320 REG_EMSCINTRMASK1, 0x00,
1321 REG_HDCP2X_INTR0, 0xFF,
1323 REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD
1324 | BIT_SYS_CTRL1_TX_CTRL_HDMI
1328 #define SII8620_MHL_VERSION 0x32
1329 #define SII8620_SCRATCHPAD_SIZE 16
1330 #define SII8620_INT_STAT_SIZE 0x33
1332 static void sii8620_set_dev_cap(struct sii8620 *ctx)
1334 static const u8 devcap[MHL_DCAP_SIZE] = {
1335 [MHL_DCAP_MHL_VERSION] = SII8620_MHL_VERSION,
1336 [MHL_DCAP_CAT] = MHL_DCAP_CAT_SOURCE | MHL_DCAP_CAT_POWER,
1337 [MHL_DCAP_ADOPTER_ID_H] = 0x01,
1338 [MHL_DCAP_ADOPTER_ID_L] = 0x41,
1339 [MHL_DCAP_VID_LINK_MODE] = MHL_DCAP_VID_LINK_RGB444
1340 | MHL_DCAP_VID_LINK_PPIXEL
1341 | MHL_DCAP_VID_LINK_16BPP,
1342 [MHL_DCAP_AUD_LINK_MODE] = MHL_DCAP_AUD_LINK_2CH,
1343 [MHL_DCAP_VIDEO_TYPE] = MHL_DCAP_VT_GRAPHICS,
1344 [MHL_DCAP_LOG_DEV_MAP] = MHL_DCAP_LD_GUI,
1345 [MHL_DCAP_BANDWIDTH] = 0x0f,
1346 [MHL_DCAP_FEATURE_FLAG] = MHL_DCAP_FEATURE_RCP_SUPPORT
1347 | MHL_DCAP_FEATURE_RAP_SUPPORT
1348 | MHL_DCAP_FEATURE_SP_SUPPORT,
1349 [MHL_DCAP_SCRATCHPAD_SIZE] = SII8620_SCRATCHPAD_SIZE,
1350 [MHL_DCAP_INT_STAT_SIZE] = SII8620_INT_STAT_SIZE,
1352 static const u8 xdcap[MHL_XDC_SIZE] = {
1353 [MHL_XDC_ECBUS_SPEEDS] = MHL_XDC_ECBUS_S_075
1354 | MHL_XDC_ECBUS_S_8BIT,
1355 [MHL_XDC_TMDS_SPEEDS] = MHL_XDC_TMDS_150
1356 | MHL_XDC_TMDS_300 | MHL_XDC_TMDS_600,
1357 [MHL_XDC_ECBUS_ROLES] = MHL_XDC_DEV_HOST,
1358 [MHL_XDC_LOG_DEV_MAPX] = MHL_XDC_LD_PHONE,
1361 sii8620_write_buf(ctx, REG_MHL_DEVCAP_0, devcap, ARRAY_SIZE(devcap));
1362 sii8620_write_buf(ctx, REG_MHL_EXTDEVCAP_0, xdcap, ARRAY_SIZE(xdcap));
1365 static void sii8620_mhl_init(struct sii8620 *ctx)
1367 sii8620_write_seq_static(ctx,
1368 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1369 REG_CBUS_MSC_COMPAT_CTRL,
1370 BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN,
1373 sii8620_peer_specific_init(ctx);
1375 sii8620_disable_hpd(ctx);
1377 sii8620_write_seq_static(ctx,
1378 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
1379 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1380 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1381 REG_TMDS0_CCTRL1, 0x90,
1382 REG_TMDS_CLK_EN, 0x01,
1383 REG_TMDS_CH_EN, 0x11,
1385 REG_ALICE0_ZONE_CTRL, 0xE8,
1386 REG_ALICE0_MODE_CTRL, 0x04,
1388 sii8620_setbits(ctx, REG_LM_DDC, BIT_LM_DDC_SW_TPI_EN_DISABLED, 0);
1389 sii8620_write_seq_static(ctx,
1390 REG_TPI_HW_OPT3, 0x76,
1391 REG_TMDS_CCTRL, BIT_TMDS_CCTRL_TMDS_OE,
1394 sii8620_set_dev_cap(ctx);
1395 sii8620_write_seq_static(ctx,
1396 REG_MDT_XMIT_TIMEOUT, 100,
1397 REG_MDT_XMIT_CTRL, 0x03,
1398 REG_MDT_XFIFO_STAT, 0x00,
1399 REG_MDT_RCV_TIMEOUT, 100,
1400 REG_CBUS_LINK_CTRL_8, 0x1D,
1403 sii8620_start_gen2_write_burst(ctx);
1404 sii8620_write_seq_static(ctx,
1405 REG_BIST_CTRL, 0x00,
1409 REG_COC_CTL11, 0xF8,
1410 REG_COC_CTL17, 0x61,
1411 REG_COC_CTL18, 0x46,
1412 REG_COC_CTL19, 0x15,
1413 REG_COC_CTL1A, 0x01,
1414 REG_MHL_COC_CTL3, BIT_MHL_COC_CTL3_COC_AECHO_EN,
1415 REG_MHL_COC_CTL4, 0x2D,
1416 REG_MHL_COC_CTL5, 0xF9,
1417 REG_MSC_HEARTBEAT_CTRL, 0x27,
1419 sii8620_disable_gen2_write_burst(ctx);
1421 sii8620_mt_write_stat(ctx, MHL_DST_REG(VERSION), SII8620_MHL_VERSION);
1422 sii8620_mt_write_stat(ctx, MHL_DST_REG(CONNECTED_RDY),
1423 MHL_DST_CONN_DCAP_RDY | MHL_DST_CONN_XDEVCAPP_SUPP
1424 | MHL_DST_CONN_POW_STAT);
1425 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE), MHL_INT_RC_DCAP_CHG);
1428 static void sii8620_emsc_enable(struct sii8620 *ctx)
1432 sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_EMSC_EN
1433 | BIT_GENCTL_CLR_EMSC_RFIFO
1434 | BIT_GENCTL_CLR_EMSC_XFIFO, ~0);
1435 sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_CLR_EMSC_RFIFO
1436 | BIT_GENCTL_CLR_EMSC_XFIFO, 0);
1437 sii8620_setbits(ctx, REG_COMMECNT, BIT_COMMECNT_I2C_TO_EMSC_EN, ~0);
1438 reg = sii8620_readb(ctx, REG_EMSCINTR);
1439 sii8620_write(ctx, REG_EMSCINTR, reg);
1440 sii8620_write(ctx, REG_EMSCINTRMASK, BIT_EMSCINTR_SPI_DVLD);
1443 static int sii8620_wait_for_fsm_state(struct sii8620 *ctx, u8 state)
1447 for (i = 0; i < 10; ++i) {
1448 u8 s = sii8620_readb(ctx, REG_COC_STAT_0);
1450 if ((s & MSK_COC_STAT_0_FSM_STATE) == state)
1452 if (!(s & BIT_COC_STAT_0_PLL_LOCKED))
1454 usleep_range(4000, 6000);
1459 static void sii8620_set_mode(struct sii8620 *ctx, enum sii8620_mode mode)
1463 if (ctx->mode == mode)
1468 sii8620_write_seq_static(ctx,
1469 REG_CBUS_MSC_COMPAT_CTRL, 0x02,
1470 REG_M3_CTRL, VAL_M3_CTRL_MHL1_2_VALUE,
1471 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
1473 REG_COC_INTR_MASK, 0
1478 sii8620_write(ctx, REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE);
1482 sii8620_emsc_enable(ctx);
1483 sii8620_write_seq_static(ctx,
1486 REG_TTXHSICNUMS, 0x14,
1487 REG_TRXHSICNUMS, 0x14,
1488 REG_TTXTOTNUMS, 0x18,
1489 REG_TRXTOTNUMS, 0x18,
1490 REG_PWD_SRST, BIT_PWD_SRST_COC_DOC_RST
1491 | BIT_PWD_SRST_CBUS_RST_SW_EN,
1492 REG_MHL_COC_CTL1, 0xbd,
1493 REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN,
1496 REG_COC_CTL14, 0x03,
1497 REG_COC_CTL15, 0x80,
1498 REG_MHL_DP_CTL6, BIT_MHL_DP_CTL6_DP_TAP1_SGN
1499 | BIT_MHL_DP_CTL6_DP_TAP1_EN
1500 | BIT_MHL_DP_CTL6_DT_PREDRV_FEEDCAP_EN,
1501 REG_MHL_DP_CTL8, 0x03
1503 ret = sii8620_wait_for_fsm_state(ctx, 0x03);
1504 sii8620_write_seq_static(ctx,
1505 REG_COC_CTL14, 0x00,
1509 sii8620_write(ctx, REG_CBUS3_CNVT, 0x85);
1511 sii8620_disconnect(ctx);
1513 case CM_DISCONNECTED:
1517 dev_err(ctx->dev, "%s mode %d not supported\n", __func__, mode);
1521 sii8620_set_auto_zone(ctx);
1523 if (mode != CM_MHL1)
1526 sii8620_write_seq_static(ctx,
1527 REG_MHL_DP_CTL0, 0xBC,
1528 REG_MHL_DP_CTL1, 0xBB,
1529 REG_MHL_DP_CTL3, 0x48,
1530 REG_MHL_DP_CTL5, 0x39,
1531 REG_MHL_DP_CTL2, 0x2A,
1532 REG_MHL_DP_CTL6, 0x2A,
1533 REG_MHL_DP_CTL7, 0x08
1537 static void sii8620_disconnect(struct sii8620 *ctx)
1539 sii8620_disable_gen2_write_burst(ctx);
1540 sii8620_stop_video(ctx);
1542 sii8620_cbus_reset(ctx);
1543 sii8620_set_mode(ctx, CM_DISCONNECTED);
1544 sii8620_write_seq_static(ctx,
1545 REG_TX_ZONE_CTL1, 0,
1546 REG_MHL_PLL_CTL0, 0x07,
1548 REG_CBUS3_CNVT, 0x84,
1549 REG_COC_CTL14, 0x00,
1552 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1553 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1554 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
1555 REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
1556 | BIT_MHL_DP_CTL0_TX_OE_OVR,
1557 REG_MHL_DP_CTL1, 0xBB,
1558 REG_MHL_DP_CTL3, 0x48,
1559 REG_MHL_DP_CTL5, 0x3F,
1560 REG_MHL_DP_CTL2, 0x2F,
1561 REG_MHL_DP_CTL6, 0x2A,
1562 REG_MHL_DP_CTL7, 0x03
1564 sii8620_disable_hpd(ctx);
1565 sii8620_write_seq_static(ctx,
1566 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
1567 REG_MHL_COC_CTL1, 0x07,
1568 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1569 REG_DISC_CTRL8, 0x00,
1570 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1571 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1573 REG_MSC_HEARTBEAT_CTRL, 0x27,
1574 REG_DISC_CTRL1, 0x25,
1575 REG_CBUS_DISC_INTR0, (u8)~BIT_RGND_READY_INT,
1576 REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT,
1577 REG_MDT_INT_1, 0xff,
1578 REG_MDT_INT_1_MASK, 0x00,
1579 REG_MDT_INT_0, 0xff,
1580 REG_MDT_INT_0_MASK, 0x00,
1582 REG_COC_INTR_MASK, 0x00,
1585 REG_CBUS_INT_0, 0xff,
1586 REG_CBUS_INT_0_MASK, 0x00,
1587 REG_CBUS_INT_1, 0xff,
1588 REG_CBUS_INT_1_MASK, 0x00,
1590 REG_EMSCINTRMASK, 0x00,
1591 REG_EMSCINTR1, 0xff,
1592 REG_EMSCINTRMASK1, 0x00,
1594 REG_INTR8_MASK, 0x00,
1595 REG_TPI_INTR_ST0, 0xff,
1596 REG_TPI_INTR_EN, 0x00,
1597 REG_HDCP2X_INTR0, 0xff,
1598 REG_HDCP2X_INTR0_MASK, 0x00,
1600 REG_INTR9_MASK, 0x00,
1602 REG_INTR3_MASK, 0x00,
1604 REG_INTR5_MASK, 0x00,
1606 REG_INTR2_MASK, 0x00,
1608 memset(ctx->stat, 0, sizeof(ctx->stat));
1609 memset(ctx->xstat, 0, sizeof(ctx->xstat));
1610 memset(ctx->devcap, 0, sizeof(ctx->devcap));
1611 memset(ctx->xdevcap, 0, sizeof(ctx->xdevcap));
1612 ctx->cbus_status = 0;
1613 ctx->sink_type = SINK_NONE;
1616 sii8620_mt_cleanup(ctx);
1619 static void sii8620_mhl_disconnected(struct sii8620 *ctx)
1621 sii8620_write_seq_static(ctx,
1622 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1623 REG_CBUS_MSC_COMPAT_CTRL,
1624 BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN
1626 sii8620_disconnect(ctx);
1629 static void sii8620_irq_disc(struct sii8620 *ctx)
1631 u8 stat = sii8620_readb(ctx, REG_CBUS_DISC_INTR0);
1633 if (stat & VAL_CBUS_MHL_DISCON)
1634 sii8620_mhl_disconnected(ctx);
1636 if (stat & BIT_RGND_READY_INT) {
1637 u8 stat2 = sii8620_readb(ctx, REG_DISC_STAT2);
1639 if ((stat2 & MSK_DISC_STAT2_RGND) == VAL_RGND_1K) {
1640 sii8620_mhl_discover(ctx);
1642 sii8620_write_seq_static(ctx,
1643 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1644 | BIT_DISC_CTRL9_NOMHL_EST
1645 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1646 REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT
1647 | BIT_CBUS_MHL3_DISCON_INT
1648 | BIT_CBUS_MHL12_DISCON_INT
1649 | BIT_NOT_MHL_EST_INT
1653 if (stat & BIT_MHL_EST_INT)
1654 sii8620_mhl_init(ctx);
1656 sii8620_write(ctx, REG_CBUS_DISC_INTR0, stat);
1659 static void sii8620_read_burst(struct sii8620 *ctx)
1663 sii8620_read_buf(ctx, REG_MDT_RCV_READ_PORT, buf, ARRAY_SIZE(buf));
1664 sii8620_write(ctx, REG_MDT_RCV_CTRL, BIT_MDT_RCV_CTRL_MDT_RCV_EN |
1665 BIT_MDT_RCV_CTRL_MDT_DELAY_RCV_EN |
1666 BIT_MDT_RCV_CTRL_MDT_RFIFO_CLR_CUR);
1667 sii8620_readb(ctx, REG_MDT_RFIFO_STAT);
1670 static void sii8620_irq_g2wb(struct sii8620 *ctx)
1672 u8 stat = sii8620_readb(ctx, REG_MDT_INT_0);
1674 if (stat & BIT_MDT_IDLE_AFTER_HAWB_DISABLE)
1675 if (sii8620_is_mhl3(ctx))
1676 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
1677 MHL_INT_RC_FEAT_COMPLETE);
1679 if (stat & BIT_MDT_RFIFO_DATA_RDY)
1680 sii8620_read_burst(ctx);
1682 if (stat & BIT_MDT_XFIFO_EMPTY)
1683 sii8620_write(ctx, REG_MDT_XMIT_CTRL, 0);
1685 sii8620_write(ctx, REG_MDT_INT_0, stat);
1688 static void sii8620_status_dcap_ready(struct sii8620 *ctx)
1690 enum sii8620_mode mode;
1692 mode = ctx->stat[MHL_DST_VERSION] >= 0x30 ? CM_MHL3 : CM_MHL1;
1693 if (mode > ctx->mode)
1694 sii8620_set_mode(ctx, mode);
1695 sii8620_peer_specific_init(ctx);
1696 sii8620_write(ctx, REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE
1697 | BIT_INTR9_EDID_DONE | BIT_INTR9_EDID_ERROR);
1700 static void sii8620_status_changed_path(struct sii8620 *ctx)
1702 if (ctx->stat[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED) {
1703 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1704 MHL_DST_LM_CLK_MODE_NORMAL
1705 | MHL_DST_LM_PATH_ENABLED);
1706 if (!sii8620_is_mhl3(ctx))
1707 sii8620_mt_read_devcap(ctx, false);
1708 sii8620_mt_set_cont(ctx, sii8620_sink_detected);
1710 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1711 MHL_DST_LM_CLK_MODE_NORMAL);
1715 static void sii8620_msc_mr_write_stat(struct sii8620 *ctx)
1717 u8 st[MHL_DST_SIZE], xst[MHL_XDS_SIZE];
1719 sii8620_read_buf(ctx, REG_MHL_STAT_0, st, MHL_DST_SIZE);
1720 sii8620_read_buf(ctx, REG_MHL_EXTSTAT_0, xst, MHL_XDS_SIZE);
1722 sii8620_update_array(ctx->stat, st, MHL_DST_SIZE);
1723 sii8620_update_array(ctx->xstat, xst, MHL_XDS_SIZE);
1725 if (ctx->stat[MHL_DST_CONNECTED_RDY] & MHL_DST_CONN_DCAP_RDY)
1726 sii8620_status_dcap_ready(ctx);
1728 if (st[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED)
1729 sii8620_status_changed_path(ctx);
1732 static void sii8620_ecbus_up(struct sii8620 *ctx, int ret)
1737 sii8620_set_mode(ctx, CM_ECBUS_S);
1740 static void sii8620_got_ecbus_speed(struct sii8620 *ctx, int ret)
1745 sii8620_mt_write_stat(ctx, MHL_XDS_REG(CURR_ECBUS_MODE),
1746 MHL_XDS_ECBUS_S | MHL_XDS_SLOT_MODE_8BIT);
1747 sii8620_mt_rap(ctx, MHL_RAP_CBUS_MODE_UP);
1748 sii8620_mt_set_cont(ctx, sii8620_ecbus_up);
1751 static void sii8620_mhl_burst_emsc_support_set(struct mhl_burst_emsc_support *d,
1752 enum mhl_burst_id id)
1754 sii8620_mhl_burst_hdr_set(&d->hdr, MHL_BURST_ID_EMSC_SUPPORT);
1756 d->burst_id[0] = cpu_to_be16(id);
1759 static void sii8620_send_features(struct sii8620 *ctx)
1763 sii8620_write(ctx, REG_MDT_XMIT_CTRL, BIT_MDT_XMIT_CTRL_EN
1764 | BIT_MDT_XMIT_CTRL_FIXED_BURST_LEN);
1765 sii8620_mhl_burst_emsc_support_set((void *)buf,
1766 MHL_BURST_ID_HID_PAYLOAD);
1767 sii8620_write_buf(ctx, REG_MDT_XMIT_WRITE_PORT, buf, ARRAY_SIZE(buf));
1770 static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
1772 bool pressed = !(scancode & MHL_RCP_KEY_RELEASED_MASK);
1774 scancode &= MHL_RCP_KEY_ID_MASK;
1777 dev_dbg(ctx->dev, "RCP input device not initialized\n");
1782 rc_keydown(ctx->rc_dev, RC_PROTO_CEC, scancode, 0);
1784 rc_keyup(ctx->rc_dev);
1789 static void sii8620_msc_mr_set_int(struct sii8620 *ctx)
1791 u8 ints[MHL_INT_SIZE];
1793 sii8620_read_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
1794 sii8620_write_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
1796 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_DCAP_CHG) {
1797 switch (ctx->mode) {
1799 sii8620_mt_read_xdevcap_reg(ctx, MHL_XDC_ECBUS_SPEEDS);
1800 sii8620_mt_set_cont(ctx, sii8620_got_ecbus_speed);
1803 sii8620_mt_read_devcap(ctx, true);
1809 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_REQ)
1810 sii8620_send_features(ctx);
1811 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_COMPLETE)
1812 sii8620_edid_read(ctx, 0);
1815 static struct sii8620_mt_msg *sii8620_msc_msg_first(struct sii8620 *ctx)
1817 struct device *dev = ctx->dev;
1819 if (list_empty(&ctx->mt_queue)) {
1820 dev_err(dev, "unexpected MSC MT response\n");
1824 return list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
1827 static void sii8620_msc_mt_done(struct sii8620 *ctx)
1829 struct sii8620_mt_msg *msg = sii8620_msc_msg_first(ctx);
1834 msg->ret = sii8620_readb(ctx, REG_MSC_MT_RCVD_DATA0);
1835 ctx->mt_state = MT_STATE_DONE;
1838 static void sii8620_msc_mr_msc_msg(struct sii8620 *ctx)
1840 struct sii8620_mt_msg *msg;
1843 sii8620_read_buf(ctx, REG_MSC_MR_MSC_MSG_RCVD_1ST_DATA, buf, 2);
1846 case MHL_MSC_MSG_RAPK:
1847 msg = sii8620_msc_msg_first(ctx);
1851 ctx->mt_state = MT_STATE_DONE;
1853 case MHL_MSC_MSG_RCP:
1854 if (!sii8620_rcp_consume(ctx, buf[1]))
1855 sii8620_mt_rcpe(ctx,
1856 MHL_RCPE_STATUS_INEFFECTIVE_KEY_CODE);
1857 sii8620_mt_rcpk(ctx, buf[1]);
1860 dev_err(ctx->dev, "%s message type %d,%d not supported",
1861 __func__, buf[0], buf[1]);
1865 static void sii8620_irq_msc(struct sii8620 *ctx)
1867 u8 stat = sii8620_readb(ctx, REG_CBUS_INT_0);
1869 if (stat & ~BIT_CBUS_HPD_CHG)
1870 sii8620_write(ctx, REG_CBUS_INT_0, stat & ~BIT_CBUS_HPD_CHG);
1872 if (stat & BIT_CBUS_HPD_CHG) {
1873 u8 cbus_stat = sii8620_readb(ctx, REG_CBUS_STATUS);
1875 if ((cbus_stat ^ ctx->cbus_status) & BIT_CBUS_STATUS_CBUS_HPD) {
1876 sii8620_write(ctx, REG_CBUS_INT_0, BIT_CBUS_HPD_CHG);
1878 stat ^= BIT_CBUS_STATUS_CBUS_HPD;
1879 cbus_stat ^= BIT_CBUS_STATUS_CBUS_HPD;
1881 ctx->cbus_status = cbus_stat;
1884 if (stat & BIT_CBUS_MSC_MR_WRITE_STAT)
1885 sii8620_msc_mr_write_stat(ctx);
1887 if (stat & BIT_CBUS_MSC_MR_SET_INT)
1888 sii8620_msc_mr_set_int(ctx);
1890 if (stat & BIT_CBUS_MSC_MT_DONE)
1891 sii8620_msc_mt_done(ctx);
1893 if (stat & BIT_CBUS_MSC_MR_MSC_MSG)
1894 sii8620_msc_mr_msc_msg(ctx);
1897 static void sii8620_irq_coc(struct sii8620 *ctx)
1899 u8 stat = sii8620_readb(ctx, REG_COC_INTR);
1901 if (stat & BIT_COC_CALIBRATION_DONE) {
1902 u8 cstat = sii8620_readb(ctx, REG_COC_STAT_0);
1904 cstat &= BIT_COC_STAT_0_PLL_LOCKED | MSK_COC_STAT_0_FSM_STATE;
1905 if (cstat == (BIT_COC_STAT_0_PLL_LOCKED | 0x02)) {
1906 sii8620_write_seq_static(ctx,
1908 REG_TRXINTMH, BIT_TDM_INTR_SYNC_DATA
1909 | BIT_TDM_INTR_SYNC_WAIT
1914 sii8620_write(ctx, REG_COC_INTR, stat);
1917 static void sii8620_irq_merr(struct sii8620 *ctx)
1919 u8 stat = sii8620_readb(ctx, REG_CBUS_INT_1);
1921 sii8620_write(ctx, REG_CBUS_INT_1, stat);
1924 static void sii8620_irq_edid(struct sii8620 *ctx)
1926 u8 stat = sii8620_readb(ctx, REG_INTR9);
1928 sii8620_write(ctx, REG_INTR9, stat);
1930 if (stat & BIT_INTR9_DEVCAP_DONE)
1931 ctx->mt_state = MT_STATE_DONE;
1934 static void sii8620_scdt_high(struct sii8620 *ctx)
1936 sii8620_write_seq_static(ctx,
1937 REG_INTR8_MASK, BIT_CEA_NEW_AVI | BIT_CEA_NEW_VSI,
1938 REG_TPI_SC, BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI,
1942 static void sii8620_irq_scdt(struct sii8620 *ctx)
1944 u8 stat = sii8620_readb(ctx, REG_INTR5);
1946 if (stat & BIT_INTR_SCDT_CHANGE) {
1947 u8 cstat = sii8620_readb(ctx, REG_TMDS_CSTAT_P3);
1949 if (cstat & BIT_TMDS_CSTAT_P3_SCDT) {
1950 if (ctx->sink_type == SINK_HDMI)
1951 /* enable infoframe interrupt */
1952 sii8620_scdt_high(ctx);
1954 sii8620_start_video(ctx);
1958 sii8620_write(ctx, REG_INTR5, stat);
1961 static void sii8620_new_vsi(struct sii8620 *ctx)
1965 sii8620_write(ctx, REG_RX_HDMI_CTRL2,
1966 VAL_RX_HDMI_CTRL2_DEFVAL |
1967 BIT_RX_HDMI_CTRL2_VSI_MON_SEL_VSI);
1968 sii8620_read_buf(ctx, REG_RX_HDMI_MON_PKT_HEADER1, vsif,
1972 static void sii8620_new_avi(struct sii8620 *ctx)
1974 sii8620_write(ctx, REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL);
1975 sii8620_read_buf(ctx, REG_RX_HDMI_MON_PKT_HEADER1, ctx->avif,
1976 ARRAY_SIZE(ctx->avif));
1979 static void sii8620_irq_infr(struct sii8620 *ctx)
1981 u8 stat = sii8620_readb(ctx, REG_INTR8)
1982 & (BIT_CEA_NEW_VSI | BIT_CEA_NEW_AVI);
1984 sii8620_write(ctx, REG_INTR8, stat);
1986 if (stat & BIT_CEA_NEW_VSI)
1987 sii8620_new_vsi(ctx);
1989 if (stat & BIT_CEA_NEW_AVI)
1990 sii8620_new_avi(ctx);
1992 if (stat & (BIT_CEA_NEW_VSI | BIT_CEA_NEW_AVI))
1993 sii8620_start_video(ctx);
1996 static void sii8620_got_xdevcap(struct sii8620 *ctx, int ret)
2001 sii8620_mt_read_devcap(ctx, false);
2004 static void sii8620_irq_tdm(struct sii8620 *ctx)
2006 u8 stat = sii8620_readb(ctx, REG_TRXINTH);
2007 u8 tdm = sii8620_readb(ctx, REG_TRXSTA2);
2009 if ((tdm & MSK_TDM_SYNCHRONIZED) == VAL_TDM_SYNCHRONIZED) {
2010 ctx->mode = CM_ECBUS_S;
2011 ctx->burst.rx_ack = 0;
2012 ctx->burst.r_size = SII8620_BURST_BUF_LEN;
2013 sii8620_burst_tx_rbuf_info(ctx, SII8620_BURST_BUF_LEN);
2014 sii8620_mt_read_devcap(ctx, true);
2015 sii8620_mt_set_cont(ctx, sii8620_got_xdevcap);
2017 sii8620_write_seq_static(ctx,
2018 REG_MHL_PLL_CTL2, 0,
2019 REG_MHL_PLL_CTL2, BIT_MHL_PLL_CTL2_CLKDETECT_EN
2023 sii8620_write(ctx, REG_TRXINTH, stat);
2026 static void sii8620_irq_block(struct sii8620 *ctx)
2028 u8 stat = sii8620_readb(ctx, REG_EMSCINTR);
2030 if (stat & BIT_EMSCINTR_SPI_DVLD) {
2031 u8 bstat = sii8620_readb(ctx, REG_SPIBURSTSTAT);
2033 if (bstat & BIT_SPIBURSTSTAT_EMSC_NORMAL_MODE)
2034 sii8620_burst_receive(ctx);
2037 sii8620_write(ctx, REG_EMSCINTR, stat);
2040 static void sii8620_irq_ddc(struct sii8620 *ctx)
2042 u8 stat = sii8620_readb(ctx, REG_INTR3);
2044 if (stat & BIT_DDC_CMD_DONE) {
2045 sii8620_write(ctx, REG_INTR3_MASK, 0);
2046 if (sii8620_is_mhl3(ctx))
2047 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
2048 MHL_INT_RC_FEAT_REQ);
2050 sii8620_edid_read(ctx, 0);
2052 sii8620_write(ctx, REG_INTR3, stat);
2055 /* endian agnostic, non-volatile version of test_bit */
2056 static bool sii8620_test_bit(unsigned int nr, const u8 *addr)
2058 return 1 & (addr[nr / BITS_PER_BYTE] >> (nr % BITS_PER_BYTE));
2061 static irqreturn_t sii8620_irq_thread(int irq, void *data)
2063 static const struct {
2065 void (*handler)(struct sii8620 *ctx);
2067 { BIT_FAST_INTR_STAT_DISC, sii8620_irq_disc },
2068 { BIT_FAST_INTR_STAT_G2WB, sii8620_irq_g2wb },
2069 { BIT_FAST_INTR_STAT_COC, sii8620_irq_coc },
2070 { BIT_FAST_INTR_STAT_TDM, sii8620_irq_tdm },
2071 { BIT_FAST_INTR_STAT_MSC, sii8620_irq_msc },
2072 { BIT_FAST_INTR_STAT_MERR, sii8620_irq_merr },
2073 { BIT_FAST_INTR_STAT_BLOCK, sii8620_irq_block },
2074 { BIT_FAST_INTR_STAT_EDID, sii8620_irq_edid },
2075 { BIT_FAST_INTR_STAT_DDC, sii8620_irq_ddc },
2076 { BIT_FAST_INTR_STAT_SCDT, sii8620_irq_scdt },
2077 { BIT_FAST_INTR_STAT_INFR, sii8620_irq_infr },
2079 struct sii8620 *ctx = data;
2080 u8 stats[LEN_FAST_INTR_STAT];
2083 mutex_lock(&ctx->lock);
2085 sii8620_read_buf(ctx, REG_FAST_INTR_STAT, stats, ARRAY_SIZE(stats));
2086 for (i = 0; i < ARRAY_SIZE(irq_vec); ++i)
2087 if (sii8620_test_bit(irq_vec[i].bit, stats))
2088 irq_vec[i].handler(ctx);
2090 sii8620_burst_rx_all(ctx);
2091 sii8620_mt_work(ctx);
2092 sii8620_burst_send(ctx);
2094 ret = sii8620_clear_error(ctx);
2096 dev_err(ctx->dev, "Error during IRQ handling, %d.\n", ret);
2097 sii8620_mhl_disconnected(ctx);
2099 mutex_unlock(&ctx->lock);
2104 static void sii8620_cable_in(struct sii8620 *ctx)
2106 struct device *dev = ctx->dev;
2110 ret = sii8620_hw_on(ctx);
2112 dev_err(dev, "Error powering on, %d.\n", ret);
2115 sii8620_hw_reset(ctx);
2117 sii8620_read_buf(ctx, REG_VND_IDL, ver, ARRAY_SIZE(ver));
2118 ret = sii8620_clear_error(ctx);
2120 dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
2124 dev_info(dev, "ChipID %02x%02x:%02x%02x rev %02x.\n", ver[1], ver[0],
2125 ver[3], ver[2], ver[4]);
2127 sii8620_write(ctx, REG_DPD,
2128 BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN);
2130 sii8620_xtal_set_rate(ctx);
2131 sii8620_disconnect(ctx);
2133 sii8620_write_seq_static(ctx,
2134 REG_MHL_CBUS_CTL0, VAL_MHL_CBUS_CTL0_CBUS_DRV_SEL_STRONG
2135 | VAL_MHL_CBUS_CTL0_CBUS_RGND_VBIAS_734,
2136 REG_MHL_CBUS_CTL1, VAL_MHL_CBUS_CTL1_1115_OHM,
2137 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN,
2140 ret = sii8620_clear_error(ctx);
2142 dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
2146 enable_irq(to_i2c_client(ctx->dev)->irq);
2149 static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
2151 struct rc_dev *rc_dev;
2154 rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
2156 dev_err(ctx->dev, "Failed to allocate RC device\n");
2157 ctx->error = -ENOMEM;
2161 rc_dev->input_phys = "sii8620/input0";
2162 rc_dev->input_id.bustype = BUS_VIRTUAL;
2163 rc_dev->map_name = RC_MAP_CEC;
2164 rc_dev->allowed_protocols = RC_PROTO_BIT_CEC;
2165 rc_dev->driver_name = "sii8620";
2166 rc_dev->device_name = "sii8620";
2168 ret = rc_register_device(rc_dev);
2171 dev_err(ctx->dev, "Failed to register RC device\n");
2173 rc_free_device(ctx->rc_dev);
2176 ctx->rc_dev = rc_dev;
2179 static void sii8620_cable_out(struct sii8620 *ctx)
2181 disable_irq(to_i2c_client(ctx->dev)->irq);
2182 sii8620_hw_off(ctx);
2185 static void sii8620_extcon_work(struct work_struct *work)
2187 struct sii8620 *ctx =
2188 container_of(work, struct sii8620, extcon_wq);
2189 int state = extcon_get_state(ctx->extcon, EXTCON_DISP_MHL);
2191 if (state == ctx->cable_state)
2194 ctx->cable_state = state;
2197 sii8620_cable_in(ctx);
2199 sii8620_cable_out(ctx);
2202 static int sii8620_extcon_notifier(struct notifier_block *self,
2203 unsigned long event, void *ptr)
2205 struct sii8620 *ctx =
2206 container_of(self, struct sii8620, extcon_nb);
2208 schedule_work(&ctx->extcon_wq);
2213 static int sii8620_extcon_init(struct sii8620 *ctx)
2215 struct extcon_dev *edev;
2216 struct device_node *musb, *muic;
2219 /* get micro-USB connector node */
2220 musb = of_graph_get_remote_node(ctx->dev->of_node, 1, -1);
2221 /* next get micro-USB Interface Controller node */
2222 muic = of_get_next_parent(musb);
2225 dev_info(ctx->dev, "no extcon found, switching to 'always on' mode\n");
2229 edev = extcon_find_edev_by_node(muic);
2232 if (PTR_ERR(edev) == -EPROBE_DEFER)
2233 return -EPROBE_DEFER;
2234 dev_err(ctx->dev, "Invalid or missing extcon\n");
2235 return PTR_ERR(edev);
2239 ctx->extcon_nb.notifier_call = sii8620_extcon_notifier;
2240 INIT_WORK(&ctx->extcon_wq, sii8620_extcon_work);
2241 ret = extcon_register_notifier(edev, EXTCON_DISP_MHL, &ctx->extcon_nb);
2243 dev_err(ctx->dev, "failed to register notifier for MHL\n");
2250 static inline struct sii8620 *bridge_to_sii8620(struct drm_bridge *bridge)
2252 return container_of(bridge, struct sii8620, bridge);
2255 static int sii8620_attach(struct drm_bridge *bridge)
2257 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2259 sii8620_init_rcp_input_dev(ctx);
2261 return sii8620_clear_error(ctx);
2264 static void sii8620_detach(struct drm_bridge *bridge)
2266 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2268 rc_unregister_device(ctx->rc_dev);
2271 static enum drm_mode_status sii8620_mode_valid(struct drm_bridge *bridge,
2272 const struct drm_display_mode *mode)
2274 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2275 bool can_pack = ctx->devcap[MHL_DCAP_VID_LINK_MODE] &
2276 MHL_DCAP_VID_LINK_PPIXEL;
2277 unsigned int max_pclk = sii8620_is_mhl3(ctx) ? MHL3_MAX_LCLK :
2279 max_pclk /= can_pack ? 2 : 3;
2281 return (mode->clock > max_pclk) ? MODE_CLOCK_HIGH : MODE_OK;
2284 static bool sii8620_mode_fixup(struct drm_bridge *bridge,
2285 const struct drm_display_mode *mode,
2286 struct drm_display_mode *adjusted_mode)
2288 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2292 mutex_lock(&ctx->lock);
2294 max_lclk = sii8620_is_mhl3(ctx) ? MHL3_MAX_LCLK : MHL1_MAX_LCLK;
2295 if (max_lclk > 3 * adjusted_mode->clock) {
2296 ctx->use_packed_pixel = 0;
2299 if ((ctx->devcap[MHL_DCAP_VID_LINK_MODE] & MHL_DCAP_VID_LINK_PPIXEL) &&
2300 max_lclk > 2 * adjusted_mode->clock) {
2301 ctx->use_packed_pixel = 1;
2307 u8 vic = drm_match_cea_mode(adjusted_mode);
2310 union hdmi_infoframe frm;
2311 u8 mhl_vic[] = { 0, 95, 94, 93, 98 };
2313 /* FIXME: We need the connector here */
2314 drm_hdmi_vendor_infoframe_from_display_mode(
2315 &frm.vendor.hdmi, NULL, adjusted_mode);
2316 vic = frm.vendor.hdmi.vic;
2317 if (vic >= ARRAY_SIZE(mhl_vic))
2321 ctx->video_code = vic;
2322 ctx->pixel_clock = adjusted_mode->clock;
2324 mutex_unlock(&ctx->lock);
2328 static const struct drm_bridge_funcs sii8620_bridge_funcs = {
2329 .attach = sii8620_attach,
2330 .detach = sii8620_detach,
2331 .mode_fixup = sii8620_mode_fixup,
2332 .mode_valid = sii8620_mode_valid,
2335 static int sii8620_probe(struct i2c_client *client,
2336 const struct i2c_device_id *id)
2338 struct device *dev = &client->dev;
2339 struct sii8620 *ctx;
2342 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
2347 mutex_init(&ctx->lock);
2348 INIT_LIST_HEAD(&ctx->mt_queue);
2350 ctx->clk_xtal = devm_clk_get(dev, "xtal");
2351 if (IS_ERR(ctx->clk_xtal)) {
2352 dev_err(dev, "failed to get xtal clock from DT\n");
2353 return PTR_ERR(ctx->clk_xtal);
2357 dev_err(dev, "no irq provided\n");
2360 irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
2361 ret = devm_request_threaded_irq(dev, client->irq, NULL,
2363 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2366 dev_err(dev, "failed to install IRQ handler\n");
2370 ctx->gpio_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
2371 if (IS_ERR(ctx->gpio_reset)) {
2372 dev_err(dev, "failed to get reset gpio from DT\n");
2373 return PTR_ERR(ctx->gpio_reset);
2376 ctx->supplies[0].supply = "cvcc10";
2377 ctx->supplies[1].supply = "iovcc18";
2378 ret = devm_regulator_bulk_get(dev, 2, ctx->supplies);
2382 ret = sii8620_extcon_init(ctx);
2384 dev_err(ctx->dev, "failed to initialize EXTCON\n");
2388 i2c_set_clientdata(client, ctx);
2390 ctx->bridge.funcs = &sii8620_bridge_funcs;
2391 ctx->bridge.of_node = dev->of_node;
2392 drm_bridge_add(&ctx->bridge);
2395 sii8620_cable_in(ctx);
2400 static int sii8620_remove(struct i2c_client *client)
2402 struct sii8620 *ctx = i2c_get_clientdata(client);
2405 extcon_unregister_notifier(ctx->extcon, EXTCON_DISP_MHL,
2407 flush_work(&ctx->extcon_wq);
2408 if (ctx->cable_state > 0)
2409 sii8620_cable_out(ctx);
2411 sii8620_cable_out(ctx);
2413 drm_bridge_remove(&ctx->bridge);
2418 static const struct of_device_id sii8620_dt_match[] = {
2419 { .compatible = "sil,sii8620" },
2422 MODULE_DEVICE_TABLE(of, sii8620_dt_match);
2424 static const struct i2c_device_id sii8620_id[] = {
2429 MODULE_DEVICE_TABLE(i2c, sii8620_id);
2430 static struct i2c_driver sii8620_driver = {
2433 .of_match_table = of_match_ptr(sii8620_dt_match),
2435 .probe = sii8620_probe,
2436 .remove = sii8620_remove,
2437 .id_table = sii8620_id,
2440 module_i2c_driver(sii8620_driver);
2441 MODULE_LICENSE("GPL v2");