18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ADIS16260/ADIS16265 Programmable Digital Gyroscope Sensor Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2010 Analog Devices Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
98c2ecf20Sopenharmony_ci#include <linux/mutex.h>
108c2ecf20Sopenharmony_ci#include <linux/device.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
138c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
178c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h>
188c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h>
198c2ecf20Sopenharmony_ci#include <linux/iio/imu/adis.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define ADIS16260_STARTUP_DELAY	220 /* ms */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define ADIS16260_FLASH_CNT  0x00 /* Flash memory write count */
248c2ecf20Sopenharmony_ci#define ADIS16260_SUPPLY_OUT 0x02 /* Power supply measurement */
258c2ecf20Sopenharmony_ci#define ADIS16260_GYRO_OUT   0x04 /* X-axis gyroscope output */
268c2ecf20Sopenharmony_ci#define ADIS16260_AUX_ADC    0x0A /* analog input channel measurement */
278c2ecf20Sopenharmony_ci#define ADIS16260_TEMP_OUT   0x0C /* internal temperature measurement */
288c2ecf20Sopenharmony_ci#define ADIS16260_ANGL_OUT   0x0E /* angle displacement */
298c2ecf20Sopenharmony_ci#define ADIS16260_GYRO_OFF   0x14 /* Calibration, offset/bias adjustment */
308c2ecf20Sopenharmony_ci#define ADIS16260_GYRO_SCALE 0x16 /* Calibration, scale adjustment */
318c2ecf20Sopenharmony_ci#define ADIS16260_ALM_MAG1   0x20 /* Alarm 1 magnitude/polarity setting */
328c2ecf20Sopenharmony_ci#define ADIS16260_ALM_MAG2   0x22 /* Alarm 2 magnitude/polarity setting */
338c2ecf20Sopenharmony_ci#define ADIS16260_ALM_SMPL1  0x24 /* Alarm 1 dynamic rate of change setting */
348c2ecf20Sopenharmony_ci#define ADIS16260_ALM_SMPL2  0x26 /* Alarm 2 dynamic rate of change setting */
358c2ecf20Sopenharmony_ci#define ADIS16260_ALM_CTRL   0x28 /* Alarm control */
368c2ecf20Sopenharmony_ci#define ADIS16260_AUX_DAC    0x30 /* Auxiliary DAC data */
378c2ecf20Sopenharmony_ci#define ADIS16260_GPIO_CTRL  0x32 /* Control, digital I/O line */
388c2ecf20Sopenharmony_ci#define ADIS16260_MSC_CTRL   0x34 /* Control, data ready, self-test settings */
398c2ecf20Sopenharmony_ci#define ADIS16260_SMPL_PRD   0x36 /* Control, internal sample rate */
408c2ecf20Sopenharmony_ci#define ADIS16260_SENS_AVG   0x38 /* Control, dynamic range, filtering */
418c2ecf20Sopenharmony_ci#define ADIS16260_SLP_CNT    0x3A /* Control, sleep mode initiation */
428c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT  0x3C /* Diagnostic, error flags */
438c2ecf20Sopenharmony_ci#define ADIS16260_GLOB_CMD   0x3E /* Control, global commands */
448c2ecf20Sopenharmony_ci#define ADIS16260_LOT_ID1    0x52 /* Lot Identification Code 1 */
458c2ecf20Sopenharmony_ci#define ADIS16260_LOT_ID2    0x54 /* Lot Identification Code 2 */
468c2ecf20Sopenharmony_ci#define ADIS16260_PROD_ID    0x56 /* Product identifier;
478c2ecf20Sopenharmony_ci				   * convert to decimal = 16,265/16,260 */
488c2ecf20Sopenharmony_ci#define ADIS16260_SERIAL_NUM 0x58 /* Serial number */
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define ADIS16260_ERROR_ACTIVE			(1<<14)
518c2ecf20Sopenharmony_ci#define ADIS16260_NEW_DATA			(1<<15)
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* MSC_CTRL */
548c2ecf20Sopenharmony_ci#define ADIS16260_MSC_CTRL_MEM_TEST		(1<<11)
558c2ecf20Sopenharmony_ci/* Internal self-test enable */
568c2ecf20Sopenharmony_ci#define ADIS16260_MSC_CTRL_INT_SELF_TEST	(1<<10)
578c2ecf20Sopenharmony_ci#define ADIS16260_MSC_CTRL_NEG_SELF_TEST	(1<<9)
588c2ecf20Sopenharmony_ci#define ADIS16260_MSC_CTRL_POS_SELF_TEST	(1<<8)
598c2ecf20Sopenharmony_ci#define ADIS16260_MSC_CTRL_DATA_RDY_EN		(1<<2)
608c2ecf20Sopenharmony_ci#define ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH	(1<<1)
618c2ecf20Sopenharmony_ci#define ADIS16260_MSC_CTRL_DATA_RDY_DIO2	(1<<0)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/* SMPL_PRD */
648c2ecf20Sopenharmony_ci/* Time base (tB): 0 = 1.953 ms, 1 = 60.54 ms */
658c2ecf20Sopenharmony_ci#define ADIS16260_SMPL_PRD_TIME_BASE	(1<<7)
668c2ecf20Sopenharmony_ci#define ADIS16260_SMPL_PRD_DIV_MASK	0x7F
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* SLP_CNT */
698c2ecf20Sopenharmony_ci#define ADIS16260_SLP_CNT_POWER_OFF     0x80
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* DIAG_STAT */
728c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_ALARM2	(1<<9)
738c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_ALARM1	(1<<8)
748c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_FLASH_CHK_BIT	6
758c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_SELF_TEST_BIT	5
768c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_OVERFLOW_BIT	4
778c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_SPI_FAIL_BIT	3
788c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_FLASH_UPT_BIT	2
798c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_POWER_HIGH_BIT	1
808c2ecf20Sopenharmony_ci#define ADIS16260_DIAG_STAT_POWER_LOW_BIT	0
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* GLOB_CMD */
838c2ecf20Sopenharmony_ci#define ADIS16260_GLOB_CMD_SW_RESET	(1<<7)
848c2ecf20Sopenharmony_ci#define ADIS16260_GLOB_CMD_FLASH_UPD	(1<<3)
858c2ecf20Sopenharmony_ci#define ADIS16260_GLOB_CMD_DAC_LATCH	(1<<2)
868c2ecf20Sopenharmony_ci#define ADIS16260_GLOB_CMD_FAC_CALIB	(1<<1)
878c2ecf20Sopenharmony_ci#define ADIS16260_GLOB_CMD_AUTO_NULL	(1<<0)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define ADIS16260_SPI_SLOW	(u32)(300 * 1000)
908c2ecf20Sopenharmony_ci#define ADIS16260_SPI_BURST	(u32)(1000 * 1000)
918c2ecf20Sopenharmony_ci#define ADIS16260_SPI_FAST	(u32)(2000 * 1000)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* At the moment triggers are only used for ring buffer
948c2ecf20Sopenharmony_ci * filling. This may change!
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#define ADIS16260_SCAN_GYRO	0
988c2ecf20Sopenharmony_ci#define ADIS16260_SCAN_SUPPLY	1
998c2ecf20Sopenharmony_ci#define ADIS16260_SCAN_AUX_ADC	2
1008c2ecf20Sopenharmony_ci#define ADIS16260_SCAN_TEMP	3
1018c2ecf20Sopenharmony_ci#define ADIS16260_SCAN_ANGL	4
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistruct adis16260_chip_info {
1048c2ecf20Sopenharmony_ci	unsigned int gyro_max_val;
1058c2ecf20Sopenharmony_ci	unsigned int gyro_max_scale;
1068c2ecf20Sopenharmony_ci	const struct iio_chan_spec *channels;
1078c2ecf20Sopenharmony_ci	unsigned int num_channels;
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistruct adis16260 {
1118c2ecf20Sopenharmony_ci	const struct adis16260_chip_info *info;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	struct adis adis;
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cienum adis16260_type {
1178c2ecf20Sopenharmony_ci	ADIS16251,
1188c2ecf20Sopenharmony_ci	ADIS16260,
1198c2ecf20Sopenharmony_ci	ADIS16266,
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const struct iio_chan_spec adis16260_channels[] = {
1238c2ecf20Sopenharmony_ci	ADIS_GYRO_CHAN(X, ADIS16260_GYRO_OUT, ADIS16260_SCAN_GYRO,
1248c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_CALIBBIAS) |
1258c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_CALIBSCALE),
1268c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 14),
1278c2ecf20Sopenharmony_ci	ADIS_INCLI_CHAN(X, ADIS16260_ANGL_OUT, ADIS16260_SCAN_ANGL, 0,
1288c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 14),
1298c2ecf20Sopenharmony_ci	ADIS_TEMP_CHAN(ADIS16260_TEMP_OUT, ADIS16260_SCAN_TEMP,
1308c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
1318c2ecf20Sopenharmony_ci	ADIS_SUPPLY_CHAN(ADIS16260_SUPPLY_OUT, ADIS16260_SCAN_SUPPLY,
1328c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
1338c2ecf20Sopenharmony_ci	ADIS_AUX_ADC_CHAN(ADIS16260_AUX_ADC, ADIS16260_SCAN_AUX_ADC,
1348c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
1358c2ecf20Sopenharmony_ci	IIO_CHAN_SOFT_TIMESTAMP(5),
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic const struct iio_chan_spec adis16266_channels[] = {
1398c2ecf20Sopenharmony_ci	ADIS_GYRO_CHAN(X, ADIS16260_GYRO_OUT, ADIS16260_SCAN_GYRO,
1408c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_CALIBBIAS) |
1418c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_CALIBSCALE),
1428c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 14),
1438c2ecf20Sopenharmony_ci	ADIS_TEMP_CHAN(ADIS16260_TEMP_OUT, ADIS16260_SCAN_TEMP,
1448c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
1458c2ecf20Sopenharmony_ci	ADIS_SUPPLY_CHAN(ADIS16260_SUPPLY_OUT, ADIS16260_SCAN_SUPPLY,
1468c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
1478c2ecf20Sopenharmony_ci	ADIS_AUX_ADC_CHAN(ADIS16260_AUX_ADC, ADIS16260_SCAN_AUX_ADC,
1488c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
1498c2ecf20Sopenharmony_ci	IIO_CHAN_SOFT_TIMESTAMP(4),
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic const struct adis16260_chip_info adis16260_chip_info_table[] = {
1538c2ecf20Sopenharmony_ci	[ADIS16251] = {
1548c2ecf20Sopenharmony_ci		.gyro_max_scale = 80,
1558c2ecf20Sopenharmony_ci		.gyro_max_val = IIO_RAD_TO_DEGREE(4368),
1568c2ecf20Sopenharmony_ci		.channels = adis16260_channels,
1578c2ecf20Sopenharmony_ci		.num_channels = ARRAY_SIZE(adis16260_channels),
1588c2ecf20Sopenharmony_ci	},
1598c2ecf20Sopenharmony_ci	[ADIS16260] = {
1608c2ecf20Sopenharmony_ci		.gyro_max_scale = 320,
1618c2ecf20Sopenharmony_ci		.gyro_max_val = IIO_RAD_TO_DEGREE(4368),
1628c2ecf20Sopenharmony_ci		.channels = adis16260_channels,
1638c2ecf20Sopenharmony_ci		.num_channels = ARRAY_SIZE(adis16260_channels),
1648c2ecf20Sopenharmony_ci	},
1658c2ecf20Sopenharmony_ci	[ADIS16266] = {
1668c2ecf20Sopenharmony_ci		.gyro_max_scale = 14000,
1678c2ecf20Sopenharmony_ci		.gyro_max_val = IIO_RAD_TO_DEGREE(3357),
1688c2ecf20Sopenharmony_ci		.channels = adis16266_channels,
1698c2ecf20Sopenharmony_ci		.num_channels = ARRAY_SIZE(adis16266_channels),
1708c2ecf20Sopenharmony_ci	},
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/* Power down the device */
1748c2ecf20Sopenharmony_cistatic int adis16260_stop_device(struct iio_dev *indio_dev)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	struct adis16260 *adis16260 = iio_priv(indio_dev);
1778c2ecf20Sopenharmony_ci	int ret;
1788c2ecf20Sopenharmony_ci	u16 val = ADIS16260_SLP_CNT_POWER_OFF;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	ret = adis_write_reg_16(&adis16260->adis, ADIS16260_SLP_CNT, val);
1818c2ecf20Sopenharmony_ci	if (ret)
1828c2ecf20Sopenharmony_ci		dev_err(&indio_dev->dev, "problem with turning device off: SLP_CNT");
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return ret;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic const u8 adis16260_addresses[][2] = {
1888c2ecf20Sopenharmony_ci	[ADIS16260_SCAN_GYRO] = { ADIS16260_GYRO_OFF, ADIS16260_GYRO_SCALE },
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic int adis16260_read_raw(struct iio_dev *indio_dev,
1928c2ecf20Sopenharmony_ci			      struct iio_chan_spec const *chan,
1938c2ecf20Sopenharmony_ci			      int *val, int *val2,
1948c2ecf20Sopenharmony_ci			      long mask)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct adis16260 *adis16260 = iio_priv(indio_dev);
1978c2ecf20Sopenharmony_ci	const struct adis16260_chip_info *info = adis16260->info;
1988c2ecf20Sopenharmony_ci	struct adis *adis = &adis16260->adis;
1998c2ecf20Sopenharmony_ci	int ret;
2008c2ecf20Sopenharmony_ci	u8 addr;
2018c2ecf20Sopenharmony_ci	s16 val16;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	switch (mask) {
2048c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
2058c2ecf20Sopenharmony_ci		return adis_single_conversion(indio_dev, chan,
2068c2ecf20Sopenharmony_ci				ADIS16260_ERROR_ACTIVE, val);
2078c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SCALE:
2088c2ecf20Sopenharmony_ci		switch (chan->type) {
2098c2ecf20Sopenharmony_ci		case IIO_ANGL_VEL:
2108c2ecf20Sopenharmony_ci			*val = info->gyro_max_scale;
2118c2ecf20Sopenharmony_ci			*val2 = info->gyro_max_val;
2128c2ecf20Sopenharmony_ci			return IIO_VAL_FRACTIONAL;
2138c2ecf20Sopenharmony_ci		case IIO_INCLI:
2148c2ecf20Sopenharmony_ci			*val = 0;
2158c2ecf20Sopenharmony_ci			*val2 = IIO_DEGREE_TO_RAD(36630);
2168c2ecf20Sopenharmony_ci			return IIO_VAL_INT_PLUS_MICRO;
2178c2ecf20Sopenharmony_ci		case IIO_VOLTAGE:
2188c2ecf20Sopenharmony_ci			if (chan->channel == 0) {
2198c2ecf20Sopenharmony_ci				*val = 1;
2208c2ecf20Sopenharmony_ci				*val2 = 831500; /* 1.8315 mV */
2218c2ecf20Sopenharmony_ci			} else {
2228c2ecf20Sopenharmony_ci				*val = 0;
2238c2ecf20Sopenharmony_ci				*val2 = 610500; /* 610.5 uV */
2248c2ecf20Sopenharmony_ci			}
2258c2ecf20Sopenharmony_ci			return IIO_VAL_INT_PLUS_MICRO;
2268c2ecf20Sopenharmony_ci		case IIO_TEMP:
2278c2ecf20Sopenharmony_ci			*val = 145;
2288c2ecf20Sopenharmony_ci			*val2 = 300000; /* 0.1453 C */
2298c2ecf20Sopenharmony_ci			return IIO_VAL_INT_PLUS_MICRO;
2308c2ecf20Sopenharmony_ci		default:
2318c2ecf20Sopenharmony_ci			return -EINVAL;
2328c2ecf20Sopenharmony_ci		}
2338c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_OFFSET:
2348c2ecf20Sopenharmony_ci		*val = 250000 / 1453; /* 25 C = 0x00 */
2358c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
2368c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_CALIBBIAS:
2378c2ecf20Sopenharmony_ci		addr = adis16260_addresses[chan->scan_index][0];
2388c2ecf20Sopenharmony_ci		ret = adis_read_reg_16(adis, addr, &val16);
2398c2ecf20Sopenharmony_ci		if (ret)
2408c2ecf20Sopenharmony_ci			return ret;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci		*val = sign_extend32(val16, 11);
2438c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
2448c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_CALIBSCALE:
2458c2ecf20Sopenharmony_ci		addr = adis16260_addresses[chan->scan_index][1];
2468c2ecf20Sopenharmony_ci		ret = adis_read_reg_16(adis, addr, &val16);
2478c2ecf20Sopenharmony_ci		if (ret)
2488c2ecf20Sopenharmony_ci			return ret;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci		*val = val16;
2518c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
2528c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SAMP_FREQ:
2538c2ecf20Sopenharmony_ci		ret = adis_read_reg_16(adis, ADIS16260_SMPL_PRD, &val16);
2548c2ecf20Sopenharmony_ci		if (ret)
2558c2ecf20Sopenharmony_ci			return ret;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci		if (spi_get_device_id(adis->spi)->driver_data)
2588c2ecf20Sopenharmony_ci		/* If an adis16251 */
2598c2ecf20Sopenharmony_ci			*val = (val16 & ADIS16260_SMPL_PRD_TIME_BASE) ?
2608c2ecf20Sopenharmony_ci				8 : 256;
2618c2ecf20Sopenharmony_ci		else
2628c2ecf20Sopenharmony_ci			*val = (val16 & ADIS16260_SMPL_PRD_TIME_BASE) ?
2638c2ecf20Sopenharmony_ci				66 : 2048;
2648c2ecf20Sopenharmony_ci		*val /= (val16 & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
2658c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci	return -EINVAL;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic int adis16260_write_raw(struct iio_dev *indio_dev,
2718c2ecf20Sopenharmony_ci			       struct iio_chan_spec const *chan,
2728c2ecf20Sopenharmony_ci			       int val,
2738c2ecf20Sopenharmony_ci			       int val2,
2748c2ecf20Sopenharmony_ci			       long mask)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct adis16260 *adis16260 = iio_priv(indio_dev);
2778c2ecf20Sopenharmony_ci	struct adis *adis = &adis16260->adis;
2788c2ecf20Sopenharmony_ci	int ret;
2798c2ecf20Sopenharmony_ci	u8 addr;
2808c2ecf20Sopenharmony_ci	u8 t;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	switch (mask) {
2838c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_CALIBBIAS:
2848c2ecf20Sopenharmony_ci		if (val < -2048 || val >= 2048)
2858c2ecf20Sopenharmony_ci			return -EINVAL;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci		addr = adis16260_addresses[chan->scan_index][0];
2888c2ecf20Sopenharmony_ci		return adis_write_reg_16(adis, addr, val);
2898c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_CALIBSCALE:
2908c2ecf20Sopenharmony_ci		if (val < 0 || val >= 4096)
2918c2ecf20Sopenharmony_ci			return -EINVAL;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		addr = adis16260_addresses[chan->scan_index][1];
2948c2ecf20Sopenharmony_ci		return adis_write_reg_16(adis, addr, val);
2958c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SAMP_FREQ:
2968c2ecf20Sopenharmony_ci		mutex_lock(&adis->state_lock);
2978c2ecf20Sopenharmony_ci		if (spi_get_device_id(adis->spi)->driver_data)
2988c2ecf20Sopenharmony_ci			t = 256 / val;
2998c2ecf20Sopenharmony_ci		else
3008c2ecf20Sopenharmony_ci			t = 2048 / val;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci		if (t > ADIS16260_SMPL_PRD_DIV_MASK)
3038c2ecf20Sopenharmony_ci			t = ADIS16260_SMPL_PRD_DIV_MASK;
3048c2ecf20Sopenharmony_ci		else if (t > 0)
3058c2ecf20Sopenharmony_ci			t--;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci		if (t >= 0x0A)
3088c2ecf20Sopenharmony_ci			adis->spi->max_speed_hz = ADIS16260_SPI_SLOW;
3098c2ecf20Sopenharmony_ci		else
3108c2ecf20Sopenharmony_ci			adis->spi->max_speed_hz = ADIS16260_SPI_FAST;
3118c2ecf20Sopenharmony_ci		ret = __adis_write_reg_8(adis, ADIS16260_SMPL_PRD, t);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci		mutex_unlock(&adis->state_lock);
3148c2ecf20Sopenharmony_ci		return ret;
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci	return -EINVAL;
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic const struct iio_info adis16260_info = {
3208c2ecf20Sopenharmony_ci	.read_raw = &adis16260_read_raw,
3218c2ecf20Sopenharmony_ci	.write_raw = &adis16260_write_raw,
3228c2ecf20Sopenharmony_ci	.update_scan_mode = adis_update_scan_mode,
3238c2ecf20Sopenharmony_ci};
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic const char * const adis1620_status_error_msgs[] = {
3268c2ecf20Sopenharmony_ci	[ADIS16260_DIAG_STAT_FLASH_CHK_BIT] = "Flash checksum error",
3278c2ecf20Sopenharmony_ci	[ADIS16260_DIAG_STAT_SELF_TEST_BIT] = "Self test error",
3288c2ecf20Sopenharmony_ci	[ADIS16260_DIAG_STAT_OVERFLOW_BIT] = "Sensor overrange",
3298c2ecf20Sopenharmony_ci	[ADIS16260_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
3308c2ecf20Sopenharmony_ci	[ADIS16260_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
3318c2ecf20Sopenharmony_ci	[ADIS16260_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 5.25",
3328c2ecf20Sopenharmony_ci	[ADIS16260_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 4.75",
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic const struct adis_timeout adis16260_timeouts = {
3368c2ecf20Sopenharmony_ci	.reset_ms = ADIS16260_STARTUP_DELAY,
3378c2ecf20Sopenharmony_ci	.sw_reset_ms = ADIS16260_STARTUP_DELAY,
3388c2ecf20Sopenharmony_ci	.self_test_ms = ADIS16260_STARTUP_DELAY,
3398c2ecf20Sopenharmony_ci};
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic const struct adis_data adis16260_data = {
3428c2ecf20Sopenharmony_ci	.write_delay = 30,
3438c2ecf20Sopenharmony_ci	.read_delay = 30,
3448c2ecf20Sopenharmony_ci	.msc_ctrl_reg = ADIS16260_MSC_CTRL,
3458c2ecf20Sopenharmony_ci	.glob_cmd_reg = ADIS16260_GLOB_CMD,
3468c2ecf20Sopenharmony_ci	.diag_stat_reg = ADIS16260_DIAG_STAT,
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	.self_test_mask = ADIS16260_MSC_CTRL_MEM_TEST,
3498c2ecf20Sopenharmony_ci	.self_test_reg = ADIS16260_MSC_CTRL,
3508c2ecf20Sopenharmony_ci	.timeouts = &adis16260_timeouts,
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	.status_error_msgs = adis1620_status_error_msgs,
3538c2ecf20Sopenharmony_ci	.status_error_mask = BIT(ADIS16260_DIAG_STAT_FLASH_CHK_BIT) |
3548c2ecf20Sopenharmony_ci		BIT(ADIS16260_DIAG_STAT_SELF_TEST_BIT) |
3558c2ecf20Sopenharmony_ci		BIT(ADIS16260_DIAG_STAT_OVERFLOW_BIT) |
3568c2ecf20Sopenharmony_ci		BIT(ADIS16260_DIAG_STAT_SPI_FAIL_BIT) |
3578c2ecf20Sopenharmony_ci		BIT(ADIS16260_DIAG_STAT_FLASH_UPT_BIT) |
3588c2ecf20Sopenharmony_ci		BIT(ADIS16260_DIAG_STAT_POWER_HIGH_BIT) |
3598c2ecf20Sopenharmony_ci		BIT(ADIS16260_DIAG_STAT_POWER_LOW_BIT),
3608c2ecf20Sopenharmony_ci};
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic void adis16260_stop(void *data)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	adis16260_stop_device(data);
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistatic int adis16260_probe(struct spi_device *spi)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	const struct spi_device_id *id;
3708c2ecf20Sopenharmony_ci	struct adis16260 *adis16260;
3718c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
3728c2ecf20Sopenharmony_ci	int ret;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	id = spi_get_device_id(spi);
3758c2ecf20Sopenharmony_ci	if (!id)
3768c2ecf20Sopenharmony_ci		return -ENODEV;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	/* setup the industrialio driver allocated elements */
3798c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16260));
3808c2ecf20Sopenharmony_ci	if (!indio_dev)
3818c2ecf20Sopenharmony_ci		return -ENOMEM;
3828c2ecf20Sopenharmony_ci	adis16260 = iio_priv(indio_dev);
3838c2ecf20Sopenharmony_ci	/* this is only used for removal purposes */
3848c2ecf20Sopenharmony_ci	spi_set_drvdata(spi, indio_dev);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	adis16260->info = &adis16260_chip_info_table[id->driver_data];
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	indio_dev->name = id->name;
3898c2ecf20Sopenharmony_ci	indio_dev->info = &adis16260_info;
3908c2ecf20Sopenharmony_ci	indio_dev->channels = adis16260->info->channels;
3918c2ecf20Sopenharmony_ci	indio_dev->num_channels = adis16260->info->num_channels;
3928c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	ret = adis_init(&adis16260->adis, indio_dev, spi, &adis16260_data);
3958c2ecf20Sopenharmony_ci	if (ret)
3968c2ecf20Sopenharmony_ci		return ret;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	ret = devm_adis_setup_buffer_and_trigger(&adis16260->adis, indio_dev, NULL);
3998c2ecf20Sopenharmony_ci	if (ret)
4008c2ecf20Sopenharmony_ci		return ret;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/* Get the device into a sane initial state */
4038c2ecf20Sopenharmony_ci	ret = adis_initial_startup(&adis16260->adis);
4048c2ecf20Sopenharmony_ci	if (ret)
4058c2ecf20Sopenharmony_ci		return ret;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	ret = devm_add_action_or_reset(&spi->dev, adis16260_stop, indio_dev);
4088c2ecf20Sopenharmony_ci	if (ret)
4098c2ecf20Sopenharmony_ci		return ret;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	return devm_iio_device_register(&spi->dev, indio_dev);
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci/*
4158c2ecf20Sopenharmony_ci * These parts do not need to be differentiated until someone adds
4168c2ecf20Sopenharmony_ci * support for the on chip filtering.
4178c2ecf20Sopenharmony_ci */
4188c2ecf20Sopenharmony_cistatic const struct spi_device_id adis16260_id[] = {
4198c2ecf20Sopenharmony_ci	{"adis16260", ADIS16260},
4208c2ecf20Sopenharmony_ci	{"adis16265", ADIS16260},
4218c2ecf20Sopenharmony_ci	{"adis16266", ADIS16266},
4228c2ecf20Sopenharmony_ci	{"adis16250", ADIS16260},
4238c2ecf20Sopenharmony_ci	{"adis16255", ADIS16260},
4248c2ecf20Sopenharmony_ci	{"adis16251", ADIS16251},
4258c2ecf20Sopenharmony_ci	{}
4268c2ecf20Sopenharmony_ci};
4278c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, adis16260_id);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic struct spi_driver adis16260_driver = {
4308c2ecf20Sopenharmony_ci	.driver = {
4318c2ecf20Sopenharmony_ci		.name = "adis16260",
4328c2ecf20Sopenharmony_ci	},
4338c2ecf20Sopenharmony_ci	.probe = adis16260_probe,
4348c2ecf20Sopenharmony_ci	.id_table = adis16260_id,
4358c2ecf20Sopenharmony_ci};
4368c2ecf20Sopenharmony_cimodule_spi_driver(adis16260_driver);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ciMODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
4398c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
4408c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
4418c2ecf20Sopenharmony_ciMODULE_IMPORT_NS(IIO_ADISLIB);
442