therm_adt746x.c 16.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5
/*
 * Device driver for the i2c thermostat found on the iBook G4, Albook G4
 *
 * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt
 *
6 7 8
 * Documentation from 115254175ADT7467_pra.pdf and 3686221171167ADT7460_b.pdf
 * http://www.onsemi.com/PowerSolutions/product.do?id=ADT7467
 * http://www.onsemi.com/PowerSolutions/product.do?id=ADT7460
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 */

#include <linux/types.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/wait.h>
#include <linux/suspend.h>
#include <linux/kthread.h>
#include <linux/moduleparam.h>
26
#include <linux/freezer.h>
27
#include <linux/of_platform.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30 31 32 33 34 35 36 37 38

#include <asm/prom.h>
#include <asm/machdep.h>
#include <asm/io.h>
#include <asm/sections.h>

#undef DEBUG

#define CONFIG_REG   0x40
#define MANUAL_MASK  0xe0
#define AUTO_MASK    0x20
39
#define INVERT_MASK  0x10
Linus Torvalds's avatar
Linus Torvalds committed
40

41 42
static u8 TEMP_REG[3]    = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */
static u8 LIMIT_REG[3]   = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */
Linus Torvalds's avatar
Linus Torvalds committed
43 44 45 46 47
static u8 MANUAL_MODE[2] = {0x5c, 0x5d};       
static u8 REM_CONTROL[2] = {0x00, 0x40};
static u8 FAN_SPEED[2]   = {0x28, 0x2a};
static u8 FAN_SPD_SET[2] = {0x30, 0x31};

48 49
static u8 default_limits_local[3] = {70, 50, 70};    /* local, sensor1, sensor2 */
static u8 default_limits_chip[3] = {80, 65, 80};    /* local, sensor1, sensor2 */
50
static const char *sensor_location[3] = { "?", "?", "?" };
Linus Torvalds's avatar
Linus Torvalds committed
51

52
static int limit_adjust;
Linus Torvalds's avatar
Linus Torvalds committed
53
static int fan_speed = -1;
54
static bool verbose;
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59 60 61

MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "
		   "Powerbook G4 Alu");
MODULE_LICENSE("GPL");

module_param(limit_adjust, int, 0644);
62
MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
Linus Torvalds's avatar
Linus Torvalds committed
63 64 65 66 67 68
		 "by N degrees.");

module_param(fan_speed, int, 0644);
MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) "
		 "(default 64)");

69 70 71 72
module_param(verbose, bool, 0);
MODULE_PARM_DESC(verbose,"Verbose log operations "
		 "(default 0)");

Linus Torvalds's avatar
Linus Torvalds committed
73
struct thermostat {
74
	struct i2c_client	*clt;
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78 79 80
	u8			temps[3];
	u8			cached_temp[3];
	u8			initial_limits[3];
	u8			limits[3];
	int			last_speed[2];
	int			last_var[2];
81
	int			pwm_inv[2];
82 83 84 85 86 87
	struct task_struct	*thread;
	struct platform_device	*pdev;
	enum {
		ADT7460,
		ADT7467
	}			type;
Linus Torvalds's avatar
Linus Torvalds committed
88 89 90 91 92 93 94 95 96 97 98 99 100
};

static void write_both_fan_speed(struct thermostat *th, int speed);
static void write_fan_speed(struct thermostat *th, int speed, int fan);

static int
write_reg(struct thermostat* th, int reg, u8 data)
{
	u8 tmp[2];
	int rc;
	
	tmp[0] = reg;
	tmp[1] = data;
101
	rc = i2c_master_send(th->clt, (const char *)tmp, 2);
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
	if (rc < 0)
		return rc;
	if (rc != 2)
		return -ENODEV;
	return 0;
}

static int
read_reg(struct thermostat* th, int reg)
{
	u8 reg_addr, data;
	int rc;

	reg_addr = (u8)reg;
116
	rc = i2c_master_send(th->clt, &reg_addr, 1);
Linus Torvalds's avatar
Linus Torvalds committed
117 118 119 120
	if (rc < 0)
		return rc;
	if (rc != 1)
		return -ENODEV;
121
	rc = i2c_master_recv(th->clt, (char *)&data, 1);
Linus Torvalds's avatar
Linus Torvalds committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	if (rc < 0)
		return rc;
	return data;
}

