aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/hdmi/Sii8338/si_drv_mdt_tx.c
blob: 3d9373c6eb5e6604cc4412e5427e8df53fc26690 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 Silicon Image Driver Extension

 Copyright (C) 2012 Silicon Image Inc.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
 published by the Free Software Foundation version 2.

 This program is distributed .as is. WITHOUT ANY WARRANTY of any
 kind, whether express or implied; without even the implied warranty
 of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 PURPOSE.  See the
 GNU General Public License for more details.
*/
/* !file     si_drv_mdt_tx.c */
/* !brief    Silicon Image implementation */
/*  */

#ifdef MDT_SUPPORT

#include "si_common.h"
#ifndef	__KERNEL__
#include "hal_timers.h"
#endif				/* not defined __KERNEL */

#include "sii_hal.h"
#include "si_cra.h"
#include "si_cra_cfg.h"
#include "si_mhl_defs.h"
#include "si_mhl_tx_api.h"
#include "si_mhl_tx_base_drv_api.h"
#include "si_drv_mdt_tx.h"
#include "si_mdt_inputdev.h"

#include "../../platform/hal/sii_hal_priv.h"

#ifdef MDT_SUPPORT
unsigned char g_mdt_is_ready = 0;
struct device *g_mdt_dev = 0;
struct class *g_mdt_class = 0;

static struct g_mdt_t {

	struct i2c_client *i2c_cbus_client;
	unsigned char is_ready;

} g_mdt = {
.i2c_cbus_client = 0, .is_ready = 0,};
#endif

/* #define mdt_i2c_cbus_read_block() */
/* #define mdt_i2c_cbus_write_block() */

static void mdt_cbus_read_block(unsigned char reg_offset, unsigned char byte_count,
				unsigned char *data)
{
	unsigned char ret;
#if 0
	struct i2c_msg i2cMsg[2];

	i2cMsg[0].addr = (0xC8 >> 1);
	i2cMsg[0].flags = 0;
	i2cMsg[0].len = 1;
	i2cMsg[0].buf = &reg_offset;

	i2cMsg[1].addr = (0xC8 >> 1);
	i2cMsg[1].flags = 1;
	i2cMsg[1].len = byte_count;
	i2cMsg[1].buf = data;

	if (g_mdt.i2c_cbus_client == 0) {
		return;
	}

	ret = i2c_transfer(g_mdt.i2c_cbus_client->adapter, i2cMsg, 2);

	if (ret < 0) {
		return;
	}
#else

	int32_t status;
	u32 client_main_addr = g_mdt.i2c_cbus_client->addr;
	g_mdt.i2c_cbus_client->addr = 0xC8 >> 1;

	/* set spedd, panjie */
	g_mdt.i2c_cbus_client->ext_flag |= I2C_HS_FLAG;
	/*  */

	g_mdt.i2c_cbus_client->ext_flag |= I2C_DIRECTION_FLAG;
	status = i2c_master_send(g_mdt.i2c_cbus_client, (const char *)&reg_offset, 1);
	if (status < 0) {
		printk(" mdt_cbus_read_block,%d send error\n", __LINE__);
	}
	/* set spedd, panjie */
	g_mdt.i2c_cbus_client->ext_flag |= I2C_HS_FLAG;
	/*  */


	status = i2c_master_recv(g_mdt.i2c_cbus_client, data, byte_count);

	g_mdt.i2c_cbus_client->addr = client_main_addr;
#endif




#ifdef I2C_ANALYZER
	MHL_log_event(I2C_BLOCK_0x73, reg_offset, data[0]);
#endif
}

static void mdt_cbus_write_block(unsigned char reg_offset, unsigned char byte_count,
				 unsigned char *data)
{
	unsigned char ret;
	unsigned char buffer[0x0F + 1];

	if (byte_count > 0x0F)
		return;

	buffer[0] = reg_offset;

	memcpy(&buffer[1], data, byte_count);
	ret = i2c_master_send(g_mdt.i2c_cbus_client, buffer, (byte_count + 1));
	if (ret < 0) {
		return;
	}
#ifdef I2C_ANALYZER
	MHL_log_event(I2C_BLOCK_0x72, reg_offset, data[0]);
#endif
}

