blob: 59e0a815bf3d277c3b4486c9a7c817e0a5ed0be9 [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);
Lars Poeschel3fc04dd2020-11-03 10:58:07 +0100226 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100227 u8 init;
228
Lars Poeschel3fc04dd2020-11-03 10:58:07 +0100229 if (hdc->ifwidth != 4 && hdc->ifwidth != 8)
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100230 return -EINVAL;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100231
232 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
233 LCD_FLAG_C | LCD_FLAG_B;
234
235 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
236
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100237 /*
238 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
239 * the LCD is in 8-bit mode afterwards
240 */
241 init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
Lars Poeschel3fc04dd2020-11-03 10:58:07 +0100242 if (hdc->ifwidth == 4) {
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100243 init >>= 4;
244 write_cmd_raw = lcd->ops->write_cmd_raw4;
245 } else {
246 write_cmd_raw = lcd->ops->write_cmd;
247 }
248 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100249 long_sleep(10);
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100250 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100251 long_sleep(10);
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100252 write_cmd_raw(lcd, init);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100253 long_sleep(10);
254
Lars Poeschel3fc04dd2020-11-03 10:58:07 +0100255 if (hdc->ifwidth == 4) {
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100256 /* Switch to 4-bit mode, 1 line, small fonts */
257 lcd->ops->write_cmd_raw4(lcd, LCD_CMD_FUNCTION_SET >> 4);
258 long_sleep(10);
259 }
260
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100261 /* set font height and lines number */
262 lcd->ops->write_cmd(lcd,
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100263 LCD_CMD_FUNCTION_SET |
Lars Poeschel3fc04dd2020-11-03 10:58:07 +0100264 ((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100265 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
266 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
267 long_sleep(10);
268
269 /* display off, cursor off, blink off */
270 lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CTRL);
271 long_sleep(10);
272
273 lcd->ops->write_cmd(lcd,
274 LCD_CMD_DISPLAY_CTRL | /* set display mode */
275 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
276 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
277 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
278
279 charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0);
280
281 long_sleep(10);
282
283 /* entry mode set : increment, cursor shifting */
284 lcd->ops->write_cmd(lcd, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
285
286 charlcd_clear_display(lcd);
287 return 0;
288}
289
290/*
Miguel Ojedab34050f2018-02-27 23:09:52 +0100291 * Parses a movement command of the form "(.*);", where the group can be
292 * any number of subcommands of the form "(x|y)[0-9]+".
293 *
294 * Returns whether the command is valid. The position arguments are
295 * only written if the parsing was successful.
296 *
297 * For instance:
298 * - ";" returns (<original x>, <original y>).
299 * - "x1;" returns (1, <original y>).
300 * - "y2x1;" returns (1, 2).
301 * - "x12y34x56;" returns (56, 34).
302 * - "" fails.
303 * - "x" fails.
304 * - "x;" fails.
305 * - "x1" fails.
306 * - "xy12;" fails.
307 * - "x12yy12;" fails.
308 * - "xx" fails.
309 */
310static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
311{
312 unsigned long new_x = *x;
313 unsigned long new_y = *y;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800314 char *p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100315
316 for (;;) {
317 if (!*s)
318 return false;
319
320 if (*s == ';')
321 break;
322
323 if (*s == 'x') {
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800324 new_x = simple_strtoul(s + 1, &p, 10);
325 if (p == s + 1)
Miguel Ojedab34050f2018-02-27 23:09:52 +0100326 return false;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800327 s = p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100328 } else if (*s == 'y') {
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800329 new_y = simple_strtoul(s + 1, &p, 10);
330 if (p == s + 1)
Miguel Ojedab34050f2018-02-27 23:09:52 +0100331 return false;
Andy Shevchenkod717e7d2019-12-04 16:50:36 -0800332 s = p;
Miguel Ojedab34050f2018-02-27 23:09:52 +0100333 } else {
334 return false;
335 }
336 }
337
338 *x = new_x;
339 *y = new_y;
340 return true;
341}
342
343/*
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100344 * These are the file operation function for user access to /dev/lcd
345 * This function can also be called from inside the kernel, by
346 * setting file and ppos to NULL.
347 *
348 */
349
350static inline int handle_lcd_special_code(struct charlcd *lcd)
351{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200352 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100353 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100354
355 /* LCD special codes */
356
357 int processed = 0;
358
359 char *esc = priv->esc_seq.buf + 2;
360 int oldflags = priv->flags;
361
362 /* check for display mode flags */
363 switch (*esc) {
364 case 'D': /* Display ON */
365 priv->flags |= LCD_FLAG_D;
366 processed = 1;
367 break;
368 case 'd': /* Display OFF */
369 priv->flags &= ~LCD_FLAG_D;
370 processed = 1;
371 break;
372 case 'C': /* Cursor ON */
373 priv->flags |= LCD_FLAG_C;
374 processed = 1;
375 break;
376 case 'c': /* Cursor OFF */
377 priv->flags &= ~LCD_FLAG_C;
378 processed = 1;
379 break;
380 case 'B': /* Blink ON */
381 priv->flags |= LCD_FLAG_B;
382 processed = 1;
383 break;
384 case 'b': /* Blink OFF */
385 priv->flags &= ~LCD_FLAG_B;
386 processed = 1;
387 break;
388 case '+': /* Back light ON */
389 priv->flags |= LCD_FLAG_L;
390 processed = 1;
391 break;
392 case '-': /* Back light OFF */
393 priv->flags &= ~LCD_FLAG_L;
394 processed = 1;
395 break;
396 case '*': /* Flash back light */
397 charlcd_poke(lcd);
398 processed = 1;
399 break;
400 case 'f': /* Small Font */
401 priv->flags &= ~LCD_FLAG_F;
402 processed = 1;
403 break;
404 case 'F': /* Large Font */
405 priv->flags |= LCD_FLAG_F;
406 processed = 1;
407 break;
408 case 'n': /* One Line */
409 priv->flags &= ~LCD_FLAG_N;
410 processed = 1;
411 break;
412 case 'N': /* Two Lines */
413 priv->flags |= LCD_FLAG_N;
Robert Abel99b9b492018-02-26 00:54:29 +0100414 processed = 1;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100415 break;
416 case 'l': /* Shift Cursor Left */
417 if (priv->addr.x > 0) {
418 /* back one char if not at end of line */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100419 if (priv->addr.x < hdc->bwidth)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100420 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
421 priv->addr.x--;
422 }
423 processed = 1;
424 break;
425 case 'r': /* shift cursor right */
426 if (priv->addr.x < lcd->width) {
427 /* allow the cursor to pass the end of the line */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100428 if (priv->addr.x < (hdc->bwidth - 1))
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100429 lcd->ops->write_cmd(lcd,
430 LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
431 priv->addr.x++;
432 }
433 processed = 1;
434 break;
435 case 'L': /* shift display left */
436 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
437 processed = 1;
438 break;
439 case 'R': /* shift display right */
440 lcd->ops->write_cmd(lcd,
441 LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
442 LCD_CMD_SHIFT_RIGHT);
443 processed = 1;
444 break;
445 case 'k': { /* kill end of line */
446 int x;
447
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100448 for (x = priv->addr.x; x < hdc->bwidth; x++)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100449 lcd->ops->write_data(lcd, ' ');
450
451 /* restore cursor position */
452 charlcd_gotoxy(lcd);
453 processed = 1;
454 break;
455 }
456 case 'I': /* reinitialize display */
457 charlcd_init_display(lcd);
458 processed = 1;
459 break;
460 case 'G': {
461 /* Generator : LGcxxxxx...xx; must have <c> between '0'
462 * and '7', representing the numerical ASCII code of the
463 * redefined character, and <xx...xx> a sequence of 16
464 * hex digits representing 8 bytes for each character.
465 * Most LCDs will only use 5 lower bits of the 7 first
466 * bytes.
467 */
468
469 unsigned char cgbytes[8];
470 unsigned char cgaddr;
471 int cgoffset;
472 int shift;
473 char value;
474 int addr;
475
476 if (!strchr(esc, ';'))
477 break;
478
479 esc++;
480
481 cgaddr = *(esc++) - '0';
482 if (cgaddr > 7) {
483 processed = 1;
484 break;
485 }
486
487 cgoffset = 0;
488 shift = 0;
489 value = 0;
490 while (*esc && cgoffset < 8) {
Andy Shevchenko3f03b642020-05-18 22:36:17 +0300491 int half;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100492
Andy Shevchenko3f03b642020-05-18 22:36:17 +0300493 shift ^= 4;
494
495 half = hex_to_bin(*esc++);
496 if (half < 0)
497 continue;
498
499 value |= half << shift;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100500 if (shift == 0) {
501 cgbytes[cgoffset++] = value;
502 value = 0;
503 }
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100504 }
505
506 lcd->ops->write_cmd(lcd, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
507 for (addr = 0; addr < cgoffset; addr++)
508 lcd->ops->write_data(lcd, cgbytes[addr]);
509
510 /* ensures that we stop writing to CGRAM */
511 charlcd_gotoxy(lcd);
512 processed = 1;
513 break;
514 }
515 case 'x': /* gotoxy : LxXXX[yYYY]; */
516 case 'y': /* gotoxy : LyYYY[xXXX]; */
Mans Rullgard9bc30ab2018-12-05 13:52:47 +0000517 if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';')
518 break;
519
Miguel Ojedab34050f2018-02-27 23:09:52 +0100520 /* If the command is valid, move to the new address */
521 if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
522 charlcd_gotoxy(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100523
Miguel Ojedab34050f2018-02-27 23:09:52 +0100524 /* Regardless of its validity, mark as processed */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100525 processed = 1;
526 break;
527 }
528
529 /* TODO: This indent party here got ugly, clean it! */
530 /* Check whether one flag was changed */
531 if (oldflags == priv->flags)
532 return processed;
533
534 /* check whether one of B,C,D flags were changed */
535 if ((oldflags ^ priv->flags) &
536 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
537 /* set display mode */
538 lcd->ops->write_cmd(lcd,
539 LCD_CMD_DISPLAY_CTRL |
540 ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) |
541 ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) |
542 ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0));
543 /* check whether one of F,N flags was changed */
544 else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
545 lcd->ops->write_cmd(lcd,
Geert Uytterhoevenac201472017-03-10 15:15:18 +0100546 LCD_CMD_FUNCTION_SET |
Lars Poeschel3fc04dd2020-11-03 10:58:07 +0100547 ((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100548 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
549 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
550 /* check whether L flag was changed */
551 else if ((oldflags ^ priv->flags) & LCD_FLAG_L)
552 charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L));
553
554 return processed;
555}
556
557static void charlcd_write_char(struct charlcd *lcd, char c)
558{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200559 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100560 struct hd44780_common *hdc = lcd->drvdata;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100561
562 /* first, we'll test if we're in escape mode */
563 if ((c != '\n') && priv->esc_seq.len >= 0) {
564 /* yes, let's add this char to the buffer */
565 priv->esc_seq.buf[priv->esc_seq.len++] = c;
Robert Abel8c483752018-02-10 00:50:11 +0100566 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100567 } else {
568 /* aborts any previous escape sequence */
569 priv->esc_seq.len = -1;
570
571 switch (c) {
572 case LCD_ESCAPE_CHAR:
573 /* start of an escape sequence */
574 priv->esc_seq.len = 0;
Robert Abel8c483752018-02-10 00:50:11 +0100575 priv->esc_seq.buf[priv->esc_seq.len] = '\0';
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100576 break;
577 case '\b':
578 /* go back one char and clear it */
579 if (priv->addr.x > 0) {
580 /*
581 * check if we're not at the
582 * end of the line
583 */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100584 if (priv->addr.x < hdc->bwidth)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100585 /* back one char */
586 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
587 priv->addr.x--;
588 }
589 /* replace with a space */
590 lcd->ops->write_data(lcd, ' ');
591 /* back one char again */
592 lcd->ops->write_cmd(lcd, LCD_CMD_SHIFT);
593 break;
Robert Abel9629ccc2018-02-10 00:50:12 +0100594 case '\f':
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100595 /* quickly clear the display */
596 charlcd_clear_fast(lcd);
597 break;
598 case '\n':
599 /*
600 * flush the remainder of the current line and
601 * go to the beginning of the next line
602 */
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100603 for (; priv->addr.x < hdc->bwidth; priv->addr.x++)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100604 lcd->ops->write_data(lcd, ' ');
605 priv->addr.x = 0;
606 priv->addr.y = (priv->addr.y + 1) % lcd->height;
607 charlcd_gotoxy(lcd);
608 break;
609 case '\r':
610 /* go to the beginning of the same line */
611 priv->addr.x = 0;
612 charlcd_gotoxy(lcd);
613 break;
614 case '\t':
615 /* print a space instead of the tab */
616 charlcd_print(lcd, ' ');
617 break;
618 default:
619 /* simply print this char */
620 charlcd_print(lcd, c);
621 break;
622 }
623 }
624
625 /*
626 * now we'll see if we're in an escape mode and if the current
627 * escape sequence can be understood.
628 */
629 if (priv->esc_seq.len >= 2) {
630 int processed = 0;
631
632 if (!strcmp(priv->esc_seq.buf, "[2J")) {
633 /* clear the display */
634 charlcd_clear_fast(lcd);
635 processed = 1;
636 } else if (!strcmp(priv->esc_seq.buf, "[H")) {
637 /* cursor to home */
638 charlcd_home(lcd);
639 processed = 1;
640 }
641 /* codes starting with ^[[L */
642 else if ((priv->esc_seq.len >= 3) &&
643 (priv->esc_seq.buf[0] == '[') &&
644 (priv->esc_seq.buf[1] == 'L')) {
645 processed = handle_lcd_special_code(lcd);
646 }
647
648 /* LCD special escape codes */
649 /*
650 * flush the escape sequence if it's been processed
651 * or if it is getting too long.
652 */
653 if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN))
654 priv->esc_seq.len = -1;
655 } /* escape codes */
656}
657
658static struct charlcd *the_charlcd;
659
660static ssize_t charlcd_write(struct file *file, const char __user *buf,
661 size_t count, loff_t *ppos)
662{
663 const char __user *tmp = buf;
664 char c;
665
666 for (; count-- > 0; (*ppos)++, tmp++) {
667 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
668 /*
669 * let's be a little nice with other processes
670 * that need some CPU
671 */
672 schedule();
673
674 if (get_user(c, tmp))
675 return -EFAULT;
676
677 charlcd_write_char(the_charlcd, c);
678 }
679
680 return tmp - buf;
681}
682
683static int charlcd_open(struct inode *inode, struct file *file)
684{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200685 struct charlcd_priv *priv = charlcd_to_priv(the_charlcd);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200686 int ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100687
Willy Tarreau93dc1772017-09-07 15:37:30 +0200688 ret = -EBUSY;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100689 if (!atomic_dec_and_test(&charlcd_available))
Willy Tarreau93dc1772017-09-07 15:37:30 +0200690 goto fail; /* open only once at a time */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100691
Willy Tarreau93dc1772017-09-07 15:37:30 +0200692 ret = -EPERM;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100693 if (file->f_mode & FMODE_READ) /* device is write-only */
Willy Tarreau93dc1772017-09-07 15:37:30 +0200694 goto fail;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100695
696 if (priv->must_clear) {
697 charlcd_clear_display(&priv->lcd);
698 priv->must_clear = false;
699 }
700 return nonseekable_open(inode, file);
Willy Tarreau93dc1772017-09-07 15:37:30 +0200701
702 fail:
703 atomic_inc(&charlcd_available);
704 return ret;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100705}
706
707static int charlcd_release(struct inode *inode, struct file *file)
708{
709 atomic_inc(&charlcd_available);
710 return 0;
711}
712
713static const struct file_operations charlcd_fops = {
714 .write = charlcd_write,
715 .open = charlcd_open,
716 .release = charlcd_release,
717 .llseek = no_llseek,
718};
719
720static struct miscdevice charlcd_dev = {
721 .minor = LCD_MINOR,
722 .name = "lcd",
723 .fops = &charlcd_fops,
724};
725
726static void charlcd_puts(struct charlcd *lcd, const char *s)
727{
728 const char *tmp = s;
729 int count = strlen(s);
730
731 for (; count-- > 0; tmp++) {
732 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
733 /*
734 * let's be a little nice with other processes
735 * that need some CPU
736 */
737 schedule();
738
739 charlcd_write_char(lcd, *tmp);
740 }
741}
742
Mans Rullgardc9171722019-03-01 18:48:15 +0000743#ifdef CONFIG_PANEL_BOOT_MESSAGE
744#define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
745#else
746#define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n"
747#endif
748
Mans Rullgardcc5d04d2019-03-01 18:48:16 +0000749#ifdef CONFIG_CHARLCD_BL_ON
750#define LCD_INIT_BL "\x1b[L+"
751#elif defined(CONFIG_CHARLCD_BL_FLASH)
752#define LCD_INIT_BL "\x1b[L*"
753#else
754#define LCD_INIT_BL "\x1b[L-"
755#endif
756
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100757/* initialize the LCD driver */
758static int charlcd_init(struct charlcd *lcd)
759{
Andy Shevchenkob658a212019-03-12 16:44:29 +0200760 struct charlcd_priv *priv = charlcd_to_priv(lcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100761 int ret;
762
763 if (lcd->ops->backlight) {
764 mutex_init(&priv->bl_tempo_lock);
765 INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off);
766 }
767
768 /*
769 * before this line, we must NOT send anything to the display.
770 * Since charlcd_init_display() needs to write data, we have to
771 * enable mark the LCD initialized just before.
772 */
773 ret = charlcd_init_display(lcd);
774 if (ret)
775 return ret;
776
777 /* display a short message */
Mans Rullgardcc5d04d2019-03-01 18:48:16 +0000778 charlcd_puts(lcd, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT);
Mans Rullgardc9171722019-03-01 18:48:15 +0000779
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100780 /* clear the display on the next device opening */
781 priv->must_clear = true;
782 charlcd_home(lcd);
783 return 0;
784}
785
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100786struct charlcd *charlcd_alloc(void)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100787{
788 struct charlcd_priv *priv;
789 struct charlcd *lcd;
790
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100791 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100792 if (!priv)
793 return NULL;
794
795 priv->esc_seq.len = -1;
796
797 lcd = &priv->lcd;
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");