Newer
Older
/*
* Silicon Image SiI8620 HDMI/MHL bridge driver
*
* Copyright (C) 2015, Samsung Electronics Co., Ltd.
* Andrzej Hajda <a.hajda@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <asm/unaligned.h>
#include <drm/bridge/mhl.h>
#include <drm/drm_crtc.h>
#include <drm/drm_edid.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include "sil-sii8620.h"
#define SII8620_BURST_BUF_LEN 288
#define VAL_RX_HDMI_CTRL2_DEFVAL VAL_RX_HDMI_CTRL2_IDLE_CNT(3)
#define MHL1_MAX_LCLK 225000
#define MHL3_MAX_LCLK 600000
enum sii8620_mode {
CM_DISCONNECTED,
CM_DISCOVERY,
CM_MHL1,
CM_MHL3,
CM_ECBUS_S
};
enum sii8620_sink_type {
SINK_NONE,
SINK_HDMI,
SINK_DVI
};
enum sii8620_mt_state {
MT_STATE_READY,
MT_STATE_BUSY,
MT_STATE_DONE
};
struct sii8620 {
struct drm_bridge bridge;
struct device *dev;
struct clk *clk_xtal;
struct gpio_desc *gpio_reset;
struct gpio_desc *gpio_int;
struct regulator_bulk_data supplies[2];
struct mutex lock; /* context lock, protects fields below */
int error;
int pixel_clock;
unsigned int use_packed_pixel:1;
int video_code;
enum sii8620_mode mode;
enum sii8620_sink_type sink_type;
u8 cbus_status;
u8 stat[MHL_DST_SIZE];
u8 xstat[MHL_XDS_SIZE];
u8 devcap[MHL_DCAP_SIZE];
u8 xdevcap[MHL_XDC_SIZE];
u8 avif[HDMI_INFOFRAME_SIZE(AVI)];
struct edid *edid;
unsigned int gen2_write_burst:1;
enum sii8620_mt_state mt_state;
struct list_head mt_queue;
struct {
int r_size;
int r_count;
int rx_ack;
int rx_count;
u8 rx_buf[32];
int tx_count;
u8 tx_buf[32];
} burst;
};
struct sii8620_mt_msg;
typedef void (*sii8620_mt_msg_cb)(struct sii8620 *ctx,
struct sii8620_mt_msg *msg);
typedef void (*sii8620_cb)(struct sii8620 *ctx, int ret);
struct sii8620_mt_msg {
struct list_head node;
u8 reg[4];
u8 ret;
sii8620_mt_msg_cb send;
sii8620_mt_msg_cb recv;
sii8620_cb continuation;
};
static const u8 sii8620_i2c_page[] = {
0x39, /* Main System */
0x3d, /* TDM and HSIC */
0x49, /* TMDS Receiver, MHL EDID */
0x4d, /* eMSC, HDCP, HSIC */
0x5d, /* MHL Spec */
0x64, /* MHL CBUS */
0x59, /* Hardware TPI (Transmitter Programming Interface) */
0x61, /* eCBUS-S, eCBUS-D */
};
static void sii8620_fetch_edid(struct sii8620 *ctx);
static void sii8620_set_upstream_edid(struct sii8620 *ctx);
static void sii8620_enable_hpd(struct sii8620 *ctx);
static void sii8620_mhl_disconnected(struct sii8620 *ctx);
static void sii8620_disconnect(struct sii8620 *ctx);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
static int sii8620_clear_error(struct sii8620 *ctx)
{
int ret = ctx->error;
ctx->error = 0;
return ret;
}
static void sii8620_read_buf(struct sii8620 *ctx, u16 addr, u8 *buf, int len)
{
struct device *dev = ctx->dev;
struct i2c_client *client = to_i2c_client(dev);
u8 data = addr;
struct i2c_msg msg[] = {
{
.addr = sii8620_i2c_page[addr >> 8],
.flags = client->flags,
.len = 1,
.buf = &data
},
{
.addr = sii8620_i2c_page[addr >> 8],
.flags = client->flags | I2C_M_RD,
.len = len,
.buf = buf
},
};
int ret;
if (ctx->error)
return;
ret = i2c_transfer(client->adapter, msg, 2);
dev_dbg(dev, "read at %04x: %*ph, %d\n", addr, len, buf, ret);
if (ret != 2) {
dev_err(dev, "Read at %#06x of %d bytes failed with code %d.\n",
addr, len, ret);
ctx->error = ret < 0 ? ret : -EIO;
}
}
static u8 sii8620_readb(struct sii8620 *ctx, u16 addr)
{
u8 ret;
sii8620_read_buf(ctx, addr, &ret, 1);
return ret;
}
static void sii8620_write_buf(struct sii8620 *ctx, u16 addr, const u8 *buf,
int len)
{
struct device *dev = ctx->dev;
struct i2c_client *client = to_i2c_client(dev);
u8 data[2];
struct i2c_msg msg = {
.addr = sii8620_i2c_page[addr >> 8],
.flags = client->flags,
.len = len + 1,
};
int ret;
if (ctx->error)
return;
if (len > 1) {
msg.buf = kmalloc(len + 1, GFP_KERNEL);
if (!msg.buf) {
ctx->error = -ENOMEM;
return;
}
memcpy(msg.buf + 1, buf, len);
} else {
msg.buf = data;
msg.buf[1] = *buf;
}
msg.buf[0] = addr;
ret = i2c_transfer(client->adapter, &msg, 1);
dev_dbg(dev, "write at %04x: %*ph, %d\n", addr, len, buf, ret);
if (ret != 1) {
dev_err(dev, "Write at %#06x of %*ph failed with code %d.\n",
addr, len, buf, ret);
ctx->error = ret ?: -EIO;
}
if (len > 1)
kfree(msg.buf);
}
#define sii8620_write(ctx, addr, arr...) \
({\
u8 d[] = { arr }; \
sii8620_write_buf(ctx, addr, d, ARRAY_SIZE(d)); \
})
static void __sii8620_write_seq(struct sii8620 *ctx, const u16 *seq, int len)
{
int i;
for (i = 0; i < len; i += 2)
sii8620_write(ctx, seq[i], seq[i + 1]);
}
#define sii8620_write_seq(ctx, seq...) \
({\
const u16 d[] = { seq }; \
__sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
})
#define sii8620_write_seq_static(ctx, seq...) \
({\
static const u16 d[] = { seq }; \
__sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
})
static void sii8620_setbits(struct sii8620 *ctx, u16 addr, u8 mask, u8 val)
{
val = (val & mask) | (sii8620_readb(ctx, addr) & ~mask);
sii8620_write(ctx, addr, val);
}
static inline bool sii8620_is_mhl3(struct sii8620 *ctx)
{
return ctx->mode >= CM_MHL3;
}
static void sii8620_mt_cleanup(struct sii8620 *ctx)
{
struct sii8620_mt_msg *msg, *n;
list_for_each_entry_safe(msg, n, &ctx->mt_queue, node) {
list_del(&msg->node);
kfree(msg);
}
ctx->mt_state = MT_STATE_READY;
}
static void sii8620_mt_work(struct sii8620 *ctx)
{
struct sii8620_mt_msg *msg;
if (ctx->error)
return;
if (ctx->mt_state == MT_STATE_BUSY || list_empty(&ctx->mt_queue))
return;
if (ctx->mt_state == MT_STATE_DONE) {
ctx->mt_state = MT_STATE_READY;
msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg,
node);
if (msg->recv)
msg->recv(ctx, msg);
if (msg->continuation)
msg->continuation(ctx, msg->ret);
kfree(msg);
}
if (ctx->mt_state != MT_STATE_READY || list_empty(&ctx->mt_queue))
return;
ctx->mt_state = MT_STATE_BUSY;
msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
if (msg->send)
msg->send(ctx, msg);
}
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
static void sii8620_enable_gen2_write_burst(struct sii8620 *ctx)
{
u8 ctrl = BIT_MDT_RCV_CTRL_MDT_RCV_EN;
if (ctx->gen2_write_burst)
return;
if (ctx->mode >= CM_MHL1)
ctrl |= BIT_MDT_RCV_CTRL_MDT_DELAY_RCV_EN;
sii8620_write_seq(ctx,
REG_MDT_RCV_TIMEOUT, 100,
REG_MDT_RCV_CTRL, ctrl
);
ctx->gen2_write_burst = 1;
}
static void sii8620_disable_gen2_write_burst(struct sii8620 *ctx)
{
if (!ctx->gen2_write_burst)
return;
sii8620_write_seq_static(ctx,
REG_MDT_XMIT_CTRL, 0,
REG_MDT_RCV_CTRL, 0
);
ctx->gen2_write_burst = 0;
}
static void sii8620_start_gen2_write_burst(struct sii8620 *ctx)
{
sii8620_write_seq_static(ctx,
REG_MDT_INT_1_MASK, BIT_MDT_RCV_TIMEOUT
| BIT_MDT_RCV_SM_ABORT_PKT_RCVD | BIT_MDT_RCV_SM_ERROR
| BIT_MDT_XMIT_TIMEOUT | BIT_MDT_XMIT_SM_ABORT_PKT_RCVD
| BIT_MDT_XMIT_SM_ERROR,
REG_MDT_INT_0_MASK, BIT_MDT_XFIFO_EMPTY
| BIT_MDT_IDLE_AFTER_HAWB_DISABLE
| BIT_MDT_RFIFO_DATA_RDY
);
sii8620_enable_gen2_write_burst(ctx);
}
static void sii8620_mt_msc_cmd_send(struct sii8620 *ctx,
struct sii8620_mt_msg *msg)
{
if (msg->reg[0] == MHL_SET_INT &&
msg->reg[1] == MHL_INT_REG(RCHANGE) &&
msg->reg[2] == MHL_INT_RC_FEAT_REQ)
sii8620_enable_gen2_write_burst(ctx);
else
sii8620_disable_gen2_write_burst(ctx);
switch (msg->reg[0]) {
case MHL_WRITE_STAT:
case MHL_SET_INT:
sii8620_write_buf(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg + 1, 2);
sii8620_write(ctx, REG_MSC_COMMAND_START,
BIT_MSC_COMMAND_START_WRITE_STAT);
break;
case MHL_MSC_MSG:
sii8620_write_buf(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg, 3);
sii8620_write(ctx, REG_MSC_COMMAND_START,
BIT_MSC_COMMAND_START_MSC_MSG);
break;
case MHL_READ_DEVCAP_REG:
case MHL_READ_XDEVCAP_REG:
sii8620_write(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg[1]);
sii8620_write(ctx, REG_MSC_COMMAND_START,
BIT_MSC_COMMAND_START_READ_DEVCAP);
break;
default:
dev_err(ctx->dev, "%s: command %#x not supported\n", __func__,
msg->reg[0]);
}
}
static struct sii8620_mt_msg *sii8620_mt_msg_new(struct sii8620 *ctx)
{
struct sii8620_mt_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
if (!msg)
ctx->error = -ENOMEM;
else
list_add_tail(&msg->node, &ctx->mt_queue);
return msg;
}
static void sii8620_mt_set_cont(struct sii8620 *ctx, sii8620_cb cont)
{
struct sii8620_mt_msg *msg;
if (ctx->error)
return;
if (list_empty(&ctx->mt_queue)) {
ctx->error = -EINVAL;
return;
}
msg = list_last_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
msg->continuation = cont;
}
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
static void sii8620_mt_msc_cmd(struct sii8620 *ctx, u8 cmd, u8 arg1, u8 arg2)
{
struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
if (!msg)
return;
msg->reg[0] = cmd;
msg->reg[1] = arg1;
msg->reg[2] = arg2;
msg->send = sii8620_mt_msc_cmd_send;
}
static void sii8620_mt_write_stat(struct sii8620 *ctx, u8 reg, u8 val)
{
sii8620_mt_msc_cmd(ctx, MHL_WRITE_STAT, reg, val);
}
static inline void sii8620_mt_set_int(struct sii8620 *ctx, u8 irq, u8 mask)
{
sii8620_mt_msc_cmd(ctx, MHL_SET_INT, irq, mask);
}
static void sii8620_mt_msc_msg(struct sii8620 *ctx, u8 cmd, u8 data)
{
sii8620_mt_msc_cmd(ctx, MHL_MSC_MSG, cmd, data);
}
static void sii8620_mt_rap(struct sii8620 *ctx, u8 code)
{
sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RAP, code);
}
static void sii8620_mt_read_devcap_send(struct sii8620 *ctx,
struct sii8620_mt_msg *msg)
{
u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
| BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
| BIT_EDID_CTRL_EDID_MODE_EN;
if (msg->reg[0] == MHL_READ_XDEVCAP)
ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
sii8620_write_seq(ctx,
REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE,
REG_EDID_CTRL, ctrl,
REG_TPI_CBUS_START, BIT_TPI_CBUS_START_GET_DEVCAP_START
);
}
/* copy src to dst and set changed bits in src */
static void sii8620_update_array(u8 *dst, u8 *src, int count)
{
while (--count >= 0) {
*src ^= *dst;
*dst++ ^= *src++;
}
}
static void sii8620_sink_detected(struct sii8620 *ctx, int ret)
{
static const char * const sink_str[] = {
[SINK_NONE] = "NONE",
[SINK_HDMI] = "HDMI",
[SINK_DVI] = "DVI"
};
char sink_name[20];
struct device *dev = ctx->dev;
return;
sii8620_fetch_edid(ctx);
if (!ctx->edid) {
dev_err(ctx->dev, "Cannot fetch EDID\n");
sii8620_mhl_disconnected(ctx);
return;
}
if (drm_detect_hdmi_monitor(ctx->edid))
ctx->sink_type = SINK_HDMI;
else
ctx->sink_type = SINK_DVI;
drm_edid_get_monitor_name(ctx->edid, sink_name, ARRAY_SIZE(sink_name));
dev_info(dev, "detected sink(type: %s): %s\n",
sink_str[ctx->sink_type], sink_name);
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
static void sii8620_hsic_init(struct sii8620 *ctx)
{
if (!sii8620_is_mhl3(ctx))
return;
sii8620_write(ctx, REG_FCGC,
BIT_FCGC_HSIC_HOSTMODE | BIT_FCGC_HSIC_ENABLE);
sii8620_setbits(ctx, REG_HRXCTRL3,
BIT_HRXCTRL3_HRX_STAY_RESET | BIT_HRXCTRL3_STATUS_EN, ~0);
sii8620_setbits(ctx, REG_TTXNUMB, MSK_TTXNUMB_TTX_NUMBPS, 4);
sii8620_setbits(ctx, REG_TRXCTRL, BIT_TRXCTRL_TRX_FROM_SE_COC, ~0);
sii8620_setbits(ctx, REG_HTXCTRL, BIT_HTXCTRL_HTX_DRVCONN1, 0);
sii8620_setbits(ctx, REG_KEEPER, MSK_KEEPER_MODE, VAL_KEEPER_MODE_HOST);
sii8620_write_seq_static(ctx,
REG_TDMLLCTL, 0,
REG_UTSRST, BIT_UTSRST_HRX_SRST | BIT_UTSRST_HTX_SRST |
BIT_UTSRST_KEEPER_SRST | BIT_UTSRST_FC_SRST,
REG_UTSRST, BIT_UTSRST_HRX_SRST | BIT_UTSRST_HTX_SRST,
REG_HRXINTL, 0xff,
REG_HRXINTH, 0xff,
REG_TTXINTL, 0xff,
REG_TTXINTH, 0xff,
REG_TRXINTL, 0xff,
REG_TRXINTH, 0xff,
REG_HTXINTL, 0xff,
REG_HTXINTH, 0xff,
REG_FCINTR0, 0xff,
REG_FCINTR1, 0xff,
REG_FCINTR2, 0xff,
REG_FCINTR3, 0xff,
REG_FCINTR4, 0xff,
REG_FCINTR5, 0xff,
REG_FCINTR6, 0xff,
REG_FCINTR7, 0xff
);
}
static void sii8620_edid_read(struct sii8620 *ctx, int ret)
{
if (ret < 0)
return;
sii8620_set_upstream_edid(ctx);
sii8620_hsic_init(ctx);
sii8620_enable_hpd(ctx);
}
static void sii8620_mr_devcap(struct sii8620 *ctx)
{
u8 dcap[MHL_DCAP_SIZE];
struct device *dev = ctx->dev;
sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, dcap, MHL_DCAP_SIZE);
if (ctx->error < 0)
return;
dev_info(dev, "detected dongle MHL %d.%d, ChipID %02x%02x:%02x%02x\n",
dcap[MHL_DCAP_MHL_VERSION] / 16,
dcap[MHL_DCAP_MHL_VERSION] % 16,
dcap[MHL_DCAP_ADOPTER_ID_H], dcap[MHL_DCAP_ADOPTER_ID_L],
dcap[MHL_DCAP_DEVICE_ID_H], dcap[MHL_DCAP_DEVICE_ID_L]);
sii8620_update_array(ctx->devcap, dcap, MHL_DCAP_SIZE);
}
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
static void sii8620_mr_xdevcap(struct sii8620 *ctx)
{
sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, ctx->xdevcap,
MHL_XDC_SIZE);
}
static void sii8620_mt_read_devcap_recv(struct sii8620 *ctx,
struct sii8620_mt_msg *msg)
{
u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
| BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
| BIT_EDID_CTRL_EDID_MODE_EN;
if (msg->reg[0] == MHL_READ_XDEVCAP)
ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
sii8620_write_seq(ctx,
REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE | BIT_INTR9_EDID_DONE
| BIT_INTR9_EDID_ERROR,
REG_EDID_CTRL, ctrl,
REG_EDID_FIFO_ADDR, 0
);
if (msg->reg[0] == MHL_READ_XDEVCAP)
sii8620_mr_xdevcap(ctx);
else
sii8620_mr_devcap(ctx);
}
static void sii8620_mt_read_devcap(struct sii8620 *ctx, bool xdevcap)
{
struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
if (!msg)
return;
msg->reg[0] = xdevcap ? MHL_READ_XDEVCAP : MHL_READ_DEVCAP;
msg->send = sii8620_mt_read_devcap_send;
msg->recv = sii8620_mt_read_devcap_recv;
}
static void sii8620_mt_read_devcap_reg_recv(struct sii8620 *ctx,
struct sii8620_mt_msg *msg)
{
ctx->xdevcap[reg] = msg->ret;
else
ctx->devcap[reg] = msg->ret;
}
static void sii8620_mt_read_devcap_reg(struct sii8620 *ctx, u8 reg)
{
struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
if (!msg)
return;
msg->reg[0] = (reg & 0x80) ? MHL_READ_XDEVCAP_REG : MHL_READ_DEVCAP_REG;
msg->reg[1] = reg;
msg->send = sii8620_mt_msc_cmd_send;
msg->recv = sii8620_mt_read_devcap_reg_recv;
}
static inline void sii8620_mt_read_xdevcap_reg(struct sii8620 *ctx, u8 reg)
{
sii8620_mt_read_devcap_reg(ctx, reg | 0x80);
}
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
static void *sii8620_burst_get_tx_buf(struct sii8620 *ctx, int len)
{
u8 *buf = &ctx->burst.tx_buf[ctx->burst.tx_count];
int size = len + 2;
if (ctx->burst.tx_count + size > ARRAY_SIZE(ctx->burst.tx_buf)) {
dev_err(ctx->dev, "TX-BLK buffer exhausted\n");
ctx->error = -EINVAL;
return NULL;
}
ctx->burst.tx_count += size;
buf[1] = len;
return buf + 2;
}
static u8 *sii8620_burst_get_rx_buf(struct sii8620 *ctx, int len)
{
u8 *buf = &ctx->burst.rx_buf[ctx->burst.rx_count];
int size = len + 1;
if (ctx->burst.tx_count + size > ARRAY_SIZE(ctx->burst.tx_buf)) {
dev_err(ctx->dev, "RX-BLK buffer exhausted\n");
ctx->error = -EINVAL;
return NULL;
}
ctx->burst.rx_count += size;
buf[0] = len;
return buf + 1;
}
static void sii8620_burst_send(struct sii8620 *ctx)
{
int tx_left = ctx->burst.tx_count;
u8 *d = ctx->burst.tx_buf;
while (tx_left > 0) {
int len = d[1] + 2;
if (ctx->burst.r_count + len > ctx->burst.r_size)
break;
d[0] = min(ctx->burst.rx_ack, 255);
ctx->burst.rx_ack -= d[0];
sii8620_write_buf(ctx, REG_EMSC_XMIT_WRITE_PORT, d, len);
ctx->burst.r_count += len;
tx_left -= len;
d += len;
}
ctx->burst.tx_count = tx_left;
while (ctx->burst.rx_ack > 0) {
u8 b[2] = { min(ctx->burst.rx_ack, 255), 0 };
if (ctx->burst.r_count + 2 > ctx->burst.r_size)
break;
ctx->burst.rx_ack -= b[0];
sii8620_write_buf(ctx, REG_EMSC_XMIT_WRITE_PORT, b, 2);
ctx->burst.r_count += 2;
}
}
static void sii8620_burst_receive(struct sii8620 *ctx)
{
u8 buf[3], *d;
int count;
sii8620_read_buf(ctx, REG_EMSCRFIFOBCNTL, buf, 2);
count = get_unaligned_le16(buf);
while (count > 0) {
int len = min(count, 3);
sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, buf, len);
count -= len;
ctx->burst.rx_ack += len - 1;
ctx->burst.r_count -= buf[1];
if (ctx->burst.r_count < 0)
ctx->burst.r_count = 0;
if (len < 3 || !buf[2])
continue;
len = buf[2];
d = sii8620_burst_get_rx_buf(ctx, len);
if (!d)
continue;
sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, d, len);
count -= len;
ctx->burst.rx_ack += len;
}
}
static void sii8620_burst_tx_rbuf_info(struct sii8620 *ctx, int size)
{
struct mhl_burst_blk_rcv_buffer_info *d =
sii8620_burst_get_tx_buf(ctx, sizeof(*d));
if (!d)
return;
d->id = cpu_to_be16(MHL_BURST_ID_BLK_RCV_BUFFER_INFO);
d->size = cpu_to_le16(size);
}
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
static u8 sii8620_checksum(void *ptr, int size)
{
u8 *d = ptr, sum = 0;
while (size--)
sum += *d++;
return sum;
}
static void sii8620_mhl_burst_hdr_set(struct mhl3_burst_header *h,
enum mhl_burst_id id)
{
h->id = cpu_to_be16(id);
h->total_entries = 1;
h->sequence_index = 1;
}
static void sii8620_burst_tx_bits_per_pixel_fmt(struct sii8620 *ctx, u8 fmt)
{
struct mhl_burst_bits_per_pixel_fmt *d;
const int size = sizeof(*d) + sizeof(d->desc[0]);
d = sii8620_burst_get_tx_buf(ctx, size);
if (!d)
return;
sii8620_mhl_burst_hdr_set(&d->hdr, MHL_BURST_ID_BITS_PER_PIXEL_FMT);
d->num_entries = 1;
d->desc[0].stream_id = 0;
d->desc[0].pixel_format = fmt;
d->hdr.checksum -= sii8620_checksum(d, size);
}
static void sii8620_burst_rx_all(struct sii8620 *ctx)
{
u8 *d = ctx->burst.rx_buf;
int count = ctx->burst.rx_count;
while (count-- > 0) {
int len = *d++;
int id = get_unaligned_be16(&d[0]);
switch (id) {
case MHL_BURST_ID_BLK_RCV_BUFFER_INFO:
ctx->burst.r_size = get_unaligned_le16(&d[2]);
break;
default:
break;
}
count -= len;
d += len;
}
ctx->burst.rx_count = 0;
}
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
static void sii8620_fetch_edid(struct sii8620 *ctx)
{
u8 lm_ddc, ddc_cmd, int3, cbus;
int fetched, i;
int edid_len = EDID_LENGTH;
u8 *edid;
sii8620_readb(ctx, REG_CBUS_STATUS);
lm_ddc = sii8620_readb(ctx, REG_LM_DDC);
ddc_cmd = sii8620_readb(ctx, REG_DDC_CMD);
sii8620_write_seq(ctx,
REG_INTR9_MASK, 0,
REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
REG_HDCP2X_POLL_CS, 0x71,
REG_HDCP2X_CTRL_0, BIT_HDCP2X_CTRL_0_HDCP2X_HDCPTX,
REG_LM_DDC, lm_ddc | BIT_LM_DDC_SW_TPI_EN_DISABLED,
);
for (i = 0; i < 256; ++i) {
u8 ddc_stat = sii8620_readb(ctx, REG_DDC_STATUS);
if (!(ddc_stat & BIT_DDC_STATUS_DDC_I2C_IN_PROG))
break;
sii8620_write(ctx, REG_DDC_STATUS,
BIT_DDC_STATUS_DDC_FIFO_EMPTY);
}
sii8620_write(ctx, REG_DDC_ADDR, 0x50 << 1);
edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
if (!edid) {
ctx->error = -ENOMEM;
return;
}
#define FETCH_SIZE 16
for (fetched = 0; fetched < edid_len; fetched += FETCH_SIZE) {
sii8620_readb(ctx, REG_DDC_STATUS);
sii8620_write_seq(ctx,
REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_ABORT,
REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_CLEAR_FIFO,
REG_DDC_STATUS, BIT_DDC_STATUS_DDC_FIFO_EMPTY
);
sii8620_write_seq(ctx,
REG_DDC_SEGM, fetched >> 8,
REG_DDC_OFFSET, fetched & 0xff,
REG_DDC_DIN_CNT1, FETCH_SIZE,
REG_DDC_DIN_CNT2, 0,
REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_ENH_DDC_READ_NO_ACK
);
do {
int3 = sii8620_readb(ctx, REG_INTR3);
cbus = sii8620_readb(ctx, REG_CBUS_STATUS);
if (int3 & BIT_DDC_CMD_DONE)
break;
if (!(cbus & BIT_CBUS_STATUS_CBUS_CONNECTED)) {
kfree(edid);
edid = NULL;
goto end;
}
} while (1);
sii8620_readb(ctx, REG_DDC_STATUS);
while (sii8620_readb(ctx, REG_DDC_DOUT_CNT) < FETCH_SIZE)
usleep_range(10, 20);
sii8620_read_buf(ctx, REG_DDC_DATA, edid + fetched, FETCH_SIZE);
if (fetched + FETCH_SIZE == EDID_LENGTH) {
u8 ext = ((struct edid *)edid)->extensions;
if (ext) {
u8 *new_edid;
edid_len += ext * EDID_LENGTH;
new_edid = krealloc(edid, edid_len, GFP_KERNEL);
if (!new_edid) {
kfree(edid);
ctx->error = -ENOMEM;
return;
}
edid = new_edid;
}
}
}
sii8620_write_seq(ctx,
REG_INTR3_MASK, BIT_DDC_CMD_DONE,
REG_LM_DDC, lm_ddc
);
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
end:
kfree(ctx->edid);
ctx->edid = (struct edid *)edid;
}
static void sii8620_set_upstream_edid(struct sii8620 *ctx)
{
sii8620_setbits(ctx, REG_DPD, BIT_DPD_PDNRX12 | BIT_DPD_PDIDCK_N
| BIT_DPD_PD_MHL_CLK_N, 0xff);
sii8620_write_seq_static(ctx,
REG_RX_HDMI_CTRL3, 0x00,
REG_PKT_FILTER_0, 0xFF,
REG_PKT_FILTER_1, 0xFF,
REG_ALICE0_BW_I2C, 0x06
);
sii8620_setbits(ctx, REG_RX_HDMI_CLR_BUFFER,
BIT_RX_HDMI_CLR_BUFFER_VSI_CLR_EN, 0xff);
sii8620_write_seq_static(ctx,
REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
| BIT_EDID_CTRL_EDID_MODE_EN,
REG_EDID_FIFO_ADDR, 0,
);
sii8620_write_buf(ctx, REG_EDID_FIFO_WR_DATA, (u8 *)ctx->edid,
(ctx->edid->extensions + 1) * EDID_LENGTH);
sii8620_write_seq_static(ctx,
REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID
| BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
| BIT_EDID_CTRL_EDID_MODE_EN,
REG_INTR5_MASK, BIT_INTR_SCDT_CHANGE,
REG_INTR9_MASK, 0
);
}
static void sii8620_xtal_set_rate(struct sii8620 *ctx)
{
static const struct {
unsigned int rate;
u8 div;
u8 tp1;
} rates[] = {
{ 19200, 0x04, 0x53 },
{ 20000, 0x04, 0x62 },
{ 24000, 0x05, 0x75 },
{ 30000, 0x06, 0x92 },
{ 38400, 0x0c, 0xbc },
};
unsigned long rate = clk_get_rate(ctx->clk_xtal) / 1000;
int i;
for (i = 0; i < ARRAY_SIZE(rates) - 1; ++i)
if (rate <= rates[i].rate)
break;
if (rate != rates[i].rate)
dev_err(ctx->dev, "xtal clock rate(%lukHz) not supported, setting MHL for %ukHz.\n",
rate, rates[i].rate);
sii8620_write(ctx, REG_DIV_CTL_MAIN, rates[i].div);
sii8620_write(ctx, REG_HDCP2X_TP1, rates[i].tp1);
}
static int sii8620_hw_on(struct sii8620 *ctx)
{
int ret;
ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
if (ret)
return ret;
usleep_range(10000, 20000);
return clk_prepare_enable(ctx->clk_xtal);
}
static int sii8620_hw_off(struct sii8620 *ctx)
{
clk_disable_unprepare(ctx->clk_xtal);
gpiod_set_value(ctx->gpio_reset, 1);
return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
}
static void sii8620_hw_reset(struct sii8620 *ctx)
{
usleep_range(10000, 20000);
gpiod_set_value(ctx->gpio_reset, 0);
usleep_range(5000, 20000);
gpiod_set_value(ctx->gpio_reset, 1);
usleep_range(10000, 20000);
gpiod_set_value(ctx->gpio_reset, 0);
msleep(300);
}
static void sii8620_cbus_reset(struct sii8620 *ctx)
{
sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST
| BIT_PWD_SRST_CBUS_RST_SW_EN);
usleep_range(10000, 20000);
sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN);
}
static void sii8620_set_auto_zone(struct sii8620 *ctx)
{
if (ctx->mode != CM_MHL1) {
sii8620_write_seq_static(ctx,
REG_TX_ZONE_CTL1, 0x0,
REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
| BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
| BIT_MHL_PLL_CTL0_ZONE_MASK_OE
);
} else {
sii8620_write_seq_static(ctx,
REG_TX_ZONE_CTL1, VAL_TX_ZONE_CTL1_TX_ZONE_CTRL_MODE,
REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
| BIT_MHL_PLL_CTL0_ZONE_MASK_OE
);
}