linux/drivers/iio/gyro/itg3200_core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * itg3200_core.c -- support InvenSense ITG3200
   4 *                   Digital 3-Axis Gyroscope driver
   5 *
   6 * Copyright (c) 2011 Christian Strobel <christian.strobel@iis.fraunhofer.de>
   7 * Copyright (c) 2011 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
   8 * Copyright (c) 2012 Thorsten Nowak <thorsten.nowak@iis.fraunhofer.de>
   9 *
  10 * TODO:
  11 * - Support digital low pass filter
  12 * - Support power management
  13 */
  14
  15#include <linux/interrupt.h>
  16#include <linux/irq.h>
  17#include <linux/i2c.h>
  18#include <linux/slab.h>
  19#include <linux/stat.h>
  20#include <linux/module.h>
  21#include <linux/delay.h>
  22
  23#include <linux/iio/iio.h>
  24#include <linux/iio/sysfs.h>
  25#include <linux/iio/events.h>
  26#include <linux/iio/buffer.h>
  27
  28#include <linux/iio/gyro/itg3200.h>
  29
  30
  31int itg3200_write_reg_8(struct iio_dev *indio_dev,
  32                u8 reg_address, u8 val)
  33{
  34        struct itg3200 *st = iio_priv(indio_dev);
  35
  36        return i2c_smbus_write_byte_data(st->i2c, 0x80 | reg_address, val);
  37}
  38
  39int itg3200_read_reg_8(struct iio_dev *indio_dev,
  40                u8 reg_address, u8 *val)
  41{
  42        struct itg3200 *st = iio_priv(indio_dev);
  43        int ret;
  44
  45        ret = i2c_smbus_read_byte_data(st->i2c, reg_address);
  46        if (ret < 0)
  47                return ret;
  48        *val = ret;
  49        return 0;
  50}
  51
  52static int itg3200_read_reg_s16(struct iio_dev *indio_dev, u8 lower_reg_address,
  53                int *val)
  54{
  55        struct itg3200 *st = iio_priv(indio_dev);
  56        struct i2c_client *client = st->i2c;
  57        int ret;
  58        s16 out;
  59
  60        struct i2c_msg msg[2] = {
  61                {
  62                        .addr = client->addr,
  63                        .flags = client->flags,
  64                        .len = 1,
  65                        .buf = (char *)&lower_reg_address,
  66                },
  67                {
  68                        .addr = client->addr,
  69                        .flags = client->flags | I2C_M_RD,
  70                        .len = 2,
  71                        .buf = (char *)&out,
  72                },
  73        };
  74
  75        lower_reg_address |= 0x80;
  76        ret = i2c_transfer(client->adapter, msg, 2);
  77        be16_to_cpus(&out);
  78        *val = out;
  79
  80        return (ret == 2) ? 0 : ret;
  81}
  82
  83static int itg3200_read_raw(struct iio_dev *indio_dev,
  84                const struct iio_chan_spec *chan,
  85                int *val, int *val2, long info)
  86{
  87        int ret = 0;
  88        u8 reg;
  89        u8 regval;
  90
  91        switch (info) {
  92        case IIO_CHAN_INFO_RAW:
  93                reg = (u8)chan->address;
  94                ret = itg3200_read_reg_s16(indio_dev, reg, val);
  95                return IIO_VAL_INT;
  96        case IIO_CHAN_INFO_SCALE:
  97                *val = 0;
  98                if (chan->type == IIO_TEMP)
  99                        *val2 = 1000000000/280;
 100                else
 101                        *val2 = 1214142; /* (1 / 14,375) * (PI / 180) */
 102                return IIO_VAL_INT_PLUS_NANO;
 103        case IIO_CHAN_INFO_OFFSET:
 104                /* Only the temperature channel has an offset */
 105                *val = 23000;
 106                return IIO_VAL_INT;
 107        case IIO_CHAN_INFO_SAMP_FREQ:
 108                ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &regval);
 109                if (ret)
 110                        return ret;
 111
 112                *val = (regval & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
 113
 114                ret = itg3200_read_reg_8(indio_dev,
 115                                         ITG3200_REG_SAMPLE_RATE_DIV,
 116                                         &regval);
 117                if (ret)
 118                        return ret;
 119
 120                *val /= regval + 1;
 121                return IIO_VAL_INT;
 122
 123        default:
 124                return -EINVAL;
 125        }
 126}
 127
 128static int itg3200_write_raw(struct iio_dev *indio_dev,
 129                             struct iio_chan_spec const *chan,
 130                             int val,
 131                             int val2,
 132                             long mask)
 133{
 134        int ret;
 135        u8 t;
 136
 137        switch (mask) {
 138        case IIO_CHAN_INFO_SAMP_FREQ:
 139                if (val == 0 || val2 != 0)
 140                        return -EINVAL;
 141
 142                mutex_lock(&indio_dev->mlock);
 143
 144                ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
 145                if (ret) {
 146                        mutex_unlock(&indio_dev->mlock);
 147                        return ret;
 148                }
 149                t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
 150
 151                ret = itg3200_write_reg_8(indio_dev,
 152                                          ITG3200_REG_SAMPLE_RATE_DIV,
 153                                          t);
 154
 155                mutex_unlock(&indio_dev->mlock);
 156                return ret;
 157
 158        default:
 159                return -EINVAL;
 160        }
 161}
 162
 163/*
 164 * Reset device and internal registers to the power-up-default settings
 165 * Use the gyro clock as reference, as suggested by the datasheet
 166 */
 167static int itg3200_reset(struct iio_dev *indio_dev)
 168{
 169        struct itg3200 *st = iio_priv(indio_dev);
 170        int ret;
 171
 172        dev_dbg(&st->i2c->dev, "reset device");
 173
 174        ret = itg3200_write_reg_8(indio_dev,
 175                        ITG3200_REG_POWER_MANAGEMENT,
 176                        ITG3200_RESET);
 177        if (ret) {
 178                dev_err(&st->i2c->dev, "error resetting device");
 179                goto error_ret;
 180        }
 181
 182        /* Wait for PLL (1ms according to datasheet) */
 183        udelay(1500);
 184
 185        ret = itg3200_write_reg_8(indio_dev,
 186                        ITG3200_REG_IRQ_CONFIG,
 187                        ITG3200_IRQ_ACTIVE_HIGH |
 188                        ITG3200_IRQ_PUSH_PULL |
 189                        ITG3200_IRQ_LATCH_50US_PULSE |
 190                        ITG3200_IRQ_LATCH_CLEAR_ANY);
 191
 192        if (ret)
 193                dev_err(&st->i2c->dev, "error init device");
 194
 195error_ret:
 196        return ret;
 197}
 198
 199/* itg3200_enable_full_scale() - Disables the digital low pass filter */
 200static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
 201{
 202        u8 val;
 203        int ret;
 204
 205        ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
 206        if (ret)
 207                goto err_ret;
 208
 209        val |= ITG3200_DLPF_FS_SEL_2000;
 210        return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
 211
 212err_ret:
 213        return ret;
 214}
 215
 216static int itg3200_initial_setup(struct iio_dev *indio_dev)
 217{
 218        struct itg3200 *st = iio_priv(indio_dev);
 219        int ret;
 220        u8 val;
 221
 222        ret = itg3200_reset(indio_dev);
 223        if (ret)
 224                goto err_ret;
 225
 226        ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
 227        if (ret)
 228                goto err_ret;
 229
 230        if (((val >> 1) & 0x3f) != 0x34) {
 231                dev_err(&st->i2c->dev, "invalid reg value 0x%02x", val);
 232                ret = -ENXIO;
 233                goto err_ret;
 234        }
 235
 236        ret = itg3200_enable_full_scale(indio_dev);
 237err_ret:
 238        return ret;
 239}
 240
 241static const struct iio_mount_matrix *
 242itg3200_get_mount_matrix(const struct iio_dev *indio_dev,
 243                          const struct iio_chan_spec *chan)
 244{
 245        struct itg3200 *data = iio_priv(indio_dev);
 246
 247        return &data->orientation;
 248}
 249
 250static const struct iio_chan_spec_ext_info itg3200_ext_info[] = {
 251        IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, itg3200_get_mount_matrix),
 252        { }
 253};
 254
 255#define ITG3200_ST                                              \
 256        { .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
 257
 258#define ITG3200_GYRO_CHAN(_mod) { \
 259        .type = IIO_ANGL_VEL, \
 260        .modified = 1, \
 261        .channel2 = IIO_MOD_ ## _mod, \
 262        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
 263        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
 264        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 265        .address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
 266        .scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
 267        .scan_type = ITG3200_ST, \
 268        .ext_info = itg3200_ext_info, \
 269}
 270
 271static const struct iio_chan_spec itg3200_channels[] = {
 272        {
 273                .type = IIO_TEMP,
 274                .channel2 = IIO_NO_MOD,
 275                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 276                .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 277                BIT(IIO_CHAN_INFO_SCALE),
 278                .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 279                .address = ITG3200_REG_TEMP_OUT_H,
 280                .scan_index = ITG3200_SCAN_TEMP,
 281                .scan_type = ITG3200_ST,
 282        },
 283        ITG3200_GYRO_CHAN(X),
 284        ITG3200_GYRO_CHAN(Y),
 285        ITG3200_GYRO_CHAN(Z),
 286        IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
 287};
 288
 289static const struct iio_info itg3200_info = {
 290        .read_raw = &itg3200_read_raw,
 291        .write_raw = &itg3200_write_raw,
 292};
 293
 294static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
 295
 296static int itg3200_probe(struct i2c_client *client,
 297                const struct i2c_device_id *id)
 298{
 299        int ret;
 300        struct itg3200 *st;
 301        struct iio_dev *indio_dev;
 302
 303        dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
 304
 305        indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
 306        if (!indio_dev)
 307                return -ENOMEM;
 308
 309        st = iio_priv(indio_dev);
 310
 311        ret = iio_read_mount_matrix(&client->dev, "mount-matrix",
 312                                &st->orientation);
 313        if (ret)
 314                return ret;
 315
 316        i2c_set_clientdata(client, indio_dev);
 317        st->i2c = client;
 318
 319        indio_dev->name = client->dev.driver->name;
 320        indio_dev->channels = itg3200_channels;
 321        indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
 322        indio_dev->available_scan_masks = itg3200_available_scan_masks;
 323        indio_dev->info = &itg3200_info;
 324        indio_dev->modes = INDIO_DIRECT_MODE;
 325
 326        ret = itg3200_buffer_configure(indio_dev);
 327        if (ret)
 328                return ret;
 329
 330        if (client->irq) {
 331                ret = itg3200_probe_trigger(indio_dev);
 332                if (ret)
 333                        goto error_unconfigure_buffer;
 334        }
 335
 336        ret = itg3200_initial_setup(indio_dev);
 337        if (ret)
 338                goto error_remove_trigger;
 339
 340        ret = iio_device_register(indio_dev);
 341        if (ret)
 342                goto error_remove_trigger;
 343
 344        return 0;
 345
 346error_remove_trigger:
 347        if (client->irq)
 348                itg3200_remove_trigger(indio_dev);
 349error_unconfigure_buffer:
 350        itg3200_buffer_unconfigure(indio_dev);
 351        return ret;
 352}
 353
 354static int itg3200_remove(struct i2c_client *client)
 355{
 356        struct iio_dev *indio_dev = i2c_get_clientdata(client);
 357
 358        iio_device_unregister(indio_dev);
 359
 360        if (client->irq)
 361                itg3200_remove_trigger(indio_dev);
 362
 363        itg3200_buffer_unconfigure(indio_dev);
 364
 365        return 0;
 366}
 367
 368static int __maybe_unused itg3200_suspend(struct device *dev)
 369{
 370        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 371        struct itg3200 *st = iio_priv(indio_dev);
 372
 373        dev_dbg(&st->i2c->dev, "suspend device");
 374
 375        return itg3200_write_reg_8(indio_dev, ITG3200_REG_POWER_MANAGEMENT,
 376                                   ITG3200_SLEEP);
 377}
 378
 379static int __maybe_unused itg3200_resume(struct device *dev)
 380{
 381        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 382
 383        return itg3200_initial_setup(indio_dev);
 384}
 385
 386static SIMPLE_DEV_PM_OPS(itg3200_pm_ops, itg3200_suspend, itg3200_resume);
 387
 388static const struct i2c_device_id itg3200_id[] = {
 389        { "itg3200", 0 },
 390        { }
 391};
 392MODULE_DEVICE_TABLE(i2c, itg3200_id);
 393
 394static const struct of_device_id itg3200_of_match[] = {
 395        { .compatible = "invensense,itg3200" },
 396        { }
 397};
 398MODULE_DEVICE_TABLE(of, itg3200_of_match);
 399
 400static struct i2c_driver itg3200_driver = {
 401        .driver = {
 402                .name   = "itg3200",
 403                .of_match_table = itg3200_of_match,
 404                .pm     = &itg3200_pm_ops,
 405        },
 406        .id_table       = itg3200_id,
 407        .probe          = itg3200_probe,
 408        .remove         = itg3200_remove,
 409};
 410
 411module_i2c_driver(itg3200_driver);
 412
 413MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
 414MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
 415MODULE_LICENSE("GPL v2");
 416