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