1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <common.h>
27#include <watchdog.h>
28
29#include <sed156x.h>
30
31
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
43#include <video_font.h>
44#include <video_font_data.h>
45
46#if VIDEO_FONT_WIDTH != 8 || VIDEO_FONT_HEIGHT != 16
47#error Expecting VIDEO_FONT_WIDTH == 8 && VIDEO_FONT_HEIGHT == 16
48#endif
49
50#define LCD_BYTE_WIDTH (LCD_WIDTH / 8)
51#define VIDEO_FONT_BYTE_WIDTH (VIDEO_FONT_WIDTH / 8)
52
53#define LCD_TEXT_WIDTH (LCD_WIDTH / VIDEO_FONT_WIDTH)
54#define LCD_TEXT_HEIGHT (LCD_HEIGHT / VIDEO_FONT_HEIGHT)
55
56#define LCD_BYTE_LINESZ (LCD_BYTE_WIDTH * VIDEO_FONT_HEIGHT)
57
58const int sed156x_text_width = LCD_TEXT_WIDTH;
59const int sed156x_text_height = LCD_TEXT_HEIGHT;
60
61
62
63#define SED156X_SPI_RXD() (SED156X_SPI_RXD_PORT & SED156X_SPI_RXD_MASK)
64
65#define SED156X_SPI_TXD(x) \
66 do { \
67 if (x) \
68 SED156X_SPI_TXD_PORT |= SED156X_SPI_TXD_MASK; \
69 else \
70 SED156X_SPI_TXD_PORT &= ~SED156X_SPI_TXD_MASK; \
71 } while(0)
72
73#define SED156X_SPI_CLK(x) \
74 do { \
75 if (x) \
76 SED156X_SPI_CLK_PORT |= SED156X_SPI_CLK_MASK; \
77 else \
78 SED156X_SPI_CLK_PORT &= ~SED156X_SPI_CLK_MASK; \
79 } while(0)
80
81#define SED156X_SPI_CLK_TOGGLE() (SED156X_SPI_CLK_PORT ^= SED156X_SPI_CLK_MASK)
82
83#define SED156X_SPI_BIT_DELAY()
84
85#define SED156X_CS(x) \
86 do { \
87 if (x) \
88 SED156X_CS_PORT |= SED156X_CS_MASK; \
89 else \
90 SED156X_CS_PORT &= ~SED156X_CS_MASK; \
91 } while(0)
92
93#define SED156X_A0(x) \
94 do { \
95 if (x) \
96 SED156X_A0_PORT |= SED156X_A0_MASK; \
97 else \
98 SED156X_A0_PORT &= ~SED156X_A0_MASK; \
99 } while(0)
100
101
102
103
104
105#define LCD_ON 0xAF
106#define LCD_OFF 0xAE
107#define LCD_LADDR 0x40
108#define LCD_PADDR 0xB0
109#define LCD_CADRH 0x10
110#define LCD_CADRL 0x00
111#define LCD_ADC_NRM 0xA0
112#define LCD_ADC_REV 0xA1
113#define LCD_DSP_NRM 0xA6
114#define LCD_DSP_REV 0xA7
115#define LCD_DPT_NRM 0xA4
116#define LCD_DPT_ALL 0xA5
117#define LCD_BIAS9 0xA2
118#define LCD_BIAS7 0xA3
119#define LCD_CAINC 0xE0
120#define LCD_CAEND 0xEE
121#define LCD_RESET 0xE2
122#define LCD_C_NRM 0xC0
123#define LCD_C_RVS 0xC8
124#define LCD_PWRMD 0x28
125#define LCD_RESRT 0x20
126#define LCD_EVSET 0x81
127#define LCD_SIOFF 0xAC
128#define LCD_SION 0xAD
129#define LCD_NOP 0xE3
130#define LCD_TEST 0xF0
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146#define LCD_PPB 8
147
148
149#define LCD_S_BUSY 0x80
150#define LCD_S_ADC 0x40
151#define LCD_S_ONOFF 0x20
152#define LCD_S_RESET 0x10
153
154
155#define LCD_M_LADDR 0x3F
156#define LCD_M_PADDR 0x0F
157#define LCD_M_CADRH 0x0F
158#define LCD_M_CADRL 0x0F
159#define LCD_M_PWRMD 0x07
160#define LCD_M_RESRT 0x07
161#define LCD_M_EVSET 0x3F
162#define LCD_M_SION 0x03
163
164
165#define LCD_PWRBSTR 0x04
166#define LCD_PWRVREG 0x02
167#define LCD_PWRVFOL 0x01
168
169
170#define LCD_SIS_OFF 0x00
171#define LCD_SIS_BL 0x01
172#define LCD_SIS_RBL 0x02
173#define LCD_SIS_ON 0x03
174
175
176#define LCD_PREVP 0x80
177#define LCD_NEXTP 0x81
178#define LCD_ERR_P 0xFF
179
180
181#define LCD_BIAS LCD_BIAS9
182#define LCD_ADCMODE LCD_ADC_NRM
183#define LCD_COMDIR LCD_C_NRM
184#define LCD_RRATIO 0
185#define LCD_CNTRST 0x1C
186#define LCD_POWERM (LCD_PWRBSTR | LCD_PWRVREG | LCD_PWRVFOL)
187
188
189
190static inline unsigned int sed156x_transfer(unsigned int val)
191{
192 unsigned int rx;
193 int b;
194
195 rx = 0; b = 8;
196 while (--b >= 0) {
197 SED156X_SPI_TXD(val & 0x80);
198 val <<= 1;
199 SED156X_SPI_CLK_TOGGLE();
200 SED156X_SPI_BIT_DELAY();
201 rx <<= 1;
202 if (SED156X_SPI_RXD())
203 rx |= 1;
204 SED156X_SPI_CLK_TOGGLE();
205 SED156X_SPI_BIT_DELAY();
206 }
207
208 return rx;
209}
210
211unsigned int sed156x_data_transfer(unsigned int val)
212{
213 unsigned int rx;
214
215 SED156X_SPI_CLK(1);
216 SED156X_CS(0);
217 SED156X_A0(1);
218
219 rx = sed156x_transfer(val);
220
221 SED156X_CS(1);
222
223 return rx;
224}
225
226void sed156x_data_block_transfer(const u8 *p, int size)
227{
228 SED156X_SPI_CLK(1);
229 SED156X_CS(0);
230 SED156X_A0(1);
231
232 while (--size >= 0)
233 sed156x_transfer(*p++);
234
235 SED156X_CS(1);
236}
237
238unsigned int sed156x_cmd_transfer(unsigned int val)
239{
240 unsigned int rx;
241
242 SED156X_SPI_CLK(1);
243 SED156X_CS(0);
244 SED156X_A0(0);
245
246 rx = sed156x_transfer(val);
247
248 SED156X_CS(1);
249 SED156X_A0(1);
250
251 return rx;
252}
253
254
255
256static u8 hw_screen[LCD_PAGES][LCD_COLUMNS];
257static u8 last_hw_screen[LCD_PAGES][LCD_COLUMNS];
258static u8 sw_screen[LCD_BYTE_WIDTH * LCD_HEIGHT];
259
260void sed156x_sync(void)
261{
262 int i, j, last_page;
263 u8 *d;
264 const u8 *s, *e, *b, *r;
265 u8 v0, v1, v2, v3, v4, v5, v6, v7;
266
267
268 for (i = 0; i < LCD_HEIGHT / 8; i++) {
269
270 d = &hw_screen[i][0];
271 s = &sw_screen[LCD_BYTE_WIDTH * 8 * i + LCD_BYTE_WIDTH - 1];
272
273 for (j = 0; j < LCD_WIDTH / 8; j++) {
274
275 v0 = s[0 * LCD_BYTE_WIDTH];
276 v1 = s[1 * LCD_BYTE_WIDTH];
277 v2 = s[2 * LCD_BYTE_WIDTH];
278 v3 = s[3 * LCD_BYTE_WIDTH];
279 v4 = s[4 * LCD_BYTE_WIDTH];
280 v5 = s[5 * LCD_BYTE_WIDTH];
281 v6 = s[6 * LCD_BYTE_WIDTH];
282 v7 = s[7 * LCD_BYTE_WIDTH];
283
284 d[0] = ((v7 & 0x01) << 7) |
285 ((v6 & 0x01) << 6) |
286 ((v5 & 0x01) << 5) |
287 ((v4 & 0x01) << 4) |
288 ((v3 & 0x01) << 3) |
289 ((v2 & 0x01) << 2) |
290 ((v1 & 0x01) << 1) |
291 (v0 & 0x01) ;
292
293 d[1] = ((v7 & 0x02) << 6) |
294 ((v6 & 0x02) << 5) |
295 ((v5 & 0x02) << 4) |
296 ((v4 & 0x02) << 3) |
297 ((v3 & 0x02) << 2) |
298 ((v2 & 0x02) << 1) |
299 ((v1 & 0x02) << 0) |
300 ((v0 & 0x02) >> 1) ;
301
302 d[2] = ((v7 & 0x04) << 5) |
303 ((v6 & 0x04) << 4) |
304 ((v5 & 0x04) << 3) |
305 ((v4 & 0x04) << 2) |
306 ((v3 & 0x04) << 1) |
307 (v2 & 0x04) |
308 ((v1 & 0x04) >> 1) |
309 ((v0 & 0x04) >> 2) ;
310
311 d[3] = ((v7 & 0x08) << 4) |
312 ((v6 & 0x08) << 3) |
313 ((v5 & 0x08) << 2) |
314 ((v4 & 0x08) << 1) |
315 (v3 & 0x08) |
316 ((v2 & 0x08) >> 1) |
317 ((v1 & 0x08) >> 2) |
318 ((v0 & 0x08) >> 3) ;
319
320 d[4] = ((v7 & 0x10) << 3) |
321 ((v6 & 0x10) << 2) |
322 ((v5 & 0x10) << 1) |
323 (v4 & 0x10) |
324 ((v3 & 0x10) >> 1) |
325 ((v2 & 0x10) >> 2) |
326 ((v1 & 0x10) >> 3) |
327 ((v0 & 0x10) >> 4) ;
328
329 d[5] = ((v7 & 0x20) << 2) |
330 ((v6 & 0x20) << 1) |
331 (v5 & 0x20) |
332 ((v4 & 0x20) >> 1) |
333 ((v3 & 0x20) >> 2) |
334 ((v2 & 0x20) >> 3) |
335 ((v1 & 0x20) >> 4) |
336 ((v0 & 0x20) >> 5) ;
337
338 d[6] = ((v7 & 0x40) << 1) |
339 (v6 & 0x40) |
340 ((v5 & 0x40) >> 1) |
341 ((v4 & 0x40) >> 2) |
342 ((v3 & 0x40) >> 3) |
343 ((v2 & 0x40) >> 4) |
344 ((v1 & 0x40) >> 5) |
345 ((v0 & 0x40) >> 6) ;
346
347 d[7] = (v7 & 0x80) |
348 ((v6 & 0x80) >> 1) |
349 ((v5 & 0x80) >> 2) |
350 ((v4 & 0x80) >> 3) |
351 ((v3 & 0x80) >> 4) |
352 ((v2 & 0x80) >> 5) |
353 ((v1 & 0x80) >> 6) |
354 ((v0 & 0x80) >> 7) ;
355
356 d += 8;
357 s--;
358 }
359 }
360
361
362 for (i = 0; i < LCD_PAGES; i++) {
363
364 b = &hw_screen[i][0];
365 e = &hw_screen[i][LCD_COLUMNS];
366
367 d = &last_hw_screen[i][0];
368 s = b;
369
370 last_page = -1;
371
372
373 do {
374 while (s < e && *s == *d) {
375 s++;
376 d++;
377 }
378 if (s == e)
379 break;
380 r = s;
381 while (s < e && *s != *d)
382 *d++ = *s++;
383
384 j = r - b;
385
386 if (i != last_page) {
387 sed156x_cmd_transfer(LCD_PADDR | i);
388 last_page = i;
389 }
390
391 sed156x_cmd_transfer(LCD_CADRH | ((j >> 4) & 0x0F));
392 sed156x_cmd_transfer(LCD_CADRL | (j & 0x0F));
393 sed156x_data_block_transfer(r, s - r);
394
395 } while (s < e);
396 }
397
398
399
400
401
402
403
404
405
406
407}
408
409void sed156x_clear(void)
410{
411 memset(sw_screen, 0, sizeof(sw_screen));
412}
413
414void sed156x_output_at(int x, int y, const char *str, int size)
415{
416 int i, j;
417 u8 *p;
418 const u8 *s;
419
420 if ((unsigned int)y >= LCD_TEXT_HEIGHT || (unsigned int)x >= LCD_TEXT_WIDTH)
421 return;
422
423 p = &sw_screen[y * VIDEO_FONT_HEIGHT * LCD_BYTE_WIDTH + x * VIDEO_FONT_BYTE_WIDTH];
424
425 while (--size >= 0) {
426
427 s = &video_fontdata[((int)*str++ & 0xff) * VIDEO_FONT_BYTE_WIDTH * VIDEO_FONT_HEIGHT];
428 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
429 for (j = 0; j < VIDEO_FONT_BYTE_WIDTH; j++)
430 *p++ = *s++;
431 p += LCD_BYTE_WIDTH - VIDEO_FONT_BYTE_WIDTH;
432 }
433 p -= (LCD_BYTE_LINESZ - VIDEO_FONT_BYTE_WIDTH);
434
435 if (x >= LCD_TEXT_WIDTH)
436 break;
437 x++;
438 }
439}
440
441void sed156x_reverse_at(int x, int y, int size)
442{
443 int i, j;
444 u8 *p;
445
446 if ((unsigned int)y >= LCD_TEXT_HEIGHT || (unsigned int)x >= LCD_TEXT_WIDTH)
447 return;
448
449 p = &sw_screen[y * VIDEO_FONT_HEIGHT * LCD_BYTE_WIDTH + x * VIDEO_FONT_BYTE_WIDTH];
450
451 while (--size >= 0) {
452
453 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
454 for (j = 0; j < VIDEO_FONT_BYTE_WIDTH; j++, p++)
455 *p = ~*p;
456 p += LCD_BYTE_WIDTH - VIDEO_FONT_BYTE_WIDTH;
457 }
458 p -= (LCD_BYTE_LINESZ - VIDEO_FONT_BYTE_WIDTH);
459
460 if (x >= LCD_TEXT_WIDTH)
461 break;
462 x++;
463 }
464}
465
466void sed156x_scroll_line(void)
467{
468 memmove(&sw_screen[0],
469 &sw_screen[LCD_BYTE_LINESZ],
470 LCD_BYTE_WIDTH * (LCD_HEIGHT - VIDEO_FONT_HEIGHT));
471}
472
473void sed156x_scroll(int dx, int dy)
474{
475 u8 *p1 = NULL, *p2 = NULL, *p3 = NULL;
476 int adx, ady, i, sz;
477
478 adx = dx > 0 ? dx : -dx;
479 ady = dy > 0 ? dy : -dy;
480
481
482 if (adx >= LCD_TEXT_WIDTH || ady >= LCD_TEXT_HEIGHT) {
483 memset(sw_screen, 0, sizeof(sw_screen));
484 return;
485 }
486
487 sz = LCD_BYTE_LINESZ * ady;
488 if (dy > 0) {
489 p1 = &sw_screen[0];
490 p2 = &sw_screen[sz];
491 p3 = &sw_screen[LCD_BYTE_WIDTH * LCD_HEIGHT - sz];
492 } else if (dy < 0) {
493 p1 = &sw_screen[sz];
494 p2 = &sw_screen[0];
495 p3 = &sw_screen[0];
496 }
497
498 if (ady > 0) {
499 memmove(p1, p2, LCD_BYTE_WIDTH * LCD_HEIGHT - sz);
500 memset(p3, 0, sz);
501 }
502
503 sz = VIDEO_FONT_BYTE_WIDTH * adx;
504 if (dx > 0) {
505 p1 = &sw_screen[0];
506 p2 = &sw_screen[0] + sz;
507 p3 = &sw_screen[0] + LCD_BYTE_WIDTH - sz;
508 } else if (dx < 0) {
509 p1 = &sw_screen[0] + sz;
510 p2 = &sw_screen[0];
511 p3 = &sw_screen[0];
512 }
513
514
515 if (adx > 0) {
516 for (i = 0; i < LCD_HEIGHT; i++) {
517 memmove(p1, p2, LCD_BYTE_WIDTH - sz);
518 memset(p3, 0, sz);
519 p1 += LCD_BYTE_WIDTH;
520 p2 += LCD_BYTE_WIDTH;
521 p3 += LCD_BYTE_WIDTH;
522 }
523 }
524}
525
526void sed156x_init(void)
527{
528 int i;
529
530 SED156X_CS(1);
531 SED156X_A0(1);
532
533
534 sed156x_cmd_transfer(LCD_OFF);
535 sed156x_cmd_transfer(LCD_BIAS);
536 sed156x_cmd_transfer(LCD_ADCMODE);
537 sed156x_cmd_transfer(LCD_COMDIR);
538 sed156x_cmd_transfer(LCD_RESRT | LCD_RRATIO);
539 sed156x_cmd_transfer(LCD_EVSET);
540 sed156x_cmd_transfer(LCD_CNTRST);
541 sed156x_cmd_transfer(LCD_PWRMD | LCD_POWERM);
542 sed156x_cmd_transfer(LCD_PADDR | 0);
543 sed156x_cmd_transfer(LCD_CADRH | 0);
544 sed156x_cmd_transfer(LCD_CADRL | 0);
545 sed156x_cmd_transfer(LCD_LADDR | 0);
546 sed156x_cmd_transfer(LCD_DSP_NRM);
547
548
549 memset(sw_screen, 0, sizeof(sw_screen));
550 memset(hw_screen, 0, sizeof(hw_screen));
551 memset(last_hw_screen, 0, sizeof(last_hw_screen));
552
553 for (i = 0; i < LCD_PAGES; i++) {
554 sed156x_cmd_transfer(LCD_PADDR | i);
555 sed156x_cmd_transfer(LCD_CADRH | 0);
556 sed156x_cmd_transfer(LCD_CADRL | 0);
557 sed156x_data_block_transfer(&hw_screen[i][0], LCD_COLUMNS);
558 }
559
560 sed156x_clear();
561 sed156x_sync();
562 sed156x_cmd_transfer(LCD_ON);
563}
564