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/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/module.h>
16 #include <asm/unaligned.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/imu/adis.h>
21 #define ADIS_MSC_CTRL_DATA_RDY_EN BIT(2)
22 #define ADIS_MSC_CTRL_DATA_RDY_POL_HIGH BIT(1)
23 #define ADIS_MSC_CTRL_DATA_RDY_DIO2 BIT(0)
24 #define ADIS_GLOB_CMD_SW_RESET BIT(7)
27 * __adis_write_reg() - write N bytes to register (unlocked version)
28 * @adis: The adis device
29 * @reg: The address of the lower of the two registers
30 * @value: The value to write to device (up to 4 bytes)
31 * @size: The size of the @value (in bytes)
33 int __adis_write_reg(struct adis *adis, unsigned int reg, unsigned int value,
36 unsigned int page = reg / ADIS_PAGE_SIZE;
38 struct spi_message msg;
39 struct spi_transfer xfers[] = {
45 .delay.value = adis->data->write_delay,
46 .delay.unit = SPI_DELAY_UNIT_USECS,
48 .tx_buf = adis->tx + 2,
52 .delay.value = adis->data->write_delay,
53 .delay.unit = SPI_DELAY_UNIT_USECS,
55 .tx_buf = adis->tx + 4,
59 .delay.value = adis->data->write_delay,
60 .delay.unit = SPI_DELAY_UNIT_USECS,
62 .tx_buf = adis->tx + 6,
65 .delay.value = adis->data->write_delay,
66 .delay.unit = SPI_DELAY_UNIT_USECS,
68 .tx_buf = adis->tx + 8,
71 .delay.value = adis->data->write_delay,
72 .delay.unit = SPI_DELAY_UNIT_USECS,
76 spi_message_init(&msg);
78 if (adis->current_page != page) {
79 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
81 spi_message_add_tail(&xfers[0], &msg);
86 adis->tx[8] = ADIS_WRITE_REG(reg + 3);
87 adis->tx[9] = (value >> 24) & 0xff;
88 adis->tx[6] = ADIS_WRITE_REG(reg + 2);
89 adis->tx[7] = (value >> 16) & 0xff;
92 adis->tx[4] = ADIS_WRITE_REG(reg + 1);
93 adis->tx[5] = (value >> 8) & 0xff;
96 adis->tx[2] = ADIS_WRITE_REG(reg);
97 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;
118 EXPORT_SYMBOL_NS_GPL(__adis_write_reg, IIO_ADISLIB);
121 * __adis_read_reg() - read N bytes from register (unlocked version)
122 * @adis: The adis device
123 * @reg: The address of the lower of the two registers
124 * @val: The value read back from the device
125 * @size: The size of the @val buffer
127 int __adis_read_reg(struct adis *adis, unsigned int reg, unsigned int *val,
130 unsigned int page = reg / ADIS_PAGE_SIZE;
131 struct spi_message msg;
133 struct spi_transfer xfers[] = {
139 .delay.value = adis->data->write_delay,
140 .delay.unit = SPI_DELAY_UNIT_USECS,
142 .tx_buf = adis->tx + 2,
146 .delay.value = adis->data->read_delay,
147 .delay.unit = SPI_DELAY_UNIT_USECS,
149 .tx_buf = adis->tx + 4,
154 .delay.value = adis->data->read_delay,
155 .delay.unit = SPI_DELAY_UNIT_USECS,
157 .rx_buf = adis->rx + 2,
160 .delay.value = adis->data->read_delay,
161 .delay.unit = SPI_DELAY_UNIT_USECS,
165 spi_message_init(&msg);
167 if (adis->current_page != page) {
168 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
170 spi_message_add_tail(&xfers[0], &msg);
175 adis->tx[2] = ADIS_READ_REG(reg + 2);
177 spi_message_add_tail(&xfers[1], &msg);
180 adis->tx[4] = ADIS_READ_REG(reg);
182 spi_message_add_tail(&xfers[2], &msg);
183 spi_message_add_tail(&xfers[3], &msg);
189 ret = spi_sync(adis->spi, &msg);
191 dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n",
196 adis->current_page = page;
200 *val = get_unaligned_be32(adis->rx);
203 *val = get_unaligned_be16(adis->rx + 2);
209 EXPORT_SYMBOL_NS_GPL(__adis_read_reg, IIO_ADISLIB);
211 * __adis_update_bits_base() - ADIS Update bits function - Unlocked version
212 * @adis: The adis device
213 * @reg: The address of the lower of the two registers
214 * @mask: Bitmask to change
215 * @val: Value to be written
216 * @size: Size of the register to update
218 * Updates the desired bits of @reg in accordance with @mask and @val.
220 int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
221 const u32 val, u8 size)
226 ret = __adis_read_reg(adis, reg, &__val, size);
230 __val = (__val & ~mask) | (val & mask);
232 return __adis_write_reg(adis, reg, __val, size);
234 EXPORT_SYMBOL_NS_GPL(__adis_update_bits_base, IIO_ADISLIB);
236 #ifdef CONFIG_DEBUG_FS
238 int adis_debugfs_reg_access(struct iio_dev *indio_dev, unsigned int reg,
239 unsigned int writeval, unsigned int *readval)
241 struct adis *adis = iio_device_get_drvdata(indio_dev);
247 ret = adis_read_reg_16(adis, reg, &val16);
254 return adis_write_reg_16(adis, reg, writeval);
256 EXPORT_SYMBOL_NS(adis_debugfs_reg_access, IIO_ADISLIB);
261 * __adis_enable_irq() - Enable or disable data ready IRQ (unlocked)
262 * @adis: The adis device
263 * @enable: Whether to enable the IRQ
265 * Returns 0 on success, negative error code otherwise
267 int __adis_enable_irq(struct adis *adis, bool enable)
272 if (adis->data->enable_irq)
273 return adis->data->enable_irq(adis, enable);
275 if (adis->data->unmasked_drdy) {
277 enable_irq(adis->spi->irq);
279 disable_irq(adis->spi->irq);
284 ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
288 msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
289 msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
291 msc |= ADIS_MSC_CTRL_DATA_RDY_EN;
293 msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
295 return __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
297 EXPORT_SYMBOL_NS(__adis_enable_irq, IIO_ADISLIB);
300 * __adis_check_status() - Check the device for error conditions (unlocked)
301 * @adis: The adis device
303 * Returns 0 on success, a negative error code otherwise
305 int __adis_check_status(struct adis *adis)
311 ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
315 status &= adis->data->status_error_mask;
320 for (i = 0; i < 16; ++i) {
321 if (status & BIT(i)) {
322 dev_err(&adis->spi->dev, "%s.\n",
323 adis->data->status_error_msgs[i]);
329 EXPORT_SYMBOL_NS_GPL(__adis_check_status, IIO_ADISLIB);
332 * __adis_reset() - Reset the device (unlocked version)
333 * @adis: The adis device
335 * Returns 0 on success, a negative error code otherwise
337 int __adis_reset(struct adis *adis)
340 const struct adis_timeout *timeouts = adis->data->timeouts;
342 ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg,
343 ADIS_GLOB_CMD_SW_RESET);
345 dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
349 msleep(timeouts->sw_reset_ms);
353 EXPORT_SYMBOL_NS_GPL(__adis_reset, IIO_ADIS_LIB);
355 static int adis_self_test(struct adis *adis)
358 const struct adis_timeout *timeouts = adis->data->timeouts;
360 ret = __adis_write_reg_16(adis, adis->data->self_test_reg,
361 adis->data->self_test_mask);
363 dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
368 msleep(timeouts->self_test_ms);
370 ret = __adis_check_status(adis);
372 if (adis->data->self_test_no_autoclear)
373 __adis_write_reg_16(adis, adis->data->self_test_reg, 0x00);
379 * __adis_initial_startup() - Device initial setup
380 * @adis: The adis device
382 * The function performs a HW reset via a reset pin that should be specified
383 * via GPIOLIB. If no pin is configured a SW reset will be performed.
384 * The RST pin for the ADIS devices should be configured as ACTIVE_LOW.
386 * After the self-test operation is performed, the function will also check
387 * that the product ID is as expected. This assumes that drivers providing
388 * 'prod_id_reg' will also provide the 'prod_id'.
390 * Returns 0 if the device is operational, a negative error code otherwise.
392 * This function should be called early on in the device initialization sequence
393 * to ensure that the device is in a sane and known state and that it is usable.
395 int __adis_initial_startup(struct adis *adis)
397 const struct adis_timeout *timeouts = adis->data->timeouts;
398 struct gpio_desc *gpio;
402 /* check if the device has rst pin low */
403 gpio = devm_gpiod_get_optional(&adis->spi->dev, "reset", GPIOD_OUT_HIGH);
405 return PTR_ERR(gpio);
408 usleep_range(10, 12);
409 /* bring device out of reset */
410 gpiod_set_value_cansleep(gpio, 0);
411 msleep(timeouts->reset_ms);
413 ret = __adis_reset(adis);
418 ret = adis_self_test(adis);
423 * don't bother calling this if we can't unmask the IRQ as in this case
424 * the IRQ is most likely not yet requested and we will request it
425 * with 'IRQF_NO_AUTOEN' anyways.
427 if (!adis->data->unmasked_drdy)
428 __adis_enable_irq(adis, false);
430 if (!adis->data->prod_id_reg)
433 ret = adis_read_reg_16(adis, adis->data->prod_id_reg, &prod_id);
437 if (prod_id != adis->data->prod_id)
438 dev_warn(&adis->spi->dev,
439 "Device ID(%u) and product ID(%u) do not match.\n",
440 adis->data->prod_id, prod_id);
444 EXPORT_SYMBOL_NS_GPL(__adis_initial_startup, IIO_ADISLIB);
447 * adis_single_conversion() - Performs a single sample conversion
448 * @indio_dev: The IIO device
449 * @chan: The IIO channel
450 * @error_mask: Mask for the error bit
451 * @val: Result of the conversion
453 * Returns IIO_VAL_INT on success, a negative error code otherwise.
455 * The function performs a single conversion on a given channel and post
456 * processes the value accordingly to the channel spec. If a error_mask is given
457 * the function will check if the mask is set in the returned raw value. If it
458 * is set the function will perform a self-check. If the device does not report
459 * a error bit in the channels raw value set error_mask to 0.
461 int adis_single_conversion(struct iio_dev *indio_dev,
462 const struct iio_chan_spec *chan,
463 unsigned int error_mask, int *val)
465 struct adis *adis = iio_device_get_drvdata(indio_dev);
469 mutex_lock(&adis->state_lock);
471 ret = __adis_read_reg(adis, chan->address, &uval,
472 chan->scan_type.storagebits / 8);
476 if (uval & error_mask) {
477 ret = __adis_check_status(adis);
482 if (chan->scan_type.sign == 's')
483 *val = sign_extend32(uval, chan->scan_type.realbits - 1);
485 *val = uval & ((1 << chan->scan_type.realbits) - 1);
489 mutex_unlock(&adis->state_lock);
492 EXPORT_SYMBOL_NS_GPL(adis_single_conversion, IIO_ADISLIB);
495 * adis_init() - Initialize adis device structure
496 * @adis: The adis device
497 * @indio_dev: The iio device
498 * @spi: The spi device
499 * @data: Chip specific data
501 * Returns 0 on success, a negative error code otherwise.
503 * This function must be called, before any other adis helper function may be
506 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
507 struct spi_device *spi, const struct adis_data *data)
509 if (!data || !data->timeouts) {
510 dev_err(&spi->dev, "No config data or timeouts not defined!\n");
514 mutex_init(&adis->state_lock);
516 if (!spi->cs_inactive.value) {
517 spi->cs_inactive.value = data->cs_change_delay;
518 spi->cs_inactive.unit = SPI_DELAY_UNIT_USECS;
523 iio_device_set_drvdata(indio_dev, adis);
525 if (data->has_paging) {
526 /* Need to set the page before first read/write */
527 adis->current_page = -1;
529 /* Page will always be 0 */
530 adis->current_page = 0;
535 EXPORT_SYMBOL_NS_GPL(adis_init, IIO_ADISLIB);
537 MODULE_LICENSE("GPL");
538 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
539 MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");