Commit | Line | Data |
---|---|---|
80503b23 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1b2f99e1 BS |
2 | /* |
3 | * ADIS16080/100 Yaw Rate Gyroscope with SPI driver | |
4 | * | |
5 | * Copyright 2010 Analog Devices Inc. | |
1b2f99e1 | 6 | */ |
1b2f99e1 BS |
7 | #include <linux/delay.h> |
8 | #include <linux/mutex.h> | |
9 | #include <linux/device.h> | |
10 | #include <linux/kernel.h> | |
11 | #include <linux/spi/spi.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/sysfs.h> | |
99c97852 | 14 | #include <linux/module.h> |
1b2f99e1 | 15 | |
06458e27 JC |
16 | #include <linux/iio/iio.h> |
17 | #include <linux/iio/sysfs.h> | |
1b2f99e1 | 18 | |
35d2b6f9 JC |
19 | #define ADIS16080_DIN_GYRO (0 << 10) /* Gyroscope output */ |
20 | #define ADIS16080_DIN_TEMP (1 << 10) /* Temperature output */ | |
21 | #define ADIS16080_DIN_AIN1 (2 << 10) | |
22 | #define ADIS16080_DIN_AIN2 (3 << 10) | |
23 | ||
24 | /* | |
25 | * 1: Write contents on DIN to control register. | |
26 | * 0: No changes to control register. | |
27 | */ | |
28 | ||
29 | #define ADIS16080_DIN_WRITE (1 << 15) | |
30 | ||
668cce24 LPC |
31 | struct adis16080_chip_info { |
32 | int scale_val; | |
33 | int scale_val2; | |
34 | }; | |
35 | ||
35d2b6f9 JC |
36 | /** |
37 | * struct adis16080_state - device instance specific data | |
38 | * @us: actual spi_device to write data | |
668cce24 | 39 | * @info: chip specific parameters |
25985edc | 40 | * @buf: transmit or receive buffer |
faeda919 | 41 | * @lock: lock to protect buffer during reads |
35d2b6f9 JC |
42 | **/ |
43 | struct adis16080_state { | |
44 | struct spi_device *us; | |
668cce24 | 45 | const struct adis16080_chip_info *info; |
9318a9e5 | 46 | struct mutex lock; |
35d2b6f9 | 47 | |
ae6eeb53 | 48 | __be16 buf __aligned(IIO_DMA_MINALIGN); |
35d2b6f9 | 49 | }; |
1b2f99e1 | 50 | |
9ab82f07 LPC |
51 | static int adis16080_read_sample(struct iio_dev *indio_dev, |
52 | u16 addr, int *val) | |
1b2f99e1 | 53 | { |
1dd9290a | 54 | struct adis16080_state *st = iio_priv(indio_dev); |
1b2f99e1 | 55 | int ret; |
9ab82f07 LPC |
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 | }; | |
1b2f99e1 | 66 | |
3c80372d | 67 | st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE); |
1b2f99e1 | 68 | |
af3d5cad | 69 | ret = spi_sync_transfer(st->us, t, ARRAY_SIZE(t)); |
1b2f99e1 | 70 | if (ret == 0) |
3c80372d | 71 | *val = sign_extend32(be16_to_cpu(st->buf), 11); |
1b2f99e1 BS |
72 | |
73 | return ret; | |
74 | } | |
75 | ||
584c81fb JC |
76 | static int adis16080_read_raw(struct iio_dev *indio_dev, |
77 | struct iio_chan_spec const *chan, | |
78 | int *val, | |
79 | int *val2, | |
80 | long mask) | |
1b2f99e1 | 81 | { |
668cce24 | 82 | struct adis16080_state *st = iio_priv(indio_dev); |
9ab82f07 | 83 | int ret; |
584c81fb | 84 | |
584c81fb | 85 | switch (mask) { |
fbaff213 | 86 | case IIO_CHAN_INFO_RAW: |
9318a9e5 | 87 | mutex_lock(&st->lock); |
9ab82f07 | 88 | ret = adis16080_read_sample(indio_dev, chan->address, val); |
9318a9e5 | 89 | mutex_unlock(&st->lock); |
9ab82f07 | 90 | return ret ? ret : IIO_VAL_INT; |
668cce24 LPC |
91 | case IIO_CHAN_INFO_SCALE: |
92 | switch (chan->type) { | |
93 | case IIO_ANGL_VEL: | |
94 | *val = st->info->scale_val; | |
95 | *val2 = st->info->scale_val2; | |
96 | return IIO_VAL_FRACTIONAL; | |
97 | case IIO_VOLTAGE: | |
98 | /* VREF = 5V, 12 bits */ | |
99 | *val = 5000; | |
100 | *val2 = 12; | |
101 | return IIO_VAL_FRACTIONAL_LOG2; | |
102 | case IIO_TEMP: | |
103 | /* 85 C = 585, 25 C = 0 */ | |
104 | *val = 85000 - 25000; | |
105 | *val2 = 585; | |
106 | return IIO_VAL_FRACTIONAL; | |
107 | default: | |
108 | return -EINVAL; | |
109 | } | |
110 | case IIO_CHAN_INFO_OFFSET: | |
111 | switch (chan->type) { | |
112 | case IIO_VOLTAGE: | |
113 | /* 2.5 V = 0 */ | |
114 | *val = 2048; | |
115 | return IIO_VAL_INT; | |
116 | case IIO_TEMP: | |
117 | /* 85 C = 585, 25 C = 0 */ | |
118 | *val = DIV_ROUND_CLOSEST(25 * 585, 85 - 25); | |
119 | return IIO_VAL_INT; | |
120 | default: | |
121 | return -EINVAL; | |
122 | } | |
123 | default: | |
124 | break; | |
584c81fb | 125 | } |
1b2f99e1 | 126 | |
9ab82f07 | 127 | return -EINVAL; |
1b2f99e1 | 128 | } |
1b2f99e1 | 129 | |
584c81fb JC |
130 | static const struct iio_chan_spec adis16080_channels[] = { |
131 | { | |
41ea040c | 132 | .type = IIO_ANGL_VEL, |
584c81fb JC |
133 | .modified = 1, |
134 | .channel2 = IIO_MOD_Z, | |
89352e96 JC |
135 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
136 | BIT(IIO_CHAN_INFO_SCALE), | |
584c81fb JC |
137 | .address = ADIS16080_DIN_GYRO, |
138 | }, { | |
6835cb6b | 139 | .type = IIO_VOLTAGE, |
584c81fb JC |
140 | .indexed = 1, |
141 | .channel = 0, | |
89352e96 JC |
142 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
143 | BIT(IIO_CHAN_INFO_SCALE) | | |
144 | BIT(IIO_CHAN_INFO_OFFSET), | |
584c81fb JC |
145 | .address = ADIS16080_DIN_AIN1, |
146 | }, { | |
6835cb6b | 147 | .type = IIO_VOLTAGE, |
584c81fb JC |
148 | .indexed = 1, |
149 | .channel = 1, | |
89352e96 JC |
150 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
151 | BIT(IIO_CHAN_INFO_SCALE) | | |
152 | BIT(IIO_CHAN_INFO_OFFSET), | |
584c81fb JC |
153 | .address = ADIS16080_DIN_AIN2, |
154 | }, { | |
155 | .type = IIO_TEMP, | |
156 | .indexed = 1, | |
157 | .channel = 0, | |
89352e96 JC |
158 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
159 | BIT(IIO_CHAN_INFO_SCALE) | | |
160 | BIT(IIO_CHAN_INFO_OFFSET), | |
584c81fb JC |
161 | .address = ADIS16080_DIN_TEMP, |
162 | } | |
1b2f99e1 BS |
163 | }; |
164 | ||
6fe8135f | 165 | static const struct iio_info adis16080_info = { |
584c81fb | 166 | .read_raw = &adis16080_read_raw, |
6fe8135f JC |
167 | }; |
168 | ||
668cce24 LPC |
169 | enum { |
170 | ID_ADIS16080, | |
171 | ID_ADIS16100, | |
172 | }; | |
173 | ||
174 | static 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 | ||
4ae1c61f | 187 | static int adis16080_probe(struct spi_device *spi) |
1b2f99e1 | 188 | { |
668cce24 | 189 | const struct spi_device_id *id = spi_get_device_id(spi); |
1dd9290a JC |
190 | struct adis16080_state *st; |
191 | struct iio_dev *indio_dev; | |
192 | ||
193 | /* setup the industrialio driver allocated elements */ | |
da419463 SK |
194 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
195 | if (!indio_dev) | |
196 | return -ENOMEM; | |
1dd9290a | 197 | st = iio_priv(indio_dev); |
1b2f99e1 | 198 | |
9318a9e5 AA |
199 | mutex_init(&st->lock); |
200 | ||
1b2f99e1 | 201 | /* Allocate the comms buffers */ |
1b2f99e1 | 202 | st->us = spi; |
668cce24 | 203 | st->info = &adis16080_chip_info[id->driver_data]; |
1b2f99e1 | 204 | |
1dd9290a | 205 | indio_dev->name = spi->dev.driver->name; |
584c81fb JC |
206 | indio_dev->channels = adis16080_channels; |
207 | indio_dev->num_channels = ARRAY_SIZE(adis16080_channels); | |
1dd9290a JC |
208 | indio_dev->info = &adis16080_info; |
209 | indio_dev->modes = INDIO_DIRECT_MODE; | |
1b2f99e1 | 210 | |
da6fd259 | 211 | return devm_iio_device_register(&spi->dev, indio_dev); |
1b2f99e1 BS |
212 | } |
213 | ||
c21ab700 | 214 | static const struct spi_device_id adis16080_ids[] = { |
668cce24 LPC |
215 | { "adis16080", ID_ADIS16080 }, |
216 | { "adis16100", ID_ADIS16100 }, | |
c21ab700 LPC |
217 | {}, |
218 | }; | |
219 | MODULE_DEVICE_TABLE(spi, adis16080_ids); | |
220 | ||
1b2f99e1 BS |
221 | static struct spi_driver adis16080_driver = { |
222 | .driver = { | |
223 | .name = "adis16080", | |
1b2f99e1 BS |
224 | }, |
225 | .probe = adis16080_probe, | |
c21ab700 | 226 | .id_table = adis16080_ids, |
1b2f99e1 | 227 | }; |
ae6ae6fe | 228 | module_spi_driver(adis16080_driver); |
1b2f99e1 BS |
229 | |
230 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | |
8d016b43 | 231 | MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver"); |
1b2f99e1 | 232 | MODULE_LICENSE("GPL v2"); |