1 // SPDX-License-Identifier: GPL-2.0-or-later 2 #include <linux/module.h> 3 #include <linux/sched.h> 4 #include <linux/slab.h> 5 6 #include "charlcd.h" 7 #include "hd44780_common.h" 8 9 /* LCD commands */ 10 #define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */ 11 12 #define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */ 13 #define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */ 14 15 #define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */ 16 #define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */ 17 #define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */ 18 #define LCD_CMD_BLINK_ON 0x01 /* Set blink on */ 19 20 #define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */ 21 #define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */ 22 #define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */ 23 24 #define LCD_CMD_FUNCTION_SET 0x20 /* Set function */ 25 #define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */ 26 #define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */ 27 #define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */ 28 29 #define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */ 30 31 #define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */ 32 33 /* sleeps that many milliseconds with a reschedule */ 34 static void long_sleep(int ms) 35 { 36 schedule_timeout_interruptible(msecs_to_jiffies(ms)); 37 } 38 39 int hd44780_common_print(struct charlcd *lcd, int c) 40 { 41 struct hd44780_common *hdc = lcd->drvdata; 42 43 if (lcd->addr.x < hdc->bwidth) { 44 hdc->write_data(hdc, c); 45 return 0; 46 } 47 48 return 1; 49 } 50 EXPORT_SYMBOL_GPL(hd44780_common_print); 51 52 int hd44780_common_gotoxy(struct charlcd *lcd) 53 { 54 struct hd44780_common *hdc = lcd->drvdata; 55 unsigned int addr; 56 57 /* 58 * we force the cursor to stay at the end of the 59 * line if it wants to go farther 60 */ 61 addr = lcd->addr.x < hdc->bwidth ? lcd->addr.x & (hdc->hwidth - 1) 62 : hdc->bwidth - 1; 63 if (lcd->addr.y & 1) 64 addr += hdc->hwidth; 65 if (lcd->addr.y & 2) 66 addr += hdc->bwidth; 67 hdc->write_cmd(hdc, LCD_CMD_SET_DDRAM_ADDR | addr); 68 return 0; 69 } 70 EXPORT_SYMBOL_GPL(hd44780_common_gotoxy); 71 72 int hd44780_common_home(struct charlcd *lcd) 73 { 74 lcd->addr.x = 0; 75 lcd->addr.y = 0; 76 return hd44780_common_gotoxy(lcd); 77 } 78 EXPORT_SYMBOL_GPL(hd44780_common_home); 79 80 /* clears the display and resets X/Y */ 81 int hd44780_common_clear_display(struct charlcd *lcd) 82 { 83 struct hd44780_common *hdc = lcd->drvdata; 84 85 hdc->write_cmd(hdc, LCD_CMD_DISPLAY_CLEAR); 86 /* we must wait a few milliseconds (15) */ 87 long_sleep(15); 88 return 0; 89 } 90 EXPORT_SYMBOL_GPL(hd44780_common_clear_display); 91 92 int hd44780_common_init_display(struct charlcd *lcd) 93 { 94 struct hd44780_common *hdc = lcd->drvdata; 95 96 void (*write_cmd_raw)(struct hd44780_common *hdc, int cmd); 97 u8 init; 98 99 if (hdc->ifwidth != 4 && hdc->ifwidth != 8) 100 return -EINVAL; 101 102 hdc->hd44780_common_flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | 103 LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B; 104 105 long_sleep(20); /* wait 20 ms after power-up for the paranoid */ 106 107 /* 108 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure 109 * the LCD is in 8-bit mode afterwards 110 */ 111 init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS; 112 if (hdc->ifwidth == 4) { 113 init >>= 4; 114 write_cmd_raw = hdc->write_cmd_raw4; 115 } else { 116 write_cmd_raw = hdc->write_cmd; 117 } 118 write_cmd_raw(hdc, init); 119 long_sleep(10); 120 write_cmd_raw(hdc, init); 121 long_sleep(10); 122 write_cmd_raw(hdc, init); 123 long_sleep(10); 124 125 if (hdc->ifwidth == 4) { 126 /* Switch to 4-bit mode, 1 line, small fonts */ 127 hdc->write_cmd_raw4(hdc, LCD_CMD_FUNCTION_SET >> 4); 128 long_sleep(10); 129 } 130 131 /* set font height and lines number */ 132 hdc->write_cmd(hdc, 133 LCD_CMD_FUNCTION_SET | 134 ((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) | 135 ((hdc->hd44780_common_flags & LCD_FLAG_F) ? 136 LCD_CMD_FONT_5X10_DOTS : 0) | 137 ((hdc->hd44780_common_flags & LCD_FLAG_N) ? 138 LCD_CMD_TWO_LINES : 0)); 139 long_sleep(10); 140 141 /* display off, cursor off, blink off */ 142 hdc->write_cmd(hdc, LCD_CMD_DISPLAY_CTRL); 143 long_sleep(10); 144 145 hdc->write_cmd(hdc, 146 LCD_CMD_DISPLAY_CTRL | /* set display mode */ 147 ((hdc->hd44780_common_flags & LCD_FLAG_D) ? 148 LCD_CMD_DISPLAY_ON : 0) | 149 ((hdc->hd44780_common_flags & LCD_FLAG_C) ? 150 LCD_CMD_CURSOR_ON : 0) | 151 ((hdc->hd44780_common_flags & LCD_FLAG_B) ? 152 LCD_CMD_BLINK_ON : 0)); 153 154 charlcd_backlight(lcd, 155 (hdc->hd44780_common_flags & LCD_FLAG_L) ? 1 : 0); 156 157 long_sleep(10); 158 159 /* entry mode set : increment, cursor shifting */ 160 hdc->write_cmd(hdc, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC); 161 162 hd44780_common_clear_display(lcd); 163 return 0; 164 } 165 EXPORT_SYMBOL_GPL(hd44780_common_init_display); 166 167 int hd44780_common_shift_cursor(struct charlcd *lcd, enum charlcd_shift_dir dir) 168 { 169 struct hd44780_common *hdc = lcd->drvdata; 170 171 if (dir == CHARLCD_SHIFT_LEFT) { 172 /* back one char if not at end of line */ 173 if (lcd->addr.x < hdc->bwidth) 174 hdc->write_cmd(hdc, LCD_CMD_SHIFT); 175 } else if (dir == CHARLCD_SHIFT_RIGHT) { 176 /* allow the cursor to pass the end of the line */ 177 if (lcd->addr.x < (hdc->bwidth - 1)) 178 hdc->write_cmd(hdc, 179 LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT); 180 } 181 182 return 0; 183 } 184 EXPORT_SYMBOL_GPL(hd44780_common_shift_cursor); 185 186 int hd44780_common_shift_display(struct charlcd *lcd, 187 enum charlcd_shift_dir dir) 188 { 189 struct hd44780_common *hdc = lcd->drvdata; 190 191 if (dir == CHARLCD_SHIFT_LEFT) 192 hdc->write_cmd(hdc, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT); 193 else if (dir == CHARLCD_SHIFT_RIGHT) 194 hdc->write_cmd(hdc, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT | 195 LCD_CMD_SHIFT_RIGHT); 196 197 return 0; 198 } 199 EXPORT_SYMBOL_GPL(hd44780_common_shift_display); 200 201 static void hd44780_common_set_mode(struct hd44780_common *hdc) 202 { 203 hdc->write_cmd(hdc, 204 LCD_CMD_DISPLAY_CTRL | 205 ((hdc->hd44780_common_flags & LCD_FLAG_D) ? 206 LCD_CMD_DISPLAY_ON : 0) | 207 ((hdc->hd44780_common_flags & LCD_FLAG_C) ? 208 LCD_CMD_CURSOR_ON : 0) | 209 ((hdc->hd44780_common_flags & LCD_FLAG_B) ? 210 LCD_CMD_BLINK_ON : 0)); 211 } 212 213 int hd44780_common_display(struct charlcd *lcd, enum charlcd_onoff on) 214 { 215 struct hd44780_common *hdc = lcd->drvdata; 216 217 if (on == CHARLCD_ON) 218 hdc->hd44780_common_flags |= LCD_FLAG_D; 219 else 220 hdc->hd44780_common_flags &= ~LCD_FLAG_D; 221 222 hd44780_common_set_mode(hdc); 223 return 0; 224 } 225 EXPORT_SYMBOL_GPL(hd44780_common_display); 226 227 int hd44780_common_cursor(struct charlcd *lcd, enum charlcd_onoff on) 228 { 229 struct hd44780_common *hdc = lcd->drvdata; 230 231 if (on == CHARLCD_ON) 232 hdc->hd44780_common_flags |= LCD_FLAG_C; 233 else 234 hdc->hd44780_common_flags &= ~LCD_FLAG_C; 235 236 hd44780_common_set_mode(hdc); 237 return 0; 238 } 239 EXPORT_SYMBOL_GPL(hd44780_common_cursor); 240 241 int hd44780_common_blink(struct charlcd *lcd, enum charlcd_onoff on) 242 { 243 struct hd44780_common *hdc = lcd->drvdata; 244 245 if (on == CHARLCD_ON) 246 hdc->hd44780_common_flags |= LCD_FLAG_B; 247 else 248 hdc->hd44780_common_flags &= ~LCD_FLAG_B; 249 250 hd44780_common_set_mode(hdc); 251 return 0; 252 } 253 EXPORT_SYMBOL_GPL(hd44780_common_blink); 254 255 static void hd44780_common_set_function(struct hd44780_common *hdc) 256 { 257 hdc->write_cmd(hdc, 258 LCD_CMD_FUNCTION_SET | 259 ((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) | 260 ((hdc->hd44780_common_flags & LCD_FLAG_F) ? 261 LCD_CMD_FONT_5X10_DOTS : 0) | 262 ((hdc->hd44780_common_flags & LCD_FLAG_N) ? 263 LCD_CMD_TWO_LINES : 0)); 264 } 265 266 int hd44780_common_fontsize(struct charlcd *lcd, enum charlcd_fontsize size) 267 { 268 struct hd44780_common *hdc = lcd->drvdata; 269 270 if (size == CHARLCD_FONTSIZE_LARGE) 271 hdc->hd44780_common_flags |= LCD_FLAG_F; 272 else 273 hdc->hd44780_common_flags &= ~LCD_FLAG_F; 274 275 hd44780_common_set_function(hdc); 276 return 0; 277 } 278 EXPORT_SYMBOL_GPL(hd44780_common_fontsize); 279 280 int hd44780_common_lines(struct charlcd *lcd, enum charlcd_lines lines) 281 { 282 struct hd44780_common *hdc = lcd->drvdata; 283 284 if (lines == CHARLCD_LINES_2) 285 hdc->hd44780_common_flags |= LCD_FLAG_N; 286 else 287 hdc->hd44780_common_flags &= ~LCD_FLAG_N; 288 289 hd44780_common_set_function(hdc); 290 return 0; 291 } 292 EXPORT_SYMBOL_GPL(hd44780_common_lines); 293 294 int hd44780_common_redefine_char(struct charlcd *lcd, char *esc) 295 { 296 /* Generator : LGcxxxxx...xx; must have <c> between '0' 297 * and '7', representing the numerical ASCII code of the 298 * redefined character, and <xx...xx> a sequence of 16 299 * hex digits representing 8 bytes for each character. 300 * Most LCDs will only use 5 lower bits of the 7 first 301 * bytes. 302 */ 303 304 struct hd44780_common *hdc = lcd->drvdata; 305 unsigned char cgbytes[8]; 306 unsigned char cgaddr; 307 int cgoffset; 308 int shift; 309 char value; 310 int addr; 311 312 if (!strchr(esc, ';')) 313 return 0; 314 315 esc++; 316 317 cgaddr = *(esc++) - '0'; 318 if (cgaddr > 7) 319 return 1; 320 321 cgoffset = 0; 322 shift = 0; 323 value = 0; 324 while (*esc && cgoffset < 8) { 325 int half; 326 327 shift ^= 4; 328 half = hex_to_bin(*esc++); 329 if (half < 0) 330 continue; 331 332 value |= half << shift; 333 if (shift == 0) { 334 cgbytes[cgoffset++] = value; 335 value = 0; 336 } 337 } 338 339 hdc->write_cmd(hdc, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8)); 340 for (addr = 0; addr < cgoffset; addr++) 341 hdc->write_data(hdc, cgbytes[addr]); 342 343 /* ensures that we stop writing to CGRAM */ 344 lcd->ops->gotoxy(lcd); 345 return 1; 346 } 347 EXPORT_SYMBOL_GPL(hd44780_common_redefine_char); 348 349 struct hd44780_common *hd44780_common_alloc(void) 350 { 351 struct hd44780_common *hd; 352 353 hd = kzalloc(sizeof(*hd), GFP_KERNEL); 354 if (!hd) 355 return NULL; 356 357 hd->ifwidth = 8; 358 hd->bwidth = DEFAULT_LCD_BWIDTH; 359 hd->hwidth = DEFAULT_LCD_HWIDTH; 360 return hd; 361 } 362 EXPORT_SYMBOL_GPL(hd44780_common_alloc); 363 364 MODULE_LICENSE("GPL"); 365