static int read_fan_speed(struct thermostat *th, u8 addr)
{
	u8 tmp[2];
	u16 res;
	
	/* should start with low byte */
	tmp[1] = read_reg(th, addr);
	tmp[0] = read_reg(th, addr + 1);
	
	res = tmp[1] + (tmp[0] << 8);
	/* "a value of 0xffff means that the fan has stopped" */
	return (res == 0xffff ? 0 : (90000*60)/res);
}

static void write_both_fan_speed(struct thermostat *th, int speed)
{
	write_fan_speed(th, speed, 0);
144
	if (th->type == ADT7460)
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147 148 149 150 151 152 153 154 155 156
		write_fan_speed(th, speed, 1);
}

static void write_fan_speed(struct thermostat *th, int speed, int fan)
{
	u8 manual;
	
	if (speed > 0xff) 
		speed = 0xff;
	else if (speed < -1) 
		speed = 0;
	
157
	if (th->type == ADT7467 && fan == 1)
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160
		return;
	
	if (th->last_speed[fan] != speed) {
161 162 163 164 165 166 167 168
		if (verbose) {
			if (speed == -1)
				printk(KERN_DEBUG "adt746x: Setting speed to automatic "
					"for %s fan.\n", sensor_location[fan+1]);
			else
				printk(KERN_DEBUG "adt746x: Setting speed to %d "
					"for %s fan.\n", speed, sensor_location[fan+1]);
		}
Linus Torvalds's avatar
Linus Torvalds committed
169 170 171 172 173
	} else
		return;
	
	if (speed >= 0) {
		manual = read_reg(th, MANUAL_MODE[fan]);
174
		manual &= ~INVERT_MASK;
175
		write_reg(th, MANUAL_MODE[fan],
176
			manual | MANUAL_MASK | th->pwm_inv[fan]);
Linus Torvalds's avatar
Linus Torvalds committed
177 178 179
		write_reg(th, FAN_SPD_SET[fan], speed);
	} else {
		/* back to automatic */
180
		if(th->type == ADT7460) {
Linus Torvalds's avatar
Linus Torvalds committed
181 182
			manual = read_reg(th,
				MANUAL_MODE[fan]) & (~MANUAL_MASK);
183 184
			manual &= ~INVERT_MASK;
			manual |= th->pwm_inv[fan];
Linus Torvalds's avatar
Linus Torvalds committed
185 186 187 188
			write_reg(th,
				MANUAL_MODE[fan], manual|REM_CONTROL[fan]);
		} else {
			manual = read_reg(th, MANUAL_MODE[fan]);
189 190
			manual &= ~INVERT_MASK;
			manual |= th->pwm_inv[fan];
Linus Torvalds's avatar
Linus Torvalds committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
			write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK));
		}
	}
	
	th->last_speed[fan] = speed;			
}

static void read_sensors(struct thermostat *th)
{
	int i = 0;

	for (i = 0; i < 3; i++)
		th->temps[i]  = read_reg(th, TEMP_REG[i]);
}

#ifdef DEBUG
static void display_stats(struct thermostat *th)
{
	if (th->temps[0] != th->cached_temp[0]
	||  th->temps[1] != th->cached_temp[1]
	||  th->temps[2] != th->cached_temp[2]) {
		printk(KERN_INFO "adt746x: Temperature infos:"
				 " thermostats: %d,%d,%d;"
				 " limits: %d,%d,%d;"
				 " fan speed: %d RPM\n",
				 th->temps[0], th->temps[1], th->temps[2],
				 th->limits[0],  th->limits[1],  th->limits[2],
				 read_fan_speed(th, FAN_SPEED[0]));
	}
	th->cached_temp[0] = th->temps[0];
	th->cached_temp[1] = th->temps[1];
	th->cached_temp[2] = th->temps[2];
}
#endif

