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
|
/*
* cma3000_d0x.c
* VTI CMA3000_D0x Accelerometer driver
* Supports I2C/SPI interfaces
*
* Copyright (C) 2010 Texas Instruments
* Author: Hemanth V <hemanthv@ti.com>
* Contributor: Dan Murphy <dmurphy@ti.com>
*
* 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.
*
* 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/interrupt.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/platform_device.h>
#include <linux/i2c/cma3000.h>
#include "cma3000_d0x.h"
#define CMA3000_WHOAMI 0x00
#define CMA3000_REVID 0x01
#define CMA3000_CTRL 0x02
#define CMA3000_STATUS 0x03
#define CMA3000_RSTR 0x04
#define CMA3000_INTSTATUS 0x05
#define CMA3000_DOUTX 0x06
#define CMA3000_DOUTY 0x07
#define CMA3000_DOUTZ 0x08
#define CMA3000_MDTHR 0x09
#define CMA3000_MDFFTMR 0x0A
#define CMA3000_FFTHR 0x0B
#define CMA3000_RANGE2G (1 << 7)
#define CMA3000_RANGE8G (0 << 7)
#define CMA3000_BUSI2C (0 << 4)
#define CMA3000_MODEMASK (7 << 1)
#define CMA3000_GRANGEMASK (1 << 7)
#define CMA3000_STATUS_PERR 1
#define CMA3000_INTSTATUS_FFDET (1 << 2)
/* Settling time delay in ms */
#define CMA3000_SETDELAY 30
/* Delay for clearing interrupt in us */
#define CMA3000_INTDELAY 44
/*
* Bit weights in mg for bit 0, other bits need
* multipy factor 2^n. Eight bit is the sign bit.
*/
#define BIT_TO_2G 18
#define BIT_TO_8G 71
/*
* Conversion for each of the eight modes to g, depending
* on G range i.e 2G or 8G. Some modes always operate in
* 8G.
*/
static int mode_to_mg[8][2] = {
{0, 0},
{BIT_TO_8G, BIT_TO_2G},
{BIT_TO_8G, BIT_TO_2G},
{BIT_TO_8G, BIT_TO_8G},
{BIT_TO_8G, BIT_TO_8G},
{BIT_TO_8G, BIT_TO_2G},
{BIT_TO_8G, BIT_TO_2G},
{0, 0},
};
/* interval between samples for the different rates, in msecs */
static const unsigned int cma3000_measure_interval[] = {
0, 1000 / 10, 1000 / 400, 1000 / 40,
};
static uint32_t accl_debug;
module_param_named(cma3000_debug, accl_debug, uint, 0664);
static int cma3000_set_mode(struct cma3000_accl_data *data, int val)
{
uint8_t ctrl;
int error = 0;
mutex_lock(&data->mutex);
val &= (CMA3000_MODEMASK >> 1);
ctrl = cma3000_read(data, CMA3000_CTRL, "ctrl");
if (ctrl < 0) {
error = ctrl;
goto err_op2_failed;
}
ctrl &= ~CMA3000_MODEMASK;
ctrl |= (val << 1);
if (!data->client->irq)
ctrl |= 0x01;
error = cma3000_set(data, CMA3000_CTRL, ctrl, "ctrl");
if (error < 0)
goto err_op1_failed;
/* Settling time delay required after mode change */
msleep(CMA3000_SETDELAY);
err_op1_failed:
err_op2_failed:
mutex_unlock(&data->mutex);
return error;
}
static ssize_t cma3000_show_attr_mode(struct device *dev,
struct device_attribute *attr,
char *buf)
{
uint8_t mode;
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
mode = cma3000_read(data, CMA3000_CTRL, "ctrl");
if (mode < 0)
return mode;
return sprintf(buf, "%d\n", (mode & CMA3000_MODEMASK) >> 1);
}
static ssize_t cma3000_store_attr_mode(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
unsigned long val;
int error;
error = strict_strtoul(buf, 0, &val);
if (error)
return error;
if (val < CMAMODE_DEFAULT || val > CMAMODE_POFF)
return -EINVAL;
return cma3000_set_mode(data, val);
}
static ssize_t cma3000_show_attr_delay(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
return sprintf(buf, "%d\n", data->req_poll_rate);
}
static ssize_t cma3000_store_attr_delay(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
unsigned long interval;
int error;
int i = 0;
error = strict_strtoul(buf, 0, &interval);
if (error)
return error;
if (interval < 0)
return -EINVAL;
cancel_delayed_work_sync(&data->input_work);
if (interval == 0) {
/* Set to the fastest speed */
i = CMAMODE_MEAS400;
} else {
if (interval < cma3000_measure_interval[CMAMODE_MEAS40])
i = CMAMODE_MEAS400;
else if (interval >= cma3000_measure_interval[CMAMODE_MEAS100])
i = CMAMODE_MEAS100;
else
i = CMAMODE_MEAS40;
}
data->req_poll_rate = interval;
cma3000_set_mode(data, i);
schedule_delayed_work(&data->input_work, 0);
return count;
}
static ssize_t cma3000_show_attr_enable(struct device *dev,
struct device_attribute *attr,
char *buf)
{
uint8_t mode;
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
mode = cma3000_read(data, CMA3000_CTRL, "ctrl");
if (mode < 0)
return mode;
return sprintf(buf, "%d\n", (mode & CMA3000_MODEMASK) >> 1);
}
static ssize_t cma3000_store_attr_enable(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
unsigned long val;
int error, enable;
error = strict_strtoul(buf, 0, &val);
if (error)
return error;
if (val < CMAMODE_DEFAULT || val > CMAMODE_POFF)
return -EINVAL;
enable = val;
cma3000_set_mode(data, enable);
data->pdata.mode = val;
if (val == CMAMODE_DEFAULT || val == CMAMODE_POFF)
cancel_delayed_work_sync(&data->input_work);
else
schedule_delayed_work(&data->input_work, 0);
return count;
}
static ssize_t cma3000_show_attr_grange(struct device *dev,
struct device_attribute *attr,
char *buf)
{
uint8_t mode;
int g_range;
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
mode = cma3000_read(data, CMA3000_CTRL, "ctrl");
if (mode < 0)
return mode;
g_range = (mode & CMA3000_GRANGEMASK) ? CMARANGE_2G : CMARANGE_8G;
return sprintf(buf, "%d\n", g_range);
}
static ssize_t cma3000_store_attr_grange(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
unsigned long val;
int error, g_range, fuzz_x, fuzz_y, fuzz_z;
uint8_t ctrl;
error = strict_strtoul(buf, 0, &val);
if (error)
goto err_op3_failed;
mutex_lock(&data->mutex);
ctrl = cma3000_read(data, CMA3000_CTRL, "ctrl");
if (ctrl < 0) {
error = ctrl;
goto err_op2_failed;
}
ctrl &= ~CMA3000_GRANGEMASK;
if (val == CMARANGE_2G) {
ctrl |= CMA3000_RANGE2G;
data->pdata.g_range = CMARANGE_2G;
} else if (val == CMARANGE_8G) {
ctrl |= CMA3000_RANGE8G;
data->pdata.g_range = CMARANGE_8G;
} else {
error = -EINVAL;
goto err_op2_failed;
}
g_range = data->pdata.g_range;
fuzz_x = data->pdata.fuzz_x;
fuzz_y = data->pdata.fuzz_y;
fuzz_z = data->pdata.fuzz_z;
disable_irq(data->client->irq);
error = cma3000_set(data, CMA3000_CTRL, ctrl, "ctrl");
if (error < 0)
goto err_op1_failed;
input_set_abs_params(data->input_dev, ABS_X, -g_range,
g_range, fuzz_x, 0);
input_set_abs_params(data->input_dev, ABS_Y, -g_range,
g_range, fuzz_y, 0);
input_set_abs_params(data->input_dev, ABS_Z, -g_range,
g_range, fuzz_z, 0);
enable_irq(data->client->irq);
mutex_unlock(&data->mutex);
return count;
err_op1_failed:
enable_irq(data->client->irq);
err_op2_failed:
mutex_unlock(&data->mutex);
err_op3_failed:
return error;
}
static ssize_t cma3000_show_attr_mdthr(struct device *dev,
struct device_attribute *attr,
char *buf)
{
uint8_t mode;
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
mode = cma3000_read(data, CMA3000_MDTHR, "mdthr");
if (mode < 0)
return mode;
return sprintf(buf, "%d\n", mode);
}
static ssize_t cma3000_store_attr_mdthr(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
unsigned long val;
int error;
error = strict_strtoul(buf, 0, &val);
if (error)
return error;
mutex_lock(&data->mutex);
data->pdata.mdthr = val;
disable_irq(data->client->irq);
error = cma3000_set(data, CMA3000_MDTHR, val, "mdthr");
enable_irq(data->client->irq);
mutex_unlock(&data->mutex);
/* If there was error during write, return error */
if (error < 0)
return error;
else
return count;
}
static ssize_t cma3000_show_attr_mdfftmr(struct device *dev,
struct device_attribute *attr,
char *buf)
{
uint8_t mode;
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
mode = cma3000_read(data, CMA3000_MDFFTMR, "mdfftmr");
if (mode < 0)
return mode;
return sprintf(buf, "%d\n", mode);
}
static ssize_t cma3000_store_attr_mdfftmr(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
unsigned long val;
int error;
error = strict_strtoul(buf, 0, &val);
if (error)
return error;
mutex_lock(&data->mutex);
data->pdata.mdfftmr = val;
disable_irq(data->client->irq);
error = cma3000_set(data, CMA3000_MDFFTMR, val, "mdthr");
enable_irq(data->client->irq);
mutex_unlock(&data->mutex);
/* If there was error during write, return error */
if (error < 0)
return error;
else
return count;
}
static ssize_t cma3000_show_attr_ffthr(struct device *dev,
struct device_attribute *attr,
char *buf)
{
uint8_t mode;
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
mode = cma3000_read(data, CMA3000_FFTHR, "ffthr");
if (mode < 0)
return mode;
return sprintf(buf, "%d\n", mode);
}
static ssize_t cma3000_store_attr_ffthr(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct cma3000_accl_data *data = platform_get_drvdata(pdev);
unsigned long val;
int error;
error = strict_strtoul(buf, 0, &val);
if (error)
return error;
mutex_lock(&data->mutex);
data->pdata.ffthr = val;
disable_irq(data->client->irq);
error = cma3000_set(data, CMA3000_FFTHR, val, "mdthr");
enable_irq(data->client->irq);
mutex_unlock(&data->mutex);
/* If there was error during write, return error */
if (error < 0)
return error;
else
return count;
}
static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
cma3000_show_attr_mode, cma3000_store_attr_mode);
static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO,
cma3000_show_attr_enable, cma3000_store_attr_enable);
static DEVICE_ATTR(delay, S_IWUSR | S_IRUGO,
cma3000_show_attr_delay, cma3000_store_attr_delay);
static DEVICE_ATTR(grange, S_IWUSR | S_IRUGO,
cma3000_show_attr_grange, cma3000_store_attr_grange);
static DEVICE_ATTR(mdthr, S_IWUSR | S_IRUGO,
cma3000_show_attr_mdthr, cma3000_store_attr_mdthr);
static DEVICE_ATTR(mdfftmr, S_IWUSR | S_IRUGO,
cma3000_show_attr_mdfftmr, cma3000_store_attr_mdfftmr);
static DEVICE_ATTR(ffthr, S_IWUSR | S_IRUGO,
cma3000_show_attr_ffthr, cma3000_store_attr_ffthr);
static struct attribute *cma_attrs[] = {
&dev_attr_mode.attr,
&dev_attr_enable.attr,
&dev_attr_delay.attr,
&dev_attr_grange.attr,
&dev_attr_mdthr.attr,
&dev_attr_mdfftmr.attr,
&dev_attr_ffthr.attr,
NULL,
};
static struct attribute_group cma3000_attr_group = {
.attrs = cma_attrs,
};
static void decode_mg(struct cma3000_accl_data *data, int *datax,
int *datay, int *dataz)
{
/* Data in 2's complement, convert to mg */
*datax = (((s8)(*datax)) * (data->bit_to_mg));
*datay = (((s8)(*datay)) * (data->bit_to_mg));
*dataz = (((s8)(*dataz)) * (data->bit_to_mg));
}
static void cma3000_read_report_data(struct cma3000_accl_data *data)
{
int datax, datay, dataz;
u8 ctrl, mode, range, intr_status;
mutex_lock(&data->mutex);
intr_status = cma3000_read(data, CMA3000_INTSTATUS, "interrupt status");
if (intr_status < 0) {
pr_info("%s:No interrupt from the device\n", __func__);
goto not_from_device;
}
/* Check if free fall is detected, report immediately */
if (intr_status & CMA3000_INTSTATUS_FFDET) {
if (accl_debug)
pr_info("%s:Free fall\n", __func__);
input_report_abs(data->input_dev, ABS_MISC, 1);
input_sync(data->input_dev);
} else {
input_report_abs(data->input_dev, ABS_MISC, 0);
}
datax = cma3000_read(data, CMA3000_DOUTX, "X");
datay = cma3000_read(data, CMA3000_DOUTY, "Y");
dataz = cma3000_read(data, CMA3000_DOUTZ, "Z");
if (accl_debug)
pr_info("%s:Raw x= %d, y= %d, z= %d\n",
__func__, datax, datay, dataz);
ctrl = cma3000_read(data, CMA3000_CTRL, "ctrl");
mode = (ctrl & CMA3000_MODEMASK) >> 1;
range = (ctrl & CMA3000_GRANGEMASK) >> 7;
data->bit_to_mg = mode_to_mg[mode][range];
/* Interrupt not for this device */
if (data->bit_to_mg == 0)
goto not_from_device;
/* Decode register values to milli g */
decode_mg(data, &datax, &datay, &dataz);
if (accl_debug)
pr_info("%s:Reporting x= %d, y= %d, z= %d\n",
__func__, datax, datay, dataz);
input_report_abs(data->input_dev, ABS_X, datax);
input_report_abs(data->input_dev, ABS_Y, datay);
input_report_abs(data->input_dev, ABS_Z, dataz);
input_sync(data->input_dev);
not_from_device:
mutex_unlock(&data->mutex);
if (data->client->irq)
enable_irq(data->client->irq);
}
static void cma3000_input_work_func(struct work_struct *work)
{
struct cma3000_accl_data *cma = container_of((struct delayed_work *)work,
struct cma3000_accl_data,
input_work);
if (cma->pdata.mode > CMAMODE_DEFAULT &&
cma->pdata.mode < CMAMODE_POFF) {
cma3000_read_report_data(cma);
if (!cma->client->irq)
schedule_delayed_work(&cma->input_work,
msecs_to_jiffies(cma->req_poll_rate));
}
}
static irqreturn_t cma3000_isr(int irq, void *dev_id)
{
struct cma3000_accl_data *cma = dev_id;
disable_irq_nosync(irq);
schedule_delayed_work(&cma->input_work, 0);
return IRQ_HANDLED;
}
static int cma3000_reset(struct cma3000_accl_data *data)
{
int ret;
/* Reset sequence */
cma3000_set(data, CMA3000_RSTR, 0x02, "Reset");
cma3000_set(data, CMA3000_RSTR, 0x0A, "Reset");
cma3000_set(data, CMA3000_RSTR, 0x04, "Reset");
/* Settling time delay */
mdelay(10);
ret = cma3000_read(data, CMA3000_STATUS, "Status");
if (ret < 0) {
dev_err(&data->client->dev, "Reset failed\n");
return ret;
} else if (ret & CMA3000_STATUS_PERR) {
dev_err(&data->client->dev, "Parity Error\n");
return -EIO;
} else {
return 0;
}
}
int cma3000_poweron(struct cma3000_accl_data *data)
{
uint8_t ctrl = 0, mdthr, mdfftmr, ffthr, mode;
int g_range, ret;
g_range = data->pdata.g_range;
mode = data->pdata.mode;
mdthr = data->pdata.mdthr;
mdfftmr = data->pdata.mdfftmr;
ffthr = data->pdata.ffthr;
if (mode < CMAMODE_DEFAULT || mode > CMAMODE_POFF) {
data->pdata.mode = CMAMODE_MOTDET;
mode = data->pdata.mode;
dev_info(&data->client->dev,
"Invalid mode specified, assuming Motion Detect\n");
}
if (g_range == CMARANGE_2G) {
ctrl = (mode << 1) | CMA3000_RANGE2G;
} else if (g_range == CMARANGE_8G) {
ctrl = (mode << 1) | CMA3000_RANGE8G;
} else {
dev_info(&data->client->dev,
"Invalid G range specified, assuming 8G\n");
ctrl = (mode << 1) | CMA3000_RANGE8G;
data->pdata.g_range = CMARANGE_8G;
}
#ifdef CONFIG_INPUT_CMA3000_I2C
ctrl |= CMA3000_BUSI2C;
#endif
/* Disable IEQ if not configured for irq mode*/
if (!data->client->irq)
ctrl |= 0x1;
cma3000_set(data, CMA3000_MDTHR, mdthr, "Motion Detect Threshold");
cma3000_set(data, CMA3000_MDFFTMR, mdfftmr, "Time register");
cma3000_set(data, CMA3000_FFTHR, ffthr, "Free fall threshold");
ret = cma3000_set(data, CMA3000_CTRL, ctrl, "Mode setting");
if (ret < 0)
return -EIO;
msleep(CMA3000_SETDELAY);
return 0;
}
int cma3000_poweroff(struct cma3000_accl_data *data)
{
int ret;
ret = cma3000_set_mode(data, CMAMODE_POFF);
msleep(CMA3000_SETDELAY);
return ret;
}
int cma3000_init(struct cma3000_accl_data *data)
{
int ret = 0, fuzz_x, fuzz_y, fuzz_z, g_range;
uint32_t irqflags;
uint8_t ctrl;
INIT_DELAYED_WORK(&data->input_work, cma3000_input_work_func);
if (data->client->dev.platform_data == NULL) {
dev_err(&data->client->dev, "platform data not found\n");
goto err_op2_failed;
}
memcpy(&(data->pdata), data->client->dev.platform_data,
sizeof(struct cma3000_platform_data));
ret = cma3000_reset(data);
if (ret)
goto err_op2_failed;
ret = cma3000_read(data, CMA3000_REVID, "Revid");
if (ret < 0)
goto err_op2_failed;
pr_info("CMA3000 Acclerometer : Revision %x\n", ret);
/* Bring it out of default power down state */
ret = cma3000_poweron(data);
if (ret < 0)
goto err_op2_failed;
data->req_poll_rate = data->pdata.def_poll_rate;
fuzz_x = data->pdata.fuzz_x;
fuzz_y = data->pdata.fuzz_y;
fuzz_z = data->pdata.fuzz_z;
g_range = data->pdata.g_range;
irqflags = data->pdata.irqflags;
data->input_dev = input_allocate_device();
if (data->input_dev == NULL) {
ret = -ENOMEM;
dev_err(&data->client->dev,
"Failed to allocate input device\n");
goto err_op2_failed;
}
data->input_dev->name = "cma3000-acclerometer";
#ifdef CONFIG_INPUT_CMA3000_I2C
data->input_dev->id.bustype = BUS_I2C;
#endif
__set_bit(EV_ABS, data->input_dev->evbit);
__set_bit(EV_MSC, data->input_dev->evbit);
input_set_abs_params(data->input_dev, ABS_X, -g_range,
g_range, fuzz_x, 0);
input_set_abs_params(data->input_dev, ABS_Y, -g_range,
g_range, fuzz_y, 0);
input_set_abs_params(data->input_dev, ABS_Z, -g_range,
g_range, fuzz_z, 0);
input_set_abs_params(data->input_dev, ABS_MISC, 0,
1, 0, 0);
ret = input_register_device(data->input_dev);
if (ret) {
dev_err(&data->client->dev,
"Unable to register input device\n");
goto err_op2_failed;
}
mutex_init(&data->mutex);
if (data->client->irq) {
ret = request_irq(data->client->irq, cma3000_isr,
irqflags | IRQF_ONESHOT,
data->client->name, data);
if (ret < 0) {
dev_err(&data->client->dev,
"request_threaded_irq failed\n");
goto err_op1_failed;
}
} else {
/*There is no IRQ set, disable IRQ on CMA*/
ctrl = cma3000_read(data, CMA3000_CTRL, "Status");
ctrl |= 0x1;
cma3000_set(data, CMA3000_CTRL, ctrl, "Disable IRQ");
}
ret = sysfs_create_group(&data->client->dev.kobj, &cma3000_attr_group);
if (ret) {
dev_err(&data->client->dev,
"failed to create sysfs entries\n");
goto err_op1_failed;
}
cma3000_set_mode(data, CMAMODE_POFF);
return 0;
err_op1_failed:
mutex_destroy(&data->mutex);
input_unregister_device(data->input_dev);
err_op2_failed:
if (data != NULL) {
if (data->input_dev != NULL)
input_free_device(data->input_dev);
}
return ret;
}
int cma3000_exit(struct cma3000_accl_data *data)
{
int ret;
ret = cma3000_poweroff(data);
if (data->client->irq)
free_irq(data->client->irq, data);
mutex_destroy(&data->mutex);
input_unregister_device(data->input_dev);
input_free_device(data->input_dev);
sysfs_remove_group(&data->client->dev.kobj, &cma3000_attr_group);
return ret;
}
|