Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
/* Copyright (c) 2008-2009, Code Aurora Forum. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that 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 Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "msm_fb.h"
#include "mddihost.h"
#include "mddihosti.h"

#define SHARP_QVGA_PRIM 1
#define SHARP_128X128_SECD 2

extern uint32 mddi_host_core_version;
static boolean mddi_debug_prim_wait = FALSE;
static boolean mddi_sharp_vsync_wake = TRUE;
static boolean mddi_sharp_monitor_refresh_value = TRUE;
static boolean mddi_sharp_report_refresh_measurements = FALSE;
static uint32 mddi_sharp_rows_per_second = 13830;	/* 5200000/376 */
static uint32 mddi_sharp_rows_per_refresh = 338;
static uint32 mddi_sharp_usecs_per_refresh = 24440;	/* (376+338)/5200000 */
static boolean mddi_sharp_debug_60hz_refresh = FALSE;

extern mddi_gpio_info_type mddi_gpio;
extern boolean mddi_vsync_detect_enabled;
static msm_fb_vsync_handler_type mddi_sharp_vsync_handler;
static void *mddi_sharp_vsync_handler_arg;
static uint16 mddi_sharp_vsync_attempts;

static void mddi_sharp_prim_lcd_init(void);
static void mddi_sharp_sub_lcd_init(void);
static void mddi_sharp_lcd_set_backlight(struct msm_fb_data_type *mfd);
static void mddi_sharp_vsync_set_handler(msm_fb_vsync_handler_type handler,
					 void *);
static void mddi_sharp_lcd_vsync_detected(boolean detected);
static struct msm_panel_common_pdata *mddi_sharp_pdata;

#define REG_SYSCTL    0x0000
#define REG_INTR    0x0006
#define REG_CLKCNF    0x000C
#define REG_CLKDIV1    0x000E
#define REG_CLKDIV2    0x0010

#define REG_GIOD    0x0040
#define REG_GIOA    0x0042

#define REG_AGM      0x010A
#define REG_FLFT    0x0110
#define REG_FRGT    0x0112
#define REG_FTOP    0x0114
#define REG_FBTM    0x0116
#define REG_FSTRX    0x0118
#define REG_FSTRY    0x011A
#define REG_VRAM    0x0202
#define REG_SSDCTL    0x0330
#define REG_SSD0    0x0332
#define REG_PSTCTL1    0x0400
#define REG_PSTCTL2    0x0402
#define REG_PTGCTL    0x042A
#define REG_PTHP    0x042C
#define REG_PTHB    0x042E
#define REG_PTHW    0x0430
#define REG_PTHF    0x0432
#define REG_PTVP    0x0434
#define REG_PTVB    0x0436
#define REG_PTVW    0x0438
#define REG_PTVF    0x043A
#define REG_VBLKS    0x0458
#define REG_VBLKE    0x045A
#define REG_SUBCTL    0x0700
#define REG_SUBTCMD    0x0702
#define REG_SUBTCMDD  0x0704
#define REG_REVBYTE    0x0A02
#define REG_REVCNT    0x0A04
#define REG_REVATTR    0x0A06
#define REG_REVFMT    0x0A08

#define SHARP_SUB_UNKNOWN 0xffffffff
#define SHARP_SUB_HYNIX 1
#define SHARP_SUB_ROHM  2

static uint32 sharp_subpanel_type = SHARP_SUB_UNKNOWN;

static void sub_through_write(int sub_rs, uint32 sub_data)
{
	mddi_queue_register_write(REG_SUBTCMDD, sub_data, FALSE, 0);

	/* CS=1,RD=1,WE=1,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x000e | sub_rs, FALSE, 0);

	/* CS=0,RD=1,WE=1,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x0006 | sub_rs, FALSE, 0);

	/* CS=0,RD=1,WE=0,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x0004 | sub_rs, FALSE, 0);

	/* CS=0,RD=1,WE=1,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x0006 | sub_rs, FALSE, 0);

	/* CS=1,RD=1,WE=1,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x000e | sub_rs, TRUE, 0);
}

static uint32 sub_through_read(int sub_rs)
{
	uint32 sub_data;

	/* CS=1,RD=1,WE=1,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x000e | sub_rs, FALSE, 0);

	/* CS=0,RD=1,WE=1,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x0006 | sub_rs, FALSE, 0);

	/* CS=0,RD=1,WE=0,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x0002 | sub_rs, TRUE, 0);

	mddi_queue_register_read(REG_SUBTCMDD, &sub_data, TRUE, 0);

	/* CS=0,RD=1,WE=1,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x0006 | sub_rs, FALSE, 0);

	/* CS=1,RD=1,WE=1,RS=sub_rs */
	mddi_queue_register_write(REG_SUBTCMD, 0x000e | sub_rs, TRUE, 0);

	return sub_data;
}

