blob: 95b676a19be72c791235a5d3c9594a0911f1fa22 [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
Michel Dänzer0512a9a2009-05-22 10:59:10 +000040#define INVERT_MASK 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Colin Leroyd20c5072005-05-25 12:31:35 -070042static u8 TEMP_REG[3] = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */
43static u8 LIMIT_REG[3] = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static 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
Colin Leroyd20c5072005-05-25 12:31:35 -070049static u8 default_limits_local[3] = {70, 50, 70}; /* local, sensor1, sensor2 */
50static u8 default_limits_chip[3] = {80, 65, 80}; /* local, sensor1, sensor2 */
Olaf Hering87275852007-02-10 21:35:12 +010051static const char *sensor_location[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Olaf Hering87275852007-02-10 21:35:12 +010053static int limit_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int fan_speed = -1;
Olaf Hering87275852007-02-10 21:35:12 +010055static int verbose;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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);
Colin Leroyd20c5072005-05-25 12:31:35 -070063MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 "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
Ben Collins9f6d4b02006-01-06 00:11:40 -080070module_param(verbose, bool, 0);
71MODULE_PARM_DESC(verbose,"Verbose log operations "
72 "(default 0)");
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct thermostat {
Jean Delvare2e613e72009-06-15 18:01:51 +020075 struct i2c_client *clt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void write_both_fan_speed(struct thermostat *th, int speed);
91static void write_fan_speed(struct thermostat *th, int speed, int fan);
92
93static int
94write_reg(struct thermostat* th, int reg, u8 data)
95{
96 u8 tmp[2];
97 int rc;
98
99 tmp[0] = reg;
100 tmp[1] = data;
Jean Delvare2e613e72009-06-15 18:01:51 +0200101 rc = i2c_master_send(th->clt, (const char *)tmp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (rc < 0)
103 return rc;
104 if (rc != 2)
105 return -ENODEV;
106 return 0;
107}
108
109static int
110read_reg(struct thermostat* th, int reg)
111{
112 u8 reg_addr, data;
113 int rc;
114
115 reg_addr = (u8)reg;
Jean Delvare2e613e72009-06-15 18:01:51 +0200116 rc = i2c_master_send(th->clt, &reg_addr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (rc < 0)
118 return rc;
119 if (rc != 1)
120 return -ENODEV;
Jean Delvare2e613e72009-06-15 18:01:51 +0200121 rc = i2c_master_recv(th->clt, (char *)&data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (rc < 0)
123 return rc;
124 return data;
125}
126
Jean Delvare6f6b35e2009-10-04 22:53:46 +0200127static struct i2c_driver thermostat_driver;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int
130attach_thermostat(struct i2c_adapter *adapter)
131{
132 unsigned long bus_no;
Jean Delvare2e613e72009-06-15 18:01:51 +0200133 struct i2c_board_info info;
134 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if (strncmp(adapter->name, "uni-n", 5))
137 return -ENODEV;
138 bus_no = simple_strtoul(adapter->name + 6, NULL, 10);
139 if (bus_no != therm_bus)
140 return -ENODEV;
Jean Delvare2e613e72009-06-15 18:01:51 +0200141
142 memset(&info, 0, sizeof(struct i2c_board_info));
143 strlcpy(info.type, "therm_adt746x", I2C_NAME_SIZE);
144 info.addr = therm_address;
145 client = i2c_new_device(adapter, &info);
146 if (!client)
147 return -ENODEV;
148
149 /*
150 * Let i2c-core delete that device on driver removal.
151 * This is safe because i2c-core holds the core_lock mutex for us.
152 */
Jean Delvare6f6b35e2009-10-04 22:53:46 +0200153 list_add_tail(&client->detected, &thermostat_driver.clients);
Jean Delvare2e613e72009-06-15 18:01:51 +0200154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static int
Jean Delvare2e613e72009-06-15 18:01:51 +0200158remove_thermostat(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Jean Delvare2e613e72009-06-15 18:01:51 +0200160 struct thermostat *th = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int i;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (thread_therm != NULL) {
164 kthread_stop(thread_therm);
165 }
Ben Collins9f6d4b02006-01-06 00:11:40 -0800166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printk(KERN_INFO "adt746x: Putting max temperatures back from "
168 "%d, %d, %d to %d, %d, %d\n",
169 th->limits[0], th->limits[1], th->limits[2],
170 th->initial_limits[0], th->initial_limits[1],
171 th->initial_limits[2]);
Ben Collins9f6d4b02006-01-06 00:11:40 -0800172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 for (i = 0; i < 3; i++)
174 write_reg(th, LIMIT_REG[i], th->initial_limits[i]);
175
176 write_both_fan_speed(th, -1);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 thermostat = NULL;
179
180 kfree(th);
181
182 return 0;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static int read_fan_speed(struct thermostat *th, u8 addr)
186{
187 u8 tmp[2];
188 u16 res;
189
190 /* should start with low byte */
191 tmp[1] = read_reg(th, addr);
192 tmp[0] = read_reg(th, addr + 1);
193
194 res = tmp[1] + (tmp[0] << 8);
195 /* "a value of 0xffff means that the fan has stopped" */
196 return (res == 0xffff ? 0 : (90000*60)/res);
197}
198
199static void write_both_fan_speed(struct thermostat *th, int speed)
200{
201 write_fan_speed(th, speed, 0);
202 if (therm_type == ADT7460)
203 write_fan_speed(th, speed, 1);
204}
205
206static void write_fan_speed(struct thermostat *th, int speed, int fan)
207{
208 u8 manual;
209
210 if (speed > 0xff)
211 speed = 0xff;
212 else if (speed < -1)
213 speed = 0;
214
215 if (therm_type == ADT7467 && fan == 1)
216 return;
217
218 if (th->last_speed[fan] != speed) {
Ben Collins9f6d4b02006-01-06 00:11:40 -0800219 if (verbose) {
220 if (speed == -1)
221 printk(KERN_DEBUG "adt746x: Setting speed to automatic "
222 "for %s fan.\n", sensor_location[fan+1]);
223 else
224 printk(KERN_DEBUG "adt746x: Setting speed to %d "
225 "for %s fan.\n", speed, sensor_location[fan+1]);
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } else
228 return;
229
230 if (speed >= 0) {
231 manual = read_reg(th, MANUAL_MODE[fan]);
Michel Dänzer0512a9a2009-05-22 10:59:10 +0000232 write_reg(th, MANUAL_MODE[fan],
233 (manual|MANUAL_MASK) & (~INVERT_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 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
Ben Collins9f6d4b02006-01-06 00:11:40 -0800308 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 write_both_fan_speed(th, new_speed);
314 th->last_var[fan_number] = var;
315 } else if (var < -2) {
Colin Leroyd20c5072005-05-25 12:31:35 -0700316 /* don't stop fan if sensor2 is cold and sensor1 is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * so cold (lastvar >= -1) */
318 if (i == 2 && lastvar < -1) {
319 if (th->last_speed[fan_number] != 0)
Ben Collins9f6d4b02006-01-06 00:11:40 -0800320 if (verbose)
321 printk(KERN_DEBUG "adt746x: Stopping "
322 "fans.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 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
Colin Leroyd20c5072005-05-25 12:31:35 -0700331 * if sensor1 is heating and sensor2 is not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333}
334
335static int monitor_task(void *arg)
336{
337 struct thermostat* th = arg;
338
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700339 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 while(!kthread_should_stop()) {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700341 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 msleep_interruptible(2000);
343
344#ifndef DEBUG
345 if (fan_speed != -1)
346 read_sensors(th);
347#else
348 read_sensors(th);
349#endif
350
351 if (fan_speed != -1)
352 update_fans_speed(th);
353
354#ifdef DEBUG
355 display_stats(th);
356#endif
357
358 }
359
360 return 0;
361}
362
363static void set_limit(struct thermostat *th, int i)
364{
Colin Leroyd20c5072005-05-25 12:31:35 -0700365 /* Set sensor1 limit higher to avoid powerdowns */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 th->limits[i] = default_limits_chip[i] + limit_adjust;
367 write_reg(th, LIMIT_REG[i], th->limits[i]);
368
369 /* set our limits to normal */
370 th->limits[i] = default_limits_local[i] + limit_adjust;
371}
372
Jean Delvare2e613e72009-06-15 18:01:51 +0200373static int probe_thermostat(struct i2c_client *client,
374 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct thermostat* th;
377 int rc;
378 int i;
379
380 if (thermostat)
381 return 0;
382
Mariusz Kozlowskicc61f952007-08-01 08:10:03 +1000383 th = kzalloc(sizeof(struct thermostat), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!th)
385 return -ENOMEM;
386
Jean Delvare2e613e72009-06-15 18:01:51 +0200387 i2c_set_clientdata(client, th);
388 th->clt = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Jean Delvare99ca177a2009-10-14 05:31:32 +0000390 rc = read_reg(th, CONFIG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (rc < 0) {
Jean Delvare2e613e72009-06-15 18:01:51 +0200392 dev_err(&client->dev, "Thermostat failed to read config!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 kfree(th);
394 return -ENODEV;
395 }
396
397 /* force manual control to start the fan quieter */
398 if (fan_speed == -1)
399 fan_speed = 64;
400
401 if(therm_type == ADT7460) {
402 printk(KERN_INFO "adt746x: ADT7460 initializing\n");
403 /* The 7460 needs to be started explicitly */
404 write_reg(th, CONFIG_REG, 1);
405 } else
406 printk(KERN_INFO "adt746x: ADT7467 initializing\n");
407
408 for (i = 0; i < 3; i++) {
409 th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
410 set_limit(th, i);
411 }
Ben Collins9f6d4b02006-01-06 00:11:40 -0800412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"
414 " to %d, %d, %d\n",
415 th->initial_limits[0], th->initial_limits[1],
416 th->initial_limits[2], th->limits[0], th->limits[1],
417 th->limits[2]);
418
419 thermostat = th;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* be sure to really write fan speed the first time */
422 th->last_speed[0] = -2;
423 th->last_speed[1] = -2;
424 th->last_var[0] = -80;
425 th->last_var[1] = -80;
426
427 if (fan_speed != -1) {
428 /* manual mode, stop fans */
429 write_both_fan_speed(th, 0);
430 } else {
431 /* automatic mode */
432 write_both_fan_speed(th, -1);
433 }
434
435 thread_therm = kthread_run(monitor_task, th, "kfand");
436
437 if (thread_therm == ERR_PTR(-ENOMEM)) {
438 printk(KERN_INFO "adt746x: Kthread creation failed\n");
439 thread_therm = NULL;
440 return -ENOMEM;
441 }
442
443 return 0;
444}
445
Jean Delvare2e613e72009-06-15 18:01:51 +0200446static const struct i2c_device_id therm_adt746x_id[] = {
447 { "therm_adt746x", 0 },
448 { }
449};
450
451static struct i2c_driver thermostat_driver = {
452 .driver = {
453 .name = "therm_adt746x",
454 },
455 .attach_adapter = attach_thermostat,
456 .probe = probe_thermostat,
457 .remove = remove_thermostat,
458 .id_table = therm_adt746x_id,
459};
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
462 * Now, unfortunately, sysfs doesn't give us a nice void * we could
463 * pass around to the attribute functions, so we don't really have
464 * choice but implement a bunch of them...
465 *
Yani Ioannoue404e272005-05-17 06:42:58 -0400466 * FIXME, it does now...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
468#define BUILD_SHOW_FUNC_INT(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400469static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{ \
471 return sprintf(buf, "%d\n", data); \
472}
473
Colin Leroyd20c5072005-05-25 12:31:35 -0700474#define BUILD_SHOW_FUNC_STR(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400475static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
Colin Leroyd20c5072005-05-25 12:31:35 -0700476{ \
477 return sprintf(buf, "%s\n", data); \
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480#define BUILD_SHOW_FUNC_FAN(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400481static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{ \
483 return sprintf(buf, "%d (%d rpm)\n", \
484 thermostat->last_speed[data], \
485 read_fan_speed(thermostat, FAN_SPEED[data]) \
486 ); \
487}
488
489#define BUILD_STORE_FUNC_DEG(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400490static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{ \
492 int val; \
493 int i; \
494 val = simple_strtol(buf, NULL, 10); \
Colin Leroyd20c5072005-05-25 12:31:35 -0700495 printk(KERN_INFO "Adjusting limits by %d degrees\n", val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 limit_adjust = val; \
497 for (i=0; i < 3; i++) \
498 set_limit(thermostat, i); \
499 return n; \
500}
501
502#define BUILD_STORE_FUNC_INT(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400503static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{ \
roel kluin2e747782009-01-18 02:03:47 +0000505 int val; \
506 val = simple_strtol(buf, NULL, 10); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (val < 0 || val > 255) \
508 return -EINVAL; \
509 printk(KERN_INFO "Setting specified fan speed to %d\n", val); \
510 data = val; \
511 return n; \
512}
513
Colin Leroyd20c5072005-05-25 12:31:35 -0700514BUILD_SHOW_FUNC_INT(sensor1_temperature, (read_reg(thermostat, TEMP_REG[1])))
515BUILD_SHOW_FUNC_INT(sensor2_temperature, (read_reg(thermostat, TEMP_REG[2])))
516BUILD_SHOW_FUNC_INT(sensor1_limit, thermostat->limits[1])
517BUILD_SHOW_FUNC_INT(sensor2_limit, thermostat->limits[2])
518BUILD_SHOW_FUNC_STR(sensor1_location, sensor_location[1])
519BUILD_SHOW_FUNC_STR(sensor2_location, sensor_location[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521BUILD_SHOW_FUNC_INT(specified_fan_speed, fan_speed)
Colin Leroyd20c5072005-05-25 12:31:35 -0700522BUILD_SHOW_FUNC_FAN(sensor1_fan_speed, 0)
523BUILD_SHOW_FUNC_FAN(sensor2_fan_speed, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)
526BUILD_SHOW_FUNC_INT(limit_adjust, limit_adjust)
527BUILD_STORE_FUNC_DEG(limit_adjust, thermostat)
528
Colin Leroyd20c5072005-05-25 12:31:35 -0700529static DEVICE_ATTR(sensor1_temperature, S_IRUGO,
530 show_sensor1_temperature,NULL);
531static DEVICE_ATTR(sensor2_temperature, S_IRUGO,
532 show_sensor2_temperature,NULL);
533static DEVICE_ATTR(sensor1_limit, S_IRUGO,
534 show_sensor1_limit, NULL);
535static DEVICE_ATTR(sensor2_limit, S_IRUGO,
536 show_sensor2_limit, NULL);
537static DEVICE_ATTR(sensor1_location, S_IRUGO,
538 show_sensor1_location, NULL);
539static DEVICE_ATTR(sensor2_location, S_IRUGO,
540 show_sensor2_location, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542static DEVICE_ATTR(specified_fan_speed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
543 show_specified_fan_speed,store_specified_fan_speed);
544
Colin Leroyd20c5072005-05-25 12:31:35 -0700545static DEVICE_ATTR(sensor1_fan_speed, S_IRUGO,
546 show_sensor1_fan_speed, NULL);
547static DEVICE_ATTR(sensor2_fan_speed, S_IRUGO,
548 show_sensor2_fan_speed, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550static DEVICE_ATTR(limit_adjust, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
551 show_limit_adjust, store_limit_adjust);
552
553
554static int __init
555thermostat_init(void)
556{
557 struct device_node* np;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000558 const u32 *prop;
Colin Leroyd20c5072005-05-25 12:31:35 -0700559 int i = 0, offset = 0;
Stephen Rothwell10804f02008-01-03 15:17:12 +1100560 int err;
Nicolas Palix44ea9c82009-01-06 14:41:35 -0800561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 np = of_find_node_by_name(NULL, "fan");
563 if (!np)
564 return -ENODEV;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000565 if (of_device_is_compatible(np, "adt7460"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 therm_type = ADT7460;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000567 else if (of_device_is_compatible(np, "adt7467"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 therm_type = ADT7467;
Julia Lawall958a65f2008-06-09 22:21:51 +1000569 else {
570 of_node_put(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -ENODEV;
Julia Lawall958a65f2008-06-09 22:21:51 +1000572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Stephen Rothwell01b27262007-04-27 13:41:15 +1000574 prop = of_get_property(np, "hwsensor-params-version", NULL);
Colin Leroya8bacec2005-05-25 12:31:34 -0700575 printk(KERN_INFO "adt746x: version %d (%ssupported)\n", *prop,
576 (*prop == 1)?"":"un");
Julia Lawall958a65f2008-06-09 22:21:51 +1000577 if (*prop != 1) {
578 of_node_put(np);
Colin Leroya8bacec2005-05-25 12:31:34 -0700579 return -ENODEV;
Julia Lawall958a65f2008-06-09 22:21:51 +1000580 }
Colin Leroya8bacec2005-05-25 12:31:34 -0700581
Stephen Rothwell01b27262007-04-27 13:41:15 +1000582 prop = of_get_property(np, "reg", NULL);
Julia Lawall958a65f2008-06-09 22:21:51 +1000583 if (!prop) {
584 of_node_put(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -ENODEV;
Julia Lawall958a65f2008-06-09 22:21:51 +1000586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* look for bus either by path or using "reg" */
589 if (strstr(np->full_name, "/i2c-bus@") != NULL) {
590 const char *tmp_bus = (strstr(np->full_name, "/i2c-bus@") + 9);
591 therm_bus = tmp_bus[0]-'0';
592 } else {
593 therm_bus = ((*prop) >> 8) & 0x0f;
594 }
595
596 therm_address = ((*prop) & 0xff) >> 1;
597
598 printk(KERN_INFO "adt746x: Thermostat bus: %d, address: 0x%02x, "
599 "limit_adjust: %d, fan_speed: %d\n",
600 therm_bus, therm_address, limit_adjust, fan_speed);
601
Stephen Rothwell01b27262007-04-27 13:41:15 +1000602 if (of_get_property(np, "hwsensor-location", NULL)) {
Colin Leroyd20c5072005-05-25 12:31:35 -0700603 for (i = 0; i < 3; i++) {
Stephen Rothwell01b27262007-04-27 13:41:15 +1000604 sensor_location[i] = of_get_property(np,
Colin Leroyd20c5072005-05-25 12:31:35 -0700605 "hwsensor-location", NULL) + offset;
606
607 if (sensor_location[i] == NULL)
608 sensor_location[i] = "";
609
610 printk(KERN_INFO "sensor %d: %s\n", i, sensor_location[i]);
611 offset += strlen(sensor_location[i]) + 1;
612 }
613 } else {
614 sensor_location[0] = "?";
615 sensor_location[1] = "?";
616 sensor_location[2] = "?";
617 }
618
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700619 of_dev = of_platform_device_create(np, "temperatures", NULL);
Nicolas Palix44ea9c82009-01-06 14:41:35 -0800620 of_node_put(np);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (of_dev == NULL) {
623 printk(KERN_ERR "Can't register temperatures device !\n");
624 return -ENODEV;
625 }
Nicolas Palix44ea9c82009-01-06 14:41:35 -0800626
Stephen Rothwell10804f02008-01-03 15:17:12 +1100627 err = device_create_file(&of_dev->dev, &dev_attr_sensor1_temperature);
628 err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_temperature);
629 err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_limit);
630 err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_limit);
631 err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_location);
632 err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_location);
633 err |= device_create_file(&of_dev->dev, &dev_attr_limit_adjust);
634 err |= device_create_file(&of_dev->dev, &dev_attr_specified_fan_speed);
635 err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if(therm_type == ADT7460)
Stephen Rothwell10804f02008-01-03 15:17:12 +1100637 err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_fan_speed);
638 if (err)
639 printk(KERN_WARNING
640 "Failed to create tempertaure attribute file(s).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Benjamin Herrenschmidt4d6c5882006-04-21 15:04:22 +1000642#ifndef CONFIG_I2C_POWERMAC
643 request_module("i2c-powermac");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644#endif
645
646 return i2c_add_driver(&thermostat_driver);
647}
648
649static void __exit
650thermostat_exit(void)
651{
652 if (of_dev) {
Colin Leroyd20c5072005-05-25 12:31:35 -0700653 device_remove_file(&of_dev->dev, &dev_attr_sensor1_temperature);
654 device_remove_file(&of_dev->dev, &dev_attr_sensor2_temperature);
655 device_remove_file(&of_dev->dev, &dev_attr_sensor1_limit);
656 device_remove_file(&of_dev->dev, &dev_attr_sensor2_limit);
657 device_remove_file(&of_dev->dev, &dev_attr_sensor1_location);
658 device_remove_file(&of_dev->dev, &dev_attr_sensor2_location);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 device_remove_file(&of_dev->dev, &dev_attr_limit_adjust);
660 device_remove_file(&of_dev->dev, &dev_attr_specified_fan_speed);
Colin Leroyd20c5072005-05-25 12:31:35 -0700661 device_remove_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if(therm_type == ADT7460)
664 device_remove_file(&of_dev->dev,
Colin Leroyd20c5072005-05-25 12:31:35 -0700665 &dev_attr_sensor2_fan_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 of_device_unregister(of_dev);
668 }
669 i2c_del_driver(&thermostat_driver);
670}
671
672module_init(thermostat_init);
673module_exit(thermostat_exit);