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