static void serigo(uint32 ssd)
{
	uint32 ssdctl;

	mddi_queue_register_read(REG_SSDCTL, &ssdctl, TRUE, 0);
	ssdctl = ((ssdctl & 0xE7) | 0x02);

	mddi_queue_register_write(REG_SSD0, ssd, FALSE, 0);
	mddi_queue_register_write(REG_SSDCTL, ssdctl, TRUE, 0);

	do {
		mddi_queue_register_read(REG_SSDCTL, &ssdctl, TRUE, 0);
	} while ((ssdctl & 0x0002) != 0);

	if (mddi_debug_prim_wait)
		mddi_wait(2);
}

static void mddi_sharp_lcd_powerdown(void)
{
	serigo(0x0131);
	serigo(0x0300);
	mddi_wait(40);
	serigo(0x0135);
	mddi_wait(20);
	serigo(0x2122);
	mddi_wait(20);
	serigo(0x0201);
	mddi_wait(20);
	serigo(0x2100);
	mddi_wait(20);
	serigo(0x2000);
	mddi_wait(20);

	mddi_queue_register_write(REG_PSTCTL1, 0x1, TRUE, 0);
	mddi_wait(100);
	mddi_queue_register_write(REG_PSTCTL1, 0x0, TRUE, 0);
	mddi_wait(2);
	mddi_queue_register_write(REG_SYSCTL, 0x1, TRUE, 0);
	mddi_wait(2);
	mddi_queue_register_write(REG_CLKDIV1, 0x3, TRUE, 0);
	mddi_wait(2);
	mddi_queue_register_write(REG_SSDCTL, 0x0000, TRUE, 0);	/* SSDRESET */
	mddi_queue_register_write(REG_SYSCTL, 0x0, TRUE, 0);
	mddi_wait(2);
}

static void mddi_sharp_lcd_set_backlight(struct msm_fb_data_type *mfd)
{
	uint32 regdata;
	int32 level;
	int max = mfd->panel_info.bl_max;
	int min = mfd->panel_info.bl_min;

	if (mddi_sharp_pdata && mddi_sharp_pdata->backlight_level) {
		level = mddi_sharp_pdata->backlight_level(mfd->bl_level,
							  max,
							  min);

		if (level < 0)
			return;

		/* use Rodem GPIO(2:0) to give 8 levels of backlight (7-0) */
		/* Set lower 3 GPIOs as Outputs (set to 0) */
		mddi_queue_register_read(REG_GIOA, &regdata, TRUE, 0);
		mddi_queue_register_write(REG_GIOA, regdata & 0xfff8, TRUE, 0);

		/* Set lower 3 GPIOs as level */
		mddi_queue_register_read(REG_GIOD, &regdata, TRUE, 0);
		mddi_queue_register_write(REG_GIOD,
			  (regdata & 0xfff8) | (0x07 & level), TRUE, 0);
	}
}

