aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/misc/bma023.c
blob: 09c2e0b0b0d55c73505e439cd5df6a923a0e15b9 (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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
/*
 * BMA023 accelerometer driver
 *
 * 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/delay.h>
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/types.h>
#include <linux/workqueue.h>
#include <linux/slab.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>

#define BMA023_NAME "bma023"

/* Default parameters */
#define BMA023_DEFAULT_DELAY            100
#define BMA023_MAX_DELAY                2000

/* Registers */
#define BMA023_CHIP_ID_REG              0x00
#define BMA023_CHIP_ID                  0x02

#define BMA023_AL_VERSION_REG           0x01
#define BMA023_AL_VERSION_MASK          0x0f
#define BMA023_AL_VERSION_SHIFT         0

#define BMA023_ML_VERSION_REG           0x01
#define BMA023_ML_VERSION_MASK          0xf0
#define BMA023_ML_VERSION_SHIFT         4

#define BMA023_ACC_REG                  0x02

#define BMA023_SOFT_RESET_REG           0x0a
#define BMA023_SOFT_RESET_MASK          0x02
#define BMA023_SOFT_RESET_SHIFT         1

#define BMA023_SLEEP_REG                0x0a
#define BMA023_SLEEP_MASK               0x01
#define BMA023_SLEEP_SHIFT              0

#define BMA023_RANGE_REG                0x14
#define BMA023_RANGE_MASK               0x18
#define BMA023_RANGE_SHIFT              3
#define BMA023_RANGE_2G                 0
#define BMA023_RANGE_4G                 1
#define BMA023_RANGE_8G                 2

#define BMA023_BANDWIDTH_REG            0x14
#define BMA023_BANDWIDTH_MASK           0x07
#define BMA023_BANDWIDTH_SHIFT          0
#define BMA023_BANDWIDTH_25HZ           0
#define BMA023_BANDWIDTH_50HZ           1
#define BMA023_BANDWIDTH_100HZ          2
#define BMA023_BANDWIDTH_190HZ          3
#define BMA023_BANDWIDTH_375HZ          4
#define BMA023_BANDWIDTH_750HZ          5
#define BMA023_BANDWIDTH_1500HZ         6

#define BMA023_SHADOW_DIS_REG           0x15
#define BMA023_SHADOW_DIS_MASK          0x08
#define BMA023_SHADOW_DIS_SHIFT         3

#define BMA023_WAKE_UP_PAUSE_REG        0x15
#define BMA023_WAKE_UP_PAUSE_MASK       0x06
#define BMA023_WAKE_UP_PAUSE_SHIFT      1

#define BMA023_WAKE_UP_REG              0x15
#define BMA023_WAKE_UP_MASK             0x01
#define BMA023_WAKE_UP_SHIFT            0

/* filter setting */
#define BMA023_FILTER_LEN		8
#define BMA023_STABLE_TH		5

/* ioctl commnad */
#define DCM_IOC_MAGIC			's'
#define BMA023_CALIBRATION		_IOWR(DCM_IOC_MAGIC, 48, short)

static DEFINE_MUTEX(bma023_mutex);

/* Acceleration measurement */
struct acceleration {
	short x;
	short y;
	short z;
};

/* Output data rate  */
struct bma023_odr {
	unsigned long delay;	/* min delay (msec) in the range of ODR */
	u8 odr;			/* bandwidth register value */
};

static const struct bma023_odr bma023_odr_table[] = {
/*	{  1, BMA023_BANDWIDTH_1500HZ }, */
	{  2, BMA023_BANDWIDTH_750HZ },
	{  3, BMA023_BANDWIDTH_375HZ },
	{  6, BMA023_BANDWIDTH_190HZ },
	{ 10, BMA023_BANDWIDTH_100HZ },
	{ 20, BMA023_BANDWIDTH_50HZ },
	{ 40, BMA023_BANDWIDTH_25HZ },
};

struct bma023_fir_filter {
	int num;
	int filter_len;
	int index;
	int32_t sequence[BMA023_FILTER_LEN];
};

/* driver private data */
struct bma023_data {
	atomic_t enable;                /* attribute value */
	atomic_t delay;                 /* attribute value */
	struct mutex enable_mutex;
	struct mutex data_mutex;
	struct i2c_client *client;
	struct input_dev *input;
	struct delayed_work work;
	struct miscdevice bma023_device;
	struct bma023_fir_filter filter[3];
};

#define delay_to_jiffies(d) ((d) ? msecs_to_jiffies(d) : 1)
#define actual_delay(d)     (jiffies_to_msecs(delay_to_jiffies(d)))

/* register access functions */
#define bma023_read_bits(p, r) \
	((i2c_smbus_read_byte_data((p)->client, r##_REG) \
	& r##_MASK) >> r##_SHIFT)

#define bma023_update_bits(p, r, v) \
	i2c_smbus_write_byte_data((p)->client, r##_REG, \
	((i2c_smbus_read_byte_data((p)->client, r##_REG) \
	& ~r##_MASK) | ((v) << r##_SHIFT)))

static void fir_filter_init(struct bma023_fir_filter *filter, int len)
{
	int i;

	filter->num = 0;
	filter->index = 0;
	filter->filter_len = len;

	for (i = 0; i < filter->filter_len; ++i)
		filter->sequence[i] = 0;
}

static s16 fir_filter_filter(struct bma023_fir_filter *filter, s16 in)
{
	int out = 0;
	int i;

	if (filter->filter_len == 0)
		return in;
	if (filter->num < filter->filter_len) {
		filter->sequence[filter->index++] = in;
		filter->num++;
		return in;
	} else {
		if (filter->filter_len <= filter->index)
			filter->index = 0;
		filter->sequence[filter->index++] = in;

		for (i = 0; i < filter->filter_len; i++)
			out += filter->sequence[i];
		return out / filter->filter_len;
	}
}

static void filter_init(struct bma023_data *bma023)
{
	int i;

	for (i = 0; i < 3; i++)
		fir_filter_init(&bma023->filter[i], BMA023_FILTER_LEN);
}

static void filter_filter(struct bma023_data *bma023, s16 *orig, s16 *filtered)
{
	int i;

	for (i = 0; i < 3; i++)
		filtered[i] = fir_filter_filter(&bma023->filter[i], orig[i]);
}

static void filter_stabilizer(struct bma023_data *bma023,
					s16 *orig, s16 *stabled)
{
	int i;
	static s16 buffer[3] = { 0, };

	for (i = 0; i < 3; i++) {
		if ((buffer[i] - orig[i] >= BMA023_STABLE_TH)
			|| (buffer[i] - orig[i] <= -BMA023_STABLE_TH)) {
			stabled[i] = orig[i];
			buffer[i] = stabled[i];
		} else
			stabled[i] = buffer[i];
	}
}

/* Device dependant operations */
static int bma023_power_up(struct bma023_data *bma023)
{
	bma023_update_bits(bma023, BMA023_SLEEP, 0);
	/* wait 1ms for wake-up time from sleep to operational mode */
	msleep(1);
	return 0;
}

static int bma023_power_down(struct bma023_data *bma023)
{
	bma023_update_bits(bma023, BMA023_SLEEP, 1);
	return 0;
}

static int bma023_hw_init(struct bma023_data *bma023)
{
	/* reset hardware */
	bma023_power_up(bma023);
	i2c_smbus_write_byte_data(bma023->client,
			  BMA023_SOFT_RESET_REG, BMA023_SOFT_RESET_MASK);

	/* wait 10us after soft_reset to start I2C transaction */
	msleep(1);

	bma023_update_bits(bma023, BMA023_RANGE, BMA023_RANGE_2G);
	bma023_power_down(bma023);

	return 0;
}

static int bma023_get_enable(struct device *dev)
{
	struct bma023_data *bma023 = dev_get_drvdata(dev);
	return atomic_read(&bma023->enable);
}

static void bma023_set_enable(struct device *dev, int enable)
{
	struct bma023_data *bma023 = dev_get_drvdata(dev);
	int delay = atomic_read(&bma023->delay);

	mutex_lock(&bma023->enable_mutex);
	if (enable) { /* enable if state will be changed */
		if (!atomic_cmpxchg(&bma023->enable, 0, 1)) {
			bma023_power_up(bma023);
			schedule_delayed_work(&bma023->work,
					      delay_to_jiffies(delay) + 1);
		}
	} else { /* disable if state will be changed */
		if (atomic_cmpxchg(&bma023->enable, 1, 0)) {
			cancel_delayed_work_sync(&bma023->work);
			bma023_power_down(bma023);
		}
	}
	atomic_set(&bma023->enable, enable);
	mutex_unlock(&bma023->enable_mutex);
}

static int bma023_get_delay(struct device *dev)
{
	struct bma023_data *bma023 = dev_get_drvdata(dev);
	return atomic_read(&bma023->delay);
}

static void bma023_set_delay(struct device *dev, int delay)
{
	struct bma023_data *bma023 = dev_get_drvdata(dev);
	int i;
	u8 odr;

	/* determine optimum ODR */
	for (i = 1; (i < ARRAY_SIZE(bma023_odr_table)) &&
		     (actual_delay(delay) >= bma023_odr_table[i].delay); i++)
		;
	odr = bma023_odr_table[i-1].odr;
	atomic_set(&bma023->delay, delay);

	mutex_lock(&bma023->enable_mutex);
	if (bma023_get_enable(dev)) {
		cancel_delayed_work_sync(&bma023->work);
		bma023_update_bits(bma023, BMA023_BANDWIDTH, odr);
		schedule_delayed_work(&bma023->work,
				      delay_to_jiffies(delay) + 1);
	} else {
		bma023_power_up(bma023);
		bma023_update_bits(bma023, BMA023_BANDWIDTH, odr);
		bma023_power_down(bma023);
	}
	mutex_unlock(&bma023->enable_mutex);
}

static int bma023_measure(struct bma023_data *bma023,
				struct acceleration *accel)
{
	struct i2c_client *client = bma023->client;
	int err;
	int i;
	s16 raw_data[3];
	s16 filtered_data[3];
	s16 stabled_data[3];
	u8 buf[6];

	/* read acceleration raw data */
	err = i2c_smbus_read_i2c_block_data(client,
			BMA023_ACC_REG, sizeof(buf), buf) != sizeof(buf);
	if (err < 0) {
		pr_err("%s: i2c read fail addr=0x%02x, len=%d\n",
			__func__, BMA023_ACC_REG, sizeof(buf));
		for (i = 0; i < 3; i++)
			raw_data[i] = 0;
	} else
		for (i = 0; i < 3; i++)
			raw_data[i] = (*(s16 *)&buf[i*2]) >> 6;

	/* filter out sizzling values */
	/* filter_filter(bma023, raw_data, filtered_data); */
	/* filter_stabilizer(bma023, filtered_data, stabled_data); */
	filter_stabilizer(bma023, raw_data, stabled_data);

	accel->x = stabled_data[0];
	accel->y = stabled_data[1];
	accel->z = stabled_data[2];

	return err;
}

static void bma023_work_func(struct work_struct *work)
{
	struct bma023_data *bma023 = container_of((struct delayed_work *)work,
						  struct bma023_data, work);
	struct acceleration accel;
	unsigned long delay = delay_to_jiffies(atomic_read(&bma023->delay));
	bma023_measure(bma023, &accel);
	
#if defined(CONFIG_SAMSUNG_CAPTIVATE)	
	input_report_rel(bma023->input, REL_X, (-(accel.y)));
	input_report_rel(bma023->input, REL_Y, accel.x);
	input_report_rel(bma023->input, REL_Z, accel.z);
#else
	input_report_rel(bma023->input, REL_X, accel.x);
	input_report_rel(bma023->input, REL_Y, accel.y);
	input_report_rel(bma023->input, REL_Z, accel.z);
#endif
	input_sync(bma023->input);
	schedule_delayed_work(&bma023->work, delay);
}

/* Input device interface */
static int bma023_input_init(struct bma023_data *bma023)
{
	struct input_dev *dev;
	int err;

	dev = input_allocate_device();
	if (!dev)
		return -ENOMEM;
	dev->name = "accelerometer_sensor";
	dev->id.bustype = BUS_I2C;

	input_set_capability(dev, EV_REL, REL_RY);
	input_set_capability(dev, EV_REL, REL_X);
	input_set_capability(dev, EV_REL, REL_Y);
	input_set_capability(dev, EV_REL, REL_Z);
	input_set_drvdata(dev, bma023);

	err = input_register_device(dev);
	if (err < 0) {
		input_free_device(dev);
		return err;
	}
	bma023->input = dev;

	return 0;
}

static void bma023_input_fini(struct bma023_data *bma023)
{
	struct input_dev *dev = bma023->input;

	input_unregister_device(dev);
	input_free_device(dev);
}

/* sysfs device attributes */
static ssize_t bma023_enable_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", bma023_get_enable(dev));
}

static ssize_t bma023_enable_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count)
{
	int err;
	unsigned long enable;
	err = strict_strtoul(buf, 10, &enable);

	if ((enable == 0) || (enable == 1))
		bma023_set_enable(dev, enable);

	return count;
}

static ssize_t bma023_delay_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", bma023_get_delay(dev));
}

static ssize_t bma023_delay_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	int err;
	long delay;

	err = strict_strtoul(buf, 10, &delay);
	if (err < 0)
		return count;

	if (delay > BMA023_MAX_DELAY)
		delay = BMA023_MAX_DELAY;

	bma023_set_delay(dev, delay);

	return count;
}

static ssize_t bma023_wake_store(struct device *dev,
				 struct device_attribute *attr,
				 const char *buf, size_t count)
{
	struct input_dev *input = to_input_dev(dev);
	static atomic_t serial = ATOMIC_INIT(0);

	input_report_rel(input, REL_RY, atomic_inc_return(&serial));

	return count;
}

static ssize_t bma023_data_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct input_dev *input = to_input_dev(dev);
	struct bma023_data *bma023 = input_get_drvdata(input);
	struct acceleration accel;
	int on;

	mutex_lock(&bma023->data_mutex);
	on = bma023_get_enable(dev);
	if (!on)
		bma023_set_enable(dev, 1);
	bma023_measure(bma023, &accel);
	if (!on)
		bma023_set_enable(dev, 0);
	mutex_unlock(&bma023->data_mutex);

	return sprintf(buf, "%d,%d,%d\n", accel.x, accel.y, accel.z);
}

static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR|S_IWGRP,
		   bma023_enable_show, bma023_enable_store);
static DEVICE_ATTR(delay, S_IRUGO|S_IWUSR|S_IWGRP,
		   bma023_delay_show, bma023_delay_store);
static DEVICE_ATTR(wake, S_IWUSR|S_IWGRP,
		   NULL, bma023_wake_store);
static DEVICE_ATTR(data, S_IRUGO,
		   bma023_data_show, NULL);

static struct attribute *bma023_attributes[] = {
	&dev_attr_enable.attr,
	&dev_attr_delay.attr,
	&dev_attr_wake.attr,
	&dev_attr_data.attr,
	NULL
};

static struct attribute_group bma023_attribute_group = {
	.attrs = bma023_attributes
};

static int bma023_detect(struct i2c_client *client, struct i2c_board_info *info)
{
	int id;

	id = i2c_smbus_read_byte_data(client, BMA023_CHIP_ID_REG);
	if (id != BMA023_CHIP_ID)
		return -ENODEV;

	return 0;
}

int bma023_get_offset(struct bma023_data *bma023,
			unsigned char xyz, unsigned short *offset)
{
	unsigned char data;

	data = i2c_smbus_read_byte_data(bma023->client, 0x16 + xyz);
	data = (data & 0xC0) >> 6;

	*offset = data;
	data = i2c_smbus_read_byte_data(bma023->client, 0x1A + xyz);
	*offset |= (data << 2);

	return 0;
}

int bma023_set_offset(struct bma023_data *bma023,
			unsigned char xyz, unsigned short offset)
{
	int err;
	unsigned char data;

	data = i2c_smbus_read_byte_data(bma023->client, 0x16 + xyz);
	data = (data & ~0xC0) | ((offset << 6) & 0xC0);

	err = i2c_smbus_write_byte_data(bma023->client, 0x16 + xyz, data);
	data = (offset & 0x3ff) >> 2;
	err = i2c_smbus_write_byte_data(bma023->client, 0x1A + xyz, data);

	return err;
}

int bma023_set_offset_eeprom(struct bma023_data *bma023,
		unsigned char xyz, unsigned short offset)
{
	int err;
	unsigned char data;

	data = i2c_smbus_read_byte_data(bma023->client, 0x16 + xyz);
	data = (data & ~0xC0) | ((offset << 6) & 0xC0);

	data = i2c_smbus_write_byte_data(bma023->client,
					0x20 + 0x16 + xyz, data);
	msleep(34);

	data = (offset&0x3ff)>>2;
	err = i2c_smbus_write_byte_data(bma023->client,
					0x20 + 0x1A + xyz, data);
	msleep(34);

	return err;
}

int bma023_set_ee_w(struct bma023_data *bma023, unsigned char eew)
{
	unsigned char data;
	int err;

	data = i2c_smbus_read_byte_data(bma023->client, 0x0A);
	data = (data & ~0x10) | ((eew<<4)&0x10);
	err = i2c_smbus_write_byte_data(bma023->client, 0x0A, data);
	return err;
}

int bma023_calc_new_offset(struct acceleration orientation,
				struct acceleration accel,
				unsigned short *offset_x,
				unsigned short *offset_y,
				unsigned short *offset_z)
{
	short old_offset_x, old_offset_y, old_offset_z;
	short new_offset_x, new_offset_y, new_offset_z;
	unsigned char  calibrated = 0;

	old_offset_x = *offset_x;
	old_offset_y = *offset_y;
	old_offset_z = *offset_z;

	accel.x = accel.x - (orientation.x * 256);
	accel.y = accel.y - (orientation.y * 256);
	accel.z = accel.z - (orientation.z * 256);

	/* does x axis need calibration? */
	if ((accel.x > 4) | (accel.x < -4)) {
		/* check for values less than quantisation of offset register */
		if ((accel.x < 8) && accel.x > 0)
			new_offset_x = old_offset_x - 1;
		else if ((accel.x > -8) && (accel.x < 0))
			new_offset_x = old_offset_x + 1;
		else
			/* calculate new offset due to formula */
			new_offset_x = old_offset_x - (accel.x / 8);
		/* check for register boundary */
		if (new_offset_x < 0)
			new_offset_x = 0;
		else if (new_offset_x > 1023)
			new_offset_x = 1023;
		*offset_x = new_offset_x;
		calibrated = 1;
	}

	/* does y axis need calibration? */
	if ((accel.y > 4) | (accel.y < -4)) {
		/* check for values less than quantisation of offset register */
		if ((accel.y < 8) && accel.y > 0)
			new_offset_y = old_offset_y - 1;
		else if ((accel.y > -8) && accel.y < 0)
			new_offset_y = old_offset_y + 1;
		else
			/* calculate new offset due to formula */
			new_offset_y = old_offset_y - accel.y / 8;
		/* check for register boundary */
		if (new_offset_y < 0)
			new_offset_y = 0;
		else if (new_offset_y > 1023)
			new_offset_y = 1023;
		*offset_y = new_offset_y;
		calibrated = 1;
	}

	/* does z axis need calibration? */
	if ((accel.z > 4) | (accel.z < -4)) {
		/* check for values less than quantisation of offset register */
		if ((accel.z < 8) && accel.z > 0)
			new_offset_z = old_offset_z - 1;
		else if ((accel.z > -8) && accel.z < 0)
			new_offset_z = old_offset_z + 1;
		else
			/* calculate new offset due to formula */
			new_offset_z = old_offset_z - (accel.z / 8);
		/* check for register boundary */
		if (new_offset_z < 0)
			new_offset_z = 0;
		else if (new_offset_z > 1023)
			new_offset_z = 1023;
		*offset_z = new_offset_z;
		calibrated = 1;
	}
	return calibrated;
}

/* reads out acceleration data and averages them, measures min and max
 * \param orientation pass orientation one axis needs to be absolute 1
 * the others need to be 0
 * \param num_avg numer of samples for averaging
 * \param *min returns the minimum measured value
 * \param *max returns the maximum measured value
 * \param *average returns the average value
 */

int bma023_read_accel_avg(struct bma023_data *bma023, int num_avg,
					struct acceleration *min,
					struct acceleration *max,
					struct acceleration *avg)
{
	long x_avg = 0, y_avg = 0, z_avg = 0;
	int comres = 0;
	int i;
	struct acceleration accel;

	x_avg = 0; y_avg = 0; z_avg = 0;
	max->x = -512; max->y = -512; max->z = -512;
	min->x = 512;  min->y = 512; min->z = 512;

	for (i = 0; i < num_avg; i++) {
		/* read 10 acceleration data triples */
		comres += bma023_measure(bma023, &accel);
		if (accel.x > max->x)
			max->x = accel.x;
		if (accel.x < min->x)
			min->x = accel.x;

		if (accel.y > max->y)
			max->y = accel.y;
		if (accel.y < min->y)
			min->y = accel.y;

		if (accel.z > max->z)
			max->z = accel.z;
		if (accel.z < min->z)
			min->z = accel.z;

		x_avg += accel.x;
		y_avg += accel.y;
		z_avg += accel.z;
		msleep(10);
	}
	/* calculate averages, min and max values */
	avg->x = x_avg /= num_avg;
	avg->y = y_avg /= num_avg;
	avg->z = z_avg /= num_avg;
	return comres;
}

/* verifies the accerleration values to be good enough
 * for calibration calculations
 * \param min takes the minimum measured value
 * \param max takes the maximum measured value
 * \param takes returns the average value
 * \return 1: min,max values are in range, 0: not in range
 */

int bma023_verify_min_max(struct acceleration min, struct acceleration max,
						struct acceleration avg)
{
	short dx, dy, dz;
	int ver_ok = 1;

	/* calc delta max-min */
	dx =  max.x - min.x;
	dy =  max.y - min.y;
	dz =  max.z - min.z;

	if (dx > 10 || dx < -10)
		ver_ok = 0;
	if (dy > 10 || dy < -10)
		ver_ok = 0;
	if (dz > 10 || dz < -10)
		ver_ok = 0;

	return ver_ok;
}

/* overall calibration process. This function
 * takes care about all other functions
 * \param orientation input for orientation [0;0;1] for measuring the device
 * in horizontal surface up
 * \param *tries takes the number of wanted iteration steps, this pointer
 * returns the number of calculated steps after this routine has finished
 * \return 1: calibration passed 2: did not pass within N steps
 */

int bma023_calibrate(struct bma023_data *bma023,
			struct acceleration orientation, int *tries)
{
	struct acceleration min, max, avg;
	unsigned short offset_x, offset_y, offset_z;
	unsigned short old_offset_x, old_offset_y, old_offset_z;
	int need_calibration = 0, min_max_ok = 0;
	int ltries;
	int retry = 30;
	int on;

	on = bma023_get_enable(&bma023->client->dev);
	if (!on)
		bma023_set_enable(&bma023->client->dev, 1);

	bma023_update_bits(bma023, BMA023_RANGE, BMA023_RANGE_2G);
	bma023_update_bits(bma023, BMA023_BANDWIDTH, BMA023_BANDWIDTH_25HZ);
	bma023_set_ee_w(bma023, 1);

	bma023_get_offset(bma023, 0, &offset_x);
	bma023_get_offset(bma023, 1, &offset_y);
	bma023_get_offset(bma023, 2, &offset_z);

	old_offset_x = offset_x;
	old_offset_y = offset_y;
	old_offset_z = offset_z;
	ltries = *tries;

	do {
		/* read acceleration data min, max, avg */
		bma023_read_accel_avg(bma023, 10, &min, &max, &avg);
		min_max_ok = bma023_verify_min_max(min, max, avg);

		if (!min_max_ok) {
			retry--;
			if (retry <= 0)
				return -1;
		}
		/* check if calibration is needed */
		if (min_max_ok)
			need_calibration
				= bma023_calc_new_offset(orientation, avg,
					&offset_x, &offset_y, &offset_z);

		if (*tries == 0) /*number of maximum tries reached? */
			break;

		if (need_calibration) {
			/* when needed calibration is updated in image */
			(*tries)--;
			bma023_set_offset(bma023, 0, offset_x);
			bma023_set_offset(bma023, 1, offset_y);
			bma023_set_offset(bma023, 2, offset_z);
			msleep(20);
		}
	} while (need_calibration || !min_max_ok);

	if (*tries > 0 && *tries < ltries) {
		if (old_offset_x != offset_x)
			bma023_set_offset_eeprom(bma023, 0, offset_x);
		if (old_offset_y != offset_y)
			bma023_set_offset_eeprom(bma023, 1, offset_y);
		if (old_offset_z != offset_z)
			bma023_set_offset_eeprom(bma023, 2, offset_z);
	}

	bma023_set_ee_w(bma023, 0);
	msleep(20);
	*tries = ltries - *tries;

	if (!on)
		bma023_set_enable(&bma023->client->dev, 0);

	return !need_calibration;
}

static int bma023_open(struct inode *inode, struct file *file)
{
	struct bma023_data *bma023 = container_of(file->private_data,
						struct bma023_data,
						bma023_device);
	file->private_data = bma023;
	return 0;
}

static int bma023_close(struct inode *inode, struct file *file)
{
	return 0;
}

static int bma023_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct bma023_data *bma023 = file->private_data;
	int try;
	int err = 0;
	unsigned char data[6];

	mutex_lock(&bma023_mutex);

	switch (cmd) {
	case BMA023_CALIBRATION:
		if (copy_from_user((struct acceleration *)data,
			(struct acceleration *)arg, 6) != 0) {
			pr_err("copy_from_user error\n");
			return -EFAULT;
		}
		/* iteration time = 20 */
		try = 20;
		err = bma023_calibrate(bma023,
				*(struct acceleration *)data, &try);
		break;
	default:
		break;
	}

	mutex_unlock(&bma023_mutex);

	return 0;
}

static const struct file_operations bma023_fops = {
	.owner = THIS_MODULE,
	.open = bma023_open,
	.release = bma023_close,
	.unlocked_ioctl = bma023_ioctl,
};

static int bma023_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct bma023_data *bma023;
	int err;

	/* setup private data */
	bma023 = kzalloc(sizeof(struct bma023_data), GFP_KERNEL);
	if (!bma023)
		return -ENOMEM;
	mutex_init(&bma023->enable_mutex);
	mutex_init(&bma023->data_mutex);

	/* setup i2c client */
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		err = -ENODEV;
		goto err_i2c_fail;
	}
	i2c_set_clientdata(client, bma023);
	bma023->client = client;

	/* detect and init hardware */
	err = bma023_detect(client, NULL);
	if (err)
		goto err_id_read;
	dev_info(&client->dev, "%s found\n", id->name);
	dev_info(&client->dev, "al_version=%d, ml_version=%d\n",
		 bma023_read_bits(bma023, BMA023_AL_VERSION),
		 bma023_read_bits(bma023, BMA023_ML_VERSION));

	bma023_hw_init(bma023);
	bma023_set_delay(&client->dev, BMA023_DEFAULT_DELAY);

	/* setup driver interfaces */
	INIT_DELAYED_WORK(&bma023->work, bma023_work_func);

	err = bma023_input_init(bma023);
	if (err < 0)
		goto err_input_allocate;

	err = sysfs_create_group(&bma023->input->dev.kobj,
					&bma023_attribute_group);
	if (err < 0)
		goto err_sys_create;

	bma023->bma023_device.minor = MISC_DYNAMIC_MINOR;
	bma023->bma023_device.name = "accelerometer";
	bma023->bma023_device.fops = &bma023_fops;

	err = misc_register(&bma023->bma023_device);
	if (err) {
		pr_err("%s: misc_register failed\n", __FILE__);
		goto err_misc_register;
	}

	/* filter init */
	filter_init(bma023);

	return 0;

