xref: /openbmc/linux/drivers/auxdisplay/charlcd.c (revision 3fc04dd7)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Character LCD driver for Linux
4  *
5  * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
6  * Copyright (C) 2016-2017 Glider bvba
7  */
8 
9 #include <linux/atomic.h>
10 #include <linux/ctype.h>
11 #include <linux/delay.h>
12 #include <linux/fs.h>
13 #include <linux/miscdevice.h>
14 #include <linux/module.h>
15 #include <linux/notifier.h>
16 #include <linux/reboot.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/workqueue.h>
20 
21 #include <generated/utsrelease.h>
22 
23 #include "charlcd.h"
24 #include "hd44780_common.h"
25 
26 /* Keep the backlight on this many seconds for each flash */
27 #define LCD_BL_TEMPO_PERIOD	4
28 
29 #define LCD_FLAG_B		0x0004	/* Blink on */
30 #define LCD_FLAG_C		0x0008	/* Cursor on */
31 #define LCD_FLAG_D		0x0010	/* Display on */
32 #define LCD_FLAG_F		0x0020	/* Large font mode */
33 #define LCD_FLAG_N		0x0040	/* 2-rows mode */
34 #define LCD_FLAG_L		0x0080	/* Backlight enabled */
35 
36 /* LCD commands */
37 #define LCD_CMD_DISPLAY_CLEAR	0x01	/* Clear entire display */
38 
39 #define LCD_CMD_ENTRY_MODE	0x04	/* Set entry mode */
40 #define LCD_CMD_CURSOR_INC	0x02	/* Increment cursor */
41 
42 #define LCD_CMD_DISPLAY_CTRL	0x08	/* Display control */
43 #define LCD_CMD_DISPLAY_ON	0x04	/* Set display on */
44 #define LCD_CMD_CURSOR_ON	0x02	/* Set cursor on */
45 #define LCD_CMD_BLINK_ON	0x01	/* Set blink on */
46 
47 #define LCD_CMD_SHIFT		0x10	/* Shift cursor/display */
48 #define LCD_CMD_DISPLAY_SHIFT	0x08	/* Shift display instead of cursor */
49 #define LCD_CMD_SHIFT_RIGHT	0x04	/* Shift display/cursor to the right */
50 
51 #define LCD_CMD_FUNCTION_SET	0x20	/* Set function */
52 #define LCD_CMD_DATA_LEN_8BITS	0x10	/* Set data length to 8 bits */
53 #define LCD_CMD_TWO_LINES	0x08	/* Set to two display lines */
54 #define LCD_CMD_FONT_5X10_DOTS	0x04	/* Set char font to 5x10 dots */
55 
56 #define LCD_CMD_SET_CGRAM_ADDR	0x40	/* Set char generator RAM address */
57 
58 #define LCD_CMD_SET_DDRAM_ADDR	0x80	/* Set display data RAM address */
59 
60 #define LCD_ESCAPE_LEN		24	/* Max chars for LCD escape command */
61 #define LCD_ESCAPE_CHAR		27	/* Use char 27 for escape command */
62 
63 struct charlcd_priv {
64 	struct charlcd lcd;
65 
66 	struct delayed_work bl_work;
67 	struct mutex bl_tempo_lock;	/* Protects access to bl_tempo */
68 	bool bl_tempo;
69 
70 	bool must_clear;
71 
72 	/* contains the LCD config state */
73 	unsigned long int flags;
74 
75 	/* Contains the LCD X and Y offset */
76 	struct {
77 		unsigned long int x;
78 		unsigned long int y;
79 	} addr;
80 
81 	/* Current escape sequence and it's length or -1 if outside */
82 	struct {
83 		char buf[LCD_ESCAPE_LEN + 1];
84 		int len;
85 	} esc_seq;
86 
87 	unsigned long long drvdata[];
88 };
89 
90 #define charlcd_to_priv(p)	container_of(p, struct charlcd_priv, lcd)
91 
92 /* Device single-open policy control */
93 static atomic_t charlcd_available = ATOMIC_INIT(1);
94 
95 /* sleeps that many milliseconds with a reschedule */
96 static void long_sleep(int ms)
97 {
98 	schedule_timeout_interruptible(msecs_to_jiffies(ms));
99 }
100 
101 /* turn the backlight on or off */
102 static void charlcd_backlight(struct charlcd *lcd, enum charlcd_onoff on)
103 {
104 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
105 
106 	if (!lcd->ops->backlight)
107 		return;
108 
109 	mutex_lock(&priv->bl_tempo_lock);
110 	if (!priv->bl_tempo)
111 		lcd->ops->backlight(lcd, on);
112 	mutex_unlock(&priv->bl_tempo_lock);
113 }
114 
115 static void charlcd_bl_off(struct work_struct *work)
116 {
117 	struct delayed_work *dwork = to_delayed_work(work);
118 	struct charlcd_priv *priv =
119 		container_of(dwork, struct charlcd_priv, bl_work);
120 
121 	mutex_lock(&priv->bl_tempo_lock);
122 	if (priv->bl_tempo) {
123 		priv->bl_tempo = false;
124 		if (!(priv->flags & LCD_FLAG_L))
125 			priv->lcd.ops->backlight(&priv->lcd, 0);
126 	}
127 	mutex_unlock(&priv->bl_tempo_lock);
128 }
129 
130 /* turn the backlight on for a little while */
131 void charlcd_poke(struct charlcd *lcd)
132 {
133 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
134 
135 	if (!lcd->ops->backlight)
136 		return;
137 
138 	cancel_delayed_work_sync(&priv->bl_work);
139 
140 	mutex_lock(&priv->bl_tempo_lock);
141 	if (!priv->bl_tempo && !(priv->flags & LCD_FLAG_L))
142 		lcd->ops->backlight(lcd, 1);
143 	priv->bl_tempo = true;
144 	schedule_delayed_work(&priv->bl_work, LCD_BL_TEMPO_PERIOD * HZ);
145 	mutex_unlock(&priv->bl_tempo_lock);
146 }
147 EXPORT_SYMBOL_GPL(charlcd_poke);
148 
149 static void charlcd_gotoxy(struct charlcd *lcd)
150 {
151 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
152 	struct hd44780_common *hdc = lcd->drvdata;
153 	unsigned int addr;
154 
155 	/*
156 	 * we force the cursor to stay at the end of the
157 	 * line if it wants to go farther
158 	 */
159 	addr = priv->addr.x < hdc->bwidth ? priv->addr.x & (hdc->hwidth - 1)
160 					  : hdc->bwidth - 1;
161 	if (priv->addr.y & 1)
162 		addr += hdc->hwidth;
163 	if (priv->addr.y & 2)
164 		addr += hdc->bwidth;
165 	lcd->ops->write_cmd(lcd, LCD_CMD_SET_DDRAM_ADDR | addr);
166 }
167 
168 static void charlcd_home(struct charlcd *lcd)
169 {
170 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
171 
172 	priv->addr.x = 0;
173 	priv->addr.y = 0;
174 	charlcd_gotoxy(lcd);
175 }
176 
177 static void charlcd_print(struct charlcd *lcd, char c)
178 {
179 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
180 	struct hd44780_common *hdc = lcd->drvdata;
181 
182 	if (priv->addr.x < hdc->bwidth) {
183 		if (lcd->char_conv)
184 			c = lcd->char_conv[(unsigned char)c];
185 		lcd->ops->write_data(lcd, c);
186 		priv->addr.x++;
187 
188 		/* prevents the cursor from wrapping onto the next line */
189 		if (priv->addr.x == hdc->bwidth)
190 			charlcd_gotoxy(lcd);
191 	}
192 }
193 
194 static void charlcd_clear_fast(struct charlcd *lcd)
195 {
196 	struct hd44780_common *hdc = lcd->drvdata;
197 	int pos;
198 
199 	charlcd_home(lcd);
200 
201 	if (lcd->ops->clear_fast)
202 		lcd->ops->clear_fast(lcd);
203 	else
204 		for (pos = 0; pos < min(2, lcd->height) * hdc->hwidth; pos++)
205 			lcd->ops->write_data(lcd, ' ');
206 
207 	charlcd_home(lcd);
208 }
209 
210 /* clears the display and resets X/Y */
211 static void charlcd_clear_display(struct charlcd *lcd)
212 {
213 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
214 
215 	lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CLEAR);
216 	priv->addr.x = 0;
217 	priv->addr.y = 0;
218 	/* we must wait a few milliseconds (15) */
219 	long_sleep(15);
220 }
221 
222 static int charlcd_init_display(struct charlcd *lcd)
223 {
224 	void (*write_cmd_raw)(struct charlcd *lcd, int cmd);
225 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
226 	struct hd44780_common *hdc = lcd->drvdata;
227 	u8 init;
228 
229 	if (hdc->ifwidth != 4 && hdc->ifwidth != 8)
230 		return -EINVAL;
231 
232 	priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
233 		      LCD_FLAG_C | LCD_FLAG_B;
234 
235 	long_sleep(20);		/* wait 20 ms after power-up for the paranoid */
236 
237 	/*
238 	 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
239 	 * the LCD is in 8-bit mode afterwards
240 	 */
241 	init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
242 	if (hdc->ifwidth == 4) {
243 		init >>= 4;
244 		write_cmd_raw = lcd->ops->write_cmd_raw4;
245 	} else {
246 		write_cmd_raw = lcd->ops->write_cmd;
247 	}
248 	write_cmd_raw(lcd, init);
249 	long_sleep(10);
250 	write_cmd_raw(lcd, init);
251 	long_sleep(10);
252 	write_cmd_raw(lcd, init);
253 	long_sleep(10);
254 
255 	if (hdc->ifwidth == 4) {
256 		/* Switch to 4-bit mode, 1 line, small fonts */
257 		lcd->ops->write_cmd_raw4(lcd, LCD_CMD_FUNCTION_SET >> 4);
258 		long_sleep(10);
259 	}
260 
261 	/* set font height and lines number */
262 	lcd->ops->write_cmd(lcd,
263 		LCD_CMD_FUNCTION_SET |
264 		((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
265 		((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
266 		((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
267 	long_sleep(10);
268 
269 	/* display off, cursor off, blink off */
270 	lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CTRL);
271 	long_sleep(10);
272 
273 	lcd->ops->write_cmd(lcd,
274 		LCD_CMD_DISPLAY_CTRL |	/* set display mode */
275 		((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
276 		((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
277 		((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
278 
279 	charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0);
280 
281 	long_sleep(10);
282 
283 	/* entry mode set : increment, cursor shifting */
284 	lcd->ops->write_cmd(lcd, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
285 
286 	charlcd_clear_display(lcd);
287 	return 0;
288 }
289 
290 /*
291  * Parses a movement command of the form "(.*);", where the group can be
292  * any number of subcommands of the form "(x|y)[0-9]+".
293  *
294  * Returns whether the command is valid. The position arguments are
295  * only written if the parsing was successful.
296  *
297  * For instance:
298  *   - ";"          returns (<original x>, <original y>).
299  *   - "x1;"        returns (1, <original y>).
300  *   - "y2x1;"      returns (1, 2).
301  *   - "x12y34x56;" returns (56, 34).
302  *   - ""           fails.
303  *   - "x"          fails.
304  *   - "x;"         fails.
305  *   - "x1"         fails.
306  *   - "xy12;"      fails.
307  *   - "x12yy12;"   fails.
308  *   - "xx"         fails.
309  */
310 static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
311 {
312 	unsigned long new_x = *x;
313 	unsigned long new_y = *y;
314 	char *p;
315 
316 	for (;;) {
317 		if (!*s)
318 			return false;
319 
320 		if (*s == ';')
321 			break;
322 
323 		if (*s == 'x') {
324 			new_x = simple_strtoul(s + 1, &p, 10);
325 			if (p == s + 1)
326 				return false;
327 			s = p;
328 		} else if (*s == 'y') {
329 			new_y = simple_strtoul(s + 1, &p, 10);
330 			if (p == s + 1)
331 				return false;
332 			s = p;
333 		} else {
334 			return false;
335 		}
336 	}
337 
338 	*x = new_x;
339 	*y = new_y;
340 	return true;
341 }
342 
343 /*
344  * These are the file operation function for user access to /dev/lcd
345  * This function can also be called from inside the kernel, by
346  * setting file and ppos to NULL.
347  *
348  */
349 
350 static inline int handle_lcd_special_code(struct charlcd *lcd)
351 {
352 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
353 	struct hd44780_common *hdc = lcd->drvdata;
354 
355 	/* LCD special codes */
356 
357 	int processed = 0;
358 
359 	char *esc = priv->esc_seq.buf + 2;
360 	int oldflags = priv->flags;
361 
362 	/* check for display mode flags */
363 	switch (*esc) {
364 	case 'D':	/* Display ON */
365 		priv->flags |= LCD_FLAG_D;
366 		processed = 1;
367 		break;
368 	case 'd':	/* Display OFF */
369 		priv->flags &= ~LCD_FLAG_D;
370 		processed = 1;
371 		break;
372 	case 'C':	/* Cursor ON */
373 		priv->flags |= LCD_FLAG_C;
374 		processed = 1;
375 		break;
376 	case 'c':	/* Cursor OFF */
377 		priv->flags &= ~LCD_FLAG_C;
378 		processed = 1;
379 		break;
380 	case 'B':	/* Blink ON */
381 		priv->flags |= LCD_FLAG_B;
382 		processed = 1;
383 		break;
384 	case 'b':	/* Blink OFF */
385 		priv->flags &= ~LCD_FLAG_B;
386 		processed = 1;
387 		break;
388 	case '+':	/* Back light ON */
389 		priv->flags |= LCD_FLAG_L;
390 		processed = 1;
391 		break;
392 	case '-':	/* Back light OFF */
393 		priv->flags &= ~LCD_FLAG_L;
394 		processed = 1;
395 		break;
396 	case '*':	/* Flash back light */
397 		charlcd_poke(lcd);
398 		processed = 1;
399 		break;
400 	case 'f':	/* Small Font */
401 		priv->flags &= ~LCD_FLAG_F;
402 		processed = 1;
403 		break;
404 	case 'F':	/* Large Font */
405 		priv->flags |= LCD_FLAG_F;
406 		processed = 1;
407 		break;
408 	case 'n':	/* One Line */
409 		priv->flags &= ~LCD_FLAG_N;
410 		processed = 1;
411 		break;
412 	case 'N':	/* Two Lines */
413 		priv->flags |= LCD_FLAG_N;
414 		processed = 1;
415 		break;
416 	case 'l':	/* Shift Cursor Left */
417 		if (priv->addr.x > 0) {
418 			/* back one char if not at end of line */
419 			if (priv->addr.x < hdc->bwidth)
420 				lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
421 			priv->addr.x--;
422 		}
423 		processed = 1;
424 		break;
425 	case 'r':	/* shift cursor right */
426 		if (priv->addr.x < lcd->width) {
427 			/* allow the cursor to pass the end of the line */
428 			if (priv->addr.x < (hdc->bwidth - 1))
429 				lcd->ops->write_cmd(lcd,
430 					LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
431 			priv->addr.x++;
432 		}
433 		processed = 1;
434 		break;
435 	case 'L':	/* shift display left */
436 		lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
437 		processed = 1;
438 		break;
439 	case 'R':	/* shift display right */
440 		lcd->ops->write_cmd(lcd,
441 				    LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
442 				    LCD_CMD_SHIFT_RIGHT);
443 		processed = 1;
444 		break;
445 	case 'k': {	/* kill end of line */
446 		int x;
447 
448 		for (x = priv->addr.x; x < hdc->bwidth; x++)
449 			lcd->ops->write_data(lcd, ' ');
450 
451 		/* restore cursor position */
452 		charlcd_gotoxy(lcd);
453 		processed = 1;
454 		break;
455 	}
456 	case 'I':	/* reinitialize display */
457 		charlcd_init_display(lcd);
458 		processed = 1;
459 		break;
460 	case 'G': {
461 		/* Generator : LGcxxxxx...xx; must have <c> between '0'
462 		 * and '7', representing the numerical ASCII code of the
463 		 * redefined character, and <xx...xx> a sequence of 16
464 		 * hex digits representing 8 bytes for each character.
465 		 * Most LCDs will only use 5 lower bits of the 7 first
466 		 * bytes.
467 		 */
468 
469 		unsigned char cgbytes[8];
470 		unsigned char cgaddr;
471 		int cgoffset;
472 		int shift;
473 		char value;
474 		int addr;
475 
476 		if (!strchr(esc, ';'))
477 			break;
478 
479 		esc++;
480 
481 		cgaddr = *(esc++) - '0';
482 		if (cgaddr > 7) {
483 			processed = 1;
484 			break;
485 		}
486 
487 		cgoffset = 0;
488 		shift = 0;
489 		value = 0;
490 		while (*esc && cgoffset < 8) {
491 			int half;
492 
493 			shift ^= 4;
494 
495 			half = hex_to_bin(*esc++);
496 			if (half < 0)
497 				continue;
498 
499 			value |= half << shift;
500 			if (shift == 0) {
501 				cgbytes[cgoffset++] = value;
502 				value = 0;
503 			}
504 		}
505 
506 		lcd->ops->write_cmd(lcd, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
507 		for (addr = 0; addr < cgoffset; addr++)
508 			lcd->ops->write_data(lcd, cgbytes[addr]);
509 
510 		/* ensures that we stop writing to CGRAM */
511 		charlcd_gotoxy(lcd);
512 		processed = 1;
513 		break;
514 	}
515 	case 'x':	/* gotoxy : LxXXX[yYYY]; */
516 	case 'y':	/* gotoxy : LyYYY[xXXX]; */
517 		if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';')
518 			break;
519 
520 		/* If the command is valid, move to the new address */
521 		if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
522 			charlcd_gotoxy(lcd);
523 
524 		/* Regardless of its validity, mark as processed */
525 		processed = 1;
526 		break;
527 	}
528 
529 	/* TODO: This indent party here got ugly, clean it! */
530 	/* Check whether one flag was changed */
531 	if (oldflags == priv->flags)
532 		return processed;
533 
534 	/* check whether one of B,C,D flags were changed */
535 	if ((oldflags ^ priv->flags) &
536 	    (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
537 		/* set display mode */
538 		lcd->ops->write_cmd(lcd,
539 			LCD_CMD_DISPLAY_CTRL |
540 			((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
541 			((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
542 			((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
543 	/* check whether one of F,N flags was changed */
544 	else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
545 		lcd->ops->write_cmd(lcd,
546 			LCD_CMD_FUNCTION_SET |
547 			((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
548 			((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
549 			((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
550 	/* check whether L flag was changed */
551 	else if ((oldflags ^ priv->flags) & LCD_FLAG_L)
552 		charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
553 
554 	return processed;
555 }
556 
557 static void charlcd_write_char(struct charlcd *lcd, char c)
558 {
559 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
560 	struct hd44780_common *hdc = lcd->drvdata;
561 
562 	/* first, we'll test if we're in escape mode */
563 	if ((c != '\n') && priv->esc_seq.len >= 0) {
564 		/* yes, let's add this char to the buffer */
565 		priv->esc_seq.buf[priv->esc_seq.len++] = c;
566 		priv->esc_seq.buf[priv->esc_seq.len] = '\0';
567 	} else {
568 		/* aborts any previous escape sequence */
569 		priv->esc_seq.len = -1;
570 
571 		switch (c) {
572 		case LCD_ESCAPE_CHAR:
573 			/* start of an escape sequence */
574 			priv->esc_seq.len = 0;
575 			priv->esc_seq.buf[priv->esc_seq.len] = '\0';
576 			break;
577 		case '\b':
578 			/* go back one char and clear it */
579 			if (priv->addr.x > 0) {
580 				/*
581 				 * check if we're not at the
582 				 * end of the line
583 				 */
584 				if (priv->addr.x < hdc->bwidth)
585 					/* back one char */
586 					lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
587 				priv->addr.x--;
588 			}
589 			/* replace with a space */
590 			lcd->ops->write_data(lcd, ' ');
591 			/* back one char again */
592 			lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
593 			break;
594 		case '\f':
595 			/* quickly clear the display */
596 			charlcd_clear_fast(lcd);
597 			break;
598 		case '\n':
599 			/*
600 			 * flush the remainder of the current line and
601 			 * go to the beginning of the next line
602 			 */
603 			for (; priv->addr.x < hdc->bwidth; priv->addr.x++)
604 				lcd->ops->write_data(lcd, ' ');
605 			priv->addr.x = 0;
606 			priv->addr.y = (priv->addr.y + 1) % lcd->height;
607 			charlcd_gotoxy(lcd);
608 			break;
609 		case '\r':
610 			/* go to the beginning of the same line */
611 			priv->addr.x = 0;
612 			charlcd_gotoxy(lcd);
613 			break;
614 		case '\t':
615 			/* print a space instead of the tab */
616 			charlcd_print(lcd, ' ');
617 			break;
618 		default:
619 			/* simply print this char */
620 			charlcd_print(lcd, c);
621 			break;
622 		}
623 	}
624 
625 	/*
626 	 * now we'll see if we're in an escape mode and if the current
627 	 * escape sequence can be understood.
628 	 */
629 	if (priv->esc_seq.len >= 2) {
630 		int processed = 0;
631 
632 		if (!strcmp(priv->esc_seq.buf, "[2J")) {
633 			/* clear the display */
634 			charlcd_clear_fast(lcd);
635 			processed = 1;
636 		} else if (!strcmp(priv->esc_seq.buf, "[H")) {
637 			/* cursor to home */
638 			charlcd_home(lcd);
639 			processed = 1;
640 		}
641 		/* codes starting with ^[[L */
642 		else if ((priv->esc_seq.len >= 3) &&
643 			 (priv->esc_seq.buf[0] == '[') &&
644 			 (priv->esc_seq.buf[1] == 'L')) {
645 			processed = handle_lcd_special_code(lcd);
646 		}
647 
648 		/* LCD special escape codes */
649 		/*
650 		 * flush the escape sequence if it's been processed
651 		 * or if it is getting too long.
652 		 */
653 		if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
654 			priv->esc_seq.len = -1;
655 	} /* escape codes */
656 }
657 
658 static struct charlcd *the_charlcd;
659 
660 static ssize_t charlcd_write(struct file *file, const char __user *buf,
661 			     size_t count, loff_t *ppos)
662 {
663 	const char __user *tmp = buf;
664 	char c;
665 
666 	for (; count-- > 0; (*ppos)++, tmp++) {
667 		if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
668 			/*
669 			 * let's be a little nice with other processes
670 			 * that need some CPU
671 			 */
672 			schedule();
673 
674 		if (get_user(c, tmp))
675 			return -EFAULT;
676 
677 		charlcd_write_char(the_charlcd, c);
678 	}
679 
680 	return tmp - buf;
681 }
682 
683 static int charlcd_open(struct inode *inode, struct file *file)
684 {
685 	struct charlcd_priv *priv = charlcd_to_priv(the_charlcd);
686 	int ret;
687 
688 	ret = -EBUSY;
689 	if (!atomic_dec_and_test(&charlcd_available))
690 		goto fail;	/* open only once at a time */
691 
692 	ret = -EPERM;
693 	if (file->f_mode & FMODE_READ)	/* device is write-only */
694 		goto fail;
695 
696 	if (priv->must_clear) {
697 		charlcd_clear_display(&priv->lcd);
698 		priv->must_clear = false;
699 	}
700 	return nonseekable_open(inode, file);
701 
702  fail:
703 	atomic_inc(&charlcd_available);
704 	return ret;
705 }
706 
707 static int charlcd_release(struct inode *inode, struct file *file)
708 {
709 	atomic_inc(&charlcd_available);
710 	return 0;
711 }
712 
713 static const struct file_operations charlcd_fops = {
714 	.write   = charlcd_write,
715 	.open    = charlcd_open,
716 	.release = charlcd_release,
717 	.llseek  = no_llseek,
718 };
719 
720 static struct miscdevice charlcd_dev = {
721 	.minor	= LCD_MINOR,
722 	.name	= "lcd",
723 	.fops	= &charlcd_fops,
724 };
725 
726 static void charlcd_puts(struct charlcd *lcd, const char *s)
727 {
728 	const char *tmp = s;
729 	int count = strlen(s);
730 
731 	for (; count-- > 0; tmp++) {
732 		if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
733 			/*
734 			 * let's be a little nice with other processes
735 			 * that need some CPU
736 			 */
737 			schedule();
738 
739 		charlcd_write_char(lcd, *tmp);
740 	}
741 }
742 
743 #ifdef CONFIG_PANEL_BOOT_MESSAGE
744 #define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
745 #else
746 #define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
747 #endif
748 
749 #ifdef CONFIG_CHARLCD_BL_ON
750 #define LCD_INIT_BL "\x1b[L+"
751 #elif defined(CONFIG_CHARLCD_BL_FLASH)
752 #define LCD_INIT_BL "\x1b[L*"
753 #else
754 #define LCD_INIT_BL "\x1b[L-"
755 #endif
756 
757 /* initialize the LCD driver */
758 static int charlcd_init(struct charlcd *lcd)
759 {
760 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
761 	int ret;
762 
763 	if (lcd->ops->backlight) {
764 		mutex_init(&priv->bl_tempo_lock);
765 		INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
766 	}
767 
768 	/*
769 	 * before this line, we must NOT send anything to the display.
770 	 * Since charlcd_init_display() needs to write data, we have to
771 	 * enable mark the LCD initialized just before.
772 	 */
773 	ret = charlcd_init_display(lcd);
774 	if (ret)
775 		return ret;
776 
777 	/* display a short message */
778 	charlcd_puts(lcd, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT);
779 
780 	/* clear the display on the next device opening */
781 	priv->must_clear = true;
782 	charlcd_home(lcd);
783 	return 0;
784 }
785 
786 struct charlcd *charlcd_alloc(void)
787 {
788 	struct charlcd_priv *priv;
789 	struct charlcd *lcd;
790 
791 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
792 	if (!priv)
793 		return NULL;
794 
795 	priv->esc_seq.len = -1;
796 
797 	lcd = &priv->lcd;
798 
799 	return lcd;
800 }
801 EXPORT_SYMBOL_GPL(charlcd_alloc);
802 
803 void charlcd_free(struct charlcd *lcd)
804 {
805 	kfree(charlcd_to_priv(lcd));
806 }
807 EXPORT_SYMBOL_GPL(charlcd_free);
808 
809 static int panel_notify_sys(struct notifier_block *this, unsigned long code,
810 			    void *unused)
811 {
812 	struct charlcd *lcd = the_charlcd;
813 
814 	switch (code) {
815 	case SYS_DOWN:
816 		charlcd_puts(lcd,
817 			     "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
818 		break;
819 	case SYS_HALT:
820 		charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
821 		break;
822 	case SYS_POWER_OFF:
823 		charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
824 		break;
825 	default:
826 		break;
827 	}
828 	return NOTIFY_DONE;
829 }
830 
831 static struct notifier_block panel_notifier = {
832 	panel_notify_sys,
833 	NULL,
834 	0
835 };
836 
837 int charlcd_register(struct charlcd *lcd)
838 {
839 	int ret;
840 
841 	ret = charlcd_init(lcd);
842 	if (ret)
843 		return ret;
844 
845 	ret = misc_register(&charlcd_dev);
846 	if (ret)
847 		return ret;
848 
849 	the_charlcd = lcd;
850 	register_reboot_notifier(&panel_notifier);
851 	return 0;
852 }
853 EXPORT_SYMBOL_GPL(charlcd_register);
854 
855 int charlcd_unregister(struct charlcd *lcd)
856 {
857 	struct charlcd_priv *priv = charlcd_to_priv(lcd);
858 
859 	unregister_reboot_notifier(&panel_notifier);
860 	charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
861 	misc_deregister(&charlcd_dev);
862 	the_charlcd = NULL;
863 	if (lcd->ops->backlight) {
864 		cancel_delayed_work_sync(&priv->bl_work);
865 		priv->lcd.ops->backlight(&priv->lcd, 0);
866 	}
867 
868 	return 0;
869 }
870 EXPORT_SYMBOL_GPL(charlcd_unregister);
871 
872 MODULE_LICENSE("GPL");
873