2 * Device driver for the i2c thermostat found on the iBook G4, Albook G4
4 * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt
7 * http://www.analog.com/UploadedFiles/Data_Sheets/115254175ADT7467_pra.pdf
8 * http://www.analog.com/UploadedFiles/Data_Sheets/3686221171167ADT7460_b.pdf
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/spinlock.h>
23 #include <linux/smp_lock.h>
24 #include <linux/wait.h>
26 #include <asm/machdep.h>
28 #include <asm/system.h>
29 #include <asm/sections.h>
30 #include <asm/of_device.h>
34 #define CONFIG_REG 0x40
35 #define MANUAL_MASK 0xe0
36 #define AUTO_MASK 0x20
38 static u8 TEMP_REG[3] = {0x26, 0x25, 0x27}; /* local, cpu, gpu */
39 static u8 LIMIT_REG[3] = {0x6b, 0x6a, 0x6c}; /* local, cpu, gpu */
40 static u8 MANUAL_MODE[2] = {0x5c, 0x5d};
41 static u8 REM_CONTROL[2] = {0x00, 0x40};
42 static u8 FAN_SPEED[2] = {0x28, 0x2a};
43 static u8 FAN_SPD_SET[2] = {0x30, 0x31};
45 static u8 default_limits_local[3] = {70, 50, 70}; /* local, cpu, gpu */
46 static u8 default_limits_chip[3] = {80, 65, 80}; /* local, cpu, gpu */
48 static int limit_adjust = 0;
49 static int fan_speed = -1;
51 MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
52 MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and Powerbook G4 Alu");
53 MODULE_LICENSE("GPL");
55 MODULE_PARM(limit_adjust,"i");
56 MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50°C cpu, 70°C gpu) by N °C.");
57 MODULE_PARM(fan_speed,"i");
58 MODULE_PARM_DESC(fan_speed,"Specify fan speed (0-255) when lim < temp < lim+8 (default 128)");
61 struct i2c_client clt;
69 static enum {ADT7460, ADT7467} therm_type;
70 static int therm_bus, therm_address;
71 static struct of_device * of_dev;
72 static struct thermostat* thermostat;
73 static pid_t monitor_thread_id;
74 static int monitor_running;
75 static struct completion monitor_task_compl;
77 static int attach_one_thermostat(struct i2c_adapter *adapter, int addr, int busno);
78 static void write_both_fan_speed(struct thermostat *th, int speed);
79 static void write_fan_speed(struct thermostat *th, int speed, int fan);
82 write_reg(struct thermostat* th, int reg, u8 data)
89 rc = i2c_master_send(&th->clt, (const char *)tmp, 2);
98 read_reg(struct thermostat* th, int reg)
104 rc = i2c_master_send(&th->clt, ®_addr, 1);
109 rc = i2c_master_recv(&th->clt, (char *)&data, 1);
116 attach_thermostat(struct i2c_adapter *adapter)
118 unsigned long bus_no;
120 if (strncmp(adapter->name, "uni-n", 5))
122 bus_no = simple_strtoul(adapter->name + 6, NULL, 10);
123 if (bus_no != therm_bus)
125 return attach_one_thermostat(adapter, therm_address, bus_no);
129 detach_thermostat(struct i2c_adapter *adapter)
131 struct thermostat* th;
134 if (thermostat == NULL)
139 if (monitor_running) {
141 wait_for_completion(&monitor_task_compl);
144 printk(KERN_INFO "adt746x: Putting max temperatures back from %d, %d, %d,"
145 " to %d, %d, %d, (°C)\n",
146 th->limits[0], th->limits[1], th->limits[2],
147 th->initial_limits[0], th->initial_limits[1], th->initial_limits[2]);
149 for (i = 0; i < 3; i++)
150 write_reg(th, LIMIT_REG[i], th->initial_limits[i]);
152 write_both_fan_speed(th, -1);
154 i2c_detach_client(&th->clt);
163 static struct i2c_driver thermostat_driver = {
164 .name ="Apple Thermostat ADT746x",
166 .flags =I2C_DF_NOTIFY,
167 .attach_adapter =&attach_thermostat,
168 .detach_adapter =&detach_thermostat,
171 static int read_fan_speed(struct thermostat *th, u8 addr)
176 /* should start with low byte */
177 tmp[1] = read_reg(th, addr);
178 tmp[0] = read_reg(th, addr + 1);
180 res = tmp[1] + (tmp[0] << 8);
181 return (90000*60)/res;
184 static void write_both_fan_speed(struct thermostat *th, int speed)
186 write_fan_speed(th, speed, 0);
187 if (therm_type == ADT7460)
188 write_fan_speed(th, speed, 1);
191 static void write_fan_speed(struct thermostat *th, int speed, int fan)
200 if (therm_type == ADT7467 && fan == 1)
203 if (th->last_speed[fan] != speed) {
205 printk(KERN_INFO "adt746x: Setting speed to: automatic for %s fan.\n",
208 printk(KERN_INFO "adt746x: Setting speed to: %d for %s fan.\n",
209 speed, fan?"GPU":"CPU");
214 manual = read_reg(th, MANUAL_MODE[fan]);
215 write_reg(th, MANUAL_MODE[fan], manual|MANUAL_MASK);
216 write_reg(th, FAN_SPD_SET[fan], speed);
218 /* back to automatic */
219 if(therm_type == ADT7460) {
220 manual = read_reg(th, MANUAL_MODE[fan]) & (~MANUAL_MASK);
221 write_reg(th, MANUAL_MODE[fan], manual|REM_CONTROL[fan]);
223 manual = read_reg(th, MANUAL_MODE[fan]);
224 write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK));
228 th->last_speed[fan] = speed;
231 static int monitor_task(void *arg)
233 struct thermostat* th = arg;
244 strcpy(current->comm, "thermostat");
247 while(monitor_running)
256 if (fan_speed != -1) {
258 for (i = 0; i < 3; i++) {
259 temps[i] = read_reg(th, TEMP_REG[i]);
260 lims[i] = th->limits[i];
265 if (fan_speed != -1) {
266 int lastvar = 0; /* for iBook */
267 for (i = 1; i < 3; i++) { /* we don't care about local sensor */
269 int fan_number = (therm_type == ADT7460 && i == 2);
270 int var = temps[i] - lims[i];
272 if (th->overriding[fan_number] == 0)
273 printk(KERN_INFO "adt746x: Limit exceeded by %d°C, overriding specified fan speed for %s.\n",
274 var, fan_number?"GPU":"CPU");
275 th->overriding[fan_number] = 1;
276 write_fan_speed(th, 255, fan_number);
278 } else if ((!th->overriding[fan_number] || var < 6) && var > 0) {
279 if (th->overriding[fan_number] == 1)
280 printk(KERN_INFO "adt746x: Limit exceeded by %d°C, setting speed to specified for %s.\n",
281 var, fan_number?"GPU":"CPU");
282 th->overriding[fan_number] = 0;
283 write_fan_speed(th, fan_speed, fan_number);
285 } else if (var < -1) {
286 /* don't stop iBook fan if GPU is cold and CPU is not
287 * so cold (lastvar >= -1) */
288 if (therm_type == ADT7460 || lastvar < -1 || i == 1) {
289 if (th->last_speed[fan_number] != 0)
290 printk(KERN_INFO "adt746x: Stopping %s fan.\n",
291 fan_number?"GPU":"CPU");
292 write_fan_speed(th, 0, fan_number);
298 if (started && therm_type == ADT7467)
299 break; /* we don't want to re-stop the fan
300 * if CPU is heating and GPU is not */
304 mfan_speed = read_fan_speed(th, FAN_SPEED[0]);
305 /* only one fan in the iBook G4 */
307 if (temps[0] != th->cached_temp[0]
308 || temps[1] != th->cached_temp[1]
309 || temps[2] != th->cached_temp[2]) {
310 printk(KERN_INFO "adt746x: Temperature infos:"
311 " thermostats: %d,%d,%d °C;"
312 " limits: %d,%d,%d °C;"
313 " fan speed: %d RPM\n",
314 temps[0], temps[1], temps[2],
315 lims[0], lims[1], lims[2],
318 th->cached_temp[0] = temps[0];
319 th->cached_temp[1] = temps[1];
320 th->cached_temp[2] = temps[2];
324 complete_and_exit(&monitor_task_compl, 0);
329 set_limit(struct thermostat *th, int i)
331 /* Set CPU limit higher to avoid powerdowns */
332 th->limits[i] = default_limits_chip[i] + limit_adjust;
333 write_reg(th, LIMIT_REG[i], th->limits[i]);
335 /* set our limits to normal */
336 th->limits[i] = default_limits_local[i] + limit_adjust;
340 attach_one_thermostat(struct i2c_adapter *adapter, int addr, int busno)
342 struct thermostat* th;
348 th = (struct thermostat *)kmalloc(sizeof(struct thermostat), GFP_KERNEL);
351 memset(th, 0, sizeof(*th));
353 th->clt.adapter = adapter;
354 th->clt.driver = &thermostat_driver;
355 th->clt.id = 0xDEAD7467;
356 strcpy(th->clt.name, "thermostat");
358 rc = read_reg(th, 0);
360 printk(KERN_ERR "adt746x: Thermostat failed to read config from bus %d !\n",
365 /* force manual control to start the fan quieter */
370 if(therm_type == ADT7460) {
371 printk(KERN_INFO "adt746x: ADT7460 initializing\n");
372 /* The 7460 needs to be started explicitly */
373 write_reg(th, CONFIG_REG, 1);
375 printk(KERN_INFO "adt746x: ADT7467 initializing\n");
377 for (i = 0; i < 3; i++) {
378 th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
382 printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"
383 " to %d, %d, %d (°C)\n",
384 th->initial_limits[0], th->initial_limits[1], th->initial_limits[2],
385 th->limits[0], th->limits[1], th->limits[2]);
389 if (i2c_attach_client(&th->clt)) {
390 printk("adt746x: Thermostat failed to attach client !\n");
396 /* be sure to really write fan speed the first time */
397 th->last_speed[0] = -2;
398 th->last_speed[1] = -2;
400 if (fan_speed != -1) {
401 write_both_fan_speed(th, 0);
403 write_both_fan_speed(th, -1);
406 init_completion(&monitor_task_compl);
408 monitor_thread_id = kernel_thread(monitor_task, th,
409 SIGCHLD | CLONE_KERNEL);
415 * Now, unfortunately, sysfs doesn't give us a nice void * we could
416 * pass around to the attribute functions, so we don't really have
417 * choice but implement a bunch of them...
420 #define BUILD_SHOW_FUNC_DEG(name, data) \
421 static ssize_t show_##name(struct device *dev, char *buf) \
423 return sprintf(buf, "%d°C\n", data); \
425 #define BUILD_SHOW_FUNC_INT(name, data) \
426 static ssize_t show_##name(struct device *dev, char *buf) \
428 return sprintf(buf, "%d\n", data); \
431 #define BUILD_STORE_FUNC_DEG(name, data) \
432 static ssize_t store_##name(struct device *dev, const char *buf, size_t n) \
436 val = simple_strtol(buf, NULL, 10); \
437 printk(KERN_INFO "Adjusting limits by %d°C\n", val); \
438 limit_adjust = val; \
439 for (i=0; i < 3; i++) \
440 set_limit(thermostat, i); \
444 #define BUILD_STORE_FUNC_INT(name, data) \
445 static ssize_t store_##name(struct device *dev, const char *buf, size_t n) \
448 val = simple_strtoul(buf, NULL, 10); \
449 if (val < 0 || val > 255) \
451 printk(KERN_INFO "Setting fan speed to %d\n", val); \
456 BUILD_SHOW_FUNC_DEG(cpu_temperature, (read_reg(thermostat, TEMP_REG[1])))
457 BUILD_SHOW_FUNC_DEG(gpu_temperature, (read_reg(thermostat, TEMP_REG[2])))
458 BUILD_SHOW_FUNC_DEG(cpu_limit, thermostat->limits[1])
459 BUILD_SHOW_FUNC_DEG(gpu_limit, thermostat->limits[2])
461 BUILD_SHOW_FUNC_INT(specified_fan_speed, fan_speed)
462 BUILD_SHOW_FUNC_INT(cpu_fan_speed, (read_fan_speed(thermostat, FAN_SPEED[0])))
463 BUILD_SHOW_FUNC_INT(gpu_fan_speed, (read_fan_speed(thermostat, FAN_SPEED[1])))
465 BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)
466 BUILD_SHOW_FUNC_INT(limit_adjust, limit_adjust)
467 BUILD_STORE_FUNC_DEG(limit_adjust, thermostat)
469 static DEVICE_ATTR(cpu_temperature, S_IRUGO,
470 show_cpu_temperature,NULL);
471 static DEVICE_ATTR(gpu_temperature, S_IRUGO,
472 show_gpu_temperature,NULL);
473 static DEVICE_ATTR(cpu_limit, S_IRUGO,
474 show_cpu_limit, NULL);
475 static DEVICE_ATTR(gpu_limit, S_IRUGO,
476 show_gpu_limit, NULL);
478 static DEVICE_ATTR(specified_fan_speed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
479 show_specified_fan_speed,store_specified_fan_speed);
481 static DEVICE_ATTR(cpu_fan_speed, S_IRUGO,
482 show_cpu_fan_speed, NULL);
483 static DEVICE_ATTR(gpu_fan_speed, S_IRUGO,
484 show_gpu_fan_speed, NULL);
486 static DEVICE_ATTR(limit_adjust, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
487 show_limit_adjust, store_limit_adjust);
491 thermostat_init(void)
493 struct device_node* np;
496 np = of_find_node_by_name(NULL, "fan");
499 if (device_is_compatible(np, "adt7460"))
500 therm_type = ADT7460;
501 else if (device_is_compatible(np, "adt7467"))
502 therm_type = ADT7467;
506 prop = (u32 *)get_property(np, "reg", NULL);
509 therm_bus = ((*prop) >> 8) & 0x0f;
510 therm_address = ((*prop) & 0xff) >> 1;
512 printk(KERN_INFO "adt746x: Thermostat bus: %d, address: 0x%02x, limit_adjust: %d, fan_speed: %d\n",
513 therm_bus, therm_address, limit_adjust, fan_speed);
515 of_dev = of_platform_device_create(np, "temperatures");
517 if (of_dev == NULL) {
518 printk(KERN_ERR "Can't register temperatures device !\n");
522 device_create_file(&of_dev->dev, &dev_attr_cpu_temperature);
523 device_create_file(&of_dev->dev, &dev_attr_gpu_temperature);
524 device_create_file(&of_dev->dev, &dev_attr_cpu_limit);
525 device_create_file(&of_dev->dev, &dev_attr_gpu_limit);
526 device_create_file(&of_dev->dev, &dev_attr_limit_adjust);
527 device_create_file(&of_dev->dev, &dev_attr_specified_fan_speed);
528 device_create_file(&of_dev->dev, &dev_attr_cpu_fan_speed);
529 if(therm_type == ADT7460)
530 device_create_file(&of_dev->dev, &dev_attr_gpu_fan_speed);
532 #ifndef CONFIG_I2C_KEYWEST
533 request_module("i2c-keywest");
536 return i2c_add_driver(&thermostat_driver);
540 thermostat_exit(void)
543 device_remove_file(&of_dev->dev, &dev_attr_cpu_temperature);
544 device_remove_file(&of_dev->dev, &dev_attr_gpu_temperature);
545 device_remove_file(&of_dev->dev, &dev_attr_cpu_limit);
546 device_remove_file(&of_dev->dev, &dev_attr_gpu_limit);
547 device_remove_file(&of_dev->dev, &dev_attr_limit_adjust);
548 device_remove_file(&of_dev->dev, &dev_attr_specified_fan_speed);
549 device_remove_file(&of_dev->dev, &dev_attr_cpu_fan_speed);
550 if(therm_type == ADT7460)
551 device_remove_file(&of_dev->dev, &dev_attr_gpu_fan_speed);
552 of_device_unregister(of_dev);
554 i2c_del_driver(&thermostat_driver);
557 module_init(thermostat_init);
558 module_exit(thermostat_exit);