fix irq flags in rtc-ds1511
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / macintosh / windfarm_max6690_sensor.c
blobe207a90d6b2762fa887bdfdc30344958f00a8373
1 /*
2 * Windfarm PowerMac thermal control. MAX6690 sensor.
4 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
6 * Use and redistribute under the terms of the GNU GPL v2.
7 */
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <asm/prom.h>
15 #include <asm/pmac_low_i2c.h>
17 #include "windfarm.h"
19 #define VERSION "0.2"
21 /* This currently only exports the external temperature sensor,
22 since that's all the control loops need. */
24 /* Some MAX6690 register numbers */
25 #define MAX6690_INTERNAL_TEMP 0
26 #define MAX6690_EXTERNAL_TEMP 1
28 struct wf_6690_sensor {
29 struct i2c_client i2c;
30 struct wf_sensor sens;
33 #define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens)
34 #define i2c_to_6690(x) container_of((x), struct wf_6690_sensor, i2c)
36 static int wf_max6690_attach(struct i2c_adapter *adapter);
37 static int wf_max6690_detach(struct i2c_client *client);
39 static struct i2c_driver wf_max6690_driver = {
40 .driver = {
41 .name = "wf_max6690",
43 .attach_adapter = wf_max6690_attach,
44 .detach_client = wf_max6690_detach,
47 static int wf_max6690_get(struct wf_sensor *sr, s32 *value)
49 struct wf_6690_sensor *max = wf_to_6690(sr);
50 s32 data;
52 if (max->i2c.adapter == NULL)
53 return -ENODEV;
55 /* chip gets initialized by firmware */
56 data = i2c_smbus_read_byte_data(&max->i2c, MAX6690_EXTERNAL_TEMP);
57 if (data < 0)
58 return data;
59 *value = data << 16;
60 return 0;
63 static void wf_max6690_release(struct wf_sensor *sr)
65 struct wf_6690_sensor *max = wf_to_6690(sr);
67 if (max->i2c.adapter) {
68 i2c_detach_client(&max->i2c);
69 max->i2c.adapter = NULL;
71 kfree(max);
74 static struct wf_sensor_ops wf_max6690_ops = {
75 .get_value = wf_max6690_get,
76 .release = wf_max6690_release,
77 .owner = THIS_MODULE,
80 static void wf_max6690_create(struct i2c_adapter *adapter, u8 addr,
81 const char *loc)
83 struct wf_6690_sensor *max;
84 char *name;
86 max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
87 if (max == NULL) {
88 printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor %s: "
89 "no memory\n", loc);
90 return;
93 if (!strcmp(loc, "BACKSIDE"))
94 name = "backside-temp";
95 else if (!strcmp(loc, "NB Ambient"))
96 name = "north-bridge-temp";
97 else if (!strcmp(loc, "GPU Ambient"))
98 name = "gpu-temp";
99 else
100 goto fail;
102 max->sens.ops = &wf_max6690_ops;
103 max->sens.name = name;
104 max->i2c.addr = addr >> 1;
105 max->i2c.adapter = adapter;
106 max->i2c.driver = &wf_max6690_driver;
107 strncpy(max->i2c.name, name, I2C_NAME_SIZE-1);
109 if (i2c_attach_client(&max->i2c)) {
110 printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");
111 goto fail;
114 if (wf_register_sensor(&max->sens)) {
115 i2c_detach_client(&max->i2c);
116 goto fail;
119 return;
121 fail:
122 kfree(max);
125 static int wf_max6690_attach(struct i2c_adapter *adapter)
127 struct device_node *busnode, *dev = NULL;
128 struct pmac_i2c_bus *bus;
129 const char *loc;
131 bus = pmac_i2c_adapter_to_bus(adapter);
132 if (bus == NULL)
133 return -ENODEV;
134 busnode = pmac_i2c_get_bus_node(bus);
136 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
137 u8 addr;
139 /* We must re-match the adapter in order to properly check
140 * the channel on multibus setups
142 if (!pmac_i2c_match_adapter(dev, adapter))
143 continue;
144 if (!of_device_is_compatible(dev, "max6690"))
145 continue;
146 addr = pmac_i2c_get_dev_addr(dev);
147 loc = of_get_property(dev, "hwsensor-location", NULL);
148 if (loc == NULL || addr == 0)
149 continue;
150 printk("found max6690, loc=%s addr=0x%02x\n", loc, addr);
151 wf_max6690_create(adapter, addr, loc);
154 return 0;
157 static int wf_max6690_detach(struct i2c_client *client)
159 struct wf_6690_sensor *max = i2c_to_6690(client);
161 max->i2c.adapter = NULL;
162 wf_unregister_sensor(&max->sens);
164 return 0;
167 static int __init wf_max6690_sensor_init(void)
169 /* Don't register on old machines that use therm_pm72 for now */
170 if (machine_is_compatible("PowerMac7,2") ||
171 machine_is_compatible("PowerMac7,3") ||
172 machine_is_compatible("RackMac3,1"))
173 return -ENODEV;
174 return i2c_add_driver(&wf_max6690_driver);
177 static void __exit wf_max6690_sensor_exit(void)
179 i2c_del_driver(&wf_max6690_driver);
182 module_init(wf_max6690_sensor_init);
183 module_exit(wf_max6690_sensor_exit);
185 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
186 MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
187 MODULE_LICENSE("GPL");