xref: /linux/drivers/iio/imu/adis16460.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ADIS16460 IMU driver
4  *
5  * Copyright 2019 Analog Devices Inc.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/spi/spi.h>
10 
11 #include <linux/iio/iio.h>
12 #include <linux/iio/imu/adis.h>
13 
14 #include <linux/debugfs.h>
15 
16 #define ADIS16460_REG_FLASH_CNT		0x00
17 #define ADIS16460_REG_DIAG_STAT		0x02
18 #define ADIS16460_REG_X_GYRO_LOW	0x04
19 #define ADIS16460_REG_X_GYRO_OUT	0x06
20 #define ADIS16460_REG_Y_GYRO_LOW	0x08
21 #define ADIS16460_REG_Y_GYRO_OUT	0x0A
22 #define ADIS16460_REG_Z_GYRO_LOW	0x0C
23 #define ADIS16460_REG_Z_GYRO_OUT	0x0E
24 #define ADIS16460_REG_X_ACCL_LOW	0x10
25 #define ADIS16460_REG_X_ACCL_OUT	0x12
26 #define ADIS16460_REG_Y_ACCL_LOW	0x14
27 #define ADIS16460_REG_Y_ACCL_OUT	0x16
28 #define ADIS16460_REG_Z_ACCL_LOW	0x18
29 #define ADIS16460_REG_Z_ACCL_OUT	0x1A
30 #define ADIS16460_REG_SMPL_CNTR		0x1C
31 #define ADIS16460_REG_TEMP_OUT		0x1E
32 #define ADIS16460_REG_X_DELT_ANG	0x24
33 #define ADIS16460_REG_Y_DELT_ANG	0x26
34 #define ADIS16460_REG_Z_DELT_ANG	0x28
35 #define ADIS16460_REG_X_DELT_VEL	0x2A
36 #define ADIS16460_REG_Y_DELT_VEL	0x2C
37 #define ADIS16460_REG_Z_DELT_VEL	0x2E
38 #define ADIS16460_REG_MSC_CTRL		0x32
39 #define ADIS16460_REG_SYNC_SCAL		0x34
40 #define ADIS16460_REG_DEC_RATE		0x36
41 #define ADIS16460_REG_FLTR_CTRL		0x38
42 #define ADIS16460_REG_GLOB_CMD		0x3E
43 #define ADIS16460_REG_X_GYRO_OFF	0x40
44 #define ADIS16460_REG_Y_GYRO_OFF	0x42
45 #define ADIS16460_REG_Z_GYRO_OFF	0x44
46 #define ADIS16460_REG_X_ACCL_OFF	0x46
47 #define ADIS16460_REG_Y_ACCL_OFF	0x48
48 #define ADIS16460_REG_Z_ACCL_OFF	0x4A
49 #define ADIS16460_REG_LOT_ID1		0x52
50 #define ADIS16460_REG_LOT_ID2		0x54
51 #define ADIS16460_REG_PROD_ID		0x56
52 #define ADIS16460_REG_SERIAL_NUM	0x58
53 #define ADIS16460_REG_CAL_SGNTR		0x60
54 #define ADIS16460_REG_CAL_CRC		0x62
55 #define ADIS16460_REG_CODE_SGNTR	0x64
56 #define ADIS16460_REG_CODE_CRC		0x66
57 
58 struct adis16460_chip_info {
59 	unsigned int num_channels;
60 	const struct iio_chan_spec *channels;
61 	unsigned int gyro_max_val;
62 	unsigned int gyro_max_scale;
63 	unsigned int accel_max_val;
64 	unsigned int accel_max_scale;
65 };
66 
67 struct adis16460 {
68 	const struct adis16460_chip_info *chip_info;
69 	struct adis adis;
70 };
71 
adis16460_show_serial_number(void * arg,u64 * val)72 static int adis16460_show_serial_number(void *arg, u64 *val)
73 {
74 	struct adis16460 *adis16460 = arg;
75 	u16 serial;
76 	int ret;
77 
78 	ret = adis_read_reg_16(&adis16460->adis, ADIS16460_REG_SERIAL_NUM,
79 		&serial);
80 	if (ret)
81 		return ret;
82 
83 	*val = serial;
84 
85 	return 0;
86 }
87 DEFINE_DEBUGFS_ATTRIBUTE(adis16460_serial_number_fops,
88 		adis16460_show_serial_number, NULL, "0x%.4llx\n");
89 
adis16460_show_product_id(void * arg,u64 * val)90 static int adis16460_show_product_id(void *arg, u64 *val)
91 {
92 	struct adis16460 *adis16460 = arg;
93 	u16 prod_id;
94 	int ret;
95 
96 	ret = adis_read_reg_16(&adis16460->adis, ADIS16460_REG_PROD_ID,
97 		&prod_id);
98 	if (ret)
99 		return ret;
100 
101 	*val = prod_id;
102 
103 	return 0;
104 }
105 DEFINE_DEBUGFS_ATTRIBUTE(adis16460_product_id_fops,
106 		adis16460_show_product_id, NULL, "%llu\n");
107 
adis16460_show_flash_count(void * arg,u64 * val)108 static int adis16460_show_flash_count(void *arg, u64 *val)
109 {
110 	struct adis16460 *adis16460 = arg;
111 	u32 flash_count;
112 	int ret;
113 
114 	ret = adis_read_reg_32(&adis16460->adis, ADIS16460_REG_FLASH_CNT,
115 		&flash_count);
116 	if (ret)
117 		return ret;
118 
119 	*val = flash_count;
120 
121 	return 0;
122 }
123 DEFINE_DEBUGFS_ATTRIBUTE(adis16460_flash_count_fops,
124 		adis16460_show_flash_count, NULL, "%lld\n");
125 
adis16460_debugfs_init(struct iio_dev * indio_dev)126 static void adis16460_debugfs_init(struct iio_dev *indio_dev)
127 {
128 	struct adis16460 *adis16460 = iio_priv(indio_dev);
129 	struct dentry *d = iio_get_debugfs_dentry(indio_dev);
130 
131 	if (!IS_ENABLED(CONFIG_DEBUG_FS))
132 		return;
133 
134 	debugfs_create_file_unsafe("serial_number", 0400,
135 			d, adis16460, &adis16460_serial_number_fops);
136 	debugfs_create_file_unsafe("product_id", 0400,
137 			d, adis16460, &adis16460_product_id_fops);
138 	debugfs_create_file_unsafe("flash_count", 0400,
139 			d, adis16460, &adis16460_flash_count_fops);
140 }
141 
adis16460_set_freq(struct iio_dev * indio_dev,int val,int val2)142 static int adis16460_set_freq(struct iio_dev *indio_dev, int val, int val2)
143 {
144 	struct adis16460 *st = iio_priv(indio_dev);
145 	int t;
146 
147 	t =  val * 1000 + val2 / 1000;
148 	if (t <= 0)
149 		return -EINVAL;
150 
151 	t = 2048000 / t;
152 	if (t > 2048)
153 		t = 2048;
154 
155 	if (t != 0)
156 		t--;
157 
158 	return adis_write_reg_16(&st->adis, ADIS16460_REG_DEC_RATE, t);
159 }
160 
adis16460_get_freq(struct iio_dev * indio_dev,int * val,int * val2)161 static int adis16460_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
162 {
163 	struct adis16460 *st = iio_priv(indio_dev);
164 	uint16_t t;
165 	int ret;
166 	unsigned int freq;
167 
168 	ret = adis_read_reg_16(&st->adis, ADIS16460_REG_DEC_RATE, &t);
169 	if (ret)
170 		return ret;
171 
172 	freq = 2048000 / (t + 1);
173 	*val = freq / 1000;
174 	*val2 = (freq % 1000) * 1000;
175 
176 	return IIO_VAL_INT_PLUS_MICRO;
177 }
178 
adis16460_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long info)179 static int adis16460_read_raw(struct iio_dev *indio_dev,
180 	const struct iio_chan_spec *chan, int *val, int *val2, long info)
181 {
182 	struct adis16460 *st = iio_priv(indio_dev);
183 
184 	switch (info) {
185 	case IIO_CHAN_INFO_RAW:
186 		return adis_single_conversion(indio_dev, chan, 0, val);
187 	case IIO_CHAN_INFO_SCALE:
188 		switch (chan->type) {
189 		case IIO_ANGL_VEL:
190 			*val = st->chip_info->gyro_max_scale;
191 			*val2 = st->chip_info->gyro_max_val;
192 			return IIO_VAL_FRACTIONAL;
193 		case IIO_ACCEL:
194 			*val = st->chip_info->accel_max_scale;
195 			*val2 = st->chip_info->accel_max_val;
196 			return IIO_VAL_FRACTIONAL;
197 		case IIO_TEMP:
198 			*val = 50; /* 50 milli degrees Celsius/LSB */
199 			return IIO_VAL_INT;
200 		default:
201 			return -EINVAL;
202 		}
203 	case IIO_CHAN_INFO_OFFSET:
204 		*val = 500; /* 25 degrees Celsius = 0x0000 */
205 		return IIO_VAL_INT;
206 	case IIO_CHAN_INFO_SAMP_FREQ:
207 		return adis16460_get_freq(indio_dev, val, val2);
208 	default:
209 		return -EINVAL;
210 	}
211 }
212 
adis16460_write_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int val,int val2,long info)213 static int adis16460_write_raw(struct iio_dev *indio_dev,
214 	const struct iio_chan_spec *chan, int val, int val2, long info)
215 {
216 	switch (info) {
217 	case IIO_CHAN_INFO_SAMP_FREQ:
218 		return adis16460_set_freq(indio_dev, val, val2);
219 	default:
220 		return -EINVAL;
221 	}
222 }
223 
224 enum {
225 	ADIS16460_SCAN_GYRO_X,
226 	ADIS16460_SCAN_GYRO_Y,
227 	ADIS16460_SCAN_GYRO_Z,
228 	ADIS16460_SCAN_ACCEL_X,
229 	ADIS16460_SCAN_ACCEL_Y,
230 	ADIS16460_SCAN_ACCEL_Z,
231 	ADIS16460_SCAN_TEMP,
232 };
233 
234 #define ADIS16460_MOD_CHANNEL(_type, _mod, _address, _si, _bits) \
235 	{ \
236 		.type = (_type), \
237 		.modified = 1, \
238 		.channel2 = (_mod), \
239 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
240 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
241 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
242 		.address = (_address), \
243 		.scan_index = (_si), \
244 		.scan_type = { \
245 			.sign = 's', \
246 			.realbits = (_bits), \
247 			.storagebits = (_bits), \
248 			.endianness = IIO_BE, \
249 		}, \
250 	}
251 
252 #define ADIS16460_GYRO_CHANNEL(_mod) \
253 	ADIS16460_MOD_CHANNEL(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
254 	ADIS16460_REG_ ## _mod ## _GYRO_LOW, ADIS16460_SCAN_GYRO_ ## _mod, \
255 	32)
256 
257 #define ADIS16460_ACCEL_CHANNEL(_mod) \
258 	ADIS16460_MOD_CHANNEL(IIO_ACCEL, IIO_MOD_ ## _mod, \
259 	ADIS16460_REG_ ## _mod ## _ACCL_LOW, ADIS16460_SCAN_ACCEL_ ## _mod, \
260 	32)
261 
262 #define ADIS16460_TEMP_CHANNEL() { \
263 		.type = IIO_TEMP, \
264 		.indexed = 1, \
265 		.channel = 0, \
266 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
267 			BIT(IIO_CHAN_INFO_SCALE) | \
268 			BIT(IIO_CHAN_INFO_OFFSET), \
269 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
270 		.address = ADIS16460_REG_TEMP_OUT, \
271 		.scan_index = ADIS16460_SCAN_TEMP, \
272 		.scan_type = { \
273 			.sign = 's', \
274 			.realbits = 16, \
275 			.storagebits = 16, \
276 			.endianness = IIO_BE, \
277 		}, \
278 	}
279 
280 static const struct iio_chan_spec adis16460_channels[] = {
281 	ADIS16460_GYRO_CHANNEL(X),
282 	ADIS16460_GYRO_CHANNEL(Y),
283 	ADIS16460_GYRO_CHANNEL(Z),
284 	ADIS16460_ACCEL_CHANNEL(X),
285 	ADIS16460_ACCEL_CHANNEL(Y),
286 	ADIS16460_ACCEL_CHANNEL(Z),
287 	ADIS16460_TEMP_CHANNEL(),
288 	IIO_CHAN_SOFT_TIMESTAMP(7)
289 };
290 
291 static const struct adis16460_chip_info adis16460_chip_info = {
292 	.channels = adis16460_channels,
293 	.num_channels = ARRAY_SIZE(adis16460_channels),
294 	/*
295 	 * storing the value in rad/degree and the scale in degree
296 	 * gives us the result in rad and better precession than
297 	 * storing the scale directly in rad.
298 	 */
299 	.gyro_max_val = IIO_RAD_TO_DEGREE(200 << 16),
300 	.gyro_max_scale = 1,
301 	.accel_max_val = IIO_M_S_2_TO_G(20000 << 16),
302 	.accel_max_scale = 5,
303 };
304 
305 static const struct iio_info adis16460_info = {
306 	.read_raw = &adis16460_read_raw,
307 	.write_raw = &adis16460_write_raw,
308 	.update_scan_mode = adis_update_scan_mode,
309 	.debugfs_reg_access = adis_debugfs_reg_access,
310 };
311 
312 #define ADIS16460_DIAG_STAT_IN_CLK_OOS	7
313 #define ADIS16460_DIAG_STAT_FLASH_MEM	6
314 #define ADIS16460_DIAG_STAT_SELF_TEST	5
315 #define ADIS16460_DIAG_STAT_OVERRANGE	4
316 #define ADIS16460_DIAG_STAT_SPI_COMM	3
317 #define ADIS16460_DIAG_STAT_FLASH_UPT	2
318 
319 static const char * const adis16460_status_error_msgs[] = {
320 	[ADIS16460_DIAG_STAT_IN_CLK_OOS] = "Input clock out of sync",
321 	[ADIS16460_DIAG_STAT_FLASH_MEM] = "Flash memory failure",
322 	[ADIS16460_DIAG_STAT_SELF_TEST] = "Self test diagnostic failure",
323 	[ADIS16460_DIAG_STAT_OVERRANGE] = "Sensor overrange",
324 	[ADIS16460_DIAG_STAT_SPI_COMM] = "SPI communication failure",
325 	[ADIS16460_DIAG_STAT_FLASH_UPT] = "Flash update failure",
326 };
327 
328 static const struct adis_timeout adis16460_timeouts = {
329 	.reset_ms = 225,
330 	.sw_reset_ms = 225,
331 	.self_test_ms = 10,
332 };
333 
334 static const struct adis_data adis16460_data = {
335 	.diag_stat_reg = ADIS16460_REG_DIAG_STAT,
336 	.glob_cmd_reg = ADIS16460_REG_GLOB_CMD,
337 	.prod_id_reg = ADIS16460_REG_PROD_ID,
338 	.prod_id = 16460,
339 	.self_test_mask = BIT(2),
340 	.self_test_reg = ADIS16460_REG_GLOB_CMD,
341 	.has_paging = false,
342 	.read_delay = 5,
343 	.write_delay = 5,
344 	.cs_change_delay = 16,
345 	.status_error_msgs = adis16460_status_error_msgs,
346 	.status_error_mask = BIT(ADIS16460_DIAG_STAT_IN_CLK_OOS) |
347 		BIT(ADIS16460_DIAG_STAT_FLASH_MEM) |
348 		BIT(ADIS16460_DIAG_STAT_SELF_TEST) |
349 		BIT(ADIS16460_DIAG_STAT_OVERRANGE) |
350 		BIT(ADIS16460_DIAG_STAT_SPI_COMM) |
351 		BIT(ADIS16460_DIAG_STAT_FLASH_UPT),
352 	.unmasked_drdy = true,
353 	.timeouts = &adis16460_timeouts,
354 };
355 
adis16460_probe(struct spi_device * spi)356 static int adis16460_probe(struct spi_device *spi)
357 {
358 	struct iio_dev *indio_dev;
359 	struct adis16460 *st;
360 	int ret;
361 
362 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
363 	if (indio_dev == NULL)
364 		return -ENOMEM;
365 
366 	st = iio_priv(indio_dev);
367 
368 	st->chip_info = &adis16460_chip_info;
369 	indio_dev->name = spi_get_device_id(spi)->name;
370 	indio_dev->channels = st->chip_info->channels;
371 	indio_dev->num_channels = st->chip_info->num_channels;
372 	indio_dev->info = &adis16460_info;
373 	indio_dev->modes = INDIO_DIRECT_MODE;
374 
375 	ret = adis_init(&st->adis, indio_dev, spi, &adis16460_data);
376 	if (ret)
377 		return ret;
378 
379 	ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev, NULL);
380 	if (ret)
381 		return ret;
382 
383 	ret = __adis_initial_startup(&st->adis);
384 	if (ret)
385 		return ret;
386 
387 	ret = devm_iio_device_register(&spi->dev, indio_dev);
388 	if (ret)
389 		return ret;
390 
391 	adis16460_debugfs_init(indio_dev);
392 
393 	return 0;
394 }
395 
396 static const struct spi_device_id adis16460_ids[] = {
397 	{ "adis16460", 0 },
398 	{}
399 };
400 MODULE_DEVICE_TABLE(spi, adis16460_ids);
401 
402 static const struct of_device_id adis16460_of_match[] = {
403 	{ .compatible = "adi,adis16460" },
404 	{}
405 };
406 MODULE_DEVICE_TABLE(of, adis16460_of_match);
407 
408 static struct spi_driver adis16460_driver = {
409 	.driver = {
410 		.name = "adis16460",
411 		.of_match_table = adis16460_of_match,
412 	},
413 	.id_table = adis16460_ids,
414 	.probe = adis16460_probe,
415 };
416 module_spi_driver(adis16460_driver);
417 
418 MODULE_AUTHOR("Dragos Bogdan <dragos.bogdan@analog.com>");
419 MODULE_DESCRIPTION("Analog Devices ADIS16460 IMU driver");
420 MODULE_LICENSE("GPL");
421 MODULE_IMPORT_NS(IIO_ADISLIB);
422