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 
13 #include <s3c24x0.h>
14 
15 /* LCD driver for T3'5 */
16 #define LCD_WIDTH 240
17 #define LCD_HEIGHT 320
18 #define LCD_PIXCLOCK 4
19 
20 #define LCD_RIGHT_MARGIN 25
21 #define LCD_LEFT_MARGIN 0
22 #define LCD_HSYNC_LEN 4
23 
24 #define LCD_UPPER_MARGIN 1
25 #define LCD_LOWER_MARGIN 4
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 
108 #define S3C2410_LCDINT_FRSYNC       (1<<1)
109 
110 volatile rt_uint16_t _rt_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
111 //volatile rt_uint16_t _rt_hw_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
112 static struct rt_device_graphic_info _lcd_info;
113 
lcd_power_enable(int invpwren,int pwren)114 static void lcd_power_enable(int invpwren, int pwren)
115 {
116     //GPG4 is setted as LCD_PWREN
117     GPGUP  = GPGUP | (1<<4); // Pull-up disable
118     GPGCON = GPGCON | (3<<8); //GPG4=LCD_PWREN
119 
120     //Enable LCD POWER ENABLE Function
121     LCDCON5 = LCDCON5&(~(1<<3))|(pwren<<3);   // PWREN
122     LCDCON5 = LCDCON5&(~(1<<5))|(invpwren<<5);   // INVPWREN
123 }
124 
lcd_envid_on_off(int onoff)125 static void lcd_envid_on_off(int onoff)
126 {
127     if(onoff==1)
128         /*ENVID=ON*/
129         LCDCON1|=1;
130     else
131         /*ENVID Off*/
132         LCDCON1 =LCDCON1 & 0x3fffe;
133 }
134 
135 //********************** BOARD LCD backlight ****************************
LcdBkLtSet(rt_uint32_t HiRatio)136 static void LcdBkLtSet(rt_uint32_t HiRatio)
137 {
138 #define FREQ_PWM1       1000
139     if(!HiRatio)
140     {
141         GPBCON  = GPBCON & (~(3<<2)) | (1<<2) ;
142         GPBDAT &= ~(1<<1);
143         return;
144     }
145     GPBCON = GPBCON & (~(3<<2)) | (2<<2) ;
146 
147     if( HiRatio > 100 ) HiRatio = 100 ;
148 
149     TCON = TCON & (~(0xf<<8)) ;             // clear manual update bit, stop Timer1
150 
151     TCFG0 &= 0xffffff00;                    // set Timer 0&1 prescaler 0
152     TCFG0 |= 15;                            //prescaler = 15+1
153 
154     TCFG1 &= 0xffffff0f;                    // set Timer 1 MUX 1/16
155     TCFG1 |= 0x00000030;                    // set Timer 1 MUX 1/16
156 
157     TCNTB1   = ( 100000000>>8 )/FREQ_PWM1;  //if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
158     TCMPB1  = ( TCNTB1*(100-HiRatio))/100 ; //if set inverter on,  when TCNT2<=TCMP2, TOUT is low,  TCNT2>TCMP2, TOUT is high
159 
160     TCON = TCON & (~(0xf<<8)) | (0x0e<<8) ;
161     TCON = TCON & (~(0xf<<8)) | (0x0d<<8) ;
162 }
163 
164 /* RT-Thread Device Interface */
rt_lcd_init(rt_device_t dev)165 static rt_err_t rt_lcd_init (rt_device_t dev)
166 {
167     GPB1_TO_OUT();
168     GPB1_TO_1();
169 
170     GPCUP  = 0x00000000;
171     GPCCON = 0xaaaa02a9;
172 
173     GPDUP  = 0x00000000;
174     GPDCON = 0xaaaaaaaa;
175 
176 #define M5D(n)  ((n)&0x1fffff)
177 #define LCD_ADDR ((rt_uint32_t)_rt_framebuffer)
178     LCDCON1 = (LCD_PIXCLOCK << 8) | (3 <<  5) | (12 << 1);
179     LCDCON2 = (LCD_UPPER_MARGIN << 24) | ((LCD_HEIGHT - 1) << 14) | (LCD_LOWER_MARGIN << 6) | (LCD_VSYNC_LEN << 0);
180     LCDCON3 = (LCD_RIGHT_MARGIN << 19) | ((LCD_WIDTH  - 1) <<  8) | (LCD_LEFT_MARGIN << 0);
181     LCDCON4 = (13 <<  8) | (LCD_HSYNC_LEN << 0);
182 
183 #if !defined(LCD_CON5)
184 #define LCD_CON5 ((1<<11) | (1 << 9) | (1 << 8) | (1 << 3) | (1 << 0))
185 #endif
186     LCDCON5   =  LCD_CON5;
187 
188     LCDSADDR1 = ((LCD_ADDR >> 22) << 21) | ((M5D(LCD_ADDR >> 1)) <<  0);
189     LCDSADDR2 = M5D((LCD_ADDR + LCD_WIDTH * LCD_HEIGHT * 2) >> 1);
190     LCDSADDR3 = LCD_WIDTH;
191 
192     LCDINTMSK |= (3);
193     LPCSEL &= (~7) ;
194     TPAL=0;
195 
196     LcdBkLtSet(70) ;
197     lcd_power_enable(0, 1);
198     lcd_envid_on_off(1);
199 
200     return RT_EOK;
201 }
202 
rt_lcd_control(rt_device_t dev,int cmd,void * args)203 static rt_err_t rt_lcd_control (rt_device_t dev, int cmd, void *args)
204 {
205     switch (cmd)
206     {
207     case RTGRAPHIC_CTRL_RECT_UPDATE:
208         break;
209     case RTGRAPHIC_CTRL_POWERON:
210         break;
211     case RTGRAPHIC_CTRL_POWEROFF:
212         break;
213     case RTGRAPHIC_CTRL_GET_INFO:
214         rt_memcpy(args, &_lcd_info, sizeof(_lcd_info));
215         break;
216     case RTGRAPHIC_CTRL_SET_MODE:
217         break;
218     }
219 
220     return RT_EOK;
221 }
222 
rt_hw_lcd_init(void)223 int rt_hw_lcd_init(void)
224 {
225     rt_device_t lcd = rt_malloc(sizeof(struct rt_device));
226     if (lcd == RT_NULL)
227         return -RT_ERROR; /* no memory yet */
228 
229     _lcd_info.bits_per_pixel = 16;
230     _lcd_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
231     _lcd_info.framebuffer = (void*)_rt_framebuffer;
232     _lcd_info.width = LCD_WIDTH;
233     _lcd_info.height = LCD_HEIGHT;
234 
235     /* init device structure */
236     lcd->type = RT_Device_Class_Unknown;
237     lcd->init = rt_lcd_init;
238     lcd->open = RT_NULL;
239     lcd->close = RT_NULL;
240     lcd->control = rt_lcd_control;
241     lcd->user_data = (void*)&_lcd_info;
242 
243     /* register lcd device to RT-Thread */
244     rt_device_register(lcd, "lcd", RT_DEVICE_FLAG_RDWR);
245 }
246 
247 INIT_BOARD_EXPORT(rt_hw_lcd_init);
248