[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [PATCH] Latest patch to therm_adt7467



On 24 Jan 2004 at 10h01, Colin Leroy wrote:

Hi, 

> . limits are set quite high on the ADT7460 chip, but the fan starts much 
>   below these limits (in order to avoid poweroffs).

Yuck. My iBook just powered off. Probably did not happen before because I was
in colder environments... This new patch sets limits higher on iBook too, and 
obsolotes the previous one.

Phew. 
-- 
Colin
Index: drivers/macintosh/therm_adt7467.c
===================================================================
RCS file: /home/cvsroot/linuxppc/drivers/macintosh/therm_adt7467.c,v
retrieving revision 1.4
diff -u -u -r1.4 therm_adt7467.c
--- drivers/macintosh/therm_adt7467.c	24 Jan 2004 10:04:30 -0000	1.4
+++ drivers/macintosh/therm_adt7467.c	24 Jan 2004 10:36:46 -0000
@@ -1,10 +1,11 @@
 /*
- * Device driver for the i2c thermostat found on the iBook G4
+ * Device driver for the i2c thermostat found on the iBook G4, Albook G4
  *
- * Copyright (C) 2003 Colin Leroy, Benjamin Herrenschmidt
+ * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt
  *
  * Documentation from
  * http://www.analog.com/UploadedFiles/Data_Sheets/115254175ADT7467_pra.pdf
+ * http://www.analog.com/UploadedFiles/Data_Sheets/3686221171167ADT7460_b.pdf
  *
  */
 
@@ -30,18 +31,19 @@
 
 #undef DEBUG
 
-#define TEMP_LOCAL   0x26
-#define TEMP_REMOTE1 0x25
-#define TEMP_REMOTE2 0x27
-#define LIM_LOCAL    0x6a
-#define LIM_REMOTE1  0x6b
-#define LIM_REMOTE2  0x6c
-#define FAN0_SPEED   0x28
-
-#define MANUAL_MODE  0x5c
+#define CONFIG_REG   0x40
 #define MANUAL_MASK  0xe0
 #define AUTO_MASK    0x20
-#define FAN_SPD_SET  0x30
+
+static u8 TEMP_REG[3]    = {0x26, 0x25, 0x27}; /* local, cpu, gpu */
+static u8 LIMIT_REG[3]   = {0x6b, 0x6a, 0x6c}; /* local, cpu, gpu */
+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};
+
+static u8 default_limits_local[3] = {70, 50, 70};    /* local, cpu, gpu */
+static u8 default_limits_chip[3] = {80, 65, 80};    /* local, cpu, gpu */
 
 static int limit_decrease = 0;
 static int fan_speed = -1;
@@ -53,17 +55,18 @@
 MODULE_PARM(limit_decrease,"i");
 MODULE_PARM_DESC(limit_decrease,"Decrease maximum temperatures (50°C cpu, 70°C gpu) by N °C.");
 MODULE_PARM(fan_speed,"i");
-MODULE_PARM_DESC(fan_speed,"Specify fan speed (0-255) when lim < temp < lim+8 (dangerous !), default is automatic");
+MODULE_PARM_DESC(fan_speed,"Specify fan speed (0-255) when lim < temp < lim+8 (default 128)");
 
 struct thermostat {
 	struct i2c_client	clt;
 	u8			cached_temp[3];
 	u8			initial_limits[3];
 	u8			limits[3];
-	int			last_speed;
-	int			overriding;
+	int			last_speed[2];
+	int			overriding[2];
 };
 
+static enum {ADT7460, ADT7467} therm_type;
 static int therm_bus, therm_address;
 static struct of_device * of_dev;
 static struct thermostat* thermostat;
@@ -72,7 +75,8 @@
 static struct completion monitor_task_compl;
 
 static int attach_one_thermostat(struct i2c_adapter *adapter, int addr, int busno);
