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