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