Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
492 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "thread.h"
26
#include "s5l8701.h"
27
#include "util.h"
28
 
29
 
30
static struct mutex lcd_mutex;
31
 
32
 
33
void lcd_init()
34
{
35
    DMACON8 = 0x20590000;
36
    LCDCON = 0xd01;
37
    LCDPHTIME = 0;
38
}
39
 
40
int lcd_get_width()
41
{
42
    return LCD_WIDTH;
43
}
44
 
45
int lcd_get_height()
46
{
47
    return LCD_HEIGHT;
48
}
49
 
50
int lcd_get_bytes_per_pixel()
51
{
52
    return LCD_BYTESPERPIXEL;
53
}
54
 
55
int lcd_get_format()
56
{
57
    return LCD_FORMAT;
58
}
59
 
60
static void lcd_send_cmd(uint16_t cmd) ICODE_ATTR __attribute__((noinline));
61
static void lcd_send_cmd(uint16_t cmd)
62
{
63
    while (LCDSTATUS & 0x10);
64
    LCDWCMD = cmd;
65
}
66
 
67
static void lcd_send_data(uint16_t data) ICODE_ATTR __attribute__((noinline));
68
static void lcd_send_data(uint16_t data)
69
{
70
    while (LCDSTATUS & 0x10);
71
    LCDWDATA = data;
72
}
73
 
74
static uint32_t lcd_detect() ICODE_ATTR;
75
static uint32_t lcd_detect()
76
{
77
    return (PDAT13 & 1) | (PDAT14 & 2);
78
}
79
 
80
bool displaylcd_busy() ICODE_ATTR;
81
bool displaylcd_busy()
82
{
83
    return DMAALLST2 & 0x70000;
84
}
85
 
86
bool displaylcd_safe()
87
{
88
    return !(DMAALLST2 & 0x70000);
89
}
90
 
91
void displaylcd_sync() ICODE_ATTR;
92
void displaylcd_sync()
93
{
94
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
95
    while (displaylcd_busy()) sleep(100);
96
    mutex_unlock(&lcd_mutex);
97
}
98
 
99
void displaylcd_setup(unsigned int startx, unsigned int endx,
100
                      unsigned int starty, unsigned int endy) ICODE_ATTR;
101
void displaylcd_setup(unsigned int startx, unsigned int endx,
102
                      unsigned int starty, unsigned int endy)
103
{
104
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
105
    displaylcd_sync();
106
    if (lcd_detect() == 2)
107
    {
108
        lcd_send_cmd(0x50);
109
        lcd_send_data(startx);
110
        lcd_send_cmd(0x51);
111
        lcd_send_data(endx);
112
        lcd_send_cmd(0x52);
113
        lcd_send_data(starty);
114
        lcd_send_cmd(0x53);
115
        lcd_send_data(endy);
116
        lcd_send_cmd(0x20);
117
        lcd_send_data(startx);
118
        lcd_send_cmd(0x21);
119
        lcd_send_data(starty);
120
        lcd_send_cmd(0x22);
121
    }
122
    else
123
    {
124
        lcd_send_cmd(0x2a);
125
        lcd_send_data(startx);
126
        lcd_send_data(endx);
127
        lcd_send_cmd(0x2b);
128
        lcd_send_data(starty);
129
        lcd_send_data(endy);
130
        lcd_send_cmd(0x2c);
131
    }
132
}
133
 
134
static void displaylcd_dma(void* data, int pixels) ICODE_ATTR;
135
static void displaylcd_dma(void* data, int pixels)
136
{
137
    uint16_t* in = (uint16_t*)data;
138
    while (LCDSTATUS & 8);
139
    while (pixels & 3)
140
	{
141
        LCDWDATA = *in++;
142
        pixels--;
143
	}
144
    DMABASE8 = in;
145
    DMACON8 = 0x20590000;
146
    DMATCNT8 = pixels / 4;
147
    clean_dcache();
148
    DMACOM8 = 4;
149
}
150
 
151
static void displaylcd_solid(uint16_t data, int pixels) ICODE_ATTR;
152
static void displaylcd_solid(uint16_t data, int pixels)
153
{
154
    while (pixels >= 4)
155
    {
156
        while (LCDSTATUS & 8);
157
        LCDWDATA = data;
158
        LCDWDATA = data;
159
        LCDWDATA = data;
160
        LCDWDATA = data;
161
        pixels -= 4;
162
    }
163
    while (LCDSTATUS & 8);
164
    while (pixels & 3)
165
	{
166
        LCDWDATA = data;
167
        pixels--;
168
	}
169
}
170
 
171
void displaylcd_native(unsigned int startx, unsigned int endx,
172
                       unsigned int starty, unsigned int endy, void* data)
173
{
174
    int pixels = (endx - startx + 1) * (endy - starty + 1);
175
    if (pixels <= 0) return;
176
    displaylcd_setup(startx, endx, starty, endy);
177
    displaylcd_dma(data, pixels);
178
    mutex_unlock(&lcd_mutex);
179
}
180
 
181
void filllcd_native(unsigned int startx, unsigned int endx,
182
                    unsigned int starty, unsigned int endy, int color)
