/
lis3dh_acc.c
1596 lines (1335 loc) · 39.3 KB
/
lis3dh_acc.c
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
/******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
*
* File Name : lis3dh_acc.c
* Authors : MSH - Motion Mems BU - Application Team
* : Matteo Dameno (matteo.dameno@st.com)
* : Carmine Iascone (carmine.iascone@st.com)
* : Samuel Huo (samuel.huo@st.com)
* Version : V.1.1.0
* Date : 07/10/2012
* Description : LIS3DH accelerometer sensor driver
*
*******************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* THE PRESENT SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, FOR THE SOLE
* PURPOSE TO SUPPORT YOUR APPLICATION DEVELOPMENT.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* THIS SOFTWARE IS SPECIFICALLY DESIGNED FOR EXCLUSIVE USE WITH ST PARTS.
*
******************************************************************************
Revision 1.0.0 05/11/09
First Release;
Revision 1.0.3 22/01/2010
Linux K&R Compliant Release;
Revision 1.0.5 16/08/2010
modified _get_acceleration_data function;
modified _update_odr function;
manages 2 interrupts;
Revision 1.0.6 15/11/2010
supports sysfs;
no more support for ioctl;
Revision 1.0.7 26/11/2010
checks for availability of interrupts pins
correction on FUZZ and FLAT values;
Revision 1.0.8 2010/Apr/01
corrects a bug in interrupt pin management in 1.0.7
Revision 1.0.9 07/25/2011
Romove several unused functions,add 5ms delay in init,change sysfs attributes.
Revision 1.1.0 07/10/2012
To replace some deprecated functions for 3.4 kernel;
To pass the checkpatch's formatting requirement;
To add regulator request;
******************************************************************************/
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/uaccess.h>
#include <linux/workqueue.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/pm.h>
#include <linux/input/lis3dh.h>
#include <linux/module.h>
#include <linux/regulator/consumer.h>
#define DEBUG 1
#define G_MAX 16000
#define SENSITIVITY_2G 1 /** mg/LSB */
#define SENSITIVITY_4G 2 /** mg/LSB */
#define SENSITIVITY_8G 4 /** mg/LSB */
#define SENSITIVITY_16G 12 /** mg/LSB */
/* Accelerometer Sensor Operating Mode */
#define LIS3DH_ACC_ENABLE 0x01
#define LIS3DH_ACC_DISABLE 0x00
#define HIGH_RESOLUTION 0x08
#define AXISDATA_REG 0x28
#define WHOAMI_LIS3DH_ACC 0x33 /* Expected content for WAI */
/* CONTROL REGISTERS */
#define WHO_AM_I 0x0F /* WhoAmI register */
#define TEMP_CFG_REG 0x1F /* temper sens control reg */
/* ctrl 1: ODR3 ODR2 ODR ODR0 LPen Zenable Yenable Zenable */
#define CTRL_REG1 0x20 /* control reg 1 */
#define CTRL_REG2 0x21 /* control reg 2 */
#define CTRL_REG3 0x22 /* control reg 3 */
#define CTRL_REG4 0x23 /* control reg 4 */
#define CTRL_REG5 0x24 /* control reg 5 */
#define CTRL_REG6 0x25 /* control reg 6 */
#define FIFO_CTRL_REG 0x2E /* FiFo control reg */
#define INT_CFG1 0x30 /* interrupt 1 config */
#define INT_SRC1 0x31 /* interrupt 1 source */
#define INT_THS1 0x32 /* interrupt 1 threshold */
#define INT_DUR1 0x33 /* interrupt 1 duration */
#define TT_CFG 0x38 /* tap config */
#define TT_SRC 0x39 /* tap source */
#define TT_THS 0x3A /* tap threshold */
#define TT_LIM 0x3B /* tap time limit */
#define TT_TLAT 0x3C /* tap time latency */
#define TT_TW 0x3D /* tap time window */
/* end CONTROL REGISTRES */
#define ENABLE_HIGH_RESOLUTION 1
#define LIS3DH_ACC_PM_OFF 0x00
#define LIS3DH_ACC_ENABLE_ALL_AXES 0x07
#define PMODE_MASK 0x08
#define ODR_MASK 0XF0
#define ODR1 0x10 /* 1Hz output data rate */
#define ODR10 0x20 /* 10Hz output data rate */
#define ODR25 0x30 /* 25Hz output data rate */
#define ODR50 0x40 /* 50Hz output data rate */
#define ODR100 0x50 /* 100Hz output data rate */
#define ODR200 0x60 /* 200Hz output data rate */
#define ODR400 0x70 /* 400Hz output data rate */
#define ODR1250 0x90 /* 1250Hz output data rate */
#define IA 0x40
#define ZH 0x20
#define ZL 0x10
#define YH 0x08
#define YL 0x04
#define XH 0x02
#define XL 0x01
/* */
/* CTRL REG BITS*/
#define CTRL_REG3_I1_AOI1 0x40
#define CTRL_REG6_I2_TAPEN 0x80
#define CTRL_REG6_HLACTIVE 0x02
/* */
#define NO_MASK 0xFF
#define INT1_DURATION_MASK 0x7F
#define INT1_THRESHOLD_MASK 0x7F
#define TAP_CFG_MASK 0x3F
#define TAP_THS_MASK 0x7F
#define TAP_TLIM_MASK 0x7F
#define TAP_TLAT_MASK NO_MASK
#define TAP_TW_MASK NO_MASK
/* TAP_SOURCE_REG BIT */
#define DTAP 0x20
#define STAP 0x10
#define SIGNTAP 0x08
#define ZTAP 0x04
#define YTAP 0x02
#define XTAZ 0x01
#define FUZZ 0
#define FLAT 0
#define I2C_RETRY_DELAY 5
#define I2C_RETRIES 5
#define I2C_AUTO_INCREMENT 0x80
/* RESUME STATE INDICES */
#define RES_CTRL_REG1 0
#define RES_CTRL_REG2 1
#define RES_CTRL_REG3 2
#define RES_CTRL_REG4 3
#define RES_CTRL_REG5 4
#define RES_CTRL_REG6 5
#define RES_INT_CFG1 6
#define RES_INT_THS1 7
#define RES_INT_DUR1 8
#define RES_TT_CFG 9
#define RES_TT_THS 10
#define RES_TT_LIM 11
#define RES_TT_TLAT 12
#define RES_TT_TW 13
#define RES_TEMP_CFG_REG 14
#define RES_REFERENCE_REG 15
#define RES_FIFO_CTRL_REG 16
#define RESUME_ENTRIES 17
/* end RESUME STATE INDICES */
struct {
unsigned int cutoff_ms;
unsigned int mask;
} lis3dh_acc_odr_table[] = {
{ 1, ODR1250 },
{ 3, ODR400 },
{ 5, ODR200 },
{ 10, ODR100 },
{ 20, ODR50 },
{ 40, ODR25 },
{ 100, ODR10 },
{ 1000, ODR1 },
};
struct lis3dh_acc_data {
struct i2c_client *client;
struct lis3dh_acc_platform_data *pdata;
struct mutex lock;
struct delayed_work input_work;
struct input_dev *input_dev;
int hw_initialized;
/* hw_working=-1 means not tested yet */
int hw_working;
atomic_t enabled;
int on_before_suspend;
u8 sensitivity;
u8 resume_state[RESUME_ENTRIES];
int irq1;
struct work_struct irq1_work;
struct workqueue_struct *irq1_work_queue;
int irq2;
struct work_struct irq2_work;
struct workqueue_struct *irq2_work_queue;
#ifdef DEBUG
u8 reg_addr;
#endif
};
struct sensor_regulator {
struct regulator *vreg;
const char *name;
u32 min_uV;
u32 max_uV;
};
struct sensor_regulator lis3dh_acc_vreg[] = {
{NULL, "vdd", 1700000, 3600000},
{NULL, "vddio", 1700000, 3600000},
};
static int lis3dh_acc_config_regulator(struct lis3dh_acc_data *acc, bool on)
{
int rc = 0, i;
int num_reg = sizeof(lis3dh_acc_vreg) / sizeof(struct sensor_regulator);
if (on) {
for (i = 0; i < num_reg; i++) {
lis3dh_acc_vreg[i].vreg =
regulator_get(&acc->client->dev,
lis3dh_acc_vreg[i].name);
if (IS_ERR(lis3dh_acc_vreg[i].vreg)) {
rc = PTR_ERR(lis3dh_acc_vreg[i].vreg);
pr_err("%s:regulator get failed rc=%d\n",
__func__, rc);
lis3dh_acc_vreg[i].vreg = NULL;
goto error_vdd;
}
if (regulator_count_voltages(
lis3dh_acc_vreg[i].vreg) > 0) {
rc = regulator_set_voltage(
lis3dh_acc_vreg[i].vreg,
lis3dh_acc_vreg[i].min_uV,
lis3dh_acc_vreg[i].max_uV);
if (rc) {
pr_err("%s: set voltage failed rc=%d\n",
__func__, rc);
regulator_put(lis3dh_acc_vreg[i].vreg);
lis3dh_acc_vreg[i].vreg = NULL;
goto error_vdd;
}
}
rc = regulator_enable(lis3dh_acc_vreg[i].vreg);
if (rc) {
pr_err("%s: regulator_enable failed rc =%d\n",
__func__, rc);
if (regulator_count_voltages(
lis3dh_acc_vreg[i].vreg) > 0) {
regulator_set_voltage(
lis3dh_acc_vreg[i].vreg, 0,
lis3dh_acc_vreg[i].max_uV);
}
regulator_put(lis3dh_acc_vreg[i].vreg);
lis3dh_acc_vreg[i].vreg = NULL;
goto error_vdd;
}
}
return rc;
} else {
i = num_reg;
}
error_vdd:
while (--i >= 0) {
if (!IS_ERR_OR_NULL(lis3dh_acc_vreg[i].vreg)) {
if (regulator_count_voltages(
lis3dh_acc_vreg[i].vreg) > 0) {
regulator_set_voltage(lis3dh_acc_vreg[i].vreg,
0, lis3dh_acc_vreg[i].max_uV);
}
regulator_disable(lis3dh_acc_vreg[i].vreg);
regulator_put(lis3dh_acc_vreg[i].vreg);
lis3dh_acc_vreg[i].vreg = NULL;
}
}
return rc;
}
static int lis3dh_acc_i2c_read(struct lis3dh_acc_data *acc,
u8 *buf, int len)
{
int err;
int tries = 0;
struct i2c_msg msgs[] = {
{
.addr = acc->client->addr,
.flags = acc->client->flags & I2C_M_TEN,
.len = 1,
.buf = buf,
},
{
.addr = acc->client->addr,
.flags = (acc->client->flags & I2C_M_TEN) | I2C_M_RD,
.len = len,
.buf = buf,
},
};
do {
err = i2c_transfer(acc->client->adapter, msgs, 2);
if (err != 2)
msleep_interruptible(I2C_RETRY_DELAY);
} while ((err != 2) && (++tries < I2C_RETRIES));
if (err != 2) {
dev_err(&acc->client->dev, "read transfer error\n");
err = -EIO;
} else {
err = 0;
}
return err;
}
static int lis3dh_acc_i2c_write(struct lis3dh_acc_data *acc, u8 *buf, int len)
{
int err;
int tries = 0;
struct i2c_msg msgs[] = {
{
.addr = acc->client->addr,
.flags = acc->client->flags & I2C_M_TEN,
.len = len + 1,
.buf = buf,
},
};
do {
err = i2c_transfer(acc->client->adapter, msgs, 1);
if (err != 1)
msleep_interruptible(I2C_RETRY_DELAY);
} while ((err != 1) && (++tries < I2C_RETRIES));
if (err != 1) {
dev_err(&acc->client->dev, "write transfer error\n");
err = -EIO;
} else {
err = 0;
}
return err;
}
static int lis3dh_acc_hw_init(struct lis3dh_acc_data *acc)
{
int err = -1;
u8 buf[7];
printk(KERN_INFO "%s: hw init start\n", LIS3DH_ACC_DEV_NAME);
buf[0] = WHO_AM_I;
err = lis3dh_acc_i2c_read(acc, buf, 1);
if (err < 0) {
dev_warn(&acc->client->dev,
"Error reading WHO_AM_I: is device available/working?\n");
goto err_firstread;
} else
acc->hw_working = 1;
if (buf[0] != WHOAMI_LIS3DH_ACC) {
dev_err(&acc->client->dev,
"device unknown. Expected: 0x%x, Replies: 0x%x\n",
WHOAMI_LIS3DH_ACC, buf[0]);
err = -1; /* choose the right coded error */
goto err_unknown_device;
}
buf[0] = CTRL_REG1;
buf[1] = acc->resume_state[RES_CTRL_REG1];
err = lis3dh_acc_i2c_write(acc, buf, 1);
if (err < 0)
goto err_resume_state;
buf[0] = TEMP_CFG_REG;
buf[1] = acc->resume_state[RES_TEMP_CFG_REG];
err = lis3dh_acc_i2c_write(acc, buf, 1);
if (err < 0)
goto err_resume_state;
buf[0] = FIFO_CTRL_REG;
buf[1] = acc->resume_state[RES_FIFO_CTRL_REG];
err = lis3dh_acc_i2c_write(acc, buf, 1);
if (err < 0)
goto err_resume_state;
buf[0] = (I2C_AUTO_INCREMENT | TT_THS);
buf[1] = acc->resume_state[RES_TT_THS];
buf[2] = acc->resume_state[RES_TT_LIM];
buf[3] = acc->resume_state[RES_TT_TLAT];
buf[4] = acc->resume_state[RES_TT_TW];
err = lis3dh_acc_i2c_write(acc, buf, 4);
if (err < 0)
goto err_resume_state;
buf[0] = TT_CFG;
buf[1] = acc->resume_state[RES_TT_CFG];
err = lis3dh_acc_i2c_write(acc, buf, 1);
if (err < 0)
goto err_resume_state;
buf[0] = (I2C_AUTO_INCREMENT | INT_THS1);
buf[1] = acc->resume_state[RES_INT_THS1];
buf[2] = acc->resume_state[RES_INT_DUR1];
err = lis3dh_acc_i2c_write(acc, buf, 2);
if (err < 0)
goto err_resume_state;
buf[0] = INT_CFG1;
buf[1] = acc->resume_state[RES_INT_CFG1];
err = lis3dh_acc_i2c_write(acc, buf, 1);
if (err < 0)
goto err_resume_state;
buf[0] = (I2C_AUTO_INCREMENT | CTRL_REG2);
buf[1] = acc->resume_state[RES_CTRL_REG2];
buf[2] = acc->resume_state[RES_CTRL_REG3];
buf[3] = acc->resume_state[RES_CTRL_REG4];
buf[4] = acc->resume_state[RES_CTRL_REG5];
buf[5] = acc->resume_state[RES_CTRL_REG6];
err = lis3dh_acc_i2c_write(acc, buf, 5);
if (err < 0)
goto err_resume_state;
acc->hw_initialized = 1;
printk(KERN_INFO "%s: hw init done\n", LIS3DH_ACC_DEV_NAME);
return 0;
err_firstread:
acc->hw_working = 0;
err_unknown_device:
err_resume_state:
acc->hw_initialized = 0;
dev_err(&acc->client->dev, "hw init error 0x%x,0x%x: %d\n", buf[0],
buf[1], err);
return err;
}
static void lis3dh_acc_device_power_off(struct lis3dh_acc_data *acc)
{
int err;
u8 buf[2] = { CTRL_REG1, LIS3DH_ACC_PM_OFF };
err = lis3dh_acc_i2c_write(acc, buf, 1);
if (err < 0)
dev_err(&acc->client->dev, "soft power off failed: %d\n", err);
if (acc->pdata->gpio_int1)
disable_irq_nosync(acc->irq1);
if (acc->pdata->gpio_int2)
disable_irq_nosync(acc->irq2);
lis3dh_acc_config_regulator(acc, false);
if (acc->hw_initialized) {
if (acc->pdata->gpio_int1)
disable_irq_nosync(acc->irq1);
if (acc->pdata->gpio_int2)
disable_irq_nosync(acc->irq2);
acc->hw_initialized = 0;
}
}
static int lis3dh_acc_device_power_on(struct lis3dh_acc_data *acc)
{
int err = -1;
err = lis3dh_acc_config_regulator(acc, true);
if (err < 0) {
dev_err(&acc->client->dev,
"power_on failed: %d\n", err);
return err;
}
if (acc->pdata->gpio_int1 >= 0)
enable_irq(acc->irq1);
if (acc->pdata->gpio_int2 >= 0)
enable_irq(acc->irq2);
msleep(20);
if (!acc->hw_initialized) {
err = lis3dh_acc_hw_init(acc);
if (acc->hw_working == 1 && err < 0) {
lis3dh_acc_device_power_off(acc);
return err;
}
}
if (acc->hw_initialized) {
if (acc->pdata->gpio_int1 >= 0)
enable_irq(acc->irq1);
if (acc->pdata->gpio_int2 >= 0)
enable_irq(acc->irq2);
}
return 0;
}
static irqreturn_t lis3dh_acc_isr1(int irq, void *dev)
{
struct lis3dh_acc_data *acc = dev;
disable_irq_nosync(irq);
queue_work(acc->irq1_work_queue, &acc->irq1_work);
#ifdef DEBUG
printk(KERN_INFO "%s: isr1 queued\n", LIS3DH_ACC_DEV_NAME);
#endif
return IRQ_HANDLED;
}
static irqreturn_t lis3dh_acc_isr2(int irq, void *dev)
{
struct lis3dh_acc_data *acc = dev;
disable_irq_nosync(irq);
queue_work(acc->irq2_work_queue, &acc->irq2_work);
#ifdef DEBUG
printk(KERN_INFO "%s: isr2 queued\n", LIS3DH_ACC_DEV_NAME);
#endif
return IRQ_HANDLED;
}
static void lis3dh_acc_irq1_work_func(struct work_struct *work)
{
struct lis3dh_acc_data *acc =
container_of(work, struct lis3dh_acc_data, irq1_work);
/* TODO add interrupt service procedure.
ie:lis3dh_acc_get_int1_source(acc); */
;
/* */
printk(KERN_INFO "%s: IRQ1 triggered\n", LIS3DH_ACC_DEV_NAME);
goto exit;
exit:
enable_irq(acc->irq1);
}
static void lis3dh_acc_irq2_work_func(struct work_struct *work)
{
struct lis3dh_acc_data *acc =
container_of(work, struct lis3dh_acc_data, irq2_work);
/* TODO add interrupt service procedure.
ie:lis3dh_acc_get_tap_source(acc); */
;
/* */
printk(KERN_INFO "%s: IRQ2 triggered\n", LIS3DH_ACC_DEV_NAME);
goto exit;
exit:
enable_irq(acc->irq2);
}
int lis3dh_acc_update_g_range(struct lis3dh_acc_data *acc, u8 new_g_range)
{
int err = -1;
u8 sensitivity;
u8 buf[2];
u8 updated_val;
u8 init_val;
u8 new_val;
u8 mask = LIS3DH_ACC_FS_MASK | HIGH_RESOLUTION;
switch (new_g_range) {
case LIS3DH_ACC_G_2G:
sensitivity = SENSITIVITY_2G;
break;
case LIS3DH_ACC_G_4G:
sensitivity = SENSITIVITY_4G;
break;
case LIS3DH_ACC_G_8G:
sensitivity = SENSITIVITY_8G;
break;
case LIS3DH_ACC_G_16G:
sensitivity = SENSITIVITY_16G;
break;
default:
dev_err(&acc->client->dev, "invalid g range requested: %u\n",
new_g_range);
return -EINVAL;
}
if (atomic_read(&acc->enabled)) {
/* Updates configuration register 4,
* which contains g range setting */
buf[0] = CTRL_REG4;
err = lis3dh_acc_i2c_read(acc, buf, 1);
if (err < 0)
goto error;
init_val = buf[0];
acc->resume_state[RES_CTRL_REG4] = init_val;
new_val = new_g_range | HIGH_RESOLUTION;
updated_val = ((mask & new_val) | ((~mask) & init_val));
buf[1] = updated_val;
buf[0] = CTRL_REG4;
err = lis3dh_acc_i2c_write(acc, buf, 1);
if (err < 0)
goto error;
acc->resume_state[RES_CTRL_REG4] = updated_val;
acc->sensitivity = sensitivity;
}
return err;
error:
dev_err(&acc->client->dev, "update g range failed 0x%x,0x%x: %d\n",
buf[0], buf[1], err);
return err;
}
int lis3dh_acc_update_odr(struct lis3dh_acc_data *acc, int poll_interval_ms)
{
int err = -1;
int i;
u8 config[2];
/* Following, looks for the longest possible odr interval scrolling the
* odr_table vector from the end (shortest interval) backward (longest
* interval), to support the poll_interval requested by the system.
* It must be the longest interval lower then the poll interval.*/
for (i = ARRAY_SIZE(lis3dh_acc_odr_table) - 1; i >= 0; i--) {
if (lis3dh_acc_odr_table[i].cutoff_ms <= poll_interval_ms)
break;
}
config[1] = lis3dh_acc_odr_table[i].mask;
config[1] |= LIS3DH_ACC_ENABLE_ALL_AXES;
/* If device is currently enabled, we need to write new
* configuration out to it */
if (atomic_read(&acc->enabled)) {
config[0] = CTRL_REG1;
err = lis3dh_acc_i2c_write(acc, config, 1);
if (err < 0)
goto error;
acc->resume_state[RES_CTRL_REG1] = config[1];
}
return err;
error:
dev_err(&acc->client->dev, "update odr failed 0x%x,0x%x: %d\n",
config[0], config[1], err);
return err;
}
static int lis3dh_acc_register_write(struct lis3dh_acc_data *acc, u8 *buf,
u8 reg_address, u8 new_value)
{
int err = -1;
/* Sets configuration register at reg_address
* NOTE: this is a straight overwrite */
buf[0] = reg_address;
buf[1] = new_value;
err = lis3dh_acc_i2c_write(acc, buf, 1);
if (err < 0)
return err;
return err;
}
static int lis3dh_acc_get_acceleration_data(struct lis3dh_acc_data *acc,
int *xyz)
{
int err = -1;
/* Data bytes from hardware xL, xH, yL, yH, zL, zH */
u8 acc_data[6];
/* x,y,z hardware data */
s16 hw_d[3] = { 0 };
acc_data[0] = (I2C_AUTO_INCREMENT | AXISDATA_REG);
err = lis3dh_acc_i2c_read(acc, acc_data, 6);
if (err < 0)
return err;
hw_d[0] = (((s16) ((acc_data[1] << 8) | acc_data[0])) >> 4);
hw_d[1] = (((s16) ((acc_data[3] << 8) | acc_data[2])) >> 4);
hw_d[2] = (((s16) ((acc_data[5] << 8) | acc_data[4])) >> 4);
hw_d[0] = hw_d[0] * acc->sensitivity;
hw_d[1] = hw_d[1] * acc->sensitivity;
hw_d[2] = hw_d[2] * acc->sensitivity;
xyz[0] = ((acc->pdata->negate_x) ? (-hw_d[acc->pdata->axis_map_x])
: (hw_d[acc->pdata->axis_map_x]));
xyz[1] = ((acc->pdata->negate_y) ? (-hw_d[acc->pdata->axis_map_y])
: (hw_d[acc->pdata->axis_map_y]));
xyz[2] = ((acc->pdata->negate_z) ? (-hw_d[acc->pdata->axis_map_z])
: (hw_d[acc->pdata->axis_map_z]));
#ifdef DEBUG
/*
printk(KERN_INFO "%s read x=%d, y=%d, z=%d\n",
LIS3DH_ACC_DEV_NAME, xyz[0], xyz[1], xyz[2]);
*/
#endif
return err;
}
static void lis3dh_acc_report_values(struct lis3dh_acc_data *acc,
int *xyz)
{
input_report_abs(acc->input_dev, ABS_X, xyz[0]);
input_report_abs(acc->input_dev, ABS_Y, xyz[1]);
input_report_abs(acc->input_dev, ABS_Z, xyz[2]);
input_sync(acc->input_dev);
}
static int lis3dh_acc_enable(struct lis3dh_acc_data *acc)
{
int err;
if (!atomic_cmpxchg(&acc->enabled, 0, 1)) {
err = lis3dh_acc_device_power_on(acc);
if (err < 0) {
atomic_set(&acc->enabled, 0);
return err;
}
schedule_delayed_work(&acc->input_work,
msecs_to_jiffies(acc->pdata->poll_interval));
}
return 0;
}
static int lis3dh_acc_disable(struct lis3dh_acc_data *acc)
{
if (atomic_cmpxchg(&acc->enabled, 1, 0)) {
cancel_delayed_work_sync(&acc->input_work);
lis3dh_acc_device_power_off(acc);
}
return 0;
}
static ssize_t read_single_reg(struct device *dev, char *buf, u8 reg)
{
ssize_t ret;
struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
int err;
u8 data = reg;
err = lis3dh_acc_i2c_read(acc, &data, 1);
if (err < 0)
return err;
ret = snprintf(buf, 4, "0x%02x\n", data);
return ret;
}
static int write_reg(struct device *dev, const char *buf, u8 reg,
u8 mask, int resumeIndex)
{
int err = -1;
struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
u8 x[2];
u8 new_val;
unsigned long val;
if (kstrtoul(buf, 16, &val))
return -EINVAL;
new_val = ((u8) val & mask);
x[0] = reg;
x[1] = new_val;
err = lis3dh_acc_register_write(acc, x, reg, new_val);
if (err < 0)
return err;
acc->resume_state[resumeIndex] = new_val;
return err;
}
static ssize_t attr_get_polling_rate(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int val;
struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
mutex_lock(&acc->lock);
val = acc->pdata->poll_interval;
mutex_unlock(&acc->lock);
return snprintf(buf, 8, "%d\n", val);
}
static ssize_t attr_set_polling_rate(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
unsigned long interval_ms;
if (kstrtoul(buf, 10, &interval_ms))
return -EINVAL;
if (!interval_ms)
return -EINVAL;
mutex_lock(&acc->lock);
acc->pdata->poll_interval = interval_ms;
lis3dh_acc_update_odr(acc, interval_ms);
mutex_unlock(&acc->lock);
return size;
}
static ssize_t attr_get_range(struct device *dev,
struct device_attribute *attr, char *buf)
{
char val;
struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
char range = 2;
mutex_lock(&acc->lock);
val = acc->pdata->g_range ;
switch (val) {
case LIS3DH_ACC_G_2G:
range = 2;
break;
case LIS3DH_ACC_G_4G:
range = 4;
break;
case LIS3DH_ACC_G_8G:
range = 8;
break;
case LIS3DH_ACC_G_16G:
range = 16;
break;
}
mutex_unlock(&acc->lock);
return snprintf(buf, 4, "%d\n", range);
}
static ssize_t attr_set_range(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
unsigned long val;
if (kstrtoul(buf, 10, &val))
return -EINVAL;
mutex_lock(&acc->lock);
acc->pdata->g_range = val;
lis3dh_acc_update_g_range(acc, val);
mutex_unlock(&acc->lock);
return size;
}
static ssize_t attr_get_enable(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
int val = atomic_read(&acc->enabled);
return snprintf(buf, sizeof(val) + 2, "%d\n", val);
}
static ssize_t attr_set_enable(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
unsigned long val;
if (kstrtoul(buf, 10, &val))
return -EINVAL;
if (val)
lis3dh_acc_enable(acc);
else
lis3dh_acc_disable(acc);
return size;
}
static ssize_t attr_set_intconfig1(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
return write_reg(dev, buf, INT_CFG1, NO_MASK, RES_INT_CFG1);
}
static ssize_t attr_get_intconfig1(struct device *dev,
struct device_attribute *attr, char *buf)
{
return read_single_reg(dev, buf, INT_CFG1);
}
static ssize_t attr_set_duration1(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
return write_reg(dev, buf, INT_DUR1, INT1_DURATION_MASK, RES_INT_DUR1);
}
static ssize_t attr_get_duration1(struct device *dev,
struct device_attribute *attr, char *buf)
{
return read_single_reg(dev, buf, INT_DUR1);
}
static ssize_t attr_set_thresh1(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
return write_reg(dev, buf, INT_THS1, INT1_THRESHOLD_MASK, RES_INT_THS1);
}
static ssize_t attr_get_thresh1(struct device *dev,
struct device_attribute *attr, char *buf)
{
return read_single_reg(dev, buf, INT_THS1);
}
static ssize_t attr_get_source1(struct device *dev,
struct device_attribute *attr, char *buf)
{
return read_single_reg(dev, buf, INT_SRC1);
}
static ssize_t attr_set_click_cfg(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
return write_reg(dev, buf, TT_CFG, TAP_CFG_MASK, RES_TT_CFG);
}
static ssize_t attr_get_click_cfg(struct device *dev,
struct device_attribute *attr, char *buf)
{
return read_single_reg(dev, buf, TT_CFG);
}
static ssize_t attr_get_click_source(struct device *dev,
struct device_attribute *attr, char *buf)
{
return read_single_reg(dev, buf, TT_SRC);
}
static ssize_t attr_set_click_ths(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
return write_reg(dev, buf, TT_THS, TAP_THS_MASK, RES_TT_THS);
}
static ssize_t attr_get_click_ths(struct device *dev,
struct device_attribute *attr, char *buf)