1 // SPDX-License-Identifier: GPL-2.0-only
3 * Windfarm PowerMac thermal control. SMU "satellite" controller sensors.
5 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/wait.h>
14 #include <linux/i2c.h>
15 #include <linux/mutex.h>
18 #include <asm/pmac_low_i2c.h>
24 /* If the cache is older than 800ms we'll refetch it */
25 #define MAX_AGE msecs_to_jiffies(800)
31 unsigned long last_read; /* jiffies when cache last updated */
33 struct list_head sensors;
34 struct i2c_client *i2c;
35 struct device_node *node;
38 static struct wf_sat *sats[2];
40 struct wf_sat_sensor {
41 struct list_head link;
43 int index2; /* used for power sensors */
46 struct wf_sensor sens;
49 #define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)
51 struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
60 /* TODO: Add the resulting partition to the device-tree */
62 if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
65 err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
67 printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
71 err = i2c_smbus_read_word_data(sat->i2c, 9);
73 printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
78 printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
82 len = le16_to_cpu(len);
84 buf = kmalloc(len, GFP_KERNEL);
88 for (i = 0; i < len; i += 4) {
89 err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
91 printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
101 printk(KERN_DEBUG "sat %d partition %x:", sat_id, id);
102 print_hex_dump(KERN_DEBUG, " ", DUMP_PREFIX_OFFSET,
103 16, 1, buf, len, false);
106 return (struct smu_sdbp_header *) buf;
112 EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
114 /* refresh the cache */
115 static int wf_sat_read_cache(struct wf_sat *sat)
119 err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
122 sat->last_read = jiffies;
127 printk(KERN_DEBUG "wf_sat_get: data is");
128 print_hex_dump(KERN_DEBUG, " ", DUMP_PREFIX_OFFSET,
129 16, 1, sat->cache, 16, false);
135 static int wf_sat_sensor_get(struct wf_sensor *sr, s32 *value)
137 struct wf_sat_sensor *sens = wf_to_sat(sr);
138 struct wf_sat *sat = sens->sat;
142 if (sat->i2c == NULL)
145 mutex_lock(&sat->mutex);
146 if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
147 err = wf_sat_read_cache(sat);
153 val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
154 if (sens->index2 >= 0) {
155 i = sens->index2 * 2;
156 /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
157 val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
164 mutex_unlock(&sat->mutex);
168 static void wf_sat_release(struct kref *ref)
170 struct wf_sat *sat = container_of(ref, struct wf_sat, ref);
173 sats[sat->nr] = NULL;
174 of_node_put(sat->node);
178 static void wf_sat_sensor_release(struct wf_sensor *sr)
180 struct wf_sat_sensor *sens = wf_to_sat(sr);
181 struct wf_sat *sat = sens->sat;
184 kref_put(&sat->ref, wf_sat_release);
187 static const struct wf_sensor_ops wf_sat_ops = {
188 .get_value = wf_sat_sensor_get,
189 .release = wf_sat_sensor_release,
190 .owner = THIS_MODULE,
193 static int wf_sat_probe(struct i2c_client *client)
195 struct device_node *dev = client->dev.of_node;
197 struct wf_sat_sensor *sens;
201 struct device_node *child;
202 int shift, cpu, index;
204 int vsens[2], isens[2];
206 sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
210 sat->node = of_node_get(dev);
211 kref_init(&sat->ref);
212 mutex_init(&sat->mutex);
214 INIT_LIST_HEAD(&sat->sensors);
215 i2c_set_clientdata(client, sat);
217 vsens[0] = vsens[1] = -1;
218 isens[0] = isens[1] = -1;
219 for_each_child_of_node(dev, child) {
220 reg = of_get_property(child, "reg", NULL);
221 loc = of_get_property(child, "location", NULL);
222 if (reg == NULL || loc == NULL)
225 /* the cooked sensors are between 0x30 and 0x37 */
226 if (*reg < 0x30 || *reg > 0x37)
230 /* expect location to be CPU [AB][01] ... */
231 if (strncmp(loc, "CPU ", 4) != 0)
235 if (chip > 1 || core > 1) {
236 printk(KERN_ERR "wf_sat_create: don't understand "
237 "location %s for %pOF\n", loc, child);
240 cpu = 2 * chip + core;
243 else if (sat->nr != chip) {
244 printk(KERN_ERR "wf_sat_create: can't cope with "
245 "multiple CPU chips on one SAT (%s)\n", loc);
249 if (of_node_is_type(child, "voltage-sensor")) {
250 name = "cpu-voltage";
253 } else if (of_node_is_type(child, "current-sensor")) {
254 name = "cpu-current";
257 } else if (of_node_is_type(child, "temp-sensor")) {
261 continue; /* hmmm shouldn't happen */
263 /* the +16 is enough for "cpu-voltage-n" */
264 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
266 printk(KERN_ERR "wf_sat_create: couldn't create "
267 "%s sensor %d (no memory)\n", name, cpu);
274 sens->sens.ops = &wf_sat_ops;
275 sens->sens.name = (char *) (sens + 1);
276 snprintf((char *)sens->sens.name, 16, "%s-%d", name, cpu);
278 if (wf_register_sensor(&sens->sens))
281 list_add(&sens->link, &sat->sensors);
286 /* make the power sensors */
287 for (core = 0; core < 2; ++core) {
288 if (vsens[core] < 0 || isens[core] < 0)
290 cpu = 2 * sat->nr + core;
291 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
293 printk(KERN_ERR "wf_sat_create: couldn't create power "
294 "sensor %d (no memory)\n", cpu);
297 sens->index = vsens[core];
298 sens->index2 = isens[core];
301 sens->sens.ops = &wf_sat_ops;
302 sens->sens.name = (char *) (sens + 1);
303 snprintf((char *)sens->sens.name, 16, "cpu-power-%d", cpu);
305 if (wf_register_sensor(&sens->sens))
308 list_add(&sens->link, &sat->sensors);
319 static void wf_sat_remove(struct i2c_client *client)
321 struct wf_sat *sat = i2c_get_clientdata(client);
322 struct wf_sat_sensor *sens;
324 /* release sensors */
325 while(!list_empty(&sat->sensors)) {
326 sens = list_first_entry(&sat->sensors,
327 struct wf_sat_sensor, link);
328 list_del(&sens->link);
329 wf_unregister_sensor(&sens->sens);
332 kref_put(&sat->ref, wf_sat_release);
335 static const struct i2c_device_id wf_sat_id[] = {
336 { "MAC,smu-sat", 0 },
339 MODULE_DEVICE_TABLE(i2c, wf_sat_id);
341 static const struct of_device_id wf_sat_of_id[] = {
342 { .compatible = "smu-sat", },
345 MODULE_DEVICE_TABLE(of, wf_sat_of_id);
347 static struct i2c_driver wf_sat_driver = {
349 .name = "wf_smu_sat",
350 .of_match_table = wf_sat_of_id,
352 .probe = wf_sat_probe,
353 .remove = wf_sat_remove,
354 .id_table = wf_sat_id,
357 module_i2c_driver(wf_sat_driver);
359 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
360 MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
361 MODULE_LICENSE("GPL");