spi: Drop owner assignment from spi_drivers
[linux-2.6/btrfs-unstable.git] / drivers / staging / iio / accel / adis16209_core.c
blob8b42bf8c3f607d9216650a59d9eb530a17460150
1 /*
2 * ADIS16209 Programmable Digital Vibration Sensor driver
4 * Copyright 2010 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/list.h>
17 #include <linux/module.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/imu/adis.h>
24 #include "adis16209.h"
26 static const u8 adis16209_addresses[8][1] = {
27 [ADIS16209_SCAN_SUPPLY] = { },
28 [ADIS16209_SCAN_AUX_ADC] = { },
29 [ADIS16209_SCAN_ACC_X] = { ADIS16209_XACCL_NULL },
30 [ADIS16209_SCAN_ACC_Y] = { ADIS16209_YACCL_NULL },
31 [ADIS16209_SCAN_INCLI_X] = { ADIS16209_XINCL_NULL },
32 [ADIS16209_SCAN_INCLI_Y] = { ADIS16209_YINCL_NULL },
33 [ADIS16209_SCAN_ROT] = { },
34 [ADIS16209_SCAN_TEMP] = { },
37 static int adis16209_write_raw(struct iio_dev *indio_dev,
38 struct iio_chan_spec const *chan,
39 int val,
40 int val2,
41 long mask)
43 struct adis *st = iio_priv(indio_dev);
44 int bits;
45 s16 val16;
46 u8 addr;
48 switch (mask) {
49 case IIO_CHAN_INFO_CALIBBIAS:
50 switch (chan->type) {
51 case IIO_ACCEL:
52 case IIO_INCLI:
53 bits = 14;
54 break;
55 default:
56 return -EINVAL;
58 val16 = val & ((1 << bits) - 1);
59 addr = adis16209_addresses[chan->scan_index][0];
60 return adis_write_reg_16(st, addr, val16);
62 return -EINVAL;
65 static int adis16209_read_raw(struct iio_dev *indio_dev,
66 struct iio_chan_spec const *chan,
67 int *val, int *val2,
68 long mask)
70 struct adis *st = iio_priv(indio_dev);
71 int ret;
72 int bits;
73 u8 addr;
74 s16 val16;
76 switch (mask) {
77 case IIO_CHAN_INFO_RAW:
78 return adis_single_conversion(indio_dev, chan,
79 ADIS16209_ERROR_ACTIVE, val);
80 case IIO_CHAN_INFO_SCALE:
81 switch (chan->type) {
82 case IIO_VOLTAGE:
83 *val = 0;
84 if (chan->channel == 0)
85 *val2 = 305180; /* 0.30518 mV */
86 else
87 *val2 = 610500; /* 0.6105 mV */
88 return IIO_VAL_INT_PLUS_MICRO;
89 case IIO_TEMP:
90 *val = -470; /* -0.47 C */
91 *val2 = 0;
92 return IIO_VAL_INT_PLUS_MICRO;
93 case IIO_ACCEL:
94 *val = 0;
95 *val2 = IIO_G_TO_M_S_2(244140); /* 0.244140 mg */
96 return IIO_VAL_INT_PLUS_NANO;
97 case IIO_INCLI:
98 case IIO_ROT:
99 *val = 0;
100 *val2 = 25000; /* 0.025 degree */
101 return IIO_VAL_INT_PLUS_MICRO;
102 default:
103 return -EINVAL;
105 break;
106 case IIO_CHAN_INFO_OFFSET:
107 *val = 25000 / -470 - 0x4FE; /* 25 C = 0x4FE */
108 return IIO_VAL_INT;
109 case IIO_CHAN_INFO_CALIBBIAS:
110 switch (chan->type) {
111 case IIO_ACCEL:
112 bits = 14;
113 break;
114 default:
115 return -EINVAL;
117 mutex_lock(&indio_dev->mlock);
118 addr = adis16209_addresses[chan->scan_index][0];
119 ret = adis_read_reg_16(st, addr, &val16);
120 if (ret) {
121 mutex_unlock(&indio_dev->mlock);
122 return ret;
124 val16 &= (1 << bits) - 1;
125 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
126 *val = val16;
127 mutex_unlock(&indio_dev->mlock);
128 return IIO_VAL_INT;
130 return -EINVAL;
133 static const struct iio_chan_spec adis16209_channels[] = {
134 ADIS_SUPPLY_CHAN(ADIS16209_SUPPLY_OUT, ADIS16209_SCAN_SUPPLY, 0, 14),
135 ADIS_TEMP_CHAN(ADIS16209_TEMP_OUT, ADIS16209_SCAN_TEMP, 0, 12),
136 ADIS_ACCEL_CHAN(X, ADIS16209_XACCL_OUT, ADIS16209_SCAN_ACC_X,
137 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
138 ADIS_ACCEL_CHAN(Y, ADIS16209_YACCL_OUT, ADIS16209_SCAN_ACC_Y,
139 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
140 ADIS_AUX_ADC_CHAN(ADIS16209_AUX_ADC, ADIS16209_SCAN_AUX_ADC, 0, 12),
141 ADIS_INCLI_CHAN(X, ADIS16209_XINCL_OUT, ADIS16209_SCAN_INCLI_X,
142 0, 0, 14),
143 ADIS_INCLI_CHAN(Y, ADIS16209_YINCL_OUT, ADIS16209_SCAN_INCLI_Y,
144 0, 0, 14),
145 ADIS_ROT_CHAN(X, ADIS16209_ROT_OUT, ADIS16209_SCAN_ROT, 0, 0, 14),
146 IIO_CHAN_SOFT_TIMESTAMP(8)
149 static const struct iio_info adis16209_info = {
150 .read_raw = &adis16209_read_raw,
151 .write_raw = &adis16209_write_raw,
152 .update_scan_mode = adis_update_scan_mode,
153 .driver_module = THIS_MODULE,
156 static const char * const adis16209_status_error_msgs[] = {
157 [ADIS16209_DIAG_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
158 [ADIS16209_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
159 [ADIS16209_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
160 [ADIS16209_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
161 [ADIS16209_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
164 static const struct adis_data adis16209_data = {
165 .read_delay = 30,
166 .msc_ctrl_reg = ADIS16209_MSC_CTRL,
167 .glob_cmd_reg = ADIS16209_GLOB_CMD,
168 .diag_stat_reg = ADIS16209_DIAG_STAT,
170 .self_test_mask = ADIS16209_MSC_CTRL_SELF_TEST_EN,
171 .startup_delay = ADIS16209_STARTUP_DELAY,
173 .status_error_msgs = adis16209_status_error_msgs,
174 .status_error_mask = BIT(ADIS16209_DIAG_STAT_SELFTEST_FAIL_BIT) |
175 BIT(ADIS16209_DIAG_STAT_SPI_FAIL_BIT) |
176 BIT(ADIS16209_DIAG_STAT_FLASH_UPT_BIT) |
177 BIT(ADIS16209_DIAG_STAT_POWER_HIGH_BIT) |
178 BIT(ADIS16209_DIAG_STAT_POWER_LOW_BIT),
181 static int adis16209_probe(struct spi_device *spi)
183 int ret;
184 struct adis *st;
185 struct iio_dev *indio_dev;
187 /* setup the industrialio driver allocated elements */
188 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
189 if (!indio_dev)
190 return -ENOMEM;
191 st = iio_priv(indio_dev);
192 /* this is only used for removal purposes */
193 spi_set_drvdata(spi, indio_dev);
195 indio_dev->name = spi->dev.driver->name;
196 indio_dev->dev.parent = &spi->dev;
197 indio_dev->info = &adis16209_info;
198 indio_dev->channels = adis16209_channels;
199 indio_dev->num_channels = ARRAY_SIZE(adis16209_channels);
200 indio_dev->modes = INDIO_DIRECT_MODE;
202 ret = adis_init(st, indio_dev, spi, &adis16209_data);
203 if (ret)
204 return ret;
205 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
206 if (ret)
207 return ret;
209 /* Get the device into a sane initial state */
210 ret = adis_initial_startup(st);
211 if (ret)
212 goto error_cleanup_buffer_trigger;
213 ret = iio_device_register(indio_dev);
214 if (ret)
215 goto error_cleanup_buffer_trigger;
217 return 0;
219 error_cleanup_buffer_trigger:
220 adis_cleanup_buffer_and_trigger(st, indio_dev);
221 return ret;
224 static int adis16209_remove(struct spi_device *spi)
226 struct iio_dev *indio_dev = spi_get_drvdata(spi);
227 struct adis *st = iio_priv(indio_dev);
229 iio_device_unregister(indio_dev);
230 adis_cleanup_buffer_and_trigger(st, indio_dev);
232 return 0;
235 static struct spi_driver adis16209_driver = {
236 .driver = {
237 .name = "adis16209",
239 .probe = adis16209_probe,
240 .remove = adis16209_remove,
242 module_spi_driver(adis16209_driver);
244 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
245 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
246 MODULE_LICENSE("GPL v2");
247 MODULE_ALIAS("spi:adis16209");