aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorMichael Hennerich <michael.hennerich@analog.com>2010-06-25 11:44:10 -0400
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2010-06-25 11:55:07 -0400
commite27c729219ad24c8ac9a4b34cf192e56917565c5 (patch)
treeb8cc2d09a31eac384aa90dd99304081e2ef95846 /drivers
parent69a4af606ed4836faa2ec69b1d217f384b8235e7 (diff)
Input: add driver for ADXL345/346 Digital Accelerometers
This is a driver for the ADXL345/346 Three-Axis Digital Accelerometers. Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Signed-off-by: Chris Verges <chrisv@cyberswitching.com> Signed-off-by: Luotao Fu <l.fu@pengutronix.de> Signed-off-by: Barry Song <barry.song@analog.com> Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/input/misc/Kconfig37
-rw-r--r--drivers/input/misc/Makefile3
-rw-r--r--drivers/input/misc/adxl34x-i2c.c163
-rw-r--r--drivers/input/misc/adxl34x-spi.c145
-rw-r--r--drivers/input/misc/adxl34x.c840
-rw-r--r--drivers/input/misc/adxl34x.h30
6 files changed, 1218 insertions, 0 deletions
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index c44b9eafc556..ede6d52fe95c 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -390,4 +390,41 @@ config INPUT_PCAP
390 To compile this driver as a module, choose M here: the 390 To compile this driver as a module, choose M here: the
391 module will be called pcap_keys. 391 module will be called pcap_keys.
392 392
393config INPUT_ADXL34X
394 tristate "Analog Devices ADXL34x Three-Axis Digital Accelerometer"
395 default n
396 help
397 Say Y here if you have a Accelerometer interface using the
398 ADXL345/6 controller, and your board-specific initialization
399 code includes that in its table of devices.
400
401 This driver can use either I2C or SPI communication to the
402 ADXL345/6 controller. Select the appropriate method for
403 your system.
404
405 If unsure, say N (but it's safe to say "Y").
406
407 To compile this driver as a module, choose M here: the
408 module will be called adxl34x.
409
410config INPUT_ADXL34X_I2C
411 tristate "support I2C bus connection"
412 depends on INPUT_ADXL34X && I2C
413 default y
414 help
415 Say Y here if you have ADXL345/6 hooked to an I2C bus.
416
417 To compile this driver as a module, choose M here: the
418 module will be called adxl34x-i2c.
419
420config INPUT_ADXL34X_SPI
421 tristate "support SPI bus connection"
422 depends on INPUT_ADXL34X && SPI
423 default y
424 help
425 Say Y here if you have ADXL345/6 hooked to a SPI bus.
426
427 To compile this driver as a module, choose M here: the
428 module will be called adxl34x-spi.
429
393endif 430endif
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
index 71fe57d8023f..97b5dc32df19 100644
--- a/drivers/input/misc/Makefile
+++ b/drivers/input/misc/Makefile
@@ -8,6 +8,9 @@ obj-$(CONFIG_INPUT_88PM860X_ONKEY) += 88pm860x_onkey.o
8obj-$(CONFIG_INPUT_AD714X) += ad714x.o 8obj-$(CONFIG_INPUT_AD714X) += ad714x.o
9obj-$(CONFIG_INPUT_AD714X_I2C) += ad714x-i2c.o 9obj-$(CONFIG_INPUT_AD714X_I2C) += ad714x-i2c.o
10obj-$(CONFIG_INPUT_AD714X_SPI) += ad714x-spi.o 10obj-$(CONFIG_INPUT_AD714X_SPI) += ad714x-spi.o
11obj-$(CONFIG_INPUT_ADXL34X) += adxl34x.o
12obj-$(CONFIG_INPUT_ADXL34X_I2C) += adxl34x-i2c.o
13obj-$(CONFIG_INPUT_ADXL34X_SPI) += adxl34x-spi.o
11obj-$(CONFIG_INPUT_APANEL) += apanel.o 14obj-$(CONFIG_INPUT_APANEL) += apanel.o
12obj-$(CONFIG_INPUT_ATI_REMOTE) += ati_remote.o 15obj-$(CONFIG_INPUT_ATI_REMOTE) += ati_remote.o
13obj-$(CONFIG_INPUT_ATI_REMOTE2) += ati_remote2.o 16obj-$(CONFIG_INPUT_ATI_REMOTE2) += ati_remote2.o
diff --git a/drivers/input/misc/adxl34x-i2c.c b/drivers/input/misc/adxl34x-i2c.c
new file mode 100644
index 000000000000..76194b58bd0b
--- /dev/null
+++ b/drivers/input/misc/adxl34x-i2c.c
@@ -0,0 +1,163 @@
1/*
2 * ADLX345/346 Three-Axis Digital Accelerometers (I2C Interface)
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/input.h> /* BUS_I2C */
11#include <linux/i2c.h>
12#include <linux/module.h>
13#include <linux/types.h>
14#include "adxl34x.h"
15
16static int adxl34x_smbus_read(struct device *dev, unsigned char reg)
17{
18 struct i2c_client *client = to_i2c_client(dev);
19
20 return i2c_smbus_read_byte_data(client, reg);
21}
22
23static int adxl34x_smbus_write(struct device *dev,
24 unsigned char reg, unsigned char val)
25{
26 struct i2c_client *client = to_i2c_client(dev);
27
28 return i2c_smbus_write_byte_data(client, reg, val);
29}
30
31static int adxl34x_smbus_read_block(struct device *dev,
32 unsigned char reg, int count,
33 void *buf)
34{
35 struct i2c_client *client = to_i2c_client(dev);
36
37 return i2c_smbus_read_i2c_block_data(client, reg, count, buf);
38}
39
40static int adxl34x_i2c_read_block(struct device *dev,
41 unsigned char reg, int count,
42 void *buf)
43{
44 struct i2c_client *client = to_i2c_client(dev);
45 int ret;
46
47 ret = i2c_master_send(client, &reg, 1);
48 if (ret < 0)
49 return ret;
50
51 ret = i2c_master_recv(client, buf, count);
52 if (ret < 0)
53 return ret;
54
55 if (ret != count)
56 return -EIO;
57
58 return 0;
59}
60
61static const struct adxl34x_bus_ops adx134x_smbus_bops = {
62 .bustype = BUS_I2C,
63 .write = adxl34x_smbus_write,
64 .read = adxl34x_smbus_read,
65 .read_block = adxl34x_smbus_read_block,
66};
67
68static const struct adxl34x_bus_ops adx134x_i2c_bops = {
69 .bustype = BUS_I2C,
70 .write = adxl34x_smbus_write,
71 .read = adxl34x_smbus_read,
72 .read_block = adxl34x_i2c_read_block,
73};
74
75static int __devinit adxl34x_i2c_probe(struct i2c_client *client,
76 const struct i2c_device_id *id)
77{
78 struct adxl34x *ac;
79 int error;
80
81 error = i2c_check_functionality(client->adapter,
82 I2C_FUNC_SMBUS_BYTE_DATA);
83 if (!error) {
84 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
85 return -EIO;
86 }
87
88 ac = adxl34x_probe(&client->dev, client->irq, false,
89 i2c_check_functionality(client->adapter,
90 I2C_FUNC_SMBUS_READ_I2C_BLOCK) ?
91 &adx134x_smbus_bops : &adx134x_i2c_bops);
92 if (IS_ERR(ac))
93 return PTR_ERR(ac);
94
95 i2c_set_clientdata(client, ac);
96
97 return 0;
98}
99
100static int __devexit adxl34x_i2c_remove(struct i2c_client *client)
101{
102 struct adxl34x *ac = i2c_get_clientdata(client);
103
104 return adxl34x_remove(ac);
105}
106
107#ifdef CONFIG_PM
108static int adxl34x_suspend(struct i2c_client *client, pm_message_t message)
109{
110 struct adxl34x *ac = i2c_get_clientdata(client);
111
112 adxl34x_disable(ac);
113
114 return 0;
115}
116
117static int adxl34x_resume(struct i2c_client *client)
118{
119 struct adxl34x *ac = i2c_get_clientdata(client);
120
121 adxl34x_enable(ac);
122
123 return 0;
124}
125#else
126# define adxl34x_suspend NULL
127# define adxl34x_resume NULL
128#endif
129
130static const struct i2c_device_id adxl34x_id[] = {
131 { "adxl34x", 0 },
132 { }
133};
134
135MODULE_DEVICE_TABLE(i2c, adxl34x_id);
136
137static struct i2c_driver adxl34x_driver = {
138 .driver = {
139 .name = "adxl34x",
140 .owner = THIS_MODULE,
141 },
142 .probe = adxl34x_i2c_probe,
143 .remove = __devexit_p(adxl34x_i2c_remove),
144 .suspend = adxl34x_suspend,
145 .resume = adxl34x_resume,
146 .id_table = adxl34x_id,
147};
148
149static int __init adxl34x_i2c_init(void)
150{
151 return i2c_add_driver(&adxl34x_driver);
152}
153module_init(adxl34x_i2c_init);
154
155static void __exit adxl34x_i2c_exit(void)
156{
157 i2c_del_driver(&adxl34x_driver);
158}
159module_exit(adxl34x_i2c_exit);
160
161MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
162MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
163MODULE_LICENSE("GPL");
diff --git a/drivers/input/misc/adxl34x-spi.c b/drivers/input/misc/adxl34x-spi.c
new file mode 100644
index 000000000000..7f992353ffdd
--- /dev/null
+++ b/drivers/input/misc/adxl34x-spi.c
@@ -0,0 +1,145 @@
1/*
2 * ADLX345/346 Three-Axis Digital Accelerometers (SPI Interface)
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/input.h> /* BUS_SPI */
11#include <linux/module.h>
12#include <linux/spi/spi.h>
13#include <linux/types.h>
14#include "adxl34x.h"
15
16#define MAX_SPI_FREQ_HZ 5000000
17#define MAX_FREQ_NO_FIFODELAY 1500000
18#define ADXL34X_CMD_MULTB (1 << 6)
19#define ADXL34X_CMD_READ (1 << 7)
20#define ADXL34X_WRITECMD(reg) (reg & 0x3F)
21#define ADXL34X_READCMD(reg) (ADXL34X_CMD_READ | (reg & 0x3F))
22#define ADXL34X_READMB_CMD(reg) (ADXL34X_CMD_READ | ADXL34X_CMD_MULTB \
23 | (reg & 0x3F))
24
25static int adxl34x_spi_read(struct device *dev, unsigned char reg)
26{
27 struct spi_device *spi = to_spi_device(dev);
28 unsigned char cmd;
29
30 cmd = ADXL34X_READCMD(reg);
31
32 return spi_w8r8(spi, cmd);
33}
34
35static int adxl34x_spi_write(struct device *dev,
36 unsigned char reg, unsigned char val)
37{
38 struct spi_device *spi = to_spi_device(dev);
39 unsigned char buf[2];
40
41 buf[0] = ADXL34X_WRITECMD(reg);
42 buf[1] = val;
43
44 return spi_write(spi, buf, sizeof(buf));
45}
46
47static int adxl34x_spi_read_block(struct device *dev,
48 unsigned char reg, int count,
49 void *buf)
50{
51 struct spi_device *spi = to_spi_device(dev);
52 ssize_t status;
53
54 reg = ADXL34X_READMB_CMD(reg);
55 status = spi_write_then_read(spi, &reg, 1, buf, count);
56
57 return (status < 0) ? status : 0;
58}
59
60static const struct adxl34x_bus_ops adx134x_spi_bops = {
61 .bustype = BUS_SPI,
62 .write = adxl34x_spi_write,
63 .read = adxl34x_spi_read,
64 .read_block = adxl34x_spi_read_block,
65};
66
67static int __devinit adxl34x_spi_probe(struct spi_device *spi)
68{
69 struct adxl34x *ac;
70
71 /* don't exceed max specified SPI CLK frequency */
72 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
73 dev_err(&spi->dev, "SPI CLK %d Hz too fast\n", spi->max_speed_hz);
74 return -EINVAL;
75 }
76
77 ac = adxl34x_probe(&spi->dev, spi->irq,
78 spi->max_speed_hz > MAX_FREQ_NO_FIFODELAY,
79 &adx134x_spi_bops);
80
81 if (IS_ERR(ac))
82 return PTR_ERR(ac);
83
84 spi_set_drvdata(spi, ac);
85
86 return 0;
87}
88
89static int __devexit adxl34x_spi_remove(struct spi_device *spi)
90{
91 struct adxl34x *ac = dev_get_drvdata(&spi->dev);
92
93 return adxl34x_remove(ac);
94}
95
96#ifdef CONFIG_PM
97static int adxl34x_suspend(struct spi_device *spi, pm_message_t message)
98{
99 struct adxl34x *ac = dev_get_drvdata(&spi->dev);
100
101 adxl34x_disable(ac);
102
103 return 0;
104}
105
106static int adxl34x_resume(struct spi_device *spi)
107{
108 struct adxl34x *ac = dev_get_drvdata(&spi->dev);
109
110 adxl34x_enable(ac);
111
112 return 0;
113}
114#else
115# define adxl34x_suspend NULL
116# define adxl34x_resume NULL
117#endif
118
119static struct spi_driver adxl34x_driver = {
120 .driver = {
121 .name = "adxl34x",
122 .bus = &spi_bus_type,
123 .owner = THIS_MODULE,
124 },
125 .probe = adxl34x_spi_probe,
126 .remove = __devexit_p(adxl34x_spi_remove),
127 .suspend = adxl34x_suspend,
128 .resume = adxl34x_resume,
129};
130
131static int __init adxl34x_spi_init(void)
132{
133 return spi_register_driver(&adxl34x_driver);
134}
135module_init(adxl34x_spi_init);
136
137static void __exit adxl34x_spi_exit(void)
138{
139 spi_unregister_driver(&adxl34x_driver);
140}
141module_exit(adxl34x_spi_exit);
142
143MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
144MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer SPI Bus Driver");
145MODULE_LICENSE("GPL");
diff --git a/drivers/input/misc/adxl34x.c b/drivers/input/misc/adxl34x.c
new file mode 100644
index 000000000000..07f9ef631540
--- /dev/null
+++ b/drivers/input/misc/adxl34x.c
@@ -0,0 +1,840 @@
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 bool disabled; /* P: mutex */
200 bool opened; /* P: mutex */
201 bool fifo_delay;
202 int irq;
203 unsigned model;
204 unsigned int_mask;
205
206 const struct adxl34x_bus_ops *bops;
207};
208
209static const struct adxl34x_platform_data adxl34x_default_init = {
210 .tap_threshold = 35,
211 .tap_duration = 3,
212 .tap_latency = 20,
213 .tap_window = 20,
214 .tap_axis_control = ADXL_TAP_X_EN | ADXL_TAP_Y_EN | ADXL_TAP_Z_EN,
215 .act_axis_control = 0xFF,
216 .activity_threshold = 6,
217 .inactivity_threshold = 4,
218 .inactivity_time = 3,
219 .free_fall_threshold = 8,
220 .free_fall_time = 0x20,
221 .data_rate = 8,
222 .data_range = ADXL_FULL_RES,
223
224 .ev_type = EV_ABS,
225 .ev_code_x = ABS_X, /* EV_REL */
226 .ev_code_y = ABS_Y, /* EV_REL */
227 .ev_code_z = ABS_Z, /* EV_REL */
228
229 .ev_code_tap = {BTN_TOUCH, BTN_TOUCH, BTN_TOUCH}, /* EV_KEY {x,y,z} */
230 .power_mode = ADXL_AUTO_SLEEP | ADXL_LINK,
231 .fifo_mode = FIFO_STREAM,
232 .watermark = 0,
233};
234
235static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
236{
237 short buf[3];
238
239 ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
240
241 mutex_lock(&ac->mutex);
242 ac->saved.x = (s16) le16_to_cpu(buf[0]);
243 axis->x = ac->saved.x;
244
245 ac->saved.y = (s16) le16_to_cpu(buf[1]);
246 axis->y = ac->saved.y;
247
248 ac->saved.z = (s16) le16_to_cpu(buf[2]);
249 axis->z = ac->saved.z;
250 mutex_unlock(&ac->mutex);
251}
252
253static void adxl34x_service_ev_fifo(struct adxl34x *ac)
254{
255 struct adxl34x_platform_data *pdata = &ac->pdata;
256 struct axis_triple axis;
257
258 adxl34x_get_triple(ac, &axis);
259
260 input_event(ac->input, pdata->ev_type, pdata->ev_code_x,
261 axis.x - ac->swcal.x);
262 input_event(ac->input, pdata->ev_type, pdata->ev_code_y,
263 axis.y - ac->swcal.y);
264 input_event(ac->input, pdata->ev_type, pdata->ev_code_z,
265 axis.z - ac->swcal.z);
266}
267
268static void adxl34x_report_key_single(struct input_dev *input, int key)
269{
270 input_report_key(input, key, true);
271 input_sync(input);
272 input_report_key(input, key, false);
273}
274
275static void adxl34x_send_key_events(struct adxl34x *ac,
276 struct adxl34x_platform_data *pdata, int status, int press)
277{
278 int i;
279
280 for (i = ADXL_X_AXIS; i <= ADXL_Z_AXIS; i++) {
281 if (status & (1 << (ADXL_Z_AXIS - i)))
282 input_report_key(ac->input,
283 pdata->ev_code_tap[i], press);
284 }
285}
286
287static void adxl34x_do_tap(struct adxl34x *ac,
288 struct adxl34x_platform_data *pdata, int status)
289{
290 adxl34x_send_key_events(ac, pdata, status, true);
291 input_sync(ac->input);
292 adxl34x_send_key_events(ac, pdata, status, false);
293}
294
295static irqreturn_t adxl34x_irq(int irq, void *handle)
296{
297 struct adxl34x *ac = handle;
298 struct adxl34x_platform_data *pdata = &ac->pdata;
299 int int_stat, tap_stat, samples;
300
301 /*
302 * ACT_TAP_STATUS should be read before clearing the interrupt
303 * Avoid reading ACT_TAP_STATUS in case TAP detection is disabled
304 */
305
306 if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
307 tap_stat = AC_READ(ac, ACT_TAP_STATUS);
308 else
309 tap_stat = 0;
310
311 int_stat = AC_READ(ac, INT_SOURCE);
312
313 if (int_stat & FREE_FALL)
314 adxl34x_report_key_single(ac->input, pdata->ev_code_ff);
315
316 if (int_stat & OVERRUN)
317 dev_dbg(ac->dev, "OVERRUN\n");
318
319 if (int_stat & (SINGLE_TAP | DOUBLE_TAP)) {
320 adxl34x_do_tap(ac, pdata, tap_stat);
321
322 if (int_stat & DOUBLE_TAP)
323 adxl34x_do_tap(ac, pdata, tap_stat);
324 }
325
326 if (pdata->ev_code_act_inactivity) {
327 if (int_stat & ACTIVITY)
328 input_report_key(ac->input,
329 pdata->ev_code_act_inactivity, 1);
330 if (int_stat & INACTIVITY)
331 input_report_key(ac->input,
332 pdata->ev_code_act_inactivity, 0);
333 }
334
335 if (int_stat & (DATA_READY | WATERMARK)) {
336
337 if (pdata->fifo_mode)
338 samples = ENTRIES(AC_READ(ac, FIFO_STATUS)) + 1;
339 else
340 samples = 1;
341
342 for (; samples > 0; samples--) {
343 adxl34x_service_ev_fifo(ac);
344 /*
345 * To ensure that the FIFO has
346 * completely popped, there must be at least 5 us between
347 * the end of reading the data registers, signified by the
348 * transition to register 0x38 from 0x37 or the CS pin
349 * going high, and the start of new reads of the FIFO or
350 * reading the FIFO_STATUS register. For SPI operation at
351 * 1.5 MHz or lower, the register addressing portion of the
352 * transmission is sufficient delay to ensure the FIFO has
353 * completely popped. It is necessary for SPI operation
354 * greater than 1.5 MHz to de-assert the CS pin to ensure a
355 * total of 5 us, which is at most 3.4 us at 5 MHz
356 * operation.
357 */
358 if (ac->fifo_delay && (samples > 1))
359 udelay(3);
360 }
361 }
362
363 input_sync(ac->input);
364
365 return IRQ_HANDLED;
366}
367
368static void __adxl34x_disable(struct adxl34x *ac)
369{
370 if (!ac->disabled && ac->opened) {
371 /*
372 * A '0' places the ADXL34x into standby mode
373 * with minimum power consumption.
374 */
375 AC_WRITE(ac, POWER_CTL, 0);
376
377 ac->disabled = true;
378 }
379}
380
381static void __adxl34x_enable(struct adxl34x *ac)
382{
383 if (ac->disabled && ac->opened) {
384 AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
385 ac->disabled = false;
386 }
387}
388
389void adxl34x_disable(struct adxl34x *ac)
390{
391 mutex_lock(&ac->mutex);
392 __adxl34x_disable(ac);
393 mutex_unlock(&ac->mutex);
394}
395EXPORT_SYMBOL_GPL(adxl34x_disable);
396
397void adxl34x_enable(struct adxl34x *ac)
398{
399 mutex_lock(&ac->mutex);
400 __adxl34x_enable(ac);
401 mutex_unlock(&ac->mutex);
402}
403
404EXPORT_SYMBOL_GPL(adxl34x_enable);
405
406static ssize_t adxl34x_disable_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
409 struct adxl34x *ac = dev_get_drvdata(dev);
410
411 return sprintf(buf, "%u\n", ac->disabled);
412}
413
414static ssize_t adxl34x_disable_store(struct device *dev,
415 struct device_attribute *attr,
416 const char *buf, size_t count)
417{
418 struct adxl34x *ac = dev_get_drvdata(dev);
419 unsigned long val;
420 int error;
421
422 error = strict_strtoul(buf, 10, &val);
423 if (error)
424 return error;
425
426 if (val)
427 adxl34x_disable(ac);
428 else
429 adxl34x_enable(ac);
430
431 return count;
432}
433
434static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
435
436static ssize_t adxl34x_calibrate_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
438{
439 struct adxl34x *ac = dev_get_drvdata(dev);
440 ssize_t count;
441
442 mutex_lock(&ac->mutex);
443 count = sprintf(buf, "%d,%d,%d\n",
444 ac->hwcal.x * 4 + ac->swcal.x,
445 ac->hwcal.y * 4 + ac->swcal.y,
446 ac->hwcal.z * 4 + ac->swcal.z);
447 mutex_unlock(&ac->mutex);
448
449 return count;
450}
451
452static ssize_t adxl34x_calibrate_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
458 /*
459 * Hardware offset calibration has a resolution of 15.6 mg/LSB.
460 * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
461 */
462
463 mutex_lock(&ac->mutex);
464 ac->hwcal.x -= (ac->saved.x / 4);
465 ac->swcal.x = ac->saved.x % 4;
466
467 ac->hwcal.y -= (ac->saved.y / 4);
468 ac->swcal.y = ac->saved.y % 4;
469
470 ac->hwcal.z -= (ac->saved.z / 4);
471 ac->swcal.z = ac->saved.z % 4;
472
473 AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
474 AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
475 AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
476 mutex_unlock(&ac->mutex);
477
478 return count;
479}
480
481static DEVICE_ATTR(calibrate, 0664,
482 adxl34x_calibrate_show, adxl34x_calibrate_store);
483
484static ssize_t adxl34x_rate_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
486{
487 struct adxl34x *ac = dev_get_drvdata(dev);
488
489 return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
490}
491
492static ssize_t adxl34x_rate_store(struct device *dev,
493 struct device_attribute *attr,
494 const char *buf, size_t count)
495{
496 struct adxl34x *ac = dev_get_drvdata(dev);
497 unsigned long val;
498 int error;
499
500 error = strict_strtoul(buf, 10, &val);
501 if (error)
502 return error;
503
504 mutex_lock(&ac->mutex);
505
506 ac->pdata.data_rate = RATE(val);
507 AC_WRITE(ac, BW_RATE,
508 ac->pdata.data_rate |
509 (ac->pdata.low_power_mode ? LOW_POWER : 0));
510
511 mutex_unlock(&ac->mutex);
512
513 return count;
514}
515
516static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
517
518static ssize_t adxl34x_autosleep_show(struct device *dev,
519 struct device_attribute *attr, char *buf)
520{
521 struct adxl34x *ac = dev_get_drvdata(dev);
522
523 return sprintf(buf, "%u\n",
524 ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
525}
526
527static ssize_t adxl34x_autosleep_store(struct device *dev,
528 struct device_attribute *attr,
529 const char *buf, size_t count)
530{
531 struct adxl34x *ac = dev_get_drvdata(dev);
532 unsigned long val;
533 int error;
534
535 error = strict_strtoul(buf, 10, &val);
536 if (error)
537 return error;
538
539 mutex_lock(&ac->mutex);
540
541 if (val)
542 ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
543 else
544 ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
545
546 if (!ac->disabled && ac->opened)
547 AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
548
549 mutex_unlock(&ac->mutex);
550
551 return count;
552}
553
554static DEVICE_ATTR(autosleep, 0664,
555 adxl34x_autosleep_show, adxl34x_autosleep_store);
556
557static ssize_t adxl34x_position_show(struct device *dev,
558 struct device_attribute *attr, char *buf)
559{
560 struct adxl34x *ac = dev_get_drvdata(dev);
561 ssize_t count;
562
563 mutex_lock(&ac->mutex);
564 count = sprintf(buf, "(%d, %d, %d)\n",
565 ac->saved.x, ac->saved.y, ac->saved.z);
566 mutex_unlock(&ac->mutex);
567
568 return count;
569}
570
571static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
572
573#ifdef ADXL_DEBUG
574static ssize_t adxl34x_write_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 long val;
580 int error;
581
582 /*
583 * This allows basic ADXL register write access for debug purposes.
584 */
585 error = strict_strtoul(buf, 16, &val);
586 if (error)
587 return error;
588
589 mutex_lock(&ac->mutex);
590 AC_WRITE(ac, val >> 8, val & 0xFF);
591 mutex_unlock(&ac->mutex);
592
593 return count;
594}
595
596static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
597#endif
598
599static struct attribute *adxl34x_attributes[] = {
600 &dev_attr_disable.attr,
601 &dev_attr_calibrate.attr,
602 &dev_attr_rate.attr,
603 &dev_attr_autosleep.attr,
604 &dev_attr_position.attr,
605#ifdef ADXL_DEBUG
606 &dev_attr_write.attr,
607#endif
608 NULL
609};
610
611static const struct attribute_group adxl34x_attr_group = {
612 .attrs = adxl34x_attributes,
613};
614
615static int adxl34x_input_open(struct input_dev *input)
616{
617 struct adxl34x *ac = input_get_drvdata(input);
618
619 mutex_lock(&ac->mutex);
620 ac->opened = true;
621 __adxl34x_enable(ac);
622 mutex_unlock(&ac->mutex);
623
624 return 0;
625}
626
627static void adxl34x_input_close(struct input_dev *input)
628{
629 struct adxl34x *ac = input_get_drvdata(input);
630
631 mutex_lock(&ac->mutex);
632 __adxl34x_disable(ac);
633 ac->opened = false;
634 mutex_unlock(&ac->mutex);
635}
636
637struct adxl34x *adxl34x_probe(struct device *dev, int irq,
638 bool fifo_delay_default,
639 const struct adxl34x_bus_ops *bops)
640{
641 struct adxl34x *ac;
642 struct input_dev *input_dev;
643 const struct adxl34x_platform_data *pdata;
644 int err, range;
645 unsigned char revid;
646
647 if (!irq) {
648 dev_err(dev, "no IRQ?\n");
649 err = -ENODEV;
650 goto err_out;
651 }
652
653 ac = kzalloc(sizeof(*ac), GFP_KERNEL);
654 input_dev = input_allocate_device();
655 if (!ac || !input_dev) {
656 err = -ENOMEM;
657 goto err_out;
658 }
659
660 ac->fifo_delay = fifo_delay_default;
661
662 pdata = dev->platform_data;
663 if (!pdata) {
664 dev_dbg(dev,
665 "No platfrom data: Using default initialization\n");
666 pdata = &adxl34x_default_init;
667 }
668
669 ac->pdata = *pdata;
670 pdata = &ac->pdata;
671
672 ac->input = input_dev;
673 ac->disabled = true;
674 ac->dev = dev;
675 ac->irq = irq;
676 ac->bops = bops;
677
678 mutex_init(&ac->mutex);
679
680 input_dev->name = "ADXL34x accelerometer";
681 revid = ac->bops->read(dev, DEVID);
682
683 switch (revid) {
684 case ID_ADXL345:
685 ac->model = 345;
686 break;
687 case ID_ADXL346:
688 ac->model = 346;
689 break;
690 default:
691 dev_err(dev, "Failed to probe %s\n", input_dev->name);
692 err = -ENODEV;
693 goto err_free_mem;
694 }
695
696 snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
697
698 input_dev->phys = ac->phys;
699 input_dev->dev.parent = dev;
700 input_dev->id.product = ac->model;
701 input_dev->id.bustype = bops->bustype;
702 input_dev->open = adxl34x_input_open;
703 input_dev->close = adxl34x_input_close;
704
705 input_set_drvdata(input_dev, ac);
706
707 __set_bit(ac->pdata.ev_type, input_dev->evbit);
708
709 if (ac->pdata.ev_type == EV_REL) {
710 __set_bit(REL_X, input_dev->relbit);
711 __set_bit(REL_Y, input_dev->relbit);
712 __set_bit(REL_Z, input_dev->relbit);
713 } else {
714 /* EV_ABS */
715 __set_bit(ABS_X, input_dev->absbit);
716 __set_bit(ABS_Y, input_dev->absbit);
717 __set_bit(ABS_Z, input_dev->absbit);
718
719 if (pdata->data_range & FULL_RES)
720 range = ADXL_FULLRES_MAX_VAL; /* Signed 13-bit */
721 else
722 range = ADXL_FIXEDRES_MAX_VAL; /* Signed 10-bit */
723
724 input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
725 input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
726 input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
727 }
728
729 __set_bit(EV_KEY, input_dev->evbit);
730 __set_bit(pdata->ev_code_tap[ADXL_X_AXIS], input_dev->keybit);
731 __set_bit(pdata->ev_code_tap[ADXL_Y_AXIS], input_dev->keybit);
732 __set_bit(pdata->ev_code_tap[ADXL_Z_AXIS], input_dev->keybit);
733
734 if (pdata->ev_code_ff) {
735 ac->int_mask = FREE_FALL;
736 __set_bit(pdata->ev_code_ff, input_dev->keybit);
737 }
738
739 if (pdata->ev_code_act_inactivity)
740 __set_bit(pdata->ev_code_act_inactivity, input_dev->keybit);
741
742 ac->int_mask |= ACTIVITY | INACTIVITY;
743
744 if (pdata->watermark) {
745 ac->int_mask |= WATERMARK;
746 if (!FIFO_MODE(pdata->fifo_mode))
747 ac->pdata.fifo_mode |= FIFO_STREAM;
748 } else {
749 ac->int_mask |= DATA_READY;
750 }
751
752 if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
753 ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
754
755 if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
756 ac->fifo_delay = false;
757
758 ac->bops->write(dev, POWER_CTL, 0);
759
760 err = request_threaded_irq(ac->irq, NULL, adxl34x_irq,
761 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
762 dev_name(dev), ac);
763 if (err) {
764 dev_err(dev, "irq %d busy?\n", ac->irq);
765 goto err_free_mem;
766 }
767
768 err = sysfs_create_group(&dev->kobj, &adxl34x_attr_group);
769 if (err)
770 goto err_free_irq;
771
772 err = input_register_device(input_dev);
773 if (err)
774 goto err_remove_attr;
775
776 AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
777 AC_WRITE(ac, OFSX, pdata->x_axis_offset);
778 ac->hwcal.x = pdata->x_axis_offset;
779 AC_WRITE(ac, OFSY, pdata->y_axis_offset);
780 ac->hwcal.y = pdata->y_axis_offset;
781 AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
782 ac->hwcal.z = pdata->z_axis_offset;
783 AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
784 AC_WRITE(ac, DUR, pdata->tap_duration);
785 AC_WRITE(ac, LATENT, pdata->tap_latency);
786 AC_WRITE(ac, WINDOW, pdata->tap_window);
787 AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
788 AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
789 AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
790 AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
791 AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
792 AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
793 AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
794 AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
795 (pdata->low_power_mode ? LOW_POWER : 0));
796 AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
797 AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
798 SAMPLES(pdata->watermark));
799
800 if (pdata->use_int2)
801 /* Map all INTs to INT2 */
802 AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
803 else
804 /* Map all INTs to INT1 */
805 AC_WRITE(ac, INT_MAP, 0);
806
807 AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
808
809 ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
810
811 return ac;
812
813 err_remove_attr:
814 sysfs_remove_group(&dev->kobj, &adxl34x_attr_group);
815 err_free_irq:
816 free_irq(ac->irq, ac);
817 err_free_mem:
818 input_free_device(input_dev);
819 kfree(ac);
820 err_out:
821 return ERR_PTR(err);
822}
823EXPORT_SYMBOL_GPL(adxl34x_probe);
824
825int adxl34x_remove(struct adxl34x *ac)
826{
827 adxl34x_disable(ac);
828 sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
829 free_irq(ac->irq, ac);
830 input_unregister_device(ac->input);
831 kfree(ac);
832
833 dev_dbg(ac->dev, "unregistered accelerometer\n");
834 return 0;
835}
836EXPORT_SYMBOL_GPL(adxl34x_remove);
837
838MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
839MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
840MODULE_LICENSE("GPL");
diff --git a/drivers/input/misc/adxl34x.h b/drivers/input/misc/adxl34x.h
new file mode 100644
index 000000000000..ea9093c15c81
--- /dev/null
+++ b/drivers/input/misc/adxl34x.h
@@ -0,0 +1,30 @@
1/*
2 * ADXL345/346 Three-Axis Digital Accelerometers (I2C/SPI Interface)
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#ifndef _ADXL34X_H_
11#define _ADXL34X_H_
12
13struct device;
14struct adxl34x;
15
16struct adxl34x_bus_ops {
17 u16 bustype;
18 int (*read)(struct device *, unsigned char);
19 int (*read_block)(struct device *, unsigned char, int, void *);
20 int (*write)(struct device *, unsigned char, unsigned char);
21};
22
23void adxl34x_disable(struct adxl34x *ac);
24void adxl34x_enable(struct adxl34x *ac);
25struct adxl34x *adxl34x_probe(struct device *dev, int irq,
26 bool fifo_delay_default,
27 const struct adxl34x_bus_ops *bops);
28int adxl34x_remove(struct adxl34x *ac);
29
30#endif