blob: 586faba83a75a5aa12eb3fef57d8aa5221c156c4 [file] [log] [blame]
Robin van der Gracht8992da42016-11-07 10:56:35 +01001/*
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
58struct ht16k33_keypad {
Dmitry Torokhovcac513f2017-02-09 10:15:52 -080059 struct i2c_client *client;
Robin van der Gracht8992da42016-11-07 10:56:35 +010060 struct input_dev *dev;
Robin van der Gracht8992da42016-11-07 10:56:35 +010061 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];
Dmitry Torokhovcac513f2017-02-09 10:15:52 -080066
67 wait_queue_head_t wait;
68 bool stopped;
Robin van der Gracht8992da42016-11-07 10:56:35 +010069};
70
71struct 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
79struct ht16k33_priv {
80 struct i2c_client *client;
81 struct ht16k33_keypad keypad;
82 struct ht16k33_fbdev fbdev;
83 struct workqueue_struct *workqueue;
84};
85
86static struct fb_fix_screeninfo ht16k33_fb_fix = {
87 .id = DRIVER_NAME,
88 .type = FB_TYPE_PACKED_PIXELS,
89 .visual = FB_VISUAL_MONO10,
90 .xpanstep = 0,
91 .ypanstep = 0,
92 .ywrapstep = 0,
93 .line_length = HT16K33_MATRIX_LED_MAX_ROWS,
94 .accel = FB_ACCEL_NONE,
95};
96
97static struct fb_var_screeninfo ht16k33_fb_var = {
98 .xres = HT16K33_MATRIX_LED_MAX_ROWS,
99 .yres = HT16K33_MATRIX_LED_MAX_COLS,
100 .xres_virtual = HT16K33_MATRIX_LED_MAX_ROWS,
101 .yres_virtual = HT16K33_MATRIX_LED_MAX_COLS,
102 .bits_per_pixel = 1,
103 .red = { 0, 1, 0 },
104 .green = { 0, 1, 0 },
105 .blue = { 0, 1, 0 },
106 .left_margin = 0,
107 .right_margin = 0,
108 .upper_margin = 0,
109 .lower_margin = 0,
110 .vmode = FB_VMODE_NONINTERLACED,
111};
112
113static int ht16k33_display_on(struct ht16k33_priv *priv)
114{
115 uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON;
116
117 return i2c_smbus_write_byte(priv->client, data);
118}
119
120static int ht16k33_display_off(struct ht16k33_priv *priv)
121{
122 return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP);
123}
124
125static void ht16k33_fb_queue(struct ht16k33_priv *priv)
126{
127 struct ht16k33_fbdev *fbdev = &priv->fbdev;
128
129 queue_delayed_work(priv->workqueue, &fbdev->work,
130 msecs_to_jiffies(HZ / fbdev->refresh_rate));
131}
132
Robin van der Gracht8992da42016-11-07 10:56:35 +0100133/*
134 * This gets the fb data from cache and copies it to ht16k33 display RAM
135 */
136static void ht16k33_fb_update(struct work_struct *work)
137{
138 struct ht16k33_fbdev *fbdev =
139 container_of(work, struct ht16k33_fbdev, work.work);
140 struct ht16k33_priv *priv =
141 container_of(fbdev, struct ht16k33_priv, fbdev);
142
143 uint8_t *p1, *p2;
144 int len, pos = 0, first = -1;
145
146 p1 = fbdev->cache;
147 p2 = fbdev->buffer;
148
149 /* Search for the first byte with changes */
150 while (pos < HT16K33_FB_SIZE && first < 0) {
151 if (*(p1++) - *(p2++))
152 first = pos;
153 pos++;
154 }
155
156 /* No changes found */
157 if (first < 0)
158 goto requeue;
159
160 len = HT16K33_FB_SIZE - first;
161 p1 = fbdev->cache + HT16K33_FB_SIZE - 1;
162 p2 = fbdev->buffer + HT16K33_FB_SIZE - 1;
163
164 /* Determine i2c transfer length */
165 while (len > 1) {
166 if (*(p1--) - *(p2--))
167 break;
168 len--;
169 }
170
171 p1 = fbdev->cache + first;
172 p2 = fbdev->buffer + first;
173 if (!i2c_smbus_write_i2c_block_data(priv->client, first, len, p2))
174 memcpy(p1, p2, len);
175requeue:
176 ht16k33_fb_queue(priv);
177}
178
Robin van der Gracht8992da42016-11-07 10:56:35 +0100179static int ht16k33_initialize(struct ht16k33_priv *priv)
180{
181 uint8_t byte;
182 int err;
183 uint8_t data[HT16K33_MATRIX_LED_MAX_COLS * 2];
184
185 /* Clear RAM (8 * 16 bits) */
186 memset(data, 0, sizeof(data));
187 err = i2c_smbus_write_block_data(priv->client, 0, sizeof(data), data);
188 if (err)
189 return err;
190
191 /* Turn on internal oscillator */
192 byte = REG_SYSTEM_SETUP_OSC_ON | REG_SYSTEM_SETUP;
193 err = i2c_smbus_write_byte(priv->client, byte);
194 if (err)
195 return err;
196
197 /* Configure INT pin */
198 byte = REG_ROWINT_SET | REG_ROWINT_SET_INT_ACT_HIGH;
199 if (priv->client->irq > 0)
200 byte |= REG_ROWINT_SET_INT_EN;
201 return i2c_smbus_write_byte(priv->client, byte);
202}
203
Robin van der Gracht8992da42016-11-07 10:56:35 +0100204static int ht16k33_bl_update_status(struct backlight_device *bl)
205{
206 int brightness = bl->props.brightness;
207 struct ht16k33_priv *priv = bl_get_data(bl);
208
209 if (bl->props.power != FB_BLANK_UNBLANK ||
210 bl->props.fb_blank != FB_BLANK_UNBLANK ||
211 bl->props.state & BL_CORE_FBBLANK || brightness == 0) {
212 return ht16k33_display_off(priv);
213 }
214
215 ht16k33_display_on(priv);
216 return i2c_smbus_write_byte(priv->client,
217 REG_BRIGHTNESS | (brightness - 1));
218}
219
220static int ht16k33_bl_check_fb(struct backlight_device *bl, struct fb_info *fi)
221{
222 struct ht16k33_priv *priv = bl_get_data(bl);
223
224 return (fi == NULL) || (fi->par == priv);
225}
226
227static const struct backlight_ops ht16k33_bl_ops = {
228 .update_status = ht16k33_bl_update_status,
229 .check_fb = ht16k33_bl_check_fb,
230};
231
232static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
233{
234 struct ht16k33_priv *priv = info->par;
235
236 return vm_insert_page(vma, vma->vm_start,
237 virt_to_page(priv->fbdev.buffer));
238}
239
240static struct fb_ops ht16k33_fb_ops = {
241 .owner = THIS_MODULE,
242 .fb_read = fb_sys_read,
243 .fb_write = fb_sys_write,
244 .fb_fillrect = sys_fillrect,
245 .fb_copyarea = sys_copyarea,
246 .fb_imageblit = sys_imageblit,
247 .fb_mmap = ht16k33_mmap,
248};
249
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800250/*
251 * This gets the keys from keypad and reports it to input subsystem.
252 * Returns true if a key is pressed.
253 */
254static bool ht16k33_keypad_scan(struct ht16k33_keypad *keypad)
255{
256 const unsigned short *keycodes = keypad->dev->keycode;
257 u16 new_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
258 u8 data[HT16K33_MATRIX_KEYPAD_MAX_COLS * 2];
259 unsigned long bits_changed;
260 int row, col, code;
261 bool pressed = false;
262
263 if (i2c_smbus_read_i2c_block_data(keypad->client, 0x40, 6, data) != 6) {
264 dev_err(&keypad->client->dev, "Failed to read key data\n");
265 return false;
266 }
267
268 for (col = 0; col < keypad->cols; col++) {
269 new_state[col] = (data[col * 2 + 1] << 8) | data[col * 2];
270 if (new_state[col])
271 pressed = true;
272 bits_changed = keypad->last_key_state[col] ^ new_state[col];
273
274 for_each_set_bit(row, &bits_changed, BITS_PER_LONG) {
275 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
276 input_event(keypad->dev, EV_MSC, MSC_SCAN, code);
277 input_report_key(keypad->dev, keycodes[code],
278 new_state[col] & BIT(row));
279 }
280 }
281 input_sync(keypad->dev);
282 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
283
284 return pressed;
285}
286
287static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
288{
289 struct ht16k33_keypad *keypad = dev;
290
291 do {
292 wait_event_timeout(keypad->wait, keypad->stopped,
293 msecs_to_jiffies(keypad->debounce_ms));
294 if (keypad->stopped)
295 break;
296 } while (ht16k33_keypad_scan(keypad));
297
298 return IRQ_HANDLED;
299}
300
301static int ht16k33_keypad_start(struct input_dev *dev)
302{
303 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
304
305 keypad->stopped = false;
306 mb();
307 enable_irq(keypad->client->irq);
308
309 return 0;
310}
311
312static void ht16k33_keypad_stop(struct input_dev *dev)
313{
314 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
315
316 keypad->stopped = true;
317 mb();
318 wake_up(&keypad->wait);
319 disable_irq(keypad->client->irq);
320}
321
322static int ht16k33_keypad_probe(struct i2c_client *client,
323 struct ht16k33_keypad *keypad)
324{
325 struct device_node *node = client->dev.of_node;
326 u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
327 u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
328 int err;
329
330 keypad->client = client;
331 init_waitqueue_head(&keypad->wait);
332
333 keypad->dev = devm_input_allocate_device(&client->dev);
334 if (!keypad->dev)
335 return -ENOMEM;
336
337 input_set_drvdata(keypad->dev, keypad);
338
339 keypad->dev->name = DRIVER_NAME"-keypad";
340 keypad->dev->id.bustype = BUS_I2C;
341 keypad->dev->open = ht16k33_keypad_start;
342 keypad->dev->close = ht16k33_keypad_stop;
343
344 if (!of_get_property(node, "linux,no-autorepeat", NULL))
345 __set_bit(EV_REP, keypad->dev->evbit);
346
347 err = of_property_read_u32(node, "debounce-delay-ms",
348 &keypad->debounce_ms);
349 if (err) {
350 dev_err(&client->dev, "key debounce delay not specified\n");
351 return err;
352 }
353
354 err = matrix_keypad_parse_of_params(&client->dev, &rows, &cols);
355 if (err)
356 return err;
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
Robin van der Gracht8992da42016-11-07 10:56:35 +0100388static int ht16k33_probe(struct i2c_client *client,
389 const struct i2c_device_id *id)
390{
391 int err;
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800392 uint32_t dft_brightness;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100393 struct backlight_device *bl;
394 struct backlight_properties bl_props;
395 struct ht16k33_priv *priv;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100396 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 if (client->irq <= 0) {
405 dev_err(&client->dev, "No IRQ specified\n");
406 return -EINVAL;
407 }
408
409 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
410 if (!priv)
411 return -ENOMEM;
412
413 priv->client = client;
414 i2c_set_clientdata(client, priv);
415 fbdev = &priv->fbdev;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100416
417 priv->workqueue = create_singlethread_workqueue(DRIVER_NAME "-wq");
418 if (priv->workqueue == NULL)
419 return -ENOMEM;
420
421 err = ht16k33_initialize(priv);
422 if (err)
423 goto err_destroy_wq;
424
425 /* Framebuffer (2 bytes per column) */
426 BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
427 fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
428 if (!fbdev->buffer) {
429 err = -ENOMEM;
Dmitry Torokhovbbd39d12017-02-09 10:15:51 -0800430 goto err_destroy_wq;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100431 }
432
433 fbdev->cache = devm_kmalloc(&client->dev, HT16K33_FB_SIZE, GFP_KERNEL);
434 if (!fbdev->cache) {
435 err = -ENOMEM;
436 goto err_fbdev_buffer;
437 }
438
439 fbdev->info = framebuffer_alloc(0, &client->dev);
440 if (!fbdev->info) {
441 err = -ENOMEM;
442 goto err_fbdev_buffer;
443 }
444
445 err = of_property_read_u32(node, "refresh-rate-hz",
446 &fbdev->refresh_rate);
447 if (err) {
448 dev_err(&client->dev, "refresh rate not specified\n");
449 goto err_fbdev_info;
450 }
451 fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
452
453 INIT_DELAYED_WORK(&fbdev->work, ht16k33_fb_update);
454 fbdev->info->fbops = &ht16k33_fb_ops;
455 fbdev->info->screen_base = (char __iomem *) fbdev->buffer;
456 fbdev->info->screen_size = HT16K33_FB_SIZE;
457 fbdev->info->fix = ht16k33_fb_fix;
458 fbdev->info->var = ht16k33_fb_var;
459 fbdev->info->pseudo_palette = NULL;
460 fbdev->info->flags = FBINFO_FLAG_DEFAULT;
461 fbdev->info->par = priv;
462
463 err = register_framebuffer(fbdev->info);
464 if (err)
465 goto err_fbdev_info;
466
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800467 err = ht16k33_keypad_probe(client, &priv->keypad);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100468 if (err)
469 goto err_fbdev_unregister;
470
471 /* Backlight */
472 memset(&bl_props, 0, sizeof(struct backlight_properties));
473 bl_props.type = BACKLIGHT_RAW;
474 bl_props.max_brightness = MAX_BRIGHTNESS;
475
476 bl = devm_backlight_device_register(&client->dev, DRIVER_NAME"-bl",
477 &client->dev, priv,
478 &ht16k33_bl_ops, &bl_props);
479 if (IS_ERR(bl)) {
480 dev_err(&client->dev, "failed to register backlight\n");
481 err = PTR_ERR(bl);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800482 goto err_fbdev_unregister;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100483 }
484
485 err = of_property_read_u32(node, "default-brightness-level",
486 &dft_brightness);
487 if (err) {
488 dft_brightness = MAX_BRIGHTNESS;
489 } else if (dft_brightness > MAX_BRIGHTNESS) {
490 dev_warn(&client->dev,
491 "invalid default brightness level: %u, using %u\n",
492 dft_brightness, MAX_BRIGHTNESS);
493 dft_brightness = MAX_BRIGHTNESS;
494 }
495
496 bl->props.brightness = dft_brightness;
497 ht16k33_bl_update_status(bl);
498
499 ht16k33_fb_queue(priv);
500 return 0;
501
Robin van der Gracht8992da42016-11-07 10:56:35 +0100502err_fbdev_unregister:
503 unregister_framebuffer(fbdev->info);
504err_fbdev_info:
505 framebuffer_release(fbdev->info);
506err_fbdev_buffer:
507 free_page((unsigned long) fbdev->buffer);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100508err_destroy_wq:
509 destroy_workqueue(priv->workqueue);
510
511 return err;
512}
513
514static int ht16k33_remove(struct i2c_client *client)
515{
516 struct ht16k33_priv *priv = i2c_get_clientdata(client);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100517 struct ht16k33_fbdev *fbdev = &priv->fbdev;
518
Robin van der Gracht8992da42016-11-07 10:56:35 +0100519 cancel_delayed_work(&fbdev->work);
520 unregister_framebuffer(fbdev->info);
521 framebuffer_release(fbdev->info);
522 free_page((unsigned long) fbdev->buffer);
523
524 destroy_workqueue(priv->workqueue);
525 return 0;
526}
527
528static const struct i2c_device_id ht16k33_i2c_match[] = {
529 { "ht16k33", 0 },
530 { }
531};
532MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
533
534static const struct of_device_id ht16k33_of_match[] = {
535 { .compatible = "holtek,ht16k33", },
536 { }
537};
538MODULE_DEVICE_TABLE(of, ht16k33_of_match);
539
540static struct i2c_driver ht16k33_driver = {
541 .probe = ht16k33_probe,
542 .remove = ht16k33_remove,
543 .driver = {
544 .name = DRIVER_NAME,
545 .of_match_table = of_match_ptr(ht16k33_of_match),
546 },
547 .id_table = ht16k33_i2c_match,
548};
549module_i2c_driver(ht16k33_driver);
550
551MODULE_DESCRIPTION("Holtek HT16K33 driver");
552MODULE_LICENSE("GPL");
553MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");