hd44780_common.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/hex.h>
  3. #include <linux/module.h>
  4. #include <linux/sched.h>
  5. #include <linux/slab.h>
  6. #include "charlcd.h"
  7. #include "hd44780_common.h"
  8. /* LCD commands */
  9. #define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
  10. #define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
  11. #define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
  12. #define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
  13. #define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
  14. #define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
  15. #define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
  16. #define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
  17. #define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
  18. #define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
  19. #define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
  20. #define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
  21. #define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
  22. #define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
  23. #define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
  24. #define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
  25. /* sleeps that many milliseconds with a reschedule */
  26. static void long_sleep(int ms)
  27. {
  28. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  29. }
  30. int hd44780_common_print(struct charlcd *lcd, int c)
  31. {
  32. struct hd44780_common *hdc = lcd->drvdata;
  33. if (lcd->addr.x < hdc->bwidth) {
  34. hdc->write_data(hdc, c);
  35. return 0;
  36. }
  37. return 1;
  38. }
  39. EXPORT_SYMBOL_GPL(hd44780_common_print);
  40. int hd44780_common_gotoxy(struct charlcd *lcd, unsigned int x, unsigned int y)
  41. {
  42. struct hd44780_common *hdc = lcd->drvdata;
  43. unsigned int addr;
  44. /*
  45. * we force the cursor to stay at the end of the
  46. * line if it wants to go farther
  47. */
  48. addr = x < hdc->bwidth ? x & (hdc->hwidth - 1) : hdc->bwidth - 1;
  49. if (y & 1)
  50. addr += hdc->hwidth;
  51. if (y & 2)
  52. addr += hdc->bwidth;
  53. hdc->write_cmd(hdc, LCD_CMD_SET_DDRAM_ADDR | addr);
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(hd44780_common_gotoxy);
  57. int hd44780_common_home(struct charlcd *lcd)
  58. {
  59. return hd44780_common_gotoxy(lcd, 0, 0);
  60. }
  61. EXPORT_SYMBOL_GPL(hd44780_common_home);
  62. /* clears the display and resets X/Y */
  63. int hd44780_common_clear_display(struct charlcd *lcd)
  64. {
  65. struct hd44780_common *hdc = lcd->drvdata;
  66. hdc->write_cmd(hdc, LCD_CMD_DISPLAY_CLEAR);
  67. /* datasheet says to wait 1,64 milliseconds */
  68. long_sleep(2);
  69. /*
  70. * The Hitachi HD44780 controller (and compatible ones) reset the DDRAM
  71. * address when executing the DISPLAY_CLEAR command, thus the
  72. * following call is not required. However, other controllers do not
  73. * (e.g. NewHaven NHD-0220DZW-AG5), thus move the cursor to home
  74. * unconditionally to support both.
  75. */
  76. return hd44780_common_home(lcd);
  77. }
  78. EXPORT_SYMBOL_GPL(hd44780_common_clear_display);
  79. int hd44780_common_init_display(struct charlcd *lcd)
  80. {
  81. struct hd44780_common *hdc = lcd->drvdata;
  82. void (*write_cmd_raw)(struct hd44780_common *hdc, int cmd);
  83. u8 init;
  84. if (hdc->ifwidth != 4 && hdc->ifwidth != 8)
  85. return -EINVAL;
  86. hdc->hd44780_common_flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) |
  87. LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
  88. long_sleep(20); /* wait 20 ms after power-up for the paranoid */
  89. /*
  90. * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
  91. * the LCD is in 8-bit mode afterwards
  92. */
  93. init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
  94. if (hdc->ifwidth == 4) {
  95. init >>= 4;
  96. write_cmd_raw = hdc->write_cmd_raw4;
  97. } else {
  98. write_cmd_raw = hdc->write_cmd;
  99. }
  100. write_cmd_raw(hdc, init);
  101. long_sleep(10);
  102. write_cmd_raw(hdc, init);
  103. long_sleep(10);
  104. write_cmd_raw(hdc, init);
  105. long_sleep(10);
  106. if (hdc->ifwidth == 4) {
  107. /* Switch to 4-bit mode, 1 line, small fonts */
  108. hdc->write_cmd_raw4(hdc, LCD_CMD_FUNCTION_SET >> 4);
  109. long_sleep(10);
  110. }
  111. /* set font height and lines number */
  112. hdc->write_cmd(hdc,
  113. LCD_CMD_FUNCTION_SET |
  114. ((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
  115. ((hdc->hd44780_common_flags & LCD_FLAG_F) ?
  116. LCD_CMD_FONT_5X10_DOTS : 0) |
  117. ((hdc->hd44780_common_flags & LCD_FLAG_N) ?
  118. LCD_CMD_TWO_LINES : 0));
  119. long_sleep(10);
  120. /* display off, cursor off, blink off */
  121. hdc->write_cmd(hdc, LCD_CMD_DISPLAY_CTRL);
  122. long_sleep(10);
  123. hdc->write_cmd(hdc,
  124. LCD_CMD_DISPLAY_CTRL | /* set display mode */
  125. ((hdc->hd44780_common_flags & LCD_FLAG_D) ?
  126. LCD_CMD_DISPLAY_ON : 0) |
  127. ((hdc->hd44780_common_flags & LCD_FLAG_C) ?
  128. LCD_CMD_CURSOR_ON : 0) |
  129. ((hdc->hd44780_common_flags & LCD_FLAG_B) ?
  130. LCD_CMD_BLINK_ON : 0));
  131. charlcd_backlight(lcd,
  132. (hdc->hd44780_common_flags & LCD_FLAG_L) ? 1 : 0);
  133. long_sleep(10);
  134. /* entry mode set : increment, cursor shifting */
  135. hdc->write_cmd(hdc, LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
  136. hd44780_common_clear_display(lcd);
  137. return 0;
  138. }
  139. EXPORT_SYMBOL_GPL(hd44780_common_init_display);
  140. int hd44780_common_shift_cursor(struct charlcd *lcd, enum charlcd_shift_dir dir)
  141. {
  142. struct hd44780_common *hdc = lcd->drvdata;
  143. if (dir == CHARLCD_SHIFT_LEFT) {
  144. /* back one char if not at end of line */
  145. if (lcd->addr.x < hdc->bwidth)
  146. hdc->write_cmd(hdc, LCD_CMD_SHIFT);
  147. } else if (dir == CHARLCD_SHIFT_RIGHT) {
  148. /* allow the cursor to pass the end of the line */
  149. if (lcd->addr.x < (hdc->bwidth - 1))
  150. hdc->write_cmd(hdc,
  151. LCD_CMD_SHIFT | LCD_CMD_SHIFT_RIGHT);
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(hd44780_common_shift_cursor);
  156. int hd44780_common_shift_display(struct charlcd *lcd,
  157. enum charlcd_shift_dir dir)
  158. {
  159. struct hd44780_common *hdc = lcd->drvdata;
  160. if (dir == CHARLCD_SHIFT_LEFT)
  161. hdc->write_cmd(hdc, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
  162. else if (dir == CHARLCD_SHIFT_RIGHT)
  163. hdc->write_cmd(hdc, LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
  164. LCD_CMD_SHIFT_RIGHT);
  165. return 0;
  166. }
  167. EXPORT_SYMBOL_GPL(hd44780_common_shift_display);
  168. static void hd44780_common_set_mode(struct hd44780_common *hdc)
  169. {
  170. hdc->write_cmd(hdc,
  171. LCD_CMD_DISPLAY_CTRL |
  172. ((hdc->hd44780_common_flags & LCD_FLAG_D) ?
  173. LCD_CMD_DISPLAY_ON : 0) |
  174. ((hdc->hd44780_common_flags & LCD_FLAG_C) ?
  175. LCD_CMD_CURSOR_ON : 0) |
  176. ((hdc->hd44780_common_flags & LCD_FLAG_B) ?
  177. LCD_CMD_BLINK_ON : 0));
  178. }
  179. int hd44780_common_display(struct charlcd *lcd, enum charlcd_onoff on)
  180. {
  181. struct hd44780_common *hdc = lcd->drvdata;
  182. if (on == CHARLCD_ON)
  183. hdc->hd44780_common_flags |= LCD_FLAG_D;
  184. else
  185. hdc->hd44780_common_flags &= ~LCD_FLAG_D;
  186. hd44780_common_set_mode(hdc);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(hd44780_common_display);
  190. int hd44780_common_cursor(struct charlcd *lcd, enum charlcd_onoff on)
  191. {
  192. struct hd44780_common *hdc = lcd->drvdata;
  193. if (on == CHARLCD_ON)
  194. hdc->hd44780_common_flags |= LCD_FLAG_C;
  195. else
  196. hdc->hd44780_common_flags &= ~LCD_FLAG_C;
  197. hd44780_common_set_mode(hdc);
  198. return 0;
  199. }
  200. EXPORT_SYMBOL_GPL(hd44780_common_cursor);
  201. int hd44780_common_blink(struct charlcd *lcd, enum charlcd_onoff on)
  202. {
  203. struct hd44780_common *hdc = lcd->drvdata;
  204. if (on == CHARLCD_ON)
  205. hdc->hd44780_common_flags |= LCD_FLAG_B;
  206. else
  207. hdc->hd44780_common_flags &= ~LCD_FLAG_B;
  208. hd44780_common_set_mode(hdc);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(hd44780_common_blink);
  212. static void hd44780_common_set_function(struct hd44780_common *hdc)
  213. {
  214. hdc->write_cmd(hdc,
  215. LCD_CMD_FUNCTION_SET |
  216. ((hdc->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
  217. ((hdc->hd44780_common_flags & LCD_FLAG_F) ?
  218. LCD_CMD_FONT_5X10_DOTS : 0) |
  219. ((hdc->hd44780_common_flags & LCD_FLAG_N) ?
  220. LCD_CMD_TWO_LINES : 0));
  221. }
  222. int hd44780_common_fontsize(struct charlcd *lcd, enum charlcd_fontsize size)
  223. {
  224. struct hd44780_common *hdc = lcd->drvdata;
  225. if (size == CHARLCD_FONTSIZE_LARGE)
  226. hdc->hd44780_common_flags |= LCD_FLAG_F;
  227. else
  228. hdc->hd44780_common_flags &= ~LCD_FLAG_F;
  229. hd44780_common_set_function(hdc);
  230. return 0;
  231. }
  232. EXPORT_SYMBOL_GPL(hd44780_common_fontsize);
  233. int hd44780_common_lines(struct charlcd *lcd, enum charlcd_lines lines)
  234. {
  235. struct hd44780_common *hdc = lcd->drvdata;
  236. if (lines == CHARLCD_LINES_2)
  237. hdc->hd44780_common_flags |= LCD_FLAG_N;
  238. else
  239. hdc->hd44780_common_flags &= ~LCD_FLAG_N;
  240. hd44780_common_set_function(hdc);
  241. return 0;
  242. }
  243. EXPORT_SYMBOL_GPL(hd44780_common_lines);
  244. int hd44780_common_redefine_char(struct charlcd *lcd, char *esc)
  245. {
  246. /* Generator : LGcxxxxx...xx; must have <c> between '0'
  247. * and '7', representing the numerical ASCII code of the
  248. * redefined character, and <xx...xx> a sequence of 16
  249. * hex digits representing 8 bytes for each character.
  250. * Most LCDs will only use 5 lower bits of the 7 first
  251. * bytes.
  252. */
  253. struct hd44780_common *hdc = lcd->drvdata;
  254. unsigned char cgbytes[8];
  255. unsigned char cgaddr;
  256. int cgoffset;
  257. int shift;
  258. char value;
  259. int addr;
  260. if (!strchr(esc, ';'))
  261. return 0;
  262. esc++;
  263. cgaddr = *(esc++) - '0';
  264. if (cgaddr > 7)
  265. return 1;
  266. cgoffset = 0;
  267. shift = 0;
  268. value = 0;
  269. while (*esc && cgoffset < 8) {
  270. int half;
  271. shift ^= 4;
  272. half = hex_to_bin(*esc++);
  273. if (half < 0)
  274. continue;
  275. value |= half << shift;
  276. if (shift == 0) {
  277. cgbytes[cgoffset++] = value;
  278. value = 0;
  279. }
  280. }
  281. hdc->write_cmd(hdc, LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
  282. for (addr = 0; addr < cgoffset; addr++)
  283. hdc->write_data(hdc, cgbytes[addr]);
  284. /* ensures that we stop writing to CGRAM */
  285. lcd->ops->gotoxy(lcd, lcd->addr.x, lcd->addr.y);
  286. return 1;
  287. }
  288. EXPORT_SYMBOL_GPL(hd44780_common_redefine_char);
  289. struct charlcd *hd44780_common_alloc(void)
  290. {
  291. struct hd44780_common *hdc;
  292. struct charlcd *lcd;
  293. lcd = charlcd_alloc(sizeof(*hdc));
  294. if (!lcd)
  295. return NULL;
  296. hdc = lcd->drvdata;
  297. hdc->ifwidth = 8;
  298. hdc->bwidth = DEFAULT_LCD_BWIDTH;
  299. hdc->hwidth = DEFAULT_LCD_HWIDTH;
  300. return lcd;
  301. }
  302. EXPORT_SYMBOL_GPL(hd44780_common_alloc);
  303. void hd44780_common_free(struct charlcd *lcd)
  304. {
  305. charlcd_free(lcd);
  306. }
  307. EXPORT_SYMBOL_GPL(hd44780_common_free);
  308. MODULE_DESCRIPTION("Common functions for HD44780 (and compatibles) LCD displays");
  309. MODULE_LICENSE("GPL");