aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-02-21 15:11:44 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-02-21 15:11:44 -0500
commitb5c78e04dd061b776978dad61dd85357081147b0 (patch)
tree2416b2dc61c452c3aeb2a32bcedf15e6257be638 /drivers/iio
parent06991c28f37ad68e5c03777f5c3b679b56e3dac1 (diff)
parent951348b377385475aa256c27e1c9e2564c9ec160 (diff)
Merge tag 'staging-3.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
Pull staging tree update from Greg Kroah-Hartman: "Here's the big staging tree merge for 3.9-rc1 Lots of cleanups and updates for drivers all through the staging tree. We are pretty much "code neutral" here, adding just about as many lines as we removed. All of these have been in linux-next for a while." * tag 'staging-3.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (804 commits) staging: comedi: vmk80xx: wait for URBs to complete staging: comedi: drivers: addi-data: hwdrv_apci3200.c: Add a missing semicolon staging: et131x: Update TODO list staging: et131x: Remove assignment of skb->dev staging: wlan-ng: hfa384x.h: fix for error reported by smatch staging/zache checkpatch ERROR: spaces prohibited around that staging/ozwpan: Mark read only parameters and structs as const staging/ozwpan: Remove empty and unused function oz_cdev_heartbeat staging/ozwpan: Mark local functions as static (fix sparse warnings) staging/ozwpan: Add missing header includes staging/usbip: Mark local functions as static (fix sparse warnings) staging/xgifb: Remove duplicated code in loops. staging/xgifb: Consolidate return paths staging/xgifb: Remove code without effect staging/xgifb: Remove unnecessary casts staging/xgifb: Consolidate if/else if with identical code branches staging: vt6656: replaced custom TRUE definition with true staging: vt6656: replaced custom FALSE definition with false staging: vt6656: replace custom BOOL definition with bool staging/rtl8187se: Mark functions as static to silence sparse ...
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/accel/Kconfig38
-rw-r--r--drivers/iio/accel/Makefile9
-rw-r--r--drivers/iio/accel/hid-sensor-accel-3d.c3
-rw-r--r--drivers/iio/accel/kxsd9.c287
-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.c500
-rw-r--r--drivers/iio/accel/st_accel_i2c.c86
-rw-r--r--drivers/iio/accel/st_accel_spi.c85
-rw-r--r--drivers/iio/adc/Kconfig4
-rw-r--r--drivers/iio/adc/lp8788_adc.c18
-rw-r--r--drivers/iio/adc/max1363.c167
-rw-r--r--drivers/iio/buffer_cb.c4
-rw-r--r--drivers/iio/common/Kconfig1
-rw-r--r--drivers/iio/common/Makefile1
-rw-r--r--drivers/iio/common/hid-sensors/hid-sensor-attributes.c11
-rw-r--r--drivers/iio/common/hid-sensors/hid-sensor-attributes.h57
-rw-r--r--drivers/iio/common/hid-sensors/hid-sensor-trigger.c5
-rw-r--r--drivers/iio/common/hid-sensors/hid-sensor-trigger.h2
-rw-r--r--drivers/iio/common/st_sensors/Kconfig14
-rw-r--r--drivers/iio/common/st_sensors/Makefile10
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_buffer.c116
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c446
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_i2c.c81
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_spi.c128
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_trigger.c77
-rw-r--r--drivers/iio/dac/ad5360.c7
-rw-r--r--drivers/iio/dac/ad5421.c7
-rw-r--r--drivers/iio/dac/ad5504.c6
-rw-r--r--drivers/iio/dac/ad5686.c7
-rw-r--r--drivers/iio/dac/ad5755.c7
-rw-r--r--drivers/iio/dac/ad5764.c7
-rw-r--r--drivers/iio/dac/ad5791.c6
-rw-r--r--drivers/iio/frequency/ad9523.c14
-rw-r--r--drivers/iio/gyro/Kconfig55
-rw-r--r--drivers/iio/gyro/Makefile14
-rw-r--r--drivers/iio/gyro/adis16080.c259
-rw-r--r--drivers/iio/gyro/adxrs450.c494
-rw-r--r--drivers/iio/gyro/hid-sensor-gyro-3d.c3
-rw-r--r--drivers/iio/gyro/itg3200_buffer.c156
-rw-r--r--drivers/iio/gyro/itg3200_core.c401
-rw-r--r--drivers/iio/gyro/st_gyro.h45
-rw-r--r--drivers/iio/gyro/st_gyro_buffer.c114
-rw-r--r--drivers/iio/gyro/st_gyro_core.c368
-rw-r--r--drivers/iio/gyro/st_gyro_i2c.c84
-rw-r--r--drivers/iio/gyro/st_gyro_spi.c83
-rw-r--r--drivers/iio/imu/Kconfig13
-rw-r--r--drivers/iio/imu/Makefile5
-rw-r--r--drivers/iio/imu/adis16400.h212
-rw-r--r--drivers/iio/imu/adis16400_buffer.c96
-rw-r--r--drivers/iio/imu/adis16400_core.c965
-rw-r--r--drivers/iio/imu/inv_mpu6050/Kconfig13
-rw-r--r--drivers/iio/imu/inv_mpu6050/Makefile6
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_core.c795
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h246
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c196
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c155
-rw-r--r--drivers/iio/industrialio-trigger.c12
-rw-r--r--drivers/iio/inkern.c53
-rw-r--r--drivers/iio/kfifo_buf.c1
-rw-r--r--drivers/iio/light/Kconfig10
-rw-r--r--drivers/iio/light/Makefile1
-rw-r--r--drivers/iio/light/hid-sensor-als.c3
-rw-r--r--drivers/iio/light/tsl2563.c887
-rw-r--r--drivers/iio/magnetometer/Kconfig30
-rw-r--r--drivers/iio/magnetometer/Makefile7
-rw-r--r--drivers/iio/magnetometer/hid-sensor-magn-3d.c3
-rw-r--r--drivers/iio/magnetometer/st_magn.h45
-rw-r--r--drivers/iio/magnetometer/st_magn_buffer.c98
-rw-r--r--drivers/iio/magnetometer/st_magn_core.c400
-rw-r--r--drivers/iio/magnetometer/st_magn_i2c.c80
-rw-r--r--drivers/iio/magnetometer/st_magn_spi.c79
72 files changed, 8580 insertions, 279 deletions
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index 05e996fafc9d..bb594963f91e 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -14,4 +14,42 @@ config HID_SENSOR_ACCEL_3D
14 Say yes here to build support for the HID SENSOR 14 Say yes here to build support for the HID SENSOR
15 accelerometers 3D. 15 accelerometers 3D.
16 16
17config KXSD9
18 tristate "Kionix KXSD9 Accelerometer Driver"
19 depends on SPI
20 help
21 Say yes here to build support for the Kionix KXSD9 accelerometer.
22 Currently this only supports the device via an SPI interface.
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
17endmenu 55endmenu
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index 5bc6855a973e..87d8fa264894 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -3,3 +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
14obj-$(CONFIG_KXSD9) += kxsd9.o
diff --git a/drivers/iio/accel/hid-sensor-accel-3d.c b/drivers/iio/accel/hid-sensor-accel-3d.c
index 0b0c3c66f6c0..dd8ea4284934 100644
--- a/drivers/iio/accel/hid-sensor-accel-3d.c
+++ b/drivers/iio/accel/hid-sensor-accel-3d.c
@@ -28,7 +28,6 @@
28#include <linux/iio/buffer.h> 28#include <linux/iio/buffer.h>
29#include <linux/iio/trigger_consumer.h> 29#include <linux/iio/trigger_consumer.h>
30#include <linux/iio/triggered_buffer.h> 30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-attributes.h"
32#include "../common/hid-sensors/hid-sensor-trigger.h" 31#include "../common/hid-sensors/hid-sensor-trigger.h"
33 32
34/*Format: HID-SENSOR-usage_id_in_hex*/ 33/*Format: HID-SENSOR-usage_id_in_hex*/
@@ -44,7 +43,7 @@ enum accel_3d_channel {
44 43
45struct accel_3d_state { 44struct accel_3d_state {
46 struct hid_sensor_hub_callbacks callbacks; 45 struct hid_sensor_hub_callbacks callbacks;
47 struct hid_sensor_iio_common common_attributes; 46 struct hid_sensor_common common_attributes;
48 struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX]; 47 struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
49 u32 accel_val[ACCEL_3D_CHANNEL_MAX]; 48 u32 accel_val[ACCEL_3D_CHANNEL_MAX];
50}; 49};
diff --git a/drivers/iio/accel/kxsd9.c b/drivers/iio/accel/kxsd9.c
new file mode 100644
index 000000000000..c2229a521ab9
--- /dev/null
+++ b/drivers/iio/accel/kxsd9.c
@@ -0,0 +1,287 @@
1/*
2 * kxsd9.c simple support for the Kionix KXSD9 3D
3 * accelerometer.
4 *
5 * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * The i2c interface is very similar, so shouldn't be a problem once
12 * I have a suitable wire made up.
13 *
14 * TODO: Support the motion detector
15 * Uses register address incrementing so could have a
16 * heavily optimized ring buffer access function.
17 */
18
19#include <linux/device.h>
20#include <linux/kernel.h>
21#include <linux/spi/spi.h>
22#include <linux/sysfs.h>
23#include <linux/slab.h>
24#include <linux/module.h>
25
26#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h>
28
29#define KXSD9_REG_X 0x00
30#define KXSD9_REG_Y 0x02
31#define KXSD9_REG_Z 0x04
32#define KXSD9_REG_AUX 0x06
33#define KXSD9_REG_RESET 0x0a
34#define KXSD9_REG_CTRL_C 0x0c
35
36#define KXSD9_FS_MASK 0x03
37
38#define KXSD9_REG_CTRL_B 0x0d
39#define KXSD9_REG_CTRL_A 0x0e
40
41#define KXSD9_READ(a) (0x80 | (a))
42#define KXSD9_WRITE(a) (a)
43
44#define KXSD9_STATE_RX_SIZE 2
45#define KXSD9_STATE_TX_SIZE 2
46/**
47 * struct kxsd9_state - device related storage
48 * @buf_lock: protect the rx and tx buffers.
49 * @us: spi device
50 * @rx: single rx buffer storage
51 * @tx: single tx buffer storage
52 **/
53struct kxsd9_state {
54 struct mutex buf_lock;
55 struct spi_device *us;
56 u8 rx[KXSD9_STATE_RX_SIZE] ____cacheline_aligned;
57 u8 tx[KXSD9_STATE_TX_SIZE];
58};
59
60#define KXSD9_SCALE_2G "0.011978"
61#define KXSD9_SCALE_4G "0.023927"
62#define KXSD9_SCALE_6G "0.035934"
63#define KXSD9_SCALE_8G "0.047853"
64
65/* reverse order */
66static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 };
67
68static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro)
69{
70 int ret, i;
71 struct kxsd9_state *st = iio_priv(indio_dev);
72 bool foundit = false;
73
74 for (i = 0; i < 4; i++)
75 if (micro == kxsd9_micro_scales[i]) {
76 foundit = true;
77 break;
78 }
79 if (!foundit)
80 return -EINVAL;
81
82 mutex_lock(&st->buf_lock);
83 ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
84 if (ret)
85 goto error_ret;
86 st->tx[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C);
87 st->tx[1] = (ret & ~KXSD9_FS_MASK) | i;
88
89 ret = spi_write(st->us, st->tx, 2);
90error_ret:
91 mutex_unlock(&st->buf_lock);
92 return ret;
93}
94
95static int kxsd9_read(struct iio_dev *indio_dev, u8 address)
96{
97 int ret;
98 struct kxsd9_state *st = iio_priv(indio_dev);
99 struct spi_transfer xfers[] = {
100 {
101 .bits_per_word = 8,
102 .len = 1,
103 .delay_usecs = 200,
104 .tx_buf = st->tx,
105 }, {
106 .bits_per_word = 8,
107 .len = 2,
108 .rx_buf = st->rx,
109 },
110 };
111
112 mutex_lock(&st->buf_lock);
113 st->tx[0] = KXSD9_READ(address);
114 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
115 if (ret)
116 return ret;
117 return (((u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0);
118}
119
120static IIO_CONST_ATTR(accel_scale_available,
121 KXSD9_SCALE_2G " "
122 KXSD9_SCALE_4G " "
123 KXSD9_SCALE_6G " "
124 KXSD9_SCALE_8G);
125
126static struct attribute *kxsd9_attributes[] = {
127 &iio_const_attr_accel_scale_available.dev_attr.attr,
128 NULL,
129};
130
131static int kxsd9_write_raw(struct iio_dev *indio_dev,
132 struct iio_chan_spec const *chan,
133 int val,
134 int val2,
135 long mask)
136{
137 int ret = -EINVAL;
138
139 if (mask == IIO_CHAN_INFO_SCALE) {
140 /* Check no integer component */
141 if (val)
142 return -EINVAL;
143 ret = kxsd9_write_scale(indio_dev, val2);
144 }
145
146 return ret;
147}
148
149static int kxsd9_read_raw(struct iio_dev *indio_dev,
150 struct iio_chan_spec const *chan,
151 int *val, int *val2, long mask)
152{
153 int ret = -EINVAL;
154 struct kxsd9_state *st = iio_priv(indio_dev);
155
156 switch (mask) {
157 case IIO_CHAN_INFO_RAW:
158 ret = kxsd9_read(indio_dev, chan->address);
159 if (ret < 0)
160 goto error_ret;
161 *val = ret;
162 break;
163 case IIO_CHAN_INFO_SCALE:
164 ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
165 if (ret)
166 goto error_ret;
167 *val2 = kxsd9_micro_scales[ret & KXSD9_FS_MASK];
168 ret = IIO_VAL_INT_PLUS_MICRO;
169 break;
170 }
171
172error_ret:
173 return ret;
174};
175#define KXSD9_ACCEL_CHAN(axis) \
176 { \
177 .type = IIO_ACCEL, \
178 .modified = 1, \
179 .channel2 = IIO_MOD_##axis, \
180 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
181 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
182 .address = KXSD9_REG_##axis, \
183 }
184
185static const struct iio_chan_spec kxsd9_channels[] = {
186 KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z),
187 {
188 .type = IIO_VOLTAGE,
189 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
190 .indexed = 1,
191 .address = KXSD9_REG_AUX,
192 }
193};
194
195static const struct attribute_group kxsd9_attribute_group = {
196 .attrs = kxsd9_attributes,
197};
198
199static int kxsd9_power_up(struct kxsd9_state *st)
200{
201 int ret;
202
203 st->tx[0] = 0x0d;
204 st->tx[1] = 0x40;
205 ret = spi_write(st->us, st->tx, 2);
206 if (ret)
207 return ret;
208
209 st->tx[0] = 0x0c;
210 st->tx[1] = 0x9b;
211 return spi_write(st->us, st->tx, 2);
212};
213
214static const struct iio_info kxsd9_info = {
215 .read_raw = &kxsd9_read_raw,
216 .write_raw = &kxsd9_write_raw,
217 .attrs = &kxsd9_attribute_group,
218 .driver_module = THIS_MODULE,
219};
220
221static int kxsd9_probe(struct spi_device *spi)
222{
223 struct iio_dev *indio_dev;
224 struct kxsd9_state *st;
225 int ret;
226
227 indio_dev = iio_device_alloc(sizeof(*st));
228 if (indio_dev == NULL) {
229 ret = -ENOMEM;
230 goto error_ret;
231 }
232 st = iio_priv(indio_dev);
233 spi_set_drvdata(spi, indio_dev);
234
235 st->us = spi;
236 mutex_init(&st->buf_lock);
237 indio_dev->channels = kxsd9_channels;
238 indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels);
239 indio_dev->name = spi_get_device_id(spi)->name;
240 indio_dev->dev.parent = &spi->dev;
241 indio_dev->info = &kxsd9_info;
242 indio_dev->modes = INDIO_DIRECT_MODE;
243
244 spi->mode = SPI_MODE_0;
245 spi_setup(spi);
246 kxsd9_power_up(st);
247
248 ret = iio_device_register(indio_dev);
249 if (ret)
250 goto error_free_dev;
251
252 return 0;
253
254error_free_dev:
255 iio_device_free(indio_dev);
256error_ret:
257 return ret;
258}
259
260static int kxsd9_remove(struct spi_device *spi)
261{
262 iio_device_unregister(spi_get_drvdata(spi));
263 iio_device_free(spi_get_drvdata(spi));
264
265 return 0;
266}
267
268static const struct spi_device_id kxsd9_id[] = {
269 {"kxsd9", 0},
270 { },
271};
272MODULE_DEVICE_TABLE(spi, kxsd9_id);
273
274static struct spi_driver kxsd9_driver = {
275 .driver = {
276 .name = "kxsd9",
277 .owner = THIS_MODULE,
278 },
279 .probe = kxsd9_probe,
280 .remove = kxsd9_remove,
281 .id_table = kxsd9_id,
282};
283module_spi_driver(kxsd9_driver);
284
285MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
286MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
287MODULE_LICENSE("GPL v2");
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..e0f5a3ceba5e
--- /dev/null
+++ b/drivers/iio/accel/st_accel_core.c
@@ -0,0 +1,500 @@
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.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
422#ifdef CONFIG_IIO_TRIGGER
423static const struct iio_trigger_ops st_accel_trigger_ops = {
424 .owner = THIS_MODULE,
425 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
426};
427#define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
428#else
429#define ST_ACCEL_TRIGGER_OPS NULL
430#endif
431
432int st_accel_common_probe(struct iio_dev *indio_dev)
433{
434 int err;
435 struct st_sensor_data *adata = iio_priv(indio_dev);
436
437 indio_dev->modes = INDIO_DIRECT_MODE;
438 indio_dev->info = &accel_info;
439
440 err = st_sensors_check_device_support(indio_dev,
441 ARRAY_SIZE(st_accel_sensors), st_accel_sensors);
442 if (err < 0)
443 goto st_accel_common_probe_error;
444
445 adata->multiread_bit = adata->sensor->multi_read_bit;
446 indio_dev->channels = adata->sensor->ch;
447 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
448
449 adata->current_fullscale = (struct st_sensor_fullscale_avl *)
450 &adata->sensor->fs.fs_avl[0];
451 adata->odr = adata->sensor->odr.odr_avl[0].hz;
452
453 err = st_sensors_init_sensor(indio_dev);
454 if (err < 0)
455 goto st_accel_common_probe_error;
456
457 if (adata->get_irq_data_ready(indio_dev) > 0) {
458 err = st_accel_allocate_ring(indio_dev);
459 if (err < 0)
460 goto st_accel_common_probe_error;
461
462 err = st_sensors_allocate_trigger(indio_dev,
463 ST_ACCEL_TRIGGER_OPS);
464 if (err < 0)
465 goto st_accel_probe_trigger_error;
466 }
467
468 err = iio_device_register(indio_dev);
469 if (err)
470 goto st_accel_device_register_error;
471
472 return err;
473
474st_accel_device_register_error:
475 if (adata->get_irq_data_ready(indio_dev) > 0)
476 st_sensors_deallocate_trigger(indio_dev);
477st_accel_probe_trigger_error:
478 if (adata->get_irq_data_ready(indio_dev) > 0)
479 st_accel_deallocate_ring(indio_dev);
480st_accel_common_probe_error:
481 return err;
482}
483EXPORT_SYMBOL(st_accel_common_probe);
484
485void st_accel_common_remove(struct iio_dev *indio_dev)
486{
487 struct st_sensor_data *adata = iio_priv(indio_dev);
488
489 iio_device_unregister(indio_dev);
490 if (adata->get_irq_data_ready(indio_dev) > 0) {
491 st_sensors_deallocate_trigger(indio_dev);
492 st_accel_deallocate_ring(indio_dev);
493 }
494 iio_device_free(indio_dev);
495}
496EXPORT_SYMBOL(st_accel_common_remove);
497
498MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
499MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
500MODULE_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..ffc9d097e484
--- /dev/null
+++ b/drivers/iio/accel/st_accel_i2c.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/i2c.h>
15#include <linux/iio/iio.h>
16
17#include <linux/iio/common/st_sensors.h>
18#include <linux/iio/common/st_sensors_i2c.h>
19#include "st_accel.h"
20
21static int st_accel_i2c_probe(struct i2c_client *client,
22 const struct i2c_device_id *id)
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 = &client->dev;
36
37 st_sensors_i2c_configure(indio_dev, client, 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_i2c_remove(struct i2c_client *client)
52{
53 st_accel_common_remove(i2c_get_clientdata(client));
54
55 return 0;
56}
57
58static const struct i2c_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(i2c, st_accel_id_table);
72
73static struct i2c_driver st_accel_driver = {
74 .driver = {
75 .owner = THIS_MODULE,
76 .name = "st-accel-i2c",
77 },
78 .probe = st_accel_i2c_probe,
79 .remove = st_accel_i2c_remove,
80 .id_table = st_accel_id_table,
81};
82module_i2c_driver(st_accel_driver);
83
84MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
85MODULE_DESCRIPTION("STMicroelectronics accelerometers i2c driver");
86MODULE_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..22b35bfea7d2
--- /dev/null
+++ b/drivers/iio/accel/st_accel_spi.c
@@ -0,0 +1,85 @@
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
17#include <linux/iio/common/st_sensors.h>
18#include <linux/iio/common/st_sensors_spi.h>
19#include "st_accel.h"
20
21static int st_accel_spi_probe(struct spi_device *spi)
22{
23 struct iio_dev *indio_dev;
24 struct st_sensor_data *adata;
25 int err;
26
27 indio_dev = iio_device_alloc(sizeof(*adata));
28 if (indio_dev == NULL) {
29 err = -ENOMEM;
30 goto iio_device_alloc_error;
31 }
32
33 adata = iio_priv(indio_dev);
34 adata->dev = &spi->dev;
35
36 st_sensors_spi_configure(indio_dev, spi, adata);
37
38 err = st_accel_common_probe(indio_dev);
39 if (err < 0)
40 goto st_accel_common_probe_error;
41
42 return 0;
43
44st_accel_common_probe_error:
45 iio_device_free(indio_dev);
46iio_device_alloc_error:
47 return err;
48}
49
50static int st_accel_spi_remove(struct spi_device *spi)
51{
52 st_accel_common_remove(spi_get_drvdata(spi));
53
54 return 0;
55}
56
57static const struct spi_device_id st_accel_id_table[] = {
58 { LSM303DLH_ACCEL_DEV_NAME },
59 { LSM303DLHC_ACCEL_DEV_NAME },
60 { LIS3DH_ACCEL_DEV_NAME },
61 { LSM330D_ACCEL_DEV_NAME },
62 { LSM330DL_ACCEL_DEV_NAME },
63 { LSM330DLC_ACCEL_DEV_NAME },
64 { LIS331DLH_ACCEL_DEV_NAME },
65 { LSM303DL_ACCEL_DEV_NAME },
66 { LSM303DLM_ACCEL_DEV_NAME },
67 { LSM330_ACCEL_DEV_NAME },
68 {},
69};
70MODULE_DEVICE_TABLE(spi, st_accel_id_table);
71
72static struct spi_driver st_accel_driver = {
73 .driver = {
74 .owner = THIS_MODULE,
75 .name = "st-accel-spi",
76 },
77 .probe = st_accel_spi_probe,
78 .remove = st_accel_spi_remove,
79 .id_table = st_accel_id_table,
80};
81module_spi_driver(st_accel_driver);
82
83MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
84MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
85MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index fe822a14d130..e372257a8494 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -100,10 +100,8 @@ config LP8788_ADC
100config MAX1363 100config MAX1363
101 tristate "Maxim max1363 ADC driver" 101 tristate "Maxim max1363 ADC driver"
102 depends on I2C 102 depends on I2C
103 select IIO_TRIGGER
104 select MAX1363_RING_BUFFER
105 select IIO_BUFFER 103 select IIO_BUFFER
106 select IIO_KFIFO_BUF 104 select IIO_TRIGGERED_BUFFER
107 help 105 help
108 Say yes here to build support for many Maxim i2c analog to digital 106 Say yes here to build support for many Maxim i2c analog to digital
109 converters (ADC). (max1361, max1362, max1363, max1364, max1036, 107 converters (ADC). (max1361, max1362, max1363, max1364, max1036,
diff --git a/drivers/iio/adc/lp8788_adc.c b/drivers/iio/adc/lp8788_adc.c
index 72955e45e9e0..763f57565ee4 100644
--- a/drivers/iio/adc/lp8788_adc.c
+++ b/drivers/iio/adc/lp8788_adc.c
@@ -179,7 +179,7 @@ static int lp8788_iio_map_register(struct iio_dev *indio_dev,
179 179
180 ret = iio_map_array_register(indio_dev, map); 180 ret = iio_map_array_register(indio_dev, map);
181 if (ret) { 181 if (ret) {
182 dev_err(adc->lp->dev, "iio map err: %d\n", ret); 182 dev_err(&indio_dev->dev, "iio map err: %d\n", ret);
183 return ret; 183 return ret;
184 } 184 }
185 185
@@ -187,12 +187,6 @@ static int lp8788_iio_map_register(struct iio_dev *indio_dev,
187 return 0; 187 return 0;
188} 188}
189 189
190static inline void lp8788_iio_map_unregister(struct iio_dev *indio_dev,
191 struct lp8788_adc *adc)
192{
193 iio_map_array_unregister(indio_dev, adc->map);
194}
195
196static int lp8788_adc_probe(struct platform_device *pdev) 190static int lp8788_adc_probe(struct platform_device *pdev)
197{ 191{
198 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent); 192 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
@@ -208,13 +202,14 @@ static int lp8788_adc_probe(struct platform_device *pdev)
208 adc->lp = lp; 202 adc->lp = lp;
209 platform_set_drvdata(pdev, indio_dev); 203 platform_set_drvdata(pdev, indio_dev);
210 204
205 indio_dev->dev.of_node = pdev->dev.of_node;
211 ret = lp8788_iio_map_register(indio_dev, lp->pdata, adc); 206 ret = lp8788_iio_map_register(indio_dev, lp->pdata, adc);
212 if (ret) 207 if (ret)
213 goto err_iio_map; 208 goto err_iio_map;
214 209
215 mutex_init(&adc->lock); 210 mutex_init(&adc->lock);
216 211
217 indio_dev->dev.parent = lp->dev; 212 indio_dev->dev.parent = &pdev->dev;
218 indio_dev->name = pdev->name; 213 indio_dev->name = pdev->name;
219 indio_dev->modes = INDIO_DIRECT_MODE; 214 indio_dev->modes = INDIO_DIRECT_MODE;
220 indio_dev->info = &lp8788_adc_info; 215 indio_dev->info = &lp8788_adc_info;
@@ -223,14 +218,14 @@ static int lp8788_adc_probe(struct platform_device *pdev)
223 218
224 ret = iio_device_register(indio_dev); 219 ret = iio_device_register(indio_dev);
225 if (ret) { 220 if (ret) {
226 dev_err(lp->dev, "iio dev register err: %d\n", ret); 221 dev_err(&pdev->dev, "iio dev register err: %d\n", ret);
227 goto err_iio_device; 222 goto err_iio_device;
228 } 223 }
229 224
230 return 0; 225 return 0;
231 226
232err_iio_device: 227err_iio_device:
233 lp8788_iio_map_unregister(indio_dev, adc); 228 iio_map_array_unregister(indio_dev);
234err_iio_map: 229err_iio_map:
235 iio_device_free(indio_dev); 230 iio_device_free(indio_dev);
236 return ret; 231 return ret;
@@ -239,10 +234,9 @@ err_iio_map:
239static int lp8788_adc_remove(struct platform_device *pdev) 234static int lp8788_adc_remove(struct platform_device *pdev)
240{ 235{
241 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 236 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
242 struct lp8788_adc *adc = iio_priv(indio_dev);
243 237
244 iio_device_unregister(indio_dev); 238 iio_device_unregister(indio_dev);
245 lp8788_iio_map_unregister(indio_dev, adc); 239 iio_map_array_unregister(indio_dev);
246 iio_device_free(indio_dev); 240 iio_device_free(indio_dev);
247 241
248 return 0; 242 return 0;
diff --git a/drivers/iio/adc/max1363.c b/drivers/iio/adc/max1363.c
index 03b25b3dc71e..6c1cfb74bdfc 100644
--- a/drivers/iio/adc/max1363.c
+++ b/drivers/iio/adc/max1363.c
@@ -39,6 +39,7 @@
39#include <linux/iio/driver.h> 39#include <linux/iio/driver.h>
40#include <linux/iio/kfifo_buf.h> 40#include <linux/iio/kfifo_buf.h>
41#include <linux/iio/trigger_consumer.h> 41#include <linux/iio/trigger_consumer.h>
42#include <linux/iio/triggered_buffer.h>
42 43
43#define MAX1363_SETUP_BYTE(a) ((a) | 0x80) 44#define MAX1363_SETUP_BYTE(a) ((a) | 0x80)
44 45
@@ -55,7 +56,7 @@
55#define MAX1363_SETUP_POWER_UP_INT_REF 0x10 56#define MAX1363_SETUP_POWER_UP_INT_REF 0x10
56#define MAX1363_SETUP_POWER_DOWN_INT_REF 0x00 57#define MAX1363_SETUP_POWER_DOWN_INT_REF 0x00
57 58
58/* think about includeing max11600 etc - more settings */ 59/* think about including max11600 etc - more settings */
59#define MAX1363_SETUP_EXT_CLOCK 0x08 60#define MAX1363_SETUP_EXT_CLOCK 0x08
60#define MAX1363_SETUP_INT_CLOCK 0x00 61#define MAX1363_SETUP_INT_CLOCK 0x00
61#define MAX1363_SETUP_UNIPOLAR 0x00 62#define MAX1363_SETUP_UNIPOLAR 0x00
@@ -86,7 +87,7 @@
86/* max123{6-9} only */ 87/* max123{6-9} only */
87#define MAX1236_SCAN_MID_TO_CHANNEL 0x40 88#define MAX1236_SCAN_MID_TO_CHANNEL 0x40
88 89
89/* max1363 only - merely part of channel selects or don't care for others*/ 90/* max1363 only - merely part of channel selects or don't care for others */
90#define MAX1363_CONFIG_EN_MON_MODE_READ 0x18 91#define MAX1363_CONFIG_EN_MON_MODE_READ 0x18
91 92
92#define MAX1363_CHANNEL_SEL(a) ((a) << 1) 93#define MAX1363_CHANNEL_SEL(a) ((a) << 1)
@@ -133,7 +134,7 @@ enum max1363_modes {
133 * @mode_list: array of available scan modes 134 * @mode_list: array of available scan modes
134 * @default_mode: the scan mode in which the chip starts up 135 * @default_mode: the scan mode in which the chip starts up
135 * @int_vref_mv: the internal reference voltage 136 * @int_vref_mv: the internal reference voltage
136 * @num_channels: number of channels 137 * @num_modes: number of modes
137 * @bits: accuracy of the adc in bits 138 * @bits: accuracy of the adc in bits
138 */ 139 */
139struct max1363_chip_info { 140struct max1363_chip_info {
@@ -152,7 +153,7 @@ struct max1363_chip_info {
152 * @client: i2c_client 153 * @client: i2c_client
153 * @setupbyte: cache of current device setup byte 154 * @setupbyte: cache of current device setup byte
154 * @configbyte: cache of current device config byte 155 * @configbyte: cache of current device config byte
155 * @chip_info: chip model specific constants, available modes etc 156 * @chip_info: chip model specific constants, available modes, etc.
156 * @current_mode: the scan mode of this chip 157 * @current_mode: the scan mode of this chip
157 * @requestedmask: a valid requested set of channels 158 * @requestedmask: a valid requested set of channels
158 * @reg: supply regulator 159 * @reg: supply regulator
@@ -162,6 +163,8 @@ struct max1363_chip_info {
162 * @mask_low: bitmask for enabled low thresholds 163 * @mask_low: bitmask for enabled low thresholds
163 * @thresh_high: high threshold values 164 * @thresh_high: high threshold values
164 * @thresh_low: low threshold values 165 * @thresh_low: low threshold values
166 * @vref: Reference voltage regulator
167 * @vref_uv: Actual (external or internal) reference voltage
165 */ 168 */
166struct max1363_state { 169struct max1363_state {
167 struct i2c_client *client; 170 struct i2c_client *client;
@@ -181,6 +184,8 @@ struct max1363_state {
181 /* 4x unipolar first then the fours bipolar ones */ 184 /* 4x unipolar first then the fours bipolar ones */
182 s16 thresh_high[8]; 185 s16 thresh_high[8];
183 s16 thresh_low[8]; 186 s16 thresh_low[8];
187 struct regulator *vref;
188 u32 vref_uv;
184}; 189};
185 190
186#define MAX1363_MODE_SINGLE(_num, _mask) { \ 191#define MAX1363_MODE_SINGLE(_num, _mask) { \
@@ -293,7 +298,7 @@ static const struct max1363_mode max1363_mode_table[] = {
293 298
294static const struct max1363_mode 299static const struct max1363_mode
295*max1363_match_mode(const unsigned long *mask, 300*max1363_match_mode(const unsigned long *mask,
296const struct max1363_chip_info *ci) 301 const struct max1363_chip_info *ci)
297{ 302{
298 int i; 303 int i;
299 if (mask) 304 if (mask)
@@ -334,7 +339,7 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
334{ 339{
335 int ret = 0; 340 int ret = 0;
336 s32 data; 341 s32 data;
337 char rxbuf[2]; 342 u8 rxbuf[2];
338 struct max1363_state *st = iio_priv(indio_dev); 343 struct max1363_state *st = iio_priv(indio_dev);
339 struct i2c_client *client = st->client; 344 struct i2c_client *client = st->client;
340 345
@@ -366,7 +371,8 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
366 ret = data; 371 ret = data;
367 goto error_ret; 372 goto error_ret;
368 } 373 }
369 data = (s32)(rxbuf[1]) | ((s32)(rxbuf[0] & 0x0F)) << 8; 374 data = (rxbuf[1] | rxbuf[0] << 8) &
375 ((1 << st->chip_info->bits) - 1);
370 } else { 376 } else {
371 /* Get reading */ 377 /* Get reading */
372 data = i2c_master_recv(client, rxbuf, 1); 378 data = i2c_master_recv(client, rxbuf, 1);
@@ -391,6 +397,8 @@ static int max1363_read_raw(struct iio_dev *indio_dev,
391{ 397{
392 struct max1363_state *st = iio_priv(indio_dev); 398 struct max1363_state *st = iio_priv(indio_dev);
393 int ret; 399 int ret;
400 unsigned long scale_uv;
401
394 switch (m) { 402 switch (m) {
395 case IIO_CHAN_INFO_RAW: 403 case IIO_CHAN_INFO_RAW:
396 ret = max1363_read_single_chan(indio_dev, chan, val, m); 404 ret = max1363_read_single_chan(indio_dev, chan, val, m);
@@ -398,16 +406,10 @@ static int max1363_read_raw(struct iio_dev *indio_dev,
398 return ret; 406 return ret;
399 return IIO_VAL_INT; 407 return IIO_VAL_INT;
400 case IIO_CHAN_INFO_SCALE: 408 case IIO_CHAN_INFO_SCALE:
401 if ((1 << (st->chip_info->bits + 1)) > 409 scale_uv = st->vref_uv >> st->chip_info->bits;
402 st->chip_info->int_vref_mv) { 410 *val = scale_uv / 1000;
403 *val = 0; 411 *val2 = (scale_uv % 1000) * 1000;
404 *val2 = 500000; 412 return IIO_VAL_INT_PLUS_MICRO;
405 return IIO_VAL_INT_PLUS_MICRO;
406 } else {
407 *val = (st->chip_info->int_vref_mv)
408 >> st->chip_info->bits;
409 return IIO_VAL_INT;
410 }
411 default: 413 default:
412 return -EINVAL; 414 return -EINVAL;
413 } 415 }
@@ -1388,13 +1390,17 @@ static const struct max1363_chip_info max1363_chip_info_tbl[] = {
1388 1390
1389static int max1363_initial_setup(struct max1363_state *st) 1391static int max1363_initial_setup(struct max1363_state *st)
1390{ 1392{
1391 st->setupbyte = MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD 1393 st->setupbyte = MAX1363_SETUP_INT_CLOCK
1392 | MAX1363_SETUP_POWER_UP_INT_REF
1393 | MAX1363_SETUP_INT_CLOCK
1394 | MAX1363_SETUP_UNIPOLAR 1394 | MAX1363_SETUP_UNIPOLAR
1395 | MAX1363_SETUP_NORESET; 1395 | MAX1363_SETUP_NORESET;
1396 1396
1397 /* Set scan mode writes the config anyway so wait until then*/ 1397 if (st->vref)
1398 st->setupbyte |= MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF;
1399 else
1400 st->setupbyte |= MAX1363_SETUP_POWER_UP_INT_REF
1401 | MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT;
1402
1403 /* Set scan mode writes the config anyway so wait until then */
1398 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte); 1404 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
1399 st->current_mode = &max1363_mode_table[st->chip_info->default_mode]; 1405 st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
1400 st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte); 1406 st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte);
@@ -1408,8 +1414,9 @@ static int max1363_alloc_scan_masks(struct iio_dev *indio_dev)
1408 unsigned long *masks; 1414 unsigned long *masks;
1409 int i; 1415 int i;
1410 1416
1411 masks = kzalloc(BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*sizeof(long)* 1417 masks = devm_kzalloc(&indio_dev->dev,
1412 (st->chip_info->num_modes + 1), GFP_KERNEL); 1418 BITS_TO_LONGS(MAX1363_MAX_CHANNELS) * sizeof(long) *
1419 (st->chip_info->num_modes + 1), GFP_KERNEL);
1413 if (!masks) 1420 if (!masks)
1414 return -ENOMEM; 1421 return -ENOMEM;
1415 1422
@@ -1423,7 +1430,6 @@ static int max1363_alloc_scan_masks(struct iio_dev *indio_dev)
1423 return 0; 1430 return 0;
1424} 1431}
1425 1432
1426
1427static irqreturn_t max1363_trigger_handler(int irq, void *p) 1433static irqreturn_t max1363_trigger_handler(int irq, void *p)
1428{ 1434{
1429 struct iio_poll_func *pf = p; 1435 struct iio_poll_func *pf = p;
@@ -1483,54 +1489,13 @@ static const struct iio_buffer_setup_ops max1363_buffered_setup_ops = {
1483 .predisable = &iio_triggered_buffer_predisable, 1489 .predisable = &iio_triggered_buffer_predisable,
1484}; 1490};
1485 1491
1486static int max1363_register_buffered_funcs_and_init(struct iio_dev *indio_dev)
1487{
1488 struct max1363_state *st = iio_priv(indio_dev);
1489 int ret = 0;
1490
1491 indio_dev->buffer = iio_kfifo_allocate(indio_dev);
1492 if (!indio_dev->buffer) {
1493 ret = -ENOMEM;
1494 goto error_ret;
1495 }
1496 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
1497 &max1363_trigger_handler,
1498 IRQF_ONESHOT,
1499 indio_dev,
1500 "%s_consumer%d",
1501 st->client->name,
1502 indio_dev->id);
1503 if (indio_dev->pollfunc == NULL) {
1504 ret = -ENOMEM;
1505 goto error_deallocate_sw_rb;
1506 }
1507 /* Buffer functions - here trigger setup related */
1508 indio_dev->setup_ops = &max1363_buffered_setup_ops;
1509
1510 /* Flag that polled buffering is possible */
1511 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
1512
1513 return 0;
1514
1515error_deallocate_sw_rb:
1516 iio_kfifo_free(indio_dev->buffer);
1517error_ret:
1518 return ret;
1519}
1520
1521static void max1363_buffer_cleanup(struct iio_dev *indio_dev)
1522{
1523 /* ensure that the trigger has been detached */
1524 iio_dealloc_pollfunc(indio_dev->pollfunc);
1525 iio_kfifo_free(indio_dev->buffer);
1526}
1527
1528static int max1363_probe(struct i2c_client *client, 1492static int max1363_probe(struct i2c_client *client,
1529 const struct i2c_device_id *id) 1493 const struct i2c_device_id *id)
1530{ 1494{
1531 int ret; 1495 int ret;
1532 struct max1363_state *st; 1496 struct max1363_state *st;
1533 struct iio_dev *indio_dev; 1497 struct iio_dev *indio_dev;
1498 struct regulator *vref;
1534 1499
1535 indio_dev = iio_device_alloc(sizeof(struct max1363_state)); 1500 indio_dev = iio_device_alloc(sizeof(struct max1363_state));
1536 if (indio_dev == NULL) { 1501 if (indio_dev == NULL) {
@@ -1538,13 +1503,14 @@ static int max1363_probe(struct i2c_client *client,
1538 goto error_out; 1503 goto error_out;
1539 } 1504 }
1540 1505
1506 indio_dev->dev.of_node = client->dev.of_node;
1541 ret = iio_map_array_register(indio_dev, client->dev.platform_data); 1507 ret = iio_map_array_register(indio_dev, client->dev.platform_data);
1542 if (ret < 0) 1508 if (ret < 0)
1543 goto error_free_device; 1509 goto error_free_device;
1544 1510
1545 st = iio_priv(indio_dev); 1511 st = iio_priv(indio_dev);
1546 1512
1547 st->reg = regulator_get(&client->dev, "vcc"); 1513 st->reg = devm_regulator_get(&client->dev, "vcc");
1548 if (IS_ERR(st->reg)) { 1514 if (IS_ERR(st->reg)) {
1549 ret = PTR_ERR(st->reg); 1515 ret = PTR_ERR(st->reg);
1550 goto error_unregister_map; 1516 goto error_unregister_map;
@@ -1552,7 +1518,7 @@ static int max1363_probe(struct i2c_client *client,
1552 1518
1553 ret = regulator_enable(st->reg); 1519 ret = regulator_enable(st->reg);
1554 if (ret) 1520 if (ret)
1555 goto error_put_reg; 1521 goto error_unregister_map;
1556 1522
1557 /* this is only used for device removal purposes */ 1523 /* this is only used for device removal purposes */
1558 i2c_set_clientdata(client, indio_dev); 1524 i2c_set_clientdata(client, indio_dev);
@@ -1560,35 +1526,45 @@ static int max1363_probe(struct i2c_client *client,
1560 st->chip_info = &max1363_chip_info_tbl[id->driver_data]; 1526 st->chip_info = &max1363_chip_info_tbl[id->driver_data];
1561 st->client = client; 1527 st->client = client;
1562 1528
1529 st->vref_uv = st->chip_info->int_vref_mv * 1000;
1530 vref = devm_regulator_get(&client->dev, "vref");
1531 if (!IS_ERR(vref)) {
1532 int vref_uv;
1533
1534 ret = regulator_enable(vref);
1535 if (ret)
1536 goto error_disable_reg;
1537 st->vref = vref;
1538 vref_uv = regulator_get_voltage(vref);
1539 if (vref_uv <= 0) {
1540 ret = -EINVAL;
1541 goto error_disable_reg;
1542 }
1543 st->vref_uv = vref_uv;
1544 }
1545
1563 ret = max1363_alloc_scan_masks(indio_dev); 1546 ret = max1363_alloc_scan_masks(indio_dev);
1564 if (ret) 1547 if (ret)
1565 goto error_disable_reg; 1548 goto error_disable_reg;
1566 1549
1567 /* Estabilish that the iio_dev is a child of the i2c device */ 1550 /* Establish that the iio_dev is a child of the i2c device */
1568 indio_dev->dev.parent = &client->dev; 1551 indio_dev->dev.parent = &client->dev;
1569 indio_dev->name = id->name; 1552 indio_dev->name = id->name;
1570 indio_dev->channels = st->chip_info->channels; 1553 indio_dev->channels = st->chip_info->channels;
1571 indio_dev->num_channels = st->chip_info->num_channels; 1554 indio_dev->num_channels = st->chip_info->num_channels;
1572 indio_dev->info = st->chip_info->info; 1555 indio_dev->info = st->chip_info->info;
1573 indio_dev->modes = INDIO_DIRECT_MODE; 1556 indio_dev->modes = INDIO_DIRECT_MODE;
1574 indio_dev->channels = st->chip_info->channels;
1575 indio_dev->num_channels = st->chip_info->num_channels;
1576 ret = max1363_initial_setup(st); 1557 ret = max1363_initial_setup(st);
1577 if (ret < 0) 1558 if (ret < 0)
1578 goto error_free_available_scan_masks; 1559 goto error_disable_reg;
1579
1580 ret = max1363_register_buffered_funcs_and_init(indio_dev);
1581 if (ret)
1582 goto error_free_available_scan_masks;
1583 1560
1584 ret = iio_buffer_register(indio_dev, 1561 ret = iio_triggered_buffer_setup(indio_dev, NULL,
1585 st->chip_info->channels, 1562 &max1363_trigger_handler, &max1363_buffered_setup_ops);
1586 st->chip_info->num_channels);
1587 if (ret) 1563 if (ret)
1588 goto error_cleanup_buffer; 1564 goto error_disable_reg;
1589 1565
1590 if (client->irq) { 1566 if (client->irq) {
1591 ret = request_threaded_irq(st->client->irq, 1567 ret = devm_request_threaded_irq(&client->dev, st->client->irq,
1592 NULL, 1568 NULL,
1593 &max1363_event_handler, 1569 &max1363_event_handler,
1594 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 1570 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
@@ -1601,24 +1577,18 @@ static int max1363_probe(struct i2c_client *client,
1601 1577
1602 ret = iio_device_register(indio_dev); 1578 ret = iio_device_register(indio_dev);
1603 if (ret < 0) 1579 if (ret < 0)
1604 goto error_free_irq; 1580 goto error_uninit_buffer;
1605 1581
1606 return 0; 1582 return 0;
1607error_free_irq: 1583
1608 if (client->irq)
1609 free_irq(st->client->irq, indio_dev);
1610error_uninit_buffer: 1584error_uninit_buffer:
1611 iio_buffer_unregister(indio_dev); 1585 iio_triggered_buffer_cleanup(indio_dev);
1612error_cleanup_buffer:
1613 max1363_buffer_cleanup(indio_dev);
1614error_free_available_scan_masks:
1615 kfree(indio_dev->available_scan_masks);
1616error_disable_reg: 1586error_disable_reg:
1587 if (st->vref)
1588 regulator_disable(st->vref);
1617 regulator_disable(st->reg); 1589 regulator_disable(st->reg);
1618error_put_reg:
1619 regulator_put(st->reg);
1620error_unregister_map: 1590error_unregister_map:
1621 iio_map_array_unregister(indio_dev, client->dev.platform_data); 1591 iio_map_array_unregister(indio_dev);
1622error_free_device: 1592error_free_device:
1623 iio_device_free(indio_dev); 1593 iio_device_free(indio_dev);
1624error_out: 1594error_out:
@@ -1631,14 +1601,11 @@ static int max1363_remove(struct i2c_client *client)
1631 struct max1363_state *st = iio_priv(indio_dev); 1601 struct max1363_state *st = iio_priv(indio_dev);
1632 1602
1633 iio_device_unregister(indio_dev); 1603 iio_device_unregister(indio_dev);
1634 if (client->irq) 1604 iio_triggered_buffer_cleanup(indio_dev);
1635 free_irq(st->client->irq, indio_dev); 1605 if (st->vref)
1636 iio_buffer_unregister(indio_dev); 1606 regulator_disable(st->vref);
1637 max1363_buffer_cleanup(indio_dev);
1638 kfree(indio_dev->available_scan_masks);
1639 regulator_disable(st->reg); 1607 regulator_disable(st->reg);
1640 regulator_put(st->reg); 1608 iio_map_array_unregister(indio_dev);
1641 iio_map_array_unregister(indio_dev, client->dev.platform_data);
1642 iio_device_free(indio_dev); 1609 iio_device_free(indio_dev);
1643 1610
1644 return 0; 1611 return 0;
diff --git a/drivers/iio/buffer_cb.c b/drivers/iio/buffer_cb.c
index 4d40e24f3721..9201022945e9 100644
--- a/drivers/iio/buffer_cb.c
+++ b/drivers/iio/buffer_cb.c
@@ -25,7 +25,7 @@ static struct iio_buffer_access_funcs iio_cb_access = {
25 .store_to = &iio_buffer_cb_store_to, 25 .store_to = &iio_buffer_cb_store_to,
26}; 26};
27 27
28struct iio_cb_buffer *iio_channel_get_all_cb(const char *name, 28struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
29 int (*cb)(u8 *data, 29 int (*cb)(u8 *data,
30 void *private), 30 void *private),
31 void *private) 31 void *private)
@@ -46,7 +46,7 @@ struct iio_cb_buffer *iio_channel_get_all_cb(const char *name,
46 cb_buff->buffer.access = &iio_cb_access; 46 cb_buff->buffer.access = &iio_cb_access;
47 INIT_LIST_HEAD(&cb_buff->buffer.demux_list); 47 INIT_LIST_HEAD(&cb_buff->buffer.demux_list);
48 48
49 cb_buff->channels = iio_channel_get_all(name); 49 cb_buff->channels = iio_channel_get_all(dev);
50 if (IS_ERR(cb_buff->channels)) { 50 if (IS_ERR(cb_buff->channels)) {
51 ret = PTR_ERR(cb_buff->channels); 51 ret = PTR_ERR(cb_buff->channels);
52 goto error_free_cb_buff; 52 goto error_free_cb_buff;
diff --git a/drivers/iio/common/Kconfig b/drivers/iio/common/Kconfig
index ed45ee54500c..0b6e97d18fa0 100644
--- a/drivers/iio/common/Kconfig
+++ b/drivers/iio/common/Kconfig
@@ -3,3 +3,4 @@
3# 3#
4 4
5source "drivers/iio/common/hid-sensors/Kconfig" 5source "drivers/iio/common/hid-sensors/Kconfig"
6source "drivers/iio/common/st_sensors/Kconfig"
diff --git a/drivers/iio/common/Makefile b/drivers/iio/common/Makefile
index 81584009b21b..c2352beb5d97 100644
--- a/drivers/iio/common/Makefile
+++ b/drivers/iio/common/Makefile
@@ -7,3 +7,4 @@
7# 7#
8 8
9obj-y += hid-sensors/ 9obj-y += hid-sensors/
10obj-y += st_sensors/
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
index 75374955caba..75b54730a963 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
@@ -25,7 +25,6 @@
25#include <linux/hid-sensor-hub.h> 25#include <linux/hid-sensor-hub.h>
26#include <linux/iio/iio.h> 26#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h> 27#include <linux/iio/sysfs.h>
28#include "hid-sensor-attributes.h"
29 28
30static int pow_10(unsigned power) 29static int pow_10(unsigned power)
31{ 30{
@@ -114,7 +113,7 @@ static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
114 return value; 113 return value;
115} 114}
116 115
117int hid_sensor_read_samp_freq_value(struct hid_sensor_iio_common *st, 116int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
118 int *val1, int *val2) 117 int *val1, int *val2)
119{ 118{
120 s32 value; 119 s32 value;
@@ -141,7 +140,7 @@ int hid_sensor_read_samp_freq_value(struct hid_sensor_iio_common *st,
141} 140}
142EXPORT_SYMBOL(hid_sensor_read_samp_freq_value); 141EXPORT_SYMBOL(hid_sensor_read_samp_freq_value);
143 142
144int hid_sensor_write_samp_freq_value(struct hid_sensor_iio_common *st, 143int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
145 int val1, int val2) 144 int val1, int val2)
146{ 145{
147 s32 value; 146 s32 value;
@@ -169,7 +168,7 @@ int hid_sensor_write_samp_freq_value(struct hid_sensor_iio_common *st,
169} 168}
170EXPORT_SYMBOL(hid_sensor_write_samp_freq_value); 169EXPORT_SYMBOL(hid_sensor_write_samp_freq_value);
171 170
172int hid_sensor_read_raw_hyst_value(struct hid_sensor_iio_common *st, 171int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
173 int *val1, int *val2) 172 int *val1, int *val2)
174{ 173{
175 s32 value; 174 s32 value;
@@ -191,7 +190,7 @@ int hid_sensor_read_raw_hyst_value(struct hid_sensor_iio_common *st,
191} 190}
192EXPORT_SYMBOL(hid_sensor_read_raw_hyst_value); 191EXPORT_SYMBOL(hid_sensor_read_raw_hyst_value);
193 192
194int hid_sensor_write_raw_hyst_value(struct hid_sensor_iio_common *st, 193int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
195 int val1, int val2) 194 int val1, int val2)
196{ 195{
197 s32 value; 196 s32 value;
@@ -212,7 +211,7 @@ EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
212 211
213int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev, 212int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
214 u32 usage_id, 213 u32 usage_id,
215 struct hid_sensor_iio_common *st) 214 struct hid_sensor_common *st)
216{ 215{
217 216
218 sensor_hub_input_get_attribute_info(hsdev, 217 sensor_hub_input_get_attribute_info(hsdev,
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.h b/drivers/iio/common/hid-sensors/hid-sensor-attributes.h
deleted file mode 100644
index a4676a0c3de5..000000000000
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.h
+++ /dev/null
@@ -1,57 +0,0 @@
1/*
2 * HID Sensors Driver
3 * Copyright (c) 2012, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19#ifndef _HID_SENSORS_ATTRIBUTES_H
20#define _HID_SENSORS_ATTRIBUTES_H
21
22/* Common hid sensor iio structure */
23struct hid_sensor_iio_common {
24 struct hid_sensor_hub_device *hsdev;
25 struct platform_device *pdev;
26 unsigned usage_id;
27 bool data_ready;
28 struct hid_sensor_hub_attribute_info poll;
29 struct hid_sensor_hub_attribute_info report_state;
30 struct hid_sensor_hub_attribute_info power_state;
31 struct hid_sensor_hub_attribute_info sensitivity;
32};
33
34/*Convert from hid unit expo to regular exponent*/
35static inline int hid_sensor_convert_exponent(int unit_expo)
36{
37 if (unit_expo < 0x08)
38 return unit_expo;
39 else if (unit_expo <= 0x0f)
40 return -(0x0f-unit_expo+1);
41 else
42 return 0;
43}
44
45int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
46 u32 usage_id,
47 struct hid_sensor_iio_common *st);
48int hid_sensor_write_raw_hyst_value(struct hid_sensor_iio_common *st,
49 int val1, int val2);
50int hid_sensor_read_raw_hyst_value(struct hid_sensor_iio_common *st,
51 int *val1, int *val2);
52int hid_sensor_write_samp_freq_value(struct hid_sensor_iio_common *st,
53 int val1, int val2);
54int hid_sensor_read_samp_freq_value(struct hid_sensor_iio_common *st,
55 int *val1, int *val2);
56
57#endif
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
index d60198a6ca29..7a525a91105d 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
@@ -26,13 +26,12 @@
26#include <linux/iio/iio.h> 26#include <linux/iio/iio.h>
27#include <linux/iio/trigger.h> 27#include <linux/iio/trigger.h>
28#include <linux/iio/sysfs.h> 28#include <linux/iio/sysfs.h>
29#include "hid-sensor-attributes.h"
30#include "hid-sensor-trigger.h" 29#include "hid-sensor-trigger.h"
31 30
32static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig, 31static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
33 bool state) 32 bool state)
34{ 33{
35 struct hid_sensor_iio_common *st = trig->private_data; 34 struct hid_sensor_common *st = trig->private_data;
36 int state_val; 35 int state_val;
37 36
38 state_val = state ? 1 : 0; 37 state_val = state ? 1 : 0;
@@ -64,7 +63,7 @@ static const struct iio_trigger_ops hid_sensor_trigger_ops = {
64}; 63};
65 64
66int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, 65int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
67 struct hid_sensor_iio_common *attrb) 66 struct hid_sensor_common *attrb)
68{ 67{
69 int ret; 68 int ret;
70 struct iio_trigger *trig; 69 struct iio_trigger *trig;
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-trigger.h b/drivers/iio/common/hid-sensors/hid-sensor-trigger.h
index fd982971b1b8..9a8731478eda 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-trigger.h
+++ b/drivers/iio/common/hid-sensors/hid-sensor-trigger.h
@@ -20,7 +20,7 @@
20#define _HID_SENSOR_TRIGGER_H 20#define _HID_SENSOR_TRIGGER_H
21 21
22int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, 22int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
23 struct hid_sensor_iio_common *attrb); 23 struct hid_sensor_common *attrb);
24void hid_sensor_remove_trigger(struct iio_dev *indio_dev); 24void hid_sensor_remove_trigger(struct iio_dev *indio_dev);
25 25
26#endif 26#endif
diff --git a/drivers/iio/common/st_sensors/Kconfig b/drivers/iio/common/st_sensors/Kconfig
new file mode 100644
index 000000000000..865f1ca33eb9
--- /dev/null
+++ b/drivers/iio/common/st_sensors/Kconfig
@@ -0,0 +1,14 @@
1#
2# STMicroelectronics sensors common library
3#
4
5config IIO_ST_SENSORS_I2C
6 tristate
7
8config IIO_ST_SENSORS_SPI
9 tristate
10
11config IIO_ST_SENSORS_CORE
12 tristate
13 select IIO_ST_SENSORS_I2C if I2C
14 select IIO_ST_SENSORS_SPI if SPI_MASTER
diff --git a/drivers/iio/common/st_sensors/Makefile b/drivers/iio/common/st_sensors/Makefile
new file mode 100644
index 000000000000..9f3e24f3024b
--- /dev/null
+++ b/drivers/iio/common/st_sensors/Makefile
@@ -0,0 +1,10 @@
1#
2# Makefile for the STMicroelectronics sensor common modules.
3#
4
5obj-$(CONFIG_IIO_ST_SENSORS_I2C) += st_sensors_i2c.o
6obj-$(CONFIG_IIO_ST_SENSORS_SPI) += st_sensors_spi.o
7obj-$(CONFIG_IIO_ST_SENSORS_CORE) += st_sensors.o
8st_sensors-y := st_sensors_core.o
9st_sensors-$(CONFIG_IIO_BUFFER) += st_sensors_buffer.o
10st_sensors-$(CONFIG_IIO_TRIGGER) += st_sensors_trigger.o
diff --git a/drivers/iio/common/st_sensors/st_sensors_buffer.c b/drivers/iio/common/st_sensors/st_sensors_buffer.c
new file mode 100644
index 000000000000..09b236d6ee89
--- /dev/null
+++ b/drivers/iio/common/st_sensors/st_sensors_buffer.c
@@ -0,0 +1,116 @@
1/*
2 * STMicroelectronics sensors buffer library 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/iio/iio.h>
15#include <linux/iio/trigger.h>
16#include <linux/interrupt.h>
17#include <linux/iio/buffer.h>
18#include <linux/iio/trigger_consumer.h>
19#include <linux/iio/triggered_buffer.h>
20#include <linux/irqreturn.h>
21
22#include <linux/iio/common/st_sensors.h>
23
24
25int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
26{
27 int i, n = 0, len;
28 u8 addr[ST_SENSORS_NUMBER_DATA_CHANNELS];
29 struct st_sensor_data *sdata = iio_priv(indio_dev);
30
31 for (i = 0; i < ST_SENSORS_NUMBER_DATA_CHANNELS; i++) {
32 if (test_bit(i, indio_dev->active_scan_mask)) {
33 addr[n] = indio_dev->channels[i].address;
34 n++;
35 }
36 }
37 switch (n) {
38 case 1:
39 len = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
40 addr[0], ST_SENSORS_BYTE_FOR_CHANNEL, buf,
41 sdata->multiread_bit);
42 break;
43 case 2:
44 if ((addr[1] - addr[0]) == ST_SENSORS_BYTE_FOR_CHANNEL) {
45 len = sdata->tf->read_multiple_byte(&sdata->tb,
46 sdata->dev, addr[0],
47 ST_SENSORS_BYTE_FOR_CHANNEL*n,
48 buf, sdata->multiread_bit);
49 } else {
50 u8 rx_array[ST_SENSORS_BYTE_FOR_CHANNEL*
51 ST_SENSORS_NUMBER_DATA_CHANNELS];
52 len = sdata->tf->read_multiple_byte(&sdata->tb,
53 sdata->dev, addr[0],
54 ST_SENSORS_BYTE_FOR_CHANNEL*
55 ST_SENSORS_NUMBER_DATA_CHANNELS,
56 rx_array, sdata->multiread_bit);
57 if (len < 0)
58 goto read_data_channels_error;
59
60 for (i = 0; i < n * ST_SENSORS_NUMBER_DATA_CHANNELS;
61 i++) {
62 if (i < n)
63 buf[i] = rx_array[i];
64 else
65 buf[i] = rx_array[n + i];
66 }
67 len = ST_SENSORS_BYTE_FOR_CHANNEL*n;
68 }
69 break;
70 case 3:
71 len = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
72 addr[0], ST_SENSORS_BYTE_FOR_CHANNEL*
73 ST_SENSORS_NUMBER_DATA_CHANNELS,
74 buf, sdata->multiread_bit);
75 break;
76 default:
77 len = -EINVAL;
78 goto read_data_channels_error;
79 }
80 if (len != ST_SENSORS_BYTE_FOR_CHANNEL*n) {
81 len = -EIO;
82 goto read_data_channels_error;
83 }
84
85read_data_channels_error:
86 return len;
87}
88EXPORT_SYMBOL(st_sensors_get_buffer_element);
89
90irqreturn_t st_sensors_trigger_handler(int irq, void *p)
91{
92 int len;
93 struct iio_poll_func *pf = p;
94 struct iio_dev *indio_dev = pf->indio_dev;
95 struct st_sensor_data *sdata = iio_priv(indio_dev);
96
97 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
98 if (len < 0)
99 goto st_sensors_get_buffer_element_error;
100
101 if (indio_dev->scan_timestamp)
102 *(s64 *)((u8 *)sdata->buffer_data +
103 ALIGN(len, sizeof(s64))) = pf->timestamp;
104
105 iio_push_to_buffers(indio_dev, sdata->buffer_data);
106
107st_sensors_get_buffer_element_error:
108 iio_trigger_notify_done(indio_dev->trig);
109
110 return IRQ_HANDLED;
111}
112EXPORT_SYMBOL(st_sensors_trigger_handler);
113
114MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
115MODULE_DESCRIPTION("STMicroelectronics ST-sensors buffer");
116MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
new file mode 100644
index 000000000000..0198324a8b0c
--- /dev/null
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -0,0 +1,446 @@
1/*
2 * STMicroelectronics sensors core library 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/delay.h>
15#include <linux/iio/iio.h>
16#include <asm/unaligned.h>
17
18#include <linux/iio/common/st_sensors.h>
19
20
21#define ST_SENSORS_WAI_ADDRESS 0x0f
22
23static int st_sensors_write_data_with_mask(struct iio_dev *indio_dev,
24 u8 reg_addr, u8 mask, u8 data)
25{
26 int err;
27 u8 new_data;
28 struct st_sensor_data *sdata = iio_priv(indio_dev);
29
30 err = sdata->tf->read_byte(&sdata->tb, sdata->dev, reg_addr, &new_data);
31 if (err < 0)
32 goto st_sensors_write_data_with_mask_error;
33
34 new_data = ((new_data & (~mask)) | ((data << __ffs(mask)) & mask));
35 err = sdata->tf->write_byte(&sdata->tb, sdata->dev, reg_addr, new_data);
36
37st_sensors_write_data_with_mask_error:
38 return err;
39}
40
41static int st_sensors_match_odr(struct st_sensors *sensor,
42 unsigned int odr, struct st_sensor_odr_avl *odr_out)
43{
44 int i, ret = -EINVAL;
45
46 for (i = 0; i < ST_SENSORS_ODR_LIST_MAX; i++) {
47 if (sensor->odr.odr_avl[i].hz == 0)
48 goto st_sensors_match_odr_error;
49
50 if (sensor->odr.odr_avl[i].hz == odr) {
51 odr_out->hz = sensor->odr.odr_avl[i].hz;
52 odr_out->value = sensor->odr.odr_avl[i].value;
53 ret = 0;
54 break;
55 }
56 }
57
58st_sensors_match_odr_error:
59 return ret;
60}
61
62int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr)
63{
64 int err;
65 struct st_sensor_odr_avl odr_out;
66 struct st_sensor_data *sdata = iio_priv(indio_dev);
67
68 err = st_sensors_match_odr(sdata->sensor, odr, &odr_out);
69 if (err < 0)
70 goto st_sensors_match_odr_error;
71
72 if ((sdata->sensor->odr.addr == sdata->sensor->pw.addr) &&
73 (sdata->sensor->odr.mask == sdata->sensor->pw.mask)) {
74 if (sdata->enabled == true) {
75 err = st_sensors_write_data_with_mask(indio_dev,
76 sdata->sensor->odr.addr,
77 sdata->sensor->odr.mask,
78 odr_out.value);
79 } else {
80 err = 0;
81 }
82 } else {
83 err = st_sensors_write_data_with_mask(indio_dev,
84 sdata->sensor->odr.addr, sdata->sensor->odr.mask,
85 odr_out.value);
86 }
87 if (err >= 0)
88 sdata->odr = odr_out.hz;
89
90st_sensors_match_odr_error:
91 return err;
92}
93EXPORT_SYMBOL(st_sensors_set_odr);
94
95static int st_sensors_match_fs(struct st_sensors *sensor,
96 unsigned int fs, int *index_fs_avl)
97{
98 int i, ret = -EINVAL;
99
100 for (i = 0; i < ST_SENSORS_FULLSCALE_AVL_MAX; i++) {
101 if (sensor->fs.fs_avl[i].num == 0)
102 goto st_sensors_match_odr_error;
103
104 if (sensor->fs.fs_avl[i].num == fs) {
105 *index_fs_avl = i;
106 ret = 0;
107 break;
108 }
109 }
110
111st_sensors_match_odr_error:
112 return ret;
113}
114
115static int st_sensors_set_fullscale(struct iio_dev *indio_dev, unsigned int fs)
116{
117 int err, i;
118 struct st_sensor_data *sdata = iio_priv(indio_dev);
119
120 err = st_sensors_match_fs(sdata->sensor, fs, &i);
121 if (err < 0)
122 goto st_accel_set_fullscale_error;
123
124 err = st_sensors_write_data_with_mask(indio_dev,
125 sdata->sensor->fs.addr,
126 sdata->sensor->fs.mask,
127 sdata->sensor->fs.fs_avl[i].value);
128 if (err < 0)
129 goto st_accel_set_fullscale_error;
130
131 sdata->current_fullscale = (struct st_sensor_fullscale_avl *)
132 &sdata->sensor->fs.fs_avl[i];
133 return err;
134
135st_accel_set_fullscale_error:
136 dev_err(&indio_dev->dev, "failed to set new fullscale.\n");
137 return err;
138}
139
140int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable)
141{
142 bool found;
143 u8 tmp_value;
144 int err = -EINVAL;
145 struct st_sensor_odr_avl odr_out;
146 struct st_sensor_data *sdata = iio_priv(indio_dev);
147
148 if (enable) {
149 found = false;
150 tmp_value = sdata->sensor->pw.value_on;
151 if ((sdata->sensor->odr.addr == sdata->sensor->pw.addr) &&
152 (sdata->sensor->odr.mask == sdata->sensor->pw.mask)) {
153 err = st_sensors_match_odr(sdata->sensor,
154 sdata->odr, &odr_out);
155 if (err < 0)
156 goto set_enable_error;
157 tmp_value = odr_out.value;
158 found = true;
159 }
160 err = st_sensors_write_data_with_mask(indio_dev,
161 sdata->sensor->pw.addr,
162 sdata->sensor->pw.mask, tmp_value);
163 if (err < 0)
164 goto set_enable_error;
165
166 sdata->enabled = true;
167
168 if (found)
169 sdata->odr = odr_out.hz;
170 } else {
171 err = st_sensors_write_data_with_mask(indio_dev,
172 sdata->sensor->pw.addr,
173 sdata->sensor->pw.mask,
174 sdata->sensor->pw.value_off);
175 if (err < 0)
176 goto set_enable_error;
177
178 sdata->enabled = false;
179 }
180
181set_enable_error:
182 return err;
183}
184EXPORT_SYMBOL(st_sensors_set_enable);
185
186int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable)
187{
188 struct st_sensor_data *sdata = iio_priv(indio_dev);
189
190 return st_sensors_write_data_with_mask(indio_dev,
191 sdata->sensor->enable_axis.addr,
192 sdata->sensor->enable_axis.mask, axis_enable);
193}
194EXPORT_SYMBOL(st_sensors_set_axis_enable);
195
196int st_sensors_init_sensor(struct iio_dev *indio_dev)
197{
198 int err;
199 struct st_sensor_data *sdata = iio_priv(indio_dev);
200
201 mutex_init(&sdata->tb.buf_lock);
202
203 err = st_sensors_set_enable(indio_dev, false);
204 if (err < 0)
205 goto init_error;
206
207 err = st_sensors_set_fullscale(indio_dev,
208 sdata->current_fullscale->num);
209 if (err < 0)
210 goto init_error;
211
212 err = st_sensors_set_odr(indio_dev, sdata->odr);
213 if (err < 0)
214 goto init_error;
215
216 /* set BDU */
217 err = st_sensors_write_data_with_mask(indio_dev,
218 sdata->sensor->bdu.addr, sdata->sensor->bdu.mask, true);
219 if (err < 0)
220 goto init_error;
221
222 err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
223
224init_error:
225 return err;
226}
227EXPORT_SYMBOL(st_sensors_init_sensor);
228
229int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable)
230{
231 int err;
232 struct st_sensor_data *sdata = iio_priv(indio_dev);
233
234 /* Enable/Disable the interrupt generator 1. */
235 if (sdata->sensor->drdy_irq.ig1.en_addr > 0) {
236 err = st_sensors_write_data_with_mask(indio_dev,
237 sdata->sensor->drdy_irq.ig1.en_addr,
238 sdata->sensor->drdy_irq.ig1.en_mask, (int)enable);
239 if (err < 0)
240 goto st_accel_set_dataready_irq_error;
241 }
242
243 /* Enable/Disable the interrupt generator for data ready. */
244 err = st_sensors_write_data_with_mask(indio_dev,
245 sdata->sensor->drdy_irq.addr,
246 sdata->sensor->drdy_irq.mask, (int)enable);
247
248st_accel_set_dataready_irq_error:
249 return err;
250}
251EXPORT_SYMBOL(st_sensors_set_dataready_irq);
252
253int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale)
254{
255 int err = -EINVAL, i;
256 struct st_sensor_data *sdata = iio_priv(indio_dev);
257
258 for (i = 0; i < ST_SENSORS_FULLSCALE_AVL_MAX; i++) {
259 if ((sdata->sensor->fs.fs_avl[i].gain == scale) &&
260 (sdata->sensor->fs.fs_avl[i].gain != 0)) {
261 err = 0;
262 break;
263 }
264 }
265 if (err < 0)
266 goto st_sensors_match_scale_error;
267
268 err = st_sensors_set_fullscale(indio_dev,
269 sdata->sensor->fs.fs_avl[i].num);
270
271st_sensors_match_scale_error:
272 return err;
273}
274EXPORT_SYMBOL(st_sensors_set_fullscale_by_gain);
275
276static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
277 u8 ch_addr, int *data)
278{
279 int err;
280 u8 outdata[ST_SENSORS_BYTE_FOR_CHANNEL];
281 struct st_sensor_data *sdata = iio_priv(indio_dev);
282
283 err = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
284 ch_addr, ST_SENSORS_BYTE_FOR_CHANNEL,
285 outdata, sdata->multiread_bit);
286 if (err < 0)
287 goto read_error;
288
289 *data = (s16)get_unaligned_le16(outdata);
290
291read_error:
292 return err;
293}
294
295int st_sensors_read_info_raw(struct iio_dev *indio_dev,
296 struct iio_chan_spec const *ch, int *val)
297{
298 int err;
299 struct st_sensor_data *sdata = iio_priv(indio_dev);
300
301 mutex_lock(&indio_dev->mlock);
302 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
303 err = -EBUSY;
304 goto read_error;
305 } else {
306 err = st_sensors_set_enable(indio_dev, true);
307 if (err < 0)
308 goto read_error;
309
310 msleep((sdata->sensor->bootime * 1000) / sdata->odr);
311 err = st_sensors_read_axis_data(indio_dev, ch->address, val);
312 if (err < 0)
313 goto read_error;
314
315 *val = *val >> ch->scan_type.shift;
316 }
317 mutex_unlock(&indio_dev->mlock);
318
319 return err;
320
321read_error:
322 mutex_unlock(&indio_dev->mlock);
323 return err;
324}
325EXPORT_SYMBOL(st_sensors_read_info_raw);
326
327int st_sensors_check_device_support(struct iio_dev *indio_dev,
328 int num_sensors_list, const struct st_sensors *sensors)
329{
330 u8 wai;
331 int i, n, err;
332 struct st_sensor_data *sdata = iio_priv(indio_dev);
333
334 err = sdata->tf->read_byte(&sdata->tb, sdata->dev,
335 ST_SENSORS_DEFAULT_WAI_ADDRESS, &wai);
336 if (err < 0) {
337 dev_err(&indio_dev->dev, "failed to read Who-Am-I register.\n");
338 goto read_wai_error;
339 }
340
341 for (i = 0; i < num_sensors_list; i++) {
342 if (sensors[i].wai == wai)
343 break;
344 }
345 if (i == num_sensors_list)
346 goto device_not_supported;
347
348 for (n = 0; n < ARRAY_SIZE(sensors[i].sensors_supported); n++) {
349 if (strcmp(indio_dev->name,
350 &sensors[i].sensors_supported[n][0]) == 0)
351 break;
352 }
353 if (n == ARRAY_SIZE(sensors[i].sensors_supported)) {
354 dev_err(&indio_dev->dev, "device name and WhoAmI mismatch.\n");
355 goto sensor_name_mismatch;
356 }
357
358 sdata->sensor = (struct st_sensors *)&sensors[i];
359
360 return i;
361
362device_not_supported:
363 dev_err(&indio_dev->dev, "device not supported: WhoAmI (0x%x).\n", wai);
364sensor_name_mismatch:
365 err = -ENODEV;
366read_wai_error:
367 return err;
368}
369EXPORT_SYMBOL(st_sensors_check_device_support);
370
371ssize_t st_sensors_sysfs_get_sampling_frequency(struct device *dev,
372 struct device_attribute *attr, char *buf)
373{
374 struct st_sensor_data *adata = iio_priv(dev_get_drvdata(dev));
375
376 return sprintf(buf, "%d\n", adata->odr);
377}
378EXPORT_SYMBOL(st_sensors_sysfs_get_sampling_frequency);
379
380ssize_t st_sensors_sysfs_set_sampling_frequency(struct device *dev,
381 struct device_attribute *attr, const char *buf, size_t size)
382{
383 int err;
384 unsigned int odr;
385 struct iio_dev *indio_dev = dev_get_drvdata(dev);
386
387 err = kstrtoint(buf, 10, &odr);
388 if (err < 0)
389 goto conversion_error;
390
391 mutex_lock(&indio_dev->mlock);
392 err = st_sensors_set_odr(indio_dev, odr);
393 mutex_unlock(&indio_dev->mlock);
394
395conversion_error:
396 return err < 0 ? err : size;
397}
398EXPORT_SYMBOL(st_sensors_sysfs_set_sampling_frequency);
399
400ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
401 struct device_attribute *attr, char *buf)
402{
403 int i, len = 0;
404 struct iio_dev *indio_dev = dev_get_drvdata(dev);
405 struct st_sensor_data *sdata = iio_priv(indio_dev);
406
407 mutex_lock(&indio_dev->mlock);
408 for (i = 0; i < ST_SENSORS_ODR_LIST_MAX; i++) {
409 if (sdata->sensor->odr.odr_avl[i].hz == 0)
410 break;
411
412 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
413 sdata->sensor->odr.odr_avl[i].hz);
414 }
415 mutex_unlock(&indio_dev->mlock);
416 buf[len - 1] = '\n';
417
418 return len;
419}
420EXPORT_SYMBOL(st_sensors_sysfs_sampling_frequency_avail);
421
422ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
423 struct device_attribute *attr, char *buf)
424{
425 int i, len = 0;
426 struct iio_dev *indio_dev = dev_get_drvdata(dev);
427 struct st_sensor_data *sdata = iio_priv(indio_dev);
428
429 mutex_lock(&indio_dev->mlock);
430 for (i = 0; i < ST_SENSORS_FULLSCALE_AVL_MAX; i++) {
431 if (sdata->sensor->fs.fs_avl[i].num == 0)
432 break;
433
434 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ",
435 sdata->sensor->fs.fs_avl[i].gain);
436 }
437 mutex_unlock(&indio_dev->mlock);
438 buf[len - 1] = '\n';
439
440 return len;
441}
442EXPORT_SYMBOL(st_sensors_sysfs_scale_avail);
443
444MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
445MODULE_DESCRIPTION("STMicroelectronics ST-sensors core");
446MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/common/st_sensors/st_sensors_i2c.c b/drivers/iio/common/st_sensors/st_sensors_i2c.c
new file mode 100644
index 000000000000..38af9440c103
--- /dev/null
+++ b/drivers/iio/common/st_sensors/st_sensors_i2c.c
@@ -0,0 +1,81 @@
1/*
2 * STMicroelectronics sensors i2c library 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/iio/iio.h>
15
16#include <linux/iio/common/st_sensors_i2c.h>
17
18
19#define ST_SENSORS_I2C_MULTIREAD 0x80
20
21static unsigned int st_sensors_i2c_get_irq(struct iio_dev *indio_dev)
22{
23 struct st_sensor_data *sdata = iio_priv(indio_dev);
24
25 return to_i2c_client(sdata->dev)->irq;
26}
27
28static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer *tb,
29 struct device *dev, u8 reg_addr, u8 *res_byte)
30{
31 int err;
32
33 err = i2c_smbus_read_byte_data(to_i2c_client(dev), reg_addr);
34 if (err < 0)
35 goto st_accel_i2c_read_byte_error;
36
37 *res_byte = err & 0xff;
38
39st_accel_i2c_read_byte_error:
40 return err < 0 ? err : 0;
41}
42
43static int st_sensors_i2c_read_multiple_byte(
44 struct st_sensor_transfer_buffer *tb, struct device *dev,
45 u8 reg_addr, int len, u8 *data, bool multiread_bit)
46{
47 if (multiread_bit)
48 reg_addr |= ST_SENSORS_I2C_MULTIREAD;
49
50 return i2c_smbus_read_i2c_block_data(to_i2c_client(dev),
51 reg_addr, len, data);
52}
53
54static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer *tb,
55 struct device *dev, u8 reg_addr, u8 data)
56{
57 return i2c_smbus_write_byte_data(to_i2c_client(dev), reg_addr, data);
58}
59
60static const struct st_sensor_transfer_function st_sensors_tf_i2c = {
61 .read_byte = st_sensors_i2c_read_byte,
62 .write_byte = st_sensors_i2c_write_byte,
63 .read_multiple_byte = st_sensors_i2c_read_multiple_byte,
64};
65
66void st_sensors_i2c_configure(struct iio_dev *indio_dev,
67 struct i2c_client *client, struct st_sensor_data *sdata)
68{
69 i2c_set_clientdata(client, indio_dev);
70
71 indio_dev->dev.parent = &client->dev;
72 indio_dev->name = client->name;
73
74 sdata->tf = &st_sensors_tf_i2c;
75 sdata->get_irq_data_ready = st_sensors_i2c_get_irq;
76}
77EXPORT_SYMBOL(st_sensors_i2c_configure);
78
79MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
80MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
81MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/common/st_sensors/st_sensors_spi.c b/drivers/iio/common/st_sensors/st_sensors_spi.c
new file mode 100644
index 000000000000..f0aa2f105222
--- /dev/null
+++ b/drivers/iio/common/st_sensors/st_sensors_spi.c
@@ -0,0 +1,128 @@
1/*
2 * STMicroelectronics sensors spi library 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/iio/iio.h>
15
16#include <linux/iio/common/st_sensors_spi.h>
17
18
19#define ST_SENSORS_SPI_MULTIREAD 0xc0
20#define ST_SENSORS_SPI_READ 0x80
21
22static unsigned int st_sensors_spi_get_irq(struct iio_dev *indio_dev)
23{
24 struct st_sensor_data *sdata = iio_priv(indio_dev);
25
26 return to_spi_device(sdata->dev)->irq;
27}
28
29static int st_sensors_spi_read(struct st_sensor_transfer_buffer *tb,
30 struct device *dev, u8 reg_addr, int len, u8 *data, bool multiread_bit)
31{
32 struct spi_message msg;
33 int err;
34
35 struct spi_transfer xfers[] = {
36 {
37 .tx_buf = tb->tx_buf,
38 .bits_per_word = 8,
39 .len = 1,
40 },
41 {
42 .rx_buf = tb->rx_buf,
43 .bits_per_word = 8,
44 .len = len,
45 }
46 };
47
48 mutex_lock(&tb->buf_lock);
49 if ((multiread_bit) && (len > 1))
50 tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_MULTIREAD;
51 else
52 tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_READ;
53
54 spi_message_init(&msg);
55 spi_message_add_tail(&xfers[0], &msg);
56 spi_message_add_tail(&xfers[1], &msg);
57 err = spi_sync(to_spi_device(dev), &msg);
58 if (err)
59 goto acc_spi_read_error;
60
61 memcpy(data, tb->rx_buf, len*sizeof(u8));
62 mutex_unlock(&tb->buf_lock);
63 return len;
64
65acc_spi_read_error:
66 mutex_unlock(&tb->buf_lock);
67 return err;
68}
69
70static int st_sensors_spi_read_byte(struct st_sensor_transfer_buffer *tb,
71 struct device *dev, u8 reg_addr, u8 *res_byte)
72{
73 return st_sensors_spi_read(tb, dev, reg_addr, 1, res_byte, false);
74}
75
76static int st_sensors_spi_read_multiple_byte(
77 struct st_sensor_transfer_buffer *tb, struct device *dev,
78 u8 reg_addr, int len, u8 *data, bool multiread_bit)
79{
80 return st_sensors_spi_read(tb, dev, reg_addr, len, data, multiread_bit);
81}
82
83static int st_sensors_spi_write_byte(struct st_sensor_transfer_buffer *tb,
84 struct device *dev, u8 reg_addr, u8 data)
85{
86 struct spi_message msg;
87 int err;
88
89 struct spi_transfer xfers = {
90 .tx_buf = tb->tx_buf,
91 .bits_per_word = 8,
92 .len = 2,
93 };
94
95 mutex_lock(&tb->buf_lock);
96 tb->tx_buf[0] = reg_addr;
97 tb->tx_buf[1] = data;
98
99 spi_message_init(&msg);
100 spi_message_add_tail(&xfers, &msg);
101 err = spi_sync(to_spi_device(dev), &msg);
102 mutex_unlock(&tb->buf_lock);
103
104 return err;
105}
106
107static const struct st_sensor_transfer_function st_sensors_tf_spi = {
108 .read_byte = st_sensors_spi_read_byte,
109 .write_byte = st_sensors_spi_write_byte,
110 .read_multiple_byte = st_sensors_spi_read_multiple_byte,
111};
112
113void st_sensors_spi_configure(struct iio_dev *indio_dev,
114 struct spi_device *spi, struct st_sensor_data *sdata)
115{
116 spi_set_drvdata(spi, indio_dev);
117
118 indio_dev->dev.parent = &spi->dev;
119 indio_dev->name = spi->modalias;
120
121 sdata->tf = &st_sensors_tf_spi;
122 sdata->get_irq_data_ready = st_sensors_spi_get_irq;
123}
124EXPORT_SYMBOL(st_sensors_spi_configure);
125
126MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
127MODULE_DESCRIPTION("STMicroelectronics ST-sensors spi driver");
128MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/common/st_sensors/st_sensors_trigger.c b/drivers/iio/common/st_sensors/st_sensors_trigger.c
new file mode 100644
index 000000000000..139ed030abb0
--- /dev/null
+++ b/drivers/iio/common/st_sensors/st_sensors_trigger.c
@@ -0,0 +1,77 @@
1/*
2 * STMicroelectronics sensors trigger library 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/iio/iio.h>
15#include <linux/iio/trigger.h>
16#include <linux/interrupt.h>
17
18#include <linux/iio/common/st_sensors.h>
19
20
21int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
22 const struct iio_trigger_ops *trigger_ops)
23{
24 int err;
25 struct st_sensor_data *sdata = iio_priv(indio_dev);
26
27 sdata->trig = iio_trigger_alloc("%s-trigger", indio_dev->name);
28 if (sdata->trig == NULL) {
29 err = -ENOMEM;
30 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
31 goto iio_trigger_alloc_error;
32 }
33
34 err = request_threaded_irq(sdata->get_irq_data_ready(indio_dev),
35 iio_trigger_generic_data_rdy_poll,
36 NULL,
37 IRQF_TRIGGER_RISING,
38 sdata->trig->name,
39 sdata->trig);
40 if (err)
41 goto request_irq_error;
42
43 sdata->trig->private_data = indio_dev;
44 sdata->trig->ops = trigger_ops;
45 sdata->trig->dev.parent = sdata->dev;
46
47 err = iio_trigger_register(sdata->trig);
48 if (err < 0) {
49 dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
50 goto iio_trigger_register_error;
51 }
52 indio_dev->trig = sdata->trig;
53
54 return 0;
55
56iio_trigger_register_error:
57 free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
58request_irq_error:
59 iio_trigger_free(sdata->trig);
60iio_trigger_alloc_error:
61 return err;
62}
63EXPORT_SYMBOL(st_sensors_allocate_trigger);
64
65void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
66{
67 struct st_sensor_data *sdata = iio_priv(indio_dev);
68
69 iio_trigger_unregister(sdata->trig);
70 free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
71 iio_trigger_free(sdata->trig);
72}
73EXPORT_SYMBOL(st_sensors_deallocate_trigger);
74
75MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
76MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
77MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/dac/ad5360.c b/drivers/iio/dac/ad5360.c
index 54b46fd3aede..92771217f665 100644
--- a/drivers/iio/dac/ad5360.c
+++ b/drivers/iio/dac/ad5360.c
@@ -213,7 +213,6 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
213 unsigned int addr) 213 unsigned int addr)
214{ 214{
215 struct ad5360_state *st = iio_priv(indio_dev); 215 struct ad5360_state *st = iio_priv(indio_dev);
216 struct spi_message m;
217 int ret; 216 int ret;
218 struct spi_transfer t[] = { 217 struct spi_transfer t[] = {
219 { 218 {
@@ -226,10 +225,6 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
226 }, 225 },
227 }; 226 };
228 227
229 spi_message_init(&m);
230 spi_message_add_tail(&t[0], &m);
231 spi_message_add_tail(&t[1], &m);
232
233 mutex_lock(&indio_dev->mlock); 228 mutex_lock(&indio_dev->mlock);
234 229
235 st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) | 230 st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
@@ -237,7 +232,7 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
237 AD5360_READBACK_TYPE(type) | 232 AD5360_READBACK_TYPE(type) |
238 AD5360_READBACK_ADDR(addr)); 233 AD5360_READBACK_ADDR(addr));
239 234
240 ret = spi_sync(st->spi, &m); 235 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
241 if (ret >= 0) 236 if (ret >= 0)
242 ret = be32_to_cpu(st->data[1].d32) & 0xffff; 237 ret = be32_to_cpu(st->data[1].d32) & 0xffff;
243 238
diff --git a/drivers/iio/dac/ad5421.c b/drivers/iio/dac/ad5421.c
index 43be948db83e..6b86a638dad0 100644
--- a/drivers/iio/dac/ad5421.c
+++ b/drivers/iio/dac/ad5421.c
@@ -127,7 +127,6 @@ static int ad5421_write(struct iio_dev *indio_dev, unsigned int reg,
127static int ad5421_read(struct iio_dev *indio_dev, unsigned int reg) 127static int ad5421_read(struct iio_dev *indio_dev, unsigned int reg)
128{ 128{
129 struct ad5421_state *st = iio_priv(indio_dev); 129 struct ad5421_state *st = iio_priv(indio_dev);
130 struct spi_message m;
131 int ret; 130 int ret;
132 struct spi_transfer t[] = { 131 struct spi_transfer t[] = {
133 { 132 {
@@ -140,15 +139,11 @@ static int ad5421_read(struct iio_dev *indio_dev, unsigned int reg)
140 }, 139 },
141 }; 140 };
142 141
143 spi_message_init(&m);
144 spi_message_add_tail(&t[0], &m);
145 spi_message_add_tail(&t[1], &m);
146
147 mutex_lock(&indio_dev->mlock); 142 mutex_lock(&indio_dev->mlock);
148 143
149 st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16)); 144 st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16));
150 145
151 ret = spi_sync(st->spi, &m); 146 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
152 if (ret >= 0) 147 if (ret >= 0)
153 ret = be32_to_cpu(st->data[1].d32) & 0xffff; 148 ret = be32_to_cpu(st->data[1].d32) & 0xffff;
154 149
diff --git a/drivers/iio/dac/ad5504.c b/drivers/iio/dac/ad5504.c
index 0661829f2773..e5e59749f109 100644
--- a/drivers/iio/dac/ad5504.c
+++ b/drivers/iio/dac/ad5504.c
@@ -85,11 +85,7 @@ static int ad5504_spi_read(struct spi_device *spi, u8 addr)
85 .rx_buf = &val, 85 .rx_buf = &val,
86 .len = 2, 86 .len = 2,
87 }; 87 };
88 struct spi_message m; 88 ret = spi_sync_transfer(spi, &t, 1);
89
90 spi_message_init(&m);
91 spi_message_add_tail(&t, &m);
92 ret = spi_sync(spi, &m);
93 89
94 if (ret < 0) 90 if (ret < 0)
95 return ret; 91 return ret;
diff --git a/drivers/iio/dac/ad5686.c b/drivers/iio/dac/ad5686.c
index ca9609d7a15c..5e554af21703 100644
--- a/drivers/iio/dac/ad5686.c
+++ b/drivers/iio/dac/ad5686.c
@@ -117,18 +117,13 @@ static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
117 .len = 3, 117 .len = 3,
118 }, 118 },
119 }; 119 };
120 struct spi_message m;
121 int ret; 120 int ret;
122 121
123 spi_message_init(&m);
124 spi_message_add_tail(&t[0], &m);
125 spi_message_add_tail(&t[1], &m);
126
127 st->data[0].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_READBACK_ENABLE) | 122 st->data[0].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_READBACK_ENABLE) |
128 AD5686_ADDR(addr)); 123 AD5686_ADDR(addr));
129 st->data[1].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP)); 124 st->data[1].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP));
130 125
131 ret = spi_sync(st->spi, &m); 126 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
132 if (ret < 0) 127 if (ret < 0)
133 return ret; 128 return ret;
134 129
diff --git a/drivers/iio/dac/ad5755.c b/drivers/iio/dac/ad5755.c
index 0869bbd27d30..71faabc6b14e 100644
--- a/drivers/iio/dac/ad5755.c
+++ b/drivers/iio/dac/ad5755.c
@@ -153,7 +153,6 @@ static int ad5755_write_ctrl(struct iio_dev *indio_dev, unsigned int channel,
153static int ad5755_read(struct iio_dev *indio_dev, unsigned int addr) 153static int ad5755_read(struct iio_dev *indio_dev, unsigned int addr)
154{ 154{
155 struct ad5755_state *st = iio_priv(indio_dev); 155 struct ad5755_state *st = iio_priv(indio_dev);
156 struct spi_message m;
157 int ret; 156 int ret;
158 struct spi_transfer t[] = { 157 struct spi_transfer t[] = {
159 { 158 {
@@ -167,16 +166,12 @@ static int ad5755_read(struct iio_dev *indio_dev, unsigned int addr)
167 }, 166 },
168 }; 167 };
169 168
170 spi_message_init(&m);
171 spi_message_add_tail(&t[0], &m);
172 spi_message_add_tail(&t[1], &m);
173
174 mutex_lock(&indio_dev->mlock); 169 mutex_lock(&indio_dev->mlock);
175 170
176 st->data[0].d32 = cpu_to_be32(AD5755_READ_FLAG | (addr << 16)); 171 st->data[0].d32 = cpu_to_be32(AD5755_READ_FLAG | (addr << 16));
177 st->data[1].d32 = cpu_to_be32(AD5755_NOOP); 172 st->data[1].d32 = cpu_to_be32(AD5755_NOOP);
178 173
179 ret = spi_sync(st->spi, &m); 174 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
180 if (ret >= 0) 175 if (ret >= 0)
181 ret = be32_to_cpu(st->data[1].d32) & 0xffff; 176 ret = be32_to_cpu(st->data[1].d32) & 0xffff;
182 177
diff --git a/drivers/iio/dac/ad5764.c b/drivers/iio/dac/ad5764.c
index 7f9045e6daa4..5b7acd3a2c77 100644
--- a/drivers/iio/dac/ad5764.c
+++ b/drivers/iio/dac/ad5764.c
@@ -135,7 +135,6 @@ static int ad5764_read(struct iio_dev *indio_dev, unsigned int reg,
135 unsigned int *val) 135 unsigned int *val)
136{ 136{
137 struct ad5764_state *st = iio_priv(indio_dev); 137 struct ad5764_state *st = iio_priv(indio_dev);
138 struct spi_message m;
139 int ret; 138 int ret;
140 struct spi_transfer t[] = { 139 struct spi_transfer t[] = {
141 { 140 {
@@ -148,15 +147,11 @@ static int ad5764_read(struct iio_dev *indio_dev, unsigned int reg,
148 }, 147 },
149 }; 148 };
150 149
151 spi_message_init(&m);
152 spi_message_add_tail(&t[0], &m);
153 spi_message_add_tail(&t[1], &m);
154
155 mutex_lock(&indio_dev->mlock); 150 mutex_lock(&indio_dev->mlock);
156 151
157 st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16)); 152 st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16));
158 153
159 ret = spi_sync(st->spi, &m); 154 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
160 if (ret >= 0) 155 if (ret >= 0)
161 *val = be32_to_cpu(st->data[1].d32) & 0xffff; 156 *val = be32_to_cpu(st->data[1].d32) & 0xffff;
162 157
diff --git a/drivers/iio/dac/ad5791.c b/drivers/iio/dac/ad5791.c
index 6407b5407ddd..8dfd3da8a07b 100644
--- a/drivers/iio/dac/ad5791.c
+++ b/drivers/iio/dac/ad5791.c
@@ -125,7 +125,6 @@ static int ad5791_spi_read(struct spi_device *spi, u8 addr, u32 *val)
125 u8 d8[4]; 125 u8 d8[4];
126 } data[3]; 126 } data[3];
127 int ret; 127 int ret;
128 struct spi_message msg;
129 struct spi_transfer xfers[] = { 128 struct spi_transfer xfers[] = {
130 { 129 {
131 .tx_buf = &data[0].d8[1], 130 .tx_buf = &data[0].d8[1],
@@ -144,10 +143,7 @@ static int ad5791_spi_read(struct spi_device *spi, u8 addr, u32 *val)
144 AD5791_ADDR(addr)); 143 AD5791_ADDR(addr));
145 data[1].d32 = cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP)); 144 data[1].d32 = cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP));
146 145
147 spi_message_init(&msg); 146 ret = spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers));
148 spi_message_add_tail(&xfers[0], &msg);
149 spi_message_add_tail(&xfers[1], &msg);
150 ret = spi_sync(spi, &msg);
151 147
152 *val = be32_to_cpu(data[2].d32); 148 *val = be32_to_cpu(data[2].d32);
153 149
diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c
index 80307473e3a9..1ea132e239ea 100644
--- a/drivers/iio/frequency/ad9523.c
+++ b/drivers/iio/frequency/ad9523.c
@@ -287,7 +287,6 @@ struct ad9523_state {
287static int ad9523_read(struct iio_dev *indio_dev, unsigned addr) 287static int ad9523_read(struct iio_dev *indio_dev, unsigned addr)
288{ 288{
289 struct ad9523_state *st = iio_priv(indio_dev); 289 struct ad9523_state *st = iio_priv(indio_dev);
290 struct spi_message m;
291 int ret; 290 int ret;
292 291
293 /* We encode the register size 1..3 bytes into the register address. 292 /* We encode the register size 1..3 bytes into the register address.
@@ -305,15 +304,11 @@ static int ad9523_read(struct iio_dev *indio_dev, unsigned addr)
305 }, 304 },
306 }; 305 };
307 306
308 spi_message_init(&m);
309 spi_message_add_tail(&t[0], &m);
310 spi_message_add_tail(&t[1], &m);
311
312 st->data[0].d32 = cpu_to_be32(AD9523_READ | 307 st->data[0].d32 = cpu_to_be32(AD9523_READ |
313 AD9523_CNT(AD9523_TRANSF_LEN(addr)) | 308 AD9523_CNT(AD9523_TRANSF_LEN(addr)) |
314 AD9523_ADDR(addr)); 309 AD9523_ADDR(addr));
315 310
316 ret = spi_sync(st->spi, &m); 311 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
317 if (ret < 0) 312 if (ret < 0)
318 dev_err(&indio_dev->dev, "read failed (%d)", ret); 313 dev_err(&indio_dev->dev, "read failed (%d)", ret);
319 else 314 else
@@ -326,7 +321,6 @@ static int ad9523_read(struct iio_dev *indio_dev, unsigned addr)
326static int ad9523_write(struct iio_dev *indio_dev, unsigned addr, unsigned val) 321static int ad9523_write(struct iio_dev *indio_dev, unsigned addr, unsigned val)
327{ 322{
328 struct ad9523_state *st = iio_priv(indio_dev); 323 struct ad9523_state *st = iio_priv(indio_dev);
329 struct spi_message m;
330 int ret; 324 int ret;
331 struct spi_transfer t[] = { 325 struct spi_transfer t[] = {
332 { 326 {
@@ -338,16 +332,12 @@ static int ad9523_write(struct iio_dev *indio_dev, unsigned addr, unsigned val)
338 }, 332 },
339 }; 333 };
340 334
341 spi_message_init(&m);
342 spi_message_add_tail(&t[0], &m);
343 spi_message_add_tail(&t[1], &m);
344
345 st->data[0].d32 = cpu_to_be32(AD9523_WRITE | 335 st->data[0].d32 = cpu_to_be32(AD9523_WRITE |
346 AD9523_CNT(AD9523_TRANSF_LEN(addr)) | 336 AD9523_CNT(AD9523_TRANSF_LEN(addr)) |
347 AD9523_ADDR(addr)); 337 AD9523_ADDR(addr));
348 st->data[1].d32 = cpu_to_be32(val); 338 st->data[1].d32 = cpu_to_be32(val);
349 339
350 ret = spi_sync(st->spi, &m); 340 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
351 341
352 if (ret < 0) 342 if (ret < 0)
353 dev_err(&indio_dev->dev, "write failed (%d)", ret); 343 dev_err(&indio_dev->dev, "write failed (%d)", ret);
diff --git a/drivers/iio/gyro/Kconfig b/drivers/iio/gyro/Kconfig
index 96b68f63a902..6be4628faffe 100644
--- a/drivers/iio/gyro/Kconfig
+++ b/drivers/iio/gyro/Kconfig
@@ -3,6 +3,13 @@
3# 3#
4menu "Digital gyroscope sensors" 4menu "Digital gyroscope sensors"
5 5
6config ADIS16080
7 tristate "Analog Devices ADIS16080/100 Yaw Rate Gyroscope with SPI driver"
8 depends on SPI
9 help
10 Say yes here to build support for Analog Devices ADIS16080, ADIS16100 Yaw
11 Rate Gyroscope with SPI.
12
6config ADIS16136 13config ADIS16136
7 tristate "Analog devices ADIS16136 and similar gyroscopes driver" 14 tristate "Analog devices ADIS16136 and similar gyroscopes driver"
8 depends on SPI_MASTER 15 depends on SPI_MASTER
@@ -12,6 +19,16 @@ config ADIS16136
12 Say yes here to build support for the Analog Devices ADIS16133, ADIS16135, 19 Say yes here to build support for the Analog Devices ADIS16133, ADIS16135,
13 ADIS16136 gyroscope devices. 20 ADIS16136 gyroscope devices.
14 21
22config ADXRS450
23 tristate "Analog Devices ADXRS450/3 Digital Output Gyroscope SPI driver"
24 depends on SPI
25 help
26 Say yes here to build support for Analog Devices ADXRS450 and ADXRS453
27 programmable digital output gyroscope.
28
29 This driver can also be built as a module. If so, the module
30 will be called adxrs450.
31
15config HID_SENSOR_GYRO_3D 32config HID_SENSOR_GYRO_3D
16 depends on HID_SENSOR_HUB 33 depends on HID_SENSOR_HUB
17 select IIO_BUFFER 34 select IIO_BUFFER
@@ -23,4 +40,42 @@ config HID_SENSOR_GYRO_3D
23 Say yes here to build support for the HID SENSOR 40 Say yes here to build support for the HID SENSOR
24 Gyroscope 3D. 41 Gyroscope 3D.
25 42
43config IIO_ST_GYRO_3AXIS
44 tristate "STMicroelectronics gyroscopes 3-Axis Driver"
45 depends on (I2C || SPI_MASTER) && SYSFS
46 select IIO_ST_SENSORS_CORE
47 select IIO_ST_GYRO_I2C_3AXIS if (I2C)
48 select IIO_ST_GYRO_SPI_3AXIS if (SPI_MASTER)
49 select IIO_TRIGGERED_BUFFER if (IIO_BUFFER)
50 select IIO_ST_GYRO_BUFFER if (IIO_TRIGGERED_BUFFER)
51 help
52 Say yes here to build support for STMicroelectronics gyroscopes:
53 L3G4200D, LSM330DL, L3GD20, L3GD20H, LSM330DLC, L3G4IS, LSM330.
54
55 This driver can also be built as a module. If so, will be created
56 these modules:
57 - st_gyro (core functions for the driver [it is mandatory]);
58 - st_gyro_i2c (necessary for the I2C devices [optional*]);
59 - st_gyro_spi (necessary for the SPI devices [optional*]);
60
61 (*) one of these is necessary to do something.
62
63config IIO_ST_GYRO_I2C_3AXIS
64 tristate
65 depends on IIO_ST_GYRO_3AXIS
66 depends on IIO_ST_SENSORS_I2C
67
68config IIO_ST_GYRO_SPI_3AXIS
69 tristate
70 depends on IIO_ST_GYRO_3AXIS
71 depends on IIO_ST_SENSORS_SPI
72
73config ITG3200
74 tristate "InvenSense ITG3200 Digital 3-Axis Gyroscope I2C driver"
75 depends on I2C
76 select IIO_TRIGGERED_BUFFER if IIO_BUFFER
77 help
78 Say yes here to add support for the InvenSense ITG3200 digital
79 3-axis gyroscope sensor.
80
26endmenu 81endmenu
diff --git a/drivers/iio/gyro/Makefile b/drivers/iio/gyro/Makefile
index 702a058907e3..225d289082e6 100644
--- a/drivers/iio/gyro/Makefile
+++ b/drivers/iio/gyro/Makefile
@@ -2,5 +2,19 @@
2# Makefile for industrial I/O gyroscope sensor drivers 2# Makefile for industrial I/O gyroscope sensor drivers
3# 3#
4 4
5obj-$(CONFIG_ADIS16080) += adis16080.o
5obj-$(CONFIG_ADIS16136) += adis16136.o 6obj-$(CONFIG_ADIS16136) += adis16136.o
7obj-$(CONFIG_ADXRS450) += adxrs450.o
8
6obj-$(CONFIG_HID_SENSOR_GYRO_3D) += hid-sensor-gyro-3d.o 9obj-$(CONFIG_HID_SENSOR_GYRO_3D) += hid-sensor-gyro-3d.o
10
11itg3200-y := itg3200_core.o
12itg3200-$(CONFIG_IIO_BUFFER) += itg3200_buffer.o
13obj-$(CONFIG_ITG3200) += itg3200.o
14
15obj-$(CONFIG_IIO_ST_GYRO_3AXIS) += st_gyro.o
16st_gyro-y := st_gyro_core.o
17st_gyro-$(CONFIG_IIO_BUFFER) += st_gyro_buffer.o
18
19obj-$(CONFIG_IIO_ST_GYRO_I2C_3AXIS) += st_gyro_i2c.o
20obj-$(CONFIG_IIO_ST_GYRO_SPI_3AXIS) += st_gyro_spi.o
diff --git a/drivers/iio/gyro/adis16080.c b/drivers/iio/gyro/adis16080.c
new file mode 100644
index 000000000000..1861287911f1
--- /dev/null
+++ b/drivers/iio/gyro/adis16080.c
@@ -0,0 +1,259 @@
1/*
2 * ADIS16080/100 Yaw Rate Gyroscope with SPI driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8#include <linux/delay.h>
9#include <linux/mutex.h>
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/spi/spi.h>
13#include <linux/slab.h>
14#include <linux/sysfs.h>
15#include <linux/module.h>
16
17#include <linux/iio/iio.h>
18#include <linux/iio/sysfs.h>
19
20#define ADIS16080_DIN_GYRO (0 << 10) /* Gyroscope output */
21#define ADIS16080_DIN_TEMP (1 << 10) /* Temperature output */
22#define ADIS16080_DIN_AIN1 (2 << 10)
23#define ADIS16080_DIN_AIN2 (3 << 10)
24
25/*
26 * 1: Write contents on DIN to control register.
27 * 0: No changes to control register.
28 */
29
30#define ADIS16080_DIN_WRITE (1 << 15)
31
32struct adis16080_chip_info {
33 int scale_val;
34 int scale_val2;
35};
36
37/**
38 * struct adis16080_state - device instance specific data
39 * @us: actual spi_device to write data
40 * @info: chip specific parameters
41 * @buf: transmit or receive buffer
42 **/
43struct adis16080_state {
44 struct spi_device *us;
45 const struct adis16080_chip_info *info;
46
47 __be16 buf ____cacheline_aligned;
48};
49
50static int adis16080_read_sample(struct iio_dev *indio_dev,
51 u16 addr, int *val)
52{
53 struct adis16080_state *st = iio_priv(indio_dev);
54 struct spi_message m;
55 int ret;
56 struct spi_transfer t[] = {
57 {
58 .tx_buf = &st->buf,
59 .len = 2,
60 .cs_change = 1,
61 }, {
62 .rx_buf = &st->buf,
63 .len = 2,
64 },
65 };
66
67 st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE);
68
69 spi_message_init(&m);
70 spi_message_add_tail(&t[0], &m);
71 spi_message_add_tail(&t[1], &m);
72
73 ret = spi_sync(st->us, &m);
74 if (ret == 0)
75 *val = sign_extend32(be16_to_cpu(st->buf), 11);
76
77 return ret;
78}
79
80static int adis16080_read_raw(struct iio_dev *indio_dev,
81 struct iio_chan_spec const *chan,
82 int *val,
83 int *val2,
84 long mask)
85{
86 struct adis16080_state *st = iio_priv(indio_dev);
87 int ret;
88
89 switch (mask) {
90 case IIO_CHAN_INFO_RAW:
91 mutex_lock(&indio_dev->mlock);
92 ret = adis16080_read_sample(indio_dev, chan->address, val);
93 mutex_unlock(&indio_dev->mlock);
94 return ret ? ret : IIO_VAL_INT;
95 case IIO_CHAN_INFO_SCALE:
96 switch (chan->type) {
97 case IIO_ANGL_VEL:
98 *val = st->info->scale_val;
99 *val2 = st->info->scale_val2;
100 return IIO_VAL_FRACTIONAL;
101 case IIO_VOLTAGE:
102 /* VREF = 5V, 12 bits */
103 *val = 5000;
104 *val2 = 12;
105 return IIO_VAL_FRACTIONAL_LOG2;
106 case IIO_TEMP:
107 /* 85 C = 585, 25 C = 0 */
108 *val = 85000 - 25000;
109 *val2 = 585;
110 return IIO_VAL_FRACTIONAL;
111 default:
112 return -EINVAL;
113 }
114 case IIO_CHAN_INFO_OFFSET:
115 switch (chan->type) {
116 case IIO_VOLTAGE:
117 /* 2.5 V = 0 */
118 *val = 2048;
119 return IIO_VAL_INT;
120 case IIO_TEMP:
121 /* 85 C = 585, 25 C = 0 */
122 *val = DIV_ROUND_CLOSEST(25 * 585, 85 - 25);
123 return IIO_VAL_INT;
124 default:
125 return -EINVAL;
126 }
127 default:
128 break;
129 }
130
131 return -EINVAL;
132}
133
134static const struct iio_chan_spec adis16080_channels[] = {
135 {
136 .type = IIO_ANGL_VEL,
137 .modified = 1,
138 .channel2 = IIO_MOD_Z,
139 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
140 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
141 .address = ADIS16080_DIN_GYRO,
142 }, {
143 .type = IIO_VOLTAGE,
144 .indexed = 1,
145 .channel = 0,
146 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
147 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
148 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
149 .address = ADIS16080_DIN_AIN1,
150 }, {
151 .type = IIO_VOLTAGE,
152 .indexed = 1,
153 .channel = 1,
154 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
155 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
156 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
157 .address = ADIS16080_DIN_AIN2,
158 }, {
159 .type = IIO_TEMP,
160 .indexed = 1,
161 .channel = 0,
162 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
163 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
164 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
165 .address = ADIS16080_DIN_TEMP,
166 }
167};
168
169static const struct iio_info adis16080_info = {
170 .read_raw = &adis16080_read_raw,
171 .driver_module = THIS_MODULE,
172};
173
174enum {
175 ID_ADIS16080,
176 ID_ADIS16100,
177};
178
179static const struct adis16080_chip_info adis16080_chip_info[] = {
180 [ID_ADIS16080] = {
181 /* 80 degree = 819, 819 rad = 46925 degree */
182 .scale_val = 80,
183 .scale_val2 = 46925,
184 },
185 [ID_ADIS16100] = {
186 /* 300 degree = 1230, 1230 rad = 70474 degree */
187 .scale_val = 300,
188 .scale_val2 = 70474,
189 },
190};
191
192static int adis16080_probe(struct spi_device *spi)
193{
194 const struct spi_device_id *id = spi_get_device_id(spi);
195 int ret;
196 struct adis16080_state *st;
197 struct iio_dev *indio_dev;
198
199 /* setup the industrialio driver allocated elements */
200 indio_dev = iio_device_alloc(sizeof(*st));
201 if (indio_dev == NULL) {
202 ret = -ENOMEM;
203 goto error_ret;
204 }
205 st = iio_priv(indio_dev);
206 /* this is only used for removal purposes */
207 spi_set_drvdata(spi, indio_dev);
208
209 /* Allocate the comms buffers */
210 st->us = spi;
211 st->info = &adis16080_chip_info[id->driver_data];
212
213 indio_dev->name = spi->dev.driver->name;
214 indio_dev->channels = adis16080_channels;
215 indio_dev->num_channels = ARRAY_SIZE(adis16080_channels);
216 indio_dev->dev.parent = &spi->dev;
217 indio_dev->info = &adis16080_info;
218 indio_dev->modes = INDIO_DIRECT_MODE;
219
220 ret = iio_device_register(indio_dev);
221 if (ret)
222 goto error_free_dev;
223 return 0;
224
225error_free_dev:
226 iio_device_free(indio_dev);
227error_ret:
228 return ret;
229}
230
231static int adis16080_remove(struct spi_device *spi)
232{
233 iio_device_unregister(spi_get_drvdata(spi));
234 iio_device_free(spi_get_drvdata(spi));
235
236 return 0;
237}
238
239static const struct spi_device_id adis16080_ids[] = {
240 { "adis16080", ID_ADIS16080 },
241 { "adis16100", ID_ADIS16100 },
242 {},
243};
244MODULE_DEVICE_TABLE(spi, adis16080_ids);
245
246static struct spi_driver adis16080_driver = {
247 .driver = {
248 .name = "adis16080",
249 .owner = THIS_MODULE,
250 },
251 .probe = adis16080_probe,
252 .remove = adis16080_remove,
253 .id_table = adis16080_ids,
254};
255module_spi_driver(adis16080_driver);
256
257MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
258MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver");
259MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/adxrs450.c b/drivers/iio/gyro/adxrs450.c
new file mode 100644
index 000000000000..5b79953f7011
--- /dev/null
+++ b/drivers/iio/gyro/adxrs450.c
@@ -0,0 +1,494 @@
1/*
2 * ADXRS450/ADXRS453 Digital Output Gyroscope Driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9#include <linux/interrupt.h>
10#include <linux/irq.h>
11#include <linux/delay.h>
12#include <linux/mutex.h>
13#include <linux/device.h>
14#include <linux/kernel.h>
15#include <linux/spi/spi.h>
16#include <linux/slab.h>
17#include <linux/sysfs.h>
18#include <linux/list.h>
19#include <linux/module.h>
20
21#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23
24#define ADXRS450_STARTUP_DELAY 50 /* ms */
25
26/* The MSB for the spi commands */
27#define ADXRS450_SENSOR_DATA (0x20 << 24)
28#define ADXRS450_WRITE_DATA (0x40 << 24)
29#define ADXRS450_READ_DATA (0x80 << 24)
30
31#define ADXRS450_RATE1 0x00 /* Rate Registers */
32#define ADXRS450_TEMP1 0x02 /* Temperature Registers */
33#define ADXRS450_LOCST1 0x04 /* Low CST Memory Registers */
34#define ADXRS450_HICST1 0x06 /* High CST Memory Registers */
35#define ADXRS450_QUAD1 0x08 /* Quad Memory Registers */
36#define ADXRS450_FAULT1 0x0A /* Fault Registers */
37#define ADXRS450_PID1 0x0C /* Part ID Register 1 */
38#define ADXRS450_SNH 0x0E /* Serial Number Registers, 4 bytes */
39#define ADXRS450_SNL 0x10
40#define ADXRS450_DNC1 0x12 /* Dynamic Null Correction Registers */
41/* Check bits */
42#define ADXRS450_P 0x01
43#define ADXRS450_CHK 0x02
44#define ADXRS450_CST 0x04
45#define ADXRS450_PWR 0x08
46#define ADXRS450_POR 0x10
47#define ADXRS450_NVM 0x20
48#define ADXRS450_Q 0x40
49#define ADXRS450_PLL 0x80
50#define ADXRS450_UV 0x100
51#define ADXRS450_OV 0x200
52#define ADXRS450_AMP 0x400
53#define ADXRS450_FAIL 0x800
54
55#define ADXRS450_WRERR_MASK (0x7 << 29)
56
57#define ADXRS450_MAX_RX 4
58#define ADXRS450_MAX_TX 4
59
60#define ADXRS450_GET_ST(a) ((a >> 26) & 0x3)
61
62enum {
63 ID_ADXRS450,
64 ID_ADXRS453,
65};
66
67/**
68 * struct adxrs450_state - device instance specific data
69 * @us: actual spi_device
70 * @buf_lock: mutex to protect tx and rx
71 * @tx: transmit buffer
72 * @rx: receive buffer
73 **/
74struct adxrs450_state {
75 struct spi_device *us;
76 struct mutex buf_lock;
77 __be32 tx ____cacheline_aligned;
78 __be32 rx;
79
80};
81
82/**
83 * adxrs450_spi_read_reg_16() - read 2 bytes from a register pair
84 * @indio_dev: device associated with child of actual iio_dev
85 * @reg_address: the address of the lower of the two registers, which should be
86 * an even address, the second register's address is reg_address + 1.
87 * @val: somewhere to pass back the value read
88 **/
89static int adxrs450_spi_read_reg_16(struct iio_dev *indio_dev,
90 u8 reg_address,
91 u16 *val)
92{
93 struct spi_message msg;
94 struct adxrs450_state *st = iio_priv(indio_dev);
95 u32 tx;
96 int ret;
97 struct spi_transfer xfers[] = {
98 {
99 .tx_buf = &st->tx,
100 .bits_per_word = 8,
101 .len = sizeof(st->tx),
102 .cs_change = 1,
103 }, {
104 .rx_buf = &st->rx,
105 .bits_per_word = 8,
106 .len = sizeof(st->rx),
107 },
108 };
109
110 mutex_lock(&st->buf_lock);
111 tx = ADXRS450_READ_DATA | (reg_address << 17);
112
113 if (!(hweight32(tx) & 1))
114 tx |= ADXRS450_P;
115
116 st->tx = cpu_to_be32(tx);
117 spi_message_init(&msg);
118 spi_message_add_tail(&xfers[0], &msg);
119 spi_message_add_tail(&xfers[1], &msg);
120 ret = spi_sync(st->us, &msg);
121 if (ret) {
122 dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
123 reg_address);
124 goto error_ret;
125 }
126
127 *val = (be32_to_cpu(st->rx) >> 5) & 0xFFFF;
128
129error_ret:
130 mutex_unlock(&st->buf_lock);
131 return ret;
132}
133
134/**
135 * adxrs450_spi_write_reg_16() - write 2 bytes data to a register pair
136 * @indio_dev: device associated with child of actual actual iio_dev
137 * @reg_address: the address of the lower of the two registers,which should be
138 * an even address, the second register's address is reg_address + 1.
139 * @val: value to be written.
140 **/
141static int adxrs450_spi_write_reg_16(struct iio_dev *indio_dev,
142 u8 reg_address,
143 u16 val)
144{
145 struct adxrs450_state *st = iio_priv(indio_dev);
146 u32 tx;
147 int ret;
148
149 mutex_lock(&st->buf_lock);
150 tx = ADXRS450_WRITE_DATA | (reg_address << 17) | (val << 1);
151
152 if (!(hweight32(tx) & 1))
153 tx |= ADXRS450_P;
154
155 st->tx = cpu_to_be32(tx);
156 ret = spi_write(st->us, &st->tx, sizeof(st->tx));
157 if (ret)
158 dev_err(&st->us->dev, "problem while writing 16 bit register 0x%02x\n",
159 reg_address);
160 usleep_range(100, 1000); /* enforce sequential transfer delay 0.1ms */
161 mutex_unlock(&st->buf_lock);
162 return ret;
163}
164
165/**
166 * adxrs450_spi_sensor_data() - read 2 bytes sensor data
167 * @indio_dev: device associated with child of actual iio_dev
168 * @val: somewhere to pass back the value read
169 **/
170static int adxrs450_spi_sensor_data(struct iio_dev *indio_dev, s16 *val)
171{
172 struct spi_message msg;
173 struct adxrs450_state *st = iio_priv(indio_dev);
174 int ret;
175 struct spi_transfer xfers[] = {
176 {
177 .tx_buf = &st->tx,
178 .bits_per_word = 8,
179 .len = sizeof(st->tx),
180 .cs_change = 1,
181 }, {
182 .rx_buf = &st->rx,
183 .bits_per_word = 8,
184 .len = sizeof(st->rx),
185 },
186 };
187
188 mutex_lock(&st->buf_lock);
189 st->tx = cpu_to_be32(ADXRS450_SENSOR_DATA);
190
191 spi_message_init(&msg);
192 spi_message_add_tail(&xfers[0], &msg);
193 spi_message_add_tail(&xfers[1], &msg);
194 ret = spi_sync(st->us, &msg);
195 if (ret) {
196 dev_err(&st->us->dev, "Problem while reading sensor data\n");
197 goto error_ret;
198 }
199
200 *val = (be32_to_cpu(st->rx) >> 10) & 0xFFFF;
201
202error_ret:
203 mutex_unlock(&st->buf_lock);
204 return ret;
205}
206
207/**
208 * adxrs450_spi_initial() - use for initializing procedure.
209 * @st: device instance specific data
210 * @val: somewhere to pass back the value read
211 * @chk: Whether to perform fault check
212 **/
213static int adxrs450_spi_initial(struct adxrs450_state *st,
214 u32 *val, char chk)
215{
216 int ret;
217 u32 tx;
218 struct spi_transfer xfers = {
219 .tx_buf = &st->tx,
220 .rx_buf = &st->rx,
221 .bits_per_word = 8,
222 .len = sizeof(st->tx),
223 };
224
225 mutex_lock(&st->buf_lock);
226 tx = ADXRS450_SENSOR_DATA;
227 if (chk)
228 tx |= (ADXRS450_CHK | ADXRS450_P);
229 st->tx = cpu_to_be32(tx);
230 ret = spi_sync_transfer(st->us, &xfers, 1);
231 if (ret) {
232 dev_err(&st->us->dev, "Problem while reading initializing data\n");
233 goto error_ret;
234 }
235
236 *val = be32_to_cpu(st->rx);
237
238error_ret:
239 mutex_unlock(&st->buf_lock);
240 return ret;
241}
242
243/* Recommended Startup Sequence by spec */
244static int adxrs450_initial_setup(struct iio_dev *indio_dev)
245{
246 u32 t;
247 u16 data;
248 int ret;
249 struct adxrs450_state *st = iio_priv(indio_dev);
250
251 msleep(ADXRS450_STARTUP_DELAY*2);
252 ret = adxrs450_spi_initial(st, &t, 1);
253 if (ret)
254 return ret;
255 if (t != 0x01)
256 dev_warn(&st->us->dev, "The initial power on response is not correct! Restart without reset?\n");
257
258 msleep(ADXRS450_STARTUP_DELAY);
259 ret = adxrs450_spi_initial(st, &t, 0);
260 if (ret)
261 return ret;
262
263 msleep(ADXRS450_STARTUP_DELAY);
264 ret = adxrs450_spi_initial(st, &t, 0);
265 if (ret)
266 return ret;
267 if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
268 dev_err(&st->us->dev, "The second response is not correct!\n");
269 return -EIO;
270
271 }
272 ret = adxrs450_spi_initial(st, &t, 0);
273 if (ret)
274 return ret;
275 if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
276 dev_err(&st->us->dev, "The third response is not correct!\n");
277 return -EIO;
278
279 }
280 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_FAULT1, &data);
281 if (ret)
282 return ret;
283 if (data & 0x0fff) {
284 dev_err(&st->us->dev, "The device is not in normal status!\n");
285 return -EINVAL;
286 }
287
288 return 0;
289}
290
291static int adxrs450_write_raw(struct iio_dev *indio_dev,
292 struct iio_chan_spec const *chan,
293 int val,
294 int val2,
295 long mask)
296{
297 int ret;
298 switch (mask) {
299 case IIO_CHAN_INFO_CALIBBIAS:
300 if (val < -0x400 || val >= 0x400)
301 return -EINVAL;
302 ret = adxrs450_spi_write_reg_16(indio_dev,
303 ADXRS450_DNC1, val);
304 break;
305 default:
306 ret = -EINVAL;
307 break;
308 }
309 return ret;
310}
311
312static int adxrs450_read_raw(struct iio_dev *indio_dev,
313 struct iio_chan_spec const *chan,
314 int *val,
315 int *val2,
316 long mask)
317{
318 int ret;
319 s16 t;
320
321 switch (mask) {
322 case IIO_CHAN_INFO_RAW:
323 switch (chan->type) {
324 case IIO_ANGL_VEL:
325 ret = adxrs450_spi_sensor_data(indio_dev, &t);
326 if (ret)
327 break;
328 *val = t;
329 ret = IIO_VAL_INT;
330 break;
331 case IIO_TEMP:
332 ret = adxrs450_spi_read_reg_16(indio_dev,
333 ADXRS450_TEMP1, &t);
334 if (ret)
335 break;
336 *val = (t >> 6) + 225;
337 ret = IIO_VAL_INT;
338 break;
339 default:
340 ret = -EINVAL;
341 break;
342 }
343 break;
344 case IIO_CHAN_INFO_SCALE:
345 switch (chan->type) {
346 case IIO_ANGL_VEL:
347 *val = 0;
348 *val2 = 218166;
349 return IIO_VAL_INT_PLUS_NANO;
350 case IIO_TEMP:
351 *val = 200;
352 *val2 = 0;
353 return IIO_VAL_INT;
354 default:
355 return -EINVAL;
356 }
357 break;
358 case IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW:
359 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_QUAD1, &t);
360 if (ret)
361 break;
362 *val = t;
363 ret = IIO_VAL_INT;
364 break;
365 case IIO_CHAN_INFO_CALIBBIAS:
366 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_DNC1, &t);
367 if (ret)
368 break;
369 *val = sign_extend32(t, 9);
370 ret = IIO_VAL_INT;
371 break;
372 default:
373 ret = -EINVAL;
374 break;
375 }
376
377 return ret;
378}
379
380static const struct iio_chan_spec adxrs450_channels[2][2] = {
381 [ID_ADXRS450] = {
382 {
383 .type = IIO_ANGL_VEL,
384 .modified = 1,
385 .channel2 = IIO_MOD_Z,
386 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
387 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
388 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE_BIT |
389 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
390 }, {
391 .type = IIO_TEMP,
392 .indexed = 1,
393 .channel = 0,
394 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
395 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
396 }
397 },
398 [ID_ADXRS453] = {
399 {
400 .type = IIO_ANGL_VEL,
401 .modified = 1,
402 .channel2 = IIO_MOD_Z,
403 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
404 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
405 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE_BIT,
406 }, {
407 .type = IIO_TEMP,
408 .indexed = 1,
409 .channel = 0,
410 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
411 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
412 }
413 },
414};
415
416static const struct iio_info adxrs450_info = {
417 .driver_module = THIS_MODULE,
418 .read_raw = &adxrs450_read_raw,
419 .write_raw = &adxrs450_write_raw,
420};
421
422static int adxrs450_probe(struct spi_device *spi)
423{
424 int ret;
425 struct adxrs450_state *st;
426 struct iio_dev *indio_dev;
427
428 /* setup the industrialio driver allocated elements */
429 indio_dev = iio_device_alloc(sizeof(*st));
430 if (indio_dev == NULL) {
431 ret = -ENOMEM;
432 goto error_ret;
433 }
434 st = iio_priv(indio_dev);
435 st->us = spi;
436 mutex_init(&st->buf_lock);
437 /* This is only used for removal purposes */
438 spi_set_drvdata(spi, indio_dev);
439
440 indio_dev->dev.parent = &spi->dev;
441 indio_dev->info = &adxrs450_info;
442 indio_dev->modes = INDIO_DIRECT_MODE;
443 indio_dev->channels =
444 adxrs450_channels[spi_get_device_id(spi)->driver_data];
445 indio_dev->num_channels = ARRAY_SIZE(adxrs450_channels);
446 indio_dev->name = spi->dev.driver->name;
447
448 ret = iio_device_register(indio_dev);
449 if (ret)
450 goto error_free_dev;
451
452 /* Get the device into a sane initial state */
453 ret = adxrs450_initial_setup(indio_dev);
454 if (ret)
455 goto error_initial;
456 return 0;
457error_initial:
458 iio_device_unregister(indio_dev);
459error_free_dev:
460 iio_device_free(indio_dev);
461
462error_ret:
463 return ret;
464}
465
466static int adxrs450_remove(struct spi_device *spi)
467{
468 iio_device_unregister(spi_get_drvdata(spi));
469 iio_device_free(spi_get_drvdata(spi));
470
471 return 0;
472}
473
474static const struct spi_device_id adxrs450_id[] = {
475 {"adxrs450", ID_ADXRS450},
476 {"adxrs453", ID_ADXRS453},
477 {}
478};
479MODULE_DEVICE_TABLE(spi, adxrs450_id);
480
481static struct spi_driver adxrs450_driver = {
482 .driver = {
483 .name = "adxrs450",
484 .owner = THIS_MODULE,
485 },
486 .probe = adxrs450_probe,
487 .remove = adxrs450_remove,
488 .id_table = adxrs450_id,
489};
490module_spi_driver(adxrs450_driver);
491
492MODULE_AUTHOR("Cliff Cai <cliff.cai@xxxxxxxxxx>");
493MODULE_DESCRIPTION("Analog Devices ADXRS450/ADXRS453 Gyroscope SPI driver");
494MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/hid-sensor-gyro-3d.c b/drivers/iio/gyro/hid-sensor-gyro-3d.c
index 06e7cc35450c..fcfc83a9f861 100644
--- a/drivers/iio/gyro/hid-sensor-gyro-3d.c
+++ b/drivers/iio/gyro/hid-sensor-gyro-3d.c
@@ -28,7 +28,6 @@
28#include <linux/iio/buffer.h> 28#include <linux/iio/buffer.h>
29#include <linux/iio/trigger_consumer.h> 29#include <linux/iio/trigger_consumer.h>
30#include <linux/iio/triggered_buffer.h> 30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-attributes.h"
32#include "../common/hid-sensors/hid-sensor-trigger.h" 31#include "../common/hid-sensors/hid-sensor-trigger.h"
33 32
34/*Format: HID-SENSOR-usage_id_in_hex*/ 33/*Format: HID-SENSOR-usage_id_in_hex*/
@@ -44,7 +43,7 @@ enum gyro_3d_channel {
44 43
45struct gyro_3d_state { 44struct gyro_3d_state {
46 struct hid_sensor_hub_callbacks callbacks; 45 struct hid_sensor_hub_callbacks callbacks;
47 struct hid_sensor_iio_common common_attributes; 46 struct hid_sensor_common common_attributes;
48 struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX]; 47 struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
49 u32 gyro_val[GYRO_3D_CHANNEL_MAX]; 48 u32 gyro_val[GYRO_3D_CHANNEL_MAX];
50}; 49};
diff --git a/drivers/iio/gyro/itg3200_buffer.c b/drivers/iio/gyro/itg3200_buffer.c
new file mode 100644
index 000000000000..f667d2c8c00f
--- /dev/null
+++ b/drivers/iio/gyro/itg3200_buffer.c
@@ -0,0 +1,156 @@
1/*
2 * itg3200_buffer.c -- support InvenSense ITG3200
3 * Digital 3-Axis Gyroscope driver
4 *
5 * Copyright (c) 2011 Christian Strobel <christian.strobel@iis.fraunhofer.de>
6 * Copyright (c) 2011 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
7 * Copyright (c) 2012 Thorsten Nowak <thorsten.nowak@iis.fraunhofer.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/slab.h>
15#include <linux/i2c.h>
16#include <linux/interrupt.h>
17
18#include <linux/iio/iio.h>
19#include <linux/iio/buffer.h>
20#include <linux/iio/trigger.h>
21#include <linux/iio/trigger_consumer.h>
22#include <linux/iio/triggered_buffer.h>
23#include <linux/iio/gyro/itg3200.h>
24
25
26static int itg3200_read_all_channels(struct i2c_client *i2c, __be16 *buf)
27{
28 u8 tx = 0x80 | ITG3200_REG_TEMP_OUT_H;
29 struct i2c_msg msg[2] = {
30 {
31 .addr = i2c->addr,
32 .flags = i2c->flags,
33 .len = 1,
34 .buf = &tx,
35 },
36 {
37 .addr = i2c->addr,
38 .flags = i2c->flags | I2C_M_RD,
39 .len = ITG3200_SCAN_ELEMENTS * sizeof(s16),
40 .buf = (char *)&buf,
41 },
42 };
43
44 return i2c_transfer(i2c->adapter, msg, 2);
45}
46
47static irqreturn_t itg3200_trigger_handler(int irq, void *p)
48{
49 struct iio_poll_func *pf = p;
50 struct iio_dev *indio_dev = pf->indio_dev;
51 struct itg3200 *st = iio_priv(indio_dev);
52 __be16 buf[ITG3200_SCAN_ELEMENTS + sizeof(s64)/sizeof(u16)];
53
54 int ret = itg3200_read_all_channels(st->i2c, buf);
55 if (ret < 0)
56 goto error_ret;
57
58 if (indio_dev->scan_timestamp)
59 memcpy(buf + indio_dev->scan_bytes - sizeof(s64),
60 &pf->timestamp, sizeof(pf->timestamp));
61
62 iio_push_to_buffers(indio_dev, (u8 *)buf);
63 iio_trigger_notify_done(indio_dev->trig);
64
65error_ret:
66 return IRQ_HANDLED;
67}
68
69int itg3200_buffer_configure(struct iio_dev *indio_dev)
70{
71 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
72 itg3200_trigger_handler, NULL);
73}
74
75void itg3200_buffer_unconfigure(struct iio_dev *indio_dev)
76{
77 iio_triggered_buffer_cleanup(indio_dev);
78}
79
80
81static int itg3200_data_rdy_trigger_set_state(struct iio_trigger *trig,
82 bool state)
83{
84 struct iio_dev *indio_dev = trig->private_data;
85 int ret;
86 u8 msc;
87
88 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, &msc);
89 if (ret)
90 goto error_ret;
91
92 if (state)
93 msc |= ITG3200_IRQ_DATA_RDY_ENABLE;
94 else
95 msc &= ~ITG3200_IRQ_DATA_RDY_ENABLE;
96
97 ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, msc);
98 if (ret)
99 goto error_ret;
100
101error_ret:
102 return ret;
103
104}
105
106static const struct iio_trigger_ops itg3200_trigger_ops = {
107 .owner = THIS_MODULE,
108 .set_trigger_state = &itg3200_data_rdy_trigger_set_state,
109};
110
111int itg3200_probe_trigger(struct iio_dev *indio_dev)
112{
113 int ret;
114 struct itg3200 *st = iio_priv(indio_dev);
115
116 st->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
117 indio_dev->id);
118 if (!st->trig)
119 return -ENOMEM;
120
121 ret = request_irq(st->i2c->irq,
122 &iio_trigger_generic_data_rdy_poll,
123 IRQF_TRIGGER_RISING,
124 "itg3200_data_rdy",
125 st->trig);
126 if (ret)
127 goto error_free_trig;
128
129
130 st->trig->dev.parent = &st->i2c->dev;
131 st->trig->ops = &itg3200_trigger_ops;
132 st->trig->private_data = indio_dev;
133 ret = iio_trigger_register(st->trig);
134 if (ret)
135 goto error_free_irq;
136
137 /* select default trigger */
138 indio_dev->trig = st->trig;
139
140 return 0;
141
142error_free_irq:
143 free_irq(st->i2c->irq, st->trig);
144error_free_trig:
145 iio_trigger_free(st->trig);
146 return ret;
147}
148
149void itg3200_remove_trigger(struct iio_dev *indio_dev)
150{
151 struct itg3200 *st = iio_priv(indio_dev);
152
153 iio_trigger_unregister(st->trig);
154 free_irq(st->i2c->irq, st->trig);
155 iio_trigger_free(st->trig);
156}
diff --git a/drivers/iio/gyro/itg3200_core.c b/drivers/iio/gyro/itg3200_core.c
new file mode 100644
index 000000000000..df2e6aa5d73b
--- /dev/null
+++ b/drivers/iio/gyro/itg3200_core.c
@@ -0,0 +1,401 @@
1/*
2 * itg3200_core.c -- support InvenSense ITG3200
3 * Digital 3-Axis Gyroscope driver
4 *
5 * Copyright (c) 2011 Christian Strobel <christian.strobel@iis.fraunhofer.de>
6 * Copyright (c) 2011 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
7 * Copyright (c) 2012 Thorsten Nowak <thorsten.nowak@iis.fraunhofer.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * TODO:
14 * - Support digital low pass filter
15 * - Support power management
16 */
17
18#include <linux/interrupt.h>
19#include <linux/irq.h>
20#include <linux/i2c.h>
21#include <linux/gpio.h>
22#include <linux/slab.h>
23#include <linux/stat.h>
24#include <linux/module.h>
25#include <linux/delay.h>
26
27#include <linux/iio/iio.h>
28#include <linux/iio/sysfs.h>
29#include <linux/iio/events.h>
30#include <linux/iio/buffer.h>
31
32#include <linux/iio/gyro/itg3200.h>
33
34
35int itg3200_write_reg_8(struct iio_dev *indio_dev,
36 u8 reg_address, u8 val)
37{
38 struct itg3200 *st = iio_priv(indio_dev);
39
40 return i2c_smbus_write_byte_data(st->i2c, 0x80 | reg_address, val);
41}
42
43int itg3200_read_reg_8(struct iio_dev *indio_dev,
44 u8 reg_address, u8 *val)
45{
46 struct itg3200 *st = iio_priv(indio_dev);
47 int ret;
48
49 ret = i2c_smbus_read_byte_data(st->i2c, reg_address);
50 if (ret < 0)
51 return ret;
52 *val = ret;
53 return 0;
54}
55
56static int itg3200_read_reg_s16(struct iio_dev *indio_dev, u8 lower_reg_address,
57 int *val)
58{
59 struct itg3200 *st = iio_priv(indio_dev);
60 struct i2c_client *client = st->i2c;
61 int ret;
62 s16 out;
63
64 struct i2c_msg msg[2] = {
65 {
66 .addr = client->addr,
67 .flags = client->flags,
68 .len = 1,
69 .buf = (char *)&lower_reg_address,
70 },
71 {
72 .addr = client->addr,
73 .flags = client->flags | I2C_M_RD,
74 .len = 2,
75 .buf = (char *)&out,
76 },
77 };
78
79 lower_reg_address |= 0x80;
80 ret = i2c_transfer(client->adapter, msg, 2);
81 be16_to_cpus(&out);
82 *val = out;
83
84 return (ret == 2) ? 0 : ret;
85}
86
87static int itg3200_read_raw(struct iio_dev *indio_dev,
88 const struct iio_chan_spec *chan,
89 int *val, int *val2, long info)
90{
91 int ret = 0;
92 u8 reg;
93
94 switch (info) {
95 case IIO_CHAN_INFO_RAW:
96 reg = (u8)chan->address;
97 ret = itg3200_read_reg_s16(indio_dev, reg, val);
98 return IIO_VAL_INT;
99 case IIO_CHAN_INFO_SCALE:
100 *val = 0;
101 if (chan->type == IIO_TEMP)
102 *val2 = 1000000000/280;
103 else
104 *val2 = 1214142; /* (1 / 14,375) * (PI / 180) */
105 return IIO_VAL_INT_PLUS_NANO;
106 case IIO_CHAN_INFO_OFFSET:
107 /* Only the temperature channel has an offset */
108 *val = 23000;
109 return IIO_VAL_INT;
110 default:
111 return -EINVAL;
112 }
113
114 return ret;
115}
116
117static ssize_t itg3200_read_frequency(struct device *dev,
118 struct device_attribute *attr, char *buf)
119{
120 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
121 int ret, sps;
122 u8 val;
123
124 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
125 if (ret)
126 return ret;
127
128 sps = (val & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
129
130 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_SAMPLE_RATE_DIV, &val);
131 if (ret)
132 return ret;
133
134 sps /= val + 1;
135
136 return sprintf(buf, "%d\n", sps);
137}
138
139static ssize_t itg3200_write_frequency(struct device *dev,
140 struct device_attribute *attr,
141 const char *buf,
142 size_t len)
143{
144 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
145 unsigned val;
146 int ret;
147 u8 t;
148
149 ret = kstrtouint(buf, 10, &val);
150 if (ret)
151 return ret;
152
153 mutex_lock(&indio_dev->mlock);
154
155 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
156 if (ret)
157 goto err_ret;
158
159 if (val == 0) {
160 ret = -EINVAL;
161 goto err_ret;
162 }
163 t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
164
165 ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_SAMPLE_RATE_DIV, t);
166
167err_ret:
168 mutex_unlock(&indio_dev->mlock);
169
170 return ret ? ret : len;
171}
172
173/*
174 * Reset device and internal registers to the power-up-default settings
175 * Use the gyro clock as reference, as suggested by the datasheet
176 */
177static int itg3200_reset(struct iio_dev *indio_dev)
178{
179 struct itg3200 *st = iio_priv(indio_dev);
180 int ret;
181
182 dev_dbg(&st->i2c->dev, "reset device");
183
184 ret = itg3200_write_reg_8(indio_dev,
185 ITG3200_REG_POWER_MANAGEMENT,
186 ITG3200_RESET);
187 if (ret) {
188 dev_err(&st->i2c->dev, "error resetting device");
189 goto error_ret;
190 }
191
192 /* Wait for PLL (1ms according to datasheet) */
193 udelay(1500);
194
195 ret = itg3200_write_reg_8(indio_dev,
196 ITG3200_REG_IRQ_CONFIG,
197 ITG3200_IRQ_ACTIVE_HIGH |
198 ITG3200_IRQ_PUSH_PULL |
199 ITG3200_IRQ_LATCH_50US_PULSE |
200 ITG3200_IRQ_LATCH_CLEAR_ANY);
201
202 if (ret)
203 dev_err(&st->i2c->dev, "error init device");
204
205error_ret:
206 return ret;
207}
208
209/* itg3200_enable_full_scale() - Disables the digital low pass filter */
210static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
211{
212 u8 val;
213 int ret;
214
215 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
216 if (ret)
217 goto err_ret;
218
219 val |= ITG3200_DLPF_FS_SEL_2000;
220 return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
221
222err_ret:
223 return ret;
224}
225
226static int itg3200_initial_setup(struct iio_dev *indio_dev)
227{
228 struct itg3200 *st = iio_priv(indio_dev);
229 int ret;
230 u8 val;
231
232 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
233 if (ret)
234 goto err_ret;
235
236 if (((val >> 1) & 0x3f) != 0x34) {
237 dev_err(&st->i2c->dev, "invalid reg value 0x%02x", val);
238 ret = -ENXIO;
239 goto err_ret;
240 }
241
242 ret = itg3200_reset(indio_dev);
243 if (ret)
244 goto err_ret;
245
246 ret = itg3200_enable_full_scale(indio_dev);
247err_ret:
248 return ret;
249}
250
251#define ITG3200_TEMP_INFO_MASK (IIO_CHAN_INFO_OFFSET_SHARED_BIT | \
252 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
253 IIO_CHAN_INFO_RAW_SEPARATE_BIT)
254#define ITG3200_GYRO_INFO_MASK (IIO_CHAN_INFO_SCALE_SHARED_BIT | \
255 IIO_CHAN_INFO_RAW_SEPARATE_BIT)
256
257#define ITG3200_ST \
258 { .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
259
260#define ITG3200_GYRO_CHAN(_mod) { \
261 .type = IIO_ANGL_VEL, \
262 .modified = 1, \
263 .channel2 = IIO_MOD_ ## _mod, \
264 .info_mask = ITG3200_GYRO_INFO_MASK, \
265 .address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
266 .scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
267 .scan_type = ITG3200_ST, \
268}
269
270static const struct iio_chan_spec itg3200_channels[] = {
271 {
272 .type = IIO_TEMP,
273 .channel2 = IIO_NO_MOD,
274 .info_mask = ITG3200_TEMP_INFO_MASK,
275 .address = ITG3200_REG_TEMP_OUT_H,
276 .scan_index = ITG3200_SCAN_TEMP,
277 .scan_type = ITG3200_ST,
278 },
279 ITG3200_GYRO_CHAN(X),
280 ITG3200_GYRO_CHAN(Y),
281 ITG3200_GYRO_CHAN(Z),
282 IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
283};
284
285/* IIO device attributes */
286static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, itg3200_read_frequency,
287 itg3200_write_frequency);
288
289static struct attribute *itg3200_attributes[] = {
290 &iio_dev_attr_sampling_frequency.dev_attr.attr,
291 NULL
292};
293
294static const struct attribute_group itg3200_attribute_group = {
295 .attrs = itg3200_attributes,
296};
297
298static const struct iio_info itg3200_info = {
299 .attrs = &itg3200_attribute_group,
300 .read_raw = &itg3200_read_raw,
301 .driver_module = THIS_MODULE,
302};
303
304static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
305
306static int itg3200_probe(struct i2c_client *client,
307 const struct i2c_device_id *id)
308{
309 int ret;
310 struct itg3200 *st;
311 struct iio_dev *indio_dev;
312
313 dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
314
315 indio_dev = iio_device_alloc(sizeof(*st));
316 if (indio_dev == NULL) {
317 ret = -ENOMEM;
318 goto error_ret;
319 }
320
321 st = iio_priv(indio_dev);
322
323 i2c_set_clientdata(client, indio_dev);
324 st->i2c = client;
325
326 indio_dev->dev.parent = &client->dev;
327 indio_dev->name = client->dev.driver->name;
328 indio_dev->channels = itg3200_channels;
329 indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
330 indio_dev->available_scan_masks = itg3200_available_scan_masks;
331 indio_dev->info = &itg3200_info;
332 indio_dev->modes = INDIO_DIRECT_MODE;
333
334 ret = itg3200_buffer_configure(indio_dev);
335 if (ret)
336 goto error_free_dev;
337
338 if (client->irq) {
339 ret = itg3200_probe_trigger(indio_dev);
340 if (ret)
341 goto error_unconfigure_buffer;
342 }
343
344 ret = itg3200_initial_setup(indio_dev);
345 if (ret)
346 goto error_remove_trigger;
347
348 ret = iio_device_register(indio_dev);
349 if (ret)
350 goto error_remove_trigger;
351
352 return 0;
353
354error_remove_trigger:
355 if (client->irq)
356 itg3200_remove_trigger(indio_dev);
357error_unconfigure_buffer:
358 itg3200_buffer_unconfigure(indio_dev);
359error_free_dev:
360 iio_device_free(indio_dev);
361error_ret:
362 return ret;
363}
364
365static int itg3200_remove(struct i2c_client *client)
366{
367 struct iio_dev *indio_dev = i2c_get_clientdata(client);
368
369 iio_device_unregister(indio_dev);
370
371 if (client->irq)
372 itg3200_remove_trigger(indio_dev);
373
374 itg3200_buffer_unconfigure(indio_dev);
375
376 iio_device_free(indio_dev);
377
378 return 0;
379}
380
381static const struct i2c_device_id itg3200_id[] = {
382 { "itg3200", 0 },
383 { }
384};
385MODULE_DEVICE_TABLE(i2c, itg3200_id);
386
387static struct i2c_driver itg3200_driver = {
388 .driver = {
389 .owner = THIS_MODULE,
390 .name = "itg3200",
391 },
392 .id_table = itg3200_id,
393 .probe = itg3200_probe,
394 .remove = itg3200_remove,
395};
396
397module_i2c_driver(itg3200_driver);
398
399MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
400MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
401MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/st_gyro.h b/drivers/iio/gyro/st_gyro.h
new file mode 100644
index 000000000000..3ad9907bb154
--- /dev/null
+++ b/drivers/iio/gyro/st_gyro.h
@@ -0,0 +1,45 @@
1/*
2 * STMicroelectronics gyroscopes 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_GYRO_H
12#define ST_GYRO_H
13
14#include <linux/types.h>
15#include <linux/iio/common/st_sensors.h>
16
17#define L3G4200D_GYRO_DEV_NAME "l3g4200d"
18#define LSM330D_GYRO_DEV_NAME "lsm330d_gyro"
19#define LSM330DL_GYRO_DEV_NAME "lsm330dl_gyro"
20#define LSM330DLC_GYRO_DEV_NAME "lsm330dlc_gyro"
21#define L3GD20_GYRO_DEV_NAME "l3gd20"
22#define L3GD20H_GYRO_DEV_NAME "l3gd20h"
23#define L3G4IS_GYRO_DEV_NAME "l3g4is_ui"
24#define LSM330_GYRO_DEV_NAME "lsm330_gyro"
25
26int st_gyro_common_probe(struct iio_dev *indio_dev);
27void st_gyro_common_remove(struct iio_dev *indio_dev);
28
29#ifdef CONFIG_IIO_BUFFER
30int st_gyro_allocate_ring(struct iio_dev *indio_dev);
31void st_gyro_deallocate_ring(struct iio_dev *indio_dev);
32int st_gyro_trig_set_state(struct iio_trigger *trig, bool state);
33#define ST_GYRO_TRIGGER_SET_STATE (&st_gyro_trig_set_state)
34#else /* CONFIG_IIO_BUFFER */
35static inline int st_gyro_allocate_ring(struct iio_dev *indio_dev)
36{
37 return 0;
38}
39static inline void st_gyro_deallocate_ring(struct iio_dev *indio_dev)
40{
41}
42#define ST_GYRO_TRIGGER_SET_STATE NULL
43#endif /* CONFIG_IIO_BUFFER */
44
45#endif /* ST_GYRO_H */
diff --git a/drivers/iio/gyro/st_gyro_buffer.c b/drivers/iio/gyro/st_gyro_buffer.c
new file mode 100644
index 000000000000..da4d122ec7dc
--- /dev/null
+++ b/drivers/iio/gyro/st_gyro_buffer.c
@@ -0,0 +1,114 @@
1/*
2 * STMicroelectronics gyroscopes 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_gyro.h"
25
26int st_gyro_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_gyro_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_gyro_set_enable_error;
40
41 err = iio_sw_buffer_preenable(indio_dev);
42
43st_gyro_set_enable_error:
44 return err;
45}
46
47static int st_gyro_buffer_postenable(struct iio_dev *indio_dev)
48{
49 int err;
50 struct st_sensor_data *gdata = iio_priv(indio_dev);
51
52 gdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
53 if (gdata->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_gyro_buffer_postenable_error;
62
63 err = iio_triggered_buffer_postenable(indio_dev);
64 if (err < 0)
65 goto st_gyro_buffer_postenable_error;
66
67 return err;
68
69st_gyro_buffer_postenable_error:
70 kfree(gdata->buffer_data);
71allocate_memory_error:
72 return err;
73}
74
75static int st_gyro_buffer_predisable(struct iio_dev *indio_dev)
76{
77 int err;
78 struct st_sensor_data *gdata = iio_priv(indio_dev);
79
80 err = iio_triggered_buffer_predisable(indio_dev);
81 if (err < 0)
82 goto st_gyro_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_gyro_buffer_predisable_error;
87
88 err = st_sensors_set_enable(indio_dev, false);
89
90st_gyro_buffer_predisable_error:
91 kfree(gdata->buffer_data);
92 return err;
93}
94
95static const struct iio_buffer_setup_ops st_gyro_buffer_setup_ops = {
96 .preenable = &st_gyro_buffer_preenable,
97 .postenable = &st_gyro_buffer_postenable,
98 .predisable = &st_gyro_buffer_predisable,
99};
100
101int st_gyro_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_gyro_buffer_setup_ops);
105}
106
107void st_gyro_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 gyroscopes buffer");
114MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c
new file mode 100644
index 000000000000..fa9b24219987
--- /dev/null
+++ b/drivers/iio/gyro/st_gyro_core.c
@@ -0,0 +1,368 @@
1/*
2 * STMicroelectronics gyroscopes 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/delay.h>
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/trigger.h>
25#include <linux/iio/buffer.h>
26
27#include <linux/iio/common/st_sensors.h>
28#include "st_gyro.h"
29
30/* DEFAULT VALUE FOR SENSORS */
31#define ST_GYRO_DEFAULT_OUT_X_L_ADDR 0x28
32#define ST_GYRO_DEFAULT_OUT_Y_L_ADDR 0x2a
33#define ST_GYRO_DEFAULT_OUT_Z_L_ADDR 0x2c
34
35/* FULLSCALE */
36#define ST_GYRO_FS_AVL_250DPS 250
37#define ST_GYRO_FS_AVL_500DPS 500
38#define ST_GYRO_FS_AVL_2000DPS 2000
39
40/* CUSTOM VALUES FOR SENSOR 1 */
41#define ST_GYRO_1_WAI_EXP 0xd3
42#define ST_GYRO_1_ODR_ADDR 0x20
43#define ST_GYRO_1_ODR_MASK 0xc0
44#define ST_GYRO_1_ODR_AVL_100HZ_VAL 0x00
45#define ST_GYRO_1_ODR_AVL_200HZ_VAL 0x01
46#define ST_GYRO_1_ODR_AVL_400HZ_VAL 0x02
47#define ST_GYRO_1_ODR_AVL_800HZ_VAL 0x03
48#define ST_GYRO_1_PW_ADDR 0x20
49#define ST_GYRO_1_PW_MASK 0x08
50#define ST_GYRO_1_FS_ADDR 0x23
51#define ST_GYRO_1_FS_MASK 0x30
52#define ST_GYRO_1_FS_AVL_250_VAL 0x00
53#define ST_GYRO_1_FS_AVL_500_VAL 0x01
54#define ST_GYRO_1_FS_AVL_2000_VAL 0x02
55#define ST_GYRO_1_FS_AVL_250_GAIN IIO_DEGREE_TO_RAD(8750)
56#define ST_GYRO_1_FS_AVL_500_GAIN IIO_DEGREE_TO_RAD(17500)
57#define ST_GYRO_1_FS_AVL_2000_GAIN IIO_DEGREE_TO_RAD(70000)
58#define ST_GYRO_1_BDU_ADDR 0x23
59#define ST_GYRO_1_BDU_MASK 0x80
60#define ST_GYRO_1_DRDY_IRQ_ADDR 0x22
61#define ST_GYRO_1_DRDY_IRQ_MASK 0x08
62#define ST_GYRO_1_MULTIREAD_BIT true
63
64/* CUSTOM VALUES FOR SENSOR 2 */
65#define ST_GYRO_2_WAI_EXP 0xd4
66#define ST_GYRO_2_ODR_ADDR 0x20
67#define ST_GYRO_2_ODR_MASK 0xc0
68#define ST_GYRO_2_ODR_AVL_95HZ_VAL 0x00
69#define ST_GYRO_2_ODR_AVL_190HZ_VAL 0x01
70#define ST_GYRO_2_ODR_AVL_380HZ_VAL 0x02
71#define ST_GYRO_2_ODR_AVL_760HZ_VAL 0x03
72#define ST_GYRO_2_PW_ADDR 0x20
73#define ST_GYRO_2_PW_MASK 0x08
74#define ST_GYRO_2_FS_ADDR 0x23
75#define ST_GYRO_2_FS_MASK 0x30
76#define ST_GYRO_2_FS_AVL_250_VAL 0x00
77#define ST_GYRO_2_FS_AVL_500_VAL 0x01
78#define ST_GYRO_2_FS_AVL_2000_VAL 0x02
79#define ST_GYRO_2_FS_AVL_250_GAIN IIO_DEGREE_TO_RAD(8750)
80#define ST_GYRO_2_FS_AVL_500_GAIN IIO_DEGREE_TO_RAD(17500)
81#define ST_GYRO_2_FS_AVL_2000_GAIN IIO_DEGREE_TO_RAD(70000)
82#define ST_GYRO_2_BDU_ADDR 0x23
83#define ST_GYRO_2_BDU_MASK 0x80
84#define ST_GYRO_2_DRDY_IRQ_ADDR 0x22
85#define ST_GYRO_2_DRDY_IRQ_MASK 0x08
86#define ST_GYRO_2_MULTIREAD_BIT true
87
88static const struct iio_chan_spec st_gyro_16bit_channels[] = {
89 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL, ST_SENSORS_SCAN_X,
90 IIO_MOD_X, IIO_LE, ST_SENSORS_DEFAULT_16_REALBITS,
91 ST_GYRO_DEFAULT_OUT_X_L_ADDR),
92 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL, ST_SENSORS_SCAN_Y,
93 IIO_MOD_Y, IIO_LE, ST_SENSORS_DEFAULT_16_REALBITS,
94 ST_GYRO_DEFAULT_OUT_Y_L_ADDR),
95 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL, ST_SENSORS_SCAN_Z,
96 IIO_MOD_Z, IIO_LE, ST_SENSORS_DEFAULT_16_REALBITS,
97 ST_GYRO_DEFAULT_OUT_Z_L_ADDR),
98 IIO_CHAN_SOFT_TIMESTAMP(3)
99};
100
101static const struct st_sensors st_gyro_sensors[] = {
102 {
103 .wai = ST_GYRO_1_WAI_EXP,
104 .sensors_supported = {
105 [0] = L3G4200D_GYRO_DEV_NAME,
106 [1] = LSM330DL_GYRO_DEV_NAME,
107 },
108 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
109 .odr = {
110 .addr = ST_GYRO_1_ODR_ADDR,
111 .mask = ST_GYRO_1_ODR_MASK,
112 .odr_avl = {
113 { 100, ST_GYRO_1_ODR_AVL_100HZ_VAL, },
114 { 200, ST_GYRO_1_ODR_AVL_200HZ_VAL, },
115 { 400, ST_GYRO_1_ODR_AVL_400HZ_VAL, },
116 { 800, ST_GYRO_1_ODR_AVL_800HZ_VAL, },
117 },
118 },
119 .pw = {
120 .addr = ST_GYRO_1_PW_ADDR,
121 .mask = ST_GYRO_1_PW_MASK,
122 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
123 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
124 },
125 .enable_axis = {
126 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
127 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
128 },
129 .fs = {
130 .addr = ST_GYRO_1_FS_ADDR,
131 .mask = ST_GYRO_1_FS_MASK,
132 .fs_avl = {
133 [0] = {
134 .num = ST_GYRO_FS_AVL_250DPS,
135 .value = ST_GYRO_1_FS_AVL_250_VAL,
136 .gain = ST_GYRO_1_FS_AVL_250_GAIN,
137 },
138 [1] = {
139 .num = ST_GYRO_FS_AVL_500DPS,
140 .value = ST_GYRO_1_FS_AVL_500_VAL,
141 .gain = ST_GYRO_1_FS_AVL_500_GAIN,
142 },
143 [2] = {
144 .num = ST_GYRO_FS_AVL_2000DPS,
145 .value = ST_GYRO_1_FS_AVL_2000_VAL,
146 .gain = ST_GYRO_1_FS_AVL_2000_GAIN,
147 },
148 },
149 },
150 .bdu = {
151 .addr = ST_GYRO_1_BDU_ADDR,
152 .mask = ST_GYRO_1_BDU_MASK,
153 },
154 .drdy_irq = {
155 .addr = ST_GYRO_1_DRDY_IRQ_ADDR,
156 .mask = ST_GYRO_1_DRDY_IRQ_MASK,
157 },
158 .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT,
159 .bootime = 2,
160 },
161 {
162 .wai = ST_GYRO_2_WAI_EXP,
163 .sensors_supported = {
164 [0] = L3GD20_GYRO_DEV_NAME,
165 [1] = L3GD20H_GYRO_DEV_NAME,
166 [2] = LSM330D_GYRO_DEV_NAME,
167 [3] = LSM330DLC_GYRO_DEV_NAME,
168 [4] = L3G4IS_GYRO_DEV_NAME,
169 [5] = LSM330_GYRO_DEV_NAME,
170 },
171 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
172 .odr = {
173 .addr = ST_GYRO_2_ODR_ADDR,
174 .mask = ST_GYRO_2_ODR_MASK,
175 .odr_avl = {
176 { 95, ST_GYRO_2_ODR_AVL_95HZ_VAL, },
177 { 190, ST_GYRO_2_ODR_AVL_190HZ_VAL, },
178 { 380, ST_GYRO_2_ODR_AVL_380HZ_VAL, },
179 { 760, ST_GYRO_2_ODR_AVL_760HZ_VAL, },
180 },
181 },
182 .pw = {
183 .addr = ST_GYRO_2_PW_ADDR,
184 .mask = ST_GYRO_2_PW_MASK,
185 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
186 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
187 },
188 .enable_axis = {
189 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
190 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
191 },
192 .fs = {
193 .addr = ST_GYRO_2_FS_ADDR,
194 .mask = ST_GYRO_2_FS_MASK,
195 .fs_avl = {
196 [0] = {
197 .num = ST_GYRO_FS_AVL_250DPS,
198 .value = ST_GYRO_2_FS_AVL_250_VAL,
199 .gain = ST_GYRO_2_FS_AVL_250_GAIN,
200 },
201 [1] = {
202 .num = ST_GYRO_FS_AVL_500DPS,
203 .value = ST_GYRO_2_FS_AVL_500_VAL,
204 .gain = ST_GYRO_2_FS_AVL_500_GAIN,
205 },
206 [2] = {
207 .num = ST_GYRO_FS_AVL_2000DPS,
208 .value = ST_GYRO_2_FS_AVL_2000_VAL,
209 .gain = ST_GYRO_2_FS_AVL_2000_GAIN,
210 },
211 },
212 },
213 .bdu = {
214 .addr = ST_GYRO_2_BDU_ADDR,
215 .mask = ST_GYRO_2_BDU_MASK,
216 },
217 .drdy_irq = {
218 .addr = ST_GYRO_2_DRDY_IRQ_ADDR,
219 .mask = ST_GYRO_2_DRDY_IRQ_MASK,
220 },
221 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT,
222 .bootime = 2,
223 },
224};
225
226static int st_gyro_read_raw(struct iio_dev *indio_dev,
227 struct iio_chan_spec const *ch, int *val,
228 int *val2, long mask)
229{
230 int err;
231 struct st_sensor_data *gdata = iio_priv(indio_dev);
232
233 switch (mask) {
234 case IIO_CHAN_INFO_RAW:
235 err = st_sensors_read_info_raw(indio_dev, ch, val);
236 if (err < 0)
237 goto read_error;
238
239 return IIO_VAL_INT;
240 case IIO_CHAN_INFO_SCALE:
241 *val = 0;
242 *val2 = gdata->current_fullscale->gain;
243 return IIO_VAL_INT_PLUS_MICRO;
244 default:
245 return -EINVAL;
246 }
247
248read_error:
249 return err;
250}
251
252static int st_gyro_write_raw(struct iio_dev *indio_dev,
253 struct iio_chan_spec const *chan, int val, int val2, long mask)
254{
255 int err;
256
257 switch (mask) {
258 case IIO_CHAN_INFO_SCALE:
259 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
260 break;
261 default:
262 err = -EINVAL;
263 }
264
265 return err;
266}
267
268static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
269static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
270static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
271
272static struct attribute *st_gyro_attributes[] = {
273 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
274 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
275 &iio_dev_attr_sampling_frequency.dev_attr.attr,
276 NULL,
277};
278
279static const struct attribute_group st_gyro_attribute_group = {
280 .attrs = st_gyro_attributes,
281};
282
283static const struct iio_info gyro_info = {
284 .driver_module = THIS_MODULE,
285 .attrs = &st_gyro_attribute_group,
286 .read_raw = &st_gyro_read_raw,
287 .write_raw = &st_gyro_write_raw,
288};
289
290#ifdef CONFIG_IIO_TRIGGER
291static const struct iio_trigger_ops st_gyro_trigger_ops = {
292 .owner = THIS_MODULE,
293 .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
294};
295#define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
296#else
297#define ST_GYRO_TRIGGER_OPS NULL
298#endif
299
300int st_gyro_common_probe(struct iio_dev *indio_dev)
301{
302 int err;
303 struct st_sensor_data *gdata = iio_priv(indio_dev);
304
305 indio_dev->modes = INDIO_DIRECT_MODE;
306 indio_dev->info = &gyro_info;
307
308 err = st_sensors_check_device_support(indio_dev,
309 ARRAY_SIZE(st_gyro_sensors), st_gyro_sensors);
310 if (err < 0)
311 goto st_gyro_common_probe_error;
312
313 gdata->multiread_bit = gdata->sensor->multi_read_bit;
314 indio_dev->channels = gdata->sensor->ch;
315 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
316
317 gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
318 &gdata->sensor->fs.fs_avl[0];
319 gdata->odr = gdata->sensor->odr.odr_avl[0].hz;
320
321 err = st_sensors_init_sensor(indio_dev);
322 if (err < 0)
323 goto st_gyro_common_probe_error;
324
325 if (gdata->get_irq_data_ready(indio_dev) > 0) {
326 err = st_gyro_allocate_ring(indio_dev);
327 if (err < 0)
328 goto st_gyro_common_probe_error;
329
330 err = st_sensors_allocate_trigger(indio_dev,
331 ST_GYRO_TRIGGER_OPS);
332 if (err < 0)
333 goto st_gyro_probe_trigger_error;
334 }
335
336 err = iio_device_register(indio_dev);
337 if (err)
338 goto st_gyro_device_register_error;
339
340 return err;
341
342st_gyro_device_register_error:
343 if (gdata->get_irq_data_ready(indio_dev) > 0)
344 st_sensors_deallocate_trigger(indio_dev);
345st_gyro_probe_trigger_error:
346 if (gdata->get_irq_data_ready(indio_dev) > 0)
347 st_gyro_deallocate_ring(indio_dev);
348st_gyro_common_probe_error:
349 return err;
350}
351EXPORT_SYMBOL(st_gyro_common_probe);
352
353void st_gyro_common_remove(struct iio_dev *indio_dev)
354{
355 struct st_sensor_data *gdata = iio_priv(indio_dev);
356
357 iio_device_unregister(indio_dev);
358 if (gdata->get_irq_data_ready(indio_dev) > 0) {
359 st_sensors_deallocate_trigger(indio_dev);
360 st_gyro_deallocate_ring(indio_dev);
361 }
362 iio_device_free(indio_dev);
363}
364EXPORT_SYMBOL(st_gyro_common_remove);
365
366MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
367MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
368MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/st_gyro_i2c.c b/drivers/iio/gyro/st_gyro_i2c.c
new file mode 100644
index 000000000000..8a310500573d
--- /dev/null
+++ b/drivers/iio/gyro/st_gyro_i2c.c
@@ -0,0 +1,84 @@
1/*
2 * STMicroelectronics gyroscopes 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
17#include <linux/iio/common/st_sensors.h>
18#include <linux/iio/common/st_sensors_i2c.h>
19#include "st_gyro.h"
20
21static int st_gyro_i2c_probe(struct i2c_client *client,
22 const struct i2c_device_id *id)
23{
24 struct iio_dev *indio_dev;
25 struct st_sensor_data *gdata;
26 int err;
27
28 indio_dev = iio_device_alloc(sizeof(*gdata));
29 if (indio_dev == NULL) {
30 err = -ENOMEM;
31 goto iio_device_alloc_error;
32 }
33
34 gdata = iio_priv(indio_dev);
35 gdata->dev = &client->dev;
36
37 st_sensors_i2c_configure(indio_dev, client, gdata);
38
39 err = st_gyro_common_probe(indio_dev);
40 if (err < 0)
41 goto st_gyro_common_probe_error;
42
43 return 0;
44
45st_gyro_common_probe_error:
46 iio_device_free(indio_dev);
47iio_device_alloc_error:
48 return err;
49}
50
51static int st_gyro_i2c_remove(struct i2c_client *client)
52{
53 st_gyro_common_remove(i2c_get_clientdata(client));
54
55 return 0;
56}
57
58static const struct i2c_device_id st_gyro_id_table[] = {
59 { L3G4200D_GYRO_DEV_NAME },
60 { LSM330D_GYRO_DEV_NAME },
61 { LSM330DL_GYRO_DEV_NAME },
62 { LSM330DLC_GYRO_DEV_NAME },
63 { L3GD20_GYRO_DEV_NAME },
64 { L3GD20H_GYRO_DEV_NAME },
65 { L3G4IS_GYRO_DEV_NAME },
66 { LSM330_GYRO_DEV_NAME },
67 {},
68};
69MODULE_DEVICE_TABLE(i2c, st_gyro_id_table);
70
71static struct i2c_driver st_gyro_driver = {
72 .driver = {
73 .owner = THIS_MODULE,
74 .name = "st-gyro-i2c",
75 },
76 .probe = st_gyro_i2c_probe,
77 .remove = st_gyro_i2c_remove,
78 .id_table = st_gyro_id_table,
79};
80module_i2c_driver(st_gyro_driver);
81
82MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
83MODULE_DESCRIPTION("STMicroelectronics gyroscopes i2c driver");
84MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/st_gyro_spi.c b/drivers/iio/gyro/st_gyro_spi.c
new file mode 100644
index 000000000000..f3540390eb22
--- /dev/null
+++ b/drivers/iio/gyro/st_gyro_spi.c
@@ -0,0 +1,83 @@
1/*
2 * STMicroelectronics gyroscopes 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
17#include <linux/iio/common/st_sensors.h>
18#include <linux/iio/common/st_sensors_spi.h>
19#include "st_gyro.h"
20
21static int st_gyro_spi_probe(struct spi_device *spi)
22{
23 struct iio_dev *indio_dev;
24 struct st_sensor_data *gdata;
25 int err;
26
27 indio_dev = iio_device_alloc(sizeof(*gdata));
28 if (indio_dev == NULL) {
29 err = -ENOMEM;
30 goto iio_device_alloc_error;
31 }
32
33 gdata = iio_priv(indio_dev);
34 gdata->dev = &spi->dev;
35
36 st_sensors_spi_configure(indio_dev, spi, gdata);
37
38 err = st_gyro_common_probe(indio_dev);
39 if (err < 0)
40 goto st_gyro_common_probe_error;
41
42 return 0;
43
44st_gyro_common_probe_error:
45 iio_device_free(indio_dev);
46iio_device_alloc_error:
47 return err;
48}
49
50static int st_gyro_spi_remove(struct spi_device *spi)
51{
52 st_gyro_common_remove(spi_get_drvdata(spi));
53
54 return 0;
55}
56
57static const struct spi_device_id st_gyro_id_table[] = {
58 { L3G4200D_GYRO_DEV_NAME },
59 { LSM330D_GYRO_DEV_NAME },
60 { LSM330DL_GYRO_DEV_NAME },
61 { LSM330DLC_GYRO_DEV_NAME },
62 { L3GD20_GYRO_DEV_NAME },
63 { L3GD20H_GYRO_DEV_NAME },
64 { L3G4IS_GYRO_DEV_NAME },
65 { LSM330_GYRO_DEV_NAME },
66 {},
67};
68MODULE_DEVICE_TABLE(spi, st_gyro_id_table);
69
70static struct spi_driver st_gyro_driver = {
71 .driver = {
72 .owner = THIS_MODULE,
73 .name = "st-gyro-spi",
74 },
75 .probe = st_gyro_spi_probe,
76 .remove = st_gyro_spi_remove,
77 .id_table = st_gyro_id_table,
78};
79module_spi_driver(st_gyro_driver);
80
81MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
82MODULE_DESCRIPTION("STMicroelectronics gyroscopes spi driver");
83MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/imu/Kconfig b/drivers/iio/imu/Kconfig
index 3d79a40e916b..4f40a10cb74f 100644
--- a/drivers/iio/imu/Kconfig
+++ b/drivers/iio/imu/Kconfig
@@ -3,6 +3,17 @@
3# 3#
4menu "Inertial measurement units" 4menu "Inertial measurement units"
5 5
6config ADIS16400
7 tristate "Analog Devices ADIS16400 and similar IMU SPI driver"
8 depends on SPI
9 select IIO_ADIS_LIB
10 select IIO_ADIS_LIB_BUFFER if IIO_BUFFER
11 help
12 Say yes here to build support for Analog Devices adis16300, adis16344,
13 adis16350, adis16354, adis16355, adis16360, adis16362, adis16364,
14 adis16365, adis16400 and adis16405 triaxial inertial sensors
15 (adis16400 series also have magnetometers).
16
6config ADIS16480 17config ADIS16480
7 tristate "Analog Devices ADIS16480 and similar IMU driver" 18 tristate "Analog Devices ADIS16480 and similar IMU driver"
8 depends on SPI 19 depends on SPI
@@ -25,3 +36,5 @@ config IIO_ADIS_LIB_BUFFER
25 help 36 help
26 A set of buffer helper functions for the Analog Devices ADIS* device 37 A set of buffer helper functions for the Analog Devices ADIS* device
27 family. 38 family.
39
40source "drivers/iio/imu/inv_mpu6050/Kconfig"
diff --git a/drivers/iio/imu/Makefile b/drivers/iio/imu/Makefile
index cfe57638f6f9..f2f56ceaed26 100644
--- a/drivers/iio/imu/Makefile
+++ b/drivers/iio/imu/Makefile
@@ -2,9 +2,14 @@
2# Makefile for Inertial Measurement Units 2# Makefile for Inertial Measurement Units
3# 3#
4 4
5adis16400-y := adis16400_core.o
6adis16400-$(CONFIG_IIO_BUFFER) += adis16400_buffer.o
7obj-$(CONFIG_ADIS16400) += adis16400.o
5obj-$(CONFIG_ADIS16480) += adis16480.o 8obj-$(CONFIG_ADIS16480) += adis16480.o
6 9
7adis_lib-y += adis.o 10adis_lib-y += adis.o
8adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_trigger.o 11adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_trigger.o
9adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o 12adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
10obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o 13obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
14
15obj-y += inv_mpu6050/
diff --git a/drivers/iio/imu/adis16400.h b/drivers/iio/imu/adis16400.h
new file mode 100644
index 000000000000..2f8f9d632386
--- /dev/null
+++ b/drivers/iio/imu/adis16400.h
@@ -0,0 +1,212 @@
1/*
2 * adis16400.h support Analog Devices ADIS16400
3 * 3d 18g accelerometers,
4 * 3d gyroscopes,
5 * 3d 2.5gauss magnetometers via SPI
6 *
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
9 *
10 * Loosely based upon lis3l02dq.h
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#ifndef SPI_ADIS16400_H_
18#define SPI_ADIS16400_H_
19
20#include <linux/iio/imu/adis.h>
21
22#define ADIS16400_STARTUP_DELAY 290 /* ms */
23#define ADIS16400_MTEST_DELAY 90 /* ms */
24
25#define ADIS16400_FLASH_CNT 0x00 /* Flash memory write count */
26#define ADIS16400_SUPPLY_OUT 0x02 /* Power supply measurement */
27#define ADIS16400_XGYRO_OUT 0x04 /* X-axis gyroscope output */
28#define ADIS16400_YGYRO_OUT 0x06 /* Y-axis gyroscope output */
29#define ADIS16400_ZGYRO_OUT 0x08 /* Z-axis gyroscope output */
30#define ADIS16400_XACCL_OUT 0x0A /* X-axis accelerometer output */
31#define ADIS16400_YACCL_OUT 0x0C /* Y-axis accelerometer output */
32#define ADIS16400_ZACCL_OUT 0x0E /* Z-axis accelerometer output */
33#define ADIS16400_XMAGN_OUT 0x10 /* X-axis magnetometer measurement */
34#define ADIS16400_YMAGN_OUT 0x12 /* Y-axis magnetometer measurement */
35#define ADIS16400_ZMAGN_OUT 0x14 /* Z-axis magnetometer measurement */
36#define ADIS16400_TEMP_OUT 0x16 /* Temperature output */
37#define ADIS16400_AUX_ADC 0x18 /* Auxiliary ADC measurement */
38
39#define ADIS16350_XTEMP_OUT 0x10 /* X-axis gyroscope temperature measurement */
40#define ADIS16350_YTEMP_OUT 0x12 /* Y-axis gyroscope temperature measurement */
41#define ADIS16350_ZTEMP_OUT 0x14 /* Z-axis gyroscope temperature measurement */
42
43#define ADIS16300_PITCH_OUT 0x12 /* X axis inclinometer output measurement */
44#define ADIS16300_ROLL_OUT 0x14 /* Y axis inclinometer output measurement */
45#define ADIS16300_AUX_ADC 0x16 /* Auxiliary ADC measurement */
46
47#define ADIS16448_BARO_OUT 0x16 /* Barometric pressure output */
48#define ADIS16448_TEMP_OUT 0x18 /* Temperature output */
49
50/* Calibration parameters */
51#define ADIS16400_XGYRO_OFF 0x1A /* X-axis gyroscope bias offset factor */
52#define ADIS16400_YGYRO_OFF 0x1C /* Y-axis gyroscope bias offset factor */
53#define ADIS16400_ZGYRO_OFF 0x1E /* Z-axis gyroscope bias offset factor */
54#define ADIS16400_XACCL_OFF 0x20 /* X-axis acceleration bias offset factor */
55#define ADIS16400_YACCL_OFF 0x22 /* Y-axis acceleration bias offset factor */
56#define ADIS16400_ZACCL_OFF 0x24 /* Z-axis acceleration bias offset factor */
57#define ADIS16400_XMAGN_HIF 0x26 /* X-axis magnetometer, hard-iron factor */
58#define ADIS16400_YMAGN_HIF 0x28 /* Y-axis magnetometer, hard-iron factor */
59#define ADIS16400_ZMAGN_HIF 0x2A /* Z-axis magnetometer, hard-iron factor */
60#define ADIS16400_XMAGN_SIF 0x2C /* X-axis magnetometer, soft-iron factor */
61#define ADIS16400_YMAGN_SIF 0x2E /* Y-axis magnetometer, soft-iron factor */
62#define ADIS16400_ZMAGN_SIF 0x30 /* Z-axis magnetometer, soft-iron factor */
63
64#define ADIS16400_GPIO_CTRL 0x32 /* Auxiliary digital input/output control */
65#define ADIS16400_MSC_CTRL 0x34 /* Miscellaneous control */
66#define ADIS16400_SMPL_PRD 0x36 /* Internal sample period (rate) control */
67#define ADIS16400_SENS_AVG 0x38 /* Dynamic range and digital filter control */
68#define ADIS16400_SLP_CNT 0x3A /* Sleep mode control */
69#define ADIS16400_DIAG_STAT 0x3C /* System status */
70
71/* Alarm functions */
72#define ADIS16400_GLOB_CMD 0x3E /* System command */
73#define ADIS16400_ALM_MAG1 0x40 /* Alarm 1 amplitude threshold */
74#define ADIS16400_ALM_MAG2 0x42 /* Alarm 2 amplitude threshold */
75#define ADIS16400_ALM_SMPL1 0x44 /* Alarm 1 sample size */
76#define ADIS16400_ALM_SMPL2 0x46 /* Alarm 2 sample size */
77#define ADIS16400_ALM_CTRL 0x48 /* Alarm control */
78#define ADIS16400_AUX_DAC 0x4A /* Auxiliary DAC data */
79
80#define ADIS16334_LOT_ID1 0x52 /* Lot identification code 1 */
81#define ADIS16334_LOT_ID2 0x54 /* Lot identification code 2 */
82#define ADIS16400_PRODUCT_ID 0x56 /* Product identifier */
83#define ADIS16334_SERIAL_NUMBER 0x58 /* Serial number, lot specific */
84
85#define ADIS16400_ERROR_ACTIVE (1<<14)
86#define ADIS16400_NEW_DATA (1<<14)
87
88/* MSC_CTRL */
89#define ADIS16400_MSC_CTRL_MEM_TEST (1<<11)
90#define ADIS16400_MSC_CTRL_INT_SELF_TEST (1<<10)
91#define ADIS16400_MSC_CTRL_NEG_SELF_TEST (1<<9)
92#define ADIS16400_MSC_CTRL_POS_SELF_TEST (1<<8)
93#define ADIS16400_MSC_CTRL_GYRO_BIAS (1<<7)
94#define ADIS16400_MSC_CTRL_ACCL_ALIGN (1<<6)
95#define ADIS16400_MSC_CTRL_DATA_RDY_EN (1<<2)
96#define ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH (1<<1)
97#define ADIS16400_MSC_CTRL_DATA_RDY_DIO2 (1<<0)
98
99/* SMPL_PRD */
100#define ADIS16400_SMPL_PRD_TIME_BASE (1<<7)
101#define ADIS16400_SMPL_PRD_DIV_MASK 0x7F
102
103/* DIAG_STAT */
104#define ADIS16400_DIAG_STAT_ZACCL_FAIL 15
105#define ADIS16400_DIAG_STAT_YACCL_FAIL 14
106#define ADIS16400_DIAG_STAT_XACCL_FAIL 13
107#define ADIS16400_DIAG_STAT_XGYRO_FAIL 12
108#define ADIS16400_DIAG_STAT_YGYRO_FAIL 11
109#define ADIS16400_DIAG_STAT_ZGYRO_FAIL 10
110#define ADIS16400_DIAG_STAT_ALARM2 9
111#define ADIS16400_DIAG_STAT_ALARM1 8
112#define ADIS16400_DIAG_STAT_FLASH_CHK 6
113#define ADIS16400_DIAG_STAT_SELF_TEST 5
114#define ADIS16400_DIAG_STAT_OVERFLOW 4
115#define ADIS16400_DIAG_STAT_SPI_FAIL 3
116#define ADIS16400_DIAG_STAT_FLASH_UPT 2
117#define ADIS16400_DIAG_STAT_POWER_HIGH 1
118#define ADIS16400_DIAG_STAT_POWER_LOW 0
119
120/* GLOB_CMD */
121#define ADIS16400_GLOB_CMD_SW_RESET (1<<7)
122#define ADIS16400_GLOB_CMD_P_AUTO_NULL (1<<4)
123#define ADIS16400_GLOB_CMD_FLASH_UPD (1<<3)
124#define ADIS16400_GLOB_CMD_DAC_LATCH (1<<2)
125#define ADIS16400_GLOB_CMD_FAC_CALIB (1<<1)
126#define ADIS16400_GLOB_CMD_AUTO_NULL (1<<0)
127
128/* SLP_CNT */
129#define ADIS16400_SLP_CNT_POWER_OFF (1<<8)
130
131#define ADIS16334_RATE_DIV_SHIFT 8
132#define ADIS16334_RATE_INT_CLK BIT(0)
133
134#define ADIS16400_SPI_SLOW (u32)(300 * 1000)
135#define ADIS16400_SPI_BURST (u32)(1000 * 1000)
136#define ADIS16400_SPI_FAST (u32)(2000 * 1000)
137
138#define ADIS16400_HAS_PROD_ID BIT(0)
139#define ADIS16400_NO_BURST BIT(1)
140#define ADIS16400_HAS_SLOW_MODE BIT(2)
141#define ADIS16400_HAS_SERIAL_NUMBER BIT(3)
142
143struct adis16400_state;
144
145struct adis16400_chip_info {
146 const struct iio_chan_spec *channels;
147 const int num_channels;
148 const long flags;
149 unsigned int gyro_scale_micro;
150 unsigned int accel_scale_micro;
151 int temp_scale_nano;
152 int temp_offset;
153 int (*set_freq)(struct adis16400_state *st, unsigned int freq);
154 int (*get_freq)(struct adis16400_state *st);
155};
156
157/**
158 * struct adis16400_state - device instance specific data
159 * @variant: chip variant info
160 * @filt_int: integer part of requested filter frequency
161 * @adis: adis device
162 **/
163struct adis16400_state {
164 struct adis16400_chip_info *variant;
165 int filt_int;
166
167 struct adis adis;
168};
169
170/* At the moment triggers are only used for ring buffer
171 * filling. This may change!
172 */
173
174enum {
175 ADIS16400_SCAN_SUPPLY,
176 ADIS16400_SCAN_GYRO_X,
177 ADIS16400_SCAN_GYRO_Y,
178 ADIS16400_SCAN_GYRO_Z,
179 ADIS16400_SCAN_ACC_X,
180 ADIS16400_SCAN_ACC_Y,
181 ADIS16400_SCAN_ACC_Z,
182 ADIS16400_SCAN_MAGN_X,
183 ADIS16400_SCAN_MAGN_Y,
184 ADIS16400_SCAN_MAGN_Z,
185 ADIS16400_SCAN_BARO,
186 ADIS16350_SCAN_TEMP_X,
187 ADIS16350_SCAN_TEMP_Y,
188 ADIS16350_SCAN_TEMP_Z,
189 ADIS16300_SCAN_INCLI_X,
190 ADIS16300_SCAN_INCLI_Y,
191 ADIS16400_SCAN_ADC,
192};
193
194#ifdef CONFIG_IIO_BUFFER
195
196ssize_t adis16400_read_data_from_ring(struct device *dev,
197 struct device_attribute *attr,
198 char *buf);
199
200
201int adis16400_update_scan_mode(struct iio_dev *indio_dev,
202 const unsigned long *scan_mask);
203irqreturn_t adis16400_trigger_handler(int irq, void *p);
204
205#else /* CONFIG_IIO_BUFFER */
206
207#define adis16400_update_scan_mode NULL
208#define adis16400_trigger_handler NULL
209
210#endif /* CONFIG_IIO_BUFFER */
211
212#endif /* SPI_ADIS16400_H_ */
diff --git a/drivers/iio/imu/adis16400_buffer.c b/drivers/iio/imu/adis16400_buffer.c
new file mode 100644
index 000000000000..054c01d6e73c
--- /dev/null
+++ b/drivers/iio/imu/adis16400_buffer.c
@@ -0,0 +1,96 @@
1#include <linux/interrupt.h>
2#include <linux/mutex.h>
3#include <linux/kernel.h>
4#include <linux/spi/spi.h>
5#include <linux/slab.h>
6#include <linux/bitops.h>
7#include <linux/export.h>
8
9#include <linux/iio/iio.h>
10#include <linux/iio/buffer.h>
11#include <linux/iio/triggered_buffer.h>
12#include <linux/iio/trigger_consumer.h>
13
14#include "adis16400.h"
15
16int adis16400_update_scan_mode(struct iio_dev *indio_dev,
17 const unsigned long *scan_mask)
18{
19 struct adis16400_state *st = iio_priv(indio_dev);
20 struct adis *adis = &st->adis;
21 uint16_t *tx, *rx;
22
23 if (st->variant->flags & ADIS16400_NO_BURST)
24 return adis_update_scan_mode(indio_dev, scan_mask);
25
26 kfree(adis->xfer);
27 kfree(adis->buffer);
28
29 adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
30 if (!adis->xfer)
31 return -ENOMEM;
32
33 adis->buffer = kzalloc(indio_dev->scan_bytes + sizeof(u16),
34 GFP_KERNEL);
35 if (!adis->buffer)
36 return -ENOMEM;
37
38 rx = adis->buffer;
39 tx = adis->buffer + indio_dev->scan_bytes;
40
41 tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD);
42 tx[1] = 0;
43
44 adis->xfer[0].tx_buf = tx;
45 adis->xfer[0].bits_per_word = 8;
46 adis->xfer[0].len = 2;
47 adis->xfer[1].tx_buf = tx;
48 adis->xfer[1].bits_per_word = 8;
49 adis->xfer[1].len = indio_dev->scan_bytes;
50
51 spi_message_init(&adis->msg);
52 spi_message_add_tail(&adis->xfer[0], &adis->msg);
53 spi_message_add_tail(&adis->xfer[1], &adis->msg);
54
55 return 0;
56}
57
58irqreturn_t adis16400_trigger_handler(int irq, void *p)
59{
60 struct iio_poll_func *pf = p;
61 struct iio_dev *indio_dev = pf->indio_dev;
62 struct adis16400_state *st = iio_priv(indio_dev);
63 struct adis *adis = &st->adis;
64 u32 old_speed_hz = st->adis.spi->max_speed_hz;
65 int ret;
66
67 if (!adis->buffer)
68 return -ENOMEM;
69
70 if (!(st->variant->flags & ADIS16400_NO_BURST) &&
71 st->adis.spi->max_speed_hz > ADIS16400_SPI_BURST) {
72 st->adis.spi->max_speed_hz = ADIS16400_SPI_BURST;
73 spi_setup(st->adis.spi);
74 }
75
76 ret = spi_sync(adis->spi, &adis->msg);
77 if (ret)
78 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
79
80 if (!(st->variant->flags & ADIS16400_NO_BURST)) {
81 st->adis.spi->max_speed_hz = old_speed_hz;
82 spi_setup(st->adis.spi);
83 }
84
85 /* Guaranteed to be aligned with 8 byte boundary */
86 if (indio_dev->scan_timestamp) {
87 void *b = adis->buffer + indio_dev->scan_bytes - sizeof(s64);
88 *(s64 *)b = pf->timestamp;
89 }
90
91 iio_push_to_buffers(indio_dev, adis->buffer);
92
93 iio_trigger_notify_done(indio_dev->trig);
94
95 return IRQ_HANDLED;
96}
diff --git a/drivers/iio/imu/adis16400_core.c b/drivers/iio/imu/adis16400_core.c
new file mode 100644
index 000000000000..b7f215eab5de
--- /dev/null
+++ b/drivers/iio/imu/adis16400_core.c
@@ -0,0 +1,965 @@
1/*
2 * adis16400.c support Analog Devices ADIS16400/5
3 * 3d 2g Linear Accelerometers,
4 * 3d Gyroscopes,
5 * 3d Magnetometers via SPI
6 *
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
9 * Copyright (c) 2011 Analog Devices Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/spi/spi.h>
24#include <linux/slab.h>
25#include <linux/sysfs.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/debugfs.h>
29
30#include <linux/iio/iio.h>
31#include <linux/iio/sysfs.h>
32#include <linux/iio/buffer.h>
33
34#include "adis16400.h"
35
36#ifdef CONFIG_DEBUG_FS
37
38static ssize_t adis16400_show_serial_number(struct file *file,
39 char __user *userbuf, size_t count, loff_t *ppos)
40{
41 struct adis16400_state *st = file->private_data;
42 u16 lot1, lot2, serial_number;
43 char buf[16];
44 size_t len;
45 int ret;
46
47 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
48 if (ret < 0)
49 return ret;
50
51 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
52 if (ret < 0)
53 return ret;
54
55 ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
56 &serial_number);
57 if (ret < 0)
58 return ret;
59
60 len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
61 serial_number);
62
63 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
64}
65
66static const struct file_operations adis16400_serial_number_fops = {
67 .open = simple_open,
68 .read = adis16400_show_serial_number,
69 .llseek = default_llseek,
70 .owner = THIS_MODULE,
71};
72
73static int adis16400_show_product_id(void *arg, u64 *val)
74{
75 struct adis16400_state *st = arg;
76 uint16_t prod_id;
77 int ret;
78
79 ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
80 if (ret < 0)
81 return ret;
82
83 *val = prod_id;
84
85 return 0;
86}
87DEFINE_SIMPLE_ATTRIBUTE(adis16400_product_id_fops,
88 adis16400_show_product_id, NULL, "%lld\n");
89
90static int adis16400_show_flash_count(void *arg, u64 *val)
91{
92 struct adis16400_state *st = arg;
93 uint16_t flash_count;
94 int ret;
95
96 ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
97 if (ret < 0)
98 return ret;
99
100 *val = flash_count;
101
102 return 0;
103}
104DEFINE_SIMPLE_ATTRIBUTE(adis16400_flash_count_fops,
105 adis16400_show_flash_count, NULL, "%lld\n");
106
107static int adis16400_debugfs_init(struct iio_dev *indio_dev)
108{
109 struct adis16400_state *st = iio_priv(indio_dev);
110
111 if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
112 debugfs_create_file("serial_number", 0400,
113 indio_dev->debugfs_dentry, st,
114 &adis16400_serial_number_fops);
115 if (st->variant->flags & ADIS16400_HAS_PROD_ID)
116 debugfs_create_file("product_id", 0400,
117 indio_dev->debugfs_dentry, st,
118 &adis16400_product_id_fops);
119 debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
120 st, &adis16400_flash_count_fops);
121
122 return 0;
123}
124
125#else
126
127static int adis16400_debugfs_init(struct iio_dev *indio_dev)
128{
129 return 0;
130}
131
132#endif
133
134enum adis16400_chip_variant {
135 ADIS16300,
136 ADIS16334,
137 ADIS16350,
138 ADIS16360,
139 ADIS16362,
140 ADIS16364,
141 ADIS16400,
142 ADIS16448,
143};
144
145static int adis16334_get_freq(struct adis16400_state *st)
146{
147 int ret;
148 uint16_t t;
149
150 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
151 if (ret < 0)
152 return ret;
153
154 t >>= ADIS16334_RATE_DIV_SHIFT;
155
156 return 819200 >> t;
157}
158
159static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
160{
161 unsigned int t;
162
163 if (freq < 819200)
164 t = ilog2(819200 / freq);
165 else
166 t = 0;
167
168 if (t > 0x31)
169 t = 0x31;
170
171 t <<= ADIS16334_RATE_DIV_SHIFT;
172 t |= ADIS16334_RATE_INT_CLK;
173
174 return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
175}
176
177static int adis16400_get_freq(struct adis16400_state *st)
178{
179 int sps, ret;
180 uint16_t t;
181
182 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
183 if (ret < 0)
184 return ret;
185
186 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
187 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
188
189 return sps;
190}
191
192static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
193{
194 unsigned int t;
195 uint8_t val = 0;
196
197 t = 1638404 / freq;
198 if (t >= 128) {
199 val |= ADIS16400_SMPL_PRD_TIME_BASE;
200 t = 52851 / freq;
201 if (t >= 128)
202 t = 127;
203 } else if (t != 0) {
204 t--;
205 }
206
207 val |= t;
208
209 if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
210 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
211 else
212 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
213
214 return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
215}
216
217static ssize_t adis16400_read_frequency(struct device *dev,
218 struct device_attribute *attr,
219 char *buf)
220{
221 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
222 struct adis16400_state *st = iio_priv(indio_dev);
223 int ret;
224
225 ret = st->variant->get_freq(st);
226 if (ret < 0)
227 return ret;
228
229 return sprintf(buf, "%d.%.3d\n", ret / 1000, ret % 1000);
230}
231
232static const unsigned adis16400_3db_divisors[] = {
233 [0] = 2, /* Special case */
234 [1] = 6,
235 [2] = 12,
236 [3] = 25,
237 [4] = 50,
238 [5] = 100,
239 [6] = 200,
240 [7] = 200, /* Not a valid setting */
241};
242
243static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
244{
245 struct adis16400_state *st = iio_priv(indio_dev);
246 uint16_t val16;
247 int i, ret;
248
249 for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
250 if (sps / adis16400_3db_divisors[i] >= val)
251 break;
252 }
253
254 ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
255 if (ret < 0)
256 return ret;
257
258 ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
259 (val16 & ~0x07) | i);
260 return ret;
261}
262
263static ssize_t adis16400_write_frequency(struct device *dev,
264 struct device_attribute *attr, const char *buf, size_t len)
265{
266 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
267 struct adis16400_state *st = iio_priv(indio_dev);
268 int i, f, val;
269 int ret;
270
271 ret = iio_str_to_fixpoint(buf, 100, &i, &f);
272 if (ret)
273 return ret;
274
275 val = i * 1000 + f;
276
277 if (val <= 0)
278 return -EINVAL;
279
280 mutex_lock(&indio_dev->mlock);
281 st->variant->set_freq(st, val);
282 mutex_unlock(&indio_dev->mlock);
283
284 return ret ? ret : len;
285}
286
287/* Power down the device */
288static int adis16400_stop_device(struct iio_dev *indio_dev)
289{
290 struct adis16400_state *st = iio_priv(indio_dev);
291 int ret;
292
293 ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
294 ADIS16400_SLP_CNT_POWER_OFF);
295 if (ret)
296 dev_err(&indio_dev->dev,
297 "problem with turning device off: SLP_CNT");
298
299 return ret;
300}
301
302static int adis16400_initial_setup(struct iio_dev *indio_dev)
303{
304 struct adis16400_state *st = iio_priv(indio_dev);
305 uint16_t prod_id, smp_prd;
306 unsigned int device_id;
307 int ret;
308
309 /* use low spi speed for init if the device has a slow mode */
310 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
311 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
312 else
313 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
314 st->adis.spi->mode = SPI_MODE_3;
315 spi_setup(st->adis.spi);
316
317 ret = adis_initial_startup(&st->adis);
318 if (ret)
319 return ret;
320
321 if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
322 ret = adis_read_reg_16(&st->adis,
323 ADIS16400_PRODUCT_ID, &prod_id);
324 if (ret)
325 goto err_ret;
326
327 sscanf(indio_dev->name, "adis%u\n", &device_id);
328
329 if (prod_id != device_id)
330 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
331 device_id, prod_id);
332
333 dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
334 indio_dev->name, prod_id,
335 st->adis.spi->chip_select, st->adis.spi->irq);
336 }
337 /* use high spi speed if possible */
338 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
339 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
340 if (ret)
341 goto err_ret;
342
343 if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
344 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
345 spi_setup(st->adis.spi);
346 }
347 }
348
349err_ret:
350 return ret;
351}
352
353static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
354 adis16400_read_frequency,
355 adis16400_write_frequency);
356
357static const uint8_t adis16400_addresses[] = {
358 [ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
359 [ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
360 [ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
361 [ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
362 [ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
363 [ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
364};
365
366static int adis16400_write_raw(struct iio_dev *indio_dev,
367 struct iio_chan_spec const *chan, int val, int val2, long info)
368{
369 struct adis16400_state *st = iio_priv(indio_dev);
370 int ret, sps;
371
372 switch (info) {
373 case IIO_CHAN_INFO_CALIBBIAS:
374 mutex_lock(&indio_dev->mlock);
375 ret = adis_write_reg_16(&st->adis,
376 adis16400_addresses[chan->scan_index], val);
377 mutex_unlock(&indio_dev->mlock);
378 return ret;
379 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
380 /*
381 * Need to cache values so we can update if the frequency
382 * changes.
383 */
384 mutex_lock(&indio_dev->mlock);
385 st->filt_int = val;
386 /* Work out update to current value */
387 sps = st->variant->get_freq(st);
388 if (sps < 0) {
389 mutex_unlock(&indio_dev->mlock);
390 return sps;
391 }
392
393 ret = adis16400_set_filter(indio_dev, sps,
394 val * 1000 + val2 / 1000);
395 mutex_unlock(&indio_dev->mlock);
396 return ret;
397 default:
398 return -EINVAL;
399 }
400}
401
402static int adis16400_read_raw(struct iio_dev *indio_dev,
403 struct iio_chan_spec const *chan, int *val, int *val2, long info)
404{
405 struct adis16400_state *st = iio_priv(indio_dev);
406 int16_t val16;
407 int ret;
408
409 switch (info) {
410 case IIO_CHAN_INFO_RAW:
411 return adis_single_conversion(indio_dev, chan, 0, val);
412 case IIO_CHAN_INFO_SCALE:
413 switch (chan->type) {
414 case IIO_ANGL_VEL:
415 *val = 0;
416 *val2 = st->variant->gyro_scale_micro;
417 return IIO_VAL_INT_PLUS_MICRO;
418 case IIO_VOLTAGE:
419 *val = 0;
420 if (chan->channel == 0) {
421 *val = 2;
422 *val2 = 418000; /* 2.418 mV */
423 } else {
424 *val = 0;
425 *val2 = 805800; /* 805.8 uV */
426 }
427 return IIO_VAL_INT_PLUS_MICRO;
428 case IIO_ACCEL:
429 *val = 0;
430 *val2 = st->variant->accel_scale_micro;
431 return IIO_VAL_INT_PLUS_MICRO;
432 case IIO_MAGN:
433 *val = 0;
434 *val2 = 500; /* 0.5 mgauss */
435 return IIO_VAL_INT_PLUS_MICRO;
436 case IIO_TEMP:
437 *val = st->variant->temp_scale_nano / 1000000;
438 *val2 = (st->variant->temp_scale_nano % 1000000);
439 return IIO_VAL_INT_PLUS_MICRO;
440 default:
441 return -EINVAL;
442 }
443 case IIO_CHAN_INFO_CALIBBIAS:
444 mutex_lock(&indio_dev->mlock);
445 ret = adis_read_reg_16(&st->adis,
446 adis16400_addresses[chan->scan_index], &val16);
447 mutex_unlock(&indio_dev->mlock);
448 if (ret)
449 return ret;
450 val16 = ((val16 & 0xFFF) << 4) >> 4;
451 *val = val16;
452 return IIO_VAL_INT;
453 case IIO_CHAN_INFO_OFFSET:
454 /* currently only temperature */
455 *val = st->variant->temp_offset;
456 return IIO_VAL_INT;
457 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
458 mutex_lock(&indio_dev->mlock);
459 /* Need both the number of taps and the sampling frequency */
460 ret = adis_read_reg_16(&st->adis,
461 ADIS16400_SENS_AVG,
462 &val16);
463 if (ret < 0) {
464 mutex_unlock(&indio_dev->mlock);
465 return ret;
466 }
467 ret = st->variant->get_freq(st);
468 if (ret >= 0) {
469 ret /= adis16400_3db_divisors[val16 & 0x07];
470 *val = ret / 1000;
471 *val2 = (ret % 1000) * 1000;
472 }
473 mutex_unlock(&indio_dev->mlock);
474 if (ret < 0)
475 return ret;
476 return IIO_VAL_INT_PLUS_MICRO;
477 default:
478 return -EINVAL;
479 }
480}
481
482#define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si) { \
483 .type = IIO_VOLTAGE, \
484 .indexed = 1, \
485 .channel = 0, \
486 .extend_name = name, \
487 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
488 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
489 .address = (addr), \
490 .scan_index = (si), \
491 .scan_type = { \
492 .sign = 'u', \
493 .realbits = (bits), \
494 .storagebits = 16, \
495 .shift = 0, \
496 .endianness = IIO_BE, \
497 }, \
498}
499
500#define ADIS16400_SUPPLY_CHAN(addr, bits) \
501 ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY)
502
503#define ADIS16400_AUX_ADC_CHAN(addr, bits) \
504 ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC)
505
506#define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
507 .type = IIO_ANGL_VEL, \
508 .modified = 1, \
509 .channel2 = IIO_MOD_ ## mod, \
510 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
511 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT | \
512 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
513 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT, \
514 .address = addr, \
515 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
516 .scan_type = { \
517 .sign = 's', \
518 .realbits = (bits), \
519 .storagebits = 16, \
520 .shift = 0, \
521 .endianness = IIO_BE, \
522 }, \
523}
524
525#define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
526 .type = IIO_ACCEL, \
527 .modified = 1, \
528 .channel2 = IIO_MOD_ ## mod, \
529 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
530 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT | \
531 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
532 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT, \
533 .address = (addr), \
534 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
535 .scan_type = { \
536 .sign = 's', \
537 .realbits = (bits), \
538 .storagebits = 16, \
539 .shift = 0, \
540 .endianness = IIO_BE, \
541 }, \
542}
543
544#define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
545 .type = IIO_MAGN, \
546 .modified = 1, \
547 .channel2 = IIO_MOD_ ## mod, \
548 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
549 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
550 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT, \
551 .address = (addr), \
552 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
553 .scan_type = { \
554 .sign = 's', \
555 .realbits = (bits), \
556 .storagebits = 16, \
557 .shift = 0, \
558 .endianness = IIO_BE, \
559 }, \
560}
561
562#define ADIS16400_MOD_TEMP_NAME_X "x"
563#define ADIS16400_MOD_TEMP_NAME_Y "y"
564#define ADIS16400_MOD_TEMP_NAME_Z "z"
565
566#define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
567 .type = IIO_TEMP, \
568 .indexed = 1, \
569 .channel = 0, \
570 .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
571 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
572 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
573 IIO_CHAN_INFO_SCALE_SEPARATE_BIT | \
574 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT, \
575 .address = (addr), \
576 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
577 .scan_type = { \
578 .sign = 's', \
579 .realbits = (bits), \
580 .storagebits = 16, \
581 .shift = 0, \
582 .endianness = IIO_BE, \
583 }, \
584}
585
586#define ADIS16400_TEMP_CHAN(addr, bits) { \
587 .type = IIO_TEMP, \
588 .indexed = 1, \
589 .channel = 0, \
590 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
591 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
592 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
593 .address = (addr), \
594 .scan_index = ADIS16350_SCAN_TEMP_X, \
595 .scan_type = { \
596 .sign = 's', \
597 .realbits = (bits), \
598 .storagebits = 16, \
599 .shift = 0, \
600 .endianness = IIO_BE, \
601 }, \
602}
603
604#define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
605 .type = IIO_INCLI, \
606 .modified = 1, \
607 .channel2 = IIO_MOD_ ## mod, \
608 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
609 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
610 .address = (addr), \
611 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
612 .scan_type = { \
613 .sign = 's', \
614 .realbits = (bits), \
615 .storagebits = 16, \
616 .shift = 0, \
617 .endianness = IIO_BE, \
618 }, \
619}
620
621static const struct iio_chan_spec adis16400_channels[] = {
622 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
623 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
624 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
625 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
626 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
627 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
628 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
629 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
630 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
631 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
632 ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
633 ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
634 IIO_CHAN_SOFT_TIMESTAMP(12)
635};
636
637static const struct iio_chan_spec adis16448_channels[] = {
638 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
639 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
640 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
641 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
642 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
643 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
644 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
645 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
646 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
647 {
648 .type = IIO_PRESSURE,
649 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
650 IIO_CHAN_INFO_SCALE_SHARED_BIT,
651 .address = ADIS16448_BARO_OUT,
652 .scan_index = ADIS16400_SCAN_BARO,
653 .scan_type = IIO_ST('s', 16, 16, 0),
654 },
655 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
656 IIO_CHAN_SOFT_TIMESTAMP(11)
657};
658
659static const struct iio_chan_spec adis16350_channels[] = {
660 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
661 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
662 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
663 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
664 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
665 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
666 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
667 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
668 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
669 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
670 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
671 ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
672 ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
673 ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
674 IIO_CHAN_SOFT_TIMESTAMP(11)
675};
676
677static const struct iio_chan_spec adis16300_channels[] = {
678 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
679 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
680 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
681 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
682 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
683 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
684 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
685 ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
686 ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
687 IIO_CHAN_SOFT_TIMESTAMP(14)
688};
689
690static const struct iio_chan_spec adis16334_channels[] = {
691 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
692 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
693 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
694 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
695 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
696 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
697 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
698 IIO_CHAN_SOFT_TIMESTAMP(8)
699};
700
701static struct attribute *adis16400_attributes[] = {
702 &iio_dev_attr_sampling_frequency.dev_attr.attr,
703 NULL
704};
705
706static const struct attribute_group adis16400_attribute_group = {
707 .attrs = adis16400_attributes,
708};
709
710static struct adis16400_chip_info adis16400_chips[] = {
711 [ADIS16300] = {
712 .channels = adis16300_channels,
713 .num_channels = ARRAY_SIZE(adis16300_channels),
714 .flags = ADIS16400_HAS_SLOW_MODE,
715 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
716 .accel_scale_micro = 5884,
717 .temp_scale_nano = 140000000, /* 0.14 C */
718 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
719 .set_freq = adis16400_set_freq,
720 .get_freq = adis16400_get_freq,
721 },
722 [ADIS16334] = {
723 .channels = adis16334_channels,
724 .num_channels = ARRAY_SIZE(adis16334_channels),
725 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
726 ADIS16400_HAS_SERIAL_NUMBER,
727 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
728 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
729 .temp_scale_nano = 67850000, /* 0.06785 C */
730 .temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
731 .set_freq = adis16334_set_freq,
732 .get_freq = adis16334_get_freq,
733 },
734 [ADIS16350] = {
735 .channels = adis16350_channels,
736 .num_channels = ARRAY_SIZE(adis16350_channels),
737 .gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
738 .accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
739 .temp_scale_nano = 145300000, /* 0.1453 C */
740 .temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
741 .flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
742 .set_freq = adis16400_set_freq,
743 .get_freq = adis16400_get_freq,
744 },
745 [ADIS16360] = {
746 .channels = adis16350_channels,
747 .num_channels = ARRAY_SIZE(adis16350_channels),
748 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
749 ADIS16400_HAS_SERIAL_NUMBER,
750 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
751 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
752 .temp_scale_nano = 136000000, /* 0.136 C */
753 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
754 .set_freq = adis16400_set_freq,
755 .get_freq = adis16400_get_freq,
756 },
757 [ADIS16362] = {
758 .channels = adis16350_channels,
759 .num_channels = ARRAY_SIZE(adis16350_channels),
760 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
761 ADIS16400_HAS_SERIAL_NUMBER,
762 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
763 .accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
764 .temp_scale_nano = 136000000, /* 0.136 C */
765 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
766 .set_freq = adis16400_set_freq,
767 .get_freq = adis16400_get_freq,
768 },
769 [ADIS16364] = {
770 .channels = adis16350_channels,
771 .num_channels = ARRAY_SIZE(adis16350_channels),
772 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
773 ADIS16400_HAS_SERIAL_NUMBER,
774 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
775 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
776 .temp_scale_nano = 136000000, /* 0.136 C */
777 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
778 .set_freq = adis16400_set_freq,
779 .get_freq = adis16400_get_freq,
780 },
781 [ADIS16400] = {
782 .channels = adis16400_channels,
783 .num_channels = ARRAY_SIZE(adis16400_channels),
784 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
785 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
786 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
787 .temp_scale_nano = 140000000, /* 0.14 C */
788 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
789 .set_freq = adis16400_set_freq,
790 .get_freq = adis16400_get_freq,
791 },
792 [ADIS16448] = {
793 .channels = adis16448_channels,
794 .num_channels = ARRAY_SIZE(adis16448_channels),
795 .flags = ADIS16400_HAS_PROD_ID |
796 ADIS16400_HAS_SERIAL_NUMBER,
797 .gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
798 .accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
799 .temp_scale_nano = 73860000, /* 0.07386 C */
800 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
801 .set_freq = adis16334_set_freq,
802 .get_freq = adis16334_get_freq,
803 }
804};
805
806static const struct iio_info adis16400_info = {
807 .driver_module = THIS_MODULE,
808 .read_raw = &adis16400_read_raw,
809 .write_raw = &adis16400_write_raw,
810 .attrs = &adis16400_attribute_group,
811 .update_scan_mode = adis16400_update_scan_mode,
812 .debugfs_reg_access = adis_debugfs_reg_access,
813};
814
815static const unsigned long adis16400_burst_scan_mask[] = {
816 ~0UL,
817 0,
818};
819
820static const char * const adis16400_status_error_msgs[] = {
821 [ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
822 [ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
823 [ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
824 [ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
825 [ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
826 [ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
827 [ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
828 [ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
829 [ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
830 [ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
831 [ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
832 [ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
833 [ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
834 [ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
835 [ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
836};
837
838static const struct adis_data adis16400_data = {
839 .msc_ctrl_reg = ADIS16400_MSC_CTRL,
840 .glob_cmd_reg = ADIS16400_GLOB_CMD,
841 .diag_stat_reg = ADIS16400_DIAG_STAT,
842
843 .read_delay = 50,
844 .write_delay = 50,
845
846 .self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
847 .startup_delay = ADIS16400_STARTUP_DELAY,
848
849 .status_error_msgs = adis16400_status_error_msgs,
850 .status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
851 BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
852 BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
853 BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
854 BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
855 BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
856 BIT(ADIS16400_DIAG_STAT_ALARM2) |
857 BIT(ADIS16400_DIAG_STAT_ALARM1) |
858 BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
859 BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
860 BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
861 BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
862 BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
863 BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
864 BIT(ADIS16400_DIAG_STAT_POWER_LOW),
865};
866
867static int adis16400_probe(struct spi_device *spi)
868{
869 struct adis16400_state *st;
870 struct iio_dev *indio_dev;
871 int ret;
872
873 indio_dev = iio_device_alloc(sizeof(*st));
874 if (indio_dev == NULL)
875 return -ENOMEM;
876
877 st = iio_priv(indio_dev);
878 /* this is only used for removal purposes */
879 spi_set_drvdata(spi, indio_dev);
880
881 /* setup the industrialio driver allocated elements */
882 st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
883 indio_dev->dev.parent = &spi->dev;
884 indio_dev->name = spi_get_device_id(spi)->name;
885 indio_dev->channels = st->variant->channels;
886 indio_dev->num_channels = st->variant->num_channels;
887 indio_dev->info = &adis16400_info;
888 indio_dev->modes = INDIO_DIRECT_MODE;
889
890 if (!(st->variant->flags & ADIS16400_NO_BURST))
891 indio_dev->available_scan_masks = adis16400_burst_scan_mask;
892
893 ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
894 if (ret)
895 goto error_free_dev;
896
897 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
898 adis16400_trigger_handler);
899 if (ret)
900 goto error_free_dev;
901
902 /* Get the device into a sane initial state */
903 ret = adis16400_initial_setup(indio_dev);
904 if (ret)
905 goto error_cleanup_buffer;
906 ret = iio_device_register(indio_dev);
907 if (ret)
908 goto error_cleanup_buffer;
909
910 adis16400_debugfs_init(indio_dev);
911 return 0;
912
913error_cleanup_buffer:
914 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
915error_free_dev:
916 iio_device_free(indio_dev);
917 return ret;
918}
919
920static int adis16400_remove(struct spi_device *spi)
921{
922 struct iio_dev *indio_dev = spi_get_drvdata(spi);
923 struct adis16400_state *st = iio_priv(indio_dev);
924
925 iio_device_unregister(indio_dev);
926 adis16400_stop_device(indio_dev);
927
928 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
929
930 iio_device_free(indio_dev);
931
932 return 0;
933}
934
935static const struct spi_device_id adis16400_id[] = {
936 {"adis16300", ADIS16300},
937 {"adis16334", ADIS16334},
938 {"adis16350", ADIS16350},
939 {"adis16354", ADIS16350},
940 {"adis16355", ADIS16350},
941 {"adis16360", ADIS16360},
942 {"adis16362", ADIS16362},
943 {"adis16364", ADIS16364},
944 {"adis16365", ADIS16360},
945 {"adis16400", ADIS16400},
946 {"adis16405", ADIS16400},
947 {"adis16448", ADIS16448},
948 {}
949};
950MODULE_DEVICE_TABLE(spi, adis16400_id);
951
952static struct spi_driver adis16400_driver = {
953 .driver = {
954 .name = "adis16400",
955 .owner = THIS_MODULE,
956 },
957 .id_table = adis16400_id,
958 .probe = adis16400_probe,
959 .remove = adis16400_remove,
960};
961module_spi_driver(adis16400_driver);
962
963MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
964MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
965MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/imu/inv_mpu6050/Kconfig b/drivers/iio/imu/inv_mpu6050/Kconfig
new file mode 100644
index 000000000000..b5cfa3a354cf
--- /dev/null
+++ b/drivers/iio/imu/inv_mpu6050/Kconfig
@@ -0,0 +1,13 @@
1#
2# inv-mpu6050 drivers for Invensense MPU devices and combos
3#
4
5config INV_MPU6050_IIO
6 tristate "Invensense MPU6050 devices"
7 depends on I2C && SYSFS
8 select IIO_TRIGGERED_BUFFER
9 help
10 This driver supports the Invensense MPU6050 devices.
11 It is a gyroscope/accelerometer combo device.
12 This driver can be built as a module. The module will be called
13 inv-mpu6050.
diff --git a/drivers/iio/imu/inv_mpu6050/Makefile b/drivers/iio/imu/inv_mpu6050/Makefile
new file mode 100644
index 000000000000..3a677c778afb
--- /dev/null
+++ b/drivers/iio/imu/inv_mpu6050/Makefile
@@ -0,0 +1,6 @@
1#
2# Makefile for Invensense MPU6050 device.
3#
4
5obj-$(CONFIG_INV_MPU6050_IIO) += inv-mpu6050.o
6inv-mpu6050-objs := inv_mpu_core.o inv_mpu_ring.o inv_mpu_trigger.o
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
new file mode 100644
index 000000000000..37ca05b47e4b
--- /dev/null
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
@@ -0,0 +1,795 @@
1/*
2* Copyright (C) 2012 Invensense, Inc.
3*
4* This software is licensed under the terms of the GNU General Public
5* License version 2, as published by the Free Software Foundation, and
6* may be copied, distributed, and modified under those terms.
7*
8* This program is distributed in the hope that it will be useful,
9* but WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*/
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/i2c.h>
18#include <linux/err.h>
19#include <linux/delay.h>
20#include <linux/sysfs.h>
21#include <linux/jiffies.h>
22#include <linux/irq.h>
23#include <linux/interrupt.h>
24#include <linux/kfifo.h>
25#include <linux/spinlock.h>
26#include "inv_mpu_iio.h"
27
28/*
29 * this is the gyro scale translated from dynamic range plus/minus
30 * {250, 500, 1000, 2000} to rad/s
31 */
32static const int gyro_scale_6050[] = {133090, 266181, 532362, 1064724};
33
34/*
35 * this is the accel scale translated from dynamic range plus/minus
36 * {2, 4, 8, 16} to m/s^2
37 */
38static const int accel_scale[] = {598, 1196, 2392, 4785};
39
40static const struct inv_mpu6050_reg_map reg_set_6050 = {
41 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
42 .lpf = INV_MPU6050_REG_CONFIG,
43 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
44 .fifo_en = INV_MPU6050_REG_FIFO_EN,
45 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
46 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
47 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
48 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
49 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
50 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
51 .temperature = INV_MPU6050_REG_TEMPERATURE,
52 .int_enable = INV_MPU6050_REG_INT_ENABLE,
53 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
54 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
55};
56
57static const struct inv_mpu6050_chip_config chip_config_6050 = {
58 .fsr = INV_MPU6050_FSR_2000DPS,
59 .lpf = INV_MPU6050_FILTER_20HZ,
60 .fifo_rate = INV_MPU6050_INIT_FIFO_RATE,
61 .gyro_fifo_enable = false,
62 .accl_fifo_enable = false,
63 .accl_fs = INV_MPU6050_FS_02G,
64};
65
66static const struct inv_mpu6050_hw hw_info[INV_NUM_PARTS] = {
67 {
68 .num_reg = 117,
69 .name = "MPU6050",
70 .reg = &reg_set_6050,
71 .config = &chip_config_6050,
72 },
73};
74
75int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 d)
76{
77 return i2c_smbus_write_i2c_block_data(st->client, reg, 1, &d);
78}
79
80int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
81{
82 u8 d, mgmt_1;
83 int result;
84
85 /* switch clock needs to be careful. Only when gyro is on, can
86 clock source be switched to gyro. Otherwise, it must be set to
87 internal clock */
88 if (INV_MPU6050_BIT_PWR_GYRO_STBY == mask) {
89 result = i2c_smbus_read_i2c_block_data(st->client,
90 st->reg->pwr_mgmt_1, 1, &mgmt_1);
91 if (result != 1)
92 return result;
93
94 mgmt_1 &= ~INV_MPU6050_BIT_CLK_MASK;
95 }
96
97 if ((INV_MPU6050_BIT_PWR_GYRO_STBY == mask) && (!en)) {
98 /* turning off gyro requires switch to internal clock first.
99 Then turn off gyro engine */
100 mgmt_1 |= INV_CLK_INTERNAL;
101 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1, mgmt_1);
102 if (result)
103 return result;
104 }
105
106 result = i2c_smbus_read_i2c_block_data(st->client,
107 st->reg->pwr_mgmt_2, 1, &d);
108 if (result != 1)
109 return result;
110 if (en)
111 d &= ~mask;
112 else
113 d |= mask;
114 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_2, d);
115 if (result)
116 return result;
117
118 if (en) {
119 /* Wait for output stablize */
120 msleep(INV_MPU6050_TEMP_UP_TIME);
121 if (INV_MPU6050_BIT_PWR_GYRO_STBY == mask) {
122 /* switch internal clock to PLL */
123 mgmt_1 |= INV_CLK_PLL;
124 result = inv_mpu6050_write_reg(st,
125 st->reg->pwr_mgmt_1, mgmt_1);
126 if (result)
127 return result;
128 }
129 }
130
131 return 0;
132}
133
134int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on)
135{
136 int result;
137
138 if (power_on)
139 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1, 0);
140 else
141 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1,
142 INV_MPU6050_BIT_SLEEP);
143 if (result)
144 return result;
145
146 if (power_on)
147 msleep(INV_MPU6050_REG_UP_TIME);
148
149 return 0;
150}
151
152/**
153 * inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
154 *
155 * Initial configuration:
156 * FSR: ± 2000DPS
157 * DLPF: 20Hz
158 * FIFO rate: 50Hz
159 * Clock source: Gyro PLL
160 */
161static int inv_mpu6050_init_config(struct iio_dev *indio_dev)
162{
163 int result;
164 u8 d;
165 struct inv_mpu6050_state *st = iio_priv(indio_dev);
166
167 result = inv_mpu6050_set_power_itg(st, true);
168 if (result)
169 return result;
170 d = (INV_MPU6050_FSR_2000DPS << INV_MPU6050_GYRO_CONFIG_FSR_SHIFT);
171 result = inv_mpu6050_write_reg(st, st->reg->gyro_config, d);
172 if (result)
173 return result;
174
175 d = INV_MPU6050_FILTER_20HZ;
176 result = inv_mpu6050_write_reg(st, st->reg->lpf, d);
177 if (result)
178 return result;
179
180 d = INV_MPU6050_ONE_K_HZ / INV_MPU6050_INIT_FIFO_RATE - 1;
181 result = inv_mpu6050_write_reg(st, st->reg->sample_rate_div, d);
182 if (result)
183 return result;
184
185 d = (INV_MPU6050_FS_02G << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
186 result = inv_mpu6050_write_reg(st, st->reg->accl_config, d);
187 if (result)
188 return result;
189
190 memcpy(&st->chip_config, hw_info[st->chip_type].config,
191 sizeof(struct inv_mpu6050_chip_config));
192 result = inv_mpu6050_set_power_itg(st, false);
193
194 return result;
195}
196
197static int inv_mpu6050_sensor_show(struct inv_mpu6050_state *st, int reg,
198 int axis, int *val)
199{
200 int ind, result;
201 __be16 d;
202
203 ind = (axis - IIO_MOD_X) * 2;
204 result = i2c_smbus_read_i2c_block_data(st->client, reg + ind, 2,
205 (u8 *)&d);
206 if (result != 2)
207 return -EINVAL;
208 *val = (short)be16_to_cpup(&d);
209
210 return IIO_VAL_INT;
211}
212
213static int inv_mpu6050_read_raw(struct iio_dev *indio_dev,
214 struct iio_chan_spec const *chan,
215 int *val,
216 int *val2,
217 long mask) {
218 struct inv_mpu6050_state *st = iio_priv(indio_dev);
219
220 switch (mask) {
221 case IIO_CHAN_INFO_RAW:
222 {
223 int ret, result;
224
225 ret = IIO_VAL_INT;
226 result = 0;
227 mutex_lock(&indio_dev->mlock);
228 if (!st->chip_config.enable) {
229 result = inv_mpu6050_set_power_itg(st, true);
230 if (result)
231 goto error_read_raw;
232 }
233 /* when enable is on, power is already on */
234 switch (chan->type) {
235 case IIO_ANGL_VEL:
236 if (!st->chip_config.gyro_fifo_enable ||
237 !st->chip_config.enable) {
238 result = inv_mpu6050_switch_engine(st, true,
239 INV_MPU6050_BIT_PWR_GYRO_STBY);
240 if (result)
241 goto error_read_raw;
242 }
243 ret = inv_mpu6050_sensor_show(st, st->reg->raw_gyro,
244 chan->channel2, val);
245 if (!st->chip_config.gyro_fifo_enable ||
246 !st->chip_config.enable) {
247 result = inv_mpu6050_switch_engine(st, false,
248 INV_MPU6050_BIT_PWR_GYRO_STBY);
249 if (result)
250 goto error_read_raw;
251 }
252 break;
253 case IIO_ACCEL:
254 if (!st->chip_config.accl_fifo_enable ||
255 !st->chip_config.enable) {
256 result = inv_mpu6050_switch_engine(st, true,
257 INV_MPU6050_BIT_PWR_ACCL_STBY);
258 if (result)
259 goto error_read_raw;
260 }
261 ret = inv_mpu6050_sensor_show(st, st->reg->raw_accl,
262 chan->channel2, val);
263 if (!st->chip_config.accl_fifo_enable ||
264 !st->chip_config.enable) {
265 result = inv_mpu6050_switch_engine(st, false,
266 INV_MPU6050_BIT_PWR_ACCL_STBY);
267 if (result)
268 goto error_read_raw;
269 }
270 break;
271 case IIO_TEMP:
272 /* wait for stablization */
273 msleep(INV_MPU6050_SENSOR_UP_TIME);
274 inv_mpu6050_sensor_show(st, st->reg->temperature,
275 IIO_MOD_X, val);
276 break;
277 default:
278 ret = -EINVAL;
279 break;
280 }
281error_read_raw:
282 if (!st->chip_config.enable)
283 result |= inv_mpu6050_set_power_itg(st, false);
284 mutex_unlock(&indio_dev->mlock);
285 if (result)
286 return result;
287
288 return ret;
289 }
290 case IIO_CHAN_INFO_SCALE:
291 switch (chan->type) {
292 case IIO_ANGL_VEL:
293 *val = 0;
294 *val2 = gyro_scale_6050[st->chip_config.fsr];
295
296 return IIO_VAL_INT_PLUS_NANO;
297 case IIO_ACCEL:
298 *val = 0;
299 *val2 = accel_scale[st->chip_config.accl_fs];
300
301 return IIO_VAL_INT_PLUS_MICRO;
302 case IIO_TEMP:
303 *val = 0;
304 *val2 = INV_MPU6050_TEMP_SCALE;
305
306 return IIO_VAL_INT_PLUS_MICRO;
307 default:
308 return -EINVAL;
309 }
310 case IIO_CHAN_INFO_OFFSET:
311 switch (chan->type) {
312 case IIO_TEMP:
313 *val = INV_MPU6050_TEMP_OFFSET;
314
315 return IIO_VAL_INT;
316 default:
317 return -EINVAL;
318 }
319 default:
320 return -EINVAL;
321 }
322}
323
324static int inv_mpu6050_write_fsr(struct inv_mpu6050_state *st, int fsr)
325{
326 int result;
327 u8 d;
328
329 if (fsr < 0 || fsr > INV_MPU6050_MAX_GYRO_FS_PARAM)
330 return -EINVAL;
331 if (fsr == st->chip_config.fsr)
332 return 0;
333
334 d = (fsr << INV_MPU6050_GYRO_CONFIG_FSR_SHIFT);
335 result = inv_mpu6050_write_reg(st, st->reg->gyro_config, d);
336 if (result)
337 return result;
338 st->chip_config.fsr = fsr;
339
340 return 0;
341}
342
343static int inv_mpu6050_write_accel_fs(struct inv_mpu6050_state *st, int fs)
344{
345 int result;
346 u8 d;
347
348 if (fs < 0 || fs > INV_MPU6050_MAX_ACCL_FS_PARAM)
349 return -EINVAL;
350 if (fs == st->chip_config.accl_fs)
351 return 0;
352
353 d = (fs << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
354 result = inv_mpu6050_write_reg(st, st->reg->accl_config, d);
355 if (result)
356 return result;
357 st->chip_config.accl_fs = fs;
358
359 return 0;
360}
361
362static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
363 struct iio_chan_spec const *chan,
364 int val,
365 int val2,
366 long mask) {
367 struct inv_mpu6050_state *st = iio_priv(indio_dev);
368 int result;
369
370 mutex_lock(&indio_dev->mlock);
371 /* we should only update scale when the chip is disabled, i.e.,
372 not running */
373 if (st->chip_config.enable) {
374 result = -EBUSY;
375 goto error_write_raw;
376 }
377 result = inv_mpu6050_set_power_itg(st, true);
378 if (result)
379 goto error_write_raw;
380
381 switch (mask) {
382 case IIO_CHAN_INFO_SCALE:
383 switch (chan->type) {
384 case IIO_ANGL_VEL:
385 result = inv_mpu6050_write_fsr(st, val);
386 break;
387 case IIO_ACCEL:
388 result = inv_mpu6050_write_accel_fs(st, val);
389 break;
390 default:
391 result = -EINVAL;
392 break;
393 }
394 break;
395 default:
396 result = -EINVAL;
397 break;
398 }
399
400error_write_raw:
401 result |= inv_mpu6050_set_power_itg(st, false);
402 mutex_unlock(&indio_dev->mlock);
403
404 return result;
405}
406
407/**
408 * inv_mpu6050_set_lpf() - set low pass filer based on fifo rate.
409 *
410 * Based on the Nyquist principle, the sampling rate must
411 * exceed twice of the bandwidth of the signal, or there
412 * would be alising. This function basically search for the
413 * correct low pass parameters based on the fifo rate, e.g,
414 * sampling frequency.
415 */
416static int inv_mpu6050_set_lpf(struct inv_mpu6050_state *st, int rate)
417{
418 const int hz[] = {188, 98, 42, 20, 10, 5};
419 const int d[] = {INV_MPU6050_FILTER_188HZ, INV_MPU6050_FILTER_98HZ,
420 INV_MPU6050_FILTER_42HZ, INV_MPU6050_FILTER_20HZ,
421 INV_MPU6050_FILTER_10HZ, INV_MPU6050_FILTER_5HZ};
422 int i, h, result;
423 u8 data;
424
425 h = (rate >> 1);
426 i = 0;
427 while ((h < hz[i]) && (i < ARRAY_SIZE(d) - 1))
428 i++;
429 data = d[i];
430 result = inv_mpu6050_write_reg(st, st->reg->lpf, data);
431 if (result)
432 return result;
433 st->chip_config.lpf = data;
434
435 return 0;
436}
437
438/**
439 * inv_mpu6050_fifo_rate_store() - Set fifo rate.
440 */
441static ssize_t inv_mpu6050_fifo_rate_store(struct device *dev,
442 struct device_attribute *attr, const char *buf, size_t count)
443{
444 s32 fifo_rate;
445 u8 d;
446 int result;
447 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
448 struct inv_mpu6050_state *st = iio_priv(indio_dev);
449
450 if (kstrtoint(buf, 10, &fifo_rate))
451 return -EINVAL;
452 if (fifo_rate < INV_MPU6050_MIN_FIFO_RATE ||
453 fifo_rate > INV_MPU6050_MAX_FIFO_RATE)
454 return -EINVAL;
455 if (fifo_rate == st->chip_config.fifo_rate)
456 return count;
457
458 mutex_lock(&indio_dev->mlock);
459 if (st->chip_config.enable) {
460 result = -EBUSY;
461 goto fifo_rate_fail;
462 }
463 result = inv_mpu6050_set_power_itg(st, true);
464 if (result)
465 goto fifo_rate_fail;
466
467 d = INV_MPU6050_ONE_K_HZ / fifo_rate - 1;
468 result = inv_mpu6050_write_reg(st, st->reg->sample_rate_div, d);
469 if (result)
470 goto fifo_rate_fail;
471 st->chip_config.fifo_rate = fifo_rate;
472
473 result = inv_mpu6050_set_lpf(st, fifo_rate);
474 if (result)
475 goto fifo_rate_fail;
476
477fifo_rate_fail:
478 result |= inv_mpu6050_set_power_itg(st, false);
479 mutex_unlock(&indio_dev->mlock);
480 if (result)
481 return result;
482
483 return count;
484}
485
486/**
487 * inv_fifo_rate_show() - Get the current sampling rate.
488 */
489static ssize_t inv_fifo_rate_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
492 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
493
494 return sprintf(buf, "%d\n", st->chip_config.fifo_rate);
495}
496
497/**
498 * inv_attr_show() - calling this function will show current
499 * parameters.
500 */
501static ssize_t inv_attr_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
503{
504 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
505 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
506 s8 *m;
507
508 switch (this_attr->address) {
509 /* In MPU6050, the two matrix are the same because gyro and accel
510 are integrated in one chip */
511 case ATTR_GYRO_MATRIX:
512 case ATTR_ACCL_MATRIX:
513 m = st->plat_data.orientation;
514
515 return sprintf(buf, "%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
516 m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
517 default:
518 return -EINVAL;
519 }
520}
521
522/**
523 * inv_mpu6050_validate_trigger() - validate_trigger callback for invensense
524 * MPU6050 device.
525 * @indio_dev: The IIO device
526 * @trig: The new trigger
527 *
528 * Returns: 0 if the 'trig' matches the trigger registered by the MPU6050
529 * device, -EINVAL otherwise.
530 */
531static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
532 struct iio_trigger *trig)
533{
534 struct inv_mpu6050_state *st = iio_priv(indio_dev);
535
536 if (st->trig != trig)
537 return -EINVAL;
538
539 return 0;
540}
541
542#define INV_MPU6050_CHAN(_type, _channel2, _index) \
543 { \
544 .type = _type, \
545 .modified = 1, \
546 .channel2 = _channel2, \
547 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT \
548 | IIO_CHAN_INFO_RAW_SEPARATE_BIT, \
549 .scan_index = _index, \
550 .scan_type = { \
551 .sign = 's', \
552 .realbits = 16, \
553 .storagebits = 16, \
554 .shift = 0 , \
555 .endianness = IIO_BE, \
556 }, \
557 }
558
559static const struct iio_chan_spec inv_mpu_channels[] = {
560 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
561 /*
562 * Note that temperature should only be via polled reading only,
563 * not the final scan elements output.
564 */
565 {
566 .type = IIO_TEMP,
567 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT
568 | IIO_CHAN_INFO_OFFSET_SEPARATE_BIT
569 | IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
570 .scan_index = -1,
571 },
572 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
573 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
574 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
575
576 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
577 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
578 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
579};
580
581/* constant IIO attribute */
582static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
583static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show,
584 inv_mpu6050_fifo_rate_store);
585static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL,
586 ATTR_GYRO_MATRIX);
587static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL,
588 ATTR_ACCL_MATRIX);
589
590static struct attribute *inv_attributes[] = {
591 &iio_dev_attr_in_gyro_matrix.dev_attr.attr,
592 &iio_dev_attr_in_accel_matrix.dev_attr.attr,
593 &iio_dev_attr_sampling_frequency.dev_attr.attr,
594 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
595 NULL,
596};
597
598static const struct attribute_group inv_attribute_group = {
599 .attrs = inv_attributes
600};
601
602static const struct iio_info mpu_info = {
603 .driver_module = THIS_MODULE,
604 .read_raw = &inv_mpu6050_read_raw,
605 .write_raw = &inv_mpu6050_write_raw,
606 .attrs = &inv_attribute_group,
607 .validate_trigger = inv_mpu6050_validate_trigger,
608};
609
610/**
611 * inv_check_and_setup_chip() - check and setup chip.
612 */
613static int inv_check_and_setup_chip(struct inv_mpu6050_state *st,
614 const struct i2c_device_id *id)
615{
616 int result;
617
618 st->chip_type = INV_MPU6050;
619 st->hw = &hw_info[st->chip_type];
620 st->reg = hw_info[st->chip_type].reg;
621
622 /* reset to make sure previous state are not there */
623 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1,
624 INV_MPU6050_BIT_H_RESET);
625 if (result)
626 return result;
627 msleep(INV_MPU6050_POWER_UP_TIME);
628 /* toggle power state. After reset, the sleep bit could be on
629 or off depending on the OTP settings. Toggling power would
630 make it in a definite state as well as making the hardware
631 state align with the software state */
632 result = inv_mpu6050_set_power_itg(st, false);
633 if (result)
634 return result;
635 result = inv_mpu6050_set_power_itg(st, true);
636 if (result)
637 return result;
638
639 result = inv_mpu6050_switch_engine(st, false,
640 INV_MPU6050_BIT_PWR_ACCL_STBY);
641 if (result)
642 return result;
643 result = inv_mpu6050_switch_engine(st, false,
644 INV_MPU6050_BIT_PWR_GYRO_STBY);
645 if (result)
646 return result;
647
648 return 0;
649}
650
651/**
652 * inv_mpu_probe() - probe function.
653 * @client: i2c client.
654 * @id: i2c device id.
655 *
656 * Returns 0 on success, a negative error code otherwise.
657 */
658static int inv_mpu_probe(struct i2c_client *client,
659 const struct i2c_device_id *id)
660{
661 struct inv_mpu6050_state *st;
662 struct iio_dev *indio_dev;
663 int result;
664
665 if (!i2c_check_functionality(client->adapter,
666 I2C_FUNC_SMBUS_READ_I2C_BLOCK |
667 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
668 result = -ENOSYS;
669 goto out_no_free;
670 }
671 indio_dev = iio_device_alloc(sizeof(*st));
672 if (indio_dev == NULL) {
673 result = -ENOMEM;
674 goto out_no_free;
675 }
676 st = iio_priv(indio_dev);
677 st->client = client;
678 st->plat_data = *(struct inv_mpu6050_platform_data
679 *)dev_get_platdata(&client->dev);
680 /* power is turned on inside check chip type*/
681 result = inv_check_and_setup_chip(st, id);
682 if (result)
683 goto out_free;
684
685 result = inv_mpu6050_init_config(indio_dev);
686 if (result) {
687 dev_err(&client->dev,
688 "Could not initialize device.\n");
689 goto out_free;
690 }
691
692 i2c_set_clientdata(client, indio_dev);
693 indio_dev->dev.parent = &client->dev;
694 indio_dev->name = id->name;
695 indio_dev->channels = inv_mpu_channels;
696 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
697
698 indio_dev->info = &mpu_info;
699 indio_dev->modes = INDIO_BUFFER_TRIGGERED;
700
701 result = iio_triggered_buffer_setup(indio_dev,
702 inv_mpu6050_irq_handler,
703 inv_mpu6050_read_fifo,
704 NULL);
705 if (result) {
706 dev_err(&st->client->dev, "configure buffer fail %d\n",
707 result);
708 goto out_free;
709 }
710 result = inv_mpu6050_probe_trigger(indio_dev);
711 if (result) {
712 dev_err(&st->client->dev, "trigger probe fail %d\n", result);
713 goto out_unreg_ring;
714 }
715
716 INIT_KFIFO(st->timestamps);
717 spin_lock_init(&st->time_stamp_lock);
718 result = iio_device_register(indio_dev);
719 if (result) {
720 dev_err(&st->client->dev, "IIO register fail %d\n", result);
721 goto out_remove_trigger;
722 }
723
724 return 0;
725
726out_remove_trigger:
727 inv_mpu6050_remove_trigger(st);
728out_unreg_ring:
729 iio_triggered_buffer_cleanup(indio_dev);
730out_free:
731 iio_device_free(indio_dev);
732out_no_free:
733
734 return result;
735}
736
737static int inv_mpu_remove(struct i2c_client *client)
738{
739 struct iio_dev *indio_dev = i2c_get_clientdata(client);
740 struct inv_mpu6050_state *st = iio_priv(indio_dev);
741
742 iio_device_unregister(indio_dev);
743 inv_mpu6050_remove_trigger(st);
744 iio_triggered_buffer_cleanup(indio_dev);
745 iio_device_free(indio_dev);
746
747 return 0;
748}
749#ifdef CONFIG_PM_SLEEP
750
751static int inv_mpu_resume(struct device *dev)
752{
753 return inv_mpu6050_set_power_itg(
754 iio_priv(i2c_get_clientdata(to_i2c_client(dev))), true);
755}
756
757static int inv_mpu_suspend(struct device *dev)
758{
759 return inv_mpu6050_set_power_itg(
760 iio_priv(i2c_get_clientdata(to_i2c_client(dev))), false);
761}
762static SIMPLE_DEV_PM_OPS(inv_mpu_pmops, inv_mpu_suspend, inv_mpu_resume);
763
764#define INV_MPU6050_PMOPS (&inv_mpu_pmops)
765#else
766#define INV_MPU6050_PMOPS NULL
767#endif /* CONFIG_PM_SLEEP */
768
769/*
770 * device id table is used to identify what device can be
771 * supported by this driver
772 */
773static const struct i2c_device_id inv_mpu_id[] = {
774 {"mpu6050", INV_MPU6050},
775 {}
776};
777
778MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
779
780static struct i2c_driver inv_mpu_driver = {
781 .probe = inv_mpu_probe,
782 .remove = inv_mpu_remove,
783 .id_table = inv_mpu_id,
784 .driver = {
785 .owner = THIS_MODULE,
786 .name = "inv-mpu6050",
787 .pm = INV_MPU6050_PMOPS,
788 },
789};
790
791module_i2c_driver(inv_mpu_driver);
792
793MODULE_AUTHOR("Invensense Corporation");
794MODULE_DESCRIPTION("Invensense device MPU6050 driver");
795MODULE_LICENSE("GPL");
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
new file mode 100644
index 000000000000..f38395529a44
--- /dev/null
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
@@ -0,0 +1,246 @@
1/*
2* Copyright (C) 2012 Invensense, Inc.
3*
4* This software is licensed under the terms of the GNU General Public
5* License version 2, as published by the Free Software Foundation, and
6* may be copied, distributed, and modified under those terms.
7*
8* This program is distributed in the hope that it will be useful,
9* but WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*/
13#include <linux/i2c.h>
14#include <linux/kfifo.h>
15#include <linux/spinlock.h>
16#include <linux/iio/iio.h>
17#include <linux/iio/buffer.h>
18#include <linux/iio/sysfs.h>
19#include <linux/iio/kfifo_buf.h>
20#include <linux/iio/trigger.h>
21#include <linux/iio/triggered_buffer.h>
22#include <linux/iio/trigger_consumer.h>
23#include <linux/platform_data/invensense_mpu6050.h>
24
25/**
26 * struct inv_mpu6050_reg_map - Notable registers.
27 * @sample_rate_div: Divider applied to gyro output rate.
28 * @lpf: Configures internal low pass filter.
29 * @user_ctrl: Enables/resets the FIFO.
30 * @fifo_en: Determines which data will appear in FIFO.
31 * @gyro_config: gyro config register.
32 * @accl_config: accel config register
33 * @fifo_count_h: Upper byte of FIFO count.
34 * @fifo_r_w: FIFO register.
35 * @raw_gyro: Address of first gyro register.
36 * @raw_accl: Address of first accel register.
37 * @temperature: temperature register
38 * @int_enable: Interrupt enable register.
39 * @pwr_mgmt_1: Controls chip's power state and clock source.
40 * @pwr_mgmt_2: Controls power state of individual sensors.
41 */
42struct inv_mpu6050_reg_map {
43 u8 sample_rate_div;
44 u8 lpf;
45 u8 user_ctrl;
46 u8 fifo_en;
47 u8 gyro_config;
48 u8 accl_config;
49 u8 fifo_count_h;
50 u8 fifo_r_w;
51 u8 raw_gyro;
52 u8 raw_accl;
53 u8 temperature;
54 u8 int_enable;
55 u8 pwr_mgmt_1;
56 u8 pwr_mgmt_2;
57};
58
59/*device enum */
60enum inv_devices {
61 INV_MPU6050,
62 INV_NUM_PARTS
63};
64
65/**
66 * struct inv_mpu6050_chip_config - Cached chip configuration data.
67 * @fsr: Full scale range.
68 * @lpf: Digital low pass filter frequency.
69 * @accl_fs: accel full scale range.
70 * @enable: master enable state.
71 * @accl_fifo_enable: enable accel data output
72 * @gyro_fifo_enable: enable gyro data output
73 * @fifo_rate: FIFO update rate.
74 */
75struct inv_mpu6050_chip_config {
76 unsigned int fsr:2;
77 unsigned int lpf:3;
78 unsigned int accl_fs:2;
79 unsigned int enable:1;
80 unsigned int accl_fifo_enable:1;
81 unsigned int gyro_fifo_enable:1;
82 u16 fifo_rate;
83};
84
85/**
86 * struct inv_mpu6050_hw - Other important hardware information.
87 * @num_reg: Number of registers on device.
88 * @name: name of the chip.
89 * @reg: register map of the chip.
90 * @config: configuration of the chip.
91 */
92struct inv_mpu6050_hw {
93 u8 num_reg;
94 u8 *name;
95 const struct inv_mpu6050_reg_map *reg;
96 const struct inv_mpu6050_chip_config *config;
97};
98
99/*
100 * struct inv_mpu6050_state - Driver state variables.
101 * @TIMESTAMP_FIFO_SIZE: fifo size for timestamp.
102 * @trig: IIO trigger.
103 * @chip_config: Cached attribute information.
104 * @reg: Map of important registers.
105 * @hw: Other hardware-specific information.
106 * @chip_type: chip type.
107 * @time_stamp_lock: spin lock to time stamp.
108 * @client: i2c client handle.
109 * @plat_data: platform data.
110 * @timestamps: kfifo queue to store time stamp.
111 */
112struct inv_mpu6050_state {
113#define TIMESTAMP_FIFO_SIZE 16
114 struct iio_trigger *trig;
115 struct inv_mpu6050_chip_config chip_config;
116 const struct inv_mpu6050_reg_map *reg;
117 const struct inv_mpu6050_hw *hw;
118 enum inv_devices chip_type;
119 spinlock_t time_stamp_lock;
120 struct i2c_client *client;
121 struct inv_mpu6050_platform_data plat_data;
122 DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE);
123};
124
125/*register and associated bit definition*/
126#define INV_MPU6050_REG_SAMPLE_RATE_DIV 0x19
127#define INV_MPU6050_REG_CONFIG 0x1A
128#define INV_MPU6050_REG_GYRO_CONFIG 0x1B
129#define INV_MPU6050_REG_ACCEL_CONFIG 0x1C
130
131#define INV_MPU6050_REG_FIFO_EN 0x23
132#define INV_MPU6050_BIT_ACCEL_OUT 0x08
133#define INV_MPU6050_BITS_GYRO_OUT 0x70
134
135#define INV_MPU6050_REG_INT_ENABLE 0x38
136#define INV_MPU6050_BIT_DATA_RDY_EN 0x01
137#define INV_MPU6050_BIT_DMP_INT_EN 0x02
138
139#define INV_MPU6050_REG_RAW_ACCEL 0x3B
140#define INV_MPU6050_REG_TEMPERATURE 0x41
141#define INV_MPU6050_REG_RAW_GYRO 0x43
142
143#define INV_MPU6050_REG_USER_CTRL 0x6A
144#define INV_MPU6050_BIT_FIFO_RST 0x04
145#define INV_MPU6050_BIT_DMP_RST 0x08
146#define INV_MPU6050_BIT_I2C_MST_EN 0x20
147#define INV_MPU6050_BIT_FIFO_EN 0x40
148#define INV_MPU6050_BIT_DMP_EN 0x80
149
150#define INV_MPU6050_REG_PWR_MGMT_1 0x6B
151#define INV_MPU6050_BIT_H_RESET 0x80
152#define INV_MPU6050_BIT_SLEEP 0x40
153#define INV_MPU6050_BIT_CLK_MASK 0x7
154
155#define INV_MPU6050_REG_PWR_MGMT_2 0x6C
156#define INV_MPU6050_BIT_PWR_ACCL_STBY 0x38
157#define INV_MPU6050_BIT_PWR_GYRO_STBY 0x07
158
159#define INV_MPU6050_REG_FIFO_COUNT_H 0x72
160#define INV_MPU6050_REG_FIFO_R_W 0x74
161
162#define INV_MPU6050_BYTES_PER_3AXIS_SENSOR 6
163#define INV_MPU6050_FIFO_COUNT_BYTE 2
164#define INV_MPU6050_FIFO_THRESHOLD 500
165#define INV_MPU6050_POWER_UP_TIME 100
166#define INV_MPU6050_TEMP_UP_TIME 100
167#define INV_MPU6050_SENSOR_UP_TIME 30
168#define INV_MPU6050_REG_UP_TIME 5
169
170#define INV_MPU6050_TEMP_OFFSET 12421
171#define INV_MPU6050_TEMP_SCALE 2941
172#define INV_MPU6050_MAX_GYRO_FS_PARAM 3
173#define INV_MPU6050_MAX_ACCL_FS_PARAM 3
174#define INV_MPU6050_THREE_AXIS 3
175#define INV_MPU6050_GYRO_CONFIG_FSR_SHIFT 3
176#define INV_MPU6050_ACCL_CONFIG_FSR_SHIFT 3
177
178/* 6 + 6 round up and plus 8 */
179#define INV_MPU6050_OUTPUT_DATA_SIZE 24
180
181/* init parameters */
182#define INV_MPU6050_INIT_FIFO_RATE 50
183#define INV_MPU6050_TIME_STAMP_TOR 5
184#define INV_MPU6050_MAX_FIFO_RATE 1000
185#define INV_MPU6050_MIN_FIFO_RATE 4
186#define INV_MPU6050_ONE_K_HZ 1000
187
188/* scan element definition */
189enum inv_mpu6050_scan {
190 INV_MPU6050_SCAN_ACCL_X,
191 INV_MPU6050_SCAN_ACCL_Y,
192 INV_MPU6050_SCAN_ACCL_Z,
193 INV_MPU6050_SCAN_GYRO_X,
194 INV_MPU6050_SCAN_GYRO_Y,
195 INV_MPU6050_SCAN_GYRO_Z,
196 INV_MPU6050_SCAN_TIMESTAMP,
197};
198
199enum inv_mpu6050_filter_e {
200 INV_MPU6050_FILTER_256HZ_NOLPF2 = 0,
201 INV_MPU6050_FILTER_188HZ,
202 INV_MPU6050_FILTER_98HZ,
203 INV_MPU6050_FILTER_42HZ,
204 INV_MPU6050_FILTER_20HZ,
205 INV_MPU6050_FILTER_10HZ,
206 INV_MPU6050_FILTER_5HZ,
207 INV_MPU6050_FILTER_2100HZ_NOLPF,
208 NUM_MPU6050_FILTER
209};
210
211/* IIO attribute address */
212enum INV_MPU6050_IIO_ATTR_ADDR {
213 ATTR_GYRO_MATRIX,
214 ATTR_ACCL_MATRIX,
215};
216
217enum inv_mpu6050_accl_fs_e {
218 INV_MPU6050_FS_02G = 0,
219 INV_MPU6050_FS_04G,
220 INV_MPU6050_FS_08G,
221 INV_MPU6050_FS_16G,
222 NUM_ACCL_FSR
223};
224
225enum inv_mpu6050_fsr_e {
226 INV_MPU6050_FSR_250DPS = 0,
227 INV_MPU6050_FSR_500DPS,
228 INV_MPU6050_FSR_1000DPS,
229 INV_MPU6050_FSR_2000DPS,
230 NUM_MPU6050_FSR
231};
232
233enum inv_mpu6050_clock_sel_e {
234 INV_CLK_INTERNAL = 0,
235 INV_CLK_PLL,
236 NUM_CLK
237};
238
239irqreturn_t inv_mpu6050_irq_handler(int irq, void *p);
240irqreturn_t inv_mpu6050_read_fifo(int irq, void *p);
241int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev);
242void inv_mpu6050_remove_trigger(struct inv_mpu6050_state *st);
243int inv_reset_fifo(struct iio_dev *indio_dev);
244int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask);
245int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 val);
246int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on);
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
new file mode 100644
index 000000000000..331781ffbb15
--- /dev/null
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
@@ -0,0 +1,196 @@
1/*
2* Copyright (C) 2012 Invensense, Inc.
3*
4* This software is licensed under the terms of the GNU General Public
5* License version 2, as published by the Free Software Foundation, and
6* may be copied, distributed, and modified under those terms.
7*
8* This program is distributed in the hope that it will be useful,
9* but WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*/
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/i2c.h>
18#include <linux/err.h>
19#include <linux/delay.h>
20#include <linux/sysfs.h>
21#include <linux/jiffies.h>
22#include <linux/irq.h>
23#include <linux/interrupt.h>
24#include <linux/kfifo.h>
25#include <linux/poll.h>
26#include "inv_mpu_iio.h"
27
28int inv_reset_fifo(struct iio_dev *indio_dev)
29{
30 int result;
31 u8 d;
32 struct inv_mpu6050_state *st = iio_priv(indio_dev);
33
34 /* disable interrupt */
35 result = inv_mpu6050_write_reg(st, st->reg->int_enable, 0);
36 if (result) {
37 dev_err(&st->client->dev, "int_enable failed %d\n", result);
38 return result;
39 }
40 /* disable the sensor output to FIFO */
41 result = inv_mpu6050_write_reg(st, st->reg->fifo_en, 0);
42 if (result)
43 goto reset_fifo_fail;
44 /* disable fifo reading */
45 result = inv_mpu6050_write_reg(st, st->reg->user_ctrl, 0);
46 if (result)
47 goto reset_fifo_fail;
48
49 /* reset FIFO*/
50 result = inv_mpu6050_write_reg(st, st->reg->user_ctrl,
51 INV_MPU6050_BIT_FIFO_RST);
52 if (result)
53 goto reset_fifo_fail;
54 /* enable interrupt */
55 if (st->chip_config.accl_fifo_enable ||
56 st->chip_config.gyro_fifo_enable) {
57 result = inv_mpu6050_write_reg(st, st->reg->int_enable,
58 INV_MPU6050_BIT_DATA_RDY_EN);
59 if (result)
60 return result;
61 }
62 /* enable FIFO reading and I2C master interface*/
63 result = inv_mpu6050_write_reg(st, st->reg->user_ctrl,
64 INV_MPU6050_BIT_FIFO_EN);
65 if (result)
66 goto reset_fifo_fail;
67 /* enable sensor output to FIFO */
68 d = 0;
69 if (st->chip_config.gyro_fifo_enable)
70 d |= INV_MPU6050_BITS_GYRO_OUT;
71 if (st->chip_config.accl_fifo_enable)
72 d |= INV_MPU6050_BIT_ACCEL_OUT;
73 result = inv_mpu6050_write_reg(st, st->reg->fifo_en, d);
74 if (result)
75 goto reset_fifo_fail;
76
77 return 0;
78
79reset_fifo_fail:
80 dev_err(&st->client->dev, "reset fifo failed %d\n", result);
81 result = inv_mpu6050_write_reg(st, st->reg->int_enable,
82 INV_MPU6050_BIT_DATA_RDY_EN);
83
84 return result;
85}
86
87static void inv_clear_kfifo(struct inv_mpu6050_state *st)
88{
89 unsigned long flags;
90
91 /* take the spin lock sem to avoid interrupt kick in */
92 spin_lock_irqsave(&st->time_stamp_lock, flags);
93 kfifo_reset(&st->timestamps);
94 spin_unlock_irqrestore(&st->time_stamp_lock, flags);
95}
96
97/**
98 * inv_mpu6050_irq_handler() - Cache a timestamp at each data ready interrupt.
99 */
100irqreturn_t inv_mpu6050_irq_handler(int irq, void *p)
101{
102 struct iio_poll_func *pf = p;
103 struct iio_dev *indio_dev = pf->indio_dev;
104 struct inv_mpu6050_state *st = iio_priv(indio_dev);
105 s64 timestamp;
106
107 timestamp = iio_get_time_ns();
108 spin_lock(&st->time_stamp_lock);
109 kfifo_in(&st->timestamps, &timestamp, 1);
110 spin_unlock(&st->time_stamp_lock);
111
112 return IRQ_WAKE_THREAD;
113}
114
115/**
116 * inv_mpu6050_read_fifo() - Transfer data from hardware FIFO to KFIFO.
117 */
118irqreturn_t inv_mpu6050_read_fifo(int irq, void *p)
119{
120 struct iio_poll_func *pf = p;
121 struct iio_dev *indio_dev = pf->indio_dev;
122 struct inv_mpu6050_state *st = iio_priv(indio_dev);
123 size_t bytes_per_datum;
124 int result;
125 u8 data[INV_MPU6050_OUTPUT_DATA_SIZE];
126 u16 fifo_count;
127 s64 timestamp;
128 u64 *tmp;
129
130 mutex_lock(&indio_dev->mlock);
131 if (!(st->chip_config.accl_fifo_enable |
132 st->chip_config.gyro_fifo_enable))
133 goto end_session;
134 bytes_per_datum = 0;
135 if (st->chip_config.accl_fifo_enable)
136 bytes_per_datum += INV_MPU6050_BYTES_PER_3AXIS_SENSOR;
137
138 if (st->chip_config.gyro_fifo_enable)
139 bytes_per_datum += INV_MPU6050_BYTES_PER_3AXIS_SENSOR;
140
141 /*
142 * read fifo_count register to know how many bytes inside FIFO
143 * right now
144 */
145 result = i2c_smbus_read_i2c_block_data(st->client,
146 st->reg->fifo_count_h,
147 INV_MPU6050_FIFO_COUNT_BYTE, data);
148 if (result != INV_MPU6050_FIFO_COUNT_BYTE)
149 goto end_session;
150 fifo_count = be16_to_cpup((__be16 *)(&data[0]));
151 if (fifo_count < bytes_per_datum)
152 goto end_session;
153 /* fifo count can't be odd number, if it is odd, reset fifo*/
154 if (fifo_count & 1)
155 goto flush_fifo;
156 if (fifo_count > INV_MPU6050_FIFO_THRESHOLD)
157 goto flush_fifo;
158 /* Timestamp mismatch. */
159 if (kfifo_len(&st->timestamps) >
160 fifo_count / bytes_per_datum + INV_MPU6050_TIME_STAMP_TOR)
161 goto flush_fifo;
162 while (fifo_count >= bytes_per_datum) {
163 result = i2c_smbus_read_i2c_block_data(st->client,
164 st->reg->fifo_r_w,
165 bytes_per_datum, data);
166 if (result != bytes_per_datum)
167 goto flush_fifo;
168
169 result = kfifo_out(&st->timestamps, &timestamp, 1);
170 /* when there is no timestamp, put timestamp as 0 */
171 if (0 == result)
172 timestamp = 0;
173
174 tmp = (u64 *)data;
175 tmp[DIV_ROUND_UP(bytes_per_datum, 8)] = timestamp;
176 result = iio_push_to_buffers(indio_dev, data);
177 if (result)
178 goto flush_fifo;
179 fifo_count -= bytes_per_datum;
180 }
181
182end_session:
183 mutex_unlock(&indio_dev->mlock);
184 iio_trigger_notify_done(indio_dev->trig);
185
186 return IRQ_HANDLED;
187
188flush_fifo:
189 /* Flush HW and SW FIFOs. */
190 inv_reset_fifo(indio_dev);
191 inv_clear_kfifo(st);
192 mutex_unlock(&indio_dev->mlock);
193 iio_trigger_notify_done(indio_dev->trig);
194
195 return IRQ_HANDLED;
196}
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c
new file mode 100644
index 000000000000..e1d0869e0ad1
--- /dev/null
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c
@@ -0,0 +1,155 @@
1/*
2* Copyright (C) 2012 Invensense, Inc.
3*
4* This software is licensed under the terms of the GNU General Public
5* License version 2, as published by the Free Software Foundation, and
6* may be copied, distributed, and modified under those terms.
7*
8* This program is distributed in the hope that it will be useful,
9* but WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*/
13
14#include "inv_mpu_iio.h"
15
16static void inv_scan_query(struct iio_dev *indio_dev)
17{
18 struct inv_mpu6050_state *st = iio_priv(indio_dev);
19
20 st->chip_config.gyro_fifo_enable =
21 test_bit(INV_MPU6050_SCAN_GYRO_X,
22 indio_dev->active_scan_mask) ||
23 test_bit(INV_MPU6050_SCAN_GYRO_Y,
24 indio_dev->active_scan_mask) ||
25 test_bit(INV_MPU6050_SCAN_GYRO_Z,
26 indio_dev->active_scan_mask);
27
28 st->chip_config.accl_fifo_enable =
29 test_bit(INV_MPU6050_SCAN_ACCL_X,
30 indio_dev->active_scan_mask) ||
31 test_bit(INV_MPU6050_SCAN_ACCL_Y,
32 indio_dev->active_scan_mask) ||
33 test_bit(INV_MPU6050_SCAN_ACCL_Z,
34 indio_dev->active_scan_mask);
35}
36
37/**
38 * inv_mpu6050_set_enable() - enable chip functions.
39 * @indio_dev: Device driver instance.
40 * @enable: enable/disable
41 */
42static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
43{
44 struct inv_mpu6050_state *st = iio_priv(indio_dev);
45 int result;
46
47 if (enable) {
48 result = inv_mpu6050_set_power_itg(st, true);
49 if (result)
50 return result;
51 inv_scan_query(indio_dev);
52 if (st->chip_config.gyro_fifo_enable) {
53 result = inv_mpu6050_switch_engine(st, true,
54 INV_MPU6050_BIT_PWR_GYRO_STBY);
55 if (result)
56 return result;
57 }
58 if (st->chip_config.accl_fifo_enable) {
59 result = inv_mpu6050_switch_engine(st, true,
60 INV_MPU6050_BIT_PWR_ACCL_STBY);
61 if (result)
62 return result;
63 }
64 result = inv_reset_fifo(indio_dev);
65 if (result)
66 return result;
67 } else {
68 result = inv_mpu6050_write_reg(st, st->reg->fifo_en, 0);
69 if (result)
70 return result;
71
72 result = inv_mpu6050_write_reg(st, st->reg->int_enable, 0);
73 if (result)
74 return result;
75
76 result = inv_mpu6050_write_reg(st, st->reg->user_ctrl, 0);
77 if (result)
78 return result;
79
80 result = inv_mpu6050_switch_engine(st, false,
81 INV_MPU6050_BIT_PWR_GYRO_STBY);
82 if (result)
83 return result;
84
85 result = inv_mpu6050_switch_engine(st, false,
86 INV_MPU6050_BIT_PWR_ACCL_STBY);
87 if (result)
88 return result;
89 result = inv_mpu6050_set_power_itg(st, false);
90 if (result)
91 return result;
92 }
93 st->chip_config.enable = enable;
94
95 return 0;
96}
97
98/**
99 * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
100 * @trig: Trigger instance
101 * @state: Desired trigger state
102 */
103static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
104 bool state)
105{
106 return inv_mpu6050_set_enable(trig->private_data, state);
107}
108
109static const struct iio_trigger_ops inv_mpu_trigger_ops = {
110 .owner = THIS_MODULE,
111 .set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
112};
113
114int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev)
115{
116 int ret;
117 struct inv_mpu6050_state *st = iio_priv(indio_dev);
118
119 st->trig = iio_trigger_alloc("%s-dev%d",
120 indio_dev->name,
121 indio_dev->id);
122 if (st->trig == NULL) {
123 ret = -ENOMEM;
124 goto error_ret;
125 }
126 ret = request_irq(st->client->irq, &iio_trigger_generic_data_rdy_poll,
127 IRQF_TRIGGER_RISING,
128 "inv_mpu",
129 st->trig);
130 if (ret)
131 goto error_free_trig;
132 st->trig->dev.parent = &st->client->dev;
133 st->trig->private_data = indio_dev;
134 st->trig->ops = &inv_mpu_trigger_ops;
135 ret = iio_trigger_register(st->trig);
136 if (ret)
137 goto error_free_irq;
138 indio_dev->trig = st->trig;
139
140 return 0;
141
142error_free_irq:
143 free_irq(st->client->irq, st->trig);
144error_free_trig:
145 iio_trigger_free(st->trig);
146error_ret:
147 return ret;
148}
149
150void inv_mpu6050_remove_trigger(struct inv_mpu6050_state *st)
151{
152 iio_trigger_unregister(st->trig);
153 free_irq(st->client->irq, st->trig);
154 iio_trigger_free(st->trig);
155}
diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 4fe0ead84213..4d6c7d84e155 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -160,7 +160,7 @@ void iio_trigger_notify_done(struct iio_trigger *trig)
160 trig->use_count--; 160 trig->use_count--;
161 if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable) 161 if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable)
162 if (trig->ops->try_reenable(trig)) 162 if (trig->ops->try_reenable(trig))
163 /* Missed and interrupt so launch new poll now */ 163 /* Missed an interrupt so launch new poll now */
164 iio_trigger_poll(trig, 0); 164 iio_trigger_poll(trig, 0);
165} 165}
166EXPORT_SYMBOL(iio_trigger_notify_done); 166EXPORT_SYMBOL(iio_trigger_notify_done);
@@ -193,7 +193,7 @@ static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
193 * This is not currently handled. Alternative of not enabling trigger unless 193 * This is not currently handled. Alternative of not enabling trigger unless
194 * the relevant function is in there may be the best option. 194 * the relevant function is in there may be the best option.
195 */ 195 */
196/* Worth protecting against double additions?*/ 196/* Worth protecting against double additions? */
197static int iio_trigger_attach_poll_func(struct iio_trigger *trig, 197static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
198 struct iio_poll_func *pf) 198 struct iio_poll_func *pf)
199{ 199{
@@ -201,7 +201,7 @@ static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
201 bool notinuse 201 bool notinuse
202 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER); 202 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
203 203
204 /* Prevent the module being removed whilst attached to a trigger */ 204 /* Prevent the module from being removed whilst attached to a trigger */
205 __module_get(pf->indio_dev->info->driver_module); 205 __module_get(pf->indio_dev->info->driver_module);
206 pf->irq = iio_trigger_get_irq(trig); 206 pf->irq = iio_trigger_get_irq(trig);
207 ret = request_threaded_irq(pf->irq, pf->h, pf->thread, 207 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
@@ -288,7 +288,7 @@ void iio_dealloc_pollfunc(struct iio_poll_func *pf)
288EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc); 288EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
289 289
290/** 290/**
291 * iio_trigger_read_current() - trigger consumer sysfs query which trigger 291 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
292 * 292 *
293 * For trigger consumers the current_trigger interface allows the trigger 293 * For trigger consumers the current_trigger interface allows the trigger
294 * used by the device to be queried. 294 * used by the device to be queried.
@@ -305,7 +305,7 @@ static ssize_t iio_trigger_read_current(struct device *dev,
305} 305}
306 306
307/** 307/**
308 * iio_trigger_write_current() trigger consumer sysfs set current trigger 308 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
309 * 309 *
310 * For trigger consumers the current_trigger interface allows the trigger 310 * For trigger consumers the current_trigger interface allows the trigger
311 * used for this device to be specified at run time based on the triggers 311 * used for this device to be specified at run time based on the triggers
@@ -476,7 +476,7 @@ void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
476 476
477void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev) 477void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
478{ 478{
479 /* Clean up and associated but not attached triggers references */ 479 /* Clean up an associated but not attached trigger reference */
480 if (indio_dev->trig) 480 if (indio_dev->trig)
481 iio_trigger_put(indio_dev->trig); 481 iio_trigger_put(indio_dev->trig);
482} 482}
diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index d55e98fb300e..b289915b8469 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -54,39 +54,25 @@ error_ret:
54EXPORT_SYMBOL_GPL(iio_map_array_register); 54EXPORT_SYMBOL_GPL(iio_map_array_register);
55 55
56 56
57/* Assumes the exact same array (e.g. memory locations) 57/*
58 * used at unregistration as used at registration rather than 58 * Remove all map entries associated with the given iio device
59 * more complex checking of contents.
60 */ 59 */
61int iio_map_array_unregister(struct iio_dev *indio_dev, 60int iio_map_array_unregister(struct iio_dev *indio_dev)
62 struct iio_map *maps)
63{ 61{
64 int i = 0, ret = 0; 62 int ret = -ENODEV;
65 bool found_it;
66 struct iio_map_internal *mapi; 63 struct iio_map_internal *mapi;
67 64 struct list_head *pos, *tmp;
68 if (maps == NULL)
69 return 0;
70 65
71 mutex_lock(&iio_map_list_lock); 66 mutex_lock(&iio_map_list_lock);
72 while (maps[i].consumer_dev_name != NULL) { 67 list_for_each_safe(pos, tmp, &iio_map_list) {
73 found_it = false; 68 mapi = list_entry(pos, struct iio_map_internal, l);
74 list_for_each_entry(mapi, &iio_map_list, l) 69 if (indio_dev == mapi->indio_dev) {
75 if (&maps[i] == mapi->map) { 70 list_del(&mapi->l);
76 list_del(&mapi->l); 71 kfree(mapi);
77 kfree(mapi); 72 ret = 0;
78 found_it = true;
79 break;
80 }
81 if (!found_it) {
82 ret = -ENODEV;
83 goto error_ret;
84 } 73 }
85 i++;
86 } 74 }
87error_ret:
88 mutex_unlock(&iio_map_list_lock); 75 mutex_unlock(&iio_map_list_lock);
89
90 return ret; 76 return ret;
91} 77}
92EXPORT_SYMBOL_GPL(iio_map_array_unregister); 78EXPORT_SYMBOL_GPL(iio_map_array_unregister);
@@ -107,7 +93,8 @@ static const struct iio_chan_spec
107} 93}
108 94
109 95
110struct iio_channel *iio_channel_get(const char *name, const char *channel_name) 96static struct iio_channel *iio_channel_get_sys(const char *name,
97 const char *channel_name)
111{ 98{
112 struct iio_map_internal *c_i = NULL, *c = NULL; 99 struct iio_map_internal *c_i = NULL, *c = NULL;
113 struct iio_channel *channel; 100 struct iio_channel *channel;
@@ -158,6 +145,14 @@ error_no_mem:
158 iio_device_put(c->indio_dev); 145 iio_device_put(c->indio_dev);
159 return ERR_PTR(err); 146 return ERR_PTR(err);
160} 147}
148
149struct iio_channel *iio_channel_get(struct device *dev,
150 const char *channel_name)
151{
152 const char *name = dev ? dev_name(dev) : NULL;
153
154 return iio_channel_get_sys(name, channel_name);
155}
161EXPORT_SYMBOL_GPL(iio_channel_get); 156EXPORT_SYMBOL_GPL(iio_channel_get);
162 157
163void iio_channel_release(struct iio_channel *channel) 158void iio_channel_release(struct iio_channel *channel)
@@ -167,16 +162,18 @@ void iio_channel_release(struct iio_channel *channel)
167} 162}
168EXPORT_SYMBOL_GPL(iio_channel_release); 163EXPORT_SYMBOL_GPL(iio_channel_release);
169 164
170struct iio_channel *iio_channel_get_all(const char *name) 165struct iio_channel *iio_channel_get_all(struct device *dev)
171{ 166{
167 const char *name;
172 struct iio_channel *chans; 168 struct iio_channel *chans;
173 struct iio_map_internal *c = NULL; 169 struct iio_map_internal *c = NULL;
174 int nummaps = 0; 170 int nummaps = 0;
175 int mapind = 0; 171 int mapind = 0;
176 int i, ret; 172 int i, ret;
177 173
178 if (name == NULL) 174 if (dev == NULL)
179 return ERR_PTR(-EINVAL); 175 return ERR_PTR(-EINVAL);
176 name = dev_name(dev);
180 177
181 mutex_lock(&iio_map_list_lock); 178 mutex_lock(&iio_map_list_lock);
182 /* first count the matching maps */ 179 /* first count the matching maps */
diff --git a/drivers/iio/kfifo_buf.c b/drivers/iio/kfifo_buf.c
index 5bc5c860e9ca..a923c78d5cb4 100644
--- a/drivers/iio/kfifo_buf.c
+++ b/drivers/iio/kfifo_buf.c
@@ -22,7 +22,6 @@ static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
22 if ((length == 0) || (bytes_per_datum == 0)) 22 if ((length == 0) || (bytes_per_datum == 0))
23 return -EINVAL; 23 return -EINVAL;
24 24
25 __iio_update_buffer(&buf->buffer, bytes_per_datum, length);
26 return __kfifo_alloc((struct __kfifo *)&buf->kf, length, 25 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
27 bytes_per_datum, GFP_KERNEL); 26 bytes_per_datum, GFP_KERNEL);
28} 27}
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index dbf80abc834f..5ef1a396e0c9 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -32,6 +32,16 @@ config SENSORS_LM3533
32 changes. The ALS-control output values can be set per zone for the 32 changes. The ALS-control output values can be set per zone for the
33 three current output channels. 33 three current output channels.
34 34
35config SENSORS_TSL2563
36 tristate "TAOS TSL2560, TSL2561, TSL2562 and TSL2563 ambient light sensors"
37 depends on I2C
38 help
39 If you say yes here you get support for the Taos TSL2560,
40 TSL2561, TSL2562 and TSL2563 ambient light sensors.
41
42 This driver can also be built as a module. If so, the module
43 will be called tsl2563.
44
35config VCNL4000 45config VCNL4000
36 tristate "VCNL4000 combined ALS and proximity sensor" 46 tristate "VCNL4000 combined ALS and proximity sensor"
37 depends on I2C 47 depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 21a8f0df1407..040d9c75f8e6 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -4,5 +4,6 @@
4 4
5obj-$(CONFIG_ADJD_S311) += adjd_s311.o 5obj-$(CONFIG_ADJD_S311) += adjd_s311.o
6obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o 6obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o
7obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o
7obj-$(CONFIG_VCNL4000) += vcnl4000.o 8obj-$(CONFIG_VCNL4000) += vcnl4000.o
8obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o 9obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o
diff --git a/drivers/iio/light/hid-sensor-als.c b/drivers/iio/light/hid-sensor-als.c
index e2d042f2a544..3d7e8c9b4beb 100644
--- a/drivers/iio/light/hid-sensor-als.c
+++ b/drivers/iio/light/hid-sensor-als.c
@@ -28,7 +28,6 @@
28#include <linux/iio/buffer.h> 28#include <linux/iio/buffer.h>
29#include <linux/iio/trigger_consumer.h> 29#include <linux/iio/trigger_consumer.h>
30#include <linux/iio/triggered_buffer.h> 30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-attributes.h"
32#include "../common/hid-sensors/hid-sensor-trigger.h" 31#include "../common/hid-sensors/hid-sensor-trigger.h"
33 32
34/*Format: HID-SENSOR-usage_id_in_hex*/ 33/*Format: HID-SENSOR-usage_id_in_hex*/
@@ -39,7 +38,7 @@
39 38
40struct als_state { 39struct als_state {
41 struct hid_sensor_hub_callbacks callbacks; 40 struct hid_sensor_hub_callbacks callbacks;
42 struct hid_sensor_iio_common common_attributes; 41 struct hid_sensor_common common_attributes;
43 struct hid_sensor_hub_attribute_info als_illum; 42 struct hid_sensor_hub_attribute_info als_illum;
44 u32 illum; 43 u32 illum;
45}; 44};
diff --git a/drivers/iio/light/tsl2563.c b/drivers/iio/light/tsl2563.c
new file mode 100644
index 000000000000..fd8be69b7d05
--- /dev/null
+++ b/drivers/iio/light/tsl2563.c
@@ -0,0 +1,887 @@
1/*
2 * drivers/iio/light/tsl2563.c
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
6 * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
7 * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
8 *
9 * Converted to IIO driver
10 * Amit Kucheria <amit.kucheria@verdurent.com>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * version 2 as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 * 02110-1301 USA
25 */
26
27#include <linux/module.h>
28#include <linux/i2c.h>
29#include <linux/interrupt.h>
30#include <linux/irq.h>
31#include <linux/sched.h>
32#include <linux/mutex.h>
33#include <linux/delay.h>
34#include <linux/pm.h>
35#include <linux/err.h>
36#include <linux/slab.h>
37
38#include <linux/iio/iio.h>
39#include <linux/iio/sysfs.h>
40#include <linux/iio/events.h>
41#include <linux/platform_data/tsl2563.h>
42
43/* Use this many bits for fraction part. */
44#define ADC_FRAC_BITS 14
45
46/* Given number of 1/10000's in ADC_FRAC_BITS precision. */
47#define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
48
49/* Bits used for fraction in calibration coefficients.*/
50#define CALIB_FRAC_BITS 10
51/* 0.5 in CALIB_FRAC_BITS precision */
52#define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1))
53/* Make a fraction from a number n that was multiplied with b. */
54#define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b))
55/* Decimal 10^(digits in sysfs presentation) */
56#define CALIB_BASE_SYSFS 1000
57
58#define TSL2563_CMD 0x80
59#define TSL2563_CLEARINT 0x40
60
61#define TSL2563_REG_CTRL 0x00
62#define TSL2563_REG_TIMING 0x01
63#define TSL2563_REG_LOWLOW 0x02 /* data0 low threshold, 2 bytes */
64#define TSL2563_REG_LOWHIGH 0x03
65#define TSL2563_REG_HIGHLOW 0x04 /* data0 high threshold, 2 bytes */
66#define TSL2563_REG_HIGHHIGH 0x05
67#define TSL2563_REG_INT 0x06
68#define TSL2563_REG_ID 0x0a
69#define TSL2563_REG_DATA0LOW 0x0c /* broadband sensor value, 2 bytes */
70#define TSL2563_REG_DATA0HIGH 0x0d
71#define TSL2563_REG_DATA1LOW 0x0e /* infrared sensor value, 2 bytes */
72#define TSL2563_REG_DATA1HIGH 0x0f
73
74#define TSL2563_CMD_POWER_ON 0x03
75#define TSL2563_CMD_POWER_OFF 0x00
76#define TSL2563_CTRL_POWER_MASK 0x03
77
78#define TSL2563_TIMING_13MS 0x00
79#define TSL2563_TIMING_100MS 0x01
80#define TSL2563_TIMING_400MS 0x02
81#define TSL2563_TIMING_MASK 0x03
82#define TSL2563_TIMING_GAIN16 0x10
83#define TSL2563_TIMING_GAIN1 0x00
84
85#define TSL2563_INT_DISBLED 0x00
86#define TSL2563_INT_LEVEL 0x10
87#define TSL2563_INT_PERSIST(n) ((n) & 0x0F)
88
89struct tsl2563_gainlevel_coeff {
90 u8 gaintime;
91 u16 min;
92 u16 max;
93};
94
95static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
96 {
97 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
98 .min = 0,
99 .max = 65534,
100 }, {
101 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
102 .min = 2048,
103 .max = 65534,
104 }, {
105 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
106 .min = 4095,
107 .max = 37177,
108 }, {
109 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
110 .min = 3000,
111 .max = 65535,
112 },
113};
114
115struct tsl2563_chip {
116 struct mutex lock;
117 struct i2c_client *client;
118 struct delayed_work poweroff_work;
119
120 /* Remember state for suspend and resume functions */
121 bool suspended;
122
123 struct tsl2563_gainlevel_coeff const *gainlevel;
124
125 u16 low_thres;
126 u16 high_thres;
127 u8 intr;
128 bool int_enabled;
129
130 /* Calibration coefficients */
131 u32 calib0;
132 u32 calib1;
133 int cover_comp_gain;
134
135 /* Cache current values, to be returned while suspended */
136 u32 data0;
137 u32 data1;
138};
139
140static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
141{
142 struct i2c_client *client = chip->client;
143 u8 cmd;
144
145 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
146 return i2c_smbus_write_byte_data(client,
147 TSL2563_CMD | TSL2563_REG_CTRL, cmd);
148}
149
150/*
151 * Return value is 0 for off, 1 for on, or a negative error
152 * code if reading failed.
153 */
154static int tsl2563_get_power(struct tsl2563_chip *chip)
155{
156 struct i2c_client *client = chip->client;
157 int ret;
158
159 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
160 if (ret < 0)
161 return ret;
162
163 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
164}
165
166static int tsl2563_configure(struct tsl2563_chip *chip)
167{
168 int ret;
169
170 ret = i2c_smbus_write_byte_data(chip->client,
171 TSL2563_CMD | TSL2563_REG_TIMING,
172 chip->gainlevel->gaintime);
173 if (ret)
174 goto error_ret;
175 ret = i2c_smbus_write_byte_data(chip->client,
176 TSL2563_CMD | TSL2563_REG_HIGHLOW,
177 chip->high_thres & 0xFF);
178 if (ret)
179 goto error_ret;
180 ret = i2c_smbus_write_byte_data(chip->client,
181 TSL2563_CMD | TSL2563_REG_HIGHHIGH,
182 (chip->high_thres >> 8) & 0xFF);
183 if (ret)
184 goto error_ret;
185 ret = i2c_smbus_write_byte_data(chip->client,
186 TSL2563_CMD | TSL2563_REG_LOWLOW,
187 chip->low_thres & 0xFF);
188 if (ret)
189 goto error_ret;
190 ret = i2c_smbus_write_byte_data(chip->client,
191 TSL2563_CMD | TSL2563_REG_LOWHIGH,
192 (chip->low_thres >> 8) & 0xFF);
193/*
194 * Interrupt register is automatically written anyway if it is relevant
195 * so is not here.
196 */
197error_ret:
198 return ret;
199}
200
201static void tsl2563_poweroff_work(struct work_struct *work)
202{
203 struct tsl2563_chip *chip =
204 container_of(work, struct tsl2563_chip, poweroff_work.work);
205 tsl2563_set_power(chip, 0);
206}
207
208static int tsl2563_detect(struct tsl2563_chip *chip)
209{
210 int ret;
211
212 ret = tsl2563_set_power(chip, 1);
213 if (ret)
214 return ret;
215
216 ret = tsl2563_get_power(chip);
217 if (ret < 0)
218 return ret;
219
220 return ret ? 0 : -ENODEV;
221}
222
223static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
224{
225 struct i2c_client *client = chip->client;
226 int ret;
227
228 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
229 if (ret < 0)
230 return ret;
231
232 *id = ret;
233
234 return 0;
235}
236
237/*
238 * "Normalized" ADC value is one obtained with 400ms of integration time and
239 * 16x gain. This function returns the number of bits of shift needed to
240 * convert between normalized values and HW values obtained using given
241 * timing and gain settings.
242 */
243static int adc_shiftbits(u8 timing)
244{
245 int shift = 0;
246
247 switch (timing & TSL2563_TIMING_MASK) {
248 case TSL2563_TIMING_13MS:
249 shift += 5;
250 break;
251 case TSL2563_TIMING_100MS:
252 shift += 2;
253 break;
254 case TSL2563_TIMING_400MS:
255 /* no-op */
256 break;
257 }
258
259 if (!(timing & TSL2563_TIMING_GAIN16))
260 shift += 4;
261
262 return shift;
263}
264
265/* Convert a HW ADC value to normalized scale. */
266static u32 normalize_adc(u16 adc, u8 timing)
267{
268 return adc << adc_shiftbits(timing);
269}
270
271static void tsl2563_wait_adc(struct tsl2563_chip *chip)
272{
273 unsigned int delay;
274
275 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
276 case TSL2563_TIMING_13MS:
277 delay = 14;
278 break;
279 case TSL2563_TIMING_100MS:
280 delay = 101;
281 break;
282 default:
283 delay = 402;
284 }
285 /*
286 * TODO: Make sure that we wait at least required delay but why we
287 * have to extend it one tick more?
288 */
289 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
290}
291
292static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
293{
294 struct i2c_client *client = chip->client;
295
296 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
297
298 (adc > chip->gainlevel->max) ?
299 chip->gainlevel++ : chip->gainlevel--;
300
301 i2c_smbus_write_byte_data(client,
302 TSL2563_CMD | TSL2563_REG_TIMING,
303 chip->gainlevel->gaintime);
304
305 tsl2563_wait_adc(chip);
306 tsl2563_wait_adc(chip);
307
308 return 1;
309 } else
310 return 0;
311}
312
313static int tsl2563_get_adc(struct tsl2563_chip *chip)
314{
315 struct i2c_client *client = chip->client;
316 u16 adc0, adc1;
317 int retry = 1;
318 int ret = 0;
319
320 if (chip->suspended)
321 goto out;
322
323 if (!chip->int_enabled) {
324 cancel_delayed_work(&chip->poweroff_work);
325
326 if (!tsl2563_get_power(chip)) {
327 ret = tsl2563_set_power(chip, 1);
328 if (ret)
329 goto out;
330 ret = tsl2563_configure(chip);
331 if (ret)
332 goto out;
333 tsl2563_wait_adc(chip);
334 }
335 }
336
337 while (retry) {
338 ret = i2c_smbus_read_word_data(client,
339 TSL2563_CMD | TSL2563_REG_DATA0LOW);
340 if (ret < 0)
341 goto out;
342 adc0 = ret;
343
344 ret = i2c_smbus_read_word_data(client,
345 TSL2563_CMD | TSL2563_REG_DATA1LOW);
346 if (ret < 0)
347 goto out;
348 adc1 = ret;
349
350 retry = tsl2563_adjust_gainlevel(chip, adc0);
351 }
352
353 chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
354 chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
355
356 if (!chip->int_enabled)
357 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
358
359 ret = 0;
360out:
361 return ret;
362}
363
364static inline int calib_to_sysfs(u32 calib)
365{
366 return (int) (((calib * CALIB_BASE_SYSFS) +
367 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
368}
369
370static inline u32 calib_from_sysfs(int value)
371{
372 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
373}
374
375/*
376 * Conversions between lux and ADC values.
377 *
378 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
379 * appropriate constants. Different constants are needed for different
380 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
381 * of the intensities in infrared and visible wavelengths). lux_table below
382 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
383 * constants.
384 */
385
386struct tsl2563_lux_coeff {
387 unsigned long ch_ratio;
388 unsigned long ch0_coeff;
389 unsigned long ch1_coeff;
390};
391
392static const struct tsl2563_lux_coeff lux_table[] = {
393 {
394 .ch_ratio = FRAC10K(1300),
395 .ch0_coeff = FRAC10K(315),
396 .ch1_coeff = FRAC10K(262),
397 }, {
398 .ch_ratio = FRAC10K(2600),
399 .ch0_coeff = FRAC10K(337),
400 .ch1_coeff = FRAC10K(430),
401 }, {
402 .ch_ratio = FRAC10K(3900),
403 .ch0_coeff = FRAC10K(363),
404 .ch1_coeff = FRAC10K(529),
405 }, {
406 .ch_ratio = FRAC10K(5200),
407 .ch0_coeff = FRAC10K(392),
408 .ch1_coeff = FRAC10K(605),
409 }, {
410 .ch_ratio = FRAC10K(6500),
411 .ch0_coeff = FRAC10K(229),
412 .ch1_coeff = FRAC10K(291),
413 }, {
414 .ch_ratio = FRAC10K(8000),
415 .ch0_coeff = FRAC10K(157),
416 .ch1_coeff = FRAC10K(180),
417 }, {
418 .ch_ratio = FRAC10K(13000),
419 .ch0_coeff = FRAC10K(34),
420 .ch1_coeff = FRAC10K(26),
421 }, {
422 .ch_ratio = ULONG_MAX,
423 .ch0_coeff = 0,
424 .ch1_coeff = 0,
425 },
426};
427
428/* Convert normalized, scaled ADC values to lux. */
429static unsigned int adc_to_lux(u32 adc0, u32 adc1)
430{
431 const struct tsl2563_lux_coeff *lp = lux_table;
432 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
433
434 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
435
436 while (lp->ch_ratio < ratio)
437 lp++;
438
439 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
440
441 return (unsigned int) (lux >> ADC_FRAC_BITS);
442}
443
444/* Apply calibration coefficient to ADC count. */
445static u32 calib_adc(u32 adc, u32 calib)
446{
447 unsigned long scaled = adc;
448
449 scaled *= calib;
450 scaled >>= CALIB_FRAC_BITS;
451
452 return (u32) scaled;
453}
454
455static int tsl2563_write_raw(struct iio_dev *indio_dev,
456 struct iio_chan_spec const *chan,
457 int val,
458 int val2,
459 long mask)
460{
461 struct tsl2563_chip *chip = iio_priv(indio_dev);
462
463 if (chan->channel == IIO_MOD_LIGHT_BOTH)
464 chip->calib0 = calib_from_sysfs(val);
465 else
466 chip->calib1 = calib_from_sysfs(val);
467
468 return 0;
469}
470
471static int tsl2563_read_raw(struct iio_dev *indio_dev,
472 struct iio_chan_spec const *chan,
473 int *val,
474 int *val2,
475 long m)
476{
477 int ret = -EINVAL;
478 u32 calib0, calib1;
479 struct tsl2563_chip *chip = iio_priv(indio_dev);
480
481 mutex_lock(&chip->lock);
482 switch (m) {
483 case IIO_CHAN_INFO_RAW:
484 case IIO_CHAN_INFO_PROCESSED:
485 switch (chan->type) {
486 case IIO_LIGHT:
487 ret = tsl2563_get_adc(chip);
488 if (ret)
489 goto error_ret;
490 calib0 = calib_adc(chip->data0, chip->calib0) *
491 chip->cover_comp_gain;
492 calib1 = calib_adc(chip->data1, chip->calib1) *
493 chip->cover_comp_gain;
494 *val = adc_to_lux(calib0, calib1);
495 ret = IIO_VAL_INT;
496 break;
497 case IIO_INTENSITY:
498 ret = tsl2563_get_adc(chip);
499 if (ret)
500 goto error_ret;
501 if (chan->channel == 0)
502 *val = chip->data0;
503 else
504 *val = chip->data1;
505 ret = IIO_VAL_INT;
506 break;
507 default:
508 break;
509 }
510 break;
511
512 case IIO_CHAN_INFO_CALIBSCALE:
513 if (chan->channel == 0)
514 *val = calib_to_sysfs(chip->calib0);
515 else
516 *val = calib_to_sysfs(chip->calib1);
517 ret = IIO_VAL_INT;
518 break;
519 default:
520 ret = -EINVAL;
521 goto error_ret;
522 }
523
524error_ret:
525 mutex_unlock(&chip->lock);
526 return ret;
527}
528
529static const struct iio_chan_spec tsl2563_channels[] = {
530 {
531 .type = IIO_LIGHT,
532 .indexed = 1,
533 .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT,
534 .channel = 0,
535 }, {
536 .type = IIO_INTENSITY,
537 .modified = 1,
538 .channel2 = IIO_MOD_LIGHT_BOTH,
539 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
540 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
541 .event_mask = (IIO_EV_BIT(IIO_EV_TYPE_THRESH,
542 IIO_EV_DIR_RISING) |
543 IIO_EV_BIT(IIO_EV_TYPE_THRESH,
544 IIO_EV_DIR_FALLING)),
545 }, {
546 .type = IIO_INTENSITY,
547 .modified = 1,
548 .channel2 = IIO_MOD_LIGHT_IR,
549 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
550 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
551 }
552};
553
554static int tsl2563_read_thresh(struct iio_dev *indio_dev,
555 u64 event_code,
556 int *val)
557{
558 struct tsl2563_chip *chip = iio_priv(indio_dev);
559
560 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
561 case IIO_EV_DIR_RISING:
562 *val = chip->high_thres;
563 break;
564 case IIO_EV_DIR_FALLING:
565 *val = chip->low_thres;
566 break;
567 default:
568 return -EINVAL;
569 }
570
571 return 0;
572}
573
574static int tsl2563_write_thresh(struct iio_dev *indio_dev,
575 u64 event_code,
576 int val)
577{
578 struct tsl2563_chip *chip = iio_priv(indio_dev);
579 int ret;
580 u8 address;
581
582 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
583 address = TSL2563_REG_HIGHLOW;
584 else
585 address = TSL2563_REG_LOWLOW;
586 mutex_lock(&chip->lock);
587 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
588 val & 0xFF);
589 if (ret)
590 goto error_ret;
591 ret = i2c_smbus_write_byte_data(chip->client,
592 TSL2563_CMD | (address + 1),
593 (val >> 8) & 0xFF);
594 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
595 chip->high_thres = val;
596 else
597 chip->low_thres = val;
598
599error_ret:
600 mutex_unlock(&chip->lock);
601
602 return ret;
603}
604
605static irqreturn_t tsl2563_event_handler(int irq, void *private)
606{
607 struct iio_dev *dev_info = private;
608 struct tsl2563_chip *chip = iio_priv(dev_info);
609
610 iio_push_event(dev_info,
611 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
612 0,
613 IIO_EV_TYPE_THRESH,
614 IIO_EV_DIR_EITHER),
615 iio_get_time_ns());
616
617 /* clear the interrupt and push the event */
618 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
619 return IRQ_HANDLED;
620}
621
622static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
623 u64 event_code,
624 int state)
625{
626 struct tsl2563_chip *chip = iio_priv(indio_dev);
627 int ret = 0;
628
629 mutex_lock(&chip->lock);
630 if (state && !(chip->intr & 0x30)) {
631 chip->intr &= ~0x30;
632 chip->intr |= 0x10;
633 /* ensure the chip is actually on */
634 cancel_delayed_work(&chip->poweroff_work);
635 if (!tsl2563_get_power(chip)) {
636 ret = tsl2563_set_power(chip, 1);
637 if (ret)
638 goto out;
639 ret = tsl2563_configure(chip);
640 if (ret)
641 goto out;
642 }
643 ret = i2c_smbus_write_byte_data(chip->client,
644 TSL2563_CMD | TSL2563_REG_INT,
645 chip->intr);
646 chip->int_enabled = true;
647 }
648
649 if (!state && (chip->intr & 0x30)) {
650 chip->intr &= ~0x30;
651 ret = i2c_smbus_write_byte_data(chip->client,
652 TSL2563_CMD | TSL2563_REG_INT,
653 chip->intr);
654 chip->int_enabled = false;
655 /* now the interrupt is not enabled, we can go to sleep */
656 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
657 }
658out:
659 mutex_unlock(&chip->lock);
660
661 return ret;
662}
663
664static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
665 u64 event_code)
666{
667 struct tsl2563_chip *chip = iio_priv(indio_dev);
668 int ret;
669
670 mutex_lock(&chip->lock);
671 ret = i2c_smbus_read_byte_data(chip->client,
672 TSL2563_CMD | TSL2563_REG_INT);
673 mutex_unlock(&chip->lock);
674 if (ret < 0)
675 return ret;
676
677 return !!(ret & 0x30);
678}
679
680static const struct iio_info tsl2563_info_no_irq = {
681 .driver_module = THIS_MODULE,
682 .read_raw = &tsl2563_read_raw,
683 .write_raw = &tsl2563_write_raw,
684};
685
686static const struct iio_info tsl2563_info = {
687 .driver_module = THIS_MODULE,
688 .read_raw = &tsl2563_read_raw,
689 .write_raw = &tsl2563_write_raw,
690 .read_event_value = &tsl2563_read_thresh,
691 .write_event_value = &tsl2563_write_thresh,
692 .read_event_config = &tsl2563_read_interrupt_config,
693 .write_event_config = &tsl2563_write_interrupt_config,
694};
695
696static int tsl2563_probe(struct i2c_client *client,
697 const struct i2c_device_id *device_id)
698{
699 struct iio_dev *indio_dev;
700 struct tsl2563_chip *chip;
701 struct tsl2563_platform_data *pdata = client->dev.platform_data;
702 int err = 0;
703 u8 id = 0;
704
705 indio_dev = iio_device_alloc(sizeof(*chip));
706 if (!indio_dev)
707 return -ENOMEM;
708
709 chip = iio_priv(indio_dev);
710
711 i2c_set_clientdata(client, chip);
712 chip->client = client;
713
714 err = tsl2563_detect(chip);
715 if (err) {
716 dev_err(&client->dev, "detect error %d\n", -err);
717 goto fail1;
718 }
719
720 err = tsl2563_read_id(chip, &id);
721 if (err) {
722 dev_err(&client->dev, "read id error %d\n", -err);
723 goto fail1;
724 }
725
726 mutex_init(&chip->lock);
727
728 /* Default values used until userspace says otherwise */
729 chip->low_thres = 0x0;
730 chip->high_thres = 0xffff;
731 chip->gainlevel = tsl2563_gainlevel_table;
732 chip->intr = TSL2563_INT_PERSIST(4);
733 chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
734 chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
735
736 if (pdata)
737 chip->cover_comp_gain = pdata->cover_comp_gain;
738 else
739 chip->cover_comp_gain = 1;
740
741 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
742 indio_dev->name = client->name;
743 indio_dev->channels = tsl2563_channels;
744 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
745 indio_dev->dev.parent = &client->dev;
746 indio_dev->modes = INDIO_DIRECT_MODE;
747
748 if (client->irq)
749 indio_dev->info = &tsl2563_info;
750 else
751 indio_dev->info = &tsl2563_info_no_irq;
752
753 if (client->irq) {
754 err = request_threaded_irq(client->irq,
755 NULL,
756 &tsl2563_event_handler,
757 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
758 "tsl2563_event",
759 indio_dev);
760 if (err) {
761 dev_err(&client->dev, "irq request error %d\n", -err);
762 goto fail1;
763 }
764 }
765
766 err = tsl2563_configure(chip);
767 if (err) {
768 dev_err(&client->dev, "configure error %d\n", -err);
769 goto fail2;
770 }
771
772 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
773
774 /* The interrupt cannot yet be enabled so this is fine without lock */
775 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
776
777 err = iio_device_register(indio_dev);
778 if (err) {
779 dev_err(&client->dev, "iio registration error %d\n", -err);
780 goto fail3;
781 }
782
783 return 0;
784
785fail3:
786 cancel_delayed_work(&chip->poweroff_work);
787 flush_scheduled_work();
788fail2:
789 if (client->irq)
790 free_irq(client->irq, indio_dev);
791fail1:
792 iio_device_free(indio_dev);
793 return err;
794}
795
796static int tsl2563_remove(struct i2c_client *client)
797{
798 struct tsl2563_chip *chip = i2c_get_clientdata(client);
799 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
800
801 iio_device_unregister(indio_dev);
802 if (!chip->int_enabled)
803 cancel_delayed_work(&chip->poweroff_work);
804 /* Ensure that interrupts are disabled - then flush any bottom halves */
805 chip->intr &= ~0x30;
806 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
807 chip->intr);
808 flush_scheduled_work();
809 tsl2563_set_power(chip, 0);
810 if (client->irq)
811 free_irq(client->irq, indio_dev);
812
813 iio_device_free(indio_dev);
814
815 return 0;
816}
817
818#ifdef CONFIG_PM_SLEEP
819static int tsl2563_suspend(struct device *dev)
820{
821 struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
822 int ret;
823
824 mutex_lock(&chip->lock);
825
826 ret = tsl2563_set_power(chip, 0);
827 if (ret)
828 goto out;
829
830 chip->suspended = true;
831
832out:
833 mutex_unlock(&chip->lock);
834 return ret;
835}
836
837static int tsl2563_resume(struct device *dev)
838{
839 struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
840 int ret;
841
842 mutex_lock(&chip->lock);
843
844 ret = tsl2563_set_power(chip, 1);
845 if (ret)
846 goto out;
847
848 ret = tsl2563_configure(chip);
849 if (ret)
850 goto out;
851
852 chip->suspended = false;
853
854out:
855 mutex_unlock(&chip->lock);
856 return ret;
857}
858
859static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume);
860#define TSL2563_PM_OPS (&tsl2563_pm_ops)
861#else
862#define TSL2563_PM_OPS NULL
863#endif
864
865static const struct i2c_device_id tsl2563_id[] = {
866 { "tsl2560", 0 },
867 { "tsl2561", 1 },
868 { "tsl2562", 2 },
869 { "tsl2563", 3 },
870 {}
871};
872MODULE_DEVICE_TABLE(i2c, tsl2563_id);
873
874static struct i2c_driver tsl2563_i2c_driver = {
875 .driver = {
876 .name = "tsl2563",
877 .pm = TSL2563_PM_OPS,
878 },
879 .probe = tsl2563_probe,
880 .remove = tsl2563_remove,
881 .id_table = tsl2563_id,
882};
883module_i2c_driver(tsl2563_i2c_driver);
884
885MODULE_AUTHOR("Nokia Corporation");
886MODULE_DESCRIPTION("tsl2563 light sensor driver");
887MODULE_LICENSE("GPL");
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig
index ff11d68225cf..cd29be54f643 100644
--- a/drivers/iio/magnetometer/Kconfig
+++ b/drivers/iio/magnetometer/Kconfig
@@ -14,4 +14,34 @@ config HID_SENSOR_MAGNETOMETER_3D
14 Say yes here to build support for the HID SENSOR 14 Say yes here to build support for the HID SENSOR
15 Magnetometer 3D. 15 Magnetometer 3D.
16 16
17config IIO_ST_MAGN_3AXIS
18 tristate "STMicroelectronics magnetometers 3-Axis Driver"
19 depends on (I2C || SPI_MASTER) && SYSFS
20 select IIO_ST_SENSORS_CORE
21 select IIO_ST_MAGN_I2C_3AXIS if (I2C)
22 select IIO_ST_MAGN_SPI_3AXIS if (SPI_MASTER)
23 select IIO_TRIGGERED_BUFFER if (IIO_BUFFER)
24 select IIO_ST_MAGN_BUFFER if (IIO_TRIGGERED_BUFFER)
25 help
26 Say yes here to build support for STMicroelectronics magnetometers:
27 LSM303DLHC, LSM303DLM, LIS3MDL.
28
29 This driver can also be built as a module. If so, will be created
30 these modules:
31 - st_magn (core functions for the driver [it is mandatory]);
32 - st_magn_i2c (necessary for the I2C devices [optional*]);
33 - st_magn_spi (necessary for the SPI devices [optional*]);
34
35 (*) one of these is necessary to do something.
36
37config IIO_ST_MAGN_I2C_3AXIS
38 tristate
39 depends on IIO_ST_MAGN_3AXIS
40 depends on IIO_ST_SENSORS_I2C
41
42config IIO_ST_MAGN_SPI_3AXIS
43 tristate
44 depends on IIO_ST_MAGN_3AXIS
45 depends on IIO_ST_SENSORS_SPI
46
17endmenu 47endmenu
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile
index 60dc4f2b1963..e78672876dc2 100644
--- a/drivers/iio/magnetometer/Makefile
+++ b/drivers/iio/magnetometer/Makefile
@@ -3,3 +3,10 @@
3# 3#
4 4
5obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o 5obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o
6
7obj-$(CONFIG_IIO_ST_MAGN_3AXIS) += st_magn.o
8st_magn-y := st_magn_core.o
9st_magn-$(CONFIG_IIO_BUFFER) += st_magn_buffer.o
10
11obj-$(CONFIG_IIO_ST_MAGN_I2C_3AXIS) += st_magn_i2c.o
12obj-$(CONFIG_IIO_ST_MAGN_SPI_3AXIS) += st_magn_spi.o
diff --git a/drivers/iio/magnetometer/hid-sensor-magn-3d.c b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
index 7ac2c7483ba8..d8d01265220b 100644
--- a/drivers/iio/magnetometer/hid-sensor-magn-3d.c
+++ b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
@@ -28,7 +28,6 @@
28#include <linux/iio/buffer.h> 28#include <linux/iio/buffer.h>
29#include <linux/iio/trigger_consumer.h> 29#include <linux/iio/trigger_consumer.h>
30#include <linux/iio/triggered_buffer.h> 30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-attributes.h"
32#include "../common/hid-sensors/hid-sensor-trigger.h" 31#include "../common/hid-sensors/hid-sensor-trigger.h"
33 32
34/*Format: HID-SENSOR-usage_id_in_hex*/ 33/*Format: HID-SENSOR-usage_id_in_hex*/
@@ -44,7 +43,7 @@ enum magn_3d_channel {
44 43
45struct magn_3d_state { 44struct magn_3d_state {
46 struct hid_sensor_hub_callbacks callbacks; 45 struct hid_sensor_hub_callbacks callbacks;
47 struct hid_sensor_iio_common common_attributes; 46 struct hid_sensor_common common_attributes;
48 struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX]; 47 struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
49 u32 magn_val[MAGN_3D_CHANNEL_MAX]; 48 u32 magn_val[MAGN_3D_CHANNEL_MAX];
50}; 49};
diff --git a/drivers/iio/magnetometer/st_magn.h b/drivers/iio/magnetometer/st_magn.h
new file mode 100644
index 000000000000..7e81d00ef0c3
--- /dev/null
+++ b/drivers/iio/magnetometer/st_magn.h
@@ -0,0 +1,45 @@
1/*
2 * STMicroelectronics magnetometers 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_MAGN_H
12#define ST_MAGN_H
13
14#include <linux/types.h>
15#include <linux/iio/common/st_sensors.h>
16
17#define LSM303DLHC_MAGN_DEV_NAME "lsm303dlhc_magn"
18#define LSM303DLM_MAGN_DEV_NAME "lsm303dlm_magn"
19#define LIS3MDL_MAGN_DEV_NAME "lis3mdl"
20
21int st_magn_common_probe(struct iio_dev *indio_dev);
22void st_magn_common_remove(struct iio_dev *indio_dev);
23
24#ifdef CONFIG_IIO_BUFFER
25int st_magn_allocate_ring(struct iio_dev *indio_dev);
26void st_magn_deallocate_ring(struct iio_dev *indio_dev);
27#else /* CONFIG_IIO_BUFFER */
28static inline int st_magn_probe_trigger(struct iio_dev *indio_dev, int irq)
29{
30 return 0;
31}
32static inline void st_magn_remove_trigger(struct iio_dev *indio_dev, int irq)
33{
34 return;
35}
36static inline int st_magn_allocate_ring(struct iio_dev *indio_dev)
37{
38 return 0;
39}
40static inline void st_magn_deallocate_ring(struct iio_dev *indio_dev)
41{
42}
43#endif /* CONFIG_IIO_BUFFER */
44
45#endif /* ST_MAGN_H */
diff --git a/drivers/iio/magnetometer/st_magn_buffer.c b/drivers/iio/magnetometer/st_magn_buffer.c
new file mode 100644
index 000000000000..708857bdb47d
--- /dev/null
+++ b/drivers/iio/magnetometer/st_magn_buffer.c
@@ -0,0 +1,98 @@
1/*
2 * STMicroelectronics magnetometers 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_magn.h"
25
26static int st_magn_buffer_preenable(struct iio_dev *indio_dev)
27{
28 int err;
29
30 err = st_sensors_set_enable(indio_dev, true);
31 if (err < 0)
32 goto st_magn_set_enable_error;
33
34 err = iio_sw_buffer_preenable(indio_dev);
35
36st_magn_set_enable_error:
37 return err;
38}
39
40static int st_magn_buffer_postenable(struct iio_dev *indio_dev)
41{
42 int err;
43 struct st_sensor_data *mdata = iio_priv(indio_dev);
44
45 mdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
46 if (mdata->buffer_data == NULL) {
47 err = -ENOMEM;
48 goto allocate_memory_error;
49 }
50
51 err = iio_triggered_buffer_postenable(indio_dev);
52 if (err < 0)
53 goto st_magn_buffer_postenable_error;
54
55 return err;
56
57st_magn_buffer_postenable_error:
58 kfree(mdata->buffer_data);
59allocate_memory_error:
60 return err;
61}
62
63static int st_magn_buffer_predisable(struct iio_dev *indio_dev)
64{
65 int err;
66 struct st_sensor_data *mdata = iio_priv(indio_dev);
67
68 err = iio_triggered_buffer_predisable(indio_dev);
69 if (err < 0)
70 goto st_magn_buffer_predisable_error;
71
72 err = st_sensors_set_enable(indio_dev, false);
73
74st_magn_buffer_predisable_error:
75 kfree(mdata->buffer_data);
76 return err;
77}
78
79static const struct iio_buffer_setup_ops st_magn_buffer_setup_ops = {
80 .preenable = &st_magn_buffer_preenable,
81 .postenable = &st_magn_buffer_postenable,
82 .predisable = &st_magn_buffer_predisable,
83};
84
85int st_magn_allocate_ring(struct iio_dev *indio_dev)
86{
87 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
88 &st_sensors_trigger_handler, &st_magn_buffer_setup_ops);
89}
90
91void st_magn_deallocate_ring(struct iio_dev *indio_dev)
92{
93 iio_triggered_buffer_cleanup(indio_dev);
94}
95
96MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
97MODULE_DESCRIPTION("STMicroelectronics magnetometers buffer");
98MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/magnetometer/st_magn_core.c b/drivers/iio/magnetometer/st_magn_core.c
new file mode 100644
index 000000000000..16f0d6df239f
--- /dev/null
+++ b/drivers/iio/magnetometer/st_magn_core.c
@@ -0,0 +1,400 @@
1/*
2 * STMicroelectronics magnetometers 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/delay.h>
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/buffer.h>
25
26#include <linux/iio/common/st_sensors.h>
27#include "st_magn.h"
28
29/* DEFAULT VALUE FOR SENSORS */
30#define ST_MAGN_DEFAULT_OUT_X_L_ADDR 0X04
31#define ST_MAGN_DEFAULT_OUT_Y_L_ADDR 0X08
32#define ST_MAGN_DEFAULT_OUT_Z_L_ADDR 0X06
33
34/* FULLSCALE */
35#define ST_MAGN_FS_AVL_1300MG 1300
36#define ST_MAGN_FS_AVL_1900MG 1900
37#define ST_MAGN_FS_AVL_2500MG 2500
38#define ST_MAGN_FS_AVL_4000MG 4000
39#define ST_MAGN_FS_AVL_4700MG 4700
40#define ST_MAGN_FS_AVL_5600MG 5600
41#define ST_MAGN_FS_AVL_8000MG 8000
42#define ST_MAGN_FS_AVL_8100MG 8100
43#define ST_MAGN_FS_AVL_10000MG 10000
44
45/* CUSTOM VALUES FOR SENSOR 1 */
46#define ST_MAGN_1_WAI_EXP 0x3c
47#define ST_MAGN_1_ODR_ADDR 0x00
48#define ST_MAGN_1_ODR_MASK 0x1c
49#define ST_MAGN_1_ODR_AVL_1HZ_VAL 0x00
50#define ST_MAGN_1_ODR_AVL_2HZ_VAL 0x01
51#define ST_MAGN_1_ODR_AVL_3HZ_VAL 0x02
52#define ST_MAGN_1_ODR_AVL_8HZ_VAL 0x03
53#define ST_MAGN_1_ODR_AVL_15HZ_VAL 0x04
54#define ST_MAGN_1_ODR_AVL_30HZ_VAL 0x05
55#define ST_MAGN_1_ODR_AVL_75HZ_VAL 0x06
56#define ST_MAGN_1_ODR_AVL_220HZ_VAL 0x07
57#define ST_MAGN_1_PW_ADDR 0x02
58#define ST_MAGN_1_PW_MASK 0x03
59#define ST_MAGN_1_PW_ON 0x00
60#define ST_MAGN_1_PW_OFF 0x03
61#define ST_MAGN_1_FS_ADDR 0x01
62#define ST_MAGN_1_FS_MASK 0xe0
63#define ST_MAGN_1_FS_AVL_1300_VAL 0x01
64#define ST_MAGN_1_FS_AVL_1900_VAL 0x02
65#define ST_MAGN_1_FS_AVL_2500_VAL 0x03
66#define ST_MAGN_1_FS_AVL_4000_VAL 0x04
67#define ST_MAGN_1_FS_AVL_4700_VAL 0x05
68#define ST_MAGN_1_FS_AVL_5600_VAL 0x06
69#define ST_MAGN_1_FS_AVL_8100_VAL 0x07
70#define ST_MAGN_1_FS_AVL_1300_GAIN_XY 1100
71#define ST_MAGN_1_FS_AVL_1900_GAIN_XY 855
72#define ST_MAGN_1_FS_AVL_2500_GAIN_XY 670
73#define ST_MAGN_1_FS_AVL_4000_GAIN_XY 450
74#define ST_MAGN_1_FS_AVL_4700_GAIN_XY 400
75#define ST_MAGN_1_FS_AVL_5600_GAIN_XY 330
76#define ST_MAGN_1_FS_AVL_8100_GAIN_XY 230
77#define ST_MAGN_1_FS_AVL_1300_GAIN_Z 980
78#define ST_MAGN_1_FS_AVL_1900_GAIN_Z 760
79#define ST_MAGN_1_FS_AVL_2500_GAIN_Z 600
80#define ST_MAGN_1_FS_AVL_4000_GAIN_Z 400
81#define ST_MAGN_1_FS_AVL_4700_GAIN_Z 355
82#define ST_MAGN_1_FS_AVL_5600_GAIN_Z 295
83#define ST_MAGN_1_FS_AVL_8100_GAIN_Z 205
84#define ST_MAGN_1_MULTIREAD_BIT false
85
86/* CUSTOM VALUES FOR SENSOR 2 */
87#define ST_MAGN_2_WAI_EXP 0x3d
88#define ST_MAGN_2_ODR_ADDR 0x20
89#define ST_MAGN_2_ODR_MASK 0x1c
90#define ST_MAGN_2_ODR_AVL_1HZ_VAL 0x00
91#define ST_MAGN_2_ODR_AVL_2HZ_VAL 0x01
92#define ST_MAGN_2_ODR_AVL_3HZ_VAL 0x02
93#define ST_MAGN_2_ODR_AVL_5HZ_VAL 0x03
94#define ST_MAGN_2_ODR_AVL_10HZ_VAL 0x04
95#define ST_MAGN_2_ODR_AVL_20HZ_VAL 0x05
96#define ST_MAGN_2_ODR_AVL_40HZ_VAL 0x06
97#define ST_MAGN_2_ODR_AVL_80HZ_VAL 0x07
98#define ST_MAGN_2_PW_ADDR 0x22
99#define ST_MAGN_2_PW_MASK 0x03
100#define ST_MAGN_2_PW_ON 0x00
101#define ST_MAGN_2_PW_OFF 0x03
102#define ST_MAGN_2_FS_ADDR 0x21
103#define ST_MAGN_2_FS_MASK 0x60
104#define ST_MAGN_2_FS_AVL_4000_VAL 0x00
105#define ST_MAGN_2_FS_AVL_8000_VAL 0x01
106#define ST_MAGN_2_FS_AVL_10000_VAL 0x02
107#define ST_MAGN_2_FS_AVL_4000_GAIN 430
108#define ST_MAGN_2_FS_AVL_8000_GAIN 230
109#define ST_MAGN_2_FS_AVL_10000_GAIN 230
110#define ST_MAGN_2_MULTIREAD_BIT false
111#define ST_MAGN_2_OUT_X_L_ADDR 0x28
112#define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
113#define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
114
115static const struct iio_chan_spec st_magn_16bit_channels[] = {
116 ST_SENSORS_LSM_CHANNELS(IIO_MAGN, ST_SENSORS_SCAN_X, IIO_MOD_X, IIO_LE,
117 ST_SENSORS_DEFAULT_16_REALBITS, ST_MAGN_DEFAULT_OUT_X_L_ADDR),
118 ST_SENSORS_LSM_CHANNELS(IIO_MAGN, ST_SENSORS_SCAN_Y, IIO_MOD_Y, IIO_LE,
119 ST_SENSORS_DEFAULT_16_REALBITS, ST_MAGN_DEFAULT_OUT_Y_L_ADDR),
120 ST_SENSORS_LSM_CHANNELS(IIO_MAGN, ST_SENSORS_SCAN_Z, IIO_MOD_Z, IIO_LE,
121 ST_SENSORS_DEFAULT_16_REALBITS, ST_MAGN_DEFAULT_OUT_Z_L_ADDR),
122 IIO_CHAN_SOFT_TIMESTAMP(3)
123};
124
125static const struct iio_chan_spec st_magn_2_16bit_channels[] = {
126 ST_SENSORS_LSM_CHANNELS(IIO_MAGN, ST_SENSORS_SCAN_X, IIO_MOD_X, IIO_LE,
127 ST_SENSORS_DEFAULT_16_REALBITS, ST_MAGN_2_OUT_X_L_ADDR),
128 ST_SENSORS_LSM_CHANNELS(IIO_MAGN, ST_SENSORS_SCAN_Y, IIO_MOD_Y, IIO_LE,
129 ST_SENSORS_DEFAULT_16_REALBITS, ST_MAGN_2_OUT_Y_L_ADDR),
130 ST_SENSORS_LSM_CHANNELS(IIO_MAGN, ST_SENSORS_SCAN_Z, IIO_MOD_Z, IIO_LE,
131 ST_SENSORS_DEFAULT_16_REALBITS, ST_MAGN_2_OUT_Z_L_ADDR),
132 IIO_CHAN_SOFT_TIMESTAMP(3)
133};
134
135static const struct st_sensors st_magn_sensors[] = {
136 {
137 .wai = ST_MAGN_1_WAI_EXP,
138 .sensors_supported = {
139 [0] = LSM303DLHC_MAGN_DEV_NAME,
140 [1] = LSM303DLM_MAGN_DEV_NAME,
141 },
142 .ch = (struct iio_chan_spec *)st_magn_16bit_channels,
143 .odr = {
144 .addr = ST_MAGN_1_ODR_ADDR,
145 .mask = ST_MAGN_1_ODR_MASK,
146 .odr_avl = {
147 { 1, ST_MAGN_1_ODR_AVL_1HZ_VAL, },
148 { 2, ST_MAGN_1_ODR_AVL_2HZ_VAL, },
149 { 3, ST_MAGN_1_ODR_AVL_3HZ_VAL, },
150 { 8, ST_MAGN_1_ODR_AVL_8HZ_VAL, },
151 { 15, ST_MAGN_1_ODR_AVL_15HZ_VAL, },
152 { 30, ST_MAGN_1_ODR_AVL_30HZ_VAL, },
153 { 75, ST_MAGN_1_ODR_AVL_75HZ_VAL, },
154 { 220, ST_MAGN_1_ODR_AVL_220HZ_VAL, },
155 },
156 },
157 .pw = {
158 .addr = ST_MAGN_1_PW_ADDR,
159 .mask = ST_MAGN_1_PW_MASK,
160 .value_on = ST_MAGN_1_PW_ON,
161 .value_off = ST_MAGN_1_PW_OFF,
162 },
163 .fs = {
164 .addr = ST_MAGN_1_FS_ADDR,
165 .mask = ST_MAGN_1_FS_MASK,
166 .fs_avl = {
167 [0] = {
168 .num = ST_MAGN_FS_AVL_1300MG,
169 .value = ST_MAGN_1_FS_AVL_1300_VAL,
170 .gain = ST_MAGN_1_FS_AVL_1300_GAIN_XY,
171 .gain2 = ST_MAGN_1_FS_AVL_1300_GAIN_Z,
172 },
173 [1] = {
174 .num = ST_MAGN_FS_AVL_1900MG,
175 .value = ST_MAGN_1_FS_AVL_1900_VAL,
176 .gain = ST_MAGN_1_FS_AVL_1900_GAIN_XY,
177 .gain2 = ST_MAGN_1_FS_AVL_1900_GAIN_Z,
178 },
179 [2] = {
180 .num = ST_MAGN_FS_AVL_2500MG,
181 .value = ST_MAGN_1_FS_AVL_2500_VAL,
182 .gain = ST_MAGN_1_FS_AVL_2500_GAIN_XY,
183 .gain2 = ST_MAGN_1_FS_AVL_2500_GAIN_Z,
184 },
185 [3] = {
186 .num = ST_MAGN_FS_AVL_4000MG,
187 .value = ST_MAGN_1_FS_AVL_4000_VAL,
188 .gain = ST_MAGN_1_FS_AVL_4000_GAIN_XY,
189 .gain2 = ST_MAGN_1_FS_AVL_4000_GAIN_Z,
190 },
191 [4] = {
192 .num = ST_MAGN_FS_AVL_4700MG,
193 .value = ST_MAGN_1_FS_AVL_4700_VAL,
194 .gain = ST_MAGN_1_FS_AVL_4700_GAIN_XY,
195 .gain2 = ST_MAGN_1_FS_AVL_4700_GAIN_Z,
196 },
197 [5] = {
198 .num = ST_MAGN_FS_AVL_5600MG,
199 .value = ST_MAGN_1_FS_AVL_5600_VAL,
200 .gain = ST_MAGN_1_FS_AVL_5600_GAIN_XY,
201 .gain2 = ST_MAGN_1_FS_AVL_5600_GAIN_Z,
202 },
203 [6] = {
204 .num = ST_MAGN_FS_AVL_8100MG,
205 .value = ST_MAGN_1_FS_AVL_8100_VAL,
206 .gain = ST_MAGN_1_FS_AVL_8100_GAIN_XY,
207 .gain2 = ST_MAGN_1_FS_AVL_8100_GAIN_Z,
208 },
209 },
210 },
211 .multi_read_bit = ST_MAGN_1_MULTIREAD_BIT,
212 .bootime = 2,
213 },
214 {
215 .wai = ST_MAGN_2_WAI_EXP,
216 .sensors_supported = {
217 [0] = LIS3MDL_MAGN_DEV_NAME,
218 },
219 .ch = (struct iio_chan_spec *)st_magn_2_16bit_channels,
220 .odr = {
221 .addr = ST_MAGN_2_ODR_ADDR,
222 .mask = ST_MAGN_2_ODR_MASK,
223 .odr_avl = {
224 { 1, ST_MAGN_2_ODR_AVL_1HZ_VAL, },
225 { 2, ST_MAGN_2_ODR_AVL_2HZ_VAL, },
226 { 3, ST_MAGN_2_ODR_AVL_3HZ_VAL, },
227 { 5, ST_MAGN_2_ODR_AVL_5HZ_VAL, },
228 { 10, ST_MAGN_2_ODR_AVL_10HZ_VAL, },
229 { 20, ST_MAGN_2_ODR_AVL_20HZ_VAL, },
230 { 40, ST_MAGN_2_ODR_AVL_40HZ_VAL, },
231 { 80, ST_MAGN_2_ODR_AVL_80HZ_VAL, },
232 },
233 },
234 .pw = {
235 .addr = ST_MAGN_2_PW_ADDR,
236 .mask = ST_MAGN_2_PW_MASK,
237 .value_on = ST_MAGN_2_PW_ON,
238 .value_off = ST_MAGN_2_PW_OFF,
239 },
240 .fs = {
241 .addr = ST_MAGN_2_FS_ADDR,
242 .mask = ST_MAGN_2_FS_MASK,
243 .fs_avl = {
244 [0] = {
245 .num = ST_MAGN_FS_AVL_4000MG,
246 .value = ST_MAGN_2_FS_AVL_4000_VAL,
247 .gain = ST_MAGN_2_FS_AVL_4000_GAIN,
248 },
249 [1] = {
250 .num = ST_MAGN_FS_AVL_8000MG,
251 .value = ST_MAGN_2_FS_AVL_8000_VAL,
252 .gain = ST_MAGN_2_FS_AVL_8000_GAIN,
253 },
254 [2] = {
255 .num = ST_MAGN_FS_AVL_10000MG,
256 .value = ST_MAGN_2_FS_AVL_10000_VAL,
257 .gain = ST_MAGN_2_FS_AVL_10000_GAIN,
258 },
259 },
260 },
261 .multi_read_bit = ST_MAGN_2_MULTIREAD_BIT,
262 .bootime = 2,
263 },
264};
265
266static int st_magn_read_raw(struct iio_dev *indio_dev,
267 struct iio_chan_spec const *ch, int *val,
268 int *val2, long mask)
269{
270 int err;
271 struct st_sensor_data *mdata = iio_priv(indio_dev);
272
273 switch (mask) {
274 case IIO_CHAN_INFO_RAW:
275 err = st_sensors_read_info_raw(indio_dev, ch, val);
276 if (err < 0)
277 goto read_error;
278
279 return IIO_VAL_INT;
280 case IIO_CHAN_INFO_SCALE:
281 *val = 0;
282 if ((ch->scan_index == ST_SENSORS_SCAN_Z) &&
283 (mdata->current_fullscale->gain2 != 0))
284 *val2 = mdata->current_fullscale->gain2;
285 else
286 *val2 = mdata->current_fullscale->gain;
287 return IIO_VAL_INT_PLUS_MICRO;
288 default:
289 return -EINVAL;
290 }
291
292read_error:
293 return err;
294}
295
296static int st_magn_write_raw(struct iio_dev *indio_dev,
297 struct iio_chan_spec const *chan, int val, int val2, long mask)
298{
299 int err;
300
301 switch (mask) {
302 case IIO_CHAN_INFO_SCALE:
303 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
304 break;
305 default:
306 err = -EINVAL;
307 }
308
309 return err;
310}
311
312static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
313static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
314static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
315
316static struct attribute *st_magn_attributes[] = {
317 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
318 &iio_dev_attr_in_magn_scale_available.dev_attr.attr,
319 &iio_dev_attr_sampling_frequency.dev_attr.attr,
320 NULL,
321};
322
323static const struct attribute_group st_magn_attribute_group = {
324 .attrs = st_magn_attributes,
325};
326
327static const struct iio_info magn_info = {
328 .driver_module = THIS_MODULE,
329 .attrs = &st_magn_attribute_group,
330 .read_raw = &st_magn_read_raw,
331 .write_raw = &st_magn_write_raw,
332};
333
334int st_magn_common_probe(struct iio_dev *indio_dev)
335{
336 int err;
337 struct st_sensor_data *mdata = iio_priv(indio_dev);
338
339 indio_dev->modes = INDIO_DIRECT_MODE;
340 indio_dev->info = &magn_info;
341
342 err = st_sensors_check_device_support(indio_dev,
343 ARRAY_SIZE(st_magn_sensors), st_magn_sensors);
344 if (err < 0)
345 goto st_magn_common_probe_error;
346
347 mdata->multiread_bit = mdata->sensor->multi_read_bit;
348 indio_dev->channels = mdata->sensor->ch;
349 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
350
351 mdata->current_fullscale = (struct st_sensor_fullscale_avl *)
352 &mdata->sensor->fs.fs_avl[0];
353 mdata->odr = mdata->sensor->odr.odr_avl[0].hz;
354
355 err = st_sensors_init_sensor(indio_dev);
356 if (err < 0)
357 goto st_magn_common_probe_error;
358
359 if (mdata->get_irq_data_ready(indio_dev) > 0) {
360 err = st_magn_allocate_ring(indio_dev);
361 if (err < 0)
362 goto st_magn_common_probe_error;
363 err = st_sensors_allocate_trigger(indio_dev, NULL);
364 if (err < 0)
365 goto st_magn_probe_trigger_error;
366 }
367
368 err = iio_device_register(indio_dev);
369 if (err)
370 goto st_magn_device_register_error;
371
372 return err;
373
374st_magn_device_register_error:
375 if (mdata->get_irq_data_ready(indio_dev) > 0)
376 st_sensors_deallocate_trigger(indio_dev);
377st_magn_probe_trigger_error:
378 if (mdata->get_irq_data_ready(indio_dev) > 0)
379 st_magn_deallocate_ring(indio_dev);
380st_magn_common_probe_error:
381 return err;
382}
383EXPORT_SYMBOL(st_magn_common_probe);
384
385void st_magn_common_remove(struct iio_dev *indio_dev)
386{
387 struct st_sensor_data *mdata = iio_priv(indio_dev);
388
389 iio_device_unregister(indio_dev);
390 if (mdata->get_irq_data_ready(indio_dev) > 0) {
391 st_sensors_deallocate_trigger(indio_dev);
392 st_magn_deallocate_ring(indio_dev);
393 }
394 iio_device_free(indio_dev);
395}
396EXPORT_SYMBOL(st_magn_common_remove);
397
398MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
399MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
400MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/magnetometer/st_magn_i2c.c b/drivers/iio/magnetometer/st_magn_i2c.c
new file mode 100644
index 000000000000..e6adc4a86425
--- /dev/null
+++ b/drivers/iio/magnetometer/st_magn_i2c.c
@@ -0,0 +1,80 @@
1/*
2 * STMicroelectronics magnetometers 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
17#include <linux/iio/common/st_sensors.h>
18#include <linux/iio/common/st_sensors_i2c.h>
19#include "st_magn.h"
20
21static int st_magn_i2c_probe(struct i2c_client *client,
22 const struct i2c_device_id *id)
23{
24 struct iio_dev *indio_dev;
25 struct st_sensor_data *mdata;
26 int err;
27
28 indio_dev = iio_device_alloc(sizeof(*mdata));
29 if (indio_dev == NULL) {
30 err = -ENOMEM;
31 goto iio_device_alloc_error;
32 }
33
34 mdata = iio_priv(indio_dev);
35 mdata->dev = &client->dev;
36
37 st_sensors_i2c_configure(indio_dev, client, mdata);
38
39 err = st_magn_common_probe(indio_dev);
40 if (err < 0)
41 goto st_magn_common_probe_error;
42
43 return 0;
44
45st_magn_common_probe_error:
46 iio_device_free(indio_dev);
47iio_device_alloc_error:
48 return err;
49}
50
51static int st_magn_i2c_remove(struct i2c_client *client)
52{
53 struct iio_dev *indio_dev = i2c_get_clientdata(client);
54 st_magn_common_remove(indio_dev);
55
56 return 0;
57}
58
59static const struct i2c_device_id st_magn_id_table[] = {
60 { LSM303DLHC_MAGN_DEV_NAME },
61 { LSM303DLM_MAGN_DEV_NAME },
62 { LIS3MDL_MAGN_DEV_NAME },
63 {},
64};
65MODULE_DEVICE_TABLE(i2c, st_magn_id_table);
66
67static struct i2c_driver st_magn_driver = {
68 .driver = {
69 .owner = THIS_MODULE,
70 .name = "st-magn-i2c",
71 },
72 .probe = st_magn_i2c_probe,
73 .remove = st_magn_i2c_remove,
74 .id_table = st_magn_id_table,
75};
76module_i2c_driver(st_magn_driver);
77
78MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
79MODULE_DESCRIPTION("STMicroelectronics magnetometers i2c driver");
80MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/magnetometer/st_magn_spi.c b/drivers/iio/magnetometer/st_magn_spi.c
new file mode 100644
index 000000000000..51adb797cb7d
--- /dev/null
+++ b/drivers/iio/magnetometer/st_magn_spi.c
@@ -0,0 +1,79 @@
1/*
2 * STMicroelectronics magnetometers 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
17#include <linux/iio/common/st_sensors.h>
18#include <linux/iio/common/st_sensors_spi.h>
19#include "st_magn.h"
20
21static int st_magn_spi_probe(struct spi_device *spi)
22{
23 struct iio_dev *indio_dev;
24 struct st_sensor_data *mdata;
25 int err;
26
27 indio_dev = iio_device_alloc(sizeof(*mdata));
28 if (indio_dev == NULL) {
29 err = -ENOMEM;
30 goto iio_device_alloc_error;
31 }
32
33 mdata = iio_priv(indio_dev);
34 mdata->dev = &spi->dev;
35
36 st_sensors_spi_configure(indio_dev, spi, mdata);
37
38 err = st_magn_common_probe(indio_dev);
39 if (err < 0)
40 goto st_magn_common_probe_error;
41
42 return 0;
43
44st_magn_common_probe_error:
45 iio_device_free(indio_dev);
46iio_device_alloc_error:
47 return err;
48}
49
50static int st_magn_spi_remove(struct spi_device *spi)
51{
52 struct iio_dev *indio_dev = spi_get_drvdata(spi);
53 st_magn_common_remove(indio_dev);
54
55 return 0;
56}
57
58static const struct spi_device_id st_magn_id_table[] = {
59 { LSM303DLHC_MAGN_DEV_NAME },
60 { LSM303DLM_MAGN_DEV_NAME },
61 { LIS3MDL_MAGN_DEV_NAME },
62 {},
63};
64MODULE_DEVICE_TABLE(spi, st_magn_id_table);
65
66static struct spi_driver st_magn_driver = {
67 .driver = {
68 .owner = THIS_MODULE,
69 .name = "st-magn-spi",
70 },
71 .probe = st_magn_spi_probe,
72 .remove = st_magn_spi_remove,
73 .id_table = st_magn_id_table,
74};
75module_spi_driver(st_magn_driver);
76
77MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
78MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
79MODULE_LICENSE("GPL v2");