summaryrefslogtreecommitdiff
path: root/drivers/input/keyboard/tegra-kbc.c
blob: ea400bdeadcaa4674f08fa2296628fda1e975a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * drivers/input/keyboard/tegra-kbc.c
 *
 * Keyboard class input driver for the NVIDIA Tegra SoC internal matrix
 * keyboard controller
 *
 * Copyright (c) 2009, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/module.h>
#include <linux/input.h>
#include <linux/platform_device.h>
#include <linux/kthread.h>

#include <mach/nvrm_linux.h>
#include <nvddk_kbc.h>
#include <nvos.h>
#include <nvodm_kbc_keymapping.h>


static int tegra_kbc_event(struct input_dev *dev, unsigned int type,
	unsigned int code, int value)
{
	return 0;
}

struct NvOdmKeyVirtTableDetail **key_tab = NULL;
NvU32 key_tab_total = 0;

struct tegra_kbc_driver_data {
	struct input_dev	*input_dev;
	struct task_struct	*task;
	NvOsSemaphoreHandle	semaphore;
	NvDdkKbcHandle		ddkHandle;
	int			done;
};

#define in_table(_code, _tabl) \
	(((_code)>=(_tabl)->StartScanCode) && ((_code)<=(_tabl)->EndScanCode))

#define table_size(_tabl) ((_tabl)->EndScanCode - (_tabl)->StartScanCode + 1)

static NvU32 tegra_kbc_handle_keyev(struct tegra_kbc_driver_data *kbc)
{
	NvDdkKbcKeyEvent key_ev[16];
	NvU32 codes[16];
	NvU32 k_idx;
	NvU32 l_idx;
	NvU32 EventCount;
	NvU32 WaitTime;
	NvU32 i;
	NvU32 value;
	NvU32 key;

	WaitTime = NvDdkKbcGetKeyEvents(kbc->ddkHandle,
		&EventCount, codes, key_ev);

	for (i = 0; i < EventCount; i++) {
		if (key_ev[i] == NvDdkKbcKeyEvent_KeyPress) {
			value = 1;
		} else if (key_ev[i] == NvDdkKbcKeyEvent_KeyRelease) {
			value = 0;
		} else
			continue;

		key = 0;

		for (l_idx = 0; l_idx < key_tab_total; ++l_idx) {
			if (!in_table(codes[i], key_tab[l_idx]))
				continue;
			k_idx = codes[i] - key_tab[l_idx]->StartScanCode;
			key = key_tab[l_idx]->pVirtualKeyTable[k_idx];
		}

		input_report_key(kbc->input_dev, key, value);
	}

	if (WaitTime) {
		NvOsSleepMS(WaitTime);
	}

	return WaitTime;
}

static int tegra_kbc_thread(void *pdata)
{
	struct tegra_kbc_driver_data *kbc = pdata;
	NvU32 loop;

	for (;;) {
		/* FIXME should we use a NvOsSemaphoreWaitTimeout instead? */
		NvOsSemaphoreWait(kbc->semaphore);
		if (kbc->done)
			break;
		do {
			loop = tegra_kbc_handle_keyev(kbc);
		} while (loop);
	}

	return 0;
}

static void tegra_kbc_cleanup(struct tegra_kbc_driver_data *kbc)
{
	if (!kbc)
		return;

	if (kbc->task) {
		kbc->done = 1;
		NvOsSemaphoreSignal(kbc->semaphore);
		kthread_stop(kbc->task);
	}
	if (kbc->ddkHandle) {
		NvDdkKbcStop(kbc->ddkHandle);
		NvDdkKbcClose(kbc->ddkHandle);
	}

	if (kbc->semaphore)
		NvOsSemaphoreDestroy(kbc->semaphore);

	if (kbc->input_dev)
		input_free_device(kbc->input_dev);

	kfree(kbc);
}