static void mddi_sharp_prim_lcd_init(void)
{
	mddi_queue_register_write(REG_SYSCTL, 0x4000, TRUE, 0);
	mddi_wait(1);
	mddi_queue_register_write(REG_SYSCTL, 0x0000, TRUE, 0);
	mddi_wait(5);
	mddi_queue_register_write(REG_SYSCTL, 0x0001, FALSE, 0);
	mddi_queue_register_write(REG_CLKDIV1, 0x000b, FALSE, 0);

	/* new reg write below */
	if (mddi_sharp_debug_60hz_refresh)
		mddi_queue_register_write(REG_CLKCNF, 0x070d, FALSE, 0);
	else
		mddi_queue_register_write(REG_CLKCNF, 0x0708, FALSE, 0);

	mddi_queue_register_write(REG_SYSCTL, 0x0201, FALSE, 0);
	mddi_queue_register_write(REG_PTGCTL, 0x0010, FALSE, 0);
	mddi_queue_register_write(REG_PTHP, 4, FALSE, 0);
	mddi_queue_register_write(REG_PTHB, 40, FALSE, 0);
	mddi_queue_register_write(REG_PTHW, 240, FALSE, 0);
	if (mddi_sharp_debug_60hz_refresh)
		mddi_queue_register_write(REG_PTHF, 12, FALSE, 0);
	else
		mddi_queue_register_write(REG_PTHF, 92, FALSE, 0);

	mddi_wait(1);

	mddi_queue_register_write(REG_PTVP, 1, FALSE, 0);
	mddi_queue_register_write(REG_PTVB, 2, FALSE, 0);
	mddi_queue_register_write(REG_PTVW, 320, FALSE, 0);
	mddi_queue_register_write(REG_PTVF, 15, FALSE, 0);

	mddi_wait(1);

	/* vram_color set REG_AGM???? */
	mddi_queue_register_write(REG_AGM, 0x0000, TRUE, 0);

	mddi_queue_register_write(REG_SSDCTL, 0x0000, FALSE, 0);
	mddi_queue_register_write(REG_SSDCTL, 0x0001, TRUE, 0);
	mddi_wait(1);
	mddi_queue_register_write(REG_PSTCTL1, 0x0001, TRUE, 0);
	mddi_wait(10);

	serigo(0x0701);
	/* software reset */
	mddi_wait(1);
	/* Wait over 50us */

	serigo(0x0400);
	/* DCLK~ACHSYNC~ACVSYNC polarity setting */
	serigo(0x2900);
	/* EEPROM start read address setting */
	serigo(0x2606);
	/* EEPROM start read register setting */
	mddi_wait(20);
	/* Wait over 20ms */

	serigo(0x0503);
	/* Horizontal timing setting */
	serigo(0x062C);
	/* Veritical timing setting */
	serigo(0x2001);
	/* power initialize setting(VDC2) */
	mddi_wait(20);
	/* Wait over 20ms */

	serigo(0x2120);
	/* Initialize power setting(CPS) */
	mddi_wait(20);
	/* Wait over 20ms */

	serigo(0x2130);
	/* Initialize power setting(CPS) */
	mddi_wait(20);
	/* Wait over 20ms */

	serigo(0x2132);
	/* Initialize power setting(CPS) */
	mddi_wait(10);
	/* Wait over 10ms */

	serigo(0x2133);
	/* Initialize power setting(CPS) */
	mddi_wait(20);
	/* Wait over 20ms */

	serigo(0x0200);
	/* Panel initialize release(INIT) */
	mddi_wait(1);
	/* Wait over 1ms */

	serigo(0x0131);
	/* Panel setting(CPS) */
	mddi_wait(1);
	/* Wait over 1ms */

	mddi_queue_register_write(REG_PSTCTL1, 0x0003, TRUE, 0);

	/* if (FFA LCD is upside down) -> serigo(0x0100); */
	serigo(0x0130);

	/* Black mask release(display ON) */
	mddi_wait(1);
	/* Wait over 1ms */

	if (mddi_sharp_vsync_wake) {
		mddi_queue_register_write(REG_VBLKS, 0x1001, TRUE, 0);
		mddi_queue_register_write(REG_VBLKE, 0x1002, TRUE, 0);
	}

	/* Set the MDP pixel data attributes for Primary Display */
	mddi_host_write_pix_attr_reg(0x00C3);
	return;

}

