Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 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 "dma-imx233.h" |
| 31 | #include "regs/regs-uartdbg.h" |
| 32 | #include "logf.h" |
| 33 | #ifndef BOOTLOADER |
| 34 | #include "button.h" |
| 35 | #include "font.h" |
| 36 | #include "action.h" |
| 37 | #endif |
| 38 | |
| 39 | #ifdef HAVE_LCD_ENABLE |
| 40 | static bool lcd_on; |
| 41 | #endif |
| 42 | |
| 43 | /** |
| 44 | * DMA |
| 45 | */ |
| 46 | |
| 47 | /* Used for DMA */ |
| 48 | struct lcdif_dma_command_t |
| 49 | { |
| 50 | struct apb_dma_command_t dma; |
| 51 | uint32_t pad; |
| 52 | } __attribute__((packed)) CACHEALIGN_ATTR; |
| 53 | |
| 54 | __ENSURE_STRUCT_CACHE_FRIENDLY(struct lcdif_dma_command_t) |
| 55 | |
| 56 | #define NR_CMDS ((IMX233_FRAMEBUFFER_SIZE + IMX233_MAX_SINGLE_DMA_XFER_SIZE - 1) / IMX233_MAX_SINGLE_DMA_XFER_SIZE) |
| 57 | |
| 58 | struct lcdif_dma_command_t lcdif_dma[NR_CMDS]; |
| 59 | |
| 60 | /** |
| 61 | * Utils |
| 62 | */ |
| 63 | static int g_wait_nr_frame = 0; |
| 64 | static struct semaphore g_wait_sema; |
| 65 | |
| 66 | static void wait_frames_cb(void) |
| 67 | { |
| 68 | if(--g_wait_nr_frame == 0) |
| 69 | semaphore_release(&g_wait_sema); |
| 70 | } |
| 71 | |
| 72 | static void wait_nr_frames(int nr) |
| 73 | { |
| 74 | g_wait_nr_frame = 1 + nr; // +1 because we want entire frames |
| 75 | imx233_lcdif_set_vsync_edge_cb(wait_frames_cb); |
| 76 | imx233_lcdif_enable_vsync_edge_irq(true); |
| 77 | semaphore_wait(&g_wait_sema, TIMEOUT_BLOCK); |
| 78 | imx233_lcdif_enable_vsync_edge_irq(false); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * SPI |
| 83 | */ |
| 84 | |
| 85 | #define SPI_CS(v) imx233_pinctrl_set_gpio(1, 11, v) |
| 86 | #define SPI_SCL(v) imx233_pinctrl_set_gpio(1, 10, v) |
| 87 | #define SPI_SDO(v) imx233_pinctrl_set_gpio(1, 9, v) |
| 88 | |
| 89 | #define DEV_ID 0x74 |
| 90 | #define RS 0x2 |
| 91 | #define RW 0x1 |
| 92 | |
| 93 | static void spi_init(void) |
| 94 | { |
| 95 | imx233_pinctrl_acquire(1, 9, "lcd_spi_sdo"); |
| 96 | imx233_pinctrl_acquire(1, 10, "lcd_spi_scl"); |
| 97 | imx233_pinctrl_acquire(1, 11, "lcd_spi_cs"); |
| 98 | imx233_pinctrl_set_function(1, 9, PINCTRL_FUNCTION_GPIO); |
| 99 | imx233_pinctrl_set_function(1, 10, PINCTRL_FUNCTION_GPIO); |
| 100 | imx233_pinctrl_set_function(1, 11, PINCTRL_FUNCTION_GPIO); |
| 101 | imx233_pinctrl_set_gpio(1, 9, true); |
| 102 | imx233_pinctrl_set_gpio(1, 10, true); |
| 103 | imx233_pinctrl_set_gpio(1, 11, true); |
| 104 | imx233_pinctrl_enable_gpio(1, 9, true); |
| 105 | imx233_pinctrl_enable_gpio(1, 10, true); |
| 106 | imx233_pinctrl_enable_gpio(1, 11, true); |
| 107 | } |
| 108 | |
| 109 | static void spi_delay(void) |
| 110 | { |
| 111 | udelay(1); |
| 112 | } |
| 113 | |
| 114 | static void spi_begin(void) |
| 115 | { |
| 116 | SPI_CS(false); |
| 117 | spi_delay(); |
| 118 | } |
| 119 | |
| 120 | static void spi_write(unsigned char b) |
| 121 | { |
| 122 | for(int i = 7; i >= 0; i--) |
| 123 | { |
| 124 | SPI_SCL(false); |
| 125 | spi_delay(); |
| 126 | SPI_SDO((b >> i) & 1); |
| 127 | spi_delay(); |
| 128 | SPI_SCL(true); |
| 129 | spi_delay(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void spi_end(void) |
| 134 | { |
| 135 | SPI_CS(true); |
| 136 | spi_delay(); |
| 137 | } |
| 138 | |
| 139 | static void spi_write_reg(uint8_t reg, uint16_t value) |
| 140 | { |
| 141 | spi_begin(); |
| 142 | spi_write(DEV_ID); |
| 143 | spi_write(0); |
| 144 | spi_write(reg); |
| 145 | spi_end(); |
| 146 | spi_begin(); |
| 147 | spi_write(DEV_ID | RS); |
| 148 | spi_write(value >> 8); |
| 149 | spi_write(value & 0xff); |
| 150 | spi_end(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * LCD control |
| 155 | */ |
| 156 | |
| 157 | static void lcd_power(bool en) |
| 158 | { |
| 159 | imx233_pinctrl_set_gpio(1, 8, en); |
| 160 | mdelay(10); |
| 161 | } |
| 162 | |
| 163 | static void lcd_power_seq(void) |
| 164 | { |
| 165 | spi_write_reg(0x7, 0); |
| 166 | mdelay(10); |
| 167 | spi_write_reg(0x12, 0x1618); |
| 168 | spi_write_reg(0x11, 0x2227); |
| 169 | spi_write_reg(0x13, 0x61d1); |
| 170 | spi_write_reg(0x10, 0x550c); |
| 171 | wait_nr_frames(5); |
| 172 | spi_write_reg(0x12, 0x0c58); |
| 173 | } |
| 174 | |
| 175 | static void lcd_init_seq(void) |
| 176 | { |
| 177 | /* NOTE I don't understand why I have to use BGR, logic would say I should not */ |
| 178 | spi_write_reg(0x1, 0x2b1d);// inversion |
| 179 | spi_write_reg(0x2, 0x300); |
| 180 | /* NOTE by default stmp3700 has vsync/hsync active low and data launch |
| 181 | * at negative edge of dotclk, reflect this in the polarity settings */ |
| 182 | spi_write_reg(0x3, 0xd040);// polarity (OF uses 0xc040, seems incorrect) |
| 183 | spi_write_reg(0x8, 0); // vsync back porch (0=3H) |
| 184 | spi_write_reg(0x9, 0); // hsync back porhc (0=24clk) |
| 185 | spi_write_reg(0x76, 0x2213); |
| 186 | spi_write_reg(0xb, 0x33e1); |
| 187 | spi_write_reg(0xc, 0x23); |
| 188 | spi_write_reg(0x76, 0); |
| 189 | spi_write_reg(0xd, 7); |
| 190 | spi_write_reg(0xe, 0); |
| 191 | spi_write_reg(0x15, 0x803); |
| 192 | spi_write_reg(0x14, 0); |
| 193 | spi_write_reg(0x16, 0); |
| 194 | spi_write_reg(0x30, 0x706); |
| 195 | spi_write_reg(0x31, 0x406); |
| 196 | spi_write_reg(0x32, 0xc09); |
| 197 | spi_write_reg(0x33, 0x606); |
| 198 | spi_write_reg(0x34, 0x706); |
| 199 | spi_write_reg(0x35, 0x406); |
| 200 | spi_write_reg(0x36, 0xc06); |
| 201 | spi_write_reg(0x37, 0x601); |
| 202 | spi_write_reg(0x38, 0x504); |
| 203 | spi_write_reg(0x39, 0x504); |
| 204 | } |
| 205 | |
| 206 | static void lcd_display_on_seq(void) |
| 207 | { |
| 208 | spi_write_reg(0x7, 1); |
| 209 | wait_nr_frames(1); |
| 210 | spi_write_reg(0x7, 0x101); |
| 211 | wait_nr_frames(2); |
| 212 | spi_write_reg(0x76, 0x2213); |
| 213 | spi_write_reg(0x1c, 0x6650); |
| 214 | spi_write_reg(0xb, 0x33e1); |
| 215 | spi_write_reg(0x76, 0); |
| 216 | spi_write_reg(0x7, 0x103); |
| 217 | } |
| 218 | |
Amaury Pouly | 1ef7b65 | 2013-11-19 22:58:41 +0000 | [diff] [blame] | 219 | #ifdef HAVE_LCD_ENABLE |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 220 | static void lcd_display_off_seq(void) |
| 221 | { |
| 222 | spi_write_reg(0xb, 0x30e1); |
| 223 | spi_write_reg(0x7, 0x102); |
| 224 | wait_nr_frames(2); |
| 225 | spi_write_reg(0x7, 0); |
| 226 | spi_write_reg(0x12, 0); |
| 227 | spi_write_reg(0x10, 0x100); |
| 228 | } |
| 229 | |
| 230 | static void lcd_standby_in_seq(void) |
| 231 | { |
| 232 | lcd_display_off_seq(); |
| 233 | spi_write_reg(0x10, 0x1); |
| 234 | } |
| 235 | |
| 236 | static void lcd_standby_out_seq(void) |
| 237 | { |
| 238 | spi_write_reg(0x10, 0); |
| 239 | lcd_power_seq(); |
| 240 | lcd_display_on_seq(); |
| 241 | } |
Amaury Pouly | 0fb0524 | 2013-11-19 22:29:31 +0000 | [diff] [blame] | 242 | #endif |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * Rockbox |
| 246 | */ |
| 247 | |
| 248 | void lcd_init_device(void) |
| 249 | { |
| 250 | semaphore_init(&g_wait_sema, 1, 0); |
| 251 | #ifdef HAVE_LCD_ENABLE |
| 252 | lcd_on = true; |
| 253 | #endif |
| 254 | /** lcd is 320x240, data bus is 8-bit, depth is 24-bit so we need 3clk/pix |
| 255 | * by running PIX clock at 24MHz we can sustain ~100 fps */ |
| 256 | imx233_clkctrl_enable(CLK_PIX, false); |
| 257 | imx233_clkctrl_set_div(CLK_PIX, 2); |
| 258 | imx233_clkctrl_set_bypass(CLK_PIX, true); /* use XTAL */ |
| 259 | imx233_clkctrl_enable(CLK_PIX, true); |
| 260 | imx233_dma_reset_channel(APB_LCDIF); |
| 261 | imx233_dma_clkgate_channel(APB_LCDIF, true); |
| 262 | imx233_lcdif_init(); |
| 263 | imx233_lcdif_setup_dotclk_pins(8, false); |
| 264 | imx233_lcdif_set_word_length(8); |
| 265 | /** Datasheet states: |
| 266 | * 257H >= VBP >= 3H, VBP > VLW, VFP > 1H |
| 267 | * 1533clk >= HBP >= 24clk, HBP > HLW, HFP >= 4clk |
| 268 | * |
| 269 | * Take VLW=1H, VBP=3H, VFP=2H, HLW=8, HBP=24, HFP=4 |
| 270 | * Take 3clk/pix because we send 24-bit/pix with 8-bit data bus |
| 271 | * Keep consistent with register setting in lcd_init_seq |
| 272 | */ |
| 273 | imx233_lcdif_setup_dotclk_ex(/*v_pulse_width*/1, /*v_back_porch*/3, |
| 274 | /*v_front_porch*/1, /*h_pulse_width*/8, /*h_back_porch*/24, |
| 275 | /*h_front_porch*/4, LCD_WIDTH, LCD_HEIGHT, /*clk_per_pix*/3, |
| 276 | /*enable_present*/false); |
| 277 | imx233_lcdif_set_byte_packing_format(0xf); |
| 278 | // prepare pins |
| 279 | spi_init(); |
| 280 | imx233_pinctrl_acquire(1, 8, "lcd_power"); |
| 281 | imx233_pinctrl_set_function(1, 8, PINCTRL_FUNCTION_GPIO); |
| 282 | imx233_pinctrl_enable_gpio(1, 8, true); |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 283 | // reset lcd |
| 284 | imx233_lcdif_reset_lcd(true); |
Amaury Pouly | c2c2274 | 2013-11-19 19:04:03 +0000 | [diff] [blame] | 285 | mdelay(10); |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 286 | imx233_lcdif_reset_lcd(false); |
Amaury Pouly | c2c2274 | 2013-11-19 19:04:03 +0000 | [diff] [blame] | 287 | mdelay(10); |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 288 | imx233_lcdif_reset_lcd(true); |
Amaury Pouly | c2c2274 | 2013-11-19 19:04:03 +0000 | [diff] [blame] | 289 | mdelay(10); |
| 290 | // power up |
| 291 | lcd_power(true); |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 292 | // setup registers |
| 293 | imx233_lcdif_enable_sync_signals(true); // we need frame signals during init |
| 294 | lcd_power_seq(); |
| 295 | lcd_init_seq(); |
| 296 | lcd_display_on_seq(); |
| 297 | // setup refresh |
| 298 | unsigned size = IMX233_FRAMEBUFFER_SIZE; |
| 299 | uint8_t *frame_p = FRAME; |
| 300 | for(int i = 0; i < NR_CMDS; i++) |
| 301 | { |
| 302 | unsigned xfer = MIN(IMX233_MAX_SINGLE_DMA_XFER_SIZE, size); |
| 303 | lcdif_dma[i].dma.next = &lcdif_dma[(i + 1) % NR_CMDS].dma; |
| 304 | lcdif_dma[i].dma.cmd = BF_OR3(APB_CHx_CMD, CHAIN(1), |
| 305 | COMMAND(BV_APB_CHx_CMD_COMMAND__READ), XFER_COUNT(xfer)); |
| 306 | lcdif_dma[i].dma.buffer = frame_p; |
| 307 | size -= xfer; |
| 308 | frame_p += xfer; |
| 309 | } |
| 310 | imx233_dma_start_command(APB_LCDIF, &lcdif_dma[0].dma); |
| 311 | BF_SET(LCDIF_CTRL, RUN); |
| 312 | } |
| 313 | |
| 314 | #ifdef HAVE_LCD_ENABLE |
| 315 | bool lcd_active(void) |
| 316 | { |
| 317 | return lcd_on; |
| 318 | } |
| 319 | |
| 320 | void lcd_enable(bool enable) |
| 321 | { |
| 322 | if(lcd_on == enable) |
| 323 | return; |
| 324 | |
| 325 | lcd_on = enable; |
Amaury Pouly | 7bbdcc1 | 2013-11-11 02:00:41 +0000 | [diff] [blame] | 326 | if(lcd_on) |
| 327 | lcd_standby_out_seq(); |
| 328 | else |
| 329 | lcd_standby_in_seq(); |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 330 | } |
| 331 | #endif |
| 332 | |
| 333 | void lcd_update(void) |
| 334 | { |
| 335 | lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT); |
| 336 | } |
| 337 | |
| 338 | void lcd_update_rect(int x, int y, int w, int h) |
| 339 | { |
| 340 | #ifdef HAVE_LCD_ENABLE |
| 341 | if(!lcd_on) |
| 342 | return; |
| 343 | #endif |
Amaury Pouly | a54c4ab | 2013-11-11 02:01:05 +0000 | [diff] [blame] | 344 | for(int yy = y; yy < y + h; yy++) |
| 345 | { |
| 346 | uint16_t *pix = FBADDR(x, yy); |
| 347 | uint8_t *p = 3 * (yy * LCD_WIDTH + x) + (uint8_t *)FRAME; |
| 348 | for(int xx = 0; xx < w; xx++, pix++) |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 349 | { |
Amaury Pouly | a54c4ab | 2013-11-11 02:01:05 +0000 | [diff] [blame] | 350 | *p++ = RGB_UNPACK_RED(*pix); |
| 351 | *p++ = RGB_UNPACK_GREEN(*pix); |
| 352 | *p++ = RGB_UNPACK_BLUE(*pix); |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 353 | } |
Amaury Pouly | a54c4ab | 2013-11-11 02:01:05 +0000 | [diff] [blame] | 354 | } |
Amaury Pouly | a072867 | 2013-10-22 00:30:43 +0200 | [diff] [blame] | 355 | } |