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>
31 #include <linux/kthread.h>
35 #define CONFIG_REG 0x40
36 #define MANUAL_MASK 0xe0
37 #define AUTO_MASK 0x20
39 static u8 TEMP_REG
[3] = {0x26, 0x25, 0x27}; /* local, cpu, gpu */
40 static u8 LIMIT_REG
[3] = {0x6b, 0x6a, 0x6c}; /* local, cpu, gpu */
41 static u8 MANUAL_MODE
[2] = {0x5c, 0x5d};
42 static u8 REM_CONTROL
[2] = {0x00, 0x40};
43 static u8 FAN_SPEED
[2] = {0x28, 0x2a};
44 static u8 FAN_SPD_SET
[2] = {0x30, 0x31};
46 static u8 default_limits_local
[3] = {70, 50, 70}; /* local, cpu, gpu */
47 static u8 default_limits_chip
[3] = {80, 65, 80}; /* local, cpu, gpu */
49 static int limit_adjust
= 0;
50 static int fan_speed
= -1;
52 MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
53 MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and Powerbook G4 Alu");
54 MODULE_LICENSE("GPL");
56 MODULE_PARM(limit_adjust
,"i");
57 MODULE_PARM_DESC(limit_adjust
,"Adjust maximum temperatures (50 cpu, 70 gpu) by N degrees.");
58 MODULE_PARM(fan_speed
,"i");
59 MODULE_PARM_DESC(fan_speed
,"Specify fan speed (0-255) when lim < temp < lim+8 (default 128)");
62 struct i2c_client clt
;
70 static enum {ADT7460
, ADT7467
} therm_type
;
71 static int therm_bus
, therm_address
;
72 static struct of_device
* of_dev
;
73 static struct thermostat
* thermostat
;
74 static struct task_struct
*thread_therm
= NULL
;
76 static int attach_one_thermostat(struct i2c_adapter
*adapter
, int addr
, int busno
);
77 static void write_both_fan_speed(struct thermostat
*th
, int speed
);
78 static void write_fan_speed(struct thermostat
*th
, int speed
, int fan
);
81 write_reg(struct thermostat
* th
, int reg
, u8 data
)
88 rc
= i2c_master_send(&th
->clt
, (const char *)tmp
, 2);
97 read_reg(struct thermostat
* th
, int reg
)
103 rc
= i2c_master_send(&th
->clt
, ®_addr
, 1);
108 rc
= i2c_master_recv(&th
->clt
, (char *)&data
, 1);
115 attach_thermostat(struct i2c_adapter
*adapter
)
117 unsigned long bus_no
;
119 if (strncmp(adapter
->name
, "uni-n", 5))
121 bus_no
= simple_strtoul(adapter
->name
+ 6, NULL
, 10);
122 if (bus_no
!= therm_bus
)
124 return attach_one_thermostat(adapter
, therm_address
, bus_no
);
128 detach_thermostat(struct i2c_adapter
*adapter
)
130 struct thermostat
* th
;
133 if (thermostat
== NULL
)
138 if (thread_therm
!= NULL
) {
139 kthread_stop(thread_therm
);
142 printk(KERN_INFO
"adt746x: Putting max temperatures back from %d, %d, %d,"
144 th
->limits
[0], th
->limits
[1], th
->limits
[2],
145 th
->initial_limits
[0], th
->initial_limits
[1], th
->initial_limits
[2]);
147 for (i
= 0; i
< 3; i
++)
148 write_reg(th
, LIMIT_REG
[i
], th
->initial_limits
[i
]);
150 write_both_fan_speed(th
, -1);
152 i2c_detach_client(&th
->clt
);
161 static struct i2c_driver thermostat_driver
= {
162 .name
="Apple Thermostat ADT746x",
164 .flags
=I2C_DF_NOTIFY
,
165 .attach_adapter
=&attach_thermostat
,
166 .detach_adapter
=&detach_thermostat
,
169 static int read_fan_speed(struct thermostat
*th
, u8 addr
)
174 /* should start with low byte */
175 tmp
[1] = read_reg(th
, addr
);
176 tmp
[0] = read_reg(th
, addr
+ 1);
178 res
= tmp
[1] + (tmp
[0] << 8);
179 /* "a value of 0xffff means that the fan has stopped" */
180 return (res
== 0xffff ? 0 : (90000*60)/res
);
183 static void write_both_fan_speed(struct thermostat
*th
, int speed
)
185 write_fan_speed(th
, speed
, 0);
186 if (therm_type
== ADT7460
)
187 write_fan_speed(th
, speed
, 1);
190 static void write_fan_speed(struct thermostat
*th
, int speed
, int fan
)
199 if (therm_type
== ADT7467
&& fan
== 1)
202 if (th
->last_speed
[fan
] != speed
) {
204 printk(KERN_INFO
"adt746x: Setting speed to: automatic for %s fan.\n",
207 printk(KERN_INFO
"adt746x: Setting speed to: %d for %s fan.\n",
208 speed
, fan
?"GPU":"CPU");
213 manual
= read_reg(th
, MANUAL_MODE
[fan
]);
214 write_reg(th
, MANUAL_MODE
[fan
], manual
|MANUAL_MASK
);
215 write_reg(th
, FAN_SPD_SET
[fan
], speed
);
217 /* back to automatic */
218 if(therm_type
== ADT7460
) {
219 manual
= read_reg(th
, MANUAL_MODE
[fan
]) & (~MANUAL_MASK
);
220 write_reg(th
, MANUAL_MODE
[fan
], manual
|REM_CONTROL
[fan
]);
222 manual
= read_reg(th
, MANUAL_MODE
[fan
]);
223 write_reg(th
, MANUAL_MODE
[fan
], manual
&(~AUTO_MASK
));
227 th
->last_speed
[fan
] = speed
;
230 static int monitor_task(void *arg
)
232 struct thermostat
* th
= arg
;
239 while(!kthread_should_stop())
241 msleep_interruptible(2000);
248 if (fan_speed
!= -1) {
250 for (i
= 0; i
< 3; i
++) {
251 temps
[i
] = read_reg(th
, TEMP_REG
[i
]);
252 lims
[i
] = th
->limits
[i
];
257 if (fan_speed
!= -1) {
258 int lastvar
= 0; /* for iBook */
259 for (i
= 1; i
< 3; i
++) { /* we don't care about local sensor */
261 int fan_number
= (therm_type
== ADT7460
&& i
== 2);
262 int var
= temps
[i
] - lims
[i
];
264 if (th
->overriding
[fan_number
] == 0)
265 printk(KERN_INFO
"adt746x: Limit exceeded by %d, overriding specified fan speed for %s.\n",
266 var
, fan_number
?"GPU":"CPU");
267 th
->overriding
[fan_number
] = 1;
268 write_fan_speed(th
, 255, fan_number
);
270 } else if ((!th
->overriding
[fan_number
] || var
< 6) && var
> 0) {
271 if (th
->overriding
[fan_number
] == 1)
272 printk(KERN_INFO
"adt746x: Limit exceeded by %d, setting speed to specified for %s.\n",
273 var
, fan_number
?"GPU":"CPU");
274 th
->overriding
[fan_number
] = 0;
275 write_fan_speed(th
, fan_speed
, fan_number
);
277 } else if (var
< -1) {
278 /* don't stop iBook fan if GPU is cold and CPU is not
279 * so cold (lastvar >= -1) */
280 if (therm_type
== ADT7460
|| lastvar
< -1 || i
== 1) {
281 if (th
->last_speed
[fan_number
] != 0)
282 printk(KERN_INFO
"adt746x: Stopping %s fan.\n",
283 fan_number
?"GPU":"CPU");
284 write_fan_speed(th
, 0, fan_number
);
290 if (started
&& therm_type
== ADT7467
)
291 break; /* we don't want to re-stop the fan
292 * if CPU is heating and GPU is not */
296 mfan_speed
= read_fan_speed(th
, FAN_SPEED
[0]);
297 /* only one fan in the iBook G4 */
299 if (temps
[0] != th
->cached_temp
[0]
300 || temps
[1] != th
->cached_temp
[1]
301 || temps
[2] != th
->cached_temp
[2]) {
302 printk(KERN_INFO
"adt746x: Temperature infos:"
303 " thermostats: %d,%d,%d;"
305 " fan speed: %d RPM\n",
306 temps
[0], temps
[1], temps
[2],
307 lims
[0], lims
[1], lims
[2],
310 th
->cached_temp
[0] = temps
[0];
311 th
->cached_temp
[1] = temps
[1];
312 th
->cached_temp
[2] = temps
[2];
320 set_limit(struct thermostat
*th
, int i
)
322 /* Set CPU limit higher to avoid powerdowns */
323 th
->limits
[i
] = default_limits_chip
[i
] + limit_adjust
;
324 write_reg(th
, LIMIT_REG
[i
], th
->limits
[i
]);
326 /* set our limits to normal */
327 th
->limits
[i
] = default_limits_local
[i
] + limit_adjust
;
331 attach_one_thermostat(struct i2c_adapter
*adapter
, int addr
, int busno
)
333 struct thermostat
* th
;
339 th
= (struct thermostat
*)kmalloc(sizeof(struct thermostat
), GFP_KERNEL
);
342 memset(th
, 0, sizeof(*th
));
344 th
->clt
.adapter
= adapter
;
345 th
->clt
.driver
= &thermostat_driver
;
346 th
->clt
.id
= 0xDEAD7467;
347 strcpy(th
->clt
.name
, "thermostat");
349 rc
= read_reg(th
, 0);
351 printk(KERN_ERR
"adt746x: Thermostat failed to read config from bus %d !\n",
356 /* force manual control to start the fan quieter */
361 if(therm_type
== ADT7460
) {
362 printk(KERN_INFO
"adt746x: ADT7460 initializing\n");
363 /* The 7460 needs to be started explicitly */
364 write_reg(th
, CONFIG_REG
, 1);
366 printk(KERN_INFO
"adt746x: ADT7467 initializing\n");
368 for (i
= 0; i
< 3; i
++) {
369 th
->initial_limits
[i
] = read_reg(th
, LIMIT_REG
[i
]);
373 printk(KERN_INFO
"adt746x: Lowering max temperatures from %d, %d, %d"
375 th
->initial_limits
[0], th
->initial_limits
[1], th
->initial_limits
[2],
376 th
->limits
[0], th
->limits
[1], th
->limits
[2]);
380 if (i2c_attach_client(&th
->clt
)) {
381 printk(KERN_INFO
"adt746x: Thermostat failed to attach client !\n");
387 /* be sure to really write fan speed the first time */
388 th
->last_speed
[0] = -2;
389 th
->last_speed
[1] = -2;
391 if (fan_speed
!= -1) {
392 write_both_fan_speed(th
, 0);
394 write_both_fan_speed(th
, -1);
397 thread_therm
= kthread_run(monitor_task
, th
, "kfand");
399 if (thread_therm
== ERR_PTR(-ENOMEM
)) {
400 printk(KERN_INFO
"adt746x: Kthread creation failed\n");
409 * Now, unfortunately, sysfs doesn't give us a nice void * we could
410 * pass around to the attribute functions, so we don't really have
411 * choice but implement a bunch of them...
414 #define BUILD_SHOW_FUNC_INT(name, data) \
415 static ssize_t show_##name(struct device *dev, char *buf) \
417 return sprintf(buf, "%d\n", data); \
420 #define BUILD_SHOW_FUNC_FAN(name, data) \
421 static ssize_t show_##name(struct device *dev, char *buf) \
423 return sprintf(buf, "%d (%d rpm)\n", \
424 thermostat->last_speed[data], \
425 read_fan_speed(thermostat, FAN_SPEED[data]) \
429 #define BUILD_STORE_FUNC_DEG(name, data) \
430 static ssize_t store_##name(struct device *dev, const char *buf, size_t n) \
434 val = simple_strtol(buf, NULL, 10); \
435 printk(KERN_INFO "Adjusting limits by %d°C\n", val); \
436 limit_adjust = val; \
437 for (i=0; i < 3; i++) \
438 set_limit(thermostat, i); \
442 #define BUILD_STORE_FUNC_INT(name, data) \
443 static ssize_t store_##name(struct device *dev, const char *buf, size_t n) \
446 val = simple_strtoul(buf, NULL, 10); \
447 if (val < 0 || val > 255) \
449 printk(KERN_INFO "Setting specified fan speed to %d\n", val); \
454 BUILD_SHOW_FUNC_INT(cpu_temperature
, (read_reg(thermostat
, TEMP_REG
[1])))
455 BUILD_SHOW_FUNC_INT(gpu_temperature
, (read_reg(thermostat
, TEMP_REG
[2])))
456 BUILD_SHOW_FUNC_INT(cpu_limit
, thermostat
->limits
[1])
457 BUILD_SHOW_FUNC_INT(gpu_limit
, thermostat
->limits
[2])
459 BUILD_SHOW_FUNC_INT(specified_fan_speed
, fan_speed
)
460 BUILD_SHOW_FUNC_FAN(cpu_fan_speed
, 0)
461 BUILD_SHOW_FUNC_FAN(gpu_fan_speed
, 1)
463 BUILD_STORE_FUNC_INT(specified_fan_speed
,fan_speed
)
464 BUILD_SHOW_FUNC_INT(limit_adjust
, limit_adjust
)
465 BUILD_STORE_FUNC_DEG(limit_adjust
, thermostat
)
467 static DEVICE_ATTR(cpu_temperature
, S_IRUGO
,
468 show_cpu_temperature
,NULL
);
469 static DEVICE_ATTR(gpu_temperature
, S_IRUGO
,
470 show_gpu_temperature
,NULL
);
471 static DEVICE_ATTR(cpu_limit
, S_IRUGO
,
472 show_cpu_limit
, NULL
);
473 static DEVICE_ATTR(gpu_limit
, S_IRUGO
,
474 show_gpu_limit
, NULL
);
476 static DEVICE_ATTR(specified_fan_speed
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
,
477 show_specified_fan_speed
,store_specified_fan_speed
);
479 static DEVICE_ATTR(cpu_fan_speed
, S_IRUGO
,
480 show_cpu_fan_speed
, NULL
);
481 static DEVICE_ATTR(gpu_fan_speed
, S_IRUGO
,
482 show_gpu_fan_speed
, NULL
);
484 static DEVICE_ATTR(limit_adjust
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
,
485 show_limit_adjust
, store_limit_adjust
);
489 thermostat_init(void)
491 struct device_node
* np
;
494 np
= of_find_node_by_name(NULL
, "fan");
497 if (device_is_compatible(np
, "adt7460"))
498 therm_type
= ADT7460
;
499 else if (device_is_compatible(np
, "adt7467"))
500 therm_type
= ADT7467
;
504 prop
= (u32
*)get_property(np
, "reg", NULL
);
507 therm_bus
= ((*prop
) >> 8) & 0x0f;
508 therm_address
= ((*prop
) & 0xff) >> 1;
510 printk(KERN_INFO
"adt746x: Thermostat bus: %d, address: 0x%02x, limit_adjust: %d, fan_speed: %d\n",
511 therm_bus
, therm_address
, limit_adjust
, fan_speed
);
513 of_dev
= of_platform_device_create(np
, "temperatures");
515 if (of_dev
== NULL
) {
516 printk(KERN_ERR
"Can't register temperatures device !\n");
520 device_create_file(&of_dev
->dev
, &dev_attr_cpu_temperature
);
521 device_create_file(&of_dev
->dev
, &dev_attr_gpu_temperature
);
522 device_create_file(&of_dev
->dev
, &dev_attr_cpu_limit
);
523 device_create_file(&of_dev
->dev
, &dev_attr_gpu_limit
);
524 device_create_file(&of_dev
->dev
, &dev_attr_limit_adjust
);
525 device_create_file(&of_dev
->dev
, &dev_attr_specified_fan_speed
);
526 device_create_file(&of_dev
->dev
, &dev_attr_cpu_fan_speed
);
527 if(therm_type
== ADT7460
)
528 device_create_file(&of_dev
->dev
, &dev_attr_gpu_fan_speed
);
530 #ifndef CONFIG_I2C_KEYWEST
531 request_module("i2c-keywest");
534 return i2c_add_driver(&thermostat_driver
);
538 thermostat_exit(void)
541 device_remove_file(&of_dev
->dev
, &dev_attr_cpu_temperature
);
542 device_remove_file(&of_dev
->dev
, &dev_attr_gpu_temperature
);
543 device_remove_file(&of_dev
->dev
, &dev_attr_cpu_limit
);
544 device_remove_file(&of_dev
->dev
, &dev_attr_gpu_limit
);
545 device_remove_file(&of_dev
->dev
, &dev_attr_limit_adjust
);
546 device_remove_file(&of_dev
->dev
, &dev_attr_specified_fan_speed
);
547 device_remove_file(&of_dev
->dev
, &dev_attr_cpu_fan_speed
);
548 if(therm_type
== ADT7460
)
549 device_remove_file(&of_dev
->dev
, &dev_attr_gpu_fan_speed
);
550 of_device_unregister(of_dev
);
552 i2c_del_driver(&thermostat_driver
);
555 module_init(thermostat_init
);
556 module_exit(thermostat_exit
);