static void update_fans_speed (struct thermostat *th)
{
	int lastvar = 0; /* last variation, for iBook */
	int i = 0;

	/* we don't care about local sensor, so we start at sensor 1 */
	for (i = 1; i < 3; i++) {
		int started = 0;
234
		int fan_number = (th->type == ADT7460 && i == 2);
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
		int var = th->temps[i] - th->limits[i];

		if (var > -1) {
			int step = (255 - fan_speed) / 7;
			int new_speed = 0;

			/* hysteresis : change fan speed only if variation is
			 * more than two degrees */
			if (abs(var - th->last_var[fan_number]) < 2)
				continue;

			started = 1;
			new_speed = fan_speed + ((var-1)*step);

			if (new_speed < fan_speed)
				new_speed = fan_speed;
			if (new_speed > 255)
				new_speed = 255;

254 255
			if (verbose)
				printk(KERN_DEBUG "adt746x: Setting fans speed to %d "
256
						 "(limit exceeded by %d on %s)\n",
257 258
						new_speed, var,
						sensor_location[fan_number+1]);
Linus Torvalds's avatar
Linus Torvalds committed
259 260 261
			write_both_fan_speed(th, new_speed);
			th->last_var[fan_number] = var;
		} else if (var < -2) {
262
			/* don't stop fan if sensor2 is cold and sensor1 is not
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265
			 * so cold (lastvar >= -1) */
			if (i == 2 && lastvar < -1) {
				if (th->last_speed[fan_number] != 0)
266 267 268
					if (verbose)
						printk(KERN_DEBUG "adt746x: Stopping "
							"fans.\n");
Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273 274 275 276
				write_both_fan_speed(th, 0);
			}
		}

		lastvar = var;

		if (started)
			return; /* we don't want to re-stop the fan
277
				* if sensor1 is heating and sensor2 is not */
Linus Torvalds's avatar
Linus Torvalds committed
278 279 280 281 282 283 284
	}
}

static int monitor_task(void *arg)
{
	struct thermostat* th = arg;

285
	set_freezable();
Linus Torvalds's avatar
Linus Torvalds committed
286
	while(!kthread_should_stop()) {
287
		try_to_freeze();
Linus Torvalds's avatar
Linus Torvalds committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		msleep_interruptible(2000);

#ifndef DEBUG
		if (fan_speed != -1)
			read_sensors(th);
#else
		read_sensors(th);
#endif		

		if (fan_speed != -1)
			update_fans_speed(th);

#ifdef DEBUG
		display_stats(th);
#endif

	}

	return 0;
}

static void set_limit(struct thermostat *th, int i)
{
311 312 313
	/* Set sensor1 limit higher to avoid powerdowns */
	th->limits[i] = default_limits_chip[i] + limit_adjust;
	write_reg(th, LIMIT_REG[i], th->limits[i]);
Linus Torvalds's avatar
Linus Torvalds committed
314
		
315 316
	/* set our limits to normal */
	th->limits[i] = default_limits_local[i] + limit_adjust;
Linus Torvalds's avatar
Linus Torvalds committed
317 318
}

319 320 321 322 323
#define BUILD_SHOW_FUNC_INT(name, data)				\
static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)	\
{								\
	struct thermostat *th = dev_get_drvdata(dev);		\
	return sprintf(buf, "%d\n", data);			\
Linus Torvalds's avatar
Linus Torvalds committed
324 325
}

326
#define BUILD_SHOW_FUNC_INT_LITE(name, data)				\
327
static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)	\
Linus Torvalds's avatar
Linus Torvalds committed
328 329 330 331
{								\
	return sprintf(buf, "%d\n", data);			\
}

332
#define BUILD_SHOW_FUNC_STR(name, data)				\
333
static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
334 335 336 337
{								\
	return sprintf(buf, "%s\n", data);			\
}

