aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.c
blob: 015badc1264be057d04c77b5b73792b0a7abcca3 (plain)
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
/* -*- pse-c -*-
 *-----------------------------------------------------------------------------
 * Filename: sdvo_hdmi.c
 * $Revision: 1.4 $
 *-----------------------------------------------------------------------------
 * Copyright © 2002-2010, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *-----------------------------------------------------------------------------
 * Description:
 *  Port driver HDMI interface functions
 *-----------------------------------------------------------------------------
 */

#include "sdvo_hdmi.h"

#if 0
 /*	HDMI Attributes 
	Currently if omitting out this attributes as this should NOT be avalable
	to user 
	TODO: Integrate this attributes into HDMI  */
/* HDMI attributes */
#define PD_HDMI_COLORIMETRY_256_RGB					0
#define PD_HDMI_COLORIMETRY_220_RGB					1
#define PD_HDMI_COLORIMETRY_422_YCRCB				2
#define PD_HDMI_COLORIMETRY_444_YCRCB				3

#define PD_HDMI_PIXEL_REPLICATION_1X_REPITION				1
#define PD_HDMI_PIXEL_REPLICATION_2X_REPITION				2
#define PD_HDMI_PIXEL_REPLICATION_4X_REPITION				4
/-----------------------------------------------------------------------------/
 {PD_ATTR_ID_COLORIMETRY, MAKE_NAME("Colorimetry"), {4}, {0x0F},
  GET_CURRENT_COLORIMETRY, SET_CURRENT_COLORIMETRY},

  /* 256 RGB is passthrough, real value is 0x01 but 0x00 works as well
	and upon boot up the value is 0x00 */
 {PD_HDMI_COLORIMETRY_256_RGB,
  MAKE_NAME("RGB 256 level"),							{0}, {0x00}, 0, 0}, 
 {PD_HDMI_COLORIMETRY_220_RGB,
  MAKE_NAME("RGB 220 level"),							{0}, {0x02}, 0, 0},
 {PD_HDMI_COLORIMETRY_422_YCRCB,
  MAKE_NAME("4:2:2 YcrCb"),							{0}, {0x04}, 0, 0},
 {PD_HDMI_COLORIMETRY_444_YCRCB,
  MAKE_NAME("4:4:4 YcrCb"),							{0}, {0x08}, 0, 0},

 {PD_ATTR_ID_PIXEL_REPLICATION, MAKE_NAME("Pixel Replication"), {3}, {0x1F},
  GET_PIXEL_REPLICATION, SET_PIXEL_REPLICATION},

 {PD_HDMI_PIXEL_REPLICATION_1X_REPITION,
  MAKE_NAME("No Repetition"),							{0}, {0x00}, 0, 0},
 {PD_HDMI_PIXEL_REPLICATION_2X_REPITION,
  MAKE_NAME("1X Repetition"),							{0}, {0x01}, 0, 0},
 {PD_HDMI_PIXEL_REPLICATION_4X_REPITION,
  MAKE_NAME("3X Repetition"),							{0}, {0x03}, 0, 0}, 
#endif
#if defined(SDVO_HDMI)

/*	============================================================================
	Function	:	sdvo_hdmi_transmitter

	Parameters	:	

	Remarks 	:	Detect if transmitter is a HDMI transmitter.

	Returns 	:	
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_transmitter(sdvo_device_context_t *p_ctx)
{
	return sdvo_get_supported_encoding_modes(p_ctx,&(p_ctx->hdmi.version));
}

/*	============================================================================
	Function	:	sdvo_hdmi_support is called to see if the transmitter 
					and the monitor device support HDMI.
					
	Parameters	:	p_context: Pointer to port driver allocated context
								structure

	Remarks 	:	

	Returns 	:	sdvo_status_t : Status of command execution
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_support(void *p_context)
{
	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
	sdvo_status_t status = SS_SUCCESS;

	/* Monitor support is determined by valid CEA 861 extension and */
	/* vendor block with 0xC03 IEEE regstration ID */
	/*
	if((p_ctx->p_callback->eld) == NULL){
		return SS_NOT_SUPPORTED;
	}

	if((*p_ctx->p_callback->eld)->vendor_block_size) {
		return SS_NOT_SUPPORTED;
	}

	if((*p_ctx->p_callback->eld)->vendor_ieee_id != CEA_IEEE_HDMI_ID) {
		return SS_NOT_SUPPORTED;
	}
	*/
	/* Transmitter support is determined by SDVO GetSupportDigitalEncodingModes */
	status = sdvo_hdmi_transmitter(p_ctx);
	if (status != SS_SUCCESS) {
		return status;
	}

	return SS_SUCCESS;
}



