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 <misc/charlcd.h> 18 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, int on) 42 { 43 struct hd44780 *hd = lcd->drvdata; 44 45 if (hd->pins[PIN_CTRL_BL]) 46 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on); 47 } 48 49 static void hd44780_strobe_gpio(struct hd44780 *hd) 50 { 51 /* Maintain the data during 20 us before the strobe */ 52 udelay(20); 53 54 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1); 55 56 /* Maintain the strobe during 40 us */ 57 udelay(40); 58 59 gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0); 60 } 61 62 /* write to an LCD panel register in 8 bit GPIO mode */ 63 static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs) 64 { 65 int values[10]; /* for DATA[0-7], RS, RW */ 66 unsigned int i, n; 67 68 for (i = 0; i < 8; i++) 69 values[PIN_DATA0 + i] = !!(val & BIT(i)); 70 values[PIN_CTRL_RS] = rs; 71 n = 9; 72 if (hd->pins[PIN_CTRL_RW]) { 73 values[PIN_CTRL_RW] = 0; 74 n++; 75 } 76 77 /* Present the data to the port */ 78 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], values); 79 80 hd44780_strobe_gpio(hd); 81 } 82 83 /* write to an LCD panel register in 4 bit GPIO mode */ 84 static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs) 85 { 86 int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */ 87 unsigned int i, n; 88 89 /* High nibble + RS, RW */ 90 for (i = 4; i < 8; i++) 91 values[PIN_DATA0 + i] = !!(val & BIT(i)); 92 values[PIN_CTRL_RS] = rs; 93 n = 5; 94 if (hd->pins[PIN_CTRL_RW]) { 95 values[PIN_CTRL_RW] = 0; 96 n++; 97 } 98 99 /* Present the data to the port */ 100 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], 101 &values[PIN_DATA4]); 102 103 hd44780_strobe_gpio(hd); 104 105 /* Low nibble */ 106 for (i = 0; i < 4; i++) 107 values[PIN_DATA4 + i] = !!(val & BIT(i)); 108 109 /* Present the data to the port */ 110 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], 111 &values[PIN_DATA4]); 112 113 hd44780_strobe_gpio(hd); 114 } 115 116 /* Send a command to the LCD panel in 8 bit GPIO mode */ 117 static void hd44780_write_cmd_gpio8(struct charlcd *lcd, int cmd) 118 { 119 struct hd44780 *hd = lcd->drvdata; 120 121 hd44780_write_gpio8(hd, cmd, 0); 122 123 /* The shortest command takes at least 120 us */ 124 udelay(120); 125 } 126 127 /* Send data to the LCD panel in 8 bit GPIO mode */ 128 static void hd44780_write_data_gpio8(struct charlcd *lcd, int data) 129 { 130 struct hd44780 *hd = lcd->drvdata; 131 132 hd44780_write_gpio8(hd, data, 1); 133 134 /* The shortest data takes at least 45 us */ 135 udelay(45); 136 } 137 138 static const struct charlcd_ops hd44780_ops_gpio8 = { 139 .write_cmd = hd44780_write_cmd_gpio8, 140 .write_data = hd44780_write_data_gpio8, 141 .backlight = hd44780_backlight, 142 }; 143 144 /* Send a command to the LCD panel in 4 bit GPIO mode */ 145 static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd) 146 { 147 struct hd44780 *hd = lcd->drvdata; 148 149 hd44780_write_gpio4(hd, cmd, 0); 150 151 /* The shortest command takes at least 120 us */ 152 udelay(120); 153 } 154 155 /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */ 156 static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd) 157 { 158 int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */ 159 struct hd44780 *hd = lcd->drvdata; 160 unsigned int i, n; 161 162 /* Command nibble + RS, RW */ 163 for (i = 0; i < 4; i++) 164 values[PIN_DATA4 + i] = !!(cmd & BIT(i)); 165 values[PIN_CTRL_RS] = 0; 166 n = 5; 167 if (hd->pins[PIN_CTRL_RW]) { 168 values[PIN_CTRL_RW] = 0; 169 n++; 170 } 171 172 /* Present the data to the port */ 173 gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], 174 &values[PIN_DATA4]); 175 176 hd44780_strobe_gpio(hd); 177 } 178 179 /* Send data to the LCD panel in 4 bit GPIO mode */ 180 static void hd44780_write_data_gpio4(struct charlcd *lcd, int data) 181 { 182 struct hd44780 *hd = lcd->drvdata; 183 184 hd44780_write_gpio4(hd, data, 1); 185 186 /* The shortest data takes at least 45 us */ 187 udelay(45); 188 } 189 190 static const struct charlcd_ops hd44780_ops_gpio4 = { 191 .write_cmd = hd44780_write_cmd_gpio4, 192 .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4, 193 .write_data = hd44780_write_data_gpio4, 194 .backlight = hd44780_backlight, 195 }; 196 197 static int hd44780_probe(struct platform_device *pdev) 198 { 199 struct device *dev = &pdev->dev; 200 unsigned int i, base; 201 struct charlcd *lcd; 202 struct hd44780 *hd; 203 int ifwidth, ret; 204 205 /* Required pins */ 206 ifwidth = gpiod_count(dev, "data"); 207 if (ifwidth < 0) 208 return ifwidth; 209 210 switch (ifwidth) { 211 case 4: 212 base = PIN_DATA4; 213 break; 214 case 8: 215 base = PIN_DATA0; 216 break; 217 default: 218 return -EINVAL; 219 } 220 221 lcd = charlcd_alloc(sizeof(struct hd44780)); 222 if (!lcd) 223 return -ENOMEM; 224 225 hd = lcd->drvdata; 226 227 for (i = 0; i < ifwidth; i++) { 228 hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i, 229 GPIOD_OUT_LOW); 230 if (IS_ERR(hd->pins[base + i])) { 231 ret = PTR_ERR(hd->pins[base + i]); 232 goto fail; 233 } 234 } 235 236 hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); 237 if (IS_ERR(hd->pins[PIN_CTRL_E])) { 238 ret = PTR_ERR(hd->pins[PIN_CTRL_E]); 239 goto fail; 240 } 241 242 hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH); 243 if (IS_ERR(hd->pins[PIN_CTRL_RS])) { 244 ret = PTR_ERR(hd->pins[PIN_CTRL_RS]); 245 goto fail; 246 } 247 248 /* Optional pins */ 249 hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw", 250 GPIOD_OUT_LOW); 251 if (IS_ERR(hd->pins[PIN_CTRL_RW])) { 252 ret = PTR_ERR(hd->pins[PIN_CTRL_RW]); 253 goto fail; 254 } 255 256 hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight", 257 GPIOD_OUT_LOW); 258 if (IS_ERR(hd->pins[PIN_CTRL_BL])) { 259 ret = PTR_ERR(hd->pins[PIN_CTRL_BL]); 260 goto fail; 261 } 262 263 /* Required properties */ 264 ret = device_property_read_u32(dev, "display-height-chars", 265 &lcd->height); 266 if (ret) 267 goto fail; 268 ret = device_property_read_u32(dev, "display-width-chars", &lcd->width); 269 if (ret) 270 goto fail; 271 272 /* 273 * On displays with more than two rows, the internal buffer width is 274 * usually equal to the display width 275 */ 276 if (lcd->height > 2) 277 lcd->bwidth = lcd->width; 278 279 /* Optional properties */ 280 device_property_read_u32(dev, "internal-buffer-width", &lcd->bwidth); 281 282 lcd->ifwidth = ifwidth; 283 lcd->ops = ifwidth == 8 ? &hd44780_ops_gpio8 : &hd44780_ops_gpio4; 284 285 ret = charlcd_register(lcd); 286 if (ret) 287 goto fail; 288 289 platform_set_drvdata(pdev, lcd); 290 return 0; 291 292 fail: 293 kfree(lcd); 294 return ret; 295 } 296 297 static int hd44780_remove(struct platform_device *pdev) 298 { 299 struct charlcd *lcd = platform_get_drvdata(pdev); 300 301 charlcd_unregister(lcd); 302 return 0; 303 } 304 305 static const struct of_device_id hd44780_of_match[] = { 306 { .compatible = "hit,hd44780" }, 307 { /* sentinel */ } 308 }; 309 MODULE_DEVICE_TABLE(of, hd44780_of_match); 310 311 static struct platform_driver hd44780_driver = { 312 .probe = hd44780_probe, 313 .remove = hd44780_remove, 314 .driver = { 315 .name = "hd44780", 316 .of_match_table = hd44780_of_match, 317 }, 318 }; 319 320 module_platform_driver(hd44780_driver); 321 MODULE_DESCRIPTION("HD44780 Character LCD driver"); 322 MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>"); 323 MODULE_LICENSE("GPL"); 324