fxos8700.c
27.8 KB
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
/*
* Copyright (C) 2012-2013 Freescale Semiconductor, Inc. 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/pm.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/input-polldev.h>
#include <linux/miscdevice.h>
#include <linux/poll.h>
/*register define*/
#define FXOS8700_STATUS 0x00
#define FXOS8700_OUT_X_MSB 0x01
#define FXOS8700_OUT_X_LSB 0x02
#define FXOS8700_OUT_Y_MSB 0x03
#define FXOS8700_OUT_Y_LSB 0x04
#define FXOS8700_OUT_Z_MSB 0x05
#define FXOS8700_OUT_Z_LSB 0x06
#define FXOS8700_F_SETUP 0x09
#define FXOS8700_TRIG_CFG 0x0a
#define FXOS8700_SYSMOD 0x0B
#define FXOS8700_INT_SOURCE 0x0c
#define FXOS8700_WHO_AM_I 0x0d
#define FXOS8700_XYZ_DATA_CFG 0x0e
#define FXOS8700_HP_FILTER_CUTOFF 0x0f
#define FXOS8700_PL_STATUS 0x10
#define FXOS8700_PL_CFG 0x11
#define FXOS8700_PL_COUNT 0x12
#define FXOS8700_PL_BF_ZCOMP 0x13
#define FXOS8700_PL_P_L_THS_REG 0x14
#define FXOS8700_FFMT_CFG 0x15
#define FXOS8700_FFMT_SRC 0x16
#define FXOS8700_FFMT_THS 0x17
#define FXOS8700_FFMT_COUNT 0x18
#define FXOS8700_TRANSIDENT1_CFG 0x1d
#define FXOS8700_TRANSIDENT1_SRC 0x1e
#define FXOS8700_TRANSIDENT1_THS 0x1f
#define FXOS8700_TRANSIDENT1_COUNT 0x20
#define FXOS8700_PULSE_CFG 0x21
#define FXOS8700_PULSE_SRC 0x22
#define FXOS8700_PULSE_THSX 0x23
#define FXOS8700_PULSE_THSY 0x24
#define FXOS8700_PULSE_THSZ 0x25
#define FXOS8700_PULSE_TMLT 0x26
#define FXOS8700_PULSE_LTCY 0x27
#define FXOS8700_PULSE_WIND 0x28
#define FXOS8700_ATSLP_COUNT 0x29
#define FXOS8700_CTRL_REG1 0x2a
#define FXOS8700_CTRL_REG2 0x2b
#define FXOS8700_CTRL_REG3 0x2c
#define FXOS8700_CTRL_REG4 0x2d
#define FXOS8700_CTRL_REG5 0x2e
#define FXOS8700_OFF_X 0x2f
#define FXOS8700_OFF_Y 0x30
#define FXOS8700_OFF_Z 0x31
#define FXOS8700_M_DR_STATUS 0x32
#define FXOS8700_M_OUT_X_MSB 0x33
#define FXOS8700_M_OUT_X_LSB 0x34
#define FXOS8700_M_OUT_Y_MSB 0x35
#define FXOS8700_M_OUT_Y_LSB 0x36
#define FXOS8700_M_OUT_Z_MSB 0x37
#define FXOS8700_M_OUT_Z_LSB 0x38
#define FXOS8700_CMP_X_MSB 0x39
#define FXOS8700_CMP_X_LSB 0x3a
#define FXOS8700_CMP_Y_MSB 0x3b
#define FXOS8700_CMP_Y_LSB 0x3c
#define FXOS8700_CMP_Z_MSB 0x3d
#define FXOS8700_CMP_Z_LSB 0x3e
#define FXOS8700_M_OFF_X_MSB 0x3f
#define FXOS8700_M_OFF_X_LSB 0x40
#define FXOS8700_M_OFF_Y_MSB 0x41
#define FXOS8700_M_OFF_Y_LSB 0x42
#define FXOS8700_M_OFF_Z_MSB 0x43
#define FXOS8700_M_OFF_Z_LSB 0x44
#define FXOS8700_MAX_X_MSB 0x45
#define FXOS8700_MAX_X_LSB 0x46
#define FXOS8700_MAX_Y_MSB 0x47
#define FXOS8700_MAX_Y_LSB 0x48
#define FXOS8700_MAX_Z_MSB 0x49
#define FXOS8700_MAX_Z_LSB 0x4a
#define FXOS8700_MIN_X_MSB 0x4b
#define FXOS8700_MIN_X_LSB 0x4c
#define FXOS8700_MIN_Y_MSB 0x4d
#define FXOS8700_MIN_Y_LSB 0x4e
#define FXOS8700_MIN_Z_MSB 0x4f
#define FXOS8700_MIN_Z_LSB 0x50
#define FXOS8700_M_TEMP 0x51
#define FXOS8700_MAG_THS_CFG 0x52
#define FXOS8700_MAG_THS_SRC 0x53
#define FXOS8700_MAG_THS_THS_X1 0x54
#define FXOS8700_MAG_THS_THS_X0 0x55
#define FXOS8700_MAG_THS_THS_Y1 0x56
#define FXOS8700_MAG_THS_THS_Y0 0x57
#define FXOS8700_MAG_THS_THS_Z1 0x58
#define FXOS8700_MAG_THS_THS_Z0 0x59
#define FXOS8700_MAG_THS_CUNT 0x5a
#define FXOS8700_M_CTRL_REG1 0x5b
#define FXOS8700_M_CTRL_REG2 0x5c
#define FXOS8700_M_CTRL_REG3 0x5d
#define FXOS8700_M_INT_SOURCE 0x5e
#define FXOS8700_G_VECM_CFG 0x5f
#define FXOS8700_G_VECM_THS_MSB 0x60
#define FXOS8700_G_VECM_THS_LSB 0x61
#define FXOS8700_G_VECM_CNT 0x62
#define FXOS8700_G_VECM_INITX_MSB 0x63
#define FXOS8700_G_VECM_INITX_LSB 0x64
#define FXOS8700_G_VECM_INITY_MSB 0x65
#define FXOS8700_G_VECM_INITY_LSB 0x66
#define FXOS8700_G_VECM_INITZ_MSB 0x67
#define FXOS8700_G_VECM_INITZ_LSB 0x68
#define FXOS8700_M_VECM_CFG 0x69
#define FXOS8700_M_VECM_THS_MSB 0x6a
#define FXOS8700_M_VECM_THS_LSB 0x6b
#define FXOS8700_M_VECM_CNT 0x6d
#define FXOS8700_M_VECM_INITX_MSB 0x6d
#define FXOS8700_M_VECM_INITX_LSB 0x6e
#define FXOS8700_M_VECM_INITY_MSB 0x6f
#define FXOS8700_M_VECM_INITY_LSB 0x70
#define FXOS8700_M_VECM_INITZ_MSB 0x71
#define FXOS8700_M_VECM_INITZ_LSB 0x72
#define FXOS8700_G_FFMT_THS_X1 0x73
#define FXOS8700_G_FFMT_THS_X0 0x74
#define FXOS8700_G_FFMT_THS_Y1 0x75
#define FXOS8700_G_FFMT_THS_Y0 0x76
#define FXOS8700_G_FFMT_THS_Z1 0x77
#define FXOS8700_G_FFMT_THS_Z0 0x78
#define FXOS8700_G_TRAN_INIT_MSB 0x79
#define FXOS8700_G_TRAN_INIT_LSB_X 0x7a
#define FXOS8700_G_TRAN_INIT_LSB_Y 0x7b
#define FXOS8700_G_TRAN_INIT_LSB_Z 0x7d
#define FXOS8700_TM_NVM_LOCK 0x7e
#define FXOS8700_NVM_DATA0_35 0x80
#define FXOS8700_NVM_DATA_BNK3 0xa4
#define FXOS8700_NVM_DATA_BNK2 0xa5
#define FXOS8700_NVM_DATA_BNK1 0xa6
#define FXOS8700_NVM_DATA_BNK0 0xa7
#define SENSOR_IOCTL_BASE 'S'
#define SENSOR_GET_MODEL_NAME _IOR(SENSOR_IOCTL_BASE, 0, char *)
#define SENSOR_GET_POWER_STATUS _IOR(SENSOR_IOCTL_BASE, 2, int)
#define SENSOR_SET_POWER_STATUS _IOR(SENSOR_IOCTL_BASE, 3, int)
#define SENSOR_GET_DELAY_TIME _IOR(SENSOR_IOCTL_BASE, 4, int)
#define SENSOR_SET_DELAY_TIME _IOR(SENSOR_IOCTL_BASE, 5, int)
#define SENSOR_GET_RAW_DATA _IOR(SENSOR_IOCTL_BASE, 6, short[3])
#define FXOS8700_I2C_ADDR 0x1E
#define FXOS8700_DEVICE_ID 0xC7
#define FXOS8700_PRE_DEVICE_ID 0xC4
#define FXOS8700_DATA_BUF_SIZE 6
#define FXOS8700_DELAY_DEFAULT 200 /* msecs */
#define FXOS8700_POSITION_DEFAULT 1 /* msecs */
#define FXOS8700_TYPE_ACC 0x00
#define FXOS8700_TYPE_MAG 0x01
#define FXOS8700_STANDBY 0x00
#define FXOS8700_ACTIVED 0x01
#define ABS_STATUS ABS_WHEEL
#define FXOS8700_DRIVER "fxos8700"
#define ABSMAX_ACC_VAL 0x01FF
#define ABSMIN_ACC_VAL -(ABSMAX_ACC_VAL)
#define FXOS8700_POLL_INTERVAL 400
#define FXOS8700_POLL_MAX 800
#define FXOS8700_POLL_MIN 100
enum { MODE_2G = 0, MODE_4G, MODE_8G,
};
struct fxos8700_data_axis {
short x;
short y;
short z;
};
struct fxos8700_data {
struct i2c_client *client;
struct input_polled_dev *input_polled;
struct miscdevice *acc_miscdev;
struct miscdevice *mag_miscdev;
atomic_t acc_delay;
atomic_t mag_delay;
atomic_t acc_active;
atomic_t acc_active_poll;
atomic_t mag_active_poll;
atomic_t mag_active;
atomic_t position;
atomic_t range;
};
static struct fxos8700_data *g_fxos8700_data;
static int fxos8700_position_settings[8][3][3] = {
{ { 0, -1, 0}, { 1, 0, 0}, {0, 0, 1} },
{ {-1, 0, 0}, { 0, -1, 0}, {0, 0, 1} },
{ { 0, 1, 0}, {-1, 0, 0}, {0, 0, 1} },
{ { 1, 0, 0}, { 0, 1, 0}, {0, 0, 1} },
{ { 0, -1, 0}, {-1, 0, 0}, {0, 0, -1} },
{ {-1, 0, 0}, { 0, 1, 0}, {0, 0, -1} },
{ { 0, 1, 0}, { 1, 0, 0}, {0, 0, -1} },
{ { 1, 0, 0}, { 0, -1, 0}, {0, 0, -1} },
};
static int fxos8700_data_convert(struct fxos8700_data_axis *axis_data, int position)
{
short rawdata[3], data[3];
int i, j;
if (position < 0 || position > 7)
position = 0;
rawdata[0] = axis_data->x;
rawdata[1] = axis_data->y;
rawdata[2] = axis_data->z;
for (i = 0; i < 3 ; i++) {
data[i] = 0;
for (j = 0; j < 3; j++)
data[i] += rawdata[j] * fxos8700_position_settings[position][i][j];
}
axis_data->x = data[0];
axis_data->y = data[1];
axis_data->z = data[2];
return 0;
}
static int fxos8700_change_mode(struct i2c_client *client, int type, int active)
{
u8 data;
int acc_act, mag_act;
struct fxos8700_data *pdata = i2c_get_clientdata(client);
acc_act = atomic_read(&pdata->acc_active);
mag_act = atomic_read(&pdata->mag_active);
data = i2c_smbus_read_byte_data(client, FXOS8700_CTRL_REG1);
if (type == FXOS8700_TYPE_ACC)
acc_act = active;
else
mag_act = active;
if (acc_act == FXOS8700_ACTIVED || mag_act == FXOS8700_ACTIVED)
data |= 0x01;
else
data &= ~0x01;
i2c_smbus_write_byte_data(client, FXOS8700_CTRL_REG1, data);
return 0;
}
static int fxos8700_change_range(struct i2c_client *client, int range)
{
int ret;
ret = i2c_smbus_write_byte_data(client, FXOS8700_XYZ_DATA_CFG, range);
return ret;
}
static int fxos8700_set_odr(struct i2c_client *client, int type, int delay)
{
return 0;
}
static int fxos8700_device_init(struct i2c_client *client)
{
int result;
struct device_node *np = client->dev.of_node;
struct fxos8700_data *pdata = i2c_get_clientdata(client);
/* set interrupt pin as open-drain */
if (of_get_property(np, "interrupt-open-drain", NULL)) {
result = i2c_smbus_write_byte_data(client, FXOS8700_CTRL_REG3, 0x01);
if (result < 0)
goto out;
}
/* standby mode */
result = i2c_smbus_write_byte_data(client, FXOS8700_CTRL_REG1, 0x00);
if (result < 0)
goto out;
result = i2c_smbus_write_byte_data(client, FXOS8700_M_CTRL_REG1, 0x1F);
if (result < 0)
goto out;
result = i2c_smbus_write_byte_data(client, FXOS8700_M_CTRL_REG2, 0x5c);
if (result < 0)
goto out;
result = i2c_smbus_write_byte_data(client, FXOS8700_CTRL_REG1, 0x03 << 3);
if (result < 0)
goto out;
result = i2c_smbus_write_byte_data(client, FXOS8700_XYZ_DATA_CFG,
MODE_2G);
if (result < 0)
goto out;
atomic_set(&pdata->acc_active, FXOS8700_STANDBY);
atomic_set(&pdata->mag_active, FXOS8700_STANDBY);
atomic_set(&pdata->position, FXOS8700_POSITION_DEFAULT);
atomic_set(&pdata->range, MODE_2G);
return 0;
out:
dev_err(&client->dev, "Error when init fxos8700 device:(%d)", result);
return result;
}
static int fxos8700_device_stop(struct i2c_client *client)
{
i2c_smbus_write_byte_data(client, FXOS8700_CTRL_REG1, 0x00);
return 0;
}
static int
fxos8700_read_data(struct i2c_client *client, struct fxos8700_data_axis *data, int type)
{
u8 tmp_data[FXOS8700_DATA_BUF_SIZE];
int ret;
u8 reg;
if (type == FXOS8700_TYPE_ACC)
reg = FXOS8700_OUT_X_MSB;
else
reg = FXOS8700_M_OUT_X_MSB;
ret = i2c_smbus_read_i2c_block_data(client, reg, FXOS8700_DATA_BUF_SIZE, tmp_data);
if (ret < FXOS8700_DATA_BUF_SIZE) {
dev_err(&client->dev, "i2c block read %s failed\n",
(type == FXOS8700_TYPE_ACC ? "acc" : "mag"));
return -EIO;
}
data->x = ((tmp_data[0] << 8) & 0xff00) | tmp_data[1];
data->y = ((tmp_data[2] << 8) & 0xff00) | tmp_data[3];
data->z = ((tmp_data[4] << 8) & 0xff00) | tmp_data[5];
return 0;
}
static long fxos8700_acc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct fxos8700_data *pdata = file->private_data;
void __user *argp = (void __user *)arg;
struct fxos8700_data_axis data;
long ret = 0;
short sdata[3];
int enable;
int delay;
int position;
if (!pdata) {
printk(KERN_ERR "fxos8700 struct datt point is NULL.");
return -EFAULT;
}
switch (cmd) {
case SENSOR_GET_MODEL_NAME:
if (copy_to_user(argp, "FXOS8700 ACC", strlen("FXOS8700 ACC") + 1)) {
printk(KERN_ERR "SENSOR_GET_MODEL_NAME copy_to_user failed.");
ret = -EFAULT;
}
break;
case SENSOR_GET_POWER_STATUS:
enable = atomic_read(&pdata->acc_active);
if (copy_to_user(argp, &enable, sizeof(int))) {
printk(KERN_ERR "SENSOR_SET_POWER_STATUS copy_to_user failed.");
ret = -EFAULT;
}
break;
case SENSOR_SET_POWER_STATUS:
if (copy_from_user(&enable, argp, sizeof(int))) {
printk(KERN_ERR "SENSOR_SET_POWER_STATUS copy_to_user failed.");
ret = -EFAULT;
}
if (pdata->client) {
ret = fxos8700_change_mode(pdata->client, FXOS8700_TYPE_ACC,
enable ? FXOS8700_ACTIVED : FXOS8700_STANDBY);
if (!ret)
atomic_set(&pdata->acc_active, enable);
}
break;
case SENSOR_GET_DELAY_TIME:
delay = atomic_read(&pdata->acc_delay);
if (copy_to_user(argp, &delay, sizeof(delay))) {
printk(KERN_ERR "SENSOR_GET_DELAY_TIME copy_to_user failed.");
return -EFAULT;
}
break;
case SENSOR_SET_DELAY_TIME:
if (copy_from_user(&delay, argp, sizeof(int))) {
printk(KERN_ERR "SENSOR_SET_DELAY_TIME copy_to_user failed.");
ret = -EFAULT;
}
if (pdata->client && delay > 0 && delay <= 500) {
ret = fxos8700_set_odr(pdata->client, FXOS8700_TYPE_ACC, delay);
if (!ret)
atomic_set(&pdata->acc_delay, delay);
}
break;
case SENSOR_GET_RAW_DATA:
position = atomic_read(&pdata->position);
ret = fxos8700_read_data(pdata->client, &data, FXOS8700_TYPE_ACC);
if (!ret) {
fxos8700_data_convert(&data, position);
sdata[0] = data.x;
sdata[1] = data.y;
sdata[2] = data.z;
if (copy_to_user(argp, sdata, sizeof(sdata))) {
printk(KERN_ERR "SENSOR_GET_RAW_DATA copy_to_user failed.");
ret = -EFAULT;
}
}
break;
default:
ret = -1;
}
return ret;
}
static int fxos8700_acc_open(struct inode *inode, struct file *file)
{
file->private_data = g_fxos8700_data;
return nonseekable_open(inode, file);
}
static int fxos8700_acc_release(struct inode *inode, struct file *file)
{
/* note: releasing the wdt in NOWAYOUT-mode does not stop it */
return 0;
}
static const struct file_operations fxos8700_acc_fops = {
.owner = THIS_MODULE,
.open = fxos8700_acc_open,
.release = fxos8700_acc_release,
.unlocked_ioctl = fxos8700_acc_ioctl,
};
/* mag char miscdevice */
static long fxos8700_mag_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct fxos8700_data *pdata = file->private_data;
void __user *argp = (void __user *)arg;
struct fxos8700_data_axis data;
long ret = 0;
short sdata[3];
int enable;
int delay;
int position;
if (!pdata) {
printk(KERN_ERR "fxos8700 struct datt point is NULL.");
return -EFAULT;
}
switch (cmd) {
case SENSOR_GET_MODEL_NAME:
if (copy_to_user(argp, "FXOS8700 MAG", strlen("FXOS8700 MAG") + 1)) {
printk(KERN_ERR "SENSOR_GET_MODEL_NAME copy_to_user failed.");
ret = -EFAULT;
}
break;
case SENSOR_GET_POWER_STATUS:
enable = atomic_read(&pdata->mag_active);
if (copy_to_user(argp, &enable, sizeof(int))) {
printk(KERN_ERR "SENSOR_SET_POWER_STATUS copy_to_user failed.");
ret = -EFAULT;
}
break;
case SENSOR_SET_POWER_STATUS:
if (copy_from_user(&enable, argp, sizeof(int))) {
printk(KERN_ERR "SENSOR_SET_POWER_STATUS copy_to_user failed.");
ret = -EFAULT;
}
if (pdata->client) {
ret = fxos8700_change_mode(pdata->client, FXOS8700_TYPE_MAG,
enable ? FXOS8700_ACTIVED : FXOS8700_STANDBY);
if (!ret)
atomic_set(&pdata->mag_active, enable);
}
break;
case SENSOR_GET_DELAY_TIME:
delay = atomic_read(&pdata->mag_delay);
if (copy_to_user(argp, &delay, sizeof(delay))) {
printk(KERN_ERR "SENSOR_GET_DELAY_TIME copy_to_user failed.");
return -EFAULT;
}
break;
case SENSOR_SET_DELAY_TIME:
if (copy_from_user(&delay, argp, sizeof(int))) {
printk(KERN_ERR "SENSOR_SET_DELAY_TIME copy_to_user failed.");
ret = -EFAULT;
}
if (pdata->client && delay > 0 && delay <= 500) {
ret = fxos8700_set_odr(pdata->client, FXOS8700_TYPE_MAG, delay);
if (!ret)
atomic_set(&pdata->mag_delay, delay);
}
break;
case SENSOR_GET_RAW_DATA:
position = atomic_read(&pdata->position);
ret = fxos8700_read_data(pdata->client, &data, FXOS8700_TYPE_MAG);
if (!ret) {
fxos8700_data_convert(&data, position);
sdata[0] = data.x;
sdata[1] = data.y;
sdata[2] = data.z;
if (copy_to_user(argp, sdata, sizeof(sdata))) {
printk(KERN_ERR "SENSOR_GET_RAW_DATA copy_to_user failed.");
ret = -EFAULT;
}
}
break;
default:
ret = -1;
}
return ret;
}
static int fxos8700_mag_open(struct inode *inode, struct file *file)
{
file->private_data = g_fxos8700_data;
return nonseekable_open(inode, file);
}
static int fxos8700_mag_release(struct inode *inode, struct file *file)
{
/* note: releasing the wdt in NOWAYOUT-mode does not stop it */
return 0;
}
static const struct file_operations fxos8700_mag_fops = {
.owner = THIS_MODULE,
.open = fxos8700_mag_open,
.release = fxos8700_mag_release,
.unlocked_ioctl = fxos8700_mag_ioctl,
};
static struct miscdevice fxos8700_acc_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "FreescaleAccelerometer",
.fops = &fxos8700_acc_fops,
};
static struct miscdevice fxos8700_mag_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "FreescaleMagnetometer",
.fops = &fxos8700_mag_fops,
};
static ssize_t fxos8700_enable_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct miscdevice *misc_dev = dev_get_drvdata(dev);
struct fxos8700_data *pdata = g_fxos8700_data;
int enable = 0;
if (pdata->acc_miscdev == misc_dev)
enable = atomic_read(&pdata->acc_active);
if (pdata->mag_miscdev == misc_dev)
enable = atomic_read(&pdata->mag_active);
return sprintf(buf, "%d\n", enable);
}
static ssize_t fxos8700_enable_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct miscdevice *misc_dev = dev_get_drvdata(dev);
struct fxos8700_data *pdata = g_fxos8700_data;
struct i2c_client *client = pdata->client;
unsigned long enable;
int type;
int ret;
if (kstrtoul(buf, 10, &enable) < 0)
return -EINVAL;
if (misc_dev == pdata->acc_miscdev)
type = FXOS8700_TYPE_ACC;
if (misc_dev == pdata->mag_miscdev)
type = FXOS8700_TYPE_MAG;
enable = (enable > 0 ? FXOS8700_ACTIVED : FXOS8700_STANDBY);
ret = fxos8700_change_mode(client, type, enable);
if (!ret) {
if (type == FXOS8700_TYPE_ACC) {
atomic_set(&pdata->acc_active, enable);
atomic_set(&pdata->acc_active_poll, enable);
} else {
atomic_set(&pdata->mag_active, enable);
atomic_set(&pdata->mag_active_poll, enable);
}
}
return count;
}
static ssize_t fxos8700_poll_delay_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct miscdevice *misc_dev = dev_get_drvdata(dev);
struct fxos8700_data *pdata = g_fxos8700_data;
int poll_delay = 0;
if (pdata->acc_miscdev == misc_dev)
poll_delay = atomic_read(&pdata->acc_delay);
if (pdata->mag_miscdev == misc_dev)
poll_delay = atomic_read(&pdata->mag_delay);
return sprintf(buf, "%d\n", poll_delay);
}
static ssize_t fxos8700_poll_delay_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct miscdevice *misc_dev = dev_get_drvdata(dev);
struct fxos8700_data *pdata = g_fxos8700_data;
struct i2c_client *client = pdata->client;
unsigned long delay;
int type;
int ret;
if (kstrtoul(buf, 10, &delay) < 0)
return -EINVAL;
if (misc_dev == pdata->acc_miscdev)
type = FXOS8700_TYPE_ACC;
if (misc_dev == pdata->mag_miscdev)
type = FXOS8700_TYPE_MAG;
ret = fxos8700_set_odr(client, type, delay);
if (!ret) {
if (type == FXOS8700_TYPE_ACC)
atomic_set(&pdata->acc_delay, delay);
else
atomic_set(&pdata->mag_delay, delay);
}
return count;
}
static ssize_t fxos8700_position_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct fxos8700_data *pdata = g_fxos8700_data;
unsigned long position = atomic_read(&pdata->position);
return sprintf(buf, "%ld\n", position);
}
static ssize_t fxos8700_position_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned long position;
struct fxos8700_data *pdata = g_fxos8700_data;
if (kstrtoul(buf, 10, &position) < 0)
return -EINVAL;
atomic_set(&pdata->position, position);
return count;
}
static ssize_t fxos8700_range_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct fxos8700_data *pdata = g_fxos8700_data;
unsigned long range = atomic_read(&pdata->range);
return sprintf(buf, "%ld\n", range);
}
static ssize_t fxos8700_range_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned long range;
struct fxos8700_data *pdata = g_fxos8700_data;
struct i2c_client *client = pdata->client;
int ret;
if (kstrtoul(buf, 10, &range) < 0)
return -EINVAL;
if (range == atomic_read(&pdata->range))
return count;
if (atomic_read(&pdata->acc_active) | atomic_read(&pdata->mag_active))
printk(KERN_INFO "Pls set the sensor standby and then actived\n");
ret = fxos8700_change_range(client, range);
if (!ret)
atomic_set(&pdata->range, range);
return count;
}
static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO, fxos8700_enable_show, fxos8700_enable_store);
static DEVICE_ATTR(poll_delay, S_IWUSR | S_IRUGO, fxos8700_poll_delay_show, fxos8700_poll_delay_store);
static DEVICE_ATTR(position, S_IWUSR | S_IRUGO, fxos8700_position_show, fxos8700_position_store);
static DEVICE_ATTR(range, S_IWUSR | S_IRUGO, fxos8700_range_show, fxos8700_range_store);
static struct attribute *fxos8700_attributes[] = {
&dev_attr_enable.attr,
&dev_attr_poll_delay.attr,
&dev_attr_position.attr,
&dev_attr_range.attr,
NULL
};
static const struct attribute_group fxos8700_attr_group = {
.attrs = fxos8700_attributes,
};
static int fxos8700_register_sysfs_device(struct fxos8700_data *pdata)
{
struct miscdevice *misc_dev = NULL;
int err = -1;
/* register sysfs for acc */
misc_dev = pdata->acc_miscdev;
err = sysfs_create_group(&misc_dev->this_device->kobj, &fxos8700_attr_group);
if (err)
goto out;
/* register sysfs for mag */
misc_dev = pdata->mag_miscdev;
err = sysfs_create_group(&misc_dev->this_device->kobj, &fxos8700_attr_group);
if (err)
goto err_register_sysfs;
return 0;
err_register_sysfs:
misc_dev = pdata->acc_miscdev;
sysfs_remove_group(&misc_dev->this_device->kobj, &fxos8700_attr_group);
printk("reigster mag sysfs error\n");
out:
printk("reigster acc sysfs error\n");
return err;
}
static int fxos8700_unregister_sysfs_device(struct fxos8700_data *pdata)
{
struct miscdevice *misc_dev;
misc_dev = pdata->acc_miscdev;
sysfs_remove_group(&misc_dev->this_device->kobj, &fxos8700_attr_group);
misc_dev = pdata->mag_miscdev;
sysfs_remove_group(&misc_dev->this_device->kobj, &fxos8700_attr_group);
return 0;
}
static void fxos8700_report(struct input_polled_dev *dev, int type)
{
struct fxos8700_data_axis data;
struct fxos8700_data *pdata = g_fxos8700_data;
struct input_dev *idev = pdata->input_polled->input;
int position;
int ret;
position = atomic_read(&pdata->position);
ret = fxos8700_read_data(pdata->client, &data, type);
if (!ret) {
fxos8700_data_convert(&data, position);
input_report_abs(idev, ABS_X, data.x);
input_report_abs(idev, ABS_Y, data.y);
input_report_abs(idev, ABS_Z, data.z);
input_sync(idev);
}
}
static void fxos8700_poll(struct input_polled_dev *dev)
{
struct fxos8700_data *pdata = g_fxos8700_data;
int type;
if (!(atomic_read(&pdata->acc_active_poll) ||
atomic_read(&pdata->mag_active_poll)))
return;
if (atomic_read(&pdata->acc_active_poll))
type = FXOS8700_TYPE_ACC;
if (atomic_read(&pdata->mag_active_poll))
type =FXOS8700_TYPE_MAG;
fxos8700_report(dev, type);
}
static int fxo8700_register_polled_device(struct fxos8700_data *pdata)
{
struct input_polled_dev *ipoll_dev;
struct input_dev *idev;
int error;
ipoll_dev = input_allocate_polled_device();
if (!ipoll_dev)
return -ENOMEM;
ipoll_dev->private = pdata;
ipoll_dev->poll = fxos8700_poll;
ipoll_dev->poll_interval = FXOS8700_POLL_INTERVAL;
ipoll_dev->poll_interval_min = FXOS8700_POLL_MIN;
ipoll_dev->poll_interval_max = FXOS8700_POLL_MAX;
idev = ipoll_dev->input;
idev->name = FXOS8700_DRIVER;
idev->id.bustype = BUS_I2C;
idev->dev.parent = &pdata->client->dev;
idev->evbit[0] = BIT_MASK(EV_ABS);
input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
error = input_register_polled_device(ipoll_dev);
if (error) {
input_free_polled_device(ipoll_dev);
return error;
}
pdata->input_polled = ipoll_dev;
return 0;
}
static int fxos8700_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int result, client_id;
struct fxos8700_data *pdata;
struct i2c_adapter *adapter;
adapter = to_i2c_adapter(client->dev.parent);
result = i2c_check_functionality(adapter,
I2C_FUNC_SMBUS_BYTE |
I2C_FUNC_SMBUS_BYTE_DATA);
if (!result)
goto err_out;
client_id = i2c_smbus_read_byte_data(client, FXOS8700_WHO_AM_I);
if (client_id != FXOS8700_DEVICE_ID && client_id != FXOS8700_PRE_DEVICE_ID) {
dev_err(&client->dev,
"read chip ID 0x%x is not equal to 0x%x or 0x%x\n",
result, FXOS8700_DEVICE_ID, FXOS8700_PRE_DEVICE_ID);
result = -EINVAL;
goto err_out;
}
pdata = kzalloc(sizeof(struct fxos8700_data), GFP_KERNEL);
if (!pdata) {
result = -ENOMEM;
dev_err(&client->dev, "alloc data memory error!\n");
goto err_out;
}
g_fxos8700_data = pdata;
pdata->client = client;
atomic_set(&pdata->acc_delay, FXOS8700_DELAY_DEFAULT);
atomic_set(&pdata->mag_delay, FXOS8700_DELAY_DEFAULT);
i2c_set_clientdata(client, pdata);
result = misc_register(&fxos8700_acc_device);
if (result != 0) {
printk(KERN_ERR "register acc miscdevice error");
goto err_regsiter_acc_misc;
}
pdata->acc_miscdev = &fxos8700_acc_device;
result = misc_register(&fxos8700_mag_device);
if (result != 0) {
printk(KERN_ERR "register acc miscdevice error");
goto err_regsiter_mag_misc;
}
pdata->mag_miscdev = &fxos8700_mag_device;
/* for debug */
if (client->irq <= 0) {
result = fxo8700_register_polled_device(g_fxos8700_data);
if (result)
dev_err(&client->dev,
"IRQ GPIO conf. error %d, error %d\n",
client->irq, result);
}
result = fxos8700_register_sysfs_device(pdata);
if (result) {
dev_err(&client->dev, "create device file failed!\n");
result = -EINVAL;
goto err_register_sys;
}
fxos8700_device_init(client);
printk("fxos8700 device driver probe successfully");
return 0;
err_register_sys:
misc_deregister(&fxos8700_mag_device);
pdata->mag_miscdev = NULL;
err_regsiter_mag_misc:
misc_deregister(&fxos8700_acc_device);
pdata->acc_miscdev = NULL;
err_regsiter_acc_misc:
i2c_set_clientdata(client, NULL);
kfree(pdata);
err_out:
return result;
}
static int fxos8700_remove(struct i2c_client *client)
{
struct fxos8700_data *pdata = i2c_get_clientdata(client);
if (!pdata)
return 0;
fxos8700_device_stop(client);
if (client->irq <= 0) {
input_unregister_polled_device(pdata->input_polled);
input_free_polled_device(pdata->input_polled);
}
fxos8700_unregister_sysfs_device(pdata);
misc_deregister(&fxos8700_acc_device);
misc_deregister(&fxos8700_mag_device);
kfree(pdata);
return 0;
}
#ifdef CONFIG_PM_SLEEP
static int fxos8700_suspend(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct fxos8700_data *pdata = i2c_get_clientdata(client);
if (atomic_read(&pdata->acc_active) || atomic_read(&pdata->mag_active))
fxos8700_device_stop(client);
return 0;
}
static int fxos8700_resume(struct device *dev)
{
int ret = 0;
struct i2c_client *client = to_i2c_client(dev);
struct fxos8700_data *pdata = i2c_get_clientdata(client);
if (atomic_read(&pdata->acc_active))
fxos8700_change_mode(client, FXOS8700_TYPE_ACC, FXOS8700_ACTIVED);
if (atomic_read(&pdata->mag_active))
fxos8700_change_mode(client, FXOS8700_TYPE_MAG, FXOS8700_ACTIVED);
return ret;
}
#endif
static const struct i2c_device_id fxos8700_id[] = {
{"fxos8700", 0},
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(i2c, fxos8700_id);
static SIMPLE_DEV_PM_OPS(fxos8700_pm_ops, fxos8700_suspend, fxos8700_resume);
static struct i2c_driver fxos8700_driver = {
.driver = {
.name = FXOS8700_DRIVER,
.owner = THIS_MODULE,
.pm = &fxos8700_pm_ops,
},
.probe = fxos8700_probe,
.remove = fxos8700_remove,
.id_table = fxos8700_id,
};
module_i2c_driver(fxos8700_driver);
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("FXOS8700 6-Axis Acc and Mag Combo Sensor driver");
MODULE_LICENSE("GPL");