xref: /openbmc/linux/drivers/iio/imu/adis.c (revision e149ca29)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Common library for ADIS16XXX devices
4  *
5  * Copyright 2012 Analog Devices Inc.
6  *   Author: Lars-Peter Clausen <lars@metafoo.de>
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/mutex.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/module.h>
18 #include <asm/unaligned.h>
19 
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/imu/adis.h>
24 
25 #define ADIS_MSC_CTRL_DATA_RDY_EN	BIT(2)
26 #define ADIS_MSC_CTRL_DATA_RDY_POL_HIGH	BIT(1)
27 #define ADIS_MSC_CTRL_DATA_RDY_DIO2	BIT(0)
28 #define ADIS_GLOB_CMD_SW_RESET		BIT(7)
29 
30 /**
31  * __adis_write_reg() - write N bytes to register (unlocked version)
32  * @adis: The adis device
33  * @reg: The address of the lower of the two registers
34  * @value: The value to write to device (up to 4 bytes)
35  * @size: The size of the @value (in bytes)
36  */
37 int __adis_write_reg(struct adis *adis, unsigned int reg,
38 	unsigned int value, unsigned int size)
39 {
40 	unsigned int page = reg / ADIS_PAGE_SIZE;
41 	int ret, i;
42 	struct spi_message msg;
43 	struct spi_transfer xfers[] = {
44 		{
45 			.tx_buf = adis->tx,
46 			.bits_per_word = 8,
47 			.len = 2,
48 			.cs_change = 1,
49 			.delay.value = adis->data->write_delay,
50 			.delay.unit = SPI_DELAY_UNIT_USECS,
51 			.cs_change_delay.value = adis->data->cs_change_delay,
52 			.cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
53 		}, {
54 			.tx_buf = adis->tx + 2,
55 			.bits_per_word = 8,
56 			.len = 2,
57 			.cs_change = 1,
58 			.delay.value = adis->data->write_delay,
59 			.delay.unit = SPI_DELAY_UNIT_USECS,
60 			.cs_change_delay.value = adis->data->cs_change_delay,
61 			.cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
62 		}, {
63 			.tx_buf = adis->tx + 4,
64 			.bits_per_word = 8,
65 			.len = 2,
66 			.cs_change = 1,
67 			.delay.value = adis->data->write_delay,
68 			.delay.unit = SPI_DELAY_UNIT_USECS,
69 			.cs_change_delay.value = adis->data->cs_change_delay,
70 			.cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
71 		}, {
72 			.tx_buf = adis->tx + 6,
73 			.bits_per_word = 8,
74 			.len = 2,
75 			.delay.value = adis->data->write_delay,
76 			.delay.unit = SPI_DELAY_UNIT_USECS,
77 		}, {
78 			.tx_buf = adis->tx + 8,
79 			.bits_per_word = 8,
80 			.len = 2,
81 			.delay.value = adis->data->write_delay,
82 			.delay.unit = SPI_DELAY_UNIT_USECS,
83 		},
84 	};
85 
86 	spi_message_init(&msg);
87 
88 	if (adis->current_page != page) {
89 		adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
90 		adis->tx[1] = page;
91 		spi_message_add_tail(&xfers[0], &msg);
92 	}
93 
94 	switch (size) {
95 	case 4:
96 		adis->tx[8] = ADIS_WRITE_REG(reg + 3);
97 		adis->tx[9] = (value >> 24) & 0xff;
98 		adis->tx[6] = ADIS_WRITE_REG(reg + 2);
99 		adis->tx[7] = (value >> 16) & 0xff;
100 		/* fall through */
101 	case 2:
102 		adis->tx[4] = ADIS_WRITE_REG(reg + 1);
103 		adis->tx[5] = (value >> 8) & 0xff;
104 		/* fall through */
105 	case 1:
106 		adis->tx[2] = ADIS_WRITE_REG(reg);
107 		adis->tx[3] = value & 0xff;
108 		break;
109 	default:
110 		return -EINVAL;
111 	}
112 
113 	xfers[size].cs_change = 0;
114 
115 	for (i = 1; i <= size; i++)
116 		spi_message_add_tail(&xfers[i], &msg);
117 
118 	ret = spi_sync(adis->spi, &msg);
119 	if (ret) {
120 		dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n",
121 				reg, ret);
122 	} else {
123 		adis->current_page = page;
124 	}
125 
126 	return ret;
127 }
128 EXPORT_SYMBOL_GPL(__adis_write_reg);
129 
130 /**
131  * __adis_read_reg() - read N bytes from register (unlocked version)
132  * @adis: The adis device
133  * @reg: The address of the lower of the two registers
134  * @val: The value read back from the device
135  * @size: The size of the @val buffer
136  */
137 int __adis_read_reg(struct adis *adis, unsigned int reg,
138 	unsigned int *val, unsigned int size)
139 {
140 	unsigned int page = reg / ADIS_PAGE_SIZE;
141 	struct spi_message msg;
142 	int ret;
143 	struct spi_transfer xfers[] = {
144 		{
145 			.tx_buf = adis->tx,
146 			.bits_per_word = 8,
147 			.len = 2,
148 			.cs_change = 1,
149 			.delay.value = adis->data->write_delay,
150 			.delay.unit = SPI_DELAY_UNIT_USECS,
151 			.cs_change_delay.value = adis->data->cs_change_delay,
152 			.cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
153 		}, {
154 			.tx_buf = adis->tx + 2,
155 			.bits_per_word = 8,
156 			.len = 2,
157 			.cs_change = 1,
158 			.delay.value = adis->data->read_delay,
159 			.delay.unit = SPI_DELAY_UNIT_USECS,
160 			.cs_change_delay.value = adis->data->cs_change_delay,
161 			.cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
162 		}, {
163 			.tx_buf = adis->tx + 4,
164 			.rx_buf = adis->rx,
165 			.bits_per_word = 8,
166 			.len = 2,
167 			.cs_change = 1,
168 			.delay.value = adis->data->read_delay,
169 			.delay.unit = SPI_DELAY_UNIT_USECS,
170 			.cs_change_delay.value = adis->data->cs_change_delay,
171 			.cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
172 		}, {
173 			.rx_buf = adis->rx + 2,
174 			.bits_per_word = 8,
175 			.len = 2,
176 			.delay.value = adis->data->read_delay,
177 			.delay.unit = SPI_DELAY_UNIT_USECS,
178 		},
179 	};
180 
181 	spi_message_init(&msg);
182 
183 	if (adis->current_page != page) {
184 		adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
185 		adis->tx[1] = page;
186 		spi_message_add_tail(&xfers[0], &msg);
187 	}
188 
189 	switch (size) {
190 	case 4:
191 		adis->tx[2] = ADIS_READ_REG(reg + 2);
192 		adis->tx[3] = 0;
193 		spi_message_add_tail(&xfers[1], &msg);
194 		/* fall through */
195 	case 2:
196 		adis->tx[4] = ADIS_READ_REG(reg);
197 		adis->tx[5] = 0;
198 		spi_message_add_tail(&xfers[2], &msg);
199 		spi_message_add_tail(&xfers[3], &msg);
200 		break;
201 	default:
202 		return -EINVAL;
203 	}
204 
205 	ret = spi_sync(adis->spi, &msg);
206 	if (ret) {
207 		dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n",
208 				reg, ret);
209 		return ret;
210 	} else {
211 		adis->current_page = page;
212 	}
213 
214 	switch (size) {
215 	case 4:
216 		*val = get_unaligned_be32(adis->rx);
217 		break;
218 	case 2:
219 		*val = get_unaligned_be16(adis->rx + 2);
220 		break;
221 	}
222 
223 	return ret;
224 }
225 EXPORT_SYMBOL_GPL(__adis_read_reg);
226 
227 #ifdef CONFIG_DEBUG_FS
228 
229 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
230 	unsigned int reg, unsigned int writeval, unsigned int *readval)
231 {
232 	struct adis *adis = iio_device_get_drvdata(indio_dev);
233 
234 	if (readval) {
235 		uint16_t val16;
236 		int ret;
237 
238 		ret = adis_read_reg_16(adis, reg, &val16);
239 		if (ret == 0)
240 			*readval = val16;
241 
242 		return ret;
243 	} else {
244 		return adis_write_reg_16(adis, reg, writeval);
245 	}
246 }
247 EXPORT_SYMBOL(adis_debugfs_reg_access);
248 
249 #endif
250 
251 /**
252  * adis_enable_irq() - Enable or disable data ready IRQ
253  * @adis: The adis device
254  * @enable: Whether to enable the IRQ
255  *
256  * Returns 0 on success, negative error code otherwise
257  */
258 int adis_enable_irq(struct adis *adis, bool enable)
259 {
260 	int ret = 0;
261 	uint16_t msc;
262 
263 	mutex_lock(&adis->state_lock);
264 
265 	if (adis->data->enable_irq) {
266 		ret = adis->data->enable_irq(adis, enable);
267 		goto out_unlock;
268 	}
269 
270 	ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
271 	if (ret)
272 		goto out_unlock;
273 
274 	msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
275 	msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
276 	if (enable)
277 		msc |= ADIS_MSC_CTRL_DATA_RDY_EN;
278 	else
279 		msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
280 
281 	ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
282 
283 out_unlock:
284 	mutex_unlock(&adis->state_lock);
285 	return ret;
286 }
287 EXPORT_SYMBOL(adis_enable_irq);
288 
289 /**
290  * __adis_check_status() - Check the device for error conditions (unlocked)
291  * @adis: The adis device
292  *
293  * Returns 0 on success, a negative error code otherwise
294  */
295 int __adis_check_status(struct adis *adis)
296 {
297 	uint16_t status;
298 	int ret;
299 	int i;
300 
301 	ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
302 	if (ret)
303 		return ret;
304 
305 	status &= adis->data->status_error_mask;
306 
307 	if (status == 0)
308 		return 0;
309 
310 	for (i = 0; i < 16; ++i) {
311 		if (status & BIT(i)) {
312 			dev_err(&adis->spi->dev, "%s.\n",
313 				adis->data->status_error_msgs[i]);
314 		}
315 	}
316 
317 	return -EIO;
318 }
319 EXPORT_SYMBOL_GPL(__adis_check_status);
320 
321 /**
322  * __adis_reset() - Reset the device (unlocked version)
323  * @adis: The adis device
324  *
325  * Returns 0 on success, a negative error code otherwise
326  */
327 int __adis_reset(struct adis *adis)
328 {
329 	int ret;
330 	const struct adis_timeout *timeouts = adis->data->timeouts;
331 
332 	ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg,
333 			ADIS_GLOB_CMD_SW_RESET);
334 	if (ret) {
335 		dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
336 		return ret;
337 	}
338 
339 	msleep(timeouts->sw_reset_ms);
340 
341 	return 0;
342 }
343 EXPORT_SYMBOL_GPL(__adis_reset);
344 
345 static int adis_self_test(struct adis *adis)
346 {
347 	int ret;
348 	const struct adis_timeout *timeouts = adis->data->timeouts;
349 
350 	ret = __adis_write_reg_16(adis, adis->data->self_test_reg,
351 				  adis->data->self_test_mask);
352 	if (ret) {
353 		dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
354 			ret);
355 		return ret;
356 	}
357 
358 	msleep(timeouts->self_test_ms);
359 
360 	ret = __adis_check_status(adis);
361 
362 	if (adis->data->self_test_no_autoclear)
363 		__adis_write_reg_16(adis, adis->data->self_test_reg, 0x00);
364 
365 	return ret;
366 }
367 
368 /**
369  * __adis_initial_startup() - Device initial setup
370  * @adis: The adis device
371  *
372  * The function performs a HW reset via a reset pin that should be specified
373  * via GPIOLIB. If no pin is configured a SW reset will be performed.
374  * The RST pin for the ADIS devices should be configured as ACTIVE_LOW.
375  *
376  * After the self-test operation is performed, the function will also check
377  * that the product ID is as expected. This assumes that drivers providing
378  * 'prod_id_reg' will also provide the 'prod_id'.
379  *
380  * Returns 0 if the device is operational, a negative error code otherwise.
381  *
382  * This function should be called early on in the device initialization sequence
383  * to ensure that the device is in a sane and known state and that it is usable.
384  */
385 int __adis_initial_startup(struct adis *adis)
386 {
387 	const struct adis_timeout *timeouts = adis->data->timeouts;
388 	struct gpio_desc *gpio;
389 	uint16_t prod_id;
390 	int ret;
391 
392 	/* check if the device has rst pin low */
393 	gpio = devm_gpiod_get_optional(&adis->spi->dev, "reset", GPIOD_ASIS);
394 	if (IS_ERR(gpio))
395 		return PTR_ERR(gpio);
396 
397 	if (gpio) {
398 		gpiod_set_value_cansleep(gpio, 1);
399 		msleep(10);
400 		/* bring device out of reset */
401 		gpiod_set_value_cansleep(gpio, 0);
402 		msleep(timeouts->reset_ms);
403 	} else {
404 		ret = __adis_reset(adis);
405 		if (ret)
406 			return ret;
407 	}
408 
409 	ret = adis_self_test(adis);
410 	if (ret)
411 		return ret;
412 
413 	if (!adis->data->prod_id_reg)
414 		return 0;
415 
416 	ret = adis_read_reg_16(adis, adis->data->prod_id_reg, &prod_id);
417 	if (ret)
418 		return ret;
419 
420 	if (prod_id != adis->data->prod_id)
421 		dev_warn(&adis->spi->dev,
422 			 "Device ID(%u) and product ID(%u) do not match.",
423 			 adis->data->prod_id, prod_id);
424 
425 	return 0;
426 }
427 EXPORT_SYMBOL_GPL(__adis_initial_startup);
428 
429 /**
430  * adis_single_conversion() - Performs a single sample conversion
431  * @indio_dev: The IIO device
432  * @chan: The IIO channel
433  * @error_mask: Mask for the error bit
434  * @val: Result of the conversion
435  *
436  * Returns IIO_VAL_INT on success, a negative error code otherwise.
437  *
438  * The function performs a single conversion on a given channel and post
439  * processes the value accordingly to the channel spec. If a error_mask is given
440  * the function will check if the mask is set in the returned raw value. If it
441  * is set the function will perform a self-check. If the device does not report
442  * a error bit in the channels raw value set error_mask to 0.
443  */
444 int adis_single_conversion(struct iio_dev *indio_dev,
445 	const struct iio_chan_spec *chan, unsigned int error_mask, int *val)
446 {
447 	struct adis *adis = iio_device_get_drvdata(indio_dev);
448 	unsigned int uval;
449 	int ret;
450 
451 	mutex_lock(&adis->state_lock);
452 
453 	ret = __adis_read_reg(adis, chan->address, &uval,
454 			chan->scan_type.storagebits / 8);
455 	if (ret)
456 		goto err_unlock;
457 
458 	if (uval & error_mask) {
459 		ret = __adis_check_status(adis);
460 		if (ret)
461 			goto err_unlock;
462 	}
463 
464 	if (chan->scan_type.sign == 's')
465 		*val = sign_extend32(uval, chan->scan_type.realbits - 1);
466 	else
467 		*val = uval & ((1 << chan->scan_type.realbits) - 1);
468 
469 	ret = IIO_VAL_INT;
470 err_unlock:
471 	mutex_unlock(&adis->state_lock);
472 	return ret;
473 }
474 EXPORT_SYMBOL_GPL(adis_single_conversion);
475 
476 /**
477  * adis_init() - Initialize adis device structure
478  * @adis:	The adis device
479  * @indio_dev:	The iio device
480  * @spi:	The spi device
481  * @data:	Chip specific data
482  *
483  * Returns 0 on success, a negative error code otherwise.
484  *
485  * This function must be called, before any other adis helper function may be
486  * called.
487  */
488 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
489 	struct spi_device *spi, const struct adis_data *data)
490 {
491 	if (!data || !data->timeouts) {
492 		dev_err(&spi->dev, "No config data or timeouts not defined!\n");
493 		return -EINVAL;
494 	}
495 
496 	mutex_init(&adis->state_lock);
497 	adis->spi = spi;
498 	adis->data = data;
499 	iio_device_set_drvdata(indio_dev, adis);
500 
501 	if (data->has_paging) {
502 		/* Need to set the page before first read/write */
503 		adis->current_page = -1;
504 	} else {
505 		/* Page will always be 0 */
506 		adis->current_page = 0;
507 	}
508 
509 	return adis_enable_irq(adis, false);
510 }
511 EXPORT_SYMBOL_GPL(adis_init);
512 
513 MODULE_LICENSE("GPL");
514 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
515 MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");
516