therm_adt746x.c revision d05c7a80cf39ae7d0f8d0c3e47c93d51fcd393d3
1/*
2 * Device driver for the i2c thermostat found on the iBook G4, Albook G4
3 *
4 * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt
5 *
6 * Documentation from
7 * http://www.analog.com/UploadedFiles/Data_Sheets/115254175ADT7467_pra.pdf
8 * http://www.analog.com/UploadedFiles/Data_Sheets/3686221171167ADT7460_b.pdf
9 *
10 */
11
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/smp_lock.h>
23#include <linux/wait.h>
24#include <linux/suspend.h>
25#include <linux/kthread.h>
26#include <linux/moduleparam.h>
27#include <linux/freezer.h>
28
29#include <asm/prom.h>
30#include <asm/machdep.h>
31#include <asm/io.h>
32#include <asm/system.h>
33#include <asm/sections.h>
34#include <asm/of_platform.h>
35
36#undef DEBUG
37
38#define CONFIG_REG   0x40
39#define MANUAL_MASK  0xe0
40#define AUTO_MASK    0x20
41
42static u8 TEMP_REG[3]    = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */
43static u8 LIMIT_REG[3]   = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */
44static u8 MANUAL_MODE[2] = {0x5c, 0x5d};
45static u8 REM_CONTROL[2] = {0x00, 0x40};
46static u8 FAN_SPEED[2]   = {0x28, 0x2a};
47static u8 FAN_SPD_SET[2] = {0x30, 0x31};
48
49static u8 default_limits_local[3] = {70, 50, 70};    /* local, sensor1, sensor2 */
50static u8 default_limits_chip[3] = {80, 65, 80};    /* local, sensor1, sensor2 */
51static const char *sensor_location[3];
52
53static int limit_adjust;
54static int fan_speed = -1;
55static int verbose;
56
57MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
58MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "
59		   "Powerbook G4 Alu");
60MODULE_LICENSE("GPL");
61
62module_param(limit_adjust, int, 0644);
63MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
64		 "by N degrees.");
65
66module_param(fan_speed, int, 0644);
67MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) "
68		 "(default 64)");
69
70module_param(verbose, bool, 0);
71MODULE_PARM_DESC(verbose,"Verbose log operations "
72		 "(default 0)");
73
74struct thermostat {
75	struct i2c_client	clt;
76	u8			temps[3];
77	u8			cached_temp[3];
78	u8			initial_limits[3];
79	u8			limits[3];
80	int			last_speed[2];
81	int			last_var[2];
82};
83
84static enum {ADT7460, ADT7467} therm_type;
85static int therm_bus, therm_address;
86static struct of_device * of_dev;
87static struct thermostat* thermostat;
88static struct task_struct *thread_therm = NULL;
89
90static int attach_one_thermostat(struct i2c_adapter *adapter, int addr,
91				 int busno);
92
93static void write_both_fan_speed(struct thermostat *th, int speed);
94static void write_fan_speed(struct thermostat *th, int speed, int fan);
95
96static int
97write_reg(struct thermostat* th, int reg, u8 data)
98{
99	u8 tmp[2];
100	int rc;
101
102	tmp[0] = reg;
103	tmp[1] = data;
104	rc = i2c_master_send(&th->clt, (const char *)tmp, 2);
105	if (rc < 0)
106		return rc;
107	if (rc != 2)
108		return -ENODEV;
109	return 0;
110}
111
112static int
113read_reg(struct thermostat* th, int reg)
114{
115	u8 reg_addr, data;
116	int rc;
117
118	reg_addr = (u8)reg;
119	rc = i2c_master_send(&th->clt, &reg_addr, 1);
120	if (rc < 0)
121		return rc;
122	if (rc != 1)
123		return -ENODEV;
124	rc = i2c_master_recv(&th->clt, (char *)&data, 1);
125	if (rc < 0)
126		return rc;
127	return data;
128}
129
130static int
131attach_thermostat(struct i2c_adapter *adapter)
132{
133	unsigned long bus_no;
134
135	if (strncmp(adapter->name, "uni-n", 5))
136		return -ENODEV;
137	bus_no = simple_strtoul(adapter->name + 6, NULL, 10);
138	if (bus_no != therm_bus)
139		return -ENODEV;
140	return attach_one_thermostat(adapter, therm_address, bus_no);
141}
142
143static int
144detach_thermostat(struct i2c_adapter *adapter)
145{
146	struct thermostat* th;
147	int i;
148
149	if (thermostat == NULL)
150		return 0;
151
152	th = thermostat;
153
154	if (thread_therm != NULL) {
155		kthread_stop(thread_therm);
156	}
157
158	printk(KERN_INFO "adt746x: Putting max temperatures back from "
159			 "%d, %d, %d to %d, %d, %d\n",
160		th->limits[0], th->limits[1], th->limits[2],
161		th->initial_limits[0], th->initial_limits[1],
162		th->initial_limits[2]);
163
164	for (i = 0; i < 3; i++)
165		write_reg(th, LIMIT_REG[i], th->initial_limits[i]);
166
167	write_both_fan_speed(th, -1);
168
169	i2c_detach_client(&th->clt);
170
171	thermostat = NULL;
172
173	kfree(th);
174
175	return 0;
176}
177
178static struct i2c_driver thermostat_driver = {
179	.driver = {
180		.name	= "therm_adt746x",
181	},
182	.attach_adapter	= attach_thermostat,
183	.detach_adapter	= detach_thermostat,
184};
185
186static int read_fan_speed(struct thermostat *th, u8 addr)
187{
188	u8 tmp[2];
189	u16 res;
190
191	/* should start with low byte */
192	tmp[1] = read_reg(th, addr);
193	tmp[0] = read_reg(th, addr + 1);
194
195	res = tmp[1] + (tmp[0] << 8);
196	/* "a value of 0xffff means that the fan has stopped" */
197	return (res == 0xffff ? 0 : (90000*60)/res);
198}
199
200static void write_both_fan_speed(struct thermostat *th, int speed)
201{
202	write_fan_speed(th, speed, 0);
203	if (therm_type == ADT7460)
204		write_fan_speed(th, speed, 1);
205}
206
207static void write_fan_speed(struct thermostat *th, int speed, int fan)
208{
209	u8 manual;
210
211	if (speed > 0xff)
212		speed = 0xff;
213	else if (speed < -1)
214		speed = 0;
215
216	if (therm_type == ADT7467 && fan == 1)
217		return;
218
219	if (th->last_speed[fan] != speed) {
220		if (verbose) {
221			if (speed == -1)
222				printk(KERN_DEBUG "adt746x: Setting speed to automatic "
223					"for %s fan.\n", sensor_location[fan+1]);
224			else
225				printk(KERN_DEBUG "adt746x: Setting speed to %d "
226					"for %s fan.\n", speed, sensor_location[fan+1]);
227		}
228	} else
229		return;
230
231	if (speed >= 0) {
232		manual = read_reg(th, MANUAL_MODE[fan]);
233		write_reg(th, MANUAL_MODE[fan], manual|MANUAL_MASK);
234		write_reg(th, FAN_SPD_SET[fan], speed);
235	} else {
236		/* back to automatic */
237		if(therm_type == ADT7460) {
238			manual = read_reg(th,
239				MANUAL_MODE[fan]) & (~MANUAL_MASK);
240
241			write_reg(th,
242				MANUAL_MODE[fan], manual|REM_CONTROL[fan]);
243		} else {
244			manual = read_reg(th, MANUAL_MODE[fan]);
245			write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK));
246		}
247	}
248
249	th->last_speed[fan] = speed;
250}
251
252static void read_sensors(struct thermostat *th)
253{
254	int i = 0;
255
256	for (i = 0; i < 3; i++)
257		th->temps[i]  = read_reg(th, TEMP_REG[i]);
258}
259
260#ifdef DEBUG
261static void display_stats(struct thermostat *th)
262{
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;"
268				 " limits: %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]));
273	}
274	th->cached_temp[0] = th->temps[0];
275	th->cached_temp[1] = th->temps[1];
276	th->cached_temp[2] = th->temps[2];
277}
278#endif
279
280static void update_fans_speed (struct thermostat *th)
281{
282	int lastvar = 0; /* last variation, for iBook */
283	int i = 0;
284
285	/* we don't care about local sensor, so we start at sensor 1 */
286	for (i = 1; i < 3; i++) {
287		int started = 0;
288		int fan_number = (therm_type == ADT7460 && i == 2);
289		int var = th->temps[i] - th->limits[i];
290
291		if (var > -1) {
292			int step = (255 - fan_speed) / 7;
293			int new_speed = 0;
294
295			/* hysteresis : change fan speed only if variation is
296			 * more than two degrees */
297			if (abs(var - th->last_var[fan_number]) < 2)
298				continue;
299
300			started = 1;
301			new_speed = fan_speed + ((var-1)*step);
302
303			if (new_speed < fan_speed)
304				new_speed = fan_speed;
305			if (new_speed > 255)
306				new_speed = 255;
307
308			if (verbose)
309				printk(KERN_DEBUG "adt746x: Setting fans speed to %d "
310						 "(limit exceeded by %d on %s) \n",
311						new_speed, var,
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)
320					if (verbose)
321						printk(KERN_DEBUG "adt746x: Stopping "
322							"fans.\n");
323				write_both_fan_speed(th, 0);
324			}
325		}
326
327		lastvar = var;
328
329		if (started)
330			return; /* we don't want to re-stop the fan
331				* if sensor1 is heating and sensor2 is not */
332	}
333}
334
335static int monitor_task(void *arg)
336{
337	struct thermostat* th = arg;
338
339	while(!kthread_should_stop()) {
340		try_to_freeze();
341		msleep_interruptible(2000);
342
343#ifndef DEBUG
344		if (fan_speed != -1)
345			read_sensors(th);
346#else
347		read_sensors(th);
348#endif
349
350		if (fan_speed != -1)
351			update_fans_speed(th);
352
353#ifdef DEBUG
354		display_stats(th);
355#endif
356
357	}
358
359	return 0;
360}
361
362static void set_limit(struct thermostat *th, int i)
363{
364		/* Set sensor1 limit higher to avoid powerdowns */
365		th->limits[i] = default_limits_chip[i] + limit_adjust;
366		write_reg(th, LIMIT_REG[i], th->limits[i]);
367
368		/* set our limits to normal */
369		th->limits[i] = default_limits_local[i] + limit_adjust;
370}
371
372static int attach_one_thermostat(struct i2c_adapter *adapter, int addr,
373				 int busno)
374{
375	struct thermostat* th;
376	int rc;
377	int i;
378
379	if (thermostat)
380		return 0;
381
382	th = (struct thermostat *)
383		kmalloc(sizeof(struct thermostat), GFP_KERNEL);
384
385	if (!th)
386		return -ENOMEM;
387
388	memset(th, 0, sizeof(*th));
389	th->clt.addr = addr;
390	th->clt.adapter = adapter;
391	th->clt.driver = &thermostat_driver;
392	strcpy(th->clt.name, "thermostat");
393
394	rc = read_reg(th, 0);
395	if (rc < 0) {
396		printk(KERN_ERR "adt746x: Thermostat failed to read config "
397				"from bus %d !\n",
398				busno);
399		kfree(th);
400		return -ENODEV;
401	}
402
403	/* force manual control to start the fan quieter */
404	if (fan_speed == -1)
405		fan_speed = 64;
406
407	if(therm_type == ADT7460) {
408		printk(KERN_INFO "adt746x: ADT7460 initializing\n");
409		/* The 7460 needs to be started explicitly */
410		write_reg(th, CONFIG_REG, 1);
411	} else
412		printk(KERN_INFO "adt746x: ADT7467 initializing\n");
413
414	for (i = 0; i < 3; i++) {
415		th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
416		set_limit(th, i);
417	}
418
419	printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"
420			 " to %d, %d, %d\n",
421			 th->initial_limits[0], th->initial_limits[1],
422			 th->initial_limits[2], th->limits[0], th->limits[1],
423			 th->limits[2]);
424
425	thermostat = th;
426
427	if (i2c_attach_client(&th->clt)) {
428		printk(KERN_INFO "adt746x: Thermostat failed to attach "
429				 "client !\n");
430		thermostat = NULL;
431		kfree(th);
432		return -ENODEV;
433	}
434
435	/* be sure to really write fan speed the first time */
436	th->last_speed[0] = -2;
437	th->last_speed[1] = -2;
438	th->last_var[0] = -80;
439	th->last_var[1] = -80;
440
441	if (fan_speed != -1) {
442		/* manual mode, stop fans */
443		write_both_fan_speed(th, 0);
444	} else {
445		/* automatic mode */
446		write_both_fan_speed(th, -1);
447	}
448
449	thread_therm = kthread_run(monitor_task, th, "kfand");
450
451	if (thread_therm == ERR_PTR(-ENOMEM)) {
452		printk(KERN_INFO "adt746x: Kthread creation failed\n");
453		thread_therm = NULL;
454		return -ENOMEM;
455	}
456
457	return 0;
458}
459
460/*
461 * Now, unfortunately, sysfs doesn't give us a nice void * we could
462 * pass around to the attribute functions, so we don't really have
463 * choice but implement a bunch of them...
464 *
465 * FIXME, it does now...
466 */
467#define BUILD_SHOW_FUNC_INT(name, data)				\
468static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)	\
469{								\
470	return sprintf(buf, "%d\n", data);			\
471}
472
473#define BUILD_SHOW_FUNC_STR(name, data)				\
474static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
475{								\
476	return sprintf(buf, "%s\n", data);			\
477}
478
479#define BUILD_SHOW_FUNC_FAN(name, data)				\
480static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
481{								\
482	return sprintf(buf, "%d (%d rpm)\n", 			\
483		thermostat->last_speed[data],			\
484		read_fan_speed(thermostat, FAN_SPEED[data])	\
485		);						\
486}
487
488#define BUILD_STORE_FUNC_DEG(name, data)			\
489static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
490{								\
491	int val;						\
492	int i;							\
493	val = simple_strtol(buf, NULL, 10);			\
494	printk(KERN_INFO "Adjusting limits by %d degrees\n", val);	\
495	limit_adjust = val;					\
496	for (i=0; i < 3; i++)					\
497		set_limit(thermostat, i);			\
498	return n;						\
499}
500
501#define BUILD_STORE_FUNC_INT(name, data)			\
502static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
503{								\
504	u32 val;						\
505	val = simple_strtoul(buf, NULL, 10);			\
506	if (val < 0 || val > 255)				\
507		return -EINVAL;					\
508	printk(KERN_INFO "Setting specified fan speed to %d\n", val);	\
509	data = val;						\
510	return n;						\
511}
512
513BUILD_SHOW_FUNC_INT(sensor1_temperature,	 (read_reg(thermostat, TEMP_REG[1])))
514BUILD_SHOW_FUNC_INT(sensor2_temperature,	 (read_reg(thermostat, TEMP_REG[2])))
515BUILD_SHOW_FUNC_INT(sensor1_limit,		 thermostat->limits[1])
516BUILD_SHOW_FUNC_INT(sensor2_limit,		 thermostat->limits[2])
517BUILD_SHOW_FUNC_STR(sensor1_location,		 sensor_location[1])
518BUILD_SHOW_FUNC_STR(sensor2_location,		 sensor_location[2])
519
520BUILD_SHOW_FUNC_INT(specified_fan_speed, fan_speed)
521BUILD_SHOW_FUNC_FAN(sensor1_fan_speed,	 0)
522BUILD_SHOW_FUNC_FAN(sensor2_fan_speed,	 1)
523
524BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)
525BUILD_SHOW_FUNC_INT(limit_adjust,	 limit_adjust)
526BUILD_STORE_FUNC_DEG(limit_adjust,	 thermostat)
527
528static DEVICE_ATTR(sensor1_temperature,	S_IRUGO,
529		   show_sensor1_temperature,NULL);
530static DEVICE_ATTR(sensor2_temperature,	S_IRUGO,
531		   show_sensor2_temperature,NULL);
532static DEVICE_ATTR(sensor1_limit, S_IRUGO,
533		   show_sensor1_limit,	NULL);
534static DEVICE_ATTR(sensor2_limit, S_IRUGO,
535		   show_sensor2_limit,	NULL);
536static DEVICE_ATTR(sensor1_location, S_IRUGO,
537		   show_sensor1_location, NULL);
538static DEVICE_ATTR(sensor2_location, S_IRUGO,
539		   show_sensor2_location, NULL);
540
541static DEVICE_ATTR(specified_fan_speed,	S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
542		   show_specified_fan_speed,store_specified_fan_speed);
543
544static DEVICE_ATTR(sensor1_fan_speed,	S_IRUGO,
545		   show_sensor1_fan_speed,	NULL);
546static DEVICE_ATTR(sensor2_fan_speed,	S_IRUGO,
547		   show_sensor2_fan_speed,	NULL);
548
549static DEVICE_ATTR(limit_adjust,	S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
550		   show_limit_adjust,	store_limit_adjust);
551
552
553static int __init
554thermostat_init(void)
555{
556	struct device_node* np;
557	const u32 *prop;
558	int i = 0, offset = 0;
559
560	np = of_find_node_by_name(NULL, "fan");
561	if (!np)
562		return -ENODEV;
563	if (device_is_compatible(np, "adt7460"))
564		therm_type = ADT7460;
565	else if (device_is_compatible(np, "adt7467"))
566		therm_type = ADT7467;
567	else
568		return -ENODEV;
569
570	prop = of_get_property(np, "hwsensor-params-version", NULL);
571	printk(KERN_INFO "adt746x: version %d (%ssupported)\n", *prop,
572			 (*prop == 1)?"":"un");
573	if (*prop != 1)
574		return -ENODEV;
575
576	prop = of_get_property(np, "reg", NULL);
577	if (!prop)
578		return -ENODEV;
579
580	/* look for bus either by path or using "reg" */
581	if (strstr(np->full_name, "/i2c-bus@") != NULL) {
582		const char *tmp_bus = (strstr(np->full_name, "/i2c-bus@") + 9);
583		therm_bus = tmp_bus[0]-'0';
584	} else {
585		therm_bus = ((*prop) >> 8) & 0x0f;
586	}
587
588	therm_address = ((*prop) & 0xff) >> 1;
589
590	printk(KERN_INFO "adt746x: Thermostat bus: %d, address: 0x%02x, "
591			 "limit_adjust: %d, fan_speed: %d\n",
592			 therm_bus, therm_address, limit_adjust, fan_speed);
593
594	if (of_get_property(np, "hwsensor-location", NULL)) {
595		for (i = 0; i < 3; i++) {
596			sensor_location[i] = of_get_property(np,
597					"hwsensor-location", NULL) + offset;
598
599			if (sensor_location[i] == NULL)
600				sensor_location[i] = "";
601
602			printk(KERN_INFO "sensor %d: %s\n", i, sensor_location[i]);
603			offset += strlen(sensor_location[i]) + 1;
604		}
605	} else {
606		sensor_location[0] = "?";
607		sensor_location[1] = "?";
608		sensor_location[2] = "?";
609	}
610
611	of_dev = of_platform_device_create(np, "temperatures", NULL);
612
613	if (of_dev == NULL) {
614		printk(KERN_ERR "Can't register temperatures device !\n");
615		return -ENODEV;
616	}
617
618	device_create_file(&of_dev->dev, &dev_attr_sensor1_temperature);
619	device_create_file(&of_dev->dev, &dev_attr_sensor2_temperature);
620	device_create_file(&of_dev->dev, &dev_attr_sensor1_limit);
621	device_create_file(&of_dev->dev, &dev_attr_sensor2_limit);
622	device_create_file(&of_dev->dev, &dev_attr_sensor1_location);
623	device_create_file(&of_dev->dev, &dev_attr_sensor2_location);
624	device_create_file(&of_dev->dev, &dev_attr_limit_adjust);
625	device_create_file(&of_dev->dev, &dev_attr_specified_fan_speed);
626	device_create_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);
627	if(therm_type == ADT7460)
628		device_create_file(&of_dev->dev, &dev_attr_sensor2_fan_speed);
629
630#ifndef CONFIG_I2C_POWERMAC
631	request_module("i2c-powermac");
632#endif
633
634	return i2c_add_driver(&thermostat_driver);
635}
636
637static void __exit
638thermostat_exit(void)
639{
640	if (of_dev) {
641		device_remove_file(&of_dev->dev, &dev_attr_sensor1_temperature);
642		device_remove_file(&of_dev->dev, &dev_attr_sensor2_temperature);
643		device_remove_file(&of_dev->dev, &dev_attr_sensor1_limit);
644		device_remove_file(&of_dev->dev, &dev_attr_sensor2_limit);
645		device_remove_file(&of_dev->dev, &dev_attr_sensor1_location);
646		device_remove_file(&of_dev->dev, &dev_attr_sensor2_location);
647		device_remove_file(&of_dev->dev, &dev_attr_limit_adjust);
648		device_remove_file(&of_dev->dev, &dev_attr_specified_fan_speed);
649		device_remove_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);
650
651		if(therm_type == ADT7460)
652			device_remove_file(&of_dev->dev,
653					   &dev_attr_sensor2_fan_speed);
654
655		of_device_unregister(of_dev);
656	}
657	i2c_del_driver(&thermostat_driver);
658}
659
660module_init(thermostat_init);
661module_exit(thermostat_exit);
662