summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/tms320dm320/sansa-connect/lcd-sansaconnect.c
blob: 07be11e849fb4e7a91574302b787a28a4ad7d400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: $
 *
 * Copyright (C) 2011 by Tomasz Moń
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <sys/types.h>
#include "config.h"
#include "cpu.h"
#include "string.h"
#include "kernel.h"
#include "system.h"
#include "system-target.h"
#include "lcd.h"
#include "lcd-target.h"
#include "avr-sansaconnect.h"

extern bool lcd_on; /* lcd-memframe.c */

#if defined(HAVE_LCD_SLEEP)
void lcd_sleep(void)
{
    if (lcd_on)
    {
        lcd_on = false;

        avr_hid_lcm_sleep();
        sleep(HZ/20);

        /* disable video encoder */
        bitclr16(&IO_VID_ENC_VMOD, 0x01);

        sleep(HZ/20);

        /* disable video encoder clock */
        bitclr16(&IO_CLK_MOD1, CLK_MOD1_VENC);
    }
}

void lcd_awake(void)
{
    if (!lcd_on)
    {
        lcd_on = true;

        /* enable video encoder clock */
        bitset16(&IO_CLK_MOD1, CLK_MOD1_VENC); 

        /* enable video encoder */
        bitset16(&IO_VID_ENC_VMOD, 0x01);

        avr_hid_lcm_wake();

        send_event(LCD_EVENT_ACTIVATION, NULL);

        lcd_update();
    }
}
#endif

void lcd_init_device(void)
{
    unsigned int addr;
 
    /* Disable Video Encoder clock */
    bitclr16(&IO_CLK_MOD1, CLK_MOD1_VENC);

    /* configure GIO39, GIO34 as outputs */
    IO_GIO_DIR2 &= ~((1 << 7) /* GIO39 */ | (1 << 2) /* GIO34 */);
    
    IO_GIO_FSEL3 = (IO_GIO_FSEL3 & ~(0x300F)) |
                   (0x1000) /* GIO39 - FIELD_VENC */ |
                   (0x4);   /* GIO34 - PWM1 (brightness control) */

    /* OSD Clock = VENC Clock /2,
       CCD clock PCLK,
       VENC Clock from PLLA */
    IO_CLK_SEL1 = 0x3;

    /* Set VENC Clock Division to 11
       OF bootloader sets division to 8, vmlinux sets it to 11 */
    IO_CLK_DIV3 = (IO_CLK_DIV3 & ~(0x1F00)) | 0xB00;

    /* Enable OSD clock */
    bitset16(&IO_CLK_MOD1, CLK_MOD1_OSD);

    /* magic values based on OF bootloader initialization */
    /* Set DAC to powerdown mode (bit 2 to 1 in VMOD) */
    IO_VID_ENC_VMOD = 0x2014; /* OF sets 0x2010 */
    IO_VID_ENC_VDPRO = 0x80;
    IO_VID_ENC_HSPLS = 0x4;
    IO_VID_ENC_HINT = 0x4B0;
    IO_VID_ENC_HSTART = 0x88;
    IO_VID_ENC_HVALID = 0x3C0;
    IO_VID_ENC_HSDLY = 0;
    IO_VID_ENC_VSPLS = 0x2;
    IO_VID_ENC_VINT = 0x152;
    IO_VID_ENC_VSTART = 0x6;
    IO_VID_ENC_VVALID = 0x140;
    IO_VID_ENC_VSDLY = 0;
    IO_VID_ENC_DCLKCTL = 0x3;
    IO_VID_ENC_DCLKPTN0 = 0xC;
    IO_VID_ENC_VDCTL = 0x6000;
    IO_VID_ENC_SYNCTL = 0x2;
    IO_VID_ENC_LCDOUT = 0x101;
    IO_VID_ENC_VMOD = 0x2015; /* OF sets 0x2011 */

    /* Copy Rockbox frame buffer to the second framebuffer */
    lcd_update();

    avr_hid_lcm_power_on();

    /* set framebuffer address - OF sets RAM start address to 0x1000000 */
    addr = ((int)FRAME-CONFIG_SDRAM_START)/32;

    IO_OSD_OSDWINADH    = addr >> 16;
    IO_OSD_OSDWIN0ADL   = addr & 0xFFFF;

    IO_OSD_BASEPX = 0x44;
    IO_OSD_BASEPY = 0x6;
    IO_OSD_OSDWIN0XP = 0;
    IO_OSD_OSDWIN0YP = 0;
    IO_OSD_OSDWIN0XL = LCD_WIDTH*2;  /* OF bootloader sets 480 */
    IO_OSD_OSDWIN0YL = LCD_HEIGHT;   /* OF bootloader sets 320 */
    IO_OSD_OSDWIN0OFST = 0xF;
    IO_OSD_OSDWINMD0 = 0x25FB;/* OF bootloader sets 25C3,
                                 vmlinux changes this to 0x25FB */
    IO_OSD_VIDWINMD = 0; /* disable video windows (OF sets 0x03) */

    IO_OSD_OSDWINMD1 = 0; /* disable OSD window 1 */

    /* Enable Video Encoder and OSD clocks */
    bitset16(&IO_CLK_MOD1, CLK_MOD1_VENC | CLK_MOD1_OSD);

    /* Enable Video Encoder - RGB666, custom timing */
    IO_VID_ENC_VMOD = 0x2015;
    avr_hid_lcm_wake();
    lcd_on = true;
}

