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 * Copyright (C) 2007--2014 Jean Delvare <jdelvare@suse.de> 9 * 10 * Chip details at <http://www.national.com/ds/LM/LM85.pdf> 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 25 */ 26 27 #include <linux/module.h> 28 #include <linux/of_device.h> 29 #include <linux/init.h> 30 #include <linux/slab.h> 31 #include <linux/jiffies.h> 32 #include <linux/i2c.h> 33 #include <linux/hwmon.h> 34 #include <linux/hwmon-vid.h> 35 #include <linux/hwmon-sysfs.h> 36 #include <linux/err.h> 37 #include <linux/mutex.h> 38 #include <linux/util_macros.h> 39 40 /* Addresses to scan */ 41 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; 42 43 enum chips { 44 lm85, lm96000, 45 adm1027, adt7463, adt7468, 46 emc6d100, emc6d102, emc6d103, emc6d103s 47 }; 48 49 /* The LM85 registers */ 50 51 #define LM85_REG_IN(nr) (0x20 + (nr)) 52 #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2) 53 #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2) 54 55 #define LM85_REG_TEMP(nr) (0x25 + (nr)) 56 #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2) 57 #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2) 58 59 /* Fan speeds are LSB, MSB (2 bytes) */ 60 #define LM85_REG_FAN(nr) (0x28 + (nr) * 2) 61 #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2) 62 63 #define LM85_REG_PWM(nr) (0x30 + (nr)) 64 65 #define LM85_REG_COMPANY 0x3e 66 #define LM85_REG_VERSTEP 0x3f 67 68 #define ADT7468_REG_CFG5 0x7c 69 #define ADT7468_OFF64 (1 << 0) 70 #define ADT7468_HFPWM (1 << 1) 71 #define IS_ADT7468_OFF64(data) \ 72 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64)) 73 #define IS_ADT7468_HFPWM(data) \ 74 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM)) 75 76 /* These are the recognized values for the above regs */ 77 #define LM85_COMPANY_NATIONAL 0x01 78 #define LM85_COMPANY_ANALOG_DEV 0x41 79 #define LM85_COMPANY_SMSC 0x5c 80 #define LM85_VERSTEP_LM85C 0x60 81 #define LM85_VERSTEP_LM85B 0x62 82 #define LM85_VERSTEP_LM96000_1 0x68 83 #define LM85_VERSTEP_LM96000_2 0x69 84 #define LM85_VERSTEP_ADM1027 0x60 85 #define LM85_VERSTEP_ADT7463 0x62 86 #define LM85_VERSTEP_ADT7463C 0x6A 87 #define LM85_VERSTEP_ADT7468_1 0x71 88 #define LM85_VERSTEP_ADT7468_2 0x72 89 #define LM85_VERSTEP_EMC6D100_A0 0x60 90 #define LM85_VERSTEP_EMC6D100_A1 0x61 91 #define LM85_VERSTEP_EMC6D102 0x65 92 #define LM85_VERSTEP_EMC6D103_A0 0x68 93 #define LM85_VERSTEP_EMC6D103_A1 0x69 94 #define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */ 95 96 #define LM85_REG_CONFIG 0x40 97 98 #define LM85_REG_ALARM1 0x41 99 #define LM85_REG_ALARM2 0x42 100 101 #define LM85_REG_VID 0x43 102 103 /* Automated FAN control */ 104 #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr)) 105 #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr)) 106 #define LM85_REG_AFAN_SPIKE1 0x62 107 #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr)) 108 #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr)) 109 #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr)) 110 #define LM85_REG_AFAN_HYST1 0x6d 111 #define LM85_REG_AFAN_HYST2 0x6e 112 113 #define ADM1027_REG_EXTEND_ADC1 0x76 114 #define ADM1027_REG_EXTEND_ADC2 0x77 115 116 #define EMC6D100_REG_ALARM3 0x7d 117 /* IN5, IN6 and IN7 */ 118 #define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5)) 119 #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2) 120 #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2) 121 #define EMC6D102_REG_EXTEND_ADC1 0x85 122 #define EMC6D102_REG_EXTEND_ADC2 0x86 123 #define EMC6D102_REG_EXTEND_ADC3 0x87 124 #define EMC6D102_REG_EXTEND_ADC4 0x88 125 126 /* 127 * Conversions. Rounding and limit checking is only done on the TO_REG 128 * variants. Note that you should be a bit careful with which arguments 129 * these macros are called: arguments may be evaluated more than once. 130 */ 131 132 /* IN are scaled according to built-in resistors */ 133 static const int lm85_scaling[] = { /* .001 Volts */ 134 2500, 2250, 3300, 5000, 12000, 135 3300, 1500, 1800 /*EMC6D100*/ 136 }; 137 #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from)) 138 139 #define INS_TO_REG(n, val) \ 140 SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \ 141 lm85_scaling[n], 192) 142 143 #define INSEXT_FROM_REG(n, val, ext) \ 144 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n]) 145 146 #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n]) 147 148 /* FAN speed is measured using 90kHz clock */ 149 static inline u16 FAN_TO_REG(unsigned long val) 150 { 151 if (!val) 152 return 0xffff; 153 return clamp_val(5400000 / val, 1, 0xfffe); 154 } 155 #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \ 156 5400000 / (val)) 157 158 /* Temperature is reported in .001 degC increments */ 159 #define TEMP_TO_REG(val) \ 160 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000) 161 #define TEMPEXT_FROM_REG(val, ext) \ 162 SCALE(((val) << 4) + (ext), 16, 1000) 163 #define TEMP_FROM_REG(val) ((val) * 1000) 164 165 #define PWM_TO_REG(val) clamp_val(val, 0, 255) 166 #define PWM_FROM_REG(val) (val) 167 168 169 /* 170 * ZONEs have the following parameters: 171 * Limit (low) temp, 1. degC 172 * Hysteresis (below limit), 1. degC (0-15) 173 * Range of speed control, .1 degC (2-80) 174 * Critical (high) temp, 1. degC 175 * 176 * FAN PWMs have the following parameters: 177 * Reference Zone, 1, 2, 3, etc. 178 * Spinup time, .05 sec 179 * PWM value at limit/low temp, 1 count 180 * PWM Frequency, 1. Hz 181 * PWM is Min or OFF below limit, flag 182 * Invert PWM output, flag 183 * 184 * Some chips filter the temp, others the fan. 185 * Filter constant (or disabled) .1 seconds 186 */ 187 188 /* These are the zone temperature range encodings in .001 degree C */ 189 static const int lm85_range_map[] = { 190 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000, 191 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000 192 }; 193 194 static int RANGE_TO_REG(long range) 195 { 196 return find_closest(range, lm85_range_map, ARRAY_SIZE(lm85_range_map)); 197 } 198 #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f] 199 200 /* These are the PWM frequency encodings */ 201 static const int lm85_freq_map[] = { /* 1 Hz */ 202 10, 15, 23, 30, 38, 47, 61, 94 203 }; 204 205 static const int lm96000_freq_map[] = { /* 1 Hz */ 206 10, 15, 23, 30, 38, 47, 61, 94, 207 22500, 24000, 25700, 25700, 27700, 27700, 30000, 30000 208 }; 209 210 static const int adm1027_freq_map[] = { /* 1 Hz */ 211 11, 15, 22, 29, 35, 44, 59, 88 212 }; 213 214 static int FREQ_TO_REG(const int *map, 215 unsigned int map_size, unsigned long freq) 216 { 217 return find_closest(freq, map, map_size); 218 } 219 220 static int FREQ_FROM_REG(const int *map, unsigned int map_size, u8 reg) 221 { 222 return map[reg % map_size]; 223 } 224 225 /* 226 * Since we can't use strings, I'm abusing these numbers 227 * to stand in for the following meanings: 228 * 1 -- PWM responds to Zone 1 229 * 2 -- PWM responds to Zone 2 230 * 3 -- PWM responds to Zone 3 231 * 23 -- PWM responds to the higher temp of Zone 2 or 3 232 * 123 -- PWM responds to highest of Zone 1, 2, or 3 233 * 0 -- PWM is always at 0% (ie, off) 234 * -1 -- PWM is always at 100% 235 * -2 -- PWM responds to manual control 236 */ 237 238 static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 }; 239 #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5] 240 241 static int ZONE_TO_REG(int zone) 242 { 243 int i; 244 245 for (i = 0; i <= 7; ++i) 246 if (zone == lm85_zone_map[i]) 247 break; 248 if (i > 7) /* Not found. */ 249 i = 3; /* Always 100% */ 250 return i << 5; 251 } 252 253 #define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15) 254 #define HYST_FROM_REG(val) ((val) * 1000) 255 256 /* 257 * Chip sampling rates 258 * 259 * Some sensors are not updated more frequently than once per second 260 * so it doesn't make sense to read them more often than that. 261 * We cache the results and return the saved data if the driver 262 * is called again before a second has elapsed. 263 * 264 * Also, there is significant configuration data for this chip 265 * given the automatic PWM fan control that is possible. There 266 * are about 47 bytes of config data to only 22 bytes of actual 267 * readings. So, we keep the config data up to date in the cache 268 * when it is written and only sample it once every 1 *minute* 269 */ 270 #define LM85_DATA_INTERVAL (HZ + HZ / 2) 271 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ) 272 273 /* 274 * LM85 can automatically adjust fan speeds based on temperature 275 * This structure encapsulates an entire Zone config. There are 276 * three zones (one for each temperature input) on the lm85 277 */ 278 struct lm85_zone { 279 s8 limit; /* Low temp limit */ 280 u8 hyst; /* Low limit hysteresis. (0-15) */ 281 u8 range; /* Temp range, encoded */ 282 s8 critical; /* "All fans ON" temp limit */ 283 u8 max_desired; /* 284 * Actual "max" temperature specified. Preserved 285 * to prevent "drift" as other autofan control 286 * values change. 287 */ 288 }; 289 290 struct lm85_autofan { 291 u8 config; /* Register value */ 292 u8 min_pwm; /* Minimum PWM value, encoded */ 293 u8 min_off; /* Min PWM or OFF below "limit", flag */ 294 }; 295 296 /* 297 * For each registered chip, we need to keep some data in memory. 298 * The structure is dynamically allocated. 299 */ 300 struct lm85_data { 301 struct i2c_client *client; 302 const struct attribute_group *groups[6]; 303 const int *freq_map; 304 unsigned int freq_map_size; 305 306 enum chips type; 307 308 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */ 309 310 struct mutex update_lock; 311 int valid; /* !=0 if following fields are valid */ 312 unsigned long last_reading; /* In jiffies */ 313 unsigned long last_config; /* In jiffies */ 314 315 u8 in[8]; /* Register value */ 316 u8 in_max[8]; /* Register value */ 317 u8 in_min[8]; /* Register value */ 318 s8 temp[3]; /* Register value */ 319 s8 temp_min[3]; /* Register value */ 320 s8 temp_max[3]; /* Register value */ 321 u16 fan[4]; /* Register value */ 322 u16 fan_min[4]; /* Register value */ 323 u8 pwm[3]; /* Register value */ 324 u8 pwm_freq[3]; /* Register encoding */ 325 u8 temp_ext[3]; /* Decoded values */ 326 u8 in_ext[8]; /* Decoded values */ 327 u8 vid; /* Register value */ 328 u8 vrm; /* VRM version */ 329 u32 alarms; /* Register encoding, combined */ 330 u8 cfg5; /* Config Register 5 on ADT7468 */ 331 struct lm85_autofan autofan[3]; 332 struct lm85_zone zone[3]; 333 }; 334 335 static int lm85_read_value(struct i2c_client *client, u8 reg) 336 { 337 int res; 338 339 /* What size location is it? */ 340 switch (reg) { 341 case LM85_REG_FAN(0): /* Read WORD data */ 342 case LM85_REG_FAN(1): 343 case LM85_REG_FAN(2): 344 case LM85_REG_FAN(3): 345 case LM85_REG_FAN_MIN(0): 346 case LM85_REG_FAN_MIN(1): 347 case LM85_REG_FAN_MIN(2): 348 case LM85_REG_FAN_MIN(3): 349 case LM85_REG_ALARM1: /* Read both bytes at once */ 350 res = i2c_smbus_read_byte_data(client, reg) & 0xff; 351 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8; 352 break; 353 default: /* Read BYTE data */ 354 res = i2c_smbus_read_byte_data(client, reg); 355 break; 356 } 357 358 return res; 359 } 360 361 static void lm85_write_value(struct i2c_client *client, u8 reg, int value) 362 { 363 switch (reg) { 364 case LM85_REG_FAN(0): /* Write WORD data */ 365 case LM85_REG_FAN(1): 366 case LM85_REG_FAN(2): 367 case LM85_REG_FAN(3): 368 case LM85_REG_FAN_MIN(0): 369 case LM85_REG_FAN_MIN(1): 370 case LM85_REG_FAN_MIN(2): 371 case LM85_REG_FAN_MIN(3): 372 /* NOTE: ALARM is read only, so not included here */ 373 i2c_smbus_write_byte_data(client, reg, value & 0xff); 374 i2c_smbus_write_byte_data(client, reg + 1, value >> 8); 375 break; 376 default: /* Write BYTE data */ 377 i2c_smbus_write_byte_data(client, reg, value); 378 break; 379 } 380 } 381 382 static struct lm85_data *lm85_update_device(struct device *dev) 383 { 384 struct lm85_data *data = dev_get_drvdata(dev); 385 struct i2c_client *client = data->client; 386 int i; 387 388 mutex_lock(&data->update_lock); 389 390 if (!data->valid || 391 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) { 392 /* Things that change quickly */ 393 dev_dbg(&client->dev, "Reading sensor values\n"); 394 395 /* 396 * Have to read extended bits first to "freeze" the 397 * more significant bits that are read later. 398 * There are 2 additional resolution bits per channel and we 399 * have room for 4, so we shift them to the left. 400 */ 401 if (data->type == adm1027 || data->type == adt7463 || 402 data->type == adt7468) { 403 int ext1 = lm85_read_value(client, 404 ADM1027_REG_EXTEND_ADC1); 405 int ext2 = lm85_read_value(client, 406 ADM1027_REG_EXTEND_ADC2); 407 int val = (ext1 << 8) + ext2; 408 409 for (i = 0; i <= 4; i++) 410 data->in_ext[i] = 411 ((val >> (i * 2)) & 0x03) << 2; 412 413 for (i = 0; i <= 2; i++) 414 data->temp_ext[i] = 415 (val >> ((i + 4) * 2)) & 0x0c; 416 } 417 418 data->vid = lm85_read_value(client, LM85_REG_VID); 419 420 for (i = 0; i <= 3; ++i) { 421 data->in[i] = 422 lm85_read_value(client, LM85_REG_IN(i)); 423 data->fan[i] = 424 lm85_read_value(client, LM85_REG_FAN(i)); 425 } 426 427 if (!data->has_vid5) 428 data->in[4] = lm85_read_value(client, LM85_REG_IN(4)); 429 430 if (data->type == adt7468) 431 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5); 432 433 for (i = 0; i <= 2; ++i) { 434 data->temp[i] = 435 lm85_read_value(client, LM85_REG_TEMP(i)); 436 data->pwm[i] = 437 lm85_read_value(client, LM85_REG_PWM(i)); 438 439 if (IS_ADT7468_OFF64(data)) 440 data->temp[i] -= 64; 441 } 442 443 data->alarms = lm85_read_value(client, LM85_REG_ALARM1); 444 445 if (data->type == emc6d100) { 446 /* Three more voltage sensors */ 447 for (i = 5; i <= 7; ++i) { 448 data->in[i] = lm85_read_value(client, 449 EMC6D100_REG_IN(i)); 450 } 451 /* More alarm bits */ 452 data->alarms |= lm85_read_value(client, 453 EMC6D100_REG_ALARM3) << 16; 454 } else if (data->type == emc6d102 || data->type == emc6d103 || 455 data->type == emc6d103s) { 456 /* 457 * Have to read LSB bits after the MSB ones because 458 * the reading of the MSB bits has frozen the 459 * LSBs (backward from the ADM1027). 460 */ 461 int ext1 = lm85_read_value(client, 462 EMC6D102_REG_EXTEND_ADC1); 463 int ext2 = lm85_read_value(client, 464 EMC6D102_REG_EXTEND_ADC2); 465 int ext3 = lm85_read_value(client, 466 EMC6D102_REG_EXTEND_ADC3); 467 int ext4 = lm85_read_value(client, 468 EMC6D102_REG_EXTEND_ADC4); 469 data->in_ext[0] = ext3 & 0x0f; 470 data->in_ext[1] = ext4 & 0x0f; 471 data->in_ext[2] = ext4 >> 4; 472 data->in_ext[3] = ext3 >> 4; 473 data->in_ext[4] = ext2 >> 4; 474 475 data->temp_ext[0] = ext1 & 0x0f; 476 data->temp_ext[1] = ext2 & 0x0f; 477 data->temp_ext[2] = ext1 >> 4; 478 } 479 480 data->last_reading = jiffies; 481 } /* last_reading */ 482 483 if (!data->valid || 484 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) { 485 /* Things that don't change often */ 486 dev_dbg(&client->dev, "Reading config values\n"); 487 488 for (i = 0; i <= 3; ++i) { 489 data->in_min[i] = 490 lm85_read_value(client, LM85_REG_IN_MIN(i)); 491 data->in_max[i] = 492 lm85_read_value(client, LM85_REG_IN_MAX(i)); 493 data->fan_min[i] = 494 lm85_read_value(client, LM85_REG_FAN_MIN(i)); 495 } 496 497 if (!data->has_vid5) { 498 data->in_min[4] = lm85_read_value(client, 499 LM85_REG_IN_MIN(4)); 500 data->in_max[4] = lm85_read_value(client, 501 LM85_REG_IN_MAX(4)); 502 } 503 504 if (data->type == emc6d100) { 505 for (i = 5; i <= 7; ++i) { 506 data->in_min[i] = lm85_read_value(client, 507 EMC6D100_REG_IN_MIN(i)); 508 data->in_max[i] = lm85_read_value(client, 509 EMC6D100_REG_IN_MAX(i)); 510 } 511 } 512 513 for (i = 0; i <= 2; ++i) { 514 int val; 515 516 data->temp_min[i] = 517 lm85_read_value(client, LM85_REG_TEMP_MIN(i)); 518 data->temp_max[i] = 519 lm85_read_value(client, LM85_REG_TEMP_MAX(i)); 520 521 data->autofan[i].config = 522 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i)); 523 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i)); 524 data->pwm_freq[i] = val % data->freq_map_size; 525 data->zone[i].range = val >> 4; 526 data->autofan[i].min_pwm = 527 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i)); 528 data->zone[i].limit = 529 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i)); 530 data->zone[i].critical = 531 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i)); 532 533 if (IS_ADT7468_OFF64(data)) { 534 data->temp_min[i] -= 64; 535 data->temp_max[i] -= 64; 536 data->zone[i].limit -= 64; 537 data->zone[i].critical -= 64; 538 } 539 } 540 541 if (data->type != emc6d103s) { 542 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1); 543 data->autofan[0].min_off = (i & 0x20) != 0; 544 data->autofan[1].min_off = (i & 0x40) != 0; 545 data->autofan[2].min_off = (i & 0x80) != 0; 546 547 i = lm85_read_value(client, LM85_REG_AFAN_HYST1); 548 data->zone[0].hyst = i >> 4; 549 data->zone[1].hyst = i & 0x0f; 550 551 i = lm85_read_value(client, LM85_REG_AFAN_HYST2); 552 data->zone[2].hyst = i >> 4; 553 } 554 555 data->last_config = jiffies; 556 } /* last_config */ 557 558 data->valid = 1; 559 560 mutex_unlock(&data->update_lock); 561 562 return data; 563 } 564 565 /* 4 Fans */ 566 static ssize_t show_fan(struct device *dev, struct device_attribute *attr, 567 char *buf) 568 { 569 int nr = to_sensor_dev_attr(attr)->index; 570 struct lm85_data *data = lm85_update_device(dev); 571 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr])); 572 } 573 574 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, 575 char *buf) 576 { 577 int nr = to_sensor_dev_attr(attr)->index; 578 struct lm85_data *data = lm85_update_device(dev); 579 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr])); 580 } 581 582 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, 583 const char *buf, size_t count) 584 { 585 int nr = to_sensor_dev_attr(attr)->index; 586 struct lm85_data *data = dev_get_drvdata(dev); 587 struct i2c_client *client = data->client; 588 unsigned long val; 589 int err; 590 591 err = kstrtoul(buf, 10, &val); 592 if (err) 593 return err; 594 595 mutex_lock(&data->update_lock); 596 data->fan_min[nr] = FAN_TO_REG(val); 597 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]); 598 mutex_unlock(&data->update_lock); 599 return count; 600 } 601 602 #define show_fan_offset(offset) \ 603 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ 604 show_fan, NULL, offset - 1); \ 605 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ 606 show_fan_min, set_fan_min, offset - 1) 607 608 show_fan_offset(1); 609 show_fan_offset(2); 610 show_fan_offset(3); 611 show_fan_offset(4); 612 613 /* vid, vrm, alarms */ 614 615 static ssize_t cpu0_vid_show(struct device *dev, 616 struct device_attribute *attr, char *buf) 617 { 618 struct lm85_data *data = lm85_update_device(dev); 619 int vid; 620 621 if (data->has_vid5) { 622 /* 6-pin VID (VRM 10) */ 623 vid = vid_from_reg(data->vid & 0x3f, data->vrm); 624 } else { 625 /* 5-pin VID (VRM 9) */ 626 vid = vid_from_reg(data->vid & 0x1f, data->vrm); 627 } 628 629 return sprintf(buf, "%d\n", vid); 630 } 631 632 static DEVICE_ATTR_RO(cpu0_vid); 633 634 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr, 635 char *buf) 636 { 637 struct lm85_data *data = dev_get_drvdata(dev); 638 return sprintf(buf, "%ld\n", (long) data->vrm); 639 } 640 641 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr, 642 const char *buf, size_t count) 643 { 644 struct lm85_data *data = dev_get_drvdata(dev); 645 unsigned long val; 646 int err; 647 648 err = kstrtoul(buf, 10, &val); 649 if (err) 650 return err; 651 652 if (val > 255) 653 return -EINVAL; 654 655 data->vrm = val; 656 return count; 657 } 658 659 static DEVICE_ATTR_RW(vrm); 660 661 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr, 662 char *buf) 663 { 664 struct lm85_data *data = lm85_update_device(dev); 665 return sprintf(buf, "%u\n", data->alarms); 666 } 667 668 static DEVICE_ATTR_RO(alarms); 669 670 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, 671 char *buf) 672 { 673 int nr = to_sensor_dev_attr(attr)->index; 674 struct lm85_data *data = lm85_update_device(dev); 675 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1); 676 } 677 678 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); 679 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); 680 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); 681 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); 682 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); 683 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18); 684 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16); 685 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17); 686 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); 687 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14); 688 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); 689 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6); 690 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15); 691 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10); 692 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11); 693 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12); 694 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13); 695 696 /* pwm */ 697 698 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, 699 char *buf) 700 { 701 int nr = to_sensor_dev_attr(attr)->index; 702 struct lm85_data *data = lm85_update_device(dev); 703 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr])); 704 } 705 706 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, 707 const char *buf, size_t count) 708 { 709 int nr = to_sensor_dev_attr(attr)->index; 710 struct lm85_data *data = dev_get_drvdata(dev); 711 struct i2c_client *client = data->client; 712 unsigned long val; 713 int err; 714 715 err = kstrtoul(buf, 10, &val); 716 if (err) 717 return err; 718 719 mutex_lock(&data->update_lock); 720 data->pwm[nr] = PWM_TO_REG(val); 721 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]); 722 mutex_unlock(&data->update_lock); 723 return count; 724 } 725 726 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute 727 *attr, char *buf) 728 { 729 int nr = to_sensor_dev_attr(attr)->index; 730 struct lm85_data *data = lm85_update_device(dev); 731 int pwm_zone, enable; 732 733 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config); 734 switch (pwm_zone) { 735 case -1: /* PWM is always at 100% */ 736 enable = 0; 737 break; 738 case 0: /* PWM is always at 0% */ 739 case -2: /* PWM responds to manual control */ 740 enable = 1; 741 break; 742 default: /* PWM in automatic mode */ 743 enable = 2; 744 } 745 return sprintf(buf, "%d\n", enable); 746 } 747 748 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute 749 *attr, const char *buf, size_t count) 750 { 751 int nr = to_sensor_dev_attr(attr)->index; 752 struct lm85_data *data = dev_get_drvdata(dev); 753 struct i2c_client *client = data->client; 754 u8 config; 755 unsigned long val; 756 int err; 757 758 err = kstrtoul(buf, 10, &val); 759 if (err) 760 return err; 761 762 switch (val) { 763 case 0: 764 config = 3; 765 break; 766 case 1: 767 config = 7; 768 break; 769 case 2: 770 /* 771 * Here we have to choose arbitrarily one of the 5 possible 772 * configurations; I go for the safest 773 */ 774 config = 6; 775 break; 776 default: 777 return -EINVAL; 778 } 779 780 mutex_lock(&data->update_lock); 781 data->autofan[nr].config = lm85_read_value(client, 782 LM85_REG_AFAN_CONFIG(nr)); 783 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0) 784 | (config << 5); 785 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr), 786 data->autofan[nr].config); 787 mutex_unlock(&data->update_lock); 788 return count; 789 } 790 791 static ssize_t show_pwm_freq(struct device *dev, 792 struct device_attribute *attr, char *buf) 793 { 794 int nr = to_sensor_dev_attr(attr)->index; 795 struct lm85_data *data = lm85_update_device(dev); 796 int freq; 797 798 if (IS_ADT7468_HFPWM(data)) 799 freq = 22500; 800 else 801 freq = FREQ_FROM_REG(data->freq_map, data->freq_map_size, 802 data->pwm_freq[nr]); 803 804 return sprintf(buf, "%d\n", freq); 805 } 806 807 static ssize_t set_pwm_freq(struct device *dev, 808 struct device_attribute *attr, const char *buf, size_t count) 809 { 810 int nr = to_sensor_dev_attr(attr)->index; 811 struct lm85_data *data = dev_get_drvdata(dev); 812 struct i2c_client *client = data->client; 813 unsigned long val; 814 int err; 815 816 err = kstrtoul(buf, 10, &val); 817 if (err) 818 return err; 819 820 mutex_lock(&data->update_lock); 821 /* 822 * The ADT7468 has a special high-frequency PWM output mode, 823 * where all PWM outputs are driven by a 22.5 kHz clock. 824 * This might confuse the user, but there's not much we can do. 825 */ 826 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */ 827 data->cfg5 &= ~ADT7468_HFPWM; 828 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5); 829 } else { /* Low freq. mode */ 830 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, 831 data->freq_map_size, val); 832 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), 833 (data->zone[nr].range << 4) 834 | data->pwm_freq[nr]); 835 if (data->type == adt7468) { 836 data->cfg5 |= ADT7468_HFPWM; 837 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5); 838 } 839 } 840 mutex_unlock(&data->update_lock); 841 return count; 842 } 843 844 #define show_pwm_reg(offset) \ 845 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ 846 show_pwm, set_pwm, offset - 1); \ 847 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ 848 show_pwm_enable, set_pwm_enable, offset - 1); \ 849 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \ 850 show_pwm_freq, set_pwm_freq, offset - 1) 851 852 show_pwm_reg(1); 853 show_pwm_reg(2); 854 show_pwm_reg(3); 855 856 /* Voltages */ 857 858 static ssize_t show_in(struct device *dev, struct device_attribute *attr, 859 char *buf) 860 { 861 int nr = to_sensor_dev_attr(attr)->index; 862 struct lm85_data *data = lm85_update_device(dev); 863 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr], 864 data->in_ext[nr])); 865 } 866 867 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, 868 char *buf) 869 { 870 int nr = to_sensor_dev_attr(attr)->index; 871 struct lm85_data *data = lm85_update_device(dev); 872 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr])); 873 } 874 875 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, 876 const char *buf, size_t count) 877 { 878 int nr = to_sensor_dev_attr(attr)->index; 879 struct lm85_data *data = dev_get_drvdata(dev); 880 struct i2c_client *client = data->client; 881 long val; 882 int err; 883 884 err = kstrtol(buf, 10, &val); 885 if (err) 886 return err; 887 888 mutex_lock(&data->update_lock); 889 data->in_min[nr] = INS_TO_REG(nr, val); 890 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]); 891 mutex_unlock(&data->update_lock); 892 return count; 893 } 894 895 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, 896 char *buf) 897 { 898 int nr = to_sensor_dev_attr(attr)->index; 899 struct lm85_data *data = lm85_update_device(dev); 900 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr])); 901 } 902 903 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, 904 const char *buf, size_t count) 905 { 906 int nr = to_sensor_dev_attr(attr)->index; 907 struct lm85_data *data = dev_get_drvdata(dev); 908 struct i2c_client *client = data->client; 909 long val; 910 int err; 911 912 err = kstrtol(buf, 10, &val); 913 if (err) 914 return err; 915 916 mutex_lock(&data->update_lock); 917 data->in_max[nr] = INS_TO_REG(nr, val); 918 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]); 919 mutex_unlock(&data->update_lock); 920 return count; 921 } 922 923 #define show_in_reg(offset) \ 924 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ 925 show_in, NULL, offset); \ 926 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ 927 show_in_min, set_in_min, offset); \ 928 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ 929 show_in_max, set_in_max, offset) 930 931 show_in_reg(0); 932 show_in_reg(1); 933 show_in_reg(2); 934 show_in_reg(3); 935 show_in_reg(4); 936 show_in_reg(5); 937 show_in_reg(6); 938 show_in_reg(7); 939 940 /* Temps */ 941 942 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, 943 char *buf) 944 { 945 int nr = to_sensor_dev_attr(attr)->index; 946 struct lm85_data *data = lm85_update_device(dev); 947 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr], 948 data->temp_ext[nr])); 949 } 950 951 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, 952 char *buf) 953 { 954 int nr = to_sensor_dev_attr(attr)->index; 955 struct lm85_data *data = lm85_update_device(dev); 956 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); 957 } 958 959 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, 960 const char *buf, size_t count) 961 { 962 int nr = to_sensor_dev_attr(attr)->index; 963 struct lm85_data *data = dev_get_drvdata(dev); 964 struct i2c_client *client = data->client; 965 long val; 966 int err; 967 968 err = kstrtol(buf, 10, &val); 969 if (err) 970 return err; 971 972 if (IS_ADT7468_OFF64(data)) 973 val += 64; 974 975 mutex_lock(&data->update_lock); 976 data->temp_min[nr] = TEMP_TO_REG(val); 977 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]); 978 mutex_unlock(&data->update_lock); 979 return count; 980 } 981 982 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, 983 char *buf) 984 { 985 int nr = to_sensor_dev_attr(attr)->index; 986 struct lm85_data *data = lm85_update_device(dev); 987 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); 988 } 989 990 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, 991 const char *buf, size_t count) 992 { 993 int nr = to_sensor_dev_attr(attr)->index; 994 struct lm85_data *data = dev_get_drvdata(dev); 995 struct i2c_client *client = data->client; 996 long val; 997 int err; 998 999 err = kstrtol(buf, 10, &val); 1000 if (err) 1001 return err; 1002 1003 if (IS_ADT7468_OFF64(data)) 1004 val += 64; 1005 1006 mutex_lock(&data->update_lock); 1007 data->temp_max[nr] = TEMP_TO_REG(val); 1008 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]); 1009 mutex_unlock(&data->update_lock); 1010 return count; 1011 } 1012 1013 #define show_temp_reg(offset) \ 1014 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ 1015 show_temp, NULL, offset - 1); \ 1016 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ 1017 show_temp_min, set_temp_min, offset - 1); \ 1018 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ 1019 show_temp_max, set_temp_max, offset - 1); 1020 1021 show_temp_reg(1); 1022 show_temp_reg(2); 1023 show_temp_reg(3); 1024 1025 1026 /* Automatic PWM control */ 1027 1028 static ssize_t show_pwm_auto_channels(struct device *dev, 1029 struct device_attribute *attr, char *buf) 1030 { 1031 int nr = to_sensor_dev_attr(attr)->index; 1032 struct lm85_data *data = lm85_update_device(dev); 1033 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config)); 1034 } 1035 1036 static ssize_t set_pwm_auto_channels(struct device *dev, 1037 struct device_attribute *attr, const char *buf, size_t count) 1038 { 1039 int nr = to_sensor_dev_attr(attr)->index; 1040 struct lm85_data *data = dev_get_drvdata(dev); 1041 struct i2c_client *client = data->client; 1042 long val; 1043 int err; 1044 1045 err = kstrtol(buf, 10, &val); 1046 if (err) 1047 return err; 1048 1049 mutex_lock(&data->update_lock); 1050 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0)) 1051 | ZONE_TO_REG(val); 1052 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr), 1053 data->autofan[nr].config); 1054 mutex_unlock(&data->update_lock); 1055 return count; 1056 } 1057 1058 static ssize_t show_pwm_auto_pwm_min(struct device *dev, 1059 struct device_attribute *attr, char *buf) 1060 { 1061 int nr = to_sensor_dev_attr(attr)->index; 1062 struct lm85_data *data = lm85_update_device(dev); 1063 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm)); 1064 } 1065 1066 static ssize_t set_pwm_auto_pwm_min(struct device *dev, 1067 struct device_attribute *attr, const char *buf, size_t count) 1068 { 1069 int nr = to_sensor_dev_attr(attr)->index; 1070 struct lm85_data *data = dev_get_drvdata(dev); 1071 struct i2c_client *client = data->client; 1072 unsigned long val; 1073 int err; 1074 1075 err = kstrtoul(buf, 10, &val); 1076 if (err) 1077 return err; 1078 1079 mutex_lock(&data->update_lock); 1080 data->autofan[nr].min_pwm = PWM_TO_REG(val); 1081 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr), 1082 data->autofan[nr].min_pwm); 1083 mutex_unlock(&data->update_lock); 1084 return count; 1085 } 1086 1087 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, 1088 struct device_attribute *attr, char *buf) 1089 { 1090 int nr = to_sensor_dev_attr(attr)->index; 1091 struct lm85_data *data = lm85_update_device(dev); 1092 return sprintf(buf, "%d\n", data->autofan[nr].min_off); 1093 } 1094 1095 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, 1096 struct device_attribute *attr, const char *buf, size_t count) 1097 { 1098 int nr = to_sensor_dev_attr(attr)->index; 1099 struct lm85_data *data = dev_get_drvdata(dev); 1100 struct i2c_client *client = data->client; 1101 u8 tmp; 1102 long val; 1103 int err; 1104 1105 err = kstrtol(buf, 10, &val); 1106 if (err) 1107 return err; 1108 1109 mutex_lock(&data->update_lock); 1110 data->autofan[nr].min_off = val; 1111 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1); 1112 tmp &= ~(0x20 << nr); 1113 if (data->autofan[nr].min_off) 1114 tmp |= 0x20 << nr; 1115 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp); 1116 mutex_unlock(&data->update_lock); 1117 return count; 1118 } 1119 1120 #define pwm_auto(offset) \ 1121 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \ 1122 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \ 1123 set_pwm_auto_channels, offset - 1); \ 1124 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \ 1125 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \ 1126 set_pwm_auto_pwm_min, offset - 1); \ 1127 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \ 1128 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \ 1129 set_pwm_auto_pwm_minctl, offset - 1) 1130 1131 pwm_auto(1); 1132 pwm_auto(2); 1133 pwm_auto(3); 1134 1135 /* Temperature settings for automatic PWM control */ 1136 1137 static ssize_t show_temp_auto_temp_off(struct device *dev, 1138 struct device_attribute *attr, char *buf) 1139 { 1140 int nr = to_sensor_dev_attr(attr)->index; 1141 struct lm85_data *data = lm85_update_device(dev); 1142 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) - 1143 HYST_FROM_REG(data->zone[nr].hyst)); 1144 } 1145 1146 static ssize_t set_temp_auto_temp_off(struct device *dev, 1147 struct device_attribute *attr, const char *buf, size_t count) 1148 { 1149 int nr = to_sensor_dev_attr(attr)->index; 1150 struct lm85_data *data = dev_get_drvdata(dev); 1151 struct i2c_client *client = data->client; 1152 int min; 1153 long val; 1154 int err; 1155 1156 err = kstrtol(buf, 10, &val); 1157 if (err) 1158 return err; 1159 1160 mutex_lock(&data->update_lock); 1161 min = TEMP_FROM_REG(data->zone[nr].limit); 1162 data->zone[nr].hyst = HYST_TO_REG(min - val); 1163 if (nr == 0 || nr == 1) { 1164 lm85_write_value(client, LM85_REG_AFAN_HYST1, 1165 (data->zone[0].hyst << 4) 1166 | data->zone[1].hyst); 1167 } else { 1168 lm85_write_value(client, LM85_REG_AFAN_HYST2, 1169 (data->zone[2].hyst << 4)); 1170 } 1171 mutex_unlock(&data->update_lock); 1172 return count; 1173 } 1174 1175 static ssize_t show_temp_auto_temp_min(struct device *dev, 1176 struct device_attribute *attr, char *buf) 1177 { 1178 int nr = to_sensor_dev_attr(attr)->index; 1179 struct lm85_data *data = lm85_update_device(dev); 1180 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit)); 1181 } 1182 1183 static ssize_t set_temp_auto_temp_min(struct device *dev, 1184 struct device_attribute *attr, const char *buf, size_t count) 1185 { 1186 int nr = to_sensor_dev_attr(attr)->index; 1187 struct lm85_data *data = dev_get_drvdata(dev); 1188 struct i2c_client *client = data->client; 1189 long val; 1190 int err; 1191 1192 err = kstrtol(buf, 10, &val); 1193 if (err) 1194 return err; 1195 1196 mutex_lock(&data->update_lock); 1197 data->zone[nr].limit = TEMP_TO_REG(val); 1198 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr), 1199 data->zone[nr].limit); 1200 1201 /* Update temp_auto_max and temp_auto_range */ 1202 data->zone[nr].range = RANGE_TO_REG( 1203 TEMP_FROM_REG(data->zone[nr].max_desired) - 1204 TEMP_FROM_REG(data->zone[nr].limit)); 1205 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), 1206 ((data->zone[nr].range & 0x0f) << 4) 1207 | data->pwm_freq[nr]); 1208 1209 mutex_unlock(&data->update_lock); 1210 return count; 1211 } 1212 1213 static ssize_t show_temp_auto_temp_max(struct device *dev, 1214 struct device_attribute *attr, char *buf) 1215 { 1216 int nr = to_sensor_dev_attr(attr)->index; 1217 struct lm85_data *data = lm85_update_device(dev); 1218 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) + 1219 RANGE_FROM_REG(data->zone[nr].range)); 1220 } 1221 1222 static ssize_t set_temp_auto_temp_max(struct device *dev, 1223 struct device_attribute *attr, const char *buf, size_t count) 1224 { 1225 int nr = to_sensor_dev_attr(attr)->index; 1226 struct lm85_data *data = dev_get_drvdata(dev); 1227 struct i2c_client *client = data->client; 1228 int min; 1229 long val; 1230 int err; 1231 1232 err = kstrtol(buf, 10, &val); 1233 if (err) 1234 return err; 1235 1236 mutex_lock(&data->update_lock); 1237 min = TEMP_FROM_REG(data->zone[nr].limit); 1238 data->zone[nr].max_desired = TEMP_TO_REG(val); 1239 data->zone[nr].range = RANGE_TO_REG( 1240 val - min); 1241 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), 1242 ((data->zone[nr].range & 0x0f) << 4) 1243 | data->pwm_freq[nr]); 1244 mutex_unlock(&data->update_lock); 1245 return count; 1246 } 1247 1248 static ssize_t show_temp_auto_temp_crit(struct device *dev, 1249 struct device_attribute *attr, char *buf) 1250 { 1251 int nr = to_sensor_dev_attr(attr)->index; 1252 struct lm85_data *data = lm85_update_device(dev); 1253 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical)); 1254 } 1255 1256 static ssize_t set_temp_auto_temp_crit(struct device *dev, 1257 struct device_attribute *attr, const char *buf, size_t count) 1258 { 1259 int nr = to_sensor_dev_attr(attr)->index; 1260 struct lm85_data *data = dev_get_drvdata(dev); 1261 struct i2c_client *client = data->client; 1262 long val; 1263 int err; 1264 1265 err = kstrtol(buf, 10, &val); 1266 if (err) 1267 return err; 1268 1269 mutex_lock(&data->update_lock); 1270 data->zone[nr].critical = TEMP_TO_REG(val); 1271 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr), 1272 data->zone[nr].critical); 1273 mutex_unlock(&data->update_lock); 1274 return count; 1275 } 1276 1277 #define temp_auto(offset) \ 1278 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \ 1279 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \ 1280 set_temp_auto_temp_off, offset - 1); \ 1281 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \ 1282 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \ 1283 set_temp_auto_temp_min, offset - 1); \ 1284 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \ 1285 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \ 1286 set_temp_auto_temp_max, offset - 1); \ 1287 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \ 1288 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \ 1289 set_temp_auto_temp_crit, offset - 1); 1290 1291 temp_auto(1); 1292 temp_auto(2); 1293 temp_auto(3); 1294 1295 static struct attribute *lm85_attributes[] = { 1296 &sensor_dev_attr_fan1_input.dev_attr.attr, 1297 &sensor_dev_attr_fan2_input.dev_attr.attr, 1298 &sensor_dev_attr_fan3_input.dev_attr.attr, 1299 &sensor_dev_attr_fan4_input.dev_attr.attr, 1300 &sensor_dev_attr_fan1_min.dev_attr.attr, 1301 &sensor_dev_attr_fan2_min.dev_attr.attr, 1302 &sensor_dev_attr_fan3_min.dev_attr.attr, 1303 &sensor_dev_attr_fan4_min.dev_attr.attr, 1304 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 1305 &sensor_dev_attr_fan2_alarm.dev_attr.attr, 1306 &sensor_dev_attr_fan3_alarm.dev_attr.attr, 1307 &sensor_dev_attr_fan4_alarm.dev_attr.attr, 1308 1309 &sensor_dev_attr_pwm1.dev_attr.attr, 1310 &sensor_dev_attr_pwm2.dev_attr.attr, 1311 &sensor_dev_attr_pwm3.dev_attr.attr, 1312 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 1313 &sensor_dev_attr_pwm2_enable.dev_attr.attr, 1314 &sensor_dev_attr_pwm3_enable.dev_attr.attr, 1315 &sensor_dev_attr_pwm1_freq.dev_attr.attr, 1316 &sensor_dev_attr_pwm2_freq.dev_attr.attr, 1317 &sensor_dev_attr_pwm3_freq.dev_attr.attr, 1318 1319 &sensor_dev_attr_in0_input.dev_attr.attr, 1320 &sensor_dev_attr_in1_input.dev_attr.attr, 1321 &sensor_dev_attr_in2_input.dev_attr.attr, 1322 &sensor_dev_attr_in3_input.dev_attr.attr, 1323 &sensor_dev_attr_in0_min.dev_attr.attr, 1324 &sensor_dev_attr_in1_min.dev_attr.attr, 1325 &sensor_dev_attr_in2_min.dev_attr.attr, 1326 &sensor_dev_attr_in3_min.dev_attr.attr, 1327 &sensor_dev_attr_in0_max.dev_attr.attr, 1328 &sensor_dev_attr_in1_max.dev_attr.attr, 1329 &sensor_dev_attr_in2_max.dev_attr.attr, 1330 &sensor_dev_attr_in3_max.dev_attr.attr, 1331 &sensor_dev_attr_in0_alarm.dev_attr.attr, 1332 &sensor_dev_attr_in1_alarm.dev_attr.attr, 1333 &sensor_dev_attr_in2_alarm.dev_attr.attr, 1334 &sensor_dev_attr_in3_alarm.dev_attr.attr, 1335 1336 &sensor_dev_attr_temp1_input.dev_attr.attr, 1337 &sensor_dev_attr_temp2_input.dev_attr.attr, 1338 &sensor_dev_attr_temp3_input.dev_attr.attr, 1339 &sensor_dev_attr_temp1_min.dev_attr.attr, 1340 &sensor_dev_attr_temp2_min.dev_attr.attr, 1341 &sensor_dev_attr_temp3_min.dev_attr.attr, 1342 &sensor_dev_attr_temp1_max.dev_attr.attr, 1343 &sensor_dev_attr_temp2_max.dev_attr.attr, 1344 &sensor_dev_attr_temp3_max.dev_attr.attr, 1345 &sensor_dev_attr_temp1_alarm.dev_attr.attr, 1346 &sensor_dev_attr_temp2_alarm.dev_attr.attr, 1347 &sensor_dev_attr_temp3_alarm.dev_attr.attr, 1348 &sensor_dev_attr_temp1_fault.dev_attr.attr, 1349 &sensor_dev_attr_temp3_fault.dev_attr.attr, 1350 1351 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr, 1352 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr, 1353 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr, 1354 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr, 1355 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr, 1356 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr, 1357 1358 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr, 1359 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr, 1360 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr, 1361 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr, 1362 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr, 1363 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr, 1364 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr, 1365 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr, 1366 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr, 1367 1368 &dev_attr_vrm.attr, 1369 &dev_attr_cpu0_vid.attr, 1370 &dev_attr_alarms.attr, 1371 NULL 1372 }; 1373 1374 static const struct attribute_group lm85_group = { 1375 .attrs = lm85_attributes, 1376 }; 1377 1378 static struct attribute *lm85_attributes_minctl[] = { 1379 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr, 1380 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr, 1381 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr, 1382 NULL 1383 }; 1384 1385 static const struct attribute_group lm85_group_minctl = { 1386 .attrs = lm85_attributes_minctl, 1387 }; 1388 1389 static struct attribute *lm85_attributes_temp_off[] = { 1390 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr, 1391 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr, 1392 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr, 1393 NULL 1394 }; 1395 1396 static const struct attribute_group lm85_group_temp_off = { 1397 .attrs = lm85_attributes_temp_off, 1398 }; 1399 1400 static struct attribute *lm85_attributes_in4[] = { 1401 &sensor_dev_attr_in4_input.dev_attr.attr, 1402 &sensor_dev_attr_in4_min.dev_attr.attr, 1403 &sensor_dev_attr_in4_max.dev_attr.attr, 1404 &sensor_dev_attr_in4_alarm.dev_attr.attr, 1405 NULL 1406 }; 1407 1408 static const struct attribute_group lm85_group_in4 = { 1409 .attrs = lm85_attributes_in4, 1410 }; 1411 1412 static struct attribute *lm85_attributes_in567[] = { 1413 &sensor_dev_attr_in5_input.dev_attr.attr, 1414 &sensor_dev_attr_in6_input.dev_attr.attr, 1415 &sensor_dev_attr_in7_input.dev_attr.attr, 1416 &sensor_dev_attr_in5_min.dev_attr.attr, 1417 &sensor_dev_attr_in6_min.dev_attr.attr, 1418 &sensor_dev_attr_in7_min.dev_attr.attr, 1419 &sensor_dev_attr_in5_max.dev_attr.attr, 1420 &sensor_dev_attr_in6_max.dev_attr.attr, 1421 &sensor_dev_attr_in7_max.dev_attr.attr, 1422 &sensor_dev_attr_in5_alarm.dev_attr.attr, 1423 &sensor_dev_attr_in6_alarm.dev_attr.attr, 1424 &sensor_dev_attr_in7_alarm.dev_attr.attr, 1425 NULL 1426 }; 1427 1428 static const struct attribute_group lm85_group_in567 = { 1429 .attrs = lm85_attributes_in567, 1430 }; 1431 1432 static void lm85_init_client(struct i2c_client *client) 1433 { 1434 int value; 1435 1436 /* Start monitoring if needed */ 1437 value = lm85_read_value(client, LM85_REG_CONFIG); 1438 if (!(value & 0x01)) { 1439 dev_info(&client->dev, "Starting monitoring\n"); 1440 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01); 1441 } 1442 1443 /* Warn about unusual configuration bits */ 1444 if (value & 0x02) 1445 dev_warn(&client->dev, "Device configuration is locked\n"); 1446 if (!(value & 0x04)) 1447 dev_warn(&client->dev, "Device is not ready\n"); 1448 } 1449 1450 static int lm85_is_fake(struct i2c_client *client) 1451 { 1452 /* 1453 * Differenciate between real LM96000 and Winbond WPCD377I. The latter 1454 * emulate the former except that it has no hardware monitoring function 1455 * so the readings are always 0. 1456 */ 1457 int i; 1458 u8 in_temp, fan; 1459 1460 for (i = 0; i < 8; i++) { 1461 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i); 1462 fan = i2c_smbus_read_byte_data(client, 0x28 + i); 1463 if (in_temp != 0x00 || fan != 0xff) 1464 return 0; 1465 } 1466 1467 return 1; 1468 } 1469 1470 /* Return 0 if detection is successful, -ENODEV otherwise */ 1471 static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info) 1472 { 1473 struct i2c_adapter *adapter = client->adapter; 1474 int address = client->addr; 1475 const char *type_name = NULL; 1476 int company, verstep; 1477 1478 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 1479 /* We need to be able to do byte I/O */ 1480 return -ENODEV; 1481 } 1482 1483 /* Determine the chip type */ 1484 company = lm85_read_value(client, LM85_REG_COMPANY); 1485 verstep = lm85_read_value(client, LM85_REG_VERSTEP); 1486 1487 dev_dbg(&adapter->dev, 1488 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n", 1489 address, company, verstep); 1490 1491 if (company == LM85_COMPANY_NATIONAL) { 1492 switch (verstep) { 1493 case LM85_VERSTEP_LM85C: 1494 type_name = "lm85c"; 1495 break; 1496 case LM85_VERSTEP_LM85B: 1497 type_name = "lm85b"; 1498 break; 1499 case LM85_VERSTEP_LM96000_1: 1500 case LM85_VERSTEP_LM96000_2: 1501 /* Check for Winbond WPCD377I */ 1502 if (lm85_is_fake(client)) { 1503 dev_dbg(&adapter->dev, 1504 "Found Winbond WPCD377I, ignoring\n"); 1505 return -ENODEV; 1506 } 1507 type_name = "lm96000"; 1508 break; 1509 } 1510 } else if (company == LM85_COMPANY_ANALOG_DEV) { 1511 switch (verstep) { 1512 case LM85_VERSTEP_ADM1027: 1513 type_name = "adm1027"; 1514 break; 1515 case LM85_VERSTEP_ADT7463: 1516 case LM85_VERSTEP_ADT7463C: 1517 type_name = "adt7463"; 1518 break; 1519 case LM85_VERSTEP_ADT7468_1: 1520 case LM85_VERSTEP_ADT7468_2: 1521 type_name = "adt7468"; 1522 break; 1523 } 1524 } else if (company == LM85_COMPANY_SMSC) { 1525 switch (verstep) { 1526 case LM85_VERSTEP_EMC6D100_A0: 1527 case LM85_VERSTEP_EMC6D100_A1: 1528 /* Note: we can't tell a '100 from a '101 */ 1529 type_name = "emc6d100"; 1530 break; 1531 case LM85_VERSTEP_EMC6D102: 1532 type_name = "emc6d102"; 1533 break; 1534 case LM85_VERSTEP_EMC6D103_A0: 1535 case LM85_VERSTEP_EMC6D103_A1: 1536 type_name = "emc6d103"; 1537 break; 1538 case LM85_VERSTEP_EMC6D103S: 1539 type_name = "emc6d103s"; 1540 break; 1541 } 1542 } 1543 1544 if (!type_name) 1545 return -ENODEV; 1546 1547 strlcpy(info->type, type_name, I2C_NAME_SIZE); 1548 1549 return 0; 1550 } 1551 1552 static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id) 1553 { 1554 struct device *dev = &client->dev; 1555 struct device *hwmon_dev; 1556 struct lm85_data *data; 1557 int idx = 0; 1558 1559 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL); 1560 if (!data) 1561 return -ENOMEM; 1562 1563 data->client = client; 1564 if (client->dev.of_node) 1565 data->type = (enum chips)of_device_get_match_data(&client->dev); 1566 else 1567 data->type = id->driver_data; 1568 mutex_init(&data->update_lock); 1569 1570 /* Fill in the chip specific driver values */ 1571 switch (data->type) { 1572 case adm1027: 1573 case adt7463: 1574 case adt7468: 1575 case emc6d100: 1576 case emc6d102: 1577 case emc6d103: 1578 case emc6d103s: 1579 data->freq_map = adm1027_freq_map; 1580 data->freq_map_size = ARRAY_SIZE(adm1027_freq_map); 1581 break; 1582 case lm96000: 1583 data->freq_map = lm96000_freq_map; 1584 data->freq_map_size = ARRAY_SIZE(lm96000_freq_map); 1585 break; 1586 default: 1587 data->freq_map = lm85_freq_map; 1588 data->freq_map_size = ARRAY_SIZE(lm85_freq_map); 1589 } 1590 1591 /* Set the VRM version */ 1592 data->vrm = vid_which_vrm(); 1593 1594 /* Initialize the LM85 chip */ 1595 lm85_init_client(client); 1596 1597 /* sysfs hooks */ 1598 data->groups[idx++] = &lm85_group; 1599 1600 /* minctl and temp_off exist on all chips except emc6d103s */ 1601 if (data->type != emc6d103s) { 1602 data->groups[idx++] = &lm85_group_minctl; 1603 data->groups[idx++] = &lm85_group_temp_off; 1604 } 1605 1606 /* 1607 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used 1608 * as a sixth digital VID input rather than an analog input. 1609 */ 1610 if (data->type == adt7463 || data->type == adt7468) { 1611 u8 vid = lm85_read_value(client, LM85_REG_VID); 1612 if (vid & 0x80) 1613 data->has_vid5 = true; 1614 } 1615 1616 if (!data->has_vid5) 1617 data->groups[idx++] = &lm85_group_in4; 1618 1619 /* The EMC6D100 has 3 additional voltage inputs */ 1620 if (data->type == emc6d100) 1621 data->groups[idx++] = &lm85_group_in567; 1622 1623 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 1624 data, data->groups); 1625 return PTR_ERR_OR_ZERO(hwmon_dev); 1626 } 1627 1628 static const struct i2c_device_id lm85_id[] = { 1629 { "adm1027", adm1027 }, 1630 { "adt7463", adt7463 }, 1631 { "adt7468", adt7468 }, 1632 { "lm85", lm85 }, 1633 { "lm85b", lm85 }, 1634 { "lm85c", lm85 }, 1635 { "lm96000", lm96000 }, 1636 { "emc6d100", emc6d100 }, 1637 { "emc6d101", emc6d100 }, 1638 { "emc6d102", emc6d102 }, 1639 { "emc6d103", emc6d103 }, 1640 { "emc6d103s", emc6d103s }, 1641 { } 1642 }; 1643 MODULE_DEVICE_TABLE(i2c, lm85_id); 1644 1645 static const struct of_device_id lm85_of_match[] = { 1646 { 1647 .compatible = "adi,adm1027", 1648 .data = (void *)adm1027 1649 }, 1650 { 1651 .compatible = "adi,adt7463", 1652 .data = (void *)adt7463 1653 }, 1654 { 1655 .compatible = "adi,adt7468", 1656 .data = (void *)adt7468 1657 }, 1658 { 1659 .compatible = "national,lm85", 1660 .data = (void *)lm85 1661 }, 1662 { 1663 .compatible = "national,lm85b", 1664 .data = (void *)lm85 1665 }, 1666 { 1667 .compatible = "national,lm85c", 1668 .data = (void *)lm85 1669 }, 1670 { 1671 .compatible = "ti,lm96000", 1672 .data = (void *)lm96000 1673 }, 1674 { 1675 .compatible = "smsc,emc6d100", 1676 .data = (void *)emc6d100 1677 }, 1678 { 1679 .compatible = "smsc,emc6d101", 1680 .data = (void *)emc6d100 1681 }, 1682 { 1683 .compatible = "smsc,emc6d102", 1684 .data = (void *)emc6d102 1685 }, 1686 { 1687 .compatible = "smsc,emc6d103", 1688 .data = (void *)emc6d103 1689 }, 1690 { 1691 .compatible = "smsc,emc6d103s", 1692 .data = (void *)emc6d103s 1693 }, 1694 { }, 1695 }; 1696 MODULE_DEVICE_TABLE(of, lm85_of_match); 1697 1698 static struct i2c_driver lm85_driver = { 1699 .class = I2C_CLASS_HWMON, 1700 .driver = { 1701 .name = "lm85", 1702 .of_match_table = of_match_ptr(lm85_of_match), 1703 }, 1704 .probe = lm85_probe, 1705 .id_table = lm85_id, 1706 .detect = lm85_detect, 1707 .address_list = normal_i2c, 1708 }; 1709 1710 module_i2c_driver(lm85_driver); 1711 1712 MODULE_LICENSE("GPL"); 1713 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, " 1714 "Margit Schubert-While <margitsw@t-online.de>, " 1715 "Justin Thiessen <jthiessen@penguincomputing.com>"); 1716 MODULE_DESCRIPTION("LM85-B, LM85-C driver"); 1717