dm355evm_keys.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * dm355evm_keys.c - support buttons and IR remote on DM355 EVM board
  3. *
  4. * Copyright (c) 2008 by David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/input.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/i2c/dm355evm_msp.h>
  17. /*
  18. * The MSP430 firmware on the DM355 EVM monitors on-board pushbuttons
  19. * and an IR receptor used for the remote control. When any key is
  20. * pressed, or its autorepeat kicks in, an event is sent. This driver
  21. * read those events from the small (32 event) queue and reports them.
  22. *
  23. * Note that physically there can only be one of these devices.
  24. *
  25. * This driver was tested with firmware revision A4.
  26. */
  27. struct dm355evm_keys {
  28. struct input_dev *input;
  29. struct device *dev;
  30. int irq;
  31. };
  32. /* These initial keycodes can be remapped by dm355evm_setkeycode(). */
  33. static struct {
  34. u16 event;
  35. u16 keycode;
  36. } dm355evm_keys[] = {
  37. /*
  38. * Pushbuttons on the EVM board ... note that the labels for these
  39. * are SW10/SW11/etc on the PC board. The left/right orientation
  40. * comes only from the firmware's documentation, and presumes the
  41. * power connector is immediately in front of you and the IR sensor
  42. * is to the right. (That is, rotate the board counter-clockwise
  43. * by 90 degrees from the SW10/etc and "DM355 EVM" labels.)
  44. */
  45. { 0x00d8, KEY_OK, }, /* SW12 */
  46. { 0x00b8, KEY_UP, }, /* SW13 */
  47. { 0x00e8, KEY_DOWN, }, /* SW11 */
  48. { 0x0078, KEY_LEFT, }, /* SW14 */
  49. { 0x00f0, KEY_RIGHT, }, /* SW10 */
  50. /*
  51. * IR buttons ... codes assigned to match the universal remote
  52. * provided with the EVM (Philips PM4S) using DVD code 0020.
  53. *
  54. * These event codes match firmware documentation, but other
  55. * remote controls could easily send more RC5-encoded events.
  56. * The PM4S manual was used in several cases to help select
  57. * a keycode reflecting the intended usage.
  58. *
  59. * RC5 codes are 14 bits, with two start bits (0x3 prefix)
  60. * and a toggle bit (masked out below).
  61. */
  62. { 0x300c, KEY_POWER, }, /* NOTE: docs omit this */
  63. { 0x3000, KEY_NUMERIC_0, },
  64. { 0x3001, KEY_NUMERIC_1, },
  65. { 0x3002, KEY_NUMERIC_2, },
  66. { 0x3003, KEY_NUMERIC_3, },
  67. { 0x3004, KEY_NUMERIC_4, },
  68. { 0x3005, KEY_NUMERIC_5, },
  69. { 0x3006, KEY_NUMERIC_6, },
  70. { 0x3007, KEY_NUMERIC_7, },
  71. { 0x3008, KEY_NUMERIC_8, },
  72. { 0x3009, KEY_NUMERIC_9, },
  73. { 0x3022, KEY_ENTER, },
  74. { 0x30ec, KEY_MODE, }, /* "tv/vcr/..." */
  75. { 0x300f, KEY_SELECT, }, /* "info" */
  76. { 0x3020, KEY_CHANNELUP, }, /* "up" */
  77. { 0x302e, KEY_MENU, }, /* "in/out" */
  78. { 0x3011, KEY_VOLUMEDOWN, }, /* "left" */
  79. { 0x300d, KEY_MUTE, }, /* "ok" */
  80. { 0x3010, KEY_VOLUMEUP, }, /* "right" */
  81. { 0x301e, KEY_SUBTITLE, }, /* "cc" */
  82. { 0x3021, KEY_CHANNELDOWN, }, /* "down" */
  83. { 0x3022, KEY_PREVIOUS, },
  84. { 0x3026, KEY_SLEEP, },
  85. { 0x3172, KEY_REWIND, }, /* NOTE: docs wrongly say 0x30ca */
  86. { 0x3175, KEY_PLAY, },
  87. { 0x3174, KEY_FASTFORWARD, },
  88. { 0x3177, KEY_RECORD, },
  89. { 0x3176, KEY_STOP, },
  90. { 0x3169, KEY_PAUSE, },
  91. };
  92. /*
  93. * Because we communicate with the MSP430 using I2C, and all I2C calls
  94. * in Linux sleep, we use a threaded IRQ handler. The IRQ itself is
  95. * active low, but we go through the GPIO controller so we can trigger
  96. * on falling edges and not worry about enabling/disabling the IRQ in
  97. * the keypress handling path.
  98. */
  99. static irqreturn_t dm355evm_keys_irq(int irq, void *_keys)
  100. {
  101. struct dm355evm_keys *keys = _keys;
  102. int status;
  103. /* For simplicity we ignore INPUT_COUNT and just read
  104. * events until we get the "queue empty" indicator.
  105. * Reading INPUT_LOW decrements the count.
  106. */
  107. for (;;) {
  108. static u16 last_event;
  109. u16 event;
  110. int keycode;
  111. int i;
  112. status = dm355evm_msp_read(DM355EVM_MSP_INPUT_HIGH);
  113. if (status < 0) {
  114. dev_dbg(keys->dev, "input high err %d\n",
  115. status);
  116. break;
  117. }
  118. event = status << 8;
  119. status = dm355evm_msp_read(DM355EVM_MSP_INPUT_LOW);
  120. if (status < 0) {
  121. dev_dbg(keys->dev, "input low err %d\n",
  122. status);
  123. break;
  124. }
  125. event |= status;
  126. if (event == 0xdead)
  127. break;
  128. /* Press and release a button: two events, same code.
  129. * Press and hold (autorepeat), then release: N events
  130. * (N > 2), same code. For RC5 buttons the toggle bits
  131. * distinguish (for example) "1-autorepeat" from "1 1";
  132. * but PCB buttons don't support that bit.
  133. *
  134. * So we must synthesize release events. We do that by
  135. * mapping events to a press/release event pair; then
  136. * to avoid adding extra events, skip the second event
  137. * of each pair.
  138. */
  139. if (event == last_event) {
  140. last_event = 0;
  141. continue;
  142. }
  143. last_event = event;
  144. /* ignore the RC5 toggle bit */
  145. event &= ~0x0800;
  146. /* find the key, or leave it as unknown */
  147. keycode = KEY_UNKNOWN;
  148. for (i = 0; i < ARRAY_SIZE(dm355evm_keys); i++) {
  149. if (dm355evm_keys[i].event != event)
  150. continue;
  151. keycode = dm355evm_keys[i].keycode;
  152. break;
  153. }
  154. dev_dbg(keys->dev,
  155. "input event 0x%04x--> keycode %d\n",
  156. event, keycode);
  157. /* report press + release */
  158. input_report_key(keys->input, keycode, 1);
  159. input_sync(keys->input);
  160. input_report_key(keys->input, keycode, 0);
  161. input_sync(keys->input);
  162. }
  163. return IRQ_HANDLED;
  164. }
  165. static int dm355evm_setkeycode(struct input_dev *dev, int index, int keycode)
  166. {
  167. u16 old_keycode;
  168. unsigned i;
  169. if (((unsigned)index) >= ARRAY_SIZE(dm355evm_keys))
  170. return -EINVAL;
  171. old_keycode = dm355evm_keys[index].keycode;
  172. dm355evm_keys[index].keycode = keycode;
  173. set_bit(keycode, dev->keybit);
  174. for (i = 0; i < ARRAY_SIZE(dm355evm_keys); i++) {
  175. if (dm355evm_keys[index].keycode == old_keycode)
  176. goto done;
  177. }
  178. clear_bit(old_keycode, dev->keybit);
  179. done:
  180. return 0;
  181. }
  182. static int dm355evm_getkeycode(struct input_dev *dev, int index, int *keycode)
  183. {
  184. if (((unsigned)index) >= ARRAY_SIZE(dm355evm_keys))
  185. return -EINVAL;
  186. return dm355evm_keys[index].keycode;
  187. }
  188. /*----------------------------------------------------------------------*/
  189. static int __devinit dm355evm_keys_probe(struct platform_device *pdev)
  190. {
  191. struct dm355evm_keys *keys;
  192. struct input_dev *input;
  193. int status;
  194. int i;
  195. /* allocate instance struct and input dev */
  196. keys = kzalloc(sizeof *keys, GFP_KERNEL);
  197. input = input_allocate_device();
  198. if (!keys || !input) {
  199. status = -ENOMEM;
  200. goto fail1;
  201. }
  202. keys->dev = &pdev->dev;
  203. keys->input = input;
  204. /* set up "threaded IRQ handler" */
  205. status = platform_get_irq(pdev, 0);
  206. if (status < 0)
  207. goto fail1;
  208. keys->irq = status;
  209. input_set_drvdata(input, keys);
  210. input->name = "DM355 EVM Controls";
  211. input->phys = "dm355evm/input0";
  212. input->dev.parent = &pdev->dev;
  213. input->id.bustype = BUS_I2C;
  214. input->id.product = 0x0355;
  215. input->id.version = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
  216. input->evbit[0] = BIT(EV_KEY);
  217. for (i = 0; i < ARRAY_SIZE(dm355evm_keys); i++)
  218. __set_bit(dm355evm_keys[i].keycode, input->keybit);
  219. input->setkeycode = dm355evm_setkeycode;
  220. input->getkeycode = dm355evm_getkeycode;
  221. /* REVISIT: flush the event queue? */
  222. status = request_threaded_irq(keys->irq, NULL, dm355evm_keys_irq,
  223. IRQF_TRIGGER_FALLING, dev_name(&pdev->dev), keys);
  224. if (status < 0)
  225. goto fail1;
  226. /* register */
  227. status = input_register_device(input);
  228. if (status < 0)
  229. goto fail2;
  230. platform_set_drvdata(pdev, keys);
  231. return 0;
  232. fail2:
  233. free_irq(keys->irq, keys);
  234. fail1:
  235. input_free_device(input);
  236. kfree(keys);
  237. dev_err(&pdev->dev, "can't register, err %d\n", status);
  238. return status;
  239. }
  240. static int __devexit dm355evm_keys_remove(struct platform_device *pdev)
  241. {
  242. struct dm355evm_keys *keys = platform_get_drvdata(pdev);
  243. free_irq(keys->irq, keys);
  244. input_unregister_device(keys->input);
  245. kfree(keys);
  246. return 0;
  247. }
  248. /* REVISIT: add suspend/resume when DaVinci supports it. The IRQ should
  249. * be able to wake up the system. When device_may_wakeup(&pdev->dev), call
  250. * enable_irq_wake() on suspend, and disable_irq_wake() on resume.
  251. */
  252. /*
  253. * I2C is used to talk to the MSP430, but this platform device is
  254. * exposed by an MFD driver that manages I2C communications.
  255. */
  256. static struct platform_driver dm355evm_keys_driver = {
  257. .probe = dm355evm_keys_probe,
  258. .remove = __devexit_p(dm355evm_keys_remove),
  259. .driver = {
  260. .owner = THIS_MODULE,
  261. .name = "dm355evm_keys",
  262. },
  263. };
  264. static int __init dm355evm_keys_init(void)
  265. {
  266. return platform_driver_register(&dm355evm_keys_driver);
  267. }
  268. module_init(dm355evm_keys_init);
  269. static void __exit dm355evm_keys_exit(void)
  270. {
  271. platform_driver_unregister(&dm355evm_keys_driver);
  272. }
  273. module_exit(dm355evm_keys_exit);
  274. MODULE_LICENSE("GPL");