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