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 | 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 Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 66 | |
Lars-Peter Clausen | 3c80372 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 67 | st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 68 | |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 69 | spi_message_init(&m); |
| 70 | spi_message_add_tail(&t[0], &m); |
| 71 | spi_message_add_tail(&t[1], &m); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 72 | |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 73 | ret = spi_sync(st->us, &m); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 74 | if (ret == 0) |
Lars-Peter Clausen | 3c80372 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 75 | *val = sign_extend32(be16_to_cpu(st->buf), 11); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 80 | static 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 Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 85 | { |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 86 | struct adis16080_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 87 | int ret; |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 88 | |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 89 | switch (mask) { |
Jonathan Cameron | fbaff21 | 2012-04-15 17:41:20 +0100 | [diff] [blame] | 90 | case IIO_CHAN_INFO_RAW: |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 91 | 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 Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 95 | 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 Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 129 | } |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 130 | |
Lars-Peter Clausen | 9ab82f0 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 131 | return -EINVAL; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 132 | } |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 133 | |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 134 | static const struct iio_chan_spec adis16080_channels[] = { |
| 135 | { |
Jonathan Cameron | 41ea040 | 2011-10-05 15:27:59 +0100 | [diff] [blame] | 136 | .type = IIO_ANGL_VEL, |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 137 | .modified = 1, |
| 138 | .channel2 = IIO_MOD_Z, |
Jonathan Cameron | 89352e9 | 2013-02-27 19:29:24 +0000 | [diff] [blame] | 139 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 140 | BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 141 | .address = ADIS16080_DIN_GYRO, |
| 142 | }, { |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 143 | .type = IIO_VOLTAGE, |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 144 | .indexed = 1, |
| 145 | .channel = 0, |
Jonathan Cameron | 89352e9 | 2013-02-27 19:29:24 +0000 | [diff] [blame] | 146 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 147 | BIT(IIO_CHAN_INFO_SCALE) | |
| 148 | BIT(IIO_CHAN_INFO_OFFSET), |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 149 | .address = ADIS16080_DIN_AIN1, |
| 150 | }, { |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 151 | .type = IIO_VOLTAGE, |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 152 | .indexed = 1, |
| 153 | .channel = 1, |
Jonathan Cameron | 89352e9 | 2013-02-27 19:29:24 +0000 | [diff] [blame] | 154 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 155 | BIT(IIO_CHAN_INFO_SCALE) | |
| 156 | BIT(IIO_CHAN_INFO_OFFSET), |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 157 | .address = ADIS16080_DIN_AIN2, |
| 158 | }, { |
| 159 | .type = IIO_TEMP, |
| 160 | .indexed = 1, |
| 161 | .channel = 0, |
Jonathan Cameron | 89352e9 | 2013-02-27 19:29:24 +0000 | [diff] [blame] | 162 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 163 | BIT(IIO_CHAN_INFO_SCALE) | |
| 164 | BIT(IIO_CHAN_INFO_OFFSET), |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 165 | .address = ADIS16080_DIN_TEMP, |
| 166 | } |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 167 | }; |
| 168 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 169 | static const struct iio_info adis16080_info = { |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 170 | .read_raw = &adis16080_read_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 171 | .driver_module = THIS_MODULE, |
| 172 | }; |
| 173 | |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 174 | enum { |
| 175 | ID_ADIS16080, |
| 176 | ID_ADIS16100, |
| 177 | }; |
| 178 | |
| 179 | static 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 Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 192 | static int adis16080_probe(struct spi_device *spi) |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 193 | { |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 194 | const struct spi_device_id *id = spi_get_device_id(spi); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 195 | int ret; |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 196 | struct adis16080_state *st; |
| 197 | struct iio_dev *indio_dev; |
| 198 | |
| 199 | /* setup the industrialio driver allocated elements */ |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 200 | indio_dev = iio_device_alloc(sizeof(*st)); |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 201 | if (indio_dev == NULL) { |
| 202 | ret = -ENOMEM; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 203 | goto error_ret; |
| 204 | } |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 205 | st = iio_priv(indio_dev); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 206 | /* this is only used for removal purposes */ |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 207 | spi_set_drvdata(spi, indio_dev); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 208 | |
| 209 | /* Allocate the comms buffers */ |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 210 | st->us = spi; |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 211 | st->info = &adis16080_chip_info[id->driver_data]; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 212 | |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 213 | indio_dev->name = spi->dev.driver->name; |
Jonathan Cameron | 584c81f | 2011-08-12 17:47:57 +0100 | [diff] [blame] | 214 | indio_dev->channels = adis16080_channels; |
| 215 | indio_dev->num_channels = ARRAY_SIZE(adis16080_channels); |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 216 | indio_dev->dev.parent = &spi->dev; |
| 217 | indio_dev->info = &adis16080_info; |
| 218 | indio_dev->modes = INDIO_DIRECT_MODE; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 219 | |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 220 | ret = iio_device_register(indio_dev); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 221 | if (ret) |
Jonathan Cameron | 8d016b4 | 2011-02-11 14:20:01 +0000 | [diff] [blame] | 222 | goto error_free_dev; |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 223 | return 0; |
| 224 | |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 225 | error_free_dev: |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 226 | iio_device_free(indio_dev); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 227 | error_ret: |
| 228 | return ret; |
| 229 | } |
| 230 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 231 | static int adis16080_remove(struct spi_device *spi) |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 232 | { |
Jonathan Cameron | 1dd9290 | 2011-06-27 13:07:44 +0100 | [diff] [blame] | 233 | iio_device_unregister(spi_get_drvdata(spi)); |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 234 | iio_device_free(spi_get_drvdata(spi)); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Lars-Peter Clausen | c21ab70 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 239 | static const struct spi_device_id adis16080_ids[] = { |
Lars-Peter Clausen | 668cce2 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 240 | { "adis16080", ID_ADIS16080 }, |
| 241 | { "adis16100", ID_ADIS16100 }, |
Lars-Peter Clausen | c21ab70 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 242 | {}, |
| 243 | }; |
| 244 | MODULE_DEVICE_TABLE(spi, adis16080_ids); |
| 245 | |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 246 | static struct spi_driver adis16080_driver = { |
| 247 | .driver = { |
| 248 | .name = "adis16080", |
| 249 | .owner = THIS_MODULE, |
| 250 | }, |
| 251 | .probe = adis16080_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 252 | .remove = adis16080_remove, |
Lars-Peter Clausen | c21ab70 | 2013-01-09 14:01:00 +0000 | [diff] [blame] | 253 | .id_table = adis16080_ids, |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 254 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 255 | module_spi_driver(adis16080_driver); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 256 | |
| 257 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
Jonathan Cameron | 8d016b4 | 2011-02-11 14:20:01 +0000 | [diff] [blame] | 258 | MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver"); |
Barry Song | 1b2f99e | 2010-10-27 21:44:05 -0400 | [diff] [blame] | 259 | MODULE_LICENSE("GPL v2"); |