Linux-libre 3.10.98-gnu
[librecmc/linux-libre.git] / drivers / staging / iio / accel / adis16240_core.c
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>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/imu/adis.h>
26
27 #include "adis16240.h"
28
29 static ssize_t adis16240_spi_read_signed(struct device *dev,
30                 struct device_attribute *attr,
31                 char *buf,
32                 unsigned bits)
33 {
34         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
35         struct adis *st = iio_priv(indio_dev);
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
41         ret = adis_read_reg_16(st,
42                                         this_attr->address, (u16 *)&val);
43         if (ret)
44                 return ret;
45
46         if (val & ADIS16240_ERROR_ACTIVE)
47                 adis_check_status(st);
48
49         val = ((s16)(val << shift) >> shift);
50         return sprintf(buf, "%d\n", val);
51 }
52
53 static ssize_t adis16240_read_12bit_signed(struct device *dev,
54                 struct device_attribute *attr,
55                 char *buf)
56 {
57         ssize_t ret;
58         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
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
68 static IIO_DEVICE_ATTR(in_accel_xyz_squared_peak_raw, S_IRUGO,
69                        adis16240_read_12bit_signed, NULL,
70                        ADIS16240_XYZPEAK_OUT);
71
72 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
73
74 static 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 },
78 };
79
80 static 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 {
85         struct adis *st = iio_priv(indio_dev);
86         int ret;
87         int bits;
88         u8 addr;
89         s16 val16;
90
91         switch (mask) {
92         case IIO_CHAN_INFO_RAW:
93                 return adis_single_conversion(indio_dev, chan,
94                                 ADIS16240_ERROR_ACTIVE, val);
95         case IIO_CHAN_INFO_SCALE:
96                 switch (chan->type) {
97                 case IIO_VOLTAGE:
98                         if (chan->channel == 0) {
99                                 *val = 4;
100                                 *val2 = 880000; /* 4.88 mV */
101                                 return IIO_VAL_INT_PLUS_MICRO;
102                         } else {
103                                 return -EINVAL;
104                         }
105                 case IIO_TEMP:
106                         *val = 244; /* 0.244 C */
107                         *val2 = 0;
108                         return IIO_VAL_INT_PLUS_MICRO;
109                 case IIO_ACCEL:
110                         *val = 0;
111                         *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
112                         return IIO_VAL_INT_PLUS_MICRO;
113                 default:
114                         return -EINVAL;
115                 }
116                 break;
117         case IIO_CHAN_INFO_PEAK_SCALE:
118                 *val = 0;
119                 *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
120                 return IIO_VAL_INT_PLUS_MICRO;
121         case IIO_CHAN_INFO_OFFSET:
122                 *val = 25000 / 244 - 0x133; /* 25 C = 0x133 */
123                 return IIO_VAL_INT;
124         case IIO_CHAN_INFO_CALIBBIAS:
125                 bits = 10;
126                 mutex_lock(&indio_dev->mlock);
127                 addr = adis16240_addresses[chan->scan_index][0];
128                 ret = adis_read_reg_16(st, addr, &val16);
129                 if (ret) {
130                         mutex_unlock(&indio_dev->mlock);
131                         return ret;
132                 }
133                 val16 &= (1 << bits) - 1;
134                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
135                 *val = val16;
136                 mutex_unlock(&indio_dev->mlock);
137                 return IIO_VAL_INT;
138         case IIO_CHAN_INFO_PEAK:
139                 bits = 10;
140                 mutex_lock(&indio_dev->mlock);
141                 addr = adis16240_addresses[chan->scan_index][1];
142                 ret = adis_read_reg_16(st, addr, &val16);
143                 if (ret) {
144                         mutex_unlock(&indio_dev->mlock);
145                         return ret;
146                 }
147                 val16 &= (1 << bits) - 1;
148                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
149                 *val = val16;
150                 mutex_unlock(&indio_dev->mlock);
151                 return IIO_VAL_INT;
152         }
153         return -EINVAL;
154 }
155
156 static int adis16240_write_raw(struct iio_dev *indio_dev,
157                                struct iio_chan_spec const *chan,
158                                int val,
159                                int val2,
160                                long mask)
161 {
162         struct adis *st = iio_priv(indio_dev);
163         int bits = 10;
164         s16 val16;
165         u8 addr;
166         switch (mask) {
167         case IIO_CHAN_INFO_CALIBBIAS:
168                 val16 = val & ((1 << bits) - 1);
169                 addr = adis16240_addresses[chan->scan_index][0];
170                 return adis_write_reg_16(st, addr, val16);
171         }
172         return -EINVAL;
173 }
174
175 static const struct iio_chan_spec adis16240_channels[] = {
176         ADIS_SUPPLY_CHAN(ADIS16240_SUPPLY_OUT, ADIS16240_SCAN_SUPPLY, 10),
177         ADIS_AUX_ADC_CHAN(ADIS16240_AUX_ADC, ADIS16240_SCAN_AUX_ADC, 10),
178         ADIS_ACCEL_CHAN(X, ADIS16240_XACCL_OUT, ADIS16240_SCAN_ACC_X,
179                 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK), 10),
180         ADIS_ACCEL_CHAN(Y, ADIS16240_YACCL_OUT, ADIS16240_SCAN_ACC_Y,
181                 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK), 10),
182         ADIS_ACCEL_CHAN(Z, ADIS16240_ZACCL_OUT, ADIS16240_SCAN_ACC_Z,
183                 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK), 10),
184         ADIS_TEMP_CHAN(ADIS16240_TEMP_OUT, ADIS16240_SCAN_TEMP, 10),
185         IIO_CHAN_SOFT_TIMESTAMP(6)
186 };
187
188 static struct attribute *adis16240_attributes[] = {
189         &iio_dev_attr_in_accel_xyz_squared_peak_raw.dev_attr.attr,
190         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
191         NULL
192 };
193
194 static const struct attribute_group adis16240_attribute_group = {
195         .attrs = adis16240_attributes,
196 };
197
198 static const struct iio_info adis16240_info = {
199         .attrs = &adis16240_attribute_group,
200         .read_raw = &adis16240_read_raw,
201         .write_raw = &adis16240_write_raw,
202         .update_scan_mode = adis_update_scan_mode,
203         .driver_module = THIS_MODULE,
204 };
205
206 static const char * const adis16240_status_error_msgs[] = {
207         [ADIS16240_DIAG_STAT_PWRON_FAIL_BIT] = "Power on, self-test failed",
208         [ADIS16240_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
209         [ADIS16240_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
210         [ADIS16240_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
211         [ADIS16240_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.225V",
212 };
213
214 static const struct adis_data adis16240_data = {
215         .write_delay = 35,
216         .read_delay = 35,
217         .msc_ctrl_reg = ADIS16240_MSC_CTRL,
218         .glob_cmd_reg = ADIS16240_GLOB_CMD,
219         .diag_stat_reg = ADIS16240_DIAG_STAT,
220
221         .self_test_mask = ADIS16240_MSC_CTRL_SELF_TEST_EN,
222         .startup_delay = ADIS16240_STARTUP_DELAY,
223
224         .status_error_msgs = adis16240_status_error_msgs,
225         .status_error_mask = BIT(ADIS16240_DIAG_STAT_PWRON_FAIL_BIT) |
226                 BIT(ADIS16240_DIAG_STAT_SPI_FAIL_BIT) |
227                 BIT(ADIS16240_DIAG_STAT_FLASH_UPT_BIT) |
228                 BIT(ADIS16240_DIAG_STAT_POWER_HIGH_BIT) |
229                 BIT(ADIS16240_DIAG_STAT_POWER_LOW_BIT),
230 };
231
232 static int adis16240_probe(struct spi_device *spi)
233 {
234         int ret;
235         struct adis *st;
236         struct iio_dev *indio_dev;
237
238         /* setup the industrialio driver allocated elements */
239         indio_dev = iio_device_alloc(sizeof(*st));
240         if (indio_dev == NULL) {
241                 ret = -ENOMEM;
242                 goto error_ret;
243         }
244         st = iio_priv(indio_dev);
245         /* this is only used for removal purposes */
246         spi_set_drvdata(spi, indio_dev);
247
248         indio_dev->name = spi->dev.driver->name;
249         indio_dev->dev.parent = &spi->dev;
250         indio_dev->info = &adis16240_info;
251         indio_dev->channels = adis16240_channels;
252         indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
253         indio_dev->modes = INDIO_DIRECT_MODE;
254
255         ret = adis_init(st, indio_dev, spi, &adis16240_data);
256         if (ret)
257                 goto error_free_dev;
258         ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
259         if (ret)
260                 goto error_free_dev;
261
262         /* Get the device into a sane initial state */
263         ret = adis_initial_startup(st);
264         if (ret)
265                 goto error_cleanup_buffer_trigger;
266         ret = iio_device_register(indio_dev);
267         if (ret)
268                 goto error_cleanup_buffer_trigger;
269         return 0;
270
271 error_cleanup_buffer_trigger:
272         adis_cleanup_buffer_and_trigger(st, indio_dev);
273 error_free_dev:
274         iio_device_free(indio_dev);
275 error_ret:
276         return ret;
277 }
278
279 static int adis16240_remove(struct spi_device *spi)
280 {
281         struct iio_dev *indio_dev = spi_get_drvdata(spi);
282         struct adis *st = iio_priv(indio_dev);
283
284         iio_device_unregister(indio_dev);
285         adis_cleanup_buffer_and_trigger(st, indio_dev);
286         iio_device_free(indio_dev);
287
288         return 0;
289 }
290
291 static struct spi_driver adis16240_driver = {
292         .driver = {
293                 .name = "adis16240",
294                 .owner = THIS_MODULE,
295         },
296         .probe = adis16240_probe,
297         .remove = adis16240_remove,
298 };
299 module_spi_driver(adis16240_driver);
300
301 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
302 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
303 MODULE_LICENSE("GPL v2");
304 MODULE_ALIAS("spi:adis16240");