1 /* 2 * Device driver for the i2c thermostat found on the iBook G4, Albook G4 3 * 4 * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt 5 * 6 * Documentation from 7 * http://www.analog.com/UploadedFiles/Data_Sheets/115254175ADT7467_pra.pdf 8 * http://www.analog.com/UploadedFiles/Data_Sheets/3686221171167ADT7460_b.pdf 9 * 10 */ 11 12 #include <linux/types.h> 13 #include <linux/module.h> 14 #include <linux/errno.h> 15 #include <linux/kernel.h> 16 #include <linux/delay.h> 17 #include <linux/sched.h> 18 #include <linux/i2c.h> 19 #include <linux/slab.h> 20 #include <linux/init.h> 21 #include <linux/spinlock.h> 22 #include <linux/wait.h> 23 #include <linux/suspend.h> 24 #include <linux/kthread.h> 25 #include <linux/moduleparam.h> 26 #include <linux/freezer.h> 27 #include <linux/of_platform.h> 28 29 #include <asm/prom.h> 30 #include <asm/machdep.h> 31 #include <asm/io.h> 32 #include <asm/system.h> 33 #include <asm/sections.h> 34 35 #undef DEBUG 36 37 #define CONFIG_REG 0x40 38 #define MANUAL_MASK 0xe0 39 #define AUTO_MASK 0x20 40 #define INVERT_MASK 0x10 41 42 static u8 TEMP_REG[3] = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */ 43 static u8 LIMIT_REG[3] = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */ 44 static u8 MANUAL_MODE[2] = {0x5c, 0x5d}; 45 static u8 REM_CONTROL[2] = {0x00, 0x40}; 46 static u8 FAN_SPEED[2] = {0x28, 0x2a}; 47 static u8 FAN_SPD_SET[2] = {0x30, 0x31}; 48 49 static u8 default_limits_local[3] = {70, 50, 70}; /* local, sensor1, sensor2 */ 50 static u8 default_limits_chip[3] = {80, 65, 80}; /* local, sensor1, sensor2 */ 51 static const char *sensor_location[3]; 52 53 static int limit_adjust; 54 static int fan_speed = -1; 55 static int verbose; 56 57 MODULE_AUTHOR("Colin Leroy <colin@colino.net>"); 58 MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and " 59 "Powerbook G4 Alu"); 60 MODULE_LICENSE("GPL"); 61 62 module_param(limit_adjust, int, 0644); 63 MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) " 64 "by N degrees."); 65 66 module_param(fan_speed, int, 0644); 67 MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) " 68 "(default 64)"); 69 70 module_param(verbose, bool, 0); 71 MODULE_PARM_DESC(verbose,"Verbose log operations " 72 "(default 0)"); 73 74 struct thermostat { 75 struct i2c_client *clt; 76 u8 temps[3]; 77 u8 cached_temp[3]; 78 u8 initial_limits[3]; 79 u8 limits[3]; 80 int last_speed[2]; 81 int last_var[2]; 82 }; 83 84 static enum {ADT7460, ADT7467} therm_type; 85 static int therm_bus, therm_address; 86 static struct of_device * of_dev; 87 static struct thermostat* thermostat; 88 static struct task_struct *thread_therm = NULL; 89 90 static void write_both_fan_speed(struct thermostat *th, int speed); 91 static void write_fan_speed(struct thermostat *th, int speed, int fan); 92 93 static int 94 write_reg(struct thermostat* th, int reg, u8 data) 95 { 96 u8 tmp[2]; 97 int rc; 98 99 tmp[0] = reg; 100 tmp[1] = data; 101 rc = i2c_master_send(th->clt, (const char *)tmp, 2); 102 if (rc < 0) 103 return rc; 104 if (rc != 2) 105 return -ENODEV; 106 return 0; 107 } 108 109 static int 110 read_reg(struct thermostat* th, int reg) 111 { 112 u8 reg_addr, data; 113 int rc; 114 115 reg_addr = (u8)reg; 116 rc = i2c_master_send(th->clt, ®_addr, 1); 117 if (rc < 0) 118 return rc; 119 if (rc != 1) 120 return -ENODEV; 121 rc = i2c_master_recv(th->clt, (char *)&data, 1); 122 if (rc < 0) 123 return rc; 124 return data; 125 } 126 127 static struct i2c_driver thermostat_driver; 128 129 static int 130 attach_thermostat(struct i2c_adapter *adapter) 131 { 132 unsigned long bus_no; 133 struct i2c_board_info info; 134 struct i2c_client *client; 135 136 if (strncmp(adapter->name, "uni-n", 5)) 137 return -ENODEV; 138 bus_no = simple_strtoul(adapter->name + 6, NULL, 10); 139 if (bus_no != therm_bus) 140 return -ENODEV; 141 142 memset(&info, 0, sizeof(struct i2c_board_info)); 143 strlcpy(info.type, "therm_adt746x", I2C_NAME_SIZE); 144 info.addr = therm_address; 145 client = i2c_new_device(adapter, &info); 146 if (!client) 147 return -ENODEV; 148 149 /* 150 * Let i2c-core delete that device on driver removal. 151 * This is safe because i2c-core holds the core_lock mutex for us. 152 */ 153 list_add_tail(&client->detected, &thermostat_driver.clients); 154 return 0; 155 } 156 157 static int 158 remove_thermostat(struct i2c_client *client) 159 { 160 struct thermostat *th = i2c_get_clientdata(client); 161 int i; 162 163 if (thread_therm != NULL) { 164 kthread_stop(thread_therm); 165 } 166 167 printk(KERN_INFO "adt746x: Putting max temperatures back from " 168 "%d, %d, %d to %d, %d, %d\n", 169 th->limits[0], th->limits[1], th->limits[2], 170 th->initial_limits[0], th->initial_limits[1], 171 th->initial_limits[2]); 172 173 for (i = 0; i < 3; i++) 174 write_reg(th, LIMIT_REG[i], th->initial_limits[i]); 175 176 write_both_fan_speed(th, -1); 177 178 thermostat = NULL; 179 180 kfree(th); 181 182 return 0; 183 } 184 185 static int read_fan_speed(struct thermostat *th, u8 addr) 186 { 187 u8 tmp[2]; 188 u16 res; 189 190 /* should start with low byte */ 191 tmp[1] = read_reg(th, addr); 192 tmp[0] = read_reg(th, addr + 1); 193 194 res = tmp[1] + (tmp[0] << 8); 195 /* "a value of 0xffff means that the fan has stopped" */ 196 return (res == 0xffff ? 0 : (90000*60)/res); 197 } 198 199 static void write_both_fan_speed(struct thermostat *th, int speed) 200 { 201 write_fan_speed(th, speed, 0); 202 if (therm_type == ADT7460) 203 write_fan_speed(th, speed, 1); 204 } 205 206 static void write_fan_speed(struct thermostat *th, int speed, int fan) 207 { 208 u8 manual; 209 210 if (speed > 0xff) 211 speed = 0xff; 212 else if (speed < -1) 213 speed = 0; 214 215 if (therm_type == ADT7467 && fan == 1) 216 return; 217 218 if (th->last_speed[fan] != speed) { 219 if (verbose) { 220 if (speed == -1) 221 printk(KERN_DEBUG "adt746x: Setting speed to automatic " 222 "for %s fan.\n", sensor_location[fan+1]); 223 else 224 printk(KERN_DEBUG "adt746x: Setting speed to %d " 225 "for %s fan.\n", speed, sensor_location[fan+1]); 226 } 227 } else 228 return; 229 230 if (speed >= 0) { 231 manual = read_reg(th, MANUAL_MODE[fan]); 232 write_reg(th, MANUAL_MODE[fan], 233 (manual|MANUAL_MASK) & (~INVERT_MASK)); 234 write_reg(th, FAN_SPD_SET[fan], speed); 235 } else { 236 /* back to automatic */ 237 if(therm_type == ADT7460) { 238 manual = read_reg(th, 239 MANUAL_MODE[fan]) & (~MANUAL_MASK); 240 241 write_reg(th, 242 MANUAL_MODE[fan], manual|REM_CONTROL[fan]); 243 } else { 244 manual = read_reg(th, MANUAL_MODE[fan]); 245 write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK)); 246 } 247 } 248 249 th->last_speed[fan] = speed; 250 } 251 252 static void read_sensors(struct thermostat *th) 253 { 254 int i = 0; 255 256 for (i = 0; i < 3; i++) 257 th->temps[i] = read_reg(th, TEMP_REG[i]); 258 } 259 260 #ifdef DEBUG 261 static void display_stats(struct thermostat *th) 262 { 263 if (th->temps[0] != th->cached_temp[0] 264 || th->temps[1] != th->cached_temp[1] 265 || th->temps[2] != th->cached_temp[2]) { 266 printk(KERN_INFO "adt746x: Temperature infos:" 267 " thermostats: %d,%d,%d;" 268 " limits: %d,%d,%d;" 269 " fan speed: %d RPM\n", 270 th->temps[0], th->temps[1], th->temps[2], 271 th->limits[0], th->limits[1], th->limits[2], 272 read_fan_speed(th, FAN_SPEED[0])); 273 } 274 th->cached_temp[0] = th->temps[0]; 275 th->cached_temp[1] = th->temps[1]; 276 th->cached_temp[2] = th->temps[2]; 277 } 278 #endif 279 280 static void update_fans_speed (struct thermostat *th) 281 { 282 int lastvar = 0; /* last variation, for iBook */ 283 int i = 0; 284 285 /* we don't care about local sensor, so we start at sensor 1 */ 286 for (i = 1; i < 3; i++) { 287 int started = 0; 288 int fan_number = (therm_type == ADT7460 && i == 2); 289 int var = th->temps[i] - th->limits[i]; 290 291 if (var > -1) { 292 int step = (255 - fan_speed) / 7; 293 int new_speed = 0; 294 295 /* hysteresis : change fan speed only if variation is 296 * more than two degrees */ 297 if (abs(var - th->last_var[fan_number]) < 2) 298 continue; 299 300 started = 1; 301 new_speed = fan_speed + ((var-1)*step); 302 303 if (new_speed < fan_speed) 304 new_speed = fan_speed; 305 if (new_speed > 255) 306 new_speed = 255; 307 308 if (verbose) 309 printk(KERN_DEBUG "adt746x: Setting fans speed to %d " 310 "(limit exceeded by %d on %s) \n", 311 new_speed, var, 312 sensor_location[fan_number+1]); 313 write_both_fan_speed(th, new_speed); 314 th->last_var[fan_number] = var; 315 } else if (var < -2) { 316 /* don't stop fan if sensor2 is cold and sensor1 is not 317 * so cold (lastvar >= -1) */ 318 if (i == 2 && lastvar < -1) { 319 if (th->last_speed[fan_number] != 0) 320 if (verbose) 321 printk(KERN_DEBUG "adt746x: Stopping " 322 "fans.\n"); 323 write_both_fan_speed(th, 0); 324 } 325 } 326 327 lastvar = var; 328 329 if (started) 330 return; /* we don't want to re-stop the fan 331 * if sensor1 is heating and sensor2 is not */ 332 } 333 } 334 335 static int monitor_task(void *arg) 336 { 337 struct thermostat* th = arg; 338 339 set_freezable(); 340 while(!kthread_should_stop()) { 341 try_to_freeze(); 342 msleep_interruptible(2000); 343 344 #ifndef DEBUG 345 if (fan_speed != -1) 346 read_sensors(th); 347 #else 348 read_sensors(th); 349 #endif 350 351 if (fan_speed != -1) 352 update_fans_speed(th); 353 354 #ifdef DEBUG 355 display_stats(th); 356 #endif 357 358 } 359 360 return 0; 361 } 362 363 static void set_limit(struct thermostat *th, int i) 364 { 365 /* Set sensor1 limit higher to avoid powerdowns */ 366 th->limits[i] = default_limits_chip[i] + limit_adjust; 367 write_reg(th, LIMIT_REG[i], th->limits[i]); 368 369 /* set our limits to normal */ 370 th->limits[i] = default_limits_local[i] + limit_adjust; 371 } 372 373 static int probe_thermostat(struct i2c_client *client, 374 const struct i2c_device_id *id) 375 { 376 struct thermostat* th; 377 int rc; 378 int i; 379 380 if (thermostat) 381 return 0; 382 383 th = kzalloc(sizeof(struct thermostat), GFP_KERNEL); 384 if (!th) 385 return -ENOMEM; 386 387 i2c_set_clientdata(client, th); 388 th->clt = client; 389 390 rc = read_reg(th, 0); 391 if (rc < 0) { 392 dev_err(&client->dev, "Thermostat failed to read config!\n"); 393 kfree(th); 394 return -ENODEV; 395 } 396 397 /* force manual control to start the fan quieter */ 398 if (fan_speed == -1) 399 fan_speed = 64; 400 401 if(therm_type == ADT7460) { 402 printk(KERN_INFO "adt746x: ADT7460 initializing\n"); 403 /* The 7460 needs to be started explicitly */ 404 write_reg(th, CONFIG_REG, 1); 405 } else 406 printk(KERN_INFO "adt746x: ADT7467 initializing\n"); 407 408 for (i = 0; i < 3; i++) { 409 th->initial_limits[i] = read_reg(th, LIMIT_REG[i]); 410 set_limit(th, i); 411 } 412 413 printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d" 414 " to %d, %d, %d\n", 415 th->initial_limits[0], th->initial_limits[1], 416 th->initial_limits[2], th->limits[0], th->limits[1], 417 th->limits[2]); 418 419 thermostat = th; 420 421 /* be sure to really write fan speed the first time */ 422 th->last_speed[0] = -2; 423 th->last_speed[1] = -2; 424 th->last_var[0] = -80; 425 th->last_var[1] = -80; 426 427 if (fan_speed != -1) { 428 /* manual mode, stop fans */ 429 write_both_fan_speed(th, 0); 430 } else { 431 /* automatic mode */ 432 write_both_fan_speed(th, -1); 433 } 434 435 thread_therm = kthread_run(monitor_task, th, "kfand"); 436 437 if (thread_therm == ERR_PTR(-ENOMEM)) { 438 printk(KERN_INFO "adt746x: Kthread creation failed\n"); 439 thread_therm = NULL; 440 return -ENOMEM; 441 } 442 443 return 0; 444 } 445 446 static const struct i2c_device_id therm_adt746x_id[] = { 447 { "therm_adt746x", 0 }, 448 { } 449 }; 450 451 static struct i2c_driver thermostat_driver = { 452 .driver = { 453 .name = "therm_adt746x", 454 }, 455 .attach_adapter = attach_thermostat, 456 .probe = probe_thermostat, 457 .remove = remove_thermostat, 458 .id_table = therm_adt746x_id, 459 }; 460 461 /* 462 * Now, unfortunately, sysfs doesn't give us a nice void * we could 463 * pass around to the attribute functions, so we don't really have 464 * choice but implement a bunch of them... 465 * 466 * FIXME, it does now... 467 */ 468 #define BUILD_SHOW_FUNC_INT(name, data) \ 469 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \ 470 { \ 471 return sprintf(buf, "%d\n", data); \ 472 } 473 474 #define BUILD_SHOW_FUNC_STR(name, data) \ 475 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \ 476 { \ 477 return sprintf(buf, "%s\n", data); \ 478 } 479 480 #define BUILD_SHOW_FUNC_FAN(name, data) \ 481 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \ 482 { \ 483 return sprintf(buf, "%d (%d rpm)\n", \ 484 thermostat->last_speed[data], \ 485 read_fan_speed(thermostat, FAN_SPEED[data]) \ 486 ); \ 487 } 488 489 #define BUILD_STORE_FUNC_DEG(name, data) \ 490 static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \ 491 { \ 492 int val; \ 493 int i; \ 494 val = simple_strtol(buf, NULL, 10); \ 495 printk(KERN_INFO "Adjusting limits by %d degrees\n", val); \ 496 limit_adjust = val; \ 497 for (i=0; i < 3; i++) \ 498 set_limit(thermostat, i); \ 499 return n; \ 500 } 501 502 #define BUILD_STORE_FUNC_INT(name, data) \ 503 static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \ 504 { \ 505 int val; \ 506 val = simple_strtol(buf, NULL, 10); \ 507 if (val < 0 || val > 255) \ 508 return -EINVAL; \ 509 printk(KERN_INFO "Setting specified fan speed to %d\n", val); \ 510 data = val; \ 511 return n; \ 512 } 513 514 BUILD_SHOW_FUNC_INT(sensor1_temperature, (read_reg(thermostat, TEMP_REG[1]))) 515 BUILD_SHOW_FUNC_INT(sensor2_temperature, (read_reg(thermostat, TEMP_REG[2]))) 516 BUILD_SHOW_FUNC_INT(sensor1_limit, thermostat->limits[1]) 517 BUILD_SHOW_FUNC_INT(sensor2_limit, thermostat->limits[2]) 518 BUILD_SHOW_FUNC_STR(sensor1_location, sensor_location[1]) 519 BUILD_SHOW_FUNC_STR(sensor2_location, sensor_location[2]) 520 521 BUILD_SHOW_FUNC_INT(specified_fan_speed, fan_speed) 522 BUILD_SHOW_FUNC_FAN(sensor1_fan_speed, 0) 523 BUILD_SHOW_FUNC_FAN(sensor2_fan_speed, 1) 524 525 BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed) 526 BUILD_SHOW_FUNC_INT(limit_adjust, limit_adjust) 527 BUILD_STORE_FUNC_DEG(limit_adjust, thermostat) 528 529 static DEVICE_ATTR(sensor1_temperature, S_IRUGO, 530 show_sensor1_temperature,NULL); 531 static DEVICE_ATTR(sensor2_temperature, S_IRUGO, 532 show_sensor2_temperature,NULL); 533 static DEVICE_ATTR(sensor1_limit, S_IRUGO, 534 show_sensor1_limit, NULL); 535 static DEVICE_ATTR(sensor2_limit, S_IRUGO, 536 show_sensor2_limit, NULL); 537 static DEVICE_ATTR(sensor1_location, S_IRUGO, 538 show_sensor1_location, NULL); 539 static DEVICE_ATTR(sensor2_location, S_IRUGO, 540 show_sensor2_location, NULL); 541 542 static DEVICE_ATTR(specified_fan_speed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 543 show_specified_fan_speed,store_specified_fan_speed); 544 545 static DEVICE_ATTR(sensor1_fan_speed, S_IRUGO, 546 show_sensor1_fan_speed, NULL); 547 static DEVICE_ATTR(sensor2_fan_speed, S_IRUGO, 548 show_sensor2_fan_speed, NULL); 549 550 static DEVICE_ATTR(limit_adjust, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 551 show_limit_adjust, store_limit_adjust); 552 553 554 static int __init 555 thermostat_init(void) 556 { 557 struct device_node* np; 558 const u32 *prop; 559 int i = 0, offset = 0; 560 int err; 561 562 np = of_find_node_by_name(NULL, "fan"); 563 if (!np) 564 return -ENODEV; 565 if (of_device_is_compatible(np, "adt7460")) 566 therm_type = ADT7460; 567 else if (of_device_is_compatible(np, "adt7467")) 568 therm_type = ADT7467; 569 else { 570 of_node_put(np); 571 return -ENODEV; 572 } 573 574 prop = of_get_property(np, "hwsensor-params-version", NULL); 575 printk(KERN_INFO "adt746x: version %d (%ssupported)\n", *prop, 576 (*prop == 1)?"":"un"); 577 if (*prop != 1) { 578 of_node_put(np); 579 return -ENODEV; 580 } 581 582 prop = of_get_property(np, "reg", NULL); 583 if (!prop) { 584 of_node_put(np); 585 return -ENODEV; 586 } 587 588 /* look for bus either by path or using "reg" */ 589 if (strstr(np->full_name, "/i2c-bus@") != NULL) { 590 const char *tmp_bus = (strstr(np->full_name, "/i2c-bus@") + 9); 591 therm_bus = tmp_bus[0]-'0'; 592 } else { 593 therm_bus = ((*prop) >> 8) & 0x0f; 594 } 595 596 therm_address = ((*prop) & 0xff) >> 1; 597 598 printk(KERN_INFO "adt746x: Thermostat bus: %d, address: 0x%02x, " 599 "limit_adjust: %d, fan_speed: %d\n", 600 therm_bus, therm_address, limit_adjust, fan_speed); 601 602 if (of_get_property(np, "hwsensor-location", NULL)) { 603 for (i = 0; i < 3; i++) { 604 sensor_location[i] = of_get_property(np, 605 "hwsensor-location", NULL) + offset; 606 607 if (sensor_location[i] == NULL) 608 sensor_location[i] = ""; 609 610 printk(KERN_INFO "sensor %d: %s\n", i, sensor_location[i]); 611 offset += strlen(sensor_location[i]) + 1; 612 } 613 } else { 614 sensor_location[0] = "?"; 615 sensor_location[1] = "?"; 616 sensor_location[2] = "?"; 617 } 618 619 of_dev = of_platform_device_create(np, "temperatures", NULL); 620 of_node_put(np); 621 622 if (of_dev == NULL) { 623 printk(KERN_ERR "Can't register temperatures device !\n"); 624 return -ENODEV; 625 } 626 627 err = device_create_file(&of_dev->dev, &dev_attr_sensor1_temperature); 628 err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_temperature); 629 err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_limit); 630 err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_limit); 631 err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_location); 632 err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_location); 633 err |= device_create_file(&of_dev->dev, &dev_attr_limit_adjust); 634 err |= device_create_file(&of_dev->dev, &dev_attr_specified_fan_speed); 635 err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_fan_speed); 636 if(therm_type == ADT7460) 637 err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_fan_speed); 638 if (err) 639 printk(KERN_WARNING 640 "Failed to create tempertaure attribute file(s).\n"); 641 642 #ifndef CONFIG_I2C_POWERMAC 643 request_module("i2c-powermac"); 644 #endif 645 646 return i2c_add_driver(&thermostat_driver); 647 } 648 649 static void __exit 650 thermostat_exit(void) 651 { 652 if (of_dev) { 653 device_remove_file(&of_dev->dev, &dev_attr_sensor1_temperature); 654 device_remove_file(&of_dev->dev, &dev_attr_sensor2_temperature); 655 device_remove_file(&of_dev->dev, &dev_attr_sensor1_limit); 656 device_remove_file(&of_dev->dev, &dev_attr_sensor2_limit); 657 device_remove_file(&of_dev->dev, &dev_attr_sensor1_location); 658 device_remove_file(&of_dev->dev, &dev_attr_sensor2_location); 659 device_remove_file(&of_dev->dev, &dev_attr_limit_adjust); 660 device_remove_file(&of_dev->dev, &dev_attr_specified_fan_speed); 661 device_remove_file(&of_dev->dev, &dev_attr_sensor1_fan_speed); 662 663 if(therm_type == ADT7460) 664 device_remove_file(&of_dev->dev, 665 &dev_attr_sensor2_fan_speed); 666 667 of_device_unregister(of_dev); 668 } 669 i2c_del_driver(&thermostat_driver); 670 } 671 672 module_init(thermostat_init); 673 module_exit(thermostat_exit); 674