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 list_head sensors
;
43 struct i2c_client
*i2c
;
44 struct device_node
*node
;
47 static struct wf_sat
*sats
[2];
49 struct wf_sat_sensor
{
50 struct list_head link
;
52 int index2
; /* used for power sensors */
55 struct wf_sensor sens
;
58 #define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)
60 struct smu_sdbp_header
*smu_sat_get_sdb_partition(unsigned int sat_id
, int id
,
69 /* TODO: Add the resulting partition to the device-tree */
71 if (sat_id
> 1 || (sat
= sats
[sat_id
]) == NULL
)
74 err
= i2c_smbus_write_word_data(sat
->i2c
, 8, id
<< 8);
76 printk(KERN_ERR
"smu_sat_get_sdb_part wr error %d\n", err
);
80 err
= i2c_smbus_read_word_data(sat
->i2c
, 9);
82 printk(KERN_ERR
"smu_sat_get_sdb_part rd len error\n");
87 printk(KERN_ERR
"smu_sat_get_sdb_part no partition %x\n", id
);
91 len
= le16_to_cpu(len
);
93 buf
= kmalloc(len
, GFP_KERNEL
);
97 for (i
= 0; i
< len
; i
+= 4) {
98 err
= i2c_smbus_read_i2c_block_data(sat
->i2c
, 0xa, 4, data
);
100 printk(KERN_ERR
"smu_sat_get_sdb_part rd err %d\n",
110 DBG(KERN_DEBUG
"sat %d partition %x:", sat_id
, id
);
111 for (i
= 0; i
< len
; ++i
)
118 return (struct smu_sdbp_header
*) buf
;
124 EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition
);
126 /* refresh the cache */
127 static int wf_sat_read_cache(struct wf_sat
*sat
)
131 err
= i2c_smbus_read_i2c_block_data(sat
->i2c
, 0x3f, 16, sat
->cache
);
134 sat
->last_read
= jiffies
;
138 DBG(KERN_DEBUG
"wf_sat_get: data is");
139 for (i
= 0; i
< 16; ++i
)
140 DBG(" %.2x", sat
->cache
[i
]);
147 static int wf_sat_sensor_get(struct wf_sensor
*sr
, s32
*value
)
149 struct wf_sat_sensor
*sens
= wf_to_sat(sr
);
150 struct wf_sat
*sat
= sens
->sat
;
154 if (sat
->i2c
== NULL
)
157 mutex_lock(&sat
->mutex
);
158 if (time_after(jiffies
, (sat
->last_read
+ MAX_AGE
))) {
159 err
= wf_sat_read_cache(sat
);
165 val
= ((sat
->cache
[i
] << 8) + sat
->cache
[i
+1]) << sens
->shift
;
166 if (sens
->index2
>= 0) {
167 i
= sens
->index2
* 2;
168 /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
169 val
= (val
* ((sat
->cache
[i
] << 8) + sat
->cache
[i
+1])) >> 4;
176 mutex_unlock(&sat
->mutex
);
180 static void wf_sat_release(struct kref
*ref
)
182 struct wf_sat
*sat
= container_of(ref
, struct wf_sat
, ref
);
185 sats
[sat
->nr
] = NULL
;
189 static void wf_sat_sensor_release(struct wf_sensor
*sr
)
191 struct wf_sat_sensor
*sens
= wf_to_sat(sr
);
192 struct wf_sat
*sat
= sens
->sat
;
195 kref_put(&sat
->ref
, wf_sat_release
);
198 static struct wf_sensor_ops wf_sat_ops
= {
199 .get_value
= wf_sat_sensor_get
,
200 .release
= wf_sat_sensor_release
,
201 .owner
= THIS_MODULE
,
204 static int wf_sat_probe(struct i2c_client
*client
,
205 const struct i2c_device_id
*id
)
207 struct device_node
*dev
= client
->dev
.of_node
;
209 struct wf_sat_sensor
*sens
;
211 const char *loc
, *type
;
213 struct device_node
*child
;
214 int shift
, cpu
, index
;
216 int vsens
[2], isens
[2];
218 sat
= kzalloc(sizeof(struct wf_sat
), GFP_KERNEL
);
222 sat
->node
= of_node_get(dev
);
223 kref_init(&sat
->ref
);
224 mutex_init(&sat
->mutex
);
226 INIT_LIST_HEAD(&sat
->sensors
);
227 i2c_set_clientdata(client
, sat
);
229 vsens
[0] = vsens
[1] = -1;
230 isens
[0] = isens
[1] = -1;
232 while ((child
= of_get_next_child(dev
, child
)) != NULL
) {
233 reg
= of_get_property(child
, "reg", NULL
);
234 type
= of_get_property(child
, "device_type", NULL
);
235 loc
= of_get_property(child
, "location", NULL
);
236 if (reg
== NULL
|| loc
== NULL
)
239 /* the cooked sensors are between 0x30 and 0x37 */
240 if (*reg
< 0x30 || *reg
> 0x37)
244 /* expect location to be CPU [AB][01] ... */
245 if (strncmp(loc
, "CPU ", 4) != 0)
249 if (chip
> 1 || core
> 1) {
250 printk(KERN_ERR
"wf_sat_create: don't understand "
251 "location %s for %s\n", loc
, child
->full_name
);
254 cpu
= 2 * chip
+ core
;
257 else if (sat
->nr
!= chip
) {
258 printk(KERN_ERR
"wf_sat_create: can't cope with "
259 "multiple CPU chips on one SAT (%s)\n", loc
);
263 if (strcmp(type
, "voltage-sensor") == 0) {
264 name
= "cpu-voltage";
267 } else if (strcmp(type
, "current-sensor") == 0) {
268 name
= "cpu-current";
271 } else if (strcmp(type
, "temp-sensor") == 0) {
275 continue; /* hmmm shouldn't happen */
277 /* the +16 is enough for "cpu-voltage-n" */
278 sens
= kzalloc(sizeof(struct wf_sat_sensor
) + 16, GFP_KERNEL
);
280 printk(KERN_ERR
"wf_sat_create: couldn't create "
281 "%s sensor %d (no memory)\n", name
, cpu
);
288 sens
->sens
.ops
= &wf_sat_ops
;
289 sens
->sens
.name
= (char *) (sens
+ 1);
290 snprintf((char *)sens
->sens
.name
, 16, "%s-%d", name
, cpu
);
292 if (wf_register_sensor(&sens
->sens
))
295 list_add(&sens
->link
, &sat
->sensors
);
300 /* make the power sensors */
301 for (core
= 0; core
< 2; ++core
) {
302 if (vsens
[core
] < 0 || isens
[core
] < 0)
304 cpu
= 2 * sat
->nr
+ core
;
305 sens
= kzalloc(sizeof(struct wf_sat_sensor
) + 16, GFP_KERNEL
);
307 printk(KERN_ERR
"wf_sat_create: couldn't create power "
308 "sensor %d (no memory)\n", cpu
);
311 sens
->index
= vsens
[core
];
312 sens
->index2
= isens
[core
];
315 sens
->sens
.ops
= &wf_sat_ops
;
316 sens
->sens
.name
= (char *) (sens
+ 1);
317 snprintf((char *)sens
->sens
.name
, 16, "cpu-power-%d", cpu
);
319 if (wf_register_sensor(&sens
->sens
))
322 list_add(&sens
->link
, &sat
->sensors
);
333 static int wf_sat_remove(struct i2c_client
*client
)
335 struct wf_sat
*sat
= i2c_get_clientdata(client
);
336 struct wf_sat_sensor
*sens
;
338 /* release sensors */
339 while(!list_empty(&sat
->sensors
)) {
340 sens
= list_first_entry(&sat
->sensors
,
341 struct wf_sat_sensor
, link
);
342 list_del(&sens
->link
);
343 wf_unregister_sensor(&sens
->sens
);
346 i2c_set_clientdata(client
, NULL
);
347 kref_put(&sat
->ref
, wf_sat_release
);
352 static const struct i2c_device_id wf_sat_id
[] = {
353 { "MAC,smu-sat", 0 },
356 MODULE_DEVICE_TABLE(i2c
, wf_sat_id
);
358 static struct i2c_driver wf_sat_driver
= {
360 .name
= "wf_smu_sat",
362 .probe
= wf_sat_probe
,
363 .remove
= wf_sat_remove
,
364 .id_table
= wf_sat_id
,
367 static int __init
sat_sensors_init(void)
369 return i2c_add_driver(&wf_sat_driver
);
372 static void __exit
sat_sensors_exit(void)
374 i2c_del_driver(&wf_sat_driver
);
377 module_init(sat_sensors_init
);
378 module_exit(sat_sensors_exit
);
380 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
381 MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
382 MODULE_LICENSE("GPL");