auxdisplay: Move write_data pointer to hd44780_common
[linux-2.6-microblaze.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 charlcd *lcd, int cmd)
107 {
108         struct hd44780_common *hdc = lcd->drvdata;
109         struct hd44780 *hd = hdc->hd44780;
110
111         hd44780_write_gpio8(hd, cmd, 0);
112
113         /* The shortest command takes at least 120 us */
114         udelay(120);
115 }
116
117 /* Send data to the LCD panel in 8 bit GPIO mode */
118 static void hd44780_write_data_gpio8(struct hd44780_common *hdc, int data)
119 {
120         struct hd44780 *hd = hdc->hd44780;
121
122         hd44780_write_gpio8(hd, data, 1);
123
124         /* The shortest data takes at least 45 us */
125         udelay(45);
126 }
127
128 static const struct charlcd_ops hd44780_ops_gpio8 = {
129         .write_cmd      = hd44780_write_cmd_gpio8,
130         .backlight      = hd44780_backlight,
131 };
132
133 /* Send a command to the LCD panel in 4 bit GPIO mode */
134 static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd)
135 {
136         struct hd44780_common *hdc = lcd->drvdata;
137         struct hd44780 *hd = hdc->hd44780;
138
139         hd44780_write_gpio4(hd, cmd, 0);
140
141         /* The shortest command takes at least 120 us */
142         udelay(120);
143 }
144
145 /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
146 static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd)
147 {
148         DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
149         struct hd44780_common *hdc = lcd->drvdata;
150         struct hd44780 *hd = hdc->hd44780;
151         unsigned int n;
152
153         /* Command nibble + RS, RW */
154         values[0] = cmd & 0x0f;
155         n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
156
157         /* Present the data to the port */
158         gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
159
160         hd44780_strobe_gpio(hd);
161 }
162
163 /* Send data to the LCD panel in 4 bit GPIO mode */
164 static void hd44780_write_data_gpio4(struct hd44780_common *hdc, int data)
165 {
166         struct hd44780 *hd = hdc->hd44780;
167
168         hd44780_write_gpio4(hd, data, 1);
169
170         /* The shortest data takes at least 45 us */
171         udelay(45);
172 }
173
174 static const struct charlcd_ops hd44780_ops_gpio4 = {
175         .write_cmd      = hd44780_write_cmd_gpio4,
176         .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4,
177         .backlight      = hd44780_backlight,
178 };
179
180 static int hd44780_probe(struct platform_device *pdev)
181 {
182         struct device *dev = &pdev->dev;
183         unsigned int i, base;
184         struct charlcd *lcd;
185         struct hd44780_common *hdc;
186         struct hd44780 *hd;
187         int ifwidth, ret = -ENOMEM;
188
189         /* Required pins */
190         ifwidth = gpiod_count(dev, "data");
191         if (ifwidth < 0)
192                 return ifwidth;
193
194         switch (ifwidth) {
195         case 4:
196                 base = PIN_DATA4;
197                 break;
198         case 8:
199                 base = PIN_DATA0;
200                 break;
201         default:
202                 return -EINVAL;
203         }
204
205         hdc = hd44780_common_alloc();
206         if (!hdc)
207                 return -ENOMEM;
208
209         lcd = charlcd_alloc();
210         if (!lcd)
211                 goto fail1;
212
213         hd = kzalloc(sizeof(struct hd44780), GFP_KERNEL);
214         if (!hd)
215                 goto fail2;
216
217         hdc->hd44780 = hd;
218         lcd->drvdata = hdc;
219         for (i = 0; i < ifwidth; i++) {
220                 hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i,
221                                                           GPIOD_OUT_LOW);
222                 if (IS_ERR(hd->pins[base + i])) {
223                         ret = PTR_ERR(hd->pins[base + i]);
224                         goto fail3;
225                 }
226         }
227
228         hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
229         if (IS_ERR(hd->pins[PIN_CTRL_E])) {
230                 ret = PTR_ERR(hd->pins[PIN_CTRL_E]);
231                 goto fail3;
232         }
233
234         hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH);
235         if (IS_ERR(hd->pins[PIN_CTRL_RS])) {
236                 ret = PTR_ERR(hd->pins[PIN_CTRL_RS]);
237                 goto fail3;
238         }
239
240         /* Optional pins */
241         hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw",
242                                                         GPIOD_OUT_LOW);
243         if (IS_ERR(hd->pins[PIN_CTRL_RW])) {
244                 ret = PTR_ERR(hd->pins[PIN_CTRL_RW]);
245                 goto fail3;
246         }
247
248         hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight",
249                                                         GPIOD_OUT_LOW);
250         if (IS_ERR(hd->pins[PIN_CTRL_BL])) {
251                 ret = PTR_ERR(hd->pins[PIN_CTRL_BL]);
252                 goto fail3;
253         }
254
255         /* Required properties */
256         ret = device_property_read_u32(dev, "display-height-chars",
257                                        &lcd->height);
258         if (ret)
259                 goto fail3;
260         ret = device_property_read_u32(dev, "display-width-chars", &lcd->width);
261         if (ret)
262                 goto fail3;
263
264         /*
265          * On displays with more than two rows, the internal buffer width is
266          * usually equal to the display width
267          */
268         if (lcd->height > 2)
269                 hdc->bwidth = lcd->width;
270
271         /* Optional properties */
272         device_property_read_u32(dev, "internal-buffer-width", &hdc->bwidth);
273
274         hdc->ifwidth = ifwidth;
275         if (ifwidth == 8) {
276                 lcd->ops = &hd44780_ops_gpio8;
277                 hdc->write_data = hd44780_write_data_gpio8;
278         } else {
279                 lcd->ops = &hd44780_ops_gpio4;
280                 hdc->write_data = hd44780_write_data_gpio4;
281         }
282
283         ret = charlcd_register(lcd);
284         if (ret)
285                 goto fail3;
286
287         platform_set_drvdata(pdev, lcd);
288         return 0;
289
290 fail3:
291         kfree(hd);
292 fail2:
293         kfree(lcd);
294 fail1:
295         kfree(hdc);
296         return ret;
297 }
298
299 static int hd44780_remove(struct platform_device *pdev)
300 {
301         struct charlcd *lcd = platform_get_drvdata(pdev);
302
303         kfree(lcd->drvdata);
304         charlcd_unregister(lcd);
305
306         kfree(lcd);
307         return 0;
308 }
309
310 static const struct of_device_id hd44780_of_match[] = {
311         { .compatible = "hit,hd44780" },
312         { /* sentinel */ }
313 };
314 MODULE_DEVICE_TABLE(of, hd44780_of_match);
315
316 static struct platform_driver hd44780_driver = {
317         .probe = hd44780_probe,
318         .remove = hd44780_remove,
319         .driver         = {
320                 .name   = "hd44780",
321                 .of_match_table = hd44780_of_match,
322         },
323 };
324
325 module_platform_driver(hd44780_driver);
326 MODULE_DESCRIPTION("HD44780 Character LCD driver");
327 MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
328 MODULE_LICENSE("GPL");