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