2 * ADIS16060 Wide Bandwidth Yaw Rate Gyroscope with SPI driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/mutex.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/module.h>
22 #define ADIS16060_GYRO 0x20 /* Measure Angular Rate (Gyro) */
23 #define ADIS16060_TEMP_OUT 0x10 /* Measure Temperature */
24 #define ADIS16060_AIN2 0x80 /* Measure AIN2 */
25 #define ADIS16060_AIN1 0x40 /* Measure AIN1 */
28 * struct adis16060_state - device instance specific data
29 * @us_w: actual spi_device to write config
30 * @us_r: actual spi_device to read back data
31 * @buf: transmit or receive buffer
32 * @buf_lock: mutex to protect tx and rx
34 struct adis16060_state {
35 struct spi_device *us_w;
36 struct spi_device *us_r;
37 struct mutex buf_lock;
39 u8 buf[3] ____cacheline_aligned;
42 static struct iio_dev *adis16060_iio_dev;
44 static int adis16060_spi_write(struct iio_dev *indio_dev, u8 val)
47 struct adis16060_state *st = iio_priv(indio_dev);
49 mutex_lock(&st->buf_lock);
50 st->buf[2] = val; /* The last 8 bits clocked in are latched */
51 ret = spi_write(st->us_w, st->buf, 3);
52 mutex_unlock(&st->buf_lock);
57 static int adis16060_spi_read(struct iio_dev *indio_dev, u16 *val)
60 struct adis16060_state *st = iio_priv(indio_dev);
62 mutex_lock(&st->buf_lock);
64 ret = spi_read(st->us_r, st->buf, 3);
66 /* The internal successive approximation ADC begins the
67 * conversion process on the falling edge of MSEL1 and
68 * starts to place data MSB first on the DOUT line at
69 * the 6th falling edge of SCLK
72 *val = ((st->buf[0] & 0x3) << 12) |
74 ((st->buf[2] >> 4) & 0xF);
75 mutex_unlock(&st->buf_lock);
80 static int adis16060_read_raw(struct iio_dev *indio_dev,
81 struct iio_chan_spec const *chan,
90 /* Take the iio_dev status lock */
91 mutex_lock(&indio_dev->mlock);
92 ret = adis16060_spi_write(indio_dev, chan->address);
94 mutex_unlock(&indio_dev->mlock);
97 ret = adis16060_spi_read(indio_dev, &tval);
98 mutex_unlock(&indio_dev->mlock);
101 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
104 return IIO_VAL_INT_PLUS_MICRO;
105 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
108 return IIO_VAL_INT_PLUS_MICRO;
114 static const struct iio_info adis16060_info = {
115 .read_raw = &adis16060_read_raw,
116 .driver_module = THIS_MODULE,
119 static const struct iio_chan_spec adis16060_channels[] = {
121 .type = IIO_ANGL_VEL,
123 .channel2 = IIO_MOD_Z,
124 .address = ADIS16060_GYRO,
129 .address = ADIS16060_AIN1,
134 .address = ADIS16060_AIN2,
139 .info_mask = (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
140 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
141 .address = ADIS16060_TEMP_OUT,
145 static int __devinit adis16060_r_probe(struct spi_device *spi)
148 struct adis16060_state *st;
149 struct iio_dev *indio_dev;
151 /* setup the industrialio driver allocated elements */
152 indio_dev = iio_allocate_device(sizeof(*st));
153 if (indio_dev == NULL) {
157 /* this is only used for removal purposes */
158 spi_set_drvdata(spi, indio_dev);
159 st = iio_priv(indio_dev);
161 mutex_init(&st->buf_lock);
163 indio_dev->name = spi->dev.driver->name;
164 indio_dev->dev.parent = &spi->dev;
165 indio_dev->info = &adis16060_info;
166 indio_dev->modes = INDIO_DIRECT_MODE;
167 indio_dev->channels = adis16060_channels;
168 indio_dev->num_channels = ARRAY_SIZE(adis16060_channels);
170 ret = iio_device_register(indio_dev);
174 adis16060_iio_dev = indio_dev;
178 iio_free_device(indio_dev);
183 /* fixme, confirm ordering in this function */
184 static int adis16060_r_remove(struct spi_device *spi)
186 iio_device_unregister(spi_get_drvdata(spi));
187 iio_free_device(spi_get_drvdata(spi));
192 static int __devinit adis16060_w_probe(struct spi_device *spi)
195 struct iio_dev *indio_dev = adis16060_iio_dev;
196 struct adis16060_state *st;
201 st = iio_priv(indio_dev);
202 spi_set_drvdata(spi, indio_dev);
210 static int adis16060_w_remove(struct spi_device *spi)
215 static struct spi_driver adis16060_r_driver = {
217 .name = "adis16060_r",
218 .owner = THIS_MODULE,
220 .probe = adis16060_r_probe,
221 .remove = __devexit_p(adis16060_r_remove),
224 static struct spi_driver adis16060_w_driver = {
226 .name = "adis16060_w",
227 .owner = THIS_MODULE,
229 .probe = adis16060_w_probe,
230 .remove = __devexit_p(adis16060_w_remove),
233 static __init int adis16060_init(void)
237 ret = spi_register_driver(&adis16060_r_driver);
241 ret = spi_register_driver(&adis16060_w_driver);
243 spi_unregister_driver(&adis16060_r_driver);
249 module_init(adis16060_init);
251 static __exit void adis16060_exit(void)
253 spi_unregister_driver(&adis16060_w_driver);
254 spi_unregister_driver(&adis16060_r_driver);
256 module_exit(adis16060_exit);
258 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
259 MODULE_DESCRIPTION("Analog Devices ADIS16060 Yaw Rate Gyroscope Driver");
260 MODULE_LICENSE("GPL v2");