-static void write_fan_speed(struct thermostat *th, int speed);
+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)
@@ -125,6 +129,7 @@
 detach_thermostat(struct i2c_adapter *adapter)
 {
 	struct thermostat* th;
+	int i;
 	
 	if (thermostat == NULL)
 		return 0;
@@ -136,14 +141,15 @@
 		wait_for_completion(&monitor_task_compl);
 	}
 		
-	printk(KERN_INFO "adt7467: Putting max temperatures back from %d, %d, %d,"
+	printk(KERN_INFO "adt746x: Putting max temperatures back from %d, %d, %d,"
 		" to %d, %d, %d, (°C)\n", 
 		th->limits[0], th->limits[1], th->limits[2],
 		th->initial_limits[0], th->initial_limits[1], th->initial_limits[2]);
-	write_reg(th, LIM_LOCAL,   th->initial_limits[0]);
-	write_reg(th, LIM_REMOTE1, th->initial_limits[1]);
-	write_reg(th, LIM_REMOTE2, th->initial_limits[2]);
-	write_fan_speed(th, -1);
+	
+	for (i = 0; i < 3; i++)
+		write_reg(th, LIMIT_REG[i], th->initial_limits[i]);
+
+	write_both_fan_speed(th, -1);
 
 	i2c_detach_client(&th->clt);
 
@@ -175,7 +181,14 @@
 	return (90000*60)/res;
 }
 
-static void write_fan_speed(struct thermostat *th, int speed)
+static void write_both_fan_speed(struct thermostat *th, int speed)
+{
+	write_fan_speed(th, speed, 0);
+	if (therm_type == ADT7460)
+		write_fan_speed(th, speed, 1);
+}
+
+static void write_fan_speed(struct thermostat *th, int speed, int fan)
 {
 	u8 manual;
 	
@@ -184,21 +197,35 @@
 	else if (speed < -1) 
 		speed = 0;
 	
+	if (therm_type == ADT7467 && fan == 1)
+		return;
+	
+	if (th->last_speed[fan] != speed) {
+		if (speed == -1)
+			printk(KERN_INFO "adt746x: Setting speed to: automatic for %s fan.\n",
+				fan?"GPU":"CPU");
+		else
+			printk(KERN_INFO "adt746x: Setting speed to: %d for %s fan.\n",
+				speed, fan?"GPU":"CPU");
+	} else
+		return;
+	
 	if (speed >= 0) {
-		manual = read_reg(th, MANUAL_MODE);
-		write_reg(th, MANUAL_MODE, manual|MANUAL_MASK);
-		if (th->last_speed != speed)
-			printk(KERN_INFO "adt7467: Setting speed to: %d\n", speed);
-		th->last_speed = speed;
-		write_reg(th, FAN_SPD_SET, speed);
+		manual = read_reg(th, MANUAL_MODE[fan]);
+		write_reg(th, MANUAL_MODE[fan], manual|MANUAL_MASK);
+		write_reg(th, FAN_SPD_SET[fan], speed);
 	} else {
 		/* back to automatic */
-		manual = read_reg(th, MANUAL_MODE);
-		if (th->last_speed != -1)
-			printk(KERN_INFO "adt7467: Setting speed to: automatic\n");
-		th->last_speed = -1;
-		write_reg(th, MANUAL_MODE, manual&(~AUTO_MASK));
+		if(therm_type == ADT7460) {
+			manual = read_reg(th, MANUAL_MODE[fan]) & (~MANUAL_MASK);
+			write_reg(th, MANUAL_MODE[fan], manual|REM_CONTROL[fan]);
+		} else {
+			manual = read_reg(th, MANUAL_MODE[fan]);
+			write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK));
+		}
 	}
+	
+	th->last_speed[fan] = speed;			
 }
 
 static int monitor_task(void *arg)
@@ -206,6 +233,7 @@
 	struct thermostat* th = arg;
 	u8 temps[3];
 	u8 lims[3];
