linux/drivers/iio/imu/adis16400_core.c
<<
>>
Prefs
   1/*
   2 * adis16400.c  support Analog Devices ADIS16400/5
   3 *              3d 2g Linear Accelerometers,
   4 *              3d Gyroscopes,
   5 *              3d Magnetometers via SPI
   6 *
   7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
   8 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
   9 * Copyright (c) 2011 Analog Devices Inc.
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License version 2 as
  13 * published by the Free Software Foundation.
  14 *
  15 */
  16
  17#include <linux/interrupt.h>
  18#include <linux/irq.h>
  19#include <linux/delay.h>
  20#include <linux/mutex.h>
  21#include <linux/device.h>
  22#include <linux/kernel.h>
  23#include <linux/spi/spi.h>
  24#include <linux/slab.h>
  25#include <linux/sysfs.h>
  26#include <linux/list.h>
  27#include <linux/module.h>
  28#include <linux/debugfs.h>
  29#include <linux/bitops.h>
  30
  31#include <linux/iio/iio.h>
  32#include <linux/iio/sysfs.h>
  33#include <linux/iio/buffer.h>
  34
  35#include "adis16400.h"
  36
  37#ifdef CONFIG_DEBUG_FS
  38
  39static ssize_t adis16400_show_serial_number(struct file *file,
  40                char __user *userbuf, size_t count, loff_t *ppos)
  41{
  42        struct adis16400_state *st = file->private_data;
  43        u16 lot1, lot2, serial_number;
  44        char buf[16];
  45        size_t len;
  46        int ret;
  47
  48        ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
  49        if (ret < 0)
  50                return ret;
  51
  52        ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
  53        if (ret < 0)
  54                return ret;
  55
  56        ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
  57                        &serial_number);
  58        if (ret < 0)
  59                return ret;
  60
  61        len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
  62                        serial_number);
  63
  64        return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  65}
  66
  67static const struct file_operations adis16400_serial_number_fops = {
  68        .open = simple_open,
  69        .read = adis16400_show_serial_number,
  70        .llseek = default_llseek,
  71        .owner = THIS_MODULE,
  72};
  73
  74static int adis16400_show_product_id(void *arg, u64 *val)
  75{
  76        struct adis16400_state *st = arg;
  77        uint16_t prod_id;
  78        int ret;
  79
  80        ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
  81        if (ret < 0)
  82                return ret;
  83
  84        *val = prod_id;
  85
  86        return 0;
  87}
  88DEFINE_SIMPLE_ATTRIBUTE(adis16400_product_id_fops,
  89        adis16400_show_product_id, NULL, "%lld\n");
  90
  91static int adis16400_show_flash_count(void *arg, u64 *val)
  92{
  93        struct adis16400_state *st = arg;
  94        uint16_t flash_count;
  95        int ret;
  96
  97        ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
  98        if (ret < 0)
  99                return ret;
 100
 101        *val = flash_count;
 102
 103        return 0;
 104}
 105DEFINE_SIMPLE_ATTRIBUTE(adis16400_flash_count_fops,
 106        adis16400_show_flash_count, NULL, "%lld\n");
 107
 108static int adis16400_debugfs_init(struct iio_dev *indio_dev)
 109{
 110        struct adis16400_state *st = iio_priv(indio_dev);
 111
 112        if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
 113                debugfs_create_file("serial_number", 0400,
 114                        indio_dev->debugfs_dentry, st,
 115                        &adis16400_serial_number_fops);
 116        if (st->variant->flags & ADIS16400_HAS_PROD_ID)
 117                debugfs_create_file("product_id", 0400,
 118                        indio_dev->debugfs_dentry, st,
 119                        &adis16400_product_id_fops);
 120        debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
 121                st, &adis16400_flash_count_fops);
 122
 123        return 0;
 124}
 125
 126#else
 127
 128static int adis16400_debugfs_init(struct iio_dev *indio_dev)
 129{
 130        return 0;
 131}
 132
 133#endif
 134
 135enum adis16400_chip_variant {
 136        ADIS16300,
 137        ADIS16334,
 138        ADIS16350,
 139        ADIS16360,
 140        ADIS16362,
 141        ADIS16364,
 142        ADIS16367,
 143        ADIS16400,
 144        ADIS16445,
 145        ADIS16448,
 146};
 147
 148static int adis16334_get_freq(struct adis16400_state *st)
 149{
 150        int ret;
 151        uint16_t t;
 152
 153        ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
 154        if (ret < 0)
 155                return ret;
 156
 157        t >>= ADIS16334_RATE_DIV_SHIFT;
 158
 159        return 819200 >> t;
 160}
 161
 162static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
 163{
 164        unsigned int t;
 165
 166        if (freq < 819200)
 167                t = ilog2(819200 / freq);
 168        else
 169                t = 0;
 170
 171        if (t > 0x31)
 172                t = 0x31;
 173
 174        t <<= ADIS16334_RATE_DIV_SHIFT;
 175        t |= ADIS16334_RATE_INT_CLK;
 176
 177        return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
 178}
 179
 180static int adis16400_get_freq(struct adis16400_state *st)
 181{
 182        int sps, ret;
 183        uint16_t t;
 184
 185        ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
 186        if (ret < 0)
 187                return ret;
 188
 189        sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
 190        sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
 191
 192        return sps;
 193}
 194
 195static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
 196{
 197        unsigned int t;
 198        uint8_t val = 0;
 199
 200        t = 1638404 / freq;
 201        if (t >= 128) {
 202                val |= ADIS16400_SMPL_PRD_TIME_BASE;
 203                t = 52851 / freq;
 204                if (t >= 128)
 205                        t = 127;
 206        } else if (t != 0) {
 207                t--;
 208        }
 209
 210        val |= t;
 211
 212        if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
 213                st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
 214        else
 215                st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
 216
 217        return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
 218}
 219
 220static const unsigned adis16400_3db_divisors[] = {
 221        [0] = 2, /* Special case */
 222        [1] = 6,
 223        [2] = 12,
 224        [3] = 25,
 225        [4] = 50,
 226        [5] = 100,
 227        [6] = 200,
 228        [7] = 200, /* Not a valid setting */
 229};
 230
 231static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
 232{
 233        struct adis16400_state *st = iio_priv(indio_dev);
 234        uint16_t val16;
 235        int i, ret;
 236
 237        for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
 238                if (sps / adis16400_3db_divisors[i] >= val)
 239                        break;
 240        }
 241
 242        ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
 243        if (ret < 0)
 244                return ret;
 245
 246        ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
 247                                         (val16 & ~0x07) | i);
 248        return ret;
 249}
 250
 251/* Power down the device */
 252static int adis16400_stop_device(struct iio_dev *indio_dev)
 253{
 254        struct adis16400_state *st = iio_priv(indio_dev);
 255        int ret;
 256
 257        ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
 258                        ADIS16400_SLP_CNT_POWER_OFF);
 259        if (ret)
 260                dev_err(&indio_dev->dev,
 261                        "problem with turning device off: SLP_CNT");
 262
 263        return ret;
 264}
 265
 266static int adis16400_initial_setup(struct iio_dev *indio_dev)
 267{
 268        struct adis16400_state *st = iio_priv(indio_dev);
 269        uint16_t prod_id, smp_prd;
 270        unsigned int device_id;
 271        int ret;
 272
 273        /* use low spi speed for init if the device has a slow mode */
 274        if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
 275                st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
 276        else
 277                st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
 278        st->adis.spi->mode = SPI_MODE_3;
 279        spi_setup(st->adis.spi);
 280
 281        ret = adis_initial_startup(&st->adis);
 282        if (ret)
 283                return ret;
 284
 285        if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
 286                ret = adis_read_reg_16(&st->adis,
 287                                                ADIS16400_PRODUCT_ID, &prod_id);
 288                if (ret)
 289                        goto err_ret;
 290
 291                ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
 292                if (ret != 1) {
 293                        ret = -EINVAL;
 294                        goto err_ret;
 295                }
 296
 297                if (prod_id != device_id)
 298                        dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
 299                                        device_id, prod_id);
 300
 301                dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
 302                        indio_dev->name, prod_id,
 303                        st->adis.spi->chip_select, st->adis.spi->irq);
 304        }
 305        /* use high spi speed if possible */
 306        if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
 307                ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
 308                if (ret)
 309                        goto err_ret;
 310
 311                if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
 312                        st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
 313                        spi_setup(st->adis.spi);
 314                }
 315        }
 316
 317err_ret:
 318        return ret;
 319}
 320
 321static const uint8_t adis16400_addresses[] = {
 322        [ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
 323        [ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
 324        [ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
 325        [ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
 326        [ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
 327        [ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
 328};
 329
 330static int adis16400_write_raw(struct iio_dev *indio_dev,
 331        struct iio_chan_spec const *chan, int val, int val2, long info)
 332{
 333        struct adis16400_state *st = iio_priv(indio_dev);
 334        int ret, sps;
 335
 336        switch (info) {
 337        case IIO_CHAN_INFO_CALIBBIAS:
 338                mutex_lock(&indio_dev->mlock);
 339                ret = adis_write_reg_16(&st->adis,
 340                                adis16400_addresses[chan->scan_index], val);
 341                mutex_unlock(&indio_dev->mlock);
 342                return ret;
 343        case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
 344                /*
 345                 * Need to cache values so we can update if the frequency
 346                 * changes.
 347                 */
 348                mutex_lock(&indio_dev->mlock);
 349                st->filt_int = val;
 350                /* Work out update to current value */
 351                sps = st->variant->get_freq(st);
 352                if (sps < 0) {
 353                        mutex_unlock(&indio_dev->mlock);
 354                        return sps;
 355                }
 356
 357                ret = adis16400_set_filter(indio_dev, sps,
 358                        val * 1000 + val2 / 1000);
 359                mutex_unlock(&indio_dev->mlock);
 360                return ret;
 361        case IIO_CHAN_INFO_SAMP_FREQ:
 362                sps = val * 1000 + val2 / 1000;
 363
 364                if (sps <= 0)
 365                        return -EINVAL;
 366
 367                mutex_lock(&indio_dev->mlock);
 368                ret = st->variant->set_freq(st, sps);
 369                mutex_unlock(&indio_dev->mlock);
 370                return ret;
 371        default:
 372                return -EINVAL;
 373        }
 374}
 375
 376static int adis16400_read_raw(struct iio_dev *indio_dev,
 377        struct iio_chan_spec const *chan, int *val, int *val2, long info)
 378{
 379        struct adis16400_state *st = iio_priv(indio_dev);
 380        int16_t val16;
 381        int ret;
 382
 383        switch (info) {
 384        case IIO_CHAN_INFO_RAW:
 385                return adis_single_conversion(indio_dev, chan, 0, val);
 386        case IIO_CHAN_INFO_SCALE:
 387                switch (chan->type) {
 388                case IIO_ANGL_VEL:
 389                        *val = 0;
 390                        *val2 = st->variant->gyro_scale_micro;
 391                        return IIO_VAL_INT_PLUS_MICRO;
 392                case IIO_VOLTAGE:
 393                        *val = 0;
 394                        if (chan->channel == 0) {
 395                                *val = 2;
 396                                *val2 = 418000; /* 2.418 mV */
 397                        } else {
 398                                *val = 0;
 399                                *val2 = 805800; /* 805.8 uV */
 400                        }
 401                        return IIO_VAL_INT_PLUS_MICRO;
 402                case IIO_ACCEL:
 403                        *val = 0;
 404                        *val2 = st->variant->accel_scale_micro;
 405                        return IIO_VAL_INT_PLUS_MICRO;
 406                case IIO_MAGN:
 407                        *val = 0;
 408                        *val2 = 500; /* 0.5 mgauss */
 409                        return IIO_VAL_INT_PLUS_MICRO;
 410                case IIO_TEMP:
 411                        *val = st->variant->temp_scale_nano / 1000000;
 412                        *val2 = (st->variant->temp_scale_nano % 1000000);
 413                        return IIO_VAL_INT_PLUS_MICRO;
 414                case IIO_PRESSURE:
 415                        /* 20 uBar = 0.002kPascal */
 416                        *val = 0;
 417                        *val2 = 2000;
 418                        return IIO_VAL_INT_PLUS_MICRO;
 419                default:
 420                        return -EINVAL;
 421                }
 422        case IIO_CHAN_INFO_CALIBBIAS:
 423                mutex_lock(&indio_dev->mlock);
 424                ret = adis_read_reg_16(&st->adis,
 425                                adis16400_addresses[chan->scan_index], &val16);
 426                mutex_unlock(&indio_dev->mlock);
 427                if (ret)
 428                        return ret;
 429                val16 = sign_extend32(val16, 11);
 430                *val = val16;
 431                return IIO_VAL_INT;
 432        case IIO_CHAN_INFO_OFFSET:
 433                /* currently only temperature */
 434                *val = st->variant->temp_offset;
 435                return IIO_VAL_INT;
 436        case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
 437                mutex_lock(&indio_dev->mlock);
 438                /* Need both the number of taps and the sampling frequency */
 439                ret = adis_read_reg_16(&st->adis,
 440                                                ADIS16400_SENS_AVG,
 441                                                &val16);
 442                if (ret < 0) {
 443                        mutex_unlock(&indio_dev->mlock);
 444                        return ret;
 445                }
 446                ret = st->variant->get_freq(st);
 447                if (ret >= 0) {
 448                        ret /= adis16400_3db_divisors[val16 & 0x07];
 449                        *val = ret / 1000;
 450                        *val2 = (ret % 1000) * 1000;
 451                }
 452                mutex_unlock(&indio_dev->mlock);
 453                if (ret < 0)
 454                        return ret;
 455                return IIO_VAL_INT_PLUS_MICRO;
 456        case IIO_CHAN_INFO_SAMP_FREQ:
 457                ret = st->variant->get_freq(st);
 458                if (ret < 0)
 459                        return ret;
 460                *val = ret / 1000;
 461                *val2 = (ret % 1000) * 1000;
 462                return IIO_VAL_INT_PLUS_MICRO;
 463        default:
 464                return -EINVAL;
 465        }
 466}
 467
 468#define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \
 469        .type = IIO_VOLTAGE, \
 470        .indexed = 1, \
 471        .channel = chn, \
 472        .extend_name = name, \
 473        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 474                BIT(IIO_CHAN_INFO_SCALE), \
 475        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 476        .address = (addr), \
 477        .scan_index = (si), \
 478        .scan_type = { \
 479                .sign = 'u', \
 480                .realbits = (bits), \
 481                .storagebits = 16, \
 482                .shift = 0, \
 483                .endianness = IIO_BE, \
 484        }, \
 485}
 486
 487#define ADIS16400_SUPPLY_CHAN(addr, bits) \
 488        ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0)
 489
 490#define ADIS16400_AUX_ADC_CHAN(addr, bits) \
 491        ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1)
 492
 493#define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
 494        .type = IIO_ANGL_VEL, \
 495        .modified = 1, \
 496        .channel2 = IIO_MOD_ ## mod, \
 497        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 498                BIT(IIO_CHAN_INFO_CALIBBIAS),             \
 499        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
 500                BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
 501        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 502        .address = addr, \
 503        .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
 504        .scan_type = { \
 505                .sign = 's', \
 506                .realbits = (bits), \
 507                .storagebits = 16, \
 508                .shift = 0, \
 509                .endianness = IIO_BE, \
 510        }, \
 511}
 512
 513#define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
 514        .type = IIO_ACCEL, \
 515        .modified = 1, \
 516        .channel2 = IIO_MOD_ ## mod, \
 517        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 518                BIT(IIO_CHAN_INFO_CALIBBIAS), \
 519        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
 520                BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
 521        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 522        .address = (addr), \
 523        .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
 524        .scan_type = { \
 525                .sign = 's', \
 526                .realbits = (bits), \
 527                .storagebits = 16, \
 528                .shift = 0, \
 529                .endianness = IIO_BE, \
 530        }, \
 531}
 532
 533#define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
 534        .type = IIO_MAGN, \
 535        .modified = 1, \
 536        .channel2 = IIO_MOD_ ## mod, \
 537        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
 538        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
 539                BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
 540        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 541        .address = (addr), \
 542        .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
 543        .scan_type = { \
 544                .sign = 's', \
 545                .realbits = (bits), \
 546                .storagebits = 16, \
 547                .shift = 0, \
 548                .endianness = IIO_BE, \
 549        }, \
 550}
 551
 552#define ADIS16400_MOD_TEMP_NAME_X "x"
 553#define ADIS16400_MOD_TEMP_NAME_Y "y"
 554#define ADIS16400_MOD_TEMP_NAME_Z "z"
 555
 556#define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
 557        .type = IIO_TEMP, \
 558        .indexed = 1, \
 559        .channel = 0, \
 560        .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
 561        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 562                BIT(IIO_CHAN_INFO_OFFSET) | \
 563                BIT(IIO_CHAN_INFO_SCALE), \
 564        .info_mask_shared_by_type = \
 565                BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
 566        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 567        .address = (addr), \
 568        .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
 569        .scan_type = { \
 570                .sign = 's', \
 571                .realbits = (bits), \
 572                .storagebits = 16, \
 573                .shift = 0, \
 574                .endianness = IIO_BE, \
 575        }, \
 576}
 577
 578#define ADIS16400_TEMP_CHAN(addr, bits) { \
 579        .type = IIO_TEMP, \
 580        .indexed = 1, \
 581        .channel = 0, \
 582        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 583                BIT(IIO_CHAN_INFO_OFFSET) | \
 584                BIT(IIO_CHAN_INFO_SCALE), \
 585        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 586        .address = (addr), \
 587        .scan_index = ADIS16350_SCAN_TEMP_X, \
 588        .scan_type = { \
 589                .sign = 's', \
 590                .realbits = (bits), \
 591                .storagebits = 16, \
 592                .shift = 0, \
 593                .endianness = IIO_BE, \
 594        }, \
 595}
 596
 597#define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
 598        .type = IIO_INCLI, \
 599        .modified = 1, \
 600        .channel2 = IIO_MOD_ ## mod, \
 601        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
 602        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
 603        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 604        .address = (addr), \
 605        .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
 606        .scan_type = { \
 607                .sign = 's', \
 608                .realbits = (bits), \
 609                .storagebits = 16, \
 610                .shift = 0, \
 611                .endianness = IIO_BE, \
 612        }, \
 613}
 614
 615static const struct iio_chan_spec adis16400_channels[] = {
 616        ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
 617        ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
 618        ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
 619        ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
 620        ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
 621        ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
 622        ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
 623        ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
 624        ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
 625        ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
 626        ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
 627        ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
 628        IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
 629};
 630
 631static const struct iio_chan_spec adis16445_channels[] = {
 632        ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
 633        ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
 634        ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
 635        ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
 636        ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
 637        ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
 638        ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
 639        IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
 640};
 641
 642static const struct iio_chan_spec adis16448_channels[] = {
 643        ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
 644        ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
 645        ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
 646        ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
 647        ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
 648        ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
 649        ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
 650        ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
 651        ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
 652        {
 653                .type = IIO_PRESSURE,
 654                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 655                .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
 656                .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 657                .address = ADIS16448_BARO_OUT,
 658                .scan_index = ADIS16400_SCAN_BARO,
 659                .scan_type = {
 660                        .sign = 's',
 661                        .realbits = 16,
 662                        .storagebits = 16,
 663                        .endianness = IIO_BE,
 664                },
 665        },
 666        ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
 667        IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
 668};
 669
 670static const struct iio_chan_spec adis16350_channels[] = {
 671        ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
 672        ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
 673        ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
 674        ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
 675        ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
 676        ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
 677        ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
 678        ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
 679        ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
 680        ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
 681        ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
 682        ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
 683        ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
 684        ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
 685        IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
 686};
 687
 688static const struct iio_chan_spec adis16300_channels[] = {
 689        ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
 690        ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
 691        ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
 692        ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
 693        ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
 694        ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
 695        ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
 696        ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
 697        ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
 698        IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
 699};
 700
 701static const struct iio_chan_spec adis16334_channels[] = {
 702        ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
 703        ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
 704        ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
 705        ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
 706        ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
 707        ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
 708        ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
 709        IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
 710};
 711
 712static struct adis16400_chip_info adis16400_chips[] = {
 713        [ADIS16300] = {
 714                .channels = adis16300_channels,
 715                .num_channels = ARRAY_SIZE(adis16300_channels),
 716                .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
 717                                ADIS16400_HAS_SERIAL_NUMBER,
 718                .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
 719                .accel_scale_micro = 5884,
 720                .temp_scale_nano = 140000000, /* 0.14 C */
 721                .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
 722                .set_freq = adis16400_set_freq,
 723                .get_freq = adis16400_get_freq,
 724        },
 725        [ADIS16334] = {
 726                .channels = adis16334_channels,
 727                .num_channels = ARRAY_SIZE(adis16334_channels),
 728                .flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
 729                                ADIS16400_HAS_SERIAL_NUMBER,
 730                .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
 731                .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
 732                .temp_scale_nano = 67850000, /* 0.06785 C */
 733                .temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
 734                .set_freq = adis16334_set_freq,
 735                .get_freq = adis16334_get_freq,
 736        },
 737        [ADIS16350] = {
 738                .channels = adis16350_channels,
 739                .num_channels = ARRAY_SIZE(adis16350_channels),
 740                .gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
 741                .accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
 742                .temp_scale_nano = 145300000, /* 0.1453 C */
 743                .temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
 744                .flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
 745                .set_freq = adis16400_set_freq,
 746                .get_freq = adis16400_get_freq,
 747        },
 748        [ADIS16360] = {
 749                .channels = adis16350_channels,
 750                .num_channels = ARRAY_SIZE(adis16350_channels),
 751                .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
 752                                ADIS16400_HAS_SERIAL_NUMBER,
 753                .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
 754                .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
 755                .temp_scale_nano = 136000000, /* 0.136 C */
 756                .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
 757                .set_freq = adis16400_set_freq,
 758                .get_freq = adis16400_get_freq,
 759        },
 760        [ADIS16362] = {
 761                .channels = adis16350_channels,
 762                .num_channels = ARRAY_SIZE(adis16350_channels),
 763                .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
 764                                ADIS16400_HAS_SERIAL_NUMBER,
 765                .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
 766                .accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
 767                .temp_scale_nano = 136000000, /* 0.136 C */
 768                .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
 769                .set_freq = adis16400_set_freq,
 770                .get_freq = adis16400_get_freq,
 771        },
 772        [ADIS16364] = {
 773                .channels = adis16350_channels,
 774                .num_channels = ARRAY_SIZE(adis16350_channels),
 775                .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
 776                                ADIS16400_HAS_SERIAL_NUMBER,
 777                .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
 778                .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
 779                .temp_scale_nano = 136000000, /* 0.136 C */
 780                .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
 781                .set_freq = adis16400_set_freq,
 782                .get_freq = adis16400_get_freq,
 783        },
 784        [ADIS16367] = {
 785                .channels = adis16350_channels,
 786                .num_channels = ARRAY_SIZE(adis16350_channels),
 787                .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
 788                                ADIS16400_HAS_SERIAL_NUMBER,
 789                .gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
 790                .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
 791                .temp_scale_nano = 136000000, /* 0.136 C */
 792                .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
 793                .set_freq = adis16400_set_freq,
 794                .get_freq = adis16400_get_freq,
 795        },
 796        [ADIS16400] = {
 797                .channels = adis16400_channels,
 798                .num_channels = ARRAY_SIZE(adis16400_channels),
 799                .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
 800                .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
 801                .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
 802                .temp_scale_nano = 140000000, /* 0.14 C */
 803                .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
 804                .set_freq = adis16400_set_freq,
 805                .get_freq = adis16400_get_freq,
 806        },
 807        [ADIS16445] = {
 808                .channels = adis16445_channels,
 809                .num_channels = ARRAY_SIZE(adis16445_channels),
 810                .flags = ADIS16400_HAS_PROD_ID |
 811                                ADIS16400_HAS_SERIAL_NUMBER |
 812                                ADIS16400_BURST_DIAG_STAT,
 813                .gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
 814                .accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */
 815                .temp_scale_nano = 73860000, /* 0.07386 C */
 816                .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
 817                .set_freq = adis16334_set_freq,
 818                .get_freq = adis16334_get_freq,
 819        },
 820        [ADIS16448] = {
 821                .channels = adis16448_channels,
 822                .num_channels = ARRAY_SIZE(adis16448_channels),
 823                .flags = ADIS16400_HAS_PROD_ID |
 824                                ADIS16400_HAS_SERIAL_NUMBER |
 825                                ADIS16400_BURST_DIAG_STAT,
 826                .gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
 827                .accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
 828                .temp_scale_nano = 73860000, /* 0.07386 C */
 829                .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
 830                .set_freq = adis16334_set_freq,
 831                .get_freq = adis16334_get_freq,
 832        }
 833};
 834
 835static const struct iio_info adis16400_info = {
 836        .driver_module = THIS_MODULE,
 837        .read_raw = &adis16400_read_raw,
 838        .write_raw = &adis16400_write_raw,
 839        .update_scan_mode = adis16400_update_scan_mode,
 840        .debugfs_reg_access = adis_debugfs_reg_access,
 841};
 842
 843static const char * const adis16400_status_error_msgs[] = {
 844        [ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
 845        [ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
 846        [ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
 847        [ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
 848        [ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
 849        [ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
 850        [ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
 851        [ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
 852        [ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
 853        [ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
 854        [ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
 855        [ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
 856        [ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
 857        [ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
 858        [ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
 859};
 860
 861static const struct adis_data adis16400_data = {
 862        .msc_ctrl_reg = ADIS16400_MSC_CTRL,
 863        .glob_cmd_reg = ADIS16400_GLOB_CMD,
 864        .diag_stat_reg = ADIS16400_DIAG_STAT,
 865
 866        .read_delay = 50,
 867        .write_delay = 50,
 868
 869        .self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
 870        .startup_delay = ADIS16400_STARTUP_DELAY,
 871
 872        .status_error_msgs = adis16400_status_error_msgs,
 873        .status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
 874                BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
 875                BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
 876                BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
 877                BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
 878                BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
 879                BIT(ADIS16400_DIAG_STAT_ALARM2) |
 880                BIT(ADIS16400_DIAG_STAT_ALARM1) |
 881                BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
 882                BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
 883                BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
 884                BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
 885                BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
 886                BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
 887                BIT(ADIS16400_DIAG_STAT_POWER_LOW),
 888};
 889
 890static void adis16400_setup_chan_mask(struct adis16400_state *st)
 891{
 892        const struct adis16400_chip_info *chip_info = st->variant;
 893        unsigned i;
 894
 895        for (i = 0; i < chip_info->num_channels; i++) {
 896                const struct iio_chan_spec *ch = &chip_info->channels[i];
 897
 898                if (ch->scan_index >= 0 &&
 899                    ch->scan_index != ADIS16400_SCAN_TIMESTAMP)
 900                        st->avail_scan_mask[0] |= BIT(ch->scan_index);
 901        }
 902}
 903
 904static int adis16400_probe(struct spi_device *spi)
 905{
 906        struct adis16400_state *st;
 907        struct iio_dev *indio_dev;
 908        int ret;
 909
 910        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 911        if (indio_dev == NULL)
 912                return -ENOMEM;
 913
 914        st = iio_priv(indio_dev);
 915        /* this is only used for removal purposes */
 916        spi_set_drvdata(spi, indio_dev);
 917
 918        /* setup the industrialio driver allocated elements */
 919        st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
 920        indio_dev->dev.parent = &spi->dev;
 921        indio_dev->name = spi_get_device_id(spi)->name;
 922        indio_dev->channels = st->variant->channels;
 923        indio_dev->num_channels = st->variant->num_channels;
 924        indio_dev->info = &adis16400_info;
 925        indio_dev->modes = INDIO_DIRECT_MODE;
 926
 927        if (!(st->variant->flags & ADIS16400_NO_BURST)) {
 928                adis16400_setup_chan_mask(st);
 929                indio_dev->available_scan_masks = st->avail_scan_mask;
 930        }
 931
 932        ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
 933        if (ret)
 934                return ret;
 935
 936        ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
 937                        adis16400_trigger_handler);
 938        if (ret)
 939                return ret;
 940
 941        /* Get the device into a sane initial state */
 942        ret = adis16400_initial_setup(indio_dev);
 943        if (ret)
 944                goto error_cleanup_buffer;
 945        ret = iio_device_register(indio_dev);
 946        if (ret)
 947                goto error_cleanup_buffer;
 948
 949        adis16400_debugfs_init(indio_dev);
 950        return 0;
 951
 952error_cleanup_buffer:
 953        adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
 954        return ret;
 955}
 956
 957static int adis16400_remove(struct spi_device *spi)
 958{
 959        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 960        struct adis16400_state *st = iio_priv(indio_dev);
 961
 962        iio_device_unregister(indio_dev);
 963        adis16400_stop_device(indio_dev);
 964
 965        adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
 966
 967        return 0;
 968}
 969
 970static const struct spi_device_id adis16400_id[] = {
 971        {"adis16300", ADIS16300},
 972        {"adis16305", ADIS16300},
 973        {"adis16334", ADIS16334},
 974        {"adis16350", ADIS16350},
 975        {"adis16354", ADIS16350},
 976        {"adis16355", ADIS16350},
 977        {"adis16360", ADIS16360},
 978        {"adis16362", ADIS16362},
 979        {"adis16364", ADIS16364},
 980        {"adis16365", ADIS16360},
 981        {"adis16367", ADIS16367},
 982        {"adis16400", ADIS16400},
 983        {"adis16405", ADIS16400},
 984        {"adis16445", ADIS16445},
 985        {"adis16448", ADIS16448},
 986        {}
 987};
 988MODULE_DEVICE_TABLE(spi, adis16400_id);
 989
 990static struct spi_driver adis16400_driver = {
 991        .driver = {
 992                .name = "adis16400",
 993        },
 994        .id_table = adis16400_id,
 995        .probe = adis16400_probe,
 996        .remove = adis16400_remove,
 997};
 998module_spi_driver(adis16400_driver);
 999
1000MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
1001MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
1002MODULE_LICENSE("GPL v2");
1003