blob: 707250d24962e8d61522940cf1b93ae7eb219299 [file] [log] [blame]
wdenk5a8c51c2004-04-15 21:16:42 +00001/*
2 * (C) Copyright 2003
3 *
4 * Pantelis Antoniou <panto@intracom.gr>
5 * Intracom S.A.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27#include <watchdog.h>
28
29#include <sed156x.h>
30
wdenk5a8c51c2004-04-15 21:16:42 +000031/* configure according to the selected display */
32#if defined(CONFIG_SED156X_PG12864Q)
33#define LCD_WIDTH 128
34#define LCD_HEIGHT 64
35#define LCD_LINES 64
36#define LCD_PAGES 9
37#define LCD_COLUMNS 132
38#else
39#error Unsupported SED156x configuration
40#endif
41
42/* include the font data */
43#include <video_font.h>
44
45#if VIDEO_FONT_WIDTH != 8 || VIDEO_FONT_HEIGHT != 16
46#error Expecting VIDEO_FONT_WIDTH == 8 && VIDEO_FONT_HEIGHT == 16
47#endif
48
49#define LCD_BYTE_WIDTH (LCD_WIDTH / 8)
50#define VIDEO_FONT_BYTE_WIDTH (VIDEO_FONT_WIDTH / 8)
51
52#define LCD_TEXT_WIDTH (LCD_WIDTH / VIDEO_FONT_WIDTH)
53#define LCD_TEXT_HEIGHT (LCD_HEIGHT / VIDEO_FONT_HEIGHT)
54
55#define LCD_BYTE_LINESZ (LCD_BYTE_WIDTH * VIDEO_FONT_HEIGHT)
56
57const int sed156x_text_width = LCD_TEXT_WIDTH;
58const int sed156x_text_height = LCD_TEXT_HEIGHT;
59
60/**************************************************************************************/
61
62#define SED156X_SPI_RXD() (SED156X_SPI_RXD_PORT & SED156X_SPI_RXD_MASK)
63
64#define SED156X_SPI_TXD(x) \
65 do { \
66 if (x) \
67 SED156X_SPI_TXD_PORT |= SED156X_SPI_TXD_MASK; \
68 else \
69 SED156X_SPI_TXD_PORT &= ~SED156X_SPI_TXD_MASK; \
70 } while(0)
71
72#define SED156X_SPI_CLK(x) \
73 do { \
74 if (x) \
75 SED156X_SPI_CLK_PORT |= SED156X_SPI_CLK_MASK; \
76 else \
77 SED156X_SPI_CLK_PORT &= ~SED156X_SPI_CLK_MASK; \
78 } while(0)
79
80#define SED156X_SPI_CLK_TOGGLE() (SED156X_SPI_CLK_PORT ^= SED156X_SPI_CLK_MASK)
81
82#define SED156X_SPI_BIT_DELAY() /* no delay */
83
84#define SED156X_CS(x) \
85 do { \
86 if (x) \
87 SED156X_CS_PORT |= SED156X_CS_MASK; \
88 else \
89 SED156X_CS_PORT &= ~SED156X_CS_MASK; \
90 } while(0)
91
92#define SED156X_A0(x) \
93 do { \
94 if (x) \
95 SED156X_A0_PORT |= SED156X_A0_MASK; \
96 else \
97 SED156X_A0_PORT &= ~SED156X_A0_MASK; \
98 } while(0)
99
100/**************************************************************************************/
101
102/*** LCD Commands ***/
103
104#define LCD_ON 0xAF /* Display ON */
105#define LCD_OFF 0xAE /* Display OFF */
106#define LCD_LADDR 0x40 /* Display start line set + (6-bit) address */
107#define LCD_PADDR 0xB0 /* Page address set + (4-bit) page */
108#define LCD_CADRH 0x10 /* Column address set upper + (4-bit) column hi */
109#define LCD_CADRL 0x00 /* Column address set lower + (4-bit) column lo */
110#define LCD_ADC_NRM 0xA0 /* ADC select Normal */
111#define LCD_ADC_REV 0xA1 /* ADC select Reverse */
112#define LCD_DSP_NRM 0xA6 /* LCD display Normal */
113#define LCD_DSP_REV 0xA7 /* LCD display Reverse */
114#define LCD_DPT_NRM 0xA4 /* Display all points Normal */
115#define LCD_DPT_ALL 0xA5 /* Display all points ON */
116#define LCD_BIAS9 0xA2 /* LCD bias set 1/9 */
117#define LCD_BIAS7 0xA3 /* LCD bias set 1/7 */
118#define LCD_CAINC 0xE0 /* Read/modify/write */
119#define LCD_CAEND 0xEE /* End */
120#define LCD_RESET 0xE2 /* Reset */
121#define LCD_C_NRM 0xC0 /* Common output mode select Normal direction */
122#define LCD_C_RVS 0xC8 /* Common output mode select Reverse direction */
123#define LCD_PWRMD 0x28 /* Power control set + (3-bit) mode */
124#define LCD_RESRT 0x20 /* V5 v. reg. int. resistor ratio set + (3-bit) ratio */
125#define LCD_EVSET 0x81 /* Electronic volume mode set + byte = (6-bit) volume */
126#define LCD_SIOFF 0xAC /* Static indicator OFF */
127#define LCD_SION 0xAD /* Static indicator ON + byte = (2-bit) mode */
128#define LCD_NOP 0xE3 /* NOP */
129#define LCD_TEST 0xF0 /* Test/Test mode reset (Note: *DO NOT USE*) */
130
131/*-------------------------------------------------------------------------------
132 Compound commands
133 -------------------------------------------------------------------------------
134 Command Description Commands
135 ---------- ------------------------ -------------------------------------
136 POWS_ON POWER SAVER ON command LCD_OFF, LCD_D_ALL
137 POWS_OFF POWER SAVER OFF command LCD_D_NRM
138 SLEEPON SLEEP mode LCD_SIOFF, POWS_ON
139 SLEEPOFF SLEEP mode cancel LCD_D_NRM, LCD_SION, LCD_SIS_???
140 STDBYON STAND BY mode LCD_SION, POWS_ON
141 STDBYOFF STAND BY mode cancel LCD_D_NRM
142 -------------------------------------------------------------------------------*/
143
144/*** LCD various parameters ***/
145#define LCD_PPB 8 /* Pixels per byte (display is B/W, 1 bit per pixel) */
146
147/*** LCD Status byte masks ***/
148#define LCD_S_BUSY 0x80 /* Status Read - BUSY mask */
149#define LCD_S_ADC 0x40 /* Status Read - ADC mask */
150#define LCD_S_ONOFF 0x20 /* Status Read - ON/OFF mask */
151#define LCD_S_RESET 0x10 /* Status Read - RESET mask */
152
153/*** LCD commands parameter masks ***/
154#define LCD_M_LADDR 0x3F /* Display start line (6-bit) address mask */
155#define LCD_M_PADDR 0x0F /* Page address (4-bit) page mask */
156#define LCD_M_CADRH 0x0F /* Column address upper (4-bit) column hi mask */
157#define LCD_M_CADRL 0x0F /* Column address lower (4-bit) column lo mask */
158#define LCD_M_PWRMD 0x07 /* Power control (3-bit) mode mask */
159#define LCD_M_RESRT 0x07 /* V5 v. reg. int. resistor ratio (3-bit) ratio mask */
160#define LCD_M_EVSET 0x3F /* Electronic volume mode byte (6-bit) volume mask */
161#define LCD_M_SION 0x03 /* Static indicator ON (2-bit) mode mask */
162
163/*** LCD Power control cirquits control masks ***/
164#define LCD_PWRBSTR 0x04 /* Power control mode - Booster cirquit ON */
165#define LCD_PWRVREG 0x02 /* Power control mode - Voltage regulator cirquit ON */
166#define LCD_PWRVFOL 0x01 /* Power control mode - Voltage follower cirquit ON */
167
168/*** LCD Static indicator states ***/
169#define LCD_SIS_OFF 0x00 /* Static indicator register set - OFF state */
170#define LCD_SIS_BL 0x01 /* Static indicator register set - 1s blink state */
171#define LCD_SIS_RBL 0x02 /* Static indicator register set - .5s rapid blink state */
172#define LCD_SIS_ON 0x03 /* Static indicator register set - constantly on state */
173
174/*** LCD functions special parameters (commands) ***/
175#define LCD_PREVP 0x80 /* Page number for moving to previous */
176#define LCD_NEXTP 0x81 /* or next page */
177#define LCD_ERR_P 0xFF /* Error in page number */
178
179/*** LCD initialization settings ***/
180#define LCD_BIAS LCD_BIAS9 /* Bias: 1/9 */
181#define LCD_ADCMODE LCD_ADC_NRM /* ADC mode: normal */
182#define LCD_COMDIR LCD_C_NRM /* Common output mode: normal */
183#define LCD_RRATIO 0 /* Resistor ratio: 0 */
184#define LCD_CNTRST 0x1C /* electronic volume: 1Ch */
185#define LCD_POWERM (LCD_PWRBSTR | LCD_PWRVREG | LCD_PWRVFOL) /* Power mode: All on */
186
187/**************************************************************************************/
188
189static inline unsigned int sed156x_transfer(unsigned int val)
190{
191 unsigned int rx;
192 int b;
193
194 rx = 0; b = 8;
195 while (--b >= 0) {
196 SED156X_SPI_TXD(val & 0x80);
197 val <<= 1;
198 SED156X_SPI_CLK_TOGGLE();
199 SED156X_SPI_BIT_DELAY();
200 rx <<= 1;
201 if (SED156X_SPI_RXD())
202 rx |= 1;
203 SED156X_SPI_CLK_TOGGLE();
204 SED156X_SPI_BIT_DELAY();
205 }
206
207 return rx;
208}
209
210unsigned int sed156x_data_transfer(unsigned int val)
211{
212 unsigned int rx;
213
214 SED156X_SPI_CLK(1);
215 SED156X_CS(0);
216 SED156X_A0(1);
217
218 rx = sed156x_transfer(val);
219
220 SED156X_CS(1);
221
222 return rx;
223}
224
225void sed156x_data_block_transfer(const u8 *p, int size)
226{
227 SED156X_SPI_CLK(1);
228 SED156X_CS(0);
229 SED156X_A0(1);
230
231 while (--size >= 0)
232 sed156x_transfer(*p++);
233
234 SED156X_CS(1);
235}
236
237unsigned int sed156x_cmd_transfer(unsigned int val)
238{
239 unsigned int rx;
240
241 SED156X_SPI_CLK(1);
242 SED156X_CS(0);
243 SED156X_A0(0);
244
245 rx = sed156x_transfer(val);
246
247 SED156X_CS(1);
248 SED156X_A0(1);
249
250 return rx;
251}
252
253/******************************************************************************/
254
255static u8 hw_screen[LCD_PAGES][LCD_COLUMNS];
256static u8 last_hw_screen[LCD_PAGES][LCD_COLUMNS];
257static u8 sw_screen[LCD_BYTE_WIDTH * LCD_HEIGHT];
258
259void sed156x_sync(void)
260{
261 int i, j, last_page;
262 u8 *d;
263 const u8 *s, *e, *b, *r;
264 u8 v0, v1, v2, v3, v4, v5, v6, v7;
265
266 /* copy and rotate sw_screen to hw_screen */
267 for (i = 0; i < LCD_HEIGHT / 8; i++) {
268
269 d = &hw_screen[i][0];
270 s = &sw_screen[LCD_BYTE_WIDTH * 8 * i + LCD_BYTE_WIDTH - 1];
271
272 for (j = 0; j < LCD_WIDTH / 8; j++) {
273
274 v0 = s[0 * LCD_BYTE_WIDTH];
275 v1 = s[1 * LCD_BYTE_WIDTH];
276 v2 = s[2 * LCD_BYTE_WIDTH];
277 v3 = s[3 * LCD_BYTE_WIDTH];
278 v4 = s[4 * LCD_BYTE_WIDTH];
279 v5 = s[5 * LCD_BYTE_WIDTH];
280 v6 = s[6 * LCD_BYTE_WIDTH];
281 v7 = s[7 * LCD_BYTE_WIDTH];
282
283 d[0] = ((v7 & 0x01) << 7) |
284 ((v6 & 0x01) << 6) |
285 ((v5 & 0x01) << 5) |
286 ((v4 & 0x01) << 4) |
287 ((v3 & 0x01) << 3) |
288 ((v2 & 0x01) << 2) |
289 ((v1 & 0x01) << 1) |
290 (v0 & 0x01) ;
291
292 d[1] = ((v7 & 0x02) << 6) |
293 ((v6 & 0x02) << 5) |
294 ((v5 & 0x02) << 4) |
295 ((v4 & 0x02) << 3) |
296 ((v3 & 0x02) << 2) |
297 ((v2 & 0x02) << 1) |
298 ((v1 & 0x02) << 0) |
299 ((v0 & 0x02) >> 1) ;
300
301 d[2] = ((v7 & 0x04) << 5) |
302 ((v6 & 0x04) << 4) |
303 ((v5 & 0x04) << 3) |
304 ((v4 & 0x04) << 2) |
305 ((v3 & 0x04) << 1) |
306 (v2 & 0x04) |
307 ((v1 & 0x04) >> 1) |
308 ((v0 & 0x04) >> 2) ;
309
310 d[3] = ((v7 & 0x08) << 4) |
311 ((v6 & 0x08) << 3) |
312 ((v5 & 0x08) << 2) |
313 ((v4 & 0x08) << 1) |
314 (v3 & 0x08) |
315 ((v2 & 0x08) >> 1) |
316 ((v1 & 0x08) >> 2) |
317 ((v0 & 0x08) >> 3) ;
318
319 d[4] = ((v7 & 0x10) << 3) |
320 ((v6 & 0x10) << 2) |
321 ((v5 & 0x10) << 1) |
322 (v4 & 0x10) |
323 ((v3 & 0x10) >> 1) |
324 ((v2 & 0x10) >> 2) |
325 ((v1 & 0x10) >> 3) |
326 ((v0 & 0x10) >> 4) ;
327
328 d[5] = ((v7 & 0x20) << 2) |
329 ((v6 & 0x20) << 1) |
330 (v5 & 0x20) |
331 ((v4 & 0x20) >> 1) |
332 ((v3 & 0x20) >> 2) |
333 ((v2 & 0x20) >> 3) |
334 ((v1 & 0x20) >> 4) |
335 ((v0 & 0x20) >> 5) ;
336
337 d[6] = ((v7 & 0x40) << 1) |
338 (v6 & 0x40) |
339 ((v5 & 0x40) >> 1) |
340 ((v4 & 0x40) >> 2) |
341 ((v3 & 0x40) >> 3) |
342 ((v2 & 0x40) >> 4) |
343 ((v1 & 0x40) >> 5) |
344 ((v0 & 0x40) >> 6) ;
345
346 d[7] = (v7 & 0x80) |
347 ((v6 & 0x80) >> 1) |
348 ((v5 & 0x80) >> 2) |
349 ((v4 & 0x80) >> 3) |
350 ((v3 & 0x80) >> 4) |
351 ((v2 & 0x80) >> 5) |
352 ((v1 & 0x80) >> 6) |
353 ((v0 & 0x80) >> 7) ;
354
355 d += 8;
356 s--;
357 }
358 }
359
360 /* and now output only the differences */
361 for (i = 0; i < LCD_PAGES; i++) {
362
363 b = &hw_screen[i][0];
364 e = &hw_screen[i][LCD_COLUMNS];
365
366 d = &last_hw_screen[i][0];
367 s = b;
368
369 last_page = -1;
370
371 /* update only the differences */
372 do {
373 while (s < e && *s == *d) {
374 s++;
375 d++;
376 }
377 if (s == e)
378 break;
379 r = s;
380 while (s < e && *s != *d)
381 *d++ = *s++;
382
383 j = r - b;
384
385 if (i != last_page) {
386 sed156x_cmd_transfer(LCD_PADDR | i);
387 last_page = i;
388 }
389
390 sed156x_cmd_transfer(LCD_CADRH | ((j >> 4) & 0x0F));
391 sed156x_cmd_transfer(LCD_CADRL | (j & 0x0F));
392 sed156x_data_block_transfer(r, s - r);
393
394 } while (s < e);
395 }
396
397/********
398 for (i = 0; i < LCD_PAGES; i++) {
399 sed156x_cmd_transfer(LCD_PADDR | i);
400 sed156x_cmd_transfer(LCD_CADRH | 0);
401 sed156x_cmd_transfer(LCD_CADRL | 0);
402 sed156x_data_block_transfer(&hw_screen[i][0], LCD_COLUMNS);
403 }
404 memcpy(last_hw_screen, hw_screen, sizeof(last_hw_screen));
405********/
406}
407
408void sed156x_clear(void)
409{
410 memset(sw_screen, 0, sizeof(sw_screen));
411}
412
413void sed156x_output_at(int x, int y, const char *str, int size)
414{
415 int i, j;
416 u8 *p;
417 const u8 *s;
418
419 if ((unsigned int)y >= LCD_TEXT_HEIGHT || (unsigned int)x >= LCD_TEXT_WIDTH)
420 return;
421
422 p = &sw_screen[y * VIDEO_FONT_HEIGHT * LCD_BYTE_WIDTH + x * VIDEO_FONT_BYTE_WIDTH];
423
424 while (--size >= 0) {
425
426 s = &video_fontdata[((int)*str++ & 0xff) * VIDEO_FONT_BYTE_WIDTH * VIDEO_FONT_HEIGHT];
427 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
428 for (j = 0; j < VIDEO_FONT_BYTE_WIDTH; j++)
429 *p++ = *s++;
430 p += LCD_BYTE_WIDTH - VIDEO_FONT_BYTE_WIDTH;
431 }
432 p -= (LCD_BYTE_LINESZ - VIDEO_FONT_BYTE_WIDTH);
433
434 if (x >= LCD_TEXT_WIDTH)
435 break;
436 x++;
437 }
438}
439
440void sed156x_reverse_at(int x, int y, int size)
441{
442 int i, j;
443 u8 *p;
444
445 if ((unsigned int)y >= LCD_TEXT_HEIGHT || (unsigned int)x >= LCD_TEXT_WIDTH)
446 return;
447
448 p = &sw_screen[y * VIDEO_FONT_HEIGHT * LCD_BYTE_WIDTH + x * VIDEO_FONT_BYTE_WIDTH];
449
450 while (--size >= 0) {
451
452 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
453 for (j = 0; j < VIDEO_FONT_BYTE_WIDTH; j++, p++)
454 *p = ~*p;
455 p += LCD_BYTE_WIDTH - VIDEO_FONT_BYTE_WIDTH;
456 }
457 p -= (LCD_BYTE_LINESZ - VIDEO_FONT_BYTE_WIDTH);
458
459 if (x >= LCD_TEXT_WIDTH)
460 break;
461 x++;
462 }
463}
464
465void sed156x_scroll_line(void)
466{
467 memmove(&sw_screen[0],
468 &sw_screen[LCD_BYTE_LINESZ],
469 LCD_BYTE_WIDTH * (LCD_HEIGHT - VIDEO_FONT_HEIGHT));
470}
471
472void sed156x_scroll(int dx, int dy)
473{
474 u8 *p1 = NULL, *p2 = NULL, *p3 = NULL; /* pacify gcc */
475 int adx, ady, i, sz;
476
477 adx = dx > 0 ? dx : -dx;
478 ady = dy > 0 ? dy : -dy;
479
480 /* overscroll? erase everything */
481 if (adx >= LCD_TEXT_WIDTH || ady >= LCD_TEXT_HEIGHT) {
482 memset(sw_screen, 0, sizeof(sw_screen));
483 return;
484 }
485
486 sz = LCD_BYTE_LINESZ * ady;
487 if (dy > 0) {
488 p1 = &sw_screen[0];
489 p2 = &sw_screen[sz];
490 p3 = &sw_screen[LCD_BYTE_WIDTH * LCD_HEIGHT - sz];
491 } else if (dy < 0) {
492 p1 = &sw_screen[sz];
493 p2 = &sw_screen[0];
494 p3 = &sw_screen[0];
495 }
496
497 if (ady > 0) {
498 memmove(p1, p2, LCD_BYTE_WIDTH * LCD_HEIGHT - sz);
499 memset(p3, 0, sz);
500 }
501
502 sz = VIDEO_FONT_BYTE_WIDTH * adx;
503 if (dx > 0) {
504 p1 = &sw_screen[0];
505 p2 = &sw_screen[0] + sz;
506 p3 = &sw_screen[0] + LCD_BYTE_WIDTH - sz;
507 } else if (dx < 0) {
508 p1 = &sw_screen[0] + sz;
509 p2 = &sw_screen[0];
510 p3 = &sw_screen[0];
511 }
512
513 /* xscroll */
514 if (adx > 0) {
515 for (i = 0; i < LCD_HEIGHT; i++) {
516 memmove(p1, p2, LCD_BYTE_WIDTH - sz);
517 memset(p3, 0, sz);
518 p1 += LCD_BYTE_WIDTH;
519 p2 += LCD_BYTE_WIDTH;
520 p3 += LCD_BYTE_WIDTH;
521 }
522 }
523}
524
525void sed156x_init(void)
526{
527 int i;
528
529 SED156X_CS(1);
530 SED156X_A0(1);
531
532 /* Send initialization commands to the LCD */
533 sed156x_cmd_transfer(LCD_OFF); /* Turn display OFF */
534 sed156x_cmd_transfer(LCD_BIAS); /* set the LCD Bias, */
535 sed156x_cmd_transfer(LCD_ADCMODE); /* ADC mode, */
536 sed156x_cmd_transfer(LCD_COMDIR); /* common output mode, */
537 sed156x_cmd_transfer(LCD_RESRT | LCD_RRATIO); /* resistor ratio, */
538 sed156x_cmd_transfer(LCD_EVSET); /* electronic volume, */
539 sed156x_cmd_transfer(LCD_CNTRST);
540 sed156x_cmd_transfer(LCD_PWRMD | LCD_POWERM); /* and power mode */
541 sed156x_cmd_transfer(LCD_PADDR | 0); /* cursor home */
542 sed156x_cmd_transfer(LCD_CADRH | 0);
543 sed156x_cmd_transfer(LCD_CADRL | 0);
544 sed156x_cmd_transfer(LCD_LADDR | 0); /* and display start line */
545 sed156x_cmd_transfer(LCD_DSP_NRM); /* LCD display Normal */
546
547 /* clear everything */
548 memset(sw_screen, 0, sizeof(sw_screen));
549 memset(hw_screen, 0, sizeof(hw_screen));
550 memset(last_hw_screen, 0, sizeof(last_hw_screen));
551
552 for (i = 0; i < LCD_PAGES; i++) {
553 sed156x_cmd_transfer(LCD_PADDR | i);
554 sed156x_cmd_transfer(LCD_CADRH | 0);
555 sed156x_cmd_transfer(LCD_CADRL | 0);
556 sed156x_data_block_transfer(&hw_screen[i][0], LCD_COLUMNS);
557 }
558
559 sed156x_clear();
560 sed156x_sync();
561 sed156x_cmd_transfer(LCD_ON); /* Turn display ON */
562}