xref: /linux/drivers/auxdisplay/hd44780_common.c (revision d3a2fb810f273b7a2c393d4de28ae91a3f76985d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/module.h>
3 #include <linux/slab.h>
4 
5 #include "charlcd.h"
6 #include "hd44780_common.h"
7 
8 /* LCD commands */
9 #define LCD_CMD_SET_DDRAM_ADDR	0x80	/* Set display data RAM address */
10 
11 int hd44780_common_print(struct charlcd *lcd, int c)
12 {
13 	struct hd44780_common *hdc = lcd->drvdata;
14 
15 	if (lcd->addr.x < hdc->bwidth) {
16 		hdc->write_data(hdc, c);
17 		return 0;
18 	}
19 
20 	return 1;
21 }
22 EXPORT_SYMBOL_GPL(hd44780_common_print);
23 
24 int hd44780_common_gotoxy(struct charlcd *lcd)
25 {
26 	struct hd44780_common *hdc = lcd->drvdata;
27 	unsigned int addr;
28 
29 	/*
30 	 * we force the cursor to stay at the end of the
31 	 * line if it wants to go farther
32 	 */
33 	addr = lcd->addr.x < hdc->bwidth ? lcd->addr.x & (hdc->hwidth - 1)
34 					  : hdc->bwidth - 1;
35 	if (lcd->addr.y & 1)
36 		addr += hdc->hwidth;
37 	if (lcd->addr.y & 2)
38 		addr += hdc->bwidth;
39 	hdc->write_cmd(hdc, LCD_CMD_SET_DDRAM_ADDR | addr);
40 	return 0;
41 }
42 EXPORT_SYMBOL_GPL(hd44780_common_gotoxy);
43 
44 struct hd44780_common *hd44780_common_alloc(void)
45 {
46 	struct hd44780_common *hd;
47 
48 	hd = kzalloc(sizeof(*hd), GFP_KERNEL);
49 	if (!hd)
50 		return NULL;
51 
52 	hd->ifwidth = 8;
53 	hd->bwidth = DEFAULT_LCD_BWIDTH;
54 	hd->hwidth = DEFAULT_LCD_HWIDTH;
55 	return hd;
56 }
57 EXPORT_SYMBOL_GPL(hd44780_common_alloc);
58 
59 MODULE_LICENSE("GPL");
60