2 * Windfarm PowerMac thermal control. SMU "satellite" controller sensors.
4 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
6 * Released under the terms of the GNU GPL v2.
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/i2c.h>
16 #include <linux/mutex.h>
19 #include <asm/pmac_low_i2c.h>
25 /* If the cache is older than 800ms we'll refetch it */
26 #define MAX_AGE msecs_to_jiffies(800)
32 unsigned long last_read; /* jiffies when cache last updated */
34 struct list_head sensors;
35 struct i2c_client *i2c;
36 struct device_node *node;
39 static struct wf_sat *sats[2];
41 struct wf_sat_sensor {
42 struct list_head link;
44 int index2; /* used for power sensors */
47 struct wf_sensor sens;
50 #define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)
52 struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
61 /* TODO: Add the resulting partition to the device-tree */
63 if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
66 err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
68 printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
72 err = i2c_smbus_read_word_data(sat->i2c, 9);
74 printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
79 printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
83 len = le16_to_cpu(len);
85 buf = kmalloc(len, GFP_KERNEL);
89 for (i = 0; i < len; i += 4) {
90 err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
92 printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
102 printk(KERN_DEBUG "sat %d partition %x:", sat_id, id);
103 print_hex_dump(KERN_DEBUG, " ", DUMP_PREFIX_OFFSET,
104 16, 1, buf, len, false);
107 return (struct smu_sdbp_header *) buf;
113 EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
115 /* refresh the cache */
116 static int wf_sat_read_cache(struct wf_sat *sat)
120 err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
123 sat->last_read = jiffies;
128 printk(KERN_DEBUG "wf_sat_get: data is");
129 print_hex_dump(KERN_DEBUG, " ", DUMP_PREFIX_OFFSET,
130 16, 1, sat->cache, 16, false);
136 static int wf_sat_sensor_get(struct wf_sensor *sr, s32 *value)
138 struct wf_sat_sensor *sens = wf_to_sat(sr);
139 struct wf_sat *sat = sens->sat;
143 if (sat->i2c == NULL)
146 mutex_lock(&sat->mutex);
147 if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
148 err = wf_sat_read_cache(sat);
154 val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
155 if (sens->index2 >= 0) {
156 i = sens->index2 * 2;
157 /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
158 val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
165 mutex_unlock(&sat->mutex);
169 static void wf_sat_release(struct kref *ref)
171 struct wf_sat *sat = container_of(ref, struct wf_sat, ref);
174 sats[sat->nr] = NULL;
178 static void wf_sat_sensor_release(struct wf_sensor *sr)
180 struct wf_sat_sensor *sens = wf_to_sat(sr);
181 struct wf_sat *sat = sens->sat;
184 kref_put(&sat->ref, wf_sat_release);
187 static struct wf_sensor_ops wf_sat_ops = {
188 .get_value = wf_sat_sensor_get,
189 .release = wf_sat_sensor_release,
190 .owner = THIS_MODULE,
193 static int wf_sat_probe(struct i2c_client *client,
194 const struct i2c_device_id *id)
196 struct device_node *dev = client->dev.of_node;
198 struct wf_sat_sensor *sens;
200 const char *loc, *type;
202 struct device_node *child;
203 int shift, cpu, index;
205 int vsens[2], isens[2];
207 sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
211 sat->node = of_node_get(dev);
212 kref_init(&sat->ref);
213 mutex_init(&sat->mutex);
215 INIT_LIST_HEAD(&sat->sensors);
216 i2c_set_clientdata(client, sat);
218 vsens[0] = vsens[1] = -1;
219 isens[0] = isens[1] = -1;
221 while ((child = of_get_next_child(dev, child)) != NULL) {
222 reg = of_get_property(child, "reg", NULL);
223 type = of_get_property(child, "device_type", NULL);
224 loc = of_get_property(child, "location", NULL);
225 if (reg == NULL || loc == NULL)
228 /* the cooked sensors are between 0x30 and 0x37 */
229 if (*reg < 0x30 || *reg > 0x37)
233 /* expect location to be CPU [AB][01] ... */
234 if (strncmp(loc, "CPU ", 4) != 0)
238 if (chip > 1 || core > 1) {
239 printk(KERN_ERR "wf_sat_create: don't understand "
240 "location %s for %s\n", loc, child->full_name);
243 cpu = 2 * chip + core;
246 else if (sat->nr != chip) {
247 printk(KERN_ERR "wf_sat_create: can't cope with "
248 "multiple CPU chips on one SAT (%s)\n", loc);
252 if (strcmp(type, "voltage-sensor") == 0) {
253 name = "cpu-voltage";
256 } else if (strcmp(type, "current-sensor") == 0) {
257 name = "cpu-current";
260 } else if (strcmp(type, "temp-sensor") == 0) {
264 continue; /* hmmm shouldn't happen */
266 /* the +16 is enough for "cpu-voltage-n" */
267 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
269 printk(KERN_ERR "wf_sat_create: couldn't create "
270 "%s sensor %d (no memory)\n", name, cpu);
277 sens->sens.ops = &wf_sat_ops;
278 sens->sens.name = (char *) (sens + 1);
279 snprintf((char *)sens->sens.name, 16, "%s-%d", name, cpu);
281 if (wf_register_sensor(&sens->sens))
284 list_add(&sens->link, &sat->sensors);
289 /* make the power sensors */
290 for (core = 0; core < 2; ++core) {
291 if (vsens[core] < 0 || isens[core] < 0)
293 cpu = 2 * sat->nr + core;
294 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
296 printk(KERN_ERR "wf_sat_create: couldn't create power "
297 "sensor %d (no memory)\n", cpu);
300 sens->index = vsens[core];
301 sens->index2 = isens[core];
304 sens->sens.ops = &wf_sat_ops;
305 sens->sens.name = (char *) (sens + 1);
306 snprintf((char *)sens->sens.name, 16, "cpu-power-%d", cpu);
308 if (wf_register_sensor(&sens->sens))
311 list_add(&sens->link, &sat->sensors);
322 static int wf_sat_remove(struct i2c_client *client)
324 struct wf_sat *sat = i2c_get_clientdata(client);
325 struct wf_sat_sensor *sens;
327 /* release sensors */
328 while(!list_empty(&sat->sensors)) {
329 sens = list_first_entry(&sat->sensors,
330 struct wf_sat_sensor, link);
331 list_del(&sens->link);
332 wf_unregister_sensor(&sens->sens);
335 kref_put(&sat->ref, wf_sat_release);
340 static const struct i2c_device_id wf_sat_id[] = {
341 { "MAC,smu-sat", 0 },
344 MODULE_DEVICE_TABLE(i2c, wf_sat_id);
346 static struct i2c_driver wf_sat_driver = {
348 .name = "wf_smu_sat",
350 .probe = wf_sat_probe,
351 .remove = wf_sat_remove,
352 .id_table = wf_sat_id,
355 module_i2c_driver(wf_sat_driver);
357 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
358 MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
359 MODULE_LICENSE("GPL");