dm355evm_keys.c 8.7 KB

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