]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/accel/adis16204_core.c
Merge branch 'raid56-experimental' into for-linus-3.9
[~andy/linux] / drivers / staging / iio / accel / adis16204_core.c
1 /*
2  * ADIS16204 Programmable High-g Digital Impact Sensor and Recorder
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/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 #include <linux/iio/buffer.h>
24 #include <linux/iio/imu/adis.h>
25
26 #include "adis16204.h"
27
28 /* Unique to this driver currently */
29
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 },
34 };
35
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)
40 {
41         struct adis *st = iio_priv(indio_dev);
42         int ret;
43         int bits;
44         u8 addr;
45         s16 val16;
46         int addrind;
47
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 */
61                         }
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;
78                         }
79                         return IIO_VAL_INT_PLUS_MICRO;
80                 default:
81                         return -EINVAL;
82                 }
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;
95                 }
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;
102                 }
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;
108         }
109         return -EINVAL;
110 }
111
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)
117 {
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;
130                 }
131                 val16 = val & ((1 << bits) - 1);
132                 addr = adis16204_addresses[chan->scan_index][1];
133                 return adis_write_reg_16(st, addr, val16);
134         }
135         return -EINVAL;
136 }
137
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                 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
144                 IIO_CHAN_INFO_PEAK_SEPARATE_BIT, 14),
145         ADIS_ACCEL_CHAN(Y, ADIS16204_YACCL_OUT, ADIS16204_SCAN_ACC_Y,
146                 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
147                 IIO_CHAN_INFO_PEAK_SEPARATE_BIT, 14),
148         ADIS_ACCEL_CHAN(ROOT_SUM_SQUARED_X_Y, ADIS16204_XY_RSS_OUT,
149                 ADIS16204_SCAN_ACC_XY, IIO_CHAN_INFO_PEAK_SEPARATE_BIT, 14),
150         IIO_CHAN_SOFT_TIMESTAMP(5),
151 };
152
153 static const struct iio_info adis16204_info = {
154         .read_raw = &adis16204_read_raw,
155         .write_raw = &adis16204_write_raw,
156         .update_scan_mode = adis_update_scan_mode,
157         .driver_module = THIS_MODULE,
158 };
159
160 static const char * const adis16204_status_error_msgs[] = {
161         [ADIS16204_DIAG_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
162         [ADIS16204_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
163         [ADIS16204_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
164         [ADIS16204_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
165         [ADIS16204_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.975V",
166 };
167
168 static const struct adis_data adis16204_data = {
169         .read_delay = 20,
170         .msc_ctrl_reg = ADIS16204_MSC_CTRL,
171         .glob_cmd_reg = ADIS16204_GLOB_CMD,
172         .diag_stat_reg = ADIS16204_DIAG_STAT,
173
174         .self_test_mask = ADIS16204_MSC_CTRL_SELF_TEST_EN,
175         .startup_delay = ADIS16204_STARTUP_DELAY,
176
177         .status_error_msgs = adis16204_status_error_msgs,
178         .status_error_mask = BIT(ADIS16204_DIAG_STAT_SELFTEST_FAIL_BIT) |
179                 BIT(ADIS16204_DIAG_STAT_SPI_FAIL_BIT) |
180                 BIT(ADIS16204_DIAG_STAT_FLASH_UPT_BIT) |
181                 BIT(ADIS16204_DIAG_STAT_POWER_HIGH_BIT) |
182                 BIT(ADIS16204_DIAG_STAT_POWER_LOW_BIT),
183 };
184
185 static int adis16204_probe(struct spi_device *spi)
186 {
187         int ret;
188         struct adis *st;
189         struct iio_dev *indio_dev;
190
191         /* setup the industrialio driver allocated elements */
192         indio_dev = iio_device_alloc(sizeof(*st));
193         if (indio_dev == NULL) {
194                 ret = -ENOMEM;
195                 goto error_ret;
196         }
197         st = iio_priv(indio_dev);
198         /* this is only used for removal purposes */
199         spi_set_drvdata(spi, indio_dev);
200
201         indio_dev->name = spi->dev.driver->name;
202         indio_dev->dev.parent = &spi->dev;
203         indio_dev->info = &adis16204_info;
204         indio_dev->channels = adis16204_channels;
205         indio_dev->num_channels = ARRAY_SIZE(adis16204_channels);
206         indio_dev->modes = INDIO_DIRECT_MODE;
207
208         ret = adis_init(st, indio_dev, spi, &adis16204_data);
209         if (ret)
210                 goto error_free_dev;
211
212         ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
213         if (ret)
214                 goto error_free_dev;
215
216         /* Get the device into a sane initial state */
217         ret = adis_initial_startup(st);
218         if (ret)
219                 goto error_cleanup_buffer_trigger;
220         ret = iio_device_register(indio_dev);
221         if (ret)
222                 goto error_cleanup_buffer_trigger;
223
224         return 0;
225
226 error_cleanup_buffer_trigger:
227         adis_cleanup_buffer_and_trigger(st, indio_dev);
228 error_free_dev:
229         iio_device_free(indio_dev);
230 error_ret:
231         return ret;
232 }
233
234 static int adis16204_remove(struct spi_device *spi)
235 {
236         struct iio_dev *indio_dev = spi_get_drvdata(spi);
237         struct adis *st = iio_priv(indio_dev);
238
239         iio_device_unregister(indio_dev);
240         adis_cleanup_buffer_and_trigger(st, indio_dev);
241         iio_device_free(indio_dev);
242
243         return 0;
244 }
245
246 static struct spi_driver adis16204_driver = {
247         .driver = {
248                 .name = "adis16204",
249                 .owner = THIS_MODULE,
250         },
251         .probe = adis16204_probe,
252         .remove = adis16204_remove,
253 };
254 module_spi_driver(adis16204_driver);
255
256 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
257 MODULE_DESCRIPTION("ADIS16204 High-g Digital Impact Sensor and Recorder");
258 MODULE_LICENSE("GPL v2");
259 MODULE_ALIAS("spi:adis16204");