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