1 // SPDX-License-Identifier: GPL-2.0
5 * Author: Robin van der Gracht <robin@protonic.nl>
7 * Copyright: (C) 2016 Protonic Holland.
8 * Copyright (C) 2021 Glider bv
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/i2c.h>
15 #include <linux/property.h>
17 #include <linux/backlight.h>
18 #include <linux/input.h>
19 #include <linux/input/matrix_keypad.h>
20 #include <linux/leds.h>
21 #include <linux/workqueue.h>
24 #include <linux/map_to_7segment.h>
25 #include <linux/map_to_14segment.h>
27 #include <asm/unaligned.h>
29 #include "line-display.h"
32 #define REG_SYSTEM_SETUP 0x20
33 #define REG_SYSTEM_SETUP_OSC_ON BIT(0)
35 #define REG_DISPLAY_SETUP 0x80
36 #define REG_DISPLAY_SETUP_ON BIT(0)
37 #define REG_DISPLAY_SETUP_BLINK_OFF (0 << 1)
38 #define REG_DISPLAY_SETUP_BLINK_2HZ (1 << 1)
39 #define REG_DISPLAY_SETUP_BLINK_1HZ (2 << 1)
40 #define REG_DISPLAY_SETUP_BLINK_0HZ5 (3 << 1)
42 #define REG_ROWINT_SET 0xA0
43 #define REG_ROWINT_SET_INT_EN BIT(0)
44 #define REG_ROWINT_SET_INT_ACT_HIGH BIT(1)
46 #define REG_BRIGHTNESS 0xE0
49 #define DRIVER_NAME "ht16k33"
51 #define MIN_BRIGHTNESS 0x1
52 #define MAX_BRIGHTNESS 0x10
54 #define HT16K33_MATRIX_LED_MAX_COLS 8
55 #define HT16K33_MATRIX_LED_MAX_ROWS 16
56 #define HT16K33_MATRIX_KEYPAD_MAX_COLS 3
57 #define HT16K33_MATRIX_KEYPAD_MAX_ROWS 12
59 #define BYTES_PER_ROW (HT16K33_MATRIX_LED_MAX_ROWS / 8)
60 #define HT16K33_FB_SIZE (HT16K33_MATRIX_LED_MAX_COLS * BYTES_PER_ROW)
68 struct ht16k33_keypad {
69 struct i2c_client *client;
70 struct input_dev *dev;
75 uint16_t last_key_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
77 wait_queue_head_t wait;
81 struct ht16k33_fbdev {
83 uint32_t refresh_rate;
89 struct linedisp linedisp;
91 struct seg7_conversion_map seg7;
92 struct seg14_conversion_map seg14;
94 unsigned int map_size;
99 struct i2c_client *client;
100 struct delayed_work work;
101 struct led_classdev led;
102 struct ht16k33_keypad keypad;
104 struct ht16k33_fbdev fbdev;
105 struct ht16k33_seg seg;
107 enum display_type type;
111 static const struct fb_fix_screeninfo ht16k33_fb_fix = {
113 .type = FB_TYPE_PACKED_PIXELS,
114 .visual = FB_VISUAL_MONO10,
118 .line_length = HT16K33_MATRIX_LED_MAX_ROWS,
119 .accel = FB_ACCEL_NONE,
122 static const struct fb_var_screeninfo ht16k33_fb_var = {
123 .xres = HT16K33_MATRIX_LED_MAX_ROWS,
124 .yres = HT16K33_MATRIX_LED_MAX_COLS,
125 .xres_virtual = HT16K33_MATRIX_LED_MAX_ROWS,
126 .yres_virtual = HT16K33_MATRIX_LED_MAX_COLS,
129 .green = { 0, 1, 0 },
135 .vmode = FB_VMODE_NONINTERLACED,
138 static const SEG7_DEFAULT_MAP(initial_map_seg7);
139 static const SEG14_DEFAULT_MAP(initial_map_seg14);
141 static ssize_t map_seg_show(struct device *dev, struct device_attribute *attr,
144 struct ht16k33_priv *priv = dev_get_drvdata(dev);
146 memcpy(buf, &priv->seg.map, priv->seg.map_size);
147 return priv->seg.map_size;
150 static ssize_t map_seg_store(struct device *dev, struct device_attribute *attr,
151 const char *buf, size_t cnt)
153 struct ht16k33_priv *priv = dev_get_drvdata(dev);
155 if (cnt != priv->seg.map_size)
158 memcpy(&priv->seg.map, buf, cnt);
162 static DEVICE_ATTR(map_seg7, 0644, map_seg_show, map_seg_store);
163 static DEVICE_ATTR(map_seg14, 0644, map_seg_show, map_seg_store);
165 static int ht16k33_display_on(struct ht16k33_priv *priv)
167 uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON | priv->blink;
169 return i2c_smbus_write_byte(priv->client, data);
172 static int ht16k33_display_off(struct ht16k33_priv *priv)
174 return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP);
177 static int ht16k33_brightness_set(struct ht16k33_priv *priv,
178 unsigned int brightness)
182 if (brightness == 0) {
183 priv->blink = REG_DISPLAY_SETUP_BLINK_OFF;
184 return ht16k33_display_off(priv);
187 err = ht16k33_display_on(priv);
191 return i2c_smbus_write_byte(priv->client,
192 REG_BRIGHTNESS | (brightness - 1));
195 static int ht16k33_brightness_set_blocking(struct led_classdev *led_cdev,
196 enum led_brightness brightness)
198 struct ht16k33_priv *priv = container_of(led_cdev, struct ht16k33_priv,
201 return ht16k33_brightness_set(priv, brightness);
204 static int ht16k33_blink_set(struct led_classdev *led_cdev,
205 unsigned long *delay_on, unsigned long *delay_off)
207 struct ht16k33_priv *priv = container_of(led_cdev, struct ht16k33_priv,
213 if (!*delay_on && !*delay_off) {
214 blink = REG_DISPLAY_SETUP_BLINK_1HZ;
216 } else if (*delay_on <= 750) {
217 blink = REG_DISPLAY_SETUP_BLINK_2HZ;
219 } else if (*delay_on <= 1500) {
220 blink = REG_DISPLAY_SETUP_BLINK_1HZ;
223 blink = REG_DISPLAY_SETUP_BLINK_0HZ5;
227 err = i2c_smbus_write_byte(priv->client,
228 REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON |
234 *delay_on = *delay_off = delay;
238 static void ht16k33_fb_queue(struct ht16k33_priv *priv)
240 struct ht16k33_fbdev *fbdev = &priv->fbdev;
242 schedule_delayed_work(&priv->work, HZ / fbdev->refresh_rate);
246 * This gets the fb data from cache and copies it to ht16k33 display RAM
248 static void ht16k33_fb_update(struct work_struct *work)
250 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
252 struct ht16k33_fbdev *fbdev = &priv->fbdev;
255 int len, pos = 0, first = -1;
260 /* Search for the first byte with changes */
261 while (pos < HT16K33_FB_SIZE && first < 0) {
262 if (*(p1++) - *(p2++))
267 /* No changes found */
271 len = HT16K33_FB_SIZE - first;
272 p1 = fbdev->cache + HT16K33_FB_SIZE - 1;
273 p2 = fbdev->buffer + HT16K33_FB_SIZE - 1;
275 /* Determine i2c transfer length */
277 if (*(p1--) - *(p2--))
282 p1 = fbdev->cache + first;
283 p2 = fbdev->buffer + first;
284 if (!i2c_smbus_write_i2c_block_data(priv->client, first, len, p2))
287 ht16k33_fb_queue(priv);
290 static int ht16k33_initialize(struct ht16k33_priv *priv)
292 uint8_t data[HT16K33_FB_SIZE];
296 /* Clear RAM (8 * 16 bits) */
297 memset(data, 0, sizeof(data));
298 err = i2c_smbus_write_block_data(priv->client, 0, sizeof(data), data);
302 /* Turn on internal oscillator */
303 byte = REG_SYSTEM_SETUP_OSC_ON | REG_SYSTEM_SETUP;
304 err = i2c_smbus_write_byte(priv->client, byte);
308 /* Configure INT pin */
309 byte = REG_ROWINT_SET | REG_ROWINT_SET_INT_ACT_HIGH;
310 if (priv->client->irq > 0)
311 byte |= REG_ROWINT_SET_INT_EN;
312 return i2c_smbus_write_byte(priv->client, byte);
315 static int ht16k33_bl_update_status(struct backlight_device *bl)
317 int brightness = bl->props.brightness;
318 struct ht16k33_priv *priv = bl_get_data(bl);
320 if (bl->props.power != FB_BLANK_UNBLANK ||
321 bl->props.fb_blank != FB_BLANK_UNBLANK ||
322 bl->props.state & BL_CORE_FBBLANK)
325 return ht16k33_brightness_set(priv, brightness);
328 static int ht16k33_bl_check_fb(struct backlight_device *bl, struct fb_info *fi)
330 struct ht16k33_priv *priv = bl_get_data(bl);
332 return (fi == NULL) || (fi->par == priv);
335 static const struct backlight_ops ht16k33_bl_ops = {
336 .update_status = ht16k33_bl_update_status,
337 .check_fb = ht16k33_bl_check_fb,
341 * Blank events will be passed to the actual device handling the backlight when
342 * we return zero here.
344 static int ht16k33_blank(int blank, struct fb_info *info)
349 static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
351 struct ht16k33_priv *priv = info->par;
352 struct page *pages = virt_to_page(priv->fbdev.buffer);
354 return vm_map_pages_zero(vma, &pages, 1);
357 static const struct fb_ops ht16k33_fb_ops = {
358 .owner = THIS_MODULE,
359 .fb_read = fb_sys_read,
360 .fb_write = fb_sys_write,
361 .fb_blank = ht16k33_blank,
362 .fb_fillrect = sys_fillrect,
363 .fb_copyarea = sys_copyarea,
364 .fb_imageblit = sys_imageblit,
365 .fb_mmap = ht16k33_mmap,
369 * This gets the keys from keypad and reports it to input subsystem.
370 * Returns true if a key is pressed.
372 static bool ht16k33_keypad_scan(struct ht16k33_keypad *keypad)
374 const unsigned short *keycodes = keypad->dev->keycode;
375 u16 new_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
376 __le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS];
377 unsigned long bits_changed;
380 bool pressed = false;
382 rc = i2c_smbus_read_i2c_block_data(keypad->client, 0x40,
383 sizeof(data), (u8 *)data);
384 if (rc != sizeof(data)) {
385 dev_err(&keypad->client->dev,
386 "Failed to read key data, rc=%d\n", rc);
390 for (col = 0; col < keypad->cols; col++) {
391 new_state[col] = le16_to_cpu(data[col]);
394 bits_changed = keypad->last_key_state[col] ^ new_state[col];
396 for_each_set_bit(row, &bits_changed, BITS_PER_LONG) {
397 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
398 input_event(keypad->dev, EV_MSC, MSC_SCAN, code);
399 input_report_key(keypad->dev, keycodes[code],
400 new_state[col] & BIT(row));
403 input_sync(keypad->dev);
404 memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols);
409 static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
411 struct ht16k33_keypad *keypad = dev;
414 wait_event_timeout(keypad->wait, keypad->stopped,
415 msecs_to_jiffies(keypad->debounce_ms));
418 } while (ht16k33_keypad_scan(keypad));
423 static int ht16k33_keypad_start(struct input_dev *dev)
425 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
427 keypad->stopped = false;
429 enable_irq(keypad->client->irq);
434 static void ht16k33_keypad_stop(struct input_dev *dev)
436 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
438 keypad->stopped = true;
440 wake_up(&keypad->wait);
441 disable_irq(keypad->client->irq);
444 static void ht16k33_linedisp_update(struct linedisp *linedisp)
446 struct ht16k33_priv *priv = container_of(linedisp, struct ht16k33_priv,
449 schedule_delayed_work(&priv->work, 0);
452 static void ht16k33_seg7_update(struct work_struct *work)
454 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
456 struct ht16k33_seg *seg = &priv->seg;
460 buf[0] = map_to_seg7(&seg->map.seg7, *s++);
462 buf[2] = map_to_seg7(&seg->map.seg7, *s++);
466 buf[6] = map_to_seg7(&seg->map.seg7, *s++);
468 buf[8] = map_to_seg7(&seg->map.seg7, *s++);
470 i2c_smbus_write_i2c_block_data(priv->client, 0, ARRAY_SIZE(buf), buf);
473 static void ht16k33_seg14_update(struct work_struct *work)
475 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
477 struct ht16k33_seg *seg = &priv->seg;
481 put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf);
482 put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 2);
483 put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 4);
484 put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 6);
486 i2c_smbus_write_i2c_block_data(priv->client, 0, ARRAY_SIZE(buf), buf);
489 static int ht16k33_led_probe(struct device *dev, struct led_classdev *led,
490 unsigned int brightness)
492 struct led_init_data init_data = {};
495 /* The LED is optional */
496 init_data.fwnode = device_get_named_child_node(dev, "led");
497 if (!init_data.fwnode)
500 init_data.devicename = "auxdisplay";
501 init_data.devname_mandatory = true;
503 led->brightness_set_blocking = ht16k33_brightness_set_blocking;
504 led->blink_set = ht16k33_blink_set;
505 led->flags = LED_CORE_SUSPENDRESUME;
506 led->brightness = brightness;
507 led->max_brightness = MAX_BRIGHTNESS;
509 err = devm_led_classdev_register_ext(dev, led, &init_data);
511 dev_err(dev, "Failed to register LED\n");
516 static int ht16k33_keypad_probe(struct i2c_client *client,
517 struct ht16k33_keypad *keypad)
519 struct device *dev = &client->dev;
520 u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
521 u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
524 keypad->client = client;
525 init_waitqueue_head(&keypad->wait);
527 keypad->dev = devm_input_allocate_device(dev);
531 input_set_drvdata(keypad->dev, keypad);
533 keypad->dev->name = DRIVER_NAME"-keypad";
534 keypad->dev->id.bustype = BUS_I2C;
535 keypad->dev->open = ht16k33_keypad_start;
536 keypad->dev->close = ht16k33_keypad_stop;
538 if (!device_property_read_bool(dev, "linux,no-autorepeat"))
539 __set_bit(EV_REP, keypad->dev->evbit);
541 err = device_property_read_u32(dev, "debounce-delay-ms",
542 &keypad->debounce_ms);
544 dev_err(dev, "key debounce delay not specified\n");
548 err = matrix_keypad_parse_properties(dev, &rows, &cols);
551 if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS ||
552 cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) {
553 dev_err(dev, "%u rows or %u cols out of range in DT\n", rows,
560 keypad->row_shift = get_count_order(cols);
562 err = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL,
565 dev_err(dev, "failed to build keymap\n");
569 err = devm_request_threaded_irq(dev, client->irq, NULL,
570 ht16k33_keypad_irq_thread,
571 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
572 DRIVER_NAME, keypad);
574 dev_err(dev, "irq request failed %d, error %d\n", client->irq,
579 ht16k33_keypad_stop(keypad->dev);
581 return input_register_device(keypad->dev);
584 static int ht16k33_fbdev_probe(struct device *dev, struct ht16k33_priv *priv,
587 struct ht16k33_fbdev *fbdev = &priv->fbdev;
588 struct backlight_device *bl = NULL;
592 err = ht16k33_brightness_set(priv, brightness);
596 /* backwards compatibility with DT lacking an led subnode */
597 struct backlight_properties bl_props;
599 memset(&bl_props, 0, sizeof(struct backlight_properties));
600 bl_props.type = BACKLIGHT_RAW;
601 bl_props.max_brightness = MAX_BRIGHTNESS;
603 bl = devm_backlight_device_register(dev, DRIVER_NAME"-bl", dev,
604 priv, &ht16k33_bl_ops,
607 dev_err(dev, "failed to register backlight\n");
611 bl->props.brightness = brightness;
612 ht16k33_bl_update_status(bl);
615 /* Framebuffer (2 bytes per column) */
616 BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
617 fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
621 fbdev->cache = devm_kmalloc(dev, HT16K33_FB_SIZE, GFP_KERNEL);
624 goto err_fbdev_buffer;
627 fbdev->info = framebuffer_alloc(0, dev);
630 goto err_fbdev_buffer;
633 err = device_property_read_u32(dev, "refresh-rate-hz",
634 &fbdev->refresh_rate);
636 dev_err(dev, "refresh rate not specified\n");
639 fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
641 INIT_DELAYED_WORK(&priv->work, ht16k33_fb_update);
642 fbdev->info->fbops = &ht16k33_fb_ops;
643 fbdev->info->screen_base = (char __iomem *) fbdev->buffer;
644 fbdev->info->screen_size = HT16K33_FB_SIZE;
645 fbdev->info->fix = ht16k33_fb_fix;
646 fbdev->info->var = ht16k33_fb_var;
647 fbdev->info->bl_dev = bl;
648 fbdev->info->pseudo_palette = NULL;
649 fbdev->info->flags = FBINFO_FLAG_DEFAULT;
650 fbdev->info->par = priv;
652 err = register_framebuffer(fbdev->info);
656 ht16k33_fb_queue(priv);
660 framebuffer_release(fbdev->info);
662 free_page((unsigned long) fbdev->buffer);
667 static int ht16k33_seg_probe(struct device *dev, struct ht16k33_priv *priv,
670 struct ht16k33_seg *seg = &priv->seg;
673 err = ht16k33_brightness_set(priv, brightness);
677 switch (priv->type) {
679 /* not handled here */
684 INIT_DELAYED_WORK(&priv->work, ht16k33_seg7_update);
685 seg->map.seg7 = initial_map_seg7;
686 seg->map_size = sizeof(seg->map.seg7);
687 err = device_create_file(dev, &dev_attr_map_seg7);
690 case DISP_QUAD_14SEG:
691 INIT_DELAYED_WORK(&priv->work, ht16k33_seg14_update);
692 seg->map.seg14 = initial_map_seg14;
693 seg->map_size = sizeof(seg->map.seg14);
694 err = device_create_file(dev, &dev_attr_map_seg14);
700 err = linedisp_register(&seg->linedisp, dev, 4, seg->curr,
701 ht16k33_linedisp_update);
703 goto err_remove_map_file;
708 device_remove_file(dev, &dev_attr_map_seg7);
709 device_remove_file(dev, &dev_attr_map_seg14);
713 static int ht16k33_probe(struct i2c_client *client)
715 struct device *dev = &client->dev;
716 const struct of_device_id *id;
717 struct ht16k33_priv *priv;
718 uint32_t dft_brightness;
721 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
722 dev_err(dev, "i2c_check_functionality error\n");
726 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
730 priv->client = client;
731 id = i2c_of_match_device(dev->driver->of_match_table, client);
733 priv->type = (uintptr_t)id->data;
734 i2c_set_clientdata(client, priv);
736 err = ht16k33_initialize(priv);
740 err = device_property_read_u32(dev, "default-brightness-level",
743 dft_brightness = MAX_BRIGHTNESS;
744 } else if (dft_brightness > MAX_BRIGHTNESS) {
746 "invalid default brightness level: %u, using %u\n",
747 dft_brightness, MAX_BRIGHTNESS);
748 dft_brightness = MAX_BRIGHTNESS;
752 err = ht16k33_led_probe(dev, &priv->led, dft_brightness);
757 if (client->irq > 0) {
758 err = ht16k33_keypad_probe(client, &priv->keypad);
763 switch (priv->type) {
765 /* Frame Buffer Display */
766 err = ht16k33_fbdev_probe(dev, priv, dft_brightness);
770 case DISP_QUAD_14SEG:
771 /* Segment Display */
772 err = ht16k33_seg_probe(dev, priv, dft_brightness);
778 static int ht16k33_remove(struct i2c_client *client)
780 struct ht16k33_priv *priv = i2c_get_clientdata(client);
781 struct ht16k33_fbdev *fbdev = &priv->fbdev;
783 cancel_delayed_work_sync(&priv->work);
785 switch (priv->type) {
787 unregister_framebuffer(fbdev->info);
788 framebuffer_release(fbdev->info);
789 free_page((unsigned long)fbdev->buffer);
793 case DISP_QUAD_14SEG:
794 linedisp_unregister(&priv->seg.linedisp);
795 device_remove_file(&client->dev, &dev_attr_map_seg7);
796 device_remove_file(&client->dev, &dev_attr_map_seg14);
803 static const struct i2c_device_id ht16k33_i2c_match[] = {
807 MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
809 static const struct of_device_id ht16k33_of_match[] = {
811 /* 0.56" 4-Digit 7-Segment FeatherWing Display (Red) */
812 .compatible = "adafruit,3108", .data = (void *)DISP_QUAD_7SEG,
814 /* 0.54" Quad Alphanumeric FeatherWing Display (Red) */
815 .compatible = "adafruit,3130", .data = (void *)DISP_QUAD_14SEG,
817 /* Generic, assumed Dot-Matrix Display */
818 .compatible = "holtek,ht16k33", .data = (void *)DISP_MATRIX,
822 MODULE_DEVICE_TABLE(of, ht16k33_of_match);
824 static struct i2c_driver ht16k33_driver = {
825 .probe_new = ht16k33_probe,
826 .remove = ht16k33_remove,
829 .of_match_table = ht16k33_of_match,
831 .id_table = ht16k33_i2c_match,
833 module_i2c_driver(ht16k33_driver);
835 MODULE_DESCRIPTION("Holtek HT16K33 driver");
836 MODULE_LICENSE("GPL");
837 MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");