1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/module.h>
3 #include <linux/sched.h>
4 #include <linux/slab.h>
7 #include "hd44780_common.h"
10 #define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
12 #define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
13 #define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
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 */
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 */
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 */
29 #define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
31 #define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
33 /* sleeps that many milliseconds with a reschedule */
34 static void long_sleep(int ms
)
36 schedule_timeout_interruptible(msecs_to_jiffies(ms
));
39 int hd44780_common_print(struct charlcd
*lcd
, int c
)
41 struct hd44780_common
*hdc
= lcd
->drvdata
;
43 if (lcd
->addr
.x
< hdc
->bwidth
) {
44 hdc
->write_data(hdc
, c
);
50 EXPORT_SYMBOL_GPL(hd44780_common_print
);
52 int hd44780_common_gotoxy(struct charlcd
*lcd
, unsigned int x
, unsigned int y
)
54 struct hd44780_common
*hdc
= lcd
->drvdata
;
58 * we force the cursor to stay at the end of the
59 * line if it wants to go farther
61 addr
= x
< hdc
->bwidth
? x
& (hdc
->hwidth
- 1) : hdc
->bwidth
- 1;
66 hdc
->write_cmd(hdc
, LCD_CMD_SET_DDRAM_ADDR
| addr
);
69 EXPORT_SYMBOL_GPL(hd44780_common_gotoxy
);
71 int hd44780_common_home(struct charlcd
*lcd
)
73 return hd44780_common_gotoxy(lcd
, 0, 0);
75 EXPORT_SYMBOL_GPL(hd44780_common_home
);
77 /* clears the display and resets X/Y */
78 int hd44780_common_clear_display(struct charlcd
*lcd
)
80 struct hd44780_common
*hdc
= lcd
->drvdata
;
82 hdc
->write_cmd(hdc
, LCD_CMD_DISPLAY_CLEAR
);
83 /* datasheet says to wait 1,64 milliseconds */
87 * The Hitachi HD44780 controller (and compatible ones) reset the DDRAM
88 * address when executing the DISPLAY_CLEAR command, thus the
89 * following call is not required. However, other controllers do not
90 * (e.g. NewHaven NHD-0220DZW-AG5), thus move the cursor to home
91 * unconditionally to support both.
93 return hd44780_common_home(lcd
);
95 EXPORT_SYMBOL_GPL(hd44780_common_clear_display
);
97 int hd44780_common_init_display(struct charlcd
*lcd
)
99 struct hd44780_common
*hdc
= lcd
->drvdata
;
101 void (*write_cmd_raw
)(struct hd44780_common
*hdc
, int cmd
);
104 if (hdc
->ifwidth
!= 4 && hdc
->ifwidth
!= 8)
107 hdc
->hd44780_common_flags
= ((lcd
->height
> 1) ? LCD_FLAG_N
: 0) |
108 LCD_FLAG_D
| LCD_FLAG_C
| LCD_FLAG_B
;
110 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
113 * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
114 * the LCD is in 8-bit mode afterwards
116 init
= LCD_CMD_FUNCTION_SET
| LCD_CMD_DATA_LEN_8BITS
;
117 if (hdc
->ifwidth
== 4) {
119 write_cmd_raw
= hdc
->write_cmd_raw4
;
121 write_cmd_raw
= hdc
->write_cmd
;
123 write_cmd_raw(hdc
, init
);
125 write_cmd_raw(hdc
, init
);
127 write_cmd_raw(hdc
, init
);
130 if (hdc
->ifwidth
== 4) {
131 /* Switch to 4-bit mode, 1 line, small fonts */
132 hdc
->write_cmd_raw4(hdc
, LCD_CMD_FUNCTION_SET
>> 4);
136 /* set font height and lines number */
138 LCD_CMD_FUNCTION_SET
|
139 ((hdc
->ifwidth
== 8) ? LCD_CMD_DATA_LEN_8BITS
: 0) |
140 ((hdc
->hd44780_common_flags
& LCD_FLAG_F
) ?
141 LCD_CMD_FONT_5X10_DOTS
: 0) |
142 ((hdc
->hd44780_common_flags
& LCD_FLAG_N
) ?
143 LCD_CMD_TWO_LINES
: 0));
146 /* display off, cursor off, blink off */
147 hdc
->write_cmd(hdc
, LCD_CMD_DISPLAY_CTRL
);
151 LCD_CMD_DISPLAY_CTRL
| /* set display mode */
152 ((hdc
->hd44780_common_flags
& LCD_FLAG_D
) ?
153 LCD_CMD_DISPLAY_ON
: 0) |
154 ((hdc
->hd44780_common_flags
& LCD_FLAG_C
) ?
155 LCD_CMD_CURSOR_ON
: 0) |
156 ((hdc
->hd44780_common_flags
& LCD_FLAG_B
) ?
157 LCD_CMD_BLINK_ON
: 0));
159 charlcd_backlight(lcd
,
160 (hdc
->hd44780_common_flags
& LCD_FLAG_L
) ? 1 : 0);
164 /* entry mode set : increment, cursor shifting */
165 hdc
->write_cmd(hdc
, LCD_CMD_ENTRY_MODE
| LCD_CMD_CURSOR_INC
);
167 hd44780_common_clear_display(lcd
);
170 EXPORT_SYMBOL_GPL(hd44780_common_init_display
);
172 int hd44780_common_shift_cursor(struct charlcd
*lcd
, enum charlcd_shift_dir dir
)
174 struct hd44780_common
*hdc
= lcd
->drvdata
;
176 if (dir
== CHARLCD_SHIFT_LEFT
) {
177 /* back one char if not at end of line */
178 if (lcd
->addr
.x
< hdc
->bwidth
)
179 hdc
->write_cmd(hdc
, LCD_CMD_SHIFT
);
180 } else if (dir
== CHARLCD_SHIFT_RIGHT
) {
181 /* allow the cursor to pass the end of the line */
182 if (lcd
->addr
.x
< (hdc
->bwidth
- 1))
184 LCD_CMD_SHIFT
| LCD_CMD_SHIFT_RIGHT
);
189 EXPORT_SYMBOL_GPL(hd44780_common_shift_cursor
);
191 int hd44780_common_shift_display(struct charlcd
*lcd
,
192 enum charlcd_shift_dir dir
)
194 struct hd44780_common
*hdc
= lcd
->drvdata
;
196 if (dir
== CHARLCD_SHIFT_LEFT
)
197 hdc
->write_cmd(hdc
, LCD_CMD_SHIFT
| LCD_CMD_DISPLAY_SHIFT
);
198 else if (dir
== CHARLCD_SHIFT_RIGHT
)
199 hdc
->write_cmd(hdc
, LCD_CMD_SHIFT
| LCD_CMD_DISPLAY_SHIFT
|
200 LCD_CMD_SHIFT_RIGHT
);
204 EXPORT_SYMBOL_GPL(hd44780_common_shift_display
);
206 static void hd44780_common_set_mode(struct hd44780_common
*hdc
)
209 LCD_CMD_DISPLAY_CTRL
|
210 ((hdc
->hd44780_common_flags
& LCD_FLAG_D
) ?
211 LCD_CMD_DISPLAY_ON
: 0) |
212 ((hdc
->hd44780_common_flags
& LCD_FLAG_C
) ?
213 LCD_CMD_CURSOR_ON
: 0) |
214 ((hdc
->hd44780_common_flags
& LCD_FLAG_B
) ?
215 LCD_CMD_BLINK_ON
: 0));
218 int hd44780_common_display(struct charlcd
*lcd
, enum charlcd_onoff on
)
220 struct hd44780_common
*hdc
= lcd
->drvdata
;
222 if (on
== CHARLCD_ON
)
223 hdc
->hd44780_common_flags
|= LCD_FLAG_D
;
225 hdc
->hd44780_common_flags
&= ~LCD_FLAG_D
;
227 hd44780_common_set_mode(hdc
);
230 EXPORT_SYMBOL_GPL(hd44780_common_display
);
232 int hd44780_common_cursor(struct charlcd
*lcd
, enum charlcd_onoff on
)
234 struct hd44780_common
*hdc
= lcd
->drvdata
;
236 if (on
== CHARLCD_ON
)
237 hdc
->hd44780_common_flags
|= LCD_FLAG_C
;
239 hdc
->hd44780_common_flags
&= ~LCD_FLAG_C
;
241 hd44780_common_set_mode(hdc
);
244 EXPORT_SYMBOL_GPL(hd44780_common_cursor
);
246 int hd44780_common_blink(struct charlcd
*lcd
, enum charlcd_onoff on
)
248 struct hd44780_common
*hdc
= lcd
->drvdata
;
250 if (on
== CHARLCD_ON
)
251 hdc
->hd44780_common_flags
|= LCD_FLAG_B
;
253 hdc
->hd44780_common_flags
&= ~LCD_FLAG_B
;
255 hd44780_common_set_mode(hdc
);
258 EXPORT_SYMBOL_GPL(hd44780_common_blink
);
260 static void hd44780_common_set_function(struct hd44780_common
*hdc
)
263 LCD_CMD_FUNCTION_SET
|
264 ((hdc
->ifwidth
== 8) ? LCD_CMD_DATA_LEN_8BITS
: 0) |
265 ((hdc
->hd44780_common_flags
& LCD_FLAG_F
) ?
266 LCD_CMD_FONT_5X10_DOTS
: 0) |
267 ((hdc
->hd44780_common_flags
& LCD_FLAG_N
) ?
268 LCD_CMD_TWO_LINES
: 0));
271 int hd44780_common_fontsize(struct charlcd
*lcd
, enum charlcd_fontsize size
)
273 struct hd44780_common
*hdc
= lcd
->drvdata
;
275 if (size
== CHARLCD_FONTSIZE_LARGE
)
276 hdc
->hd44780_common_flags
|= LCD_FLAG_F
;
278 hdc
->hd44780_common_flags
&= ~LCD_FLAG_F
;
280 hd44780_common_set_function(hdc
);
283 EXPORT_SYMBOL_GPL(hd44780_common_fontsize
);
285 int hd44780_common_lines(struct charlcd
*lcd
, enum charlcd_lines lines
)
287 struct hd44780_common
*hdc
= lcd
->drvdata
;
289 if (lines
== CHARLCD_LINES_2
)
290 hdc
->hd44780_common_flags
|= LCD_FLAG_N
;
292 hdc
->hd44780_common_flags
&= ~LCD_FLAG_N
;
294 hd44780_common_set_function(hdc
);
297 EXPORT_SYMBOL_GPL(hd44780_common_lines
);
299 int hd44780_common_redefine_char(struct charlcd
*lcd
, char *esc
)
301 /* Generator : LGcxxxxx...xx; must have <c> between '0'
302 * and '7', representing the numerical ASCII code of the
303 * redefined character, and <xx...xx> a sequence of 16
304 * hex digits representing 8 bytes for each character.
305 * Most LCDs will only use 5 lower bits of the 7 first
309 struct hd44780_common
*hdc
= lcd
->drvdata
;
310 unsigned char cgbytes
[8];
311 unsigned char cgaddr
;
317 if (!strchr(esc
, ';'))
322 cgaddr
= *(esc
++) - '0';
329 while (*esc
&& cgoffset
< 8) {
333 half
= hex_to_bin(*esc
++);
337 value
|= half
<< shift
;
339 cgbytes
[cgoffset
++] = value
;
344 hdc
->write_cmd(hdc
, LCD_CMD_SET_CGRAM_ADDR
| (cgaddr
* 8));
345 for (addr
= 0; addr
< cgoffset
; addr
++)
346 hdc
->write_data(hdc
, cgbytes
[addr
]);
348 /* ensures that we stop writing to CGRAM */
349 lcd
->ops
->gotoxy(lcd
, lcd
->addr
.x
, lcd
->addr
.y
);
352 EXPORT_SYMBOL_GPL(hd44780_common_redefine_char
);
354 struct hd44780_common
*hd44780_common_alloc(void)
356 struct hd44780_common
*hd
;
358 hd
= kzalloc(sizeof(*hd
), GFP_KERNEL
);
363 hd
->bwidth
= DEFAULT_LCD_BWIDTH
;
364 hd
->hwidth
= DEFAULT_LCD_HWIDTH
;
367 EXPORT_SYMBOL_GPL(hd44780_common_alloc
);
369 MODULE_DESCRIPTION("Common functions for HD44780 (and compatibles) LCD displays");
370 MODULE_LICENSE("GPL");