1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Common library for ADIS16XXX devices
5 * Copyright 2012 Analog Devices Inc.
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,
34 unsigned int value, unsigned int size)
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,
47 .cs_change_delay.value = adis->data->cs_change_delay,
48 .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
50 .tx_buf = adis->tx + 2,
54 .delay.value = adis->data->write_delay,
55 .delay.unit = SPI_DELAY_UNIT_USECS,
56 .cs_change_delay.value = adis->data->cs_change_delay,
57 .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
59 .tx_buf = adis->tx + 4,
63 .delay.value = adis->data->write_delay,
64 .delay.unit = SPI_DELAY_UNIT_USECS,
65 .cs_change_delay.value = adis->data->cs_change_delay,
66 .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
68 .tx_buf = adis->tx + 6,
71 .delay.value = adis->data->write_delay,
72 .delay.unit = SPI_DELAY_UNIT_USECS,
74 .tx_buf = adis->tx + 8,
77 .delay.value = adis->data->write_delay,
78 .delay.unit = SPI_DELAY_UNIT_USECS,
82 spi_message_init(&msg);
84 if (adis->current_page != page) {
85 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
87 spi_message_add_tail(&xfers[0], &msg);
92 adis->tx[8] = ADIS_WRITE_REG(reg + 3);
93 adis->tx[9] = (value >> 24) & 0xff;
94 adis->tx[6] = ADIS_WRITE_REG(reg + 2);
95 adis->tx[7] = (value >> 16) & 0xff;
98 adis->tx[4] = ADIS_WRITE_REG(reg + 1);
99 adis->tx[5] = (value >> 8) & 0xff;
102 adis->tx[2] = ADIS_WRITE_REG(reg);
103 adis->tx[3] = value & 0xff;
109 xfers[size].cs_change = 0;
111 for (i = 1; i <= size; i++)
112 spi_message_add_tail(&xfers[i], &msg);
114 ret = spi_sync(adis->spi, &msg);
116 dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n",
119 adis->current_page = page;
124 EXPORT_SYMBOL_GPL(__adis_write_reg);
127 * __adis_read_reg() - read N bytes from register (unlocked version)
128 * @adis: The adis device
129 * @reg: The address of the lower of the two registers
130 * @val: The value read back from the device
131 * @size: The size of the @val buffer
133 int __adis_read_reg(struct adis *adis, unsigned int reg,
134 unsigned int *val, unsigned int size)
136 unsigned int page = reg / ADIS_PAGE_SIZE;
137 struct spi_message msg;
139 struct spi_transfer xfers[] = {
145 .delay.value = adis->data->write_delay,
146 .delay.unit = SPI_DELAY_UNIT_USECS,
147 .cs_change_delay.value = adis->data->cs_change_delay,
148 .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
150 .tx_buf = adis->tx + 2,
154 .delay.value = adis->data->read_delay,
155 .delay.unit = SPI_DELAY_UNIT_USECS,
156 .cs_change_delay.value = adis->data->cs_change_delay,
157 .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
159 .tx_buf = adis->tx + 4,
164 .delay.value = adis->data->read_delay,
165 .delay.unit = SPI_DELAY_UNIT_USECS,
166 .cs_change_delay.value = adis->data->cs_change_delay,
167 .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
169 .rx_buf = adis->rx + 2,
172 .delay.value = adis->data->read_delay,
173 .delay.unit = SPI_DELAY_UNIT_USECS,
177 spi_message_init(&msg);
179 if (adis->current_page != page) {
180 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
182 spi_message_add_tail(&xfers[0], &msg);
187 adis->tx[2] = ADIS_READ_REG(reg + 2);
189 spi_message_add_tail(&xfers[1], &msg);
192 adis->tx[4] = ADIS_READ_REG(reg);
194 spi_message_add_tail(&xfers[2], &msg);
195 spi_message_add_tail(&xfers[3], &msg);
201 ret = spi_sync(adis->spi, &msg);
203 dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n",
207 adis->current_page = page;
212 *val = get_unaligned_be32(adis->rx);
215 *val = get_unaligned_be16(adis->rx + 2);
221 EXPORT_SYMBOL_GPL(__adis_read_reg);
223 * __adis_update_bits_base() - ADIS Update bits function - Unlocked version
224 * @adis: The adis device
225 * @reg: The address of the lower of the two registers
226 * @mask: Bitmask to change
227 * @val: Value to be written
228 * @size: Size of the register to update
230 * Updates the desired bits of @reg in accordance with @mask and @val.
232 int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
233 const u32 val, u8 size)
238 ret = __adis_read_reg(adis, reg, &__val, size);
242 __val = (__val & ~mask) | (val & mask);
244 return __adis_write_reg(adis, reg, __val, size);
246 EXPORT_SYMBOL_GPL(__adis_update_bits_base);
248 #ifdef CONFIG_DEBUG_FS
250 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
251 unsigned int reg, unsigned int writeval, unsigned int *readval)
253 struct adis *adis = iio_device_get_drvdata(indio_dev);
259 ret = adis_read_reg_16(adis, reg, &val16);
265 return adis_write_reg_16(adis, reg, writeval);
268 EXPORT_SYMBOL(adis_debugfs_reg_access);
273 * adis_enable_irq() - Enable or disable data ready IRQ
274 * @adis: The adis device
275 * @enable: Whether to enable the IRQ
277 * Returns 0 on success, negative error code otherwise
279 int adis_enable_irq(struct adis *adis, bool enable)
284 mutex_lock(&adis->state_lock);
286 if (adis->data->enable_irq) {
287 ret = adis->data->enable_irq(adis, enable);
289 } else if (adis->data->unmasked_drdy) {
291 enable_irq(adis->spi->irq);
293 disable_irq(adis->spi->irq);
298 ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
302 msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
303 msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
305 msc |= ADIS_MSC_CTRL_DATA_RDY_EN;
307 msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
309 ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
312 mutex_unlock(&adis->state_lock);
315 EXPORT_SYMBOL(adis_enable_irq);
318 * __adis_check_status() - Check the device for error conditions (unlocked)
319 * @adis: The adis device
321 * Returns 0 on success, a negative error code otherwise
323 int __adis_check_status(struct adis *adis)
329 ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
333 status &= adis->data->status_error_mask;
338 for (i = 0; i < 16; ++i) {
339 if (status & BIT(i)) {
340 dev_err(&adis->spi->dev, "%s.\n",
341 adis->data->status_error_msgs[i]);
347 EXPORT_SYMBOL_GPL(__adis_check_status);
350 * __adis_reset() - Reset the device (unlocked version)
351 * @adis: The adis device
353 * Returns 0 on success, a negative error code otherwise
355 int __adis_reset(struct adis *adis)
358 const struct adis_timeout *timeouts = adis->data->timeouts;
360 ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg,
361 ADIS_GLOB_CMD_SW_RESET);
363 dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
367 msleep(timeouts->sw_reset_ms);
371 EXPORT_SYMBOL_GPL(__adis_reset);
373 static int adis_self_test(struct adis *adis)
376 const struct adis_timeout *timeouts = adis->data->timeouts;
378 ret = __adis_write_reg_16(adis, adis->data->self_test_reg,
379 adis->data->self_test_mask);
381 dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
386 msleep(timeouts->self_test_ms);
388 ret = __adis_check_status(adis);
390 if (adis->data->self_test_no_autoclear)
391 __adis_write_reg_16(adis, adis->data->self_test_reg, 0x00);
397 * __adis_initial_startup() - Device initial setup
398 * @adis: The adis device
400 * The function performs a HW reset via a reset pin that should be specified
401 * via GPIOLIB. If no pin is configured a SW reset will be performed.
402 * The RST pin for the ADIS devices should be configured as ACTIVE_LOW.
404 * After the self-test operation is performed, the function will also check
405 * that the product ID is as expected. This assumes that drivers providing
406 * 'prod_id_reg' will also provide the 'prod_id'.
408 * Returns 0 if the device is operational, a negative error code otherwise.
410 * This function should be called early on in the device initialization sequence
411 * to ensure that the device is in a sane and known state and that it is usable.
413 int __adis_initial_startup(struct adis *adis)
415 const struct adis_timeout *timeouts = adis->data->timeouts;
416 struct gpio_desc *gpio;
420 /* check if the device has rst pin low */
421 gpio = devm_gpiod_get_optional(&adis->spi->dev, "reset", GPIOD_OUT_HIGH);
423 return PTR_ERR(gpio);
427 /* bring device out of reset */
428 gpiod_set_value_cansleep(gpio, 0);
429 msleep(timeouts->reset_ms);
431 ret = __adis_reset(adis);
436 ret = adis_self_test(adis);
441 * don't bother calling this if we can't unmask the IRQ as in this case
442 * the IRQ is most likely not yet requested and we will request it
443 * with 'IRQF_NO_AUTOEN' anyways.
445 if (!adis->data->unmasked_drdy)
446 adis_enable_irq(adis, false);
448 if (!adis->data->prod_id_reg)
451 ret = adis_read_reg_16(adis, adis->data->prod_id_reg, &prod_id);
455 if (prod_id != adis->data->prod_id)
456 dev_warn(&adis->spi->dev,
457 "Device ID(%u) and product ID(%u) do not match.\n",
458 adis->data->prod_id, prod_id);
462 EXPORT_SYMBOL_GPL(__adis_initial_startup);
465 * adis_single_conversion() - Performs a single sample conversion
466 * @indio_dev: The IIO device
467 * @chan: The IIO channel
468 * @error_mask: Mask for the error bit
469 * @val: Result of the conversion
471 * Returns IIO_VAL_INT on success, a negative error code otherwise.
473 * The function performs a single conversion on a given channel and post
474 * processes the value accordingly to the channel spec. If a error_mask is given
475 * the function will check if the mask is set in the returned raw value. If it
476 * is set the function will perform a self-check. If the device does not report
477 * a error bit in the channels raw value set error_mask to 0.
479 int adis_single_conversion(struct iio_dev *indio_dev,
480 const struct iio_chan_spec *chan, unsigned int error_mask, int *val)
482 struct adis *adis = iio_device_get_drvdata(indio_dev);
486 mutex_lock(&adis->state_lock);
488 ret = __adis_read_reg(adis, chan->address, &uval,
489 chan->scan_type.storagebits / 8);
493 if (uval & error_mask) {
494 ret = __adis_check_status(adis);
499 if (chan->scan_type.sign == 's')
500 *val = sign_extend32(uval, chan->scan_type.realbits - 1);
502 *val = uval & ((1 << chan->scan_type.realbits) - 1);
506 mutex_unlock(&adis->state_lock);
509 EXPORT_SYMBOL_GPL(adis_single_conversion);
512 * adis_init() - Initialize adis device structure
513 * @adis: The adis device
514 * @indio_dev: The iio device
515 * @spi: The spi device
516 * @data: Chip specific data
518 * Returns 0 on success, a negative error code otherwise.
520 * This function must be called, before any other adis helper function may be
523 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
524 struct spi_device *spi, const struct adis_data *data)
526 if (!data || !data->timeouts) {
527 dev_err(&spi->dev, "No config data or timeouts not defined!\n");
531 mutex_init(&adis->state_lock);
534 iio_device_set_drvdata(indio_dev, adis);
536 if (data->has_paging) {
537 /* Need to set the page before first read/write */
538 adis->current_page = -1;
540 /* Page will always be 0 */
541 adis->current_page = 0;
546 EXPORT_SYMBOL_GPL(adis_init);
548 MODULE_LICENSE("GPL");
550 MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");