blob: b6b7bd4e54625d80e1112640c0226d6c4df68e6e [file] [log] [blame]
David Brownelleb990b52009-04-23 19:25:29 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
David Brownelleb990b52009-04-23 19:25:29 -070013#include <linux/input.h>
Dmitry Torokhov66040722009-12-04 10:22:25 -080014#include <linux/input/sparse-keymap.h>
David Brownelleb990b52009-04-23 19:25:29 -070015#include <linux/platform_device.h>
16#include <linux/interrupt.h>
17
18#include <linux/i2c/dm355evm_msp.h>
Paul Gortmakerd2d84422011-07-03 13:53:48 -040019#include <linux/module.h>
David Brownelleb990b52009-04-23 19:25:29 -070020
21
22/*
23 * The MSP430 firmware on the DM355 EVM monitors on-board pushbuttons
24 * and an IR receptor used for the remote control. When any key is
25 * pressed, or its autorepeat kicks in, an event is sent. This driver
26 * read those events from the small (32 event) queue and reports them.
27 *
David Brownelleb990b52009-04-23 19:25:29 -070028 * Note that physically there can only be one of these devices.
29 *
30 * This driver was tested with firmware revision A4.
31 */
32struct dm355evm_keys {
David Brownelleb990b52009-04-23 19:25:29 -070033 struct input_dev *input;
34 struct device *dev;
35 int irq;
36};
37
Dmitry Torokhov66040722009-12-04 10:22:25 -080038/* These initial keycodes can be remapped */
39static const struct key_entry dm355evm_keys[] = {
David Brownelleb990b52009-04-23 19:25:29 -070040 /*
41 * Pushbuttons on the EVM board ... note that the labels for these
42 * are SW10/SW11/etc on the PC board. The left/right orientation
43 * comes only from the firmware's documentation, and presumes the
44 * power connector is immediately in front of you and the IR sensor
45 * is to the right. (That is, rotate the board counter-clockwise
46 * by 90 degrees from the SW10/etc and "DM355 EVM" labels.)
47 */
Dmitry Torokhov66040722009-12-04 10:22:25 -080048 { KE_KEY, 0x00d8, { KEY_OK } }, /* SW12 */
49 { KE_KEY, 0x00b8, { KEY_UP } }, /* SW13 */
50 { KE_KEY, 0x00e8, { KEY_DOWN } }, /* SW11 */
51 { KE_KEY, 0x0078, { KEY_LEFT } }, /* SW14 */
52 { KE_KEY, 0x00f0, { KEY_RIGHT } }, /* SW10 */
David Brownelleb990b52009-04-23 19:25:29 -070053
54 /*
55 * IR buttons ... codes assigned to match the universal remote
56 * provided with the EVM (Philips PM4S) using DVD code 0020.
57 *
58 * These event codes match firmware documentation, but other
59 * remote controls could easily send more RC5-encoded events.
60 * The PM4S manual was used in several cases to help select
61 * a keycode reflecting the intended usage.
62 *
63 * RC5 codes are 14 bits, with two start bits (0x3 prefix)
64 * and a toggle bit (masked out below).
65 */
Dmitry Torokhov66040722009-12-04 10:22:25 -080066 { KE_KEY, 0x300c, { KEY_POWER } }, /* NOTE: docs omit this */
67 { KE_KEY, 0x3000, { KEY_NUMERIC_0 } },
68 { KE_KEY, 0x3001, { KEY_NUMERIC_1 } },
69 { KE_KEY, 0x3002, { KEY_NUMERIC_2 } },
70 { KE_KEY, 0x3003, { KEY_NUMERIC_3 } },
71 { KE_KEY, 0x3004, { KEY_NUMERIC_4 } },
72 { KE_KEY, 0x3005, { KEY_NUMERIC_5 } },
73 { KE_KEY, 0x3006, { KEY_NUMERIC_6 } },
74 { KE_KEY, 0x3007, { KEY_NUMERIC_7 } },
75 { KE_KEY, 0x3008, { KEY_NUMERIC_8 } },
76 { KE_KEY, 0x3009, { KEY_NUMERIC_9 } },
77 { KE_KEY, 0x3022, { KEY_ENTER } },
78 { KE_KEY, 0x30ec, { KEY_MODE } }, /* "tv/vcr/..." */
79 { KE_KEY, 0x300f, { KEY_SELECT } }, /* "info" */
80 { KE_KEY, 0x3020, { KEY_CHANNELUP } }, /* "up" */
81 { KE_KEY, 0x302e, { KEY_MENU } }, /* "in/out" */
82 { KE_KEY, 0x3011, { KEY_VOLUMEDOWN } }, /* "left" */
83 { KE_KEY, 0x300d, { KEY_MUTE } }, /* "ok" */
84 { KE_KEY, 0x3010, { KEY_VOLUMEUP } }, /* "right" */
85 { KE_KEY, 0x301e, { KEY_SUBTITLE } }, /* "cc" */
86 { KE_KEY, 0x3021, { KEY_CHANNELDOWN } },/* "down" */
87 { KE_KEY, 0x3022, { KEY_PREVIOUS } },
88 { KE_KEY, 0x3026, { KEY_SLEEP } },
89 { KE_KEY, 0x3172, { KEY_REWIND } }, /* NOTE: docs wrongly say 0x30ca */
90 { KE_KEY, 0x3175, { KEY_PLAY } },
91 { KE_KEY, 0x3174, { KEY_FASTFORWARD } },
92 { KE_KEY, 0x3177, { KEY_RECORD } },
93 { KE_KEY, 0x3176, { KEY_STOP } },
94 { KE_KEY, 0x3169, { KEY_PAUSE } },
David Brownelleb990b52009-04-23 19:25:29 -070095};
96
Dmitry Torokhov3b0a9ce2009-09-16 01:06:42 -070097/*
98 * Because we communicate with the MSP430 using I2C, and all I2C calls
99 * in Linux sleep, we use a threaded IRQ handler. The IRQ itself is
100 * active low, but we go through the GPIO controller so we can trigger
101 * on falling edges and not worry about enabling/disabling the IRQ in
102 * the keypress handling path.
103 */
David Brownell666cbe32009-06-28 23:50:08 -0700104static irqreturn_t dm355evm_keys_irq(int irq, void *_keys)
David Brownelleb990b52009-04-23 19:25:29 -0700105{
Dmitry Torokhov66040722009-12-04 10:22:25 -0800106 static u16 last_event;
107 struct dm355evm_keys *keys = _keys;
108 const struct key_entry *ke;
109 unsigned int keycode;
110 int status;
111 u16 event;
David Brownelleb990b52009-04-23 19:25:29 -0700112
David Brownelleb990b52009-04-23 19:25:29 -0700113 /* For simplicity we ignore INPUT_COUNT and just read
114 * events until we get the "queue empty" indicator.
115 * Reading INPUT_LOW decrements the count.
116 */
117 for (;;) {
David Brownelleb990b52009-04-23 19:25:29 -0700118 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
126 status = dm355evm_msp_read(DM355EVM_MSP_INPUT_LOW);
127 if (status < 0) {
128 dev_dbg(keys->dev, "input low err %d\n",
129 status);
130 break;
131 }
132 event |= status;
133 if (event == 0xdead)
134 break;
135
136 /* Press and release a button: two events, same code.
137 * Press and hold (autorepeat), then release: N events
138 * (N > 2), same code. For RC5 buttons the toggle bits
139 * distinguish (for example) "1-autorepeat" from "1 1";
140 * but PCB buttons don't support that bit.
141 *
142 * So we must synthesize release events. We do that by
143 * mapping events to a press/release event pair; then
144 * to avoid adding extra events, skip the second event
145 * of each pair.
146 */
147 if (event == last_event) {
148 last_event = 0;
149 continue;
150 }
151 last_event = event;
152
153 /* ignore the RC5 toggle bit */
154 event &= ~0x0800;
155
Dmitry Torokhov66040722009-12-04 10:22:25 -0800156 /* find the key, or report it as unknown */
157 ke = sparse_keymap_entry_from_scancode(keys->input, event);
158 keycode = ke ? ke->keycode : KEY_UNKNOWN;
David Brownelleb990b52009-04-23 19:25:29 -0700159 dev_dbg(keys->dev,
160 "input event 0x%04x--> keycode %d\n",
161 event, keycode);
162
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 }
Dmitry Torokhov66040722009-12-04 10:22:25 -0800169
David Brownell666cbe32009-06-28 23:50:08 -0700170 return IRQ_HANDLED;
171}
172
David Brownelleb990b52009-04-23 19:25:29 -0700173/*----------------------------------------------------------------------*/
174
Bill Pemberton5298cc42012-11-23 21:38:25 -0800175static int dm355evm_keys_probe(struct platform_device *pdev)
David Brownelleb990b52009-04-23 19:25:29 -0700176{
177 struct dm355evm_keys *keys;
178 struct input_dev *input;
179 int status;
David Brownelleb990b52009-04-23 19:25:29 -0700180
181 /* allocate instance struct and input dev */
182 keys = kzalloc(sizeof *keys, GFP_KERNEL);
183 input = input_allocate_device();
184 if (!keys || !input) {
185 status = -ENOMEM;
186 goto fail1;
187 }
188
189 keys->dev = &pdev->dev;
190 keys->input = input;
David Brownelleb990b52009-04-23 19:25:29 -0700191
192 /* set up "threaded IRQ handler" */
193 status = platform_get_irq(pdev, 0);
194 if (status < 0)
195 goto fail1;
196 keys->irq = status;
197
198 input_set_drvdata(input, keys);
199
200 input->name = "DM355 EVM Controls";
201 input->phys = "dm355evm/input0";
202 input->dev.parent = &pdev->dev;
203
204 input->id.bustype = BUS_I2C;
205 input->id.product = 0x0355;
206 input->id.version = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
207
Dmitry Torokhov66040722009-12-04 10:22:25 -0800208 status = sparse_keymap_setup(input, dm355evm_keys, NULL);
209 if (status)
210 goto fail1;
David Brownelleb990b52009-04-23 19:25:29 -0700211
212 /* REVISIT: flush the event queue? */
213
Dmitry Torokhov3b0a9ce2009-09-16 01:06:42 -0700214 status = request_threaded_irq(keys->irq, NULL, dm355evm_keys_irq,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -0700215 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
216 dev_name(&pdev->dev), keys);
David Brownelleb990b52009-04-23 19:25:29 -0700217 if (status < 0)
Dmitry Torokhov66040722009-12-04 10:22:25 -0800218 goto fail2;
David Brownelleb990b52009-04-23 19:25:29 -0700219
220 /* register */
221 status = input_register_device(input);
222 if (status < 0)
Dmitry Torokhov66040722009-12-04 10:22:25 -0800223 goto fail3;
David Brownelleb990b52009-04-23 19:25:29 -0700224
225 platform_set_drvdata(pdev, keys);
226
227 return 0;
228
Dmitry Torokhov66040722009-12-04 10:22:25 -0800229fail3:
David Brownelleb990b52009-04-23 19:25:29 -0700230 free_irq(keys->irq, keys);
Dmitry Torokhov66040722009-12-04 10:22:25 -0800231fail2:
232 sparse_keymap_free(input);
David Brownelleb990b52009-04-23 19:25:29 -0700233fail1:
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
Bill Pembertone2619cf2012-11-23 21:50:47 -0800241static int dm355evm_keys_remove(struct platform_device *pdev)
David Brownelleb990b52009-04-23 19:25:29 -0700242{
243 struct dm355evm_keys *keys = platform_get_drvdata(pdev);
244
245 free_irq(keys->irq, keys);
Dmitry Torokhov66040722009-12-04 10:22:25 -0800246 sparse_keymap_free(keys->input);
David Brownelleb990b52009-04-23 19:25:29 -0700247 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 */
262static struct platform_driver dm355evm_keys_driver = {
263 .probe = dm355evm_keys_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800264 .remove = dm355evm_keys_remove,
David Brownelleb990b52009-04-23 19:25:29 -0700265 .driver = {
David Brownelleb990b52009-04-23 19:25:29 -0700266 .name = "dm355evm_keys",
267 },
268};
JJ Ding840a7462011-11-29 11:08:40 -0800269module_platform_driver(dm355evm_keys_driver);
David Brownelleb990b52009-04-23 19:25:29 -0700270
271MODULE_LICENSE("GPL");