static void mdt_cbus_write_byte(unsigned char reg_offset, unsigned char value)
{
	mdt_cbus_write_block(reg_offset, 1, &value);
}


static unsigned char mdt_cbus_read_byte(unsigned char reg_offset)
{
	unsigned char ret;
	mdt_cbus_read_block(reg_offset, 1, &ret);
	return ret;

}

#ifdef MDT_SUPPORT_DEBUG
struct input_event g_events[15000];
struct input_event *g_e;
static void init_log_file(void);
static void deregster_log_file(void);
#endif


struct msc_request g_prior_msc_request = { 0, 0 };


extern struct si_mdt_inputdevs_t mdt;

static uint8_t sii8338_parse_received_burst_for_mdt(union mdt_event_t *mdt_packet,
						    SiiReg_t scratchpad_offset)
{
	memset(mdt_packet->bytes, 0x0, 0x0F);

	mdt_cbus_read_block(scratchpad_offset, MDT_MIN_PACKET_LENGTH, mdt_packet->bytes);

	if (mdt_packet->header.isHID == 0)
		return 0xFF;

	if ((mdt_packet->header.isKeyboard) || (mdt_packet->header.isNotLast)
	    || (mdt_packet->event_mouse.header.isNotMouse))

		mdt_cbus_read_block(scratchpad_offset + MDT_MIN_PACKET_LENGTH,
				    MDT_KEYBOARD_PACKET_TAIL_LENGTH,
				    (mdt_packet->bytes + MDT_MIN_PACKET_LENGTH));

	printk(KERN_ERR "MHL data: %x %x %x %x %x %x %x %x %x\n",
	       mdt_packet->bytes[0],
	       mdt_packet->bytes[1],
	       mdt_packet->bytes[2],
	       mdt_packet->bytes[3],
	       mdt_packet->bytes[4],
	       mdt_packet->bytes[5],
	       mdt_packet->bytes[6], mdt_packet->bytes[7], mdt_packet->bytes[8]);


	printk(KERN_ERR "MHL info: %x %x %x\n",
	       mdt_packet->header.isKeyboard,
	       mdt_packet->event_cursor.header.touch.isNotMouse,
	       mdt_packet->event_cursor.body.suffix.isGame);


	if (mdt_packet->header.isKeyboard) {
		mdt_generate_event_keyboard(&(mdt_packet->event_keyboard));
	} else if (mdt_packet->event_cursor.header.touch.isNotMouse == 0) {
		mdt_generate_event_mouse(&(mdt_packet->event_mouse));
	} else if (mdt_packet->event_cursor.body.suffix.isGame == 0) {
		mdt_generate_event_touchscreen(&(mdt_packet->event_cursor), 1);
	} else {
		mdt_generate_event_gamepad(&(mdt_packet->event_cursor));
	}
	return 0;
}

static void sii8338_msc_req_for_mdt(uint8_t req_type, uint8_t offset, uint8_t first_data)
{
	if ((offset != g_prior_msc_request.offset) &&
	    (first_data != g_prior_msc_request.first_data)) {
		g_prior_msc_request.offset = offset;
		g_prior_msc_request.first_data = first_data;
		mdt_cbus_write_block(0x13, 2, (uint8_t *) &g_prior_msc_request);
	} else if (offset != g_prior_msc_request.offset) {
		g_prior_msc_request.offset = offset;
		SiiRegWrite(TX_PAGE_CBUS | 0x0013, g_prior_msc_request.offset);
	} else if (first_data != g_prior_msc_request.first_data) {
		g_prior_msc_request.first_data = first_data;
		SiiRegWrite(TX_PAGE_CBUS | 0x0014, g_prior_msc_request.first_data);
	}

	SiiRegWrite(TX_PAGE_CBUS | 0x0012, req_type);
}


