FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-clip / lcd-ssd1303.c
blobedc1d9810ed0436748a2037e1de55e76f6db12bf
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 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 #include "as3525.h"
34 #include "ascodec.h"
36 /*** definitions ***/
38 #define LCD_SET_LOWER_COLUMN_ADDRESS ((char)0x00)
39 #define LCD_SET_HIGHER_COLUMN_ADDRESS ((char)0x10)
40 #define LCD_SET_DISPLAY_START_LINE ((char)0x40)
41 #define LCD_SET_CONTRAST_CONTROL_REGISTER ((char)0x81)
42 #define LCD_SET_SEGMENT_REMAP ((char)0xA0)
43 #define LCD_SET_SEGMENT_REMAP_INV ((char)0xA1)
44 #define LCD_SET_ENTIRE_DISPLAY_OFF ((char)0xA4)
45 #define LCD_SET_ENTIRE_DISPLAY_ON ((char)0xA5)
46 #define LCD_SET_NORMAL_DISPLAY ((char)0xA6)
47 #define LCD_SET_REVERSE_DISPLAY ((char)0xA7)
48 #define LCD_SET_MULTIPLEX_RATIO ((char)0xA8)
49 #define LCD_SET_DC_DC ((char)0xAD)
50 #define LCD_SET_DC_DC_PART2 ((char)0x8A)
51 #define LCD_SET_DISPLAY_OFF ((char)0xAE)
52 #define LCD_SET_DISPLAY_ON ((char)0xAF)
53 #define LCD_SET_PAGE_ADDRESS ((char)0xB0)
54 #define LCD_SET_COM_OUTPUT_SCAN_DIRECTION ((char)0xC0)
55 #define LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV ((char)0xC8)
56 #define LCD_SET_DISPLAY_CLOCK_AND_OSC_FREQ ((char)0xD5)
57 #define LCD_SET_VCOM_DESELECT_LEVEL ((char)0xDB)
58 #define LCD_SET_PRECHARGE_PERIOD ((char)0xD9)
59 #define LCD_NOP ((char)0xE3)
61 /* LCD command codes */
62 #define LCD_CNTL_CONTRAST 0x81 /* Contrast */
63 #define LCD_CNTL_OUTSCAN 0xc8 /* Output scan direction */
64 #define LCD_CNTL_SEGREMAP 0xa1 /* Segment remap */
65 #define LCD_CNTL_DISPON 0xaf /* Display on */
67 #define LCD_CNTL_PAGE 0xb0 /* Page address */
68 #define LCD_CNTL_HIGHCOL 0x10 /* Upper column address */
69 #define LCD_CNTL_LOWCOL 0x00 /* Lower column address */
71 /* DBOP initialisation, do what OF does */
72 static void ams3525_dbop_init(void)
74 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
76 GPIOB_AFSEL = 0x08; /* DBOP on pin 3 */
77 GPIOC_AFSEL = 0x0f; /* DBOP on pins 3:0 */
79 DBOP_CTRL = 0x51008;
80 DBOP_TIMPOL_01 = 0x6E167;
81 DBOP_TIMPOL_23 = 0xA167E06F;
84 void lcd_write_command(int byte)
86 volatile int i = 0;
87 while(i<1) i++;
88 /* unset D/C# (data or command) */
89 GPIOA_PIN(5) = 0;
91 /* Write command */
92 /* Only bits 15:12 and 3:0 of DBOP_DOUT are meaningful */
93 DBOP_DOUT = (byte << 8) | byte;
95 /* While push fifo is not empty */
96 while ((DBOP_STAT & (1<<10)) == 0)
100 void lcd_write_data(const fb_data* p_bytes, int count)
102 volatile int i = 0;
103 while(i<1) i++;
104 /* set D/C# (data or command) */
105 GPIOA_PIN(5) = (1<<5);
107 while (count--)
109 /* Write pixels */
110 /* Only bits 15:12 and 3:0 of DBOP_DOUT are meaningful */
111 DBOP_DOUT = (*p_bytes << 8) | *p_bytes;
113 p_bytes++; /* next packed pixels */
115 /* Wait if push fifo is full */
116 while ((DBOP_STAT & (1<<6)) != 0);
118 /* While push fifo is not empty */
119 while ((DBOP_STAT & (1<<10)) == 0);
123 /** globals **/
125 static bool display_on; /* used by lcd_enable */
127 /*** hardware configuration ***/
129 int lcd_default_contrast(void)
131 return DEFAULT_CONTRAST_SETTING;
134 void lcd_set_contrast(int val)
136 lcd_write_command(LCD_CNTL_CONTRAST);
137 lcd_write_command(val);
140 void lcd_set_invert_display(bool yesno)
142 if (yesno)
143 lcd_write_command(LCD_SET_REVERSE_DISPLAY);
144 else
145 lcd_write_command(LCD_SET_NORMAL_DISPLAY);
148 /* turn the display upside down (call lcd_update() afterwards) */
149 void lcd_set_flip(bool yesno)
151 if (yesno)
153 lcd_write_command(LCD_SET_SEGMENT_REMAP);
154 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION);
156 else
158 lcd_write_command(LCD_SET_SEGMENT_REMAP_INV);
159 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV);
163 #ifdef HAVE_LCD_ENABLE
164 void lcd_enable(bool enable)
166 if(display_on == enable)
167 return;
169 if( (display_on = enable) ) /* simple '=' is not a typo ! */
171 /* Enable DC-DC AS3525 for some Clip v1 that need it */
172 ascodec_write(AS3514_DCDC15, 1);
174 lcd_write_command(LCD_SET_DISPLAY_ON);
175 lcd_activation_call_hook();
177 else {
178 lcd_write_command(LCD_SET_DISPLAY_OFF);
180 /* Disable DC-DC AS3525 */
181 ascodec_write(AS3514_DCDC15, 0);
185 bool lcd_active(void)
187 return display_on;
190 #endif
192 /* LCD init, largely based on what OF does */
193 void lcd_init_device(void)
195 int i;
196 #define LCD_FULLSCREEN (128+4)
197 fb_data p_bytes[LCD_FULLSCREEN]; /* framebuffer used to clear the screen */
199 ams3525_dbop_init();
201 GPIOA_DIR |= 0x33; /* pins 5:4 and 1:0 out */
202 GPIOB_DIR |= 0x40; /* pin 6 out */
204 GPIOA_PIN(1) = (1<<1);
205 GPIOA_PIN(0) = (1<<0);
206 GPIOA_PIN(4) = 0;
207 GPIOB_PIN(6) = (1<<6);
209 /* Set display clock (divide ratio = 1) and oscillator frequency (1) */
210 lcd_write_command(LCD_SET_DISPLAY_CLOCK_AND_OSC_FREQ);
211 lcd_write_command(0x10);
213 /* Set VCOM deselect level to 0.76V */
214 lcd_write_command(LCD_SET_VCOM_DESELECT_LEVEL);
215 lcd_write_command(0x34);
217 /* Set pre-charge period (p1period is 2 dclk and p2period is 5 dclk) */
218 lcd_write_command(LCD_SET_PRECHARGE_PERIOD);
219 lcd_write_command(0x25);
221 /* Set contrast register to 12% */
222 lcd_set_contrast(lcd_default_contrast());
224 /* Disable DC-DC */
225 lcd_write_command(LCD_SET_DC_DC);
226 lcd_write_command(LCD_SET_DC_DC_PART2/*|0*/);
228 /* Set starting line as 0 */
229 lcd_write_command(LCD_SET_DISPLAY_START_LINE /*|(0 & 0x3f)*/);
231 /* Column 131 is remapped to SEG0 */
232 lcd_write_command(LCD_SET_SEGMENT_REMAP_INV);
234 /* Invert COM scan direction (N-1 to 0) */
235 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV);
237 /* Set normal display mode (not every pixel ON) */
238 lcd_write_command(LCD_SET_ENTIRE_DISPLAY_OFF);
240 /* Set normal display mode (not inverted) */
241 lcd_write_command(LCD_SET_NORMAL_DISPLAY);
243 /* Clear whole framebuffer, including "overscan"
244 * We don't need to handle that out of screen columns in lcd_clear_display()
245 * since we will never write into it anymore
247 lcd_write_command (LCD_SET_HIGHER_COLUMN_ADDRESS /*| 0*/);
248 lcd_write_command (LCD_SET_LOWER_COLUMN_ADDRESS /*| 0*/);
250 memset(p_bytes, 0, sizeof(p_bytes)); /* fills with 0 : pixel off */
252 for(i = 0; i < 8; i++)
254 lcd_write_command (LCD_SET_PAGE_ADDRESS | (i /*& 0xf*/));
255 lcd_write_data(p_bytes, LCD_FULLSCREEN /* overscan */);
258 lcd_enable(true);
260 lcd_update();
263 /*** Update functions ***/
265 /* Performance function that works with an external buffer
266 note that by and bheight are in 8-pixel units! */
267 void lcd_blit_mono(const unsigned char *data, int x, int by, int width,
268 int bheight, int stride)
270 if(!display_on)
271 return;
273 /* Copy display bitmap to hardware */
274 while (bheight--)
276 lcd_write_command (LCD_CNTL_PAGE | (by++ & 0xf));
277 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2)>>4) & 0xf));
278 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
280 lcd_write_data(data, width);
281 data += stride;
285 /* Helper function for lcd_grey_phase_blit(). */
286 void lcd_grey_data(unsigned char *values, unsigned char *phases, int count);
288 /* Performance function that works with an external buffer
289 note that by and bheight are in 8-pixel units! */
290 void lcd_blit_grey_phase(unsigned char *values, unsigned char *phases,
291 int x, int by, int width, int bheight, int stride)
293 if(!display_on)
294 return;
296 stride <<= 3; /* 8 pixels per block */
297 /* Copy display bitmap to hardware */
298 while (bheight--)
300 lcd_write_command (LCD_CNTL_PAGE | (by++ & 0xf));
301 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2)>>4) & 0xf));
302 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
304 lcd_grey_data(values, phases, width);
306 values += stride;
307 phases += stride;
311 /* Update the display.
312 This must be called after all other LCD functions that change the display. */
313 void lcd_update(void) ICODE_ATTR;
314 void lcd_update(void)
316 int y;
318 if(!display_on)
319 return;
321 /* Copy display bitmap to hardware */
322 for (y = 0; y < LCD_FBHEIGHT; y++)
324 lcd_write_command (LCD_CNTL_PAGE | (y & 0xf));
325 lcd_write_command (LCD_CNTL_HIGHCOL | ((2 >> 4) & 0xf));
326 lcd_write_command (LCD_CNTL_LOWCOL | (2 & 0xf));
328 lcd_write_data (lcd_framebuffer[y], LCD_WIDTH);
332 /* Update a fraction of the display. */
333 void lcd_update_rect(int, int, int, int) ICODE_ATTR;
334 void lcd_update_rect(int x, int y, int width, int height)
336 int ymax;
338 if(!display_on)
339 return;
341 /* The Y coordinates have to work on even 8 pixel rows */
342 ymax = (y + height-1) >> 3;
343 y >>= 3;
345 if(x + width > LCD_WIDTH)
346 width = LCD_WIDTH - x;
347 if (width <= 0)
348 return; /* nothing left to do, 0 is harmful to lcd_write_data() */
349 if(ymax >= LCD_FBHEIGHT)
350 ymax = LCD_FBHEIGHT-1;
352 /* Copy specified rectange bitmap to hardware */
353 for (; y <= ymax; y++)
355 lcd_write_command (LCD_CNTL_PAGE | (y & 0xf));
356 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2) >> 4) & 0xf));
357 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
359 lcd_write_data (&lcd_framebuffer[y][x], width);