1 /* 2 * A hwmon driver for the Analog Devices ADT7470 3 * Copyright (C) 2007 IBM 4 * 5 * Author: Darrick J. Wong <djwong@us.ibm.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 #include <linux/module.h> 23 #include <linux/jiffies.h> 24 #include <linux/i2c.h> 25 #include <linux/hwmon.h> 26 #include <linux/hwmon-sysfs.h> 27 #include <linux/err.h> 28 #include <linux/mutex.h> 29 #include <linux/delay.h> 30 #include <linux/log2.h> 31 32 /* Addresses to scan */ 33 static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END }; 34 35 /* Insmod parameters */ 36 I2C_CLIENT_INSMOD_1(adt7470); 37 38 /* ADT7470 registers */ 39 #define ADT7470_REG_BASE_ADDR 0x20 40 #define ADT7470_REG_TEMP_BASE_ADDR 0x20 41 #define ADT7470_REG_TEMP_MAX_ADDR 0x29 42 #define ADT7470_REG_FAN_BASE_ADDR 0x2A 43 #define ADT7470_REG_FAN_MAX_ADDR 0x31 44 #define ADT7470_REG_PWM_BASE_ADDR 0x32 45 #define ADT7470_REG_PWM_MAX_ADDR 0x35 46 #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38 47 #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B 48 #define ADT7470_REG_CFG 0x40 49 #define ADT7470_FSPD_MASK 0x04 50 #define ADT7470_REG_ALARM1 0x41 51 #define ADT7470_R1T_ALARM 0x01 52 #define ADT7470_R2T_ALARM 0x02 53 #define ADT7470_R3T_ALARM 0x04 54 #define ADT7470_R4T_ALARM 0x08 55 #define ADT7470_R5T_ALARM 0x10 56 #define ADT7470_R6T_ALARM 0x20 57 #define ADT7470_R7T_ALARM 0x40 58 #define ADT7470_OOL_ALARM 0x80 59 #define ADT7470_REG_ALARM2 0x42 60 #define ADT7470_R8T_ALARM 0x01 61 #define ADT7470_R9T_ALARM 0x02 62 #define ADT7470_R10T_ALARM 0x04 63 #define ADT7470_FAN1_ALARM 0x10 64 #define ADT7470_FAN2_ALARM 0x20 65 #define ADT7470_FAN3_ALARM 0x40 66 #define ADT7470_FAN4_ALARM 0x80 67 #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44 68 #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57 69 #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58 70 #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F 71 #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60 72 #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67 73 #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68 74 #define ADT7470_REG_PWM12_CFG 0x68 75 #define ADT7470_PWM2_AUTO_MASK 0x40 76 #define ADT7470_PWM1_AUTO_MASK 0x80 77 #define ADT7470_REG_PWM34_CFG 0x69 78 #define ADT7470_PWM3_AUTO_MASK 0x40 79 #define ADT7470_PWM4_AUTO_MASK 0x80 80 #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A 81 #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D 82 #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E 83 #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71 84 #define ADT7470_REG_ACOUSTICS12 0x75 85 #define ADT7470_REG_ACOUSTICS34 0x76 86 #define ADT7470_REG_DEVICE 0x3D 87 #define ADT7470_REG_VENDOR 0x3E 88 #define ADT7470_REG_REVISION 0x3F 89 #define ADT7470_REG_ALARM1_MASK 0x72 90 #define ADT7470_REG_ALARM2_MASK 0x73 91 #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C 92 #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D 93 #define ADT7470_REG_MAX_ADDR 0x81 94 95 #define ADT7470_TEMP_COUNT 10 96 #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x)) 97 #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2)) 98 #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \ 99 ((x) * 2) + 1) 100 101 #define ADT7470_FAN_COUNT 4 102 #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2)) 103 #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2)) 104 #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2)) 105 106 #define ADT7470_PWM_COUNT 4 107 #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x)) 108 #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x)) 109 #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x)) 110 #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x)) 111 #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2)) 112 #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \ 113 ((x) / 2)) 114 115 #define ALARM2(x) ((x) << 8) 116 117 #define ADT7470_VENDOR 0x41 118 #define ADT7470_DEVICE 0x70 119 /* datasheet only mentions a revision 2 */ 120 #define ADT7470_REVISION 0x02 121 122 /* "all temps" according to hwmon sysfs interface spec */ 123 #define ADT7470_PWM_ALL_TEMPS 0x3FF 124 125 /* How often do we reread sensors values? (In jiffies) */ 126 #define SENSOR_REFRESH_INTERVAL (5 * HZ) 127 128 /* How often do we reread sensor limit values? (In jiffies) */ 129 #define LIMIT_REFRESH_INTERVAL (60 * HZ) 130 131 /* sleep 1s while gathering temperature data */ 132 #define TEMP_COLLECTION_TIME 1000 133 134 /* datasheet says to divide this number by the fan reading to get fan rpm */ 135 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x)) 136 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM 137 #define FAN_PERIOD_INVALID 65535 138 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID) 139 140 struct adt7470_data { 141 struct i2c_client client; 142 struct device *hwmon_dev; 143 struct attribute_group attrs; 144 struct mutex lock; 145 char sensors_valid; 146 char limits_valid; 147 unsigned long sensors_last_updated; /* In jiffies */ 148 unsigned long limits_last_updated; /* In jiffies */ 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 167 static int adt7470_attach_adapter(struct i2c_adapter *adapter); 168 static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind); 169 static int adt7470_detach_client(struct i2c_client *client); 170 171 static struct i2c_driver adt7470_driver = { 172 .driver = { 173 .name = "adt7470", 174 }, 175 .attach_adapter = adt7470_attach_adapter, 176 .detach_client = adt7470_detach_client, 177 }; 178 179 /* 180 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says 181 * that the low byte must be read before the high byte. 182 */ 183 static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg) 184 { 185 u16 foo; 186 foo = i2c_smbus_read_byte_data(client, reg); 187 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8); 188 return foo; 189 } 190 191 static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg, 192 u16 value) 193 { 194 return i2c_smbus_write_byte_data(client, reg, value & 0xFF) 195 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8); 196 } 197 198 static void adt7470_init_client(struct i2c_client *client) 199 { 200 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); 201 202 if (reg < 0) { 203 dev_err(&client->dev, "cannot read configuration register\n"); 204 } else { 205 /* start monitoring (and do a self-test) */ 206 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3); 207 } 208 } 209 210 static struct adt7470_data *adt7470_update_device(struct device *dev) 211 { 212 struct i2c_client *client = to_i2c_client(dev); 213 struct adt7470_data *data = i2c_get_clientdata(client); 214 unsigned long local_jiffies = jiffies; 215 u8 cfg; 216 int i; 217 218 mutex_lock(&data->lock); 219 if (time_before(local_jiffies, data->sensors_last_updated + 220 SENSOR_REFRESH_INTERVAL) 221 && data->sensors_valid) 222 goto no_sensor_update; 223 224 /* start reading temperature sensors */ 225 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); 226 cfg |= 0x80; 227 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg); 228 229 /* 230 * Delay is 200ms * number of tmp05 sensors. Too bad 231 * there's no way to figure out how many are connected. 232 * For now, assume 1s will work. 233 */ 234 msleep(TEMP_COLLECTION_TIME); 235 236 /* done reading temperature sensors */ 237 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); 238 cfg &= ~0x80; 239 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg); 240 241 for (i = 0; i < ADT7470_TEMP_COUNT; i++) 242 data->temp[i] = i2c_smbus_read_byte_data(client, 243 ADT7470_TEMP_REG(i)); 244 245 for (i = 0; i < ADT7470_FAN_COUNT; i++) 246 data->fan[i] = adt7470_read_word_data(client, 247 ADT7470_REG_FAN(i)); 248 249 for (i = 0; i < ADT7470_PWM_COUNT; i++) { 250 int reg; 251 int reg_mask; 252 253 data->pwm[i] = i2c_smbus_read_byte_data(client, 254 ADT7470_REG_PWM(i)); 255 256 if (i % 2) 257 reg_mask = ADT7470_PWM2_AUTO_MASK; 258 else 259 reg_mask = ADT7470_PWM1_AUTO_MASK; 260 261 reg = ADT7470_REG_PWM_CFG(i); 262 if (i2c_smbus_read_byte_data(client, reg) & reg_mask) 263 data->pwm_automatic[i] = 1; 264 else 265 data->pwm_automatic[i] = 0; 266 267 reg = ADT7470_REG_PWM_AUTO_TEMP(i); 268 cfg = i2c_smbus_read_byte_data(client, reg); 269 if (!(i % 2)) 270 data->pwm_auto_temp[i] = cfg >> 4; 271 else 272 data->pwm_auto_temp[i] = cfg & 0xF; 273 } 274 275 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) & 276 ADT7470_FSPD_MASK) 277 data->force_pwm_max = 1; 278 else 279 data->force_pwm_max = 0; 280 281 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1); 282 if (data->alarm & ADT7470_OOL_ALARM) 283 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client, 284 ADT7470_REG_ALARM2)); 285 data->alarms_mask = adt7470_read_word_data(client, 286 ADT7470_REG_ALARM1_MASK); 287 288 data->sensors_last_updated = local_jiffies; 289 data->sensors_valid = 1; 290 291 no_sensor_update: 292 if (time_before(local_jiffies, data->limits_last_updated + 293 LIMIT_REFRESH_INTERVAL) 294 && data->limits_valid) 295 goto out; 296 297 for (i = 0; i < ADT7470_TEMP_COUNT; i++) { 298 data->temp_min[i] = i2c_smbus_read_byte_data(client, 299 ADT7470_TEMP_MIN_REG(i)); 300 data->temp_max[i] = i2c_smbus_read_byte_data(client, 301 ADT7470_TEMP_MAX_REG(i)); 302 } 303 304 for (i = 0; i < ADT7470_FAN_COUNT; i++) { 305 data->fan_min[i] = adt7470_read_word_data(client, 306 ADT7470_REG_FAN_MIN(i)); 307 data->fan_max[i] = adt7470_read_word_data(client, 308 ADT7470_REG_FAN_MAX(i)); 309 } 310 311 for (i = 0; i < ADT7470_PWM_COUNT; i++) { 312 data->pwm_max[i] = i2c_smbus_read_byte_data(client, 313 ADT7470_REG_PWM_MAX(i)); 314 data->pwm_min[i] = i2c_smbus_read_byte_data(client, 315 ADT7470_REG_PWM_MIN(i)); 316 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client, 317 ADT7470_REG_PWM_TMIN(i)); 318 } 319 320 data->limits_last_updated = local_jiffies; 321 data->limits_valid = 1; 322 323 out: 324 mutex_unlock(&data->lock); 325 return data; 326 } 327 328 static ssize_t show_temp_min(struct device *dev, 329 struct device_attribute *devattr, 330 char *buf) 331 { 332 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 333 struct adt7470_data *data = adt7470_update_device(dev); 334 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]); 335 } 336 337 static ssize_t set_temp_min(struct device *dev, 338 struct device_attribute *devattr, 339 const char *buf, 340 size_t count) 341 { 342 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 343 struct i2c_client *client = to_i2c_client(dev); 344 struct adt7470_data *data = i2c_get_clientdata(client); 345 int temp = simple_strtol(buf, NULL, 10) / 1000; 346 347 mutex_lock(&data->lock); 348 data->temp_min[attr->index] = temp; 349 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index), 350 temp); 351 mutex_unlock(&data->lock); 352 353 return count; 354 } 355 356 static ssize_t show_temp_max(struct device *dev, 357 struct device_attribute *devattr, 358 char *buf) 359 { 360 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 361 struct adt7470_data *data = adt7470_update_device(dev); 362 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]); 363 } 364 365 static ssize_t set_temp_max(struct device *dev, 366 struct device_attribute *devattr, 367 const char *buf, 368 size_t count) 369 { 370 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 371 struct i2c_client *client = to_i2c_client(dev); 372 struct adt7470_data *data = i2c_get_clientdata(client); 373 int temp = simple_strtol(buf, NULL, 10) / 1000; 374 375 mutex_lock(&data->lock); 376 data->temp_max[attr->index] = temp; 377 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index), 378 temp); 379 mutex_unlock(&data->lock); 380 381 return count; 382 } 383 384 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, 385 char *buf) 386 { 387 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 388 struct adt7470_data *data = adt7470_update_device(dev); 389 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]); 390 } 391 392 static ssize_t show_alarm_mask(struct device *dev, 393 struct device_attribute *devattr, 394 char *buf) 395 { 396 struct adt7470_data *data = adt7470_update_device(dev); 397 398 return sprintf(buf, "%x\n", data->alarms_mask); 399 } 400 401 static ssize_t show_fan_max(struct device *dev, 402 struct device_attribute *devattr, 403 char *buf) 404 { 405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 406 struct adt7470_data *data = adt7470_update_device(dev); 407 408 if (FAN_DATA_VALID(data->fan_max[attr->index])) 409 return sprintf(buf, "%d\n", 410 FAN_PERIOD_TO_RPM(data->fan_max[attr->index])); 411 else 412 return sprintf(buf, "0\n"); 413 } 414 415 static ssize_t set_fan_max(struct device *dev, 416 struct device_attribute *devattr, 417 const char *buf, size_t count) 418 { 419 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 420 struct i2c_client *client = to_i2c_client(dev); 421 struct adt7470_data *data = i2c_get_clientdata(client); 422 int temp = simple_strtol(buf, NULL, 10); 423 424 if (!temp) 425 return -EINVAL; 426 temp = FAN_RPM_TO_PERIOD(temp); 427 428 mutex_lock(&data->lock); 429 data->fan_max[attr->index] = temp; 430 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp); 431 mutex_unlock(&data->lock); 432 433 return count; 434 } 435 436 static ssize_t show_fan_min(struct device *dev, 437 struct device_attribute *devattr, 438 char *buf) 439 { 440 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 441 struct adt7470_data *data = adt7470_update_device(dev); 442 443 if (FAN_DATA_VALID(data->fan_min[attr->index])) 444 return sprintf(buf, "%d\n", 445 FAN_PERIOD_TO_RPM(data->fan_min[attr->index])); 446 else 447 return sprintf(buf, "0\n"); 448 } 449 450 static ssize_t set_fan_min(struct device *dev, 451 struct device_attribute *devattr, 452 const char *buf, size_t count) 453 { 454 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 455 struct i2c_client *client = to_i2c_client(dev); 456 struct adt7470_data *data = i2c_get_clientdata(client); 457 int temp = simple_strtol(buf, NULL, 10); 458 459 if (!temp) 460 return -EINVAL; 461 temp = FAN_RPM_TO_PERIOD(temp); 462 463 mutex_lock(&data->lock); 464 data->fan_min[attr->index] = temp; 465 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp); 466 mutex_unlock(&data->lock); 467 468 return count; 469 } 470 471 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, 472 char *buf) 473 { 474 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 475 struct adt7470_data *data = adt7470_update_device(dev); 476 477 if (FAN_DATA_VALID(data->fan[attr->index])) 478 return sprintf(buf, "%d\n", 479 FAN_PERIOD_TO_RPM(data->fan[attr->index])); 480 else 481 return sprintf(buf, "0\n"); 482 } 483 484 static ssize_t show_force_pwm_max(struct device *dev, 485 struct device_attribute *devattr, 486 char *buf) 487 { 488 struct adt7470_data *data = adt7470_update_device(dev); 489 return sprintf(buf, "%d\n", data->force_pwm_max); 490 } 491 492 static ssize_t set_force_pwm_max(struct device *dev, 493 struct device_attribute *devattr, 494 const char *buf, 495 size_t count) 496 { 497 struct i2c_client *client = to_i2c_client(dev); 498 struct adt7470_data *data = i2c_get_clientdata(client); 499 int temp = simple_strtol(buf, NULL, 10); 500 u8 reg; 501 502 mutex_lock(&data->lock); 503 data->force_pwm_max = temp; 504 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); 505 if (temp) 506 reg |= ADT7470_FSPD_MASK; 507 else 508 reg &= ~ADT7470_FSPD_MASK; 509 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg); 510 mutex_unlock(&data->lock); 511 512 return count; 513 } 514 515 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, 516 char *buf) 517 { 518 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 519 struct adt7470_data *data = adt7470_update_device(dev); 520 return sprintf(buf, "%d\n", data->pwm[attr->index]); 521 } 522 523 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, 524 const char *buf, size_t count) 525 { 526 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 527 struct i2c_client *client = to_i2c_client(dev); 528 struct adt7470_data *data = i2c_get_clientdata(client); 529 int temp = simple_strtol(buf, NULL, 10); 530 531 mutex_lock(&data->lock); 532 data->pwm[attr->index] = temp; 533 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp); 534 mutex_unlock(&data->lock); 535 536 return count; 537 } 538 539 static ssize_t show_pwm_max(struct device *dev, 540 struct device_attribute *devattr, 541 char *buf) 542 { 543 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 544 struct adt7470_data *data = adt7470_update_device(dev); 545 return sprintf(buf, "%d\n", data->pwm_max[attr->index]); 546 } 547 548 static ssize_t set_pwm_max(struct device *dev, 549 struct device_attribute *devattr, 550 const char *buf, 551 size_t count) 552 { 553 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 554 struct i2c_client *client = to_i2c_client(dev); 555 struct adt7470_data *data = i2c_get_clientdata(client); 556 int temp = simple_strtol(buf, NULL, 10); 557 558 mutex_lock(&data->lock); 559 data->pwm_max[attr->index] = temp; 560 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index), 561 temp); 562 mutex_unlock(&data->lock); 563 564 return count; 565 } 566 567 static ssize_t show_pwm_min(struct device *dev, 568 struct device_attribute *devattr, 569 char *buf) 570 { 571 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 572 struct adt7470_data *data = adt7470_update_device(dev); 573 return sprintf(buf, "%d\n", data->pwm_min[attr->index]); 574 } 575 576 static ssize_t set_pwm_min(struct device *dev, 577 struct device_attribute *devattr, 578 const char *buf, 579 size_t count) 580 { 581 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 582 struct i2c_client *client = to_i2c_client(dev); 583 struct adt7470_data *data = i2c_get_clientdata(client); 584 int temp = simple_strtol(buf, NULL, 10); 585 586 mutex_lock(&data->lock); 587 data->pwm_min[attr->index] = temp; 588 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index), 589 temp); 590 mutex_unlock(&data->lock); 591 592 return count; 593 } 594 595 static ssize_t show_pwm_tmax(struct device *dev, 596 struct device_attribute *devattr, 597 char *buf) 598 { 599 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 600 struct adt7470_data *data = adt7470_update_device(dev); 601 /* the datasheet says that tmax = tmin + 20C */ 602 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index])); 603 } 604 605 static ssize_t show_pwm_tmin(struct device *dev, 606 struct device_attribute *devattr, 607 char *buf) 608 { 609 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 610 struct adt7470_data *data = adt7470_update_device(dev); 611 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]); 612 } 613 614 static ssize_t set_pwm_tmin(struct device *dev, 615 struct device_attribute *devattr, 616 const char *buf, 617 size_t count) 618 { 619 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 620 struct i2c_client *client = to_i2c_client(dev); 621 struct adt7470_data *data = i2c_get_clientdata(client); 622 int temp = simple_strtol(buf, NULL, 10) / 1000; 623 624 mutex_lock(&data->lock); 625 data->pwm_tmin[attr->index] = temp; 626 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index), 627 temp); 628 mutex_unlock(&data->lock); 629 630 return count; 631 } 632 633 static ssize_t show_pwm_auto(struct device *dev, 634 struct device_attribute *devattr, 635 char *buf) 636 { 637 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 638 struct adt7470_data *data = adt7470_update_device(dev); 639 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]); 640 } 641 642 static ssize_t set_pwm_auto(struct device *dev, 643 struct device_attribute *devattr, 644 const char *buf, 645 size_t count) 646 { 647 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 648 struct i2c_client *client = to_i2c_client(dev); 649 struct adt7470_data *data = i2c_get_clientdata(client); 650 int temp = simple_strtol(buf, NULL, 10); 651 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index); 652 int pwm_auto_reg_mask; 653 u8 reg; 654 655 if (attr->index % 2) 656 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK; 657 else 658 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK; 659 660 if (temp != 2 && temp != 1) 661 return -EINVAL; 662 temp--; 663 664 mutex_lock(&data->lock); 665 data->pwm_automatic[attr->index] = temp; 666 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg); 667 if (temp) 668 reg |= pwm_auto_reg_mask; 669 else 670 reg &= ~pwm_auto_reg_mask; 671 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg); 672 mutex_unlock(&data->lock); 673 674 return count; 675 } 676 677 static ssize_t show_pwm_auto_temp(struct device *dev, 678 struct device_attribute *devattr, 679 char *buf) 680 { 681 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 682 struct adt7470_data *data = adt7470_update_device(dev); 683 u8 ctrl = data->pwm_auto_temp[attr->index]; 684 685 if (ctrl) 686 return sprintf(buf, "%d\n", 1 << (ctrl - 1)); 687 else 688 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS); 689 } 690 691 static int cvt_auto_temp(int input) 692 { 693 if (input == ADT7470_PWM_ALL_TEMPS) 694 return 0; 695 if (input < 1 || !is_power_of_2(input)) 696 return -EINVAL; 697 return ilog2(input) + 1; 698 } 699 700 static ssize_t set_pwm_auto_temp(struct device *dev, 701 struct device_attribute *devattr, 702 const char *buf, 703 size_t count) 704 { 705 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 706 struct i2c_client *client = to_i2c_client(dev); 707 struct adt7470_data *data = i2c_get_clientdata(client); 708 int temp = cvt_auto_temp(simple_strtol(buf, NULL, 10)); 709 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index); 710 u8 reg; 711 712 if (temp < 0) 713 return temp; 714 715 mutex_lock(&data->lock); 716 data->pwm_automatic[attr->index] = temp; 717 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg); 718 719 if (!(attr->index % 2)) { 720 reg &= 0xF; 721 reg |= (temp << 4) & 0xF0; 722 } else { 723 reg &= 0xF0; 724 reg |= temp & 0xF; 725 } 726 727 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg); 728 mutex_unlock(&data->lock); 729 730 return count; 731 } 732 733 static ssize_t show_alarm(struct device *dev, 734 struct device_attribute *devattr, 735 char *buf) 736 { 737 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 738 struct adt7470_data *data = adt7470_update_device(dev); 739 740 if (data->alarm & attr->index) 741 return sprintf(buf, "1\n"); 742 else 743 return sprintf(buf, "0\n"); 744 } 745 746 static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL); 747 748 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, 749 set_temp_max, 0); 750 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, 751 set_temp_max, 1); 752 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max, 753 set_temp_max, 2); 754 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max, 755 set_temp_max, 3); 756 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max, 757 set_temp_max, 4); 758 static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max, 759 set_temp_max, 5); 760 static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max, 761 set_temp_max, 6); 762 static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max, 763 set_temp_max, 7); 764 static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max, 765 set_temp_max, 8); 766 static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max, 767 set_temp_max, 9); 768 769 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min, 770 set_temp_min, 0); 771 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min, 772 set_temp_min, 1); 773 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min, 774 set_temp_min, 2); 775 static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min, 776 set_temp_min, 3); 777 static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min, 778 set_temp_min, 4); 779 static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min, 780 set_temp_min, 5); 781 static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min, 782 set_temp_min, 6); 783 static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min, 784 set_temp_min, 7); 785 static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min, 786 set_temp_min, 8); 787 static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min, 788 set_temp_min, 9); 789 790 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); 791 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); 792 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); 793 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); 794 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4); 795 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5); 796 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6); 797 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7); 798 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8); 799 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9); 800 801 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 802 ADT7470_R1T_ALARM); 803 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 804 ADT7470_R2T_ALARM); 805 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 806 ADT7470_R3T_ALARM); 807 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 808 ADT7470_R4T_ALARM); 809 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 810 ADT7470_R5T_ALARM); 811 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 812 ADT7470_R6T_ALARM); 813 static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL, 814 ADT7470_R7T_ALARM); 815 static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL, 816 ALARM2(ADT7470_R8T_ALARM)); 817 static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL, 818 ALARM2(ADT7470_R9T_ALARM)); 819 static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL, 820 ALARM2(ADT7470_R10T_ALARM)); 821 822 static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max, 823 set_fan_max, 0); 824 static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max, 825 set_fan_max, 1); 826 static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max, 827 set_fan_max, 2); 828 static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max, 829 set_fan_max, 3); 830 831 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, 832 set_fan_min, 0); 833 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, 834 set_fan_min, 1); 835 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, 836 set_fan_min, 2); 837 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min, 838 set_fan_min, 3); 839 840 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); 841 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); 842 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2); 843 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3); 844 845 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 846 ALARM2(ADT7470_FAN1_ALARM)); 847 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 848 ALARM2(ADT7470_FAN2_ALARM)); 849 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 850 ALARM2(ADT7470_FAN3_ALARM)); 851 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 852 ALARM2(ADT7470_FAN4_ALARM)); 853 854 static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO, 855 show_force_pwm_max, set_force_pwm_max, 0); 856 857 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0); 858 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1); 859 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2); 860 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3); 861 862 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO, 863 show_pwm_min, set_pwm_min, 0); 864 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO, 865 show_pwm_min, set_pwm_min, 1); 866 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO, 867 show_pwm_min, set_pwm_min, 2); 868 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO, 869 show_pwm_min, set_pwm_min, 3); 870 871 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO, 872 show_pwm_max, set_pwm_max, 0); 873 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO, 874 show_pwm_max, set_pwm_max, 1); 875 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO, 876 show_pwm_max, set_pwm_max, 2); 877 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO, 878 show_pwm_max, set_pwm_max, 3); 879 880 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO, 881 show_pwm_tmin, set_pwm_tmin, 0); 882 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO, 883 show_pwm_tmin, set_pwm_tmin, 1); 884 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO, 885 show_pwm_tmin, set_pwm_tmin, 2); 886 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO, 887 show_pwm_tmin, set_pwm_tmin, 3); 888 889 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax, 890 NULL, 0); 891 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax, 892 NULL, 1); 893 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax, 894 NULL, 2); 895 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax, 896 NULL, 3); 897 898 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto, 899 set_pwm_auto, 0); 900 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto, 901 set_pwm_auto, 1); 902 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto, 903 set_pwm_auto, 2); 904 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto, 905 set_pwm_auto, 3); 906 907 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO, 908 show_pwm_auto_temp, set_pwm_auto_temp, 0); 909 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO, 910 show_pwm_auto_temp, set_pwm_auto_temp, 1); 911 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO, 912 show_pwm_auto_temp, set_pwm_auto_temp, 2); 913 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO, 914 show_pwm_auto_temp, set_pwm_auto_temp, 3); 915 916 static struct attribute *adt7470_attr[] = 917 { 918 &dev_attr_alarm_mask.attr, 919 &sensor_dev_attr_temp1_max.dev_attr.attr, 920 &sensor_dev_attr_temp2_max.dev_attr.attr, 921 &sensor_dev_attr_temp3_max.dev_attr.attr, 922 &sensor_dev_attr_temp4_max.dev_attr.attr, 923 &sensor_dev_attr_temp5_max.dev_attr.attr, 924 &sensor_dev_attr_temp6_max.dev_attr.attr, 925 &sensor_dev_attr_temp7_max.dev_attr.attr, 926 &sensor_dev_attr_temp8_max.dev_attr.attr, 927 &sensor_dev_attr_temp9_max.dev_attr.attr, 928 &sensor_dev_attr_temp10_max.dev_attr.attr, 929 &sensor_dev_attr_temp1_min.dev_attr.attr, 930 &sensor_dev_attr_temp2_min.dev_attr.attr, 931 &sensor_dev_attr_temp3_min.dev_attr.attr, 932 &sensor_dev_attr_temp4_min.dev_attr.attr, 933 &sensor_dev_attr_temp5_min.dev_attr.attr, 934 &sensor_dev_attr_temp6_min.dev_attr.attr, 935 &sensor_dev_attr_temp7_min.dev_attr.attr, 936 &sensor_dev_attr_temp8_min.dev_attr.attr, 937 &sensor_dev_attr_temp9_min.dev_attr.attr, 938 &sensor_dev_attr_temp10_min.dev_attr.attr, 939 &sensor_dev_attr_temp1_input.dev_attr.attr, 940 &sensor_dev_attr_temp2_input.dev_attr.attr, 941 &sensor_dev_attr_temp3_input.dev_attr.attr, 942 &sensor_dev_attr_temp4_input.dev_attr.attr, 943 &sensor_dev_attr_temp5_input.dev_attr.attr, 944 &sensor_dev_attr_temp6_input.dev_attr.attr, 945 &sensor_dev_attr_temp7_input.dev_attr.attr, 946 &sensor_dev_attr_temp8_input.dev_attr.attr, 947 &sensor_dev_attr_temp9_input.dev_attr.attr, 948 &sensor_dev_attr_temp10_input.dev_attr.attr, 949 &sensor_dev_attr_temp1_alarm.dev_attr.attr, 950 &sensor_dev_attr_temp2_alarm.dev_attr.attr, 951 &sensor_dev_attr_temp3_alarm.dev_attr.attr, 952 &sensor_dev_attr_temp4_alarm.dev_attr.attr, 953 &sensor_dev_attr_temp5_alarm.dev_attr.attr, 954 &sensor_dev_attr_temp6_alarm.dev_attr.attr, 955 &sensor_dev_attr_temp7_alarm.dev_attr.attr, 956 &sensor_dev_attr_temp8_alarm.dev_attr.attr, 957 &sensor_dev_attr_temp9_alarm.dev_attr.attr, 958 &sensor_dev_attr_temp10_alarm.dev_attr.attr, 959 &sensor_dev_attr_fan1_max.dev_attr.attr, 960 &sensor_dev_attr_fan2_max.dev_attr.attr, 961 &sensor_dev_attr_fan3_max.dev_attr.attr, 962 &sensor_dev_attr_fan4_max.dev_attr.attr, 963 &sensor_dev_attr_fan1_min.dev_attr.attr, 964 &sensor_dev_attr_fan2_min.dev_attr.attr, 965 &sensor_dev_attr_fan3_min.dev_attr.attr, 966 &sensor_dev_attr_fan4_min.dev_attr.attr, 967 &sensor_dev_attr_fan1_input.dev_attr.attr, 968 &sensor_dev_attr_fan2_input.dev_attr.attr, 969 &sensor_dev_attr_fan3_input.dev_attr.attr, 970 &sensor_dev_attr_fan4_input.dev_attr.attr, 971 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 972 &sensor_dev_attr_fan2_alarm.dev_attr.attr, 973 &sensor_dev_attr_fan3_alarm.dev_attr.attr, 974 &sensor_dev_attr_fan4_alarm.dev_attr.attr, 975 &sensor_dev_attr_force_pwm_max.dev_attr.attr, 976 &sensor_dev_attr_pwm1.dev_attr.attr, 977 &sensor_dev_attr_pwm2.dev_attr.attr, 978 &sensor_dev_attr_pwm3.dev_attr.attr, 979 &sensor_dev_attr_pwm4.dev_attr.attr, 980 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 981 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 982 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 983 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr, 984 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 985 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, 986 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, 987 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr, 988 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, 989 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr, 990 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr, 991 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, 992 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, 993 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr, 994 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr, 995 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr, 996 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 997 &sensor_dev_attr_pwm2_enable.dev_attr.attr, 998 &sensor_dev_attr_pwm3_enable.dev_attr.attr, 999 &sensor_dev_attr_pwm4_enable.dev_attr.attr, 1000 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, 1001 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, 1002 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, 1003 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr, 1004 NULL 1005 }; 1006 1007 static int adt7470_attach_adapter(struct i2c_adapter *adapter) 1008 { 1009 if (!(adapter->class & I2C_CLASS_HWMON)) 1010 return 0; 1011 return i2c_probe(adapter, &addr_data, adt7470_detect); 1012 } 1013 1014 static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind) 1015 { 1016 struct i2c_client *client; 1017 struct adt7470_data *data; 1018 int err = 0; 1019 1020 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 1021 goto exit; 1022 1023 if (!(data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL))) { 1024 err = -ENOMEM; 1025 goto exit; 1026 } 1027 1028 client = &data->client; 1029 client->addr = address; 1030 client->adapter = adapter; 1031 client->driver = &adt7470_driver; 1032 1033 i2c_set_clientdata(client, data); 1034 1035 mutex_init(&data->lock); 1036 1037 if (kind <= 0) { 1038 int vendor, device, revision; 1039 1040 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR); 1041 if (vendor != ADT7470_VENDOR) { 1042 err = -ENODEV; 1043 goto exit_free; 1044 } 1045 1046 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE); 1047 if (device != ADT7470_DEVICE) { 1048 err = -ENODEV; 1049 goto exit_free; 1050 } 1051 1052 revision = i2c_smbus_read_byte_data(client, 1053 ADT7470_REG_REVISION); 1054 if (revision != ADT7470_REVISION) { 1055 err = -ENODEV; 1056 goto exit_free; 1057 } 1058 } else 1059 dev_dbg(&adapter->dev, "detection forced\n"); 1060 1061 strlcpy(client->name, "adt7470", I2C_NAME_SIZE); 1062 1063 if ((err = i2c_attach_client(client))) 1064 goto exit_free; 1065 1066 dev_info(&client->dev, "%s chip found\n", client->name); 1067 1068 /* Initialize the ADT7470 chip */ 1069 adt7470_init_client(client); 1070 1071 /* Register sysfs hooks */ 1072 data->attrs.attrs = adt7470_attr; 1073 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs))) 1074 goto exit_detach; 1075 1076 data->hwmon_dev = hwmon_device_register(&client->dev); 1077 if (IS_ERR(data->hwmon_dev)) { 1078 err = PTR_ERR(data->hwmon_dev); 1079 goto exit_remove; 1080 } 1081 1082 return 0; 1083 1084 exit_remove: 1085 sysfs_remove_group(&client->dev.kobj, &data->attrs); 1086 exit_detach: 1087 i2c_detach_client(client); 1088 exit_free: 1089 kfree(data); 1090 exit: 1091 return err; 1092 } 1093 1094 static int adt7470_detach_client(struct i2c_client *client) 1095 { 1096 struct adt7470_data *data = i2c_get_clientdata(client); 1097 1098 hwmon_device_unregister(data->hwmon_dev); 1099 sysfs_remove_group(&client->dev.kobj, &data->attrs); 1100 i2c_detach_client(client); 1101 kfree(data); 1102 return 0; 1103 } 1104 1105 static int __init adt7470_init(void) 1106 { 1107 return i2c_add_driver(&adt7470_driver); 1108 } 1109 1110 static void __exit adt7470_exit(void) 1111 { 1112 i2c_del_driver(&adt7470_driver); 1113 } 1114 1115 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>"); 1116 MODULE_DESCRIPTION("ADT7470 driver"); 1117 MODULE_LICENSE("GPL"); 1118 1119 module_init(adt7470_init); 1120 module_exit(adt7470_exit); 1121