1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ADIS16080/100 Yaw Rate Gyroscope with SPI driver
5 * Copyright 2010 Analog Devices Inc.
7 #include <linux/delay.h>
8 #include <linux/mutex.h>
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/module.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
19 #define ADIS16080_DIN_GYRO (0 << 10) /* Gyroscope output */
20 #define ADIS16080_DIN_TEMP (1 << 10) /* Temperature output */
21 #define ADIS16080_DIN_AIN1 (2 << 10)
22 #define ADIS16080_DIN_AIN2 (3 << 10)
25 * 1: Write contents on DIN to control register.
26 * 0: No changes to control register.
29 #define ADIS16080_DIN_WRITE (1 << 15)
31 struct adis16080_chip_info
{
37 * struct adis16080_state - device instance specific data
38 * @us: actual spi_device to write data
39 * @info: chip specific parameters
40 * @buf: transmit or receive buffer
41 * @lock: lock to protect buffer during reads
43 struct adis16080_state
{
44 struct spi_device
*us
;
45 const struct adis16080_chip_info
*info
;
48 __be16 buf
__aligned(IIO_DMA_MINALIGN
);
51 static int adis16080_read_sample(struct iio_dev
*indio_dev
,
54 struct adis16080_state
*st
= iio_priv(indio_dev
);
56 struct spi_transfer t
[] = {
67 st
->buf
= cpu_to_be16(addr
| ADIS16080_DIN_WRITE
);
69 ret
= spi_sync_transfer(st
->us
, t
, ARRAY_SIZE(t
));
71 *val
= sign_extend32(be16_to_cpu(st
->buf
), 11);
76 static int adis16080_read_raw(struct iio_dev
*indio_dev
,
77 struct iio_chan_spec
const *chan
,
82 struct adis16080_state
*st
= iio_priv(indio_dev
);
86 case IIO_CHAN_INFO_RAW
:
87 mutex_lock(&st
->lock
);
88 ret
= adis16080_read_sample(indio_dev
, chan
->address
, val
);
89 mutex_unlock(&st
->lock
);
90 return ret
? ret
: IIO_VAL_INT
;
91 case IIO_CHAN_INFO_SCALE
:
94 *val
= st
->info
->scale_val
;
95 *val2
= st
->info
->scale_val2
;
96 return IIO_VAL_FRACTIONAL
;
98 /* VREF = 5V, 12 bits */
101 return IIO_VAL_FRACTIONAL_LOG2
;
103 /* 85 C = 585, 25 C = 0 */
104 *val
= 85000 - 25000;
106 return IIO_VAL_FRACTIONAL
;
110 case IIO_CHAN_INFO_OFFSET
:
111 switch (chan
->type
) {
117 /* 85 C = 585, 25 C = 0 */
118 *val
= DIV_ROUND_CLOSEST(25 * 585, 85 - 25);
130 static const struct iio_chan_spec adis16080_channels
[] = {
132 .type
= IIO_ANGL_VEL
,
134 .channel2
= IIO_MOD_Z
,
135 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
136 BIT(IIO_CHAN_INFO_SCALE
),
137 .address
= ADIS16080_DIN_GYRO
,
142 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
143 BIT(IIO_CHAN_INFO_SCALE
) |
144 BIT(IIO_CHAN_INFO_OFFSET
),
145 .address
= ADIS16080_DIN_AIN1
,
150 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
151 BIT(IIO_CHAN_INFO_SCALE
) |
152 BIT(IIO_CHAN_INFO_OFFSET
),
153 .address
= ADIS16080_DIN_AIN2
,
158 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
159 BIT(IIO_CHAN_INFO_SCALE
) |
160 BIT(IIO_CHAN_INFO_OFFSET
),
161 .address
= ADIS16080_DIN_TEMP
,
165 static const struct iio_info adis16080_info
= {
166 .read_raw
= &adis16080_read_raw
,
174 static const struct adis16080_chip_info adis16080_chip_info
[] = {
176 /* 80 degree = 819, 819 rad = 46925 degree */
181 /* 300 degree = 1230, 1230 rad = 70474 degree */
187 static int adis16080_probe(struct spi_device
*spi
)
189 const struct spi_device_id
*id
= spi_get_device_id(spi
);
190 struct adis16080_state
*st
;
191 struct iio_dev
*indio_dev
;
193 /* setup the industrialio driver allocated elements */
194 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
197 st
= iio_priv(indio_dev
);
199 mutex_init(&st
->lock
);
201 /* Allocate the comms buffers */
203 st
->info
= &adis16080_chip_info
[id
->driver_data
];
205 indio_dev
->name
= spi
->dev
.driver
->name
;
206 indio_dev
->channels
= adis16080_channels
;
207 indio_dev
->num_channels
= ARRAY_SIZE(adis16080_channels
);
208 indio_dev
->info
= &adis16080_info
;
209 indio_dev
->modes
= INDIO_DIRECT_MODE
;
211 return devm_iio_device_register(&spi
->dev
, indio_dev
);
214 static const struct spi_device_id adis16080_ids
[] = {
215 { "adis16080", ID_ADIS16080
},
216 { "adis16100", ID_ADIS16100
},
219 MODULE_DEVICE_TABLE(spi
, adis16080_ids
);
221 static struct spi_driver adis16080_driver
= {
225 .probe
= adis16080_probe
,
226 .id_table
= adis16080_ids
,
228 module_spi_driver(adis16080_driver
);
230 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
231 MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver");
232 MODULE_LICENSE("GPL v2");