183
{
184
    int pixels = (endx - startx + 1) * (endy - starty + 1);
185
    if (pixels <= 0) return;
186
    displaylcd_setup(startx, endx, starty, endy);
187
    displaylcd_solid(color, pixels);
188
    mutex_unlock(&lcd_mutex);
189
}
190
 
191
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
192
                       unsigned int height, void* data, unsigned int datax,
193
                       unsigned int datay, unsigned int stride, bool solid) ICODE_ATTR;
194
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
195
                       unsigned int height, void* data, unsigned int datax,
196
                       unsigned int datay, unsigned int stride, bool solid)
197
{
198
    int pixels = width * height;
199
    if (pixels <= 0) return;
200
    displaylcd_setup(x, x + width - 1, y, y + height - 1);
201
    int corrsize = width * 3;
202
    signed char* corr = (signed char*)malloc(corrsize);
203
    if (!corr)
204
    {
205
        mutex_unlock(&lcd_mutex);
206
        return;
207
    }
208
    memset(corr, 0, corrsize);
209
    unsigned char* in = (unsigned char*)data + (stride * datay + datax) * 3;
210
    for (y = 0; y < height; y++)
211
    {
212
        int i;
213
        signed char* corrptr = corr;
214
        signed char lastcorr[3] = {0};
215
        for (x = 0; x < width; x++)
216
        {
217
            unsigned int pixel = 0;
218
            signed char* lastcorrptr = lastcorr;
219
            int orig = *in++ + *corrptr + *lastcorrptr;
220
            orig = MAX(0, MIN(255, orig));
221
            unsigned int real = orig >> 3;
222
            pixel |= real << 11;
223
            int err = orig - ((real << 3) | (real >> 2));
224
            *corrptr++ = (*lastcorrptr >> 1) + err >> 2;
225
            *lastcorrptr++ = err >> 1;
226
            orig = *in++ + *corrptr + *lastcorrptr;
227
            orig = MAX(0, MIN(255, orig));
228
            real = orig >> 2;
229
            pixel |= real << 5;
230
            err = orig - ((real << 2) | (real >> 4));
231
            *corrptr++ = (*lastcorrptr >> 1) + err >> 2;
232
            *lastcorrptr++ = err >> 1;
233
            orig = *in++ + *corrptr + *lastcorrptr;
234
            orig = MAX(0, MIN(255, orig));
235
            real = orig >> 3;
236
            pixel |= real;
237
            err = orig - ((real << 3) | (real >> 2));
238
            *corrptr++ = (*lastcorrptr >> 1) + err >> 2;
239
            *lastcorrptr++ = err >> 1;
240
            LCDWDATA = pixel;
241
            if (solid) in -= 3;
242
        }
243
        if (solid) in += stride * 3;
244
        else in += (stride - width) * 3;
245
    }
246
    free(corr);
247
    mutex_unlock(&lcd_mutex);
248
}
249
 
250
void displaylcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height,
251
                void* data, unsigned int datax, unsigned int datay, unsigned int stride)
252
{
253
    displaylcd_dither(x, y, width, height, data, datax, datay, stride, false);
254
}
255
 
256
void filllcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height, int color)
257
{
258
    if (width * height <= 0) return;
259
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
260
    displaylcd_dither(x, y, width, height, &color, 0, 0, 0, true);
261
    mutex_unlock(&lcd_mutex);
262
}
263
 
264
void lcd_shutdown()
265
{
266
    displaylcd_sync();
267
    uint32_t type = lcd_detect();
268
    if (type == 2)
269
    {
270
        lcd_send_cmd(0x7);
271
        lcd_send_data(0x232);
272
        lcd_send_cmd(0x13);
273
        lcd_send_data(0x1137);
274
        lcd_send_cmd(0x7);
275
        lcd_send_data(0x201);
276
        lcd_send_cmd(0x13);
277
        lcd_send_data(0x137);
278
        lcd_send_cmd(0x7);
279
        lcd_send_data(0x200);
280
        lcd_send_cmd(0x10);
281
        lcd_send_data(0x680);
282
        lcd_send_cmd(0x12);
283
        lcd_send_data(0x160);
284
        lcd_send_cmd(0x13);
285
        lcd_send_data(0x127);
286
        lcd_send_cmd(0x10);
287
        lcd_send_data(0x600);
288
    }
289
    else
290
    {
291
        lcd_send_cmd(0x28);
292
        lcd_send_cmd(0x10);
293
    }
294
    sleep(5000);
295
}
296
 
297
void INT_DMA8()
298
{
299
    DMACOM8 = 7;
300
    lcdconsole_callback();
301
}
302
 
303
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
304
    ICODE_ATTR __attribute__((naked, noinline));
305
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
306
{
307
    asm volatile(
308
        "cmp r0, #0xff             \n\t"
309
        "moveq r0, #-1             \n\t"
310
        "moveq pc, lr              \n\t"
311
        "cmp r0, #0                \n\t"
312
        "movne r0, #0xff000000     \n\t"
313
        "orrne r0, r0, #0xff0000   \n\t"
314
        "mov r2, r2,lsr#2          \n\t"
315
        "orr r0, r0, r3,lsr#3      \n\t"
316
        "mov r1, r1,lsr#3          \n\t"
317
        "orr r0, r0, r2,lsl#5      \n\t"
318
        "orr r0, r0, r1,lsl#11     \n\t"
319
        "mov pc, lr                \n\t"
320
    );
321
}