dm355evm_keys.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/slab.h>
  13. #include <linux/input.h>
  14. #include <linux/input/sparse-keymap.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/mfd/dm355evm_msp.h>
  18. #include <linux/module.h>
  19. /*
  20. * The MSP430 firmware on the DM355 EVM monitors on-board pushbuttons
  21. * and an IR receptor used for the remote control. When any key is
  22. * pressed, or its autorepeat kicks in, an event is sent. This driver
  23. * read those events from the small (32 event) queue and reports them.
  24. *
  25. * Note that physically there can only be one of these devices.
  26. *
  27. * This driver was tested with firmware revision A4.
  28. */
  29. struct dm355evm_keys {
  30. struct input_dev *input;
  31. struct device *dev;
  32. };
  33. /* These initial keycodes can be remapped */
  34. static const struct key_entry dm355evm_keys[] = {
  35. /*
  36. * Pushbuttons on the EVM board ... note that the labels for these
  37. * are SW10/SW11/etc on the PC board. The left/right orientation
  38. * comes only from the firmware's documentation, and presumes the
  39. * power connector is immediately in front of you and the IR sensor
  40. * is to the right. (That is, rotate the board counter-clockwise
  41. * by 90 degrees from the SW10/etc and "DM355 EVM" labels.)
  42. */
  43. { KE_KEY, 0x00d8, { KEY_OK } }, /* SW12 */
  44. { KE_KEY, 0x00b8, { KEY_UP } }, /* SW13 */
  45. { KE_KEY, 0x00e8, { KEY_DOWN } }, /* SW11 */
  46. { KE_KEY, 0x0078, { KEY_LEFT } }, /* SW14 */
  47. { KE_KEY, 0x00f0, { KEY_RIGHT } }, /* SW10 */
  48. /*
  49. * IR buttons ... codes assigned to match the universal remote
  50. * provided with the EVM (Philips PM4S) using DVD code 0020.
  51. *
  52. * These event codes match firmware documentation, but other
  53. * remote controls could easily send more RC5-encoded events.
  54. * The PM4S manual was used in several cases to help select
  55. * a keycode reflecting the intended usage.
  56. *
  57. * RC5 codes are 14 bits, with two start bits (0x3 prefix)
  58. * and a toggle bit (masked out below).
  59. */
  60. { KE_KEY, 0x300c, { KEY_POWER } }, /* NOTE: docs omit this */
  61. { KE_KEY, 0x3000, { KEY_NUMERIC_0 } },
  62. { KE_KEY, 0x3001, { KEY_NUMERIC_1 } },
  63. { KE_KEY, 0x3002, { KEY_NUMERIC_2 } },
  64. { KE_KEY, 0x3003, { KEY_NUMERIC_3 } },
  65. { KE_KEY, 0x3004, { KEY_NUMERIC_4 } },
  66. { KE_KEY, 0x3005, { KEY_NUMERIC_5 } },
  67. { KE_KEY, 0x3006, { KEY_NUMERIC_6 } },
  68. { KE_KEY, 0x3007, { KEY_NUMERIC_7 } },
  69. { KE_KEY, 0x3008, { KEY_NUMERIC_8 } },
  70. { KE_KEY, 0x3009, { KEY_NUMERIC_9 } },
  71. { KE_KEY, 0x3022, { KEY_ENTER } },
  72. { KE_KEY, 0x30ec, { KEY_MODE } }, /* "tv/vcr/..." */
  73. { KE_KEY, 0x300f, { KEY_SELECT } }, /* "info" */
  74. { KE_KEY, 0x3020, { KEY_CHANNELUP } }, /* "up" */
  75. { KE_KEY, 0x302e, { KEY_MENU } }, /* "in/out" */
  76. { KE_KEY, 0x3011, { KEY_VOLUMEDOWN } }, /* "left" */
  77. { KE_KEY, 0x300d, { KEY_MUTE } }, /* "ok" */
  78. { KE_KEY, 0x3010, { KEY_VOLUMEUP } }, /* "right" */
  79. { KE_KEY, 0x301e, { KEY_SUBTITLE } }, /* "cc" */
  80. { KE_KEY, 0x3021, { KEY_CHANNELDOWN } },/* "down" */
  81. { KE_KEY, 0x3022, { KEY_PREVIOUS } },
  82. { KE_KEY, 0x3026, { KEY_SLEEP } },
  83. { KE_KEY, 0x3172, { KEY_REWIND } }, /* NOTE: docs wrongly say 0x30ca */
  84. { KE_KEY, 0x3175, { KEY_PLAY } },
  85. { KE_KEY, 0x3174, { KEY_FASTFORWARD } },
  86. { KE_KEY, 0x3177, { KEY_RECORD } },
  87. { KE_KEY, 0x3176, { KEY_STOP } },
  88. { KE_KEY, 0x3169, { KEY_PAUSE } },
  89. };
  90. /*
  91. * Because we communicate with the MSP430 using I2C, and all I2C calls
  92. * in Linux sleep, we use a threaded IRQ handler. The IRQ itself is
  93. * active low, but we go through the GPIO controller so we can trigger
  94. * on falling edges and not worry about enabling/disabling the IRQ in
  95. * the keypress handling path.
  96. */
  97. static irqreturn_t dm355evm_keys_irq(int irq, void *_keys)
  98. {
  99. static u16 last_event;
  100. struct dm355evm_keys *keys = _keys;
  101. const struct key_entry *ke;
  102. unsigned int keycode;
  103. int status;
  104. u16 event;
  105. /* For simplicity we ignore INPUT_COUNT and just read
  106. * events until we get the "queue empty" indicator.
  107. * Reading INPUT_LOW decrements the count.
  108. */
  109. for (;;) {
  110. status = dm355evm_msp_read(DM355EVM_MSP_INPUT_HIGH);
  111. if (status < 0) {
  112. dev_dbg(keys->dev, "input high err %d\n",
  113. status);
  114. break;
  115. }
  116. event = status << 8;
  117. status = dm355evm_msp_read(DM355EVM_MSP_INPUT_LOW);
  118. if (status < 0) {
  119. dev_dbg(keys->dev, "input low err %d\n",
  120. status);
  121. break;
  122. }
  123. event |= status;
  124. if (event == 0xdead)
  125. break;
  126. /* Press and release a button: two events, same code.
  127. * Press and hold (autorepeat), then release: N events
  128. * (N > 2), same code. For RC5 buttons the toggle bits
  129. * distinguish (for example) "1-autorepeat" from "1 1";
  130. * but PCB buttons don't support that bit.
  131. *
  132. * So we must synthesize release events. We do that by
  133. * mapping events to a press/release event pair; then
  134. * to avoid adding extra events, skip the second event
  135. * of each pair.
  136. */
  137. if (event == last_event) {
  138. last_event = 0;
  139. continue;
  140. }
  141. last_event = event;
  142. /* ignore the RC5 toggle bit */
  143. event &= ~0x0800;
  144. /* find the key, or report it as unknown */
  145. ke = sparse_keymap_entry_from_scancode(keys->input, event);
  146. keycode = ke ? ke->keycode : KEY_UNKNOWN;
  147. dev_dbg(keys->dev,
  148. "input event 0x%04x--> keycode %d\n",
  149. event, keycode);
  150. /* report press + release */
  151. input_report_key(keys->input, keycode, 1);
  152. input_sync(keys->input);
  153. input_report_key(keys->input, keycode, 0);
  154. input_sync(keys->input);
  155. }
  156. return IRQ_HANDLED;
  157. }
  158. /*----------------------------------------------------------------------*/
  159. static int dm355evm_keys_probe(struct platform_device *pdev)
  160. {
  161. struct dm355evm_keys *keys;
  162. struct input_dev *input;
  163. int irq;
  164. int error;
  165. keys = devm_kzalloc(&pdev->dev, sizeof (*keys), GFP_KERNEL);
  166. if (!keys)
  167. return -ENOMEM;
  168. input = devm_input_allocate_device(&pdev->dev);
  169. if (!input)
  170. return -ENOMEM;
  171. keys->dev = &pdev->dev;
  172. keys->input = input;
  173. input->name = "DM355 EVM Controls";
  174. input->phys = "dm355evm/input0";
  175. input->id.bustype = BUS_I2C;
  176. input->id.product = 0x0355;
  177. input->id.version = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
  178. error = sparse_keymap_setup(input, dm355evm_keys, NULL);
  179. if (error)
  180. return error;
  181. /* REVISIT: flush the event queue? */
  182. /* set up "threaded IRQ handler" */
  183. irq = platform_get_irq(pdev, 0);
  184. if (irq < 0)
  185. return irq;
  186. error = devm_request_threaded_irq(&pdev->dev, irq,
  187. NULL, dm355evm_keys_irq,
  188. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  189. dev_name(&pdev->dev), keys);
  190. if (error)
  191. return error;
  192. /* register */
  193. error = input_register_device(input);
  194. if (error)
  195. return error;
  196. return 0;
  197. }
  198. /* REVISIT: add suspend/resume when DaVinci supports it. The IRQ should
  199. * be able to wake up the system. When device_may_wakeup(&pdev->dev), call
  200. * enable_irq_wake() on suspend, and disable_irq_wake() on resume.
  201. */
  202. /*
  203. * I2C is used to talk to the MSP430, but this platform device is
  204. * exposed by an MFD driver that manages I2C communications.
  205. */
  206. static struct platform_driver dm355evm_keys_driver = {
  207. .probe = dm355evm_keys_probe,
  208. .driver = {
  209. .name = "dm355evm_keys",
  210. },
  211. };
  212. module_platform_driver(dm355evm_keys_driver);
  213. MODULE_LICENSE("GPL");