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/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>
30 #include <asm/machdep.h>
32 #include <asm/system.h>
33 #include <asm/sections.h>
37 #define CONFIG_REG 0x40
38 #define MANUAL_MASK 0xe0
39 #define AUTO_MASK 0x20
40 #define INVERT_MASK 0x10
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};
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];
53 static int limit_adjust
;
54 static int fan_speed
= -1;
57 MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
58 MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "
60 MODULE_LICENSE("GPL");
62 module_param(limit_adjust
, int, 0644);
63 MODULE_PARM_DESC(limit_adjust
,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
66 module_param(fan_speed
, int, 0644);
67 MODULE_PARM_DESC(fan_speed
,"Specify starting fan speed (0-255) "
70 module_param(verbose
, bool, 0);
71 MODULE_PARM_DESC(verbose
,"Verbose log operations "
75 struct i2c_client
*clt
;
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
;
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
);
94 write_reg(struct thermostat
* th
, int reg
, u8 data
)
101 rc
= i2c_master_send(th
->clt
, (const char *)tmp
, 2);
110 read_reg(struct thermostat
* th
, int reg
)
116 rc
= i2c_master_send(th
->clt
, ®_addr
, 1);
121 rc
= i2c_master_recv(th
->clt
, (char *)&data
, 1);
127 static struct i2c_driver thermostat_driver
;
130 attach_thermostat(struct i2c_adapter
*adapter
)
132 unsigned long bus_no
;
133 struct i2c_board_info info
;
134 struct i2c_client
*client
;
136 if (strncmp(adapter
->name
, "uni-n", 5))
138 bus_no
= simple_strtoul(adapter
->name
+ 6, NULL
, 10);
139 if (bus_no
!= therm_bus
)
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
);
150 * Let i2c-core delete that device on driver removal.
151 * This is safe because i2c-core holds the core_lock mutex for us.
153 list_add_tail(&client
->detected
, &thermostat_driver
.clients
);
158 remove_thermostat(struct i2c_client
*client
)
160 struct thermostat
*th
= i2c_get_clientdata(client
);
163 if (thread_therm
!= NULL
) {
164 kthread_stop(thread_therm
);
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]);
173 for (i
= 0; i
< 3; i
++)
174 write_reg(th
, LIMIT_REG
[i
], th
->initial_limits
[i
]);
176 write_both_fan_speed(th
, -1);
185 static int read_fan_speed(struct thermostat
*th
, u8 addr
)
190 /* should start with low byte */
191 tmp
[1] = read_reg(th
, addr
);
192 tmp
[0] = read_reg(th
, addr
+ 1);
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
);
199 static void write_both_fan_speed(struct thermostat
*th
, int speed
)
201 write_fan_speed(th
, speed
, 0);
202 if (therm_type
== ADT7460
)
203 write_fan_speed(th
, speed
, 1);
206 static void write_fan_speed(struct thermostat
*th
, int speed
, int fan
)
215 if (therm_type
== ADT7467
&& fan
== 1)
218 if (th
->last_speed
[fan
] != speed
) {
221 printk(KERN_DEBUG
"adt746x: Setting speed to automatic "
222 "for %s fan.\n", sensor_location
[fan
+1]);
224 printk(KERN_DEBUG
"adt746x: Setting speed to %d "
225 "for %s fan.\n", speed
, sensor_location
[fan
+1]);
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
);
236 /* back to automatic */
237 if(therm_type
== ADT7460
) {
238 manual
= read_reg(th
,
239 MANUAL_MODE
[fan
]) & (~MANUAL_MASK
);
242 MANUAL_MODE
[fan
], manual
|REM_CONTROL
[fan
]);
244 manual
= read_reg(th
, MANUAL_MODE
[fan
]);
245 write_reg(th
, MANUAL_MODE
[fan
], manual
&(~AUTO_MASK
));
249 th
->last_speed
[fan
] = speed
;
252 static void read_sensors(struct thermostat
*th
)
256 for (i
= 0; i
< 3; i
++)
257 th
->temps
[i
] = read_reg(th
, TEMP_REG
[i
]);
261 static void display_stats(struct thermostat
*th
)
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;"
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]));
274 th
->cached_temp
[0] = th
->temps
[0];
275 th
->cached_temp
[1] = th
->temps
[1];
276 th
->cached_temp
[2] = th
->temps
[2];
280 static void update_fans_speed (struct thermostat
*th
)
282 int lastvar
= 0; /* last variation, for iBook */
285 /* we don't care about local sensor, so we start at sensor 1 */
286 for (i
= 1; i
< 3; i
++) {
288 int fan_number
= (therm_type
== ADT7460
&& i
== 2);
289 int var
= th
->temps
[i
] - th
->limits
[i
];
292 int step
= (255 - fan_speed
) / 7;
295 /* hysteresis : change fan speed only if variation is
296 * more than two degrees */
297 if (abs(var
- th
->last_var
[fan_number
]) < 2)
301 new_speed
= fan_speed
+ ((var
-1)*step
);
303 if (new_speed
< fan_speed
)
304 new_speed
= fan_speed
;
309 printk(KERN_DEBUG
"adt746x: Setting fans speed to %d "
310 "(limit exceeded by %d on %s) \n",
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)
321 printk(KERN_DEBUG
"adt746x: Stopping "
323 write_both_fan_speed(th
, 0);
330 return; /* we don't want to re-stop the fan
331 * if sensor1 is heating and sensor2 is not */
335 static int monitor_task(void *arg
)
337 struct thermostat
* th
= arg
;
340 while(!kthread_should_stop()) {
342 msleep_interruptible(2000);
352 update_fans_speed(th
);
363 static void set_limit(struct thermostat
*th
, int i
)
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
]);
369 /* set our limits to normal */
370 th
->limits
[i
] = default_limits_local
[i
] + limit_adjust
;
373 static int probe_thermostat(struct i2c_client
*client
,
374 const struct i2c_device_id
*id
)
376 struct thermostat
* th
;
383 th
= kzalloc(sizeof(struct thermostat
), GFP_KERNEL
);
387 i2c_set_clientdata(client
, th
);
390 rc
= read_reg(th
, 0);
392 dev_err(&client
->dev
, "Thermostat failed to read config!\n");
397 /* force manual control to start the fan quieter */
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);
406 printk(KERN_INFO
"adt746x: ADT7467 initializing\n");
408 for (i
= 0; i
< 3; i
++) {
409 th
->initial_limits
[i
] = read_reg(th
, LIMIT_REG
[i
]);
413 printk(KERN_INFO
"adt746x: Lowering max temperatures from %d, %d, %d"
415 th
->initial_limits
[0], th
->initial_limits
[1],
416 th
->initial_limits
[2], th
->limits
[0], th
->limits
[1],
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;
427 if (fan_speed
!= -1) {
428 /* manual mode, stop fans */
429 write_both_fan_speed(th
, 0);
432 write_both_fan_speed(th
, -1);
435 thread_therm
= kthread_run(monitor_task
, th
, "kfand");
437 if (thread_therm
== ERR_PTR(-ENOMEM
)) {
438 printk(KERN_INFO
"adt746x: Kthread creation failed\n");
446 static const struct i2c_device_id therm_adt746x_id
[] = {
447 { "therm_adt746x", 0 },
451 static struct i2c_driver thermostat_driver
= {
453 .name
= "therm_adt746x",
455 .attach_adapter
= attach_thermostat
,
456 .probe
= probe_thermostat
,
457 .remove
= remove_thermostat
,
458 .id_table
= therm_adt746x_id
,
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...
466 * FIXME, it does now...
468 #define BUILD_SHOW_FUNC_INT(name, data) \
469 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
471 return sprintf(buf, "%d\n", data); \
474 #define BUILD_SHOW_FUNC_STR(name, data) \
475 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
477 return sprintf(buf, "%s\n", data); \
480 #define BUILD_SHOW_FUNC_FAN(name, data) \
481 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
483 return sprintf(buf, "%d (%d rpm)\n", \
484 thermostat->last_speed[data], \
485 read_fan_speed(thermostat, FAN_SPEED[data]) \
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) \
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); \
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) \
506 val = simple_strtol(buf, NULL, 10); \
507 if (val < 0 || val > 255) \
509 printk(KERN_INFO "Setting specified fan speed to %d\n", val); \
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])
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)
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
)
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
);
542 static DEVICE_ATTR(specified_fan_speed
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
,
543 show_specified_fan_speed
,store_specified_fan_speed
);
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
);
550 static DEVICE_ATTR(limit_adjust
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
,
551 show_limit_adjust
, store_limit_adjust
);
555 thermostat_init(void)
557 struct device_node
* np
;
559 int i
= 0, offset
= 0;
562 np
= of_find_node_by_name(NULL
, "fan");
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
;
574 prop
= of_get_property(np
, "hwsensor-params-version", NULL
);
575 printk(KERN_INFO
"adt746x: version %d (%ssupported)\n", *prop
,
576 (*prop
== 1)?"":"un");
582 prop
= of_get_property(np
, "reg", NULL
);
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';
593 therm_bus
= ((*prop
) >> 8) & 0x0f;
596 therm_address
= ((*prop
) & 0xff) >> 1;
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
);
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
;
607 if (sensor_location
[i
] == NULL
)
608 sensor_location
[i
] = "";
610 printk(KERN_INFO
"sensor %d: %s\n", i
, sensor_location
[i
]);
611 offset
+= strlen(sensor_location
[i
]) + 1;
614 sensor_location
[0] = "?";
615 sensor_location
[1] = "?";
616 sensor_location
[2] = "?";
619 of_dev
= of_platform_device_create(np
, "temperatures", NULL
);
622 if (of_dev
== NULL
) {
623 printk(KERN_ERR
"Can't register temperatures device !\n");
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
);
640 "Failed to create tempertaure attribute file(s).\n");
642 #ifndef CONFIG_I2C_POWERMAC
643 request_module("i2c-powermac");
646 return i2c_add_driver(&thermostat_driver
);
650 thermostat_exit(void)
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
);
663 if(therm_type
== ADT7460
)
664 device_remove_file(&of_dev
->dev
,
665 &dev_attr_sensor2_fan_speed
);
667 of_device_unregister(of_dev
);
669 i2c_del_driver(&thermostat_driver
);
672 module_init(thermostat_init
);
673 module_exit(thermostat_exit
);