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 178 struct adt7316_chip_info { 179 struct adt7316_bus bus; 180 struct gpio_desc *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 214 struct adt7316_limit_regs { 215 u16 data_high; 216 u16 data_low; 217 }; 218 219 static 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 229 static 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 static 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 269 static IIO_DEVICE_ATTR(enabled, 0644, 270 adt7316_show_enabled, 271 adt7316_store_enabled, 272 0); 273 274 static 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 287 static 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 313 static IIO_DEVICE_ATTR(select_ex_temp, 0644, 314 adt7316_show_select_ex_temp, 315 adt7316_store_select_ex_temp, 316 0); 317 318 static 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 331 static 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 354 static IIO_DEVICE_ATTR(mode, 0644, 355 adt7316_show_mode, 356 adt7316_store_mode, 357 0); 358 359 static 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 366 static IIO_DEVICE_ATTR(all_modes, 0444, adt7316_show_all_modes, NULL, 0); 367 368 static 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 406 static 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 447 static IIO_DEVICE_ATTR(ad_channel, 0644, 448 adt7316_show_ad_channel, 449 adt7316_store_ad_channel, 450 0); 451 452 static 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 470 static IIO_DEVICE_ATTR(all_ad_channels, 0444, 471 adt7316_show_all_ad_channels, NULL, 0); 472 473 static 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 484 static 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 507 static IIO_DEVICE_ATTR(disable_averaging, 0644, 508 adt7316_show_disable_averaging, 509 adt7316_store_disable_averaging, 510 0); 511 512 static 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 523 static 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 546 static IIO_DEVICE_ATTR(enable_smbus_timeout, 0644, 547 adt7316_show_enable_smbus_timeout, 548 adt7316_store_enable_smbus_timeout, 549 0); 550 551 static 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 561 static 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 584 static IIO_DEVICE_ATTR(powerdown, 0644, 585 adt7316_show_powerdown, 586 adt7316_store_powerdown, 587 0); 588 589 static 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 599 static 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 622 static IIO_DEVICE_ATTR(fast_ad_clock, 0644, 623 adt7316_show_fast_ad_clock, 624 adt7316_store_fast_ad_clock, 625 0); 626 627 static 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 644 static 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 675 static IIO_DEVICE_ATTR(da_high_resolution, 0644, 676 adt7316_show_da_high_resolution, 677 adt7316_store_da_high_resolution, 678 0); 679 680 static 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 694 static 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 721 static IIO_DEVICE_ATTR(AIN_internal_Vref, 0644, 722 adt7316_show_AIN_internal_Vref, 723 adt7316_store_AIN_internal_Vref, 724 0); 725 726 static 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 737 static 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 760 static IIO_DEVICE_ATTR(enable_proportion_DACA, 0644, 761 adt7316_show_enable_prop_DACA, 762 adt7316_store_enable_prop_DACA, 763 0); 764 765 static 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 776 static 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 799 static IIO_DEVICE_ATTR(enable_proportion_DACB, 0644, 800 adt7316_show_enable_prop_DACB, 801 adt7316_store_enable_prop_DACB, 802 0); 803 804 static 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 815 static 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 842 static 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 847 static 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 872 static 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 902 static IIO_DEVICE_ATTR(DAC_update_mode, 0644, 903 adt7316_show_DAC_update_mode, 904 adt7316_store_DAC_update_mode, 905 0); 906 907 static 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 922 static IIO_DEVICE_ATTR(all_DAC_update_modes, 0444, 923 adt7316_show_all_DAC_update_modes, NULL, 0); 924 925 static 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 gpiod_set_value(chip->ldac_pin, 0); 954 gpiod_set_value(chip->ldac_pin, 1); 955 } 956 957 return len; 958 } 959 960 static IIO_DEVICE_ATTR(update_DAC, 0644, 961 NULL, 962 adt7316_store_update_DAC, 963 0); 964 965 static 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 979 static 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 1005 static 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 1010 static 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 1024 static 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 1050 static 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 1055 static 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 1070 static 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 1111 static IIO_DEVICE_ATTR(DAC_internal_Vref, 0644, 1112 adt7316_show_DAC_internal_Vref, 1113 adt7316_store_DAC_internal_Vref, 1114 0); 1115 1116 static 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 1191 static 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 } 1200 static IIO_DEVICE_ATTR(VDD, 0444, adt7316_show_VDD, NULL, 0); 1201 1202 static 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 1212 static IIO_DEVICE_ATTR(in_temp, 0444, adt7316_show_in_temp, NULL, 0); 1213 1214 static 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 1224 static IIO_DEVICE_ATTR(ex_temp_AIN1, 0444, adt7316_show_ex_temp_AIN1, 1225 NULL, 0); 1226 static IIO_DEVICE_ATTR(ex_temp, 0444, adt7316_show_ex_temp_AIN1, NULL, 0); 1227 1228 static 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 } 1237 static IIO_DEVICE_ATTR(AIN2, 0444, adt7316_show_AIN2, NULL, 0); 1238 1239 static 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 } 1248 static IIO_DEVICE_ATTR(AIN3, 0444, adt7316_show_AIN3, NULL, 0); 1249 1250 static 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 } 1259 static IIO_DEVICE_ATTR(AIN4, 0444, adt7316_show_AIN4, NULL, 0); 1260 1261 static 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 1279 static ssize_t adt7316_store_temp_offset(struct adt7316_chip_info *chip, 1280 int offset_addr, 1281 const char *buf, 1282 size_t len) 1283 { 1284 int data; 1285 u8 val; 1286 int ret; 1287 1288 ret = kstrtoint(buf, 10, &data); 1289 if (ret || data > 127 || data < -128) 1290 return -EINVAL; 1291 1292 if (data < 0) 1293 data += 256; 1294 1295 val = (u8)data; 1296 1297 ret = chip->bus.write(chip->bus.client, offset_addr, val); 1298 if (ret) 1299 return -EIO; 1300 1301 return len; 1302 } 1303 1304 static ssize_t adt7316_show_in_temp_offset(struct device *dev, 1305 struct device_attribute *attr, 1306 char *buf) 1307 { 1308 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1309 struct adt7316_chip_info *chip = iio_priv(dev_info); 1310 1311 return adt7316_show_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf); 1312 } 1313 1314 static ssize_t adt7316_store_in_temp_offset(struct device *dev, 1315 struct device_attribute *attr, 1316 const char *buf, 1317 size_t len) 1318 { 1319 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1320 struct adt7316_chip_info *chip = iio_priv(dev_info); 1321 1322 return adt7316_store_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf, 1323 len); 1324 } 1325 1326 static IIO_DEVICE_ATTR(in_temp_offset, 0644, 1327 adt7316_show_in_temp_offset, 1328 adt7316_store_in_temp_offset, 0); 1329 1330 static ssize_t adt7316_show_ex_temp_offset(struct device *dev, 1331 struct device_attribute *attr, 1332 char *buf) 1333 { 1334 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1335 struct adt7316_chip_info *chip = iio_priv(dev_info); 1336 1337 return adt7316_show_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf); 1338 } 1339 1340 static ssize_t adt7316_store_ex_temp_offset(struct device *dev, 1341 struct device_attribute *attr, 1342 const char *buf, 1343 size_t len) 1344 { 1345 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1346 struct adt7316_chip_info *chip = iio_priv(dev_info); 1347 1348 return adt7316_store_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf, 1349 len); 1350 } 1351 1352 static IIO_DEVICE_ATTR(ex_temp_offset, 0644, 1353 adt7316_show_ex_temp_offset, 1354 adt7316_store_ex_temp_offset, 0); 1355 1356 static ssize_t adt7316_show_in_analog_temp_offset(struct device *dev, 1357 struct device_attribute *attr, 1358 char *buf) 1359 { 1360 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1361 struct adt7316_chip_info *chip = iio_priv(dev_info); 1362 1363 return adt7316_show_temp_offset(chip, 1364 ADT7316_IN_ANALOG_TEMP_OFFSET, buf); 1365 } 1366 1367 static ssize_t adt7316_store_in_analog_temp_offset(struct device *dev, 1368 struct device_attribute *attr, 1369 const char *buf, 1370 size_t len) 1371 { 1372 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1373 struct adt7316_chip_info *chip = iio_priv(dev_info); 1374 1375 return adt7316_store_temp_offset(chip, 1376 ADT7316_IN_ANALOG_TEMP_OFFSET, buf, len); 1377 } 1378 1379 static IIO_DEVICE_ATTR(in_analog_temp_offset, 0644, 1380 adt7316_show_in_analog_temp_offset, 1381 adt7316_store_in_analog_temp_offset, 0); 1382 1383 static ssize_t adt7316_show_ex_analog_temp_offset(struct device *dev, 1384 struct device_attribute *attr, 1385 char *buf) 1386 { 1387 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1388 struct adt7316_chip_info *chip = iio_priv(dev_info); 1389 1390 return adt7316_show_temp_offset(chip, 1391 ADT7316_EX_ANALOG_TEMP_OFFSET, buf); 1392 } 1393 1394 static ssize_t adt7316_store_ex_analog_temp_offset(struct device *dev, 1395 struct device_attribute *attr, 1396 const char *buf, 1397 size_t len) 1398 { 1399 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1400 struct adt7316_chip_info *chip = iio_priv(dev_info); 1401 1402 return adt7316_store_temp_offset(chip, 1403 ADT7316_EX_ANALOG_TEMP_OFFSET, buf, len); 1404 } 1405 1406 static IIO_DEVICE_ATTR(ex_analog_temp_offset, 0644, 1407 adt7316_show_ex_analog_temp_offset, 1408 adt7316_store_ex_analog_temp_offset, 0); 1409 1410 static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip, 1411 int channel, char *buf) 1412 { 1413 u16 data; 1414 u8 msb, lsb, offset; 1415 int ret; 1416 1417 if (channel >= ADT7316_DA_MSB_DATA_REGS || 1418 (channel == 0 && 1419 (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) || 1420 (channel == 1 && 1421 (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB))) 1422 return -EPERM; 1423 1424 offset = chip->dac_bits - 8; 1425 1426 if (chip->dac_bits > 8) { 1427 ret = chip->bus.read(chip->bus.client, 1428 ADT7316_DA_DATA_BASE + channel * 2, &lsb); 1429 if (ret) 1430 return -EIO; 1431 } 1432 1433 ret = chip->bus.read(chip->bus.client, 1434 ADT7316_DA_DATA_BASE + 1 + channel * 2, &msb); 1435 if (ret) 1436 return -EIO; 1437 1438 data = (msb << offset) + (lsb & ((1 << offset) - 1)); 1439 1440 return sprintf(buf, "%d\n", data); 1441 } 1442 1443 static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip, 1444 int channel, const char *buf, size_t len) 1445 { 1446 u8 msb, lsb, offset; 1447 u16 data; 1448 int ret; 1449 1450 if (channel >= ADT7316_DA_MSB_DATA_REGS || 1451 (channel == 0 && 1452 (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) || 1453 (channel == 1 && 1454 (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB))) 1455 return -EPERM; 1456 1457 offset = chip->dac_bits - 8; 1458 1459 ret = kstrtou16(buf, 10, &data); 1460 if (ret || data >= (1 << chip->dac_bits)) 1461 return -EINVAL; 1462 1463 if (chip->dac_bits > 8) { 1464 lsb = data & (1 << offset); 1465 ret = chip->bus.write(chip->bus.client, 1466 ADT7316_DA_DATA_BASE + channel * 2, lsb); 1467 if (ret) 1468 return -EIO; 1469 } 1470 1471 msb = data >> offset; 1472 ret = chip->bus.write(chip->bus.client, 1473 ADT7316_DA_DATA_BASE + 1 + channel * 2, msb); 1474 if (ret) 1475 return -EIO; 1476 1477 return len; 1478 } 1479 1480 static ssize_t adt7316_show_DAC_A(struct device *dev, 1481 struct device_attribute *attr, 1482 char *buf) 1483 { 1484 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1485 struct adt7316_chip_info *chip = iio_priv(dev_info); 1486 1487 return adt7316_show_DAC(chip, 0, buf); 1488 } 1489 1490 static ssize_t adt7316_store_DAC_A(struct device *dev, 1491 struct device_attribute *attr, 1492 const char *buf, 1493 size_t len) 1494 { 1495 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1496 struct adt7316_chip_info *chip = iio_priv(dev_info); 1497 1498 return adt7316_store_DAC(chip, 0, buf, len); 1499 } 1500 1501 static IIO_DEVICE_ATTR(DAC_A, 0644, adt7316_show_DAC_A, 1502 adt7316_store_DAC_A, 0); 1503 1504 static ssize_t adt7316_show_DAC_B(struct device *dev, 1505 struct device_attribute *attr, 1506 char *buf) 1507 { 1508 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1509 struct adt7316_chip_info *chip = iio_priv(dev_info); 1510 1511 return adt7316_show_DAC(chip, 1, buf); 1512 } 1513 1514 static ssize_t adt7316_store_DAC_B(struct device *dev, 1515 struct device_attribute *attr, 1516 const char *buf, 1517 size_t len) 1518 { 1519 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1520 struct adt7316_chip_info *chip = iio_priv(dev_info); 1521 1522 return adt7316_store_DAC(chip, 1, buf, len); 1523 } 1524 1525 static IIO_DEVICE_ATTR(DAC_B, 0644, adt7316_show_DAC_B, 1526 adt7316_store_DAC_B, 0); 1527 1528 static ssize_t adt7316_show_DAC_C(struct device *dev, 1529 struct device_attribute *attr, 1530 char *buf) 1531 { 1532 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1533 struct adt7316_chip_info *chip = iio_priv(dev_info); 1534 1535 return adt7316_show_DAC(chip, 2, buf); 1536 } 1537 1538 static ssize_t adt7316_store_DAC_C(struct device *dev, 1539 struct device_attribute *attr, 1540 const char *buf, 1541 size_t len) 1542 { 1543 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1544 struct adt7316_chip_info *chip = iio_priv(dev_info); 1545 1546 return adt7316_store_DAC(chip, 2, buf, len); 1547 } 1548 1549 static IIO_DEVICE_ATTR(DAC_C, 0644, adt7316_show_DAC_C, 1550 adt7316_store_DAC_C, 0); 1551 1552 static ssize_t adt7316_show_DAC_D(struct device *dev, 1553 struct device_attribute *attr, 1554 char *buf) 1555 { 1556 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1557 struct adt7316_chip_info *chip = iio_priv(dev_info); 1558 1559 return adt7316_show_DAC(chip, 3, buf); 1560 } 1561 1562 static ssize_t adt7316_store_DAC_D(struct device *dev, 1563 struct device_attribute *attr, 1564 const char *buf, 1565 size_t len) 1566 { 1567 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1568 struct adt7316_chip_info *chip = iio_priv(dev_info); 1569 1570 return adt7316_store_DAC(chip, 3, buf, len); 1571 } 1572 1573 static IIO_DEVICE_ATTR(DAC_D, 0644, adt7316_show_DAC_D, 1574 adt7316_store_DAC_D, 0); 1575 1576 static ssize_t adt7316_show_device_id(struct device *dev, 1577 struct device_attribute *attr, 1578 char *buf) 1579 { 1580 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1581 struct adt7316_chip_info *chip = iio_priv(dev_info); 1582 u8 id; 1583 int ret; 1584 1585 ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_ID, &id); 1586 if (ret) 1587 return -EIO; 1588 1589 return sprintf(buf, "%d\n", id); 1590 } 1591 1592 static IIO_DEVICE_ATTR(device_id, 0444, adt7316_show_device_id, NULL, 0); 1593 1594 static ssize_t adt7316_show_manufactorer_id(struct device *dev, 1595 struct device_attribute *attr, 1596 char *buf) 1597 { 1598 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1599 struct adt7316_chip_info *chip = iio_priv(dev_info); 1600 u8 id; 1601 int ret; 1602 1603 ret = chip->bus.read(chip->bus.client, ADT7316_MANUFACTURE_ID, &id); 1604 if (ret) 1605 return -EIO; 1606 1607 return sprintf(buf, "%d\n", id); 1608 } 1609 1610 static IIO_DEVICE_ATTR(manufactorer_id, 0444, 1611 adt7316_show_manufactorer_id, NULL, 0); 1612 1613 static ssize_t adt7316_show_device_rev(struct device *dev, 1614 struct device_attribute *attr, 1615 char *buf) 1616 { 1617 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1618 struct adt7316_chip_info *chip = iio_priv(dev_info); 1619 u8 rev; 1620 int ret; 1621 1622 ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_REV, &rev); 1623 if (ret) 1624 return -EIO; 1625 1626 return sprintf(buf, "%d\n", rev); 1627 } 1628 1629 static IIO_DEVICE_ATTR(device_rev, 0444, adt7316_show_device_rev, NULL, 0); 1630 1631 static ssize_t adt7316_show_bus_type(struct device *dev, 1632 struct device_attribute *attr, 1633 char *buf) 1634 { 1635 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1636 struct adt7316_chip_info *chip = iio_priv(dev_info); 1637 u8 stat; 1638 int ret; 1639 1640 ret = chip->bus.read(chip->bus.client, ADT7316_SPI_LOCK_STAT, &stat); 1641 if (ret) 1642 return -EIO; 1643 1644 if (stat) 1645 return sprintf(buf, "spi\n"); 1646 1647 return sprintf(buf, "i2c\n"); 1648 } 1649 1650 static IIO_DEVICE_ATTR(bus_type, 0444, adt7316_show_bus_type, NULL, 0); 1651 1652 static struct attribute *adt7316_attributes[] = { 1653 &iio_dev_attr_all_modes.dev_attr.attr, 1654 &iio_dev_attr_mode.dev_attr.attr, 1655 &iio_dev_attr_enabled.dev_attr.attr, 1656 &iio_dev_attr_ad_channel.dev_attr.attr, 1657 &iio_dev_attr_all_ad_channels.dev_attr.attr, 1658 &iio_dev_attr_disable_averaging.dev_attr.attr, 1659 &iio_dev_attr_enable_smbus_timeout.dev_attr.attr, 1660 &iio_dev_attr_powerdown.dev_attr.attr, 1661 &iio_dev_attr_fast_ad_clock.dev_attr.attr, 1662 &iio_dev_attr_da_high_resolution.dev_attr.attr, 1663 &iio_dev_attr_enable_proportion_DACA.dev_attr.attr, 1664 &iio_dev_attr_enable_proportion_DACB.dev_attr.attr, 1665 &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr, 1666 &iio_dev_attr_DAC_update_mode.dev_attr.attr, 1667 &iio_dev_attr_all_DAC_update_modes.dev_attr.attr, 1668 &iio_dev_attr_update_DAC.dev_attr.attr, 1669 &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr, 1670 &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr, 1671 &iio_dev_attr_DAC_internal_Vref.dev_attr.attr, 1672 &iio_dev_attr_VDD.dev_attr.attr, 1673 &iio_dev_attr_in_temp.dev_attr.attr, 1674 &iio_dev_attr_ex_temp.dev_attr.attr, 1675 &iio_dev_attr_in_temp_offset.dev_attr.attr, 1676 &iio_dev_attr_ex_temp_offset.dev_attr.attr, 1677 &iio_dev_attr_in_analog_temp_offset.dev_attr.attr, 1678 &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr, 1679 &iio_dev_attr_DAC_A.dev_attr.attr, 1680 &iio_dev_attr_DAC_B.dev_attr.attr, 1681 &iio_dev_attr_DAC_C.dev_attr.attr, 1682 &iio_dev_attr_DAC_D.dev_attr.attr, 1683 &iio_dev_attr_device_id.dev_attr.attr, 1684 &iio_dev_attr_manufactorer_id.dev_attr.attr, 1685 &iio_dev_attr_device_rev.dev_attr.attr, 1686 &iio_dev_attr_bus_type.dev_attr.attr, 1687 NULL, 1688 }; 1689 1690 static const struct attribute_group adt7316_attribute_group = { 1691 .attrs = adt7316_attributes, 1692 }; 1693 1694 static struct attribute *adt7516_attributes[] = { 1695 &iio_dev_attr_all_modes.dev_attr.attr, 1696 &iio_dev_attr_mode.dev_attr.attr, 1697 &iio_dev_attr_select_ex_temp.dev_attr.attr, 1698 &iio_dev_attr_enabled.dev_attr.attr, 1699 &iio_dev_attr_ad_channel.dev_attr.attr, 1700 &iio_dev_attr_all_ad_channels.dev_attr.attr, 1701 &iio_dev_attr_disable_averaging.dev_attr.attr, 1702 &iio_dev_attr_enable_smbus_timeout.dev_attr.attr, 1703 &iio_dev_attr_powerdown.dev_attr.attr, 1704 &iio_dev_attr_fast_ad_clock.dev_attr.attr, 1705 &iio_dev_attr_AIN_internal_Vref.dev_attr.attr, 1706 &iio_dev_attr_da_high_resolution.dev_attr.attr, 1707 &iio_dev_attr_enable_proportion_DACA.dev_attr.attr, 1708 &iio_dev_attr_enable_proportion_DACB.dev_attr.attr, 1709 &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr, 1710 &iio_dev_attr_DAC_update_mode.dev_attr.attr, 1711 &iio_dev_attr_all_DAC_update_modes.dev_attr.attr, 1712 &iio_dev_attr_update_DAC.dev_attr.attr, 1713 &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr, 1714 &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr, 1715 &iio_dev_attr_DAC_internal_Vref.dev_attr.attr, 1716 &iio_dev_attr_VDD.dev_attr.attr, 1717 &iio_dev_attr_in_temp.dev_attr.attr, 1718 &iio_dev_attr_ex_temp_AIN1.dev_attr.attr, 1719 &iio_dev_attr_AIN2.dev_attr.attr, 1720 &iio_dev_attr_AIN3.dev_attr.attr, 1721 &iio_dev_attr_AIN4.dev_attr.attr, 1722 &iio_dev_attr_in_temp_offset.dev_attr.attr, 1723 &iio_dev_attr_ex_temp_offset.dev_attr.attr, 1724 &iio_dev_attr_in_analog_temp_offset.dev_attr.attr, 1725 &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr, 1726 &iio_dev_attr_DAC_A.dev_attr.attr, 1727 &iio_dev_attr_DAC_B.dev_attr.attr, 1728 &iio_dev_attr_DAC_C.dev_attr.attr, 1729 &iio_dev_attr_DAC_D.dev_attr.attr, 1730 &iio_dev_attr_device_id.dev_attr.attr, 1731 &iio_dev_attr_manufactorer_id.dev_attr.attr, 1732 &iio_dev_attr_device_rev.dev_attr.attr, 1733 &iio_dev_attr_bus_type.dev_attr.attr, 1734 NULL, 1735 }; 1736 1737 static const struct attribute_group adt7516_attribute_group = { 1738 .attrs = adt7516_attributes, 1739 }; 1740 1741 static irqreturn_t adt7316_event_handler(int irq, void *private) 1742 { 1743 struct iio_dev *indio_dev = private; 1744 struct adt7316_chip_info *chip = iio_priv(indio_dev); 1745 u8 stat1, stat2; 1746 int ret; 1747 s64 time; 1748 1749 ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT1, &stat1); 1750 if (!ret) { 1751 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX) 1752 stat1 &= 0x1F; 1753 1754 time = iio_get_time_ns(indio_dev); 1755 if (stat1 & BIT(0)) 1756 iio_push_event(indio_dev, 1757 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0, 1758 IIO_EV_TYPE_THRESH, 1759 IIO_EV_DIR_RISING), 1760 time); 1761 if (stat1 & BIT(1)) 1762 iio_push_event(indio_dev, 1763 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0, 1764 IIO_EV_TYPE_THRESH, 1765 IIO_EV_DIR_FALLING), 1766 time); 1767 if (stat1 & BIT(2)) 1768 iio_push_event(indio_dev, 1769 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1, 1770 IIO_EV_TYPE_THRESH, 1771 IIO_EV_DIR_RISING), 1772 time); 1773 if (stat1 & BIT(3)) 1774 iio_push_event(indio_dev, 1775 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1, 1776 IIO_EV_TYPE_THRESH, 1777 IIO_EV_DIR_FALLING), 1778 time); 1779 if (stat1 & BIT(5)) 1780 iio_push_event(indio_dev, 1781 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1, 1782 IIO_EV_TYPE_THRESH, 1783 IIO_EV_DIR_EITHER), 1784 time); 1785 if (stat1 & BIT(6)) 1786 iio_push_event(indio_dev, 1787 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2, 1788 IIO_EV_TYPE_THRESH, 1789 IIO_EV_DIR_EITHER), 1790 time); 1791 if (stat1 & BIT(7)) 1792 iio_push_event(indio_dev, 1793 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3, 1794 IIO_EV_TYPE_THRESH, 1795 IIO_EV_DIR_EITHER), 1796 time); 1797 } 1798 ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT2, &stat2); 1799 if (!ret) { 1800 if (stat2 & ADT7316_INT_MASK2_VDD) 1801 iio_push_event(indio_dev, 1802 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1803 0, 1804 IIO_EV_TYPE_THRESH, 1805 IIO_EV_DIR_RISING), 1806 iio_get_time_ns(indio_dev)); 1807 } 1808 1809 return IRQ_HANDLED; 1810 } 1811 1812 /* 1813 * Show mask of enabled interrupts in Hex. 1814 */ 1815 static ssize_t adt7316_show_int_mask(struct device *dev, 1816 struct device_attribute *attr, 1817 char *buf) 1818 { 1819 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1820 struct adt7316_chip_info *chip = iio_priv(dev_info); 1821 1822 return sprintf(buf, "0x%x\n", chip->int_mask); 1823 } 1824 1825 /* 1826 * Set 1 to the mask in Hex to enabled interrupts. 1827 */ 1828 static ssize_t adt7316_set_int_mask(struct device *dev, 1829 struct device_attribute *attr, 1830 const char *buf, 1831 size_t len) 1832 { 1833 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1834 struct adt7316_chip_info *chip = iio_priv(dev_info); 1835 u16 data; 1836 int ret; 1837 u8 mask; 1838 1839 ret = kstrtou16(buf, 16, &data); 1840 if (ret || data >= ADT7316_VDD_INT_MASK + 1) 1841 return -EINVAL; 1842 1843 if (data & ADT7316_VDD_INT_MASK) 1844 mask = 0; /* enable vdd int */ 1845 else 1846 mask = ADT7316_INT_MASK2_VDD; /* disable vdd int */ 1847 1848 ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK2, mask); 1849 if (!ret) { 1850 chip->int_mask &= ~ADT7316_VDD_INT_MASK; 1851 chip->int_mask |= data & ADT7316_VDD_INT_MASK; 1852 } 1853 1854 if (data & ADT7316_TEMP_AIN_INT_MASK) { 1855 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX) 1856 /* mask in reg is opposite, set 1 to disable */ 1857 mask = (~data) & ADT7316_TEMP_INT_MASK; 1858 else 1859 /* mask in reg is opposite, set 1 to disable */ 1860 mask = (~data) & ADT7316_TEMP_AIN_INT_MASK; 1861 } 1862 ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK1, mask); 1863 1864 chip->int_mask = mask; 1865 1866 return len; 1867 } 1868 1869 static inline ssize_t adt7316_show_ad_bound(struct device *dev, 1870 struct device_attribute *attr, 1871 char *buf) 1872 { 1873 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 1874 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1875 struct adt7316_chip_info *chip = iio_priv(dev_info); 1876 u8 val; 1877 int data; 1878 int ret; 1879 1880 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX && 1881 this_attr->address > ADT7316_EX_TEMP_LOW) 1882 return -EPERM; 1883 1884 ret = chip->bus.read(chip->bus.client, this_attr->address, &val); 1885 if (ret) 1886 return -EIO; 1887 1888 data = (int)val; 1889 1890 if (!((chip->id & ID_FAMILY_MASK) == ID_ADT75XX && 1891 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)) { 1892 if (data & 0x80) 1893 data -= 256; 1894 } 1895 1896 return sprintf(buf, "%d\n", data); 1897 } 1898 1899 static inline ssize_t adt7316_set_ad_bound(struct device *dev, 1900 struct device_attribute *attr, 1901 const char *buf, 1902 size_t len) 1903 { 1904 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 1905 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1906 struct adt7316_chip_info *chip = iio_priv(dev_info); 1907 int data; 1908 u8 val; 1909 int ret; 1910 1911 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX && 1912 this_attr->address > ADT7316_EX_TEMP_LOW) 1913 return -EPERM; 1914 1915 ret = kstrtoint(buf, 10, &data); 1916 if (ret) 1917 return -EINVAL; 1918 1919 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX && 1920 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0) { 1921 if (data > 255 || data < 0) 1922 return -EINVAL; 1923 } else { 1924 if (data > 127 || data < -128) 1925 return -EINVAL; 1926 1927 if (data < 0) 1928 data += 256; 1929 } 1930 1931 val = (u8)data; 1932 1933 ret = chip->bus.write(chip->bus.client, this_attr->address, val); 1934 if (ret) 1935 return -EIO; 1936 1937 return len; 1938 } 1939 1940 static ssize_t adt7316_show_int_enabled(struct device *dev, 1941 struct device_attribute *attr, 1942 char *buf) 1943 { 1944 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1945 struct adt7316_chip_info *chip = iio_priv(dev_info); 1946 1947 return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_INT_EN)); 1948 } 1949 1950 static ssize_t adt7316_set_int_enabled(struct device *dev, 1951 struct device_attribute *attr, 1952 const char *buf, 1953 size_t len) 1954 { 1955 struct iio_dev *dev_info = dev_to_iio_dev(dev); 1956 struct adt7316_chip_info *chip = iio_priv(dev_info); 1957 u8 config1; 1958 int ret; 1959 1960 config1 = chip->config1 & (~ADT7316_INT_EN); 1961 if (buf[0] == '1') 1962 config1 |= ADT7316_INT_EN; 1963 1964 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1); 1965 if (ret) 1966 return -EIO; 1967 1968 chip->config1 = config1; 1969 1970 return len; 1971 } 1972 1973 static IIO_DEVICE_ATTR(int_mask, 1974 0644, 1975 adt7316_show_int_mask, adt7316_set_int_mask, 1976 0); 1977 static IIO_DEVICE_ATTR(in_temp_high_value, 1978 0644, 1979 adt7316_show_ad_bound, adt7316_set_ad_bound, 1980 ADT7316_IN_TEMP_HIGH); 1981 static IIO_DEVICE_ATTR(in_temp_low_value, 1982 0644, 1983 adt7316_show_ad_bound, adt7316_set_ad_bound, 1984 ADT7316_IN_TEMP_LOW); 1985 static IIO_DEVICE_ATTR(ex_temp_high_value, 1986 0644, 1987 adt7316_show_ad_bound, adt7316_set_ad_bound, 1988 ADT7316_EX_TEMP_HIGH); 1989 static IIO_DEVICE_ATTR(ex_temp_low_value, 1990 0644, 1991 adt7316_show_ad_bound, adt7316_set_ad_bound, 1992 ADT7316_EX_TEMP_LOW); 1993 1994 /* NASTY duplication to be fixed */ 1995 static IIO_DEVICE_ATTR(ex_temp_ain1_high_value, 1996 0644, 1997 adt7316_show_ad_bound, adt7316_set_ad_bound, 1998 ADT7316_EX_TEMP_HIGH); 1999 static IIO_DEVICE_ATTR(ex_temp_ain1_low_value, 2000 0644, 2001 adt7316_show_ad_bound, adt7316_set_ad_bound, 2002 ADT7316_EX_TEMP_LOW); 2003 static IIO_DEVICE_ATTR(ain2_high_value, 2004 0644, 2005 adt7316_show_ad_bound, adt7316_set_ad_bound, 2006 ADT7516_AIN2_HIGH); 2007 static IIO_DEVICE_ATTR(ain2_low_value, 2008 0644, 2009 adt7316_show_ad_bound, adt7316_set_ad_bound, 2010 ADT7516_AIN2_LOW); 2011 static IIO_DEVICE_ATTR(ain3_high_value, 2012 0644, 2013 adt7316_show_ad_bound, adt7316_set_ad_bound, 2014 ADT7516_AIN3_HIGH); 2015 static IIO_DEVICE_ATTR(ain3_low_value, 2016 0644, 2017 adt7316_show_ad_bound, adt7316_set_ad_bound, 2018 ADT7516_AIN3_LOW); 2019 static IIO_DEVICE_ATTR(ain4_high_value, 2020 0644, 2021 adt7316_show_ad_bound, adt7316_set_ad_bound, 2022 ADT7516_AIN4_HIGH); 2023 static IIO_DEVICE_ATTR(ain4_low_value, 2024 0644, 2025 adt7316_show_ad_bound, adt7316_set_ad_bound, 2026 ADT7516_AIN4_LOW); 2027 static IIO_DEVICE_ATTR(int_enabled, 2028 0644, 2029 adt7316_show_int_enabled, 2030 adt7316_set_int_enabled, 0); 2031 2032 static struct attribute *adt7316_event_attributes[] = { 2033 &iio_dev_attr_int_mask.dev_attr.attr, 2034 &iio_dev_attr_in_temp_high_value.dev_attr.attr, 2035 &iio_dev_attr_in_temp_low_value.dev_attr.attr, 2036 &iio_dev_attr_ex_temp_high_value.dev_attr.attr, 2037 &iio_dev_attr_ex_temp_low_value.dev_attr.attr, 2038 &iio_dev_attr_int_enabled.dev_attr.attr, 2039 NULL, 2040 }; 2041 2042 static const struct attribute_group adt7316_event_attribute_group = { 2043 .attrs = adt7316_event_attributes, 2044 .name = "events", 2045 }; 2046 2047 static struct attribute *adt7516_event_attributes[] = { 2048 &iio_dev_attr_int_mask.dev_attr.attr, 2049 &iio_dev_attr_in_temp_high_value.dev_attr.attr, 2050 &iio_dev_attr_in_temp_low_value.dev_attr.attr, 2051 &iio_dev_attr_ex_temp_ain1_high_value.dev_attr.attr, 2052 &iio_dev_attr_ex_temp_ain1_low_value.dev_attr.attr, 2053 &iio_dev_attr_ain2_high_value.dev_attr.attr, 2054 &iio_dev_attr_ain2_low_value.dev_attr.attr, 2055 &iio_dev_attr_ain3_high_value.dev_attr.attr, 2056 &iio_dev_attr_ain3_low_value.dev_attr.attr, 2057 &iio_dev_attr_ain4_high_value.dev_attr.attr, 2058 &iio_dev_attr_ain4_low_value.dev_attr.attr, 2059 &iio_dev_attr_int_enabled.dev_attr.attr, 2060 NULL, 2061 }; 2062 2063 static const struct attribute_group adt7516_event_attribute_group = { 2064 .attrs = adt7516_event_attributes, 2065 .name = "events", 2066 }; 2067 2068 #ifdef CONFIG_PM_SLEEP 2069 static int adt7316_disable(struct device *dev) 2070 { 2071 struct iio_dev *dev_info = dev_get_drvdata(dev); 2072 struct adt7316_chip_info *chip = iio_priv(dev_info); 2073 2074 return _adt7316_store_enabled(chip, 0); 2075 } 2076 2077 static int adt7316_enable(struct device *dev) 2078 { 2079 struct iio_dev *dev_info = dev_get_drvdata(dev); 2080 struct adt7316_chip_info *chip = iio_priv(dev_info); 2081 2082 return _adt7316_store_enabled(chip, 1); 2083 } 2084 EXPORT_SYMBOL_GPL(adt7316_pm_ops); 2085 SIMPLE_DEV_PM_OPS(adt7316_pm_ops, adt7316_disable, adt7316_enable); 2086 #endif 2087 2088 static const struct iio_info adt7316_info = { 2089 .attrs = &adt7316_attribute_group, 2090 .event_attrs = &adt7316_event_attribute_group, 2091 }; 2092 2093 static const struct iio_info adt7516_info = { 2094 .attrs = &adt7516_attribute_group, 2095 .event_attrs = &adt7516_event_attribute_group, 2096 }; 2097 2098 /* 2099 * device probe and remove 2100 */ 2101 int adt7316_probe(struct device *dev, struct adt7316_bus *bus, 2102 const char *name) 2103 { 2104 struct adt7316_chip_info *chip; 2105 struct iio_dev *indio_dev; 2106 unsigned short *adt7316_platform_data = dev->platform_data; 2107 int irq_type = IRQF_TRIGGER_LOW; 2108 int ret = 0; 2109 2110 indio_dev = devm_iio_device_alloc(dev, sizeof(*chip)); 2111 if (!indio_dev) 2112 return -ENOMEM; 2113 chip = iio_priv(indio_dev); 2114 /* this is only used for device removal purposes */ 2115 dev_set_drvdata(dev, indio_dev); 2116 2117 chip->bus = *bus; 2118 2119 if (name[4] == '3') 2120 chip->id = ID_ADT7316 + (name[6] - '6'); 2121 else if (name[4] == '5') 2122 chip->id = ID_ADT7516 + (name[6] - '6'); 2123 else 2124 return -ENODEV; 2125 2126 chip->ldac_pin = devm_gpiod_get_optional(dev, "adi,ldac", GPIOD_OUT_LOW); 2127 if (IS_ERR(chip->ldac_pin)) { 2128 ret = PTR_ERR(chip->ldac_pin); 2129 dev_err(dev, "Failed to request ldac GPIO: %d\n", ret); 2130 return ret; 2131 } 2132 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 irq_type = adt7316_platform_data[0]; 2153 2154 ret = devm_request_threaded_irq(dev, chip->bus.irq, 2155 NULL, 2156 adt7316_event_handler, 2157 irq_type | IRQF_ONESHOT, 2158 indio_dev->name, 2159 indio_dev); 2160 if (ret) 2161 return ret; 2162 2163 if (irq_type & IRQF_TRIGGER_HIGH) 2164 chip->config1 |= ADT7316_INT_POLARITY; 2165 } 2166 2167 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, chip->config1); 2168 if (ret) 2169 return -EIO; 2170 2171 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, chip->config3); 2172 if (ret) 2173 return -EIO; 2174 2175 ret = devm_iio_device_register(dev, indio_dev); 2176 if (ret) 2177 return ret; 2178 2179 dev_info(dev, "%s temperature sensor, ADC and DAC registered.\n", 2180 indio_dev->name); 2181 2182 return 0; 2183 } 2184 EXPORT_SYMBOL(adt7316_probe); 2185 2186 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>"); 2187 MODULE_DESCRIPTION("Analog Devices ADT7316/7/8 and ADT7516/7/9 digital temperature sensor, ADC and DAC driver"); 2188 MODULE_LICENSE("GPL v2"); 2189