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