linux/drivers/staging/iio/accel/sca3000_core.c
<<
>>
Prefs
   1/*
   2 * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License version 2 as published by
   6 * the Free Software Foundation.
   7 *
   8 * Copyright (c) 2009 Jonathan Cameron <jic23@cam.ac.uk>
   9 *
  10 * See industrialio/accels/sca3000.h for comments.
  11 */
  12
  13#include <linux/interrupt.h>
  14#include <linux/gpio.h>
  15#include <linux/fs.h>
  16#include <linux/device.h>
  17#include <linux/slab.h>
  18#include <linux/kernel.h>
  19#include <linux/spi/spi.h>
  20#include <linux/sysfs.h>
  21#include "../iio.h"
  22#include "../sysfs.h"
  23#include "../ring_generic.h"
  24
  25#include "accel.h"
  26#include "sca3000.h"
  27
  28enum sca3000_variant {
  29        d01,
  30        e02,
  31        e04,
  32        e05,
  33};
  34
  35/* Note where option modes are not defined, the chip simply does not
  36 * support any.
  37 * Other chips in the sca3000 series use i2c and are not included here.
  38 *
  39 * Some of these devices are only listed in the family data sheet and
  40 * do not actually appear to be available.
  41 */
  42static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
  43        {
  44                .name = "sca3000-d01",
  45                .scale = " 0.0073575",
  46                .temp_output = true,
  47                .measurement_mode_freq = 250,
  48                .option_mode_1 = SCA3000_OP_MODE_BYPASS,
  49                .option_mode_1_freq = 250,
  50        }, {
  51                .name = "sca3000-e02",
  52                .scale = "0.00981",
  53                .measurement_mode_freq = 125,
  54                .option_mode_1 = SCA3000_OP_MODE_NARROW,
  55                .option_mode_1_freq = 63,
  56        }, {
  57                .name = "sca3000-e04",
  58                .scale = "0.01962",
  59                .measurement_mode_freq = 100,
  60                .option_mode_1 = SCA3000_OP_MODE_NARROW,
  61                .option_mode_1_freq = 50,
  62                .option_mode_2 = SCA3000_OP_MODE_WIDE,
  63                .option_mode_2_freq = 400,
  64        }, {
  65                .name = "sca3000-e05",
  66                .scale = "0.0613125",
  67                .measurement_mode_freq = 200,
  68                .option_mode_1 = SCA3000_OP_MODE_NARROW,
  69                .option_mode_1_freq = 50,
  70                .option_mode_2 = SCA3000_OP_MODE_WIDE,
  71                .option_mode_2_freq = 400,
  72        },
  73};
  74
  75
  76int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
  77{
  78        struct spi_transfer xfer = {
  79                .bits_per_word = 8,
  80                .len = 2,
  81                .cs_change = 1,
  82                .tx_buf = st->tx,
  83        };
  84        struct spi_message msg;
  85
  86        st->tx[0] = SCA3000_WRITE_REG(address);
  87        st->tx[1] = val;
  88        spi_message_init(&msg);
  89        spi_message_add_tail(&xfer, &msg);
  90
  91        return spi_sync(st->us, &msg);
  92}
  93
  94int sca3000_read_data(struct sca3000_state *st,
  95                      uint8_t reg_address_high,
  96                      u8 **rx_p,
  97                      int len)
  98{
  99        int ret;
 100        struct spi_message msg;
 101        struct spi_transfer xfer = {
 102                .bits_per_word = 8,
 103                .len = len + 1,
 104                .cs_change = 1,
 105                .tx_buf = st->tx,
 106        };
 107
 108        *rx_p = kmalloc(len + 1, GFP_KERNEL);
 109        if (*rx_p == NULL) {
 110                ret = -ENOMEM;
 111                goto error_ret;
 112        }
 113        xfer.rx_buf = *rx_p;
 114        st->tx[0] = SCA3000_READ_REG(reg_address_high);
 115        spi_message_init(&msg);
 116        spi_message_add_tail(&xfer, &msg);
 117
 118        ret = spi_sync(st->us, &msg);
 119
 120        if (ret) {
 121                dev_err(get_device(&st->us->dev), "problem reading register");
 122                goto error_free_rx;
 123        }
 124
 125        return 0;
 126error_free_rx:
 127        kfree(*rx_p);
 128error_ret:
 129        return ret;
 130
 131}
 132/**
 133 * sca3000_reg_lock_on() test if the ctrl register lock is on
 134 *
 135 * Lock must be held.
 136 **/
 137static int sca3000_reg_lock_on(struct sca3000_state *st)
 138{
 139        u8 *rx;
 140        int ret;
 141
 142        ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1);
 143
 144        if (ret < 0)
 145                return ret;
 146        ret = !(rx[1] & SCA3000_LOCKED);
 147        kfree(rx);
 148
 149        return ret;
 150}
 151
 152/**
 153 * __sca3000_unlock_reg_lock() unlock the control registers
 154 *
 155 * Note the device does not appear to support doing this in a single transfer.
 156 * This should only ever be used as part of ctrl reg read.
 157 * Lock must be held before calling this
 158 **/
 159static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
 160{
 161        struct spi_message msg;
 162        struct spi_transfer xfer[3] = {
 163                {
 164                        .bits_per_word = 8,
 165                        .len = 2,
 166                        .cs_change = 1,
 167                        .tx_buf = st->tx,
 168                }, {
 169                        .bits_per_word = 8,
 170                        .len = 2,
 171                        .cs_change = 1,
 172                        .tx_buf = st->tx + 2,
 173                }, {
 174                        .bits_per_word = 8,
 175                        .len = 2,
 176                        .cs_change = 1,
 177                        .tx_buf = st->tx + 4,
 178                },
 179        };
 180        st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
 181        st->tx[1] = 0x00;
 182        st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
 183        st->tx[3] = 0x50;
 184        st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
 185        st->tx[5] = 0xA0;
 186        spi_message_init(&msg);
 187        spi_message_add_tail(&xfer[0], &msg);
 188        spi_message_add_tail(&xfer[1], &msg);
 189        spi_message_add_tail(&xfer[2], &msg);
 190
 191        return spi_sync(st->us, &msg);
 192}
 193
 194/**
 195 * sca3000_write_ctrl_reg() write to a lock protect ctrl register
 196 * @sel: selects which registers we wish to write to
 197 * @val: the value to be written
 198 *
 199 * Certain control registers are protected against overwriting by the lock
 200 * register and use a shared write address. This function allows writing of
 201 * these registers.
 202 * Lock must be held.
 203 **/
 204static int sca3000_write_ctrl_reg(struct sca3000_state *st,
 205                                  uint8_t sel,
 206                                  uint8_t val)
 207{
 208
 209        int ret;
 210
 211        ret = sca3000_reg_lock_on(st);
 212        if (ret < 0)
 213                goto error_ret;
 214        if (ret) {
 215                ret = __sca3000_unlock_reg_lock(st);
 216                if (ret)
 217                        goto error_ret;
 218        }
 219
 220        /* Set the control select register */
 221        ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
 222        if (ret)
 223                goto error_ret;
 224
 225        /* Write the actual value into the register */
 226        ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
 227
 228error_ret:
 229        return ret;
 230}
 231
 232/* Crucial that lock is called before calling this */
 233/**
 234 * sca3000_read_ctrl_reg() read from lock protected control register.
 235 *
 236 * Lock must be held.
 237 **/
 238static int sca3000_read_ctrl_reg(struct sca3000_state *st,
 239                                 u8 ctrl_reg,
 240                                 u8 **rx_p)
 241{
 242        int ret;
 243
 244        ret = sca3000_reg_lock_on(st);
 245        if (ret < 0)
 246                goto error_ret;
 247        if (ret) {
 248                ret = __sca3000_unlock_reg_lock(st);
 249                if (ret)
 250                        goto error_ret;
 251        }
 252        /* Set the control select register */
 253        ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
 254        if (ret)
 255                goto error_ret;
 256        ret = sca3000_read_data(st, SCA3000_REG_ADDR_CTRL_DATA, rx_p, 1);
 257
 258error_ret:
 259        return ret;
 260}
 261
 262#ifdef SCA3000_DEBUG
 263/**
 264 * sca3000_check_status() check the status register
 265 *
 266 * Only used for debugging purposes
 267 **/
 268static int sca3000_check_status(struct device *dev)
 269{
 270        u8 *rx;
 271        int ret;
 272        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 273        struct sca3000_state *st = indio_dev->dev_data;
 274
 275        mutex_lock(&st->lock);
 276        ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1);
 277        if (ret < 0)
 278                goto error_ret;
 279        if (rx[1] & SCA3000_EEPROM_CS_ERROR)
 280                dev_err(dev, "eeprom error\n");
 281        if (rx[1] & SCA3000_SPI_FRAME_ERROR)
 282                dev_err(dev, "Previous SPI Frame was corrupt\n");
 283        kfree(rx);
 284
 285error_ret:
 286        mutex_unlock(&st->lock);
 287        return ret;
 288}
 289#endif /* SCA3000_DEBUG */
 290
 291/**
 292 * sca3000_read_13bit_signed() sysfs interface to read 13 bit signed registers
 293 *
 294 * These are described as signed 12 bit on the data sheet, which appears
 295 * to be a conventional 2's complement 13 bit.
 296 **/
 297static ssize_t sca3000_read_13bit_signed(struct device *dev,
 298                                         struct device_attribute *attr,
 299                                         char *buf)
 300{
 301        int len = 0, ret;
 302        int val;
 303        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 304        u8 *rx;
 305        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 306        struct sca3000_state *st = indio_dev->dev_data;
 307
 308        mutex_lock(&st->lock);
 309        ret = sca3000_read_data(st, this_attr->address, &rx, 2);
 310        if (ret < 0)
 311                goto error_ret;
 312        val = sca3000_13bit_convert(rx[1], rx[2]);
 313        len += sprintf(buf + len, "%d\n", val);
 314        kfree(rx);
 315error_ret:
 316        mutex_unlock(&st->lock);
 317
 318        return ret ? ret : len;
 319}
 320
 321static ssize_t sca3000_show_scale(struct device *dev,
 322                                  struct device_attribute *attr,
 323                                  char *buf)
 324{
 325        struct iio_dev *dev_info = dev_get_drvdata(dev);
 326        struct sca3000_state *st = dev_info->dev_data;
 327        return sprintf(buf, "%s\n", st->info->scale);
 328}
 329
 330static ssize_t sca3000_show_name(struct device *dev,
 331                                 struct device_attribute *attr,
 332                                 char *buf)
 333{
 334        struct iio_dev *dev_info = dev_get_drvdata(dev);
 335        struct sca3000_state *st = dev_info->dev_data;
 336        return sprintf(buf, "%s\n", st->info->name);
 337}
 338/**
 339 * sca3000_show_reg() - sysfs interface to read the chip revision number
 340 **/
 341static ssize_t sca3000_show_rev(struct device *dev,
 342                                struct device_attribute *attr,
 343                                char *buf)
 344{
 345        int len = 0, ret;
 346        struct iio_dev *dev_info = dev_get_drvdata(dev);
 347        struct sca3000_state *st = dev_info->dev_data;
 348
 349        u8 *rx;
 350
 351        mutex_lock(&st->lock);
 352        ret = sca3000_read_data(st, SCA3000_REG_ADDR_REVID, &rx, 1);
 353        if (ret < 0)
 354                goto error_ret;
 355        len += sprintf(buf + len,
 356                       "major=%d, minor=%d\n",
 357                       rx[1] & SCA3000_REVID_MAJOR_MASK,
 358                       rx[1] & SCA3000_REVID_MINOR_MASK);
 359        kfree(rx);
 360
 361error_ret:
 362        mutex_unlock(&st->lock);
 363
 364        return ret ? ret : len;
 365}
 366
 367/**
 368 * sca3000_show_available_measurement_modes() display available modes
 369 *
 370 * This is all read from chip specific data in the driver. Not all
 371 * of the sca3000 series support modes other than normal.
 372 **/
 373static ssize_t
 374sca3000_show_available_measurement_modes(struct device *dev,
 375                                         struct device_attribute *attr,
 376                                         char *buf)
 377{
 378        struct iio_dev *dev_info = dev_get_drvdata(dev);
 379        struct sca3000_state *st = dev_info->dev_data;
 380        int len = 0;
 381
 382        len += sprintf(buf + len, "0 - normal mode");
 383        switch (st->info->option_mode_1) {
 384        case SCA3000_OP_MODE_NARROW:
 385                len += sprintf(buf + len, ", 1 - narrow mode");
 386                break;
 387        case SCA3000_OP_MODE_BYPASS:
 388                len += sprintf(buf + len, ", 1 - bypass mode");
 389                break;
 390        }
 391        switch (st->info->option_mode_2) {
 392        case SCA3000_OP_MODE_WIDE:
 393                len += sprintf(buf + len, ", 2 - wide mode");
 394                break;
 395        }
 396        /* always supported */
 397        len += sprintf(buf + len, " 3 - motion detection\n");
 398
 399        return len;
 400}
 401
 402/**
 403 * sca3000_show_measurmenet_mode() sysfs read of current mode
 404 **/
 405static ssize_t
 406sca3000_show_measurement_mode(struct device *dev,
 407                              struct device_attribute *attr,
 408                              char *buf)
 409{
 410        struct iio_dev *dev_info = dev_get_drvdata(dev);
 411        struct sca3000_state *st = dev_info->dev_data;
 412        int len = 0, ret;
 413        u8 *rx;
 414
 415        mutex_lock(&st->lock);
 416        ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
 417        if (ret)
 418                goto error_ret;
 419        /* mask bottom 2 bits - only ones that are relevant */
 420        rx[1] &= 0x03;
 421        switch (rx[1]) {
 422        case SCA3000_MEAS_MODE_NORMAL:
 423                len += sprintf(buf + len, "0 - normal mode\n");
 424                break;
 425        case SCA3000_MEAS_MODE_MOT_DET:
 426                len += sprintf(buf + len, "3 - motion detection\n");
 427                break;
 428        case SCA3000_MEAS_MODE_OP_1:
 429                switch (st->info->option_mode_1) {
 430                case SCA3000_OP_MODE_NARROW:
 431                        len += sprintf(buf + len, "1 - narrow mode\n");
 432                        break;
 433                case SCA3000_OP_MODE_BYPASS:
 434                        len += sprintf(buf + len, "1 - bypass mode\n");
 435                        break;
 436                }
 437                break;
 438        case SCA3000_MEAS_MODE_OP_2:
 439                switch (st->info->option_mode_2) {
 440                case SCA3000_OP_MODE_WIDE:
 441                        len += sprintf(buf + len, "2 - wide mode\n");
 442                        break;
 443                }
 444                break;
 445        }
 446
 447error_ret:
 448        mutex_unlock(&st->lock);
 449
 450        return ret ? ret : len;
 451}
 452
 453/**
 454 * sca3000_store_measurement_mode() set the current mode
 455 **/
 456static ssize_t
 457sca3000_store_measurement_mode(struct device *dev,
 458                               struct device_attribute *attr,
 459                               const char *buf,
 460                               size_t len)
 461{
 462        struct iio_dev *dev_info = dev_get_drvdata(dev);
 463        struct sca3000_state *st = dev_info->dev_data;
 464        int ret;
 465        u8 *rx;
 466        int mask = 0x03;
 467        long val;
 468
 469        mutex_lock(&st->lock);
 470        ret = strict_strtol(buf, 10, &val);
 471        if (ret)
 472                goto error_ret;
 473        ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
 474        if (ret)
 475                goto error_ret;
 476        rx[1] &= ~mask;
 477        rx[1] |= (val & mask);
 478        ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, rx[1]);
 479        if (ret)
 480                goto error_free_rx;
 481        mutex_unlock(&st->lock);
 482
 483        return len;
 484
 485error_free_rx:
 486        kfree(rx);
 487error_ret:
 488        mutex_unlock(&st->lock);
 489
 490        return ret;
 491}
 492
 493
 494/* Not even vaguely standard attributes so defined here rather than
 495 * in the relevant IIO core headers
 496 */
 497static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO,
 498                       sca3000_show_available_measurement_modes,
 499                       NULL, 0);
 500
 501static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
 502                       sca3000_show_measurement_mode,
 503                       sca3000_store_measurement_mode,
 504                       0);
 505
 506/* More standard attributes */
 507
 508static IIO_DEV_ATTR_NAME(sca3000_show_name);
 509static IIO_DEV_ATTR_REV(sca3000_show_rev);
 510static IIO_DEVICE_ATTR(accel_scale, S_IRUGO, sca3000_show_scale,
 511                       NULL, 0);
 512
 513static IIO_DEV_ATTR_ACCEL_X(sca3000_read_13bit_signed,
 514                            SCA3000_REG_ADDR_X_MSB);
 515static IIO_DEV_ATTR_ACCEL_Y(sca3000_read_13bit_signed,
 516                            SCA3000_REG_ADDR_Y_MSB);
 517static IIO_DEV_ATTR_ACCEL_Z(sca3000_read_13bit_signed,
 518                            SCA3000_REG_ADDR_Z_MSB);
 519
 520
 521/**
 522 * sca3000_read_av_freq() sysfs function to get available frequencies
 523 *
 524 * The later modes are only relevant to the ring buffer - and depend on current
 525 * mode. Note that data sheet gives rather wide tolerances for these so integer
 526 * division will give good enough answer and not all chips have them specified
 527 * at all.
 528 **/
 529static ssize_t sca3000_read_av_freq(struct device *dev,
 530                             struct device_attribute *attr,
 531                             char *buf)
 532{
 533        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 534        struct sca3000_state *st = indio_dev->dev_data;
 535        int len = 0, ret;
 536        u8 *rx;
 537        mutex_lock(&st->lock);
 538        ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
 539        mutex_unlock(&st->lock);
 540        if (ret)
 541                goto error_ret;
 542        rx[1] &= 0x03;
 543        switch (rx[1]) {
 544        case SCA3000_MEAS_MODE_NORMAL:
 545                len += sprintf(buf + len, "%d %d %d\n",
 546                               st->info->measurement_mode_freq,
 547                               st->info->measurement_mode_freq/2,
 548                               st->info->measurement_mode_freq/4);
 549                break;
 550        case SCA3000_MEAS_MODE_OP_1:
 551                len += sprintf(buf + len, "%d %d %d\n",
 552                               st->info->option_mode_1_freq,
 553                               st->info->option_mode_1_freq/2,
 554                               st->info->option_mode_1_freq/4);
 555                break;
 556        case SCA3000_MEAS_MODE_OP_2:
 557                len += sprintf(buf + len, "%d %d %d\n",
 558                               st->info->option_mode_2_freq,
 559                               st->info->option_mode_2_freq/2,
 560                               st->info->option_mode_2_freq/4);
 561                break;
 562        }
 563        kfree(rx);
 564        return len;
 565error_ret:
 566        return ret;
 567}
 568/**
 569 * __sca3000_get_base_frequency() obtain mode specific base frequency
 570 *
 571 * lock must be held
 572 **/
 573static inline int __sca3000_get_base_freq(struct sca3000_state *st,
 574                                          const struct sca3000_chip_info *info,
 575                                          int *base_freq)
 576{
 577        int ret;
 578        u8 *rx;
 579
 580        ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
 581        if (ret)
 582                goto error_ret;
 583        switch (0x03 & rx[1]) {
 584        case SCA3000_MEAS_MODE_NORMAL:
 585                *base_freq = info->measurement_mode_freq;
 586                break;
 587        case SCA3000_MEAS_MODE_OP_1:
 588                *base_freq = info->option_mode_1_freq;
 589                break;
 590        case SCA3000_MEAS_MODE_OP_2:
 591                *base_freq = info->option_mode_2_freq;
 592                break;
 593        }
 594        kfree(rx);
 595error_ret:
 596        return ret;
 597}
 598
 599/**
 600 * sca3000_read_frequency() sysfs interface to get the current frequency
 601 **/
 602static ssize_t sca3000_read_frequency(struct device *dev,
 603                               struct device_attribute *attr,
 604                               char *buf)
 605{
 606        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 607        struct sca3000_state *st = indio_dev->dev_data;
 608        int ret, len = 0, base_freq = 0;
 609        u8 *rx;
 610        mutex_lock(&st->lock);
 611        ret = __sca3000_get_base_freq(st, st->info, &base_freq);
 612        if (ret)
 613                goto error_ret_mut;
 614        ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx);
 615        mutex_unlock(&st->lock);
 616        if (ret)
 617                goto error_ret;
 618        if (base_freq > 0)
 619                switch (rx[1]&0x03) {
 620                case 0x00:
 621                case 0x03:
 622                        len = sprintf(buf, "%d\n", base_freq);
 623                        break;
 624                case 0x01:
 625                        len = sprintf(buf, "%d\n", base_freq/2);
 626                        break;
 627                case 0x02:
 628                        len = sprintf(buf, "%d\n", base_freq/4);
 629                        break;
 630        }
 631        kfree(rx);
 632        return len;
 633error_ret_mut:
 634        mutex_unlock(&st->lock);
 635error_ret:
 636        return ret;
 637}
 638
 639/**
 640 * sca3000_set_frequency() sysfs interface to set the current frequency
 641 **/
 642static ssize_t sca3000_set_frequency(struct device *dev,
 643                              struct device_attribute *attr,
 644                              const char *buf,
 645                              size_t len)
 646{
 647        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 648        struct sca3000_state *st = indio_dev->dev_data;
 649        int ret, base_freq = 0;
 650        u8 *rx;
 651        long val;
 652
 653        ret = strict_strtol(buf, 10, &val);
 654        if (ret)
 655                return ret;
 656
 657        mutex_lock(&st->lock);
 658        /* What mode are we in? */
 659        ret = __sca3000_get_base_freq(st, st->info, &base_freq);
 660        if (ret)
 661                goto error_free_lock;
 662
 663        ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx);
 664        if (ret)
 665                goto error_free_lock;
 666        /* clear the bits */
 667        rx[1] &= ~0x03;
 668
 669        if (val == base_freq/2) {
 670                rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_2;
 671        } else if (val == base_freq/4) {
 672                rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_4;
 673        } else if (val != base_freq) {
 674                ret = -EINVAL;
 675                goto error_free_lock;
 676        }
 677        ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, rx[1]);
 678error_free_lock:
 679        mutex_unlock(&st->lock);
 680
 681        return ret ? ret : len;
 682}
 683
 684/* Should only really be registered if ring buffer support is compiled in.
 685 * Does no harm however and doing it right would add a fair bit of complexity
 686 */
 687static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
 688
 689static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
 690                              sca3000_read_frequency,
 691                              sca3000_set_frequency);
 692
 693
 694/**
 695 * sca3000_read_temp() sysfs interface to get the temperature when available
 696 *
 697* The alignment of data in here is downright odd. See data sheet.
 698* Converting this into a meaningful value is left to inline functions in
 699* userspace part of header.
 700**/
 701static ssize_t sca3000_read_temp(struct device *dev,
 702                                 struct device_attribute *attr,
 703                                 char *buf)
 704{
 705        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 706        struct sca3000_state *st = indio_dev->dev_data;
 707        int len = 0, ret;
 708        int val;
 709        u8 *rx;
 710        ret = sca3000_read_data(st, SCA3000_REG_ADDR_TEMP_MSB, &rx, 2);
 711        if (ret < 0)
 712                goto error_ret;
 713        val = ((rx[1]&0x3F) << 3) | ((rx[2] & 0xE0) >> 5);
 714        len += sprintf(buf + len, "%d\n", val);
 715        kfree(rx);
 716
 717        return len;
 718
 719error_ret:
 720        return ret;
 721}
 722static IIO_DEV_ATTR_TEMP_RAW(sca3000_read_temp);
 723
 724static IIO_CONST_ATTR_TEMP_SCALE("0.555556");
 725static IIO_CONST_ATTR_TEMP_OFFSET("-214.6");
 726
 727/**
 728 * sca3000_show_thresh() sysfs query of a threshold
 729 **/
 730static ssize_t sca3000_show_thresh(struct device *dev,
 731                                   struct device_attribute *attr,
 732                                   char *buf)
 733{
 734        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 735        struct sca3000_state *st = indio_dev->dev_data;
 736        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 737        int len = 0, ret;
 738        u8 *rx;
 739
 740        mutex_lock(&st->lock);
 741        ret = sca3000_read_ctrl_reg(st,
 742                                    this_attr->address,
 743                                    &rx);
 744        mutex_unlock(&st->lock);
 745        if (ret)
 746                return ret;
 747        len += sprintf(buf + len, "%d\n", rx[1]);
 748        kfree(rx);
 749
 750        return len;
 751}
 752
 753/**
 754 * sca3000_write_thresh() sysfs control of threshold
 755 **/
 756static ssize_t sca3000_write_thresh(struct device *dev,
 757                                    struct device_attribute *attr,
 758                                    const char *buf,
 759                                    size_t len)
 760{
 761        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 762        struct sca3000_state *st = indio_dev->dev_data;
 763        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 764        int ret;
 765        long val;
 766
 767        ret = strict_strtol(buf, 10, &val);
 768        if (ret)
 769                return ret;
 770        mutex_lock(&st->lock);
 771        ret = sca3000_write_ctrl_reg(st, this_attr->address, val);
 772        mutex_unlock(&st->lock);
 773
 774        return ret ? ret : len;
 775}
 776
 777static IIO_DEVICE_ATTR(accel_x_raw_mag_rising_value,
 778                S_IRUGO | S_IWUSR,
 779                sca3000_show_thresh,
 780                sca3000_write_thresh,
 781                SCA3000_REG_CTRL_SEL_MD_X_TH);
 782
 783static IIO_DEVICE_ATTR(accel_y_raw_mag_rising_value,
 784                S_IRUGO | S_IWUSR,
 785                sca3000_show_thresh,
 786                sca3000_write_thresh,
 787                SCA3000_REG_CTRL_SEL_MD_Y_TH);
 788
 789static IIO_DEVICE_ATTR(accel_z_raw_mag_rising_value,
 790                S_IRUGO | S_IWUSR,
 791                sca3000_show_thresh,
 792                sca3000_write_thresh,
 793                SCA3000_REG_CTRL_SEL_MD_Z_TH);
 794
 795static struct attribute *sca3000_attributes[] = {
 796        &iio_dev_attr_name.dev_attr.attr,
 797        &iio_dev_attr_revision.dev_attr.attr,
 798        &iio_dev_attr_accel_scale.dev_attr.attr,
 799        &iio_dev_attr_accel_x_raw.dev_attr.attr,
 800        &iio_dev_attr_accel_y_raw.dev_attr.attr,
 801        &iio_dev_attr_accel_z_raw.dev_attr.attr,
 802        &iio_dev_attr_measurement_mode_available.dev_attr.attr,
 803        &iio_dev_attr_measurement_mode.dev_attr.attr,
 804        &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
 805        &iio_dev_attr_sampling_frequency.dev_attr.attr,
 806        NULL,
 807};
 808
 809static struct attribute *sca3000_attributes_with_temp[] = {
 810        &iio_dev_attr_name.dev_attr.attr,
 811        &iio_dev_attr_revision.dev_attr.attr,
 812        &iio_dev_attr_accel_scale.dev_attr.attr,
 813        &iio_dev_attr_accel_x_raw.dev_attr.attr,
 814        &iio_dev_attr_accel_y_raw.dev_attr.attr,
 815        &iio_dev_attr_accel_z_raw.dev_attr.attr,
 816        &iio_dev_attr_measurement_mode_available.dev_attr.attr,
 817        &iio_dev_attr_measurement_mode.dev_attr.attr,
 818        &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
 819        &iio_dev_attr_sampling_frequency.dev_attr.attr,
 820        /* Only present if temp sensor is */
 821        &iio_dev_attr_temp_raw.dev_attr.attr,
 822        &iio_const_attr_temp_offset.dev_attr.attr,
 823        &iio_const_attr_temp_scale.dev_attr.attr,
 824        NULL,
 825};
 826
 827static const struct attribute_group sca3000_attribute_group = {
 828        .attrs = sca3000_attributes,
 829};
 830
 831static const struct attribute_group sca3000_attribute_group_with_temp = {
 832        .attrs = sca3000_attributes_with_temp,
 833};
 834
 835/* RING RELATED interrupt handler */
 836/* depending on event, push to the ring buffer event chrdev or the event one */
 837
 838/**
 839 * sca3000_interrupt_handler_bh() - handling ring and non ring events
 840 *
 841 * This function is complicated by the fact that the devices can signify ring
 842 * and non ring events via the same interrupt line and they can only
 843 * be distinguished via a read of the relevant status register.
 844 **/
 845static void sca3000_interrupt_handler_bh(struct work_struct *work_s)
 846{
 847        struct sca3000_state *st
 848                = container_of(work_s, struct sca3000_state,
 849                               interrupt_handler_ws);
 850        u8 *rx;
 851        int ret;
 852
 853        /* Could lead if badly timed to an extra read of status reg,
 854         * but ensures no interrupt is missed.
 855         */
 856        enable_irq(st->us->irq);
 857        mutex_lock(&st->lock);
 858        ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS,
 859                                &rx, 1);
 860        mutex_unlock(&st->lock);
 861        if (ret)
 862                goto done;
 863
 864        sca3000_ring_int_process(rx[1], st->indio_dev->ring);
 865
 866        if (rx[1] & SCA3000_INT_STATUS_FREE_FALL)
 867                iio_push_event(st->indio_dev, 0,
 868                               IIO_MOD_EVENT_CODE(IIO_EV_CLASS_ACCEL,
 869                                                  0,
 870                                                  IIO_EV_MOD_X_AND_Y_AND_Z,
 871                                                  IIO_EV_TYPE_MAG,
 872                                                  IIO_EV_DIR_FALLING),
 873                               st->last_timestamp);
 874
 875        if (rx[1] & SCA3000_INT_STATUS_Y_TRIGGER)
 876                iio_push_event(st->indio_dev, 0,
 877                               IIO_MOD_EVENT_CODE(IIO_EV_CLASS_ACCEL,
 878                                                  0,
 879                                                  IIO_EV_MOD_Y,
 880                                                  IIO_EV_TYPE_MAG,
 881                                                  IIO_EV_DIR_RISING),
 882                               st->last_timestamp);
 883
 884        if (rx[1] & SCA3000_INT_STATUS_X_TRIGGER)
 885                iio_push_event(st->indio_dev, 0,
 886                               IIO_MOD_EVENT_CODE(IIO_EV_CLASS_ACCEL,
 887                                                  0,
 888                                                  IIO_EV_MOD_X,
 889                                                  IIO_EV_TYPE_MAG,
 890                                                  IIO_EV_DIR_RISING),
 891                               st->last_timestamp);
 892
 893        if (rx[1] & SCA3000_INT_STATUS_Z_TRIGGER)
 894                iio_push_event(st->indio_dev, 0,
 895                               IIO_MOD_EVENT_CODE(IIO_EV_CLASS_ACCEL,
 896                                                  0,
 897                                                  IIO_EV_MOD_Z,
 898                                                  IIO_EV_TYPE_MAG,
 899                                                  IIO_EV_DIR_RISING),
 900                               st->last_timestamp);
 901
 902done:
 903        kfree(rx);
 904        return;
 905}
 906
 907/**
 908 * sca3000_handler_th() handles all interrupt events from device
 909 *
 910 * These devices deploy unified interrupt status registers meaning
 911 * all interrupts must be handled together
 912 **/
 913static int sca3000_handler_th(struct iio_dev *dev_info,
 914                              int index,
 915                              s64 timestamp,
 916                              int no_test)
 917{
 918        struct sca3000_state *st = dev_info->dev_data;
 919
 920        st->last_timestamp = timestamp;
 921        schedule_work(&st->interrupt_handler_ws);
 922
 923        return 0;
 924}
 925
 926/**
 927 * sca3000_query_mo_det() is motion detection enabled for this axis
 928 *
 929 * First queries if motion detection is enabled and then if this axis is
 930 * on.
 931 **/
 932static ssize_t sca3000_query_mo_det(struct device *dev,
 933                                    struct device_attribute *attr,
 934                                    char *buf)
 935{
 936        struct iio_dev *indio_dev = dev_get_drvdata(dev->parent);
 937        struct sca3000_state *st = indio_dev->dev_data;
 938        struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 939        int ret, len = 0;
 940        u8 *rx;
 941        u8 protect_mask = 0x03;
 942
 943        /* read current value of mode register */
 944        mutex_lock(&st->lock);
 945        ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
 946        if (ret)
 947                goto error_ret;
 948
 949        if ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
 950                len += sprintf(buf + len, "0\n");
 951        else {
 952                kfree(rx);
 953                ret = sca3000_read_ctrl_reg(st,
 954                                            SCA3000_REG_CTRL_SEL_MD_CTRL,
 955                                            &rx);
 956                if (ret)
 957                        goto error_ret;
 958                /* only supporting logical or's for now */
 959                len += sprintf(buf + len, "%d\n",
 960                               (rx[1] & this_attr->mask) ? 1 : 0);
 961        }
 962        kfree(rx);
 963error_ret:
 964        mutex_unlock(&st->lock);
 965
 966        return ret ? ret : len;
 967}
 968/**
 969 * sca3000_query_free_fall_mode() is free fall mode enabled
 970 **/
 971static ssize_t sca3000_query_free_fall_mode(struct device *dev,
 972                                            struct device_attribute *attr,
 973                                            char *buf)
 974{
 975        int ret, len;
 976        u8 *rx;
 977        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 978        struct sca3000_state *st = indio_dev->dev_data;
 979
 980        mutex_lock(&st->lock);
 981        ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
 982        mutex_unlock(&st->lock);
 983        if (ret)
 984                return ret;
 985        len = sprintf(buf, "%d\n",
 986                      !!(rx[1] & SCA3000_FREE_FALL_DETECT));
 987        kfree(rx);
 988
 989        return len;
 990}
 991/**
 992 * sca3000_query_ring_int() is the hardware ring status interrupt enabled
 993 **/
 994static ssize_t sca3000_query_ring_int(struct device *dev,
 995                                      struct device_attribute *attr,
 996                                      char *buf)
 997{
 998        struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 999        int ret, len;
1000        u8 *rx;
1001        struct iio_dev *indio_dev = dev_get_drvdata(dev->parent);
1002        struct sca3000_state *st = indio_dev->dev_data;
1003        mutex_lock(&st->lock);
1004        ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1005        mutex_unlock(&st->lock);
1006        if (ret)
1007                return ret;
1008        len = sprintf(buf, "%d\n", (rx[1] & this_attr->mask) ? 1 : 0);
1009        kfree(rx);
1010
1011        return len;
1012}
1013/**
1014 * sca3000_set_ring_int() set state of ring status interrupt
1015 **/
1016static ssize_t sca3000_set_ring_int(struct device *dev,
1017                                      struct device_attribute *attr,
1018                                      const char *buf,
1019                                      size_t len)
1020{
1021        struct iio_dev *indio_dev = dev_get_drvdata(dev->parent);
1022        struct sca3000_state *st = indio_dev->dev_data;
1023        struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1024
1025        long val;
1026        int ret;
1027        u8 *rx;
1028
1029        mutex_lock(&st->lock);
1030        ret = strict_strtol(buf, 10, &val);
1031        if (ret)
1032                goto error_ret;
1033        ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1034        if (ret)
1035                goto error_ret;
1036        if (val)
1037                ret = sca3000_write_reg(st,
1038                                        SCA3000_REG_ADDR_INT_MASK,
1039                                        rx[1] | this_attr->mask);
1040        else
1041                ret = sca3000_write_reg(st,
1042                                        SCA3000_REG_ADDR_INT_MASK,
1043                                        rx[1] & ~this_attr->mask);
1044        kfree(rx);
1045error_ret:
1046        mutex_unlock(&st->lock);
1047
1048        return ret ? ret : len;
1049}
1050
1051/**
1052 * sca3000_set_free_fall_mode() simple on off control for free fall int
1053 *
1054 * In these chips the free fall detector should send an interrupt if
1055 * the device falls more than 25cm.  This has not been tested due
1056 * to fragile wiring.
1057 **/
1058
1059static ssize_t sca3000_set_free_fall_mode(struct device *dev,
1060                                          struct device_attribute *attr,
1061                                          const char *buf,
1062                                          size_t len)
1063{
1064        struct iio_dev *indio_dev = dev_get_drvdata(dev);
1065        struct sca3000_state *st = indio_dev->dev_data;
1066        long val;
1067        int ret;
1068        u8 *rx;
1069        u8 protect_mask = SCA3000_FREE_FALL_DETECT;
1070
1071        mutex_lock(&st->lock);
1072        ret = strict_strtol(buf, 10, &val);
1073        if (ret)
1074                goto error_ret;
1075
1076        /* read current value of mode register */
1077        ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
1078        if (ret)
1079                goto error_ret;
1080
1081        /*if off and should be on*/
1082        if (val && !(rx[1] & protect_mask))
1083                ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1084                                        (rx[1] | SCA3000_FREE_FALL_DETECT));
1085        /* if on and should be off */
1086        else if (!val && (rx[1]&protect_mask))
1087                ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1088                                        (rx[1] & ~protect_mask));
1089
1090        kfree(rx);
1091error_ret:
1092        mutex_unlock(&st->lock);
1093
1094        return ret ? ret : len;
1095}
1096
1097/**
1098 * sca3000_set_mo_det() simple on off control for motion detector
1099 *
1100 * This is a per axis control, but enabling any will result in the
1101 * motion detector unit being enabled.
1102 * N.B. enabling motion detector stops normal data acquisition.
1103 * There is a complexity in knowing which mode to return to when
1104 * this mode is disabled.  Currently normal mode is assumed.
1105 **/
1106static ssize_t sca3000_set_mo_det(struct device *dev,
1107                                  struct device_attribute *attr,
1108                                  const char *buf,
1109                                  size_t len)
1110{
1111        struct iio_dev *indio_dev = dev_get_drvdata(dev->parent);
1112        struct sca3000_state *st = indio_dev->dev_data;
1113        struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1114        long val;
1115        int ret;
1116        u8 *rx;
1117        u8 protect_mask = 0x03;
1118        ret = strict_strtol(buf, 10, &val);
1119        if (ret)
1120                return ret;
1121
1122        mutex_lock(&st->lock);
1123        /* First read the motion detector config to find out if
1124         * this axis is on*/
1125        ret = sca3000_read_ctrl_reg(st,
1126                                    SCA3000_REG_CTRL_SEL_MD_CTRL,
1127                                    &rx);
1128        if (ret)
1129                goto exit_point;
1130        /* Off and should be on */
1131        if (val && !(rx[1] & this_attr->mask)) {
1132                ret = sca3000_write_ctrl_reg(st,
1133                                             SCA3000_REG_CTRL_SEL_MD_CTRL,
1134                                             rx[1] | this_attr->mask);
1135                if (ret)
1136                        goto exit_point_free_rx;
1137                st->mo_det_use_count++;
1138        } else if (!val && (rx[1]&this_attr->mask)) {
1139                ret = sca3000_write_ctrl_reg(st,
1140                                             SCA3000_REG_CTRL_SEL_MD_CTRL,
1141                                             rx[1] & ~(this_attr->mask));
1142                if (ret)
1143                        goto exit_point_free_rx;
1144                st->mo_det_use_count--;
1145        } else /* relies on clean state for device on boot */
1146                goto exit_point_free_rx;
1147        kfree(rx);
1148        /* read current value of mode register */
1149        ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
1150        if (ret)
1151                goto exit_point;
1152        /*if off and should be on*/
1153        if ((st->mo_det_use_count)
1154            && ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1155                ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1156                                        (rx[1] & ~protect_mask)
1157                                        | SCA3000_MEAS_MODE_MOT_DET);
1158        /* if on and should be off */
1159        else if (!(st->mo_det_use_count)
1160                 && ((rx[1]&protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1161                ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1162                                        (rx[1] & ~protect_mask));
1163exit_point_free_rx:
1164        kfree(rx);
1165exit_point:
1166        mutex_unlock(&st->lock);
1167
1168        return ret ? ret : len;
1169}
1170
1171/* Shared event handler for all events as single event status register */
1172IIO_EVENT_SH(all, &sca3000_handler_th);
1173
1174/* Free fall detector related event attribute */
1175IIO_EVENT_ATTR_NAMED_SH(accel_xayaz_mag_falling_en,
1176                        accel_x&y&z_mag_falling_en,
1177                        iio_event_all,
1178                        sca3000_query_free_fall_mode,
1179                        sca3000_set_free_fall_mode,
1180                        0);
1181
1182IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period,
1183                     accel_x&y&z_mag_falling_period,
1184                     "0.226");
1185
1186/* Motion detector related event attributes */
1187IIO_EVENT_ATTR_SH(accel_x_mag_rising_en,
1188                  iio_event_all,
1189                  sca3000_query_mo_det,
1190                  sca3000_set_mo_det,
1191                  SCA3000_MD_CTRL_OR_X);
1192
1193IIO_EVENT_ATTR_SH(accel_y_mag_rising_en,
1194                  iio_event_all,
1195                  sca3000_query_mo_det,
1196                  sca3000_set_mo_det,
1197                  SCA3000_MD_CTRL_OR_Y);
1198
1199IIO_EVENT_ATTR_SH(accel_z_mag_rising_en,
1200                  iio_event_all,
1201                  sca3000_query_mo_det,
1202                  sca3000_set_mo_det,
1203                  SCA3000_MD_CTRL_OR_Z);
1204
1205/* Hardware ring buffer related event attributes */
1206IIO_EVENT_ATTR_RING_50_FULL_SH(iio_event_all,
1207                               sca3000_query_ring_int,
1208                               sca3000_set_ring_int,
1209                               SCA3000_INT_MASK_RING_HALF);
1210
1211IIO_EVENT_ATTR_RING_75_FULL_SH(iio_event_all,
1212                               sca3000_query_ring_int,
1213                               sca3000_set_ring_int,
1214                               SCA3000_INT_MASK_RING_THREE_QUARTER);
1215
1216static struct attribute *sca3000_event_attributes[] = {
1217        &iio_event_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1218        &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1219        &iio_event_attr_accel_x_mag_rising_en.dev_attr.attr,
1220        &iio_dev_attr_accel_x_raw_mag_rising_value.dev_attr.attr,
1221        &iio_event_attr_accel_y_mag_rising_en.dev_attr.attr,
1222        &iio_dev_attr_accel_y_raw_mag_rising_value.dev_attr.attr,
1223        &iio_event_attr_accel_z_mag_rising_en.dev_attr.attr,
1224        &iio_dev_attr_accel_z_raw_mag_rising_value.dev_attr.attr,
1225        &iio_event_attr_ring_50_full.dev_attr.attr,
1226        &iio_event_attr_ring_75_full.dev_attr.attr,
1227        NULL,
1228};
1229
1230static struct attribute_group sca3000_event_attribute_group = {
1231        .attrs = sca3000_event_attributes,
1232};
1233
1234/**
1235 * sca3000_clean_setup() get the device into a predictable state
1236 *
1237 * Devices use flash memory to store many of the register values
1238 * and hence can come up in somewhat unpredictable states.
1239 * Hence reset everything on driver load.
1240  **/
1241static int sca3000_clean_setup(struct sca3000_state *st)
1242{
1243        int ret;
1244        u8 *rx;
1245
1246        mutex_lock(&st->lock);
1247        /* Ensure all interrupts have been acknowledged */
1248        ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS, &rx, 1);
1249        if (ret)
1250                goto error_ret;
1251        kfree(rx);
1252
1253        /* Turn off all motion detection channels */
1254        ret = sca3000_read_ctrl_reg(st,
1255                                    SCA3000_REG_CTRL_SEL_MD_CTRL,
1256                                    &rx);
1257        if (ret)
1258                goto error_ret;
1259        ret = sca3000_write_ctrl_reg(st,
1260                                     SCA3000_REG_CTRL_SEL_MD_CTRL,
1261                                     rx[1] & SCA3000_MD_CTRL_PROT_MASK);
1262        kfree(rx);
1263        if (ret)
1264                goto error_ret;
1265
1266        /* Disable ring buffer */
1267        sca3000_read_ctrl_reg(st,
1268                              SCA3000_REG_CTRL_SEL_OUT_CTRL,
1269                              &rx);
1270        /* Frequency of ring buffer sampling deliberately restricted to make
1271         * debugging easier - add control of this later */
1272        ret = sca3000_write_ctrl_reg(st,
1273                                     SCA3000_REG_CTRL_SEL_OUT_CTRL,
1274                                     (rx[1] & SCA3000_OUT_CTRL_PROT_MASK)
1275                                     | SCA3000_OUT_CTRL_BUF_X_EN
1276                                     | SCA3000_OUT_CTRL_BUF_Y_EN
1277                                     | SCA3000_OUT_CTRL_BUF_Z_EN
1278                                     | SCA3000_OUT_CTRL_BUF_DIV_4);
1279        kfree(rx);
1280
1281        if (ret)
1282                goto error_ret;
1283        /* Enable interrupts, relevant to mode and set up as active low */
1284        ret = sca3000_read_data(st,
1285                          SCA3000_REG_ADDR_INT_MASK,
1286                          &rx, 1);
1287        if (ret)
1288                goto error_ret;
1289        ret = sca3000_write_reg(st,
1290                                SCA3000_REG_ADDR_INT_MASK,
1291                                (rx[1] & SCA3000_INT_MASK_PROT_MASK)
1292                                | SCA3000_INT_MASK_ACTIVE_LOW);
1293        kfree(rx);
1294        if (ret)
1295                goto error_ret;
1296        /* Select normal measurement mode, free fall off, ring off */
1297        /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1298         * as that occurs in one of the example on the datasheet */
1299        ret = sca3000_read_data(st,
1300                          SCA3000_REG_ADDR_MODE,
1301                          &rx, 1);
1302        if (ret)
1303                goto error_ret;
1304        ret = sca3000_write_reg(st,
1305                                SCA3000_REG_ADDR_MODE,
1306                                (rx[1] & SCA3000_MODE_PROT_MASK));
1307        kfree(rx);
1308        st->bpse = 11;
1309
1310error_ret:
1311        mutex_unlock(&st->lock);
1312        return ret;
1313}
1314
1315static int __devinit __sca3000_probe(struct spi_device *spi,
1316                                     enum sca3000_variant variant)
1317{
1318        int ret, regdone = 0;
1319        struct sca3000_state *st;
1320
1321        st = kzalloc(sizeof(struct sca3000_state), GFP_KERNEL);
1322        if (st == NULL) {
1323                ret = -ENOMEM;
1324                goto error_ret;
1325        }
1326        spi_set_drvdata(spi, st);
1327
1328        st->tx = kmalloc(sizeof(*st->tx)*6, GFP_KERNEL);
1329        if (st->tx == NULL) {
1330                ret = -ENOMEM;
1331                goto error_clear_st;
1332        }
1333        st->rx = kmalloc(sizeof(*st->rx)*3, GFP_KERNEL);
1334        if (st->rx == NULL) {
1335                ret = -ENOMEM;
1336                goto error_free_tx;
1337        }
1338        st->us = spi;
1339        mutex_init(&st->lock);
1340        st->info = &sca3000_spi_chip_info_tbl[variant];
1341
1342        st->indio_dev = iio_allocate_device();
1343        if (st->indio_dev == NULL) {
1344                ret = -ENOMEM;
1345                goto error_free_rx;
1346        }
1347
1348        st->indio_dev->dev.parent = &spi->dev;
1349        st->indio_dev->num_interrupt_lines = 1;
1350        st->indio_dev->event_attrs = &sca3000_event_attribute_group;
1351        if (st->info->temp_output)
1352                st->indio_dev->attrs = &sca3000_attribute_group_with_temp;
1353        else
1354                st->indio_dev->attrs = &sca3000_attribute_group;
1355        st->indio_dev->dev_data = (void *)(st);
1356        st->indio_dev->modes = INDIO_DIRECT_MODE;
1357
1358        sca3000_configure_ring(st->indio_dev);
1359
1360        ret = iio_device_register(st->indio_dev);
1361        if (ret < 0)
1362                goto error_free_dev;
1363        regdone = 1;
1364        ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
1365        if (ret < 0)
1366                goto error_unregister_dev;
1367        if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
1368                INIT_WORK(&st->interrupt_handler_ws,
1369                          sca3000_interrupt_handler_bh);
1370                ret = iio_register_interrupt_line(spi->irq,
1371                                                  st->indio_dev,
1372                                                  0,
1373                                                  IRQF_TRIGGER_FALLING,
1374                                                  "sca3000");
1375                if (ret)
1376                        goto error_unregister_ring;
1377                /* RFC
1378                 * Probably a common situation.  All interrupts need an ack
1379                 * and there is only one handler so the complicated list system
1380                 * is overkill.  At very least a simpler registration method
1381                 * might be worthwhile.
1382                 */
1383                iio_add_event_to_list(
1384                        iio_event_attr_accel_z_mag_rising_en.listel,
1385                        &st->indio_dev
1386                        ->interrupts[0]->ev_list);
1387        }
1388        sca3000_register_ring_funcs(st->indio_dev);
1389        ret = sca3000_clean_setup(st);
1390        if (ret)
1391                goto error_unregister_interrupt_line;
1392        return 0;
1393
1394error_unregister_interrupt_line:
1395        if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1396                iio_unregister_interrupt_line(st->indio_dev, 0);
1397error_unregister_ring:
1398        iio_ring_buffer_unregister(st->indio_dev->ring);
1399error_unregister_dev:
1400error_free_dev:
1401        if (regdone)
1402                iio_device_unregister(st->indio_dev);
1403        else
1404                iio_free_device(st->indio_dev);
1405error_free_rx:
1406        kfree(st->rx);
1407error_free_tx:
1408        kfree(st->tx);
1409error_clear_st:
1410        kfree(st);
1411error_ret:
1412        return ret;
1413}
1414
1415static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1416{
1417        int ret;
1418        u8 *rx;
1419
1420        mutex_lock(&st->lock);
1421        ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1422        if (ret)
1423                goto error_ret;
1424        ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1425                                (rx[1] & ~(SCA3000_INT_MASK_RING_THREE_QUARTER
1426                                           | SCA3000_INT_MASK_RING_HALF
1427                                           | SCA3000_INT_MASK_ALL_INTS)));
1428error_ret:
1429        kfree(rx);
1430        return ret;
1431
1432}
1433
1434static int sca3000_remove(struct spi_device *spi)
1435{
1436        struct sca3000_state *st =  spi_get_drvdata(spi);
1437        struct iio_dev *indio_dev = st->indio_dev;
1438        int ret;
1439        /* Must ensure no interrupts can be generated after this!*/
1440        ret = sca3000_stop_all_interrupts(st);
1441        if (ret)
1442                return ret;
1443        if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1444                iio_unregister_interrupt_line(indio_dev, 0);
1445        iio_ring_buffer_unregister(indio_dev->ring);
1446        sca3000_unconfigure_ring(indio_dev);
1447        iio_device_unregister(indio_dev);
1448
1449        kfree(st->tx);
1450        kfree(st->rx);
1451        kfree(st);
1452
1453        return 0;
1454}
1455
1456/* These macros save on an awful lot of repeated code */
1457#define SCA3000_VARIANT_PROBE(_name)                            \
1458        static int __devinit                                    \
1459        sca3000_##_name##_probe(struct spi_device *spi)         \
1460        {                                                       \
1461                return __sca3000_probe(spi, _name);             \
1462        }
1463
1464#define SCA3000_VARIANT_SPI_DRIVER(_name)                       \
1465        struct spi_driver sca3000_##_name##_driver = {          \
1466                .driver = {                                     \
1467                        .name = "sca3000_" #_name,              \
1468                        .owner = THIS_MODULE,                   \
1469                },                                              \
1470                .probe = sca3000_##_name##_probe,               \
1471                .remove = __devexit_p(sca3000_remove),          \
1472        }
1473
1474SCA3000_VARIANT_PROBE(d01);
1475static SCA3000_VARIANT_SPI_DRIVER(d01);
1476
1477SCA3000_VARIANT_PROBE(e02);
1478static SCA3000_VARIANT_SPI_DRIVER(e02);
1479
1480SCA3000_VARIANT_PROBE(e04);
1481static SCA3000_VARIANT_SPI_DRIVER(e04);
1482
1483SCA3000_VARIANT_PROBE(e05);
1484static SCA3000_VARIANT_SPI_DRIVER(e05);
1485
1486static __init int sca3000_init(void)
1487{
1488        int ret;
1489
1490        ret = spi_register_driver(&sca3000_d01_driver);
1491        if (ret)
1492                goto error_ret;
1493        ret = spi_register_driver(&sca3000_e02_driver);
1494        if (ret)
1495                goto error_unreg_d01;
1496        ret = spi_register_driver(&sca3000_e04_driver);
1497        if (ret)
1498                goto error_unreg_e02;
1499        ret = spi_register_driver(&sca3000_e05_driver);
1500        if (ret)
1501                goto error_unreg_e04;
1502
1503        return 0;
1504
1505error_unreg_e04:
1506        spi_unregister_driver(&sca3000_e04_driver);
1507error_unreg_e02:
1508        spi_unregister_driver(&sca3000_e02_driver);
1509error_unreg_d01:
1510        spi_unregister_driver(&sca3000_d01_driver);
1511error_ret:
1512
1513        return ret;
1514}
1515
1516static __exit void sca3000_exit(void)
1517{
1518        spi_unregister_driver(&sca3000_e05_driver);
1519        spi_unregister_driver(&sca3000_e04_driver);
1520        spi_unregister_driver(&sca3000_e02_driver);
1521        spi_unregister_driver(&sca3000_d01_driver);
1522}
1523
1524module_init(sca3000_init);
1525module_exit(sca3000_exit);
1526
1527MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1528MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1529MODULE_LICENSE("GPL v2");
1530