summaryrefslogtreecommitdiff
path: root/drivers/video/mcde/display-av8100.c
blob: 567a7d0d1f497ded86c64cb38fba3241f4ec4f83 (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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
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
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
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
877
878
879
880
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
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * ST-Ericsson HDMI display driver
 *
 * Author: Per Persson <per-xb-persson@stericsson.com>
 * for ST-Ericsson.
 *
 * License terms: GNU General Public License (GPL), version 2.
 */

#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/slab.h>

#include <video/mcde_display.h>
#include <video/mcde_display-av8100.h>
#include <video/av8100.h>
#include <video/hdmi.h>

struct display_driver_data {
	struct regulator *cvbs_regulator;
	bool cvbs_regulator_enabled;
};

static int hdmi_try_video_mode(
	struct mcde_display_device *ddev, struct mcde_video_mode *video_mode);
static int hdmi_set_video_mode(
	struct mcde_display_device *ddev, struct mcde_video_mode *video_mode);

static ssize_t store_hdmisdtvswitch(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count);

static DEVICE_ATTR(hdmisdtvswitch, S_IWUSR, NULL, store_hdmisdtvswitch);

static ssize_t store_hdmisdtvswitch(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct mcde_display_device *mdev = to_mcde_display_device(dev);
	dev_dbg(dev, "%s\n", __func__);

	if (count > 0) {
		if ((*buf == 0) || (*buf == '0')) {
			dev_dbg(dev, "hdmi/sdtv switch = hdmi\n");
			mdev->port->hdmi_sdtv_switch = HDMI_SWITCH;
			mdev->native_x_res = NATIVE_XRES_HDMI;
			mdev->native_y_res = NATIVE_YRES_HDMI;
			mdev->video_mode.force_update = true;
		} else {
			dev_dbg(dev, "hdmi/sdtv switch = sdtv\n");
			mdev->port->hdmi_sdtv_switch = SDTV_SWITCH;
			mdev->native_x_res = NATIVE_XRES_SDTV;
			mdev->native_y_res = NATIVE_YRES_SDTV;
			mdev->video_mode.force_update = true;
		}
	}

	return count;
}

/* Supported HDMI modes */
static struct mcde_video_mode video_modes_supp_hdmi[] = {
	/* 640_480_60_P */
	{
		.xres = 640,	.yres = 480,
		.pixclock = 39682,
		.hbp = 112,	.hfp = 48,
		.vbp1 = 33,	.vfp1 = 12
	},
	/* 720_480_60_P */
	{
		.xres = 720,	.yres = 480,
		.pixclock = 37000,
		.hbp = 104,	.hfp = 34,
		.vbp1 = 30,	.vfp1 = 15
	},
	/* 720_576_50_P */
	{
		.xres = 720,	.yres = 576,
		.pixclock = 37037,
		.hbp = 132,	.hfp = 12,
		.vbp1 = 44,	.vfp1 = 5
	},
	/* 1280_720_60_P */
	{
		.xres = 1280,	.yres = 720,
		.pixclock = 13468,
		.hbp = 256,	.hfp = 114,
		.vbp1 = 20,	.vfp1 = 10
	},
	/* 1280_720_50_P */
	{
		.xres = 1280,	.yres = 720,
		.pixclock = 13468,
		.hbp = 260,	.hfp = 440,
		.vbp1 = 25,	.vfp1 = 5
	},
	/* 1920_1080_30_P */
	{
		.xres = 1920,	.yres = 1080,
		.pixclock = 13468,
		.hbp = 189,	.hfp = 91,
		.vbp1 = 36,	.vfp1 = 9
	},
	/* 1920_1080_24_P */
	{
		.xres = 1920,	.yres = 1080,
		.pixclock = 13468,
		.hbp = 170,	.hfp = 660,
		.vbp1 = 36,	.vfp1 = 9
	},
	/* 1920_1080_25_P */
	{
		.xres = 1920,	.yres = 1080,
		.pixclock = 13468,
		.hbp = 192,	.hfp = 528,
		.vbp1 = 36,	.vfp1 = 9
	},
	/* 720_480_60_I) */
	{
		.xres = 720,	.yres = 480,
		.pixclock = 74074,
		.hbp = 126,	.hfp = 12,
		.vbp1 = 44,	.vfp1 = 1,
		.interlaced = true,
	},
	/* 720_576_50_I) */
	{
		.xres = 720,	.yres = 576,
		.pixclock = 74074,
		.hbp = 132,	.hfp = 12,
		.vbp1 = 44,	.vfp1 = 5,
		.interlaced = true,
	},
	/* 1920_1080_50_I) */
	{
		.xres = 1920,	.yres = 1080,
		.pixclock = 13468,
		.hbp = 192,	.hfp = 528,
		.vbp1 = 20,	.vfp1 = 25,
		.interlaced = true,
	},
	/* 1920_1080_60_I) */
	{
		.xres = 1920,	.yres = 1080,
		.pixclock = 13468,
		.hbp = 192,	.hfp = 88,
		.vbp1 = 20,	.vfp1 = 25,
		.interlaced = true,
	},
};

/* Supported TVout modes */
static struct mcde_video_mode video_modes_supp_sdtv[] = {
	/* 720_480_60_I) */
	{
		.xres = 720,	.yres = 480,
		.pixclock = 74074,
		.hbp = 126,	.hfp = 12,
		.vbp1 = 44,	.vfp1 = 1,
		.interlaced = true,
	},
	/* 720_576_50_I) */
	{
		.xres = 720,	.yres = 576,
		.pixclock = 74074,
		.hbp = 132,	.hfp = 12,
		.vbp1 = 44,	.vfp1 = 5,
		.interlaced = true,
	},
};

#define AV8100_MAX_LEVEL 255

static int hdmi_try_video_mode(
	struct mcde_display_device *ddev, struct mcde_video_mode *video_mode)
{
	int index = 0;
	int match_level = AV8100_MAX_LEVEL;
	int found_index = -1;
	struct mcde_video_mode *video_modes_supp;
	int array_size;

	if (ddev == NULL || video_mode == NULL) {
		pr_warning("%s:ddev = NULL or video_mode = NULL\n", __func__);
		return -EINVAL;
	}

	dev_vdbg(&ddev->dev, "%s\n", __func__);

	if (ddev->port->hdmi_sdtv_switch == SDTV_SWITCH) {
		video_mode->interlaced = true;
		video_modes_supp = video_modes_supp_sdtv;
		array_size = ARRAY_SIZE(video_modes_supp_sdtv);
	} else {
		video_modes_supp = video_modes_supp_hdmi;
		array_size = ARRAY_SIZE(video_modes_supp_hdmi);
	}

	while (index < array_size) {
		/* 1. Check if all parameters match */
		if ((video_mode->xres == video_modes_supp[index].xres) &&
			(video_mode->yres == video_modes_supp[index].yres) &&
			((video_mode->xres + video_mode->hbp +
				video_mode->hfp) ==
			(video_modes_supp[index].xres +
				video_modes_supp[index].hbp +
				video_modes_supp[index].hfp)) &&
			((video_mode->yres + video_mode->vbp1 +
				video_mode->vbp2 + video_mode->vfp1 +
				video_mode->vfp2) ==
			(video_modes_supp[index].yres +
				video_modes_supp[index].vbp1 +
				video_modes_supp[index].vbp2 +
				video_modes_supp[index].vfp1 +
				video_modes_supp[index].vfp2)) &&
			(video_mode->pixclock ==
				video_modes_supp[index].pixclock) &&
			(video_mode->interlaced ==
				video_modes_supp[index].interlaced)) {
					match_level = 1;
					found_index = index;
					break;
		}

		/* 2. Check if xres,yres,htot,vtot,interlaced match */
		if ((match_level > 2) &&
			(video_mode->xres == video_modes_supp[index].xres) &&
			(video_mode->yres == video_modes_supp[index].yres) &&
			((video_mode->xres + video_mode->hbp +
				video_mode->hfp) ==
			(video_modes_supp[index].xres +
				video_modes_supp[index].hbp +
				video_modes_supp[index].hfp)) &&
			((video_mode->yres + video_mode->vbp1 +
				video_mode->vbp2 + video_mode->vfp1 +
				video_mode->vfp2) ==
			(video_modes_supp[index].yres +
				video_modes_supp[index].vbp1 +
				video_modes_supp[index].vbp2 +
				video_modes_supp[index].vfp1 +
				video_modes_supp[index].vfp2)) &&
			(video_mode->interlaced ==
				video_modes_supp[index].interlaced)) {
			match_level = 2;
			found_index = index;
		}

		/* 3. Check if xres,yres,pixelclock,interlaced match */
		if ((match_level > 3) &&
			(video_mode->xres == video_modes_supp[index].xres) &&
			(video_mode->yres == video_modes_supp[index].yres) &&
			(video_mode->interlaced ==
				video_modes_supp[index].interlaced) &&
			(video_mode->pixclock ==
				video_modes_supp[index].pixclock)) {
			match_level = 3;
			found_index = index;
		}

		/* 4. Check if xres,yres,interlaced match */
		if ((match_level > 4) &&
			(video_mode->xres == video_modes_supp[index].xres) &&
			(video_mode->yres == video_modes_supp[index].yres) &&
			(video_mode->interlaced ==
				video_modes_supp[index].interlaced)) {
			match_level = 4;
			found_index = index;
		}

		index++;
	}

	if (found_index == -1) {
		dev_dbg(&ddev->dev, "video_mode not accepted\n");
		dev_dbg(&ddev->dev, "xres:%d yres:%d pixclock:%d hbp:%d hfp:%d "
			"vfp1:%d vfp2:%d vbp1:%d vbp2:%d intlcd:%d\n",
			video_mode->xres, video_mode->yres,
			video_mode->pixclock, video_mode->hbp,
			video_mode->hfp, video_mode->vfp1, video_mode->vfp2,
			video_mode->vbp1, video_mode->vbp2,
			video_mode->interlaced);
		return -EINVAL;
	}

	memset(video_mode, 0, sizeof(struct mcde_video_mode));
	memcpy(video_mode, &video_modes_supp[found_index],
		sizeof(struct mcde_video_mode));

	dev_dbg(&ddev->dev, "%s:HDMI video_mode %d chosen. Level:%d\n",
		__func__, found_index, match_level);

	return 0;
}

static int hdmi_set_video_mode(
	struct mcde_display_device *dev, struct mcde_video_mode *video_mode)
{
	int ret;
	bool update = 0;
	union av8100_configuration av8100_config;
	struct mcde_display_hdmi_platform_data *pdata = dev->dev.platform_data;
	struct av8100_status status;

	/* TODO check video_mode_params */
	if (dev == NULL || video_mode == NULL) {
		pr_warning("%s:ddev = NULL or video_mode = NULL\n", __func__);
		return -EINVAL;
	}

	dev_vdbg(&dev->dev, "%s:\n", __func__);
	dev_vdbg(&dev->dev, "%s:xres:%d yres:%d hbp:%d hfp:%d vbp1:%d vfp1:%d "
		"vbp2:%d vfp2:%d interlaced:%d\n", __func__,
		video_mode->xres,
		video_mode->yres,
		video_mode->hbp,
		video_mode->hfp,
		video_mode->vbp1,
		video_mode->vfp1,
		video_mode->vbp2,
		video_mode->vfp2,
		video_mode->interlaced);

	memset(&(dev->video_mode), 0, sizeof(struct mcde_video_mode));
	memcpy(&(dev->video_mode), video_mode, sizeof(struct mcde_video_mode));

	if (dev->port->pixel_format == MCDE_PORTPIXFMT_DSI_YCBCR422)
		mcde_chnl_set_col_convert(dev->chnl_state,
						&pdata->rgb_2_yCbCr_transform);
	mcde_chnl_stop_flow(dev->chnl_state);

	ret = mcde_chnl_set_video_mode(dev->chnl_state, &dev->video_mode);
	if (ret < 0) {
		dev_warn(&dev->dev, "Failed to set video mode\n");
		return ret;
	}

	status = av8100_status_get();
	if (status.av8100_state == AV8100_OPMODE_UNDEFINED)
		return -EINVAL;

	if (av8100_ver_get() == AV8100_CHIPVER_1) {
		if (status.av8100_state >= AV8100_OPMODE_STANDBY) {
			/* Disable interrupts */
			ret = av8100_disable_interrupt();
			if (ret) {
				dev_err(&dev->dev,
					"%s:av8100_disable_interrupt failed\n",
					__func__);
				return ret;
			}

			ret = av8100_powerdown();
			if (ret) {
				dev_err(&dev->dev,
						"av8100_powerdown failed\n");
				return ret;
			}

			msleep(10);
		}
	}

	/* Set to powerup with interrupts disabled */
	status = av8100_status_get();
	if (status.av8100_state < AV8100_OPMODE_STANDBY) {
		ret = av8100_powerup();
		if (ret) {
			dev_err(&dev->dev, "av8100_powerup failed\n");
			return ret;
		}

		ret = av8100_download_firmware(NULL, 0, I2C_INTERFACE);
		if (ret) {
			dev_err(&dev->dev, "av8100_download_firmware failed\n");
			av8100_powerdown();
			return ret;
		}
	} else if (av8100_disable_interrupt()) {
		return -EFAULT;
	}

	/* Set HDMI mode to OFF */
	av8100_config.hdmi_format.hdmi_mode = AV8100_HDMI_OFF;
	av8100_config.hdmi_format.dvi_format = AV8100_DVI_CTRL_CTL0;
	av8100_config.hdmi_format.hdmi_format = AV8100_HDMI;
	if (av8100_conf_prep(AV8100_COMMAND_HDMI, &av8100_config))
		return -EFAULT;

	if (av8100_conf_w(AV8100_COMMAND_HDMI, NULL, NULL, I2C_INTERFACE))
		return -EFAULT;

	/* Get current av8100 video output format */
	ret = av8100_conf_get(AV8100_COMMAND_VIDEO_OUTPUT_FORMAT,
		&av8100_config);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_get "
			"AV8100_COMMAND_VIDEO_OUTPUT_FORMAT failed\n",
			__func__);
		return ret;
	}

	if (dev->port->hdmi_sdtv_switch == SDTV_SWITCH)
		av8100_config.video_output_format.video_output_cea_vesa =
			dev->video_mode.yres == NATIVE_YRES_SDTV ?
				AV8100_CEA21_22_576I_PAL_50HZ :
				AV8100_CEA6_7_NTSC_60HZ;
	else
		av8100_config.video_output_format.video_output_cea_vesa =
			av8100_video_output_format_get(
				dev->video_mode.xres,
				dev->video_mode.yres,
				dev->video_mode.xres +
				dev->video_mode.hbp + dev->video_mode.hfp,
				dev->video_mode.yres +
				dev->video_mode.vbp1 + dev->video_mode.vfp1 +
				dev->video_mode.vbp2 + dev->video_mode.vfp2,
				dev->video_mode.pixclock,
				dev->video_mode.interlaced);

	if (AV8100_VIDEO_OUTPUT_CEA_VESA_MAX ==
		av8100_config.video_output_format.video_output_cea_vesa) {
		dev_err(&dev->dev, "%s:video output format not found "
			"\n", __func__);
		return ret;
	}

	ret = av8100_conf_prep(AV8100_COMMAND_VIDEO_OUTPUT_FORMAT,
		&av8100_config);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_prep "
			"AV8100_COMMAND_VIDEO_OUTPUT_FORMAT failed\n",
			__func__);
		return ret;
	}

	/* Get current av8100 video input format */
	ret = av8100_conf_get(AV8100_COMMAND_VIDEO_INPUT_FORMAT,
		&av8100_config);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_get "
			"AV8100_COMMAND_VIDEO_INPUT_FORMAT failed\n",
			__func__);
		return ret;
	}

	/* Set correct av8100 video input pixel format */
	switch (dev->port->pixel_format) {
	case MCDE_PORTPIXFMT_DSI_16BPP:
	default:
		av8100_config.video_input_format.input_pixel_format =
			AV8100_INPUT_PIX_RGB565;
		break;
	case MCDE_PORTPIXFMT_DSI_18BPP:
		av8100_config.video_input_format.input_pixel_format =
			AV8100_INPUT_PIX_RGB666;
		break;
	case MCDE_PORTPIXFMT_DSI_18BPP_PACKED:
		av8100_config.video_input_format.input_pixel_format =
			AV8100_INPUT_PIX_RGB666P;
		break;
	case MCDE_PORTPIXFMT_DSI_24BPP:
		av8100_config.video_input_format.input_pixel_format =
			AV8100_INPUT_PIX_RGB888;
		break;
	case MCDE_PORTPIXFMT_DSI_YCBCR422:
		av8100_config.video_input_format.input_pixel_format =
			AV8100_INPUT_PIX_YCBCR422;
		break;
	}

	/*  Set ui_x4 */
	av8100_config.video_input_format.ui_x4 = dev->port->phy.dsi.ui;

	/* Set TE_config */
	switch (dev->port->sync_src) {
	case MCDE_SYNCSRC_TE0:
		av8100_config.video_input_format.TE_config = AV8100_TE_IT_LINE;
		break;
	case MCDE_SYNCSRC_TE1:
		av8100_config.video_input_format.TE_config = AV8100_TE_GPIO_IT;
		break;
	case MCDE_SYNCSRC_TE_POLLING:
		av8100_config.video_input_format.TE_config =
			AV8100_TE_DSI_LANE; /* Only on DSI, no interrupts */
		break;
	case MCDE_SYNCSRC_OFF:
	default:
		av8100_config.video_input_format.TE_config = AV8100_TE_OFF;
		break;
	}

	ret = av8100_conf_prep(AV8100_COMMAND_VIDEO_INPUT_FORMAT,
		&av8100_config);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_prep "
				"AV8100_COMMAND_VIDEO_INPUT_FORMAT failed\n",
				__func__);
		return ret;
	}

	ret = av8100_conf_w(AV8100_COMMAND_VIDEO_INPUT_FORMAT,
		NULL, NULL, I2C_INTERFACE);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_w "
				"AV8100_COMMAND_VIDEO_INPUT_FORMAT failed\n",
				__func__);
		return ret;
	}

	if (dev->port->hdmi_sdtv_switch == SDTV_SWITCH) {
		if (dev->port->pixel_format != MCDE_PORTPIXFMT_DSI_YCBCR422)
			av8100_config.color_transform =
					AV8100_COLOR_TRANSFORM_RGB_TO_DENC;
		else
			av8100_config.color_transform =
					AV8100_COLOR_TRANSFORM_YUV_TO_DENC;
	} else if (dev->port->pixel_format == MCDE_PORTPIXFMT_DSI_YCBCR422) {
		av8100_config.color_transform =
					AV8100_COLOR_TRANSFORM_YUV_TO_RGB;
	} else {
		av8100_config.color_transform =
					AV8100_COLOR_TRANSFORM_INDENTITY;
	}

	ret = av8100_conf_prep(
		AV8100_COMMAND_COLORSPACECONVERSION,
		&av8100_config);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_configuration_prepare "
			"AV8100_COMMAND_COLORSPACECONVERSION failed\n",
			__func__);
		return ret;
	}

	ret = av8100_conf_w(
			AV8100_COMMAND_COLORSPACECONVERSION,
			NULL, NULL, I2C_INTERFACE);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_w "
			"AV8100_COMMAND_COLORSPACECONVERSION failed\n",
			__func__);
		return ret;
	}

	/* Set video output format */
	ret = av8100_conf_w(AV8100_COMMAND_VIDEO_OUTPUT_FORMAT,
		NULL, NULL, I2C_INTERFACE);
	if (ret) {
		dev_err(&dev->dev, "av8100_conf_w failed\n");
		return ret;
	}

	/* Set audio input format */
	ret = av8100_conf_w(AV8100_COMMAND_AUDIO_INPUT_FORMAT,
		NULL, NULL, I2C_INTERFACE);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_w "
				"AV8100_COMMAND_AUDIO_INPUT_FORMAT failed\n",
			__func__);
		return ret;
	}

	/* Get current av8100 video denc settings format */
	ret = av8100_conf_get(AV8100_COMMAND_DENC,
		&av8100_config);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_get "
				"AV8100_COMMAND_DENC failed\n", __func__);
		return ret;
	}

	if (dev->port->hdmi_sdtv_switch == SDTV_SWITCH) {
		update = true;
		if (dev->video_mode.yres == NATIVE_YRES_SDTV) {
			av8100_config.denc_format.standard_selection =
					AV8100_PAL_BDGHI;
			av8100_config.denc_format.cvbs_video_format  =
					AV8100_CVBS_625;
		} else {
			av8100_config.denc_format.standard_selection =
					AV8100_NTSC_M;
			av8100_config.denc_format.cvbs_video_format  =
					AV8100_CVBS_525;
		}
	} else
		update = (av8100_config.denc_format.enable != 0);

	if (update) {
		if (dev->port->hdmi_sdtv_switch == SDTV_SWITCH)
			av8100_config.denc_format.enable = 1;
		else
			av8100_config.denc_format.enable = 0;

		ret = av8100_conf_prep(AV8100_COMMAND_DENC,
			&av8100_config);
		if (ret) {
			dev_err(&dev->dev, "%s:av8100_conf_prep "
				"AV8100_COMMAND_DENC failed\n", __func__);
			return ret;
		}

		/* TODO: prepare depending on OUT fmt */
		ret = av8100_conf_w(AV8100_COMMAND_DENC,
			NULL, NULL, I2C_INTERFACE);
		if (ret) {
			dev_err(&dev->dev, "%s:av8100_conf_w "
				"AV8100_COMMAND_DENC failed\n", __func__);
			return ret;
		}
	}

	dev->update_flags |= UPDATE_FLAG_VIDEO_MODE;
	dev->first_update = true;

	return 0;
}

