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