linux/drivers/staging/iio/gyro/adis16260_core.c
<<
>>
Prefs
   1/*
   2 * ADIS16260/ADIS16265 Programmable Digital Gyroscope Sensor 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/mutex.h>
  14#include <linux/device.h>
  15#include <linux/kernel.h>
  16#include <linux/spi/spi.h>
  17#include <linux/slab.h>
  18#include <linux/sysfs.h>
  19#include <linux/list.h>
  20
  21#include "../iio.h"
  22#include "../sysfs.h"
  23#include "../ring_generic.h"
  24#include "../adc/adc.h"
  25#include "gyro.h"
  26
  27#include "adis16260.h"
  28
  29#define DRIVER_NAME             "adis16260"
  30
  31static int adis16260_check_status(struct device *dev);
  32
  33/**
  34 * adis16260_spi_write_reg_8() - write single byte to a register
  35 * @dev: device associated with child of actual device (iio_dev or iio_trig)
  36 * @reg_address: the address of the register to be written
  37 * @val: the value to write
  38 **/
  39static int adis16260_spi_write_reg_8(struct device *dev,
  40                u8 reg_address,
  41                u8 val)
  42{
  43        int ret;
  44        struct iio_dev *indio_dev = dev_get_drvdata(dev);
  45        struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
  46
  47        mutex_lock(&st->buf_lock);
  48        st->tx[0] = ADIS16260_WRITE_REG(reg_address);
  49        st->tx[1] = val;
  50
  51        ret = spi_write(st->us, st->tx, 2);
  52        mutex_unlock(&st->buf_lock);
  53
  54        return ret;
  55}
  56
  57/**
  58 * adis16260_spi_write_reg_16() - write 2 bytes to a pair of registers
  59 * @dev: device associated with child of actual device (iio_dev or iio_trig)
  60 * @reg_address: the address of the lower of the two registers. Second register
  61 *               is assumed to have address one greater.
  62 * @val: value to be written
  63 **/
  64static int adis16260_spi_write_reg_16(struct device *dev,
  65                u8 lower_reg_address,
  66                u16 value)
  67{
  68        int ret;
  69        struct spi_message msg;
  70        struct iio_dev *indio_dev = dev_get_drvdata(dev);
  71        struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
  72        struct spi_transfer xfers[] = {
  73                {
  74                        .tx_buf = st->tx,
  75                        .bits_per_word = 8,
  76                        .len = 2,
  77                        .cs_change = 1,
  78                        .delay_usecs = 20,
  79                }, {
  80                        .tx_buf = st->tx + 2,
  81                        .bits_per_word = 8,
  82                        .len = 2,
  83                        .cs_change = 1,
  84                        .delay_usecs = 20,
  85                },
  86        };
  87
  88        mutex_lock(&st->buf_lock);
  89        st->tx[0] = ADIS16260_WRITE_REG(lower_reg_address);
  90        st->tx[1] = value & 0xFF;
  91        st->tx[2] = ADIS16260_WRITE_REG(lower_reg_address + 1);
  92        st->tx[3] = (value >> 8) & 0xFF;
  93
  94        spi_message_init(&msg);
  95        spi_message_add_tail(&xfers[0], &msg);
  96        spi_message_add_tail(&xfers[1], &msg);
  97        ret = spi_sync(st->us, &msg);
  98        mutex_unlock(&st->buf_lock);
  99
 100        return ret;
 101}
 102
 103/**
 104 * adis16260_spi_read_reg_16() - read 2 bytes from a 16-bit register
 105 * @dev: device associated with child of actual device (iio_dev or iio_trig)
 106 * @reg_address: the address of the lower of the two registers. Second register
 107 *               is assumed to have address one greater.
 108 * @val: somewhere to pass back the value read
 109 **/
 110static int adis16260_spi_read_reg_16(struct device *dev,
 111                u8 lower_reg_address,
 112                u16 *val)
 113{
 114        struct spi_message msg;
 115        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 116        struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
 117        int ret;
 118        struct spi_transfer xfers[] = {
 119                {
 120                        .tx_buf = st->tx,
 121                        .bits_per_word = 8,
 122                        .len = 2,
 123                        .cs_change = 1,
 124                        .delay_usecs = 30,
 125                }, {
 126                        .rx_buf = st->rx,
 127                        .bits_per_word = 8,
 128                        .len = 2,
 129                        .cs_change = 1,
 130                        .delay_usecs = 30,
 131                },
 132        };
 133
 134        mutex_lock(&st->buf_lock);
 135        st->tx[0] = ADIS16260_READ_REG(lower_reg_address);
 136        st->tx[1] = 0;
 137
 138        spi_message_init(&msg);
 139        spi_message_add_tail(&xfers[0], &msg);
 140        spi_message_add_tail(&xfers[1], &msg);
 141        ret = spi_sync(st->us, &msg);
 142        if (ret) {
 143                dev_err(&st->us->dev,
 144                        "problem when reading 16 bit register 0x%02X",
 145                        lower_reg_address);
 146                goto error_ret;
 147        }
 148        *val = (st->rx[0] << 8) | st->rx[1];
 149
 150error_ret:
 151        mutex_unlock(&st->buf_lock);
 152        return ret;
 153}
 154
 155static ssize_t adis16260_spi_read_signed(struct device *dev,
 156                struct device_attribute *attr,
 157                char *buf,
 158                unsigned bits)
 159{
 160        int ret;
 161        s16 val = 0;
 162        unsigned shift = 16 - bits;
 163        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 164
 165        ret = adis16260_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
 166        if (ret)
 167                return ret;
 168
 169        if (val & ADIS16260_ERROR_ACTIVE)
 170                adis16260_check_status(dev);
 171        val = ((s16)(val << shift) >> shift);
 172        return sprintf(buf, "%d\n", val);
 173}
 174
 175static ssize_t adis16260_read_12bit_unsigned(struct device *dev,
 176                struct device_attribute *attr,
 177                char *buf)
 178{
 179        int ret;
 180        u16 val = 0;
 181        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 182
 183        ret = adis16260_spi_read_reg_16(dev, this_attr->address, &val);
 184        if (ret)
 185                return ret;
 186
 187        if (val & ADIS16260_ERROR_ACTIVE)
 188                adis16260_check_status(dev);
 189
 190        return sprintf(buf, "%u\n", val & 0x0FFF);
 191}
 192
 193static ssize_t adis16260_read_12bit_signed(struct device *dev,
 194                struct device_attribute *attr,
 195                char *buf)
 196{
 197        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 198        ssize_t ret;
 199
 200        /* Take the iio_dev status lock */
 201        mutex_lock(&indio_dev->mlock);
 202        ret =  adis16260_spi_read_signed(dev, attr, buf, 12);
 203        mutex_unlock(&indio_dev->mlock);
 204
 205        return ret;
 206}
 207
 208static ssize_t adis16260_read_14bit_signed(struct device *dev,
 209                struct device_attribute *attr,
 210                char *buf)
 211{
 212        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 213        ssize_t ret;
 214
 215        /* Take the iio_dev status lock */
 216        mutex_lock(&indio_dev->mlock);
 217        ret =  adis16260_spi_read_signed(dev, attr, buf, 14);
 218        mutex_unlock(&indio_dev->mlock);
 219
 220        return ret;
 221}
 222
 223static ssize_t adis16260_write_16bit(struct device *dev,
 224                struct device_attribute *attr,
 225                const char *buf,
 226                size_t len)
 227{
 228        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 229        int ret;
 230        long val;
 231
 232        ret = strict_strtol(buf, 10, &val);
 233        if (ret)
 234                goto error_ret;
 235        ret = adis16260_spi_write_reg_16(dev, this_attr->address, val);
 236
 237error_ret:
 238        return ret ? ret : len;
 239}
 240
 241static ssize_t adis16260_read_frequency(struct device *dev,
 242                struct device_attribute *attr,
 243                char *buf)
 244{
 245        int ret, len = 0;
 246        u16 t;
 247        int sps;
 248        ret = adis16260_spi_read_reg_16(dev,
 249                        ADIS16260_SMPL_PRD,
 250                        &t);
 251        if (ret)
 252                return ret;
 253        sps =  (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048;
 254        sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
 255        len = sprintf(buf, "%d SPS\n", sps);
 256        return len;
 257}
 258
 259static ssize_t adis16260_write_frequency(struct device *dev,
 260                struct device_attribute *attr,
 261                const char *buf,
 262                size_t len)
 263{
 264        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 265        struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
 266        long val;
 267        int ret;
 268        u8 t;
 269
 270        ret = strict_strtol(buf, 10, &val);
 271        if (ret)
 272                return ret;
 273
 274        mutex_lock(&indio_dev->mlock);
 275
 276        t = (2048 / val);
 277        if (t > 0)
 278                t--;
 279        t &= ADIS16260_SMPL_PRD_DIV_MASK;
 280        if ((t & ADIS16260_SMPL_PRD_DIV_MASK) >= 0x0A)
 281                st->us->max_speed_hz = ADIS16260_SPI_SLOW;
 282        else
 283                st->us->max_speed_hz = ADIS16260_SPI_FAST;
 284
 285        ret = adis16260_spi_write_reg_8(dev,
 286                        ADIS16260_SMPL_PRD,
 287                        t);
 288
 289        mutex_unlock(&indio_dev->mlock);
 290
 291        return ret ? ret : len;
 292}
 293
 294static ssize_t adis16260_read_gyro_scale(struct device *dev,
 295                struct device_attribute *attr,
 296                char *buf)
 297{
 298        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 299        struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
 300        ssize_t ret = 0;
 301
 302        if (st->negate)
 303                ret = sprintf(buf, "-");
 304        /* Take the iio_dev status lock */
 305        ret += sprintf(buf + ret, "%s\n", "0.00127862821");
 306
 307        return ret;
 308}
 309
 310static int adis16260_reset(struct device *dev)
 311{
 312        int ret;
 313        ret = adis16260_spi_write_reg_8(dev,
 314                        ADIS16260_GLOB_CMD,
 315                        ADIS16260_GLOB_CMD_SW_RESET);
 316        if (ret)
 317                dev_err(dev, "problem resetting device");
 318
 319        return ret;
 320}
 321
 322static ssize_t adis16260_write_reset(struct device *dev,
 323                struct device_attribute *attr,
 324                const char *buf, size_t len)
 325{
 326        if (len < 1)
 327                return -EINVAL;
 328        switch (buf[0]) {
 329        case '1':
 330        case 'y':
 331        case 'Y':
 332                return adis16260_reset(dev);
 333        }
 334        return -EINVAL;
 335}
 336
 337int adis16260_set_irq(struct device *dev, bool enable)
 338{
 339        int ret;
 340        u16 msc;
 341        ret = adis16260_spi_read_reg_16(dev, ADIS16260_MSC_CTRL, &msc);
 342        if (ret)
 343                goto error_ret;
 344
 345        msc |= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH;
 346        if (enable)
 347                msc |= ADIS16260_MSC_CTRL_DATA_RDY_EN;
 348        else
 349                msc &= ~ADIS16260_MSC_CTRL_DATA_RDY_EN;
 350
 351        ret = adis16260_spi_write_reg_16(dev, ADIS16260_MSC_CTRL, msc);
 352        if (ret)
 353                goto error_ret;
 354
 355error_ret:
 356        return ret;
 357}
 358
 359/* Power down the device */
 360static int adis16260_stop_device(struct device *dev)
 361{
 362        int ret;
 363        u16 val = ADIS16260_SLP_CNT_POWER_OFF;
 364
 365        ret = adis16260_spi_write_reg_16(dev, ADIS16260_SLP_CNT, val);
 366        if (ret)
 367                dev_err(dev, "problem with turning device off: SLP_CNT");
 368
 369        return ret;
 370}
 371
 372static int adis16260_self_test(struct device *dev)
 373{
 374        int ret;
 375        ret = adis16260_spi_write_reg_16(dev,
 376                        ADIS16260_MSC_CTRL,
 377                        ADIS16260_MSC_CTRL_MEM_TEST);
 378        if (ret) {
 379                dev_err(dev, "problem starting self test");
 380                goto err_ret;
 381        }
 382
 383        adis16260_check_status(dev);
 384
 385err_ret:
 386        return ret;
 387}
 388
 389static int adis16260_check_status(struct device *dev)
 390{
 391        u16 status;
 392        int ret;
 393
 394        ret = adis16260_spi_read_reg_16(dev, ADIS16260_DIAG_STAT, &status);
 395
 396        if (ret < 0) {
 397                dev_err(dev, "Reading status failed\n");
 398                goto error_ret;
 399        }
 400        ret = status & 0x7F;
 401        if (status & ADIS16260_DIAG_STAT_FLASH_CHK)
 402                dev_err(dev, "Flash checksum error\n");
 403        if (status & ADIS16260_DIAG_STAT_SELF_TEST)
 404                dev_err(dev, "Self test error\n");
 405        if (status & ADIS16260_DIAG_STAT_OVERFLOW)
 406                dev_err(dev, "Sensor overrange\n");
 407        if (status & ADIS16260_DIAG_STAT_SPI_FAIL)
 408                dev_err(dev, "SPI failure\n");
 409        if (status & ADIS16260_DIAG_STAT_FLASH_UPT)
 410                dev_err(dev, "Flash update failed\n");
 411        if (status & ADIS16260_DIAG_STAT_POWER_HIGH)
 412                dev_err(dev, "Power supply above 5.25V\n");
 413        if (status & ADIS16260_DIAG_STAT_POWER_LOW)
 414                dev_err(dev, "Power supply below 4.75V\n");
 415
 416error_ret:
 417        return ret;
 418}
 419
 420static int adis16260_initial_setup(struct adis16260_state *st)
 421{
 422        int ret;
 423        struct device *dev = &st->indio_dev->dev;
 424
 425        /* Disable IRQ */
 426        ret = adis16260_set_irq(dev, false);
 427        if (ret) {
 428                dev_err(dev, "disable irq failed");
 429                goto err_ret;
 430        }
 431
 432        /* Do self test */
 433        ret = adis16260_self_test(dev);
 434        if (ret) {
 435                dev_err(dev, "self test failure");
 436                goto err_ret;
 437        }
 438
 439        /* Read status register to check the result */
 440        ret = adis16260_check_status(dev);
 441        if (ret) {
 442                adis16260_reset(dev);
 443                dev_err(dev, "device not playing ball -> reset");
 444                msleep(ADIS16260_STARTUP_DELAY);
 445                ret = adis16260_check_status(dev);
 446                if (ret) {
 447                        dev_err(dev, "giving up");
 448                        goto err_ret;
 449                }
 450        }
 451
 452        printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
 453                        st->us->chip_select, st->us->irq);
 454
 455err_ret:
 456        return ret;
 457}
 458
 459static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply,
 460                                adis16260_read_12bit_unsigned,
 461                                ADIS16260_SUPPLY_OUT);
 462static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "0.0018315");
 463
 464static IIO_DEV_ATTR_TEMP_RAW(adis16260_read_12bit_unsigned);
 465static IIO_CONST_ATTR_TEMP_OFFSET("25");
 466static IIO_CONST_ATTR_TEMP_SCALE("0.1453");
 467
 468static IIO_DEV_ATTR_IN_RAW(1, adis16260_read_12bit_unsigned,
 469                ADIS16260_AUX_ADC);
 470static IIO_CONST_ATTR(in1_scale, "0.0006105");
 471
 472static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
 473                adis16260_read_frequency,
 474                adis16260_write_frequency);
 475
 476static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16260_write_reset, 0);
 477
 478static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("256 2048");
 479
 480static IIO_CONST_ATTR_NAME("adis16260");
 481
 482static struct attribute *adis16260_event_attributes[] = {
 483        NULL
 484};
 485
 486static struct attribute_group adis16260_event_attribute_group = {
 487        .attrs = adis16260_event_attributes,
 488};
 489
 490#define ADIS16260_GYRO_ATTR_SET(axis)                                   \
 491        IIO_DEV_ATTR_GYRO##axis(adis16260_read_14bit_signed,            \
 492                                ADIS16260_GYRO_OUT);                    \
 493        static IIO_DEV_ATTR_GYRO##axis##_SCALE(S_IRUGO,                 \
 494                                        adis16260_read_gyro_scale,      \
 495                                        NULL,                           \
 496                                        0);                             \
 497        static IIO_DEV_ATTR_GYRO##axis##_CALIBSCALE(S_IRUGO | S_IWUSR,  \
 498                                        adis16260_read_12bit_unsigned,  \
 499                                        adis16260_write_16bit,          \
 500                                        ADIS16260_GYRO_SCALE);          \
 501        static IIO_DEV_ATTR_GYRO##axis##_CALIBBIAS(S_IWUSR | S_IRUGO,   \
 502                                        adis16260_read_12bit_signed,    \
 503                                        adis16260_write_16bit,          \
 504                                        ADIS16260_GYRO_OFF);            \
 505        static IIO_DEV_ATTR_ANGL##axis(adis16260_read_14bit_signed,     \
 506                                       ADIS16260_ANGL_OUT);
 507
 508static ADIS16260_GYRO_ATTR_SET();
 509static ADIS16260_GYRO_ATTR_SET(_X);
 510static ADIS16260_GYRO_ATTR_SET(_Y);
 511static ADIS16260_GYRO_ATTR_SET(_Z);
 512
 513#define ADIS16260_ATTR_GROUP(axis)                                      \
 514        struct attribute *adis16260_attributes##axis[] = {              \
 515                &iio_dev_attr_in0_supply_raw.dev_attr.attr,             \
 516                &iio_const_attr_in0_supply_scale.dev_attr.attr,         \
 517                &iio_dev_attr_gyro##axis##_raw.dev_attr.attr,           \
 518                &iio_dev_attr_gyro##axis##_scale.dev_attr.attr,         \
 519                &iio_dev_attr_gyro##axis##_calibscale.dev_attr.attr,    \
 520                &iio_dev_attr_gyro##axis##_calibbias.dev_attr.attr,     \
 521                &iio_dev_attr_angl##axis##_raw.dev_attr.attr,           \
 522                &iio_dev_attr_temp_raw.dev_attr.attr,                   \
 523                &iio_const_attr_temp_offset.dev_attr.attr,              \
 524                &iio_const_attr_temp_scale.dev_attr.attr,               \
 525                &iio_dev_attr_in1_raw.dev_attr.attr,                    \
 526                &iio_const_attr_in1_scale.dev_attr.attr,                \
 527                &iio_dev_attr_sampling_frequency.dev_attr.attr,         \
 528                &iio_const_attr_sampling_frequency_available.dev_attr.attr, \
 529                &iio_dev_attr_reset.dev_attr.attr,                      \
 530                &iio_const_attr_name.dev_attr.attr,                     \
 531                NULL                                                    \
 532        };                                                              \
 533        static const struct attribute_group adis16260_attribute_group##axis \
 534        = {                                                             \
 535                .attrs = adis16260_attributes##axis,                    \
 536        };
 537
 538static ADIS16260_ATTR_GROUP();
 539static ADIS16260_ATTR_GROUP(_x);
 540static ADIS16260_ATTR_GROUP(_y);
 541static ADIS16260_ATTR_GROUP(_z);
 542
 543static int __devinit adis16260_probe(struct spi_device *spi)
 544{
 545        int ret, regdone = 0;
 546        struct adis16260_platform_data *pd = spi->dev.platform_data;
 547        struct adis16260_state *st = kzalloc(sizeof *st, GFP_KERNEL);
 548        if (!st) {
 549                ret =  -ENOMEM;
 550                goto error_ret;
 551        }
 552        if (pd)
 553                st->negate = pd->negate;
 554        /* this is only used for removal purposes */
 555        spi_set_drvdata(spi, st);
 556
 557        /* Allocate the comms buffers */
 558        st->rx = kzalloc(sizeof(*st->rx)*ADIS16260_MAX_RX, GFP_KERNEL);
 559        if (st->rx == NULL) {
 560                ret = -ENOMEM;
 561                goto error_free_st;
 562        }
 563        st->tx = kzalloc(sizeof(*st->tx)*ADIS16260_MAX_TX, GFP_KERNEL);
 564        if (st->tx == NULL) {
 565                ret = -ENOMEM;
 566                goto error_free_rx;
 567        }
 568        st->us = spi;
 569        mutex_init(&st->buf_lock);
 570        /* setup the industrialio driver allocated elements */
 571        st->indio_dev = iio_allocate_device();
 572        if (st->indio_dev == NULL) {
 573                ret = -ENOMEM;
 574                goto error_free_tx;
 575        }
 576
 577        st->indio_dev->dev.parent = &spi->dev;
 578        st->indio_dev->num_interrupt_lines = 1;
 579        st->indio_dev->event_attrs = &adis16260_event_attribute_group;
 580        if (pd && pd->direction)
 581                switch (pd->direction) {
 582                case 'x':
 583                        st->indio_dev->attrs = &adis16260_attribute_group_x;
 584                        break;
 585                case 'y':
 586                        st->indio_dev->attrs = &adis16260_attribute_group_y;
 587                        break;
 588                case 'z':
 589                        st->indio_dev->attrs = &adis16260_attribute_group_z;
 590                        break;
 591                default:
 592                        st->indio_dev->attrs = &adis16260_attribute_group;
 593                        break;
 594                }
 595        else
 596                st->indio_dev->attrs = &adis16260_attribute_group;
 597
 598        st->indio_dev->dev_data = (void *)(st);
 599        st->indio_dev->driver_module = THIS_MODULE;
 600        st->indio_dev->modes = INDIO_DIRECT_MODE;
 601
 602        ret = adis16260_configure_ring(st->indio_dev);
 603        if (ret)
 604                goto error_free_dev;
 605
 606        ret = iio_device_register(st->indio_dev);
 607        if (ret)
 608                goto error_unreg_ring_funcs;
 609        regdone = 1;
 610        ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
 611        if (ret) {
 612                printk(KERN_ERR "failed to initialize the ring\n");
 613                goto error_unreg_ring_funcs;
 614        }
 615
 616        if (spi->irq) {
 617                ret = iio_register_interrupt_line(spi->irq,
 618                                st->indio_dev,
 619                                0,
 620                                IRQF_TRIGGER_RISING,
 621                                "adis16260");
 622                if (ret)
 623                        goto error_uninitialize_ring;
 624
 625                ret = adis16260_probe_trigger(st->indio_dev);
 626                if (ret)
 627                        goto error_unregister_line;
 628        }
 629
 630        /* Get the device into a sane initial state */
 631        ret = adis16260_initial_setup(st);
 632        if (ret)
 633                goto error_remove_trigger;
 634        return 0;
 635
 636error_remove_trigger:
 637        adis16260_remove_trigger(st->indio_dev);
 638error_unregister_line:
 639        if (spi->irq)
 640                iio_unregister_interrupt_line(st->indio_dev, 0);
 641error_uninitialize_ring:
 642        iio_ring_buffer_unregister(st->indio_dev->ring);
 643error_unreg_ring_funcs:
 644        adis16260_unconfigure_ring(st->indio_dev);
 645error_free_dev:
 646        if (regdone)
 647                iio_device_unregister(st->indio_dev);
 648        else
 649                iio_free_device(st->indio_dev);
 650error_free_tx:
 651        kfree(st->tx);
 652error_free_rx:
 653        kfree(st->rx);
 654error_free_st:
 655        kfree(st);
 656error_ret:
 657        return ret;
 658}
 659
 660static int adis16260_remove(struct spi_device *spi)
 661{
 662        int ret;
 663        struct adis16260_state *st = spi_get_drvdata(spi);
 664        struct iio_dev *indio_dev = st->indio_dev;
 665
 666        ret = adis16260_stop_device(&(indio_dev->dev));
 667        if (ret)
 668                goto err_ret;
 669
 670        flush_scheduled_work();
 671
 672        adis16260_remove_trigger(indio_dev);
 673        if (spi->irq)
 674                iio_unregister_interrupt_line(indio_dev, 0);
 675
 676        iio_ring_buffer_unregister(st->indio_dev->ring);
 677        iio_device_unregister(indio_dev);
 678        adis16260_unconfigure_ring(indio_dev);
 679        kfree(st->tx);
 680        kfree(st->rx);
 681        kfree(st);
 682
 683err_ret:
 684        return ret;
 685}
 686
 687/*
 688 * These parts do not need to be differentiated until someone adds
 689 * support for the on chip filtering.
 690 */
 691static const struct spi_device_id adis16260_id[] = {
 692        {"adis16260", 0},
 693        {"adis16265", 0},
 694        {"adis16250", 0},
 695        {"adis16255", 0},
 696        {}
 697};
 698
 699static struct spi_driver adis16260_driver = {
 700        .driver = {
 701                .name = "adis16260",
 702                .owner = THIS_MODULE,
 703        },
 704        .probe = adis16260_probe,
 705        .remove = __devexit_p(adis16260_remove),
 706        .id_table = adis16260_id,
 707};
 708
 709static __init int adis16260_init(void)
 710{
 711        return spi_register_driver(&adis16260_driver);
 712}
 713module_init(adis16260_init);
 714
 715static __exit void adis16260_exit(void)
 716{
 717        spi_unregister_driver(&adis16260_driver);
 718}
 719module_exit(adis16260_exit);
 720
 721MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
 722MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
 723MODULE_LICENSE("GPL v2");
 724