static u16 rotate_byte_left(u8 c, int nr)
{
	return (0xff & (c << nr)) | (0xff & (c >> (8 - nr)));
}

static u16 map_yv(u8 in)
{
	return rotate_byte_left(in, 3) << 4;
}

static u16 map_u(u8 in)
{
	return rotate_byte_left(in, 5) << 4;
}

static int hdmi_set_pixel_format(
	struct mcde_display_device *ddev, enum mcde_ovly_pix_fmt format)
{
	int ret;

	ddev->pixel_format = format;
	mcde_chnl_stop_flow(ddev->chnl_state);
	ret = mcde_chnl_set_pixel_format(ddev->chnl_state,
						ddev->port->pixel_format);

	if (ret < 0) {
		dev_warn(&ddev->dev, "%s:Failed to set pixel format = %d\n",
							__func__, format);
		return ret;
	}

	if (ddev->port->pixel_format == MCDE_PORTPIXFMT_DSI_YCBCR422 &&
						av8100_ver_get() == 2) {
		/* The V2 version has an error for unpacking YUV422 */
		struct mcde_palette_table palette = {
			.map_col_ch0 = *map_yv,
			.map_col_ch1 = *map_u,
			.map_col_ch2 = *map_yv,
		};
		ret = mcde_chnl_set_palette(ddev->chnl_state, &palette);
	}

