xref: /linux/drivers/auxdisplay/lcd2s.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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 
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 
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 
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 
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 
103 	lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
104 	return 0;
105 }
106 
107 static int lcd2s_gotoxy(struct charlcd *lcd, unsigned int x, unsigned int y)
108 {
109 	struct lcd2s_data *lcd2s = lcd->drvdata;
110 	u8 buf[3] = { LCD2S_CMD_CUR_POS, y + 1, x + 1 };
111 
112 	lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
113 
114 	return 0;
115 }
116 
117 static int lcd2s_home(struct charlcd *lcd)
118 {
119 	struct lcd2s_data *lcd2s = lcd->drvdata;
120 
121 	lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_RESET);
122 	return 0;
123 }
124 
125 static int lcd2s_init_display(struct charlcd *lcd)
126 {
127 	struct lcd2s_data *lcd2s = lcd->drvdata;
128 
129 	/* turn everything off, but display on */
130 	lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_ON);
131 	lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_OFF);
132 	lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_MOVES_FWD);
133 	lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_OFF);
134 	lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_OFF);
135 	lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CLEAR);
136 
137 	return 0;
138 }
139 
140 static int lcd2s_shift_cursor(struct charlcd *lcd, enum charlcd_shift_dir dir)
141 {
142 	struct lcd2s_data *lcd2s = lcd->drvdata;
143 
144 	if (dir == CHARLCD_SHIFT_LEFT)
145 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_MOV_CUR_LEFT);
146 	else
147 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_MOV_CUR_RIGHT);
148 
149 	return 0;
150 }
151 
152 static int lcd2s_shift_display(struct charlcd *lcd, enum charlcd_shift_dir dir)
153 {
154 	struct lcd2s_data *lcd2s = lcd->drvdata;
155 
156 	if (dir == CHARLCD_SHIFT_LEFT)
157 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_SHIFT_LEFT);
158 	else
159 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_SHIFT_RIGHT);
160 
161 	return 0;
162 }
163 
164 static void lcd2s_backlight(struct charlcd *lcd, enum charlcd_onoff on)
165 {
166 	struct lcd2s_data *lcd2s = lcd->drvdata;
167 
168 	if (on)
169 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_ON);
170 	else
171 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_BACKLIGHT_OFF);
172 }
173 
174 static int lcd2s_display(struct charlcd *lcd, enum charlcd_onoff on)
175 {
176 	struct lcd2s_data *lcd2s = lcd->drvdata;
177 
178 	if (on)
179 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_ON);
180 	else
181 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_DISPLAY_OFF);
182 
183 	return 0;
184 }
185 
186 static int lcd2s_cursor(struct charlcd *lcd, enum charlcd_onoff on)
187 {
188 	struct lcd2s_data *lcd2s = lcd->drvdata;
189 
190 	if (on)
191 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_ON);
192 	else
193 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_UL_OFF);
194 
195 	return 0;
196 }
197 
198 static int lcd2s_blink(struct charlcd *lcd, enum charlcd_onoff on)
199 {
200 	struct lcd2s_data *lcd2s = lcd->drvdata;
201 
202 	if (on)
203 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_ON);
204 	else
205 		lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CUR_BLINK_OFF);
206 
207 	return 0;
208 }
209 
210 static int lcd2s_fontsize(struct charlcd *lcd, enum charlcd_fontsize size)
211 {
212 	return 0;
213 }
214 
215 static int lcd2s_lines(struct charlcd *lcd, enum charlcd_lines lines)
216 {
217 	return 0;
218 }
219 
220 /*
221  * Generator: LGcxxxxx...xx; must have <c> between '0' and '7',
222  * representing the numerical ASCII code of the redefined character,
223  * and <xx...xx> a sequence of 16 hex digits representing 8 bytes
224  * for each character. Most LCDs will only use 5 lower bits of
225  * the 7 first bytes.
226  */
227 static int lcd2s_redefine_char(struct charlcd *lcd, char *esc)
228 {
229 	struct lcd2s_data *lcd2s = lcd->drvdata;
230 	u8 buf[LCD2S_CHARACTER_SIZE + 2] = { LCD2S_CMD_DEF_CUSTOM_CHAR };
231 	u8 value;
232 	int shift, i;
233 
234 	if (!strchr(esc, ';'))
235 		return 0;
236 
237 	esc++;
238 
239 	buf[1] = *(esc++) - '0';
240 	if (buf[1] > 7)
241 		return 1;
242 
243 	i = 2;
244 	shift = 0;
245 	value = 0;
246 	while (*esc && i < LCD2S_CHARACTER_SIZE + 2) {
247 		int half;
248 
249 		shift ^= 4;
250 		half = hex_to_bin(*esc++);
251 		if (half < 0)
252 			continue;
253 
254 		value |= half << shift;
255 		if (shift == 0) {
256 			buf[i++] = value;
257 			value = 0;
258 		}
259 	}
260 
261 	lcd2s_i2c_master_send(lcd2s->i2c, buf, sizeof(buf));
262 	return 1;
263 }
264 
265 static int lcd2s_clear_display(struct charlcd *lcd)
266 {
267 	struct lcd2s_data *lcd2s = lcd->drvdata;
268 
269 	/* This implicitly sets cursor to first row and column */
270 	lcd2s_i2c_smbus_write_byte(lcd2s->i2c, LCD2S_CMD_CLEAR);
271 	return 0;
272 }
273 
274 static const struct charlcd_ops lcd2s_ops = {
275 	.print		= lcd2s_print,
276 	.backlight	= lcd2s_backlight,
277 	.gotoxy		= lcd2s_gotoxy,
278 	.home		= lcd2s_home,
279 	.clear_display	= lcd2s_clear_display,
280 	.init_display	= lcd2s_init_display,
281 	.shift_cursor	= lcd2s_shift_cursor,
282 	.shift_display	= lcd2s_shift_display,
283 	.display	= lcd2s_display,
284 	.cursor		= lcd2s_cursor,
285 	.blink		= lcd2s_blink,
286 	.fontsize	= lcd2s_fontsize,
287 	.lines		= lcd2s_lines,
288 	.redefine_char	= lcd2s_redefine_char,
289 };
290 
291 static int lcd2s_i2c_probe(struct i2c_client *i2c)
292 {
293 	struct charlcd *lcd;
294 	struct lcd2s_data *lcd2s;
295 	int err;
296 
297 	if (!i2c_check_functionality(i2c->adapter,
298 			I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
299 			I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
300 		return -EIO;
301 
302 	/* Test, if the display is responding */
303 	err = lcd2s_i2c_smbus_write_byte(i2c, LCD2S_CMD_DISPLAY_OFF);
304 	if (err < 0)
305 		return err;
306 
307 	lcd = charlcd_alloc(sizeof(*lcd2s));
308 	if (!lcd)
309 		return -ENOMEM;
310 
311 	lcd->ops = &lcd2s_ops;
312 
313 	lcd2s = lcd->drvdata;
314 	lcd2s->i2c = i2c;
315 	lcd2s->charlcd = lcd;
316 
317 	/* Required properties */
318 	err = device_property_read_u32(&i2c->dev, "display-height-chars",
319 			&lcd->height);
320 	if (err)
321 		goto fail1;
322 
323 	err = device_property_read_u32(&i2c->dev, "display-width-chars",
324 			&lcd->width);
325 	if (err)
326 		goto fail1;
327 
328 	err = charlcd_register(lcd2s->charlcd);
329 	if (err)
330 		goto fail1;
331 
332 	i2c_set_clientdata(i2c, lcd2s);
333 	return 0;
334 
335 fail1:
336 	charlcd_free(lcd2s->charlcd);
337 	return err;
338 }
339 
340 static void lcd2s_i2c_remove(struct i2c_client *i2c)
341 {
342 	struct lcd2s_data *lcd2s = i2c_get_clientdata(i2c);
343 
344 	charlcd_unregister(lcd2s->charlcd);
345 	charlcd_free(lcd2s->charlcd);
346 }
347 
348 static const struct i2c_device_id lcd2s_i2c_id[] = {
349 	{ "lcd2s" },
350 	{ }
351 };
352 MODULE_DEVICE_TABLE(i2c, lcd2s_i2c_id);
353 
354 static const struct of_device_id lcd2s_of_table[] = {
355 	{ .compatible = "modtronix,lcd2s" },
356 	{ }
357 };
358 MODULE_DEVICE_TABLE(of, lcd2s_of_table);
359 
360 static struct i2c_driver lcd2s_i2c_driver = {
361 	.driver = {
362 		.name = "lcd2s",
363 		.of_match_table = lcd2s_of_table,
364 	},
365 	.probe = lcd2s_i2c_probe,
366 	.remove = lcd2s_i2c_remove,
367 	.id_table = lcd2s_i2c_id,
368 };
369 module_i2c_driver(lcd2s_i2c_driver);
370 
371 MODULE_DESCRIPTION("LCD2S character display driver");
372 MODULE_AUTHOR("Lars Poeschel");
373 MODULE_LICENSE("GPL");
374