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