fuze+: correctly handle settings (flip and invert) accross enable
[maemo-rb.git] / firmware / target / arm / imx233 / sansa-fuzeplus / lcd-fuzeplus.c
blob8f0efdf6718e1923a46fb9783dd0bdc3f1fb75fb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2011 by Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <sys/types.h> /* off_t */
22 #include <string.h>
23 #include "cpu.h"
24 #include "system.h"
25 #include "backlight-target.h"
26 #include "lcd.h"
27 #include "lcdif-imx233.h"
28 #include "clkctrl-imx233.h"
29 #include "pinctrl-imx233.h"
30 #include "dcp-imx233.h"
31 #include "logf.h"
32 #ifndef BOOTLOADER
33 #include "button.h"
34 #include "font.h"
35 #include "action.h"
36 #endif
38 #ifdef HAVE_LCD_ENABLE
39 static bool lcd_on;
40 #endif
41 static unsigned lcd_yuv_options = 0;
42 static int lcd_dcp_channel = -1;
43 #ifdef HAVE_LCD_INVERT
44 static int lcd_reg_0x61_val = 1; /* used to invert display */
45 #endif
46 #ifdef HAVE_LCD_FLIP
47 static int lcd_reg_3_val = 0x1030; /* controls to flip display */
48 #endif
50 static enum lcd_kind_t
52 LCD_KIND_7783 = 0x7783,
53 LCD_KIND_9325 = 0x9325,
54 LCD_KIND_OTHER = 0,
55 } lcd_kind = LCD_KIND_OTHER;
57 static void setup_parameters(void)
59 imx233_lcdif_reset();
60 imx233_lcdif_set_lcd_databus_width(HW_LCDIF_CTRL__LCD_DATABUS_WIDTH_18_BIT);
61 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_18_BIT);
62 imx233_lcdif_set_timings(1, 2, 2, 2);
63 imx233_lcdif_enable_underflow_recover(true);
66 static void setup_lcd_pins(bool use_lcdif)
68 /* WARNING
69 * the B1P22 and B1P24 pins are used by the tuner i2c! Do NOT drive
70 * them as lcd_dotclk and lcd_hsync or it will break the tuner! */
71 imx233_pinctrl_acquire_pin(1, 18, "lcd reset");
72 imx233_pinctrl_acquire_pin(1, 19, "lcd rs");
73 imx233_pinctrl_acquire_pin(1, 20, "lcd wr");
74 imx233_pinctrl_acquire_pin(1, 21, "lcd cs");
75 imx233_pinctrl_acquire_pin(1, 23, "lcd enable");
76 imx233_pinctrl_acquire_pin(1, 25, "lcd vsync");
77 imx233_pinctrl_acquire_pin_mask(1, 0x3ffff, "lcd data");
78 if(use_lcdif)
80 imx233_set_pin_function(1, 25, PINCTRL_FUNCTION_GPIO); /* lcd_vsync */
81 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_MAIN); /* lcd_cs */
82 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
83 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_MAIN); /* lcd_reset */
84 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
85 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_MAIN); /* lcd_d16 */
86 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_MAIN); /* lcd_d17 */
87 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_MAIN); /* lcd_wr */
88 __REG_CLR(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} */
90 else
92 __REG_SET(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} */
93 imx233_enable_gpio_output_mask(1, 0x2bfffff, false); /* lcd_{d{0-17},reset,rs,wr,cs,enable,vsync} */
94 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_GPIO); /* lcd_d16 */
95 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_GPIO); /* lcd_d17 */
96 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
97 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_GPIO); /* lcd_wr */
98 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_GPIO); /* lcd_cs */
99 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
100 imx233_set_pin_function(1, 25, PINCTRL_FUNCTION_GPIO); /* lcd_vsync */
104 static void setup_lcd_pins_i80(bool i80)
106 if(i80)
108 imx233_set_pin_drive_strength(1, 19, PINCTRL_DRIVE_12mA); /* lcd_rs */
109 imx233_set_pin_drive_strength(1, 20, PINCTRL_DRIVE_12mA); /* lcd_wr */
110 imx233_set_pin_drive_strength(1, 21, PINCTRL_DRIVE_12mA); /* lcd_cs */
111 imx233_set_pin_drive_strength(1, 23, PINCTRL_DRIVE_12mA); /* lcd_enable */
112 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
113 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_GPIO); /* lcd_wr */
114 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_GPIO); /* lcd_cs */
115 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
116 /* lcd_{rs,wr,cs,enable} */
117 imx233_enable_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
118 imx233_set_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
120 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
121 __REG_SET(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} as GPIO */
122 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_GPIO); /* lcd_d16 */
123 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_GPIO); /* lcd_d17 */
124 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_GPIO); /* lcd_reset */
125 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
127 else
129 imx233_set_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
130 imx233_set_pin_drive_strength(1, 19, PINCTRL_DRIVE_4mA); /* lcd_rs */
131 imx233_set_pin_drive_strength(1, 20, PINCTRL_DRIVE_4mA); /* lcd_wr */
132 imx233_set_pin_drive_strength(1, 21, PINCTRL_DRIVE_4mA); /* lcd_cs */
133 imx233_set_pin_drive_strength(1, 23, PINCTRL_DRIVE_4mA); /* lcd_enable */
134 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
135 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_MAIN); /* lcd_wr */
136 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_MAIN); /* lcd_cs */
137 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
138 __REG_CLR(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} as lcd_d{0-15} */
139 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_MAIN); /* lcd_d16 */
140 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_MAIN); /* lcd_d17 */
141 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_MAIN); /* lcd_reset */
142 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
146 static void common_lcd_enable(bool enable)
148 imx233_lcdif_enable(enable);
149 setup_lcd_pins(enable); /* use GPIO pins when disable */
152 static void setup_lcdif(void)
154 setup_parameters();
155 common_lcd_enable(true);
156 imx233_lcdif_enable_bus_master(true);
159 static inline uint32_t encode_16_to_18(uint32_t a)
161 return ((a & 0xff) << 1) | (((a >> 8) & 0xff) << 10);
164 static inline uint32_t decode_18_to_16(uint32_t a)
166 return ((a >> 1) & 0xff) | ((a >> 2) & 0xff00);
169 static void setup_lcdif_clock(void)
171 /* the LCD seems to work at 24Mhz, so use the xtal clock with no divider */
172 imx233_clkctrl_enable_clock(CLK_PIX, false);
173 imx233_clkctrl_set_clock_divisor(CLK_PIX, 1);
174 imx233_clkctrl_set_bypass_pll(CLK_PIX, true); /* use XTAL */
175 imx233_clkctrl_enable_clock(CLK_PIX, true);
178 static uint32_t i80_read_register(uint32_t data_out)
180 imx233_lcdif_wait_ready();
181 /* lcd_enable is mapped to the RD pin of the controller */
182 imx233_set_gpio_output(1, 21, true); /* lcd_cs */
183 imx233_set_gpio_output(1, 19, true); /* lcd_rs */
184 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
185 imx233_set_gpio_output(1, 20, true); /* lcd_wr */
186 imx233_enable_gpio_output_mask(1, 0x3ffff, true); /* lcd_d{0-17} */
187 udelay(2);
188 imx233_set_gpio_output(1, 19, false); /* lcd_rs */
189 udelay(1);
190 imx233_set_gpio_output(1, 21, false); /* lcd_cs */
191 udelay(1);
192 imx233_set_gpio_output(1, 20, false); /* lcd_wr */
193 udelay(1);
194 imx233_set_gpio_output_mask(1, data_out & 0x3ffff, true); /* lcd_d{0-17} */
195 udelay(1);
196 imx233_set_gpio_output(1, 20, true); /* lcd_wr */
197 udelay(3);
198 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
199 udelay(2);
200 imx233_set_gpio_output(1, 23, false); /* lcd_enable */
201 udelay(1);
202 imx233_set_gpio_output(1, 19, true); /* lcd_rs */
203 udelay(1);
204 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
205 udelay(3);
206 imx233_set_gpio_output(1, 23, false); /* lcd_enable */
207 udelay(2);
208 uint32_t data_in = imx233_get_gpio_input_mask(1, 0x3ffff); /* lcd_d{0-17} */
209 udelay(1);
210 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
211 udelay(1);
212 imx233_set_gpio_output(1, 21, true); /* lcd_cs */
213 udelay(1);
214 return data_in;
217 static void lcd_write_reg(uint32_t reg, uint32_t data)
219 uint32_t old_reg = reg;
220 imx233_lcdif_wait_ready();
221 /* get back to 18-bit word length */
222 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_18_BIT);
223 reg = encode_16_to_18(reg);
224 data = encode_16_to_18(data);
226 imx233_lcdif_pio_send(false, 2, &reg);
227 if(old_reg != 0x22)
228 imx233_lcdif_pio_send(true, 2, &data);
231 static uint32_t lcd_read_reg(uint32_t reg)
233 setup_lcd_pins_i80(true);
234 uint32_t data_in = i80_read_register(encode_16_to_18(reg));
235 setup_lcd_pins_i80(false);
236 lcd_write_reg(0x22, 0);
237 return decode_18_to_16(data_in);
240 #define REG_MDELAY 0xffffffff
241 struct lcd_sequence_entry_t
243 uint32_t reg, data;
246 static void lcd_send_sequence(struct lcd_sequence_entry_t *seq, unsigned count)
248 for(;count-- > 0; seq++)
250 if(seq->reg == REG_MDELAY)
251 mdelay(seq->data);
252 else
253 lcd_write_reg(seq->reg, seq->data);
257 #define _begin_seq() static struct lcd_sequence_entry_t __seq[] = {
258 #define _mdelay(a) {REG_MDELAY, a},
259 #define _lcd_write_reg(a, b) {a, b},
260 #define _end_seq() }; lcd_send_sequence(__seq, sizeof(__seq) / sizeof(__seq[0]));
262 static void lcd_init_seq_7783(void)
264 _begin_seq()
265 _mdelay(200)
266 _lcd_write_reg(1, 0x100)
267 _lcd_write_reg(2, 0x700)
268 _lcd_write_reg(3, 0x1030)
269 _lcd_write_reg(7, 0x121)
270 _lcd_write_reg(8, 0x302)
271 _lcd_write_reg(9, 0x200)
272 _lcd_write_reg(0xa, 0)
273 _lcd_write_reg(0x10, 0x790)
274 _lcd_write_reg(0x11, 5)
275 _lcd_write_reg(0x12, 0)
276 _lcd_write_reg(0x13, 0)
277 _mdelay(100)
278 _lcd_write_reg(0x10, 0x12b0)
279 _mdelay(100)
280 _lcd_write_reg(0x11, 7)
281 _mdelay(100)
282 _lcd_write_reg(0x12, 0x89)
283 _lcd_write_reg(0x13, 0x1d00)
284 _lcd_write_reg(0x29, 0x2f)
285 _mdelay(50)
286 _lcd_write_reg(0x30, 0)
287 _lcd_write_reg(0x31, 0x505)
288 _lcd_write_reg(0x32, 0x205)
289 _lcd_write_reg(0x35, 0x206)
290 _lcd_write_reg(0x36, 0x408)
291 _lcd_write_reg(0x37, 0)
292 _lcd_write_reg(0x38, 0x504)
293 _lcd_write_reg(0x39, 0x206)
294 _lcd_write_reg(0x3c, 0x206)
295 _lcd_write_reg(0x3d, 0x408)
296 _lcd_write_reg(0x50, 0) /* left X ? */
297 _lcd_write_reg(0x51, 0xef) /* right X ? */
298 _lcd_write_reg(0x52, 0) /* top Y ? */
299 _lcd_write_reg(0x53, 0x13f) /* bottom Y ? */
300 _lcd_write_reg(0x20, 0) /* left X ? */
301 _lcd_write_reg(0x21, 0) /* top Y ? */
302 _lcd_write_reg(0x60, 0xa700)
303 _lcd_write_reg(0x61, 1)
304 _lcd_write_reg(0x90, 0x33)
305 _lcd_write_reg(0x2b, 0xa)
306 _lcd_write_reg(9, 0)
307 _lcd_write_reg(7, 0x133)
308 _mdelay(50)
309 _lcd_write_reg(0x22, 0)
310 _end_seq()
313 static void lcd_init_seq_9325(void)
315 _begin_seq()
316 _lcd_write_reg(0xe5, 0x78f0)
317 _lcd_write_reg(0xe3, 0x3008)
318 _lcd_write_reg(0xe7, 0x12)
319 _lcd_write_reg(0xef, 0x1231)
320 _lcd_write_reg(0, 1)
321 _lcd_write_reg(1, 0x100)
322 _lcd_write_reg(2, 0x700)
323 _lcd_write_reg(3, 0x1030)
324 _lcd_write_reg(4, 0)
325 _lcd_write_reg(8, 0x207)
326 _lcd_write_reg(9, 0)
327 _lcd_write_reg(0xa, 0)
328 _lcd_write_reg(0xc, 0)
329 _lcd_write_reg(0xd, 0)
330 _lcd_write_reg(0xf, 0)
331 _lcd_write_reg(0x10, 0)
332 _lcd_write_reg(0x11, 7)
333 _lcd_write_reg(0x12, 0)
334 _lcd_write_reg(0x13, 0)
335 _mdelay(20)
336 _lcd_write_reg(0x10, 0x1290)
337 _lcd_write_reg(0x11, 7)
338 _mdelay(50)
339 _lcd_write_reg(0x12, 0x19)
340 _mdelay(50)
341 _lcd_write_reg(0x13, 0x1700)
342 _lcd_write_reg(0x29, 0x14)
343 _mdelay(50)
344 _lcd_write_reg(0x20, 0)
345 _lcd_write_reg(0x21, 0)
346 _lcd_write_reg(0x30, 0x504)
347 _lcd_write_reg(0x31, 7)
348 _lcd_write_reg(0x32, 6)
349 _lcd_write_reg(0x35, 0x106)
350 _lcd_write_reg(0x36, 0x202)
351 _lcd_write_reg(0x37, 0x504)
352 _lcd_write_reg(0x38, 0x500)
353 _lcd_write_reg(0x39, 0x706)
354 _lcd_write_reg(0x3c, 0x204)
355 _lcd_write_reg(0x3d, 0x202)
356 _lcd_write_reg(0x50, 0)
357 _lcd_write_reg(0x51, 0xef)
358 _lcd_write_reg(0x52, 0)
359 _lcd_write_reg(0x53, 0x13f)
360 _lcd_write_reg(0x60, 0xa700)
361 _lcd_write_reg(0x61, 1)
362 _lcd_write_reg(0x6a, 0)
363 _lcd_write_reg(0x2b, 0xd)
364 _mdelay(50)
365 _lcd_write_reg(0x90, 0x11)
366 _lcd_write_reg(0x92, 0x600)
367 _lcd_write_reg(0x93, 3)
368 _lcd_write_reg(0x95, 0x110)
369 _lcd_write_reg(0x97, 0)
370 _lcd_write_reg(0x98, 0)
371 _lcd_write_reg(7, 0x173)
372 _lcd_write_reg(0x22, 0)
373 _end_seq()
376 static void lcd_sync_settings(void)
378 #ifdef HAVE_LCD_INVERT
379 lcd_write_reg(0x61, lcd_reg_0x61_val);
380 #endif
381 #ifdef HAVE_LCD_FLIP
382 lcd_write_reg(3, lcd_reg_3_val);
383 #endif
386 void lcd_init_device(void)
388 lcd_dcp_channel = imx233_dcp_acquire_channel(TIMEOUT_NOBLOCK);
389 if(lcd_dcp_channel < 0)
390 panicf("imx233_framebuffer_init: imx233_dcp_acquire_channel failed!");
391 setup_lcdif();
392 setup_lcdif_clock();
394 for(int i = 0; i < 10; i++)
396 lcd_kind = lcd_read_reg(0);
397 mdelay(5);
398 if(lcd_kind == LCD_KIND_7783 || lcd_kind == LCD_KIND_9325)
399 break;
401 // reset device
402 __REG_SET(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
403 mdelay(50);
404 __REG_CLR(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
405 mdelay(10);
406 __REG_SET(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
408 switch(lcd_kind)
410 case LCD_KIND_7783: lcd_init_seq_7783(); break;
411 case LCD_KIND_9325: lcd_init_seq_9325(); break;
412 default:
413 lcd_kind = LCD_KIND_7783;
414 lcd_init_seq_7783(); break;
417 lcd_sync_settings();
419 #ifdef HAVE_LCD_ENABLE
420 lcd_on = true;
421 #endif
424 #ifdef HAVE_LCD_ENABLE
425 bool lcd_active(void)
427 return lcd_on;
430 static void lcd_enable_7783(bool enable)
432 if(!enable)
434 _begin_seq()
435 _lcd_write_reg(7, 0x131)
436 _mdelay(50)
437 _lcd_write_reg(7, 0x20)
438 _mdelay(50)
439 _lcd_write_reg(0x10, 0x82)
440 _mdelay(50)
441 _end_seq()
443 else
445 _begin_seq()
446 _lcd_write_reg(0x11, 5)
447 _lcd_write_reg(0x10, 0x12b0)
448 _mdelay(50)
449 _lcd_write_reg(7, 0x11)
450 _mdelay(50)
451 _lcd_write_reg(0x12, 0x89)
452 _mdelay(50)
453 _lcd_write_reg(0x13, 0x1d00)
454 _mdelay(50)
455 _lcd_write_reg(0x29, 0x2f)
456 _mdelay(50)
457 _lcd_write_reg(0x2b, 0xa)
458 _lcd_write_reg(7, 0x133)
459 _mdelay(50)
460 _lcd_write_reg(0x22, 0)
461 _end_seq()
465 static void lcd_enable_9325(bool enable)
467 if(!enable)
469 _begin_seq()
470 _lcd_write_reg(7, 0x131)
471 _mdelay(10)
472 _lcd_write_reg(7, 0x130)
473 _mdelay(10)
474 _lcd_write_reg(7, 0)
475 _lcd_write_reg(0x10, 0x80)
476 _lcd_write_reg(0x11, 0)
477 _lcd_write_reg(0x12, 0)
478 _lcd_write_reg(0x13, 0)
479 _mdelay(200)
480 _lcd_write_reg(0x10, 0x82)
481 _end_seq()
483 else
485 _begin_seq()
486 _lcd_write_reg(0x10, 0x80)
487 _lcd_write_reg(0x11, 0)
488 _lcd_write_reg(0x12, 0)
489 _lcd_write_reg(0x13, 0)
490 _lcd_write_reg(7, 1)
491 _mdelay(200)
492 _lcd_write_reg(0x10, 0x1290)
493 _lcd_write_reg(0x11, 7)
494 _mdelay(50)
495 _lcd_write_reg(0x12, 0x19)
496 _mdelay(50)
497 _lcd_write_reg(0x13, 0x1700)
498 _lcd_write_reg(0x29, 0x10)
499 _mdelay(50)
500 _lcd_write_reg(7, 0x133)
501 _lcd_write_reg(0x22, 0)
502 _end_seq()
506 void lcd_enable(bool enable)
508 if(lcd_on == enable)
509 return;
511 lcd_on = enable;
513 if(enable)
514 common_lcd_enable(true);
515 switch(lcd_kind)
517 case LCD_KIND_7783: lcd_enable_7783(enable); break;
518 case LCD_KIND_9325: lcd_enable_9325(enable); break;
519 default: lcd_enable_7783(enable); break;
521 if(!enable)
522 common_lcd_enable(false);
523 else
525 lcd_sync_settings();
528 #endif
530 #ifdef HAVE_LCD_INVERT
531 void lcd_set_invert_display(bool yesno)
533 lcd_reg_0x61_val = yesno ? 0 : 1;
534 #ifdef HAVE_LCD_ENABLE
535 if(!lcd_on)
536 return;
537 #endif
538 /* same for both kinds */
539 lcd_write_reg(0x61, lcd_reg_0x61_val);
541 #endif
543 #ifdef HAVE_LCD_FLIP
544 void lcd_set_flip(bool yesno)
546 lcd_reg_3_val = yesno ? 0x1000 : 0x1030;
547 #ifdef HAVE_LCD_ENABLE
548 if(!lcd_on)
549 return;
550 #endif
551 /* same for both kinds */
552 lcd_write_reg(3, lcd_reg_3_val);
554 #endif
556 void lcd_update(void)
558 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
561 void lcd_update_rect(int x, int y, int w, int h)
563 #ifdef HAVE_LCD_ENABLE
564 if(!lcd_on)
565 return;
566 #endif
567 /* make sure the rectangle is included in the screen */
568 x = MIN(x, LCD_WIDTH);
569 y = MIN(y, LCD_HEIGHT);
570 w = MIN(w, LCD_WIDTH - x);
571 h = MIN(h, LCD_HEIGHT - y);
573 imx233_lcdif_wait_ready();
574 lcd_write_reg(0x50, x);
575 lcd_write_reg(0x51, x + w - 1);
576 lcd_write_reg(0x52, y);
577 lcd_write_reg(0x53, y + h - 1);
578 lcd_write_reg(0x20, x);
579 lcd_write_reg(0x21, y);
580 lcd_write_reg(0x22, 0);
581 imx233_lcdif_wait_ready();
582 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_16_BIT);
583 imx233_lcdif_set_byte_packing_format(0xf); /* two pixels per 32-bit word */
584 imx233_lcdif_set_data_format(false, false, false); /* RGB565, don't care, don't care */
585 /* there are two cases here:
586 * - either width = LCD_WIDTH and we can directly memcopy a part of lcd_framebuffer to FRAME
587 * and send it
588 * - either width != LCD_WIDTH and we have to build a contiguous copy of the rectangular area
589 * into FRAME before sending it (which is slower and doesn't use the hardware)
590 * In all cases, FRAME just acts as a temporary buffer.
591 * NOTE It's more interesting to do a copy to FRAME in all cases since in system mode
592 * the clock runs at 24MHz which provides barely 10MB/s bandwidth compared to >100MB/s
593 * for memcopy operations
595 if(w == LCD_WIDTH)
597 memcpy((void *)FRAME, FBADDR(x,y), w * h * sizeof(fb_data));
599 else
601 for(int i = 0; i < h; i++)
602 memcpy((fb_data *)FRAME + i * w, FBADDR(x,y + i), w * sizeof(fb_data));
604 /* WARNING The LCDIF has a limitation on the vertical count ! In 16-bit packed mode
605 * (which we used, ie 16-bit per pixel, 2 pixels per 32-bit words), the v_count
606 * field must be a multiple of 2. Furthermore, it seems the lcd controller doesn't
607 * really like when both w and h are even, probably because the writes to the GRAM
608 * are done on several words and the controller requires dummy writes.
609 * The workaround is to always make sure that we send a number of pixels which is
610 * a multiple of 4 so that both the lcdif and the controller are happy. If any
611 * of w or h is odd, we will send a copy of the first pixels as dummy writes. We will
612 * send at most 3 bytes. We then send (w * h + 3) / 4 x 4 bytes.
614 if(w % 2 == 1 || h % 2 == 1)
616 /* copy three pixel after the last one */
617 for(int i = 0; i < 3; i++)
618 *((fb_data *)FRAME + w * h + i) = *((fb_data *)FRAME + i);
619 /* WARNING we need to update w and h to reflect the pixel count BUT it
620 * has no relation to w * h (it can even be 2 * prime). Hopefully, w <= 240 and
621 * h <= 320 so w * h <= 76800 and (w * h + 3) / 4 <= 38400 which fits into
622 * a 16-bit integer (horizontal count). */
623 h = (w * h + 3) / 4;
624 w = 4;
626 imx233_lcdif_dma_send((void *)FRAME_PHYS_ADDR, w, h);
629 void lcd_yuv_set_options(unsigned options)
631 lcd_yuv_options = options;
634 #define YFAC (74)
635 #define RVFAC (101)
636 #define GUFAC (-24)
637 #define GVFAC (-51)
638 #define BUFAC (128)
640 static inline int clamp(int val, int min, int max)
642 if (val < min)
643 val = min;
644 else if (val > max)
645 val = max;
646 return val;
649 void lcd_blit_yuv(unsigned char * const src[3],
650 int src_x, int src_y, int stride,
651 int x, int y, int width, int height)
653 const unsigned char *ysrc, *usrc, *vsrc;
654 int linecounter;
655 fb_data *dst, *row_end;
656 long z;
658 /* width and height must be >= 2 and an even number */
659 width &= ~1;
660 linecounter = height >> 1;
662 #if LCD_WIDTH >= LCD_HEIGHT
663 dst = FBADDR(x,y);
664 row_end = dst + width;
665 #else
666 dst = FBADDR(LCD_WIDTH - y - 1,x);
667 row_end = dst + LCD_WIDTH * width;
668 #endif
670 z = stride * src_y;
671 ysrc = src[0] + z + src_x;
672 usrc = src[1] + (z >> 2) + (src_x >> 1);
673 vsrc = src[2] + (usrc - src[1]);
675 /* stride => amount to jump from end of last row to start of next */
676 stride -= width;
678 /* upsampling, YUV->RGB conversion and reduction to RGB565 in one go */
684 int y, cb, cr, rv, guv, bu, r, g, b;
686 y = YFAC*(*ysrc++ - 16);
687 cb = *usrc++ - 128;
688 cr = *vsrc++ - 128;
690 rv = RVFAC*cr;
691 guv = GUFAC*cb + GVFAC*cr;
692 bu = BUFAC*cb;
694 r = y + rv;
695 g = y + guv;
696 b = y + bu;
698 if ((unsigned)(r | g | b) > 64*256-1)
700 r = clamp(r, 0, 64*256-1);
701 g = clamp(g, 0, 64*256-1);
702 b = clamp(b, 0, 64*256-1);
705 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
707 #if LCD_WIDTH >= LCD_HEIGHT
708 dst++;
709 #else
710 dst += LCD_WIDTH;
711 #endif
713 y = YFAC*(*ysrc++ - 16);
714 r = y + rv;
715 g = y + guv;
716 b = y + bu;
718 if ((unsigned)(r | g | b) > 64*256-1)
720 r = clamp(r, 0, 64*256-1);
721 g = clamp(g, 0, 64*256-1);
722 b = clamp(b, 0, 64*256-1);
725 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
727 #if LCD_WIDTH >= LCD_HEIGHT
728 dst++;
729 #else
730 dst += LCD_WIDTH;
731 #endif
733 while (dst < row_end);
735 ysrc += stride;
736 usrc -= width >> 1;
737 vsrc -= width >> 1;
739 #if LCD_WIDTH >= LCD_HEIGHT
740 row_end += LCD_WIDTH;
741 dst += LCD_WIDTH - width;
742 #else
743 row_end -= 1;
744 dst -= LCD_WIDTH*width + 1;
745 #endif
749 int y, cb, cr, rv, guv, bu, r, g, b;
751 y = YFAC*(*ysrc++ - 16);
752 cb = *usrc++ - 128;
753 cr = *vsrc++ - 128;
755 rv = RVFAC*cr;
756 guv = GUFAC*cb + GVFAC*cr;
757 bu = BUFAC*cb;
759 r = y + rv;
760 g = y + guv;
761 b = y + bu;
763 if ((unsigned)(r | g | b) > 64*256-1)
765 r = clamp(r, 0, 64*256-1);
766 g = clamp(g, 0, 64*256-1);
767 b = clamp(b, 0, 64*256-1);
770 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
772 #if LCD_WIDTH >= LCD_HEIGHT
773 dst++;
774 #else
775 dst += LCD_WIDTH;
776 #endif
778 y = YFAC*(*ysrc++ - 16);
779 r = y + rv;
780 g = y + guv;
781 b = y + bu;
783 if ((unsigned)(r | g | b) > 64*256-1)
785 r = clamp(r, 0, 64*256-1);
786 g = clamp(g, 0, 64*256-1);
787 b = clamp(b, 0, 64*256-1);
790 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
792 #if LCD_WIDTH >= LCD_HEIGHT
793 dst++;
794 #else
795 dst += LCD_WIDTH;
796 #endif
798 while (dst < row_end);
800 ysrc += stride;
801 usrc += stride >> 1;
802 vsrc += stride >> 1;
804 #if LCD_WIDTH >= LCD_HEIGHT
805 row_end += LCD_WIDTH;
806 dst += LCD_WIDTH - width;
807 #else
808 row_end -= 1;
809 dst -= LCD_WIDTH*width + 1;
810 #endif
812 while (--linecounter > 0);
814 #if LCD_WIDTH >= LCD_HEIGHT
815 lcd_update_rect(x, y, width, height);
816 #else
817 lcd_update_rect(LCD_WIDTH - y - height, x, height, width);
818 #endif
821 #ifndef BOOTLOADER
822 bool lcd_debug_screen(void)
824 lcd_setfont(FONT_SYSFIXED);
826 while(1)
828 int button = get_action(CONTEXT_STD, HZ / 10);
829 switch(button)
831 case ACTION_STD_NEXT:
832 case ACTION_STD_PREV:
833 case ACTION_STD_OK:
834 case ACTION_STD_MENU:
835 lcd_setfont(FONT_UI);
836 return true;
837 case ACTION_STD_CANCEL:
838 lcd_setfont(FONT_UI);
839 return false;
842 lcd_clear_display();
843 lcd_putsf(0, 0, "lcd kind: %s",
844 lcd_kind == LCD_KIND_7783 ? "st7783" :
845 lcd_kind == LCD_KIND_9325 ? "ili9325" : "unknown");
846 lcd_update();
847 yield();
850 return true;
852 #endif