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 #include <generated/utsrelease.h>
24 /* Keep the backlight on this many seconds for each flash */
25 #define LCD_BL_TEMPO_PERIOD 4
27 #define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
28 #define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
33 struct delayed_work bl_work
;
34 struct mutex bl_tempo_lock
; /* Protects access to bl_tempo */
39 /* contains the LCD config state */
40 unsigned long int flags
;
42 /* Current escape sequence and it's length or -1 if outside */
44 char buf
[LCD_ESCAPE_LEN
+ 1];
48 unsigned long long drvdata
[];
51 #define charlcd_to_priv(p) container_of(p, struct charlcd_priv, lcd)
53 /* Device single-open policy control */
54 static atomic_t charlcd_available
= ATOMIC_INIT(1);
56 /* turn the backlight on or off */
57 void charlcd_backlight(struct charlcd
*lcd
, enum charlcd_onoff on
)
59 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
61 if (!lcd
->ops
->backlight
)
64 mutex_lock(&priv
->bl_tempo_lock
);
66 lcd
->ops
->backlight(lcd
, on
);
67 mutex_unlock(&priv
->bl_tempo_lock
);
69 EXPORT_SYMBOL_GPL(charlcd_backlight
);
71 static void charlcd_bl_off(struct work_struct
*work
)
73 struct delayed_work
*dwork
= to_delayed_work(work
);
74 struct charlcd_priv
*priv
=
75 container_of(dwork
, struct charlcd_priv
, bl_work
);
77 mutex_lock(&priv
->bl_tempo_lock
);
79 priv
->bl_tempo
= false;
80 if (!(priv
->flags
& LCD_FLAG_L
))
81 priv
->lcd
.ops
->backlight(&priv
->lcd
, CHARLCD_OFF
);
83 mutex_unlock(&priv
->bl_tempo_lock
);
86 /* turn the backlight on for a little while */
87 void charlcd_poke(struct charlcd
*lcd
)
89 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
91 if (!lcd
->ops
->backlight
)
94 cancel_delayed_work_sync(&priv
->bl_work
);
96 mutex_lock(&priv
->bl_tempo_lock
);
97 if (!priv
->bl_tempo
&& !(priv
->flags
& LCD_FLAG_L
))
98 lcd
->ops
->backlight(lcd
, CHARLCD_ON
);
99 priv
->bl_tempo
= true;
100 schedule_delayed_work(&priv
->bl_work
, LCD_BL_TEMPO_PERIOD
* HZ
);
101 mutex_unlock(&priv
->bl_tempo_lock
);
103 EXPORT_SYMBOL_GPL(charlcd_poke
);
105 static void charlcd_home(struct charlcd
*lcd
)
112 static void charlcd_print(struct charlcd
*lcd
, char c
)
114 if (lcd
->addr
.x
>= lcd
->width
)
118 c
= lcd
->char_conv
[(unsigned char)c
];
120 if (!lcd
->ops
->print(lcd
, c
))
123 /* prevents the cursor from wrapping onto the next line */
124 if (lcd
->addr
.x
== lcd
->width
)
125 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
- 1, lcd
->addr
.y
);
128 static void charlcd_clear_display(struct charlcd
*lcd
)
130 lcd
->ops
->clear_display(lcd
);
136 * Parses a movement command of the form "(.*);", where the group can be
137 * any number of subcommands of the form "(x|y)[0-9]+".
139 * Returns whether the command is valid. The position arguments are
140 * only written if the parsing was successful.
143 * - ";" returns (<original x>, <original y>).
144 * - "x1;" returns (1, <original y>).
145 * - "y2x1;" returns (1, 2).
146 * - "x12y34x56;" returns (56, 34).
152 * - "x12yy12;" fails.
155 static bool parse_xy(const char *s
, unsigned long *x
, unsigned long *y
)
157 unsigned long new_x
= *x
;
158 unsigned long new_y
= *y
;
169 new_x
= simple_strtoul(s
+ 1, &p
, 10);
173 } else if (*s
== 'y') {
174 new_y
= simple_strtoul(s
+ 1, &p
, 10);
189 * These are the file operation function for user access to /dev/lcd
190 * This function can also be called from inside the kernel, by
191 * setting file and ppos to NULL.
195 static inline int handle_lcd_special_code(struct charlcd
*lcd
)
197 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
199 /* LCD special codes */
203 char *esc
= priv
->esc_seq
.buf
+ 2;
204 int oldflags
= priv
->flags
;
206 /* check for display mode flags */
208 case 'D': /* Display ON */
209 priv
->flags
|= LCD_FLAG_D
;
210 if (priv
->flags
!= oldflags
)
211 lcd
->ops
->display(lcd
, CHARLCD_ON
);
215 case 'd': /* Display OFF */
216 priv
->flags
&= ~LCD_FLAG_D
;
217 if (priv
->flags
!= oldflags
)
218 lcd
->ops
->display(lcd
, CHARLCD_OFF
);
222 case 'C': /* Cursor ON */
223 priv
->flags
|= LCD_FLAG_C
;
224 if (priv
->flags
!= oldflags
)
225 lcd
->ops
->cursor(lcd
, CHARLCD_ON
);
229 case 'c': /* Cursor OFF */
230 priv
->flags
&= ~LCD_FLAG_C
;
231 if (priv
->flags
!= oldflags
)
232 lcd
->ops
->cursor(lcd
, CHARLCD_OFF
);
236 case 'B': /* Blink ON */
237 priv
->flags
|= LCD_FLAG_B
;
238 if (priv
->flags
!= oldflags
)
239 lcd
->ops
->blink(lcd
, CHARLCD_ON
);
243 case 'b': /* Blink OFF */
244 priv
->flags
&= ~LCD_FLAG_B
;
245 if (priv
->flags
!= oldflags
)
246 lcd
->ops
->blink(lcd
, CHARLCD_OFF
);
250 case '+': /* Back light ON */
251 priv
->flags
|= LCD_FLAG_L
;
252 if (priv
->flags
!= oldflags
)
253 charlcd_backlight(lcd
, CHARLCD_ON
);
257 case '-': /* Back light OFF */
258 priv
->flags
&= ~LCD_FLAG_L
;
259 if (priv
->flags
!= oldflags
)
260 charlcd_backlight(lcd
, CHARLCD_OFF
);
264 case '*': /* Flash back light */
268 case 'f': /* Small Font */
269 priv
->flags
&= ~LCD_FLAG_F
;
270 if (priv
->flags
!= oldflags
)
271 lcd
->ops
->fontsize(lcd
, CHARLCD_FONTSIZE_SMALL
);
275 case 'F': /* Large Font */
276 priv
->flags
|= LCD_FLAG_F
;
277 if (priv
->flags
!= oldflags
)
278 lcd
->ops
->fontsize(lcd
, CHARLCD_FONTSIZE_LARGE
);
282 case 'n': /* One Line */
283 priv
->flags
&= ~LCD_FLAG_N
;
284 if (priv
->flags
!= oldflags
)
285 lcd
->ops
->lines(lcd
, CHARLCD_LINES_1
);
289 case 'N': /* Two Lines */
290 priv
->flags
|= LCD_FLAG_N
;
291 if (priv
->flags
!= oldflags
)
292 lcd
->ops
->lines(lcd
, CHARLCD_LINES_2
);
296 case 'l': /* Shift Cursor Left */
297 if (lcd
->addr
.x
> 0) {
298 if (!lcd
->ops
->shift_cursor(lcd
, CHARLCD_SHIFT_LEFT
))
304 case 'r': /* shift cursor right */
305 if (lcd
->addr
.x
< lcd
->width
) {
306 if (!lcd
->ops
->shift_cursor(lcd
, CHARLCD_SHIFT_RIGHT
))
312 case 'L': /* shift display left */
313 lcd
->ops
->shift_display(lcd
, CHARLCD_SHIFT_LEFT
);
316 case 'R': /* shift display right */
317 lcd
->ops
->shift_display(lcd
, CHARLCD_SHIFT_RIGHT
);
320 case 'k': { /* kill end of line */
325 for (x
= lcd
->addr
.x
; x
< lcd
->width
; x
++)
326 lcd
->ops
->print(lcd
, ' ');
328 /* restore cursor position */
331 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
335 case 'I': /* reinitialize display */
336 lcd
->ops
->init_display(lcd
);
337 priv
->flags
= ((lcd
->height
> 1) ? LCD_FLAG_N
: 0) | LCD_FLAG_D
|
338 LCD_FLAG_C
| LCD_FLAG_B
;
342 if (lcd
->ops
->redefine_char
)
343 processed
= lcd
->ops
->redefine_char(lcd
, esc
);
348 case 'x': /* gotoxy : LxXXX[yYYY]; */
349 case 'y': /* gotoxy : LyYYY[xXXX]; */
350 if (priv
->esc_seq
.buf
[priv
->esc_seq
.len
- 1] != ';')
353 /* If the command is valid, move to the new address */
354 if (parse_xy(esc
, &lcd
->addr
.x
, &lcd
->addr
.y
))
355 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
357 /* Regardless of its validity, mark as processed */
365 static void charlcd_write_char(struct charlcd
*lcd
, char c
)
367 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
369 /* first, we'll test if we're in escape mode */
370 if ((c
!= '\n') && priv
->esc_seq
.len
>= 0) {
371 /* yes, let's add this char to the buffer */
372 priv
->esc_seq
.buf
[priv
->esc_seq
.len
++] = c
;
373 priv
->esc_seq
.buf
[priv
->esc_seq
.len
] = '\0';
375 /* aborts any previous escape sequence */
376 priv
->esc_seq
.len
= -1;
379 case LCD_ESCAPE_CHAR
:
380 /* start of an escape sequence */
381 priv
->esc_seq
.len
= 0;
382 priv
->esc_seq
.buf
[priv
->esc_seq
.len
] = '\0';
385 /* go back one char and clear it */
386 if (lcd
->addr
.x
> 0) {
388 if (!lcd
->ops
->shift_cursor(lcd
,
392 /* replace with a space */
393 charlcd_print(lcd
, ' ');
394 /* back one char again */
395 if (!lcd
->ops
->shift_cursor(lcd
, CHARLCD_SHIFT_LEFT
))
400 /* quickly clear the display */
401 charlcd_clear_display(lcd
);
405 * flush the remainder of the current line and
406 * go to the beginning of the next line
408 for (; lcd
->addr
.x
< lcd
->width
; lcd
->addr
.x
++)
409 lcd
->ops
->print(lcd
, ' ');
412 lcd
->addr
.y
= (lcd
->addr
.y
+ 1) % lcd
->height
;
413 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
416 /* go to the beginning of the same line */
418 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
421 /* print a space instead of the tab */
422 charlcd_print(lcd
, ' ');
425 /* simply print this char */
426 charlcd_print(lcd
, c
);
432 * now we'll see if we're in an escape mode and if the current
433 * escape sequence can be understood.
435 if (priv
->esc_seq
.len
>= 2) {
438 if (!strcmp(priv
->esc_seq
.buf
, "[2J")) {
439 /* clear the display */
440 charlcd_clear_display(lcd
);
442 } else if (!strcmp(priv
->esc_seq
.buf
, "[H")) {
447 /* codes starting with ^[[L */
448 else if ((priv
->esc_seq
.len
>= 3) &&
449 (priv
->esc_seq
.buf
[0] == '[') &&
450 (priv
->esc_seq
.buf
[1] == 'L')) {
451 processed
= handle_lcd_special_code(lcd
);
454 /* LCD special escape codes */
456 * flush the escape sequence if it's been processed
457 * or if it is getting too long.
459 if (processed
|| (priv
->esc_seq
.len
>= LCD_ESCAPE_LEN
))
460 priv
->esc_seq
.len
= -1;
464 static struct charlcd
*the_charlcd
;
466 static ssize_t
charlcd_write(struct file
*file
, const char __user
*buf
,
467 size_t count
, loff_t
*ppos
)
469 const char __user
*tmp
= buf
;
472 for (; count
-- > 0; (*ppos
)++, tmp
++) {
473 if (!in_interrupt() && (((count
+ 1) & 0x1f) == 0))
475 * let's be a little nice with other processes
480 if (get_user(c
, tmp
))
483 charlcd_write_char(the_charlcd
, c
);
489 static int charlcd_open(struct inode
*inode
, struct file
*file
)
491 struct charlcd_priv
*priv
= charlcd_to_priv(the_charlcd
);
495 if (!atomic_dec_and_test(&charlcd_available
))
496 goto fail
; /* open only once at a time */
499 if (file
->f_mode
& FMODE_READ
) /* device is write-only */
502 if (priv
->must_clear
) {
503 priv
->lcd
.ops
->clear_display(&priv
->lcd
);
504 priv
->must_clear
= false;
505 priv
->lcd
.addr
.x
= 0;
506 priv
->lcd
.addr
.y
= 0;
508 return nonseekable_open(inode
, file
);
511 atomic_inc(&charlcd_available
);
515 static int charlcd_release(struct inode
*inode
, struct file
*file
)
517 atomic_inc(&charlcd_available
);
521 static const struct file_operations charlcd_fops
= {
522 .write
= charlcd_write
,
523 .open
= charlcd_open
,
524 .release
= charlcd_release
,
528 static struct miscdevice charlcd_dev
= {
531 .fops
= &charlcd_fops
,
534 static void charlcd_puts(struct charlcd
*lcd
, const char *s
)
537 int count
= strlen(s
);
539 for (; count
-- > 0; tmp
++) {
540 if (!in_interrupt() && (((count
+ 1) & 0x1f) == 0))
542 * let's be a little nice with other processes
547 charlcd_write_char(lcd
, *tmp
);
551 #ifdef CONFIG_PANEL_BOOT_MESSAGE
552 #define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
554 #define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
557 #ifdef CONFIG_CHARLCD_BL_ON
558 #define LCD_INIT_BL "\x1b[L+"
559 #elif defined(CONFIG_CHARLCD_BL_FLASH)
560 #define LCD_INIT_BL "\x1b[L*"
562 #define LCD_INIT_BL "\x1b[L-"
565 /* initialize the LCD driver */
566 static int charlcd_init(struct charlcd
*lcd
)
568 struct charlcd_priv
*priv
= charlcd_to_priv(lcd
);
571 priv
->flags
= ((lcd
->height
> 1) ? LCD_FLAG_N
: 0) | LCD_FLAG_D
|
572 LCD_FLAG_C
| LCD_FLAG_B
;
573 if (lcd
->ops
->backlight
) {
574 mutex_init(&priv
->bl_tempo_lock
);
575 INIT_DELAYED_WORK(&priv
->bl_work
, charlcd_bl_off
);
579 * before this line, we must NOT send anything to the display.
580 * Since charlcd_init_display() needs to write data, we have to
581 * enable mark the LCD initialized just before.
583 ret
= lcd
->ops
->init_display(lcd
);
587 /* display a short message */
588 charlcd_puts(lcd
, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT
);
590 /* clear the display on the next device opening */
591 priv
->must_clear
= true;
596 struct charlcd
*charlcd_alloc(void)
598 struct charlcd_priv
*priv
;
601 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
605 priv
->esc_seq
.len
= -1;
611 EXPORT_SYMBOL_GPL(charlcd_alloc
);
613 void charlcd_free(struct charlcd
*lcd
)
615 kfree(charlcd_to_priv(lcd
));
617 EXPORT_SYMBOL_GPL(charlcd_free
);
619 static int panel_notify_sys(struct notifier_block
*this, unsigned long code
,
622 struct charlcd
*lcd
= the_charlcd
;
627 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
630 charlcd_puts(lcd
, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
633 charlcd_puts(lcd
, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
641 static struct notifier_block panel_notifier
= {
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_LICENSE("GPL");