lcd2s.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Console driver for LCD2S 4x20 character displays connected through i2c.
  4. * The display also has a SPI interface, but the driver does not support
  5. * this yet.
  6. *
  7. * This is a driver allowing you to use a LCD2S 4x20 from Modtronix
  8. * engineering as auxdisplay character device.
  9. *
  10. * (C) 2019 by Lemonage Software GmbH
  11. * Author: Lars Pöschel <poeschel@lemonage.de>
  12. * All rights reserved.
  13. */
  14. #include <linux/hex.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/property.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/delay.h>
  22. #include "charlcd.h"
  23. #define LCD2S_CMD_CUR_MOVES_FWD 0x09
  24. #define LCD2S_CMD_CUR_BLINK_OFF 0x10
  25. #define LCD2S_CMD_CUR_UL_OFF 0x11
  26. #define LCD2S_CMD_DISPLAY_OFF 0x12
  27. #define LCD2S_CMD_CUR_BLINK_ON 0x18
  28. #define LCD2S_CMD_CUR_UL_ON 0x19
  29. #define LCD2S_CMD_DISPLAY_ON 0x1a
  30. #define LCD2S_CMD_BACKLIGHT_OFF 0x20
  31. #define LCD2S_CMD_BACKLIGHT_ON 0x28
  32. #define LCD2S_CMD_WRITE 0x80
  33. #define LCD2S_CMD_MOV_CUR_RIGHT 0x83
  34. #define LCD2S_CMD_MOV_CUR_LEFT 0x84
  35. #define LCD2S_CMD_SHIFT_RIGHT 0x85
  36. #define LCD2S_CMD_SHIFT_LEFT 0x86
  37. #define LCD2S_CMD_SHIFT_UP 0x87
  38. #define LCD2S_CMD_SHIFT_DOWN 0x88
  39. #define LCD2S_CMD_CUR_ADDR 0x89
  40. #define LCD2S_CMD_CUR_POS 0x8a
  41. #define LCD2S_CMD_CUR_RESET 0x8b
  42. #define LCD2S_CMD_CLEAR 0x8c
  43. #define LCD2S_CMD_DEF_CUSTOM_CHAR 0x92
  44. #define LCD2S_CMD_READ_STATUS 0xd0
  45. #define LCD2S_CHARACTER_SIZE 8
  46. #define LCD2S_STATUS_BUF_MASK 0x7f
  47. struct lcd2s_data {
  48. struct i2c_client *i2c;
  49. struct charlcd *charlcd;
  50. };
  51. static s32 lcd2s_wait_buf_free(const struct i2c_client *client, int count)
  52. {
  53. s32 status;
  54. status = i2c_smbus_read_byte_data(client, LCD2S_CMD_READ_STATUS);
  55. if (status < 0)
  56. return status;
  57. while ((status & LCD2S_STATUS_BUF_MASK) < count) {
  58. mdelay(1);
  59. status = i2c_smbus_read_byte_data(client,
  60. LCD2S_CMD_READ_STATUS);
  61. if (status < 0)
  62. return status;
  63. }
  64. return 0;
  65. }
  66. static int lcd2s_i2c_master_send(const struct i2c_client *client,
  67. const char *buf, int count)
  68. {
  69. s32 status;
  70. status = lcd2s_wait_buf_free(client, count);
  71. if (status < 0)
  72. return status;
  73. return i2c_master_send(client, buf, count);
  74. }
  75. static int lcd2s_i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
  76. {
  77. s32 status;
  78. status = lcd2s_wait_buf_free(client, 1);
  79. if (status < 0)
  80. return status;
  81. return i2c_smbus_write_byte(client, value);
  82. }
  83. static int lcd2s_print(struct charlcd *lcd, int c)
  84. {
  85. struct lcd2s_data *lcd2s = lcd->drvdata;
  86. u8 buf[2] = { LCD2S_CMD_WRITE, c };
  87. int ret;
  88. ret = lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
  89. if (ret < 0)
  90. return ret;
  91. if (ret != sizeof(buf))
  92. return -EIO;
  93. return 0;
  94. }
  95. static int lcd2s_gotoxy(struct charlcd *lcd, unsigned int x, unsigned int y)
  96. {
  97. struct lcd2s_data *lcd2s = lcd->drvdata;
  98. u8 buf[3] = { LCD2S_CMD_CUR_POS, y + 1, x + 1 };
  99. int ret;
  100. ret = lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
  101. if (ret < 0)
  102. return ret;
  103. if (ret != sizeof(buf))
  104. return -EIO;
  105. return 0;
  106. }
  107. static int lcd2s_home(struct charlcd *lcd)
  108. {
  109. struct lcd2s_data *lcd2s = lcd->drvdata;
  110. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_RESET);
  111. return 0;
  112. }
  113. static int lcd2s_init_display(struct charlcd *lcd)
  114. {
  115. struct lcd2s_data *lcd2s = lcd->drvdata;
  116. /* turn everything off, but display on */
  117. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_ON);
  118. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_OFF);
  119. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_MOVES_FWD);
  120. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_OFF);
  121. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_OFF);
  122. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CLEAR);
  123. return 0;
  124. }
  125. static int lcd2s_shift_cursor(struct charlcd *lcd, enum charlcd_shift_dir dir)
  126. {
  127. struct lcd2s_data *lcd2s = lcd->drvdata;
  128. if (dir == CHARLCD_SHIFT_LEFT)
  129. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_MOV_CUR_LEFT);
  130. else
  131. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_MOV_CUR_RIGHT);
  132. return 0;
  133. }
  134. static int lcd2s_shift_display(struct charlcd *lcd, enum charlcd_shift_dir dir)
  135. {
  136. struct lcd2s_data *lcd2s = lcd->drvdata;
  137. if (dir == CHARLCD_SHIFT_LEFT)
  138. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_SHIFT_LEFT);
  139. else
  140. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_SHIFT_RIGHT);
  141. return 0;
  142. }
  143. static void lcd2s_backlight(struct charlcd *lcd, enum charlcd_onoff on)
  144. {
  145. struct lcd2s_data *lcd2s = lcd->drvdata;
  146. if (on)
  147. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_ON);
  148. else
  149. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_OFF);
  150. }
  151. static int lcd2s_display(struct charlcd *lcd, enum charlcd_onoff on)
  152. {
  153. struct lcd2s_data *lcd2s = lcd->drvdata;
  154. if (on)
  155. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_ON);
  156. else
  157. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_OFF);
  158. return 0;
  159. }
  160. static int lcd2s_cursor(struct charlcd *lcd, enum charlcd_onoff on)
  161. {
  162. struct lcd2s_data *lcd2s = lcd->drvdata;
  163. if (on)
  164. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_ON);
  165. else
  166. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_OFF);
  167. return 0;
  168. }
  169. static int lcd2s_blink(struct charlcd *lcd, enum charlcd_onoff on)
  170. {
  171. struct lcd2s_data *lcd2s = lcd->drvdata;
  172. if (on)
  173. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_ON);
  174. else
  175. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_OFF);
  176. return 0;
  177. }
  178. static int lcd2s_fontsize(struct charlcd *lcd, enum charlcd_fontsize size)
  179. {
  180. return 0;
  181. }
  182. static int lcd2s_lines(struct charlcd *lcd, enum charlcd_lines lines)
  183. {
  184. return 0;
  185. }
  186. /*
  187. * Generator: LGcxxxxx...xx; must have <c> between '0' and '7',
  188. * representing the numerical ASCII code of the redefined character,
  189. * and <xx...xx> a sequence of 16 hex digits representing 8 bytes
  190. * for each character. Most LCDs will only use 5 lower bits of
  191. * the 7 first bytes.
  192. */
  193. static int lcd2s_redefine_char(struct charlcd *lcd, char *esc)
  194. {
  195. struct lcd2s_data *lcd2s = lcd->drvdata;
  196. u8 buf[LCD2S_CHARACTER_SIZE + 2] = { LCD2S_CMD_DEF_CUSTOM_CHAR };
  197. u8 value;
  198. int shift, i;
  199. if (!strchr(esc, ';'))
  200. return 0;
  201. esc++;
  202. buf[1] = *(esc++) - '0';
  203. if (buf[1] > 7)
  204. return 1;
  205. i = 2;
  206. shift = 0;
  207. value = 0;
  208. while (*esc && i < LCD2S_CHARACTER_SIZE + 2) {
  209. int half;
  210. shift ^= 4;
  211. half = hex_to_bin(*esc++);
  212. if (half < 0)
  213. continue;
  214. value |= half << shift;
  215. if (shift == 0) {
  216. buf[i++] = value;
  217. value = 0;
  218. }
  219. }
  220. lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
  221. return 1;
  222. }
  223. static int lcd2s_clear_display(struct charlcd *lcd)
  224. {
  225. struct lcd2s_data *lcd2s = lcd->drvdata;
  226. /* This implicitly sets cursor to first row and column */
  227. lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CLEAR);
  228. return 0;
  229. }
  230. static const struct charlcd_ops lcd2s_ops = {
  231. .print = lcd2s_print,
  232. .backlight = lcd2s_backlight,
  233. .gotoxy = lcd2s_gotoxy,
  234. .home = lcd2s_home,
  235. .clear_display = lcd2s_clear_display,
  236. .init_display = lcd2s_init_display,
  237. .shift_cursor = lcd2s_shift_cursor,
  238. .shift_display = lcd2s_shift_display,
  239. .display = lcd2s_display,
  240. .cursor = lcd2s_cursor,
  241. .blink = lcd2s_blink,
  242. .fontsize = lcd2s_fontsize,
  243. .lines = lcd2s_lines,
  244. .redefine_char = lcd2s_redefine_char,
  245. };
  246. static int lcd2s_i2c_probe(struct i2c_client *i2c)
  247. {
  248. struct charlcd *lcd;
  249. struct lcd2s_data *lcd2s;
  250. int err;
  251. if (!i2c_check_functionality(i2c->adapter,
  252. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  253. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
  254. return -EIO;
  255. /* Test, if the display is responding */
  256. err = lcd2s_i2c_smbus_write_byte(i2c, LCD2S_CMD_DISPLAY_OFF);
  257. if (err < 0)
  258. return err;
  259. lcd = charlcd_alloc(sizeof(*lcd2s));
  260. if (!lcd)
  261. return -ENOMEM;
  262. lcd->ops = &lcd2s_ops;
  263. lcd2s = lcd->drvdata;
  264. lcd2s->i2c = i2c;
  265. lcd2s->charlcd = lcd;
  266. /* Required properties */
  267. err = device_property_read_u32(&i2c->dev, "display-height-chars",
  268. &lcd->height);
  269. if (err)
  270. goto fail1;
  271. err = device_property_read_u32(&i2c->dev, "display-width-chars",
  272. &lcd->width);
  273. if (err)
  274. goto fail1;
  275. err = charlcd_register(lcd2s->charlcd);
  276. if (err)
  277. goto fail1;
  278. i2c_set_clientdata(i2c, lcd2s);
  279. return 0;
  280. fail1:
  281. charlcd_free(lcd2s->charlcd);
  282. return err;
  283. }
  284. static void lcd2s_i2c_remove(struct i2c_client *i2c)
  285. {
  286. struct lcd2s_data *lcd2s = i2c_get_clientdata(i2c);
  287. charlcd_unregister(lcd2s->charlcd);
  288. charlcd_free(lcd2s->charlcd);
  289. }
  290. static const struct i2c_device_id lcd2s_i2c_id[] = {
  291. { "lcd2s" },
  292. { }
  293. };
  294. MODULE_DEVICE_TABLE(i2c, lcd2s_i2c_id);
  295. static const struct of_device_id lcd2s_of_table[] = {
  296. { .compatible = "modtronix,lcd2s" },
  297. { }
  298. };
  299. MODULE_DEVICE_TABLE(of, lcd2s_of_table);
  300. static struct i2c_driver lcd2s_i2c_driver = {
  301. .driver = {
  302. .name = "lcd2s",
  303. .of_match_table = lcd2s_of_table,
  304. },
  305. .probe = lcd2s_i2c_probe,
  306. .remove = lcd2s_i2c_remove,
  307. .id_table = lcd2s_i2c_id,
  308. };
  309. module_i2c_driver(lcd2s_i2c_driver);
  310. MODULE_DESCRIPTION("LCD2S character display driver");
  311. MODULE_AUTHOR("Lars Poeschel");
  312. MODULE_LICENSE("GPL");