aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Ciocca <denis.ciocca@gmail.com>2013-01-25 18:44:00 -0500
committerJonathan Cameron <jic23@kernel.org>2013-01-31 12:56:43 -0500
commitd62511689de5d34d3a07c43db1f46a234bb77b5f (patch)
tree945bdd5e1ab436db7ed3c1d58966cb7291320662
parent23491b513bcd3dfe4ddb94547d73d9deb94eda44 (diff)
iio:accel: Add STMicroelectronics accelerometers driver
This patch adds a generic accelerometer driver for STMicroelectronics accelerometers, currently it supports: LSM303DLH, LSM303DLHC, LIS3DH, LSM330D, LSM330DL, LSM330DLC, LIS331DLH, LSM303DL, LSM303DLM, LSM330. Signed-off-by: Denis Ciocca <denis.ciocca@st.com> Reviewed-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r--drivers/iio/accel/Kconfig31
-rw-r--r--drivers/iio/accel/Makefile8
-rw-r--r--drivers/iio/accel/st_accel.h47
-rw-r--r--drivers/iio/accel/st_accel_buffer.c114
-rw-r--r--drivers/iio/accel/st_accel_core.c495
-rw-r--r--drivers/iio/accel/st_accel_i2c.c87
-rw-r--r--drivers/iio/accel/st_accel_spi.c86
7 files changed, 868 insertions, 0 deletions
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index d6003531bd63..bb594963f91e 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -21,4 +21,35 @@ config KXSD9
21 Say yes here to build support for the Kionix KXSD9 accelerometer. 21 Say yes here to build support for the Kionix KXSD9 accelerometer.
22 Currently this only supports the device via an SPI interface. 22 Currently this only supports the device via an SPI interface.
23 23
24config IIO_ST_ACCEL_3AXIS
25 tristate "STMicroelectronics accelerometers 3-Axis Driver"
26 depends on (I2C || SPI_MASTER) && SYSFS
27 select IIO_ST_SENSORS_CORE
28 select IIO_ST_ACCEL_I2C_3AXIS if (I2C)
29 select IIO_ST_ACCEL_SPI_3AXIS if (SPI_MASTER)
30 select IIO_TRIGGERED_BUFFER if (IIO_BUFFER)
31 select IIO_ST_ACCEL_BUFFER if (IIO_TRIGGERED_BUFFER)
32 help
33 Say yes here to build support for STMicroelectronics accelerometers:
34 LSM303DLH, LSM303DLHC, LIS3DH, LSM330D, LSM330DL, LSM330DLC,
35 LIS331DLH, LSM303DL, LSM303DLM, LSM330.
36
37 This driver can also be built as a module. If so, will be created
38 these modules:
39 - st_accel (core functions for the driver [it is mandatory]);
40 - st_accel_i2c (necessary for the I2C devices [optional*]);
41 - st_accel_spi (necessary for the SPI devices [optional*]);
42
43 (*) one of these is necessary to do something.
44
45config IIO_ST_ACCEL_I2C_3AXIS
46 tristate
47 depends on IIO_ST_ACCEL_3AXIS
48 depends on IIO_ST_SENSORS_I2C
49
50config IIO_ST_ACCEL_SPI_3AXIS
51 tristate
52 depends on IIO_ST_ACCEL_3AXIS
53 depends on IIO_ST_SENSORS_SPI
54
24endmenu 55endmenu
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index 4e1c85987d13..87d8fa264894 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -3,4 +3,12 @@
3# 3#
4 4
5obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o 5obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
6
7obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o
8st_accel-y := st_accel_core.o
9st_accel-$(CONFIG_IIO_BUFFER) += st_accel_buffer.o
10
11obj-$(CONFIG_IIO_ST_ACCEL_I2C_3AXIS) += st_accel_i2c.o
12obj-$(CONFIG_IIO_ST_ACCEL_SPI_3AXIS) += st_accel_spi.o
13
6obj-$(CONFIG_KXSD9) += kxsd9.o 14obj-$(CONFIG_KXSD9) += kxsd9.o
diff --git a/drivers/iio/accel/st_accel.h b/drivers/iio/accel/st_accel.h
new file mode 100644
index 000000000000..37949b94377d
--- /dev/null
+++ b/drivers/iio/accel/st_accel.h
@@ -0,0 +1,47 @@
1/*
2 * STMicroelectronics accelerometers driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 * v. 1.0.0
8 * Licensed under the GPL-2.
9 */
10
11#ifndef ST_ACCEL_H
12#define ST_ACCEL_H
13
14#include <linux/types.h>
15#include <linux/iio/common/st_sensors.h>
16
17#define LSM303DLHC_ACCEL_DEV_NAME "lsm303dlhc_accel"
18#define LIS3DH_ACCEL_DEV_NAME "lis3dh"
19#define LSM330D_ACCEL_DEV_NAME "lsm330d_accel"
20#define LSM330DL_ACCEL_DEV_NAME "lsm330dl_accel"
21#define LSM330DLC_ACCEL_DEV_NAME "lsm330dlc_accel"
22#define LIS331DLH_ACCEL_DEV_NAME "lis331dlh"
23#define LSM303DL_ACCEL_DEV_NAME "lsm303dl_accel"
24#define LSM303DLH_ACCEL_DEV_NAME "lsm303dlh_accel"
25#define LSM303DLM_ACCEL_DEV_NAME "lsm303dlm_accel"
26#define LSM330_ACCEL_DEV_NAME "lsm330_accel"
27
28int st_accel_common_probe(struct iio_dev *indio_dev);
29void st_accel_common_remove(struct iio_dev *indio_dev);
30
31#ifdef CONFIG_IIO_BUFFER
32int st_accel_allocate_ring(struct iio_dev *indio_dev);
33void st_accel_deallocate_ring(struct iio_dev *indio_dev);
34int st_accel_trig_set_state(struct iio_trigger *trig, bool state);
35#define ST_ACCEL_TRIGGER_SET_STATE (&st_accel_trig_set_state)
36#else /* CONFIG_IIO_BUFFER */
37static inline int st_accel_allocate_ring(struct iio_dev *indio_dev)
38{
39 return 0;
40}
41static inline void st_accel_deallocate_ring(struct iio_dev *indio_dev)
42{
43}
44#define ST_ACCEL_TRIGGER_SET_STATE NULL
45#endif /* CONFIG_IIO_BUFFER */
46
47#endif /* ST_ACCEL_H */
diff --git a/drivers/iio/accel/st_accel_buffer.c b/drivers/iio/accel/st_accel_buffer.c
new file mode 100644
index 000000000000..6bd82c7f769c
--- /dev/null
+++ b/drivers/iio/accel/st_accel_buffer.c
@@ -0,0 +1,114 @@
1/*
2 * STMicroelectronics accelerometers driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/stat.h>
15#include <linux/interrupt.h>
16#include <linux/i2c.h>
17#include <linux/delay.h>
18#include <linux/iio/iio.h>
19#include <linux/iio/buffer.h>
20#include <linux/iio/trigger_consumer.h>
21#include <linux/iio/triggered_buffer.h>
22
23#include <linux/iio/common/st_sensors.h>
24#include "st_accel.h"
25
26int st_accel_trig_set_state(struct iio_trigger *trig, bool state)
27{
28 struct iio_dev *indio_dev = trig->private_data;
29
30 return st_sensors_set_dataready_irq(indio_dev, state);
31}
32
33static int st_accel_buffer_preenable(struct iio_dev *indio_dev)
34{
35 int err;
36
37 err = st_sensors_set_enable(indio_dev, true);
38 if (err < 0)
39 goto st_accel_set_enable_error;
40
41 err = iio_sw_buffer_preenable(indio_dev);
42
43st_accel_set_enable_error:
44 return err;
45}
46
47static int st_accel_buffer_postenable(struct iio_dev *indio_dev)
48{
49 int err;
50 struct st_sensor_data *adata = iio_priv(indio_dev);
51
52 adata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
53 if (adata->buffer_data == NULL) {
54 err = -ENOMEM;
55 goto allocate_memory_error;
56 }
57
58 err = st_sensors_set_axis_enable(indio_dev,
59 (u8)indio_dev->active_scan_mask[0]);
60 if (err < 0)
61 goto st_accel_buffer_postenable_error;
62
63 err = iio_triggered_buffer_postenable(indio_dev);
64 if (err < 0)
65 goto st_accel_buffer_postenable_error;
66
67 return err;
68
69st_accel_buffer_postenable_error:
70 kfree(adata->buffer_data);
71allocate_memory_error:
72 return err;
73}
74
75static int st_accel_buffer_predisable(struct iio_dev *indio_dev)
76{
77 int err;
78 struct st_sensor_data *adata = iio_priv(indio_dev);
79
80 err = iio_triggered_buffer_predisable(indio_dev);
81 if (err < 0)
82 goto st_accel_buffer_predisable_error;
83
84 err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
85 if (err < 0)
86 goto st_accel_buffer_predisable_error;
87
88 err = st_sensors_set_enable(indio_dev, false);
89
90st_accel_buffer_predisable_error:
91 kfree(adata->buffer_data);
92 return err;
93}
94
95static const struct iio_buffer_setup_ops st_accel_buffer_setup_ops = {
96 .preenable = &st_accel_buffer_preenable,
97 .postenable = &st_accel_buffer_postenable,
98 .predisable = &st_accel_buffer_predisable,
99};
100
101int st_accel_allocate_ring(struct iio_dev *indio_dev)
102{
103 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
104 &st_sensors_trigger_handler, &st_accel_buffer_setup_ops);
105}
106
107void st_accel_deallocate_ring(struct iio_dev *indio_dev)
108{
109 iio_triggered_buffer_cleanup(indio_dev);
110}
111
112MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
113MODULE_DESCRIPTION("STMicroelectronics accelerometers buffer");
114MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
new file mode 100644
index 000000000000..a235de252a90
--- /dev/null
+++ b/drivers/iio/accel/st_accel_core.c
@@ -0,0 +1,495 @@
1/*
2 * STMicroelectronics accelerometers driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/errno.h>
15#include <linux/types.h>
16#include <linux/mutex.h>
17#include <linux/interrupt.h>
18#include <linux/i2c.h>
19#include <linux/gpio.h>
20#include <linux/irq.h>
21#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23#include <linux/iio/trigger_consumer.h>
24#include <linux/iio/buffer.h>
25
26#include <linux/iio/common/st_sensors.h>
27#include "st_accel.h"
28
29/* DEFAULT VALUE FOR SENSORS */
30#define ST_ACCEL_DEFAULT_OUT_X_L_ADDR 0x28
31#define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR 0x2a
32#define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR 0x2c
33
34/* FULLSCALE */
35#define ST_ACCEL_FS_AVL_2G 2
36#define ST_ACCEL_FS_AVL_4G 4
37#define ST_ACCEL_FS_AVL_6G 6
38#define ST_ACCEL_FS_AVL_8G 8
39#define ST_ACCEL_FS_AVL_16G 16
40
41/* CUSTOM VALUES FOR SENSOR 1 */
42#define ST_ACCEL_1_WAI_EXP 0x33
43#define ST_ACCEL_1_ODR_ADDR 0x20
44#define ST_ACCEL_1_ODR_MASK 0xf0
45#define ST_ACCEL_1_ODR_AVL_1HZ_VAL 0x01
46#define ST_ACCEL_1_ODR_AVL_10HZ_VAL 0x02
47#define ST_ACCEL_1_ODR_AVL_25HZ_VAL 0x03
48#define ST_ACCEL_1_ODR_AVL_50HZ_VAL 0x04
49#define ST_ACCEL_1_ODR_AVL_100HZ_VAL 0x05
50#define ST_ACCEL_1_ODR_AVL_200HZ_VAL 0x06
51#define ST_ACCEL_1_ODR_AVL_400HZ_VAL 0x07
52#define ST_ACCEL_1_ODR_AVL_1600HZ_VAL 0x08
53#define ST_ACCEL_1_FS_ADDR 0x23
54#define ST_ACCEL_1_FS_MASK 0x30
55#define ST_ACCEL_1_FS_AVL_2_VAL 0x00
56#define ST_ACCEL_1_FS_AVL_4_VAL 0x01
57#define ST_ACCEL_1_FS_AVL_8_VAL 0x02
58#define ST_ACCEL_1_FS_AVL_16_VAL 0x03
59#define ST_ACCEL_1_FS_AVL_2_GAIN IIO_G_TO_M_S_2(1000)
60#define ST_ACCEL_1_FS_AVL_4_GAIN IIO_G_TO_M_S_2(2000)
61#define ST_ACCEL_1_FS_AVL_8_GAIN IIO_G_TO_M_S_2(4000)
62#define ST_ACCEL_1_FS_AVL_16_GAIN IIO_G_TO_M_S_2(12000)
63#define ST_ACCEL_1_BDU_ADDR 0x23
64#define ST_ACCEL_1_BDU_MASK 0x80
65#define ST_ACCEL_1_DRDY_IRQ_ADDR 0x22
66#define ST_ACCEL_1_DRDY_IRQ_MASK 0x10
67#define ST_ACCEL_1_MULTIREAD_BIT true
68
69/* CUSTOM VALUES FOR SENSOR 2 */
70#define ST_ACCEL_2_WAI_EXP 0x32
71#define ST_ACCEL_2_ODR_ADDR 0x20
72#define ST_ACCEL_2_ODR_MASK 0x18
73#define ST_ACCEL_2_ODR_AVL_50HZ_VAL 0x00
74#define ST_ACCEL_2_ODR_AVL_100HZ_VAL 0x01
75#define ST_ACCEL_2_ODR_AVL_400HZ_VAL 0x02
76#define ST_ACCEL_2_ODR_AVL_1000HZ_VAL 0x03
77#define ST_ACCEL_2_PW_ADDR 0x20
78#define ST_ACCEL_2_PW_MASK 0xe0
79#define ST_ACCEL_2_FS_ADDR 0x23
80#define ST_ACCEL_2_FS_MASK 0x30
81#define ST_ACCEL_2_FS_AVL_2_VAL 0X00
82#define ST_ACCEL_2_FS_AVL_4_VAL 0X01
83#define ST_ACCEL_2_FS_AVL_8_VAL 0x03
84#define ST_ACCEL_2_FS_AVL_2_GAIN IIO_G_TO_M_S_2(1000)
85#define ST_ACCEL_2_FS_AVL_4_GAIN IIO_G_TO_M_S_2(2000)
86#define ST_ACCEL_2_FS_AVL_8_GAIN IIO_G_TO_M_S_2(3900)
87#define ST_ACCEL_2_BDU_ADDR 0x23
88#define ST_ACCEL_2_BDU_MASK 0x80
89#define ST_ACCEL_2_DRDY_IRQ_ADDR 0x22
90#define ST_ACCEL_2_DRDY_IRQ_MASK 0x02
91#define ST_ACCEL_2_MULTIREAD_BIT true
92
93/* CUSTOM VALUES FOR SENSOR 3 */
94#define ST_ACCEL_3_WAI_EXP 0x40
95#define ST_ACCEL_3_ODR_ADDR 0x20
96#define ST_ACCEL_3_ODR_MASK 0xf0
97#define ST_ACCEL_3_ODR_AVL_3HZ_VAL 0x01
98#define ST_ACCEL_3_ODR_AVL_6HZ_VAL 0x02
99#define ST_ACCEL_3_ODR_AVL_12HZ_VAL 0x03
100#define ST_ACCEL_3_ODR_AVL_25HZ_VAL 0x04
101#define ST_ACCEL_3_ODR_AVL_50HZ_VAL 0x05
102#define ST_ACCEL_3_ODR_AVL_100HZ_VAL 0x06
103#define ST_ACCEL_3_ODR_AVL_200HZ_VAL 0x07
104#define ST_ACCEL_3_ODR_AVL_400HZ_VAL 0x08
105#define ST_ACCEL_3_ODR_AVL_800HZ_VAL 0x09
106#define ST_ACCEL_3_ODR_AVL_1600HZ_VAL 0x0a
107#define ST_ACCEL_3_FS_ADDR 0x24
108#define ST_ACCEL_3_FS_MASK 0x38
109#define ST_ACCEL_3_FS_AVL_2_VAL 0X00
110#define ST_ACCEL_3_FS_AVL_4_VAL 0X01
111#define ST_ACCEL_3_FS_AVL_6_VAL 0x02
112#define ST_ACCEL_3_FS_AVL_8_VAL 0x03
113#define ST_ACCEL_3_FS_AVL_16_VAL 0x04
114#define ST_ACCEL_3_FS_AVL_2_GAIN IIO_G_TO_M_S_2(61)
115#define ST_ACCEL_3_FS_AVL_4_GAIN IIO_G_TO_M_S_2(122)
116#define ST_ACCEL_3_FS_AVL_6_GAIN IIO_G_TO_M_S_2(183)
117#define ST_ACCEL_3_FS_AVL_8_GAIN IIO_G_TO_M_S_2(244)
118#define ST_ACCEL_3_FS_AVL_16_GAIN IIO_G_TO_M_S_2(732)
119#define ST_ACCEL_3_BDU_ADDR 0x20
120#define ST_ACCEL_3_BDU_MASK 0x08
121#define ST_ACCEL_3_DRDY_IRQ_ADDR 0x23
122#define ST_ACCEL_3_DRDY_IRQ_MASK 0x80
123#define ST_ACCEL_3_IG1_EN_ADDR 0x23
124#define ST_ACCEL_3_IG1_EN_MASK 0x08
125#define ST_ACCEL_3_MULTIREAD_BIT false
126
127static const struct iio_chan_spec st_accel_12bit_channels[] = {
128 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, ST_SENSORS_SCAN_X, IIO_MOD_X, IIO_LE,
129 ST_SENSORS_DEFAULT_12_REALBITS, ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
130 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, ST_SENSORS_SCAN_Y, IIO_MOD_Y, IIO_LE,
131 ST_SENSORS_DEFAULT_12_REALBITS, ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
132 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, ST_SENSORS_SCAN_Z, IIO_MOD_Z, IIO_LE,
133 ST_SENSORS_DEFAULT_12_REALBITS, ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
134 IIO_CHAN_SOFT_TIMESTAMP(3)
135};
136
137static const struct iio_chan_spec st_accel_16bit_channels[] = {
138 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, ST_SENSORS_SCAN_X, IIO_MOD_X, IIO_LE,
139 ST_SENSORS_DEFAULT_16_REALBITS, ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
140 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, ST_SENSORS_SCAN_Y, IIO_MOD_Y, IIO_LE,
141 ST_SENSORS_DEFAULT_16_REALBITS, ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
142 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, ST_SENSORS_SCAN_Z, IIO_MOD_Z, IIO_LE,
143 ST_SENSORS_DEFAULT_16_REALBITS, ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
144 IIO_CHAN_SOFT_TIMESTAMP(3)
145};
146
147static const struct st_sensors st_accel_sensors[] = {
148 {
149 .wai = ST_ACCEL_1_WAI_EXP,
150 .sensors_supported = {
151 [0] = LIS3DH_ACCEL_DEV_NAME,
152 [1] = LSM303DLHC_ACCEL_DEV_NAME,
153 [2] = LSM330D_ACCEL_DEV_NAME,
154 [3] = LSM330DL_ACCEL_DEV_NAME,
155 [4] = LSM330DLC_ACCEL_DEV_NAME,
156 },
157 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
158 .odr = {
159 .addr = ST_ACCEL_1_ODR_ADDR,
160 .mask = ST_ACCEL_1_ODR_MASK,
161 .odr_avl = {
162 { 1, ST_ACCEL_1_ODR_AVL_1HZ_VAL, },
163 { 10, ST_ACCEL_1_ODR_AVL_10HZ_VAL, },
164 { 25, ST_ACCEL_1_ODR_AVL_25HZ_VAL, },
165 { 50, ST_ACCEL_1_ODR_AVL_50HZ_VAL, },
166 { 100, ST_ACCEL_1_ODR_AVL_100HZ_VAL, },
167 { 200, ST_ACCEL_1_ODR_AVL_200HZ_VAL, },
168 { 400, ST_ACCEL_1_ODR_AVL_400HZ_VAL, },
169 { 1600, ST_ACCEL_1_ODR_AVL_1600HZ_VAL, },
170 },
171 },
172 .pw = {
173 .addr = ST_ACCEL_1_ODR_ADDR,
174 .mask = ST_ACCEL_1_ODR_MASK,
175 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
176 },
177 .enable_axis = {
178 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
179 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
180 },
181 .fs = {
182 .addr = ST_ACCEL_1_FS_ADDR,
183 .mask = ST_ACCEL_1_FS_MASK,
184 .fs_avl = {
185 [0] = {
186 .num = ST_ACCEL_FS_AVL_2G,
187 .value = ST_ACCEL_1_FS_AVL_2_VAL,
188 .gain = ST_ACCEL_1_FS_AVL_2_GAIN,
189 },
190 [1] = {
191 .num = ST_ACCEL_FS_AVL_4G,
192 .value = ST_ACCEL_1_FS_AVL_4_VAL,
193 .gain = ST_ACCEL_1_FS_AVL_4_GAIN,
194 },
195 [2] = {
196 .num = ST_ACCEL_FS_AVL_8G,
197 .value = ST_ACCEL_1_FS_AVL_8_VAL,
198 .gain = ST_ACCEL_1_FS_AVL_8_GAIN,
199 },
200 [3] = {
201 .num = ST_ACCEL_FS_AVL_16G,
202 .value = ST_ACCEL_1_FS_AVL_16_VAL,
203 .gain = ST_ACCEL_1_FS_AVL_16_GAIN,
204 },
205 },
206 },
207 .bdu = {
208 .addr = ST_ACCEL_1_BDU_ADDR,
209 .mask = ST_ACCEL_1_BDU_MASK,
210 },
211 .drdy_irq = {
212 .addr = ST_ACCEL_1_DRDY_IRQ_ADDR,
213 .mask = ST_ACCEL_1_DRDY_IRQ_MASK,
214 },
215 .multi_read_bit = ST_ACCEL_1_MULTIREAD_BIT,
216 .bootime = 2,
217 },
218 {
219 .wai = ST_ACCEL_2_WAI_EXP,
220 .sensors_supported = {
221 [0] = LIS331DLH_ACCEL_DEV_NAME,
222 [1] = LSM303DL_ACCEL_DEV_NAME,
223 [2] = LSM303DLH_ACCEL_DEV_NAME,
224 [3] = LSM303DLM_ACCEL_DEV_NAME,
225 },
226 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
227 .odr = {
228 .addr = ST_ACCEL_2_ODR_ADDR,
229 .mask = ST_ACCEL_2_ODR_MASK,
230 .odr_avl = {
231 { 50, ST_ACCEL_2_ODR_AVL_50HZ_VAL, },
232 { 100, ST_ACCEL_2_ODR_AVL_100HZ_VAL, },
233 { 400, ST_ACCEL_2_ODR_AVL_400HZ_VAL, },
234 { 1000, ST_ACCEL_2_ODR_AVL_1000HZ_VAL, },
235 },
236 },
237 .pw = {
238 .addr = ST_ACCEL_2_PW_ADDR,
239 .mask = ST_ACCEL_2_PW_MASK,
240 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
241 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
242 },
243 .enable_axis = {
244 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
245 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
246 },
247 .fs = {
248 .addr = ST_ACCEL_2_FS_ADDR,
249 .mask = ST_ACCEL_2_FS_MASK,
250 .fs_avl = {
251 [0] = {
252 .num = ST_ACCEL_FS_AVL_2G,
253 .value = ST_ACCEL_2_FS_AVL_2_VAL,
254 .gain = ST_ACCEL_2_FS_AVL_2_GAIN,
255 },
256 [1] = {
257 .num = ST_ACCEL_FS_AVL_4G,
258 .value = ST_ACCEL_2_FS_AVL_4_VAL,
259 .gain = ST_ACCEL_2_FS_AVL_4_GAIN,
260 },
261 [2] = {
262 .num = ST_ACCEL_FS_AVL_8G,
263 .value = ST_ACCEL_2_FS_AVL_8_VAL,
264 .gain = ST_ACCEL_2_FS_AVL_8_GAIN,
265 },
266 },
267 },
268 .bdu = {
269 .addr = ST_ACCEL_2_BDU_ADDR,
270 .mask = ST_ACCEL_2_BDU_MASK,
271 },
272 .drdy_irq = {
273 .addr = ST_ACCEL_2_DRDY_IRQ_ADDR,
274 .mask = ST_ACCEL_2_DRDY_IRQ_MASK,
275 },
276 .multi_read_bit = ST_ACCEL_2_MULTIREAD_BIT,
277 .bootime = 2,
278 },
279 {
280 .wai = ST_ACCEL_3_WAI_EXP,
281 .sensors_supported = {
282 [0] = LSM330_ACCEL_DEV_NAME,
283 },
284 .ch = (struct iio_chan_spec *)st_accel_16bit_channels,
285 .odr = {
286 .addr = ST_ACCEL_3_ODR_ADDR,
287 .mask = ST_ACCEL_3_ODR_MASK,
288 .odr_avl = {
289 { 3, ST_ACCEL_3_ODR_AVL_3HZ_VAL },
290 { 6, ST_ACCEL_3_ODR_AVL_6HZ_VAL, },
291 { 12, ST_ACCEL_3_ODR_AVL_12HZ_VAL, },
292 { 25, ST_ACCEL_3_ODR_AVL_25HZ_VAL, },
293 { 50, ST_ACCEL_3_ODR_AVL_50HZ_VAL, },
294 { 100, ST_ACCEL_3_ODR_AVL_100HZ_VAL, },
295 { 200, ST_ACCEL_3_ODR_AVL_200HZ_VAL, },
296 { 400, ST_ACCEL_3_ODR_AVL_400HZ_VAL, },
297 { 800, ST_ACCEL_3_ODR_AVL_800HZ_VAL, },
298 { 1600, ST_ACCEL_3_ODR_AVL_1600HZ_VAL, },
299 },
300 },
301 .pw = {
302 .addr = ST_ACCEL_3_ODR_ADDR,
303 .mask = ST_ACCEL_3_ODR_MASK,
304 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
305 },
306 .enable_axis = {
307 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
308 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
309 },
310 .fs = {
311 .addr = ST_ACCEL_3_FS_ADDR,
312 .mask = ST_ACCEL_3_FS_MASK,
313 .fs_avl = {
314 [0] = {
315 .num = ST_ACCEL_FS_AVL_2G,
316 .value = ST_ACCEL_3_FS_AVL_2_VAL,
317 .gain = ST_ACCEL_3_FS_AVL_2_GAIN,
318 },
319 [1] = {
320 .num = ST_ACCEL_FS_AVL_4G,
321 .value = ST_ACCEL_3_FS_AVL_4_VAL,
322 .gain = ST_ACCEL_3_FS_AVL_4_GAIN,
323 },
324 [2] = {
325 .num = ST_ACCEL_FS_AVL_6G,
326 .value = ST_ACCEL_3_FS_AVL_6_VAL,
327 .gain = ST_ACCEL_3_FS_AVL_6_GAIN,
328 },
329 [3] = {
330 .num = ST_ACCEL_FS_AVL_8G,
331 .value = ST_ACCEL_3_FS_AVL_8_VAL,
332 .gain = ST_ACCEL_3_FS_AVL_8_GAIN,
333 },
334 [4] = {
335 .num = ST_ACCEL_FS_AVL_16G,
336 .value = ST_ACCEL_3_FS_AVL_16_VAL,
337 .gain = ST_ACCEL_3_FS_AVL_16_GAIN,
338 },
339 },
340 },
341 .bdu = {
342 .addr = ST_ACCEL_3_BDU_ADDR,
343 .mask = ST_ACCEL_3_BDU_MASK,
344 },
345 .drdy_irq = {
346 .addr = ST_ACCEL_3_DRDY_IRQ_ADDR,
347 .mask = ST_ACCEL_3_DRDY_IRQ_MASK,
348 .ig1 = {
349 .en_addr = ST_ACCEL_3_IG1_EN_ADDR,
350 .en_mask = ST_ACCEL_3_IG1_EN_MASK,
351 },
352 },
353 .multi_read_bit = ST_ACCEL_3_MULTIREAD_BIT,
354 .bootime = 2,
355 },
356};
357
358static int st_accel_read_raw(struct iio_dev *indio_dev,
359 struct iio_chan_spec const *ch, int *val,
360 int *val2, long mask)
361{
362 int err;
363 struct st_sensor_data *adata = iio_priv(indio_dev);
364
365 switch (mask) {
366 case IIO_CHAN_INFO_RAW:
367 err = st_sensors_read_info_raw(indio_dev, ch, val);
368 if (err < 0)
369 goto read_error;
370
371 return IIO_VAL_INT;
372 case IIO_CHAN_INFO_SCALE:
373 *val = 0;
374 *val2 = adata->current_fullscale->gain;
375 return IIO_VAL_INT_PLUS_MICRO;
376 default:
377 return -EINVAL;
378 }
379
380read_error:
381 return err;
382}
383
384static int st_accel_write_raw(struct iio_dev *indio_dev,
385 struct iio_chan_spec const *chan, int val, int val2, long mask)
386{
387 int err;
388
389 switch (mask) {
390 case IIO_CHAN_INFO_SCALE:
391 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
392 break;
393 default:
394 return -EINVAL;
395 }
396
397 return err;
398}
399
400static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
401static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
402static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
403
404static struct attribute *st_accel_attributes[] = {
405 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
406 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
407 &iio_dev_attr_sampling_frequency.dev_attr.attr,
408 NULL,
409};
410
411static const struct attribute_group st_accel_attribute_group = {
412 .attrs = st_accel_attributes,
413};
414
415static const struct iio_info accel_info = {
416 .driver_module = THIS_MODULE,
417 .attrs = &st_accel_attribute_group,
418 .read_raw = &st_accel_read_raw,
419 .write_raw = &st_accel_write_raw,
420};
421
422static const struct iio_trigger_ops st_accel_trigger_ops = {
423 .owner = THIS_MODULE,
424 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
425};
426
427int st_accel_common_probe(struct iio_dev *indio_dev)
428{
429 int err;
430 struct st_sensor_data *adata = iio_priv(indio_dev);
431
432 indio_dev->modes = INDIO_DIRECT_MODE;
433 indio_dev->info = &accel_info;
434
435 err = st_sensors_check_device_support(indio_dev,
436 ARRAY_SIZE(st_accel_sensors), st_accel_sensors);
437 if (err < 0)
438 goto st_accel_common_probe_error;
439
440 adata->multiread_bit = adata->sensor->multi_read_bit;
441 indio_dev->channels = adata->sensor->ch;
442 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
443
444 adata->current_fullscale = (struct st_sensor_fullscale_avl *)
445 &adata->sensor->fs.fs_avl[0];
446 adata->odr = adata->sensor->odr.odr_avl[0].hz;
447
448 err = st_sensors_init_sensor(indio_dev);
449 if (err < 0)
450 goto st_accel_common_probe_error;
451
452 if (adata->get_irq_data_ready(indio_dev) > 0) {
453 err = st_accel_allocate_ring(indio_dev);
454 if (err < 0)
455 goto st_accel_common_probe_error;
456
457 err = st_sensors_allocate_trigger(indio_dev,
458 &st_accel_trigger_ops);
459 if (err < 0)
460 goto st_accel_probe_trigger_error;
461 }
462
463 err = iio_device_register(indio_dev);
464 if (err)
465 goto st_accel_device_register_error;
466
467 return err;
468
469st_accel_device_register_error:
470 if (adata->get_irq_data_ready(indio_dev) > 0)
471 st_sensors_deallocate_trigger(indio_dev);
472st_accel_probe_trigger_error:
473 if (adata->get_irq_data_ready(indio_dev) > 0)
474 st_accel_deallocate_ring(indio_dev);
475st_accel_common_probe_error:
476 return err;
477}
478EXPORT_SYMBOL(st_accel_common_probe);
479
480void st_accel_common_remove(struct iio_dev *indio_dev)
481{
482 struct st_sensor_data *adata = iio_priv(indio_dev);
483
484 iio_device_unregister(indio_dev);
485 if (adata->get_irq_data_ready(indio_dev) > 0) {
486 st_sensors_deallocate_trigger(indio_dev);
487 st_accel_deallocate_ring(indio_dev);
488 }
489 iio_device_free(indio_dev);
490}
491EXPORT_SYMBOL(st_accel_common_remove);
492
493MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
494MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
495MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/accel/st_accel_i2c.c b/drivers/iio/accel/st_accel_i2c.c
new file mode 100644
index 000000000000..90b8ddfb61ed
--- /dev/null
+++ b/drivers/iio/accel/st_accel_i2c.c
@@ -0,0 +1,87 @@
1/*
2 * STMicroelectronics accelerometers driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/i2c.h>
15#include <linux/iio/iio.h>
16#include <linux/iio/trigger.h>
17
18#include <linux/iio/common/st_sensors.h>
19#include <linux/iio/common/st_sensors_i2c.h>
20#include "st_accel.h"
21
22static int st_accel_i2c_probe(struct i2c_client *client,
23 const struct i2c_device_id *id)
24{
25 struct iio_dev *indio_dev;
26 struct st_sensor_data *adata;
27 int err;
28
29 indio_dev = iio_device_alloc(sizeof(*adata));
30 if (indio_dev == NULL) {
31 err = -ENOMEM;
32 goto iio_device_alloc_error;
33 }
34
35 adata = iio_priv(indio_dev);
36 adata->dev = &client->dev;
37
38 st_sensors_i2c_configure(indio_dev, client, adata);
39
40 err = st_accel_common_probe(indio_dev);
41 if (err < 0)
42 goto st_accel_common_probe_error;
43
44 return 0;
45
46st_accel_common_probe_error:
47 iio_device_free(indio_dev);
48iio_device_alloc_error:
49 return err;
50}
51
52static int st_accel_i2c_remove(struct i2c_client *client)
53{
54 st_accel_common_remove(i2c_get_clientdata(client));
55
56 return 0;
57}
58
59static const struct i2c_device_id st_accel_id_table[] = {
60 { LSM303DLH_ACCEL_DEV_NAME },
61 { LSM303DLHC_ACCEL_DEV_NAME },
62 { LIS3DH_ACCEL_DEV_NAME },
63 { LSM330D_ACCEL_DEV_NAME },
64 { LSM330DL_ACCEL_DEV_NAME },
65 { LSM330DLC_ACCEL_DEV_NAME },
66 { LIS331DLH_ACCEL_DEV_NAME },
67 { LSM303DL_ACCEL_DEV_NAME },
68 { LSM303DLM_ACCEL_DEV_NAME },
69 { LSM330_ACCEL_DEV_NAME },
70 {},
71};
72MODULE_DEVICE_TABLE(i2c, st_accel_id_table);
73
74static struct i2c_driver st_accel_driver = {
75 .driver = {
76 .owner = THIS_MODULE,
77 .name = "st-accel-i2c",
78 },
79 .probe = st_accel_i2c_probe,
80 .remove = st_accel_i2c_remove,
81 .id_table = st_accel_id_table,
82};
83module_i2c_driver(st_accel_driver);
84
85MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
86MODULE_DESCRIPTION("STMicroelectronics accelerometers i2c driver");
87MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/accel/st_accel_spi.c b/drivers/iio/accel/st_accel_spi.c
new file mode 100644
index 000000000000..dbd45c08711f
--- /dev/null
+++ b/drivers/iio/accel/st_accel_spi.c
@@ -0,0 +1,86 @@
1/*
2 * STMicroelectronics accelerometers driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/spi/spi.h>
15#include <linux/iio/iio.h>
16#include <linux/iio/trigger.h>
17
18#include <linux/iio/common/st_sensors.h>
19#include <linux/iio/common/st_sensors_spi.h>
20#include "st_accel.h"
21
22static int st_accel_spi_probe(struct spi_device *spi)
23{
24 struct iio_dev *indio_dev;
25 struct st_sensor_data *adata;
26 int err;
27
28 indio_dev = iio_device_alloc(sizeof(*adata));
29 if (indio_dev == NULL) {
30 err = -ENOMEM;
31 goto iio_device_alloc_error;
32 }
33
34 adata = iio_priv(indio_dev);
35 adata->dev = &spi->dev;
36
37 st_sensors_spi_configure(indio_dev, spi, adata);
38
39 err = st_accel_common_probe(indio_dev);
40 if (err < 0)
41 goto st_accel_common_probe_error;
42
43 return 0;
44
45st_accel_common_probe_error:
46 iio_device_free(indio_dev);
47iio_device_alloc_error:
48 return err;
49}
50
51static int st_accel_spi_remove(struct spi_device *spi)
52{
53 st_accel_common_remove(spi_get_drvdata(spi));
54
55 return 0;
56}
57
58static const struct spi_device_id st_accel_id_table[] = {
59 { LSM303DLH_ACCEL_DEV_NAME },
60 { LSM303DLHC_ACCEL_DEV_NAME },
61 { LIS3DH_ACCEL_DEV_NAME },
62 { LSM330D_ACCEL_DEV_NAME },
63 { LSM330DL_ACCEL_DEV_NAME },
64 { LSM330DLC_ACCEL_DEV_NAME },
65 { LIS331DLH_ACCEL_DEV_NAME },
66 { LSM303DL_ACCEL_DEV_NAME },
67 { LSM303DLM_ACCEL_DEV_NAME },
68 { LSM330_ACCEL_DEV_NAME },
69 {},
70};
71MODULE_DEVICE_TABLE(spi, st_accel_id_table);
72
73static struct spi_driver st_accel_driver = {
74 .driver = {
75 .owner = THIS_MODULE,
76 .name = "st-accel-spi",
77 },
78 .probe = st_accel_spi_probe,
79 .remove = st_accel_spi_remove,
80 .id_table = st_accel_id_table,
81};
82module_spi_driver(st_accel_driver);
83
84MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
85MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
86MODULE_LICENSE("GPL v2");