Linux Audio

Check our new training course

Loading...
v6.8
  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 */
 34static void long_sleep(int ms)
 35{
 36	schedule_timeout_interruptible(msecs_to_jiffies(ms));
 37}
 38
 39int 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}
 50EXPORT_SYMBOL_GPL(hd44780_common_print);
 51
 52int 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}
 69EXPORT_SYMBOL_GPL(hd44780_common_gotoxy);
 70
 71int hd44780_common_home(struct charlcd *lcd)
 72{
 73	return hd44780_common_gotoxy(lcd, 0, 0);
 74}
 75EXPORT_SYMBOL_GPL(hd44780_common_home);
 76
 77/* clears the display and resets X/Y */
 78int 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
 86	/*
 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.
 92	 */
 93	return hd44780_common_home(lcd);
 94}
 95EXPORT_SYMBOL_GPL(hd44780_common_clear_display);
 96
 97int hd44780_common_init_display(struct charlcd *lcd)
 98{
 99	struct hd44780_common *hdc = lcd->drvdata;
100
101	void (*write_cmd_raw)(struct hd44780_common *hdc, int cmd);
102	u8 init;
103
104	if (hdc->ifwidth != 4 && hdc->ifwidth != 8)
105		return -EINVAL;
106
107	hdc->hd44780_common_flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) |
108		LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
109
110	long_sleep(20);		/* wait 20 ms after power-up for the paranoid */
111
112	/*
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
115	 */
116	init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
117	if (hdc->ifwidth == 4) {
118		init >>= 4;
119		write_cmd_raw = hdc->write_cmd_raw4;
120	} else {
121		write_cmd_raw = hdc->write_cmd;
122	}
123	write_cmd_raw(hdc, init);
124	long_sleep(10);
125	write_cmd_raw(hdc, init);
126	long_sleep(10);
127	write_cmd_raw(hdc, init);
128	long_sleep(10);
129
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);
133		long_sleep(10);
134	}
135
136	/* set font height and lines number */
137	hdc->write_cmd(hdc,
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));
144	long_sleep(10);
145
146	/* display off, cursor off, blink off */
147	hdc->write_cmd(hdc, LCD_CMD_DISPLAY_CTRL);
148	long_sleep(10);
149
150	hdc->write_cmd(hdc,
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));
158
159	charlcd_backlight(lcd,
160			(hdc->hd44780_common_flags & LCD_FLAG_L) ? 1 : 0);
161
162	long_sleep(10);
163
164	/* entry mode set : increment, cursor shifting */
165	hdc->write_cmd(hdc, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
166
167	hd44780_common_clear_display(lcd);
168	return 0;
169}
170EXPORT_SYMBOL_GPL(hd44780_common_init_display);
171
172int hd44780_common_shift_cursor(struct charlcd *lcd, enum charlcd_shift_dir dir)
173{
174	struct hd44780_common *hdc = lcd->drvdata;
175
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))
183			hdc->write_cmd(hdc,
184					LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
185	}
186
187	return 0;
188}
189EXPORT_SYMBOL_GPL(hd44780_common_shift_cursor);
190
191int hd44780_common_shift_display(struct charlcd *lcd,
192		enum charlcd_shift_dir dir)
193{
194	struct hd44780_common *hdc = lcd->drvdata;
195
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);
201
202	return 0;
203}
204EXPORT_SYMBOL_GPL(hd44780_common_shift_display);
205
206static void hd44780_common_set_mode(struct hd44780_common *hdc)
207{
208	hdc->write_cmd(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));
216}
217
218int hd44780_common_display(struct charlcd *lcd, enum charlcd_onoff on)
219{
220	struct hd44780_common *hdc = lcd->drvdata;
221
222	if (on == CHARLCD_ON)
223		hdc->hd44780_common_flags |= LCD_FLAG_D;
224	else
225		hdc->hd44780_common_flags &= ~LCD_FLAG_D;
226
227	hd44780_common_set_mode(hdc);
228	return 0;
229}
230EXPORT_SYMBOL_GPL(hd44780_common_display);
231
232int hd44780_common_cursor(struct charlcd *lcd, enum charlcd_onoff on)
233{
234	struct hd44780_common *hdc = lcd->drvdata;
235
236	if (on == CHARLCD_ON)
237		hdc->hd44780_common_flags |= LCD_FLAG_C;
238	else
239		hdc->hd44780_common_flags &= ~LCD_FLAG_C;
240
241	hd44780_common_set_mode(hdc);
242	return 0;
243}
244EXPORT_SYMBOL_GPL(hd44780_common_cursor);
245
246int hd44780_common_blink(struct charlcd *lcd, enum charlcd_onoff on)
247{
248	struct hd44780_common *hdc = lcd->drvdata;
249
250	if (on == CHARLCD_ON)
251		hdc->hd44780_common_flags |= LCD_FLAG_B;
252	else
253		hdc->hd44780_common_flags &= ~LCD_FLAG_B;
254
255	hd44780_common_set_mode(hdc);
256	return 0;
257}
258EXPORT_SYMBOL_GPL(hd44780_common_blink);
259
260static void hd44780_common_set_function(struct hd44780_common *hdc)
261{
262	hdc->write_cmd(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));
269}
270
271int hd44780_common_fontsize(struct charlcd *lcd, enum charlcd_fontsize size)
272{
273	struct hd44780_common *hdc = lcd->drvdata;
274
275	if (size == CHARLCD_FONTSIZE_LARGE)
276		hdc->hd44780_common_flags |= LCD_FLAG_F;
277	else
278		hdc->hd44780_common_flags &= ~LCD_FLAG_F;
279
280	hd44780_common_set_function(hdc);
281	return 0;
282}
283EXPORT_SYMBOL_GPL(hd44780_common_fontsize);
284
285int hd44780_common_lines(struct charlcd *lcd, enum charlcd_lines lines)
286{
287	struct hd44780_common *hdc = lcd->drvdata;
288
289	if (lines == CHARLCD_LINES_2)
290		hdc->hd44780_common_flags |= LCD_FLAG_N;
291	else
292		hdc->hd44780_common_flags &= ~LCD_FLAG_N;
293
294	hd44780_common_set_function(hdc);
295	return 0;
296}
297EXPORT_SYMBOL_GPL(hd44780_common_lines);
298
299int hd44780_common_redefine_char(struct charlcd *lcd, char *esc)
300{
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
306	 * bytes.
307	 */
308
309	struct hd44780_common *hdc = lcd->drvdata;
310	unsigned char cgbytes[8];
311	unsigned char cgaddr;
312	int cgoffset;
313	int shift;
314	char value;
315	int addr;
316
317	if (!strchr(esc, ';'))
318		return 0;
319
320	esc++;
321
322	cgaddr = *(esc++) - '0';
323	if (cgaddr > 7)
324		return 1;
325
326	cgoffset = 0;
327	shift = 0;
328	value = 0;
329	while (*esc && cgoffset < 8) {
330		int half;
331
332		shift ^= 4;
333		half = hex_to_bin(*esc++);
334		if (half < 0)
335			continue;
336
337		value |= half << shift;
338		if (shift == 0) {
339			cgbytes[cgoffset++] = value;
340			value = 0;
341		}
342	}
343
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]);
347
348	/* ensures that we stop writing to CGRAM */
349	lcd->ops->gotoxy(lcd, lcd->addr.x, lcd->addr.y);
350	return 1;
351}
352EXPORT_SYMBOL_GPL(hd44780_common_redefine_char);
353
354struct hd44780_common *hd44780_common_alloc(void)
355{
356	struct hd44780_common *hd;
357
358	hd = kzalloc(sizeof(*hd), GFP_KERNEL);
359	if (!hd)
360		return NULL;
361
362	hd->ifwidth = 8;
363	hd->bwidth = DEFAULT_LCD_BWIDTH;
364	hd->hwidth = DEFAULT_LCD_HWIDTH;
365	return hd;
366}
367EXPORT_SYMBOL_GPL(hd44780_common_alloc);
368
369MODULE_LICENSE("GPL");
v6.2
  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 */
 34static void long_sleep(int ms)
 35{
 36	schedule_timeout_interruptible(msecs_to_jiffies(ms));
 37}
 38
 39int 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}
 50EXPORT_SYMBOL_GPL(hd44780_common_print);
 51
 52int 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}
 69EXPORT_SYMBOL_GPL(hd44780_common_gotoxy);
 70
 71int hd44780_common_home(struct charlcd *lcd)
 72{
 73	return hd44780_common_gotoxy(lcd, 0, 0);
 74}
 75EXPORT_SYMBOL_GPL(hd44780_common_home);
 76
 77/* clears the display and resets X/Y */
 78int 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}
 87EXPORT_SYMBOL_GPL(hd44780_common_clear_display);
 88
 89int 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}
162EXPORT_SYMBOL_GPL(hd44780_common_init_display);
163
164int 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}
181EXPORT_SYMBOL_GPL(hd44780_common_shift_cursor);
182
183int 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}
196EXPORT_SYMBOL_GPL(hd44780_common_shift_display);
197
198static 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
210int 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}
222EXPORT_SYMBOL_GPL(hd44780_common_display);
223
224int 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}
236EXPORT_SYMBOL_GPL(hd44780_common_cursor);
237
238int 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}
250EXPORT_SYMBOL_GPL(hd44780_common_blink);
251
252static 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
263int 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}
275EXPORT_SYMBOL_GPL(hd44780_common_fontsize);
276
277int 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}
289EXPORT_SYMBOL_GPL(hd44780_common_lines);
290
291int 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}
344EXPORT_SYMBOL_GPL(hd44780_common_redefine_char);
345
346struct 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}
359EXPORT_SYMBOL_GPL(hd44780_common_alloc);
360
361MODULE_LICENSE("GPL");