1 /* 2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware 3 monitoring 4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> 5 Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com> 6 Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de> 7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com> 8 9 Chip details at <http://www.national.com/ds/LM/LM85.pdf> 10 11 This program is free software; you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation; either version 2 of the License, or 14 (at your option) any later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; if not, write to the Free Software 23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 */ 25 26 #include <linux/module.h> 27 #include <linux/init.h> 28 #include <linux/slab.h> 29 #include <linux/jiffies.h> 30 #include <linux/i2c.h> 31 #include <linux/i2c-sensor.h> 32 #include <linux/i2c-vid.h> 33 #include <linux/hwmon.h> 34 #include <linux/err.h> 35 36 /* Addresses to scan */ 37 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; 38 39 /* Insmod parameters */ 40 SENSORS_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102); 41 42 /* The LM85 registers */ 43 44 #define LM85_REG_IN(nr) (0x20 + (nr)) 45 #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2) 46 #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2) 47 48 #define LM85_REG_TEMP(nr) (0x25 + (nr)) 49 #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2) 50 #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2) 51 52 /* Fan speeds are LSB, MSB (2 bytes) */ 53 #define LM85_REG_FAN(nr) (0x28 + (nr) *2) 54 #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) *2) 55 56 #define LM85_REG_PWM(nr) (0x30 + (nr)) 57 58 #define ADT7463_REG_OPPOINT(nr) (0x33 + (nr)) 59 60 #define ADT7463_REG_TMIN_CTL1 0x36 61 #define ADT7463_REG_TMIN_CTL2 0x37 62 63 #define LM85_REG_DEVICE 0x3d 64 #define LM85_REG_COMPANY 0x3e 65 #define LM85_REG_VERSTEP 0x3f 66 /* These are the recognized values for the above regs */ 67 #define LM85_DEVICE_ADX 0x27 68 #define LM85_COMPANY_NATIONAL 0x01 69 #define LM85_COMPANY_ANALOG_DEV 0x41 70 #define LM85_COMPANY_SMSC 0x5c 71 #define LM85_VERSTEP_VMASK 0xf0 72 #define LM85_VERSTEP_GENERIC 0x60 73 #define LM85_VERSTEP_LM85C 0x60 74 #define LM85_VERSTEP_LM85B 0x62 75 #define LM85_VERSTEP_ADM1027 0x60 76 #define LM85_VERSTEP_ADT7463 0x62 77 #define LM85_VERSTEP_ADT7463C 0x6A 78 #define LM85_VERSTEP_EMC6D100_A0 0x60 79 #define LM85_VERSTEP_EMC6D100_A1 0x61 80 #define LM85_VERSTEP_EMC6D102 0x65 81 82 #define LM85_REG_CONFIG 0x40 83 84 #define LM85_REG_ALARM1 0x41 85 #define LM85_REG_ALARM2 0x42 86 87 #define LM85_REG_VID 0x43 88 89 /* Automated FAN control */ 90 #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr)) 91 #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr)) 92 #define LM85_REG_AFAN_SPIKE1 0x62 93 #define LM85_REG_AFAN_SPIKE2 0x63 94 #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr)) 95 #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr)) 96 #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr)) 97 #define LM85_REG_AFAN_HYST1 0x6d 98 #define LM85_REG_AFAN_HYST2 0x6e 99 100 #define LM85_REG_TACH_MODE 0x74 101 #define LM85_REG_SPINUP_CTL 0x75 102 103 #define ADM1027_REG_TEMP_OFFSET(nr) (0x70 + (nr)) 104 #define ADM1027_REG_CONFIG2 0x73 105 #define ADM1027_REG_INTMASK1 0x74 106 #define ADM1027_REG_INTMASK2 0x75 107 #define ADM1027_REG_EXTEND_ADC1 0x76 108 #define ADM1027_REG_EXTEND_ADC2 0x77 109 #define ADM1027_REG_CONFIG3 0x78 110 #define ADM1027_REG_FAN_PPR 0x7b 111 112 #define ADT7463_REG_THERM 0x79 113 #define ADT7463_REG_THERM_LIMIT 0x7A 114 115 #define EMC6D100_REG_ALARM3 0x7d 116 /* IN5, IN6 and IN7 */ 117 #define EMC6D100_REG_IN(nr) (0x70 + ((nr)-5)) 118 #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr)-5) * 2) 119 #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr)-5) * 2) 120 #define EMC6D102_REG_EXTEND_ADC1 0x85 121 #define EMC6D102_REG_EXTEND_ADC2 0x86 122 #define EMC6D102_REG_EXTEND_ADC3 0x87 123 #define EMC6D102_REG_EXTEND_ADC4 0x88 124 125 #define LM85_ALARM_IN0 0x0001 126 #define LM85_ALARM_IN1 0x0002 127 #define LM85_ALARM_IN2 0x0004 128 #define LM85_ALARM_IN3 0x0008 129 #define LM85_ALARM_TEMP1 0x0010 130 #define LM85_ALARM_TEMP2 0x0020 131 #define LM85_ALARM_TEMP3 0x0040 132 #define LM85_ALARM_ALARM2 0x0080 133 #define LM85_ALARM_IN4 0x0100 134 #define LM85_ALARM_RESERVED 0x0200 135 #define LM85_ALARM_FAN1 0x0400 136 #define LM85_ALARM_FAN2 0x0800 137 #define LM85_ALARM_FAN3 0x1000 138 #define LM85_ALARM_FAN4 0x2000 139 #define LM85_ALARM_TEMP1_FAULT 0x4000 140 #define LM85_ALARM_TEMP3_FAULT 0x8000 141 142 143 /* Conversions. Rounding and limit checking is only done on the TO_REG 144 variants. Note that you should be a bit careful with which arguments 145 these macros are called: arguments may be evaluated more than once. 146 */ 147 148 /* IN are scaled acording to built-in resistors */ 149 static int lm85_scaling[] = { /* .001 Volts */ 150 2500, 2250, 3300, 5000, 12000, 151 3300, 1500, 1800 /*EMC6D100*/ 152 }; 153 #define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from)) 154 155 #define INS_TO_REG(n,val) \ 156 SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255) 157 158 #define INSEXT_FROM_REG(n,val,ext,scale) \ 159 SCALE((val)*(scale) + (ext),192*(scale),lm85_scaling[n]) 160 161 #define INS_FROM_REG(n,val) INSEXT_FROM_REG(n,val,0,1) 162 163 /* FAN speed is measured using 90kHz clock */ 164 #define FAN_TO_REG(val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534)) 165 #define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val)) 166 167 /* Temperature is reported in .001 degC increments */ 168 #define TEMP_TO_REG(val) \ 169 SENSORS_LIMIT(SCALE(val,1000,1),-127,127) 170 #define TEMPEXT_FROM_REG(val,ext,scale) \ 171 SCALE((val)*scale + (ext),scale,1000) 172 #define TEMP_FROM_REG(val) \ 173 TEMPEXT_FROM_REG(val,0,1) 174 175 #define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255)) 176 #define PWM_FROM_REG(val) (val) 177 178 179 /* ZONEs have the following parameters: 180 * Limit (low) temp, 1. degC 181 * Hysteresis (below limit), 1. degC (0-15) 182 * Range of speed control, .1 degC (2-80) 183 * Critical (high) temp, 1. degC 184 * 185 * FAN PWMs have the following parameters: 186 * Reference Zone, 1, 2, 3, etc. 187 * Spinup time, .05 sec 188 * PWM value at limit/low temp, 1 count 189 * PWM Frequency, 1. Hz 190 * PWM is Min or OFF below limit, flag 191 * Invert PWM output, flag 192 * 193 * Some chips filter the temp, others the fan. 194 * Filter constant (or disabled) .1 seconds 195 */ 196 197 /* These are the zone temperature range encodings in .001 degree C */ 198 static int lm85_range_map[] = { 199 2000, 2500, 3300, 4000, 5000, 6600, 200 8000, 10000, 13300, 16000, 20000, 26600, 201 32000, 40000, 53300, 80000 202 }; 203 static int RANGE_TO_REG( int range ) 204 { 205 int i; 206 207 if ( range < lm85_range_map[0] ) { 208 return 0 ; 209 } else if ( range > lm85_range_map[15] ) { 210 return 15 ; 211 } else { /* find closest match */ 212 for ( i = 14 ; i >= 0 ; --i ) { 213 if ( range > lm85_range_map[i] ) { /* range bracketed */ 214 if ((lm85_range_map[i+1] - range) < 215 (range - lm85_range_map[i])) { 216 i++; 217 break; 218 } 219 break; 220 } 221 } 222 } 223 return( i & 0x0f ); 224 } 225 #define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f]) 226 227 /* These are the Acoustic Enhancement, or Temperature smoothing encodings 228 * NOTE: The enable/disable bit is INCLUDED in these encodings as the 229 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value 230 * is ignored, or set to 0. 231 */ 232 /* These are the PWM frequency encodings */ 233 static int lm85_freq_map[] = { /* .1 Hz */ 234 100, 150, 230, 300, 380, 470, 620, 940 235 }; 236 static int FREQ_TO_REG( int freq ) 237 { 238 int i; 239 240 if( freq >= lm85_freq_map[7] ) { return 7 ; } 241 for( i = 0 ; i < 7 ; ++i ) 242 if( freq <= lm85_freq_map[i] ) 243 break ; 244 return( i & 0x07 ); 245 } 246 #define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07]) 247 248 /* Since we can't use strings, I'm abusing these numbers 249 * to stand in for the following meanings: 250 * 1 -- PWM responds to Zone 1 251 * 2 -- PWM responds to Zone 2 252 * 3 -- PWM responds to Zone 3 253 * 23 -- PWM responds to the higher temp of Zone 2 or 3 254 * 123 -- PWM responds to highest of Zone 1, 2, or 3 255 * 0 -- PWM is always at 0% (ie, off) 256 * -1 -- PWM is always at 100% 257 * -2 -- PWM responds to manual control 258 */ 259 260 static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 }; 261 #define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07]) 262 263 static int ZONE_TO_REG( int zone ) 264 { 265 int i; 266 267 for( i = 0 ; i <= 7 ; ++i ) 268 if( zone == lm85_zone_map[i] ) 269 break ; 270 if( i > 7 ) /* Not found. */ 271 i = 3; /* Always 100% */ 272 return( (i & 0x07)<<5 ); 273 } 274 275 #define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15)) 276 #define HYST_FROM_REG(val) ((val)*1000) 277 278 #define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127)) 279 #define OFFSET_FROM_REG(val) ((val)*25) 280 281 #define PPR_MASK(fan) (0x03<<(fan *2)) 282 #define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2)) 283 #define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1) 284 285 /* Chip sampling rates 286 * 287 * Some sensors are not updated more frequently than once per second 288 * so it doesn't make sense to read them more often than that. 289 * We cache the results and return the saved data if the driver 290 * is called again before a second has elapsed. 291 * 292 * Also, there is significant configuration data for this chip 293 * given the automatic PWM fan control that is possible. There 294 * are about 47 bytes of config data to only 22 bytes of actual 295 * readings. So, we keep the config data up to date in the cache 296 * when it is written and only sample it once every 1 *minute* 297 */ 298 #define LM85_DATA_INTERVAL (HZ + HZ / 2) 299 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ) 300 301 /* For each registered LM85, we need to keep some data in memory. That 302 data is pointed to by lm85_list[NR]->data. The structure itself is 303 dynamically allocated, at the same time when a new lm85 client is 304 allocated. */ 305 306 /* LM85 can automatically adjust fan speeds based on temperature 307 * This structure encapsulates an entire Zone config. There are 308 * three zones (one for each temperature input) on the lm85 309 */ 310 struct lm85_zone { 311 s8 limit; /* Low temp limit */ 312 u8 hyst; /* Low limit hysteresis. (0-15) */ 313 u8 range; /* Temp range, encoded */ 314 s8 critical; /* "All fans ON" temp limit */ 315 u8 off_desired; /* Actual "off" temperature specified. Preserved 316 * to prevent "drift" as other autofan control 317 * values change. 318 */ 319 u8 max_desired; /* Actual "max" temperature specified. Preserved 320 * to prevent "drift" as other autofan control 321 * values change. 322 */ 323 }; 324 325 struct lm85_autofan { 326 u8 config; /* Register value */ 327 u8 freq; /* PWM frequency, encoded */ 328 u8 min_pwm; /* Minimum PWM value, encoded */ 329 u8 min_off; /* Min PWM or OFF below "limit", flag */ 330 }; 331 332 struct lm85_data { 333 struct i2c_client client; 334 struct class_device *class_dev; 335 struct semaphore lock; 336 enum chips type; 337 338 struct semaphore update_lock; 339 int valid; /* !=0 if following fields are valid */ 340 unsigned long last_reading; /* In jiffies */ 341 unsigned long last_config; /* In jiffies */ 342 343 u8 in[8]; /* Register value */ 344 u8 in_max[8]; /* Register value */ 345 u8 in_min[8]; /* Register value */ 346 s8 temp[3]; /* Register value */ 347 s8 temp_min[3]; /* Register value */ 348 s8 temp_max[3]; /* Register value */ 349 s8 temp_offset[3]; /* Register value */ 350 u16 fan[4]; /* Register value */ 351 u16 fan_min[4]; /* Register value */ 352 u8 pwm[3]; /* Register value */ 353 u8 spinup_ctl; /* Register encoding, combined */ 354 u8 tach_mode; /* Register encoding, combined */ 355 u8 temp_ext[3]; /* Decoded values */ 356 u8 in_ext[8]; /* Decoded values */ 357 u8 adc_scale; /* ADC Extended bits scaling factor */ 358 u8 fan_ppr; /* Register value */ 359 u8 smooth[3]; /* Register encoding */ 360 u8 vid; /* Register value */ 361 u8 vrm; /* VRM version */ 362 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */ 363 u8 oppoint[3]; /* Register value */ 364 u16 tmin_ctl; /* Register value */ 365 unsigned long therm_total; /* Cummulative therm count */ 366 u8 therm_limit; /* Register value */ 367 u32 alarms; /* Register encoding, combined */ 368 struct lm85_autofan autofan[3]; 369 struct lm85_zone zone[3]; 370 }; 371 372 static int lm85_attach_adapter(struct i2c_adapter *adapter); 373 static int lm85_detect(struct i2c_adapter *adapter, int address, 374 int kind); 375 static int lm85_detach_client(struct i2c_client *client); 376 377 static int lm85_read_value(struct i2c_client *client, u8 register); 378 static int lm85_write_value(struct i2c_client *client, u8 register, int value); 379 static struct lm85_data *lm85_update_device(struct device *dev); 380 static void lm85_init_client(struct i2c_client *client); 381 382 383 static struct i2c_driver lm85_driver = { 384 .owner = THIS_MODULE, 385 .name = "lm85", 386 .id = I2C_DRIVERID_LM85, 387 .flags = I2C_DF_NOTIFY, 388 .attach_adapter = lm85_attach_adapter, 389 .detach_client = lm85_detach_client, 390 }; 391 392 393 /* 4 Fans */ 394 static ssize_t show_fan(struct device *dev, char *buf, int nr) 395 { 396 struct lm85_data *data = lm85_update_device(dev); 397 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) ); 398 } 399 static ssize_t show_fan_min(struct device *dev, char *buf, int nr) 400 { 401 struct lm85_data *data = lm85_update_device(dev); 402 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) ); 403 } 404 static ssize_t set_fan_min(struct device *dev, const char *buf, 405 size_t count, int nr) 406 { 407 struct i2c_client *client = to_i2c_client(dev); 408 struct lm85_data *data = i2c_get_clientdata(client); 409 long val = simple_strtol(buf, NULL, 10); 410 411 down(&data->update_lock); 412 data->fan_min[nr] = FAN_TO_REG(val); 413 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]); 414 up(&data->update_lock); 415 return count; 416 } 417 418 #define show_fan_offset(offset) \ 419 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ 420 { \ 421 return show_fan(dev, buf, offset - 1); \ 422 } \ 423 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ 424 { \ 425 return show_fan_min(dev, buf, offset - 1); \ 426 } \ 427 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \ 428 const char *buf, size_t count) \ 429 { \ 430 return set_fan_min(dev, buf, count, offset - 1); \ 431 } \ 432 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \ 433 NULL); \ 434 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ 435 show_fan_##offset##_min, set_fan_##offset##_min); 436 437 show_fan_offset(1); 438 show_fan_offset(2); 439 show_fan_offset(3); 440 show_fan_offset(4); 441 442 /* vid, vrm, alarms */ 443 444 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf) 445 { 446 struct lm85_data *data = lm85_update_device(dev); 447 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm)); 448 } 449 450 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); 451 452 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf) 453 { 454 struct lm85_data *data = lm85_update_device(dev); 455 return sprintf(buf, "%ld\n", (long) data->vrm); 456 } 457 458 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 459 { 460 struct i2c_client *client = to_i2c_client(dev); 461 struct lm85_data *data = i2c_get_clientdata(client); 462 u32 val; 463 464 val = simple_strtoul(buf, NULL, 10); 465 data->vrm = val; 466 return count; 467 } 468 469 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); 470 471 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf) 472 { 473 struct lm85_data *data = lm85_update_device(dev); 474 return sprintf(buf, "%u\n", data->alarms); 475 } 476 477 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL); 478 479 /* pwm */ 480 481 static ssize_t show_pwm(struct device *dev, char *buf, int nr) 482 { 483 struct lm85_data *data = lm85_update_device(dev); 484 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) ); 485 } 486 static ssize_t set_pwm(struct device *dev, const char *buf, 487 size_t count, int nr) 488 { 489 struct i2c_client *client = to_i2c_client(dev); 490 struct lm85_data *data = i2c_get_clientdata(client); 491 long val = simple_strtol(buf, NULL, 10); 492 493 down(&data->update_lock); 494 data->pwm[nr] = PWM_TO_REG(val); 495 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]); 496 up(&data->update_lock); 497 return count; 498 } 499 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr) 500 { 501 struct lm85_data *data = lm85_update_device(dev); 502 int pwm_zone; 503 504 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config); 505 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) ); 506 } 507 508 #define show_pwm_reg(offset) \ 509 static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ 510 { \ 511 return show_pwm(dev, buf, offset - 1); \ 512 } \ 513 static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \ 514 const char *buf, size_t count) \ 515 { \ 516 return set_pwm(dev, buf, count, offset - 1); \ 517 } \ 518 static ssize_t show_pwm_enable##offset (struct device *dev, struct device_attribute *attr, char *buf) \ 519 { \ 520 return show_pwm_enable(dev, buf, offset - 1); \ 521 } \ 522 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ 523 show_pwm_##offset, set_pwm_##offset); \ 524 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \ 525 show_pwm_enable##offset, NULL); 526 527 show_pwm_reg(1); 528 show_pwm_reg(2); 529 show_pwm_reg(3); 530 531 /* Voltages */ 532 533 static ssize_t show_in(struct device *dev, char *buf, int nr) 534 { 535 struct lm85_data *data = lm85_update_device(dev); 536 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr, 537 data->in[nr], 538 data->in_ext[nr], 539 data->adc_scale) ); 540 } 541 static ssize_t show_in_min(struct device *dev, char *buf, int nr) 542 { 543 struct lm85_data *data = lm85_update_device(dev); 544 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) ); 545 } 546 static ssize_t set_in_min(struct device *dev, const char *buf, 547 size_t count, int nr) 548 { 549 struct i2c_client *client = to_i2c_client(dev); 550 struct lm85_data *data = i2c_get_clientdata(client); 551 long val = simple_strtol(buf, NULL, 10); 552 553 down(&data->update_lock); 554 data->in_min[nr] = INS_TO_REG(nr, val); 555 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]); 556 up(&data->update_lock); 557 return count; 558 } 559 static ssize_t show_in_max(struct device *dev, char *buf, int nr) 560 { 561 struct lm85_data *data = lm85_update_device(dev); 562 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) ); 563 } 564 static ssize_t set_in_max(struct device *dev, const char *buf, 565 size_t count, int nr) 566 { 567 struct i2c_client *client = to_i2c_client(dev); 568 struct lm85_data *data = i2c_get_clientdata(client); 569 long val = simple_strtol(buf, NULL, 10); 570 571 down(&data->update_lock); 572 data->in_max[nr] = INS_TO_REG(nr, val); 573 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]); 574 up(&data->update_lock); 575 return count; 576 } 577 #define show_in_reg(offset) \ 578 static ssize_t show_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ 579 { \ 580 return show_in(dev, buf, offset); \ 581 } \ 582 static ssize_t show_in_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ 583 { \ 584 return show_in_min(dev, buf, offset); \ 585 } \ 586 static ssize_t show_in_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \ 587 { \ 588 return show_in_max(dev, buf, offset); \ 589 } \ 590 static ssize_t set_in_##offset##_min (struct device *dev, struct device_attribute *attr, \ 591 const char *buf, size_t count) \ 592 { \ 593 return set_in_min(dev, buf, count, offset); \ 594 } \ 595 static ssize_t set_in_##offset##_max (struct device *dev, struct device_attribute *attr, \ 596 const char *buf, size_t count) \ 597 { \ 598 return set_in_max(dev, buf, count, offset); \ 599 } \ 600 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, \ 601 NULL); \ 602 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ 603 show_in_##offset##_min, set_in_##offset##_min); \ 604 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ 605 show_in_##offset##_max, set_in_##offset##_max); 606 607 show_in_reg(0); 608 show_in_reg(1); 609 show_in_reg(2); 610 show_in_reg(3); 611 show_in_reg(4); 612 613 /* Temps */ 614 615 static ssize_t show_temp(struct device *dev, char *buf, int nr) 616 { 617 struct lm85_data *data = lm85_update_device(dev); 618 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr], 619 data->temp_ext[nr], 620 data->adc_scale) ); 621 } 622 static ssize_t show_temp_min(struct device *dev, char *buf, int nr) 623 { 624 struct lm85_data *data = lm85_update_device(dev); 625 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) ); 626 } 627 static ssize_t set_temp_min(struct device *dev, const char *buf, 628 size_t count, int nr) 629 { 630 struct i2c_client *client = to_i2c_client(dev); 631 struct lm85_data *data = i2c_get_clientdata(client); 632 long val = simple_strtol(buf, NULL, 10); 633 634 down(&data->update_lock); 635 data->temp_min[nr] = TEMP_TO_REG(val); 636 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]); 637 up(&data->update_lock); 638 return count; 639 } 640 static ssize_t show_temp_max(struct device *dev, char *buf, int nr) 641 { 642 struct lm85_data *data = lm85_update_device(dev); 643 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) ); 644 } 645 static ssize_t set_temp_max(struct device *dev, const char *buf, 646 size_t count, int nr) 647 { 648 struct i2c_client *client = to_i2c_client(dev); 649 struct lm85_data *data = i2c_get_clientdata(client); 650 long val = simple_strtol(buf, NULL, 10); 651 652 down(&data->update_lock); 653 data->temp_max[nr] = TEMP_TO_REG(val); 654 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]); 655 up(&data->update_lock); 656 return count; 657 } 658 #define show_temp_reg(offset) \ 659 static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ 660 { \ 661 return show_temp(dev, buf, offset - 1); \ 662 } \ 663 static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ 664 { \ 665 return show_temp_min(dev, buf, offset - 1); \ 666 } \ 667 static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \ 668 { \ 669 return show_temp_max(dev, buf, offset - 1); \ 670 } \ 671 static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \ 672 const char *buf, size_t count) \ 673 { \ 674 return set_temp_min(dev, buf, count, offset - 1); \ 675 } \ 676 static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \ 677 const char *buf, size_t count) \ 678 { \ 679 return set_temp_max(dev, buf, count, offset - 1); \ 680 } \ 681 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \ 682 NULL); \ 683 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ 684 show_temp_##offset##_min, set_temp_##offset##_min); \ 685 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ 686 show_temp_##offset##_max, set_temp_##offset##_max); 687 688 show_temp_reg(1); 689 show_temp_reg(2); 690 show_temp_reg(3); 691 692 693 /* Automatic PWM control */ 694 695 static ssize_t show_pwm_auto_channels(struct device *dev, char *buf, int nr) 696 { 697 struct lm85_data *data = lm85_update_device(dev); 698 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config)); 699 } 700 static ssize_t set_pwm_auto_channels(struct device *dev, const char *buf, 701 size_t count, int nr) 702 { 703 struct i2c_client *client = to_i2c_client(dev); 704 struct lm85_data *data = i2c_get_clientdata(client); 705 long val = simple_strtol(buf, NULL, 10); 706 707 down(&data->update_lock); 708 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0)) 709 | ZONE_TO_REG(val) ; 710 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr), 711 data->autofan[nr].config); 712 up(&data->update_lock); 713 return count; 714 } 715 static ssize_t show_pwm_auto_pwm_min(struct device *dev, char *buf, int nr) 716 { 717 struct lm85_data *data = lm85_update_device(dev); 718 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm)); 719 } 720 static ssize_t set_pwm_auto_pwm_min(struct device *dev, const char *buf, 721 size_t count, int nr) 722 { 723 struct i2c_client *client = to_i2c_client(dev); 724 struct lm85_data *data = i2c_get_clientdata(client); 725 long val = simple_strtol(buf, NULL, 10); 726 727 down(&data->update_lock); 728 data->autofan[nr].min_pwm = PWM_TO_REG(val); 729 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr), 730 data->autofan[nr].min_pwm); 731 up(&data->update_lock); 732 return count; 733 } 734 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, char *buf, int nr) 735 { 736 struct lm85_data *data = lm85_update_device(dev); 737 return sprintf(buf,"%d\n", data->autofan[nr].min_off); 738 } 739 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, const char *buf, 740 size_t count, int nr) 741 { 742 struct i2c_client *client = to_i2c_client(dev); 743 struct lm85_data *data = i2c_get_clientdata(client); 744 long val = simple_strtol(buf, NULL, 10); 745 746 down(&data->update_lock); 747 data->autofan[nr].min_off = val; 748 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0] 749 | data->syncpwm3 750 | (data->autofan[0].min_off ? 0x20 : 0) 751 | (data->autofan[1].min_off ? 0x40 : 0) 752 | (data->autofan[2].min_off ? 0x80 : 0) 753 ); 754 up(&data->update_lock); 755 return count; 756 } 757 static ssize_t show_pwm_auto_pwm_freq(struct device *dev, char *buf, int nr) 758 { 759 struct lm85_data *data = lm85_update_device(dev); 760 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq)); 761 } 762 static ssize_t set_pwm_auto_pwm_freq(struct device *dev, const char *buf, 763 size_t count, int nr) 764 { 765 struct i2c_client *client = to_i2c_client(dev); 766 struct lm85_data *data = i2c_get_clientdata(client); 767 long val = simple_strtol(buf, NULL, 10); 768 769 down(&data->update_lock); 770 data->autofan[nr].freq = FREQ_TO_REG(val); 771 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), 772 (data->zone[nr].range << 4) 773 | data->autofan[nr].freq 774 ); 775 up(&data->update_lock); 776 return count; 777 } 778 #define pwm_auto(offset) \ 779 static ssize_t show_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr, \ 780 char *buf) \ 781 { \ 782 return show_pwm_auto_channels(dev, buf, offset - 1); \ 783 } \ 784 static ssize_t set_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr, \ 785 const char *buf, size_t count) \ 786 { \ 787 return set_pwm_auto_channels(dev, buf, count, offset - 1); \ 788 } \ 789 static ssize_t show_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr, \ 790 char *buf) \ 791 { \ 792 return show_pwm_auto_pwm_min(dev, buf, offset - 1); \ 793 } \ 794 static ssize_t set_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr, \ 795 const char *buf, size_t count) \ 796 { \ 797 return set_pwm_auto_pwm_min(dev, buf, count, offset - 1); \ 798 } \ 799 static ssize_t show_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr, \ 800 char *buf) \ 801 { \ 802 return show_pwm_auto_pwm_minctl(dev, buf, offset - 1); \ 803 } \ 804 static ssize_t set_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr, \ 805 const char *buf, size_t count) \ 806 { \ 807 return set_pwm_auto_pwm_minctl(dev, buf, count, offset - 1); \ 808 } \ 809 static ssize_t show_pwm##offset##_auto_pwm_freq (struct device *dev, struct device_attribute *attr, \ 810 char *buf) \ 811 { \ 812 return show_pwm_auto_pwm_freq(dev, buf, offset - 1); \ 813 } \ 814 static ssize_t set_pwm##offset##_auto_pwm_freq(struct device *dev, struct device_attribute *attr, \ 815 const char *buf, size_t count) \ 816 { \ 817 return set_pwm_auto_pwm_freq(dev, buf, count, offset - 1); \ 818 } \ 819 static DEVICE_ATTR(pwm##offset##_auto_channels, S_IRUGO | S_IWUSR, \ 820 show_pwm##offset##_auto_channels, \ 821 set_pwm##offset##_auto_channels); \ 822 static DEVICE_ATTR(pwm##offset##_auto_pwm_min, S_IRUGO | S_IWUSR, \ 823 show_pwm##offset##_auto_pwm_min, \ 824 set_pwm##offset##_auto_pwm_min); \ 825 static DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, S_IRUGO | S_IWUSR, \ 826 show_pwm##offset##_auto_pwm_minctl, \ 827 set_pwm##offset##_auto_pwm_minctl); \ 828 static DEVICE_ATTR(pwm##offset##_auto_pwm_freq, S_IRUGO | S_IWUSR, \ 829 show_pwm##offset##_auto_pwm_freq, \ 830 set_pwm##offset##_auto_pwm_freq); 831 pwm_auto(1); 832 pwm_auto(2); 833 pwm_auto(3); 834 835 /* Temperature settings for automatic PWM control */ 836 837 static ssize_t show_temp_auto_temp_off(struct device *dev, char *buf, int nr) 838 { 839 struct lm85_data *data = lm85_update_device(dev); 840 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) - 841 HYST_FROM_REG(data->zone[nr].hyst)); 842 } 843 static ssize_t set_temp_auto_temp_off(struct device *dev, const char *buf, 844 size_t count, int nr) 845 { 846 struct i2c_client *client = to_i2c_client(dev); 847 struct lm85_data *data = i2c_get_clientdata(client); 848 int min; 849 long val = simple_strtol(buf, NULL, 10); 850 851 down(&data->update_lock); 852 min = TEMP_FROM_REG(data->zone[nr].limit); 853 data->zone[nr].off_desired = TEMP_TO_REG(val); 854 data->zone[nr].hyst = HYST_TO_REG(min - val); 855 if ( nr == 0 || nr == 1 ) { 856 lm85_write_value(client, LM85_REG_AFAN_HYST1, 857 (data->zone[0].hyst << 4) 858 | data->zone[1].hyst 859 ); 860 } else { 861 lm85_write_value(client, LM85_REG_AFAN_HYST2, 862 (data->zone[2].hyst << 4) 863 ); 864 } 865 up(&data->update_lock); 866 return count; 867 } 868 static ssize_t show_temp_auto_temp_min(struct device *dev, char *buf, int nr) 869 { 870 struct lm85_data *data = lm85_update_device(dev); 871 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) ); 872 } 873 static ssize_t set_temp_auto_temp_min(struct device *dev, const char *buf, 874 size_t count, int nr) 875 { 876 struct i2c_client *client = to_i2c_client(dev); 877 struct lm85_data *data = i2c_get_clientdata(client); 878 long val = simple_strtol(buf, NULL, 10); 879 880 down(&data->update_lock); 881 data->zone[nr].limit = TEMP_TO_REG(val); 882 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr), 883 data->zone[nr].limit); 884 885 /* Update temp_auto_max and temp_auto_range */ 886 data->zone[nr].range = RANGE_TO_REG( 887 TEMP_FROM_REG(data->zone[nr].max_desired) - 888 TEMP_FROM_REG(data->zone[nr].limit)); 889 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), 890 ((data->zone[nr].range & 0x0f) << 4) 891 | (data->autofan[nr].freq & 0x07)); 892 893 /* Update temp_auto_hyst and temp_auto_off */ 894 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG( 895 data->zone[nr].limit) - TEMP_FROM_REG( 896 data->zone[nr].off_desired)); 897 if ( nr == 0 || nr == 1 ) { 898 lm85_write_value(client, LM85_REG_AFAN_HYST1, 899 (data->zone[0].hyst << 4) 900 | data->zone[1].hyst 901 ); 902 } else { 903 lm85_write_value(client, LM85_REG_AFAN_HYST2, 904 (data->zone[2].hyst << 4) 905 ); 906 } 907 up(&data->update_lock); 908 return count; 909 } 910 static ssize_t show_temp_auto_temp_max(struct device *dev, char *buf, int nr) 911 { 912 struct lm85_data *data = lm85_update_device(dev); 913 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) + 914 RANGE_FROM_REG(data->zone[nr].range)); 915 } 916 static ssize_t set_temp_auto_temp_max(struct device *dev, const char *buf, 917 size_t count, int nr) 918 { 919 struct i2c_client *client = to_i2c_client(dev); 920 struct lm85_data *data = i2c_get_clientdata(client); 921 int min; 922 long val = simple_strtol(buf, NULL, 10); 923 924 down(&data->update_lock); 925 min = TEMP_FROM_REG(data->zone[nr].limit); 926 data->zone[nr].max_desired = TEMP_TO_REG(val); 927 data->zone[nr].range = RANGE_TO_REG( 928 val - min); 929 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), 930 ((data->zone[nr].range & 0x0f) << 4) 931 | (data->autofan[nr].freq & 0x07)); 932 up(&data->update_lock); 933 return count; 934 } 935 static ssize_t show_temp_auto_temp_crit(struct device *dev, char *buf, int nr) 936 { 937 struct lm85_data *data = lm85_update_device(dev); 938 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical)); 939 } 940 static ssize_t set_temp_auto_temp_crit(struct device *dev, const char *buf, 941 size_t count, int nr) 942 { 943 struct i2c_client *client = to_i2c_client(dev); 944 struct lm85_data *data = i2c_get_clientdata(client); 945 long val = simple_strtol(buf, NULL, 10); 946 947 down(&data->update_lock); 948 data->zone[nr].critical = TEMP_TO_REG(val); 949 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr), 950 data->zone[nr].critical); 951 up(&data->update_lock); 952 return count; 953 } 954 #define temp_auto(offset) \ 955 static ssize_t show_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr, \ 956 char *buf) \ 957 { \ 958 return show_temp_auto_temp_off(dev, buf, offset - 1); \ 959 } \ 960 static ssize_t set_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr, \ 961 const char *buf, size_t count) \ 962 { \ 963 return set_temp_auto_temp_off(dev, buf, count, offset - 1); \ 964 } \ 965 static ssize_t show_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr, \ 966 char *buf) \ 967 { \ 968 return show_temp_auto_temp_min(dev, buf, offset - 1); \ 969 } \ 970 static ssize_t set_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr, \ 971 const char *buf, size_t count) \ 972 { \ 973 return set_temp_auto_temp_min(dev, buf, count, offset - 1); \ 974 } \ 975 static ssize_t show_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr, \ 976 char *buf) \ 977 { \ 978 return show_temp_auto_temp_max(dev, buf, offset - 1); \ 979 } \ 980 static ssize_t set_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr, \ 981 const char *buf, size_t count) \ 982 { \ 983 return set_temp_auto_temp_max(dev, buf, count, offset - 1); \ 984 } \ 985 static ssize_t show_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr, \ 986 char *buf) \ 987 { \ 988 return show_temp_auto_temp_crit(dev, buf, offset - 1); \ 989 } \ 990 static ssize_t set_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr, \ 991 const char *buf, size_t count) \ 992 { \ 993 return set_temp_auto_temp_crit(dev, buf, count, offset - 1); \ 994 } \ 995 static DEVICE_ATTR(temp##offset##_auto_temp_off, S_IRUGO | S_IWUSR, \ 996 show_temp##offset##_auto_temp_off, \ 997 set_temp##offset##_auto_temp_off); \ 998 static DEVICE_ATTR(temp##offset##_auto_temp_min, S_IRUGO | S_IWUSR, \ 999 show_temp##offset##_auto_temp_min, \ 1000 set_temp##offset##_auto_temp_min); \ 1001 static DEVICE_ATTR(temp##offset##_auto_temp_max, S_IRUGO | S_IWUSR, \ 1002 show_temp##offset##_auto_temp_max, \ 1003 set_temp##offset##_auto_temp_max); \ 1004 static DEVICE_ATTR(temp##offset##_auto_temp_crit, S_IRUGO | S_IWUSR, \ 1005 show_temp##offset##_auto_temp_crit, \ 1006 set_temp##offset##_auto_temp_crit); 1007 temp_auto(1); 1008 temp_auto(2); 1009 temp_auto(3); 1010 1011 int lm85_attach_adapter(struct i2c_adapter *adapter) 1012 { 1013 if (!(adapter->class & I2C_CLASS_HWMON)) 1014 return 0; 1015 return i2c_probe(adapter, &addr_data, lm85_detect); 1016 } 1017 1018 int lm85_detect(struct i2c_adapter *adapter, int address, 1019 int kind) 1020 { 1021 int company, verstep ; 1022 struct i2c_client *new_client = NULL; 1023 struct lm85_data *data; 1024 int err = 0; 1025 const char *type_name = ""; 1026 1027 if (!i2c_check_functionality(adapter, 1028 I2C_FUNC_SMBUS_BYTE_DATA)) { 1029 /* We need to be able to do byte I/O */ 1030 goto ERROR0 ; 1031 }; 1032 1033 /* OK. For now, we presume we have a valid client. We now create the 1034 client structure, even though we cannot fill it completely yet. 1035 But it allows us to access lm85_{read,write}_value. */ 1036 1037 if (!(data = kmalloc(sizeof(struct lm85_data), GFP_KERNEL))) { 1038 err = -ENOMEM; 1039 goto ERROR0; 1040 } 1041 memset(data, 0, sizeof(struct lm85_data)); 1042 1043 new_client = &data->client; 1044 i2c_set_clientdata(new_client, data); 1045 new_client->addr = address; 1046 new_client->adapter = adapter; 1047 new_client->driver = &lm85_driver; 1048 new_client->flags = 0; 1049 1050 /* Now, we do the remaining detection. */ 1051 1052 company = lm85_read_value(new_client, LM85_REG_COMPANY); 1053 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP); 1054 1055 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with" 1056 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n", 1057 i2c_adapter_id(new_client->adapter), new_client->addr, 1058 company, verstep); 1059 1060 /* If auto-detecting, Determine the chip type. */ 1061 if (kind <= 0) { 1062 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n", 1063 i2c_adapter_id(adapter), address ); 1064 if( company == LM85_COMPANY_NATIONAL 1065 && verstep == LM85_VERSTEP_LM85C ) { 1066 kind = lm85c ; 1067 } else if( company == LM85_COMPANY_NATIONAL 1068 && verstep == LM85_VERSTEP_LM85B ) { 1069 kind = lm85b ; 1070 } else if( company == LM85_COMPANY_NATIONAL 1071 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) { 1072 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x" 1073 " Defaulting to LM85.\n", verstep); 1074 kind = any_chip ; 1075 } else if( company == LM85_COMPANY_ANALOG_DEV 1076 && verstep == LM85_VERSTEP_ADM1027 ) { 1077 kind = adm1027 ; 1078 } else if( company == LM85_COMPANY_ANALOG_DEV 1079 && (verstep == LM85_VERSTEP_ADT7463 1080 || verstep == LM85_VERSTEP_ADT7463C) ) { 1081 kind = adt7463 ; 1082 } else if( company == LM85_COMPANY_ANALOG_DEV 1083 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) { 1084 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x" 1085 " Defaulting to Generic LM85.\n", verstep ); 1086 kind = any_chip ; 1087 } else if( company == LM85_COMPANY_SMSC 1088 && (verstep == LM85_VERSTEP_EMC6D100_A0 1089 || verstep == LM85_VERSTEP_EMC6D100_A1) ) { 1090 /* Unfortunately, we can't tell a '100 from a '101 1091 * from the registers. Since a '101 is a '100 1092 * in a package with fewer pins and therefore no 1093 * 3.3V, 1.5V or 1.8V inputs, perhaps if those 1094 * inputs read 0, then it's a '101. 1095 */ 1096 kind = emc6d100 ; 1097 } else if( company == LM85_COMPANY_SMSC 1098 && verstep == LM85_VERSTEP_EMC6D102) { 1099 kind = emc6d102 ; 1100 } else if( company == LM85_COMPANY_SMSC 1101 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) { 1102 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n"); 1103 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x" 1104 " Defaulting to Generic LM85.\n", verstep ); 1105 kind = any_chip ; 1106 } else if( kind == any_chip 1107 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) { 1108 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n"); 1109 /* Leave kind as "any_chip" */ 1110 } else { 1111 dev_dbg(&adapter->dev, "Autodetection failed\n"); 1112 /* Not an LM85 ... */ 1113 if( kind == any_chip ) { /* User used force=x,y */ 1114 dev_err(&adapter->dev, "Generic LM85 Version 6 not" 1115 " found at %d,0x%02x. Try force_lm85c.\n", 1116 i2c_adapter_id(adapter), address ); 1117 } 1118 err = 0 ; 1119 goto ERROR1; 1120 } 1121 } 1122 1123 /* Fill in the chip specific driver values */ 1124 if ( kind == any_chip ) { 1125 type_name = "lm85"; 1126 } else if ( kind == lm85b ) { 1127 type_name = "lm85b"; 1128 } else if ( kind == lm85c ) { 1129 type_name = "lm85c"; 1130 } else if ( kind == adm1027 ) { 1131 type_name = "adm1027"; 1132 } else if ( kind == adt7463 ) { 1133 type_name = "adt7463"; 1134 } else if ( kind == emc6d100){ 1135 type_name = "emc6d100"; 1136 } else if ( kind == emc6d102 ) { 1137 type_name = "emc6d102"; 1138 } 1139 strlcpy(new_client->name, type_name, I2C_NAME_SIZE); 1140 1141 /* Fill in the remaining client fields */ 1142 data->type = kind; 1143 data->valid = 0; 1144 init_MUTEX(&data->update_lock); 1145 1146 /* Tell the I2C layer a new client has arrived */ 1147 if ((err = i2c_attach_client(new_client))) 1148 goto ERROR1; 1149 1150 /* Set the VRM version */ 1151 data->vrm = i2c_which_vrm(); 1152 1153 /* Initialize the LM85 chip */ 1154 lm85_init_client(new_client); 1155 1156 /* Register sysfs hooks */ 1157 data->class_dev = hwmon_device_register(&new_client->dev); 1158 if (IS_ERR(data->class_dev)) { 1159 err = PTR_ERR(data->class_dev); 1160 goto ERROR2; 1161 } 1162 1163 device_create_file(&new_client->dev, &dev_attr_fan1_input); 1164 device_create_file(&new_client->dev, &dev_attr_fan2_input); 1165 device_create_file(&new_client->dev, &dev_attr_fan3_input); 1166 device_create_file(&new_client->dev, &dev_attr_fan4_input); 1167 device_create_file(&new_client->dev, &dev_attr_fan1_min); 1168 device_create_file(&new_client->dev, &dev_attr_fan2_min); 1169 device_create_file(&new_client->dev, &dev_attr_fan3_min); 1170 device_create_file(&new_client->dev, &dev_attr_fan4_min); 1171 device_create_file(&new_client->dev, &dev_attr_pwm1); 1172 device_create_file(&new_client->dev, &dev_attr_pwm2); 1173 device_create_file(&new_client->dev, &dev_attr_pwm3); 1174 device_create_file(&new_client->dev, &dev_attr_pwm1_enable); 1175 device_create_file(&new_client->dev, &dev_attr_pwm2_enable); 1176 device_create_file(&new_client->dev, &dev_attr_pwm3_enable); 1177 device_create_file(&new_client->dev, &dev_attr_in0_input); 1178 device_create_file(&new_client->dev, &dev_attr_in1_input); 1179 device_create_file(&new_client->dev, &dev_attr_in2_input); 1180 device_create_file(&new_client->dev, &dev_attr_in3_input); 1181 device_create_file(&new_client->dev, &dev_attr_in4_input); 1182 device_create_file(&new_client->dev, &dev_attr_in0_min); 1183 device_create_file(&new_client->dev, &dev_attr_in1_min); 1184 device_create_file(&new_client->dev, &dev_attr_in2_min); 1185 device_create_file(&new_client->dev, &dev_attr_in3_min); 1186 device_create_file(&new_client->dev, &dev_attr_in4_min); 1187 device_create_file(&new_client->dev, &dev_attr_in0_max); 1188 device_create_file(&new_client->dev, &dev_attr_in1_max); 1189 device_create_file(&new_client->dev, &dev_attr_in2_max); 1190 device_create_file(&new_client->dev, &dev_attr_in3_max); 1191 device_create_file(&new_client->dev, &dev_attr_in4_max); 1192 device_create_file(&new_client->dev, &dev_attr_temp1_input); 1193 device_create_file(&new_client->dev, &dev_attr_temp2_input); 1194 device_create_file(&new_client->dev, &dev_attr_temp3_input); 1195 device_create_file(&new_client->dev, &dev_attr_temp1_min); 1196 device_create_file(&new_client->dev, &dev_attr_temp2_min); 1197 device_create_file(&new_client->dev, &dev_attr_temp3_min); 1198 device_create_file(&new_client->dev, &dev_attr_temp1_max); 1199 device_create_file(&new_client->dev, &dev_attr_temp2_max); 1200 device_create_file(&new_client->dev, &dev_attr_temp3_max); 1201 device_create_file(&new_client->dev, &dev_attr_vrm); 1202 device_create_file(&new_client->dev, &dev_attr_cpu0_vid); 1203 device_create_file(&new_client->dev, &dev_attr_alarms); 1204 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_channels); 1205 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_channels); 1206 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_channels); 1207 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_min); 1208 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_min); 1209 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_min); 1210 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_minctl); 1211 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_minctl); 1212 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_minctl); 1213 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_freq); 1214 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_freq); 1215 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_freq); 1216 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_off); 1217 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_off); 1218 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_off); 1219 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_min); 1220 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_min); 1221 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_min); 1222 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_max); 1223 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_max); 1224 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_max); 1225 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_crit); 1226 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_crit); 1227 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_crit); 1228 1229 return 0; 1230 1231 /* Error out and cleanup code */ 1232 ERROR2: 1233 i2c_detach_client(new_client); 1234 ERROR1: 1235 kfree(data); 1236 ERROR0: 1237 return err; 1238 } 1239 1240 int lm85_detach_client(struct i2c_client *client) 1241 { 1242 struct lm85_data *data = i2c_get_clientdata(client); 1243 hwmon_device_unregister(data->class_dev); 1244 i2c_detach_client(client); 1245 kfree(data); 1246 return 0; 1247 } 1248 1249 1250 int lm85_read_value(struct i2c_client *client, u8 reg) 1251 { 1252 int res; 1253 1254 /* What size location is it? */ 1255 switch( reg ) { 1256 case LM85_REG_FAN(0) : /* Read WORD data */ 1257 case LM85_REG_FAN(1) : 1258 case LM85_REG_FAN(2) : 1259 case LM85_REG_FAN(3) : 1260 case LM85_REG_FAN_MIN(0) : 1261 case LM85_REG_FAN_MIN(1) : 1262 case LM85_REG_FAN_MIN(2) : 1263 case LM85_REG_FAN_MIN(3) : 1264 case LM85_REG_ALARM1 : /* Read both bytes at once */ 1265 res = i2c_smbus_read_byte_data(client, reg) & 0xff ; 1266 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ; 1267 break ; 1268 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */ 1269 res = i2c_smbus_read_byte_data(client, reg) << 8 ; 1270 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ; 1271 break ; 1272 default: /* Read BYTE data */ 1273 res = i2c_smbus_read_byte_data(client, reg); 1274 break ; 1275 } 1276 1277 return res ; 1278 } 1279 1280 int lm85_write_value(struct i2c_client *client, u8 reg, int value) 1281 { 1282 int res ; 1283 1284 switch( reg ) { 1285 case LM85_REG_FAN(0) : /* Write WORD data */ 1286 case LM85_REG_FAN(1) : 1287 case LM85_REG_FAN(2) : 1288 case LM85_REG_FAN(3) : 1289 case LM85_REG_FAN_MIN(0) : 1290 case LM85_REG_FAN_MIN(1) : 1291 case LM85_REG_FAN_MIN(2) : 1292 case LM85_REG_FAN_MIN(3) : 1293 /* NOTE: ALARM is read only, so not included here */ 1294 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ; 1295 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ; 1296 break ; 1297 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */ 1298 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff); 1299 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ; 1300 break ; 1301 default: /* Write BYTE data */ 1302 res = i2c_smbus_write_byte_data(client, reg, value); 1303 break ; 1304 } 1305 1306 return res ; 1307 } 1308 1309 void lm85_init_client(struct i2c_client *client) 1310 { 1311 int value; 1312 struct lm85_data *data = i2c_get_clientdata(client); 1313 1314 dev_dbg(&client->dev, "Initializing device\n"); 1315 1316 /* Warn if part was not "READY" */ 1317 value = lm85_read_value(client, LM85_REG_CONFIG); 1318 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value); 1319 if( value & 0x02 ) { 1320 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n", 1321 i2c_adapter_id(client->adapter), client->addr ); 1322 }; 1323 if( ! (value & 0x04) ) { 1324 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n", 1325 i2c_adapter_id(client->adapter), client->addr ); 1326 }; 1327 if( value & 0x10 1328 && ( data->type == adm1027 1329 || data->type == adt7463 ) ) { 1330 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. " 1331 "Please report this to the lm85 maintainer.\n", 1332 i2c_adapter_id(client->adapter), client->addr ); 1333 }; 1334 1335 /* WE INTENTIONALLY make no changes to the limits, 1336 * offsets, pwms, fans and zones. If they were 1337 * configured, we don't want to mess with them. 1338 * If they weren't, the default is 100% PWM, no 1339 * control and will suffice until 'sensors -s' 1340 * can be run by the user. 1341 */ 1342 1343 /* Start monitoring */ 1344 value = lm85_read_value(client, LM85_REG_CONFIG); 1345 /* Try to clear LOCK, Set START, save everything else */ 1346 value = (value & ~ 0x02) | 0x01 ; 1347 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value); 1348 lm85_write_value(client, LM85_REG_CONFIG, value); 1349 } 1350 1351 static struct lm85_data *lm85_update_device(struct device *dev) 1352 { 1353 struct i2c_client *client = to_i2c_client(dev); 1354 struct lm85_data *data = i2c_get_clientdata(client); 1355 int i; 1356 1357 down(&data->update_lock); 1358 1359 if ( !data->valid || 1360 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) { 1361 /* Things that change quickly */ 1362 dev_dbg(&client->dev, "Reading sensor values\n"); 1363 1364 /* Have to read extended bits first to "freeze" the 1365 * more significant bits that are read later. 1366 */ 1367 if ( (data->type == adm1027) || (data->type == adt7463) ) { 1368 int ext1 = lm85_read_value(client, 1369 ADM1027_REG_EXTEND_ADC1); 1370 int ext2 = lm85_read_value(client, 1371 ADM1027_REG_EXTEND_ADC2); 1372 int val = (ext1 << 8) + ext2; 1373 1374 for(i = 0; i <= 4; i++) 1375 data->in_ext[i] = (val>>(i * 2))&0x03; 1376 1377 for(i = 0; i <= 2; i++) 1378 data->temp_ext[i] = (val>>((i + 5) * 2))&0x03; 1379 } 1380 1381 /* adc_scale is 2^(number of LSBs). There are 4 extra bits in 1382 the emc6d102 and 2 in the adt7463 and adm1027. In all 1383 other chips ext is always 0 and the value of scale is 1384 irrelevant. So it is left in 4*/ 1385 data->adc_scale = (data->type == emc6d102 ) ? 16 : 4; 1386 1387 for (i = 0; i <= 4; ++i) { 1388 data->in[i] = 1389 lm85_read_value(client, LM85_REG_IN(i)); 1390 } 1391 1392 for (i = 0; i <= 3; ++i) { 1393 data->fan[i] = 1394 lm85_read_value(client, LM85_REG_FAN(i)); 1395 } 1396 1397 for (i = 0; i <= 2; ++i) { 1398 data->temp[i] = 1399 lm85_read_value(client, LM85_REG_TEMP(i)); 1400 } 1401 1402 for (i = 0; i <= 2; ++i) { 1403 data->pwm[i] = 1404 lm85_read_value(client, LM85_REG_PWM(i)); 1405 } 1406 1407 data->alarms = lm85_read_value(client, LM85_REG_ALARM1); 1408 1409 if ( data->type == adt7463 ) { 1410 if( data->therm_total < ULONG_MAX - 256 ) { 1411 data->therm_total += 1412 lm85_read_value(client, ADT7463_REG_THERM ); 1413 } 1414 } else if ( data->type == emc6d100 ) { 1415 /* Three more voltage sensors */ 1416 for (i = 5; i <= 7; ++i) { 1417 data->in[i] = 1418 lm85_read_value(client, EMC6D100_REG_IN(i)); 1419 } 1420 /* More alarm bits */ 1421 data->alarms |= 1422 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16; 1423 } else if (data->type == emc6d102 ) { 1424 /* Have to read LSB bits after the MSB ones because 1425 the reading of the MSB bits has frozen the 1426 LSBs (backward from the ADM1027). 1427 */ 1428 int ext1 = lm85_read_value(client, 1429 EMC6D102_REG_EXTEND_ADC1); 1430 int ext2 = lm85_read_value(client, 1431 EMC6D102_REG_EXTEND_ADC2); 1432 int ext3 = lm85_read_value(client, 1433 EMC6D102_REG_EXTEND_ADC3); 1434 int ext4 = lm85_read_value(client, 1435 EMC6D102_REG_EXTEND_ADC4); 1436 data->in_ext[0] = ext3 & 0x0f; 1437 data->in_ext[1] = ext4 & 0x0f; 1438 data->in_ext[2] = (ext4 >> 4) & 0x0f; 1439 data->in_ext[3] = (ext3 >> 4) & 0x0f; 1440 data->in_ext[4] = (ext2 >> 4) & 0x0f; 1441 1442 data->temp_ext[0] = ext1 & 0x0f; 1443 data->temp_ext[1] = ext2 & 0x0f; 1444 data->temp_ext[2] = (ext1 >> 4) & 0x0f; 1445 } 1446 1447 data->last_reading = jiffies ; 1448 }; /* last_reading */ 1449 1450 if ( !data->valid || 1451 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) { 1452 /* Things that don't change often */ 1453 dev_dbg(&client->dev, "Reading config values\n"); 1454 1455 for (i = 0; i <= 4; ++i) { 1456 data->in_min[i] = 1457 lm85_read_value(client, LM85_REG_IN_MIN(i)); 1458 data->in_max[i] = 1459 lm85_read_value(client, LM85_REG_IN_MAX(i)); 1460 } 1461 1462 if ( data->type == emc6d100 ) { 1463 for (i = 5; i <= 7; ++i) { 1464 data->in_min[i] = 1465 lm85_read_value(client, EMC6D100_REG_IN_MIN(i)); 1466 data->in_max[i] = 1467 lm85_read_value(client, EMC6D100_REG_IN_MAX(i)); 1468 } 1469 } 1470 1471 for (i = 0; i <= 3; ++i) { 1472 data->fan_min[i] = 1473 lm85_read_value(client, LM85_REG_FAN_MIN(i)); 1474 } 1475 1476 for (i = 0; i <= 2; ++i) { 1477 data->temp_min[i] = 1478 lm85_read_value(client, LM85_REG_TEMP_MIN(i)); 1479 data->temp_max[i] = 1480 lm85_read_value(client, LM85_REG_TEMP_MAX(i)); 1481 } 1482 1483 data->vid = lm85_read_value(client, LM85_REG_VID); 1484 1485 for (i = 0; i <= 2; ++i) { 1486 int val ; 1487 data->autofan[i].config = 1488 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i)); 1489 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i)); 1490 data->autofan[i].freq = val & 0x07 ; 1491 data->zone[i].range = (val >> 4) & 0x0f ; 1492 data->autofan[i].min_pwm = 1493 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i)); 1494 data->zone[i].limit = 1495 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i)); 1496 data->zone[i].critical = 1497 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i)); 1498 } 1499 1500 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1); 1501 data->smooth[0] = i & 0x0f ; 1502 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */ 1503 data->autofan[0].min_off = (i & 0x20) != 0 ; 1504 data->autofan[1].min_off = (i & 0x40) != 0 ; 1505 data->autofan[2].min_off = (i & 0x80) != 0 ; 1506 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2); 1507 data->smooth[1] = (i>>4) & 0x0f ; 1508 data->smooth[2] = i & 0x0f ; 1509 1510 i = lm85_read_value(client, LM85_REG_AFAN_HYST1); 1511 data->zone[0].hyst = (i>>4) & 0x0f ; 1512 data->zone[1].hyst = i & 0x0f ; 1513 1514 i = lm85_read_value(client, LM85_REG_AFAN_HYST2); 1515 data->zone[2].hyst = (i>>4) & 0x0f ; 1516 1517 if ( (data->type == lm85b) || (data->type == lm85c) ) { 1518 data->tach_mode = lm85_read_value(client, 1519 LM85_REG_TACH_MODE ); 1520 data->spinup_ctl = lm85_read_value(client, 1521 LM85_REG_SPINUP_CTL ); 1522 } else if ( (data->type == adt7463) || (data->type == adm1027) ) { 1523 if ( data->type == adt7463 ) { 1524 for (i = 0; i <= 2; ++i) { 1525 data->oppoint[i] = lm85_read_value(client, 1526 ADT7463_REG_OPPOINT(i) ); 1527 } 1528 data->tmin_ctl = lm85_read_value(client, 1529 ADT7463_REG_TMIN_CTL1 ); 1530 data->therm_limit = lm85_read_value(client, 1531 ADT7463_REG_THERM_LIMIT ); 1532 } 1533 for (i = 0; i <= 2; ++i) { 1534 data->temp_offset[i] = lm85_read_value(client, 1535 ADM1027_REG_TEMP_OFFSET(i) ); 1536 } 1537 data->tach_mode = lm85_read_value(client, 1538 ADM1027_REG_CONFIG3 ); 1539 data->fan_ppr = lm85_read_value(client, 1540 ADM1027_REG_FAN_PPR ); 1541 } 1542 1543 data->last_config = jiffies; 1544 }; /* last_config */ 1545 1546 data->valid = 1; 1547 1548 up(&data->update_lock); 1549 1550 return data; 1551 } 1552 1553 1554 static int __init sm_lm85_init(void) 1555 { 1556 return i2c_add_driver(&lm85_driver); 1557 } 1558 1559 static void __exit sm_lm85_exit(void) 1560 { 1561 i2c_del_driver(&lm85_driver); 1562 } 1563 1564 /* Thanks to Richard Barrington for adding the LM85 to sensors-detect. 1565 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with 1566 * post 2.7.0 CVS changes. 1567 */ 1568 MODULE_LICENSE("GPL"); 1569 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com"); 1570 MODULE_DESCRIPTION("LM85-B, LM85-C driver"); 1571 1572 module_init(sm_lm85_init); 1573 module_exit(sm_lm85_exit); 1574