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