1 // SPDX-License-Identifier: GPL-2.0+
3 * Character LCD driver for Linux
5 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
6 * Copyright (C) 2016-2017 Glider bvba
9 #include <linux/atomic.h>
10 #include <linux/ctype.h>
11 #include <linux/delay.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>
21 #include <generated/utsrelease.h>
27 #define DEFAULT_LCD_BWIDTH 40
28 #define DEFAULT_LCD_HWIDTH 64
30 /* Keep the backlight on this many seconds for each flash */
31 #define LCD_BL_TEMPO_PERIOD 4
33 #define LCD_FLAG_B 0x0004 /* Blink on */
34 #define LCD_FLAG_C 0x0008 /* Cursor on */
35 #define LCD_FLAG_D 0x0010 /* Display on */
36 #define LCD_FLAG_F 0x0020 /* Large font mode */
37 #define LCD_FLAG_N 0x0040 /* 2-rows mode */
38 #define LCD_FLAG_L 0x0080 /* Backlight enabled */
41 #define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
43 #define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
44 #define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
46 #define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
47 #define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
48 #define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
49 #define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
51 #define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
52 #define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
53 #define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
55 #define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
56 #define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
57 #define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
58 #define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
60 #define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
62 #define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
64 #define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
65 #define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
70 struct delayed_work bl_work;
71 struct mutex bl_tempo_lock; /* Protects access to bl_tempo */
76 /* contains the LCD config state */
77 unsigned long int flags;
79 /* Contains the LCD X and Y offset */
85 /* Current escape sequence and it's length or -1 if outside */
87 char buf[LCD_ESCAPE_LEN + 1];
91 unsigned long long drvdata[0];
94 #define charlcd_to_priv(p) container_of(p, struct charlcd_priv, lcd)
96 /* Device single-open policy control */
97 static atomic_t charlcd_available = ATOMIC_INIT(1);
99 /* sleeps that many milliseconds with a reschedule */
100 static void long_sleep(int ms)
102 schedule_timeout_interruptible(msecs_to_jiffies(ms));
105 /* turn the backlight on or off */
106 static void charlcd_backlight(struct charlcd *lcd, int on)
108 struct charlcd_priv *priv = charlcd_to_priv(lcd);
110 if (!lcd->ops->backlight)
113 mutex_lock(&priv->bl_tempo_lock);
115 lcd->ops->backlight(lcd, on);
116 mutex_unlock(&priv->bl_tempo_lock);
119 static void charlcd_bl_off(struct work_struct *work)
121 struct delayed_work *dwork = to_delayed_work(work);
122 struct charlcd_priv *priv =
123 container_of(dwork, struct charlcd_priv, bl_work);
125 mutex_lock(&priv->bl_tempo_lock);
126 if (priv->bl_tempo) {
127 priv->bl_tempo = false;
128 if (!(priv->flags & LCD_FLAG_L))
129 priv->lcd.ops->backlight(&priv->lcd, 0);
131 mutex_unlock(&priv->bl_tempo_lock);
134 /* turn the backlight on for a little while */
135 void charlcd_poke(struct charlcd *lcd)
137 struct charlcd_priv *priv = charlcd_to_priv(lcd);
139 if (!lcd->ops->backlight)
142 cancel_delayed_work_sync(&priv->bl_work);
144 mutex_lock(&priv->bl_tempo_lock);
145 if (!priv->bl_tempo && !(priv->flags & LCD_FLAG_L))
146 lcd->ops->backlight(lcd, 1);
147 priv->bl_tempo = true;
148 schedule_delayed_work(&priv->bl_work, LCD_BL_TEMPO_PERIOD * HZ);
149 mutex_unlock(&priv->bl_tempo_lock);
151 EXPORT_SYMBOL_GPL(charlcd_poke);
153 static void charlcd_gotoxy(struct charlcd *lcd)
155 struct charlcd_priv *priv = charlcd_to_priv(lcd);
159 * we force the cursor to stay at the end of the
160 * line if it wants to go farther
162 addr = priv->addr.x < lcd->bwidth ? priv->addr.x & (lcd->hwidth - 1)
164 if (priv->addr.y & 1)
166 if (priv->addr.y & 2)
168 lcd->ops->write_cmd(lcd, LCD_CMD_SET_DDRAM_ADDR | addr);
171 static void charlcd_home(struct charlcd *lcd)
173 struct charlcd_priv *priv = charlcd_to_priv(lcd);
180 static void charlcd_print(struct charlcd *lcd, char c)
182 struct charlcd_priv *priv = charlcd_to_priv(lcd);
184 if (priv->addr.x < lcd->bwidth) {
186 c = lcd->char_conv[(unsigned char)c];
187 lcd->ops->write_data(lcd, c);
190 /* prevents the cursor from wrapping onto the next line */
191 if (priv->addr.x == lcd->bwidth)
196 static void charlcd_clear_fast(struct charlcd *lcd)
202 if (lcd->ops->clear_fast)
203 lcd->ops->clear_fast(lcd);
205 for (pos = 0; pos < min(2, lcd->height) * lcd->hwidth; pos++)
206 lcd->ops->write_data(lcd, ' ');
211 /* clears the display and resets X/Y */
212 static void charlcd_clear_display(struct charlcd *lcd)
214 struct charlcd_priv *priv = charlcd_to_priv(lcd);
216 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CLEAR);
219 /* we must wait a few milliseconds (15) */
223 static int charlcd_init_display(struct charlcd *lcd)
225 void (*write_cmd_raw)(struct charlcd *lcd, int cmd);
226 struct charlcd_priv *priv = charlcd_to_priv(lcd);
229 if (lcd->ifwidth != 4 && lcd->ifwidth != 8)
232 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
233 LCD_FLAG_C | LCD_FLAG_B;
235 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
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
241 init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
242 if (lcd->ifwidth == 4) {
244 write_cmd_raw = lcd->ops->write_cmd_raw4;
246 write_cmd_raw = lcd->ops->write_cmd;
248 write_cmd_raw(lcd, init);
250 write_cmd_raw(lcd, init);
252 write_cmd_raw(lcd, init);
255 if (lcd->ifwidth == 4) {
256 /* Switch to 4-bit mode, 1 line, small fonts */
257 lcd->ops->write_cmd_raw4(lcd, LCD_CMD_FUNCTION_SET >> 4);
261 /* set font height and lines number */
262 lcd->ops->write_cmd(lcd,
263 LCD_CMD_FUNCTION_SET |
264 ((lcd->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));
269 /* display off, cursor off, blink off */
270 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CTRL);
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));
279 charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0);
283 /* entry mode set : increment, cursor shifting */
284 lcd->ops->write_cmd(lcd, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
286 charlcd_clear_display(lcd);
291 * Parses an unsigned integer from a string, until a non-digit character
292 * is found. The empty string is not accepted. No overflow checks are done.
294 * Returns whether the parsing was successful. Only in that case
295 * the output parameters are written to.
297 * TODO: If the kernel adds an inplace version of kstrtoul(), this function
298 * could be easily replaced by that.
300 static bool parse_n(const char *s, unsigned long *res, const char **next_s)
306 while (isdigit(*s)) {
307 *res = *res * 10 + (*s - '0');
316 * Parses a movement command of the form "(.*);", where the group can be
317 * any number of subcommands of the form "(x|y)[0-9]+".
319 * Returns whether the command is valid. The position arguments are
320 * only written if the parsing was successful.
323 * - ";" returns (<original x>, <original y>).
324 * - "x1;" returns (1, <original y>).
325 * - "y2x1;" returns (1, 2).
326 * - "x12y34x56;" returns (56, 34).
332 * - "x12yy12;" fails.
335 static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
337 unsigned long new_x = *x;
338 unsigned long new_y = *y;
348 if (!parse_n(s + 1, &new_x, &s))
350 } else if (*s == 'y') {
351 if (!parse_n(s + 1, &new_y, &s))
364 * These are the file operation function for user access to /dev/lcd
365 * This function can also be called from inside the kernel, by
366 * setting file and ppos to NULL.
370 static inline int handle_lcd_special_code(struct charlcd *lcd)
372 struct charlcd_priv *priv = charlcd_to_priv(lcd);
374 /* LCD special codes */
378 char *esc = priv->esc_seq.buf + 2;
379 int oldflags = priv->flags;
381 /* check for display mode flags */
383 case 'D': /* Display ON */
384 priv->flags |= LCD_FLAG_D;
387 case 'd': /* Display OFF */
388 priv->flags &= ~LCD_FLAG_D;
391 case 'C': /* Cursor ON */
392 priv->flags |= LCD_FLAG_C;
395 case 'c': /* Cursor OFF */
396 priv->flags &= ~LCD_FLAG_C;
399 case 'B': /* Blink ON */
400 priv->flags |= LCD_FLAG_B;
403 case 'b': /* Blink OFF */
404 priv->flags &= ~LCD_FLAG_B;
407 case '+': /* Back light ON */
408 priv->flags |= LCD_FLAG_L;
411 case '-': /* Back light OFF */
412 priv->flags &= ~LCD_FLAG_L;
415 case '*': /* Flash back light */
419 case 'f': /* Small Font */
420 priv->flags &= ~LCD_FLAG_F;
423 case 'F': /* Large Font */
424 priv->flags |= LCD_FLAG_F;
427 case 'n': /* One Line */
428 priv->flags &= ~LCD_FLAG_N;
431 case 'N': /* Two Lines */
432 priv->flags |= LCD_FLAG_N;
435 case 'l': /* Shift Cursor Left */
436 if (priv->addr.x > 0) {
437 /* back one char if not at end of line */
438 if (priv->addr.x < lcd->bwidth)
439 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
444 case 'r': /* shift cursor right */
445 if (priv->addr.x < lcd->width) {
446 /* allow the cursor to pass the end of the line */
447 if (priv->addr.x < (lcd->bwidth - 1))
448 lcd->ops->write_cmd(lcd,
449 LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
454 case 'L': /* shift display left */
455 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
458 case 'R': /* shift display right */
459 lcd->ops->write_cmd(lcd,
460 LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
461 LCD_CMD_SHIFT_RIGHT);
464 case 'k': { /* kill end of line */
467 for (x = priv->addr.x; x < lcd->bwidth; x++)
468 lcd->ops->write_data(lcd, ' ');
470 /* restore cursor position */
475 case 'I': /* reinitialize display */
476 charlcd_init_display(lcd);
480 /* Generator : LGcxxxxx...xx; must have <c> between '0'
481 * and '7', representing the numerical ASCII code of the
482 * redefined character, and <xx...xx> a sequence of 16
483 * hex digits representing 8 bytes for each character.
484 * Most LCDs will only use 5 lower bits of the 7 first
488 unsigned char cgbytes[8];
489 unsigned char cgaddr;
495 if (!strchr(esc, ';'))
500 cgaddr = *(esc++) - '0';
509 while (*esc && cgoffset < 8) {
511 if (*esc >= '0' && *esc <= '9') {
512 value |= (*esc - '0') << shift;
513 } else if (*esc >= 'A' && *esc <= 'F') {
514 value |= (*esc - 'A' + 10) << shift;
515 } else if (*esc >= 'a' && *esc <= 'f') {
516 value |= (*esc - 'a' + 10) << shift;
523 cgbytes[cgoffset++] = value;
530 lcd->ops->write_cmd(lcd, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
531 for (addr = 0; addr < cgoffset; addr++)
532 lcd->ops->write_data(lcd, cgbytes[addr]);
534 /* ensures that we stop writing to CGRAM */
539 case 'x': /* gotoxy : LxXXX[yYYY]; */
540 case 'y': /* gotoxy : LyYYY[xXXX]; */
541 if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';')
544 /* If the command is valid, move to the new address */
545 if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
548 /* Regardless of its validity, mark as processed */
553 /* TODO: This indent party here got ugly, clean it! */
554 /* Check whether one flag was changed */
555 if (oldflags == priv->flags)
558 /* check whether one of B,C,D flags were changed */
559 if ((oldflags ^ priv->flags) &
560 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
561 /* set display mode */
562 lcd->ops->write_cmd(lcd,
563 LCD_CMD_DISPLAY_CTRL |
564 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
565 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
566 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
567 /* check whether one of F,N flags was changed */
568 else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
569 lcd->ops->write_cmd(lcd,
570 LCD_CMD_FUNCTION_SET |
571 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
572 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
573 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
574 /* check whether L flag was changed */
575 else if ((oldflags ^ priv->flags) & LCD_FLAG_L)
576 charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
581 static void charlcd_write_char(struct charlcd *lcd, char c)
583 struct charlcd_priv *priv = charlcd_to_priv(lcd);
585 /* first, we'll test if we're in escape mode */
586 if ((c != '\n') && priv->esc_seq.len >= 0) {
587 /* yes, let's add this char to the buffer */
588 priv->esc_seq.buf[priv->esc_seq.len++] = c;
589 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
591 /* aborts any previous escape sequence */
592 priv->esc_seq.len = -1;
595 case LCD_ESCAPE_CHAR:
596 /* start of an escape sequence */
597 priv->esc_seq.len = 0;
598 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
601 /* go back one char and clear it */
602 if (priv->addr.x > 0) {
604 * check if we're not at the
607 if (priv->addr.x < lcd->bwidth)
609 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
612 /* replace with a space */
613 lcd->ops->write_data(lcd, ' ');
614 /* back one char again */
615 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
618 /* quickly clear the display */
619 charlcd_clear_fast(lcd);
623 * flush the remainder of the current line and
624 * go to the beginning of the next line
626 for (; priv->addr.x < lcd->bwidth; priv->addr.x++)
627 lcd->ops->write_data(lcd, ' ');
629 priv->addr.y = (priv->addr.y + 1) % lcd->height;
633 /* go to the beginning of the same line */
638 /* print a space instead of the tab */
639 charlcd_print(lcd, ' ');
642 /* simply print this char */
643 charlcd_print(lcd, c);
649 * now we'll see if we're in an escape mode and if the current
650 * escape sequence can be understood.
652 if (priv->esc_seq.len >= 2) {
655 if (!strcmp(priv->esc_seq.buf, "[2J")) {
656 /* clear the display */
657 charlcd_clear_fast(lcd);
659 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
664 /* codes starting with ^[[L */
665 else if ((priv->esc_seq.len >= 3) &&
666 (priv->esc_seq.buf[0] == '[') &&
667 (priv->esc_seq.buf[1] == 'L')) {
668 processed = handle_lcd_special_code(lcd);
671 /* LCD special escape codes */
673 * flush the escape sequence if it's been processed
674 * or if it is getting too long.
676 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
677 priv->esc_seq.len = -1;
681 static struct charlcd *the_charlcd;
683 static ssize_t charlcd_write(struct file *file, const char __user *buf,
684 size_t count, loff_t *ppos)
686 const char __user *tmp = buf;
689 for (; count-- > 0; (*ppos)++, tmp++) {
690 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
692 * let's be a little nice with other processes
697 if (get_user(c, tmp))
700 charlcd_write_char(the_charlcd, c);
706 static int charlcd_open(struct inode *inode, struct file *file)
708 struct charlcd_priv *priv = charlcd_to_priv(the_charlcd);
712 if (!atomic_dec_and_test(&charlcd_available))
713 goto fail; /* open only once at a time */
716 if (file->f_mode & FMODE_READ) /* device is write-only */
719 if (priv->must_clear) {
720 charlcd_clear_display(&priv->lcd);
721 priv->must_clear = false;
723 return nonseekable_open(inode, file);
726 atomic_inc(&charlcd_available);
730 static int charlcd_release(struct inode *inode, struct file *file)
732 atomic_inc(&charlcd_available);
736 static const struct file_operations charlcd_fops = {
737 .write = charlcd_write,
738 .open = charlcd_open,
739 .release = charlcd_release,
743 static struct miscdevice charlcd_dev = {
746 .fops = &charlcd_fops,
749 static void charlcd_puts(struct charlcd *lcd, const char *s)
752 int count = strlen(s);
754 for (; count-- > 0; tmp++) {
755 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
757 * let's be a little nice with other processes
762 charlcd_write_char(lcd, *tmp);
766 #ifdef CONFIG_PANEL_BOOT_MESSAGE
767 #define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
769 #define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
772 #ifdef CONFIG_CHARLCD_BL_ON
773 #define LCD_INIT_BL "\x1b[L+"
774 #elif defined(CONFIG_CHARLCD_BL_FLASH)
775 #define LCD_INIT_BL "\x1b[L*"
777 #define LCD_INIT_BL "\x1b[L-"
780 /* initialize the LCD driver */
781 static int charlcd_init(struct charlcd *lcd)
783 struct charlcd_priv *priv = charlcd_to_priv(lcd);
786 if (lcd->ops->backlight) {
787 mutex_init(&priv->bl_tempo_lock);
788 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
792 * before this line, we must NOT send anything to the display.
793 * Since charlcd_init_display() needs to write data, we have to
794 * enable mark the LCD initialized just before.
796 ret = charlcd_init_display(lcd);
800 /* display a short message */
801 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT);
803 /* clear the display on the next device opening */
804 priv->must_clear = true;
809 struct charlcd *charlcd_alloc(unsigned int drvdata_size)
811 struct charlcd_priv *priv;
814 priv = kzalloc(sizeof(*priv) + drvdata_size, GFP_KERNEL);
818 priv->esc_seq.len = -1;
822 lcd->bwidth = DEFAULT_LCD_BWIDTH;
823 lcd->hwidth = DEFAULT_LCD_HWIDTH;
824 lcd->drvdata = priv->drvdata;
828 EXPORT_SYMBOL_GPL(charlcd_alloc);
830 void charlcd_free(struct charlcd *lcd)
832 kfree(charlcd_to_priv(lcd));
834 EXPORT_SYMBOL_GPL(charlcd_free);
836 static int panel_notify_sys(struct notifier_block *this, unsigned long code,
839 struct charlcd *lcd = the_charlcd;
844 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
847 charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
850 charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
858 static struct notifier_block panel_notifier = {
864 int charlcd_register(struct charlcd *lcd)
868 ret = charlcd_init(lcd);
872 ret = misc_register(&charlcd_dev);
877 register_reboot_notifier(&panel_notifier);
880 EXPORT_SYMBOL_GPL(charlcd_register);
882 int charlcd_unregister(struct charlcd *lcd)
884 struct charlcd_priv *priv = charlcd_to_priv(lcd);
886 unregister_reboot_notifier(&panel_notifier);
887 charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
888 misc_deregister(&charlcd_dev);
890 if (lcd->ops->backlight) {
891 cancel_delayed_work_sync(&priv->bl_work);
892 priv->lcd.ops->backlight(&priv->lcd, 0);
897 EXPORT_SYMBOL_GPL(charlcd_unregister);
899 MODULE_LICENSE("GPL");