4 * Pantelis Antoniou <panto@intracom.gr>
7 * SPDX-License-Identifier: GPL-2.0+
15 /* configure according to the selected display */
16 #if defined(CONFIG_SED156X_PG12864Q)
21 #define LCD_COLUMNS 132
23 #error Unsupported SED156x configuration
26 /* include the font data */
27 #include <video_font.h>
29 #if VIDEO_FONT_WIDTH != 8 || VIDEO_FONT_HEIGHT != 16
30 #error Expecting VIDEO_FONT_WIDTH == 8 && VIDEO_FONT_HEIGHT == 16
33 #define LCD_BYTE_WIDTH (LCD_WIDTH / 8)
34 #define VIDEO_FONT_BYTE_WIDTH (VIDEO_FONT_WIDTH / 8)
36 #define LCD_TEXT_WIDTH (LCD_WIDTH / VIDEO_FONT_WIDTH)
37 #define LCD_TEXT_HEIGHT (LCD_HEIGHT / VIDEO_FONT_HEIGHT)
39 #define LCD_BYTE_LINESZ (LCD_BYTE_WIDTH * VIDEO_FONT_HEIGHT)
41 const int sed156x_text_width = LCD_TEXT_WIDTH;
42 const int sed156x_text_height = LCD_TEXT_HEIGHT;
44 /**************************************************************************************/
46 #define SED156X_SPI_RXD() (SED156X_SPI_RXD_PORT & SED156X_SPI_RXD_MASK)
48 #define SED156X_SPI_TXD(x) \
51 SED156X_SPI_TXD_PORT |= SED156X_SPI_TXD_MASK; \
53 SED156X_SPI_TXD_PORT &= ~SED156X_SPI_TXD_MASK; \
56 #define SED156X_SPI_CLK(x) \
59 SED156X_SPI_CLK_PORT |= SED156X_SPI_CLK_MASK; \
61 SED156X_SPI_CLK_PORT &= ~SED156X_SPI_CLK_MASK; \
64 #define SED156X_SPI_CLK_TOGGLE() (SED156X_SPI_CLK_PORT ^= SED156X_SPI_CLK_MASK)
66 #define SED156X_SPI_BIT_DELAY() /* no delay */
68 #define SED156X_CS(x) \
71 SED156X_CS_PORT |= SED156X_CS_MASK; \
73 SED156X_CS_PORT &= ~SED156X_CS_MASK; \
76 #define SED156X_A0(x) \
79 SED156X_A0_PORT |= SED156X_A0_MASK; \
81 SED156X_A0_PORT &= ~SED156X_A0_MASK; \
84 /**************************************************************************************/
86 /*** LCD Commands ***/
88 #define LCD_ON 0xAF /* Display ON */
89 #define LCD_OFF 0xAE /* Display OFF */
90 #define LCD_LADDR 0x40 /* Display start line set + (6-bit) address */
91 #define LCD_PADDR 0xB0 /* Page address set + (4-bit) page */
92 #define LCD_CADRH 0x10 /* Column address set upper + (4-bit) column hi */
93 #define LCD_CADRL 0x00 /* Column address set lower + (4-bit) column lo */
94 #define LCD_ADC_NRM 0xA0 /* ADC select Normal */
95 #define LCD_ADC_REV 0xA1 /* ADC select Reverse */
96 #define LCD_DSP_NRM 0xA6 /* LCD display Normal */
97 #define LCD_DSP_REV 0xA7 /* LCD display Reverse */
98 #define LCD_DPT_NRM 0xA4 /* Display all points Normal */
99 #define LCD_DPT_ALL 0xA5 /* Display all points ON */
100 #define LCD_BIAS9 0xA2 /* LCD bias set 1/9 */
101 #define LCD_BIAS7 0xA3 /* LCD bias set 1/7 */
102 #define LCD_CAINC 0xE0 /* Read/modify/write */
103 #define LCD_CAEND 0xEE /* End */
104 #define LCD_RESET 0xE2 /* Reset */
105 #define LCD_C_NRM 0xC0 /* Common output mode select Normal direction */
106 #define LCD_C_RVS 0xC8 /* Common output mode select Reverse direction */
107 #define LCD_PWRMD 0x28 /* Power control set + (3-bit) mode */
108 #define LCD_RESRT 0x20 /* V5 v. reg. int. resistor ratio set + (3-bit) ratio */
109 #define LCD_EVSET 0x81 /* Electronic volume mode set + byte = (6-bit) volume */
110 #define LCD_SIOFF 0xAC /* Static indicator OFF */
111 #define LCD_SION 0xAD /* Static indicator ON + byte = (2-bit) mode */
112 #define LCD_NOP 0xE3 /* NOP */
113 #define LCD_TEST 0xF0 /* Test/Test mode reset (Note: *DO NOT USE*) */
115 /*-------------------------------------------------------------------------------
117 -------------------------------------------------------------------------------
118 Command Description Commands
119 ---------- ------------------------ -------------------------------------
120 POWS_ON POWER SAVER ON command LCD_OFF, LCD_D_ALL
121 POWS_OFF POWER SAVER OFF command LCD_D_NRM
122 SLEEPON SLEEP mode LCD_SIOFF, POWS_ON
123 SLEEPOFF SLEEP mode cancel LCD_D_NRM, LCD_SION, LCD_SIS_???
124 STDBYON STAND BY mode LCD_SION, POWS_ON
125 STDBYOFF STAND BY mode cancel LCD_D_NRM
126 -------------------------------------------------------------------------------*/
128 /*** LCD various parameters ***/
129 #define LCD_PPB 8 /* Pixels per byte (display is B/W, 1 bit per pixel) */
131 /*** LCD Status byte masks ***/
132 #define LCD_S_BUSY 0x80 /* Status Read - BUSY mask */
133 #define LCD_S_ADC 0x40 /* Status Read - ADC mask */
134 #define LCD_S_ONOFF 0x20 /* Status Read - ON/OFF mask */
135 #define LCD_S_RESET 0x10 /* Status Read - RESET mask */
137 /*** LCD commands parameter masks ***/
138 #define LCD_M_LADDR 0x3F /* Display start line (6-bit) address mask */
139 #define LCD_M_PADDR 0x0F /* Page address (4-bit) page mask */
140 #define LCD_M_CADRH 0x0F /* Column address upper (4-bit) column hi mask */
141 #define LCD_M_CADRL 0x0F /* Column address lower (4-bit) column lo mask */
142 #define LCD_M_PWRMD 0x07 /* Power control (3-bit) mode mask */
143 #define LCD_M_RESRT 0x07 /* V5 v. reg. int. resistor ratio (3-bit) ratio mask */
144 #define LCD_M_EVSET 0x3F /* Electronic volume mode byte (6-bit) volume mask */
145 #define LCD_M_SION 0x03 /* Static indicator ON (2-bit) mode mask */
147 /*** LCD Power control cirquits control masks ***/
148 #define LCD_PWRBSTR 0x04 /* Power control mode - Booster cirquit ON */
149 #define LCD_PWRVREG 0x02 /* Power control mode - Voltage regulator cirquit ON */
150 #define LCD_PWRVFOL 0x01 /* Power control mode - Voltage follower cirquit ON */
152 /*** LCD Static indicator states ***/
153 #define LCD_SIS_OFF 0x00 /* Static indicator register set - OFF state */
154 #define LCD_SIS_BL 0x01 /* Static indicator register set - 1s blink state */
155 #define LCD_SIS_RBL 0x02 /* Static indicator register set - .5s rapid blink state */
156 #define LCD_SIS_ON 0x03 /* Static indicator register set - constantly on state */
158 /*** LCD functions special parameters (commands) ***/
159 #define LCD_PREVP 0x80 /* Page number for moving to previous */
160 #define LCD_NEXTP 0x81 /* or next page */
161 #define LCD_ERR_P 0xFF /* Error in page number */
163 /*** LCD initialization settings ***/
164 #define LCD_BIAS LCD_BIAS9 /* Bias: 1/9 */
165 #define LCD_ADCMODE LCD_ADC_NRM /* ADC mode: normal */
166 #define LCD_COMDIR LCD_C_NRM /* Common output mode: normal */
167 #define LCD_RRATIO 0 /* Resistor ratio: 0 */
168 #define LCD_CNTRST 0x1C /* electronic volume: 1Ch */
169 #define LCD_POWERM (LCD_PWRBSTR | LCD_PWRVREG | LCD_PWRVFOL) /* Power mode: All on */
171 /**************************************************************************************/
173 static inline unsigned int sed156x_transfer(unsigned int val)
180 SED156X_SPI_TXD(val & 0x80);
182 SED156X_SPI_CLK_TOGGLE();
183 SED156X_SPI_BIT_DELAY();
185 if (SED156X_SPI_RXD())
187 SED156X_SPI_CLK_TOGGLE();
188 SED156X_SPI_BIT_DELAY();
194 unsigned int sed156x_data_transfer(unsigned int val)
202 rx = sed156x_transfer(val);
209 void sed156x_data_block_transfer(const u8 *p, int size)
216 sed156x_transfer(*p++);
221 unsigned int sed156x_cmd_transfer(unsigned int val)
229 rx = sed156x_transfer(val);
237 /******************************************************************************/
239 static u8 hw_screen[LCD_PAGES][LCD_COLUMNS];
240 static u8 last_hw_screen[LCD_PAGES][LCD_COLUMNS];
241 static u8 sw_screen[LCD_BYTE_WIDTH * LCD_HEIGHT];
243 void sed156x_sync(void)
247 const u8 *s, *e, *b, *r;
248 u8 v0, v1, v2, v3, v4, v5, v6, v7;
250 /* copy and rotate sw_screen to hw_screen */
251 for (i = 0; i < LCD_HEIGHT / 8; i++) {
253 d = &hw_screen[i][0];
254 s = &sw_screen[LCD_BYTE_WIDTH * 8 * i + LCD_BYTE_WIDTH - 1];
256 for (j = 0; j < LCD_WIDTH / 8; j++) {
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];
267 d[0] = ((v7 & 0x01) << 7) |
276 d[1] = ((v7 & 0x02) << 6) |
285 d[2] = ((v7 & 0x04) << 5) |
294 d[3] = ((v7 & 0x08) << 4) |
303 d[4] = ((v7 & 0x10) << 3) |
312 d[5] = ((v7 & 0x20) << 2) |
321 d[6] = ((v7 & 0x40) << 1) |
344 /* and now output only the differences */
345 for (i = 0; i < LCD_PAGES; i++) {
347 b = &hw_screen[i][0];
348 e = &hw_screen[i][LCD_COLUMNS];
350 d = &last_hw_screen[i][0];
355 /* update only the differences */
357 while (s < e && *s == *d) {
364 while (s < e && *s != *d)
369 if (i != last_page) {
370 sed156x_cmd_transfer(LCD_PADDR | i);
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);
382 for (i = 0; i < LCD_PAGES; i++) {
383 sed156x_cmd_transfer(LCD_PADDR | i);
384 sed156x_cmd_transfer(LCD_CADRH | 0);
385 sed156x_cmd_transfer(LCD_CADRL | 0);
386 sed156x_data_block_transfer(&hw_screen[i][0], LCD_COLUMNS);
388 memcpy(last_hw_screen, hw_screen, sizeof(last_hw_screen));
392 void sed156x_clear(void)
394 memset(sw_screen, 0, sizeof(sw_screen));
397 void sed156x_output_at(int x, int y, const char *str, int size)
403 if ((unsigned int)y >= LCD_TEXT_HEIGHT || (unsigned int)x >= LCD_TEXT_WIDTH)
406 p = &sw_screen[y * VIDEO_FONT_HEIGHT * LCD_BYTE_WIDTH + x * VIDEO_FONT_BYTE_WIDTH];
408 while (--size >= 0) {
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++)
414 p += LCD_BYTE_WIDTH - VIDEO_FONT_BYTE_WIDTH;
416 p -= (LCD_BYTE_LINESZ - VIDEO_FONT_BYTE_WIDTH);
418 if (x >= LCD_TEXT_WIDTH)
424 void sed156x_reverse_at(int x, int y, int size)
429 if ((unsigned int)y >= LCD_TEXT_HEIGHT || (unsigned int)x >= LCD_TEXT_WIDTH)
432 p = &sw_screen[y * VIDEO_FONT_HEIGHT * LCD_BYTE_WIDTH + x * VIDEO_FONT_BYTE_WIDTH];
434 while (--size >= 0) {
436 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
437 for (j = 0; j < VIDEO_FONT_BYTE_WIDTH; j++, p++)
439 p += LCD_BYTE_WIDTH - VIDEO_FONT_BYTE_WIDTH;
441 p -= (LCD_BYTE_LINESZ - VIDEO_FONT_BYTE_WIDTH);
443 if (x >= LCD_TEXT_WIDTH)
449 void sed156x_scroll_line(void)
451 memmove(&sw_screen[0],
452 &sw_screen[LCD_BYTE_LINESZ],
453 LCD_BYTE_WIDTH * (LCD_HEIGHT - VIDEO_FONT_HEIGHT));
456 void sed156x_scroll(int dx, int dy)
458 u8 *p1 = NULL, *p2 = NULL, *p3 = NULL; /* pacify gcc */
461 adx = dx > 0 ? dx : -dx;
462 ady = dy > 0 ? dy : -dy;
464 /* overscroll? erase everything */
465 if (adx >= LCD_TEXT_WIDTH || ady >= LCD_TEXT_HEIGHT) {
466 memset(sw_screen, 0, sizeof(sw_screen));
470 sz = LCD_BYTE_LINESZ * ady;
474 p3 = &sw_screen[LCD_BYTE_WIDTH * LCD_HEIGHT - sz];
482 memmove(p1, p2, LCD_BYTE_WIDTH * LCD_HEIGHT - sz);
486 sz = VIDEO_FONT_BYTE_WIDTH * adx;
489 p2 = &sw_screen[0] + sz;
490 p3 = &sw_screen[0] + LCD_BYTE_WIDTH - sz;
492 p1 = &sw_screen[0] + sz;
499 for (i = 0; i < LCD_HEIGHT; i++) {
500 memmove(p1, p2, LCD_BYTE_WIDTH - sz);
502 p1 += LCD_BYTE_WIDTH;
503 p2 += LCD_BYTE_WIDTH;
504 p3 += LCD_BYTE_WIDTH;
509 void sed156x_init(void)
516 /* Send initialization commands to the LCD */
517 sed156x_cmd_transfer(LCD_OFF); /* Turn display OFF */
518 sed156x_cmd_transfer(LCD_BIAS); /* set the LCD Bias, */
519 sed156x_cmd_transfer(LCD_ADCMODE); /* ADC mode, */
520 sed156x_cmd_transfer(LCD_COMDIR); /* common output mode, */
521 sed156x_cmd_transfer(LCD_RESRT | LCD_RRATIO); /* resistor ratio, */
522 sed156x_cmd_transfer(LCD_EVSET); /* electronic volume, */
523 sed156x_cmd_transfer(LCD_CNTRST);
524 sed156x_cmd_transfer(LCD_PWRMD | LCD_POWERM); /* and power mode */
525 sed156x_cmd_transfer(LCD_PADDR | 0); /* cursor home */
526 sed156x_cmd_transfer(LCD_CADRH | 0);
527 sed156x_cmd_transfer(LCD_CADRL | 0);
528 sed156x_cmd_transfer(LCD_LADDR | 0); /* and display start line */
529 sed156x_cmd_transfer(LCD_DSP_NRM); /* LCD display Normal */
531 /* clear everything */
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));
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);
545 sed156x_cmd_transfer(LCD_ON); /* Turn display ON */