aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/mpu3050/mpu-accel.c
blob: 4b5c64187c6f6aac88cdcce9036b2e954999ce53 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
    mpu-accel.c - mpu3050 input device interface

    Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.

    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, see <http://www.gnu.org/licenses/>.
*/

#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/signal.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <linux/pm.h>
#include <linux/mutex.h>
#include <linux/suspend.h>

#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/input.h>

#include "mpuirq.h"
#include "slaveirq.h"
#include "mlsl.h"
#include "mpu-i2c.h"
#include "mldl_cfg.h"
#include "mpu.h"
#include "mpu-accel.h"

#define MPUACC_DEBUG 0
#define MPUACC_DEBUG_CFG 0

#define MPUACCEL_INPUT_NAME "mpu-accel"

struct mpuaccel_data {
	struct input_dev *input_data;
	struct delayed_work work;
	struct mutex data_mutex;

	struct mldl_cfg *mldl_cfg;
	void *accel_handle;

	atomic_t enable;
	atomic_t poll_delay;
	int device_is_on;
#ifdef MPUACC_USES_CACHED_DATA
	unsigned char cached_data[6];
#endif				/* MPUACC_USES_CACHED_DATA */
};

static struct mpuaccel_data *pThisData;
extern struct acc_data cal_data;

static void mpu_accel_print_mldl_cfg(struct mldl_cfg *mldl_cfg)
{
	if (MPUACC_DEBUG_CFG) {
		pr_info("requested_sensors:%ld\n", mldl_cfg->requested_sensors);
/*  pr_info("ignore_system_suspend:%d\n", mldl_cfg->ignore_system_suspend); */
		pr_info("addr:%d\n", mldl_cfg->addr);
		pr_info("int_config:%d\n", mldl_cfg->int_config);
		pr_info("ext_sync:%d\n", mldl_cfg->ext_sync);
		pr_info("full_scale:%d\n", mldl_cfg->full_scale);
		pr_info("dmp_enable:%d\n", mldl_cfg->dmp_enable);
		pr_info("fifo_enable:%d\n", mldl_cfg->fifo_enable);
		pr_info("dmp_cfg1:%d\n", mldl_cfg->dmp_cfg1);
		pr_info("dmp_cfg2:%d\n", mldl_cfg->dmp_cfg2);
		pr_info("gyro_power:%d\n", mldl_cfg->gyro_power);
		pr_info("gyro_is_bypassed:%d\n", mldl_cfg->gyro_is_bypassed);
		pr_info("dmp_is_running:%d\n", mldl_cfg->dmp_is_running);
		pr_info("gyro_is_suspended:%d\n", mldl_cfg->gyro_is_suspended);
		pr_info("accel_is_suspended:%d\n",
			mldl_cfg->accel_is_suspended);
		pr_info("compass_is_suspended:%d\n",
			mldl_cfg->compass_is_suspended);
		pr_info("pressure_is_suspended:%d\n",
			mldl_cfg->pressure_is_suspended);
		pr_info("gyro_needs_reset:%d\n", mldl_cfg->gyro_needs_reset);
	}
}

static int mpu_accel_mutex_lock(struct mpuaccel_data *data)
{
	mutex_lock(&data->data_mutex);

	return ML_SUCCESS;
}

static int mpu_accel_mutex_unlock(struct mpuaccel_data *data)
{
	mutex_unlock(&data->data_mutex);

	return ML_SUCCESS;
}

static int mpu_accel_activate_device(struct mpuaccel_data *data, int enable)
{
	int result = ML_SUCCESS;
	struct mldl_cfg *mldl_cfg = data->mldl_cfg;

	if (enable) {
		/*turn on accel */
		if (NULL != mldl_cfg->accel
		 && NULL != mldl_cfg->accel->resume) {
			result = mldl_cfg->accel->resume(data->accel_handle,
							 mldl_cfg->accel,
							 &mldl_cfg->pdata->
							 accel);
		}
	} else {
		/*turn off accel */
		if (NULL != mldl_cfg->accel
		 && NULL != mldl_cfg->accel->suspend) {
			result = mldl_cfg->accel->suspend(data->accel_handle,
							  mldl_cfg->accel,
							  &mldl_cfg->pdata->
							  accel);
		}
	}

	if (result == ML_SUCCESS)
		data->device_is_on = enable;

	if (MPUACC_DEBUG)
		pr_info("activate device:%d, result=%d\n", enable, result);

	return result;
}

