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