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