blob: 4fab3b2c702390f602edf4ff51fd21681afd0e5a [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>
Geert Uytterhoeven5d343f72021-10-19 16:45:20 +020015#include <linux/property.h>
Robin van der Gracht8992da42016-11-07 10:56:35 +010016#include <linux/fb.h>
Robin van der Gracht8992da42016-11-07 10:56:35 +010017#include <linux/backlight.h>
18#include <linux/input.h>
19#include <linux/input/matrix_keypad.h>
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +020020#include <linux/leds.h>
Robin van der Gracht8992da42016-11-07 10:56:35 +010021#include <linux/workqueue.h>
22#include <linux/mm.h>
23
Geert Uytterhoevena0428722021-10-19 16:45:17 +020024#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
Robin van der Gracht8992da42016-11-07 10:56:35 +010031/* 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)
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +020037#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)
Robin van der Gracht8992da42016-11-07 10:56:35 +010041
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
Geert Uytterhoevena0428722021-10-19 16:45:17 +020062enum display_type {
63 DISP_MATRIX = 0,
64 DISP_QUAD_7SEG,
65 DISP_QUAD_14SEG,
66};
67
Robin van der Gracht8992da42016-11-07 10:56:35 +010068struct ht16k33_keypad {
Dmitry Torokhovcac513f2017-02-09 10:15:52 -080069 struct i2c_client *client;
Robin van der Gracht8992da42016-11-07 10:56:35 +010070 struct input_dev *dev;
Robin van der Gracht8992da42016-11-07 10:56:35 +010071 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];
Dmitry Torokhovcac513f2017-02-09 10:15:52 -080076
77 wait_queue_head_t wait;
78 bool stopped;
Robin van der Gracht8992da42016-11-07 10:56:35 +010079};
80
81struct ht16k33_fbdev {
82 struct fb_info *info;
83 uint32_t refresh_rate;
84 uint8_t *buffer;
85 uint8_t *cache;
Robin van der Gracht8992da42016-11-07 10:56:35 +010086};
87
Geert Uytterhoevena0428722021-10-19 16:45:17 +020088struct 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
Robin van der Gracht8992da42016-11-07 10:56:35 +010098struct ht16k33_priv {
99 struct i2c_client *client;
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200100 struct delayed_work work;
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200101 struct led_classdev led;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100102 struct ht16k33_keypad keypad;
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200103 union {
104 struct ht16k33_fbdev fbdev;
105 struct ht16k33_seg seg;
106 };
107 enum display_type type;
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200108 uint8_t blink;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100109};
110
Nishka Dasguptaa180d022019-08-19 13:21:26 +0530111static const struct fb_fix_screeninfo ht16k33_fb_fix = {
Robin van der Gracht8992da42016-11-07 10:56:35 +0100112 .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
Nishka Dasguptaa180d022019-08-19 13:21:26 +0530122static const struct fb_var_screeninfo ht16k33_fb_var = {
Robin van der Gracht8992da42016-11-07 10:56:35 +0100123 .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
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200138static const SEG7_DEFAULT_MAP(initial_map_seg7);
139static const SEG14_DEFAULT_MAP(initial_map_seg14);
140
141static 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
150static 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
162static DEVICE_ATTR(map_seg7, 0644, map_seg_show, map_seg_store);
163static DEVICE_ATTR(map_seg14, 0644, map_seg_show, map_seg_store);
164
Robin van der Gracht8992da42016-11-07 10:56:35 +0100165static int ht16k33_display_on(struct ht16k33_priv *priv)
166{
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200167 uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON | priv->blink;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100168
169 return i2c_smbus_write_byte(priv->client, data);
170}
171
172static int ht16k33_display_off(struct ht16k33_priv *priv)
173{
174 return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP);
175}
176
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200177static int ht16k33_brightness_set(struct ht16k33_priv *priv,
178 unsigned int brightness)
179{
180 int err;
181
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200182 if (brightness == 0) {
183 priv->blink = REG_DISPLAY_SETUP_BLINK_OFF;
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200184 return ht16k33_display_off(priv);
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200185 }
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200186
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
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200195static 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
204static 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
Robin van der Gracht8992da42016-11-07 10:56:35 +0100238static void ht16k33_fb_queue(struct ht16k33_priv *priv)
239{
240 struct ht16k33_fbdev *fbdev = &priv->fbdev;
241
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200242 schedule_delayed_work(&priv->work, HZ / fbdev->refresh_rate);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100243}
244
Robin van der Gracht8992da42016-11-07 10:56:35 +0100245/*
246 * This gets the fb data from cache and copies it to ht16k33 display RAM
247 */
248static void ht16k33_fb_update(struct work_struct *work)
249{
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200250 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
251 work.work);
252 struct ht16k33_fbdev *fbdev = &priv->fbdev;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100253
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);
286requeue:
287 ht16k33_fb_queue(priv);
288}
289
Robin van der Gracht8992da42016-11-07 10:56:35 +0100290static int ht16k33_initialize(struct ht16k33_priv *priv)
291{
Geert Uytterhoevenfb61e132021-10-19 16:45:10 +0200292 uint8_t data[HT16K33_FB_SIZE];
Robin van der Gracht8992da42016-11-07 10:56:35 +0100293 uint8_t byte;
294 int err;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100295
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
Robin van der Gracht8992da42016-11-07 10:56:35 +0100315static 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 ||
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200322 bl->props.state & BL_CORE_FBBLANK)
323 brightness = 0;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100324
Geert Uytterhoevenb37cc222021-10-19 16:45:15 +0200325 return ht16k33_brightness_set(priv, brightness);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100326}
327
328static 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
335static const struct backlight_ops ht16k33_bl_ops = {
336 .update_status = ht16k33_bl_update_status,
337 .check_fb = ht16k33_bl_check_fb,
338};
339
Geert Uytterhoeven840fe252021-10-19 16:45:09 +0200340/*
341 * Blank events will be passed to the actual device handling the backlight when
342 * we return zero here.
343 */
344static int ht16k33_blank(int blank, struct fb_info *info)
345{
346 return 0;
347}
348
Robin van der Gracht8992da42016-11-07 10:56:35 +0100349static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
350{
351 struct ht16k33_priv *priv = info->par;
Souptick Joarderf4bb1f82019-05-26 21:41:10 +0530352 struct page *pages = virt_to_page(priv->fbdev.buffer);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100353
Souptick Joarderf4bb1f82019-05-26 21:41:10 +0530354 return vm_map_pages_zero(vma, &pages, 1);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100355}
356
Jani Nikulabd223ac2019-12-03 18:38:54 +0200357static const struct fb_ops ht16k33_fb_ops = {
Robin van der Gracht8992da42016-11-07 10:56:35 +0100358 .owner = THIS_MODULE,
359 .fb_read = fb_sys_read,
360 .fb_write = fb_sys_write,
Geert Uytterhoeven840fe252021-10-19 16:45:09 +0200361 .fb_blank = ht16k33_blank,
Robin van der Gracht8992da42016-11-07 10:56:35 +0100362 .fb_fillrect = sys_fillrect,
363 .fb_copyarea = sys_copyarea,
364 .fb_imageblit = sys_imageblit,
365 .fb_mmap = ht16k33_mmap,
366};
367
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800368/*
369 * This gets the keys from keypad and reports it to input subsystem.
370 * Returns true if a key is pressed.
371 */
372static bool ht16k33_keypad_scan(struct ht16k33_keypad *keypad)
373{
374 const unsigned short *keycodes = keypad->dev->keycode;
375 u16 new_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
Dmitry Torokhov6ef0c332017-04-01 10:27:14 -0700376 __le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS];
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800377 unsigned long bits_changed;
378 int row, col, code;
Dmitry Torokhov6ef0c332017-04-01 10:27:14 -0700379 int rc;
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800380 bool pressed = false;
381
Dmitry Torokhov6ef0c332017-04-01 10:27:14 -0700382 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);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800387 return false;
388 }
389
390 for (col = 0; col < keypad->cols; col++) {
Dmitry Torokhov6ef0c332017-04-01 10:27:14 -0700391 new_state[col] = le16_to_cpu(data[col]);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800392 if (new_state[col])
393 pressed = true;
394 bits_changed = keypad->last_key_state[col] ^ new_state[col];
395
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));
401 }
402 }
403 input_sync(keypad->dev);
Arnd Bergmanne1f990c2017-03-28 12:11:49 +0200404 memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800405
406 return pressed;
407}
408
409static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
410{
411 struct ht16k33_keypad *keypad = dev;
412
413 do {
414 wait_event_timeout(keypad->wait, keypad->stopped,
415 msecs_to_jiffies(keypad->debounce_ms));
416 if (keypad->stopped)
417 break;
418 } while (ht16k33_keypad_scan(keypad));
419
420 return IRQ_HANDLED;
421}
422
423static int ht16k33_keypad_start(struct input_dev *dev)
424{
425 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
426
427 keypad->stopped = false;
428 mb();
429 enable_irq(keypad->client->irq);
430
431 return 0;
432}
433
434static void ht16k33_keypad_stop(struct input_dev *dev)
435{
436 struct ht16k33_keypad *keypad = input_get_drvdata(dev);
437
438 keypad->stopped = true;
439 mb();
440 wake_up(&keypad->wait);
441 disable_irq(keypad->client->irq);
442}
443
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200444static void ht16k33_linedisp_update(struct linedisp *linedisp)
445{
446 struct ht16k33_priv *priv = container_of(linedisp, struct ht16k33_priv,
447 seg.linedisp);
448
449 schedule_delayed_work(&priv->work, 0);
450}
451
452static void ht16k33_seg7_update(struct work_struct *work)
453{
454 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
455 work.work);
456 struct ht16k33_seg *seg = &priv->seg;
457 char *s = seg->curr;
458 uint8_t buf[9];
459
460 buf[0] = map_to_seg7(&seg->map.seg7, *s++);
461 buf[1] = 0;
462 buf[2] = map_to_seg7(&seg->map.seg7, *s++);
463 buf[3] = 0;
464 buf[4] = 0;
465 buf[5] = 0;
466 buf[6] = map_to_seg7(&seg->map.seg7, *s++);
467 buf[7] = 0;
468 buf[8] = map_to_seg7(&seg->map.seg7, *s++);
469
470 i2c_smbus_write_i2c_block_data(priv->client, 0, ARRAY_SIZE(buf), buf);
471}
472
473static void ht16k33_seg14_update(struct work_struct *work)
474{
475 struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv,
476 work.work);
477 struct ht16k33_seg *seg = &priv->seg;
478 char *s = seg->curr;
479 uint8_t buf[8];
480
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);
485
486 i2c_smbus_write_i2c_block_data(priv->client, 0, ARRAY_SIZE(buf), buf);
487}
488
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200489static int ht16k33_led_probe(struct device *dev, struct led_classdev *led,
490 unsigned int brightness)
491{
492 struct led_init_data init_data = {};
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200493 int err;
494
495 /* The LED is optional */
Geert Uytterhoeven5d343f72021-10-19 16:45:20 +0200496 init_data.fwnode = device_get_named_child_node(dev, "led");
497 if (!init_data.fwnode)
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200498 return 0;
499
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200500 init_data.devicename = "auxdisplay";
501 init_data.devname_mandatory = true;
502
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;
508
509 err = devm_led_classdev_register_ext(dev, led, &init_data);
510 if (err)
511 dev_err(dev, "Failed to register LED\n");
512
513 return err;
514}
515
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800516static int ht16k33_keypad_probe(struct i2c_client *client,
517 struct ht16k33_keypad *keypad)
518{
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200519 struct device *dev = &client->dev;
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800520 u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
521 u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
522 int err;
523
524 keypad->client = client;
525 init_waitqueue_head(&keypad->wait);
526
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200527 keypad->dev = devm_input_allocate_device(dev);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800528 if (!keypad->dev)
529 return -ENOMEM;
530
531 input_set_drvdata(keypad->dev, keypad);
532
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;
537
Geert Uytterhoeven5d343f72021-10-19 16:45:20 +0200538 if (!device_property_read_bool(dev, "linux,no-autorepeat"))
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800539 __set_bit(EV_REP, keypad->dev->evbit);
540
Geert Uytterhoeven5d343f72021-10-19 16:45:20 +0200541 err = device_property_read_u32(dev, "debounce-delay-ms",
542 &keypad->debounce_ms);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800543 if (err) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200544 dev_err(dev, "key debounce delay not specified\n");
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800545 return err;
546 }
547
Geert Uytterhoeven5d343f72021-10-19 16:45:20 +0200548 err = matrix_keypad_parse_properties(dev, &rows, &cols);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800549 if (err)
550 return err;
Arnd Bergmanne1f990c2017-03-28 12:11:49 +0200551 if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS ||
552 cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200553 dev_err(dev, "%u rows or %u cols out of range in DT\n", rows,
554 cols);
Arnd Bergmanne1f990c2017-03-28 12:11:49 +0200555 return -ERANGE;
556 }
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800557
558 keypad->rows = rows;
559 keypad->cols = cols;
560 keypad->row_shift = get_count_order(cols);
561
562 err = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL,
563 keypad->dev);
564 if (err) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200565 dev_err(dev, "failed to build keymap\n");
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800566 return err;
567 }
568
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200569 err = devm_request_threaded_irq(dev, client->irq, NULL,
570 ht16k33_keypad_irq_thread,
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800571 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
572 DRIVER_NAME, keypad);
573 if (err) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200574 dev_err(dev, "irq request failed %d, error %d\n", client->irq,
575 err);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800576 return err;
577 }
578
579 ht16k33_keypad_stop(keypad->dev);
580
Geert Uytterhoeven11b92912021-10-19 16:45:11 +0200581 return input_register_device(keypad->dev);
Dmitry Torokhovcac513f2017-02-09 10:15:52 -0800582}
583
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200584static int ht16k33_fbdev_probe(struct device *dev, struct ht16k33_priv *priv,
585 uint32_t brightness)
Robin van der Gracht8992da42016-11-07 10:56:35 +0100586{
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200587 struct ht16k33_fbdev *fbdev = &priv->fbdev;
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200588 struct backlight_device *bl = NULL;
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200589 int err;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100590
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200591 if (priv->led.dev) {
592 err = ht16k33_brightness_set(priv, brightness);
593 if (err)
594 return err;
595 } else {
596 /* backwards compatibility with DT lacking an led subnode */
597 struct backlight_properties bl_props;
Geert Uytterhoeven80f9eb72021-10-19 16:45:08 +0200598
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200599 memset(&bl_props, 0, sizeof(struct backlight_properties));
600 bl_props.type = BACKLIGHT_RAW;
601 bl_props.max_brightness = MAX_BRIGHTNESS;
602
603 bl = devm_backlight_device_register(dev, DRIVER_NAME"-bl", dev,
604 priv, &ht16k33_bl_ops,
605 &bl_props);
606 if (IS_ERR(bl)) {
607 dev_err(dev, "failed to register backlight\n");
608 return PTR_ERR(bl);
609 }
610
611 bl->props.brightness = brightness;
612 ht16k33_bl_update_status(bl);
Geert Uytterhoeven80f9eb72021-10-19 16:45:08 +0200613 }
614
Robin van der Gracht8992da42016-11-07 10:56:35 +0100615 /* Framebuffer (2 bytes per column) */
616 BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
617 fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
Dmitry Torokhov8fa8bea2017-02-09 10:15:53 -0800618 if (!fbdev->buffer)
619 return -ENOMEM;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100620
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200621 fbdev->cache = devm_kmalloc(dev, HT16K33_FB_SIZE, GFP_KERNEL);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100622 if (!fbdev->cache) {
623 err = -ENOMEM;
624 goto err_fbdev_buffer;
625 }
626
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200627 fbdev->info = framebuffer_alloc(0, dev);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100628 if (!fbdev->info) {
629 err = -ENOMEM;
630 goto err_fbdev_buffer;
631 }
632
Geert Uytterhoeven5d343f72021-10-19 16:45:20 +0200633 err = device_property_read_u32(dev, "refresh-rate-hz",
634 &fbdev->refresh_rate);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100635 if (err) {
Geert Uytterhoevend08a44d2021-10-19 16:45:13 +0200636 dev_err(dev, "refresh rate not specified\n");
Robin van der Gracht8992da42016-11-07 10:56:35 +0100637 goto err_fbdev_info;
638 }
639 fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
640
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200641 INIT_DELAYED_WORK(&priv->work, ht16k33_fb_update);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100642 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;
Geert Uytterhoeven80f9eb72021-10-19 16:45:08 +0200647 fbdev->info->bl_dev = bl;
Robin van der Gracht8992da42016-11-07 10:56:35 +0100648 fbdev->info->pseudo_palette = NULL;
649 fbdev->info->flags = FBINFO_FLAG_DEFAULT;
650 fbdev->info->par = priv;
651
652 err = register_framebuffer(fbdev->info);
653 if (err)
654 goto err_fbdev_info;
655
Robin van der Gracht8992da42016-11-07 10:56:35 +0100656 ht16k33_fb_queue(priv);
657 return 0;
658
Robin van der Gracht8992da42016-11-07 10:56:35 +0100659err_fbdev_info:
660 framebuffer_release(fbdev->info);
661err_fbdev_buffer:
662 free_page((unsigned long) fbdev->buffer);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100663
664 return err;
665}
666
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200667static int ht16k33_seg_probe(struct device *dev, struct ht16k33_priv *priv,
668 uint32_t brightness)
669{
670 struct ht16k33_seg *seg = &priv->seg;
671 int err;
672
673 err = ht16k33_brightness_set(priv, brightness);
674 if (err)
675 return err;
676
677 switch (priv->type) {
678 case DISP_MATRIX:
679 /* not handled here */
680 err = -EINVAL;
681 break;
682
683 case DISP_QUAD_7SEG:
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);
688 break;
689
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);
695 break;
696 }
697 if (err)
698 return err;
699
700 err = linedisp_register(&seg->linedisp, dev, 4, seg->curr,
701 ht16k33_linedisp_update);
702 if (err)
703 goto err_remove_map_file;
704
705 return 0;
706
707err_remove_map_file:
708 device_remove_file(dev, &dev_attr_map_seg7);
709 device_remove_file(dev, &dev_attr_map_seg14);
710 return err;
711}
712
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200713static int ht16k33_probe(struct i2c_client *client)
714{
715 struct device *dev = &client->dev;
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200716 const struct of_device_id *id;
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200717 struct ht16k33_priv *priv;
718 uint32_t dft_brightness;
719 int err;
720
721 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
722 dev_err(dev, "i2c_check_functionality error\n");
723 return -EIO;
724 }
725
726 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
727 if (!priv)
728 return -ENOMEM;
729
730 priv->client = client;
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200731 id = i2c_of_match_device(dev->driver->of_match_table, client);
732 if (id)
733 priv->type = (uintptr_t)id->data;
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200734 i2c_set_clientdata(client, priv);
735
736 err = ht16k33_initialize(priv);
737 if (err)
738 return err;
739
Geert Uytterhoeven5d343f72021-10-19 16:45:20 +0200740 err = device_property_read_u32(dev, "default-brightness-level",
741 &dft_brightness);
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200742 if (err) {
743 dft_brightness = MAX_BRIGHTNESS;
744 } else if (dft_brightness > MAX_BRIGHTNESS) {
745 dev_warn(dev,
746 "invalid default brightness level: %u, using %u\n",
747 dft_brightness, MAX_BRIGHTNESS);
748 dft_brightness = MAX_BRIGHTNESS;
749 }
750
Geert Uytterhoevenc223d9c2021-10-19 16:45:19 +0200751 /* LED */
752 err = ht16k33_led_probe(dev, &priv->led, dft_brightness);
753 if (err)
754 return err;
755
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200756 /* Keypad */
757 if (client->irq > 0) {
758 err = ht16k33_keypad_probe(client, &priv->keypad);
759 if (err)
760 return err;
761 }
762
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200763 switch (priv->type) {
764 case DISP_MATRIX:
765 /* Frame Buffer Display */
766 err = ht16k33_fbdev_probe(dev, priv, dft_brightness);
767 break;
768
769 case DISP_QUAD_7SEG:
770 case DISP_QUAD_14SEG:
771 /* Segment Display */
772 err = ht16k33_seg_probe(dev, priv, dft_brightness);
773 break;
774 }
775 return err;
Geert Uytterhoevenfcbb3c32021-10-19 16:45:16 +0200776}
777
Robin van der Gracht8992da42016-11-07 10:56:35 +0100778static int ht16k33_remove(struct i2c_client *client)
779{
780 struct ht16k33_priv *priv = i2c_get_clientdata(client);
Robin van der Gracht8992da42016-11-07 10:56:35 +0100781 struct ht16k33_fbdev *fbdev = &priv->fbdev;
782
Geert Uytterhoeven85d93b12021-10-19 16:45:14 +0200783 cancel_delayed_work_sync(&priv->work);
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200784
785 switch (priv->type) {
786 case DISP_MATRIX:
787 unregister_framebuffer(fbdev->info);
788 framebuffer_release(fbdev->info);
789 free_page((unsigned long)fbdev->buffer);
790 break;
791
792 case DISP_QUAD_7SEG:
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);
797 break;
798 }
Robin van der Gracht8992da42016-11-07 10:56:35 +0100799
Robin van der Gracht8992da42016-11-07 10:56:35 +0100800 return 0;
801}
802
803static const struct i2c_device_id ht16k33_i2c_match[] = {
804 { "ht16k33", 0 },
805 { }
806};
807MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
808
809static const struct of_device_id ht16k33_of_match[] = {
Geert Uytterhoevena0428722021-10-19 16:45:17 +0200810 {
811 /* 0.56" 4-Digit 7-Segment FeatherWing Display (Red) */
812 .compatible = "adafruit,3108", .data = (void *)DISP_QUAD_7SEG,
813 }, {
814 /* 0.54" Quad Alphanumeric FeatherWing Display (Red) */
815 .compatible = "adafruit,3130", .data = (void *)DISP_QUAD_14SEG,
816 }, {
817 /* Generic, assumed Dot-Matrix Display */
818 .compatible = "holtek,ht16k33", .data = (void *)DISP_MATRIX,
819 },
Robin van der Gracht8992da42016-11-07 10:56:35 +0100820 { }
821};
822MODULE_DEVICE_TABLE(of, ht16k33_of_match);
823
824static struct i2c_driver ht16k33_driver = {
Geert Uytterhoevene66b4f42021-10-19 16:45:12 +0200825 .probe_new = ht16k33_probe,
Robin van der Gracht8992da42016-11-07 10:56:35 +0100826 .remove = ht16k33_remove,
827 .driver = {
828 .name = DRIVER_NAME,
Geert Uytterhoeven5d343f72021-10-19 16:45:20 +0200829 .of_match_table = ht16k33_of_match,
Robin van der Gracht8992da42016-11-07 10:56:35 +0100830 },
831 .id_table = ht16k33_i2c_match,
832};
833module_i2c_driver(ht16k33_driver);
834
835MODULE_DESCRIPTION("Holtek HT16K33 driver");
836MODULE_LICENSE("GPL");
837MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");