static int mpu_accel_get_data_from_device(struct mpuaccel_data *data,
					 unsigned char *buffer)
{
	int result = ML_SUCCESS;
	struct mldl_cfg *mldl_cfg = data->mldl_cfg;

	if (NULL != mldl_cfg->accel && NULL != mldl_cfg->accel->read) {
		result = mldl_cfg->accel->read(data->accel_handle,
							 mldl_cfg->accel,
							 &mldl_cfg->pdata->accel, buffer);
	}

	return result;
}

static int mpu_accel_get_data_from_mpu(struct mpuaccel_data *data, unsigned char *buffer)
{
	int result = ML_SUCCESS;
	struct mldl_cfg *mldl_cfg = data->mldl_cfg;
	result =
	    MLSLSerialRead(data->accel_handle, mldl_cfg->addr, 0x23, 6, buffer);
	return result;
}

static int mpu_accel_get_data(struct mpuaccel_data *data, unsigned char *buffer,
		   int *from_mpu)
{
	int res = ML_SUCCESS;
	struct mldl_cfg *mldl_cfg = data->mldl_cfg;

	if (mldl_cfg->accel_is_suspended == 1 ||
	   (mldl_cfg->dmp_is_running == 0
	   && mldl_cfg->accel_is_suspended == 0)) {

		if (from_mpu != NULL)
			*from_mpu = 0;

		/*
		   Retrieve accel data from accel device driver directly.
		 */
		res = mpu_accel_get_data_from_device(data, buffer);
	} else if (mldl_cfg->dmp_is_running &&
		   mldl_cfg->accel_is_suspended == 0) {

		if (from_mpu != NULL)
			*from_mpu = 1;

		/*
		   Retrieve accel data from MPU registers(0x23 to 0x28).
		 */
		res = mpu_accel_get_data_from_mpu(data, buffer);
	}

	return res;
}

static int mpu_accel_build_data(struct mpuaccel_data *data,
			const unsigned char *buffer, s16 *val)
{
	struct mldl_cfg *mldl_cfg = data->mldl_cfg;
	int endian = mldl_cfg->accel->endian;
	int dev_id = mldl_cfg->accel->id;

	if (endian == EXT_SLAVE_LITTLE_ENDIAN) {
		if (dev_id == ACCEL_ID_BMA150)
			*val = (*(s16 *)&buffer[0]) >> 6;
		else if (dev_id == ACCEL_ID_KXTF9) {
			*val =
			    (short)(((signed char)buffer[1] << 4) |
			    (buffer[0] >> 4));
		} else
			*val = (buffer[1] << 8) | buffer[0];
	} else if (endian == EXT_SLAVE_BIG_ENDIAN) {
		*val = (buffer[0] << 8) | buffer[1];
	}

	return ML_SUCCESS;
}