#ifdef LCD_USE_DMA
static void dma_lcd_copy_buffer_rect(int x, int y, int width, int height)
{
    char *dst, *src;

    /* Image buffer A is 4KW, every pixel is one Word */
    /* lines is maximum number of lines we can transfer in single run */
    int lines = 4096/width;
    if (lines > height)
        lines = height;

    /* Set source and destination addresses */
    dst = (char*)(FRAME + LCD_WIDTH*y + x);
    src = (char*)(FBADDR(x,y));
 
    /* Flush cache to memory */
    commit_dcache();

    /* Addresses are relative to start of SDRAM */
    src -= CONFIG_SDRAM_START;
    dst -= CONFIG_SDRAM_START;
    
    /* Enable Image Buffer clock */
    bitset16(&IO_CLK_MOD1, CLK_MOD1_IMGBUF);

    /* Use Image Buffer A for DMA */
    COP_BUF_MUX0 = (COP_BUF_MUX0 & 0xFFF0) | 0x0003;

    /* Setup buffer offsets and transfer width/height */
    COP_BUF_LOFST = width;
    COP_DMA_XNUM  = width;
    COP_DMA_YNUM  = lines;

    /* DMA: No byte SWAP, no transformation, data bus shift down 0 bit */
    COP_IMG_MODE &= 0xC0;

    /* Set the start address of buffer */
    COP_BUF_ADDR = 0x0000;
    
    /* Setup SDRAM stride */
    COP_SDEM_LOFST = LCD_WIDTH;

    do {
        int addr;

        addr = (int)src;
        addr >>= 1; /* Addresses are in 16-bit words */
        
        /* Setup the registers to initiate the read from SDRAM */
        COP_SDEM_ADDRH = addr >> 16;
        COP_SDEM_ADDRL = addr & 0xFFFF;
        
        /* Set direction and start */
        COP_DMA_CTRL = 0x0001;
        COP_DMA_CTRL |= 0x0002;

        /* Wait for read to finish */
        while (COP_DMA_CTRL & 0x02) {};
        
        addr = (int)dst;
        addr >>= 1;
        
        COP_SDEM_ADDRH = addr >> 16;
        COP_SDEM_ADDRL = addr & 0xFFFF;
        
        /* Set direction and start transfer */
        COP_DMA_CTRL = 0x0000;
        COP_DMA_CTRL |= 0x0002;
        
        /* Wait for the transfer to complete */
        while (COP_DMA_CTRL & 0x02) {};

        /* Decrease height, update pointers */
        src += (LCD_WIDTH << 1)*lines;
        dst += (LCD_WIDTH << 1)*lines;

        height -= lines;
        if (height < lines)
        {
            lines = height;
            COP_DMA_YNUM = height;
        }
    } while (height > 0);

    /* Disable Image Buffer clock */
    bitclr16(&IO_CLK_MOD1, CLK_MOD1_IMGBUF);
}

/* Update a fraction of the display. */
void lcd_update_rect(int x, int y, int width, int height)
        __attribute__ ((section(".icode")));
void lcd_update_rect(int x, int y, int width, int height)
{
    if (!lcd_on)
        return;

    if ((width | height) < 0)
        return; /* Nothing left to do */

    if (x + width > LCD_WIDTH)
        width = LCD_WIDTH - x; /* Clip right */
    if (x < 0)
        width += x, x = 0; /* Clip left */

    if (y + height > LCD_HEIGHT)
        height = LCD_HEIGHT - y; /* Clip bottom */
    if (y < 0)
        height += y, y = 0; /* Clip top */

    dma_lcd_copy_buffer_rect(x, y, width, height);
}

/* Update the display.
   This must be called after all other LCD functions that change the display. */
void lcd_update(void) __attribute__ ((section(".icode")));
void lcd_update(void)
{
    if (!lcd_on)
        return;

    lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
}
#endif

void lcd_set_contrast(int val) {
  (void) val;
  // TODO:
}

void lcd_set_invert_display(bool yesno) {
  (void) yesno;
  // TODO:
}

void lcd_set_flip(bool yesno) {
  (void) yesno;
  // TODO:
}