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
ht16k33_display_on(struct ht16k33_priv * priv)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
ht16k33_display_off(struct ht16k33_priv * priv)111 static int ht16k33_display_off(struct ht16k33_priv *priv)
112 {
113 return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP);
114 }
115
ht16k33_fb_queue(struct ht16k33_priv * priv)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 */
ht16k33_fb_update(struct work_struct * work)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
ht16k33_initialize(struct ht16k33_priv * priv)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
ht16k33_bl_update_status(struct backlight_device * bl)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
ht16k33_bl_check_fb(struct backlight_device * bl,struct fb_info * fi)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 /*
223 * Blank events will be passed to the actual device handling the backlight when
224 * we return zero here.
225 */
ht16k33_blank(int blank,struct fb_info * info)226 static int ht16k33_blank(int blank, struct fb_info *info)
227 {
228 return 0;
229 }
230
ht16k33_mmap(struct fb_info * info,struct vm_area_struct * vma)231 static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
232 {
233 struct ht16k33_priv *priv = info->par;
234 struct page *pages = virt_to_page(priv->fbdev.buffer);
235
236 return vm_map_pages_zero(vma, &pages, 1);
237 }
238
239 static const struct fb_ops ht16k33_fb_ops = {
240 .owner = THIS_MODULE,
241 .fb_read = fb_sys_read,
242 .fb_write = fb_sys_write,
243 .fb_blank = ht16k33_blank,
244 .fb_fillrect = sys_fillrect,
245 .fb_copyarea = sys_copyarea,
246 .fb_imageblit = sys_imageblit,
247 .fb_mmap = ht16k33_mmap,
248 };
249
250 /*
251 * This gets the keys from keypad and reports it to input subsystem.
252 * Returns true if a key is pressed.
253 */
ht16k33_keypad_scan(struct ht16k33_keypad * keypad)254 static 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 __le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS];
259 unsigned long bits_changed;
260 int row, col, code;
261 int rc;
262 bool pressed = false;
263
264 rc = i2c_smbus_read_i2c_block_data(keypad->client, 0x40,
265 sizeof(data), (u8 *)data);
266 if (rc != sizeof(data)) {
267 dev_err(&keypad->client->dev,
268 "Failed to read key data, rc=%d\n", rc);
269 return false;
270 }
271
272 for (col = 0; col < keypad->cols; col++) {
273 new_state[col] = le16_to_cpu(data[col]);
274 if (new_state[col])
275 pressed = true;
276 bits_changed = keypad->last_key_state[col] ^ new_state[col];
277
278 for_each_set_bit(row, &bits_changed, BITS_PER_LONG) {
279 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
280 input_event(keypad->dev, EV_MSC, MSC_SCAN, code);
281 input_report_key(keypad->dev, keycodes[code],
282 new_state[col] & BIT(row));
283 }
284 }
285 input_sync(keypad->dev);
286 memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols);
287
288 return pressed;
289 }
290
ht16k33_keypad_irq_thread(int irq,void * dev)291 static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
292 {
293 struct ht16k33_keypad *keypad = dev;
294
295 do {
296 wait_event_timeout(keypad->wait, keypad->stopped,
297 msecs_to_jiffies(keypad->debounce_ms));
298 if (keypad->stopped)
299 break;
300 } while (ht16k33_keypad_scan(keypad));
301
302 return IRQ_HANDLED;
303 }
304
ht16k33_keypad_start(struct input_dev * dev)305 static int ht16k33_keypad_start(struct input_dev *dev)
306 {
307 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
308
309 keypad->stopped = false;
310 mb();
311 enable_irq(keypad->client->irq);
312
313 return 0;
314 }
315
ht16k33_keypad_stop(struct input_dev * dev)316 static void ht16k33_keypad_stop(struct input_dev *dev)
317 {
318 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
319
320 keypad->stopped = true;
321 mb();
322 wake_up(&keypad->wait);
323 disable_irq(keypad->client->irq);
324 }
325
ht16k33_keypad_probe(struct i2c_client * client,struct ht16k33_keypad * keypad)326 static int ht16k33_keypad_probe(struct i2c_client *client,
327 struct ht16k33_keypad *keypad)
328 {
329 struct device_node *node = client->dev.of_node;
330 u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
331 u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
332 int err;
333
334 keypad->client = client;
335 init_waitqueue_head(&keypad->wait);
336
337 keypad->dev = devm_input_allocate_device(&client->dev);
338 if (!keypad->dev)
339 return -ENOMEM;
340
341 input_set_drvdata(keypad->dev, keypad);
342
343 keypad->dev->name = DRIVER_NAME"-keypad";
344 keypad->dev->id.bustype = BUS_I2C;
345 keypad->dev->open = ht16k33_keypad_start;
346 keypad->dev->close = ht16k33_keypad_stop;
347
348 if (!of_get_property(node, "linux,no-autorepeat", NULL))
349 __set_bit(EV_REP, keypad->dev->evbit);
350
351 err = of_property_read_u32(node, "debounce-delay-ms",
352 &keypad->debounce_ms);
353 if (err) {
354 dev_err(&client->dev, "key debounce delay not specified\n");
355 return err;
356 }
357
358 err = matrix_keypad_parse_of_params(&client->dev, &rows, &cols);
359 if (err)
360 return err;
361 if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS ||
362 cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) {
363 dev_err(&client->dev, "%u rows or %u cols out of range in DT\n",
364 rows, cols);
365 return -ERANGE;
366 }
367
368 keypad->rows = rows;
369 keypad->cols = cols;
370 keypad->row_shift = get_count_order(cols);
371
372 err = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL,
373 keypad->dev);
374 if (err) {
375 dev_err(&client->dev, "failed to build keymap\n");
376 return err;
377 }
378
379 err = devm_request_threaded_irq(&client->dev, client->irq,
380 NULL, ht16k33_keypad_irq_thread,
381 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
382 DRIVER_NAME, keypad);
383 if (err) {
384 dev_err(&client->dev, "irq request failed %d, error %d\n",
385 client->irq, err);
386 return err;
387 }
388
389 ht16k33_keypad_stop(keypad->dev);
390
391 err = input_register_device(keypad->dev);
392 if (err)
393 return err;
394
395 return 0;
396 }
397
ht16k33_probe(struct i2c_client * client,const struct i2c_device_id * id)398 static int ht16k33_probe(struct i2c_client *client,
399 const struct i2c_device_id *id)
400 {
401 int err;
402 uint32_t dft_brightness;
403 struct backlight_device *bl;
404 struct backlight_properties bl_props;
405 struct ht16k33_priv *priv;
406 struct ht16k33_fbdev *fbdev;
407 struct device_node *node = client->dev.of_node;
408
409 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
410 dev_err(&client->dev, "i2c_check_functionality error\n");
411 return -EIO;
412 }
413
414 if (client->irq <= 0) {
415 dev_err(&client->dev, "No IRQ specified\n");
416 return -EINVAL;
417 }
418
419 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
420 if (!priv)
421 return -ENOMEM;
422
423 priv->client = client;
424 i2c_set_clientdata(client, priv);
425 fbdev = &priv->fbdev;
426
427 err = ht16k33_initialize(priv);
428 if (err)
429 return err;
430
431 /* Backlight */
432 memset(&bl_props, 0, sizeof(struct backlight_properties));
433 bl_props.type = BACKLIGHT_RAW;
434 bl_props.max_brightness = MAX_BRIGHTNESS;
435
436 bl = devm_backlight_device_register(&client->dev, DRIVER_NAME"-bl",
437 &client->dev, priv,
438 &ht16k33_bl_ops, &bl_props);
439 if (IS_ERR(bl)) {
440 dev_err(&client->dev, "failed to register backlight\n");
441 return PTR_ERR(bl);
442 }
443
444 err = of_property_read_u32(node, "default-brightness-level",
445 &dft_brightness);
446 if (err) {
447 dft_brightness = MAX_BRIGHTNESS;
448 } else if (dft_brightness > MAX_BRIGHTNESS) {
449 dev_warn(&client->dev,
450 "invalid default brightness level: %u, using %u\n",
451 dft_brightness, MAX_BRIGHTNESS);
452 dft_brightness = MAX_BRIGHTNESS;
453 }
454
455 bl->props.brightness = dft_brightness;
456 ht16k33_bl_update_status(bl);
457
458 /* Framebuffer (2 bytes per column) */
459 BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
460 fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
461 if (!fbdev->buffer)
462 return -ENOMEM;
463
464 fbdev->cache = devm_kmalloc(&client->dev, HT16K33_FB_SIZE, GFP_KERNEL);
465 if (!fbdev->cache) {
466 err = -ENOMEM;
467 goto err_fbdev_buffer;
468 }
469
470 fbdev->info = framebuffer_alloc(0, &client->dev);
471 if (!fbdev->info) {
472 err = -ENOMEM;
473 goto err_fbdev_buffer;
474 }
475
476 err = of_property_read_u32(node, "refresh-rate-hz",
477 &fbdev->refresh_rate);
478 if (err) {
479 dev_err(&client->dev, "refresh rate not specified\n");
480 goto err_fbdev_info;
481 }
482 fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
483
484 INIT_DELAYED_WORK(&fbdev->work, ht16k33_fb_update);
485 fbdev->info->fbops = &ht16k33_fb_ops;
486 fbdev->info->screen_base = (char __iomem *) fbdev->buffer;
487 fbdev->info->screen_size = HT16K33_FB_SIZE;
488 fbdev->info->fix = ht16k33_fb_fix;
489 fbdev->info->var = ht16k33_fb_var;
490 fbdev->info->bl_dev = bl;
491 fbdev->info->pseudo_palette = NULL;
492 fbdev->info->flags = FBINFO_FLAG_DEFAULT;
493 fbdev->info->par = priv;
494
495 err = register_framebuffer(fbdev->info);
496 if (err)
497 goto err_fbdev_info;
498
499 err = ht16k33_keypad_probe(client, &priv->keypad);
500 if (err)
501 goto err_fbdev_unregister;
502
503 ht16k33_fb_queue(priv);
504 return 0;
505
506 err_fbdev_unregister:
507 unregister_framebuffer(fbdev->info);
508 err_fbdev_info:
509 framebuffer_release(fbdev->info);
510 err_fbdev_buffer:
511 free_page((unsigned long) fbdev->buffer);
512
513 return err;
514 }
515
ht16k33_remove(struct i2c_client * client)516 static int ht16k33_remove(struct i2c_client *client)
517 {
518 struct ht16k33_priv *priv = i2c_get_clientdata(client);
519 struct ht16k33_fbdev *fbdev = &priv->fbdev;
520
521 cancel_delayed_work_sync(&fbdev->work);
522 unregister_framebuffer(fbdev->info);
523 framebuffer_release(fbdev->info);
524 free_page((unsigned long) fbdev->buffer);
525
526 return 0;
527 }
528
529 static const struct i2c_device_id ht16k33_i2c_match[] = {
530 { "ht16k33", 0 },
531 { }
532 };
533 MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
534
535 static const struct of_device_id ht16k33_of_match[] = {
536 { .compatible = "holtek,ht16k33", },
537 { }
538 };
539 MODULE_DEVICE_TABLE(of, ht16k33_of_match);
540
541 static struct i2c_driver ht16k33_driver = {
542 .probe = ht16k33_probe,
543 .remove = ht16k33_remove,
544 .driver = {
545 .name = DRIVER_NAME,
546 .of_match_table = of_match_ptr(ht16k33_of_match),
547 },
548 .id_table = ht16k33_i2c_match,
549 };
550 module_i2c_driver(ht16k33_driver);
551
552 MODULE_DESCRIPTION("Holtek HT16K33 driver");
553 MODULE_LICENSE("GPL");
554 MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");
555