staging:iio:accel:adis move to info_mask_(shared_by_type/separate)
[linux-2.6/btrfs-unstable.git] / drivers / staging / iio / accel / adis16204_core.c
blob792ec25a50dca5b9c8ef18827ead6a6e3b5f002e
1 /*
2 * ADIS16204 Programmable High-g Digital Impact Sensor and Recorder
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/imu/adis.h>
26 #include "adis16204.h"
28 /* Unique to this driver currently */
30 static const u8 adis16204_addresses[][2] = {
31 [ADIS16204_SCAN_ACC_X] = { ADIS16204_XACCL_NULL, ADIS16204_X_PEAK_OUT },
32 [ADIS16204_SCAN_ACC_Y] = { ADIS16204_YACCL_NULL, ADIS16204_Y_PEAK_OUT },
33 [ADIS16204_SCAN_ACC_XY] = { 0, ADIS16204_XY_PEAK_OUT },
36 static int adis16204_read_raw(struct iio_dev *indio_dev,
37 struct iio_chan_spec const *chan,
38 int *val, int *val2,
39 long mask)
41 struct adis *st = iio_priv(indio_dev);
42 int ret;
43 int bits;
44 u8 addr;
45 s16 val16;
46 int addrind;
48 switch (mask) {
49 case IIO_CHAN_INFO_RAW:
50 return adis_single_conversion(indio_dev, chan,
51 ADIS16204_ERROR_ACTIVE, val);
52 case IIO_CHAN_INFO_SCALE:
53 switch (chan->type) {
54 case IIO_VOLTAGE:
55 if (chan->channel == 0) {
56 *val = 1;
57 *val2 = 220000; /* 1.22 mV */
58 } else {
59 *val = 0;
60 *val2 = 610000; /* 0.61 mV */
62 return IIO_VAL_INT_PLUS_MICRO;
63 case IIO_TEMP:
64 *val = -470; /* 0.47 C */
65 *val2 = 0;
66 return IIO_VAL_INT_PLUS_MICRO;
67 case IIO_ACCEL:
68 *val = 0;
69 switch (chan->channel2) {
70 case IIO_MOD_X:
71 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
72 *val2 = IIO_G_TO_M_S_2(17125); /* 17.125 mg */
73 break;
74 case IIO_MOD_Y:
75 case IIO_MOD_Z:
76 *val2 = IIO_G_TO_M_S_2(8407); /* 8.407 mg */
77 break;
79 return IIO_VAL_INT_PLUS_MICRO;
80 default:
81 return -EINVAL;
83 break;
84 case IIO_CHAN_INFO_OFFSET:
85 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
86 return IIO_VAL_INT;
87 case IIO_CHAN_INFO_CALIBBIAS:
88 case IIO_CHAN_INFO_PEAK:
89 if (mask == IIO_CHAN_INFO_CALIBBIAS) {
90 bits = 12;
91 addrind = 0;
92 } else { /* PEAK_SEPARATE */
93 bits = 14;
94 addrind = 1;
96 mutex_lock(&indio_dev->mlock);
97 addr = adis16204_addresses[chan->scan_index][addrind];
98 ret = adis_read_reg_16(st, addr, &val16);
99 if (ret) {
100 mutex_unlock(&indio_dev->mlock);
101 return ret;
103 val16 &= (1 << bits) - 1;
104 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
105 *val = val16;
106 mutex_unlock(&indio_dev->mlock);
107 return IIO_VAL_INT;
109 return -EINVAL;
112 static int adis16204_write_raw(struct iio_dev *indio_dev,
113 struct iio_chan_spec const *chan,
114 int val,
115 int val2,
116 long mask)
118 struct adis *st = iio_priv(indio_dev);
119 int bits;
120 s16 val16;
121 u8 addr;
122 switch (mask) {
123 case IIO_CHAN_INFO_CALIBBIAS:
124 switch (chan->type) {
125 case IIO_ACCEL:
126 bits = 12;
127 break;
128 default:
129 return -EINVAL;
131 val16 = val & ((1 << bits) - 1);
132 addr = adis16204_addresses[chan->scan_index][1];
133 return adis_write_reg_16(st, addr, val16);
135 return -EINVAL;
138 static const struct iio_chan_spec adis16204_channels[] = {
139 ADIS_SUPPLY_CHAN(ADIS16204_SUPPLY_OUT, ADIS16204_SCAN_SUPPLY, 12),
140 ADIS_AUX_ADC_CHAN(ADIS16204_AUX_ADC, ADIS16204_SCAN_AUX_ADC, 12),
141 ADIS_TEMP_CHAN(ADIS16204_TEMP_OUT, ADIS16204_SCAN_TEMP, 12),
142 ADIS_ACCEL_CHAN(X, ADIS16204_XACCL_OUT, ADIS16204_SCAN_ACC_X,
143 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK), 14),
144 ADIS_ACCEL_CHAN(Y, ADIS16204_YACCL_OUT, ADIS16204_SCAN_ACC_Y,
145 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK), 14),
146 ADIS_ACCEL_CHAN(ROOT_SUM_SQUARED_X_Y, ADIS16204_XY_RSS_OUT,
147 ADIS16204_SCAN_ACC_XY, BIT(IIO_CHAN_INFO_PEAK), 14),
148 IIO_CHAN_SOFT_TIMESTAMP(5),
151 static const struct iio_info adis16204_info = {
152 .read_raw = &adis16204_read_raw,
153 .write_raw = &adis16204_write_raw,
154 .update_scan_mode = adis_update_scan_mode,
155 .driver_module = THIS_MODULE,
158 static const char * const adis16204_status_error_msgs[] = {
159 [ADIS16204_DIAG_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
160 [ADIS16204_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
161 [ADIS16204_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
162 [ADIS16204_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
163 [ADIS16204_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.975V",
166 static const struct adis_data adis16204_data = {
167 .read_delay = 20,
168 .msc_ctrl_reg = ADIS16204_MSC_CTRL,
169 .glob_cmd_reg = ADIS16204_GLOB_CMD,
170 .diag_stat_reg = ADIS16204_DIAG_STAT,
172 .self_test_mask = ADIS16204_MSC_CTRL_SELF_TEST_EN,
173 .startup_delay = ADIS16204_STARTUP_DELAY,
175 .status_error_msgs = adis16204_status_error_msgs,
176 .status_error_mask = BIT(ADIS16204_DIAG_STAT_SELFTEST_FAIL_BIT) |
177 BIT(ADIS16204_DIAG_STAT_SPI_FAIL_BIT) |
178 BIT(ADIS16204_DIAG_STAT_FLASH_UPT_BIT) |
179 BIT(ADIS16204_DIAG_STAT_POWER_HIGH_BIT) |
180 BIT(ADIS16204_DIAG_STAT_POWER_LOW_BIT),
183 static int adis16204_probe(struct spi_device *spi)
185 int ret;
186 struct adis *st;
187 struct iio_dev *indio_dev;
189 /* setup the industrialio driver allocated elements */
190 indio_dev = iio_device_alloc(sizeof(*st));
191 if (indio_dev == NULL) {
192 ret = -ENOMEM;
193 goto error_ret;
195 st = iio_priv(indio_dev);
196 /* this is only used for removal purposes */
197 spi_set_drvdata(spi, indio_dev);
199 indio_dev->name = spi->dev.driver->name;
200 indio_dev->dev.parent = &spi->dev;
201 indio_dev->info = &adis16204_info;
202 indio_dev->channels = adis16204_channels;
203 indio_dev->num_channels = ARRAY_SIZE(adis16204_channels);
204 indio_dev->modes = INDIO_DIRECT_MODE;
206 ret = adis_init(st, indio_dev, spi, &adis16204_data);
207 if (ret)
208 goto error_free_dev;
210 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
211 if (ret)
212 goto error_free_dev;
214 /* Get the device into a sane initial state */
215 ret = adis_initial_startup(st);
216 if (ret)
217 goto error_cleanup_buffer_trigger;
218 ret = iio_device_register(indio_dev);
219 if (ret)
220 goto error_cleanup_buffer_trigger;
222 return 0;
224 error_cleanup_buffer_trigger:
225 adis_cleanup_buffer_and_trigger(st, indio_dev);
226 error_free_dev:
227 iio_device_free(indio_dev);
228 error_ret:
229 return ret;
232 static int adis16204_remove(struct spi_device *spi)
234 struct iio_dev *indio_dev = spi_get_drvdata(spi);
235 struct adis *st = iio_priv(indio_dev);
237 iio_device_unregister(indio_dev);
238 adis_cleanup_buffer_and_trigger(st, indio_dev);
239 iio_device_free(indio_dev);
241 return 0;
244 static struct spi_driver adis16204_driver = {
245 .driver = {
246 .name = "adis16204",
247 .owner = THIS_MODULE,
249 .probe = adis16204_probe,
250 .remove = adis16204_remove,
252 module_spi_driver(adis16204_driver);
254 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
255 MODULE_DESCRIPTION("ADIS16204 High-g Digital Impact Sensor and Recorder");
256 MODULE_LICENSE("GPL v2");
257 MODULE_ALIAS("spi:adis16204");