2 * ADIS16130 Digital Output, High Precision Angular Rate Sensor driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/mutex.h>
10 #include <linux/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/module.h>
14 #include <linux/iio/iio.h>
16 #define ADIS16130_CON 0x0
17 #define ADIS16130_CON_RD (1 << 6)
18 #define ADIS16130_IOP 0x1
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; */
35 * struct adis16130_state - device instance specific data
36 * @us: actual spi_device to write data
37 * @buf_lock: mutex to protect tx and rx
38 * @buf: unified tx/rx buffer
40 struct adis16130_state
{
41 struct spi_device
*us
;
42 struct mutex buf_lock
;
43 u8 buf
[4] ____cacheline_aligned
;
46 static int adis16130_spi_read(struct iio_dev
*indio_dev
, u8 reg_addr
, u32
*val
)
49 struct adis16130_state
*st
= iio_priv(indio_dev
);
50 struct spi_transfer xfer
= {
56 mutex_lock(&st
->buf_lock
);
58 st
->buf
[0] = ADIS16130_CON_RD
| reg_addr
;
59 st
->buf
[1] = st
->buf
[2] = st
->buf
[3] = 0;
61 ret
= spi_sync_transfer(st
->us
, &xfer
, 1);
63 *val
= (st
->buf
[1] << 16) | (st
->buf
[2] << 8) | st
->buf
[3];
64 mutex_unlock(&st
->buf_lock
);
69 static int adis16130_read_raw(struct iio_dev
*indio_dev
,
70 struct iio_chan_spec
const *chan
,
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
);
87 case IIO_CHAN_INFO_SCALE
:
90 /* 0 degree = 838860, 250 degree = 14260608 */
92 *val2
= 336440817; /* RAD_TO_DEGREE(14260608 - 8388608) */
93 return IIO_VAL_FRACTIONAL
;
95 /* 0C = 8036283, 105C = 9516048 */
97 *val2
= 9516048 - 8036283;
98 return IIO_VAL_FRACTIONAL
;
102 case IIO_CHAN_INFO_OFFSET
:
103 switch (chan
->type
) {
118 static const struct iio_chan_spec adis16130_channels
[] = {
120 .type
= IIO_ANGL_VEL
,
122 .channel2
= IIO_MOD_Z
,
123 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
124 BIT(IIO_CHAN_INFO_SCALE
) |
125 BIT(IIO_CHAN_INFO_OFFSET
),
126 .address
= ADIS16130_RATEDATA
,
131 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
132 BIT(IIO_CHAN_INFO_SCALE
) |
133 BIT(IIO_CHAN_INFO_OFFSET
),
134 .address
= ADIS16130_TEMPDATA
,
138 static const struct iio_info adis16130_info
= {
139 .read_raw
= &adis16130_read_raw
,
140 .driver_module
= THIS_MODULE
,
143 static int adis16130_probe(struct spi_device
*spi
)
145 struct adis16130_state
*st
;
146 struct iio_dev
*indio_dev
;
148 /* setup the industrialio driver allocated elements */
149 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
152 st
= iio_priv(indio_dev
);
153 /* this is only used for removal purposes */
154 spi_set_drvdata(spi
, indio_dev
);
156 mutex_init(&st
->buf_lock
);
157 indio_dev
->name
= spi
->dev
.driver
->name
;
158 indio_dev
->channels
= adis16130_channels
;
159 indio_dev
->num_channels
= ARRAY_SIZE(adis16130_channels
);
160 indio_dev
->dev
.parent
= &spi
->dev
;
161 indio_dev
->info
= &adis16130_info
;
162 indio_dev
->modes
= INDIO_DIRECT_MODE
;
164 return iio_device_register(indio_dev
);
167 static int adis16130_remove(struct spi_device
*spi
)
169 iio_device_unregister(spi_get_drvdata(spi
));
173 static struct spi_driver adis16130_driver
= {
176 .owner
= THIS_MODULE
,
178 .probe
= adis16130_probe
,
179 .remove
= adis16130_remove
,
181 module_spi_driver(adis16130_driver
);
183 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
184 MODULE_DESCRIPTION("Analog Devices ADIS16130 High Precision Angular Rate");
185 MODULE_LICENSE("GPL v2");
186 MODULE_ALIAS("spi:adis16130");