blob: e5241f41e65e53cb174fc780ecae38e74d6128fa [file] [log] [blame]
Barry Song7a83f602010-10-27 21:44:06 -04001/*
2 * ADIS16130 Digital Output, High Precision Angular Rate Sensor driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
Barry Song7a83f602010-10-27 21:44:06 -04009#include <linux/mutex.h>
Barry Song7a83f602010-10-27 21:44:06 -040010#include <linux/kernel.h>
11#include <linux/spi/spi.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040012#include <linux/module.h>
Barry Song7a83f602010-10-27 21:44:06 -040013
Jonathan Cameron06458e22012-04-25 15:54:58 +010014#include <linux/iio/iio.h>
Barry Song7a83f602010-10-27 21:44:06 -040015
Jonathan Cameron8e678752011-02-26 17:30:18 +000016#define ADIS16130_CON 0x0
17#define ADIS16130_CON_RD (1 << 6)
18#define ADIS16130_IOP 0x1
19
20/* 1 = data-ready signal low when unread data on all channels; */
21#define ADIS16130_IOP_ALL_RDY (1 << 3)
22#define ADIS16130_IOP_SYNC (1 << 0) /* 1 = synchronization enabled */
23#define ADIS16130_RATEDATA 0x8 /* Gyroscope output, rate of rotation */
24#define ADIS16130_TEMPDATA 0xA /* Temperature output */
25#define ADIS16130_RATECS 0x28 /* Gyroscope channel setup */
26#define ADIS16130_RATECS_EN (1 << 3) /* 1 = channel enable; */
27#define ADIS16130_TEMPCS 0x2A /* Temperature channel setup */
28#define ADIS16130_TEMPCS_EN (1 << 3)
29#define ADIS16130_RATECONV 0x30
30#define ADIS16130_TEMPCONV 0x32
31#define ADIS16130_MODE 0x38
32#define ADIS16130_MODE_24BIT (1 << 1) /* 1 = 24-bit resolution; */
33
34/**
35 * struct adis16130_state - device instance specific data
36 * @us: actual spi_device to write data
Jonathan Cameron8e678752011-02-26 17:30:18 +000037 * @buf_lock: mutex to protect tx and rx
38 * @buf: unified tx/rx buffer
39 **/
40struct adis16130_state {
41 struct spi_device *us;
Jonathan Cameron8e678752011-02-26 17:30:18 +000042 struct mutex buf_lock;
43 u8 buf[4] ____cacheline_aligned;
44};
Barry Song7a83f602010-10-27 21:44:06 -040045
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +010046static int adis16130_spi_read(struct iio_dev *indio_dev, u8 reg_addr, u32 *val)
Barry Song7a83f602010-10-27 21:44:06 -040047{
48 int ret;
Jonathan Cameron9f8632d2011-06-27 13:07:45 +010049 struct adis16130_state *st = iio_priv(indio_dev);
Jonathan Cameroncdf6e812011-08-12 16:55:28 +010050 struct spi_transfer xfer = {
51 .tx_buf = st->buf,
52 .rx_buf = st->buf,
Jonathan Camerona5e73632011-08-12 17:08:41 +010053 .len = 4,
Jonathan Cameroncdf6e812011-08-12 16:55:28 +010054 };
Barry Song7a83f602010-10-27 21:44:06 -040055
56 mutex_lock(&st->buf_lock);
57
Jonathan Cameron8e678752011-02-26 17:30:18 +000058 st->buf[0] = ADIS16130_CON_RD | reg_addr;
Jonathan Cameroncdf6e812011-08-12 16:55:28 +010059 st->buf[1] = st->buf[2] = st->buf[3] = 0;
Barry Song7a83f602010-10-27 21:44:06 -040060
Lars-Peter Clausenf28607f2013-10-05 08:45:00 +010061 ret = spi_sync_transfer(st->us, &xfer, 1);
Jonathan Camerona5e73632011-08-12 17:08:41 +010062 if (ret == 0)
63 *val = (st->buf[1] << 16) | (st->buf[2] << 8) | st->buf[3];
Barry Song7a83f602010-10-27 21:44:06 -040064 mutex_unlock(&st->buf_lock);
65
66 return ret;
67}
68
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +010069static int adis16130_read_raw(struct iio_dev *indio_dev,
70 struct iio_chan_spec const *chan,
71 int *val, int *val2,
72 long mask)
Barry Song7a83f602010-10-27 21:44:06 -040073{
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +010074 int ret;
75 u32 temp;
Barry Song7a83f602010-10-27 21:44:06 -040076
Lars-Peter Clausen597e1a82013-06-10 15:00:00 +010077 switch (mask) {
78 case IIO_CHAN_INFO_RAW:
79 /* Take the iio_dev status lock */
80 mutex_lock(&indio_dev->mlock);
81 ret = adis16130_spi_read(indio_dev, chan->address, &temp);
82 mutex_unlock(&indio_dev->mlock);
83 if (ret)
84 return ret;
85 *val = temp;
86 return IIO_VAL_INT;
87 case IIO_CHAN_INFO_SCALE:
88 switch (chan->type) {
89 case IIO_ANGL_VEL:
90 /* 0 degree = 838860, 250 degree = 14260608 */
91 *val = 250;
92 *val2 = 336440817; /* RAD_TO_DEGREE(14260608 - 8388608) */
93 return IIO_VAL_FRACTIONAL;
94 case IIO_TEMP:
95 /* 0C = 8036283, 105C = 9516048 */
96 *val = 105000;
97 *val2 = 9516048 - 8036283;
98 return IIO_VAL_FRACTIONAL;
99 default:
100 return -EINVAL;
101 }
Lars-Peter Clausen597e1a82013-06-10 15:00:00 +0100102 case IIO_CHAN_INFO_OFFSET:
103 switch (chan->type) {
104 case IIO_ANGL_VEL:
105 *val = -8388608;
106 return IIO_VAL_INT;
107 case IIO_TEMP:
108 *val = -8036283;
109 return IIO_VAL_INT;
110 default:
111 return -EINVAL;
112 }
Lars-Peter Clausen597e1a82013-06-10 15:00:00 +0100113 }
114
115 return -EINVAL;
Barry Song7a83f602010-10-27 21:44:06 -0400116}
117
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +0100118static const struct iio_chan_spec adis16130_channels[] = {
119 {
Jonathan Cameron41ea0402011-10-05 15:27:59 +0100120 .type = IIO_ANGL_VEL,
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +0100121 .modified = 1,
122 .channel2 = IIO_MOD_Z,
Lars-Peter Clausen597e1a82013-06-10 15:00:00 +0100123 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
124 BIT(IIO_CHAN_INFO_SCALE) |
125 BIT(IIO_CHAN_INFO_OFFSET),
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +0100126 .address = ADIS16130_RATEDATA,
127 }, {
128 .type = IIO_TEMP,
129 .indexed = 1,
130 .channel = 0,
Lars-Peter Clausen597e1a82013-06-10 15:00:00 +0100131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
132 BIT(IIO_CHAN_INFO_SCALE) |
133 BIT(IIO_CHAN_INFO_OFFSET),
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +0100134 .address = ADIS16130_TEMPDATA,
135 }
Barry Song7a83f602010-10-27 21:44:06 -0400136};
137
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100138static const struct iio_info adis16130_info = {
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +0100139 .read_raw = &adis16130_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100140 .driver_module = THIS_MODULE,
141};
142
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500143static int adis16130_probe(struct spi_device *spi)
Barry Song7a83f602010-10-27 21:44:06 -0400144{
Jonathan Cameron9f8632d2011-06-27 13:07:45 +0100145 struct adis16130_state *st;
146 struct iio_dev *indio_dev;
147
148 /* setup the industrialio driver allocated elements */
Sachin Kamat42aceff2013-08-13 07:34:00 +0100149 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
150 if (!indio_dev)
151 return -ENOMEM;
Jonathan Cameron9f8632d2011-06-27 13:07:45 +0100152 st = iio_priv(indio_dev);
Barry Song7a83f602010-10-27 21:44:06 -0400153 /* this is only used for removal purposes */
Jonathan Cameron9f8632d2011-06-27 13:07:45 +0100154 spi_set_drvdata(spi, indio_dev);
Barry Song7a83f602010-10-27 21:44:06 -0400155 st->us = spi;
156 mutex_init(&st->buf_lock);
Jonathan Cameron9f8632d2011-06-27 13:07:45 +0100157 indio_dev->name = spi->dev.driver->name;
Jonathan Cameronae0b4bdd2011-08-12 17:47:59 +0100158 indio_dev->channels = adis16130_channels;
159 indio_dev->num_channels = ARRAY_SIZE(adis16130_channels);
Jonathan Cameron9f8632d2011-06-27 13:07:45 +0100160 indio_dev->dev.parent = &spi->dev;
161 indio_dev->info = &adis16130_info;
162 indio_dev->modes = INDIO_DIRECT_MODE;
Barry Song7a83f602010-10-27 21:44:06 -0400163
Sachin Kamat79788b72013-10-29 11:39:00 +0000164 return devm_iio_device_register(&spi->dev, indio_dev);
Barry Song7a83f602010-10-27 21:44:06 -0400165}
166
167static struct spi_driver adis16130_driver = {
168 .driver = {
169 .name = "adis16130",
Barry Song7a83f602010-10-27 21:44:06 -0400170 },
171 .probe = adis16130_probe,
Barry Song7a83f602010-10-27 21:44:06 -0400172};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100173module_spi_driver(adis16130_driver);
Barry Song7a83f602010-10-27 21:44:06 -0400174
175MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
Jonathan Cameron8e678752011-02-26 17:30:18 +0000176MODULE_DESCRIPTION("Analog Devices ADIS16130 High Precision Angular Rate");
Barry Song7a83f602010-10-27 21:44:06 -0400177MODULE_LICENSE("GPL v2");
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100178MODULE_ALIAS("spi:adis16130");