blob: ee39d1a85bf79465c497a71d8ad7ae4a39ac2b6c [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);
Geert Uytterhoeven1d3b2af2017-03-10 15:15:19 +0100162 unsigned int addr;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100163
Geert Uytterhoeven1d3b2af2017-03-10 15:15:19 +0100164 /*
165 * we force the cursor to stay at the end of the
166 * line if it wants to go farther
167 */
168 addr = priv->addr.x < lcd->bwidth ? priv->addr.x & (lcd->hwidth - 1)
169 : lcd->bwidth - 1;
170 if (priv->addr.y & 1)
171 addr += lcd->hwidth;
172 if (priv->addr.y & 2)
173 addr += lcd->bwidth;
174 lcd->ops->write_cmd(lcd, LCD_CMD_SET_DDRAM_ADDR | addr);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100175}
176
177static void charlcd_home(struct charlcd *lcd)
178{
179 struct charlcd_priv *priv = to_priv(lcd);
180
181 priv->addr.x = 0;
182 priv->addr.y = 0;
183 charlcd_gotoxy(lcd);
184}
185
186static void charlcd_print(struct charlcd *lcd, char c)
187{
188 struct charlcd_priv *priv = to_priv(lcd);
189
190 if (priv->addr.x < lcd->bwidth) {
191 if (lcd->char_conv)
192 c = lcd->char_conv[(unsigned char)c];
193 lcd->ops->write_data(lcd, c);
194 priv->addr.x++;
Sean Young54bc9372018-01-15 09:43:48 +0000195
196 /* prevents the cursor from wrapping onto the next line */
197 if (priv->addr.x == lcd->bwidth)
198 charlcd_gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100199 }
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100200}
201
202static void charlcd_clear_fast(struct charlcd *lcd)
203{
204 int pos;
205
206 charlcd_home(lcd);
207
208 if (lcd->ops->clear_fast)
209 lcd->ops->clear_fast(lcd);
210 else
Geert Uytterhoeven1d3b2af2017-03-10 15:15:19 +0100211 for (pos = 0; pos < min(2, lcd->height) * lcd->hwidth; pos++)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100212 lcd->ops->write_data(lcd, ' ');
213
214 charlcd_home(lcd);
215}
216
217/* clears the display and resets X/Y */
218static void charlcd_clear_display(struct charlcd *lcd)
219{
220 struct charlcd_priv *priv = to_priv(lcd);
221
222 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CLEAR);
223 priv->addr.x = 0;
224 priv->addr.y = 0;
225 /* we must wait a few milliseconds (15) */
226 long_sleep(15);
227}
228
229static int charlcd_init_display(struct charlcd *lcd)
230{
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100231 void (*write_cmd_raw)(struct charlcd *lcd, int cmd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100232 struct charlcd_priv *priv = to_priv(lcd);
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100233 u8 init;
234
235 if (lcd->ifwidth != 4 && lcd->ifwidth != 8)
236 return -EINVAL;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100237
238 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
239 LCD_FLAG_C | LCD_FLAG_B;
240
241 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
242
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100243 /*
244 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
245 * the LCD is in 8-bit mode afterwards
246 */
247 init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
248 if (lcd->ifwidth == 4) {
249 init >>= 4;
250 write_cmd_raw = lcd->ops->write_cmd_raw4;
251 } else {
252 write_cmd_raw = lcd->ops->write_cmd;
253 }
254 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100255 long_sleep(10);
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100256 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100257 long_sleep(10);
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100258 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100259 long_sleep(10);
260
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100261 if (lcd->ifwidth == 4) {
262 /* Switch to 4-bit mode, 1 line, small fonts */
263 lcd->ops->write_cmd_raw4(lcd, LCD_CMD_FUNCTION_SET >> 4);
264 long_sleep(10);
265 }
266
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100267 /* set font height and lines number */
268 lcd->ops->write_cmd(lcd,
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100269 LCD_CMD_FUNCTION_SET |
270 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100271 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
272 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
273 long_sleep(10);
274
275 /* display off, cursor off, blink off */
276 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CTRL);
277 long_sleep(10);
278
279 lcd->ops->write_cmd(lcd,
280 LCD_CMD_DISPLAY_CTRL | /* set display mode */
281 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
282 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
283 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
284
285 charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0);
286
287 long_sleep(10);
288
289 /* entry mode set : increment, cursor shifting */
290 lcd->ops->write_cmd(lcd, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
291
292 charlcd_clear_display(lcd);
293 return 0;
294}
295
296/*
297 * These are the file operation function for user access to /dev/lcd
298 * This function can also be called from inside the kernel, by
299 * setting file and ppos to NULL.
300 *
301 */
302
303static inline int handle_lcd_special_code(struct charlcd *lcd)
304{
305 struct charlcd_priv *priv = to_priv(lcd);
306
307 /* LCD special codes */
308
309 int processed = 0;
310
311 char *esc = priv->esc_seq.buf + 2;
312 int oldflags = priv->flags;
313
314 /* check for display mode flags */
315 switch (*esc) {
316 case 'D': /* Display ON */
317 priv->flags |= LCD_FLAG_D;
318 processed = 1;
319 break;
320 case 'd': /* Display OFF */
321 priv->flags &= ~LCD_FLAG_D;
322 processed = 1;
323 break;
324 case 'C': /* Cursor ON */
325 priv->flags |= LCD_FLAG_C;
326 processed = 1;
327 break;
328 case 'c': /* Cursor OFF */
329 priv->flags &= ~LCD_FLAG_C;
330 processed = 1;
331 break;
332 case 'B': /* Blink ON */
333 priv->flags |= LCD_FLAG_B;
334 processed = 1;
335 break;
336 case 'b': /* Blink OFF */
337 priv->flags &= ~LCD_FLAG_B;
338 processed = 1;
339 break;
340 case '+': /* Back light ON */
341 priv->flags |= LCD_FLAG_L;
342 processed = 1;
343 break;
344 case '-': /* Back light OFF */
345 priv->flags &= ~LCD_FLAG_L;
346 processed = 1;
347 break;
348 case '*': /* Flash back light */
349 charlcd_poke(lcd);
350 processed = 1;
351 break;
352 case 'f': /* Small Font */
353 priv->flags &= ~LCD_FLAG_F;
354 processed = 1;
355 break;
356 case 'F': /* Large Font */
357 priv->flags |= LCD_FLAG_F;
358 processed = 1;
359 break;
360 case 'n': /* One Line */
361 priv->flags &= ~LCD_FLAG_N;
362 processed = 1;
363 break;
364 case 'N': /* Two Lines */
365 priv->flags |= LCD_FLAG_N;
366 break;
367 case 'l': /* Shift Cursor Left */
368 if (priv->addr.x > 0) {
369 /* back one char if not at end of line */
370 if (priv->addr.x < lcd->bwidth)
371 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
372 priv->addr.x--;
373 }
374 processed = 1;
375 break;
376 case 'r': /* shift cursor right */
377 if (priv->addr.x < lcd->width) {
378 /* allow the cursor to pass the end of the line */
379 if (priv->addr.x < (lcd->bwidth - 1))
380 lcd->ops->write_cmd(lcd,
381 LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
382 priv->addr.x++;
383 }
384 processed = 1;
385 break;
386 case 'L': /* shift display left */
387 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
388 processed = 1;
389 break;
390 case 'R': /* shift display right */
391 lcd->ops->write_cmd(lcd,
392 LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
393 LCD_CMD_SHIFT_RIGHT);
394 processed = 1;
395 break;
396 case 'k': { /* kill end of line */
397 int x;
398
399 for (x = priv->addr.x; x < lcd->bwidth; x++)
400 lcd->ops->write_data(lcd, ' ');
401
402 /* restore cursor position */
403 charlcd_gotoxy(lcd);
404 processed = 1;
405 break;
406 }
407 case 'I': /* reinitialize display */
408 charlcd_init_display(lcd);
409 processed = 1;
410 break;
411 case 'G': {
412 /* Generator : LGcxxxxx...xx; must have <c> between '0'
413 * and '7', representing the numerical ASCII code of the
414 * redefined character, and <xx...xx> a sequence of 16
415 * hex digits representing 8 bytes for each character.
416 * Most LCDs will only use 5 lower bits of the 7 first
417 * bytes.
418 */
419
420 unsigned char cgbytes[8];
421 unsigned char cgaddr;
422 int cgoffset;
423 int shift;
424 char value;
425 int addr;
426
427 if (!strchr(esc, ';'))
428 break;
429
430 esc++;
431
432 cgaddr = *(esc++) - '0';
433 if (cgaddr > 7) {
434 processed = 1;
435 break;
436 }
437
438 cgoffset = 0;
439 shift = 0;
440 value = 0;
441 while (*esc && cgoffset < 8) {
442 shift ^= 4;
443 if (*esc >= '0' && *esc <= '9') {
444 value |= (*esc - '0') << shift;
445 } else if (*esc >= 'A' && *esc <= 'Z') {
446 value |= (*esc - 'A' + 10) << shift;
447 } else if (*esc >= 'a' && *esc <= 'z') {
448 value |= (*esc - 'a' + 10) << shift;
449 } else {
450 esc++;
451 continue;
452 }
453
454 if (shift == 0) {
455 cgbytes[cgoffset++] = value;
456 value = 0;
457 }
458
459 esc++;
460 }
461
462 lcd->ops->write_cmd(lcd, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
463 for (addr = 0; addr < cgoffset; addr++)
464 lcd->ops->write_data(lcd, cgbytes[addr]);
465
466 /* ensures that we stop writing to CGRAM */
467 charlcd_gotoxy(lcd);
468 processed = 1;
469 break;
470 }
471 case 'x': /* gotoxy : LxXXX[yYYY]; */
472 case 'y': /* gotoxy : LyYYY[xXXX]; */
473 if (!strchr(esc, ';'))
474 break;
475
476 while (*esc) {
477 if (*esc == 'x') {
478 esc++;
479 if (kstrtoul(esc, 10, &priv->addr.x) < 0)
480 break;
481 } else if (*esc == 'y') {
482 esc++;
483 if (kstrtoul(esc, 10, &priv->addr.y) < 0)
484 break;
485 } else {
486 break;
487 }
488 }
489
490 charlcd_gotoxy(lcd);
491 processed = 1;
492 break;
493 }
494
495 /* TODO: This indent party here got ugly, clean it! */
496 /* Check whether one flag was changed */
497 if (oldflags == priv->flags)
498 return processed;
499
500 /* check whether one of B,C,D flags were changed */
501 if ((oldflags ^ priv->flags) &
502 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
503 /* set display mode */
504 lcd->ops->write_cmd(lcd,
505 LCD_CMD_DISPLAY_CTRL |
506 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
507 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
508 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
509 /* check whether one of F,N flags was changed */
510 else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
511 lcd->ops->write_cmd(lcd,
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100512 LCD_CMD_FUNCTION_SET |
513 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100514 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
515 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
516 /* check whether L flag was changed */
517 else if ((oldflags ^ priv->flags) & LCD_FLAG_L)
518 charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
519
520 return processed;
521}
522
523static void charlcd_write_char(struct charlcd *lcd, char c)
524{
525 struct charlcd_priv *priv = to_priv(lcd);
526
527 /* first, we'll test if we're in escape mode */
528 if ((c != '\n') && priv->esc_seq.len >= 0) {
529 /* yes, let's add this char to the buffer */
530 priv->esc_seq.buf[priv->esc_seq.len++] = c;
Robert Abel8c483752018-02-10 00:50:11 +0100531 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100532 } else {
533 /* aborts any previous escape sequence */
534 priv->esc_seq.len = -1;
535
536 switch (c) {
537 case LCD_ESCAPE_CHAR:
538 /* start of an escape sequence */
539 priv->esc_seq.len = 0;
Robert Abel8c483752018-02-10 00:50:11 +0100540 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100541 break;
542 case '\b':
543 /* go back one char and clear it */
544 if (priv->addr.x > 0) {
545 /*
546 * check if we're not at the
547 * end of the line
548 */
549 if (priv->addr.x < lcd->bwidth)
550 /* back one char */
551 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
552 priv->addr.x--;
553 }
554 /* replace with a space */
555 lcd->ops->write_data(lcd, ' ');
556 /* back one char again */
557 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
558 break;
Robert Abel9629ccc2018-02-10 00:50:12 +0100559 case '\f':
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100560 /* quickly clear the display */
561 charlcd_clear_fast(lcd);
562 break;
563 case '\n':
564 /*
565 * flush the remainder of the current line and
566 * go to the beginning of the next line
567 */
568 for (; priv->addr.x < lcd->bwidth; priv->addr.x++)
569 lcd->ops->write_data(lcd, ' ');
570 priv->addr.x = 0;
571 priv->addr.y = (priv->addr.y + 1) % lcd->height;
572 charlcd_gotoxy(lcd);
573 break;
574 case '\r':
575 /* go to the beginning of the same line */
576 priv->addr.x = 0;
577 charlcd_gotoxy(lcd);
578 break;
579 case '\t':
580 /* print a space instead of the tab */
581 charlcd_print(lcd, ' ');
582 break;
583 default:
584 /* simply print this char */
585 charlcd_print(lcd, c);
586 break;
587 }
588 }
589
590 /*
591 * now we'll see if we're in an escape mode and if the current
592 * escape sequence can be understood.
593 */
594 if (priv->esc_seq.len >= 2) {
595 int processed = 0;
596
597 if (!strcmp(priv->esc_seq.buf, "[2J")) {
598 /* clear the display */
599 charlcd_clear_fast(lcd);
600 processed = 1;
601 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
602 /* cursor to home */
603 charlcd_home(lcd);
604 processed = 1;
605 }
606 /* codes starting with ^[[L */
607 else if ((priv->esc_seq.len >= 3) &&
608 (priv->esc_seq.buf[0] == '[') &&
609 (priv->esc_seq.buf[1] == 'L')) {
610 processed = handle_lcd_special_code(lcd);
611 }
612
613 /* LCD special escape codes */
614 /*
615 * flush the escape sequence if it's been processed
616 * or if it is getting too long.
617 */
618 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
619 priv->esc_seq.len = -1;
620 } /* escape codes */
621}
622
623static struct charlcd *the_charlcd;
624
625static ssize_t charlcd_write(struct file *file, const char __user *buf,
626 size_t count, loff_t *ppos)
627{
628 const char __user *tmp = buf;
629 char c;
630
631 for (; count-- > 0; (*ppos)++, tmp++) {
632 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
633 /*
634 * let's be a little nice with other processes
635 * that need some CPU
636 */
637 schedule();
638
639 if (get_user(c, tmp))
640 return -EFAULT;
641
642 charlcd_write_char(the_charlcd, c);
643 }
644
645 return tmp - buf;
646}
647
648static int charlcd_open(struct inode *inode, struct file *file)
649{
650 struct charlcd_priv *priv = to_priv(the_charlcd);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200651 int ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100652
Willy Tarreau93dc1772017-09-07 15:37:30 +0200653 ret = -EBUSY;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100654 if (!atomic_dec_and_test(&charlcd_available))
Willy Tarreau93dc1772017-09-07 15:37:30 +0200655 goto fail; /* open only once at a time */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100656
Willy Tarreau93dc1772017-09-07 15:37:30 +0200657 ret = -EPERM;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100658 if (file->f_mode & FMODE_READ) /* device is write-only */
Willy Tarreau93dc1772017-09-07 15:37:30 +0200659 goto fail;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100660
661 if (priv->must_clear) {
662 charlcd_clear_display(&priv->lcd);
663 priv->must_clear = false;
664 }
665 return nonseekable_open(inode, file);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200666
667 fail:
668 atomic_inc(&charlcd_available);
669 return ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100670}
671
672static int charlcd_release(struct inode *inode, struct file *file)
673{
674 atomic_inc(&charlcd_available);
675 return 0;
676}
677
678static const struct file_operations charlcd_fops = {
679 .write = charlcd_write,
680 .open = charlcd_open,
681 .release = charlcd_release,
682 .llseek = no_llseek,
683};
684
685static struct miscdevice charlcd_dev = {
686 .minor = LCD_MINOR,
687 .name = "lcd",
688 .fops = &charlcd_fops,
689};
690
691static void charlcd_puts(struct charlcd *lcd, const char *s)
692{
693 const char *tmp = s;
694 int count = strlen(s);
695
696 for (; count-- > 0; tmp++) {
697 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
698 /*
699 * let's be a little nice with other processes
700 * that need some CPU
701 */
702 schedule();
703
704 charlcd_write_char(lcd, *tmp);
705 }
706}
707
708/* initialize the LCD driver */
709static int charlcd_init(struct charlcd *lcd)
710{
711 struct charlcd_priv *priv = to_priv(lcd);
712 int ret;
713
714 if (lcd->ops->backlight) {
715 mutex_init(&priv->bl_tempo_lock);
716 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
717 }
718
719 /*
720 * before this line, we must NOT send anything to the display.
721 * Since charlcd_init_display() needs to write data, we have to
722 * enable mark the LCD initialized just before.
723 */
724 ret = charlcd_init_display(lcd);
725 if (ret)
726 return ret;
727
728 /* display a short message */
729#ifdef CONFIG_PANEL_CHANGE_MESSAGE
730#ifdef CONFIG_PANEL_BOOT_MESSAGE
731 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
732#endif
733#else
734 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\n");
735#endif
736 /* clear the display on the next device opening */
737 priv->must_clear = true;
738 charlcd_home(lcd);
739 return 0;
740}
741
742struct charlcd *charlcd_alloc(unsigned int drvdata_size)
743{
744 struct charlcd_priv *priv;
745 struct charlcd *lcd;
746
747 priv = kzalloc(sizeof(*priv) + drvdata_size, GFP_KERNEL);
748 if (!priv)
749 return NULL;
750
751 priv->esc_seq.len = -1;
752
753 lcd = &priv->lcd;
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100754 lcd->ifwidth = 8;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100755 lcd->bwidth = DEFAULT_LCD_BWIDTH;
756 lcd->hwidth = DEFAULT_LCD_HWIDTH;
757 lcd->drvdata = priv->drvdata;
758
759 return lcd;
760}
761EXPORT_SYMBOL_GPL(charlcd_alloc);
762
763static int panel_notify_sys(struct notifier_block *this, unsigned long code,
764 void *unused)
765{
766 struct charlcd *lcd = the_charlcd;
767
768 switch (code) {
769 case SYS_DOWN:
770 charlcd_puts(lcd,
771 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
772 break;
773 case SYS_HALT:
774 charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
775 break;
776 case SYS_POWER_OFF:
777 charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
778 break;
779 default:
780 break;
781 }
782 return NOTIFY_DONE;
783}
784
785static struct notifier_block panel_notifier = {
786 panel_notify_sys,
787 NULL,
788 0
789};
790
791int charlcd_register(struct charlcd *lcd)
792{
793 int ret;
794
795 ret = charlcd_init(lcd);
796 if (ret)
797 return ret;
798
799 ret = misc_register(&charlcd_dev);
800 if (ret)
801 return ret;
802
803 the_charlcd = lcd;
804 register_reboot_notifier(&panel_notifier);
805 return 0;
806}
807EXPORT_SYMBOL_GPL(charlcd_register);
808
809int charlcd_unregister(struct charlcd *lcd)
810{
811 struct charlcd_priv *priv = to_priv(lcd);
812
813 unregister_reboot_notifier(&panel_notifier);
814 charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
815 misc_deregister(&charlcd_dev);
816 the_charlcd = NULL;
817 if (lcd->ops->backlight) {
818 cancel_delayed_work_sync(&priv->bl_work);
819 priv->lcd.ops->backlight(&priv->lcd, 0);
820 }
821
822 return 0;
823}
824EXPORT_SYMBOL_GPL(charlcd_unregister);
825
826MODULE_LICENSE("GPL");