180503b23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2555d5a9fSLars-Peter Clausen /*
3555d5a9fSLars-Peter Clausen * ADIS16260/ADIS16265 Programmable Digital Gyroscope Sensor Driver
4555d5a9fSLars-Peter Clausen *
5555d5a9fSLars-Peter Clausen * Copyright 2010 Analog Devices Inc.
6555d5a9fSLars-Peter Clausen */
7555d5a9fSLars-Peter Clausen
8555d5a9fSLars-Peter Clausen #include <linux/device.h>
9555d5a9fSLars-Peter Clausen #include <linux/kernel.h>
10555d5a9fSLars-Peter Clausen #include <linux/spi/spi.h>
11555d5a9fSLars-Peter Clausen #include <linux/module.h>
12555d5a9fSLars-Peter Clausen
13555d5a9fSLars-Peter Clausen #include <linux/iio/iio.h>
14555d5a9fSLars-Peter Clausen #include <linux/iio/imu/adis.h>
15555d5a9fSLars-Peter Clausen
16555d5a9fSLars-Peter Clausen #define ADIS16260_STARTUP_DELAY 220 /* ms */
17555d5a9fSLars-Peter Clausen
18555d5a9fSLars-Peter Clausen #define ADIS16260_FLASH_CNT 0x00 /* Flash memory write count */
19555d5a9fSLars-Peter Clausen #define ADIS16260_SUPPLY_OUT 0x02 /* Power supply measurement */
20555d5a9fSLars-Peter Clausen #define ADIS16260_GYRO_OUT 0x04 /* X-axis gyroscope output */
21555d5a9fSLars-Peter Clausen #define ADIS16260_AUX_ADC 0x0A /* analog input channel measurement */
22555d5a9fSLars-Peter Clausen #define ADIS16260_TEMP_OUT 0x0C /* internal temperature measurement */
23555d5a9fSLars-Peter Clausen #define ADIS16260_ANGL_OUT 0x0E /* angle displacement */
24555d5a9fSLars-Peter Clausen #define ADIS16260_GYRO_OFF 0x14 /* Calibration, offset/bias adjustment */
25555d5a9fSLars-Peter Clausen #define ADIS16260_GYRO_SCALE 0x16 /* Calibration, scale adjustment */
26555d5a9fSLars-Peter Clausen #define ADIS16260_ALM_MAG1 0x20 /* Alarm 1 magnitude/polarity setting */
27555d5a9fSLars-Peter Clausen #define ADIS16260_ALM_MAG2 0x22 /* Alarm 2 magnitude/polarity setting */
28555d5a9fSLars-Peter Clausen #define ADIS16260_ALM_SMPL1 0x24 /* Alarm 1 dynamic rate of change setting */
29555d5a9fSLars-Peter Clausen #define ADIS16260_ALM_SMPL2 0x26 /* Alarm 2 dynamic rate of change setting */
30555d5a9fSLars-Peter Clausen #define ADIS16260_ALM_CTRL 0x28 /* Alarm control */
31555d5a9fSLars-Peter Clausen #define ADIS16260_AUX_DAC 0x30 /* Auxiliary DAC data */
32555d5a9fSLars-Peter Clausen #define ADIS16260_GPIO_CTRL 0x32 /* Control, digital I/O line */
33555d5a9fSLars-Peter Clausen #define ADIS16260_MSC_CTRL 0x34 /* Control, data ready, self-test settings */
34555d5a9fSLars-Peter Clausen #define ADIS16260_SMPL_PRD 0x36 /* Control, internal sample rate */
35555d5a9fSLars-Peter Clausen #define ADIS16260_SENS_AVG 0x38 /* Control, dynamic range, filtering */
36555d5a9fSLars-Peter Clausen #define ADIS16260_SLP_CNT 0x3A /* Control, sleep mode initiation */
37555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT 0x3C /* Diagnostic, error flags */
38555d5a9fSLars-Peter Clausen #define ADIS16260_GLOB_CMD 0x3E /* Control, global commands */
39555d5a9fSLars-Peter Clausen #define ADIS16260_LOT_ID1 0x52 /* Lot Identification Code 1 */
40555d5a9fSLars-Peter Clausen #define ADIS16260_LOT_ID2 0x54 /* Lot Identification Code 2 */
41555d5a9fSLars-Peter Clausen #define ADIS16260_PROD_ID 0x56 /* Product identifier;
42555d5a9fSLars-Peter Clausen * convert to decimal = 16,265/16,260 */
43555d5a9fSLars-Peter Clausen #define ADIS16260_SERIAL_NUM 0x58 /* Serial number */
44555d5a9fSLars-Peter Clausen
45555d5a9fSLars-Peter Clausen #define ADIS16260_ERROR_ACTIVE (1<<14)
46555d5a9fSLars-Peter Clausen #define ADIS16260_NEW_DATA (1<<15)
47555d5a9fSLars-Peter Clausen
48555d5a9fSLars-Peter Clausen /* MSC_CTRL */
49555d5a9fSLars-Peter Clausen #define ADIS16260_MSC_CTRL_MEM_TEST (1<<11)
50555d5a9fSLars-Peter Clausen /* Internal self-test enable */
51555d5a9fSLars-Peter Clausen #define ADIS16260_MSC_CTRL_INT_SELF_TEST (1<<10)
52555d5a9fSLars-Peter Clausen #define ADIS16260_MSC_CTRL_NEG_SELF_TEST (1<<9)
53555d5a9fSLars-Peter Clausen #define ADIS16260_MSC_CTRL_POS_SELF_TEST (1<<8)
54555d5a9fSLars-Peter Clausen #define ADIS16260_MSC_CTRL_DATA_RDY_EN (1<<2)
55555d5a9fSLars-Peter Clausen #define ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH (1<<1)
56555d5a9fSLars-Peter Clausen #define ADIS16260_MSC_CTRL_DATA_RDY_DIO2 (1<<0)
57555d5a9fSLars-Peter Clausen
58555d5a9fSLars-Peter Clausen /* SMPL_PRD */
59555d5a9fSLars-Peter Clausen /* Time base (tB): 0 = 1.953 ms, 1 = 60.54 ms */
60555d5a9fSLars-Peter Clausen #define ADIS16260_SMPL_PRD_TIME_BASE (1<<7)
61555d5a9fSLars-Peter Clausen #define ADIS16260_SMPL_PRD_DIV_MASK 0x7F
62555d5a9fSLars-Peter Clausen
63555d5a9fSLars-Peter Clausen /* SLP_CNT */
64555d5a9fSLars-Peter Clausen #define ADIS16260_SLP_CNT_POWER_OFF 0x80
65555d5a9fSLars-Peter Clausen
66555d5a9fSLars-Peter Clausen /* DIAG_STAT */
67555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_ALARM2 (1<<9)
68555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_ALARM1 (1<<8)
69555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_FLASH_CHK_BIT 6
70555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_SELF_TEST_BIT 5
71555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_OVERFLOW_BIT 4
72555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_SPI_FAIL_BIT 3
73555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_FLASH_UPT_BIT 2
74555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_POWER_HIGH_BIT 1
75555d5a9fSLars-Peter Clausen #define ADIS16260_DIAG_STAT_POWER_LOW_BIT 0
76555d5a9fSLars-Peter Clausen
77555d5a9fSLars-Peter Clausen /* GLOB_CMD */
78555d5a9fSLars-Peter Clausen #define ADIS16260_GLOB_CMD_SW_RESET (1<<7)
79555d5a9fSLars-Peter Clausen #define ADIS16260_GLOB_CMD_FLASH_UPD (1<<3)
80555d5a9fSLars-Peter Clausen #define ADIS16260_GLOB_CMD_DAC_LATCH (1<<2)
81555d5a9fSLars-Peter Clausen #define ADIS16260_GLOB_CMD_FAC_CALIB (1<<1)
82555d5a9fSLars-Peter Clausen #define ADIS16260_GLOB_CMD_AUTO_NULL (1<<0)
83555d5a9fSLars-Peter Clausen
84555d5a9fSLars-Peter Clausen #define ADIS16260_SPI_SLOW (u32)(300 * 1000)
85555d5a9fSLars-Peter Clausen #define ADIS16260_SPI_BURST (u32)(1000 * 1000)
86555d5a9fSLars-Peter Clausen #define ADIS16260_SPI_FAST (u32)(2000 * 1000)
87555d5a9fSLars-Peter Clausen
88555d5a9fSLars-Peter Clausen /* At the moment triggers are only used for ring buffer
89555d5a9fSLars-Peter Clausen * filling. This may change!
90555d5a9fSLars-Peter Clausen */
91555d5a9fSLars-Peter Clausen
92555d5a9fSLars-Peter Clausen #define ADIS16260_SCAN_GYRO 0
93555d5a9fSLars-Peter Clausen #define ADIS16260_SCAN_SUPPLY 1
94555d5a9fSLars-Peter Clausen #define ADIS16260_SCAN_AUX_ADC 2
95555d5a9fSLars-Peter Clausen #define ADIS16260_SCAN_TEMP 3
96555d5a9fSLars-Peter Clausen #define ADIS16260_SCAN_ANGL 4
97555d5a9fSLars-Peter Clausen
98f1afda12SLars-Peter Clausen struct adis16260_chip_info {
99f1afda12SLars-Peter Clausen unsigned int gyro_max_val;
100f1afda12SLars-Peter Clausen unsigned int gyro_max_scale;
101f1afda12SLars-Peter Clausen const struct iio_chan_spec *channels;
102f1afda12SLars-Peter Clausen unsigned int num_channels;
103f1afda12SLars-Peter Clausen };
104555d5a9fSLars-Peter Clausen
105f1afda12SLars-Peter Clausen struct adis16260 {
106f1afda12SLars-Peter Clausen const struct adis16260_chip_info *info;
107555d5a9fSLars-Peter Clausen
108f1afda12SLars-Peter Clausen struct adis adis;
109f1afda12SLars-Peter Clausen };
110f1afda12SLars-Peter Clausen
111f1afda12SLars-Peter Clausen enum adis16260_type {
112f1afda12SLars-Peter Clausen ADIS16251,
113f1afda12SLars-Peter Clausen ADIS16260,
114f1afda12SLars-Peter Clausen ADIS16266,
115f1afda12SLars-Peter Clausen };
116555d5a9fSLars-Peter Clausen
117555d5a9fSLars-Peter Clausen static const struct iio_chan_spec adis16260_channels[] = {
118555d5a9fSLars-Peter Clausen ADIS_GYRO_CHAN(X, ADIS16260_GYRO_OUT, ADIS16260_SCAN_GYRO,
119555d5a9fSLars-Peter Clausen BIT(IIO_CHAN_INFO_CALIBBIAS) |
12082695ef5SJonathan Cameron BIT(IIO_CHAN_INFO_CALIBSCALE),
12182695ef5SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ), 14),
12282695ef5SJonathan Cameron ADIS_INCLI_CHAN(X, ADIS16260_ANGL_OUT, ADIS16260_SCAN_ANGL, 0,
12382695ef5SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ), 14),
12482695ef5SJonathan Cameron ADIS_TEMP_CHAN(ADIS16260_TEMP_OUT, ADIS16260_SCAN_TEMP,
12582695ef5SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
12682695ef5SJonathan Cameron ADIS_SUPPLY_CHAN(ADIS16260_SUPPLY_OUT, ADIS16260_SCAN_SUPPLY,
12782695ef5SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
12882695ef5SJonathan Cameron ADIS_AUX_ADC_CHAN(ADIS16260_AUX_ADC, ADIS16260_SCAN_AUX_ADC,
12982695ef5SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
130555d5a9fSLars-Peter Clausen IIO_CHAN_SOFT_TIMESTAMP(5),
131555d5a9fSLars-Peter Clausen };
132555d5a9fSLars-Peter Clausen
133f1afda12SLars-Peter Clausen static const struct iio_chan_spec adis16266_channels[] = {
134f1afda12SLars-Peter Clausen ADIS_GYRO_CHAN(X, ADIS16260_GYRO_OUT, ADIS16260_SCAN_GYRO,
135f1afda12SLars-Peter Clausen BIT(IIO_CHAN_INFO_CALIBBIAS) |
136f1afda12SLars-Peter Clausen BIT(IIO_CHAN_INFO_CALIBSCALE),
137f1afda12SLars-Peter Clausen BIT(IIO_CHAN_INFO_SAMP_FREQ), 14),
138f1afda12SLars-Peter Clausen ADIS_TEMP_CHAN(ADIS16260_TEMP_OUT, ADIS16260_SCAN_TEMP,
139f1afda12SLars-Peter Clausen BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
140f1afda12SLars-Peter Clausen ADIS_SUPPLY_CHAN(ADIS16260_SUPPLY_OUT, ADIS16260_SCAN_SUPPLY,
141f1afda12SLars-Peter Clausen BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
142f1afda12SLars-Peter Clausen ADIS_AUX_ADC_CHAN(ADIS16260_AUX_ADC, ADIS16260_SCAN_AUX_ADC,
143f1afda12SLars-Peter Clausen BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
144f1afda12SLars-Peter Clausen IIO_CHAN_SOFT_TIMESTAMP(4),
145f1afda12SLars-Peter Clausen };
146f1afda12SLars-Peter Clausen
147f1afda12SLars-Peter Clausen static const struct adis16260_chip_info adis16260_chip_info_table[] = {
148f1afda12SLars-Peter Clausen [ADIS16251] = {
149f1afda12SLars-Peter Clausen .gyro_max_scale = 80,
150f1afda12SLars-Peter Clausen .gyro_max_val = IIO_RAD_TO_DEGREE(4368),
151f1afda12SLars-Peter Clausen .channels = adis16260_channels,
152f1afda12SLars-Peter Clausen .num_channels = ARRAY_SIZE(adis16260_channels),
153f1afda12SLars-Peter Clausen },
154f1afda12SLars-Peter Clausen [ADIS16260] = {
155f1afda12SLars-Peter Clausen .gyro_max_scale = 320,
156f1afda12SLars-Peter Clausen .gyro_max_val = IIO_RAD_TO_DEGREE(4368),
157f1afda12SLars-Peter Clausen .channels = adis16260_channels,
158f1afda12SLars-Peter Clausen .num_channels = ARRAY_SIZE(adis16260_channels),
159f1afda12SLars-Peter Clausen },
160f1afda12SLars-Peter Clausen [ADIS16266] = {
161f1afda12SLars-Peter Clausen .gyro_max_scale = 14000,
162f1afda12SLars-Peter Clausen .gyro_max_val = IIO_RAD_TO_DEGREE(3357),
163f1afda12SLars-Peter Clausen .channels = adis16266_channels,
164f1afda12SLars-Peter Clausen .num_channels = ARRAY_SIZE(adis16266_channels),
165f1afda12SLars-Peter Clausen },
166f1afda12SLars-Peter Clausen };
167f1afda12SLars-Peter Clausen
168f1afda12SLars-Peter Clausen /* Power down the device */
adis16260_stop_device(struct iio_dev * indio_dev)169f1afda12SLars-Peter Clausen static int adis16260_stop_device(struct iio_dev *indio_dev)
170f1afda12SLars-Peter Clausen {
171f1afda12SLars-Peter Clausen struct adis16260 *adis16260 = iio_priv(indio_dev);
172f1afda12SLars-Peter Clausen int ret;
173f1afda12SLars-Peter Clausen u16 val = ADIS16260_SLP_CNT_POWER_OFF;
174f1afda12SLars-Peter Clausen
175f1afda12SLars-Peter Clausen ret = adis_write_reg_16(&adis16260->adis, ADIS16260_SLP_CNT, val);
176f1afda12SLars-Peter Clausen if (ret)
177f1afda12SLars-Peter Clausen dev_err(&indio_dev->dev, "problem with turning device off: SLP_CNT");
178f1afda12SLars-Peter Clausen
179f1afda12SLars-Peter Clausen return ret;
180f1afda12SLars-Peter Clausen }
181f1afda12SLars-Peter Clausen
182555d5a9fSLars-Peter Clausen static const u8 adis16260_addresses[][2] = {
183555d5a9fSLars-Peter Clausen [ADIS16260_SCAN_GYRO] = { ADIS16260_GYRO_OFF, ADIS16260_GYRO_SCALE },
184555d5a9fSLars-Peter Clausen };
185555d5a9fSLars-Peter Clausen
adis16260_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)186555d5a9fSLars-Peter Clausen static int adis16260_read_raw(struct iio_dev *indio_dev,
187555d5a9fSLars-Peter Clausen struct iio_chan_spec const *chan,
188555d5a9fSLars-Peter Clausen int *val, int *val2,
189555d5a9fSLars-Peter Clausen long mask)
190555d5a9fSLars-Peter Clausen {
191f1afda12SLars-Peter Clausen struct adis16260 *adis16260 = iio_priv(indio_dev);
192f1afda12SLars-Peter Clausen const struct adis16260_chip_info *info = adis16260->info;
193f1afda12SLars-Peter Clausen struct adis *adis = &adis16260->adis;
194555d5a9fSLars-Peter Clausen int ret;
195555d5a9fSLars-Peter Clausen u8 addr;
196555d5a9fSLars-Peter Clausen s16 val16;
197555d5a9fSLars-Peter Clausen
198555d5a9fSLars-Peter Clausen switch (mask) {
199555d5a9fSLars-Peter Clausen case IIO_CHAN_INFO_RAW:
200555d5a9fSLars-Peter Clausen return adis_single_conversion(indio_dev, chan,
201555d5a9fSLars-Peter Clausen ADIS16260_ERROR_ACTIVE, val);
202555d5a9fSLars-Peter Clausen case IIO_CHAN_INFO_SCALE:
203555d5a9fSLars-Peter Clausen switch (chan->type) {
204555d5a9fSLars-Peter Clausen case IIO_ANGL_VEL:
205f1afda12SLars-Peter Clausen *val = info->gyro_max_scale;
206f1afda12SLars-Peter Clausen *val2 = info->gyro_max_val;
207f1afda12SLars-Peter Clausen return IIO_VAL_FRACTIONAL;
208555d5a9fSLars-Peter Clausen case IIO_INCLI:
209555d5a9fSLars-Peter Clausen *val = 0;
210555d5a9fSLars-Peter Clausen *val2 = IIO_DEGREE_TO_RAD(36630);
211555d5a9fSLars-Peter Clausen return IIO_VAL_INT_PLUS_MICRO;
212555d5a9fSLars-Peter Clausen case IIO_VOLTAGE:
213555d5a9fSLars-Peter Clausen if (chan->channel == 0) {
214555d5a9fSLars-Peter Clausen *val = 1;
215555d5a9fSLars-Peter Clausen *val2 = 831500; /* 1.8315 mV */
216555d5a9fSLars-Peter Clausen } else {
217555d5a9fSLars-Peter Clausen *val = 0;
218555d5a9fSLars-Peter Clausen *val2 = 610500; /* 610.5 uV */
219555d5a9fSLars-Peter Clausen }
220555d5a9fSLars-Peter Clausen return IIO_VAL_INT_PLUS_MICRO;
221555d5a9fSLars-Peter Clausen case IIO_TEMP:
222555d5a9fSLars-Peter Clausen *val = 145;
223555d5a9fSLars-Peter Clausen *val2 = 300000; /* 0.1453 C */
224555d5a9fSLars-Peter Clausen return IIO_VAL_INT_PLUS_MICRO;
225555d5a9fSLars-Peter Clausen default:
226555d5a9fSLars-Peter Clausen return -EINVAL;
227555d5a9fSLars-Peter Clausen }
228555d5a9fSLars-Peter Clausen case IIO_CHAN_INFO_OFFSET:
229555d5a9fSLars-Peter Clausen *val = 250000 / 1453; /* 25 C = 0x00 */
230555d5a9fSLars-Peter Clausen return IIO_VAL_INT;
231555d5a9fSLars-Peter Clausen case IIO_CHAN_INFO_CALIBBIAS:
232555d5a9fSLars-Peter Clausen addr = adis16260_addresses[chan->scan_index][0];
233555d5a9fSLars-Peter Clausen ret = adis_read_reg_16(adis, addr, &val16);
234555d5a9fSLars-Peter Clausen if (ret)
235555d5a9fSLars-Peter Clausen return ret;
236555d5a9fSLars-Peter Clausen
237555d5a9fSLars-Peter Clausen *val = sign_extend32(val16, 11);
238555d5a9fSLars-Peter Clausen return IIO_VAL_INT;
239555d5a9fSLars-Peter Clausen case IIO_CHAN_INFO_CALIBSCALE:
240555d5a9fSLars-Peter Clausen addr = adis16260_addresses[chan->scan_index][1];
241555d5a9fSLars-Peter Clausen ret = adis_read_reg_16(adis, addr, &val16);
242555d5a9fSLars-Peter Clausen if (ret)
243555d5a9fSLars-Peter Clausen return ret;
244555d5a9fSLars-Peter Clausen
245555d5a9fSLars-Peter Clausen *val = val16;
246555d5a9fSLars-Peter Clausen return IIO_VAL_INT;
24782695ef5SJonathan Cameron case IIO_CHAN_INFO_SAMP_FREQ:
24882695ef5SJonathan Cameron ret = adis_read_reg_16(adis, ADIS16260_SMPL_PRD, &val16);
24982695ef5SJonathan Cameron if (ret)
25082695ef5SJonathan Cameron return ret;
25182695ef5SJonathan Cameron
25282695ef5SJonathan Cameron if (spi_get_device_id(adis->spi)->driver_data)
25382695ef5SJonathan Cameron /* If an adis16251 */
25482695ef5SJonathan Cameron *val = (val16 & ADIS16260_SMPL_PRD_TIME_BASE) ?
25582695ef5SJonathan Cameron 8 : 256;
25682695ef5SJonathan Cameron else
25782695ef5SJonathan Cameron *val = (val16 & ADIS16260_SMPL_PRD_TIME_BASE) ?
25882695ef5SJonathan Cameron 66 : 2048;
25982695ef5SJonathan Cameron *val /= (val16 & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
26082695ef5SJonathan Cameron return IIO_VAL_INT;
261555d5a9fSLars-Peter Clausen }
262555d5a9fSLars-Peter Clausen return -EINVAL;
263555d5a9fSLars-Peter Clausen }
264555d5a9fSLars-Peter Clausen
adis16260_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)265555d5a9fSLars-Peter Clausen static int adis16260_write_raw(struct iio_dev *indio_dev,
266555d5a9fSLars-Peter Clausen struct iio_chan_spec const *chan,
267555d5a9fSLars-Peter Clausen int val,
268555d5a9fSLars-Peter Clausen int val2,
269555d5a9fSLars-Peter Clausen long mask)
270555d5a9fSLars-Peter Clausen {
271f1afda12SLars-Peter Clausen struct adis16260 *adis16260 = iio_priv(indio_dev);
272f1afda12SLars-Peter Clausen struct adis *adis = &adis16260->adis;
27382695ef5SJonathan Cameron int ret;
274555d5a9fSLars-Peter Clausen u8 addr;
27582695ef5SJonathan Cameron u8 t;
276555d5a9fSLars-Peter Clausen
277555d5a9fSLars-Peter Clausen switch (mask) {
278555d5a9fSLars-Peter Clausen case IIO_CHAN_INFO_CALIBBIAS:
279555d5a9fSLars-Peter Clausen if (val < -2048 || val >= 2048)
280555d5a9fSLars-Peter Clausen return -EINVAL;
281555d5a9fSLars-Peter Clausen
282555d5a9fSLars-Peter Clausen addr = adis16260_addresses[chan->scan_index][0];
283555d5a9fSLars-Peter Clausen return adis_write_reg_16(adis, addr, val);
284555d5a9fSLars-Peter Clausen case IIO_CHAN_INFO_CALIBSCALE:
285555d5a9fSLars-Peter Clausen if (val < 0 || val >= 4096)
286555d5a9fSLars-Peter Clausen return -EINVAL;
287555d5a9fSLars-Peter Clausen
288555d5a9fSLars-Peter Clausen addr = adis16260_addresses[chan->scan_index][1];
289555d5a9fSLars-Peter Clausen return adis_write_reg_16(adis, addr, val);
29082695ef5SJonathan Cameron case IIO_CHAN_INFO_SAMP_FREQ:
2910990c6e4SNuno Sá adis_dev_lock(adis);
29282695ef5SJonathan Cameron if (spi_get_device_id(adis->spi)->driver_data)
29382695ef5SJonathan Cameron t = 256 / val;
29482695ef5SJonathan Cameron else
29582695ef5SJonathan Cameron t = 2048 / val;
29682695ef5SJonathan Cameron
29782695ef5SJonathan Cameron if (t > ADIS16260_SMPL_PRD_DIV_MASK)
29882695ef5SJonathan Cameron t = ADIS16260_SMPL_PRD_DIV_MASK;
29982695ef5SJonathan Cameron else if (t > 0)
30082695ef5SJonathan Cameron t--;
30182695ef5SJonathan Cameron
30282695ef5SJonathan Cameron if (t >= 0x0A)
30382695ef5SJonathan Cameron adis->spi->max_speed_hz = ADIS16260_SPI_SLOW;
30482695ef5SJonathan Cameron else
30582695ef5SJonathan Cameron adis->spi->max_speed_hz = ADIS16260_SPI_FAST;
306160b37f4SAlexandru Ardelean ret = __adis_write_reg_8(adis, ADIS16260_SMPL_PRD, t);
30782695ef5SJonathan Cameron
3080990c6e4SNuno Sá adis_dev_unlock(adis);
30982695ef5SJonathan Cameron return ret;
310555d5a9fSLars-Peter Clausen }
311555d5a9fSLars-Peter Clausen return -EINVAL;
312555d5a9fSLars-Peter Clausen }
313555d5a9fSLars-Peter Clausen
314555d5a9fSLars-Peter Clausen static const struct iio_info adis16260_info = {
315555d5a9fSLars-Peter Clausen .read_raw = &adis16260_read_raw,
316555d5a9fSLars-Peter Clausen .write_raw = &adis16260_write_raw,
317555d5a9fSLars-Peter Clausen .update_scan_mode = adis_update_scan_mode,
318555d5a9fSLars-Peter Clausen };
319555d5a9fSLars-Peter Clausen
320555d5a9fSLars-Peter Clausen static const char * const adis1620_status_error_msgs[] = {
321555d5a9fSLars-Peter Clausen [ADIS16260_DIAG_STAT_FLASH_CHK_BIT] = "Flash checksum error",
322555d5a9fSLars-Peter Clausen [ADIS16260_DIAG_STAT_SELF_TEST_BIT] = "Self test error",
323555d5a9fSLars-Peter Clausen [ADIS16260_DIAG_STAT_OVERFLOW_BIT] = "Sensor overrange",
324555d5a9fSLars-Peter Clausen [ADIS16260_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
325555d5a9fSLars-Peter Clausen [ADIS16260_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
326555d5a9fSLars-Peter Clausen [ADIS16260_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 5.25",
327555d5a9fSLars-Peter Clausen [ADIS16260_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 4.75",
328555d5a9fSLars-Peter Clausen };
329555d5a9fSLars-Peter Clausen
330380b107bSNuno Sá static const struct adis_timeout adis16260_timeouts = {
331380b107bSNuno Sá .reset_ms = ADIS16260_STARTUP_DELAY,
332380b107bSNuno Sá .sw_reset_ms = ADIS16260_STARTUP_DELAY,
333380b107bSNuno Sá .self_test_ms = ADIS16260_STARTUP_DELAY,
334380b107bSNuno Sá };
335380b107bSNuno Sá
336555d5a9fSLars-Peter Clausen static const struct adis_data adis16260_data = {
337555d5a9fSLars-Peter Clausen .write_delay = 30,
338555d5a9fSLars-Peter Clausen .read_delay = 30,
339555d5a9fSLars-Peter Clausen .msc_ctrl_reg = ADIS16260_MSC_CTRL,
340555d5a9fSLars-Peter Clausen .glob_cmd_reg = ADIS16260_GLOB_CMD,
341555d5a9fSLars-Peter Clausen .diag_stat_reg = ADIS16260_DIAG_STAT,
342555d5a9fSLars-Peter Clausen
343555d5a9fSLars-Peter Clausen .self_test_mask = ADIS16260_MSC_CTRL_MEM_TEST,
344fdcf6bbbSNuno Sá .self_test_reg = ADIS16260_MSC_CTRL,
345380b107bSNuno Sá .timeouts = &adis16260_timeouts,
346555d5a9fSLars-Peter Clausen
347555d5a9fSLars-Peter Clausen .status_error_msgs = adis1620_status_error_msgs,
348555d5a9fSLars-Peter Clausen .status_error_mask = BIT(ADIS16260_DIAG_STAT_FLASH_CHK_BIT) |
349555d5a9fSLars-Peter Clausen BIT(ADIS16260_DIAG_STAT_SELF_TEST_BIT) |
350555d5a9fSLars-Peter Clausen BIT(ADIS16260_DIAG_STAT_OVERFLOW_BIT) |
351555d5a9fSLars-Peter Clausen BIT(ADIS16260_DIAG_STAT_SPI_FAIL_BIT) |
352555d5a9fSLars-Peter Clausen BIT(ADIS16260_DIAG_STAT_FLASH_UPT_BIT) |
353555d5a9fSLars-Peter Clausen BIT(ADIS16260_DIAG_STAT_POWER_HIGH_BIT) |
354555d5a9fSLars-Peter Clausen BIT(ADIS16260_DIAG_STAT_POWER_LOW_BIT),
355555d5a9fSLars-Peter Clausen };
356555d5a9fSLars-Peter Clausen
adis16260_stop(void * data)3575a9b7ca6SNuno Sá static void adis16260_stop(void *data)
3585a9b7ca6SNuno Sá {
3595a9b7ca6SNuno Sá adis16260_stop_device(data);
3605a9b7ca6SNuno Sá }
3615a9b7ca6SNuno Sá
adis16260_probe(struct spi_device * spi)362555d5a9fSLars-Peter Clausen static int adis16260_probe(struct spi_device *spi)
363555d5a9fSLars-Peter Clausen {
364f1afda12SLars-Peter Clausen const struct spi_device_id *id;
365f1afda12SLars-Peter Clausen struct adis16260 *adis16260;
366555d5a9fSLars-Peter Clausen struct iio_dev *indio_dev;
367555d5a9fSLars-Peter Clausen int ret;
368555d5a9fSLars-Peter Clausen
369f1afda12SLars-Peter Clausen id = spi_get_device_id(spi);
370f1afda12SLars-Peter Clausen if (!id)
371f1afda12SLars-Peter Clausen return -ENODEV;
372f1afda12SLars-Peter Clausen
373555d5a9fSLars-Peter Clausen /* setup the industrialio driver allocated elements */
374f1afda12SLars-Peter Clausen indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16260));
375827b14a8SSachin Kamat if (!indio_dev)
376827b14a8SSachin Kamat return -ENOMEM;
377f1afda12SLars-Peter Clausen adis16260 = iio_priv(indio_dev);
378555d5a9fSLars-Peter Clausen /* this is only used for removal purposes */
379555d5a9fSLars-Peter Clausen spi_set_drvdata(spi, indio_dev);
380555d5a9fSLars-Peter Clausen
381f1afda12SLars-Peter Clausen adis16260->info = &adis16260_chip_info_table[id->driver_data];
382f1afda12SLars-Peter Clausen
383f1afda12SLars-Peter Clausen indio_dev->name = id->name;
384555d5a9fSLars-Peter Clausen indio_dev->info = &adis16260_info;
385f1afda12SLars-Peter Clausen indio_dev->channels = adis16260->info->channels;
386f1afda12SLars-Peter Clausen indio_dev->num_channels = adis16260->info->num_channels;
387555d5a9fSLars-Peter Clausen indio_dev->modes = INDIO_DIRECT_MODE;
388555d5a9fSLars-Peter Clausen
389f1afda12SLars-Peter Clausen ret = adis_init(&adis16260->adis, indio_dev, spi, &adis16260_data);
390555d5a9fSLars-Peter Clausen if (ret)
391827b14a8SSachin Kamat return ret;
392555d5a9fSLars-Peter Clausen
3935a9b7ca6SNuno Sá ret = devm_adis_setup_buffer_and_trigger(&adis16260->adis, indio_dev, NULL);
394555d5a9fSLars-Peter Clausen if (ret)
395827b14a8SSachin Kamat return ret;
396555d5a9fSLars-Peter Clausen
397555d5a9fSLars-Peter Clausen /* Get the device into a sane initial state */
398*2647f0e4SRamona Bolboaca ret = __adis_initial_startup(&adis16260->adis);
399555d5a9fSLars-Peter Clausen if (ret)
400555d5a9fSLars-Peter Clausen return ret;
401555d5a9fSLars-Peter Clausen
4025a9b7ca6SNuno Sá ret = devm_add_action_or_reset(&spi->dev, adis16260_stop, indio_dev);
4035a9b7ca6SNuno Sá if (ret)
4045a9b7ca6SNuno Sá return ret;
405555d5a9fSLars-Peter Clausen
4065a9b7ca6SNuno Sá return devm_iio_device_register(&spi->dev, indio_dev);
407555d5a9fSLars-Peter Clausen }
408555d5a9fSLars-Peter Clausen
409555d5a9fSLars-Peter Clausen /*
410555d5a9fSLars-Peter Clausen * These parts do not need to be differentiated until someone adds
411555d5a9fSLars-Peter Clausen * support for the on chip filtering.
412555d5a9fSLars-Peter Clausen */
413555d5a9fSLars-Peter Clausen static const struct spi_device_id adis16260_id[] = {
414f1afda12SLars-Peter Clausen {"adis16260", ADIS16260},
415f1afda12SLars-Peter Clausen {"adis16265", ADIS16260},
416f1afda12SLars-Peter Clausen {"adis16266", ADIS16266},
417f1afda12SLars-Peter Clausen {"adis16250", ADIS16260},
418f1afda12SLars-Peter Clausen {"adis16255", ADIS16260},
419f1afda12SLars-Peter Clausen {"adis16251", ADIS16251},
420555d5a9fSLars-Peter Clausen {}
421555d5a9fSLars-Peter Clausen };
422555d5a9fSLars-Peter Clausen MODULE_DEVICE_TABLE(spi, adis16260_id);
423555d5a9fSLars-Peter Clausen
424555d5a9fSLars-Peter Clausen static struct spi_driver adis16260_driver = {
425555d5a9fSLars-Peter Clausen .driver = {
426555d5a9fSLars-Peter Clausen .name = "adis16260",
427555d5a9fSLars-Peter Clausen },
428555d5a9fSLars-Peter Clausen .probe = adis16260_probe,
429555d5a9fSLars-Peter Clausen .id_table = adis16260_id,
430555d5a9fSLars-Peter Clausen };
431555d5a9fSLars-Peter Clausen module_spi_driver(adis16260_driver);
432555d5a9fSLars-Peter Clausen
433555d5a9fSLars-Peter Clausen MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
434555d5a9fSLars-Peter Clausen MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
435555d5a9fSLars-Peter Clausen MODULE_LICENSE("GPL v2");
4366c9304d6SJonathan Cameron MODULE_IMPORT_NS(IIO_ADISLIB);
437