void mddi_sharp_sub_lcd_init(void)
{

	mddi_queue_register_write(REG_SYSCTL, 0x4000, FALSE, 0);
	mddi_queue_register_write(REG_SYSCTL, 0x0000, TRUE, 0);
	mddi_wait(100);

	mddi_queue_register_write(REG_SYSCTL, 0x0001, FALSE, 0);
	mddi_queue_register_write(REG_CLKDIV1, 0x000b, FALSE, 0);
	mddi_queue_register_write(REG_CLKCNF, 0x0708, FALSE, 0);
	mddi_queue_register_write(REG_SYSCTL, 0x0201, FALSE, 0);
	mddi_queue_register_write(REG_PTGCTL, 0x0010, FALSE, 0);
	mddi_queue_register_write(REG_PTHP, 4, FALSE, 0);
	mddi_queue_register_write(REG_PTHB, 40, FALSE, 0);
	mddi_queue_register_write(REG_PTHW, 128, FALSE, 0);
	mddi_queue_register_write(REG_PTHF, 92, FALSE, 0);
	mddi_queue_register_write(REG_PTVP, 1, FALSE, 0);
	mddi_queue_register_write(REG_PTVB, 2, FALSE, 0);
	mddi_queue_register_write(REG_PTVW, 128, FALSE, 0);
	mddi_queue_register_write(REG_PTVF, 15, FALSE, 0);

	/* Now the sub display..... */
	/* Reset High */
	mddi_queue_register_write(REG_SUBCTL, 0x0200, FALSE, 0);
	/* CS=1,RD=1,WE=1,RS=1 */
	mddi_queue_register_write(REG_SUBTCMD, 0x000f, TRUE, 0);
	mddi_wait(1);
	/* Wait 5us */

	if (sharp_subpanel_type == SHARP_SUB_UNKNOWN) {
		uint32 data;

		sub_through_write(1, 0x05);
		sub_through_write(1, 0x6A);
		sub_through_write(1, 0x1D);
		sub_through_write(1, 0x05);
		data = sub_through_read(1);
		if (data == 0x6A) {
			sharp_subpanel_type = SHARP_SUB_HYNIX;
		} else {
			sub_through_write(0, 0x36);
			sub_through_write(1, 0xA8);
			sub_through_write(0, 0x09);
			data = sub_through_read(1);
			data = sub_through_read(1);
			if (data == 0x54) {
				sub_through_write(0, 0x36);
				sub_through_write(1, 0x00);
				sharp_subpanel_type = SHARP_SUB_ROHM;
			}
		}
	}

	if (sharp_subpanel_type == SHARP_SUB_HYNIX) {
		sub_through_write(1, 0x00);	/* Display setting 1 */
		sub_through_write(1, 0x04);
		sub_through_write(1, 0x01);
		sub_through_write(1, 0x05);
		sub_through_write(1, 0x0280);
		sub_through_write(1, 0x0301);
		sub_through_write(1, 0x0402);
		sub_through_write(1, 0x0500);
		sub_through_write(1, 0x0681);
		sub_through_write(1, 0x077F);
		sub_through_write(1, 0x08C0);
		sub_through_write(1, 0x0905);
		sub_through_write(1, 0x0A02);
		sub_through_write(1, 0x0B00);
		sub_through_write(1, 0x0C00);
		sub_through_write(1, 0x0D00);
		sub_through_write(1, 0x0E00);
		sub_through_write(1, 0x0F00);

		sub_through_write(1, 0x100B);	/* Display setting 2 */
		sub_through_write(1, 0x1103);
		sub_through_write(1, 0x1237);
		sub_through_write(1, 0x1300);
		sub_through_write(1, 0x1400);
		sub_through_write(1, 0x1500);
		sub_through_write(1, 0x1605);
		sub_through_write(1, 0x1700);
		sub_through_write(1, 0x1800);
		sub_through_write(1, 0x192E);
		sub_through_write(1, 0x1A00);
		sub_through_write(1, 0x1B00);
		sub_through_write(1, 0x1C00);

		sub_through_write(1, 0x151A);	/* Power setting */

		sub_through_write(1, 0x2002);	/* Gradation Palette setting */
		sub_through_write(1, 0x2107);
		sub_through_write(1, 0x220C);
		sub_through_write(1, 0x2310);
		sub_through_write(1, 0x2414);
		sub_through_write(1, 0x2518);
		sub_through_write(1, 0x261C);
		sub_through_write(1, 0x2720);
		sub_through_write(1, 0x2824);
		sub_through_write(1, 0x2928);
		sub_through_write(1, 0x2A2B);
		sub_through_write(1, 0x2B2E);
		sub_through_write(1, 0x2C31);
		sub_through_write(1, 0x2D34);
		sub_through_write(1, 0x2E37);
		sub_through_write(1, 0x2F3A);
		sub_through_write(1, 0x303C);
		sub_through_write(1, 0x313E);
		sub_through_write(1, 0x323F);
		sub_through_write(1, 0x3340);
		sub_through_write(1, 0x3441);
		sub_through_write(1, 0x3543);
		sub_through_write(1, 0x3646);
		sub_through_write(1, 0x3749);
		sub_through_write(1, 0x384C);
		sub_through_write(1, 0x394F);
		sub_through_write(1, 0x3A52);
		sub_through_write(1, 0x3B59);
		sub_through_write(1, 0x3C60);
		sub_through_write(1, 0x3D67);
		sub_through_write(1, 0x3E6E);
		sub_through_write(1, 0x3F7F);
		sub_through_write(1, 0x4001);
		sub_through_write(1, 0x4107);
		sub_through_write(1, 0x420C);
		sub_through_write(1, 0x4310);
		sub_through_write(1, 0x4414);
		sub_through_write(1, 0x4518);
		sub_through_write(1, 0x461C);
		sub_through_write(1, 0x4720);
		sub_through_write(1, 0x4824);
		sub_through_write(1, 0x4928);
		sub_through_write(1, 0x4A2B);
		sub_through_write(1, 0x4B2E);
		sub_through_write(1, 0x4C31);
		sub_through_write(1, 0x4D34);
		sub_through_write(1, 0x4E37);
		sub_through_write(1, 0x4F3A);
		sub_through_write(1, 0x503C);
		sub_through_write(1, 0x513E);
		sub_through_write(1, 0x523F);
		sub_through_write(1, 0x5340);
		sub_through_write(1, 0x5441);
		sub_through_write(1, 0x5543);
		sub_through_write(1, 0x5646);
		sub_through_write(1, 0x5749);
		sub_through_write(1, 0x584C);
		sub_through_write(1, 0x594F);
		sub_through_write(1, 0x5A52);
		sub_through_write(1, 0x5B59);
		sub_through_write(1, 0x5C60);
		sub_through_write(1, 0x5D67);
		sub_through_write(1, 0x5E6E);
		sub_through_write(1, 0x5F7E);
		sub_through_write(1, 0x6000);
		sub_through_write(1, 0x6107);
		sub_through_write(1, 0x620C);
		sub_through_write(1, 0x6310);
		sub_through_write(1, 0x6414);
		sub_through_write(1, 0x6518);
		sub_through_write(1, 0x661C);
		sub_through_write(1, 0x6720);
		sub_through_write(1, 0x6824);
		sub_through_write(1, 0x6928);
		sub_through_write(1, 0x6A2B);
		sub_through_write(1, 0x6B2E);
		sub_through_write(1, 0x6C31);
		sub_through_write(1, 0x6D34);
		sub_through_write(1, 0x6E37);
		sub_through_write(1, 0x6F3A);
		sub_through_write(1, 0x703C);
		sub_through_write(1, 0x713E);
		sub_through_write(1, 0x723F);
		sub_through_write(1, 0x7340);
		sub_through_write(1, 0x7441);
		sub_through_write(1, 0x7543);
		sub_through_write(1, 0x7646);
		sub_through_write(1, 0x7749);
		sub_through_write(1, 0x784C);
		sub_through_write(1, 0x794F);
		sub_through_write(1, 0x7A52);
		sub_through_write(1, 0x7B59);
		sub_through_write(1, 0x7C60);
		sub_through_write(1, 0x7D67);
		sub_through_write(1, 0x7E6E);
		sub_through_write(1, 0x7F7D);

		sub_through_write(1, 0x1851);	/* Display on */

		mddi_queue_register_write(REG_AGM, 0x0000, TRUE, 0);

		/* 1 pixel / 1 post clock */
		mddi_queue_register_write(REG_CLKDIV2, 0x3b00, FALSE, 0);

		/* SUB LCD select */
		mddi_queue_register_write(REG_PSTCTL2, 0x0080, FALSE, 0);

		/* RS=0,command initiate number=0,select master mode */
		mddi_queue_register_write(REG_SUBCTL, 0x0202, FALSE, 0);

		/* Sub LCD Data transform start */
		mddi_queue_register_write(REG_PSTCTL1, 0x0003, FALSE, 0);

	} else if (sharp_subpanel_type == SHARP_SUB_ROHM) {

		sub_through_write(0, 0x01);	/* Display setting */
		sub_through_write(1, 0x00);

		mddi_wait(1);
		/* Wait 100us  <----- ******* Update 2005/01/24 */

		sub_through_write(0, 0xB6);
		sub_through_write(1, 0x0C);
		sub_through_write(1, 0x4A);
		sub_through_write(1, 0x20);
		sub_through_write(0, 0x3A);
		sub_through_write(1, 0x05);
		sub_through_write(0, 0xB7);
		sub_through_write(1, 0x01);
		sub_through_write(0, 0xBA);
		sub_through_write(1, 0x20);
		sub_through_write(1, 0x02);
		sub_through_write(0, 0x25);
		sub_through_write(1, 0x4F);
		sub_through_write(0, 0xBB);
		sub_through_write(1, 0x00);
		sub_through_write(0, 0x36);
		sub_through_write(1, 0x00);
		sub_through_write(0, 0xB1);
		sub_through_write(1, 0x05);
		sub_through_write(0, 0xBE);
		sub_through_write(1, 0x80);
		sub_through_write(0, 0x26);
		sub_through_write(1, 0x01);
		sub_through_write(0, 0x2A);
		sub_through_write(1, 0x02);
		sub_through_write(1, 0x81);
		sub_through_write(0, 0x2B);
		sub_through_write(1, 0x00);
		sub_through_write(1, 0x7F);

		sub_through_write(0, 0x2C);
		sub_through_write(0, 0x11);	/* Sleep mode off */

		mddi_wait(1);
		/* Wait 100 ms <----- ******* Update 2005/01/24 */

		sub_through_write(0, 0x29);	/* Display on */
		sub_through_write(0, 0xB3);
		sub_through_write(1, 0x20);
		sub_through_write(1, 0xAA);
		sub_through_write(1, 0xA0);
		sub_through_write(1, 0x20);
		sub_through_write(1, 0x30);
		sub_through_write(1, 0xA6);
		sub_through_write(1, 0xFF);
		sub_through_write(1, 0x9A);
		sub_through_write(1, 0x9F);
		sub_through_write(1, 0xAF);
		sub_through_write(1, 0xBC);
		sub_through_write(1, 0xCF);
		sub_through_write(1, 0xDF);
		sub_through_write(1, 0x20);
		sub_through_write(1, 0x9C);
		sub_through_write(1, 0x8A);

		sub_through_write(0, 0x002C);	/* Display on */

		/* 1 pixel / 2 post clock */
		mddi_queue_register_write(REG_CLKDIV2, 0x7b00, FALSE, 0);

		/* SUB LCD select */
		mddi_queue_register_write(REG_PSTCTL2, 0x0080, FALSE, 0);

		/* RS=1,command initiate number=0,select master mode */
		mddi_queue_register_write(REG_SUBCTL, 0x0242, FALSE, 0);

		/* Sub LCD Data transform start */
		mddi_queue_register_write(REG_PSTCTL1, 0x0003, FALSE, 0);

	}

	/* Set the MDP pixel data attributes for Sub Display */
	mddi_host_write_pix_attr_reg(0x00C0);
}

