Merge branch 'from-linus'
[sfrench/cifs-2.6.git] / drivers / macintosh / windfarm_smu_sat.c
1 /*
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>
16 #include <linux/i2c-dev.h>
17 #include <asm/semaphore.h>
18 #include <asm/prom.h>
19 #include <asm/smu.h>
20 #include <asm/pmac_low_i2c.h>
21
22 #include "windfarm.h"
23
24 #define VERSION "0.2"
25
26 #define DEBUG
27
28 #ifdef DEBUG
29 #define DBG(args...)    printk(args)
30 #else
31 #define DBG(args...)    do { } while(0)
32 #endif
33
34 /* If the cache is older than 800ms we'll refetch it */
35 #define MAX_AGE         msecs_to_jiffies(800)
36
37 struct wf_sat {
38         int                     nr;
39         atomic_t                refcnt;
40         struct semaphore        mutex;
41         unsigned long           last_read; /* jiffies when cache last updated */
42         u8                      cache[16];
43         struct i2c_client       i2c;
44         struct device_node      *node;
45 };
46
47 static struct wf_sat *sats[2];
48
49 struct wf_sat_sensor {
50         int             index;
51         int             index2;         /* used for power sensors */
52         int             shift;
53         struct wf_sat   *sat;
54         struct wf_sensor sens;
55 };
56
57 #define wf_to_sat(c)    container_of(c, struct wf_sat_sensor, sens)
58 #define i2c_to_sat(c)   container_of(c, struct wf_sat, i2c)
59
60 static int wf_sat_attach(struct i2c_adapter *adapter);
61 static int wf_sat_detach(struct i2c_client *client);
62
63 static struct i2c_driver wf_sat_driver = {
64         .driver = {
65                 .name           = "wf_smu_sat",
66         },
67         .attach_adapter = wf_sat_attach,
68         .detach_client  = wf_sat_detach,
69 };
70
71 /*
72  * XXX i2c_smbus_read_i2c_block_data doesn't pass the requested
73  * length down to the low-level driver, so we use this, which
74  * works well enough with the SMU i2c driver code...
75  */
76 static int sat_read_block(struct i2c_client *client, u8 command,
77                           u8 *values, int len)
78 {
79         union i2c_smbus_data data;
80         int err;
81
82         data.block[0] = len;
83         err = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
84                              I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA,
85                              &data);
86         if (!err)
87                 memcpy(values, data.block, len);
88         return err;
89 }
90
91 struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
92                                                   unsigned int *size)
93 {
94         struct wf_sat *sat;
95         int err;
96         unsigned int i, len;
97         u8 *buf;
98         u8 data[4];
99
100         /* TODO: Add the resulting partition to the device-tree */
101
102         if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
103                 return NULL;
104
105         err = i2c_smbus_write_word_data(&sat->i2c, 8, id << 8);
106         if (err) {
107                 printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
108                 return NULL;
109         }
110
111         len = i2c_smbus_read_word_data(&sat->i2c, 9);
112         if (len < 0) {
113                 printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
114                 return NULL;
115         }
116         if (len == 0) {
117                 printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
118                 return NULL;
119         }
120
121         len = le16_to_cpu(len);
122         len = (len + 3) & ~3;
123         buf = kmalloc(len, GFP_KERNEL);
124         if (buf == NULL)
125                 return NULL;
126
127         for (i = 0; i < len; i += 4) {
128                 err = sat_read_block(&sat->i2c, 0xa, data, 4);
129                 if (err) {
130                         printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
131                                err);
132                         goto fail;
133                 }
134                 buf[i] = data[1];
135                 buf[i+1] = data[0];
136                 buf[i+2] = data[3];
137                 buf[i+3] = data[2];
138         }
139 #ifdef DEBUG
140         DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id);
141         for (i = 0; i < len; ++i)
142                 DBG(" %x", buf[i]);
143         DBG("\n");
144 #endif
145
146         if (size)
147                 *size = len;
148         return (struct smu_sdbp_header *) buf;
149
150  fail:
151         kfree(buf);
152         return NULL;
153 }
154 EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
155
156 /* refresh the cache */
157 static int wf_sat_read_cache(struct wf_sat *sat)
158 {
159         int err;
160
161         err = sat_read_block(&sat->i2c, 0x3f, sat->cache, 16);
162         if (err)
163                 return err;
164         sat->last_read = jiffies;
165 #ifdef LOTSA_DEBUG
166         {
167                 int i;
168                 DBG(KERN_DEBUG "wf_sat_get: data is");
169                 for (i = 0; i < 16; ++i)
170                         DBG(" %.2x", sat->cache[i]);
171                 DBG("\n");
172         }
173 #endif
174         return 0;
175 }
176
177 static int wf_sat_get(struct wf_sensor *sr, s32 *value)
178 {
179         struct wf_sat_sensor *sens = wf_to_sat(sr);
180         struct wf_sat *sat = sens->sat;
181         int i, err;
182         s32 val;
183
184         if (sat->i2c.adapter == NULL)
185                 return -ENODEV;
186
187         down(&sat->mutex);
188         if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
189                 err = wf_sat_read_cache(sat);
190                 if (err)
191                         goto fail;
192         }
193
194         i = sens->index * 2;
195         val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
196         if (sens->index2 >= 0) {
197                 i = sens->index2 * 2;
198                 /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
199                 val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
200         }
201
202         *value = val;
203         err = 0;
204
205  fail:
206         up(&sat->mutex);
207         return err;
208 }
209
210 static void wf_sat_release(struct wf_sensor *sr)
211 {
212         struct wf_sat_sensor *sens = wf_to_sat(sr);
213         struct wf_sat *sat = sens->sat;
214
215         if (atomic_dec_and_test(&sat->refcnt)) {
216                 if (sat->i2c.adapter) {
217                         i2c_detach_client(&sat->i2c);
218                         sat->i2c.adapter = NULL;
219                 }
220                 if (sat->nr >= 0)
221                         sats[sat->nr] = NULL;
222                 kfree(sat);
223         }
224         kfree(sens);
225 }
226
227 static struct wf_sensor_ops wf_sat_ops = {
228         .get_value      = wf_sat_get,
229         .release        = wf_sat_release,
230         .owner          = THIS_MODULE,
231 };
232
233 static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
234 {
235         struct wf_sat *sat;
236         struct wf_sat_sensor *sens;
237         u32 *reg;
238         char *loc, *type;
239         u8 addr, chip, core;
240         struct device_node *child;
241         int shift, cpu, index;
242         char *name;
243         int vsens[2], isens[2];
244
245         reg = (u32 *) get_property(dev, "reg", NULL);
246         if (reg == NULL)
247                 return;
248         addr = *reg;
249         DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);
250
251         sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
252         if (sat == NULL)
253                 return;
254         sat->nr = -1;
255         sat->node = of_node_get(dev);
256         atomic_set(&sat->refcnt, 0);
257         init_MUTEX(&sat->mutex);
258         sat->i2c.addr = (addr >> 1) & 0x7f;
259         sat->i2c.adapter = adapter;
260         sat->i2c.driver = &wf_sat_driver;
261         strncpy(sat->i2c.name, "smu-sat", I2C_NAME_SIZE-1);
262
263         if (i2c_attach_client(&sat->i2c)) {
264                 printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");
265                 goto fail;
266         }
267
268         vsens[0] = vsens[1] = -1;
269         isens[0] = isens[1] = -1;
270         child = NULL;
271         while ((child = of_get_next_child(dev, child)) != NULL) {
272                 reg = (u32 *) get_property(child, "reg", NULL);
273                 type = get_property(child, "device_type", NULL);
274                 loc = get_property(child, "location", NULL);
275                 if (reg == NULL || loc == NULL)
276                         continue;
277
278                 /* the cooked sensors are between 0x30 and 0x37 */
279                 if (*reg < 0x30 || *reg > 0x37)
280                         continue;
281                 index = *reg - 0x30;
282
283                 /* expect location to be CPU [AB][01] ... */
284                 if (strncmp(loc, "CPU ", 4) != 0)
285                         continue;
286                 chip = loc[4] - 'A';
287                 core = loc[5] - '0';
288                 if (chip > 1 || core > 1) {
289                         printk(KERN_ERR "wf_sat_create: don't understand "
290                                "location %s for %s\n", loc, child->full_name);
291                         continue;
292                 }
293                 cpu = 2 * chip + core;
294                 if (sat->nr < 0)
295                         sat->nr = chip;
296                 else if (sat->nr != chip) {
297                         printk(KERN_ERR "wf_sat_create: can't cope with "
298                                "multiple CPU chips on one SAT (%s)\n", loc);
299                         continue;
300                 }
301
302                 if (strcmp(type, "voltage-sensor") == 0) {
303                         name = "cpu-voltage";
304                         shift = 4;
305                         vsens[core] = index;
306                 } else if (strcmp(type, "current-sensor") == 0) {
307                         name = "cpu-current";
308                         shift = 8;
309                         isens[core] = index;
310                 } else if (strcmp(type, "temp-sensor") == 0) {
311                         name = "cpu-temp";
312                         shift = 10;
313                 } else
314                         continue;       /* hmmm shouldn't happen */
315
316                 /* the +16 is enough for "cpu-voltage-n" */
317                 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
318                 if (sens == NULL) {
319                         printk(KERN_ERR "wf_sat_create: couldn't create "
320                                "%s sensor %d (no memory)\n", name, cpu);
321                         continue;
322                 }
323                 sens->index = index;
324                 sens->index2 = -1;
325                 sens->shift = shift;
326                 sens->sat = sat;
327                 atomic_inc(&sat->refcnt);
328                 sens->sens.ops = &wf_sat_ops;
329                 sens->sens.name = (char *) (sens + 1);
330                 snprintf(sens->sens.name, 16, "%s-%d", name, cpu);
331
332                 if (wf_register_sensor(&sens->sens)) {
333                         atomic_dec(&sat->refcnt);
334                         kfree(sens);
335                 }
336         }
337
338         /* make the power sensors */
339         for (core = 0; core < 2; ++core) {
340                 if (vsens[core] < 0 || isens[core] < 0)
341                         continue;
342                 cpu = 2 * sat->nr + core;
343                 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
344                 if (sens == NULL) {
345                         printk(KERN_ERR "wf_sat_create: couldn't create power "
346                                "sensor %d (no memory)\n", cpu);
347                         continue;
348                 }
349                 sens->index = vsens[core];
350                 sens->index2 = isens[core];
351                 sens->shift = 0;
352                 sens->sat = sat;
353                 atomic_inc(&sat->refcnt);
354                 sens->sens.ops = &wf_sat_ops;
355                 sens->sens.name = (char *) (sens + 1);
356                 snprintf(sens->sens.name, 16, "cpu-power-%d", cpu);
357
358                 if (wf_register_sensor(&sens->sens)) {
359                         atomic_dec(&sat->refcnt);
360                         kfree(sens);
361                 }
362         }
363
364         if (sat->nr >= 0)
365                 sats[sat->nr] = sat;
366
367         return;
368
369  fail:
370         kfree(sat);
371 }
372
373 static int wf_sat_attach(struct i2c_adapter *adapter)
374 {
375         struct device_node *busnode, *dev = NULL;
376         struct pmac_i2c_bus *bus;
377
378         bus = pmac_i2c_adapter_to_bus(adapter);
379         if (bus == NULL)
380                 return -ENODEV;
381         busnode = pmac_i2c_get_bus_node(bus);
382
383         while ((dev = of_get_next_child(busnode, dev)) != NULL)
384                 if (device_is_compatible(dev, "smu-sat"))
385                         wf_sat_create(adapter, dev);
386         return 0;
387 }
388
389 static int wf_sat_detach(struct i2c_client *client)
390 {
391         struct wf_sat *sat = i2c_to_sat(client);
392
393         /* XXX TODO */
394
395         sat->i2c.adapter = NULL;
396         return 0;
397 }
398
399 static int __init sat_sensors_init(void)
400 {
401         int err;
402
403         err = i2c_add_driver(&wf_sat_driver);
404         if (err < 0)
405                 return err;
406         return 0;
407 }
408
409 static void __exit sat_sensors_exit(void)
410 {
411         i2c_del_driver(&wf_sat_driver);
412 }
413
414 module_init(sat_sensors_init);
415 /*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */
416
417 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
418 MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
419 MODULE_LICENSE("GPL");