1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Common library for ADIS16XXX devices
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
9 #include <linux/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/module.h>
17 #include <asm/unaligned.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/imu/adis.h>
24 #define ADIS_MSC_CTRL_DATA_RDY_EN BIT(2)
25 #define ADIS_MSC_CTRL_DATA_RDY_POL_HIGH BIT(1)
26 #define ADIS_MSC_CTRL_DATA_RDY_DIO2 BIT(0)
27 #define ADIS_GLOB_CMD_SW_RESET BIT(7)
29 int adis_write_reg(struct adis *adis, unsigned int reg,
30 unsigned int value, unsigned int size)
32 unsigned int page = reg / ADIS_PAGE_SIZE;
34 struct spi_message msg;
35 struct spi_transfer xfers[] = {
41 .delay_usecs = adis->data->write_delay,
42 .cs_change_delay = adis->data->cs_change_delay,
43 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS,
45 .tx_buf = adis->tx + 2,
49 .delay_usecs = adis->data->write_delay,
50 .cs_change_delay = adis->data->cs_change_delay,
51 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS,
53 .tx_buf = adis->tx + 4,
57 .delay_usecs = adis->data->write_delay,
58 .cs_change_delay = adis->data->cs_change_delay,
59 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS,
61 .tx_buf = adis->tx + 6,
64 .delay_usecs = adis->data->write_delay,
66 .tx_buf = adis->tx + 8,
69 .delay_usecs = adis->data->write_delay,
73 mutex_lock(&adis->txrx_lock);
75 spi_message_init(&msg);
77 if (adis->current_page != page) {
78 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
80 spi_message_add_tail(&xfers[0], &msg);
85 adis->tx[8] = ADIS_WRITE_REG(reg + 3);
86 adis->tx[9] = (value >> 24) & 0xff;
87 adis->tx[6] = ADIS_WRITE_REG(reg + 2);
88 adis->tx[7] = (value >> 16) & 0xff;
91 adis->tx[4] = ADIS_WRITE_REG(reg + 1);
92 adis->tx[5] = (value >> 8) & 0xff;
95 adis->tx[2] = ADIS_WRITE_REG(reg);
96 adis->tx[3] = value & 0xff;
103 xfers[size].cs_change = 0;
105 for (i = 1; i <= size; i++)
106 spi_message_add_tail(&xfers[i], &msg);
108 ret = spi_sync(adis->spi, &msg);
110 dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n",
113 adis->current_page = page;
117 mutex_unlock(&adis->txrx_lock);
121 EXPORT_SYMBOL_GPL(adis_write_reg);
124 * adis_read_reg() - read 2 bytes from a 16-bit register
125 * @adis: The adis device
126 * @reg: The address of the lower of the two registers
127 * @val: The value read back from the device
129 int adis_read_reg(struct adis *adis, unsigned int reg,
130 unsigned int *val, unsigned int size)
132 unsigned int page = reg / ADIS_PAGE_SIZE;
133 struct spi_message msg;
135 struct spi_transfer xfers[] = {
141 .delay_usecs = adis->data->write_delay,
142 .cs_change_delay = adis->data->cs_change_delay,
143 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS,
145 .tx_buf = adis->tx + 2,
149 .delay_usecs = adis->data->read_delay,
150 .cs_change_delay = adis->data->cs_change_delay,
151 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS,
153 .tx_buf = adis->tx + 4,
158 .delay_usecs = adis->data->read_delay,
159 .cs_change_delay = adis->data->cs_change_delay,
160 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS,
162 .rx_buf = adis->rx + 2,
165 .delay_usecs = adis->data->read_delay,
169 mutex_lock(&adis->txrx_lock);
170 spi_message_init(&msg);
172 if (adis->current_page != page) {
173 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
175 spi_message_add_tail(&xfers[0], &msg);
180 adis->tx[2] = ADIS_READ_REG(reg + 2);
182 spi_message_add_tail(&xfers[1], &msg);
185 adis->tx[4] = ADIS_READ_REG(reg);
187 spi_message_add_tail(&xfers[2], &msg);
188 spi_message_add_tail(&xfers[3], &msg);
195 ret = spi_sync(adis->spi, &msg);
197 dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n",
201 adis->current_page = page;
206 *val = get_unaligned_be32(adis->rx);
209 *val = get_unaligned_be16(adis->rx + 2);
214 mutex_unlock(&adis->txrx_lock);
218 EXPORT_SYMBOL_GPL(adis_read_reg);
220 #ifdef CONFIG_DEBUG_FS
222 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
223 unsigned int reg, unsigned int writeval, unsigned int *readval)
225 struct adis *adis = iio_device_get_drvdata(indio_dev);
231 ret = adis_read_reg_16(adis, reg, &val16);
236 return adis_write_reg_16(adis, reg, writeval);
239 EXPORT_SYMBOL(adis_debugfs_reg_access);
244 * adis_enable_irq() - Enable or disable data ready IRQ
245 * @adis: The adis device
246 * @enable: Whether to enable the IRQ
248 * Returns 0 on success, negative error code otherwise
250 int adis_enable_irq(struct adis *adis, bool enable)
255 if (adis->data->enable_irq)
256 return adis->data->enable_irq(adis, enable);
258 ret = adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
262 msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
263 msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
265 msc |= ADIS_MSC_CTRL_DATA_RDY_EN;
267 msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
269 ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
274 EXPORT_SYMBOL(adis_enable_irq);
277 * adis_check_status() - Check the device for error conditions
278 * @adis: The adis device
280 * Returns 0 on success, a negative error code otherwise
282 int adis_check_status(struct adis *adis)
288 ret = adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
292 status &= adis->data->status_error_mask;
297 for (i = 0; i < 16; ++i) {
298 if (status & BIT(i)) {
299 dev_err(&adis->spi->dev, "%s.\n",
300 adis->data->status_error_msgs[i]);
306 EXPORT_SYMBOL_GPL(adis_check_status);
309 * adis_reset() - Reset the device
310 * @adis: The adis device
312 * Returns 0 on success, a negative error code otherwise
314 int adis_reset(struct adis *adis)
318 ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg,
319 ADIS_GLOB_CMD_SW_RESET);
321 dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
325 EXPORT_SYMBOL_GPL(adis_reset);
327 static int adis_self_test(struct adis *adis)
331 ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg,
332 adis->data->self_test_mask);
334 dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
339 msleep(adis->data->startup_delay);
341 ret = adis_check_status(adis);
343 if (adis->data->self_test_no_autoclear)
344 adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00);
350 * adis_inital_startup() - Performs device self-test
351 * @adis: The adis device
353 * Returns 0 if the device is operational, a negative error code otherwise.
355 * This function should be called early on in the device initialization sequence
356 * to ensure that the device is in a sane and known state and that it is usable.
358 int adis_initial_startup(struct adis *adis)
362 ret = adis_self_test(adis);
364 dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n");
366 msleep(adis->data->startup_delay);
367 ret = adis_self_test(adis);
369 dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n");
376 EXPORT_SYMBOL_GPL(adis_initial_startup);
379 * adis_single_conversion() - Performs a single sample conversion
380 * @indio_dev: The IIO device
381 * @chan: The IIO channel
382 * @error_mask: Mask for the error bit
383 * @val: Result of the conversion
385 * Returns IIO_VAL_INT on success, a negative error code otherwise.
387 * The function performs a single conversion on a given channel and post
388 * processes the value accordingly to the channel spec. If a error_mask is given
389 * the function will check if the mask is set in the returned raw value. If it
390 * is set the function will perform a self-check. If the device does not report
391 * a error bit in the channels raw value set error_mask to 0.
393 int adis_single_conversion(struct iio_dev *indio_dev,
394 const struct iio_chan_spec *chan, unsigned int error_mask, int *val)
396 struct adis *adis = iio_device_get_drvdata(indio_dev);
400 mutex_lock(&indio_dev->mlock);
402 ret = adis_read_reg(adis, chan->address, &uval,
403 chan->scan_type.storagebits / 8);
407 if (uval & error_mask) {
408 ret = adis_check_status(adis);
413 if (chan->scan_type.sign == 's')
414 *val = sign_extend32(uval, chan->scan_type.realbits - 1);
416 *val = uval & ((1 << chan->scan_type.realbits) - 1);
420 mutex_unlock(&indio_dev->mlock);
423 EXPORT_SYMBOL_GPL(adis_single_conversion);
426 * adis_init() - Initialize adis device structure
427 * @adis: The adis device
428 * @indio_dev: The iio device
429 * @spi: The spi device
430 * @data: Chip specific data
432 * Returns 0 on success, a negative error code otherwise.
434 * This function must be called, before any other adis helper function may be
437 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
438 struct spi_device *spi, const struct adis_data *data)
440 mutex_init(&adis->txrx_lock);
443 iio_device_set_drvdata(indio_dev, adis);
445 if (data->has_paging) {
446 /* Need to set the page before first read/write */
447 adis->current_page = -1;
449 /* Page will always be 0 */
450 adis->current_page = 0;
453 return adis_enable_irq(adis, false);
455 EXPORT_SYMBOL_GPL(adis_init);
457 MODULE_LICENSE("GPL");
458 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
459 MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");