+	int i;
 #ifdef DEBUG
 	int mfan_speed;
 #endif
@@ -228,53 +256,59 @@
 #ifndef DEBUG
 		if (fan_speed != -1) {
 #endif
-			temps[0]  = read_reg(th, TEMP_LOCAL);
-			temps[1]  = read_reg(th, TEMP_REMOTE1);
-			temps[2]  = read_reg(th, TEMP_REMOTE2);
-			lims[0]   = th->limits[0];
-			lims[1]   = th->limits[1];
-			lims[2]   = th->limits[2];
+			for (i = 0; i < 3; i++) {
+				temps[i]  = read_reg(th, TEMP_REG[i]);
+				lims[i]   = th->limits[i];
+			}
 #ifndef DEBUG
 		}
 #endif		
 		if (fan_speed != -1) {
-			if (temps[0] > lims[0]
-			||  temps[1] > lims[1]
-			||  temps[2] > lims[2]) {
-				int var = 0;
-				var = (temps[0] - lims[0] > var) ? temps[0] - lims[0] : var;
-				var = (temps[1] - lims[1] > var) ? temps[1] - lims[1] : var;
-				var = (temps[2] - lims[2] > var) ? temps[2] - lims[2] : var;
+			int lastvar = 0;		/* for iBook */
+			for (i = 1; i < 3; i++) {	/* we don't care about local sensor */
+				int started = 0;
+				int fan_number = (therm_type == ADT7460 && i == 2);
+				int var = temps[i] - lims[i];
 				if (var > 8) {
-					if (th->overriding == 0)
-						printk(KERN_INFO "adt7467: Limit exceeded by %d°C, overriding specified fan speed.\n",
-							var);
-					th->overriding = 1;
-					write_fan_speed(th, 255);
-				} else if (!th->overriding || var < 6) {
-					if (th->overriding == 1)
-						printk(KERN_INFO "adt7467: Limit exceeded by %d°C, setting speed to specified.\n",
-							var);					
-					th->overriding = 0;
-					write_fan_speed(th, fan_speed);
+					if (th->overriding[fan_number] == 0)
+						printk(KERN_INFO "adt746x: Limit exceeded by %d°C, overriding specified fan speed for %s.\n",
+							var, fan_number?"GPU":"CPU");
+					th->overriding[fan_number] = 1;
+					write_fan_speed(th, 255, fan_number);
+					started = 1;
+				} else if ((!th->overriding[fan_number] || var < 6) && var > 0) {
+					if (th->overriding[fan_number] == 1)
+						printk(KERN_INFO "adt746x: Limit exceeded by %d°C, setting speed to specified for %s.\n",
+							var, fan_number?"GPU":"CPU");					
+					th->overriding[fan_number] = 0;
+					write_fan_speed(th, fan_speed, fan_number);
+					started = 1;
+				} else if (var < -1) {
+					/* don't stop iBook fan if GPU is cold and CPU is not
+					 * so cold (lastvar >= -1) */
+					if (therm_type == ADT7460 || lastvar < -1 || i == 1) {
+						if (th->last_speed[fan_number] != 0)
+							printk(KERN_INFO "adt746x: Stopping %s fan.\n",
+								fan_number?"GPU":"CPU");
+						write_fan_speed(th, 0, fan_number);
+					}
 				}
-			} else {
-				int var = 10;
-				var = (lims[0] - temps[0] < var) ? lims[0] - temps[0] : var;
-				var = (lims[1] - temps[1] < var) ? lims[1] - temps[1] : var;
-				var = (lims[2] - temps[2] < var) ? lims[2] - temps[2] : var;
-				if (var >= 2) /* pseudo hysteresis */
-					write_fan_speed(th, 0);
+				
+				lastvar = var;
+				
+				if (started && therm_type == ADT7467)
+					break; /* we don't want to re-stop the fan
+						* if CPU is heating and GPU is not */
 			}
 		}
 #ifdef DEBUG
-		mfan_speed = read_fan_speed(th, FAN0_SPEED);
+		mfan_speed = read_fan_speed(th, FAN_SPEED[0]);
 		/* only one fan in the iBook G4 */
 				
 		if (temps[0] != th->cached_temp[0]
 		||  temps[1] != th->cached_temp[1]
 		||  temps[2] != th->cached_temp[2]) {
-			printk(KERN_INFO "adt7467: Temperature infos:"
+			printk(KERN_INFO "adt746x: Temperature infos:"
 					 " thermostats: %d,%d,%d °C;"
 					 " limits: %d,%d,%d °C;"
 					 " fan speed: %d RPM\n",
@@ -297,6 +331,7 @@
 {
 	struct thermostat* th;
 	int rc;
+	int i;
 
 	if (thermostat)
 		return 0;
@@ -312,41 +347,56 @@
 
 	rc = read_reg(th, 0);
 	if (rc < 0) {
-		printk(KERN_ERR "adt7467: Thermostat failed to read config from bus %d !\n",
+		printk(KERN_ERR "adt746x: Thermostat failed to read config from bus %d !\n",
 			busno);
 		kfree(th);
 		return -ENODEV;
 	}
-	printk(KERN_INFO "adt7467: ADT7467 initializing\n");
-
-	th->initial_limits[0] = read_reg(th, LIM_LOCAL);
-	th->initial_limits[1] = read_reg(th, LIM_REMOTE1);
-	th->initial_limits[2] = read_reg(th, LIM_REMOTE2);
-	th->limits[0] = 70 - limit_decrease;	/* Local */
-	th->limits[1] = 50 - limit_decrease;	/* CPU */
-	th->limits[2] = 70 - limit_decrease;	/* GPU */
+	/* force manual control to start the fan quieter */
+	
+	if (fan_speed == -1)
+		fan_speed=128;
+	
+	if(therm_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");
+
+	for (i = 0; i < 3; i++) {
+		th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
+
+		/* Set CPU limit higher to avoid powerdowns */ 
+		th->limits[i] = default_limits_chip[i] - limit_decrease;
+		write_reg(th, LIMIT_REG[i], th->limits[i]);
+		
+		/* set our limits to normal */
+		th->limits[i] = default_limits_local[i] - limit_decrease;
+	}
 	
-	printk(KERN_INFO "adt7467: Lowering max temperatures from %d, %d, %d"
+	printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"
 		" to %d, %d, %d (°C)\n", 
 		th->initial_limits[0], th->initial_limits[1], th->initial_limits[2], 
 		th->limits[0], th->limits[1], th->limits[2]);
-	write_reg(th, LIM_LOCAL,   th->limits[0]);
-	write_reg(th, LIM_REMOTE1, th->limits[1]);
-	write_reg(th, LIM_REMOTE2, th->limits[2]);
 
 	thermostat = th;
 
 	if (i2c_attach_client(&th->clt)) {
-		printk("adt7467: Thermostat failed to attach client !\n");
+		printk("adt746x: Thermostat failed to attach client !\n");
 		thermostat = NULL;
 		kfree(th);
 		return -ENODEV;
 	}
 
+	/* be sure to really write fan speed the first time */
+	th->last_speed[0] = -2;
+	th->last_speed[1] = -2;
+	
 	if (fan_speed != -1) {
-		write_fan_speed(th, 0);
+		write_both_fan_speed(th, 0);
 	} else {
-		write_fan_speed(th, -1);
+		write_both_fan_speed(th, -1);
 	}
 	
 	init_completion(&monitor_task_compl);
@@ -374,18 +424,20 @@
 	return sprintf(buf, "%d", data);			\
 }
 
-BUILD_SHOW_FUNC_DEG(cpu_temperature, (read_reg(thermostat, TEMP_REMOTE1)))
-BUILD_SHOW_FUNC_DEG(gpu_temperature, (read_reg(thermostat, TEMP_REMOTE2)))
+BUILD_SHOW_FUNC_DEG(cpu_temperature, (read_reg(thermostat, TEMP_REG[1])))
+BUILD_SHOW_FUNC_DEG(gpu_temperature, (read_reg(thermostat, TEMP_REG[2])))
 BUILD_SHOW_FUNC_DEG(cpu_limit, thermostat->limits[1])
 BUILD_SHOW_FUNC_DEG(gpu_limit, thermostat->limits[2])
-BUILD_SHOW_FUNC_INT(fan_speed, (read_fan_speed(thermostat, FAN0_SPEED)))
-		
+BUILD_SHOW_FUNC_INT(fan_speed, (read_fan_speed(thermostat, FAN_SPEED[0])))
+BUILD_SHOW_FUNC_INT(fan_gpu_speed, (read_fan_speed(thermostat, FAN_SPEED[1])))
+
 
 static DEVICE_ATTR(cpu_temperature,S_IRUGO,show_cpu_temperature,NULL);
 static DEVICE_ATTR(gpu_temperature,S_IRUGO,show_gpu_temperature,NULL);
 static DEVICE_ATTR(cpu_limit,S_IRUGO,show_cpu_limit,NULL);
 static DEVICE_ATTR(gpu_limit,S_IRUGO,show_gpu_limit,NULL);
 static DEVICE_ATTR(fan_speed,S_IRUGO,show_fan_speed,NULL);
+static DEVICE_ATTR(fan_gpu_speed,S_IRUGO,show_fan_gpu_speed,NULL);
 
 static int __init
 thermostat_init(void)
@@ -399,7 +451,11 @@
 	np = of_find_node_by_name(NULL, "fan");
 	if (!np)
 		return -ENODEV;
-	if (!device_is_compatible(np, "adt7467"))
+	if (device_is_compatible(np, "adt7460"))
+		therm_type = ADT7460;
+	else if (device_is_compatible(np, "adt7467"))
+		therm_type = ADT7467;
+	else
 		return -ENODEV;
 
 	prop = (u32 *)get_property(np, "reg", NULL);
@@ -408,7 +464,7 @@
 	therm_bus = ((*prop) >> 8) & 0x0f;
 	therm_address = ((*prop) & 0xff) >> 1;
 
-	printk(KERN_INFO "adt7467: Thermostat bus: %d, address: 0x%02x, limit_decrease: %d, fan_speed: %d\n",
+	printk(KERN_INFO "adt746x: Thermostat bus: %d, address: 0x%02x, limit_decrease: %d, fan_speed: %d\n",
 		therm_bus, therm_address, limit_decrease, fan_speed);
 
 	of_dev = of_platform_device_create(np, "temperatures");
@@ -423,7 +479,9 @@
 	device_create_file(&of_dev->dev, &dev_attr_cpu_limit);
 	device_create_file(&of_dev->dev, &dev_attr_gpu_limit);
 	device_create_file(&of_dev->dev, &dev_attr_fan_speed);
-	
+	if(therm_type == ADT7460)
+		device_create_file(&of_dev->dev, &dev_attr_fan_gpu_speed);
+
 #ifndef CONFIG_I2C_KEYWEST
 	request_module("i2c-keywest");
 #endif
@@ -440,6 +498,8 @@
 		device_remove_file(&of_dev->dev, &dev_attr_cpu_limit);
 		device_remove_file(&of_dev->dev, &dev_attr_gpu_limit);
 		device_remove_file(&of_dev->dev, &dev_attr_fan_speed);
+		if(therm_type == ADT7460)
+			device_remove_file(&of_dev->dev, &dev_attr_fan_gpu_speed);
 		of_device_unregister(of_dev);
 	}
 	i2c_del_driver(&thermostat_driver);

Reply to: