blob: 5da729e58f99294ec43cd23ec7bee680da7e70b7 [file] [log] [blame]
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +11001/*
2 * Windfarm PowerMac thermal control. SMU "satellite" controller sensors.
3 *
4 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
5 *
6 * Released under the terms of the GNU GPL v2.
7 */
8
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>
Daniel Walker56783c52008-05-03 06:34:02 +100016#include <linux/mutex.h>
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110017#include <asm/prom.h>
18#include <asm/smu.h>
19#include <asm/pmac_low_i2c.h>
20
21#include "windfarm.h"
22
23#define VERSION "0.2"
24
25#define DEBUG
26
27#ifdef DEBUG
28#define DBG(args...) printk(args)
29#else
30#define DBG(args...) do { } while(0)
31#endif
32
33/* If the cache is older than 800ms we'll refetch it */
34#define MAX_AGE msecs_to_jiffies(800)
35
36struct wf_sat {
37 int nr;
38 atomic_t refcnt;
Daniel Walker56783c52008-05-03 06:34:02 +100039 struct mutex mutex;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110040 unsigned long last_read; /* jiffies when cache last updated */
41 u8 cache[16];
Jean Delvare351ca3e2009-06-15 18:01:51 +020042 struct i2c_client *i2c;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110043 struct device_node *node;
44};
45
46static struct wf_sat *sats[2];
47
48struct wf_sat_sensor {
49 int index;
50 int index2; /* used for power sensors */
51 int shift;
52 struct wf_sat *sat;
53 struct wf_sensor sens;
54};
55
56#define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110057
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110058struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
59 unsigned int *size)
60{
61 struct wf_sat *sat;
62 int err;
63 unsigned int i, len;
64 u8 *buf;
65 u8 data[4];
66
67 /* TODO: Add the resulting partition to the device-tree */
68
69 if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
70 return NULL;
71
Jean Delvare351ca3e2009-06-15 18:01:51 +020072 err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110073 if (err) {
74 printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
75 return NULL;
76 }
77
Jean Delvare351ca3e2009-06-15 18:01:51 +020078 err = i2c_smbus_read_word_data(sat->i2c, 9);
roel kluin2fd091f2008-11-29 01:17:27 +000079 if (err < 0) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110080 printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
81 return NULL;
82 }
roel kluin2fd091f2008-11-29 01:17:27 +000083 len = err;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110084 if (len == 0) {
85 printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
86 return NULL;
87 }
88
89 len = le16_to_cpu(len);
90 len = (len + 3) & ~3;
91 buf = kmalloc(len, GFP_KERNEL);
92 if (buf == NULL)
93 return NULL;
94
95 for (i = 0; i < len; i += 4) {
Jean Delvare351ca3e2009-06-15 18:01:51 +020096 err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
Jean Delvare4b2643d2007-07-12 14:12:29 +020097 if (err < 0) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110098 printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
99 err);
100 goto fail;
101 }
102 buf[i] = data[1];
103 buf[i+1] = data[0];
104 buf[i+2] = data[3];
105 buf[i+3] = data[2];
106 }
107#ifdef DEBUG
108 DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id);
109 for (i = 0; i < len; ++i)
110 DBG(" %x", buf[i]);
111 DBG("\n");
112#endif
113
114 if (size)
115 *size = len;
116 return (struct smu_sdbp_header *) buf;
117
118 fail:
119 kfree(buf);
120 return NULL;
121}
Johannes Berg9127dd12006-02-17 13:52:54 -0800122EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100123
124/* refresh the cache */
125static int wf_sat_read_cache(struct wf_sat *sat)
126{
127 int err;
128
Jean Delvare351ca3e2009-06-15 18:01:51 +0200129 err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
Jean Delvare4b2643d2007-07-12 14:12:29 +0200130 if (err < 0)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100131 return err;
132 sat->last_read = jiffies;
133#ifdef LOTSA_DEBUG
134 {
135 int i;
136 DBG(KERN_DEBUG "wf_sat_get: data is");
137 for (i = 0; i < 16; ++i)
138 DBG(" %.2x", sat->cache[i]);
139 DBG("\n");
140 }
141#endif
142 return 0;
143}
144
145static int wf_sat_get(struct wf_sensor *sr, s32 *value)
146{
147 struct wf_sat_sensor *sens = wf_to_sat(sr);
148 struct wf_sat *sat = sens->sat;
149 int i, err;
150 s32 val;
151
Jean Delvare351ca3e2009-06-15 18:01:51 +0200152 if (sat->i2c == NULL)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100153 return -ENODEV;
154
Daniel Walker56783c52008-05-03 06:34:02 +1000155 mutex_lock(&sat->mutex);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100156 if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
157 err = wf_sat_read_cache(sat);
158 if (err)
159 goto fail;
160 }
161
162 i = sens->index * 2;
163 val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
164 if (sens->index2 >= 0) {
165 i = sens->index2 * 2;
166 /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
167 val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
168 }
169
170 *value = val;
171 err = 0;
172
173 fail:
Daniel Walker56783c52008-05-03 06:34:02 +1000174 mutex_unlock(&sat->mutex);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100175 return err;
176}
177
178static void wf_sat_release(struct wf_sensor *sr)
179{
180 struct wf_sat_sensor *sens = wf_to_sat(sr);
181 struct wf_sat *sat = sens->sat;
182
183 if (atomic_dec_and_test(&sat->refcnt)) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100184 if (sat->nr >= 0)
185 sats[sat->nr] = NULL;
186 kfree(sat);
187 }
188 kfree(sens);
189}
190
191static struct wf_sensor_ops wf_sat_ops = {
192 .get_value = wf_sat_get,
193 .release = wf_sat_release,
194 .owner = THIS_MODULE,
195};
196
197static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
198{
Jean Delvare351ca3e2009-06-15 18:01:51 +0200199 struct i2c_board_info info;
200 struct i2c_client *client;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000201 const u32 *reg;
Jean Delvare351ca3e2009-06-15 18:01:51 +0200202 u8 addr;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100203
Stephen Rothwell01b27262007-04-27 13:41:15 +1000204 reg = of_get_property(dev, "reg", NULL);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100205 if (reg == NULL)
206 return;
207 addr = *reg;
208 DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);
209
Jean Delvare351ca3e2009-06-15 18:01:51 +0200210 memset(&info, 0, sizeof(struct i2c_board_info));
211 info.addr = (addr >> 1) & 0x7f;
212 info.platform_data = dev;
213 strlcpy(info.type, "wf_sat", I2C_NAME_SIZE);
214
215 client = i2c_new_device(adapter, &info);
216 if (client == NULL) {
217 printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");
218 return;
219 }
220
221 /*
222 * Let i2c-core delete that device on driver removal.
223 * This is safe because i2c-core holds the core_lock mutex for us.
224 */
225 list_add_tail(&client->detected, &client->driver->clients);
226}
227
228static int wf_sat_probe(struct i2c_client *client,
229 const struct i2c_device_id *id)
230{
231 struct device_node *dev = client->dev.platform_data;
232 struct wf_sat *sat;
233 struct wf_sat_sensor *sens;
234 const u32 *reg;
235 const char *loc, *type;
236 u8 chip, core;
237 struct device_node *child;
238 int shift, cpu, index;
239 char *name;
240 int vsens[2], isens[2];
241
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100242 sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
243 if (sat == NULL)
Jean Delvare351ca3e2009-06-15 18:01:51 +0200244 return -ENOMEM;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100245 sat->nr = -1;
246 sat->node = of_node_get(dev);
247 atomic_set(&sat->refcnt, 0);
Daniel Walker56783c52008-05-03 06:34:02 +1000248 mutex_init(&sat->mutex);
Jean Delvare351ca3e2009-06-15 18:01:51 +0200249 sat->i2c = client;
250 i2c_set_clientdata(client, sat);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100251
252 vsens[0] = vsens[1] = -1;
253 isens[0] = isens[1] = -1;
254 child = NULL;
255 while ((child = of_get_next_child(dev, child)) != NULL) {
Stephen Rothwell01b27262007-04-27 13:41:15 +1000256 reg = of_get_property(child, "reg", NULL);
257 type = of_get_property(child, "device_type", NULL);
258 loc = of_get_property(child, "location", NULL);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100259 if (reg == NULL || loc == NULL)
260 continue;
261
262 /* the cooked sensors are between 0x30 and 0x37 */
263 if (*reg < 0x30 || *reg > 0x37)
264 continue;
265 index = *reg - 0x30;
266
267 /* expect location to be CPU [AB][01] ... */
268 if (strncmp(loc, "CPU ", 4) != 0)
269 continue;
270 chip = loc[4] - 'A';
271 core = loc[5] - '0';
272 if (chip > 1 || core > 1) {
273 printk(KERN_ERR "wf_sat_create: don't understand "
274 "location %s for %s\n", loc, child->full_name);
275 continue;
276 }
277 cpu = 2 * chip + core;
278 if (sat->nr < 0)
279 sat->nr = chip;
280 else if (sat->nr != chip) {
281 printk(KERN_ERR "wf_sat_create: can't cope with "
282 "multiple CPU chips on one SAT (%s)\n", loc);
283 continue;
284 }
285
286 if (strcmp(type, "voltage-sensor") == 0) {
287 name = "cpu-voltage";
288 shift = 4;
289 vsens[core] = index;
290 } else if (strcmp(type, "current-sensor") == 0) {
291 name = "cpu-current";
292 shift = 8;
293 isens[core] = index;
294 } else if (strcmp(type, "temp-sensor") == 0) {
295 name = "cpu-temp";
296 shift = 10;
297 } else
298 continue; /* hmmm shouldn't happen */
299
300 /* the +16 is enough for "cpu-voltage-n" */
301 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
302 if (sens == NULL) {
303 printk(KERN_ERR "wf_sat_create: couldn't create "
304 "%s sensor %d (no memory)\n", name, cpu);
305 continue;
306 }
307 sens->index = index;
308 sens->index2 = -1;
309 sens->shift = shift;
310 sens->sat = sat;
311 atomic_inc(&sat->refcnt);
312 sens->sens.ops = &wf_sat_ops;
313 sens->sens.name = (char *) (sens + 1);
314 snprintf(sens->sens.name, 16, "%s-%d", name, cpu);
315
316 if (wf_register_sensor(&sens->sens)) {
317 atomic_dec(&sat->refcnt);
318 kfree(sens);
319 }
320 }
321
322 /* make the power sensors */
323 for (core = 0; core < 2; ++core) {
324 if (vsens[core] < 0 || isens[core] < 0)
325 continue;
326 cpu = 2 * sat->nr + core;
327 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
328 if (sens == NULL) {
329 printk(KERN_ERR "wf_sat_create: couldn't create power "
330 "sensor %d (no memory)\n", cpu);
331 continue;
332 }
333 sens->index = vsens[core];
334 sens->index2 = isens[core];
335 sens->shift = 0;
336 sens->sat = sat;
337 atomic_inc(&sat->refcnt);
338 sens->sens.ops = &wf_sat_ops;
339 sens->sens.name = (char *) (sens + 1);
340 snprintf(sens->sens.name, 16, "cpu-power-%d", cpu);
341
342 if (wf_register_sensor(&sens->sens)) {
343 atomic_dec(&sat->refcnt);
344 kfree(sens);
345 }
346 }
347
348 if (sat->nr >= 0)
349 sats[sat->nr] = sat;
350
Jean Delvare351ca3e2009-06-15 18:01:51 +0200351 return 0;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100352}
353
354static int wf_sat_attach(struct i2c_adapter *adapter)
355{
356 struct device_node *busnode, *dev = NULL;
357 struct pmac_i2c_bus *bus;
358
359 bus = pmac_i2c_adapter_to_bus(adapter);
360 if (bus == NULL)
361 return -ENODEV;
362 busnode = pmac_i2c_get_bus_node(bus);
363
364 while ((dev = of_get_next_child(busnode, dev)) != NULL)
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000365 if (of_device_is_compatible(dev, "smu-sat"))
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100366 wf_sat_create(adapter, dev);
367 return 0;
368}
369
Jean Delvare351ca3e2009-06-15 18:01:51 +0200370static int wf_sat_remove(struct i2c_client *client)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100371{
Jean Delvare351ca3e2009-06-15 18:01:51 +0200372 struct wf_sat *sat = i2c_get_clientdata(client);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100373
374 /* XXX TODO */
375
Jean Delvare351ca3e2009-06-15 18:01:51 +0200376 sat->i2c = NULL;
377 i2c_set_clientdata(client, NULL);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100378 return 0;
379}
380
Jean Delvare351ca3e2009-06-15 18:01:51 +0200381static const struct i2c_device_id wf_sat_id[] = {
382 { "wf_sat", 0 },
383 { }
384};
385
386static struct i2c_driver wf_sat_driver = {
387 .driver = {
388 .name = "wf_smu_sat",
389 },
390 .attach_adapter = wf_sat_attach,
391 .probe = wf_sat_probe,
392 .remove = wf_sat_remove,
393 .id_table = wf_sat_id,
394};
395
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100396static int __init sat_sensors_init(void)
397{
Alexey Dobriyan92e9a852006-09-29 02:00:58 -0700398 return i2c_add_driver(&wf_sat_driver);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100399}
400
Stephen Rothwella05afe92007-08-15 16:54:04 +1000401#if 0 /* uncomment when module_exit() below is uncommented */
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100402static void __exit sat_sensors_exit(void)
403{
404 i2c_del_driver(&wf_sat_driver);
405}
Stephen Rothwella05afe92007-08-15 16:54:04 +1000406#endif
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100407
408module_init(sat_sensors_init);
409/*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */
410
411MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
412MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
413MODULE_LICENSE("GPL");