auxdisplay: cleanup unnecessary hd44780 code in charlcd
[linux-2.6-block.git] / drivers / auxdisplay / hd44780.c
CommitLineData
351f683b 1// SPDX-License-Identifier: GPL-2.0+
d47d8836
GU
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
d47d8836
GU
7 */
8
9#include <linux/delay.h>
10#include <linux/gpio/consumer.h>
11#include <linux/module.h>
ac316725 12#include <linux/mod_devicetable.h>
d47d8836
GU
13#include <linux/platform_device.h>
14#include <linux/property.h>
15#include <linux/slab.h>
16
75354284 17#include "charlcd.h"
718e05ed 18#include "hd44780_common.h"
d47d8836
GU
19
20enum 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
37struct hd44780 {
38 struct gpio_desc *pins[PIN_NUM];
39};
40
66ce7d5c 41static void hd44780_backlight(struct charlcd *lcd, enum charlcd_onoff on)
d47d8836 42{
2545c1c9
LP
43 struct hd44780_common *hdc = lcd->drvdata;
44 struct hd44780 *hd = hdc->hd44780;
d47d8836
GU
45
46 if (hd->pins[PIN_CTRL_BL])
47 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on);
48}
49
50static 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 */
64static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs)
65{
b9762beb
JK
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;
d47d8836
GU
72
73 /* Present the data to the port */
77588c14 74 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], NULL, values);
d47d8836
GU
75
76 hd44780_strobe_gpio(hd);
77}
78
79/* write to an LCD panel register in 4 bit GPIO mode */
80static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs)
81{
b9762beb
JK
82 DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
83 unsigned int n;
d47d8836
GU
84
85 /* High nibble + RS, RW */
b9762beb
JK
86 values[0] = val >> 4;
87 __assign_bit(4, values, rs);
88 n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
d47d8836
GU
89
90 /* Present the data to the port */
77588c14 91 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
d47d8836
GU
92
93 hd44780_strobe_gpio(hd);
94
95 /* Low nibble */
b9762beb
JK
96 values[0] &= ~0x0fUL;
97 values[0] |= val & 0x0f;
d47d8836
GU
98
99 /* Present the data to the port */
77588c14 100 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
d47d8836
GU
101
102 hd44780_strobe_gpio(hd);
103}
104
105/* Send a command to the LCD panel in 8 bit GPIO mode */
2c6a82f2 106static void hd44780_write_cmd_gpio8(struct hd44780_common *hdc, int cmd)
d47d8836 107{
2545c1c9 108 struct hd44780 *hd = hdc->hd44780;
d47d8836
GU
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 */
71ff701b 117static void hd44780_write_data_gpio8(struct hd44780_common *hdc, int data)
d47d8836 118{
2545c1c9 119 struct hd44780 *hd = hdc->hd44780;
d47d8836
GU
120
121 hd44780_write_gpio8(hd, data, 1);
122
123 /* The shortest data takes at least 45 us */
124 udelay(45);
125}
126
127static const struct charlcd_ops hd44780_ops_gpio8 = {
d47d8836 128 .backlight = hd44780_backlight,
b26deabb 129 .print = hd44780_common_print,
d3a2fb81 130 .gotoxy = hd44780_common_gotoxy,
88645a86 131 .home = hd44780_common_home,
45421ffe 132 .clear_display = hd44780_common_clear_display,
01ec46df 133 .init_display = hd44780_common_init_display,
d2f2187e
LP
134 .shift_cursor = hd44780_common_shift_cursor,
135 .shift_display = hd44780_common_shift_display,
136 .display = hd44780_common_display,
137 .cursor = hd44780_common_cursor,
138 .blink = hd44780_common_blink,
139 .fontsize = hd44780_common_fontsize,
140 .lines = hd44780_common_lines,
d47d8836
GU
141};
142
143/* Send a command to the LCD panel in 4 bit GPIO mode */
2c6a82f2 144static void hd44780_write_cmd_gpio4(struct hd44780_common *hdc, int cmd)
d47d8836 145{
2545c1c9 146 struct hd44780 *hd = hdc->hd44780;
d47d8836
GU
147
148 hd44780_write_gpio4(hd, cmd, 0);
149
150 /* The shortest command takes at least 120 us */
151 udelay(120);
152}
153
154/* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
2c6a82f2 155static void hd44780_write_cmd_raw_gpio4(struct hd44780_common *hdc, int cmd)
d47d8836 156{
b9762beb 157 DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
2545c1c9 158 struct hd44780 *hd = hdc->hd44780;
b9762beb 159 unsigned int n;
d47d8836
GU
160
161 /* Command nibble + RS, RW */
b9762beb
JK
162 values[0] = cmd & 0x0f;
163 n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
d47d8836
GU
164
165 /* Present the data to the port */
77588c14 166 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
d47d8836
GU
167
168 hd44780_strobe_gpio(hd);
169}
170
171/* Send data to the LCD panel in 4 bit GPIO mode */
71ff701b 172static void hd44780_write_data_gpio4(struct hd44780_common *hdc, int data)
d47d8836 173{
2545c1c9 174 struct hd44780 *hd = hdc->hd44780;
d47d8836
GU
175
176 hd44780_write_gpio4(hd, data, 1);
177
178 /* The shortest data takes at least 45 us */
179 udelay(45);
180}
181
182static const struct charlcd_ops hd44780_ops_gpio4 = {
d47d8836 183 .backlight = hd44780_backlight,
b26deabb 184 .print = hd44780_common_print,
d3a2fb81 185 .gotoxy = hd44780_common_gotoxy,
88645a86 186 .home = hd44780_common_home,
45421ffe 187 .clear_display = hd44780_common_clear_display,
01ec46df 188 .init_display = hd44780_common_init_display,
d2f2187e
LP
189 .shift_cursor = hd44780_common_shift_cursor,
190 .shift_display = hd44780_common_shift_display,
191 .display = hd44780_common_display,
192 .cursor = hd44780_common_cursor,
193 .blink = hd44780_common_blink,
194 .fontsize = hd44780_common_fontsize,
195 .lines = hd44780_common_lines,
d47d8836
GU
196};
197
198static int hd44780_probe(struct platform_device *pdev)
199{
200 struct device *dev = &pdev->dev;
201 unsigned int i, base;
202 struct charlcd *lcd;
718e05ed 203 struct hd44780_common *hdc;
d47d8836 204 struct hd44780 *hd;
718e05ed 205 int ifwidth, ret = -ENOMEM;
d47d8836
GU
206
207 /* Required pins */
208 ifwidth = gpiod_count(dev, "data");
209 if (ifwidth < 0)
210 return ifwidth;
211
212 switch (ifwidth) {
213 case 4:
214 base = PIN_DATA4;
215 break;
216 case 8:
217 base = PIN_DATA0;
218 break;
219 default:
220 return -EINVAL;
221 }
222
718e05ed
LP
223 hdc = hd44780_common_alloc();
224 if (!hdc)
225 return -ENOMEM;
226
2545c1c9 227 lcd = charlcd_alloc();
d47d8836 228 if (!lcd)
718e05ed 229 goto fail1;
d47d8836 230
718e05ed
LP
231 hd = kzalloc(sizeof(struct hd44780), GFP_KERNEL);
232 if (!hd)
233 goto fail2;
d47d8836 234
718e05ed
LP
235 hdc->hd44780 = hd;
236 lcd->drvdata = hdc;
d47d8836
GU
237 for (i = 0; i < ifwidth; i++) {
238 hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i,
239 GPIOD_OUT_LOW);
240 if (IS_ERR(hd->pins[base + i])) {
241 ret = PTR_ERR(hd->pins[base + i]);
718e05ed 242 goto fail3;
d47d8836
GU
243 }
244 }
245
246 hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
247 if (IS_ERR(hd->pins[PIN_CTRL_E])) {
248 ret = PTR_ERR(hd->pins[PIN_CTRL_E]);
718e05ed 249 goto fail3;
d47d8836
GU
250 }
251
252 hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH);
253 if (IS_ERR(hd->pins[PIN_CTRL_RS])) {
254 ret = PTR_ERR(hd->pins[PIN_CTRL_RS]);
718e05ed 255 goto fail3;
d47d8836
GU
256 }
257
258 /* Optional pins */
259 hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw",
260 GPIOD_OUT_LOW);
261 if (IS_ERR(hd->pins[PIN_CTRL_RW])) {
262 ret = PTR_ERR(hd->pins[PIN_CTRL_RW]);
718e05ed 263 goto fail3;
d47d8836
GU
264 }
265
266 hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight",
267 GPIOD_OUT_LOW);
268 if (IS_ERR(hd->pins[PIN_CTRL_BL])) {
269 ret = PTR_ERR(hd->pins[PIN_CTRL_BL]);
718e05ed 270 goto fail3;
d47d8836
GU
271 }
272
273 /* Required properties */
c7c3f096
GU
274 ret = device_property_read_u32(dev, "display-height-chars",
275 &lcd->height);
d47d8836 276 if (ret)
718e05ed 277 goto fail3;
c7c3f096 278 ret = device_property_read_u32(dev, "display-width-chars", &lcd->width);
d47d8836 279 if (ret)
718e05ed 280 goto fail3;
d47d8836
GU
281
282 /*
283 * On displays with more than two rows, the internal buffer width is
284 * usually equal to the display width
285 */
286 if (lcd->height > 2)
2545c1c9 287 hdc->bwidth = lcd->width;
d47d8836
GU
288
289 /* Optional properties */
2545c1c9 290 device_property_read_u32(dev, "internal-buffer-width", &hdc->bwidth);
d47d8836 291
3fc04dd7 292 hdc->ifwidth = ifwidth;
71ff701b
LP
293 if (ifwidth == 8) {
294 lcd->ops = &hd44780_ops_gpio8;
295 hdc->write_data = hd44780_write_data_gpio8;
2c6a82f2 296 hdc->write_cmd = hd44780_write_cmd_gpio8;
71ff701b
LP
297 } else {
298 lcd->ops = &hd44780_ops_gpio4;
299 hdc->write_data = hd44780_write_data_gpio4;
2c6a82f2
LP
300 hdc->write_cmd = hd44780_write_cmd_gpio4;
301 hdc->write_cmd_raw4 = hd44780_write_cmd_raw_gpio4;
71ff701b 302 }
d47d8836
GU
303
304 ret = charlcd_register(lcd);
305 if (ret)
718e05ed 306 goto fail3;
d47d8836
GU
307
308 platform_set_drvdata(pdev, lcd);
309 return 0;
310
718e05ed
LP
311fail3:
312 kfree(hd);
313fail2:
314 kfree(lcd);
315fail1:
316 kfree(hdc);
d47d8836
GU
317 return ret;
318}
319
320static int hd44780_remove(struct platform_device *pdev)
321{
322 struct charlcd *lcd = platform_get_drvdata(pdev);
323
718e05ed 324 kfree(lcd->drvdata);
d47d8836 325 charlcd_unregister(lcd);
41c8d0ad 326
718e05ed 327 kfree(lcd);
d47d8836
GU
328 return 0;
329}
330
331static const struct of_device_id hd44780_of_match[] = {
332 { .compatible = "hit,hd44780" },
333 { /* sentinel */ }
334};
335MODULE_DEVICE_TABLE(of, hd44780_of_match);
336
337static struct platform_driver hd44780_driver = {
338 .probe = hd44780_probe,
339 .remove = hd44780_remove,
340 .driver = {
341 .name = "hd44780",
342 .of_match_table = hd44780_of_match,
343 },
344};
345
346module_platform_driver(hd44780_driver);
347MODULE_DESCRIPTION("HD44780 Character LCD driver");
348MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
349MODULE_LICENSE("GPL");