1351f683bSMiguel Ojeda // SPDX-License-Identifier: GPL-2.0+ 2d47d8836SGeert Uytterhoeven /* 3d47d8836SGeert Uytterhoeven * HD44780 Character LCD driver for Linux 4d47d8836SGeert Uytterhoeven * 5d47d8836SGeert Uytterhoeven * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu> 6d47d8836SGeert Uytterhoeven * Copyright (C) 2016-2017 Glider bvba 7d47d8836SGeert Uytterhoeven */ 8d47d8836SGeert Uytterhoeven 9d47d8836SGeert Uytterhoeven #include <linux/delay.h> 10d47d8836SGeert Uytterhoeven #include <linux/gpio/consumer.h> 11d47d8836SGeert Uytterhoeven #include <linux/module.h> 12ac316725SRandy Dunlap #include <linux/mod_devicetable.h> 13d47d8836SGeert Uytterhoeven #include <linux/platform_device.h> 14d47d8836SGeert Uytterhoeven #include <linux/property.h> 15d47d8836SGeert Uytterhoeven #include <linux/slab.h> 16d47d8836SGeert Uytterhoeven 1775354284SMasahiro Yamada #include "charlcd.h" 18718e05edSLars Poeschel #include "hd44780_common.h" 19d47d8836SGeert Uytterhoeven 20d47d8836SGeert Uytterhoeven enum hd44780_pin { 21d47d8836SGeert Uytterhoeven /* Order does matter due to writing to GPIO array subsets! */ 22d47d8836SGeert Uytterhoeven PIN_DATA0, /* Optional */ 23d47d8836SGeert Uytterhoeven PIN_DATA1, /* Optional */ 24d47d8836SGeert Uytterhoeven PIN_DATA2, /* Optional */ 25d47d8836SGeert Uytterhoeven PIN_DATA3, /* Optional */ 26d47d8836SGeert Uytterhoeven PIN_DATA4, 27d47d8836SGeert Uytterhoeven PIN_DATA5, 28d47d8836SGeert Uytterhoeven PIN_DATA6, 29d47d8836SGeert Uytterhoeven PIN_DATA7, 30d47d8836SGeert Uytterhoeven PIN_CTRL_RS, 31d47d8836SGeert Uytterhoeven PIN_CTRL_RW, /* Optional */ 32d47d8836SGeert Uytterhoeven PIN_CTRL_E, 33d47d8836SGeert Uytterhoeven PIN_CTRL_BL, /* Optional */ 34d47d8836SGeert Uytterhoeven PIN_NUM 35d47d8836SGeert Uytterhoeven }; 36d47d8836SGeert Uytterhoeven 37d47d8836SGeert Uytterhoeven struct hd44780 { 38d47d8836SGeert Uytterhoeven struct gpio_desc *pins[PIN_NUM]; 39d47d8836SGeert Uytterhoeven }; 40d47d8836SGeert Uytterhoeven 4166ce7d5cSLars Poeschel static void hd44780_backlight(struct charlcd *lcd, enum charlcd_onoff on) 42d47d8836SGeert Uytterhoeven { 432545c1c9SLars Poeschel struct hd44780_common *hdc = lcd->drvdata; 442545c1c9SLars Poeschel struct hd44780 *hd = hdc->hd44780; 45d47d8836SGeert Uytterhoeven 46d47d8836SGeert Uytterhoeven if (hd->pins[PIN_CTRL_BL]) 47d47d8836SGeert Uytterhoeven gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on); 48d47d8836SGeert Uytterhoeven } 49d47d8836SGeert Uytterhoeven 50d47d8836SGeert Uytterhoeven static void hd44780_strobe_gpio(struct hd44780 *hd) 51d47d8836SGeert Uytterhoeven { 52d47d8836SGeert Uytterhoeven /* Maintain the data during 20 us before the strobe */ 53d47d8836SGeert Uytterhoeven udelay(20); 54d47d8836SGeert Uytterhoeven 55d47d8836SGeert Uytterhoeven gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1); 56d47d8836SGeert Uytterhoeven 57d47d8836SGeert Uytterhoeven /* Maintain the strobe during 40 us */ 58d47d8836SGeert Uytterhoeven udelay(40); 59d47d8836SGeert Uytterhoeven 60d47d8836SGeert Uytterhoeven gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0); 61d47d8836SGeert Uytterhoeven } 62d47d8836SGeert Uytterhoeven 63d47d8836SGeert Uytterhoeven /* write to an LCD panel register in 8 bit GPIO mode */ 64d47d8836SGeert Uytterhoeven static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs) 65d47d8836SGeert Uytterhoeven { 66b9762bebSJanusz Krzysztofik DECLARE_BITMAP(values, 10); /* for DATA[0-7], RS, RW */ 67b9762bebSJanusz Krzysztofik unsigned int n; 68d47d8836SGeert Uytterhoeven 69b9762bebSJanusz Krzysztofik values[0] = val; 70b9762bebSJanusz Krzysztofik __assign_bit(8, values, rs); 71b9762bebSJanusz Krzysztofik n = hd->pins[PIN_CTRL_RW] ? 10 : 9; 72d47d8836SGeert Uytterhoeven 73d47d8836SGeert Uytterhoeven /* Present the data to the port */ 7477588c14SJanusz Krzysztofik gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], NULL, values); 75d47d8836SGeert Uytterhoeven 76d47d8836SGeert Uytterhoeven hd44780_strobe_gpio(hd); 77d47d8836SGeert Uytterhoeven } 78d47d8836SGeert Uytterhoeven 79d47d8836SGeert Uytterhoeven /* write to an LCD panel register in 4 bit GPIO mode */ 80d47d8836SGeert Uytterhoeven static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs) 81d47d8836SGeert Uytterhoeven { 82b9762bebSJanusz Krzysztofik DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */ 83b9762bebSJanusz Krzysztofik unsigned int n; 84d47d8836SGeert Uytterhoeven 85d47d8836SGeert Uytterhoeven /* High nibble + RS, RW */ 86b9762bebSJanusz Krzysztofik values[0] = val >> 4; 87b9762bebSJanusz Krzysztofik __assign_bit(4, values, rs); 88b9762bebSJanusz Krzysztofik n = hd->pins[PIN_CTRL_RW] ? 6 : 5; 89d47d8836SGeert Uytterhoeven 90d47d8836SGeert Uytterhoeven /* Present the data to the port */ 9177588c14SJanusz Krzysztofik gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); 92d47d8836SGeert Uytterhoeven 93d47d8836SGeert Uytterhoeven hd44780_strobe_gpio(hd); 94d47d8836SGeert Uytterhoeven 95d47d8836SGeert Uytterhoeven /* Low nibble */ 96b9762bebSJanusz Krzysztofik values[0] &= ~0x0fUL; 97b9762bebSJanusz Krzysztofik values[0] |= val & 0x0f; 98d47d8836SGeert Uytterhoeven 99d47d8836SGeert Uytterhoeven /* Present the data to the port */ 10077588c14SJanusz Krzysztofik gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); 101d47d8836SGeert Uytterhoeven 102d47d8836SGeert Uytterhoeven hd44780_strobe_gpio(hd); 103d47d8836SGeert Uytterhoeven } 104d47d8836SGeert Uytterhoeven 105d47d8836SGeert Uytterhoeven /* Send a command to the LCD panel in 8 bit GPIO mode */ 1062c6a82f2SLars Poeschel static void hd44780_write_cmd_gpio8(struct hd44780_common *hdc, int cmd) 107d47d8836SGeert Uytterhoeven { 1082545c1c9SLars Poeschel struct hd44780 *hd = hdc->hd44780; 109d47d8836SGeert Uytterhoeven 110d47d8836SGeert Uytterhoeven hd44780_write_gpio8(hd, cmd, 0); 111d47d8836SGeert Uytterhoeven 112d47d8836SGeert Uytterhoeven /* The shortest command takes at least 120 us */ 113d47d8836SGeert Uytterhoeven udelay(120); 114d47d8836SGeert Uytterhoeven } 115d47d8836SGeert Uytterhoeven 116d47d8836SGeert Uytterhoeven /* Send data to the LCD panel in 8 bit GPIO mode */ 11771ff701bSLars Poeschel static void hd44780_write_data_gpio8(struct hd44780_common *hdc, int data) 118d47d8836SGeert Uytterhoeven { 1192545c1c9SLars Poeschel struct hd44780 *hd = hdc->hd44780; 120d47d8836SGeert Uytterhoeven 121d47d8836SGeert Uytterhoeven hd44780_write_gpio8(hd, data, 1); 122d47d8836SGeert Uytterhoeven 123d47d8836SGeert Uytterhoeven /* The shortest data takes at least 45 us */ 124d47d8836SGeert Uytterhoeven udelay(45); 125d47d8836SGeert Uytterhoeven } 126d47d8836SGeert Uytterhoeven 127d47d8836SGeert Uytterhoeven static const struct charlcd_ops hd44780_ops_gpio8 = { 128d47d8836SGeert Uytterhoeven .backlight = hd44780_backlight, 129b26deabbSLars Poeschel .print = hd44780_common_print, 130d3a2fb81SLars Poeschel .gotoxy = hd44780_common_gotoxy, 13188645a86SLars Poeschel .home = hd44780_common_home, 13245421ffeSLars Poeschel .clear_display = hd44780_common_clear_display, 133*01ec46dfSLars Poeschel .init_display = hd44780_common_init_display, 134d47d8836SGeert Uytterhoeven }; 135d47d8836SGeert Uytterhoeven 136d47d8836SGeert Uytterhoeven /* Send a command to the LCD panel in 4 bit GPIO mode */ 1372c6a82f2SLars Poeschel static void hd44780_write_cmd_gpio4(struct hd44780_common *hdc, int cmd) 138d47d8836SGeert Uytterhoeven { 1392545c1c9SLars Poeschel struct hd44780 *hd = hdc->hd44780; 140d47d8836SGeert Uytterhoeven 141d47d8836SGeert Uytterhoeven hd44780_write_gpio4(hd, cmd, 0); 142d47d8836SGeert Uytterhoeven 143d47d8836SGeert Uytterhoeven /* The shortest command takes at least 120 us */ 144d47d8836SGeert Uytterhoeven udelay(120); 145d47d8836SGeert Uytterhoeven } 146d47d8836SGeert Uytterhoeven 147d47d8836SGeert Uytterhoeven /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */ 1482c6a82f2SLars Poeschel static void hd44780_write_cmd_raw_gpio4(struct hd44780_common *hdc, int cmd) 149d47d8836SGeert Uytterhoeven { 150b9762bebSJanusz Krzysztofik DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */ 1512545c1c9SLars Poeschel struct hd44780 *hd = hdc->hd44780; 152b9762bebSJanusz Krzysztofik unsigned int n; 153d47d8836SGeert Uytterhoeven 154d47d8836SGeert Uytterhoeven /* Command nibble + RS, RW */ 155b9762bebSJanusz Krzysztofik values[0] = cmd & 0x0f; 156b9762bebSJanusz Krzysztofik n = hd->pins[PIN_CTRL_RW] ? 6 : 5; 157d47d8836SGeert Uytterhoeven 158d47d8836SGeert Uytterhoeven /* Present the data to the port */ 15977588c14SJanusz Krzysztofik gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); 160d47d8836SGeert Uytterhoeven 161d47d8836SGeert Uytterhoeven hd44780_strobe_gpio(hd); 162d47d8836SGeert Uytterhoeven } 163d47d8836SGeert Uytterhoeven 164d47d8836SGeert Uytterhoeven /* Send data to the LCD panel in 4 bit GPIO mode */ 16571ff701bSLars Poeschel static void hd44780_write_data_gpio4(struct hd44780_common *hdc, int data) 166d47d8836SGeert Uytterhoeven { 1672545c1c9SLars Poeschel struct hd44780 *hd = hdc->hd44780; 168d47d8836SGeert Uytterhoeven 169d47d8836SGeert Uytterhoeven hd44780_write_gpio4(hd, data, 1); 170d47d8836SGeert Uytterhoeven 171d47d8836SGeert Uytterhoeven /* The shortest data takes at least 45 us */ 172d47d8836SGeert Uytterhoeven udelay(45); 173d47d8836SGeert Uytterhoeven } 174d47d8836SGeert Uytterhoeven 175d47d8836SGeert Uytterhoeven static const struct charlcd_ops hd44780_ops_gpio4 = { 176d47d8836SGeert Uytterhoeven .backlight = hd44780_backlight, 177b26deabbSLars Poeschel .print = hd44780_common_print, 178d3a2fb81SLars Poeschel .gotoxy = hd44780_common_gotoxy, 17988645a86SLars Poeschel .home = hd44780_common_home, 18045421ffeSLars Poeschel .clear_display = hd44780_common_clear_display, 181*01ec46dfSLars Poeschel .init_display = hd44780_common_init_display, 182d47d8836SGeert Uytterhoeven }; 183d47d8836SGeert Uytterhoeven 184d47d8836SGeert Uytterhoeven static int hd44780_probe(struct platform_device *pdev) 185d47d8836SGeert Uytterhoeven { 186d47d8836SGeert Uytterhoeven struct device *dev = &pdev->dev; 187d47d8836SGeert Uytterhoeven unsigned int i, base; 188d47d8836SGeert Uytterhoeven struct charlcd *lcd; 189718e05edSLars Poeschel struct hd44780_common *hdc; 190d47d8836SGeert Uytterhoeven struct hd44780 *hd; 191718e05edSLars Poeschel int ifwidth, ret = -ENOMEM; 192d47d8836SGeert Uytterhoeven 193d47d8836SGeert Uytterhoeven /* Required pins */ 194d47d8836SGeert Uytterhoeven ifwidth = gpiod_count(dev, "data"); 195d47d8836SGeert Uytterhoeven if (ifwidth < 0) 196d47d8836SGeert Uytterhoeven return ifwidth; 197d47d8836SGeert Uytterhoeven 198d47d8836SGeert Uytterhoeven switch (ifwidth) { 199d47d8836SGeert Uytterhoeven case 4: 200d47d8836SGeert Uytterhoeven base = PIN_DATA4; 201d47d8836SGeert Uytterhoeven break; 202d47d8836SGeert Uytterhoeven case 8: 203d47d8836SGeert Uytterhoeven base = PIN_DATA0; 204d47d8836SGeert Uytterhoeven break; 205d47d8836SGeert Uytterhoeven default: 206d47d8836SGeert Uytterhoeven return -EINVAL; 207d47d8836SGeert Uytterhoeven } 208d47d8836SGeert Uytterhoeven 209718e05edSLars Poeschel hdc = hd44780_common_alloc(); 210718e05edSLars Poeschel if (!hdc) 211d47d8836SGeert Uytterhoeven return -ENOMEM; 212d47d8836SGeert Uytterhoeven 2132545c1c9SLars Poeschel lcd = charlcd_alloc(); 214718e05edSLars Poeschel if (!lcd) 215718e05edSLars Poeschel goto fail1; 216d47d8836SGeert Uytterhoeven 217718e05edSLars Poeschel hd = kzalloc(sizeof(struct hd44780), GFP_KERNEL); 218718e05edSLars Poeschel if (!hd) 219718e05edSLars Poeschel goto fail2; 220718e05edSLars Poeschel 221718e05edSLars Poeschel hdc->hd44780 = hd; 222718e05edSLars Poeschel lcd->drvdata = hdc; 223d47d8836SGeert Uytterhoeven for (i = 0; i < ifwidth; i++) { 224d47d8836SGeert Uytterhoeven hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i, 225d47d8836SGeert Uytterhoeven GPIOD_OUT_LOW); 226d47d8836SGeert Uytterhoeven if (IS_ERR(hd->pins[base + i])) { 227d47d8836SGeert Uytterhoeven ret = PTR_ERR(hd->pins[base + i]); 228718e05edSLars Poeschel goto fail3; 229d47d8836SGeert Uytterhoeven } 230d47d8836SGeert Uytterhoeven } 231d47d8836SGeert Uytterhoeven 232d47d8836SGeert Uytterhoeven hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); 233d47d8836SGeert Uytterhoeven if (IS_ERR(hd->pins[PIN_CTRL_E])) { 234d47d8836SGeert Uytterhoeven ret = PTR_ERR(hd->pins[PIN_CTRL_E]); 235718e05edSLars Poeschel goto fail3; 236d47d8836SGeert Uytterhoeven } 237d47d8836SGeert Uytterhoeven 238d47d8836SGeert Uytterhoeven hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH); 239d47d8836SGeert Uytterhoeven if (IS_ERR(hd->pins[PIN_CTRL_RS])) { 240d47d8836SGeert Uytterhoeven ret = PTR_ERR(hd->pins[PIN_CTRL_RS]); 241718e05edSLars Poeschel goto fail3; 242d47d8836SGeert Uytterhoeven } 243d47d8836SGeert Uytterhoeven 244d47d8836SGeert Uytterhoeven /* Optional pins */ 245d47d8836SGeert Uytterhoeven hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw", 246d47d8836SGeert Uytterhoeven GPIOD_OUT_LOW); 247d47d8836SGeert Uytterhoeven if (IS_ERR(hd->pins[PIN_CTRL_RW])) { 248d47d8836SGeert Uytterhoeven ret = PTR_ERR(hd->pins[PIN_CTRL_RW]); 249718e05edSLars Poeschel goto fail3; 250d47d8836SGeert Uytterhoeven } 251d47d8836SGeert Uytterhoeven 252d47d8836SGeert Uytterhoeven hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight", 253d47d8836SGeert Uytterhoeven GPIOD_OUT_LOW); 254d47d8836SGeert Uytterhoeven if (IS_ERR(hd->pins[PIN_CTRL_BL])) { 255d47d8836SGeert Uytterhoeven ret = PTR_ERR(hd->pins[PIN_CTRL_BL]); 256718e05edSLars Poeschel goto fail3; 257d47d8836SGeert Uytterhoeven } 258d47d8836SGeert Uytterhoeven 259d47d8836SGeert Uytterhoeven /* Required properties */ 260c7c3f096SGeert Uytterhoeven ret = device_property_read_u32(dev, "display-height-chars", 261c7c3f096SGeert Uytterhoeven &lcd->height); 262d47d8836SGeert Uytterhoeven if (ret) 263718e05edSLars Poeschel goto fail3; 264c7c3f096SGeert Uytterhoeven ret = device_property_read_u32(dev, "display-width-chars", &lcd->width); 265d47d8836SGeert Uytterhoeven if (ret) 266718e05edSLars Poeschel goto fail3; 267d47d8836SGeert Uytterhoeven 268d47d8836SGeert Uytterhoeven /* 269d47d8836SGeert Uytterhoeven * On displays with more than two rows, the internal buffer width is 270d47d8836SGeert Uytterhoeven * usually equal to the display width 271d47d8836SGeert Uytterhoeven */ 272d47d8836SGeert Uytterhoeven if (lcd->height > 2) 2732545c1c9SLars Poeschel hdc->bwidth = lcd->width; 274d47d8836SGeert Uytterhoeven 275d47d8836SGeert Uytterhoeven /* Optional properties */ 2762545c1c9SLars Poeschel device_property_read_u32(dev, "internal-buffer-width", &hdc->bwidth); 277d47d8836SGeert Uytterhoeven 2783fc04dd7SLars Poeschel hdc->ifwidth = ifwidth; 27971ff701bSLars Poeschel if (ifwidth == 8) { 28071ff701bSLars Poeschel lcd->ops = &hd44780_ops_gpio8; 28171ff701bSLars Poeschel hdc->write_data = hd44780_write_data_gpio8; 2822c6a82f2SLars Poeschel hdc->write_cmd = hd44780_write_cmd_gpio8; 28371ff701bSLars Poeschel } else { 28471ff701bSLars Poeschel lcd->ops = &hd44780_ops_gpio4; 28571ff701bSLars Poeschel hdc->write_data = hd44780_write_data_gpio4; 2862c6a82f2SLars Poeschel hdc->write_cmd = hd44780_write_cmd_gpio4; 2872c6a82f2SLars Poeschel hdc->write_cmd_raw4 = hd44780_write_cmd_raw_gpio4; 28871ff701bSLars Poeschel } 289d47d8836SGeert Uytterhoeven 290d47d8836SGeert Uytterhoeven ret = charlcd_register(lcd); 291d47d8836SGeert Uytterhoeven if (ret) 292718e05edSLars Poeschel goto fail3; 293d47d8836SGeert Uytterhoeven 294d47d8836SGeert Uytterhoeven platform_set_drvdata(pdev, lcd); 295d47d8836SGeert Uytterhoeven return 0; 296d47d8836SGeert Uytterhoeven 297718e05edSLars Poeschel fail3: 298718e05edSLars Poeschel kfree(hd); 299718e05edSLars Poeschel fail2: 300718e05edSLars Poeschel kfree(lcd); 301718e05edSLars Poeschel fail1: 302718e05edSLars Poeschel kfree(hdc); 303d47d8836SGeert Uytterhoeven return ret; 304d47d8836SGeert Uytterhoeven } 305d47d8836SGeert Uytterhoeven 306d47d8836SGeert Uytterhoeven static int hd44780_remove(struct platform_device *pdev) 307d47d8836SGeert Uytterhoeven { 308d47d8836SGeert Uytterhoeven struct charlcd *lcd = platform_get_drvdata(pdev); 309d47d8836SGeert Uytterhoeven 310718e05edSLars Poeschel kfree(lcd->drvdata); 311d47d8836SGeert Uytterhoeven charlcd_unregister(lcd); 31241c8d0adSAndy Shevchenko 313718e05edSLars Poeschel kfree(lcd); 314d47d8836SGeert Uytterhoeven return 0; 315d47d8836SGeert Uytterhoeven } 316d47d8836SGeert Uytterhoeven 317d47d8836SGeert Uytterhoeven static const struct of_device_id hd44780_of_match[] = { 318d47d8836SGeert Uytterhoeven { .compatible = "hit,hd44780" }, 319d47d8836SGeert Uytterhoeven { /* sentinel */ } 320d47d8836SGeert Uytterhoeven }; 321d47d8836SGeert Uytterhoeven MODULE_DEVICE_TABLE(of, hd44780_of_match); 322d47d8836SGeert Uytterhoeven 323d47d8836SGeert Uytterhoeven static struct platform_driver hd44780_driver = { 324d47d8836SGeert Uytterhoeven .probe = hd44780_probe, 325d47d8836SGeert Uytterhoeven .remove = hd44780_remove, 326d47d8836SGeert Uytterhoeven .driver = { 327d47d8836SGeert Uytterhoeven .name = "hd44780", 328d47d8836SGeert Uytterhoeven .of_match_table = hd44780_of_match, 329d47d8836SGeert Uytterhoeven }, 330d47d8836SGeert Uytterhoeven }; 331d47d8836SGeert Uytterhoeven 332d47d8836SGeert Uytterhoeven module_platform_driver(hd44780_driver); 333d47d8836SGeert Uytterhoeven MODULE_DESCRIPTION("HD44780 Character LCD driver"); 334d47d8836SGeert Uytterhoeven MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>"); 335d47d8836SGeert Uytterhoeven MODULE_LICENSE("GPL"); 336