blob: eae1189d6c41f6755636629df11621e6ef2d76a6 [file] [log] [blame]
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +11001/*
2 * Windfarm PowerMac thermal control. MAX6690 sensor.
3 *
4 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
5 *
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>
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110014#include <asm/prom.h>
15#include <asm/pmac_low_i2c.h>
16
17#include "windfarm.h"
18
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +110019#define VERSION "0.2"
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110020
21/* This currently only exports the external temperature sensor,
22 since that's all the control loops need. */
23
24/* Some MAX6690 register numbers */
25#define MAX6690_INTERNAL_TEMP 0
26#define MAX6690_EXTERNAL_TEMP 1
27
28struct wf_6690_sensor {
29 struct i2c_client i2c;
30 struct wf_sensor sens;
31};
32
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)
35
36static int wf_max6690_attach(struct i2c_adapter *adapter);
37static int wf_max6690_detach(struct i2c_client *client);
38
39static struct i2c_driver wf_max6690_driver = {
40 .driver = {
41 .name = "wf_max6690",
42 },
43 .attach_adapter = wf_max6690_attach,
44 .detach_client = wf_max6690_detach,
45};
46
47static int wf_max6690_get(struct wf_sensor *sr, s32 *value)
48{
49 struct wf_6690_sensor *max = wf_to_6690(sr);
50 s32 data;
51
52 if (max->i2c.adapter == NULL)
53 return -ENODEV;
54
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;
61}
62
63static void wf_max6690_release(struct wf_sensor *sr)
64{
65 struct wf_6690_sensor *max = wf_to_6690(sr);
66
67 if (max->i2c.adapter) {
68 i2c_detach_client(&max->i2c);
69 max->i2c.adapter = NULL;
70 }
71 kfree(max);
72}
73
74static struct wf_sensor_ops wf_max6690_ops = {
75 .get_value = wf_max6690_get,
76 .release = wf_max6690_release,
77 .owner = THIS_MODULE,
78};
79
80static void wf_max6690_create(struct i2c_adapter *adapter, u8 addr)
81{
82 struct wf_6690_sensor *max;
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +110083 char *name = "backside-temp";
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110084
85 max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
86 if (max == NULL) {
87 printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor %s: "
88 "no memory\n", name);
89 return;
90 }
91
92 max->sens.ops = &wf_max6690_ops;
93 max->sens.name = name;
94 max->i2c.addr = addr >> 1;
95 max->i2c.adapter = adapter;
96 max->i2c.driver = &wf_max6690_driver;
97 strncpy(max->i2c.name, name, I2C_NAME_SIZE-1);
98
99 if (i2c_attach_client(&max->i2c)) {
100 printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");
101 goto fail;
102 }
103
104 if (wf_register_sensor(&max->sens)) {
105 i2c_detach_client(&max->i2c);
106 goto fail;
107 }
108
109 return;
110
111 fail:
112 kfree(max);
113}
114
115static int wf_max6690_attach(struct i2c_adapter *adapter)
116{
117 struct device_node *busnode, *dev = NULL;
118 struct pmac_i2c_bus *bus;
119 const char *loc;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100120
121 bus = pmac_i2c_adapter_to_bus(adapter);
122 if (bus == NULL)
123 return -ENODEV;
124 busnode = pmac_i2c_get_bus_node(bus);
125
126 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100127 u8 addr;
128
129 /* We must re-match the adapter in order to properly check
130 * the channel on multibus setups
131 */
132 if (!pmac_i2c_match_adapter(dev, adapter))
133 continue;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100134 if (!device_is_compatible(dev, "max6690"))
135 continue;
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100136 addr = pmac_i2c_get_dev_addr(dev);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100137 loc = get_property(dev, "hwsensor-location", NULL);
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100138 if (loc == NULL || addr == 0)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100139 continue;
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100140 printk("found max6690, loc=%s addr=0x%02x\n", loc, addr);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100141 if (strcmp(loc, "BACKSIDE"))
142 continue;
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100143 wf_max6690_create(adapter, addr);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100144 }
145
146 return 0;
147}
148
149static int wf_max6690_detach(struct i2c_client *client)
150{
151 struct wf_6690_sensor *max = i2c_to_6690(client);
152
153 max->i2c.adapter = NULL;
154 wf_unregister_sensor(&max->sens);
155
156 return 0;
157}
158
159static int __init wf_max6690_sensor_init(void)
160{
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100161 /* Don't register on old machines that use therm_pm72 for now */
162 if (machine_is_compatible("PowerMac7,2") ||
163 machine_is_compatible("PowerMac7,3") ||
164 machine_is_compatible("RackMac3,1"))
165 return -ENODEV;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100166 return i2c_add_driver(&wf_max6690_driver);
167}
168
169static void __exit wf_max6690_sensor_exit(void)
170{
171 i2c_del_driver(&wf_max6690_driver);
172}
173
174module_init(wf_max6690_sensor_init);
175module_exit(wf_max6690_sensor_exit);
176
177MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
178MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
179MODULE_LICENSE("GPL");