4 * Author: Robin van der Gracht <robin@protonic.nl>
6 * Copyright: (C) 2016 Protonic Holland.
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.
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.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
21 #include <linux/i2c.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>
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)
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)
42 #define REG_BRIGHTNESS 0xE0
45 #define DRIVER_NAME "ht16k33"
47 #define MIN_BRIGHTNESS 0x1
48 #define MAX_BRIGHTNESS 0x10
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
55 #define BYTES_PER_ROW (HT16K33_MATRIX_LED_MAX_ROWS / 8)
56 #define HT16K33_FB_SIZE (HT16K33_MATRIX_LED_MAX_COLS * BYTES_PER_ROW)
58 struct ht16k33_keypad {
59 struct i2c_client *client;
60 struct input_dev *dev;
65 uint16_t last_key_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
67 wait_queue_head_t wait;
71 struct ht16k33_fbdev {
73 uint32_t refresh_rate;
76 struct delayed_work work;
80 struct i2c_client *client;
81 struct ht16k33_keypad keypad;
82 struct ht16k33_fbdev fbdev;
85 static struct fb_fix_screeninfo ht16k33_fb_fix = {
87 .type = FB_TYPE_PACKED_PIXELS,
88 .visual = FB_VISUAL_MONO10,
92 .line_length = HT16K33_MATRIX_LED_MAX_ROWS,
93 .accel = FB_ACCEL_NONE,
96 static struct fb_var_screeninfo ht16k33_fb_var = {
97 .xres = HT16K33_MATRIX_LED_MAX_ROWS,
98 .yres = HT16K33_MATRIX_LED_MAX_COLS,
99 .xres_virtual = HT16K33_MATRIX_LED_MAX_ROWS,
100 .yres_virtual = HT16K33_MATRIX_LED_MAX_COLS,
103 .green = { 0, 1, 0 },
109 .vmode = FB_VMODE_NONINTERLACED,
112 static int ht16k33_display_on(struct ht16k33_priv *priv)
114 uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON;
116 return i2c_smbus_write_byte(priv->client, data);
119 static int ht16k33_display_off(struct ht16k33_priv *priv)
121 return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP);
124 static void ht16k33_fb_queue(struct ht16k33_priv *priv)
126 struct ht16k33_fbdev *fbdev = &priv->fbdev;
128 schedule_delayed_work(&fbdev->work, HZ / fbdev->refresh_rate);
132 * This gets the fb data from cache and copies it to ht16k33 display RAM
134 static void ht16k33_fb_update(struct work_struct *work)
136 struct ht16k33_fbdev *fbdev =
137 container_of(work, struct ht16k33_fbdev, work.work);
138 struct ht16k33_priv *priv =
139 container_of(fbdev, struct ht16k33_priv, fbdev);
142 int len, pos = 0, first = -1;
147 /* Search for the first byte with changes */
148 while (pos < HT16K33_FB_SIZE && first < 0) {
149 if (*(p1++) - *(p2++))
154 /* No changes found */
158 len = HT16K33_FB_SIZE - first;
159 p1 = fbdev->cache + HT16K33_FB_SIZE - 1;
160 p2 = fbdev->buffer + HT16K33_FB_SIZE - 1;
162 /* Determine i2c transfer length */
164 if (*(p1--) - *(p2--))
169 p1 = fbdev->cache + first;
170 p2 = fbdev->buffer + first;
171 if (!i2c_smbus_write_i2c_block_data(priv->client, first, len, p2))
174 ht16k33_fb_queue(priv);
177 static int ht16k33_initialize(struct ht16k33_priv *priv)
181 uint8_t data[HT16K33_MATRIX_LED_MAX_COLS * 2];
183 /* Clear RAM (8 * 16 bits) */
184 memset(data, 0, sizeof(data));
185 err = i2c_smbus_write_block_data(priv->client, 0, sizeof(data), data);
189 /* Turn on internal oscillator */
190 byte = REG_SYSTEM_SETUP_OSC_ON | REG_SYSTEM_SETUP;
191 err = i2c_smbus_write_byte(priv->client, byte);
195 /* Configure INT pin */
196 byte = REG_ROWINT_SET | REG_ROWINT_SET_INT_ACT_HIGH;
197 if (priv->client->irq > 0)
198 byte |= REG_ROWINT_SET_INT_EN;
199 return i2c_smbus_write_byte(priv->client, byte);
202 static int ht16k33_bl_update_status(struct backlight_device *bl)
204 int brightness = bl->props.brightness;
205 struct ht16k33_priv *priv = bl_get_data(bl);
207 if (bl->props.power != FB_BLANK_UNBLANK ||
208 bl->props.fb_blank != FB_BLANK_UNBLANK ||
209 bl->props.state & BL_CORE_FBBLANK || brightness == 0) {
210 return ht16k33_display_off(priv);
213 ht16k33_display_on(priv);
214 return i2c_smbus_write_byte(priv->client,
215 REG_BRIGHTNESS | (brightness - 1));
218 static int ht16k33_bl_check_fb(struct backlight_device *bl, struct fb_info *fi)
220 struct ht16k33_priv *priv = bl_get_data(bl);
222 return (fi == NULL) || (fi->par == priv);
225 static const struct backlight_ops ht16k33_bl_ops = {
226 .update_status = ht16k33_bl_update_status,
227 .check_fb = ht16k33_bl_check_fb,
231 * Blank events will be passed to the actual device handling the backlight when
232 * we return zero here.
234 static int ht16k33_blank(int blank, struct fb_info *info)
239 static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
241 struct ht16k33_priv *priv = info->par;
243 return vm_insert_page(vma, vma->vm_start,
244 virt_to_page(priv->fbdev.buffer));
247 static struct fb_ops ht16k33_fb_ops = {
248 .owner = THIS_MODULE,
249 .fb_read = fb_sys_read,
250 .fb_write = fb_sys_write,
251 .fb_blank = ht16k33_blank,
252 .fb_fillrect = sys_fillrect,
253 .fb_copyarea = sys_copyarea,
254 .fb_imageblit = sys_imageblit,
255 .fb_mmap = ht16k33_mmap,
259 * This gets the keys from keypad and reports it to input subsystem.
260 * Returns true if a key is pressed.
262 static bool ht16k33_keypad_scan(struct ht16k33_keypad *keypad)
264 const unsigned short *keycodes = keypad->dev->keycode;
265 u16 new_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
266 __le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS];
267 unsigned long bits_changed;
270 bool pressed = false;
272 rc = i2c_smbus_read_i2c_block_data(keypad->client, 0x40,
273 sizeof(data), (u8 *)data);
274 if (rc != sizeof(data)) {
275 dev_err(&keypad->client->dev,
276 "Failed to read key data, rc=%d\n", rc);
280 for (col = 0; col < keypad->cols; col++) {
281 new_state[col] = le16_to_cpu(data[col]);
284 bits_changed = keypad->last_key_state[col] ^ new_state[col];
286 for_each_set_bit(row, &bits_changed, BITS_PER_LONG) {
287 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
288 input_event(keypad->dev, EV_MSC, MSC_SCAN, code);
289 input_report_key(keypad->dev, keycodes[code],
290 new_state[col] & BIT(row));
293 input_sync(keypad->dev);
294 memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols);
299 static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
301 struct ht16k33_keypad *keypad = dev;
304 wait_event_timeout(keypad->wait, keypad->stopped,
305 msecs_to_jiffies(keypad->debounce_ms));
308 } while (ht16k33_keypad_scan(keypad));
313 static int ht16k33_keypad_start(struct input_dev *dev)
315 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
317 keypad->stopped = false;
319 enable_irq(keypad->client->irq);
324 static void ht16k33_keypad_stop(struct input_dev *dev)
326 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
328 keypad->stopped = true;
330 wake_up(&keypad->wait);
331 disable_irq(keypad->client->irq);
334 static int ht16k33_keypad_probe(struct i2c_client *client,
335 struct ht16k33_keypad *keypad)
337 struct device_node *node = client->dev.of_node;
338 u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
339 u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
342 keypad->client = client;
343 init_waitqueue_head(&keypad->wait);
345 keypad->dev = devm_input_allocate_device(&client->dev);
349 input_set_drvdata(keypad->dev, keypad);
351 keypad->dev->name = DRIVER_NAME"-keypad";
352 keypad->dev->id.bustype = BUS_I2C;
353 keypad->dev->open = ht16k33_keypad_start;
354 keypad->dev->close = ht16k33_keypad_stop;
356 if (!of_get_property(node, "linux,no-autorepeat", NULL))
357 __set_bit(EV_REP, keypad->dev->evbit);
359 err = of_property_read_u32(node, "debounce-delay-ms",
360 &keypad->debounce_ms);
362 dev_err(&client->dev, "key debounce delay not specified\n");
366 err = matrix_keypad_parse_of_params(&client->dev, &rows, &cols);
369 if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS ||
370 cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) {
371 dev_err(&client->dev, "%u rows or %u cols out of range in DT\n",
378 keypad->row_shift = get_count_order(cols);
380 err = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL,
383 dev_err(&client->dev, "failed to build keymap\n");
387 err = devm_request_threaded_irq(&client->dev, client->irq,
388 NULL, ht16k33_keypad_irq_thread,
389 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
390 DRIVER_NAME, keypad);
392 dev_err(&client->dev, "irq request failed %d, error %d\n",
397 ht16k33_keypad_stop(keypad->dev);
399 err = input_register_device(keypad->dev);
406 static int ht16k33_probe(struct i2c_client *client,
407 const struct i2c_device_id *id)
410 uint32_t dft_brightness;
411 struct backlight_device *bl;
412 struct backlight_properties bl_props;
413 struct ht16k33_priv *priv;
414 struct ht16k33_fbdev *fbdev;
415 struct device_node *node = client->dev.of_node;
417 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
418 dev_err(&client->dev, "i2c_check_functionality error\n");
422 if (client->irq <= 0) {
423 dev_err(&client->dev, "No IRQ specified\n");
427 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
431 priv->client = client;
432 i2c_set_clientdata(client, priv);
433 fbdev = &priv->fbdev;
435 err = ht16k33_initialize(priv);
440 memset(&bl_props, 0, sizeof(struct backlight_properties));
441 bl_props.type = BACKLIGHT_RAW;
442 bl_props.max_brightness = MAX_BRIGHTNESS;
444 bl = devm_backlight_device_register(&client->dev, DRIVER_NAME"-bl",
446 &ht16k33_bl_ops, &bl_props);
448 dev_err(&client->dev, "failed to register backlight\n");
452 err = of_property_read_u32(node, "default-brightness-level",
455 dft_brightness = MAX_BRIGHTNESS;
456 } else if (dft_brightness > MAX_BRIGHTNESS) {
457 dev_warn(&client->dev,
458 "invalid default brightness level: %u, using %u\n",
459 dft_brightness, MAX_BRIGHTNESS);
460 dft_brightness = MAX_BRIGHTNESS;
463 bl->props.brightness = dft_brightness;
464 ht16k33_bl_update_status(bl);
466 /* Framebuffer (2 bytes per column) */
467 BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
468 fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
472 fbdev->cache = devm_kmalloc(&client->dev, HT16K33_FB_SIZE, GFP_KERNEL);
475 goto err_fbdev_buffer;
478 fbdev->info = framebuffer_alloc(0, &client->dev);
481 goto err_fbdev_buffer;
484 err = of_property_read_u32(node, "refresh-rate-hz",
485 &fbdev->refresh_rate);
487 dev_err(&client->dev, "refresh rate not specified\n");
490 fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
492 INIT_DELAYED_WORK(&fbdev->work, ht16k33_fb_update);
493 fbdev->info->fbops = &ht16k33_fb_ops;
494 fbdev->info->screen_base = (char __iomem *) fbdev->buffer;
495 fbdev->info->screen_size = HT16K33_FB_SIZE;
496 fbdev->info->fix = ht16k33_fb_fix;
497 fbdev->info->var = ht16k33_fb_var;
498 fbdev->info->bl_dev = bl;
499 fbdev->info->pseudo_palette = NULL;
500 fbdev->info->flags = FBINFO_FLAG_DEFAULT;
501 fbdev->info->par = priv;
503 err = register_framebuffer(fbdev->info);
507 err = ht16k33_keypad_probe(client, &priv->keypad);
509 goto err_fbdev_unregister;
511 ht16k33_fb_queue(priv);
514 err_fbdev_unregister:
515 unregister_framebuffer(fbdev->info);
517 framebuffer_release(fbdev->info);
519 free_page((unsigned long) fbdev->buffer);
524 static int ht16k33_remove(struct i2c_client *client)
526 struct ht16k33_priv *priv = i2c_get_clientdata(client);
527 struct ht16k33_fbdev *fbdev = &priv->fbdev;
529 cancel_delayed_work_sync(&fbdev->work);
530 unregister_framebuffer(fbdev->info);
531 framebuffer_release(fbdev->info);
532 free_page((unsigned long) fbdev->buffer);
537 static const struct i2c_device_id ht16k33_i2c_match[] = {
541 MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
543 static const struct of_device_id ht16k33_of_match[] = {
544 { .compatible = "holtek,ht16k33", },
547 MODULE_DEVICE_TABLE(of, ht16k33_of_match);
549 static struct i2c_driver ht16k33_driver = {
550 .probe = ht16k33_probe,
551 .remove = ht16k33_remove,
554 .of_match_table = of_match_ptr(ht16k33_of_match),
556 .id_table = ht16k33_i2c_match,
558 module_i2c_driver(ht16k33_driver);
560 MODULE_DESCRIPTION("Holtek HT16K33 driver");
561 MODULE_LICENSE("GPL");
562 MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");