static int __init tegra_kbc_probe(struct platform_device *pdev)
{
	struct tegra_kbc_driver_data *kbc = NULL;
	struct input_dev *input_dev = NULL;
	int err;
	NvError nverr;
	NvU32 l_idx;
	NvU32 k_idx;
	NvU32 TotalKey;

	kbc = kzalloc(sizeof(struct tegra_kbc_driver_data), GFP_KERNEL);
	input_dev = input_allocate_device();

	if (input_dev == NULL || kbc == NULL) {
		input_free_device(input_dev);
		kfree(kbc);
		err = -ENOMEM;
		pr_err("tegra_kbc_probe: Failed to allocate input device\n");
		return err;
	}
	nverr = NvOsSemaphoreCreate(&kbc->semaphore, 0);
	if (nverr != NvSuccess)	{
		err = -1;
		pr_err("tegra_kbc_probe: Semaphore creation failed\n");
		goto fail;
	}

	nverr = NvDdkKbcOpen (s_hRmGlobal, &kbc->ddkHandle);
	if (nverr != NvSuccess) {
		err = -1;
		pr_err("tegra_kbc_probe: NvDdkKbcOpen failed\n");
		goto fail;
	}

	nverr = NvDdkKbcStart(kbc->ddkHandle, kbc->semaphore);
	if (nverr != NvSuccess) {
		err = -1;
		pr_err("tegra_kbc_probe: NvDdkKbcStart failed\n");
		goto fail;
	}

	kbc->task = kthread_create(tegra_kbc_thread, kbc, "tegra_kbc_thread");
	if(kbc->task == NULL) {
		err = -1;
		goto fail;
	}
	wake_up_process( kbc->task );

	kbc->input_dev = input_dev;
	input_dev->event = tegra_kbc_event;
	input_dev->name = "tegra-kbc";
	__set_bit(EV_KEY, input_dev->evbit);

	key_tab_total = NvOdmKbcKeyMappingGetVirtualKeyMappingList(&key_tab);

	for (l_idx = 0; l_idx < key_tab_total; ++l_idx) {
		TotalKey = table_size(key_tab[l_idx]);
		for (k_idx = 0; k_idx < TotalKey; ++k_idx) {
			__set_bit(key_tab[l_idx]->pVirtualKeyTable[k_idx],
				input_dev->keybit);
		}
	}

	platform_set_drvdata(pdev, kbc);

	err = input_register_device(input_dev);
	if (err) {
		pr_err("tegra_kbc_probe: Unable to register %s input device\n",
			input_dev->name);
		goto fail;
	}

	return 0;

fail:
	tegra_kbc_cleanup(kbc);	  
	return err;
}

static int tegra_kbc_remove(struct platform_device *pdev)
{
	struct tegra_kbc_driver_data *kbc = platform_get_drvdata(pdev);
	tegra_kbc_cleanup(kbc);
	return 0;
}

static int tegra_kbc_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct tegra_kbc_driver_data *kbc = platform_get_drvdata(pdev);
	NvError e = NvError_Success;

	if (!kbc)
		return -1;

	if (!kbc->ddkHandle) {
		printk("%s: device handle is NULL\n", __func__);
		return -1;
	}

	/* power down hardware */
	e = NvDdkKbcSuspend(kbc->ddkHandle);
	if (e != NvSuccess) {
		printk("%s: hardware power down fail\n", __func__);
		return -1;
	}

	return 0;
}

static int tegra_kbc_resume(struct platform_device *pdev)
{
	struct tegra_kbc_driver_data *kbc = platform_get_drvdata(pdev);
	NvError e = NvError_Success;

	if (!kbc)
		return -1;

	if (!kbc->ddkHandle) {
		printk("%s: device handle is NULL\n", __func__);
		return -1;
	}

	/* power up hardware */
	e = NvDdkKbcResume(kbc->ddkHandle);
	if (e != NvSuccess) {
		printk("%s: hardware power up fail\n", __func__);
		return -1;
	}

	return 0;
}

static struct platform_driver tegra_kbc_driver = {
	.probe		= tegra_kbc_probe,
	.remove		= tegra_kbc_remove,
	.suspend	= tegra_kbc_suspend,
	.resume		= tegra_kbc_resume,
	.driver		= {
		.name	= "tegra_kbc",
	},
};

static int __devinit tegra_kbc_init(void)
{
	return platform_driver_register(&tegra_kbc_driver);
}

static void __exit tegra_kbc_exit(void)
{
	platform_driver_unregister(&tegra_kbc_driver);
}

module_init(tegra_kbc_init);
module_exit(tegra_kbc_exit);

MODULE_DESCRIPTION("Tegra Key board controller driver");