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