1 // SPDX-License-Identifier: GPL-2.0+
3 * HD44780 Character LCD driver for Linux
6 * Copyright (C) 2016-2017 Glider bvba
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 #include <linux/slab.h>
16 #include <misc/charlcd.h>
20 /* Order does matter due to writing to GPIO array subsets! */
21 PIN_DATA0, /* Optional */
22 PIN_DATA1, /* Optional */
23 PIN_DATA2, /* Optional */
24 PIN_DATA3, /* Optional */
30 PIN_CTRL_RW, /* Optional */
32 PIN_CTRL_BL, /* Optional */
37 struct gpio_desc *pins[PIN_NUM];
40 static void hd44780_backlight(struct charlcd *lcd, int on)
42 struct hd44780 *hd = lcd->drvdata;
44 if (hd->pins[PIN_CTRL_BL])
45 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on);
48 static void hd44780_strobe_gpio(struct hd44780 *hd)
50 /* Maintain the data during 20 us before the strobe */
53 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1);
55 /* Maintain the strobe during 40 us */
58 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0);
61 /* write to an LCD panel register in 8 bit GPIO mode */
62 static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs)
64 int values[10]; /* for DATA[0-7], RS, RW */
67 for (i = 0; i < 8; i++)
68 values[PIN_DATA0 + i] = !!(val & BIT(i));
69 values[PIN_CTRL_RS] = rs;
71 if (hd->pins[PIN_CTRL_RW]) {
72 values[PIN_CTRL_RW] = 0;
76 /* Present the data to the port */
77 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], values);
79 hd44780_strobe_gpio(hd);
82 /* write to an LCD panel register in 4 bit GPIO mode */
83 static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs)
85 int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
88 /* High nibble + RS, RW */
89 for (i = 4; i < 8; i++)
90 values[PIN_DATA0 + i] = !!(val & BIT(i));
91 values[PIN_CTRL_RS] = rs;
93 if (hd->pins[PIN_CTRL_RW]) {
94 values[PIN_CTRL_RW] = 0;
98 /* Present the data to the port */
99 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
102 hd44780_strobe_gpio(hd);
105 for (i = 0; i < 4; i++)
106 values[PIN_DATA4 + i] = !!(val & BIT(i));
108 /* Present the data to the port */
109 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
112 hd44780_strobe_gpio(hd);
115 /* Send a command to the LCD panel in 8 bit GPIO mode */
116 static void hd44780_write_cmd_gpio8(struct charlcd *lcd, int cmd)
118 struct hd44780 *hd = lcd->drvdata;
120 hd44780_write_gpio8(hd, cmd, 0);
122 /* The shortest command takes at least 120 us */
126 /* Send data to the LCD panel in 8 bit GPIO mode */
127 static void hd44780_write_data_gpio8(struct charlcd *lcd, int data)
129 struct hd44780 *hd = lcd->drvdata;
131 hd44780_write_gpio8(hd, data, 1);
133 /* The shortest data takes at least 45 us */
137 static const struct charlcd_ops hd44780_ops_gpio8 = {
138 .write_cmd = hd44780_write_cmd_gpio8,
139 .write_data = hd44780_write_data_gpio8,
140 .backlight = hd44780_backlight,
143 /* Send a command to the LCD panel in 4 bit GPIO mode */
144 static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd)
146 struct hd44780 *hd = lcd->drvdata;
148 hd44780_write_gpio4(hd, cmd, 0);
150 /* The shortest command takes at least 120 us */
154 /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
155 static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd)
157 int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
158 struct hd44780 *hd = lcd->drvdata;
161 /* Command nibble + RS, RW */
162 for (i = 0; i < 4; i++)
163 values[PIN_DATA4 + i] = !!(cmd & BIT(i));
164 values[PIN_CTRL_RS] = 0;
166 if (hd->pins[PIN_CTRL_RW]) {
167 values[PIN_CTRL_RW] = 0;
171 /* Present the data to the port */
172 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
175 hd44780_strobe_gpio(hd);
178 /* Send data to the LCD panel in 4 bit GPIO mode */
179 static void hd44780_write_data_gpio4(struct charlcd *lcd, int data)
181 struct hd44780 *hd = lcd->drvdata;
183 hd44780_write_gpio4(hd, data, 1);
185 /* The shortest data takes at least 45 us */
189 static const struct charlcd_ops hd44780_ops_gpio4 = {
190 .write_cmd = hd44780_write_cmd_gpio4,
191 .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4,
192 .write_data = hd44780_write_data_gpio4,
193 .backlight = hd44780_backlight,
196 static int hd44780_probe(struct platform_device *pdev)
198 struct device *dev = &pdev->dev;
199 unsigned int i, base;
205 ifwidth = gpiod_count(dev, "data");
220 lcd = charlcd_alloc(sizeof(struct hd44780));
226 for (i = 0; i < ifwidth; i++) {
227 hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i,
229 if (IS_ERR(hd->pins[base + i])) {
230 ret = PTR_ERR(hd->pins[base + i]);
235 hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
236 if (IS_ERR(hd->pins[PIN_CTRL_E])) {
237 ret = PTR_ERR(hd->pins[PIN_CTRL_E]);
241 hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH);
242 if (IS_ERR(hd->pins[PIN_CTRL_RS])) {
243 ret = PTR_ERR(hd->pins[PIN_CTRL_RS]);
248 hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw",
250 if (IS_ERR(hd->pins[PIN_CTRL_RW])) {
251 ret = PTR_ERR(hd->pins[PIN_CTRL_RW]);
255 hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight",
257 if (IS_ERR(hd->pins[PIN_CTRL_BL])) {
258 ret = PTR_ERR(hd->pins[PIN_CTRL_BL]);
262 /* Required properties */
263 ret = device_property_read_u32(dev, "display-height-chars",
267 ret = device_property_read_u32(dev, "display-width-chars", &lcd->width);
272 * On displays with more than two rows, the internal buffer width is
273 * usually equal to the display width
276 lcd->bwidth = lcd->width;
278 /* Optional properties */
279 device_property_read_u32(dev, "internal-buffer-width", &lcd->bwidth);
281 lcd->ifwidth = ifwidth;
282 lcd->ops = ifwidth == 8 ? &hd44780_ops_gpio8 : &hd44780_ops_gpio4;
284 ret = charlcd_register(lcd);
288 platform_set_drvdata(pdev, lcd);
296 static int hd44780_remove(struct platform_device *pdev)
298 struct charlcd *lcd = platform_get_drvdata(pdev);
300 charlcd_unregister(lcd);
304 static const struct of_device_id hd44780_of_match[] = {
305 { .compatible = "hit,hd44780" },
308 MODULE_DEVICE_TABLE(of, hd44780_of_match);
310 static struct platform_driver hd44780_driver = {
311 .probe = hd44780_probe,
312 .remove = hd44780_remove,
315 .of_match_table = hd44780_of_match,
319 module_platform_driver(hd44780_driver);
320 MODULE_DESCRIPTION("HD44780 Character LCD driver");
322 MODULE_LICENSE("GPL");