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