/*	============================================================================
	Function	:	sdvo_hdmi_audio_characteristic

	Parameters	:	

	Remarks 	:	Set audio characteristic to be used in ELD calcilation.
					TODO: Make sure this is recalled if hotplug is detected

	Returns 	:	
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_audio_characteristic(sdvo_device_context_t *p_ctx)
{
	sdvo_status_t status;
	i2c_reg_t hdmi_audio_char[3];

	/* Skip since no CEA data available */
	if((!p_ctx->p_callback) || ((p_ctx->p_callback->eld) == NULL)){
		return SS_NOT_SUPPORTED;
	}
	if(*(p_ctx->p_callback->eld) == NULL){
		return SS_NOT_SUPPORTED;
	}

	status = sdvo_get_hdmi_audio_transmission_char(p_ctx, hdmi_audio_char);
	if(status == SS_SUCCESS){
		(*p_ctx->p_callback->eld)->audio_flag |= PD_AUDIO_CHAR_AVAIL;
		(*p_ctx->p_callback->eld)->NPL = hdmi_audio_char[0];
		(*p_ctx->p_callback->eld)->K0	= hdmi_audio_char[1];
		(*p_ctx->p_callback->eld)->K1	= hdmi_audio_char[2];
	}else{
		(*p_ctx->p_callback->eld)->audio_flag &= ~PD_AUDIO_CHAR_AVAIL;
	}

	return SS_SUCCESS;
}

/*	============================================================================
	Function	:	sdvo_hdmi_configure is called to program AVI infoframes, SPD
					infoframes, ELD data for audio, colorimetry and pixel replication.
					
	Parameters	:	p_context: Pointer to port driver allocated context
								structure
					p_mode: Point to current video output display mode

	Remarks 	:	

	Returns 	:	sdvo_status_t : Status of command execution
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_configure(sdvo_device_context_t *p_ctx)
{
	i2c_reg_t data; 
	sdvo_status_t status = SS_SUCCESS;
	i2c_reg_t encoding_mode = DVI_MODE;

	if((p_ctx->p_callback->eld) == NULL){
		sdvo_set_digital_encoding_mode(p_ctx, encoding_mode);
		return SS_NOT_SUPPORTED;
	}
	if(*(p_ctx->p_callback->eld) == NULL){
		sdvo_set_digital_encoding_mode(p_ctx, encoding_mode);
		return SS_NOT_SUPPORTED;
	}

	/* Prepare buffer for video information. Video only requires a total of 3 
	 * buffers. (0)ELD (1)AVI (2)SPD. SO we will set it to 2 indexbased buffer */ 
	status = sdvo_execute_command(p_ctx, GET_HDMI_BUFFER_AUDIO_VIDEO_SPLIT,
				0, NULL,
				1, &data);
	data = 2;
	status = sdvo_execute_command(p_ctx, SET_HDMI_BUFFER_AUDIO_VIDEO_SPLIT,
				1, &data,
				0, NULL);

	/* Send pixel replication and colorimetry */
	if (sdvo_hdmi_pr_and_color(p_ctx) != SS_SUCCESS) {
		PD_ERROR("Fail toset pixel replication and colorimetry");
	}

	/* Build and send ELD Info Frames */
	if(sdvo_hdmi_send_eld(p_ctx) != SS_SUCCESS){
		PD_ERROR("Fail to write ELD to transmitter");
	}

	/* Build and send AVI Info Frames */
	if (sdvo_hdmi_avi_info_frame(p_ctx) != SS_SUCCESS) {
		PD_ERROR("Fail to write AVI infoframes to transmitter");
	}

	/* Build and send SPD Info Frames */
