1*fda8d26eSThomas 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/interrupt.h> 109caed0d9SLars-Peter Clausen #include <linux/delay.h> 119caed0d9SLars-Peter Clausen #include <linux/mutex.h> 129caed0d9SLars-Peter Clausen #include <linux/device.h> 139caed0d9SLars-Peter Clausen #include <linux/kernel.h> 149caed0d9SLars-Peter Clausen #include <linux/spi/spi.h> 159caed0d9SLars-Peter Clausen #include <linux/slab.h> 169caed0d9SLars-Peter Clausen #include <linux/sysfs.h> 179caed0d9SLars-Peter Clausen #include <linux/module.h> 189caed0d9SLars-Peter Clausen 199caed0d9SLars-Peter Clausen #include <linux/iio/iio.h> 209caed0d9SLars-Peter Clausen #include <linux/iio/sysfs.h> 219caed0d9SLars-Peter Clausen #include <linux/iio/buffer.h> 229caed0d9SLars-Peter Clausen #include <linux/iio/imu/adis.h> 239caed0d9SLars-Peter Clausen 249caed0d9SLars-Peter Clausen #include <linux/debugfs.h> 259caed0d9SLars-Peter Clausen 269caed0d9SLars-Peter Clausen #define ADIS16136_REG_FLASH_CNT 0x00 279caed0d9SLars-Peter Clausen #define ADIS16136_REG_TEMP_OUT 0x02 289caed0d9SLars-Peter Clausen #define ADIS16136_REG_GYRO_OUT2 0x04 299caed0d9SLars-Peter Clausen #define ADIS16136_REG_GYRO_OUT 0x06 309caed0d9SLars-Peter Clausen #define ADIS16136_REG_GYRO_OFF2 0x08 319caed0d9SLars-Peter Clausen #define ADIS16136_REG_GYRO_OFF 0x0A 329caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_MAG1 0x10 339caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_MAG2 0x12 349caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_SAMPL1 0x14 359caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_SAMPL2 0x16 369caed0d9SLars-Peter Clausen #define ADIS16136_REG_ALM_CTRL 0x18 379caed0d9SLars-Peter Clausen #define ADIS16136_REG_GPIO_CTRL 0x1A 389caed0d9SLars-Peter Clausen #define ADIS16136_REG_MSC_CTRL 0x1C 399caed0d9SLars-Peter Clausen #define ADIS16136_REG_SMPL_PRD 0x1E 409caed0d9SLars-Peter Clausen #define ADIS16136_REG_AVG_CNT 0x20 419caed0d9SLars-Peter Clausen #define ADIS16136_REG_DEC_RATE 0x22 429caed0d9SLars-Peter Clausen #define ADIS16136_REG_SLP_CTRL 0x24 439caed0d9SLars-Peter Clausen #define ADIS16136_REG_DIAG_STAT 0x26 449caed0d9SLars-Peter Clausen #define ADIS16136_REG_GLOB_CMD 0x28 459caed0d9SLars-Peter Clausen #define ADIS16136_REG_LOT1 0x32 469caed0d9SLars-Peter Clausen #define ADIS16136_REG_LOT2 0x34 479caed0d9SLars-Peter Clausen #define ADIS16136_REG_LOT3 0x36 489caed0d9SLars-Peter Clausen #define ADIS16136_REG_PROD_ID 0x38 499caed0d9SLars-Peter Clausen #define ADIS16136_REG_SERIAL_NUM 0x3A 509caed0d9SLars-Peter Clausen 519caed0d9SLars-Peter Clausen #define ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL 2 529caed0d9SLars-Peter Clausen #define ADIS16136_DIAG_STAT_SPI_FAIL 3 539caed0d9SLars-Peter Clausen #define ADIS16136_DIAG_STAT_SELF_TEST_FAIL 5 549caed0d9SLars-Peter Clausen #define ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL 6 559caed0d9SLars-Peter Clausen 569caed0d9SLars-Peter Clausen #define ADIS16136_MSC_CTRL_MEMORY_TEST BIT(11) 579caed0d9SLars-Peter Clausen #define ADIS16136_MSC_CTRL_SELF_TEST BIT(10) 589caed0d9SLars-Peter Clausen 599caed0d9SLars-Peter Clausen struct adis16136_chip_info { 609caed0d9SLars-Peter Clausen unsigned int precision; 619caed0d9SLars-Peter Clausen unsigned int fullscale; 629caed0d9SLars-Peter Clausen }; 639caed0d9SLars-Peter Clausen 649caed0d9SLars-Peter Clausen struct adis16136 { 659caed0d9SLars-Peter Clausen const struct adis16136_chip_info *chip_info; 669caed0d9SLars-Peter Clausen 679caed0d9SLars-Peter Clausen struct adis adis; 689caed0d9SLars-Peter Clausen }; 699caed0d9SLars-Peter Clausen 709caed0d9SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 719caed0d9SLars-Peter Clausen 729caed0d9SLars-Peter Clausen static ssize_t adis16136_show_serial(struct file *file, 739caed0d9SLars-Peter Clausen char __user *userbuf, size_t count, loff_t *ppos) 749caed0d9SLars-Peter Clausen { 759caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = file->private_data; 769caed0d9SLars-Peter Clausen uint16_t lot1, lot2, lot3, serial; 779caed0d9SLars-Peter Clausen char buf[20]; 789caed0d9SLars-Peter Clausen size_t len; 799caed0d9SLars-Peter Clausen int ret; 809caed0d9SLars-Peter Clausen 819caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SERIAL_NUM, 829caed0d9SLars-Peter Clausen &serial); 839caed0d9SLars-Peter Clausen if (ret < 0) 849caed0d9SLars-Peter Clausen return ret; 859caed0d9SLars-Peter Clausen 869caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT1, &lot1); 879caed0d9SLars-Peter Clausen if (ret < 0) 889caed0d9SLars-Peter Clausen return ret; 899caed0d9SLars-Peter Clausen 909caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT2, &lot2); 919caed0d9SLars-Peter Clausen if (ret < 0) 929caed0d9SLars-Peter Clausen return ret; 939caed0d9SLars-Peter Clausen 949caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT3, &lot3); 959caed0d9SLars-Peter Clausen if (ret < 0) 969caed0d9SLars-Peter Clausen return ret; 979caed0d9SLars-Peter Clausen 989caed0d9SLars-Peter Clausen len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2, 999caed0d9SLars-Peter Clausen lot3, serial); 1009caed0d9SLars-Peter Clausen 1019caed0d9SLars-Peter Clausen return simple_read_from_buffer(userbuf, count, ppos, buf, len); 1029caed0d9SLars-Peter Clausen } 1039caed0d9SLars-Peter Clausen 1049caed0d9SLars-Peter Clausen static const struct file_operations adis16136_serial_fops = { 1059caed0d9SLars-Peter Clausen .open = simple_open, 1069caed0d9SLars-Peter Clausen .read = adis16136_show_serial, 1079caed0d9SLars-Peter Clausen .llseek = default_llseek, 1089caed0d9SLars-Peter Clausen .owner = THIS_MODULE, 1099caed0d9SLars-Peter Clausen }; 1109caed0d9SLars-Peter Clausen 1119caed0d9SLars-Peter Clausen static int adis16136_show_product_id(void *arg, u64 *val) 1129caed0d9SLars-Peter Clausen { 1139caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = arg; 1149caed0d9SLars-Peter Clausen u16 prod_id; 1159caed0d9SLars-Peter Clausen int ret; 1169caed0d9SLars-Peter Clausen 1179caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID, 1189caed0d9SLars-Peter Clausen &prod_id); 1199caed0d9SLars-Peter Clausen if (ret < 0) 1209caed0d9SLars-Peter Clausen return ret; 1219caed0d9SLars-Peter Clausen 1229caed0d9SLars-Peter Clausen *val = prod_id; 1239caed0d9SLars-Peter Clausen 1249caed0d9SLars-Peter Clausen return 0; 1259caed0d9SLars-Peter Clausen } 1269aaea09bSVenkat Prashanth B U DEFINE_DEBUGFS_ATTRIBUTE(adis16136_product_id_fops, 1279caed0d9SLars-Peter Clausen adis16136_show_product_id, NULL, "%llu\n"); 1289caed0d9SLars-Peter Clausen 1299caed0d9SLars-Peter Clausen static int adis16136_show_flash_count(void *arg, u64 *val) 1309caed0d9SLars-Peter Clausen { 1319caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = arg; 1329caed0d9SLars-Peter Clausen uint16_t flash_count; 1339caed0d9SLars-Peter Clausen int ret; 1349caed0d9SLars-Peter Clausen 1359caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_FLASH_CNT, 1369caed0d9SLars-Peter Clausen &flash_count); 1379caed0d9SLars-Peter Clausen if (ret < 0) 1389caed0d9SLars-Peter Clausen return ret; 1399caed0d9SLars-Peter Clausen 1409caed0d9SLars-Peter Clausen *val = flash_count; 1419caed0d9SLars-Peter Clausen 1429caed0d9SLars-Peter Clausen return 0; 1439caed0d9SLars-Peter Clausen } 1449aaea09bSVenkat Prashanth B U DEFINE_DEBUGFS_ATTRIBUTE(adis16136_flash_count_fops, 1459caed0d9SLars-Peter Clausen adis16136_show_flash_count, NULL, "%lld\n"); 1469caed0d9SLars-Peter Clausen 1479caed0d9SLars-Peter Clausen static int adis16136_debugfs_init(struct iio_dev *indio_dev) 1489caed0d9SLars-Peter Clausen { 1499caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 1509caed0d9SLars-Peter Clausen 1519aaea09bSVenkat Prashanth B U debugfs_create_file_unsafe("serial_number", 0400, 1529aaea09bSVenkat Prashanth B U indio_dev->debugfs_dentry, adis16136, 1539aaea09bSVenkat Prashanth B U &adis16136_serial_fops); 1549aaea09bSVenkat Prashanth B U debugfs_create_file_unsafe("product_id", 0400, 1559aaea09bSVenkat Prashanth B U indio_dev->debugfs_dentry, 1569caed0d9SLars-Peter Clausen adis16136, &adis16136_product_id_fops); 1579aaea09bSVenkat Prashanth B U debugfs_create_file_unsafe("flash_count", 0400, 1589aaea09bSVenkat Prashanth B U indio_dev->debugfs_dentry, 1599caed0d9SLars-Peter Clausen adis16136, &adis16136_flash_count_fops); 1609caed0d9SLars-Peter Clausen 1619caed0d9SLars-Peter Clausen return 0; 1629caed0d9SLars-Peter Clausen } 1639caed0d9SLars-Peter Clausen 1649caed0d9SLars-Peter Clausen #else 1659caed0d9SLars-Peter Clausen 1669caed0d9SLars-Peter Clausen static int adis16136_debugfs_init(struct iio_dev *indio_dev) 1679caed0d9SLars-Peter Clausen { 1689caed0d9SLars-Peter Clausen return 0; 1699caed0d9SLars-Peter Clausen } 1709caed0d9SLars-Peter Clausen 1719caed0d9SLars-Peter Clausen #endif 1729caed0d9SLars-Peter Clausen 1739caed0d9SLars-Peter Clausen static int adis16136_set_freq(struct adis16136 *adis16136, unsigned int freq) 1749caed0d9SLars-Peter Clausen { 1759caed0d9SLars-Peter Clausen unsigned int t; 1769caed0d9SLars-Peter Clausen 1779caed0d9SLars-Peter Clausen t = 32768 / freq; 1789caed0d9SLars-Peter Clausen if (t < 0xf) 1799caed0d9SLars-Peter Clausen t = 0xf; 1809caed0d9SLars-Peter Clausen else if (t > 0xffff) 1819caed0d9SLars-Peter Clausen t = 0xffff; 1829caed0d9SLars-Peter Clausen else 1839caed0d9SLars-Peter Clausen t--; 1849caed0d9SLars-Peter Clausen 1859caed0d9SLars-Peter Clausen return adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, t); 1869caed0d9SLars-Peter Clausen } 1879caed0d9SLars-Peter Clausen 1889caed0d9SLars-Peter Clausen static int adis16136_get_freq(struct adis16136 *adis16136, unsigned int *freq) 1899caed0d9SLars-Peter Clausen { 1909caed0d9SLars-Peter Clausen uint16_t t; 1919caed0d9SLars-Peter Clausen int ret; 1929caed0d9SLars-Peter Clausen 1939caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, &t); 1949caed0d9SLars-Peter Clausen if (ret < 0) 1959caed0d9SLars-Peter Clausen return ret; 1969caed0d9SLars-Peter Clausen 1979caed0d9SLars-Peter Clausen *freq = 32768 / (t + 1); 1989caed0d9SLars-Peter Clausen 1999caed0d9SLars-Peter Clausen return 0; 2009caed0d9SLars-Peter Clausen } 2019caed0d9SLars-Peter Clausen 2029caed0d9SLars-Peter Clausen static ssize_t adis16136_write_frequency(struct device *dev, 2039caed0d9SLars-Peter Clausen struct device_attribute *attr, const char *buf, size_t len) 2049caed0d9SLars-Peter Clausen { 2059caed0d9SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 2069caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 20712660138SDan Carpenter unsigned int val; 2089caed0d9SLars-Peter Clausen int ret; 2099caed0d9SLars-Peter Clausen 21012660138SDan Carpenter ret = kstrtouint(buf, 10, &val); 2119caed0d9SLars-Peter Clausen if (ret) 2129caed0d9SLars-Peter Clausen return ret; 2139caed0d9SLars-Peter Clausen 2149caed0d9SLars-Peter Clausen if (val == 0) 2159caed0d9SLars-Peter Clausen return -EINVAL; 2169caed0d9SLars-Peter Clausen 2179caed0d9SLars-Peter Clausen ret = adis16136_set_freq(adis16136, val); 2189caed0d9SLars-Peter Clausen 2199caed0d9SLars-Peter Clausen return ret ? ret : len; 2209caed0d9SLars-Peter Clausen } 2219caed0d9SLars-Peter Clausen 2229caed0d9SLars-Peter Clausen static ssize_t adis16136_read_frequency(struct device *dev, 2239caed0d9SLars-Peter Clausen struct device_attribute *attr, char *buf) 2249caed0d9SLars-Peter Clausen { 2259caed0d9SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 2269caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 2279caed0d9SLars-Peter Clausen unsigned int freq; 2289caed0d9SLars-Peter Clausen int ret; 2299caed0d9SLars-Peter Clausen 2309caed0d9SLars-Peter Clausen ret = adis16136_get_freq(adis16136, &freq); 2319caed0d9SLars-Peter Clausen if (ret < 0) 2329caed0d9SLars-Peter Clausen return ret; 2339caed0d9SLars-Peter Clausen 2349caed0d9SLars-Peter Clausen return sprintf(buf, "%d\n", freq); 2359caed0d9SLars-Peter Clausen } 2369caed0d9SLars-Peter Clausen 2379caed0d9SLars-Peter Clausen static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, 2389caed0d9SLars-Peter Clausen adis16136_read_frequency, 2399caed0d9SLars-Peter Clausen adis16136_write_frequency); 2409caed0d9SLars-Peter Clausen 2419caed0d9SLars-Peter Clausen static const unsigned adis16136_3db_divisors[] = { 2429caed0d9SLars-Peter Clausen [0] = 2, /* Special case */ 2439caed0d9SLars-Peter Clausen [1] = 6, 2449caed0d9SLars-Peter Clausen [2] = 12, 2459caed0d9SLars-Peter Clausen [3] = 25, 2469caed0d9SLars-Peter Clausen [4] = 50, 2479caed0d9SLars-Peter Clausen [5] = 100, 2489caed0d9SLars-Peter Clausen [6] = 200, 2499caed0d9SLars-Peter Clausen [7] = 200, /* Not a valid setting */ 2509caed0d9SLars-Peter Clausen }; 2519caed0d9SLars-Peter Clausen 2529caed0d9SLars-Peter Clausen static int adis16136_set_filter(struct iio_dev *indio_dev, int val) 2539caed0d9SLars-Peter Clausen { 2549caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 2559caed0d9SLars-Peter Clausen unsigned int freq; 2569caed0d9SLars-Peter Clausen int i, ret; 2579caed0d9SLars-Peter Clausen 2589caed0d9SLars-Peter Clausen ret = adis16136_get_freq(adis16136, &freq); 2599caed0d9SLars-Peter Clausen if (ret < 0) 2609caed0d9SLars-Peter Clausen return ret; 2619caed0d9SLars-Peter Clausen 2629caed0d9SLars-Peter Clausen for (i = ARRAY_SIZE(adis16136_3db_divisors) - 1; i >= 1; i--) { 2639caed0d9SLars-Peter Clausen if (freq / adis16136_3db_divisors[i] >= val) 2649caed0d9SLars-Peter Clausen break; 2659caed0d9SLars-Peter Clausen } 2669caed0d9SLars-Peter Clausen 2679caed0d9SLars-Peter Clausen return adis_write_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, i); 2689caed0d9SLars-Peter Clausen } 2699caed0d9SLars-Peter Clausen 2709caed0d9SLars-Peter Clausen static int adis16136_get_filter(struct iio_dev *indio_dev, int *val) 2719caed0d9SLars-Peter Clausen { 2729caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 2739caed0d9SLars-Peter Clausen unsigned int freq; 2749caed0d9SLars-Peter Clausen uint16_t val16; 2759caed0d9SLars-Peter Clausen int ret; 2769caed0d9SLars-Peter Clausen 2779caed0d9SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 2789caed0d9SLars-Peter Clausen 2799caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, &val16); 2809caed0d9SLars-Peter Clausen if (ret < 0) 2819caed0d9SLars-Peter Clausen goto err_unlock; 2829caed0d9SLars-Peter Clausen 2839caed0d9SLars-Peter Clausen ret = adis16136_get_freq(adis16136, &freq); 2849caed0d9SLars-Peter Clausen if (ret < 0) 2859caed0d9SLars-Peter Clausen goto err_unlock; 2869caed0d9SLars-Peter Clausen 2879caed0d9SLars-Peter Clausen *val = freq / adis16136_3db_divisors[val16 & 0x07]; 2889caed0d9SLars-Peter Clausen 2899caed0d9SLars-Peter Clausen err_unlock: 2909caed0d9SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 2919caed0d9SLars-Peter Clausen 2929caed0d9SLars-Peter Clausen return ret ? ret : IIO_VAL_INT; 2939caed0d9SLars-Peter Clausen } 2949caed0d9SLars-Peter Clausen 2959caed0d9SLars-Peter Clausen static int adis16136_read_raw(struct iio_dev *indio_dev, 2969caed0d9SLars-Peter Clausen const struct iio_chan_spec *chan, int *val, int *val2, long info) 2979caed0d9SLars-Peter Clausen { 2989caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 2999caed0d9SLars-Peter Clausen uint32_t val32; 3009caed0d9SLars-Peter Clausen int ret; 3019caed0d9SLars-Peter Clausen 3029caed0d9SLars-Peter Clausen switch (info) { 3039caed0d9SLars-Peter Clausen case IIO_CHAN_INFO_RAW: 3049caed0d9SLars-Peter Clausen return adis_single_conversion(indio_dev, chan, 0, val); 3059caed0d9SLars-Peter Clausen case IIO_CHAN_INFO_SCALE: 3069caed0d9SLars-Peter Clausen switch (chan->type) { 3079caed0d9SLars-Peter Clausen case IIO_ANGL_VEL: 3089caed0d9SLars-Peter Clausen *val = adis16136->chip_info->precision; 3099caed0d9SLars-Peter Clausen *val2 = (adis16136->chip_info->fullscale << 16); 3109caed0d9SLars-Peter Clausen return IIO_VAL_FRACTIONAL; 3119caed0d9SLars-Peter Clausen case IIO_TEMP: 3129caed0d9SLars-Peter Clausen *val = 10; 3139caed0d9SLars-Peter Clausen *val2 = 697000; /* 0.010697 degree Celsius */ 3149caed0d9SLars-Peter Clausen return IIO_VAL_INT_PLUS_MICRO; 3159caed0d9SLars-Peter Clausen default: 3169caed0d9SLars-Peter Clausen return -EINVAL; 3179caed0d9SLars-Peter Clausen } 3189caed0d9SLars-Peter Clausen case IIO_CHAN_INFO_CALIBBIAS: 3199caed0d9SLars-Peter Clausen ret = adis_read_reg_32(&adis16136->adis, 3209caed0d9SLars-Peter Clausen ADIS16136_REG_GYRO_OFF2, &val32); 3219caed0d9SLars-Peter Clausen if (ret < 0) 3229caed0d9SLars-Peter Clausen return ret; 3239caed0d9SLars-Peter Clausen 3249caed0d9SLars-Peter Clausen *val = sign_extend32(val32, 31); 3259caed0d9SLars-Peter Clausen 3269caed0d9SLars-Peter Clausen return IIO_VAL_INT; 3279caed0d9SLars-Peter Clausen case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 3289caed0d9SLars-Peter Clausen return adis16136_get_filter(indio_dev, val); 3299caed0d9SLars-Peter Clausen default: 3309caed0d9SLars-Peter Clausen return -EINVAL; 3319caed0d9SLars-Peter Clausen } 3329caed0d9SLars-Peter Clausen } 3339caed0d9SLars-Peter Clausen 3349caed0d9SLars-Peter Clausen static int adis16136_write_raw(struct iio_dev *indio_dev, 3359caed0d9SLars-Peter Clausen const struct iio_chan_spec *chan, int val, int val2, long info) 3369caed0d9SLars-Peter Clausen { 3379caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 3389caed0d9SLars-Peter Clausen 3399caed0d9SLars-Peter Clausen switch (info) { 3409caed0d9SLars-Peter Clausen case IIO_CHAN_INFO_CALIBBIAS: 3419caed0d9SLars-Peter Clausen return adis_write_reg_32(&adis16136->adis, 3429caed0d9SLars-Peter Clausen ADIS16136_REG_GYRO_OFF2, val); 3439caed0d9SLars-Peter Clausen case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 3449caed0d9SLars-Peter Clausen return adis16136_set_filter(indio_dev, val); 3459caed0d9SLars-Peter Clausen default: 3469caed0d9SLars-Peter Clausen break; 3479caed0d9SLars-Peter Clausen } 3489caed0d9SLars-Peter Clausen 3499caed0d9SLars-Peter Clausen return -EINVAL; 3509caed0d9SLars-Peter Clausen } 3519caed0d9SLars-Peter Clausen 3529caed0d9SLars-Peter Clausen enum { 3539caed0d9SLars-Peter Clausen ADIS16136_SCAN_GYRO, 3549caed0d9SLars-Peter Clausen ADIS16136_SCAN_TEMP, 3559caed0d9SLars-Peter Clausen }; 3569caed0d9SLars-Peter Clausen 3579caed0d9SLars-Peter Clausen static const struct iio_chan_spec adis16136_channels[] = { 3589caed0d9SLars-Peter Clausen { 3599caed0d9SLars-Peter Clausen .type = IIO_ANGL_VEL, 3609caed0d9SLars-Peter Clausen .modified = 1, 3619caed0d9SLars-Peter Clausen .channel2 = IIO_MOD_X, 362606f9067SJonathan Cameron .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 363606f9067SJonathan Cameron BIT(IIO_CHAN_INFO_CALIBBIAS) | 364606f9067SJonathan Cameron BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 365606f9067SJonathan Cameron .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 366606f9067SJonathan Cameron 3679caed0d9SLars-Peter Clausen .address = ADIS16136_REG_GYRO_OUT2, 3689caed0d9SLars-Peter Clausen .scan_index = ADIS16136_SCAN_GYRO, 3699caed0d9SLars-Peter Clausen .scan_type = { 3709caed0d9SLars-Peter Clausen .sign = 's', 3719caed0d9SLars-Peter Clausen .realbits = 32, 3729caed0d9SLars-Peter Clausen .storagebits = 32, 3739caed0d9SLars-Peter Clausen .endianness = IIO_BE, 3749caed0d9SLars-Peter Clausen }, 3759caed0d9SLars-Peter Clausen }, { 3769caed0d9SLars-Peter Clausen .type = IIO_TEMP, 3779caed0d9SLars-Peter Clausen .indexed = 1, 3789caed0d9SLars-Peter Clausen .channel = 0, 379606f9067SJonathan Cameron .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 380606f9067SJonathan Cameron BIT(IIO_CHAN_INFO_SCALE), 3819caed0d9SLars-Peter Clausen .address = ADIS16136_REG_TEMP_OUT, 3829caed0d9SLars-Peter Clausen .scan_index = ADIS16136_SCAN_TEMP, 3839caed0d9SLars-Peter Clausen .scan_type = { 3849caed0d9SLars-Peter Clausen .sign = 's', 3859caed0d9SLars-Peter Clausen .realbits = 16, 3869caed0d9SLars-Peter Clausen .storagebits = 16, 3879caed0d9SLars-Peter Clausen .endianness = IIO_BE, 3889caed0d9SLars-Peter Clausen }, 3899caed0d9SLars-Peter Clausen }, 3909caed0d9SLars-Peter Clausen IIO_CHAN_SOFT_TIMESTAMP(2), 3919caed0d9SLars-Peter Clausen }; 3929caed0d9SLars-Peter Clausen 3939caed0d9SLars-Peter Clausen static struct attribute *adis16136_attributes[] = { 3949caed0d9SLars-Peter Clausen &iio_dev_attr_sampling_frequency.dev_attr.attr, 3959caed0d9SLars-Peter Clausen NULL 3969caed0d9SLars-Peter Clausen }; 3979caed0d9SLars-Peter Clausen 3989caed0d9SLars-Peter Clausen static const struct attribute_group adis16136_attribute_group = { 3999caed0d9SLars-Peter Clausen .attrs = adis16136_attributes, 4009caed0d9SLars-Peter Clausen }; 4019caed0d9SLars-Peter Clausen 4029caed0d9SLars-Peter Clausen static const struct iio_info adis16136_info = { 4039caed0d9SLars-Peter Clausen .attrs = &adis16136_attribute_group, 4049caed0d9SLars-Peter Clausen .read_raw = &adis16136_read_raw, 4059caed0d9SLars-Peter Clausen .write_raw = &adis16136_write_raw, 4069caed0d9SLars-Peter Clausen .update_scan_mode = adis_update_scan_mode, 4079caed0d9SLars-Peter Clausen .debugfs_reg_access = adis_debugfs_reg_access, 4089caed0d9SLars-Peter Clausen }; 4099caed0d9SLars-Peter Clausen 4109caed0d9SLars-Peter Clausen static int adis16136_stop_device(struct iio_dev *indio_dev) 4119caed0d9SLars-Peter Clausen { 4129caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 4139caed0d9SLars-Peter Clausen int ret; 4149caed0d9SLars-Peter Clausen 4159caed0d9SLars-Peter Clausen ret = adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SLP_CTRL, 0xff); 4169caed0d9SLars-Peter Clausen if (ret) 4179caed0d9SLars-Peter Clausen dev_err(&indio_dev->dev, 4189caed0d9SLars-Peter Clausen "Could not power down device: %d\n", ret); 4199caed0d9SLars-Peter Clausen 4209caed0d9SLars-Peter Clausen return ret; 4219caed0d9SLars-Peter Clausen } 4229caed0d9SLars-Peter Clausen 4239caed0d9SLars-Peter Clausen static int adis16136_initial_setup(struct iio_dev *indio_dev) 4249caed0d9SLars-Peter Clausen { 4259caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 4269caed0d9SLars-Peter Clausen unsigned int device_id; 4279caed0d9SLars-Peter Clausen uint16_t prod_id; 4289caed0d9SLars-Peter Clausen int ret; 4299caed0d9SLars-Peter Clausen 4309caed0d9SLars-Peter Clausen ret = adis_initial_startup(&adis16136->adis); 4319caed0d9SLars-Peter Clausen if (ret) 4329caed0d9SLars-Peter Clausen return ret; 4339caed0d9SLars-Peter Clausen 4349caed0d9SLars-Peter Clausen ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID, 4359caed0d9SLars-Peter Clausen &prod_id); 4369caed0d9SLars-Peter Clausen if (ret) 4379caed0d9SLars-Peter Clausen return ret; 4389caed0d9SLars-Peter Clausen 439a106b474SIoana Ciornei ret = sscanf(indio_dev->name, "adis%u\n", &device_id); 440a106b474SIoana Ciornei if (ret != 1) 441a106b474SIoana Ciornei return -EINVAL; 4429caed0d9SLars-Peter Clausen 4439caed0d9SLars-Peter Clausen if (prod_id != device_id) 4449caed0d9SLars-Peter Clausen dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.", 4459caed0d9SLars-Peter Clausen device_id, prod_id); 4469caed0d9SLars-Peter Clausen 4479caed0d9SLars-Peter Clausen return 0; 4489caed0d9SLars-Peter Clausen } 4499caed0d9SLars-Peter Clausen 4509caed0d9SLars-Peter Clausen static const char * const adis16136_status_error_msgs[] = { 4519caed0d9SLars-Peter Clausen [ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL] = "Flash update failed", 4529caed0d9SLars-Peter Clausen [ADIS16136_DIAG_STAT_SPI_FAIL] = "SPI failure", 4539caed0d9SLars-Peter Clausen [ADIS16136_DIAG_STAT_SELF_TEST_FAIL] = "Self test error", 4549caed0d9SLars-Peter Clausen [ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL] = "Flash checksum error", 4559caed0d9SLars-Peter Clausen }; 4569caed0d9SLars-Peter Clausen 4579caed0d9SLars-Peter Clausen static const struct adis_data adis16136_data = { 4589caed0d9SLars-Peter Clausen .diag_stat_reg = ADIS16136_REG_DIAG_STAT, 4599caed0d9SLars-Peter Clausen .glob_cmd_reg = ADIS16136_REG_GLOB_CMD, 4609caed0d9SLars-Peter Clausen .msc_ctrl_reg = ADIS16136_REG_MSC_CTRL, 4619caed0d9SLars-Peter Clausen 4629caed0d9SLars-Peter Clausen .self_test_mask = ADIS16136_MSC_CTRL_SELF_TEST, 4639caed0d9SLars-Peter Clausen .startup_delay = 80, 4649caed0d9SLars-Peter Clausen 4659caed0d9SLars-Peter Clausen .read_delay = 10, 4669caed0d9SLars-Peter Clausen .write_delay = 10, 4679caed0d9SLars-Peter Clausen 4689caed0d9SLars-Peter Clausen .status_error_msgs = adis16136_status_error_msgs, 4699caed0d9SLars-Peter Clausen .status_error_mask = BIT(ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL) | 4709caed0d9SLars-Peter Clausen BIT(ADIS16136_DIAG_STAT_SPI_FAIL) | 4719caed0d9SLars-Peter Clausen BIT(ADIS16136_DIAG_STAT_SELF_TEST_FAIL) | 4729caed0d9SLars-Peter Clausen BIT(ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL), 4739caed0d9SLars-Peter Clausen }; 4749caed0d9SLars-Peter Clausen 4759caed0d9SLars-Peter Clausen enum adis16136_id { 4769caed0d9SLars-Peter Clausen ID_ADIS16133, 4779caed0d9SLars-Peter Clausen ID_ADIS16135, 4789caed0d9SLars-Peter Clausen ID_ADIS16136, 4795450c360SLars-Peter Clausen ID_ADIS16137, 4809caed0d9SLars-Peter Clausen }; 4819caed0d9SLars-Peter Clausen 4829caed0d9SLars-Peter Clausen static const struct adis16136_chip_info adis16136_chip_info[] = { 4839caed0d9SLars-Peter Clausen [ID_ADIS16133] = { 4849caed0d9SLars-Peter Clausen .precision = IIO_DEGREE_TO_RAD(1200), 4859caed0d9SLars-Peter Clausen .fullscale = 24000, 4869caed0d9SLars-Peter Clausen }, 4879caed0d9SLars-Peter Clausen [ID_ADIS16135] = { 4889caed0d9SLars-Peter Clausen .precision = IIO_DEGREE_TO_RAD(300), 4899caed0d9SLars-Peter Clausen .fullscale = 24000, 4909caed0d9SLars-Peter Clausen }, 4919caed0d9SLars-Peter Clausen [ID_ADIS16136] = { 4929caed0d9SLars-Peter Clausen .precision = IIO_DEGREE_TO_RAD(450), 4939caed0d9SLars-Peter Clausen .fullscale = 24623, 4949caed0d9SLars-Peter Clausen }, 4955450c360SLars-Peter Clausen [ID_ADIS16137] = { 4965450c360SLars-Peter Clausen .precision = IIO_DEGREE_TO_RAD(1000), 4975450c360SLars-Peter Clausen .fullscale = 24609, 4985450c360SLars-Peter Clausen }, 4999caed0d9SLars-Peter Clausen }; 5009caed0d9SLars-Peter Clausen 5019caed0d9SLars-Peter Clausen static int adis16136_probe(struct spi_device *spi) 5029caed0d9SLars-Peter Clausen { 5039caed0d9SLars-Peter Clausen const struct spi_device_id *id = spi_get_device_id(spi); 5049caed0d9SLars-Peter Clausen struct adis16136 *adis16136; 5059caed0d9SLars-Peter Clausen struct iio_dev *indio_dev; 5069caed0d9SLars-Peter Clausen int ret; 5079caed0d9SLars-Peter Clausen 508c0ca6d31SSachin Kamat indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16136)); 5099caed0d9SLars-Peter Clausen if (indio_dev == NULL) 5109caed0d9SLars-Peter Clausen return -ENOMEM; 5119caed0d9SLars-Peter Clausen 5129caed0d9SLars-Peter Clausen spi_set_drvdata(spi, indio_dev); 5139caed0d9SLars-Peter Clausen 5149caed0d9SLars-Peter Clausen adis16136 = iio_priv(indio_dev); 5159caed0d9SLars-Peter Clausen 5169caed0d9SLars-Peter Clausen adis16136->chip_info = &adis16136_chip_info[id->driver_data]; 5179caed0d9SLars-Peter Clausen indio_dev->dev.parent = &spi->dev; 5189caed0d9SLars-Peter Clausen indio_dev->name = spi_get_device_id(spi)->name; 5199caed0d9SLars-Peter Clausen indio_dev->channels = adis16136_channels; 5209caed0d9SLars-Peter Clausen indio_dev->num_channels = ARRAY_SIZE(adis16136_channels); 5219caed0d9SLars-Peter Clausen indio_dev->info = &adis16136_info; 5229caed0d9SLars-Peter Clausen indio_dev->modes = INDIO_DIRECT_MODE; 5239caed0d9SLars-Peter Clausen 5249caed0d9SLars-Peter Clausen ret = adis_init(&adis16136->adis, indio_dev, spi, &adis16136_data); 5259caed0d9SLars-Peter Clausen if (ret) 526c0ca6d31SSachin Kamat return ret; 5279caed0d9SLars-Peter Clausen 5289caed0d9SLars-Peter Clausen ret = adis_setup_buffer_and_trigger(&adis16136->adis, indio_dev, NULL); 5299caed0d9SLars-Peter Clausen if (ret) 530c0ca6d31SSachin Kamat return ret; 5319caed0d9SLars-Peter Clausen 5329caed0d9SLars-Peter Clausen ret = adis16136_initial_setup(indio_dev); 5339caed0d9SLars-Peter Clausen if (ret) 5349caed0d9SLars-Peter Clausen goto error_cleanup_buffer; 5359caed0d9SLars-Peter Clausen 5369caed0d9SLars-Peter Clausen ret = iio_device_register(indio_dev); 5379caed0d9SLars-Peter Clausen if (ret) 5389caed0d9SLars-Peter Clausen goto error_stop_device; 5399caed0d9SLars-Peter Clausen 5409caed0d9SLars-Peter Clausen adis16136_debugfs_init(indio_dev); 5419caed0d9SLars-Peter Clausen 5429caed0d9SLars-Peter Clausen return 0; 5439caed0d9SLars-Peter Clausen 5449caed0d9SLars-Peter Clausen error_stop_device: 5459caed0d9SLars-Peter Clausen adis16136_stop_device(indio_dev); 5469caed0d9SLars-Peter Clausen error_cleanup_buffer: 5479caed0d9SLars-Peter Clausen adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev); 5489caed0d9SLars-Peter Clausen return ret; 5499caed0d9SLars-Peter Clausen } 5509caed0d9SLars-Peter Clausen 5519caed0d9SLars-Peter Clausen static int adis16136_remove(struct spi_device *spi) 5529caed0d9SLars-Peter Clausen { 5539caed0d9SLars-Peter Clausen struct iio_dev *indio_dev = spi_get_drvdata(spi); 5549caed0d9SLars-Peter Clausen struct adis16136 *adis16136 = iio_priv(indio_dev); 5559caed0d9SLars-Peter Clausen 5569caed0d9SLars-Peter Clausen iio_device_unregister(indio_dev); 5579caed0d9SLars-Peter Clausen adis16136_stop_device(indio_dev); 5589caed0d9SLars-Peter Clausen 5599caed0d9SLars-Peter Clausen adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev); 5609caed0d9SLars-Peter Clausen 5619caed0d9SLars-Peter Clausen return 0; 5629caed0d9SLars-Peter Clausen } 5639caed0d9SLars-Peter Clausen 5649caed0d9SLars-Peter Clausen static const struct spi_device_id adis16136_ids[] = { 5659caed0d9SLars-Peter Clausen { "adis16133", ID_ADIS16133 }, 5669caed0d9SLars-Peter Clausen { "adis16135", ID_ADIS16135 }, 5679caed0d9SLars-Peter Clausen { "adis16136", ID_ADIS16136 }, 5685450c360SLars-Peter Clausen { "adis16137", ID_ADIS16137 }, 5699caed0d9SLars-Peter Clausen { } 5709caed0d9SLars-Peter Clausen }; 5719caed0d9SLars-Peter Clausen MODULE_DEVICE_TABLE(spi, adis16136_ids); 5729caed0d9SLars-Peter Clausen 5739caed0d9SLars-Peter Clausen static struct spi_driver adis16136_driver = { 5749caed0d9SLars-Peter Clausen .driver = { 5759caed0d9SLars-Peter Clausen .name = "adis16136", 5769caed0d9SLars-Peter Clausen }, 5779caed0d9SLars-Peter Clausen .id_table = adis16136_ids, 5789caed0d9SLars-Peter Clausen .probe = adis16136_probe, 5799caed0d9SLars-Peter Clausen .remove = adis16136_remove, 5809caed0d9SLars-Peter Clausen }; 5819caed0d9SLars-Peter Clausen module_spi_driver(adis16136_driver); 5829caed0d9SLars-Peter Clausen 5839caed0d9SLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 5849caed0d9SLars-Peter Clausen MODULE_DESCRIPTION("Analog Devices ADIS16133/ADIS16135/ADIS16136 gyroscope driver"); 5859caed0d9SLars-Peter Clausen MODULE_LICENSE("GPL v2"); 586