dm355evm_keys.c 7.1 KB

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