#ifndef CONFIG_MICRO
	if (sdvo_hdmi_spd_info_frame(p_ctx) != SS_SUCCESS) {
		PD_ERROR("Fail to write SPD Infoframes to transmitter");
	}
#endif
	/* Set to HDMI mode */
	if((*p_ctx->p_callback->eld)->audio_support){
		encoding_mode = HDMI_MODE;
	}
	status = sdvo_set_digital_encoding_mode(p_ctx, encoding_mode);
	if (status != SS_SUCCESS) {
		PD_DEBUG("sdvo: Fail to set HDMI mode ");
	}

	return SS_SUCCESS;
}
/*	============================================================================
	Function	:	sdvo_hdmi_pr_and_color is called to send pixel replication
					and colorimetry data to SDVO device.
					
	Parameters	:	p_context: Pointer to port driver allocated context
								structure

	Remarks 	:	

	Returns 	:	sdvo_status_t : Status of command execution
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_pr_and_color(sdvo_device_context_t *p_context)
{
	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
	sdvo_status_t status = SS_SUCCESS;
	i2c_reg_t pixel_rep = (i2c_reg_t)(*p_ctx->p_callback->eld)->pixel_rep - 1;
	i2c_reg_t quantization = (i2c_reg_t)(*p_ctx->p_callback->eld)->quantization;

	/* Set pixel replication */
	status = sdvo_execute_command(p_ctx, SET_PIXEL_REPLICATION,
				1, &pixel_rep,
				0, NULL);
	if (status != SS_SUCCESS) {
		return status;
	}

	/* Set colorimetry */
	status = sdvo_execute_command(p_ctx, SET_CURRENT_COLORIMETRY,
				1, &quantization,
				0, NULL);
	if (status != SS_SUCCESS) {
		return status;
	}
	return status;
};
/*	============================================================================
	Function	:	sdvo_hdmi_send_eld

	Parameters	:	

	Remarks 	:	Builds eld structure and write it into SDVO ELD buffers

	Returns 	:	
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_send_eld(sdvo_device_context_t *p_ctx)
{
	sdvo_status_t status;	
	sdvo_audio_state_t audio_state;

	if((p_ctx->p_callback->eld) == NULL){
		return SS_NOT_SUPPORTED;
	}
	if(*(p_ctx->p_callback->eld) == NULL){
		return SS_NOT_SUPPORTED;
	}
	if(!((*p_ctx->p_callback->eld)->audio_flag & ELD_AVAIL)){
		PD_DEBUG("Eld not available");
		return SS_NOT_SUPPORTED;
	}

	/* ELD 1.0, CEA version retrieve from callback */
	(*p_ctx->p_callback->eld)->cea_ver = 0;
	(*p_ctx->p_callback->eld)->eld_ver = 1;

	/* Capability Flags */
	(*p_ctx->p_callback->eld)->repeater = 0;
	(*p_ctx->p_callback->eld)->hdcp = 0;
#ifdef CONFIG_MICRO
	(*p_ctx->p_callback->eld)->_44ms = 0;
#endif	

	/* We do not provide Monitor length */
	(*p_ctx->p_callback->eld)->mnl = 0;
	
#ifdef CONFIG_MICRO
	(*p_ctx->p_callback->eld)->sadc = 0;
