1 /* 2 * adm1031.c - Part of lm_sensors, Linux kernel modules for hardware 3 * monitoring 4 * Based on lm75.c and lm85.c 5 * Supports adm1030 / adm1031 6 * Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org> 7 * Reworked by Jean Delvare <jdelvare@suse.de> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 */ 23 24 #include <linux/module.h> 25 #include <linux/init.h> 26 #include <linux/slab.h> 27 #include <linux/jiffies.h> 28 #include <linux/i2c.h> 29 #include <linux/hwmon.h> 30 #include <linux/hwmon-sysfs.h> 31 #include <linux/err.h> 32 #include <linux/mutex.h> 33 34 /* Following macros takes channel parameter starting from 0 to 2 */ 35 #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr)) 36 #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr)) 37 #define ADM1031_REG_PWM (0x22) 38 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr)) 39 #define ADM1031_REG_FAN_FILTER (0x23) 40 41 #define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr)) 42 #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr)) 43 #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr)) 44 #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr)) 45 46 #define ADM1031_REG_TEMP(nr) (0x0a + (nr)) 47 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr)) 48 49 #define ADM1031_REG_STATUS(nr) (0x2 + (nr)) 50 51 #define ADM1031_REG_CONF1 0x00 52 #define ADM1031_REG_CONF2 0x01 53 #define ADM1031_REG_EXT_TEMP 0x06 54 55 #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */ 56 #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */ 57 #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */ 58 59 #define ADM1031_CONF2_PWM1_ENABLE 0x01 60 #define ADM1031_CONF2_PWM2_ENABLE 0x02 61 #define ADM1031_CONF2_TACH1_ENABLE 0x04 62 #define ADM1031_CONF2_TACH2_ENABLE 0x08 63 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan)) 64 65 #define ADM1031_UPDATE_RATE_MASK 0x1c 66 #define ADM1031_UPDATE_RATE_SHIFT 2 67 68 /* Addresses to scan */ 69 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; 70 71 enum chips { adm1030, adm1031 }; 72 73 typedef u8 auto_chan_table_t[8][2]; 74 75 /* Each client has this additional data */ 76 struct adm1031_data { 77 struct i2c_client *client; 78 const struct attribute_group *groups[3]; 79 struct mutex update_lock; 80 int chip_type; 81 char valid; /* !=0 if following fields are valid */ 82 unsigned long last_updated; /* In jiffies */ 83 unsigned int update_interval; /* In milliseconds */ 84 /* 85 * The chan_select_table contains the possible configurations for 86 * auto fan control. 87 */ 88 const auto_chan_table_t *chan_select_table; 89 u16 alarm; 90 u8 conf1; 91 u8 conf2; 92 u8 fan[2]; 93 u8 fan_div[2]; 94 u8 fan_min[2]; 95 u8 pwm[2]; 96 u8 old_pwm[2]; 97 s8 temp[3]; 98 u8 ext_temp[3]; 99 u8 auto_temp[3]; 100 u8 auto_temp_min[3]; 101 u8 auto_temp_off[3]; 102 u8 auto_temp_max[3]; 103 s8 temp_offset[3]; 104 s8 temp_min[3]; 105 s8 temp_max[3]; 106 s8 temp_crit[3]; 107 }; 108 109 static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg) 110 { 111 return i2c_smbus_read_byte_data(client, reg); 112 } 113 114 static inline int 115 adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value) 116 { 117 return i2c_smbus_write_byte_data(client, reg, value); 118 } 119 120 static struct adm1031_data *adm1031_update_device(struct device *dev) 121 { 122 struct adm1031_data *data = dev_get_drvdata(dev); 123 struct i2c_client *client = data->client; 124 unsigned long next_update; 125 int chan; 126 127 mutex_lock(&data->update_lock); 128 129 next_update = data->last_updated 130 + msecs_to_jiffies(data->update_interval); 131 if (time_after(jiffies, next_update) || !data->valid) { 132 133 dev_dbg(&client->dev, "Starting adm1031 update\n"); 134 for (chan = 0; 135 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) { 136 u8 oldh, newh; 137 138 oldh = 139 adm1031_read_value(client, ADM1031_REG_TEMP(chan)); 140 data->ext_temp[chan] = 141 adm1031_read_value(client, ADM1031_REG_EXT_TEMP); 142 newh = 143 adm1031_read_value(client, ADM1031_REG_TEMP(chan)); 144 if (newh != oldh) { 145 data->ext_temp[chan] = 146 adm1031_read_value(client, 147 ADM1031_REG_EXT_TEMP); 148 #ifdef DEBUG 149 oldh = 150 adm1031_read_value(client, 151 ADM1031_REG_TEMP(chan)); 152 153 /* oldh is actually newer */ 154 if (newh != oldh) 155 dev_warn(&client->dev, 156 "Remote temperature may be wrong.\n"); 157 #endif 158 } 159 data->temp[chan] = newh; 160 161 data->temp_offset[chan] = 162 adm1031_read_value(client, 163 ADM1031_REG_TEMP_OFFSET(chan)); 164 data->temp_min[chan] = 165 adm1031_read_value(client, 166 ADM1031_REG_TEMP_MIN(chan)); 167 data->temp_max[chan] = 168 adm1031_read_value(client, 169 ADM1031_REG_TEMP_MAX(chan)); 170 data->temp_crit[chan] = 171 adm1031_read_value(client, 172 ADM1031_REG_TEMP_CRIT(chan)); 173 data->auto_temp[chan] = 174 adm1031_read_value(client, 175 ADM1031_REG_AUTO_TEMP(chan)); 176 177 } 178 179 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1); 180 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2); 181 182 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0)) 183 | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8); 184 if (data->chip_type == adm1030) 185 data->alarm &= 0xc0ff; 186 187 for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2); 188 chan++) { 189 data->fan_div[chan] = 190 adm1031_read_value(client, 191 ADM1031_REG_FAN_DIV(chan)); 192 data->fan_min[chan] = 193 adm1031_read_value(client, 194 ADM1031_REG_FAN_MIN(chan)); 195 data->fan[chan] = 196 adm1031_read_value(client, 197 ADM1031_REG_FAN_SPEED(chan)); 198 data->pwm[chan] = 199 (adm1031_read_value(client, 200 ADM1031_REG_PWM) >> (4 * chan)) & 0x0f; 201 } 202 data->last_updated = jiffies; 203 data->valid = 1; 204 } 205 206 mutex_unlock(&data->update_lock); 207 208 return data; 209 } 210 211 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \ 212 ((val + 500) / 1000))) 213 214 #define TEMP_FROM_REG(val) ((val) * 1000) 215 216 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125) 217 218 #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f) 219 #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \ 220 (val) | 0x70 : (val)) 221 222 #define FAN_FROM_REG(reg, div) ((reg) ? \ 223 (11250 * 60) / ((reg) * (div)) : 0) 224 225 static int FAN_TO_REG(int reg, int div) 226 { 227 int tmp; 228 tmp = FAN_FROM_REG(clamp_val(reg, 0, 65535), div); 229 return tmp > 255 ? 255 : tmp; 230 } 231 232 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6)) 233 234 #define PWM_TO_REG(val) (clamp_val((val), 0, 255) >> 4) 235 #define PWM_FROM_REG(val) ((val) << 4) 236 237 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7) 238 #define FAN_CHAN_TO_REG(val, reg) \ 239 (((reg) & 0x1F) | (((val) << 5) & 0xe0)) 240 241 #define AUTO_TEMP_MIN_TO_REG(val, reg) \ 242 ((((val) / 500) & 0xf8) | ((reg) & 0x7)) 243 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7))) 244 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2)) 245 246 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2) 247 248 #define AUTO_TEMP_OFF_FROM_REG(reg) \ 249 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000) 250 251 #define AUTO_TEMP_MAX_FROM_REG(reg) \ 252 (AUTO_TEMP_RANGE_FROM_REG(reg) + \ 253 AUTO_TEMP_MIN_FROM_REG(reg)) 254 255 static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm) 256 { 257 int ret; 258 int range = val - AUTO_TEMP_MIN_FROM_REG(reg); 259 260 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm); 261 ret = ((reg & 0xf8) | 262 (range < 10000 ? 0 : 263 range < 20000 ? 1 : 264 range < 40000 ? 2 : range < 80000 ? 3 : 4)); 265 return ret; 266 } 267 268 /* FAN auto control */ 269 #define GET_FAN_AUTO_BITFIELD(data, idx) \ 270 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2] 271 272 /* 273 * The tables below contains the possible values for the auto fan 274 * control bitfields. the index in the table is the register value. 275 * MSb is the auto fan control enable bit, so the four first entries 276 * in the table disables auto fan control when both bitfields are zero. 277 */ 278 static const auto_chan_table_t auto_channel_select_table_adm1031 = { 279 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 280 { 2 /* 0b010 */ , 4 /* 0b100 */ }, 281 { 2 /* 0b010 */ , 2 /* 0b010 */ }, 282 { 4 /* 0b100 */ , 4 /* 0b100 */ }, 283 { 7 /* 0b111 */ , 7 /* 0b111 */ }, 284 }; 285 286 static const auto_chan_table_t auto_channel_select_table_adm1030 = { 287 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 288 { 2 /* 0b10 */ , 0 }, 289 { 0xff /* invalid */ , 0 }, 290 { 0xff /* invalid */ , 0 }, 291 { 3 /* 0b11 */ , 0 }, 292 }; 293 294 /* 295 * That function checks if a bitfield is valid and returns the other bitfield 296 * nearest match if no exact match where found. 297 */ 298 static int 299 get_fan_auto_nearest(struct adm1031_data *data, int chan, u8 val, u8 reg) 300 { 301 int i; 302 int first_match = -1, exact_match = -1; 303 u8 other_reg_val = 304 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1]; 305 306 if (val == 0) 307 return 0; 308 309 for (i = 0; i < 8; i++) { 310 if ((val == (*data->chan_select_table)[i][chan]) && 311 ((*data->chan_select_table)[i][chan ? 0 : 1] == 312 other_reg_val)) { 313 /* We found an exact match */ 314 exact_match = i; 315 break; 316 } else if (val == (*data->chan_select_table)[i][chan] && 317 first_match == -1) { 318 /* 319 * Save the first match in case of an exact match has 320 * not been found 321 */ 322 first_match = i; 323 } 324 } 325 326 if (exact_match >= 0) 327 return exact_match; 328 else if (first_match >= 0) 329 return first_match; 330 331 return -EINVAL; 332 } 333 334 static ssize_t show_fan_auto_channel(struct device *dev, 335 struct device_attribute *attr, char *buf) 336 { 337 int nr = to_sensor_dev_attr(attr)->index; 338 struct adm1031_data *data = adm1031_update_device(dev); 339 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr)); 340 } 341 342 static ssize_t 343 set_fan_auto_channel(struct device *dev, struct device_attribute *attr, 344 const char *buf, size_t count) 345 { 346 struct adm1031_data *data = dev_get_drvdata(dev); 347 struct i2c_client *client = data->client; 348 int nr = to_sensor_dev_attr(attr)->index; 349 long val; 350 u8 reg; 351 int ret; 352 u8 old_fan_mode; 353 354 ret = kstrtol(buf, 10, &val); 355 if (ret) 356 return ret; 357 358 old_fan_mode = data->conf1; 359 360 mutex_lock(&data->update_lock); 361 362 ret = get_fan_auto_nearest(data, nr, val, data->conf1); 363 if (ret < 0) { 364 mutex_unlock(&data->update_lock); 365 return ret; 366 } 367 reg = ret; 368 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1); 369 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^ 370 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) { 371 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) { 372 /* 373 * Switch to Auto Fan Mode 374 * Save PWM registers 375 * Set PWM registers to 33% Both 376 */ 377 data->old_pwm[0] = data->pwm[0]; 378 data->old_pwm[1] = data->pwm[1]; 379 adm1031_write_value(client, ADM1031_REG_PWM, 0x55); 380 } else { 381 /* Switch to Manual Mode */ 382 data->pwm[0] = data->old_pwm[0]; 383 data->pwm[1] = data->old_pwm[1]; 384 /* Restore PWM registers */ 385 adm1031_write_value(client, ADM1031_REG_PWM, 386 data->pwm[0] | (data->pwm[1] << 4)); 387 } 388 } 389 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1); 390 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1); 391 mutex_unlock(&data->update_lock); 392 return count; 393 } 394 395 static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR, 396 show_fan_auto_channel, set_fan_auto_channel, 0); 397 static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR, 398 show_fan_auto_channel, set_fan_auto_channel, 1); 399 400 /* Auto Temps */ 401 static ssize_t show_auto_temp_off(struct device *dev, 402 struct device_attribute *attr, char *buf) 403 { 404 int nr = to_sensor_dev_attr(attr)->index; 405 struct adm1031_data *data = adm1031_update_device(dev); 406 return sprintf(buf, "%d\n", 407 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr])); 408 } 409 static ssize_t show_auto_temp_min(struct device *dev, 410 struct device_attribute *attr, char *buf) 411 { 412 int nr = to_sensor_dev_attr(attr)->index; 413 struct adm1031_data *data = adm1031_update_device(dev); 414 return sprintf(buf, "%d\n", 415 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr])); 416 } 417 static ssize_t 418 set_auto_temp_min(struct device *dev, struct device_attribute *attr, 419 const char *buf, size_t count) 420 { 421 struct adm1031_data *data = dev_get_drvdata(dev); 422 struct i2c_client *client = data->client; 423 int nr = to_sensor_dev_attr(attr)->index; 424 long val; 425 int ret; 426 427 ret = kstrtol(buf, 10, &val); 428 if (ret) 429 return ret; 430 431 val = clamp_val(val, 0, 127000); 432 mutex_lock(&data->update_lock); 433 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]); 434 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr), 435 data->auto_temp[nr]); 436 mutex_unlock(&data->update_lock); 437 return count; 438 } 439 static ssize_t show_auto_temp_max(struct device *dev, 440 struct device_attribute *attr, char *buf) 441 { 442 int nr = to_sensor_dev_attr(attr)->index; 443 struct adm1031_data *data = adm1031_update_device(dev); 444 return sprintf(buf, "%d\n", 445 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr])); 446 } 447 static ssize_t 448 set_auto_temp_max(struct device *dev, struct device_attribute *attr, 449 const char *buf, size_t count) 450 { 451 struct adm1031_data *data = dev_get_drvdata(dev); 452 struct i2c_client *client = data->client; 453 int nr = to_sensor_dev_attr(attr)->index; 454 long val; 455 int ret; 456 457 ret = kstrtol(buf, 10, &val); 458 if (ret) 459 return ret; 460 461 val = clamp_val(val, 0, 127000); 462 mutex_lock(&data->update_lock); 463 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], 464 data->pwm[nr]); 465 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr), 466 data->temp_max[nr]); 467 mutex_unlock(&data->update_lock); 468 return count; 469 } 470 471 #define auto_temp_reg(offset) \ 472 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \ 473 show_auto_temp_off, NULL, offset - 1); \ 474 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \ 475 show_auto_temp_min, set_auto_temp_min, offset - 1); \ 476 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \ 477 show_auto_temp_max, set_auto_temp_max, offset - 1) 478 479 auto_temp_reg(1); 480 auto_temp_reg(2); 481 auto_temp_reg(3); 482 483 /* pwm */ 484 static ssize_t show_pwm(struct device *dev, 485 struct device_attribute *attr, char *buf) 486 { 487 int nr = to_sensor_dev_attr(attr)->index; 488 struct adm1031_data *data = adm1031_update_device(dev); 489 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr])); 490 } 491 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, 492 const char *buf, size_t count) 493 { 494 struct adm1031_data *data = dev_get_drvdata(dev); 495 struct i2c_client *client = data->client; 496 int nr = to_sensor_dev_attr(attr)->index; 497 long val; 498 int ret, reg; 499 500 ret = kstrtol(buf, 10, &val); 501 if (ret) 502 return ret; 503 504 mutex_lock(&data->update_lock); 505 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) && 506 (((val>>4) & 0xf) != 5)) { 507 /* In automatic mode, the only PWM accepted is 33% */ 508 mutex_unlock(&data->update_lock); 509 return -EINVAL; 510 } 511 data->pwm[nr] = PWM_TO_REG(val); 512 reg = adm1031_read_value(client, ADM1031_REG_PWM); 513 adm1031_write_value(client, ADM1031_REG_PWM, 514 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf) 515 : (data->pwm[nr] & 0xf) | (reg & 0xf0)); 516 mutex_unlock(&data->update_lock); 517 return count; 518 } 519 520 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0); 521 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1); 522 static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR, 523 show_pwm, set_pwm, 0); 524 static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR, 525 show_pwm, set_pwm, 1); 526 527 /* Fans */ 528 529 /* 530 * That function checks the cases where the fan reading is not 531 * relevant. It is used to provide 0 as fan reading when the fan is 532 * not supposed to run 533 */ 534 static int trust_fan_readings(struct adm1031_data *data, int chan) 535 { 536 int res = 0; 537 538 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) { 539 switch (data->conf1 & 0x60) { 540 case 0x00: 541 /* 542 * remote temp1 controls fan1, 543 * remote temp2 controls fan2 544 */ 545 res = data->temp[chan+1] >= 546 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]); 547 break; 548 case 0x20: /* remote temp1 controls both fans */ 549 res = 550 data->temp[1] >= 551 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]); 552 break; 553 case 0x40: /* remote temp2 controls both fans */ 554 res = 555 data->temp[2] >= 556 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]); 557 break; 558 case 0x60: /* max controls both fans */ 559 res = 560 data->temp[0] >= 561 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0]) 562 || data->temp[1] >= 563 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]) 564 || (data->chip_type == adm1031 565 && data->temp[2] >= 566 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2])); 567 break; 568 } 569 } else { 570 res = data->pwm[chan] > 0; 571 } 572 return res; 573 } 574 575 576 static ssize_t show_fan(struct device *dev, 577 struct device_attribute *attr, char *buf) 578 { 579 int nr = to_sensor_dev_attr(attr)->index; 580 struct adm1031_data *data = adm1031_update_device(dev); 581 int value; 582 583 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr], 584 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0; 585 return sprintf(buf, "%d\n", value); 586 } 587 588 static ssize_t show_fan_div(struct device *dev, 589 struct device_attribute *attr, char *buf) 590 { 591 int nr = to_sensor_dev_attr(attr)->index; 592 struct adm1031_data *data = adm1031_update_device(dev); 593 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr])); 594 } 595 static ssize_t show_fan_min(struct device *dev, 596 struct device_attribute *attr, char *buf) 597 { 598 int nr = to_sensor_dev_attr(attr)->index; 599 struct adm1031_data *data = adm1031_update_device(dev); 600 return sprintf(buf, "%d\n", 601 FAN_FROM_REG(data->fan_min[nr], 602 FAN_DIV_FROM_REG(data->fan_div[nr]))); 603 } 604 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, 605 const char *buf, size_t count) 606 { 607 struct adm1031_data *data = dev_get_drvdata(dev); 608 struct i2c_client *client = data->client; 609 int nr = to_sensor_dev_attr(attr)->index; 610 long val; 611 int ret; 612 613 ret = kstrtol(buf, 10, &val); 614 if (ret) 615 return ret; 616 617 mutex_lock(&data->update_lock); 618 if (val) { 619 data->fan_min[nr] = 620 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr])); 621 } else { 622 data->fan_min[nr] = 0xff; 623 } 624 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]); 625 mutex_unlock(&data->update_lock); 626 return count; 627 } 628 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, 629 const char *buf, size_t count) 630 { 631 struct adm1031_data *data = dev_get_drvdata(dev); 632 struct i2c_client *client = data->client; 633 int nr = to_sensor_dev_attr(attr)->index; 634 long val; 635 u8 tmp; 636 int old_div; 637 int new_min; 638 int ret; 639 640 ret = kstrtol(buf, 10, &val); 641 if (ret) 642 return ret; 643 644 tmp = val == 8 ? 0xc0 : 645 val == 4 ? 0x80 : 646 val == 2 ? 0x40 : 647 val == 1 ? 0x00 : 648 0xff; 649 if (tmp == 0xff) 650 return -EINVAL; 651 652 mutex_lock(&data->update_lock); 653 /* Get fresh readings */ 654 data->fan_div[nr] = adm1031_read_value(client, 655 ADM1031_REG_FAN_DIV(nr)); 656 data->fan_min[nr] = adm1031_read_value(client, 657 ADM1031_REG_FAN_MIN(nr)); 658 659 /* Write the new clock divider and fan min */ 660 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]); 661 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]); 662 new_min = data->fan_min[nr] * old_div / val; 663 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min; 664 665 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr), 666 data->fan_div[nr]); 667 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), 668 data->fan_min[nr]); 669 670 /* Invalidate the cache: fan speed is no longer valid */ 671 data->valid = 0; 672 mutex_unlock(&data->update_lock); 673 return count; 674 } 675 676 #define fan_offset(offset) \ 677 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ 678 show_fan, NULL, offset - 1); \ 679 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ 680 show_fan_min, set_fan_min, offset - 1); \ 681 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ 682 show_fan_div, set_fan_div, offset - 1) 683 684 fan_offset(1); 685 fan_offset(2); 686 687 688 /* Temps */ 689 static ssize_t show_temp(struct device *dev, 690 struct device_attribute *attr, char *buf) 691 { 692 int nr = to_sensor_dev_attr(attr)->index; 693 struct adm1031_data *data = adm1031_update_device(dev); 694 int ext; 695 ext = nr == 0 ? 696 ((data->ext_temp[nr] >> 6) & 0x3) * 2 : 697 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7)); 698 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext)); 699 } 700 static ssize_t show_temp_offset(struct device *dev, 701 struct device_attribute *attr, char *buf) 702 { 703 int nr = to_sensor_dev_attr(attr)->index; 704 struct adm1031_data *data = adm1031_update_device(dev); 705 return sprintf(buf, "%d\n", 706 TEMP_OFFSET_FROM_REG(data->temp_offset[nr])); 707 } 708 static ssize_t show_temp_min(struct device *dev, 709 struct device_attribute *attr, char *buf) 710 { 711 int nr = to_sensor_dev_attr(attr)->index; 712 struct adm1031_data *data = adm1031_update_device(dev); 713 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); 714 } 715 static ssize_t show_temp_max(struct device *dev, 716 struct device_attribute *attr, char *buf) 717 { 718 int nr = to_sensor_dev_attr(attr)->index; 719 struct adm1031_data *data = adm1031_update_device(dev); 720 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); 721 } 722 static ssize_t show_temp_crit(struct device *dev, 723 struct device_attribute *attr, char *buf) 724 { 725 int nr = to_sensor_dev_attr(attr)->index; 726 struct adm1031_data *data = adm1031_update_device(dev); 727 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr])); 728 } 729 static ssize_t set_temp_offset(struct device *dev, 730 struct device_attribute *attr, const char *buf, 731 size_t count) 732 { 733 struct adm1031_data *data = dev_get_drvdata(dev); 734 struct i2c_client *client = data->client; 735 int nr = to_sensor_dev_attr(attr)->index; 736 long val; 737 int ret; 738 739 ret = kstrtol(buf, 10, &val); 740 if (ret) 741 return ret; 742 743 val = clamp_val(val, -15000, 15000); 744 mutex_lock(&data->update_lock); 745 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val); 746 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr), 747 data->temp_offset[nr]); 748 mutex_unlock(&data->update_lock); 749 return count; 750 } 751 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, 752 const char *buf, size_t count) 753 { 754 struct adm1031_data *data = dev_get_drvdata(dev); 755 struct i2c_client *client = data->client; 756 int nr = to_sensor_dev_attr(attr)->index; 757 long val; 758 int ret; 759 760 ret = kstrtol(buf, 10, &val); 761 if (ret) 762 return ret; 763 764 val = clamp_val(val, -55000, 127000); 765 mutex_lock(&data->update_lock); 766 data->temp_min[nr] = TEMP_TO_REG(val); 767 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr), 768 data->temp_min[nr]); 769 mutex_unlock(&data->update_lock); 770 return count; 771 } 772 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, 773 const char *buf, size_t count) 774 { 775 struct adm1031_data *data = dev_get_drvdata(dev); 776 struct i2c_client *client = data->client; 777 int nr = to_sensor_dev_attr(attr)->index; 778 long val; 779 int ret; 780 781 ret = kstrtol(buf, 10, &val); 782 if (ret) 783 return ret; 784 785 val = clamp_val(val, -55000, 127000); 786 mutex_lock(&data->update_lock); 787 data->temp_max[nr] = TEMP_TO_REG(val); 788 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr), 789 data->temp_max[nr]); 790 mutex_unlock(&data->update_lock); 791 return count; 792 } 793 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, 794 const char *buf, size_t count) 795 { 796 struct adm1031_data *data = dev_get_drvdata(dev); 797 struct i2c_client *client = data->client; 798 int nr = to_sensor_dev_attr(attr)->index; 799 long val; 800 int ret; 801 802 ret = kstrtol(buf, 10, &val); 803 if (ret) 804 return ret; 805 806 val = clamp_val(val, -55000, 127000); 807 mutex_lock(&data->update_lock); 808 data->temp_crit[nr] = TEMP_TO_REG(val); 809 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr), 810 data->temp_crit[nr]); 811 mutex_unlock(&data->update_lock); 812 return count; 813 } 814 815 #define temp_reg(offset) \ 816 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ 817 show_temp, NULL, offset - 1); \ 818 static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \ 819 show_temp_offset, set_temp_offset, offset - 1); \ 820 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ 821 show_temp_min, set_temp_min, offset - 1); \ 822 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ 823 show_temp_max, set_temp_max, offset - 1); \ 824 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \ 825 show_temp_crit, set_temp_crit, offset - 1) 826 827 temp_reg(1); 828 temp_reg(2); 829 temp_reg(3); 830 831 /* Alarms */ 832 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr, 833 char *buf) 834 { 835 struct adm1031_data *data = adm1031_update_device(dev); 836 return sprintf(buf, "%d\n", data->alarm); 837 } 838 839 static DEVICE_ATTR_RO(alarms); 840 841 static ssize_t show_alarm(struct device *dev, 842 struct device_attribute *attr, char *buf) 843 { 844 int bitnr = to_sensor_dev_attr(attr)->index; 845 struct adm1031_data *data = adm1031_update_device(dev); 846 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1); 847 } 848 849 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0); 850 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1); 851 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2); 852 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); 853 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4); 854 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5); 855 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); 856 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7); 857 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8); 858 static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9); 859 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10); 860 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11); 861 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12); 862 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13); 863 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14); 864 865 /* Update Interval */ 866 static const unsigned int update_intervals[] = { 867 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 868 }; 869 870 static ssize_t update_interval_show(struct device *dev, 871 struct device_attribute *attr, char *buf) 872 { 873 struct adm1031_data *data = dev_get_drvdata(dev); 874 875 return sprintf(buf, "%u\n", data->update_interval); 876 } 877 878 static ssize_t update_interval_store(struct device *dev, 879 struct device_attribute *attr, 880 const char *buf, size_t count) 881 { 882 struct adm1031_data *data = dev_get_drvdata(dev); 883 struct i2c_client *client = data->client; 884 unsigned long val; 885 int i, err; 886 u8 reg; 887 888 err = kstrtoul(buf, 10, &val); 889 if (err) 890 return err; 891 892 /* 893 * Find the nearest update interval from the table. 894 * Use it to determine the matching update rate. 895 */ 896 for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) { 897 if (val >= update_intervals[i]) 898 break; 899 } 900 /* if not found, we point to the last entry (lowest update interval) */ 901 902 /* set the new update rate while preserving other settings */ 903 reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER); 904 reg &= ~ADM1031_UPDATE_RATE_MASK; 905 reg |= i << ADM1031_UPDATE_RATE_SHIFT; 906 adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg); 907 908 mutex_lock(&data->update_lock); 909 data->update_interval = update_intervals[i]; 910 mutex_unlock(&data->update_lock); 911 912 return count; 913 } 914 915 static DEVICE_ATTR_RW(update_interval); 916 917 static struct attribute *adm1031_attributes[] = { 918 &sensor_dev_attr_fan1_input.dev_attr.attr, 919 &sensor_dev_attr_fan1_div.dev_attr.attr, 920 &sensor_dev_attr_fan1_min.dev_attr.attr, 921 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 922 &sensor_dev_attr_fan1_fault.dev_attr.attr, 923 &sensor_dev_attr_pwm1.dev_attr.attr, 924 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr, 925 &sensor_dev_attr_temp1_input.dev_attr.attr, 926 &sensor_dev_attr_temp1_offset.dev_attr.attr, 927 &sensor_dev_attr_temp1_min.dev_attr.attr, 928 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 929 &sensor_dev_attr_temp1_max.dev_attr.attr, 930 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 931 &sensor_dev_attr_temp1_crit.dev_attr.attr, 932 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 933 &sensor_dev_attr_temp2_input.dev_attr.attr, 934 &sensor_dev_attr_temp2_offset.dev_attr.attr, 935 &sensor_dev_attr_temp2_min.dev_attr.attr, 936 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, 937 &sensor_dev_attr_temp2_max.dev_attr.attr, 938 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 939 &sensor_dev_attr_temp2_crit.dev_attr.attr, 940 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 941 &sensor_dev_attr_temp2_fault.dev_attr.attr, 942 943 &sensor_dev_attr_auto_temp1_off.dev_attr.attr, 944 &sensor_dev_attr_auto_temp1_min.dev_attr.attr, 945 &sensor_dev_attr_auto_temp1_max.dev_attr.attr, 946 947 &sensor_dev_attr_auto_temp2_off.dev_attr.attr, 948 &sensor_dev_attr_auto_temp2_min.dev_attr.attr, 949 &sensor_dev_attr_auto_temp2_max.dev_attr.attr, 950 951 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr, 952 953 &dev_attr_update_interval.attr, 954 &dev_attr_alarms.attr, 955 956 NULL 957 }; 958 959 static const struct attribute_group adm1031_group = { 960 .attrs = adm1031_attributes, 961 }; 962 963 static struct attribute *adm1031_attributes_opt[] = { 964 &sensor_dev_attr_fan2_input.dev_attr.attr, 965 &sensor_dev_attr_fan2_div.dev_attr.attr, 966 &sensor_dev_attr_fan2_min.dev_attr.attr, 967 &sensor_dev_attr_fan2_alarm.dev_attr.attr, 968 &sensor_dev_attr_fan2_fault.dev_attr.attr, 969 &sensor_dev_attr_pwm2.dev_attr.attr, 970 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr, 971 &sensor_dev_attr_temp3_input.dev_attr.attr, 972 &sensor_dev_attr_temp3_offset.dev_attr.attr, 973 &sensor_dev_attr_temp3_min.dev_attr.attr, 974 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, 975 &sensor_dev_attr_temp3_max.dev_attr.attr, 976 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, 977 &sensor_dev_attr_temp3_crit.dev_attr.attr, 978 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, 979 &sensor_dev_attr_temp3_fault.dev_attr.attr, 980 &sensor_dev_attr_auto_temp3_off.dev_attr.attr, 981 &sensor_dev_attr_auto_temp3_min.dev_attr.attr, 982 &sensor_dev_attr_auto_temp3_max.dev_attr.attr, 983 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr, 984 NULL 985 }; 986 987 static const struct attribute_group adm1031_group_opt = { 988 .attrs = adm1031_attributes_opt, 989 }; 990 991 /* Return 0 if detection is successful, -ENODEV otherwise */ 992 static int adm1031_detect(struct i2c_client *client, 993 struct i2c_board_info *info) 994 { 995 struct i2c_adapter *adapter = client->adapter; 996 const char *name; 997 int id, co; 998 999 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 1000 return -ENODEV; 1001 1002 id = i2c_smbus_read_byte_data(client, 0x3d); 1003 co = i2c_smbus_read_byte_data(client, 0x3e); 1004 1005 if (!((id == 0x31 || id == 0x30) && co == 0x41)) 1006 return -ENODEV; 1007 name = (id == 0x30) ? "adm1030" : "adm1031"; 1008 1009 strlcpy(info->type, name, I2C_NAME_SIZE); 1010 1011 return 0; 1012 } 1013 1014 static void adm1031_init_client(struct i2c_client *client) 1015 { 1016 unsigned int read_val; 1017 unsigned int mask; 1018 int i; 1019 struct adm1031_data *data = i2c_get_clientdata(client); 1020 1021 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE); 1022 if (data->chip_type == adm1031) { 1023 mask |= (ADM1031_CONF2_PWM2_ENABLE | 1024 ADM1031_CONF2_TACH2_ENABLE); 1025 } 1026 /* Initialize the ADM1031 chip (enables fan speed reading ) */ 1027 read_val = adm1031_read_value(client, ADM1031_REG_CONF2); 1028 if ((read_val | mask) != read_val) 1029 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask); 1030 1031 read_val = adm1031_read_value(client, ADM1031_REG_CONF1); 1032 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) { 1033 adm1031_write_value(client, ADM1031_REG_CONF1, 1034 read_val | ADM1031_CONF1_MONITOR_ENABLE); 1035 } 1036 1037 /* Read the chip's update rate */ 1038 mask = ADM1031_UPDATE_RATE_MASK; 1039 read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER); 1040 i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT; 1041 /* Save it as update interval */ 1042 data->update_interval = update_intervals[i]; 1043 } 1044 1045 static int adm1031_probe(struct i2c_client *client, 1046 const struct i2c_device_id *id) 1047 { 1048 struct device *dev = &client->dev; 1049 struct device *hwmon_dev; 1050 struct adm1031_data *data; 1051 1052 data = devm_kzalloc(dev, sizeof(struct adm1031_data), GFP_KERNEL); 1053 if (!data) 1054 return -ENOMEM; 1055 1056 i2c_set_clientdata(client, data); 1057 data->client = client; 1058 data->chip_type = id->driver_data; 1059 mutex_init(&data->update_lock); 1060 1061 if (data->chip_type == adm1030) 1062 data->chan_select_table = &auto_channel_select_table_adm1030; 1063 else 1064 data->chan_select_table = &auto_channel_select_table_adm1031; 1065 1066 /* Initialize the ADM1031 chip */ 1067 adm1031_init_client(client); 1068 1069 /* sysfs hooks */ 1070 data->groups[0] = &adm1031_group; 1071 if (data->chip_type == adm1031) 1072 data->groups[1] = &adm1031_group_opt; 1073 1074 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 1075 data, data->groups); 1076 return PTR_ERR_OR_ZERO(hwmon_dev); 1077 } 1078 1079 static const struct i2c_device_id adm1031_id[] = { 1080 { "adm1030", adm1030 }, 1081 { "adm1031", adm1031 }, 1082 { } 1083 }; 1084 MODULE_DEVICE_TABLE(i2c, adm1031_id); 1085 1086 static struct i2c_driver adm1031_driver = { 1087 .class = I2C_CLASS_HWMON, 1088 .driver = { 1089 .name = "adm1031", 1090 }, 1091 .probe = adm1031_probe, 1092 .id_table = adm1031_id, 1093 .detect = adm1031_detect, 1094 .address_list = normal_i2c, 1095 }; 1096 1097 module_i2c_driver(adm1031_driver); 1098 1099 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>"); 1100 MODULE_DESCRIPTION("ADM1031/ADM1030 driver"); 1101 MODULE_LICENSE("GPL"); 1102