xref: /openbmc/linux/drivers/iio/gyro/adis16136.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1fda8d26eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29caed0d9SLars-Peter Clausen /*
39caed0d9SLars-Peter Clausen  * ADIS16133/ADIS16135/ADIS16136 gyroscope driver
49caed0d9SLars-Peter Clausen  *
59caed0d9SLars-Peter Clausen  * Copyright 2012 Analog Devices Inc.
69caed0d9SLars-Peter Clausen  *   Author: Lars-Peter Clausen <lars@metafoo.de>
79caed0d9SLars-Peter Clausen  */
89caed0d9SLars-Peter Clausen 
99caed0d9SLars-Peter Clausen #include <linux/device.h>
109caed0d9SLars-Peter Clausen #include <linux/kernel.h>
119caed0d9SLars-Peter Clausen #include <linux/spi/spi.h>
129caed0d9SLars-Peter Clausen #include <linux/sysfs.h>
139caed0d9SLars-Peter Clausen #include <linux/module.h>
149caed0d9SLars-Peter Clausen 
159caed0d9SLars-Peter Clausen #include <linux/iio/iio.h>
169caed0d9SLars-Peter Clausen #include <linux/iio/sysfs.h>
179caed0d9SLars-Peter Clausen #include <linux/iio/imu/adis.h>
189caed0d9SLars-Peter Clausen 
199caed0d9SLars-Peter Clausen #include <linux/debugfs.h>
209caed0d9SLars-Peter Clausen 
219caed0d9SLars-Peter Clausen #define ADIS16136_REG_FLASH_CNT		0x00
229caed0d9SLars-Peter Clausen #define ADIS16136_REG_TEMP_OUT		0x02
239caed0d9SLars-Peter Clausen #define ADIS16136_REG_GYRO_OUT2		0x04
249caed0d9SLars-Peter Clausen #define ADIS16136_REG_GYRO_OUT		0x06
259caed0d9SLars-Peter Clausen #define ADIS16136_REG_GYRO_OFF2		0x08
269caed0d9SLars-Peter Clausen #define ADIS16136_REG_GYRO_OFF		0x0A
279caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_MAG1		0x10
289caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_MAG2		0x12
299caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_SAMPL1	0x14
309caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_SAMPL2	0x16
319caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_CTRL		0x18
329caed0d9SLars-Peter Clausen #define ADIS16136_REG_GPIO_CTRL		0x1A
339caed0d9SLars-Peter Clausen #define ADIS16136_REG_MSC_CTRL		0x1C
349caed0d9SLars-Peter Clausen #define ADIS16136_REG_SMPL_PRD		0x1E
359caed0d9SLars-Peter Clausen #define ADIS16136_REG_AVG_CNT		0x20
369caed0d9SLars-Peter Clausen #define ADIS16136_REG_DEC_RATE		0x22
379caed0d9SLars-Peter Clausen #define ADIS16136_REG_SLP_CTRL		0x24
389caed0d9SLars-Peter Clausen #define ADIS16136_REG_DIAG_STAT		0x26
399caed0d9SLars-Peter Clausen #define ADIS16136_REG_GLOB_CMD		0x28
409caed0d9SLars-Peter Clausen #define ADIS16136_REG_LOT1		0x32
419caed0d9SLars-Peter Clausen #define ADIS16136_REG_LOT2		0x34
429caed0d9SLars-Peter Clausen #define ADIS16136_REG_LOT3		0x36
439caed0d9SLars-Peter Clausen #define ADIS16136_REG_PROD_ID		0x38
449caed0d9SLars-Peter Clausen #define ADIS16136_REG_SERIAL_NUM	0x3A
459caed0d9SLars-Peter Clausen 
469caed0d9SLars-Peter Clausen #define ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL	2
479caed0d9SLars-Peter Clausen #define ADIS16136_DIAG_STAT_SPI_FAIL		3
489caed0d9SLars-Peter Clausen #define ADIS16136_DIAG_STAT_SELF_TEST_FAIL	5
499caed0d9SLars-Peter Clausen #define ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL	6
509caed0d9SLars-Peter Clausen 
519caed0d9SLars-Peter Clausen #define ADIS16136_MSC_CTRL_MEMORY_TEST BIT(11)
529caed0d9SLars-Peter Clausen #define ADIS16136_MSC_CTRL_SELF_TEST BIT(10)
539caed0d9SLars-Peter Clausen 
549caed0d9SLars-Peter Clausen struct adis16136_chip_info {
559caed0d9SLars-Peter Clausen 	unsigned int precision;
569caed0d9SLars-Peter Clausen 	unsigned int fullscale;
57e914cfdfSAlexandru Ardelean 	const struct adis_data adis_data;
589caed0d9SLars-Peter Clausen };
599caed0d9SLars-Peter Clausen 
609caed0d9SLars-Peter Clausen struct adis16136 {
619caed0d9SLars-Peter Clausen 	const struct adis16136_chip_info *chip_info;
629caed0d9SLars-Peter Clausen 
639caed0d9SLars-Peter Clausen 	struct adis adis;
649caed0d9SLars-Peter Clausen };
659caed0d9SLars-Peter Clausen 
669caed0d9SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
679caed0d9SLars-Peter Clausen 
adis16136_show_serial(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)689caed0d9SLars-Peter Clausen static ssize_t adis16136_show_serial(struct file *file,
699caed0d9SLars-Peter Clausen 		char __user *userbuf, size_t count, loff_t *ppos)
709caed0d9SLars-Peter Clausen {
719caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = file->private_data;
729caed0d9SLars-Peter Clausen 	uint16_t lot1, lot2, lot3, serial;
739caed0d9SLars-Peter Clausen 	char buf[20];
749caed0d9SLars-Peter Clausen 	size_t len;
759caed0d9SLars-Peter Clausen 	int ret;
769caed0d9SLars-Peter Clausen 
779caed0d9SLars-Peter Clausen 	ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SERIAL_NUM,
789caed0d9SLars-Peter Clausen 		&serial);
7926ba6db6SAlexandru Ardelean 	if (ret)
809caed0d9SLars-Peter Clausen 		return ret;
819caed0d9SLars-Peter Clausen 
829caed0d9SLars-Peter Clausen 	ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT1, &lot1);
8326ba6db6SAlexandru Ardelean 	if (ret)
849caed0d9SLars-Peter Clausen 		return ret;
859caed0d9SLars-Peter Clausen 
869caed0d9SLars-Peter Clausen 	ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT2, &lot2);
8726ba6db6SAlexandru Ardelean 	if (ret)
889caed0d9SLars-Peter Clausen 		return ret;
899caed0d9SLars-Peter Clausen 
909caed0d9SLars-Peter Clausen 	ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT3, &lot3);
9126ba6db6SAlexandru Ardelean 	if (ret)
929caed0d9SLars-Peter Clausen 		return ret;
939caed0d9SLars-Peter Clausen 
949caed0d9SLars-Peter Clausen 	len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
959caed0d9SLars-Peter Clausen 		lot3, serial);
969caed0d9SLars-Peter Clausen 
979caed0d9SLars-Peter Clausen 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
989caed0d9SLars-Peter Clausen }
999caed0d9SLars-Peter Clausen 
1009caed0d9SLars-Peter Clausen static const struct file_operations adis16136_serial_fops = {
1019caed0d9SLars-Peter Clausen 	.open = simple_open,
1029caed0d9SLars-Peter Clausen 	.read = adis16136_show_serial,
1039caed0d9SLars-Peter Clausen 	.llseek = default_llseek,
1049caed0d9SLars-Peter Clausen 	.owner = THIS_MODULE,
1059caed0d9SLars-Peter Clausen };
1069caed0d9SLars-Peter Clausen 
adis16136_show_product_id(void * arg,u64 * val)1079caed0d9SLars-Peter Clausen static int adis16136_show_product_id(void *arg, u64 *val)
1089caed0d9SLars-Peter Clausen {
1099caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = arg;
1109caed0d9SLars-Peter Clausen 	u16 prod_id;
1119caed0d9SLars-Peter Clausen 	int ret;
1129caed0d9SLars-Peter Clausen 
1139caed0d9SLars-Peter Clausen 	ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID,
1149caed0d9SLars-Peter Clausen 		&prod_id);
11526ba6db6SAlexandru Ardelean 	if (ret)
1169caed0d9SLars-Peter Clausen 		return ret;
1179caed0d9SLars-Peter Clausen 
1189caed0d9SLars-Peter Clausen 	*val = prod_id;
1199caed0d9SLars-Peter Clausen 
1209caed0d9SLars-Peter Clausen 	return 0;
1219caed0d9SLars-Peter Clausen }
1229aaea09bSVenkat Prashanth B U DEFINE_DEBUGFS_ATTRIBUTE(adis16136_product_id_fops,
1239caed0d9SLars-Peter Clausen 	adis16136_show_product_id, NULL, "%llu\n");
1249caed0d9SLars-Peter Clausen 
adis16136_show_flash_count(void * arg,u64 * val)1259caed0d9SLars-Peter Clausen static int adis16136_show_flash_count(void *arg, u64 *val)
1269caed0d9SLars-Peter Clausen {
1279caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = arg;
1289caed0d9SLars-Peter Clausen 	uint16_t flash_count;
1299caed0d9SLars-Peter Clausen 	int ret;
1309caed0d9SLars-Peter Clausen 
1319caed0d9SLars-Peter Clausen 	ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_FLASH_CNT,
1329caed0d9SLars-Peter Clausen 		&flash_count);
13326ba6db6SAlexandru Ardelean 	if (ret)
1349caed0d9SLars-Peter Clausen 		return ret;
1359caed0d9SLars-Peter Clausen 
1369caed0d9SLars-Peter Clausen 	*val = flash_count;
1379caed0d9SLars-Peter Clausen 
1389caed0d9SLars-Peter Clausen 	return 0;
1399caed0d9SLars-Peter Clausen }
1409aaea09bSVenkat Prashanth B U DEFINE_DEBUGFS_ATTRIBUTE(adis16136_flash_count_fops,
1419caed0d9SLars-Peter Clausen 	adis16136_show_flash_count, NULL, "%lld\n");
1429caed0d9SLars-Peter Clausen 
adis16136_debugfs_init(struct iio_dev * indio_dev)1439caed0d9SLars-Peter Clausen static int adis16136_debugfs_init(struct iio_dev *indio_dev)
1449caed0d9SLars-Peter Clausen {
1459caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
146b7190859SAlexandru Ardelean 	struct dentry *d = iio_get_debugfs_dentry(indio_dev);
1479caed0d9SLars-Peter Clausen 
1489aaea09bSVenkat Prashanth B U 	debugfs_create_file_unsafe("serial_number", 0400,
149b7190859SAlexandru Ardelean 		d, adis16136, &adis16136_serial_fops);
1509aaea09bSVenkat Prashanth B U 	debugfs_create_file_unsafe("product_id", 0400,
151b7190859SAlexandru Ardelean 		d, adis16136, &adis16136_product_id_fops);
1529aaea09bSVenkat Prashanth B U 	debugfs_create_file_unsafe("flash_count", 0400,
153b7190859SAlexandru Ardelean 		d, adis16136, &adis16136_flash_count_fops);
1549caed0d9SLars-Peter Clausen 
1559caed0d9SLars-Peter Clausen 	return 0;
1569caed0d9SLars-Peter Clausen }
1579caed0d9SLars-Peter Clausen 
1589caed0d9SLars-Peter Clausen #else
1599caed0d9SLars-Peter Clausen 
adis16136_debugfs_init(struct iio_dev * indio_dev)1609caed0d9SLars-Peter Clausen static int adis16136_debugfs_init(struct iio_dev *indio_dev)
1619caed0d9SLars-Peter Clausen {
1629caed0d9SLars-Peter Clausen 	return 0;
1639caed0d9SLars-Peter Clausen }
1649caed0d9SLars-Peter Clausen 
1659caed0d9SLars-Peter Clausen #endif
1669caed0d9SLars-Peter Clausen 
adis16136_set_freq(struct adis16136 * adis16136,unsigned int freq)1679caed0d9SLars-Peter Clausen static int adis16136_set_freq(struct adis16136 *adis16136, unsigned int freq)
1689caed0d9SLars-Peter Clausen {
1699caed0d9SLars-Peter Clausen 	unsigned int t;
1709caed0d9SLars-Peter Clausen 
1719caed0d9SLars-Peter Clausen 	t = 32768 / freq;
1729caed0d9SLars-Peter Clausen 	if (t < 0xf)
1739caed0d9SLars-Peter Clausen 		t = 0xf;
1749caed0d9SLars-Peter Clausen 	else if (t > 0xffff)
1759caed0d9SLars-Peter Clausen 		t = 0xffff;
1769caed0d9SLars-Peter Clausen 	else
1779caed0d9SLars-Peter Clausen 		t--;
1789caed0d9SLars-Peter Clausen 
1799caed0d9SLars-Peter Clausen 	return adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, t);
1809caed0d9SLars-Peter Clausen }
1819caed0d9SLars-Peter Clausen 
__adis16136_get_freq(struct adis16136 * adis16136,unsigned int * freq)1820aee99a1SAlexandru Ardelean static int __adis16136_get_freq(struct adis16136 *adis16136, unsigned int *freq)
1839caed0d9SLars-Peter Clausen {
1849caed0d9SLars-Peter Clausen 	uint16_t t;
1859caed0d9SLars-Peter Clausen 	int ret;
1869caed0d9SLars-Peter Clausen 
1870aee99a1SAlexandru Ardelean 	ret = __adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, &t);
18826ba6db6SAlexandru Ardelean 	if (ret)
1899caed0d9SLars-Peter Clausen 		return ret;
1909caed0d9SLars-Peter Clausen 
1919caed0d9SLars-Peter Clausen 	*freq = 32768 / (t + 1);
1929caed0d9SLars-Peter Clausen 
1939caed0d9SLars-Peter Clausen 	return 0;
1949caed0d9SLars-Peter Clausen }
1959caed0d9SLars-Peter Clausen 
adis16136_write_frequency(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1969caed0d9SLars-Peter Clausen static ssize_t adis16136_write_frequency(struct device *dev,
1979caed0d9SLars-Peter Clausen 	struct device_attribute *attr, const char *buf, size_t len)
1989caed0d9SLars-Peter Clausen {
1999caed0d9SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
2009caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
20112660138SDan Carpenter 	unsigned int val;
2029caed0d9SLars-Peter Clausen 	int ret;
2039caed0d9SLars-Peter Clausen 
20412660138SDan Carpenter 	ret = kstrtouint(buf, 10, &val);
2059caed0d9SLars-Peter Clausen 	if (ret)
2069caed0d9SLars-Peter Clausen 		return ret;
2079caed0d9SLars-Peter Clausen 
2089caed0d9SLars-Peter Clausen 	if (val == 0)
2099caed0d9SLars-Peter Clausen 		return -EINVAL;
2109caed0d9SLars-Peter Clausen 
2119caed0d9SLars-Peter Clausen 	ret = adis16136_set_freq(adis16136, val);
2129caed0d9SLars-Peter Clausen 
2139caed0d9SLars-Peter Clausen 	return ret ? ret : len;
2149caed0d9SLars-Peter Clausen }
2159caed0d9SLars-Peter Clausen 
adis16136_read_frequency(struct device * dev,struct device_attribute * attr,char * buf)2169caed0d9SLars-Peter Clausen static ssize_t adis16136_read_frequency(struct device *dev,
2179caed0d9SLars-Peter Clausen 	struct device_attribute *attr, char *buf)
2189caed0d9SLars-Peter Clausen {
2199caed0d9SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
2209caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
2219caed0d9SLars-Peter Clausen 	unsigned int freq;
2229caed0d9SLars-Peter Clausen 	int ret;
2239caed0d9SLars-Peter Clausen 
2245d142d41SNuno Sá 	adis_dev_lock(&adis16136->adis);
2250aee99a1SAlexandru Ardelean 	ret = __adis16136_get_freq(adis16136, &freq);
2265d142d41SNuno Sá 	adis_dev_unlock(&adis16136->adis);
22726ba6db6SAlexandru Ardelean 	if (ret)
2289caed0d9SLars-Peter Clausen 		return ret;
2299caed0d9SLars-Peter Clausen 
2309caed0d9SLars-Peter Clausen 	return sprintf(buf, "%d\n", freq);
2319caed0d9SLars-Peter Clausen }
2329caed0d9SLars-Peter Clausen 
2339caed0d9SLars-Peter Clausen static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
2349caed0d9SLars-Peter Clausen 				  adis16136_read_frequency,
2359caed0d9SLars-Peter Clausen 				  adis16136_write_frequency);
2369caed0d9SLars-Peter Clausen 
2379caed0d9SLars-Peter Clausen static const unsigned adis16136_3db_divisors[] = {
2389caed0d9SLars-Peter Clausen 	[0] = 2, /* Special case */
2399caed0d9SLars-Peter Clausen 	[1] = 6,
2409caed0d9SLars-Peter Clausen 	[2] = 12,
2419caed0d9SLars-Peter Clausen 	[3] = 25,
2429caed0d9SLars-Peter Clausen 	[4] = 50,
2439caed0d9SLars-Peter Clausen 	[5] = 100,
2449caed0d9SLars-Peter Clausen 	[6] = 200,
2459caed0d9SLars-Peter Clausen 	[7] = 200, /* Not a valid setting */
2469caed0d9SLars-Peter Clausen };
2479caed0d9SLars-Peter Clausen 
adis16136_set_filter(struct iio_dev * indio_dev,int val)2489caed0d9SLars-Peter Clausen static int adis16136_set_filter(struct iio_dev *indio_dev, int val)
2499caed0d9SLars-Peter Clausen {
2509caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
2519caed0d9SLars-Peter Clausen 	unsigned int freq;
2529caed0d9SLars-Peter Clausen 	int i, ret;
2539caed0d9SLars-Peter Clausen 
2545d142d41SNuno Sá 	adis_dev_lock(&adis16136->adis);
2550aee99a1SAlexandru Ardelean 	ret = __adis16136_get_freq(adis16136, &freq);
25626ba6db6SAlexandru Ardelean 	if (ret)
2570aee99a1SAlexandru Ardelean 		goto out_unlock;
2589caed0d9SLars-Peter Clausen 
2599caed0d9SLars-Peter Clausen 	for (i = ARRAY_SIZE(adis16136_3db_divisors) - 1; i >= 1; i--) {
2609caed0d9SLars-Peter Clausen 		if (freq / adis16136_3db_divisors[i] >= val)
2619caed0d9SLars-Peter Clausen 			break;
2629caed0d9SLars-Peter Clausen 	}
2639caed0d9SLars-Peter Clausen 
2640aee99a1SAlexandru Ardelean 	ret = __adis_write_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, i);
2650aee99a1SAlexandru Ardelean out_unlock:
2665d142d41SNuno Sá 	adis_dev_unlock(&adis16136->adis);
2670aee99a1SAlexandru Ardelean 
2680aee99a1SAlexandru Ardelean 	return ret;
2699caed0d9SLars-Peter Clausen }
2709caed0d9SLars-Peter Clausen 
adis16136_get_filter(struct iio_dev * indio_dev,int * val)2719caed0d9SLars-Peter Clausen static int adis16136_get_filter(struct iio_dev *indio_dev, int *val)
2729caed0d9SLars-Peter Clausen {
2739caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
2749caed0d9SLars-Peter Clausen 	unsigned int freq;
2759caed0d9SLars-Peter Clausen 	uint16_t val16;
2769caed0d9SLars-Peter Clausen 	int ret;
2779caed0d9SLars-Peter Clausen 
2785d142d41SNuno Sá 	adis_dev_lock(&adis16136->adis);
2799caed0d9SLars-Peter Clausen 
2800aee99a1SAlexandru Ardelean 	ret = __adis_read_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT,
2810aee99a1SAlexandru Ardelean 				 &val16);
28226ba6db6SAlexandru Ardelean 	if (ret)
2839caed0d9SLars-Peter Clausen 		goto err_unlock;
2849caed0d9SLars-Peter Clausen 
2850aee99a1SAlexandru Ardelean 	ret = __adis16136_get_freq(adis16136, &freq);
28626ba6db6SAlexandru Ardelean 	if (ret)
2879caed0d9SLars-Peter Clausen 		goto err_unlock;
2889caed0d9SLars-Peter Clausen 
2899caed0d9SLars-Peter Clausen 	*val = freq / adis16136_3db_divisors[val16 & 0x07];
2909caed0d9SLars-Peter Clausen 
2919caed0d9SLars-Peter Clausen err_unlock:
2925d142d41SNuno Sá 	adis_dev_unlock(&adis16136->adis);
2939caed0d9SLars-Peter Clausen 
2949caed0d9SLars-Peter Clausen 	return ret ? ret : IIO_VAL_INT;
2959caed0d9SLars-Peter Clausen }
2969caed0d9SLars-Peter Clausen 
adis16136_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long info)2979caed0d9SLars-Peter Clausen static int adis16136_read_raw(struct iio_dev *indio_dev,
2989caed0d9SLars-Peter Clausen 	const struct iio_chan_spec *chan, int *val, int *val2, long info)
2999caed0d9SLars-Peter Clausen {
3009caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
3019caed0d9SLars-Peter Clausen 	uint32_t val32;
3029caed0d9SLars-Peter Clausen 	int ret;
3039caed0d9SLars-Peter Clausen 
3049caed0d9SLars-Peter Clausen 	switch (info) {
3059caed0d9SLars-Peter Clausen 	case IIO_CHAN_INFO_RAW:
3069caed0d9SLars-Peter Clausen 		return adis_single_conversion(indio_dev, chan, 0, val);
3079caed0d9SLars-Peter Clausen 	case IIO_CHAN_INFO_SCALE:
3089caed0d9SLars-Peter Clausen 		switch (chan->type) {
3099caed0d9SLars-Peter Clausen 		case IIO_ANGL_VEL:
3109caed0d9SLars-Peter Clausen 			*val = adis16136->chip_info->precision;
3119caed0d9SLars-Peter Clausen 			*val2 = (adis16136->chip_info->fullscale << 16);
3129caed0d9SLars-Peter Clausen 			return IIO_VAL_FRACTIONAL;
3139caed0d9SLars-Peter Clausen 		case IIO_TEMP:
3149caed0d9SLars-Peter Clausen 			*val = 10;
3159caed0d9SLars-Peter Clausen 			*val2 = 697000; /* 0.010697 degree Celsius */
3169caed0d9SLars-Peter Clausen 			return IIO_VAL_INT_PLUS_MICRO;
3179caed0d9SLars-Peter Clausen 		default:
3189caed0d9SLars-Peter Clausen 			return -EINVAL;
3199caed0d9SLars-Peter Clausen 		}
3209caed0d9SLars-Peter Clausen 	case IIO_CHAN_INFO_CALIBBIAS:
3219caed0d9SLars-Peter Clausen 		ret = adis_read_reg_32(&adis16136->adis,
3229caed0d9SLars-Peter Clausen 			ADIS16136_REG_GYRO_OFF2, &val32);
32326ba6db6SAlexandru Ardelean 		if (ret)
3249caed0d9SLars-Peter Clausen 			return ret;
3259caed0d9SLars-Peter Clausen 
3269caed0d9SLars-Peter Clausen 		*val = sign_extend32(val32, 31);
3279caed0d9SLars-Peter Clausen 
3289caed0d9SLars-Peter Clausen 		return IIO_VAL_INT;
3299caed0d9SLars-Peter Clausen 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
3309caed0d9SLars-Peter Clausen 		return adis16136_get_filter(indio_dev, val);
3319caed0d9SLars-Peter Clausen 	default:
3329caed0d9SLars-Peter Clausen 		return -EINVAL;
3339caed0d9SLars-Peter Clausen 	}
3349caed0d9SLars-Peter Clausen }
3359caed0d9SLars-Peter Clausen 
adis16136_write_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int val,int val2,long info)3369caed0d9SLars-Peter Clausen static int adis16136_write_raw(struct iio_dev *indio_dev,
3379caed0d9SLars-Peter Clausen 	const struct iio_chan_spec *chan, int val, int val2, long info)
3389caed0d9SLars-Peter Clausen {
3399caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
3409caed0d9SLars-Peter Clausen 
3419caed0d9SLars-Peter Clausen 	switch (info) {
3429caed0d9SLars-Peter Clausen 	case IIO_CHAN_INFO_CALIBBIAS:
3439caed0d9SLars-Peter Clausen 		return adis_write_reg_32(&adis16136->adis,
3449caed0d9SLars-Peter Clausen 			ADIS16136_REG_GYRO_OFF2, val);
3459caed0d9SLars-Peter Clausen 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
3469caed0d9SLars-Peter Clausen 		return adis16136_set_filter(indio_dev, val);
3479caed0d9SLars-Peter Clausen 	default:
3489caed0d9SLars-Peter Clausen 		break;
3499caed0d9SLars-Peter Clausen 	}
3509caed0d9SLars-Peter Clausen 
3519caed0d9SLars-Peter Clausen 	return -EINVAL;
3529caed0d9SLars-Peter Clausen }
3539caed0d9SLars-Peter Clausen 
3549caed0d9SLars-Peter Clausen enum {
3559caed0d9SLars-Peter Clausen 	ADIS16136_SCAN_GYRO,
3569caed0d9SLars-Peter Clausen 	ADIS16136_SCAN_TEMP,
3579caed0d9SLars-Peter Clausen };
3589caed0d9SLars-Peter Clausen 
3599caed0d9SLars-Peter Clausen static const struct iio_chan_spec adis16136_channels[] = {
3609caed0d9SLars-Peter Clausen 	{
3619caed0d9SLars-Peter Clausen 		.type = IIO_ANGL_VEL,
3629caed0d9SLars-Peter Clausen 		.modified = 1,
3639caed0d9SLars-Peter Clausen 		.channel2 = IIO_MOD_X,
364606f9067SJonathan Cameron 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
365606f9067SJonathan Cameron 			BIT(IIO_CHAN_INFO_CALIBBIAS) |
366606f9067SJonathan Cameron 			BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
367606f9067SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
368606f9067SJonathan Cameron 
3699caed0d9SLars-Peter Clausen 		.address = ADIS16136_REG_GYRO_OUT2,
3709caed0d9SLars-Peter Clausen 		.scan_index = ADIS16136_SCAN_GYRO,
3719caed0d9SLars-Peter Clausen 		.scan_type = {
3729caed0d9SLars-Peter Clausen 			.sign = 's',
3739caed0d9SLars-Peter Clausen 			.realbits = 32,
3749caed0d9SLars-Peter Clausen 			.storagebits = 32,
3759caed0d9SLars-Peter Clausen 			.endianness = IIO_BE,
3769caed0d9SLars-Peter Clausen 		},
3779caed0d9SLars-Peter Clausen 	}, {
3789caed0d9SLars-Peter Clausen 		.type = IIO_TEMP,
3799caed0d9SLars-Peter Clausen 		.indexed = 1,
3809caed0d9SLars-Peter Clausen 		.channel = 0,
381606f9067SJonathan Cameron 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
382606f9067SJonathan Cameron 			BIT(IIO_CHAN_INFO_SCALE),
3839caed0d9SLars-Peter Clausen 		.address = ADIS16136_REG_TEMP_OUT,
3849caed0d9SLars-Peter Clausen 		.scan_index = ADIS16136_SCAN_TEMP,
3859caed0d9SLars-Peter Clausen 		.scan_type = {
3869caed0d9SLars-Peter Clausen 			.sign = 's',
3879caed0d9SLars-Peter Clausen 			.realbits = 16,
3889caed0d9SLars-Peter Clausen 			.storagebits = 16,
3899caed0d9SLars-Peter Clausen 			.endianness = IIO_BE,
3909caed0d9SLars-Peter Clausen 		},
3919caed0d9SLars-Peter Clausen 	},
3929caed0d9SLars-Peter Clausen 	IIO_CHAN_SOFT_TIMESTAMP(2),
3939caed0d9SLars-Peter Clausen };
3949caed0d9SLars-Peter Clausen 
3959caed0d9SLars-Peter Clausen static struct attribute *adis16136_attributes[] = {
3969caed0d9SLars-Peter Clausen 	&iio_dev_attr_sampling_frequency.dev_attr.attr,
3979caed0d9SLars-Peter Clausen 	NULL
3989caed0d9SLars-Peter Clausen };
3999caed0d9SLars-Peter Clausen 
4009caed0d9SLars-Peter Clausen static const struct attribute_group adis16136_attribute_group = {
4019caed0d9SLars-Peter Clausen 	.attrs = adis16136_attributes,
4029caed0d9SLars-Peter Clausen };
4039caed0d9SLars-Peter Clausen 
4049caed0d9SLars-Peter Clausen static const struct iio_info adis16136_info = {
4059caed0d9SLars-Peter Clausen 	.attrs = &adis16136_attribute_group,
4069caed0d9SLars-Peter Clausen 	.read_raw = &adis16136_read_raw,
4079caed0d9SLars-Peter Clausen 	.write_raw = &adis16136_write_raw,
4089caed0d9SLars-Peter Clausen 	.update_scan_mode = adis_update_scan_mode,
4099caed0d9SLars-Peter Clausen 	.debugfs_reg_access = adis_debugfs_reg_access,
4109caed0d9SLars-Peter Clausen };
4119caed0d9SLars-Peter Clausen 
adis16136_stop_device(struct iio_dev * indio_dev)4129caed0d9SLars-Peter Clausen static int adis16136_stop_device(struct iio_dev *indio_dev)
4139caed0d9SLars-Peter Clausen {
4149caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
4159caed0d9SLars-Peter Clausen 	int ret;
4169caed0d9SLars-Peter Clausen 
4179caed0d9SLars-Peter Clausen 	ret = adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SLP_CTRL, 0xff);
4189caed0d9SLars-Peter Clausen 	if (ret)
4199caed0d9SLars-Peter Clausen 		dev_err(&indio_dev->dev,
4209caed0d9SLars-Peter Clausen 			"Could not power down device: %d\n", ret);
4219caed0d9SLars-Peter Clausen 
4229caed0d9SLars-Peter Clausen 	return ret;
4239caed0d9SLars-Peter Clausen }
4249caed0d9SLars-Peter Clausen 
adis16136_initial_setup(struct iio_dev * indio_dev)4259caed0d9SLars-Peter Clausen static int adis16136_initial_setup(struct iio_dev *indio_dev)
4269caed0d9SLars-Peter Clausen {
4279caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136 = iio_priv(indio_dev);
4289caed0d9SLars-Peter Clausen 	unsigned int device_id;
4299caed0d9SLars-Peter Clausen 	uint16_t prod_id;
4309caed0d9SLars-Peter Clausen 	int ret;
4319caed0d9SLars-Peter Clausen 
432*c5de7d4cSRamona Bolboaca 	ret = __adis_initial_startup(&adis16136->adis);
4339caed0d9SLars-Peter Clausen 	if (ret)
4349caed0d9SLars-Peter Clausen 		return ret;
4359caed0d9SLars-Peter Clausen 
4369caed0d9SLars-Peter Clausen 	ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID,
4379caed0d9SLars-Peter Clausen 		&prod_id);
4389caed0d9SLars-Peter Clausen 	if (ret)
4399caed0d9SLars-Peter Clausen 		return ret;
4409caed0d9SLars-Peter Clausen 
441a106b474SIoana Ciornei 	ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
442a106b474SIoana Ciornei 	if (ret != 1)
443a106b474SIoana Ciornei 		return -EINVAL;
4449caed0d9SLars-Peter Clausen 
4459caed0d9SLars-Peter Clausen 	if (prod_id != device_id)
4469caed0d9SLars-Peter Clausen 		dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
4479caed0d9SLars-Peter Clausen 				device_id, prod_id);
4489caed0d9SLars-Peter Clausen 
4499caed0d9SLars-Peter Clausen 	return 0;
4509caed0d9SLars-Peter Clausen }
4519caed0d9SLars-Peter Clausen 
4529caed0d9SLars-Peter Clausen static const char * const adis16136_status_error_msgs[] = {
4539caed0d9SLars-Peter Clausen 	[ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL] = "Flash update failed",
4549caed0d9SLars-Peter Clausen 	[ADIS16136_DIAG_STAT_SPI_FAIL] = "SPI failure",
4559caed0d9SLars-Peter Clausen 	[ADIS16136_DIAG_STAT_SELF_TEST_FAIL] = "Self test error",
4569caed0d9SLars-Peter Clausen 	[ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL] = "Flash checksum error",
4579caed0d9SLars-Peter Clausen };
4589caed0d9SLars-Peter Clausen 
459e914cfdfSAlexandru Ardelean #define ADIS16136_DATA(_timeouts)					\
460e914cfdfSAlexandru Ardelean {									\
461e914cfdfSAlexandru Ardelean 	.diag_stat_reg = ADIS16136_REG_DIAG_STAT,			\
462e914cfdfSAlexandru Ardelean 	.glob_cmd_reg = ADIS16136_REG_GLOB_CMD,				\
463e914cfdfSAlexandru Ardelean 	.msc_ctrl_reg = ADIS16136_REG_MSC_CTRL,				\
464fdcf6bbbSNuno Sá 	.self_test_reg = ADIS16136_REG_MSC_CTRL,			\
465e914cfdfSAlexandru Ardelean 	.self_test_mask = ADIS16136_MSC_CTRL_SELF_TEST,			\
466e914cfdfSAlexandru Ardelean 	.read_delay = 10,						\
467e914cfdfSAlexandru Ardelean 	.write_delay = 10,						\
468e914cfdfSAlexandru Ardelean 	.status_error_msgs = adis16136_status_error_msgs,		\
469e914cfdfSAlexandru Ardelean 	.status_error_mask = BIT(ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL) |	\
470e914cfdfSAlexandru Ardelean 		BIT(ADIS16136_DIAG_STAT_SPI_FAIL) |			\
471e914cfdfSAlexandru Ardelean 		BIT(ADIS16136_DIAG_STAT_SELF_TEST_FAIL) |		\
472e914cfdfSAlexandru Ardelean 		BIT(ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL),		\
473e914cfdfSAlexandru Ardelean 	.timeouts = (_timeouts),					\
474e914cfdfSAlexandru Ardelean }
4759caed0d9SLars-Peter Clausen 
4769caed0d9SLars-Peter Clausen enum adis16136_id {
4779caed0d9SLars-Peter Clausen 	ID_ADIS16133,
4789caed0d9SLars-Peter Clausen 	ID_ADIS16135,
4799caed0d9SLars-Peter Clausen 	ID_ADIS16136,
4805450c360SLars-Peter Clausen 	ID_ADIS16137,
4819caed0d9SLars-Peter Clausen };
4829caed0d9SLars-Peter Clausen 
483380b107bSNuno Sá static const struct adis_timeout adis16133_timeouts = {
484380b107bSNuno Sá 	.reset_ms = 75,
485380b107bSNuno Sá 	.sw_reset_ms = 75,
486380b107bSNuno Sá 	.self_test_ms = 50,
487380b107bSNuno Sá };
488380b107bSNuno Sá 
489380b107bSNuno Sá static const struct adis_timeout adis16136_timeouts = {
490380b107bSNuno Sá 	.reset_ms = 128,
491380b107bSNuno Sá 	.sw_reset_ms = 75,
492380b107bSNuno Sá 	.self_test_ms = 245,
493380b107bSNuno Sá };
494380b107bSNuno Sá 
4959caed0d9SLars-Peter Clausen static const struct adis16136_chip_info adis16136_chip_info[] = {
4969caed0d9SLars-Peter Clausen 	[ID_ADIS16133] = {
4979caed0d9SLars-Peter Clausen 		.precision = IIO_DEGREE_TO_RAD(1200),
4989caed0d9SLars-Peter Clausen 		.fullscale = 24000,
499e914cfdfSAlexandru Ardelean 		.adis_data = ADIS16136_DATA(&adis16133_timeouts),
5009caed0d9SLars-Peter Clausen 	},
5019caed0d9SLars-Peter Clausen 	[ID_ADIS16135] = {
5029caed0d9SLars-Peter Clausen 		.precision = IIO_DEGREE_TO_RAD(300),
5039caed0d9SLars-Peter Clausen 		.fullscale = 24000,
504e914cfdfSAlexandru Ardelean 		.adis_data = ADIS16136_DATA(&adis16133_timeouts),
5059caed0d9SLars-Peter Clausen 	},
5069caed0d9SLars-Peter Clausen 	[ID_ADIS16136] = {
5079caed0d9SLars-Peter Clausen 		.precision = IIO_DEGREE_TO_RAD(450),
5089caed0d9SLars-Peter Clausen 		.fullscale = 24623,
509e914cfdfSAlexandru Ardelean 		.adis_data = ADIS16136_DATA(&adis16136_timeouts),
5109caed0d9SLars-Peter Clausen 	},
5115450c360SLars-Peter Clausen 	[ID_ADIS16137] = {
5125450c360SLars-Peter Clausen 		.precision = IIO_DEGREE_TO_RAD(1000),
5135450c360SLars-Peter Clausen 		.fullscale = 24609,
514e914cfdfSAlexandru Ardelean 		.adis_data = ADIS16136_DATA(&adis16136_timeouts),
5155450c360SLars-Peter Clausen 	},
5169caed0d9SLars-Peter Clausen };
5179caed0d9SLars-Peter Clausen 
adis16136_stop(void * data)51877802e09SNuno Sá static void adis16136_stop(void *data)
51977802e09SNuno Sá {
52077802e09SNuno Sá 	adis16136_stop_device(data);
52177802e09SNuno Sá }
52277802e09SNuno Sá 
adis16136_probe(struct spi_device * spi)5239caed0d9SLars-Peter Clausen static int adis16136_probe(struct spi_device *spi)
5249caed0d9SLars-Peter Clausen {
5259caed0d9SLars-Peter Clausen 	const struct spi_device_id *id = spi_get_device_id(spi);
5269caed0d9SLars-Peter Clausen 	struct adis16136 *adis16136;
5279caed0d9SLars-Peter Clausen 	struct iio_dev *indio_dev;
528380b107bSNuno Sá 	const struct adis_data *adis16136_data;
5299caed0d9SLars-Peter Clausen 	int ret;
5309caed0d9SLars-Peter Clausen 
531c0ca6d31SSachin Kamat 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16136));
5329caed0d9SLars-Peter Clausen 	if (indio_dev == NULL)
5339caed0d9SLars-Peter Clausen 		return -ENOMEM;
5349caed0d9SLars-Peter Clausen 
5359caed0d9SLars-Peter Clausen 	spi_set_drvdata(spi, indio_dev);
5369caed0d9SLars-Peter Clausen 
5379caed0d9SLars-Peter Clausen 	adis16136 = iio_priv(indio_dev);
5389caed0d9SLars-Peter Clausen 
5399caed0d9SLars-Peter Clausen 	adis16136->chip_info = &adis16136_chip_info[id->driver_data];
5409caed0d9SLars-Peter Clausen 	indio_dev->name = spi_get_device_id(spi)->name;
5419caed0d9SLars-Peter Clausen 	indio_dev->channels = adis16136_channels;
5429caed0d9SLars-Peter Clausen 	indio_dev->num_channels = ARRAY_SIZE(adis16136_channels);
5439caed0d9SLars-Peter Clausen 	indio_dev->info = &adis16136_info;
5449caed0d9SLars-Peter Clausen 	indio_dev->modes = INDIO_DIRECT_MODE;
5459caed0d9SLars-Peter Clausen 
546e914cfdfSAlexandru Ardelean 	adis16136_data = &adis16136->chip_info->adis_data;
547380b107bSNuno Sá 
548380b107bSNuno Sá 	ret = adis_init(&adis16136->adis, indio_dev, spi, adis16136_data);
5499caed0d9SLars-Peter Clausen 	if (ret)
550c0ca6d31SSachin Kamat 		return ret;
5519caed0d9SLars-Peter Clausen 
55277802e09SNuno Sá 	ret = devm_adis_setup_buffer_and_trigger(&adis16136->adis, indio_dev, NULL);
5539caed0d9SLars-Peter Clausen 	if (ret)
554c0ca6d31SSachin Kamat 		return ret;
5559caed0d9SLars-Peter Clausen 
5569caed0d9SLars-Peter Clausen 	ret = adis16136_initial_setup(indio_dev);
5579caed0d9SLars-Peter Clausen 	if (ret)
55877802e09SNuno Sá 		return ret;
5599caed0d9SLars-Peter Clausen 
56077802e09SNuno Sá 	ret = devm_add_action_or_reset(&spi->dev, adis16136_stop, indio_dev);
5619caed0d9SLars-Peter Clausen 	if (ret)
56277802e09SNuno Sá 		return ret;
56377802e09SNuno Sá 
56477802e09SNuno Sá 	ret = devm_iio_device_register(&spi->dev, indio_dev);
56577802e09SNuno Sá 	if (ret)
56677802e09SNuno Sá 		return ret;
5679caed0d9SLars-Peter Clausen 
5689caed0d9SLars-Peter Clausen 	adis16136_debugfs_init(indio_dev);
5699caed0d9SLars-Peter Clausen 
5709caed0d9SLars-Peter Clausen 	return 0;
5719caed0d9SLars-Peter Clausen }
5729caed0d9SLars-Peter Clausen 
5739caed0d9SLars-Peter Clausen static const struct spi_device_id adis16136_ids[] = {
5749caed0d9SLars-Peter Clausen 	{ "adis16133", ID_ADIS16133 },
5759caed0d9SLars-Peter Clausen 	{ "adis16135", ID_ADIS16135 },
5769caed0d9SLars-Peter Clausen 	{ "adis16136", ID_ADIS16136 },
5775450c360SLars-Peter Clausen 	{ "adis16137", ID_ADIS16137 },
5789caed0d9SLars-Peter Clausen 	{ }
5799caed0d9SLars-Peter Clausen };
5809caed0d9SLars-Peter Clausen MODULE_DEVICE_TABLE(spi, adis16136_ids);
5819caed0d9SLars-Peter Clausen 
5829caed0d9SLars-Peter Clausen static struct spi_driver adis16136_driver = {
5839caed0d9SLars-Peter Clausen 	.driver = {
5849caed0d9SLars-Peter Clausen 		.name = "adis16136",
5859caed0d9SLars-Peter Clausen 	},
5869caed0d9SLars-Peter Clausen 	.id_table = adis16136_ids,
5879caed0d9SLars-Peter Clausen 	.probe = adis16136_probe,
5889caed0d9SLars-Peter Clausen };
5899caed0d9SLars-Peter Clausen module_spi_driver(adis16136_driver);
5909caed0d9SLars-Peter Clausen 
5919caed0d9SLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
5929caed0d9SLars-Peter Clausen MODULE_DESCRIPTION("Analog Devices ADIS16133/ADIS16135/ADIS16136 gyroscope driver");
5939caed0d9SLars-Peter Clausen MODULE_LICENSE("GPL v2");
5946c9304d6SJonathan Cameron MODULE_IMPORT_NS(IIO_ADISLIB);
595