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>
28 #define DBG(args...) printk(args)
30 #define DBG(args...) do { } while(0)
33 /* If the cache is older than 800ms we'll refetch it */
34 #define MAX_AGE msecs_to_jiffies(800)
40 unsigned long last_read
; /* jiffies when cache last updated */
42 struct i2c_client i2c
;
43 struct device_node
*node
;
46 static struct wf_sat
*sats
[2];
48 struct wf_sat_sensor
{
50 int index2
; /* used for power sensors */
53 struct wf_sensor sens
;
56 #define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)
57 #define i2c_to_sat(c) container_of(c, struct wf_sat, i2c)
59 static int wf_sat_attach(struct i2c_adapter
*adapter
);
60 static int wf_sat_detach(struct i2c_client
*client
);
62 static struct i2c_driver wf_sat_driver
= {
66 .attach_adapter
= wf_sat_attach
,
67 .detach_client
= wf_sat_detach
,
70 struct smu_sdbp_header
*smu_sat_get_sdb_partition(unsigned int sat_id
, int id
,
79 /* TODO: Add the resulting partition to the device-tree */
81 if (sat_id
> 1 || (sat
= sats
[sat_id
]) == NULL
)
84 err
= i2c_smbus_write_word_data(&sat
->i2c
, 8, id
<< 8);
86 printk(KERN_ERR
"smu_sat_get_sdb_part wr error %d\n", err
);
90 len
= i2c_smbus_read_word_data(&sat
->i2c
, 9);
92 printk(KERN_ERR
"smu_sat_get_sdb_part rd len error\n");
96 printk(KERN_ERR
"smu_sat_get_sdb_part no partition %x\n", id
);
100 len
= le16_to_cpu(len
);
101 len
= (len
+ 3) & ~3;
102 buf
= kmalloc(len
, GFP_KERNEL
);
106 for (i
= 0; i
< len
; i
+= 4) {
107 err
= i2c_smbus_read_i2c_block_data(&sat
->i2c
, 0xa, 4, data
);
109 printk(KERN_ERR
"smu_sat_get_sdb_part rd err %d\n",
119 DBG(KERN_DEBUG
"sat %d partition %x:", sat_id
, id
);
120 for (i
= 0; i
< len
; ++i
)
127 return (struct smu_sdbp_header
*) buf
;
133 EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition
);
135 /* refresh the cache */
136 static int wf_sat_read_cache(struct wf_sat
*sat
)
140 err
= i2c_smbus_read_i2c_block_data(&sat
->i2c
, 0x3f, 16, sat
->cache
);
143 sat
->last_read
= jiffies
;
147 DBG(KERN_DEBUG
"wf_sat_get: data is");
148 for (i
= 0; i
< 16; ++i
)
149 DBG(" %.2x", sat
->cache
[i
]);
156 static int wf_sat_get(struct wf_sensor
*sr
, s32
*value
)
158 struct wf_sat_sensor
*sens
= wf_to_sat(sr
);
159 struct wf_sat
*sat
= sens
->sat
;
163 if (sat
->i2c
.adapter
== NULL
)
166 mutex_lock(&sat
->mutex
);
167 if (time_after(jiffies
, (sat
->last_read
+ MAX_AGE
))) {
168 err
= wf_sat_read_cache(sat
);
174 val
= ((sat
->cache
[i
] << 8) + sat
->cache
[i
+1]) << sens
->shift
;
175 if (sens
->index2
>= 0) {
176 i
= sens
->index2
* 2;
177 /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
178 val
= (val
* ((sat
->cache
[i
] << 8) + sat
->cache
[i
+1])) >> 4;
185 mutex_unlock(&sat
->mutex
);
189 static void wf_sat_release(struct wf_sensor
*sr
)
191 struct wf_sat_sensor
*sens
= wf_to_sat(sr
);
192 struct wf_sat
*sat
= sens
->sat
;
194 if (atomic_dec_and_test(&sat
->refcnt
)) {
195 if (sat
->i2c
.adapter
) {
196 i2c_detach_client(&sat
->i2c
);
197 sat
->i2c
.adapter
= NULL
;
200 sats
[sat
->nr
] = NULL
;
206 static struct wf_sensor_ops wf_sat_ops
= {
207 .get_value
= wf_sat_get
,
208 .release
= wf_sat_release
,
209 .owner
= THIS_MODULE
,
212 static void wf_sat_create(struct i2c_adapter
*adapter
, struct device_node
*dev
)
215 struct wf_sat_sensor
*sens
;
217 const char *loc
, *type
;
219 struct device_node
*child
;
220 int shift
, cpu
, index
;
222 int vsens
[2], isens
[2];
224 reg
= of_get_property(dev
, "reg", NULL
);
228 DBG(KERN_DEBUG
"wf_sat: creating sat at address %x\n", addr
);
230 sat
= kzalloc(sizeof(struct wf_sat
), GFP_KERNEL
);
234 sat
->node
= of_node_get(dev
);
235 atomic_set(&sat
->refcnt
, 0);
236 mutex_init(&sat
->mutex
);
237 sat
->i2c
.addr
= (addr
>> 1) & 0x7f;
238 sat
->i2c
.adapter
= adapter
;
239 sat
->i2c
.driver
= &wf_sat_driver
;
240 strncpy(sat
->i2c
.name
, "smu-sat", I2C_NAME_SIZE
-1);
242 if (i2c_attach_client(&sat
->i2c
)) {
243 printk(KERN_ERR
"windfarm: failed to attach smu-sat to i2c\n");
247 vsens
[0] = vsens
[1] = -1;
248 isens
[0] = isens
[1] = -1;
250 while ((child
= of_get_next_child(dev
, child
)) != NULL
) {
251 reg
= of_get_property(child
, "reg", NULL
);
252 type
= of_get_property(child
, "device_type", NULL
);
253 loc
= of_get_property(child
, "location", NULL
);
254 if (reg
== NULL
|| loc
== NULL
)
257 /* the cooked sensors are between 0x30 and 0x37 */
258 if (*reg
< 0x30 || *reg
> 0x37)
262 /* expect location to be CPU [AB][01] ... */
263 if (strncmp(loc
, "CPU ", 4) != 0)
267 if (chip
> 1 || core
> 1) {
268 printk(KERN_ERR
"wf_sat_create: don't understand "
269 "location %s for %s\n", loc
, child
->full_name
);
272 cpu
= 2 * chip
+ core
;
275 else if (sat
->nr
!= chip
) {
276 printk(KERN_ERR
"wf_sat_create: can't cope with "
277 "multiple CPU chips on one SAT (%s)\n", loc
);
281 if (strcmp(type
, "voltage-sensor") == 0) {
282 name
= "cpu-voltage";
285 } else if (strcmp(type
, "current-sensor") == 0) {
286 name
= "cpu-current";
289 } else if (strcmp(type
, "temp-sensor") == 0) {
293 continue; /* hmmm shouldn't happen */
295 /* the +16 is enough for "cpu-voltage-n" */
296 sens
= kzalloc(sizeof(struct wf_sat_sensor
) + 16, GFP_KERNEL
);
298 printk(KERN_ERR
"wf_sat_create: couldn't create "
299 "%s sensor %d (no memory)\n", name
, cpu
);
306 atomic_inc(&sat
->refcnt
);
307 sens
->sens
.ops
= &wf_sat_ops
;
308 sens
->sens
.name
= (char *) (sens
+ 1);
309 snprintf(sens
->sens
.name
, 16, "%s-%d", name
, cpu
);
311 if (wf_register_sensor(&sens
->sens
)) {
312 atomic_dec(&sat
->refcnt
);
317 /* make the power sensors */
318 for (core
= 0; core
< 2; ++core
) {
319 if (vsens
[core
] < 0 || isens
[core
] < 0)
321 cpu
= 2 * sat
->nr
+ core
;
322 sens
= kzalloc(sizeof(struct wf_sat_sensor
) + 16, GFP_KERNEL
);
324 printk(KERN_ERR
"wf_sat_create: couldn't create power "
325 "sensor %d (no memory)\n", cpu
);
328 sens
->index
= vsens
[core
];
329 sens
->index2
= isens
[core
];
332 atomic_inc(&sat
->refcnt
);
333 sens
->sens
.ops
= &wf_sat_ops
;
334 sens
->sens
.name
= (char *) (sens
+ 1);
335 snprintf(sens
->sens
.name
, 16, "cpu-power-%d", cpu
);
337 if (wf_register_sensor(&sens
->sens
)) {
338 atomic_dec(&sat
->refcnt
);
352 static int wf_sat_attach(struct i2c_adapter
*adapter
)
354 struct device_node
*busnode
, *dev
= NULL
;
355 struct pmac_i2c_bus
*bus
;
357 bus
= pmac_i2c_adapter_to_bus(adapter
);
360 busnode
= pmac_i2c_get_bus_node(bus
);
362 while ((dev
= of_get_next_child(busnode
, dev
)) != NULL
)
363 if (of_device_is_compatible(dev
, "smu-sat"))
364 wf_sat_create(adapter
, dev
);
368 static int wf_sat_detach(struct i2c_client
*client
)
370 struct wf_sat
*sat
= i2c_to_sat(client
);
374 sat
->i2c
.adapter
= NULL
;
378 static int __init
sat_sensors_init(void)
380 return i2c_add_driver(&wf_sat_driver
);
383 #if 0 /* uncomment when module_exit() below is uncommented */
384 static void __exit
sat_sensors_exit(void)
386 i2c_del_driver(&wf_sat_driver
);
390 module_init(sat_sensors_init
);
391 /*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */
393 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
394 MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
395 MODULE_LICENSE("GPL");