xref: /linux/drivers/input/misc/adxl34x.c (revision e3966940559d52aa1800a008dcfeec218dd31f88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ADXL345/346 Three-Axis Digital Accelerometers
4  *
5  * Enter bugs at http://blackfin.uclinux.org/
6  *
7  * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
8  */
9 
10 #include <linux/device.h>
11 #include <linux/delay.h>
12 #include <linux/export.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>
19 #include <linux/module.h>
20 
21 #include "adxl34x.h"
22 
23 /* ADXL345/6 Register Map */
24 #define DEVID		0x00	/* R   Device ID */
25 #define THRESH_TAP	0x1D	/* R/W Tap threshold */
26 #define OFSX		0x1E	/* R/W X-axis offset */
27 #define OFSY		0x1F	/* R/W Y-axis offset */
28 #define OFSZ		0x20	/* R/W Z-axis offset */
29 #define DUR		0x21	/* R/W Tap duration */
30 #define LATENT		0x22	/* R/W Tap latency */
31 #define WINDOW		0x23	/* R/W Tap window */
32 #define THRESH_ACT	0x24	/* R/W Activity threshold */
33 #define THRESH_INACT	0x25	/* R/W Inactivity threshold */
34 #define TIME_INACT	0x26	/* R/W Inactivity time */
35 #define ACT_INACT_CTL	0x27	/* R/W Axis enable control for activity and */
36 				/* inactivity detection */
37 #define THRESH_FF	0x28	/* R/W Free-fall threshold */
38 #define TIME_FF		0x29	/* R/W Free-fall time */
39 #define TAP_AXES	0x2A	/* R/W Axis control for tap/double tap */
40 #define ACT_TAP_STATUS	0x2B	/* R   Source of tap/double tap */
41 #define BW_RATE		0x2C	/* R/W Data rate and power mode control */
42 #define POWER_CTL	0x2D	/* R/W Power saving features control */
43 #define INT_ENABLE	0x2E	/* R/W Interrupt enable control */
44 #define INT_MAP		0x2F	/* R/W Interrupt mapping control */
45 #define INT_SOURCE	0x30	/* R   Source of interrupts */
46 #define DATA_FORMAT	0x31	/* R/W Data format control */
47 #define DATAX0		0x32	/* R   X-Axis Data 0 */
48 #define DATAX1		0x33	/* R   X-Axis Data 1 */
49 #define DATAY0		0x34	/* R   Y-Axis Data 0 */
50 #define DATAY1		0x35	/* R   Y-Axis Data 1 */
51 #define DATAZ0		0x36	/* R   Z-Axis Data 0 */
52 #define DATAZ1		0x37	/* R   Z-Axis Data 1 */
53 #define FIFO_CTL	0x38	/* R/W FIFO control */
54 #define FIFO_STATUS	0x39	/* R   FIFO status */
55 #define TAP_SIGN	0x3A	/* R   Sign and source for tap/double tap */
56 /* Orientation ADXL346 only */
57 #define ORIENT_CONF	0x3B	/* R/W Orientation configuration */
58 #define ORIENT		0x3C	/* R   Orientation status */
59 
60 /* DEVIDs */
61 #define ID_ADXL345	0xE5
62 #define ID_ADXL346	0xE6
63 
64 /* INT_ENABLE/INT_MAP/INT_SOURCE Bits */
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)
73 
74 /* ACT_INACT_CONTROL Bits */
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)
83 
84 /* TAP_AXES Bits */
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)
89 
90 /* ACT_TAP_STATUS Bits */
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)
98 
99 /* BW_RATE Bits */
100 #define LOW_POWER	(1 << 4)
101 #define RATE(x)		((x) & 0xF)
102 
103 /* POWER_CTL Bits */
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)
109 
110 /* DATA_FORMAT Bits */
111 #define SELF_TEST	(1 << 7)
112 #define SPI		(1 << 6)
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
121 
122 /*
123  * Maximum value our axis may get in full res mode for the input device
124  * (signed 13 bits)
125  */
126 #define ADXL_FULLRES_MAX_VAL 4096
127 
128 /*
129  * Maximum value our axis may get in fixed res mode for the input device
130  * (signed 10 bits)
131  */
132 #define ADXL_FIXEDRES_MAX_VAL 512
133 
134 /* FIFO_CTL Bits */
135 #define FIFO_MODE(x)	(((x) & 0x3) << 6)
136 #define FIFO_BYPASS	0
137 #define FIFO_FIFO	1
138 #define FIFO_STREAM	2
139 #define FIFO_TRIGGER	3
140 #define TRIGGER		(1 << 5)
141 #define SAMPLES(x)	((x) & 0x1F)
142 
143 /* FIFO_STATUS Bits */
144 #define FIFO_TRIG	(1 << 7)
145 #define ENTRIES(x)	((x) & 0x3F)
146 
147 /* TAP_SIGN Bits ADXL346 only */
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)
154 
155 /* ORIENT_CONF ADXL346 only */
156 #define ORIENT_DEADZONE(x)	(((x) & 0x7) << 4)
157 #define ORIENT_DIVISOR(x)	((x) & 0x7)
158 
159 /* ORIENT ADXL346 only */
160 #define ADXL346_2D_VALID		(1 << 6)
161 #define ADXL346_2D_ORIENT(x)		(((x) & 0x30) >> 4)
162 #define ADXL346_3D_VALID		(1 << 3)
163 #define ADXL346_3D_ORIENT(x)		((x) & 0x7)
164 #define ADXL346_2D_PORTRAIT_POS		0	/* +X */
165 #define ADXL346_2D_PORTRAIT_NEG		1	/* -X */
166 #define ADXL346_2D_LANDSCAPE_POS	2	/* +Y */
167 #define ADXL346_2D_LANDSCAPE_NEG	3	/* -Y */
168 
169 #define ADXL346_3D_FRONT		3	/* +X */
170 #define ADXL346_3D_BACK			4	/* -X */
171 #define ADXL346_3D_RIGHT		2	/* +Y */
172 #define ADXL346_3D_LEFT			5	/* -Y */
173 #define ADXL346_3D_TOP			1	/* +Z */
174 #define ADXL346_3D_BOTTOM		6	/* -Z */
175 
176 #undef ADXL_DEBUG
177 
178 #define ADXL_X_AXIS			0
179 #define ADXL_Y_AXIS			1
180 #define ADXL_Z_AXIS			2
181 
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))
184 
185 struct axis_triple {
186 	int x;
187 	int y;
188 	int z;
189 };
190 
191 struct adxl34x {
192 	struct device *dev;
193 	struct input_dev *input;
194 	struct mutex mutex;	/* reentrant protection for struct */
195 	struct adxl34x_platform_data pdata;
196 	struct axis_triple swcal;
197 	struct axis_triple hwcal;
198 	struct axis_triple saved;
199 	char phys[32];
200 	unsigned orient2d_saved;
201 	unsigned orient3d_saved;
202 	bool disabled;	/* P: mutex */
203 	bool opened;	/* P: mutex */
204 	bool suspended;	/* P: mutex */
205 	bool fifo_delay;
206 	int irq;
207 	unsigned model;
208 	unsigned int_mask;
209 
210 	const struct adxl34x_bus_ops *bops;
211 };
212 
213 static const struct adxl34x_platform_data adxl34x_default_init = {
214 	.tap_threshold = 35,
215 	.tap_duration = 3,
216 	.tap_latency = 20,
217 	.tap_window = 20,
218 	.tap_axis_control = ADXL_TAP_X_EN | ADXL_TAP_Y_EN | ADXL_TAP_Z_EN,
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,
225 	.data_rate = 8,
226 	.data_range = ADXL_FULL_RES,
227 
228 	.ev_type = EV_ABS,
229 	.ev_code_x = ABS_X,	/* EV_REL */
230 	.ev_code_y = ABS_Y,	/* EV_REL */
231 	.ev_code_z = ABS_Z,	/* EV_REL */
232 
233 	.ev_code_tap = {BTN_TOUCH, BTN_TOUCH, BTN_TOUCH}, /* EV_KEY {x,y,z} */
234 	.power_mode = ADXL_AUTO_SLEEP | ADXL_LINK,
235 	.fifo_mode = ADXL_FIFO_STREAM,
236 	.watermark = 0,
237 };
238 
239 static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
240 {
241 	__le16 buf[3];
242 
243 	ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
244 
245 	guard(mutex)(&ac->mutex);
246 
247 	ac->saved.x = (s16) le16_to_cpu(buf[0]);
248 	axis->x = ac->saved.x;
249 
250 	ac->saved.y = (s16) le16_to_cpu(buf[1]);
251 	axis->y = ac->saved.y;
252 
253 	ac->saved.z = (s16) le16_to_cpu(buf[2]);
254 	axis->z = ac->saved.z;
255 }
256 
257 static void adxl34x_service_ev_fifo(struct adxl34x *ac)
258 {
259 	struct adxl34x_platform_data *pdata = &ac->pdata;
260 	struct axis_triple axis;
261 
262 	adxl34x_get_triple(ac, &axis);
263 
264 	input_event(ac->input, pdata->ev_type, pdata->ev_code_x,
265 		    axis.x - ac->swcal.x);
266 	input_event(ac->input, pdata->ev_type, pdata->ev_code_y,
267 		    axis.y - ac->swcal.y);
268 	input_event(ac->input, pdata->ev_type, pdata->ev_code_z,
269 		    axis.z - ac->swcal.z);
270 }
271 
272 static void adxl34x_report_key_single(struct input_dev *input, int key)
273 {
274 	input_report_key(input, key, true);
275 	input_sync(input);
276 	input_report_key(input, key, false);
277 }
278 
279 static void adxl34x_send_key_events(struct adxl34x *ac,
280 		struct adxl34x_platform_data *pdata, int status, int press)
281 {
282 	int i;
283 
284 	for (i = ADXL_X_AXIS; i <= ADXL_Z_AXIS; i++) {
285 		if (status & (1 << (ADXL_Z_AXIS - i)))
286 			input_report_key(ac->input,
287 					 pdata->ev_code_tap[i], press);
288 	}
289 }
290 
291 static void adxl34x_do_tap(struct adxl34x *ac,
292 		struct adxl34x_platform_data *pdata, int status)
293 {
294 	adxl34x_send_key_events(ac, pdata, status, true);
295 	input_sync(ac->input);
296 	adxl34x_send_key_events(ac, pdata, status, false);
297 }
298 
299 static irqreturn_t adxl34x_irq(int irq, void *handle)
300 {
301 	struct adxl34x *ac = handle;
302 	struct adxl34x_platform_data *pdata = &ac->pdata;
303 	int int_stat, tap_stat, samples, orient, orient_code;
304 
305 	/*
306 	 * ACT_TAP_STATUS should be read before clearing the interrupt
307 	 * Avoid reading ACT_TAP_STATUS in case TAP detection is disabled
308 	 */
309 
310 	if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
311 		tap_stat = AC_READ(ac, ACT_TAP_STATUS);
312 	else
313 		tap_stat = 0;
314 
315 	int_stat = AC_READ(ac, INT_SOURCE);
316 
317 	if (int_stat & FREE_FALL)
318 		adxl34x_report_key_single(ac->input, pdata->ev_code_ff);
319 
320 	if (int_stat & OVERRUN)
321 		dev_dbg(ac->dev, "OVERRUN\n");
322 
323 	if (int_stat & (SINGLE_TAP | DOUBLE_TAP)) {
324 		adxl34x_do_tap(ac, pdata, tap_stat);
325 
326 		if (int_stat & DOUBLE_TAP)
327 			adxl34x_do_tap(ac, pdata, tap_stat);
328 	}
329 
330 	if (pdata->ev_code_act_inactivity) {
331 		if (int_stat & ACTIVITY)
332 			input_report_key(ac->input,
333 					 pdata->ev_code_act_inactivity, 1);
334 		if (int_stat & INACTIVITY)
335 			input_report_key(ac->input,
336 					 pdata->ev_code_act_inactivity, 0);
337 	}
338 
339 	/*
340 	 * ORIENTATION SENSING ADXL346 only
341 	 */
342 	if (pdata->orientation_enable) {
343 		orient = AC_READ(ac, ORIENT);
344 		if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_2D) &&
345 		    (orient & ADXL346_2D_VALID)) {
346 
347 			orient_code = ADXL346_2D_ORIENT(orient);
348 			/* Report orientation only when it changes */
349 			if (ac->orient2d_saved != orient_code) {
350 				ac->orient2d_saved = orient_code;
351 				adxl34x_report_key_single(ac->input,
352 					pdata->ev_codes_orient_2d[orient_code]);
353 			}
354 		}
355 
356 		if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_3D) &&
357 		    (orient & ADXL346_3D_VALID)) {
358 
359 			orient_code = ADXL346_3D_ORIENT(orient) - 1;
360 			/* Report orientation only when it changes */
361 			if (ac->orient3d_saved != orient_code) {
362 				ac->orient3d_saved = orient_code;
363 				adxl34x_report_key_single(ac->input,
364 					pdata->ev_codes_orient_3d[orient_code]);
365 			}
366 		}
367 	}
368 
369 	if (int_stat & (DATA_READY | WATERMARK)) {
370 
371 		if (pdata->fifo_mode)
372 			samples = ENTRIES(AC_READ(ac, FIFO_STATUS)) + 1;
373 		else
374 			samples = 1;
375 
376 		for (; samples > 0; samples--) {
377 			adxl34x_service_ev_fifo(ac);
378 			/*
379 			 * To ensure that the FIFO has
380 			 * completely popped, there must be at least 5 us between
381 			 * the end of reading the data registers, signified by the
382 			 * transition to register 0x38 from 0x37 or the CS pin
383 			 * going high, and the start of new reads of the FIFO or
384 			 * reading the FIFO_STATUS register. For SPI operation at
385 			 * 1.5 MHz or lower, the register addressing portion of the
386 			 * transmission is sufficient delay to ensure the FIFO has
387 			 * completely popped. It is necessary for SPI operation
388 			 * greater than 1.5 MHz to de-assert the CS pin to ensure a
389 			 * total of 5 us, which is at most 3.4 us at 5 MHz
390 			 * operation.
391 			 */
392 			if (ac->fifo_delay && (samples > 1))
393 				udelay(3);
394 		}
395 	}
396 
397 	input_sync(ac->input);
398 
399 	return IRQ_HANDLED;
400 }
401 
402 static void __adxl34x_disable(struct adxl34x *ac)
403 {
404 	/*
405 	 * A '0' places the ADXL34x into standby mode
406 	 * with minimum power consumption.
407 	 */
408 	AC_WRITE(ac, POWER_CTL, 0);
409 }
410 
411 static void __adxl34x_enable(struct adxl34x *ac)
412 {
413 	AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
414 }
415 
416 static int adxl34x_suspend(struct device *dev)
417 {
418 	struct adxl34x *ac = dev_get_drvdata(dev);
419 
420 	guard(mutex)(&ac->mutex);
421 
422 	if (!ac->suspended && !ac->disabled && ac->opened)
423 		__adxl34x_disable(ac);
424 
425 	ac->suspended = true;
426 
427 	return 0;
428 }
429 
430 static int adxl34x_resume(struct device *dev)
431 {
432 	struct adxl34x *ac = dev_get_drvdata(dev);
433 
434 	guard(mutex)(&ac->mutex);
435 
436 	if (ac->suspended && !ac->disabled && ac->opened)
437 		__adxl34x_enable(ac);
438 
439 	ac->suspended = false;
440 
441 	return 0;
442 }
443 
444 static ssize_t adxl34x_disable_show(struct device *dev,
445 				    struct device_attribute *attr, char *buf)
446 {
447 	struct adxl34x *ac = dev_get_drvdata(dev);
448 
449 	return sprintf(buf, "%u\n", ac->disabled);
450 }
451 
452 static ssize_t adxl34x_disable_store(struct device *dev,
453 				     struct device_attribute *attr,
454 				     const char *buf, size_t count)
455 {
456 	struct adxl34x *ac = dev_get_drvdata(dev);
457 	unsigned int val;
458 	int error;
459 
460 	error = kstrtouint(buf, 10, &val);
461 	if (error)
462 		return error;
463 
464 	guard(mutex)(&ac->mutex);
465 
466 	if (!ac->suspended && ac->opened) {
467 		if (val) {
468 			if (!ac->disabled)
469 				__adxl34x_disable(ac);
470 		} else {
471 			if (ac->disabled)
472 				__adxl34x_enable(ac);
473 		}
474 	}
475 
476 	ac->disabled = !!val;
477 
478 	return count;
479 }
480 
481 static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
482 
483 static ssize_t adxl34x_calibrate_show(struct device *dev,
484 				      struct device_attribute *attr, char *buf)
485 {
486 	struct adxl34x *ac = dev_get_drvdata(dev);
487 
488 	guard(mutex)(&ac->mutex);
489 
490 	return sprintf(buf, "%d,%d,%d\n",
491 		       ac->hwcal.x * 4 + ac->swcal.x,
492 		       ac->hwcal.y * 4 + ac->swcal.y,
493 		       ac->hwcal.z * 4 + ac->swcal.z);
494 }
495 
496 static ssize_t adxl34x_calibrate_store(struct device *dev,
497 				       struct device_attribute *attr,
498 				       const char *buf, size_t count)
499 {
500 	struct adxl34x *ac = dev_get_drvdata(dev);
501 
502 	/*
503 	 * Hardware offset calibration has a resolution of 15.6 mg/LSB.
504 	 * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
505 	 */
506 
507 	guard(mutex)(&ac->mutex);
508 
509 	ac->hwcal.x -= (ac->saved.x / 4);
510 	ac->swcal.x = ac->saved.x % 4;
511 
512 	ac->hwcal.y -= (ac->saved.y / 4);
513 	ac->swcal.y = ac->saved.y % 4;
514 
515 	ac->hwcal.z -= (ac->saved.z / 4);
516 	ac->swcal.z = ac->saved.z % 4;
517 
518 	AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
519 	AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
520 	AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
521 
522 	return count;
523 }
524 
525 static DEVICE_ATTR(calibrate, 0664,
526 		   adxl34x_calibrate_show, adxl34x_calibrate_store);
527 
528 static ssize_t adxl34x_rate_show(struct device *dev,
529 				 struct device_attribute *attr, char *buf)
530 {
531 	struct adxl34x *ac = dev_get_drvdata(dev);
532 
533 	return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
534 }
535 
536 static ssize_t adxl34x_rate_store(struct device *dev,
537 				  struct device_attribute *attr,
538 				  const char *buf, size_t count)
539 {
540 	struct adxl34x *ac = dev_get_drvdata(dev);
541 	unsigned char val;
542 	int error;
543 
544 	error = kstrtou8(buf, 10, &val);
545 	if (error)
546 		return error;
547 
548 	guard(mutex)(&ac->mutex);
549 
550 	ac->pdata.data_rate = RATE(val);
551 	AC_WRITE(ac, BW_RATE,
552 		 ac->pdata.data_rate |
553 			(ac->pdata.low_power_mode ? LOW_POWER : 0));
554 
555 	return count;
556 }
557 
558 static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
559 
560 static ssize_t adxl34x_autosleep_show(struct device *dev,
561 				 struct device_attribute *attr, char *buf)
562 {
563 	struct adxl34x *ac = dev_get_drvdata(dev);
564 
565 	return sprintf(buf, "%u\n",
566 		ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
567 }
568 
569 static ssize_t adxl34x_autosleep_store(struct device *dev,
570 				  struct device_attribute *attr,
571 				  const char *buf, size_t count)
572 {
573 	struct adxl34x *ac = dev_get_drvdata(dev);
574 	unsigned int val;
575 	int error;
576 
577 	error = kstrtouint(buf, 10, &val);
578 	if (error)
579 		return error;
580 
581 	guard(mutex)(&ac->mutex);
582 
583 	if (val)
584 		ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
585 	else
586 		ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
587 
588 	if (!ac->disabled && !ac->suspended && ac->opened)
589 		AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
590 
591 	return count;
592 }
593 
594 static DEVICE_ATTR(autosleep, 0664,
595 		   adxl34x_autosleep_show, adxl34x_autosleep_store);
596 
597 static ssize_t adxl34x_position_show(struct device *dev,
598 				 struct device_attribute *attr, char *buf)
599 {
600 	struct adxl34x *ac = dev_get_drvdata(dev);
601 
602 	guard(mutex)(&ac->mutex);
603 
604 	return sprintf(buf, "(%d, %d, %d)\n",
605 		       ac->saved.x, ac->saved.y, ac->saved.z);
606 }
607 
608 static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
609 
610 #ifdef ADXL_DEBUG
611 static ssize_t adxl34x_write_store(struct device *dev,
612 				   struct device_attribute *attr,
613 				   const char *buf, size_t count)
614 {
615 	struct adxl34x *ac = dev_get_drvdata(dev);
616 	unsigned int val;
617 	int error;
618 
619 	/*
620 	 * This allows basic ADXL register write access for debug purposes.
621 	 */
622 	error = kstrtouint(buf, 16, &val);
623 	if (error)
624 		return error;
625 
626 	guard(mutex)(&ac->mutex);
627 	AC_WRITE(ac, val >> 8, val & 0xFF);
628 
629 	return count;
630 }
631 
632 static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
633 #endif
634 
635 static struct attribute *adxl34x_attributes[] = {
636 	&dev_attr_disable.attr,
637 	&dev_attr_calibrate.attr,
638 	&dev_attr_rate.attr,
639 	&dev_attr_autosleep.attr,
640 	&dev_attr_position.attr,
641 #ifdef ADXL_DEBUG
642 	&dev_attr_write.attr,
643 #endif
644 	NULL
645 };
646 
647 static const struct attribute_group adxl34x_attr_group = {
648 	.attrs = adxl34x_attributes,
649 };
650 
651 const struct attribute_group *adxl34x_groups[] = {
652 	&adxl34x_attr_group,
653 	NULL
654 };
655 EXPORT_SYMBOL_GPL(adxl34x_groups);
656 
657 static int adxl34x_input_open(struct input_dev *input)
658 {
659 	struct adxl34x *ac = input_get_drvdata(input);
660 
661 	guard(mutex)(&ac->mutex);
662 
663 	if (!ac->suspended && !ac->disabled)
664 		__adxl34x_enable(ac);
665 
666 	ac->opened = true;
667 
668 	return 0;
669 }
670 
671 static void adxl34x_input_close(struct input_dev *input)
672 {
673 	struct adxl34x *ac = input_get_drvdata(input);
674 
675 	guard(mutex)(&ac->mutex);
676 
677 	if (!ac->suspended && !ac->disabled)
678 		__adxl34x_disable(ac);
679 
680 	ac->opened = false;
681 }
682 
683 struct adxl34x *adxl34x_probe(struct device *dev, int irq,
684 			      bool fifo_delay_default,
685 			      const struct adxl34x_bus_ops *bops)
686 {
687 	struct adxl34x *ac;
688 	struct input_dev *input_dev;
689 	const struct adxl34x_platform_data *pdata;
690 	int error, range, i;
691 	int revid;
692 
693 	if (!irq) {
694 		dev_err(dev, "no IRQ?\n");
695 		return ERR_PTR(-ENODEV);
696 	}
697 
698 	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
699 	if (!ac)
700 		return ERR_PTR(-ENOMEM);
701 
702 	input_dev = devm_input_allocate_device(dev);
703 	if (!input_dev)
704 		return ERR_PTR(-ENOMEM);
705 
706 	ac->fifo_delay = fifo_delay_default;
707 
708 	pdata = dev_get_platdata(dev);
709 	if (!pdata) {
710 		dev_dbg(dev,
711 			"No platform data: Using default initialization\n");
712 		pdata = &adxl34x_default_init;
713 	}
714 
715 	ac->pdata = *pdata;
716 	pdata = &ac->pdata;
717 
718 	ac->input = input_dev;
719 	ac->dev = dev;
720 	ac->irq = irq;
721 	ac->bops = bops;
722 
723 	mutex_init(&ac->mutex);
724 
725 	input_dev->name = "ADXL34x accelerometer";
726 	revid = AC_READ(ac, DEVID);
727 
728 	switch (revid) {
729 	case ID_ADXL345:
730 		ac->model = 345;
731 		break;
732 	case ID_ADXL346:
733 		ac->model = 346;
734 		break;
735 	default:
736 		dev_err(dev, "Failed to probe %s\n", input_dev->name);
737 		return ERR_PTR(-ENODEV);
738 	}
739 
740 	snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
741 
742 	input_dev->phys = ac->phys;
743 	input_dev->id.product = ac->model;
744 	input_dev->id.bustype = bops->bustype;
745 	input_dev->open = adxl34x_input_open;
746 	input_dev->close = adxl34x_input_close;
747 
748 	input_set_drvdata(input_dev, ac);
749 
750 	if (ac->pdata.ev_type == EV_REL) {
751 		input_set_capability(input_dev, EV_REL, REL_X);
752 		input_set_capability(input_dev, EV_REL, REL_Y);
753 		input_set_capability(input_dev, EV_REL, REL_Z);
754 	} else {
755 		/* EV_ABS */
756 		if (pdata->data_range & FULL_RES)
757 			range = ADXL_FULLRES_MAX_VAL;	/* Signed 13-bit */
758 		else
759 			range = ADXL_FIXEDRES_MAX_VAL;	/* Signed 10-bit */
760 
761 		input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
762 		input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
763 		input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
764 	}
765 
766 	input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_X_AXIS]);
767 	input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_Y_AXIS]);
768 	input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_Z_AXIS]);
769 
770 	if (pdata->ev_code_ff) {
771 		ac->int_mask = FREE_FALL;
772 		input_set_capability(input_dev, EV_KEY, pdata->ev_code_ff);
773 	}
774 
775 	if (pdata->ev_code_act_inactivity)
776 		input_set_capability(input_dev, EV_KEY,
777 				     pdata->ev_code_act_inactivity);
778 
779 	ac->int_mask |= ACTIVITY | INACTIVITY;
780 
781 	if (pdata->watermark) {
782 		ac->int_mask |= WATERMARK;
783 		if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
784 			ac->pdata.fifo_mode |= FIFO_STREAM;
785 	} else {
786 		ac->int_mask |= DATA_READY;
787 	}
788 
789 	if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
790 		ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
791 
792 	if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
793 		ac->fifo_delay = false;
794 
795 	AC_WRITE(ac, POWER_CTL, 0);
796 
797 	error = devm_request_threaded_irq(dev, ac->irq, NULL, adxl34x_irq,
798 					  IRQF_ONESHOT, dev_name(dev), ac);
799 	if (error) {
800 		dev_err(dev, "irq %d busy?\n", ac->irq);
801 		return ERR_PTR(error);
802 	}
803 
804 	error = input_register_device(input_dev);
805 	if (error)
806 		return ERR_PTR(error);
807 
808 	AC_WRITE(ac, OFSX, pdata->x_axis_offset);
809 	ac->hwcal.x = pdata->x_axis_offset;
810 	AC_WRITE(ac, OFSY, pdata->y_axis_offset);
811 	ac->hwcal.y = pdata->y_axis_offset;
812 	AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
813 	ac->hwcal.z = pdata->z_axis_offset;
814 	AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
815 	AC_WRITE(ac, DUR, pdata->tap_duration);
816 	AC_WRITE(ac, LATENT, pdata->tap_latency);
817 	AC_WRITE(ac, WINDOW, pdata->tap_window);
818 	AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
819 	AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
820 	AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
821 	AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
822 	AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
823 	AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
824 	AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
825 	AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
826 		 (pdata->low_power_mode ? LOW_POWER : 0));
827 	AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
828 	AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
829 			SAMPLES(pdata->watermark));
830 
831 	if (pdata->use_int2) {
832 		/* Map all INTs to INT2 */
833 		AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
834 	} else {
835 		/* Map all INTs to INT1 */
836 		AC_WRITE(ac, INT_MAP, 0);
837 	}
838 
839 	if (ac->model == 346 && ac->pdata.orientation_enable) {
840 		AC_WRITE(ac, ORIENT_CONF,
841 			ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
842 			ORIENT_DIVISOR(ac->pdata.divisor_length));
843 
844 		ac->orient2d_saved = 1234;
845 		ac->orient3d_saved = 1234;
846 
847 		if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
848 			for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
849 				input_set_capability(input_dev, EV_KEY,
850 						     pdata->ev_codes_orient_3d[i]);
851 
852 		if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
853 			for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
854 				input_set_capability(input_dev, EV_KEY,
855 						     pdata->ev_codes_orient_2d[i]);
856 	} else {
857 		ac->pdata.orientation_enable = 0;
858 	}
859 
860 	AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
861 
862 	ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
863 
864 	return ac;
865 }
866 EXPORT_SYMBOL_GPL(adxl34x_probe);
867 
868 EXPORT_GPL_SIMPLE_DEV_PM_OPS(adxl34x_pm, adxl34x_suspend, adxl34x_resume);
869 
870 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
871 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
872 MODULE_LICENSE("GPL");
873