Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * ADXL345/346 Three-Axis Digital Accelerometers
  3 *
  4 * Enter bugs at http://blackfin.uclinux.org/
  5 *
  6 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
  7 * Licensed under the GPL-2 or later.
  8 */
  9
 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>
 
 19
 20#include "adxl34x.h"
 21
 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 */
 58
 59/* DEVIDs */
 60#define ID_ADXL345	0xE5
 61#define ID_ADXL346	0xE6
 62
 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)
 72
 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)
 82
 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)
 88
 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)
 97
 98/* BW_RATE Bits */
 99#define LOW_POWER	(1 << 4)
100#define RATE(x)		((x) & 0xF)
101
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)
108
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
120
121/*
122 * Maximum value our axis may get in full res mode for the input device
123 * (signed 13 bits)
124 */
125#define ADXL_FULLRES_MAX_VAL 4096
126
127/*
128 * Maximum value our axis may get in fixed res mode for the input device
129 * (signed 10 bits)
130 */
131#define ADXL_FIXEDRES_MAX_VAL 512
132
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)
141
142/* FIFO_STATUS Bits */
143#define FIFO_TRIG	(1 << 7)
144#define ENTRIES(x)	((x) & 0x3F)
145
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)
153
154/* ORIENT_CONF ADXL346 only */
155#define ORIENT_DEADZONE(x)	(((x) & 0x7) << 4)
156#define ORIENT_DIVISOR(x)	((x) & 0x7)
157
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 */
167
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 */
174
175#undef ADXL_DEBUG
176
177#define ADXL_X_AXIS			0
178#define ADXL_Y_AXIS			1
179#define ADXL_Z_AXIS			2
180
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))
183
184struct axis_triple {
185	int x;
186	int y;
187	int z;
188};
189
190struct 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 suspended;	/* P: mutex */
204	bool fifo_delay;
205	int irq;
206	unsigned model;
207	unsigned int_mask;
208
209	const struct adxl34x_bus_ops *bops;
210};
211
212static const struct adxl34x_platform_data adxl34x_default_init = {
213	.tap_threshold = 35,
214	.tap_duration = 3,
215	.tap_latency = 20,
216	.tap_window = 20,
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,
224	.data_rate = 8,
225	.data_range = ADXL_FULL_RES,
226
227	.ev_type = EV_ABS,
228	.ev_code_x = ABS_X,	/* EV_REL */
229	.ev_code_y = ABS_Y,	/* EV_REL */
230	.ev_code_z = ABS_Z,	/* EV_REL */
231
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 = FIFO_STREAM,
235	.watermark = 0,
236};
237
238static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
239{
240	short buf[3];
241
242	ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
243
244	mutex_lock(&ac->mutex);
245	ac->saved.x = (s16) le16_to_cpu(buf[0]);
246	axis->x = ac->saved.x;
247
248	ac->saved.y = (s16) le16_to_cpu(buf[1]);
249	axis->y = ac->saved.y;
250
251	ac->saved.z = (s16) le16_to_cpu(buf[2]);
252	axis->z = ac->saved.z;
253	mutex_unlock(&ac->mutex);
254}
255
256static void adxl34x_service_ev_fifo(struct adxl34x *ac)
257{
258	struct adxl34x_platform_data *pdata = &ac->pdata;
259	struct axis_triple axis;
260
261	adxl34x_get_triple(ac, &axis);
262
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);
269}
270
271static void adxl34x_report_key_single(struct input_dev *input, int key)
272{
273	input_report_key(input, key, true);
274	input_sync(input);
275	input_report_key(input, key, false);
276}
277
278static void adxl34x_send_key_events(struct adxl34x *ac,
279		struct adxl34x_platform_data *pdata, int status, int press)
280{
281	int i;
282
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);
287	}
288}
289
290static void adxl34x_do_tap(struct adxl34x *ac,
291		struct adxl34x_platform_data *pdata, int status)
292{
293	adxl34x_send_key_events(ac, pdata, status, true);
294	input_sync(ac->input);
295	adxl34x_send_key_events(ac, pdata, status, false);
296}
297
298static irqreturn_t adxl34x_irq(int irq, void *handle)
299{
300	struct adxl34x *ac = handle;
301	struct adxl34x_platform_data *pdata = &ac->pdata;
302	int int_stat, tap_stat, samples, orient, orient_code;
303
304	/*
305	 * ACT_TAP_STATUS should be read before clearing the interrupt
306	 * Avoid reading ACT_TAP_STATUS in case TAP detection is disabled
307	 */
308
309	if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
310		tap_stat = AC_READ(ac, ACT_TAP_STATUS);
311	else
312		tap_stat = 0;
313
314	int_stat = AC_READ(ac, INT_SOURCE);
315
316	if (int_stat & FREE_FALL)
317		adxl34x_report_key_single(ac->input, pdata->ev_code_ff);
318
319	if (int_stat & OVERRUN)
320		dev_dbg(ac->dev, "OVERRUN\n");
321
322	if (int_stat & (SINGLE_TAP | DOUBLE_TAP)) {
323		adxl34x_do_tap(ac, pdata, tap_stat);
324
325		if (int_stat & DOUBLE_TAP)
326			adxl34x_do_tap(ac, pdata, tap_stat);
327	}
328
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);
336	}
337
338	/*
339	 * ORIENTATION SENSING ADXL346 only
340	 */
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)) {
345
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]);
352			}
353		}
354
355		if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_3D) &&
356		    (orient & ADXL346_3D_VALID)) {
357
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]);
364			}
365		}
366	}
367
368	if (int_stat & (DATA_READY | WATERMARK)) {
369
370		if (pdata->fifo_mode)
371			samples = ENTRIES(AC_READ(ac, FIFO_STATUS)) + 1;
372		else
373			samples = 1;
374
375		for (; samples > 0; samples--) {
376			adxl34x_service_ev_fifo(ac);
377			/*
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
389			 * operation.
390			 */
391			if (ac->fifo_delay && (samples > 1))
392				udelay(3);
393		}
394	}
395
396	input_sync(ac->input);
397
398	return IRQ_HANDLED;
399}
400
401static void __adxl34x_disable(struct adxl34x *ac)
402{
403	/*
404	 * A '0' places the ADXL34x into standby mode
405	 * with minimum power consumption.
406	 */
407	AC_WRITE(ac, POWER_CTL, 0);
408}
409
410static void __adxl34x_enable(struct adxl34x *ac)
411{
412	AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
413}
414
415void adxl34x_suspend(struct adxl34x *ac)
416{
417	mutex_lock(&ac->mutex);
418
419	if (!ac->suspended && !ac->disabled && ac->opened)
420		__adxl34x_disable(ac);
421
422	ac->suspended = true;
423
424	mutex_unlock(&ac->mutex);
425}
426EXPORT_SYMBOL_GPL(adxl34x_suspend);
427
428void adxl34x_resume(struct adxl34x *ac)
429{
430	mutex_lock(&ac->mutex);
431
432	if (ac->suspended && !ac->disabled && ac->opened)
433		__adxl34x_enable(ac);
434
435	ac->suspended = false;
436
437	mutex_unlock(&ac->mutex);
438}
439EXPORT_SYMBOL_GPL(adxl34x_resume);
440
441static ssize_t adxl34x_disable_show(struct device *dev,
442				    struct device_attribute *attr, char *buf)
443{
444	struct adxl34x *ac = dev_get_drvdata(dev);
445
446	return sprintf(buf, "%u\n", ac->disabled);
447}
448
449static ssize_t adxl34x_disable_store(struct device *dev,
450				     struct device_attribute *attr,
451				     const char *buf, size_t count)
452{
453	struct adxl34x *ac = dev_get_drvdata(dev);
454	unsigned long val;
455	int error;
456
457	error = strict_strtoul(buf, 10, &val);
458	if (error)
459		return error;
460
461	mutex_lock(&ac->mutex);
462
463	if (!ac->suspended && ac->opened) {
464		if (val) {
465			if (!ac->disabled)
466				__adxl34x_disable(ac);
467		} else {
468			if (ac->disabled)
469				__adxl34x_enable(ac);
470		}
471	}
472
473	ac->disabled = !!val;
474
475	mutex_unlock(&ac->mutex);
476
477	return count;
478}
479
480static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
481
482static ssize_t adxl34x_calibrate_show(struct device *dev,
483				      struct device_attribute *attr, char *buf)
484{
485	struct adxl34x *ac = dev_get_drvdata(dev);
486	ssize_t count;
487
488	mutex_lock(&ac->mutex);
489	count = 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);
493	mutex_unlock(&ac->mutex);
494
495	return count;
496}
497
498static ssize_t adxl34x_calibrate_store(struct device *dev,
499				       struct device_attribute *attr,
500				       const char *buf, size_t count)
501{
502	struct adxl34x *ac = dev_get_drvdata(dev);
503
504	/*
505	 * Hardware offset calibration has a resolution of 15.6 mg/LSB.
506	 * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
507	 */
508
509	mutex_lock(&ac->mutex);
510	ac->hwcal.x -= (ac->saved.x / 4);
511	ac->swcal.x = ac->saved.x % 4;
512
513	ac->hwcal.y -= (ac->saved.y / 4);
514	ac->swcal.y = ac->saved.y % 4;
515
516	ac->hwcal.z -= (ac->saved.z / 4);
517	ac->swcal.z = ac->saved.z % 4;
518
519	AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
520	AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
521	AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
522	mutex_unlock(&ac->mutex);
523
524	return count;
525}
526
527static DEVICE_ATTR(calibrate, 0664,
528		   adxl34x_calibrate_show, adxl34x_calibrate_store);
529
530static ssize_t adxl34x_rate_show(struct device *dev,
531				 struct device_attribute *attr, char *buf)
532{
533	struct adxl34x *ac = dev_get_drvdata(dev);
534
535	return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
536}
537
538static ssize_t adxl34x_rate_store(struct device *dev,
539				  struct device_attribute *attr,
540				  const char *buf, size_t count)
541{
542	struct adxl34x *ac = dev_get_drvdata(dev);
543	unsigned long val;
544	int error;
545
546	error = strict_strtoul(buf, 10, &val);
547	if (error)
548		return error;
549
550	mutex_lock(&ac->mutex);
551
552	ac->pdata.data_rate = RATE(val);
553	AC_WRITE(ac, BW_RATE,
554		 ac->pdata.data_rate |
555			(ac->pdata.low_power_mode ? LOW_POWER : 0));
556
557	mutex_unlock(&ac->mutex);
558
559	return count;
560}
561
562static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
563
564static ssize_t adxl34x_autosleep_show(struct device *dev,
565				 struct device_attribute *attr, char *buf)
566{
567	struct adxl34x *ac = dev_get_drvdata(dev);
568
569	return sprintf(buf, "%u\n",
570		ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
571}
572
573static ssize_t adxl34x_autosleep_store(struct device *dev,
574				  struct device_attribute *attr,
575				  const char *buf, size_t count)
576{
577	struct adxl34x *ac = dev_get_drvdata(dev);
578	unsigned long val;
579	int error;
580
581	error = strict_strtoul(buf, 10, &val);
582	if (error)
583		return error;
584
585	mutex_lock(&ac->mutex);
586
587	if (val)
588		ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
589	else
590		ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
591
592	if (!ac->disabled && !ac->suspended && ac->opened)
593		AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
594
595	mutex_unlock(&ac->mutex);
596
597	return count;
598}
599
600static DEVICE_ATTR(autosleep, 0664,
601		   adxl34x_autosleep_show, adxl34x_autosleep_store);
602
603static ssize_t adxl34x_position_show(struct device *dev,
604				 struct device_attribute *attr, char *buf)
605{
606	struct adxl34x *ac = dev_get_drvdata(dev);
607	ssize_t count;
608
609	mutex_lock(&ac->mutex);
610	count = sprintf(buf, "(%d, %d, %d)\n",
611			ac->saved.x, ac->saved.y, ac->saved.z);
612	mutex_unlock(&ac->mutex);
613
614	return count;
615}
616
617static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
618
619#ifdef ADXL_DEBUG
620static ssize_t adxl34x_write_store(struct device *dev,
621				   struct device_attribute *attr,
622				   const char *buf, size_t count)
623{
624	struct adxl34x *ac = dev_get_drvdata(dev);
625	unsigned long val;
626	int error;
627
628	/*
629	 * This allows basic ADXL register write access for debug purposes.
630	 */
631	error = strict_strtoul(buf, 16, &val);
632	if (error)
633		return error;
634
635	mutex_lock(&ac->mutex);
636	AC_WRITE(ac, val >> 8, val & 0xFF);
637	mutex_unlock(&ac->mutex);
638
639	return count;
640}
641
642static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
643#endif
644
645static struct attribute *adxl34x_attributes[] = {
646	&dev_attr_disable.attr,
647	&dev_attr_calibrate.attr,
648	&dev_attr_rate.attr,
649	&dev_attr_autosleep.attr,
650	&dev_attr_position.attr,
651#ifdef ADXL_DEBUG
652	&dev_attr_write.attr,
653#endif
654	NULL
655};
656
657static const struct attribute_group adxl34x_attr_group = {
658	.attrs = adxl34x_attributes,
659};
660
661static int adxl34x_input_open(struct input_dev *input)
662{
663	struct adxl34x *ac = input_get_drvdata(input);
664
665	mutex_lock(&ac->mutex);
666
667	if (!ac->suspended && !ac->disabled)
668		__adxl34x_enable(ac);
669
670	ac->opened = true;
671
672	mutex_unlock(&ac->mutex);
673
674	return 0;
675}
676
677static void adxl34x_input_close(struct input_dev *input)
678{
679	struct adxl34x *ac = input_get_drvdata(input);
680
681	mutex_lock(&ac->mutex);
682
683	if (!ac->suspended && !ac->disabled)
684		__adxl34x_disable(ac);
685
686	ac->opened = false;
687
688	mutex_unlock(&ac->mutex);
689}
690
691struct adxl34x *adxl34x_probe(struct device *dev, int irq,
692			      bool fifo_delay_default,
693			      const struct adxl34x_bus_ops *bops)
694{
695	struct adxl34x *ac;
696	struct input_dev *input_dev;
697	const struct adxl34x_platform_data *pdata;
698	int err, range, i;
699	unsigned char revid;
700
701	if (!irq) {
702		dev_err(dev, "no IRQ?\n");
703		err = -ENODEV;
704		goto err_out;
705	}
706
707	ac = kzalloc(sizeof(*ac), GFP_KERNEL);
708	input_dev = input_allocate_device();
709	if (!ac || !input_dev) {
710		err = -ENOMEM;
711		goto err_free_mem;
712	}
713
714	ac->fifo_delay = fifo_delay_default;
715
716	pdata = dev->platform_data;
717	if (!pdata) {
718		dev_dbg(dev,
719			"No platform data: Using default initialization\n");
720		pdata = &adxl34x_default_init;
721	}
722
723	ac->pdata = *pdata;
724	pdata = &ac->pdata;
725
726	ac->input = input_dev;
727	ac->dev = dev;
728	ac->irq = irq;
729	ac->bops = bops;
730
731	mutex_init(&ac->mutex);
732
733	input_dev->name = "ADXL34x accelerometer";
734	revid = ac->bops->read(dev, DEVID);
735
736	switch (revid) {
737	case ID_ADXL345:
738		ac->model = 345;
739		break;
740	case ID_ADXL346:
741		ac->model = 346;
742		break;
743	default:
744		dev_err(dev, "Failed to probe %s\n", input_dev->name);
745		err = -ENODEV;
746		goto err_free_mem;
747	}
748
749	snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
750
751	input_dev->phys = ac->phys;
752	input_dev->dev.parent = dev;
753	input_dev->id.product = ac->model;
754	input_dev->id.bustype = bops->bustype;
755	input_dev->open = adxl34x_input_open;
756	input_dev->close = adxl34x_input_close;
757
758	input_set_drvdata(input_dev, ac);
759
760	__set_bit(ac->pdata.ev_type, input_dev->evbit);
761
762	if (ac->pdata.ev_type == EV_REL) {
763		__set_bit(REL_X, input_dev->relbit);
764		__set_bit(REL_Y, input_dev->relbit);
765		__set_bit(REL_Z, input_dev->relbit);
766	} else {
767		/* EV_ABS */
768		__set_bit(ABS_X, input_dev->absbit);
769		__set_bit(ABS_Y, input_dev->absbit);
770		__set_bit(ABS_Z, input_dev->absbit);
771
772		if (pdata->data_range & FULL_RES)
773			range = ADXL_FULLRES_MAX_VAL;	/* Signed 13-bit */
774		else
775			range = ADXL_FIXEDRES_MAX_VAL;	/* Signed 10-bit */
776
777		input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
778		input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
779		input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
780	}
781
782	__set_bit(EV_KEY, input_dev->evbit);
783	__set_bit(pdata->ev_code_tap[ADXL_X_AXIS], input_dev->keybit);
784	__set_bit(pdata->ev_code_tap[ADXL_Y_AXIS], input_dev->keybit);
785	__set_bit(pdata->ev_code_tap[ADXL_Z_AXIS], input_dev->keybit);
786
787	if (pdata->ev_code_ff) {
788		ac->int_mask = FREE_FALL;
789		__set_bit(pdata->ev_code_ff, input_dev->keybit);
790	}
791
792	if (pdata->ev_code_act_inactivity)
793		__set_bit(pdata->ev_code_act_inactivity, input_dev->keybit);
794
795	ac->int_mask |= ACTIVITY | INACTIVITY;
796
797	if (pdata->watermark) {
798		ac->int_mask |= WATERMARK;
799		if (!FIFO_MODE(pdata->fifo_mode))
800			ac->pdata.fifo_mode |= FIFO_STREAM;
801	} else {
802		ac->int_mask |= DATA_READY;
803	}
804
805	if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
806		ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
807
808	if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
809		ac->fifo_delay = false;
810
811	ac->bops->write(dev, POWER_CTL, 0);
812
813	err = request_threaded_irq(ac->irq, NULL, adxl34x_irq,
814				   IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
815				   dev_name(dev), ac);
816	if (err) {
817		dev_err(dev, "irq %d busy?\n", ac->irq);
818		goto err_free_mem;
819	}
820
821	err = sysfs_create_group(&dev->kobj, &adxl34x_attr_group);
822	if (err)
823		goto err_free_irq;
824
825	err = input_register_device(input_dev);
826	if (err)
827		goto err_remove_attr;
828
829	AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
830	AC_WRITE(ac, OFSX, pdata->x_axis_offset);
831	ac->hwcal.x = pdata->x_axis_offset;
832	AC_WRITE(ac, OFSY, pdata->y_axis_offset);
833	ac->hwcal.y = pdata->y_axis_offset;
834	AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
835	ac->hwcal.z = pdata->z_axis_offset;
836	AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
837	AC_WRITE(ac, DUR, pdata->tap_duration);
838	AC_WRITE(ac, LATENT, pdata->tap_latency);
839	AC_WRITE(ac, WINDOW, pdata->tap_window);
840	AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
841	AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
842	AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
843	AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
844	AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
845	AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
846	AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
847	AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
848		 (pdata->low_power_mode ? LOW_POWER : 0));
849	AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
850	AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
851			SAMPLES(pdata->watermark));
852
853	if (pdata->use_int2) {
854		/* Map all INTs to INT2 */
855		AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
856	} else {
857		/* Map all INTs to INT1 */
858		AC_WRITE(ac, INT_MAP, 0);
859	}
860
861	if (ac->model == 346 && ac->pdata.orientation_enable) {
862		AC_WRITE(ac, ORIENT_CONF,
863			ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
864			ORIENT_DIVISOR(ac->pdata.divisor_length));
865
866		ac->orient2d_saved = 1234;
867		ac->orient3d_saved = 1234;
868
869		if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
870			for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
871				__set_bit(pdata->ev_codes_orient_3d[i],
872					  input_dev->keybit);
873
874		if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
875			for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
876				__set_bit(pdata->ev_codes_orient_2d[i],
877					  input_dev->keybit);
878	} else {
879		ac->pdata.orientation_enable = 0;
880	}
881
882	AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
883
884	ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
885
886	return ac;
887
888 err_remove_attr:
889	sysfs_remove_group(&dev->kobj, &adxl34x_attr_group);
890 err_free_irq:
891	free_irq(ac->irq, ac);
892 err_free_mem:
893	input_free_device(input_dev);
894	kfree(ac);
895 err_out:
896	return ERR_PTR(err);
897}
898EXPORT_SYMBOL_GPL(adxl34x_probe);
899
900int adxl34x_remove(struct adxl34x *ac)
901{
902	sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
903	free_irq(ac->irq, ac);
904	input_unregister_device(ac->input);
905	dev_dbg(ac->dev, "unregistered accelerometer\n");
906	kfree(ac);
907
908	return 0;
909}
910EXPORT_SYMBOL_GPL(adxl34x_remove);
911
912MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
913MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
914MODULE_LICENSE("GPL");
v3.5.6
  1/*
  2 * ADXL345/346 Three-Axis Digital Accelerometers
  3 *
  4 * Enter bugs at http://blackfin.uclinux.org/
  5 *
  6 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
  7 * Licensed under the GPL-2 or later.
  8 */
  9
 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>
 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) & 0x3) >> 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
