summaryrefslogtreecommitdiff
path: root/firmware/target/coldfire/iaudio/m3/lcd-m3.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/coldfire/iaudio/m3/lcd-m3.c')
-rw-r--r--firmware/target/coldfire/iaudio/m3/lcd-m3.c496
1 files changed, 496 insertions, 0 deletions
diff --git a/firmware/target/coldfire/iaudio/m3/lcd-m3.c b/firmware/target/coldfire/iaudio/m3/lcd-m3.c
new file mode 100644
index 0000000000..ac60006565
--- /dev/null
+++ b/firmware/target/coldfire/iaudio/m3/lcd-m3.c
@@ -0,0 +1,496 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include "config.h"
21#include "system.h"
22#include "file.h"
23#include "lcd.h"
24#include "scroll_engine.h"
25
26/* The LCD in the iAudio M3/M5/X5 remote control is a Tomato LSI 0350 */
27
28#define LCD_SET_DUTY_RATIO 0x48
29#define LCD_SELECT_ADC 0xa0
30#define LCD_SELECT_SHL 0xc0
31#define LCD_SET_COM0 0x44
32#define LCD_OSC_ON 0xab
33#define LCD_SELECT_DCDC 0x64
34#define LCD_SELECT_RES 0x20
35#define LCD_SET_VOLUME 0x81
36#define LCD_SET_BIAS 0x50
37#define LCD_CONTROL_POWER 0x28
38#define LCD_DISPLAY_ON 0xae
39#define LCD_SET_INITLINE 0x40
40#define LCD_SET_COLUMN 0x10
41#define LCD_SET_PAGE 0xb0
42#define LCD_SET_GRAY 0x88
43#define LCD_SET_PWM_FRC 0x90
44#define LCD_SET_POWER_SAVE 0xa8
45#define LCD_REVERSE 0xa6
46
47#define CS_LO and_l(~0x00010000, &GPIO1_OUT)
48#define CS_HI or_l( 0x00010000, &GPIO1_OUT)
49#define CLK_LO and_l(~0x20000000, &GPIO_OUT)
50#define CLK_HI or_l( 0x20000000, &GPIO_OUT)
51#define DATA_LO and_l(~0x04000000, &GPIO_OUT)
52#define DATA_HI or_l( 0x04000000, &GPIO_OUT)
53#define RS_LO and_l(~0x00001000, &GPIO1_OUT)
54#define RS_HI or_l( 0x00001000, &GPIO1_OUT)
55
56/* cached settings values */
57static bool cached_invert = false;
58static bool cached_flip = false;
59static int cached_contrast = DEFAULT_CONTRAST_SETTING;
60
61bool initialized = false;
62
63static void lcd_tick(void);
64
65/* Standard low-level byte writer. Requires CLK high on entry */
66static inline void _write_byte(unsigned data)
67{
68 asm volatile (
69 "move.l (%[gpo0]), %%d0 \n" /* Get current state of data line */
70 "and.l %[dbit], %%d0 \n"
71 "beq.s 1f \n" /* and set it as previous-state bit */
72 "bset #8, %[data] \n"
73 "1: \n"
74 "move.l %[data], %%d0 \n" /* Compute the 'bit derivative', i.e. a value */
75 "lsr.l #1, %%d0 \n" /* with 1's where the data changes from the */
76 "eor.l %%d0, %[data] \n" /* previous state, and 0's where it doesn't */
77 "swap %[data] \n" /* Shift data to upper byte */
78 "lsl.l #8, %[data] \n"
79
80 "move.l %[cbit], %%d1 \n" /* Prepare mask for flipping CLK */
81 "or.l %[dbit], %%d1 \n" /* and DATA at once */
82
83 "lsl.l #1,%[data] \n" /* Shift out MSB */
84 "bcc.s 1f \n"
85 "eor.l %%d1, (%[gpo0]) \n" /* 1: Flip both CLK and DATA */
86 ".word 0x51fa \n" /* (trapf.w - shadow next insn) */
87 "1: \n"
88 "eor.l %[cbit], (%[gpo0]) \n" /* else flip CLK only */
89 "eor.l %[cbit], (%[gpo0]) \n" /* Flip CLK again */
90
91 "lsl.l #1,%[data] \n" /* ..unrolled.. */
92 "bcc.s 1f \n"
93 "eor.l %%d1, (%[gpo0]) \n"
94 ".word 0x51fa \n"
95 "1: \n"
96 "eor.l %[cbit], (%[gpo0]) \n"
97 "eor.l %[cbit], (%[gpo0]) \n"
98
99 "lsl.l #1,%[data] \n"
100 "bcc.s 1f \n"
101 "eor.l %%d1, (%[gpo0]) \n"
102 ".word 0x51fa \n"
103 "1: \n"
104 "eor.l %[cbit], (%[gpo0]) \n"
105 "eor.l %[cbit], (%[gpo0]) \n"
106
107 "lsl.l #1,%[data] \n"
108 "bcc.s 1f \n"
109 "eor.l %%d1, (%[gpo0]) \n"
110 ".word 0x51fa \n"
111 "1: \n"
112 "eor.l %[cbit], (%[gpo0]) \n"
113 "eor.l %[cbit], (%[gpo0]) \n"
114
115 "lsl.l #1,%[data] \n"
116 "bcc.s 1f \n"
117 "eor.l %%d1, (%[gpo0]) \n"
118 ".word 0x51fa \n"
119 "1: \n"
120 "eor.l %[cbit], (%[gpo0]) \n"
121 "eor.l %[cbit], (%[gpo0]) \n"
122
123 "lsl.l #1,%[data] \n"
124 "bcc.s 1f \n"
125 "eor.l %%d1, (%[gpo0]) \n"
126 ".word 0x51fa \n"
127 "1: \n"
128 "eor.l %[cbit], (%[gpo0]) \n"
129 "eor.l %[cbit], (%[gpo0]) \n"
130
131 "lsl.l #1,%[data] \n"
132 "bcc.s 1f \n"
133 "eor.l %%d1, (%[gpo0]) \n"
134 ".word 0x51fa \n"
135 "1: \n"
136 "eor.l %[cbit], (%[gpo0]) \n"
137 "eor.l %[cbit], (%[gpo0]) \n"
138
139 "lsl.l #1,%[data] \n"
140 "bcc.s 1f \n"
141 "eor.l %%d1, (%[gpo0]) \n"
142 ".word 0x51fa \n"
143 "1: \n"
144 "eor.l %[cbit], (%[gpo0]) \n"
145 "eor.l %[cbit], (%[gpo0]) \n"
146 : /* outputs */
147 [data]"+d"(data)
148 : /* inputs */
149 [gpo0]"a"(&GPIO_OUT),
150 [cbit]"d"(0x20000000),
151 [dbit]"d"(0x04000000)
152 : /* clobbers */
153 "d0", "d1"
154 );
155}
156
157/* Fast low-level byte writer. Don't use with high CPU clock.
158 * Requires CLK high on entry */
159static inline void _write_fast(unsigned data)
160{
161 asm volatile (
162 "move.w %%sr,%%d3 \n" /* Get current interrupt level */
163 "move.w #0x2700,%%sr \n" /* Disable interrupts */
164
165 "move.l (%[gpo0]), %%d0 \n" /* Get current state of data port */
166 "move.l %%d0, %%d1 \n"
167 "and.l %[dbit], %%d1 \n" /* Check current state of data line */
168 "beq.s 1f \n" /* and set it as previous-state bit */
169 "bset #8, %[data] \n"
170 "1: \n"
171 "move.l %[data], %%d1 \n" /* Compute the 'bit derivative', i.e. a value */
172 "lsr.l #1, %%d1 \n" /* with 1's where the data changes from the */
173 "eor.l %%d1, %[data] \n" /* previous state, and 0's where it doesn't */
174 "swap %[data] \n" /* Shift data to upper byte */
175 "lsl.l #8, %[data] \n"
176
177 "move.l %%d0, %%d1 \n" /* precalculate opposite state of clock line */
178 "eor.l %[cbit], %%d1 \n"
179
180 "lsl.l #1,%[data] \n" /* Shift out MSB */
181 "bcc.s 1f \n"
182 "eor.l %[dbit], %%d0 \n" /* 1: Flip data bit */
183 "eor.l %[dbit], %%d1 \n" /* for both clock states */
184 "1: \n"
185 "move.l %%d1, (%[gpo0]) \n" /* Output new state and set CLK */
186 "move.l %%d0, (%[gpo0]) \n" /* reset CLK */
187
188 "lsl.l #1,%[data] \n" /* ..unrolled.. */
189 "bcc.s 1f \n"
190 "eor.l %[dbit], %%d0 \n"
191 "eor.l %[dbit], %%d1 \n"
192 "1: \n"
193 "move.l %%d1, (%[gpo0]) \n"
194 "move.l %%d0, (%[gpo0]) \n"
195
196 "lsl.l #1,%[data] \n"
197 "bcc.s 1f \n"
198 "eor.l %[dbit], %%d0 \n"
199 "eor.l %[dbit], %%d1 \n"
200 "1: \n"
201 "move.l %%d1, (%[gpo0]) \n"
202 "move.l %%d0, (%[gpo0]) \n"
203
204 "lsl.l #1,%[data] \n"
205 "bcc.s 1f \n"
206 "eor.l %[dbit], %%d0 \n"
207 "eor.l %[dbit], %%d1 \n"
208 "1: \n"
209 "move.l %%d1, (%[gpo0]) \n"
210 "move.l %%d0, (%[gpo0]) \n"
211
212 "lsl.l #1,%[data] \n"
213 "bcc.s 1f \n"
214 "eor.l %[dbit], %%d0 \n"
215 "eor.l %[dbit], %%d1 \n"
216 "1: \n"
217 "move.l %%d1, (%[gpo0]) \n"
218 "move.l %%d0, (%[gpo0]) \n"
219
220 "lsl.l #1,%[data] \n"
221 "bcc.s 1f \n"
222 "eor.l %[dbit], %%d0 \n"
223 "eor.l %[dbit], %%d1 \n"
224 "1: \n"
225 "move.l %%d1, (%[gpo0]) \n"
226 "move.l %%d0, (%[gpo0]) \n"
227
228 "lsl.l #1,%[data] \n"
229 "bcc.s 1f \n"
230 "eor.l %[dbit], %%d0 \n"
231 "eor.l %[dbit], %%d1 \n"
232 "1: \n"
233 "move.l %%d1, (%[gpo0]) \n"
234 "move.l %%d0, (%[gpo0]) \n"
235
236 "lsl.l #1,%[data] \n"
237 "bcc.s 1f \n"
238 "eor.l %[dbit], %%d0 \n"
239 "eor.l %[dbit], %%d1 \n"
240 "1: \n"
241 "move.l %%d1, (%[gpo0]) \n"
242 "move.l %%d0, (%[gpo0]) \n"
243
244 "move.w %%d3, %%sr \n" /* Restore interrupt level */
245 : /* outputs */
246 [data]"+d"(data)
247 : /* inputs */
248 [gpo0]"a"(&GPIO_OUT),
249 [cbit]"d"(0x20000000),
250 [dbit]"d"(0x04000000)
251 : /* clobbers */
252 "d0", "d1", "d2", "d3"
253 );
254}
255
256void lcd_write_command(int cmd)
257{
258 RS_LO;
259 CS_LO;
260 _write_byte(cmd);
261 CS_HI;
262}
263
264void lcd_write_command_e(int cmd, int data)
265{
266 RS_LO;
267 CS_LO;
268 _write_byte(cmd);
269 _write_byte(data);
270 CS_HI;
271}
272
273void lcd_write_data(const fb_data *p_words, int count)
274{
275 const unsigned char *p_bytes = (const unsigned char *)p_words;
276 const unsigned char *p_end = (const unsigned char *)(p_words + count);
277
278 RS_HI;
279 CS_LO;
280 if (cpu_frequency < 50000000)
281 {
282 while (p_bytes < p_end)
283 _write_fast(*p_bytes++);
284 }
285 else
286 {
287 while (p_bytes < p_end)
288 _write_byte(*p_bytes++);
289 }
290 CS_HI;
291}
292
293int lcd_default_contrast(void)
294{
295 return DEFAULT_CONTRAST_SETTING;
296}
297
298void lcdset_contrast(int val)
299{
300 if (val < 0)
301 val = 0;
302 else if (val > 63)
303 val = 63;
304
305 cached_contrast = val;
306 if (initialized)
307 lcd_write_command_e(LCD_SET_VOLUME, val);
308}
309
310bool remote_detect(void)
311{
312 return (GPIO_READ & 0x40000000) == 0;
313}
314
315void lcd_init_device(void)
316{
317 or_l(0x24000000, &GPIO_OUT);
318 or_l(0x24000000, &GPIO_ENABLE);
319 or_l(0x24000000, &GPIO_FUNCTION);
320
321 or_l(0x00011000, &GPIO1_OUT);
322 or_l(0x00011000, &GPIO1_ENABLE);
323 or_l(0x00011000, &GPIO1_FUNCTION);
324
325 and_l(~0x40000000, &GPIO_OUT);
326 and_l(~0x40000000, &GPIO_ENABLE);
327 or_l(0x40000000, &GPIO_FUNCTION);
328
329 lcd_clear_display();
330 tick_add_task(lcd_tick);
331}
332
333void lcd_on(void)
334{
335 CS_HI;
336 CLK_HI;
337 sleep(10);
338
339 lcd_write_command(LCD_SET_DUTY_RATIO);
340 lcd_write_command(0x70); /* 1/128 */
341
342 lcd_write_command(LCD_OSC_ON);
343
344 lcd_write_command(LCD_SELECT_DCDC | 2); /* DC/DC 5xboost */
345
346 lcd_write_command(LCD_SELECT_RES | 7); /* Regulator resistor: 7.2 */
347
348 lcd_write_command(LCD_SET_BIAS | 6); /* 1/11 */
349
350 lcd_write_command(LCD_CONTROL_POWER | 7); /* All circuits ON */
351
352 sleep(30);
353
354 lcd_write_command_e(LCD_SET_GRAY | 0, 0x00);
355 lcd_write_command_e(LCD_SET_GRAY | 1, 0x00);
356 lcd_write_command_e(LCD_SET_GRAY | 2, 0x0c);
357 lcd_write_command_e(LCD_SET_GRAY | 3, 0x00);
358 lcd_write_command_e(LCD_SET_GRAY | 4, 0xcc);
359 lcd_write_command_e(LCD_SET_GRAY | 5, 0x00);
360 lcd_write_command_e(LCD_SET_GRAY | 6, 0xcc);
361 lcd_write_command_e(LCD_SET_GRAY | 7, 0x0c);
362
363 lcd_write_command(LCD_SET_PWM_FRC | 6); /* 4FRC + 12PWM */
364
365 lcd_write_command(LCD_DISPLAY_ON | 1); /* display on */
366
367 initialized = true;
368
369 lcd_set_flip(cached_flip);
370 lcd_set_contrast(cached_contrast);
371 lcd_set_invert_display(cached_invert);
372}
373
374void lcd_off(void)
375{
376 initialized = false;
377 CS_HI;
378 RS_HI;
379}
380
381void lcd_poweroff(void)
382{
383 /* Set power save -> Power OFF (VDD - VSS) .. that's it */
384 if (initialized && remote_detect())
385 lcd_write_command(LCD_SET_POWER_SAVE | 1);
386}
387
388/* Monitor remote hotswap */
389static void lcd_tick(void)
390{
391 static bool last_status = false;
392 static int countdown = 0;
393 bool current_status;
394
395 current_status = remote_detect();
396
397 /* Only report when the status has changed */
398 if (current_status != last_status)
399 {
400 last_status = current_status;
401 countdown = current_status ? 20*HZ : 1;
402 }
403 else
404 {
405 /* Count down until it gets negative */
406 if (countdown >= 0)
407 countdown--;
408
409 if (current_status)
410 {
411 if (!(countdown % 48))
412 {
413 queue_broadcast(SYS_REMOTE_PLUGGED, 0);
414 }
415 }
416 else
417 {
418 if (countdown == 0)
419 {
420 queue_broadcast(SYS_REMOTE_UNPLUGGED, 0);
421 }
422 }
423 }
424}
425
426/* Update the display.
427 This must be called after all other LCD functions that change the display. */
428void lcd_update(void) ICODE_ATTR;
429void lcd_update(void)
430{
431 int y;
432 if(initialized) {
433 for(y = 0;y < LCD_FBHEIGHT;y++) {
434 /* Copy display bitmap to hardware.
435 The COM48-COM63 lines are not connected so we have to skip
436 them. Further, the column address doesn't wrap, so we
437 have to update one page at a time. */
438 lcd_write_command(LCD_SET_PAGE | (y>5?y+2:y));
439 lcd_write_command_e(LCD_SET_COLUMN | 0, 0);
440 lcd_write_data(lcd_framebuffer[y], LCD_WIDTH);
441 }
442 }
443}
444
445/* Update a fraction of the display. */
446void lcd_update_rect(int, int, int, int) ICODE_ATTR;
447void lcd_update_rect(int x, int y, int width, int height)
448{
449 if(initialized) {
450 int ymax;
451
452 /* The Y coordinates have to work on even 8 pixel rows */
453 ymax = (y + height-1) >> 3;
454 y >>= 3;
455
456 if(x + width > LCD_WIDTH)
457 width = LCD_WIDTH - x;
458 if (width <= 0)
459 return; /* nothing left to do, 0 is harmful to lcd_write_data() */
460 if(ymax >= LCD_FBHEIGHT)
461 ymax = LCD_FBHEIGHT-1;
462
463 /* Copy specified rectangle bitmap to hardware
464 COM48-COM63 are not connected, so we need to skip those */
465 for (; y <= ymax; y++)
466 {
467 lcd_write_command(LCD_SET_PAGE | ((y > 5?y + 2:y) & 0xf));
468 lcd_write_command_e(LCD_SET_COLUMN | ((x >> 4) & 0xf), x & 0xf);
469
470 lcd_write_data(&lcd_framebuffer[y][x], width);
471 }
472 }
473}
474
475void lcd_set_invert_display(bool yesno)
476{
477 cached_invert = yesno;
478 if(initialized)
479 lcd_write_command(LCD_REVERSE | yesno);
480}
481
482void lcd_set_flip(bool yesno)
483{
484 cached_flip = yesno;
485 if(initialized) {
486 if(yesno) {
487 lcd_write_command(LCD_SELECT_ADC | 0);
488 lcd_write_command(LCD_SELECT_SHL | 0);
489 lcd_write_command_e(LCD_SET_COM0, 16);
490 } else {
491 lcd_write_command(LCD_SELECT_ADC | 1);
492 lcd_write_command(LCD_SELECT_SHL | 8);
493 lcd_write_command_e(LCD_SET_COM0, 0);
494 }
495 }
496}