Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 1 | /* |
Michael Hennerich | f733d02 | 2010-10-27 21:43:47 -0400 | [diff] [blame] | 2 | * ADIS16260/ADIS16265 Programmable Digital Gyroscope Sensor Driver |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2010 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/interrupt.h> |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 10 | #include <linux/mutex.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/spi/spi.h> |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 14 | #include <linux/sysfs.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [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> |
| 19 | #include <linux/iio/buffer.h> |
Lars-Peter Clausen | 988c79a | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 20 | #include <linux/iio/imu/adis.h> |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 21 | |
Lars-Peter Clausen | 988c79a | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 22 | #define ADIS16260_STARTUP_DELAY 220 /* ms */ |
| 23 | |
| 24 | #define ADIS16260_FLASH_CNT 0x00 /* Flash memory write count */ |
| 25 | #define ADIS16260_SUPPLY_OUT 0x02 /* Power supply measurement */ |
| 26 | #define ADIS16260_GYRO_OUT 0x04 /* X-axis gyroscope output */ |
| 27 | #define ADIS16260_AUX_ADC 0x0A /* analog input channel measurement */ |
| 28 | #define ADIS16260_TEMP_OUT 0x0C /* internal temperature measurement */ |
| 29 | #define ADIS16260_ANGL_OUT 0x0E /* angle displacement */ |
| 30 | #define ADIS16260_GYRO_OFF 0x14 /* Calibration, offset/bias adjustment */ |
| 31 | #define ADIS16260_GYRO_SCALE 0x16 /* Calibration, scale adjustment */ |
| 32 | #define ADIS16260_ALM_MAG1 0x20 /* Alarm 1 magnitude/polarity setting */ |
| 33 | #define ADIS16260_ALM_MAG2 0x22 /* Alarm 2 magnitude/polarity setting */ |
| 34 | #define ADIS16260_ALM_SMPL1 0x24 /* Alarm 1 dynamic rate of change setting */ |
| 35 | #define ADIS16260_ALM_SMPL2 0x26 /* Alarm 2 dynamic rate of change setting */ |
| 36 | #define ADIS16260_ALM_CTRL 0x28 /* Alarm control */ |
| 37 | #define ADIS16260_AUX_DAC 0x30 /* Auxiliary DAC data */ |
| 38 | #define ADIS16260_GPIO_CTRL 0x32 /* Control, digital I/O line */ |
| 39 | #define ADIS16260_MSC_CTRL 0x34 /* Control, data ready, self-test settings */ |
| 40 | #define ADIS16260_SMPL_PRD 0x36 /* Control, internal sample rate */ |
| 41 | #define ADIS16260_SENS_AVG 0x38 /* Control, dynamic range, filtering */ |
| 42 | #define ADIS16260_SLP_CNT 0x3A /* Control, sleep mode initiation */ |
| 43 | #define ADIS16260_DIAG_STAT 0x3C /* Diagnostic, error flags */ |
| 44 | #define ADIS16260_GLOB_CMD 0x3E /* Control, global commands */ |
| 45 | #define ADIS16260_LOT_ID1 0x52 /* Lot Identification Code 1 */ |
| 46 | #define ADIS16260_LOT_ID2 0x54 /* Lot Identification Code 2 */ |
| 47 | #define ADIS16260_PROD_ID 0x56 /* Product identifier; |
| 48 | * convert to decimal = 16,265/16,260 */ |
| 49 | #define ADIS16260_SERIAL_NUM 0x58 /* Serial number */ |
| 50 | |
| 51 | #define ADIS16260_ERROR_ACTIVE (1<<14) |
| 52 | #define ADIS16260_NEW_DATA (1<<15) |
| 53 | |
| 54 | /* MSC_CTRL */ |
| 55 | #define ADIS16260_MSC_CTRL_MEM_TEST (1<<11) |
| 56 | /* Internal self-test enable */ |
| 57 | #define ADIS16260_MSC_CTRL_INT_SELF_TEST (1<<10) |
| 58 | #define ADIS16260_MSC_CTRL_NEG_SELF_TEST (1<<9) |
| 59 | #define ADIS16260_MSC_CTRL_POS_SELF_TEST (1<<8) |
| 60 | #define ADIS16260_MSC_CTRL_DATA_RDY_EN (1<<2) |
| 61 | #define ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH (1<<1) |
| 62 | #define ADIS16260_MSC_CTRL_DATA_RDY_DIO2 (1<<0) |
| 63 | |
| 64 | /* SMPL_PRD */ |
| 65 | /* Time base (tB): 0 = 1.953 ms, 1 = 60.54 ms */ |
| 66 | #define ADIS16260_SMPL_PRD_TIME_BASE (1<<7) |
| 67 | #define ADIS16260_SMPL_PRD_DIV_MASK 0x7F |
| 68 | |
| 69 | /* SLP_CNT */ |
| 70 | #define ADIS16260_SLP_CNT_POWER_OFF 0x80 |
| 71 | |
| 72 | /* DIAG_STAT */ |
| 73 | #define ADIS16260_DIAG_STAT_ALARM2 (1<<9) |
| 74 | #define ADIS16260_DIAG_STAT_ALARM1 (1<<8) |
| 75 | #define ADIS16260_DIAG_STAT_FLASH_CHK_BIT 6 |
| 76 | #define ADIS16260_DIAG_STAT_SELF_TEST_BIT 5 |
| 77 | #define ADIS16260_DIAG_STAT_OVERFLOW_BIT 4 |
| 78 | #define ADIS16260_DIAG_STAT_SPI_FAIL_BIT 3 |
| 79 | #define ADIS16260_DIAG_STAT_FLASH_UPT_BIT 2 |
| 80 | #define ADIS16260_DIAG_STAT_POWER_HIGH_BIT 1 |
| 81 | #define ADIS16260_DIAG_STAT_POWER_LOW_BIT 0 |
| 82 | |
| 83 | /* GLOB_CMD */ |
| 84 | #define ADIS16260_GLOB_CMD_SW_RESET (1<<7) |
| 85 | #define ADIS16260_GLOB_CMD_FLASH_UPD (1<<3) |
| 86 | #define ADIS16260_GLOB_CMD_DAC_LATCH (1<<2) |
| 87 | #define ADIS16260_GLOB_CMD_FAC_CALIB (1<<1) |
| 88 | #define ADIS16260_GLOB_CMD_AUTO_NULL (1<<0) |
| 89 | |
| 90 | #define ADIS16260_SPI_SLOW (u32)(300 * 1000) |
| 91 | #define ADIS16260_SPI_BURST (u32)(1000 * 1000) |
| 92 | #define ADIS16260_SPI_FAST (u32)(2000 * 1000) |
| 93 | |
| 94 | /* At the moment triggers are only used for ring buffer |
| 95 | * filling. This may change! |
| 96 | */ |
| 97 | |
| 98 | #define ADIS16260_SCAN_GYRO 0 |
| 99 | #define ADIS16260_SCAN_SUPPLY 1 |
| 100 | #define ADIS16260_SCAN_AUX_ADC 2 |
| 101 | #define ADIS16260_SCAN_TEMP 3 |
| 102 | #define ADIS16260_SCAN_ANGL 4 |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 103 | |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 104 | static ssize_t adis16260_read_frequency(struct device *dev, |
| 105 | struct device_attribute *attr, |
| 106 | char *buf) |
| 107 | { |
Lars-Peter Clausen | f72e083 | 2012-05-12 15:39:48 +0200 | [diff] [blame] | 108 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 109 | struct adis *adis = iio_priv(indio_dev); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 110 | int ret, len = 0; |
| 111 | u16 t; |
| 112 | int sps; |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 113 | ret = adis_read_reg_16(adis, ADIS16260_SMPL_PRD, &t); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 114 | if (ret) |
| 115 | return ret; |
Jonathan Cameron | 03d1b7d | 2011-02-26 17:27:17 +0000 | [diff] [blame] | 116 | |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 117 | if (spi_get_device_id(adis->spi)->driver_data) /* If an adis16251 */ |
Lars-Peter Clausen | 8aa55d1 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 118 | sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 8 : 256; |
Jonathan Cameron | 03d1b7d | 2011-02-26 17:27:17 +0000 | [diff] [blame] | 119 | else |
Lars-Peter Clausen | 8aa55d1 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 120 | sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048; |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 121 | sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1; |
Lars-Peter Clausen | 9aa5aad | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 122 | len = sprintf(buf, "%d\n", sps); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 123 | return len; |
| 124 | } |
| 125 | |
| 126 | static ssize_t adis16260_write_frequency(struct device *dev, |
| 127 | struct device_attribute *attr, |
| 128 | const char *buf, |
| 129 | size_t len) |
| 130 | { |
Lars-Peter Clausen | f72e083 | 2012-05-12 15:39:48 +0200 | [diff] [blame] | 131 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 132 | struct adis *adis = iio_priv(indio_dev); |
Lars-Peter Clausen | e4ddf31 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 133 | unsigned int val; |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 134 | int ret; |
| 135 | u8 t; |
| 136 | |
Lars-Peter Clausen | e4ddf31 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 137 | ret = kstrtouint(buf, 10, &val); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 138 | if (ret) |
| 139 | return ret; |
| 140 | |
| 141 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | e4ddf31 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 142 | if (spi_get_device_id(adis->spi)->driver_data) |
| 143 | t = 256 / val; |
| 144 | else |
| 145 | t = 2048 / val; |
| 146 | |
| 147 | if (t > ADIS16260_SMPL_PRD_DIV_MASK) |
| 148 | t = ADIS16260_SMPL_PRD_DIV_MASK; |
| 149 | else if (t > 0) |
| 150 | t--; |
| 151 | |
| 152 | if (t >= 0x0A) |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 153 | adis->spi->max_speed_hz = ADIS16260_SPI_SLOW; |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 154 | else |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 155 | adis->spi->max_speed_hz = ADIS16260_SPI_FAST; |
| 156 | ret = adis_write_reg_8(adis, ADIS16260_SMPL_PRD, t); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 157 | |
| 158 | mutex_unlock(&indio_dev->mlock); |
| 159 | |
| 160 | return ret ? ret : len; |
| 161 | } |
| 162 | |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 163 | /* Power down the device */ |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 164 | static int adis16260_stop_device(struct iio_dev *indio_dev) |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 165 | { |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 166 | struct adis *adis = iio_priv(indio_dev); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 167 | int ret; |
| 168 | u16 val = ADIS16260_SLP_CNT_POWER_OFF; |
| 169 | |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 170 | ret = adis_write_reg_16(adis, ADIS16260_SLP_CNT, val); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 171 | if (ret) |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 172 | dev_err(&indio_dev->dev, "problem with turning device off: SLP_CNT"); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 177 | static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, |
| 178 | adis16260_read_frequency, |
| 179 | adis16260_write_frequency); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 180 | |
Lars-Peter Clausen | e1b83f4 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 181 | static const struct iio_chan_spec adis16260_channels[] = { |
| 182 | ADIS_GYRO_CHAN(X, ADIS16260_GYRO_OUT, ADIS16260_SCAN_GYRO, |
| 183 | BIT(IIO_CHAN_INFO_CALIBBIAS) | |
| 184 | BIT(IIO_CHAN_INFO_CALIBSCALE), 14), |
| 185 | ADIS_INCLI_CHAN(X, ADIS16260_ANGL_OUT, ADIS16260_SCAN_ANGL, 0, 14), |
| 186 | ADIS_TEMP_CHAN(ADIS16260_TEMP_OUT, ADIS16260_SCAN_TEMP, 12), |
| 187 | ADIS_SUPPLY_CHAN(ADIS16260_SUPPLY_OUT, ADIS16260_SCAN_SUPPLY, 12), |
| 188 | ADIS_AUX_ADC_CHAN(ADIS16260_AUX_ADC, ADIS16260_SCAN_AUX_ADC, 12), |
| 189 | IIO_CHAN_SOFT_TIMESTAMP(5), |
| 190 | }; |
Jonathan Cameron | fe34604 | 2010-11-22 23:09:48 +0000 | [diff] [blame] | 191 | |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 192 | static const u8 adis16260_addresses[][2] = { |
| 193 | [ADIS16260_SCAN_GYRO] = { ADIS16260_GYRO_OFF, ADIS16260_GYRO_SCALE }, |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 194 | }; |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 195 | |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 196 | static int adis16260_read_raw(struct iio_dev *indio_dev, |
| 197 | struct iio_chan_spec const *chan, |
| 198 | int *val, int *val2, |
| 199 | long mask) |
| 200 | { |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 201 | struct adis *adis = iio_priv(indio_dev); |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 202 | int ret; |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 203 | u8 addr; |
| 204 | s16 val16; |
Jonathan Cameron | fe34604 | 2010-11-22 23:09:48 +0000 | [diff] [blame] | 205 | |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 206 | switch (mask) { |
Jonathan Cameron | fbaff21 | 2012-04-15 17:41:20 +0100 | [diff] [blame] | 207 | case IIO_CHAN_INFO_RAW: |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 208 | return adis_single_conversion(indio_dev, chan, |
| 209 | ADIS16260_ERROR_ACTIVE, val); |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 210 | case IIO_CHAN_INFO_SCALE: |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 211 | switch (chan->type) { |
Jonathan Cameron | 41ea040 | 2011-10-05 15:27:59 +0100 | [diff] [blame] | 212 | case IIO_ANGL_VEL: |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 213 | *val = 0; |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 214 | if (spi_get_device_id(adis->spi)->driver_data) { |
Lars-Peter Clausen | 7e72a54 | 2012-10-15 10:35:00 +0100 | [diff] [blame] | 215 | /* 0.01832 degree / sec */ |
| 216 | *val2 = IIO_DEGREE_TO_RAD(18320); |
| 217 | } else { |
| 218 | /* 0.07326 degree / sec */ |
| 219 | *val2 = IIO_DEGREE_TO_RAD(73260); |
| 220 | } |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 221 | return IIO_VAL_INT_PLUS_MICRO; |
Lars-Peter Clausen | ebdee94 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 222 | case IIO_INCLI: |
| 223 | *val = 0; |
| 224 | *val2 = IIO_DEGREE_TO_RAD(36630); |
| 225 | return IIO_VAL_INT_PLUS_MICRO; |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 226 | case IIO_VOLTAGE: |
Lars-Peter Clausen | 7e72a54 | 2012-10-15 10:35:00 +0100 | [diff] [blame] | 227 | if (chan->channel == 0) { |
| 228 | *val = 1; |
| 229 | *val2 = 831500; /* 1.8315 mV */ |
| 230 | } else { |
| 231 | *val = 0; |
| 232 | *val2 = 610500; /* 610.5 uV */ |
| 233 | } |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 234 | return IIO_VAL_INT_PLUS_MICRO; |
| 235 | case IIO_TEMP: |
Lars-Peter Clausen | 7e72a54 | 2012-10-15 10:35:00 +0100 | [diff] [blame] | 236 | *val = 145; |
| 237 | *val2 = 300000; /* 0.1453 C */ |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 238 | return IIO_VAL_INT_PLUS_MICRO; |
| 239 | default: |
| 240 | return -EINVAL; |
| 241 | } |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 242 | case IIO_CHAN_INFO_OFFSET: |
Lars-Peter Clausen | 7e72a54 | 2012-10-15 10:35:00 +0100 | [diff] [blame] | 243 | *val = 250000 / 1453; /* 25 C = 0x00 */ |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 244 | return IIO_VAL_INT; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 245 | case IIO_CHAN_INFO_CALIBBIAS: |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 246 | addr = adis16260_addresses[chan->scan_index][0]; |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 247 | ret = adis_read_reg_16(adis, addr, &val16); |
Lars-Peter Clausen | 73149ba | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 248 | if (ret) |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 249 | return ret; |
Lars-Peter Clausen | 73149ba | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 250 | |
| 251 | *val = sign_extend32(val16, 11); |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 252 | return IIO_VAL_INT; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 253 | case IIO_CHAN_INFO_CALIBSCALE: |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 254 | addr = adis16260_addresses[chan->scan_index][1]; |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 255 | ret = adis_read_reg_16(adis, addr, &val16); |
Lars-Peter Clausen | 73149ba | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 256 | if (ret) |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 257 | return ret; |
Lars-Peter Clausen | 73149ba | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 258 | |
Lars-Peter Clausen | f153041 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 259 | *val = val16; |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 260 | return IIO_VAL_INT; |
| 261 | } |
| 262 | return -EINVAL; |
| 263 | } |
| 264 | |
| 265 | static int adis16260_write_raw(struct iio_dev *indio_dev, |
| 266 | struct iio_chan_spec const *chan, |
| 267 | int val, |
| 268 | int val2, |
| 269 | long mask) |
| 270 | { |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 271 | struct adis *adis = iio_priv(indio_dev); |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 272 | u8 addr; |
Lars-Peter Clausen | f4f87d0 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 273 | |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 274 | switch (mask) { |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 275 | case IIO_CHAN_INFO_CALIBBIAS: |
Lars-Peter Clausen | f4f87d0 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 276 | if (val < -2048 || val >= 2048) |
| 277 | return -EINVAL; |
| 278 | |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 279 | addr = adis16260_addresses[chan->scan_index][0]; |
Lars-Peter Clausen | f4f87d0 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 280 | return adis_write_reg_16(adis, addr, val); |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 281 | case IIO_CHAN_INFO_CALIBSCALE: |
Lars-Peter Clausen | f4f87d0 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 282 | if (val < 0 || val >= 4096) |
| 283 | return -EINVAL; |
| 284 | |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 285 | addr = adis16260_addresses[chan->scan_index][1]; |
Lars-Peter Clausen | f4f87d0 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 286 | return adis_write_reg_16(adis, addr, val); |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 287 | } |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | |
| 291 | static struct attribute *adis16260_attributes[] = { |
| 292 | &iio_dev_attr_sampling_frequency.dev_attr.attr, |
Jonathan Cameron | 29b7f43 | 2011-05-18 14:41:59 +0100 | [diff] [blame] | 293 | NULL |
| 294 | }; |
| 295 | |
| 296 | static const struct attribute_group adis16260_attribute_group = { |
| 297 | .attrs = adis16260_attributes, |
| 298 | }; |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 299 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 300 | static const struct iio_info adis16260_info = { |
| 301 | .attrs = &adis16260_attribute_group, |
| 302 | .read_raw = &adis16260_read_raw, |
| 303 | .write_raw = &adis16260_write_raw, |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 304 | .update_scan_mode = adis_update_scan_mode, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 305 | .driver_module = THIS_MODULE, |
| 306 | }; |
| 307 | |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 308 | static const char * const adis1620_status_error_msgs[] = { |
| 309 | [ADIS16260_DIAG_STAT_FLASH_CHK_BIT] = "Flash checksum error", |
| 310 | [ADIS16260_DIAG_STAT_SELF_TEST_BIT] = "Self test error", |
| 311 | [ADIS16260_DIAG_STAT_OVERFLOW_BIT] = "Sensor overrange", |
| 312 | [ADIS16260_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure", |
| 313 | [ADIS16260_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed", |
| 314 | [ADIS16260_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 5.25", |
| 315 | [ADIS16260_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 4.75", |
| 316 | }; |
| 317 | |
| 318 | static const struct adis_data adis16260_data = { |
| 319 | .write_delay = 30, |
| 320 | .read_delay = 30, |
| 321 | .msc_ctrl_reg = ADIS16260_MSC_CTRL, |
| 322 | .glob_cmd_reg = ADIS16260_GLOB_CMD, |
| 323 | .diag_stat_reg = ADIS16260_DIAG_STAT, |
| 324 | |
| 325 | .self_test_mask = ADIS16260_MSC_CTRL_MEM_TEST, |
| 326 | .startup_delay = ADIS16260_STARTUP_DELAY, |
| 327 | |
| 328 | .status_error_msgs = adis1620_status_error_msgs, |
| 329 | .status_error_mask = BIT(ADIS16260_DIAG_STAT_FLASH_CHK_BIT) | |
| 330 | BIT(ADIS16260_DIAG_STAT_SELF_TEST_BIT) | |
| 331 | BIT(ADIS16260_DIAG_STAT_OVERFLOW_BIT) | |
| 332 | BIT(ADIS16260_DIAG_STAT_SPI_FAIL_BIT) | |
| 333 | BIT(ADIS16260_DIAG_STAT_FLASH_UPT_BIT) | |
| 334 | BIT(ADIS16260_DIAG_STAT_POWER_HIGH_BIT) | |
| 335 | BIT(ADIS16260_DIAG_STAT_POWER_LOW_BIT), |
| 336 | }; |
| 337 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 338 | static int adis16260_probe(struct spi_device *spi) |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 339 | { |
Jonathan Cameron | d088ab8 | 2011-06-27 13:07:46 +0100 | [diff] [blame] | 340 | struct iio_dev *indio_dev; |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 341 | struct adis *adis; |
Lars-Peter Clausen | e1b83f4 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 342 | int ret; |
Jonathan Cameron | d088ab8 | 2011-06-27 13:07:46 +0100 | [diff] [blame] | 343 | |
| 344 | /* setup the industrialio driver allocated elements */ |
Sachin Kamat | 827b14a | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 345 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis)); |
| 346 | if (!indio_dev) |
| 347 | return -ENOMEM; |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 348 | adis = iio_priv(indio_dev); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 349 | /* this is only used for removal purposes */ |
Lars-Peter Clausen | 97271c3 | 2012-07-13 13:04:00 +0100 | [diff] [blame] | 350 | spi_set_drvdata(spi, indio_dev); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 351 | |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 352 | indio_dev->name = spi_get_device_id(spi)->name; |
Jonathan Cameron | d088ab8 | 2011-06-27 13:07:46 +0100 | [diff] [blame] | 353 | indio_dev->dev.parent = &spi->dev; |
| 354 | indio_dev->info = &adis16260_info; |
Lars-Peter Clausen | e1b83f4 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 355 | indio_dev->channels = adis16260_channels; |
| 356 | indio_dev->num_channels = ARRAY_SIZE(adis16260_channels); |
Jonathan Cameron | d088ab8 | 2011-06-27 13:07:46 +0100 | [diff] [blame] | 357 | indio_dev->modes = INDIO_DIRECT_MODE; |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 358 | |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 359 | ret = adis_init(adis, indio_dev, spi, &adis16260_data); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 360 | if (ret) |
Sachin Kamat | 827b14a | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 361 | return ret; |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 362 | |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 363 | ret = adis_setup_buffer_and_trigger(adis, indio_dev, NULL); |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 364 | if (ret) |
Sachin Kamat | 827b14a | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 365 | return ret; |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 366 | |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 367 | /* Get the device into a sane initial state */ |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 368 | ret = adis_initial_startup(adis); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 369 | if (ret) |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 370 | goto error_cleanup_buffer_trigger; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 371 | ret = iio_device_register(indio_dev); |
| 372 | if (ret) |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 373 | goto error_cleanup_buffer_trigger; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 374 | |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 375 | return 0; |
| 376 | |
Lars-Peter Clausen | 9d5e9fd | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 377 | error_cleanup_buffer_trigger: |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 378 | adis_cleanup_buffer_and_trigger(adis, indio_dev); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 379 | return ret; |
| 380 | } |
| 381 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 382 | static int adis16260_remove(struct spi_device *spi) |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 383 | { |
Jonathan Cameron | d088ab8 | 2011-06-27 13:07:46 +0100 | [diff] [blame] | 384 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 385 | struct adis *adis = iio_priv(indio_dev); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 386 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 387 | iio_device_unregister(indio_dev); |
Lars-Peter Clausen | bea3e8a | 2012-09-22 09:56:00 +0100 | [diff] [blame] | 388 | adis16260_stop_device(indio_dev); |
Lars-Peter Clausen | 6389216 | 2013-07-17 15:44:00 +0100 | [diff] [blame] | 389 | adis_cleanup_buffer_and_trigger(adis, indio_dev); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 390 | |
Lars-Peter Clausen | bea3e8a | 2012-09-22 09:56:00 +0100 | [diff] [blame] | 391 | return 0; |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Jonathan Cameron | fe34604 | 2010-11-22 23:09:48 +0000 | [diff] [blame] | 394 | /* |
| 395 | * These parts do not need to be differentiated until someone adds |
| 396 | * support for the on chip filtering. |
| 397 | */ |
Jonathan Cameron | a967295 | 2010-11-22 23:09:47 +0000 | [diff] [blame] | 398 | static const struct spi_device_id adis16260_id[] = { |
| 399 | {"adis16260", 0}, |
| 400 | {"adis16265", 0}, |
Jonathan Cameron | fe34604 | 2010-11-22 23:09:48 +0000 | [diff] [blame] | 401 | {"adis16250", 0}, |
| 402 | {"adis16255", 0}, |
Jonathan Cameron | 03d1b7d | 2011-02-26 17:27:17 +0000 | [diff] [blame] | 403 | {"adis16251", 1}, |
Jonathan Cameron | a967295 | 2010-11-22 23:09:47 +0000 | [diff] [blame] | 404 | {} |
| 405 | }; |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 406 | MODULE_DEVICE_TABLE(spi, adis16260_id); |
Jonathan Cameron | a967295 | 2010-11-22 23:09:47 +0000 | [diff] [blame] | 407 | |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 408 | static struct spi_driver adis16260_driver = { |
| 409 | .driver = { |
| 410 | .name = "adis16260", |
| 411 | .owner = THIS_MODULE, |
| 412 | }, |
| 413 | .probe = adis16260_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 414 | .remove = adis16260_remove, |
Jonathan Cameron | a967295 | 2010-11-22 23:09:47 +0000 | [diff] [blame] | 415 | .id_table = adis16260_id, |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 416 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 417 | module_spi_driver(adis16260_driver); |
Barry Song | 089a419 | 2010-05-12 19:34:06 +0100 | [diff] [blame] | 418 | |
| 419 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 420 | MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor"); |
| 421 | MODULE_LICENSE("GPL v2"); |