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>
12 #include <linux/miscdevice.h>
13 #include <linux/module.h>
14 #include <linux/notifier.h>
15 #include <linux/reboot.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/workqueue.h>
20 #ifndef CONFIG_PANEL_BOOT_MESSAGE
21 #include <generated/utsrelease.h>
26 /* Keep the backlight on this many seconds for each flash */
27 #define LCD_BL_TEMPO_PERIOD 4
29 #define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
30 #define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
35 struct delayed_work bl_work
;
36 struct mutex bl_tempo_lock
; /* Protects access to bl_tempo */
41 /* contains the LCD config state */
44 /* Current escape sequence and it's length or -1 if outside */
46 char buf
[LCD_ESCAPE_LEN
+ 1];
50 unsigned long long drvdata
[];
53 #define charlcd_to_priv(p) container_of(p, struct charlcd_priv, lcd)
55 /* Device single-open policy control */
56 static atomic_t charlcd_available
= ATOMIC_INIT(1);
58 /* turn the backlight on or off */
59 void charlcd_backlight(struct charlcd
*lcd
, enum charlcd_onoff on
)
61 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
63 if (!lcd
->ops
->backlight
)
66 mutex_lock(&priv
->bl_tempo_lock
);
68 lcd
->ops
->backlight(lcd
, on
);
69 mutex_unlock(&priv
->bl_tempo_lock
);
71 EXPORT_SYMBOL_GPL(charlcd_backlight
);
73 static void charlcd_bl_off(struct work_struct
*work
)
75 struct delayed_work
*dwork
= to_delayed_work(work
);
76 struct charlcd_priv
*priv
=
77 container_of(dwork
, struct charlcd_priv
, bl_work
);
79 mutex_lock(&priv
->bl_tempo_lock
);
81 priv
->bl_tempo
= false;
82 if (!(priv
->flags
& LCD_FLAG_L
))
83 priv
->lcd
.ops
->backlight(&priv
->lcd
, CHARLCD_OFF
);
85 mutex_unlock(&priv
->bl_tempo_lock
);
88 /* turn the backlight on for a little while */
89 void charlcd_poke(struct charlcd
*lcd
)
91 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
93 if (!lcd
->ops
->backlight
)
96 cancel_delayed_work_sync(&priv
->bl_work
);
98 mutex_lock(&priv
->bl_tempo_lock
);
99 if (!priv
->bl_tempo
&& !(priv
->flags
& LCD_FLAG_L
))
100 lcd
->ops
->backlight(lcd
, CHARLCD_ON
);
101 priv
->bl_tempo
= true;
102 schedule_delayed_work(&priv
->bl_work
, LCD_BL_TEMPO_PERIOD
* HZ
);
103 mutex_unlock(&priv
->bl_tempo_lock
);
105 EXPORT_SYMBOL_GPL(charlcd_poke
);
107 static void charlcd_home(struct charlcd
*lcd
)
114 static void charlcd_print(struct charlcd
*lcd
, char c
)
116 if (lcd
->addr
.x
>= lcd
->width
)
120 c
= lcd
->char_conv
[(unsigned char)c
];
122 if (!lcd
->ops
->print(lcd
, c
))
125 /* prevents the cursor from wrapping onto the next line */
126 if (lcd
->addr
.x
== lcd
->width
)
127 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
- 1, lcd
->addr
.y
);
130 static void charlcd_clear_display(struct charlcd
*lcd
)
132 lcd
->ops
->clear_display(lcd
);
138 * Parses a movement command of the form "(.*);", where the group can be
139 * any number of subcommands of the form "(x|y)[0-9]+".
141 * Returns whether the command is valid. The position arguments are
142 * only written if the parsing was successful.
145 * - ";" returns (<original x>, <original y>).
146 * - "x1;" returns (1, <original y>).
147 * - "y2x1;" returns (1, 2).
148 * - "x12y34x56;" returns (56, 34).
154 * - "x12yy12;" fails.
157 static bool parse_xy(const char *s
, unsigned long *x
, unsigned long *y
)
159 unsigned long new_x
= *x
;
160 unsigned long new_y
= *y
;
171 new_x
= simple_strtoul(s
+ 1, &p
, 10);
175 } else if (*s
== 'y') {
176 new_y
= simple_strtoul(s
+ 1, &p
, 10);
191 * These are the file operation function for user access to /dev/lcd
192 * This function can also be called from inside the kernel, by
193 * setting file and ppos to NULL.
197 static inline int handle_lcd_special_code(struct charlcd
*lcd
)
199 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
201 /* LCD special codes */
205 char *esc
= priv
->esc_seq
.buf
+ 2;
206 int oldflags
= priv
->flags
;
208 /* check for display mode flags */
210 case 'D': /* Display ON */
211 priv
->flags
|= LCD_FLAG_D
;
212 if (priv
->flags
!= oldflags
)
213 lcd
->ops
->display(lcd
, CHARLCD_ON
);
217 case 'd': /* Display OFF */
218 priv
->flags
&= ~LCD_FLAG_D
;
219 if (priv
->flags
!= oldflags
)
220 lcd
->ops
->display(lcd
, CHARLCD_OFF
);
224 case 'C': /* Cursor ON */
225 priv
->flags
|= LCD_FLAG_C
;
226 if (priv
->flags
!= oldflags
)
227 lcd
->ops
->cursor(lcd
, CHARLCD_ON
);
231 case 'c': /* Cursor OFF */
232 priv
->flags
&= ~LCD_FLAG_C
;
233 if (priv
->flags
!= oldflags
)
234 lcd
->ops
->cursor(lcd
, CHARLCD_OFF
);
238 case 'B': /* Blink ON */
239 priv
->flags
|= LCD_FLAG_B
;
240 if (priv
->flags
!= oldflags
)
241 lcd
->ops
->blink(lcd
, CHARLCD_ON
);
245 case 'b': /* Blink OFF */
246 priv
->flags
&= ~LCD_FLAG_B
;
247 if (priv
->flags
!= oldflags
)
248 lcd
->ops
->blink(lcd
, CHARLCD_OFF
);
252 case '+': /* Back light ON */
253 priv
->flags
|= LCD_FLAG_L
;
254 if (priv
->flags
!= oldflags
)
255 charlcd_backlight(lcd
, CHARLCD_ON
);
259 case '-': /* Back light OFF */
260 priv
->flags
&= ~LCD_FLAG_L
;
261 if (priv
->flags
!= oldflags
)
262 charlcd_backlight(lcd
, CHARLCD_OFF
);
266 case '*': /* Flash back light */
270 case 'f': /* Small Font */
271 priv
->flags
&= ~LCD_FLAG_F
;
272 if (priv
->flags
!= oldflags
)
273 lcd
->ops
->fontsize(lcd
, CHARLCD_FONTSIZE_SMALL
);
277 case 'F': /* Large Font */
278 priv
->flags
|= LCD_FLAG_F
;
279 if (priv
->flags
!= oldflags
)
280 lcd
->ops
->fontsize(lcd
, CHARLCD_FONTSIZE_LARGE
);
284 case 'n': /* One Line */
285 priv
->flags
&= ~LCD_FLAG_N
;
286 if (priv
->flags
!= oldflags
)
287 lcd
->ops
->lines(lcd
, CHARLCD_LINES_1
);
291 case 'N': /* Two Lines */
292 priv
->flags
|= LCD_FLAG_N
;
293 if (priv
->flags
!= oldflags
)
294 lcd
->ops
->lines(lcd
, CHARLCD_LINES_2
);
298 case 'l': /* Shift Cursor Left */
299 if (lcd
->addr
.x
> 0) {
300 if (!lcd
->ops
->shift_cursor(lcd
, CHARLCD_SHIFT_LEFT
))
306 case 'r': /* shift cursor right */
307 if (lcd
->addr
.x
< lcd
->width
) {
308 if (!lcd
->ops
->shift_cursor(lcd
, CHARLCD_SHIFT_RIGHT
))
314 case 'L': /* shift display left */
315 lcd
->ops
->shift_display(lcd
, CHARLCD_SHIFT_LEFT
);
318 case 'R': /* shift display right */
319 lcd
->ops
->shift_display(lcd
, CHARLCD_SHIFT_RIGHT
);
322 case 'k': { /* kill end of line */
327 for (x
= lcd
->addr
.x
; x
< lcd
->width
; x
++)
328 lcd
->ops
->print(lcd
, ' ');
330 /* restore cursor position */
333 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
337 case 'I': /* reinitialize display */
338 lcd
->ops
->init_display(lcd
);
339 priv
->flags
= ((lcd
->height
> 1) ? LCD_FLAG_N
: 0) | LCD_FLAG_D
|
340 LCD_FLAG_C
| LCD_FLAG_B
;
344 if (lcd
->ops
->redefine_char
)
345 processed
= lcd
->ops
->redefine_char(lcd
, esc
);
350 case 'x': /* gotoxy : LxXXX[yYYY]; */
351 case 'y': /* gotoxy : LyYYY[xXXX]; */
352 if (priv
->esc_seq
.buf
[priv
->esc_seq
.len
- 1] != ';')
355 /* If the command is valid, move to the new address */
356 if (parse_xy(esc
, &lcd
->addr
.x
, &lcd
->addr
.y
))
357 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
359 /* Regardless of its validity, mark as processed */
367 static void charlcd_write_char(struct charlcd
*lcd
, char c
)
369 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
371 /* first, we'll test if we're in escape mode */
372 if ((c
!= '\n') && priv
->esc_seq
.len
>= 0) {
373 /* yes, let's add this char to the buffer */
374 priv
->esc_seq
.buf
[priv
->esc_seq
.len
++] = c
;
375 priv
->esc_seq
.buf
[priv
->esc_seq
.len
] = '\0';
377 /* aborts any previous escape sequence */
378 priv
->esc_seq
.len
= -1;
381 case LCD_ESCAPE_CHAR
:
382 /* start of an escape sequence */
383 priv
->esc_seq
.len
= 0;
384 priv
->esc_seq
.buf
[priv
->esc_seq
.len
] = '\0';
387 /* go back one char and clear it */
388 if (lcd
->addr
.x
> 0) {
390 if (!lcd
->ops
->shift_cursor(lcd
,
394 /* replace with a space */
395 charlcd_print(lcd
, ' ');
396 /* back one char again */
397 if (!lcd
->ops
->shift_cursor(lcd
, CHARLCD_SHIFT_LEFT
))
402 /* quickly clear the display */
403 charlcd_clear_display(lcd
);
407 * flush the remainder of the current line and
408 * go to the beginning of the next line
410 for (; lcd
->addr
.x
< lcd
->width
; lcd
->addr
.x
++)
411 lcd
->ops
->print(lcd
, ' ');
414 lcd
->addr
.y
= (lcd
->addr
.y
+ 1) % lcd
->height
;
415 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
418 /* go to the beginning of the same line */
420 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
423 /* print a space instead of the tab */
424 charlcd_print(lcd
, ' ');
427 /* simply print this char */
428 charlcd_print(lcd
, c
);
434 * now we'll see if we're in an escape mode and if the current
435 * escape sequence can be understood.
437 if (priv
->esc_seq
.len
>= 2) {
440 if (!strcmp(priv
->esc_seq
.buf
, "[2J")) {
441 /* clear the display */
442 charlcd_clear_display(lcd
);
444 } else if (!strcmp(priv
->esc_seq
.buf
, "[H")) {
449 /* codes starting with ^[[L */
450 else if ((priv
->esc_seq
.len
>= 3) &&
451 (priv
->esc_seq
.buf
[0] == '[') &&
452 (priv
->esc_seq
.buf
[1] == 'L')) {
453 processed
= handle_lcd_special_code(lcd
);
456 /* LCD special escape codes */
458 * flush the escape sequence if it's been processed
459 * or if it is getting too long.
461 if (processed
|| (priv
->esc_seq
.len
>= LCD_ESCAPE_LEN
))
462 priv
->esc_seq
.len
= -1;
466 static struct charlcd
*the_charlcd
;
468 static ssize_t
charlcd_write(struct file
*file
, const char __user
*buf
,
469 size_t count
, loff_t
*ppos
)
471 const char __user
*tmp
= buf
;
474 for (; count
-- > 0; (*ppos
)++, tmp
++) {
475 if (((count
+ 1) & 0x1f) == 0) {
477 * charlcd_write() is invoked as a VFS->write() callback
478 * and as such it is always invoked from preemptible
479 * context and may sleep.
484 if (get_user(c
, tmp
))
487 charlcd_write_char(the_charlcd
, c
);
493 static int charlcd_open(struct inode
*inode
, struct file
*file
)
495 struct charlcd_priv
*priv
= charlcd_to_priv(the_charlcd
);
499 if (!atomic_dec_and_test(&charlcd_available
))
500 goto fail
; /* open only once at a time */
503 if (file
->f_mode
& FMODE_READ
) /* device is write-only */
506 if (priv
->must_clear
) {
507 priv
->lcd
.ops
->clear_display(&priv
->lcd
);
508 priv
->must_clear
= false;
509 priv
->lcd
.addr
.x
= 0;
510 priv
->lcd
.addr
.y
= 0;
512 return nonseekable_open(inode
, file
);
515 atomic_inc(&charlcd_available
);
519 static int charlcd_release(struct inode
*inode
, struct file
*file
)
521 atomic_inc(&charlcd_available
);
525 static const struct file_operations charlcd_fops
= {
526 .write
= charlcd_write
,
527 .open
= charlcd_open
,
528 .release
= charlcd_release
,
531 static struct miscdevice charlcd_dev
= {
534 .fops
= &charlcd_fops
,
537 static void charlcd_puts(struct charlcd
*lcd
, const char *s
)
540 int count
= strlen(s
);
542 for (; count
-- > 0; tmp
++) {
543 if (((count
+ 1) & 0x1f) == 0)
546 charlcd_write_char(lcd
, *tmp
);
550 #ifdef CONFIG_PANEL_BOOT_MESSAGE
551 #define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
553 #define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
556 #ifdef CONFIG_CHARLCD_BL_ON
557 #define LCD_INIT_BL "\x1b[L+"
558 #elif defined(CONFIG_CHARLCD_BL_FLASH)
559 #define LCD_INIT_BL "\x1b[L*"
561 #define LCD_INIT_BL "\x1b[L-"
564 /* initialize the LCD driver */
565 static int charlcd_init(struct charlcd
*lcd
)
567 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
570 priv
->flags
= ((lcd
->height
> 1) ? LCD_FLAG_N
: 0) | LCD_FLAG_D
|
571 LCD_FLAG_C
| LCD_FLAG_B
;
572 if (lcd
->ops
->backlight
) {
573 mutex_init(&priv
->bl_tempo_lock
);
574 INIT_DELAYED_WORK(&priv
->bl_work
, charlcd_bl_off
);
578 * before this line, we must NOT send anything to the display.
579 * Since charlcd_init_display() needs to write data, we have to
580 * enable mark the LCD initialized just before.
582 if (WARN_ON(!lcd
->ops
->init_display
))
585 ret
= lcd
->ops
->init_display(lcd
);
589 /* display a short message */
590 charlcd_puts(lcd
, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT
);
592 /* clear the display on the next device opening */
593 priv
->must_clear
= true;
598 struct charlcd
*charlcd_alloc(void)
600 struct charlcd_priv
*priv
;
603 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
607 priv
->esc_seq
.len
= -1;
613 EXPORT_SYMBOL_GPL(charlcd_alloc
);
615 void charlcd_free(struct charlcd
*lcd
)
617 kfree(charlcd_to_priv(lcd
));
619 EXPORT_SYMBOL_GPL(charlcd_free
);
621 static int panel_notify_sys(struct notifier_block
*this, unsigned long code
,
624 struct charlcd
*lcd
= the_charlcd
;
629 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
632 charlcd_puts(lcd
, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
635 charlcd_puts(lcd
, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
643 static struct notifier_block panel_notifier
= {
644 .notifier_call
= panel_notify_sys
,
647 int charlcd_register(struct charlcd
*lcd
)
651 ret
= charlcd_init(lcd
);
655 ret
= misc_register(&charlcd_dev
);
660 register_reboot_notifier(&panel_notifier
);
663 EXPORT_SYMBOL_GPL(charlcd_register
);
665 int charlcd_unregister(struct charlcd
*lcd
)
667 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
669 unregister_reboot_notifier(&panel_notifier
);
670 charlcd_puts(lcd
, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
671 misc_deregister(&charlcd_dev
);
673 if (lcd
->ops
->backlight
) {
674 cancel_delayed_work_sync(&priv
->bl_work
);
675 priv
->lcd
.ops
->backlight(&priv
->lcd
, CHARLCD_OFF
);
680 EXPORT_SYMBOL_GPL(charlcd_unregister
);
682 MODULE_DESCRIPTION("Character LCD core support");
683 MODULE_LICENSE("GPL");