Fix onda red. Unlike all other targets, it built the whole backlight driver in the...
[kugel-rb.git] / firmware / target / mips / ingenic_jz47xx / lcd-jz4740.c
blob44d80e1c58004a83c76ee9e7b6aa23e050bff4bb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Maurus Cuelenaere
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 ****************************************************************************/
22 #include "config.h"
23 #include "jz4740.h"
24 #include "lcd.h"
25 #include "lcd-target.h"
26 #include "system.h"
27 #include "kernel.h"
30 Warning: code behaviour is unpredictable when switch_thread() gets called in IRQ mode!
31 So don't update the LCD in an interrupt handler!
34 static volatile bool lcd_is_on = false;
35 static struct mutex lcd_mtx;
36 static struct wakeup lcd_wkup;
38 /* LCD init */
39 void lcd_init_device(void)
41 __cpm_start_lcd();
42 lcd_init_controller();
43 __cpm_stop_lcd();
45 lcd_is_on = true;
46 mutex_init(&lcd_mtx);
47 wakeup_init(&lcd_wkup);
48 system_enable_irq(DMA_IRQ(DMA_LCD_CHANNEL));
51 void lcd_enable(bool state)
53 if(state)
55 lcd_on();
56 #ifdef HAVE_LCD_ENABLE
57 lcd_activation_call_hook();
58 #endif
60 else
61 lcd_off();
63 lcd_is_on = state;
66 bool lcd_active(void)
68 return lcd_is_on;
71 /* Update a fraction of the display. */
72 void lcd_update_rect(int x, int y, int width, int height)
74 /* Currently only do full updates.
75 * DMA can't handle partial updates and CPU is too slow compared
76 * to full DMA updates */
77 x = 0;
78 y = 0;
79 width = LCD_WIDTH;
80 height = LCD_HEIGHT;
82 mutex_lock(&lcd_mtx);
84 __cpm_start_lcd();
86 lcd_set_target(x, y, width, height);
88 dma_enable();
90 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) = DMAC_DCCSR_NDES;
91 REG_DMAC_DSAR(DMA_LCD_CHANNEL) = PHYSADDR((unsigned long)&lcd_framebuffer[y][x]);
92 REG_DMAC_DRSR(DMA_LCD_CHANNEL) = DMAC_DRSR_RS_SLCD;
93 REG_DMAC_DTAR(DMA_LCD_CHANNEL) = PHYSADDR(SLCD_FIFO);
94 REG_DMAC_DTCR(DMA_LCD_CHANNEL) = (width * height) >> 3;
96 REG_DMAC_DCMD(DMA_LCD_CHANNEL) = ( DMAC_DCMD_SAI | DMAC_DCMD_RDIL_IGN | DMAC_DCMD_SWDH_32
97 | DMAC_DCMD_DWDH_16 | DMAC_DCMD_DS_16BYTE );
99 __dcache_writeback_all(); /* Size of framebuffer is way bigger than cache size.
100 We need to find a way to make the framebuffer uncached, so this statement can get removed. */
102 while(REG_SLCD_STATE & SLCD_STATE_BUSY);
103 REG_SLCD_CTRL |= SLCD_CTRL_DMA_EN; /* Enable SLCD DMA support */
105 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) |= DMAC_DCCSR_EN; /* Enable DMA channel */
106 REG_DMAC_DCMD(DMA_LCD_CHANNEL) |= DMAC_DCMD_TIE; /* Enable DMA interrupt */
108 wakeup_wait(&lcd_wkup, TIMEOUT_BLOCK); /* Sleeping in lcd_update() should be safe */
110 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_EN; /* Disable DMA channel */
111 dma_disable();
113 while(REG_SLCD_STATE & SLCD_STATE_BUSY);
114 REG_SLCD_CTRL &= ~SLCD_CTRL_DMA_EN; /* Disable SLCD DMA support */
116 __cpm_stop_lcd();
118 mutex_unlock(&lcd_mtx);
121 void DMA_CALLBACK(DMA_LCD_CHANNEL)(void)
123 if (REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_HLT)
124 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_HLT;
126 if (REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_AR)
127 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_AR;
129 if (REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_CT)
130 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_CT;
132 if (REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_TT)
133 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_TT;
135 wakeup_signal(&lcd_wkup);
138 /* Update the display.
139 This must be called after all other LCD functions that change the display. */
140 void lcd_update(void)
142 if(!lcd_is_on)
143 return;
145 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
148 /* (Mis)use LCD framebuffer as a temporary buffer */
149 void lcd_blit_yuv(unsigned char * const src[3],
150 int src_x, int src_y, int stride,
151 int x, int y, int width, int height)
153 unsigned char const * yuv_src[3];
154 register off_t z;
156 if(!lcd_is_on)
157 return;
159 z = stride * src_y;
160 yuv_src[0] = src[0] + z + src_x;
161 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
162 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
164 __dcache_writeback_all();
166 __cpm_start_ipu();
168 IPU_STOP_IPU();
169 IPU_RESET_IPU();
170 IPU_CLEAR_END_FLAG();
172 IPU_DISABLE_RSIZE();
173 IPU_DISABLE_IRQ();
175 IPU_SET_INFMT(INFMT_YUV420);
176 IPU_SET_OUTFMT(OUTFMT_RGB565);
178 IPU_SET_IN_FM(width, height);
179 IPU_SET_Y_STRIDE(stride);
180 IPU_SET_UV_STRIDE(stride, stride);
182 IPU_SET_Y_ADDR(PHYSADDR((unsigned long)yuv_src[0]));
183 IPU_SET_U_ADDR(PHYSADDR((unsigned long)yuv_src[1]));
184 IPU_SET_V_ADDR(PHYSADDR((unsigned long)yuv_src[2]));
185 IPU_SET_OUT_ADDR(PHYSADDR((unsigned long)&lcd_framebuffer[x][y]));
187 IPU_SET_OUT_FM(height, width);
188 IPU_SET_OUT_STRIDE(height);
190 IPU_SET_CSC_C0_COEF(YUV_CSC_C0);
191 IPU_SET_CSC_C1_COEF(YUV_CSC_C1);
192 IPU_SET_CSC_C2_COEF(YUV_CSC_C2);
193 IPU_SET_CSC_C3_COEF(YUV_CSC_C3);
194 IPU_SET_CSC_C4_COEF(YUV_CSC_C4);
196 IPU_RUN_IPU();
198 while(!(IPU_POLLING_END_FLAG()) && IPU_IS_ENABLED());
200 IPU_CLEAR_END_FLAG();
201 IPU_STOP_IPU();
202 IPU_RESET_IPU();
204 __cpm_stop_ipu();
206 /* YUV speed is limited by LCD speed */
207 lcd_update_rect(y, x, height, width);