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