Add runtime estimation for the iaudio X5.
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-clip / lcd-ssd1303.c
blobdba5b9c3d7546fdb8767a29792acf49cd026fd15
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Alan Korr
11 * Copyright (C) 2008 François Dinel
12 * Copyright (C) 2008-2009 Rafaël Carré
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "config.h"
25 #include "hwcompat.h"
26 #include "kernel.h"
27 #include "lcd.h"
28 #include "system.h"
29 #include "cpu.h"
30 #include "string.h"
32 /*** AS3525 specifics ***/
33 #ifdef SANSA_CLIPV2
34 #include "as3525v2.h"
35 #else
36 #include "as3525.h"
37 #endif
39 #include "ascodec.h"
41 /*** definitions ***/
43 #define LCD_SET_LOWER_COLUMN_ADDRESS ((char)0x00)
44 #define LCD_SET_HIGHER_COLUMN_ADDRESS ((char)0x10)
45 #define LCD_SET_DISPLAY_START_LINE ((char)0x40)
46 #define LCD_SET_CONTRAST_CONTROL_REGISTER ((char)0x81)
47 #define LCD_SET_SEGMENT_REMAP ((char)0xA0)
48 #define LCD_SET_SEGMENT_REMAP_INV ((char)0xA1)
49 #define LCD_SET_ENTIRE_DISPLAY_OFF ((char)0xA4)
50 #define LCD_SET_ENTIRE_DISPLAY_ON ((char)0xA5)
51 #define LCD_SET_NORMAL_DISPLAY ((char)0xA6)
52 #define LCD_SET_REVERSE_DISPLAY ((char)0xA7)
53 #define LCD_SET_MULTIPLEX_RATIO ((char)0xA8)
54 #define LCD_SET_DC_DC ((char)0xAD)
55 #define LCD_SET_DC_DC_PART2 ((char)0x8A)
56 #define LCD_SET_DISPLAY_OFF ((char)0xAE)
57 #define LCD_SET_DISPLAY_ON ((char)0xAF)
58 #define LCD_SET_PAGE_ADDRESS ((char)0xB0)
59 #define LCD_SET_COM_OUTPUT_SCAN_DIRECTION ((char)0xC0)
60 #define LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV ((char)0xC8)
61 #define LCD_SET_DISPLAY_CLOCK_AND_OSC_FREQ ((char)0xD5)
62 #define LCD_SET_VCOM_DESELECT_LEVEL ((char)0xDB)
63 #define LCD_SET_PRECHARGE_PERIOD ((char)0xD9)
64 #define LCD_NOP ((char)0xE3)
66 /* LCD command codes */
67 #define LCD_CNTL_CONTRAST 0x81 /* Contrast */
68 #define LCD_CNTL_OUTSCAN 0xc8 /* Output scan direction */
69 #define LCD_CNTL_SEGREMAP 0xa1 /* Segment remap */
70 #define LCD_CNTL_DISPON 0xaf /* Display on */
72 #define LCD_CNTL_PAGE 0xb0 /* Page address */
73 #define LCD_CNTL_HIGHCOL 0x10 /* Upper column address */
74 #define LCD_CNTL_LOWCOL 0x00 /* Lower column address */
77 static void lcd_hw_init(void)
79 #if defined(SANSA_CLIP)
80 /* DBOP initialisation, do what OF does */
81 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
83 GPIOB_AFSEL = 0x08; /* DBOP on pin 3 */
84 GPIOC_AFSEL = 0x0f; /* DBOP on pins 3:0 */
86 DBOP_CTRL = 0x51008;
87 DBOP_TIMPOL_01 = 0x6E167;
88 DBOP_TIMPOL_23 = 0xA167E06F;
89 #elif defined(SANSA_CLIPV2)
90 /* DBOP initialisation, do what OF does */
91 CCU_IO |= (1<<12); /* ?? */
92 CGU_DBOP |= /*(1<<3)*/ 0x18 | AS3525_DBOP_DIV;
94 DBOP_CTRL = 0x51004;
95 DBOP_TIMPOL_01 = 0x36A12F;
96 DBOP_TIMPOL_23 = 0xE037E037;
97 #elif defined(SANSA_CLIPPLUS)
98 CGU_PERI |= CGU_SSP_CLOCK_ENABLE;
100 SSP_CPSR = AS3525_SSP_PRESCALER; /* OF = 0x10 */
101 SSP_CR0 = (1<<7) | (1<<6) | 7; /* Motorola SPI frame format, 8 bits */
102 SSP_CR1 = (1<<3) | (1<<1); /* SSP Operation enabled */
103 SSP_IMSC = 0; /* No interrupts */
104 #endif
107 #ifdef SANSA_CLIP
108 #define LCD_DELAY 1
109 #else /* SANSA_CLIPV2 */
110 #define LCD_DELAY 10
111 #endif
113 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
114 void lcd_write_command(int byte)
116 volatile int i = 0;
117 while(i<LCD_DELAY) i++;
119 /* unset D/C# (data or command) */
120 #ifdef SANSA_CLIP
121 GPIOA_PIN(5) = 0;
122 #else /* SANSA_CLIPV2 */
123 GPIOB_PIN(2) = 0;
124 DBOP_TIMPOL_23 = 0xE0370036;
125 #endif
127 /* Write command */
128 /* Only bits 15:12 and 3:0 of DBOP_DOUT are meaningful */
129 DBOP_DOUT = (byte << 8) | byte;
131 /* While push fifo is not empty */
132 while ((DBOP_STAT & (1<<10)) == 0)
135 #ifdef SANSA_CLIPV2
136 DBOP_TIMPOL_23 = 0xE037E037;
137 #endif
139 #elif defined(SANSA_CLIPPLUS)
140 void lcd_write_command(int byte)
142 while(SSP_SR & (1<<4)) /* BSY flag */
145 GPIOB_PIN(2) = 0;
146 SSP_DATA = byte;
148 while(SSP_SR & (1<<4)) /* BSY flag */
151 #endif
153 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
154 void lcd_write_data(const fb_data* p_bytes, int count)
156 volatile int i = 0;
157 while(i<LCD_DELAY) i++;
158 /* set D/C# (data or command) */
159 #ifdef SANSA_CLIP
160 GPIOA_PIN(5) = (1<<5);
161 #else /* SANSA_CLIPV2 */
162 GPIOB_PIN(2) = (1<<2);
163 #endif
165 while (count--)
167 /* Write pixels */
168 /* Only bits 15:12 and 3:0 of DBOP_DOUT are meaningful */
169 DBOP_DOUT = (*p_bytes << 8) | *p_bytes;
171 p_bytes++; /* next packed pixels */
173 /* Wait if push fifo is full */
174 while ((DBOP_STAT & (1<<6)) != 0);
176 /* While push fifo is not empty */
177 while ((DBOP_STAT & (1<<10)) == 0);
179 #elif defined(SANSA_CLIPPLUS)
180 void lcd_write_data(const fb_data* p_bytes, int count)
182 GPIOB_PIN(2) = (1<<2);
184 while (count--)
186 while(!(SSP_SR & (1<<1))) /* wait until transmit FIFO is not full */
189 SSP_DATA = *p_bytes++;
192 #endif
195 /** globals **/
197 static bool display_on; /* used by lcd_enable */
199 /*** hardware configuration ***/
201 int lcd_default_contrast(void)
203 return DEFAULT_CONTRAST_SETTING;
206 void lcd_set_contrast(int val)
208 lcd_write_command(LCD_CNTL_CONTRAST);
209 lcd_write_command(val);
212 void lcd_set_invert_display(bool yesno)
214 if (yesno)
215 lcd_write_command(LCD_SET_REVERSE_DISPLAY);
216 else
217 lcd_write_command(LCD_SET_NORMAL_DISPLAY);
220 /* turn the display upside down (call lcd_update() afterwards) */
221 void lcd_set_flip(bool yesno)
223 if (yesno)
225 lcd_write_command(LCD_SET_SEGMENT_REMAP);
226 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION);
228 else
230 lcd_write_command(LCD_SET_SEGMENT_REMAP_INV);
231 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV);
235 #ifdef HAVE_LCD_ENABLE
236 void lcd_enable(bool enable)
238 if(display_on == enable)
239 return;
241 if( (display_on = enable) ) /* simple '=' is not a typo ! */
243 #ifdef SANSA_CLIP
244 /* Enable DC-DC AS3525 for some Clip v1 that need it */
245 ascodec_write(AS3514_DCDC15, 1);
246 #endif
248 lcd_write_command(LCD_SET_DISPLAY_ON);
249 send_event(LCD_EVENT_ACTIVATION, NULL);
251 else {
252 lcd_write_command(LCD_SET_DISPLAY_OFF);
254 #ifdef SANSA_CLIP
255 /* Disable DC-DC AS3525 */
256 ascodec_write(AS3514_DCDC15, 0);
257 #endif
261 bool lcd_active(void)
263 return display_on;
266 #endif
268 /* LCD init, largely based on what OF does */
269 void lcd_init_device(void)
271 int i;
272 #define LCD_FULLSCREEN (128+4)
273 fb_data p_bytes[LCD_FULLSCREEN]; /* framebuffer used to clear the screen */
275 lcd_hw_init();
277 #if defined(SANSA_CLIP)
278 GPIOA_DIR |= 0x33; /* pins 5:4 and 1:0 out */
279 GPIOB_DIR |= 0x40; /* pin 6 out */
281 GPIOA_PIN(1) = (1<<1);
282 GPIOA_PIN(0) = (1<<0);
283 GPIOA_PIN(4) = 0;
284 GPIOB_PIN(6) = (1<<6);
285 #elif defined(SANSA_CLIPV2)
286 GPIOB_DIR |= (1<<2)|(1<<5);
287 GPIOB_PIN(5) = (1<<5);
288 #elif defined(SANSA_CLIPPLUS)
289 GPIOA_DIR |= (1<<5);
290 GPIOB_DIR |= (1<<2) | (1<<7);
291 GPIOB_PIN(7) = 0;
292 GPIOA_PIN(5) = (1<<5);
293 #endif
295 /* Set display clock (divide ratio = 1) and oscillator frequency (1) */
296 lcd_write_command(LCD_SET_DISPLAY_CLOCK_AND_OSC_FREQ);
297 lcd_write_command(0x10);
299 /* Set VCOM deselect level to 0.76V */
300 lcd_write_command(LCD_SET_VCOM_DESELECT_LEVEL);
301 lcd_write_command(0x34);
303 /* Set pre-charge period (p1period is 2 dclk and p2period is 5 dclk) */
304 lcd_write_command(LCD_SET_PRECHARGE_PERIOD);
305 lcd_write_command(0x25);
307 /* Set contrast register to 12% */
308 lcd_set_contrast(lcd_default_contrast());
310 /* Disable DC-DC */
311 lcd_write_command(LCD_SET_DC_DC);
312 lcd_write_command(LCD_SET_DC_DC_PART2/*|0*/);
314 /* Set starting line as 0 */
315 lcd_write_command(LCD_SET_DISPLAY_START_LINE /*|(0 & 0x3f)*/);
317 /* Column 131 is remapped to SEG0 */
318 lcd_write_command(LCD_SET_SEGMENT_REMAP_INV);
320 /* Invert COM scan direction (N-1 to 0) */
321 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV);
323 /* Set normal display mode (not every pixel ON) */
324 lcd_write_command(LCD_SET_ENTIRE_DISPLAY_OFF);
326 /* Set normal display mode (not inverted) */
327 lcd_write_command(LCD_SET_NORMAL_DISPLAY);
329 /* Clear whole framebuffer, including "overscan"
330 * We don't need to handle that out of screen columns in lcd_clear_display()
331 * since we will never write into it anymore
333 lcd_write_command (LCD_SET_HIGHER_COLUMN_ADDRESS /*| 0*/);
334 lcd_write_command (LCD_SET_LOWER_COLUMN_ADDRESS /*| 0*/);
336 memset(p_bytes, 0, sizeof(p_bytes)); /* fills with 0 : pixel off */
338 for(i = 0; i < 8; i++)
340 lcd_write_command (LCD_SET_PAGE_ADDRESS | (i /*& 0xf*/));
341 lcd_write_data(p_bytes, LCD_FULLSCREEN /* overscan */);
344 lcd_enable(true);
346 lcd_update();
349 /*** Update functions ***/
351 /* Performance function that works with an external buffer
352 note that by and bheight are in 8-pixel units! */
353 void lcd_blit_mono(const unsigned char *data, int x, int by, int width,
354 int bheight, int stride)
356 if(!display_on)
357 return;
359 /* Copy display bitmap to hardware */
360 while (bheight--)
362 lcd_write_command (LCD_CNTL_PAGE | (by++ & 0xf));
363 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2)>>4) & 0xf));
364 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
366 lcd_write_data(data, width);
367 data += stride;
371 /* Helper function for lcd_grey_phase_blit(). */
372 void lcd_grey_data(unsigned char *values, unsigned char *phases, int count);
374 /* Performance function that works with an external buffer
375 note that by and bheight are in 8-pixel units! */
376 void lcd_blit_grey_phase(unsigned char *values, unsigned char *phases,
377 int x, int by, int width, int bheight, int stride)
379 if(!display_on)
380 return;
382 stride <<= 3; /* 8 pixels per block */
383 /* Copy display bitmap to hardware */
384 while (bheight--)
386 lcd_write_command (LCD_CNTL_PAGE | (by++ & 0xf));
387 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2)>>4) & 0xf));
388 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
390 lcd_grey_data(values, phases, width);
392 values += stride;
393 phases += stride;
397 /* Update the display.
398 This must be called after all other LCD functions that change the display. */
399 void lcd_update(void) ICODE_ATTR;
400 void lcd_update(void)
402 int y;
404 if(!display_on)
405 return;
407 /* Copy display bitmap to hardware */
408 for (y = 0; y < LCD_FBHEIGHT; y++)
410 lcd_write_command (LCD_CNTL_PAGE | (y & 0xf));
411 lcd_write_command (LCD_CNTL_HIGHCOL | ((2 >> 4) & 0xf));
412 lcd_write_command (LCD_CNTL_LOWCOL | (2 & 0xf));
414 lcd_write_data (lcd_framebuffer[y], LCD_WIDTH);
418 /* Update a fraction of the display. */
419 void lcd_update_rect(int, int, int, int) ICODE_ATTR;
420 void lcd_update_rect(int x, int y, int width, int height)
422 int ymax;
424 if(!display_on)
425 return;
427 /* The Y coordinates have to work on even 8 pixel rows */
428 ymax = (y + height-1) >> 3;
429 y >>= 3;
431 if(x + width > LCD_WIDTH)
432 width = LCD_WIDTH - x;
433 if (width <= 0)
434 return; /* nothing left to do, 0 is harmful to lcd_write_data() */
435 if(ymax >= LCD_FBHEIGHT)
436 ymax = LCD_FBHEIGHT-1;
438 /* Copy specified rectange bitmap to hardware */
439 for (; y <= ymax; y++)
441 lcd_write_command (LCD_CNTL_PAGE | (y & 0xf));
442 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2) >> 4) & 0xf));
443 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
445 lcd_write_data (&lcd_framebuffer[y][x], width);