Added battery profile change to correct file, removed unused powermgmt-as3525.c
[kugel-rb.git] / firmware / target / arm / as3525 / lcd-ssd1303.c
blob09f2638ff7781e9b665b4f0aeacd254dd903134d
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 "kernel.h"
26 #include "lcd.h"
27 #include "system.h"
28 #include "cpu.h"
29 #include "string.h"
31 #include "lcd-clip.h"
33 /*** definitions ***/
35 #define LCD_SET_LOWER_COLUMN_ADDRESS ((char)0x00)
36 #define LCD_SET_HIGHER_COLUMN_ADDRESS ((char)0x10)
37 #define LCD_SET_DISPLAY_START_LINE ((char)0x40)
38 #define LCD_SET_CONTRAST_CONTROL_REGISTER ((char)0x81)
39 #define LCD_SET_SEGMENT_REMAP ((char)0xA0)
40 #define LCD_SET_SEGMENT_REMAP_INV ((char)0xA1)
41 #define LCD_SET_ENTIRE_DISPLAY_OFF ((char)0xA4)
42 #define LCD_SET_ENTIRE_DISPLAY_ON ((char)0xA5)
43 #define LCD_SET_NORMAL_DISPLAY ((char)0xA6)
44 #define LCD_SET_REVERSE_DISPLAY ((char)0xA7)
45 #define LCD_SET_MULTIPLEX_RATIO ((char)0xA8)
46 #define LCD_SET_DC_DC ((char)0xAD)
47 #define LCD_SET_DC_DC_PART2 ((char)0x8A)
48 #define LCD_SET_DISPLAY_OFF ((char)0xAE)
49 #define LCD_SET_DISPLAY_ON ((char)0xAF)
50 #define LCD_SET_PAGE_ADDRESS ((char)0xB0)
51 #define LCD_SET_COM_OUTPUT_SCAN_DIRECTION ((char)0xC0)
52 #define LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV ((char)0xC8)
53 #define LCD_SET_DISPLAY_CLOCK_AND_OSC_FREQ ((char)0xD5)
54 #define LCD_SET_VCOM_DESELECT_LEVEL ((char)0xDB)
55 #define LCD_SET_PRECHARGE_PERIOD ((char)0xD9)
56 #define LCD_NOP ((char)0xE3)
58 /* LCD command codes */
59 #define LCD_CNTL_CONTRAST 0x81 /* Contrast */
60 #define LCD_CNTL_OUTSCAN 0xc8 /* Output scan direction */
61 #define LCD_CNTL_SEGREMAP 0xa1 /* Segment remap */
62 #define LCD_CNTL_DISPON 0xaf /* Display on */
64 #define LCD_CNTL_PAGE 0xb0 /* Page address */
65 #define LCD_CNTL_HIGHCOL 0x10 /* Upper column address */
66 #define LCD_CNTL_LOWCOL 0x00 /* Lower column address */
68 /** globals **/
70 static bool display_on; /* used by lcd_enable */
72 /*** hardware configuration ***/
74 int lcd_default_contrast(void)
76 return DEFAULT_CONTRAST_SETTING;
79 void lcd_set_contrast(int val)
81 lcd_write_command(LCD_CNTL_CONTRAST);
82 lcd_write_command(val);
85 void lcd_set_invert_display(bool yesno)
87 if (yesno)
88 lcd_write_command(LCD_SET_REVERSE_DISPLAY);
89 else
90 lcd_write_command(LCD_SET_NORMAL_DISPLAY);
93 /* turn the display upside down (call lcd_update() afterwards) */
94 void lcd_set_flip(bool yesno)
96 if (yesno)
98 lcd_write_command(LCD_SET_SEGMENT_REMAP);
99 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION);
101 else
103 lcd_write_command(LCD_SET_SEGMENT_REMAP_INV);
104 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV);
108 #ifdef HAVE_LCD_ENABLE
109 void lcd_enable(bool enable)
111 if(display_on == enable)
112 return;
114 if( (display_on = enable) ) /* simple '=' is not a typo ! */
116 lcd_enable_power(enable);
117 lcd_write_command(LCD_SET_DISPLAY_ON);
118 send_event(LCD_EVENT_ACTIVATION, NULL);
120 else
122 lcd_write_command(LCD_SET_DISPLAY_OFF);
123 lcd_enable_power(enable);
127 bool lcd_active(void)
129 return display_on;
132 #endif
134 /* LCD init, largely based on what OF does */
135 void lcd_init_device(void)
137 int i;
138 #define LCD_FULLSCREEN (128+4)
139 fb_data p_bytes[LCD_FULLSCREEN]; /* framebuffer used to clear the screen */
141 lcd_hw_init();
143 /* Set display clock (divide ratio = 1) and oscillator frequency (1) */
144 lcd_write_command(LCD_SET_DISPLAY_CLOCK_AND_OSC_FREQ);
145 lcd_write_command(0x10);
147 /* Set VCOM deselect level to 0.76V */
148 lcd_write_command(LCD_SET_VCOM_DESELECT_LEVEL);
149 lcd_write_command(0x34);
151 /* Set pre-charge period (p1period is 2 dclk and p2period is 5 dclk) */
152 lcd_write_command(LCD_SET_PRECHARGE_PERIOD);
153 lcd_write_command(0x25);
155 /* Set contrast register to 12% */
156 lcd_set_contrast(lcd_default_contrast());
158 /* Disable DC-DC */
159 lcd_write_command(LCD_SET_DC_DC);
160 lcd_write_command(LCD_SET_DC_DC_PART2/*|0*/);
162 /* Set starting line as 0 */
163 lcd_write_command(LCD_SET_DISPLAY_START_LINE /*|(0 & 0x3f)*/);
165 /* Column 131 is remapped to SEG0 */
166 lcd_write_command(LCD_SET_SEGMENT_REMAP_INV);
168 /* Invert COM scan direction (N-1 to 0) */
169 lcd_write_command(LCD_SET_COM_OUTPUT_SCAN_DIRECTION_INV);
171 /* Set normal display mode (not every pixel ON) */
172 lcd_write_command(LCD_SET_ENTIRE_DISPLAY_OFF);
174 /* Set normal display mode (not inverted) */
175 lcd_write_command(LCD_SET_NORMAL_DISPLAY);
177 /* Clear whole framebuffer, including "overscan"
178 * We don't need to handle that out of screen columns in lcd_clear_display()
179 * since we will never write into it anymore
181 lcd_write_command (LCD_SET_HIGHER_COLUMN_ADDRESS /*| 0*/);
182 lcd_write_command (LCD_SET_LOWER_COLUMN_ADDRESS /*| 0*/);
184 memset(p_bytes, 0, sizeof(p_bytes)); /* fills with 0 : pixel off */
186 for(i = 0; i < 8; i++)
188 lcd_write_command (LCD_SET_PAGE_ADDRESS | (i /*& 0xf*/));
189 lcd_write_data(p_bytes, LCD_FULLSCREEN /* overscan */);
192 lcd_enable(true);
194 lcd_update();
197 /*** Update functions ***/
199 /* Performance function that works with an external buffer
200 note that by and bheight are in 8-pixel units! */
201 void lcd_blit_mono(const unsigned char *data, int x, int by, int width,
202 int bheight, int stride)
204 if(!display_on)
205 return;
207 /* Copy display bitmap to hardware */
208 while (bheight--)
210 lcd_write_command (LCD_CNTL_PAGE | (by++ & 0xf));
211 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2)>>4) & 0xf));
212 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
214 lcd_write_data(data, width);
215 data += stride;
220 #ifndef BOOTLOADER
221 /* Helper function for lcd_grey_phase_blit(). */
222 void lcd_grey_data(unsigned char *values, unsigned char *phases, int count);
224 /* Performance function that works with an external buffer
225 note that by and bheight are in 8-pixel units! */
226 void lcd_blit_grey_phase(unsigned char *values, unsigned char *phases,
227 int x, int by, int width, int bheight, int stride)
229 if(!display_on)
230 return;
232 stride <<= 3; /* 8 pixels per block */
233 /* Copy display bitmap to hardware */
234 while (bheight--)
236 lcd_write_command (LCD_CNTL_PAGE | (by++ & 0xf));
237 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2)>>4) & 0xf));
238 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
240 lcd_grey_data(values, phases, width);
242 values += stride;
243 phases += stride;
247 #endif
250 /* Update the display.
251 This must be called after all other LCD functions that change the display. */
252 void lcd_update(void) ICODE_ATTR;
253 void lcd_update(void)
255 int y;
257 if(!display_on)
258 return;
260 /* Copy display bitmap to hardware */
261 for (y = 0; y < LCD_FBHEIGHT; y++)
263 lcd_write_command (LCD_CNTL_PAGE | (y & 0xf));
264 lcd_write_command (LCD_CNTL_HIGHCOL | ((2 >> 4) & 0xf));
265 lcd_write_command (LCD_CNTL_LOWCOL | (2 & 0xf));
267 lcd_write_data (lcd_framebuffer[y], LCD_WIDTH);
271 /* Update a fraction of the display. */
272 void lcd_update_rect(int, int, int, int) ICODE_ATTR;
273 void lcd_update_rect(int x, int y, int width, int height)
275 int ymax;
277 if(!display_on)
278 return;
280 /* The Y coordinates have to work on even 8 pixel rows */
281 ymax = (y + height-1) >> 3;
282 y >>= 3;
284 if(x + width > LCD_WIDTH)
285 width = LCD_WIDTH - x;
286 if (width <= 0)
287 return; /* nothing left to do, 0 is harmful to lcd_write_data() */
288 if(ymax >= LCD_FBHEIGHT)
289 ymax = LCD_FBHEIGHT-1;
291 /* Copy specified rectange bitmap to hardware */
292 for (; y <= ymax; y++)
294 lcd_write_command (LCD_CNTL_PAGE | (y & 0xf));
295 lcd_write_command (LCD_CNTL_HIGHCOL | (((x+2) >> 4) & 0xf));
296 lcd_write_command (LCD_CNTL_LOWCOL | ((x+2) & 0xf));
298 lcd_write_data (&lcd_framebuffer[y][x], width);