ht16k33.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * HT16K33 driver
  4. *
  5. * Author: Robin van der Gracht <robin@protonic.nl>
  6. *
  7. * Copyright: (C) 2016 Protonic Holland.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/i2c.h>
  13. #include <linux/of.h>
  14. #include <linux/fb.h>
  15. #include <linux/slab.h>
  16. #include <linux/backlight.h>
  17. #include <linux/input.h>
  18. #include <linux/input/matrix_keypad.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/mm.h>
  21. /* Registers */
  22. #define REG_SYSTEM_SETUP 0x20
  23. #define REG_SYSTEM_SETUP_OSC_ON BIT(0)
  24. #define REG_DISPLAY_SETUP 0x80
  25. #define REG_DISPLAY_SETUP_ON BIT(0)
  26. #define REG_ROWINT_SET 0xA0
  27. #define REG_ROWINT_SET_INT_EN BIT(0)
  28. #define REG_ROWINT_SET_INT_ACT_HIGH BIT(1)
  29. #define REG_BRIGHTNESS 0xE0
  30. /* Defines */
  31. #define DRIVER_NAME "ht16k33"
  32. #define MIN_BRIGHTNESS 0x1
  33. #define MAX_BRIGHTNESS 0x10
  34. #define HT16K33_MATRIX_LED_MAX_COLS 8
  35. #define HT16K33_MATRIX_LED_MAX_ROWS 16
  36. #define HT16K33_MATRIX_KEYPAD_MAX_COLS 3
  37. #define HT16K33_MATRIX_KEYPAD_MAX_ROWS 12
  38. #define BYTES_PER_ROW (HT16K33_MATRIX_LED_MAX_ROWS / 8)
  39. #define HT16K33_FB_SIZE (HT16K33_MATRIX_LED_MAX_COLS * BYTES_PER_ROW)
  40. struct ht16k33_keypad {
  41. struct i2c_client *client;
  42. struct input_dev *dev;
  43. uint32_t cols;
  44. uint32_t rows;
  45. uint32_t row_shift;
  46. uint32_t debounce_ms;
  47. uint16_t last_key_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
  48. wait_queue_head_t wait;
  49. bool stopped;
  50. };
  51. struct ht16k33_fbdev {
  52. struct fb_info *info;
  53. uint32_t refresh_rate;
  54. uint8_t *buffer;
  55. uint8_t *cache;
  56. struct delayed_work work;
  57. };
  58. struct ht16k33_priv {
  59. struct i2c_client *client;
  60. struct ht16k33_keypad keypad;
  61. struct ht16k33_fbdev fbdev;
  62. };
  63. static struct fb_fix_screeninfo ht16k33_fb_fix = {
  64. .id = DRIVER_NAME,
  65. .type = FB_TYPE_PACKED_PIXELS,
  66. .visual = FB_VISUAL_MONO10,
  67. .xpanstep = 0,
  68. .ypanstep = 0,
  69. .ywrapstep = 0,
  70. .line_length = HT16K33_MATRIX_LED_MAX_ROWS,
  71. .accel = FB_ACCEL_NONE,
  72. };
  73. static struct fb_var_screeninfo ht16k33_fb_var = {
  74. .xres = HT16K33_MATRIX_LED_MAX_ROWS,
  75. .yres = HT16K33_MATRIX_LED_MAX_COLS,
  76. .xres_virtual = HT16K33_MATRIX_LED_MAX_ROWS,
  77. .yres_virtual = HT16K33_MATRIX_LED_MAX_COLS,
  78. .bits_per_pixel = 1,
  79. .red = { 0, 1, 0 },
  80. .green = { 0, 1, 0 },
  81. .blue = { 0, 1, 0 },
  82. .left_margin = 0,
  83. .right_margin = 0,
  84. .upper_margin = 0,
  85. .lower_margin = 0,
  86. .vmode = FB_VMODE_NONINTERLACED,
  87. };
  88. static int ht16k33_display_on(struct ht16k33_priv *priv)
  89. {
  90. uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON;
  91. return i2c_smbus_write_byte(priv->client, data);
  92. }
  93. static int ht16k33_display_off(struct ht16k33_priv *priv)
  94. {
  95. return i2c_smbus_write_byte(priv->client, REG_DISPLAY_SETUP);
  96. }
  97. static void ht16k33_fb_queue(struct ht16k33_priv *priv)
  98. {
  99. struct ht16k33_fbdev *fbdev = &priv->fbdev;
  100. schedule_delayed_work(&fbdev->work,
  101. msecs_to_jiffies(HZ / fbdev->refresh_rate));
  102. }
  103. /*
  104. * This gets the fb data from cache and copies it to ht16k33 display RAM
  105. */
  106. static void ht16k33_fb_update(struct work_struct *work)
  107. {
  108. struct ht16k33_fbdev *fbdev =
  109. container_of(work, struct ht16k33_fbdev, work.work);
  110. struct ht16k33_priv *priv =
  111. container_of(fbdev, struct ht16k33_priv, fbdev);
  112. uint8_t *p1, *p2;
  113. int len, pos = 0, first = -1;
  114. p1 = fbdev->cache;
  115. p2 = fbdev->buffer;
  116. /* Search for the first byte with changes */
  117. while (pos < HT16K33_FB_SIZE && first < 0) {
  118. if (*(p1++) - *(p2++))
  119. first = pos;
  120. pos++;
  121. }
  122. /* No changes found */
  123. if (first < 0)
  124. goto requeue;
  125. len = HT16K33_FB_SIZE - first;
  126. p1 = fbdev->cache + HT16K33_FB_SIZE - 1;
  127. p2 = fbdev->buffer + HT16K33_FB_SIZE - 1;
  128. /* Determine i2c transfer length */
  129. while (len > 1) {
  130. if (*(p1--) - *(p2--))
  131. break;
  132. len--;
  133. }
  134. p1 = fbdev->cache + first;
  135. p2 = fbdev->buffer + first;
  136. if (!i2c_smbus_write_i2c_block_data(priv->client, first, len, p2))
  137. memcpy(p1, p2, len);
  138. requeue:
  139. ht16k33_fb_queue(priv);
  140. }
  141. static int ht16k33_initialize(struct ht16k33_priv *priv)
  142. {
  143. uint8_t byte;
  144. int err;
  145. uint8_t data[HT16K33_MATRIX_LED_MAX_COLS * 2];
  146. /* Clear RAM (8 * 16 bits) */
  147. memset(data, 0, sizeof(data));
  148. err = i2c_smbus_write_block_data(priv->client, 0, sizeof(data), data);
  149. if (err)
  150. return err;
  151. /* Turn on internal oscillator */
  152. byte = REG_SYSTEM_SETUP_OSC_ON | REG_SYSTEM_SETUP;
  153. err = i2c_smbus_write_byte(priv->client, byte);
  154. if (err)
  155. return err;
  156. /* Configure INT pin */
  157. byte = REG_ROWINT_SET | REG_ROWINT_SET_INT_ACT_HIGH;
  158. if (priv->client->irq > 0)
  159. byte |= REG_ROWINT_SET_INT_EN;
  160. return i2c_smbus_write_byte(priv->client, byte);
  161. }
  162. static int ht16k33_bl_update_status(struct backlight_device *bl)
  163. {
  164. int brightness = bl->props.brightness;
  165. struct ht16k33_priv *priv = bl_get_data(bl);
  166. if (bl->props.power != FB_BLANK_UNBLANK ||
  167. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  168. bl->props.state & BL_CORE_FBBLANK || brightness == 0) {
  169. return ht16k33_display_off(priv);
  170. }
  171. ht16k33_display_on(priv);
  172. return i2c_smbus_write_byte(priv->client,
  173. REG_BRIGHTNESS | (brightness - 1));
  174. }
  175. static int ht16k33_bl_check_fb(struct backlight_device *bl, struct fb_info *fi)
  176. {
  177. struct ht16k33_priv *priv = bl_get_data(bl);
  178. return (fi == NULL) || (fi->par == priv);
  179. }
  180. static const struct backlight_ops ht16k33_bl_ops = {
  181. .update_status = ht16k33_bl_update_status,
  182. .check_fb = ht16k33_bl_check_fb,
  183. };
  184. static int ht16k33_mmap(struct fb_info *info, struct vm_area_struct *vma)
  185. {
  186. struct ht16k33_priv *priv = info->par;
  187. return vm_insert_page(vma, vma->vm_start,
  188. virt_to_page(priv->fbdev.buffer));
  189. }
  190. static struct fb_ops ht16k33_fb_ops = {
  191. .owner = THIS_MODULE,
  192. .fb_read = fb_sys_read,
  193. .fb_write = fb_sys_write,
  194. .fb_fillrect = sys_fillrect,
  195. .fb_copyarea = sys_copyarea,
  196. .fb_imageblit = sys_imageblit,
  197. .fb_mmap = ht16k33_mmap,
  198. };
  199. /*
  200. * This gets the keys from keypad and reports it to input subsystem.
  201. * Returns true if a key is pressed.
  202. */
  203. static bool ht16k33_keypad_scan(struct ht16k33_keypad *keypad)
  204. {
  205. const unsigned short *keycodes = keypad->dev->keycode;
  206. u16 new_state[HT16K33_MATRIX_KEYPAD_MAX_COLS];
  207. __le16 data[HT16K33_MATRIX_KEYPAD_MAX_COLS];
  208. unsigned long bits_changed;
  209. int row, col, code;
  210. int rc;
  211. bool pressed = false;
  212. rc = i2c_smbus_read_i2c_block_data(keypad->client, 0x40,
  213. sizeof(data), (u8 *)data);
  214. if (rc != sizeof(data)) {
  215. dev_err(&keypad->client->dev,
  216. "Failed to read key data, rc=%d\n", rc);
  217. return false;
  218. }
  219. for (col = 0; col < keypad->cols; col++) {
  220. new_state[col] = le16_to_cpu(data[col]);
  221. if (new_state[col])
  222. pressed = true;
  223. bits_changed = keypad->last_key_state[col] ^ new_state[col];
  224. for_each_set_bit(row, &bits_changed, BITS_PER_LONG) {
  225. code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
  226. input_event(keypad->dev, EV_MSC, MSC_SCAN, code);
  227. input_report_key(keypad->dev, keycodes[code],
  228. new_state[col] & BIT(row));
  229. }
  230. }
  231. input_sync(keypad->dev);
  232. memcpy(keypad->last_key_state, new_state, sizeof(u16) * keypad->cols);
  233. return pressed;
  234. }
  235. static irqreturn_t ht16k33_keypad_irq_thread(int irq, void *dev)
  236. {
  237. struct ht16k33_keypad *keypad = dev;
  238. do {
  239. wait_event_timeout(keypad->wait, keypad->stopped,
  240. msecs_to_jiffies(keypad->debounce_ms));
  241. if (keypad->stopped)
  242. break;
  243. } while (ht16k33_keypad_scan(keypad));
  244. return IRQ_HANDLED;
  245. }
  246. static int ht16k33_keypad_start(struct input_dev *dev)
  247. {
  248. struct ht16k33_keypad *keypad = input_get_drvdata(dev);
  249. keypad->stopped = false;
  250. mb();
  251. enable_irq(keypad->client->irq);
  252. return 0;
  253. }
  254. static void ht16k33_keypad_stop(struct input_dev *dev)
  255. {
  256. struct ht16k33_keypad *keypad = input_get_drvdata(dev);
  257. keypad->stopped = true;
  258. mb();
  259. wake_up(&keypad->wait);
  260. disable_irq(keypad->client->irq);
  261. }
  262. static int ht16k33_keypad_probe(struct i2c_client *client,
  263. struct ht16k33_keypad *keypad)
  264. {
  265. struct device_node *node = client->dev.of_node;
  266. u32 rows = HT16K33_MATRIX_KEYPAD_MAX_ROWS;
  267. u32 cols = HT16K33_MATRIX_KEYPAD_MAX_COLS;
  268. int err;
  269. keypad->client = client;
  270. init_waitqueue_head(&keypad->wait);
  271. keypad->dev = devm_input_allocate_device(&client->dev);
  272. if (!keypad->dev)
  273. return -ENOMEM;
  274. input_set_drvdata(keypad->dev, keypad);
  275. keypad->dev->name = DRIVER_NAME"-keypad";
  276. keypad->dev->id.bustype = BUS_I2C;
  277. keypad->dev->open = ht16k33_keypad_start;
  278. keypad->dev->close = ht16k33_keypad_stop;
  279. if (!of_get_property(node, "linux,no-autorepeat", NULL))
  280. __set_bit(EV_REP, keypad->dev->evbit);
  281. err = of_property_read_u32(node, "debounce-delay-ms",
  282. &keypad->debounce_ms);
  283. if (err) {
  284. dev_err(&client->dev, "key debounce delay not specified\n");
  285. return err;
  286. }
  287. err = matrix_keypad_parse_of_params(&client->dev, &rows, &cols);
  288. if (err)
  289. return err;
  290. if (rows > HT16K33_MATRIX_KEYPAD_MAX_ROWS ||
  291. cols > HT16K33_MATRIX_KEYPAD_MAX_COLS) {
  292. dev_err(&client->dev, "%u rows or %u cols out of range in DT\n",
  293. rows, cols);
  294. return -ERANGE;
  295. }
  296. keypad->rows = rows;
  297. keypad->cols = cols;
  298. keypad->row_shift = get_count_order(cols);
  299. err = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL,
  300. keypad->dev);
  301. if (err) {
  302. dev_err(&client->dev, "failed to build keymap\n");
  303. return err;
  304. }
  305. err = devm_request_threaded_irq(&client->dev, client->irq,
  306. NULL, ht16k33_keypad_irq_thread,
  307. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  308. DRIVER_NAME, keypad);
  309. if (err) {
  310. dev_err(&client->dev, "irq request failed %d, error %d\n",
  311. client->irq, err);
  312. return err;
  313. }
  314. ht16k33_keypad_stop(keypad->dev);
  315. err = input_register_device(keypad->dev);
  316. if (err)
  317. return err;
  318. return 0;
  319. }
  320. static int ht16k33_probe(struct i2c_client *client,
  321. const struct i2c_device_id *id)
  322. {
  323. int err;
  324. uint32_t dft_brightness;
  325. struct backlight_device *bl;
  326. struct backlight_properties bl_props;
  327. struct ht16k33_priv *priv;
  328. struct ht16k33_fbdev *fbdev;
  329. struct device_node *node = client->dev.of_node;
  330. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  331. dev_err(&client->dev, "i2c_check_functionality error\n");
  332. return -EIO;
  333. }
  334. if (client->irq <= 0) {
  335. dev_err(&client->dev, "No IRQ specified\n");
  336. return -EINVAL;
  337. }
  338. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  339. if (!priv)
  340. return -ENOMEM;
  341. priv->client = client;
  342. i2c_set_clientdata(client, priv);
  343. fbdev = &priv->fbdev;
  344. err = ht16k33_initialize(priv);
  345. if (err)
  346. return err;
  347. /* Framebuffer (2 bytes per column) */
  348. BUILD_BUG_ON(PAGE_SIZE < HT16K33_FB_SIZE);
  349. fbdev->buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
  350. if (!fbdev->buffer)
  351. return -ENOMEM;
  352. fbdev->cache = devm_kmalloc(&client->dev, HT16K33_FB_SIZE, GFP_KERNEL);
  353. if (!fbdev->cache) {
  354. err = -ENOMEM;
  355. goto err_fbdev_buffer;
  356. }
  357. fbdev->info = framebuffer_alloc(0, &client->dev);
  358. if (!fbdev->info) {
  359. err = -ENOMEM;
  360. goto err_fbdev_buffer;
  361. }
  362. err = of_property_read_u32(node, "refresh-rate-hz",
  363. &fbdev->refresh_rate);
  364. if (err) {
  365. dev_err(&client->dev, "refresh rate not specified\n");
  366. goto err_fbdev_info;
  367. }
  368. fb_bl_default_curve(fbdev->info, 0, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
  369. INIT_DELAYED_WORK(&fbdev->work, ht16k33_fb_update);
  370. fbdev->info->fbops = &ht16k33_fb_ops;
  371. fbdev->info->screen_base = (char __iomem *) fbdev->buffer;
  372. fbdev->info->screen_size = HT16K33_FB_SIZE;
  373. fbdev->info->fix = ht16k33_fb_fix;
  374. fbdev->info->var = ht16k33_fb_var;
  375. fbdev->info->pseudo_palette = NULL;
  376. fbdev->info->flags = FBINFO_FLAG_DEFAULT;
  377. fbdev->info->par = priv;
  378. err = register_framebuffer(fbdev->info);
  379. if (err)
  380. goto err_fbdev_info;
  381. err = ht16k33_keypad_probe(client, &priv->keypad);
  382. if (err)
  383. goto err_fbdev_unregister;
  384. /* Backlight */
  385. memset(&bl_props, 0, sizeof(struct backlight_properties));
  386. bl_props.type = BACKLIGHT_RAW;
  387. bl_props.max_brightness = MAX_BRIGHTNESS;
  388. bl = devm_backlight_device_register(&client->dev, DRIVER_NAME"-bl",
  389. &client->dev, priv,
  390. &ht16k33_bl_ops, &bl_props);
  391. if (IS_ERR(bl)) {
  392. dev_err(&client->dev, "failed to register backlight\n");
  393. err = PTR_ERR(bl);
  394. goto err_fbdev_unregister;
  395. }
  396. err = of_property_read_u32(node, "default-brightness-level",
  397. &dft_brightness);
  398. if (err) {
  399. dft_brightness = MAX_BRIGHTNESS;
  400. } else if (dft_brightness > MAX_BRIGHTNESS) {
  401. dev_warn(&client->dev,
  402. "invalid default brightness level: %u, using %u\n",
  403. dft_brightness, MAX_BRIGHTNESS);
  404. dft_brightness = MAX_BRIGHTNESS;
  405. }
  406. bl->props.brightness = dft_brightness;
  407. ht16k33_bl_update_status(bl);
  408. ht16k33_fb_queue(priv);
  409. return 0;
  410. err_fbdev_unregister:
  411. unregister_framebuffer(fbdev->info);
  412. err_fbdev_info:
  413. framebuffer_release(fbdev->info);
  414. err_fbdev_buffer:
  415. free_page((unsigned long) fbdev->buffer);
  416. return err;
  417. }
  418. static int ht16k33_remove(struct i2c_client *client)
  419. {
  420. struct ht16k33_priv *priv = i2c_get_clientdata(client);
  421. struct ht16k33_fbdev *fbdev = &priv->fbdev;
  422. cancel_delayed_work_sync(&fbdev->work);
  423. unregister_framebuffer(fbdev->info);
  424. framebuffer_release(fbdev->info);
  425. free_page((unsigned long) fbdev->buffer);
  426. return 0;
  427. }
  428. static const struct i2c_device_id ht16k33_i2c_match[] = {
  429. { "ht16k33", 0 },
  430. { }
  431. };
  432. MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match);
  433. static const struct of_device_id ht16k33_of_match[] = {
  434. { .compatible = "holtek,ht16k33", },
  435. { }
  436. };
  437. MODULE_DEVICE_TABLE(of, ht16k33_of_match);
  438. static struct i2c_driver ht16k33_driver = {
  439. .probe = ht16k33_probe,
  440. .remove = ht16k33_remove,
  441. .driver = {
  442. .name = DRIVER_NAME,
  443. .of_match_table = of_match_ptr(ht16k33_of_match),
  444. },
  445. .id_table = ht16k33_i2c_match,
  446. };
  447. module_i2c_driver(ht16k33_driver);
  448. MODULE_DESCRIPTION("Holtek HT16K33 driver");
  449. MODULE_LICENSE("GPL");
  450. MODULE_AUTHOR("Robin van der Gracht <robin@protonic.nl>");