Miguel Ojeda | 351f683b | 2018-02-17 20:33:13 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 2 | /* |
| 3 | * HT16K33 driver |
| 4 | * |
| 5 | * Author: Robin van der Gracht <robin@protonic.nl> |
| 6 | * |
| 7 | * Copyright: (C) 2016 Protonic Holland. |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 8 | * Copyright (C) 2021 Glider bv |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 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/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 Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 21 | #include <linux/leds.h> |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/mm.h> |
| 24 | |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 25 | #include <linux/map_to_7segment.h> |
| 26 | #include <linux/map_to_14segment.h> |
| 27 | |
| 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include "line-display.h" |
| 31 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 32 | /* Registers */ |
| 33 | #define REG_SYSTEM_SETUP 0x20 |
| 34 | #define REG_SYSTEM_SETUP_OSC_ON BIT(0) |
| 35 | |
| 36 | #define REG_DISPLAY_SETUP 0x80 |
| 37 | #define REG_DISPLAY_SETUP_ON BIT(0) |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 38 | #define REG_DISPLAY_SETUP_BLINK_OFF (0 << 1) |
| 39 | #define REG_DISPLAY_SETUP_BLINK_2HZ (1 << 1) |
| 40 | #define REG_DISPLAY_SETUP_BLINK_1HZ (2 << 1) |
| 41 | #define REG_DISPLAY_SETUP_BLINK_0HZ5 (3 << 1) |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 42 | |
| 43 | #define REG_ROWINT_SET 0xA0 |
| 44 | #define REG_ROWINT_SET_INT_EN BIT(0) |
| 45 | #define REG_ROWINT_SET_INT_ACT_HIGH BIT(1) |
| 46 | |
| 47 | #define REG_BRIGHTNESS 0xE0 |
| 48 | |
| 49 | /* Defines */ |
| 50 | #define DRIVER_NAME "ht16k33" |
| 51 | |
| 52 | #define MIN_BRIGHTNESS 0x1 |
| 53 | #define MAX_BRIGHTNESS 0x10 |
| 54 | |
| 55 | #define HT16K33_MATRIX_LED_MAX_COLS 8 |
| 56 | #define HT16K33_MATRIX_LED_MAX_ROWS 16 |
| 57 | #define HT16K33_MATRIX_KEYPAD_MAX_COLS 3 |
| 58 | #define HT16K33_MATRIX_KEYPAD_MAX_ROWS 12 |
| 59 | |
| 60 | #define BYTES_PER_ROW (HT16K33_MATRIX_LED_MAX_ROWS / 8) |
| 61 | #define HT16K33_FB_SIZE (HT16K33_MATRIX_LED_MAX_COLS * BYTES_PER_ROW) |
| 62 | |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 63 | enum display_type { |
| 64 | DISP_MATRIX = 0, |
| 65 | DISP_QUAD_7SEG, |
| 66 | DISP_QUAD_14SEG, |
| 67 | }; |
| 68 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 69 | struct ht16k33_keypad { |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 70 | struct i2c_client *client; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 71 | struct input_dev *dev; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 72 | uint32_t cols; |
| 73 | uint32_t rows; |
| 74 | uint32_t row_shift; |
| 75 | uint32_t debounce_ms; |
| 76 | uint16_t last_key_state[HT16K33_MATRIX_KEYPAD_MAX_COLS]; |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 77 | |
| 78 | wait_queue_head_t wait; |
| 79 | bool stopped; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct ht16k33_fbdev { |
| 83 | struct fb_info *info; |
| 84 | uint32_t refresh_rate; |
| 85 | uint8_t *buffer; |
| 86 | uint8_t *cache; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 87 | }; |
| 88 | |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 89 | struct 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 Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 99 | struct ht16k33_priv { |
| 100 | struct i2c_client *client; |
Geert Uytterhoeven | 85d93b1 | 2021-10-19 16:45:14 +0200 | [diff] [blame] | 101 | struct delayed_work work; |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 102 | struct led_classdev led; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 103 | struct ht16k33_keypad keypad; |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 104 | union { |
| 105 | struct ht16k33_fbdev fbdev; |
| 106 | struct ht16k33_seg seg; |
| 107 | }; |
| 108 | enum display_type type; |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 109 | uint8_t blink; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 110 | }; |
| 111 | |
Nishka Dasgupta | a180d02 | 2019-08-19 13:21:26 +0530 | [diff] [blame] | 112 | static const struct fb_fix_screeninfo ht16k33_fb_fix = { |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 113 | .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 Dasgupta | a180d02 | 2019-08-19 13:21:26 +0530 | [diff] [blame] | 123 | static const struct fb_var_screeninfo ht16k33_fb_var = { |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 124 | .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 Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 139 | static const SEG7_DEFAULT_MAP(initial_map_seg7); |
| 140 | static const SEG14_DEFAULT_MAP(initial_map_seg14); |
| 141 | |
| 142 | static 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 | |
| 151 | static 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 | |
| 163 | static DEVICE_ATTR(map_seg7, 0644, map_seg_show, map_seg_store); |
| 164 | static DEVICE_ATTR(map_seg14, 0644, map_seg_show, map_seg_store); |
| 165 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 166 | static int ht16k33_display_on(struct ht16k33_priv *priv) |
| 167 | { |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 168 | uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON | priv->blink; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 169 | |
| 170 | return i2c_smbus_write_byte(priv->client, data); |
| 171 | } |
| 172 | |
| 173 | static int ht16k33_display_off(struct ht16k33_priv *priv) |
| 174 | { |
| 175 | return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP); |
| 176 | } |
| 177 | |
Geert Uytterhoeven | b37cc22 | 2021-10-19 16:45:15 +0200 | [diff] [blame] | 178 | static int ht16k33_brightness_set(struct ht16k33_priv *priv, |
| 179 | unsigned int brightness) |
| 180 | { |
| 181 | int err; |
| 182 | |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 183 | if (brightness == 0) { |
| 184 | priv->blink = REG_DISPLAY_SETUP_BLINK_OFF; |
Geert Uytterhoeven | b37cc22 | 2021-10-19 16:45:15 +0200 | [diff] [blame] | 185 | return ht16k33_display_off(priv); |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 186 | } |
Geert Uytterhoeven | b37cc22 | 2021-10-19 16:45:15 +0200 | [diff] [blame] | 187 | |
| 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 Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 196 | static 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 | |
| 205 | static 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 Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 239 | static void ht16k33_fb_queue(struct ht16k33_priv *priv) |
| 240 | { |
| 241 | struct ht16k33_fbdev *fbdev = &priv->fbdev; |
| 242 | |
Geert Uytterhoeven | 85d93b1 | 2021-10-19 16:45:14 +0200 | [diff] [blame] | 243 | schedule_delayed_work(&priv->work, HZ / fbdev->refresh_rate); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 246 | /* |
| 247 | * This gets the fb data from cache and copies it to ht16k33 display RAM |
| 248 | */ |
| 249 | static void ht16k33_fb_update(struct work_struct *work) |
| 250 | { |
Geert Uytterhoeven | 85d93b1 | 2021-10-19 16:45:14 +0200 | [diff] [blame] | 251 | struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv, |
| 252 | work.work); |
| 253 | struct ht16k33_fbdev *fbdev = &priv->fbdev; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 254 | |
| 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); |
| 287 | requeue: |
| 288 | ht16k33_fb_queue(priv); |
| 289 | } |
| 290 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 291 | static int ht16k33_initialize(struct ht16k33_priv *priv) |
| 292 | { |
Geert Uytterhoeven | fb61e13 | 2021-10-19 16:45:10 +0200 | [diff] [blame] | 293 | uint8_t data[HT16K33_FB_SIZE]; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 294 | uint8_t byte; |
| 295 | int err; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 296 | |
| 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 Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 316 | static 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 Uytterhoeven | b37cc22 | 2021-10-19 16:45:15 +0200 | [diff] [blame] | 323 | bl->props.state & BL_CORE_FBBLANK) |
| 324 | brightness = 0; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 325 | |
Geert Uytterhoeven | b37cc22 | 2021-10-19 16:45:15 +0200 | [diff] [blame] | 326 | return ht16k33_brightness_set(priv, brightness); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static 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 | |
| 336 | static const struct backlight_ops ht16k33_bl_ops = { |
| 337 | .update_status = ht16k33_bl_update_status, |
| 338 | .check_fb = ht16k33_bl_check_fb, |
| 339 | }; |
| 340 | |
Geert Uytterhoeven | 840fe25 | 2021-10-19 16:45:09 +0200 | [diff] [blame] | 341 | /* |
| 342 | * Blank events will be passed to the actual device handling the backlight when |
| 343 | * we return zero here. |
| 344 | */ |
| 345 | static int ht16k33_blank(int blank, struct fb_info *info) |
| 346 | { |
| 347 | return 0; |
| 348 | } |
| 349 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 350 | static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma) |
| 351 | { |
| 352 | struct ht16k33_priv *priv = info->par; |
Souptick Joarder | f4bb1f8 | 2019-05-26 21:41:10 +0530 | [diff] [blame] | 353 | struct page *pages = virt_to_page(priv->fbdev.buffer); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 354 | |
Souptick Joarder | f4bb1f8 | 2019-05-26 21:41:10 +0530 | [diff] [blame] | 355 | return vm_map_pages_zero(vma, &pages, 1); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Jani Nikula | bd223ac | 2019-12-03 18:38:54 +0200 | [diff] [blame] | 358 | static const struct fb_ops ht16k33_fb_ops = { |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 359 | .owner = THIS_MODULE, |
| 360 | .fb_read = fb_sys_read, |
| 361 | .fb_write = fb_sys_write, |
Geert Uytterhoeven | 840fe25 | 2021-10-19 16:45:09 +0200 | [diff] [blame] | 362 | .fb_blank = ht16k33_blank, |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 363 | .fb_fillrect = sys_fillrect, |
| 364 | .fb_copyarea = sys_copyarea, |
| 365 | .fb_imageblit = sys_imageblit, |
| 366 | .fb_mmap = ht16k33_mmap, |
| 367 | }; |
| 368 | |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 369 | /* |
| 370 | * This gets the keys from keypad and reports it to input subsystem. |
| 371 | * Returns true if a key is pressed. |
| 372 | */ |
| 373 | static 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 Torokhov | 6ef0c33 | 2017-04-01 10:27:14 -0700 | [diff] [blame] | 377 | __le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS]; |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 378 | unsigned long bits_changed; |
| 379 | int row, col, code; |
Dmitry Torokhov | 6ef0c33 | 2017-04-01 10:27:14 -0700 | [diff] [blame] | 380 | int rc; |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 381 | bool pressed = false; |
| 382 | |
Dmitry Torokhov | 6ef0c33 | 2017-04-01 10:27:14 -0700 | [diff] [blame] | 383 | 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 Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | |
| 391 | for (col = 0; col < keypad->cols; col++) { |
Dmitry Torokhov | 6ef0c33 | 2017-04-01 10:27:14 -0700 | [diff] [blame] | 392 | new_state[col] = le16_to_cpu(data[col]); |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 393 | 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 Bergmann | e1f990c | 2017-03-28 12:11:49 +0200 | [diff] [blame] | 405 | memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols); |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 406 | |
| 407 | return pressed; |
| 408 | } |
| 409 | |
| 410 | static 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 | |
| 424 | static 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 | |
| 435 | static 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 Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 445 | static 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 | |
| 453 | static 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 | |
| 474 | static 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 Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 490 | static 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 Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 519 | static int ht16k33_keypad_probe(struct i2c_client *client, |
| 520 | struct ht16k33_keypad *keypad) |
| 521 | { |
Geert Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 522 | struct device *dev = &client->dev; |
| 523 | struct device_node *node = dev->of_node; |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 524 | 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 Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 531 | keypad->dev = devm_input_allocate_device(dev); |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 532 | 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 Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 548 | dev_err(dev, "key debounce delay not specified\n"); |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 549 | return err; |
| 550 | } |
| 551 | |
Geert Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 552 | err = matrix_keypad_parse_of_params(dev, &rows, &cols); |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 553 | if (err) |
| 554 | return err; |
Arnd Bergmann | e1f990c | 2017-03-28 12:11:49 +0200 | [diff] [blame] | 555 | if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS || |
| 556 | cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) { |
Geert Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 557 | dev_err(dev, "%u rows or %u cols out of range in DT\n", rows, |
| 558 | cols); |
Arnd Bergmann | e1f990c | 2017-03-28 12:11:49 +0200 | [diff] [blame] | 559 | return -ERANGE; |
| 560 | } |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 561 | |
| 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 Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 569 | dev_err(dev, "failed to build keymap\n"); |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 570 | return err; |
| 571 | } |
| 572 | |
Geert Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 573 | err = devm_request_threaded_irq(dev, client->irq, NULL, |
| 574 | ht16k33_keypad_irq_thread, |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 575 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
| 576 | DRIVER_NAME, keypad); |
| 577 | if (err) { |
Geert Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 578 | dev_err(dev, "irq request failed %d, error %d\n", client->irq, |
| 579 | err); |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 580 | return err; |
| 581 | } |
| 582 | |
| 583 | ht16k33_keypad_stop(keypad->dev); |
| 584 | |
Geert Uytterhoeven | 11b9291 | 2021-10-19 16:45:11 +0200 | [diff] [blame] | 585 | return input_register_device(keypad->dev); |
Dmitry Torokhov | cac513f | 2017-02-09 10:15:52 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Geert Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 588 | static int ht16k33_fbdev_probe(struct device *dev, struct ht16k33_priv *priv, |
| 589 | uint32_t brightness) |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 590 | { |
Geert Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 591 | struct ht16k33_fbdev *fbdev = &priv->fbdev; |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 592 | struct backlight_device *bl = NULL; |
Geert Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 593 | int err; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 594 | |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 595 | 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 Uytterhoeven | 80f9eb7 | 2021-10-19 16:45:08 +0200 | [diff] [blame] | 602 | |
Geert Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 603 | 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 Uytterhoeven | 80f9eb7 | 2021-10-19 16:45:08 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 619 | /* 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 Torokhov | 8fa8bea | 2017-02-09 10:15:53 -0800 | [diff] [blame] | 622 | if (!fbdev->buffer) |
| 623 | return -ENOMEM; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 624 | |
Geert Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 625 | fbdev->cache = devm_kmalloc(dev, HT16K33_FB_SIZE, GFP_KERNEL); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 626 | if (!fbdev->cache) { |
| 627 | err = -ENOMEM; |
| 628 | goto err_fbdev_buffer; |
| 629 | } |
| 630 | |
Geert Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 631 | fbdev->info = framebuffer_alloc(0, dev); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 632 | if (!fbdev->info) { |
| 633 | err = -ENOMEM; |
| 634 | goto err_fbdev_buffer; |
| 635 | } |
| 636 | |
Geert Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 637 | err = of_property_read_u32(dev->of_node, "refresh-rate-hz", |
| 638 | &fbdev->refresh_rate); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 639 | if (err) { |
Geert Uytterhoeven | d08a44d | 2021-10-19 16:45:13 +0200 | [diff] [blame] | 640 | dev_err(dev, "refresh rate not specified\n"); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 641 | goto err_fbdev_info; |
| 642 | } |
| 643 | fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS); |
| 644 | |
Geert Uytterhoeven | 85d93b1 | 2021-10-19 16:45:14 +0200 | [diff] [blame] | 645 | INIT_DELAYED_WORK(&priv->work, ht16k33_fb_update); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 646 | 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 Uytterhoeven | 80f9eb7 | 2021-10-19 16:45:08 +0200 | [diff] [blame] | 651 | fbdev->info->bl_dev = bl; |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 652 | 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 Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 660 | ht16k33_fb_queue(priv); |
| 661 | return 0; |
| 662 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 663 | err_fbdev_info: |
| 664 | framebuffer_release(fbdev->info); |
| 665 | err_fbdev_buffer: |
| 666 | free_page((unsigned long) fbdev->buffer); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 667 | |
| 668 | return err; |
| 669 | } |
| 670 | |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 671 | static 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 | |
| 711 | err_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 Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 717 | static int ht16k33_probe(struct i2c_client *client) |
| 718 | { |
| 719 | struct device *dev = &client->dev; |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 720 | const struct of_device_id *id; |
Geert Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 721 | 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 Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 735 | id = i2c_of_match_device(dev->driver->of_match_table, client); |
| 736 | if (id) |
| 737 | priv->type = (uintptr_t)id->data; |
Geert Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 738 | 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 Uytterhoeven | c223d9c | 2021-10-19 16:45:19 +0200 | [diff] [blame^] | 755 | /* LED */ |
| 756 | err = ht16k33_led_probe(dev, &priv->led, dft_brightness); |
| 757 | if (err) |
| 758 | return err; |
| 759 | |
Geert Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 760 | /* Keypad */ |
| 761 | if (client->irq > 0) { |
| 762 | err = ht16k33_keypad_probe(client, &priv->keypad); |
| 763 | if (err) |
| 764 | return err; |
| 765 | } |
| 766 | |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 767 | 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 Uytterhoeven | fcbb3c3 | 2021-10-19 16:45:16 +0200 | [diff] [blame] | 780 | } |
| 781 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 782 | static int ht16k33_remove(struct i2c_client *client) |
| 783 | { |
| 784 | struct ht16k33_priv *priv = i2c_get_clientdata(client); |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 785 | struct ht16k33_fbdev *fbdev = &priv->fbdev; |
| 786 | |
Geert Uytterhoeven | 85d93b1 | 2021-10-19 16:45:14 +0200 | [diff] [blame] | 787 | cancel_delayed_work_sync(&priv->work); |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 788 | |
| 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 Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 803 | |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static const struct i2c_device_id ht16k33_i2c_match[] = { |
| 808 | { "ht16k33", 0 }, |
| 809 | { } |
| 810 | }; |
| 811 | MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match); |
| 812 | |
| 813 | static const struct of_device_id ht16k33_of_match[] = { |
Geert Uytterhoeven | a042872 | 2021-10-19 16:45:17 +0200 | [diff] [blame] | 814 | { |
| 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 Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 824 | { } |
| 825 | }; |
| 826 | MODULE_DEVICE_TABLE(of, ht16k33_of_match); |
| 827 | |
| 828 | static struct i2c_driver ht16k33_driver = { |
Geert Uytterhoeven | e66b4f4 | 2021-10-19 16:45:12 +0200 | [diff] [blame] | 829 | .probe_new = ht16k33_probe, |
Robin van der Gracht | 8992da4 | 2016-11-07 10:56:35 +0100 | [diff] [blame] | 830 | .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 | }; |
| 837 | module_i2c_driver(ht16k33_driver); |
| 838 | |
| 839 | MODULE_DESCRIPTION("Holtek HT16K33 driver"); |
| 840 | MODULE_LICENSE("GPL"); |
| 841 | MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>"); |