static void mpu_accel_input_work_func(struct work_struct *work)
{
	int res = 0;
	int poll_time = 0;
	int enable = 0;
	int i = 0;

	struct mpuaccel_data *data = container_of((struct delayed_work *)work,
						  struct mpuaccel_data, work);

	struct mldl_cfg *mldl_cfg = data->mldl_cfg;

	poll_time = atomic_read(&data->poll_delay);

	if (MPUACC_DEBUG)
		pr_info("________________START____________________\n");
	if (MPUACC_DEBUG_CFG)
		mpu_accel_print_mldl_cfg(mldl_cfg);

	mpu_accel_mutex_lock(data);
	enable = atomic_read(&data->enable);
	mpu_accel_mutex_unlock(data);

	if (enable) {
		unsigned char buffer[6] = { 0, };
		s16 raw[3] = { 0, };
		int data_is_avail = 0;
		int data_is_from_mpu = 0;

		mpu_accel_mutex_lock(data);
		mpu_accel_get_data(data, buffer, &data_is_from_mpu);
		mpu_accel_mutex_unlock(data);

		if (res == ML_SUCCESS)
			data_is_avail = 1;

		if (data_is_avail) {
			int data_is_valid = 0;

			for (i = 0; i < 3; i++) {
				mpu_accel_build_data(data, &buffer[i * 2],
						     &raw[i]);
			}
			raw[0] += cal_data.x;
			raw[1] += cal_data.y;
			raw[2] += cal_data.z;

			if (raw[0] && raw[1] && raw[2])
				data_is_valid = 1;

			if (data_is_valid) {
				int accel[3] = { 0, };

				/*apply mounting matrix */
				for (i = 0; i < 3; i++) {
#ifdef MPUACC_USES_MOUNTING_MATRIX
					int j = 0;
					for (j = 0; j < 3; j++) {
						accel[i] +=
						    mldl_cfg->pdata->accel.
						    orientation[i * 3 +
								j] * raw[j];
					}
#else
					accel[i] = raw[i];
#endif
				}

				if (MPUACC_DEBUG) {
					if (data_is_from_mpu == 1)
						pr_info
						 ("MPU_ACCEL:[%d][%d][%d]\n",
						  accel[0], accel[1],
						  accel[2]);
					else
						pr_info("ACCEL:[%d][%d][%d]\n",
							 accel[0], accel[1],
							 accel[2]);
				}
#ifdef MPUACC_USES_CACHED_DATA
				memcpy(data->cached_data, buffer,
						 sizeof(unsigned char) * 6);
#endif /* #ifdef MPUACC_USES_CACHED_DATA */
				input_report_rel(data->input_data, REL_X,
						 accel[0]);
				input_report_rel(data->input_data, REL_Y,
						 accel[1]);
				input_report_rel(data->input_data, REL_Z,
						 accel[2]);
				input_sync(data->input_data);

				if (MPUACC_DEBUG)
					pr_info("input device is updated\n");
			}
		}
	}

	if (MPUACC_DEBUG)
		pr_info("________________END____________________\n");

	mpu_accel_mutex_lock(data);
	enable = atomic_read(&data->enable);
	mpu_accel_mutex_unlock(data);

	if (enable) {
		if (poll_time > 0) {
			schedule_delayed_work(&data->work,
						msecs_to_jiffies(poll_time)
						/*+ 1 */);
		} else {
			schedule_delayed_work(&data->work, 0);
		}

	}

}

static int mpu_accel_enable(struct mpuaccel_data *data)
{
	int res = ML_SUCCESS;
	struct mldl_cfg *mldl_cfg = data->mldl_cfg;

	if (MPUACC_DEBUG)
		pr_info("mpu_accel_enable : %d\n", atomic_read(&data->enable));

	if (atomic_read(&data->enable) != 1) {

		if (MPUACC_DEBUG)
			pr_info("mpu_accel_enable : enabled\n");

		if (mldl_cfg->accel_is_suspended == 1) {
			if (MPUACC_DEBUG)
				pr_info("mpu_accel_enable : turn on accel\n");
			mpu_accel_activate_device(data, 1);
		}

		atomic_set(&data->enable, 1);
		schedule_delayed_work(&data->work, 0);

	}

	return res;
}

static int mpu_accel_disable(struct mpuaccel_data *data)
{
	int res = ML_SUCCESS;
	struct mldl_cfg *mldl_cfg = data->mldl_cfg;

	if (MPUACC_DEBUG)
		pr_info("mpu_accel_disable : %d\n", atomic_read(&data->enable));

	if (atomic_read(&data->enable) != 0) {
		atomic_set(&data->enable, 0);
		cancel_delayed_work(&data->work);

		if (MPUACC_DEBUG)
			pr_info("mpu_accel_disable : disabled\n");

		if (mldl_cfg->accel_is_suspended == 1) {
			if (MPUACC_DEBUG)
				pr_info("mpu_accel_disable : turn off accel\n");

			/*turn off accel */
			mpu_accel_activate_device(data, 0);
		}
	}

	return res;
}