void mddi_sharp_lcd_vsync_detected(boolean detected)
{
	/* static timetick_type start_time = 0; */
	static struct timeval start_time;
	static boolean first_time = TRUE;
	/* uint32 mdp_cnt_val = 0; */
	/* timetick_type elapsed_us; */
	struct timeval now;
	uint32 elapsed_us;
	uint32 num_vsyncs;

	if ((detected) || (mddi_sharp_vsync_attempts > 5)) {
		if ((detected) && (mddi_sharp_monitor_refresh_value)) {
			/* if (start_time != 0) */
			if (!first_time) {
				jiffies_to_timeval(jiffies, &now);
				elapsed_us =
				    (now.tv_sec - start_time.tv_sec) * 1000000 +
				    now.tv_usec - start_time.tv_usec;
				/*
				* LCD is configured for a refresh every usecs,
				* so to determine the number of vsyncs that
				* have occurred since the last measurement add
				* half that to the time difference and divide
				* by the refresh rate.
				*/
				num_vsyncs = (elapsed_us +
					      (mddi_sharp_usecs_per_refresh >>
					       1)) /
				    mddi_sharp_usecs_per_refresh;
				/*
				 * LCD is configured for * hsyncs (rows) per
				 * refresh cycle. Calculate new rows_per_second
				 * value based upon these new measurements.
				 * MDP can update with this new value.
				 */
				mddi_sharp_rows_per_second =
				    (mddi_sharp_rows_per_refresh * 1000 *
				     num_vsyncs) / (elapsed_us / 1000);
			}
			/* start_time = timetick_get(); */
			first_time = FALSE;
			jiffies_to_timeval(jiffies, &start_time);
			if (mddi_sharp_report_refresh_measurements) {
				/* mdp_cnt_val = MDP_LINE_COUNT; */
			}
		}
		/* if detected = TRUE, client initiated wakeup was detected */
		if (mddi_sharp_vsync_handler != NULL) {
			(*mddi_sharp_vsync_handler)
			    (mddi_sharp_vsync_handler_arg);
			mddi_sharp_vsync_handler = NULL;
		}
		mddi_vsync_detect_enabled = FALSE;
		mddi_sharp_vsync_attempts = 0;
		/* need to clear this vsync wakeup */
		if (!mddi_queue_register_write_int(REG_INTR, 0x0000)) {
			MDDI_MSG_ERR("Vsync interrupt clear failed!\n");
		}
		if (!detected) {
			/* give up after 5 failed attempts but show error */
			MDDI_MSG_NOTICE("Vsync detection failed!\n");
		} else if ((mddi_sharp_monitor_refresh_value) &&
			(mddi_sharp_report_refresh_measurements)) {
			MDDI_MSG_NOTICE("  Lines Per Second=%d!\n",
				mddi_sharp_rows_per_second);
		}
	} else
		/* if detected = FALSE, we woke up from hibernation, but did not
		 * detect client initiated wakeup.
		 */
		mddi_sharp_vsync_attempts++;
}

