s5pc110: fb: possible memory leak (un-free'd malloc)
[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 #include <malloc.h>
31
32 #include "s5p-fb.h"
33 #include "logo.h"
34 /*
35 #include "logo_rgb24_wvga_portrait.h"
36 #include "opening_logo_rgb24_143_44.h"
37 */
38
39 extern void tl2796_panel_power_on(void);
40 extern void tl2796_panel_enable(void);
41 extern void tl2796_panel_init(void);
42
43 int lcd_line_length;
44 int lcd_color_fg;
45 int lcd_color_bg;
46
47 void *lcd_base;
48 void *lcd_console_address;
49
50 short console_col;
51 short console_row;
52
53 static unsigned int panel_width, panel_height;
54
55 #ifdef USE_LCD_TEST
56 static unsigned short makepixel565(char r, char g, char b)
57 {
58     return (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
59 }
60
61 static unsigned int makepixel8888(char a, char r, char g, char b)
62 {
63         return (unsigned int)((a << 24) | (r << 16) | (g << 8)  | b);
64 }
65
66 static void read_image16(char* pImg, int x1pos, int y1pos, int x2pos,
67                 int y2pos, unsigned short pixel)
68 {
69         unsigned short *pDst = (unsigned short *)pImg;
70         unsigned int offset_s;
71         int i, j;
72
73         for(i = y1pos; i < y2pos; i++) {
74                 for(j = x1pos; j < x2pos; j++) {
75                         offset_s = i * panel_width + j;
76                         *(pDst + offset_s) = pixel;
77                 }
78         }
79 }
80
81 static void read_image32(char* pImg, int x1pos, int y1pos, int x2pos,
82                 int y2pos, unsigned int pixel)
83 {
84         unsigned int *pDst = (unsigned int *)pImg;
85         unsigned int offset_s;
86         int i, j;
87
88         for (i = y1pos; i < y2pos; i++) {
89                 for (j = x1pos; j < x2pos; j++) {
90                         offset_s = i * panel_width + j;
91                         *(pDst+offset_s) = pixel;
92                 }
93         }
94 }
95 #endif
96
97 /* LCD Panel data */
98 vidinfo_t panel_info;
99
100 static void s5pc_lcd_init_mem(void *lcdbase, vidinfo_t *vid)
101 {
102         unsigned long palette_size, palette_mem_size;
103         unsigned int fb_size;
104
105         fb_size = vid->vl_row * vid->vl_col * (vid->vl_bpix >> 3);
106
107         lcd_base = lcdbase;
108
109         palette_size = NBITS(vid->vl_bpix) == 8 ? 256 : 16;
110         palette_mem_size = palette_size * sizeof(u32);
111
112         s5pc_fimd_lcd_init_mem((unsigned long)lcd_base, (unsigned long)fb_size, palette_size);
113
114         udebug("fb_size=%d, screen_base=%x, palette_size=%d, palettle_mem_size=%d\n",
115                         fb_size, (unsigned int)lcd_base, (int)palette_size, (int)palette_mem_size);
116 }
117
118 static void s5pc_lcd_init(vidinfo_t *vid)
119 {
120         s5pc_fimd_lcd_init(vid);
121 }
122
123 #ifdef USE_LCD_TEST
124 static void lcd_test(unsigned int width, unsigned int height)
125 {
126         unsigned int height_level = height / 3;
127
128         /* red */
129         read_image32((char *)lcd_base, 0, 0, width, height_level,
130                         makepixel8888(0, 255, 0, 0));
131         /* green */
132         read_image32((char *)lcd_base, 0, height_level, width,
133                 height_level * 2, makepixel8888(0, 0, 255, 0));
134         /* blue */
135         read_image32((char *)lcd_base, 0, height_level * 2, width, height,
136                         makepixel8888(0, 0, 0, 255));
137 }
138 #endif
139
140 int conv_rgb565_to_rgb888(unsigned short rgb565, unsigned int sw)
141 {
142         char red, green, blue;
143         unsigned int threshold = 150;
144
145         red = (rgb565 & 0xF800) >> 11;
146         green = (rgb565 & 0x7E0) >> 5;
147         blue = (rgb565 & 0x1F);
148
149         red = red << 3;
150         green = green << 2;
151         blue = blue << 3;
152
153         /* correct error pixels of samsung logo. */
154         if (sw) {
155                 if (red > threshold)
156                         red = 255;
157                 if (green > threshold)
158                         green = 255;
159                 if (blue > threshold)
160                         blue = 255;
161         }
162
163         return (red << 16 | green << 8 | blue);
164 }
165
166 void draw_bitmap(void *lcdbase, int x, int y, int w, int h, unsigned long *bmp)
167 {
168         int i, j;
169         short k = 0;
170         unsigned long *fb = (unsigned  long*)lcdbase;
171
172         for (j = y; j < (y + h); j++) {
173                 for (i = x; i < (x + w); i++)
174                         *(fb + (j * panel_width) + i) = *(bmp + k++);
175         }
176 }
177
178 void _draw_samsung_logo(void *lcdbase, int x, int y, int w, int h, unsigned short *bmp)
179 {
180         int i, j, error_range = 40;
181         short k = 0;
182         unsigned int pixel;
183         unsigned long *fb = (unsigned  long*)lcdbase;
184
185         for (j = y; j < (y + h); j++) {
186                 for (i = x; i < (x + w); i++) {
187                         pixel = (*(bmp + k++));
188
189                         /* 40 lines under samsung logo image are error range. */
190                         if (j > h + y - error_range)
191                                 *(fb + (j * panel_width) + i) =
192                                         conv_rgb565_to_rgb888(pixel, 1);
193                         else
194                                 *(fb + (j * panel_width) + i) =
195                                         conv_rgb565_to_rgb888(pixel, 0);
196                 }
197         }
198 }
199
200 static void draw_samsung_logo(void* lcdbase)
201 {
202         int x, y;
203         unsigned int in_len, width, height;
204         unsigned long out_len;
205         void *dst = NULL;
206         width = 298;
207         height = 78;
208         x = ((panel_width - width) >> 1);
209         y = ((panel_height - height) >> 1) - 5;
210
211         in_len = width * height * 4;
212         dst = malloc(in_len);
213         if (dst == NULL) {
214                 printf("Error: malloc in gunzip failed!\n");
215                 return;
216         }
217         if (gunzip(dst, in_len, (uchar *)logo, &out_len) != 0) {
218                 free(dst);
219                 return;
220         }
221         if (out_len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
222                 printf("Image could be truncated"
223                                 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
224         _draw_samsung_logo(lcdbase, x, y, width, height, (unsigned short *) dst);
225         free(dst);
226 }
227
228 static void lcd_panel_on(vidinfo_t *vid)
229 {
230         udelay(vid->init_delay);
231
232         if (vid->cfg_gpio)
233                 vid->cfg_gpio();
234
235         if (vid->lcd_power_on)
236                 vid->lcd_power_on(1);
237
238         udelay(vid->power_on_delay);
239
240         if (vid->reset_lcd)
241                 vid->reset_lcd();
242
243         udelay(vid->reset_delay);
244
245         if (vid->backlight_on)
246                 vid->backlight_on(1);
247
248
249         if (vid->cfg_ldo)
250                 vid->cfg_ldo();
251
252
253         if (vid->enable_ldo)
254                 vid->enable_ldo(1);
255
256 }
257
258 /* extern void init_onenand_ext2(void); */
259 extern void init_panel_info(vidinfo_t *vid);
260 extern int s5p_no_lcd_support(void);
261
262 void lcd_ctrl_init(void *lcdbase)
263 {
264         char *option;
265
266         if (s5p_no_lcd_support())
267                 return;
268
269         s5pc_lcd_init_mem(lcdbase, &panel_info);
270
271         /* initialize parameters which is specific to panel. */
272         init_panel_info(&panel_info);
273
274         panel_width = panel_info.vl_width;
275         panel_height = panel_info.vl_height;
276
277         option = getenv("lcd");
278
279         /*
280         if (strcmp(option, "test") == 0) {
281                 memset(lcdbase, 0, PANEL_WIDTH*PANEL_HEIGHT*S5P_LCD_BPP >> 3);
282 #ifdef USE_LCD_TEST
283                 lcd_test(panel_width, panel_height);
284 #endif
285         } else if (strcmp(option, "image") == 0)
286                 memcpy(lcdbase, LOGO_RGB24, PANEL_WIDTH*PANEL_HEIGHT*S5P_LCD_BPP >> 3);
287         else {
288                 memset(lcdbase, 0, PANEL_WIDTH*PANEL_HEIGHT*S5P_LCD_BPP >> 3);
289                 draw_samsung_logo(lcdbase);
290         }
291         */
292
293         memset(lcdbase, 0, panel_width * panel_height * (32 >> 3));
294         draw_samsung_logo(lcdbase);
295
296         s5pc_lcd_init(&panel_info);
297
298         /* font test */
299         /*
300         init_font();
301         set_font_color(FONT_WHITE);
302         fb_printf("Test\n");
303         exit_font();
304         */
305
306         /* init_onenand_ext2(); */
307 }
308
309
310 void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blud)
311 {
312         return;
313 }
314
315 void lcd_enable(void)
316 {
317         lcd_panel_on(&panel_info);
318 }
319
320 ulong calc_fbsize(void)
321 {
322         return s5pc_fimd_calc_fbsize();
323 }
324
325 void s5pc1xxfb_test(void *lcdbase)
326 {
327         lcd_ctrl_init(lcdbase);
328         lcd_enable();
329 }