blob: 930ffb2fb31787c50d1fea5f2299d6a77b963691 [file] [log] [blame]
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001/*
2 * Character LCD driver for Linux
3 *
4 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
5 * Copyright (C) 2016-2017 Glider bvba
6 *
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.
11 */
12
13#include <linux/atomic.h>
14#include <linux/delay.h>
15#include <linux/fs.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>
23
24#include <generated/utsrelease.h>
25
26#include <misc/charlcd.h>
27
28#define LCD_MINOR 156
29
30#define DEFAULT_LCD_BWIDTH 40
31#define DEFAULT_LCD_HWIDTH 64
32
33/* Keep the backlight on this many seconds for each flash */
34#define LCD_BL_TEMPO_PERIOD 4
35
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 */
42
43/* LCD commands */
44#define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
45
46#define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
47#define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
48
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 */
53
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 */
57
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 */
62
63#define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
64
65#define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
66
67#define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
68#define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
69
70struct charlcd_priv {
71 struct charlcd lcd;
72
73 struct delayed_work bl_work;
74 struct mutex bl_tempo_lock; /* Protects access to bl_tempo */
75 bool bl_tempo;
76
77 bool must_clear;
78
79 /* contains the LCD config state */
80 unsigned long int flags;
81
82 /* Contains the LCD X and Y offset */
83 struct {
84 unsigned long int x;
85 unsigned long int y;
86 } addr;
87
88 /* Current escape sequence and it's length or -1 if outside */
89 struct {
90 char buf[LCD_ESCAPE_LEN + 1];
91 int len;
92 } esc_seq;
93
94 unsigned long long drvdata[0];
95};
96
97#define to_priv(p) container_of(p, struct charlcd_priv, lcd)
98
99/* Device single-open policy control */
100static atomic_t charlcd_available = ATOMIC_INIT(1);
101
102/* sleeps that many milliseconds with a reschedule */
103static void long_sleep(int ms)
104{
105 if (in_interrupt())
106 mdelay(ms);
107 else
108 schedule_timeout_interruptible(msecs_to_jiffies(ms));
109}
110
111/* turn the backlight on or off */
112static void charlcd_backlight(struct charlcd *lcd, int on)
113{
114 struct charlcd_priv *priv = to_priv(lcd);
115
116 if (!lcd->ops->backlight)
117 return;
118
119 mutex_lock(&priv->bl_tempo_lock);
120 if (!priv->bl_tempo)
121 lcd->ops->backlight(lcd, on);
122 mutex_unlock(&priv->bl_tempo_lock);
123}
124
125static void charlcd_bl_off(struct work_struct *work)
126{
127 struct delayed_work *dwork = to_delayed_work(work);
128 struct charlcd_priv *priv =
129 container_of(dwork, struct charlcd_priv, bl_work);
130
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);
136 }
137 mutex_unlock(&priv->bl_tempo_lock);
138}
139
140/* turn the backlight on for a little while */
141void charlcd_poke(struct charlcd *lcd)
142{
143 struct charlcd_priv *priv = to_priv(lcd);
144
145 if (!lcd->ops->backlight)
146 return;
147
148 cancel_delayed_work_sync(&priv->bl_work);
149
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);
156}
157EXPORT_SYMBOL_GPL(charlcd_poke);
158
159static void charlcd_gotoxy(struct charlcd *lcd)
160{
161 struct charlcd_priv *priv = to_priv(lcd);
162
163 lcd->ops->write_cmd(lcd,
164 LCD_CMD_SET_DDRAM_ADDR | (priv->addr.y ? lcd->hwidth : 0) |
165 /*
166 * we force the cursor to stay at the end of the
167 * line if it wants to go farther
168 */
169 ((priv->addr.x < lcd->bwidth) ? priv->addr.x & (lcd->hwidth - 1)
170 : lcd->bwidth - 1));
171}
172
173static void charlcd_home(struct charlcd *lcd)
174{
175 struct charlcd_priv *priv = to_priv(lcd);
176
177 priv->addr.x = 0;
178 priv->addr.y = 0;
179 charlcd_gotoxy(lcd);
180}
181
182static void charlcd_print(struct charlcd *lcd, char c)
183{
184 struct charlcd_priv *priv = to_priv(lcd);
185
186 if (priv->addr.x < lcd->bwidth) {
187 if (lcd->char_conv)
188 c = lcd->char_conv[(unsigned char)c];
189 lcd->ops->write_data(lcd, c);
190 priv->addr.x++;
191 }
192 /* prevents the cursor from wrapping onto the next line */
193 if (priv->addr.x == lcd->bwidth)
194 charlcd_gotoxy(lcd);
195}
196
197static void charlcd_clear_fast(struct charlcd *lcd)
198{
199 int pos;
200
201 charlcd_home(lcd);
202
203 if (lcd->ops->clear_fast)
204 lcd->ops->clear_fast(lcd);
205 else
206 for (pos = 0; pos < lcd->height * lcd->hwidth; pos++)
207 lcd->ops->write_data(lcd, ' ');
208
209 charlcd_home(lcd);
210}
211
212/* clears the display and resets X/Y */
213static void charlcd_clear_display(struct charlcd *lcd)
214{
215 struct charlcd_priv *priv = to_priv(lcd);
216
217 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CLEAR);
218 priv->addr.x = 0;
219 priv->addr.y = 0;
220 /* we must wait a few milliseconds (15) */
221 long_sleep(15);
222}
223
224static int charlcd_init_display(struct charlcd *lcd)
225{
226 struct charlcd_priv *priv = to_priv(lcd);
227
228 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
229 LCD_FLAG_C | LCD_FLAG_B;
230
231 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
232
233 /* 8bits, 1 line, small fonts; let's do it 3 times */
234 lcd->ops->write_cmd(lcd, LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
235 long_sleep(10);
236 lcd->ops->write_cmd(lcd, LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
237 long_sleep(10);
238 lcd->ops->write_cmd(lcd, LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
239 long_sleep(10);
240
241 /* set font height and lines number */
242 lcd->ops->write_cmd(lcd,
243 LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS |
244 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
245 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
246 long_sleep(10);
247
248 /* display off, cursor off, blink off */
249 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CTRL);
250 long_sleep(10);
251
252 lcd->ops->write_cmd(lcd,
253 LCD_CMD_DISPLAY_CTRL | /* set display mode */
254 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
255 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
256 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
257
258 charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0);
259
260 long_sleep(10);
261
262 /* entry mode set : increment, cursor shifting */
263 lcd->ops->write_cmd(lcd, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
264
265 charlcd_clear_display(lcd);
266 return 0;
267}
268
269/*
270 * These are the file operation function for user access to /dev/lcd
271 * This function can also be called from inside the kernel, by
272 * setting file and ppos to NULL.
273 *
274 */
275
276static inline int handle_lcd_special_code(struct charlcd *lcd)
277{
278 struct charlcd_priv *priv = to_priv(lcd);
279
280 /* LCD special codes */
281
282 int processed = 0;
283
284 char *esc = priv->esc_seq.buf + 2;
285 int oldflags = priv->flags;
286
287 /* check for display mode flags */
288 switch (*esc) {
289 case 'D': /* Display ON */
290 priv->flags |= LCD_FLAG_D;
291 processed = 1;
292 break;
293 case 'd': /* Display OFF */
294 priv->flags &= ~LCD_FLAG_D;
295 processed = 1;
296 break;
297 case 'C': /* Cursor ON */
298 priv->flags |= LCD_FLAG_C;
299 processed = 1;
300 break;
301 case 'c': /* Cursor OFF */
302 priv->flags &= ~LCD_FLAG_C;
303 processed = 1;
304 break;
305 case 'B': /* Blink ON */
306 priv->flags |= LCD_FLAG_B;
307 processed = 1;
308 break;
309 case 'b': /* Blink OFF */
310 priv->flags &= ~LCD_FLAG_B;
311 processed = 1;
312 break;
313 case '+': /* Back light ON */
314 priv->flags |= LCD_FLAG_L;
315 processed = 1;
316 break;
317 case '-': /* Back light OFF */
318 priv->flags &= ~LCD_FLAG_L;
319 processed = 1;
320 break;
321 case '*': /* Flash back light */
322 charlcd_poke(lcd);
323 processed = 1;
324 break;
325 case 'f': /* Small Font */
326 priv->flags &= ~LCD_FLAG_F;
327 processed = 1;
328 break;
329 case 'F': /* Large Font */
330 priv->flags |= LCD_FLAG_F;
331 processed = 1;
332 break;
333 case 'n': /* One Line */
334 priv->flags &= ~LCD_FLAG_N;
335 processed = 1;
336 break;
337 case 'N': /* Two Lines */
338 priv->flags |= LCD_FLAG_N;
339 break;
340 case 'l': /* Shift Cursor Left */
341 if (priv->addr.x > 0) {
342 /* back one char if not at end of line */
343 if (priv->addr.x < lcd->bwidth)
344 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
345 priv->addr.x--;
346 }
347 processed = 1;
348 break;
349 case 'r': /* shift cursor right */
350 if (priv->addr.x < lcd->width) {
351 /* allow the cursor to pass the end of the line */
352 if (priv->addr.x < (lcd->bwidth - 1))
353 lcd->ops->write_cmd(lcd,
354 LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
355 priv->addr.x++;
356 }
357 processed = 1;
358 break;
359 case 'L': /* shift display left */
360 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
361 processed = 1;
362 break;
363 case 'R': /* shift display right */
364 lcd->ops->write_cmd(lcd,
365 LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
366 LCD_CMD_SHIFT_RIGHT);
367 processed = 1;
368 break;
369 case 'k': { /* kill end of line */
370 int x;
371
372 for (x = priv->addr.x; x < lcd->bwidth; x++)
373 lcd->ops->write_data(lcd, ' ');
374
375 /* restore cursor position */
376 charlcd_gotoxy(lcd);
377 processed = 1;
378 break;
379 }
380 case 'I': /* reinitialize display */
381 charlcd_init_display(lcd);
382 processed = 1;
383 break;
384 case 'G': {
385 /* Generator : LGcxxxxx...xx; must have <c> between '0'
386 * and '7', representing the numerical ASCII code of the
387 * redefined character, and <xx...xx> a sequence of 16
388 * hex digits representing 8 bytes for each character.
389 * Most LCDs will only use 5 lower bits of the 7 first
390 * bytes.
391 */
392
393 unsigned char cgbytes[8];
394 unsigned char cgaddr;
395 int cgoffset;
396 int shift;
397 char value;
398 int addr;
399
400 if (!strchr(esc, ';'))
401 break;
402
403 esc++;
404
405 cgaddr = *(esc++) - '0';
406 if (cgaddr > 7) {
407 processed = 1;
408 break;
409 }
410
411 cgoffset = 0;
412 shift = 0;
413 value = 0;
414 while (*esc && cgoffset < 8) {
415 shift ^= 4;
416 if (*esc >= '0' && *esc <= '9') {
417 value |= (*esc - '0') << shift;
418 } else if (*esc >= 'A' && *esc <= 'Z') {
419 value |= (*esc - 'A' + 10) << shift;
420 } else if (*esc >= 'a' && *esc <= 'z') {
421 value |= (*esc - 'a' + 10) << shift;
422 } else {
423 esc++;
424 continue;
425 }
426
427 if (shift == 0) {
428 cgbytes[cgoffset++] = value;
429 value = 0;
430 }
431
432 esc++;
433 }
434
435 lcd->ops->write_cmd(lcd, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
436 for (addr = 0; addr < cgoffset; addr++)
437 lcd->ops->write_data(lcd, cgbytes[addr]);
438
439 /* ensures that we stop writing to CGRAM */
440 charlcd_gotoxy(lcd);
441 processed = 1;
442 break;
443 }
444 case 'x': /* gotoxy : LxXXX[yYYY]; */
445 case 'y': /* gotoxy : LyYYY[xXXX]; */
446 if (!strchr(esc, ';'))
447 break;
448
449 while (*esc) {
450 if (*esc == 'x') {
451 esc++;
452 if (kstrtoul(esc, 10, &priv->addr.x) < 0)
453 break;
454 } else if (*esc == 'y') {
455 esc++;
456 if (kstrtoul(esc, 10, &priv->addr.y) < 0)
457 break;
458 } else {
459 break;
460 }
461 }
462
463 charlcd_gotoxy(lcd);
464 processed = 1;
465 break;
466 }
467
468 /* TODO: This indent party here got ugly, clean it! */
469 /* Check whether one flag was changed */
470 if (oldflags == priv->flags)
471 return processed;
472
473 /* check whether one of B,C,D flags were changed */
474 if ((oldflags ^ priv->flags) &
475 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
476 /* set display mode */
477 lcd->ops->write_cmd(lcd,
478 LCD_CMD_DISPLAY_CTRL |
479 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
480 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
481 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
482 /* check whether one of F,N flags was changed */
483 else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
484 lcd->ops->write_cmd(lcd,
485 LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS |
486 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
487 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
488 /* check whether L flag was changed */
489 else if ((oldflags ^ priv->flags) & LCD_FLAG_L)
490 charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
491
492 return processed;
493}
494
495static void charlcd_write_char(struct charlcd *lcd, char c)
496{
497 struct charlcd_priv *priv = to_priv(lcd);
498
499 /* first, we'll test if we're in escape mode */
500 if ((c != '\n') && priv->esc_seq.len >= 0) {
501 /* yes, let's add this char to the buffer */
502 priv->esc_seq.buf[priv->esc_seq.len++] = c;
503 priv->esc_seq.buf[priv->esc_seq.len] = 0;
504 } else {
505 /* aborts any previous escape sequence */
506 priv->esc_seq.len = -1;
507
508 switch (c) {
509 case LCD_ESCAPE_CHAR:
510 /* start of an escape sequence */
511 priv->esc_seq.len = 0;
512 priv->esc_seq.buf[priv->esc_seq.len] = 0;
513 break;
514 case '\b':
515 /* go back one char and clear it */
516 if (priv->addr.x > 0) {
517 /*
518 * check if we're not at the
519 * end of the line
520 */
521 if (priv->addr.x < lcd->bwidth)
522 /* back one char */
523 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
524 priv->addr.x--;
525 }
526 /* replace with a space */
527 lcd->ops->write_data(lcd, ' ');
528 /* back one char again */
529 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
530 break;
531 case '\014':
532 /* quickly clear the display */
533 charlcd_clear_fast(lcd);
534 break;
535 case '\n':
536 /*
537 * flush the remainder of the current line and
538 * go to the beginning of the next line
539 */
540 for (; priv->addr.x < lcd->bwidth; priv->addr.x++)
541 lcd->ops->write_data(lcd, ' ');
542 priv->addr.x = 0;
543 priv->addr.y = (priv->addr.y + 1) % lcd->height;
544 charlcd_gotoxy(lcd);
545 break;
546 case '\r':
547 /* go to the beginning of the same line */
548 priv->addr.x = 0;
549 charlcd_gotoxy(lcd);
550 break;
551 case '\t':
552 /* print a space instead of the tab */
553 charlcd_print(lcd, ' ');
554 break;
555 default:
556 /* simply print this char */
557 charlcd_print(lcd, c);
558 break;
559 }
560 }
561
562 /*
563 * now we'll see if we're in an escape mode and if the current
564 * escape sequence can be understood.
565 */
566 if (priv->esc_seq.len >= 2) {
567 int processed = 0;
568
569 if (!strcmp(priv->esc_seq.buf, "[2J")) {
570 /* clear the display */
571 charlcd_clear_fast(lcd);
572 processed = 1;
573 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
574 /* cursor to home */
575 charlcd_home(lcd);
576 processed = 1;
577 }
578 /* codes starting with ^[[L */
579 else if ((priv->esc_seq.len >= 3) &&
580 (priv->esc_seq.buf[0] == '[') &&
581 (priv->esc_seq.buf[1] == 'L')) {
582 processed = handle_lcd_special_code(lcd);
583 }
584
585 /* LCD special escape codes */
586 /*
587 * flush the escape sequence if it's been processed
588 * or if it is getting too long.
589 */
590 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
591 priv->esc_seq.len = -1;
592 } /* escape codes */
593}
594
595static struct charlcd *the_charlcd;
596
597static ssize_t charlcd_write(struct file *file, const char __user *buf,
598 size_t count, loff_t *ppos)
599{
600 const char __user *tmp = buf;
601 char c;
602
603 for (; count-- > 0; (*ppos)++, tmp++) {
604 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
605 /*
606 * let's be a little nice with other processes
607 * that need some CPU
608 */
609 schedule();
610
611 if (get_user(c, tmp))
612 return -EFAULT;
613
614 charlcd_write_char(the_charlcd, c);
615 }
616
617 return tmp - buf;
618}
619
620static int charlcd_open(struct inode *inode, struct file *file)
621{
622 struct charlcd_priv *priv = to_priv(the_charlcd);
623
624 if (!atomic_dec_and_test(&charlcd_available))
625 return -EBUSY; /* open only once at a time */
626
627 if (file->f_mode & FMODE_READ) /* device is write-only */
628 return -EPERM;
629
630 if (priv->must_clear) {
631 charlcd_clear_display(&priv->lcd);
632 priv->must_clear = false;
633 }
634 return nonseekable_open(inode, file);
635}
636
637static int charlcd_release(struct inode *inode, struct file *file)
638{
639 atomic_inc(&charlcd_available);
640 return 0;
641}
642
643static const struct file_operations charlcd_fops = {
644 .write = charlcd_write,
645 .open = charlcd_open,
646 .release = charlcd_release,
647 .llseek = no_llseek,
648};
649
650static struct miscdevice charlcd_dev = {
651 .minor = LCD_MINOR,
652 .name = "lcd",
653 .fops = &charlcd_fops,
654};
655
656static void charlcd_puts(struct charlcd *lcd, const char *s)
657{
658 const char *tmp = s;
659 int count = strlen(s);
660
661 for (; count-- > 0; tmp++) {
662 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
663 /*
664 * let's be a little nice with other processes
665 * that need some CPU
666 */
667 schedule();
668
669 charlcd_write_char(lcd, *tmp);
670 }
671}
672
673/* initialize the LCD driver */
674static int charlcd_init(struct charlcd *lcd)
675{
676 struct charlcd_priv *priv = to_priv(lcd);
677 int ret;
678
679 if (lcd->ops->backlight) {
680 mutex_init(&priv->bl_tempo_lock);
681 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
682 }
683
684 /*
685 * before this line, we must NOT send anything to the display.
686 * Since charlcd_init_display() needs to write data, we have to
687 * enable mark the LCD initialized just before.
688 */
689 ret = charlcd_init_display(lcd);
690 if (ret)
691 return ret;
692
693 /* display a short message */
694#ifdef CONFIG_PANEL_CHANGE_MESSAGE
695#ifdef CONFIG_PANEL_BOOT_MESSAGE
696 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
697#endif
698#else
699 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\n");
700#endif
701 /* clear the display on the next device opening */
702 priv->must_clear = true;
703 charlcd_home(lcd);
704 return 0;
705}
706
707struct charlcd *charlcd_alloc(unsigned int drvdata_size)
708{
709 struct charlcd_priv *priv;
710 struct charlcd *lcd;
711
712 priv = kzalloc(sizeof(*priv) + drvdata_size, GFP_KERNEL);
713 if (!priv)
714 return NULL;
715
716 priv->esc_seq.len = -1;
717
718 lcd = &priv->lcd;
719 lcd->bwidth = DEFAULT_LCD_BWIDTH;
720 lcd->hwidth = DEFAULT_LCD_HWIDTH;
721 lcd->drvdata = priv->drvdata;
722
723 return lcd;
724}
725EXPORT_SYMBOL_GPL(charlcd_alloc);
726
727static int panel_notify_sys(struct notifier_block *this, unsigned long code,
728 void *unused)
729{
730 struct charlcd *lcd = the_charlcd;
731
732 switch (code) {
733 case SYS_DOWN:
734 charlcd_puts(lcd,
735 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
736 break;
737 case SYS_HALT:
738 charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
739 break;
740 case SYS_POWER_OFF:
741 charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
742 break;
743 default:
744 break;
745 }
746 return NOTIFY_DONE;
747}
748
749static struct notifier_block panel_notifier = {
750 panel_notify_sys,
751 NULL,
752 0
753};
754
755int charlcd_register(struct charlcd *lcd)
756{
757 int ret;
758
759 ret = charlcd_init(lcd);
760 if (ret)
761 return ret;
762
763 ret = misc_register(&charlcd_dev);
764 if (ret)
765 return ret;
766
767 the_charlcd = lcd;
768 register_reboot_notifier(&panel_notifier);
769 return 0;
770}
771EXPORT_SYMBOL_GPL(charlcd_register);
772
773int charlcd_unregister(struct charlcd *lcd)
774{
775 struct charlcd_priv *priv = to_priv(lcd);
776
777 unregister_reboot_notifier(&panel_notifier);
778 charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
779 misc_deregister(&charlcd_dev);
780 the_charlcd = NULL;
781 if (lcd->ops->backlight) {
782 cancel_delayed_work_sync(&priv->bl_work);
783 priv->lcd.ops->backlight(&priv->lcd, 0);
784 }
785
786 return 0;
787}
788EXPORT_SYMBOL_GPL(charlcd_unregister);
789
790MODULE_LICENSE("GPL");