Revert "locking/lockdep: Add debug_locks check in __lock_downgrade()"
[linux-stable.git] / drivers / auxdisplay / hd44780.c
blob2d927feb3db4f5069c071023dd16725bdba36a6b
1 /*
2 * HD44780 Character LCD driver for Linux
4 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
5 * Copyright (C) 2016-2017 Glider bvba
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/slab.h>
20 #include <misc/charlcd.h>
23 enum hd44780_pin {
24 /* Order does matter due to writing to GPIO array subsets! */
25 PIN_DATA0, /* Optional */
26 PIN_DATA1, /* Optional */
27 PIN_DATA2, /* Optional */
28 PIN_DATA3, /* Optional */
29 PIN_DATA4,
30 PIN_DATA5,
31 PIN_DATA6,
32 PIN_DATA7,
33 PIN_CTRL_RS,
34 PIN_CTRL_RW, /* Optional */
35 PIN_CTRL_E,
36 PIN_CTRL_BL, /* Optional */
37 PIN_NUM
40 struct hd44780 {
41 struct gpio_desc *pins[PIN_NUM];
44 static void hd44780_backlight(struct charlcd *lcd, int on)
46 struct hd44780 *hd = lcd->drvdata;
48 if (hd->pins[PIN_CTRL_BL])
49 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on);
52 static void hd44780_strobe_gpio(struct hd44780 *hd)
54 /* Maintain the data during 20 us before the strobe */
55 udelay(20);
57 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1);
59 /* Maintain the strobe during 40 us */
60 udelay(40);
62 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0);
65 /* write to an LCD panel register in 8 bit GPIO mode */
66 static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs)
68 int values[10]; /* for DATA[0-7], RS, RW */
69 unsigned int i, n;
71 for (i = 0; i < 8; i++)
72 values[PIN_DATA0 + i] = !!(val & BIT(i));
73 values[PIN_CTRL_RS] = rs;
74 n = 9;
75 if (hd->pins[PIN_CTRL_RW]) {
76 values[PIN_CTRL_RW] = 0;
77 n++;
80 /* Present the data to the port */
81 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], values);
83 hd44780_strobe_gpio(hd);
86 /* write to an LCD panel register in 4 bit GPIO mode */
87 static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs)
89 int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
90 unsigned int i, n;
92 /* High nibble + RS, RW */
93 for (i = 4; i < 8; i++)
94 values[PIN_DATA0 + i] = !!(val & BIT(i));
95 values[PIN_CTRL_RS] = rs;
96 n = 5;
97 if (hd->pins[PIN_CTRL_RW]) {
98 values[PIN_CTRL_RW] = 0;
99 n++;
102 /* Present the data to the port */
103 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
104 &values[PIN_DATA4]);
106 hd44780_strobe_gpio(hd);
108 /* Low nibble */
109 for (i = 0; i < 4; i++)
110 values[PIN_DATA4 + i] = !!(val & BIT(i));
112 /* Present the data to the port */
113 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
114 &values[PIN_DATA4]);
116 hd44780_strobe_gpio(hd);
119 /* Send a command to the LCD panel in 8 bit GPIO mode */
120 static void hd44780_write_cmd_gpio8(struct charlcd *lcd, int cmd)
122 struct hd44780 *hd = lcd->drvdata;
124 hd44780_write_gpio8(hd, cmd, 0);
126 /* The shortest command takes at least 120 us */
127 udelay(120);
130 /* Send data to the LCD panel in 8 bit GPIO mode */
131 static void hd44780_write_data_gpio8(struct charlcd *lcd, int data)
133 struct hd44780 *hd = lcd->drvdata;
135 hd44780_write_gpio8(hd, data, 1);
137 /* The shortest data takes at least 45 us */
138 udelay(45);
141 static const struct charlcd_ops hd44780_ops_gpio8 = {
142 .write_cmd = hd44780_write_cmd_gpio8,
143 .write_data = hd44780_write_data_gpio8,
144 .backlight = hd44780_backlight,
147 /* Send a command to the LCD panel in 4 bit GPIO mode */
148 static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd)
150 struct hd44780 *hd = lcd->drvdata;
152 hd44780_write_gpio4(hd, cmd, 0);
154 /* The shortest command takes at least 120 us */
155 udelay(120);
158 /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
159 static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd)
161 int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
162 struct hd44780 *hd = lcd->drvdata;
163 unsigned int i, n;
165 /* Command nibble + RS, RW */
166 for (i = 0; i < 4; i++)
167 values[PIN_DATA4 + i] = !!(cmd & BIT(i));
168 values[PIN_CTRL_RS] = 0;
169 n = 5;
170 if (hd->pins[PIN_CTRL_RW]) {
171 values[PIN_CTRL_RW] = 0;
172 n++;
175 /* Present the data to the port */
176 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
177 &values[PIN_DATA4]);
179 hd44780_strobe_gpio(hd);
182 /* Send data to the LCD panel in 4 bit GPIO mode */
183 static void hd44780_write_data_gpio4(struct charlcd *lcd, int data)
185 struct hd44780 *hd = lcd->drvdata;
187 hd44780_write_gpio4(hd, data, 1);
189 /* The shortest data takes at least 45 us */
190 udelay(45);
193 static const struct charlcd_ops hd44780_ops_gpio4 = {
194 .write_cmd = hd44780_write_cmd_gpio4,
195 .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4,
196 .write_data = hd44780_write_data_gpio4,
197 .backlight = hd44780_backlight,
200 static int hd44780_probe(struct platform_device *pdev)
202 struct device *dev = &pdev->dev;
203 unsigned int i, base;
204 struct charlcd *lcd;
205 struct hd44780 *hd;
206 int ifwidth, ret;
208 /* Required pins */
209 ifwidth = gpiod_count(dev, "data");
210 if (ifwidth < 0)
211 return ifwidth;
213 switch (ifwidth) {
214 case 4:
215 base = PIN_DATA4;
216 break;
217 case 8:
218 base = PIN_DATA0;
219 break;
220 default:
221 return -EINVAL;
224 lcd = charlcd_alloc(sizeof(struct hd44780));
225 if (!lcd)
226 return -ENOMEM;
228 hd = lcd->drvdata;
230 for (i = 0; i < ifwidth; i++) {
231 hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i,
232 GPIOD_OUT_LOW);
233 if (IS_ERR(hd->pins[base + i])) {
234 ret = PTR_ERR(hd->pins[base + i]);
235 goto fail;
239 hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
240 if (IS_ERR(hd->pins[PIN_CTRL_E])) {
241 ret = PTR_ERR(hd->pins[PIN_CTRL_E]);
242 goto fail;
245 hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH);
246 if (IS_ERR(hd->pins[PIN_CTRL_RS])) {
247 ret = PTR_ERR(hd->pins[PIN_CTRL_RS]);
248 goto fail;
251 /* Optional pins */
252 hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw",
253 GPIOD_OUT_LOW);
254 if (IS_ERR(hd->pins[PIN_CTRL_RW])) {
255 ret = PTR_ERR(hd->pins[PIN_CTRL_RW]);
256 goto fail;
259 hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight",
260 GPIOD_OUT_LOW);
261 if (IS_ERR(hd->pins[PIN_CTRL_BL])) {
262 ret = PTR_ERR(hd->pins[PIN_CTRL_BL]);
263 goto fail;
266 /* Required properties */
267 ret = device_property_read_u32(dev, "display-height-chars",
268 &lcd->height);
269 if (ret)
270 goto fail;
271 ret = device_property_read_u32(dev, "display-width-chars", &lcd->width);
272 if (ret)
273 goto fail;
276 * On displays with more than two rows, the internal buffer width is
277 * usually equal to the display width
279 if (lcd->height > 2)
280 lcd->bwidth = lcd->width;
282 /* Optional properties */
283 device_property_read_u32(dev, "internal-buffer-width", &lcd->bwidth);
285 lcd->ifwidth = ifwidth;
286 lcd->ops = ifwidth == 8 ? &hd44780_ops_gpio8 : &hd44780_ops_gpio4;
288 ret = charlcd_register(lcd);
289 if (ret)
290 goto fail;
292 platform_set_drvdata(pdev, lcd);
293 return 0;
295 fail:
296 kfree(lcd);
297 return ret;
300 static int hd44780_remove(struct platform_device *pdev)
302 struct charlcd *lcd = platform_get_drvdata(pdev);
304 charlcd_unregister(lcd);
306 kfree(lcd);
307 return 0;
310 static const struct of_device_id hd44780_of_match[] = {
311 { .compatible = "hit,hd44780" },
312 { /* sentinel */ }
314 MODULE_DEVICE_TABLE(of, hd44780_of_match);
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,
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");