summaryrefslogtreecommitdiff
path: root/drivers/input/misc/tegra_odm_accel.c
blob: badd580c41033c14f559da12654e0d69ae275e2d (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * drivers/input/misc/tegra_odm_accel.c
 *
 * Accelerometer input device using NVIDIA Tegra ODM kit
 *
 * 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 <linux/delay.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/tegra_devices.h>

#include <nvodm_services.h>
#include <nvodm_accelerometer.h>

#define DRIVER_NAME "nvodm_accelerometer"
#define READ_BUFFER_LENGTH 20

static struct input_dev *g_input_dev;

struct accelerometer_data {
	NvU32 x;
	NvU32 y;
	NvU32 z;
};

struct tegra_acc_device_data
{
	NvOdmAcrDeviceHandle	hOdmAcr;
	struct task_struct	*task;
	struct input_dev	*input_dev;
	NvU32			freq;
	NvBool			bThreadAlive;
	NvBool			show_log;
	NvOdmAccelIntType	IntType;
	NvOdmAccelAxisType	IntMotionAxis;
	NvOdmAccelAxisType	IntTapAxis;
	struct timeval		tv;
	struct accelerometer_data prev_data;
	struct accelerometer_data min_data;
	struct accelerometer_data max_data;
};

struct tegra_acc_device_data *accel_dev;

static const char* parameter[] = {
	"log=",
	"frequency=",
	"forcemotion=",
	"forcetap=",
	"timetap=",
	"openclose=",
};

static enum {
	COMMAND_LOG = 0,
	COMMAND_FREQUENCY,
	COMMAND_FORCEMOTION,
	COMMAND_FORCETAP,
	COMMAND_TIMETAP,
	COMMAND_OPENCLOSE,
}accel_enum;

/** Function to close the ODM device. This function will help in switching
 * between power modes 
 */
void close_odm_accl(void)
{
	NvOdmAccelClose(accel_dev->hOdmAcr);
	accel_dev->hOdmAcr = 0;
}

/** Function to open the ODM device with a set of default values. The values
 * are hardcoded as of now. Each time the device is closed/open, previous 
 * settings will be lost. This function will help in switching
 * between power modes
 */
int open_def_odm_accl(void)
{
	NvS32 err = -1;
	
	err = NvOdmAccelOpen(&(accel_dev->hOdmAcr));
	if (!err) {
		err = -ENODEV;
		pr_err("open_def_odm_accl: NvOdmAccelOpen failed\n");
		return err;
	}

	err = NvOdmAccelSetIntForceThreshold(accel_dev->hOdmAcr,
		 NvOdmAccelInt_MotionThreshold, 0, 900);
	if (!err) {
		pr_err("open_def_odm_accl: NvOdmAccelSetIntForceThreshold\n");
		return err;
	}

	err = NvOdmAccelSetIntEnable(accel_dev->hOdmAcr,
		NvOdmAccelInt_MotionThreshold, NvOdmAccelAxis_All, 0, NV_TRUE);

	if (!err) {
		pr_err("open_def_odm_accl: NvOdmAccelSetIntEnable failed\n");
		return err;
	}
	
	
	err = NvOdmAccelSetIntEnable(accel_dev->hOdmAcr,
		NvOdmAccelInt_TapThreshold, NvOdmAccelAxis_All, 0, NV_TRUE);

	if (!err) {
		pr_err("open_def_odm_accl: NvOdmAccelSetIntEnable failed\n");
		return err;
	}

	err = NvOdmAccelSetIntForceThreshold(accel_dev->hOdmAcr,
		NvOdmAccelInt_TapThreshold, 0, 120);

	if (!err) {
		pr_err("open_def_odm_accl: NvOdmAccelSetIntForceThreshold\n");
		return err;
	}

	err = NvOdmAccelSetIntTimeThreshold(accel_dev->hOdmAcr,
		NvOdmAccelInt_TapThreshold, 0, 2);

	if (!err) {
		pr_err("open_def_odm_accl: NvOdmAccelSetIntTimeThreshold\n");
		return err;
	}
	return err;
}

/**Function to parse the values sent through sysfs and sets them accordingly
 */
void change_nvodm_accelerometer_settings(NvU32 command, NvS32 value)
{
	switch (command) {
	case COMMAND_LOG:
		accel_dev->show_log = (value == 0 ? NV_FALSE : NV_TRUE);
		break;
	case COMMAND_FREQUENCY:
		if (value <  3) {
			NvOdmAccelSetSampleRate(accel_dev->hOdmAcr,
				NvOdmAccelPower_Low);
			if (NvOdmAccelSetSampleRate(accel_dev->hOdmAcr, value)) {
				accel_dev->freq = value;
			}
		} else {
			NvOdmAccelSetSampleRate(accel_dev->hOdmAcr,
				NvOdmAccelPower_Fullrun);
			if (NvOdmAccelSetSampleRate(accel_dev->hOdmAcr, value)) {
				accel_dev->freq = value;
			}
		}
		break;
	case COMMAND_TIMETAP:
		if( value > 0) {
			NvOdmAccelSetIntEnable(accel_dev->hOdmAcr,
				NvOdmAccelInt_TapThreshold,
				NvOdmAccelAxis_All, 0, NV_TRUE);

			NvOdmAccelSetIntTimeThreshold(accel_dev->hOdmAcr,
				NvOdmAccelInt_TapThreshold, 0, value);
		} else {
			NvOdmAccelSetIntEnable(accel_dev->hOdmAcr,
				NvOdmAccelInt_TapThreshold,
				NvOdmAccelAxis_All, 0, NV_FALSE);
		}
		break;
	case COMMAND_FORCETAP:
		if( value > 0) {
			NvOdmAccelSetIntEnable(accel_dev->hOdmAcr,
				NvOdmAccelInt_TapThreshold,
				NvOdmAccelAxis_All, 0, NV_TRUE);

			NvOdmAccelSetIntForceThreshold(accel_dev->hOdmAcr,
				NvOdmAccelInt_TapThreshold, 0, value);
		} else {
			NvOdmAccelSetIntEnable(accel_dev->hOdmAcr,
				NvOdmAccelInt_TapThreshold,
				NvOdmAccelAxis_All, 0, NV_FALSE);
		}
		break;
	case COMMAND_FORCEMOTION:
		if (value > 0) {
			NvOdmAccelSetIntEnable(accel_dev->hOdmAcr,
				NvOdmAccelInt_MotionThreshold,
				NvOdmAccelAxis_All, 0, NV_TRUE);
			NvOdmAccelSetIntForceThreshold(accel_dev->hOdmAcr,
				NvOdmAccelInt_MotionThreshold, 0, value);
		} else {
			NvOdmAccelSetIntEnable(accel_dev->hOdmAcr,
				NvOdmAccelInt_MotionThreshold,
				NvOdmAccelAxis_All, 0, NV_FALSE);
		}
		break;
	case COMMAND_OPENCLOSE:
		if (value) {
			if (!accel_dev->hOdmAcr)
				open_def_odm_accl();
		} else {
			if (accel_dev->hOdmAcr)
				close_odm_accl();
		}
		break;
	default:
		break;
	}
}


/** Function to give out settings values to the caller modules/application
 * though the use of sysfs. This function is stub as of now, but can be used
 * to return the device specific caps/attributes to the caller modules/app
 *  This function maps to show of sysfs
 */
ssize_t read_sysfs_accel(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	ssize_t len = 0;
	return len;
}

/** Function to take settings values to the caller modules/application
 * though the use of sysfs. This function can be used to set the 
 * device specific caps/attributes as specidied by the caller modules/app
 *  This function maps to store of sysfs
 */
ssize_t write_sysfs_accel(struct device *dev,
	struct device_attribute *attr, const char *buffer, size_t count)
{
	NvS32  i = 0;
	NvS8 *input;
	NvS32 value;

	count = count > READ_BUFFER_LENGTH ? READ_BUFFER_LENGTH : count;
	input = kzalloc(((int)count+1), GFP_KERNEL);
	if (!input) {
		goto err_0;
	}

	memcpy(input, buffer, count);

	input[count] = '\0';
	for (i=0; i<NV_ARRAY_SIZE(parameter); i++)  { 
		if (count > strlen(parameter[i]) &&
			!strncmp(parameter[i], input, strlen(parameter[i]))) {
			value = simple_strtol(&input[strlen(parameter[i])],
				'\0', 10);
			accel_enum = i;
			goto proceed;
		}
	}
	goto err_0;

proceed:
	if (accel_dev->hOdmAcr)
		change_nvodm_accelerometer_settings(accel_enum, value);
	else
		count = 0;

	kfree(input);
	return count;
err_0:
	if(input)
		kfree(input);
	count = 0;
	return count;

}

/** 
 * Function to register sysfs "device" and register the corresponding read
 * write functions
 */

DEVICE_ATTR(tegra_accelerometer, 0777, read_sysfs_accel, write_sysfs_accel);
NvS32 add_sysfs_entry(void)
{
	return device_create_file(&accel_dev->input_dev->dev,
		&dev_attr_tegra_accelerometer);
}

/** 
 * Function to register sysfs "device" and register the corresponding read
 * write functions
 */
NvS32 remove_sysfs_entry(void)
{
	device_remove_file(&accel_dev->input_dev->dev, 
		&dev_attr_tegra_accelerometer);
	return 0;
}

/**
 * Thread that waits for the interrupt from ODM and then sends out the
 * corresponding events.
 */
static NvS32 tegra_acc_thread (void *pdata)
{
	
	struct tegra_acc_device_data *accelerometer =
		(struct tegra_acc_device_data*)pdata;
	NvS32 x=0, y=0, z=0;
	accelerometer->bThreadAlive = 1;

	while (accelerometer->bThreadAlive) {
		NvOdmAccelWaitInt(accelerometer->hOdmAcr,
			&(accelerometer->IntType),
			&(accelerometer->IntMotionAxis),
			&(accelerometer->IntTapAxis));
		NvOdmAccelGetAcceleration(
			accelerometer->hOdmAcr, &x, &y, &z);
		if (accelerometer->show_log) {
			printk("Accelerometer: x=%d, y=%d, z=%d\n", x, y, z);
		}

		input_report_abs(accelerometer->input_dev, ABS_X, x);
		input_report_abs(accelerometer->input_dev, ABS_Y, y);
		input_report_abs(accelerometer->input_dev, ABS_Z, z);
		input_sync(accelerometer->input_dev);

		accelerometer->prev_data.x = x;
		accelerometer->prev_data.y = y;
		accelerometer->prev_data.z = z;		
	}
	
	return -1;
}

/**
 * All the device spefic initializations happen here. 
 */
static NvS32 __init tegra_acc_probe(struct platform_device *pdev)
{
	struct tegra_acc_device_data *accelerometer = NULL;
	struct input_dev *input_dev = NULL;
	NvS32 err;

	accelerometer = kzalloc(sizeof(*accelerometer), GFP_KERNEL);
	if (accelerometer == NULL) {
		err = -ENOMEM;
		pr_err("tegra_acc_probe: Failed to memory\n");
		goto allocate_dev_fail;
	}
	accel_dev = accelerometer;

	input_dev = input_allocate_device();
	if (input_dev == NULL) {
		err = -ENOMEM;
		pr_err("tegra_acc_probe: Failed to allocate input device\n");
		goto allocate_dev_fail;
	}
	g_input_dev = input_dev;
	
	err = open_def_odm_accl();
	if (!err) {
		pr_err("open_def_odm_accl: NvOdmAccelSetIntForceThreshold\n");
		goto allocate_dev_fail;
	}

	//start the Int thread.
	accelerometer->task = kthread_create(tegra_acc_thread, 
		accelerometer, "tegra_acc_thread");
	if(accelerometer->task == NULL) {
		err = -1;
		goto thread_create_failed;
	}
	wake_up_process(accelerometer->task);

	accelerometer->input_dev = input_dev;
	set_bit(EV_SYN, accelerometer->input_dev->evbit);
	set_bit(EV_KEY, accelerometer->input_dev->evbit);
	set_bit(EV_ABS, accelerometer->input_dev->evbit);

	input_set_abs_params(accelerometer->input_dev, ABS_X, 
		accelerometer->min_data.x, 
		accelerometer->max_data.x, 0, 0);
	input_set_abs_params(accelerometer->input_dev, ABS_Y,
		accelerometer->min_data.y, 
		accelerometer->max_data.y, 0, 0);
	input_set_abs_params(accelerometer->input_dev, ABS_Z,
		accelerometer->min_data.z, 
		accelerometer->max_data.z, 0, 0);

	platform_set_drvdata(pdev, accelerometer);

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

	err = add_sysfs_entry();
	if (err) {
		pr_err("tegra_acc_probe: add_sysfs_entry failed\n");
		goto sysfs_failed;
	}

	printk(KERN_INFO DRIVER_NAME "successfully registered\n");
	return err;

sysfs_failed:
	input_unregister_device(input_dev);
input_register_device_failed:
	accelerometer->bThreadAlive = 0;
thread_create_failed:
	//KillThreadHere!
allocate_dev_fail:
	close_odm_accl();
	input_free_device(input_dev);
	kfree(accelerometer);
	accelerometer = 0;
	err = -ENOMEM;

	return err;
}

static NvS32 tegra_acc_remove(struct platform_device *pdev)
{
	remove_sysfs_entry();
	input_unregister_device(g_input_dev);
	return 0;
}

static struct platform_driver tegra_acc_driver = {
	.probe	= tegra_acc_probe,
	.remove	= tegra_acc_remove,
	.driver	= {
		.name = "tegra_acc",
	},
};

static NvS32 __devinit tegra_acc_init(void)
{
	return platform_driver_register(&tegra_acc_driver);
}

static void __exit tegra_acc_exit(void)
{
	platform_driver_unregister(&tegra_acc_driver);
}

module_init(tegra_acc_init);
module_exit(tegra_acc_exit);

MODULE_DESCRIPTION("Tegra ODM Accelerometer driver");