linux/drivers/staging/iio/gyro/adis16060_core.c
<<
>>
Prefs
   1/*
   2 * ADIS16060 Wide Bandwidth Yaw Rate Gyroscope with SPI driver
   3 *
   4 * Copyright 2010 Analog Devices Inc.
   5 *
   6 * Licensed under the GPL-2 or later.
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/delay.h>
  11#include <linux/mutex.h>
  12#include <linux/device.h>
  13#include <linux/kernel.h>
  14#include <linux/spi/spi.h>
  15#include <linux/slab.h>
  16#include <linux/sysfs.h>
  17
  18#include <linux/iio/iio.h>
  19#include <linux/iio/sysfs.h>
  20
  21#define ADIS16060_GYRO          0x20 /* Measure Angular Rate (Gyro) */
  22#define ADIS16060_TEMP_OUT      0x10 /* Measure Temperature */
  23#define ADIS16060_AIN2          0x80 /* Measure AIN2 */
  24#define ADIS16060_AIN1          0x40 /* Measure AIN1 */
  25
  26/**
  27 * struct adis16060_state - device instance specific data
  28 * @us_w:               actual spi_device to write config
  29 * @us_r:               actual spi_device to read back data
  30 * @buf:                transmit or receive buffer
  31 * @buf_lock:           mutex to protect tx and rx
  32 **/
  33struct adis16060_state {
  34        struct spi_device               *us_w;
  35        struct spi_device               *us_r;
  36        struct mutex                    buf_lock;
  37
  38        u8 buf[3] ____cacheline_aligned;
  39};
  40
  41static struct iio_dev *adis16060_iio_dev;
  42
  43static int adis16060_spi_write(struct iio_dev *indio_dev, u8 val)
  44{
  45        int ret;
  46        struct adis16060_state *st = iio_priv(indio_dev);
  47
  48        mutex_lock(&st->buf_lock);
  49        st->buf[2] = val; /* The last 8 bits clocked in are latched */
  50        ret = spi_write(st->us_w, st->buf, 3);
  51        mutex_unlock(&st->buf_lock);
  52
  53        return ret;
  54}
  55
  56static int adis16060_spi_read(struct iio_dev *indio_dev, u16 *val)
  57{
  58        int ret;
  59        struct adis16060_state *st = iio_priv(indio_dev);
  60
  61        mutex_lock(&st->buf_lock);
  62
  63        ret = spi_read(st->us_r, st->buf, 3);
  64
  65        /* The internal successive approximation ADC begins the
  66         * conversion process on the falling edge of MSEL1 and
  67         * starts to place data MSB first on the DOUT line at
  68         * the 6th falling edge of SCLK
  69         */
  70        if (!ret)
  71                *val = ((st->buf[0] & 0x3) << 12) |
  72                        (st->buf[1] << 4) |
  73                        ((st->buf[2] >> 4) & 0xF);
  74        mutex_unlock(&st->buf_lock);
  75
  76        return ret;
  77}
  78
  79static int adis16060_read_raw(struct iio_dev *indio_dev,
  80                              struct iio_chan_spec const *chan,
  81                              int *val, int *val2,
  82                              long mask)
  83{
  84        u16 tval = 0;
  85        int ret;
  86
  87        switch (mask) {
  88        case IIO_CHAN_INFO_RAW:
  89                /* Take the iio_dev status lock */
  90                mutex_lock(&indio_dev->mlock);
  91                ret = adis16060_spi_write(indio_dev, chan->address);
  92                if (ret < 0)
  93                        goto out_unlock;
  94
  95                ret = adis16060_spi_read(indio_dev, &tval);
  96                if (ret < 0)
  97                        goto out_unlock;
  98
  99                mutex_unlock(&indio_dev->mlock);
 100                *val = tval;
 101                return IIO_VAL_INT;
 102        case IIO_CHAN_INFO_OFFSET:
 103                *val = -7;
 104                *val2 = 461117;
 105                return IIO_VAL_INT_PLUS_MICRO;
 106        case IIO_CHAN_INFO_SCALE:
 107                *val = 0;
 108                *val2 = 34000;
 109                return IIO_VAL_INT_PLUS_MICRO;
 110        }
 111
 112        return -EINVAL;
 113
 114out_unlock:
 115        mutex_unlock(&indio_dev->mlock);
 116        return ret;
 117}
 118
 119static const struct iio_info adis16060_info = {
 120        .read_raw = &adis16060_read_raw,
 121        .driver_module = THIS_MODULE,
 122};
 123
 124static const struct iio_chan_spec adis16060_channels[] = {
 125        {
 126                .type = IIO_ANGL_VEL,
 127                .modified = 1,
 128                .channel2 = IIO_MOD_Z,
 129                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 130                .address = ADIS16060_GYRO,
 131        }, {
 132                .type = IIO_VOLTAGE,
 133                .indexed = 1,
 134                .channel = 0,
 135                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 136                .address = ADIS16060_AIN1,
 137        }, {
 138                .type = IIO_VOLTAGE,
 139                .indexed = 1,
 140                .channel = 1,
 141                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 142                .address = ADIS16060_AIN2,
 143        }, {
 144                .type = IIO_TEMP,
 145                .indexed = 1,
 146                .channel = 0,
 147                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
 148                BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE),
 149                .address = ADIS16060_TEMP_OUT,
 150        }
 151};
 152
 153static int adis16060_r_probe(struct spi_device *spi)
 154{
 155        int ret;
 156        struct adis16060_state *st;
 157        struct iio_dev *indio_dev;
 158
 159        /* setup the industrialio driver allocated elements */
 160        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 161        if (!indio_dev)
 162                return -ENOMEM;
 163        /* this is only used for removal purposes */
 164        spi_set_drvdata(spi, indio_dev);
 165        st = iio_priv(indio_dev);
 166        st->us_r = spi;
 167        mutex_init(&st->buf_lock);
 168
 169        indio_dev->name = spi->dev.driver->name;
 170        indio_dev->dev.parent = &spi->dev;
 171        indio_dev->info = &adis16060_info;
 172        indio_dev->modes = INDIO_DIRECT_MODE;
 173        indio_dev->channels = adis16060_channels;
 174        indio_dev->num_channels = ARRAY_SIZE(adis16060_channels);
 175
 176        ret = devm_iio_device_register(&spi->dev, indio_dev);
 177        if (ret)
 178                return ret;
 179
 180        adis16060_iio_dev = indio_dev;
 181        return 0;
 182}
 183
 184static int adis16060_w_probe(struct spi_device *spi)
 185{
 186        int ret;
 187        struct iio_dev *indio_dev = adis16060_iio_dev;
 188        struct adis16060_state *st;
 189
 190        if (!indio_dev) {
 191                ret =  -ENODEV;
 192                goto error_ret;
 193        }
 194        st = iio_priv(indio_dev);
 195        spi_set_drvdata(spi, indio_dev);
 196        st->us_w = spi;
 197        return 0;
 198
 199error_ret:
 200        return ret;
 201}
 202
 203static int adis16060_w_remove(struct spi_device *spi)
 204{
 205        return 0;
 206}
 207
 208static struct spi_driver adis16060_r_driver = {
 209        .driver = {
 210                .name = "adis16060_r",
 211        },
 212        .probe = adis16060_r_probe,
 213};
 214
 215static struct spi_driver adis16060_w_driver = {
 216        .driver = {
 217                .name = "adis16060_w",
 218        },
 219        .probe = adis16060_w_probe,
 220        .remove = adis16060_w_remove,
 221};
 222
 223static __init int adis16060_init(void)
 224{
 225        int ret;
 226
 227        ret = spi_register_driver(&adis16060_r_driver);
 228        if (ret < 0)
 229                return ret;
 230
 231        ret = spi_register_driver(&adis16060_w_driver);
 232        if (ret < 0) {
 233                spi_unregister_driver(&adis16060_r_driver);
 234                return ret;
 235        }
 236
 237        return 0;
 238}
 239module_init(adis16060_init);
 240
 241static __exit void adis16060_exit(void)
 242{
 243        spi_unregister_driver(&adis16060_w_driver);
 244        spi_unregister_driver(&adis16060_r_driver);
 245}
 246module_exit(adis16060_exit);
 247
 248MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
 249MODULE_DESCRIPTION("Analog Devices ADIS16060 Yaw Rate Gyroscope Driver");
 250MODULE_LICENSE("GPL v2");
 251