hd44780.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/property.h>
  14. #include <linux/slab.h>
  15. #include <misc/charlcd.h>
  16. enum hd44780_pin {
  17. /* Order does matter due to writing to GPIO array subsets! */
  18. PIN_DATA0, /* Optional */
  19. PIN_DATA1, /* Optional */
  20. PIN_DATA2, /* Optional */
  21. PIN_DATA3, /* Optional */
  22. PIN_DATA4,
  23. PIN_DATA5,
  24. PIN_DATA6,
  25. PIN_DATA7,
  26. PIN_CTRL_RS,
  27. PIN_CTRL_RW, /* Optional */
  28. PIN_CTRL_E,
  29. PIN_CTRL_BL, /* Optional */
  30. PIN_NUM
  31. };
  32. struct hd44780 {
  33. struct gpio_desc *pins[PIN_NUM];
  34. };
  35. static void hd44780_backlight(struct charlcd *lcd, int on)
  36. {
  37. struct hd44780 *hd = lcd->drvdata;
  38. if (hd->pins[PIN_CTRL_BL])
  39. gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on);
  40. }
  41. static void hd44780_strobe_gpio(struct hd44780 *hd)
  42. {
  43. /* Maintain the data during 20 us before the strobe */
  44. udelay(20);
  45. gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1);
  46. /* Maintain the strobe during 40 us */
  47. udelay(40);
  48. gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0);
  49. }
  50. /* write to an LCD panel register in 8 bit GPIO mode */
  51. static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs)
  52. {
  53. int values[10]; /* for DATA[0-7], RS, RW */
  54. unsigned int i, n;
  55. for (i = 0; i < 8; i++)
  56. values[PIN_DATA0 + i] = !!(val & BIT(i));
  57. values[PIN_CTRL_RS] = rs;
  58. n = 9;
  59. if (hd->pins[PIN_CTRL_RW]) {
  60. values[PIN_CTRL_RW] = 0;
  61. n++;
  62. }
  63. /* Present the data to the port */
  64. gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], values);
  65. hd44780_strobe_gpio(hd);
  66. }
  67. /* write to an LCD panel register in 4 bit GPIO mode */
  68. static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs)
  69. {
  70. int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
  71. unsigned int i, n;
  72. /* High nibble + RS, RW */
  73. for (i = 4; i < 8; i++)
  74. values[PIN_DATA0 + i] = !!(val & BIT(i));
  75. values[PIN_CTRL_RS] = rs;
  76. n = 5;
  77. if (hd->pins[PIN_CTRL_RW]) {
  78. values[PIN_CTRL_RW] = 0;
  79. n++;
  80. }
  81. /* Present the data to the port */
  82. gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
  83. &values[PIN_DATA4]);
  84. hd44780_strobe_gpio(hd);
  85. /* Low nibble */
  86. for (i = 0; i < 4; i++)
  87. values[PIN_DATA4 + i] = !!(val & BIT(i));
  88. /* Present the data to the port */
  89. gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
  90. &values[PIN_DATA4]);
  91. hd44780_strobe_gpio(hd);
  92. }
  93. /* Send a command to the LCD panel in 8 bit GPIO mode */
  94. static void hd44780_write_cmd_gpio8(struct charlcd *lcd, int cmd)
  95. {
  96. struct hd44780 *hd = lcd->drvdata;
  97. hd44780_write_gpio8(hd, cmd, 0);
  98. /* The shortest command takes at least 120 us */
  99. udelay(120);
  100. }
  101. /* Send data to the LCD panel in 8 bit GPIO mode */
  102. static void hd44780_write_data_gpio8(struct charlcd *lcd, int data)
  103. {
  104. struct hd44780 *hd = lcd->drvdata;
  105. hd44780_write_gpio8(hd, data, 1);
  106. /* The shortest data takes at least 45 us */
  107. udelay(45);
  108. }
  109. static const struct charlcd_ops hd44780_ops_gpio8 = {
  110. .write_cmd = hd44780_write_cmd_gpio8,
  111. .write_data = hd44780_write_data_gpio8,
  112. .backlight = hd44780_backlight,
  113. };
  114. /* Send a command to the LCD panel in 4 bit GPIO mode */
  115. static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd)
  116. {
  117. struct hd44780 *hd = lcd->drvdata;
  118. hd44780_write_gpio4(hd, cmd, 0);
  119. /* The shortest command takes at least 120 us */
  120. udelay(120);
  121. }
  122. /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
  123. static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd)
  124. {
  125. int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
  126. struct hd44780 *hd = lcd->drvdata;
  127. unsigned int i, n;
  128. /* Command nibble + RS, RW */
  129. for (i = 0; i < 4; i++)
  130. values[PIN_DATA4 + i] = !!(cmd & BIT(i));
  131. values[PIN_CTRL_RS] = 0;
  132. n = 5;
  133. if (hd->pins[PIN_CTRL_RW]) {
  134. values[PIN_CTRL_RW] = 0;
  135. n++;
  136. }
  137. /* Present the data to the port */
  138. gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
  139. &values[PIN_DATA4]);
  140. hd44780_strobe_gpio(hd);
  141. }
  142. /* Send data to the LCD panel in 4 bit GPIO mode */
  143. static void hd44780_write_data_gpio4(struct charlcd *lcd, int data)
  144. {
  145. struct hd44780 *hd = lcd->drvdata;
  146. hd44780_write_gpio4(hd, data, 1);
  147. /* The shortest data takes at least 45 us */
  148. udelay(45);
  149. }
  150. static const struct charlcd_ops hd44780_ops_gpio4 = {
  151. .write_cmd = hd44780_write_cmd_gpio4,
  152. .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4,
  153. .write_data = hd44780_write_data_gpio4,
  154. .backlight = hd44780_backlight,
  155. };
  156. static int hd44780_probe(struct platform_device *pdev)
  157. {
  158. struct device *dev = &pdev->dev;
  159. unsigned int i, base;
  160. struct charlcd *lcd;
  161. struct hd44780 *hd;
  162. int ifwidth, ret;
  163. /* Required pins */
  164. ifwidth = gpiod_count(dev, "data");
  165. if (ifwidth < 0)
  166. return ifwidth;
  167. switch (ifwidth) {
  168. case 4:
  169. base = PIN_DATA4;
  170. break;
  171. case 8:
  172. base = PIN_DATA0;
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. lcd = charlcd_alloc(sizeof(struct hd44780));
  178. if (!lcd)
  179. return -ENOMEM;
  180. hd = lcd->drvdata;
  181. for (i = 0; i < ifwidth; i++) {
  182. hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i,
  183. GPIOD_OUT_LOW);
  184. if (IS_ERR(hd->pins[base + i])) {
  185. ret = PTR_ERR(hd->pins[base + i]);
  186. goto fail;
  187. }
  188. }
  189. hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
  190. if (IS_ERR(hd->pins[PIN_CTRL_E])) {
  191. ret = PTR_ERR(hd->pins[PIN_CTRL_E]);
  192. goto fail;
  193. }
  194. hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH);
  195. if (IS_ERR(hd->pins[PIN_CTRL_RS])) {
  196. ret = PTR_ERR(hd->pins[PIN_CTRL_RS]);
  197. goto fail;
  198. }
  199. /* Optional pins */
  200. hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw",
  201. GPIOD_OUT_LOW);
  202. if (IS_ERR(hd->pins[PIN_CTRL_RW])) {
  203. ret = PTR_ERR(hd->pins[PIN_CTRL_RW]);
  204. goto fail;
  205. }
  206. hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight",
  207. GPIOD_OUT_LOW);
  208. if (IS_ERR(hd->pins[PIN_CTRL_BL])) {
  209. ret = PTR_ERR(hd->pins[PIN_CTRL_BL]);
  210. goto fail;
  211. }
  212. /* Required properties */
  213. ret = device_property_read_u32(dev, "display-height-chars",
  214. &lcd->height);
  215. if (ret)
  216. goto fail;
  217. ret = device_property_read_u32(dev, "display-width-chars", &lcd->width);
  218. if (ret)
  219. goto fail;
  220. /*
  221. * On displays with more than two rows, the internal buffer width is
  222. * usually equal to the display width
  223. */
  224. if (lcd->height > 2)
  225. lcd->bwidth = lcd->width;
  226. /* Optional properties */
  227. device_property_read_u32(dev, "internal-buffer-width", &lcd->bwidth);
  228. lcd->ifwidth = ifwidth;
  229. lcd->ops = ifwidth == 8 ? &hd44780_ops_gpio8 : &hd44780_ops_gpio4;
  230. ret = charlcd_register(lcd);
  231. if (ret)
  232. goto fail;
  233. platform_set_drvdata(pdev, lcd);
  234. return 0;
  235. fail:
  236. kfree(lcd);
  237. return ret;
  238. }
  239. static int hd44780_remove(struct platform_device *pdev)
  240. {
  241. struct charlcd *lcd = platform_get_drvdata(pdev);
  242. charlcd_unregister(lcd);
  243. kfree(lcd);
  244. return 0;
  245. }
  246. static const struct of_device_id hd44780_of_match[] = {
  247. { .compatible = "hit,hd44780" },
  248. { /* sentinel */ }
  249. };
  250. MODULE_DEVICE_TABLE(of, hd44780_of_match);
  251. static struct platform_driver hd44780_driver = {
  252. .probe = hd44780_probe,
  253. .remove = hd44780_remove,
  254. .driver = {
  255. .name = "hd44780",
  256. .of_match_table = hd44780_of_match,
  257. },
  258. };
  259. module_platform_driver(hd44780_driver);
  260. MODULE_DESCRIPTION("HD44780 Character LCD driver");
  261. MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
  262. MODULE_LICENSE("GPL");