2 * ADIS16080/100 Yaw Rate Gyroscope with SPI driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
8 #include <linux/delay.h>
9 #include <linux/mutex.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/spi/spi.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/module.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
20 #define ADIS16080_DIN_GYRO (0 << 10) /* Gyroscope output */
21 #define ADIS16080_DIN_TEMP (1 << 10) /* Temperature output */
22 #define ADIS16080_DIN_AIN1 (2 << 10)
23 #define ADIS16080_DIN_AIN2 (3 << 10)
26 * 1: Write contents on DIN to control register.
27 * 0: No changes to control register.
30 #define ADIS16080_DIN_WRITE (1 << 15)
32 struct adis16080_chip_info {
38 * struct adis16080_state - device instance specific data
39 * @us: actual spi_device to write data
40 * @info: chip specific parameters
41 * @buf: transmit or receive buffer
43 struct adis16080_state {
44 struct spi_device *us;
45 const struct adis16080_chip_info *info;
47 __be16 buf ____cacheline_aligned;
50 static int adis16080_read_sample(struct iio_dev *indio_dev,
53 struct adis16080_state *st = iio_priv(indio_dev);
56 struct spi_transfer t[] = {
67 st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE);
70 spi_message_add_tail(&t[0], &m);
71 spi_message_add_tail(&t[1], &m);
73 ret = spi_sync(st->us, &m);
75 *val = sign_extend32(be16_to_cpu(st->buf), 11);
80 static int adis16080_read_raw(struct iio_dev *indio_dev,
81 struct iio_chan_spec const *chan,
86 struct adis16080_state *st = iio_priv(indio_dev);
90 case IIO_CHAN_INFO_RAW:
91 mutex_lock(&indio_dev->mlock);
92 ret = adis16080_read_sample(indio_dev, chan->address, val);
93 mutex_unlock(&indio_dev->mlock);
94 return ret ? ret : IIO_VAL_INT;
95 case IIO_CHAN_INFO_SCALE:
98 *val = st->info->scale_val;
99 *val2 = st->info->scale_val2;
100 return IIO_VAL_FRACTIONAL;
102 /* VREF = 5V, 12 bits */
105 return IIO_VAL_FRACTIONAL_LOG2;
107 /* 85 C = 585, 25 C = 0 */
108 *val = 85000 - 25000;
110 return IIO_VAL_FRACTIONAL;
114 case IIO_CHAN_INFO_OFFSET:
115 switch (chan->type) {
121 /* 85 C = 585, 25 C = 0 */
122 *val = DIV_ROUND_CLOSEST(25 * 585, 85 - 25);
134 static const struct iio_chan_spec adis16080_channels[] = {
136 .type = IIO_ANGL_VEL,
138 .channel2 = IIO_MOD_Z,
139 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
140 BIT(IIO_CHAN_INFO_SCALE),
141 .address = ADIS16080_DIN_GYRO,
146 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
147 BIT(IIO_CHAN_INFO_SCALE) |
148 BIT(IIO_CHAN_INFO_OFFSET),
149 .address = ADIS16080_DIN_AIN1,
154 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
155 BIT(IIO_CHAN_INFO_SCALE) |
156 BIT(IIO_CHAN_INFO_OFFSET),
157 .address = ADIS16080_DIN_AIN2,
162 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
163 BIT(IIO_CHAN_INFO_SCALE) |
164 BIT(IIO_CHAN_INFO_OFFSET),
165 .address = ADIS16080_DIN_TEMP,
169 static const struct iio_info adis16080_info = {
170 .read_raw = &adis16080_read_raw,
171 .driver_module = THIS_MODULE,
179 static const struct adis16080_chip_info adis16080_chip_info[] = {
181 /* 80 degree = 819, 819 rad = 46925 degree */
186 /* 300 degree = 1230, 1230 rad = 70474 degree */
192 static int adis16080_probe(struct spi_device *spi)
194 const struct spi_device_id *id = spi_get_device_id(spi);
196 struct adis16080_state *st;
197 struct iio_dev *indio_dev;
199 /* setup the industrialio driver allocated elements */
200 indio_dev = iio_device_alloc(sizeof(*st));
201 if (indio_dev == NULL) {
205 st = iio_priv(indio_dev);
206 /* this is only used for removal purposes */
207 spi_set_drvdata(spi, indio_dev);
209 /* Allocate the comms buffers */
211 st->info = &adis16080_chip_info[id->driver_data];
213 indio_dev->name = spi->dev.driver->name;
214 indio_dev->channels = adis16080_channels;
215 indio_dev->num_channels = ARRAY_SIZE(adis16080_channels);
216 indio_dev->dev.parent = &spi->dev;
217 indio_dev->info = &adis16080_info;
218 indio_dev->modes = INDIO_DIRECT_MODE;
220 ret = iio_device_register(indio_dev);
226 iio_device_free(indio_dev);
231 static int adis16080_remove(struct spi_device *spi)
233 iio_device_unregister(spi_get_drvdata(spi));
234 iio_device_free(spi_get_drvdata(spi));
239 static const struct spi_device_id adis16080_ids[] = {
240 { "adis16080", ID_ADIS16080 },
241 { "adis16100", ID_ADIS16100 },
244 MODULE_DEVICE_TABLE(spi, adis16080_ids);
246 static struct spi_driver adis16080_driver = {
249 .owner = THIS_MODULE,
251 .probe = adis16080_probe,
252 .remove = adis16080_remove,
253 .id_table = adis16080_ids,
255 module_spi_driver(adis16080_driver);
257 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
258 MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver");
259 MODULE_LICENSE("GPL v2");