Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / firmware / target / mips / ingenic_jz47xx / lcd-jz4740.c
blobef45317c3fcd1b03eb20ad0e2ae84b616b31dc08
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 ****************************************************************************/
21 #include <sys/types.h> /* off_t */
23 #include "config.h"
24 #include "jz4740.h"
25 #include "lcd.h"
26 #include "lcd-target.h"
27 #include "system.h"
28 #include "kernel.h"
31 Warning: code behaviour is unpredictable when switch_thread() gets called in IRQ mode!
32 So don't update the LCD in an interrupt handler!
35 static volatile bool lcd_is_on = false;
36 static struct mutex lcd_mtx;
37 static struct wakeup lcd_wkup;
38 static int lcd_count = 0;
40 void lcd_clock_enable(void)
42 if(++lcd_count == 1)
43 __cpm_start_lcd();
46 void lcd_clock_disable(void)
48 if(--lcd_count == 0)
49 __cpm_stop_lcd();
52 /* LCD init */
53 void lcd_init_device(void)
55 lcd_init_controller();
57 lcd_is_on = true;
58 mutex_init(&lcd_mtx);
59 wakeup_init(&lcd_wkup);
60 system_enable_irq(DMA_IRQ(DMA_LCD_CHANNEL));
63 #ifdef HAVE_LCD_ENABLE
64 void lcd_enable(bool state)
66 if(lcd_is_on == state)
67 return;
69 if(state)
71 lcd_on();
72 send_event(LCD_EVENT_ACTIVATION, NULL);
74 else
75 lcd_off();
77 lcd_is_on = state;
79 #endif
81 bool lcd_active(void)
83 return lcd_is_on;
86 /* Update a fraction of the display. */
87 void lcd_update_rect(int x, int y, int width, int height)
89 /* Currently only do updates with full LCD width.
90 * DMA can't handle full partial updates and CPU is too slow compared
91 * to DMA updates */
92 x = 0;
93 width = LCD_WIDTH;
95 mutex_lock(&lcd_mtx);
97 lcd_clock_enable();
99 lcd_set_target(x, y, width, height);
101 dma_enable();
103 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) = DMAC_DCCSR_NDES;
104 REG_DMAC_DSAR(DMA_LCD_CHANNEL) = PHYSADDR((unsigned long)&lcd_framebuffer[y][x]);
105 REG_DMAC_DRSR(DMA_LCD_CHANNEL) = DMAC_DRSR_RS_SLCD;
106 REG_DMAC_DTAR(DMA_LCD_CHANNEL) = PHYSADDR(SLCD_FIFO);
107 REG_DMAC_DTCR(DMA_LCD_CHANNEL) = (width * height) >> 3;
109 REG_DMAC_DCMD(DMA_LCD_CHANNEL) = ( DMAC_DCMD_SAI | DMAC_DCMD_RDIL_IGN | DMAC_DCMD_SWDH_32
110 | DMAC_DCMD_DWDH_16 | DMAC_DCMD_DS_16BYTE );
112 __dcache_writeback_all(); /* Size of framebuffer is way bigger than cache size.
113 We need to find a way to make the framebuffer uncached, so this statement can get removed. */
115 while(REG_SLCD_STATE & SLCD_STATE_BUSY);
116 REG_SLCD_CTRL |= SLCD_CTRL_DMA_EN; /* Enable SLCD DMA support */
118 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) |= DMAC_DCCSR_EN; /* Enable DMA channel */
119 REG_DMAC_DCMD(DMA_LCD_CHANNEL) |= DMAC_DCMD_TIE; /* Enable DMA interrupt */
121 wakeup_wait(&lcd_wkup, TIMEOUT_BLOCK); /* Sleeping in lcd_update() should be safe */
123 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_EN; /* Disable DMA channel */
124 dma_disable();
126 while(REG_SLCD_STATE & SLCD_STATE_BUSY);
127 REG_SLCD_CTRL &= ~SLCD_CTRL_DMA_EN; /* Disable SLCD DMA support */
129 lcd_clock_disable();
131 mutex_unlock(&lcd_mtx);
134 void DMA_CALLBACK(DMA_LCD_CHANNEL)(void)
136 if (REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_HLT)
137 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_HLT;
139 if (REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_AR)
140 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_AR;
142 if (REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_CT)
143 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_CT;
145 if (REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_TT)
146 REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_TT;
148 wakeup_signal(&lcd_wkup);
151 /* Update the display.
152 This must be called after all other LCD functions that change the display. */
153 void lcd_update(void)
155 if(!lcd_is_on)
156 return;
158 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
161 /* (Mis)use LCD framebuffer as a temporary buffer */
162 void lcd_blit_yuv(unsigned char * const src[3],
163 int src_x, int src_y, int stride,
164 int x, int y, int width, int height)
166 unsigned char const * yuv_src[3];
167 register off_t z;
169 if(!lcd_is_on)
170 return;
172 z = stride * src_y;
173 yuv_src[0] = src[0] + z + src_x;
174 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
175 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
177 __dcache_writeback_all();
179 __cpm_start_ipu();
181 IPU_STOP_IPU();
182 IPU_RESET_IPU();
183 IPU_CLEAR_END_FLAG();
185 IPU_DISABLE_RSIZE();
186 IPU_DISABLE_IRQ();
188 IPU_SET_INFMT(INFMT_YUV420);
189 IPU_SET_OUTFMT(OUTFMT_RGB565);
191 IPU_SET_IN_FM(width, height);
192 IPU_SET_Y_STRIDE(stride);
193 IPU_SET_UV_STRIDE(stride, stride);
195 IPU_SET_Y_ADDR(PHYSADDR((unsigned long)yuv_src[0]));
196 IPU_SET_U_ADDR(PHYSADDR((unsigned long)yuv_src[1]));
197 IPU_SET_V_ADDR(PHYSADDR((unsigned long)yuv_src[2]));
198 IPU_SET_OUT_ADDR(PHYSADDR((unsigned long)&lcd_framebuffer[x][y]));
200 IPU_SET_OUT_FM(height, width);
201 IPU_SET_OUT_STRIDE(height);
203 IPU_SET_CSC_C0_COEF(YUV_CSC_C0);
204 IPU_SET_CSC_C1_COEF(YUV_CSC_C1);
205 IPU_SET_CSC_C2_COEF(YUV_CSC_C2);
206 IPU_SET_CSC_C3_COEF(YUV_CSC_C3);
207 IPU_SET_CSC_C4_COEF(YUV_CSC_C4);
209 IPU_RUN_IPU();
211 while(!(IPU_POLLING_END_FLAG()) && IPU_IS_ENABLED());
213 IPU_CLEAR_END_FLAG();
214 IPU_STOP_IPU();
215 IPU_RESET_IPU();
217 __cpm_stop_ipu();
219 /* YUV speed is limited by LCD speed */
220 lcd_update_rect(y, x, height, width);