#endif

	audio_state.value = 0;
	audio_state.eld_valid = FALSE;
	audio_state.presense_detect = TRUE;
	status = sdvo_execute_command(p_ctx, SET_AUDIO_STATE,
				1, (i2c_reg_t *)&audio_state,
				0, NULL);
	if (status != SS_SUCCESS) {
		PD_ERROR("Set Audio eld_valid  fail (FALSE)");
		return status;
	}

	/* Write ELD to SDVO buffers */
	status = sdvo_hdmi_write_buffer(p_ctx, SDVO_HDMI_ELD_BUFFER ,
		0,  (*p_ctx->p_callback->eld)->eld_ptr, SDVO_HDMI_ELD_BUFFER_SIZE);
	if (status != SS_SUCCESS) {
		PD_ERROR("Write ELD Failed !");
		return status;
	}

	/* Send audio state change by toggling eld_valid bit */
	audio_state.eld_valid = TRUE;
	status = sdvo_execute_command(p_ctx, SET_AUDIO_STATE,
				1, (i2c_reg_t *)&audio_state,
				0, NULL);
	if (status != SS_SUCCESS) {
		PD_ERROR("Set Audio eld_valid  fail (TRUE)");
		return status;
	}

	return SS_SUCCESS;
}
/*	============================================================================
	Function	:	sdvo_hdmi_avi_info_frame is called to program AVI infoframes
					
	Parameters	:	p_context: Pointer to port driver allocated context
								structure
					p_mode: Point to current video output display mode

	Remarks 	:	

	Returns 	:	sdvo_status_t : Status of command execution
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_avi_info_frame(sdvo_device_context_t *p_context)
{
	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
	sdvo_status_t status = SS_SUCCESS;
	unsigned long sum = 0;
	int i;
	sdvo_avi_info_t		avi;

	/* Initialize AVI InfoFrame to no scan info, no bar info, no active format */
	/* mode rgb, active format same as picture format */
	/* no aspect ratio, no colorimetry, no scaling */
	pd_memset(&avi, 0, sizeof(sdvo_avi_info_t));
	avi.header.type = SDVO_HDMI_AVI_INFO_TYPE;
	avi.header.version = 2;
	avi.header.length = SDVO_HDMI_AVI_BUFFER_SIZE - sizeof(sdvo_info_header_t);

	/* Fill in VIC, colorimetry, aspect ratio */

	/* Set Video ID Code */
	avi.video_id_code = (unsigned char)(*p_ctx->p_callback->eld)->video_code;

	/* Set aspect ratio */
	avi.pic_aspect_ratio = (unsigned char)(*p_ctx->p_callback->eld)->aspect_ratio;

	/* Set Colorimetry */
	avi.colorimetry = (unsigned char)(*p_ctx->p_callback->eld)->colorimetry;
	
	/* Calc checksum */
	for (i= 0; i < SDVO_HDMI_AVI_BUFFER_SIZE; i++) {
		sum += avi.data[i];
	}
	avi.header.chksum = (unsigned char)(0x100 - (sum & 0xFF));

	/* Send data to SDVO device */
	status = sdvo_hdmi_write_buffer(p_ctx, SDVO_HDMI_AVI_BUFFER, 0, avi.data,
		SDVO_HDMI_AVI_BUFFER_SIZE);
	return status;
};
/*	============================================================================
	Function	:	sdvo_hdmi_spd_info_frame is called to program SPD infoframes
					
	Parameters	:	p_context: Pointer to port driver allocated context
								structure

	Remarks 	:	

	Returns 	:	sdvo_status_t : Status of command execution
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_spd_info_frame(sdvo_device_context_t *p_context)
{
	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
	sdvo_status_t status = SS_SUCCESS;
	sdvo_spd_info_t		spd;
	unsigned long sum = 0;
	int i;

	/* Initialize SPD InfoFrame to zero */
	pd_memset(&spd, 0, sizeof(sdvo_spd_info_t));
	spd.header.type = SDVO_HDMI_SPD_INFO_TYPE;
	spd.header.version = 1;
	spd.header.length = SDVO_HDMI_SPD_BUFFER_SIZE - sizeof(sdvo_info_header_t);

	pd_memcpy(spd.vendor_name, SDVO_HDMI_VENDOR_NAME, SDVO_HDMI_INTEL_VENDOR_NAME_SIZE);
	pd_memcpy(spd.product_desc, 
		SDVO_HDMI_VENDOR_DESCRIPTION, SDVO_HDMI_IEGD_VENDOR_DESCRIPTION_SIZE);
	spd.source_device = SDVO_HDMI_SPD_SOURCE_PC;

	/* Calc checksum */
	for (i= 0; i < SDVO_HDMI_SPD_BUFFER_SIZE; i++) {
		sum += spd.data[i];
	}
	spd.header.chksum = (unsigned char)(0x100 - (sum & 0xFF));

	/* Send data to SDVO device */
	status = sdvo_hdmi_write_buffer
		(p_ctx, SDVO_HDMI_SPD_BUFFER, 0, spd.data, SDVO_HDMI_SPD_BUFFER_SIZE);
	return status;
};
/*	============================================================================
	Function	:	sdvo_hdmi_write_buffer

	Parameters	:	

	Remarks 	:	Write into SDVO buffers

	Returns 	:	
	------------------------------------------------------------------------- */
