blob: d1e780fba4b6c58c8d12bbed76b7d15f3ac538a5 [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>
Maciej Purski68883842018-02-27 08:11:34 +010020#include <linux/extcon.h>
Andrzej Hajdace6e1532016-10-10 09:39:17 +020021#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>
Maciej Purski68883842018-02-27 08:11:34 +010029#include <linux/of_graph.h>
Andrzej Hajdace6e1532016-10-10 09:39:17 +020030#include <linux/regulator/consumer.h>
31#include <linux/slab.h>
32
Maciej Purskie25f1f72017-08-24 10:58:07 +020033#include <media/rc-core.h>
34
Andrzej Hajdace6e1532016-10-10 09:39:17 +020035#include "sil-sii8620.h"
36
Andrzej Hajdae19e9c62017-02-01 08:47:34 +010037#define SII8620_BURST_BUF_LEN 288
38#define VAL_RX_HDMI_CTRL2_DEFVAL VAL_RX_HDMI_CTRL2_IDLE_CNT(3)
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +010039#define MHL1_MAX_LCLK 225000
40#define MHL3_MAX_LCLK 600000
Andrzej Hajdace6e1532016-10-10 09:39:17 +020041
42enum sii8620_mode {
43 CM_DISCONNECTED,
44 CM_DISCOVERY,
45 CM_MHL1,
46 CM_MHL3,
47 CM_ECBUS_S
48};
49
50enum sii8620_sink_type {
51 SINK_NONE,
52 SINK_HDMI,
53 SINK_DVI
54};
55
56enum sii8620_mt_state {
57 MT_STATE_READY,
58 MT_STATE_BUSY,
59 MT_STATE_DONE
60};
61
62struct sii8620 {
63 struct drm_bridge bridge;
64 struct device *dev;
Maciej Purskie25f1f72017-08-24 10:58:07 +020065 struct rc_dev *rc_dev;
Andrzej Hajdace6e1532016-10-10 09:39:17 +020066 struct clk *clk_xtal;
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 */
71 int error;
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +010072 int pixel_clock;
73 unsigned int use_packed_pixel:1;
74 int video_code;
Andrzej Hajdace6e1532016-10-10 09:39:17 +020075 enum sii8620_mode mode;
76 enum sii8620_sink_type sink_type;
77 u8 cbus_status;
78 u8 stat[MHL_DST_SIZE];
79 u8 xstat[MHL_XDS_SIZE];
80 u8 devcap[MHL_DCAP_SIZE];
81 u8 xdevcap[MHL_XDC_SIZE];
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +010082 u8 avif[HDMI_INFOFRAME_SIZE(AVI)];
Andrzej Hajdace6e1532016-10-10 09:39:17 +020083 struct edid *edid;
84 unsigned int gen2_write_burst:1;
85 enum sii8620_mt_state mt_state;
Maciej Purski68883842018-02-27 08:11:34 +010086 struct extcon_dev *extcon;
87 struct notifier_block extcon_nb;
88 struct work_struct extcon_wq;
89 int cable_state;
Andrzej Hajdace6e1532016-10-10 09:39:17 +020090 struct list_head mt_queue;
Andrzej Hajdae19e9c62017-02-01 08:47:34 +010091 struct {
92 int r_size;
93 int r_count;
94 int rx_ack;
95 int rx_count;
96 u8 rx_buf[32];
97 int tx_count;
98 u8 tx_buf[32];
99 } burst;
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200100};
101
102struct sii8620_mt_msg;
103
104typedef void (*sii8620_mt_msg_cb)(struct sii8620 *ctx,
105 struct sii8620_mt_msg *msg);
106
Andrzej Hajda0c2d1872017-02-01 08:47:31 +0100107typedef void (*sii8620_cb)(struct sii8620 *ctx, int ret);
108
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200109struct sii8620_mt_msg {
110 struct list_head node;
111 u8 reg[4];
112 u8 ret;
113 sii8620_mt_msg_cb send;
114 sii8620_mt_msg_cb recv;
Andrzej Hajda0c2d1872017-02-01 08:47:31 +0100115 sii8620_cb continuation;
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200116};
117
118static 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 */
123 0x5d, /* MHL Spec */
124 0x64, /* MHL CBUS */
125 0x59, /* Hardware TPI (Transmitter Programming Interface) */
126 0x61, /* eCBUS-S, eCBUS-D */
127};
128
129static void sii8620_fetch_edid(struct sii8620 *ctx);
130static void sii8620_set_upstream_edid(struct sii8620 *ctx);
131static void sii8620_enable_hpd(struct sii8620 *ctx);
132static void sii8620_mhl_disconnected(struct sii8620 *ctx);
Andrzej Hajda2c8fb852017-02-01 08:47:32 +0100133static void sii8620_disconnect(struct sii8620 *ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200134
135static int sii8620_clear_error(struct sii8620 *ctx)
136{
137 int ret = ctx->error;
138
139 ctx->error = 0;
140 return ret;
141}
142
143static void sii8620_read_buf(struct sii8620 *ctx, u16 addr, u8 *buf, int len)
144{
145 struct device *dev = ctx->dev;
146 struct i2c_client *client = to_i2c_client(dev);
147 u8 data = addr;
148 struct i2c_msg msg[] = {
149 {
150 .addr = sii8620_i2c_page[addr >> 8],
151 .flags = client->flags,
152 .len = 1,
153 .buf = &data
154 },
155 {
156 .addr = sii8620_i2c_page[addr >> 8],
157 .flags = client->flags | I2C_M_RD,
158 .len = len,
159 .buf = buf
160 },
161 };
162 int ret;
163
164 if (ctx->error)
165 return;
166
167 ret = i2c_transfer(client->adapter, msg, 2);
168 dev_dbg(dev, "read at %04x: %*ph, %d\n", addr, len, buf, ret);
169
170 if (ret != 2) {
171 dev_err(dev, "Read at %#06x of %d bytes failed with code %d.\n",
172 addr, len, ret);
173 ctx->error = ret < 0 ? ret : -EIO;
174 }
175}
176
177static u8 sii8620_readb(struct sii8620 *ctx, u16 addr)
178{
179 u8 ret;
180
181 sii8620_read_buf(ctx, addr, &ret, 1);
182 return ret;
183}
184
185static void sii8620_write_buf(struct sii8620 *ctx, u16 addr, const u8 *buf,
186 int len)
187{
188 struct device *dev = ctx->dev;
189 struct i2c_client *client = to_i2c_client(dev);
190 u8 data[2];
191 struct i2c_msg msg = {
192 .addr = sii8620_i2c_page[addr >> 8],
193 .flags = client->flags,
194 .len = len + 1,
195 };
196 int ret;
197
198 if (ctx->error)
199 return;
200
201 if (len > 1) {
202 msg.buf = kmalloc(len + 1, GFP_KERNEL);
203 if (!msg.buf) {
204 ctx->error = -ENOMEM;
205 return;
206 }
207 memcpy(msg.buf + 1, buf, len);
208 } else {
209 msg.buf = data;
210 msg.buf[1] = *buf;
211 }
212
213 msg.buf[0] = addr;
214
215 ret = i2c_transfer(client->adapter, &msg, 1);
216 dev_dbg(dev, "write at %04x: %*ph, %d\n", addr, len, buf, ret);
217
218 if (ret != 1) {
219 dev_err(dev, "Write at %#06x of %*ph failed with code %d.\n",
220 addr, len, buf, ret);
221 ctx->error = ret ?: -EIO;
222 }
223
224 if (len > 1)
225 kfree(msg.buf);
226}
227
228#define sii8620_write(ctx, addr, arr...) \
229({\
230 u8 d[] = { arr }; \
231 sii8620_write_buf(ctx, addr, d, ARRAY_SIZE(d)); \
232})
233
234static void __sii8620_write_seq(struct sii8620 *ctx, const u16 *seq, int len)
235{
236 int i;
237
238 for (i = 0; i < len; i += 2)
239 sii8620_write(ctx, seq[i], seq[i + 1]);
240}
241
242#define sii8620_write_seq(ctx, seq...) \
243({\
244 const u16 d[] = { seq }; \
245 __sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
246})
247
248#define sii8620_write_seq_static(ctx, seq...) \
249({\
250 static const u16 d[] = { seq }; \
251 __sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
252})
253
254static void sii8620_setbits(struct sii8620 *ctx, u16 addr, u8 mask, u8 val)
255{
256 val = (val & mask) | (sii8620_readb(ctx, addr) & ~mask);
257 sii8620_write(ctx, addr, val);
258}
259
Andrzej Hajdabb4954c2017-02-01 08:47:29 +0100260static inline bool sii8620_is_mhl3(struct sii8620 *ctx)
261{
262 return ctx->mode >= CM_MHL3;
263}
264
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200265static void sii8620_mt_cleanup(struct sii8620 *ctx)
266{
267 struct sii8620_mt_msg *msg, *n;
268
269 list_for_each_entry_safe(msg, n, &ctx->mt_queue, node) {
270 list_del(&msg->node);
271 kfree(msg);
272 }
273 ctx->mt_state = MT_STATE_READY;
274}
275
276static void sii8620_mt_work(struct sii8620 *ctx)
277{
278 struct sii8620_mt_msg *msg;
279
280 if (ctx->error)
281 return;
282 if (ctx->mt_state == MT_STATE_BUSY || list_empty(&ctx->mt_queue))
283 return;
284
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,
288 node);
Andrzej Hajdad6d59c52017-02-01 08:47:38 +0100289 list_del(&msg->node);
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200290 if (msg->recv)
291 msg->recv(ctx, msg);
Andrzej Hajda0c2d1872017-02-01 08:47:31 +0100292 if (msg->continuation)
293 msg->continuation(ctx, msg->ret);
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200294 kfree(msg);
295 }
296
297 if (ctx->mt_state != MT_STATE_READY || list_empty(&ctx->mt_queue))
298 return;
299
300 ctx->mt_state = MT_STATE_BUSY;
301 msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
302 if (msg->send)
303 msg->send(ctx, msg);
304}
305
Andrzej Hajda26a4cef2017-02-01 08:47:41 +0100306static void sii8620_enable_gen2_write_burst(struct sii8620 *ctx)
307{
308 u8 ctrl = BIT_MDT_RCV_CTRL_MDT_RCV_EN;
309
310 if (ctx->gen2_write_burst)
311 return;
312
313 if (ctx->mode >= CM_MHL1)
314 ctrl |= BIT_MDT_RCV_CTRL_MDT_DELAY_RCV_EN;
315
316 sii8620_write_seq(ctx,
317 REG_MDT_RCV_TIMEOUT, 100,
318 REG_MDT_RCV_CTRL, ctrl
319 );
320 ctx->gen2_write_burst = 1;
321}
322
323static void sii8620_disable_gen2_write_burst(struct sii8620 *ctx)
324{
325 if (!ctx->gen2_write_burst)
326 return;
327
328 sii8620_write_seq_static(ctx,
329 REG_MDT_XMIT_CTRL, 0,
330 REG_MDT_RCV_CTRL, 0
331 );
332 ctx->gen2_write_burst = 0;
333}
334
335static void sii8620_start_gen2_write_burst(struct sii8620 *ctx)
336{
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
345 );
346 sii8620_enable_gen2_write_burst(ctx);
347}
348
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200349static void sii8620_mt_msc_cmd_send(struct sii8620 *ctx,
350 struct sii8620_mt_msg *msg)
351{
Andrzej Hajda26a4cef2017-02-01 08:47:41 +0100352 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);
356 else
357 sii8620_disable_gen2_write_burst(ctx);
358
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200359 switch (msg->reg[0]) {
360 case MHL_WRITE_STAT:
361 case MHL_SET_INT:
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);
365 break;
366 case MHL_MSC_MSG:
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);
370 break;
Andrzej Hajdae9c6da22017-02-01 08:47:30 +0100371 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);
376 break;
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200377 default:
378 dev_err(ctx->dev, "%s: command %#x not supported\n", __func__,
379 msg->reg[0]);
380 }
381}
382
383static struct sii8620_mt_msg *sii8620_mt_msg_new(struct sii8620 *ctx)
384{
385 struct sii8620_mt_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
386
387 if (!msg)
388 ctx->error = -ENOMEM;
389 else
390 list_add_tail(&msg->node, &ctx->mt_queue);
391
392 return msg;
393}
394
Andrzej Hajda0c2d1872017-02-01 08:47:31 +0100395static void sii8620_mt_set_cont(struct sii8620 *ctx, sii8620_cb cont)
396{
397 struct sii8620_mt_msg *msg;
398
399 if (ctx->error)
400 return;
401
402 if (list_empty(&ctx->mt_queue)) {
403 ctx->error = -EINVAL;
404 return;
405 }
406 msg = list_last_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
407 msg->continuation = cont;
408}
409
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200410static void sii8620_mt_msc_cmd(struct sii8620 *ctx, u8 cmd, u8 arg1, u8 arg2)
411{
412 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
413
414 if (!msg)
415 return;
416
417 msg->reg[0] = cmd;
418 msg->reg[1] = arg1;
419 msg->reg[2] = arg2;
420 msg->send = sii8620_mt_msc_cmd_send;
421}
422
423static void sii8620_mt_write_stat(struct sii8620 *ctx, u8 reg, u8 val)
424{
425 sii8620_mt_msc_cmd(ctx, MHL_WRITE_STAT, reg, val);
426}
427
428static inline void sii8620_mt_set_int(struct sii8620 *ctx, u8 irq, u8 mask)
429{
430 sii8620_mt_msc_cmd(ctx, MHL_SET_INT, irq, mask);
431}
432
433static void sii8620_mt_msc_msg(struct sii8620 *ctx, u8 cmd, u8 data)
434{
435 sii8620_mt_msc_cmd(ctx, MHL_MSC_MSG, cmd, data);
436}
437
438static void sii8620_mt_rap(struct sii8620 *ctx, u8 code)
439{
440 sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RAP, code);
441}
442
Maciej Purskie25f1f72017-08-24 10:58:07 +0200443static void sii8620_mt_rcpk(struct sii8620 *ctx, u8 code)
444{
445 sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RCPK, code);
446}
447
448static void sii8620_mt_rcpe(struct sii8620 *ctx, u8 code)
449{
450 sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RCPE, code);
451}
452
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200453static void sii8620_mt_read_devcap_send(struct sii8620 *ctx,
454 struct sii8620_mt_msg *msg)
455{
456 u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
457 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
458 | BIT_EDID_CTRL_EDID_MODE_EN;
459
460 if (msg->reg[0] == MHL_READ_XDEVCAP)
461 ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
462
463 sii8620_write_seq(ctx,
464 REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE,
465 REG_EDID_CTRL, ctrl,
466 REG_TPI_CBUS_START, BIT_TPI_CBUS_START_GET_DEVCAP_START
467 );
468}
469
470/* copy src to dst and set changed bits in src */
471static void sii8620_update_array(u8 *dst, u8 *src, int count)
472{
473 while (--count >= 0) {
474 *src ^= *dst;
475 *dst++ ^= *src++;
476 }
477}
478
Andrzej Hajda9a466cd2017-02-01 08:47:40 +0100479static void sii8620_sink_detected(struct sii8620 *ctx, int ret)
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200480{
481 static const char * const sink_str[] = {
482 [SINK_NONE] = "NONE",
483 [SINK_HDMI] = "HDMI",
484 [SINK_DVI] = "DVI"
485 };
486
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200487 char sink_name[20];
488 struct device *dev = ctx->dev;
489
Andrzej Hajda9a466cd2017-02-01 08:47:40 +0100490 if (ret < 0)
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200491 return;
492
493 sii8620_fetch_edid(ctx);
494 if (!ctx->edid) {
495 dev_err(ctx->dev, "Cannot fetch EDID\n");
496 sii8620_mhl_disconnected(ctx);
497 return;
498 }
499
500 if (drm_detect_hdmi_monitor(ctx->edid))
501 ctx->sink_type = SINK_HDMI;
502 else
503 ctx->sink_type = SINK_DVI;
504
505 drm_edid_get_monitor_name(ctx->edid, sink_name, ARRAY_SIZE(sink_name));
506
507 dev_info(dev, "detected sink(type: %s): %s\n",
508 sink_str[ctx->sink_type], sink_name);
Andrzej Hajda263b5c92017-02-01 08:47:44 +0100509}
510
Andrzej Hajdaa21e6582017-02-01 08:47:50 +0100511static void sii8620_hsic_init(struct sii8620 *ctx)
512{
513 if (!sii8620_is_mhl3(ctx))
514 return;
515
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,
525 REG_TDMLLCTL, 0,
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,
529 REG_HRXINTL, 0xff,
530 REG_HRXINTH, 0xff,
531 REG_TTXINTL, 0xff,
532 REG_TTXINTH, 0xff,
533 REG_TRXINTL, 0xff,
534 REG_TRXINTH, 0xff,
535 REG_HTXINTL, 0xff,
536 REG_HTXINTH, 0xff,
537 REG_FCINTR0, 0xff,
538 REG_FCINTR1, 0xff,
539 REG_FCINTR2, 0xff,
540 REG_FCINTR3, 0xff,
541 REG_FCINTR4, 0xff,
542 REG_FCINTR5, 0xff,
543 REG_FCINTR6, 0xff,
544 REG_FCINTR7, 0xff
545 );
546}
547
Andrzej Hajda263b5c92017-02-01 08:47:44 +0100548static void sii8620_edid_read(struct sii8620 *ctx, int ret)
549{
550 if (ret < 0)
551 return;
552
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200553 sii8620_set_upstream_edid(ctx);
Andrzej Hajdaa21e6582017-02-01 08:47:50 +0100554 sii8620_hsic_init(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200555 sii8620_enable_hpd(ctx);
556}
557
Andrzej Hajda9a466cd2017-02-01 08:47:40 +0100558static void sii8620_mr_devcap(struct sii8620 *ctx)
559{
560 u8 dcap[MHL_DCAP_SIZE];
561 struct device *dev = ctx->dev;
562
563 sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, dcap, MHL_DCAP_SIZE);
564 if (ctx->error < 0)
565 return;
566
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);
573}
574
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200575static void sii8620_mr_xdevcap(struct sii8620 *ctx)
576{
577 sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, ctx->xdevcap,
578 MHL_XDC_SIZE);
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200579}
580
581static void sii8620_mt_read_devcap_recv(struct sii8620 *ctx,
582 struct sii8620_mt_msg *msg)
583{
584 u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
585 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
586 | BIT_EDID_CTRL_EDID_MODE_EN;
587
588 if (msg->reg[0] == MHL_READ_XDEVCAP)
589 ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
590
591 sii8620_write_seq(ctx,
592 REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE | BIT_INTR9_EDID_DONE
593 | BIT_INTR9_EDID_ERROR,
594 REG_EDID_CTRL, ctrl,
595 REG_EDID_FIFO_ADDR, 0
596 );
597
598 if (msg->reg[0] == MHL_READ_XDEVCAP)
599 sii8620_mr_xdevcap(ctx);
600 else
601 sii8620_mr_devcap(ctx);
602}
603
604static void sii8620_mt_read_devcap(struct sii8620 *ctx, bool xdevcap)
605{
606 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
607
608 if (!msg)
609 return;
610
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;
614}
615
Andrzej Hajdae9c6da22017-02-01 08:47:30 +0100616static void sii8620_mt_read_devcap_reg_recv(struct sii8620 *ctx,
617 struct sii8620_mt_msg *msg)
618{
Maciej Purski79964db2017-08-21 12:32:51 +0200619 u8 reg = msg->reg[1] & 0x7f;
Andrzej Hajdae9c6da22017-02-01 08:47:30 +0100620
Maciej Purski79964db2017-08-21 12:32:51 +0200621 if (msg->reg[1] & 0x80)
Andrzej Hajdae9c6da22017-02-01 08:47:30 +0100622 ctx->xdevcap[reg] = msg->ret;
623 else
624 ctx->devcap[reg] = msg->ret;
625}
626
627static void sii8620_mt_read_devcap_reg(struct sii8620 *ctx, u8 reg)
628{
629 struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
630
631 if (!msg)
632 return;
633
634 msg->reg[0] = (reg & 0x80) ? MHL_READ_XDEVCAP_REG : MHL_READ_DEVCAP_REG;
635 msg->reg[1] = reg;
636 msg->send = sii8620_mt_msc_cmd_send;
637 msg->recv = sii8620_mt_read_devcap_reg_recv;
638}
639
640static inline void sii8620_mt_read_xdevcap_reg(struct sii8620 *ctx, u8 reg)
641{
642 sii8620_mt_read_devcap_reg(ctx, reg | 0x80);
643}
644
Andrzej Hajdae19e9c62017-02-01 08:47:34 +0100645static void *sii8620_burst_get_tx_buf(struct sii8620 *ctx, int len)
646{
647 u8 *buf = &ctx->burst.tx_buf[ctx->burst.tx_count];
648 int size = len + 2;
649
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;
653 return NULL;
654 }
655
656 ctx->burst.tx_count += size;
657 buf[1] = len;
658
659 return buf + 2;
660}
661
662static u8 *sii8620_burst_get_rx_buf(struct sii8620 *ctx, int len)
663{
664 u8 *buf = &ctx->burst.rx_buf[ctx->burst.rx_count];
665 int size = len + 1;
666
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;
670 return NULL;
671 }
672
673 ctx->burst.rx_count += size;
674 buf[0] = len;
675
676 return buf + 1;
677}
678
679static void sii8620_burst_send(struct sii8620 *ctx)
680{
681 int tx_left = ctx->burst.tx_count;
682 u8 *d = ctx->burst.tx_buf;
683
684 while (tx_left > 0) {
685 int len = d[1] + 2;
686
687 if (ctx->burst.r_count + len > ctx->burst.r_size)
688 break;
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;
693 tx_left -= len;
694 d += len;
695 }
696
697 ctx->burst.tx_count = tx_left;
698
699 while (ctx->burst.rx_ack > 0) {
700 u8 b[2] = { min(ctx->burst.rx_ack, 255), 0 };
701
702 if (ctx->burst.r_count + 2 > ctx->burst.r_size)
703 break;
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;
707 }
708}
709
710static void sii8620_burst_receive(struct sii8620 *ctx)
711{
712 u8 buf[3], *d;
713 int count;
714
715 sii8620_read_buf(ctx, REG_EMSCRFIFOBCNTL, buf, 2);
716 count = get_unaligned_le16(buf);
717 while (count > 0) {
718 int len = min(count, 3);
719
720 sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, buf, len);
721 count -= 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;
726
727 if (len < 3 || !buf[2])
728 continue;
729
730 len = buf[2];
731 d = sii8620_burst_get_rx_buf(ctx, len);
732 if (!d)
733 continue;
734 sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, d, len);
735 count -= len;
736 ctx->burst.rx_ack += len;
737 }
738}
739
740static void sii8620_burst_tx_rbuf_info(struct sii8620 *ctx, int size)
741{
742 struct mhl_burst_blk_rcv_buffer_info *d =
743 sii8620_burst_get_tx_buf(ctx, sizeof(*d));
744 if (!d)
745 return;
746
747 d->id = cpu_to_be16(MHL_BURST_ID_BLK_RCV_BUFFER_INFO);
748 d->size = cpu_to_le16(size);
749}
750
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +0100751static u8 sii8620_checksum(void *ptr, int size)
752{
753 u8 *d = ptr, sum = 0;
754
755 while (size--)
756 sum += *d++;
757
758 return sum;
759}
760
761static void sii8620_mhl_burst_hdr_set(struct mhl3_burst_header *h,
762 enum mhl_burst_id id)
763{
764 h->id = cpu_to_be16(id);
765 h->total_entries = 1;
766 h->sequence_index = 1;
767}
768
769static void sii8620_burst_tx_bits_per_pixel_fmt(struct sii8620 *ctx, u8 fmt)
770{
771 struct mhl_burst_bits_per_pixel_fmt *d;
772 const int size = sizeof(*d) + sizeof(d->desc[0]);
773
774 d = sii8620_burst_get_tx_buf(ctx, size);
775 if (!d)
776 return;
777
778 sii8620_mhl_burst_hdr_set(&d->hdr, MHL_BURST_ID_BITS_PER_PIXEL_FMT);
779 d->num_entries = 1;
780 d->desc[0].stream_id = 0;
781 d->desc[0].pixel_format = fmt;
782 d->hdr.checksum -= sii8620_checksum(d, size);
783}
784
Andrzej Hajdae19e9c62017-02-01 08:47:34 +0100785static void sii8620_burst_rx_all(struct sii8620 *ctx)
786{
787 u8 *d = ctx->burst.rx_buf;
788 int count = ctx->burst.rx_count;
789
790 while (count-- > 0) {
791 int len = *d++;
792 int id = get_unaligned_be16(&d[0]);
793
794 switch (id) {
795 case MHL_BURST_ID_BLK_RCV_BUFFER_INFO:
796 ctx->burst.r_size = get_unaligned_le16(&d[2]);
797 break;
798 default:
799 break;
800 }
801 count -= len;
802 d += len;
803 }
804 ctx->burst.rx_count = 0;
805}
806
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200807static void sii8620_fetch_edid(struct sii8620 *ctx)
808{
809 u8 lm_ddc, ddc_cmd, int3, cbus;
810 int fetched, i;
811 int edid_len = EDID_LENGTH;
812 u8 *edid;
813
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);
817
818 sii8620_write_seq(ctx,
819 REG_INTR9_MASK, 0,
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,
824 );
825
826 for (i = 0; i < 256; ++i) {
827 u8 ddc_stat = sii8620_readb(ctx, REG_DDC_STATUS);
828
829 if (!(ddc_stat & BIT_DDC_STATUS_DDC_I2C_IN_PROG))
830 break;
831 sii8620_write(ctx, REG_DDC_STATUS,
832 BIT_DDC_STATUS_DDC_FIFO_EMPTY);
833 }
834
835 sii8620_write(ctx, REG_DDC_ADDR, 0x50 << 1);
836
837 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
838 if (!edid) {
839 ctx->error = -ENOMEM;
840 return;
841 }
842
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
850 );
851 sii8620_write_seq(ctx,
852 REG_DDC_SEGM, fetched >> 8,
853 REG_DDC_OFFSET, fetched & 0xff,
854 REG_DDC_DIN_CNT1, FETCH_SIZE,
855 REG_DDC_DIN_CNT2, 0,
856 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_ENH_DDC_READ_NO_ACK
857 );
858
859 do {
860 int3 = sii8620_readb(ctx, REG_INTR3);
861 cbus = sii8620_readb(ctx, REG_CBUS_STATUS);
862
863 if (int3 & BIT_DDC_CMD_DONE)
864 break;
865
866 if (!(cbus & BIT_CBUS_STATUS_CBUS_CONNECTED)) {
867 kfree(edid);
868 edid = NULL;
869 goto end;
870 }
871 } while (1);
872
873 sii8620_readb(ctx, REG_DDC_STATUS);
874 while (sii8620_readb(ctx, REG_DDC_DOUT_CNT) < FETCH_SIZE)
875 usleep_range(10, 20);
876
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;
880
881 if (ext) {
882 u8 *new_edid;
883
884 edid_len += ext * EDID_LENGTH;
885 new_edid = krealloc(edid, edid_len, GFP_KERNEL);
886 if (!new_edid) {
887 kfree(edid);
888 ctx->error = -ENOMEM;
889 return;
890 }
891 edid = new_edid;
892 }
893 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200894 }
895
Andrzej Hajda263b5c92017-02-01 08:47:44 +0100896 sii8620_write_seq(ctx,
897 REG_INTR3_MASK, BIT_DDC_CMD_DONE,
898 REG_LM_DDC, lm_ddc
899 );
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200900
901end:
902 kfree(ctx->edid);
903 ctx->edid = (struct edid *)edid;
904}
905
906static void sii8620_set_upstream_edid(struct sii8620 *ctx)
907{
908 sii8620_setbits(ctx, REG_DPD, BIT_DPD_PDNRX12 | BIT_DPD_PDIDCK_N
909 | BIT_DPD_PD_MHL_CLK_N, 0xff);
910
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
916 );
917
918 sii8620_setbits(ctx, REG_RX_HDMI_CLR_BUFFER,
919 BIT_RX_HDMI_CLR_BUFFER_VSI_CLR_EN, 0xff);
920
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,
925 );
926
927 sii8620_write_buf(ctx, REG_EDID_FIFO_WR_DATA, (u8 *)ctx->edid,
928 (ctx->edid->extensions + 1) * EDID_LENGTH);
929
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,
935 REG_INTR9_MASK, 0
936 );
937}
938
939static void sii8620_xtal_set_rate(struct sii8620 *ctx)
940{
941 static const struct {
942 unsigned int rate;
943 u8 div;
944 u8 tp1;
945 } rates[] = {
946 { 19200, 0x04, 0x53 },
947 { 20000, 0x04, 0x62 },
948 { 24000, 0x05, 0x75 },
949 { 30000, 0x06, 0x92 },
950 { 38400, 0x0c, 0xbc },
951 };
952 unsigned long rate = clk_get_rate(ctx->clk_xtal) / 1000;
953 int i;
954
955 for (i = 0; i < ARRAY_SIZE(rates) - 1; ++i)
956 if (rate <= rates[i].rate)
957 break;
958
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);
962
963 sii8620_write(ctx, REG_DIV_CTL_MAIN, rates[i].div);
964 sii8620_write(ctx, REG_HDCP2X_TP1, rates[i].tp1);
965}
966
967static int sii8620_hw_on(struct sii8620 *ctx)
968{
969 int ret;
970
971 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
972 if (ret)
973 return ret;
Andrzej Hajda8efaac02018-06-08 08:04:57 +0200974
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200975 usleep_range(10000, 20000);
Andrzej Hajda8efaac02018-06-08 08:04:57 +0200976 ret = clk_prepare_enable(ctx->clk_xtal);
977 if (ret)
978 return ret;
979
980 msleep(100);
981 gpiod_set_value(ctx->gpio_reset, 0);
982 msleep(100);
983
984 return 0;
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200985}
986
987static int sii8620_hw_off(struct sii8620 *ctx)
988{
989 clk_disable_unprepare(ctx->clk_xtal);
990 gpiod_set_value(ctx->gpio_reset, 1);
991 return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
992}
993
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200994static void sii8620_cbus_reset(struct sii8620 *ctx)
995{
Andrzej Hajda4dc3c072017-02-01 08:47:43 +0100996 sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST
997 | BIT_PWD_SRST_CBUS_RST_SW_EN);
998 usleep_range(10000, 20000);
999 sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001000}
1001
1002static void sii8620_set_auto_zone(struct sii8620 *ctx)
1003{
1004 if (ctx->mode != CM_MHL1) {
1005 sii8620_write_seq_static(ctx,
1006 REG_TX_ZONE_CTL1, 0x0,
1007 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1008 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1009 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
1010 );
1011 } else {
1012 sii8620_write_seq_static(ctx,
1013 REG_TX_ZONE_CTL1, VAL_TX_ZONE_CTL1_TX_ZONE_CTRL_MODE,
1014 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1015 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
1016 );
1017 }
1018}
1019
1020static void sii8620_stop_video(struct sii8620 *ctx)
1021{
1022 u8 uninitialized_var(val);
1023
1024 sii8620_write_seq_static(ctx,
1025 REG_TPI_INTR_EN, 0,
1026 REG_HDCP2X_INTR0_MASK, 0,
1027 REG_TPI_COPP_DATA2, 0,
1028 REG_TPI_INTR_ST0, ~0,
1029 );
1030
1031 switch (ctx->sink_type) {
1032 case SINK_DVI:
1033 val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
1034 | BIT_TPI_SC_TPI_AV_MUTE;
1035 break;
1036 case SINK_HDMI:
Andrzej Hajdaef822a02017-02-01 08:47:39 +01001037 default:
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001038 val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
1039 | BIT_TPI_SC_TPI_AV_MUTE
1040 | BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI;
1041 break;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001042 }
1043
1044 sii8620_write(ctx, REG_TPI_SC, val);
1045}
1046
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001047static void sii8620_set_format(struct sii8620 *ctx)
1048{
1049 u8 out_fmt;
1050
1051 if (sii8620_is_mhl3(ctx)) {
1052 sii8620_setbits(ctx, REG_M3_P0CTRL,
1053 BIT_M3_P0CTRL_MHL3_P0_PIXEL_MODE_PACKED,
1054 ctx->use_packed_pixel ? ~0 : 0);
1055 } else {
1056 if (ctx->use_packed_pixel)
1057 sii8620_write_seq_static(ctx,
1058 REG_VID_MODE, BIT_VID_MODE_M1080P,
1059 REG_MHL_TOP_CTL, BIT_MHL_TOP_CTL_MHL_PP_SEL | 1,
1060 REG_MHLTX_CTL6, 0x60
1061 );
1062 else
1063 sii8620_write_seq_static(ctx,
1064 REG_VID_MODE, 0,
1065 REG_MHL_TOP_CTL, 1,
1066 REG_MHLTX_CTL6, 0xa0
1067 );
1068 }
1069
1070 if (ctx->use_packed_pixel)
1071 out_fmt = VAL_TPI_FORMAT(YCBCR422, FULL) |
1072 BIT_TPI_OUTPUT_CSCMODE709;
1073 else
1074 out_fmt = VAL_TPI_FORMAT(RGB, FULL);
1075
1076 sii8620_write_seq(ctx,
1077 REG_TPI_INPUT, VAL_TPI_FORMAT(RGB, FULL),
1078 REG_TPI_OUTPUT, out_fmt,
1079 );
1080}
1081
1082static int mhl3_infoframe_init(struct mhl3_infoframe *frame)
1083{
1084 memset(frame, 0, sizeof(*frame));
1085
1086 frame->version = 3;
1087 frame->hev_format = -1;
1088 return 0;
1089}
1090
1091static ssize_t mhl3_infoframe_pack(struct mhl3_infoframe *frame,
1092 void *buffer, size_t size)
1093{
1094 const int frm_len = HDMI_INFOFRAME_HEADER_SIZE + MHL3_INFOFRAME_SIZE;
1095 u8 *ptr = buffer;
1096
1097 if (size < frm_len)
1098 return -ENOSPC;
1099
1100 memset(buffer, 0, size);
1101 ptr[0] = HDMI_INFOFRAME_TYPE_VENDOR;
1102 ptr[1] = frame->version;
1103 ptr[2] = MHL3_INFOFRAME_SIZE;
1104 ptr[4] = MHL3_IEEE_OUI & 0xff;
1105 ptr[5] = (MHL3_IEEE_OUI >> 8) & 0xff;
1106 ptr[6] = (MHL3_IEEE_OUI >> 16) & 0xff;
1107 ptr[7] = frame->video_format & 0x3;
1108 ptr[7] |= (frame->format_type & 0x7) << 2;
1109 ptr[7] |= frame->sep_audio ? BIT(5) : 0;
1110 if (frame->hev_format >= 0) {
1111 ptr[9] = 1;
1112 ptr[10] = (frame->hev_format >> 8) & 0xff;
1113 ptr[11] = frame->hev_format & 0xff;
1114 }
1115 if (frame->av_delay) {
1116 bool sign = frame->av_delay < 0;
1117 int delay = sign ? -frame->av_delay : frame->av_delay;
1118
1119 ptr[12] = (delay >> 16) & 0xf;
1120 if (sign)
1121 ptr[12] |= BIT(4);
1122 ptr[13] = (delay >> 8) & 0xff;
1123 ptr[14] = delay & 0xff;
1124 }
1125 ptr[3] -= sii8620_checksum(buffer, frm_len);
1126 return frm_len;
1127}
1128
1129static void sii8620_set_infoframes(struct sii8620 *ctx)
1130{
1131 struct mhl3_infoframe mhl_frm;
1132 union hdmi_infoframe frm;
1133 u8 buf[31];
1134 int ret;
1135
1136 if (!sii8620_is_mhl3(ctx) || !ctx->use_packed_pixel) {
1137 sii8620_write(ctx, REG_TPI_SC,
1138 BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI);
1139 sii8620_write_buf(ctx, REG_TPI_AVI_CHSUM, ctx->avif + 3,
1140 ARRAY_SIZE(ctx->avif) - 3);
1141 sii8620_write(ctx, REG_PKT_FILTER_0,
1142 BIT_PKT_FILTER_0_DROP_CEA_GAMUT_PKT |
1143 BIT_PKT_FILTER_0_DROP_MPEG_PKT |
1144 BIT_PKT_FILTER_0_DROP_GCP_PKT,
1145 BIT_PKT_FILTER_1_DROP_GEN_PKT);
1146 return;
1147 }
1148
1149 ret = hdmi_avi_infoframe_init(&frm.avi);
1150 frm.avi.colorspace = HDMI_COLORSPACE_YUV422;
1151 frm.avi.active_aspect = HDMI_ACTIVE_ASPECT_PICTURE;
1152 frm.avi.picture_aspect = HDMI_PICTURE_ASPECT_16_9;
1153 frm.avi.colorimetry = HDMI_COLORIMETRY_ITU_709;
1154 frm.avi.video_code = ctx->video_code;
1155 if (!ret)
1156 ret = hdmi_avi_infoframe_pack(&frm.avi, buf, ARRAY_SIZE(buf));
1157 if (ret > 0)
1158 sii8620_write_buf(ctx, REG_TPI_AVI_CHSUM, buf + 3, ret - 3);
1159 sii8620_write(ctx, REG_PKT_FILTER_0,
1160 BIT_PKT_FILTER_0_DROP_CEA_GAMUT_PKT |
1161 BIT_PKT_FILTER_0_DROP_MPEG_PKT |
1162 BIT_PKT_FILTER_0_DROP_AVI_PKT |
1163 BIT_PKT_FILTER_0_DROP_GCP_PKT,
1164 BIT_PKT_FILTER_1_VSI_OVERRIDE_DIS |
1165 BIT_PKT_FILTER_1_DROP_GEN_PKT |
1166 BIT_PKT_FILTER_1_DROP_VSIF_PKT);
1167
1168 sii8620_write(ctx, REG_TPI_INFO_FSEL, BIT_TPI_INFO_FSEL_EN
1169 | BIT_TPI_INFO_FSEL_RPT | VAL_TPI_INFO_FSEL_VSI);
1170 ret = mhl3_infoframe_init(&mhl_frm);
1171 if (!ret)
1172 ret = mhl3_infoframe_pack(&mhl_frm, buf, ARRAY_SIZE(buf));
1173 sii8620_write_buf(ctx, REG_TPI_INFO_B0, buf, ret);
1174}
1175
Maciej Purski5c632e22017-11-09 11:53:42 +01001176static void sii8620_start_video(struct sii8620 *ctx)
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001177{
Maciej Purski5c632e22017-11-09 11:53:42 +01001178 if (!sii8620_is_mhl3(ctx))
1179 sii8620_stop_video(ctx);
1180
1181 if (ctx->sink_type == SINK_DVI && !sii8620_is_mhl3(ctx)) {
1182 sii8620_write(ctx, REG_RX_HDMI_CTRL2,
1183 VAL_RX_HDMI_CTRL2_DEFVAL);
1184 sii8620_write(ctx, REG_TPI_SC, 0);
1185 return;
1186 }
1187
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001188 sii8620_write_seq_static(ctx,
1189 REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL
1190 | BIT_RX_HDMI_CTRL2_USE_AV_MUTE,
1191 REG_VID_OVRRD, BIT_VID_OVRRD_PP_AUTO_DISABLE
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001192 | BIT_VID_OVRRD_M1080P_OVRRD);
1193 sii8620_set_format(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001194
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001195 if (!sii8620_is_mhl3(ctx)) {
1196 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1197 MHL_DST_LM_CLK_MODE_NORMAL | MHL_DST_LM_PATH_ENABLED);
1198 sii8620_set_auto_zone(ctx);
1199 } else {
1200 static const struct {
1201 int max_clk;
1202 u8 zone;
1203 u8 link_rate;
1204 u8 rrp_decode;
1205 } clk_spec[] = {
1206 { 150000, VAL_TX_ZONE_CTL3_TX_ZONE_1_5GBPS,
1207 MHL_XDS_LINK_RATE_1_5_GBPS, 0x38 },
1208 { 300000, VAL_TX_ZONE_CTL3_TX_ZONE_3GBPS,
1209 MHL_XDS_LINK_RATE_3_0_GBPS, 0x40 },
1210 { 600000, VAL_TX_ZONE_CTL3_TX_ZONE_6GBPS,
1211 MHL_XDS_LINK_RATE_6_0_GBPS, 0x40 },
1212 };
1213 u8 p0_ctrl = BIT_M3_P0CTRL_MHL3_P0_PORT_EN;
1214 int clk = ctx->pixel_clock * (ctx->use_packed_pixel ? 2 : 3);
1215 int i;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001216
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001217 for (i = 0; i < ARRAY_SIZE(clk_spec); ++i)
1218 if (clk < clk_spec[i].max_clk)
1219 break;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001220
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001221 if (100 * clk >= 98 * clk_spec[i].max_clk)
1222 p0_ctrl |= BIT_M3_P0CTRL_MHL3_P0_UNLIMIT_EN;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001223
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001224 sii8620_burst_tx_bits_per_pixel_fmt(ctx, ctx->use_packed_pixel);
1225 sii8620_burst_send(ctx);
1226 sii8620_write_seq(ctx,
1227 REG_MHL_DP_CTL0, 0xf0,
1228 REG_MHL3_TX_ZONE_CTL, clk_spec[i].zone);
1229 sii8620_setbits(ctx, REG_M3_P0CTRL,
1230 BIT_M3_P0CTRL_MHL3_P0_PORT_EN
1231 | BIT_M3_P0CTRL_MHL3_P0_UNLIMIT_EN, p0_ctrl);
1232 sii8620_setbits(ctx, REG_M3_POSTM, MSK_M3_POSTM_RRP_DECODE,
1233 clk_spec[i].rrp_decode);
1234 sii8620_write_seq_static(ctx,
1235 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE
1236 | BIT_M3_CTRL_H2M_SWRST,
1237 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE
1238 );
1239 sii8620_mt_write_stat(ctx, MHL_XDS_REG(AVLINK_MODE_CONTROL),
1240 clk_spec[i].link_rate);
1241 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001242
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001243 sii8620_set_infoframes(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001244}
1245
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001246static void sii8620_disable_hpd(struct sii8620 *ctx)
1247{
1248 sii8620_setbits(ctx, REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID, 0);
1249 sii8620_write_seq_static(ctx,
1250 REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN,
1251 REG_INTR8_MASK, 0
1252 );
1253}
1254
1255static void sii8620_enable_hpd(struct sii8620 *ctx)
1256{
1257 sii8620_setbits(ctx, REG_TMDS_CSTAT_P3,
1258 BIT_TMDS_CSTAT_P3_SCDT_CLR_AVI_DIS
1259 | BIT_TMDS_CSTAT_P3_CLR_AVI, ~0);
1260 sii8620_write_seq_static(ctx,
1261 REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN
1262 | BIT_HPD_CTRL_HPD_HIGH,
1263 );
1264}
1265
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001266static void sii8620_mhl_discover(struct sii8620 *ctx)
1267{
1268 sii8620_write_seq_static(ctx,
1269 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1270 | BIT_DISC_CTRL9_DISC_PULSE_PROCEED,
1271 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_5K, VAL_PUP_20K),
1272 REG_CBUS_DISC_INTR0_MASK, BIT_MHL3_EST_INT
1273 | BIT_MHL_EST_INT
1274 | BIT_NOT_MHL_EST_INT
1275 | BIT_CBUS_MHL3_DISCON_INT
1276 | BIT_CBUS_MHL12_DISCON_INT
1277 | BIT_RGND_READY_INT,
1278 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1279 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1280 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
1281 REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
1282 | BIT_MHL_DP_CTL0_TX_OE_OVR,
1283 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
1284 REG_MHL_DP_CTL1, 0xA2,
1285 REG_MHL_DP_CTL2, 0x03,
1286 REG_MHL_DP_CTL3, 0x35,
1287 REG_MHL_DP_CTL5, 0x02,
1288 REG_MHL_DP_CTL6, 0x02,
1289 REG_MHL_DP_CTL7, 0x03,
1290 REG_COC_CTLC, 0xFF,
1291 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
1292 | BIT_DPD_OSC_EN | BIT_DPD_PWRON_HSIC,
1293 REG_COC_INTR_MASK, BIT_COC_PLL_LOCK_STATUS_CHANGE
1294 | BIT_COC_CALIBRATION_DONE,
1295 REG_CBUS_INT_1_MASK, BIT_CBUS_MSC_ABORT_RCVD
1296 | BIT_CBUS_CMD_ABORT,
1297 REG_CBUS_INT_0_MASK, BIT_CBUS_MSC_MT_DONE
1298 | BIT_CBUS_HPD_CHG
1299 | BIT_CBUS_MSC_MR_WRITE_STAT
1300 | BIT_CBUS_MSC_MR_MSC_MSG
1301 | BIT_CBUS_MSC_MR_WRITE_BURST
1302 | BIT_CBUS_MSC_MR_SET_INT
1303 | BIT_CBUS_MSC_MT_DONE_NACK
1304 );
1305}
1306
1307static void sii8620_peer_specific_init(struct sii8620 *ctx)
1308{
Andrzej Hajdabb4954c2017-02-01 08:47:29 +01001309 if (sii8620_is_mhl3(ctx))
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001310 sii8620_write_seq_static(ctx,
1311 REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD,
1312 REG_EMSCINTRMASK1,
1313 BIT_EMSCINTR1_EMSC_TRAINING_COMMA_ERR
1314 );
1315 else
1316 sii8620_write_seq_static(ctx,
1317 REG_HDCP2X_INTR0_MASK, 0x00,
1318 REG_EMSCINTRMASK1, 0x00,
1319 REG_HDCP2X_INTR0, 0xFF,
1320 REG_INTR1, 0xFF,
1321 REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD
1322 | BIT_SYS_CTRL1_TX_CTRL_HDMI
1323 );
1324}
1325
1326#define SII8620_MHL_VERSION 0x32
1327#define SII8620_SCRATCHPAD_SIZE 16
1328#define SII8620_INT_STAT_SIZE 0x33
1329
1330static void sii8620_set_dev_cap(struct sii8620 *ctx)
1331{
1332 static const u8 devcap[MHL_DCAP_SIZE] = {
1333 [MHL_DCAP_MHL_VERSION] = SII8620_MHL_VERSION,
1334 [MHL_DCAP_CAT] = MHL_DCAP_CAT_SOURCE | MHL_DCAP_CAT_POWER,
1335 [MHL_DCAP_ADOPTER_ID_H] = 0x01,
1336 [MHL_DCAP_ADOPTER_ID_L] = 0x41,
1337 [MHL_DCAP_VID_LINK_MODE] = MHL_DCAP_VID_LINK_RGB444
1338 | MHL_DCAP_VID_LINK_PPIXEL
1339 | MHL_DCAP_VID_LINK_16BPP,
1340 [MHL_DCAP_AUD_LINK_MODE] = MHL_DCAP_AUD_LINK_2CH,
1341 [MHL_DCAP_VIDEO_TYPE] = MHL_DCAP_VT_GRAPHICS,
1342 [MHL_DCAP_LOG_DEV_MAP] = MHL_DCAP_LD_GUI,
1343 [MHL_DCAP_BANDWIDTH] = 0x0f,
1344 [MHL_DCAP_FEATURE_FLAG] = MHL_DCAP_FEATURE_RCP_SUPPORT
1345 | MHL_DCAP_FEATURE_RAP_SUPPORT
1346 | MHL_DCAP_FEATURE_SP_SUPPORT,
1347 [MHL_DCAP_SCRATCHPAD_SIZE] = SII8620_SCRATCHPAD_SIZE,
1348 [MHL_DCAP_INT_STAT_SIZE] = SII8620_INT_STAT_SIZE,
1349 };
1350 static const u8 xdcap[MHL_XDC_SIZE] = {
1351 [MHL_XDC_ECBUS_SPEEDS] = MHL_XDC_ECBUS_S_075
1352 | MHL_XDC_ECBUS_S_8BIT,
1353 [MHL_XDC_TMDS_SPEEDS] = MHL_XDC_TMDS_150
1354 | MHL_XDC_TMDS_300 | MHL_XDC_TMDS_600,
1355 [MHL_XDC_ECBUS_ROLES] = MHL_XDC_DEV_HOST,
1356 [MHL_XDC_LOG_DEV_MAPX] = MHL_XDC_LD_PHONE,
1357 };
1358
1359 sii8620_write_buf(ctx, REG_MHL_DEVCAP_0, devcap, ARRAY_SIZE(devcap));
1360 sii8620_write_buf(ctx, REG_MHL_EXTDEVCAP_0, xdcap, ARRAY_SIZE(xdcap));
1361}
1362
1363static void sii8620_mhl_init(struct sii8620 *ctx)
1364{
1365 sii8620_write_seq_static(ctx,
1366 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1367 REG_CBUS_MSC_COMPAT_CTRL,
1368 BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN,
1369 );
1370
1371 sii8620_peer_specific_init(ctx);
1372
1373 sii8620_disable_hpd(ctx);
1374
1375 sii8620_write_seq_static(ctx,
1376 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
1377 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1378 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1379 REG_TMDS0_CCTRL1, 0x90,
1380 REG_TMDS_CLK_EN, 0x01,
1381 REG_TMDS_CH_EN, 0x11,
1382 REG_BGR_BIAS, 0x87,
1383 REG_ALICE0_ZONE_CTRL, 0xE8,
1384 REG_ALICE0_MODE_CTRL, 0x04,
1385 );
1386 sii8620_setbits(ctx, REG_LM_DDC, BIT_LM_DDC_SW_TPI_EN_DISABLED, 0);
1387 sii8620_write_seq_static(ctx,
1388 REG_TPI_HW_OPT3, 0x76,
1389 REG_TMDS_CCTRL, BIT_TMDS_CCTRL_TMDS_OE,
1390 REG_TPI_DTD_B2, 79,
1391 );
1392 sii8620_set_dev_cap(ctx);
1393 sii8620_write_seq_static(ctx,
1394 REG_MDT_XMIT_TIMEOUT, 100,
1395 REG_MDT_XMIT_CTRL, 0x03,
1396 REG_MDT_XFIFO_STAT, 0x00,
1397 REG_MDT_RCV_TIMEOUT, 100,
1398 REG_CBUS_LINK_CTRL_8, 0x1D,
1399 );
1400
1401 sii8620_start_gen2_write_burst(ctx);
1402 sii8620_write_seq_static(ctx,
1403 REG_BIST_CTRL, 0x00,
1404 REG_COC_CTL1, 0x10,
1405 REG_COC_CTL2, 0x18,
1406 REG_COC_CTLF, 0x07,
1407 REG_COC_CTL11, 0xF8,
1408 REG_COC_CTL17, 0x61,
1409 REG_COC_CTL18, 0x46,
1410 REG_COC_CTL19, 0x15,
1411 REG_COC_CTL1A, 0x01,
1412 REG_MHL_COC_CTL3, BIT_MHL_COC_CTL3_COC_AECHO_EN,
1413 REG_MHL_COC_CTL4, 0x2D,
1414 REG_MHL_COC_CTL5, 0xF9,
1415 REG_MSC_HEARTBEAT_CTRL, 0x27,
1416 );
1417 sii8620_disable_gen2_write_burst(ctx);
1418
Andrzej Hajda9fc6ade2017-02-01 08:47:51 +01001419 sii8620_mt_write_stat(ctx, MHL_DST_REG(VERSION), SII8620_MHL_VERSION);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001420 sii8620_mt_write_stat(ctx, MHL_DST_REG(CONNECTED_RDY),
1421 MHL_DST_CONN_DCAP_RDY | MHL_DST_CONN_XDEVCAPP_SUPP
1422 | MHL_DST_CONN_POW_STAT);
1423 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE), MHL_INT_RC_DCAP_CHG);
1424}
1425
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001426static void sii8620_emsc_enable(struct sii8620 *ctx)
1427{
1428 u8 reg;
1429
1430 sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_EMSC_EN
1431 | BIT_GENCTL_CLR_EMSC_RFIFO
1432 | BIT_GENCTL_CLR_EMSC_XFIFO, ~0);
1433 sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_CLR_EMSC_RFIFO
1434 | BIT_GENCTL_CLR_EMSC_XFIFO, 0);
1435 sii8620_setbits(ctx, REG_COMMECNT, BIT_COMMECNT_I2C_TO_EMSC_EN, ~0);
1436 reg = sii8620_readb(ctx, REG_EMSCINTR);
1437 sii8620_write(ctx, REG_EMSCINTR, reg);
1438 sii8620_write(ctx, REG_EMSCINTRMASK, BIT_EMSCINTR_SPI_DVLD);
1439}
1440
1441static int sii8620_wait_for_fsm_state(struct sii8620 *ctx, u8 state)
1442{
1443 int i;
1444
1445 for (i = 0; i < 10; ++i) {
1446 u8 s = sii8620_readb(ctx, REG_COC_STAT_0);
1447
1448 if ((s & MSK_COC_STAT_0_FSM_STATE) == state)
1449 return 0;
1450 if (!(s & BIT_COC_STAT_0_PLL_LOCKED))
1451 return -EBUSY;
1452 usleep_range(4000, 6000);
1453 }
1454 return -ETIMEDOUT;
1455}
1456
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001457static void sii8620_set_mode(struct sii8620 *ctx, enum sii8620_mode mode)
1458{
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001459 int ret;
1460
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001461 if (ctx->mode == mode)
1462 return;
1463
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001464 switch (mode) {
1465 case CM_MHL1:
1466 sii8620_write_seq_static(ctx,
1467 REG_CBUS_MSC_COMPAT_CTRL, 0x02,
1468 REG_M3_CTRL, VAL_M3_CTRL_MHL1_2_VALUE,
1469 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
1470 | BIT_DPD_OSC_EN,
1471 REG_COC_INTR_MASK, 0
1472 );
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001473 ctx->mode = mode;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001474 break;
1475 case CM_MHL3:
Andrzej Hajdadd123122017-02-01 08:47:28 +01001476 sii8620_write(ctx, REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE);
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001477 ctx->mode = mode;
1478 return;
1479 case CM_ECBUS_S:
1480 sii8620_emsc_enable(ctx);
1481 sii8620_write_seq_static(ctx,
1482 REG_TTXSPINUMS, 4,
1483 REG_TRXSPINUMS, 4,
1484 REG_TTXHSICNUMS, 0x14,
1485 REG_TRXHSICNUMS, 0x14,
1486 REG_TTXTOTNUMS, 0x18,
1487 REG_TRXTOTNUMS, 0x18,
1488 REG_PWD_SRST, BIT_PWD_SRST_COC_DOC_RST
1489 | BIT_PWD_SRST_CBUS_RST_SW_EN,
1490 REG_MHL_COC_CTL1, 0xbd,
1491 REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN,
1492 REG_COC_CTLB, 0x01,
1493 REG_COC_CTL0, 0x5c,
1494 REG_COC_CTL14, 0x03,
1495 REG_COC_CTL15, 0x80,
1496 REG_MHL_DP_CTL6, BIT_MHL_DP_CTL6_DP_TAP1_SGN
1497 | BIT_MHL_DP_CTL6_DP_TAP1_EN
1498 | BIT_MHL_DP_CTL6_DT_PREDRV_FEEDCAP_EN,
1499 REG_MHL_DP_CTL8, 0x03
1500 );
1501 ret = sii8620_wait_for_fsm_state(ctx, 0x03);
1502 sii8620_write_seq_static(ctx,
1503 REG_COC_CTL14, 0x00,
1504 REG_COC_CTL15, 0x80
1505 );
1506 if (!ret)
1507 sii8620_write(ctx, REG_CBUS3_CNVT, 0x85);
1508 else
1509 sii8620_disconnect(ctx);
Andrzej Hajdadd123122017-02-01 08:47:28 +01001510 return;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001511 case CM_DISCONNECTED:
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001512 ctx->mode = mode;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001513 break;
1514 default:
1515 dev_err(ctx->dev, "%s mode %d not supported\n", __func__, mode);
1516 break;
kbuild test robot3a81e962016-10-27 00:58:36 +08001517 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001518
1519 sii8620_set_auto_zone(ctx);
1520
1521 if (mode != CM_MHL1)
1522 return;
1523
1524 sii8620_write_seq_static(ctx,
1525 REG_MHL_DP_CTL0, 0xBC,
1526 REG_MHL_DP_CTL1, 0xBB,
1527 REG_MHL_DP_CTL3, 0x48,
1528 REG_MHL_DP_CTL5, 0x39,
1529 REG_MHL_DP_CTL2, 0x2A,
1530 REG_MHL_DP_CTL6, 0x2A,
1531 REG_MHL_DP_CTL7, 0x08
1532 );
1533}
1534
1535static void sii8620_disconnect(struct sii8620 *ctx)
1536{
1537 sii8620_disable_gen2_write_burst(ctx);
1538 sii8620_stop_video(ctx);
Andrzej Hajda003f99292017-02-01 08:47:45 +01001539 msleep(100);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001540 sii8620_cbus_reset(ctx);
1541 sii8620_set_mode(ctx, CM_DISCONNECTED);
1542 sii8620_write_seq_static(ctx,
Andrzej Hajda003f99292017-02-01 08:47:45 +01001543 REG_TX_ZONE_CTL1, 0,
1544 REG_MHL_PLL_CTL0, 0x07,
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001545 REG_COC_CTL0, 0x40,
1546 REG_CBUS3_CNVT, 0x84,
1547 REG_COC_CTL14, 0x00,
1548 REG_COC_CTL0, 0x40,
1549 REG_HRXCTRL3, 0x07,
1550 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1551 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1552 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
1553 REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
1554 | BIT_MHL_DP_CTL0_TX_OE_OVR,
1555 REG_MHL_DP_CTL1, 0xBB,
1556 REG_MHL_DP_CTL3, 0x48,
1557 REG_MHL_DP_CTL5, 0x3F,
1558 REG_MHL_DP_CTL2, 0x2F,
1559 REG_MHL_DP_CTL6, 0x2A,
1560 REG_MHL_DP_CTL7, 0x03
1561 );
1562 sii8620_disable_hpd(ctx);
1563 sii8620_write_seq_static(ctx,
1564 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
1565 REG_MHL_COC_CTL1, 0x07,
1566 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1567 REG_DISC_CTRL8, 0x00,
1568 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1569 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1570 REG_INT_CTRL, 0x00,
1571 REG_MSC_HEARTBEAT_CTRL, 0x27,
1572 REG_DISC_CTRL1, 0x25,
1573 REG_CBUS_DISC_INTR0, (u8)~BIT_RGND_READY_INT,
1574 REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT,
1575 REG_MDT_INT_1, 0xff,
1576 REG_MDT_INT_1_MASK, 0x00,
1577 REG_MDT_INT_0, 0xff,
1578 REG_MDT_INT_0_MASK, 0x00,
1579 REG_COC_INTR, 0xff,
1580 REG_COC_INTR_MASK, 0x00,
1581 REG_TRXINTH, 0xff,
1582 REG_TRXINTMH, 0x00,
1583 REG_CBUS_INT_0, 0xff,
1584 REG_CBUS_INT_0_MASK, 0x00,
1585 REG_CBUS_INT_1, 0xff,
1586 REG_CBUS_INT_1_MASK, 0x00,
1587 REG_EMSCINTR, 0xff,
1588 REG_EMSCINTRMASK, 0x00,
1589 REG_EMSCINTR1, 0xff,
1590 REG_EMSCINTRMASK1, 0x00,
1591 REG_INTR8, 0xff,
1592 REG_INTR8_MASK, 0x00,
1593 REG_TPI_INTR_ST0, 0xff,
1594 REG_TPI_INTR_EN, 0x00,
1595 REG_HDCP2X_INTR0, 0xff,
1596 REG_HDCP2X_INTR0_MASK, 0x00,
1597 REG_INTR9, 0xff,
1598 REG_INTR9_MASK, 0x00,
1599 REG_INTR3, 0xff,
1600 REG_INTR3_MASK, 0x00,
1601 REG_INTR5, 0xff,
1602 REG_INTR5_MASK, 0x00,
1603 REG_INTR2, 0xff,
1604 REG_INTR2_MASK, 0x00,
1605 );
1606 memset(ctx->stat, 0, sizeof(ctx->stat));
1607 memset(ctx->xstat, 0, sizeof(ctx->xstat));
1608 memset(ctx->devcap, 0, sizeof(ctx->devcap));
1609 memset(ctx->xdevcap, 0, sizeof(ctx->xdevcap));
1610 ctx->cbus_status = 0;
1611 ctx->sink_type = SINK_NONE;
1612 kfree(ctx->edid);
1613 ctx->edid = NULL;
1614 sii8620_mt_cleanup(ctx);
1615}
1616
1617static void sii8620_mhl_disconnected(struct sii8620 *ctx)
1618{
1619 sii8620_write_seq_static(ctx,
1620 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1621 REG_CBUS_MSC_COMPAT_CTRL,
1622 BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN
1623 );
1624 sii8620_disconnect(ctx);
1625}
1626
1627static void sii8620_irq_disc(struct sii8620 *ctx)
1628{
1629 u8 stat = sii8620_readb(ctx, REG_CBUS_DISC_INTR0);
1630
1631 if (stat & VAL_CBUS_MHL_DISCON)
1632 sii8620_mhl_disconnected(ctx);
1633
1634 if (stat & BIT_RGND_READY_INT) {
1635 u8 stat2 = sii8620_readb(ctx, REG_DISC_STAT2);
1636
1637 if ((stat2 & MSK_DISC_STAT2_RGND) == VAL_RGND_1K) {
1638 sii8620_mhl_discover(ctx);
1639 } else {
1640 sii8620_write_seq_static(ctx,
1641 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1642 | BIT_DISC_CTRL9_NOMHL_EST
1643 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1644 REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT
1645 | BIT_CBUS_MHL3_DISCON_INT
1646 | BIT_CBUS_MHL12_DISCON_INT
1647 | BIT_NOT_MHL_EST_INT
1648 );
1649 }
1650 }
1651 if (stat & BIT_MHL_EST_INT)
1652 sii8620_mhl_init(ctx);
1653
1654 sii8620_write(ctx, REG_CBUS_DISC_INTR0, stat);
1655}
1656
Andrzej Hajda581a9232017-02-01 08:47:49 +01001657static void sii8620_read_burst(struct sii8620 *ctx)
1658{
1659 u8 buf[17];
1660
1661 sii8620_read_buf(ctx, REG_MDT_RCV_READ_PORT, buf, ARRAY_SIZE(buf));
1662 sii8620_write(ctx, REG_MDT_RCV_CTRL, BIT_MDT_RCV_CTRL_MDT_RCV_EN |
1663 BIT_MDT_RCV_CTRL_MDT_DELAY_RCV_EN |
1664 BIT_MDT_RCV_CTRL_MDT_RFIFO_CLR_CUR);
1665 sii8620_readb(ctx, REG_MDT_RFIFO_STAT);
1666}
1667
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001668static void sii8620_irq_g2wb(struct sii8620 *ctx)
1669{
1670 u8 stat = sii8620_readb(ctx, REG_MDT_INT_0);
1671
1672 if (stat & BIT_MDT_IDLE_AFTER_HAWB_DISABLE)
Andrzej Hajda581a9232017-02-01 08:47:49 +01001673 if (sii8620_is_mhl3(ctx))
1674 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
1675 MHL_INT_RC_FEAT_COMPLETE);
1676
1677 if (stat & BIT_MDT_RFIFO_DATA_RDY)
1678 sii8620_read_burst(ctx);
1679
1680 if (stat & BIT_MDT_XFIFO_EMPTY)
1681 sii8620_write(ctx, REG_MDT_XMIT_CTRL, 0);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001682
1683 sii8620_write(ctx, REG_MDT_INT_0, stat);
1684}
1685
Andrzej Hajda9fc6ade2017-02-01 08:47:51 +01001686static void sii8620_status_dcap_ready(struct sii8620 *ctx)
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001687{
Andrzej Hajda9fc6ade2017-02-01 08:47:51 +01001688 enum sii8620_mode mode;
1689
1690 mode = ctx->stat[MHL_DST_VERSION] >= 0x30 ? CM_MHL3 : CM_MHL1;
1691 if (mode > ctx->mode)
1692 sii8620_set_mode(ctx, mode);
1693 sii8620_peer_specific_init(ctx);
1694 sii8620_write(ctx, REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE
1695 | BIT_INTR9_EDID_DONE | BIT_INTR9_EDID_ERROR);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001696}
1697
1698static void sii8620_status_changed_path(struct sii8620 *ctx)
1699{
1700 if (ctx->stat[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED) {
1701 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1702 MHL_DST_LM_CLK_MODE_NORMAL
1703 | MHL_DST_LM_PATH_ENABLED);
Andrzej Hajdae3a65482017-02-01 08:47:36 +01001704 if (!sii8620_is_mhl3(ctx))
1705 sii8620_mt_read_devcap(ctx, false);
Andrzej Hajda9a466cd2017-02-01 08:47:40 +01001706 sii8620_mt_set_cont(ctx, sii8620_sink_detected);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001707 } else {
1708 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1709 MHL_DST_LM_CLK_MODE_NORMAL);
1710 }
1711}
1712
1713static void sii8620_msc_mr_write_stat(struct sii8620 *ctx)
1714{
1715 u8 st[MHL_DST_SIZE], xst[MHL_XDS_SIZE];
1716
1717 sii8620_read_buf(ctx, REG_MHL_STAT_0, st, MHL_DST_SIZE);
1718 sii8620_read_buf(ctx, REG_MHL_EXTSTAT_0, xst, MHL_XDS_SIZE);
1719
1720 sii8620_update_array(ctx->stat, st, MHL_DST_SIZE);
1721 sii8620_update_array(ctx->xstat, xst, MHL_XDS_SIZE);
1722
Andrzej Hajda9fc6ade2017-02-01 08:47:51 +01001723 if (ctx->stat[MHL_DST_CONNECTED_RDY] & MHL_DST_CONN_DCAP_RDY)
1724 sii8620_status_dcap_ready(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001725
1726 if (st[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED)
1727 sii8620_status_changed_path(ctx);
1728}
1729
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001730static void sii8620_ecbus_up(struct sii8620 *ctx, int ret)
1731{
1732 if (ret < 0)
1733 return;
1734
1735 sii8620_set_mode(ctx, CM_ECBUS_S);
1736}
1737
1738static void sii8620_got_ecbus_speed(struct sii8620 *ctx, int ret)
1739{
1740 if (ret < 0)
1741 return;
1742
1743 sii8620_mt_write_stat(ctx, MHL_XDS_REG(CURR_ECBUS_MODE),
1744 MHL_XDS_ECBUS_S | MHL_XDS_SLOT_MODE_8BIT);
1745 sii8620_mt_rap(ctx, MHL_RAP_CBUS_MODE_UP);
1746 sii8620_mt_set_cont(ctx, sii8620_ecbus_up);
1747}
1748
Andrzej Hajda269ed8e2017-02-01 08:47:48 +01001749static void sii8620_mhl_burst_emsc_support_set(struct mhl_burst_emsc_support *d,
1750 enum mhl_burst_id id)
1751{
1752 sii8620_mhl_burst_hdr_set(&d->hdr, MHL_BURST_ID_EMSC_SUPPORT);
1753 d->num_entries = 1;
1754 d->burst_id[0] = cpu_to_be16(id);
1755}
1756
1757static void sii8620_send_features(struct sii8620 *ctx)
1758{
1759 u8 buf[16];
1760
1761 sii8620_write(ctx, REG_MDT_XMIT_CTRL, BIT_MDT_XMIT_CTRL_EN
1762 | BIT_MDT_XMIT_CTRL_FIXED_BURST_LEN);
1763 sii8620_mhl_burst_emsc_support_set((void *)buf,
1764 MHL_BURST_ID_HID_PAYLOAD);
1765 sii8620_write_buf(ctx, REG_MDT_XMIT_WRITE_PORT, buf, ARRAY_SIZE(buf));
1766}
1767
Maciej Purskie25f1f72017-08-24 10:58:07 +02001768static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
1769{
1770 bool pressed = !(scancode & MHL_RCP_KEY_RELEASED_MASK);
1771
1772 scancode &= MHL_RCP_KEY_ID_MASK;
1773
1774 if (!ctx->rc_dev) {
1775 dev_dbg(ctx->dev, "RCP input device not initialized\n");
1776 return false;
1777 }
1778
1779 if (pressed)
1780 rc_keydown(ctx->rc_dev, RC_PROTO_CEC, scancode, 0);
1781 else
1782 rc_keyup(ctx->rc_dev);
1783
1784 return true;
1785}
1786
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001787static void sii8620_msc_mr_set_int(struct sii8620 *ctx)
1788{
1789 u8 ints[MHL_INT_SIZE];
1790
1791 sii8620_read_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
1792 sii8620_write_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001793
1794 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_DCAP_CHG) {
1795 switch (ctx->mode) {
1796 case CM_MHL3:
1797 sii8620_mt_read_xdevcap_reg(ctx, MHL_XDC_ECBUS_SPEEDS);
1798 sii8620_mt_set_cont(ctx, sii8620_got_ecbus_speed);
1799 break;
1800 case CM_ECBUS_S:
1801 sii8620_mt_read_devcap(ctx, true);
1802 break;
1803 default:
1804 break;
1805 }
1806 }
Andrzej Hajda269ed8e2017-02-01 08:47:48 +01001807 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_REQ)
1808 sii8620_send_features(ctx);
1809 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_COMPLETE)
1810 sii8620_edid_read(ctx, 0);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001811}
1812
1813static struct sii8620_mt_msg *sii8620_msc_msg_first(struct sii8620 *ctx)
1814{
1815 struct device *dev = ctx->dev;
1816
1817 if (list_empty(&ctx->mt_queue)) {
1818 dev_err(dev, "unexpected MSC MT response\n");
1819 return NULL;
1820 }
1821
1822 return list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
1823}
1824
1825static void sii8620_msc_mt_done(struct sii8620 *ctx)
1826{
1827 struct sii8620_mt_msg *msg = sii8620_msc_msg_first(ctx);
1828
1829 if (!msg)
1830 return;
1831
1832 msg->ret = sii8620_readb(ctx, REG_MSC_MT_RCVD_DATA0);
1833 ctx->mt_state = MT_STATE_DONE;
1834}
1835
1836static void sii8620_msc_mr_msc_msg(struct sii8620 *ctx)
1837{
Maciej Purskie25f1f72017-08-24 10:58:07 +02001838 struct sii8620_mt_msg *msg;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001839 u8 buf[2];
1840
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001841 sii8620_read_buf(ctx, REG_MSC_MR_MSC_MSG_RCVD_1ST_DATA, buf, 2);
1842
1843 switch (buf[0]) {
1844 case MHL_MSC_MSG_RAPK:
Maciej Purskie25f1f72017-08-24 10:58:07 +02001845 msg = sii8620_msc_msg_first(ctx);
1846 if (!msg)
1847 return;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001848 msg->ret = buf[1];
1849 ctx->mt_state = MT_STATE_DONE;
1850 break;
Maciej Purskie25f1f72017-08-24 10:58:07 +02001851 case MHL_MSC_MSG_RCP:
1852 if (!sii8620_rcp_consume(ctx, buf[1]))
1853 sii8620_mt_rcpe(ctx,
1854 MHL_RCPE_STATUS_INEFFECTIVE_KEY_CODE);
1855 sii8620_mt_rcpk(ctx, buf[1]);
1856 break;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001857 default:
1858 dev_err(ctx->dev, "%s message type %d,%d not supported",
1859 __func__, buf[0], buf[1]);
1860 }
1861}
1862
1863static void sii8620_irq_msc(struct sii8620 *ctx)
1864{
1865 u8 stat = sii8620_readb(ctx, REG_CBUS_INT_0);
1866
1867 if (stat & ~BIT_CBUS_HPD_CHG)
1868 sii8620_write(ctx, REG_CBUS_INT_0, stat & ~BIT_CBUS_HPD_CHG);
1869
1870 if (stat & BIT_CBUS_HPD_CHG) {
1871 u8 cbus_stat = sii8620_readb(ctx, REG_CBUS_STATUS);
1872
1873 if ((cbus_stat ^ ctx->cbus_status) & BIT_CBUS_STATUS_CBUS_HPD) {
1874 sii8620_write(ctx, REG_CBUS_INT_0, BIT_CBUS_HPD_CHG);
1875 } else {
1876 stat ^= BIT_CBUS_STATUS_CBUS_HPD;
1877 cbus_stat ^= BIT_CBUS_STATUS_CBUS_HPD;
1878 }
1879 ctx->cbus_status = cbus_stat;
1880 }
1881
1882 if (stat & BIT_CBUS_MSC_MR_WRITE_STAT)
1883 sii8620_msc_mr_write_stat(ctx);
1884
1885 if (stat & BIT_CBUS_MSC_MR_SET_INT)
1886 sii8620_msc_mr_set_int(ctx);
1887
1888 if (stat & BIT_CBUS_MSC_MT_DONE)
1889 sii8620_msc_mt_done(ctx);
1890
1891 if (stat & BIT_CBUS_MSC_MR_MSC_MSG)
1892 sii8620_msc_mr_msc_msg(ctx);
1893}
1894
1895static void sii8620_irq_coc(struct sii8620 *ctx)
1896{
1897 u8 stat = sii8620_readb(ctx, REG_COC_INTR);
1898
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001899 if (stat & BIT_COC_CALIBRATION_DONE) {
1900 u8 cstat = sii8620_readb(ctx, REG_COC_STAT_0);
1901
1902 cstat &= BIT_COC_STAT_0_PLL_LOCKED | MSK_COC_STAT_0_FSM_STATE;
1903 if (cstat == (BIT_COC_STAT_0_PLL_LOCKED | 0x02)) {
1904 sii8620_write_seq_static(ctx,
1905 REG_COC_CTLB, 0,
1906 REG_TRXINTMH, BIT_TDM_INTR_SYNC_DATA
1907 | BIT_TDM_INTR_SYNC_WAIT
1908 );
1909 }
1910 }
1911
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001912 sii8620_write(ctx, REG_COC_INTR, stat);
1913}
1914
1915static void sii8620_irq_merr(struct sii8620 *ctx)
1916{
1917 u8 stat = sii8620_readb(ctx, REG_CBUS_INT_1);
1918
1919 sii8620_write(ctx, REG_CBUS_INT_1, stat);
1920}
1921
1922static void sii8620_irq_edid(struct sii8620 *ctx)
1923{
1924 u8 stat = sii8620_readb(ctx, REG_INTR9);
1925
1926 sii8620_write(ctx, REG_INTR9, stat);
1927
1928 if (stat & BIT_INTR9_DEVCAP_DONE)
1929 ctx->mt_state = MT_STATE_DONE;
1930}
1931
1932static void sii8620_scdt_high(struct sii8620 *ctx)
1933{
1934 sii8620_write_seq_static(ctx,
1935 REG_INTR8_MASK, BIT_CEA_NEW_AVI | BIT_CEA_NEW_VSI,
1936 REG_TPI_SC, BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI,
1937 );
1938}
1939
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001940static void sii8620_irq_scdt(struct sii8620 *ctx)
1941{
1942 u8 stat = sii8620_readb(ctx, REG_INTR5);
1943
1944 if (stat & BIT_INTR_SCDT_CHANGE) {
1945 u8 cstat = sii8620_readb(ctx, REG_TMDS_CSTAT_P3);
1946
Maciej Purski5c632e22017-11-09 11:53:42 +01001947 if (cstat & BIT_TMDS_CSTAT_P3_SCDT) {
1948 if (ctx->sink_type == SINK_HDMI)
1949 /* enable infoframe interrupt */
1950 sii8620_scdt_high(ctx);
1951 else
1952 sii8620_start_video(ctx);
1953 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001954 }
1955
1956 sii8620_write(ctx, REG_INTR5, stat);
1957}
1958
1959static void sii8620_new_vsi(struct sii8620 *ctx)
1960{
1961 u8 vsif[11];
1962
1963 sii8620_write(ctx, REG_RX_HDMI_CTRL2,
1964 VAL_RX_HDMI_CTRL2_DEFVAL |
1965 BIT_RX_HDMI_CTRL2_VSI_MON_SEL_VSI);
1966 sii8620_read_buf(ctx, REG_RX_HDMI_MON_PKT_HEADER1, vsif,
1967 ARRAY_SIZE(vsif));
1968}
1969
1970static void sii8620_new_avi(struct sii8620 *ctx)
1971{
1972 sii8620_write(ctx, REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL);
1973 sii8620_read_buf(ctx, REG_RX_HDMI_MON_PKT_HEADER1, ctx->avif,
1974 ARRAY_SIZE(ctx->avif));
1975}
1976
1977static void sii8620_irq_infr(struct sii8620 *ctx)
1978{
1979 u8 stat = sii8620_readb(ctx, REG_INTR8)
1980 & (BIT_CEA_NEW_VSI | BIT_CEA_NEW_AVI);
1981
1982 sii8620_write(ctx, REG_INTR8, stat);
1983
1984 if (stat & BIT_CEA_NEW_VSI)
1985 sii8620_new_vsi(ctx);
1986
1987 if (stat & BIT_CEA_NEW_AVI)
1988 sii8620_new_avi(ctx);
1989
1990 if (stat & (BIT_CEA_NEW_VSI | BIT_CEA_NEW_AVI))
1991 sii8620_start_video(ctx);
1992}
1993
Andrzej Hajdae3a65482017-02-01 08:47:36 +01001994static void sii8620_got_xdevcap(struct sii8620 *ctx, int ret)
1995{
1996 if (ret < 0)
1997 return;
1998
1999 sii8620_mt_read_devcap(ctx, false);
2000}
2001
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002002static void sii8620_irq_tdm(struct sii8620 *ctx)
2003{
2004 u8 stat = sii8620_readb(ctx, REG_TRXINTH);
2005 u8 tdm = sii8620_readb(ctx, REG_TRXSTA2);
2006
2007 if ((tdm & MSK_TDM_SYNCHRONIZED) == VAL_TDM_SYNCHRONIZED) {
2008 ctx->mode = CM_ECBUS_S;
2009 ctx->burst.rx_ack = 0;
2010 ctx->burst.r_size = SII8620_BURST_BUF_LEN;
2011 sii8620_burst_tx_rbuf_info(ctx, SII8620_BURST_BUF_LEN);
2012 sii8620_mt_read_devcap(ctx, true);
Andrzej Hajdae3a65482017-02-01 08:47:36 +01002013 sii8620_mt_set_cont(ctx, sii8620_got_xdevcap);
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002014 } else {
2015 sii8620_write_seq_static(ctx,
2016 REG_MHL_PLL_CTL2, 0,
2017 REG_MHL_PLL_CTL2, BIT_MHL_PLL_CTL2_CLKDETECT_EN
2018 );
2019 }
2020
2021 sii8620_write(ctx, REG_TRXINTH, stat);
2022}
2023
2024static void sii8620_irq_block(struct sii8620 *ctx)
2025{
2026 u8 stat = sii8620_readb(ctx, REG_EMSCINTR);
2027
2028 if (stat & BIT_EMSCINTR_SPI_DVLD) {
2029 u8 bstat = sii8620_readb(ctx, REG_SPIBURSTSTAT);
2030
2031 if (bstat & BIT_SPIBURSTSTAT_EMSC_NORMAL_MODE)
2032 sii8620_burst_receive(ctx);
2033 }
2034
2035 sii8620_write(ctx, REG_EMSCINTR, stat);
2036}
2037
Andrzej Hajda263b5c92017-02-01 08:47:44 +01002038static void sii8620_irq_ddc(struct sii8620 *ctx)
2039{
2040 u8 stat = sii8620_readb(ctx, REG_INTR3);
2041
2042 if (stat & BIT_DDC_CMD_DONE) {
2043 sii8620_write(ctx, REG_INTR3_MASK, 0);
2044 if (sii8620_is_mhl3(ctx))
2045 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
2046 MHL_INT_RC_FEAT_REQ);
2047 else
2048 sii8620_edid_read(ctx, 0);
2049 }
2050 sii8620_write(ctx, REG_INTR3, stat);
2051}
2052
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002053/* endian agnostic, non-volatile version of test_bit */
2054static bool sii8620_test_bit(unsigned int nr, const u8 *addr)
2055{
2056 return 1 & (addr[nr / BITS_PER_BYTE] >> (nr % BITS_PER_BYTE));
2057}
2058
2059static irqreturn_t sii8620_irq_thread(int irq, void *data)
2060{
2061 static const struct {
2062 int bit;
2063 void (*handler)(struct sii8620 *ctx);
2064 } irq_vec[] = {
2065 { BIT_FAST_INTR_STAT_DISC, sii8620_irq_disc },
2066 { BIT_FAST_INTR_STAT_G2WB, sii8620_irq_g2wb },
2067 { BIT_FAST_INTR_STAT_COC, sii8620_irq_coc },
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002068 { BIT_FAST_INTR_STAT_TDM, sii8620_irq_tdm },
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002069 { BIT_FAST_INTR_STAT_MSC, sii8620_irq_msc },
2070 { BIT_FAST_INTR_STAT_MERR, sii8620_irq_merr },
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002071 { BIT_FAST_INTR_STAT_BLOCK, sii8620_irq_block },
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002072 { BIT_FAST_INTR_STAT_EDID, sii8620_irq_edid },
Andrzej Hajda263b5c92017-02-01 08:47:44 +01002073 { BIT_FAST_INTR_STAT_DDC, sii8620_irq_ddc },
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002074 { BIT_FAST_INTR_STAT_SCDT, sii8620_irq_scdt },
2075 { BIT_FAST_INTR_STAT_INFR, sii8620_irq_infr },
2076 };
2077 struct sii8620 *ctx = data;
2078 u8 stats[LEN_FAST_INTR_STAT];
2079 int i, ret;
2080
2081 mutex_lock(&ctx->lock);
2082
2083 sii8620_read_buf(ctx, REG_FAST_INTR_STAT, stats, ARRAY_SIZE(stats));
2084 for (i = 0; i < ARRAY_SIZE(irq_vec); ++i)
2085 if (sii8620_test_bit(irq_vec[i].bit, stats))
2086 irq_vec[i].handler(ctx);
2087
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002088 sii8620_burst_rx_all(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002089 sii8620_mt_work(ctx);
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002090 sii8620_burst_send(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002091
2092 ret = sii8620_clear_error(ctx);
2093 if (ret) {
2094 dev_err(ctx->dev, "Error during IRQ handling, %d.\n", ret);
2095 sii8620_mhl_disconnected(ctx);
2096 }
2097 mutex_unlock(&ctx->lock);
2098
2099 return IRQ_HANDLED;
2100}
2101
2102static void sii8620_cable_in(struct sii8620 *ctx)
2103{
2104 struct device *dev = ctx->dev;
2105 u8 ver[5];
2106 int ret;
2107
2108 ret = sii8620_hw_on(ctx);
2109 if (ret) {
2110 dev_err(dev, "Error powering on, %d.\n", ret);
2111 return;
2112 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002113
2114 sii8620_read_buf(ctx, REG_VND_IDL, ver, ARRAY_SIZE(ver));
2115 ret = sii8620_clear_error(ctx);
2116 if (ret) {
2117 dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
2118 return;
2119 }
2120
2121 dev_info(dev, "ChipID %02x%02x:%02x%02x rev %02x.\n", ver[1], ver[0],
2122 ver[3], ver[2], ver[4]);
2123
2124 sii8620_write(ctx, REG_DPD,
2125 BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN);
2126
2127 sii8620_xtal_set_rate(ctx);
2128 sii8620_disconnect(ctx);
2129
2130 sii8620_write_seq_static(ctx,
2131 REG_MHL_CBUS_CTL0, VAL_MHL_CBUS_CTL0_CBUS_DRV_SEL_STRONG
2132 | VAL_MHL_CBUS_CTL0_CBUS_RGND_VBIAS_734,
2133 REG_MHL_CBUS_CTL1, VAL_MHL_CBUS_CTL1_1115_OHM,
2134 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN,
2135 );
2136
2137 ret = sii8620_clear_error(ctx);
2138 if (ret) {
2139 dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
2140 return;
2141 }
2142
2143 enable_irq(to_i2c_client(ctx->dev)->irq);
2144}
2145
Maciej Purskie25f1f72017-08-24 10:58:07 +02002146static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
2147{
2148 struct rc_dev *rc_dev;
2149 int ret;
2150
2151 rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
2152 if (!rc_dev) {
2153 dev_err(ctx->dev, "Failed to allocate RC device\n");
2154 ctx->error = -ENOMEM;
2155 return;
2156 }
2157
2158 rc_dev->input_phys = "sii8620/input0";
2159 rc_dev->input_id.bustype = BUS_VIRTUAL;
2160 rc_dev->map_name = RC_MAP_CEC;
2161 rc_dev->allowed_protocols = RC_PROTO_BIT_CEC;
2162 rc_dev->driver_name = "sii8620";
2163 rc_dev->device_name = "sii8620";
2164
2165 ret = rc_register_device(rc_dev);
2166
2167 if (ret) {
2168 dev_err(ctx->dev, "Failed to register RC device\n");
2169 ctx->error = ret;
2170 rc_free_device(ctx->rc_dev);
2171 return;
2172 }
2173 ctx->rc_dev = rc_dev;
2174}
2175
Maciej Purski68883842018-02-27 08:11:34 +01002176static void sii8620_cable_out(struct sii8620 *ctx)
2177{
2178 disable_irq(to_i2c_client(ctx->dev)->irq);
2179 sii8620_hw_off(ctx);
2180}
2181
2182static void sii8620_extcon_work(struct work_struct *work)
2183{
2184 struct sii8620 *ctx =
2185 container_of(work, struct sii8620, extcon_wq);
2186 int state = extcon_get_state(ctx->extcon, EXTCON_DISP_MHL);
2187
2188 if (state == ctx->cable_state)
2189 return;
2190
2191 ctx->cable_state = state;
2192
2193 if (state > 0)
2194 sii8620_cable_in(ctx);
2195 else
2196 sii8620_cable_out(ctx);
2197}
2198
2199static int sii8620_extcon_notifier(struct notifier_block *self,
2200 unsigned long event, void *ptr)
2201{
2202 struct sii8620 *ctx =
2203 container_of(self, struct sii8620, extcon_nb);
2204
2205 schedule_work(&ctx->extcon_wq);
2206
2207 return NOTIFY_DONE;
2208}
2209
2210static int sii8620_extcon_init(struct sii8620 *ctx)
2211{
2212 struct extcon_dev *edev;
2213 struct device_node *musb, *muic;
2214 int ret;
2215
2216 /* get micro-USB connector node */
2217 musb = of_graph_get_remote_node(ctx->dev->of_node, 1, -1);
2218 /* next get micro-USB Interface Controller node */
2219 muic = of_get_next_parent(musb);
2220
2221 if (!muic) {
2222 dev_info(ctx->dev, "no extcon found, switching to 'always on' mode\n");
2223 return 0;
2224 }
2225
2226 edev = extcon_find_edev_by_node(muic);
2227 of_node_put(muic);
2228 if (IS_ERR(edev)) {
2229 if (PTR_ERR(edev) == -EPROBE_DEFER)
2230 return -EPROBE_DEFER;
2231 dev_err(ctx->dev, "Invalid or missing extcon\n");
2232 return PTR_ERR(edev);
2233 }
2234
2235 ctx->extcon = edev;
2236 ctx->extcon_nb.notifier_call = sii8620_extcon_notifier;
2237 INIT_WORK(&ctx->extcon_wq, sii8620_extcon_work);
2238 ret = extcon_register_notifier(edev, EXTCON_DISP_MHL, &ctx->extcon_nb);
2239 if (ret) {
2240 dev_err(ctx->dev, "failed to register notifier for MHL\n");
2241 return ret;
2242 }
2243
2244 return 0;
2245}
2246
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002247static inline struct sii8620 *bridge_to_sii8620(struct drm_bridge *bridge)
2248{
2249 return container_of(bridge, struct sii8620, bridge);
2250}
2251
Maciej Purskie25f1f72017-08-24 10:58:07 +02002252static int sii8620_attach(struct drm_bridge *bridge)
2253{
2254 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2255
2256 sii8620_init_rcp_input_dev(ctx);
2257
2258 return sii8620_clear_error(ctx);
2259}
2260
2261static void sii8620_detach(struct drm_bridge *bridge)
2262{
2263 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2264
2265 rc_unregister_device(ctx->rc_dev);
2266}
2267
Marek Szyprowskie7c2e4d2017-11-09 11:28:31 +01002268static enum drm_mode_status sii8620_mode_valid(struct drm_bridge *bridge,
2269 const struct drm_display_mode *mode)
2270{
2271 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2272 bool can_pack = ctx->devcap[MHL_DCAP_VID_LINK_MODE] &
2273 MHL_DCAP_VID_LINK_PPIXEL;
2274 unsigned int max_pclk = sii8620_is_mhl3(ctx) ? MHL3_MAX_LCLK :
2275 MHL1_MAX_LCLK;
2276 max_pclk /= can_pack ? 2 : 3;
2277
2278 return (mode->clock > max_pclk) ? MODE_CLOCK_HIGH : MODE_OK;
2279}
2280
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002281static bool sii8620_mode_fixup(struct drm_bridge *bridge,
2282 const struct drm_display_mode *mode,
2283 struct drm_display_mode *adjusted_mode)
2284{
2285 struct sii8620 *ctx = bridge_to_sii8620(bridge);
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002286 int max_lclk;
2287 bool ret = true;
2288
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002289 mutex_lock(&ctx->lock);
2290
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002291 max_lclk = sii8620_is_mhl3(ctx) ? MHL3_MAX_LCLK : MHL1_MAX_LCLK;
2292 if (max_lclk > 3 * adjusted_mode->clock) {
2293 ctx->use_packed_pixel = 0;
2294 goto end;
2295 }
2296 if ((ctx->devcap[MHL_DCAP_VID_LINK_MODE] & MHL_DCAP_VID_LINK_PPIXEL) &&
2297 max_lclk > 2 * adjusted_mode->clock) {
2298 ctx->use_packed_pixel = 1;
2299 goto end;
2300 }
2301 ret = false;
2302end:
2303 if (ret) {
2304 u8 vic = drm_match_cea_mode(adjusted_mode);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002305
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002306 if (!vic) {
2307 union hdmi_infoframe frm;
2308 u8 mhl_vic[] = { 0, 95, 94, 93, 98 };
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002309
Ville Syrjäläf1781e92017-11-13 19:04:19 +02002310 /* FIXME: We need the connector here */
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002311 drm_hdmi_vendor_infoframe_from_display_mode(
Ville Syrjäläf1781e92017-11-13 19:04:19 +02002312 &frm.vendor.hdmi, NULL, adjusted_mode);
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002313 vic = frm.vendor.hdmi.vic;
2314 if (vic >= ARRAY_SIZE(mhl_vic))
2315 vic = 0;
2316 vic = mhl_vic[vic];
2317 }
2318 ctx->video_code = vic;
2319 ctx->pixel_clock = adjusted_mode->clock;
2320 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002321 mutex_unlock(&ctx->lock);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002322 return ret;
2323}
2324
2325static const struct drm_bridge_funcs sii8620_bridge_funcs = {
Maciej Purskie25f1f72017-08-24 10:58:07 +02002326 .attach = sii8620_attach,
2327 .detach = sii8620_detach,
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002328 .mode_fixup = sii8620_mode_fixup,
Marek Szyprowskie7c2e4d2017-11-09 11:28:31 +01002329 .mode_valid = sii8620_mode_valid,
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002330};
2331
2332static int sii8620_probe(struct i2c_client *client,
2333 const struct i2c_device_id *id)
2334{
2335 struct device *dev = &client->dev;
2336 struct sii8620 *ctx;
2337 int ret;
2338
2339 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
2340 if (!ctx)
2341 return -ENOMEM;
2342
2343 ctx->dev = dev;
2344 mutex_init(&ctx->lock);
2345 INIT_LIST_HEAD(&ctx->mt_queue);
2346
2347 ctx->clk_xtal = devm_clk_get(dev, "xtal");
2348 if (IS_ERR(ctx->clk_xtal)) {
2349 dev_err(dev, "failed to get xtal clock from DT\n");
2350 return PTR_ERR(ctx->clk_xtal);
2351 }
2352
2353 if (!client->irq) {
2354 dev_err(dev, "no irq provided\n");
2355 return -EINVAL;
2356 }
2357 irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
2358 ret = devm_request_threaded_irq(dev, client->irq, NULL,
2359 sii8620_irq_thread,
2360 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2361 "sii8620", ctx);
Andrzej Hajdae0ba12e2017-02-22 12:47:37 +01002362 if (ret < 0) {
2363 dev_err(dev, "failed to install IRQ handler\n");
2364 return ret;
2365 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002366
2367 ctx->gpio_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
2368 if (IS_ERR(ctx->gpio_reset)) {
2369 dev_err(dev, "failed to get reset gpio from DT\n");
2370 return PTR_ERR(ctx->gpio_reset);
2371 }
2372
2373 ctx->supplies[0].supply = "cvcc10";
2374 ctx->supplies[1].supply = "iovcc18";
2375 ret = devm_regulator_bulk_get(dev, 2, ctx->supplies);
2376 if (ret)
2377 return ret;
2378
Maciej Purski68883842018-02-27 08:11:34 +01002379 ret = sii8620_extcon_init(ctx);
2380 if (ret < 0) {
2381 dev_err(ctx->dev, "failed to initialize EXTCON\n");
2382 return ret;
2383 }
2384
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002385 i2c_set_clientdata(client, ctx);
2386
2387 ctx->bridge.funcs = &sii8620_bridge_funcs;
2388 ctx->bridge.of_node = dev->of_node;
2389 drm_bridge_add(&ctx->bridge);
2390
Maciej Purski68883842018-02-27 08:11:34 +01002391 if (!ctx->extcon)
2392 sii8620_cable_in(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002393
2394 return 0;
2395}
2396
2397static int sii8620_remove(struct i2c_client *client)
2398{
2399 struct sii8620 *ctx = i2c_get_clientdata(client);
2400
Maciej Purski68883842018-02-27 08:11:34 +01002401 if (ctx->extcon) {
2402 extcon_unregister_notifier(ctx->extcon, EXTCON_DISP_MHL,
2403 &ctx->extcon_nb);
2404 flush_work(&ctx->extcon_wq);
2405 if (ctx->cable_state > 0)
2406 sii8620_cable_out(ctx);
2407 } else {
2408 sii8620_cable_out(ctx);
2409 }
Maciej Purskie25f1f72017-08-24 10:58:07 +02002410 drm_bridge_remove(&ctx->bridge);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002411
2412 return 0;
2413}
2414
2415static const struct of_device_id sii8620_dt_match[] = {
2416 { .compatible = "sil,sii8620" },
2417 { },
2418};
2419MODULE_DEVICE_TABLE(of, sii8620_dt_match);
2420
2421static const struct i2c_device_id sii8620_id[] = {
2422 { "sii8620", 0 },
2423 { },
2424};
2425
2426MODULE_DEVICE_TABLE(i2c, sii8620_id);
2427static struct i2c_driver sii8620_driver = {
2428 .driver = {
2429 .name = "sii8620",
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002430 .of_match_table = of_match_ptr(sii8620_dt_match),
2431 },
2432 .probe = sii8620_probe,
2433 .remove = sii8620_remove,
2434 .id_table = sii8620_id,
2435};
2436
2437module_i2c_driver(sii8620_driver);
2438MODULE_LICENSE("GPL v2");