xref: /linux/drivers/macintosh/windfarm_smu_sat.c (revision 9f2c9170934eace462499ba0bfe042cc72900173)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Windfarm PowerMac thermal control.  SMU "satellite" controller sensors.
4  *
5  * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
6  */
7 
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>
16 
17 #include <asm/smu.h>
18 #include <asm/pmac_low_i2c.h>
19 
20 #include "windfarm.h"
21 
22 #define VERSION "1.0"
23 
24 /* If the cache is older than 800ms we'll refetch it */
25 #define MAX_AGE		msecs_to_jiffies(800)
26 
27 struct wf_sat {
28 	struct kref		ref;
29 	int			nr;
30 	struct mutex		mutex;
31 	unsigned long		last_read; /* jiffies when cache last updated */
32 	u8			cache[16];
33 	struct list_head	sensors;
34 	struct i2c_client	*i2c;
35 	struct device_node	*node;
36 };
37 
38 static struct wf_sat *sats[2];
39 
40 struct wf_sat_sensor {
41 	struct list_head	link;
42 	int			index;
43 	int			index2;		/* used for power sensors */
44 	int			shift;
45 	struct wf_sat		*sat;
46 	struct wf_sensor 	sens;
47 };
48 
49 #define wf_to_sat(c)	container_of(c, struct wf_sat_sensor, sens)
50 
51 struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
52 						  unsigned int *size)
53 {
54 	struct wf_sat *sat;
55 	int err;
56 	unsigned int i, len;
57 	u8 *buf;
58 	u8 data[4];
59 
60 	/* TODO: Add the resulting partition to the device-tree */
61 
62 	if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
63 		return NULL;
64 
65 	err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
66 	if (err) {
67 		printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
68 		return NULL;
69 	}
70 
71 	err = i2c_smbus_read_word_data(sat->i2c, 9);
72 	if (err < 0) {
73 		printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
74 		return NULL;
75 	}
76 	len = err;
77 	if (len == 0) {
78 		printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
79 		return NULL;
80 	}
81 
82 	len = le16_to_cpu(len);
83 	len = (len + 3) & ~3;
84 	buf = kmalloc(len, GFP_KERNEL);
85 	if (buf == NULL)
86 		return NULL;
87 
88 	for (i = 0; i < len; i += 4) {
89 		err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
90 		if (err < 0) {
91 			printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
92 			       err);
93 			goto fail;
94 		}
95 		buf[i] = data[1];
96 		buf[i+1] = data[0];
97 		buf[i+2] = data[3];
98 		buf[i+3] = data[2];
99 	}
100 
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);
104 	if (size)
105 		*size = len;
106 	return (struct smu_sdbp_header *) buf;
107 
108  fail:
109 	kfree(buf);
110 	return NULL;
111 }
112 EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
113 
114 /* refresh the cache */
115 static int wf_sat_read_cache(struct wf_sat *sat)
116 {
117 	int err;
118 
119 	err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
120 	if (err < 0)
121 		return err;
122 	sat->last_read = jiffies;
123 
124 #ifdef LOTSA_DEBUG
125 	{
126 		int i;
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);
130 	}
131 #endif
132 	return 0;
133 }
134 
135 static int wf_sat_sensor_get(struct wf_sensor *sr, s32 *value)
136 {
137 	struct wf_sat_sensor *sens = wf_to_sat(sr);
138 	struct wf_sat *sat = sens->sat;
139 	int i, err;
140 	s32 val;
141 
142 	if (sat->i2c == NULL)
143 		return -ENODEV;
144 
145 	mutex_lock(&sat->mutex);
146 	if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
147 		err = wf_sat_read_cache(sat);
148 		if (err)
149 			goto fail;
150 	}
151 
152 	i = sens->index * 2;
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;
158 	}
159 
160 	*value = val;
161 	err = 0;
162 
163  fail:
164 	mutex_unlock(&sat->mutex);
165 	return err;
166 }
167 
168 static void wf_sat_release(struct kref *ref)
169 {
170 	struct wf_sat *sat = container_of(ref, struct wf_sat, ref);
171 
172 	if (sat->nr >= 0)
173 		sats[sat->nr] = NULL;
174 	kfree(sat);
175 }
176 
177 static void wf_sat_sensor_release(struct wf_sensor *sr)
178 {
179 	struct wf_sat_sensor *sens = wf_to_sat(sr);
180 	struct wf_sat *sat = sens->sat;
181 
182 	kfree(sens);
183 	kref_put(&sat->ref, wf_sat_release);
184 }
185 
186 static const struct wf_sensor_ops wf_sat_ops = {
187 	.get_value	= wf_sat_sensor_get,
188 	.release	= wf_sat_sensor_release,
189 	.owner		= THIS_MODULE,
190 };
191 
192 static int wf_sat_probe(struct i2c_client *client)
193 {
194 	struct device_node *dev = client->dev.of_node;
195 	struct wf_sat *sat;
196 	struct wf_sat_sensor *sens;
197 	const u32 *reg;
198 	const char *loc;
199 	u8 chip, core;
200 	struct device_node *child;
201 	int shift, cpu, index;
202 	char *name;
203 	int vsens[2], isens[2];
204 
205 	sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
206 	if (sat == NULL)
207 		return -ENOMEM;
208 	sat->nr = -1;
209 	sat->node = of_node_get(dev);
210 	kref_init(&sat->ref);
211 	mutex_init(&sat->mutex);
212 	sat->i2c = client;
213 	INIT_LIST_HEAD(&sat->sensors);
214 	i2c_set_clientdata(client, sat);
215 
216 	vsens[0] = vsens[1] = -1;
217 	isens[0] = isens[1] = -1;
218 	for_each_child_of_node(dev, child) {
219 		reg = of_get_property(child, "reg", NULL);
220 		loc = of_get_property(child, "location", NULL);
221 		if (reg == NULL || loc == NULL)
222 			continue;
223 
224 		/* the cooked sensors are between 0x30 and 0x37 */
225 		if (*reg < 0x30 || *reg > 0x37)
226 			continue;
227 		index = *reg - 0x30;
228 
229 		/* expect location to be CPU [AB][01] ... */
230 		if (strncmp(loc, "CPU ", 4) != 0)
231 			continue;
232 		chip = loc[4] - 'A';
233 		core = loc[5] - '0';
234 		if (chip > 1 || core > 1) {
235 			printk(KERN_ERR "wf_sat_create: don't understand "
236 			       "location %s for %pOF\n", loc, child);
237 			continue;
238 		}
239 		cpu = 2 * chip + core;
240 		if (sat->nr < 0)
241 			sat->nr = chip;
242 		else if (sat->nr != chip) {
243 			printk(KERN_ERR "wf_sat_create: can't cope with "
244 			       "multiple CPU chips on one SAT (%s)\n", loc);
245 			continue;
246 		}
247 
248 		if (of_node_is_type(child, "voltage-sensor")) {
249 			name = "cpu-voltage";
250 			shift = 4;
251 			vsens[core] = index;
252 		} else if (of_node_is_type(child, "current-sensor")) {
253 			name = "cpu-current";
254 			shift = 8;
255 			isens[core] = index;
256 		} else if (of_node_is_type(child, "temp-sensor")) {
257 			name = "cpu-temp";
258 			shift = 10;
259 		} else
260 			continue;	/* hmmm shouldn't happen */
261 
262 		/* the +16 is enough for "cpu-voltage-n" */
263 		sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
264 		if (sens == NULL) {
265 			printk(KERN_ERR "wf_sat_create: couldn't create "
266 			       "%s sensor %d (no memory)\n", name, cpu);
267 			continue;
268 		}
269 		sens->index = index;
270 		sens->index2 = -1;
271 		sens->shift = shift;
272 		sens->sat = sat;
273 		sens->sens.ops = &wf_sat_ops;
274 		sens->sens.name = (char *) (sens + 1);
275 		snprintf((char *)sens->sens.name, 16, "%s-%d", name, cpu);
276 
277 		if (wf_register_sensor(&sens->sens))
278 			kfree(sens);
279 		else {
280 			list_add(&sens->link, &sat->sensors);
281 			kref_get(&sat->ref);
282 		}
283 	}
284 
285 	/* make the power sensors */
286 	for (core = 0; core < 2; ++core) {
287 		if (vsens[core] < 0 || isens[core] < 0)
288 			continue;
289 		cpu = 2 * sat->nr + core;
290 		sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
291 		if (sens == NULL) {
292 			printk(KERN_ERR "wf_sat_create: couldn't create power "
293 			       "sensor %d (no memory)\n", cpu);
294 			continue;
295 		}
296 		sens->index = vsens[core];
297 		sens->index2 = isens[core];
298 		sens->shift = 0;
299 		sens->sat = sat;
300 		sens->sens.ops = &wf_sat_ops;
301 		sens->sens.name = (char *) (sens + 1);
302 		snprintf((char *)sens->sens.name, 16, "cpu-power-%d", cpu);
303 
304 		if (wf_register_sensor(&sens->sens))
305 			kfree(sens);
306 		else {
307 			list_add(&sens->link, &sat->sensors);
308 			kref_get(&sat->ref);
309 		}
310 	}
311 
312 	if (sat->nr >= 0)
313 		sats[sat->nr] = sat;
314 
315 	return 0;
316 }
317 
318 static void wf_sat_remove(struct i2c_client *client)
319 {
320 	struct wf_sat *sat = i2c_get_clientdata(client);
321 	struct wf_sat_sensor *sens;
322 
323 	/* release sensors */
324 	while(!list_empty(&sat->sensors)) {
325 		sens = list_first_entry(&sat->sensors,
326 					struct wf_sat_sensor, link);
327 		list_del(&sens->link);
328 		wf_unregister_sensor(&sens->sens);
329 	}
330 	sat->i2c = NULL;
331 	kref_put(&sat->ref, wf_sat_release);
332 }
333 
334 static const struct i2c_device_id wf_sat_id[] = {
335 	{ "MAC,smu-sat", 0 },
336 	{ }
337 };
338 MODULE_DEVICE_TABLE(i2c, wf_sat_id);
339 
340 static const struct of_device_id wf_sat_of_id[] = {
341 	{ .compatible = "smu-sat", },
342 	{ }
343 };
344 MODULE_DEVICE_TABLE(of, wf_sat_of_id);
345 
346 static struct i2c_driver wf_sat_driver = {
347 	.driver = {
348 		.name		= "wf_smu_sat",
349 		.of_match_table = wf_sat_of_id,
350 	},
351 	.probe_new	= wf_sat_probe,
352 	.remove		= wf_sat_remove,
353 	.id_table	= wf_sat_id,
354 };
355 
356 module_i2c_driver(wf_sat_driver);
357 
358 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
359 MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
360 MODULE_LICENSE("GPL");
361