linux/drivers/staging/iio/adc/ad7152.c
<<
>>
Prefs
   1/*
   2 * AD7152 capacitive sensor driver supporting AD7152/3
   3 *
   4 * Copyright 2010 Analog Devices Inc.
   5 *
   6 * Licensed under the GPL-2 or later.
   7 */
   8
   9#include <linux/interrupt.h>
  10#include <linux/gpio.h>
  11#include <linux/workqueue.h>
  12#include <linux/device.h>
  13#include <linux/kernel.h>
  14#include <linux/slab.h>
  15#include <linux/sysfs.h>
  16#include <linux/list.h>
  17#include <linux/i2c.h>
  18#include <linux/rtc.h>
  19
  20#include "../iio.h"
  21#include "../sysfs.h"
  22
  23/*
  24 * AD7152 registers definition
  25 */
  26
  27#define AD7152_STATUS              0
  28#define AD7152_STATUS_RDY1         (1 << 0)
  29#define AD7152_STATUS_RDY2         (1 << 1)
  30#define AD7152_CH1_DATA_HIGH       1
  31#define AD7152_CH1_DATA_LOW        2
  32#define AD7152_CH2_DATA_HIGH       3
  33#define AD7152_CH2_DATA_LOW        4
  34#define AD7152_CH1_OFFS_HIGH       5
  35#define AD7152_CH1_OFFS_LOW        6
  36#define AD7152_CH2_OFFS_HIGH       7
  37#define AD7152_CH2_OFFS_LOW        8
  38#define AD7152_CH1_GAIN_HIGH       9
  39#define AD7152_CH1_GAIN_LOW        10
  40#define AD7152_CH1_SETUP           11
  41#define AD7152_CH2_GAIN_HIGH       12
  42#define AD7152_CH2_GAIN_LOW        13
  43#define AD7152_CH2_SETUP           14
  44#define AD7152_CFG                 15
  45#define AD7152_RESEVERD            16
  46#define AD7152_CAPDAC_POS          17
  47#define AD7152_CAPDAC_NEG          18
  48#define AD7152_CFG2                26
  49
  50#define AD7152_MAX_CONV_MODE       6
  51
  52/*
  53 * struct ad7152_chip_info - chip specifc information
  54 */
  55
  56struct ad7152_chip_info {
  57        const char *name;
  58        struct i2c_client *client;
  59        struct iio_dev *indio_dev;
  60        u16 ch1_offset;     /* Channel 1 offset calibration coefficient */
  61        u16 ch1_gain;       /* Channel 1 gain coefficient */
  62        u8  ch1_setup;
  63        u16 ch2_offset;     /* Channel 2 offset calibration coefficient */
  64        u16 ch2_gain;       /* Channel 1 gain coefficient */
  65        u8  ch2_setup;
  66        u8  filter_rate_setup; /* Capacitive channel digital filter setup; conversion time/update rate setup per channel */
  67        char *conversion_mode;
  68};
  69
  70struct ad7152_conversion_mode {
  71        char *name;
  72        u8 reg_cfg;
  73};
  74
  75struct ad7152_conversion_mode ad7152_conv_mode_table[AD7152_MAX_CONV_MODE] = {
  76        { "idle", 0 },
  77        { "continuous-conversion", 1 },
  78        { "single-conversion", 2 },
  79        { "power-down", 3 },
  80        { "offset-calibration", 5 },
  81        { "gain-calibration", 6 },
  82};
  83
  84/*
  85 * ad7152 register access by I2C
  86 */
  87
  88static int ad7152_i2c_read(struct ad7152_chip_info *chip, u8 reg, u8 *data, int len)
  89{
  90        struct i2c_client *client = chip->client;
  91        int ret;
  92
  93        ret = i2c_master_send(client, &reg, 1);
  94        if (ret < 0) {
  95                dev_err(&client->dev, "I2C write error\n");
  96                return ret;
  97        }
  98
  99        ret = i2c_master_recv(client, data, len);
 100        if (ret < 0) {
 101                dev_err(&client->dev, "I2C read error\n");
 102        }
 103
 104        return ret;
 105}
 106
 107static int ad7152_i2c_write(struct ad7152_chip_info *chip, u8 reg, u8 data)
 108{
 109        struct i2c_client *client = chip->client;
 110        int ret;
 111
 112        u8 tx[2] = {
 113                reg,
 114                data,
 115        };
 116
 117        ret = i2c_master_send(client, tx, 2);
 118        if (ret < 0)
 119                dev_err(&client->dev, "I2C write error\n");
 120
 121        return ret;
 122}
 123
 124/*
 125 * sysfs nodes
 126 */
 127
 128#define IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(_show)                              \
 129        IIO_DEVICE_ATTR(available_conversion_modes, S_IRUGO, _show, NULL, 0)
 130#define IIO_DEV_ATTR_CONVERSION_MODE(_mode, _show, _store)              \
 131        IIO_DEVICE_ATTR(conversion_mode, _mode, _show, _store, 0)
 132#define IIO_DEV_ATTR_CH1_OFFSET(_mode, _show, _store)           \
 133        IIO_DEVICE_ATTR(ch1_offset, _mode, _show, _store, 0)
 134#define IIO_DEV_ATTR_CH2_OFFSET(_mode, _show, _store)           \
 135        IIO_DEVICE_ATTR(ch2_offset, _mode, _show, _store, 0)
 136#define IIO_DEV_ATTR_CH1_GAIN(_mode, _show, _store)             \
 137        IIO_DEVICE_ATTR(ch1_gain, _mode, _show, _store, 0)
 138#define IIO_DEV_ATTR_CH2_GAIN(_mode, _show, _store)             \
 139        IIO_DEVICE_ATTR(ch2_gain, _mode, _show, _store, 0)
 140#define IIO_DEV_ATTR_CH1_VALUE(_show)           \
 141        IIO_DEVICE_ATTR(ch1_value, S_IRUGO, _show, NULL, 0)
 142#define IIO_DEV_ATTR_CH2_VALUE(_show)           \
 143        IIO_DEVICE_ATTR(ch2_value, S_IRUGO, _show, NULL, 0)
 144#define IIO_DEV_ATTR_CH1_SETUP(_mode, _show, _store)            \
 145        IIO_DEVICE_ATTR(ch1_setup, _mode, _show, _store, 0)
 146#define IIO_DEV_ATTR_CH2_SETUP(_mode, _show, _store)              \
 147        IIO_DEVICE_ATTR(ch2_setup, _mode, _show, _store, 0)
 148#define IIO_DEV_ATTR_FILTER_RATE_SETUP(_mode, _show, _store)              \
 149        IIO_DEVICE_ATTR(filter_rate_setup, _mode, _show, _store, 0)
 150
 151static ssize_t ad7152_show_conversion_modes(struct device *dev,
 152                struct device_attribute *attr,
 153                char *buf)
 154{
 155        int i;
 156        int len = 0;
 157
 158        for (i = 0; i < AD7152_MAX_CONV_MODE; i++)
 159                len += sprintf(buf + len, "%s ", ad7152_conv_mode_table[i].name);
 160
 161        len += sprintf(buf + len, "\n");
 162
 163        return len;
 164}
 165
 166static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad7152_show_conversion_modes);
 167
 168static ssize_t ad7152_show_ch1_value(struct device *dev,
 169                struct device_attribute *attr,
 170                char *buf)
 171{
 172        struct iio_dev *dev_info = dev_get_drvdata(dev);
 173        struct ad7152_chip_info *chip = dev_info->dev_data;
 174        u8 data[2];
 175
 176        ad7152_i2c_read(chip, AD7152_CH1_DATA_HIGH, data, 2);
 177        return sprintf(buf, "%d\n", ((int)data[0] << 8) | data[1]);
 178}
 179
 180static IIO_DEV_ATTR_CH1_VALUE(ad7152_show_ch1_value);
 181
 182static ssize_t ad7152_show_ch2_value(struct device *dev,
 183                struct device_attribute *attr,
 184                char *buf)
 185{
 186        struct iio_dev *dev_info = dev_get_drvdata(dev);
 187        struct ad7152_chip_info *chip = dev_info->dev_data;
 188        u8 data[2];
 189
 190        ad7152_i2c_read(chip, AD7152_CH2_DATA_HIGH, data, 2);
 191        return sprintf(buf, "%d\n", ((int)data[0] << 8) | data[1]);
 192}
 193
 194static IIO_DEV_ATTR_CH2_VALUE(ad7152_show_ch2_value);
 195
 196static ssize_t ad7152_show_conversion_mode(struct device *dev,
 197                struct device_attribute *attr,
 198                char *buf)
 199{
 200        struct iio_dev *dev_info = dev_get_drvdata(dev);
 201        struct ad7152_chip_info *chip = dev_info->dev_data;
 202
 203        return sprintf(buf, "%s\n", chip->conversion_mode);
 204}
 205
 206static ssize_t ad7152_store_conversion_mode(struct device *dev,
 207                struct device_attribute *attr,
 208                const char *buf,
 209                size_t len)
 210{
 211        struct iio_dev *dev_info = dev_get_drvdata(dev);
 212        struct ad7152_chip_info *chip = dev_info->dev_data;
 213        u8 cfg;
 214        int i;
 215
 216        ad7152_i2c_read(chip, AD7152_CFG, &cfg, 1);
 217
 218        for (i = 0; i < AD7152_MAX_CONV_MODE; i++)
 219                if (strncmp(buf, ad7152_conv_mode_table[i].name,
 220                                strlen(ad7152_conv_mode_table[i].name) - 1) == 0) {
 221                        chip->conversion_mode = ad7152_conv_mode_table[i].name;
 222                        cfg |= 0x18 | ad7152_conv_mode_table[i].reg_cfg;
 223                        ad7152_i2c_write(chip, AD7152_CFG, cfg);
 224                        return len;
 225                }
 226
 227        dev_err(dev, "not supported conversion mode\n");
 228
 229        return -EINVAL;
 230}
 231
 232static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR,
 233                ad7152_show_conversion_mode,
 234                ad7152_store_conversion_mode);
 235
 236static ssize_t ad7152_show_ch1_offset(struct device *dev,
 237                struct device_attribute *attr,
 238                char *buf)
 239{
 240        struct iio_dev *dev_info = dev_get_drvdata(dev);
 241        struct ad7152_chip_info *chip = dev_info->dev_data;
 242
 243        return sprintf(buf, "%d\n", chip->ch1_offset);
 244}
 245
 246static ssize_t ad7152_store_ch1_offset(struct device *dev,
 247                struct device_attribute *attr,
 248                const char *buf,
 249                size_t len)
 250{
 251        struct iio_dev *dev_info = dev_get_drvdata(dev);
 252        struct ad7152_chip_info *chip = dev_info->dev_data;
 253        unsigned long data;
 254        int ret;
 255
 256        ret = strict_strtoul(buf, 10, &data);
 257
 258        if ((!ret) && (data < 0x10000)) {
 259                ad7152_i2c_write(chip, AD7152_CH1_OFFS_HIGH, data >> 8);
 260                ad7152_i2c_write(chip, AD7152_CH1_OFFS_LOW, data);
 261                chip->ch1_offset = data;
 262                return len;
 263        }
 264
 265        return -EINVAL;
 266}
 267
 268static IIO_DEV_ATTR_CH1_OFFSET(S_IRUGO | S_IWUSR,
 269                ad7152_show_ch1_offset,
 270                ad7152_store_ch1_offset);
 271
 272static ssize_t ad7152_show_ch2_offset(struct device *dev,
 273                struct device_attribute *attr,
 274                char *buf)
 275{
 276        struct iio_dev *dev_info = dev_get_drvdata(dev);
 277        struct ad7152_chip_info *chip = dev_info->dev_data;
 278
 279        return sprintf(buf, "%d\n", chip->ch2_offset);
 280}
 281
 282static ssize_t ad7152_store_ch2_offset(struct device *dev,
 283                struct device_attribute *attr,
 284                const char *buf,
 285                size_t len)
 286{
 287        struct iio_dev *dev_info = dev_get_drvdata(dev);
 288        struct ad7152_chip_info *chip = dev_info->dev_data;
 289        unsigned long data;
 290        int ret;
 291
 292        ret = strict_strtoul(buf, 10, &data);
 293
 294        if ((!ret) && (data < 0x10000)) {
 295                ad7152_i2c_write(chip, AD7152_CH2_OFFS_HIGH, data >> 8);
 296                ad7152_i2c_write(chip, AD7152_CH2_OFFS_LOW, data);
 297                chip->ch2_offset = data;
 298                return len;
 299        }
 300
 301        return -EINVAL;
 302}
 303
 304static IIO_DEV_ATTR_CH2_OFFSET(S_IRUGO | S_IWUSR,
 305                ad7152_show_ch2_offset,
 306                ad7152_store_ch2_offset);
 307
 308static ssize_t ad7152_show_ch1_gain(struct device *dev,
 309                struct device_attribute *attr,
 310                char *buf)
 311{
 312        struct iio_dev *dev_info = dev_get_drvdata(dev);
 313        struct ad7152_chip_info *chip = dev_info->dev_data;
 314
 315        return sprintf(buf, "%d\n", chip->ch1_gain);
 316}
 317
 318static ssize_t ad7152_store_ch1_gain(struct device *dev,
 319                struct device_attribute *attr,
 320                const char *buf,
 321                size_t len)
 322{
 323        struct iio_dev *dev_info = dev_get_drvdata(dev);
 324        struct ad7152_chip_info *chip = dev_info->dev_data;
 325        unsigned long data;
 326        int ret;
 327
 328        ret = strict_strtoul(buf, 10, &data);
 329
 330        if ((!ret) && (data < 0x10000)) {
 331                ad7152_i2c_write(chip, AD7152_CH1_GAIN_HIGH, data >> 8);
 332                ad7152_i2c_write(chip, AD7152_CH1_GAIN_LOW, data);
 333                chip->ch1_gain = data;
 334                return len;
 335        }
 336
 337        return -EINVAL;
 338}
 339
 340static IIO_DEV_ATTR_CH1_GAIN(S_IRUGO | S_IWUSR,
 341                ad7152_show_ch1_gain,
 342                ad7152_store_ch1_gain);
 343
 344static ssize_t ad7152_show_ch2_gain(struct device *dev,
 345                struct device_attribute *attr,
 346                char *buf)
 347{
 348        struct iio_dev *dev_info = dev_get_drvdata(dev);
 349        struct ad7152_chip_info *chip = dev_info->dev_data;
 350
 351        return sprintf(buf, "%d\n", chip->ch2_gain);
 352}
 353
 354static ssize_t ad7152_store_ch2_gain(struct device *dev,
 355                struct device_attribute *attr,
 356                const char *buf,
 357                size_t len)
 358{
 359        struct iio_dev *dev_info = dev_get_drvdata(dev);
 360        struct ad7152_chip_info *chip = dev_info->dev_data;
 361        unsigned long data;
 362        int ret;
 363
 364        ret = strict_strtoul(buf, 10, &data);
 365
 366        if ((!ret) && (data < 0x10000)) {
 367                ad7152_i2c_write(chip, AD7152_CH2_GAIN_HIGH, data >> 8);
 368                ad7152_i2c_write(chip, AD7152_CH2_GAIN_LOW, data);
 369                chip->ch2_gain = data;
 370                return len;
 371        }
 372
 373        return -EINVAL;
 374}
 375
 376static IIO_DEV_ATTR_CH2_GAIN(S_IRUGO | S_IWUSR,
 377                ad7152_show_ch2_gain,
 378                ad7152_store_ch2_gain);
 379
 380static ssize_t ad7152_show_ch1_setup(struct device *dev,
 381                struct device_attribute *attr,
 382                char *buf)
 383{
 384        struct iio_dev *dev_info = dev_get_drvdata(dev);
 385        struct ad7152_chip_info *chip = dev_info->dev_data;
 386
 387        return sprintf(buf, "0x%02x\n", chip->ch1_setup);
 388}
 389
 390static ssize_t ad7152_store_ch1_setup(struct device *dev,
 391                struct device_attribute *attr,
 392                const char *buf,
 393                size_t len)
 394{
 395        struct iio_dev *dev_info = dev_get_drvdata(dev);
 396        struct ad7152_chip_info *chip = dev_info->dev_data;
 397        unsigned long data;
 398        int ret;
 399
 400        ret = strict_strtoul(buf, 10, &data);
 401
 402        if ((!ret) && (data < 0x100)) {
 403                ad7152_i2c_write(chip, AD7152_CH1_SETUP, data);
 404                chip->ch1_setup = data;
 405                return len;
 406        }
 407
 408        return -EINVAL;
 409}
 410
 411static IIO_DEV_ATTR_CH1_SETUP(S_IRUGO | S_IWUSR,
 412                ad7152_show_ch1_setup,
 413                ad7152_store_ch1_setup);
 414
 415static ssize_t ad7152_show_ch2_setup(struct device *dev,
 416                struct device_attribute *attr,
 417                char *buf)
 418{
 419        struct iio_dev *dev_info = dev_get_drvdata(dev);
 420        struct ad7152_chip_info *chip = dev_info->dev_data;
 421
 422        return sprintf(buf, "0x%02x\n", chip->ch2_setup);
 423}
 424
 425static ssize_t ad7152_store_ch2_setup(struct device *dev,
 426                struct device_attribute *attr,
 427                const char *buf,
 428                size_t len)
 429{
 430        struct iio_dev *dev_info = dev_get_drvdata(dev);
 431        struct ad7152_chip_info *chip = dev_info->dev_data;
 432        unsigned long data;
 433        int ret;
 434
 435        ret = strict_strtoul(buf, 10, &data);
 436
 437        if ((!ret) && (data < 0x100)) {
 438                ad7152_i2c_write(chip, AD7152_CH2_SETUP, data);
 439                chip->ch2_setup = data;
 440                return len;
 441        }
 442
 443        return -EINVAL;
 444}
 445
 446static IIO_DEV_ATTR_CH2_SETUP(S_IRUGO | S_IWUSR,
 447                ad7152_show_ch2_setup,
 448                ad7152_store_ch2_setup);
 449
 450static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
 451                struct device_attribute *attr,
 452                char *buf)
 453{
 454        struct iio_dev *dev_info = dev_get_drvdata(dev);
 455        struct ad7152_chip_info *chip = dev_info->dev_data;
 456
 457        return sprintf(buf, "0x%02x\n", chip->filter_rate_setup);
 458}
 459
 460static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
 461                struct device_attribute *attr,
 462                const char *buf,
 463                size_t len)
 464{
 465        struct iio_dev *dev_info = dev_get_drvdata(dev);
 466        struct ad7152_chip_info *chip = dev_info->dev_data;
 467        unsigned long data;
 468        int ret;
 469
 470        ret = strict_strtoul(buf, 10, &data);
 471
 472        if ((!ret) && (data < 0x100)) {
 473                ad7152_i2c_write(chip, AD7152_CFG2, data);
 474                chip->filter_rate_setup = data;
 475                return len;
 476        }
 477
 478        return -EINVAL;
 479}
 480
 481static IIO_DEV_ATTR_FILTER_RATE_SETUP(S_IRUGO | S_IWUSR,
 482                ad7152_show_filter_rate_setup,
 483                ad7152_store_filter_rate_setup);
 484
 485static ssize_t ad7152_show_name(struct device *dev,
 486                struct device_attribute *attr,
 487                char *buf)
 488{
 489        struct iio_dev *dev_info = dev_get_drvdata(dev);
 490        struct ad7152_chip_info *chip = dev_info->dev_data;
 491        return sprintf(buf, "%s\n", chip->name);
 492}
 493
 494static IIO_DEVICE_ATTR(name, S_IRUGO, ad7152_show_name, NULL, 0);
 495
 496static struct attribute *ad7152_attributes[] = {
 497        &iio_dev_attr_available_conversion_modes.dev_attr.attr,
 498        &iio_dev_attr_conversion_mode.dev_attr.attr,
 499        &iio_dev_attr_ch1_gain.dev_attr.attr,
 500        &iio_dev_attr_ch2_gain.dev_attr.attr,
 501        &iio_dev_attr_ch1_offset.dev_attr.attr,
 502        &iio_dev_attr_ch2_offset.dev_attr.attr,
 503        &iio_dev_attr_ch1_value.dev_attr.attr,
 504        &iio_dev_attr_ch2_value.dev_attr.attr,
 505        &iio_dev_attr_ch1_setup.dev_attr.attr,
 506        &iio_dev_attr_ch2_setup.dev_attr.attr,
 507        &iio_dev_attr_filter_rate_setup.dev_attr.attr,
 508        &iio_dev_attr_name.dev_attr.attr,
 509        NULL,
 510};
 511
 512static const struct attribute_group ad7152_attribute_group = {
 513        .attrs = ad7152_attributes,
 514};
 515
 516/*
 517 * device probe and remove
 518 */
 519
 520static int __devinit ad7152_probe(struct i2c_client *client,
 521                const struct i2c_device_id *id)
 522{
 523        int ret = 0;
 524        struct ad7152_chip_info *chip = kzalloc(sizeof(*chip), GFP_KERNEL);
 525        if (chip == NULL) {
 526                ret = -ENOMEM;
 527                goto error_ret;
 528        }
 529
 530        /* this is only used for device removal purposes */
 531        i2c_set_clientdata(client, chip);
 532
 533        chip->client = client;
 534        chip->name = id->name;
 535
 536        chip->indio_dev = iio_allocate_device();
 537        if (chip->indio_dev == NULL) {
 538                ret = -ENOMEM;
 539                goto error_free_chip;
 540        }
 541
 542        /* Echipabilish that the iio_dev is a child of the i2c device */
 543        chip->indio_dev->dev.parent = &client->dev;
 544        chip->indio_dev->attrs = &ad7152_attribute_group;
 545        chip->indio_dev->dev_data = (void *)(chip);
 546        chip->indio_dev->driver_module = THIS_MODULE;
 547        chip->indio_dev->modes = INDIO_DIRECT_MODE;
 548
 549        ret = iio_device_register(chip->indio_dev);
 550        if (ret)
 551                goto error_free_dev;
 552
 553        dev_err(&client->dev, "%s capacitive sensor registered\n", id->name);
 554
 555        return 0;
 556
 557error_free_dev:
 558        iio_free_device(chip->indio_dev);
 559error_free_chip:
 560        kfree(chip);
 561error_ret:
 562        return ret;
 563}
 564
 565static int __devexit ad7152_remove(struct i2c_client *client)
 566{
 567        struct ad7152_chip_info *chip = i2c_get_clientdata(client);
 568        struct iio_dev *indio_dev = chip->indio_dev;
 569
 570        if (client->irq && gpio_is_valid(irq_to_gpio(client->irq)) > 0)
 571                iio_unregister_interrupt_line(indio_dev, 0);
 572        iio_device_unregister(indio_dev);
 573        kfree(chip);
 574
 575        return 0;
 576}
 577
 578static const struct i2c_device_id ad7152_id[] = {
 579        { "ad7152", 0 },
 580        { "ad7153", 0 },
 581        {}
 582};
 583
 584MODULE_DEVICE_TABLE(i2c, ad7152_id);
 585
 586static struct i2c_driver ad7152_driver = {
 587        .driver = {
 588                .name = "ad7152",
 589        },
 590        .probe = ad7152_probe,
 591        .remove = __devexit_p(ad7152_remove),
 592        .id_table = ad7152_id,
 593};
 594
 595static __init int ad7152_init(void)
 596{
 597        return i2c_add_driver(&ad7152_driver);
 598}
 599
 600static __exit void ad7152_exit(void)
 601{
 602        i2c_del_driver(&ad7152_driver);
 603}
 604
 605MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
 606MODULE_DESCRIPTION("Analog Devices ad7152/3 capacitive sensor driver");
 607MODULE_LICENSE("GPL v2");
 608
 609module_init(ad7152_init);
 610module_exit(ad7152_exit);
 611