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