1 /* 2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives 3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc. 4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> 5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com> 6 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de> 7 * 8 * Derived from the lm83 driver by Jean Delvare 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 */ 14 15 #include <linux/module.h> 16 #include <linux/of_device.h> 17 #include <linux/init.h> 18 #include <linux/slab.h> 19 #include <linux/i2c.h> 20 #include <linux/hwmon.h> 21 #include <linux/hwmon-sysfs.h> 22 #include <linux/hwmon-vid.h> 23 #include <linux/err.h> 24 #include <linux/jiffies.h> 25 #include <linux/util_macros.h> 26 27 /* Indexes for the sysfs hooks */ 28 29 #define INPUT 0 30 #define MIN 1 31 #define MAX 2 32 #define CONTROL 3 33 #define OFFSET 3 34 #define AUTOMIN 4 35 #define THERM 5 36 #define HYSTERSIS 6 37 38 /* 39 * These are unique identifiers for the sysfs functions - unlike the 40 * numbers above, these are not also indexes into an array 41 */ 42 43 #define ALARM 9 44 #define FAULT 10 45 46 /* 7475 Common Registers */ 47 48 #define REG_DEVREV2 0x12 /* ADT7490 only */ 49 50 #define REG_VTT 0x1E /* ADT7490 only */ 51 #define REG_EXTEND3 0x1F /* ADT7490 only */ 52 53 #define REG_VOLTAGE_BASE 0x20 54 #define REG_TEMP_BASE 0x25 55 #define REG_TACH_BASE 0x28 56 #define REG_PWM_BASE 0x30 57 #define REG_PWM_MAX_BASE 0x38 58 59 #define REG_DEVID 0x3D 60 #define REG_VENDID 0x3E 61 #define REG_DEVID2 0x3F 62 63 #define REG_CONFIG1 0x40 64 65 #define REG_STATUS1 0x41 66 #define REG_STATUS2 0x42 67 68 #define REG_VID 0x43 /* ADT7476 only */ 69 70 #define REG_VOLTAGE_MIN_BASE 0x44 71 #define REG_VOLTAGE_MAX_BASE 0x45 72 73 #define REG_TEMP_MIN_BASE 0x4E 74 #define REG_TEMP_MAX_BASE 0x4F 75 76 #define REG_TACH_MIN_BASE 0x54 77 78 #define REG_PWM_CONFIG_BASE 0x5C 79 80 #define REG_TEMP_TRANGE_BASE 0x5F 81 82 #define REG_ENHANCE_ACOUSTICS1 0x62 83 #define REG_ENHANCE_ACOUSTICS2 0x63 84 85 #define REG_PWM_MIN_BASE 0x64 86 87 #define REG_TEMP_TMIN_BASE 0x67 88 #define REG_TEMP_THERM_BASE 0x6A 89 90 #define REG_REMOTE1_HYSTERSIS 0x6D 91 #define REG_REMOTE2_HYSTERSIS 0x6E 92 93 #define REG_TEMP_OFFSET_BASE 0x70 94 95 #define REG_CONFIG2 0x73 96 97 #define REG_EXTEND1 0x76 98 #define REG_EXTEND2 0x77 99 100 #define REG_CONFIG3 0x78 101 #define REG_CONFIG5 0x7C 102 #define REG_CONFIG4 0x7D 103 104 #define REG_STATUS4 0x81 /* ADT7490 only */ 105 106 #define REG_VTT_MIN 0x84 /* ADT7490 only */ 107 #define REG_VTT_MAX 0x86 /* ADT7490 only */ 108 109 #define VID_VIDSEL 0x80 /* ADT7476 only */ 110 111 #define CONFIG2_ATTN 0x20 112 113 #define CONFIG3_SMBALERT 0x01 114 #define CONFIG3_THERM 0x02 115 116 #define CONFIG4_PINFUNC 0x03 117 #define CONFIG4_MAXDUTY 0x08 118 #define CONFIG4_ATTN_IN10 0x30 119 #define CONFIG4_ATTN_IN43 0xC0 120 121 #define CONFIG5_TWOSCOMP 0x01 122 #define CONFIG5_TEMPOFFSET 0x02 123 #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */ 124 125 /* ADT7475 Settings */ 126 127 #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */ 128 #define ADT7475_TEMP_COUNT 3 129 #define ADT7475_TACH_COUNT 4 130 #define ADT7475_PWM_COUNT 3 131 132 /* Macro to read the registers */ 133 134 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg)) 135 136 /* Macros to easily index the registers */ 137 138 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2)) 139 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2)) 140 141 #define PWM_REG(idx) (REG_PWM_BASE + (idx)) 142 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx)) 143 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx)) 144 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx)) 145 146 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx)) 147 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2)) 148 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2)) 149 150 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx)) 151 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2)) 152 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2)) 153 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx)) 154 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx)) 155 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx)) 156 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx)) 157 158 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; 159 160 enum chips { adt7473, adt7475, adt7476, adt7490 }; 161 162 static const struct i2c_device_id adt7475_id[] = { 163 { "adt7473", adt7473 }, 164 { "adt7475", adt7475 }, 165 { "adt7476", adt7476 }, 166 { "adt7490", adt7490 }, 167 { } 168 }; 169 MODULE_DEVICE_TABLE(i2c, adt7475_id); 170 171 static const struct of_device_id adt7475_of_match[] = { 172 { 173 .compatible = "adi,adt7473", 174 .data = (void *)adt7473 175 }, 176 { 177 .compatible = "adi,adt7475", 178 .data = (void *)adt7475 179 }, 180 { 181 .compatible = "adi,adt7476", 182 .data = (void *)adt7476 183 }, 184 { 185 .compatible = "adi,adt7490", 186 .data = (void *)adt7490 187 }, 188 { }, 189 }; 190 MODULE_DEVICE_TABLE(of, adt7475_of_match); 191 192 struct adt7475_data { 193 struct device *hwmon_dev; 194 struct mutex lock; 195 196 unsigned long measure_updated; 197 bool valid; 198 199 u8 config4; 200 u8 config5; 201 u8 has_voltage; 202 u8 bypass_attn; /* Bypass voltage attenuator */ 203 u8 has_pwm2:1; 204 u8 has_fan4:1; 205 u8 has_vid:1; 206 u32 alarms; 207 u16 voltage[3][6]; 208 u16 temp[7][3]; 209 u16 tach[2][4]; 210 u8 pwm[4][3]; 211 u8 range[3]; 212 u8 pwmctl[3]; 213 u8 pwmchan[3]; 214 u8 enh_acoustics[2]; 215 216 u8 vid; 217 u8 vrm; 218 }; 219 220 static struct i2c_driver adt7475_driver; 221 static struct adt7475_data *adt7475_update_device(struct device *dev); 222 static void adt7475_read_hystersis(struct i2c_client *client); 223 static void adt7475_read_pwm(struct i2c_client *client, int index); 224 225 /* Given a temp value, convert it to register value */ 226 227 static inline u16 temp2reg(struct adt7475_data *data, long val) 228 { 229 u16 ret; 230 231 if (!(data->config5 & CONFIG5_TWOSCOMP)) { 232 val = clamp_val(val, -64000, 191000); 233 ret = (val + 64500) / 1000; 234 } else { 235 val = clamp_val(val, -128000, 127000); 236 if (val < -500) 237 ret = (256500 + val) / 1000; 238 else 239 ret = (val + 500) / 1000; 240 } 241 242 return ret << 2; 243 } 244 245 /* Given a register value, convert it to a real temp value */ 246 247 static inline int reg2temp(struct adt7475_data *data, u16 reg) 248 { 249 if (data->config5 & CONFIG5_TWOSCOMP) { 250 if (reg >= 512) 251 return (reg - 1024) * 250; 252 else 253 return reg * 250; 254 } else 255 return (reg - 256) * 250; 256 } 257 258 static inline int tach2rpm(u16 tach) 259 { 260 if (tach == 0 || tach == 0xFFFF) 261 return 0; 262 263 return (90000 * 60) / tach; 264 } 265 266 static inline u16 rpm2tach(unsigned long rpm) 267 { 268 if (rpm == 0) 269 return 0; 270 271 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF); 272 } 273 274 /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */ 275 static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = { 276 { 45, 94 }, /* +2.5V */ 277 { 175, 525 }, /* Vccp */ 278 { 68, 71 }, /* Vcc */ 279 { 93, 47 }, /* +5V */ 280 { 120, 20 }, /* +12V */ 281 { 45, 45 }, /* Vtt */ 282 }; 283 284 static inline int reg2volt(int channel, u16 reg, u8 bypass_attn) 285 { 286 const int *r = adt7473_in_scaling[channel]; 287 288 if (bypass_attn & (1 << channel)) 289 return DIV_ROUND_CLOSEST(reg * 2250, 1024); 290 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024); 291 } 292 293 static inline u16 volt2reg(int channel, long volt, u8 bypass_attn) 294 { 295 const int *r = adt7473_in_scaling[channel]; 296 long reg; 297 298 if (bypass_attn & (1 << channel)) 299 reg = (volt * 1024) / 2250; 300 else 301 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250); 302 return clamp_val(reg, 0, 1023) & (0xff << 2); 303 } 304 305 static u16 adt7475_read_word(struct i2c_client *client, int reg) 306 { 307 u16 val; 308 309 val = i2c_smbus_read_byte_data(client, reg); 310 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8); 311 312 return val; 313 } 314 315 static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) 316 { 317 i2c_smbus_write_byte_data(client, reg + 1, val >> 8); 318 i2c_smbus_write_byte_data(client, reg, val & 0xFF); 319 } 320 321 static ssize_t show_voltage(struct device *dev, struct device_attribute *attr, 322 char *buf) 323 { 324 struct adt7475_data *data = adt7475_update_device(dev); 325 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 326 unsigned short val; 327 328 if (IS_ERR(data)) 329 return PTR_ERR(data); 330 331 switch (sattr->nr) { 332 case ALARM: 333 return sprintf(buf, "%d\n", 334 (data->alarms >> sattr->index) & 1); 335 default: 336 val = data->voltage[sattr->nr][sattr->index]; 337 return sprintf(buf, "%d\n", 338 reg2volt(sattr->index, val, data->bypass_attn)); 339 } 340 } 341 342 static ssize_t set_voltage(struct device *dev, struct device_attribute *attr, 343 const char *buf, size_t count) 344 { 345 346 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 347 struct i2c_client *client = to_i2c_client(dev); 348 struct adt7475_data *data = i2c_get_clientdata(client); 349 unsigned char reg; 350 long val; 351 352 if (kstrtol(buf, 10, &val)) 353 return -EINVAL; 354 355 mutex_lock(&data->lock); 356 357 data->voltage[sattr->nr][sattr->index] = 358 volt2reg(sattr->index, val, data->bypass_attn); 359 360 if (sattr->index < ADT7475_VOLTAGE_COUNT) { 361 if (sattr->nr == MIN) 362 reg = VOLTAGE_MIN_REG(sattr->index); 363 else 364 reg = VOLTAGE_MAX_REG(sattr->index); 365 } else { 366 if (sattr->nr == MIN) 367 reg = REG_VTT_MIN; 368 else 369 reg = REG_VTT_MAX; 370 } 371 372 i2c_smbus_write_byte_data(client, reg, 373 data->voltage[sattr->nr][sattr->index] >> 2); 374 mutex_unlock(&data->lock); 375 376 return count; 377 } 378 379 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, 380 char *buf) 381 { 382 struct adt7475_data *data = adt7475_update_device(dev); 383 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 384 int out; 385 386 if (IS_ERR(data)) 387 return PTR_ERR(data); 388 389 switch (sattr->nr) { 390 case HYSTERSIS: 391 mutex_lock(&data->lock); 392 out = data->temp[sattr->nr][sattr->index]; 393 if (sattr->index != 1) 394 out = (out >> 4) & 0xF; 395 else 396 out = (out & 0xF); 397 /* 398 * Show the value as an absolute number tied to 399 * THERM 400 */ 401 out = reg2temp(data, data->temp[THERM][sattr->index]) - 402 out * 1000; 403 mutex_unlock(&data->lock); 404 break; 405 406 case OFFSET: 407 /* 408 * Offset is always 2's complement, regardless of the 409 * setting in CONFIG5 410 */ 411 mutex_lock(&data->lock); 412 out = (s8)data->temp[sattr->nr][sattr->index]; 413 if (data->config5 & CONFIG5_TEMPOFFSET) 414 out *= 1000; 415 else 416 out *= 500; 417 mutex_unlock(&data->lock); 418 break; 419 420 case ALARM: 421 out = (data->alarms >> (sattr->index + 4)) & 1; 422 break; 423 424 case FAULT: 425 /* Note - only for remote1 and remote2 */ 426 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000)); 427 break; 428 429 default: 430 /* All other temp values are in the configured format */ 431 out = reg2temp(data, data->temp[sattr->nr][sattr->index]); 432 } 433 434 return sprintf(buf, "%d\n", out); 435 } 436 437 static ssize_t set_temp(struct device *dev, struct device_attribute *attr, 438 const char *buf, size_t count) 439 { 440 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 441 struct i2c_client *client = to_i2c_client(dev); 442 struct adt7475_data *data = i2c_get_clientdata(client); 443 unsigned char reg = 0; 444 u8 out; 445 int temp; 446 long val; 447 448 if (kstrtol(buf, 10, &val)) 449 return -EINVAL; 450 451 mutex_lock(&data->lock); 452 453 /* We need the config register in all cases for temp <-> reg conv. */ 454 data->config5 = adt7475_read(REG_CONFIG5); 455 456 switch (sattr->nr) { 457 case OFFSET: 458 if (data->config5 & CONFIG5_TEMPOFFSET) { 459 val = clamp_val(val, -63000, 127000); 460 out = data->temp[OFFSET][sattr->index] = val / 1000; 461 } else { 462 val = clamp_val(val, -63000, 64000); 463 out = data->temp[OFFSET][sattr->index] = val / 500; 464 } 465 break; 466 467 case HYSTERSIS: 468 /* 469 * The value will be given as an absolute value, turn it 470 * into an offset based on THERM 471 */ 472 473 /* Read fresh THERM and HYSTERSIS values from the chip */ 474 data->temp[THERM][sattr->index] = 475 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2; 476 adt7475_read_hystersis(client); 477 478 temp = reg2temp(data, data->temp[THERM][sattr->index]); 479 val = clamp_val(val, temp - 15000, temp); 480 val = (temp - val) / 1000; 481 482 if (sattr->index != 1) { 483 data->temp[HYSTERSIS][sattr->index] &= 0xF0; 484 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4; 485 } else { 486 data->temp[HYSTERSIS][sattr->index] &= 0x0F; 487 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF); 488 } 489 490 out = data->temp[HYSTERSIS][sattr->index]; 491 break; 492 493 default: 494 data->temp[sattr->nr][sattr->index] = temp2reg(data, val); 495 496 /* 497 * We maintain an extra 2 digits of precision for simplicity 498 * - shift those back off before writing the value 499 */ 500 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2); 501 } 502 503 switch (sattr->nr) { 504 case MIN: 505 reg = TEMP_MIN_REG(sattr->index); 506 break; 507 case MAX: 508 reg = TEMP_MAX_REG(sattr->index); 509 break; 510 case OFFSET: 511 reg = TEMP_OFFSET_REG(sattr->index); 512 break; 513 case AUTOMIN: 514 reg = TEMP_TMIN_REG(sattr->index); 515 break; 516 case THERM: 517 reg = TEMP_THERM_REG(sattr->index); 518 break; 519 case HYSTERSIS: 520 if (sattr->index != 2) 521 reg = REG_REMOTE1_HYSTERSIS; 522 else 523 reg = REG_REMOTE2_HYSTERSIS; 524 525 break; 526 } 527 528 i2c_smbus_write_byte_data(client, reg, out); 529 530 mutex_unlock(&data->lock); 531 return count; 532 } 533 534 /* Assuming CONFIG6[SLOW] is 0 */ 535 static const int ad7475_st_map[] = { 536 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800, 537 }; 538 539 static ssize_t show_temp_st(struct device *dev, struct device_attribute *attr, 540 char *buf) 541 { 542 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 543 struct i2c_client *client = to_i2c_client(dev); 544 struct adt7475_data *data = i2c_get_clientdata(client); 545 long val; 546 547 switch (sattr->index) { 548 case 0: 549 val = data->enh_acoustics[0] & 0xf; 550 break; 551 case 1: 552 val = (data->enh_acoustics[1] >> 4) & 0xf; 553 break; 554 case 2: 555 default: 556 val = data->enh_acoustics[1] & 0xf; 557 break; 558 } 559 560 if (val & 0x8) 561 return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]); 562 else 563 return sprintf(buf, "0\n"); 564 } 565 566 static ssize_t set_temp_st(struct device *dev, struct device_attribute *attr, 567 const char *buf, size_t count) 568 { 569 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 570 struct i2c_client *client = to_i2c_client(dev); 571 struct adt7475_data *data = i2c_get_clientdata(client); 572 unsigned char reg; 573 int shift, idx; 574 ulong val; 575 576 if (kstrtoul(buf, 10, &val)) 577 return -EINVAL; 578 579 switch (sattr->index) { 580 case 0: 581 reg = REG_ENHANCE_ACOUSTICS1; 582 shift = 0; 583 idx = 0; 584 break; 585 case 1: 586 reg = REG_ENHANCE_ACOUSTICS2; 587 shift = 0; 588 idx = 1; 589 break; 590 case 2: 591 default: 592 reg = REG_ENHANCE_ACOUSTICS2; 593 shift = 4; 594 idx = 1; 595 break; 596 } 597 598 if (val > 0) { 599 val = find_closest_descending(val, ad7475_st_map, 600 ARRAY_SIZE(ad7475_st_map)); 601 val |= 0x8; 602 } 603 604 mutex_lock(&data->lock); 605 606 data->enh_acoustics[idx] &= ~(0xf << shift); 607 data->enh_acoustics[idx] |= (val << shift); 608 609 i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]); 610 611 mutex_unlock(&data->lock); 612 613 return count; 614 } 615 616 /* 617 * Table of autorange values - the user will write the value in millidegrees, 618 * and we'll convert it 619 */ 620 static const int autorange_table[] = { 621 2000, 2500, 3330, 4000, 5000, 6670, 8000, 622 10000, 13330, 16000, 20000, 26670, 32000, 40000, 623 53330, 80000 624 }; 625 626 static ssize_t show_point2(struct device *dev, struct device_attribute *attr, 627 char *buf) 628 { 629 struct adt7475_data *data = adt7475_update_device(dev); 630 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 631 int out, val; 632 633 if (IS_ERR(data)) 634 return PTR_ERR(data); 635 636 mutex_lock(&data->lock); 637 out = (data->range[sattr->index] >> 4) & 0x0F; 638 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]); 639 mutex_unlock(&data->lock); 640 641 return sprintf(buf, "%d\n", val + autorange_table[out]); 642 } 643 644 static ssize_t set_point2(struct device *dev, struct device_attribute *attr, 645 const char *buf, size_t count) 646 { 647 struct i2c_client *client = to_i2c_client(dev); 648 struct adt7475_data *data = i2c_get_clientdata(client); 649 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 650 int temp; 651 long val; 652 653 if (kstrtol(buf, 10, &val)) 654 return -EINVAL; 655 656 mutex_lock(&data->lock); 657 658 /* Get a fresh copy of the needed registers */ 659 data->config5 = adt7475_read(REG_CONFIG5); 660 data->temp[AUTOMIN][sattr->index] = 661 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2; 662 data->range[sattr->index] = 663 adt7475_read(TEMP_TRANGE_REG(sattr->index)); 664 665 /* 666 * The user will write an absolute value, so subtract the start point 667 * to figure the range 668 */ 669 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]); 670 val = clamp_val(val, temp + autorange_table[0], 671 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]); 672 val -= temp; 673 674 /* Find the nearest table entry to what the user wrote */ 675 val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table)); 676 677 data->range[sattr->index] &= ~0xF0; 678 data->range[sattr->index] |= val << 4; 679 680 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), 681 data->range[sattr->index]); 682 683 mutex_unlock(&data->lock); 684 return count; 685 } 686 687 static ssize_t show_tach(struct device *dev, struct device_attribute *attr, 688 char *buf) 689 { 690 struct adt7475_data *data = adt7475_update_device(dev); 691 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 692 int out; 693 694 if (IS_ERR(data)) 695 return PTR_ERR(data); 696 697 if (sattr->nr == ALARM) 698 out = (data->alarms >> (sattr->index + 10)) & 1; 699 else 700 out = tach2rpm(data->tach[sattr->nr][sattr->index]); 701 702 return sprintf(buf, "%d\n", out); 703 } 704 705 static ssize_t set_tach(struct device *dev, struct device_attribute *attr, 706 const char *buf, size_t count) 707 { 708 709 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 710 struct i2c_client *client = to_i2c_client(dev); 711 struct adt7475_data *data = i2c_get_clientdata(client); 712 unsigned long val; 713 714 if (kstrtoul(buf, 10, &val)) 715 return -EINVAL; 716 717 mutex_lock(&data->lock); 718 719 data->tach[MIN][sattr->index] = rpm2tach(val); 720 721 adt7475_write_word(client, TACH_MIN_REG(sattr->index), 722 data->tach[MIN][sattr->index]); 723 724 mutex_unlock(&data->lock); 725 return count; 726 } 727 728 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, 729 char *buf) 730 { 731 struct adt7475_data *data = adt7475_update_device(dev); 732 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 733 734 if (IS_ERR(data)) 735 return PTR_ERR(data); 736 737 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]); 738 } 739 740 static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr, 741 char *buf) 742 { 743 struct adt7475_data *data = adt7475_update_device(dev); 744 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 745 746 if (IS_ERR(data)) 747 return PTR_ERR(data); 748 749 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]); 750 } 751 752 static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr, 753 char *buf) 754 { 755 struct adt7475_data *data = adt7475_update_device(dev); 756 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 757 758 if (IS_ERR(data)) 759 return PTR_ERR(data); 760 761 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]); 762 } 763 764 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, 765 const char *buf, size_t count) 766 { 767 768 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 769 struct i2c_client *client = to_i2c_client(dev); 770 struct adt7475_data *data = i2c_get_clientdata(client); 771 unsigned char reg = 0; 772 long val; 773 774 if (kstrtol(buf, 10, &val)) 775 return -EINVAL; 776 777 mutex_lock(&data->lock); 778 779 switch (sattr->nr) { 780 case INPUT: 781 /* Get a fresh value for CONTROL */ 782 data->pwm[CONTROL][sattr->index] = 783 adt7475_read(PWM_CONFIG_REG(sattr->index)); 784 785 /* 786 * If we are not in manual mode, then we shouldn't allow 787 * the user to set the pwm speed 788 */ 789 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) { 790 mutex_unlock(&data->lock); 791 return count; 792 } 793 794 reg = PWM_REG(sattr->index); 795 break; 796 797 case MIN: 798 reg = PWM_MIN_REG(sattr->index); 799 break; 800 801 case MAX: 802 reg = PWM_MAX_REG(sattr->index); 803 break; 804 } 805 806 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF); 807 i2c_smbus_write_byte_data(client, reg, 808 data->pwm[sattr->nr][sattr->index]); 809 mutex_unlock(&data->lock); 810 811 return count; 812 } 813 814 static ssize_t show_stall_disable(struct device *dev, 815 struct device_attribute *attr, char *buf) 816 { 817 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 818 struct i2c_client *client = to_i2c_client(dev); 819 struct adt7475_data *data = i2c_get_clientdata(client); 820 u8 mask = BIT(5 + sattr->index); 821 822 return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask)); 823 } 824 825 static ssize_t set_stall_disable(struct device *dev, 826 struct device_attribute *attr, const char *buf, 827 size_t count) 828 { 829 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 830 struct i2c_client *client = to_i2c_client(dev); 831 struct adt7475_data *data = i2c_get_clientdata(client); 832 long val; 833 u8 mask = BIT(5 + sattr->index); 834 835 if (kstrtol(buf, 10, &val)) 836 return -EINVAL; 837 838 mutex_lock(&data->lock); 839 840 data->enh_acoustics[0] &= ~mask; 841 if (val) 842 data->enh_acoustics[0] |= mask; 843 844 i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1, 845 data->enh_acoustics[0]); 846 847 mutex_unlock(&data->lock); 848 849 return count; 850 } 851 852 /* Called by set_pwmctrl and set_pwmchan */ 853 854 static int hw_set_pwm(struct i2c_client *client, int index, 855 unsigned int pwmctl, unsigned int pwmchan) 856 { 857 struct adt7475_data *data = i2c_get_clientdata(client); 858 long val = 0; 859 860 switch (pwmctl) { 861 case 0: 862 val = 0x03; /* Run at full speed */ 863 break; 864 case 1: 865 val = 0x07; /* Manual mode */ 866 break; 867 case 2: 868 switch (pwmchan) { 869 case 1: 870 /* Remote1 controls PWM */ 871 val = 0x00; 872 break; 873 case 2: 874 /* local controls PWM */ 875 val = 0x01; 876 break; 877 case 4: 878 /* remote2 controls PWM */ 879 val = 0x02; 880 break; 881 case 6: 882 /* local/remote2 control PWM */ 883 val = 0x05; 884 break; 885 case 7: 886 /* All three control PWM */ 887 val = 0x06; 888 break; 889 default: 890 return -EINVAL; 891 } 892 break; 893 default: 894 return -EINVAL; 895 } 896 897 data->pwmctl[index] = pwmctl; 898 data->pwmchan[index] = pwmchan; 899 900 data->pwm[CONTROL][index] &= ~0xE0; 901 data->pwm[CONTROL][index] |= (val & 7) << 5; 902 903 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 904 data->pwm[CONTROL][index]); 905 906 return 0; 907 } 908 909 static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr, 910 const char *buf, size_t count) 911 { 912 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 913 struct i2c_client *client = to_i2c_client(dev); 914 struct adt7475_data *data = i2c_get_clientdata(client); 915 int r; 916 long val; 917 918 if (kstrtol(buf, 10, &val)) 919 return -EINVAL; 920 921 mutex_lock(&data->lock); 922 /* Read Modify Write PWM values */ 923 adt7475_read_pwm(client, sattr->index); 924 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val); 925 if (r) 926 count = r; 927 mutex_unlock(&data->lock); 928 929 return count; 930 } 931 932 static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr, 933 const char *buf, size_t count) 934 { 935 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 936 struct i2c_client *client = to_i2c_client(dev); 937 struct adt7475_data *data = i2c_get_clientdata(client); 938 int r; 939 long val; 940 941 if (kstrtol(buf, 10, &val)) 942 return -EINVAL; 943 944 mutex_lock(&data->lock); 945 /* Read Modify Write PWM values */ 946 adt7475_read_pwm(client, sattr->index); 947 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]); 948 if (r) 949 count = r; 950 mutex_unlock(&data->lock); 951 952 return count; 953 } 954 955 /* List of frequencies for the PWM */ 956 static const int pwmfreq_table[] = { 957 11, 14, 22, 29, 35, 44, 58, 88, 22500 958 }; 959 960 static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr, 961 char *buf) 962 { 963 struct adt7475_data *data = adt7475_update_device(dev); 964 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 965 int idx; 966 967 if (IS_ERR(data)) 968 return PTR_ERR(data); 969 idx = clamp_val(data->range[sattr->index] & 0xf, 0, 970 ARRAY_SIZE(pwmfreq_table) - 1); 971 972 return sprintf(buf, "%d\n", pwmfreq_table[idx]); 973 } 974 975 static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr, 976 const char *buf, size_t count) 977 { 978 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 979 struct i2c_client *client = to_i2c_client(dev); 980 struct adt7475_data *data = i2c_get_clientdata(client); 981 int out; 982 long val; 983 984 if (kstrtol(buf, 10, &val)) 985 return -EINVAL; 986 987 out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table)); 988 989 mutex_lock(&data->lock); 990 991 data->range[sattr->index] = 992 adt7475_read(TEMP_TRANGE_REG(sattr->index)); 993 data->range[sattr->index] &= ~0xf; 994 data->range[sattr->index] |= out; 995 996 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), 997 data->range[sattr->index]); 998 999 mutex_unlock(&data->lock); 1000 return count; 1001 } 1002 1003 static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev, 1004 struct device_attribute *devattr, 1005 char *buf) 1006 { 1007 struct adt7475_data *data = adt7475_update_device(dev); 1008 1009 if (IS_ERR(data)) 1010 return PTR_ERR(data); 1011 1012 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY)); 1013 } 1014 1015 static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev, 1016 struct device_attribute *devattr, 1017 const char *buf, size_t count) 1018 { 1019 struct i2c_client *client = to_i2c_client(dev); 1020 struct adt7475_data *data = i2c_get_clientdata(client); 1021 long val; 1022 1023 if (kstrtol(buf, 10, &val)) 1024 return -EINVAL; 1025 if (val != 0 && val != 1) 1026 return -EINVAL; 1027 1028 mutex_lock(&data->lock); 1029 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4); 1030 if (val) 1031 data->config4 |= CONFIG4_MAXDUTY; 1032 else 1033 data->config4 &= ~CONFIG4_MAXDUTY; 1034 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4); 1035 mutex_unlock(&data->lock); 1036 1037 return count; 1038 } 1039 1040 static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr, 1041 char *buf) 1042 { 1043 struct adt7475_data *data = dev_get_drvdata(dev); 1044 return sprintf(buf, "%d\n", (int)data->vrm); 1045 } 1046 1047 static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr, 1048 const char *buf, size_t count) 1049 { 1050 struct adt7475_data *data = dev_get_drvdata(dev); 1051 long val; 1052 1053 if (kstrtol(buf, 10, &val)) 1054 return -EINVAL; 1055 if (val < 0 || val > 255) 1056 return -EINVAL; 1057 data->vrm = val; 1058 1059 return count; 1060 } 1061 1062 static ssize_t cpu0_vid_show(struct device *dev, 1063 struct device_attribute *devattr, char *buf) 1064 { 1065 struct adt7475_data *data = adt7475_update_device(dev); 1066 1067 if (IS_ERR(data)) 1068 return PTR_ERR(data); 1069 1070 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); 1071 } 1072 1073 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0); 1074 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage, 1075 set_voltage, MAX, 0); 1076 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage, 1077 set_voltage, MIN, 0); 1078 static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0); 1079 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1); 1080 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage, 1081 set_voltage, MAX, 1); 1082 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage, 1083 set_voltage, MIN, 1); 1084 static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1); 1085 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2); 1086 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage, 1087 set_voltage, MAX, 2); 1088 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage, 1089 set_voltage, MIN, 2); 1090 static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2); 1091 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3); 1092 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage, 1093 set_voltage, MAX, 3); 1094 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage, 1095 set_voltage, MIN, 3); 1096 static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3); 1097 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4); 1098 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage, 1099 set_voltage, MAX, 4); 1100 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage, 1101 set_voltage, MIN, 4); 1102 static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8); 1103 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5); 1104 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage, 1105 set_voltage, MAX, 5); 1106 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage, 1107 set_voltage, MIN, 5); 1108 static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31); 1109 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0); 1110 static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0); 1111 static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0); 1112 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 1113 MAX, 0); 1114 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 1115 MIN, 0); 1116 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp, 1117 set_temp, OFFSET, 0); 1118 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR, 1119 show_temp, set_temp, AUTOMIN, 0); 1120 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR, 1121 show_point2, set_point2, 0, 0); 1122 static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 1123 THERM, 0); 1124 static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 1125 set_temp, HYSTERSIS, 0); 1126 static SENSOR_DEVICE_ATTR_2(temp1_smoothing, S_IRUGO | S_IWUSR, show_temp_st, 1127 set_temp_st, 0, 0); 1128 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1); 1129 static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1); 1130 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 1131 MAX, 1); 1132 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 1133 MIN, 1); 1134 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp, 1135 set_temp, OFFSET, 1); 1136 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR, 1137 show_temp, set_temp, AUTOMIN, 1); 1138 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR, 1139 show_point2, set_point2, 0, 1); 1140 static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 1141 THERM, 1); 1142 static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 1143 set_temp, HYSTERSIS, 1); 1144 static SENSOR_DEVICE_ATTR_2(temp2_smoothing, S_IRUGO | S_IWUSR, show_temp_st, 1145 set_temp_st, 0, 1); 1146 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2); 1147 static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2); 1148 static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2); 1149 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 1150 MAX, 2); 1151 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 1152 MIN, 2); 1153 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp, 1154 set_temp, OFFSET, 2); 1155 static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR, 1156 show_temp, set_temp, AUTOMIN, 2); 1157 static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR, 1158 show_point2, set_point2, 0, 2); 1159 static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 1160 THERM, 2); 1161 static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 1162 set_temp, HYSTERSIS, 2); 1163 static SENSOR_DEVICE_ATTR_2(temp3_smoothing, S_IRUGO | S_IWUSR, show_temp_st, 1164 set_temp_st, 0, 2); 1165 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0); 1166 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 1167 MIN, 0); 1168 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0); 1169 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1); 1170 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 1171 MIN, 1); 1172 static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1); 1173 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2); 1174 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 1175 MIN, 2); 1176 static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2); 1177 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3); 1178 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 1179 MIN, 3); 1180 static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3); 1181 static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 1182 0); 1183 static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 1184 set_pwmfreq, INPUT, 0); 1185 static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 1186 set_pwmctrl, INPUT, 0); 1187 static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR, 1188 show_pwmchan, set_pwmchan, INPUT, 0); 1189 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 1190 set_pwm, MIN, 0); 1191 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 1192 set_pwm, MAX, 0); 1193 static SENSOR_DEVICE_ATTR_2(pwm1_stall_disable, S_IRUGO | S_IWUSR, 1194 show_stall_disable, set_stall_disable, 0, 0); 1195 static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 1196 1); 1197 static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 1198 set_pwmfreq, INPUT, 1); 1199 static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 1200 set_pwmctrl, INPUT, 1); 1201 static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR, 1202 show_pwmchan, set_pwmchan, INPUT, 1); 1203 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 1204 set_pwm, MIN, 1); 1205 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 1206 set_pwm, MAX, 1); 1207 static SENSOR_DEVICE_ATTR_2(pwm2_stall_disable, S_IRUGO | S_IWUSR, 1208 show_stall_disable, set_stall_disable, 0, 1); 1209 static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 1210 2); 1211 static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 1212 set_pwmfreq, INPUT, 2); 1213 static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 1214 set_pwmctrl, INPUT, 2); 1215 static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR, 1216 show_pwmchan, set_pwmchan, INPUT, 2); 1217 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 1218 set_pwm, MIN, 2); 1219 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 1220 set_pwm, MAX, 2); 1221 static SENSOR_DEVICE_ATTR_2(pwm3_stall_disable, S_IRUGO | S_IWUSR, 1222 show_stall_disable, set_stall_disable, 0, 2); 1223 1224 /* Non-standard name, might need revisiting */ 1225 static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit); 1226 1227 static DEVICE_ATTR_RW(vrm); 1228 static DEVICE_ATTR_RO(cpu0_vid); 1229 1230 static struct attribute *adt7475_attrs[] = { 1231 &sensor_dev_attr_in1_input.dev_attr.attr, 1232 &sensor_dev_attr_in1_max.dev_attr.attr, 1233 &sensor_dev_attr_in1_min.dev_attr.attr, 1234 &sensor_dev_attr_in1_alarm.dev_attr.attr, 1235 &sensor_dev_attr_in2_input.dev_attr.attr, 1236 &sensor_dev_attr_in2_max.dev_attr.attr, 1237 &sensor_dev_attr_in2_min.dev_attr.attr, 1238 &sensor_dev_attr_in2_alarm.dev_attr.attr, 1239 &sensor_dev_attr_temp1_input.dev_attr.attr, 1240 &sensor_dev_attr_temp1_alarm.dev_attr.attr, 1241 &sensor_dev_attr_temp1_fault.dev_attr.attr, 1242 &sensor_dev_attr_temp1_max.dev_attr.attr, 1243 &sensor_dev_attr_temp1_min.dev_attr.attr, 1244 &sensor_dev_attr_temp1_offset.dev_attr.attr, 1245 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, 1246 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, 1247 &sensor_dev_attr_temp1_crit.dev_attr.attr, 1248 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 1249 &sensor_dev_attr_temp1_smoothing.dev_attr.attr, 1250 &sensor_dev_attr_temp2_input.dev_attr.attr, 1251 &sensor_dev_attr_temp2_alarm.dev_attr.attr, 1252 &sensor_dev_attr_temp2_max.dev_attr.attr, 1253 &sensor_dev_attr_temp2_min.dev_attr.attr, 1254 &sensor_dev_attr_temp2_offset.dev_attr.attr, 1255 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, 1256 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, 1257 &sensor_dev_attr_temp2_crit.dev_attr.attr, 1258 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 1259 &sensor_dev_attr_temp2_smoothing.dev_attr.attr, 1260 &sensor_dev_attr_temp3_input.dev_attr.attr, 1261 &sensor_dev_attr_temp3_fault.dev_attr.attr, 1262 &sensor_dev_attr_temp3_alarm.dev_attr.attr, 1263 &sensor_dev_attr_temp3_max.dev_attr.attr, 1264 &sensor_dev_attr_temp3_min.dev_attr.attr, 1265 &sensor_dev_attr_temp3_offset.dev_attr.attr, 1266 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, 1267 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, 1268 &sensor_dev_attr_temp3_crit.dev_attr.attr, 1269 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 1270 &sensor_dev_attr_temp3_smoothing.dev_attr.attr, 1271 &sensor_dev_attr_fan1_input.dev_attr.attr, 1272 &sensor_dev_attr_fan1_min.dev_attr.attr, 1273 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 1274 &sensor_dev_attr_fan2_input.dev_attr.attr, 1275 &sensor_dev_attr_fan2_min.dev_attr.attr, 1276 &sensor_dev_attr_fan2_alarm.dev_attr.attr, 1277 &sensor_dev_attr_fan3_input.dev_attr.attr, 1278 &sensor_dev_attr_fan3_min.dev_attr.attr, 1279 &sensor_dev_attr_fan3_alarm.dev_attr.attr, 1280 &sensor_dev_attr_pwm1.dev_attr.attr, 1281 &sensor_dev_attr_pwm1_freq.dev_attr.attr, 1282 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 1283 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, 1284 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 1285 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 1286 &sensor_dev_attr_pwm1_stall_disable.dev_attr.attr, 1287 &sensor_dev_attr_pwm3.dev_attr.attr, 1288 &sensor_dev_attr_pwm3_freq.dev_attr.attr, 1289 &sensor_dev_attr_pwm3_enable.dev_attr.attr, 1290 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, 1291 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 1292 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, 1293 &sensor_dev_attr_pwm3_stall_disable.dev_attr.attr, 1294 &dev_attr_pwm_use_point2_pwm_at_crit.attr, 1295 NULL, 1296 }; 1297 1298 static struct attribute *fan4_attrs[] = { 1299 &sensor_dev_attr_fan4_input.dev_attr.attr, 1300 &sensor_dev_attr_fan4_min.dev_attr.attr, 1301 &sensor_dev_attr_fan4_alarm.dev_attr.attr, 1302 NULL 1303 }; 1304 1305 static struct attribute *pwm2_attrs[] = { 1306 &sensor_dev_attr_pwm2.dev_attr.attr, 1307 &sensor_dev_attr_pwm2_freq.dev_attr.attr, 1308 &sensor_dev_attr_pwm2_enable.dev_attr.attr, 1309 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, 1310 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 1311 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, 1312 &sensor_dev_attr_pwm2_stall_disable.dev_attr.attr, 1313 NULL 1314 }; 1315 1316 static struct attribute *in0_attrs[] = { 1317 &sensor_dev_attr_in0_input.dev_attr.attr, 1318 &sensor_dev_attr_in0_max.dev_attr.attr, 1319 &sensor_dev_attr_in0_min.dev_attr.attr, 1320 &sensor_dev_attr_in0_alarm.dev_attr.attr, 1321 NULL 1322 }; 1323 1324 static struct attribute *in3_attrs[] = { 1325 &sensor_dev_attr_in3_input.dev_attr.attr, 1326 &sensor_dev_attr_in3_max.dev_attr.attr, 1327 &sensor_dev_attr_in3_min.dev_attr.attr, 1328 &sensor_dev_attr_in3_alarm.dev_attr.attr, 1329 NULL 1330 }; 1331 1332 static struct attribute *in4_attrs[] = { 1333 &sensor_dev_attr_in4_input.dev_attr.attr, 1334 &sensor_dev_attr_in4_max.dev_attr.attr, 1335 &sensor_dev_attr_in4_min.dev_attr.attr, 1336 &sensor_dev_attr_in4_alarm.dev_attr.attr, 1337 NULL 1338 }; 1339 1340 static struct attribute *in5_attrs[] = { 1341 &sensor_dev_attr_in5_input.dev_attr.attr, 1342 &sensor_dev_attr_in5_max.dev_attr.attr, 1343 &sensor_dev_attr_in5_min.dev_attr.attr, 1344 &sensor_dev_attr_in5_alarm.dev_attr.attr, 1345 NULL 1346 }; 1347 1348 static struct attribute *vid_attrs[] = { 1349 &dev_attr_cpu0_vid.attr, 1350 &dev_attr_vrm.attr, 1351 NULL 1352 }; 1353 1354 static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs }; 1355 static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs }; 1356 static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs }; 1357 static const struct attribute_group in0_attr_group = { .attrs = in0_attrs }; 1358 static const struct attribute_group in3_attr_group = { .attrs = in3_attrs }; 1359 static const struct attribute_group in4_attr_group = { .attrs = in4_attrs }; 1360 static const struct attribute_group in5_attr_group = { .attrs = in5_attrs }; 1361 static const struct attribute_group vid_attr_group = { .attrs = vid_attrs }; 1362 1363 static int adt7475_detect(struct i2c_client *client, 1364 struct i2c_board_info *info) 1365 { 1366 struct i2c_adapter *adapter = client->adapter; 1367 int vendid, devid, devid2; 1368 const char *name; 1369 1370 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 1371 return -ENODEV; 1372 1373 vendid = adt7475_read(REG_VENDID); 1374 devid2 = adt7475_read(REG_DEVID2); 1375 if (vendid != 0x41 || /* Analog Devices */ 1376 (devid2 & 0xf8) != 0x68) 1377 return -ENODEV; 1378 1379 devid = adt7475_read(REG_DEVID); 1380 if (devid == 0x73) 1381 name = "adt7473"; 1382 else if (devid == 0x75 && client->addr == 0x2e) 1383 name = "adt7475"; 1384 else if (devid == 0x76) 1385 name = "adt7476"; 1386 else if ((devid2 & 0xfc) == 0x6c) 1387 name = "adt7490"; 1388 else { 1389 dev_dbg(&adapter->dev, 1390 "Couldn't detect an ADT7473/75/76/90 part at " 1391 "0x%02x\n", (unsigned int)client->addr); 1392 return -ENODEV; 1393 } 1394 1395 strlcpy(info->type, name, I2C_NAME_SIZE); 1396 1397 return 0; 1398 } 1399 1400 static void adt7475_remove_files(struct i2c_client *client, 1401 struct adt7475_data *data) 1402 { 1403 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); 1404 if (data->has_fan4) 1405 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group); 1406 if (data->has_pwm2) 1407 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group); 1408 if (data->has_voltage & (1 << 0)) 1409 sysfs_remove_group(&client->dev.kobj, &in0_attr_group); 1410 if (data->has_voltage & (1 << 3)) 1411 sysfs_remove_group(&client->dev.kobj, &in3_attr_group); 1412 if (data->has_voltage & (1 << 4)) 1413 sysfs_remove_group(&client->dev.kobj, &in4_attr_group); 1414 if (data->has_voltage & (1 << 5)) 1415 sysfs_remove_group(&client->dev.kobj, &in5_attr_group); 1416 if (data->has_vid) 1417 sysfs_remove_group(&client->dev.kobj, &vid_attr_group); 1418 } 1419 1420 static int adt7475_update_limits(struct i2c_client *client) 1421 { 1422 struct adt7475_data *data = i2c_get_clientdata(client); 1423 int i; 1424 int ret; 1425 1426 ret = adt7475_read(REG_CONFIG4); 1427 if (ret < 0) 1428 return ret; 1429 data->config4 = ret; 1430 1431 ret = adt7475_read(REG_CONFIG5); 1432 if (ret < 0) 1433 return ret; 1434 data->config5 = ret; 1435 1436 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { 1437 if (!(data->has_voltage & (1 << i))) 1438 continue; 1439 /* Adjust values so they match the input precision */ 1440 ret = adt7475_read(VOLTAGE_MIN_REG(i)); 1441 if (ret < 0) 1442 return ret; 1443 data->voltage[MIN][i] = ret << 2; 1444 1445 ret = adt7475_read(VOLTAGE_MAX_REG(i)); 1446 if (ret < 0) 1447 return ret; 1448 data->voltage[MAX][i] = ret << 2; 1449 } 1450 1451 if (data->has_voltage & (1 << 5)) { 1452 ret = adt7475_read(REG_VTT_MIN); 1453 if (ret < 0) 1454 return ret; 1455 data->voltage[MIN][5] = ret << 2; 1456 1457 ret = adt7475_read(REG_VTT_MAX); 1458 if (ret < 0) 1459 return ret; 1460 data->voltage[MAX][5] = ret << 2; 1461 } 1462 1463 for (i = 0; i < ADT7475_TEMP_COUNT; i++) { 1464 /* Adjust values so they match the input precision */ 1465 ret = adt7475_read(TEMP_MIN_REG(i)); 1466 if (ret < 0) 1467 return ret; 1468 data->temp[MIN][i] = ret << 2; 1469 1470 ret = adt7475_read(TEMP_MAX_REG(i)); 1471 if (ret < 0) 1472 return ret; 1473 data->temp[MAX][i] = ret << 2; 1474 1475 ret = adt7475_read(TEMP_TMIN_REG(i)); 1476 if (ret < 0) 1477 return ret; 1478 data->temp[AUTOMIN][i] = ret << 2; 1479 1480 ret = adt7475_read(TEMP_THERM_REG(i)); 1481 if (ret < 0) 1482 return ret; 1483 data->temp[THERM][i] = ret << 2; 1484 1485 ret = adt7475_read(TEMP_OFFSET_REG(i)); 1486 if (ret < 0) 1487 return ret; 1488 data->temp[OFFSET][i] = ret; 1489 } 1490 adt7475_read_hystersis(client); 1491 1492 for (i = 0; i < ADT7475_TACH_COUNT; i++) { 1493 if (i == 3 && !data->has_fan4) 1494 continue; 1495 ret = adt7475_read_word(client, TACH_MIN_REG(i)); 1496 if (ret < 0) 1497 return ret; 1498 data->tach[MIN][i] = ret; 1499 } 1500 1501 for (i = 0; i < ADT7475_PWM_COUNT; i++) { 1502 if (i == 1 && !data->has_pwm2) 1503 continue; 1504 ret = adt7475_read(PWM_MAX_REG(i)); 1505 if (ret < 0) 1506 return ret; 1507 data->pwm[MAX][i] = ret; 1508 1509 ret = adt7475_read(PWM_MIN_REG(i)); 1510 if (ret < 0) 1511 return ret; 1512 data->pwm[MIN][i] = ret; 1513 /* Set the channel and control information */ 1514 adt7475_read_pwm(client, i); 1515 } 1516 1517 ret = adt7475_read(TEMP_TRANGE_REG(0)); 1518 if (ret < 0) 1519 return ret; 1520 data->range[0] = ret; 1521 1522 ret = adt7475_read(TEMP_TRANGE_REG(1)); 1523 if (ret < 0) 1524 return ret; 1525 data->range[1] = ret; 1526 1527 ret = adt7475_read(TEMP_TRANGE_REG(2)); 1528 if (ret < 0) 1529 return ret; 1530 data->range[2] = ret; 1531 1532 return 0; 1533 } 1534 1535 static int adt7475_probe(struct i2c_client *client, 1536 const struct i2c_device_id *id) 1537 { 1538 enum chips chip; 1539 static const char * const names[] = { 1540 [adt7473] = "ADT7473", 1541 [adt7475] = "ADT7475", 1542 [adt7476] = "ADT7476", 1543 [adt7490] = "ADT7490", 1544 }; 1545 1546 struct adt7475_data *data; 1547 int i, ret = 0, revision; 1548 u8 config2, config3; 1549 1550 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); 1551 if (data == NULL) 1552 return -ENOMEM; 1553 1554 mutex_init(&data->lock); 1555 i2c_set_clientdata(client, data); 1556 1557 if (client->dev.of_node) 1558 chip = (enum chips)of_device_get_match_data(&client->dev); 1559 else 1560 chip = id->driver_data; 1561 1562 /* Initialize device-specific values */ 1563 switch (chip) { 1564 case adt7476: 1565 data->has_voltage = 0x0e; /* in1 to in3 */ 1566 revision = adt7475_read(REG_DEVID2) & 0x07; 1567 break; 1568 case adt7490: 1569 data->has_voltage = 0x3e; /* in1 to in5 */ 1570 revision = adt7475_read(REG_DEVID2) & 0x03; 1571 if (revision == 0x03) 1572 revision += adt7475_read(REG_DEVREV2); 1573 break; 1574 default: 1575 data->has_voltage = 0x06; /* in1, in2 */ 1576 revision = adt7475_read(REG_DEVID2) & 0x07; 1577 } 1578 1579 config3 = adt7475_read(REG_CONFIG3); 1580 /* Pin PWM2 may alternatively be used for ALERT output */ 1581 if (!(config3 & CONFIG3_SMBALERT)) 1582 data->has_pwm2 = 1; 1583 /* Meaning of this bit is inverted for the ADT7473-1 */ 1584 if (id->driver_data == adt7473 && revision >= 1) 1585 data->has_pwm2 = !data->has_pwm2; 1586 1587 data->config4 = adt7475_read(REG_CONFIG4); 1588 /* Pin TACH4 may alternatively be used for THERM */ 1589 if ((data->config4 & CONFIG4_PINFUNC) == 0x0) 1590 data->has_fan4 = 1; 1591 1592 /* 1593 * THERM configuration is more complex on the ADT7476 and ADT7490, 1594 * because 2 different pins (TACH4 and +2.5 Vin) can be used for 1595 * this function 1596 */ 1597 if (id->driver_data == adt7490) { 1598 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 && 1599 !(config3 & CONFIG3_THERM)) 1600 data->has_fan4 = 1; 1601 } 1602 if (id->driver_data == adt7476 || id->driver_data == adt7490) { 1603 if (!(config3 & CONFIG3_THERM) || 1604 (data->config4 & CONFIG4_PINFUNC) == 0x1) 1605 data->has_voltage |= (1 << 0); /* in0 */ 1606 } 1607 1608 /* 1609 * On the ADT7476, the +12V input pin may instead be used as VID5, 1610 * and VID pins may alternatively be used as GPIO 1611 */ 1612 if (id->driver_data == adt7476) { 1613 u8 vid = adt7475_read(REG_VID); 1614 if (!(vid & VID_VIDSEL)) 1615 data->has_voltage |= (1 << 4); /* in4 */ 1616 1617 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO); 1618 } 1619 1620 /* Voltage attenuators can be bypassed, globally or individually */ 1621 config2 = adt7475_read(REG_CONFIG2); 1622 if (config2 & CONFIG2_ATTN) { 1623 data->bypass_attn = (0x3 << 3) | 0x3; 1624 } else { 1625 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) | 1626 ((data->config4 & CONFIG4_ATTN_IN43) >> 3); 1627 } 1628 data->bypass_attn &= data->has_voltage; 1629 1630 /* 1631 * Call adt7475_read_pwm for all pwm's as this will reprogram any 1632 * pwm's which are disabled to manual mode with 0% duty cycle 1633 */ 1634 for (i = 0; i < ADT7475_PWM_COUNT; i++) 1635 adt7475_read_pwm(client, i); 1636 1637 /* Start monitoring */ 1638 switch (chip) { 1639 case adt7475: 1640 case adt7476: 1641 i2c_smbus_write_byte_data(client, REG_CONFIG1, 1642 adt7475_read(REG_CONFIG1) | 0x01); 1643 break; 1644 default: 1645 break; 1646 } 1647 1648 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group); 1649 if (ret) 1650 return ret; 1651 1652 /* Features that can be disabled individually */ 1653 if (data->has_fan4) { 1654 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group); 1655 if (ret) 1656 goto eremove; 1657 } 1658 if (data->has_pwm2) { 1659 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group); 1660 if (ret) 1661 goto eremove; 1662 } 1663 if (data->has_voltage & (1 << 0)) { 1664 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group); 1665 if (ret) 1666 goto eremove; 1667 } 1668 if (data->has_voltage & (1 << 3)) { 1669 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group); 1670 if (ret) 1671 goto eremove; 1672 } 1673 if (data->has_voltage & (1 << 4)) { 1674 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group); 1675 if (ret) 1676 goto eremove; 1677 } 1678 if (data->has_voltage & (1 << 5)) { 1679 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group); 1680 if (ret) 1681 goto eremove; 1682 } 1683 if (data->has_vid) { 1684 data->vrm = vid_which_vrm(); 1685 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group); 1686 if (ret) 1687 goto eremove; 1688 } 1689 1690 data->hwmon_dev = hwmon_device_register(&client->dev); 1691 if (IS_ERR(data->hwmon_dev)) { 1692 ret = PTR_ERR(data->hwmon_dev); 1693 goto eremove; 1694 } 1695 1696 dev_info(&client->dev, "%s device, revision %d\n", 1697 names[id->driver_data], revision); 1698 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2) 1699 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n", 1700 (data->has_voltage & (1 << 0)) ? " in0" : "", 1701 (data->has_voltage & (1 << 4)) ? " in4" : "", 1702 data->has_fan4 ? " fan4" : "", 1703 data->has_pwm2 ? " pwm2" : "", 1704 data->has_vid ? " vid" : ""); 1705 if (data->bypass_attn) 1706 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n", 1707 (data->bypass_attn & (1 << 0)) ? " in0" : "", 1708 (data->bypass_attn & (1 << 1)) ? " in1" : "", 1709 (data->bypass_attn & (1 << 3)) ? " in3" : "", 1710 (data->bypass_attn & (1 << 4)) ? " in4" : ""); 1711 1712 /* Limits and settings, should never change update more than once */ 1713 ret = adt7475_update_limits(client); 1714 if (ret) 1715 goto eremove; 1716 1717 return 0; 1718 1719 eremove: 1720 adt7475_remove_files(client, data); 1721 return ret; 1722 } 1723 1724 static int adt7475_remove(struct i2c_client *client) 1725 { 1726 struct adt7475_data *data = i2c_get_clientdata(client); 1727 1728 hwmon_device_unregister(data->hwmon_dev); 1729 adt7475_remove_files(client, data); 1730 1731 return 0; 1732 } 1733 1734 static struct i2c_driver adt7475_driver = { 1735 .class = I2C_CLASS_HWMON, 1736 .driver = { 1737 .name = "adt7475", 1738 .of_match_table = of_match_ptr(adt7475_of_match), 1739 }, 1740 .probe = adt7475_probe, 1741 .remove = adt7475_remove, 1742 .id_table = adt7475_id, 1743 .detect = adt7475_detect, 1744 .address_list = normal_i2c, 1745 }; 1746 1747 static void adt7475_read_hystersis(struct i2c_client *client) 1748 { 1749 struct adt7475_data *data = i2c_get_clientdata(client); 1750 1751 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS); 1752 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0]; 1753 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS); 1754 } 1755 1756 static void adt7475_read_pwm(struct i2c_client *client, int index) 1757 { 1758 struct adt7475_data *data = i2c_get_clientdata(client); 1759 unsigned int v; 1760 1761 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index)); 1762 1763 /* 1764 * Figure out the internal value for pwmctrl and pwmchan 1765 * based on the current settings 1766 */ 1767 v = (data->pwm[CONTROL][index] >> 5) & 7; 1768 1769 if (v == 3) 1770 data->pwmctl[index] = 0; 1771 else if (v == 7) 1772 data->pwmctl[index] = 1; 1773 else if (v == 4) { 1774 /* 1775 * The fan is disabled - we don't want to 1776 * support that, so change to manual mode and 1777 * set the duty cycle to 0 instead 1778 */ 1779 data->pwm[INPUT][index] = 0; 1780 data->pwm[CONTROL][index] &= ~0xE0; 1781 data->pwm[CONTROL][index] |= (7 << 5); 1782 1783 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 1784 data->pwm[INPUT][index]); 1785 1786 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 1787 data->pwm[CONTROL][index]); 1788 1789 data->pwmctl[index] = 1; 1790 } else { 1791 data->pwmctl[index] = 2; 1792 1793 switch (v) { 1794 case 0: 1795 data->pwmchan[index] = 1; 1796 break; 1797 case 1: 1798 data->pwmchan[index] = 2; 1799 break; 1800 case 2: 1801 data->pwmchan[index] = 4; 1802 break; 1803 case 5: 1804 data->pwmchan[index] = 6; 1805 break; 1806 case 6: 1807 data->pwmchan[index] = 7; 1808 break; 1809 } 1810 } 1811 } 1812 1813 static int adt7475_update_measure(struct device *dev) 1814 { 1815 struct i2c_client *client = to_i2c_client(dev); 1816 struct adt7475_data *data = i2c_get_clientdata(client); 1817 u16 ext; 1818 int i; 1819 int ret; 1820 1821 ret = adt7475_read(REG_STATUS2); 1822 if (ret < 0) 1823 return ret; 1824 data->alarms = ret << 8; 1825 1826 ret = adt7475_read(REG_STATUS1); 1827 if (ret < 0) 1828 return ret; 1829 data->alarms |= ret; 1830 1831 ret = adt7475_read(REG_EXTEND2); 1832 if (ret < 0) 1833 return ret; 1834 1835 ext = (ret << 8); 1836 1837 ret = adt7475_read(REG_EXTEND1); 1838 if (ret < 0) 1839 return ret; 1840 1841 ext |= ret; 1842 1843 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { 1844 if (!(data->has_voltage & (1 << i))) 1845 continue; 1846 ret = adt7475_read(VOLTAGE_REG(i)); 1847 if (ret < 0) 1848 return ret; 1849 data->voltage[INPUT][i] = 1850 (ret << 2) | 1851 ((ext >> (i * 2)) & 3); 1852 } 1853 1854 for (i = 0; i < ADT7475_TEMP_COUNT; i++) { 1855 ret = adt7475_read(TEMP_REG(i)); 1856 if (ret < 0) 1857 return ret; 1858 data->temp[INPUT][i] = 1859 (ret << 2) | 1860 ((ext >> ((i + 5) * 2)) & 3); 1861 } 1862 1863 if (data->has_voltage & (1 << 5)) { 1864 ret = adt7475_read(REG_STATUS4); 1865 if (ret < 0) 1866 return ret; 1867 data->alarms |= ret << 24; 1868 1869 ret = adt7475_read(REG_EXTEND3); 1870 if (ret < 0) 1871 return ret; 1872 ext = ret; 1873 1874 ret = adt7475_read(REG_VTT); 1875 if (ret < 0) 1876 return ret; 1877 data->voltage[INPUT][5] = ret << 2 | 1878 ((ext >> 4) & 3); 1879 } 1880 1881 for (i = 0; i < ADT7475_TACH_COUNT; i++) { 1882 if (i == 3 && !data->has_fan4) 1883 continue; 1884 ret = adt7475_read_word(client, TACH_REG(i)); 1885 if (ret < 0) 1886 return ret; 1887 data->tach[INPUT][i] = ret; 1888 } 1889 1890 /* Updated by hw when in auto mode */ 1891 for (i = 0; i < ADT7475_PWM_COUNT; i++) { 1892 if (i == 1 && !data->has_pwm2) 1893 continue; 1894 ret = adt7475_read(PWM_REG(i)); 1895 if (ret < 0) 1896 return ret; 1897 data->pwm[INPUT][i] = ret; 1898 } 1899 1900 if (data->has_vid) { 1901 ret = adt7475_read(REG_VID); 1902 if (ret < 0) 1903 return ret; 1904 data->vid = ret & 0x3f; 1905 } 1906 1907 return 0; 1908 } 1909 1910 static struct adt7475_data *adt7475_update_device(struct device *dev) 1911 { 1912 struct i2c_client *client = to_i2c_client(dev); 1913 struct adt7475_data *data = i2c_get_clientdata(client); 1914 int ret; 1915 1916 mutex_lock(&data->lock); 1917 1918 /* Measurement values update every 2 seconds */ 1919 if (time_after(jiffies, data->measure_updated + HZ * 2) || 1920 !data->valid) { 1921 ret = adt7475_update_measure(dev); 1922 if (ret) { 1923 data->valid = false; 1924 mutex_unlock(&data->lock); 1925 return ERR_PTR(ret); 1926 } 1927 data->measure_updated = jiffies; 1928 data->valid = true; 1929 } 1930 1931 mutex_unlock(&data->lock); 1932 1933 return data; 1934 } 1935 1936 module_i2c_driver(adt7475_driver); 1937 1938 MODULE_AUTHOR("Advanced Micro Devices, Inc"); 1939 MODULE_DESCRIPTION("adt7475 driver"); 1940 MODULE_LICENSE("GPL"); 1941