/* ISR to be executed */
void mddi_sharp_vsync_set_handler(msm_fb_vsync_handler_type handler, void *arg)
{
	boolean error = FALSE;
	unsigned long flags;

	/* Disable interrupts */
	spin_lock_irqsave(&mddi_host_spin_lock, flags);
	/* INTLOCK(); */

	if (mddi_sharp_vsync_handler != NULL)
		error = TRUE;

	/* Register the handler for this particular GROUP interrupt source */
	mddi_sharp_vsync_handler = handler;
	mddi_sharp_vsync_handler_arg = arg;

	/* Restore interrupts */
	spin_unlock_irqrestore(&mddi_host_spin_lock, flags);
	/* INTFREE(); */

	if (error)
		MDDI_MSG_ERR("MDDI: Previous Vsync handler never called\n");

	/* Enable the vsync wakeup */
	mddi_queue_register_write(REG_INTR, 0x8100, FALSE, 0);

	mddi_sharp_vsync_attempts = 1;
	mddi_vsync_detect_enabled = TRUE;
}				/* mddi_sharp_vsync_set_handler */

static int mddi_sharp_lcd_on(struct platform_device *pdev)
{
	struct msm_fb_data_type *mfd;

	mfd = platform_get_drvdata(pdev);

	if (!mfd)
		return -ENODEV;

	if (mfd->key != MFD_KEY)
		return -EINVAL;

	if (mfd->panel.id == SHARP_QVGA_PRIM)
		mddi_sharp_prim_lcd_init();
	else
		mddi_sharp_sub_lcd_init();

	return 0;
}

