blob: 715eef81bc24807f1d53afa766e6d8fd1d63011e [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Common library for ADIS16XXX devices
4 *
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#include <linux/delay.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020010#include <linux/gpio/consumer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#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
Olivier Deprez157378f2022-04-04 15:47:50 +020030/**
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 */
37int __adis_write_reg(struct adis *adis, unsigned int reg,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 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,
Olivier Deprez157378f2022-04-04 15:47:50 +020049 .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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053 }, {
54 .tx_buf = adis->tx + 2,
55 .bits_per_word = 8,
56 .len = 2,
57 .cs_change = 1,
Olivier Deprez157378f2022-04-04 15:47:50 +020058 .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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062 }, {
63 .tx_buf = adis->tx + 4,
64 .bits_per_word = 8,
65 .len = 2,
66 .cs_change = 1,
Olivier Deprez157378f2022-04-04 15:47:50 +020067 .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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071 }, {
72 .tx_buf = adis->tx + 6,
73 .bits_per_word = 8,
74 .len = 2,
Olivier Deprez157378f2022-04-04 15:47:50 +020075 .delay.value = adis->data->write_delay,
76 .delay.unit = SPI_DELAY_UNIT_USECS,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 }, {
78 .tx_buf = adis->tx + 8,
79 .bits_per_word = 8,
80 .len = 2,
Olivier Deprez157378f2022-04-04 15:47:50 +020081 .delay.value = adis->data->write_delay,
82 .delay.unit = SPI_DELAY_UNIT_USECS,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 },
84 };
85
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086 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;
Olivier Deprez157378f2022-04-04 15:47:50 +0200100 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101 case 2:
102 adis->tx[4] = ADIS_WRITE_REG(reg + 1);
103 adis->tx[5] = (value >> 8) & 0xff;
Olivier Deprez157378f2022-04-04 15:47:50 +0200104 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 case 1:
106 adis->tx[2] = ADIS_WRITE_REG(reg);
107 adis->tx[3] = value & 0xff;
108 break;
109 default:
Olivier Deprez157378f2022-04-04 15:47:50 +0200110 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111 }
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126 return ret;
127}
Olivier Deprez157378f2022-04-04 15:47:50 +0200128EXPORT_SYMBOL_GPL(__adis_write_reg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129
130/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200131 * __adis_read_reg() - read N bytes from register (unlocked version)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 * @adis: The adis device
133 * @reg: The address of the lower of the two registers
134 * @val: The value read back from the device
Olivier Deprez157378f2022-04-04 15:47:50 +0200135 * @size: The size of the @val buffer
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200137int __adis_read_reg(struct adis *adis, unsigned int reg,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138 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,
Olivier Deprez157378f2022-04-04 15:47:50 +0200149 .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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153 }, {
154 .tx_buf = adis->tx + 2,
155 .bits_per_word = 8,
156 .len = 2,
157 .cs_change = 1,
Olivier Deprez157378f2022-04-04 15:47:50 +0200158 .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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000162 }, {
163 .tx_buf = adis->tx + 4,
164 .rx_buf = adis->rx,
165 .bits_per_word = 8,
166 .len = 2,
167 .cs_change = 1,
Olivier Deprez157378f2022-04-04 15:47:50 +0200168 .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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172 }, {
173 .rx_buf = adis->rx + 2,
174 .bits_per_word = 8,
175 .len = 2,
Olivier Deprez157378f2022-04-04 15:47:50 +0200176 .delay.value = adis->data->read_delay,
177 .delay.unit = SPI_DELAY_UNIT_USECS,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 },
179 };
180
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181 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);
Olivier Deprez157378f2022-04-04 15:47:50 +0200194 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195 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:
Olivier Deprez157378f2022-04-04 15:47:50 +0200202 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203 }
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);
Olivier Deprez157378f2022-04-04 15:47:50 +0200209 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210 } 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223 return ret;
224}
Olivier Deprez157378f2022-04-04 15:47:50 +0200225EXPORT_SYMBOL_GPL(__adis_read_reg);
226/**
227 * __adis_update_bits_base() - ADIS Update bits function - Unlocked version
228 * @adis: The adis device
229 * @reg: The address of the lower of the two registers
230 * @mask: Bitmask to change
231 * @val: Value to be written
232 * @size: Size of the register to update
233 *
234 * Updates the desired bits of @reg in accordance with @mask and @val.
235 */
236int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
237 const u32 val, u8 size)
238{
239 int ret;
240 u32 __val;
241
242 ret = __adis_read_reg(adis, reg, &__val, size);
243 if (ret)
244 return ret;
245
246 __val = (__val & ~mask) | (val & mask);
247
248 return __adis_write_reg(adis, reg, __val, size);
249}
250EXPORT_SYMBOL_GPL(__adis_update_bits_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251
252#ifdef CONFIG_DEBUG_FS
253
254int adis_debugfs_reg_access(struct iio_dev *indio_dev,
255 unsigned int reg, unsigned int writeval, unsigned int *readval)
256{
257 struct adis *adis = iio_device_get_drvdata(indio_dev);
258
259 if (readval) {
260 uint16_t val16;
261 int ret;
262
263 ret = adis_read_reg_16(adis, reg, &val16);
Olivier Deprez157378f2022-04-04 15:47:50 +0200264 if (ret == 0)
265 *readval = val16;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266
267 return ret;
268 } else {
269 return adis_write_reg_16(adis, reg, writeval);
270 }
271}
272EXPORT_SYMBOL(adis_debugfs_reg_access);
273
274#endif
275
276/**
277 * adis_enable_irq() - Enable or disable data ready IRQ
278 * @adis: The adis device
279 * @enable: Whether to enable the IRQ
280 *
281 * Returns 0 on success, negative error code otherwise
282 */
283int adis_enable_irq(struct adis *adis, bool enable)
284{
285 int ret = 0;
286 uint16_t msc;
287
Olivier Deprez157378f2022-04-04 15:47:50 +0200288 mutex_lock(&adis->state_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289
Olivier Deprez157378f2022-04-04 15:47:50 +0200290 if (adis->data->enable_irq) {
291 ret = adis->data->enable_irq(adis, enable);
292 goto out_unlock;
293 }
294
295 ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000296 if (ret)
Olivier Deprez157378f2022-04-04 15:47:50 +0200297 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298
299 msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
300 msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
301 if (enable)
302 msc |= ADIS_MSC_CTRL_DATA_RDY_EN;
303 else
304 msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
305
Olivier Deprez157378f2022-04-04 15:47:50 +0200306 ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000307
Olivier Deprez157378f2022-04-04 15:47:50 +0200308out_unlock:
309 mutex_unlock(&adis->state_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000310 return ret;
311}
312EXPORT_SYMBOL(adis_enable_irq);
313
314/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200315 * __adis_check_status() - Check the device for error conditions (unlocked)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316 * @adis: The adis device
317 *
318 * Returns 0 on success, a negative error code otherwise
319 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200320int __adis_check_status(struct adis *adis)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321{
322 uint16_t status;
323 int ret;
324 int i;
325
Olivier Deprez157378f2022-04-04 15:47:50 +0200326 ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
327 if (ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000328 return ret;
329
330 status &= adis->data->status_error_mask;
331
332 if (status == 0)
333 return 0;
334
335 for (i = 0; i < 16; ++i) {
336 if (status & BIT(i)) {
337 dev_err(&adis->spi->dev, "%s.\n",
338 adis->data->status_error_msgs[i]);
339 }
340 }
341
342 return -EIO;
343}
Olivier Deprez157378f2022-04-04 15:47:50 +0200344EXPORT_SYMBOL_GPL(__adis_check_status);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000345
346/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200347 * __adis_reset() - Reset the device (unlocked version)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348 * @adis: The adis device
349 *
350 * Returns 0 on success, a negative error code otherwise
351 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200352int __adis_reset(struct adis *adis)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353{
354 int ret;
Olivier Deprez157378f2022-04-04 15:47:50 +0200355 const struct adis_timeout *timeouts = adis->data->timeouts;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356
Olivier Deprez157378f2022-04-04 15:47:50 +0200357 ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358 ADIS_GLOB_CMD_SW_RESET);
Olivier Deprez157378f2022-04-04 15:47:50 +0200359 if (ret) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000360 dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
Olivier Deprez157378f2022-04-04 15:47:50 +0200361 return ret;
362 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000363
Olivier Deprez157378f2022-04-04 15:47:50 +0200364 msleep(timeouts->sw_reset_ms);
365
366 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000367}
Olivier Deprez157378f2022-04-04 15:47:50 +0200368EXPORT_SYMBOL_GPL(__adis_reset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000369
370static int adis_self_test(struct adis *adis)
371{
372 int ret;
Olivier Deprez157378f2022-04-04 15:47:50 +0200373 const struct adis_timeout *timeouts = adis->data->timeouts;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374
Olivier Deprez157378f2022-04-04 15:47:50 +0200375 ret = __adis_write_reg_16(adis, adis->data->self_test_reg,
376 adis->data->self_test_mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000377 if (ret) {
378 dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
379 ret);
380 return ret;
381 }
382
Olivier Deprez157378f2022-04-04 15:47:50 +0200383 msleep(timeouts->self_test_ms);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000384
Olivier Deprez157378f2022-04-04 15:47:50 +0200385 ret = __adis_check_status(adis);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386
387 if (adis->data->self_test_no_autoclear)
Olivier Deprez157378f2022-04-04 15:47:50 +0200388 __adis_write_reg_16(adis, adis->data->self_test_reg, 0x00);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389
390 return ret;
391}
392
393/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200394 * __adis_initial_startup() - Device initial setup
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000395 * @adis: The adis device
396 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200397 * The function performs a HW reset via a reset pin that should be specified
398 * via GPIOLIB. If no pin is configured a SW reset will be performed.
399 * The RST pin for the ADIS devices should be configured as ACTIVE_LOW.
400 *
401 * After the self-test operation is performed, the function will also check
402 * that the product ID is as expected. This assumes that drivers providing
403 * 'prod_id_reg' will also provide the 'prod_id'.
404 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000405 * Returns 0 if the device is operational, a negative error code otherwise.
406 *
407 * This function should be called early on in the device initialization sequence
408 * to ensure that the device is in a sane and known state and that it is usable.
409 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200410int __adis_initial_startup(struct adis *adis)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000411{
Olivier Deprez157378f2022-04-04 15:47:50 +0200412 const struct adis_timeout *timeouts = adis->data->timeouts;
413 struct gpio_desc *gpio;
414 uint16_t prod_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000415 int ret;
416
Olivier Deprez157378f2022-04-04 15:47:50 +0200417 /* check if the device has rst pin low */
418 gpio = devm_gpiod_get_optional(&adis->spi->dev, "reset", GPIOD_OUT_HIGH);
419 if (IS_ERR(gpio))
420 return PTR_ERR(gpio);
421
422 if (gpio) {
423 msleep(10);
424 /* bring device out of reset */
425 gpiod_set_value_cansleep(gpio, 0);
426 msleep(timeouts->reset_ms);
427 } else {
428 ret = __adis_reset(adis);
429 if (ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000431 }
432
Olivier Deprez157378f2022-04-04 15:47:50 +0200433 ret = adis_self_test(adis);
434 if (ret)
435 return ret;
436
437 adis_enable_irq(adis, false);
438
439 if (!adis->data->prod_id_reg)
440 return 0;
441
442 ret = adis_read_reg_16(adis, adis->data->prod_id_reg, &prod_id);
443 if (ret)
444 return ret;
445
446 if (prod_id != adis->data->prod_id)
447 dev_warn(&adis->spi->dev,
448 "Device ID(%u) and product ID(%u) do not match.\n",
449 adis->data->prod_id, prod_id);
450
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451 return 0;
452}
Olivier Deprez157378f2022-04-04 15:47:50 +0200453EXPORT_SYMBOL_GPL(__adis_initial_startup);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000454
455/**
456 * adis_single_conversion() - Performs a single sample conversion
457 * @indio_dev: The IIO device
458 * @chan: The IIO channel
459 * @error_mask: Mask for the error bit
460 * @val: Result of the conversion
461 *
462 * Returns IIO_VAL_INT on success, a negative error code otherwise.
463 *
464 * The function performs a single conversion on a given channel and post
465 * processes the value accordingly to the channel spec. If a error_mask is given
466 * the function will check if the mask is set in the returned raw value. If it
467 * is set the function will perform a self-check. If the device does not report
468 * a error bit in the channels raw value set error_mask to 0.
469 */
470int adis_single_conversion(struct iio_dev *indio_dev,
471 const struct iio_chan_spec *chan, unsigned int error_mask, int *val)
472{
473 struct adis *adis = iio_device_get_drvdata(indio_dev);
474 unsigned int uval;
475 int ret;
476
Olivier Deprez157378f2022-04-04 15:47:50 +0200477 mutex_lock(&adis->state_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000478
Olivier Deprez157378f2022-04-04 15:47:50 +0200479 ret = __adis_read_reg(adis, chan->address, &uval,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000480 chan->scan_type.storagebits / 8);
481 if (ret)
482 goto err_unlock;
483
484 if (uval & error_mask) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200485 ret = __adis_check_status(adis);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486 if (ret)
487 goto err_unlock;
488 }
489
490 if (chan->scan_type.sign == 's')
491 *val = sign_extend32(uval, chan->scan_type.realbits - 1);
492 else
493 *val = uval & ((1 << chan->scan_type.realbits) - 1);
494
495 ret = IIO_VAL_INT;
496err_unlock:
Olivier Deprez157378f2022-04-04 15:47:50 +0200497 mutex_unlock(&adis->state_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000498 return ret;
499}
500EXPORT_SYMBOL_GPL(adis_single_conversion);
501
502/**
503 * adis_init() - Initialize adis device structure
504 * @adis: The adis device
505 * @indio_dev: The iio device
506 * @spi: The spi device
507 * @data: Chip specific data
508 *
509 * Returns 0 on success, a negative error code otherwise.
510 *
511 * This function must be called, before any other adis helper function may be
512 * called.
513 */
514int adis_init(struct adis *adis, struct iio_dev *indio_dev,
515 struct spi_device *spi, const struct adis_data *data)
516{
Olivier Deprez157378f2022-04-04 15:47:50 +0200517 if (!data || !data->timeouts) {
518 dev_err(&spi->dev, "No config data or timeouts not defined!\n");
519 return -EINVAL;
520 }
521
522 mutex_init(&adis->state_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 adis->spi = spi;
524 adis->data = data;
525 iio_device_set_drvdata(indio_dev, adis);
526
527 if (data->has_paging) {
528 /* Need to set the page before first read/write */
529 adis->current_page = -1;
530 } else {
531 /* Page will always be 0 */
532 adis->current_page = 0;
533 }
534
Olivier Deprez157378f2022-04-04 15:47:50 +0200535 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000536}
537EXPORT_SYMBOL_GPL(adis_init);
538
539MODULE_LICENSE("GPL");
540MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
541MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");