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>
25 #define DEFAULT_LCD_BWIDTH 40
26 #define DEFAULT_LCD_HWIDTH 64
28 /* Keep the backlight on this many seconds for each flash */
29 #define LCD_BL_TEMPO_PERIOD 4
31 #define LCD_FLAG_B 0x0004 /* Blink on */
32 #define LCD_FLAG_C 0x0008 /* Cursor on */
33 #define LCD_FLAG_D 0x0010 /* Display on */
34 #define LCD_FLAG_F 0x0020 /* Large font mode */
35 #define LCD_FLAG_N 0x0040 /* 2-rows mode */
36 #define LCD_FLAG_L 0x0080 /* Backlight enabled */
39 #define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
41 #define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
42 #define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
44 #define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
45 #define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
46 #define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
47 #define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
49 #define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
50 #define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
51 #define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
53 #define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
54 #define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
55 #define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
56 #define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
58 #define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
60 #define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
62 #define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
63 #define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
68 struct delayed_work bl_work;
69 struct mutex bl_tempo_lock; /* Protects access to bl_tempo */
74 /* contains the LCD config state */
75 unsigned long int flags;
77 /* Contains the LCD X and Y offset */
83 /* Current escape sequence and it's length or -1 if outside */
85 char buf[LCD_ESCAPE_LEN + 1];
89 unsigned long long drvdata[];
92 #define charlcd_to_priv(p) container_of(p, struct charlcd_priv, lcd)
94 /* Device single-open policy control */
95 static atomic_t charlcd_available = ATOMIC_INIT(1);
97 /* sleeps that many milliseconds with a reschedule */
98 static void long_sleep(int ms)
100 schedule_timeout_interruptible(msecs_to_jiffies(ms));
103 /* turn the backlight on or off */
104 static void charlcd_backlight(struct charlcd *lcd, int on)
106 struct charlcd_priv *priv = charlcd_to_priv(lcd);
108 if (!lcd->ops->backlight)
111 mutex_lock(&priv->bl_tempo_lock);
113 lcd->ops->backlight(lcd, on);
114 mutex_unlock(&priv->bl_tempo_lock);
117 static void charlcd_bl_off(struct work_struct *work)
119 struct delayed_work *dwork = to_delayed_work(work);
120 struct charlcd_priv *priv =
121 container_of(dwork, struct charlcd_priv, bl_work);
123 mutex_lock(&priv->bl_tempo_lock);
124 if (priv->bl_tempo) {
125 priv->bl_tempo = false;
126 if (!(priv->flags & LCD_FLAG_L))
127 priv->lcd.ops->backlight(&priv->lcd, 0);
129 mutex_unlock(&priv->bl_tempo_lock);
132 /* turn the backlight on for a little while */
133 void charlcd_poke(struct charlcd *lcd)
135 struct charlcd_priv *priv = charlcd_to_priv(lcd);
137 if (!lcd->ops->backlight)
140 cancel_delayed_work_sync(&priv->bl_work);
142 mutex_lock(&priv->bl_tempo_lock);
143 if (!priv->bl_tempo && !(priv->flags & LCD_FLAG_L))
144 lcd->ops->backlight(lcd, 1);
145 priv->bl_tempo = true;
146 schedule_delayed_work(&priv->bl_work, LCD_BL_TEMPO_PERIOD * HZ);
147 mutex_unlock(&priv->bl_tempo_lock);
149 EXPORT_SYMBOL_GPL(charlcd_poke);
151 static void charlcd_gotoxy(struct charlcd *lcd)
153 struct charlcd_priv *priv = charlcd_to_priv(lcd);
157 * we force the cursor to stay at the end of the
158 * line if it wants to go farther
160 addr = priv->addr.x < lcd->bwidth ? priv->addr.x & (lcd->hwidth - 1)
162 if (priv->addr.y & 1)
164 if (priv->addr.y & 2)
166 lcd->ops->write_cmd(lcd, LCD_CMD_SET_DDRAM_ADDR | addr);
169 static void charlcd_home(struct charlcd *lcd)
171 struct charlcd_priv *priv = charlcd_to_priv(lcd);
178 static void charlcd_print(struct charlcd *lcd, char c)
180 struct charlcd_priv *priv = charlcd_to_priv(lcd);
182 if (priv->addr.x < lcd->bwidth) {
184 c = lcd->char_conv[(unsigned char)c];
185 lcd->ops->write_data(lcd, c);
188 /* prevents the cursor from wrapping onto the next line */
189 if (priv->addr.x == lcd->bwidth)
194 static void charlcd_clear_fast(struct charlcd *lcd)
200 if (lcd->ops->clear_fast)
201 lcd->ops->clear_fast(lcd);
203 for (pos = 0; pos < min(2, lcd->height) * lcd->hwidth; pos++)
204 lcd->ops->write_data(lcd, ' ');
209 /* clears the display and resets X/Y */
210 static void charlcd_clear_display(struct charlcd *lcd)
212 struct charlcd_priv *priv = charlcd_to_priv(lcd);
214 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CLEAR);
217 /* we must wait a few milliseconds (15) */
221 static int charlcd_init_display(struct charlcd *lcd)
223 void (*write_cmd_raw)(struct charlcd *lcd, int cmd);
224 struct charlcd_priv *priv = charlcd_to_priv(lcd);
227 if (lcd->ifwidth != 4 && lcd->ifwidth != 8)
230 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
231 LCD_FLAG_C | LCD_FLAG_B;
233 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
236 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
237 * the LCD is in 8-bit mode afterwards
239 init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
240 if (lcd->ifwidth == 4) {
242 write_cmd_raw = lcd->ops->write_cmd_raw4;
244 write_cmd_raw = lcd->ops->write_cmd;
246 write_cmd_raw(lcd, init);
248 write_cmd_raw(lcd, init);
250 write_cmd_raw(lcd, init);
253 if (lcd->ifwidth == 4) {
254 /* Switch to 4-bit mode, 1 line, small fonts */
255 lcd->ops->write_cmd_raw4(lcd, LCD_CMD_FUNCTION_SET >> 4);
259 /* set font height and lines number */
260 lcd->ops->write_cmd(lcd,
261 LCD_CMD_FUNCTION_SET |
262 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
263 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
264 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
267 /* display off, cursor off, blink off */
268 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CTRL);
271 lcd->ops->write_cmd(lcd,
272 LCD_CMD_DISPLAY_CTRL | /* set display mode */
273 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
274 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
275 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
277 charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0);
281 /* entry mode set : increment, cursor shifting */
282 lcd->ops->write_cmd(lcd, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
284 charlcd_clear_display(lcd);
289 * Parses a movement command of the form "(.*);", where the group can be
290 * any number of subcommands of the form "(x|y)[0-9]+".
292 * Returns whether the command is valid. The position arguments are
293 * only written if the parsing was successful.
296 * - ";" returns (<original x>, <original y>).
297 * - "x1;" returns (1, <original y>).
298 * - "y2x1;" returns (1, 2).
299 * - "x12y34x56;" returns (56, 34).
305 * - "x12yy12;" fails.
308 static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
310 unsigned long new_x = *x;
311 unsigned long new_y = *y;
322 new_x = simple_strtoul(s + 1, &p, 10);
326 } else if (*s == 'y') {
327 new_y = simple_strtoul(s + 1, &p, 10);
342 * These are the file operation function for user access to /dev/lcd
343 * This function can also be called from inside the kernel, by
344 * setting file and ppos to NULL.
348 static inline int handle_lcd_special_code(struct charlcd *lcd)
350 struct charlcd_priv *priv = charlcd_to_priv(lcd);
352 /* LCD special codes */
356 char *esc = priv->esc_seq.buf + 2;
357 int oldflags = priv->flags;
359 /* check for display mode flags */
361 case 'D': /* Display ON */
362 priv->flags |= LCD_FLAG_D;
365 case 'd': /* Display OFF */
366 priv->flags &= ~LCD_FLAG_D;
369 case 'C': /* Cursor ON */
370 priv->flags |= LCD_FLAG_C;
373 case 'c': /* Cursor OFF */
374 priv->flags &= ~LCD_FLAG_C;
377 case 'B': /* Blink ON */
378 priv->flags |= LCD_FLAG_B;
381 case 'b': /* Blink OFF */
382 priv->flags &= ~LCD_FLAG_B;
385 case '+': /* Back light ON */
386 priv->flags |= LCD_FLAG_L;
389 case '-': /* Back light OFF */
390 priv->flags &= ~LCD_FLAG_L;
393 case '*': /* Flash back light */
397 case 'f': /* Small Font */
398 priv->flags &= ~LCD_FLAG_F;
401 case 'F': /* Large Font */
402 priv->flags |= LCD_FLAG_F;
405 case 'n': /* One Line */
406 priv->flags &= ~LCD_FLAG_N;
409 case 'N': /* Two Lines */
410 priv->flags |= LCD_FLAG_N;
413 case 'l': /* Shift Cursor Left */
414 if (priv->addr.x > 0) {
415 /* back one char if not at end of line */
416 if (priv->addr.x < lcd->bwidth)
417 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
422 case 'r': /* shift cursor right */
423 if (priv->addr.x < lcd->width) {
424 /* allow the cursor to pass the end of the line */
425 if (priv->addr.x < (lcd->bwidth - 1))
426 lcd->ops->write_cmd(lcd,
427 LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
432 case 'L': /* shift display left */
433 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
436 case 'R': /* shift display right */
437 lcd->ops->write_cmd(lcd,
438 LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
439 LCD_CMD_SHIFT_RIGHT);
442 case 'k': { /* kill end of line */
445 for (x = priv->addr.x; x < lcd->bwidth; x++)
446 lcd->ops->write_data(lcd, ' ');
448 /* restore cursor position */
453 case 'I': /* reinitialize display */
454 charlcd_init_display(lcd);
458 /* Generator : LGcxxxxx...xx; must have <c> between '0'
459 * and '7', representing the numerical ASCII code of the
460 * redefined character, and <xx...xx> a sequence of 16
461 * hex digits representing 8 bytes for each character.
462 * Most LCDs will only use 5 lower bits of the 7 first
466 unsigned char cgbytes[8];
467 unsigned char cgaddr;
473 if (!strchr(esc, ';'))
478 cgaddr = *(esc++) - '0';
487 while (*esc && cgoffset < 8) {
489 if (*esc >= '0' && *esc <= '9') {
490 value |= (*esc - '0') << shift;
491 } else if (*esc >= 'A' && *esc <= 'F') {
492 value |= (*esc - 'A' + 10) << shift;
493 } else if (*esc >= 'a' && *esc <= 'f') {
494 value |= (*esc - 'a' + 10) << shift;
501 cgbytes[cgoffset++] = value;
508 lcd->ops->write_cmd(lcd, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
509 for (addr = 0; addr < cgoffset; addr++)
510 lcd->ops->write_data(lcd, cgbytes[addr]);
512 /* ensures that we stop writing to CGRAM */
517 case 'x': /* gotoxy : LxXXX[yYYY]; */
518 case 'y': /* gotoxy : LyYYY[xXXX]; */
519 if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';')
522 /* If the command is valid, move to the new address */
523 if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
526 /* Regardless of its validity, mark as processed */
531 /* TODO: This indent party here got ugly, clean it! */
532 /* Check whether one flag was changed */
533 if (oldflags == priv->flags)
536 /* check whether one of B,C,D flags were changed */
537 if ((oldflags ^ priv->flags) &
538 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
539 /* set display mode */
540 lcd->ops->write_cmd(lcd,
541 LCD_CMD_DISPLAY_CTRL |
542 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
543 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
544 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
545 /* check whether one of F,N flags was changed */
546 else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
547 lcd->ops->write_cmd(lcd,
548 LCD_CMD_FUNCTION_SET |
549 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
550 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
551 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
552 /* check whether L flag was changed */
553 else if ((oldflags ^ priv->flags) & LCD_FLAG_L)
554 charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
559 static void charlcd_write_char(struct charlcd *lcd, char c)
561 struct charlcd_priv *priv = charlcd_to_priv(lcd);
563 /* first, we'll test if we're in escape mode */
564 if ((c != '\n') && priv->esc_seq.len >= 0) {
565 /* yes, let's add this char to the buffer */
566 priv->esc_seq.buf[priv->esc_seq.len++] = c;
567 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
569 /* aborts any previous escape sequence */
570 priv->esc_seq.len = -1;
573 case LCD_ESCAPE_CHAR:
574 /* start of an escape sequence */
575 priv->esc_seq.len = 0;
576 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
579 /* go back one char and clear it */
580 if (priv->addr.x > 0) {
582 * check if we're not at the
585 if (priv->addr.x < lcd->bwidth)
587 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
590 /* replace with a space */
591 lcd->ops->write_data(lcd, ' ');
592 /* back one char again */
593 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
596 /* quickly clear the display */
597 charlcd_clear_fast(lcd);
601 * flush the remainder of the current line and
602 * go to the beginning of the next line
604 for (; priv->addr.x < lcd->bwidth; priv->addr.x++)
605 lcd->ops->write_data(lcd, ' ');
607 priv->addr.y = (priv->addr.y + 1) % lcd->height;
611 /* go to the beginning of the same line */
616 /* print a space instead of the tab */
617 charlcd_print(lcd, ' ');
620 /* simply print this char */
621 charlcd_print(lcd, c);
627 * now we'll see if we're in an escape mode and if the current
628 * escape sequence can be understood.
630 if (priv->esc_seq.len >= 2) {
633 if (!strcmp(priv->esc_seq.buf, "[2J")) {
634 /* clear the display */
635 charlcd_clear_fast(lcd);
637 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
642 /* codes starting with ^[[L */
643 else if ((priv->esc_seq.len >= 3) &&
644 (priv->esc_seq.buf[0] == '[') &&
645 (priv->esc_seq.buf[1] == 'L')) {
646 processed = handle_lcd_special_code(lcd);
649 /* LCD special escape codes */
651 * flush the escape sequence if it's been processed
652 * or if it is getting too long.
654 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
655 priv->esc_seq.len = -1;
659 static struct charlcd *the_charlcd;
661 static ssize_t charlcd_write(struct file *file, const char __user *buf,
662 size_t count, loff_t *ppos)
664 const char __user *tmp = buf;
667 for (; count-- > 0; (*ppos)++, tmp++) {
668 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
670 * let's be a little nice with other processes
675 if (get_user(c, tmp))
678 charlcd_write_char(the_charlcd, c);
684 static int charlcd_open(struct inode *inode, struct file *file)
686 struct charlcd_priv *priv = charlcd_to_priv(the_charlcd);
690 if (!atomic_dec_and_test(&charlcd_available))
691 goto fail; /* open only once at a time */
694 if (file->f_mode & FMODE_READ) /* device is write-only */
697 if (priv->must_clear) {
698 charlcd_clear_display(&priv->lcd);
699 priv->must_clear = false;
701 return nonseekable_open(inode, file);
704 atomic_inc(&charlcd_available);
708 static int charlcd_release(struct inode *inode, struct file *file)
710 atomic_inc(&charlcd_available);
714 static const struct file_operations charlcd_fops = {
715 .write = charlcd_write,
716 .open = charlcd_open,
717 .release = charlcd_release,
721 static struct miscdevice charlcd_dev = {
724 .fops = &charlcd_fops,
727 static void charlcd_puts(struct charlcd *lcd, const char *s)
730 int count = strlen(s);
732 for (; count-- > 0; tmp++) {
733 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
735 * let's be a little nice with other processes
740 charlcd_write_char(lcd, *tmp);
744 #ifdef CONFIG_PANEL_BOOT_MESSAGE
745 #define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
747 #define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
750 #ifdef CONFIG_CHARLCD_BL_ON
751 #define LCD_INIT_BL "\x1b[L+"
752 #elif defined(CONFIG_CHARLCD_BL_FLASH)
753 #define LCD_INIT_BL "\x1b[L*"
755 #define LCD_INIT_BL "\x1b[L-"
758 /* initialize the LCD driver */
759 static int charlcd_init(struct charlcd *lcd)
761 struct charlcd_priv *priv = charlcd_to_priv(lcd);
764 if (lcd->ops->backlight) {
765 mutex_init(&priv->bl_tempo_lock);
766 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
770 * before this line, we must NOT send anything to the display.
771 * Since charlcd_init_display() needs to write data, we have to
772 * enable mark the LCD initialized just before.
774 ret = charlcd_init_display(lcd);
778 /* display a short message */
779 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT);
781 /* clear the display on the next device opening */
782 priv->must_clear = true;
787 struct charlcd *charlcd_alloc(unsigned int drvdata_size)
789 struct charlcd_priv *priv;
792 priv = kzalloc(sizeof(*priv) + drvdata_size, GFP_KERNEL);
796 priv->esc_seq.len = -1;
800 lcd->bwidth = DEFAULT_LCD_BWIDTH;
801 lcd->hwidth = DEFAULT_LCD_HWIDTH;
802 lcd->drvdata = priv->drvdata;
806 EXPORT_SYMBOL_GPL(charlcd_alloc);
808 void charlcd_free(struct charlcd *lcd)
810 kfree(charlcd_to_priv(lcd));
812 EXPORT_SYMBOL_GPL(charlcd_free);
814 static int panel_notify_sys(struct notifier_block *this, unsigned long code,
817 struct charlcd *lcd = the_charlcd;
822 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
825 charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
828 charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
836 static struct notifier_block panel_notifier = {
842 int charlcd_register(struct charlcd *lcd)
846 ret = charlcd_init(lcd);
850 ret = misc_register(&charlcd_dev);
855 register_reboot_notifier(&panel_notifier);
858 EXPORT_SYMBOL_GPL(charlcd_register);
860 int charlcd_unregister(struct charlcd *lcd)
862 struct charlcd_priv *priv = charlcd_to_priv(lcd);
864 unregister_reboot_notifier(&panel_notifier);
865 charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
866 misc_deregister(&charlcd_dev);
868 if (lcd->ops->backlight) {
869 cancel_delayed_work_sync(&priv->bl_work);
870 priv->lcd.ops->backlight(&priv->lcd, 0);
875 EXPORT_SYMBOL_GPL(charlcd_unregister);
877 MODULE_LICENSE("GPL");