blob: 720bc7c325e03b7fc24f9bde3f2e1463fa335fd0 [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;
Andrzej Hajda8e627a12018-01-15 18:33:57 +0100810 unsigned long timeout;
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200811 int fetched, i;
812 int edid_len = EDID_LENGTH;
813 u8 *edid;
814
815 sii8620_readb(ctx, REG_CBUS_STATUS);
816 lm_ddc = sii8620_readb(ctx, REG_LM_DDC);
817 ddc_cmd = sii8620_readb(ctx, REG_DDC_CMD);
818
819 sii8620_write_seq(ctx,
820 REG_INTR9_MASK, 0,
821 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
822 REG_HDCP2X_POLL_CS, 0x71,
823 REG_HDCP2X_CTRL_0, BIT_HDCP2X_CTRL_0_HDCP2X_HDCPTX,
824 REG_LM_DDC, lm_ddc | BIT_LM_DDC_SW_TPI_EN_DISABLED,
825 );
826
827 for (i = 0; i < 256; ++i) {
828 u8 ddc_stat = sii8620_readb(ctx, REG_DDC_STATUS);
829
830 if (!(ddc_stat & BIT_DDC_STATUS_DDC_I2C_IN_PROG))
831 break;
832 sii8620_write(ctx, REG_DDC_STATUS,
833 BIT_DDC_STATUS_DDC_FIFO_EMPTY);
834 }
835
836 sii8620_write(ctx, REG_DDC_ADDR, 0x50 << 1);
837
838 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
839 if (!edid) {
840 ctx->error = -ENOMEM;
841 return;
842 }
843
844#define FETCH_SIZE 16
845 for (fetched = 0; fetched < edid_len; fetched += FETCH_SIZE) {
846 sii8620_readb(ctx, REG_DDC_STATUS);
847 sii8620_write_seq(ctx,
848 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_ABORT,
849 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_CLEAR_FIFO,
850 REG_DDC_STATUS, BIT_DDC_STATUS_DDC_FIFO_EMPTY
851 );
852 sii8620_write_seq(ctx,
853 REG_DDC_SEGM, fetched >> 8,
854 REG_DDC_OFFSET, fetched & 0xff,
855 REG_DDC_DIN_CNT1, FETCH_SIZE,
856 REG_DDC_DIN_CNT2, 0,
857 REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_ENH_DDC_READ_NO_ACK
858 );
859
Andrzej Hajda8e627a12018-01-15 18:33:57 +0100860 int3 = 0;
861 timeout = jiffies + msecs_to_jiffies(200);
862 for (;;) {
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200863 cbus = sii8620_readb(ctx, REG_CBUS_STATUS);
Andrzej Hajda8e627a12018-01-15 18:33:57 +0100864 if (~cbus & BIT_CBUS_STATUS_CBUS_CONNECTED) {
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200865 kfree(edid);
866 edid = NULL;
867 goto end;
868 }
Andrzej Hajda8e627a12018-01-15 18:33:57 +0100869 if (int3 & BIT_DDC_CMD_DONE) {
870 if (sii8620_readb(ctx, REG_DDC_DOUT_CNT)
871 >= FETCH_SIZE)
872 break;
873 } else {
874 int3 = sii8620_readb(ctx, REG_INTR3);
875 }
876 if (time_is_before_jiffies(timeout)) {
877 ctx->error = -ETIMEDOUT;
878 dev_err(ctx->dev, "timeout during EDID read\n");
879 kfree(edid);
880 edid = NULL;
881 goto end;
882 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200883 usleep_range(10, 20);
Andrzej Hajda8e627a12018-01-15 18:33:57 +0100884 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200885
886 sii8620_read_buf(ctx, REG_DDC_DATA, edid + fetched, FETCH_SIZE);
887 if (fetched + FETCH_SIZE == EDID_LENGTH) {
888 u8 ext = ((struct edid *)edid)->extensions;
889
890 if (ext) {
891 u8 *new_edid;
892
893 edid_len += ext * EDID_LENGTH;
894 new_edid = krealloc(edid, edid_len, GFP_KERNEL);
895 if (!new_edid) {
896 kfree(edid);
897 ctx->error = -ENOMEM;
898 return;
899 }
900 edid = new_edid;
901 }
902 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200903 }
904
Andrzej Hajda263b5c92017-02-01 08:47:44 +0100905 sii8620_write_seq(ctx,
906 REG_INTR3_MASK, BIT_DDC_CMD_DONE,
907 REG_LM_DDC, lm_ddc
908 );
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200909
910end:
911 kfree(ctx->edid);
912 ctx->edid = (struct edid *)edid;
913}
914
915static void sii8620_set_upstream_edid(struct sii8620 *ctx)
916{
917 sii8620_setbits(ctx, REG_DPD, BIT_DPD_PDNRX12 | BIT_DPD_PDIDCK_N
918 | BIT_DPD_PD_MHL_CLK_N, 0xff);
919
920 sii8620_write_seq_static(ctx,
921 REG_RX_HDMI_CTRL3, 0x00,
922 REG_PKT_FILTER_0, 0xFF,
923 REG_PKT_FILTER_1, 0xFF,
924 REG_ALICE0_BW_I2C, 0x06
925 );
926
927 sii8620_setbits(ctx, REG_RX_HDMI_CLR_BUFFER,
928 BIT_RX_HDMI_CLR_BUFFER_VSI_CLR_EN, 0xff);
929
930 sii8620_write_seq_static(ctx,
931 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
932 | BIT_EDID_CTRL_EDID_MODE_EN,
933 REG_EDID_FIFO_ADDR, 0,
934 );
935
936 sii8620_write_buf(ctx, REG_EDID_FIFO_WR_DATA, (u8 *)ctx->edid,
937 (ctx->edid->extensions + 1) * EDID_LENGTH);
938
939 sii8620_write_seq_static(ctx,
940 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID
941 | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
942 | BIT_EDID_CTRL_EDID_MODE_EN,
943 REG_INTR5_MASK, BIT_INTR_SCDT_CHANGE,
944 REG_INTR9_MASK, 0
945 );
946}
947
948static void sii8620_xtal_set_rate(struct sii8620 *ctx)
949{
950 static const struct {
951 unsigned int rate;
952 u8 div;
953 u8 tp1;
954 } rates[] = {
955 { 19200, 0x04, 0x53 },
956 { 20000, 0x04, 0x62 },
957 { 24000, 0x05, 0x75 },
958 { 30000, 0x06, 0x92 },
959 { 38400, 0x0c, 0xbc },
960 };
961 unsigned long rate = clk_get_rate(ctx->clk_xtal) / 1000;
962 int i;
963
964 for (i = 0; i < ARRAY_SIZE(rates) - 1; ++i)
965 if (rate <= rates[i].rate)
966 break;
967
968 if (rate != rates[i].rate)
969 dev_err(ctx->dev, "xtal clock rate(%lukHz) not supported, setting MHL for %ukHz.\n",
970 rate, rates[i].rate);
971
972 sii8620_write(ctx, REG_DIV_CTL_MAIN, rates[i].div);
973 sii8620_write(ctx, REG_HDCP2X_TP1, rates[i].tp1);
974}
975
976static int sii8620_hw_on(struct sii8620 *ctx)
977{
978 int ret;
979
980 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
981 if (ret)
982 return ret;
Andrzej Hajda8efaac02018-06-08 08:04:57 +0200983
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200984 usleep_range(10000, 20000);
Andrzej Hajda8efaac02018-06-08 08:04:57 +0200985 ret = clk_prepare_enable(ctx->clk_xtal);
986 if (ret)
987 return ret;
988
989 msleep(100);
990 gpiod_set_value(ctx->gpio_reset, 0);
991 msleep(100);
992
993 return 0;
Andrzej Hajdace6e1532016-10-10 09:39:17 +0200994}
995
996static int sii8620_hw_off(struct sii8620 *ctx)
997{
998 clk_disable_unprepare(ctx->clk_xtal);
999 gpiod_set_value(ctx->gpio_reset, 1);
1000 return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
1001}
1002
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001003static void sii8620_cbus_reset(struct sii8620 *ctx)
1004{
Andrzej Hajda4dc3c072017-02-01 08:47:43 +01001005 sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST
1006 | BIT_PWD_SRST_CBUS_RST_SW_EN);
1007 usleep_range(10000, 20000);
1008 sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001009}
1010
1011static void sii8620_set_auto_zone(struct sii8620 *ctx)
1012{
1013 if (ctx->mode != CM_MHL1) {
1014 sii8620_write_seq_static(ctx,
1015 REG_TX_ZONE_CTL1, 0x0,
1016 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1017 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1018 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
1019 );
1020 } else {
1021 sii8620_write_seq_static(ctx,
1022 REG_TX_ZONE_CTL1, VAL_TX_ZONE_CTL1_TX_ZONE_CTRL_MODE,
1023 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1024 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
1025 );
1026 }
1027}
1028
1029static void sii8620_stop_video(struct sii8620 *ctx)
1030{
1031 u8 uninitialized_var(val);
1032
1033 sii8620_write_seq_static(ctx,
1034 REG_TPI_INTR_EN, 0,
1035 REG_HDCP2X_INTR0_MASK, 0,
1036 REG_TPI_COPP_DATA2, 0,
1037 REG_TPI_INTR_ST0, ~0,
1038 );
1039
1040 switch (ctx->sink_type) {
1041 case SINK_DVI:
1042 val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
1043 | BIT_TPI_SC_TPI_AV_MUTE;
1044 break;
1045 case SINK_HDMI:
Andrzej Hajdaef822a02017-02-01 08:47:39 +01001046 default:
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001047 val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
1048 | BIT_TPI_SC_TPI_AV_MUTE
1049 | BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI;
1050 break;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001051 }
1052
1053 sii8620_write(ctx, REG_TPI_SC, val);
1054}
1055
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001056static void sii8620_set_format(struct sii8620 *ctx)
1057{
1058 u8 out_fmt;
1059
1060 if (sii8620_is_mhl3(ctx)) {
1061 sii8620_setbits(ctx, REG_M3_P0CTRL,
1062 BIT_M3_P0CTRL_MHL3_P0_PIXEL_MODE_PACKED,
1063 ctx->use_packed_pixel ? ~0 : 0);
1064 } else {
1065 if (ctx->use_packed_pixel)
1066 sii8620_write_seq_static(ctx,
1067 REG_VID_MODE, BIT_VID_MODE_M1080P,
1068 REG_MHL_TOP_CTL, BIT_MHL_TOP_CTL_MHL_PP_SEL | 1,
1069 REG_MHLTX_CTL6, 0x60
1070 );
1071 else
1072 sii8620_write_seq_static(ctx,
1073 REG_VID_MODE, 0,
1074 REG_MHL_TOP_CTL, 1,
1075 REG_MHLTX_CTL6, 0xa0
1076 );
1077 }
1078
1079 if (ctx->use_packed_pixel)
1080 out_fmt = VAL_TPI_FORMAT(YCBCR422, FULL) |
1081 BIT_TPI_OUTPUT_CSCMODE709;
1082 else
1083 out_fmt = VAL_TPI_FORMAT(RGB, FULL);
1084
1085 sii8620_write_seq(ctx,
1086 REG_TPI_INPUT, VAL_TPI_FORMAT(RGB, FULL),
1087 REG_TPI_OUTPUT, out_fmt,
1088 );
1089}
1090
1091static int mhl3_infoframe_init(struct mhl3_infoframe *frame)
1092{
1093 memset(frame, 0, sizeof(*frame));
1094
1095 frame->version = 3;
1096 frame->hev_format = -1;
1097 return 0;
1098}
1099
1100static ssize_t mhl3_infoframe_pack(struct mhl3_infoframe *frame,
1101 void *buffer, size_t size)
1102{
1103 const int frm_len = HDMI_INFOFRAME_HEADER_SIZE + MHL3_INFOFRAME_SIZE;
1104 u8 *ptr = buffer;
1105
1106 if (size < frm_len)
1107 return -ENOSPC;
1108
1109 memset(buffer, 0, size);
1110 ptr[0] = HDMI_INFOFRAME_TYPE_VENDOR;
1111 ptr[1] = frame->version;
1112 ptr[2] = MHL3_INFOFRAME_SIZE;
1113 ptr[4] = MHL3_IEEE_OUI & 0xff;
1114 ptr[5] = (MHL3_IEEE_OUI >> 8) & 0xff;
1115 ptr[6] = (MHL3_IEEE_OUI >> 16) & 0xff;
1116 ptr[7] = frame->video_format & 0x3;
1117 ptr[7] |= (frame->format_type & 0x7) << 2;
1118 ptr[7] |= frame->sep_audio ? BIT(5) : 0;
1119 if (frame->hev_format >= 0) {
1120 ptr[9] = 1;
1121 ptr[10] = (frame->hev_format >> 8) & 0xff;
1122 ptr[11] = frame->hev_format & 0xff;
1123 }
1124 if (frame->av_delay) {
1125 bool sign = frame->av_delay < 0;
1126 int delay = sign ? -frame->av_delay : frame->av_delay;
1127
1128 ptr[12] = (delay >> 16) & 0xf;
1129 if (sign)
1130 ptr[12] |= BIT(4);
1131 ptr[13] = (delay >> 8) & 0xff;
1132 ptr[14] = delay & 0xff;
1133 }
1134 ptr[3] -= sii8620_checksum(buffer, frm_len);
1135 return frm_len;
1136}
1137
1138static void sii8620_set_infoframes(struct sii8620 *ctx)
1139{
1140 struct mhl3_infoframe mhl_frm;
1141 union hdmi_infoframe frm;
1142 u8 buf[31];
1143 int ret;
1144
1145 if (!sii8620_is_mhl3(ctx) || !ctx->use_packed_pixel) {
1146 sii8620_write(ctx, REG_TPI_SC,
1147 BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI);
1148 sii8620_write_buf(ctx, REG_TPI_AVI_CHSUM, ctx->avif + 3,
1149 ARRAY_SIZE(ctx->avif) - 3);
1150 sii8620_write(ctx, REG_PKT_FILTER_0,
1151 BIT_PKT_FILTER_0_DROP_CEA_GAMUT_PKT |
1152 BIT_PKT_FILTER_0_DROP_MPEG_PKT |
1153 BIT_PKT_FILTER_0_DROP_GCP_PKT,
1154 BIT_PKT_FILTER_1_DROP_GEN_PKT);
1155 return;
1156 }
1157
1158 ret = hdmi_avi_infoframe_init(&frm.avi);
1159 frm.avi.colorspace = HDMI_COLORSPACE_YUV422;
1160 frm.avi.active_aspect = HDMI_ACTIVE_ASPECT_PICTURE;
1161 frm.avi.picture_aspect = HDMI_PICTURE_ASPECT_16_9;
1162 frm.avi.colorimetry = HDMI_COLORIMETRY_ITU_709;
1163 frm.avi.video_code = ctx->video_code;
1164 if (!ret)
1165 ret = hdmi_avi_infoframe_pack(&frm.avi, buf, ARRAY_SIZE(buf));
1166 if (ret > 0)
1167 sii8620_write_buf(ctx, REG_TPI_AVI_CHSUM, buf + 3, ret - 3);
1168 sii8620_write(ctx, REG_PKT_FILTER_0,
1169 BIT_PKT_FILTER_0_DROP_CEA_GAMUT_PKT |
1170 BIT_PKT_FILTER_0_DROP_MPEG_PKT |
1171 BIT_PKT_FILTER_0_DROP_AVI_PKT |
1172 BIT_PKT_FILTER_0_DROP_GCP_PKT,
1173 BIT_PKT_FILTER_1_VSI_OVERRIDE_DIS |
1174 BIT_PKT_FILTER_1_DROP_GEN_PKT |
1175 BIT_PKT_FILTER_1_DROP_VSIF_PKT);
1176
1177 sii8620_write(ctx, REG_TPI_INFO_FSEL, BIT_TPI_INFO_FSEL_EN
1178 | BIT_TPI_INFO_FSEL_RPT | VAL_TPI_INFO_FSEL_VSI);
1179 ret = mhl3_infoframe_init(&mhl_frm);
1180 if (!ret)
1181 ret = mhl3_infoframe_pack(&mhl_frm, buf, ARRAY_SIZE(buf));
1182 sii8620_write_buf(ctx, REG_TPI_INFO_B0, buf, ret);
1183}
1184
Maciej Purski5c632e22017-11-09 11:53:42 +01001185static void sii8620_start_video(struct sii8620 *ctx)
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001186{
Maciej Purski5c632e22017-11-09 11:53:42 +01001187 if (!sii8620_is_mhl3(ctx))
1188 sii8620_stop_video(ctx);
1189
1190 if (ctx->sink_type == SINK_DVI && !sii8620_is_mhl3(ctx)) {
1191 sii8620_write(ctx, REG_RX_HDMI_CTRL2,
1192 VAL_RX_HDMI_CTRL2_DEFVAL);
1193 sii8620_write(ctx, REG_TPI_SC, 0);
1194 return;
1195 }
1196
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001197 sii8620_write_seq_static(ctx,
1198 REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL
1199 | BIT_RX_HDMI_CTRL2_USE_AV_MUTE,
1200 REG_VID_OVRRD, BIT_VID_OVRRD_PP_AUTO_DISABLE
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001201 | BIT_VID_OVRRD_M1080P_OVRRD);
1202 sii8620_set_format(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001203
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001204 if (!sii8620_is_mhl3(ctx)) {
1205 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1206 MHL_DST_LM_CLK_MODE_NORMAL | MHL_DST_LM_PATH_ENABLED);
1207 sii8620_set_auto_zone(ctx);
1208 } else {
1209 static const struct {
1210 int max_clk;
1211 u8 zone;
1212 u8 link_rate;
1213 u8 rrp_decode;
1214 } clk_spec[] = {
1215 { 150000, VAL_TX_ZONE_CTL3_TX_ZONE_1_5GBPS,
1216 MHL_XDS_LINK_RATE_1_5_GBPS, 0x38 },
1217 { 300000, VAL_TX_ZONE_CTL3_TX_ZONE_3GBPS,
1218 MHL_XDS_LINK_RATE_3_0_GBPS, 0x40 },
1219 { 600000, VAL_TX_ZONE_CTL3_TX_ZONE_6GBPS,
1220 MHL_XDS_LINK_RATE_6_0_GBPS, 0x40 },
1221 };
1222 u8 p0_ctrl = BIT_M3_P0CTRL_MHL3_P0_PORT_EN;
1223 int clk = ctx->pixel_clock * (ctx->use_packed_pixel ? 2 : 3);
1224 int i;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001225
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001226 for (i = 0; i < ARRAY_SIZE(clk_spec); ++i)
1227 if (clk < clk_spec[i].max_clk)
1228 break;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001229
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001230 if (100 * clk >= 98 * clk_spec[i].max_clk)
1231 p0_ctrl |= BIT_M3_P0CTRL_MHL3_P0_UNLIMIT_EN;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001232
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001233 sii8620_burst_tx_bits_per_pixel_fmt(ctx, ctx->use_packed_pixel);
1234 sii8620_burst_send(ctx);
1235 sii8620_write_seq(ctx,
1236 REG_MHL_DP_CTL0, 0xf0,
1237 REG_MHL3_TX_ZONE_CTL, clk_spec[i].zone);
1238 sii8620_setbits(ctx, REG_M3_P0CTRL,
1239 BIT_M3_P0CTRL_MHL3_P0_PORT_EN
1240 | BIT_M3_P0CTRL_MHL3_P0_UNLIMIT_EN, p0_ctrl);
1241 sii8620_setbits(ctx, REG_M3_POSTM, MSK_M3_POSTM_RRP_DECODE,
1242 clk_spec[i].rrp_decode);
1243 sii8620_write_seq_static(ctx,
1244 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE
1245 | BIT_M3_CTRL_H2M_SWRST,
1246 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE
1247 );
1248 sii8620_mt_write_stat(ctx, MHL_XDS_REG(AVLINK_MODE_CONTROL),
1249 clk_spec[i].link_rate);
1250 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001251
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01001252 sii8620_set_infoframes(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001253}
1254
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001255static void sii8620_disable_hpd(struct sii8620 *ctx)
1256{
1257 sii8620_setbits(ctx, REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID, 0);
1258 sii8620_write_seq_static(ctx,
1259 REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN,
1260 REG_INTR8_MASK, 0
1261 );
1262}
1263
1264static void sii8620_enable_hpd(struct sii8620 *ctx)
1265{
1266 sii8620_setbits(ctx, REG_TMDS_CSTAT_P3,
1267 BIT_TMDS_CSTAT_P3_SCDT_CLR_AVI_DIS
1268 | BIT_TMDS_CSTAT_P3_CLR_AVI, ~0);
1269 sii8620_write_seq_static(ctx,
1270 REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN
1271 | BIT_HPD_CTRL_HPD_HIGH,
1272 );
1273}
1274
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001275static void sii8620_mhl_discover(struct sii8620 *ctx)
1276{
1277 sii8620_write_seq_static(ctx,
1278 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1279 | BIT_DISC_CTRL9_DISC_PULSE_PROCEED,
1280 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_5K, VAL_PUP_20K),
1281 REG_CBUS_DISC_INTR0_MASK, BIT_MHL3_EST_INT
1282 | BIT_MHL_EST_INT
1283 | BIT_NOT_MHL_EST_INT
1284 | BIT_CBUS_MHL3_DISCON_INT
1285 | BIT_CBUS_MHL12_DISCON_INT
1286 | BIT_RGND_READY_INT,
1287 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1288 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1289 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
1290 REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
1291 | BIT_MHL_DP_CTL0_TX_OE_OVR,
1292 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
1293 REG_MHL_DP_CTL1, 0xA2,
1294 REG_MHL_DP_CTL2, 0x03,
1295 REG_MHL_DP_CTL3, 0x35,
1296 REG_MHL_DP_CTL5, 0x02,
1297 REG_MHL_DP_CTL6, 0x02,
1298 REG_MHL_DP_CTL7, 0x03,
1299 REG_COC_CTLC, 0xFF,
1300 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
1301 | BIT_DPD_OSC_EN | BIT_DPD_PWRON_HSIC,
1302 REG_COC_INTR_MASK, BIT_COC_PLL_LOCK_STATUS_CHANGE
1303 | BIT_COC_CALIBRATION_DONE,
1304 REG_CBUS_INT_1_MASK, BIT_CBUS_MSC_ABORT_RCVD
1305 | BIT_CBUS_CMD_ABORT,
1306 REG_CBUS_INT_0_MASK, BIT_CBUS_MSC_MT_DONE
1307 | BIT_CBUS_HPD_CHG
1308 | BIT_CBUS_MSC_MR_WRITE_STAT
1309 | BIT_CBUS_MSC_MR_MSC_MSG
1310 | BIT_CBUS_MSC_MR_WRITE_BURST
1311 | BIT_CBUS_MSC_MR_SET_INT
1312 | BIT_CBUS_MSC_MT_DONE_NACK
1313 );
1314}
1315
1316static void sii8620_peer_specific_init(struct sii8620 *ctx)
1317{
Andrzej Hajdabb4954c2017-02-01 08:47:29 +01001318 if (sii8620_is_mhl3(ctx))
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001319 sii8620_write_seq_static(ctx,
1320 REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD,
1321 REG_EMSCINTRMASK1,
1322 BIT_EMSCINTR1_EMSC_TRAINING_COMMA_ERR
1323 );
1324 else
1325 sii8620_write_seq_static(ctx,
1326 REG_HDCP2X_INTR0_MASK, 0x00,
1327 REG_EMSCINTRMASK1, 0x00,
1328 REG_HDCP2X_INTR0, 0xFF,
1329 REG_INTR1, 0xFF,
1330 REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD
1331 | BIT_SYS_CTRL1_TX_CTRL_HDMI
1332 );
1333}
1334
1335#define SII8620_MHL_VERSION 0x32
1336#define SII8620_SCRATCHPAD_SIZE 16
1337#define SII8620_INT_STAT_SIZE 0x33
1338
1339static void sii8620_set_dev_cap(struct sii8620 *ctx)
1340{
1341 static const u8 devcap[MHL_DCAP_SIZE] = {
1342 [MHL_DCAP_MHL_VERSION] = SII8620_MHL_VERSION,
1343 [MHL_DCAP_CAT] = MHL_DCAP_CAT_SOURCE | MHL_DCAP_CAT_POWER,
1344 [MHL_DCAP_ADOPTER_ID_H] = 0x01,
1345 [MHL_DCAP_ADOPTER_ID_L] = 0x41,
1346 [MHL_DCAP_VID_LINK_MODE] = MHL_DCAP_VID_LINK_RGB444
1347 | MHL_DCAP_VID_LINK_PPIXEL
1348 | MHL_DCAP_VID_LINK_16BPP,
1349 [MHL_DCAP_AUD_LINK_MODE] = MHL_DCAP_AUD_LINK_2CH,
1350 [MHL_DCAP_VIDEO_TYPE] = MHL_DCAP_VT_GRAPHICS,
1351 [MHL_DCAP_LOG_DEV_MAP] = MHL_DCAP_LD_GUI,
1352 [MHL_DCAP_BANDWIDTH] = 0x0f,
1353 [MHL_DCAP_FEATURE_FLAG] = MHL_DCAP_FEATURE_RCP_SUPPORT
1354 | MHL_DCAP_FEATURE_RAP_SUPPORT
1355 | MHL_DCAP_FEATURE_SP_SUPPORT,
1356 [MHL_DCAP_SCRATCHPAD_SIZE] = SII8620_SCRATCHPAD_SIZE,
1357 [MHL_DCAP_INT_STAT_SIZE] = SII8620_INT_STAT_SIZE,
1358 };
1359 static const u8 xdcap[MHL_XDC_SIZE] = {
1360 [MHL_XDC_ECBUS_SPEEDS] = MHL_XDC_ECBUS_S_075
1361 | MHL_XDC_ECBUS_S_8BIT,
1362 [MHL_XDC_TMDS_SPEEDS] = MHL_XDC_TMDS_150
1363 | MHL_XDC_TMDS_300 | MHL_XDC_TMDS_600,
1364 [MHL_XDC_ECBUS_ROLES] = MHL_XDC_DEV_HOST,
1365 [MHL_XDC_LOG_DEV_MAPX] = MHL_XDC_LD_PHONE,
1366 };
1367
1368 sii8620_write_buf(ctx, REG_MHL_DEVCAP_0, devcap, ARRAY_SIZE(devcap));
1369 sii8620_write_buf(ctx, REG_MHL_EXTDEVCAP_0, xdcap, ARRAY_SIZE(xdcap));
1370}
1371
1372static void sii8620_mhl_init(struct sii8620 *ctx)
1373{
1374 sii8620_write_seq_static(ctx,
1375 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1376 REG_CBUS_MSC_COMPAT_CTRL,
1377 BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN,
1378 );
1379
1380 sii8620_peer_specific_init(ctx);
1381
1382 sii8620_disable_hpd(ctx);
1383
1384 sii8620_write_seq_static(ctx,
1385 REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
1386 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1387 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1388 REG_TMDS0_CCTRL1, 0x90,
1389 REG_TMDS_CLK_EN, 0x01,
1390 REG_TMDS_CH_EN, 0x11,
1391 REG_BGR_BIAS, 0x87,
1392 REG_ALICE0_ZONE_CTRL, 0xE8,
1393 REG_ALICE0_MODE_CTRL, 0x04,
1394 );
1395 sii8620_setbits(ctx, REG_LM_DDC, BIT_LM_DDC_SW_TPI_EN_DISABLED, 0);
1396 sii8620_write_seq_static(ctx,
1397 REG_TPI_HW_OPT3, 0x76,
1398 REG_TMDS_CCTRL, BIT_TMDS_CCTRL_TMDS_OE,
1399 REG_TPI_DTD_B2, 79,
1400 );
1401 sii8620_set_dev_cap(ctx);
1402 sii8620_write_seq_static(ctx,
1403 REG_MDT_XMIT_TIMEOUT, 100,
1404 REG_MDT_XMIT_CTRL, 0x03,
1405 REG_MDT_XFIFO_STAT, 0x00,
1406 REG_MDT_RCV_TIMEOUT, 100,
1407 REG_CBUS_LINK_CTRL_8, 0x1D,
1408 );
1409
1410 sii8620_start_gen2_write_burst(ctx);
1411 sii8620_write_seq_static(ctx,
1412 REG_BIST_CTRL, 0x00,
1413 REG_COC_CTL1, 0x10,
1414 REG_COC_CTL2, 0x18,
1415 REG_COC_CTLF, 0x07,
1416 REG_COC_CTL11, 0xF8,
1417 REG_COC_CTL17, 0x61,
1418 REG_COC_CTL18, 0x46,
1419 REG_COC_CTL19, 0x15,
1420 REG_COC_CTL1A, 0x01,
1421 REG_MHL_COC_CTL3, BIT_MHL_COC_CTL3_COC_AECHO_EN,
1422 REG_MHL_COC_CTL4, 0x2D,
1423 REG_MHL_COC_CTL5, 0xF9,
1424 REG_MSC_HEARTBEAT_CTRL, 0x27,
1425 );
1426 sii8620_disable_gen2_write_burst(ctx);
1427
Andrzej Hajda9fc6ade2017-02-01 08:47:51 +01001428 sii8620_mt_write_stat(ctx, MHL_DST_REG(VERSION), SII8620_MHL_VERSION);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001429 sii8620_mt_write_stat(ctx, MHL_DST_REG(CONNECTED_RDY),
1430 MHL_DST_CONN_DCAP_RDY | MHL_DST_CONN_XDEVCAPP_SUPP
1431 | MHL_DST_CONN_POW_STAT);
1432 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE), MHL_INT_RC_DCAP_CHG);
1433}
1434
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001435static void sii8620_emsc_enable(struct sii8620 *ctx)
1436{
1437 u8 reg;
1438
1439 sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_EMSC_EN
1440 | BIT_GENCTL_CLR_EMSC_RFIFO
1441 | BIT_GENCTL_CLR_EMSC_XFIFO, ~0);
1442 sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_CLR_EMSC_RFIFO
1443 | BIT_GENCTL_CLR_EMSC_XFIFO, 0);
1444 sii8620_setbits(ctx, REG_COMMECNT, BIT_COMMECNT_I2C_TO_EMSC_EN, ~0);
1445 reg = sii8620_readb(ctx, REG_EMSCINTR);
1446 sii8620_write(ctx, REG_EMSCINTR, reg);
1447 sii8620_write(ctx, REG_EMSCINTRMASK, BIT_EMSCINTR_SPI_DVLD);
1448}
1449
1450static int sii8620_wait_for_fsm_state(struct sii8620 *ctx, u8 state)
1451{
1452 int i;
1453
1454 for (i = 0; i < 10; ++i) {
1455 u8 s = sii8620_readb(ctx, REG_COC_STAT_0);
1456
1457 if ((s & MSK_COC_STAT_0_FSM_STATE) == state)
1458 return 0;
1459 if (!(s & BIT_COC_STAT_0_PLL_LOCKED))
1460 return -EBUSY;
1461 usleep_range(4000, 6000);
1462 }
1463 return -ETIMEDOUT;
1464}
1465
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001466static void sii8620_set_mode(struct sii8620 *ctx, enum sii8620_mode mode)
1467{
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001468 int ret;
1469
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001470 if (ctx->mode == mode)
1471 return;
1472
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001473 switch (mode) {
1474 case CM_MHL1:
1475 sii8620_write_seq_static(ctx,
1476 REG_CBUS_MSC_COMPAT_CTRL, 0x02,
1477 REG_M3_CTRL, VAL_M3_CTRL_MHL1_2_VALUE,
1478 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
1479 | BIT_DPD_OSC_EN,
1480 REG_COC_INTR_MASK, 0
1481 );
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001482 ctx->mode = mode;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001483 break;
1484 case CM_MHL3:
Andrzej Hajdadd123122017-02-01 08:47:28 +01001485 sii8620_write(ctx, REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE);
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001486 ctx->mode = mode;
1487 return;
1488 case CM_ECBUS_S:
1489 sii8620_emsc_enable(ctx);
1490 sii8620_write_seq_static(ctx,
1491 REG_TTXSPINUMS, 4,
1492 REG_TRXSPINUMS, 4,
1493 REG_TTXHSICNUMS, 0x14,
1494 REG_TRXHSICNUMS, 0x14,
1495 REG_TTXTOTNUMS, 0x18,
1496 REG_TRXTOTNUMS, 0x18,
1497 REG_PWD_SRST, BIT_PWD_SRST_COC_DOC_RST
1498 | BIT_PWD_SRST_CBUS_RST_SW_EN,
1499 REG_MHL_COC_CTL1, 0xbd,
1500 REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN,
1501 REG_COC_CTLB, 0x01,
1502 REG_COC_CTL0, 0x5c,
1503 REG_COC_CTL14, 0x03,
1504 REG_COC_CTL15, 0x80,
1505 REG_MHL_DP_CTL6, BIT_MHL_DP_CTL6_DP_TAP1_SGN
1506 | BIT_MHL_DP_CTL6_DP_TAP1_EN
1507 | BIT_MHL_DP_CTL6_DT_PREDRV_FEEDCAP_EN,
1508 REG_MHL_DP_CTL8, 0x03
1509 );
1510 ret = sii8620_wait_for_fsm_state(ctx, 0x03);
1511 sii8620_write_seq_static(ctx,
1512 REG_COC_CTL14, 0x00,
1513 REG_COC_CTL15, 0x80
1514 );
1515 if (!ret)
1516 sii8620_write(ctx, REG_CBUS3_CNVT, 0x85);
1517 else
1518 sii8620_disconnect(ctx);
Andrzej Hajdadd123122017-02-01 08:47:28 +01001519 return;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001520 case CM_DISCONNECTED:
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001521 ctx->mode = mode;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001522 break;
1523 default:
1524 dev_err(ctx->dev, "%s mode %d not supported\n", __func__, mode);
1525 break;
kbuild test robot3a81e962016-10-27 00:58:36 +08001526 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001527
1528 sii8620_set_auto_zone(ctx);
1529
1530 if (mode != CM_MHL1)
1531 return;
1532
1533 sii8620_write_seq_static(ctx,
1534 REG_MHL_DP_CTL0, 0xBC,
1535 REG_MHL_DP_CTL1, 0xBB,
1536 REG_MHL_DP_CTL3, 0x48,
1537 REG_MHL_DP_CTL5, 0x39,
1538 REG_MHL_DP_CTL2, 0x2A,
1539 REG_MHL_DP_CTL6, 0x2A,
1540 REG_MHL_DP_CTL7, 0x08
1541 );
1542}
1543
1544static void sii8620_disconnect(struct sii8620 *ctx)
1545{
1546 sii8620_disable_gen2_write_burst(ctx);
1547 sii8620_stop_video(ctx);
Andrzej Hajda003f99292017-02-01 08:47:45 +01001548 msleep(100);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001549 sii8620_cbus_reset(ctx);
1550 sii8620_set_mode(ctx, CM_DISCONNECTED);
1551 sii8620_write_seq_static(ctx,
Andrzej Hajda003f99292017-02-01 08:47:45 +01001552 REG_TX_ZONE_CTL1, 0,
1553 REG_MHL_PLL_CTL0, 0x07,
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001554 REG_COC_CTL0, 0x40,
1555 REG_CBUS3_CNVT, 0x84,
1556 REG_COC_CTL14, 0x00,
1557 REG_COC_CTL0, 0x40,
1558 REG_HRXCTRL3, 0x07,
1559 REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
1560 | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
1561 | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
1562 REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
1563 | BIT_MHL_DP_CTL0_TX_OE_OVR,
1564 REG_MHL_DP_CTL1, 0xBB,
1565 REG_MHL_DP_CTL3, 0x48,
1566 REG_MHL_DP_CTL5, 0x3F,
1567 REG_MHL_DP_CTL2, 0x2F,
1568 REG_MHL_DP_CTL6, 0x2A,
1569 REG_MHL_DP_CTL7, 0x03
1570 );
1571 sii8620_disable_hpd(ctx);
1572 sii8620_write_seq_static(ctx,
1573 REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
1574 REG_MHL_COC_CTL1, 0x07,
1575 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1576 REG_DISC_CTRL8, 0x00,
1577 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1578 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1579 REG_INT_CTRL, 0x00,
1580 REG_MSC_HEARTBEAT_CTRL, 0x27,
1581 REG_DISC_CTRL1, 0x25,
1582 REG_CBUS_DISC_INTR0, (u8)~BIT_RGND_READY_INT,
1583 REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT,
1584 REG_MDT_INT_1, 0xff,
1585 REG_MDT_INT_1_MASK, 0x00,
1586 REG_MDT_INT_0, 0xff,
1587 REG_MDT_INT_0_MASK, 0x00,
1588 REG_COC_INTR, 0xff,
1589 REG_COC_INTR_MASK, 0x00,
1590 REG_TRXINTH, 0xff,
1591 REG_TRXINTMH, 0x00,
1592 REG_CBUS_INT_0, 0xff,
1593 REG_CBUS_INT_0_MASK, 0x00,
1594 REG_CBUS_INT_1, 0xff,
1595 REG_CBUS_INT_1_MASK, 0x00,
1596 REG_EMSCINTR, 0xff,
1597 REG_EMSCINTRMASK, 0x00,
1598 REG_EMSCINTR1, 0xff,
1599 REG_EMSCINTRMASK1, 0x00,
1600 REG_INTR8, 0xff,
1601 REG_INTR8_MASK, 0x00,
1602 REG_TPI_INTR_ST0, 0xff,
1603 REG_TPI_INTR_EN, 0x00,
1604 REG_HDCP2X_INTR0, 0xff,
1605 REG_HDCP2X_INTR0_MASK, 0x00,
1606 REG_INTR9, 0xff,
1607 REG_INTR9_MASK, 0x00,
1608 REG_INTR3, 0xff,
1609 REG_INTR3_MASK, 0x00,
1610 REG_INTR5, 0xff,
1611 REG_INTR5_MASK, 0x00,
1612 REG_INTR2, 0xff,
1613 REG_INTR2_MASK, 0x00,
1614 );
1615 memset(ctx->stat, 0, sizeof(ctx->stat));
1616 memset(ctx->xstat, 0, sizeof(ctx->xstat));
1617 memset(ctx->devcap, 0, sizeof(ctx->devcap));
1618 memset(ctx->xdevcap, 0, sizeof(ctx->xdevcap));
1619 ctx->cbus_status = 0;
1620 ctx->sink_type = SINK_NONE;
1621 kfree(ctx->edid);
1622 ctx->edid = NULL;
1623 sii8620_mt_cleanup(ctx);
1624}
1625
1626static void sii8620_mhl_disconnected(struct sii8620 *ctx)
1627{
1628 sii8620_write_seq_static(ctx,
1629 REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
1630 REG_CBUS_MSC_COMPAT_CTRL,
1631 BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN
1632 );
1633 sii8620_disconnect(ctx);
1634}
1635
1636static void sii8620_irq_disc(struct sii8620 *ctx)
1637{
1638 u8 stat = sii8620_readb(ctx, REG_CBUS_DISC_INTR0);
1639
1640 if (stat & VAL_CBUS_MHL_DISCON)
1641 sii8620_mhl_disconnected(ctx);
1642
1643 if (stat & BIT_RGND_READY_INT) {
1644 u8 stat2 = sii8620_readb(ctx, REG_DISC_STAT2);
1645
1646 if ((stat2 & MSK_DISC_STAT2_RGND) == VAL_RGND_1K) {
1647 sii8620_mhl_discover(ctx);
1648 } else {
1649 sii8620_write_seq_static(ctx,
1650 REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
1651 | BIT_DISC_CTRL9_NOMHL_EST
1652 | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
1653 REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT
1654 | BIT_CBUS_MHL3_DISCON_INT
1655 | BIT_CBUS_MHL12_DISCON_INT
1656 | BIT_NOT_MHL_EST_INT
1657 );
1658 }
1659 }
1660 if (stat & BIT_MHL_EST_INT)
1661 sii8620_mhl_init(ctx);
1662
1663 sii8620_write(ctx, REG_CBUS_DISC_INTR0, stat);
1664}
1665
Andrzej Hajda581a9232017-02-01 08:47:49 +01001666static void sii8620_read_burst(struct sii8620 *ctx)
1667{
1668 u8 buf[17];
1669
1670 sii8620_read_buf(ctx, REG_MDT_RCV_READ_PORT, buf, ARRAY_SIZE(buf));
1671 sii8620_write(ctx, REG_MDT_RCV_CTRL, BIT_MDT_RCV_CTRL_MDT_RCV_EN |
1672 BIT_MDT_RCV_CTRL_MDT_DELAY_RCV_EN |
1673 BIT_MDT_RCV_CTRL_MDT_RFIFO_CLR_CUR);
1674 sii8620_readb(ctx, REG_MDT_RFIFO_STAT);
1675}
1676
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001677static void sii8620_irq_g2wb(struct sii8620 *ctx)
1678{
1679 u8 stat = sii8620_readb(ctx, REG_MDT_INT_0);
1680
1681 if (stat & BIT_MDT_IDLE_AFTER_HAWB_DISABLE)
Andrzej Hajda581a9232017-02-01 08:47:49 +01001682 if (sii8620_is_mhl3(ctx))
1683 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
1684 MHL_INT_RC_FEAT_COMPLETE);
1685
1686 if (stat & BIT_MDT_RFIFO_DATA_RDY)
1687 sii8620_read_burst(ctx);
1688
1689 if (stat & BIT_MDT_XFIFO_EMPTY)
1690 sii8620_write(ctx, REG_MDT_XMIT_CTRL, 0);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001691
1692 sii8620_write(ctx, REG_MDT_INT_0, stat);
1693}
1694
Andrzej Hajda9fc6ade2017-02-01 08:47:51 +01001695static void sii8620_status_dcap_ready(struct sii8620 *ctx)
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001696{
Andrzej Hajda9fc6ade2017-02-01 08:47:51 +01001697 enum sii8620_mode mode;
1698
1699 mode = ctx->stat[MHL_DST_VERSION] >= 0x30 ? CM_MHL3 : CM_MHL1;
1700 if (mode > ctx->mode)
1701 sii8620_set_mode(ctx, mode);
1702 sii8620_peer_specific_init(ctx);
1703 sii8620_write(ctx, REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE
1704 | BIT_INTR9_EDID_DONE | BIT_INTR9_EDID_ERROR);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001705}
1706
1707static void sii8620_status_changed_path(struct sii8620 *ctx)
1708{
1709 if (ctx->stat[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED) {
1710 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1711 MHL_DST_LM_CLK_MODE_NORMAL
1712 | MHL_DST_LM_PATH_ENABLED);
Andrzej Hajdae3a65482017-02-01 08:47:36 +01001713 if (!sii8620_is_mhl3(ctx))
1714 sii8620_mt_read_devcap(ctx, false);
Andrzej Hajda9a466cd2017-02-01 08:47:40 +01001715 sii8620_mt_set_cont(ctx, sii8620_sink_detected);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001716 } else {
1717 sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
1718 MHL_DST_LM_CLK_MODE_NORMAL);
1719 }
1720}
1721
1722static void sii8620_msc_mr_write_stat(struct sii8620 *ctx)
1723{
1724 u8 st[MHL_DST_SIZE], xst[MHL_XDS_SIZE];
1725
1726 sii8620_read_buf(ctx, REG_MHL_STAT_0, st, MHL_DST_SIZE);
1727 sii8620_read_buf(ctx, REG_MHL_EXTSTAT_0, xst, MHL_XDS_SIZE);
1728
1729 sii8620_update_array(ctx->stat, st, MHL_DST_SIZE);
1730 sii8620_update_array(ctx->xstat, xst, MHL_XDS_SIZE);
1731
Andrzej Hajda9fc6ade2017-02-01 08:47:51 +01001732 if (ctx->stat[MHL_DST_CONNECTED_RDY] & MHL_DST_CONN_DCAP_RDY)
1733 sii8620_status_dcap_ready(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001734
1735 if (st[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED)
1736 sii8620_status_changed_path(ctx);
1737}
1738
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001739static void sii8620_ecbus_up(struct sii8620 *ctx, int ret)
1740{
1741 if (ret < 0)
1742 return;
1743
1744 sii8620_set_mode(ctx, CM_ECBUS_S);
1745}
1746
1747static void sii8620_got_ecbus_speed(struct sii8620 *ctx, int ret)
1748{
1749 if (ret < 0)
1750 return;
1751
1752 sii8620_mt_write_stat(ctx, MHL_XDS_REG(CURR_ECBUS_MODE),
1753 MHL_XDS_ECBUS_S | MHL_XDS_SLOT_MODE_8BIT);
1754 sii8620_mt_rap(ctx, MHL_RAP_CBUS_MODE_UP);
1755 sii8620_mt_set_cont(ctx, sii8620_ecbus_up);
1756}
1757
Andrzej Hajda269ed8e2017-02-01 08:47:48 +01001758static void sii8620_mhl_burst_emsc_support_set(struct mhl_burst_emsc_support *d,
1759 enum mhl_burst_id id)
1760{
1761 sii8620_mhl_burst_hdr_set(&d->hdr, MHL_BURST_ID_EMSC_SUPPORT);
1762 d->num_entries = 1;
1763 d->burst_id[0] = cpu_to_be16(id);
1764}
1765
1766static void sii8620_send_features(struct sii8620 *ctx)
1767{
1768 u8 buf[16];
1769
1770 sii8620_write(ctx, REG_MDT_XMIT_CTRL, BIT_MDT_XMIT_CTRL_EN
1771 | BIT_MDT_XMIT_CTRL_FIXED_BURST_LEN);
1772 sii8620_mhl_burst_emsc_support_set((void *)buf,
1773 MHL_BURST_ID_HID_PAYLOAD);
1774 sii8620_write_buf(ctx, REG_MDT_XMIT_WRITE_PORT, buf, ARRAY_SIZE(buf));
1775}
1776
Maciej Purskie25f1f72017-08-24 10:58:07 +02001777static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
1778{
1779 bool pressed = !(scancode & MHL_RCP_KEY_RELEASED_MASK);
1780
1781 scancode &= MHL_RCP_KEY_ID_MASK;
1782
1783 if (!ctx->rc_dev) {
1784 dev_dbg(ctx->dev, "RCP input device not initialized\n");
1785 return false;
1786 }
1787
1788 if (pressed)
1789 rc_keydown(ctx->rc_dev, RC_PROTO_CEC, scancode, 0);
1790 else
1791 rc_keyup(ctx->rc_dev);
1792
1793 return true;
1794}
1795
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001796static void sii8620_msc_mr_set_int(struct sii8620 *ctx)
1797{
1798 u8 ints[MHL_INT_SIZE];
1799
1800 sii8620_read_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
1801 sii8620_write_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
Andrzej Hajda2c8fb852017-02-01 08:47:32 +01001802
1803 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_DCAP_CHG) {
1804 switch (ctx->mode) {
1805 case CM_MHL3:
1806 sii8620_mt_read_xdevcap_reg(ctx, MHL_XDC_ECBUS_SPEEDS);
1807 sii8620_mt_set_cont(ctx, sii8620_got_ecbus_speed);
1808 break;
1809 case CM_ECBUS_S:
1810 sii8620_mt_read_devcap(ctx, true);
1811 break;
1812 default:
1813 break;
1814 }
1815 }
Andrzej Hajda269ed8e2017-02-01 08:47:48 +01001816 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_REQ)
1817 sii8620_send_features(ctx);
1818 if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_COMPLETE)
1819 sii8620_edid_read(ctx, 0);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001820}
1821
1822static struct sii8620_mt_msg *sii8620_msc_msg_first(struct sii8620 *ctx)
1823{
1824 struct device *dev = ctx->dev;
1825
1826 if (list_empty(&ctx->mt_queue)) {
1827 dev_err(dev, "unexpected MSC MT response\n");
1828 return NULL;
1829 }
1830
1831 return list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
1832}
1833
1834static void sii8620_msc_mt_done(struct sii8620 *ctx)
1835{
1836 struct sii8620_mt_msg *msg = sii8620_msc_msg_first(ctx);
1837
1838 if (!msg)
1839 return;
1840
1841 msg->ret = sii8620_readb(ctx, REG_MSC_MT_RCVD_DATA0);
1842 ctx->mt_state = MT_STATE_DONE;
1843}
1844
1845static void sii8620_msc_mr_msc_msg(struct sii8620 *ctx)
1846{
Maciej Purskie25f1f72017-08-24 10:58:07 +02001847 struct sii8620_mt_msg *msg;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001848 u8 buf[2];
1849
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001850 sii8620_read_buf(ctx, REG_MSC_MR_MSC_MSG_RCVD_1ST_DATA, buf, 2);
1851
1852 switch (buf[0]) {
1853 case MHL_MSC_MSG_RAPK:
Maciej Purskie25f1f72017-08-24 10:58:07 +02001854 msg = sii8620_msc_msg_first(ctx);
1855 if (!msg)
1856 return;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001857 msg->ret = buf[1];
1858 ctx->mt_state = MT_STATE_DONE;
1859 break;
Maciej Purskie25f1f72017-08-24 10:58:07 +02001860 case MHL_MSC_MSG_RCP:
1861 if (!sii8620_rcp_consume(ctx, buf[1]))
1862 sii8620_mt_rcpe(ctx,
1863 MHL_RCPE_STATUS_INEFFECTIVE_KEY_CODE);
1864 sii8620_mt_rcpk(ctx, buf[1]);
1865 break;
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001866 default:
1867 dev_err(ctx->dev, "%s message type %d,%d not supported",
1868 __func__, buf[0], buf[1]);
1869 }
1870}
1871
1872static void sii8620_irq_msc(struct sii8620 *ctx)
1873{
1874 u8 stat = sii8620_readb(ctx, REG_CBUS_INT_0);
1875
1876 if (stat & ~BIT_CBUS_HPD_CHG)
1877 sii8620_write(ctx, REG_CBUS_INT_0, stat & ~BIT_CBUS_HPD_CHG);
1878
1879 if (stat & BIT_CBUS_HPD_CHG) {
1880 u8 cbus_stat = sii8620_readb(ctx, REG_CBUS_STATUS);
1881
1882 if ((cbus_stat ^ ctx->cbus_status) & BIT_CBUS_STATUS_CBUS_HPD) {
1883 sii8620_write(ctx, REG_CBUS_INT_0, BIT_CBUS_HPD_CHG);
1884 } else {
1885 stat ^= BIT_CBUS_STATUS_CBUS_HPD;
1886 cbus_stat ^= BIT_CBUS_STATUS_CBUS_HPD;
1887 }
1888 ctx->cbus_status = cbus_stat;
1889 }
1890
1891 if (stat & BIT_CBUS_MSC_MR_WRITE_STAT)
1892 sii8620_msc_mr_write_stat(ctx);
1893
1894 if (stat & BIT_CBUS_MSC_MR_SET_INT)
1895 sii8620_msc_mr_set_int(ctx);
1896
1897 if (stat & BIT_CBUS_MSC_MT_DONE)
1898 sii8620_msc_mt_done(ctx);
1899
1900 if (stat & BIT_CBUS_MSC_MR_MSC_MSG)
1901 sii8620_msc_mr_msc_msg(ctx);
1902}
1903
1904static void sii8620_irq_coc(struct sii8620 *ctx)
1905{
1906 u8 stat = sii8620_readb(ctx, REG_COC_INTR);
1907
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01001908 if (stat & BIT_COC_CALIBRATION_DONE) {
1909 u8 cstat = sii8620_readb(ctx, REG_COC_STAT_0);
1910
1911 cstat &= BIT_COC_STAT_0_PLL_LOCKED | MSK_COC_STAT_0_FSM_STATE;
1912 if (cstat == (BIT_COC_STAT_0_PLL_LOCKED | 0x02)) {
1913 sii8620_write_seq_static(ctx,
1914 REG_COC_CTLB, 0,
1915 REG_TRXINTMH, BIT_TDM_INTR_SYNC_DATA
1916 | BIT_TDM_INTR_SYNC_WAIT
1917 );
1918 }
1919 }
1920
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001921 sii8620_write(ctx, REG_COC_INTR, stat);
1922}
1923
1924static void sii8620_irq_merr(struct sii8620 *ctx)
1925{
1926 u8 stat = sii8620_readb(ctx, REG_CBUS_INT_1);
1927
1928 sii8620_write(ctx, REG_CBUS_INT_1, stat);
1929}
1930
1931static void sii8620_irq_edid(struct sii8620 *ctx)
1932{
1933 u8 stat = sii8620_readb(ctx, REG_INTR9);
1934
1935 sii8620_write(ctx, REG_INTR9, stat);
1936
1937 if (stat & BIT_INTR9_DEVCAP_DONE)
1938 ctx->mt_state = MT_STATE_DONE;
1939}
1940
1941static void sii8620_scdt_high(struct sii8620 *ctx)
1942{
1943 sii8620_write_seq_static(ctx,
1944 REG_INTR8_MASK, BIT_CEA_NEW_AVI | BIT_CEA_NEW_VSI,
1945 REG_TPI_SC, BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI,
1946 );
1947}
1948
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001949static void sii8620_irq_scdt(struct sii8620 *ctx)
1950{
1951 u8 stat = sii8620_readb(ctx, REG_INTR5);
1952
1953 if (stat & BIT_INTR_SCDT_CHANGE) {
1954 u8 cstat = sii8620_readb(ctx, REG_TMDS_CSTAT_P3);
1955
Maciej Purski5c632e22017-11-09 11:53:42 +01001956 if (cstat & BIT_TMDS_CSTAT_P3_SCDT) {
1957 if (ctx->sink_type == SINK_HDMI)
1958 /* enable infoframe interrupt */
1959 sii8620_scdt_high(ctx);
1960 else
1961 sii8620_start_video(ctx);
1962 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02001963 }
1964
1965 sii8620_write(ctx, REG_INTR5, stat);
1966}
1967
1968static void sii8620_new_vsi(struct sii8620 *ctx)
1969{
1970 u8 vsif[11];
1971
1972 sii8620_write(ctx, REG_RX_HDMI_CTRL2,
1973 VAL_RX_HDMI_CTRL2_DEFVAL |
1974 BIT_RX_HDMI_CTRL2_VSI_MON_SEL_VSI);
1975 sii8620_read_buf(ctx, REG_RX_HDMI_MON_PKT_HEADER1, vsif,
1976 ARRAY_SIZE(vsif));
1977}
1978
1979static void sii8620_new_avi(struct sii8620 *ctx)
1980{
1981 sii8620_write(ctx, REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL);
1982 sii8620_read_buf(ctx, REG_RX_HDMI_MON_PKT_HEADER1, ctx->avif,
1983 ARRAY_SIZE(ctx->avif));
1984}
1985
1986static void sii8620_irq_infr(struct sii8620 *ctx)
1987{
1988 u8 stat = sii8620_readb(ctx, REG_INTR8)
1989 & (BIT_CEA_NEW_VSI | BIT_CEA_NEW_AVI);
1990
1991 sii8620_write(ctx, REG_INTR8, stat);
1992
1993 if (stat & BIT_CEA_NEW_VSI)
1994 sii8620_new_vsi(ctx);
1995
1996 if (stat & BIT_CEA_NEW_AVI)
1997 sii8620_new_avi(ctx);
1998
1999 if (stat & (BIT_CEA_NEW_VSI | BIT_CEA_NEW_AVI))
2000 sii8620_start_video(ctx);
2001}
2002
Andrzej Hajdae3a65482017-02-01 08:47:36 +01002003static void sii8620_got_xdevcap(struct sii8620 *ctx, int ret)
2004{
2005 if (ret < 0)
2006 return;
2007
2008 sii8620_mt_read_devcap(ctx, false);
2009}
2010
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002011static void sii8620_irq_tdm(struct sii8620 *ctx)
2012{
2013 u8 stat = sii8620_readb(ctx, REG_TRXINTH);
2014 u8 tdm = sii8620_readb(ctx, REG_TRXSTA2);
2015
2016 if ((tdm & MSK_TDM_SYNCHRONIZED) == VAL_TDM_SYNCHRONIZED) {
2017 ctx->mode = CM_ECBUS_S;
2018 ctx->burst.rx_ack = 0;
2019 ctx->burst.r_size = SII8620_BURST_BUF_LEN;
2020 sii8620_burst_tx_rbuf_info(ctx, SII8620_BURST_BUF_LEN);
2021 sii8620_mt_read_devcap(ctx, true);
Andrzej Hajdae3a65482017-02-01 08:47:36 +01002022 sii8620_mt_set_cont(ctx, sii8620_got_xdevcap);
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002023 } else {
2024 sii8620_write_seq_static(ctx,
2025 REG_MHL_PLL_CTL2, 0,
2026 REG_MHL_PLL_CTL2, BIT_MHL_PLL_CTL2_CLKDETECT_EN
2027 );
2028 }
2029
2030 sii8620_write(ctx, REG_TRXINTH, stat);
2031}
2032
2033static void sii8620_irq_block(struct sii8620 *ctx)
2034{
2035 u8 stat = sii8620_readb(ctx, REG_EMSCINTR);
2036
2037 if (stat & BIT_EMSCINTR_SPI_DVLD) {
2038 u8 bstat = sii8620_readb(ctx, REG_SPIBURSTSTAT);
2039
2040 if (bstat & BIT_SPIBURSTSTAT_EMSC_NORMAL_MODE)
2041 sii8620_burst_receive(ctx);
2042 }
2043
2044 sii8620_write(ctx, REG_EMSCINTR, stat);
2045}
2046
Andrzej Hajda263b5c92017-02-01 08:47:44 +01002047static void sii8620_irq_ddc(struct sii8620 *ctx)
2048{
2049 u8 stat = sii8620_readb(ctx, REG_INTR3);
2050
2051 if (stat & BIT_DDC_CMD_DONE) {
2052 sii8620_write(ctx, REG_INTR3_MASK, 0);
2053 if (sii8620_is_mhl3(ctx))
2054 sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
2055 MHL_INT_RC_FEAT_REQ);
2056 else
2057 sii8620_edid_read(ctx, 0);
2058 }
2059 sii8620_write(ctx, REG_INTR3, stat);
2060}
2061
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002062/* endian agnostic, non-volatile version of test_bit */
2063static bool sii8620_test_bit(unsigned int nr, const u8 *addr)
2064{
2065 return 1 & (addr[nr / BITS_PER_BYTE] >> (nr % BITS_PER_BYTE));
2066}
2067
2068static irqreturn_t sii8620_irq_thread(int irq, void *data)
2069{
2070 static const struct {
2071 int bit;
2072 void (*handler)(struct sii8620 *ctx);
2073 } irq_vec[] = {
2074 { BIT_FAST_INTR_STAT_DISC, sii8620_irq_disc },
2075 { BIT_FAST_INTR_STAT_G2WB, sii8620_irq_g2wb },
2076 { BIT_FAST_INTR_STAT_COC, sii8620_irq_coc },
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002077 { BIT_FAST_INTR_STAT_TDM, sii8620_irq_tdm },
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002078 { BIT_FAST_INTR_STAT_MSC, sii8620_irq_msc },
2079 { BIT_FAST_INTR_STAT_MERR, sii8620_irq_merr },
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002080 { BIT_FAST_INTR_STAT_BLOCK, sii8620_irq_block },
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002081 { BIT_FAST_INTR_STAT_EDID, sii8620_irq_edid },
Andrzej Hajda263b5c92017-02-01 08:47:44 +01002082 { BIT_FAST_INTR_STAT_DDC, sii8620_irq_ddc },
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002083 { BIT_FAST_INTR_STAT_SCDT, sii8620_irq_scdt },
2084 { BIT_FAST_INTR_STAT_INFR, sii8620_irq_infr },
2085 };
2086 struct sii8620 *ctx = data;
2087 u8 stats[LEN_FAST_INTR_STAT];
2088 int i, ret;
2089
2090 mutex_lock(&ctx->lock);
2091
2092 sii8620_read_buf(ctx, REG_FAST_INTR_STAT, stats, ARRAY_SIZE(stats));
2093 for (i = 0; i < ARRAY_SIZE(irq_vec); ++i)
2094 if (sii8620_test_bit(irq_vec[i].bit, stats))
2095 irq_vec[i].handler(ctx);
2096
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002097 sii8620_burst_rx_all(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002098 sii8620_mt_work(ctx);
Andrzej Hajdae19e9c62017-02-01 08:47:34 +01002099 sii8620_burst_send(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002100
2101 ret = sii8620_clear_error(ctx);
2102 if (ret) {
2103 dev_err(ctx->dev, "Error during IRQ handling, %d.\n", ret);
2104 sii8620_mhl_disconnected(ctx);
2105 }
2106 mutex_unlock(&ctx->lock);
2107
2108 return IRQ_HANDLED;
2109}
2110
2111static void sii8620_cable_in(struct sii8620 *ctx)
2112{
2113 struct device *dev = ctx->dev;
2114 u8 ver[5];
2115 int ret;
2116
2117 ret = sii8620_hw_on(ctx);
2118 if (ret) {
2119 dev_err(dev, "Error powering on, %d.\n", ret);
2120 return;
2121 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002122
2123 sii8620_read_buf(ctx, REG_VND_IDL, ver, ARRAY_SIZE(ver));
2124 ret = sii8620_clear_error(ctx);
2125 if (ret) {
2126 dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
2127 return;
2128 }
2129
2130 dev_info(dev, "ChipID %02x%02x:%02x%02x rev %02x.\n", ver[1], ver[0],
2131 ver[3], ver[2], ver[4]);
2132
2133 sii8620_write(ctx, REG_DPD,
2134 BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN);
2135
2136 sii8620_xtal_set_rate(ctx);
2137 sii8620_disconnect(ctx);
2138
2139 sii8620_write_seq_static(ctx,
2140 REG_MHL_CBUS_CTL0, VAL_MHL_CBUS_CTL0_CBUS_DRV_SEL_STRONG
2141 | VAL_MHL_CBUS_CTL0_CBUS_RGND_VBIAS_734,
2142 REG_MHL_CBUS_CTL1, VAL_MHL_CBUS_CTL1_1115_OHM,
2143 REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN,
2144 );
2145
2146 ret = sii8620_clear_error(ctx);
2147 if (ret) {
2148 dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
2149 return;
2150 }
2151
2152 enable_irq(to_i2c_client(ctx->dev)->irq);
2153}
2154
Maciej Purskie25f1f72017-08-24 10:58:07 +02002155static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
2156{
2157 struct rc_dev *rc_dev;
2158 int ret;
2159
2160 rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
2161 if (!rc_dev) {
2162 dev_err(ctx->dev, "Failed to allocate RC device\n");
2163 ctx->error = -ENOMEM;
2164 return;
2165 }
2166
2167 rc_dev->input_phys = "sii8620/input0";
2168 rc_dev->input_id.bustype = BUS_VIRTUAL;
2169 rc_dev->map_name = RC_MAP_CEC;
2170 rc_dev->allowed_protocols = RC_PROTO_BIT_CEC;
2171 rc_dev->driver_name = "sii8620";
2172 rc_dev->device_name = "sii8620";
2173
2174 ret = rc_register_device(rc_dev);
2175
2176 if (ret) {
2177 dev_err(ctx->dev, "Failed to register RC device\n");
2178 ctx->error = ret;
2179 rc_free_device(ctx->rc_dev);
2180 return;
2181 }
2182 ctx->rc_dev = rc_dev;
2183}
2184
Maciej Purski68883842018-02-27 08:11:34 +01002185static void sii8620_cable_out(struct sii8620 *ctx)
2186{
2187 disable_irq(to_i2c_client(ctx->dev)->irq);
2188 sii8620_hw_off(ctx);
2189}
2190
2191static void sii8620_extcon_work(struct work_struct *work)
2192{
2193 struct sii8620 *ctx =
2194 container_of(work, struct sii8620, extcon_wq);
2195 int state = extcon_get_state(ctx->extcon, EXTCON_DISP_MHL);
2196
2197 if (state == ctx->cable_state)
2198 return;
2199
2200 ctx->cable_state = state;
2201
2202 if (state > 0)
2203 sii8620_cable_in(ctx);
2204 else
2205 sii8620_cable_out(ctx);
2206}
2207
2208static int sii8620_extcon_notifier(struct notifier_block *self,
2209 unsigned long event, void *ptr)
2210{
2211 struct sii8620 *ctx =
2212 container_of(self, struct sii8620, extcon_nb);
2213
2214 schedule_work(&ctx->extcon_wq);
2215
2216 return NOTIFY_DONE;
2217}
2218
2219static int sii8620_extcon_init(struct sii8620 *ctx)
2220{
2221 struct extcon_dev *edev;
2222 struct device_node *musb, *muic;
2223 int ret;
2224
2225 /* get micro-USB connector node */
2226 musb = of_graph_get_remote_node(ctx->dev->of_node, 1, -1);
2227 /* next get micro-USB Interface Controller node */
2228 muic = of_get_next_parent(musb);
2229
2230 if (!muic) {
2231 dev_info(ctx->dev, "no extcon found, switching to 'always on' mode\n");
2232 return 0;
2233 }
2234
2235 edev = extcon_find_edev_by_node(muic);
2236 of_node_put(muic);
2237 if (IS_ERR(edev)) {
2238 if (PTR_ERR(edev) == -EPROBE_DEFER)
2239 return -EPROBE_DEFER;
2240 dev_err(ctx->dev, "Invalid or missing extcon\n");
2241 return PTR_ERR(edev);
2242 }
2243
2244 ctx->extcon = edev;
2245 ctx->extcon_nb.notifier_call = sii8620_extcon_notifier;
2246 INIT_WORK(&ctx->extcon_wq, sii8620_extcon_work);
2247 ret = extcon_register_notifier(edev, EXTCON_DISP_MHL, &ctx->extcon_nb);
2248 if (ret) {
2249 dev_err(ctx->dev, "failed to register notifier for MHL\n");
2250 return ret;
2251 }
2252
2253 return 0;
2254}
2255
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002256static inline struct sii8620 *bridge_to_sii8620(struct drm_bridge *bridge)
2257{
2258 return container_of(bridge, struct sii8620, bridge);
2259}
2260
Maciej Purskie25f1f72017-08-24 10:58:07 +02002261static int sii8620_attach(struct drm_bridge *bridge)
2262{
2263 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2264
2265 sii8620_init_rcp_input_dev(ctx);
2266
2267 return sii8620_clear_error(ctx);
2268}
2269
2270static void sii8620_detach(struct drm_bridge *bridge)
2271{
2272 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2273
2274 rc_unregister_device(ctx->rc_dev);
2275}
2276
Marek Szyprowskie7c2e4d2017-11-09 11:28:31 +01002277static enum drm_mode_status sii8620_mode_valid(struct drm_bridge *bridge,
2278 const struct drm_display_mode *mode)
2279{
2280 struct sii8620 *ctx = bridge_to_sii8620(bridge);
2281 bool can_pack = ctx->devcap[MHL_DCAP_VID_LINK_MODE] &
2282 MHL_DCAP_VID_LINK_PPIXEL;
2283 unsigned int max_pclk = sii8620_is_mhl3(ctx) ? MHL3_MAX_LCLK :
2284 MHL1_MAX_LCLK;
2285 max_pclk /= can_pack ? 2 : 3;
2286
2287 return (mode->clock > max_pclk) ? MODE_CLOCK_HIGH : MODE_OK;
2288}
2289
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002290static bool sii8620_mode_fixup(struct drm_bridge *bridge,
2291 const struct drm_display_mode *mode,
2292 struct drm_display_mode *adjusted_mode)
2293{
2294 struct sii8620 *ctx = bridge_to_sii8620(bridge);
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002295 int max_lclk;
2296 bool ret = true;
2297
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002298 mutex_lock(&ctx->lock);
2299
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002300 max_lclk = sii8620_is_mhl3(ctx) ? MHL3_MAX_LCLK : MHL1_MAX_LCLK;
2301 if (max_lclk > 3 * adjusted_mode->clock) {
2302 ctx->use_packed_pixel = 0;
2303 goto end;
2304 }
2305 if ((ctx->devcap[MHL_DCAP_VID_LINK_MODE] & MHL_DCAP_VID_LINK_PPIXEL) &&
2306 max_lclk > 2 * adjusted_mode->clock) {
2307 ctx->use_packed_pixel = 1;
2308 goto end;
2309 }
2310 ret = false;
2311end:
2312 if (ret) {
2313 u8 vic = drm_match_cea_mode(adjusted_mode);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002314
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002315 if (!vic) {
2316 union hdmi_infoframe frm;
2317 u8 mhl_vic[] = { 0, 95, 94, 93, 98 };
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002318
Ville Syrjäläf1781e92017-11-13 19:04:19 +02002319 /* FIXME: We need the connector here */
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002320 drm_hdmi_vendor_infoframe_from_display_mode(
Ville Syrjäläf1781e92017-11-13 19:04:19 +02002321 &frm.vendor.hdmi, NULL, adjusted_mode);
Andrzej Hajdabf1722ca2017-02-01 08:47:47 +01002322 vic = frm.vendor.hdmi.vic;
2323 if (vic >= ARRAY_SIZE(mhl_vic))
2324 vic = 0;
2325 vic = mhl_vic[vic];
2326 }
2327 ctx->video_code = vic;
2328 ctx->pixel_clock = adjusted_mode->clock;
2329 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002330 mutex_unlock(&ctx->lock);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002331 return ret;
2332}
2333
2334static const struct drm_bridge_funcs sii8620_bridge_funcs = {
Maciej Purskie25f1f72017-08-24 10:58:07 +02002335 .attach = sii8620_attach,
2336 .detach = sii8620_detach,
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002337 .mode_fixup = sii8620_mode_fixup,
Marek Szyprowskie7c2e4d2017-11-09 11:28:31 +01002338 .mode_valid = sii8620_mode_valid,
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002339};
2340
2341static int sii8620_probe(struct i2c_client *client,
2342 const struct i2c_device_id *id)
2343{
2344 struct device *dev = &client->dev;
2345 struct sii8620 *ctx;
2346 int ret;
2347
2348 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
2349 if (!ctx)
2350 return -ENOMEM;
2351
2352 ctx->dev = dev;
2353 mutex_init(&ctx->lock);
2354 INIT_LIST_HEAD(&ctx->mt_queue);
2355
2356 ctx->clk_xtal = devm_clk_get(dev, "xtal");
2357 if (IS_ERR(ctx->clk_xtal)) {
2358 dev_err(dev, "failed to get xtal clock from DT\n");
2359 return PTR_ERR(ctx->clk_xtal);
2360 }
2361
2362 if (!client->irq) {
2363 dev_err(dev, "no irq provided\n");
2364 return -EINVAL;
2365 }
2366 irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
2367 ret = devm_request_threaded_irq(dev, client->irq, NULL,
2368 sii8620_irq_thread,
2369 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2370 "sii8620", ctx);
Andrzej Hajdae0ba12e2017-02-22 12:47:37 +01002371 if (ret < 0) {
2372 dev_err(dev, "failed to install IRQ handler\n");
2373 return ret;
2374 }
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002375
2376 ctx->gpio_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
2377 if (IS_ERR(ctx->gpio_reset)) {
2378 dev_err(dev, "failed to get reset gpio from DT\n");
2379 return PTR_ERR(ctx->gpio_reset);
2380 }
2381
2382 ctx->supplies[0].supply = "cvcc10";
2383 ctx->supplies[1].supply = "iovcc18";
2384 ret = devm_regulator_bulk_get(dev, 2, ctx->supplies);
2385 if (ret)
2386 return ret;
2387
Maciej Purski68883842018-02-27 08:11:34 +01002388 ret = sii8620_extcon_init(ctx);
2389 if (ret < 0) {
2390 dev_err(ctx->dev, "failed to initialize EXTCON\n");
2391 return ret;
2392 }
2393
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002394 i2c_set_clientdata(client, ctx);
2395
2396 ctx->bridge.funcs = &sii8620_bridge_funcs;
2397 ctx->bridge.of_node = dev->of_node;
2398 drm_bridge_add(&ctx->bridge);
2399
Maciej Purski68883842018-02-27 08:11:34 +01002400 if (!ctx->extcon)
2401 sii8620_cable_in(ctx);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002402
2403 return 0;
2404}
2405
2406static int sii8620_remove(struct i2c_client *client)
2407{
2408 struct sii8620 *ctx = i2c_get_clientdata(client);
2409
Maciej Purski68883842018-02-27 08:11:34 +01002410 if (ctx->extcon) {
2411 extcon_unregister_notifier(ctx->extcon, EXTCON_DISP_MHL,
2412 &ctx->extcon_nb);
2413 flush_work(&ctx->extcon_wq);
2414 if (ctx->cable_state > 0)
2415 sii8620_cable_out(ctx);
2416 } else {
2417 sii8620_cable_out(ctx);
2418 }
Maciej Purskie25f1f72017-08-24 10:58:07 +02002419 drm_bridge_remove(&ctx->bridge);
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002420
2421 return 0;
2422}
2423
2424static const struct of_device_id sii8620_dt_match[] = {
2425 { .compatible = "sil,sii8620" },
2426 { },
2427};
2428MODULE_DEVICE_TABLE(of, sii8620_dt_match);
2429
2430static const struct i2c_device_id sii8620_id[] = {
2431 { "sii8620", 0 },
2432 { },
2433};
2434
2435MODULE_DEVICE_TABLE(i2c, sii8620_id);
2436static struct i2c_driver sii8620_driver = {
2437 .driver = {
2438 .name = "sii8620",
Andrzej Hajdace6e1532016-10-10 09:39:17 +02002439 .of_match_table = of_match_ptr(sii8620_dt_match),
2440 },
2441 .probe = sii8620_probe,
2442 .remove = sii8620_remove,
2443 .id_table = sii8620_id,
2444};
2445
2446module_i2c_driver(sii8620_driver);
2447MODULE_LICENSE("GPL v2");