linux/drivers/staging/iio/addac/adt7316.c
<<
>>
Prefs
   1/*
   2 * ADT7316 digital temperature sensor driver supporting ADT7316/7/8 ADT7516/7/9
   3 *
   4 *
   5 * Copyright 2010 Analog Devices Inc.
   6 *
   7 * Licensed under the GPL-2 or later.
   8 */
   9
  10#include <linux/interrupt.h>
  11#include <linux/gpio.h>
  12#include <linux/workqueue.h>
  13#include <linux/device.h>
  14#include <linux/kernel.h>
  15#include <linux/slab.h>
  16#include <linux/sysfs.h>
  17#include <linux/list.h>
  18#include <linux/i2c.h>
  19#include <linux/rtc.h>
  20#include <linux/module.h>
  21
  22#include <linux/iio/iio.h>
  23#include <linux/iio/events.h>
  24#include <linux/iio/sysfs.h>
  25#include "adt7316.h"
  26
  27/*
  28 * ADT7316 registers definition
  29 */
  30#define ADT7316_INT_STAT1               0x0
  31#define ADT7316_INT_STAT2               0x1
  32#define ADT7316_LSB_IN_TEMP_VDD         0x3
  33#define ADT7316_LSB_IN_TEMP_MASK        0x3
  34#define ADT7316_LSB_VDD_MASK            0xC
  35#define ADT7316_LSB_VDD_OFFSET          2
  36#define ADT7316_LSB_EX_TEMP_AIN         0x4
  37#define ADT7316_LSB_EX_TEMP_MASK        0x3
  38#define ADT7516_LSB_AIN_SHIFT           2
  39#define ADT7316_AD_MSB_DATA_BASE        0x6
  40#define ADT7316_AD_MSB_DATA_REGS        3
  41#define ADT7516_AD_MSB_DATA_REGS        6
  42#define ADT7316_MSB_VDD                 0x6
  43#define ADT7316_MSB_IN_TEMP             0x7
  44#define ADT7316_MSB_EX_TEMP             0x8
  45#define ADT7516_MSB_AIN1                0x8
  46#define ADT7516_MSB_AIN2                0x9
  47#define ADT7516_MSB_AIN3                0xA
  48#define ADT7516_MSB_AIN4                0xB
  49#define ADT7316_DA_DATA_BASE            0x10
  50#define ADT7316_DA_MSB_DATA_REGS        4
  51#define ADT7316_LSB_DAC_A               0x10
  52#define ADT7316_MSB_DAC_A               0x11
  53#define ADT7316_LSB_DAC_B               0x12
  54#define ADT7316_MSB_DAC_B               0x13
  55#define ADT7316_LSB_DAC_C               0x14
  56#define ADT7316_MSB_DAC_C               0x15
  57#define ADT7316_LSB_DAC_D               0x16
  58#define ADT7316_MSB_DAC_D               0x17
  59#define ADT7316_CONFIG1                 0x18
  60#define ADT7316_CONFIG2                 0x19
  61#define ADT7316_CONFIG3                 0x1A
  62#define ADT7316_LDAC_CONFIG             0x1B
  63#define ADT7316_DAC_CONFIG              0x1C
  64#define ADT7316_INT_MASK1               0x1D
  65#define ADT7316_INT_MASK2               0x1E
  66#define ADT7316_IN_TEMP_OFFSET          0x1F
  67#define ADT7316_EX_TEMP_OFFSET          0x20
  68#define ADT7316_IN_ANALOG_TEMP_OFFSET   0x21
  69#define ADT7316_EX_ANALOG_TEMP_OFFSET   0x22
  70#define ADT7316_VDD_HIGH                0x23
  71#define ADT7316_VDD_LOW                 0x24
  72#define ADT7316_IN_TEMP_HIGH            0x25
  73#define ADT7316_IN_TEMP_LOW             0x26
  74#define ADT7316_EX_TEMP_HIGH            0x27
  75#define ADT7316_EX_TEMP_LOW             0x28
  76#define ADT7516_AIN2_HIGH               0x2B
  77#define ADT7516_AIN2_LOW                0x2C
  78#define ADT7516_AIN3_HIGH               0x2D
  79#define ADT7516_AIN3_LOW                0x2E
  80#define ADT7516_AIN4_HIGH               0x2F
  81#define ADT7516_AIN4_LOW                0x30
  82#define ADT7316_DEVICE_ID               0x4D
  83#define ADT7316_MANUFACTURE_ID          0x4E
  84#define ADT7316_DEVICE_REV              0x4F
  85#define ADT7316_SPI_LOCK_STAT           0x7F
  86
  87/*
  88 * ADT7316 config1
  89 */
  90#define ADT7316_EN                      0x1
  91#define ADT7516_SEL_EX_TEMP             0x4
  92#define ADT7516_SEL_AIN1_2_EX_TEMP_MASK 0x6
  93#define ADT7516_SEL_AIN3                0x8
  94#define ADT7316_INT_EN                  0x20
  95#define ADT7316_INT_POLARITY            0x40
  96#define ADT7316_PD                      0x80
  97
  98/*
  99 * ADT7316 config2
 100 */
 101#define ADT7316_AD_SINGLE_CH_MASK       0x3
 102#define ADT7516_AD_SINGLE_CH_MASK       0x7
 103#define ADT7316_AD_SINGLE_CH_VDD        0
 104#define ADT7316_AD_SINGLE_CH_IN         1
 105#define ADT7316_AD_SINGLE_CH_EX         2
 106#define ADT7516_AD_SINGLE_CH_AIN1       2
 107#define ADT7516_AD_SINGLE_CH_AIN2       3
 108#define ADT7516_AD_SINGLE_CH_AIN3       4
 109#define ADT7516_AD_SINGLE_CH_AIN4       5
 110#define ADT7316_AD_SINGLE_CH_MODE       0x10
 111#define ADT7316_DISABLE_AVERAGING       0x20
 112#define ADT7316_EN_SMBUS_TIMEOUT        0x40
 113#define ADT7316_RESET                   0x80
 114
 115/*
 116 * ADT7316 config3
 117 */
 118#define ADT7316_ADCLK_22_5              0x1
 119#define ADT7316_DA_HIGH_RESOLUTION      0x2
 120#define ADT7316_DA_EN_VIA_DAC_LDCA      0x4
 121#define ADT7516_AIN_IN_VREF             0x10
 122#define ADT7316_EN_IN_TEMP_PROP_DACA    0x20
 123#define ADT7316_EN_EX_TEMP_PROP_DACB    0x40
 124
 125/*
 126 * ADT7316 DAC config
 127 */
 128#define ADT7316_DA_2VREF_CH_MASK        0xF
 129#define ADT7316_DA_EN_MODE_MASK         0x30
 130#define ADT7316_DA_EN_MODE_SINGLE       0x00
 131#define ADT7316_DA_EN_MODE_AB_CD        0x10
 132#define ADT7316_DA_EN_MODE_ABCD         0x20
 133#define ADT7316_DA_EN_MODE_LDAC         0x30
 134#define ADT7316_VREF_BYPASS_DAC_AB      0x40
 135#define ADT7316_VREF_BYPASS_DAC_CD      0x80
 136
 137/*
 138 * ADT7316 LDAC config
 139 */
 140#define ADT7316_LDAC_EN_DA_MASK         0xF
 141#define ADT7316_DAC_IN_VREF             0x10
 142#define ADT7516_DAC_AB_IN_VREF          0x10
 143#define ADT7516_DAC_CD_IN_VREF          0x20
 144#define ADT7516_DAC_IN_VREF_OFFSET      4
 145#define ADT7516_DAC_IN_VREF_MASK        0x30
 146
 147/*
 148 * ADT7316 INT_MASK2
 149 */
 150#define ADT7316_INT_MASK2_VDD           0x10
 151
 152/*
 153 * ADT7316 value masks
 154 */
 155#define ADT7316_VALUE_MASK              0xfff
 156#define ADT7316_T_VALUE_SIGN            0x400
 157#define ADT7316_T_VALUE_FLOAT_OFFSET    2
 158#define ADT7316_T_VALUE_FLOAT_MASK      0x2
 159
 160/*
 161 * Chip ID
 162 */
 163#define ID_ADT7316              0x1
 164#define ID_ADT7317              0x2
 165#define ID_ADT7318              0x3
 166#define ID_ADT7516              0x11
 167#define ID_ADT7517              0x12
 168#define ID_ADT7519              0x14
 169
 170#define ID_FAMILY_MASK          0xF0
 171#define ID_ADT73XX              0x0
 172#define ID_ADT75XX              0x10
 173
 174/*
 175 * struct adt7316_chip_info - chip specific information
 176 */
 177
 178struct adt7316_chip_info {
 179        struct adt7316_bus      bus;
 180        u16                     ldac_pin;
 181        u16                     int_mask;       /* 0x2f */
 182        u8                      config1;
 183        u8                      config2;
 184        u8                      config3;
 185        u8                      dac_config;     /* DAC config */
 186        u8                      ldac_config;    /* LDAC config */
 187        u8                      dac_bits;       /* 8, 10, 12 */
 188        u8                      id;             /* chip id */
 189};
 190
 191/*
 192 * Logic interrupt mask for user application to enable
 193 * interrupts.
 194 */
 195#define ADT7316_IN_TEMP_HIGH_INT_MASK   0x1
 196#define ADT7316_IN_TEMP_LOW_INT_MASK    0x2
 197#define ADT7316_EX_TEMP_HIGH_INT_MASK   0x4
 198#define ADT7316_EX_TEMP_LOW_INT_MASK    0x8
 199#define ADT7316_EX_TEMP_FAULT_INT_MASK  0x10
 200#define ADT7516_AIN1_INT_MASK           0x4
 201#define ADT7516_AIN2_INT_MASK           0x20
 202#define ADT7516_AIN3_INT_MASK           0x40
 203#define ADT7516_AIN4_INT_MASK           0x80
 204#define ADT7316_VDD_INT_MASK            0x100
 205#define ADT7316_TEMP_INT_MASK           0x1F
 206#define ADT7516_AIN_INT_MASK            0xE0
 207#define ADT7316_TEMP_AIN_INT_MASK       \
 208        (ADT7316_TEMP_INT_MASK)
 209
 210/*
 211 * struct adt7316_chip_info - chip specific information
 212 */
 213
 214struct adt7316_limit_regs {
 215        u16     data_high;
 216        u16     data_low;
 217};
 218
 219static ssize_t adt7316_show_enabled(struct device *dev,
 220                struct device_attribute *attr,
 221                char *buf)
 222{
 223        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 224        struct adt7316_chip_info *chip = iio_priv(dev_info);
 225
 226        return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_EN));
 227}
 228
 229static ssize_t _adt7316_store_enabled(struct adt7316_chip_info *chip,
 230                int enable)
 231{
 232        u8 config1;
 233        int ret;
 234
 235        if (enable)
 236                config1 = chip->config1 | ADT7316_EN;
 237        else
 238                config1 = chip->config1 & ~ADT7316_EN;
 239
 240        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
 241        if (ret)
 242                return -EIO;
 243
 244        chip->config1 = config1;
 245
 246        return ret;
 247}
 248
 249static ssize_t adt7316_store_enabled(struct device *dev,
 250                struct device_attribute *attr,
 251                const char *buf,
 252                size_t len)
 253{
 254        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 255        struct adt7316_chip_info *chip = iio_priv(dev_info);
 256        int enable;
 257
 258        if (buf[0] == '1')
 259                enable = 1;
 260        else
 261                enable = 0;
 262
 263        if (_adt7316_store_enabled(chip, enable) < 0)
 264                return -EIO;
 265
 266        return len;
 267}
 268
 269static IIO_DEVICE_ATTR(enabled, 0644,
 270                adt7316_show_enabled,
 271                adt7316_store_enabled,
 272                0);
 273
 274static ssize_t adt7316_show_select_ex_temp(struct device *dev,
 275                struct device_attribute *attr,
 276                char *buf)
 277{
 278        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 279        struct adt7316_chip_info *chip = iio_priv(dev_info);
 280
 281        if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
 282                return -EPERM;
 283
 284        return sprintf(buf, "%d\n", !!(chip->config1 & ADT7516_SEL_EX_TEMP));
 285}
 286
 287static ssize_t adt7316_store_select_ex_temp(struct device *dev,
 288                struct device_attribute *attr,
 289                const char *buf,
 290                size_t len)
 291{
 292        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 293        struct adt7316_chip_info *chip = iio_priv(dev_info);
 294        u8 config1;
 295        int ret;
 296
 297        if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
 298                return -EPERM;
 299
 300        config1 = chip->config1 & (~ADT7516_SEL_EX_TEMP);
 301        if (buf[0] == '1')
 302                config1 |= ADT7516_SEL_EX_TEMP;
 303
 304        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
 305        if (ret)
 306                return -EIO;
 307
 308        chip->config1 = config1;
 309
 310        return len;
 311}
 312
 313static IIO_DEVICE_ATTR(select_ex_temp, 0644,
 314                adt7316_show_select_ex_temp,
 315                adt7316_store_select_ex_temp,
 316                0);
 317
 318static ssize_t adt7316_show_mode(struct device *dev,
 319                struct device_attribute *attr,
 320                char *buf)
 321{
 322        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 323        struct adt7316_chip_info *chip = iio_priv(dev_info);
 324
 325        if (chip->config2 & ADT7316_AD_SINGLE_CH_MODE)
 326                return sprintf(buf, "single_channel\n");
 327
 328        return sprintf(buf, "round_robin\n");
 329}
 330
 331static ssize_t adt7316_store_mode(struct device *dev,
 332                struct device_attribute *attr,
 333                const char *buf,
 334                size_t len)
 335{
 336        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 337        struct adt7316_chip_info *chip = iio_priv(dev_info);
 338        u8 config2;
 339        int ret;
 340
 341        config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MODE);
 342        if (!memcmp(buf, "single_channel", 14))
 343                config2 |= ADT7316_AD_SINGLE_CH_MODE;
 344
 345        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
 346        if (ret)
 347                return -EIO;
 348
 349        chip->config2 = config2;
 350
 351        return len;
 352}
 353
 354static IIO_DEVICE_ATTR(mode, 0644,
 355                adt7316_show_mode,
 356                adt7316_store_mode,
 357                0);
 358
 359static ssize_t adt7316_show_all_modes(struct device *dev,
 360                struct device_attribute *attr,
 361                char *buf)
 362{
 363        return sprintf(buf, "single_channel\nround_robin\n");
 364}
 365
 366static IIO_DEVICE_ATTR(all_modes, 0444, adt7316_show_all_modes, NULL, 0);
 367
 368static ssize_t adt7316_show_ad_channel(struct device *dev,
 369                struct device_attribute *attr,
 370                char *buf)
 371{
 372        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 373        struct adt7316_chip_info *chip = iio_priv(dev_info);
 374
 375        if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
 376                return -EPERM;
 377
 378        switch (chip->config2 & ADT7516_AD_SINGLE_CH_MASK) {
 379        case ADT7316_AD_SINGLE_CH_VDD:
 380                return sprintf(buf, "0 - VDD\n");
 381        case ADT7316_AD_SINGLE_CH_IN:
 382                return sprintf(buf, "1 - Internal Temperature\n");
 383        case ADT7316_AD_SINGLE_CH_EX:
 384                if (((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) &&
 385                        (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)
 386                        return sprintf(buf, "2 - AIN1\n");
 387
 388                return sprintf(buf, "2 - External Temperature\n");
 389        case ADT7516_AD_SINGLE_CH_AIN2:
 390                if ((chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)
 391                        return sprintf(buf, "3 - AIN2\n");
 392
 393                return sprintf(buf, "N/A\n");
 394        case ADT7516_AD_SINGLE_CH_AIN3:
 395                if (chip->config1 & ADT7516_SEL_AIN3)
 396                        return sprintf(buf, "4 - AIN3\n");
 397
 398                return sprintf(buf, "N/A\n");
 399        case ADT7516_AD_SINGLE_CH_AIN4:
 400                return sprintf(buf, "5 - AIN4\n");
 401        default:
 402                return sprintf(buf, "N/A\n");
 403        }
 404}
 405
 406static ssize_t adt7316_store_ad_channel(struct device *dev,
 407                struct device_attribute *attr,
 408                const char *buf,
 409                size_t len)
 410{
 411        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 412        struct adt7316_chip_info *chip = iio_priv(dev_info);
 413        u8 config2;
 414        u8 data;
 415        int ret;
 416
 417        if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
 418                return -EPERM;
 419
 420        ret = kstrtou8(buf, 10, &data);
 421        if (ret)
 422                return -EINVAL;
 423
 424        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
 425                if (data > 5)
 426                        return -EINVAL;
 427
 428                config2 = chip->config2 & (~ADT7516_AD_SINGLE_CH_MASK);
 429        } else {
 430                if (data > 2)
 431                        return -EINVAL;
 432
 433                config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MASK);
 434        }
 435
 436        config2 |= data;
 437
 438        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
 439        if (ret)
 440                return -EIO;
 441
 442        chip->config2 = config2;
 443
 444        return len;
 445}
 446
 447static IIO_DEVICE_ATTR(ad_channel, 0644,
 448                adt7316_show_ad_channel,
 449                adt7316_store_ad_channel,
 450                0);
 451
 452static ssize_t adt7316_show_all_ad_channels(struct device *dev,
 453                struct device_attribute *attr,
 454                char *buf)
 455{
 456        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 457        struct adt7316_chip_info *chip = iio_priv(dev_info);
 458
 459        if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
 460                return -EPERM;
 461
 462        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
 463                return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
 464                                "2 - External Temperature or AIN1\n"
 465                                "3 - AIN2\n4 - AIN3\n5 - AIN4\n");
 466        return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
 467                        "2 - External Temperature\n");
 468}
 469
 470static IIO_DEVICE_ATTR(all_ad_channels, 0444,
 471                adt7316_show_all_ad_channels, NULL, 0);
 472
 473static ssize_t adt7316_show_disable_averaging(struct device *dev,
 474                struct device_attribute *attr,
 475                char *buf)
 476{
 477        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 478        struct adt7316_chip_info *chip = iio_priv(dev_info);
 479
 480        return sprintf(buf, "%d\n",
 481                !!(chip->config2 & ADT7316_DISABLE_AVERAGING));
 482}
 483
 484static ssize_t adt7316_store_disable_averaging(struct device *dev,
 485                struct device_attribute *attr,
 486                const char *buf,
 487                size_t len)
 488{
 489        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 490        struct adt7316_chip_info *chip = iio_priv(dev_info);
 491        u8 config2;
 492        int ret;
 493
 494        config2 = chip->config2 & (~ADT7316_DISABLE_AVERAGING);
 495        if (buf[0] == '1')
 496                config2 |= ADT7316_DISABLE_AVERAGING;
 497
 498        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
 499        if (ret)
 500                return -EIO;
 501
 502        chip->config2 = config2;
 503
 504        return len;
 505}
 506
 507static IIO_DEVICE_ATTR(disable_averaging, 0644,
 508                adt7316_show_disable_averaging,
 509                adt7316_store_disable_averaging,
 510                0);
 511
 512static ssize_t adt7316_show_enable_smbus_timeout(struct device *dev,
 513                struct device_attribute *attr,
 514                char *buf)
 515{
 516        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 517        struct adt7316_chip_info *chip = iio_priv(dev_info);
 518
 519        return sprintf(buf, "%d\n",
 520                !!(chip->config2 & ADT7316_EN_SMBUS_TIMEOUT));
 521}
 522
 523static ssize_t adt7316_store_enable_smbus_timeout(struct device *dev,
 524                struct device_attribute *attr,
 525                const char *buf,
 526                size_t len)
 527{
 528        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 529        struct adt7316_chip_info *chip = iio_priv(dev_info);
 530        u8 config2;
 531        int ret;
 532
 533        config2 = chip->config2 & (~ADT7316_EN_SMBUS_TIMEOUT);
 534        if (buf[0] == '1')
 535                config2 |= ADT7316_EN_SMBUS_TIMEOUT;
 536
 537        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
 538        if (ret)
 539                return -EIO;
 540
 541        chip->config2 = config2;
 542
 543        return len;
 544}
 545
 546static IIO_DEVICE_ATTR(enable_smbus_timeout, 0644,
 547                adt7316_show_enable_smbus_timeout,
 548                adt7316_store_enable_smbus_timeout,
 549                0);
 550
 551static ssize_t adt7316_show_powerdown(struct device *dev,
 552                struct device_attribute *attr,
 553                char *buf)
 554{
 555        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 556        struct adt7316_chip_info *chip = iio_priv(dev_info);
 557
 558        return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_PD));
 559}
 560
 561static ssize_t adt7316_store_powerdown(struct device *dev,
 562                struct device_attribute *attr,
 563                const char *buf,
 564                size_t len)
 565{
 566        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 567        struct adt7316_chip_info *chip = iio_priv(dev_info);
 568        u8 config1;
 569        int ret;
 570
 571        config1 = chip->config1 & (~ADT7316_PD);
 572        if (buf[0] == '1')
 573                config1 |= ADT7316_PD;
 574
 575        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
 576        if (ret)
 577                return -EIO;
 578
 579        chip->config1 = config1;
 580
 581        return len;
 582}
 583
 584static IIO_DEVICE_ATTR(powerdown, 0644,
 585                adt7316_show_powerdown,
 586                adt7316_store_powerdown,
 587                0);
 588
 589static ssize_t adt7316_show_fast_ad_clock(struct device *dev,
 590                struct device_attribute *attr,
 591                char *buf)
 592{
 593        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 594        struct adt7316_chip_info *chip = iio_priv(dev_info);
 595
 596        return sprintf(buf, "%d\n", !!(chip->config3 & ADT7316_ADCLK_22_5));
 597}
 598
 599static ssize_t adt7316_store_fast_ad_clock(struct device *dev,
 600                struct device_attribute *attr,
 601                const char *buf,
 602                size_t len)
 603{
 604        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 605        struct adt7316_chip_info *chip = iio_priv(dev_info);
 606        u8 config3;
 607        int ret;
 608
 609        config3 = chip->config3 & (~ADT7316_ADCLK_22_5);
 610        if (buf[0] == '1')
 611                config3 |= ADT7316_ADCLK_22_5;
 612
 613        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
 614        if (ret)
 615                return -EIO;
 616
 617        chip->config3 = config3;
 618
 619        return len;
 620}
 621
 622static IIO_DEVICE_ATTR(fast_ad_clock, 0644,
 623                adt7316_show_fast_ad_clock,
 624                adt7316_store_fast_ad_clock,
 625                0);
 626
 627static ssize_t adt7316_show_da_high_resolution(struct device *dev,
 628                struct device_attribute *attr,
 629                char *buf)
 630{
 631        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 632        struct adt7316_chip_info *chip = iio_priv(dev_info);
 633
 634        if (chip->config3 & ADT7316_DA_HIGH_RESOLUTION) {
 635                if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
 636                        return sprintf(buf, "1 (12 bits)\n");
 637                if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
 638                        return sprintf(buf, "1 (10 bits)\n");
 639        }
 640
 641        return sprintf(buf, "0 (8 bits)\n");
 642}
 643
 644static ssize_t adt7316_store_da_high_resolution(struct device *dev,
 645                struct device_attribute *attr,
 646                const char *buf,
 647                size_t len)
 648{
 649        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 650        struct adt7316_chip_info *chip = iio_priv(dev_info);
 651        u8 config3;
 652        int ret;
 653
 654        chip->dac_bits = 8;
 655
 656        if (buf[0] == '1') {
 657                config3 = chip->config3 | ADT7316_DA_HIGH_RESOLUTION;
 658                if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
 659                        chip->dac_bits = 12;
 660                else if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
 661                        chip->dac_bits = 10;
 662        } else {
 663                config3 = chip->config3 & (~ADT7316_DA_HIGH_RESOLUTION);
 664        }
 665
 666        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
 667        if (ret)
 668                return -EIO;
 669
 670        chip->config3 = config3;
 671
 672        return len;
 673}
 674
 675static IIO_DEVICE_ATTR(da_high_resolution, 0644,
 676                adt7316_show_da_high_resolution,
 677                adt7316_store_da_high_resolution,
 678                0);
 679
 680static ssize_t adt7316_show_AIN_internal_Vref(struct device *dev,
 681                struct device_attribute *attr,
 682                char *buf)
 683{
 684        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 685        struct adt7316_chip_info *chip = iio_priv(dev_info);
 686
 687        if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
 688                return -EPERM;
 689
 690        return sprintf(buf, "%d\n",
 691                !!(chip->config3 & ADT7516_AIN_IN_VREF));
 692}
 693
 694static ssize_t adt7316_store_AIN_internal_Vref(struct device *dev,
 695                struct device_attribute *attr,
 696                const char *buf,
 697                size_t len)
 698{
 699        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 700        struct adt7316_chip_info *chip = iio_priv(dev_info);
 701        u8 config3;
 702        int ret;
 703
 704        if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
 705                return -EPERM;
 706
 707        if (buf[0] != '1')
 708                config3 = chip->config3 & (~ADT7516_AIN_IN_VREF);
 709        else
 710                config3 = chip->config3 | ADT7516_AIN_IN_VREF;
 711
 712        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
 713        if (ret)
 714                return -EIO;
 715
 716        chip->config3 = config3;
 717
 718        return len;
 719}
 720
 721static IIO_DEVICE_ATTR(AIN_internal_Vref, 0644,
 722                adt7316_show_AIN_internal_Vref,
 723                adt7316_store_AIN_internal_Vref,
 724                0);
 725
 726static ssize_t adt7316_show_enable_prop_DACA(struct device *dev,
 727                struct device_attribute *attr,
 728                char *buf)
 729{
 730        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 731        struct adt7316_chip_info *chip = iio_priv(dev_info);
 732
 733        return sprintf(buf, "%d\n",
 734                !!(chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA));
 735}
 736
 737static ssize_t adt7316_store_enable_prop_DACA(struct device *dev,
 738                struct device_attribute *attr,
 739                const char *buf,
 740                size_t len)
 741{
 742        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 743        struct adt7316_chip_info *chip = iio_priv(dev_info);
 744        u8 config3;
 745        int ret;
 746
 747        config3 = chip->config3 & (~ADT7316_EN_IN_TEMP_PROP_DACA);
 748        if (buf[0] == '1')
 749                config3 |= ADT7316_EN_IN_TEMP_PROP_DACA;
 750
 751        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
 752        if (ret)
 753                return -EIO;
 754
 755        chip->config3 = config3;
 756
 757        return len;
 758}
 759
 760static IIO_DEVICE_ATTR(enable_proportion_DACA, 0644,
 761                adt7316_show_enable_prop_DACA,
 762                adt7316_store_enable_prop_DACA,
 763                0);
 764
 765static ssize_t adt7316_show_enable_prop_DACB(struct device *dev,
 766                struct device_attribute *attr,
 767                char *buf)
 768{
 769        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 770        struct adt7316_chip_info *chip = iio_priv(dev_info);
 771
 772        return sprintf(buf, "%d\n",
 773                !!(chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB));
 774}
 775
 776static ssize_t adt7316_store_enable_prop_DACB(struct device *dev,
 777                struct device_attribute *attr,
 778                const char *buf,
 779                size_t len)
 780{
 781        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 782        struct adt7316_chip_info *chip = iio_priv(dev_info);
 783        u8 config3;
 784        int ret;
 785
 786        config3 = chip->config3 & (~ADT7316_EN_EX_TEMP_PROP_DACB);
 787        if (buf[0] == '1')
 788                config3 |= ADT7316_EN_EX_TEMP_PROP_DACB;
 789
 790        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
 791        if (ret)
 792                return -EIO;
 793
 794        chip->config3 = config3;
 795
 796        return len;
 797}
 798
 799static IIO_DEVICE_ATTR(enable_proportion_DACB, 0644,
 800                adt7316_show_enable_prop_DACB,
 801                adt7316_store_enable_prop_DACB,
 802                0);
 803
 804static ssize_t adt7316_show_DAC_2Vref_ch_mask(struct device *dev,
 805                struct device_attribute *attr,
 806                char *buf)
 807{
 808        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 809        struct adt7316_chip_info *chip = iio_priv(dev_info);
 810
 811        return sprintf(buf, "0x%x\n",
 812                chip->dac_config & ADT7316_DA_2VREF_CH_MASK);
 813}
 814
 815static ssize_t adt7316_store_DAC_2Vref_ch_mask(struct device *dev,
 816                struct device_attribute *attr,
 817                const char *buf,
 818                size_t len)
 819{
 820        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 821        struct adt7316_chip_info *chip = iio_priv(dev_info);
 822        u8 dac_config;
 823        u8 data;
 824        int ret;
 825
 826        ret = kstrtou8(buf, 16, &data);
 827        if (ret || data > ADT7316_DA_2VREF_CH_MASK)
 828                return -EINVAL;
 829
 830        dac_config = chip->dac_config & (~ADT7316_DA_2VREF_CH_MASK);
 831        dac_config |= data;
 832
 833        ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
 834        if (ret)
 835                return -EIO;
 836
 837        chip->dac_config = dac_config;
 838
 839        return len;
 840}
 841
 842static IIO_DEVICE_ATTR(DAC_2Vref_channels_mask, 0644,
 843                adt7316_show_DAC_2Vref_ch_mask,
 844                adt7316_store_DAC_2Vref_ch_mask,
 845                0);
 846
 847static ssize_t adt7316_show_DAC_update_mode(struct device *dev,
 848                struct device_attribute *attr,
 849                char *buf)
 850{
 851        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 852        struct adt7316_chip_info *chip = iio_priv(dev_info);
 853
 854        if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA))
 855                return sprintf(buf, "manual\n");
 856
 857        switch (chip->dac_config & ADT7316_DA_EN_MODE_MASK) {
 858        case ADT7316_DA_EN_MODE_SINGLE:
 859                return sprintf(buf,
 860                        "0 - auto at any MSB DAC writing\n");
 861        case ADT7316_DA_EN_MODE_AB_CD:
 862                return sprintf(buf,
 863                        "1 - auto at MSB DAC AB and CD writing\n");
 864        case ADT7316_DA_EN_MODE_ABCD:
 865                return sprintf(buf,
 866                        "2 - auto at MSB DAC ABCD writing\n");
 867        default: /* ADT7316_DA_EN_MODE_LDAC */
 868                return sprintf(buf, "3 - manual\n");
 869        }
 870}
 871
 872static ssize_t adt7316_store_DAC_update_mode(struct device *dev,
 873                struct device_attribute *attr,
 874                const char *buf,
 875                size_t len)
 876{
 877        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 878        struct adt7316_chip_info *chip = iio_priv(dev_info);
 879        u8 dac_config;
 880        u8 data;
 881        int ret;
 882
 883        if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA))
 884                return -EPERM;
 885
 886        ret = kstrtou8(buf, 10, &data);
 887        if (ret || data > ADT7316_DA_EN_MODE_MASK)
 888                return -EINVAL;
 889
 890        dac_config = chip->dac_config & (~ADT7316_DA_EN_MODE_MASK);
 891        dac_config |= data;
 892
 893        ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
 894        if (ret)
 895                return -EIO;
 896
 897        chip->dac_config = dac_config;
 898
 899        return len;
 900}
 901
 902static IIO_DEVICE_ATTR(DAC_update_mode, 0644,
 903                adt7316_show_DAC_update_mode,
 904                adt7316_store_DAC_update_mode,
 905                0);
 906
 907static ssize_t adt7316_show_all_DAC_update_modes(struct device *dev,
 908                struct device_attribute *attr,
 909                char *buf)
 910{
 911        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 912        struct adt7316_chip_info *chip = iio_priv(dev_info);
 913
 914        if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA)
 915                return sprintf(buf, "0 - auto at any MSB DAC writing\n"
 916                                "1 - auto at MSB DAC AB and CD writing\n"
 917                                "2 - auto at MSB DAC ABCD writing\n"
 918                                "3 - manual\n");
 919        return sprintf(buf, "manual\n");
 920}
 921
 922static IIO_DEVICE_ATTR(all_DAC_update_modes, 0444,
 923                adt7316_show_all_DAC_update_modes, NULL, 0);
 924
 925static ssize_t adt7316_store_update_DAC(struct device *dev,
 926                struct device_attribute *attr,
 927                const char *buf,
 928                size_t len)
 929{
 930        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 931        struct adt7316_chip_info *chip = iio_priv(dev_info);
 932        u8 ldac_config;
 933        u8 data;
 934        int ret;
 935
 936        if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA) {
 937                if ((chip->dac_config & ADT7316_DA_EN_MODE_MASK) !=
 938                        ADT7316_DA_EN_MODE_LDAC)
 939                        return -EPERM;
 940
 941                ret = kstrtou8(buf, 16, &data);
 942                if (ret || data > ADT7316_LDAC_EN_DA_MASK)
 943                        return -EINVAL;
 944
 945                ldac_config = chip->ldac_config & (~ADT7316_LDAC_EN_DA_MASK);
 946                ldac_config |= data;
 947
 948                ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG,
 949                        ldac_config);
 950                if (ret)
 951                        return -EIO;
 952        } else {
 953                gpio_set_value(chip->ldac_pin, 0);
 954                gpio_set_value(chip->ldac_pin, 1);
 955        }
 956
 957        return len;
 958}
 959
 960static IIO_DEVICE_ATTR(update_DAC, 0644,
 961                NULL,
 962                adt7316_store_update_DAC,
 963                0);
 964
 965static ssize_t adt7316_show_DA_AB_Vref_bypass(struct device *dev,
 966                struct device_attribute *attr,
 967                char *buf)
 968{
 969        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 970        struct adt7316_chip_info *chip = iio_priv(dev_info);
 971
 972        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
 973                return -EPERM;
 974
 975        return sprintf(buf, "%d\n",
 976                !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_AB));
 977}
 978
 979static ssize_t adt7316_store_DA_AB_Vref_bypass(struct device *dev,
 980                struct device_attribute *attr,
 981                const char *buf,
 982                size_t len)
 983{
 984        struct iio_dev *dev_info = dev_to_iio_dev(dev);
 985        struct adt7316_chip_info *chip = iio_priv(dev_info);
 986        u8 dac_config;
 987        int ret;
 988
 989        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
 990                return -EPERM;
 991
 992        dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_AB);
 993        if (buf[0] == '1')
 994                dac_config |= ADT7316_VREF_BYPASS_DAC_AB;
 995
 996        ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
 997        if (ret)
 998                return -EIO;
 999
