2 * HD44780 Character LCD driver for Linux
5 * Copyright (C) 2016-2017 Glider bvba
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/slab.h>
20 #include <misc/charlcd.h>
24 /* Order does matter due to writing to GPIO array subsets! */
25 PIN_DATA0, /* Optional */
26 PIN_DATA1, /* Optional */
27 PIN_DATA2, /* Optional */
28 PIN_DATA3, /* Optional */
34 PIN_CTRL_RW, /* Optional */
36 PIN_CTRL_BL, /* Optional */
41 struct gpio_desc *pins[PIN_NUM];
44 static void hd44780_backlight(struct charlcd *lcd, int on)
46 struct hd44780 *hd = lcd->drvdata;
48 if (hd->pins[PIN_CTRL_BL])
49 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on);
52 static void hd44780_strobe_gpio(struct hd44780 *hd)
54 /* Maintain the data during 20 us before the strobe */
57 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1);
59 /* Maintain the strobe during 40 us */
62 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0);
65 /* write to an LCD panel register in 8 bit GPIO mode */
66 static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs)
68 int values[10]; /* for DATA[0-7], RS, RW */
71 for (i = 0; i < 8; i++)
72 values[PIN_DATA0 + i] = !!(val & BIT(i));
73 values[PIN_CTRL_RS] = rs;
75 if (hd->pins[PIN_CTRL_RW]) {
76 values[PIN_CTRL_RW] = 0;
80 /* Present the data to the port */
81 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], values);
83 hd44780_strobe_gpio(hd);
86 /* write to an LCD panel register in 4 bit GPIO mode */
87 static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs)
89 int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
92 /* High nibble + RS, RW */
93 for (i = 4; i < 8; i++)
94 values[PIN_DATA0 + i] = !!(val & BIT(i));
95 values[PIN_CTRL_RS] = rs;
97 if (hd->pins[PIN_CTRL_RW]) {
98 values[PIN_CTRL_RW] = 0;
102 /* Present the data to the port */
103 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
106 hd44780_strobe_gpio(hd);
109 for (i = 0; i < 4; i++)
110 values[PIN_DATA4 + i] = !!(val & BIT(i));
112 /* Present the data to the port */
113 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
116 hd44780_strobe_gpio(hd);
119 /* Send a command to the LCD panel in 8 bit GPIO mode */
120 static void hd44780_write_cmd_gpio8(struct charlcd *lcd, int cmd)
122 struct hd44780 *hd = lcd->drvdata;
124 hd44780_write_gpio8(hd, cmd, 0);
126 /* The shortest command takes at least 120 us */
130 /* Send data to the LCD panel in 8 bit GPIO mode */
131 static void hd44780_write_data_gpio8(struct charlcd *lcd, int data)
133 struct hd44780 *hd = lcd->drvdata;
135 hd44780_write_gpio8(hd, data, 1);
137 /* The shortest data takes at least 45 us */
141 static const struct charlcd_ops hd44780_ops_gpio8 = {
142 .write_cmd = hd44780_write_cmd_gpio8,
143 .write_data = hd44780_write_data_gpio8,
144 .backlight = hd44780_backlight,
147 /* Send a command to the LCD panel in 4 bit GPIO mode */
148 static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd)
150 struct hd44780 *hd = lcd->drvdata;
152 hd44780_write_gpio4(hd, cmd, 0);
154 /* The shortest command takes at least 120 us */
158 /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
159 static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd)
161 int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
162 struct hd44780 *hd = lcd->drvdata;
165 /* Command nibble + RS, RW */
166 for (i = 0; i < 4; i++)
167 values[PIN_DATA4 + i] = !!(cmd & BIT(i));
168 values[PIN_CTRL_RS] = 0;
170 if (hd->pins[PIN_CTRL_RW]) {
171 values[PIN_CTRL_RW] = 0;
175 /* Present the data to the port */
176 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
179 hd44780_strobe_gpio(hd);
182 /* Send data to the LCD panel in 4 bit GPIO mode */
183 static void hd44780_write_data_gpio4(struct charlcd *lcd, int data)
185 struct hd44780 *hd = lcd->drvdata;
187 hd44780_write_gpio4(hd, data, 1);
189 /* The shortest data takes at least 45 us */
193 static const struct charlcd_ops hd44780_ops_gpio4 = {
194 .write_cmd = hd44780_write_cmd_gpio4,
195 .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4,
196 .write_data = hd44780_write_data_gpio4,
197 .backlight = hd44780_backlight,
200 static int hd44780_probe(struct platform_device *pdev)
202 struct device *dev = &pdev->dev;
203 unsigned int i, base;
209 ifwidth = gpiod_count(dev, "data");
224 lcd = charlcd_alloc(sizeof(struct hd44780));
230 for (i = 0; i < ifwidth; i++) {
231 hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i,
233 if (IS_ERR(hd->pins[base + i])) {
234 ret = PTR_ERR(hd->pins[base + i]);
239 hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
240 if (IS_ERR(hd->pins[PIN_CTRL_E])) {
241 ret = PTR_ERR(hd->pins[PIN_CTRL_E]);
245 hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH);
246 if (IS_ERR(hd->pins[PIN_CTRL_RS])) {
247 ret = PTR_ERR(hd->pins[PIN_CTRL_RS]);
252 hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw",
254 if (IS_ERR(hd->pins[PIN_CTRL_RW])) {
255 ret = PTR_ERR(hd->pins[PIN_CTRL_RW]);
259 hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight",
261 if (IS_ERR(hd->pins[PIN_CTRL_BL])) {
262 ret = PTR_ERR(hd->pins[PIN_CTRL_BL]);
266 /* Required properties */
267 ret = device_property_read_u32(dev, "display-height-chars",
271 ret = device_property_read_u32(dev, "display-width-chars", &lcd->width);
276 * On displays with more than two rows, the internal buffer width is
277 * usually equal to the display width
280 lcd->bwidth = lcd->width;
282 /* Optional properties */
283 device_property_read_u32(dev, "internal-buffer-width", &lcd->bwidth);
285 lcd->ifwidth = ifwidth;
286 lcd->ops = ifwidth == 8 ? &hd44780_ops_gpio8 : &hd44780_ops_gpio4;
288 ret = charlcd_register(lcd);
292 platform_set_drvdata(pdev, lcd);
300 static int hd44780_remove(struct platform_device *pdev)
302 struct charlcd *lcd = platform_get_drvdata(pdev);
304 charlcd_unregister(lcd);
308 static const struct of_device_id hd44780_of_match[] = {
309 { .compatible = "hit,hd44780" },
312 MODULE_DEVICE_TABLE(of, hd44780_of_match);
314 static struct platform_driver hd44780_driver = {
315 .probe = hd44780_probe,
316 .remove = hd44780_remove,
319 .of_match_table = hd44780_of_match,
323 module_platform_driver(hd44780_driver);
324 MODULE_DESCRIPTION("HD44780 Character LCD driver");
326 MODULE_LICENSE("GPL");