1/*
2 * Windfarm PowerMac thermal control.  SMU "satellite" controller sensors.
3 *
4 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
5 *
6 * Released under the terms of the GNU GPL v2.
7 */
8
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 <asm/semaphore.h>
17#include <asm/prom.h>
18#include <asm/smu.h>
19#include <asm/pmac_low_i2c.h>
20
21#include "windfarm.h"
22
23#define VERSION "0.2"
24
25#define DEBUG
26
27#ifdef DEBUG
28#define DBG(args...)	printk(args)
29#else
30#define DBG(args...)	do { } while(0)
31#endif
32
33/* If the cache is older than 800ms we'll refetch it */
34#define MAX_AGE		msecs_to_jiffies(800)
35
36struct wf_sat {
37	int			nr;
38	atomic_t		refcnt;
39	struct semaphore	mutex;
40	unsigned long		last_read; /* jiffies when cache last updated */
41	u8			cache[16];
42	struct i2c_client	i2c;
43	struct device_node	*node;
44};
45
46static struct wf_sat *sats[2];
47
48struct wf_sat_sensor {
49	int		index;
50	int		index2;		/* used for power sensors */
51	int		shift;
52	struct wf_sat	*sat;
53	struct wf_sensor sens;
54};
55
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)
58
59static int wf_sat_attach(struct i2c_adapter *adapter);
60static int wf_sat_detach(struct i2c_client *client);
61
62static struct i2c_driver wf_sat_driver = {
63	.driver = {
64		.name		= "wf_smu_sat",
65	},
66	.attach_adapter	= wf_sat_attach,
67	.detach_client	= wf_sat_detach,
68};
69
70static int sat_read_block(struct i2c_client *client, u8 command,
71			  u8 *values, int len)
72{
73	union i2c_smbus_data data;
74	int err;
75
76	data.block[0] = len;
77	err = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
78			     I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA,
79			     &data);
80	if (!err)
81		memcpy(values, data.block, len);
82	return err;
83}
84
85struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
86						  unsigned int *size)
87{
88	struct wf_sat *sat;
89	int err;
90	unsigned int i, len;
91	u8 *buf;
92	u8 data[4];
93
94	/* TODO: Add the resulting partition to the device-tree */
95
96	if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
97		return NULL;
98
99	err = i2c_smbus_write_word_data(&sat->i2c, 8, id << 8);
100	if (err) {
101		printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
102		return NULL;
103	}
104
105	len = i2c_smbus_read_word_data(&sat->i2c, 9);
106	if (len < 0) {
107		printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
108		return NULL;
109	}
110	if (len == 0) {
111		printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
112		return NULL;
113	}
114
115	len = le16_to_cpu(len);
116	len = (len + 3) & ~3;
117	buf = kmalloc(len, GFP_KERNEL);
118	if (buf == NULL)
119		return NULL;
120
121	for (i = 0; i < len; i += 4) {
122		err = sat_read_block(&sat->i2c, 0xa, data, 4);
123		if (err) {
124			printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
125			       err);
126			goto fail;
127		}
128		buf[i] = data[1];
129		buf[i+1] = data[0];
130		buf[i+2] = data[3];
131		buf[i+3] = data[2];
132	}
133#ifdef DEBUG
134	DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id);
135	for (i = 0; i < len; ++i)
136		DBG(" %x", buf[i]);
137	DBG("\n");
138#endif
139
140	if (size)
141		*size = len;
142	return (struct smu_sdbp_header *) buf;
143
144 fail:
145	kfree(buf);
146	return NULL;
147}
148EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
149
150/* refresh the cache */
151static int wf_sat_read_cache(struct wf_sat *sat)
152{
153	int err;
154
155	err = sat_read_block(&sat->i2c, 0x3f, sat->cache, 16);
156	if (err)
157		return err;
158	sat->last_read = jiffies;
159#ifdef LOTSA_DEBUG
160	{
161		int i;
162		DBG(KERN_DEBUG "wf_sat_get: data is");
163		for (i = 0; i < 16; ++i)
164			DBG(" %.2x", sat->cache[i]);
165		DBG("\n");
166	}
167#endif
168	return 0;
169}
170
171static int wf_sat_get(struct wf_sensor *sr, s32 *value)
172{
173	struct wf_sat_sensor *sens = wf_to_sat(sr);
174	struct wf_sat *sat = sens->sat;
175	int i, err;
176	s32 val;
177
178	if (sat->i2c.adapter == NULL)
179		return -ENODEV;
180
181	down(&sat->mutex);
182	if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
183		err = wf_sat_read_cache(sat);
184		if (err)
185			goto fail;
186	}
187
188	i = sens->index * 2;
189	val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
190	if (sens->index2 >= 0) {
191		i = sens->index2 * 2;
192		/* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
193		val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
194	}
195
196	*value = val;
197	err = 0;
198
199 fail:
200	up(&sat->mutex);
201	return err;
202}
203
204static void wf_sat_release(struct wf_sensor *sr)
205{
206	struct wf_sat_sensor *sens = wf_to_sat(sr);
207	struct wf_sat *sat = sens->sat;
208
209	if (atomic_dec_and_test(&sat->refcnt)) {
210		if (sat->i2c.adapter) {
211			i2c_detach_client(&sat->i2c);
212			sat->i2c.adapter = NULL;
213		}
214		if (sat->nr >= 0)
215			sats[sat->nr] = NULL;
216		kfree(sat);
217	}
218	kfree(sens);
219}
220
221static struct wf_sensor_ops wf_sat_ops = {
222	.get_value	= wf_sat_get,
223	.release	= wf_sat_release,
224	.owner		= THIS_MODULE,
225};
226
227static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
228{
229	struct wf_sat *sat;
230	struct wf_sat_sensor *sens;
231	const u32 *reg;
232	const char *loc, *type;
233	u8 addr, chip, core;
234	struct device_node *child;
235	int shift, cpu, index;
236	char *name;
237	int vsens[2], isens[2];
238
239	reg = of_get_property(dev, "reg", NULL);
240	if (reg == NULL)
241		return;
242	addr = *reg;
243	DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);
244
245	sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
246	if (sat == NULL)
247		return;
248	sat->nr = -1;
249	sat->node = of_node_get(dev);
250	atomic_set(&sat->refcnt, 0);
251	init_MUTEX(&sat->mutex);
252	sat->i2c.addr = (addr >> 1) & 0x7f;
253	sat->i2c.adapter = adapter;
254	sat->i2c.driver = &wf_sat_driver;
255	strncpy(sat->i2c.name, "smu-sat", I2C_NAME_SIZE-1);
256
257	if (i2c_attach_client(&sat->i2c)) {
258		printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");
259		goto fail;
260	}
261
262	vsens[0] = vsens[1] = -1;
263	isens[0] = isens[1] = -1;
264	child = NULL;
265	while ((child = of_get_next_child(dev, child)) != NULL) {
266		reg = of_get_property(child, "reg", NULL);
267		type = of_get_property(child, "device_type", NULL);
268		loc = of_get_property(child, "location", NULL);
269		if (reg == NULL || loc == NULL)
270			continue;
271
272		/* the cooked sensors are between 0x30 and 0x37 */
273		if (*reg < 0x30 || *reg > 0x37)
274			continue;
275		index = *reg - 0x30;
276
277		/* expect location to be CPU [AB][01] ... */
278		if (strncmp(loc, "CPU ", 4) != 0)
279			continue;
280		chip = loc[4] - 'A';
281		core = loc[5] - '0';
282		if (chip > 1 || core > 1) {
283			printk(KERN_ERR "wf_sat_create: don't understand "
284			       "location %s for %s\n", loc, child->full_name);
285			continue;
286		}
287		cpu = 2 * chip + core;
288		if (sat->nr < 0)
289			sat->nr = chip;
290		else if (sat->nr != chip) {
291			printk(KERN_ERR "wf_sat_create: can't cope with "
292			       "multiple CPU chips on one SAT (%s)\n", loc);
293			continue;
294		}
295
296		if (strcmp(type, "voltage-sensor") == 0) {
297			name = "cpu-voltage";
298			shift = 4;
299			vsens[core] = index;
300		} else if (strcmp(type, "current-sensor") == 0) {
301			name = "cpu-current";
302			shift = 8;
303			isens[core] = index;
304		} else if (strcmp(type, "temp-sensor") == 0) {
305			name = "cpu-temp";
306			shift = 10;
307		} else
308			continue;	/* hmmm shouldn't happen */
309
310		/* the +16 is enough for "cpu-voltage-n" */
311		sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
312		if (sens == NULL) {
313			printk(KERN_ERR "wf_sat_create: couldn't create "
314			       "%s sensor %d (no memory)\n", name, cpu);
315			continue;
316		}
317		sens->index = index;
318		sens->index2 = -1;
319		sens->shift = shift;
320		sens->sat = sat;
321		atomic_inc(&sat->refcnt);
322		sens->sens.ops = &wf_sat_ops;
323		sens->sens.name = (char *) (sens + 1);
324		snprintf(sens->sens.name, 16, "%s-%d", name, cpu);
325
326		if (wf_register_sensor(&sens->sens)) {
327			atomic_dec(&sat->refcnt);
328			kfree(sens);
329		}
330	}
331
332	/* make the power sensors */
333	for (core = 0; core < 2; ++core) {
334		if (vsens[core] < 0 || isens[core] < 0)
335			continue;
336		cpu = 2 * sat->nr + core;
337		sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
338		if (sens == NULL) {
339			printk(KERN_ERR "wf_sat_create: couldn't create power "
340			       "sensor %d (no memory)\n", cpu);
341			continue;
342		}
343		sens->index = vsens[core];
344		sens->index2 = isens[core];
345		sens->shift = 0;
346		sens->sat = sat;
347		atomic_inc(&sat->refcnt);
348		sens->sens.ops = &wf_sat_ops;
349		sens->sens.name = (char *) (sens + 1);
350		snprintf(sens->sens.name, 16, "cpu-power-%d", cpu);
351
352		if (wf_register_sensor(&sens->sens)) {
353			atomic_dec(&sat->refcnt);
354			kfree(sens);
355		}
356	}
357
358	if (sat->nr >= 0)
359		sats[sat->nr] = sat;
360
361	return;
362
363 fail:
364	kfree(sat);
365}
366
367static int wf_sat_attach(struct i2c_adapter *adapter)
368{
369	struct device_node *busnode, *dev = NULL;
370	struct pmac_i2c_bus *bus;
371
372	bus = pmac_i2c_adapter_to_bus(adapter);
373	if (bus == NULL)
374		return -ENODEV;
375	busnode = pmac_i2c_get_bus_node(bus);
376
377	while ((dev = of_get_next_child(busnode, dev)) != NULL)
378		if (of_device_is_compatible(dev, "smu-sat"))
379			wf_sat_create(adapter, dev);
380	return 0;
381}
382
383static int wf_sat_detach(struct i2c_client *client)
384{
385	struct wf_sat *sat = i2c_to_sat(client);
386
387
388	sat->i2c.adapter = NULL;
389	return 0;
390}
391
392static int __init sat_sensors_init(void)
393{
394	return i2c_add_driver(&wf_sat_driver);
395}
396
397static void __exit sat_sensors_exit(void)
398{
399	i2c_del_driver(&wf_sat_driver);
400}
401
402module_init(sat_sensors_init);
403/*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */
404
405MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
406MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
407MODULE_LICENSE("GPL");
408