	ddev->update_flags |= UPDATE_FLAG_PIXEL_FORMAT;

	return 0;
}

static int hdmi_apply_config(struct mcde_display_device *ddev)
{
	int ret;

	if (!ddev->update_flags)
		return 0;

	ret = mcde_chnl_apply(ddev->chnl_state);
	if (ret < 0) {
		dev_warn(&ddev->dev, "%s:Failed to apply to channel\n",
							__func__);
		return ret;
	}
	ddev->update_flags = 0;

	return 0;
}

static int hdmi_on_first_update(struct mcde_display_device *dev)
{
	int ret;
	union av8100_configuration av8100_config;

	dev->first_update = false;

	/* Get current av8100 video denc settings format */
	ret = av8100_conf_get(AV8100_COMMAND_DENC,
		&av8100_config);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_get "
				"AV8100_COMMAND_DENC failed\n", __func__);
		return ret;
	}

	/*
	 * Avoid simultaneous output of DENC and HDMI.
	 * Only one of them should be enabled.
	 */
	if (av8100_config.denc_format.enable)
		av8100_config.hdmi_format.hdmi_mode = AV8100_HDMI_OFF;
	else
		av8100_config.hdmi_format.hdmi_mode = AV8100_HDMI_ON;

	av8100_config.hdmi_format.hdmi_format = AV8100_HDMI;
	av8100_config.hdmi_format.dvi_format = AV8100_DVI_CTRL_CTL0;

	ret = av8100_conf_prep(AV8100_COMMAND_HDMI, &av8100_config);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_prep "
			"AV8100_COMMAND_HDMI failed\n", __func__);
		return ret;
	}

	/* Enable interrupts */
	ret = av8100_enable_interrupt();
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_enable_interrupt failed\n",
			__func__);
		return ret;
	}

	ret = av8100_conf_w(AV8100_COMMAND_HDMI, NULL,
		NULL, I2C_INTERFACE);
	if (ret) {
		dev_err(&dev->dev, "%s:av8100_conf_w "
			"AV8100_COMMAND_HDMI failed\n", __func__);
	}

	return ret;
}

