hd44780_common.c 9.4 KB

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