uint8_t sii8338_irq_for_mdt(enum mdt_state *mdt_state)
{
	uint8_t ret = 0;
	uint8_t intr;
	union mdt_event_t mdt_packet;	/* Should not use typedef. Change this in the future. */

	MHL_log_event(ISR_MDT_BEGIN, 0, *mdt_state);

	mdt_init();
	switch (*mdt_state) {
	case WAIT_FOR_REQ_WRT:
		intr = SiiRegRead(TX_PAGE_CBUS | 0x00A0);
		MHL_log_event(ISR_WRITEBURST_CAUGHT, TX_PAGE_CBUS | 0x00A0, intr);
		if (intr & MHL_INT_REQ_WRT) {
			sii8338_msc_req_for_mdt(0x01 << 3, OFFSET_SET_INT, GRT_WRT);
			SiiRegWrite(TX_PAGE_CBUS | 0x00A0, (MHL_INT_REQ_WRT | MHL_INT_DSCR_CHG));

			ret = MDT_EVENT_HANDLED;
			*mdt_state = WAIT_FOR_GRT_WRT_COMPLETE;

		}
		break;
	case WAIT_FOR_GRT_WRT_COMPLETE:
		intr = SiiRegRead(TX_PAGE_CBUS | 0x0008);
		MHL_log_event(ISR_WRITEBURST_CAUGHT, TX_PAGE_CBUS | 0x0008, intr);
		if (intr & BIT4) {
			SiiRegWrite(TX_PAGE_CBUS | 0x0008, BIT4);

			ret = MDT_EVENT_HANDLED;
			*mdt_state = WAIT_FOR_WRITE_BURST_COMPLETE;
		}
		break;
	case WAIT_FOR_WRITE_BURST_COMPLETE:
		intr = SiiRegRead(TX_PAGE_CBUS | 0x001E);
		MHL_log_event(ISR_WRITEBURST_CAUGHT, TX_PAGE_CBUS | 0x001E, intr);
		if (intr & BIT0) {
			sii8338_parse_received_burst_for_mdt(&mdt_packet, TX_PAGE_CBUS | 0x00C2);
			if (mdt_packet.header.isNotLast)
				sii8338_parse_received_burst_for_mdt(&mdt_packet,
								     TX_PAGE_CBUS | 0x00C9);
			SiiRegWrite(TX_PAGE_CBUS | 0x001E, BIT0);

			ret = MDT_EVENT_HANDLED;
			*mdt_state = WAIT_FOR_REQ_WRT;
		}
		break;
	case IDLE:
	default:
		break;
	}

	MHL_log_event(ISR_MDT_END, 0xFF, 0xFF);

	return ret;
}

void mdt_init(void)
{
	if (g_mdt_is_ready != 0)
		return;

	if (gMhlDevice.pI2cClient == 0)
		return;

	g_mdt.i2c_cbus_client = gMhlDevice.pI2cClient;

#ifdef MDT_SUPPORT_DEBUG
	init_log_file();
#endif
	mdt_input_init();

	g_mdt_is_ready = 1;
}

void mdt_deregister(void)
{
	if (g_mdt_is_ready == 0)
		return;

#ifdef MDT_SUPPORT_DEBUG
	deregster_log_file();
#endif
	mdt_input_deregister();

	g_mdt_is_ready = 0;
}


#ifdef MDT_SUPPORT_DEBUG

static void init_events(void)
{
	int wevent;
	g_e = g_events;
	for (wevent = (int)0; wevent < (int)ARRAY_SIZE(g_events); wevent++)
		g_events[wevent].type = (int)-1;
}

inline void MHL_log_event(int type, int code, int value)
{
	printk(KERN_ERR "MDT %x %x %x\n", type, code, value);
/*
	struct input_event *e	 = g_e;

	if (g_mdt_is_ready == 0)
		return;

	do_gettimeofday(&(e->time));
	e->type = type;
	e->code = code;
	e->value = value;

	if ((++e - g_events) >= ARRAY_SIZE(g_events))
		e = g_events;
	e->type = -1;
	g_e = e;
*/
}

