linux/drivers/staging/iio/adc/ad7291.c
<<
>>
Prefs
   1/*
   2 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
   3 *
   4 * Copyright 2010-2011 Analog Devices Inc.
   5 *
   6 * Licensed under the GPL-2 or later.
   7 */
   8
   9#include <linux/interrupt.h>
  10#include <linux/device.h>
  11#include <linux/kernel.h>
  12#include <linux/slab.h>
  13#include <linux/sysfs.h>
  14#include <linux/i2c.h>
  15#include <linux/module.h>
  16#include <linux/mutex.h>
  17#include <linux/regulator/consumer.h>
  18#include <linux/err.h>
  19
  20#include <linux/iio/iio.h>
  21#include <linux/iio/sysfs.h>
  22#include <linux/iio/events.h>
  23
  24/*
  25 * Simplified handling
  26 *
  27 * If no events enabled - single polled channel read
  28 * If event enabled direct reads disable unless channel
  29 * is in the read mask.
  30 *
  31 * The noise-delayed bit as per datasheet suggestion is always enabled.
  32 *
  33 */
  34
  35/*
  36 * AD7291 registers definition
  37 */
  38#define AD7291_COMMAND                  0x00
  39#define AD7291_VOLTAGE                  0x01
  40#define AD7291_T_SENSE                  0x02
  41#define AD7291_T_AVERAGE                0x03
  42#define AD7291_CH0_DATA_HIGH            0x04
  43#define AD7291_CH0_DATA_LOW             0x05
  44#define AD7291_CH0_HYST                 0x06
  45#define AD7291_CH1_DATA_HIGH            0x07
  46#define AD7291_CH1_DATA_LOW             0x08
  47#define AD7291_CH1_HYST                 0x09
  48#define AD7291_CH2_DATA_HIGH            0x0A
  49#define AD7291_CH2_DATA_LOW             0x0B
  50#define AD7291_CH2_HYST                 0x0C
  51#define AD7291_CH3_DATA_HIGH            0x0D
  52#define AD7291_CH3_DATA_LOW             0x0E
  53#define AD7291_CH3_HYST                 0x0F
  54#define AD7291_CH4_DATA_HIGH            0x10
  55#define AD7291_CH4_DATA_LOW             0x11
  56#define AD7291_CH4_HYST                 0x12
  57#define AD7291_CH5_DATA_HIGH            0x13
  58#define AD7291_CH5_DATA_LOW             0x14
  59#define AD7291_CH5_HYST                 0x15
  60#define AD7291_CH6_DATA_HIGH            0x16
  61#define AD7291_CH6_DATA_LOW             0x17
  62#define AD7291_CH6_HYST                 0x18
  63#define AD7291_CH7_DATA_HIGH            0x19
  64#define AD7291_CH7_DATA_LOW             0x1A
  65#define AD7291_CH7_HYST                 0x2B
  66#define AD7291_T_SENSE_HIGH             0x1C
  67#define AD7291_T_SENSE_LOW              0x1D
  68#define AD7291_T_SENSE_HYST             0x1E
  69#define AD7291_VOLTAGE_ALERT_STATUS     0x1F
  70#define AD7291_T_ALERT_STATUS           0x20
  71
  72#define AD7291_VOLTAGE_LIMIT_COUNT      8
  73
  74
  75/*
  76 * AD7291 command
  77 */
  78#define AD7291_AUTOCYCLE                (1 << 0)
  79#define AD7291_RESET                    (1 << 1)
  80#define AD7291_ALERT_CLEAR              (1 << 2)
  81#define AD7291_ALERT_POLARITY           (1 << 3)
  82#define AD7291_EXT_REF                  (1 << 4)
  83#define AD7291_NOISE_DELAY              (1 << 5)
  84#define AD7291_T_SENSE_MASK             (1 << 7)
  85#define AD7291_VOLTAGE_MASK             0xFF00
  86#define AD7291_VOLTAGE_OFFSET           0x8
  87
  88/*
  89 * AD7291 value masks
  90 */
  91#define AD7291_CHANNEL_MASK             0xF000
  92#define AD7291_BITS                     12
  93#define AD7291_VALUE_MASK               0xFFF
  94#define AD7291_T_VALUE_SIGN             0x400
  95#define AD7291_T_VALUE_FLOAT_OFFSET     2
  96#define AD7291_T_VALUE_FLOAT_MASK       0x2
  97
  98#define AD7291_BITS                     12
  99
 100struct ad7291_chip_info {
 101        struct i2c_client       *client;
 102        struct regulator        *reg;
 103        u16                     int_vref_mv;
 104        u16                     command;
 105        u16                     c_mask; /* Active voltage channels for events */
 106        struct mutex            state_lock;
 107};
 108
 109static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
 110{
 111        struct i2c_client *client = chip->client;
 112        int ret = 0;
 113
 114        ret = i2c_smbus_read_word_data(client, reg);
 115        if (ret < 0) {
 116                dev_err(&client->dev, "I2C read error\n");
 117                return ret;
 118        }
 119
 120        *data = swab16((u16)ret);
 121
 122        return 0;
 123}
 124
 125static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
 126{
 127        return i2c_smbus_write_word_data(chip->client, reg, swab16(data));
 128}
 129
 130static ssize_t ad7291_store_reset(struct device *dev,
 131                struct device_attribute *attr,
 132                const char *buf,
 133                size_t len)
 134{
 135        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 136        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 137
 138        return ad7291_i2c_write(chip, AD7291_COMMAND,
 139                                chip->command | AD7291_RESET);
 140}
 141
 142static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, ad7291_store_reset, 0);
 143
 144static struct attribute *ad7291_attributes[] = {
 145        &iio_dev_attr_reset.dev_attr.attr,
 146        NULL,
 147};
 148
 149static const struct attribute_group ad7291_attribute_group = {
 150        .attrs = ad7291_attributes,
 151};
 152
 153static irqreturn_t ad7291_event_handler(int irq, void *private)
 154{
 155        struct iio_dev *indio_dev = private;
 156        struct ad7291_chip_info *chip = iio_priv(private);
 157        u16 t_status, v_status;
 158        u16 command;
 159        int i;
 160        s64 timestamp = iio_get_time_ns();
 161
 162        if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
 163                return IRQ_HANDLED;
 164
 165        if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
 166                return IRQ_HANDLED;
 167
 168        if (!(t_status || v_status))
 169                return IRQ_HANDLED;
 170
 171        command = chip->command | AD7291_ALERT_CLEAR;
 172        ad7291_i2c_write(chip, AD7291_COMMAND, command);
 173
 174        command = chip->command & ~AD7291_ALERT_CLEAR;
 175        ad7291_i2c_write(chip, AD7291_COMMAND, command);
 176
 177        /* For now treat t_sense and t_sense_average the same */
 178        if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
 179                iio_push_event(indio_dev,
 180                               IIO_UNMOD_EVENT_CODE(IIO_TEMP,
 181                                                    0,
 182                                                    IIO_EV_TYPE_THRESH,
 183                                                    IIO_EV_DIR_FALLING),
 184                               timestamp);
 185        if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
 186                iio_push_event(indio_dev,
 187                               IIO_UNMOD_EVENT_CODE(IIO_TEMP,
 188                                                    0,
 189                                                    IIO_EV_TYPE_THRESH,
 190                                                    IIO_EV_DIR_RISING),
 191                               timestamp);
 192
 193        for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
 194                if (v_status & (1 << i))
 195                        iio_push_event(indio_dev,
 196                                       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
 197                                                            i/2,
 198                                                            IIO_EV_TYPE_THRESH,
 199                                                            IIO_EV_DIR_FALLING),
 200                                       timestamp);
 201                if (v_status & (1 << (i + 1)))
 202                        iio_push_event(indio_dev,
 203                                       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
 204                                                            i/2,
 205                                                            IIO_EV_TYPE_THRESH,
 206                                                            IIO_EV_DIR_RISING),
 207                                       timestamp);
 208        }
 209
 210        return IRQ_HANDLED;
 211}
 212
 213static inline ssize_t ad7291_show_hyst(struct device *dev,
 214                struct device_attribute *attr,
 215                char *buf)
 216{
 217        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 218        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 219        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 220        u16 data;
 221        int ret;
 222
 223        ret = ad7291_i2c_read(chip, this_attr->address, &data);
 224        if (ret < 0)
 225                return ret;
 226
 227        return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK);
 228}
 229
 230static inline ssize_t ad7291_set_hyst(struct device *dev,
 231                                      struct device_attribute *attr,
 232                                      const char *buf,
 233                                      size_t len)
 234{
 235        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 236        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 237        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 238        u16 data;
 239        int ret;
 240
 241        ret = kstrtou16(buf, 10, &data);
 242
 243        if (ret < 0)
 244                return ret;
 245        if (data > AD7291_VALUE_MASK)
 246                return -EINVAL;
 247
 248        ret = ad7291_i2c_write(chip, this_attr->address, data);
 249        if (ret < 0)
 250                return ret;
 251
 252        return len;
 253}
 254
 255static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
 256                       S_IRUGO | S_IWUSR,
 257                       ad7291_show_hyst, ad7291_set_hyst,
 258                       AD7291_T_SENSE_HYST);
 259static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
 260                       S_IRUGO | S_IWUSR,
 261                       ad7291_show_hyst, ad7291_set_hyst, AD7291_CH0_HYST);
 262static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
 263                       S_IRUGO | S_IWUSR,
 264                       ad7291_show_hyst, ad7291_set_hyst, AD7291_CH1_HYST);
 265static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
 266                       S_IRUGO | S_IWUSR,
 267                       ad7291_show_hyst, ad7291_set_hyst, AD7291_CH2_HYST);
 268static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
 269                       S_IRUGO | S_IWUSR,
 270                       ad7291_show_hyst, ad7291_set_hyst, AD7291_CH3_HYST);
 271static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
 272                       S_IRUGO | S_IWUSR,
 273                       ad7291_show_hyst, ad7291_set_hyst, AD7291_CH4_HYST);
 274static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
 275                       S_IRUGO | S_IWUSR,
 276                       ad7291_show_hyst, ad7291_set_hyst, AD7291_CH5_HYST);
 277static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
 278                       S_IRUGO | S_IWUSR,
 279                       ad7291_show_hyst, ad7291_set_hyst, AD7291_CH6_HYST);
 280static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
 281                       S_IRUGO | S_IWUSR,
 282                       ad7291_show_hyst, ad7291_set_hyst, AD7291_CH7_HYST);
 283
 284static struct attribute *ad7291_event_attributes[] = {
 285        &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
 286        &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
 287        &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
 288        &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
 289        &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
 290        &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
 291        &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
 292        &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
 293        &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
 294        NULL,
 295};
 296
 297/* high / low */
 298static u8 ad7291_limit_regs[9][2] = {
 299        { AD7291_CH0_DATA_HIGH, AD7291_CH0_DATA_LOW },
 300        { AD7291_CH1_DATA_HIGH, AD7291_CH1_DATA_LOW },
 301        { AD7291_CH2_DATA_HIGH, AD7291_CH2_DATA_LOW },
 302        { AD7291_CH3_DATA_HIGH, AD7291_CH3_DATA_LOW }, /* FIXME: ? */
 303        { AD7291_CH4_DATA_HIGH, AD7291_CH4_DATA_LOW },
 304        { AD7291_CH5_DATA_HIGH, AD7291_CH5_DATA_LOW },
 305        { AD7291_CH6_DATA_HIGH, AD7291_CH6_DATA_LOW },
 306        { AD7291_CH7_DATA_HIGH, AD7291_CH7_DATA_LOW },
 307        /* temp */
 308        { AD7291_T_SENSE_HIGH, AD7291_T_SENSE_LOW },
 309};
 310
 311static int ad7291_read_event_value(struct iio_dev *indio_dev,
 312                                   u64 event_code,
 313                                   int *val)
 314{
 315        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 316
 317        int ret;
 318        u8 reg;
 319        u16 uval;
 320        s16 signval;
 321
 322        switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
 323        case IIO_VOLTAGE:
 324                reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)]
 325                        [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
 326                           IIO_EV_DIR_RISING)];
 327
 328                ret = ad7291_i2c_read(chip, reg, &uval);
 329                if (ret < 0)
 330                        return ret;
 331                *val = uval & AD7291_VALUE_MASK;
 332                return 0;
 333
 334        case IIO_TEMP:
 335                reg = ad7291_limit_regs[8]
 336                        [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
 337                           IIO_EV_DIR_RISING)];
 338
 339                ret = ad7291_i2c_read(chip, reg, &signval);
 340                if (ret < 0)
 341                        return ret;
 342                signval = (s16)((signval & AD7291_VALUE_MASK) << 4) >> 4;
 343                *val = signval;
 344                return 0;
 345        default:
 346                return -EINVAL;
 347        };
 348}
 349
 350static int ad7291_write_event_value(struct iio_dev *indio_dev,
 351                                    u64 event_code,
 352                                    int val)
 353{
 354        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 355        u8 reg;
 356        s16 signval;
 357
 358        switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
 359        case IIO_VOLTAGE:
 360                if (val > AD7291_VALUE_MASK || val < 0)
 361                        return -EINVAL;
 362                reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)]
 363                        [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
 364                           IIO_EV_DIR_RISING)];
 365                return ad7291_i2c_write(chip, reg, val);
 366        case IIO_TEMP:
 367                if (val > 2047 || val < -2048)
 368                        return -EINVAL;
 369                reg = ad7291_limit_regs[8]
 370                        [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
 371                           IIO_EV_DIR_RISING)];
 372                signval = val;
 373                return ad7291_i2c_write(chip, reg, *(u16 *)&signval);
 374        default:
 375                return -EINVAL;
 376        };
 377}
 378
 379static int ad7291_read_event_config(struct iio_dev *indio_dev,
 380                                    u64 event_code)
 381{
 382        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 383        /* To be enabled the channel must simply be on. If any are enabled
 384           we are in continuous sampling mode */
 385
 386        switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
 387        case IIO_VOLTAGE:
 388                if (chip->c_mask &
 389                    (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))
 390                        return 1;
 391                else
 392                        return 0;
 393        case IIO_TEMP:
 394                /* always on */
 395                return 1;
 396        default:
 397                return -EINVAL;
 398        }
 399
 400}
 401
 402static int ad7291_write_event_config(struct iio_dev *indio_dev,
 403                                     u64 event_code,
 404                                     int state)
 405{
 406        int ret = 0;
 407        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 408        u16 regval;
 409
 410        mutex_lock(&chip->state_lock);
 411        regval = chip->command;
 412        /*
 413         * To be enabled the channel must simply be on. If any are enabled
 414         * use continuous sampling mode.
 415         * Possible to disable temp as well but that makes single read tricky.
 416         */
 417
 418        switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
 419        case IIO_VOLTAGE:
 420                if ((!state) && (chip->c_mask & (1 << (15 -
 421                                IIO_EVENT_CODE_EXTRACT_CHAN(event_code)))))
 422                        chip->c_mask &= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
 423                                                        (event_code)));
 424                else if (state && (!(chip->c_mask & (1 << (15 -
 425                                IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))))
 426                        chip->c_mask |= (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
 427                                                        (event_code)));
 428                else
 429                        break;
 430
 431                regval &= ~AD7291_AUTOCYCLE;
 432                regval |= chip->c_mask;
 433                if (chip->c_mask) /* Enable autocycle? */
 434                        regval |= AD7291_AUTOCYCLE;
 435
 436                ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
 437                if (ret < 0)
 438                        goto error_ret;
 439
 440                chip->command = regval;
 441                break;
 442        default:
 443                ret = -EINVAL;
 444        }
 445
 446error_ret:
 447        mutex_unlock(&chip->state_lock);
 448        return ret;
 449}
 450
 451static int ad7291_read_raw(struct iio_dev *indio_dev,
 452                           struct iio_chan_spec const *chan,
 453                           int *val,
 454                           int *val2,
 455                           long mask)
 456{
 457        int ret;
 458        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 459        unsigned int scale_uv;
 460        u16 regval;
 461        s16 signval;
 462
 463        switch (mask) {
 464        case IIO_CHAN_INFO_RAW:
 465                switch (chan->type) {
 466                case IIO_VOLTAGE:
 467                        mutex_lock(&chip->state_lock);
 468                        /* If in autocycle mode drop through */
 469                        if (chip->command & AD7291_AUTOCYCLE) {
 470                                mutex_unlock(&chip->state_lock);
 471                                return -EBUSY;
 472                        }
 473                        /* Enable this channel alone */
 474                        regval = chip->command & (~AD7291_VOLTAGE_MASK);
 475                        regval |= 1 << (15 - chan->channel);
 476                        ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
 477                        if (ret < 0) {
 478                                mutex_unlock(&chip->state_lock);
 479                                return ret;
 480                        }
 481                        /* Read voltage */
 482                        ret = i2c_smbus_read_word_data(chip->client,
 483                                                       AD7291_VOLTAGE);
 484                        if (ret < 0) {
 485                                mutex_unlock(&chip->state_lock);
 486                                return ret;
 487                        }
 488                        *val = swab16((u16)ret) & AD7291_VALUE_MASK;
 489                        mutex_unlock(&chip->state_lock);
 490                        return IIO_VAL_INT;
 491                case IIO_TEMP:
 492                        /* Assumes tsense bit of command register always set */
 493                        ret = i2c_smbus_read_word_data(chip->client,
 494                                                       AD7291_T_SENSE);
 495                        if (ret < 0)
 496                                return ret;
 497                        signval = (s16)((swab16((u16)ret) &
 498                                AD7291_VALUE_MASK) << 4) >> 4;
 499                        *val = signval;
 500                        return IIO_VAL_INT;
 501                default:
 502                        return -EINVAL;
 503                }
 504        case IIO_CHAN_INFO_AVERAGE_RAW:
 505                ret = i2c_smbus_read_word_data(chip->client,
 506                                               AD7291_T_AVERAGE);
 507                        if (ret < 0)
 508                                return ret;
 509                        signval = (s16)((swab16((u16)ret) &
 510                                AD7291_VALUE_MASK) << 4) >> 4;
 511                        *val = signval;
 512                        return IIO_VAL_INT;
 513        case IIO_CHAN_INFO_SCALE:
 514                switch (chan->type) {
 515                case IIO_VOLTAGE:
 516                        scale_uv = (chip->int_vref_mv * 1000) >> AD7291_BITS;
 517                        *val =  scale_uv / 1000;
 518                        *val2 = (scale_uv % 1000) * 1000;
 519                        return IIO_VAL_INT_PLUS_MICRO;
 520                case IIO_TEMP:
 521                        /*
 522                         * One LSB of the ADC corresponds to 0.25 deg C.
 523                         * The temperature reading is in 12-bit twos
 524                         * complement format
 525                         */
 526                        *val = 250;
 527                        return IIO_VAL_INT;
 528                default:
 529                        return -EINVAL;
 530                }
 531        default:
 532                return -EINVAL;
 533        }
 534}
 535
 536#define AD7291_VOLTAGE_CHAN(_chan)                                      \
 537{                                                                       \
 538        .type = IIO_VOLTAGE,                                            \
 539        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
 540        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),           \
 541        .indexed = 1,                                                   \
 542        .channel = _chan,                                               \
 543        .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
 544        IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)              \
 545}
 546
 547static const struct iio_chan_spec ad7291_channels[] = {
 548        AD7291_VOLTAGE_CHAN(0),
 549        AD7291_VOLTAGE_CHAN(1),
 550        AD7291_VOLTAGE_CHAN(2),
 551        AD7291_VOLTAGE_CHAN(3),
 552        AD7291_VOLTAGE_CHAN(4),
 553        AD7291_VOLTAGE_CHAN(5),
 554        AD7291_VOLTAGE_CHAN(6),
 555        AD7291_VOLTAGE_CHAN(7),
 556        {
 557                .type = IIO_TEMP,
 558                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
 559                                BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
 560                                BIT(IIO_CHAN_INFO_SCALE),
 561                .indexed = 1,
 562                .channel = 0,
 563                .event_mask =
 564                IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
 565                IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
 566        }
 567};
 568
 569static struct attribute_group ad7291_event_attribute_group = {
 570        .attrs = ad7291_event_attributes,
 571};
 572
 573static const struct iio_info ad7291_info = {
 574        .attrs = &ad7291_attribute_group,
 575        .read_raw = &ad7291_read_raw,
 576        .read_event_config = &ad7291_read_event_config,
 577        .write_event_config = &ad7291_write_event_config,
 578        .read_event_value = &ad7291_read_event_value,
 579        .write_event_value = &ad7291_write_event_value,
 580        .event_attrs = &ad7291_event_attribute_group,
 581};
 582
 583static int ad7291_probe(struct i2c_client *client,
 584                const struct i2c_device_id *id)
 585{
 586        struct ad7291_chip_info *chip;
 587        struct iio_dev *indio_dev;
 588        int ret = 0, voltage_uv = 0;
 589
 590        indio_dev = iio_device_alloc(sizeof(*chip));
 591        if (indio_dev == NULL) {
 592                ret = -ENOMEM;
 593                goto error_ret;
 594        }
 595        chip = iio_priv(indio_dev);
 596
 597        chip->reg = regulator_get(&client->dev, "vcc");
 598        if (!IS_ERR(chip->reg)) {
 599                ret = regulator_enable(chip->reg);
 600                if (ret)
 601                        goto error_put_reg;
 602                voltage_uv = regulator_get_voltage(chip->reg);
 603        }
 604
 605        mutex_init(&chip->state_lock);
 606        /* this is only used for device removal purposes */
 607        i2c_set_clientdata(client, indio_dev);
 608
 609        chip->client = client;
 610
 611        chip->command = AD7291_NOISE_DELAY |
 612                        AD7291_T_SENSE_MASK | /* Tsense always enabled */
 613                        AD7291_ALERT_POLARITY; /* set irq polarity low level */
 614
 615        if (voltage_uv) {
 616                chip->int_vref_mv = voltage_uv / 1000;
 617                chip->command |= AD7291_EXT_REF;
 618        } else {
 619                chip->int_vref_mv = 2500; /* Build-in ref */
 620        }
 621
 622        indio_dev->name = id->name;
 623        indio_dev->channels = ad7291_channels;
 624        indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
 625
 626        indio_dev->dev.parent = &client->dev;
 627        indio_dev->info = &ad7291_info;
 628        indio_dev->modes = INDIO_DIRECT_MODE;
 629
 630        ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
 631        if (ret) {
 632                ret = -EIO;
 633                goto error_disable_reg;
 634        }
 635
 636        ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
 637        if (ret) {
 638                ret = -EIO;
 639                goto error_disable_reg;
 640        }
 641
 642        if (client->irq > 0) {
 643                ret = request_threaded_irq(client->irq,
 644                                           NULL,
 645                                           &ad7291_event_handler,
 646                                           IRQF_TRIGGER_LOW | IRQF_ONESHOT,
 647                                           id->name,
 648                                           indio_dev);
 649                if (ret)
 650                        goto error_disable_reg;
 651        }
 652
 653        ret = iio_device_register(indio_dev);
 654        if (ret)
 655                goto error_unreg_irq;
 656
 657        dev_info(&client->dev, "%s ADC registered.\n",
 658                         id->name);
 659
 660        return 0;
 661
 662error_unreg_irq:
 663        if (client->irq)
 664                free_irq(client->irq, indio_dev);
 665error_disable_reg:
 666        if (!IS_ERR(chip->reg))
 667                regulator_disable(chip->reg);
 668error_put_reg:
 669        if (!IS_ERR(chip->reg))
 670                regulator_put(chip->reg);
 671
 672        iio_device_free(indio_dev);
 673error_ret:
 674        return ret;
 675}
 676
 677static int ad7291_remove(struct i2c_client *client)
 678{
 679        struct iio_dev *indio_dev = i2c_get_clientdata(client);
 680        struct ad7291_chip_info *chip = iio_priv(indio_dev);
 681
 682        iio_device_unregister(indio_dev);
 683
 684        if (client->irq)
 685                free_irq(client->irq, indio_dev);
 686
 687        if (!IS_ERR(chip->reg)) {
 688                regulator_disable(chip->reg);
 689                regulator_put(chip->reg);
 690        }
 691
 692        iio_device_free(indio_dev);
 693
 694        return 0;
 695}
 696
 697static const struct i2c_device_id ad7291_id[] = {
 698        { "ad7291", 0 },
 699        {}
 700};
 701
 702MODULE_DEVICE_TABLE(i2c, ad7291_id);
 703
 704static struct i2c_driver ad7291_driver = {
 705        .driver = {
 706                .name = KBUILD_MODNAME,
 707        },
 708        .probe = ad7291_probe,
 709        .remove = ad7291_remove,
 710        .id_table = ad7291_id,
 711};
 712module_i2c_driver(ad7291_driver);
 713
 714MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
 715MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
 716MODULE_LICENSE("GPL v2");
 717