Linus Torvalds's avatar
Linus Torvalds committed
338
#define BUILD_SHOW_FUNC_FAN(name, data)				\
339
static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
Linus Torvalds's avatar
Linus Torvalds committed
340
{								\
341
	struct thermostat *th = dev_get_drvdata(dev);		\
Linus Torvalds's avatar
Linus Torvalds committed
342
	return sprintf(buf, "%d (%d rpm)\n", 			\
343 344
		th->last_speed[data],				\
		read_fan_speed(th, FAN_SPEED[data])		\
Linus Torvalds's avatar
Linus Torvalds committed
345 346 347 348
		);						\
}

#define BUILD_STORE_FUNC_DEG(name, data)			\
349
static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
Linus Torvalds's avatar
Linus Torvalds committed
350
{								\
351
	struct thermostat *th = dev_get_drvdata(dev);		\
Linus Torvalds's avatar
Linus Torvalds committed
352 353 354
	int val;						\
	int i;							\
	val = simple_strtol(buf, NULL, 10);			\
355
	printk(KERN_INFO "Adjusting limits by %d degrees\n", val);	\
Linus Torvalds's avatar
Linus Torvalds committed
356 357
	limit_adjust = val;					\
	for (i=0; i < 3; i++)					\
358
		set_limit(th, i);				\
Linus Torvalds's avatar
Linus Torvalds committed
359 360 361 362
	return n;						\
}

#define BUILD_STORE_FUNC_INT(name, data)			\
363
static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
Linus Torvalds's avatar
Linus Torvalds committed
364
{								\
365 366
	int val;						\
	val = simple_strtol(buf, NULL, 10);			\
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369 370 371 372 373
	if (val < 0 || val > 255)				\
		return -EINVAL;					\
	printk(KERN_INFO "Setting specified fan speed to %d\n", val);	\
	data = val;						\
	return n;						\
}

374 375 376 377
BUILD_SHOW_FUNC_INT(sensor1_temperature,	 (read_reg(th, TEMP_REG[1])))
BUILD_SHOW_FUNC_INT(sensor2_temperature,	 (read_reg(th, TEMP_REG[2])))
BUILD_SHOW_FUNC_INT(sensor1_limit,		 th->limits[1])
BUILD_SHOW_FUNC_INT(sensor2_limit,		 th->limits[2])
378 379
BUILD_SHOW_FUNC_STR(sensor1_location,		 sensor_location[1])
BUILD_SHOW_FUNC_STR(sensor2_location,		 sensor_location[2])
Linus Torvalds's avatar
Linus Torvalds committed
380

381 382 383
BUILD_SHOW_FUNC_INT_LITE(specified_fan_speed, fan_speed)
BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)

384 385
BUILD_SHOW_FUNC_FAN(sensor1_fan_speed,	 0)
BUILD_SHOW_FUNC_FAN(sensor2_fan_speed,	 1)
Linus Torvalds's avatar
Linus Torvalds committed
386

387 388
BUILD_SHOW_FUNC_INT_LITE(limit_adjust,	 limit_adjust)
BUILD_STORE_FUNC_DEG(limit_adjust,	 th)
Linus Torvalds's avatar
Linus Torvalds committed
389
		
390 391 392 393 394 395 396 397 398 399 400 401
static DEVICE_ATTR(sensor1_temperature,	S_IRUGO,
		   show_sensor1_temperature,NULL);
static DEVICE_ATTR(sensor2_temperature,	S_IRUGO,
		   show_sensor2_temperature,NULL);
static DEVICE_ATTR(sensor1_limit, S_IRUGO,
		   show_sensor1_limit,	NULL);
static DEVICE_ATTR(sensor2_limit, S_IRUGO,
		   show_sensor2_limit,	NULL);
static DEVICE_ATTR(sensor1_location, S_IRUGO,
		   show_sensor1_location, NULL);
