iwlwifi: fix more eeprom endian bugs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / macintosh / therm_adt746x.c
blob556f0feaa4df33fe3733e0ccf3638bb6bfe1aa44
1 /*
2 * Device driver for the i2c thermostat found on the iBook G4, Albook G4
4 * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt
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
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/sched.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/spinlock.h>
22 #include <linux/wait.h>
23 #include <linux/suspend.h>
24 #include <linux/kthread.h>
25 #include <linux/moduleparam.h>
26 #include <linux/freezer.h>
27 #include <linux/of_platform.h>
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>
35 #undef DEBUG
37 #define CONFIG_REG 0x40
38 #define MANUAL_MASK 0xe0
39 #define AUTO_MASK 0x20
40 #define INVERT_MASK 0x10
42 static u8 TEMP_REG[3] = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */
43 static u8 LIMIT_REG[3] = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */
44 static u8 MANUAL_MODE[2] = {0x5c, 0x5d};
45 static u8 REM_CONTROL[2] = {0x00, 0x40};
46 static u8 FAN_SPEED[2] = {0x28, 0x2a};
47 static u8 FAN_SPD_SET[2] = {0x30, 0x31};
49 static u8 default_limits_local[3] = {70, 50, 70}; /* local, sensor1, sensor2 */
50 static u8 default_limits_chip[3] = {80, 65, 80}; /* local, sensor1, sensor2 */
51 static const char *sensor_location[3];
53 static int limit_adjust;
54 static int fan_speed = -1;
55 static int verbose;
57 MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
58 MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "
59 "Powerbook G4 Alu");
60 MODULE_LICENSE("GPL");
62 module_param(limit_adjust, int, 0644);
63 MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
64 "by N degrees.");
66 module_param(fan_speed, int, 0644);
67 MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) "
68 "(default 64)");
70 module_param(verbose, bool, 0);
71 MODULE_PARM_DESC(verbose,"Verbose log operations "
72 "(default 0)");
74 struct thermostat {
75 struct i2c_client *clt;
76 u8 temps[3];
77 u8 cached_temp[3];
78 u8 initial_limits[3];
79 u8 limits[3];
80 int last_speed[2];
81 int last_var[2];
84 static enum {ADT7460, ADT7467} therm_type;
85 static int therm_bus, therm_address;
86 static struct of_device * of_dev;
87 static struct thermostat* thermostat;
88 static struct task_struct *thread_therm = NULL;
90 static void write_both_fan_speed(struct thermostat *th, int speed);
91 static void write_fan_speed(struct thermostat *th, int speed, int fan);
93 static int
94 write_reg(struct thermostat* th, int reg, u8 data)
96 u8 tmp[2];
97 int rc;
99 tmp[0] = reg;
100 tmp[1] = data;
101 rc = i2c_master_send(th->clt, (const char *)tmp, 2);
102 if (rc < 0)
103 return rc;
104 if (rc != 2)
105 return -ENODEV;
106 return 0;
109 static int
110 read_reg(struct thermostat* th, int reg)
112 u8 reg_addr, data;
113 int rc;
115 reg_addr = (u8)reg;
116 rc = i2c_master_send(th->clt, &reg_addr, 1);
117 if (rc < 0)
118 return rc;
119 if (rc != 1)
120 return -ENODEV;
121 rc = i2c_master_recv(th->clt, (char *)&data, 1);
122 if (rc < 0)
123 return rc;
124 return data;
127 static struct i2c_driver thermostat_driver;
129 static int
130 attach_thermostat(struct i2c_adapter *adapter)
132 unsigned long bus_no;
133 struct i2c_board_info info;
134 struct i2c_client *client;
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;
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;
150 * Let i2c-core delete that device on driver removal.
151 * This is safe because i2c-core holds the core_lock mutex for us.
153 list_add_tail(&client->detected, &thermostat_driver.clients);
154 return 0;
157 static int
158 remove_thermostat(struct i2c_client *client)
160 struct thermostat *th = i2c_get_clientdata(client);
161 int i;
163 if (thread_therm != NULL) {
164 kthread_stop(thread_therm);
167 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]);
173 for (i = 0; i < 3; i++)
174 write_reg(th, LIMIT_REG[i], th->initial_limits[i]);
176 write_both_fan_speed(th, -1);
178 thermostat = NULL;
180 kfree(th);
182 return 0;
185 static int read_fan_speed(struct thermostat *th, u8 addr)
187 u8 tmp[2];
188 u16 res;
190 /* should start with low byte */
191 tmp[1] = read_reg(th, addr);
192 tmp[0] = read_reg(th, addr + 1);
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);
199 static void write_both_fan_speed(struct thermostat *th, int speed)
201 write_fan_speed(th, speed, 0);
202 if (therm_type == ADT7460)
203 write_fan_speed(th, speed, 1);
206 static void write_fan_speed(struct thermostat *th, int speed, int fan)
208 u8 manual;
210 if (speed > 0xff)
211 speed = 0xff;
212 else if (speed < -1)
213 speed = 0;
215 if (therm_type == ADT7467 && fan == 1)
216 return;
218 if (th->last_speed[fan] != speed) {
219 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]);
227 } else
228 return;
230 if (speed >= 0) {
231 manual = read_reg(th, MANUAL_MODE[fan]);
232 write_reg(th, MANUAL_MODE[fan],
233 (manual|MANUAL_MASK) & (~INVERT_MASK));
234 write_reg(th, FAN_SPD_SET[fan], speed);
235 } else {
236 /* back to automatic */
237 if(therm_type == ADT7460) {
238 manual = read_reg(th,
239 MANUAL_MODE[fan]) & (~MANUAL_MASK);
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));
249 th->last_speed[fan] = speed;
252 static void read_sensors(struct thermostat *th)
254 int i = 0;
256 for (i = 0; i < 3; i++)
257 th->temps[i] = read_reg(th, TEMP_REG[i]);
260 #ifdef DEBUG
261 static void display_stats(struct thermostat *th)
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]));
274 th->cached_temp[0] = th->temps[0];
275 th->cached_temp[1] = th->temps[1];
276 th->cached_temp[2] = th->temps[2];
278 #endif
280 static void update_fans_speed (struct thermostat *th)
282 int lastvar = 0; /* last variation, for iBook */
283 int i = 0;
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];
291 if (var > -1) {
292 int step = (255 - fan_speed) / 7;
293 int new_speed = 0;
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;
300 started = 1;
301 new_speed = fan_speed + ((var-1)*step);
303 if (new_speed < fan_speed)
304 new_speed = fan_speed;
305 if (new_speed > 255)
306 new_speed = 255;
308 if (verbose)
309 printk(KERN_DEBUG "adt746x: Setting fans speed to %d "
310 "(limit exceeded by %d on %s) \n",
311 new_speed, var,
312 sensor_location[fan_number+1]);
313 write_both_fan_speed(th, new_speed);
314 th->last_var[fan_number] = var;
315 } else if (var < -2) {
316 /* don't stop fan if sensor2 is cold and sensor1 is not
317 * so cold (lastvar >= -1) */
318 if (i == 2 && lastvar < -1) {
319 if (th->last_speed[fan_number] != 0)
320 if (verbose)
321 printk(KERN_DEBUG "adt746x: Stopping "
322 "fans.\n");
323 write_both_fan_speed(th, 0);
327 lastvar = var;
329 if (started)
330 return; /* we don't want to re-stop the fan
331 * if sensor1 is heating and sensor2 is not */
335 static int monitor_task(void *arg)
337 struct thermostat* th = arg;
339 set_freezable();
340 while(!kthread_should_stop()) {
341 try_to_freeze();
342 msleep_interruptible(2000);
344 #ifndef DEBUG
345 if (fan_speed != -1)
346 read_sensors(th);
347 #else
348 read_sensors(th);
349 #endif
351 if (fan_speed != -1)
352 update_fans_speed(th);
354 #ifdef DEBUG
355 display_stats(th);
356 #endif
360 return 0;
363 static void set_limit(struct thermostat *th, int i)
365 /* Set sensor1 limit higher to avoid powerdowns */
366 th->limits[i] = default_limits_chip[i] + limit_adjust;
367 write_reg(th, LIMIT_REG[i], th->limits[i]);
369 /* set our limits to normal */
370 th->limits[i] = default_limits_local[i] + limit_adjust;
373 static int probe_thermostat(struct i2c_client *client,
374 const struct i2c_device_id *id)
376 struct thermostat* th;
377 int rc;
378 int i;
380 if (thermostat)
381 return 0;
383 th = kzalloc(sizeof(struct thermostat), GFP_KERNEL);
384 if (!th)
385 return -ENOMEM;
387 i2c_set_clientdata(client, th);
388 th->clt = client;
390 rc = read_reg(th, 0);
391 if (rc < 0) {
392 dev_err(&client->dev, "Thermostat failed to read config!\n");
393 kfree(th);
394 return -ENODEV;
397 /* force manual control to start the fan quieter */
398 if (fan_speed == -1)
399 fan_speed = 64;
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");
408 for (i = 0; i < 3; i++) {
409 th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
410 set_limit(th, i);
413 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]);
419 thermostat = th;
421 /* 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;
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);
435 thread_therm = kthread_run(monitor_task, th, "kfand");
437 if (thread_therm == ERR_PTR(-ENOMEM)) {
438 printk(KERN_INFO "adt746x: Kthread creation failed\n");
439 thread_therm = NULL;
440 return -ENOMEM;
443 return 0;
446 static const struct i2c_device_id therm_adt746x_id[] = {
447 { "therm_adt746x", 0 },
451 static struct i2c_driver thermostat_driver = {
452 .driver = {
453 .name = "therm_adt746x",
455 .attach_adapter = attach_thermostat,
456 .probe = probe_thermostat,
457 .remove = remove_thermostat,
458 .id_table = therm_adt746x_id,
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...
466 * FIXME, it does now...
468 #define BUILD_SHOW_FUNC_INT(name, data) \
469 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
471 return sprintf(buf, "%d\n", data); \
474 #define BUILD_SHOW_FUNC_STR(name, data) \
475 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
477 return sprintf(buf, "%s\n", data); \
480 #define BUILD_SHOW_FUNC_FAN(name, data) \
481 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
483 return sprintf(buf, "%d (%d rpm)\n", \
484 thermostat->last_speed[data], \
485 read_fan_speed(thermostat, FAN_SPEED[data]) \
486 ); \
489 #define BUILD_STORE_FUNC_DEG(name, data) \
490 static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
492 int val; \
493 int i; \
494 val = simple_strtol(buf, NULL, 10); \
495 printk(KERN_INFO "Adjusting limits by %d degrees\n", val); \
496 limit_adjust = val; \
497 for (i=0; i < 3; i++) \
498 set_limit(thermostat, i); \
499 return n; \
502 #define BUILD_STORE_FUNC_INT(name, data) \
503 static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
505 int val; \
506 val = simple_strtol(buf, NULL, 10); \
507 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; \
514 BUILD_SHOW_FUNC_INT(sensor1_temperature, (read_reg(thermostat, TEMP_REG[1])))
515 BUILD_SHOW_FUNC_INT(sensor2_temperature, (read_reg(thermostat, TEMP_REG[2])))
516 BUILD_SHOW_FUNC_INT(sensor1_limit, thermostat->limits[1])
517 BUILD_SHOW_FUNC_INT(sensor2_limit, thermostat->limits[2])
518 BUILD_SHOW_FUNC_STR(sensor1_location, sensor_location[1])
519 BUILD_SHOW_FUNC_STR(sensor2_location, sensor_location[2])
521 BUILD_SHOW_FUNC_INT(specified_fan_speed, fan_speed)
522 BUILD_SHOW_FUNC_FAN(sensor1_fan_speed, 0)
523 BUILD_SHOW_FUNC_FAN(sensor2_fan_speed, 1)
525 BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)
526 BUILD_SHOW_FUNC_INT(limit_adjust, limit_adjust)
527 BUILD_STORE_FUNC_DEG(limit_adjust, thermostat)
529 static DEVICE_ATTR(sensor1_temperature, S_IRUGO,
530 show_sensor1_temperature,NULL);
531 static DEVICE_ATTR(sensor2_temperature, S_IRUGO,
532 show_sensor2_temperature,NULL);
533 static DEVICE_ATTR(sensor1_limit, S_IRUGO,
534 show_sensor1_limit, NULL);
535 static DEVICE_ATTR(sensor2_limit, S_IRUGO,
536 show_sensor2_limit, NULL);
537 static DEVICE_ATTR(sensor1_location, S_IRUGO,
538 show_sensor1_location, NULL);
539 static DEVICE_ATTR(sensor2_location, S_IRUGO,
540 show_sensor2_location, NULL);
542 static DEVICE_ATTR(specified_fan_speed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
543 show_specified_fan_speed,store_specified_fan_speed);
545 static DEVICE_ATTR(sensor1_fan_speed, S_IRUGO,
546 show_sensor1_fan_speed, NULL);
547 static DEVICE_ATTR(sensor2_fan_speed, S_IRUGO,
548 show_sensor2_fan_speed, NULL);
550 static DEVICE_ATTR(limit_adjust, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
551 show_limit_adjust, store_limit_adjust);
554 static int __init
555 thermostat_init(void)
557 struct device_node* np;
558 const u32 *prop;
559 int i = 0, offset = 0;
560 int err;
562 np = of_find_node_by_name(NULL, "fan");
563 if (!np)
564 return -ENODEV;
565 if (of_device_is_compatible(np, "adt7460"))
566 therm_type = ADT7460;
567 else if (of_device_is_compatible(np, "adt7467"))
568 therm_type = ADT7467;
569 else {
570 of_node_put(np);
571 return -ENODEV;
574 prop = of_get_property(np, "hwsensor-params-version", NULL);
575 printk(KERN_INFO "adt746x: version %d (%ssupported)\n", *prop,
576 (*prop == 1)?"":"un");
577 if (*prop != 1) {
578 of_node_put(np);
579 return -ENODEV;
582 prop = of_get_property(np, "reg", NULL);
583 if (!prop) {
584 of_node_put(np);
585 return -ENODEV;
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;
596 therm_address = ((*prop) & 0xff) >> 1;
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);
602 if (of_get_property(np, "hwsensor-location", NULL)) {
603 for (i = 0; i < 3; i++) {
604 sensor_location[i] = of_get_property(np,
605 "hwsensor-location", NULL) + offset;
607 if (sensor_location[i] == NULL)
608 sensor_location[i] = "";
610 printk(KERN_INFO "sensor %d: %s\n", i, sensor_location[i]);
611 offset += strlen(sensor_location[i]) + 1;
613 } else {
614 sensor_location[0] = "?";
615 sensor_location[1] = "?";
616 sensor_location[2] = "?";
619 of_dev = of_platform_device_create(np, "temperatures", NULL);
620 of_node_put(np);
622 if (of_dev == NULL) {
623 printk(KERN_ERR "Can't register temperatures device !\n");
624 return -ENODEV;
627 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);
636 if(therm_type == ADT7460)
637 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");
642 #ifndef CONFIG_I2C_POWERMAC
643 request_module("i2c-powermac");
644 #endif
646 return i2c_add_driver(&thermostat_driver);
649 static void __exit
650 thermostat_exit(void)
652 if (of_dev) {
653 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);
659 device_remove_file(&of_dev->dev, &dev_attr_limit_adjust);
660 device_remove_file(&of_dev->dev, &dev_attr_specified_fan_speed);
661 device_remove_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);
663 if(therm_type == ADT7460)
664 device_remove_file(&of_dev->dev,
665 &dev_attr_sensor2_fan_speed);
667 of_device_unregister(of_dev);
669 i2c_del_driver(&thermostat_driver);
672 module_init(thermostat_init);
673 module_exit(thermostat_exit);