static int mddi_sharp_lcd_off(struct platform_device *pdev)
{
	mddi_sharp_lcd_powerdown();
	return 0;
}

static int __init mddi_sharp_probe(struct platform_device *pdev)
{
	if (pdev->id == 0) {
		mddi_sharp_pdata = pdev->dev.platform_data;
		return 0;
	}

	msm_fb_add_device(pdev);

	return 0;
}

static struct platform_driver this_driver = {
	.probe  = mddi_sharp_probe,
	.driver = {
		.name   = "mddi_sharp_qvga",
	},
};

static struct msm_fb_panel_data mddi_sharp_panel_data0 = {
	.on = mddi_sharp_lcd_on,
	.off = mddi_sharp_lcd_off,
	.set_backlight = mddi_sharp_lcd_set_backlight,
	.set_vsync_notifier = mddi_sharp_vsync_set_handler,
};

static struct platform_device this_device_0 = {
	.name   = "mddi_sharp_qvga",
	.id	= SHARP_QVGA_PRIM,
	.dev	= {
		.platform_data = &mddi_sharp_panel_data0,
	}
};

static struct msm_fb_panel_data mddi_sharp_panel_data1 = {
	.on = mddi_sharp_lcd_on,
	.off = mddi_sharp_lcd_off,
};