struct input_event *for_each_valid_event(struct input_event *e)
{
	struct input_event *estart = e++;

	if ((e - g_events) >= ARRAY_SIZE(g_events))
		e = g_events;

	while (e->type == (__u16) -1 && e != estart) {
		e++;
		if ((e - g_events) >= ARRAY_SIZE(g_events))
			e = g_events;
	}

	if (e == estart || e->type == (__u16) -1)
		return NULL;
	return e;
}
#endif

/* This part always compiles to support conditional debug control */
#ifdef MDT_SUPPORT_DEBUG
static ssize_t show_events(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct input_event *e = g_e;
	int ret = 0;

	if (g_mdt_is_ready == 0)
		return 0;

	while ((e = for_each_valid_event(e)) != NULL) {
		ret += sprintf(buf + ret, "%8lu.%06lu,", e->time.tv_sec, e->time.tv_usec);
		switch (e->type) {
		case IRQ_HEARTBEAT:
			ret += sprintf(buf + ret, "%s", "IRQ_HEARTBEAT,");
			break;
		case IRQ_WAKE:
			ret += sprintf(buf + ret, "%s", "IRQ_WAKE,");
			break;
		case IRQ_RECEIVED:
			ret += sprintf(buf + ret, "%s", "IRQ_RECEIVED,");
			break;
		case ISR_WRITEBURST_CAUGHT:
			ret += sprintf(buf + ret, "%s", "ISR_WRITEBURST_CAUGHT,");
			break;
		case ISR_WRITEBURST_MISSED:
			ret += sprintf(buf + ret, "%s", "ISR_WRITEBURST_MISSED,");
			break;

		case ISR_DEFFER_SCHEDULED:
			ret += sprintf(buf + ret, "%s", "ISR_DEFFER_SCHEDULED,");
			break;
		case ISR_DEFFER_NO:
			ret += sprintf(buf + ret, "%s", "ISR_DEFFER_NO,");
			break;
		case ISR_DEFFER_BEGIN:
			ret += sprintf(buf + ret, "%s", "ISR_DEFFER_BEGIN,");
			break;
		case ISR_DEFFER_END:
			ret += sprintf(buf + ret, "%s", "ISR_DEFFER_END,");
			break;
		case I2C_BLOCK_R_UNKNOWN:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_UNKNOWN_R,");
			break;
		case I2C_BLOCK_W_UNKNOWN:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_UNKNOWN_W,");
			break;
		case I2C_BLOCK_0x72:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_0x72,");
			break;
		case I2C_BLOCK_0x7A:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_0x7A,");
			break;
		case I2C_BLOCK_0x92:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_0x93,");
			break;
		case I2C_BLOCK_0xC8:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_0xC8,");
			break;
		case I2C_BLOCK_0x73:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_0x73,");
			break;
		case I2C_BLOCK_0x7B:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_0x7B,");
			break;
		case I2C_BLOCK_0x93:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_0x93,");
			break;
		case I2C_BLOCK_0xC9:
			ret += sprintf(buf + ret, "%s", "I2C_BLOCK_0xC9,");
			break;
		case I2C_BYTE_0x72:
			ret += sprintf(buf + ret, "%s", "I2C_BYTE_0x72,");
			break;
		case I2C_BYTE_0x7A:
			ret += sprintf(buf + ret, "%s", "I2C_BYTE_0x7A,");
			break;
		case I2C_BYTE_0x92:
			ret += sprintf(buf + ret, "%s", "I2C_BYTE_0x93,");
			break;
		case I2C_BYTE_0xC8:
			ret += sprintf(buf + ret, "%s", "I2C_BYTE_0xC8,");
			break;
		case I2C_BYTE_0x73:
			ret += sprintf(buf + ret, "%s", "I2C_BYTE_0x73,");
			break;
		case I2C_BYTE_0x7B:
			ret += sprintf(buf + ret, "%s", "I2C_BYTE_0x7B,");
			break;
		case I2C_BYTE_0x93:
			ret += sprintf(buf + ret, "%s", "I2C_BYTE_0x93,");
			break;
		case I2C_BYTE_0xC9:
			ret += sprintf(buf + ret, "%s", "I2C_BYTE_0xC9,");
			break;
		case ISR_THREADED_BEGIN:
			ret += sprintf(buf + ret, "%s", "ISR_THREADED_BEGIN,");
			break;
		case ISR_THREADED_END:
			ret += sprintf(buf + ret, "%s", "ISR_THREADED_END,");
			break;
		case ISR_MDT_BEGIN:
			ret += sprintf(buf + ret, "%s", "ISR_MDT_BEGIN,");
			break;
		case ISR_MDT_END:
			ret += sprintf(buf + ret, "%s", "ISR_MDT_END,");
			break;
		case ISR_FULL_BEGIN:
			ret += sprintf(buf + ret, "%s", "ISR_FULL_BEGIN,");
			break;
		case ISR_FULL_END:
			ret += sprintf(buf + ret, "%s", "ISR_FULL_END,");
			break;
		case TOUCHPAD_BEGIN:
			ret += sprintf(buf + ret, "%s", "TOUCHPAD_BEGIN,");
			break;
		case TOUCHPAD_END:
			ret += sprintf(buf + ret, "%s", "TOUCHPAD_END,");
			break;
		case MHL_ESTABLISHED:
			ret += sprintf(buf + ret, "%s", "MHL_ESTABLISHED,");
			break;
		case MSC_READY:
			ret += sprintf(buf + ret, "%s", "MSC_READY,");
			break;
		case MDT_EVENT_PARSED:
			ret += sprintf(buf + ret, "%s", "MDT_EVENT_PARSED,");
			break;
		case MDT_UNLOCK:
			ret += sprintf(buf + ret, "%s", "MDT_UNLOCK,");
			break;
		case MDT_LOCK:
			ret += sprintf(buf + ret, "%s", "MDT_LOCK,");
			break;
		case MDT_DISCOVER_REQ:
			ret += sprintf(buf + ret, "%s", "MDT_DISCOVERY_REQ,");
			break;
		}

		ret += sprintf(buf + ret, "%02x,%02x,%02x\n", e->type, e->code, e->value);
		e->type = -1;

		if (ret > (PAGE_SIZE - 512))
			return ret;
	}

	return ret;
}

