Merge branch 'master' of kmpark@party:/pub/git/u-boot-s5pc1xx
[kernel/u-boot.git] / drivers / video / s5p-fb.c
1 /*
2  * S5PC100 and S5PC110 LCD Controller driver.
3  *
4  * Author: InKi Dae <inki.dae@samsung.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21
22 #include <config.h>
23 #include <common.h>
24 #include <version.h>
25 #include <stdarg.h>
26 #include <linux/types.h>
27 #include <asm/io.h>
28 #include <asm/arch/cpu.h>
29 #include <lcd.h>
30
31 #include "s5p-fb.h"
32 #include "logo.h"
33 /*
34 #include "logo_rgb24_wvga_portrait.h"
35 #include "opening_logo_rgb24_143_44.h"
36 */
37
38 extern void tl2796_panel_power_on(void);
39 extern void tl2796_panel_enable(void);
40 extern void tl2796_panel_init(void);
41
42 int lcd_line_length;
43 int lcd_color_fg;
44 int lcd_color_bg;
45
46 void *lcd_base;
47 void *lcd_console_address;
48
49 short console_col;
50 short console_row;
51
52 static unsigned int panel_width, panel_height;
53
54 static unsigned short makepixel565(char r, char g, char b)
55 {
56     return (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
57 }
58
59 static unsigned int makepixel8888(char a, char r, char g, char b)
60 {
61         return (unsigned int)((a << 24) | (r << 16) | (g << 8)  | b);
62 }
63
64 static void read_image16(char* pImg, int x1pos, int y1pos, int x2pos,
65                 int y2pos, unsigned short pixel)
66 {
67         unsigned short *pDst = (unsigned short *)pImg;
68         unsigned int offset_s;
69         int i, j;
70
71         for(i = y1pos; i < y2pos; i++) {
72                 for(j = x1pos; j < x2pos; j++) {
73                         offset_s = i * panel_width + j;
74                         *(pDst + offset_s) = pixel;
75                 }
76         }
77 }
78
79 static void read_image32(char* pImg, int x1pos, int y1pos, int x2pos,
80                 int y2pos, unsigned int pixel)
81 {
82         unsigned int *pDst = (unsigned int *)pImg;
83         unsigned int offset_s;
84         int i, j;
85
86         for (i = y1pos; i < y2pos; i++) {
87                 for (j = x1pos; j < x2pos; j++) {
88                         offset_s = i * panel_width + j;
89                         *(pDst+offset_s) = pixel;
90                 }
91         }
92 }
93
94 /* LCD Panel data */
95 vidinfo_t panel_info;
96
97 struct lcd_panel_operation {
98         void (*lcd_panel_init)(void);
99         void (*lcd_panel_power_on)(void);
100         void (*lcd_panel_enable)(void);
101 };
102
103 static struct lcd_panel_operation lcd_calls;
104
105 static void s5pc_lcd_init_mem(void *lcdbase, vidinfo_t *vid)
106 {
107         unsigned long palette_size, palette_mem_size;
108         unsigned int fb_size;
109
110         fb_size = vid->vl_row * vid->vl_col * (vid->vl_bpix >> 3);
111
112         lcd_base = lcdbase;
113
114         palette_size = NBITS(vid->vl_bpix) == 8 ? 256 : 16;
115         palette_mem_size = palette_size * sizeof(u32);
116
117         s5pc_fimd_lcd_init_mem((unsigned long)lcd_base, (unsigned long)fb_size, palette_size);
118
119         udebug("fb_size=%d, screen_base=%x, palette_size=%d, palettle_mem_size=%d\n",
120                         fb_size, (unsigned int)lcd_base, (int)palette_size, (int)palette_mem_size);
121 }
122
123 static void s5pc_gpio_setup(void)
124 {
125         if (cpu_is_s5pc100())
126                 s5pc_c100_gpio_setup();
127         else
128                 s5pc_c110_gpio_setup();
129 }
130
131 static void s5pc_lcd_init(vidinfo_t *vid)
132 {
133         s5pc_fimd_lcd_init(vid);
134 }
135
136 static void lcd_test(void)
137 {
138         /* red */
139         read_image32((char *)lcd_base, 0, 0, 480, 200,
140                         makepixel8888(0, 255, 0, 0));
141         /* green */
142         read_image32((char *)lcd_base, 0, 200, 480, 400,
143                         makepixel8888(0, 0, 255, 0));
144         /* blue */
145         read_image32((char *)lcd_base, 0, 400, 480, 600,
146                         makepixel8888(0, 0, 0, 255));
147         /* write */
148         read_image32((char *)lcd_base, 0, 600, 480, 800,
149                         makepixel8888(0, 255, 255, 255));
150 }
151
152 int conv_rgb565_to_rgb888(unsigned short rgb565, unsigned int sw)
153 {
154         char red, green, blue;
155         unsigned int threshold = 150;
156
157         red = (rgb565 & 0xF800) >> 11;
158         green = (rgb565 & 0x7E0) >> 5;
159         blue = (rgb565 & 0x1F);
160
161         red = red << 3;
162         green = green << 2;
163         blue = blue << 3;
164
165         /* correct error pixels of samsung logo. */
166         if (sw) {
167                 if (red > threshold)
168                         red = 255;
169                 if (green > threshold)
170                         green = 255;
171                 if (blue > threshold)
172                         blue = 255;
173         }
174
175         return (red << 16 | green << 8 | blue);
176 }
177
178 void draw_bitmap(void *lcdbase, int x, int y, int w, int h, unsigned long *bmp)
179 {
180         int i, j;
181         short k = 0;
182         unsigned long *fb = (unsigned  long*)lcdbase;
183
184         for (j = y; j < (y + h); j++) {
185                 for (i = x; i < (x + w); i++)
186                         *(fb + (j * panel_width) + i) = *(bmp + k++);
187         }
188 }
189
190 void _draw_samsung_logo(void *lcdbase, int x, int y, int w, int h, unsigned short *bmp)
191 {
192         int i, j, error_range = 40;
193         short k = 0;
194         unsigned int pixel;
195         unsigned long *fb = (unsigned  long*)lcdbase;
196
197         for (j = y; j < (y + h); j++) {
198                 for (i = x; i < (x + w); i++) {
199                         pixel = (*(bmp + k++));
200
201                         /* 40 lines under samsung logo image are error range. */
202                         if (j > h + y - error_range)
203                                 *(fb + (j * panel_width) + i) =
204                                         conv_rgb565_to_rgb888(pixel, 1);
205                         else
206                                 *(fb + (j * panel_width) + i) =
207                                         conv_rgb565_to_rgb888(pixel, 0);
208                 }
209         }
210 }
211
212 static void draw_samsung_logo(void* lcdbase)
213 {
214         int x, y;
215
216         x = ((panel_width - 298) >> 1);
217         y = ((panel_height - 78) >> 1) - 5;
218
219         _draw_samsung_logo(lcdbase, x, y, 298, 78, (unsigned short *) logo);
220 }
221
222 static void s5pc_init_panel_info(vidinfo_t *vid, struct lcd_panel_operation *calls)
223 {
224         if (vid == NULL) {
225                 printf("lcd info is NULL.\n");
226                 return;
227         }
228
229         if (calls == NULL) {
230                 printf("lcd calls is NULL.\n");
231                 return;
232         }
233 #if 1
234         vid->vl_freq    = 60;
235         vid->vl_col     = 480,
236         vid->vl_row     = 800,
237         vid->vl_width   = 480,
238         vid->vl_height  = 800,
239         vid->vl_clkp    = CONFIG_SYS_HIGH,
240         vid->vl_hsp     = CONFIG_SYS_LOW,
241         vid->vl_vsp     = CONFIG_SYS_LOW,
242         vid->vl_dp      = CONFIG_SYS_HIGH,
243         vid->vl_bpix    = 32,
244
245         /* S6E63M0 LCD Panel */
246         vid->vl_hpw     = 2,    /* HLW */
247         vid->vl_blw     = 16,   /* HBP */
248         vid->vl_elw     = 16,   /* HFP */
249
250         vid->vl_vpw     = 2,    /* VLW */
251         vid->vl_bfw     = 3,    /* VBP */
252         vid->vl_efw     = 28,   /* VFP */
253
254         calls->lcd_panel_init = s6e63m0_lcd_panel_init;
255         calls->lcd_panel_power_on = s6e63m0_lcd_panel_power_on;
256         calls->lcd_panel_enable = s6e63m0_lcd_panel_enable;
257 #endif
258 #if 0
259         vid->vl_freq    = 60;
260         vid->vl_col     = 480,
261         vid->vl_row     = 800,
262         vid->vl_width   = 480,
263         vid->vl_height  = 800,
264         vid->vl_clkp    = CONFIG_SYS_HIGH,
265         vid->vl_hsp     = CONFIG_SYS_LOW,
266         vid->vl_vsp     = CONFIG_SYS_LOW,
267         vid->vl_dp      = CONFIG_SYS_HIGH,
268         vid->vl_bpix    = 32,
269
270         /* tl2796 panel. */
271         vid->vl_hpw     = 4,
272         vid->vl_blw     = 8,
273         vid->vl_elw     = 8,
274
275         vid->vl_vpw     = 4,
276         vid->vl_bfw     = 8,
277         vid->vl_efw     = 8,
278
279         calls->lcd_panel_init = /* */;
280         calls->lcd_panel_power_on = /* */;
281         calls->lcd_panel_enable = /* */;
282 #endif
283 #if 0
284         vid->vl_freq    = 60;
285         vid->vl_col     = 1024,
286         vid->vl_row     = 600,
287         vid->vl_width   = 1024,
288         vid->vl_height  = 600,
289         vid->vl_clkp    = CONFIG_SYS_HIGH,
290         vid->vl_hsp     = CONFIG_SYS_HIGH,
291         vid->vl_vsp     = CONFIG_SYS_HIGH,
292         vid->vl_dp      = CONFIG_SYS_LOW,
293         vid->vl_bpix    = 32,
294
295         /* AMS701KA AMOLED Panel. */
296         vid->vl_hpw     = 30,
297         vid->vl_blw     = 114,
298         vid->vl_elw     = 48,
299
300         vid->vl_vpw     = 2,
301         vid->vl_bfw     = 6,
302         vid->vl_efw     = 8,
303
304         calls->lcd_panel_init = ams701ka_lcd_panel_init;
305         calls->lcd_panel_power_on = ams701ka_lcd_panel_power_on;
306         calls->lcd_panel_enable = ams701ka_lcd_panel_enable;
307 #endif
308
309         panel_width = vid->vl_col;
310         panel_height = vid->vl_row;
311 }
312
313 static void lcd_panel_on(struct lcd_panel_operation *calls)
314 {
315         if (calls == NULL) {
316                 printf("lcd calls is NULL.\n");
317                 return ;
318         }
319
320         if (calls->lcd_panel_init)
321                 calls->lcd_panel_init();
322         if (calls->lcd_panel_power_on)
323                 calls->lcd_panel_power_on();
324         if (calls->lcd_panel_enable)
325                 calls->lcd_panel_enable();
326 }
327
328 void lcd_ctrl_init(void *lcdbase)
329 {
330         char *option;
331
332         s5pc_lcd_init_mem(lcdbase, &panel_info);
333
334         /* initialize parameters which is specific to panel. */
335         s5pc_init_panel_info(&panel_info, &lcd_calls);
336
337         option = getenv("lcd");
338
339         /*
340         if (strcmp(option, "test") == 0) {
341                 memset(lcdbase, 0, PANEL_WIDTH*PANEL_HEIGHT*S5P_LCD_BPP >> 3);
342                 lcd_test();
343         } else if (strcmp(option, "image") == 0)
344                 memcpy(lcdbase, LOGO_RGB24, PANEL_WIDTH*PANEL_HEIGHT*S5P_LCD_BPP >> 3);
345         else {
346                 memset(lcdbase, 0, PANEL_WIDTH*PANEL_HEIGHT*S5P_LCD_BPP >> 3);
347                 draw_samsung_logo(lcdbase);
348         }
349         */
350
351         memset(lcdbase, 0, panel_width * panel_height * (32 >> 3));
352         draw_samsung_logo(lcdbase);
353
354         s5pc_gpio_setup();
355
356         s5pc_lcd_init(&panel_info);
357
358         /* font test */
359         /*
360         init_font();
361         set_font_color(FONT_WHITE);
362         fb_printf("Test\n");
363         exit_font();
364         */
365 }
366
367
368 void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blud)
369 {
370         return;
371 }
372
373 void lcd_enable(void)
374 {
375         lcd_panel_on((struct lcd_panel_operation *) &lcd_calls);
376 }
377
378 ulong calc_fbsize(void)
379 {
380         return s5pc_fimd_calc_fbsize();
381 }
382
383 void s5pc1xxfb_test(void *lcdbase)
384 {
385         lcd_ctrl_init(lcdbase);
386         lcd_enable();
387 }