static struct platform_device this_device_1 = {
	.name   = "mddi_sharp_qvga",
	.id	= SHARP_128X128_SECD,
	.dev	= {
		.platform_data = &mddi_sharp_panel_data1,
	}
};

static int __init mddi_sharp_init(void)
{
	int ret;
	struct msm_panel_info *pinfo;

#ifdef CONFIG_FB_MSM_MDDI_AUTO_DETECT
	u32 id;

	ret = msm_fb_detect_client("mddi_sharp_qvga");
	if (ret == -ENODEV)
		return 0;

	if (ret) {
		id = mddi_get_client_id();

		if (((id >> 16) != 0x0) || ((id & 0xffff) != 0x8835))
			return 0;
	}
#endif
	if (mddi_host_core_version > 8) {
		/* can use faster refresh with newer hw revisions */
		mddi_sharp_debug_60hz_refresh = TRUE;

		/* Timing variables for tracking vsync */
		/* dot_clock = 6.00MHz
		 * horizontal count = 296
		 * vertical count = 338
		 * refresh rate = 6000000/(296+338) = 60Hz
		 */
		mddi_sharp_rows_per_second = 20270;	/* 6000000/296 */
		mddi_sharp_rows_per_refresh = 338;
		mddi_sharp_usecs_per_refresh = 16674;	/* (296+338)/6000000 */
	} else {
		/* Timing variables for tracking vsync */
		/* dot_clock = 5.20MHz
		 * horizontal count = 376
		 * vertical count = 338
		 * refresh rate = 5200000/(376+338) = 41Hz
		 */
		mddi_sharp_rows_per_second = 13830;	/* 5200000/376 */
		mddi_sharp_rows_per_refresh = 338;
		mddi_sharp_usecs_per_refresh = 24440;	/* (376+338)/5200000 */
	}

	ret = platform_driver_register(&this_driver);
	if (!ret) {
		pinfo = &mddi_sharp_panel_data0.panel_info;
		pinfo->xres = 240;
		pinfo->yres = 320;
		pinfo->type = MDDI_PANEL;
		pinfo->pdest = DISPLAY_1;
		pinfo->mddi.vdopkt = MDDI_DEFAULT_PRIM_PIX_ATTR;
		pinfo->wait_cycle = 0;
		pinfo->bpp = 18;
		pinfo->fb_num = 2;
		pinfo->clk_rate = 122880000;
		pinfo->clk_min = 120000000;
		pinfo->clk_max = 125000000;
		pinfo->lcd.vsync_enable = TRUE;
		pinfo->lcd.refx100 =
			(mddi_sharp_rows_per_second * 100) /
			mddi_sharp_rows_per_refresh;
		pinfo->lcd.v_back_porch = 12;
		pinfo->lcd.v_front_porch = 6;
		pinfo->lcd.v_pulse_width = 0;
		pinfo->lcd.hw_vsync_mode = FALSE;
		pinfo->lcd.vsync_notifier_period = (1 * HZ);
		pinfo->bl_max = 7;
		pinfo->bl_min = 1;

		ret = platform_device_register(&this_device_0);
		if (ret)
			platform_driver_unregister(&this_driver);

		pinfo = &mddi_sharp_panel_data1.panel_info;
		pinfo->xres = 128;
		pinfo->yres = 128;
		pinfo->type = MDDI_PANEL;
		pinfo->pdest = DISPLAY_2;
		pinfo->mddi.vdopkt = 0x400;
		pinfo->wait_cycle = 0;
		pinfo->bpp = 18;
		pinfo->clk_rate = 122880000;
		pinfo->clk_min = 120000000;
		pinfo->clk_max = 125000000;
		pinfo->fb_num = 2;

		ret = platform_device_register(&this_device_1);
		if (ret) {
			platform_device_unregister(&this_device_0);
			platform_driver_unregister(&this_driver);
		}
	}

	if (!ret)
		mddi_lcd.vsync_detected = mddi_sharp_lcd_vsync_detected;

	return ret;
}

module_init(mddi_sharp_init);