static DEVICE_ATTR(sensor2_location, S_IRUGO,
		   show_sensor2_location, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
402 403 404 405

static DEVICE_ATTR(specified_fan_speed,	S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
		   show_specified_fan_speed,store_specified_fan_speed);

406 407 408 409
static DEVICE_ATTR(sensor1_fan_speed,	S_IRUGO,
		   show_sensor1_fan_speed,	NULL);
static DEVICE_ATTR(sensor2_fan_speed,	S_IRUGO,
		   show_sensor2_fan_speed,	NULL);
Linus Torvalds's avatar
Linus Torvalds committed
410 411 412 413

static DEVICE_ATTR(limit_adjust,	S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
		   show_limit_adjust,	store_limit_adjust);

414
static void thermostat_create_files(struct thermostat *th)
Linus Torvalds's avatar
Linus Torvalds committed
415
{
416 417 418
	struct device_node *np = th->clt->dev.of_node;
	struct device *dev;
	int err;
419

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	/* To maintain ABI compatibility with userspace, create
	 * the old style platform driver and attach the attributes
	 * to it here
	 */
	th->pdev = of_platform_device_create(np, "temperatures", NULL);
	if (!th->pdev)
		return;
	dev = &th->pdev->dev;
	dev_set_drvdata(dev, th);
	err = device_create_file(dev, &dev_attr_sensor1_temperature);
	err |= device_create_file(dev, &dev_attr_sensor2_temperature);
	err |= device_create_file(dev, &dev_attr_sensor1_limit);
	err |= device_create_file(dev, &dev_attr_sensor2_limit);
	err |= device_create_file(dev, &dev_attr_sensor1_location);
	err |= device_create_file(dev, &dev_attr_sensor2_location);
	err |= device_create_file(dev, &dev_attr_limit_adjust);
	err |= device_create_file(dev, &dev_attr_specified_fan_speed);
	err |= device_create_file(dev, &dev_attr_sensor1_fan_speed);
	if(th->type == ADT7460)
		err |= device_create_file(dev, &dev_attr_sensor2_fan_speed);
	if (err)
		printk(KERN_WARNING
			"Failed to create temperature attribute file(s).\n");
}
444

445 446 447
static void thermostat_remove_files(struct thermostat *th)
{
	struct device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
448

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	if (!th->pdev)
		return;
	dev = &th->pdev->dev;
	device_remove_file(dev, &dev_attr_sensor1_temperature);
	device_remove_file(dev, &dev_attr_sensor2_temperature);
	device_remove_file(dev, &dev_attr_sensor1_limit);
	device_remove_file(dev, &dev_attr_sensor2_limit);
	device_remove_file(dev, &dev_attr_sensor1_location);
	device_remove_file(dev, &dev_attr_sensor2_location);
	device_remove_file(dev, &dev_attr_limit_adjust);
	device_remove_file(dev, &dev_attr_specified_fan_speed);
	device_remove_file(dev, &dev_attr_sensor1_fan_speed);	
	if (th->type == ADT7460)
		device_remove_file(dev, &dev_attr_sensor2_fan_speed);
	of_device_unregister(th->pdev);
Linus Torvalds's avatar
Linus Torvalds committed
464

465
}
Linus Torvalds's avatar
Linus Torvalds committed
466

467 468 469 470 471 472 473
static int probe_thermostat(struct i2c_client *client,
			    const struct i2c_device_id *id)
{
	struct device_node *np = client->dev.of_node;
	struct thermostat* th;
	const __be32 *prop;
	int i, rc, vers, offset = 0;
Linus Torvalds's avatar
Linus Torvalds committed
474

475 476 477 478 479 480 481 482 483 484
	if (!np)
		return -ENXIO;
	prop = of_get_property(np, "hwsensor-params-version", NULL);
	if (!prop)
		return -ENXIO;
	vers = be32_to_cpup(prop);
	printk(KERN_INFO "adt746x: version %d (%ssupported)\n",
	       vers, vers == 1 ? "" : "un");
	if (vers != 1)
		return -ENXIO;
Linus Torvalds's avatar
Linus Torvalds committed
485

486
	if (of_get_property(np, "hwsensor-location", NULL)) {
487
		for (i = 0; i < 3; i++) {
488
			sensor_location[i] = of_get_property(np,
489 490 491 492 493 494 495 496 497 498
					"hwsensor-location", NULL) + offset;

			if (sensor_location[i] == NULL)
				sensor_location[i] = "";

			printk(KERN_INFO "sensor %d: %s\n", i, sensor_location[i]);
			offset += strlen(sensor_location[i]) + 1;
		}
	}

499 500 501 502 503 504 505
	th = kzalloc(sizeof(struct thermostat), GFP_KERNEL);
	if (!th)
		return -ENOMEM;

	i2c_set_clientdata(client, th);
	th->clt = client;
	th->type = id->driver_data;
506

507 508 509 510
	rc = read_reg(th, CONFIG_REG);
	if (rc < 0) {
		dev_err(&client->dev, "Thermostat failed to read config!\n");
		kfree(th);
Linus Torvalds's avatar
Linus Torvalds committed
511 512
		return -ENODEV;
	}
513

514 515 516 517 518 519 520 521 522 523
	/* force manual control to start the fan quieter */
	if (fan_speed == -1)
		fan_speed = 64;
	
	if (th->type == ADT7460) {
		printk(KERN_INFO "adt746x: ADT7460 initializing\n");
		/* The 7460 needs to be started explicitly */
		write_reg(th, CONFIG_REG, 1);
	} else
		printk(KERN_INFO "adt746x: ADT7467 initializing\n");
524

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	for (i = 0; i < 3; i++) {
		th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
		set_limit(th, i);
	}

	printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"
			 " to %d, %d, %d\n",
			 th->initial_limits[0], th->initial_limits[1],
			 th->initial_limits[2], th->limits[0], th->limits[1],
			 th->limits[2]);

	/* record invert bit status because fw can corrupt it after suspend */
	th->pwm_inv[0] = read_reg(th, MANUAL_MODE[0]) & INVERT_MASK;
	th->pwm_inv[1] = read_reg(th, MANUAL_MODE[1]) & INVERT_MASK;

	/* be sure to really write fan speed the first time */
	th->last_speed[0] = -2;
	th->last_speed[1] = -2;
	th->last_var[0] = -80;
	th->last_var[1] = -80;

	if (fan_speed != -1) {
		/* manual mode, stop fans */
		write_both_fan_speed(th, 0);
	} else {
		/* automatic mode */
		write_both_fan_speed(th, -1);
	}
	
	th->thread = kthread_run(monitor_task, th, "kfand");
	if (th->thread == ERR_PTR(-ENOMEM)) {
		printk(KERN_INFO "adt746x: Kthread creation failed\n");
		th->thread = NULL;
		return -ENOMEM;
	}

	thermostat_create_files(th);

	return 0;
564 565
}

