hd44780.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "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. DECLARE_BITMAP(values, 10); /* for DATA[0-7], RS, RW */
  54. unsigned int n;
  55. values[0] = val;
  56. __assign_bit(8, values, rs);
  57. n = hd->pins[PIN_CTRL_RW] ? 10 : 9;
  58. /* Present the data to the port */
  59. gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], NULL, values);
  60. hd44780_strobe_gpio(hd);
  61. }
  62. /* write to an LCD panel register in 4 bit GPIO mode */
  63. static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs)
  64. {
  65. DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
  66. unsigned int n;
  67. /* High nibble + RS, RW */
  68. values[0] = val >> 4;
  69. __assign_bit(4, values, rs);
  70. n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
  71. /* Present the data to the port */
  72. gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
  73. hd44780_strobe_gpio(hd);
  74. /* Low nibble */
  75. values[0] &= ~0x0fUL;
  76. values[0] |= val & 0x0f;
  77. /* Present the data to the port */
  78. gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
  79. hd44780_strobe_gpio(hd);
  80. }
  81. /* Send a command to the LCD panel in 8 bit GPIO mode */
  82. static void hd44780_write_cmd_gpio8(struct charlcd *lcd, int cmd)
  83. {
  84. struct hd44780 *hd = lcd->drvdata;
  85. hd44780_write_gpio8(hd, cmd, 0);
  86. /* The shortest command takes at least 120 us */
  87. udelay(120);
  88. }
  89. /* Send data to the LCD panel in 8 bit GPIO mode */
  90. static void hd44780_write_data_gpio8(struct charlcd *lcd, int data)
  91. {
  92. struct hd44780 *hd = lcd->drvdata;
  93. hd44780_write_gpio8(hd, data, 1);
  94. /* The shortest data takes at least 45 us */
  95. udelay(45);
  96. }
  97. static const struct charlcd_ops hd44780_ops_gpio8 = {
  98. .write_cmd = hd44780_write_cmd_gpio8,
  99. .write_data = hd44780_write_data_gpio8,
  100. .backlight = hd44780_backlight,
  101. };
  102. /* Send a command to the LCD panel in 4 bit GPIO mode */
  103. static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd)
  104. {
  105. struct hd44780 *hd = lcd->drvdata;
  106. hd44780_write_gpio4(hd, cmd, 0);
  107. /* The shortest command takes at least 120 us */
  108. udelay(120);
  109. }
  110. /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
  111. static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd)
  112. {
  113. DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
  114. struct hd44780 *hd = lcd->drvdata;
  115. unsigned int n;
  116. /* Command nibble + RS, RW */
  117. values[0] = cmd & 0x0f;
  118. n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
  119. /* Present the data to the port */
  120. gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
  121. hd44780_strobe_gpio(hd);
  122. }
  123. /* Send data to the LCD panel in 4 bit GPIO mode */
  124. static void hd44780_write_data_gpio4(struct charlcd *lcd, int data)
  125. {
  126. struct hd44780 *hd = lcd->drvdata;
  127. hd44780_write_gpio4(hd, data, 1);
  128. /* The shortest data takes at least 45 us */
  129. udelay(45);
  130. }
  131. static const struct charlcd_ops hd44780_ops_gpio4 = {
  132. .write_cmd = hd44780_write_cmd_gpio4,
  133. .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4,
  134. .write_data = hd44780_write_data_gpio4,
  135. .backlight = hd44780_backlight,
  136. };
  137. static int hd44780_probe(struct platform_device *pdev)
  138. {
  139. struct device *dev = &pdev->dev;
  140. unsigned int i, base;
  141. struct charlcd *lcd;
  142. struct hd44780 *hd;
  143. int ifwidth, ret;
  144. /* Required pins */
  145. ifwidth = gpiod_count(dev, "data");
  146. if (ifwidth < 0)
  147. return ifwidth;
  148. switch (ifwidth) {
  149. case 4:
  150. base = PIN_DATA4;
  151. break;
  152. case 8:
  153. base = PIN_DATA0;
  154. break;
  155. default:
  156. return -EINVAL;
  157. }
  158. lcd = charlcd_alloc(sizeof(struct hd44780));
  159. if (!lcd)
  160. return -ENOMEM;
  161. hd = lcd->drvdata;
  162. for (i = 0; i < ifwidth; i++) {
  163. hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i,
  164. GPIOD_OUT_LOW);
  165. if (IS_ERR(hd->pins[base + i])) {
  166. ret = PTR_ERR(hd->pins[base + i]);
  167. goto fail;
  168. }
  169. }
  170. hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
  171. if (IS_ERR(hd->pins[PIN_CTRL_E])) {
  172. ret = PTR_ERR(hd->pins[PIN_CTRL_E]);
  173. goto fail;
  174. }
  175. hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH);
  176. if (IS_ERR(hd->pins[PIN_CTRL_RS])) {
  177. ret = PTR_ERR(hd->pins[PIN_CTRL_RS]);
  178. goto fail;
  179. }
  180. /* Optional pins */
  181. hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw",
  182. GPIOD_OUT_LOW);
  183. if (IS_ERR(hd->pins[PIN_CTRL_RW])) {
  184. ret = PTR_ERR(hd->pins[PIN_CTRL_RW]);
  185. goto fail;
  186. }
  187. hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight",
  188. GPIOD_OUT_LOW);
  189. if (IS_ERR(hd->pins[PIN_CTRL_BL])) {
  190. ret = PTR_ERR(hd->pins[PIN_CTRL_BL]);
  191. goto fail;
  192. }
  193. /* Required properties */
  194. ret = device_property_read_u32(dev, "display-height-chars",
  195. &lcd->height);
  196. if (ret)
  197. goto fail;
  198. ret = device_property_read_u32(dev, "display-width-chars", &lcd->width);
  199. if (ret)
  200. goto fail;
  201. /*
  202. * On displays with more than two rows, the internal buffer width is
  203. * usually equal to the display width
  204. */
  205. if (lcd->height > 2)
  206. lcd->bwidth = lcd->width;
  207. /* Optional properties */
  208. device_property_read_u32(dev, "internal-buffer-width", &lcd->bwidth);
  209. lcd->ifwidth = ifwidth;
  210. lcd->ops = ifwidth == 8 ? &hd44780_ops_gpio8 : &hd44780_ops_gpio4;
  211. ret = charlcd_register(lcd);
  212. if (ret)
  213. goto fail;
  214. platform_set_drvdata(pdev, lcd);
  215. return 0;
  216. fail:
  217. charlcd_free(lcd);
  218. return ret;
  219. }
  220. static int hd44780_remove(struct platform_device *pdev)
  221. {
  222. struct charlcd *lcd = platform_get_drvdata(pdev);
  223. charlcd_unregister(lcd);
  224. charlcd_free(lcd);
  225. return 0;
  226. }
  227. static const struct of_device_id hd44780_of_match[] = {
  228. { .compatible = "hit,hd44780" },
  229. { /* sentinel */ }
  230. };
  231. MODULE_DEVICE_TABLE(of, hd44780_of_match);
  232. static struct platform_driver hd44780_driver = {
  233. .probe = hd44780_probe,
  234. .remove = hd44780_remove,
  235. .driver = {
  236. .name = "hd44780",
  237. .of_match_table = hd44780_of_match,
  238. },
  239. };
  240. module_platform_driver(hd44780_driver);
  241. MODULE_DESCRIPTION("HD44780 Character LCD driver");
  242. MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
  243. MODULE_LICENSE("GPL");