staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / iio / accel / adis16209.c
blobf2dc3a5f046361ab016bd8d581a431a470f522c5
1 /*
2 * ADIS16209 Dual-Axis Digital Inclinometer and Accelerometer
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/imu/adis.h>
20 #define ADIS16209_STARTUP_DELAY_MS 220
21 #define ADIS16209_FLASH_CNT_REG 0x00
23 /* Data Output Register Definitions */
24 #define ADIS16209_SUPPLY_OUT_REG 0x02
25 #define ADIS16209_XACCL_OUT_REG 0x04
26 #define ADIS16209_YACCL_OUT_REG 0x06
27 /* Output, auxiliary ADC input */
28 #define ADIS16209_AUX_ADC_REG 0x08
29 /* Output, temperature */
30 #define ADIS16209_TEMP_OUT_REG 0x0A
31 /* Output, +/- 90 degrees X-axis inclination */
32 #define ADIS16209_XINCL_OUT_REG 0x0C
33 #define ADIS16209_YINCL_OUT_REG 0x0E
34 /* Output, +/-180 vertical rotational position */
35 #define ADIS16209_ROT_OUT_REG 0x10
38 * Calibration Register Definitions.
39 * Acceleration, inclination or rotation offset null.
41 #define ADIS16209_XACCL_NULL_REG 0x12
42 #define ADIS16209_YACCL_NULL_REG 0x14
43 #define ADIS16209_XINCL_NULL_REG 0x16
44 #define ADIS16209_YINCL_NULL_REG 0x18
45 #define ADIS16209_ROT_NULL_REG 0x1A
47 /* Alarm Register Definitions */
48 #define ADIS16209_ALM_MAG1_REG 0x20
49 #define ADIS16209_ALM_MAG2_REG 0x22
50 #define ADIS16209_ALM_SMPL1_REG 0x24
51 #define ADIS16209_ALM_SMPL2_REG 0x26
52 #define ADIS16209_ALM_CTRL_REG 0x28
54 #define ADIS16209_AUX_DAC_REG 0x30
55 #define ADIS16209_GPIO_CTRL_REG 0x32
56 #define ADIS16209_SMPL_PRD_REG 0x36
57 #define ADIS16209_AVG_CNT_REG 0x38
58 #define ADIS16209_SLP_CNT_REG 0x3A
60 #define ADIS16209_MSC_CTRL_REG 0x34
61 #define ADIS16209_MSC_CTRL_PWRUP_SELF_TEST BIT(10)
62 #define ADIS16209_MSC_CTRL_SELF_TEST_EN BIT(8)
63 #define ADIS16209_MSC_CTRL_DATA_RDY_EN BIT(2)
64 /* Data-ready polarity: 1 = active high, 0 = active low */
65 #define ADIS16209_MSC_CTRL_ACTIVE_HIGH BIT(1)
66 #define ADIS16209_MSC_CTRL_DATA_RDY_DIO2 BIT(0)
68 #define ADIS16209_STAT_REG 0x3C
69 #define ADIS16209_STAT_ALARM2 BIT(9)
70 #define ADIS16209_STAT_ALARM1 BIT(8)
71 #define ADIS16209_STAT_SELFTEST_FAIL_BIT 5
72 #define ADIS16209_STAT_SPI_FAIL_BIT 3
73 #define ADIS16209_STAT_FLASH_UPT_FAIL_BIT 2
74 /* Power supply above 3.625 V */
75 #define ADIS16209_STAT_POWER_HIGH_BIT 1
76 /* Power supply below 3.15 V */
77 #define ADIS16209_STAT_POWER_LOW_BIT 0
79 #define ADIS16209_CMD_REG 0x3E
80 #define ADIS16209_CMD_SW_RESET BIT(7)
81 #define ADIS16209_CMD_CLEAR_STAT BIT(4)
82 #define ADIS16209_CMD_FACTORY_CAL BIT(1)
84 #define ADIS16209_ERROR_ACTIVE BIT(14)
86 enum adis16209_scan {
87 ADIS16209_SCAN_SUPPLY,
88 ADIS16209_SCAN_ACC_X,
89 ADIS16209_SCAN_ACC_Y,
90 ADIS16209_SCAN_AUX_ADC,
91 ADIS16209_SCAN_TEMP,
92 ADIS16209_SCAN_INCLI_X,
93 ADIS16209_SCAN_INCLI_Y,
94 ADIS16209_SCAN_ROT,
97 static const u8 adis16209_addresses[8][1] = {
98 [ADIS16209_SCAN_SUPPLY] = { },
99 [ADIS16209_SCAN_AUX_ADC] = { },
100 [ADIS16209_SCAN_ACC_X] = { ADIS16209_XACCL_NULL_REG },
101 [ADIS16209_SCAN_ACC_Y] = { ADIS16209_YACCL_NULL_REG },
102 [ADIS16209_SCAN_INCLI_X] = { ADIS16209_XINCL_NULL_REG },
103 [ADIS16209_SCAN_INCLI_Y] = { ADIS16209_YINCL_NULL_REG },
104 [ADIS16209_SCAN_ROT] = { },
105 [ADIS16209_SCAN_TEMP] = { },
108 static int adis16209_write_raw(struct iio_dev *indio_dev,
109 struct iio_chan_spec const *chan,
110 int val,
111 int val2,
112 long mask)
114 struct adis *st = iio_priv(indio_dev);
115 int m;
117 if (mask != IIO_CHAN_INFO_CALIBBIAS)
118 return -EINVAL;
120 switch (chan->type) {
121 case IIO_ACCEL:
122 case IIO_INCLI:
123 m = GENMASK(13, 0);
124 break;
125 default:
126 return -EINVAL;
129 return adis_write_reg_16(st, adis16209_addresses[chan->scan_index][0],
130 val & m);
133 static int adis16209_read_raw(struct iio_dev *indio_dev,
134 struct iio_chan_spec const *chan,
135 int *val, int *val2,
136 long mask)
138 struct adis *st = iio_priv(indio_dev);
139 int ret;
140 int bits;
141 u8 addr;
142 s16 val16;
144 switch (mask) {
145 case IIO_CHAN_INFO_RAW:
146 return adis_single_conversion(indio_dev, chan,
147 ADIS16209_ERROR_ACTIVE, val);
148 case IIO_CHAN_INFO_SCALE:
149 switch (chan->type) {
150 case IIO_VOLTAGE:
151 *val = 0;
152 switch (chan->channel) {
153 case 0:
154 *val2 = 305180; /* 0.30518 mV */
155 break;
156 case 1:
157 *val2 = 610500; /* 0.6105 mV */
158 break;
159 default:
160 return -EINVAL;
162 return IIO_VAL_INT_PLUS_MICRO;
163 case IIO_TEMP:
164 *val = -470;
165 *val2 = 0;
166 return IIO_VAL_INT_PLUS_MICRO;
167 case IIO_ACCEL:
169 * IIO base unit for sensitivity of accelerometer
170 * is milli g.
171 * 1 LSB represents 0.244 mg.
173 *val = 0;
174 *val2 = IIO_G_TO_M_S_2(244140);
175 return IIO_VAL_INT_PLUS_NANO;
176 case IIO_INCLI:
177 case IIO_ROT:
179 * IIO base units for rotation are degrees.
180 * 1 LSB represents 0.025 milli degrees.
182 *val = 0;
183 *val2 = 25000;
184 return IIO_VAL_INT_PLUS_MICRO;
185 default:
186 return -EINVAL;
188 break;
189 case IIO_CHAN_INFO_OFFSET:
191 * The raw ADC value is 0x4FE when the temperature
192 * is 45 degrees and the scale factor per milli
193 * degree celcius is -470.
195 *val = 25000 / -470 - 0x4FE;
196 return IIO_VAL_INT;
197 case IIO_CHAN_INFO_CALIBBIAS:
198 switch (chan->type) {
199 case IIO_ACCEL:
200 bits = 14;
201 break;
202 default:
203 return -EINVAL;
205 addr = adis16209_addresses[chan->scan_index][0];
206 ret = adis_read_reg_16(st, addr, &val16);
207 if (ret)
208 return ret;
210 *val = sign_extend32(val16, bits - 1);
211 return IIO_VAL_INT;
213 return -EINVAL;
216 static const struct iio_chan_spec adis16209_channels[] = {
217 ADIS_SUPPLY_CHAN(ADIS16209_SUPPLY_OUT_REG, ADIS16209_SCAN_SUPPLY,
218 0, 14),
219 ADIS_TEMP_CHAN(ADIS16209_TEMP_OUT_REG, ADIS16209_SCAN_TEMP, 0, 12),
220 ADIS_ACCEL_CHAN(X, ADIS16209_XACCL_OUT_REG, ADIS16209_SCAN_ACC_X,
221 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
222 ADIS_ACCEL_CHAN(Y, ADIS16209_YACCL_OUT_REG, ADIS16209_SCAN_ACC_Y,
223 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
224 ADIS_AUX_ADC_CHAN(ADIS16209_AUX_ADC_REG, ADIS16209_SCAN_AUX_ADC, 0, 12),
225 ADIS_INCLI_CHAN(X, ADIS16209_XINCL_OUT_REG, ADIS16209_SCAN_INCLI_X,
226 0, 0, 14),
227 ADIS_INCLI_CHAN(Y, ADIS16209_YINCL_OUT_REG, ADIS16209_SCAN_INCLI_Y,
228 0, 0, 14),
229 ADIS_ROT_CHAN(X, ADIS16209_ROT_OUT_REG, ADIS16209_SCAN_ROT, 0, 0, 14),
230 IIO_CHAN_SOFT_TIMESTAMP(8)
233 static const struct iio_info adis16209_info = {
234 .read_raw = adis16209_read_raw,
235 .write_raw = adis16209_write_raw,
236 .update_scan_mode = adis_update_scan_mode,
239 static const char * const adis16209_status_error_msgs[] = {
240 [ADIS16209_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
241 [ADIS16209_STAT_SPI_FAIL_BIT] = "SPI failure",
242 [ADIS16209_STAT_FLASH_UPT_FAIL_BIT] = "Flash update failed",
243 [ADIS16209_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
244 [ADIS16209_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
247 static const struct adis_data adis16209_data = {
248 .read_delay = 30,
249 .msc_ctrl_reg = ADIS16209_MSC_CTRL_REG,
250 .glob_cmd_reg = ADIS16209_CMD_REG,
251 .diag_stat_reg = ADIS16209_STAT_REG,
253 .self_test_mask = ADIS16209_MSC_CTRL_SELF_TEST_EN,
254 .self_test_no_autoclear = true,
255 .startup_delay = ADIS16209_STARTUP_DELAY_MS,
257 .status_error_msgs = adis16209_status_error_msgs,
258 .status_error_mask = BIT(ADIS16209_STAT_SELFTEST_FAIL_BIT) |
259 BIT(ADIS16209_STAT_SPI_FAIL_BIT) |
260 BIT(ADIS16209_STAT_FLASH_UPT_FAIL_BIT) |
261 BIT(ADIS16209_STAT_POWER_HIGH_BIT) |
262 BIT(ADIS16209_STAT_POWER_LOW_BIT),
265 static int adis16209_probe(struct spi_device *spi)
267 struct iio_dev *indio_dev;
268 struct adis *st;
269 int ret;
271 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
272 if (!indio_dev)
273 return -ENOMEM;
275 st = iio_priv(indio_dev);
276 spi_set_drvdata(spi, indio_dev);
278 indio_dev->name = spi->dev.driver->name;
279 indio_dev->dev.parent = &spi->dev;
280 indio_dev->info = &adis16209_info;
281 indio_dev->channels = adis16209_channels;
282 indio_dev->num_channels = ARRAY_SIZE(adis16209_channels);
283 indio_dev->modes = INDIO_DIRECT_MODE;
285 ret = adis_init(st, indio_dev, spi, &adis16209_data);
286 if (ret)
287 return ret;
289 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
290 if (ret)
291 return ret;
293 ret = adis_initial_startup(st);
294 if (ret)
295 goto error_cleanup_buffer_trigger;
296 ret = iio_device_register(indio_dev);
297 if (ret)
298 goto error_cleanup_buffer_trigger;
300 return 0;
302 error_cleanup_buffer_trigger:
303 adis_cleanup_buffer_and_trigger(st, indio_dev);
304 return ret;
307 static int adis16209_remove(struct spi_device *spi)
309 struct iio_dev *indio_dev = spi_get_drvdata(spi);
310 struct adis *st = iio_priv(indio_dev);
312 iio_device_unregister(indio_dev);
313 adis_cleanup_buffer_and_trigger(st, indio_dev);
315 return 0;
318 static struct spi_driver adis16209_driver = {
319 .driver = {
320 .name = "adis16209",
322 .probe = adis16209_probe,
323 .remove = adis16209_remove,
325 module_spi_driver(adis16209_driver);
327 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
328 MODULE_DESCRIPTION("Analog Devices ADIS16209 Dual-Axis Digital Inclinometer and Accelerometer");
329 MODULE_LICENSE("GPL v2");
330 MODULE_ALIAS("spi:adis16209");