static int hdmi_set_power_mode(struct mcde_display_device *ddev,
	enum mcde_display_power_mode power_mode)
{
	struct display_driver_data *driver_data = dev_get_drvdata(&ddev->dev);
	int ret = 0;

	/* OFF -> STANDBY */
	if (ddev->power_mode == MCDE_DISPLAY_PM_OFF &&
		power_mode != MCDE_DISPLAY_PM_OFF) {
		if (ddev->platform_enable) {
			ret = ddev->platform_enable(ddev);
			if (ret)
				return ret;
		}
		/*
		 * the regulator for analog TV out is only enabled here,
		 * this means that one needs to switch to the OFF state
		 * to be able to switch from HDMI to CVBS.
		 */
		if (ddev->port->hdmi_sdtv_switch == SDTV_SWITCH) {
			ret = regulator_enable(driver_data->cvbs_regulator);
			if (ret)
				return ret;
			driver_data->cvbs_regulator_enabled = true;
		}
		ddev->power_mode = MCDE_DISPLAY_PM_STANDBY;
	}
	/* STANDBY -> ON */
	if (ddev->power_mode == MCDE_DISPLAY_PM_STANDBY &&
		power_mode == MCDE_DISPLAY_PM_ON) {
		ddev->power_mode = MCDE_DISPLAY_PM_ON;
		goto set_power_and_exit;
	}
	/* ON -> STANDBY */
	else if (ddev->power_mode == MCDE_DISPLAY_PM_ON &&
		power_mode <= MCDE_DISPLAY_PM_STANDBY) {
		ddev->power_mode = MCDE_DISPLAY_PM_STANDBY;
	}