1000        chip->dac_config = dac_config;
1001
1002        return len;
1003}
1004
1005static IIO_DEVICE_ATTR(DA_AB_Vref_bypass, 0644,
1006                adt7316_show_DA_AB_Vref_bypass,
1007                adt7316_store_DA_AB_Vref_bypass,
1008                0);
1009
1010static ssize_t adt7316_show_DA_CD_Vref_bypass(struct device *dev,
1011                struct device_attribute *attr,
1012                char *buf)
1013{
1014        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1015        struct adt7316_chip_info *chip = iio_priv(dev_info);
1016
1017        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1018                return -EPERM;
1019
1020        return sprintf(buf, "%d\n",
1021                !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_CD));
1022}
1023
1024static ssize_t adt7316_store_DA_CD_Vref_bypass(struct device *dev,
1025                struct device_attribute *attr,
1026                const char *buf,
1027                size_t len)
1028{
1029        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1030        struct adt7316_chip_info *chip = iio_priv(dev_info);
1031        u8 dac_config;
1032        int ret;
1033
1034        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1035                return -EPERM;
1036
1037        dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_CD);
1038        if (buf[0] == '1')
1039                dac_config |= ADT7316_VREF_BYPASS_DAC_CD;
1040
1041        ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
1042        if (ret)
1043                return -EIO;
1044
1045        chip->dac_config = dac_config;
1046
1047        return len;
1048}
1049
1050static IIO_DEVICE_ATTR(DA_CD_Vref_bypass, 0644,
1051                adt7316_show_DA_CD_Vref_bypass,
1052                adt7316_store_DA_CD_Vref_bypass,
1053                0);
1054
1055static ssize_t adt7316_show_DAC_internal_Vref(struct device *dev,
1056                struct device_attribute *attr,
1057                char *buf)
1058{
1059        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1060        struct adt7316_chip_info *chip = iio_priv(dev_info);
1061
1062        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1063                return sprintf(buf, "0x%x\n",
1064                        (chip->dac_config & ADT7516_DAC_IN_VREF_MASK) >>
1065                        ADT7516_DAC_IN_VREF_OFFSET);
1066        return sprintf(buf, "%d\n",
1067                       !!(chip->dac_config & ADT7316_DAC_IN_VREF));
1068}
1069
1070static ssize_t adt7316_store_DAC_internal_Vref(struct device *dev,
1071                struct device_attribute *attr,
1072                const char *buf,
1073                size_t len)
1074{
1075        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1076        struct adt7316_chip_info *chip = iio_priv(dev_info);
1077        u8 ldac_config;
1078        u8 data;
1079        int ret;
1080
1081        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
1082                ret = kstrtou8(buf, 16, &data);
1083                if (ret || data > 3)
1084                        return -EINVAL;
1085
1086                ldac_config = chip->ldac_config & (~ADT7516_DAC_IN_VREF_MASK);
1087                if (data & 0x1)
1088                        ldac_config |= ADT7516_DAC_AB_IN_VREF;
1089                else if (data & 0x2)
1090                        ldac_config |= ADT7516_DAC_CD_IN_VREF;
1091        } else {
1092                ret = kstrtou8(buf, 16, &data);
1093                if (ret)
1094                        return -EINVAL;
1095
1096                ldac_config = chip->ldac_config & (~ADT7316_DAC_IN_VREF);
1097                if (data)
1098                        ldac_config = chip->ldac_config | ADT7316_DAC_IN_VREF;
1099        }
1100
1101        ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG,
1102                        ldac_config);
1103        if (ret)
1104                return -EIO;
1105
1106        chip->ldac_config = ldac_config;
1107
1108        return len;
1109}
1110
1111static IIO_DEVICE_ATTR(DAC_internal_Vref, 0644,
1112                adt7316_show_DAC_internal_Vref,
1113                adt7316_store_DAC_internal_Vref,
1114                0);
1115
1116static ssize_t adt7316_show_ad(struct adt7316_chip_info *chip,
1117                int channel, char *buf)
1118{
1119        u16 data;
1120        u8 msb, lsb;
1121        char sign = ' ';
1122        int ret;
1123
1124        if ((chip->config2 & ADT7316_AD_SINGLE_CH_MODE) &&
1125                channel != (chip->config2 & ADT7516_AD_SINGLE_CH_MASK))
1126                return -EPERM;
1127
1128        switch (channel) {
1129        case ADT7316_AD_SINGLE_CH_IN:
1130                ret = chip->bus.read(chip->bus.client,
1131                        ADT7316_LSB_IN_TEMP_VDD, &lsb);
1132                if (ret)
1133                        return -EIO;
1134
1135                ret = chip->bus.read(chip->bus.client,
1136                        ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1137                if (ret)
1138                        return -EIO;
1139
1140                data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1141                data |= lsb & ADT7316_LSB_IN_TEMP_MASK;
1142                break;
1143        case ADT7316_AD_SINGLE_CH_VDD:
1144                ret = chip->bus.read(chip->bus.client,
1145                        ADT7316_LSB_IN_TEMP_VDD, &lsb);
1146                if (ret)
1147                        return -EIO;
1148
1149                ret = chip->bus.read(chip->bus.client,
1150
1151                        ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1152                if (ret)
1153                        return -EIO;
1154
1155                data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1156                data |= (lsb & ADT7316_LSB_VDD_MASK) >> ADT7316_LSB_VDD_OFFSET;
1157                return sprintf(buf, "%d\n", data);
1158        default: /* ex_temp and ain */
1159                ret = chip->bus.read(chip->bus.client,
1160                        ADT7316_LSB_EX_TEMP_AIN, &lsb);
1161                if (ret)
1162                        return -EIO;
1163
1164                ret = chip->bus.read(chip->bus.client,
1165                        ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1166                if (ret)
1167                        return -EIO;
1168
1169                data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1170                data |= lsb & (ADT7316_LSB_EX_TEMP_MASK <<
1171                        (ADT7516_LSB_AIN_SHIFT * (channel -
1172                        (ADT7316_MSB_EX_TEMP - ADT7316_AD_MSB_DATA_BASE))));
1173
1174                if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1175                        return sprintf(buf, "%d\n", data);
1176
1177                break;
1178        }
1179
1180        if (data & ADT7316_T_VALUE_SIGN) {
1181                /* convert supplement to positive value */
1182                data = (ADT7316_T_VALUE_SIGN << 1) - data;
1183                sign = '-';
1184        }
1185
1186        return sprintf(buf, "%c%d.%.2d\n", sign,
1187                (data >> ADT7316_T_VALUE_FLOAT_OFFSET),
1188                (data & ADT7316_T_VALUE_FLOAT_MASK) * 25);
1189}
1190
1191static ssize_t adt7316_show_VDD(struct device *dev,
1192                struct device_attribute *attr,
1193                char *buf)
1194{
1195        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1196        struct adt7316_chip_info *chip = iio_priv(dev_info);
1197
1198        return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_VDD, buf);
1199}
1200static IIO_DEVICE_ATTR(VDD, 0444, adt7316_show_VDD, NULL, 0);
1201
1202static ssize_t adt7316_show_in_temp(struct device *dev,
1203                struct device_attribute *attr,
1204                char *buf)
1205{
1206        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1207        struct adt7316_chip_info *chip = iio_priv(dev_info);
1208
1209        return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_IN, buf);
1210}
1211
1212static IIO_DEVICE_ATTR(in_temp, 0444, adt7316_show_in_temp, NULL, 0);
1213
1214static ssize_t adt7316_show_ex_temp_AIN1(struct device *dev,
1215                struct device_attribute *attr,
1216                char *buf)
1217{
1218        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1219        struct adt7316_chip_info *chip = iio_priv(dev_info);
1220
1221        return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_EX, buf);
1222}
1223
1224static IIO_DEVICE_ATTR(ex_temp_AIN1, 0444, adt7316_show_ex_temp_AIN1,
1225                NULL, 0);
1226static IIO_DEVICE_ATTR(ex_temp, 0444, adt7316_show_ex_temp_AIN1, NULL, 0);
1227
1228static ssize_t adt7316_show_AIN2(struct device *dev,
1229                struct device_attribute *attr,
1230                char *buf)
1231{
1232        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1233        struct adt7316_chip_info *chip = iio_priv(dev_info);
1234
1235        return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN2, buf);
1236}
1237static IIO_DEVICE_ATTR(AIN2, 0444, adt7316_show_AIN2, NULL, 0);
1238
1239static ssize_t adt7316_show_AIN3(struct device *dev,
1240                struct device_attribute *attr,
1241                char *buf)
1242{
1243        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1244        struct adt7316_chip_info *chip = iio_priv(dev_info);
1245
1246        return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN3, buf);
1247}
1248static IIO_DEVICE_ATTR(AIN3, 0444, adt7316_show_AIN3, NULL, 0);
1249
1250static ssize_t adt7316_show_AIN4(struct device *dev,
1251                struct device_attribute *attr,
1252                char *buf)
1253{
1254        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1255        struct adt7316_chip_info *chip = iio_priv(dev_info);
1256
1257        return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN4, buf);
1258}
1259static IIO_DEVICE_ATTR(AIN4, 0444, adt7316_show_AIN4, NULL, 0);
1260
1261static ssize_t adt7316_show_temp_offset(struct adt7316_chip_info *chip,
1262                int offset_addr, char *buf)
1263{
1264        int data;
1265        u8 val;
1266        int ret;
1267
1268        ret = chip->bus.read(chip->bus.client, offset_addr, &val);
1269        if (ret)
1270                return -EIO;
1271
1272        data = (int)val;
1273        if (val & 0x80)
1274                data -= 256;
1275
1276        return sprintf(buf, "%d\n", data);
1277}
1278
1279static ssize_t adt7316_store_temp_offset(struct adt7316_chip_info *chip,
1280                int offset_addr, const char *buf, size_t len)
1281{
1282        int data;
1283        u8 val;
1284        int ret;
1285
1286        ret = kstrtoint(buf, 10, &data);
1287        if (ret || data > 127 || data < -128)
1288                return -EINVAL;
1289
1290        if (data < 0)
1291                data += 256;
1292
1293        val = (u8)data;
1294
1295        ret = chip->bus.write(chip->bus.client, offset_addr, val);
1296        if (ret)
1297                return -EIO;
1298
1299        return len;
1300}
1301
1302static ssize_t adt7316_show_in_temp_offset(struct device *dev,
1303                struct device_attribute *attr,
1304                char *buf)
1305{
1306        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1307        struct adt7316_chip_info *chip = iio_priv(dev_info);
1308
1309        return adt7316_show_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf);
1310}
1311
1312static ssize_t adt7316_store_in_temp_offset(struct device *dev,
1313                struct device_attribute *attr,
1314                const char *buf,
1315                size_t len)
1316{
1317        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1318        struct adt7316_chip_info *chip = iio_priv(dev_info);
1319
1320        return adt7316_store_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf,
1321                        len);
1322}
1323
1324static IIO_DEVICE_ATTR(in_temp_offset, 0644,
1325                adt7316_show_in_temp_offset,
1326                adt7316_store_in_temp_offset, 0);
1327
1328static ssize_t adt7316_show_ex_temp_offset(struct device *dev,
1329                struct device_attribute *attr,
1330                char *buf)
1331{
1332        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1333        struct adt7316_chip_info *chip = iio_priv(dev_info);
1334
1335        return adt7316_show_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf);
1336}
1337
1338static ssize_t adt7316_store_ex_temp_offset(struct device *dev,
1339                struct device_attribute *attr,
1340                const char *buf,
1341                size_t len)
1342{
1343        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1344        struct adt7316_chip_info *chip = iio_priv(dev_info);
1345
1346        return adt7316_store_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf,
1347                        len);
1348}
1349
1350static IIO_DEVICE_ATTR(ex_temp_offset, 0644,
1351                adt7316_show_ex_temp_offset,
1352                adt7316_store_ex_temp_offset, 0);
1353
1354static ssize_t adt7316_show_in_analog_temp_offset(struct device *dev,
1355                struct device_attribute *attr,
1356                char *buf)
1357{
1358        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1359        struct adt7316_chip_info *chip = iio_priv(dev_info);
1360
1361        return adt7316_show_temp_offset(chip,
1362                        ADT7316_IN_ANALOG_TEMP_OFFSET, buf);
1363}
1364
1365static ssize_t adt7316_store_in_analog_temp_offset(struct device *dev,
1366                struct device_attribute *attr,
1367                const char *buf,
1368                size_t len)
1369{
1370        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1371        struct adt7316_chip_info *chip = iio_priv(dev_info);
1372
1373        return adt7316_store_temp_offset(chip,
1374                        ADT7316_IN_ANALOG_TEMP_OFFSET, buf, len);
1375}
1376
1377static IIO_DEVICE_ATTR(in_analog_temp_offset, 0644,
1378                adt7316_show_in_analog_temp_offset,
1379                adt7316_store_in_analog_temp_offset, 0);
1380
1381static ssize_t adt7316_show_ex_analog_temp_offset(struct device *dev,
1382                struct device_attribute *attr,
1383                char *buf)
1384{
1385        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1386        struct adt7316_chip_info *chip = iio_priv(dev_info);
1387
1388        return adt7316_show_temp_offset(chip,
1389                        ADT7316_EX_ANALOG_TEMP_OFFSET, buf);
1390}
1391
1392static ssize_t adt7316_store_ex_analog_temp_offset(struct device *dev,
1393                struct device_attribute *attr,
1394                const char *buf,
1395                size_t len)
1396{
1397        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1398        struct adt7316_chip_info *chip = iio_priv(dev_info);
1399
1400        return adt7316_store_temp_offset(chip,
1401                        ADT7316_EX_ANALOG_TEMP_OFFSET, buf, len);
1402}
1403
1404static IIO_DEVICE_ATTR(ex_analog_temp_offset, 0644,
1405                adt7316_show_ex_analog_temp_offset,
1406                adt7316_store_ex_analog_temp_offset, 0);
1407
1408static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip,
1409                int channel, char *buf)
1410{
1411        u16 data;
1412        u8 msb, lsb, offset;
1413        int ret;
1414
1415        if (channel >= ADT7316_DA_MSB_DATA_REGS ||
1416                (channel == 0 &&
1417                (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
1418                (channel == 1 &&
1419                (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
1420                return -EPERM;
1421
1422        offset = chip->dac_bits - 8;
1423
1424        if (chip->dac_bits > 8) {
1425                ret = chip->bus.read(chip->bus.client,
1426                        ADT7316_DA_DATA_BASE + channel * 2, &lsb);
1427                if (ret)
1428                        return -EIO;
1429        }
1430
1431        ret = chip->bus.read(chip->bus.client,
1432                ADT7316_DA_DATA_BASE + 1 + channel * 2, &msb);
1433        if (ret)
1434                return -EIO;
1435
1436        data = (msb << offset) + (lsb & ((1 << offset) - 1));
1437
1438        return sprintf(buf, "%d\n", data);
1439}
1440
1441static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip,
1442                int channel, const char *buf, size_t len)
1443{
1444        u8 msb, lsb, offset;
1445        u16 data;
1446        int ret;
1447
1448        if (channel >= ADT7316_DA_MSB_DATA_REGS ||
1449                (channel == 0 &&
1450                (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
1451                (channel == 1 &&
1452                (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
1453                return -EPERM;
1454
1455        offset = chip->dac_bits - 8;
1456
1457        ret = kstrtou16(buf, 10, &data);
1458        if (ret || data >= (1 << chip->dac_bits))
1459                return -EINVAL;
1460
1461        if (chip->dac_bits > 8) {
1462                lsb = data & (1 << offset);
1463                ret = chip->bus.write(chip->bus.client,
1464                        ADT7316_DA_DATA_BASE + channel * 2, lsb);
1465                if (ret)
1466                        return -EIO;
1467        }
1468
1469        msb = data >> offset;
1470        ret = chip->bus.write(chip->bus.client,
1471                ADT7316_DA_DATA_BASE + 1 + channel * 2, msb);
1472        if (ret)
1473                return -EIO;
1474
1475        return len;
1476}
1477
1478static ssize_t adt7316_show_DAC_A(struct device *dev,
1479                struct device_attribute *attr,
1480                char *buf)
1481{
1482        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1483        struct adt7316_chip_info *chip = iio_priv(dev_info);
1484
1485        return adt7316_show_DAC(chip, 0, buf);
1486}
1487
1488static ssize_t adt7316_store_DAC_A(struct device *dev,
1489                struct device_attribute *attr,
1490                const char *buf,
1491                size_t len)
1492{
1493        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1494        struct adt7316_chip_info *chip = iio_priv(dev_info);
1495
1496        return adt7316_store_DAC(chip, 0, buf, len);
1497}
1498
1499static IIO_DEVICE_ATTR(DAC_A, 0644, adt7316_show_DAC_A,
1500                adt7316_store_DAC_A, 0);
1501
1502static ssize_t adt7316_show_DAC_B(struct device *dev,
1503                struct device_attribute *attr,
1504                char *buf)
1505{
1506        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1507        struct adt7316_chip_info *chip = iio_priv(dev_info);
1508
1509        return adt7316_show_DAC(chip, 1, buf);
1510}
1511
1512static ssize_t adt7316_store_DAC_B(struct device *dev,
1513                struct device_attribute *attr,
1514                const char *buf,
1515                size_t len)
1516{
1517        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1518        struct adt7316_chip_info *chip = iio_priv(dev_info);
1519
1520        return adt7316_store_DAC(chip, 1, buf, len);
1521}
1522
1523static IIO_DEVICE_ATTR(DAC_B, 0644, adt7316_show_DAC_B,
1524                adt7316_store_DAC_B, 0);
1525
1526static ssize_t adt7316_show_DAC_C(struct device *dev,
1527                struct device_attribute *attr,
1528                char *buf)
1529{
1530        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1531        struct adt7316_chip_info *chip = iio_priv(dev_info);
1532
1533        return adt7316_show_DAC(chip, 2, buf);
1534}
1535
1536static ssize_t adt7316_store_DAC_C(struct device *dev,
1537                struct device_attribute *attr,
1538                const char *buf,
1539                size_t len)
1540{
1541        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1542        struct adt7316_chip_info *chip = iio_priv(dev_info);
1543
1544        return adt7316_store_DAC(chip, 2, buf, len);
1545}
1546
1547static IIO_DEVICE_ATTR(DAC_C, 0644, adt7316_show_DAC_C,
1548                adt7316_store_DAC_C, 0);
1549
1550static ssize_t adt7316_show_DAC_D(struct device *dev,
1551                struct device_attribute *attr,
1552                char *buf)
1553{
1554        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1555        struct adt7316_chip_info *chip = iio_priv(dev_info);
1556
1557        return adt7316_show_DAC(chip, 3, buf);
1558}
1559
1560static ssize_t adt7316_store_DAC_D(struct device *dev,
1561                struct device_attribute *attr,
1562                const char *buf,
1563                size_t len)
1564{
1565        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1566        struct adt7316_chip_info *chip = iio_priv(dev_info);
1567
1568        return adt7316_store_DAC(chip, 3, buf, len);
1569}
1570
1571static IIO_DEVICE_ATTR(DAC_D, 0644, adt7316_show_DAC_D,
1572                adt7316_store_DAC_D, 0);
1573
1574static ssize_t adt7316_show_device_id(struct device *dev,
1575                struct device_attribute *attr,
1576                char *buf)
1577{
1578        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1579        struct adt7316_chip_info *chip = iio_priv(dev_info);
1580        u8 id;
1581        int ret;
1582
1583        ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_ID, &id);
1584        if (ret)
1585                return -EIO;
1586
1587        return sprintf(buf, "%d\n", id);
1588}
1589
1590static IIO_DEVICE_ATTR(device_id, 0444, adt7316_show_device_id, NULL, 0);
1591
1592static ssize_t adt7316_show_manufactorer_id(struct device *dev,
1593                struct device_attribute *attr,
1594                char *buf)
1595{
1596        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1597        struct adt7316_chip_info *chip = iio_priv(dev_info);
1598        u8 id;
1599        int ret;
1600
1601        ret = chip->bus.read(chip->bus.client, ADT7316_MANUFACTURE_ID, &id);
1602        if (ret)
1603                return -EIO;
1604
1605        return sprintf(buf, "%d\n", id);
1606}
1607
1608static IIO_DEVICE_ATTR(manufactorer_id, 0444,
1609                adt7316_show_manufactorer_id, NULL, 0);
1610
1611static ssize_t adt7316_show_device_rev(struct device *dev,
1612                struct device_attribute *attr,
1613                char *buf)
1614{
1615        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1616        struct adt7316_chip_info *chip = iio_priv(dev_info);
1617        u8 rev;
1618        int ret;
1619
1620        ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_REV, &rev);
1621        if (ret)
1622                return -EIO;
1623
1624        return sprintf(buf, "%d\n", rev);
1625}
1626
1627static IIO_DEVICE_ATTR(device_rev, 0444, adt7316_show_device_rev, NULL, 0);
1628
1629static ssize_t adt7316_show_bus_type(struct device *dev,
1630                struct device_attribute *attr,
1631                char *buf)
1632{
1633        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1634        struct adt7316_chip_info *chip = iio_priv(dev_info);
1635        u8 stat;
1636        int ret;
1637
1638        ret = chip->bus.read(chip->bus.client, ADT7316_SPI_LOCK_STAT, &stat);
1639        if (ret)
1640                return -EIO;
1641
1642        if (stat)
1643                return sprintf(buf, "spi\n");
1644
1645        return sprintf(buf, "i2c\n");
1646}
1647
1648static IIO_DEVICE_ATTR(bus_type, 0444, adt7316_show_bus_type, NULL, 0);
1649
1650static struct attribute *adt7316_attributes[] = {
1651        &iio_dev_attr_all_modes.dev_attr.attr,
1652        &iio_dev_attr_mode.dev_attr.attr,
1653        &iio_dev_attr_enabled.dev_attr.attr,
1654        &iio_dev_attr_ad_channel.dev_attr.attr,
1655        &iio_dev_attr_all_ad_channels.dev_attr.attr,
1656        &iio_dev_attr_disable_averaging.dev_attr.attr,
1657        &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
1658        &iio_dev_attr_powerdown.dev_attr.attr,
1659        &iio_dev_attr_fast_ad_clock.dev_attr.attr,
1660        &iio_dev_attr_da_high_resolution.dev_attr.attr,
1661        &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
1662        &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
1663        &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
1664        &iio_dev_attr_DAC_update_mode.dev_attr.attr,
1665        &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
1666        &iio_dev_attr_update_DAC.dev_attr.attr,
1667        &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
1668        &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
1669        &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
1670        &iio_dev_attr_VDD.dev_attr.attr,
1671        &iio_dev_attr_in_temp.dev_attr.attr,
1672        &iio_dev_attr_ex_temp.dev_attr.attr,
1673        &iio_dev_attr_in_temp_offset.dev_attr.attr,
1674        &iio_dev_attr_ex_temp_offset.dev_attr.attr,
1675        &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
1676        &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
1677        &iio_dev_attr_DAC_A.dev_attr.attr,
1678        &iio_dev_attr_DAC_B.dev_attr.attr,
1679        &iio_dev_attr_DAC_C.dev_attr.attr,
1680        &iio_dev_attr_DAC_D.dev_attr.attr,
1681        &iio_dev_attr_device_id.dev_attr.attr,
1682        &iio_dev_attr_manufactorer_id.dev_attr.attr,
1683        &iio_dev_attr_device_rev.dev_attr.attr,
1684        &iio_dev_attr_bus_type.dev_attr.attr,
1685        NULL,
1686};
1687
1688static const struct attribute_group adt7316_attribute_group = {
1689        .attrs = adt7316_attributes,
1690};
1691
1692static struct attribute *adt7516_attributes[] = {
1693        &iio_dev_attr_all_modes.dev_attr.attr,
1694        &iio_dev_attr_mode.dev_attr.attr,
1695        &iio_dev_attr_select_ex_temp.dev_attr.attr,
1696        &iio_dev_attr_enabled.dev_attr.attr,
1697        &iio_dev_attr_ad_channel.dev_attr.attr,
1698        &iio_dev_attr_all_ad_channels.dev_attr.attr,
1699        &iio_dev_attr_disable_averaging.dev_attr.attr,
1700        &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
1701        &iio_dev_attr_powerdown.dev_attr.attr,
1702        &iio_dev_attr_fast_ad_clock.dev_attr.attr,
1703        &iio_dev_attr_AIN_internal_Vref.dev_attr.attr,
1704        &iio_dev_attr_da_high_resolution.dev_attr.attr,
1705        &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
1706        &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
1707        &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
1708        &iio_dev_attr_DAC_update_mode.dev_attr.attr,
1709        &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
1710        &iio_dev_attr_update_DAC.dev_attr.attr,
1711        &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
1712        &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
1713        &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
1714        &iio_dev_attr_VDD.dev_attr.attr,
1715        &iio_dev_attr_in_temp.dev_attr.attr,
1716        &iio_dev_attr_ex_temp_AIN1.dev_attr.attr,
1717        &iio_dev_attr_AIN2.dev_attr.attr,
1718        &iio_dev_attr_AIN3.dev_attr.attr,
1719        &iio_dev_attr_AIN4.dev_attr.attr,
1720        &iio_dev_attr_in_temp_offset.dev_attr.attr,
1721        &iio_dev_attr_ex_temp_offset.dev_attr.attr,
1722        &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
1723        &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
1724        &iio_dev_attr_DAC_A.dev_attr.attr,
1725        &iio_dev_attr_DAC_B.dev_attr.attr,
1726        &iio_dev_attr_DAC_C.dev_attr.attr,
1727        &iio_dev_attr_DAC_D.dev_attr.attr,
1728        &iio_dev_attr_device_id.dev_attr.attr,
1729        &iio_dev_attr_manufactorer_id.dev_attr.attr,
1730        &iio_dev_attr_device_rev.dev_attr.attr,
1731        &iio_dev_attr_bus_type.dev_attr.attr,
1732        NULL,
1733};
1734
1735static const struct attribute_group adt7516_attribute_group = {
1736        .attrs = adt7516_attributes,
1737};
1738
1739static irqreturn_t adt7316_event_handler(int irq, void *private)
1740{
1741        struct iio_dev *indio_dev = private;
1742        struct adt7316_chip_info *chip = iio_priv(indio_dev);
1743        u8 stat1, stat2;
1744        int ret;
1745        s64 time;
1746
1747        ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT1, &stat1);
1748        if (!ret) {
1749                if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
1750                        stat1 &= 0x1F;
1751
1752                time = iio_get_time_ns(indio_dev);
1753                if (stat1 & BIT(0))
1754                        iio_push_event(indio_dev,
1755                                       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
1756                                                            IIO_EV_TYPE_THRESH,
1757                                                            IIO_EV_DIR_RISING),
1758                                       time);
1759                if (stat1 & BIT(1))
1760                        iio_push_event(indio_dev,
1761                                       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
1762                                                            IIO_EV_TYPE_THRESH,
1763                                                            IIO_EV_DIR_FALLING),
1764                                       time);
1765                if (stat1 & BIT(2))
1766                        iio_push_event(indio_dev,
1767                                       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
1768                                                            IIO_EV_TYPE_THRESH,
1769                                                            IIO_EV_DIR_RISING),
1770                                       time);
1771                if (stat1 & BIT(3))
1772                        iio_push_event(indio_dev,
1773                                       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
1774                                                            IIO_EV_TYPE_THRESH,
1775                                                            IIO_EV_DIR_FALLING),
1776                                       time);
1777                if (stat1 & BIT(5))
1778                        iio_push_event(indio_dev,
1779                                       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
1780                                                            IIO_EV_TYPE_THRESH,
1781                                                            IIO_EV_DIR_EITHER),
1782                                       time);
1783                if (stat1 & BIT(6))
1784                        iio_push_event(indio_dev,
1785                                       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
1786                                                            IIO_EV_TYPE_THRESH,
1787                                                            IIO_EV_DIR_EITHER),
1788                                       time);
1789                if (stat1 & BIT(7))
1790                        iio_push_event(indio_dev,
1791                                       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
1792                                                            IIO_EV_TYPE_THRESH,
1793                                                            IIO_EV_DIR_EITHER),
1794                                       time);
1795                }
1796        ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT2, &stat2);
1797        if (!ret) {
1798                if (stat2 & ADT7316_INT_MASK2_VDD)
1799                        iio_push_event(indio_dev,
1800                                       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
1801                                                            0,
1802                                                            IIO_EV_TYPE_THRESH,
1803                                                            IIO_EV_DIR_RISING),
1804                                       iio_get_time_ns(indio_dev));
1805        }
1806
1807        return IRQ_HANDLED;
1808}
1809
1810/*
1811 * Show mask of enabled interrupts in Hex.
1812 */
1813static ssize_t adt7316_show_int_mask(struct device *dev,
1814                struct device_attribute *attr,
1815                char *buf)
1816{
1817        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1818        struct adt7316_chip_info *chip = iio_priv(dev_info);
1819
1820        return sprintf(buf, "0x%x\n", chip->int_mask);
1821}
1822
1823/*
1824 * Set 1 to the mask in Hex to enabled interrupts.
1825 */
1826static ssize_t adt7316_set_int_mask(struct device *dev,
1827                struct device_attribute *attr,
1828                const char *buf,
1829                size_t len)
1830{
1831        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1832        struct adt7316_chip_info *chip = iio_priv(dev_info);
1833        u16 data;
1834        int ret;
1835        u8 mask;
1836
1837        ret = kstrtou16(buf, 16, &data);
1838        if (ret || data >= ADT7316_VDD_INT_MASK + 1)
1839                return -EINVAL;
1840
1841        if (data & ADT7316_VDD_INT_MASK)
1842                mask = 0;                       /* enable vdd int */
1843        else
1844                mask = ADT7316_INT_MASK2_VDD;   /* disable vdd int */
1845
1846        ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK2, mask);
1847        if (!ret) {
1848                chip->int_mask &= ~ADT7316_VDD_INT_MASK;
1849                chip->int_mask |= data & ADT7316_VDD_INT_MASK;
1850        }
1851
1852        if (data & ADT7316_TEMP_AIN_INT_MASK) {
1853                if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX)
1854                        /* mask in reg is opposite, set 1 to disable */
1855                        mask = (~data) & ADT7316_TEMP_INT_MASK;
1856                else
1857                        /* mask in reg is opposite, set 1 to disable */
1858                        mask = (~data) & ADT7316_TEMP_AIN_INT_MASK;
1859        }
1860        ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK1, mask);
1861
1862        chip->int_mask = mask;
1863
1864        return len;
1865}
1866
1867static inline ssize_t adt7316_show_ad_bound(struct device *dev,
1868                struct device_attribute *attr,
1869                char *buf)
1870{
1871        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1872        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1873        struct adt7316_chip_info *chip = iio_priv(dev_info);
1874        u8 val;
1875        int data;
1876        int ret;
1877
1878        if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
1879                this_attr->address > ADT7316_EX_TEMP_LOW)
1880                return -EPERM;
1881
1882        ret = chip->bus.read(chip->bus.client, this_attr->address, &val);
1883        if (ret)
1884                return -EIO;
1885
1886        data = (int)val;
1887
1888        if (!((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
1889                (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)) {
1890                if (data & 0x80)
1891                        data -= 256;
1892        }
1893
1894        return sprintf(buf, "%d\n", data);
1895}
1896
1897static inline ssize_t adt7316_set_ad_bound(struct device *dev,
1898                struct device_attribute *attr,
1899                const char *buf,
1900                size_t len)
1901{
1902        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1903        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1904        struct adt7316_chip_info *chip = iio_priv(dev_info);
1905        int data;
1906        u8 val;
1907        int ret;
1908
1909        if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
1910                this_attr->address > ADT7316_EX_TEMP_LOW)
1911                return -EPERM;
1912
1913        ret = kstrtoint(buf, 10, &data);
1914        if (ret)
1915                return -EINVAL;
1916
1917        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
1918                (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0) {
1919                if (data > 255 || data < 0)
1920                        return -EINVAL;
1921        } else {
1922                if (data > 127 || data < -128)
1923                        return -EINVAL;
1924
1925                if (data < 0)
1926                        data += 256;
1927        }
1928
1929        val = (u8)data;
1930
1931        ret = chip->bus.write(chip->bus.client, this_attr->address, val);
1932        if (ret)
1933                return -EIO;
1934
1935        return len;
1936}
1937
1938static ssize_t adt7316_show_int_enabled(struct device *dev,
1939                struct device_attribute *attr,
1940                char *buf)
1941{
1942        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1943        struct adt7316_chip_info *chip = iio_priv(dev_info);
1944
1945        return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_INT_EN));
1946}
1947
1948static ssize_t adt7316_set_int_enabled(struct device *dev,
1949                struct device_attribute *attr,
1950                const char *buf,
1951                size_t len)
1952{
1953        struct iio_dev *dev_info = dev_to_iio_dev(dev);
1954        struct adt7316_chip_info *chip = iio_priv(dev_info);
1955        u8 config1;
1956        int ret;
1957
1958        config1 = chip->config1 & (~ADT7316_INT_EN);
1959        if (buf[0] == '1')
1960                config1 |= ADT7316_INT_EN;
1961
1962        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
1963        if (ret)
1964                return -EIO;
1965
1966        chip->config1 = config1;
1967
1968        return len;
1969}
1970
1971static IIO_DEVICE_ATTR(int_mask,
1972                       0644,
1973                       adt7316_show_int_mask, adt7316_set_int_mask,
1974                       0);
1975static IIO_DEVICE_ATTR(in_temp_high_value,
1976                       0644,
1977                       adt7316_show_ad_bound, adt7316_set_ad_bound,
1978                       ADT7316_IN_TEMP_HIGH);
1979static IIO_DEVICE_ATTR(in_temp_low_value,
1980                       0644,
1981                       adt7316_show_ad_bound, adt7316_set_ad_bound,
1982                       ADT7316_IN_TEMP_LOW);
1983static IIO_DEVICE_ATTR(ex_temp_high_value,
1984                       0644,
1985                       adt7316_show_ad_bound, adt7316_set_ad_bound,
1986                       ADT7316_EX_TEMP_HIGH);
1987static IIO_DEVICE_ATTR(ex_temp_low_value,
1988                       0644,
1989                       adt7316_show_ad_bound, adt7316_set_ad_bound,
1990                       ADT7316_EX_TEMP_LOW);
1991
1992/* NASTY duplication to be fixed */
1993static IIO_DEVICE_ATTR(ex_temp_ain1_high_value,
1994                       0644,
1995                       adt7316_show_ad_bound, adt7316_set_ad_bound,
1996                       ADT7316_EX_TEMP_HIGH);
1997static IIO_DEVICE_ATTR(ex_temp_ain1_low_value,
1998                       0644,
1999                       adt7316_show_ad_bound, adt7316_set_ad_bound,
2000                       ADT7316_EX_TEMP_LOW);
2001static IIO_DEVICE_ATTR(ain2_high_value,
2002                       0644,
2003                       adt7316_show_ad_bound, adt7316_set_ad_bound,
2004                       ADT7516_AIN2_HIGH);
2005static IIO_DEVICE_ATTR(ain2_low_value,
2006                       0644,
2007                       adt7316_show_ad_bound, adt7316_set_ad_bound,
2008                       ADT7516_AIN2_LOW);
2009static IIO_DEVICE_ATTR(ain3_high_value,
2010                       0644,
2011                       adt7316_show_ad_bound, adt7316_set_ad_bound,
2012                       ADT7516_AIN3_HIGH);
2013static IIO_DEVICE_ATTR(ain3_low_value,
2014                       0644,
2015                       adt7316_show_ad_bound, adt7316_set_ad_bound,
2016                       ADT7516_AIN3_LOW);
2017static IIO_DEVICE_ATTR(ain4_high_value,
2018                       0644,
2019                       adt7316_show_ad_bound, adt7316_set_ad_bound,
2020                       ADT7516_AIN4_HIGH);
2021static IIO_DEVICE_ATTR(ain4_low_value,
2022                       0644,
2023                       adt7316_show_ad_bound, adt7316_set_ad_bound,
2024                       ADT7516_AIN4_LOW);
2025static IIO_DEVICE_ATTR(int_enabled,
2026                       0644,
2027                       adt7316_show_int_enabled,
2028                       adt7316_set_int_enabled, 0);
2029
2030static struct attribute *adt7316_event_attributes[] = {
2031        &iio_dev_attr_int_mask.dev_attr.attr,
2032        &iio_dev_attr_in_temp_high_value.dev_attr.attr,
2033        &iio_dev_attr_in_temp_low_value.dev_attr.attr,
2034        &iio_dev_attr_ex_temp_high_value.dev_attr.attr,
2035        &iio_dev_attr_ex_temp_low_value.dev_attr.attr,
2036        &iio_dev_attr_int_enabled.dev_attr.attr,
2037        NULL,
2038};
2039
2040static const struct attribute_group adt7316_event_attribute_group = {
2041        .attrs = adt7316_event_attributes,
2042        .name = "events",
2043};
2044
2045static struct attribute *adt7516_event_attributes[] = {
2046        &iio_dev_attr_int_mask.dev_attr.attr,
2047        &iio_dev_attr_in_temp_high_value.dev_attr.attr,
2048        &iio_dev_attr_in_temp_low_value.dev_attr.attr,
2049        &iio_dev_attr_ex_temp_ain1_high_value.dev_attr.attr,
2050        &iio_dev_attr_ex_temp_ain1_low_value.dev_attr.attr,
2051        &iio_dev_attr_ain2_high_value.dev_attr.attr,
2052        &iio_dev_attr_ain2_low_value.dev_attr.attr,
2053        &iio_dev_attr_ain3_high_value.dev_attr.attr,
2054        &iio_dev_attr_ain3_low_value.dev_attr.attr,
2055        &iio_dev_attr_ain4_high_value.dev_attr.attr,
2056        &iio_dev_attr_ain4_low_value.dev_attr.attr,
2057        &iio_dev_attr_int_enabled.dev_attr.attr,
2058        NULL,
2059};
2060
2061static const struct attribute_group adt7516_event_attribute_group = {
2062        .attrs = adt7516_event_attributes,
2063        .name = "events",
2064};
2065
2066#ifdef CONFIG_PM_SLEEP
2067static int adt7316_disable(struct device *dev)
2068{
2069        struct iio_dev *dev_info = dev_get_drvdata(dev);
2070        struct adt7316_chip_info *chip = iio_priv(dev_info);
2071
2072        return _adt7316_store_enabled(chip, 0);
2073}
2074
2075static int adt7316_enable(struct device *dev)
2076{
2077        struct iio_dev *dev_info = dev_get_drvdata(dev);
2078        struct adt7316_chip_info *chip = iio_priv(dev_info);
2079
2080        return _adt7316_store_enabled(chip, 1);
2081}
2082EXPORT_SYMBOL_GPL(adt7316_pm_ops);
2083SIMPLE_DEV_PM_OPS(adt7316_pm_ops, adt7316_disable, adt7316_enable);
2084#endif
2085
2086static const struct iio_info adt7316_info = {
2087        .attrs = &adt7316_attribute_group,
2088        .event_attrs = &adt7316_event_attribute_group,
2089};
2090
2091static const struct iio_info adt7516_info = {
2092        .attrs = &adt7516_attribute_group,
2093        .event_attrs = &adt7516_event_attribute_group,
2094};
2095
2096/*
2097 * device probe and remove
2098 */
2099int adt7316_probe(struct device *dev, struct adt7316_bus *bus,
2100                const char *name)
2101{
2102        struct adt7316_chip_info *chip;
2103        struct iio_dev *indio_dev;
2104        unsigned short *adt7316_platform_data = dev->platform_data;
2105        int ret = 0;
2106
2107        indio_dev = devm_iio_device_alloc(dev, sizeof(*chip));
2108        if (!indio_dev)
2109                return -ENOMEM;
2110        chip = iio_priv(indio_dev);
2111        /* this is only used for device removal purposes */
2112        dev_set_drvdata(dev, indio_dev);
2113
2114        chip->bus = *bus;
2115
2116        if (name[4] == '3')
2117                chip->id = ID_ADT7316 + (name[6] - '6');
2118        else if (name[4] == '5')
2119                chip->id = ID_ADT7516 + (name[6] - '6');
2120        else
2121                return -ENODEV;
2122
2123        chip->ldac_pin = adt7316_platform_data[1];
2124        if (chip->ldac_pin) {
2125                chip->config3 |= ADT7316_DA_EN_VIA_DAC_LDCA;
2126                if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2127                        chip->config1 |= ADT7516_SEL_AIN3;
2128        }
2129        chip->int_mask = ADT7316_TEMP_INT_MASK | ADT7316_VDD_INT_MASK;
2130        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2131                chip->int_mask |= ADT7516_AIN_INT_MASK;
2132
2133        indio_dev->dev.parent = dev;
2134        if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2135                indio_dev->info = &adt7516_info;
2136        else
2137                indio_dev->info = &adt7316_info;
2138        indio_dev->name = name;
2139        indio_dev->modes = INDIO_DIRECT_MODE;
2140
2141        if (chip->bus.irq > 0) {
2142                if (adt7316_platform_data[0])
2143                        chip->bus.irq_flags = adt7316_platform_data[0];
2144
2145                ret = devm_request_threaded_irq(dev, chip->bus.irq,
2146                                                NULL,
2147                                                adt7316_event_handler,
2148                                                chip->bus.irq_flags |
2149                                                IRQF_ONESHOT,
2150                                                indio_dev->name,
2151                                                indio_dev);
2152                if (ret)
2153                        return ret;
2154
2155                if (chip->bus.irq_flags & IRQF_TRIGGER_HIGH)
2156                        chip->config1 |= ADT7316_INT_POLARITY;
2157        }
2158
2159        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, chip->config1);
2160        if (ret)
2161                return -EIO;
2162
2163        ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, chip->config3);
2164        if (ret)
2165                return -EIO;
2166
2167        ret = devm_iio_device_register(dev, indio_dev);
2168        if (ret)
2169                return ret;
2170
2171        dev_info(dev, "%s temperature sensor, ADC and DAC registered.\n",
2172                        indio_dev->name);
2173
2174        return 0;
2175}
2176EXPORT_SYMBOL(adt7316_probe);
2177
2178MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
2179MODULE_DESCRIPTION("Analog Devices ADT7316/7/8 and ADT7516/7/9 digital temperature sensor, ADC and DAC driver");
2180MODULE_LICENSE("GPL v2");
2181