sdvo_status_t sdvo_hdmi_write_buffer
	(sdvo_device_context_t *p_ctx,unsigned char index, 
	unsigned char offset, unsigned char *input, int len)
{
	sdvo_status_t status;
	i2c_reg_t hdmi_index[2],data[8];
	int buffer_size = 0;
	unsigned char total_bytes, *p_sdvo_buffer, *ori_p_sdvo_buffer;

	/* Set Buffer index and offset */
	hdmi_index[0] = index;
	hdmi_index[1] = offset;
	status = sdvo_execute_command(p_ctx, SET_HDMI_BUFFER_INDEX,
				2, hdmi_index,
				0, NULL);
	if (status != SS_SUCCESS) {
		PD_ERROR("HDMI wirte buffer fail : Set index");
		return status;
	}

	/* Setup buffer transmit rate */
	if(index != SDVO_HDMI_ELD_BUFFER){
		data[0] = SDVO_HDMI_TRANSMIT_EVERY;
		status = sdvo_execute_command(p_ctx, SET_HDMI_BUFFER_TRANSMIT_RATE,
					1, data,
					0, NULL);
		if (status != SS_SUCCESS) {
			PD_ERROR("HDMI wirte buffer fail : Transmite rate");
			return status;
		}
	}
	else {
		status = sdvo_execute_command(p_ctx, GET_HDMI_BUFFER_TRANSMIT_RATE,
					0, NULL,
					1, data);
		if (status != SS_SUCCESS) {
			PD_ERROR("HDMI wirte buffer fail : Transmite rate");
			return status;
		}
	}

	/* Get buffer size info */
	status = sdvo_execute_command(p_ctx, GET_HDMI_BUFFER_INFO,
			0, NULL,
			1, (i2c_reg_t *)&data);
	buffer_size = data[0] + 1;
	/* Minimum buffer size is 3 then something is wrong */
	if (status != SS_SUCCESS || buffer_size <0x03) {
		PD_ERROR("HDMI wirte buffer fail : Buffer size");
		return status;
	}

	/* Make sure to query size of buffer from SDVO and use it, not size of source infoframe buffers */
	/* copy structs into cleared memory the size of SDVO buffer */
	p_sdvo_buffer = pd_malloc(buffer_size);
	ori_p_sdvo_buffer = p_sdvo_buffer;
	pd_memset(p_sdvo_buffer, 0, buffer_size);
	pd_memcpy(p_sdvo_buffer, input, (len < buffer_size) ? len : buffer_size);
	
	while(buffer_size) {
		//total_bytes = (buffer_size >= 8) ? 8:buffer_size;
		if(buffer_size >= 8)	total_bytes = 8;
		else					total_bytes = (unsigned char)buffer_size;	
	
		status = sdvo_execute_command(p_ctx, SET_HDMI_BUFFER_DATA,
				total_bytes, (i2c_reg_t *)p_sdvo_buffer,		
				0, NULL);
		if (status != SS_SUCCESS) {
			PD_ERROR("HDMI wirte buffer fail : Set buffer data");
			return status;
		}	
		p_sdvo_buffer +=total_bytes;
		buffer_size -= total_bytes;
	};

	pd_free(ori_p_sdvo_buffer);
	return SS_SUCCESS;
}

#endif/* SDVO_HDMI */