Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 1 | /* |
| 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 Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 8 | #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 Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 16 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 17 | #include <linux/iio/iio.h> |
| 18 | #include <linux/iio/sysfs.h> |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 19 | |
Jonathan Cameron | 35d2b6f | 2011-02-25 16:09:07 +0000 | [diff] [blame] | 20 | #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 Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 32 | struct adis16080_chip_info { |
| 33 | int scale_val; |
| 34 | int scale_val2; |
| 35 | }; |
| 36 | |
Jonathan Cameron | 35d2b6f | 2011-02-25 16:09:07 +0000 | [diff] [blame] | 37 | /** |
| 38 | * struct adis16080_state - device instance specific data |
| 39 | * @us: actual spi_device to write data |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 40 | * @info: chip specific parameters |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 41 | * @buf: transmit or receive buffer |
Jonathan Cameron | 35d2b6f | 2011-02-25 16:09:07 +0000 | [diff] [blame] | 42 | **/ |
| 43 | struct adis16080_state { |
| 44 | struct spi_device *us; |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 45 | const struct adis16080_chip_info *info; |
Jonathan Cameron | 35d2b6f | 2011-02-25 16:09:07 +0000 | [diff] [blame] | 46 | |
Lars-Peter Clausen | 3c80372 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 47 | __be16 buf ____cacheline_aligned; |
Jonathan Cameron | 35d2b6f | 2011-02-25 16:09:07 +0000 | [diff] [blame] | 48 | }; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 49 | |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 50 | static int adis16080_read_sample(struct iio_dev *indio_dev, |
| 51 | u16 addr, int *val) |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 52 | { |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 53 | struct adis16080_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 54 | 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 Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 65 | |
Lars-Peter Clausen | 3c80372 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 66 | st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 67 | |
Lars-Peter Clausen | af3d5ca | 2013-10-05 08:45:00 +0100 | [diff] [blame] | 68 | ret = spi_sync_transfer(st->us, t, ARRAY_SIZE(t)); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 69 | if (ret == 0) |
Lars-Peter Clausen | 3c80372 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 70 | *val = sign_extend32(be16_to_cpu(st->buf), 11); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 75 | static 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 Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 80 | { |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 81 | struct adis16080_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 82 | int ret; |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 83 | |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 84 | switch (mask) { |
Jonathan Cameron | fbaff21 | 2012-04-15 17:41:20 +0100 | [diff] [blame] | 85 | case IIO_CHAN_INFO_RAW: |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 86 | 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 Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 90 | 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 Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 124 | } |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 125 | |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 126 | return -EINVAL; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 127 | } |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 128 | |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 129 | static const struct iio_chan_spec adis16080_channels[] = { |
| 130 | { |
Jonathan Cameron | 41ea040 | 2011-10-05 15:27:59 +0100 | [diff] [blame] | 131 | .type = IIO_ANGL_VEL, |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 132 | .modified = 1, |
| 133 | .channel2 = IIO_MOD_Z, |
Jonathan Cameron | 89352e9 | 2013-02-27 19:29:24 +0000 | [diff] [blame] | 134 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 135 | BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 136 | .address = ADIS16080_DIN_GYRO, |
| 137 | }, { |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 138 | .type = IIO_VOLTAGE, |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 139 | .indexed = 1, |
| 140 | .channel = 0, |
Jonathan Cameron | 89352e9 | 2013-02-27 19:29:24 +0000 | [diff] [blame] | 141 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 142 | BIT(IIO_CHAN_INFO_SCALE) | |
| 143 | BIT(IIO_CHAN_INFO_OFFSET), |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 144 | .address = ADIS16080_DIN_AIN1, |
| 145 | }, { |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 146 | .type = IIO_VOLTAGE, |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 147 | .indexed = 1, |
| 148 | .channel = 1, |
Jonathan Cameron | 89352e9 | 2013-02-27 19:29:24 +0000 | [diff] [blame] | 149 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 150 | BIT(IIO_CHAN_INFO_SCALE) | |
| 151 | BIT(IIO_CHAN_INFO_OFFSET), |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 152 | .address = ADIS16080_DIN_AIN2, |
| 153 | }, { |
| 154 | .type = IIO_TEMP, |
| 155 | .indexed = 1, |
| 156 | .channel = 0, |
Jonathan Cameron | 89352e9 | 2013-02-27 19:29:24 +0000 | [diff] [blame] | 157 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 158 | BIT(IIO_CHAN_INFO_SCALE) | |
| 159 | BIT(IIO_CHAN_INFO_OFFSET), |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 160 | .address = ADIS16080_DIN_TEMP, |
| 161 | } |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 162 | }; |
| 163 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 164 | static const struct iio_info adis16080_info = { |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 165 | .read_raw = &adis16080_read_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 166 | .driver_module = THIS_MODULE, |
| 167 | }; |
| 168 | |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 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 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 187 | static int adis16080_probe(struct spi_device *spi) |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 188 | { |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 189 | const struct spi_device_id *id = spi_get_device_id(spi); |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 190 | struct adis16080_state *st; |
| 191 | struct iio_dev *indio_dev; |
| 192 | |
| 193 | /* setup the industrialio driver allocated elements */ |
Sachin Kamat | da41946 | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 194 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
| 195 | if (!indio_dev) |
| 196 | return -ENOMEM; |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 197 | st = iio_priv(indio_dev); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 198 | /* this is only used for removal purposes */ |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 199 | spi_set_drvdata(spi, indio_dev); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 200 | |
| 201 | /* Allocate the comms buffers */ |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 202 | st->us = spi; |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 203 | st->info = &adis16080_chip_info[id->driver_data]; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 204 | |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 205 | indio_dev->name = spi->dev.driver->name; |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 206 | indio_dev->channels = adis16080_channels; |
| 207 | indio_dev->num_channels = ARRAY_SIZE(adis16080_channels); |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 208 | indio_dev->dev.parent = &spi->dev; |
| 209 | indio_dev->info = &adis16080_info; |
| 210 | indio_dev->modes = INDIO_DIRECT_MODE; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 211 | |
Sachin Kamat | da41946 | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 212 | return iio_device_register(indio_dev); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 215 | static int adis16080_remove(struct spi_device *spi) |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 216 | { |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 217 | iio_device_unregister(spi_get_drvdata(spi)); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Lars-Peter Clausen | c21ab70 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 221 | static const struct spi_device_id adis16080_ids[] = { |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 222 | { "adis16080", ID_ADIS16080 }, |
| 223 | { "adis16100", ID_ADIS16100 }, |
Lars-Peter Clausen | c21ab70 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 224 | {}, |
| 225 | }; |
| 226 | MODULE_DEVICE_TABLE(spi, adis16080_ids); |
| 227 | |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 228 | static struct spi_driver adis16080_driver = { |
| 229 | .driver = { |
| 230 | .name = "adis16080", |
| 231 | .owner = THIS_MODULE, |
| 232 | }, |
| 233 | .probe = adis16080_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 234 | .remove = adis16080_remove, |
Lars-Peter Clausen | c21ab70 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 235 | .id_table = adis16080_ids, |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 236 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 237 | module_spi_driver(adis16080_driver); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 238 | |
| 239 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
Jonathan Cameron | 8d016b4 | 2011-02-11 14:20:01 +0000 | [diff] [blame] | 240 | MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver"); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 241 | MODULE_LICENSE("GPL v2"); |