static ssize_t mpu_accel_delay_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct input_dev *input_data = to_input_dev(dev);
	struct mpuaccel_data *data = input_get_drvdata(input_data);

	return sprintf(buf, "%d\n", atomic_read(&data->poll_delay));
}

static ssize_t mpu_accel_delay_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct input_dev *input_data = to_input_dev(dev);
	struct mpuaccel_data *data = input_get_drvdata(input_data);
	int value = simple_strtoul(buf, NULL, 10);

	atomic_set(&data->poll_delay, value);
	return count;
}

static ssize_t mpu_accel_enable_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct input_dev *input_data = to_input_dev(dev);
	struct mpuaccel_data *data = input_get_drvdata(input_data);

	return sprintf(buf, "%d\n", atomic_read(&data->enable));
}

static ssize_t
mpu_accel_enable_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct input_dev *input_data = to_input_dev(dev);
	struct mpuaccel_data *data = input_get_drvdata(input_data);
	int value;

	value = simple_strtoul(buf, NULL, 10);
	if (value != 0 && value != 1)
		return count;

	mpu_accel_mutex_lock(data);

	if (value)
		mpu_accel_enable(data);
	else
		mpu_accel_disable(data);

	mpu_accel_mutex_unlock(data);

	return count;
}

int mpu_accel_is_active_device(void)
{
	int is_active = 0;

	if (pThisData != NULL) {
		mpu_accel_mutex_lock(pThisData);
		is_active = pThisData->device_is_on;
		mpu_accel_mutex_unlock(pThisData);
	}

	return is_active;
}

#ifdef MPUACC_USES_CACHED_DATA
int mpu_accel_get_cached_data(unsigned char *cache)
{
	int res = ML_ERROR;

	if (pThisData != NULL) {
		if (pThisData->device_is_on == 1) {
			memcpy(cache, pThisData->cached_data,
					 sizeof(unsigned char) * 6);
			pr_info("cached data:[%d][%d][%d][%d][%d][%d]\n",
				cache[0], cache[1],
				cache[2], cache[3],
				cache[4], cache[5]);
			res = ML_SUCCESS;
		}

	}

	return res;
}
#endif /* MPUACC_USES_CACHED_DATA */

static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
			mpu_accel_delay_show, mpu_accel_delay_store);
static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
			mpu_accel_enable_show, mpu_accel_enable_store);

static struct attribute *mpuaccel_attributes[] = {
	&dev_attr_poll_delay.attr,
	&dev_attr_enable.attr,
	NULL
};

static struct attribute_group mpuaccel_attribute_group = {
	.attrs = mpuaccel_attributes
};

int mpu_accel_init(struct mldl_cfg *mldl_cfg, void *accel_handle)
{
	struct input_dev *input_data = NULL;
	struct mpuaccel_data *data = NULL;
	int res = 0;

	data = kzalloc(sizeof(struct mpuaccel_data), GFP_KERNEL);
	if (data == NULL) {
		res = -ENOMEM;
		goto err;
	}

	data->mldl_cfg = mldl_cfg;
	data->accel_handle = accel_handle;
	atomic_set(&data->enable, 0);
	atomic_set(&data->poll_delay, 20);	/* set 20ms to polling time */

	mutex_init(&data->data_mutex);

	INIT_DELAYED_WORK(&data->work, mpu_accel_input_work_func);

	input_data = input_allocate_device();
	if (input_data == NULL) {
		res = -ENOMEM;
		pr_err(
				 "mpu_accel_probe: Failed to allocate input_data device\n");
		goto err;
	}

	input_data->name = MPUACCEL_INPUT_NAME;
	input_data->id.bustype = BUS_I2C;

	set_bit(EV_REL, input_data->evbit);
	input_set_capability(input_data, EV_REL, REL_X);
	input_set_capability(input_data, EV_REL, REL_Y);
	input_set_capability(input_data, EV_REL, REL_Z);

	data->input_data = input_data;

	res = input_register_device(input_data);
	if (res) {
		pr_err(
				 "mpu_accel_init: Unable to register input_data device: %s\n",
				 input_data->name);
		goto err;
	}

	input_set_drvdata(input_data, data);
	mldl_cfg->ext.mpuacc_data = (void *)data;

	pThisData = data;

	res = sysfs_create_group(&input_data->dev.kobj,
				 &mpuaccel_attribute_group);
	if (res) {
		pr_err(
				 "mpu_accel_init: sysfs_create_group failed[%s]\n",
				 input_data->name);
		goto err;
	}

	return res;

err:
	sysfs_remove_group(&input_data->dev.kobj, &mpuaccel_attribute_group);
	input_free_device(input_data);
	kfree(data);
	return res;

}

