ht16k33.c 14 KB

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