1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * HD44780 Character LCD driver for Linux
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
6*4882a593Smuzhiyun * Copyright (C) 2016-2017 Glider bvba
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
13*4882a593Smuzhiyun #include <linux/platform_device.h>
14*4882a593Smuzhiyun #include <linux/property.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include "charlcd.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun enum hd44780_pin {
20*4882a593Smuzhiyun /* Order does matter due to writing to GPIO array subsets! */
21*4882a593Smuzhiyun PIN_DATA0, /* Optional */
22*4882a593Smuzhiyun PIN_DATA1, /* Optional */
23*4882a593Smuzhiyun PIN_DATA2, /* Optional */
24*4882a593Smuzhiyun PIN_DATA3, /* Optional */
25*4882a593Smuzhiyun PIN_DATA4,
26*4882a593Smuzhiyun PIN_DATA5,
27*4882a593Smuzhiyun PIN_DATA6,
28*4882a593Smuzhiyun PIN_DATA7,
29*4882a593Smuzhiyun PIN_CTRL_RS,
30*4882a593Smuzhiyun PIN_CTRL_RW, /* Optional */
31*4882a593Smuzhiyun PIN_CTRL_E,
32*4882a593Smuzhiyun PIN_CTRL_BL, /* Optional */
33*4882a593Smuzhiyun PIN_NUM
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun struct hd44780 {
37*4882a593Smuzhiyun struct gpio_desc *pins[PIN_NUM];
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
hd44780_backlight(struct charlcd * lcd,int on)40*4882a593Smuzhiyun static void hd44780_backlight(struct charlcd *lcd, int on)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun struct hd44780 *hd = lcd->drvdata;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun if (hd->pins[PIN_CTRL_BL])
45*4882a593Smuzhiyun gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
hd44780_strobe_gpio(struct hd44780 * hd)48*4882a593Smuzhiyun static void hd44780_strobe_gpio(struct hd44780 *hd)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun /* Maintain the data during 20 us before the strobe */
51*4882a593Smuzhiyun udelay(20);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* Maintain the strobe during 40 us */
56*4882a593Smuzhiyun udelay(40);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* write to an LCD panel register in 8 bit GPIO mode */
hd44780_write_gpio8(struct hd44780 * hd,u8 val,unsigned int rs)62*4882a593Smuzhiyun static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun DECLARE_BITMAP(values, 10); /* for DATA[0-7], RS, RW */
65*4882a593Smuzhiyun unsigned int n;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun values[0] = val;
68*4882a593Smuzhiyun __assign_bit(8, values, rs);
69*4882a593Smuzhiyun n = hd->pins[PIN_CTRL_RW] ? 10 : 9;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* Present the data to the port */
72*4882a593Smuzhiyun gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], NULL, values);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun hd44780_strobe_gpio(hd);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* write to an LCD panel register in 4 bit GPIO mode */
hd44780_write_gpio4(struct hd44780 * hd,u8 val,unsigned int rs)78*4882a593Smuzhiyun static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
81*4882a593Smuzhiyun unsigned int n;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* High nibble + RS, RW */
84*4882a593Smuzhiyun values[0] = val >> 4;
85*4882a593Smuzhiyun __assign_bit(4, values, rs);
86*4882a593Smuzhiyun n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /* Present the data to the port */
89*4882a593Smuzhiyun gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun hd44780_strobe_gpio(hd);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* Low nibble */
94*4882a593Smuzhiyun values[0] &= ~0x0fUL;
95*4882a593Smuzhiyun values[0] |= val & 0x0f;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* Present the data to the port */
98*4882a593Smuzhiyun gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun hd44780_strobe_gpio(hd);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /* Send a command to the LCD panel in 8 bit GPIO mode */
hd44780_write_cmd_gpio8(struct charlcd * lcd,int cmd)104*4882a593Smuzhiyun static void hd44780_write_cmd_gpio8(struct charlcd *lcd, int cmd)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun struct hd44780 *hd = lcd->drvdata;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun hd44780_write_gpio8(hd, cmd, 0);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /* The shortest command takes at least 120 us */
111*4882a593Smuzhiyun udelay(120);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* Send data to the LCD panel in 8 bit GPIO mode */
hd44780_write_data_gpio8(struct charlcd * lcd,int data)115*4882a593Smuzhiyun static void hd44780_write_data_gpio8(struct charlcd *lcd, int data)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct hd44780 *hd = lcd->drvdata;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun hd44780_write_gpio8(hd, data, 1);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* The shortest data takes at least 45 us */
122*4882a593Smuzhiyun udelay(45);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun static const struct charlcd_ops hd44780_ops_gpio8 = {
126*4882a593Smuzhiyun .write_cmd = hd44780_write_cmd_gpio8,
127*4882a593Smuzhiyun .write_data = hd44780_write_data_gpio8,
128*4882a593Smuzhiyun .backlight = hd44780_backlight,
129*4882a593Smuzhiyun };
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /* Send a command to the LCD panel in 4 bit GPIO mode */
hd44780_write_cmd_gpio4(struct charlcd * lcd,int cmd)132*4882a593Smuzhiyun static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun struct hd44780 *hd = lcd->drvdata;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun hd44780_write_gpio4(hd, cmd, 0);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* The shortest command takes at least 120 us */
139*4882a593Smuzhiyun udelay(120);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
hd44780_write_cmd_raw_gpio4(struct charlcd * lcd,int cmd)143*4882a593Smuzhiyun static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
146*4882a593Smuzhiyun struct hd44780 *hd = lcd->drvdata;
147*4882a593Smuzhiyun unsigned int n;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* Command nibble + RS, RW */
150*4882a593Smuzhiyun values[0] = cmd & 0x0f;
151*4882a593Smuzhiyun n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* Present the data to the port */
154*4882a593Smuzhiyun gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun hd44780_strobe_gpio(hd);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* Send data to the LCD panel in 4 bit GPIO mode */
hd44780_write_data_gpio4(struct charlcd * lcd,int data)160*4882a593Smuzhiyun static void hd44780_write_data_gpio4(struct charlcd *lcd, int data)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun struct hd44780 *hd = lcd->drvdata;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun hd44780_write_gpio4(hd, data, 1);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /* The shortest data takes at least 45 us */
167*4882a593Smuzhiyun udelay(45);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun static const struct charlcd_ops hd44780_ops_gpio4 = {
171*4882a593Smuzhiyun .write_cmd = hd44780_write_cmd_gpio4,
172*4882a593Smuzhiyun .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4,
173*4882a593Smuzhiyun .write_data = hd44780_write_data_gpio4,
174*4882a593Smuzhiyun .backlight = hd44780_backlight,
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun
hd44780_probe(struct platform_device * pdev)177*4882a593Smuzhiyun static int hd44780_probe(struct platform_device *pdev)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun struct device *dev = &pdev->dev;
180*4882a593Smuzhiyun unsigned int i, base;
181*4882a593Smuzhiyun struct charlcd *lcd;
182*4882a593Smuzhiyun struct hd44780 *hd;
183*4882a593Smuzhiyun int ifwidth, ret;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* Required pins */
186*4882a593Smuzhiyun ifwidth = gpiod_count(dev, "data");
187*4882a593Smuzhiyun if (ifwidth < 0)
188*4882a593Smuzhiyun return ifwidth;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun switch (ifwidth) {
191*4882a593Smuzhiyun case 4:
192*4882a593Smuzhiyun base = PIN_DATA4;
193*4882a593Smuzhiyun break;
194*4882a593Smuzhiyun case 8:
195*4882a593Smuzhiyun base = PIN_DATA0;
196*4882a593Smuzhiyun break;
197*4882a593Smuzhiyun default:
198*4882a593Smuzhiyun return -EINVAL;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun lcd = charlcd_alloc(sizeof(struct hd44780));
202*4882a593Smuzhiyun if (!lcd)
203*4882a593Smuzhiyun return -ENOMEM;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun hd = lcd->drvdata;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun for (i = 0; i < ifwidth; i++) {
208*4882a593Smuzhiyun hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i,
209*4882a593Smuzhiyun GPIOD_OUT_LOW);
210*4882a593Smuzhiyun if (IS_ERR(hd->pins[base + i])) {
211*4882a593Smuzhiyun ret = PTR_ERR(hd->pins[base + i]);
212*4882a593Smuzhiyun goto fail;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
217*4882a593Smuzhiyun if (IS_ERR(hd->pins[PIN_CTRL_E])) {
218*4882a593Smuzhiyun ret = PTR_ERR(hd->pins[PIN_CTRL_E]);
219*4882a593Smuzhiyun goto fail;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH);
223*4882a593Smuzhiyun if (IS_ERR(hd->pins[PIN_CTRL_RS])) {
224*4882a593Smuzhiyun ret = PTR_ERR(hd->pins[PIN_CTRL_RS]);
225*4882a593Smuzhiyun goto fail;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /* Optional pins */
229*4882a593Smuzhiyun hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw",
230*4882a593Smuzhiyun GPIOD_OUT_LOW);
231*4882a593Smuzhiyun if (IS_ERR(hd->pins[PIN_CTRL_RW])) {
232*4882a593Smuzhiyun ret = PTR_ERR(hd->pins[PIN_CTRL_RW]);
233*4882a593Smuzhiyun goto fail;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight",
237*4882a593Smuzhiyun GPIOD_OUT_LOW);
238*4882a593Smuzhiyun if (IS_ERR(hd->pins[PIN_CTRL_BL])) {
239*4882a593Smuzhiyun ret = PTR_ERR(hd->pins[PIN_CTRL_BL]);
240*4882a593Smuzhiyun goto fail;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /* Required properties */
244*4882a593Smuzhiyun ret = device_property_read_u32(dev, "display-height-chars",
245*4882a593Smuzhiyun &lcd->height);
246*4882a593Smuzhiyun if (ret)
247*4882a593Smuzhiyun goto fail;
248*4882a593Smuzhiyun ret = device_property_read_u32(dev, "display-width-chars", &lcd->width);
249*4882a593Smuzhiyun if (ret)
250*4882a593Smuzhiyun goto fail;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /*
253*4882a593Smuzhiyun * On displays with more than two rows, the internal buffer width is
254*4882a593Smuzhiyun * usually equal to the display width
255*4882a593Smuzhiyun */
256*4882a593Smuzhiyun if (lcd->height > 2)
257*4882a593Smuzhiyun lcd->bwidth = lcd->width;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /* Optional properties */
260*4882a593Smuzhiyun device_property_read_u32(dev, "internal-buffer-width", &lcd->bwidth);
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun lcd->ifwidth = ifwidth;
263*4882a593Smuzhiyun lcd->ops = ifwidth == 8 ? &hd44780_ops_gpio8 : &hd44780_ops_gpio4;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun ret = charlcd_register(lcd);
266*4882a593Smuzhiyun if (ret)
267*4882a593Smuzhiyun goto fail;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun platform_set_drvdata(pdev, lcd);
270*4882a593Smuzhiyun return 0;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun fail:
273*4882a593Smuzhiyun charlcd_free(lcd);
274*4882a593Smuzhiyun return ret;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
hd44780_remove(struct platform_device * pdev)277*4882a593Smuzhiyun static int hd44780_remove(struct platform_device *pdev)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun struct charlcd *lcd = platform_get_drvdata(pdev);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun charlcd_unregister(lcd);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun charlcd_free(lcd);
284*4882a593Smuzhiyun return 0;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun static const struct of_device_id hd44780_of_match[] = {
288*4882a593Smuzhiyun { .compatible = "hit,hd44780" },
289*4882a593Smuzhiyun { /* sentinel */ }
290*4882a593Smuzhiyun };
291*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, hd44780_of_match);
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun static struct platform_driver hd44780_driver = {
294*4882a593Smuzhiyun .probe = hd44780_probe,
295*4882a593Smuzhiyun .remove = hd44780_remove,
296*4882a593Smuzhiyun .driver = {
297*4882a593Smuzhiyun .name = "hd44780",
298*4882a593Smuzhiyun .of_match_table = hd44780_of_match,
299*4882a593Smuzhiyun },
300*4882a593Smuzhiyun };
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun module_platform_driver(hd44780_driver);
303*4882a593Smuzhiyun MODULE_DESCRIPTION("HD44780 Character LCD driver");
304*4882a593Smuzhiyun MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
305*4882a593Smuzhiyun MODULE_LICENSE("GPL");
306