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; |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 165 | hdc->write_cmd(hdc, 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]; |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 185 | hdc->write_data(hdc, c); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 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++) |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 205 | hdc->write_data(hdc, ' '); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 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); |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 214 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 215 | |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 216 | hdc->write_cmd(hdc, LCD_CMD_DISPLAY_CLEAR); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 217 | priv->addr.x = 0; |
| 218 | priv->addr.y = 0; |
| 219 | /* we must wait a few milliseconds (15) */ |
| 220 | long_sleep(15); |
| 221 | } |
| 222 | |
| 223 | static int charlcd_init_display(struct charlcd *lcd) |
| 224 | { |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 225 | void (*write_cmd_raw)(struct hd44780_common *hdc, int cmd); |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 226 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Lars Poeschel | 3fc04dd | 2020-11-03 10:58:07 +0100 | [diff] [blame] | 227 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 228 | u8 init; |
| 229 | |
Lars Poeschel | 3fc04dd | 2020-11-03 10:58:07 +0100 | [diff] [blame] | 230 | if (hdc->ifwidth != 4 && hdc->ifwidth != 8) |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 231 | return -EINVAL; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 232 | |
| 233 | priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D | |
| 234 | LCD_FLAG_C | LCD_FLAG_B; |
| 235 | |
| 236 | long_sleep(20); /* wait 20 ms after power-up for the paranoid */ |
| 237 | |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 238 | /* |
| 239 | * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure |
| 240 | * the LCD is in 8-bit mode afterwards |
| 241 | */ |
| 242 | init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS; |
Lars Poeschel | 3fc04dd | 2020-11-03 10:58:07 +0100 | [diff] [blame] | 243 | if (hdc->ifwidth == 4) { |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 244 | init >>= 4; |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 245 | write_cmd_raw = hdc->write_cmd_raw4; |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 246 | } else { |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 247 | write_cmd_raw = hdc->write_cmd; |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 248 | } |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 249 | write_cmd_raw(hdc, init); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 250 | long_sleep(10); |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 251 | write_cmd_raw(hdc, init); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 252 | long_sleep(10); |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 253 | write_cmd_raw(hdc, init); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 254 | long_sleep(10); |
| 255 | |
Lars Poeschel | 3fc04dd | 2020-11-03 10:58:07 +0100 | [diff] [blame] | 256 | if (hdc->ifwidth == 4) { |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 257 | /* Switch to 4-bit mode, 1 line, small fonts */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 258 | hdc->write_cmd_raw4(hdc, LCD_CMD_FUNCTION_SET >> 4); |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 259 | long_sleep(10); |
| 260 | } |
| 261 | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 262 | /* set font height and lines number */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 263 | hdc->write_cmd(hdc, |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 264 | LCD_CMD_FUNCTION_SET | |
Lars Poeschel | 3fc04dd | 2020-11-03 10:58:07 +0100 | [diff] [blame] | 265 | ((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 266 | ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) | |
| 267 | ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)); |
| 268 | long_sleep(10); |
| 269 | |
| 270 | /* display off, cursor off, blink off */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 271 | hdc->write_cmd(hdc, LCD_CMD_DISPLAY_CTRL); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 272 | long_sleep(10); |
| 273 | |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 274 | hdc->write_cmd(hdc, |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 275 | LCD_CMD_DISPLAY_CTRL | /* set display mode */ |
| 276 | ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) | |
| 277 | ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) | |
| 278 | ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)); |
| 279 | |
| 280 | charlcd_backlight(lcd, (priv->flags & LCD_FLAG_L) ? 1 : 0); |
| 281 | |
| 282 | long_sleep(10); |
| 283 | |
| 284 | /* entry mode set : increment, cursor shifting */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 285 | hdc->write_cmd(hdc, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 286 | |
| 287 | charlcd_clear_display(lcd); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 292 | * Parses a movement command of the form "(.*);", where the group can be |
| 293 | * any number of subcommands of the form "(x|y)[0-9]+". |
| 294 | * |
| 295 | * Returns whether the command is valid. The position arguments are |
| 296 | * only written if the parsing was successful. |
| 297 | * |
| 298 | * For instance: |
| 299 | * - ";" returns (<original x>, <original y>). |
| 300 | * - "x1;" returns (1, <original y>). |
| 301 | * - "y2x1;" returns (1, 2). |
| 302 | * - "x12y34x56;" returns (56, 34). |
| 303 | * - "" fails. |
| 304 | * - "x" fails. |
| 305 | * - "x;" fails. |
| 306 | * - "x1" fails. |
| 307 | * - "xy12;" fails. |
| 308 | * - "x12yy12;" fails. |
| 309 | * - "xx" fails. |
| 310 | */ |
| 311 | static bool parse_xy(const char *s, unsigned long *x, unsigned long *y) |
| 312 | { |
| 313 | unsigned long new_x = *x; |
| 314 | unsigned long new_y = *y; |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 315 | char *p; |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 316 | |
| 317 | for (;;) { |
| 318 | if (!*s) |
| 319 | return false; |
| 320 | |
| 321 | if (*s == ';') |
| 322 | break; |
| 323 | |
| 324 | if (*s == 'x') { |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 325 | new_x = simple_strtoul(s + 1, &p, 10); |
| 326 | if (p == s + 1) |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 327 | return false; |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 328 | s = p; |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 329 | } else if (*s == 'y') { |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 330 | new_y = simple_strtoul(s + 1, &p, 10); |
| 331 | if (p == s + 1) |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 332 | return false; |
Andy Shevchenko | d717e7d | 2019-12-04 16:50:36 -0800 | [diff] [blame] | 333 | s = p; |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 334 | } else { |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | *x = new_x; |
| 340 | *y = new_y; |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | /* |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 345 | * These are the file operation function for user access to /dev/lcd |
| 346 | * This function can also be called from inside the kernel, by |
| 347 | * setting file and ppos to NULL. |
| 348 | * |
| 349 | */ |
| 350 | |
| 351 | static inline int handle_lcd_special_code(struct charlcd *lcd) |
| 352 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 353 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 354 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 355 | |
| 356 | /* LCD special codes */ |
| 357 | |
| 358 | int processed = 0; |
| 359 | |
| 360 | char *esc = priv->esc_seq.buf + 2; |
| 361 | int oldflags = priv->flags; |
| 362 | |
| 363 | /* check for display mode flags */ |
| 364 | switch (*esc) { |
| 365 | case 'D': /* Display ON */ |
| 366 | priv->flags |= LCD_FLAG_D; |
| 367 | processed = 1; |
| 368 | break; |
| 369 | case 'd': /* Display OFF */ |
| 370 | priv->flags &= ~LCD_FLAG_D; |
| 371 | processed = 1; |
| 372 | break; |
| 373 | case 'C': /* Cursor ON */ |
| 374 | priv->flags |= LCD_FLAG_C; |
| 375 | processed = 1; |
| 376 | break; |
| 377 | case 'c': /* Cursor OFF */ |
| 378 | priv->flags &= ~LCD_FLAG_C; |
| 379 | processed = 1; |
| 380 | break; |
| 381 | case 'B': /* Blink ON */ |
| 382 | priv->flags |= LCD_FLAG_B; |
| 383 | processed = 1; |
| 384 | break; |
| 385 | case 'b': /* Blink OFF */ |
| 386 | priv->flags &= ~LCD_FLAG_B; |
| 387 | processed = 1; |
| 388 | break; |
| 389 | case '+': /* Back light ON */ |
| 390 | priv->flags |= LCD_FLAG_L; |
| 391 | processed = 1; |
| 392 | break; |
| 393 | case '-': /* Back light OFF */ |
| 394 | priv->flags &= ~LCD_FLAG_L; |
| 395 | processed = 1; |
| 396 | break; |
| 397 | case '*': /* Flash back light */ |
| 398 | charlcd_poke(lcd); |
| 399 | processed = 1; |
| 400 | break; |
| 401 | case 'f': /* Small Font */ |
| 402 | priv->flags &= ~LCD_FLAG_F; |
| 403 | processed = 1; |
| 404 | break; |
| 405 | case 'F': /* Large Font */ |
| 406 | priv->flags |= LCD_FLAG_F; |
| 407 | processed = 1; |
| 408 | break; |
| 409 | case 'n': /* One Line */ |
| 410 | priv->flags &= ~LCD_FLAG_N; |
| 411 | processed = 1; |
| 412 | break; |
| 413 | case 'N': /* Two Lines */ |
| 414 | priv->flags |= LCD_FLAG_N; |
Robert Abel | 99b9b49 | 2018-02-26 00:54:29 +0100 | [diff] [blame] | 415 | processed = 1; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 416 | break; |
| 417 | case 'l': /* Shift Cursor Left */ |
| 418 | if (priv->addr.x > 0) { |
| 419 | /* back one char if not at end of line */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 420 | if (priv->addr.x < hdc->bwidth) |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 421 | hdc->write_cmd(hdc, LCD_CMD_SHIFT); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 422 | priv->addr.x--; |
| 423 | } |
| 424 | processed = 1; |
| 425 | break; |
| 426 | case 'r': /* shift cursor right */ |
| 427 | if (priv->addr.x < lcd->width) { |
| 428 | /* allow the cursor to pass the end of the line */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 429 | if (priv->addr.x < (hdc->bwidth - 1)) |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 430 | hdc->write_cmd(hdc, |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 431 | LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT); |
| 432 | priv->addr.x++; |
| 433 | } |
| 434 | processed = 1; |
| 435 | break; |
| 436 | case 'L': /* shift display left */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 437 | hdc->write_cmd(hdc, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 438 | processed = 1; |
| 439 | break; |
| 440 | case 'R': /* shift display right */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 441 | hdc->write_cmd(hdc, |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 442 | LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT | |
| 443 | LCD_CMD_SHIFT_RIGHT); |
| 444 | processed = 1; |
| 445 | break; |
| 446 | case 'k': { /* kill end of line */ |
| 447 | int x; |
| 448 | |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 449 | for (x = priv->addr.x; x < hdc->bwidth; x++) |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 450 | hdc->write_data(hdc, ' '); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 451 | |
| 452 | /* restore cursor position */ |
| 453 | charlcd_gotoxy(lcd); |
| 454 | processed = 1; |
| 455 | break; |
| 456 | } |
| 457 | case 'I': /* reinitialize display */ |
| 458 | charlcd_init_display(lcd); |
| 459 | processed = 1; |
| 460 | break; |
| 461 | case 'G': { |
| 462 | /* Generator : LGcxxxxx...xx; must have <c> between '0' |
| 463 | * and '7', representing the numerical ASCII code of the |
| 464 | * redefined character, and <xx...xx> a sequence of 16 |
| 465 | * hex digits representing 8 bytes for each character. |
| 466 | * Most LCDs will only use 5 lower bits of the 7 first |
| 467 | * bytes. |
| 468 | */ |
| 469 | |
| 470 | unsigned char cgbytes[8]; |
| 471 | unsigned char cgaddr; |
| 472 | int cgoffset; |
| 473 | int shift; |
| 474 | char value; |
| 475 | int addr; |
| 476 | |
| 477 | if (!strchr(esc, ';')) |
| 478 | break; |
| 479 | |
| 480 | esc++; |
| 481 | |
| 482 | cgaddr = *(esc++) - '0'; |
| 483 | if (cgaddr > 7) { |
| 484 | processed = 1; |
| 485 | break; |
| 486 | } |
| 487 | |
| 488 | cgoffset = 0; |
| 489 | shift = 0; |
| 490 | value = 0; |
| 491 | while (*esc && cgoffset < 8) { |
Andy Shevchenko | 3f03b64 | 2020-05-18 22:36:17 +0300 | [diff] [blame] | 492 | int half; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 493 | |
Andy Shevchenko | 3f03b64 | 2020-05-18 22:36:17 +0300 | [diff] [blame] | 494 | shift ^= 4; |
| 495 | |
| 496 | half = hex_to_bin(*esc++); |
| 497 | if (half < 0) |
| 498 | continue; |
| 499 | |
| 500 | value |= half << shift; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 501 | if (shift == 0) { |
| 502 | cgbytes[cgoffset++] = value; |
| 503 | value = 0; |
| 504 | } |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 505 | } |
| 506 | |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 507 | hdc->write_cmd(hdc, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8)); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 508 | for (addr = 0; addr < cgoffset; addr++) |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 509 | hdc->write_data(hdc, cgbytes[addr]); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 510 | |
| 511 | /* ensures that we stop writing to CGRAM */ |
| 512 | charlcd_gotoxy(lcd); |
| 513 | processed = 1; |
| 514 | break; |
| 515 | } |
| 516 | case 'x': /* gotoxy : LxXXX[yYYY]; */ |
| 517 | case 'y': /* gotoxy : LyYYY[xXXX]; */ |
Mans Rullgard | 9bc30ab | 2018-12-05 13:52:47 +0000 | [diff] [blame] | 518 | if (priv->esc_seq.buf[priv->esc_seq.len - 1] != ';') |
| 519 | break; |
| 520 | |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 521 | /* If the command is valid, move to the new address */ |
| 522 | if (parse_xy(esc, &priv->addr.x, &priv->addr.y)) |
| 523 | charlcd_gotoxy(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 524 | |
Miguel Ojeda | b34050f | 2018-02-27 23:09:52 +0100 | [diff] [blame] | 525 | /* Regardless of its validity, mark as processed */ |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 526 | processed = 1; |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | /* TODO: This indent party here got ugly, clean it! */ |
| 531 | /* Check whether one flag was changed */ |
| 532 | if (oldflags == priv->flags) |
| 533 | return processed; |
| 534 | |
| 535 | /* check whether one of B,C,D flags were changed */ |
| 536 | if ((oldflags ^ priv->flags) & |
| 537 | (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D)) |
| 538 | /* set display mode */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 539 | hdc->write_cmd(hdc, |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 540 | LCD_CMD_DISPLAY_CTRL | |
| 541 | ((priv->flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) | |
| 542 | ((priv->flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) | |
| 543 | ((priv->flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)); |
| 544 | /* check whether one of F,N flags was changed */ |
| 545 | else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N)) |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 546 | hdc->write_cmd(hdc, |
Geert Uytterhoeven | ac20147 | 2017-03-10 15:15:18 +0100 | [diff] [blame] | 547 | LCD_CMD_FUNCTION_SET | |
Lars Poeschel | 3fc04dd | 2020-11-03 10:58:07 +0100 | [diff] [blame] | 548 | ((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 549 | ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) | |
| 550 | ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)); |
| 551 | /* check whether L flag was changed */ |
| 552 | else if ((oldflags ^ priv->flags) & LCD_FLAG_L) |
| 553 | charlcd_backlight(lcd, !!(priv->flags & LCD_FLAG_L)); |
| 554 | |
| 555 | return processed; |
| 556 | } |
| 557 | |
| 558 | static void charlcd_write_char(struct charlcd *lcd, char c) |
| 559 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 560 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 561 | struct hd44780_common *hdc = lcd->drvdata; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 562 | |
| 563 | /* first, we'll test if we're in escape mode */ |
| 564 | if ((c != '\n') && priv->esc_seq.len >= 0) { |
| 565 | /* yes, let's add this char to the buffer */ |
| 566 | priv->esc_seq.buf[priv->esc_seq.len++] = c; |
Robert Abel | 8c48375 | 2018-02-10 00:50:11 +0100 | [diff] [blame] | 567 | priv->esc_seq.buf[priv->esc_seq.len] = '\0'; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 568 | } else { |
| 569 | /* aborts any previous escape sequence */ |
| 570 | priv->esc_seq.len = -1; |
| 571 | |
| 572 | switch (c) { |
| 573 | case LCD_ESCAPE_CHAR: |
| 574 | /* start of an escape sequence */ |
| 575 | priv->esc_seq.len = 0; |
Robert Abel | 8c48375 | 2018-02-10 00:50:11 +0100 | [diff] [blame] | 576 | priv->esc_seq.buf[priv->esc_seq.len] = '\0'; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 577 | break; |
| 578 | case '\b': |
| 579 | /* go back one char and clear it */ |
| 580 | if (priv->addr.x > 0) { |
| 581 | /* |
| 582 | * check if we're not at the |
| 583 | * end of the line |
| 584 | */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 585 | if (priv->addr.x < hdc->bwidth) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 586 | /* back one char */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 587 | hdc->write_cmd(hdc, LCD_CMD_SHIFT); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 588 | priv->addr.x--; |
| 589 | } |
| 590 | /* replace with a space */ |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 591 | hdc->write_data(hdc, ' '); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 592 | /* back one char again */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame^] | 593 | hdc->write_cmd(hdc, LCD_CMD_SHIFT); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 594 | break; |
Robert Abel | 9629ccc | 2018-02-10 00:50:12 +0100 | [diff] [blame] | 595 | case '\f': |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 596 | /* quickly clear the display */ |
| 597 | charlcd_clear_fast(lcd); |
| 598 | break; |
| 599 | case '\n': |
| 600 | /* |
| 601 | * flush the remainder of the current line and |
| 602 | * go to the beginning of the next line |
| 603 | */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 604 | for (; priv->addr.x < hdc->bwidth; priv->addr.x++) |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 605 | hdc->write_data(hdc, ' '); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 606 | priv->addr.x = 0; |
| 607 | priv->addr.y = (priv->addr.y + 1) % lcd->height; |
| 608 | charlcd_gotoxy(lcd); |
| 609 | break; |
| 610 | case '\r': |
| 611 | /* go to the beginning of the same line */ |
| 612 | priv->addr.x = 0; |
| 613 | charlcd_gotoxy(lcd); |
| 614 | break; |
| 615 | case '\t': |
| 616 | /* print a space instead of the tab */ |
| 617 | charlcd_print(lcd, ' '); |
| 618 | break; |
| 619 | default: |
| 620 | /* simply print this char */ |
| 621 | charlcd_print(lcd, c); |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * now we'll see if we're in an escape mode and if the current |
| 628 | * escape sequence can be understood. |
| 629 | */ |
| 630 | if (priv->esc_seq.len >= 2) { |
| 631 | int processed = 0; |
| 632 | |
| 633 | if (!strcmp(priv->esc_seq.buf, "[2J")) { |
| 634 | /* clear the display */ |
| 635 | charlcd_clear_fast(lcd); |
| 636 | processed = 1; |
| 637 | } else if (!strcmp(priv->esc_seq.buf, "[H")) { |
| 638 | /* cursor to home */ |
| 639 | charlcd_home(lcd); |
| 640 | processed = 1; |
| 641 | } |
| 642 | /* codes starting with ^[[L */ |
| 643 | else if ((priv->esc_seq.len >= 3) && |
| 644 | (priv->esc_seq.buf[0] == '[') && |
| 645 | (priv->esc_seq.buf[1] == 'L')) { |
| 646 | processed = handle_lcd_special_code(lcd); |
| 647 | } |
| 648 | |
| 649 | /* LCD special escape codes */ |
| 650 | /* |
| 651 | * flush the escape sequence if it's been processed |
| 652 | * or if it is getting too long. |
| 653 | */ |
| 654 | if (processed || (priv->esc_seq.len >= LCD_ESCAPE_LEN)) |
| 655 | priv->esc_seq.len = -1; |
| 656 | } /* escape codes */ |
| 657 | } |
| 658 | |
| 659 | static struct charlcd *the_charlcd; |
| 660 | |
| 661 | static ssize_t charlcd_write(struct file *file, const char __user *buf, |
| 662 | size_t count, loff_t *ppos) |
| 663 | { |
| 664 | const char __user *tmp = buf; |
| 665 | char c; |
| 666 | |
| 667 | for (; count-- > 0; (*ppos)++, tmp++) { |
| 668 | if (!in_interrupt() && (((count + 1) & 0x1f) == 0)) |
| 669 | /* |
| 670 | * let's be a little nice with other processes |
| 671 | * that need some CPU |
| 672 | */ |
| 673 | schedule(); |
| 674 | |
| 675 | if (get_user(c, tmp)) |
| 676 | return -EFAULT; |
| 677 | |
| 678 | charlcd_write_char(the_charlcd, c); |
| 679 | } |
| 680 | |
| 681 | return tmp - buf; |
| 682 | } |
| 683 | |
| 684 | static int charlcd_open(struct inode *inode, struct file *file) |
| 685 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 686 | struct charlcd_priv *priv = charlcd_to_priv(the_charlcd); |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 687 | int ret; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 688 | |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 689 | ret = -EBUSY; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 690 | if (!atomic_dec_and_test(&charlcd_available)) |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 691 | goto fail; /* open only once at a time */ |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 692 | |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 693 | ret = -EPERM; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 694 | if (file->f_mode & FMODE_READ) /* device is write-only */ |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 695 | goto fail; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 696 | |
| 697 | if (priv->must_clear) { |
| 698 | charlcd_clear_display(&priv->lcd); |
| 699 | priv->must_clear = false; |
| 700 | } |
| 701 | return nonseekable_open(inode, file); |
Willy Tarreau | 93dc177 | 2017-09-07 15:37:30 +0200 | [diff] [blame] | 702 | |
| 703 | fail: |
| 704 | atomic_inc(&charlcd_available); |
| 705 | return ret; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static int charlcd_release(struct inode *inode, struct file *file) |
| 709 | { |
| 710 | atomic_inc(&charlcd_available); |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | static const struct file_operations charlcd_fops = { |
| 715 | .write = charlcd_write, |
| 716 | .open = charlcd_open, |
| 717 | .release = charlcd_release, |
| 718 | .llseek = no_llseek, |
| 719 | }; |
| 720 | |
| 721 | static struct miscdevice charlcd_dev = { |
| 722 | .minor = LCD_MINOR, |
| 723 | .name = "lcd", |
| 724 | .fops = &charlcd_fops, |
| 725 | }; |
| 726 | |
| 727 | static void charlcd_puts(struct charlcd *lcd, const char *s) |
| 728 | { |
| 729 | const char *tmp = s; |
| 730 | int count = strlen(s); |
| 731 | |
| 732 | for (; count-- > 0; tmp++) { |
| 733 | if (!in_interrupt() && (((count + 1) & 0x1f) == 0)) |
| 734 | /* |
| 735 | * let's be a little nice with other processes |
| 736 | * that need some CPU |
| 737 | */ |
| 738 | schedule(); |
| 739 | |
| 740 | charlcd_write_char(lcd, *tmp); |
| 741 | } |
| 742 | } |
| 743 | |
Mans Rullgard | c917172 | 2019-03-01 18:48:15 +0000 | [diff] [blame] | 744 | #ifdef CONFIG_PANEL_BOOT_MESSAGE |
| 745 | #define LCD_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE |
| 746 | #else |
| 747 | #define LCD_INIT_TEXT "Linux-" UTS_RELEASE "\n" |
| 748 | #endif |
| 749 | |
Mans Rullgard | cc5d04d | 2019-03-01 18:48:16 +0000 | [diff] [blame] | 750 | #ifdef CONFIG_CHARLCD_BL_ON |
| 751 | #define LCD_INIT_BL "\x1b[L+" |
| 752 | #elif defined(CONFIG_CHARLCD_BL_FLASH) |
| 753 | #define LCD_INIT_BL "\x1b[L*" |
| 754 | #else |
| 755 | #define LCD_INIT_BL "\x1b[L-" |
| 756 | #endif |
| 757 | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 758 | /* initialize the LCD driver */ |
| 759 | static int charlcd_init(struct charlcd *lcd) |
| 760 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 761 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 762 | int ret; |
| 763 | |
| 764 | if (lcd->ops->backlight) { |
| 765 | mutex_init(&priv->bl_tempo_lock); |
| 766 | INIT_DELAYED_WORK(&priv->bl_work, charlcd_bl_off); |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * before this line, we must NOT send anything to the display. |
| 771 | * Since charlcd_init_display() needs to write data, we have to |
| 772 | * enable mark the LCD initialized just before. |
| 773 | */ |
| 774 | ret = charlcd_init_display(lcd); |
| 775 | if (ret) |
| 776 | return ret; |
| 777 | |
| 778 | /* display a short message */ |
Mans Rullgard | cc5d04d | 2019-03-01 18:48:16 +0000 | [diff] [blame] | 779 | 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] | 780 | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 781 | /* clear the display on the next device opening */ |
| 782 | priv->must_clear = true; |
| 783 | charlcd_home(lcd); |
| 784 | return 0; |
| 785 | } |
| 786 | |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 787 | struct charlcd *charlcd_alloc(void) |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 788 | { |
| 789 | struct charlcd_priv *priv; |
| 790 | struct charlcd *lcd; |
| 791 | |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 792 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 793 | if (!priv) |
| 794 | return NULL; |
| 795 | |
| 796 | priv->esc_seq.len = -1; |
| 797 | |
| 798 | lcd = &priv->lcd; |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 799 | |
| 800 | return lcd; |
| 801 | } |
| 802 | EXPORT_SYMBOL_GPL(charlcd_alloc); |
| 803 | |
Andy Shevchenko | 8e44fc8 | 2019-03-12 16:44:30 +0200 | [diff] [blame] | 804 | void charlcd_free(struct charlcd *lcd) |
| 805 | { |
| 806 | kfree(charlcd_to_priv(lcd)); |
| 807 | } |
| 808 | EXPORT_SYMBOL_GPL(charlcd_free); |
| 809 | |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 810 | static int panel_notify_sys(struct notifier_block *this, unsigned long code, |
| 811 | void *unused) |
| 812 | { |
| 813 | struct charlcd *lcd = the_charlcd; |
| 814 | |
| 815 | switch (code) { |
| 816 | case SYS_DOWN: |
| 817 | charlcd_puts(lcd, |
| 818 | "\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+"); |
| 819 | break; |
| 820 | case SYS_HALT: |
| 821 | charlcd_puts(lcd, "\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+"); |
| 822 | break; |
| 823 | case SYS_POWER_OFF: |
| 824 | charlcd_puts(lcd, "\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+"); |
| 825 | break; |
| 826 | default: |
| 827 | break; |
| 828 | } |
| 829 | return NOTIFY_DONE; |
| 830 | } |
| 831 | |
| 832 | static struct notifier_block panel_notifier = { |
| 833 | panel_notify_sys, |
| 834 | NULL, |
| 835 | 0 |
| 836 | }; |
| 837 | |
| 838 | int charlcd_register(struct charlcd *lcd) |
| 839 | { |
| 840 | int ret; |
| 841 | |
| 842 | ret = charlcd_init(lcd); |
| 843 | if (ret) |
| 844 | return ret; |
| 845 | |
| 846 | ret = misc_register(&charlcd_dev); |
| 847 | if (ret) |
| 848 | return ret; |
| 849 | |
| 850 | the_charlcd = lcd; |
| 851 | register_reboot_notifier(&panel_notifier); |
| 852 | return 0; |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(charlcd_register); |
| 855 | |
| 856 | int charlcd_unregister(struct charlcd *lcd) |
| 857 | { |
Andy Shevchenko | b658a21 | 2019-03-12 16:44:29 +0200 | [diff] [blame] | 858 | struct charlcd_priv *priv = charlcd_to_priv(lcd); |
Geert Uytterhoeven | 39f8ea4 | 2017-03-10 15:15:17 +0100 | [diff] [blame] | 859 | |
| 860 | unregister_reboot_notifier(&panel_notifier); |
| 861 | charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-"); |
| 862 | misc_deregister(&charlcd_dev); |
| 863 | the_charlcd = NULL; |
| 864 | if (lcd->ops->backlight) { |
| 865 | cancel_delayed_work_sync(&priv->bl_work); |
| 866 | priv->lcd.ops->backlight(&priv->lcd, 0); |
| 867 | } |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | EXPORT_SYMBOL_GPL(charlcd_unregister); |
| 872 | |
| 873 | MODULE_LICENSE("GPL"); |