int mpu_accel_exit(struct mldl_cfg *mldl_cfg)
{
	struct mpuaccel_data *data = NULL;

	if (mldl_cfg == NULL)
		return ML_ERROR;

	data = (struct mpuaccel_data *)mldl_cfg->ext.mpuacc_data;

	if (data != NULL) {
		sysfs_remove_group(&(data->input_data->dev.kobj),
					&mpuaccel_attribute_group);
		input_free_device(data->input_data);

		kfree(data);
		data = NULL;

		mldl_cfg->ext.mpuacc_data = NULL;
	}

	return ML_SUCCESS;
}

int mpu_accel_suspend(struct mldl_cfg *mldl_cfg)
{
	int result = ML_SUCCESS;
	int enable = 0;
	struct mpuaccel_data *data = NULL;

	if (mldl_cfg == NULL)
		return ML_ERROR;

	data = (struct mpuaccel_data *)mldl_cfg->ext.mpuacc_data;

	mpu_accel_mutex_lock(data);
	enable = atomic_read(&data->enable);

	pr_info("%s: device_is_on = %d, enable = %d\n",
		__func__, data->device_is_on, enable);

	if (data->device_is_on == 1 && enable == 0) {
		pr_info("%s: mpu_accel_activate_device 0\n", __func__);
		result = mpu_accel_activate_device(data, 0);
	}

	mpu_accel_mutex_unlock(data);

	return result;
}

int mpu_accel_resume(struct mldl_cfg *mldl_cfg)
{
	int result = ML_SUCCESS;
	int enable = 0;
	struct mpuaccel_data *data = NULL;

	if (mldl_cfg == NULL)
		return ML_ERROR;

	data = (struct mpuaccel_data *)mldl_cfg->ext.mpuacc_data;

	mpu_accel_mutex_lock(data);
	enable = atomic_read(&data->enable);

	pr_info("%s: device_is_on = %d, enable = %d\n",
		__func__, data->device_is_on, enable);

	if (data->device_is_on == 0 && enable == 0) {
		pr_info("%s: mpu_accel_activate_device 1\n", __func__);
		result = mpu_accel_activate_device(data, 1);
	}

	mpu_accel_mutex_unlock(data);

	return result;
}

int mpu_accel_read(struct mldl_cfg *mldl_cfg, unsigned char *buffer)
{
	int result = ML_SUCCESS;
	int enable = 0;
	struct mpuaccel_data *data = NULL;

	if (mldl_cfg == NULL)
		return ML_ERROR;

	data = (struct mpuaccel_data *)mldl_cfg->ext.mpuacc_data;

	mpu_accel_mutex_lock(data);
	enable = atomic_read(&data->enable);
#ifdef MPUACC_USES_CACHED_DATA
	if (enable == 1)
		memcpy(buffer, data->cached_data, sizeof(unsigned char) * 6);
	else
#endif /* MPUACC_USES_CACHED_DATA */
		result = mpu_accel_get_data_from_device(data, buffer);
	mpu_accel_mutex_unlock(data);

	return result;
}