Commit | Line | Data |
---|---|---|
80503b23 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
7a83f60d BS |
2 | /* |
3 | * ADIS16130 Digital Output, High Precision Angular Rate Sensor driver | |
4 | * | |
5 | * Copyright 2010 Analog Devices Inc. | |
7a83f60d BS |
6 | */ |
7 | ||
7a83f60d | 8 | #include <linux/mutex.h> |
7a83f60d BS |
9 | #include <linux/kernel.h> |
10 | #include <linux/spi/spi.h> | |
99c97852 | 11 | #include <linux/module.h> |
7a83f60d | 12 | |
06458e27 | 13 | #include <linux/iio/iio.h> |
7a83f60d | 14 | |
e33ff8ac AS |
15 | #include <asm/unaligned.h> |
16 | ||
8e678751 JC |
17 | #define ADIS16130_CON 0x0 |
18 | #define ADIS16130_CON_RD (1 << 6) | |
19 | #define ADIS16130_IOP 0x1 | |
20 | ||
21 | /* 1 = data-ready signal low when unread data on all channels; */ | |
22 | #define ADIS16130_IOP_ALL_RDY (1 << 3) | |
23 | #define ADIS16130_IOP_SYNC (1 << 0) /* 1 = synchronization enabled */ | |
24 | #define ADIS16130_RATEDATA 0x8 /* Gyroscope output, rate of rotation */ | |
25 | #define ADIS16130_TEMPDATA 0xA /* Temperature output */ | |
26 | #define ADIS16130_RATECS 0x28 /* Gyroscope channel setup */ | |
27 | #define ADIS16130_RATECS_EN (1 << 3) /* 1 = channel enable; */ | |
28 | #define ADIS16130_TEMPCS 0x2A /* Temperature channel setup */ | |
29 | #define ADIS16130_TEMPCS_EN (1 << 3) | |
30 | #define ADIS16130_RATECONV 0x30 | |
31 | #define ADIS16130_TEMPCONV 0x32 | |
32 | #define ADIS16130_MODE 0x38 | |
33 | #define ADIS16130_MODE_24BIT (1 << 1) /* 1 = 24-bit resolution; */ | |
34 | ||
35 | /** | |
36 | * struct adis16130_state - device instance specific data | |
37 | * @us: actual spi_device to write data | |
8e678751 JC |
38 | * @buf_lock: mutex to protect tx and rx |
39 | * @buf: unified tx/rx buffer | |
40 | **/ | |
41 | struct adis16130_state { | |
42 | struct spi_device *us; | |
8e678751 | 43 | struct mutex buf_lock; |
ff3211b2 | 44 | u8 buf[4] __aligned(IIO_DMA_MINALIGN); |
8e678751 | 45 | }; |
7a83f60d | 46 | |
ae0b4bdd | 47 | static int adis16130_spi_read(struct iio_dev *indio_dev, u8 reg_addr, u32 *val) |
7a83f60d BS |
48 | { |
49 | int ret; | |
9f8632d7 | 50 | struct adis16130_state *st = iio_priv(indio_dev); |
cdf6e817 JC |
51 | struct spi_transfer xfer = { |
52 | .tx_buf = st->buf, | |
53 | .rx_buf = st->buf, | |
a5e7363c | 54 | .len = 4, |
cdf6e817 | 55 | }; |
7a83f60d BS |
56 | |
57 | mutex_lock(&st->buf_lock); | |
58 | ||
8e678751 | 59 | st->buf[0] = ADIS16130_CON_RD | reg_addr; |
cdf6e817 JC |
60 | st->buf[1] = st->buf[2] = st->buf[3] = 0; |
61 | ||
f28607f3 | 62 | ret = spi_sync_transfer(st->us, &xfer, 1); |
a5e7363c | 63 | if (ret == 0) |
e33ff8ac | 64 | *val = get_unaligned_be24(&st->buf[1]); |
7a83f60d BS |
65 | mutex_unlock(&st->buf_lock); |
66 | ||
67 | return ret; | |
68 | } | |
69 | ||
ae0b4bdd JC |
70 | static int adis16130_read_raw(struct iio_dev *indio_dev, |
71 | struct iio_chan_spec const *chan, | |
72 | int *val, int *val2, | |
73 | long mask) | |
7a83f60d | 74 | { |
ae0b4bdd JC |
75 | int ret; |
76 | u32 temp; | |
7a83f60d | 77 | |
597e1a86 LPC |
78 | switch (mask) { |
79 | case IIO_CHAN_INFO_RAW: | |
80 | /* Take the iio_dev status lock */ | |
597e1a86 | 81 | ret = adis16130_spi_read(indio_dev, chan->address, &temp); |
597e1a86 LPC |
82 | if (ret) |
83 | return ret; | |
84 | *val = temp; | |
85 | return IIO_VAL_INT; | |
86 | case IIO_CHAN_INFO_SCALE: | |
87 | switch (chan->type) { | |
88 | case IIO_ANGL_VEL: | |
89 | /* 0 degree = 838860, 250 degree = 14260608 */ | |
90 | *val = 250; | |
91 | *val2 = 336440817; /* RAD_TO_DEGREE(14260608 - 8388608) */ | |
92 | return IIO_VAL_FRACTIONAL; | |
93 | case IIO_TEMP: | |
94 | /* 0C = 8036283, 105C = 9516048 */ | |
95 | *val = 105000; | |
96 | *val2 = 9516048 - 8036283; | |
97 | return IIO_VAL_FRACTIONAL; | |
98 | default: | |
99 | return -EINVAL; | |
100 | } | |
597e1a86 LPC |
101 | case IIO_CHAN_INFO_OFFSET: |
102 | switch (chan->type) { | |
103 | case IIO_ANGL_VEL: | |
104 | *val = -8388608; | |
105 | return IIO_VAL_INT; | |
106 | case IIO_TEMP: | |
107 | *val = -8036283; | |
108 | return IIO_VAL_INT; | |
109 | default: | |
110 | return -EINVAL; | |
111 | } | |
597e1a86 LPC |
112 | } |
113 | ||
114 | return -EINVAL; | |
7a83f60d BS |
115 | } |
116 | ||
ae0b4bdd JC |
117 | static const struct iio_chan_spec adis16130_channels[] = { |
118 | { | |
41ea040c | 119 | .type = IIO_ANGL_VEL, |
ae0b4bdd JC |
120 | .modified = 1, |
121 | .channel2 = IIO_MOD_Z, | |
597e1a86 LPC |
122 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
123 | BIT(IIO_CHAN_INFO_SCALE) | | |
124 | BIT(IIO_CHAN_INFO_OFFSET), | |
ae0b4bdd JC |
125 | .address = ADIS16130_RATEDATA, |
126 | }, { | |
127 | .type = IIO_TEMP, | |
128 | .indexed = 1, | |
129 | .channel = 0, | |
597e1a86 LPC |
130 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
131 | BIT(IIO_CHAN_INFO_SCALE) | | |
132 | BIT(IIO_CHAN_INFO_OFFSET), | |
ae0b4bdd JC |
133 | .address = ADIS16130_TEMPDATA, |
134 | } | |
7a83f60d BS |
135 | }; |
136 | ||
6fe8135f | 137 | static const struct iio_info adis16130_info = { |
ae0b4bdd | 138 | .read_raw = &adis16130_read_raw, |
6fe8135f JC |
139 | }; |
140 | ||
4ae1c61f | 141 | static int adis16130_probe(struct spi_device *spi) |
7a83f60d | 142 | { |
9f8632d7 JC |
143 | struct adis16130_state *st; |
144 | struct iio_dev *indio_dev; | |
145 | ||
146 | /* setup the industrialio driver allocated elements */ | |
42aceff5 SK |
147 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
148 | if (!indio_dev) | |
149 | return -ENOMEM; | |
9f8632d7 | 150 | st = iio_priv(indio_dev); |
7a83f60d | 151 | /* this is only used for removal purposes */ |
9f8632d7 | 152 | spi_set_drvdata(spi, indio_dev); |
7a83f60d BS |
153 | st->us = spi; |
154 | mutex_init(&st->buf_lock); | |
9f8632d7 | 155 | indio_dev->name = spi->dev.driver->name; |
ae0b4bdd JC |
156 | indio_dev->channels = adis16130_channels; |
157 | indio_dev->num_channels = ARRAY_SIZE(adis16130_channels); | |
9f8632d7 JC |
158 | indio_dev->info = &adis16130_info; |
159 | indio_dev->modes = INDIO_DIRECT_MODE; | |
7a83f60d | 160 | |
79788b7c | 161 | return devm_iio_device_register(&spi->dev, indio_dev); |
7a83f60d BS |
162 | } |
163 | ||
164 | static struct spi_driver adis16130_driver = { | |
165 | .driver = { | |
166 | .name = "adis16130", | |
7a83f60d BS |
167 | }, |
168 | .probe = adis16130_probe, | |
7a83f60d | 169 | }; |
ae6ae6fe | 170 | module_spi_driver(adis16130_driver); |
7a83f60d BS |
171 | |
172 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | |
8e678751 | 173 | MODULE_DESCRIPTION("Analog Devices ADIS16130 High Precision Angular Rate"); |
7a83f60d | 174 | MODULE_LICENSE("GPL v2"); |
55e4390c | 175 | MODULE_ALIAS("spi:adis16130"); |