1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2010-01-01     bernard      first version from QiuYi's driver
9  */
10 
11 #include <rtthread.h>
12 #include <s3c24x0.h>
13 
14 
15 /* LCD driver for A7' */
16 #define LCD_WIDTH 800
17 #define LCD_HEIGHT 480
18 #define LCD_PIXCLOCK 2
19 
20 #define LCD_RIGHT_MARGIN 67
21 #define LCD_LEFT_MARGIN 40
22 #define LCD_HSYNC_LEN 31
23 
24 #define LCD_UPPER_MARGIN 25
25 #define LCD_LOWER_MARGIN 5
26 #define LCD_VSYNC_LEN 1
27 
28 #define LCD_XSIZE  LCD_WIDTH
29 #define LCD_YSIZE  LCD_HEIGHT
30 #define SCR_XSIZE  LCD_WIDTH
31 #define SCR_YSIZE  LCD_HEIGHT
32 
33 #define RT_HW_LCD_WIDTH     LCD_WIDTH
34 #define RT_HW_LCD_HEIGHT    LCD_HEIGHT
35 
36 #define MVAL        (13)
37 #define MVAL_USED   (0)     //0=each frame   1=rate by MVAL
38 #define INVVDEN     (1)     //0=normal       1=inverted
39 #define BSWP        (0)     //Byte swap control
40 #define HWSWP       (1)     //Half word swap control
41 
42 #define GPB1_TO_OUT()       (GPBUP &= 0xfffd, GPBCON &= 0xfffffff3, GPBCON |= 0x00000004)
43 #define GPB1_TO_1()         (GPBDAT |= 0x0002)
44 #define GPB1_TO_0()         (GPBDAT &= 0xfffd)
45 
46 #define S3C2410_LCDCON1_CLKVAL(x)  ((x) << 8)
47 #define S3C2410_LCDCON1_MMODE      (1<<7)
48 #define S3C2410_LCDCON1_DSCAN4     (0<<5)
49 #define S3C2410_LCDCON1_STN4       (1<<5)
50 #define S3C2410_LCDCON1_STN8       (2<<5)
51 #define S3C2410_LCDCON1_TFT        (3<<5)
52 
53 #define S3C2410_LCDCON1_STN1BPP    (0<<1)
54 #define S3C2410_LCDCON1_STN2GREY   (1<<1)
55 #define S3C2410_LCDCON1_STN4GREY   (2<<1)
56 #define S3C2410_LCDCON1_STN8BPP    (3<<1)
57 #define S3C2410_LCDCON1_STN12BPP   (4<<1)
58 
59 #define S3C2410_LCDCON1_TFT1BPP    (8<<1)
60 #define S3C2410_LCDCON1_TFT2BPP    (9<<1)
61 #define S3C2410_LCDCON1_TFT4BPP    (10<<1)
62 #define S3C2410_LCDCON1_TFT8BPP    (11<<1)
63 #define S3C2410_LCDCON1_TFT16BPP   (12<<1)
64 #define S3C2410_LCDCON1_TFT24BPP   (13<<1)
65 
66 #define S3C2410_LCDCON1_ENVID      (1)
67 
68 #define S3C2410_LCDCON1_MODEMASK    0x1E
69 
70 #define S3C2410_LCDCON2_VBPD(x)     ((x) << 24)
71 #define S3C2410_LCDCON2_LINEVAL(x)  ((x) << 14)
72 #define S3C2410_LCDCON2_VFPD(x)     ((x) << 6)
73 #define S3C2410_LCDCON2_VSPW(x)     ((x) << 0)
74 
75 #define S3C2410_LCDCON2_GET_VBPD(x) ( ((x) >> 24) & 0xFF)
76 #define S3C2410_LCDCON2_GET_VFPD(x) ( ((x) >>  6) & 0xFF)
77 #define S3C2410_LCDCON2_GET_VSPW(x) ( ((x) >>  0) & 0x3F)
78 
79 #define S3C2410_LCDCON3_HBPD(x)     ((x) << 19)
80 #define S3C2410_LCDCON3_WDLY(x)     ((x) << 19)
81 #define S3C2410_LCDCON3_HOZVAL(x)   ((x) << 8)
82 #define S3C2410_LCDCON3_HFPD(x)     ((x) << 0)
83 #define S3C2410_LCDCON3_LINEBLANK(x)((x) << 0)
84 
85 #define S3C2410_LCDCON3_GET_HBPD(x) ( ((x) >> 19) & 0x7F)
86 #define S3C2410_LCDCON3_GET_HFPD(x) ( ((x) >>  0) & 0xFF)
87 
88 #define S3C2410_LCDCON4_MVAL(x)     ((x) << 8)
89 #define S3C2410_LCDCON4_HSPW(x)     ((x) << 0)
90 #define S3C2410_LCDCON4_WLH(x)      ((x) << 0)
91 
92 #define S3C2410_LCDCON4_GET_HSPW(x) ( ((x) >>  0) & 0xFF)
93 
94 #define S3C2410_LCDCON5_BPP24BL     (1<<12)
95 #define S3C2410_LCDCON5_FRM565      (1<<11)
96 #define S3C2410_LCDCON5_INVVCLK     (1<<10)
97 #define S3C2410_LCDCON5_INVVLINE    (1<<9)
98 #define S3C2410_LCDCON5_INVVFRAME   (1<<8)
99 #define S3C2410_LCDCON5_INVVD       (1<<7)
100 #define S3C2410_LCDCON5_INVVDEN     (1<<6)
101 #define S3C2410_LCDCON5_INVPWREN    (1<<5)
102 #define S3C2410_LCDCON5_INVLEND     (1<<4)
103 #define S3C2410_LCDCON5_PWREN       (1<<3)
104 #define S3C2410_LCDCON5_ENLEND      (1<<2)
105 #define S3C2410_LCDCON5_BSWP        (1<<1)
106 #define S3C2410_LCDCON5_HWSWP       (1<<0)
107 #define S3C2410_LCDINT_FRSYNC       (1<<1)
108 
109 static volatile rt_uint16_t _rt_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
110 //static volatile rt_uint16_t _rt_hw_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
111 static struct rt_device_graphic_info _lcd_info;
112 
lcd_power_enable(int invpwren,int pwren)113 static void lcd_power_enable(int invpwren, int pwren)
114 {
115     //GPG4 is setted as LCD_PWREN
116     GPGUP  = GPGUP | (1<<4); // Pull-up disable
117     GPGCON = GPGCON | (3<<8); //GPG4=LCD_PWREN
118 
119     //Enable LCD POWER ENABLE Function
120     LCDCON5 = LCDCON5&(~(1<<3))|(pwren<<3);   // PWREN
121     LCDCON5 = LCDCON5&(~(1<<5))|(invpwren<<5);   // INVPWREN
122 }
123 
lcd_envid_on_off(int onoff)124 static void lcd_envid_on_off(int onoff)
125 {
126     if(onoff==1)
127         /*ENVID=ON*/
128         LCDCON1|=1;
129     else
130         /*ENVID Off*/
131         LCDCON1 =LCDCON1 & 0x3fffe;
132 }
133 
134 //********************** BOARD LCD backlight ****************************
LcdBkLtSet(rt_uint32_t HiRatio)135 static void LcdBkLtSet(rt_uint32_t HiRatio)
136 {
137 #define FREQ_PWM1       1000
138     if(!HiRatio)
139     {
140         GPBCON  = GPBCON & (~(3<<2)) | (1<<2) ;
141         GPBDAT &= ~(1<<1);
142         return;
143     }
144     GPBCON = GPBCON & (~(3<<2)) | (2<<2) ;
145 
146     if( HiRatio > 100 ) HiRatio = 100 ;
147 
148     TCON = TCON & (~(0xf<<8)) ;             // clear manual update bit, stop Timer1
149 
150     TCFG0 &= 0xffffff00;                    // set Timer 0&1 prescaler 0
151     TCFG0 |= 15;                            //prescaler = 15+1
152 
153     TCFG1 &= 0xffffff0f;                    // set Timer 1 MUX 1/16
154     TCFG1 |= 0x00000030;                    // set Timer 1 MUX 1/16
155 
156     TCNTB1   = ( 100000000>>8 )/FREQ_PWM1;  //if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
157     TCMPB1  = ( TCNTB1*(100-HiRatio))/100 ; //if set inverter on,  when TCNT2<=TCMP2, TOUT is low,  TCNT2>TCMP2, TOUT is high
158 
159     TCON = TCON & (~(0xf<<8)) | (0x0e<<8) ;
160     TCON = TCON & (~(0xf<<8)) | (0x0d<<8) ;
161 }
162 
163 /* RT-Thread Device Interface */
rt_lcd_init(rt_device_t dev)164 static rt_err_t rt_lcd_init (rt_device_t dev)
165 {
166     GPB1_TO_OUT();
167     GPB1_TO_1();
168 
169     GPCUP  = 0x00000000;
170     GPCCON = 0xaaaa02a9;
171 
172     GPDUP  = 0x00000000;
173     GPDCON = 0xaaaaaaaa;
174 
175 #define M5D(n)  ((n)&0x1fffff)
176 #define LCD_ADDR ((rt_uint32_t)_rt_framebuffer)
177     LCDCON1 = (LCD_PIXCLOCK << 8) | (3 <<  5) | (12 << 1);
178     LCDCON2 = (LCD_UPPER_MARGIN << 24) | ((LCD_HEIGHT - 1) << 14) | (LCD_LOWER_MARGIN << 6) | (LCD_VSYNC_LEN << 0);
179     LCDCON3 = (LCD_RIGHT_MARGIN << 19) | ((LCD_WIDTH  - 1) <<  8) | (LCD_LEFT_MARGIN << 0);
180     LCDCON4 = (13 <<  8) | (LCD_HSYNC_LEN << 0);
181 #if !defined(LCD_CON5)
182 #define LCD_CON5 ((1<<11) | (1 << 9) | (1 << 8) | (1 << 3) | (1 << 0))
183 #endif
184     LCDCON5   =  LCD_CON5;
185 
186     LCDSADDR1 = ((LCD_ADDR >> 22) << 21) | ((M5D(LCD_ADDR >> 1)) <<  0);
187     LCDSADDR2 = M5D((LCD_ADDR + LCD_WIDTH * LCD_HEIGHT * 2) >> 1);
188     LCDSADDR3 = LCD_WIDTH;
189 
190     LCDINTMSK |= (3);
191     LPCSEL &= (~7) ;
192     TPAL=0;
193 
194     LcdBkLtSet(70) ;
195     lcd_power_enable(0, 1);
196     lcd_envid_on_off(1);
197 
198     return RT_EOK;
199 }
200 
rt_lcd_control(rt_device_t dev,int cmd,void * args)201 static rt_err_t rt_lcd_control (rt_device_t dev, int cmd, void *args)
202 {
203     switch (cmd)
204     {
205     case RTGRAPHIC_CTRL_RECT_UPDATE:
206         break;
207     case RTGRAPHIC_CTRL_POWERON:
208         break;
209     case RTGRAPHIC_CTRL_POWEROFF:
210         break;
211     case RTGRAPHIC_CTRL_GET_INFO:
212         rt_memcpy(args, &_lcd_info, sizeof(_lcd_info));
213         break;
214     case RTGRAPHIC_CTRL_SET_MODE:
215         break;
216     }
217 
218     return RT_EOK;
219 }
220 
rt_hw_lcd_init(void)221 int rt_hw_lcd_init(void)
222 {
223     rt_device_t lcd = rt_malloc(sizeof(struct rt_device));
224     if (lcd == RT_NULL)
225             return -RT_ERROR; /* no memory yet */
226 
227     _lcd_info.bits_per_pixel = 16;
228     _lcd_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
229     _lcd_info.framebuffer = (void*)_rt_framebuffer;
230     _lcd_info.width = LCD_WIDTH;
231     _lcd_info.height = LCD_HEIGHT;
232 
233     /* init device structure */
234     lcd->type = RT_Device_Class_Unknown;
235     lcd->init = rt_lcd_init;
236     lcd->open = RT_NULL;
237     lcd->close = RT_NULL;
238     lcd->control = rt_lcd_control;
239     lcd->user_data = (void*)&_lcd_info;
240 
241     /* register lcd device to RT-Thread */
242     rt_device_register(lcd, "lcd", RT_DEVICE_FLAG_RDWR);
243 }
244 
245 INIT_BOARD_EXPORT(rt_hw_lcd_init);
246