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/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/module.h>
21 #define ADIS16060_GYRO 0x20 /* Measure Angular Rate (Gyro) */
22 #define ADIS16060_TEMP_OUT 0x10 /* Measure Temperature */
23 #define ADIS16060_AIN2 0x80 /* Measure AIN2 */
24 #define ADIS16060_AIN1 0x40 /* Measure AIN1 */
27 * struct adis16060_state - device instance specific data
28 * @us_w: actual spi_device to write config
29 * @us_r: actual spi_device to read back data
30 * @buf: transmit or receive buffer
31 * @buf_lock: mutex to protect tx and rx
33 struct adis16060_state {
34 struct spi_device *us_w;
35 struct spi_device *us_r;
36 struct mutex buf_lock;
38 u8 buf[3] ____cacheline_aligned;
41 static struct iio_dev *adis16060_iio_dev;
43 static int adis16060_spi_write(struct iio_dev *indio_dev, u8 val)
46 struct adis16060_state *st = iio_priv(indio_dev);
48 mutex_lock(&st->buf_lock);
49 st->buf[2] = val; /* The last 8 bits clocked in are latched */
50 ret = spi_write(st->us_w, st->buf, 3);
51 mutex_unlock(&st->buf_lock);
56 static int adis16060_spi_read(struct iio_dev *indio_dev, u16 *val)
59 struct adis16060_state *st = iio_priv(indio_dev);
61 mutex_lock(&st->buf_lock);
63 ret = spi_read(st->us_r, st->buf, 3);
65 /* The internal successive approximation ADC begins the
66 * conversion process on the falling edge of MSEL1 and
67 * starts to place data MSB first on the DOUT line at
68 * the 6th falling edge of SCLK
71 *val = ((st->buf[0] & 0x3) << 12) |
73 ((st->buf[2] >> 4) & 0xF);
74 mutex_unlock(&st->buf_lock);
79 static int adis16060_read_raw(struct iio_dev *indio_dev,
80 struct iio_chan_spec const *chan,
89 /* Take the iio_dev status lock */
90 mutex_lock(&indio_dev->mlock);
91 ret = adis16060_spi_write(indio_dev, chan->address);
93 mutex_unlock(&indio_dev->mlock);
96 ret = adis16060_spi_read(indio_dev, &tval);
97 mutex_unlock(&indio_dev->mlock);
100 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
103 return IIO_VAL_INT_PLUS_MICRO;
104 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
107 return IIO_VAL_INT_PLUS_MICRO;
113 static const struct iio_info adis16060_info = {
114 .read_raw = &adis16060_read_raw,
115 .driver_module = THIS_MODULE,
118 static const struct iio_chan_spec adis16060_channels[] = {
120 .type = IIO_ANGL_VEL,
122 .channel2 = IIO_MOD_Z,
123 .address = ADIS16060_GYRO,
128 .address = ADIS16060_AIN1,
133 .address = ADIS16060_AIN2,
138 .info_mask = (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
139 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
140 .address = ADIS16060_TEMP_OUT,
144 static int __devinit adis16060_r_probe(struct spi_device *spi)
147 struct adis16060_state *st;
148 struct iio_dev *indio_dev;
150 /* setup the industrialio driver allocated elements */
151 indio_dev = iio_allocate_device(sizeof(*st));
152 if (indio_dev == NULL) {
156 /* this is only used for removal purposes */
157 spi_set_drvdata(spi, indio_dev);
158 st = iio_priv(indio_dev);
160 mutex_init(&st->buf_lock);
162 indio_dev->name = spi->dev.driver->name;
163 indio_dev->dev.parent = &spi->dev;
164 indio_dev->info = &adis16060_info;
165 indio_dev->modes = INDIO_DIRECT_MODE;
166 indio_dev->channels = adis16060_channels;
167 indio_dev->num_channels = ARRAY_SIZE(adis16060_channels);
169 ret = iio_device_register(indio_dev);
173 adis16060_iio_dev = indio_dev;
177 iio_free_device(indio_dev);
182 /* fixme, confirm ordering in this function */
183 static int adis16060_r_remove(struct spi_device *spi)
185 iio_device_unregister(spi_get_drvdata(spi));
186 iio_free_device(spi_get_drvdata(spi));
191 static int __devinit adis16060_w_probe(struct spi_device *spi)
194 struct iio_dev *indio_dev = adis16060_iio_dev;
195 struct adis16060_state *st;
200 st = iio_priv(indio_dev);
201 spi_set_drvdata(spi, indio_dev);
209 static int adis16060_w_remove(struct spi_device *spi)
214 static struct spi_driver adis16060_r_driver = {
216 .name = "adis16060_r",
217 .owner = THIS_MODULE,
219 .probe = adis16060_r_probe,
220 .remove = __devexit_p(adis16060_r_remove),
223 static struct spi_driver adis16060_w_driver = {
225 .name = "adis16060_w",
226 .owner = THIS_MODULE,
228 .probe = adis16060_w_probe,
229 .remove = __devexit_p(adis16060_w_remove),
232 static __init int adis16060_init(void)
236 ret = spi_register_driver(&adis16060_r_driver);
240 ret = spi_register_driver(&adis16060_w_driver);
242 spi_unregister_driver(&adis16060_r_driver);
248 module_init(adis16060_init);
250 static __exit void adis16060_exit(void)
252 spi_unregister_driver(&adis16060_w_driver);
253 spi_unregister_driver(&adis16060_r_driver);
255 module_exit(adis16060_exit);
257 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
258 MODULE_DESCRIPTION("Analog Devices ADIS16060 Yaw Rate Gyroscope Driver");
259 MODULE_LICENSE("GPL v2");