Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / iio / accel / adis16203_core.c
1 /*
2  * ADIS16203 Programmable Digital Vibration Sensor driver
3  *
4  * Copyright 2030 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/module.h>
17
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/imu/adis.h>
22
23 #include "adis16203.h"
24
25 #define DRIVER_NAME             "adis16203"
26
27 static const u8 adis16203_addresses[] = {
28         [ADIS16203_SCAN_INCLI_X] = ADIS16203_INCL_NULL,
29 };
30
31 static int adis16203_write_raw(struct iio_dev *indio_dev,
32                                struct iio_chan_spec const *chan,
33                                int val,
34                                int val2,
35                                long mask)
36 {
37         struct adis *st = iio_priv(indio_dev);
38         /* currently only one writable parameter which keeps this simple */
39         u8 addr = adis16203_addresses[chan->scan_index];
40
41         return adis_write_reg_16(st, addr, val & 0x3FFF);
42 }
43
44 static int adis16203_read_raw(struct iio_dev *indio_dev,
45                               struct iio_chan_spec const *chan,
46                               int *val, int *val2,
47                               long mask)
48 {
49         struct adis *st = iio_priv(indio_dev);
50         int ret;
51         int bits;
52         u8 addr;
53         s16 val16;
54
55         switch (mask) {
56         case IIO_CHAN_INFO_RAW:
57                 return adis_single_conversion(indio_dev, chan,
58                                 ADIS16203_ERROR_ACTIVE, val);
59         case IIO_CHAN_INFO_SCALE:
60                 switch (chan->type) {
61                 case IIO_VOLTAGE:
62                         if (chan->channel == 0) {
63                                 *val = 1;
64                                 *val2 = 220000; /* 1.22 mV */
65                         } else {
66                                 *val = 0;
67                                 *val2 = 610000; /* 0.61 mV */
68                         }
69                         return IIO_VAL_INT_PLUS_MICRO;
70                 case IIO_TEMP:
71                         *val = -470; /* -0.47 C */
72                         *val2 = 0;
73                         return IIO_VAL_INT_PLUS_MICRO;
74                 case IIO_INCLI:
75                         *val = 0;
76                         *val2 = 25000; /* 0.025 degree */
77                         return IIO_VAL_INT_PLUS_MICRO;
78                 default:
79                         return -EINVAL;
80                 }
81         case IIO_CHAN_INFO_OFFSET:
82                 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
83                 return IIO_VAL_INT;
84         case IIO_CHAN_INFO_CALIBBIAS:
85                 bits = 14;
86                 mutex_lock(&indio_dev->mlock);
87                 addr = adis16203_addresses[chan->scan_index];
88                 ret = adis_read_reg_16(st, addr, &val16);
89                 if (ret) {
90                         mutex_unlock(&indio_dev->mlock);
91                         return ret;
92                 }
93                 val16 &= (1 << bits) - 1;
94                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
95                 *val = val16;
96                 mutex_unlock(&indio_dev->mlock);
97                 return IIO_VAL_INT;
98         default:
99                 return -EINVAL;
100         }
101 }
102
103 static const struct iio_chan_spec adis16203_channels[] = {
104         ADIS_SUPPLY_CHAN(ADIS16203_SUPPLY_OUT, ADIS16203_SCAN_SUPPLY, 0, 12),
105         ADIS_AUX_ADC_CHAN(ADIS16203_AUX_ADC, ADIS16203_SCAN_AUX_ADC, 0, 12),
106         ADIS_INCLI_CHAN(X, ADIS16203_XINCL_OUT, ADIS16203_SCAN_INCLI_X,
107                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
108         /* Fixme: Not what it appears to be - see data sheet */
109         ADIS_INCLI_CHAN(Y, ADIS16203_YINCL_OUT, ADIS16203_SCAN_INCLI_Y,
110                         0, 0, 14),
111         ADIS_TEMP_CHAN(ADIS16203_TEMP_OUT, ADIS16203_SCAN_TEMP, 0, 12),
112         IIO_CHAN_SOFT_TIMESTAMP(5),
113 };
114
115 static const struct iio_info adis16203_info = {
116         .read_raw = &adis16203_read_raw,
117         .write_raw = &adis16203_write_raw,
118         .update_scan_mode = adis_update_scan_mode,
119         .driver_module = THIS_MODULE,
120 };
121
122 static const char * const adis16203_status_error_msgs[] = {
123         [ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
124         [ADIS16203_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
125         [ADIS16203_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
126         [ADIS16203_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
127         [ADIS16203_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
128 };
129
130 static const struct adis_data adis16203_data = {
131         .read_delay = 20,
132         .msc_ctrl_reg = ADIS16203_MSC_CTRL,
133         .glob_cmd_reg = ADIS16203_GLOB_CMD,
134         .diag_stat_reg = ADIS16203_DIAG_STAT,
135
136         .self_test_mask = ADIS16203_MSC_CTRL_SELF_TEST_EN,
137         .startup_delay = ADIS16203_STARTUP_DELAY,
138
139         .status_error_msgs = adis16203_status_error_msgs,
140         .status_error_mask = BIT(ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT) |
141                 BIT(ADIS16203_DIAG_STAT_SPI_FAIL_BIT) |
142                 BIT(ADIS16203_DIAG_STAT_FLASH_UPT_BIT) |
143                 BIT(ADIS16203_DIAG_STAT_POWER_HIGH_BIT) |
144                 BIT(ADIS16203_DIAG_STAT_POWER_LOW_BIT),
145 };
146
147 static int adis16203_probe(struct spi_device *spi)
148 {
149         int ret;
150         struct iio_dev *indio_dev;
151         struct adis *st;
152
153         /* setup the industrialio driver allocated elements */
154         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
155         if (!indio_dev)
156                 return -ENOMEM;
157         st = iio_priv(indio_dev);
158         /* this is only used for removal purposes */
159         spi_set_drvdata(spi, indio_dev);
160
161         indio_dev->name = spi->dev.driver->name;
162         indio_dev->dev.parent = &spi->dev;
163         indio_dev->channels = adis16203_channels;
164         indio_dev->num_channels = ARRAY_SIZE(adis16203_channels);
165         indio_dev->info = &adis16203_info;
166         indio_dev->modes = INDIO_DIRECT_MODE;
167
168         ret = adis_init(st, indio_dev, spi, &adis16203_data);
169         if (ret)
170                 return ret;
171
172         ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
173         if (ret)
174                 return ret;
175
176         /* Get the device into a sane initial state */
177         ret = adis_initial_startup(st);
178         if (ret)
179                 goto error_cleanup_buffer_trigger;
180
181         ret = iio_device_register(indio_dev);
182         if (ret)
183                 goto error_cleanup_buffer_trigger;
184
185         return 0;
186
187 error_cleanup_buffer_trigger:
188         adis_cleanup_buffer_and_trigger(st, indio_dev);
189         return ret;
190 }
191
192 static int adis16203_remove(struct spi_device *spi)
193 {
194         struct iio_dev *indio_dev = spi_get_drvdata(spi);
195         struct adis *st = iio_priv(indio_dev);
196
197         iio_device_unregister(indio_dev);
198         adis_cleanup_buffer_and_trigger(st, indio_dev);
199
200         return 0;
201 }
202
203 static struct spi_driver adis16203_driver = {
204         .driver = {
205                 .name = "adis16203",
206                 .owner = THIS_MODULE,
207         },
208         .probe = adis16203_probe,
209         .remove = adis16203_remove,
210 };
211 module_spi_driver(adis16203_driver);
212
213 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
214 MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
215 MODULE_LICENSE("GPL v2");
216 MODULE_ALIAS("spi:adis16203");