Miguel Ojeda | 351f683b | 2018-02-17 20:33:13 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 2 | /* |
| 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 Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/atomic.h> |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 10 | #include <linux/ctype.h> |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 11 | #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 Yamada | 7535428 | 2019-08-06 16:14:44 +0900 | [diff] [blame] | 23 | #include "charlcd.h" |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 24 | #include "hd44780_common.h" |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 25 | |
| 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 | |
| 63 | struct 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. Silva | 2f920c0 | 2020-02-12 13:52:31 -0600 | [diff] [blame] | 87 | unsigned long long drvdata[]; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 90 | #define charlcd_to_priv(p) container_of(p, struct charlcd_priv, lcd) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 91 | |
| 92 | /* Device single-open policy control */ |
| 93 | static atomic_t charlcd_available = ATOMIC_INIT(1); |
| 94 | |
| 95 | /* sleeps that many milliseconds with a reschedule */ |
| 96 | static void long_sleep(int ms) |
| 97 | { |
Jia-Ju Bai | 1716139 | 2018-01-26 23:19:15 +0800 | [diff] [blame] | 98 | schedule_timeout_interruptible(msecs_to_jiffies(ms)); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* turn the backlight on or off */ |
Lars Poeschel | 66ce7d5 | 2020-11-03 10:58:04 +0100 | [diff] [blame] | 102 | static void charlcd_backlight(struct charlcd *lcd, enum charlcd_onoff on) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 103 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 104 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 105 | |
| 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 | |
| 115 | static 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 */ |
| 131 | void charlcd_poke(struct charlcd *lcd) |
| 132 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 133 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 134 | |
| 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 | } |
| 147 | EXPORT_SYMBOL_GPL(charlcd_poke); |
| 148 | |
| 149 | static void charlcd_gotoxy(struct charlcd *lcd) |
| 150 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 151 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 152 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | 1d3b2af | 2017-03-10 15:15:19 +0100 | [diff] [blame] | 153 | unsigned int addr; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 154 | |
Geert Uytterhoeven | 1d3b2af | 2017-03-10 15:15:19 +0100 | [diff] [blame] | 155 | /* |
| 156 | * we force the cursor to stay at the end of the |
| 157 | * line if it wants to go farther |
| 158 | */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 159 | addr = priv->addr.x < hdc->bwidth ? priv->addr.x & (hdc->hwidth - 1) |
| 160 | : hdc->bwidth - 1; |
Geert Uytterhoeven | 1d3b2af | 2017-03-10 15:15:19 +0100 | [diff] [blame] | 161 | if (priv->addr.y & 1) |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 162 | addr += hdc->hwidth; |
Geert Uytterhoeven | 1d3b2af | 2017-03-10 15:15:19 +0100 | [diff] [blame] | 163 | if (priv->addr.y & 2) |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 164 | addr += hdc->bwidth; |
Geert Uytterhoeven | 1d3b2af | 2017-03-10 15:15:19 +0100 | [diff] [blame] | 165 | lcd->ops->write_cmd(lcd, LCD_CMD_SET_DDRAM_ADDR | addr); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void charlcd_home(struct charlcd *lcd) |
| 169 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 170 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 171 | |
| 172 | priv->addr.x = 0; |
| 173 | priv->addr.y = 0; |
| 174 | charlcd_gotoxy(lcd); |
| 175 | } |
| 176 | |
| 177 | static void charlcd_print(struct charlcd *lcd, char c) |
| 178 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 179 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 180 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 181 | |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 182 | if (priv->addr.x < hdc->bwidth) { |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 183 | if (lcd->char_conv) |
| 184 | c = lcd->char_conv[(unsigned char)c]; |
| 185 | lcd->ops->write_data(lcd, c); |
| 186 | priv->addr.x++; |
Sean Young | 54bc937f | 2018-01-15 09:43:48 +0000 | [diff] [blame] | 187 | |
| 188 | /* prevents the cursor from wrapping onto the next line */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 189 | if (priv->addr.x == hdc->bwidth) |
Sean Young | 54bc937f | 2018-01-15 09:43:48 +0000 | [diff] [blame] | 190 | charlcd_gotoxy(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 191 | } |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void charlcd_clear_fast(struct charlcd *lcd) |
| 195 | { |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 196 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 197 | int pos; |
| 198 | |
| 199 | charlcd_home(lcd); |
| 200 | |
| 201 | if (lcd->ops->clear_fast) |
| 202 | lcd->ops->clear_fast(lcd); |
| 203 | else |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 204 | for (pos = 0; pos < min(2, lcd->height) * hdc->hwidth; pos++) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 205 | lcd->ops->write_data(lcd, ' '); |
| 206 | |
| 207 | charlcd_home(lcd); |
| 208 | } |
| 209 | |
| 210 | /* clears the display and resets X/Y */ |
| 211 | static void charlcd_clear_display(struct charlcd *lcd) |
| 212 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 213 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 214 | |
| 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 | |
| 222 | static int charlcd_init_display(struct charlcd *lcd) |
| 223 | { |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 224 | void (*write_cmd_raw)(struct charlcd *lcd, int cmd); |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 225 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 226 | u8 init; |
| 227 | |
| 228 | if (lcd->ifwidth != 4 && lcd->ifwidth != 8) |
| 229 | return -EINVAL; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 230 | |
| 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 Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 236 | /* |
| 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 Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 248 | long_sleep(10); |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 249 | write_cmd_raw(lcd, init); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 250 | long_sleep(10); |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 251 | write_cmd_raw(lcd, init); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 252 | long_sleep(10); |
| 253 | |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 254 | 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 Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 260 | /* set font height and lines number */ |
| 261 | lcd->ops->write_cmd(lcd, |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 262 | LCD_CMD_FUNCTION_SET | |
| 263 | ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 264 | ((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 Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 290 | * 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 | */ |
| 309 | static 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 Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 313 | char *p; |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 314 | |
| 315 | for (;;) { |
| 316 | if (!*s) |
| 317 | return false; |
| 318 | |
| 319 | if (*s == ';') |
| 320 | break; |
| 321 | |
| 322 | if (*s == 'x') { |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 323 | new_x = simple_strtoul(s + 1, &p, 10); |
| 324 | if (p == s + 1) |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 325 | return false; |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 326 | s = p; |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 327 | } else if (*s == 'y') { |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 328 | new_y = simple_strtoul(s + 1, &p, 10); |
| 329 | if (p == s + 1) |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 330 | return false; |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 331 | s = p; |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 332 | } else { |
| 333 | return false; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | *x = new_x; |
| 338 | *y = new_y; |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | /* |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 343 | * 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 | |
| 349 | static inline int handle_lcd_special_code(struct charlcd *lcd) |
| 350 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 351 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 352 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 353 | |
| 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 Abel | 99b9b49 | 2018-02-26 00:54:29 +0100 | [diff] [blame] | 413 | processed = 1; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 414 | break; |
| 415 | case 'l': /* Shift Cursor Left */ |
| 416 | if (priv->addr.x > 0) { |
| 417 | /* back one char if not at end of line */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 418 | if (priv->addr.x < hdc->bwidth) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 419 | 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 Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 427 | if (priv->addr.x < (hdc->bwidth - 1)) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 428 | 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 Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 447 | for (x = priv->addr.x; x < hdc->bwidth; x++) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 448 | 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 Shevchenko | 3f03b64 | 2020-05-18 22:36:17 +0300 | [diff] [blame] | 490 | int half; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 491 | |
Andy Shevchenko | 3f03b64 | 2020-05-18 22:36:17 +0300 | [diff] [blame] | 492 | shift ^= 4; |
| 493 | |
| 494 | half = hex_to_bin(*esc++); |
| 495 | if (half < 0) |
| 496 | continue; |
| 497 | |
| 498 | value |= half << shift; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 499 | if (shift == 0) { |
| 500 | cgbytes[cgoffset++] = value; |
| 501 | value = 0; |
| 502 | } |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 503 | } |
| 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 Rullgard | 9bc30ab | 2018-12-05 13:52:47 +0000 | [diff] [blame] | 516 | if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';') |
| 517 | break; |
| 518 | |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 519 | /* 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 Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 522 | |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 523 | /* Regardless of its validity, mark as processed */ |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 524 | 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 Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 545 | LCD_CMD_FUNCTION_SET | |
| 546 | ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 547 | ((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 | |
| 556 | static void charlcd_write_char(struct charlcd *lcd, char c) |
| 557 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 558 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 559 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 560 | |
| 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 Abel | 8c48375 | 2018-02-10 00:50:11 +0100 | [diff] [blame] | 565 | priv->esc_seq.buf[priv->esc_seq.len] = '\0'; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 566 | } 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 Abel | 8c48375 | 2018-02-10 00:50:11 +0100 | [diff] [blame] | 574 | priv->esc_seq.buf[priv->esc_seq.len] = '\0'; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 575 | 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 Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 583 | if (priv->addr.x < hdc->bwidth) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 584 | /* 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 Abel | 9629ccc | 2018-02-10 00:50:12 +0100 | [diff] [blame] | 593 | case '\f': |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 594 | /* 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 Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 602 | for (; priv->addr.x < hdc->bwidth; priv->addr.x++) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 603 | 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 | |
| 657 | static struct charlcd *the_charlcd; |
| 658 | |
| 659 | static 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 | |
| 682 | static int charlcd_open(struct inode *inode, struct file *file) |
| 683 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 684 | struct charlcd_priv *priv = charlcd_to_priv(the_charlcd); |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 685 | int ret; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 686 | |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 687 | ret = -EBUSY; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 688 | if (!atomic_dec_and_test(&charlcd_available)) |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 689 | goto fail; /* open only once at a time */ |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 690 | |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 691 | ret = -EPERM; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 692 | if (file->f_mode & FMODE_READ) /* device is write-only */ |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 693 | goto fail; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 694 | |
| 695 | if (priv->must_clear) { |
| 696 | charlcd_clear_display(&priv->lcd); |
| 697 | priv->must_clear = false; |
| 698 | } |
| 699 | return nonseekable_open(inode, file); |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 700 | |
| 701 | fail: |
| 702 | atomic_inc(&charlcd_available); |
| 703 | return ret; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static int charlcd_release(struct inode *inode, struct file *file) |
| 707 | { |
| 708 | atomic_inc(&charlcd_available); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static const struct file_operations charlcd_fops = { |
| 713 | .write = charlcd_write, |
| 714 | .open = charlcd_open, |
| 715 | .release = charlcd_release, |
| 716 | .llseek = no_llseek, |
| 717 | }; |
| 718 | |
| 719 | static struct miscdevice charlcd_dev = { |
| 720 | .minor = LCD_MINOR, |
| 721 | .name = "lcd", |
| 722 | .fops = &charlcd_fops, |
| 723 | }; |
| 724 | |
| 725 | static 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 Rullgard | c917172 | 2019-03-01 18:48:15 +0000 | [diff] [blame] | 742 | #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 Rullgard | cc5d04d | 2019-03-01 18:48:16 +0000 | [diff] [blame] | 748 | #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 Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 756 | /* initialize the LCD driver */ |
| 757 | static int charlcd_init(struct charlcd *lcd) |
| 758 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 759 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 760 | 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 Rullgard | cc5d04d | 2019-03-01 18:48:16 +0000 | [diff] [blame] | 777 | charlcd_puts(lcd, "\x1b[Lc\x1b[Lb" LCD_INIT_BL LCD_INIT_TEXT); |
Mans Rullgard | c917172 | 2019-03-01 18:48:15 +0000 | [diff] [blame] | 778 | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 779 | /* clear the display on the next device opening */ |
| 780 | priv->must_clear = true; |
| 781 | charlcd_home(lcd); |
| 782 | return 0; |
| 783 | } |
| 784 | |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 785 | struct charlcd *charlcd_alloc(void) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 786 | { |
| 787 | struct charlcd_priv *priv; |
| 788 | struct charlcd *lcd; |
| 789 | |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame^] | 790 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 791 | if (!priv) |
| 792 | return NULL; |
| 793 | |
| 794 | priv->esc_seq.len = -1; |
| 795 | |
| 796 | lcd = &priv->lcd; |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 797 | lcd->ifwidth = 8; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 798 | |
| 799 | return lcd; |
| 800 | } |
| 801 | EXPORT_SYMBOL_GPL(charlcd_alloc); |
| 802 | |
Andy Shevchenko | 8e44fc8 | 2019-03-12 16:44:30 +0200 | [diff] [blame] | 803 | void charlcd_free(struct charlcd *lcd) |
| 804 | { |
| 805 | kfree(charlcd_to_priv(lcd)); |
| 806 | } |
| 807 | EXPORT_SYMBOL_GPL(charlcd_free); |
| 808 | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 809 | static 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 | |
| 831 | static struct notifier_block panel_notifier = { |
| 832 | panel_notify_sys, |
| 833 | NULL, |
| 834 | 0 |
| 835 | }; |
| 836 | |
| 837 | int 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 | } |
| 853 | EXPORT_SYMBOL_GPL(charlcd_register); |
| 854 | |
| 855 | int charlcd_unregister(struct charlcd *lcd) |
| 856 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 857 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 858 | |
| 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 | } |
| 870 | EXPORT_SYMBOL_GPL(charlcd_unregister); |
| 871 | |
| 872 | MODULE_LICENSE("GPL"); |