staging:iio:accel:adis move to info_mask_(shared_by_type/separate)
[linux-2.6/btrfs-unstable.git] / drivers / staging / iio / accel / adis16203_core.c
blobb08ac8fdeee2726cd38d5edcda192410861b2935
1 /*
2 * ADIS16203 Programmable Digital Vibration Sensor driver
4 * Copyright 2030 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
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>
23 #include "adis16203.h"
25 #define DRIVER_NAME "adis16203"
27 static const u8 adis16203_addresses[] = {
28 [ADIS16203_SCAN_INCLI_X] = ADIS16203_INCL_NULL,
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)
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 return adis_write_reg_16(st, addr, val & 0x3FFF);
43 static int adis16203_read_raw(struct iio_dev *indio_dev,
44 struct iio_chan_spec const *chan,
45 int *val, int *val2,
46 long mask)
48 struct adis *st = iio_priv(indio_dev);
49 int ret;
50 int bits;
51 u8 addr;
52 s16 val16;
53 switch (mask) {
54 case IIO_CHAN_INFO_RAW:
55 return adis_single_conversion(indio_dev, chan,
56 ADIS16203_ERROR_ACTIVE, val);
57 case IIO_CHAN_INFO_SCALE:
58 switch (chan->type) {
59 case IIO_VOLTAGE:
60 if (chan->channel == 0) {
61 *val = 1;
62 *val2 = 220000; /* 1.22 mV */
63 } else {
64 *val = 0;
65 *val2 = 610000; /* 0.61 mV */
67 return IIO_VAL_INT_PLUS_MICRO;
68 case IIO_TEMP:
69 *val = -470; /* -0.47 C */
70 *val2 = 0;
71 return IIO_VAL_INT_PLUS_MICRO;
72 case IIO_INCLI:
73 *val = 0;
74 *val2 = 25000; /* 0.025 degree */
75 return IIO_VAL_INT_PLUS_MICRO;
76 default:
77 return -EINVAL;
79 case IIO_CHAN_INFO_OFFSET:
80 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
81 return IIO_VAL_INT;
82 case IIO_CHAN_INFO_CALIBBIAS:
83 bits = 14;
84 mutex_lock(&indio_dev->mlock);
85 addr = adis16203_addresses[chan->scan_index];
86 ret = adis_read_reg_16(st, addr, &val16);
87 if (ret) {
88 mutex_unlock(&indio_dev->mlock);
89 return ret;
91 val16 &= (1 << bits) - 1;
92 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
93 *val = val16;
94 mutex_unlock(&indio_dev->mlock);
95 return IIO_VAL_INT;
96 default:
97 return -EINVAL;
101 static const struct iio_chan_spec adis16203_channels[] = {
102 ADIS_SUPPLY_CHAN(ADIS16203_SUPPLY_OUT, ADIS16203_SCAN_SUPPLY, 12),
103 ADIS_AUX_ADC_CHAN(ADIS16203_AUX_ADC, ADIS16203_SCAN_AUX_ADC, 12),
104 ADIS_INCLI_CHAN(X, ADIS16203_XINCL_OUT, ADIS16203_SCAN_INCLI_X,
105 BIT(IIO_CHAN_INFO_CALIBBIAS), 14),
106 /* Fixme: Not what it appears to be - see data sheet */
107 ADIS_INCLI_CHAN(Y, ADIS16203_YINCL_OUT, ADIS16203_SCAN_INCLI_Y, 0, 14),
108 ADIS_TEMP_CHAN(ADIS16203_TEMP_OUT, ADIS16203_SCAN_TEMP, 12),
109 IIO_CHAN_SOFT_TIMESTAMP(5),
112 static const struct iio_info adis16203_info = {
113 .read_raw = &adis16203_read_raw,
114 .write_raw = &adis16203_write_raw,
115 .update_scan_mode = adis_update_scan_mode,
116 .driver_module = THIS_MODULE,
119 static const char * const adis16203_status_error_msgs[] = {
120 [ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
121 [ADIS16203_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
122 [ADIS16203_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
123 [ADIS16203_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
124 [ADIS16203_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
127 static const struct adis_data adis16203_data = {
128 .read_delay = 20,
129 .msc_ctrl_reg = ADIS16203_MSC_CTRL,
130 .glob_cmd_reg = ADIS16203_GLOB_CMD,
131 .diag_stat_reg = ADIS16203_DIAG_STAT,
133 .self_test_mask = ADIS16203_MSC_CTRL_SELF_TEST_EN,
134 .startup_delay = ADIS16203_STARTUP_DELAY,
136 .status_error_msgs = adis16203_status_error_msgs,
137 .status_error_mask = BIT(ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT) |
138 BIT(ADIS16203_DIAG_STAT_SPI_FAIL_BIT) |
139 BIT(ADIS16203_DIAG_STAT_FLASH_UPT_BIT) |
140 BIT(ADIS16203_DIAG_STAT_POWER_HIGH_BIT) |
141 BIT(ADIS16203_DIAG_STAT_POWER_LOW_BIT),
144 static int adis16203_probe(struct spi_device *spi)
146 int ret;
147 struct iio_dev *indio_dev;
148 struct adis *st;
150 /* setup the industrialio driver allocated elements */
151 indio_dev = iio_device_alloc(sizeof(*st));
152 if (indio_dev == NULL) {
153 ret = -ENOMEM;
154 goto error_ret;
156 st = iio_priv(indio_dev);
157 /* this is only used for removal purposes */
158 spi_set_drvdata(spi, indio_dev);
160 indio_dev->name = spi->dev.driver->name;
161 indio_dev->dev.parent = &spi->dev;
162 indio_dev->channels = adis16203_channels;
163 indio_dev->num_channels = ARRAY_SIZE(adis16203_channels);
164 indio_dev->info = &adis16203_info;
165 indio_dev->modes = INDIO_DIRECT_MODE;
167 ret = adis_init(st, indio_dev, spi, &adis16203_data);
168 if (ret)
169 goto error_free_dev;
171 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
172 if (ret)
173 goto error_free_dev;
175 /* Get the device into a sane initial state */
176 ret = adis_initial_startup(st);
177 if (ret)
178 goto error_cleanup_buffer_trigger;
180 ret = iio_device_register(indio_dev);
181 if (ret)
182 goto error_cleanup_buffer_trigger;
184 return 0;
186 error_cleanup_buffer_trigger:
187 adis_cleanup_buffer_and_trigger(st, indio_dev);
188 error_free_dev:
189 iio_device_free(indio_dev);
190 error_ret:
191 return ret;
194 static int adis16203_remove(struct spi_device *spi)
196 struct iio_dev *indio_dev = spi_get_drvdata(spi);
197 struct adis *st = iio_priv(indio_dev);
199 iio_device_unregister(indio_dev);
200 adis_cleanup_buffer_and_trigger(st, indio_dev);
201 iio_device_free(indio_dev);
203 return 0;
206 static struct spi_driver adis16203_driver = {
207 .driver = {
208 .name = "adis16203",
209 .owner = THIS_MODULE,
211 .probe = adis16203_probe,
212 .remove = adis16203_remove,
214 module_spi_driver(adis16203_driver);
216 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
217 MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
218 MODULE_LICENSE("GPL v2");
219 MODULE_ALIAS("spi:adis16203");