1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * A hwmon driver for the Analog Devices ADT7470 4 * Copyright (C) 2007 IBM 5 * 6 * Author: Darrick J. Wong <darrick.wong@oracle.com> 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/module.h> 12 #include <linux/jiffies.h> 13 #include <linux/i2c.h> 14 #include <linux/hwmon.h> 15 #include <linux/hwmon-sysfs.h> 16 #include <linux/err.h> 17 #include <linux/mutex.h> 18 #include <linux/delay.h> 19 #include <linux/log2.h> 20 #include <linux/kthread.h> 21 #include <linux/regmap.h> 22 #include <linux/slab.h> 23 #include <linux/util_macros.h> 24 25 /* Addresses to scan */ 26 static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END }; 27 28 /* ADT7470 registers */ 29 #define ADT7470_REG_BASE_ADDR 0x20 30 #define ADT7470_REG_TEMP_BASE_ADDR 0x20 31 #define ADT7470_REG_TEMP_MAX_ADDR 0x29 32 #define ADT7470_REG_FAN_BASE_ADDR 0x2A 33 #define ADT7470_REG_FAN_MAX_ADDR 0x31 34 #define ADT7470_REG_PWM_BASE_ADDR 0x32 35 #define ADT7470_REG_PWM_MAX_ADDR 0x35 36 #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38 37 #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B 38 #define ADT7470_REG_CFG 0x40 39 #define ADT7470_STRT_MASK 0x01 40 #define ADT7470_TEST_MASK 0x02 41 #define ADT7470_FSPD_MASK 0x04 42 #define ADT7470_T05_STB_MASK 0x80 43 #define ADT7470_REG_ALARM1 0x41 44 #define ADT7470_R1T_ALARM 0x01 45 #define ADT7470_R2T_ALARM 0x02 46 #define ADT7470_R3T_ALARM 0x04 47 #define ADT7470_R4T_ALARM 0x08 48 #define ADT7470_R5T_ALARM 0x10 49 #define ADT7470_R6T_ALARM 0x20 50 #define ADT7470_R7T_ALARM 0x40 51 #define ADT7470_OOL_ALARM 0x80 52 #define ADT7470_REG_ALARM2 0x42 53 #define ADT7470_R8T_ALARM 0x01 54 #define ADT7470_R9T_ALARM 0x02 55 #define ADT7470_R10T_ALARM 0x04 56 #define ADT7470_FAN1_ALARM 0x10 57 #define ADT7470_FAN2_ALARM 0x20 58 #define ADT7470_FAN3_ALARM 0x40 59 #define ADT7470_FAN4_ALARM 0x80 60 #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44 61 #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57 62 #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58 63 #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F 64 #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60 65 #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67 66 #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68 67 #define ADT7470_REG_PWM12_CFG 0x68 68 #define ADT7470_PWM2_AUTO_MASK 0x40 69 #define ADT7470_PWM1_AUTO_MASK 0x80 70 #define ADT7470_PWM_AUTO_MASK 0xC0 71 #define ADT7470_REG_PWM34_CFG 0x69 72 #define ADT7470_PWM3_AUTO_MASK 0x40 73 #define ADT7470_PWM4_AUTO_MASK 0x80 74 #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A 75 #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D 76 #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E 77 #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71 78 #define ADT7470_REG_CFG_2 0x74 79 #define ADT7470_REG_ACOUSTICS12 0x75 80 #define ADT7470_REG_ACOUSTICS34 0x76 81 #define ADT7470_REG_DEVICE 0x3D 82 #define ADT7470_REG_VENDOR 0x3E 83 #define ADT7470_REG_REVISION 0x3F 84 #define ADT7470_REG_ALARM1_MASK 0x72 85 #define ADT7470_REG_ALARM2_MASK 0x73 86 #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C 87 #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D 88 #define ADT7470_REG_MAX_ADDR 0x81 89 90 #define ADT7470_TEMP_COUNT 10 91 #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x)) 92 #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2)) 93 #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \ 94 ((x) * 2) + 1) 95 96 #define ADT7470_FAN_COUNT 4 97 #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2)) 98 #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2)) 99 #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2)) 100 101 #define ADT7470_PWM_COUNT 4 102 #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x)) 103 #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x)) 104 #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x)) 105 #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x)) 106 #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2)) 107 #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \ 108 ((x) / 2)) 109 110 #define ALARM2(x) ((x) << 8) 111 112 #define ADT7470_VENDOR 0x41 113 #define ADT7470_DEVICE 0x70 114 /* datasheet only mentions a revision 2 */ 115 #define ADT7470_REVISION 0x02 116 117 /* "all temps" according to hwmon sysfs interface spec */ 118 #define ADT7470_PWM_ALL_TEMPS 0x3FF 119 120 /* How often do we reread sensors values? (In jiffies) */ 121 #define SENSOR_REFRESH_INTERVAL (5 * HZ) 122 123 /* How often do we reread sensor limit values? (In jiffies) */ 124 #define LIMIT_REFRESH_INTERVAL (60 * HZ) 125 126 /* Wait at least 200ms per sensor for 10 sensors */ 127 #define TEMP_COLLECTION_TIME 2000 128 129 /* auto update thing won't fire more than every 2s */ 130 #define AUTO_UPDATE_INTERVAL 2000 131 132 /* datasheet says to divide this number by the fan reading to get fan rpm */ 133 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x)) 134 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM 135 #define FAN_PERIOD_INVALID 65535 136 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID) 137 138 /* Config registers 1 and 2 include fields for selecting the PWM frequency */ 139 #define ADT7470_CFG_LF 0x40 140 #define ADT7470_FREQ_MASK 0x70 141 #define ADT7470_FREQ_SHIFT 4 142 143 struct adt7470_data { 144 struct regmap *regmap; 145 struct mutex lock; 146 char sensors_valid; 147 char limits_valid; 148 unsigned long sensors_last_updated; /* In jiffies */ 149 unsigned long limits_last_updated; /* In jiffies */ 150 151 int num_temp_sensors; /* -1 = probe */ 152 int temperatures_probed; 153 154 s8 temp[ADT7470_TEMP_COUNT]; 155 s8 temp_min[ADT7470_TEMP_COUNT]; 156 s8 temp_max[ADT7470_TEMP_COUNT]; 157 u16 fan[ADT7470_FAN_COUNT]; 158 u16 fan_min[ADT7470_FAN_COUNT]; 159 u16 fan_max[ADT7470_FAN_COUNT]; 160 u16 alarm; 161 u16 alarms_mask; 162 u8 force_pwm_max; 163 u8 pwm[ADT7470_PWM_COUNT]; 164 u8 pwm_max[ADT7470_PWM_COUNT]; 165 u8 pwm_automatic[ADT7470_PWM_COUNT]; 166 u8 pwm_min[ADT7470_PWM_COUNT]; 167 s8 pwm_tmin[ADT7470_PWM_COUNT]; 168 u8 pwm_auto_temp[ADT7470_PWM_COUNT]; 169 170 struct task_struct *auto_update; 171 unsigned int auto_update_interval; 172 }; 173 174 /* 175 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says 176 * that the low byte must be read before the high byte. 177 */ 178 static inline int adt7470_read_word_data(struct adt7470_data *data, unsigned int reg, 179 unsigned int *val) 180 { 181 u8 regval[2]; 182 int err; 183 184 err = regmap_bulk_read(data->regmap, reg, ®val, 2); 185 if (err < 0) 186 return err; 187 188 *val = regval[0] | (regval[1] << 8); 189 190 return 0; 191 } 192 193 static inline int adt7470_write_word_data(struct adt7470_data *data, unsigned int reg, 194 unsigned int val) 195 { 196 u8 regval[2]; 197 198 regval[0] = val & 0xFF; 199 regval[1] = val >> 8; 200 201 return regmap_bulk_write(data->regmap, reg, ®val, 2); 202 } 203 204 /* Probe for temperature sensors. Assumes lock is held */ 205 static int adt7470_read_temperatures(struct adt7470_data *data) 206 { 207 unsigned long res; 208 unsigned int pwm_cfg[2]; 209 int err; 210 int i; 211 u8 pwm[ADT7470_FAN_COUNT]; 212 213 /* save pwm[1-4] config register */ 214 err = regmap_read(data->regmap, ADT7470_REG_PWM_CFG(0), &pwm_cfg[0]); 215 if (err < 0) 216 return err; 217 err = regmap_read(data->regmap, ADT7470_REG_PWM_CFG(2), &pwm_cfg[1]); 218 if (err < 0) 219 return err; 220 221 /* set manual pwm to whatever it is set to now */ 222 err = regmap_bulk_read(data->regmap, ADT7470_REG_PWM(0), &pwm[0], 223 ADT7470_PWM_COUNT); 224 if (err < 0) 225 return err; 226 227 /* put pwm in manual mode */ 228 err = regmap_update_bits(data->regmap, ADT7470_REG_PWM_CFG(0), 229 ADT7470_PWM_AUTO_MASK, 0); 230 if (err < 0) 231 return err; 232 err = regmap_update_bits(data->regmap, ADT7470_REG_PWM_CFG(2), 233 ADT7470_PWM_AUTO_MASK, 0); 234 if (err < 0) 235 return err; 236 237 /* write pwm control to whatever it was */ 238 err = regmap_bulk_write(data->regmap, ADT7470_REG_PWM(0), &pwm[0], 239 ADT7470_PWM_COUNT); 240 if (err < 0) 241 return err; 242 243 /* start reading temperature sensors */ 244 err = regmap_update_bits(data->regmap, ADT7470_REG_CFG, 245 ADT7470_T05_STB_MASK, ADT7470_T05_STB_MASK); 246 if (err < 0) 247 return err; 248 249 /* Delay is 200ms * number of temp sensors. */ 250 res = msleep_interruptible((data->num_temp_sensors >= 0 ? 251 data->num_temp_sensors * 200 : 252 TEMP_COLLECTION_TIME)); 253 254 /* done reading temperature sensors */ 255 err = regmap_update_bits(data->regmap, ADT7470_REG_CFG, 256 ADT7470_T05_STB_MASK, 0); 257 if (err < 0) 258 return err; 259 260 /* restore pwm[1-4] config registers */ 261 err = regmap_write(data->regmap, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]); 262 if (err < 0) 263 return err; 264 err = regmap_write(data->regmap, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]); 265 if (err < 0) 266 return err; 267 268 if (res) 269 return -EAGAIN; 270 271 /* Only count fans if we have to */ 272 if (data->num_temp_sensors >= 0) 273 return 0; 274 275 err = regmap_bulk_read(data->regmap, ADT7470_TEMP_REG(0), &data->temp[0], 276 ADT7470_TEMP_COUNT); 277 if (err < 0) 278 return err; 279 for (i = 0; i < ADT7470_TEMP_COUNT; i++) { 280 if (data->temp[i]) 281 data->num_temp_sensors = i + 1; 282 } 283 data->temperatures_probed = 1; 284 return 0; 285 } 286 287 static int adt7470_update_thread(void *p) 288 { 289 struct i2c_client *client = p; 290 struct adt7470_data *data = i2c_get_clientdata(client); 291 292 while (!kthread_should_stop()) { 293 mutex_lock(&data->lock); 294 adt7470_read_temperatures(data); 295 mutex_unlock(&data->lock); 296 297 set_current_state(TASK_INTERRUPTIBLE); 298 if (kthread_should_stop()) 299 break; 300 301 schedule_timeout(msecs_to_jiffies(data->auto_update_interval)); 302 } 303 304 return 0; 305 } 306 307 static int adt7470_update_sensors(struct adt7470_data *data) 308 { 309 unsigned int val; 310 int err; 311 int i; 312 313 if (!data->temperatures_probed) 314 err = adt7470_read_temperatures(data); 315 else 316 err = regmap_bulk_read(data->regmap, ADT7470_TEMP_REG(0), &data->temp[0], 317 ADT7470_TEMP_COUNT); 318 if (err < 0) 319 return err; 320 321 for (i = 0; i < ADT7470_FAN_COUNT; i++) { 322 err = adt7470_read_word_data(data, ADT7470_REG_FAN(i), &val); 323 if (err < 0) 324 return err; 325 data->fan[i] = val; 326 } 327 328 err = regmap_bulk_read(data->regmap, ADT7470_REG_PWM(0), &data->pwm[0], ADT7470_PWM_COUNT); 329 if (err < 0) 330 return err; 331 332 for (i = 0; i < ADT7470_PWM_COUNT; i++) { 333 unsigned int mask; 334 335 if (i % 2) 336 mask = ADT7470_PWM2_AUTO_MASK; 337 else 338 mask = ADT7470_PWM1_AUTO_MASK; 339 340 err = regmap_read(data->regmap, ADT7470_REG_PWM_CFG(i), &val); 341 if (err < 0) 342 return err; 343 data->pwm_automatic[i] = !!(val & mask); 344 345 err = regmap_read(data->regmap, ADT7470_REG_PWM_AUTO_TEMP(i), &val); 346 if (err < 0) 347 return err; 348 if (!(i % 2)) 349 data->pwm_auto_temp[i] = val >> 4; 350 else 351 data->pwm_auto_temp[i] = val & 0xF; 352 } 353 354 err = regmap_read(data->regmap, ADT7470_REG_CFG, &val); 355 if (err < 0) 356 return err; 357 data->force_pwm_max = !!(val & ADT7470_FSPD_MASK); 358 359 err = regmap_read(data->regmap, ADT7470_REG_ALARM1, &val); 360 if (err < 0) 361 return err; 362 data->alarm = val; 363 if (data->alarm & ADT7470_OOL_ALARM) { 364 err = regmap_read(data->regmap, ADT7470_REG_ALARM2, &val); 365 if (err < 0) 366 return err; 367 data->alarm |= ALARM2(val); 368 } 369 370 err = adt7470_read_word_data(data, ADT7470_REG_ALARM1_MASK, &val); 371 if (err < 0) 372 return err; 373 data->alarms_mask = val; 374 375 return 0; 376 } 377 378 static int adt7470_update_limits(struct adt7470_data *data) 379 { 380 unsigned int val; 381 int err; 382 int i; 383 384 for (i = 0; i < ADT7470_TEMP_COUNT; i++) { 385 err = regmap_read(data->regmap, ADT7470_TEMP_MIN_REG(i), &val); 386 if (err < 0) 387 return err; 388 data->temp_min[i] = (s8)val; 389 err = regmap_read(data->regmap, ADT7470_TEMP_MAX_REG(i), &val); 390 if (err < 0) 391 return err; 392 data->temp_max[i] = (s8)val; 393 } 394 395 for (i = 0; i < ADT7470_FAN_COUNT; i++) { 396 err = adt7470_read_word_data(data, ADT7470_REG_FAN_MIN(i), &val); 397 if (err < 0) 398 return err; 399 data->fan_min[i] = val; 400 err = adt7470_read_word_data(data, ADT7470_REG_FAN_MAX(i), &val); 401 if (err < 0) 402 return err; 403 data->fan_max[i] = val; 404 } 405 406 for (i = 0; i < ADT7470_PWM_COUNT; i++) { 407 err = regmap_read(data->regmap, ADT7470_REG_PWM_MAX(i), &val); 408 if (err < 0) 409 return err; 410 data->pwm_max[i] = val; 411 err = regmap_read(data->regmap, ADT7470_REG_PWM_MIN(i), &val); 412 if (err < 0) 413 return err; 414 data->pwm_min[i] = val; 415 err = regmap_read(data->regmap, ADT7470_REG_PWM_TMIN(i), &val); 416 if (err < 0) 417 return err; 418 data->pwm_tmin[i] = (s8)val; 419 } 420 421 return 0; 422 } 423 424 static struct adt7470_data *adt7470_update_device(struct device *dev) 425 { 426 struct adt7470_data *data = dev_get_drvdata(dev); 427 unsigned long local_jiffies = jiffies; 428 int need_sensors = 1; 429 int need_limits = 1; 430 int err; 431 432 /* 433 * Figure out if we need to update the shadow registers. 434 * Lockless means that we may occasionally report out of 435 * date data. 436 */ 437 if (time_before(local_jiffies, data->sensors_last_updated + 438 SENSOR_REFRESH_INTERVAL) && 439 data->sensors_valid) 440 need_sensors = 0; 441 442 if (time_before(local_jiffies, data->limits_last_updated + 443 LIMIT_REFRESH_INTERVAL) && 444 data->limits_valid) 445 need_limits = 0; 446 447 if (!need_sensors && !need_limits) 448 return data; 449 450 mutex_lock(&data->lock); 451 if (need_sensors) { 452 err = adt7470_update_sensors(data); 453 if (err < 0) 454 goto out; 455 data->sensors_last_updated = local_jiffies; 456 data->sensors_valid = 1; 457 } 458 459 if (need_limits) { 460 err = adt7470_update_limits(data); 461 if (err < 0) 462 goto out; 463 data->limits_last_updated = local_jiffies; 464 data->limits_valid = 1; 465 } 466 out: 467 mutex_unlock(&data->lock); 468 469 return err < 0 ? ERR_PTR(err) : data; 470 } 471 472 static ssize_t auto_update_interval_show(struct device *dev, 473 struct device_attribute *devattr, 474 char *buf) 475 { 476 struct adt7470_data *data = adt7470_update_device(dev); 477 478 if (IS_ERR(data)) 479 return PTR_ERR(data); 480 481 return sprintf(buf, "%d\n", data->auto_update_interval); 482 } 483 484 static ssize_t auto_update_interval_store(struct device *dev, 485 struct device_attribute *devattr, 486 const char *buf, size_t count) 487 { 488 struct adt7470_data *data = dev_get_drvdata(dev); 489 long temp; 490 491 if (kstrtol(buf, 10, &temp)) 492 return -EINVAL; 493 494 temp = clamp_val(temp, 0, 60000); 495 496 mutex_lock(&data->lock); 497 data->auto_update_interval = temp; 498 mutex_unlock(&data->lock); 499 500 return count; 501 } 502 503 static ssize_t num_temp_sensors_show(struct device *dev, 504 struct device_attribute *devattr, 505 char *buf) 506 { 507 struct adt7470_data *data = adt7470_update_device(dev); 508 509 if (IS_ERR(data)) 510 return PTR_ERR(data); 511 512 return sprintf(buf, "%d\n", data->num_temp_sensors); 513 } 514 515 static ssize_t num_temp_sensors_store(struct device *dev, 516 struct device_attribute *devattr, 517 const char *buf, size_t count) 518 { 519 struct adt7470_data *data = dev_get_drvdata(dev); 520 long temp; 521 522 if (kstrtol(buf, 10, &temp)) 523 return -EINVAL; 524 525 temp = clamp_val(temp, -1, 10); 526 527 mutex_lock(&data->lock); 528 data->num_temp_sensors = temp; 529 if (temp < 0) 530 data->temperatures_probed = 0; 531 mutex_unlock(&data->lock); 532 533 return count; 534 } 535 536 static int adt7470_temp_read(struct device *dev, u32 attr, int channel, long *val) 537 { 538 struct adt7470_data *data = adt7470_update_device(dev); 539 540 if (IS_ERR(data)) 541 return PTR_ERR(data); 542 543 switch (attr) { 544 case hwmon_temp_input: 545 *val = 1000 * data->temp[channel]; 546 break; 547 case hwmon_temp_min: 548 *val = 1000 * data->temp_min[channel]; 549 break; 550 case hwmon_temp_max: 551 *val = 1000 * data->temp_max[channel]; 552 break; 553 case hwmon_temp_alarm: 554 *val = !!(data->alarm & channel); 555 break; 556 default: 557 return -EOPNOTSUPP; 558 } 559 560 return 0; 561 } 562 563 static int adt7470_temp_write(struct device *dev, u32 attr, int channel, long val) 564 { 565 struct adt7470_data *data = dev_get_drvdata(dev); 566 int err; 567 568 val = clamp_val(val, -128000, 127000); 569 val = DIV_ROUND_CLOSEST(val, 1000); 570 571 switch (attr) { 572 case hwmon_temp_min: 573 mutex_lock(&data->lock); 574 data->temp_min[channel] = val; 575 err = regmap_write(data->regmap, ADT7470_TEMP_MIN_REG(channel), val); 576 mutex_unlock(&data->lock); 577 break; 578 case hwmon_temp_max: 579 mutex_lock(&data->lock); 580 data->temp_max[channel] = val; 581 err = regmap_write(data->regmap, ADT7470_TEMP_MAX_REG(channel), val); 582 mutex_unlock(&data->lock); 583 break; 584 default: 585 return -EOPNOTSUPP; 586 } 587 588 return err; 589 } 590 591 static ssize_t alarm_mask_show(struct device *dev, 592 struct device_attribute *devattr, char *buf) 593 { 594 struct adt7470_data *data = adt7470_update_device(dev); 595 596 if (IS_ERR(data)) 597 return PTR_ERR(data); 598 599 return sprintf(buf, "%x\n", data->alarms_mask); 600 } 601 602 static ssize_t alarm_mask_store(struct device *dev, 603 struct device_attribute *devattr, 604 const char *buf, size_t count) 605 { 606 struct adt7470_data *data = dev_get_drvdata(dev); 607 long mask; 608 int err; 609 610 if (kstrtoul(buf, 0, &mask)) 611 return -EINVAL; 612 613 if (mask & ~0xffff) 614 return -EINVAL; 615 616 mutex_lock(&data->lock); 617 data->alarms_mask = mask; 618 err = adt7470_write_word_data(data, ADT7470_REG_ALARM1_MASK, mask); 619 mutex_unlock(&data->lock); 620 621 return err < 0 ? err : count; 622 } 623 624 static int adt7470_fan_read(struct device *dev, u32 attr, int channel, long *val) 625 { 626 struct adt7470_data *data = adt7470_update_device(dev); 627 628 if (IS_ERR(data)) 629 return PTR_ERR(data); 630 631 switch (attr) { 632 case hwmon_fan_input: 633 if (FAN_DATA_VALID(data->fan[channel])) 634 *val = FAN_PERIOD_TO_RPM(data->fan[channel]); 635 else 636 *val = 0; 637 break; 638 case hwmon_fan_min: 639 if (FAN_DATA_VALID(data->fan_min[channel])) 640 *val = FAN_PERIOD_TO_RPM(data->fan_min[channel]); 641 else 642 *val = 0; 643 break; 644 case hwmon_fan_max: 645 if (FAN_DATA_VALID(data->fan_max[channel])) 646 *val = FAN_PERIOD_TO_RPM(data->fan_max[channel]); 647 else 648 *val = 0; 649 break; 650 case hwmon_fan_alarm: 651 *val = !!(data->alarm & (1 << (12 + channel))); 652 break; 653 default: 654 return -EOPNOTSUPP; 655 } 656 657 return 0; 658 } 659 660 static int adt7470_fan_write(struct device *dev, u32 attr, int channel, long val) 661 { 662 struct adt7470_data *data = dev_get_drvdata(dev); 663 int err; 664 665 if (val <= 0) 666 return -EINVAL; 667 668 val = FAN_RPM_TO_PERIOD(val); 669 val = clamp_val(val, 1, 65534); 670 671 switch (attr) { 672 case hwmon_fan_min: 673 mutex_lock(&data->lock); 674 data->fan_min[channel] = val; 675 err = adt7470_write_word_data(data, ADT7470_REG_FAN_MIN(channel), val); 676 mutex_unlock(&data->lock); 677 break; 678 case hwmon_fan_max: 679 mutex_lock(&data->lock); 680 data->fan_max[channel] = val; 681 err = adt7470_write_word_data(data, ADT7470_REG_FAN_MAX(channel), val); 682 mutex_unlock(&data->lock); 683 break; 684 default: 685 return -EOPNOTSUPP; 686 } 687 688 return err; 689 } 690 691 static ssize_t force_pwm_max_show(struct device *dev, 692 struct device_attribute *devattr, char *buf) 693 { 694 struct adt7470_data *data = adt7470_update_device(dev); 695 696 if (IS_ERR(data)) 697 return PTR_ERR(data); 698 699 return sprintf(buf, "%d\n", data->force_pwm_max); 700 } 701 702 static ssize_t force_pwm_max_store(struct device *dev, 703 struct device_attribute *devattr, 704 const char *buf, size_t count) 705 { 706 struct adt7470_data *data = dev_get_drvdata(dev); 707 long temp; 708 int err; 709 710 if (kstrtol(buf, 10, &temp)) 711 return -EINVAL; 712 713 mutex_lock(&data->lock); 714 data->force_pwm_max = temp; 715 err = regmap_update_bits(data->regmap, ADT7470_REG_CFG, 716 ADT7470_FSPD_MASK, 717 temp ? ADT7470_FSPD_MASK : 0); 718 mutex_unlock(&data->lock); 719 720 return err < 0 ? err : count; 721 } 722 723 /* These are the valid PWM frequencies to the nearest Hz */ 724 static const int adt7470_freq_map[] = { 725 11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500 726 }; 727 728 static int pwm1_freq_get(struct device *dev) 729 { 730 struct adt7470_data *data = dev_get_drvdata(dev); 731 unsigned int cfg_reg_1, cfg_reg_2; 732 int index; 733 int err; 734 735 mutex_lock(&data->lock); 736 err = regmap_read(data->regmap, ADT7470_REG_CFG, &cfg_reg_1); 737 if (err < 0) 738 goto out; 739 err = regmap_read(data->regmap, ADT7470_REG_CFG_2, &cfg_reg_2); 740 if (err < 0) 741 goto out; 742 mutex_unlock(&data->lock); 743 744 index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT; 745 if (!(cfg_reg_1 & ADT7470_CFG_LF)) 746 index += 8; 747 if (index >= ARRAY_SIZE(adt7470_freq_map)) 748 index = ARRAY_SIZE(adt7470_freq_map) - 1; 749 750 return adt7470_freq_map[index]; 751 752 out: 753 mutex_unlock(&data->lock); 754 return err; 755 } 756 757 static int adt7470_pwm_read(struct device *dev, u32 attr, int channel, long *val) 758 { 759 struct adt7470_data *data = adt7470_update_device(dev); 760 761 if (IS_ERR(data)) 762 return PTR_ERR(data); 763 764 switch (attr) { 765 case hwmon_pwm_input: 766 *val = data->pwm[channel]; 767 break; 768 case hwmon_pwm_enable: 769 *val = 1 + data->pwm_automatic[channel]; 770 break; 771 case hwmon_pwm_freq: 772 *val = pwm1_freq_get(dev); 773 break; 774 default: 775 return -EOPNOTSUPP; 776 } 777 778 return 0; 779 } 780 781 static int pwm1_freq_set(struct device *dev, long freq) 782 { 783 struct adt7470_data *data = dev_get_drvdata(dev); 784 unsigned int low_freq = ADT7470_CFG_LF; 785 int index; 786 int err; 787 788 /* Round the user value given to the closest available frequency */ 789 index = find_closest(freq, adt7470_freq_map, 790 ARRAY_SIZE(adt7470_freq_map)); 791 792 if (index >= 8) { 793 index -= 8; 794 low_freq = 0; 795 } 796 797 mutex_lock(&data->lock); 798 /* Configuration Register 1 */ 799 err = regmap_update_bits(data->regmap, ADT7470_REG_CFG, 800 ADT7470_CFG_LF, low_freq); 801 if (err < 0) 802 goto out; 803 804 /* Configuration Register 2 */ 805 err = regmap_update_bits(data->regmap, ADT7470_REG_CFG_2, 806 ADT7470_FREQ_MASK, 807 index << ADT7470_FREQ_SHIFT); 808 out: 809 mutex_unlock(&data->lock); 810 811 return err; 812 } 813 814 static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val) 815 { 816 struct adt7470_data *data = dev_get_drvdata(dev); 817 unsigned int pwm_auto_reg_mask; 818 int err; 819 820 switch (attr) { 821 case hwmon_pwm_input: 822 val = clamp_val(val, 0, 255); 823 mutex_lock(&data->lock); 824 data->pwm[channel] = val; 825 err = regmap_write(data->regmap, ADT7470_REG_PWM(channel), 826 data->pwm[channel]); 827 mutex_unlock(&data->lock); 828 break; 829 case hwmon_pwm_enable: 830 if (channel % 2) 831 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK; 832 else 833 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK; 834 835 if (val != 2 && val != 1) 836 return -EINVAL; 837 val--; 838 839 mutex_lock(&data->lock); 840 data->pwm_automatic[channel] = val; 841 err = regmap_update_bits(data->regmap, ADT7470_REG_PWM_CFG(channel), 842 pwm_auto_reg_mask, 843 val ? pwm_auto_reg_mask : 0); 844 mutex_unlock(&data->lock); 845 break; 846 case hwmon_pwm_freq: 847 err = pwm1_freq_set(dev, val); 848 break; 849 default: 850 return -EOPNOTSUPP; 851 } 852 853 return err; 854 } 855 856 static ssize_t pwm_max_show(struct device *dev, 857 struct device_attribute *devattr, char *buf) 858 { 859 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 860 struct adt7470_data *data = adt7470_update_device(dev); 861 862 if (IS_ERR(data)) 863 return PTR_ERR(data); 864 865 return sprintf(buf, "%d\n", data->pwm_max[attr->index]); 866 } 867 868 static ssize_t pwm_max_store(struct device *dev, 869 struct device_attribute *devattr, 870 const char *buf, size_t count) 871 { 872 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 873 struct adt7470_data *data = dev_get_drvdata(dev); 874 long temp; 875 int err; 876 877 if (kstrtol(buf, 10, &temp)) 878 return -EINVAL; 879 880 temp = clamp_val(temp, 0, 255); 881 882 mutex_lock(&data->lock); 883 data->pwm_max[attr->index] = temp; 884 err = regmap_write(data->regmap, ADT7470_REG_PWM_MAX(attr->index), 885 temp); 886 mutex_unlock(&data->lock); 887 888 return err < 0 ? err : count; 889 } 890 891 static ssize_t pwm_min_show(struct device *dev, 892 struct device_attribute *devattr, char *buf) 893 { 894 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 895 struct adt7470_data *data = adt7470_update_device(dev); 896 897 if (IS_ERR(data)) 898 return PTR_ERR(data); 899 900 return sprintf(buf, "%d\n", data->pwm_min[attr->index]); 901 } 902 903 static ssize_t pwm_min_store(struct device *dev, 904 struct device_attribute *devattr, 905 const char *buf, size_t count) 906 { 907 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 908 struct adt7470_data *data = dev_get_drvdata(dev); 909 long temp; 910 int err; 911 912 if (kstrtol(buf, 10, &temp)) 913 return -EINVAL; 914 915 temp = clamp_val(temp, 0, 255); 916 917 mutex_lock(&data->lock); 918 data->pwm_min[attr->index] = temp; 919 err = regmap_write(data->regmap, ADT7470_REG_PWM_MIN(attr->index), 920 temp); 921 mutex_unlock(&data->lock); 922 923 return err < 0 ? err : count; 924 } 925 926 static ssize_t pwm_tmax_show(struct device *dev, 927 struct device_attribute *devattr, char *buf) 928 { 929 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 930 struct adt7470_data *data = adt7470_update_device(dev); 931 932 if (IS_ERR(data)) 933 return PTR_ERR(data); 934 935 /* the datasheet says that tmax = tmin + 20C */ 936 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index])); 937 } 938 939 static ssize_t pwm_tmin_show(struct device *dev, 940 struct device_attribute *devattr, char *buf) 941 { 942 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 943 struct adt7470_data *data = adt7470_update_device(dev); 944 945 if (IS_ERR(data)) 946 return PTR_ERR(data); 947 948 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]); 949 } 950 951 static ssize_t pwm_tmin_store(struct device *dev, 952 struct device_attribute *devattr, 953 const char *buf, size_t count) 954 { 955 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 956 struct adt7470_data *data = dev_get_drvdata(dev); 957 long temp; 958 int err; 959 960 if (kstrtol(buf, 10, &temp)) 961 return -EINVAL; 962 963 temp = clamp_val(temp, -128000, 127000); 964 temp = DIV_ROUND_CLOSEST(temp, 1000); 965 966 mutex_lock(&data->lock); 967 data->pwm_tmin[attr->index] = temp; 968 err = regmap_write(data->regmap, ADT7470_REG_PWM_TMIN(attr->index), 969 temp); 970 mutex_unlock(&data->lock); 971 972 return err < 0 ? err : count; 973 } 974 975 static ssize_t pwm_auto_temp_show(struct device *dev, 976 struct device_attribute *devattr, char *buf) 977 { 978 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 979 struct adt7470_data *data = adt7470_update_device(dev); 980 u8 ctrl; 981 982 if (IS_ERR(data)) 983 return PTR_ERR(data); 984 985 ctrl = data->pwm_auto_temp[attr->index]; 986 if (ctrl) 987 return sprintf(buf, "%d\n", 1 << (ctrl - 1)); 988 else 989 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS); 990 } 991 992 static int cvt_auto_temp(int input) 993 { 994 if (input == ADT7470_PWM_ALL_TEMPS) 995 return 0; 996 if (input < 1 || !is_power_of_2(input)) 997 return -EINVAL; 998 return ilog2(input) + 1; 999 } 1000 1001 static ssize_t pwm_auto_temp_store(struct device *dev, 1002 struct device_attribute *devattr, 1003 const char *buf, size_t count) 1004 { 1005 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 1006 struct adt7470_data *data = dev_get_drvdata(dev); 1007 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index); 1008 unsigned int mask, val; 1009 long temp; 1010 int err; 1011 1012 if (kstrtol(buf, 10, &temp)) 1013 return -EINVAL; 1014 1015 temp = cvt_auto_temp(temp); 1016 if (temp < 0) 1017 return temp; 1018 1019 mutex_lock(&data->lock); 1020 data->pwm_automatic[attr->index] = temp; 1021 1022 if (!(attr->index % 2)) { 1023 mask = 0xF0; 1024 val = (temp << 4) & 0xF0; 1025 } else { 1026 mask = 0x0F; 1027 val = temp & 0x0F; 1028 } 1029 1030 err = regmap_update_bits(data->regmap, pwm_auto_reg, mask, val); 1031 mutex_unlock(&data->lock); 1032 1033 return err < 0 ? err : count; 1034 } 1035 1036 static DEVICE_ATTR_RW(alarm_mask); 1037 static DEVICE_ATTR_RW(num_temp_sensors); 1038 static DEVICE_ATTR_RW(auto_update_interval); 1039 1040 static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0); 1041 1042 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0); 1043 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1); 1044 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2); 1045 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3); 1046 1047 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0); 1048 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1); 1049 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2); 1050 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3); 1051 1052 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0); 1053 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1); 1054 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2); 1055 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3); 1056 1057 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0); 1058 static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1); 1059 static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2); 1060 static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3); 1061 1062 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0); 1063 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1); 1064 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2); 1065 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3); 1066 1067 static struct attribute *adt7470_attrs[] = { 1068 &dev_attr_alarm_mask.attr, 1069 &dev_attr_num_temp_sensors.attr, 1070 &dev_attr_auto_update_interval.attr, 1071 &sensor_dev_attr_force_pwm_max.dev_attr.attr, 1072 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 1073 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 1074 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 1075 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr, 1076 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 1077 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, 1078 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, 1079 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr, 1080 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, 1081 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr, 1082 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr, 1083 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, 1084 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, 1085 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr, 1086 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr, 1087 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr, 1088 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, 1089 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, 1090 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, 1091 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr, 1092 NULL 1093 }; 1094 1095 ATTRIBUTE_GROUPS(adt7470); 1096 1097 static int adt7470_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, 1098 int channel, long *val) 1099 { 1100 switch (type) { 1101 case hwmon_temp: 1102 return adt7470_temp_read(dev, attr, channel, val); 1103 case hwmon_fan: 1104 return adt7470_fan_read(dev, attr, channel, val); 1105 case hwmon_pwm: 1106 return adt7470_pwm_read(dev, attr, channel, val); 1107 default: 1108 return -EOPNOTSUPP; 1109 } 1110 } 1111 1112 static int adt7470_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, 1113 int channel, long val) 1114 { 1115 switch (type) { 1116 case hwmon_temp: 1117 return adt7470_temp_write(dev, attr, channel, val); 1118 case hwmon_fan: 1119 return adt7470_fan_write(dev, attr, channel, val); 1120 case hwmon_pwm: 1121 return adt7470_pwm_write(dev, attr, channel, val); 1122 default: 1123 return -EOPNOTSUPP; 1124 } 1125 } 1126 1127 static umode_t adt7470_is_visible(const void *_data, enum hwmon_sensor_types type, 1128 u32 attr, int channel) 1129 { 1130 umode_t mode = 0; 1131 1132 switch (type) { 1133 case hwmon_temp: 1134 switch (attr) { 1135 case hwmon_temp: 1136 case hwmon_temp_alarm: 1137 mode = 0444; 1138 break; 1139 case hwmon_temp_min: 1140 case hwmon_temp_max: 1141 mode = 0644; 1142 break; 1143 default: 1144 break; 1145 } 1146 break; 1147 case hwmon_fan: 1148 switch (attr) { 1149 case hwmon_fan_input: 1150 case hwmon_fan_alarm: 1151 mode = 0444; 1152 break; 1153 case hwmon_fan_min: 1154 case hwmon_fan_max: 1155 mode = 0644; 1156 break; 1157 default: 1158 break; 1159 } 1160 break; 1161 case hwmon_pwm: 1162 switch (attr) { 1163 case hwmon_pwm_input: 1164 case hwmon_pwm_enable: 1165 mode = 0644; 1166 break; 1167 case hwmon_pwm_freq: 1168 if (channel == 0) 1169 mode = 0644; 1170 else 1171 mode = 0; 1172 break; 1173 default: 1174 break; 1175 } 1176 break; 1177 default: 1178 break; 1179 } 1180 1181 return mode; 1182 } 1183 1184 static const struct hwmon_ops adt7470_hwmon_ops = { 1185 .is_visible = adt7470_is_visible, 1186 .read = adt7470_read, 1187 .write = adt7470_write, 1188 }; 1189 1190 static const struct hwmon_channel_info *adt7470_info[] = { 1191 HWMON_CHANNEL_INFO(temp, 1192 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1193 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1194 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1195 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1196 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1197 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1198 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1199 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1200 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM, 1201 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM), 1202 HWMON_CHANNEL_INFO(fan, 1203 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX | HWMON_F_DIV | HWMON_F_ALARM, 1204 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX | HWMON_F_DIV | HWMON_F_ALARM, 1205 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX | HWMON_F_DIV | HWMON_F_ALARM, 1206 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX | HWMON_F_DIV | HWMON_F_ALARM), 1207 HWMON_CHANNEL_INFO(pwm, 1208 HWMON_PWM_INPUT | HWMON_PWM_ENABLE | HWMON_PWM_FREQ, 1209 HWMON_PWM_INPUT | HWMON_PWM_ENABLE, 1210 HWMON_PWM_INPUT | HWMON_PWM_ENABLE, 1211 HWMON_PWM_INPUT | HWMON_PWM_ENABLE), 1212 NULL 1213 }; 1214 1215 static const struct hwmon_chip_info adt7470_chip_info = { 1216 .ops = &adt7470_hwmon_ops, 1217 .info = adt7470_info, 1218 }; 1219 1220 /* Return 0 if detection is successful, -ENODEV otherwise */ 1221 static int adt7470_detect(struct i2c_client *client, 1222 struct i2c_board_info *info) 1223 { 1224 struct i2c_adapter *adapter = client->adapter; 1225 int vendor, device, revision; 1226 1227 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 1228 return -ENODEV; 1229 1230 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR); 1231 if (vendor != ADT7470_VENDOR) 1232 return -ENODEV; 1233 1234 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE); 1235 if (device != ADT7470_DEVICE) 1236 return -ENODEV; 1237 1238 revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION); 1239 if (revision != ADT7470_REVISION) 1240 return -ENODEV; 1241 1242 strscpy(info->type, "adt7470", I2C_NAME_SIZE); 1243 1244 return 0; 1245 } 1246 1247 static const struct regmap_config adt7470_regmap_config = { 1248 .reg_bits = 8, 1249 .val_bits = 8, 1250 .use_single_read = true, 1251 .use_single_write = true, 1252 }; 1253 1254 static int adt7470_probe(struct i2c_client *client) 1255 { 1256 struct device *dev = &client->dev; 1257 struct adt7470_data *data; 1258 struct device *hwmon_dev; 1259 int err; 1260 1261 data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL); 1262 if (!data) 1263 return -ENOMEM; 1264 1265 data->num_temp_sensors = -1; 1266 data->auto_update_interval = AUTO_UPDATE_INTERVAL; 1267 data->regmap = devm_regmap_init_i2c(client, &adt7470_regmap_config); 1268 if (IS_ERR(data->regmap)) 1269 return PTR_ERR(data->regmap); 1270 1271 i2c_set_clientdata(client, data); 1272 mutex_init(&data->lock); 1273 1274 dev_info(&client->dev, "%s chip found\n", client->name); 1275 1276 /* Initialize the ADT7470 chip */ 1277 err = regmap_update_bits(data->regmap, ADT7470_REG_CFG, 1278 ADT7470_STRT_MASK | ADT7470_TEST_MASK, 1279 ADT7470_STRT_MASK | ADT7470_TEST_MASK); 1280 if (err < 0) 1281 return err; 1282 1283 /* Register sysfs hooks */ 1284 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, 1285 &adt7470_chip_info, 1286 adt7470_groups); 1287 1288 if (IS_ERR(hwmon_dev)) 1289 return PTR_ERR(hwmon_dev); 1290 1291 data->auto_update = kthread_run(adt7470_update_thread, client, "%s", 1292 dev_name(hwmon_dev)); 1293 if (IS_ERR(data->auto_update)) 1294 return PTR_ERR(data->auto_update); 1295 1296 return 0; 1297 } 1298 1299 static int adt7470_remove(struct i2c_client *client) 1300 { 1301 struct adt7470_data *data = i2c_get_clientdata(client); 1302 1303 kthread_stop(data->auto_update); 1304 return 0; 1305 } 1306 1307 static const struct i2c_device_id adt7470_id[] = { 1308 { "adt7470", 0 }, 1309 { } 1310 }; 1311 MODULE_DEVICE_TABLE(i2c, adt7470_id); 1312 1313 static struct i2c_driver adt7470_driver = { 1314 .class = I2C_CLASS_HWMON, 1315 .driver = { 1316 .name = "adt7470", 1317 }, 1318 .probe_new = adt7470_probe, 1319 .remove = adt7470_remove, 1320 .id_table = adt7470_id, 1321 .detect = adt7470_detect, 1322 .address_list = normal_i2c, 1323 }; 1324 1325 module_i2c_driver(adt7470_driver); 1326 1327 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>"); 1328 MODULE_DESCRIPTION("ADT7470 driver"); 1329 MODULE_LICENSE("GPL"); 1330