linux/drivers/iio/accel/bma220_spi.c
<<
>>
Prefs
   1/**
   2 * BMA220 Digital triaxial acceleration sensor driver
   3 *
   4 * Copyright (c) 2016, Intel Corporation.
   5 *
   6 * This file is subject to the terms and conditions of version 2 of
   7 * the GNU General Public License. See the file COPYING in the main
   8 * directory of this archive for more details.
   9 */
  10
  11#include <linux/acpi.h>
  12#include <linux/kernel.h>
  13#include <linux/module.h>
  14#include <linux/iio/buffer.h>
  15#include <linux/iio/iio.h>
  16#include <linux/iio/sysfs.h>
  17#include <linux/spi/spi.h>
  18#include <linux/iio/trigger_consumer.h>
  19#include <linux/iio/triggered_buffer.h>
  20
  21#define BMA220_REG_ID                           0x00
  22#define BMA220_REG_ACCEL_X                      0x02
  23#define BMA220_REG_ACCEL_Y                      0x03
  24#define BMA220_REG_ACCEL_Z                      0x04
  25#define BMA220_REG_RANGE                        0x11
  26#define BMA220_REG_SUSPEND                      0x18
  27
  28#define BMA220_CHIP_ID                          0xDD
  29#define BMA220_READ_MASK                        0x80
  30#define BMA220_RANGE_MASK                       0x03
  31#define BMA220_DATA_SHIFT                       2
  32#define BMA220_SUSPEND_SLEEP                    0xFF
  33#define BMA220_SUSPEND_WAKE                     0x00
  34
  35#define BMA220_DEVICE_NAME                      "bma220"
  36#define BMA220_SCALE_AVAILABLE                  "0.623 1.248 2.491 4.983"
  37
  38#define BMA220_ACCEL_CHANNEL(index, reg, axis) {                        \
  39        .type = IIO_ACCEL,                                              \
  40        .address = reg,                                                 \
  41        .modified = 1,                                                  \
  42        .channel2 = IIO_MOD_##axis,                                     \
  43        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
  44        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),           \
  45        .scan_index = index,                                            \
  46        .scan_type = {                                                  \
  47                .sign = 's',                                            \
  48                .realbits = 6,                                          \
  49                .storagebits = 8,                                       \
  50                .shift = BMA220_DATA_SHIFT,                             \
  51                .endianness = IIO_CPU,                                  \
  52        },                                                              \
  53}
  54
  55enum bma220_axis {
  56        AXIS_X,
  57        AXIS_Y,
  58        AXIS_Z,
  59};
  60
  61static IIO_CONST_ATTR(in_accel_scale_available, BMA220_SCALE_AVAILABLE);
  62
  63static struct attribute *bma220_attributes[] = {
  64        &iio_const_attr_in_accel_scale_available.dev_attr.attr,
  65        NULL,
  66};
  67
  68static const struct attribute_group bma220_attribute_group = {
  69        .attrs = bma220_attributes,
  70};
  71
  72static const int bma220_scale_table[][4] = {
  73        {0, 623000}, {1, 248000}, {2, 491000}, {4, 983000}
  74};
  75
  76struct bma220_data {
  77        struct spi_device *spi_device;
  78        struct mutex lock;
  79        s8 buffer[16]; /* 3x8-bit channels + 5x8 padding + 8x8 timestamp */
  80        u8 tx_buf[2] ____cacheline_aligned;
  81};
  82
  83static const struct iio_chan_spec bma220_channels[] = {
  84        BMA220_ACCEL_CHANNEL(0, BMA220_REG_ACCEL_X, X),
  85        BMA220_ACCEL_CHANNEL(1, BMA220_REG_ACCEL_Y, Y),
  86        BMA220_ACCEL_CHANNEL(2, BMA220_REG_ACCEL_Z, Z),
  87        IIO_CHAN_SOFT_TIMESTAMP(3),
  88};
  89
  90static inline int bma220_read_reg(struct spi_device *spi, u8 reg)
  91{
  92        return spi_w8r8(spi, reg | BMA220_READ_MASK);
  93}
  94
  95static const unsigned long bma220_accel_scan_masks[] = {
  96        BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
  97        0
  98};
  99
 100static irqreturn_t bma220_trigger_handler(int irq, void *p)
 101{
 102        int ret;
 103        struct iio_poll_func *pf = p;
 104        struct iio_dev *indio_dev = pf->indio_dev;
 105        struct bma220_data *data = iio_priv(indio_dev);
 106        struct spi_device *spi = data->spi_device;
 107
 108        mutex_lock(&data->lock);
 109        data->tx_buf[0] = BMA220_REG_ACCEL_X | BMA220_READ_MASK;
 110        ret = spi_write_then_read(spi, data->tx_buf, 1, data->buffer,
 111                                  ARRAY_SIZE(bma220_channels) - 1);
 112        if (ret < 0)
 113                goto err;
 114
 115        iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
 116                                           pf->timestamp);
 117err:
 118        mutex_unlock(&data->lock);
 119        iio_trigger_notify_done(indio_dev->trig);
 120
 121        return IRQ_HANDLED;
 122}
 123
 124static int bma220_read_raw(struct iio_dev *indio_dev,
 125                           struct iio_chan_spec const *chan,
 126                           int *val, int *val2, long mask)
 127{
 128        int ret;
 129        u8 range_idx;
 130        struct bma220_data *data = iio_priv(indio_dev);
 131
 132        switch (mask) {
 133        case IIO_CHAN_INFO_RAW:
 134                ret = bma220_read_reg(data->spi_device, chan->address);
 135                if (ret < 0)
 136                        return -EINVAL;
 137                *val = sign_extend32(ret >> BMA220_DATA_SHIFT, 5);
 138                return IIO_VAL_INT;
 139        case IIO_CHAN_INFO_SCALE:
 140                ret = bma220_read_reg(data->spi_device, BMA220_REG_RANGE);
 141                if (ret < 0)
 142                        return ret;
 143                range_idx = ret & BMA220_RANGE_MASK;
 144                *val = bma220_scale_table[range_idx][0];
 145                *val2 = bma220_scale_table[range_idx][1];
 146                return IIO_VAL_INT_PLUS_MICRO;
 147        }
 148
 149        return -EINVAL;
 150}
 151
 152static int bma220_write_raw(struct iio_dev *indio_dev,
 153                            struct iio_chan_spec const *chan,
 154                            int val, int val2, long mask)
 155{
 156        int i;
 157        int ret;
 158        int index = -1;
 159        struct bma220_data *data = iio_priv(indio_dev);
 160
 161        switch (mask) {
 162        case IIO_CHAN_INFO_SCALE:
 163                for (i = 0; i < ARRAY_SIZE(bma220_scale_table); i++)
 164                        if (val == bma220_scale_table[i][0] &&
 165                            val2 == bma220_scale_table[i][1]) {
 166                                index = i;
 167                                break;
 168                        }
 169                if (index < 0)
 170                        return -EINVAL;
 171
 172                mutex_lock(&data->lock);
 173                data->tx_buf[0] = BMA220_REG_RANGE;
 174                data->tx_buf[1] = index;
 175                ret = spi_write(data->spi_device, data->tx_buf,
 176                                sizeof(data->tx_buf));
 177                if (ret < 0)
 178                        dev_err(&data->spi_device->dev,
 179                                "failed to set measurement range\n");
 180                mutex_unlock(&data->lock);
 181
 182                return 0;
 183        }
 184
 185        return -EINVAL;
 186}
 187
 188static const struct iio_info bma220_info = {
 189        .read_raw               = bma220_read_raw,
 190        .write_raw              = bma220_write_raw,
 191        .attrs                  = &bma220_attribute_group,
 192};
 193
 194static int bma220_init(struct spi_device *spi)
 195{
 196        int ret;
 197
 198        ret = bma220_read_reg(spi, BMA220_REG_ID);
 199        if (ret != BMA220_CHIP_ID)
 200                return -ENODEV;
 201
 202        /* Make sure the chip is powered on */
 203        ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
 204        if (ret < 0)
 205                return ret;
 206        else if (ret == BMA220_SUSPEND_WAKE)
 207                return bma220_read_reg(spi, BMA220_REG_SUSPEND);
 208
 209        return 0;
 210}
 211
 212static int bma220_deinit(struct spi_device *spi)
 213{
 214        int ret;
 215
 216        /* Make sure the chip is powered off */
 217        ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
 218        if (ret < 0)
 219                return ret;
 220        else if (ret == BMA220_SUSPEND_SLEEP)
 221                return bma220_read_reg(spi, BMA220_REG_SUSPEND);
 222
 223        return 0;
 224}
 225
 226static int bma220_probe(struct spi_device *spi)
 227{
 228        int ret;
 229        struct iio_dev *indio_dev;
 230        struct bma220_data *data;
 231
 232        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
 233        if (!indio_dev) {
 234                dev_err(&spi->dev, "iio allocation failed!\n");
 235                return -ENOMEM;
 236        }
 237
 238        data = iio_priv(indio_dev);
 239        data->spi_device = spi;
 240        spi_set_drvdata(spi, indio_dev);
 241        mutex_init(&data->lock);
 242
 243        indio_dev->dev.parent = &spi->dev;
 244        indio_dev->info = &bma220_info;
 245        indio_dev->name = BMA220_DEVICE_NAME;
 246        indio_dev->modes = INDIO_DIRECT_MODE;
 247        indio_dev->channels = bma220_channels;
 248        indio_dev->num_channels = ARRAY_SIZE(bma220_channels);
 249        indio_dev->available_scan_masks = bma220_accel_scan_masks;
 250
 251        ret = bma220_init(data->spi_device);
 252        if (ret < 0)
 253                return ret;
 254
 255        ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
 256                                         bma220_trigger_handler, NULL);
 257        if (ret < 0) {
 258                dev_err(&spi->dev, "iio triggered buffer setup failed\n");
 259                goto err_suspend;
 260        }
 261
 262        ret = iio_device_register(indio_dev);
 263        if (ret < 0) {
 264                dev_err(&spi->dev, "iio_device_register failed\n");
 265                iio_triggered_buffer_cleanup(indio_dev);
 266                goto err_suspend;
 267        }
 268
 269        return 0;
 270
 271err_suspend:
 272        return bma220_deinit(spi);
 273}
 274
 275static int bma220_remove(struct spi_device *spi)
 276{
 277        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 278
 279        iio_device_unregister(indio_dev);
 280        iio_triggered_buffer_cleanup(indio_dev);
 281
 282        return bma220_deinit(spi);
 283}
 284
 285#ifdef CONFIG_PM_SLEEP
 286static int bma220_suspend(struct device *dev)
 287{
 288        struct bma220_data *data =
 289                        iio_priv(spi_get_drvdata(to_spi_device(dev)));
 290
 291        /* The chip can be suspended/woken up by a simple register read. */
 292        return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
 293}
 294
 295static int bma220_resume(struct device *dev)
 296{
 297        struct bma220_data *data =
 298                        iio_priv(spi_get_drvdata(to_spi_device(dev)));
 299
 300        return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
 301}
 302
 303static SIMPLE_DEV_PM_OPS(bma220_pm_ops, bma220_suspend, bma220_resume);
 304
 305#define BMA220_PM_OPS (&bma220_pm_ops)
 306#else
 307#define BMA220_PM_OPS NULL
 308#endif
 309
 310static const struct spi_device_id bma220_spi_id[] = {
 311        {"bma220", 0},
 312        {}
 313};
 314
 315static const struct acpi_device_id bma220_acpi_id[] = {
 316        {"BMA0220", 0},
 317        {}
 318};
 319
 320MODULE_DEVICE_TABLE(spi, bma220_spi_id);
 321
 322static struct spi_driver bma220_driver = {
 323        .driver = {
 324                .name = "bma220_spi",
 325                .pm = BMA220_PM_OPS,
 326                .acpi_match_table = ACPI_PTR(bma220_acpi_id),
 327        },
 328        .probe =            bma220_probe,
 329        .remove =           bma220_remove,
 330        .id_table =         bma220_spi_id,
 331};
 332
 333module_spi_driver(bma220_driver);
 334
 335MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
 336MODULE_DESCRIPTION("BMA220 acceleration sensor driver");
 337MODULE_LICENSE("GPL v2");
 338