1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * w83l786ng.c - Linux kernel driver for hardware monitoring 4 * Copyright (c) 2007 Kevin Lo <kevlo@kevlo.org> 5 */ 6 7 /* 8 * Supports following chips: 9 * 10 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA 11 * w83l786ng 3 2 2 2 0x7b 0x5ca3 yes no 12 */ 13 14 #include <linux/module.h> 15 #include <linux/init.h> 16 #include <linux/slab.h> 17 #include <linux/i2c.h> 18 #include <linux/hwmon.h> 19 #include <linux/hwmon-sysfs.h> 20 #include <linux/err.h> 21 #include <linux/mutex.h> 22 #include <linux/jiffies.h> 23 24 /* Addresses to scan */ 25 static const unsigned short normal_i2c[] = { 0x2e, 0x2f, I2C_CLIENT_END }; 26 27 /* Insmod parameters */ 28 29 static bool reset; 30 module_param(reset, bool, 0); 31 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended"); 32 33 #define W83L786NG_REG_IN_MIN(nr) (0x2C + (nr) * 2) 34 #define W83L786NG_REG_IN_MAX(nr) (0x2B + (nr) * 2) 35 #define W83L786NG_REG_IN(nr) ((nr) + 0x20) 36 37 #define W83L786NG_REG_FAN(nr) ((nr) + 0x28) 38 #define W83L786NG_REG_FAN_MIN(nr) ((nr) + 0x3B) 39 40 #define W83L786NG_REG_CONFIG 0x40 41 #define W83L786NG_REG_ALARM1 0x41 42 #define W83L786NG_REG_ALARM2 0x42 43 #define W83L786NG_REG_GPIO_EN 0x47 44 #define W83L786NG_REG_MAN_ID2 0x4C 45 #define W83L786NG_REG_MAN_ID1 0x4D 46 #define W83L786NG_REG_CHIP_ID 0x4E 47 48 #define W83L786NG_REG_DIODE 0x53 49 #define W83L786NG_REG_FAN_DIV 0x54 50 #define W83L786NG_REG_FAN_CFG 0x80 51 52 #define W83L786NG_REG_TOLERANCE 0x8D 53 54 static const u8 W83L786NG_REG_TEMP[2][3] = { 55 { 0x25, /* TEMP 0 in DataSheet */ 56 0x35, /* TEMP 0 Over in DataSheet */ 57 0x36 }, /* TEMP 0 Hyst in DataSheet */ 58 { 0x26, /* TEMP 1 in DataSheet */ 59 0x37, /* TEMP 1 Over in DataSheet */ 60 0x38 } /* TEMP 1 Hyst in DataSheet */ 61 }; 62 63 static const u8 W83L786NG_PWM_MODE_SHIFT[] = {6, 7}; 64 static const u8 W83L786NG_PWM_ENABLE_SHIFT[] = {2, 4}; 65 66 /* FAN Duty Cycle, be used to control */ 67 static const u8 W83L786NG_REG_PWM[] = {0x81, 0x87}; 68 69 70 static inline u8 71 FAN_TO_REG(long rpm, int div) 72 { 73 if (rpm == 0) 74 return 255; 75 rpm = clamp_val(rpm, 1, 1000000); 76 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); 77 } 78 79 static int fan_from_reg(int val, int div) 80 { 81 if (val == 0) 82 return -1; 83 if (val == 255) 84 return 0; 85 return 1350000 / (val * div); 86 } 87 88 /* for temp */ 89 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (val) + 0x100 * 1000 \ 90 : (val)) / 1000, 0, 0xff)) 91 92 static int temp_from_reg(int val) 93 { 94 if (val & 0x80) 95 return (val - 0x100) * 1000; 96 return val * 1000; 97 } 98 99 /* 100 * The analog voltage inputs have 8mV LSB. Since the sysfs output is 101 * in mV as would be measured on the chip input pin, need to just 102 * multiply/divide by 8 to translate from/to register values. 103 */ 104 #define IN_TO_REG(val) (clamp_val((((val) + 4) / 8), 0, 255)) 105 #define IN_FROM_REG(val) ((val) * 8) 106 107 #define DIV_FROM_REG(val) (1 << (val)) 108 109 static inline u8 110 DIV_TO_REG(long val) 111 { 112 int i; 113 val = clamp_val(val, 1, 128) >> 1; 114 for (i = 0; i < 7; i++) { 115 if (val == 0) 116 break; 117 val >>= 1; 118 } 119 return (u8)i; 120 } 121 122 struct w83l786ng_data { 123 struct i2c_client *client; 124 struct mutex update_lock; 125 bool valid; /* true if following fields are valid */ 126 unsigned long last_updated; /* In jiffies */ 127 unsigned long last_nonvolatile; /* In jiffies, last time we update the 128 * nonvolatile registers */ 129 130 u8 in[3]; 131 u8 in_max[3]; 132 u8 in_min[3]; 133 u8 fan[2]; 134 u8 fan_div[2]; 135 u8 fan_min[2]; 136 u8 temp_type[2]; 137 u8 temp[2][3]; 138 u8 pwm[2]; 139 u8 pwm_mode[2]; /* 0->DC variable voltage 140 * 1->PWM variable duty cycle */ 141 142 u8 pwm_enable[2]; /* 1->manual 143 * 2->thermal cruise (also called SmartFan I) */ 144 u8 tolerance[2]; 145 }; 146 147 static u8 148 w83l786ng_read_value(struct i2c_client *client, u8 reg) 149 { 150 return i2c_smbus_read_byte_data(client, reg); 151 } 152 153 static int 154 w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value) 155 { 156 return i2c_smbus_write_byte_data(client, reg, value); 157 } 158 159 static struct w83l786ng_data *w83l786ng_update_device(struct device *dev) 160 { 161 struct w83l786ng_data *data = dev_get_drvdata(dev); 162 struct i2c_client *client = data->client; 163 int i, j; 164 u8 reg_tmp, pwmcfg; 165 166 mutex_lock(&data->update_lock); 167 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) 168 || !data->valid) { 169 dev_dbg(&client->dev, "Updating w83l786ng data.\n"); 170 171 /* Update the voltages measured value and limits */ 172 for (i = 0; i < 3; i++) { 173 data->in[i] = w83l786ng_read_value(client, 174 W83L786NG_REG_IN(i)); 175 data->in_min[i] = w83l786ng_read_value(client, 176 W83L786NG_REG_IN_MIN(i)); 177 data->in_max[i] = w83l786ng_read_value(client, 178 W83L786NG_REG_IN_MAX(i)); 179 } 180 181 /* Update the fan counts and limits */ 182 for (i = 0; i < 2; i++) { 183 data->fan[i] = w83l786ng_read_value(client, 184 W83L786NG_REG_FAN(i)); 185 data->fan_min[i] = w83l786ng_read_value(client, 186 W83L786NG_REG_FAN_MIN(i)); 187 } 188 189 /* Update the fan divisor */ 190 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV); 191 data->fan_div[0] = reg_tmp & 0x07; 192 data->fan_div[1] = (reg_tmp >> 4) & 0x07; 193 194 pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG); 195 for (i = 0; i < 2; i++) { 196 data->pwm_mode[i] = 197 ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1) 198 ? 0 : 1; 199 data->pwm_enable[i] = 200 ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1; 201 data->pwm[i] = 202 (w83l786ng_read_value(client, W83L786NG_REG_PWM[i]) 203 & 0x0f) * 0x11; 204 } 205 206 207 /* Update the temperature sensors */ 208 for (i = 0; i < 2; i++) { 209 for (j = 0; j < 3; j++) { 210 data->temp[i][j] = w83l786ng_read_value(client, 211 W83L786NG_REG_TEMP[i][j]); 212 } 213 } 214 215 /* Update Smart Fan I/II tolerance */ 216 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE); 217 data->tolerance[0] = reg_tmp & 0x0f; 218 data->tolerance[1] = (reg_tmp >> 4) & 0x0f; 219 220 data->last_updated = jiffies; 221 data->valid = true; 222 223 } 224 225 mutex_unlock(&data->update_lock); 226 227 return data; 228 } 229 230 /* following are the sysfs callback functions */ 231 #define show_in_reg(reg) \ 232 static ssize_t \ 233 show_##reg(struct device *dev, struct device_attribute *attr, \ 234 char *buf) \ 235 { \ 236 int nr = to_sensor_dev_attr(attr)->index; \ 237 struct w83l786ng_data *data = w83l786ng_update_device(dev); \ 238 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \ 239 } 240 241 show_in_reg(in) 242 show_in_reg(in_min) 243 show_in_reg(in_max) 244 245 #define store_in_reg(REG, reg) \ 246 static ssize_t \ 247 store_in_##reg(struct device *dev, struct device_attribute *attr, \ 248 const char *buf, size_t count) \ 249 { \ 250 int nr = to_sensor_dev_attr(attr)->index; \ 251 struct w83l786ng_data *data = dev_get_drvdata(dev); \ 252 struct i2c_client *client = data->client; \ 253 unsigned long val; \ 254 int err = kstrtoul(buf, 10, &val); \ 255 if (err) \ 256 return err; \ 257 mutex_lock(&data->update_lock); \ 258 data->in_##reg[nr] = IN_TO_REG(val); \ 259 w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \ 260 data->in_##reg[nr]); \ 261 mutex_unlock(&data->update_lock); \ 262 return count; \ 263 } 264 265 store_in_reg(MIN, min) 266 store_in_reg(MAX, max) 267 268 static struct sensor_device_attribute sda_in_input[] = { 269 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0), 270 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1), 271 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2), 272 }; 273 274 static struct sensor_device_attribute sda_in_min[] = { 275 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0), 276 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1), 277 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2), 278 }; 279 280 static struct sensor_device_attribute sda_in_max[] = { 281 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0), 282 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1), 283 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2), 284 }; 285 286 #define show_fan_reg(reg) \ 287 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ 288 char *buf) \ 289 { \ 290 int nr = to_sensor_dev_attr(attr)->index; \ 291 struct w83l786ng_data *data = w83l786ng_update_device(dev); \ 292 return sprintf(buf, "%d\n", \ 293 fan_from_reg(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \ 294 } 295 296 show_fan_reg(fan); 297 show_fan_reg(fan_min); 298 299 static ssize_t 300 store_fan_min(struct device *dev, struct device_attribute *attr, 301 const char *buf, size_t count) 302 { 303 int nr = to_sensor_dev_attr(attr)->index; 304 struct w83l786ng_data *data = dev_get_drvdata(dev); 305 struct i2c_client *client = data->client; 306 unsigned long val; 307 int err; 308 309 err = kstrtoul(buf, 10, &val); 310 if (err) 311 return err; 312 313 mutex_lock(&data->update_lock); 314 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); 315 w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr), 316 data->fan_min[nr]); 317 mutex_unlock(&data->update_lock); 318 319 return count; 320 } 321 322 static ssize_t 323 show_fan_div(struct device *dev, struct device_attribute *attr, 324 char *buf) 325 { 326 int nr = to_sensor_dev_attr(attr)->index; 327 struct w83l786ng_data *data = w83l786ng_update_device(dev); 328 return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr])); 329 } 330 331 /* 332 * Note: we save and restore the fan minimum here, because its value is 333 * determined in part by the fan divisor. This follows the principle of 334 * least surprise; the user doesn't expect the fan minimum to change just 335 * because the divisor changed. 336 */ 337 static ssize_t 338 store_fan_div(struct device *dev, struct device_attribute *attr, 339 const char *buf, size_t count) 340 { 341 int nr = to_sensor_dev_attr(attr)->index; 342 struct w83l786ng_data *data = dev_get_drvdata(dev); 343 struct i2c_client *client = data->client; 344 345 unsigned long min; 346 u8 tmp_fan_div; 347 u8 fan_div_reg; 348 u8 keep_mask = 0; 349 u8 new_shift = 0; 350 351 unsigned long val; 352 int err; 353 354 err = kstrtoul(buf, 10, &val); 355 if (err) 356 return err; 357 358 /* Save fan_min */ 359 mutex_lock(&data->update_lock); 360 min = fan_from_reg(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])); 361 362 data->fan_div[nr] = DIV_TO_REG(val); 363 364 switch (nr) { 365 case 0: 366 keep_mask = 0xf8; 367 new_shift = 0; 368 break; 369 case 1: 370 keep_mask = 0x8f; 371 new_shift = 4; 372 break; 373 } 374 375 fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV) 376 & keep_mask; 377 378 tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask; 379 380 w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV, 381 fan_div_reg | tmp_fan_div); 382 383 /* Restore fan_min */ 384 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); 385 w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr), 386 data->fan_min[nr]); 387 mutex_unlock(&data->update_lock); 388 389 return count; 390 } 391 392 static struct sensor_device_attribute sda_fan_input[] = { 393 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0), 394 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1), 395 }; 396 397 static struct sensor_device_attribute sda_fan_min[] = { 398 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, 399 store_fan_min, 0), 400 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, 401 store_fan_min, 1), 402 }; 403 404 static struct sensor_device_attribute sda_fan_div[] = { 405 SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div, 406 store_fan_div, 0), 407 SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div, 408 store_fan_div, 1), 409 }; 410 411 412 /* read/write the temperature, includes measured value and limits */ 413 414 static ssize_t 415 show_temp(struct device *dev, struct device_attribute *attr, char *buf) 416 { 417 struct sensor_device_attribute_2 *sensor_attr = 418 to_sensor_dev_attr_2(attr); 419 int nr = sensor_attr->nr; 420 int index = sensor_attr->index; 421 struct w83l786ng_data *data = w83l786ng_update_device(dev); 422 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr][index])); 423 } 424 425 static ssize_t 426 store_temp(struct device *dev, struct device_attribute *attr, 427 const char *buf, size_t count) 428 { 429 struct sensor_device_attribute_2 *sensor_attr = 430 to_sensor_dev_attr_2(attr); 431 int nr = sensor_attr->nr; 432 int index = sensor_attr->index; 433 struct w83l786ng_data *data = dev_get_drvdata(dev); 434 struct i2c_client *client = data->client; 435 long val; 436 int err; 437 438 err = kstrtol(buf, 10, &val); 439 if (err) 440 return err; 441 442 mutex_lock(&data->update_lock); 443 data->temp[nr][index] = TEMP_TO_REG(val); 444 w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index], 445 data->temp[nr][index]); 446 mutex_unlock(&data->update_lock); 447 448 return count; 449 } 450 451 static struct sensor_device_attribute_2 sda_temp_input[] = { 452 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0), 453 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0), 454 }; 455 456 static struct sensor_device_attribute_2 sda_temp_max[] = { 457 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, 458 show_temp, store_temp, 0, 1), 459 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, 460 show_temp, store_temp, 1, 1), 461 }; 462 463 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = { 464 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, 465 show_temp, store_temp, 0, 2), 466 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, 467 show_temp, store_temp, 1, 2), 468 }; 469 470 #define show_pwm_reg(reg) \ 471 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ 472 char *buf) \ 473 { \ 474 struct w83l786ng_data *data = w83l786ng_update_device(dev); \ 475 int nr = to_sensor_dev_attr(attr)->index; \ 476 return sprintf(buf, "%d\n", data->reg[nr]); \ 477 } 478 479 show_pwm_reg(pwm_mode) 480 show_pwm_reg(pwm_enable) 481 show_pwm_reg(pwm) 482 483 static ssize_t 484 store_pwm_mode(struct device *dev, struct device_attribute *attr, 485 const char *buf, size_t count) 486 { 487 int nr = to_sensor_dev_attr(attr)->index; 488 struct w83l786ng_data *data = dev_get_drvdata(dev); 489 struct i2c_client *client = data->client; 490 u8 reg; 491 unsigned long val; 492 int err; 493 494 err = kstrtoul(buf, 10, &val); 495 if (err) 496 return err; 497 498 if (val > 1) 499 return -EINVAL; 500 mutex_lock(&data->update_lock); 501 data->pwm_mode[nr] = val; 502 reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG); 503 reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]); 504 if (!val) 505 reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr]; 506 w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg); 507 mutex_unlock(&data->update_lock); 508 return count; 509 } 510 511 static ssize_t 512 store_pwm(struct device *dev, struct device_attribute *attr, 513 const char *buf, size_t count) 514 { 515 int nr = to_sensor_dev_attr(attr)->index; 516 struct w83l786ng_data *data = dev_get_drvdata(dev); 517 struct i2c_client *client = data->client; 518 unsigned long val; 519 int err; 520 521 err = kstrtoul(buf, 10, &val); 522 if (err) 523 return err; 524 val = clamp_val(val, 0, 255); 525 val = DIV_ROUND_CLOSEST(val, 0x11); 526 527 mutex_lock(&data->update_lock); 528 data->pwm[nr] = val * 0x11; 529 val |= w83l786ng_read_value(client, W83L786NG_REG_PWM[nr]) & 0xf0; 530 w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val); 531 mutex_unlock(&data->update_lock); 532 return count; 533 } 534 535 static ssize_t 536 store_pwm_enable(struct device *dev, struct device_attribute *attr, 537 const char *buf, size_t count) 538 { 539 int nr = to_sensor_dev_attr(attr)->index; 540 struct w83l786ng_data *data = dev_get_drvdata(dev); 541 struct i2c_client *client = data->client; 542 u8 reg; 543 unsigned long val; 544 int err; 545 546 err = kstrtoul(buf, 10, &val); 547 if (err) 548 return err; 549 550 if (!val || val > 2) /* only modes 1 and 2 are supported */ 551 return -EINVAL; 552 553 mutex_lock(&data->update_lock); 554 reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG); 555 data->pwm_enable[nr] = val; 556 reg &= ~(0x03 << W83L786NG_PWM_ENABLE_SHIFT[nr]); 557 reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr]; 558 w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg); 559 mutex_unlock(&data->update_lock); 560 return count; 561 } 562 563 static struct sensor_device_attribute sda_pwm[] = { 564 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0), 565 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1), 566 }; 567 568 static struct sensor_device_attribute sda_pwm_mode[] = { 569 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode, 570 store_pwm_mode, 0), 571 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode, 572 store_pwm_mode, 1), 573 }; 574 575 static struct sensor_device_attribute sda_pwm_enable[] = { 576 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable, 577 store_pwm_enable, 0), 578 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable, 579 store_pwm_enable, 1), 580 }; 581 582 /* For Smart Fan I/Thermal Cruise and Smart Fan II */ 583 static ssize_t 584 show_tolerance(struct device *dev, struct device_attribute *attr, char *buf) 585 { 586 int nr = to_sensor_dev_attr(attr)->index; 587 struct w83l786ng_data *data = w83l786ng_update_device(dev); 588 return sprintf(buf, "%ld\n", (long)data->tolerance[nr]); 589 } 590 591 static ssize_t 592 store_tolerance(struct device *dev, struct device_attribute *attr, 593 const char *buf, size_t count) 594 { 595 int nr = to_sensor_dev_attr(attr)->index; 596 struct w83l786ng_data *data = dev_get_drvdata(dev); 597 struct i2c_client *client = data->client; 598 u8 tol_tmp, tol_mask; 599 unsigned long val; 600 int err; 601 602 err = kstrtoul(buf, 10, &val); 603 if (err) 604 return err; 605 606 mutex_lock(&data->update_lock); 607 tol_mask = w83l786ng_read_value(client, 608 W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0); 609 tol_tmp = clamp_val(val, 0, 15); 610 tol_tmp &= 0x0f; 611 data->tolerance[nr] = tol_tmp; 612 if (nr == 1) 613 tol_tmp <<= 4; 614 615 w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE, 616 tol_mask | tol_tmp); 617 mutex_unlock(&data->update_lock); 618 return count; 619 } 620 621 static struct sensor_device_attribute sda_tolerance[] = { 622 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, 623 show_tolerance, store_tolerance, 0), 624 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, 625 show_tolerance, store_tolerance, 1), 626 }; 627 628 629 #define IN_UNIT_ATTRS(X) \ 630 &sda_in_input[X].dev_attr.attr, \ 631 &sda_in_min[X].dev_attr.attr, \ 632 &sda_in_max[X].dev_attr.attr 633 634 #define FAN_UNIT_ATTRS(X) \ 635 &sda_fan_input[X].dev_attr.attr, \ 636 &sda_fan_min[X].dev_attr.attr, \ 637 &sda_fan_div[X].dev_attr.attr 638 639 #define TEMP_UNIT_ATTRS(X) \ 640 &sda_temp_input[X].dev_attr.attr, \ 641 &sda_temp_max[X].dev_attr.attr, \ 642 &sda_temp_max_hyst[X].dev_attr.attr 643 644 #define PWM_UNIT_ATTRS(X) \ 645 &sda_pwm[X].dev_attr.attr, \ 646 &sda_pwm_mode[X].dev_attr.attr, \ 647 &sda_pwm_enable[X].dev_attr.attr 648 649 #define TOLERANCE_UNIT_ATTRS(X) \ 650 &sda_tolerance[X].dev_attr.attr 651 652 static struct attribute *w83l786ng_attrs[] = { 653 IN_UNIT_ATTRS(0), 654 IN_UNIT_ATTRS(1), 655 IN_UNIT_ATTRS(2), 656 FAN_UNIT_ATTRS(0), 657 FAN_UNIT_ATTRS(1), 658 TEMP_UNIT_ATTRS(0), 659 TEMP_UNIT_ATTRS(1), 660 PWM_UNIT_ATTRS(0), 661 PWM_UNIT_ATTRS(1), 662 TOLERANCE_UNIT_ATTRS(0), 663 TOLERANCE_UNIT_ATTRS(1), 664 NULL 665 }; 666 667 ATTRIBUTE_GROUPS(w83l786ng); 668 669 static int 670 w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info) 671 { 672 struct i2c_adapter *adapter = client->adapter; 673 u16 man_id; 674 u8 chip_id; 675 676 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 677 return -ENODEV; 678 679 /* Detection */ 680 if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) { 681 dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n", 682 client->addr); 683 return -ENODEV; 684 } 685 686 /* Identification */ 687 man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) + 688 w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2); 689 chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID); 690 691 if (man_id != 0x5CA3 || /* Winbond */ 692 chip_id != 0x80) { /* W83L786NG */ 693 dev_dbg(&adapter->dev, 694 "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n", 695 man_id, chip_id); 696 return -ENODEV; 697 } 698 699 strscpy(info->type, "w83l786ng", I2C_NAME_SIZE); 700 701 return 0; 702 } 703 704 static void w83l786ng_init_client(struct i2c_client *client) 705 { 706 u8 tmp; 707 708 if (reset) 709 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80); 710 711 /* Start monitoring */ 712 tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG); 713 if (!(tmp & 0x01)) 714 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01); 715 } 716 717 static int 718 w83l786ng_probe(struct i2c_client *client) 719 { 720 struct device *dev = &client->dev; 721 struct w83l786ng_data *data; 722 struct device *hwmon_dev; 723 int i; 724 u8 reg_tmp; 725 726 data = devm_kzalloc(dev, sizeof(struct w83l786ng_data), GFP_KERNEL); 727 if (!data) 728 return -ENOMEM; 729 730 data->client = client; 731 mutex_init(&data->update_lock); 732 733 /* Initialize the chip */ 734 w83l786ng_init_client(client); 735 736 /* A few vars need to be filled upon startup */ 737 for (i = 0; i < 2; i++) { 738 data->fan_min[i] = w83l786ng_read_value(client, 739 W83L786NG_REG_FAN_MIN(i)); 740 } 741 742 /* Update the fan divisor */ 743 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV); 744 data->fan_div[0] = reg_tmp & 0x07; 745 data->fan_div[1] = (reg_tmp >> 4) & 0x07; 746 747 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 748 data, 749 w83l786ng_groups); 750 return PTR_ERR_OR_ZERO(hwmon_dev); 751 } 752 753 static const struct i2c_device_id w83l786ng_id[] = { 754 { "w83l786ng" }, 755 { } 756 }; 757 MODULE_DEVICE_TABLE(i2c, w83l786ng_id); 758 759 static struct i2c_driver w83l786ng_driver = { 760 .class = I2C_CLASS_HWMON, 761 .driver = { 762 .name = "w83l786ng", 763 }, 764 .probe = w83l786ng_probe, 765 .id_table = w83l786ng_id, 766 .detect = w83l786ng_detect, 767 .address_list = normal_i2c, 768 }; 769 770 module_i2c_driver(w83l786ng_driver); 771 772 MODULE_AUTHOR("Kevin Lo"); 773 MODULE_DESCRIPTION("w83l786ng driver"); 774 MODULE_LICENSE("GPL"); 775