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)
58 struct smu_sdbp_header
*smu_sat_get_sdb_partition(unsigned int sat_id
, int id
,
67 /* TODO: Add the resulting partition to the device-tree */
69 if (sat_id
> 1 || (sat
= sats
[sat_id
]) == NULL
)
72 err
= i2c_smbus_write_word_data(sat
->i2c
, 8, id
<< 8);
74 printk(KERN_ERR
"smu_sat_get_sdb_part wr error %d\n", err
);
78 err
= i2c_smbus_read_word_data(sat
->i2c
, 9);
80 printk(KERN_ERR
"smu_sat_get_sdb_part rd len error\n");
85 printk(KERN_ERR
"smu_sat_get_sdb_part no partition %x\n", id
);
89 len
= le16_to_cpu(len
);
91 buf
= kmalloc(len
, GFP_KERNEL
);
95 for (i
= 0; i
< len
; i
+= 4) {
96 err
= i2c_smbus_read_i2c_block_data(sat
->i2c
, 0xa, 4, data
);
98 printk(KERN_ERR
"smu_sat_get_sdb_part rd err %d\n",
108 DBG(KERN_DEBUG
"sat %d partition %x:", sat_id
, id
);
109 for (i
= 0; i
< len
; ++i
)
116 return (struct smu_sdbp_header
*) buf
;
122 EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition
);
124 /* refresh the cache */
125 static int wf_sat_read_cache(struct wf_sat
*sat
)
129 err
= i2c_smbus_read_i2c_block_data(sat
->i2c
, 0x3f, 16, sat
->cache
);
132 sat
->last_read
= jiffies
;
136 DBG(KERN_DEBUG
"wf_sat_get: data is");
137 for (i
= 0; i
< 16; ++i
)
138 DBG(" %.2x", sat
->cache
[i
]);
145 static int wf_sat_get(struct wf_sensor
*sr
, s32
*value
)
147 struct wf_sat_sensor
*sens
= wf_to_sat(sr
);
148 struct wf_sat
*sat
= sens
->sat
;
152 if (sat
->i2c
== NULL
)
155 mutex_lock(&sat
->mutex
);
156 if (time_after(jiffies
, (sat
->last_read
+ MAX_AGE
))) {
157 err
= wf_sat_read_cache(sat
);
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;
174 mutex_unlock(&sat
->mutex
);
178 static void wf_sat_release(struct wf_sensor
*sr
)
180 struct wf_sat_sensor
*sens
= wf_to_sat(sr
);
181 struct wf_sat
*sat
= sens
->sat
;
183 if (atomic_dec_and_test(&sat
->refcnt
)) {
185 sats
[sat
->nr
] = NULL
;
191 static struct wf_sensor_ops wf_sat_ops
= {
192 .get_value
= wf_sat_get
,
193 .release
= wf_sat_release
,
194 .owner
= THIS_MODULE
,
197 static void wf_sat_create(struct i2c_adapter
*adapter
, struct device_node
*dev
)
199 struct i2c_board_info info
;
200 struct i2c_client
*client
;
204 reg
= of_get_property(dev
, "reg", NULL
);
208 DBG(KERN_DEBUG
"wf_sat: creating sat at address %x\n", addr
);
210 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
);
215 client
= i2c_new_device(adapter
, &info
);
216 if (client
== NULL
) {
217 printk(KERN_ERR
"windfarm: failed to attach smu-sat to i2c\n");
222 * Let i2c-core delete that device on driver removal.
223 * This is safe because i2c-core holds the core_lock mutex for us.
225 list_add_tail(&client
->detected
, &client
->driver
->clients
);
228 static int wf_sat_probe(struct i2c_client
*client
,
229 const struct i2c_device_id
*id
)
231 struct device_node
*dev
= client
->dev
.platform_data
;
233 struct wf_sat_sensor
*sens
;
235 const char *loc
, *type
;
237 struct device_node
*child
;
238 int shift
, cpu
, index
;
240 int vsens
[2], isens
[2];
242 sat
= kzalloc(sizeof(struct wf_sat
), GFP_KERNEL
);
246 sat
->node
= of_node_get(dev
);
247 atomic_set(&sat
->refcnt
, 0);
248 mutex_init(&sat
->mutex
);
250 i2c_set_clientdata(client
, sat
);
252 vsens
[0] = vsens
[1] = -1;
253 isens
[0] = isens
[1] = -1;
255 while ((child
= of_get_next_child(dev
, child
)) != NULL
) {
256 reg
= of_get_property(child
, "reg", NULL
);
257 type
= of_get_property(child
, "device_type", NULL
);
258 loc
= of_get_property(child
, "location", NULL
);
259 if (reg
== NULL
|| loc
== NULL
)
262 /* the cooked sensors are between 0x30 and 0x37 */
263 if (*reg
< 0x30 || *reg
> 0x37)
267 /* expect location to be CPU [AB][01] ... */
268 if (strncmp(loc
, "CPU ", 4) != 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
);
277 cpu
= 2 * chip
+ core
;
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
);
286 if (strcmp(type
, "voltage-sensor") == 0) {
287 name
= "cpu-voltage";
290 } else if (strcmp(type
, "current-sensor") == 0) {
291 name
= "cpu-current";
294 } else if (strcmp(type
, "temp-sensor") == 0) {
298 continue; /* hmmm shouldn't happen */
300 /* the +16 is enough for "cpu-voltage-n" */
301 sens
= kzalloc(sizeof(struct wf_sat_sensor
) + 16, GFP_KERNEL
);
303 printk(KERN_ERR
"wf_sat_create: couldn't create "
304 "%s sensor %d (no memory)\n", name
, cpu
);
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
);
316 if (wf_register_sensor(&sens
->sens
)) {
317 atomic_dec(&sat
->refcnt
);
322 /* make the power sensors */
323 for (core
= 0; core
< 2; ++core
) {
324 if (vsens
[core
] < 0 || isens
[core
] < 0)
326 cpu
= 2 * sat
->nr
+ core
;
327 sens
= kzalloc(sizeof(struct wf_sat_sensor
) + 16, GFP_KERNEL
);
329 printk(KERN_ERR
"wf_sat_create: couldn't create power "
330 "sensor %d (no memory)\n", cpu
);
333 sens
->index
= vsens
[core
];
334 sens
->index2
= isens
[core
];
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
);
342 if (wf_register_sensor(&sens
->sens
)) {
343 atomic_dec(&sat
->refcnt
);
354 static int wf_sat_attach(struct i2c_adapter
*adapter
)
356 struct device_node
*busnode
, *dev
= NULL
;
357 struct pmac_i2c_bus
*bus
;
359 bus
= pmac_i2c_adapter_to_bus(adapter
);
362 busnode
= pmac_i2c_get_bus_node(bus
);
364 while ((dev
= of_get_next_child(busnode
, dev
)) != NULL
)
365 if (of_device_is_compatible(dev
, "smu-sat"))
366 wf_sat_create(adapter
, dev
);
370 static int wf_sat_remove(struct i2c_client
*client
)
372 struct wf_sat
*sat
= i2c_get_clientdata(client
);
377 i2c_set_clientdata(client
, NULL
);
381 static const struct i2c_device_id wf_sat_id
[] = {
386 static struct i2c_driver wf_sat_driver
= {
388 .name
= "wf_smu_sat",
390 .attach_adapter
= wf_sat_attach
,
391 .probe
= wf_sat_probe
,
392 .remove
= wf_sat_remove
,
393 .id_table
= wf_sat_id
,
396 static int __init
sat_sensors_init(void)
398 return i2c_add_driver(&wf_sat_driver
);
401 #if 0 /* uncomment when module_exit() below is uncommented */
402 static void __exit
sat_sensors_exit(void)
404 i2c_del_driver(&wf_sat_driver
);
408 module_init(sat_sensors_init
);
409 /*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */
411 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
412 MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
413 MODULE_LICENSE("GPL");