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
23 #include "charlcd.h"
24
25 #define LCD2S_CMD_CUR_MOVES_FWD 0x09
26 #define LCD2S_CMD_CUR_BLINK_OFF 0x10
27 #define LCD2S_CMD_CUR_UL_OFF 0x11
28 #define LCD2S_CMD_DISPLAY_OFF 0x12
29 #define LCD2S_CMD_CUR_BLINK_ON 0x18
30 #define LCD2S_CMD_CUR_UL_ON 0x19
31 #define LCD2S_CMD_DISPLAY_ON 0x1a
32 #define LCD2S_CMD_BACKLIGHT_OFF 0x20
33 #define LCD2S_CMD_BACKLIGHT_ON 0x28
34 #define LCD2S_CMD_WRITE 0x80
35 #define LCD2S_CMD_MOV_CUR_RIGHT 0x83
36 #define LCD2S_CMD_MOV_CUR_LEFT 0x84
37 #define LCD2S_CMD_SHIFT_RIGHT 0x85
38 #define LCD2S_CMD_SHIFT_LEFT 0x86
39 #define LCD2S_CMD_SHIFT_UP 0x87
40 #define LCD2S_CMD_SHIFT_DOWN 0x88
41 #define LCD2S_CMD_CUR_ADDR 0x89
42 #define LCD2S_CMD_CUR_POS 0x8a
43 #define LCD2S_CMD_CUR_RESET 0x8b
44 #define LCD2S_CMD_CLEAR 0x8c
45 #define LCD2S_CMD_DEF_CUSTOM_CHAR 0x92
46 #define LCD2S_CMD_READ_STATUS 0xd0
47
48 #define LCD2S_CHARACTER_SIZE 8
49
50 #define LCD2S_STATUS_BUF_MASK 0x7f
51
52 struct lcd2s_data {
53 struct i2c_client *i2c;
54 struct charlcd *charlcd;
55 };
56
lcd2s_wait_buf_free(const struct i2c_client * client,int count)57 static s32 lcd2s_wait_buf_free(const struct i2c_client *client, int count)
58 {
59 s32 status;
60
61 status = i2c_smbus_read_byte_data(client, LCD2S_CMD_READ_STATUS);
62 if (status < 0)
63 return status;
64
65 while ((status & LCD2S_STATUS_BUF_MASK) < count) {
66 mdelay(1);
67 status = i2c_smbus_read_byte_data(client,
68 LCD2S_CMD_READ_STATUS);
69 if (status < 0)
70 return status;
71 }
72 return 0;
73 }
74
lcd2s_i2c_master_send(const struct i2c_client * client,const char * buf,int count)75 static int lcd2s_i2c_master_send(const struct i2c_client *client,
76 const char *buf, int count)
77 {
78 s32 status;
79
80 status = lcd2s_wait_buf_free(client, count);
81 if (status < 0)
82 return status;
83
84 return i2c_master_send(client, buf, count);
85 }
86
lcd2s_i2c_smbus_write_byte(const struct i2c_client * client,u8 value)87 static int lcd2s_i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
88 {
89 s32 status;
90
91 status = lcd2s_wait_buf_free(client, 1);
92 if (status < 0)
93 return status;
94
95 return i2c_smbus_write_byte(client, value);
96 }
97
lcd2s_print(struct charlcd * lcd,int c)98 static int lcd2s_print(struct charlcd *lcd, int c)
99 {
100 struct lcd2s_data *lcd2s = lcd->drvdata;
101 u8 buf[2] = { LCD2S_CMD_WRITE, c };
102 int ret;
103
104 ret = lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
105 if (ret < 0)
106 return ret;
107 if (ret != sizeof(buf))
108 return -EIO;
109 return 0;
110 }
111
lcd2s_gotoxy(struct charlcd * lcd,unsigned int x,unsigned int y)112 static int lcd2s_gotoxy(struct charlcd *lcd, unsigned int x, unsigned int y)
113 {
114 struct lcd2s_data *lcd2s = lcd->drvdata;
115 u8 buf[3] = { LCD2S_CMD_CUR_POS, y + 1, x + 1 };
116 int ret;
117
118 ret = lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
119 if (ret < 0)
120 return ret;
121 if (ret != sizeof(buf))
122 return -EIO;
123 return 0;
124 }
125
lcd2s_home(struct charlcd * lcd)126 static int lcd2s_home(struct charlcd *lcd)
127 {
128 struct lcd2s_data *lcd2s = lcd->drvdata;
129
130 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_RESET);
131 return 0;
132 }
133
lcd2s_init_display(struct charlcd * lcd)134 static int lcd2s_init_display(struct charlcd *lcd)
135 {
136 struct lcd2s_data *lcd2s = lcd->drvdata;
137
138 /* turn everything off, but display on */
139 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_ON);
140 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_OFF);
141 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_MOVES_FWD);
142 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_OFF);
143 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_OFF);
144 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CLEAR);
145
146 return 0;
147 }
148
lcd2s_shift_cursor(struct charlcd * lcd,enum charlcd_shift_dir dir)149 static int lcd2s_shift_cursor(struct charlcd *lcd, enum charlcd_shift_dir dir)
150 {
151 struct lcd2s_data *lcd2s = lcd->drvdata;
152
153 if (dir == CHARLCD_SHIFT_LEFT)
154 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_MOV_CUR_LEFT);
155 else
156 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_MOV_CUR_RIGHT);
157
158 return 0;
159 }
160
lcd2s_shift_display(struct charlcd * lcd,enum charlcd_shift_dir dir)161 static int lcd2s_shift_display(struct charlcd *lcd, enum charlcd_shift_dir dir)
162 {
163 struct lcd2s_data *lcd2s = lcd->drvdata;
164
165 if (dir == CHARLCD_SHIFT_LEFT)
166 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_SHIFT_LEFT);
167 else
168 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_SHIFT_RIGHT);
169
170 return 0;
171 }
172
lcd2s_backlight(struct charlcd * lcd,enum charlcd_onoff on)173 static void lcd2s_backlight(struct charlcd *lcd, enum charlcd_onoff on)
174 {
175 struct lcd2s_data *lcd2s = lcd->drvdata;
176
177 if (on)
178 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_ON);
179 else
180 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_OFF);
181 }
182
lcd2s_display(struct charlcd * lcd,enum charlcd_onoff on)183 static int lcd2s_display(struct charlcd *lcd, enum charlcd_onoff on)
184 {
185 struct lcd2s_data *lcd2s = lcd->drvdata;
186
187 if (on)
188 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_ON);
189 else
190 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_OFF);
191
192 return 0;
193 }
194
lcd2s_cursor(struct charlcd * lcd,enum charlcd_onoff on)195 static int lcd2s_cursor(struct charlcd *lcd, enum charlcd_onoff on)
196 {
197 struct lcd2s_data *lcd2s = lcd->drvdata;
198
199 if (on)
200 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_ON);
201 else
202 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_OFF);
203
204 return 0;
205 }
206
lcd2s_blink(struct charlcd * lcd,enum charlcd_onoff on)207 static int lcd2s_blink(struct charlcd *lcd, enum charlcd_onoff on)
208 {
209 struct lcd2s_data *lcd2s = lcd->drvdata;
210
211 if (on)
212 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_ON);
213 else
214 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_OFF);
215
216 return 0;
217 }
218
lcd2s_fontsize(struct charlcd * lcd,enum charlcd_fontsize size)219 static int lcd2s_fontsize(struct charlcd *lcd, enum charlcd_fontsize size)
220 {
221 return 0;
222 }
223
lcd2s_lines(struct charlcd * lcd,enum charlcd_lines lines)224 static int lcd2s_lines(struct charlcd *lcd, enum charlcd_lines lines)
225 {
226 return 0;
227 }
228
229 /*
230 * Generator: LGcxxxxx...xx; must have <c> between '0' and '7',
231 * representing the numerical ASCII code of the redefined character,
232 * and <xx...xx> a sequence of 16 hex digits representing 8 bytes
233 * for each character. Most LCDs will only use 5 lower bits of
234 * the 7 first bytes.
235 */
lcd2s_redefine_char(struct charlcd * lcd,char * esc)236 static int lcd2s_redefine_char(struct charlcd *lcd, char *esc)
237 {
238 struct lcd2s_data *lcd2s = lcd->drvdata;
239 u8 buf[LCD2S_CHARACTER_SIZE + 2] = { LCD2S_CMD_DEF_CUSTOM_CHAR };
240 u8 value;
241 int shift, i;
242
243 if (!strchr(esc, ';'))
244 return 0;
245
246 esc++;
247
248 buf[1] = *(esc++) - '0';
249 if (buf[1] > 7)
250 return 1;
251
252 i = 2;
253 shift = 0;
254 value = 0;
255 while (*esc && i < LCD2S_CHARACTER_SIZE + 2) {
256 int half;
257
258 shift ^= 4;
259 half = hex_to_bin(*esc++);
260 if (half < 0)
261 continue;
262
263 value |= half << shift;
264 if (shift == 0) {
265 buf[i++] = value;
266 value = 0;
267 }
268 }
269
270 lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
271 return 1;
272 }
273
lcd2s_clear_display(struct charlcd * lcd)274 static int lcd2s_clear_display(struct charlcd *lcd)
275 {
276 struct lcd2s_data *lcd2s = lcd->drvdata;
277
278 /* This implicitly sets cursor to first row and column */
279 lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CLEAR);
280 return 0;
281 }
282
283 static const struct charlcd_ops lcd2s_ops = {
284 .print = lcd2s_print,
285 .backlight = lcd2s_backlight,
286 .gotoxy = lcd2s_gotoxy,
287 .home = lcd2s_home,
288 .clear_display = lcd2s_clear_display,
289 .init_display = lcd2s_init_display,
290 .shift_cursor = lcd2s_shift_cursor,
291 .shift_display = lcd2s_shift_display,
292 .display = lcd2s_display,
293 .cursor = lcd2s_cursor,
294 .blink = lcd2s_blink,
295 .fontsize = lcd2s_fontsize,
296 .lines = lcd2s_lines,
297 .redefine_char = lcd2s_redefine_char,
298 };
299
lcd2s_i2c_probe(struct i2c_client * i2c)300 static int lcd2s_i2c_probe(struct i2c_client *i2c)
301 {
302 struct charlcd *lcd;
303 struct lcd2s_data *lcd2s;
304 int err;
305
306 if (!i2c_check_functionality(i2c->adapter,
307 I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
308 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
309 return -EIO;
310
311 /* Test, if the display is responding */
312 err = lcd2s_i2c_smbus_write_byte(i2c, LCD2S_CMD_DISPLAY_OFF);
313 if (err < 0)
314 return err;
315
316 lcd = charlcd_alloc(sizeof(*lcd2s));
317 if (!lcd)
318 return -ENOMEM;
319
320 lcd->ops = &lcd2s_ops;
321
322 lcd2s = lcd->drvdata;
323 lcd2s->i2c = i2c;
324 lcd2s->charlcd = lcd;
325
326 /* Required properties */
327 err = device_property_read_u32(&i2c->dev, "display-height-chars",
328 &lcd->height);
329 if (err)
330 goto fail1;
331
332 err = device_property_read_u32(&i2c->dev, "display-width-chars",
333 &lcd->width);
334 if (err)
335 goto fail1;
336
337 err = charlcd_register(lcd2s->charlcd);
338 if (err)
339 goto fail1;
340
341 i2c_set_clientdata(i2c, lcd2s);
342 return 0;
343
344 fail1:
345 charlcd_free(lcd2s->charlcd);
346 return err;
347 }
348
lcd2s_i2c_remove(struct i2c_client * i2c)349 static void lcd2s_i2c_remove(struct i2c_client *i2c)
350 {
351 struct lcd2s_data *lcd2s = i2c_get_clientdata(i2c);
352
353 charlcd_unregister(lcd2s->charlcd);
354 charlcd_free(lcd2s->charlcd);
355 }
356
357 static const struct i2c_device_id lcd2s_i2c_id[] = {
358 { "lcd2s" },
359 { }
360 };
361 MODULE_DEVICE_TABLE(i2c, lcd2s_i2c_id);
362
363 static const struct of_device_id lcd2s_of_table[] = {
364 { .compatible = "modtronix,lcd2s" },
365 { }
366 };
367 MODULE_DEVICE_TABLE(of, lcd2s_of_table);
368
369 static struct i2c_driver lcd2s_i2c_driver = {
370 .driver = {
371 .name = "lcd2s",
372 .of_match_table = lcd2s_of_table,
373 },
374 .probe = lcd2s_i2c_probe,
375 .remove = lcd2s_i2c_remove,
376 .id_table = lcd2s_i2c_id,
377 };
378 module_i2c_driver(lcd2s_i2c_driver);
379
380 MODULE_DESCRIPTION("LCD2S character display driver");
381 MODULE_AUTHOR("Lars Poeschel");
382 MODULE_LICENSE("GPL");
383