static DEVICE_ATTR(MDT_file, S_IRUGO, show_events, NULL);

static void deregster_log_file(void)
{
	if (g_mdt_is_ready == 0)
		return;
	if (g_mdt_class == 0)
		return;
	if (g_mdt_dev == 0)
		return;

	device_destroy(g_mdt_class, 0);
	class_destroy(g_mdt_class);

	g_mdt_class = 0;
	g_mdt_dev = 0;
}

static void init_log_file(void)
{
	if (g_mdt_is_ready != 0)
		return;
	if (g_mdt_class != 0)
		return;
	if (g_mdt_dev != 0)
		return;

	init_events();

	g_mdt_is_ready = 1;

	g_mdt_class = class_create(THIS_MODULE, "mdt");
	if (IS_ERR(g_mdt_class)) {
		printk(KERN_ERR "MDT ERR: Failed to create debug helper class.\n");
		return;
	}

	g_mdt_dev = device_create(g_mdt_class, NULL, 0, NULL, "mdt_debug_dev");

	if (IS_ERR(g_mdt_dev)) {
		printk(KERN_ERR "MDT ERR: Failed to create debug helper device.\n");
		return;
	}

	if (device_create_file(g_mdt_dev, &dev_attr_MDT_file) < 0) {
		printk(KERN_ERR "MDT ERR: Failed to create debug helper device file.\n");
		return;
	}

}


#else
inline void MHL_log_event(int type, int code, int value)
{
	printk(KERN_ERR "MDT %x %x %x\n", type, code, value);
}


#endif

#endif