1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ADXL345/346 Three-Axis Digital Accelerometers
5 * Enter bugs at http://blackfin.uclinux.org/
7 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
10 #include <linux/device.h>
11 #include <linux/delay.h>
12 #include <linux/input.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/input/adxl34x.h>
18 #include <linux/module.h>
22 /* ADXL345/6 Register Map */
23 #define DEVID 0x00 /* R Device ID */
24 #define THRESH_TAP 0x1D /* R/W Tap threshold */
25 #define OFSX 0x1E /* R/W X-axis offset */
26 #define OFSY 0x1F /* R/W Y-axis offset */
27 #define OFSZ 0x20 /* R/W Z-axis offset */
28 #define DUR 0x21 /* R/W Tap duration */
29 #define LATENT 0x22 /* R/W Tap latency */
30 #define WINDOW 0x23 /* R/W Tap window */
31 #define THRESH_ACT 0x24 /* R/W Activity threshold */
32 #define THRESH_INACT 0x25 /* R/W Inactivity threshold */
33 #define TIME_INACT 0x26 /* R/W Inactivity time */
34 #define ACT_INACT_CTL 0x27 /* R/W Axis enable control for activity and */
35 /* inactivity detection */
36 #define THRESH_FF 0x28 /* R/W Free-fall threshold */
37 #define TIME_FF 0x29 /* R/W Free-fall time */
38 #define TAP_AXES 0x2A /* R/W Axis control for tap/double tap */
39 #define ACT_TAP_STATUS 0x2B /* R Source of tap/double tap */
40 #define BW_RATE 0x2C /* R/W Data rate and power mode control */
41 #define POWER_CTL 0x2D /* R/W Power saving features control */
42 #define INT_ENABLE 0x2E /* R/W Interrupt enable control */
43 #define INT_MAP 0x2F /* R/W Interrupt mapping control */
44 #define INT_SOURCE 0x30 /* R Source of interrupts */
45 #define DATA_FORMAT 0x31 /* R/W Data format control */
46 #define DATAX0 0x32 /* R X-Axis Data 0 */
47 #define DATAX1 0x33 /* R X-Axis Data 1 */
48 #define DATAY0 0x34 /* R Y-Axis Data 0 */
49 #define DATAY1 0x35 /* R Y-Axis Data 1 */
50 #define DATAZ0 0x36 /* R Z-Axis Data 0 */
51 #define DATAZ1 0x37 /* R Z-Axis Data 1 */
52 #define FIFO_CTL 0x38 /* R/W FIFO control */
53 #define FIFO_STATUS 0x39 /* R FIFO status */
54 #define TAP_SIGN 0x3A /* R Sign and source for tap/double tap */
55 /* Orientation ADXL346 only */
56 #define ORIENT_CONF 0x3B /* R/W Orientation configuration */
57 #define ORIENT 0x3C /* R Orientation status */
60 #define ID_ADXL345 0xE5
61 #define ID_ADXL346 0xE6
63 /* INT_ENABLE/INT_MAP/INT_SOURCE Bits */
64 #define DATA_READY (1 << 7)
65 #define SINGLE_TAP (1 << 6)
66 #define DOUBLE_TAP (1 << 5)
67 #define ACTIVITY (1 << 4)
68 #define INACTIVITY (1 << 3)
69 #define FREE_FALL (1 << 2)
70 #define WATERMARK (1 << 1)
71 #define OVERRUN (1 << 0)
73 /* ACT_INACT_CONTROL Bits */
74 #define ACT_ACDC (1 << 7)
75 #define ACT_X_EN (1 << 6)
76 #define ACT_Y_EN (1 << 5)
77 #define ACT_Z_EN (1 << 4)
78 #define INACT_ACDC (1 << 3)
79 #define INACT_X_EN (1 << 2)
80 #define INACT_Y_EN (1 << 1)
81 #define INACT_Z_EN (1 << 0)
84 #define SUPPRESS (1 << 3)
85 #define TAP_X_EN (1 << 2)
86 #define TAP_Y_EN (1 << 1)
87 #define TAP_Z_EN (1 << 0)
89 /* ACT_TAP_STATUS Bits */
90 #define ACT_X_SRC (1 << 6)
91 #define ACT_Y_SRC (1 << 5)
92 #define ACT_Z_SRC (1 << 4)
93 #define ASLEEP (1 << 3)
94 #define TAP_X_SRC (1 << 2)
95 #define TAP_Y_SRC (1 << 1)
96 #define TAP_Z_SRC (1 << 0)
99 #define LOW_POWER (1 << 4)
100 #define RATE(x) ((x) & 0xF)
103 #define PCTL_LINK (1 << 5)
104 #define PCTL_AUTO_SLEEP (1 << 4)
105 #define PCTL_MEASURE (1 << 3)
106 #define PCTL_SLEEP (1 << 2)
107 #define PCTL_WAKEUP(x) ((x) & 0x3)
109 /* DATA_FORMAT Bits */
110 #define SELF_TEST (1 << 7)
112 #define INT_INVERT (1 << 5)
113 #define FULL_RES (1 << 3)
114 #define JUSTIFY (1 << 2)
115 #define RANGE(x) ((x) & 0x3)
116 #define RANGE_PM_2g 0
117 #define RANGE_PM_4g 1
118 #define RANGE_PM_8g 2
119 #define RANGE_PM_16g 3
122 * Maximum value our axis may get in full res mode for the input device
125 #define ADXL_FULLRES_MAX_VAL 4096
128 * Maximum value our axis may get in fixed res mode for the input device
131 #define ADXL_FIXEDRES_MAX_VAL 512
134 #define FIFO_MODE(x) (((x) & 0x3) << 6)
135 #define FIFO_BYPASS 0
137 #define FIFO_STREAM 2
138 #define FIFO_TRIGGER 3
139 #define TRIGGER (1 << 5)
140 #define SAMPLES(x) ((x) & 0x1F)
142 /* FIFO_STATUS Bits */
143 #define FIFO_TRIG (1 << 7)
144 #define ENTRIES(x) ((x) & 0x3F)
146 /* TAP_SIGN Bits ADXL346 only */
147 #define XSIGN (1 << 6)
148 #define YSIGN (1 << 5)
149 #define ZSIGN (1 << 4)
150 #define XTAP (1 << 3)
151 #define YTAP (1 << 2)
152 #define ZTAP (1 << 1)
154 /* ORIENT_CONF ADXL346 only */
155 #define ORIENT_DEADZONE(x) (((x) & 0x7) << 4)
156 #define ORIENT_DIVISOR(x) ((x) & 0x7)
158 /* ORIENT ADXL346 only */
159 #define ADXL346_2D_VALID (1 << 6)
160 #define ADXL346_2D_ORIENT(x) (((x) & 0x30) >> 4)
161 #define ADXL346_3D_VALID (1 << 3)
162 #define ADXL346_3D_ORIENT(x) ((x) & 0x7)
163 #define ADXL346_2D_PORTRAIT_POS 0 /* +X */
164 #define ADXL346_2D_PORTRAIT_NEG 1 /* -X */
165 #define ADXL346_2D_LANDSCAPE_POS 2 /* +Y */
166 #define ADXL346_2D_LANDSCAPE_NEG 3 /* -Y */
168 #define ADXL346_3D_FRONT 3 /* +X */
169 #define ADXL346_3D_BACK 4 /* -X */
170 #define ADXL346_3D_RIGHT 2 /* +Y */
171 #define ADXL346_3D_LEFT 5 /* -Y */
172 #define ADXL346_3D_TOP 1 /* +Z */
173 #define ADXL346_3D_BOTTOM 6 /* -Z */
177 #define ADXL_X_AXIS 0
178 #define ADXL_Y_AXIS 1
179 #define ADXL_Z_AXIS 2
181 #define AC_READ(ac, reg) ((ac)->bops->read((ac)->dev, reg))
182 #define AC_WRITE(ac, reg, val) ((ac)->bops->write((ac)->dev, reg, val))
192 struct input_dev
*input
;
193 struct mutex mutex
; /* reentrant protection for struct */
194 struct adxl34x_platform_data pdata
;
195 struct axis_triple swcal
;
196 struct axis_triple hwcal
;
197 struct axis_triple saved
;
199 unsigned orient2d_saved
;
200 unsigned orient3d_saved
;
201 bool disabled
; /* P: mutex */
202 bool opened
; /* P: mutex */
203 bool suspended
; /* P: mutex */
209 const struct adxl34x_bus_ops
*bops
;
212 static const struct adxl34x_platform_data adxl34x_default_init
= {
217 .tap_axis_control
= ADXL_TAP_X_EN
| ADXL_TAP_Y_EN
| ADXL_TAP_Z_EN
,
218 .act_axis_control
= 0xFF,
219 .activity_threshold
= 6,
220 .inactivity_threshold
= 4,
221 .inactivity_time
= 3,
222 .free_fall_threshold
= 8,
223 .free_fall_time
= 0x20,
225 .data_range
= ADXL_FULL_RES
,
228 .ev_code_x
= ABS_X
, /* EV_REL */
229 .ev_code_y
= ABS_Y
, /* EV_REL */
230 .ev_code_z
= ABS_Z
, /* EV_REL */
232 .ev_code_tap
= {BTN_TOUCH
, BTN_TOUCH
, BTN_TOUCH
}, /* EV_KEY {x,y,z} */
233 .power_mode
= ADXL_AUTO_SLEEP
| ADXL_LINK
,
234 .fifo_mode
= ADXL_FIFO_STREAM
,
238 static void adxl34x_get_triple(struct adxl34x
*ac
, struct axis_triple
*axis
)
242 ac
->bops
->read_block(ac
->dev
, DATAX0
, DATAZ1
- DATAX0
+ 1, buf
);
244 guard(mutex
)(&ac
->mutex
);
246 ac
->saved
.x
= (s16
) le16_to_cpu(buf
[0]);
247 axis
->x
= ac
->saved
.x
;
249 ac
->saved
.y
= (s16
) le16_to_cpu(buf
[1]);
250 axis
->y
= ac
->saved
.y
;
252 ac
->saved
.z
= (s16
) le16_to_cpu(buf
[2]);
253 axis
->z
= ac
->saved
.z
;
256 static void adxl34x_service_ev_fifo(struct adxl34x
*ac
)
258 struct adxl34x_platform_data
*pdata
= &ac
->pdata
;
259 struct axis_triple axis
;
261 adxl34x_get_triple(ac
, &axis
);
263 input_event(ac
->input
, pdata
->ev_type
, pdata
->ev_code_x
,
264 axis
.x
- ac
->swcal
.x
);
265 input_event(ac
->input
, pdata
->ev_type
, pdata
->ev_code_y
,
266 axis
.y
- ac
->swcal
.y
);
267 input_event(ac
->input
, pdata
->ev_type
, pdata
->ev_code_z
,
268 axis
.z
- ac
->swcal
.z
);
271 static void adxl34x_report_key_single(struct input_dev
*input
, int key
)
273 input_report_key(input
, key
, true);
275 input_report_key(input
, key
, false);
278 static void adxl34x_send_key_events(struct adxl34x
*ac
,
279 struct adxl34x_platform_data
*pdata
, int status
, int press
)
283 for (i
= ADXL_X_AXIS
; i
<= ADXL_Z_AXIS
; i
++) {
284 if (status
& (1 << (ADXL_Z_AXIS
- i
)))
285 input_report_key(ac
->input
,
286 pdata
->ev_code_tap
[i
], press
);
290 static void adxl34x_do_tap(struct adxl34x
*ac
,
291 struct adxl34x_platform_data
*pdata
, int status
)
293 adxl34x_send_key_events(ac
, pdata
, status
, true);
294 input_sync(ac
->input
);
295 adxl34x_send_key_events(ac
, pdata
, status
, false);
298 static irqreturn_t
adxl34x_irq(int irq
, void *handle
)
300 struct adxl34x
*ac
= handle
;
301 struct adxl34x_platform_data
*pdata
= &ac
->pdata
;
302 int int_stat
, tap_stat
, samples
, orient
, orient_code
;
305 * ACT_TAP_STATUS should be read before clearing the interrupt
306 * Avoid reading ACT_TAP_STATUS in case TAP detection is disabled
309 if (pdata
->tap_axis_control
& (TAP_X_EN
| TAP_Y_EN
| TAP_Z_EN
))
310 tap_stat
= AC_READ(ac
, ACT_TAP_STATUS
);
314 int_stat
= AC_READ(ac
, INT_SOURCE
);
316 if (int_stat
& FREE_FALL
)
317 adxl34x_report_key_single(ac
->input
, pdata
->ev_code_ff
);
319 if (int_stat
& OVERRUN
)
320 dev_dbg(ac
->dev
, "OVERRUN\n");
322 if (int_stat
& (SINGLE_TAP
| DOUBLE_TAP
)) {
323 adxl34x_do_tap(ac
, pdata
, tap_stat
);
325 if (int_stat
& DOUBLE_TAP
)
326 adxl34x_do_tap(ac
, pdata
, tap_stat
);
329 if (pdata
->ev_code_act_inactivity
) {
330 if (int_stat
& ACTIVITY
)
331 input_report_key(ac
->input
,
332 pdata
->ev_code_act_inactivity
, 1);
333 if (int_stat
& INACTIVITY
)
334 input_report_key(ac
->input
,
335 pdata
->ev_code_act_inactivity
, 0);
339 * ORIENTATION SENSING ADXL346 only
341 if (pdata
->orientation_enable
) {
342 orient
= AC_READ(ac
, ORIENT
);
343 if ((pdata
->orientation_enable
& ADXL_EN_ORIENTATION_2D
) &&
344 (orient
& ADXL346_2D_VALID
)) {
346 orient_code
= ADXL346_2D_ORIENT(orient
);
347 /* Report orientation only when it changes */
348 if (ac
->orient2d_saved
!= orient_code
) {
349 ac
->orient2d_saved
= orient_code
;
350 adxl34x_report_key_single(ac
->input
,
351 pdata
->ev_codes_orient_2d
[orient_code
]);
355 if ((pdata
->orientation_enable
& ADXL_EN_ORIENTATION_3D
) &&
356 (orient
& ADXL346_3D_VALID
)) {
358 orient_code
= ADXL346_3D_ORIENT(orient
) - 1;
359 /* Report orientation only when it changes */
360 if (ac
->orient3d_saved
!= orient_code
) {
361 ac
->orient3d_saved
= orient_code
;
362 adxl34x_report_key_single(ac
->input
,
363 pdata
->ev_codes_orient_3d
[orient_code
]);
368 if (int_stat
& (DATA_READY
| WATERMARK
)) {
370 if (pdata
->fifo_mode
)
371 samples
= ENTRIES(AC_READ(ac
, FIFO_STATUS
)) + 1;
375 for (; samples
> 0; samples
--) {
376 adxl34x_service_ev_fifo(ac
);
378 * To ensure that the FIFO has
379 * completely popped, there must be at least 5 us between
380 * the end of reading the data registers, signified by the
381 * transition to register 0x38 from 0x37 or the CS pin
382 * going high, and the start of new reads of the FIFO or
383 * reading the FIFO_STATUS register. For SPI operation at
384 * 1.5 MHz or lower, the register addressing portion of the
385 * transmission is sufficient delay to ensure the FIFO has
386 * completely popped. It is necessary for SPI operation
387 * greater than 1.5 MHz to de-assert the CS pin to ensure a
388 * total of 5 us, which is at most 3.4 us at 5 MHz
391 if (ac
->fifo_delay
&& (samples
> 1))
396 input_sync(ac
->input
);
401 static void __adxl34x_disable(struct adxl34x
*ac
)
404 * A '0' places the ADXL34x into standby mode
405 * with minimum power consumption.
407 AC_WRITE(ac
, POWER_CTL
, 0);
410 static void __adxl34x_enable(struct adxl34x
*ac
)
412 AC_WRITE(ac
, POWER_CTL
, ac
->pdata
.power_mode
| PCTL_MEASURE
);
415 static int adxl34x_suspend(struct device
*dev
)
417 struct adxl34x
*ac
= dev_get_drvdata(dev
);
419 guard(mutex
)(&ac
->mutex
);
421 if (!ac
->suspended
&& !ac
->disabled
&& ac
->opened
)
422 __adxl34x_disable(ac
);
424 ac
->suspended
= true;
429 static int adxl34x_resume(struct device
*dev
)
431 struct adxl34x
*ac
= dev_get_drvdata(dev
);
433 guard(mutex
)(&ac
->mutex
);
435 if (ac
->suspended
&& !ac
->disabled
&& ac
->opened
)
436 __adxl34x_enable(ac
);
438 ac
->suspended
= false;
443 static ssize_t
adxl34x_disable_show(struct device
*dev
,
444 struct device_attribute
*attr
, char *buf
)
446 struct adxl34x
*ac
= dev_get_drvdata(dev
);
448 return sprintf(buf
, "%u\n", ac
->disabled
);
451 static ssize_t
adxl34x_disable_store(struct device
*dev
,
452 struct device_attribute
*attr
,
453 const char *buf
, size_t count
)
455 struct adxl34x
*ac
= dev_get_drvdata(dev
);
459 error
= kstrtouint(buf
, 10, &val
);
463 guard(mutex
)(&ac
->mutex
);
465 if (!ac
->suspended
&& ac
->opened
) {
468 __adxl34x_disable(ac
);
471 __adxl34x_enable(ac
);
475 ac
->disabled
= !!val
;
480 static DEVICE_ATTR(disable
, 0664, adxl34x_disable_show
, adxl34x_disable_store
);
482 static ssize_t
adxl34x_calibrate_show(struct device
*dev
,
483 struct device_attribute
*attr
, char *buf
)
485 struct adxl34x
*ac
= dev_get_drvdata(dev
);
487 guard(mutex
)(&ac
->mutex
);
489 return sprintf(buf
, "%d,%d,%d\n",
490 ac
->hwcal
.x
* 4 + ac
->swcal
.x
,
491 ac
->hwcal
.y
* 4 + ac
->swcal
.y
,
492 ac
->hwcal
.z
* 4 + ac
->swcal
.z
);
495 static ssize_t
adxl34x_calibrate_store(struct device
*dev
,
496 struct device_attribute
*attr
,
497 const char *buf
, size_t count
)
499 struct adxl34x
*ac
= dev_get_drvdata(dev
);
502 * Hardware offset calibration has a resolution of 15.6 mg/LSB.
503 * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
506 guard(mutex
)(&ac
->mutex
);
508 ac
->hwcal
.x
-= (ac
->saved
.x
/ 4);
509 ac
->swcal
.x
= ac
->saved
.x
% 4;
511 ac
->hwcal
.y
-= (ac
->saved
.y
/ 4);
512 ac
->swcal
.y
= ac
->saved
.y
% 4;
514 ac
->hwcal
.z
-= (ac
->saved
.z
/ 4);
515 ac
->swcal
.z
= ac
->saved
.z
% 4;
517 AC_WRITE(ac
, OFSX
, (s8
) ac
->hwcal
.x
);
518 AC_WRITE(ac
, OFSY
, (s8
) ac
->hwcal
.y
);
519 AC_WRITE(ac
, OFSZ
, (s8
) ac
->hwcal
.z
);
524 static DEVICE_ATTR(calibrate
, 0664,
525 adxl34x_calibrate_show
, adxl34x_calibrate_store
);
527 static ssize_t
adxl34x_rate_show(struct device
*dev
,
528 struct device_attribute
*attr
, char *buf
)
530 struct adxl34x
*ac
= dev_get_drvdata(dev
);
532 return sprintf(buf
, "%u\n", RATE(ac
->pdata
.data_rate
));
535 static ssize_t
adxl34x_rate_store(struct device
*dev
,
536 struct device_attribute
*attr
,
537 const char *buf
, size_t count
)
539 struct adxl34x
*ac
= dev_get_drvdata(dev
);
543 error
= kstrtou8(buf
, 10, &val
);
547 guard(mutex
)(&ac
->mutex
);
549 ac
->pdata
.data_rate
= RATE(val
);
550 AC_WRITE(ac
, BW_RATE
,
551 ac
->pdata
.data_rate
|
552 (ac
->pdata
.low_power_mode
? LOW_POWER
: 0));
557 static DEVICE_ATTR(rate
, 0664, adxl34x_rate_show
, adxl34x_rate_store
);
559 static ssize_t
adxl34x_autosleep_show(struct device
*dev
,
560 struct device_attribute
*attr
, char *buf
)
562 struct adxl34x
*ac
= dev_get_drvdata(dev
);
564 return sprintf(buf
, "%u\n",
565 ac
->pdata
.power_mode
& (PCTL_AUTO_SLEEP
| PCTL_LINK
) ? 1 : 0);
568 static ssize_t
adxl34x_autosleep_store(struct device
*dev
,
569 struct device_attribute
*attr
,
570 const char *buf
, size_t count
)
572 struct adxl34x
*ac
= dev_get_drvdata(dev
);
576 error
= kstrtouint(buf
, 10, &val
);
580 guard(mutex
)(&ac
->mutex
);
583 ac
->pdata
.power_mode
|= (PCTL_AUTO_SLEEP
| PCTL_LINK
);
585 ac
->pdata
.power_mode
&= ~(PCTL_AUTO_SLEEP
| PCTL_LINK
);
587 if (!ac
->disabled
&& !ac
->suspended
&& ac
->opened
)
588 AC_WRITE(ac
, POWER_CTL
, ac
->pdata
.power_mode
| PCTL_MEASURE
);
593 static DEVICE_ATTR(autosleep
, 0664,
594 adxl34x_autosleep_show
, adxl34x_autosleep_store
);
596 static ssize_t
adxl34x_position_show(struct device
*dev
,
597 struct device_attribute
*attr
, char *buf
)
599 struct adxl34x
*ac
= dev_get_drvdata(dev
);
601 guard(mutex
)(&ac
->mutex
);
603 return sprintf(buf
, "(%d, %d, %d)\n",
604 ac
->saved
.x
, ac
->saved
.y
, ac
->saved
.z
);
607 static DEVICE_ATTR(position
, S_IRUGO
, adxl34x_position_show
, NULL
);
610 static ssize_t
adxl34x_write_store(struct device
*dev
,
611 struct device_attribute
*attr
,
612 const char *buf
, size_t count
)
614 struct adxl34x
*ac
= dev_get_drvdata(dev
);
619 * This allows basic ADXL register write access for debug purposes.
621 error
= kstrtouint(buf
, 16, &val
);
625 guard(mutex
)(&ac
->mutex
);
626 AC_WRITE(ac
, val
>> 8, val
& 0xFF);
631 static DEVICE_ATTR(write
, 0664, NULL
, adxl34x_write_store
);
634 static struct attribute
*adxl34x_attributes
[] = {
635 &dev_attr_disable
.attr
,
636 &dev_attr_calibrate
.attr
,
638 &dev_attr_autosleep
.attr
,
639 &dev_attr_position
.attr
,
641 &dev_attr_write
.attr
,
646 static const struct attribute_group adxl34x_attr_group
= {
647 .attrs
= adxl34x_attributes
,
650 const struct attribute_group
*adxl34x_groups
[] = {
654 EXPORT_SYMBOL_GPL(adxl34x_groups
);
656 static int adxl34x_input_open(struct input_dev
*input
)
658 struct adxl34x
*ac
= input_get_drvdata(input
);
660 guard(mutex
)(&ac
->mutex
);
662 if (!ac
->suspended
&& !ac
->disabled
)
663 __adxl34x_enable(ac
);
670 static void adxl34x_input_close(struct input_dev
*input
)
672 struct adxl34x
*ac
= input_get_drvdata(input
);
674 guard(mutex
)(&ac
->mutex
);
676 if (!ac
->suspended
&& !ac
->disabled
)
677 __adxl34x_disable(ac
);
682 struct adxl34x
*adxl34x_probe(struct device
*dev
, int irq
,
683 bool fifo_delay_default
,
684 const struct adxl34x_bus_ops
*bops
)
687 struct input_dev
*input_dev
;
688 const struct adxl34x_platform_data
*pdata
;
693 dev_err(dev
, "no IRQ?\n");
694 return ERR_PTR(-ENODEV
);
697 ac
= devm_kzalloc(dev
, sizeof(*ac
), GFP_KERNEL
);
699 return ERR_PTR(-ENOMEM
);
701 input_dev
= devm_input_allocate_device(dev
);
703 return ERR_PTR(-ENOMEM
);
705 ac
->fifo_delay
= fifo_delay_default
;
707 pdata
= dev_get_platdata(dev
);
710 "No platform data: Using default initialization\n");
711 pdata
= &adxl34x_default_init
;
717 ac
->input
= input_dev
;
722 mutex_init(&ac
->mutex
);
724 input_dev
->name
= "ADXL34x accelerometer";
725 revid
= AC_READ(ac
, DEVID
);
735 dev_err(dev
, "Failed to probe %s\n", input_dev
->name
);
736 return ERR_PTR(-ENODEV
);
739 snprintf(ac
->phys
, sizeof(ac
->phys
), "%s/input0", dev_name(dev
));
741 input_dev
->phys
= ac
->phys
;
742 input_dev
->id
.product
= ac
->model
;
743 input_dev
->id
.bustype
= bops
->bustype
;
744 input_dev
->open
= adxl34x_input_open
;
745 input_dev
->close
= adxl34x_input_close
;
747 input_set_drvdata(input_dev
, ac
);
749 if (ac
->pdata
.ev_type
== EV_REL
) {
750 input_set_capability(input_dev
, EV_REL
, REL_X
);
751 input_set_capability(input_dev
, EV_REL
, REL_Y
);
752 input_set_capability(input_dev
, EV_REL
, REL_Z
);
755 if (pdata
->data_range
& FULL_RES
)
756 range
= ADXL_FULLRES_MAX_VAL
; /* Signed 13-bit */
758 range
= ADXL_FIXEDRES_MAX_VAL
; /* Signed 10-bit */
760 input_set_abs_params(input_dev
, ABS_X
, -range
, range
, 3, 3);
761 input_set_abs_params(input_dev
, ABS_Y
, -range
, range
, 3, 3);
762 input_set_abs_params(input_dev
, ABS_Z
, -range
, range
, 3, 3);
765 input_set_capability(input_dev
, EV_KEY
, pdata
->ev_code_tap
[ADXL_X_AXIS
]);
766 input_set_capability(input_dev
, EV_KEY
, pdata
->ev_code_tap
[ADXL_Y_AXIS
]);
767 input_set_capability(input_dev
, EV_KEY
, pdata
->ev_code_tap
[ADXL_Z_AXIS
]);
769 if (pdata
->ev_code_ff
) {
770 ac
->int_mask
= FREE_FALL
;
771 input_set_capability(input_dev
, EV_KEY
, pdata
->ev_code_ff
);
774 if (pdata
->ev_code_act_inactivity
)
775 input_set_capability(input_dev
, EV_KEY
,
776 pdata
->ev_code_act_inactivity
);
778 ac
->int_mask
|= ACTIVITY
| INACTIVITY
;
780 if (pdata
->watermark
) {
781 ac
->int_mask
|= WATERMARK
;
782 if (FIFO_MODE(pdata
->fifo_mode
) == FIFO_BYPASS
)
783 ac
->pdata
.fifo_mode
|= FIFO_STREAM
;
785 ac
->int_mask
|= DATA_READY
;
788 if (pdata
->tap_axis_control
& (TAP_X_EN
| TAP_Y_EN
| TAP_Z_EN
))
789 ac
->int_mask
|= SINGLE_TAP
| DOUBLE_TAP
;
791 if (FIFO_MODE(pdata
->fifo_mode
) == FIFO_BYPASS
)
792 ac
->fifo_delay
= false;
794 AC_WRITE(ac
, POWER_CTL
, 0);
796 error
= devm_request_threaded_irq(dev
, ac
->irq
, NULL
, adxl34x_irq
,
797 IRQF_ONESHOT
, dev_name(dev
), ac
);
799 dev_err(dev
, "irq %d busy?\n", ac
->irq
);
800 return ERR_PTR(error
);
803 error
= input_register_device(input_dev
);
805 return ERR_PTR(error
);
807 AC_WRITE(ac
, OFSX
, pdata
->x_axis_offset
);
808 ac
->hwcal
.x
= pdata
->x_axis_offset
;
809 AC_WRITE(ac
, OFSY
, pdata
->y_axis_offset
);
810 ac
->hwcal
.y
= pdata
->y_axis_offset
;
811 AC_WRITE(ac
, OFSZ
, pdata
->z_axis_offset
);
812 ac
->hwcal
.z
= pdata
->z_axis_offset
;
813 AC_WRITE(ac
, THRESH_TAP
, pdata
->tap_threshold
);
814 AC_WRITE(ac
, DUR
, pdata
->tap_duration
);
815 AC_WRITE(ac
, LATENT
, pdata
->tap_latency
);
816 AC_WRITE(ac
, WINDOW
, pdata
->tap_window
);
817 AC_WRITE(ac
, THRESH_ACT
, pdata
->activity_threshold
);
818 AC_WRITE(ac
, THRESH_INACT
, pdata
->inactivity_threshold
);
819 AC_WRITE(ac
, TIME_INACT
, pdata
->inactivity_time
);
820 AC_WRITE(ac
, THRESH_FF
, pdata
->free_fall_threshold
);
821 AC_WRITE(ac
, TIME_FF
, pdata
->free_fall_time
);
822 AC_WRITE(ac
, TAP_AXES
, pdata
->tap_axis_control
);
823 AC_WRITE(ac
, ACT_INACT_CTL
, pdata
->act_axis_control
);
824 AC_WRITE(ac
, BW_RATE
, RATE(ac
->pdata
.data_rate
) |
825 (pdata
->low_power_mode
? LOW_POWER
: 0));
826 AC_WRITE(ac
, DATA_FORMAT
, pdata
->data_range
);
827 AC_WRITE(ac
, FIFO_CTL
, FIFO_MODE(pdata
->fifo_mode
) |
828 SAMPLES(pdata
->watermark
));
830 if (pdata
->use_int2
) {
831 /* Map all INTs to INT2 */
832 AC_WRITE(ac
, INT_MAP
, ac
->int_mask
| OVERRUN
);
834 /* Map all INTs to INT1 */
835 AC_WRITE(ac
, INT_MAP
, 0);
838 if (ac
->model
== 346 && ac
->pdata
.orientation_enable
) {
839 AC_WRITE(ac
, ORIENT_CONF
,
840 ORIENT_DEADZONE(ac
->pdata
.deadzone_angle
) |
841 ORIENT_DIVISOR(ac
->pdata
.divisor_length
));
843 ac
->orient2d_saved
= 1234;
844 ac
->orient3d_saved
= 1234;
846 if (pdata
->orientation_enable
& ADXL_EN_ORIENTATION_3D
)
847 for (i
= 0; i
< ARRAY_SIZE(pdata
->ev_codes_orient_3d
); i
++)
848 input_set_capability(input_dev
, EV_KEY
,
849 pdata
->ev_codes_orient_3d
[i
]);
851 if (pdata
->orientation_enable
& ADXL_EN_ORIENTATION_2D
)
852 for (i
= 0; i
< ARRAY_SIZE(pdata
->ev_codes_orient_2d
); i
++)
853 input_set_capability(input_dev
, EV_KEY
,
854 pdata
->ev_codes_orient_2d
[i
]);
856 ac
->pdata
.orientation_enable
= 0;
859 AC_WRITE(ac
, INT_ENABLE
, ac
->int_mask
| OVERRUN
);
861 ac
->pdata
.power_mode
&= (PCTL_AUTO_SLEEP
| PCTL_LINK
);
865 EXPORT_SYMBOL_GPL(adxl34x_probe
);
867 EXPORT_GPL_SIMPLE_DEV_PM_OPS(adxl34x_pm
, adxl34x_suspend
, adxl34x_resume
);
869 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
870 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
871 MODULE_LICENSE("GPL");