1 /* 2 * lm80.c - From lm_sensors, Linux kernel modules for hardware 3 * monitoring 4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> 5 * and Philip Edelbrock <phil@netroedge.com> 6 * 7 * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 */ 23 24 #include <linux/module.h> 25 #include <linux/init.h> 26 #include <linux/slab.h> 27 #include <linux/jiffies.h> 28 #include <linux/i2c.h> 29 #include <linux/hwmon.h> 30 #include <linux/hwmon-sysfs.h> 31 #include <linux/err.h> 32 #include <linux/mutex.h> 33 34 /* Addresses to scan */ 35 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 36 0x2e, 0x2f, I2C_CLIENT_END }; 37 38 /* Many LM80 constants specified below */ 39 40 /* The LM80 registers */ 41 #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2) 42 #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2) 43 #define LM80_REG_IN(nr) (0x20 + (nr)) 44 45 #define LM80_REG_FAN1 0x28 46 #define LM80_REG_FAN2 0x29 47 #define LM80_REG_FAN_MIN(nr) (0x3b + (nr)) 48 49 #define LM80_REG_TEMP 0x27 50 #define LM80_REG_TEMP_HOT_MAX 0x38 51 #define LM80_REG_TEMP_HOT_HYST 0x39 52 #define LM80_REG_TEMP_OS_MAX 0x3a 53 #define LM80_REG_TEMP_OS_HYST 0x3b 54 55 #define LM80_REG_CONFIG 0x00 56 #define LM80_REG_ALARM1 0x01 57 #define LM80_REG_ALARM2 0x02 58 #define LM80_REG_MASK1 0x03 59 #define LM80_REG_MASK2 0x04 60 #define LM80_REG_FANDIV 0x05 61 #define LM80_REG_RES 0x06 62 63 64 /* Conversions. Rounding and limit checking is only done on the TO_REG 65 variants. Note that you should be a bit careful with which arguments 66 these macros are called: arguments may be evaluated more than once. 67 Fixing this is just not worth it. */ 68 69 #define IN_TO_REG(val) (SENSORS_LIMIT(((val)+5)/10,0,255)) 70 #define IN_FROM_REG(val) ((val)*10) 71 72 static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div) 73 { 74 if (rpm == 0) 75 return 255; 76 rpm = SENSORS_LIMIT(rpm, 1, 1000000); 77 return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254); 78 } 79 80 #define FAN_FROM_REG(val,div) ((val)==0?-1:\ 81 (val)==255?0:1350000/((div)*(val))) 82 83 static inline long TEMP_FROM_REG(u16 temp) 84 { 85 long res; 86 87 temp >>= 4; 88 if (temp < 0x0800) 89 res = 625 * (long) temp; 90 else 91 res = ((long) temp - 0x01000) * 625; 92 93 return res / 10; 94 } 95 96 #define TEMP_LIMIT_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000) 97 98 #define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val)<0?\ 99 ((val)-500)/1000:((val)+500)/1000,0,255) 100 101 #define DIV_FROM_REG(val) (1 << (val)) 102 103 /* 104 * Client data (each client gets its own) 105 */ 106 107 struct lm80_data { 108 struct device *hwmon_dev; 109 struct mutex update_lock; 110 char valid; /* !=0 if following fields are valid */ 111 unsigned long last_updated; /* In jiffies */ 112 113 u8 in[7]; /* Register value */ 114 u8 in_max[7]; /* Register value */ 115 u8 in_min[7]; /* Register value */ 116 u8 fan[2]; /* Register value */ 117 u8 fan_min[2]; /* Register value */ 118 u8 fan_div[2]; /* Register encoding, shifted right */ 119 u16 temp; /* Register values, shifted right */ 120 u8 temp_hot_max; /* Register value */ 121 u8 temp_hot_hyst; /* Register value */ 122 u8 temp_os_max; /* Register value */ 123 u8 temp_os_hyst; /* Register value */ 124 u16 alarms; /* Register encoding, combined */ 125 }; 126 127 /* 128 * Functions declaration 129 */ 130 131 static int lm80_probe(struct i2c_client *client, 132 const struct i2c_device_id *id); 133 static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info); 134 static void lm80_init_client(struct i2c_client *client); 135 static int lm80_remove(struct i2c_client *client); 136 static struct lm80_data *lm80_update_device(struct device *dev); 137 static int lm80_read_value(struct i2c_client *client, u8 reg); 138 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); 139 140 /* 141 * Driver data (common to all clients) 142 */ 143 144 static const struct i2c_device_id lm80_id[] = { 145 { "lm80", 0 }, 146 { } 147 }; 148 MODULE_DEVICE_TABLE(i2c, lm80_id); 149 150 static struct i2c_driver lm80_driver = { 151 .class = I2C_CLASS_HWMON, 152 .driver = { 153 .name = "lm80", 154 }, 155 .probe = lm80_probe, 156 .remove = lm80_remove, 157 .id_table = lm80_id, 158 .detect = lm80_detect, 159 .address_list = normal_i2c, 160 }; 161 162 /* 163 * Sysfs stuff 164 */ 165 166 #define show_in(suffix, value) \ 167 static ssize_t show_in_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ 168 { \ 169 int nr = to_sensor_dev_attr(attr)->index; \ 170 struct lm80_data *data = lm80_update_device(dev); \ 171 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \ 172 } 173 show_in(min, in_min) 174 show_in(max, in_max) 175 show_in(input, in) 176 177 #define set_in(suffix, value, reg) \ 178 static ssize_t set_in_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \ 179 size_t count) \ 180 { \ 181 int nr = to_sensor_dev_attr(attr)->index; \ 182 struct i2c_client *client = to_i2c_client(dev); \ 183 struct lm80_data *data = i2c_get_clientdata(client); \ 184 long val = simple_strtol(buf, NULL, 10); \ 185 \ 186 mutex_lock(&data->update_lock);\ 187 data->value[nr] = IN_TO_REG(val); \ 188 lm80_write_value(client, reg(nr), data->value[nr]); \ 189 mutex_unlock(&data->update_lock);\ 190 return count; \ 191 } 192 set_in(min, in_min, LM80_REG_IN_MIN) 193 set_in(max, in_max, LM80_REG_IN_MAX) 194 195 #define show_fan(suffix, value) \ 196 static ssize_t show_fan_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ 197 { \ 198 int nr = to_sensor_dev_attr(attr)->index; \ 199 struct lm80_data *data = lm80_update_device(dev); \ 200 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \ 201 DIV_FROM_REG(data->fan_div[nr]))); \ 202 } 203 show_fan(min, fan_min) 204 show_fan(input, fan) 205 206 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, 207 char *buf) 208 { 209 int nr = to_sensor_dev_attr(attr)->index; 210 struct lm80_data *data = lm80_update_device(dev); 211 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); 212 } 213 214 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, 215 const char *buf, size_t count) 216 { 217 int nr = to_sensor_dev_attr(attr)->index; 218 struct i2c_client *client = to_i2c_client(dev); 219 struct lm80_data *data = i2c_get_clientdata(client); 220 long val = simple_strtoul(buf, NULL, 10); 221 222 mutex_lock(&data->update_lock); 223 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); 224 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]); 225 mutex_unlock(&data->update_lock); 226 return count; 227 } 228 229 /* Note: we save and restore the fan minimum here, because its value is 230 determined in part by the fan divisor. This follows the principle of 231 least surprise; the user doesn't expect the fan minimum to change just 232 because the divisor changed. */ 233 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, 234 const char *buf, size_t count) 235 { 236 int nr = to_sensor_dev_attr(attr)->index; 237 struct i2c_client *client = to_i2c_client(dev); 238 struct lm80_data *data = i2c_get_clientdata(client); 239 unsigned long min, val = simple_strtoul(buf, NULL, 10); 240 u8 reg; 241 242 /* Save fan_min */ 243 mutex_lock(&data->update_lock); 244 min = FAN_FROM_REG(data->fan_min[nr], 245 DIV_FROM_REG(data->fan_div[nr])); 246 247 switch (val) { 248 case 1: data->fan_div[nr] = 0; break; 249 case 2: data->fan_div[nr] = 1; break; 250 case 4: data->fan_div[nr] = 2; break; 251 case 8: data->fan_div[nr] = 3; break; 252 default: 253 dev_err(&client->dev, "fan_div value %ld not " 254 "supported. Choose one of 1, 2, 4 or 8!\n", val); 255 mutex_unlock(&data->update_lock); 256 return -EINVAL; 257 } 258 259 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1)))) 260 | (data->fan_div[nr] << (2 * (nr + 1))); 261 lm80_write_value(client, LM80_REG_FANDIV, reg); 262 263 /* Restore fan_min */ 264 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); 265 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]); 266 mutex_unlock(&data->update_lock); 267 268 return count; 269 } 270 271 static ssize_t show_temp_input1(struct device *dev, struct device_attribute *attr, char *buf) 272 { 273 struct lm80_data *data = lm80_update_device(dev); 274 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp)); 275 } 276 277 #define show_temp(suffix, value) \ 278 static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ 279 { \ 280 struct lm80_data *data = lm80_update_device(dev); \ 281 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \ 282 } 283 show_temp(hot_max, temp_hot_max); 284 show_temp(hot_hyst, temp_hot_hyst); 285 show_temp(os_max, temp_os_max); 286 show_temp(os_hyst, temp_os_hyst); 287 288 #define set_temp(suffix, value, reg) \ 289 static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \ 290 size_t count) \ 291 { \ 292 struct i2c_client *client = to_i2c_client(dev); \ 293 struct lm80_data *data = i2c_get_clientdata(client); \ 294 long val = simple_strtoul(buf, NULL, 10); \ 295 \ 296 mutex_lock(&data->update_lock); \ 297 data->value = TEMP_LIMIT_TO_REG(val); \ 298 lm80_write_value(client, reg, data->value); \ 299 mutex_unlock(&data->update_lock); \ 300 return count; \ 301 } 302 set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX); 303 set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST); 304 set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX); 305 set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST); 306 307 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, 308 char *buf) 309 { 310 struct lm80_data *data = lm80_update_device(dev); 311 return sprintf(buf, "%u\n", data->alarms); 312 } 313 314 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, 315 char *buf) 316 { 317 int bitnr = to_sensor_dev_attr(attr)->index; 318 struct lm80_data *data = lm80_update_device(dev); 319 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); 320 } 321 322 static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, 323 show_in_min, set_in_min, 0); 324 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, 325 show_in_min, set_in_min, 1); 326 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, 327 show_in_min, set_in_min, 2); 328 static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, 329 show_in_min, set_in_min, 3); 330 static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, 331 show_in_min, set_in_min, 4); 332 static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, 333 show_in_min, set_in_min, 5); 334 static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, 335 show_in_min, set_in_min, 6); 336 static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, 337 show_in_max, set_in_max, 0); 338 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, 339 show_in_max, set_in_max, 1); 340 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, 341 show_in_max, set_in_max, 2); 342 static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, 343 show_in_max, set_in_max, 3); 344 static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, 345 show_in_max, set_in_max, 4); 346 static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, 347 show_in_max, set_in_max, 5); 348 static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, 349 show_in_max, set_in_max, 6); 350 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0); 351 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1); 352 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2); 353 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3); 354 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4); 355 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5); 356 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6); 357 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, 358 show_fan_min, set_fan_min, 0); 359 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, 360 show_fan_min, set_fan_min, 1); 361 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0); 362 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1); 363 static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, 364 show_fan_div, set_fan_div, 0); 365 static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, 366 show_fan_div, set_fan_div, 1); 367 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL); 368 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max, 369 set_temp_hot_max); 370 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst, 371 set_temp_hot_hyst); 372 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max, 373 set_temp_os_max); 374 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst, 375 set_temp_os_hyst); 376 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); 377 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); 378 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); 379 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); 380 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); 381 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4); 382 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5); 383 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6); 384 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10); 385 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11); 386 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8); 387 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13); 388 389 /* 390 * Real code 391 */ 392 393 static struct attribute *lm80_attributes[] = { 394 &sensor_dev_attr_in0_min.dev_attr.attr, 395 &sensor_dev_attr_in1_min.dev_attr.attr, 396 &sensor_dev_attr_in2_min.dev_attr.attr, 397 &sensor_dev_attr_in3_min.dev_attr.attr, 398 &sensor_dev_attr_in4_min.dev_attr.attr, 399 &sensor_dev_attr_in5_min.dev_attr.attr, 400 &sensor_dev_attr_in6_min.dev_attr.attr, 401 &sensor_dev_attr_in0_max.dev_attr.attr, 402 &sensor_dev_attr_in1_max.dev_attr.attr, 403 &sensor_dev_attr_in2_max.dev_attr.attr, 404 &sensor_dev_attr_in3_max.dev_attr.attr, 405 &sensor_dev_attr_in4_max.dev_attr.attr, 406 &sensor_dev_attr_in5_max.dev_attr.attr, 407 &sensor_dev_attr_in6_max.dev_attr.attr, 408 &sensor_dev_attr_in0_input.dev_attr.attr, 409 &sensor_dev_attr_in1_input.dev_attr.attr, 410 &sensor_dev_attr_in2_input.dev_attr.attr, 411 &sensor_dev_attr_in3_input.dev_attr.attr, 412 &sensor_dev_attr_in4_input.dev_attr.attr, 413 &sensor_dev_attr_in5_input.dev_attr.attr, 414 &sensor_dev_attr_in6_input.dev_attr.attr, 415 &sensor_dev_attr_fan1_min.dev_attr.attr, 416 &sensor_dev_attr_fan2_min.dev_attr.attr, 417 &sensor_dev_attr_fan1_input.dev_attr.attr, 418 &sensor_dev_attr_fan2_input.dev_attr.attr, 419 &sensor_dev_attr_fan1_div.dev_attr.attr, 420 &sensor_dev_attr_fan2_div.dev_attr.attr, 421 &dev_attr_temp1_input.attr, 422 &dev_attr_temp1_max.attr, 423 &dev_attr_temp1_max_hyst.attr, 424 &dev_attr_temp1_crit.attr, 425 &dev_attr_temp1_crit_hyst.attr, 426 &dev_attr_alarms.attr, 427 &sensor_dev_attr_in0_alarm.dev_attr.attr, 428 &sensor_dev_attr_in1_alarm.dev_attr.attr, 429 &sensor_dev_attr_in2_alarm.dev_attr.attr, 430 &sensor_dev_attr_in3_alarm.dev_attr.attr, 431 &sensor_dev_attr_in4_alarm.dev_attr.attr, 432 &sensor_dev_attr_in5_alarm.dev_attr.attr, 433 &sensor_dev_attr_in6_alarm.dev_attr.attr, 434 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 435 &sensor_dev_attr_fan2_alarm.dev_attr.attr, 436 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 437 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 438 NULL 439 }; 440 441 static const struct attribute_group lm80_group = { 442 .attrs = lm80_attributes, 443 }; 444 445 /* Return 0 if detection is successful, -ENODEV otherwise */ 446 static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info) 447 { 448 struct i2c_adapter *adapter = client->adapter; 449 int i, cur; 450 451 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 452 return -ENODEV; 453 454 /* Now, we do the remaining detection. It is lousy. */ 455 if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0) 456 return -ENODEV; 457 for (i = 0x2a; i <= 0x3d; i++) { 458 cur = i2c_smbus_read_byte_data(client, i); 459 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur) 460 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur) 461 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur)) 462 return -ENODEV; 463 } 464 465 strlcpy(info->type, "lm80", I2C_NAME_SIZE); 466 467 return 0; 468 } 469 470 static int lm80_probe(struct i2c_client *client, 471 const struct i2c_device_id *id) 472 { 473 struct lm80_data *data; 474 int err; 475 476 data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL); 477 if (!data) { 478 err = -ENOMEM; 479 goto exit; 480 } 481 482 i2c_set_clientdata(client, data); 483 mutex_init(&data->update_lock); 484 485 /* Initialize the LM80 chip */ 486 lm80_init_client(client); 487 488 /* A few vars need to be filled upon startup */ 489 data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1)); 490 data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2)); 491 492 /* Register sysfs hooks */ 493 if ((err = sysfs_create_group(&client->dev.kobj, &lm80_group))) 494 goto error_free; 495 496 data->hwmon_dev = hwmon_device_register(&client->dev); 497 if (IS_ERR(data->hwmon_dev)) { 498 err = PTR_ERR(data->hwmon_dev); 499 goto error_remove; 500 } 501 502 return 0; 503 504 error_remove: 505 sysfs_remove_group(&client->dev.kobj, &lm80_group); 506 error_free: 507 kfree(data); 508 exit: 509 return err; 510 } 511 512 static int lm80_remove(struct i2c_client *client) 513 { 514 struct lm80_data *data = i2c_get_clientdata(client); 515 516 hwmon_device_unregister(data->hwmon_dev); 517 sysfs_remove_group(&client->dev.kobj, &lm80_group); 518 519 kfree(data); 520 return 0; 521 } 522 523 static int lm80_read_value(struct i2c_client *client, u8 reg) 524 { 525 return i2c_smbus_read_byte_data(client, reg); 526 } 527 528 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value) 529 { 530 return i2c_smbus_write_byte_data(client, reg, value); 531 } 532 533 /* Called when we have found a new LM80. */ 534 static void lm80_init_client(struct i2c_client *client) 535 { 536 /* Reset all except Watchdog values and last conversion values 537 This sets fan-divs to 2, among others. This makes most other 538 initializations unnecessary */ 539 lm80_write_value(client, LM80_REG_CONFIG, 0x80); 540 /* Set 11-bit temperature resolution */ 541 lm80_write_value(client, LM80_REG_RES, 0x08); 542 543 /* Start monitoring */ 544 lm80_write_value(client, LM80_REG_CONFIG, 0x01); 545 } 546 547 static struct lm80_data *lm80_update_device(struct device *dev) 548 { 549 struct i2c_client *client = to_i2c_client(dev); 550 struct lm80_data *data = i2c_get_clientdata(client); 551 int i; 552 553 mutex_lock(&data->update_lock); 554 555 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { 556 dev_dbg(&client->dev, "Starting lm80 update\n"); 557 for (i = 0; i <= 6; i++) { 558 data->in[i] = 559 lm80_read_value(client, LM80_REG_IN(i)); 560 data->in_min[i] = 561 lm80_read_value(client, LM80_REG_IN_MIN(i)); 562 data->in_max[i] = 563 lm80_read_value(client, LM80_REG_IN_MAX(i)); 564 } 565 data->fan[0] = lm80_read_value(client, LM80_REG_FAN1); 566 data->fan_min[0] = 567 lm80_read_value(client, LM80_REG_FAN_MIN(1)); 568 data->fan[1] = lm80_read_value(client, LM80_REG_FAN2); 569 data->fan_min[1] = 570 lm80_read_value(client, LM80_REG_FAN_MIN(2)); 571 572 data->temp = 573 (lm80_read_value(client, LM80_REG_TEMP) << 8) | 574 (lm80_read_value(client, LM80_REG_RES) & 0xf0); 575 data->temp_os_max = 576 lm80_read_value(client, LM80_REG_TEMP_OS_MAX); 577 data->temp_os_hyst = 578 lm80_read_value(client, LM80_REG_TEMP_OS_HYST); 579 data->temp_hot_max = 580 lm80_read_value(client, LM80_REG_TEMP_HOT_MAX); 581 data->temp_hot_hyst = 582 lm80_read_value(client, LM80_REG_TEMP_HOT_HYST); 583 584 i = lm80_read_value(client, LM80_REG_FANDIV); 585 data->fan_div[0] = (i >> 2) & 0x03; 586 data->fan_div[1] = (i >> 4) & 0x03; 587 data->alarms = lm80_read_value(client, LM80_REG_ALARM1) + 588 (lm80_read_value(client, LM80_REG_ALARM2) << 8); 589 data->last_updated = jiffies; 590 data->valid = 1; 591 } 592 593 mutex_unlock(&data->update_lock); 594 595 return data; 596 } 597 598 static int __init sensors_lm80_init(void) 599 { 600 return i2c_add_driver(&lm80_driver); 601 } 602 603 static void __exit sensors_lm80_exit(void) 604 { 605 i2c_del_driver(&lm80_driver); 606 } 607 608 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " 609 "Philip Edelbrock <phil@netroedge.com>"); 610 MODULE_DESCRIPTION("LM80 driver"); 611 MODULE_LICENSE("GPL"); 612 613 module_init(sensors_lm80_init); 614 module_exit(sensors_lm80_exit); 615