GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / staging / iio / accel / adis16201.c
1 /*
2  * ADIS16201 Dual-Axis Digital Inclinometer and Accelerometer
3  *
4  * Copyright 2010 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 #define ADIS16201_STARTUP_DELAY 220 /* ms */
24
25 /* Flash memory write count */
26 #define ADIS16201_FLASH_CNT      0x00
27
28 /* Output, power supply */
29 #define ADIS16201_SUPPLY_OUT     0x02
30
31 /* Output, x-axis accelerometer */
32 #define ADIS16201_XACCL_OUT      0x04
33
34 /* Output, y-axis accelerometer */
35 #define ADIS16201_YACCL_OUT      0x06
36
37 /* Output, auxiliary ADC input */
38 #define ADIS16201_AUX_ADC        0x08
39
40 /* Output, temperature */
41 #define ADIS16201_TEMP_OUT       0x0A
42
43 /* Output, x-axis inclination */
44 #define ADIS16201_XINCL_OUT      0x0C
45
46 /* Output, y-axis inclination */
47 #define ADIS16201_YINCL_OUT      0x0E
48
49 /* Calibration, x-axis acceleration offset */
50 #define ADIS16201_XACCL_OFFS     0x10
51
52 /* Calibration, y-axis acceleration offset */
53 #define ADIS16201_YACCL_OFFS     0x12
54
55 /* x-axis acceleration scale factor */
56 #define ADIS16201_XACCL_SCALE    0x14
57
58 /* y-axis acceleration scale factor */
59 #define ADIS16201_YACCL_SCALE    0x16
60
61 /* Calibration, x-axis inclination offset */
62 #define ADIS16201_XINCL_OFFS     0x18
63
64 /* Calibration, y-axis inclination offset */
65 #define ADIS16201_YINCL_OFFS     0x1A
66
67 /* x-axis inclination scale factor */
68 #define ADIS16201_XINCL_SCALE    0x1C
69
70 /* y-axis inclination scale factor */
71 #define ADIS16201_YINCL_SCALE    0x1E
72
73 /* Alarm 1 amplitude threshold */
74 #define ADIS16201_ALM_MAG1       0x20
75
76 /* Alarm 2 amplitude threshold */
77 #define ADIS16201_ALM_MAG2       0x22
78
79 /* Alarm 1, sample period */
80 #define ADIS16201_ALM_SMPL1      0x24
81
82 /* Alarm 2, sample period */
83 #define ADIS16201_ALM_SMPL2      0x26
84
85 /* Alarm control */
86 #define ADIS16201_ALM_CTRL       0x28
87
88 /* Auxiliary DAC data */
89 #define ADIS16201_AUX_DAC        0x30
90
91 /* General-purpose digital input/output control */
92 #define ADIS16201_GPIO_CTRL      0x32
93
94 /* Miscellaneous control */
95 #define ADIS16201_MSC_CTRL       0x34
96
97 /* Internal sample period (rate) control */
98 #define ADIS16201_SMPL_PRD       0x36
99
100 /* Operation, filter configuration */
101 #define ADIS16201_AVG_CNT        0x38
102
103 /* Operation, sleep mode control */
104 #define ADIS16201_SLP_CNT        0x3A
105
106 /* Diagnostics, system status register */
107 #define ADIS16201_DIAG_STAT      0x3C
108
109 /* Operation, system command register */
110 #define ADIS16201_GLOB_CMD       0x3E
111
112 /* MSC_CTRL */
113
114 /* Self-test enable */
115 #define ADIS16201_MSC_CTRL_SELF_TEST_EN         BIT(8)
116
117 /* Data-ready enable: 1 = enabled, 0 = disabled */
118 #define ADIS16201_MSC_CTRL_DATA_RDY_EN          BIT(2)
119
120 /* Data-ready polarity: 1 = active high, 0 = active low */
121 #define ADIS16201_MSC_CTRL_ACTIVE_HIGH          BIT(1)
122
123 /* Data-ready line selection: 1 = DIO1, 0 = DIO0 */
124 #define ADIS16201_MSC_CTRL_DATA_RDY_DIO1        BIT(0)
125
126 /* DIAG_STAT */
127
128 /* Alarm 2 status: 1 = alarm active, 0 = alarm inactive */
129 #define ADIS16201_DIAG_STAT_ALARM2        BIT(9)
130
131 /* Alarm 1 status: 1 = alarm active, 0 = alarm inactive */
132 #define ADIS16201_DIAG_STAT_ALARM1        BIT(8)
133
134 /* SPI communications failure */
135 #define ADIS16201_DIAG_STAT_SPI_FAIL_BIT   3
136
137 /* Flash update failure */
138 #define ADIS16201_DIAG_STAT_FLASH_UPT_BIT  2
139
140 /* Power supply above 3.625 V */
141 #define ADIS16201_DIAG_STAT_POWER_HIGH_BIT 1
142
143 /* Power supply below 3.15 V */
144 #define ADIS16201_DIAG_STAT_POWER_LOW_BIT  0
145
146 /* GLOB_CMD */
147
148 #define ADIS16201_GLOB_CMD_SW_RESET     BIT(7)
149 #define ADIS16201_GLOB_CMD_FACTORY_CAL  BIT(1)
150
151 #define ADIS16201_ERROR_ACTIVE          BIT(14)
152
153 enum adis16201_scan {
154         ADIS16201_SCAN_ACC_X,
155         ADIS16201_SCAN_ACC_Y,
156         ADIS16201_SCAN_INCLI_X,
157         ADIS16201_SCAN_INCLI_Y,
158         ADIS16201_SCAN_SUPPLY,
159         ADIS16201_SCAN_AUX_ADC,
160         ADIS16201_SCAN_TEMP,
161 };
162
163 static const u8 adis16201_addresses[] = {
164         [ADIS16201_SCAN_ACC_X] = ADIS16201_XACCL_OFFS,
165         [ADIS16201_SCAN_ACC_Y] = ADIS16201_YACCL_OFFS,
166         [ADIS16201_SCAN_INCLI_X] = ADIS16201_XINCL_OFFS,
167         [ADIS16201_SCAN_INCLI_Y] = ADIS16201_YINCL_OFFS,
168 };
169
170 static int adis16201_read_raw(struct iio_dev *indio_dev,
171                               struct iio_chan_spec const *chan,
172                               int *val, int *val2,
173                               long mask)
174 {
175         struct adis *st = iio_priv(indio_dev);
176         int ret;
177         int bits;
178         u8 addr;
179         s16 val16;
180
181         switch (mask) {
182         case IIO_CHAN_INFO_RAW:
183                 return adis_single_conversion(indio_dev, chan,
184                                 ADIS16201_ERROR_ACTIVE, val);
185         case IIO_CHAN_INFO_SCALE:
186                 switch (chan->type) {
187                 case IIO_VOLTAGE:
188                         if (chan->channel == 0) {
189                                 *val = 1;
190                                 *val2 = 220000; /* 1.22 mV */
191                         } else {
192                                 *val = 0;
193                                 *val2 = 610000; /* 0.610 mV */
194                         }
195                         return IIO_VAL_INT_PLUS_MICRO;
196                 case IIO_TEMP:
197                         *val = -470; /* 0.47 C */
198                         *val2 = 0;
199                         return IIO_VAL_INT_PLUS_MICRO;
200                 case IIO_ACCEL:
201                         *val = 0;
202                         *val2 = IIO_G_TO_M_S_2(462400); /* 0.4624 mg */
203                         return IIO_VAL_INT_PLUS_NANO;
204                 case IIO_INCLI:
205                         *val = 0;
206                         *val2 = 100000; /* 0.1 degree */
207                         return IIO_VAL_INT_PLUS_MICRO;
208                 default:
209                         return -EINVAL;
210                 }
211                 break;
212         case IIO_CHAN_INFO_OFFSET:
213                 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
214                 return IIO_VAL_INT;
215         case IIO_CHAN_INFO_CALIBBIAS:
216                 switch (chan->type) {
217                 case IIO_ACCEL:
218                         bits = 12;
219                         break;
220                 case IIO_INCLI:
221                         bits = 9;
222                         break;
223                 default:
224                         return -EINVAL;
225                 }
226                 addr = adis16201_addresses[chan->scan_index];
227                 ret = adis_read_reg_16(st, addr, &val16);
228                 if (ret)
229                         return ret;
230                 val16 &= (1 << bits) - 1;
231                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
232                 *val = val16;
233                 return IIO_VAL_INT;
234         }
235         return -EINVAL;
236 }
237
238 static int adis16201_write_raw(struct iio_dev *indio_dev,
239                                struct iio_chan_spec const *chan,
240                                int val,
241                                int val2,
242                                long mask)
243 {
244         struct adis *st = iio_priv(indio_dev);
245         int bits;
246         s16 val16;
247         u8 addr;
248
249         switch (mask) {
250         case IIO_CHAN_INFO_CALIBBIAS:
251                 switch (chan->type) {
252                 case IIO_ACCEL:
253                         bits = 12;
254                         break;
255                 case IIO_INCLI:
256                         bits = 9;
257                         break;
258                 default:
259                         return -EINVAL;
260                 }
261                 val16 = val & ((1 << bits) - 1);
262                 addr = adis16201_addresses[chan->scan_index];
263                 return adis_write_reg_16(st, addr, val16);
264         }
265         return -EINVAL;
266 }
267
268 static const struct iio_chan_spec adis16201_channels[] = {
269         ADIS_SUPPLY_CHAN(ADIS16201_SUPPLY_OUT, ADIS16201_SCAN_SUPPLY, 0, 12),
270         ADIS_TEMP_CHAN(ADIS16201_TEMP_OUT, ADIS16201_SCAN_TEMP, 0, 12),
271         ADIS_ACCEL_CHAN(X, ADIS16201_XACCL_OUT, ADIS16201_SCAN_ACC_X,
272                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
273         ADIS_ACCEL_CHAN(Y, ADIS16201_YACCL_OUT, ADIS16201_SCAN_ACC_Y,
274                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
275         ADIS_AUX_ADC_CHAN(ADIS16201_AUX_ADC, ADIS16201_SCAN_AUX_ADC, 0, 12),
276         ADIS_INCLI_CHAN(X, ADIS16201_XINCL_OUT, ADIS16201_SCAN_INCLI_X,
277                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
278         ADIS_INCLI_CHAN(X, ADIS16201_YINCL_OUT, ADIS16201_SCAN_INCLI_Y,
279                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
280         IIO_CHAN_SOFT_TIMESTAMP(7)
281 };
282
283 static const struct iio_info adis16201_info = {
284         .read_raw = adis16201_read_raw,
285         .write_raw = adis16201_write_raw,
286         .update_scan_mode = adis_update_scan_mode,
287         .driver_module = THIS_MODULE,
288 };
289
290 static const char * const adis16201_status_error_msgs[] = {
291         [ADIS16201_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
292         [ADIS16201_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
293         [ADIS16201_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
294         [ADIS16201_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
295 };
296
297 static const struct adis_data adis16201_data = {
298         .read_delay = 20,
299         .msc_ctrl_reg = ADIS16201_MSC_CTRL,
300         .glob_cmd_reg = ADIS16201_GLOB_CMD,
301         .diag_stat_reg = ADIS16201_DIAG_STAT,
302
303         .self_test_mask = ADIS16201_MSC_CTRL_SELF_TEST_EN,
304         .self_test_no_autoclear = true,
305         .startup_delay = ADIS16201_STARTUP_DELAY,
306
307         .status_error_msgs = adis16201_status_error_msgs,
308         .status_error_mask = BIT(ADIS16201_DIAG_STAT_SPI_FAIL_BIT) |
309                 BIT(ADIS16201_DIAG_STAT_FLASH_UPT_BIT) |
310                 BIT(ADIS16201_DIAG_STAT_POWER_HIGH_BIT) |
311                 BIT(ADIS16201_DIAG_STAT_POWER_LOW_BIT),
312 };
313
314 static int adis16201_probe(struct spi_device *spi)
315 {
316         int ret;
317         struct adis *st;
318         struct iio_dev *indio_dev;
319
320         /* setup the industrialio driver allocated elements */
321         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
322         if (!indio_dev)
323                 return -ENOMEM;
324
325         st = iio_priv(indio_dev);
326         /* this is only used for removal purposes */
327         spi_set_drvdata(spi, indio_dev);
328
329         indio_dev->name = spi->dev.driver->name;
330         indio_dev->dev.parent = &spi->dev;
331         indio_dev->info = &adis16201_info;
332
333         indio_dev->channels = adis16201_channels;
334         indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
335         indio_dev->modes = INDIO_DIRECT_MODE;
336
337         ret = adis_init(st, indio_dev, spi, &adis16201_data);
338         if (ret)
339                 return ret;
340         ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
341         if (ret)
342                 return ret;
343
344         /* Get the device into a sane initial state */
345         ret = adis_initial_startup(st);
346         if (ret)
347                 goto error_cleanup_buffer_trigger;
348
349         ret = iio_device_register(indio_dev);
350         if (ret < 0)
351                 goto error_cleanup_buffer_trigger;
352         return 0;
353
354 error_cleanup_buffer_trigger:
355         adis_cleanup_buffer_and_trigger(st, indio_dev);
356         return ret;
357 }
358
359 static int adis16201_remove(struct spi_device *spi)
360 {
361         struct iio_dev *indio_dev = spi_get_drvdata(spi);
362         struct adis *st = iio_priv(indio_dev);
363
364         iio_device_unregister(indio_dev);
365         adis_cleanup_buffer_and_trigger(st, indio_dev);
366
367         return 0;
368 }
369
370 static struct spi_driver adis16201_driver = {
371         .driver = {
372                 .name = "adis16201",
373         },
374         .probe = adis16201_probe,
375         .remove = adis16201_remove,
376 };
377 module_spi_driver(adis16201_driver);
378
379 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
380 MODULE_DESCRIPTION("Analog Devices ADIS16201 Dual-Axis Digital Inclinometer and Accelerometer");
381 MODULE_LICENSE("GPL v2");
382 MODULE_ALIAS("spi:adis16201");