	/* STANDBY -> OFF */
	if (ddev->power_mode == MCDE_DISPLAY_PM_STANDBY &&
		power_mode == MCDE_DISPLAY_PM_OFF) {
		memset(&(ddev->video_mode), 0, sizeof(struct mcde_video_mode));
		ret = av8100_powerdown();
		if (ret)
			dev_err(&ddev->dev, "%s:av8100_powerdown failed\n"
								, __func__);
		if (ddev->platform_disable) {
			ret = ddev->platform_disable(ddev);
			if (ret)
				return ret;
		}
		if (driver_data->cvbs_regulator_enabled) {
			ret = regulator_disable(driver_data->cvbs_regulator);
			if (ret)
				return ret;
			driver_data->cvbs_regulator_enabled = false;
		}
		ddev->power_mode = MCDE_DISPLAY_PM_OFF;
	}

set_power_and_exit:
	mcde_chnl_set_power_mode(ddev->chnl_state, ddev->power_mode);

	return ret;
}

static int __devinit hdmi_probe(struct mcde_display_device *dev)
{
	int ret = 0;
	struct display_driver_data *driver_data;
	struct mcde_display_hdmi_platform_data *pdata =
		dev->dev.platform_data;

	if (pdata == NULL) {
		dev_err(&dev->dev, "%s:Platform data missing\n", __func__);
		return -EINVAL;
	}

	if (dev->port->type != MCDE_PORTTYPE_DSI) {
		dev_err(&dev->dev, "%s:Invalid port type %d\n",
			__func__, dev->port->type);
		return -EINVAL;
	}

	driver_data = (struct display_driver_data *)
		kzalloc(sizeof(struct display_driver_data), GFP_KERNEL);
	if (!driver_data) {
		dev_err(&dev->dev, "Failed to allocate driver data\n");
		return -ENOMEM;
	}

	/* DSI use clock continous mode if AV8100_CHIPVER_1 > 1 */
	if (av8100_ver_get() > AV8100_CHIPVER_1)
		dev->port->phy.dsi.clk_cont = true;

	dev->prepare_for_update = NULL;
	dev->on_first_update = hdmi_on_first_update;
	dev->try_video_mode = hdmi_try_video_mode;
	dev->set_video_mode = hdmi_set_video_mode;
	dev->apply_config = hdmi_apply_config;
	dev->set_pixel_format = hdmi_set_pixel_format;
	dev->set_power_mode = hdmi_set_power_mode;

	/* Create sysfs file for switching between hdmi and sdtv */
	if (device_create_file(&dev->dev, &dev_attr_hdmisdtvswitch))
		dev_info(&dev->dev,
			"Unable to create hdmisdtvswitch attr\n");

	if (pdata->cvbs_regulator_id) {
		driver_data->cvbs_regulator = regulator_get(&dev->dev,
						pdata->cvbs_regulator_id);
		if (IS_ERR(driver_data->cvbs_regulator)) {
			ret = PTR_ERR(driver_data->cvbs_regulator);
			dev_warn(&dev->dev, "%s:Failed to get regulator %s\n",
				__func__, pdata->cvbs_regulator_id);
			driver_data->cvbs_regulator = NULL;
			goto av_regulator_get_failed;
		}
	}

	dev_set_drvdata(&dev->dev, driver_data);
	dev_info(&dev->dev, "HDMI display probed\n");

	return 0;

av_regulator_get_failed:
	kfree(driver_data);
	return ret;
}