185struct axis_triple {
186	int x;
187	int y;
188	int z;
189};
190
191struct 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
213static 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 = FIFO_STREAM,
236	.watermark = 0,
237};
238
239static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
240{
241	short buf[3];
242
243	ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
244
245	mutex_lock(&ac->mutex);
246	ac->saved.x = (s16) le16_to_cpu(buf[0]);
247	axis->x = ac->saved.x;
248
249	ac->saved.y = (s16) le16_to_cpu(buf[1]);
250	axis->y = ac->saved.y;
251
252	ac->saved.z = (s16) le16_to_cpu(buf[2]);
253	axis->z = ac->saved.z;
254	mutex_unlock(&ac->mutex);
255}
256
257static 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
272static 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
279static 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
291static 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
299static 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
402static 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
411static void __adxl34x_enable(struct adxl34x *ac)
412{
413	AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
414}
415
416void adxl34x_suspend(struct adxl34x *ac)
417{
418	mutex_lock(&ac->mutex);
419
420	if (!ac->suspended && !ac->disabled && ac->opened)
421		__adxl34x_disable(ac);
422
423	ac->suspended = true;
424
425	mutex_unlock(&ac->mutex);
426}
427EXPORT_SYMBOL_GPL(adxl34x_suspend);
428
429void adxl34x_resume(struct adxl34x *ac)
430{
431	mutex_lock(&ac->mutex);
432
433	if (ac->suspended && !ac->disabled && ac->opened)
434		__adxl34x_enable(ac);
435
436	ac->suspended = false;
437
438	mutex_unlock(&ac->mutex);
439}
440EXPORT_SYMBOL_GPL(adxl34x_resume);
441
442static ssize_t adxl34x_disable_show(struct device *dev,
443				    struct device_attribute *attr, char *buf)
444{
445	struct adxl34x *ac = dev_get_drvdata(dev);
446
447	return sprintf(buf, "%u\n", ac->disabled);
448}
449
450static ssize_t adxl34x_disable_store(struct device *dev,
451				     struct device_attribute *attr,
452				     const char *buf, size_t count)
453{
454	struct adxl34x *ac = dev_get_drvdata(dev);
455	unsigned int val;
456	int error;
457
458	error = kstrtouint(buf, 10, &val);
459	if (error)
460		return error;
461
462	mutex_lock(&ac->mutex);
463
464	if (!ac->suspended && ac->opened) {
465		if (val) {
466			if (!ac->disabled)
467				__adxl34x_disable(ac);
468		} else {
469			if (ac->disabled)
470				__adxl34x_enable(ac);
471		}
472	}
473
474	ac->disabled = !!val;
475
476	mutex_unlock(&ac->mutex);
477
478	return count;
479}
480
481static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
482
483static 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	ssize_t count;
488
489	mutex_lock(&ac->mutex);
490	count = 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	mutex_unlock(&ac->mutex);
495
496	return count;
497}
498
499static ssize_t adxl34x_calibrate_store(struct device *dev,
500				       struct device_attribute *attr,
501				       const char *buf, size_t count)
502{
503	struct adxl34x *ac = dev_get_drvdata(dev);
504
505	/*
506	 * Hardware offset calibration has a resolution of 15.6 mg/LSB.
507	 * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
508	 */
509
510	mutex_lock(&ac->mutex);
511	ac->hwcal.x -= (ac->saved.x / 4);
512	ac->swcal.x = ac->saved.x % 4;
513
514	ac->hwcal.y -= (ac->saved.y / 4);
515	ac->swcal.y = ac->saved.y % 4;
516
517	ac->hwcal.z -= (ac->saved.z / 4);
518	ac->swcal.z = ac->saved.z % 4;
519
520	AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
521	AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
522	AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
523	mutex_unlock(&ac->mutex);
524
525	return count;
526}
527
528static DEVICE_ATTR(calibrate, 0664,
529		   adxl34x_calibrate_show, adxl34x_calibrate_store);
530
531static ssize_t adxl34x_rate_show(struct device *dev,
532				 struct device_attribute *attr, char *buf)
533{
534	struct adxl34x *ac = dev_get_drvdata(dev);
535
536	return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
537}
538
539static ssize_t adxl34x_rate_store(struct device *dev,
540				  struct device_attribute *attr,
541				  const char *buf, size_t count)
542{
543	struct adxl34x *ac = dev_get_drvdata(dev);
544	unsigned char val;
545	int error;
546
547	error = kstrtou8(buf, 10, &val);
548	if (error)
549		return error;
550
551	mutex_lock(&ac->mutex);
552
553	ac->pdata.data_rate = RATE(val);
554	AC_WRITE(ac, BW_RATE,
555		 ac->pdata.data_rate |
556			(ac->pdata.low_power_mode ? LOW_POWER : 0));
557
558	mutex_unlock(&ac->mutex);
559
560	return count;
561}
562
563static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
564
565static ssize_t adxl34x_autosleep_show(struct device *dev,
566				 struct device_attribute *attr, char *buf)
567{
568	struct adxl34x *ac = dev_get_drvdata(dev);
569
570	return sprintf(buf, "%u\n",
571		ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
572}
573
574static ssize_t adxl34x_autosleep_store(struct device *dev,
575				  struct device_attribute *attr,
576				  const char *buf, size_t count)
577{
578	struct adxl34x *ac = dev_get_drvdata(dev);
579	unsigned int val;
580	int error;
581
582	error = kstrtouint(buf, 10, &val);
583	if (error)
584		return error;
585
586	mutex_lock(&ac->mutex);
587
588	if (val)
589		ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
590	else
591		ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
592
593	if (!ac->disabled && !ac->suspended && ac->opened)
594		AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
595
596	mutex_unlock(&ac->mutex);
597
598	return count;
599}
600
601static DEVICE_ATTR(autosleep, 0664,
602		   adxl34x_autosleep_show, adxl34x_autosleep_store);
603
604static ssize_t adxl34x_position_show(struct device *dev,
605				 struct device_attribute *attr, char *buf)
606{
607	struct adxl34x *ac = dev_get_drvdata(dev);
608	ssize_t count;
609
610	mutex_lock(&ac->mutex);
611	count = sprintf(buf, "(%d, %d, %d)\n",
612			ac->saved.x, ac->saved.y, ac->saved.z);
613	mutex_unlock(&ac->mutex);
614
615	return count;
616}
617
618static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
619
620#ifdef ADXL_DEBUG
621static ssize_t adxl34x_write_store(struct device *dev,
622				   struct device_attribute *attr,
623				   const char *buf, size_t count)
624{
625	struct adxl34x *ac = dev_get_drvdata(dev);
626	unsigned int val;
627	int error;
628
629	/*
630	 * This allows basic ADXL register write access for debug purposes.
631	 */
632	error = kstrtouint(buf, 16, &val);
633	if (error)
634		return error;
635
636	mutex_lock(&ac->mutex);
637	AC_WRITE(ac, val >> 8, val & 0xFF);
638	mutex_unlock(&ac->mutex);
639
640	return count;
641}
642
643static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
644#endif
645
646static struct attribute *adxl34x_attributes[] = {
647	&dev_attr_disable.attr,
648	&dev_attr_calibrate.attr,
649	&dev_attr_rate.attr,
650	&dev_attr_autosleep.attr,
651	&dev_attr_position.attr,
652#ifdef ADXL_DEBUG
653	&dev_attr_write.attr,
654#endif
655	NULL
656};
657
658static const struct attribute_group adxl34x_attr_group = {
659	.attrs = adxl34x_attributes,
660};
661
662static int adxl34x_input_open(struct input_dev *input)
663{
664	struct adxl34x *ac = input_get_drvdata(input);
665
666	mutex_lock(&ac->mutex);
667
668	if (!ac->suspended && !ac->disabled)
669		__adxl34x_enable(ac);
670
671	ac->opened = true;
672
673	mutex_unlock(&ac->mutex);
674
675	return 0;
676}
677
678static void adxl34x_input_close(struct input_dev *input)
679{
680	struct adxl34x *ac = input_get_drvdata(input);
681
682	mutex_lock(&ac->mutex);
683
684	if (!ac->suspended && !ac->disabled)
685		__adxl34x_disable(ac);
686
687	ac->opened = false;
688
689	mutex_unlock(&ac->mutex);
690}
691
692struct adxl34x *adxl34x_probe(struct device *dev, int irq,
693			      bool fifo_delay_default,
694			      const struct adxl34x_bus_ops *bops)
695{
696	struct adxl34x *ac;
697	struct input_dev *input_dev;
698	const struct adxl34x_platform_data *pdata;
699	int err, range, i;
700	unsigned char revid;
701
702	if (!irq) {
703		dev_err(dev, "no IRQ?\n");
704		err = -ENODEV;
705		goto err_out;
706	}
707
708	ac = kzalloc(sizeof(*ac), GFP_KERNEL);
709	input_dev = input_allocate_device();
710	if (!ac || !input_dev) {
711		err = -ENOMEM;
712		goto err_free_mem;
713	}
714
715	ac->fifo_delay = fifo_delay_default;
716
717	pdata = dev->platform_data;
718	if (!pdata) {
719		dev_dbg(dev,
720			"No platform data: Using default initialization\n");
721		pdata = &adxl34x_default_init;
722	}
723
724	ac->pdata = *pdata;
725	pdata = &ac->pdata;
726
727	ac->input = input_dev;
728	ac->dev = dev;
729	ac->irq = irq;
730	ac->bops = bops;
731
732	mutex_init(&ac->mutex);
733
734	input_dev->name = "ADXL34x accelerometer";
735	revid = ac->bops->read(dev, DEVID);
736
737	switch (revid) {
738	case ID_ADXL345:
739		ac->model = 345;
740		break;
741	case ID_ADXL346:
742		ac->model = 346;
743		break;
744	default:
745		dev_err(dev, "Failed to probe %s\n", input_dev->name);
746		err = -ENODEV;
747		goto err_free_mem;
748	}
749
750	snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
751
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;
758
759	input_set_drvdata(input_dev, ac);
760
761	__set_bit(ac->pdata.ev_type, input_dev->evbit);
762
763	if (ac->pdata.ev_type == EV_REL) {
764		__set_bit(REL_X, input_dev->relbit);
765		__set_bit(REL_Y, input_dev->relbit);
766		__set_bit(REL_Z, input_dev->relbit);
767	} else {
768		/* EV_ABS */
769		__set_bit(ABS_X, input_dev->absbit);
770		__set_bit(ABS_Y, input_dev->absbit);
771		__set_bit(ABS_Z, input_dev->absbit);
772
773		if (pdata->data_range & FULL_RES)
774			range = ADXL_FULLRES_MAX_VAL;	/* Signed 13-bit */
775		else
776			range = ADXL_FIXEDRES_MAX_VAL;	/* Signed 10-bit */
777
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);
781	}
782
783	__set_bit(EV_KEY, input_dev->evbit);
784	__set_bit(pdata->ev_code_tap[ADXL_X_AXIS], input_dev->keybit);
785	__set_bit(pdata->ev_code_tap[ADXL_Y_AXIS], input_dev->keybit);
786	__set_bit(pdata->ev_code_tap[ADXL_Z_AXIS], input_dev->keybit);
787
788	if (pdata->ev_code_ff) {
789		ac->int_mask = FREE_FALL;
790		__set_bit(pdata->ev_code_ff, input_dev->keybit);
791	}
792
793	if (pdata->ev_code_act_inactivity)
794		__set_bit(pdata->ev_code_act_inactivity, input_dev->keybit);
795
796	ac->int_mask |= ACTIVITY | INACTIVITY;
797
798	if (pdata->watermark) {
799		ac->int_mask |= WATERMARK;
800		if (!FIFO_MODE(pdata->fifo_mode))
801			ac->pdata.fifo_mode |= FIFO_STREAM;
802	} else {
803		ac->int_mask |= DATA_READY;
804	}
805
806	if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
807		ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
808
809	if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
810		ac->fifo_delay = false;
811
812	ac->bops->write(dev, POWER_CTL, 0);
813
814	err = request_threaded_irq(ac->irq, NULL, adxl34x_irq,
815				   IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
816				   dev_name(dev), ac);
817	if (err) {
818		dev_err(dev, "irq %d busy?\n", ac->irq);
819		goto err_free_mem;
820	}
821
822	err = sysfs_create_group(&dev->kobj, &adxl34x_attr_group);
823	if (err)
824		goto err_free_irq;
825
826	err = input_register_device(input_dev);
827	if (err)
828		goto err_remove_attr;
829
830	AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
831	AC_WRITE(ac, OFSX, pdata->x_axis_offset);
832	ac->hwcal.x = pdata->x_axis_offset;
833	AC_WRITE(ac, OFSY, pdata->y_axis_offset);
834	ac->hwcal.y = pdata->y_axis_offset;
835	AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
836	ac->hwcal.z = pdata->z_axis_offset;
837	AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
838	AC_WRITE(ac, DUR, pdata->tap_duration);
839	AC_WRITE(ac, LATENT, pdata->tap_latency);
840	AC_WRITE(ac, WINDOW, pdata->tap_window);
841	AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
842	AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
843	AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
844	AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
845	AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
846	AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
847	AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
848	AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
849		 (pdata->low_power_mode ? LOW_POWER : 0));
850	AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
851	AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
852			SAMPLES(pdata->watermark));
853
854	if (pdata->use_int2) {
855		/* Map all INTs to INT2 */
856		AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
857	} else {
858		/* Map all INTs to INT1 */
859		AC_WRITE(ac, INT_MAP, 0);
860	}
861
862	if (ac->model == 346 && ac->pdata.orientation_enable) {
863		AC_WRITE(ac, ORIENT_CONF,
864			ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
865			ORIENT_DIVISOR(ac->pdata.divisor_length));
866
867		ac->orient2d_saved = 1234;
868		ac->orient3d_saved = 1234;
869
870		if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
871			for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
872				__set_bit(pdata->ev_codes_orient_3d[i],
873					  input_dev->keybit);
874
875		if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
876			for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
877				__set_bit(pdata->ev_codes_orient_2d[i],
878					  input_dev->keybit);
879	} else {
880		ac->pdata.orientation_enable = 0;
881	}
882
883	AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
884
885	ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
886
887	return ac;
888
889 err_remove_attr:
890	sysfs_remove_group(&dev->kobj, &adxl34x_attr_group);
891 err_free_irq:
892	free_irq(ac->irq, ac);
893 err_free_mem:
894	input_free_device(input_dev);
895	kfree(ac);
896 err_out:
897	return ERR_PTR(err);
898}
899EXPORT_SYMBOL_GPL(adxl34x_probe);
900
901int adxl34x_remove(struct adxl34x *ac)
902{
903	sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
904	free_irq(ac->irq, ac);
905	input_unregister_device(ac->input);
906	dev_dbg(ac->dev, "unregistered accelerometer\n");
907	kfree(ac);
908
909	return 0;
910}
911EXPORT_SYMBOL_GPL(adxl34x_remove);
912
913MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
914MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
915MODULE_LICENSE("GPL");