Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / auxdisplay / hd44780_common.c
blob3934c2eebf33d312ff9b23af754b66358f7f879e
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/module.h>
3 #include <linux/sched.h>
4 #include <linux/slab.h>
6 #include "charlcd.h"
7 #include "hd44780_common.h"
9 /* LCD commands */
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);
45 return 0;
48 return 1;
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;
55 unsigned int addr;
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;
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;
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 */
84 long_sleep(2);
85 return 0;
87 EXPORT_SYMBOL_GPL(hd44780_common_clear_display);
89 int hd44780_common_init_display(struct charlcd *lcd)
91 struct hd44780_common *hdc = lcd->drvdata;
93 void (*write_cmd_raw)(struct hd44780_common *hdc, int cmd);
94 u8 init;
96 if (hdc->ifwidth != 4 && hdc->ifwidth != 8)
97 return -EINVAL;
99 hdc->hd44780_common_flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) |
100 LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
102 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
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
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;
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);
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);
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);
138 /* display off, cursor off, blink off */
139 hdc->write_cmd(hdc, LCD_CMD_DISPLAY_CTRL);
140 long_sleep(10);
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));
151 charlcd_backlight(lcd,
152 (hdc->hd44780_common_flags & LCD_FLAG_L) ? 1 : 0);
154 long_sleep(10);
156 /* entry mode set : increment, cursor shifting */
157 hdc->write_cmd(hdc, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
159 hd44780_common_clear_display(lcd);
160 return 0;
162 EXPORT_SYMBOL_GPL(hd44780_common_init_display);
164 int hd44780_common_shift_cursor(struct charlcd *lcd, enum charlcd_shift_dir dir)
166 struct hd44780_common *hdc = lcd->drvdata;
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);
179 return 0;
181 EXPORT_SYMBOL_GPL(hd44780_common_shift_cursor);
183 int hd44780_common_shift_display(struct charlcd *lcd,
184 enum charlcd_shift_dir dir)
186 struct hd44780_common *hdc = lcd->drvdata;
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);
194 return 0;
196 EXPORT_SYMBOL_GPL(hd44780_common_shift_display);
198 static void hd44780_common_set_mode(struct hd44780_common *hdc)
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));
210 int hd44780_common_display(struct charlcd *lcd, enum charlcd_onoff on)
212 struct hd44780_common *hdc = lcd->drvdata;
214 if (on == CHARLCD_ON)
215 hdc->hd44780_common_flags |= LCD_FLAG_D;
216 else
217 hdc->hd44780_common_flags &= ~LCD_FLAG_D;
219 hd44780_common_set_mode(hdc);
220 return 0;
222 EXPORT_SYMBOL_GPL(hd44780_common_display);
224 int hd44780_common_cursor(struct charlcd *lcd, enum charlcd_onoff on)
226 struct hd44780_common *hdc = lcd->drvdata;
228 if (on == CHARLCD_ON)
229 hdc->hd44780_common_flags |= LCD_FLAG_C;
230 else
231 hdc->hd44780_common_flags &= ~LCD_FLAG_C;
233 hd44780_common_set_mode(hdc);
234 return 0;
236 EXPORT_SYMBOL_GPL(hd44780_common_cursor);
238 int hd44780_common_blink(struct charlcd *lcd, enum charlcd_onoff on)
240 struct hd44780_common *hdc = lcd->drvdata;
242 if (on == CHARLCD_ON)
243 hdc->hd44780_common_flags |= LCD_FLAG_B;
244 else
245 hdc->hd44780_common_flags &= ~LCD_FLAG_B;
247 hd44780_common_set_mode(hdc);
248 return 0;
250 EXPORT_SYMBOL_GPL(hd44780_common_blink);
252 static void hd44780_common_set_function(struct hd44780_common *hdc)
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));
263 int hd44780_common_fontsize(struct charlcd *lcd, enum charlcd_fontsize size)
265 struct hd44780_common *hdc = lcd->drvdata;
267 if (size == CHARLCD_FONTSIZE_LARGE)
268 hdc->hd44780_common_flags |= LCD_FLAG_F;
269 else
270 hdc->hd44780_common_flags &= ~LCD_FLAG_F;
272 hd44780_common_set_function(hdc);
273 return 0;
275 EXPORT_SYMBOL_GPL(hd44780_common_fontsize);
277 int hd44780_common_lines(struct charlcd *lcd, enum charlcd_lines lines)
279 struct hd44780_common *hdc = lcd->drvdata;
281 if (lines == CHARLCD_LINES_2)
282 hdc->hd44780_common_flags |= LCD_FLAG_N;
283 else
284 hdc->hd44780_common_flags &= ~LCD_FLAG_N;
286 hd44780_common_set_function(hdc);
287 return 0;
289 EXPORT_SYMBOL_GPL(hd44780_common_lines);
291 int hd44780_common_redefine_char(struct charlcd *lcd, char *esc)
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.
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;
309 if (!strchr(esc, ';'))
310 return 0;
312 esc++;
314 cgaddr = *(esc++) - '0';
315 if (cgaddr > 7)
316 return 1;
318 cgoffset = 0;
319 shift = 0;
320 value = 0;
321 while (*esc && cgoffset < 8) {
322 int half;
324 shift ^= 4;
325 half = hex_to_bin(*esc++);
326 if (half < 0)
327 continue;
329 value |= half << shift;
330 if (shift == 0) {
331 cgbytes[cgoffset++] = value;
332 value = 0;
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]);
340 /* ensures that we stop writing to CGRAM */
341 lcd->ops->gotoxy(lcd, lcd->addr.x, lcd->addr.y);
342 return 1;
344 EXPORT_SYMBOL_GPL(hd44780_common_redefine_char);
346 struct hd44780_common *hd44780_common_alloc(void)
348 struct hd44780_common *hd;
350 hd = kzalloc(sizeof(*hd), GFP_KERNEL);
351 if (!hd)
352 return NULL;
354 hd->ifwidth = 8;
355 hd->bwidth = DEFAULT_LCD_BWIDTH;
356 hd->hwidth = DEFAULT_LCD_HWIDTH;
357 return hd;
359 EXPORT_SYMBOL_GPL(hd44780_common_alloc);
361 MODULE_LICENSE("GPL");