Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livep...
[deliverable/linux.git] / drivers / staging / iio / accel / adis16240_core.c
CommitLineData
2c834b4d
BS
1/*
2 * ADIS16240 Programmable Impact Sensor and Recorder driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/interrupt.h>
10#include <linux/irq.h>
11#include <linux/gpio.h>
12#include <linux/delay.h>
13#include <linux/mutex.h>
14#include <linux/device.h>
15#include <linux/kernel.h>
16#include <linux/spi/spi.h>
1cb6c1f5 17#include <linux/slab.h>
2c834b4d
BS
18#include <linux/sysfs.h>
19#include <linux/list.h>
99c97852 20#include <linux/module.h>
2c834b4d 21
06458e27
JC
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/buffer.h>
ec04cb04 25#include <linux/iio/imu/adis.h>
2c834b4d
BS
26
27#include "adis16240.h"
2c834b4d
BS
28
29static ssize_t adis16240_spi_read_signed(struct device *dev,
9963bce7
HM
30 struct device_attribute *attr,
31 char *buf,
32 unsigned bits)
2c834b4d 33{
4b522ce7 34 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
5cb7cb11 35 struct adis *st = iio_priv(indio_dev);
2c834b4d
BS
36 int ret;
37 s16 val = 0;
38 unsigned shift = 16 - bits;
39 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
40
5cb7cb11 41 ret = adis_read_reg_16(st,
9963bce7 42 this_attr->address, (u16 *)&val);
2c834b4d
BS
43 if (ret)
44 return ret;
45
46 if (val & ADIS16240_ERROR_ACTIVE)
5cb7cb11 47 adis_check_status(st);
2c834b4d 48
5db4851d 49 val = (s16)(val << shift) >> shift;
2c834b4d
BS
50 return sprintf(buf, "%d\n", val);
51}
52
2c834b4d 53static ssize_t adis16240_read_12bit_signed(struct device *dev,
9963bce7
HM
54 struct device_attribute *attr,
55 char *buf)
2c834b4d 56{
2c834b4d 57 ssize_t ret;
4b522ce7 58 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
2c834b4d
BS
59
60 /* Take the iio_dev status lock */
61 mutex_lock(&indio_dev->mlock);
62 ret = adis16240_spi_read_signed(dev, attr, buf, 12);
63 mutex_unlock(&indio_dev->mlock);
64
65 return ret;
66}
67
322c9563 68static IIO_DEVICE_ATTR(in_accel_xyz_squared_peak_raw, S_IRUGO,
2c834b4d
BS
69 adis16240_read_12bit_signed, NULL,
70 ADIS16240_XYZPEAK_OUT);
2c834b4d 71
51a0a5b0 72static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
2c834b4d 73
5cb7cb11
LPC
74static const u8 adis16240_addresses[][2] = {
75 [ADIS16240_SCAN_ACC_X] = { ADIS16240_XACCL_OFF, ADIS16240_XPEAK_OUT },
76 [ADIS16240_SCAN_ACC_Y] = { ADIS16240_YACCL_OFF, ADIS16240_YPEAK_OUT },
77 [ADIS16240_SCAN_ACC_Z] = { ADIS16240_ZACCL_OFF, ADIS16240_ZPEAK_OUT },
cd69f57d
JC
78};
79
80static int adis16240_read_raw(struct iio_dev *indio_dev,
81 struct iio_chan_spec const *chan,
82 int *val, int *val2,
83 long mask)
84{
5cb7cb11 85 struct adis *st = iio_priv(indio_dev);
cd69f57d
JC
86 int ret;
87 int bits;
88 u8 addr;
89 s16 val16;
90
91 switch (mask) {
31313fc6 92 case IIO_CHAN_INFO_RAW:
5cb7cb11
LPC
93 return adis_single_conversion(indio_dev, chan,
94 ADIS16240_ERROR_ACTIVE, val);
c8a9f805 95 case IIO_CHAN_INFO_SCALE:
cd69f57d 96 switch (chan->type) {
6835cb6b 97 case IIO_VOLTAGE:
acba41f8
LPC
98 if (chan->channel == 0) {
99 *val = 4;
100 *val2 = 880000; /* 4.88 mV */
101 return IIO_VAL_INT_PLUS_MICRO;
acba41f8 102 }
b5e736bd 103 return -EINVAL;
cd69f57d 104 case IIO_TEMP:
acba41f8
LPC
105 *val = 244; /* 0.244 C */
106 *val2 = 0;
cd69f57d
JC
107 return IIO_VAL_INT_PLUS_MICRO;
108 case IIO_ACCEL:
109 *val = 0;
acba41f8 110 *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
cd69f57d
JC
111 return IIO_VAL_INT_PLUS_MICRO;
112 default:
113 return -EINVAL;
114 }
115 break;
c8a9f805 116 case IIO_CHAN_INFO_PEAK_SCALE:
acba41f8
LPC
117 *val = 0;
118 *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
cd69f57d 119 return IIO_VAL_INT_PLUS_MICRO;
c8a9f805 120 case IIO_CHAN_INFO_OFFSET:
acba41f8 121 *val = 25000 / 244 - 0x133; /* 25 C = 0x133 */
cd69f57d 122 return IIO_VAL_INT;
c8a9f805 123 case IIO_CHAN_INFO_CALIBBIAS:
cd69f57d
JC
124 bits = 10;
125 mutex_lock(&indio_dev->mlock);
5cb7cb11
LPC
126 addr = adis16240_addresses[chan->scan_index][0];
127 ret = adis_read_reg_16(st, addr, &val16);
cd69f57d
JC
128 if (ret) {
129 mutex_unlock(&indio_dev->mlock);
130 return ret;
131 }
132 val16 &= (1 << bits) - 1;
133 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
134 *val = val16;
135 mutex_unlock(&indio_dev->mlock);
136 return IIO_VAL_INT;
c8a9f805 137 case IIO_CHAN_INFO_PEAK:
cd69f57d
JC
138 bits = 10;
139 mutex_lock(&indio_dev->mlock);
5cb7cb11
LPC
140 addr = adis16240_addresses[chan->scan_index][1];
141 ret = adis_read_reg_16(st, addr, &val16);
cd69f57d
JC
142 if (ret) {
143 mutex_unlock(&indio_dev->mlock);
144 return ret;
145 }
146 val16 &= (1 << bits) - 1;
147 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
148 *val = val16;
149 mutex_unlock(&indio_dev->mlock);
150 return IIO_VAL_INT;
151 }
152 return -EINVAL;
153}
154
155static int adis16240_write_raw(struct iio_dev *indio_dev,
156 struct iio_chan_spec const *chan,
157 int val,
158 int val2,
159 long mask)
160{
5cb7cb11 161 struct adis *st = iio_priv(indio_dev);
cd69f57d
JC
162 int bits = 10;
163 s16 val16;
164 u8 addr;
d7b79519 165
cd69f57d 166 switch (mask) {
c8a9f805 167 case IIO_CHAN_INFO_CALIBBIAS:
cd69f57d 168 val16 = val & ((1 << bits) - 1);
5cb7cb11
LPC
169 addr = adis16240_addresses[chan->scan_index][0];
170 return adis_write_reg_16(st, addr, val16);
cd69f57d
JC
171 }
172 return -EINVAL;
173}
174
f4e4b955 175static const struct iio_chan_spec adis16240_channels[] = {
82695ef5
JC
176 ADIS_SUPPLY_CHAN(ADIS16240_SUPPLY_OUT, ADIS16240_SCAN_SUPPLY, 0, 10),
177 ADIS_AUX_ADC_CHAN(ADIS16240_AUX_ADC, ADIS16240_SCAN_AUX_ADC, 0, 10),
5cb7cb11 178 ADIS_ACCEL_CHAN(X, ADIS16240_XACCL_OUT, ADIS16240_SCAN_ACC_X,
9963bce7
HM
179 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
180 0, 10),
5cb7cb11 181 ADIS_ACCEL_CHAN(Y, ADIS16240_YACCL_OUT, ADIS16240_SCAN_ACC_Y,
9963bce7
HM
182 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
183 0, 10),
5cb7cb11 184 ADIS_ACCEL_CHAN(Z, ADIS16240_ZACCL_OUT, ADIS16240_SCAN_ACC_Z,
9963bce7
HM
185 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
186 0, 10),
82695ef5 187 ADIS_TEMP_CHAN(ADIS16240_TEMP_OUT, ADIS16240_SCAN_TEMP, 0, 10),
cd69f57d
JC
188 IIO_CHAN_SOFT_TIMESTAMP(6)
189};
190
2c834b4d 191static struct attribute *adis16240_attributes[] = {
322c9563 192 &iio_dev_attr_in_accel_xyz_squared_peak_raw.dev_attr.attr,
51a0a5b0 193 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
2c834b4d
BS
194 NULL
195};
196
197static const struct attribute_group adis16240_attribute_group = {
198 .attrs = adis16240_attributes,
199};
200
6fe8135f
JC
201static const struct iio_info adis16240_info = {
202 .attrs = &adis16240_attribute_group,
203 .read_raw = &adis16240_read_raw,
204 .write_raw = &adis16240_write_raw,
aacff892 205 .update_scan_mode = adis_update_scan_mode,
6fe8135f
JC
206 .driver_module = THIS_MODULE,
207};
208
5cb7cb11
LPC
209static const char * const adis16240_status_error_msgs[] = {
210 [ADIS16240_DIAG_STAT_PWRON_FAIL_BIT] = "Power on, self-test failed",
211 [ADIS16240_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
212 [ADIS16240_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
213 [ADIS16240_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
214 [ADIS16240_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.225V",
215};
216
217static const struct adis_data adis16240_data = {
218 .write_delay = 35,
219 .read_delay = 35,
220 .msc_ctrl_reg = ADIS16240_MSC_CTRL,
221 .glob_cmd_reg = ADIS16240_GLOB_CMD,
222 .diag_stat_reg = ADIS16240_DIAG_STAT,
223
224 .self_test_mask = ADIS16240_MSC_CTRL_SELF_TEST_EN,
225 .startup_delay = ADIS16240_STARTUP_DELAY,
226
227 .status_error_msgs = adis16240_status_error_msgs,
228 .status_error_mask = BIT(ADIS16240_DIAG_STAT_PWRON_FAIL_BIT) |
229 BIT(ADIS16240_DIAG_STAT_SPI_FAIL_BIT) |
230 BIT(ADIS16240_DIAG_STAT_FLASH_UPT_BIT) |
231 BIT(ADIS16240_DIAG_STAT_POWER_HIGH_BIT) |
232 BIT(ADIS16240_DIAG_STAT_POWER_LOW_BIT),
233};
234
4ae1c61f 235static int adis16240_probe(struct spi_device *spi)
2c834b4d 236{
26d25ae3 237 int ret;
5cb7cb11 238 struct adis *st;
a22ff706
JC
239 struct iio_dev *indio_dev;
240
241 /* setup the industrialio driver allocated elements */
e8cac7f6
SK
242 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
243 if (!indio_dev)
244 return -ENOMEM;
a22ff706 245 st = iio_priv(indio_dev);
2c834b4d 246 /* this is only used for removal purposes */
a22ff706 247 spi_set_drvdata(spi, indio_dev);
2c834b4d 248
a22ff706
JC
249 indio_dev->name = spi->dev.driver->name;
250 indio_dev->dev.parent = &spi->dev;
251 indio_dev->info = &adis16240_info;
252 indio_dev->channels = adis16240_channels;
253 indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
254 indio_dev->modes = INDIO_DIRECT_MODE;
2c834b4d 255
5cb7cb11
LPC
256 ret = adis_init(st, indio_dev, spi, &adis16240_data);
257 if (ret)
e8cac7f6 258 return ret;
5cb7cb11 259 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
2c834b4d 260 if (ret)
e8cac7f6 261 return ret;
2c834b4d
BS
262
263 /* Get the device into a sane initial state */
5cb7cb11 264 ret = adis_initial_startup(st);
26d25ae3 265 if (ret)
5cb7cb11 266 goto error_cleanup_buffer_trigger;
26d25ae3 267 ret = iio_device_register(indio_dev);
2c834b4d 268 if (ret)
5cb7cb11 269 goto error_cleanup_buffer_trigger;
2c834b4d
BS
270 return 0;
271
5cb7cb11
LPC
272error_cleanup_buffer_trigger:
273 adis_cleanup_buffer_and_trigger(st, indio_dev);
2c834b4d
BS
274 return ret;
275}
276
447d4f29 277static int adis16240_remove(struct spi_device *spi)
2c834b4d 278{
a22ff706 279 struct iio_dev *indio_dev = spi_get_drvdata(spi);
5cb7cb11 280 struct adis *st = iio_priv(indio_dev);
2c834b4d 281
d2fffd6c 282 iio_device_unregister(indio_dev);
5cb7cb11 283 adis_cleanup_buffer_and_trigger(st, indio_dev);
2c834b4d
BS
284
285 return 0;
286}
287
288static struct spi_driver adis16240_driver = {
289 .driver = {
290 .name = "adis16240",
291 .owner = THIS_MODULE,
292 },
293 .probe = adis16240_probe,
e543acf0 294 .remove = adis16240_remove,
2c834b4d 295};
ae6ae6fe 296module_spi_driver(adis16240_driver);
2c834b4d
BS
297
298MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
299MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
300MODULE_LICENSE("GPL v2");
55e4390c 301MODULE_ALIAS("spi:adis16240");
This page took 0.541277 seconds and 5 git commands to generate.