static int __devexit hdmi_remove(struct mcde_display_device *dev)
{
	struct display_driver_data *driver_data = dev_get_drvdata(&dev->dev);
	struct mcde_display_hdmi_platform_data *pdata =
		dev->dev.platform_data;

	/* Remove sysfs file */
	device_remove_file(&dev->dev, &dev_attr_hdmisdtvswitch);

	dev->set_power_mode(dev, MCDE_DISPLAY_PM_OFF);

	if (driver_data->cvbs_regulator)
		regulator_put(driver_data->cvbs_regulator);
	kfree(driver_data);
	if (pdata->hdmi_platform_enable) {
		if (pdata->regulator)
			regulator_put(pdata->regulator);
		if (pdata->reset_gpio) {
			gpio_direction_input(pdata->reset_gpio);
			gpio_free(pdata->reset_gpio);
		}
	}

	return 0;
}

#if !defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)
static int hdmi_resume(struct mcde_display_device *ddev)
{
	int ret;

	/* set_power_mode will handle call platform_enable */
	ret = ddev->set_power_mode(ddev, MCDE_DISPLAY_PM_STANDBY);
	if (ret < 0)
		dev_warn(&ddev->dev, "%s:Failed to resume display\n"
			, __func__);

	ddev->update(ddev);
	return ret;
}

