2 * Character LCD driver for Linux
4 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
5 * Copyright (C) 2016-2017 Glider bvba
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/atomic.h>
14 #include <linux/delay.h>
16 #include <linux/miscdevice.h>
17 #include <linux/module.h>
18 #include <linux/notifier.h>
19 #include <linux/reboot.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/workqueue.h>
24 #include <generated/utsrelease.h>
26 #include <misc/charlcd.h>
30 #define DEFAULT_LCD_BWIDTH 40
31 #define DEFAULT_LCD_HWIDTH 64
33 /* Keep the backlight on this many seconds for each flash */
34 #define LCD_BL_TEMPO_PERIOD 4
36 #define LCD_FLAG_B 0x0004 /* Blink on */
37 #define LCD_FLAG_C 0x0008 /* Cursor on */
38 #define LCD_FLAG_D 0x0010 /* Display on */
39 #define LCD_FLAG_F 0x0020 /* Large font mode */
40 #define LCD_FLAG_N 0x0040 /* 2-rows mode */
41 #define LCD_FLAG_L 0x0080 /* Backlight enabled */
44 #define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
46 #define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
47 #define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
49 #define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
50 #define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
51 #define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
52 #define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
54 #define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
55 #define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
56 #define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
58 #define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
59 #define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
60 #define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
61 #define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
63 #define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
65 #define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
67 #define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
68 #define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
73 struct delayed_work bl_work
;
74 struct mutex bl_tempo_lock
; /* Protects access to bl_tempo */
79 /* contains the LCD config state */
80 unsigned long int flags
;
82 /* Contains the LCD X and Y offset */
88 /* Current escape sequence and it's length or -1 if outside */
90 char buf
[LCD_ESCAPE_LEN
+ 1];
94 unsigned long long drvdata
[0];
97 #define to_priv(p) container_of(p, struct charlcd_priv, lcd)
99 /* Device single-open policy control */
100 static atomic_t charlcd_available
= ATOMIC_INIT(1);
102 /* sleeps that many milliseconds with a reschedule */
103 static void long_sleep(int ms
)
108 schedule_timeout_interruptible(msecs_to_jiffies(ms
));
111 /* turn the backlight on or off */
112 static void charlcd_backlight(struct charlcd
*lcd
, int on
)
114 struct charlcd_priv
*priv
= to_priv(lcd
);
116 if (!lcd
->ops
->backlight
)
119 mutex_lock(&priv
->bl_tempo_lock
);
121 lcd
->ops
->backlight(lcd
, on
);
122 mutex_unlock(&priv
->bl_tempo_lock
);
125 static void charlcd_bl_off(struct work_struct
*work
)
127 struct delayed_work
*dwork
= to_delayed_work(work
);
128 struct charlcd_priv
*priv
=
129 container_of(dwork
, struct charlcd_priv
, bl_work
);
131 mutex_lock(&priv
->bl_tempo_lock
);
132 if (priv
->bl_tempo
) {
133 priv
->bl_tempo
= false;
134 if (!(priv
->flags
& LCD_FLAG_L
))
135 priv
->lcd
.ops
->backlight(&priv
->lcd
, 0);
137 mutex_unlock(&priv
->bl_tempo_lock
);
140 /* turn the backlight on for a little while */
141 void charlcd_poke(struct charlcd
*lcd
)
143 struct charlcd_priv
*priv
= to_priv(lcd
);
145 if (!lcd
->ops
->backlight
)
148 cancel_delayed_work_sync(&priv
->bl_work
);
150 mutex_lock(&priv
->bl_tempo_lock
);
151 if (!priv
->bl_tempo
&& !(priv
->flags
& LCD_FLAG_L
))
152 lcd
->ops
->backlight(lcd
, 1);
153 priv
->bl_tempo
= true;
154 schedule_delayed_work(&priv
->bl_work
, LCD_BL_TEMPO_PERIOD
* HZ
);
155 mutex_unlock(&priv
->bl_tempo_lock
);
157 EXPORT_SYMBOL_GPL(charlcd_poke
);
159 static void charlcd_gotoxy(struct charlcd
*lcd
)
161 struct charlcd_priv
*priv
= to_priv(lcd
);
165 * we force the cursor to stay at the end of the
166 * line if it wants to go farther
168 addr
= priv
->addr
.x
< lcd
->bwidth
? priv
->addr
.x
& (lcd
->hwidth
- 1)
170 if (priv
->addr
.y
& 1)
172 if (priv
->addr
.y
& 2)
174 lcd
->ops
->write_cmd(lcd
, LCD_CMD_SET_DDRAM_ADDR
| addr
);
177 static void charlcd_home(struct charlcd
*lcd
)
179 struct charlcd_priv
*priv
= to_priv(lcd
);
186 static void charlcd_print(struct charlcd
*lcd
, char c
)
188 struct charlcd_priv
*priv
= to_priv(lcd
);
190 if (priv
->addr
.x
< lcd
->bwidth
) {
192 c
= lcd
->char_conv
[(unsigned char)c
];
193 lcd
->ops
->write_data(lcd
, c
);
196 /* prevents the cursor from wrapping onto the next line */
197 if (priv
->addr
.x
== lcd
->bwidth
)
201 static void charlcd_clear_fast(struct charlcd
*lcd
)
207 if (lcd
->ops
->clear_fast
)
208 lcd
->ops
->clear_fast(lcd
);
210 for (pos
= 0; pos
< min(2, lcd
->height
) * lcd
->hwidth
; pos
++)
211 lcd
->ops
->write_data(lcd
, ' ');
216 /* clears the display and resets X/Y */
217 static void charlcd_clear_display(struct charlcd
*lcd
)
219 struct charlcd_priv
*priv
= to_priv(lcd
);
221 lcd
->ops
->write_cmd(lcd
, LCD_CMD_DISPLAY_CLEAR
);
224 /* we must wait a few milliseconds (15) */
228 static int charlcd_init_display(struct charlcd
*lcd
)
230 void (*write_cmd_raw
)(struct charlcd
*lcd
, int cmd
);
231 struct charlcd_priv
*priv
= to_priv(lcd
);
234 if (lcd
->ifwidth
!= 4 && lcd
->ifwidth
!= 8)
237 priv
->flags
= ((lcd
->height
> 1) ? LCD_FLAG_N
: 0) | LCD_FLAG_D
|
238 LCD_FLAG_C
| LCD_FLAG_B
;
240 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
243 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
244 * the LCD is in 8-bit mode afterwards
246 init
= LCD_CMD_FUNCTION_SET
| LCD_CMD_DATA_LEN_8BITS
;
247 if (lcd
->ifwidth
== 4) {
249 write_cmd_raw
= lcd
->ops
->write_cmd_raw4
;
251 write_cmd_raw
= lcd
->ops
->write_cmd
;
253 write_cmd_raw(lcd
, init
);
255 write_cmd_raw(lcd
, init
);
257 write_cmd_raw(lcd
, init
);
260 if (lcd
->ifwidth
== 4) {
261 /* Switch to 4-bit mode, 1 line, small fonts */
262 lcd
->ops
->write_cmd_raw4(lcd
, LCD_CMD_FUNCTION_SET
>> 4);
266 /* set font height and lines number */
267 lcd
->ops
->write_cmd(lcd
,
268 LCD_CMD_FUNCTION_SET
|
269 ((lcd
->ifwidth
== 8) ? LCD_CMD_DATA_LEN_8BITS
: 0) |
270 ((priv
->flags
& LCD_FLAG_F
) ? LCD_CMD_FONT_5X10_DOTS
: 0) |
271 ((priv
->flags
& LCD_FLAG_N
) ? LCD_CMD_TWO_LINES
: 0));
274 /* display off, cursor off, blink off */
275 lcd
->ops
->write_cmd(lcd
, LCD_CMD_DISPLAY_CTRL
);
278 lcd
->ops
->write_cmd(lcd
,
279 LCD_CMD_DISPLAY_CTRL
| /* set display mode */
280 ((priv
->flags
& LCD_FLAG_D
) ? LCD_CMD_DISPLAY_ON
: 0) |
281 ((priv
->flags
& LCD_FLAG_C
) ? LCD_CMD_CURSOR_ON
: 0) |
282 ((priv
->flags
& LCD_FLAG_B
) ? LCD_CMD_BLINK_ON
: 0));
284 charlcd_backlight(lcd
, (priv
->flags
& LCD_FLAG_L
) ? 1 : 0);
288 /* entry mode set : increment, cursor shifting */
289 lcd
->ops
->write_cmd(lcd
, LCD_CMD_ENTRY_MODE
| LCD_CMD_CURSOR_INC
);
291 charlcd_clear_display(lcd
);
296 * These are the file operation function for user access to /dev/lcd
297 * This function can also be called from inside the kernel, by
298 * setting file and ppos to NULL.
302 static inline int handle_lcd_special_code(struct charlcd
*lcd
)
304 struct charlcd_priv
*priv
= to_priv(lcd
);
306 /* LCD special codes */
310 char *esc
= priv
->esc_seq
.buf
+ 2;
311 int oldflags
= priv
->flags
;
313 /* check for display mode flags */
315 case 'D': /* Display ON */
316 priv
->flags
|= LCD_FLAG_D
;
319 case 'd': /* Display OFF */
320 priv
->flags
&= ~LCD_FLAG_D
;
323 case 'C': /* Cursor ON */
324 priv
->flags
|= LCD_FLAG_C
;
327 case 'c': /* Cursor OFF */
328 priv
->flags
&= ~LCD_FLAG_C
;
331 case 'B': /* Blink ON */
332 priv
->flags
|= LCD_FLAG_B
;
335 case 'b': /* Blink OFF */
336 priv
->flags
&= ~LCD_FLAG_B
;
339 case '+': /* Back light ON */
340 priv
->flags
|= LCD_FLAG_L
;
343 case '-': /* Back light OFF */
344 priv
->flags
&= ~LCD_FLAG_L
;
347 case '*': /* Flash back light */
351 case 'f': /* Small Font */
352 priv
->flags
&= ~LCD_FLAG_F
;
355 case 'F': /* Large Font */
356 priv
->flags
|= LCD_FLAG_F
;
359 case 'n': /* One Line */
360 priv
->flags
&= ~LCD_FLAG_N
;
363 case 'N': /* Two Lines */
364 priv
->flags
|= LCD_FLAG_N
;
366 case 'l': /* Shift Cursor Left */
367 if (priv
->addr
.x
> 0) {
368 /* back one char if not at end of line */
369 if (priv
->addr
.x
< lcd
->bwidth
)
370 lcd
->ops
->write_cmd(lcd
, LCD_CMD_SHIFT
);
375 case 'r': /* shift cursor right */
376 if (priv
->addr
.x
< lcd
->width
) {
377 /* allow the cursor to pass the end of the line */
378 if (priv
->addr
.x
< (lcd
->bwidth
- 1))
379 lcd
->ops
->write_cmd(lcd
,
380 LCD_CMD_SHIFT
| LCD_CMD_SHIFT_RIGHT
);
385 case 'L': /* shift display left */
386 lcd
->ops
->write_cmd(lcd
, LCD_CMD_SHIFT
| LCD_CMD_DISPLAY_SHIFT
);
389 case 'R': /* shift display right */
390 lcd
->ops
->write_cmd(lcd
,
391 LCD_CMD_SHIFT
| LCD_CMD_DISPLAY_SHIFT
|
392 LCD_CMD_SHIFT_RIGHT
);
395 case 'k': { /* kill end of line */
398 for (x
= priv
->addr
.x
; x
< lcd
->bwidth
; x
++)
399 lcd
->ops
->write_data(lcd
, ' ');
401 /* restore cursor position */
406 case 'I': /* reinitialize display */
407 charlcd_init_display(lcd
);
411 /* Generator : LGcxxxxx...xx; must have <c> between '0'
412 * and '7', representing the numerical ASCII code of the
413 * redefined character, and <xx...xx> a sequence of 16
414 * hex digits representing 8 bytes for each character.
415 * Most LCDs will only use 5 lower bits of the 7 first
419 unsigned char cgbytes
[8];
420 unsigned char cgaddr
;
426 if (!strchr(esc
, ';'))
431 cgaddr
= *(esc
++) - '0';
440 while (*esc
&& cgoffset
< 8) {
442 if (*esc
>= '0' && *esc
<= '9') {
443 value
|= (*esc
- '0') << shift
;
444 } else if (*esc
>= 'A' && *esc
<= 'Z') {
445 value
|= (*esc
- 'A' + 10) << shift
;
446 } else if (*esc
>= 'a' && *esc
<= 'z') {
447 value
|= (*esc
- 'a' + 10) << shift
;
454 cgbytes
[cgoffset
++] = value
;
461 lcd
->ops
->write_cmd(lcd
, LCD_CMD_SET_CGRAM_ADDR
| (cgaddr
* 8));
462 for (addr
= 0; addr
< cgoffset
; addr
++)
463 lcd
->ops
->write_data(lcd
, cgbytes
[addr
]);
465 /* ensures that we stop writing to CGRAM */
470 case 'x': /* gotoxy : LxXXX[yYYY]; */
471 case 'y': /* gotoxy : LyYYY[xXXX]; */
472 if (!strchr(esc
, ';'))
478 if (kstrtoul(esc
, 10, &priv
->addr
.x
) < 0)
480 } else if (*esc
== 'y') {
482 if (kstrtoul(esc
, 10, &priv
->addr
.y
) < 0)
494 /* TODO: This indent party here got ugly, clean it! */
495 /* Check whether one flag was changed */
496 if (oldflags
== priv
->flags
)
499 /* check whether one of B,C,D flags were changed */
500 if ((oldflags
^ priv
->flags
) &
501 (LCD_FLAG_B
| LCD_FLAG_C
| LCD_FLAG_D
))
502 /* set display mode */
503 lcd
->ops
->write_cmd(lcd
,
504 LCD_CMD_DISPLAY_CTRL
|
505 ((priv
->flags
& LCD_FLAG_D
) ? LCD_CMD_DISPLAY_ON
: 0) |
506 ((priv
->flags
& LCD_FLAG_C
) ? LCD_CMD_CURSOR_ON
: 0) |
507 ((priv
->flags
& LCD_FLAG_B
) ? LCD_CMD_BLINK_ON
: 0));
508 /* check whether one of F,N flags was changed */
509 else if ((oldflags
^ priv
->flags
) & (LCD_FLAG_F
| LCD_FLAG_N
))
510 lcd
->ops
->write_cmd(lcd
,
511 LCD_CMD_FUNCTION_SET
|
512 ((lcd
->ifwidth
== 8) ? LCD_CMD_DATA_LEN_8BITS
: 0) |
513 ((priv
->flags
& LCD_FLAG_F
) ? LCD_CMD_FONT_5X10_DOTS
: 0) |
514 ((priv
->flags
& LCD_FLAG_N
) ? LCD_CMD_TWO_LINES
: 0));
515 /* check whether L flag was changed */
516 else if ((oldflags
^ priv
->flags
) & LCD_FLAG_L
)
517 charlcd_backlight(lcd
, !!(priv
->flags
& LCD_FLAG_L
));
522 static void charlcd_write_char(struct charlcd
*lcd
, char c
)
524 struct charlcd_priv
*priv
= to_priv(lcd
);
526 /* first, we'll test if we're in escape mode */
527 if ((c
!= '\n') && priv
->esc_seq
.len
>= 0) {
528 /* yes, let's add this char to the buffer */
529 priv
->esc_seq
.buf
[priv
->esc_seq
.len
++] = c
;
530 priv
->esc_seq
.buf
[priv
->esc_seq
.len
] = 0;
532 /* aborts any previous escape sequence */
533 priv
->esc_seq
.len
= -1;
536 case LCD_ESCAPE_CHAR
:
537 /* start of an escape sequence */
538 priv
->esc_seq
.len
= 0;
539 priv
->esc_seq
.buf
[priv
->esc_seq
.len
] = 0;
542 /* go back one char and clear it */
543 if (priv
->addr
.x
> 0) {
545 * check if we're not at the
548 if (priv
->addr
.x
< lcd
->bwidth
)
550 lcd
->ops
->write_cmd(lcd
, LCD_CMD_SHIFT
);
553 /* replace with a space */
554 lcd
->ops
->write_data(lcd
, ' ');
555 /* back one char again */
556 lcd
->ops
->write_cmd(lcd
, LCD_CMD_SHIFT
);
559 /* quickly clear the display */
560 charlcd_clear_fast(lcd
);
564 * flush the remainder of the current line and
565 * go to the beginning of the next line
567 for (; priv
->addr
.x
< lcd
->bwidth
; priv
->addr
.x
++)
568 lcd
->ops
->write_data(lcd
, ' ');
570 priv
->addr
.y
= (priv
->addr
.y
+ 1) % lcd
->height
;
574 /* go to the beginning of the same line */
579 /* print a space instead of the tab */
580 charlcd_print(lcd
, ' ');
583 /* simply print this char */
584 charlcd_print(lcd
, c
);
590 * now we'll see if we're in an escape mode and if the current
591 * escape sequence can be understood.
593 if (priv
->esc_seq
.len
>= 2) {
596 if (!strcmp(priv
->esc_seq
.buf
, "[2J")) {
597 /* clear the display */
598 charlcd_clear_fast(lcd
);
600 } else if (!strcmp(priv
->esc_seq
.buf
, "[H")) {
605 /* codes starting with ^[[L */
606 else if ((priv
->esc_seq
.len
>= 3) &&
607 (priv
->esc_seq
.buf
[0] == '[') &&
608 (priv
->esc_seq
.buf
[1] == 'L')) {
609 processed
= handle_lcd_special_code(lcd
);
612 /* LCD special escape codes */
614 * flush the escape sequence if it's been processed
615 * or if it is getting too long.
617 if (processed
|| (priv
->esc_seq
.len
>= LCD_ESCAPE_LEN
))
618 priv
->esc_seq
.len
= -1;
622 static struct charlcd
*the_charlcd
;
624 static ssize_t
charlcd_write(struct file
*file
, const char __user
*buf
,
625 size_t count
, loff_t
*ppos
)
627 const char __user
*tmp
= buf
;
630 for (; count
-- > 0; (*ppos
)++, tmp
++) {
631 if (!in_interrupt() && (((count
+ 1) & 0x1f) == 0))
633 * let's be a little nice with other processes
638 if (get_user(c
, tmp
))
641 charlcd_write_char(the_charlcd
, c
);
647 static int charlcd_open(struct inode
*inode
, struct file
*file
)
649 struct charlcd_priv
*priv
= to_priv(the_charlcd
);
653 if (!atomic_dec_and_test(&charlcd_available
))
654 goto fail
; /* open only once at a time */
657 if (file
->f_mode
& FMODE_READ
) /* device is write-only */
660 if (priv
->must_clear
) {
661 charlcd_clear_display(&priv
->lcd
);
662 priv
->must_clear
= false;
664 return nonseekable_open(inode
, file
);
667 atomic_inc(&charlcd_available
);
671 static int charlcd_release(struct inode
*inode
, struct file
*file
)
673 atomic_inc(&charlcd_available
);
677 static const struct file_operations charlcd_fops
= {
678 .write
= charlcd_write
,
679 .open
= charlcd_open
,
680 .release
= charlcd_release
,
684 static struct miscdevice charlcd_dev
= {
687 .fops
= &charlcd_fops
,
690 static void charlcd_puts(struct charlcd
*lcd
, const char *s
)
693 int count
= strlen(s
);
695 for (; count
-- > 0; tmp
++) {
696 if (!in_interrupt() && (((count
+ 1) & 0x1f) == 0))
698 * let's be a little nice with other processes
703 charlcd_write_char(lcd
, *tmp
);
707 /* initialize the LCD driver */
708 static int charlcd_init(struct charlcd
*lcd
)
710 struct charlcd_priv
*priv
= to_priv(lcd
);
713 if (lcd
->ops
->backlight
) {
714 mutex_init(&priv
->bl_tempo_lock
);
715 INIT_DELAYED_WORK(&priv
->bl_work
, charlcd_bl_off
);
719 * before this line, we must NOT send anything to the display.
720 * Since charlcd_init_display() needs to write data, we have to
721 * enable mark the LCD initialized just before.
723 ret
= charlcd_init_display(lcd
);
727 /* display a short message */
728 #ifdef CONFIG_PANEL_CHANGE_MESSAGE
729 #ifdef CONFIG_PANEL_BOOT_MESSAGE
730 charlcd_puts(lcd
, "\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE
);
733 charlcd_puts(lcd
, "\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE
"\n");
735 /* clear the display on the next device opening */
736 priv
->must_clear
= true;
741 struct charlcd
*charlcd_alloc(unsigned int drvdata_size
)
743 struct charlcd_priv
*priv
;
746 priv
= kzalloc(sizeof(*priv
) + drvdata_size
, GFP_KERNEL
);
750 priv
->esc_seq
.len
= -1;
754 lcd
->bwidth
= DEFAULT_LCD_BWIDTH
;
755 lcd
->hwidth
= DEFAULT_LCD_HWIDTH
;
756 lcd
->drvdata
= priv
->drvdata
;
760 EXPORT_SYMBOL_GPL(charlcd_alloc
);
762 static int panel_notify_sys(struct notifier_block
*this, unsigned long code
,
765 struct charlcd
*lcd
= the_charlcd
;
770 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
773 charlcd_puts(lcd
, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
776 charlcd_puts(lcd
, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
784 static struct notifier_block panel_notifier
= {
790 int charlcd_register(struct charlcd
*lcd
)
794 ret
= charlcd_init(lcd
);
798 ret
= misc_register(&charlcd_dev
);
803 register_reboot_notifier(&panel_notifier
);
806 EXPORT_SYMBOL_GPL(charlcd_register
);
808 int charlcd_unregister(struct charlcd
*lcd
)
810 struct charlcd_priv
*priv
= to_priv(lcd
);
812 unregister_reboot_notifier(&panel_notifier
);
813 charlcd_puts(lcd
, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
814 misc_deregister(&charlcd_dev
);
816 if (lcd
->ops
->backlight
) {
817 cancel_delayed_work_sync(&priv
->bl_work
);
818 priv
->lcd
.ops
->backlight(&priv
->lcd
, 0);
823 EXPORT_SYMBOL_GPL(charlcd_unregister
);
825 MODULE_LICENSE("GPL");