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