static int hdmi_suspend(struct mcde_display_device *ddev, pm_message_t state)
{
	int ret;

	/* set_power_mode will handle call platform_disable */
	ret = ddev->set_power_mode(ddev, MCDE_DISPLAY_PM_OFF);
	if (ret < 0)
		dev_warn(&ddev->dev, "%s:Failed to suspend display\n"
			, __func__);

	return ret;
}
#endif

static struct mcde_display_driver hdmi_driver = {
	.probe	= hdmi_probe,
	.remove = hdmi_remove,
#if !defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)
	.suspend = hdmi_suspend,
	.resume = hdmi_resume,
#else
	.suspend = NULL,
	.resume = NULL,
#endif
	.driver = {
		.name	= "av8100_hdmi",
	},
};

/* Module init */
static int __init mcde_display_hdmi_init(void)
{
	pr_info("%s\n", __func__);

	return mcde_display_driver_register(&hdmi_driver);

}
late_initcall(mcde_display_hdmi_init);

static void __exit mcde_display_hdmi_exit(void)
{
	pr_info("%s\n", __func__);

	mcde_display_driver_unregister(&hdmi_driver);
}
module_exit(mcde_display_hdmi_exit);

MODULE_AUTHOR("Per Persson <per.xb.persson@stericsson.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ST-Ericsson hdmi display driver");