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