Blame view

drivers/macintosh/therm_adt746x.c 17.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
  /*
   * Device driver for the i2c thermostat found on the iBook G4, Albook G4
   *
   * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt
   *
631dd1a88   Justin P. Mattock   Update broken web...
6
7
8
   * Documentation from 115254175ADT7467_pra.pdf and 3686221171167ADT7460_b.pdf
   * http://www.onsemi.com/PowerSolutions/product.do?id=ADT7467
   * http://www.onsemi.com/PowerSolutions/product.do?id=ADT7460
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
17
18
19
20
  #include <linux/types.h>
  #include <linux/module.h>
  #include <linux/errno.h>
  #include <linux/kernel.h>
  #include <linux/delay.h>
  #include <linux/sched.h>
  #include <linux/i2c.h>
  #include <linux/slab.h>
  #include <linux/init.h>
  #include <linux/spinlock.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
  #include <linux/wait.h>
  #include <linux/suspend.h>
  #include <linux/kthread.h>
  #include <linux/moduleparam.h>
7dfb71030   Nigel Cunningham   [PATCH] Add inclu...
25
  #include <linux/freezer.h>
ad9e05aef   Stephen Rothwell   macintosh: Use li...
26
  #include <linux/of_platform.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
32
  
  #include <asm/prom.h>
  #include <asm/machdep.h>
  #include <asm/io.h>
  #include <asm/system.h>
  #include <asm/sections.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
36
37
38
  
  #undef DEBUG
  
  #define CONFIG_REG   0x40
  #define MANUAL_MASK  0xe0
  #define AUTO_MASK    0x20
0512a9a8e   Michel Dänzer   therm_adt746x: Al...
39
  #define INVERT_MASK  0x10
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40

d20c507f2   Colin Leroy   [PATCH] therm_adt...
41
42
  static u8 TEMP_REG[3]    = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */
  static u8 LIMIT_REG[3]   = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
  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};
d20c507f2   Colin Leroy   [PATCH] therm_adt...
47
48
  static u8 default_limits_local[3] = {70, 50, 70};    /* local, sensor1, sensor2 */
  static u8 default_limits_chip[3] = {80, 65, 80};    /* local, sensor1, sensor2 */
872758563   Olaf Hering   [POWERPC] move va...
49
  static const char *sensor_location[3];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50

872758563   Olaf Hering   [POWERPC] move va...
51
  static int limit_adjust;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
  static int fan_speed = -1;
90ab5ee94   Rusty Russell   module_param: mak...
53
  static bool verbose;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
  
  MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
  MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "
  		   "Powerbook G4 Alu");
  MODULE_LICENSE("GPL");
  
  module_param(limit_adjust, int, 0644);
d20c507f2   Colin Leroy   [PATCH] therm_adt...
61
  MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
  		 "by N degrees.");
  
  module_param(fan_speed, int, 0644);
  MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) "
  		 "(default 64)");
9f6d4b0c2   Ben Collins   [PATCH] therm_adt...
67
68
69
  module_param(verbose, bool, 0);
  MODULE_PARM_DESC(verbose,"Verbose log operations "
  		 "(default 0)");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
  struct thermostat {
2e613e7f6   Jean Delvare   therm_adt746x: Co...
71
  	struct i2c_client	*clt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
76
77
  	u8			temps[3];
  	u8			cached_temp[3];
  	u8			initial_limits[3];
  	u8			limits[3];
  	int			last_speed[2];
  	int			last_var[2];
1496e89ae   Darrick J. Wong   powerpc/therm_adt...
78
  	int			pwm_inv[2];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
81
82
  };
  
  static enum {ADT7460, ADT7467} therm_type;
  static int therm_bus, therm_address;
2dc115813   Grant Likely   of/device: Replac...
83
  static struct platform_device * of_dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
  static struct thermostat* thermostat;
  static struct task_struct *thread_therm = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
  static void write_both_fan_speed(struct thermostat *th, int speed);
  static void write_fan_speed(struct thermostat *th, int speed, int fan);
