summaryrefslogtreecommitdiff
path: root/firmware/target/arm/imx233/sony-nwz/lcd-nwze370.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/imx233/sony-nwz/lcd-nwze370.c')
-rw-r--r--firmware/target/arm/imx233/sony-nwz/lcd-nwze370.c219
1 files changed, 219 insertions, 0 deletions
diff --git a/firmware/target/arm/imx233/sony-nwz/lcd-nwze370.c b/firmware/target/arm/imx233/sony-nwz/lcd-nwze370.c
new file mode 100644
index 0000000000..999f4ee525
--- /dev/null
+++ b/firmware/target/arm/imx233/sony-nwz/lcd-nwze370.c
@@ -0,0 +1,219 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (c) 2013 by Amaury Pouly
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
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 "logf.h"
31
32#ifdef HAVE_LCD_ENABLE
33static bool lcd_on;
34#endif
35
36static void lcd_write_reg(uint8_t reg, void *data, int data_len)
37{
38 imx233_lcdif_set_data_swizzle(0);
39 imx233_lcdif_set_word_length(8);
40 imx233_lcdif_pio_send(false, 1, &reg);
41 imx233_lcdif_pio_send(true, data_len, data);
42}
43
44static void lcd_init_seq(void)
45{
46 // seems compatible with ILI9163
47 lcd_write_reg(1, NULL, 0); // software reset
48 lcd_write_reg(0x11, NULL, 0); // sleep out
49 mdelay(150);
50 /* format: (<cmd> <data size> <data0> <dataN>)+ */
51 static uint8_t init_seq[] =
52 {
53 /* cmd sz data... */
54 0x26, 1, 4, /* Gamma Set */
55 0xb1, 2, 9, 0xd, /* Frame Rate */
56 0xc0, 2, 8, 0, /* Power Control 1 */
57 0xc1, 1, 5, /* Power Control 2 */
58 0xc5, 2, 0x31, 0x40, /* VCOM Control 1 */
59 0xc7, 1, 0xc8, /* VCOM Offset Control */
60 0xec, 1, 0xc, /* Unknown */
61 0x3a, 1, 5, /* Interface Pixel Format */
62 0x2a, 4, 0, 0, 0, 0x7f, /* Column Address */
63 0x2b, 4, 0, 0, 0, 0x9f, /* Page Address Set */
64 0x35, 1, 0, /* Tear Effect Line On */
65 0x36, 1, 0xc8, /* Memory access Control */
66 0xb4, 1, 0, /* Display Inversion */
67 0xb7, 1, 0, /* Source Driver Direction Control */
68 0xb8, 1, 0, /* Gate Driver Direction Control */
69 0xf2, 1, 1, /* Gamma Adjustment */
70 0xe0, 15, 0x3f, 0x20, 0x1d, 0x2d, 0x26, 0x0c, 0x4b, 0xb7,
71 0x39, 0x17, 0x1d, 0x16, 0x16, 0x10, 0x00, /* Positive Gamma */
72 0xe1, 15, 0x00, 0x1f, 0x21, 0x12, 0x18, 0x13, 0x34, 0x48,
73 0x46, 0x08, 0x21, 0x29, 0x28, 0x2f, 0x3f, /* Negative Gamma */
74 0x29, 0, /* Display On */
75 };
76
77 for(unsigned i = 0; i < sizeof(init_seq); )
78 {
79 lcd_write_reg(init_seq[i], &init_seq[i + 2], init_seq[i + 1]);
80 i += 2 + init_seq[i + 1];
81 }
82}
83
84void lcd_init_device(void)
85{
86 /* the LCD seems to work at 24Mhz, so use the xtal clock with no divider */
87 imx233_clkctrl_enable(CLK_PIX, false);
88 imx233_clkctrl_set_div(CLK_PIX, 1);
89 imx233_clkctrl_set_bypass(CLK_PIX, true); /* use XTAL */
90 imx233_clkctrl_enable(CLK_PIX, true);
91 imx233_lcdif_init();
92 imx233_lcdif_set_lcd_databus_width(8);
93 imx233_lcdif_set_timings(2, 2, 2, 2);
94 imx233_lcdif_enable_underflow_recover(true);
95 imx233_lcdif_setup_system_pins(8);
96 imx233_lcdif_set_byte_packing_format(0xf); /* two pixels per 32-bit word */
97
98 // reset device
99 imx233_lcdif_reset_lcd(true);
100 mdelay(10);
101 imx233_lcdif_reset_lcd(false);
102 mdelay(10);
103 imx233_lcdif_reset_lcd(true);
104 mdelay(150);
105
106 lcd_init_seq();
107#ifdef HAVE_LCD_ENABLE
108 lcd_on = true;
109#endif
110}
111
112#ifdef HAVE_LCD_ENABLE
113bool lcd_active(void)
114{
115 return lcd_on;
116}
117
118static void lcd_enable_seq(bool enable)
119{
120 if(enable)
121 {
122 lcd_write_reg(0x11, NULL, 0);
123 lcd_write_reg(0x29, NULL, 0);
124 }
125 else
126 {
127 lcd_write_reg(0x28, NULL, 0);
128 lcd_write_reg(0x10, NULL, 0);
129 }
130}
131
132void lcd_enable(bool enable)
133{
134 if(lcd_on == enable)
135 return;
136
137 lcd_on = enable;
138
139 if(enable)
140 imx233_lcdif_enable(true);
141 lcd_enable_seq(enable);
142 if(!enable)
143 imx233_lcdif_enable(false);
144 else
145 send_event(LCD_EVENT_ACTIVATION, NULL);
146}
147#endif
148
149void lcd_update(void)
150{
151 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
152}
153
154void lcd_update_rect(int x, int y, int w, int h)
155{
156#ifdef HAVE_LCD_ENABLE
157 if(!lcd_on)
158 return;
159#endif
160 /* make sure the rectangle is included in the screen */
161 x = MIN(x, LCD_WIDTH);
162 y = MIN(y, LCD_HEIGHT);
163 w = MIN(w, LCD_WIDTH - x);
164 h = MIN(h, LCD_HEIGHT - y);
165
166 imx233_lcdif_wait_ready();
167 uint32_t window = x << 8 | (x + w - 1) << 24;
168 lcd_write_reg(0x2a, &window, sizeof(window));
169 window = y << 8 | (y + h - 1) << 24;
170 lcd_write_reg(0x2b, &window, sizeof(window));
171 lcd_write_reg(0x2c, NULL, 0);
172
173 imx233_lcdif_wait_ready();
174 imx233_lcdif_set_data_swizzle(3);
175 imx233_lcdif_set_word_length(8);
176 /* there are two cases here:
177 * - either width = LCD_WIDTH and we can directly memcopy a part of lcd_framebuffer to FRAME
178 * and send it
179 * - either width != LCD_WIDTH and we have to build a contiguous copy of the rectangular area
180 * into FRAME before sending it (which is slower and doesn't use the hardware)
181 * In all cases, FRAME just acts as a temporary buffer.
182 * NOTE It's more interesting to do a copy to FRAME in all cases since in system mode
183 * the clock runs at 24MHz which provides barely 10MB/s bandwidth compared to >100MB/s
184 * for memcopy operations
185 */
186 if(w == LCD_WIDTH)
187 {
188 memcpy((void *)FRAME, FBADDR(x,y), w * h * sizeof(fb_data));
189 }
190 else
191 {
192 for(int i = 0; i < h; i++)
193 memcpy((fb_data *)FRAME + i * w, FBADDR(x,y + i), w * sizeof(fb_data));
194 }
195 /* WARNING The LCDIF has a limitation on the vertical count ! In 16-bit packed mode
196 * (which we used, ie 16-bit per pixel, 2 pixels per 32-bit words), the v_count
197 * field must be a multiple of 2. Furthermore, it seems the lcd controller doesn't
198 * really like when both w and h are even, probably because the writes to the GRAM
199 * are done on several words and the controller requires dummy writes.
200 * The workaround is to always make sure that we send a number of pixels which is
201 * a multiple of 4 so that both the lcdif and the controller are happy. If any
202 * of w or h is odd, we will send a copy of the first pixels as dummy writes. We will
203 * send at most 3 bytes. We then send (w * h + 3) / 4 x 4 bytes.
204 */
205 if(w % 2 == 1 || h % 2 == 1)
206 {
207 /* copy three pixel after the last one */
208 for(int i = 0; i < 3; i++)
209 *((fb_data *)FRAME + w * h + i) = *((fb_data *)FRAME + i);
210 /* WARNING we need to update w and h to reflect the pixel count BUT it
211 * has no relation to w * h (it can even be 2 * prime). Hopefully, w <= 240 and
212 * h <= 320 so w * h <= 76800 and (w * h + 3) / 4 <= 38400 which fits into
213 * a 16-bit integer (horizontal count). */
214 h = (w * h + 3) / 4;
215 w = 4;
216 }
217 imx233_lcdif_dma_send((void *)FRAME_PHYS_ADDR, w * 2, h);
218}
219