err_misc_register:
	sysfs_remove_group(&bma023->input->dev.kobj,
				&bma023_attribute_group);
err_sys_create:
	bma023_input_fini(bma023);
err_input_allocate:
err_id_read:
err_i2c_fail:
	kfree(bma023);
	return err;
}

static int bma023_remove(struct i2c_client *client)
{
	struct bma023_data *bma023 = i2c_get_clientdata(client);

	bma023_set_enable(&client->dev, 0);

	sysfs_remove_group(&bma023->input->dev.kobj, &bma023_attribute_group);
	bma023_input_fini(bma023);
	kfree(bma023);

	return 0;
}

static int bma023_suspend(struct device *dev)
{
	struct bma023_data *bma023 = dev_get_drvdata(dev);

	mutex_lock(&bma023->enable_mutex);
	if (bma023_get_enable(dev)) {
		cancel_delayed_work_sync(&bma023->work);
		bma023_power_down(bma023);
	}
	mutex_unlock(&bma023->enable_mutex);

	return 0;
}

static int bma023_resume(struct device *dev)
{
	struct bma023_data *bma023 = dev_get_drvdata(dev);
	int delay = atomic_read(&bma023->delay);

	bma023_hw_init(bma023);
	bma023_set_delay(dev, delay);

	mutex_lock(&bma023->enable_mutex);
	if (bma023_get_enable(dev)) {
		bma023_power_up(bma023);
		schedule_delayed_work(&bma023->work,
				      delay_to_jiffies(delay) + 1);
	}
	mutex_unlock(&bma023->enable_mutex);

	return 0;
}

static const struct dev_pm_ops bma023_pm_ops = {
	.suspend = bma023_suspend,
	.resume = bma023_resume,
};

static const struct i2c_device_id bma023_id[] = {
	{BMA023_NAME, 0},
	{},
};

MODULE_DEVICE_TABLE(i2c, bma023_id);

struct i2c_driver bma023_driver = {
	.driver = {
		.name = "bma023",
		.owner = THIS_MODULE,
		.pm = &bma023_pm_ops,
	},
	.probe = bma023_probe,
	.remove = bma023_remove,
	.id_table = bma023_id,
};

static int __init bma023_init(void)
{
	return i2c_add_driver(&bma023_driver);
}
module_init(bma023_init);

static void __exit bma023_exit(void)
{
	i2c_del_driver(&bma023_driver);
}
module_exit(bma023_exit);

MODULE_DESCRIPTION("BMA023 accelerometer driver");
MODULE_AUTHOR("tim.sk.lee@samsung.com");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.0");