10 #include <linux/device.h>
13 #include <linux/input.h>
16 #include <linux/slab.h>
19 #include <linux/module.h>
25 #define THRESH_TAP 0x1D
32 #define THRESH_ACT 0x24
33 #define THRESH_INACT 0x25
34 #define TIME_INACT 0x26
35 #define ACT_INACT_CTL 0x27
37 #define THRESH_FF 0x28
40 #define ACT_TAP_STATUS 0x2B
42 #define POWER_CTL 0x2D
43 #define INT_ENABLE 0x2E
45 #define INT_SOURCE 0x30
46 #define DATA_FORMAT 0x31
54 #define FIFO_STATUS 0x39
57 #define ORIENT_CONF 0x3B
61 #define ID_ADXL345 0xE5
62 #define ID_ADXL346 0xE6
65 #define DATA_READY (1 << 7)
66 #define SINGLE_TAP (1 << 6)
67 #define DOUBLE_TAP (1 << 5)
68 #define ACTIVITY (1 << 4)
69 #define INACTIVITY (1 << 3)
70 #define FREE_FALL (1 << 2)
71 #define WATERMARK (1 << 1)
72 #define OVERRUN (1 << 0)
75 #define ACT_ACDC (1 << 7)
76 #define ACT_X_EN (1 << 6)
77 #define ACT_Y_EN (1 << 5)
78 #define ACT_Z_EN (1 << 4)
79 #define INACT_ACDC (1 << 3)
80 #define INACT_X_EN (1 << 2)
81 #define INACT_Y_EN (1 << 1)
82 #define INACT_Z_EN (1 << 0)
85 #define SUPPRESS (1 << 3)
86 #define TAP_X_EN (1 << 2)
87 #define TAP_Y_EN (1 << 1)
88 #define TAP_Z_EN (1 << 0)
91 #define ACT_X_SRC (1 << 6)
92 #define ACT_Y_SRC (1 << 5)
93 #define ACT_Z_SRC (1 << 4)
94 #define ASLEEP (1 << 3)
95 #define TAP_X_SRC (1 << 2)
96 #define TAP_Y_SRC (1 << 1)
97 #define TAP_Z_SRC (1 << 0)
100 #define LOW_POWER (1 << 4)
101 #define RATE(x) ((x) & 0xF)
104 #define PCTL_LINK (1 << 5)
105 #define PCTL_AUTO_SLEEP (1 << 4)
106 #define PCTL_MEASURE (1 << 3)
107 #define PCTL_SLEEP (1 << 2)
108 #define PCTL_WAKEUP(x) ((x) & 0x3)
111 #define SELF_TEST (1 << 7)
113 #define INT_INVERT (1 << 5)
114 #define FULL_RES (1 << 3)
115 #define JUSTIFY (1 << 2)
116 #define RANGE(x) ((x) & 0x3)
117 #define RANGE_PM_2g 0
118 #define RANGE_PM_4g 1
119 #define RANGE_PM_8g 2
120 #define RANGE_PM_16g 3
126 #define ADXL_FULLRES_MAX_VAL 4096
132 #define ADXL_FIXEDRES_MAX_VAL 512
135 #define FIFO_MODE(x) (((x) & 0x3) << 6)
136 #define FIFO_BYPASS 0
138 #define FIFO_STREAM 2
139 #define FIFO_TRIGGER 3
140 #define TRIGGER (1 << 5)
141 #define SAMPLES(x) ((x) & 0x1F)
144 #define FIFO_TRIG (1 << 7)
145 #define ENTRIES(x) ((x) & 0x3F)
148 #define XSIGN (1 << 6)
149 #define YSIGN (1 << 5)
150 #define ZSIGN (1 << 4)
151 #define XTAP (1 << 3)
152 #define YTAP (1 << 2)
153 #define ZTAP (1 << 1)
156 #define ORIENT_DEADZONE(x) (((x) & 0x7) << 4)
157 #define ORIENT_DIVISOR(x) ((x) & 0x7)
160 #define ADXL346_2D_VALID (1 << 6)
161 #define ADXL346_2D_ORIENT(x) (((x) & 0x3) >> 4)
162 #define ADXL346_3D_VALID (1 << 3)
163 #define ADXL346_3D_ORIENT(x) ((x) & 0x7)
164 #define ADXL346_2D_PORTRAIT_POS 0
165 #define ADXL346_2D_PORTRAIT_NEG 1
166 #define ADXL346_2D_LANDSCAPE_POS 2
167 #define ADXL346_2D_LANDSCAPE_NEG 3
169 #define ADXL346_3D_FRONT 3
170 #define ADXL346_3D_BACK 4
171 #define ADXL346_3D_RIGHT 2
172 #define ADXL346_3D_LEFT 5
173 #define ADXL346_3D_TOP 1
174 #define ADXL346_3D_BOTTOM 6
178 #define ADXL_X_AXIS 0
179 #define ADXL_Y_AXIS 1
180 #define ADXL_Z_AXIS 2
182 #define AC_READ(ac, reg) ((ac)->bops->read((ac)->dev, reg))
183 #define AC_WRITE(ac, reg, val) ((ac)->bops->write((ac)->dev, reg, val))
219 .act_axis_control = 0xFF,
220 .activity_threshold = 6,
221 .inactivity_threshold = 4,
222 .inactivity_time = 3,
223 .free_fall_threshold = 8,
224 .free_fall_time = 0x20,
257 static void adxl34x_service_ev_fifo(
struct adxl34x *ac)
262 adxl34x_get_triple(ac, &axis);
272 static void adxl34x_report_key_single(
struct input_dev *
input,
int key)
274 input_report_key(input, key,
true);
276 input_report_key(input, key,
false);
279 static void adxl34x_send_key_events(
struct adxl34x *ac,
286 input_report_key(ac->
input,
291 static void adxl34x_do_tap(
struct adxl34x *ac,
294 adxl34x_send_key_events(ac, pdata, status,
true);
295 input_sync(ac->
input);
296 adxl34x_send_key_events(ac, pdata, status,
false);
303 int int_stat, tap_stat, samples, orient, orient_code;
324 adxl34x_do_tap(ac, pdata, tap_stat);
327 adxl34x_do_tap(ac, pdata, tap_stat);
332 input_report_key(ac->
input,
335 input_report_key(ac->
input,
351 adxl34x_report_key_single(ac->
input,
363 adxl34x_report_key_single(ac->
input,
376 for (; samples > 0; samples--) {
377 adxl34x_service_ev_fifo(ac);
397 input_sync(ac->
input);
402 static void __adxl34x_disable(
struct adxl34x *ac)
411 static void __adxl34x_enable(
struct adxl34x *ac)
421 __adxl34x_disable(ac);
434 __adxl34x_enable(ac);
452 const char *buf,
size_t count)
467 __adxl34x_disable(ac);
470 __adxl34x_enable(ac);
490 count =
sprintf(buf,
"%d,%d,%d\n",
501 const char *buf,
size_t count)
529 adxl34x_calibrate_show, adxl34x_calibrate_store);
541 const char *buf,
size_t count)
555 ac->
pdata.data_rate |
576 const char *buf,
size_t count)
602 adxl34x_autosleep_show, adxl34x_autosleep_store);
611 count =
sprintf(buf,
"(%d, %d, %d)\n",
623 const char *buf,
size_t count)
646 static struct attribute *adxl34x_attributes[] = {
647 &dev_attr_disable.attr,
648 &dev_attr_calibrate.attr,
650 &dev_attr_autosleep.attr,
651 &dev_attr_position.attr,
653 &dev_attr_write.attr,
659 .attrs = adxl34x_attributes,
662 static int adxl34x_input_open(
struct input_dev *
input)
664 struct adxl34x *ac = input_get_drvdata(input);
669 __adxl34x_enable(ac);
678 static void adxl34x_input_close(
struct input_dev *input)
680 struct adxl34x *ac = input_get_drvdata(input);
685 __adxl34x_disable(ac);
693 bool fifo_delay_default,
697 struct input_dev *input_dev;
709 input_dev = input_allocate_device();
710 if (!ac || !input_dev) {
720 "No platform data: Using default initialization\n");
721 pdata = &adxl34x_default_init;
727 ac->
input = input_dev;
734 input_dev->name =
"ADXL34x accelerometer";
745 dev_err(dev,
"Failed to probe %s\n", input_dev->
name);
752 input_dev->phys = ac->
phys;
753 input_dev->dev.parent =
dev;
754 input_dev->id.product = ac->
model;
755 input_dev->id.bustype = bops->
bustype;
756 input_dev->open = adxl34x_input_open;
757 input_dev->close = adxl34x_input_close;
759 input_set_drvdata(input_dev, ac);
778 input_set_abs_params(input_dev,
ABS_X, -range, range, 3, 3);
779 input_set_abs_params(input_dev,
ABS_Y, -range, range, 3, 3);
780 input_set_abs_params(input_dev,
ABS_Z, -range, range, 3, 3);
826 err = input_register_device(input_dev);
828 goto err_remove_attr;
862 if (ac->
model == 346 && ac->
pdata.orientation_enable) {
880 ac->
pdata.orientation_enable = 0;
894 input_free_device(input_dev);
905 input_unregister_device(ac->
input);
906 dev_dbg(ac->
dev,
"unregistered accelerometer\n");