blob: 02392336d7d3ad230b55b38af13184364d3d7bdb [file] [log] [blame]
Miguel Ojeda351f683b2018-02-17 20:33:13 +01001// SPDX-License-Identifier: GPL-2.0+
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01002/*
3 * Character LCD driver for Linux
4 *
5 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
6 * Copyright (C) 2016-2017 Glider bvba
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01007 */
8
9#include <linux/atomic.h>
Miguel Ojedab34050f2018-02-27 23:09:52 +010010#include <linux/ctype.h>
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010011#include <linux/delay.h>
12#include <linux/fs.h>
13#include <linux/miscdevice.h>
14#include <linux/module.h>
15#include <linux/notifier.h>
16#include <linux/reboot.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19#include <linux/workqueue.h>
20
21#include <generated/utsrelease.h>
22
Masahiro Yamada75354282019-08-06 16:14:44 +090023#include "charlcd.h"
Lars Poeschel2545c1c2020-11-03 10:58:06 +010024#include "hd44780_common.h"
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010025
26/* Keep the backlight on this many seconds for each flash */
27#define LCD_BL_TEMPO_PERIOD 4
28
29#define LCD_FLAG_B 0x0004 /* Blink on */
30#define LCD_FLAG_C 0x0008 /* Cursor on */
31#define LCD_FLAG_D 0x0010 /* Display on */
32#define LCD_FLAG_F 0x0020 /* Large font mode */
33#define LCD_FLAG_N 0x0040 /* 2-rows mode */
34#define LCD_FLAG_L 0x0080 /* Backlight enabled */
35
36/* LCD commands */
37#define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
38
39#define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
40#define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
41
42#define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
43#define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
44#define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
45#define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
46
47#define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
48#define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
49#define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
50
51#define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
52#define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
53#define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
54#define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
55
56#define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
57
58#define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
59
60#define LCD_ESCAPE_LEN 24 /* Max chars for LCD escape command */
61#define LCD_ESCAPE_CHAR 27 /* Use char 27 for escape command */
62
63struct charlcd_priv {
64 struct charlcd lcd;
65
66 struct delayed_work bl_work;
67 struct mutex bl_tempo_lock; /* Protects access to bl_tempo */
68 bool bl_tempo;
69
70 bool must_clear;
71
72 /* contains the LCD config state */
73 unsigned long int flags;
74
75 /* Contains the LCD X and Y offset */
76 struct {
77 unsigned long int x;
78 unsigned long int y;
79 } addr;
80
81 /* Current escape sequence and it's length or -1 if outside */
82 struct {
83 char buf[LCD_ESCAPE_LEN + 1];
84 int len;
85 } esc_seq;
86
Gustavo A. R. Silva2f920c02020-02-12 13:52:31 -060087 unsigned long long drvdata[];
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010088};
89
Andy Shevchenkob658a212019-03-12 16:44:29 +020090#define charlcd_to_priv(p) container_of(p, struct charlcd_priv, lcd)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010091
92/* Device single-open policy control */
93static atomic_t charlcd_available = ATOMIC_INIT(1);
94
95/* sleeps that many milliseconds with a reschedule */
96static void long_sleep(int ms)
97{
Jia-Ju Bai17161392018-01-26 23:19:15 +080098 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010099}
100
101/* turn the backlight on or off */
Lars Poeschel66ce7d52020-11-03 10:58:04 +0100102static void charlcd_backlight(struct charlcd *lcd, enum charlcd_onoff on)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100103{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200104 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100105
106 if (!lcd->ops->backlight)
107 return;
108
109 mutex_lock(&priv->bl_tempo_lock);
110 if (!priv->bl_tempo)
111 lcd->ops->backlight(lcd, on);
112 mutex_unlock(&priv->bl_tempo_lock);
113}
114
115static void charlcd_bl_off(struct work_struct *work)
116{
117 struct delayed_work *dwork = to_delayed_work(work);
118 struct charlcd_priv *priv =
119 container_of(dwork, struct charlcd_priv, bl_work);
120
121 mutex_lock(&priv->bl_tempo_lock);
122 if (priv->bl_tempo) {
123 priv->bl_tempo = false;
124 if (!(priv->flags & LCD_FLAG_L))
125 priv->lcd.ops->backlight(&priv->lcd, 0);
126 }
127 mutex_unlock(&priv->bl_tempo_lock);
128}
129
130/* turn the backlight on for a little while */
131void charlcd_poke(struct charlcd *lcd)
132{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200133 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100134
135 if (!lcd->ops->backlight)
136 return;
137
138 cancel_delayed_work_sync(&priv->bl_work);
139
140 mutex_lock(&priv->bl_tempo_lock);
141 if (!priv->bl_tempo && !(priv->flags & LCD_FLAG_L))
142 lcd->ops->backlight(lcd, 1);
143 priv->bl_tempo = true;
144 schedule_delayed_work(&priv->bl_work, LCD_BL_TEMPO_PERIOD * HZ);
145 mutex_unlock(&priv->bl_tempo_lock);
146}
147EXPORT_SYMBOL_GPL(charlcd_poke);
148
149static void charlcd_gotoxy(struct charlcd *lcd)
150{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200151 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100152 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven1d3b2af2017-03-10 15:15:19 +0100153 unsigned int addr;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100154
Geert Uytterhoeven1d3b2af2017-03-10 15:15:19 +0100155 /*
156 * we force the cursor to stay at the end of the
157 * line if it wants to go farther
158 */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100159 addr = priv->addr.x < hdc->bwidth ? priv->addr.x & (hdc->hwidth - 1)
160 : hdc->bwidth - 1;
Geert Uytterhoeven1d3b2af2017-03-10 15:15:19 +0100161 if (priv->addr.y & 1)
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100162 addr += hdc->hwidth;
Geert Uytterhoeven1d3b2af2017-03-10 15:15:19 +0100163 if (priv->addr.y & 2)
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100164 addr += hdc->bwidth;
Geert Uytterhoeven1d3b2af2017-03-10 15:15:19 +0100165 lcd->ops->write_cmd(lcd, LCD_CMD_SET_DDRAM_ADDR | addr);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100166}
167
168static void charlcd_home(struct charlcd *lcd)
169{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200170 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100171
172 priv->addr.x = 0;
173 priv->addr.y = 0;
174 charlcd_gotoxy(lcd);
175}
176
177static void charlcd_print(struct charlcd *lcd, char c)
178{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200179 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100180 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100181
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100182 if (priv->addr.x < hdc->bwidth) {
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100183 if (lcd->char_conv)
184 c = lcd->char_conv[(unsigned char)c];
185 lcd->ops->write_data(lcd, c);
186 priv->addr.x++;
Sean Young54bc937f2018-01-15 09:43:48 +0000187
188 /* prevents the cursor from wrapping onto the next line */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100189 if (priv->addr.x == hdc->bwidth)
Sean Young54bc937f2018-01-15 09:43:48 +0000190 charlcd_gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100191 }
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100192}
193
194static void charlcd_clear_fast(struct charlcd *lcd)
195{
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100196 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100197 int pos;
198
199 charlcd_home(lcd);
200
201 if (lcd->ops->clear_fast)
202 lcd->ops->clear_fast(lcd);
203 else
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100204 for (pos = 0; pos < min(2, lcd->height) * hdc->hwidth; pos++)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100205 lcd->ops->write_data(lcd, ' ');
206
207 charlcd_home(lcd);
208}
209
210/* clears the display and resets X/Y */
211static void charlcd_clear_display(struct charlcd *lcd)
212{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200213 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100214
215 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CLEAR);
216 priv->addr.x = 0;
217 priv->addr.y = 0;
218 /* we must wait a few milliseconds (15) */
219 long_sleep(15);
220}
221
222static int charlcd_init_display(struct charlcd *lcd)
223{
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100224 void (*write_cmd_raw)(struct charlcd *lcd, int cmd);
Andy Shevchenkob658a212019-03-12 16:44:29 +0200225 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100226 u8 init;
227
228 if (lcd->ifwidth != 4 && lcd->ifwidth != 8)
229 return -EINVAL;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100230
231 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
232 LCD_FLAG_C | LCD_FLAG_B;
233
234 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
235
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100236 /*
237 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
238 * the LCD is in 8-bit mode afterwards
239 */
240 init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
241 if (lcd->ifwidth == 4) {
242 init >>= 4;
243 write_cmd_raw = lcd->ops->write_cmd_raw4;
244 } else {
245 write_cmd_raw = lcd->ops->write_cmd;
246 }
247 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100248 long_sleep(10);
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100249 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100250 long_sleep(10);
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100251 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100252 long_sleep(10);
253
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100254 if (lcd->ifwidth == 4) {
255 /* Switch to 4-bit mode, 1 line, small fonts */
256 lcd->ops->write_cmd_raw4(lcd, LCD_CMD_FUNCTION_SET >> 4);
257 long_sleep(10);
258 }
259
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100260 /* set font height and lines number */
261 lcd->ops->write_cmd(lcd,
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100262 LCD_CMD_FUNCTION_SET |
263 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100264 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
265 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
266 long_sleep(10);
267
268 /* display off, cursor off, blink off */
269 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CTRL);
270 long_sleep(10);
271
272 lcd->ops->write_cmd(lcd,
273 LCD_CMD_DISPLAY_CTRL | /* set display mode */
274 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
275 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
276 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
277
278 charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0);
279
280 long_sleep(10);
281
282 /* entry mode set : increment, cursor shifting */
283 lcd->ops->write_cmd(lcd, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
284
285 charlcd_clear_display(lcd);
286 return 0;
287}
288
289/*
Miguel Ojedab34050f2018-02-27 23:09:52 +0100290 * Parses a movement command of the form "(.*);", where the group can be
291 * any number of subcommands of the form "(x|y)[0-9]+".
292 *
293 * Returns whether the command is valid. The position arguments are
294 * only written if the parsing was successful.
295 *
296 * For instance:
297 * - ";" returns (<original x>, <original y>).
298 * - "x1;" returns (1, <original y>).
299 * - "y2x1;" returns (1, 2).
300 * - "x12y34x56;" returns (56, 34).
301 * - "" fails.
302 * - "x" fails.
303 * - "x;" fails.
304 * - "x1" fails.
305 * - "xy12;" fails.
306 * - "x12yy12;" fails.
307 * - "xx" fails.
308 */
309static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
310{
311 unsigned long new_x = *x;
312 unsigned long new_y = *y;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800313 char *p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100314
315 for (;;) {
316 if (!*s)
317 return false;
318
319 if (*s == ';')
320 break;
321
322 if (*s == 'x') {
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800323 new_x = simple_strtoul(s + 1, &p, 10);
324 if (p == s + 1)
Miguel Ojedab34050f2018-02-27 23:09:52 +0100325 return false;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800326 s = p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100327 } else if (*s == 'y') {
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800328 new_y = simple_strtoul(s + 1, &p, 10);
329 if (p == s + 1)
Miguel Ojedab34050f2018-02-27 23:09:52 +0100330 return false;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800331 s = p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100332 } else {
333 return false;
334 }
335 }
336
337 *x = new_x;
338 *y = new_y;
339 return true;
340}
341
342/*
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100343 * These are the file operation function for user access to /dev/lcd
344 * This function can also be called from inside the kernel, by
345 * setting file and ppos to NULL.
346 *
347 */
348
349static inline int handle_lcd_special_code(struct charlcd *lcd)
350{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200351 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100352 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100353
354 /* LCD special codes */
355
356 int processed = 0;
357
358 char *esc = priv->esc_seq.buf + 2;
359 int oldflags = priv->flags;
360
361 /* check for display mode flags */
362 switch (*esc) {
363 case 'D': /* Display ON */
364 priv->flags |= LCD_FLAG_D;
365 processed = 1;
366 break;
367 case 'd': /* Display OFF */
368 priv->flags &= ~LCD_FLAG_D;
369 processed = 1;
370 break;
371 case 'C': /* Cursor ON */
372 priv->flags |= LCD_FLAG_C;
373 processed = 1;
374 break;
375 case 'c': /* Cursor OFF */
376 priv->flags &= ~LCD_FLAG_C;
377 processed = 1;
378 break;
379 case 'B': /* Blink ON */
380 priv->flags |= LCD_FLAG_B;
381 processed = 1;
382 break;
383 case 'b': /* Blink OFF */
384 priv->flags &= ~LCD_FLAG_B;
385 processed = 1;
386 break;
387 case '+': /* Back light ON */
388 priv->flags |= LCD_FLAG_L;
389 processed = 1;
390 break;
391 case '-': /* Back light OFF */
392 priv->flags &= ~LCD_FLAG_L;
393 processed = 1;
394 break;
395 case '*': /* Flash back light */
396 charlcd_poke(lcd);
397 processed = 1;
398 break;
399 case 'f': /* Small Font */
400 priv->flags &= ~LCD_FLAG_F;
401 processed = 1;
402 break;
403 case 'F': /* Large Font */
404 priv->flags |= LCD_FLAG_F;
405 processed = 1;
406 break;
407 case 'n': /* One Line */
408 priv->flags &= ~LCD_FLAG_N;
409 processed = 1;
410 break;
411 case 'N': /* Two Lines */
412 priv->flags |= LCD_FLAG_N;
Robert Abel99b9b492018-02-26 00:54:29 +0100413 processed = 1;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100414 break;
415 case 'l': /* Shift Cursor Left */
416 if (priv->addr.x > 0) {
417 /* back one char if not at end of line */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100418 if (priv->addr.x < hdc->bwidth)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100419 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
420 priv->addr.x--;
421 }
422 processed = 1;
423 break;
424 case 'r': /* shift cursor right */
425 if (priv->addr.x < lcd->width) {
426 /* allow the cursor to pass the end of the line */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100427 if (priv->addr.x < (hdc->bwidth - 1))
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100428 lcd->ops->write_cmd(lcd,
429 LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
430 priv->addr.x++;
431 }
432 processed = 1;
433 break;
434 case 'L': /* shift display left */
435 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
436 processed = 1;
437 break;
438 case 'R': /* shift display right */
439 lcd->ops->write_cmd(lcd,
440 LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
441 LCD_CMD_SHIFT_RIGHT);
442 processed = 1;
443 break;
444 case 'k': { /* kill end of line */
445 int x;
446
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100447 for (x = priv->addr.x; x < hdc->bwidth; x++)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100448 lcd->ops->write_data(lcd, ' ');
449
450 /* restore cursor position */
451 charlcd_gotoxy(lcd);
452 processed = 1;
453 break;
454 }
455 case 'I': /* reinitialize display */
456 charlcd_init_display(lcd);
457 processed = 1;
458 break;
459 case 'G': {
460 /* Generator : LGcxxxxx...xx; must have <c> between '0'
461 * and '7', representing the numerical ASCII code of the
462 * redefined character, and <xx...xx> a sequence of 16
463 * hex digits representing 8 bytes for each character.
464 * Most LCDs will only use 5 lower bits of the 7 first
465 * bytes.
466 */
467
468 unsigned char cgbytes[8];
469 unsigned char cgaddr;
470 int cgoffset;
471 int shift;
472 char value;
473 int addr;
474
475 if (!strchr(esc, ';'))
476 break;
477
478 esc++;
479
480 cgaddr = *(esc++) - '0';
481 if (cgaddr > 7) {
482 processed = 1;
483 break;
484 }
485
486 cgoffset = 0;
487 shift = 0;
488 value = 0;
489 while (*esc && cgoffset < 8) {
Andy Shevchenko3f03b642020-05-18 22:36:17 +0300490 int half;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100491
Andy Shevchenko3f03b642020-05-18 22:36:17 +0300492 shift ^= 4;
493
494 half = hex_to_bin(*esc++);
495 if (half < 0)
496 continue;
497
498 value |= half << shift;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100499 if (shift == 0) {
500 cgbytes[cgoffset++] = value;
501 value = 0;
502 }
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100503 }
504
505 lcd->ops->write_cmd(lcd, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
506 for (addr = 0; addr < cgoffset; addr++)
507 lcd->ops->write_data(lcd, cgbytes[addr]);
508
509 /* ensures that we stop writing to CGRAM */
510 charlcd_gotoxy(lcd);
511 processed = 1;
512 break;
513 }
514 case 'x': /* gotoxy : LxXXX[yYYY]; */
515 case 'y': /* gotoxy : LyYYY[xXXX]; */
Mans Rullgard9bc30ab2018-12-05 13:52:47 +0000516 if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';')
517 break;
518
Miguel Ojedab34050f2018-02-27 23:09:52 +0100519 /* If the command is valid, move to the new address */
520 if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
521 charlcd_gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100522
Miguel Ojedab34050f2018-02-27 23:09:52 +0100523 /* Regardless of its validity, mark as processed */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100524 processed = 1;
525 break;
526 }
527
528 /* TODO: This indent party here got ugly, clean it! */
529 /* Check whether one flag was changed */
530 if (oldflags == priv->flags)
531 return processed;
532
533 /* check whether one of B,C,D flags were changed */
534 if ((oldflags ^ priv->flags) &
535 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
536 /* set display mode */
537 lcd->ops->write_cmd(lcd,
538 LCD_CMD_DISPLAY_CTRL |
539 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
540 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
541 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
542 /* check whether one of F,N flags was changed */
543 else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
544 lcd->ops->write_cmd(lcd,
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100545 LCD_CMD_FUNCTION_SET |
546 ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100547 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
548 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
549 /* check whether L flag was changed */
550 else if ((oldflags ^ priv->flags) & LCD_FLAG_L)
551 charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
552
553 return processed;
554}
555
556static void charlcd_write_char(struct charlcd *lcd, char c)
557{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200558 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100559 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100560
561 /* first, we'll test if we're in escape mode */
562 if ((c != '\n') && priv->esc_seq.len >= 0) {
563 /* yes, let's add this char to the buffer */
564 priv->esc_seq.buf[priv->esc_seq.len++] = c;
Robert Abel8c483752018-02-10 00:50:11 +0100565 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100566 } else {
567 /* aborts any previous escape sequence */
568 priv->esc_seq.len = -1;
569
570 switch (c) {
571 case LCD_ESCAPE_CHAR:
572 /* start of an escape sequence */
573 priv->esc_seq.len = 0;
Robert Abel8c483752018-02-10 00:50:11 +0100574 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100575 break;
576 case '\b':
577 /* go back one char and clear it */
578 if (priv->addr.x > 0) {
579 /*
580 * check if we're not at the
581 * end of the line
582 */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100583 if (priv->addr.x < hdc->bwidth)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100584 /* back one char */
585 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
586 priv->addr.x--;
587 }
588 /* replace with a space */
589 lcd->ops->write_data(lcd, ' ');
590 /* back one char again */
591 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
592 break;
Robert Abel9629ccc2018-02-10 00:50:12 +0100593 case '\f':
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100594 /* quickly clear the display */
595 charlcd_clear_fast(lcd);
596 break;
597 case '\n':
598 /*
599 * flush the remainder of the current line and
600 * go to the beginning of the next line
601 */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100602 for (; priv->addr.x < hdc->bwidth; priv->addr.x++)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100603 lcd->ops->write_data(lcd, ' ');
604 priv->addr.x = 0;
605 priv->addr.y = (priv->addr.y + 1) % lcd->height;
606 charlcd_gotoxy(lcd);
607 break;
608 case '\r':
609 /* go to the beginning of the same line */
610 priv->addr.x = 0;
611 charlcd_gotoxy(lcd);
612 break;
613 case '\t':
614 /* print a space instead of the tab */
615 charlcd_print(lcd, ' ');
616 break;
617 default:
618 /* simply print this char */
619 charlcd_print(lcd, c);
620 break;
621 }
622 }
623
624 /*
625 * now we'll see if we're in an escape mode and if the current
626 * escape sequence can be understood.
627 */
628 if (priv->esc_seq.len >= 2) {
629 int processed = 0;
630
631 if (!strcmp(priv->esc_seq.buf, "[2J")) {
632 /* clear the display */
633 charlcd_clear_fast(lcd);
634 processed = 1;
635 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
636 /* cursor to home */
637 charlcd_home(lcd);
638 processed = 1;
639 }
640 /* codes starting with ^[[L */
641 else if ((priv->esc_seq.len >= 3) &&
642 (priv->esc_seq.buf[0] == '[') &&
643 (priv->esc_seq.buf[1] == 'L')) {
644 processed = handle_lcd_special_code(lcd);
645 }
646
647 /* LCD special escape codes */
648 /*
649 * flush the escape sequence if it's been processed
650 * or if it is getting too long.
651 */
652 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
653 priv->esc_seq.len = -1;
654 } /* escape codes */
655}
656
657static struct charlcd *the_charlcd;
658
659static ssize_t charlcd_write(struct file *file, const char __user *buf,
660 size_t count, loff_t *ppos)
661{
662 const char __user *tmp = buf;
663 char c;
664
665 for (; count-- > 0; (*ppos)++, tmp++) {
666 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
667 /*
668 * let's be a little nice with other processes
669 * that need some CPU
670 */
671 schedule();
672
673 if (get_user(c, tmp))
674 return -EFAULT;
675
676 charlcd_write_char(the_charlcd, c);
677 }
678
679 return tmp - buf;
680}
681
682static int charlcd_open(struct inode *inode, struct file *file)
683{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200684 struct charlcd_priv *priv = charlcd_to_priv(the_charlcd);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200685 int ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100686
Willy Tarreau93dc1772017-09-07 15:37:30 +0200687 ret = -EBUSY;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100688 if (!atomic_dec_and_test(&charlcd_available))
Willy Tarreau93dc1772017-09-07 15:37:30 +0200689 goto fail; /* open only once at a time */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100690
Willy Tarreau93dc1772017-09-07 15:37:30 +0200691 ret = -EPERM;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100692 if (file->f_mode & FMODE_READ) /* device is write-only */
Willy Tarreau93dc1772017-09-07 15:37:30 +0200693 goto fail;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100694
695 if (priv->must_clear) {
696 charlcd_clear_display(&priv->lcd);
697 priv->must_clear = false;
698 }
699 return nonseekable_open(inode, file);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200700
701 fail:
702 atomic_inc(&charlcd_available);
703 return ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100704}
705
706static int charlcd_release(struct inode *inode, struct file *file)
707{
708 atomic_inc(&charlcd_available);
709 return 0;
710}
711
712static const struct file_operations charlcd_fops = {
713 .write = charlcd_write,
714 .open = charlcd_open,
715 .release = charlcd_release,
716 .llseek = no_llseek,
717};
718
719static struct miscdevice charlcd_dev = {
720 .minor = LCD_MINOR,
721 .name = "lcd",
722 .fops = &charlcd_fops,
723};
724
725static void charlcd_puts(struct charlcd *lcd, const char *s)
726{
727 const char *tmp = s;
728 int count = strlen(s);
729
730 for (; count-- > 0; tmp++) {
731 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
732 /*
733 * let's be a little nice with other processes
734 * that need some CPU
735 */
736 schedule();
737
738 charlcd_write_char(lcd, *tmp);
739 }
740}
741
Mans Rullgardc9171722019-03-01 18:48:15 +0000742#ifdef CONFIG_PANEL_BOOT_MESSAGE
743#define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
744#else
745#define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
746#endif
747
Mans Rullgardcc5d04d2019-03-01 18:48:16 +0000748#ifdef CONFIG_CHARLCD_BL_ON
749#define LCD_INIT_BL "\x1b[L+"
750#elif defined(CONFIG_CHARLCD_BL_FLASH)
751#define LCD_INIT_BL "\x1b[L*"
752#else
753#define LCD_INIT_BL "\x1b[L-"
754#endif
755
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100756/* initialize the LCD driver */
757static int charlcd_init(struct charlcd *lcd)
758{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200759 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100760 int ret;
761
762 if (lcd->ops->backlight) {
763 mutex_init(&priv->bl_tempo_lock);
764 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
765 }
766
767 /*
768 * before this line, we must NOT send anything to the display.
769 * Since charlcd_init_display() needs to write data, we have to
770 * enable mark the LCD initialized just before.
771 */
772 ret = charlcd_init_display(lcd);
773 if (ret)
774 return ret;
775
776 /* display a short message */
Mans Rullgardcc5d04d2019-03-01 18:48:16 +0000777 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT);
Mans Rullgardc9171722019-03-01 18:48:15 +0000778
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100779 /* clear the display on the next device opening */
780 priv->must_clear = true;
781 charlcd_home(lcd);
782 return 0;
783}
784
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100785struct charlcd *charlcd_alloc(void)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100786{
787 struct charlcd_priv *priv;
788 struct charlcd *lcd;
789
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100790 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100791 if (!priv)
792 return NULL;
793
794 priv->esc_seq.len = -1;
795
796 lcd = &priv->lcd;
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100797 lcd->ifwidth = 8;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100798
799 return lcd;
800}
801EXPORT_SYMBOL_GPL(charlcd_alloc);
802
Andy Shevchenko8e44fc82019-03-12 16:44:30 +0200803void charlcd_free(struct charlcd *lcd)
804{
805 kfree(charlcd_to_priv(lcd));
806}
807EXPORT_SYMBOL_GPL(charlcd_free);
808
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100809static int panel_notify_sys(struct notifier_block *this, unsigned long code,
810 void *unused)
811{
812 struct charlcd *lcd = the_charlcd;
813
814 switch (code) {
815 case SYS_DOWN:
816 charlcd_puts(lcd,
817 "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
818 break;
819 case SYS_HALT:
820 charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
821 break;
822 case SYS_POWER_OFF:
823 charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
824 break;
825 default:
826 break;
827 }
828 return NOTIFY_DONE;
829}
830
831static struct notifier_block panel_notifier = {
832 panel_notify_sys,
833 NULL,
834 0
835};
836
837int charlcd_register(struct charlcd *lcd)
838{
839 int ret;
840
841 ret = charlcd_init(lcd);
842 if (ret)
843 return ret;
844
845 ret = misc_register(&charlcd_dev);
846 if (ret)
847 return ret;
848
849 the_charlcd = lcd;
850 register_reboot_notifier(&panel_notifier);
851 return 0;
852}
853EXPORT_SYMBOL_GPL(charlcd_register);
854
855int charlcd_unregister(struct charlcd *lcd)
856{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200857 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100858
859 unregister_reboot_notifier(&panel_notifier);
860 charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
861 misc_deregister(&charlcd_dev);
862 the_charlcd = NULL;
863 if (lcd->ops->backlight) {
864 cancel_delayed_work_sync(&priv->bl_work);
865 priv->lcd.ops->backlight(&priv->lcd, 0);
866 }
867
868 return 0;
869}
870EXPORT_SYMBOL_GPL(charlcd_unregister);
871
872MODULE_LICENSE("GPL");