blob: aef3dc87dc9f5ed295fcccf80a5ca5af6b0fa7b3 [file] [log] [blame]
Miguel Ojeda351f683b2018-02-17 20:33:13 +01001// SPDX-License-Identifier: GPL-2.0
Robin van der Gracht8992da42016-11-07 10:56:35 +01002/*
3 * HT16K33 driver
4 *
5 * Author: Robin van der Gracht <robin@protonic.nl>
6 *
7 * Copyright: (C) 2016 Protonic Holland.
Geert Uytterhoevena0428722021-10-19 16:45:17 +02008 * Copyright (C) 2021 Glider bv
Robin van der Gracht8992da42016-11-07 10:56:35 +01009 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/i2c.h>
15#include <linux/of.h>
16#include <linux/fb.h>
17#include <linux/slab.h>
18#include <linux/backlight.h>
19#include <linux/input.h>
20#include <linux/input/matrix_keypad.h>
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +020021#include <linux/leds.h>
Robin van der Gracht8992da42016-11-07 10:56:35 +010022#include <linux/workqueue.h>
23#include <linux/mm.h>
24
Geert Uytterhoevena0428722021-10-19 16:45:17 +020025#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
Robin van der Gracht8992da42016-11-07 10:56:35 +010032/* 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)
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +020038#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)
Robin van der Gracht8992da42016-11-07 10:56:35 +010042
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
Geert Uytterhoevena0428722021-10-19 16:45:17 +020063enum display_type {
64 DISP_MATRIX = 0,
65 DISP_QUAD_7SEG,
66 DISP_QUAD_14SEG,
67};
68
Robin van der Gracht8992da42016-11-07 10:56:35 +010069struct ht16k33_keypad {
Dmitry Torokhovcac513f2017-02-09 10:15:52 -080070 struct i2c_client *client;
Robin van der Gracht8992da42016-11-07 10:56:35 +010071 struct input_dev *dev;
Robin van der Gracht8992da42016-11-07 10:56:35 +010072 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];
Dmitry Torokhovcac513f2017-02-09 10:15:52 -080077
78 wait_queue_head_t wait;
79 bool stopped;
Robin van der Gracht8992da42016-11-07 10:56:35 +010080};
81
82struct ht16k33_fbdev {
83 struct fb_info *info;
84 uint32_t refresh_rate;
85 uint8_t *buffer;
86 uint8_t *cache;
Robin van der Gracht8992da42016-11-07 10:56:35 +010087};
88
Geert Uytterhoevena0428722021-10-19 16:45:17 +020089struct ht16k33_seg {
90 struct linedisp linedisp;
91 union {
92 struct seg7_conversion_map seg7;
93 struct seg14_conversion_map seg14;
94 } map;
95 unsigned int map_size;
96 char curr[4];
97};
98
Robin van der Gracht8992da42016-11-07 10:56:35 +010099struct ht16k33_priv {
100 struct i2c_client *client;
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200101 struct delayed_work work;
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200102 struct led_classdev led;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100103 struct ht16k33_keypad keypad;
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200104 union {
105 struct ht16k33_fbdev fbdev;
106 struct ht16k33_seg seg;
107 };
108 enum display_type type;
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200109 uint8_t blink;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100110};
111
Nishka Dasguptaa180d022019-08-19 13:21:26 +0530112static const struct fb_fix_screeninfo ht16k33_fb_fix = {
Robin van der Gracht8992da42016-11-07 10:56:35 +0100113 .id = DRIVER_NAME,
114 .type = FB_TYPE_PACKED_PIXELS,
115 .visual = FB_VISUAL_MONO10,
116 .xpanstep = 0,
117 .ypanstep = 0,
118 .ywrapstep = 0,
119 .line_length = HT16K33_MATRIX_LED_MAX_ROWS,
120 .accel = FB_ACCEL_NONE,
121};
122
Nishka Dasguptaa180d022019-08-19 13:21:26 +0530123static const struct fb_var_screeninfo ht16k33_fb_var = {
Robin van der Gracht8992da42016-11-07 10:56:35 +0100124 .xres = HT16K33_MATRIX_LED_MAX_ROWS,
125 .yres = HT16K33_MATRIX_LED_MAX_COLS,
126 .xres_virtual = HT16K33_MATRIX_LED_MAX_ROWS,
127 .yres_virtual = HT16K33_MATRIX_LED_MAX_COLS,
128 .bits_per_pixel = 1,
129 .red = { 0, 1, 0 },
130 .green = { 0, 1, 0 },
131 .blue = { 0, 1, 0 },
132 .left_margin = 0,
133 .right_margin = 0,
134 .upper_margin = 0,
135 .lower_margin = 0,
136 .vmode = FB_VMODE_NONINTERLACED,
137};
138
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200139static const SEG7_DEFAULT_MAP(initial_map_seg7);
140static const SEG14_DEFAULT_MAP(initial_map_seg14);
141
142static ssize_t map_seg_show(struct device *dev, struct device_attribute *attr,
143 char *buf)
144{
145 struct ht16k33_priv *priv = dev_get_drvdata(dev);
146
147 memcpy(buf, &priv->seg.map, priv->seg.map_size);
148 return priv->seg.map_size;
149}
150
151static ssize_t map_seg_store(struct device *dev, struct device_attribute *attr,
152 const char *buf, size_t cnt)
153{
154 struct ht16k33_priv *priv = dev_get_drvdata(dev);
155
156 if (cnt != priv->seg.map_size)
157 return -EINVAL;
158
159 memcpy(&priv->seg.map, buf, cnt);
160 return cnt;
161}
162
163static DEVICE_ATTR(map_seg7, 0644, map_seg_show, map_seg_store);
164static DEVICE_ATTR(map_seg14, 0644, map_seg_show, map_seg_store);
165
Robin van der Gracht8992da42016-11-07 10:56:35 +0100166static int ht16k33_display_on(struct ht16k33_priv *priv)
167{
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200168 uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON | priv->blink;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100169
170 return i2c_smbus_write_byte(priv->client, data);
171}
172
173static int ht16k33_display_off(struct ht16k33_priv *priv)
174{
175 return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP);
176}
177
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200178static int ht16k33_brightness_set(struct ht16k33_priv *priv,
179 unsigned int brightness)
180{
181 int err;
182
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200183 if (brightness == 0) {
184 priv->blink = REG_DISPLAY_SETUP_BLINK_OFF;
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200185 return ht16k33_display_off(priv);
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200186 }
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200187
188 err = ht16k33_display_on(priv);
189 if (err)
190 return err;
191
192 return i2c_smbus_write_byte(priv->client,
193 REG_BRIGHTNESS | (brightness - 1));
194}
195
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200196static int ht16k33_brightness_set_blocking(struct led_classdev *led_cdev,
197 enum led_brightness brightness)
198{
199 struct ht16k33_priv *priv = container_of(led_cdev, struct ht16k33_priv,
200 led);
201
202 return ht16k33_brightness_set(priv, brightness);
203}
204
205static int ht16k33_blink_set(struct led_classdev *led_cdev,
206 unsigned long *delay_on, unsigned long *delay_off)
207{
208 struct ht16k33_priv *priv = container_of(led_cdev, struct ht16k33_priv,
209 led);
210 unsigned int delay;
211 uint8_t blink;
212 int err;
213
214 if (!*delay_on && !*delay_off) {
215 blink = REG_DISPLAY_SETUP_BLINK_1HZ;
216 delay = 1000;
217 } else if (*delay_on <= 750) {
218 blink = REG_DISPLAY_SETUP_BLINK_2HZ;
219 delay = 500;
220 } else if (*delay_on <= 1500) {
221 blink = REG_DISPLAY_SETUP_BLINK_1HZ;
222 delay = 1000;
223 } else {
224 blink = REG_DISPLAY_SETUP_BLINK_0HZ5;
225 delay = 2000;
226 }
227
228 err = i2c_smbus_write_byte(priv->client,
229 REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON |
230 blink);
231 if (err)
232 return err;
233
234 priv->blink = blink;
235 *delay_on = *delay_off = delay;
236 return 0;
237}
238
Robin van der Gracht8992da42016-11-07 10:56:35 +0100239static void ht16k33_fb_queue(struct ht16k33_priv *priv)
240{
241 struct ht16k33_fbdev *fbdev = &priv->fbdev;
242
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200243 schedule_delayed_work(&priv->work, HZ / fbdev->refresh_rate);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100244}
245
Robin van der Gracht8992da42016-11-07 10:56:35 +0100246/*
247 * This gets the fb data from cache and copies it to ht16k33 display RAM
248 */
249static void ht16k33_fb_update(struct work_struct *work)
250{
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200251 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
252 work.work);
253 struct ht16k33_fbdev *fbdev = &priv->fbdev;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100254
255 uint8_t *p1, *p2;
256 int len, pos = 0, first = -1;
257
258 p1 = fbdev->cache;
259 p2 = fbdev->buffer;
260
261 /* Search for the first byte with changes */
262 while (pos < HT16K33_FB_SIZE && first < 0) {
263 if (*(p1++) - *(p2++))
264 first = pos;
265 pos++;
266 }
267
268 /* No changes found */
269 if (first < 0)
270 goto requeue;
271
272 len = HT16K33_FB_SIZE - first;
273 p1 = fbdev->cache + HT16K33_FB_SIZE - 1;
274 p2 = fbdev->buffer + HT16K33_FB_SIZE - 1;
275
276 /* Determine i2c transfer length */
277 while (len > 1) {
278 if (*(p1--) - *(p2--))
279 break;
280 len--;
281 }
282
283 p1 = fbdev->cache + first;
284 p2 = fbdev->buffer + first;
285 if (!i2c_smbus_write_i2c_block_data(priv->client, first, len, p2))
286 memcpy(p1, p2, len);
287requeue:
288 ht16k33_fb_queue(priv);
289}
290
Robin van der Gracht8992da42016-11-07 10:56:35 +0100291static int ht16k33_initialize(struct ht16k33_priv *priv)
292{
Geert Uytterhoevenfb61e132021-10-19 16:45:10 +0200293 uint8_t data[HT16K33_FB_SIZE];
Robin van der Gracht8992da42016-11-07 10:56:35 +0100294 uint8_t byte;
295 int err;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100296
297 /* Clear RAM (8 * 16 bits) */
298 memset(data, 0, sizeof(data));
299 err = i2c_smbus_write_block_data(priv->client, 0, sizeof(data), data);
300 if (err)
301 return err;
302
303 /* Turn on internal oscillator */
304 byte = REG_SYSTEM_SETUP_OSC_ON | REG_SYSTEM_SETUP;
305 err = i2c_smbus_write_byte(priv->client, byte);
306 if (err)
307 return err;
308
309 /* Configure INT pin */
310 byte = REG_ROWINT_SET | REG_ROWINT_SET_INT_ACT_HIGH;
311 if (priv->client->irq > 0)
312 byte |= REG_ROWINT_SET_INT_EN;
313 return i2c_smbus_write_byte(priv->client, byte);
314}
315
Robin van der Gracht8992da42016-11-07 10:56:35 +0100316static int ht16k33_bl_update_status(struct backlight_device *bl)
317{
318 int brightness = bl->props.brightness;
319 struct ht16k33_priv *priv = bl_get_data(bl);
320
321 if (bl->props.power != FB_BLANK_UNBLANK ||
322 bl->props.fb_blank != FB_BLANK_UNBLANK ||
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200323 bl->props.state & BL_CORE_FBBLANK)
324 brightness = 0;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100325
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200326 return ht16k33_brightness_set(priv, brightness);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100327}
328
329static int ht16k33_bl_check_fb(struct backlight_device *bl, struct fb_info *fi)
330{
331 struct ht16k33_priv *priv = bl_get_data(bl);
332
333 return (fi == NULL) || (fi->par == priv);
334}
335
336static const struct backlight_ops ht16k33_bl_ops = {
337 .update_status = ht16k33_bl_update_status,
338 .check_fb = ht16k33_bl_check_fb,
339};
340
Geert Uytterhoeven840fe252021-10-19 16:45:09 +0200341/*
342 * Blank events will be passed to the actual device handling the backlight when
343 * we return zero here.
344 */
345static int ht16k33_blank(int blank, struct fb_info *info)
346{
347 return 0;
348}
349
Robin van der Gracht8992da42016-11-07 10:56:35 +0100350static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
351{
352 struct ht16k33_priv *priv = info->par;
Souptick Joarderf4bb1f82019-05-26 21:41:10 +0530353 struct page *pages = virt_to_page(priv->fbdev.buffer);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100354
Souptick Joarderf4bb1f82019-05-26 21:41:10 +0530355 return vm_map_pages_zero(vma, &pages, 1);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100356}
357
Jani Nikulabd223ac2019-12-03 18:38:54 +0200358static const struct fb_ops ht16k33_fb_ops = {
Robin van der Gracht8992da42016-11-07 10:56:35 +0100359 .owner = THIS_MODULE,
360 .fb_read = fb_sys_read,
361 .fb_write = fb_sys_write,
Geert Uytterhoeven840fe252021-10-19 16:45:09 +0200362 .fb_blank = ht16k33_blank,
Robin van der Gracht8992da42016-11-07 10:56:35 +0100363 .fb_fillrect = sys_fillrect,
364 .fb_copyarea = sys_copyarea,
365 .fb_imageblit = sys_imageblit,
366 .fb_mmap = ht16k33_mmap,
367};
368
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800369/*
370 * This gets the keys from keypad and reports it to input subsystem.
371 * Returns true if a key is pressed.
372 */
373static bool ht16k33_keypad_scan(struct ht16k33_keypad *keypad)
374{
375 const unsigned short *keycodes = keypad->dev->keycode;
376 u16 new_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
Dmitry Torokhov6ef0c332017-04-01 10:27:14 -0700377 __le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS];
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800378 unsigned long bits_changed;
379 int row, col, code;
Dmitry Torokhov6ef0c332017-04-01 10:27:14 -0700380 int rc;
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800381 bool pressed = false;
382
Dmitry Torokhov6ef0c332017-04-01 10:27:14 -0700383 rc = i2c_smbus_read_i2c_block_data(keypad->client, 0x40,
384 sizeof(data), (u8 *)data);
385 if (rc != sizeof(data)) {
386 dev_err(&keypad->client->dev,
387 "Failed to read key data, rc=%d\n", rc);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800388 return false;
389 }
390
391 for (col = 0; col < keypad->cols; col++) {
Dmitry Torokhov6ef0c332017-04-01 10:27:14 -0700392 new_state[col] = le16_to_cpu(data[col]);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800393 if (new_state[col])
394 pressed = true;
395 bits_changed = keypad->last_key_state[col] ^ new_state[col];
396
397 for_each_set_bit(row, &bits_changed, BITS_PER_LONG) {
398 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
399 input_event(keypad->dev, EV_MSC, MSC_SCAN, code);
400 input_report_key(keypad->dev, keycodes[code],
401 new_state[col] & BIT(row));
402 }
403 }
404 input_sync(keypad->dev);
Arnd Bergmanne1f990c2017-03-28 12:11:49 +0200405 memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800406
407 return pressed;
408}
409
410static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
411{
412 struct ht16k33_keypad *keypad = dev;
413
414 do {
415 wait_event_timeout(keypad->wait, keypad->stopped,
416 msecs_to_jiffies(keypad->debounce_ms));
417 if (keypad->stopped)
418 break;
419 } while (ht16k33_keypad_scan(keypad));
420
421 return IRQ_HANDLED;
422}
423
424static int ht16k33_keypad_start(struct input_dev *dev)
425{
426 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
427
428 keypad->stopped = false;
429 mb();
430 enable_irq(keypad->client->irq);
431
432 return 0;
433}
434
435static void ht16k33_keypad_stop(struct input_dev *dev)
436{
437 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
438
439 keypad->stopped = true;
440 mb();
441 wake_up(&keypad->wait);
442 disable_irq(keypad->client->irq);
443}
444
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200445static void ht16k33_linedisp_update(struct linedisp *linedisp)
446{
447 struct ht16k33_priv *priv = container_of(linedisp, struct ht16k33_priv,
448 seg.linedisp);
449
450 schedule_delayed_work(&priv->work, 0);
451}
452
453static void ht16k33_seg7_update(struct work_struct *work)
454{
455 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
456 work.work);
457 struct ht16k33_seg *seg = &priv->seg;
458 char *s = seg->curr;
459 uint8_t buf[9];
460
461 buf[0] = map_to_seg7(&seg->map.seg7, *s++);
462 buf[1] = 0;
463 buf[2] = map_to_seg7(&seg->map.seg7, *s++);
464 buf[3] = 0;
465 buf[4] = 0;
466 buf[5] = 0;
467 buf[6] = map_to_seg7(&seg->map.seg7, *s++);
468 buf[7] = 0;
469 buf[8] = map_to_seg7(&seg->map.seg7, *s++);
470
471 i2c_smbus_write_i2c_block_data(priv->client, 0, ARRAY_SIZE(buf), buf);
472}
473
474static void ht16k33_seg14_update(struct work_struct *work)
475{
476 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
477 work.work);
478 struct ht16k33_seg *seg = &priv->seg;
479 char *s = seg->curr;
480 uint8_t buf[8];
481
482 put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf);
483 put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 2);
484 put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 4);
485 put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 6);
486
487 i2c_smbus_write_i2c_block_data(priv->client, 0, ARRAY_SIZE(buf), buf);
488}
489
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200490static int ht16k33_led_probe(struct device *dev, struct led_classdev *led,
491 unsigned int brightness)
492{
493 struct led_init_data init_data = {};
494 struct device_node *node;
495 int err;
496
497 /* The LED is optional */
498 node = of_get_child_by_name(dev->of_node, "led");
499 if (!node)
500 return 0;
501
502 init_data.fwnode = of_fwnode_handle(node);
503 init_data.devicename = "auxdisplay";
504 init_data.devname_mandatory = true;
505
506 led->brightness_set_blocking = ht16k33_brightness_set_blocking;
507 led->blink_set = ht16k33_blink_set;
508 led->flags = LED_CORE_SUSPENDRESUME;
509 led->brightness = brightness;
510 led->max_brightness = MAX_BRIGHTNESS;
511
512 err = devm_led_classdev_register_ext(dev, led, &init_data);
513 if (err)
514 dev_err(dev, "Failed to register LED\n");
515
516 return err;
517}
518
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800519static int ht16k33_keypad_probe(struct i2c_client *client,
520 struct ht16k33_keypad *keypad)
521{
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200522 struct device *dev = &client->dev;
523 struct device_node *node = dev->of_node;
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800524 u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
525 u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
526 int err;
527
528 keypad->client = client;
529 init_waitqueue_head(&keypad->wait);
530
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200531 keypad->dev = devm_input_allocate_device(dev);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800532 if (!keypad->dev)
533 return -ENOMEM;
534
535 input_set_drvdata(keypad->dev, keypad);
536
537 keypad->dev->name = DRIVER_NAME"-keypad";
538 keypad->dev->id.bustype = BUS_I2C;
539 keypad->dev->open = ht16k33_keypad_start;
540 keypad->dev->close = ht16k33_keypad_stop;
541
542 if (!of_get_property(node, "linux,no-autorepeat", NULL))
543 __set_bit(EV_REP, keypad->dev->evbit);
544
545 err = of_property_read_u32(node, "debounce-delay-ms",
546 &keypad->debounce_ms);
547 if (err) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200548 dev_err(dev, "key debounce delay not specified\n");
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800549 return err;
550 }
551
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200552 err = matrix_keypad_parse_of_params(dev, &rows, &cols);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800553 if (err)
554 return err;
Arnd Bergmanne1f990c2017-03-28 12:11:49 +0200555 if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS ||
556 cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200557 dev_err(dev, "%u rows or %u cols out of range in DT\n", rows,
558 cols);
Arnd Bergmanne1f990c2017-03-28 12:11:49 +0200559 return -ERANGE;
560 }
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800561
562 keypad->rows = rows;
563 keypad->cols = cols;
564 keypad->row_shift = get_count_order(cols);
565
566 err = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL,
567 keypad->dev);
568 if (err) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200569 dev_err(dev, "failed to build keymap\n");
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800570 return err;
571 }
572
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200573 err = devm_request_threaded_irq(dev, client->irq, NULL,
574 ht16k33_keypad_irq_thread,
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800575 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
576 DRIVER_NAME, keypad);
577 if (err) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200578 dev_err(dev, "irq request failed %d, error %d\n", client->irq,
579 err);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800580 return err;
581 }
582
583 ht16k33_keypad_stop(keypad->dev);
584
Geert Uytterhoeven11b92912021-10-19 16:45:11 +0200585 return input_register_device(keypad->dev);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800586}
587
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200588static int ht16k33_fbdev_probe(struct device *dev, struct ht16k33_priv *priv,
589 uint32_t brightness)
Robin van der Gracht8992da42016-11-07 10:56:35 +0100590{
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200591 struct ht16k33_fbdev *fbdev = &priv->fbdev;
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200592 struct backlight_device *bl = NULL;
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200593 int err;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100594
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200595 if (priv->led.dev) {
596 err = ht16k33_brightness_set(priv, brightness);
597 if (err)
598 return err;
599 } else {
600 /* backwards compatibility with DT lacking an led subnode */
601 struct backlight_properties bl_props;
Geert Uytterhoeven80f9eb72021-10-19 16:45:08 +0200602
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200603 memset(&bl_props, 0, sizeof(struct backlight_properties));
604 bl_props.type = BACKLIGHT_RAW;
605 bl_props.max_brightness = MAX_BRIGHTNESS;
606
607 bl = devm_backlight_device_register(dev, DRIVER_NAME"-bl", dev,
608 priv, &ht16k33_bl_ops,
609 &bl_props);
610 if (IS_ERR(bl)) {
611 dev_err(dev, "failed to register backlight\n");
612 return PTR_ERR(bl);
613 }
614
615 bl->props.brightness = brightness;
616 ht16k33_bl_update_status(bl);
Geert Uytterhoeven80f9eb72021-10-19 16:45:08 +0200617 }
618
Robin van der Gracht8992da42016-11-07 10:56:35 +0100619 /* Framebuffer (2 bytes per column) */
620 BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
621 fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
Dmitry Torokhov8fa8bea2017-02-09 10:15:53 -0800622 if (!fbdev->buffer)
623 return -ENOMEM;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100624
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200625 fbdev->cache = devm_kmalloc(dev, HT16K33_FB_SIZE, GFP_KERNEL);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100626 if (!fbdev->cache) {
627 err = -ENOMEM;
628 goto err_fbdev_buffer;
629 }
630
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200631 fbdev->info = framebuffer_alloc(0, dev);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100632 if (!fbdev->info) {
633 err = -ENOMEM;
634 goto err_fbdev_buffer;
635 }
636
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200637 err = of_property_read_u32(dev->of_node, "refresh-rate-hz",
638 &fbdev->refresh_rate);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100639 if (err) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200640 dev_err(dev, "refresh rate not specified\n");
Robin van der Gracht8992da42016-11-07 10:56:35 +0100641 goto err_fbdev_info;
642 }
643 fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
644
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200645 INIT_DELAYED_WORK(&priv->work, ht16k33_fb_update);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100646 fbdev->info->fbops = &ht16k33_fb_ops;
647 fbdev->info->screen_base = (char __iomem *) fbdev->buffer;
648 fbdev->info->screen_size = HT16K33_FB_SIZE;
649 fbdev->info->fix = ht16k33_fb_fix;
650 fbdev->info->var = ht16k33_fb_var;
Geert Uytterhoeven80f9eb72021-10-19 16:45:08 +0200651 fbdev->info->bl_dev = bl;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100652 fbdev->info->pseudo_palette = NULL;
653 fbdev->info->flags = FBINFO_FLAG_DEFAULT;
654 fbdev->info->par = priv;
655
656 err = register_framebuffer(fbdev->info);
657 if (err)
658 goto err_fbdev_info;
659
Robin van der Gracht8992da42016-11-07 10:56:35 +0100660 ht16k33_fb_queue(priv);
661 return 0;
662
Robin van der Gracht8992da42016-11-07 10:56:35 +0100663err_fbdev_info:
664 framebuffer_release(fbdev->info);
665err_fbdev_buffer:
666 free_page((unsigned long) fbdev->buffer);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100667
668 return err;
669}
670
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200671static int ht16k33_seg_probe(struct device *dev, struct ht16k33_priv *priv,
672 uint32_t brightness)
673{
674 struct ht16k33_seg *seg = &priv->seg;
675 int err;
676
677 err = ht16k33_brightness_set(priv, brightness);
678 if (err)
679 return err;
680
681 switch (priv->type) {
682 case DISP_MATRIX:
683 /* not handled here */
684 err = -EINVAL;
685 break;
686
687 case DISP_QUAD_7SEG:
688 INIT_DELAYED_WORK(&priv->work, ht16k33_seg7_update);
689 seg->map.seg7 = initial_map_seg7;
690 seg->map_size = sizeof(seg->map.seg7);
691 err = device_create_file(dev, &dev_attr_map_seg7);
692 break;
693
694 case DISP_QUAD_14SEG:
695 INIT_DELAYED_WORK(&priv->work, ht16k33_seg14_update);
696 seg->map.seg14 = initial_map_seg14;
697 seg->map_size = sizeof(seg->map.seg14);
698 err = device_create_file(dev, &dev_attr_map_seg14);
699 break;
700 }
701 if (err)
702 return err;
703
704 err = linedisp_register(&seg->linedisp, dev, 4, seg->curr,
705 ht16k33_linedisp_update);
706 if (err)
707 goto err_remove_map_file;
708
709 return 0;
710
711err_remove_map_file:
712 device_remove_file(dev, &dev_attr_map_seg7);
713 device_remove_file(dev, &dev_attr_map_seg14);
714 return err;
715}
716
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200717static int ht16k33_probe(struct i2c_client *client)
718{
719 struct device *dev = &client->dev;
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200720 const struct of_device_id *id;
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200721 struct ht16k33_priv *priv;
722 uint32_t dft_brightness;
723 int err;
724
725 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
726 dev_err(dev, "i2c_check_functionality error\n");
727 return -EIO;
728 }
729
730 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
731 if (!priv)
732 return -ENOMEM;
733
734 priv->client = client;
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200735 id = i2c_of_match_device(dev->driver->of_match_table, client);
736 if (id)
737 priv->type = (uintptr_t)id->data;
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200738 i2c_set_clientdata(client, priv);
739
740 err = ht16k33_initialize(priv);
741 if (err)
742 return err;
743
744 err = of_property_read_u32(dev->of_node, "default-brightness-level",
745 &dft_brightness);
746 if (err) {
747 dft_brightness = MAX_BRIGHTNESS;
748 } else if (dft_brightness > MAX_BRIGHTNESS) {
749 dev_warn(dev,
750 "invalid default brightness level: %u, using %u\n",
751 dft_brightness, MAX_BRIGHTNESS);
752 dft_brightness = MAX_BRIGHTNESS;
753 }
754
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200755 /* LED */
756 err = ht16k33_led_probe(dev, &priv->led, dft_brightness);
757 if (err)
758 return err;
759
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200760 /* Keypad */
761 if (client->irq > 0) {
762 err = ht16k33_keypad_probe(client, &priv->keypad);
763 if (err)
764 return err;
765 }
766
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200767 switch (priv->type) {
768 case DISP_MATRIX:
769 /* Frame Buffer Display */
770 err = ht16k33_fbdev_probe(dev, priv, dft_brightness);
771 break;
772
773 case DISP_QUAD_7SEG:
774 case DISP_QUAD_14SEG:
775 /* Segment Display */
776 err = ht16k33_seg_probe(dev, priv, dft_brightness);
777 break;
778 }
779 return err;
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200780}
781
Robin van der Gracht8992da42016-11-07 10:56:35 +0100782static int ht16k33_remove(struct i2c_client *client)
783{
784 struct ht16k33_priv *priv = i2c_get_clientdata(client);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100785 struct ht16k33_fbdev *fbdev = &priv->fbdev;
786
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200787 cancel_delayed_work_sync(&priv->work);
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200788
789 switch (priv->type) {
790 case DISP_MATRIX:
791 unregister_framebuffer(fbdev->info);
792 framebuffer_release(fbdev->info);
793 free_page((unsigned long)fbdev->buffer);
794 break;
795
796 case DISP_QUAD_7SEG:
797 case DISP_QUAD_14SEG:
798 linedisp_unregister(&priv->seg.linedisp);
799 device_remove_file(&client->dev, &dev_attr_map_seg7);
800 device_remove_file(&client->dev, &dev_attr_map_seg14);
801 break;
802 }
Robin van der Gracht8992da42016-11-07 10:56:35 +0100803
Robin van der Gracht8992da42016-11-07 10:56:35 +0100804 return 0;
805}
806
807static const struct i2c_device_id ht16k33_i2c_match[] = {
808 { "ht16k33", 0 },
809 { }
810};
811MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
812
813static const struct of_device_id ht16k33_of_match[] = {
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200814 {
815 /* 0.56" 4-Digit 7-Segment FeatherWing Display (Red) */
816 .compatible = "adafruit,3108", .data = (void *)DISP_QUAD_7SEG,
817 }, {
818 /* 0.54" Quad Alphanumeric FeatherWing Display (Red) */
819 .compatible = "adafruit,3130", .data = (void *)DISP_QUAD_14SEG,
820 }, {
821 /* Generic, assumed Dot-Matrix Display */
822 .compatible = "holtek,ht16k33", .data = (void *)DISP_MATRIX,
823 },
Robin van der Gracht8992da42016-11-07 10:56:35 +0100824 { }
825};
826MODULE_DEVICE_TABLE(of, ht16k33_of_match);
827
828static struct i2c_driver ht16k33_driver = {
Geert Uytterhoevene66b4f42021-10-19 16:45:12 +0200829 .probe_new = ht16k33_probe,
Robin van der Gracht8992da42016-11-07 10:56:35 +0100830 .remove = ht16k33_remove,
831 .driver = {
832 .name = DRIVER_NAME,
833 .of_match_table = of_match_ptr(ht16k33_of_match),
834 },
835 .id_table = ht16k33_i2c_match,
836};
837module_i2c_driver(ht16k33_driver);
838
839MODULE_DESCRIPTION("Holtek HT16K33 driver");
840MODULE_LICENSE("GPL");
841MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");