blob: ad31a1372a04ce87c4d2218d07a45bff9e51901d [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 int ret;
55 struct spi_transfer t[] = {
56 {
57 .tx_buf = &st->buf,
58 .len = 2,
59 .cs_change = 1,
60 }, {
61 .rx_buf = &st->buf,
62 .len = 2,
63 },
64 };
Barry Song1b2f99e2010-10-27 21:44:05 -040065
Lars-Peter Clausen3c803722013-01-09 14:01:00 +000066 st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE);
Barry Song1b2f99e2010-10-27 21:44:05 -040067
Lars-Peter Clausenaf3d5ca2013-10-05 08:45:00 +010068 ret = spi_sync_transfer(st->us, t, ARRAY_SIZE(t));
Barry Song1b2f99e2010-10-27 21:44:05 -040069 if (ret == 0)
Lars-Peter Clausen3c803722013-01-09 14:01:00 +000070 *val = sign_extend32(be16_to_cpu(st->buf), 11);
Barry Song1b2f99e2010-10-27 21:44:05 -040071
72 return ret;
73}
74
Jonathan Cameron584c81f2011-08-12 17:47:57 +010075static int adis16080_read_raw(struct iio_dev *indio_dev,
76 struct iio_chan_spec const *chan,
77 int *val,
78 int *val2,
79 long mask)
Barry Song1b2f99e2010-10-27 21:44:05 -040080{
Lars-Peter Clausen668cce22013-01-09 14:01:00 +000081 struct adis16080_state *st = iio_priv(indio_dev);
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +000082 int ret;
Jonathan Cameron584c81f2011-08-12 17:47:57 +010083
Jonathan Cameron584c81f2011-08-12 17:47:57 +010084 switch (mask) {
Jonathan Cameronfbaff212012-04-15 17:41:20 +010085 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +000086 mutex_lock(&indio_dev->mlock);
87 ret = adis16080_read_sample(indio_dev, chan->address, val);
88 mutex_unlock(&indio_dev->mlock);
89 return ret ? ret : IIO_VAL_INT;
Lars-Peter Clausen668cce22013-01-09 14:01:00 +000090 case IIO_CHAN_INFO_SCALE:
91 switch (chan->type) {
92 case IIO_ANGL_VEL:
93 *val = st->info->scale_val;
94 *val2 = st->info->scale_val2;
95 return IIO_VAL_FRACTIONAL;
96 case IIO_VOLTAGE:
97 /* VREF = 5V, 12 bits */
98 *val = 5000;
99 *val2 = 12;
100 return IIO_VAL_FRACTIONAL_LOG2;
101 case IIO_TEMP:
102 /* 85 C = 585, 25 C = 0 */
103 *val = 85000 - 25000;
104 *val2 = 585;
105 return IIO_VAL_FRACTIONAL;
106 default:
107 return -EINVAL;
108 }
109 case IIO_CHAN_INFO_OFFSET:
110 switch (chan->type) {
111 case IIO_VOLTAGE:
112 /* 2.5 V = 0 */
113 *val = 2048;
114 return IIO_VAL_INT;
115 case IIO_TEMP:
116 /* 85 C = 585, 25 C = 0 */
117 *val = DIV_ROUND_CLOSEST(25 * 585, 85 - 25);
118 return IIO_VAL_INT;
119 default:
120 return -EINVAL;
121 }
122 default:
123 break;
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100124 }
Barry Song1b2f99e2010-10-27 21:44:05 -0400125
Lars-Peter Clausen9ab82f02013-01-09 14:01:00 +0000126 return -EINVAL;
Barry Song1b2f99e2010-10-27 21:44:05 -0400127}
Barry Song1b2f99e2010-10-27 21:44:05 -0400128
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100129static const struct iio_chan_spec adis16080_channels[] = {
130 {
Jonathan Cameron41ea0402011-10-05 15:27:59 +0100131 .type = IIO_ANGL_VEL,
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100132 .modified = 1,
133 .channel2 = IIO_MOD_Z,
Jonathan Cameron89352e92013-02-27 19:29:24 +0000134 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
135 BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100136 .address = ADIS16080_DIN_GYRO,
137 }, {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100138 .type = IIO_VOLTAGE,
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100139 .indexed = 1,
140 .channel = 0,
Jonathan Cameron89352e92013-02-27 19:29:24 +0000141 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
142 BIT(IIO_CHAN_INFO_SCALE) |
143 BIT(IIO_CHAN_INFO_OFFSET),
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100144 .address = ADIS16080_DIN_AIN1,
145 }, {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100146 .type = IIO_VOLTAGE,
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100147 .indexed = 1,
148 .channel = 1,
Jonathan Cameron89352e92013-02-27 19:29:24 +0000149 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
150 BIT(IIO_CHAN_INFO_SCALE) |
151 BIT(IIO_CHAN_INFO_OFFSET),
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100152 .address = ADIS16080_DIN_AIN2,
153 }, {
154 .type = IIO_TEMP,
155 .indexed = 1,
156 .channel = 0,
Jonathan Cameron89352e92013-02-27 19:29:24 +0000157 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
158 BIT(IIO_CHAN_INFO_SCALE) |
159 BIT(IIO_CHAN_INFO_OFFSET),
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100160 .address = ADIS16080_DIN_TEMP,
161 }
Barry Song1b2f99e2010-10-27 21:44:05 -0400162};
163
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100164static const struct iio_info adis16080_info = {
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100165 .read_raw = &adis16080_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100166 .driver_module = THIS_MODULE,
167};
168
Lars-Peter Clausen668cce22013-01-09 14:01:00 +0000169enum {
170 ID_ADIS16080,
171 ID_ADIS16100,
172};
173
174static const struct adis16080_chip_info adis16080_chip_info[] = {
175 [ID_ADIS16080] = {
176 /* 80 degree = 819, 819 rad = 46925 degree */
177 .scale_val = 80,
178 .scale_val2 = 46925,
179 },
180 [ID_ADIS16100] = {
181 /* 300 degree = 1230, 1230 rad = 70474 degree */
182 .scale_val = 300,
183 .scale_val2 = 70474,
184 },
185};
186
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500187static int adis16080_probe(struct spi_device *spi)
Barry Song1b2f99e2010-10-27 21:44:05 -0400188{
Lars-Peter Clausen668cce22013-01-09 14:01:00 +0000189 const struct spi_device_id *id = spi_get_device_id(spi);
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100190 struct adis16080_state *st;
191 struct iio_dev *indio_dev;
192
193 /* setup the industrialio driver allocated elements */
Sachin Kamatda419462013-08-13 07:34:00 +0100194 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
195 if (!indio_dev)
196 return -ENOMEM;
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100197 st = iio_priv(indio_dev);
Barry Song1b2f99e2010-10-27 21:44:05 -0400198 /* this is only used for removal purposes */
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100199 spi_set_drvdata(spi, indio_dev);
Barry Song1b2f99e2010-10-27 21:44:05 -0400200
201 /* Allocate the comms buffers */
Barry Song1b2f99e2010-10-27 21:44:05 -0400202 st->us = spi;
Lars-Peter Clausen668cce22013-01-09 14:01:00 +0000203 st->info = &adis16080_chip_info[id->driver_data];
Barry Song1b2f99e2010-10-27 21:44:05 -0400204
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100205 indio_dev->name = spi->dev.driver->name;
Jonathan Cameron584c81f2011-08-12 17:47:57 +0100206 indio_dev->channels = adis16080_channels;
207 indio_dev->num_channels = ARRAY_SIZE(adis16080_channels);
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100208 indio_dev->dev.parent = &spi->dev;
209 indio_dev->info = &adis16080_info;
210 indio_dev->modes = INDIO_DIRECT_MODE;
Barry Song1b2f99e2010-10-27 21:44:05 -0400211
Sachin Kamatda419462013-08-13 07:34:00 +0100212 return iio_device_register(indio_dev);
Barry Song1b2f99e2010-10-27 21:44:05 -0400213}
214
Bill Pemberton447d4f22012-11-19 13:26:37 -0500215static int adis16080_remove(struct spi_device *spi)
Barry Song1b2f99e2010-10-27 21:44:05 -0400216{
Jonathan Cameron1dd92902011-06-27 13:07:44 +0100217 iio_device_unregister(spi_get_drvdata(spi));
Barry Song1b2f99e2010-10-27 21:44:05 -0400218 return 0;
219}
220
Lars-Peter Clausenc21ab702013-01-09 14:01:00 +0000221static const struct spi_device_id adis16080_ids[] = {
Lars-Peter Clausen668cce22013-01-09 14:01:00 +0000222 { "adis16080", ID_ADIS16080 },
223 { "adis16100", ID_ADIS16100 },
Lars-Peter Clausenc21ab702013-01-09 14:01:00 +0000224 {},
225};
226MODULE_DEVICE_TABLE(spi, adis16080_ids);
227
Barry Song1b2f99e2010-10-27 21:44:05 -0400228static struct spi_driver adis16080_driver = {
229 .driver = {
230 .name = "adis16080",
Barry Song1b2f99e2010-10-27 21:44:05 -0400231 },
232 .probe = adis16080_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500233 .remove = adis16080_remove,
Lars-Peter Clausenc21ab702013-01-09 14:01:00 +0000234 .id_table = adis16080_ids,
Barry Song1b2f99e2010-10-27 21:44:05 -0400235};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100236module_spi_driver(adis16080_driver);
Barry Song1b2f99e2010-10-27 21:44:05 -0400237
238MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
Jonathan Cameron8d016b42011-02-11 14:20:01 +0000239MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver");
Barry Song1b2f99e2010-10-27 21:44:05 -0400240MODULE_LICENSE("GPL v2");