1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * nct7802 - Driver for Nuvoton NCT7802Y 4 * 5 * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net> 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/err.h> 11 #include <linux/i2c.h> 12 #include <linux/init.h> 13 #include <linux/hwmon.h> 14 #include <linux/hwmon-sysfs.h> 15 #include <linux/jiffies.h> 16 #include <linux/module.h> 17 #include <linux/mutex.h> 18 #include <linux/regmap.h> 19 #include <linux/slab.h> 20 21 #define DRVNAME "nct7802" 22 23 static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e }; 24 25 static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = { 26 { 0x46, 0x00, 0x40, 0x42, 0x44 }, 27 { 0x45, 0x00, 0x3f, 0x41, 0x43 }, 28 }; 29 30 static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 }; 31 32 static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = { 33 { 0, 0, 4, 0, 4 }, 34 { 2, 0, 6, 2, 6 }, 35 }; 36 37 #define REG_BANK 0x00 38 #define REG_TEMP_LSB 0x05 39 #define REG_TEMP_PECI_LSB 0x08 40 #define REG_VOLTAGE_LOW 0x0f 41 #define REG_FANCOUNT_LOW 0x13 42 #define REG_START 0x21 43 #define REG_MODE 0x22 /* 7.2.32 Mode Selection Register */ 44 #define REG_PECI_ENABLE 0x23 45 #define REG_FAN_ENABLE 0x24 46 #define REG_VMON_ENABLE 0x25 47 #define REG_PWM(x) (0x60 + (x)) 48 #define REG_SMARTFAN_EN(x) (0x64 + (x) / 2) 49 #define SMARTFAN_EN_SHIFT(x) ((x) % 2 * 4) 50 #define REG_VENDOR_ID 0xfd 51 #define REG_CHIP_ID 0xfe 52 #define REG_VERSION_ID 0xff 53 54 /* 55 * Resistance temperature detector (RTD) modes according to 7.2.32 Mode 56 * Selection Register 57 */ 58 #define RTD_MODE_CURRENT 0x1 59 #define RTD_MODE_THERMISTOR 0x2 60 #define RTD_MODE_VOLTAGE 0x3 61 62 #define MODE_RTD_MASK 0x3 63 #define MODE_LTD_EN 0x40 64 65 /* 66 * Bit offset for sensors modes in REG_MODE. 67 * Valid for index 0..2, indicating RTD1..3. 68 */ 69 #define MODE_BIT_OFFSET_RTD(index) ((index) * 2) 70 71 /* 72 * Data structures and manipulation thereof 73 */ 74 75 struct nct7802_data { 76 struct regmap *regmap; 77 struct mutex access_lock; /* for multi-byte read and write operations */ 78 u8 in_status; 79 struct mutex in_alarm_lock; 80 }; 81 82 static ssize_t temp_type_show(struct device *dev, 83 struct device_attribute *attr, char *buf) 84 { 85 struct nct7802_data *data = dev_get_drvdata(dev); 86 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 87 unsigned int mode; 88 int ret; 89 90 ret = regmap_read(data->regmap, REG_MODE, &mode); 91 if (ret < 0) 92 return ret; 93 94 return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2); 95 } 96 97 static ssize_t temp_type_store(struct device *dev, 98 struct device_attribute *attr, const char *buf, 99 size_t count) 100 { 101 struct nct7802_data *data = dev_get_drvdata(dev); 102 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 103 unsigned int type; 104 int err; 105 106 err = kstrtouint(buf, 0, &type); 107 if (err < 0) 108 return err; 109 if (sattr->index == 2 && type != 4) /* RD3 */ 110 return -EINVAL; 111 if (type < 3 || type > 4) 112 return -EINVAL; 113 err = regmap_update_bits(data->regmap, REG_MODE, 114 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index); 115 return err ? : count; 116 } 117 118 static ssize_t pwm_mode_show(struct device *dev, 119 struct device_attribute *attr, char *buf) 120 { 121 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 122 struct nct7802_data *data = dev_get_drvdata(dev); 123 unsigned int regval; 124 int ret; 125 126 if (sattr->index > 1) 127 return sprintf(buf, "1\n"); 128 129 ret = regmap_read(data->regmap, 0x5E, ®val); 130 if (ret < 0) 131 return ret; 132 133 return sprintf(buf, "%u\n", !(regval & (1 << sattr->index))); 134 } 135 136 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr, 137 char *buf) 138 { 139 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 140 struct nct7802_data *data = dev_get_drvdata(dev); 141 unsigned int val; 142 int ret; 143 144 if (!attr->index) 145 return sprintf(buf, "255\n"); 146 147 ret = regmap_read(data->regmap, attr->index, &val); 148 if (ret < 0) 149 return ret; 150 151 return sprintf(buf, "%d\n", val); 152 } 153 154 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr, 155 const char *buf, size_t count) 156 { 157 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 158 struct nct7802_data *data = dev_get_drvdata(dev); 159 int err; 160 u8 val; 161 162 err = kstrtou8(buf, 0, &val); 163 if (err < 0) 164 return err; 165 166 err = regmap_write(data->regmap, attr->index, val); 167 return err ? : count; 168 } 169 170 static ssize_t pwm_enable_show(struct device *dev, 171 struct device_attribute *attr, char *buf) 172 { 173 struct nct7802_data *data = dev_get_drvdata(dev); 174 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 175 unsigned int reg, enabled; 176 int ret; 177 178 ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), ®); 179 if (ret < 0) 180 return ret; 181 enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1; 182 return sprintf(buf, "%u\n", enabled + 1); 183 } 184 185 static ssize_t pwm_enable_store(struct device *dev, 186 struct device_attribute *attr, 187 const char *buf, size_t count) 188 { 189 struct nct7802_data *data = dev_get_drvdata(dev); 190 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 191 u8 val; 192 int ret; 193 194 ret = kstrtou8(buf, 0, &val); 195 if (ret < 0) 196 return ret; 197 if (val < 1 || val > 2) 198 return -EINVAL; 199 ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index), 200 1 << SMARTFAN_EN_SHIFT(sattr->index), 201 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index)); 202 return ret ? : count; 203 } 204 205 static int nct7802_read_temp(struct nct7802_data *data, 206 u8 reg_temp, u8 reg_temp_low, int *temp) 207 { 208 unsigned int t1, t2 = 0; 209 int err; 210 211 *temp = 0; 212 213 mutex_lock(&data->access_lock); 214 err = regmap_read(data->regmap, reg_temp, &t1); 215 if (err < 0) 216 goto abort; 217 t1 <<= 8; 218 if (reg_temp_low) { /* 11 bit data */ 219 err = regmap_read(data->regmap, reg_temp_low, &t2); 220 if (err < 0) 221 goto abort; 222 } 223 t1 |= t2 & 0xe0; 224 *temp = (s16)t1 / 32 * 125; 225 abort: 226 mutex_unlock(&data->access_lock); 227 return err; 228 } 229 230 static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan) 231 { 232 unsigned int regs[2] = {reg_fan, REG_FANCOUNT_LOW}; 233 u8 f[2]; 234 int ret; 235 236 ret = regmap_multi_reg_read(data->regmap, regs, f, 2); 237 if (ret) 238 return ret; 239 ret = (f[0] << 5) | (f[1] >> 3); 240 /* convert fan count to rpm */ 241 if (ret == 0x1fff) /* maximum value, assume fan is stopped */ 242 ret = 0; 243 else if (ret) 244 ret = DIV_ROUND_CLOSEST(1350000U, ret); 245 return ret; 246 } 247 248 static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low, 249 u8 reg_fan_high) 250 { 251 unsigned int regs[2] = {reg_fan_low, reg_fan_high}; 252 u8 f[2]; 253 int ret; 254 255 ret = regmap_multi_reg_read(data->regmap, regs, f, 2); 256 if (ret < 0) 257 return ret; 258 259 ret = f[0] | ((f[1] & 0xf8) << 5); 260 /* convert fan count to rpm */ 261 if (ret == 0x1fff) /* maximum value, assume no limit */ 262 ret = 0; 263 else if (ret) 264 ret = DIV_ROUND_CLOSEST(1350000U, ret); 265 else 266 ret = 1350000U; 267 return ret; 268 } 269 270 static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low, 271 u8 reg_fan_high, unsigned long limit) 272 { 273 int err; 274 275 if (limit) 276 limit = DIV_ROUND_CLOSEST(1350000U, limit); 277 else 278 limit = 0x1fff; 279 limit = clamp_val(limit, 0, 0x1fff); 280 281 mutex_lock(&data->access_lock); 282 err = regmap_write(data->regmap, reg_fan_low, limit & 0xff); 283 if (err < 0) 284 goto abort; 285 286 err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5); 287 abort: 288 mutex_unlock(&data->access_lock); 289 return err; 290 } 291 292 static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 }; 293 294 static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index) 295 { 296 u8 v[2]; 297 int ret; 298 299 if (index == 0) { /* voltage */ 300 unsigned int regs[2] = {REG_VOLTAGE[nr], REG_VOLTAGE_LOW}; 301 302 ret = regmap_multi_reg_read(data->regmap, regs, v, 2); 303 if (ret < 0) 304 return ret; 305 ret = ((v[0] << 2) | (v[1] >> 6)) * nct7802_vmul[nr]; 306 } else { /* limit */ 307 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; 308 unsigned int regs[2] = {REG_VOLTAGE_LIMIT_LSB[index - 1][nr], 309 REG_VOLTAGE_LIMIT_MSB[nr]}; 310 311 ret = regmap_multi_reg_read(data->regmap, regs, v, 2); 312 if (ret < 0) 313 return ret; 314 ret = (v[0] | ((v[1] << shift) & 0x300)) * nct7802_vmul[nr]; 315 } 316 return ret; 317 } 318 319 static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index, 320 unsigned long voltage) 321 { 322 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; 323 int err; 324 325 voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]); 326 voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]); 327 328 mutex_lock(&data->access_lock); 329 err = regmap_write(data->regmap, 330 REG_VOLTAGE_LIMIT_LSB[index - 1][nr], 331 voltage & 0xff); 332 if (err < 0) 333 goto abort; 334 335 err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr], 336 0x0300 >> shift, (voltage & 0x0300) >> shift); 337 abort: 338 mutex_unlock(&data->access_lock); 339 return err; 340 } 341 342 static ssize_t in_show(struct device *dev, struct device_attribute *attr, 343 char *buf) 344 { 345 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 346 struct nct7802_data *data = dev_get_drvdata(dev); 347 int voltage; 348 349 voltage = nct7802_read_voltage(data, sattr->nr, sattr->index); 350 if (voltage < 0) 351 return voltage; 352 353 return sprintf(buf, "%d\n", voltage); 354 } 355 356 static ssize_t in_store(struct device *dev, struct device_attribute *attr, 357 const char *buf, size_t count) 358 { 359 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 360 struct nct7802_data *data = dev_get_drvdata(dev); 361 int index = sattr->index; 362 int nr = sattr->nr; 363 unsigned long val; 364 int err; 365 366 err = kstrtoul(buf, 10, &val); 367 if (err < 0) 368 return err; 369 370 err = nct7802_write_voltage(data, nr, index, val); 371 return err ? : count; 372 } 373 374 static ssize_t in_alarm_show(struct device *dev, struct device_attribute *attr, 375 char *buf) 376 { 377 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 378 struct nct7802_data *data = dev_get_drvdata(dev); 379 int volt, min, max, ret; 380 unsigned int val; 381 382 mutex_lock(&data->in_alarm_lock); 383 384 /* 385 * The SMI Voltage status register is the only register giving a status 386 * for voltages. A bit is set for each input crossing a threshold, in 387 * both direction, but the "inside" or "outside" limits info is not 388 * available. Also this register is cleared on read. 389 * Note: this is not explicitly spelled out in the datasheet, but 390 * from experiment. 391 * To deal with this we use a status cache with one validity bit and 392 * one status bit for each input. Validity is cleared at startup and 393 * each time the register reports a change, and the status is processed 394 * by software based on current input value and limits. 395 */ 396 ret = regmap_read(data->regmap, 0x1e, &val); /* SMI Voltage status */ 397 if (ret < 0) 398 goto abort; 399 400 /* invalidate cached status for all inputs crossing a threshold */ 401 data->in_status &= ~((val & 0x0f) << 4); 402 403 /* if cached status for requested input is invalid, update it */ 404 if (!(data->in_status & (0x10 << sattr->index))) { 405 ret = nct7802_read_voltage(data, sattr->nr, 0); 406 if (ret < 0) 407 goto abort; 408 volt = ret; 409 410 ret = nct7802_read_voltage(data, sattr->nr, 1); 411 if (ret < 0) 412 goto abort; 413 min = ret; 414 415 ret = nct7802_read_voltage(data, sattr->nr, 2); 416 if (ret < 0) 417 goto abort; 418 max = ret; 419 420 if (volt < min || volt > max) 421 data->in_status |= (1 << sattr->index); 422 else 423 data->in_status &= ~(1 << sattr->index); 424 425 data->in_status |= 0x10 << sattr->index; 426 } 427 428 ret = sprintf(buf, "%u\n", !!(data->in_status & (1 << sattr->index))); 429 abort: 430 mutex_unlock(&data->in_alarm_lock); 431 return ret; 432 } 433 434 static ssize_t temp_show(struct device *dev, struct device_attribute *attr, 435 char *buf) 436 { 437 struct nct7802_data *data = dev_get_drvdata(dev); 438 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 439 int err, temp; 440 441 err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp); 442 if (err < 0) 443 return err; 444 445 return sprintf(buf, "%d\n", temp); 446 } 447 448 static ssize_t temp_store(struct device *dev, struct device_attribute *attr, 449 const char *buf, size_t count) 450 { 451 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 452 struct nct7802_data *data = dev_get_drvdata(dev); 453 int nr = sattr->nr; 454 long val; 455 int err; 456 457 err = kstrtol(buf, 10, &val); 458 if (err < 0) 459 return err; 460 461 val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000); 462 463 err = regmap_write(data->regmap, nr, val & 0xff); 464 return err ? : count; 465 } 466 467 static ssize_t fan_show(struct device *dev, struct device_attribute *attr, 468 char *buf) 469 { 470 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 471 struct nct7802_data *data = dev_get_drvdata(dev); 472 int speed; 473 474 speed = nct7802_read_fan(data, sattr->index); 475 if (speed < 0) 476 return speed; 477 478 return sprintf(buf, "%d\n", speed); 479 } 480 481 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr, 482 char *buf) 483 { 484 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 485 struct nct7802_data *data = dev_get_drvdata(dev); 486 int speed; 487 488 speed = nct7802_read_fan_min(data, sattr->nr, sattr->index); 489 if (speed < 0) 490 return speed; 491 492 return sprintf(buf, "%d\n", speed); 493 } 494 495 static ssize_t fan_min_store(struct device *dev, 496 struct device_attribute *attr, const char *buf, 497 size_t count) 498 { 499 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 500 struct nct7802_data *data = dev_get_drvdata(dev); 501 unsigned long val; 502 int err; 503 504 err = kstrtoul(buf, 10, &val); 505 if (err < 0) 506 return err; 507 508 err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val); 509 return err ? : count; 510 } 511 512 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, 513 char *buf) 514 { 515 struct nct7802_data *data = dev_get_drvdata(dev); 516 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 517 int bit = sattr->index; 518 unsigned int val; 519 int ret; 520 521 ret = regmap_read(data->regmap, sattr->nr, &val); 522 if (ret < 0) 523 return ret; 524 525 return sprintf(buf, "%u\n", !!(val & (1 << bit))); 526 } 527 528 static ssize_t 529 beep_show(struct device *dev, struct device_attribute *attr, char *buf) 530 { 531 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 532 struct nct7802_data *data = dev_get_drvdata(dev); 533 unsigned int regval; 534 int err; 535 536 err = regmap_read(data->regmap, sattr->nr, ®val); 537 if (err) 538 return err; 539 540 return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index))); 541 } 542 543 static ssize_t 544 beep_store(struct device *dev, struct device_attribute *attr, const char *buf, 545 size_t count) 546 { 547 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 548 struct nct7802_data *data = dev_get_drvdata(dev); 549 unsigned long val; 550 int err; 551 552 err = kstrtoul(buf, 10, &val); 553 if (err < 0) 554 return err; 555 if (val > 1) 556 return -EINVAL; 557 558 err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index, 559 val ? 1 << sattr->index : 0); 560 return err ? : count; 561 } 562 563 static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0); 564 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0x01, REG_TEMP_LSB); 565 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0x31, 0); 566 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0x30, 0); 567 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0x3a, 0); 568 569 static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1); 570 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0x02, REG_TEMP_LSB); 571 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 0x33, 0); 572 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 0x32, 0); 573 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 0x3b, 0); 574 575 static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2); 576 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0x03, REG_TEMP_LSB); 577 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 0x35, 0); 578 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 0x34, 0); 579 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 0x3c, 0); 580 581 static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 0x04, 0); 582 static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 0x37, 0); 583 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 0x36, 0); 584 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 0x3d, 0); 585 586 static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 0x06, REG_TEMP_PECI_LSB); 587 static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 0x39, 0); 588 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 0x38, 0); 589 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 0x3e, 0); 590 591 static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 0x07, REG_TEMP_PECI_LSB); 592 593 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, alarm, 0x18, 0); 594 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, alarm, 0x18, 1); 595 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, alarm, 0x18, 2); 596 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, alarm, 0x18, 3); 597 static SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm, alarm, 0x18, 4); 598 599 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, alarm, 0x19, 0); 600 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, alarm, 0x19, 1); 601 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, alarm, 0x19, 2); 602 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, alarm, 0x19, 3); 603 static SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm, alarm, 0x19, 4); 604 605 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, alarm, 0x1b, 0); 606 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, alarm, 0x1b, 1); 607 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, alarm, 0x1b, 2); 608 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, alarm, 0x1b, 3); 609 static SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm, alarm, 0x1b, 4); 610 611 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, alarm, 0x17, 0); 612 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, alarm, 0x17, 1); 613 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, alarm, 0x17, 2); 614 615 static SENSOR_DEVICE_ATTR_2_RW(temp1_beep, beep, 0x5c, 0); 616 static SENSOR_DEVICE_ATTR_2_RW(temp2_beep, beep, 0x5c, 1); 617 static SENSOR_DEVICE_ATTR_2_RW(temp3_beep, beep, 0x5c, 2); 618 static SENSOR_DEVICE_ATTR_2_RW(temp4_beep, beep, 0x5c, 3); 619 static SENSOR_DEVICE_ATTR_2_RW(temp5_beep, beep, 0x5c, 4); 620 static SENSOR_DEVICE_ATTR_2_RW(temp6_beep, beep, 0x5c, 5); 621 622 static struct attribute *nct7802_temp_attrs[] = { 623 &sensor_dev_attr_temp1_type.dev_attr.attr, 624 &sensor_dev_attr_temp1_input.dev_attr.attr, 625 &sensor_dev_attr_temp1_min.dev_attr.attr, 626 &sensor_dev_attr_temp1_max.dev_attr.attr, 627 &sensor_dev_attr_temp1_crit.dev_attr.attr, 628 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 629 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 630 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 631 &sensor_dev_attr_temp1_fault.dev_attr.attr, 632 &sensor_dev_attr_temp1_beep.dev_attr.attr, 633 634 &sensor_dev_attr_temp2_type.dev_attr.attr, /* 10 */ 635 &sensor_dev_attr_temp2_input.dev_attr.attr, 636 &sensor_dev_attr_temp2_min.dev_attr.attr, 637 &sensor_dev_attr_temp2_max.dev_attr.attr, 638 &sensor_dev_attr_temp2_crit.dev_attr.attr, 639 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, 640 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 641 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 642 &sensor_dev_attr_temp2_fault.dev_attr.attr, 643 &sensor_dev_attr_temp2_beep.dev_attr.attr, 644 645 &sensor_dev_attr_temp3_type.dev_attr.attr, /* 20 */ 646 &sensor_dev_attr_temp3_input.dev_attr.attr, 647 &sensor_dev_attr_temp3_min.dev_attr.attr, 648 &sensor_dev_attr_temp3_max.dev_attr.attr, 649 &sensor_dev_attr_temp3_crit.dev_attr.attr, 650 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, 651 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, 652 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, 653 &sensor_dev_attr_temp3_fault.dev_attr.attr, 654 &sensor_dev_attr_temp3_beep.dev_attr.attr, 655 656 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 30 */ 657 &sensor_dev_attr_temp4_min.dev_attr.attr, 658 &sensor_dev_attr_temp4_max.dev_attr.attr, 659 &sensor_dev_attr_temp4_crit.dev_attr.attr, 660 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, 661 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, 662 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, 663 &sensor_dev_attr_temp4_beep.dev_attr.attr, 664 665 &sensor_dev_attr_temp5_input.dev_attr.attr, /* 38 */ 666 &sensor_dev_attr_temp5_min.dev_attr.attr, 667 &sensor_dev_attr_temp5_max.dev_attr.attr, 668 &sensor_dev_attr_temp5_crit.dev_attr.attr, 669 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr, 670 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, 671 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, 672 &sensor_dev_attr_temp5_beep.dev_attr.attr, 673 674 &sensor_dev_attr_temp6_input.dev_attr.attr, /* 46 */ 675 &sensor_dev_attr_temp6_beep.dev_attr.attr, 676 677 NULL 678 }; 679 680 static umode_t nct7802_temp_is_visible(struct kobject *kobj, 681 struct attribute *attr, int index) 682 { 683 struct device *dev = kobj_to_dev(kobj); 684 struct nct7802_data *data = dev_get_drvdata(dev); 685 unsigned int reg; 686 int err; 687 688 err = regmap_read(data->regmap, REG_MODE, ®); 689 if (err < 0) 690 return 0; 691 692 if (index < 10 && 693 (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */ 694 return 0; 695 696 if (index >= 10 && index < 20 && 697 (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */ 698 return 0; 699 if (index >= 20 && index < 30 && (reg & 0x30) != 0x20) /* RD3 */ 700 return 0; 701 702 if (index >= 30 && index < 38) /* local */ 703 return attr->mode; 704 705 err = regmap_read(data->regmap, REG_PECI_ENABLE, ®); 706 if (err < 0) 707 return 0; 708 709 if (index >= 38 && index < 46 && !(reg & 0x01)) /* PECI 0 */ 710 return 0; 711 712 if (index >= 46 && !(reg & 0x02)) /* PECI 1 */ 713 return 0; 714 715 return attr->mode; 716 } 717 718 static const struct attribute_group nct7802_temp_group = { 719 .attrs = nct7802_temp_attrs, 720 .is_visible = nct7802_temp_is_visible, 721 }; 722 723 static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, 0); 724 static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, 1); 725 static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, 2); 726 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, in_alarm, 0, 3); 727 static SENSOR_DEVICE_ATTR_2_RW(in0_beep, beep, 0x5a, 3); 728 729 static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, 0); 730 731 static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, 0); 732 static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, 1); 733 static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, 2); 734 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, in_alarm, 2, 0); 735 static SENSOR_DEVICE_ATTR_2_RW(in2_beep, beep, 0x5a, 0); 736 737 static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, 0); 738 static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, 1); 739 static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, 2); 740 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, in_alarm, 3, 1); 741 static SENSOR_DEVICE_ATTR_2_RW(in3_beep, beep, 0x5a, 1); 742 743 static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, 0); 744 static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, 1); 745 static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, 2); 746 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, in_alarm, 4, 2); 747 static SENSOR_DEVICE_ATTR_2_RW(in4_beep, beep, 0x5a, 2); 748 749 static struct attribute *nct7802_in_attrs[] = { 750 &sensor_dev_attr_in0_input.dev_attr.attr, 751 &sensor_dev_attr_in0_min.dev_attr.attr, 752 &sensor_dev_attr_in0_max.dev_attr.attr, 753 &sensor_dev_attr_in0_alarm.dev_attr.attr, 754 &sensor_dev_attr_in0_beep.dev_attr.attr, 755 756 &sensor_dev_attr_in1_input.dev_attr.attr, /* 5 */ 757 758 &sensor_dev_attr_in2_input.dev_attr.attr, /* 6 */ 759 &sensor_dev_attr_in2_min.dev_attr.attr, 760 &sensor_dev_attr_in2_max.dev_attr.attr, 761 &sensor_dev_attr_in2_alarm.dev_attr.attr, 762 &sensor_dev_attr_in2_beep.dev_attr.attr, 763 764 &sensor_dev_attr_in3_input.dev_attr.attr, /* 11 */ 765 &sensor_dev_attr_in3_min.dev_attr.attr, 766 &sensor_dev_attr_in3_max.dev_attr.attr, 767 &sensor_dev_attr_in3_alarm.dev_attr.attr, 768 &sensor_dev_attr_in3_beep.dev_attr.attr, 769 770 &sensor_dev_attr_in4_input.dev_attr.attr, /* 16 */ 771 &sensor_dev_attr_in4_min.dev_attr.attr, 772 &sensor_dev_attr_in4_max.dev_attr.attr, 773 &sensor_dev_attr_in4_alarm.dev_attr.attr, 774 &sensor_dev_attr_in4_beep.dev_attr.attr, 775 776 NULL, 777 }; 778 779 static umode_t nct7802_in_is_visible(struct kobject *kobj, 780 struct attribute *attr, int index) 781 { 782 struct device *dev = kobj_to_dev(kobj); 783 struct nct7802_data *data = dev_get_drvdata(dev); 784 unsigned int reg; 785 int err; 786 787 if (index < 6) /* VCC, VCORE */ 788 return attr->mode; 789 790 err = regmap_read(data->regmap, REG_MODE, ®); 791 if (err < 0) 792 return 0; 793 794 if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */ 795 return 0; 796 if (index >= 11 && index < 16 && (reg & 0x0c) != 0x0c) /* VSEN2 */ 797 return 0; 798 if (index >= 16 && (reg & 0x30) != 0x30) /* VSEN3 */ 799 return 0; 800 801 return attr->mode; 802 } 803 804 static const struct attribute_group nct7802_in_group = { 805 .attrs = nct7802_in_attrs, 806 .is_visible = nct7802_in_is_visible, 807 }; 808 809 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0x10); 810 static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan_min, 0x49, 0x4c); 811 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, alarm, 0x1a, 0); 812 static SENSOR_DEVICE_ATTR_2_RW(fan1_beep, beep, 0x5b, 0); 813 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 0x11); 814 static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan_min, 0x4a, 0x4d); 815 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, alarm, 0x1a, 1); 816 static SENSOR_DEVICE_ATTR_2_RW(fan2_beep, beep, 0x5b, 1); 817 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 0x12); 818 static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan_min, 0x4b, 0x4e); 819 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, alarm, 0x1a, 2); 820 static SENSOR_DEVICE_ATTR_2_RW(fan3_beep, beep, 0x5b, 2); 821 822 /* 7.2.89 Fan Control Output Type */ 823 static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0); 824 static SENSOR_DEVICE_ATTR_RO(pwm2_mode, pwm_mode, 1); 825 static SENSOR_DEVICE_ATTR_RO(pwm3_mode, pwm_mode, 2); 826 827 /* 7.2.91... Fan Control Output Value */ 828 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, REG_PWM(0)); 829 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, REG_PWM(1)); 830 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, REG_PWM(2)); 831 832 /* 7.2.95... Temperature to Fan mapping Relationships Register */ 833 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0); 834 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1); 835 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2); 836 837 static struct attribute *nct7802_fan_attrs[] = { 838 &sensor_dev_attr_fan1_input.dev_attr.attr, 839 &sensor_dev_attr_fan1_min.dev_attr.attr, 840 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 841 &sensor_dev_attr_fan1_beep.dev_attr.attr, 842 &sensor_dev_attr_fan2_input.dev_attr.attr, 843 &sensor_dev_attr_fan2_min.dev_attr.attr, 844 &sensor_dev_attr_fan2_alarm.dev_attr.attr, 845 &sensor_dev_attr_fan2_beep.dev_attr.attr, 846 &sensor_dev_attr_fan3_input.dev_attr.attr, 847 &sensor_dev_attr_fan3_min.dev_attr.attr, 848 &sensor_dev_attr_fan3_alarm.dev_attr.attr, 849 &sensor_dev_attr_fan3_beep.dev_attr.attr, 850 851 NULL 852 }; 853 854 static umode_t nct7802_fan_is_visible(struct kobject *kobj, 855 struct attribute *attr, int index) 856 { 857 struct device *dev = kobj_to_dev(kobj); 858 struct nct7802_data *data = dev_get_drvdata(dev); 859 int fan = index / 4; /* 4 attributes per fan */ 860 unsigned int reg; 861 int err; 862 863 err = regmap_read(data->regmap, REG_FAN_ENABLE, ®); 864 if (err < 0 || !(reg & (1 << fan))) 865 return 0; 866 867 return attr->mode; 868 } 869 870 static const struct attribute_group nct7802_fan_group = { 871 .attrs = nct7802_fan_attrs, 872 .is_visible = nct7802_fan_is_visible, 873 }; 874 875 static struct attribute *nct7802_pwm_attrs[] = { 876 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 877 &sensor_dev_attr_pwm1_mode.dev_attr.attr, 878 &sensor_dev_attr_pwm1.dev_attr.attr, 879 &sensor_dev_attr_pwm2_enable.dev_attr.attr, 880 &sensor_dev_attr_pwm2_mode.dev_attr.attr, 881 &sensor_dev_attr_pwm2.dev_attr.attr, 882 &sensor_dev_attr_pwm3_enable.dev_attr.attr, 883 &sensor_dev_attr_pwm3_mode.dev_attr.attr, 884 &sensor_dev_attr_pwm3.dev_attr.attr, 885 NULL 886 }; 887 888 static const struct attribute_group nct7802_pwm_group = { 889 .attrs = nct7802_pwm_attrs, 890 }; 891 892 /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */ 893 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, temp, 0x80, 0); 894 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, temp, 0x81, 0); 895 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, temp, 0x82, 0); 896 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, temp, 0x83, 0); 897 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, temp, 0x84, 0); 898 899 /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */ 900 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm, 0x85); 901 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm, 0x86); 902 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_pwm, pwm, 0x87); 903 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_pwm, pwm, 0x88); 904 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm, 0); 905 906 /* 7.2.124 Table 2 X-axis Transition Point 1 Register */ 907 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, temp, 0x90, 0); 908 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, temp, 0x91, 0); 909 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, temp, 0x92, 0); 910 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, temp, 0x93, 0); 911 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, temp, 0x94, 0); 912 913 /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */ 914 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm, 0x95); 915 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm, 0x96); 916 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point3_pwm, pwm, 0x97); 917 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point4_pwm, pwm, 0x98); 918 static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point5_pwm, pwm, 0); 919 920 /* 7.2.133 Table 3 X-axis Transition Point 1 Register */ 921 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, temp, 0xA0, 0); 922 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, temp, 0xA1, 0); 923 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, temp, 0xA2, 0); 924 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, temp, 0xA3, 0); 925 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, temp, 0xA4, 0); 926 927 /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */ 928 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm, 0xA5); 929 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm, 0xA6); 930 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point3_pwm, pwm, 0xA7); 931 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point4_pwm, pwm, 0xA8); 932 static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point5_pwm, pwm, 0); 933 934 static struct attribute *nct7802_auto_point_attrs[] = { 935 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, 936 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, 937 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr, 938 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr, 939 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr, 940 941 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 942 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 943 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr, 944 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr, 945 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr, 946 947 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr, 948 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr, 949 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr, 950 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr, 951 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr, 952 953 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 954 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, 955 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr, 956 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr, 957 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr, 958 959 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr, 960 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr, 961 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr, 962 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr, 963 &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr, 964 965 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 966 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, 967 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr, 968 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr, 969 &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr, 970 971 NULL 972 }; 973 974 static const struct attribute_group nct7802_auto_point_group = { 975 .attrs = nct7802_auto_point_attrs, 976 }; 977 978 static const struct attribute_group *nct7802_groups[] = { 979 &nct7802_temp_group, 980 &nct7802_in_group, 981 &nct7802_fan_group, 982 &nct7802_pwm_group, 983 &nct7802_auto_point_group, 984 NULL 985 }; 986 987 static int nct7802_detect(struct i2c_client *client, 988 struct i2c_board_info *info) 989 { 990 int reg; 991 992 /* 993 * Chip identification registers are only available in bank 0, 994 * so only attempt chip detection if bank 0 is selected 995 */ 996 reg = i2c_smbus_read_byte_data(client, REG_BANK); 997 if (reg != 0x00) 998 return -ENODEV; 999 1000 reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID); 1001 if (reg != 0x50) 1002 return -ENODEV; 1003 1004 reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID); 1005 if (reg != 0xc3) 1006 return -ENODEV; 1007 1008 reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID); 1009 if (reg < 0 || (reg & 0xf0) != 0x20) 1010 return -ENODEV; 1011 1012 /* Also validate lower bits of voltage and temperature registers */ 1013 reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB); 1014 if (reg < 0 || (reg & 0x1f)) 1015 return -ENODEV; 1016 1017 reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB); 1018 if (reg < 0 || (reg & 0x3f)) 1019 return -ENODEV; 1020 1021 reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW); 1022 if (reg < 0 || (reg & 0x3f)) 1023 return -ENODEV; 1024 1025 strscpy(info->type, "nct7802", I2C_NAME_SIZE); 1026 return 0; 1027 } 1028 1029 static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg) 1030 { 1031 return (reg != REG_BANK && reg <= 0x20) || 1032 (reg >= REG_PWM(0) && reg <= REG_PWM(2)); 1033 } 1034 1035 static const struct regmap_config nct7802_regmap_config = { 1036 .reg_bits = 8, 1037 .val_bits = 8, 1038 .cache_type = REGCACHE_MAPLE, 1039 .volatile_reg = nct7802_regmap_is_volatile, 1040 }; 1041 1042 static int nct7802_get_channel_config(struct device *dev, 1043 struct device_node *node, u8 *mode_mask, 1044 u8 *mode_val) 1045 { 1046 u32 reg; 1047 const char *type_str, *md_str; 1048 u8 md; 1049 1050 if (!node->name || of_node_cmp(node->name, "channel")) 1051 return 0; 1052 1053 if (of_property_read_u32(node, "reg", ®)) { 1054 dev_err(dev, "Could not read reg value for '%s'\n", 1055 node->full_name); 1056 return -EINVAL; 1057 } 1058 1059 if (reg > 3) { 1060 dev_err(dev, "Invalid reg (%u) in '%s'\n", reg, 1061 node->full_name); 1062 return -EINVAL; 1063 } 1064 1065 if (reg == 0) { 1066 if (!of_device_is_available(node)) 1067 *mode_val &= ~MODE_LTD_EN; 1068 else 1069 *mode_val |= MODE_LTD_EN; 1070 *mode_mask |= MODE_LTD_EN; 1071 return 0; 1072 } 1073 1074 /* At this point we have reg >= 1 && reg <= 3 */ 1075 1076 if (!of_device_is_available(node)) { 1077 *mode_val &= ~(MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1)); 1078 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); 1079 return 0; 1080 } 1081 1082 if (of_property_read_string(node, "sensor-type", &type_str)) { 1083 dev_err(dev, "No type for '%s'\n", node->full_name); 1084 return -EINVAL; 1085 } 1086 1087 if (!strcmp(type_str, "voltage")) { 1088 *mode_val |= (RTD_MODE_VOLTAGE & MODE_RTD_MASK) 1089 << MODE_BIT_OFFSET_RTD(reg - 1); 1090 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); 1091 return 0; 1092 } 1093 1094 if (strcmp(type_str, "temperature")) { 1095 dev_err(dev, "Invalid type '%s' for '%s'\n", type_str, 1096 node->full_name); 1097 return -EINVAL; 1098 } 1099 1100 if (reg == 3) { 1101 /* RTD3 only supports thermistor mode */ 1102 md = RTD_MODE_THERMISTOR; 1103 } else { 1104 if (of_property_read_string(node, "temperature-mode", 1105 &md_str)) { 1106 dev_err(dev, "No mode for '%s'\n", node->full_name); 1107 return -EINVAL; 1108 } 1109 1110 if (!strcmp(md_str, "thermal-diode")) 1111 md = RTD_MODE_CURRENT; 1112 else if (!strcmp(md_str, "thermistor")) 1113 md = RTD_MODE_THERMISTOR; 1114 else { 1115 dev_err(dev, "Invalid mode '%s' for '%s'\n", md_str, 1116 node->full_name); 1117 return -EINVAL; 1118 } 1119 } 1120 1121 *mode_val |= (md & MODE_RTD_MASK) << MODE_BIT_OFFSET_RTD(reg - 1); 1122 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); 1123 1124 return 0; 1125 } 1126 1127 static int nct7802_configure_channels(struct device *dev, 1128 struct nct7802_data *data) 1129 { 1130 /* Enable local temperature sensor by default */ 1131 u8 mode_mask = MODE_LTD_EN, mode_val = MODE_LTD_EN; 1132 int err; 1133 1134 if (dev->of_node) { 1135 for_each_child_of_node_scoped(dev->of_node, node) { 1136 err = nct7802_get_channel_config(dev, node, &mode_mask, 1137 &mode_val); 1138 if (err) 1139 return err; 1140 } 1141 } 1142 1143 return regmap_update_bits(data->regmap, REG_MODE, mode_mask, mode_val); 1144 } 1145 1146 static int nct7802_init_chip(struct device *dev, struct nct7802_data *data) 1147 { 1148 int err; 1149 1150 /* Enable ADC */ 1151 err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01); 1152 if (err) 1153 return err; 1154 1155 err = nct7802_configure_channels(dev, data); 1156 if (err) 1157 return err; 1158 1159 /* Enable Vcore and VCC voltage monitoring */ 1160 return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03); 1161 } 1162 1163 static int nct7802_probe(struct i2c_client *client) 1164 { 1165 struct device *dev = &client->dev; 1166 struct nct7802_data *data; 1167 struct device *hwmon_dev; 1168 int ret; 1169 1170 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 1171 if (data == NULL) 1172 return -ENOMEM; 1173 1174 data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config); 1175 if (IS_ERR(data->regmap)) 1176 return PTR_ERR(data->regmap); 1177 1178 mutex_init(&data->access_lock); 1179 mutex_init(&data->in_alarm_lock); 1180 1181 ret = nct7802_init_chip(dev, data); 1182 if (ret < 0) 1183 return ret; 1184 1185 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 1186 data, 1187 nct7802_groups); 1188 return PTR_ERR_OR_ZERO(hwmon_dev); 1189 } 1190 1191 static const unsigned short nct7802_address_list[] = { 1192 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END 1193 }; 1194 1195 static const struct i2c_device_id nct7802_idtable[] = { 1196 { "nct7802" }, 1197 { } 1198 }; 1199 MODULE_DEVICE_TABLE(i2c, nct7802_idtable); 1200 1201 static struct i2c_driver nct7802_driver = { 1202 .class = I2C_CLASS_HWMON, 1203 .driver = { 1204 .name = DRVNAME, 1205 }, 1206 .detect = nct7802_detect, 1207 .probe = nct7802_probe, 1208 .id_table = nct7802_idtable, 1209 .address_list = nct7802_address_list, 1210 }; 1211 1212 module_i2c_driver(nct7802_driver); 1213 1214 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 1215 MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver"); 1216 MODULE_LICENSE("GPL v2"); 1217