1 /* 2 gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware 3 monitoring 4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>, 5 Ky�sti M�lkki <kmalkki@cc.hut.fi> 6 Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net> 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 22 */ 23 24 #include <linux/module.h> 25 #include <linux/init.h> 26 #include <linux/slab.h> 27 #include <linux/i2c.h> 28 #include <linux/i2c-sensor.h> 29 #include <linux/i2c-vid.h> 30 31 /* Type of the extra sensor */ 32 static unsigned short extra_sensor_type; 33 module_param(extra_sensor_type, ushort, 0); 34 MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)"); 35 36 /* Addresses to scan */ 37 static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; 38 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; 39 40 /* Insmod parameters */ 41 SENSORS_INSMOD_1(gl520sm); 42 43 /* Many GL520 constants specified below 44 One of the inputs can be configured as either temp or voltage. 45 That's why _TEMP2 and _IN4 access the same register 46 */ 47 48 /* The GL520 registers */ 49 #define GL520_REG_CHIP_ID 0x00 50 #define GL520_REG_REVISION 0x01 51 #define GL520_REG_CONF 0x03 52 #define GL520_REG_MASK 0x11 53 54 #define GL520_REG_VID_INPUT 0x02 55 56 #define GL520_REG_IN0_INPUT 0x15 57 #define GL520_REG_IN0_LIMIT 0x0c 58 #define GL520_REG_IN0_MIN GL520_REG_IN0_LIMIT 59 #define GL520_REG_IN0_MAX GL520_REG_IN0_LIMIT 60 61 #define GL520_REG_IN1_INPUT 0x14 62 #define GL520_REG_IN1_LIMIT 0x09 63 #define GL520_REG_IN1_MIN GL520_REG_IN1_LIMIT 64 #define GL520_REG_IN1_MAX GL520_REG_IN1_LIMIT 65 66 #define GL520_REG_IN2_INPUT 0x13 67 #define GL520_REG_IN2_LIMIT 0x0a 68 #define GL520_REG_IN2_MIN GL520_REG_IN2_LIMIT 69 #define GL520_REG_IN2_MAX GL520_REG_IN2_LIMIT 70 71 #define GL520_REG_IN3_INPUT 0x0d 72 #define GL520_REG_IN3_LIMIT 0x0b 73 #define GL520_REG_IN3_MIN GL520_REG_IN3_LIMIT 74 #define GL520_REG_IN3_MAX GL520_REG_IN3_LIMIT 75 76 #define GL520_REG_IN4_INPUT 0x0e 77 #define GL520_REG_IN4_MAX 0x17 78 #define GL520_REG_IN4_MIN 0x18 79 80 #define GL520_REG_TEMP1_INPUT 0x04 81 #define GL520_REG_TEMP1_MAX 0x05 82 #define GL520_REG_TEMP1_MAX_HYST 0x06 83 84 #define GL520_REG_TEMP2_INPUT 0x0e 85 #define GL520_REG_TEMP2_MAX 0x17 86 #define GL520_REG_TEMP2_MAX_HYST 0x18 87 88 #define GL520_REG_FAN_INPUT 0x07 89 #define GL520_REG_FAN_MIN 0x08 90 #define GL520_REG_FAN_DIV 0x0f 91 #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV 92 93 #define GL520_REG_ALARMS 0x12 94 #define GL520_REG_BEEP_MASK 0x10 95 #define GL520_REG_BEEP_ENABLE GL520_REG_CONF 96 97 /* 98 * Function declarations 99 */ 100 101 static int gl520_attach_adapter(struct i2c_adapter *adapter); 102 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind); 103 static void gl520_init_client(struct i2c_client *client); 104 static int gl520_detach_client(struct i2c_client *client); 105 static int gl520_read_value(struct i2c_client *client, u8 reg); 106 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value); 107 static struct gl520_data *gl520_update_device(struct device *dev); 108 109 /* Driver data */ 110 static struct i2c_driver gl520_driver = { 111 .owner = THIS_MODULE, 112 .name = "gl520sm", 113 .id = I2C_DRIVERID_GL520, 114 .flags = I2C_DF_NOTIFY, 115 .attach_adapter = gl520_attach_adapter, 116 .detach_client = gl520_detach_client, 117 }; 118 119 /* Client data */ 120 struct gl520_data { 121 struct i2c_client client; 122 struct semaphore update_lock; 123 char valid; /* zero until the following fields are valid */ 124 unsigned long last_updated; /* in jiffies */ 125 126 u8 vid; 127 u8 vrm; 128 u8 in_input[5]; /* [0] = VVD */ 129 u8 in_min[5]; /* [0] = VDD */ 130 u8 in_max[5]; /* [0] = VDD */ 131 u8 fan_input[2]; 132 u8 fan_min[2]; 133 u8 fan_div[2]; 134 u8 fan_off; 135 u8 temp_input[2]; 136 u8 temp_max[2]; 137 u8 temp_max_hyst[2]; 138 u8 alarms; 139 u8 beep_enable; 140 u8 beep_mask; 141 u8 alarm_mask; 142 u8 two_temps; 143 }; 144 145 /* 146 * Sysfs stuff 147 */ 148 149 #define sysfs_r(type, n, item, reg) \ 150 static ssize_t get_##type##item (struct gl520_data *, char *, int); \ 151 static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \ 152 static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \ 153 { \ 154 struct gl520_data *data = gl520_update_device(dev); \ 155 return get_##type##item(data, buf, (n)); \ 156 } 157 158 #define sysfs_w(type, n, item, reg) \ 159 static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \ 160 static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \ 161 static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \ 162 { \ 163 struct i2c_client *client = to_i2c_client(dev); \ 164 struct gl520_data *data = i2c_get_clientdata(client); \ 165 return set_##type##item(client, data, buf, count, (n), reg); \ 166 } 167 168 #define sysfs_rw_n(type, n, item, reg) \ 169 sysfs_r(type, n, item, reg) \ 170 sysfs_w(type, n, item, reg) \ 171 static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item); 172 173 #define sysfs_ro_n(type, n, item, reg) \ 174 sysfs_r(type, n, item, reg) \ 175 static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL); 176 177 #define sysfs_rw(type, item, reg) \ 178 sysfs_r(type, 0, item, reg) \ 179 sysfs_w(type, 0, item, reg) \ 180 static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item); 181 182 #define sysfs_ro(type, item, reg) \ 183 sysfs_r(type, 0, item, reg) \ 184 static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL); 185 186 187 #define sysfs_vid(n) \ 188 sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT) 189 190 #define device_create_file_vid(client, n) \ 191 device_create_file(&client->dev, &dev_attr_cpu##n##_vid) 192 193 #define sysfs_in(n) \ 194 sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \ 195 sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \ 196 sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \ 197 198 #define device_create_file_in(client, n) \ 199 ({device_create_file(&client->dev, &dev_attr_in##n##_input); \ 200 device_create_file(&client->dev, &dev_attr_in##n##_min); \ 201 device_create_file(&client->dev, &dev_attr_in##n##_max);}) 202 203 #define sysfs_fan(n) \ 204 sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \ 205 sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \ 206 sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV) 207 208 #define device_create_file_fan(client, n) \ 209 ({device_create_file(&client->dev, &dev_attr_fan##n##_input); \ 210 device_create_file(&client->dev, &dev_attr_fan##n##_min); \ 211 device_create_file(&client->dev, &dev_attr_fan##n##_div);}) 212 213 #define sysfs_fan_off(n) \ 214 sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \ 215 216 #define device_create_file_fan_off(client, n) \ 217 device_create_file(&client->dev, &dev_attr_fan##n##_off) 218 219 #define sysfs_temp(n) \ 220 sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \ 221 sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \ 222 sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST) 223 224 #define device_create_file_temp(client, n) \ 225 ({device_create_file(&client->dev, &dev_attr_temp##n##_input); \ 226 device_create_file(&client->dev, &dev_attr_temp##n##_max); \ 227 device_create_file(&client->dev, &dev_attr_temp##n##_max_hyst);}) 228 229 #define sysfs_alarms() \ 230 sysfs_ro(alarms, , GL520_REG_ALARMS) \ 231 sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \ 232 sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK) 233 234 #define device_create_file_alarms(client) \ 235 ({device_create_file(&client->dev, &dev_attr_alarms); \ 236 device_create_file(&client->dev, &dev_attr_beep_enable); \ 237 device_create_file(&client->dev, &dev_attr_beep_mask);}) 238 239 240 sysfs_vid(0) 241 242 sysfs_in(0) 243 sysfs_in(1) 244 sysfs_in(2) 245 sysfs_in(3) 246 sysfs_in(4) 247 248 sysfs_fan(1) 249 sysfs_fan(2) 250 sysfs_fan_off(1) 251 252 sysfs_temp(1) 253 sysfs_temp(2) 254 255 sysfs_alarms() 256 257 258 static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n) 259 { 260 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm)); 261 } 262 263 #define VDD_FROM_REG(val) (((val)*95+2)/4) 264 #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255)) 265 266 #define IN_FROM_REG(val) ((val)*19) 267 #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255)) 268 269 static ssize_t get_in_input(struct gl520_data *data, char *buf, int n) 270 { 271 u8 r = data->in_input[n]; 272 273 if (n == 0) 274 return sprintf(buf, "%d\n", VDD_FROM_REG(r)); 275 else 276 return sprintf(buf, "%d\n", IN_FROM_REG(r)); 277 } 278 279 static ssize_t get_in_min(struct gl520_data *data, char *buf, int n) 280 { 281 u8 r = data->in_min[n]; 282 283 if (n == 0) 284 return sprintf(buf, "%d\n", VDD_FROM_REG(r)); 285 else 286 return sprintf(buf, "%d\n", IN_FROM_REG(r)); 287 } 288 289 static ssize_t get_in_max(struct gl520_data *data, char *buf, int n) 290 { 291 u8 r = data->in_max[n]; 292 293 if (n == 0) 294 return sprintf(buf, "%d\n", VDD_FROM_REG(r)); 295 else 296 return sprintf(buf, "%d\n", IN_FROM_REG(r)); 297 } 298 299 static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 300 { 301 long v = simple_strtol(buf, NULL, 10); 302 u8 r; 303 304 down(&data->update_lock); 305 306 if (n == 0) 307 r = VDD_TO_REG(v); 308 else 309 r = IN_TO_REG(v); 310 311 data->in_min[n] = r; 312 313 if (n < 4) 314 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r); 315 else 316 gl520_write_value(client, reg, r); 317 318 up(&data->update_lock); 319 return count; 320 } 321 322 static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 323 { 324 long v = simple_strtol(buf, NULL, 10); 325 u8 r; 326 327 if (n == 0) 328 r = VDD_TO_REG(v); 329 else 330 r = IN_TO_REG(v); 331 332 down(&data->update_lock); 333 334 data->in_max[n] = r; 335 336 if (n < 4) 337 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8)); 338 else 339 gl520_write_value(client, reg, r); 340 341 up(&data->update_lock); 342 return count; 343 } 344 345 #define DIV_FROM_REG(val) (1 << (val)) 346 #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div)))) 347 #define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255)); 348 349 static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n) 350 { 351 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1])); 352 } 353 354 static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n) 355 { 356 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1])); 357 } 358 359 static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n) 360 { 361 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1])); 362 } 363 364 static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n) 365 { 366 return sprintf(buf, "%d\n", data->fan_off); 367 } 368 369 static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 370 { 371 unsigned long v = simple_strtoul(buf, NULL, 10); 372 u8 r; 373 374 down(&data->update_lock); 375 r = FAN_TO_REG(v, data->fan_div[n - 1]); 376 data->fan_min[n - 1] = r; 377 378 if (n == 1) 379 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8)); 380 else 381 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r); 382 383 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); 384 if (data->fan_min[n - 1] == 0) 385 data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40; 386 else 387 data->alarm_mask |= (n == 1) ? 0x20 : 0x40; 388 data->beep_mask &= data->alarm_mask; 389 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); 390 391 up(&data->update_lock); 392 return count; 393 } 394 395 static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 396 { 397 unsigned long v = simple_strtoul(buf, NULL, 10); 398 u8 r; 399 400 switch (v) { 401 case 1: r = 0; break; 402 case 2: r = 1; break; 403 case 4: r = 2; break; 404 case 8: r = 3; break; 405 default: 406 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v); 407 return -EINVAL; 408 } 409 410 down(&data->update_lock); 411 data->fan_div[n - 1] = r; 412 413 if (n == 1) 414 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6)); 415 else 416 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4)); 417 418 up(&data->update_lock); 419 return count; 420 } 421 422 static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 423 { 424 u8 r = simple_strtoul(buf, NULL, 10)?1:0; 425 426 down(&data->update_lock); 427 data->fan_off = r; 428 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2)); 429 up(&data->update_lock); 430 return count; 431 } 432 433 #define TEMP_FROM_REG(val) (((val) - 130) * 1000) 434 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255)) 435 436 static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n) 437 { 438 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1])); 439 } 440 441 static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n) 442 { 443 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1])); 444 } 445 446 static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n) 447 { 448 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1])); 449 } 450 451 static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 452 { 453 long v = simple_strtol(buf, NULL, 10); 454 455 down(&data->update_lock); 456 data->temp_max[n - 1] = TEMP_TO_REG(v);; 457 gl520_write_value(client, reg, data->temp_max[n - 1]); 458 up(&data->update_lock); 459 return count; 460 } 461 462 static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 463 { 464 long v = simple_strtol(buf, NULL, 10); 465 466 down(&data->update_lock); 467 data->temp_max_hyst[n - 1] = TEMP_TO_REG(v); 468 gl520_write_value(client, reg, data->temp_max_hyst[n - 1]); 469 up(&data->update_lock); 470 return count; 471 } 472 473 static ssize_t get_alarms(struct gl520_data *data, char *buf, int n) 474 { 475 return sprintf(buf, "%d\n", data->alarms); 476 } 477 478 static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n) 479 { 480 return sprintf(buf, "%d\n", data->beep_enable); 481 } 482 483 static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n) 484 { 485 return sprintf(buf, "%d\n", data->beep_mask); 486 } 487 488 static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 489 { 490 u8 r = simple_strtoul(buf, NULL, 10)?0:1; 491 492 down(&data->update_lock); 493 data->beep_enable = !r; 494 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2)); 495 up(&data->update_lock); 496 return count; 497 } 498 499 static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) 500 { 501 u8 r = simple_strtoul(buf, NULL, 10); 502 503 down(&data->update_lock); 504 r &= data->alarm_mask; 505 data->beep_mask = r; 506 gl520_write_value(client, reg, r); 507 up(&data->update_lock); 508 return count; 509 } 510 511 512 /* 513 * Real code 514 */ 515 516 static int gl520_attach_adapter(struct i2c_adapter *adapter) 517 { 518 if (!(adapter->class & I2C_CLASS_HWMON)) 519 return 0; 520 return i2c_detect(adapter, &addr_data, gl520_detect); 521 } 522 523 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind) 524 { 525 struct i2c_client *new_client; 526 struct gl520_data *data; 527 int err = 0; 528 529 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 530 I2C_FUNC_SMBUS_WORD_DATA)) 531 goto exit; 532 533 /* OK. For now, we presume we have a valid client. We now create the 534 client structure, even though we cannot fill it completely yet. 535 But it allows us to access gl520_{read,write}_value. */ 536 537 if (!(data = kmalloc(sizeof(struct gl520_data), GFP_KERNEL))) { 538 err = -ENOMEM; 539 goto exit; 540 } 541 memset(data, 0, sizeof(struct gl520_data)); 542 543 new_client = &data->client; 544 i2c_set_clientdata(new_client, data); 545 new_client->addr = address; 546 new_client->adapter = adapter; 547 new_client->driver = &gl520_driver; 548 new_client->flags = 0; 549 550 /* Determine the chip type. */ 551 if (kind < 0) { 552 if ((gl520_read_value(new_client, GL520_REG_CHIP_ID) != 0x20) || 553 ((gl520_read_value(new_client, GL520_REG_REVISION) & 0x7f) != 0x00) || 554 ((gl520_read_value(new_client, GL520_REG_CONF) & 0x80) != 0x00)) { 555 dev_dbg(&new_client->dev, "Unknown chip type, skipping\n"); 556 goto exit_free; 557 } 558 } 559 560 /* Fill in the remaining client fields */ 561 strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE); 562 data->valid = 0; 563 init_MUTEX(&data->update_lock); 564 565 /* Tell the I2C layer a new client has arrived */ 566 if ((err = i2c_attach_client(new_client))) 567 goto exit_free; 568 569 /* Initialize the GL520SM chip */ 570 gl520_init_client(new_client); 571 572 /* Register sysfs hooks */ 573 device_create_file_vid(new_client, 0); 574 575 device_create_file_in(new_client, 0); 576 device_create_file_in(new_client, 1); 577 device_create_file_in(new_client, 2); 578 device_create_file_in(new_client, 3); 579 if (!data->two_temps) 580 device_create_file_in(new_client, 4); 581 582 device_create_file_fan(new_client, 1); 583 device_create_file_fan(new_client, 2); 584 device_create_file_fan_off(new_client, 1); 585 586 device_create_file_temp(new_client, 1); 587 if (data->two_temps) 588 device_create_file_temp(new_client, 2); 589 590 device_create_file_alarms(new_client); 591 592 return 0; 593 594 exit_free: 595 kfree(data); 596 exit: 597 return err; 598 } 599 600 601 /* Called when we have found a new GL520SM. */ 602 static void gl520_init_client(struct i2c_client *client) 603 { 604 struct gl520_data *data = i2c_get_clientdata(client); 605 u8 oldconf, conf; 606 607 conf = oldconf = gl520_read_value(client, GL520_REG_CONF); 608 609 data->alarm_mask = 0xff; 610 data->vrm = i2c_which_vrm(); 611 612 if (extra_sensor_type == 1) 613 conf &= ~0x10; 614 else if (extra_sensor_type == 2) 615 conf |= 0x10; 616 data->two_temps = !(conf & 0x10); 617 618 /* If IRQ# is disabled, we can safely force comparator mode */ 619 if (!(conf & 0x20)) 620 conf &= 0xf7; 621 622 /* Enable monitoring if needed */ 623 conf |= 0x40; 624 625 if (conf != oldconf) 626 gl520_write_value(client, GL520_REG_CONF, conf); 627 628 gl520_update_device(&(client->dev)); 629 630 if (data->fan_min[0] == 0) 631 data->alarm_mask &= ~0x20; 632 if (data->fan_min[1] == 0) 633 data->alarm_mask &= ~0x40; 634 635 data->beep_mask &= data->alarm_mask; 636 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); 637 } 638 639 static int gl520_detach_client(struct i2c_client *client) 640 { 641 int err; 642 643 if ((err = i2c_detach_client(client))) { 644 dev_err(&client->dev, "Client deregistration failed, " 645 "client not detached.\n"); 646 return err; 647 } 648 649 kfree(i2c_get_clientdata(client)); 650 return 0; 651 } 652 653 654 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized 655 GL520 uses a high-byte first convention */ 656 static int gl520_read_value(struct i2c_client *client, u8 reg) 657 { 658 if ((reg >= 0x07) && (reg <= 0x0c)) 659 return swab16(i2c_smbus_read_word_data(client, reg)); 660 else 661 return i2c_smbus_read_byte_data(client, reg); 662 } 663 664 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value) 665 { 666 if ((reg >= 0x07) && (reg <= 0x0c)) 667 return i2c_smbus_write_word_data(client, reg, swab16(value)); 668 else 669 return i2c_smbus_write_byte_data(client, reg, value); 670 } 671 672 673 static struct gl520_data *gl520_update_device(struct device *dev) 674 { 675 struct i2c_client *client = to_i2c_client(dev); 676 struct gl520_data *data = i2c_get_clientdata(client); 677 int val; 678 679 down(&data->update_lock); 680 681 if ((jiffies - data->last_updated > 2 * HZ) || 682 (jiffies < data->last_updated) || !data->valid) { 683 684 dev_dbg(&client->dev, "Starting gl520sm update\n"); 685 686 data->alarms = gl520_read_value(client, GL520_REG_ALARMS); 687 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); 688 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f; 689 690 val = gl520_read_value(client, GL520_REG_IN0_LIMIT); 691 data->in_min[0] = val & 0xff; 692 data->in_max[0] = (val >> 8) & 0xff; 693 val = gl520_read_value(client, GL520_REG_IN1_LIMIT); 694 data->in_min[1] = val & 0xff; 695 data->in_max[1] = (val >> 8) & 0xff; 696 val = gl520_read_value(client, GL520_REG_IN2_LIMIT); 697 data->in_min[2] = val & 0xff; 698 data->in_max[2] = (val >> 8) & 0xff; 699 val = gl520_read_value(client, GL520_REG_IN3_LIMIT); 700 data->in_min[3] = val & 0xff; 701 data->in_max[3] = (val >> 8) & 0xff; 702 703 val = gl520_read_value(client, GL520_REG_FAN_INPUT); 704 data->fan_input[0] = (val >> 8) & 0xff; 705 data->fan_input[1] = val & 0xff; 706 707 val = gl520_read_value(client, GL520_REG_FAN_MIN); 708 data->fan_min[0] = (val >> 8) & 0xff; 709 data->fan_min[1] = val & 0xff; 710 711 data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT); 712 data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX); 713 data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST); 714 715 val = gl520_read_value(client, GL520_REG_FAN_DIV); 716 data->fan_div[0] = (val >> 6) & 0x03; 717 data->fan_div[1] = (val >> 4) & 0x03; 718 data->fan_off = (val >> 2) & 0x01; 719 720 data->alarms &= data->alarm_mask; 721 722 val = gl520_read_value(client, GL520_REG_CONF); 723 data->beep_enable = !((val >> 2) & 1); 724 725 data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT); 726 data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT); 727 data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT); 728 data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT); 729 730 /* Temp1 and Vin4 are the same input */ 731 if (data->two_temps) { 732 data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT); 733 data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX); 734 data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST); 735 } else { 736 data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT); 737 data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN); 738 data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX); 739 } 740 741 data->last_updated = jiffies; 742 data->valid = 1; 743 } 744 745 up(&data->update_lock); 746 747 return data; 748 } 749 750 751 static int __init sensors_gl520sm_init(void) 752 { 753 return i2c_add_driver(&gl520_driver); 754 } 755 756 static void __exit sensors_gl520sm_exit(void) 757 { 758 i2c_del_driver(&gl520_driver); 759 } 760 761 762 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " 763 "Ky�sti M�lkki <kmalkki@cc.hut.fi>, " 764 "Maarten Deprez <maartendeprez@users.sourceforge.net>"); 765 MODULE_DESCRIPTION("GL520SM driver"); 766 MODULE_LICENSE("GPL"); 767 768 module_init(sensors_gl520sm_init); 769 module_exit(sensors_gl520sm_exit); 770