Input: adxl34x - add support for ADXL346 orientation sensing
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / misc / adxl34x.c
blob77fb4098705944587d0e6d50b302e81e0dc6b96d
1 /*
2 * ADXL345/346 Three-Axis Digital Accelerometers
4 * Enter bugs at http://blackfin.uclinux.org/
6 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
8 */
10 #include <linux/device.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/input.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/slab.h>
17 #include <linux/workqueue.h>
18 #include <linux/input/adxl34x.h>
20 #include "adxl34x.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 */
59 /* DEVIDs */
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)
83 /* TAP_AXES Bits */
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)
98 /* BW_RATE Bits */
99 #define LOW_POWER (1 << 4)
100 #define RATE(x) ((x) & 0xF)
102 /* POWER_CTL Bits */
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)
111 #define SPI (1 << 6)
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
123 * (signed 13 bits)
125 #define ADXL_FULLRES_MAX_VAL 4096
128 * Maximum value our axis may get in fixed res mode for the input device
129 * (signed 10 bits)
131 #define ADXL_FIXEDRES_MAX_VAL 512
133 /* FIFO_CTL Bits */
134 #define FIFO_MODE(x) (((x) & 0x3) << 6)
135 #define FIFO_BYPASS 0
136 #define FIFO_FIFO 1
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) & 0x3) >> 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 */
175 #undef ADXL_DEBUG
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))
184 struct axis_triple {
185 int x;
186 int y;
187 int z;
190 struct adxl34x {
191 struct device *dev;
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;
198 char phys[32];
199 unsigned orient2d_saved;
200 unsigned orient3d_saved;
201 bool disabled; /* P: mutex */
202 bool opened; /* P: mutex */
203 bool fifo_delay;
204 int irq;
205 unsigned model;
206 unsigned int_mask;
208 const struct adxl34x_bus_ops *bops;
211 static const struct adxl34x_platform_data adxl34x_default_init = {
212 .tap_threshold = 35,
213 .tap_duration = 3,
214 .tap_latency = 20,
215 .tap_window = 20,
216 .tap_axis_control = ADXL_TAP_X_EN | ADXL_TAP_Y_EN | ADXL_TAP_Z_EN,
217 .act_axis_control = 0xFF,
218 .activity_threshold = 6,
219 .inactivity_threshold = 4,
220 .inactivity_time = 3,
221 .free_fall_threshold = 8,
222 .free_fall_time = 0x20,
223 .data_rate = 8,
224 .data_range = ADXL_FULL_RES,
226 .ev_type = EV_ABS,
227 .ev_code_x = ABS_X, /* EV_REL */
228 .ev_code_y = ABS_Y, /* EV_REL */
229 .ev_code_z = ABS_Z, /* EV_REL */
231 .ev_code_tap = {BTN_TOUCH, BTN_TOUCH, BTN_TOUCH}, /* EV_KEY {x,y,z} */
232 .power_mode = ADXL_AUTO_SLEEP | ADXL_LINK,
233 .fifo_mode = FIFO_STREAM,
234 .watermark = 0,
237 static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
239 short buf[3];
241 ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
243 mutex_lock(&ac->mutex);
244 ac->saved.x = (s16) le16_to_cpu(buf[0]);
245 axis->x = ac->saved.x;
247 ac->saved.y = (s16) le16_to_cpu(buf[1]);
248 axis->y = ac->saved.y;
250 ac->saved.z = (s16) le16_to_cpu(buf[2]);
251 axis->z = ac->saved.z;
252 mutex_unlock(&ac->mutex);
255 static void adxl34x_service_ev_fifo(struct adxl34x *ac)
257 struct adxl34x_platform_data *pdata = &ac->pdata;
258 struct axis_triple axis;
260 adxl34x_get_triple(ac, &axis);
262 input_event(ac->input, pdata->ev_type, pdata->ev_code_x,
263 axis.x - ac->swcal.x);
264 input_event(ac->input, pdata->ev_type, pdata->ev_code_y,
265 axis.y - ac->swcal.y);
266 input_event(ac->input, pdata->ev_type, pdata->ev_code_z,
267 axis.z - ac->swcal.z);
270 static void adxl34x_report_key_single(struct input_dev *input, int key)
272 input_report_key(input, key, true);
273 input_sync(input);
274 input_report_key(input, key, false);
277 static void adxl34x_send_key_events(struct adxl34x *ac,
278 struct adxl34x_platform_data *pdata, int status, int press)
280 int i;
282 for (i = ADXL_X_AXIS; i <= ADXL_Z_AXIS; i++) {
283 if (status & (1 << (ADXL_Z_AXIS - i)))
284 input_report_key(ac->input,
285 pdata->ev_code_tap[i], press);
289 static void adxl34x_do_tap(struct adxl34x *ac,
290 struct adxl34x_platform_data *pdata, int status)
292 adxl34x_send_key_events(ac, pdata, status, true);
293 input_sync(ac->input);
294 adxl34x_send_key_events(ac, pdata, status, false);
297 static irqreturn_t adxl34x_irq(int irq, void *handle)
299 struct adxl34x *ac = handle;
300 struct adxl34x_platform_data *pdata = &ac->pdata;
301 int int_stat, tap_stat, samples, orient, orient_code;
304 * ACT_TAP_STATUS should be read before clearing the interrupt
305 * Avoid reading ACT_TAP_STATUS in case TAP detection is disabled
308 if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
309 tap_stat = AC_READ(ac, ACT_TAP_STATUS);
310 else
311 tap_stat = 0;
313 int_stat = AC_READ(ac, INT_SOURCE);
315 if (int_stat & FREE_FALL)
316 adxl34x_report_key_single(ac->input, pdata->ev_code_ff);
318 if (int_stat & OVERRUN)
319 dev_dbg(ac->dev, "OVERRUN\n");
321 if (int_stat & (SINGLE_TAP | DOUBLE_TAP)) {
322 adxl34x_do_tap(ac, pdata, tap_stat);
324 if (int_stat & DOUBLE_TAP)
325 adxl34x_do_tap(ac, pdata, tap_stat);
328 if (pdata->ev_code_act_inactivity) {
329 if (int_stat & ACTIVITY)
330 input_report_key(ac->input,
331 pdata->ev_code_act_inactivity, 1);
332 if (int_stat & INACTIVITY)
333 input_report_key(ac->input,
334 pdata->ev_code_act_inactivity, 0);
338 * ORIENTATION SENSING ADXL346 only
340 if (pdata->orientation_enable) {
341 orient = AC_READ(ac, ORIENT);
342 if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_2D) &&
343 (orient & ADXL346_2D_VALID)) {
345 orient_code = ADXL346_2D_ORIENT(orient);
346 /* Report orientation only when it changes */
347 if (ac->orient2d_saved != orient_code) {
348 ac->orient2d_saved = orient_code;
349 adxl34x_report_key_single(ac->input,
350 pdata->ev_codes_orient_2d[orient_code]);
354 if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_3D) &&
355 (orient & ADXL346_3D_VALID)) {
357 orient_code = ADXL346_3D_ORIENT(orient) - 1;
358 /* Report orientation only when it changes */
359 if (ac->orient3d_saved != orient_code) {
360 ac->orient3d_saved = orient_code;
361 adxl34x_report_key_single(ac->input,
362 pdata->ev_codes_orient_3d[orient_code]);
367 if (int_stat & (DATA_READY | WATERMARK)) {
369 if (pdata->fifo_mode)
370 samples = ENTRIES(AC_READ(ac, FIFO_STATUS)) + 1;
371 else
372 samples = 1;
374 for (; samples > 0; samples--) {
375 adxl34x_service_ev_fifo(ac);
377 * To ensure that the FIFO has
378 * completely popped, there must be at least 5 us between
379 * the end of reading the data registers, signified by the
380 * transition to register 0x38 from 0x37 or the CS pin
381 * going high, and the start of new reads of the FIFO or
382 * reading the FIFO_STATUS register. For SPI operation at
383 * 1.5 MHz or lower, the register addressing portion of the
384 * transmission is sufficient delay to ensure the FIFO has
385 * completely popped. It is necessary for SPI operation
386 * greater than 1.5 MHz to de-assert the CS pin to ensure a
387 * total of 5 us, which is at most 3.4 us at 5 MHz
388 * operation.
390 if (ac->fifo_delay && (samples > 1))
391 udelay(3);
395 input_sync(ac->input);
397 return IRQ_HANDLED;
400 static void __adxl34x_disable(struct adxl34x *ac)
402 if (!ac->disabled && ac->opened) {
404 * A '0' places the ADXL34x into standby mode
405 * with minimum power consumption.
407 AC_WRITE(ac, POWER_CTL, 0);
409 ac->disabled = true;
413 static void __adxl34x_enable(struct adxl34x *ac)
415 if (ac->disabled && ac->opened) {
416 AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
417 ac->disabled = false;
421 void adxl34x_disable(struct adxl34x *ac)
423 mutex_lock(&ac->mutex);
424 __adxl34x_disable(ac);
425 mutex_unlock(&ac->mutex);
427 EXPORT_SYMBOL_GPL(adxl34x_disable);
429 void adxl34x_enable(struct adxl34x *ac)
431 mutex_lock(&ac->mutex);
432 __adxl34x_enable(ac);
433 mutex_unlock(&ac->mutex);
436 EXPORT_SYMBOL_GPL(adxl34x_enable);
438 static ssize_t adxl34x_disable_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct adxl34x *ac = dev_get_drvdata(dev);
443 return sprintf(buf, "%u\n", ac->disabled);
446 static ssize_t adxl34x_disable_store(struct device *dev,
447 struct device_attribute *attr,
448 const char *buf, size_t count)
450 struct adxl34x *ac = dev_get_drvdata(dev);
451 unsigned long val;
452 int error;
454 error = strict_strtoul(buf, 10, &val);
455 if (error)
456 return error;
458 if (val)
459 adxl34x_disable(ac);
460 else
461 adxl34x_enable(ac);
463 return count;
466 static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
468 static ssize_t adxl34x_calibrate_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct adxl34x *ac = dev_get_drvdata(dev);
472 ssize_t count;
474 mutex_lock(&ac->mutex);
475 count = sprintf(buf, "%d,%d,%d\n",
476 ac->hwcal.x * 4 + ac->swcal.x,
477 ac->hwcal.y * 4 + ac->swcal.y,
478 ac->hwcal.z * 4 + ac->swcal.z);
479 mutex_unlock(&ac->mutex);
481 return count;
484 static ssize_t adxl34x_calibrate_store(struct device *dev,
485 struct device_attribute *attr,
486 const char *buf, size_t count)
488 struct adxl34x *ac = dev_get_drvdata(dev);
491 * Hardware offset calibration has a resolution of 15.6 mg/LSB.
492 * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
495 mutex_lock(&ac->mutex);
496 ac->hwcal.x -= (ac->saved.x / 4);
497 ac->swcal.x = ac->saved.x % 4;
499 ac->hwcal.y -= (ac->saved.y / 4);
500 ac->swcal.y = ac->saved.y % 4;
502 ac->hwcal.z -= (ac->saved.z / 4);
503 ac->swcal.z = ac->saved.z % 4;
505 AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
506 AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
507 AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
508 mutex_unlock(&ac->mutex);
510 return count;
513 static DEVICE_ATTR(calibrate, 0664,
514 adxl34x_calibrate_show, adxl34x_calibrate_store);
516 static ssize_t adxl34x_rate_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
519 struct adxl34x *ac = dev_get_drvdata(dev);
521 return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
524 static ssize_t adxl34x_rate_store(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
528 struct adxl34x *ac = dev_get_drvdata(dev);
529 unsigned long val;
530 int error;
532 error = strict_strtoul(buf, 10, &val);
533 if (error)
534 return error;
536 mutex_lock(&ac->mutex);
538 ac->pdata.data_rate = RATE(val);
539 AC_WRITE(ac, BW_RATE,
540 ac->pdata.data_rate |
541 (ac->pdata.low_power_mode ? LOW_POWER : 0));
543 mutex_unlock(&ac->mutex);
545 return count;
548 static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
550 static ssize_t adxl34x_autosleep_show(struct device *dev,
551 struct device_attribute *attr, char *buf)
553 struct adxl34x *ac = dev_get_drvdata(dev);
555 return sprintf(buf, "%u\n",
556 ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
559 static ssize_t adxl34x_autosleep_store(struct device *dev,
560 struct device_attribute *attr,
561 const char *buf, size_t count)
563 struct adxl34x *ac = dev_get_drvdata(dev);
564 unsigned long val;
565 int error;
567 error = strict_strtoul(buf, 10, &val);
568 if (error)
569 return error;
571 mutex_lock(&ac->mutex);
573 if (val)
574 ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
575 else
576 ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
578 if (!ac->disabled && ac->opened)
579 AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
581 mutex_unlock(&ac->mutex);
583 return count;
586 static DEVICE_ATTR(autosleep, 0664,
587 adxl34x_autosleep_show, adxl34x_autosleep_store);
589 static ssize_t adxl34x_position_show(struct device *dev,
590 struct device_attribute *attr, char *buf)
592 struct adxl34x *ac = dev_get_drvdata(dev);
593 ssize_t count;
595 mutex_lock(&ac->mutex);
596 count = sprintf(buf, "(%d, %d, %d)\n",
597 ac->saved.x, ac->saved.y, ac->saved.z);
598 mutex_unlock(&ac->mutex);
600 return count;
603 static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
605 #ifdef ADXL_DEBUG
606 static ssize_t adxl34x_write_store(struct device *dev,
607 struct device_attribute *attr,
608 const char *buf, size_t count)
610 struct adxl34x *ac = dev_get_drvdata(dev);
611 unsigned long val;
612 int error;
615 * This allows basic ADXL register write access for debug purposes.
617 error = strict_strtoul(buf, 16, &val);
618 if (error)
619 return error;
621 mutex_lock(&ac->mutex);
622 AC_WRITE(ac, val >> 8, val & 0xFF);
623 mutex_unlock(&ac->mutex);
625 return count;
628 static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
629 #endif
631 static struct attribute *adxl34x_attributes[] = {
632 &dev_attr_disable.attr,
633 &dev_attr_calibrate.attr,
634 &dev_attr_rate.attr,
635 &dev_attr_autosleep.attr,
636 &dev_attr_position.attr,
637 #ifdef ADXL_DEBUG
638 &dev_attr_write.attr,
639 #endif
640 NULL
643 static const struct attribute_group adxl34x_attr_group = {
644 .attrs = adxl34x_attributes,
647 static int adxl34x_input_open(struct input_dev *input)
649 struct adxl34x *ac = input_get_drvdata(input);
651 mutex_lock(&ac->mutex);
652 ac->opened = true;
653 __adxl34x_enable(ac);
654 mutex_unlock(&ac->mutex);
656 return 0;
659 static void adxl34x_input_close(struct input_dev *input)
661 struct adxl34x *ac = input_get_drvdata(input);
663 mutex_lock(&ac->mutex);
664 __adxl34x_disable(ac);
665 ac->opened = false;
666 mutex_unlock(&ac->mutex);
669 struct adxl34x *adxl34x_probe(struct device *dev, int irq,
670 bool fifo_delay_default,
671 const struct adxl34x_bus_ops *bops)
673 struct adxl34x *ac;
674 struct input_dev *input_dev;
675 const struct adxl34x_platform_data *pdata;
676 int err, range, i;
677 unsigned char revid;
679 if (!irq) {
680 dev_err(dev, "no IRQ?\n");
681 err = -ENODEV;
682 goto err_out;
685 ac = kzalloc(sizeof(*ac), GFP_KERNEL);
686 input_dev = input_allocate_device();
687 if (!ac || !input_dev) {
688 err = -ENOMEM;
689 goto err_out;
692 ac->fifo_delay = fifo_delay_default;
694 pdata = dev->platform_data;
695 if (!pdata) {
696 dev_dbg(dev,
697 "No platfrom data: Using default initialization\n");
698 pdata = &adxl34x_default_init;
701 ac->pdata = *pdata;
702 pdata = &ac->pdata;
704 ac->input = input_dev;
705 ac->disabled = true;
706 ac->dev = dev;
707 ac->irq = irq;
708 ac->bops = bops;
710 mutex_init(&ac->mutex);
712 input_dev->name = "ADXL34x accelerometer";
713 revid = ac->bops->read(dev, DEVID);
715 switch (revid) {
716 case ID_ADXL345:
717 ac->model = 345;
718 break;
719 case ID_ADXL346:
720 ac->model = 346;
721 break;
722 default:
723 dev_err(dev, "Failed to probe %s\n", input_dev->name);
724 err = -ENODEV;
725 goto err_free_mem;
728 snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
730 input_dev->phys = ac->phys;
731 input_dev->dev.parent = dev;
732 input_dev->id.product = ac->model;
733 input_dev->id.bustype = bops->bustype;
734 input_dev->open = adxl34x_input_open;
735 input_dev->close = adxl34x_input_close;
737 input_set_drvdata(input_dev, ac);
739 __set_bit(ac->pdata.ev_type, input_dev->evbit);
741 if (ac->pdata.ev_type == EV_REL) {
742 __set_bit(REL_X, input_dev->relbit);
743 __set_bit(REL_Y, input_dev->relbit);
744 __set_bit(REL_Z, input_dev->relbit);
745 } else {
746 /* EV_ABS */
747 __set_bit(ABS_X, input_dev->absbit);
748 __set_bit(ABS_Y, input_dev->absbit);
749 __set_bit(ABS_Z, input_dev->absbit);
751 if (pdata->data_range & FULL_RES)
752 range = ADXL_FULLRES_MAX_VAL; /* Signed 13-bit */
753 else
754 range = ADXL_FIXEDRES_MAX_VAL; /* Signed 10-bit */
756 input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
757 input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
758 input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
761 __set_bit(EV_KEY, input_dev->evbit);
762 __set_bit(pdata->ev_code_tap[ADXL_X_AXIS], input_dev->keybit);
763 __set_bit(pdata->ev_code_tap[ADXL_Y_AXIS], input_dev->keybit);
764 __set_bit(pdata->ev_code_tap[ADXL_Z_AXIS], input_dev->keybit);
766 if (pdata->ev_code_ff) {
767 ac->int_mask = FREE_FALL;
768 __set_bit(pdata->ev_code_ff, input_dev->keybit);
771 if (pdata->ev_code_act_inactivity)
772 __set_bit(pdata->ev_code_act_inactivity, input_dev->keybit);
774 ac->int_mask |= ACTIVITY | INACTIVITY;
776 if (pdata->watermark) {
777 ac->int_mask |= WATERMARK;
778 if (!FIFO_MODE(pdata->fifo_mode))
779 ac->pdata.fifo_mode |= FIFO_STREAM;
780 } else {
781 ac->int_mask |= DATA_READY;
784 if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
785 ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
787 if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
788 ac->fifo_delay = false;
790 ac->bops->write(dev, POWER_CTL, 0);
792 err = request_threaded_irq(ac->irq, NULL, adxl34x_irq,
793 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
794 dev_name(dev), ac);
795 if (err) {
796 dev_err(dev, "irq %d busy?\n", ac->irq);
797 goto err_free_mem;
800 err = sysfs_create_group(&dev->kobj, &adxl34x_attr_group);
801 if (err)
802 goto err_free_irq;
804 err = input_register_device(input_dev);
805 if (err)
806 goto err_remove_attr;
808 AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
809 AC_WRITE(ac, OFSX, pdata->x_axis_offset);
810 ac->hwcal.x = pdata->x_axis_offset;
811 AC_WRITE(ac, OFSY, pdata->y_axis_offset);
812 ac->hwcal.y = pdata->y_axis_offset;
813 AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
814 ac->hwcal.z = pdata->z_axis_offset;
815 AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
816 AC_WRITE(ac, DUR, pdata->tap_duration);
817 AC_WRITE(ac, LATENT, pdata->tap_latency);
818 AC_WRITE(ac, WINDOW, pdata->tap_window);
819 AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
820 AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
821 AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
822 AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
823 AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
824 AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
825 AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
826 AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
827 (pdata->low_power_mode ? LOW_POWER : 0));
828 AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
829 AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
830 SAMPLES(pdata->watermark));
832 if (pdata->use_int2) {
833 /* Map all INTs to INT2 */
834 AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
835 } else {
836 /* Map all INTs to INT1 */
837 AC_WRITE(ac, INT_MAP, 0);
840 if (ac->model == 346 && ac->pdata.orientation_enable) {
841 AC_WRITE(ac, ORIENT_CONF,
842 ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
843 ORIENT_DIVISOR(ac->pdata.divisor_length));
845 ac->orient2d_saved = 1234;
846 ac->orient3d_saved = 1234;
848 if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
849 for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
850 __set_bit(pdata->ev_codes_orient_3d[i],
851 input_dev->keybit);
853 if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
854 for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
855 __set_bit(pdata->ev_codes_orient_2d[i],
856 input_dev->keybit);
857 } else {
858 ac->pdata.orientation_enable = 0;
861 AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
863 ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
865 return ac;
867 err_remove_attr:
868 sysfs_remove_group(&dev->kobj, &adxl34x_attr_group);
869 err_free_irq:
870 free_irq(ac->irq, ac);
871 err_free_mem:
872 input_free_device(input_dev);
873 kfree(ac);
874 err_out:
875 return ERR_PTR(err);
877 EXPORT_SYMBOL_GPL(adxl34x_probe);
879 int adxl34x_remove(struct adxl34x *ac)
881 adxl34x_disable(ac);
882 sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
883 free_irq(ac->irq, ac);
884 input_unregister_device(ac->input);
885 kfree(ac);
887 dev_dbg(ac->dev, "unregistered accelerometer\n");
888 return 0;
890 EXPORT_SYMBOL_GPL(adxl34x_remove);
892 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
893 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
894 MODULE_LICENSE("GPL");