blob: 4a8bf467ff4352871698307807905860d176328d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/wait.h>
23#include <linux/suspend.h>
24#include <linux/kthread.h>
25#include <linux/moduleparam.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Stephen Rothwellad9e05a2008-05-23 16:27:02 +100027#include <linux/of_platform.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#undef DEBUG
36
37#define CONFIG_REG 0x40
38#define MANUAL_MASK 0xe0
39#define AUTO_MASK 0x20
40
Colin Leroyd20c5072005-05-25 12:31:35 -070041static u8 TEMP_REG[3] = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */
42static u8 LIMIT_REG[3] = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static u8 MANUAL_MODE[2] = {0x5c, 0x5d};
44static u8 REM_CONTROL[2] = {0x00, 0x40};
45static u8 FAN_SPEED[2] = {0x28, 0x2a};
46static u8 FAN_SPD_SET[2] = {0x30, 0x31};
47
Colin Leroyd20c5072005-05-25 12:31:35 -070048static u8 default_limits_local[3] = {70, 50, 70}; /* local, sensor1, sensor2 */
49static u8 default_limits_chip[3] = {80, 65, 80}; /* local, sensor1, sensor2 */
Olaf Hering87275852007-02-10 21:35:12 +010050static const char *sensor_location[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Olaf Hering87275852007-02-10 21:35:12 +010052static int limit_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int fan_speed = -1;
Olaf Hering87275852007-02-10 21:35:12 +010054static int verbose;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
57MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "
58 "Powerbook G4 Alu");
59MODULE_LICENSE("GPL");
60
61module_param(limit_adjust, int, 0644);
Colin Leroyd20c5072005-05-25 12:31:35 -070062MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 "by N degrees.");
64
65module_param(fan_speed, int, 0644);
66MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) "
67 "(default 64)");
68
Ben Collins9f6d4b02006-01-06 00:11:40 -080069module_param(verbose, bool, 0);
70MODULE_PARM_DESC(verbose,"Verbose log operations "
71 "(default 0)");
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct thermostat {
Jean Delvare2e613e72009-06-15 18:01:51 +020074 struct i2c_client *clt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 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
83static enum {ADT7460, ADT7467} therm_type;
84static int therm_bus, therm_address;
85static struct of_device * of_dev;
86static struct thermostat* thermostat;
87static struct task_struct *thread_therm = NULL;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void write_both_fan_speed(struct thermostat *th, int speed);
90static void write_fan_speed(struct thermostat *th, int speed, int fan);
91
92static int
93write_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;
Jean Delvare2e613e72009-06-15 18:01:51 +0200100 rc = i2c_master_send(th->clt, (const char *)tmp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (rc < 0)
102 return rc;
103 if (rc != 2)
104 return -ENODEV;
105 return 0;
106}
107
108static int
109read_reg(struct thermostat* th, int reg)
110{
111 u8 reg_addr, data;
112 int rc;
113
114 reg_addr = (u8)reg;
Jean Delvare2e613e72009-06-15 18:01:51 +0200115 rc = i2c_master_send(th->clt, &reg_addr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (rc < 0)
117 return rc;
118 if (rc != 1)
119 return -ENODEV;
Jean Delvare2e613e72009-06-15 18:01:51 +0200120 rc = i2c_master_recv(th->clt, (char *)&data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (rc < 0)
122 return rc;
123 return data;
124}
125
126static int
127attach_thermostat(struct i2c_adapter *adapter)
128{
129 unsigned long bus_no;
Jean Delvare2e613e72009-06-15 18:01:51 +0200130 struct i2c_board_info info;
131 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
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;
Jean Delvare2e613e72009-06-15 18:01:51 +0200138
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154static int
Jean Delvare2e613e72009-06-15 18:01:51 +0200155remove_thermostat(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Jean Delvare2e613e72009-06-15 18:01:51 +0200157 struct thermostat *th = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int i;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (thread_therm != NULL) {
161 kthread_stop(thread_therm);
162 }
Ben Collins9f6d4b02006-01-06 00:11:40 -0800163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 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]);
Ben Collins9f6d4b02006-01-06 00:11:40 -0800169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 thermostat = NULL;
176
177 kfree(th);
178
179 return 0;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static 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
196static 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
203static 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) {
Ben Collins9f6d4b02006-01-06 00:11:40 -0800216 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 } 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
248static 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
257static 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
276static 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
Ben Collins9f6d4b02006-01-06 00:11:40 -0800304 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 write_both_fan_speed(th, new_speed);
310 th->last_var[fan_number] = var;
311 } else if (var < -2) {
Colin Leroyd20c5072005-05-25 12:31:35 -0700312 /* don't stop fan if sensor2 is cold and sensor1 is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * so cold (lastvar >= -1) */
314 if (i == 2 && lastvar < -1) {
315 if (th->last_speed[fan_number] != 0)
Ben Collins9f6d4b02006-01-06 00:11:40 -0800316 if (verbose)
317 printk(KERN_DEBUG "adt746x: Stopping "
318 "fans.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 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
Colin Leroyd20c5072005-05-25 12:31:35 -0700327 * if sensor1 is heating and sensor2 is not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329}
330
331static int monitor_task(void *arg)
332{
333 struct thermostat* th = arg;
334
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700335 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 while(!kthread_should_stop()) {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700337 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 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
359static void set_limit(struct thermostat *th, int i)
360{
Colin Leroyd20c5072005-05-25 12:31:35 -0700361 /* Set sensor1 limit higher to avoid powerdowns */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 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
Jean Delvare2e613e72009-06-15 18:01:51 +0200369static int probe_thermostat(struct i2c_client *client,
370 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct thermostat* th;
373 int rc;
374 int i;
375
376 if (thermostat)
377 return 0;
378
Mariusz Kozlowskicc61f952007-08-01 08:10:03 +1000379 th = kzalloc(sizeof(struct thermostat), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!th)
381 return -ENOMEM;
382
Jean Delvare2e613e72009-06-15 18:01:51 +0200383 i2c_set_clientdata(client, th);
384 th->clt = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 rc = read_reg(th, 0);
387 if (rc < 0) {
Jean Delvare2e613e72009-06-15 18:01:51 +0200388 dev_err(&client->dev, "Thermostat failed to read config!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 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 }
Ben Collins9f6d4b02006-01-06 00:11:40 -0800408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* 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
Jean Delvare2e613e72009-06-15 18:01:51 +0200442static const struct i2c_device_id therm_adt746x_id[] = {
443 { "therm_adt746x", 0 },
444 { }
445};
446
447static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
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 *
Yani Ioannoue404e272005-05-17 06:42:58 -0400462 * FIXME, it does now...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
464#define BUILD_SHOW_FUNC_INT(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400465static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{ \
467 return sprintf(buf, "%d\n", data); \
468}
469
Colin Leroyd20c5072005-05-25 12:31:35 -0700470#define BUILD_SHOW_FUNC_STR(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400471static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
Colin Leroyd20c5072005-05-25 12:31:35 -0700472{ \
473 return sprintf(buf, "%s\n", data); \
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#define BUILD_SHOW_FUNC_FAN(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400477static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{ \
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) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400486static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{ \
488 int val; \
489 int i; \
490 val = simple_strtol(buf, NULL, 10); \
Colin Leroyd20c5072005-05-25 12:31:35 -0700491 printk(KERN_INFO "Adjusting limits by %d degrees\n", val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 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) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400499static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{ \
roel kluin2e747782009-01-18 02:03:47 +0000501 int val; \
502 val = simple_strtol(buf, NULL, 10); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 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
Colin Leroyd20c5072005-05-25 12:31:35 -0700510BUILD_SHOW_FUNC_INT(sensor1_temperature, (read_reg(thermostat, TEMP_REG[1])))
511BUILD_SHOW_FUNC_INT(sensor2_temperature, (read_reg(thermostat, TEMP_REG[2])))
512BUILD_SHOW_FUNC_INT(sensor1_limit, thermostat->limits[1])
513BUILD_SHOW_FUNC_INT(sensor2_limit, thermostat->limits[2])
514BUILD_SHOW_FUNC_STR(sensor1_location, sensor_location[1])
515BUILD_SHOW_FUNC_STR(sensor2_location, sensor_location[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517BUILD_SHOW_FUNC_INT(specified_fan_speed, fan_speed)
Colin Leroyd20c5072005-05-25 12:31:35 -0700518BUILD_SHOW_FUNC_FAN(sensor1_fan_speed, 0)
519BUILD_SHOW_FUNC_FAN(sensor2_fan_speed, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)
522BUILD_SHOW_FUNC_INT(limit_adjust, limit_adjust)
523BUILD_STORE_FUNC_DEG(limit_adjust, thermostat)
524
Colin Leroyd20c5072005-05-25 12:31:35 -0700525static DEVICE_ATTR(sensor1_temperature, S_IRUGO,
526 show_sensor1_temperature,NULL);
527static DEVICE_ATTR(sensor2_temperature, S_IRUGO,
528 show_sensor2_temperature,NULL);
529static DEVICE_ATTR(sensor1_limit, S_IRUGO,
530 show_sensor1_limit, NULL);
531static DEVICE_ATTR(sensor2_limit, S_IRUGO,
532 show_sensor2_limit, NULL);
533static DEVICE_ATTR(sensor1_location, S_IRUGO,
534 show_sensor1_location, NULL);
535static DEVICE_ATTR(sensor2_location, S_IRUGO,
536 show_sensor2_location, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538static DEVICE_ATTR(specified_fan_speed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
539 show_specified_fan_speed,store_specified_fan_speed);
540
Colin Leroyd20c5072005-05-25 12:31:35 -0700541static DEVICE_ATTR(sensor1_fan_speed, S_IRUGO,
542 show_sensor1_fan_speed, NULL);
543static DEVICE_ATTR(sensor2_fan_speed, S_IRUGO,
544 show_sensor2_fan_speed, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546static DEVICE_ATTR(limit_adjust, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
547 show_limit_adjust, store_limit_adjust);
548
549
550static int __init
551thermostat_init(void)
552{
553 struct device_node* np;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000554 const u32 *prop;
Colin Leroyd20c5072005-05-25 12:31:35 -0700555 int i = 0, offset = 0;
Stephen Rothwell10804f02008-01-03 15:17:12 +1100556 int err;
Nicolas Palix44ea9c82009-01-06 14:41:35 -0800557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 np = of_find_node_by_name(NULL, "fan");
559 if (!np)
560 return -ENODEV;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000561 if (of_device_is_compatible(np, "adt7460"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 therm_type = ADT7460;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000563 else if (of_device_is_compatible(np, "adt7467"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 therm_type = ADT7467;
Julia Lawall958a65f2008-06-09 22:21:51 +1000565 else {
566 of_node_put(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return -ENODEV;
Julia Lawall958a65f2008-06-09 22:21:51 +1000568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Stephen Rothwell01b27262007-04-27 13:41:15 +1000570 prop = of_get_property(np, "hwsensor-params-version", NULL);
Colin Leroya8bacec2005-05-25 12:31:34 -0700571 printk(KERN_INFO "adt746x: version %d (%ssupported)\n", *prop,
572 (*prop == 1)?"":"un");
Julia Lawall958a65f2008-06-09 22:21:51 +1000573 if (*prop != 1) {
574 of_node_put(np);
Colin Leroya8bacec2005-05-25 12:31:34 -0700575 return -ENODEV;
Julia Lawall958a65f2008-06-09 22:21:51 +1000576 }
Colin Leroya8bacec2005-05-25 12:31:34 -0700577
Stephen Rothwell01b27262007-04-27 13:41:15 +1000578 prop = of_get_property(np, "reg", NULL);
Julia Lawall958a65f2008-06-09 22:21:51 +1000579 if (!prop) {
580 of_node_put(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -ENODEV;
Julia Lawall958a65f2008-06-09 22:21:51 +1000582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
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
Stephen Rothwell01b27262007-04-27 13:41:15 +1000598 if (of_get_property(np, "hwsensor-location", NULL)) {
Colin Leroyd20c5072005-05-25 12:31:35 -0700599 for (i = 0; i < 3; i++) {
Stephen Rothwell01b27262007-04-27 13:41:15 +1000600 sensor_location[i] = of_get_property(np,
Colin Leroyd20c5072005-05-25 12:31:35 -0700601 "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
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700615 of_dev = of_platform_device_create(np, "temperatures", NULL);
Nicolas Palix44ea9c82009-01-06 14:41:35 -0800616 of_node_put(np);
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (of_dev == NULL) {
619 printk(KERN_ERR "Can't register temperatures device !\n");
620 return -ENODEV;
621 }
Nicolas Palix44ea9c82009-01-06 14:41:35 -0800622
Stephen Rothwell10804f02008-01-03 15:17:12 +1100623 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if(therm_type == ADT7460)
Stephen Rothwell10804f02008-01-03 15:17:12 +1100633 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Benjamin Herrenschmidt4d6c5882006-04-21 15:04:22 +1000638#ifndef CONFIG_I2C_POWERMAC
639 request_module("i2c-powermac");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#endif
641
642 return i2c_add_driver(&thermostat_driver);
643}
644
645static void __exit
646thermostat_exit(void)
647{
648 if (of_dev) {
Colin Leroyd20c5072005-05-25 12:31:35 -0700649 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 device_remove_file(&of_dev->dev, &dev_attr_limit_adjust);
656 device_remove_file(&of_dev->dev, &dev_attr_specified_fan_speed);
Colin Leroyd20c5072005-05-25 12:31:35 -0700657 device_remove_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if(therm_type == ADT7460)
660 device_remove_file(&of_dev->dev,
Colin Leroyd20c5072005-05-25 12:31:35 -0700661 &dev_attr_sensor2_fan_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 of_device_unregister(of_dev);
664 }
665 i2c_del_driver(&thermostat_driver);
666}
667
668module_init(thermostat_init);
669module_exit(thermostat_exit);