33a470f6d   Jean Delvare   macintosh/therm_a...
88
89
  static void thermostat_create_files(void);
  static void thermostat_remove_files(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
94
95
96
97
98
  
  static int
  write_reg(struct thermostat* th, int reg, u8 data)
  {
  	u8 tmp[2];
  	int rc;
  	
  	tmp[0] = reg;
  	tmp[1] = data;
2e613e7f6   Jean Delvare   therm_adt746x: Co...
99
  	rc = i2c_master_send(th->clt, (const char *)tmp, 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  	if (rc < 0)
  		return rc;
  	if (rc != 2)
  		return -ENODEV;
  	return 0;
  }
  
  static int
  read_reg(struct thermostat* th, int reg)
  {
  	u8 reg_addr, data;
  	int rc;
  
  	reg_addr = (u8)reg;
2e613e7f6   Jean Delvare   therm_adt746x: Co...
114
  	rc = i2c_master_send(th->clt, &reg_addr, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
118
  	if (rc < 0)
  		return rc;
  	if (rc != 1)
  		return -ENODEV;
2e613e7f6   Jean Delvare   therm_adt746x: Co...
119
  	rc = i2c_master_recv(th->clt, (char *)&data, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
123
  	if (rc < 0)
  		return rc;
  	return data;
  }
6f6b35e13   Jean Delvare   macintosh: Don't ...
124
  static struct i2c_driver thermostat_driver;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
  static int
  attach_thermostat(struct i2c_adapter *adapter)
  {
  	unsigned long bus_no;
2e613e7f6   Jean Delvare   therm_adt746x: Co...
129
130
  	struct i2c_board_info info;
  	struct i2c_client *client;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
132
133
134
135
136
  
  	if (strncmp(adapter->name, "uni-n", 5))
  		return -ENODEV;
  	bus_no = simple_strtoul(adapter->name + 6, NULL, 10);
  	if (bus_no != therm_bus)
  		return -ENODEV;
2e613e7f6   Jean Delvare   therm_adt746x: Co...
137
138
139
140
141
142
143
144
145
146
147
148
  
  	memset(&info, 0, sizeof(struct i2c_board_info));
  	strlcpy(info.type, "therm_adt746x", I2C_NAME_SIZE);
  	info.addr = therm_address;
  	client = i2c_new_device(adapter, &info);
  	if (!client)
  		return -ENODEV;
  
  	/*
  	 * Let i2c-core delete that device on driver removal.
  	 * This is safe because i2c-core holds the core_lock mutex for us.
  	 */
6f6b35e13   Jean Delvare   macintosh: Don't ...
149
  	list_add_tail(&client->detected, &thermostat_driver.clients);
2e613e7f6   Jean Delvare   therm_adt746x: Co...
150
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
153
  }
  
  static int
2e613e7f6   Jean Delvare   therm_adt746x: Co...
154
  remove_thermostat(struct i2c_client *client)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  {
2e613e7f6   Jean Delvare   therm_adt746x: Co...
156
  	struct thermostat *th = i2c_get_clientdata(client);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
  	int i;
  	
33a470f6d   Jean Delvare   macintosh/therm_a...
159
  	thermostat_remove_files();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
  	if (thread_therm != NULL) {
  		kthread_stop(thread_therm);
  	}
9f6d4b0c2   Ben Collins   [PATCH] therm_adt...
163

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
165
166
167
168
169
  	printk(KERN_INFO "adt746x: Putting max temperatures back from "
  			 "%d, %d, %d to %d, %d, %d
  ",
  		th->limits[0], th->limits[1], th->limits[2],
  		th->initial_limits[0], th->initial_limits[1],
  		th->initial_limits[2]);
9f6d4b0c2   Ben Collins   [PATCH] therm_adt...
170

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
172
173
174
  	for (i = 0; i < 3; i++)
  		write_reg(th, LIMIT_REG[i], th->initial_limits[i]);
  
  	write_both_fan_speed(th, -1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
176
177
178
179
180
  	thermostat = NULL;
  
  	kfree(th);
  
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  static int read_fan_speed(struct thermostat *th, u8 addr)
  {
  	u8 tmp[2];
  	u16 res;
  	
  	/* should start with low byte */
  	tmp[1] = read_reg(th, addr);
  	tmp[0] = read_reg(th, addr + 1);
  	
  	res = tmp[1] + (tmp[0] << 8);
  	/* "a value of 0xffff means that the fan has stopped" */
  	return (res == 0xffff ? 0 : (90000*60)/res);
  }
  
  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;
  	
  	if (speed > 0xff) 
  		speed = 0xff;
  	else if (speed < -1) 
  		speed = 0;
  	
  	if (therm_type == ADT7467 && fan == 1)
  		return;
  	
  	if (th->last_speed[fan] != speed) {
9f6d4b0c2   Ben Collins   [PATCH] therm_adt...
215
216
217
218
219
220
221
222
223
224
  		if (verbose) {
  			if (speed == -1)
  				printk(KERN_DEBUG "adt746x: Setting speed to automatic "
  					"for %s fan.
  ", sensor_location[fan+1]);
  			else
  				printk(KERN_DEBUG "adt746x: Setting speed to %d "
  					"for %s fan.
  ", speed, sensor_location[fan+1]);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
226
227
228
229
  	} else
  		return;
  	
  	if (speed >= 0) {
  		manual = read_reg(th, MANUAL_MODE[fan]);
1496e89ae   Darrick J. Wong   powerpc/therm_adt...
230
  		manual &= ~INVERT_MASK;
0512a9a8e   Michel Dänzer   therm_adt746x: Al...
231
  		write_reg(th, MANUAL_MODE[fan],
1496e89ae   Darrick J. Wong   powerpc/therm_adt...
232
  			manual | MANUAL_MASK | th->pwm_inv[fan]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
234
235
236
237
238
  		write_reg(th, FAN_SPD_SET[fan], speed);
  	} else {
  		/* back to automatic */
  		if(therm_type == ADT7460) {
  			manual = read_reg(th,
  				MANUAL_MODE[fan]) & (~MANUAL_MASK);
1496e89ae   Darrick J. Wong   powerpc/therm_adt...
239
240
  			manual &= ~INVERT_MASK;
  			manual |= th->pwm_inv[fan];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
244
  			write_reg(th,
  				MANUAL_MODE[fan], manual|REM_CONTROL[fan]);
  		} else {
  			manual = read_reg(th, MANUAL_MODE[fan]);
1496e89ae   Darrick J. Wong   powerpc/therm_adt...
245
246
  			manual &= ~INVERT_MASK;
  			manual |= th->pwm_inv[fan];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  			write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK));
  		}
  	}
  	
  	th->last_speed[fan] = speed;			
  }
  
  static void read_sensors(struct thermostat *th)
  {
  	int i = 0;
  
  	for (i = 0; i < 3; i++)
  		th->temps[i]  = read_reg(th, TEMP_REG[i]);
  }
  
  #ifdef DEBUG
  static void display_stats(struct thermostat *th)
  {
  	if (th->temps[0] != th->cached_temp[0]
  	||  th->temps[1] != th->cached_temp[1]
  	||  th->temps[2] != th->cached_temp[2]) {
  		printk(KERN_INFO "adt746x: Temperature infos:"
  				 " thermostats: %d,%d,%d;"
  				 " limits: %d,%d,%d;"
  				 " fan speed: %d RPM
  ",
  				 th->temps[0], th->temps[1], th->temps[2],
  				 th->limits[0],  th->limits[1],  th->limits[2],
  				 read_fan_speed(th, FAN_SPEED[0]));
  	}
  	th->cached_temp[0] = th->temps[0];
  	th->cached_temp[1] = th->temps[1];
  	th->cached_temp[2] = th->temps[2];
  }
  #endif
  
  static void update_fans_speed (struct thermostat *th)
  {
  	int lastvar = 0; /* last variation, for iBook */
  	int i = 0;
  
  	/* we don't care about local sensor, so we start at sensor 1 */
  	for (i = 1; i < 3; i++) {
  		int started = 0;
  		int fan_number = (therm_type == ADT7460 && i == 2);
  		int var = th->temps[i] - th->limits[i];
  
  		if (var > -1) {
  			int step = (255 - fan_speed) / 7;
  			int new_speed = 0;
  
  			/* hysteresis : change fan speed only if variation is
  			 * more than two degrees */
  			if (abs(var - th->last_var[fan_number]) < 2)
  				continue;
  
  			started = 1;
  			new_speed = fan_speed + ((var-1)*step);
  
  			if (new_speed < fan_speed)
  				new_speed = fan_speed;
  			if (new_speed > 255)
  				new_speed = 255;
9f6d4b0c2   Ben Collins   [PATCH] therm_adt...
310
311
  			if (verbose)
  				printk(KERN_DEBUG "adt746x: Setting fans speed to %d "
8354be9c1   Frans Pop   powerpc: Remove t...
312
313
  						 "(limit exceeded by %d on %s)
  ",
9f6d4b0c2   Ben Collins   [PATCH] therm_adt...
314
315
  						new_speed, var,
  						sensor_location[fan_number+1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
317
318
  			write_both_fan_speed(th, new_speed);
  			th->last_var[fan_number] = var;
  		} else if (var < -2) {
d20c507f2   Colin Leroy   [PATCH] therm_adt...
319
  			/* don't stop fan if sensor2 is cold and sensor1 is not
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
  			 * so cold (lastvar >= -1) */
  			if (i == 2 && lastvar < -1) {
  				if (th->last_speed[fan_number] != 0)
9f6d4b0c2   Ben Collins   [PATCH] therm_adt...
323
324
325
326
  					if (verbose)
  						printk(KERN_DEBUG "adt746x: Stopping "
  							"fans.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
329
330
331
332
333
334
  				write_both_fan_speed(th, 0);
  			}
  		}
  
  		lastvar = var;
  
  		if (started)
  			return; /* we don't want to re-stop the fan
d20c507f2   Colin Leroy   [PATCH] therm_adt...
335
  				* if sensor1 is heating and sensor2 is not */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
340
341
  	}
  }
  
  static int monitor_task(void *arg)
  {
  	struct thermostat* th = arg;
831441862   Rafael J. Wysocki   Freezer: make ker...
342
  	set_freezable();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
  	while(!kthread_should_stop()) {
3e1d1d28d   Christoph Lameter   [PATCH] Cleanup p...
344
  		try_to_freeze();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  		msleep_interruptible(2000);
  
  #ifndef DEBUG
  		if (fan_speed != -1)
  			read_sensors(th);
  #else
  		read_sensors(th);
  #endif		
  
  		if (fan_speed != -1)
  			update_fans_speed(th);
  
  #ifdef DEBUG
  		display_stats(th);
  #endif
  
  	}
  
  	return 0;
  }
  
  static void set_limit(struct thermostat *th, int i)
  {
d20c507f2   Colin Leroy   [PATCH] therm_adt...
368
  		/* Set sensor1 limit higher to avoid powerdowns */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
372
373
374
  		th->limits[i] = default_limits_chip[i] + limit_adjust;
  		write_reg(th, LIMIT_REG[i], th->limits[i]);
  		
  		/* set our limits to normal */
  		th->limits[i] = default_limits_local[i] + limit_adjust;
  }
2e613e7f6   Jean Delvare   therm_adt746x: Co...
375
376
  static int probe_thermostat(struct i2c_client *client,
  			    const struct i2c_device_id *id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
378
379
380
381
382
383
  {
  	struct thermostat* th;
  	int rc;
  	int i;
  
  	if (thermostat)
  		return 0;
cc61f957f   Mariusz Kozlowski   [POWERPC] drivers...
384
  	th = kzalloc(sizeof(struct thermostat), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385
386
  	if (!th)
  		return -ENOMEM;
2e613e7f6   Jean Delvare   therm_adt746x: Co...
387
388
  	i2c_set_clientdata(client, th);
  	th->clt = client;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
389

99ca177ae   Jean Delvare   powerpc/therm_adt...
390
  	rc = read_reg(th, CONFIG_REG);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
  	if (rc < 0) {
2e613e7f6   Jean Delvare   therm_adt746x: Co...
392
393
  		dev_err(&client->dev, "Thermostat failed to read config!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
  		kfree(th);
  		return -ENODEV;
  	}
  
  	/* force manual control to start the fan quieter */
  	if (fan_speed == -1)
  		fan_speed = 64;
  	
  	if(therm_type == ADT7460) {
  		printk(KERN_INFO "adt746x: ADT7460 initializing
  ");
  		/* The 7460 needs to be started explicitly */
  		write_reg(th, CONFIG_REG, 1);
  	} else
  		printk(KERN_INFO "adt746x: ADT7467 initializing
  ");
  
  	for (i = 0; i < 3; i++) {
  		th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
  		set_limit(th, i);
  	}
9f6d4b0c2   Ben Collins   [PATCH] therm_adt...
415

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
417
418
419
420
421
422
423
  	printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"
  			 " to %d, %d, %d
  ",
  			 th->initial_limits[0], th->initial_limits[1],
  			 th->initial_limits[2], th->limits[0], th->limits[1],
  			 th->limits[2]);
  
  	thermostat = th;
1496e89ae   Darrick J. Wong   powerpc/therm_adt...
424
425
426
  	/* record invert bit status because fw can corrupt it after suspend */
  	th->pwm_inv[0] = read_reg(th, MANUAL_MODE[0]) & INVERT_MASK;
  	th->pwm_inv[1] = read_reg(th, MANUAL_MODE[1]) & INVERT_MASK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  	/* be sure to really write fan speed the first time */
  	th->last_speed[0] = -2;
  	th->last_speed[1] = -2;
  	th->last_var[0] = -80;
  	th->last_var[1] = -80;
  
  	if (fan_speed != -1) {
  		/* manual mode, stop fans */
  		write_both_fan_speed(th, 0);
  	} else {
  		/* automatic mode */
  		write_both_fan_speed(th, -1);
  	}
  	
  	thread_therm = kthread_run(monitor_task, th, "kfand");
  
  	if (thread_therm == ERR_PTR(-ENOMEM)) {
  		printk(KERN_INFO "adt746x: Kthread creation failed
  ");
  		thread_therm = NULL;
  		return -ENOMEM;
  	}
33a470f6d   Jean Delvare   macintosh/therm_a...
449
  	thermostat_create_files();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
  	return 0;
  }
2e613e7f6   Jean Delvare   therm_adt746x: Co...
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  static const struct i2c_device_id therm_adt746x_id[] = {
  	{ "therm_adt746x", 0 },
  	{ }
  };
  
  static struct i2c_driver thermostat_driver = {
  	.driver = {
  		.name	= "therm_adt746x",
  	},
  	.attach_adapter	= attach_thermostat,
  	.probe = probe_thermostat,
  	.remove = remove_thermostat,
  	.id_table = therm_adt746x_id,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
468
469
470
  /* 
   * Now, unfortunately, sysfs doesn't give us a nice void * we could
   * pass around to the attribute functions, so we don't really have
   * choice but implement a bunch of them...
   *
e404e274f   Yani Ioannou   [PATCH] Driver Co...
471
   * FIXME, it does now...
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
473
   */
  #define BUILD_SHOW_FUNC_INT(name, data)				\
e404e274f   Yani Ioannou   [PATCH] Driver Co...
474
  static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
476
477
478
  {								\
  	return sprintf(buf, "%d
  ", data);			\
  }
d20c507f2   Colin Leroy   [PATCH] therm_adt...
479
  #define BUILD_SHOW_FUNC_STR(name, data)				\
e404e274f   Yani Ioannou   [PATCH] Driver Co...
480
  static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
d20c507f2   Colin Leroy   [PATCH] therm_adt...
481
482
483
484
  {								\
  	return sprintf(buf, "%s
  ", data);			\
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
485
  #define BUILD_SHOW_FUNC_FAN(name, data)				\
e404e274f   Yani Ioannou   [PATCH] Driver Co...
486
  static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
488
489
490
491
492
493
494
495
  {								\
  	return sprintf(buf, "%d (%d rpm)
  ", 			\
  		thermostat->last_speed[data],			\
  		read_fan_speed(thermostat, FAN_SPEED[data])	\
  		);						\
  }
  
  #define BUILD_STORE_FUNC_DEG(name, data)			\
e404e274f   Yani Ioannou   [PATCH] Driver Co...
496
  static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
499
500
  {								\
  	int val;						\
  	int i;							\
  	val = simple_strtol(buf, NULL, 10);			\
d20c507f2   Colin Leroy   [PATCH] therm_adt...
501
502
  	printk(KERN_INFO "Adjusting limits by %d degrees
  ", val);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503
504
505
506
507
508
509
  	limit_adjust = val;					\
  	for (i=0; i < 3; i++)					\
  		set_limit(thermostat, i);			\
  	return n;						\
  }
  
  #define BUILD_STORE_FUNC_INT(name, data)			\
e404e274f   Yani Ioannou   [PATCH] Driver Co...
510
  static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
  {								\
2e74778c7   roel kluin   therm_adt746x: Fi...
512
513
  	int val;						\
  	val = simple_strtol(buf, NULL, 10);			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514
515
516
517
518
519
520
  	if (val < 0 || val > 255)				\
  		return -EINVAL;					\
  	printk(KERN_INFO "Setting specified fan speed to %d
  ", val);	\
  	data = val;						\
  	return n;						\
  }
d20c507f2   Colin Leroy   [PATCH] therm_adt...
521
522
523
524
525
526
  BUILD_SHOW_FUNC_INT(sensor1_temperature,	 (read_reg(thermostat, TEMP_REG[1])))
  BUILD_SHOW_FUNC_INT(sensor2_temperature,	 (read_reg(thermostat, TEMP_REG[2])))
  BUILD_SHOW_FUNC_INT(sensor1_limit,		 thermostat->limits[1])
  BUILD_SHOW_FUNC_INT(sensor2_limit,		 thermostat->limits[2])
  BUILD_SHOW_FUNC_STR(sensor1_location,		 sensor_location[1])
  BUILD_SHOW_FUNC_STR(sensor2_location,		 sensor_location[2])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
528
  
  BUILD_SHOW_FUNC_INT(specified_fan_speed, fan_speed)
d20c507f2   Colin Leroy   [PATCH] therm_adt...
529
530
  BUILD_SHOW_FUNC_FAN(sensor1_fan_speed,	 0)
  BUILD_SHOW_FUNC_FAN(sensor2_fan_speed,	 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531
532
533
534
535
  
  BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)
  BUILD_SHOW_FUNC_INT(limit_adjust,	 limit_adjust)
  BUILD_STORE_FUNC_DEG(limit_adjust,	 thermostat)
  		
d20c507f2   Colin Leroy   [PATCH] therm_adt...
536
537
538
539
540
541
542
543
544
545
546
547
  static DEVICE_ATTR(sensor1_temperature,	S_IRUGO,
  		   show_sensor1_temperature,NULL);
  static DEVICE_ATTR(sensor2_temperature,	S_IRUGO,
  		   show_sensor2_temperature,NULL);
  static DEVICE_ATTR(sensor1_limit, S_IRUGO,
  		   show_sensor1_limit,	NULL);
  static DEVICE_ATTR(sensor2_limit, S_IRUGO,
  		   show_sensor2_limit,	NULL);
  static DEVICE_ATTR(sensor1_location, S_IRUGO,
  		   show_sensor1_location, NULL);
  static DEVICE_ATTR(sensor2_location, S_IRUGO,
  		   show_sensor2_location, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
549
550
  
  static DEVICE_ATTR(specified_fan_speed,	S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
  		   show_specified_fan_speed,store_specified_fan_speed);
d20c507f2   Colin Leroy   [PATCH] therm_adt...
551
552
553
554
  static DEVICE_ATTR(sensor1_fan_speed,	S_IRUGO,
  		   show_sensor1_fan_speed,	NULL);
  static DEVICE_ATTR(sensor2_fan_speed,	S_IRUGO,
  		   show_sensor2_fan_speed,	NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
556
557
558
559
560
561
562
563
  
  static DEVICE_ATTR(limit_adjust,	S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
  		   show_limit_adjust,	store_limit_adjust);
  
  
  static int __init
  thermostat_init(void)
  {
  	struct device_node* np;
018a3d1db   Jeremy Kerr   [POWERPC] powerma...
564
  	const u32 *prop;
d20c507f2   Colin Leroy   [PATCH] therm_adt...
565
  	int i = 0, offset = 0;
44ea9c809   Nicolas Palix   drivers/macintosh...
566

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
567
568
569
  	np = of_find_node_by_name(NULL, "fan");
  	if (!np)
  		return -ENODEV;
55b61fec2   Stephen Rothwell   [POWERPC] Rename ...
570
  	if (of_device_is_compatible(np, "adt7460"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
  		therm_type = ADT7460;
55b61fec2   Stephen Rothwell   [POWERPC] Rename ...
572
  	else if (of_device_is_compatible(np, "adt7467"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573
  		therm_type = ADT7467;
958a65f20   Julia Lawall   [POWERPC] Add mis...
574
575
  	else {
  		of_node_put(np);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
576
  		return -ENODEV;
958a65f20   Julia Lawall   [POWERPC] Add mis...
577
  	}
a8bacec09   Colin Leroy   [PATCH] Make sure...
578

01b2726dd   Stephen Rothwell   [POWERPC] Rename ...
579
  	prop = of_get_property(np, "hwsensor-params-version", NULL);
a8bacec09   Colin Leroy   [PATCH] Make sure...
580
581
582
  	printk(KERN_INFO "adt746x: version %d (%ssupported)
  ", *prop,
  			 (*prop == 1)?"":"un");
958a65f20   Julia Lawall   [POWERPC] Add mis...
583
584
  	if (*prop != 1) {
  		of_node_put(np);
a8bacec09   Colin Leroy   [PATCH] Make sure...
585
  		return -ENODEV;
958a65f20   Julia Lawall   [POWERPC] Add mis...
586
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
587

01b2726dd   Stephen Rothwell   [POWERPC] Rename ...
588
  	prop = of_get_property(np, "reg", NULL);
958a65f20   Julia Lawall   [POWERPC] Add mis...
589
590
  	if (!prop) {
  		of_node_put(np);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
591
  		return -ENODEV;
958a65f20   Julia Lawall   [POWERPC] Add mis...
592
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
  
  	/* look for bus either by path or using "reg" */
  	if (strstr(np->full_name, "/i2c-bus@") != NULL) {
  		const char *tmp_bus = (strstr(np->full_name, "/i2c-bus@") + 9);
  		therm_bus = tmp_bus[0]-'0';
  	} else {
  		therm_bus = ((*prop) >> 8) & 0x0f;
  	}
  
  	therm_address = ((*prop) & 0xff) >> 1;
  
  	printk(KERN_INFO "adt746x: Thermostat bus: %d, address: 0x%02x, "
  			 "limit_adjust: %d, fan_speed: %d
  ",
  			 therm_bus, therm_address, limit_adjust, fan_speed);
01b2726dd   Stephen Rothwell   [POWERPC] Rename ...
608
  	if (of_get_property(np, "hwsensor-location", NULL)) {
d20c507f2   Colin Leroy   [PATCH] therm_adt...
609
  		for (i = 0; i < 3; i++) {
01b2726dd   Stephen Rothwell   [POWERPC] Rename ...
610
  			sensor_location[i] = of_get_property(np,
d20c507f2   Colin Leroy   [PATCH] therm_adt...
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  					"hwsensor-location", NULL) + offset;
  
  			if (sensor_location[i] == NULL)
  				sensor_location[i] = "";
  
  			printk(KERN_INFO "sensor %d: %s
  ", i, sensor_location[i]);
  			offset += strlen(sensor_location[i]) + 1;
  		}
  	} else {
  		sensor_location[0] = "?";
  		sensor_location[1] = "?";
  		sensor_location[2] = "?";
  	}
0365ba7fb   Benjamin Herrenschmidt   [PATCH] ppc64: SM...
625
  	of_dev = of_platform_device_create(np, "temperatures", NULL);
44ea9c809   Nicolas Palix   drivers/macintosh...
626
  	of_node_put(np);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
628
629
630
631
  	if (of_dev == NULL) {
  		printk(KERN_ERR "Can't register temperatures device !
  ");
  		return -ENODEV;
  	}
44ea9c809   Nicolas Palix   drivers/macintosh...
632

33a470f6d   Jean Delvare   macintosh/therm_a...
633
634
635
636
637
638
639
640
641
642
  #ifndef CONFIG_I2C_POWERMAC
  	request_module("i2c-powermac");
  #endif
  
  	return i2c_add_driver(&thermostat_driver);
  }
  
  static void thermostat_create_files(void)
  {
  	int err;
10804f0fb   Stephen Rothwell   [POWERPC] therm_a...
643
644
645
646
647
648
649
650
651
  	err = device_create_file(&of_dev->dev, &dev_attr_sensor1_temperature);
  	err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_temperature);
  	err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_limit);
  	err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_limit);
  	err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_location);
  	err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_location);
  	err |= device_create_file(&of_dev->dev, &dev_attr_limit_adjust);
  	err |= device_create_file(&of_dev->dev, &dev_attr_specified_fan_speed);
  	err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
  	if(therm_type == ADT7460)
10804f0fb   Stephen Rothwell   [POWERPC] therm_a...
653
654
655
  		err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_fan_speed);
  	if (err)
  		printk(KERN_WARNING
25985edce   Lucas De Marchi   Fix common misspe...
656
657
  			"Failed to create temperature attribute file(s).
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
  }
33a470f6d   Jean Delvare   macintosh/therm_a...
659
  static void thermostat_remove_files(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
661
  {
  	if (of_dev) {
d20c507f2   Colin Leroy   [PATCH] therm_adt...
662
663
664
665
666
667
  		device_remove_file(&of_dev->dev, &dev_attr_sensor1_temperature);
  		device_remove_file(&of_dev->dev, &dev_attr_sensor2_temperature);
  		device_remove_file(&of_dev->dev, &dev_attr_sensor1_limit);
  		device_remove_file(&of_dev->dev, &dev_attr_sensor2_limit);
  		device_remove_file(&of_dev->dev, &dev_attr_sensor1_location);
  		device_remove_file(&of_dev->dev, &dev_attr_sensor2_location);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
669
  		device_remove_file(&of_dev->dev, &dev_attr_limit_adjust);
  		device_remove_file(&of_dev->dev, &dev_attr_specified_fan_speed);
d20c507f2   Colin Leroy   [PATCH] therm_adt...
670
  		device_remove_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
672
673
  
  		if(therm_type == ADT7460)
  			device_remove_file(&of_dev->dev,
d20c507f2   Colin Leroy   [PATCH] therm_adt...
674
  					   &dev_attr_sensor2_fan_speed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
  	}
33a470f6d   Jean Delvare   macintosh/therm_a...
677
678
679
680
681
  }
  
  static void __exit
  thermostat_exit(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
  	i2c_del_driver(&thermostat_driver);
33a470f6d   Jean Delvare   macintosh/therm_a...
683
  	of_device_unregister(of_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
684
685
686
687
  }
  
  module_init(thermostat_init);
  module_exit(thermostat_exit);