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 * Copyright (C) 2021 Glider bv
9 */
10
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>
16#include <linux/fb.h>
17#include <linux/backlight.h>
18#include <linux/container_of.h>
19#include <linux/input.h>
20#include <linux/input/matrix_keypad.h>
21#include <linux/leds.h>
22#include <linux/workqueue.h>
23#include <linux/mm.h>
24
25#include <linux/map_to_7segment.h>
26#include <linux/map_to_14segment.h>
27
28#include <asm/unaligned.h>
29
30#include "line-display.h"
31
32/* Registers */
33#define REG_SYSTEM_SETUP 0x20
34#define REG_SYSTEM_SETUP_OSC_ON BIT(0)
35
36#define REG_DISPLAY_SETUP 0x80
37#define REG_DISPLAY_SETUP_ON BIT(0)
38#define REG_DISPLAY_SETUP_BLINK_OFF (0 << 1)
39#define REG_DISPLAY_SETUP_BLINK_2HZ (1 << 1)
40#define REG_DISPLAY_SETUP_BLINK_1HZ (2 << 1)
41#define REG_DISPLAY_SETUP_BLINK_0HZ5 (3 << 1)
42
43#define REG_ROWINT_SET 0xA0
44#define REG_ROWINT_SET_INT_EN BIT(0)
45#define REG_ROWINT_SET_INT_ACT_HIGH BIT(1)
46
47#define REG_BRIGHTNESS 0xE0
48
49/* Defines */
50#define DRIVER_NAME "ht16k33"
51
52#define MIN_BRIGHTNESS 0x1
53#define MAX_BRIGHTNESS 0x10
54
55#define HT16K33_MATRIX_LED_MAX_COLS 8
56#define HT16K33_MATRIX_LED_MAX_ROWS 16
57#define HT16K33_MATRIX_KEYPAD_MAX_COLS 3
58#define HT16K33_MATRIX_KEYPAD_MAX_ROWS 12
59
60#define BYTES_PER_ROW (HT16K33_MATRIX_LED_MAX_ROWS / 8)
61#define HT16K33_FB_SIZE (HT16K33_MATRIX_LED_MAX_COLS * BYTES_PER_ROW)
62
63enum display_type {
64 DISP_MATRIX = 0,
65 DISP_QUAD_7SEG,
66 DISP_QUAD_14SEG,
67};
68
69struct ht16k33_keypad {
70 struct i2c_client *client;
71 struct input_dev *dev;
72 uint32_t cols;
73 uint32_t rows;
74 uint32_t row_shift;
75 uint32_t debounce_ms;
76 uint16_t last_key_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
77
78 wait_queue_head_t wait;
79 bool stopped;
80};
81
82struct ht16k33_fbdev {
83 struct fb_info *info;
84 uint32_t refresh_rate;
85 uint8_t *buffer;
86 uint8_t *cache;
87};
88
89struct ht16k33_priv {
90 struct i2c_client *client;
91 struct delayed_work work;
92 struct led_classdev led;
93 struct ht16k33_keypad keypad;
94 union {
95 struct ht16k33_fbdev fbdev;
96 struct linedisp linedisp;
97 };
98 enum display_type type;
99 uint8_t blink;
100};
101
102#define ht16k33_work_to_priv(p) \
103 container_of(p, struct ht16k33_priv, work.work)
104
105#define ht16k33_led_to_priv(p) \
106 container_of(p, struct ht16k33_priv, led)
107
108#define ht16k33_linedisp_to_priv(p) \
109 container_of(p, struct ht16k33_priv, linedisp)
110
111static const struct fb_fix_screeninfo ht16k33_fb_fix = {
112 .id = DRIVER_NAME,
113 .type = FB_TYPE_PACKED_PIXELS,
114 .visual = FB_VISUAL_MONO10,
115 .xpanstep = 0,
116 .ypanstep = 0,
117 .ywrapstep = 0,
118 .line_length = HT16K33_MATRIX_LED_MAX_ROWS,
119 .accel = FB_ACCEL_NONE,
120};
121
122static 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,
127 .bits_per_pixel = 1,
128 .red = { 0, 1, 0 },
129 .green = { 0, 1, 0 },
130 .blue = { 0, 1, 0 },
131 .left_margin = 0,
132 .right_margin = 0,
133 .upper_margin = 0,
134 .lower_margin = 0,
135 .vmode = FB_VMODE_NONINTERLACED,
136};
137
138static int ht16k33_display_on(struct ht16k33_priv *priv)
139{
140 uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON | priv->blink;
141
142 return i2c_smbus_write_byte(client: priv->client, value: data);
143}
144
145static int ht16k33_display_off(struct ht16k33_priv *priv)
146{
147 return i2c_smbus_write_byte(client: priv->client, REG_DISPLAY_SETUP);
148}
149
150static int ht16k33_brightness_set(struct ht16k33_priv *priv,
151 unsigned int brightness)
152{
153 int err;
154
155 if (brightness == 0) {
156 priv->blink = REG_DISPLAY_SETUP_BLINK_OFF;
157 return ht16k33_display_off(priv);
158 }
159
160 err = ht16k33_display_on(priv);
161 if (err)
162 return err;
163
164 return i2c_smbus_write_byte(client: priv->client,
165 REG_BRIGHTNESS | (brightness - 1));
166}
167
168static int ht16k33_brightness_set_blocking(struct led_classdev *led_cdev,
169 enum led_brightness brightness)
170{
171 struct ht16k33_priv *priv = ht16k33_led_to_priv(led_cdev);
172
173 return ht16k33_brightness_set(priv, brightness);
174}
175
176static int ht16k33_blink_set(struct led_classdev *led_cdev,
177 unsigned long *delay_on, unsigned long *delay_off)
178{
179 struct ht16k33_priv *priv = ht16k33_led_to_priv(led_cdev);
180 unsigned int delay;
181 uint8_t blink;
182 int err;
183
184 if (!*delay_on && !*delay_off) {
185 blink = REG_DISPLAY_SETUP_BLINK_1HZ;
186 delay = 1000;
187 } else if (*delay_on <= 750) {
188 blink = REG_DISPLAY_SETUP_BLINK_2HZ;
189 delay = 500;
190 } else if (*delay_on <= 1500) {
191 blink = REG_DISPLAY_SETUP_BLINK_1HZ;
192 delay = 1000;
193 } else {
194 blink = REG_DISPLAY_SETUP_BLINK_0HZ5;
195 delay = 2000;
196 }
197
198 err = i2c_smbus_write_byte(client: priv->client,
199 REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON |
200 blink);
201 if (err)
202 return err;
203
204 priv->blink = blink;
205 *delay_on = *delay_off = delay;
206 return 0;
207}
208
209static void ht16k33_fb_queue(struct ht16k33_priv *priv)
210{
211 struct ht16k33_fbdev *fbdev = &priv->fbdev;
212
213 schedule_delayed_work(dwork: &priv->work, HZ / fbdev->refresh_rate);
214}
215
216/*
217 * This gets the fb data from cache and copies it to ht16k33 display RAM
218 */
219static void ht16k33_fb_update(struct work_struct *work)
220{
221 struct ht16k33_priv *priv = ht16k33_work_to_priv(work);
222 struct ht16k33_fbdev *fbdev = &priv->fbdev;
223
224 uint8_t *p1, *p2;
225 int len, pos = 0, first = -1;
226
227 p1 = fbdev->cache;
228 p2 = fbdev->buffer;
229
230 /* Search for the first byte with changes */
231 while (pos < HT16K33_FB_SIZE && first < 0) {
232 if (*(p1++) - *(p2++))
233 first = pos;
234 pos++;
235 }
236
237 /* No changes found */
238 if (first < 0)
239 goto requeue;
240
241 len = HT16K33_FB_SIZE - first;
242 p1 = fbdev->cache + HT16K33_FB_SIZE - 1;
243 p2 = fbdev->buffer + HT16K33_FB_SIZE - 1;
244
245 /* Determine i2c transfer length */
246 while (len > 1) {
247 if (*(p1--) - *(p2--))
248 break;
249 len--;
250 }
251
252 p1 = fbdev->cache + first;
253 p2 = fbdev->buffer + first;
254 if (!i2c_smbus_write_i2c_block_data(client: priv->client, command: first, length: len, values: p2))
255 memcpy(p1, p2, len);
256requeue:
257 ht16k33_fb_queue(priv);
258}
259
260static int ht16k33_initialize(struct ht16k33_priv *priv)
261{
262 uint8_t data[HT16K33_FB_SIZE];
263 uint8_t byte;
264 int err;
265
266 /* Clear RAM (8 * 16 bits) */
267 memset(data, 0, sizeof(data));
268 err = i2c_smbus_write_block_data(client: priv->client, command: 0, length: sizeof(data), values: data);
269 if (err)
270 return err;
271
272 /* Turn on internal oscillator */
273 byte = REG_SYSTEM_SETUP_OSC_ON | REG_SYSTEM_SETUP;
274 err = i2c_smbus_write_byte(client: priv->client, value: byte);
275 if (err)
276 return err;
277
278 /* Configure INT pin */
279 byte = REG_ROWINT_SET | REG_ROWINT_SET_INT_ACT_HIGH;
280 if (priv->client->irq > 0)
281 byte |= REG_ROWINT_SET_INT_EN;
282 return i2c_smbus_write_byte(client: priv->client, value: byte);
283}
284
285static int ht16k33_bl_update_status(struct backlight_device *bl)
286{
287 int brightness = bl->props.brightness;
288 struct ht16k33_priv *priv = bl_get_data(bl_dev: bl);
289
290 if (bl->props.power != FB_BLANK_UNBLANK ||
291 bl->props.fb_blank != FB_BLANK_UNBLANK ||
292 bl->props.state & BL_CORE_FBBLANK)
293 brightness = 0;
294
295 return ht16k33_brightness_set(priv, brightness);
296}
297
298static int ht16k33_bl_check_fb(struct backlight_device *bl, struct fb_info *fi)
299{
300 struct ht16k33_priv *priv = bl_get_data(bl_dev: bl);
301
302 return (fi == NULL) || (fi->par == priv);
303}
304
305static const struct backlight_ops ht16k33_bl_ops = {
306 .update_status = ht16k33_bl_update_status,
307 .check_fb = ht16k33_bl_check_fb,
308};
309
310/*
311 * Blank events will be passed to the actual device handling the backlight when
312 * we return zero here.
313 */
314static int ht16k33_blank(int blank, struct fb_info *info)
315{
316 return 0;
317}
318
319static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
320{
321 struct ht16k33_priv *priv = info->par;
322 struct page *pages = virt_to_page(priv->fbdev.buffer);
323
324 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
325
326 return vm_map_pages_zero(vma, pages: &pages, num: 1);
327}
328
329static const struct fb_ops ht16k33_fb_ops = {
330 .owner = THIS_MODULE,
331 __FB_DEFAULT_SYSMEM_OPS_RDWR,
332 .fb_blank = ht16k33_blank,
333 __FB_DEFAULT_SYSMEM_OPS_DRAW,
334 .fb_mmap = ht16k33_mmap,
335};
336
337/*
338 * This gets the keys from keypad and reports it to input subsystem.
339 * Returns true if a key is pressed.
340 */
341static bool ht16k33_keypad_scan(struct ht16k33_keypad *keypad)
342{
343 const unsigned short *keycodes = keypad->dev->keycode;
344 u16 new_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
345 __le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS];
346 unsigned long bits_changed;
347 int row, col, code;
348 int rc;
349 bool pressed = false;
350
351 rc = i2c_smbus_read_i2c_block_data(client: keypad->client, command: 0x40,
352 length: sizeof(data), values: (u8 *)data);
353 if (rc != sizeof(data)) {
354 dev_err(&keypad->client->dev,
355 "Failed to read key data, rc=%d\n", rc);
356 return false;
357 }
358
359 for (col = 0; col < keypad->cols; col++) {
360 new_state[col] = le16_to_cpu(data[col]);
361 if (new_state[col])
362 pressed = true;
363 bits_changed = keypad->last_key_state[col] ^ new_state[col];
364
365 for_each_set_bit(row, &bits_changed, BITS_PER_LONG) {
366 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
367 input_event(dev: keypad->dev, EV_MSC, MSC_SCAN, value: code);
368 input_report_key(dev: keypad->dev, code: keycodes[code],
369 value: new_state[col] & BIT(row));
370 }
371 }
372 input_sync(dev: keypad->dev);
373 memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols);
374
375 return pressed;
376}
377
378static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
379{
380 struct ht16k33_keypad *keypad = dev;
381
382 do {
383 wait_event_timeout(keypad->wait, keypad->stopped,
384 msecs_to_jiffies(keypad->debounce_ms));
385 if (keypad->stopped)
386 break;
387 } while (ht16k33_keypad_scan(keypad));
388
389 return IRQ_HANDLED;
390}
391
392static int ht16k33_keypad_start(struct input_dev *dev)
393{
394 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
395
396 keypad->stopped = false;
397 mb();
398 enable_irq(irq: keypad->client->irq);
399
400 return 0;
401}
402
403static void ht16k33_keypad_stop(struct input_dev *dev)
404{
405 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
406
407 keypad->stopped = true;
408 mb();
409 wake_up(&keypad->wait);
410 disable_irq(irq: keypad->client->irq);
411}
412
413static void ht16k33_seg7_update(struct work_struct *work)
414{
415 struct ht16k33_priv *priv = ht16k33_work_to_priv(work);
416 struct linedisp_map *map = priv->linedisp.map;
417 char *s = priv->linedisp.buf;
418 uint8_t buf[9];
419
420 buf[0] = map_to_seg7(map: &map->map.seg7, c: *s++);
421 buf[1] = 0;
422 buf[2] = map_to_seg7(map: &map->map.seg7, c: *s++);
423 buf[3] = 0;
424 buf[4] = 0;
425 buf[5] = 0;
426 buf[6] = map_to_seg7(map: &map->map.seg7, c: *s++);
427 buf[7] = 0;
428 buf[8] = map_to_seg7(map: &map->map.seg7, c: *s++);
429
430 i2c_smbus_write_i2c_block_data(client: priv->client, command: 0, ARRAY_SIZE(buf), values: buf);
431}
432
433static void ht16k33_seg14_update(struct work_struct *work)
434{
435 struct ht16k33_priv *priv = ht16k33_work_to_priv(work);
436 struct linedisp_map *map = priv->linedisp.map;
437 char *s = priv->linedisp.buf;
438 uint8_t buf[8];
439
440 put_unaligned_le16(val: map_to_seg14(map: &map->map.seg14, c: *s++), p: buf + 0);
441 put_unaligned_le16(val: map_to_seg14(map: &map->map.seg14, c: *s++), p: buf + 2);
442 put_unaligned_le16(val: map_to_seg14(map: &map->map.seg14, c: *s++), p: buf + 4);
443 put_unaligned_le16(val: map_to_seg14(map: &map->map.seg14, c: *s++), p: buf + 6);
444
445 i2c_smbus_write_i2c_block_data(client: priv->client, command: 0, ARRAY_SIZE(buf), values: buf);
446}
447
448static int ht16k33_linedisp_get_map_type(struct linedisp *linedisp)
449{
450 struct ht16k33_priv *priv = ht16k33_linedisp_to_priv(linedisp);
451
452 switch (priv->type) {
453 case DISP_QUAD_7SEG:
454 INIT_DELAYED_WORK(&priv->work, ht16k33_seg7_update);
455 return LINEDISP_MAP_SEG7;
456
457 case DISP_QUAD_14SEG:
458 INIT_DELAYED_WORK(&priv->work, ht16k33_seg14_update);
459 return LINEDISP_MAP_SEG14;
460
461 default:
462 return -EINVAL;
463 }
464}
465
466static void ht16k33_linedisp_update(struct linedisp *linedisp)
467{
468 struct ht16k33_priv *priv = ht16k33_linedisp_to_priv(linedisp);
469
470 schedule_delayed_work(dwork: &priv->work, delay: 0);
471}
472
473static const struct linedisp_ops ht16k33_linedisp_ops = {
474 .get_map_type = ht16k33_linedisp_get_map_type,
475 .update = ht16k33_linedisp_update,
476};
477
478static int ht16k33_led_probe(struct device *dev, struct led_classdev *led,
479 unsigned int brightness)
480{
481 struct led_init_data init_data = {};
482 int err;
483
484 /* The LED is optional */
485 init_data.fwnode = device_get_named_child_node(dev, childname: "led");
486 if (!init_data.fwnode)
487 return 0;
488
489 init_data.devicename = "auxdisplay";
490 init_data.devname_mandatory = true;
491
492 led->brightness_set_blocking = ht16k33_brightness_set_blocking;
493 led->blink_set = ht16k33_blink_set;
494 led->flags = LED_CORE_SUSPENDRESUME;
495 led->brightness = brightness;
496 led->max_brightness = MAX_BRIGHTNESS;
497
498 err = devm_led_classdev_register_ext(parent: dev, led_cdev: led, init_data: &init_data);
499 if (err)
500 dev_err(dev, "Failed to register LED\n");
501
502 return err;
503}
504
505static int ht16k33_keypad_probe(struct i2c_client *client,
506 struct ht16k33_keypad *keypad)
507{
508 struct device *dev = &client->dev;
509 u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
510 u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
511 int err;
512
513 keypad->client = client;
514 init_waitqueue_head(&keypad->wait);
515
516 keypad->dev = devm_input_allocate_device(dev);
517 if (!keypad->dev)
518 return -ENOMEM;
519
520 input_set_drvdata(dev: keypad->dev, data: keypad);
521
522 keypad->dev->name = DRIVER_NAME"-keypad";
523 keypad->dev->id.bustype = BUS_I2C;
524 keypad->dev->open = ht16k33_keypad_start;
525 keypad->dev->close = ht16k33_keypad_stop;
526
527 if (!device_property_read_bool(dev, propname: "linux,no-autorepeat"))
528 __set_bit(EV_REP, keypad->dev->evbit);
529
530 err = device_property_read_u32(dev, propname: "debounce-delay-ms",
531 val: &keypad->debounce_ms);
532 if (err) {
533 dev_err(dev, "key debounce delay not specified\n");
534 return err;
535 }
536
537 err = matrix_keypad_parse_properties(dev, rows: &rows, cols: &cols);
538 if (err)
539 return err;
540 if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS ||
541 cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) {
542 dev_err(dev, "%u rows or %u cols out of range in DT\n", rows,
543 cols);
544 return -ERANGE;
545 }
546
547 keypad->rows = rows;
548 keypad->cols = cols;
549 keypad->row_shift = get_count_order(count: cols);
550
551 err = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL,
552 input_dev: keypad->dev);
553 if (err) {
554 dev_err(dev, "failed to build keymap\n");
555 return err;
556 }
557
558 err = devm_request_threaded_irq(dev, irq: client->irq, NULL,
559 thread_fn: ht16k33_keypad_irq_thread,
560 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
561 DRIVER_NAME, dev_id: keypad);
562 if (err) {
563 dev_err(dev, "irq request failed %d, error %d\n", client->irq,
564 err);
565 return err;
566 }
567
568 ht16k33_keypad_stop(dev: keypad->dev);
569
570 return input_register_device(keypad->dev);
571}
572
573static int ht16k33_fbdev_probe(struct device *dev, struct ht16k33_priv *priv,
574 uint32_t brightness)
575{
576 struct ht16k33_fbdev *fbdev = &priv->fbdev;
577 struct backlight_device *bl = NULL;
578 int err;
579
580 if (priv->led.dev) {
581 err = ht16k33_brightness_set(priv, brightness);
582 if (err)
583 return err;
584 } else {
585 /* backwards compatibility with DT lacking an led subnode */
586 struct backlight_properties bl_props;
587
588 memset(&bl_props, 0, sizeof(struct backlight_properties));
589 bl_props.type = BACKLIGHT_RAW;
590 bl_props.max_brightness = MAX_BRIGHTNESS;
591
592 bl = devm_backlight_device_register(dev, DRIVER_NAME"-bl", parent: dev,
593 devdata: priv, ops: &ht16k33_bl_ops,
594 props: &bl_props);
595 if (IS_ERR(ptr: bl)) {
596 dev_err(dev, "failed to register backlight\n");
597 return PTR_ERR(ptr: bl);
598 }
599
600 bl->props.brightness = brightness;
601 ht16k33_bl_update_status(bl);
602 }
603
604 /* Framebuffer (2 bytes per column) */
605 BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
606 fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
607 if (!fbdev->buffer)
608 return -ENOMEM;
609
610 fbdev->cache = devm_kmalloc(dev, HT16K33_FB_SIZE, GFP_KERNEL);
611 if (!fbdev->cache) {
612 err = -ENOMEM;
613 goto err_fbdev_buffer;
614 }
615
616 fbdev->info = framebuffer_alloc(size: 0, dev);
617 if (!fbdev->info) {
618 err = -ENOMEM;
619 goto err_fbdev_buffer;
620 }
621
622 err = device_property_read_u32(dev, propname: "refresh-rate-hz",
623 val: &fbdev->refresh_rate);
624 if (err) {
625 dev_err(dev, "refresh rate not specified\n");
626 goto err_fbdev_info;
627 }
628 fb_bl_default_curve(fb_info: fbdev->info, off: 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
629
630 INIT_DELAYED_WORK(&priv->work, ht16k33_fb_update);
631 fbdev->info->fbops = &ht16k33_fb_ops;
632 fbdev->info->flags |= FBINFO_VIRTFB;
633 fbdev->info->screen_buffer = fbdev->buffer;
634 fbdev->info->screen_size = HT16K33_FB_SIZE;
635 fbdev->info->fix = ht16k33_fb_fix;
636 fbdev->info->var = ht16k33_fb_var;
637 fbdev->info->bl_dev = bl;
638 fbdev->info->pseudo_palette = NULL;
639 fbdev->info->par = priv;
640
641 err = register_framebuffer(fb_info: fbdev->info);
642 if (err)
643 goto err_fbdev_info;
644
645 ht16k33_fb_queue(priv);
646 return 0;
647
648err_fbdev_info:
649 framebuffer_release(info: fbdev->info);
650err_fbdev_buffer:
651 free_page((unsigned long) fbdev->buffer);
652
653 return err;
654}
655
656static int ht16k33_seg_probe(struct device *dev, struct ht16k33_priv *priv,
657 uint32_t brightness)
658{
659 struct linedisp *linedisp = &priv->linedisp;
660 int err;
661
662 err = ht16k33_brightness_set(priv, brightness);
663 if (err)
664 return err;
665
666 return linedisp_register(linedisp, parent: dev, num_chars: 4, ops: &ht16k33_linedisp_ops);
667}
668
669static int ht16k33_probe(struct i2c_client *client)
670{
671 struct device *dev = &client->dev;
672 const struct of_device_id *id;
673 struct ht16k33_priv *priv;
674 uint32_t dft_brightness;
675 int err;
676
677 if (!i2c_check_functionality(adap: client->adapter, I2C_FUNC_I2C)) {
678 dev_err(dev, "i2c_check_functionality error\n");
679 return -EIO;
680 }
681
682 priv = devm_kzalloc(dev, size: sizeof(*priv), GFP_KERNEL);
683 if (!priv)
684 return -ENOMEM;
685
686 priv->client = client;
687 id = i2c_of_match_device(matches: dev->driver->of_match_table, client);
688 if (id)
689 priv->type = (uintptr_t)id->data;
690 i2c_set_clientdata(client, data: priv);
691
692 err = ht16k33_initialize(priv);
693 if (err)
694 return err;
695
696 err = device_property_read_u32(dev, propname: "default-brightness-level",
697 val: &dft_brightness);
698 if (err) {
699 dft_brightness = MAX_BRIGHTNESS;
700 } else if (dft_brightness > MAX_BRIGHTNESS) {
701 dev_warn(dev,
702 "invalid default brightness level: %u, using %u\n",
703 dft_brightness, MAX_BRIGHTNESS);
704 dft_brightness = MAX_BRIGHTNESS;
705 }
706
707 /* LED */
708 err = ht16k33_led_probe(dev, led: &priv->led, brightness: dft_brightness);
709 if (err)
710 return err;
711
712 /* Keypad */
713 if (client->irq > 0) {
714 err = ht16k33_keypad_probe(client, keypad: &priv->keypad);
715 if (err)
716 return err;
717 }
718
719 switch (priv->type) {
720 case DISP_MATRIX:
721 /* Frame Buffer Display */
722 err = ht16k33_fbdev_probe(dev, priv, brightness: dft_brightness);
723 break;
724
725 case DISP_QUAD_7SEG:
726 case DISP_QUAD_14SEG:
727 /* Segment Display */
728 err = ht16k33_seg_probe(dev, priv, brightness: dft_brightness);
729 break;
730
731 default:
732 return -EINVAL;
733 }
734 return err;
735}
736
737static void ht16k33_remove(struct i2c_client *client)
738{
739 struct ht16k33_priv *priv = i2c_get_clientdata(client);
740 struct ht16k33_fbdev *fbdev = &priv->fbdev;
741
742 cancel_delayed_work_sync(dwork: &priv->work);
743
744 switch (priv->type) {
745 case DISP_MATRIX:
746 unregister_framebuffer(fb_info: fbdev->info);
747 framebuffer_release(info: fbdev->info);
748 free_page((unsigned long)fbdev->buffer);
749 break;
750
751 case DISP_QUAD_7SEG:
752 case DISP_QUAD_14SEG:
753 linedisp_unregister(linedisp: &priv->linedisp);
754 break;
755
756 default:
757 break;
758 }
759}
760
761static const struct i2c_device_id ht16k33_i2c_match[] = {
762 { "ht16k33", 0 },
763 { }
764};
765MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
766
767static const struct of_device_id ht16k33_of_match[] = {
768 {
769 /* 0.56" 4-Digit 7-Segment FeatherWing Display (Red) */
770 .compatible = "adafruit,3108", .data = (void *)DISP_QUAD_7SEG,
771 }, {
772 /* 0.54" Quad Alphanumeric FeatherWing Display (Red) */
773 .compatible = "adafruit,3130", .data = (void *)DISP_QUAD_14SEG,
774 }, {
775 /* Generic, assumed Dot-Matrix Display */
776 .compatible = "holtek,ht16k33", .data = (void *)DISP_MATRIX,
777 },
778 { }
779};
780MODULE_DEVICE_TABLE(of, ht16k33_of_match);
781
782static struct i2c_driver ht16k33_driver = {
783 .probe = ht16k33_probe,
784 .remove = ht16k33_remove,
785 .driver = {
786 .name = DRIVER_NAME,
787 .of_match_table = ht16k33_of_match,
788 },
789 .id_table = ht16k33_i2c_match,
790};
791module_i2c_driver(ht16k33_driver);
792
793MODULE_DESCRIPTION("Holtek HT16K33 driver");
794MODULE_LICENSE("GPL");
795MODULE_IMPORT_NS(LINEDISP);
796MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");
797

source code of linux/drivers/auxdisplay/ht16k33.c