xref: /openbmc/linux/drivers/auxdisplay/ht16k33.c (revision 877013bc)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * HT16K33 driver
4  *
5  * Author: Robin van der Gracht <robin@protonic.nl>
6  *
7  * Copyright: (C) 2016 Protonic Holland.
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/i2c.h>
14 #include <linux/of.h>
15 #include <linux/fb.h>
16 #include <linux/slab.h>
17 #include <linux/backlight.h>
18 #include <linux/input.h>
19 #include <linux/input/matrix_keypad.h>
20 #include <linux/workqueue.h>
21 #include <linux/mm.h>
22 
23 /* Registers */
24 #define REG_SYSTEM_SETUP		0x20
25 #define REG_SYSTEM_SETUP_OSC_ON		BIT(0)
26 
27 #define REG_DISPLAY_SETUP		0x80
28 #define REG_DISPLAY_SETUP_ON		BIT(0)
29 
30 #define REG_ROWINT_SET			0xA0
31 #define REG_ROWINT_SET_INT_EN		BIT(0)
32 #define REG_ROWINT_SET_INT_ACT_HIGH	BIT(1)
33 
34 #define REG_BRIGHTNESS			0xE0
35 
36 /* Defines */
37 #define DRIVER_NAME			"ht16k33"
38 
39 #define MIN_BRIGHTNESS			0x1
40 #define MAX_BRIGHTNESS			0x10
41 
42 #define HT16K33_MATRIX_LED_MAX_COLS	8
43 #define HT16K33_MATRIX_LED_MAX_ROWS	16
44 #define HT16K33_MATRIX_KEYPAD_MAX_COLS	3
45 #define HT16K33_MATRIX_KEYPAD_MAX_ROWS	12
46 
47 #define BYTES_PER_ROW		(HT16K33_MATRIX_LED_MAX_ROWS / 8)
48 #define HT16K33_FB_SIZE		(HT16K33_MATRIX_LED_MAX_COLS * BYTES_PER_ROW)
49 
50 struct ht16k33_keypad {
51 	struct i2c_client *client;
52 	struct input_dev *dev;
53 	uint32_t cols;
54 	uint32_t rows;
55 	uint32_t row_shift;
56 	uint32_t debounce_ms;
57 	uint16_t last_key_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
58 
59 	wait_queue_head_t wait;
60 	bool stopped;
61 };
62 
63 struct ht16k33_fbdev {
64 	struct fb_info *info;
65 	uint32_t refresh_rate;
66 	uint8_t *buffer;
67 	uint8_t *cache;
68 	struct delayed_work work;
69 };
70 
71 struct ht16k33_priv {
72 	struct i2c_client *client;
73 	struct ht16k33_keypad keypad;
74 	struct ht16k33_fbdev fbdev;
75 };
76 
77 static const struct fb_fix_screeninfo ht16k33_fb_fix = {
78 	.id		= DRIVER_NAME,
79 	.type		= FB_TYPE_PACKED_PIXELS,
80 	.visual		= FB_VISUAL_MONO10,
81 	.xpanstep	= 0,
82 	.ypanstep	= 0,
83 	.ywrapstep	= 0,
84 	.line_length	= HT16K33_MATRIX_LED_MAX_ROWS,
85 	.accel		= FB_ACCEL_NONE,
86 };
87 
88 static const struct fb_var_screeninfo ht16k33_fb_var = {
89 	.xres = HT16K33_MATRIX_LED_MAX_ROWS,
90 	.yres = HT16K33_MATRIX_LED_MAX_COLS,
91 	.xres_virtual = HT16K33_MATRIX_LED_MAX_ROWS,
92 	.yres_virtual = HT16K33_MATRIX_LED_MAX_COLS,
93 	.bits_per_pixel = 1,
94 	.red = { 0, 1, 0 },
95 	.green = { 0, 1, 0 },
96 	.blue = { 0, 1, 0 },
97 	.left_margin = 0,
98 	.right_margin = 0,
99 	.upper_margin = 0,
100 	.lower_margin = 0,
101 	.vmode = FB_VMODE_NONINTERLACED,
102 };
103 
104 static int ht16k33_display_on(struct ht16k33_priv *priv)
105 {
106 	uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON;
107 
108 	return i2c_smbus_write_byte(priv->client, data);
109 }
110 
111 static int ht16k33_display_off(struct ht16k33_priv *priv)
112 {
113 	return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP);
114 }
115 
116 static void ht16k33_fb_queue(struct ht16k33_priv *priv)
117 {
118 	struct ht16k33_fbdev *fbdev = &priv->fbdev;
119 
120 	schedule_delayed_work(&fbdev->work, HZ / fbdev->refresh_rate);
121 }
122 
123 /*
124  * This gets the fb data from cache and copies it to ht16k33 display RAM
125  */
126 static void ht16k33_fb_update(struct work_struct *work)
127 {
128 	struct ht16k33_fbdev *fbdev =
129 		container_of(work, struct ht16k33_fbdev, work.work);
130 	struct ht16k33_priv *priv =
131 		container_of(fbdev, struct ht16k33_priv, fbdev);
132 
133 	uint8_t *p1, *p2;
134 	int len, pos = 0, first = -1;
135 
136 	p1 = fbdev->cache;
137 	p2 = fbdev->buffer;
138 
139 	/* Search for the first byte with changes */
140 	while (pos < HT16K33_FB_SIZE && first < 0) {
141 		if (*(p1++) - *(p2++))
142 			first = pos;
143 		pos++;
144 	}
145 
146 	/* No changes found */
147 	if (first < 0)
148 		goto requeue;
149 
150 	len = HT16K33_FB_SIZE - first;
151 	p1 = fbdev->cache + HT16K33_FB_SIZE - 1;
152 	p2 = fbdev->buffer + HT16K33_FB_SIZE - 1;
153 
154 	/* Determine i2c transfer length */
155 	while (len > 1) {
156 		if (*(p1--) - *(p2--))
157 			break;
158 		len--;
159 	}
160 
161 	p1 = fbdev->cache + first;
162 	p2 = fbdev->buffer + first;
163 	if (!i2c_smbus_write_i2c_block_data(priv->client, first, len, p2))
164 		memcpy(p1, p2, len);
165 requeue:
166 	ht16k33_fb_queue(priv);
167 }
168 
169 static int ht16k33_initialize(struct ht16k33_priv *priv)
170 {
171 	uint8_t byte;
172 	int err;
173 	uint8_t data[HT16K33_MATRIX_LED_MAX_COLS * 2];
174 
175 	/* Clear RAM (8 * 16 bits) */
176 	memset(data, 0, sizeof(data));
177 	err = i2c_smbus_write_block_data(priv->client, 0, sizeof(data), data);
178 	if (err)
179 		return err;
180 
181 	/* Turn on internal oscillator */
182 	byte = REG_SYSTEM_SETUP_OSC_ON | REG_SYSTEM_SETUP;
183 	err = i2c_smbus_write_byte(priv->client, byte);
184 	if (err)
185 		return err;
186 
187 	/* Configure INT pin */
188 	byte = REG_ROWINT_SET | REG_ROWINT_SET_INT_ACT_HIGH;
189 	if (priv->client->irq > 0)
190 		byte |= REG_ROWINT_SET_INT_EN;
191 	return i2c_smbus_write_byte(priv->client, byte);
192 }
193 
194 static int ht16k33_bl_update_status(struct backlight_device *bl)
195 {
196 	int brightness = bl->props.brightness;
197 	struct ht16k33_priv *priv = bl_get_data(bl);
198 
199 	if (bl->props.power != FB_BLANK_UNBLANK ||
200 	    bl->props.fb_blank != FB_BLANK_UNBLANK ||
201 	    bl->props.state & BL_CORE_FBBLANK || brightness == 0) {
202 		return ht16k33_display_off(priv);
203 	}
204 
205 	ht16k33_display_on(priv);
206 	return i2c_smbus_write_byte(priv->client,
207 				    REG_BRIGHTNESS | (brightness - 1));
208 }
209 
210 static int ht16k33_bl_check_fb(struct backlight_device *bl, struct fb_info *fi)
211 {
212 	struct ht16k33_priv *priv = bl_get_data(bl);
213 
214 	return (fi == NULL) || (fi->par == priv);
215 }
216 
217 static const struct backlight_ops ht16k33_bl_ops = {
218 	.update_status	= ht16k33_bl_update_status,
219 	.check_fb	= ht16k33_bl_check_fb,
220 };
221 
222 static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
223 {
224 	struct ht16k33_priv *priv = info->par;
225 	struct page *pages = virt_to_page(priv->fbdev.buffer);
226 
227 	return vm_map_pages_zero(vma, &pages, 1);
228 }
229 
230 static const struct fb_ops ht16k33_fb_ops = {
231 	.owner = THIS_MODULE,
232 	.fb_read = fb_sys_read,
233 	.fb_write = fb_sys_write,
234 	.fb_fillrect = sys_fillrect,
235 	.fb_copyarea = sys_copyarea,
236 	.fb_imageblit = sys_imageblit,
237 	.fb_mmap = ht16k33_mmap,
238 };
239 
240 /*
241  * This gets the keys from keypad and reports it to input subsystem.
242  * Returns true if a key is pressed.
243  */
244 static bool ht16k33_keypad_scan(struct ht16k33_keypad *keypad)
245 {
246 	const unsigned short *keycodes = keypad->dev->keycode;
247 	u16 new_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
248 	__le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS];
249 	unsigned long bits_changed;
250 	int row, col, code;
251 	int rc;
252 	bool pressed = false;
253 
254 	rc = i2c_smbus_read_i2c_block_data(keypad->client, 0x40,
255 					   sizeof(data), (u8 *)data);
256 	if (rc != sizeof(data)) {
257 		dev_err(&keypad->client->dev,
258 			"Failed to read key data, rc=%d\n", rc);
259 		return false;
260 	}
261 
262 	for (col = 0; col < keypad->cols; col++) {
263 		new_state[col] = le16_to_cpu(data[col]);
264 		if (new_state[col])
265 			pressed = true;
266 		bits_changed = keypad->last_key_state[col] ^ new_state[col];
267 
268 		for_each_set_bit(row, &bits_changed, BITS_PER_LONG) {
269 			code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
270 			input_event(keypad->dev, EV_MSC, MSC_SCAN, code);
271 			input_report_key(keypad->dev, keycodes[code],
272 					 new_state[col] & BIT(row));
273 		}
274 	}
275 	input_sync(keypad->dev);
276 	memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols);
277 
278 	return pressed;
279 }
280 
281 static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
282 {
283 	struct ht16k33_keypad *keypad = dev;
284 
285 	do {
286 		wait_event_timeout(keypad->wait, keypad->stopped,
287 				    msecs_to_jiffies(keypad->debounce_ms));
288 		if (keypad->stopped)
289 			break;
290 	} while (ht16k33_keypad_scan(keypad));
291 
292 	return IRQ_HANDLED;
293 }
294 
295 static int ht16k33_keypad_start(struct input_dev *dev)
296 {
297 	struct ht16k33_keypad *keypad = input_get_drvdata(dev);
298 
299 	keypad->stopped = false;
300 	mb();
301 	enable_irq(keypad->client->irq);
302 
303 	return 0;
304 }
305 
306 static void ht16k33_keypad_stop(struct input_dev *dev)
307 {
308 	struct ht16k33_keypad *keypad = input_get_drvdata(dev);
309 
310 	keypad->stopped = true;
311 	mb();
312 	wake_up(&keypad->wait);
313 	disable_irq(keypad->client->irq);
314 }
315 
316 static int ht16k33_keypad_probe(struct i2c_client *client,
317 				struct ht16k33_keypad *keypad)
318 {
319 	struct device_node *node = client->dev.of_node;
320 	u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
321 	u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
322 	int err;
323 
324 	keypad->client = client;
325 	init_waitqueue_head(&keypad->wait);
326 
327 	keypad->dev = devm_input_allocate_device(&client->dev);
328 	if (!keypad->dev)
329 		return -ENOMEM;
330 
331 	input_set_drvdata(keypad->dev, keypad);
332 
333 	keypad->dev->name = DRIVER_NAME"-keypad";
334 	keypad->dev->id.bustype = BUS_I2C;
335 	keypad->dev->open = ht16k33_keypad_start;
336 	keypad->dev->close = ht16k33_keypad_stop;
337 
338 	if (!of_get_property(node, "linux,no-autorepeat", NULL))
339 		__set_bit(EV_REP, keypad->dev->evbit);
340 
341 	err = of_property_read_u32(node, "debounce-delay-ms",
342 				   &keypad->debounce_ms);
343 	if (err) {
344 		dev_err(&client->dev, "key debounce delay not specified\n");
345 		return err;
346 	}
347 
348 	err = matrix_keypad_parse_of_params(&client->dev, &rows, &cols);
349 	if (err)
350 		return err;
351 	if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS ||
352 	    cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) {
353 		dev_err(&client->dev, "%u rows or %u cols out of range in DT\n",
354 			rows, cols);
355 		return -ERANGE;
356 	}
357 
358 	keypad->rows = rows;
359 	keypad->cols = cols;
360 	keypad->row_shift = get_count_order(cols);
361 
362 	err = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL,
363 					 keypad->dev);
364 	if (err) {
365 		dev_err(&client->dev, "failed to build keymap\n");
366 		return err;
367 	}
368 
369 	err = devm_request_threaded_irq(&client->dev, client->irq,
370 					NULL, ht16k33_keypad_irq_thread,
371 					IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
372 					DRIVER_NAME, keypad);
373 	if (err) {
374 		dev_err(&client->dev, "irq request failed %d, error %d\n",
375 			client->irq, err);
376 		return err;
377 	}
378 
379 	ht16k33_keypad_stop(keypad->dev);
380 
381 	err = input_register_device(keypad->dev);
382 	if (err)
383 		return err;
384 
385 	return 0;
386 }
387 
388 static int ht16k33_probe(struct i2c_client *client,
389 				  const struct i2c_device_id *id)
390 {
391 	int err;
392 	uint32_t dft_brightness;
393 	struct backlight_device *bl;
394 	struct backlight_properties bl_props;
395 	struct ht16k33_priv *priv;
396 	struct ht16k33_fbdev *fbdev;
397 	struct device_node *node = client->dev.of_node;
398 
399 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
400 		dev_err(&client->dev, "i2c_check_functionality error\n");
401 		return -EIO;
402 	}
403 
404 	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
405 	if (!priv)
406 		return -ENOMEM;
407 
408 	priv->client = client;
409 	i2c_set_clientdata(client, priv);
410 	fbdev = &priv->fbdev;
411 
412 	err = ht16k33_initialize(priv);
413 	if (err)
414 		return err;
415 
416 	/* Framebuffer (2 bytes per column) */
417 	BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
418 	fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
419 	if (!fbdev->buffer)
420 		return -ENOMEM;
421 
422 	fbdev->cache = devm_kmalloc(&client->dev, HT16K33_FB_SIZE, GFP_KERNEL);
423 	if (!fbdev->cache) {
424 		err = -ENOMEM;
425 		goto err_fbdev_buffer;
426 	}
427 
428 	fbdev->info = framebuffer_alloc(0, &client->dev);
429 	if (!fbdev->info) {
430 		err = -ENOMEM;
431 		goto err_fbdev_buffer;
432 	}
433 
434 	err = of_property_read_u32(node, "refresh-rate-hz",
435 		&fbdev->refresh_rate);
436 	if (err) {
437 		dev_err(&client->dev, "refresh rate not specified\n");
438 		goto err_fbdev_info;
439 	}
440 	fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
441 
442 	INIT_DELAYED_WORK(&fbdev->work, ht16k33_fb_update);
443 	fbdev->info->fbops = &ht16k33_fb_ops;
444 	fbdev->info->screen_base = (char __iomem *) fbdev->buffer;
445 	fbdev->info->screen_size = HT16K33_FB_SIZE;
446 	fbdev->info->fix = ht16k33_fb_fix;
447 	fbdev->info->var = ht16k33_fb_var;
448 	fbdev->info->pseudo_palette = NULL;
449 	fbdev->info->flags = FBINFO_FLAG_DEFAULT;
450 	fbdev->info->par = priv;
451 
452 	err = register_framebuffer(fbdev->info);
453 	if (err)
454 		goto err_fbdev_info;
455 
456 	/* Keypad */
457 	if (client->irq > 0) {
458 		err = ht16k33_keypad_probe(client, &priv->keypad);
459 		if (err)
460 			goto err_fbdev_unregister;
461 	}
462 
463 	/* Backlight */
464 	memset(&bl_props, 0, sizeof(struct backlight_properties));
465 	bl_props.type = BACKLIGHT_RAW;
466 	bl_props.max_brightness = MAX_BRIGHTNESS;
467 
468 	bl = devm_backlight_device_register(&client->dev, DRIVER_NAME"-bl",
469 					    &client->dev, priv,
470 					    &ht16k33_bl_ops, &bl_props);
471 	if (IS_ERR(bl)) {
472 		dev_err(&client->dev, "failed to register backlight\n");
473 		err = PTR_ERR(bl);
474 		goto err_fbdev_unregister;
475 	}
476 
477 	err = of_property_read_u32(node, "default-brightness-level",
478 				   &dft_brightness);
479 	if (err) {
480 		dft_brightness = MAX_BRIGHTNESS;
481 	} else if (dft_brightness > MAX_BRIGHTNESS) {
482 		dev_warn(&client->dev,
483 			 "invalid default brightness level: %u, using %u\n",
484 			 dft_brightness, MAX_BRIGHTNESS);
485 		dft_brightness = MAX_BRIGHTNESS;
486 	}
487 
488 	bl->props.brightness = dft_brightness;
489 	ht16k33_bl_update_status(bl);
490 
491 	ht16k33_fb_queue(priv);
492 	return 0;
493 
494 err_fbdev_unregister:
495 	unregister_framebuffer(fbdev->info);
496 err_fbdev_info:
497 	framebuffer_release(fbdev->info);
498 err_fbdev_buffer:
499 	free_page((unsigned long) fbdev->buffer);
500 
501 	return err;
502 }
503 
504 static int ht16k33_remove(struct i2c_client *client)
505 {
506 	struct ht16k33_priv *priv = i2c_get_clientdata(client);
507 	struct ht16k33_fbdev *fbdev = &priv->fbdev;
508 
509 	cancel_delayed_work_sync(&fbdev->work);
510 	unregister_framebuffer(fbdev->info);
511 	framebuffer_release(fbdev->info);
512 	free_page((unsigned long) fbdev->buffer);
513 
514 	return 0;
515 }
516 
517 static const struct i2c_device_id ht16k33_i2c_match[] = {
518 	{ "ht16k33", 0 },
519 	{ }
520 };
521 MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
522 
523 static const struct of_device_id ht16k33_of_match[] = {
524 	{ .compatible = "holtek,ht16k33", },
525 	{ }
526 };
527 MODULE_DEVICE_TABLE(of, ht16k33_of_match);
528 
529 static struct i2c_driver ht16k33_driver = {
530 	.probe		= ht16k33_probe,
531 	.remove		= ht16k33_remove,
532 	.driver		= {
533 		.name		= DRIVER_NAME,
534 		.of_match_table	= of_match_ptr(ht16k33_of_match),
535 	},
536 	.id_table = ht16k33_i2c_match,
537 };
538 module_i2c_driver(ht16k33_driver);
539 
540 MODULE_DESCRIPTION("Holtek HT16K33 driver");
541 MODULE_LICENSE("GPL");
542 MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");
543