blob: e1bb5f994a548406728e3eb86f35200e76ede502 [file] [log] [blame]
Barry Song1b2f99e2010-10-27 21:44:05 -04001/*
2 * ADIS16080/100 Yaw Rate Gyroscope with SPI driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
Barry Song1b2f99e2010-10-27 21:44:05 -04008#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>
Paul Gortmaker99c97852011-07-03 15:49:50 -040015#include <linux/module.h>
Barry Song1b2f99e2010-10-27 21:44:05 -040016
Jonathan Cameron06458e22012-04-25 15:54:58 +010017#include <linux/iio/iio.h>
18#include <linux/iio/sysfs.h>
Barry Song1b2f99e2010-10-27 21:44:05 -040019
Jonathan Cameron35d2b6f2011-02-25 16:09:07 +000020#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)
24
25/*
26 * 1: Write contents on DIN to control register.
27 * 0: No changes to control register.
28 */
29
30#define ADIS16080_DIN_WRITE (1 << 15)
31
Lars-Peter Clausen668cce22013-01-09 14:01:00 +000032struct adis16080_chip_info {
33 int scale_val;
34 int scale_val2;
35};
36
Jonathan Cameron35d2b6f2011-02-25 16:09:07 +000037/**
38 * struct adis16080_state - device instance specific data
39 * @us: actual spi_device to write data
Lars-Peter Clausen668cce22013-01-09 14:01:00 +000040 * @info: chip specific parameters
Lucas De Marchi25985ed2011-03-30 22:57:33 -030041 * @buf: transmit or receive buffer
Jonathan Cameron35d2b6f2011-02-25 16:09:07 +000042 **/
43struct adis16080_state {
44 struct spi_device *us;
Lars-Peter Clausen668cce22013-01-09 14:01:00 +000045 const struct adis16080_chip_info *info;
Jonathan Cameron35d2b6f2011-02-25 16:09:07 +000046
Lars-Peter Clausen3c803722013-01-09 14:01:00 +000047 __be16 buf ____cacheline_aligned;
Jonathan Cameron35d2b6f2011-02-25 16:09:07 +000048};
Barry Song1b2f99e2010-10-27 21:44:05 -040049
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +000050static int adis16080_read_sample(struct iio_dev *indio_dev,
51 u16 addr, int *val)
Barry Song1b2f99e2010-10-27 21:44:05 -040052{
Jonathan Cameron1dd92902011-06-27 13:07:44 +010053 struct adis16080_state *st = iio_priv(indio_dev);
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +000054 struct spi_message m;
55 int ret;
56 struct spi_transfer t[] = {
57 {
58 .tx_buf = &st->buf,
59 .len = 2,
60 .cs_change = 1,
61 }, {
62 .rx_buf = &st->buf,
63 .len = 2,
64 },
65 };
Barry Song1b2f99e2010-10-27 21:44:05 -040066
Lars-Peter Clausen3c803722013-01-09 14:01:00 +000067 st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE);
Barry Song1b2f99e2010-10-27 21:44:05 -040068
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +000069 spi_message_init(&m);
70 spi_message_add_tail(&t[0], &m);
71 spi_message_add_tail(&t[1], &m);
Barry Song1b2f99e2010-10-27 21:44:05 -040072
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +000073 ret = spi_sync(st->us, &m);
Barry Song1b2f99e2010-10-27 21:44:05 -040074 if (ret == 0)
Lars-Peter Clausen3c803722013-01-09 14:01:00 +000075 *val = sign_extend32(be16_to_cpu(st->buf), 11);
Barry Song1b2f99e2010-10-27 21:44:05 -040076
77 return ret;
78}
79
Jonathan Cameron584c81f2011-08-12 17:47:57 +010080static int adis16080_read_raw(struct iio_dev *indio_dev,
81 struct iio_chan_spec const *chan,
82 int *val,
83 int *val2,
84 long mask)
Barry Song1b2f99e2010-10-27 21:44:05 -040085{
Lars-Peter Clausen668cce22013-01-09 14:01:00 +000086 struct adis16080_state *st = iio_priv(indio_dev);
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +000087 int ret;
Jonathan Cameron584c81f2011-08-12 17:47:57 +010088
Jonathan Cameron584c81f2011-08-12 17:47:57 +010089 switch (mask) {
Jonathan Cameronfbaff212012-04-15 17:41:20 +010090 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +000091 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;
Lars-Peter Clausen668cce22013-01-09 14:01:00 +000095 case IIO_CHAN_INFO_SCALE:
96 switch (chan->type) {
97 case IIO_ANGL_VEL:
98 *val = st->info->scale_val;
99 *val2 = st->info->scale_val2;
100 return IIO_VAL_FRACTIONAL;
101 case IIO_VOLTAGE:
102 /* VREF = 5V, 12 bits */
103 *val = 5000;
104 *val2 = 12;
105 return IIO_VAL_FRACTIONAL_LOG2;
106 case IIO_TEMP:
107 /* 85 C = 585, 25 C = 0 */
108 *val = 85000 - 25000;
109 *val2 = 585;
110 return IIO_VAL_FRACTIONAL;
111 default:
112 return -EINVAL;
113 }
114 case IIO_CHAN_INFO_OFFSET:
115 switch (chan->type) {
116 case IIO_VOLTAGE:
117 /* 2.5 V = 0 */
118 *val = 2048;
119 return IIO_VAL_INT;
120 case IIO_TEMP:
121 /* 85 C = 585, 25 C = 0 */
122 *val = DIV_ROUND_CLOSEST(25 * 585, 85 - 25);
123 return IIO_VAL_INT;
124 default:
125 return -EINVAL;
126 }
127 default:
128 break;
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100129 }
Barry Song1b2f99e2010-10-27 21:44:05 -0400130
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +0000131 return -EINVAL;
Barry Song1b2f99e2010-10-27 21:44:05 -0400132}
Barry Song1b2f99e2010-10-27 21:44:05 -0400133
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100134static const struct iio_chan_spec adis16080_channels[] = {
135 {
Jonathan Cameron41ea0402011-10-05 15:27:59 +0100136 .type = IIO_ANGL_VEL,
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100137 .modified = 1,
138 .channel2 = IIO_MOD_Z,
Jonathan Cameron89352e92013-02-27 19:29:24 +0000139 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
140 BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100141 .address = ADIS16080_DIN_GYRO,
142 }, {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100143 .type = IIO_VOLTAGE,
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100144 .indexed = 1,
145 .channel = 0,
Jonathan Cameron89352e92013-02-27 19:29:24 +0000146 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
147 BIT(IIO_CHAN_INFO_SCALE) |
148 BIT(IIO_CHAN_INFO_OFFSET),
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100149 .address = ADIS16080_DIN_AIN1,
150 }, {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100151 .type = IIO_VOLTAGE,
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100152 .indexed = 1,
153 .channel = 1,
Jonathan Cameron89352e92013-02-27 19:29:24 +0000154 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
155 BIT(IIO_CHAN_INFO_SCALE) |
156 BIT(IIO_CHAN_INFO_OFFSET),
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100157 .address = ADIS16080_DIN_AIN2,
158 }, {
159 .type = IIO_TEMP,
160 .indexed = 1,
161 .channel = 0,
Jonathan Cameron89352e92013-02-27 19:29:24 +0000162 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
163 BIT(IIO_CHAN_INFO_SCALE) |
164 BIT(IIO_CHAN_INFO_OFFSET),
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100165 .address = ADIS16080_DIN_TEMP,
166 }
Barry Song1b2f99e2010-10-27 21:44:05 -0400167};
168
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100169static const struct iio_info adis16080_info = {
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100170 .read_raw = &adis16080_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100171 .driver_module = THIS_MODULE,
172};
173
Lars-Peter Clausen668cce22013-01-09 14:01:00 +0000174enum {
175 ID_ADIS16080,
176 ID_ADIS16100,
177};
178
179static const struct adis16080_chip_info adis16080_chip_info[] = {
180 [ID_ADIS16080] = {
181 /* 80 degree = 819, 819 rad = 46925 degree */
182 .scale_val = 80,
183 .scale_val2 = 46925,
184 },
185 [ID_ADIS16100] = {
186 /* 300 degree = 1230, 1230 rad = 70474 degree */
187 .scale_val = 300,
188 .scale_val2 = 70474,
189 },
190};
191
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500192static int adis16080_probe(struct spi_device *spi)
Barry Song1b2f99e2010-10-27 21:44:05 -0400193{
Lars-Peter Clausen668cce22013-01-09 14:01:00 +0000194 const struct spi_device_id *id = spi_get_device_id(spi);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100195 int ret;
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100196 struct adis16080_state *st;
197 struct iio_dev *indio_dev;
198
199 /* setup the industrialio driver allocated elements */
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200200 indio_dev = iio_device_alloc(sizeof(*st));
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100201 if (indio_dev == NULL) {
202 ret = -ENOMEM;
Barry Song1b2f99e2010-10-27 21:44:05 -0400203 goto error_ret;
204 }
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100205 st = iio_priv(indio_dev);
Barry Song1b2f99e2010-10-27 21:44:05 -0400206 /* this is only used for removal purposes */
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100207 spi_set_drvdata(spi, indio_dev);
Barry Song1b2f99e2010-10-27 21:44:05 -0400208
209 /* Allocate the comms buffers */
Barry Song1b2f99e2010-10-27 21:44:05 -0400210 st->us = spi;
Lars-Peter Clausen668cce22013-01-09 14:01:00 +0000211 st->info = &adis16080_chip_info[id->driver_data];
Barry Song1b2f99e2010-10-27 21:44:05 -0400212
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100213 indio_dev->name = spi->dev.driver->name;
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100214 indio_dev->channels = adis16080_channels;
215 indio_dev->num_channels = ARRAY_SIZE(adis16080_channels);
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100216 indio_dev->dev.parent = &spi->dev;
217 indio_dev->info = &adis16080_info;
218 indio_dev->modes = INDIO_DIRECT_MODE;
Barry Song1b2f99e2010-10-27 21:44:05 -0400219
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100220 ret = iio_device_register(indio_dev);
Barry Song1b2f99e2010-10-27 21:44:05 -0400221 if (ret)
Jonathan Cameron8d016b42011-02-11 14:20:01 +0000222 goto error_free_dev;
Barry Song1b2f99e2010-10-27 21:44:05 -0400223 return 0;
224
Barry Song1b2f99e2010-10-27 21:44:05 -0400225error_free_dev:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200226 iio_device_free(indio_dev);
Barry Song1b2f99e2010-10-27 21:44:05 -0400227error_ret:
228 return ret;
229}
230
Bill Pemberton447d4f22012-11-19 13:26:37 -0500231static int adis16080_remove(struct spi_device *spi)
Barry Song1b2f99e2010-10-27 21:44:05 -0400232{
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100233 iio_device_unregister(spi_get_drvdata(spi));
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200234 iio_device_free(spi_get_drvdata(spi));
Barry Song1b2f99e2010-10-27 21:44:05 -0400235
236 return 0;
237}
238
Lars-Peter Clausenc21ab702013-01-09 14:01:00 +0000239static const struct spi_device_id adis16080_ids[] = {
Lars-Peter Clausen668cce22013-01-09 14:01:00 +0000240 { "adis16080", ID_ADIS16080 },
241 { "adis16100", ID_ADIS16100 },
Lars-Peter Clausenc21ab702013-01-09 14:01:00 +0000242 {},
243};
244MODULE_DEVICE_TABLE(spi, adis16080_ids);
245
Barry Song1b2f99e2010-10-27 21:44:05 -0400246static struct spi_driver adis16080_driver = {
247 .driver = {
248 .name = "adis16080",
249 .owner = THIS_MODULE,
250 },
251 .probe = adis16080_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500252 .remove = adis16080_remove,
Lars-Peter Clausenc21ab702013-01-09 14:01:00 +0000253 .id_table = adis16080_ids,
Barry Song1b2f99e2010-10-27 21:44:05 -0400254};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100255module_spi_driver(adis16080_driver);
Barry Song1b2f99e2010-10-27 21:44:05 -0400256
257MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
Jonathan Cameron8d016b42011-02-11 14:20:01 +0000258MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver");
Barry Song1b2f99e2010-10-27 21:44:05 -0400259MODULE_LICENSE("GPL v2");