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