566
static int remove_thermostat(struct i2c_client *client)
567
{
568 569 570 571
	struct thermostat *th = i2c_get_clientdata(client);
	int i;
	
	thermostat_remove_files(th);
572

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	if (th->thread != NULL)
		kthread_stop(th->thread);

	printk(KERN_INFO "adt746x: Putting max temperatures back from "
			 "%d, %d, %d to %d, %d, %d\n",
		th->limits[0], th->limits[1], th->limits[2],
		th->initial_limits[0], th->initial_limits[1],
		th->initial_limits[2]);

	for (i = 0; i < 3; i++)
		write_reg(th, LIMIT_REG[i], th->initial_limits[i]);

	write_both_fan_speed(th, -1);

	kfree(th);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
590 591
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
static const struct i2c_device_id therm_adt746x_id[] = {
	{ "MAC,adt7460", ADT7460 },
	{ "MAC,adt7467", ADT7467 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, therm_adt746x_id);

static struct i2c_driver thermostat_driver = {
	.driver = {
		.name	= "therm_adt746x",
	},
	.probe = probe_thermostat,
	.remove = remove_thermostat,
	.id_table = therm_adt746x_id,
};

static int __init thermostat_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
609
{
610 611 612
#ifndef CONFIG_I2C_POWERMAC
	request_module("i2c-powermac");
#endif
Linus Torvalds's avatar
Linus Torvalds committed
613

614
	return i2c_add_driver(&thermostat_driver);
615 616
}

617
static void __exit thermostat_exit(void)
618
{
Linus Torvalds's avatar
Linus Torvalds committed
619 620 621 622 623
	i2c_del_driver(&thermostat_driver);
}

module_init(thermostat_init);
module_exit(thermostat_exit);