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