0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/mutex.h>
0009 #include <linux/kernel.h>
0010 #include <linux/spi/spi.h>
0011 #include <linux/module.h>
0012
0013 #include <linux/iio/iio.h>
0014
0015 #include <asm/unaligned.h>
0016
0017 #define ADIS16130_CON 0x0
0018 #define ADIS16130_CON_RD (1 << 6)
0019 #define ADIS16130_IOP 0x1
0020
0021
0022 #define ADIS16130_IOP_ALL_RDY (1 << 3)
0023 #define ADIS16130_IOP_SYNC (1 << 0)
0024 #define ADIS16130_RATEDATA 0x8
0025 #define ADIS16130_TEMPDATA 0xA
0026 #define ADIS16130_RATECS 0x28
0027 #define ADIS16130_RATECS_EN (1 << 3)
0028 #define ADIS16130_TEMPCS 0x2A
0029 #define ADIS16130_TEMPCS_EN (1 << 3)
0030 #define ADIS16130_RATECONV 0x30
0031 #define ADIS16130_TEMPCONV 0x32
0032 #define ADIS16130_MODE 0x38
0033 #define ADIS16130_MODE_24BIT (1 << 1)
0034
0035
0036
0037
0038
0039
0040
0041 struct adis16130_state {
0042 struct spi_device *us;
0043 struct mutex buf_lock;
0044 u8 buf[4] __aligned(IIO_DMA_MINALIGN);
0045 };
0046
0047 static int adis16130_spi_read(struct iio_dev *indio_dev, u8 reg_addr, u32 *val)
0048 {
0049 int ret;
0050 struct adis16130_state *st = iio_priv(indio_dev);
0051 struct spi_transfer xfer = {
0052 .tx_buf = st->buf,
0053 .rx_buf = st->buf,
0054 .len = 4,
0055 };
0056
0057 mutex_lock(&st->buf_lock);
0058
0059 st->buf[0] = ADIS16130_CON_RD | reg_addr;
0060 st->buf[1] = st->buf[2] = st->buf[3] = 0;
0061
0062 ret = spi_sync_transfer(st->us, &xfer, 1);
0063 if (ret == 0)
0064 *val = get_unaligned_be24(&st->buf[1]);
0065 mutex_unlock(&st->buf_lock);
0066
0067 return ret;
0068 }
0069
0070 static int adis16130_read_raw(struct iio_dev *indio_dev,
0071 struct iio_chan_spec const *chan,
0072 int *val, int *val2,
0073 long mask)
0074 {
0075 int ret;
0076 u32 temp;
0077
0078 switch (mask) {
0079 case IIO_CHAN_INFO_RAW:
0080
0081 ret = adis16130_spi_read(indio_dev, chan->address, &temp);
0082 if (ret)
0083 return ret;
0084 *val = temp;
0085 return IIO_VAL_INT;
0086 case IIO_CHAN_INFO_SCALE:
0087 switch (chan->type) {
0088 case IIO_ANGL_VEL:
0089
0090 *val = 250;
0091 *val2 = 336440817;
0092 return IIO_VAL_FRACTIONAL;
0093 case IIO_TEMP:
0094
0095 *val = 105000;
0096 *val2 = 9516048 - 8036283;
0097 return IIO_VAL_FRACTIONAL;
0098 default:
0099 return -EINVAL;
0100 }
0101 case IIO_CHAN_INFO_OFFSET:
0102 switch (chan->type) {
0103 case IIO_ANGL_VEL:
0104 *val = -8388608;
0105 return IIO_VAL_INT;
0106 case IIO_TEMP:
0107 *val = -8036283;
0108 return IIO_VAL_INT;
0109 default:
0110 return -EINVAL;
0111 }
0112 }
0113
0114 return -EINVAL;
0115 }
0116
0117 static const struct iio_chan_spec adis16130_channels[] = {
0118 {
0119 .type = IIO_ANGL_VEL,
0120 .modified = 1,
0121 .channel2 = IIO_MOD_Z,
0122 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0123 BIT(IIO_CHAN_INFO_SCALE) |
0124 BIT(IIO_CHAN_INFO_OFFSET),
0125 .address = ADIS16130_RATEDATA,
0126 }, {
0127 .type = IIO_TEMP,
0128 .indexed = 1,
0129 .channel = 0,
0130 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0131 BIT(IIO_CHAN_INFO_SCALE) |
0132 BIT(IIO_CHAN_INFO_OFFSET),
0133 .address = ADIS16130_TEMPDATA,
0134 }
0135 };
0136
0137 static const struct iio_info adis16130_info = {
0138 .read_raw = &adis16130_read_raw,
0139 };
0140
0141 static int adis16130_probe(struct spi_device *spi)
0142 {
0143 struct adis16130_state *st;
0144 struct iio_dev *indio_dev;
0145
0146
0147 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
0148 if (!indio_dev)
0149 return -ENOMEM;
0150 st = iio_priv(indio_dev);
0151
0152 spi_set_drvdata(spi, indio_dev);
0153 st->us = spi;
0154 mutex_init(&st->buf_lock);
0155 indio_dev->name = spi->dev.driver->name;
0156 indio_dev->channels = adis16130_channels;
0157 indio_dev->num_channels = ARRAY_SIZE(adis16130_channels);
0158 indio_dev->info = &adis16130_info;
0159 indio_dev->modes = INDIO_DIRECT_MODE;
0160
0161 return devm_iio_device_register(&spi->dev, indio_dev);
0162 }
0163
0164 static struct spi_driver adis16130_driver = {
0165 .driver = {
0166 .name = "adis16130",
0167 },
0168 .probe = adis16130_probe,
0169 };
0170 module_spi_driver(adis16130_driver);
0171
0172 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
0173 MODULE_DESCRIPTION("Analog Devices ADIS16130 High Precision Angular Rate");
0174 MODULE_LICENSE("GPL v2");
0175 MODULE_ALIAS("spi:adis16130");