linux/drivers/staging/iio/accel/adis16240.c
<<
>>
Prefs
   1/*
   2 * ADIS16240 Programmable Impact Sensor and Recorder driver
   3 *
   4 * Copyright 2010 Analog Devices Inc.
   5 *
   6 * Licensed under the GPL-2 or later.
   7 */
   8
   9#include <linux/interrupt.h>
  10#include <linux/irq.h>
  11#include <linux/gpio.h>
  12#include <linux/delay.h>
  13#include <linux/device.h>
  14#include <linux/kernel.h>
  15#include <linux/spi/spi.h>
  16#include <linux/slab.h>
  17#include <linux/sysfs.h>
  18#include <linux/list.h>
  19#include <linux/module.h>
  20
  21#include <linux/iio/iio.h>
  22#include <linux/iio/sysfs.h>
  23#include <linux/iio/buffer.h>
  24#include <linux/iio/imu/adis.h>
  25
  26#define ADIS16240_STARTUP_DELAY 220 /* ms */
  27
  28/* Flash memory write count */
  29#define ADIS16240_FLASH_CNT      0x00
  30
  31/* Output, power supply */
  32#define ADIS16240_SUPPLY_OUT     0x02
  33
  34/* Output, x-axis accelerometer */
  35#define ADIS16240_XACCL_OUT      0x04
  36
  37/* Output, y-axis accelerometer */
  38#define ADIS16240_YACCL_OUT      0x06
  39
  40/* Output, z-axis accelerometer */
  41#define ADIS16240_ZACCL_OUT      0x08
  42
  43/* Output, auxiliary ADC input */
  44#define ADIS16240_AUX_ADC        0x0A
  45
  46/* Output, temperature */
  47#define ADIS16240_TEMP_OUT       0x0C
  48
  49/* Output, x-axis acceleration peak */
  50#define ADIS16240_XPEAK_OUT      0x0E
  51
  52/* Output, y-axis acceleration peak */
  53#define ADIS16240_YPEAK_OUT      0x10
  54
  55/* Output, z-axis acceleration peak */
  56#define ADIS16240_ZPEAK_OUT      0x12
  57
  58/* Output, sum-of-squares acceleration peak */
  59#define ADIS16240_XYZPEAK_OUT    0x14
  60
  61/* Output, Capture Buffer 1, X and Y acceleration */
  62#define ADIS16240_CAPT_BUF1      0x16
  63
  64/* Output, Capture Buffer 2, Z acceleration */
  65#define ADIS16240_CAPT_BUF2      0x18
  66
  67/* Diagnostic, error flags */
  68#define ADIS16240_DIAG_STAT      0x1A
  69
  70/* Diagnostic, event counter */
  71#define ADIS16240_EVNT_CNTR      0x1C
  72
  73/* Diagnostic, check sum value from firmware test */
  74#define ADIS16240_CHK_SUM        0x1E
  75
  76/* Calibration, x-axis acceleration offset adjustment */
  77#define ADIS16240_XACCL_OFF      0x20
  78
  79/* Calibration, y-axis acceleration offset adjustment */
  80#define ADIS16240_YACCL_OFF      0x22
  81
  82/* Calibration, z-axis acceleration offset adjustment */
  83#define ADIS16240_ZACCL_OFF      0x24
  84
  85/* Clock, hour and minute */
  86#define ADIS16240_CLK_TIME       0x2E
  87
  88/* Clock, month and day */
  89#define ADIS16240_CLK_DATE       0x30
  90
  91/* Clock, year */
  92#define ADIS16240_CLK_YEAR       0x32
  93
  94/* Wake-up setting, hour and minute */
  95#define ADIS16240_WAKE_TIME      0x34
  96
  97/* Wake-up setting, month and day */
  98#define ADIS16240_WAKE_DATE      0x36
  99
 100/* Alarm 1 amplitude threshold */
 101#define ADIS16240_ALM_MAG1       0x38
 102
 103/* Alarm 2 amplitude threshold */
 104#define ADIS16240_ALM_MAG2       0x3A
 105
 106/* Alarm control */
 107#define ADIS16240_ALM_CTRL       0x3C
 108
 109/* Capture, external trigger control */
 110#define ADIS16240_XTRIG_CTRL     0x3E
 111
 112/* Capture, address pointer */
 113#define ADIS16240_CAPT_PNTR      0x40
 114
 115/* Capture, configuration and control */
 116#define ADIS16240_CAPT_CTRL      0x42
 117
 118/* General-purpose digital input/output control */
 119#define ADIS16240_GPIO_CTRL      0x44
 120
 121/* Miscellaneous control */
 122#define ADIS16240_MSC_CTRL       0x46
 123
 124/* Internal sample period (rate) control */
 125#define ADIS16240_SMPL_PRD       0x48
 126
 127/* System command */
 128#define ADIS16240_GLOB_CMD       0x4A
 129
 130/* MSC_CTRL */
 131
 132/* Enables sum-of-squares output (XYZPEAK_OUT) */
 133#define ADIS16240_MSC_CTRL_XYZPEAK_OUT_EN       BIT(15)
 134
 135/* Enables peak tracking output (XPEAK_OUT, YPEAK_OUT, and ZPEAK_OUT) */
 136#define ADIS16240_MSC_CTRL_X_Y_ZPEAK_OUT_EN     BIT(14)
 137
 138/* Self-test enable: 1 = apply electrostatic force, 0 = disabled */
 139#define ADIS16240_MSC_CTRL_SELF_TEST_EN         BIT(8)
 140
 141/* Data-ready enable: 1 = enabled, 0 = disabled */
 142#define ADIS16240_MSC_CTRL_DATA_RDY_EN          BIT(2)
 143
 144/* Data-ready polarity: 1 = active high, 0 = active low */
 145#define ADIS16240_MSC_CTRL_ACTIVE_HIGH          BIT(1)
 146
 147/* Data-ready line selection: 1 = DIO2, 0 = DIO1 */
 148#define ADIS16240_MSC_CTRL_DATA_RDY_DIO2        BIT(0)
 149
 150/* DIAG_STAT */
 151
 152/* Alarm 2 status: 1 = alarm active, 0 = alarm inactive */
 153#define ADIS16240_DIAG_STAT_ALARM2      BIT(9)
 154
 155/* Alarm 1 status: 1 = alarm active, 0 = alarm inactive */
 156#define ADIS16240_DIAG_STAT_ALARM1      BIT(8)
 157
 158/* Capture buffer full: 1 = capture buffer is full */
 159#define ADIS16240_DIAG_STAT_CPT_BUF_FUL BIT(7)
 160
 161/* Flash test, checksum flag: 1 = mismatch, 0 = match */
 162#define ADIS16240_DIAG_STAT_CHKSUM      BIT(6)
 163
 164/* Power-on, self-test flag: 1 = failure, 0 = pass */
 165#define ADIS16240_DIAG_STAT_PWRON_FAIL_BIT  5
 166
 167/* Power-on self-test: 1 = in-progress, 0 = complete */
 168#define ADIS16240_DIAG_STAT_PWRON_BUSY  BIT(4)
 169
 170/* SPI communications failure */
 171#define ADIS16240_DIAG_STAT_SPI_FAIL_BIT        3
 172
 173/* Flash update failure */
 174#define ADIS16240_DIAG_STAT_FLASH_UPT_BIT       2
 175
 176/* Power supply above 3.625 V */
 177#define ADIS16240_DIAG_STAT_POWER_HIGH_BIT      1
 178
 179 /* Power supply below 3.15 V */
 180#define ADIS16240_DIAG_STAT_POWER_LOW_BIT       0
 181
 182/* GLOB_CMD */
 183
 184#define ADIS16240_GLOB_CMD_RESUME       BIT(8)
 185#define ADIS16240_GLOB_CMD_SW_RESET     BIT(7)
 186#define ADIS16240_GLOB_CMD_STANDBY      BIT(2)
 187
 188#define ADIS16240_ERROR_ACTIVE          BIT(14)
 189
 190/* At the moment triggers are only used for ring buffer
 191 * filling. This may change!
 192 */
 193
 194enum adis16240_scan {
 195        ADIS16240_SCAN_ACC_X,
 196        ADIS16240_SCAN_ACC_Y,
 197        ADIS16240_SCAN_ACC_Z,
 198        ADIS16240_SCAN_SUPPLY,
 199        ADIS16240_SCAN_AUX_ADC,
 200        ADIS16240_SCAN_TEMP,
 201};
 202
 203static ssize_t adis16240_spi_read_signed(struct device *dev,
 204                                         struct device_attribute *attr,
 205                                         char *buf,
 206                                         unsigned int bits)
 207{
 208        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 209        struct adis *st = iio_priv(indio_dev);
 210        int ret;
 211        s16 val = 0;
 212        unsigned int shift = 16 - bits;
 213        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 214
 215        ret = adis_read_reg_16(st,
 216                               this_attr->address, (u16 *)&val);
 217        if (ret)
 218                return ret;
 219
 220        if (val & ADIS16240_ERROR_ACTIVE)
 221                adis_check_status(st);
 222
 223        val = (s16)(val << shift) >> shift;
 224        return sprintf(buf, "%d\n", val);
 225}
 226
 227static ssize_t adis16240_read_12bit_signed(struct device *dev,
 228                                           struct device_attribute *attr,
 229                                           char *buf)
 230{
 231        return adis16240_spi_read_signed(dev, attr, buf, 12);
 232}
 233
 234static IIO_DEVICE_ATTR(in_accel_xyz_squared_peak_raw, 0444,
 235                       adis16240_read_12bit_signed, NULL,
 236                       ADIS16240_XYZPEAK_OUT);
 237
 238static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
 239
 240static const u8 adis16240_addresses[][2] = {
 241        [ADIS16240_SCAN_ACC_X] = { ADIS16240_XACCL_OFF, ADIS16240_XPEAK_OUT },
 242        [ADIS16240_SCAN_ACC_Y] = { ADIS16240_YACCL_OFF, ADIS16240_YPEAK_OUT },
 243        [ADIS16240_SCAN_ACC_Z] = { ADIS16240_ZACCL_OFF, ADIS16240_ZPEAK_OUT },
 244};
 245
 246static int adis16240_read_raw(struct iio_dev *indio_dev,
 247                              struct iio_chan_spec const *chan,
 248                              int *val, int *val2,
 249                              long mask)
 250{
 251        struct adis *st = iio_priv(indio_dev);
 252        int ret;
 253        int bits;
 254        u8 addr;
 255        s16 val16;
 256
 257        switch (mask) {
 258        case IIO_CHAN_INFO_RAW:
 259                return adis_single_conversion(indio_dev, chan,
 260                                ADIS16240_ERROR_ACTIVE, val);
 261        case IIO_CHAN_INFO_SCALE:
 262                switch (chan->type) {
 263                case IIO_VOLTAGE:
 264                        if (chan->channel == 0) {
 265                                *val = 4;
 266                                *val2 = 880000; /* 4.88 mV */
 267                                return IIO_VAL_INT_PLUS_MICRO;
 268                        }
 269                        return -EINVAL;
 270                case IIO_TEMP:
 271                        *val = 244; /* 0.244 C */
 272                        *val2 = 0;
 273                        return IIO_VAL_INT_PLUS_MICRO;
 274                case IIO_ACCEL:
 275                        *val = 0;
 276                        *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
 277                        return IIO_VAL_INT_PLUS_MICRO;
 278                default:
 279                        return -EINVAL;
 280                }
 281                break;
 282        case IIO_CHAN_INFO_PEAK_SCALE:
 283                *val = 0;
 284                *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
 285                return IIO_VAL_INT_PLUS_MICRO;
 286        case IIO_CHAN_INFO_OFFSET:
 287                *val = 25000 / 244 - 0x133; /* 25 C = 0x133 */
 288                return IIO_VAL_INT;
 289        case IIO_CHAN_INFO_CALIBBIAS:
 290                bits = 10;
 291                addr = adis16240_addresses[chan->scan_index][0];
 292                ret = adis_read_reg_16(st, addr, &val16);
 293                if (ret)
 294                        return ret;
 295                val16 &= (1 << bits) - 1;
 296                val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
 297                *val = val16;
 298                return IIO_VAL_INT;
 299        case IIO_CHAN_INFO_PEAK:
 300                bits = 10;
 301                addr = adis16240_addresses[chan->scan_index][1];
 302                ret = adis_read_reg_16(st, addr, &val16);
 303                if (ret)
 304                        return ret;
 305                val16 &= (1 << bits) - 1;
 306                val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
 307                *val = val16;
 308                return IIO_VAL_INT;
 309        }
 310        return -EINVAL;
 311}
 312
 313static int adis16240_write_raw(struct iio_dev *indio_dev,
 314                               struct iio_chan_spec const *chan,
 315                               int val,
 316                               int val2,
 317                               long mask)
 318{
 319        struct adis *st = iio_priv(indio_dev);
 320        int bits = 10;
 321        s16 val16;
 322        u8 addr;
 323
 324        switch (mask) {
 325        case IIO_CHAN_INFO_CALIBBIAS:
 326                val16 = val & ((1 << bits) - 1);
 327                addr = adis16240_addresses[chan->scan_index][0];
 328                return adis_write_reg_16(st, addr, val16);
 329        }
 330        return -EINVAL;
 331}
 332
 333static const struct iio_chan_spec adis16240_channels[] = {
 334        ADIS_SUPPLY_CHAN(ADIS16240_SUPPLY_OUT, ADIS16240_SCAN_SUPPLY, 0, 10),
 335        ADIS_AUX_ADC_CHAN(ADIS16240_AUX_ADC, ADIS16240_SCAN_AUX_ADC, 0, 10),
 336        ADIS_ACCEL_CHAN(X, ADIS16240_XACCL_OUT, ADIS16240_SCAN_ACC_X,
 337                        BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
 338                        0, 10),
 339        ADIS_ACCEL_CHAN(Y, ADIS16240_YACCL_OUT, ADIS16240_SCAN_ACC_Y,
 340                        BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
 341                        0, 10),
 342        ADIS_ACCEL_CHAN(Z, ADIS16240_ZACCL_OUT, ADIS16240_SCAN_ACC_Z,
 343                        BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
 344                        0, 10),
 345        ADIS_TEMP_CHAN(ADIS16240_TEMP_OUT, ADIS16240_SCAN_TEMP, 0, 10),
 346        IIO_CHAN_SOFT_TIMESTAMP(6)
 347};
 348
 349static struct attribute *adis16240_attributes[] = {
 350        &iio_dev_attr_in_accel_xyz_squared_peak_raw.dev_attr.attr,
 351        &iio_const_attr_sampling_frequency_available.dev_attr.attr,
 352        NULL
 353};
 354
 355static const struct attribute_group adis16240_attribute_group = {
 356        .attrs = adis16240_attributes,
 357};
 358
 359static const struct iio_info adis16240_info = {
 360        .attrs = &adis16240_attribute_group,
 361        .read_raw = adis16240_read_raw,
 362        .write_raw = adis16240_write_raw,
 363        .update_scan_mode = adis_update_scan_mode,
 364};
 365
 366static const char * const adis16240_status_error_msgs[] = {
 367        [ADIS16240_DIAG_STAT_PWRON_FAIL_BIT] = "Power on, self-test failed",
 368        [ADIS16240_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
 369        [ADIS16240_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
 370        [ADIS16240_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
 371        [ADIS16240_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.225V",
 372};
 373
 374static const struct adis_data adis16240_data = {
 375        .write_delay = 35,
 376        .read_delay = 35,
 377        .msc_ctrl_reg = ADIS16240_MSC_CTRL,
 378        .glob_cmd_reg = ADIS16240_GLOB_CMD,
 379        .diag_stat_reg = ADIS16240_DIAG_STAT,
 380
 381        .self_test_mask = ADIS16240_MSC_CTRL_SELF_TEST_EN,
 382        .self_test_no_autoclear = true,
 383        .startup_delay = ADIS16240_STARTUP_DELAY,
 384
 385        .status_error_msgs = adis16240_status_error_msgs,
 386        .status_error_mask = BIT(ADIS16240_DIAG_STAT_PWRON_FAIL_BIT) |
 387                BIT(ADIS16240_DIAG_STAT_SPI_FAIL_BIT) |
 388                BIT(ADIS16240_DIAG_STAT_FLASH_UPT_BIT) |
 389                BIT(ADIS16240_DIAG_STAT_POWER_HIGH_BIT) |
 390                BIT(ADIS16240_DIAG_STAT_POWER_LOW_BIT),
 391};
 392
 393static int adis16240_probe(struct spi_device *spi)
 394{
 395        int ret;
 396        struct adis *st;
 397        struct iio_dev *indio_dev;
 398
 399        /* setup the industrialio driver allocated elements */
 400        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 401        if (!indio_dev)
 402                return -ENOMEM;
 403        st = iio_priv(indio_dev);
 404        /* this is only used for removal purposes */
 405        spi_set_drvdata(spi, indio_dev);
 406
 407        indio_dev->name = spi->dev.driver->name;
 408        indio_dev->dev.parent = &spi->dev;
 409        indio_dev->info = &adis16240_info;
 410        indio_dev->channels = adis16240_channels;
 411        indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
 412        indio_dev->modes = INDIO_DIRECT_MODE;
 413
 414        ret = adis_init(st, indio_dev, spi, &adis16240_data);
 415        if (ret)
 416                return ret;
 417        ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
 418        if (ret)
 419                return ret;
 420
 421        /* Get the device into a sane initial state */
 422        ret = adis_initial_startup(st);
 423        if (ret)
 424                goto error_cleanup_buffer_trigger;
 425        ret = iio_device_register(indio_dev);
 426        if (ret)
 427                goto error_cleanup_buffer_trigger;
 428        return 0;
 429
 430error_cleanup_buffer_trigger:
 431        adis_cleanup_buffer_and_trigger(st, indio_dev);
 432        return ret;
 433}
 434
 435static int adis16240_remove(struct spi_device *spi)
 436{
 437        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 438        struct adis *st = iio_priv(indio_dev);
 439
 440        iio_device_unregister(indio_dev);
 441        adis_cleanup_buffer_and_trigger(st, indio_dev);
 442
 443        return 0;
 444}
 445
 446static struct spi_driver adis16240_driver = {
 447        .driver = {
 448                .name = "adis16240",
 449        },
 450        .probe = adis16240_probe,
 451        .remove = adis16240_remove,
 452};
 453module_spi_driver(adis16240_driver);
 454
 455MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
 456MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
 457MODULE_LICENSE("GPL v2");
 458MODULE_ALIAS("spi:adis16240");
 459