Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Windfarm PowerMac thermal control. SMU based sensors
  3 *
  4 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  5 *                    <benh@kernel.crashing.org>
  6 *
  7 * Released under the term of the GNU GPL v2.
  8 */
  9
 10#include <linux/types.h>
 11#include <linux/errno.h>
 12#include <linux/kernel.h>
 13#include <linux/delay.h>
 14#include <linux/slab.h>
 15#include <linux/init.h>
 16#include <linux/wait.h>
 17#include <linux/completion.h>
 18#include <asm/prom.h>
 19#include <asm/machdep.h>
 20#include <asm/io.h>
 21#include <asm/system.h>
 22#include <asm/sections.h>
 23#include <asm/smu.h>
 24
 25#include "windfarm.h"
 26
 27#define VERSION "0.2"
 28
 29#undef DEBUG
 30
 31#ifdef DEBUG
 32#define DBG(args...)	printk(args)
 33#else
 34#define DBG(args...)	do { } while(0)
 35#endif
 36
 37/*
 38 * Various SMU "partitions" calibration objects for which we
 39 * keep pointers here for use by bits & pieces of the driver
 40 */
 41static struct smu_sdbp_cpuvcp *cpuvcp;
 42static int  cpuvcp_version;
 43static struct smu_sdbp_cpudiode *cpudiode;
 44static struct smu_sdbp_slotspow *slotspow;
 45static u8 *debugswitches;
 46
 47/*
 48 * SMU basic sensors objects
 49 */
 50
 51static LIST_HEAD(smu_ads);
 52
 53struct smu_ad_sensor {
 54	struct list_head	link;
 55	u32			reg;		/* index in SMU */
 56	struct wf_sensor	sens;
 57};
 58#define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
 59
 60static void smu_ads_release(struct wf_sensor *sr)
 61{
 62	struct smu_ad_sensor *ads = to_smu_ads(sr);
 63
 64	kfree(ads);
 65}
 66
 67static int smu_read_adc(u8 id, s32 *value)
 68{
 69	struct smu_simple_cmd	cmd;
 70	DECLARE_COMPLETION_ONSTACK(comp);
 71	int rc;
 72
 73	rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1,
 74			      smu_done_complete, &comp, id);
 75	if (rc)
 76		return rc;
 77	wait_for_completion(&comp);
 78	if (cmd.cmd.status != 0)
 79		return cmd.cmd.status;
 80	if (cmd.cmd.reply_len != 2) {
 81		printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n",
 82		       id, cmd.cmd.reply_len);
 83		return -EIO;
 84	}
 85	*value = *((u16 *)cmd.buffer);
 86	return 0;
 87}
 88
 89static int smu_cputemp_get(struct wf_sensor *sr, s32 *value)
 90{
 91	struct smu_ad_sensor *ads = to_smu_ads(sr);
 92	int rc;
 93	s32 val;
 94	s64 scaled;
 95
 96	rc = smu_read_adc(ads->reg, &val);
 97	if (rc) {
 98		printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n",
 99		       rc);
100		return rc;
101	}
102
103	/* Ok, we have to scale & adjust, taking units into account */
104	scaled = (s64)(((u64)val) * (u64)cpudiode->m_value);
105	scaled >>= 3;
106	scaled += ((s64)cpudiode->b_value) << 9;
107	*value = (s32)(scaled << 1);
108
109	return 0;
110}
111
112static int smu_cpuamp_get(struct wf_sensor *sr, s32 *value)
113{
114	struct smu_ad_sensor *ads = to_smu_ads(sr);
115	s32 val, scaled;
116	int rc;
117
118	rc = smu_read_adc(ads->reg, &val);
119	if (rc) {
120		printk(KERN_ERR "windfarm: read CPU current failed, err %d\n",
121		       rc);
122		return rc;
123	}
124
125	/* Ok, we have to scale & adjust, taking units into account */
126	scaled = (s32)(val * (u32)cpuvcp->curr_scale);
127	scaled += (s32)cpuvcp->curr_offset;
128	*value = scaled << 4;
129
130	return 0;
131}
132
133static int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value)
134{
135	struct smu_ad_sensor *ads = to_smu_ads(sr);
136	s32 val, scaled;
137	int rc;
138
139	rc = smu_read_adc(ads->reg, &val);
140	if (rc) {
141		printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n",
142		       rc);
143		return rc;
144	}
145
146	/* Ok, we have to scale & adjust, taking units into account */
147	scaled = (s32)(val * (u32)cpuvcp->volt_scale);
148	scaled += (s32)cpuvcp->volt_offset;
149	*value = scaled << 4;
150
151	return 0;
152}
153
154static int smu_slotspow_get(struct wf_sensor *sr, s32 *value)
155{
156	struct smu_ad_sensor *ads = to_smu_ads(sr);
157	s32 val, scaled;
158	int rc;
159
160	rc = smu_read_adc(ads->reg, &val);
161	if (rc) {
162		printk(KERN_ERR "windfarm: read slots power failed, err %d\n",
163		       rc);
164		return rc;
165	}
166
167	/* Ok, we have to scale & adjust, taking units into account */
168	scaled = (s32)(val * (u32)slotspow->pow_scale);
169	scaled += (s32)slotspow->pow_offset;
170	*value = scaled << 4;
171
172	return 0;
173}
174
175
176static struct wf_sensor_ops smu_cputemp_ops = {
177	.get_value	= smu_cputemp_get,
178	.release	= smu_ads_release,
179	.owner		= THIS_MODULE,
180};
181static struct wf_sensor_ops smu_cpuamp_ops = {
182	.get_value	= smu_cpuamp_get,
183	.release	= smu_ads_release,
184	.owner		= THIS_MODULE,
185};
186static struct wf_sensor_ops smu_cpuvolt_ops = {
187	.get_value	= smu_cpuvolt_get,
188	.release	= smu_ads_release,
189	.owner		= THIS_MODULE,
190};
191static struct wf_sensor_ops smu_slotspow_ops = {
192	.get_value	= smu_slotspow_get,
193	.release	= smu_ads_release,
194	.owner		= THIS_MODULE,
195};
196
197
198static struct smu_ad_sensor *smu_ads_create(struct device_node *node)
199{
200	struct smu_ad_sensor *ads;
201	const char *c, *l;
202	const u32 *v;
203
204	ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL);
205	if (ads == NULL)
206		return NULL;
207	c = of_get_property(node, "device_type", NULL);
208	l = of_get_property(node, "location", NULL);
209	if (c == NULL || l == NULL)
210		goto fail;
211
212	/* We currently pick the sensors based on the OF name and location
213	 * properties, while Darwin uses the sensor-id's.
214	 * The problem with the IDs is that they are model specific while it
215	 * looks like apple has been doing a reasonably good job at keeping
216	 * the names and locations consistents so I'll stick with the names
217	 * and locations for now.
218	 */
219	if (!strcmp(c, "temp-sensor") &&
220	    !strcmp(l, "CPU T-Diode")) {
221		ads->sens.ops = &smu_cputemp_ops;
222		ads->sens.name = "cpu-temp";
223		if (cpudiode == NULL) {
224			DBG("wf: cpudiode partition (%02x) not found\n",
225			    SMU_SDB_CPUDIODE_ID);
226			goto fail;
227		}
228	} else if (!strcmp(c, "current-sensor") &&
229		   !strcmp(l, "CPU Current")) {
230		ads->sens.ops = &smu_cpuamp_ops;
231		ads->sens.name = "cpu-current";
232		if (cpuvcp == NULL) {
233			DBG("wf: cpuvcp partition (%02x) not found\n",
234			    SMU_SDB_CPUVCP_ID);
235			goto fail;
236		}
237	} else if (!strcmp(c, "voltage-sensor") &&
238		   !strcmp(l, "CPU Voltage")) {
239		ads->sens.ops = &smu_cpuvolt_ops;
240		ads->sens.name = "cpu-voltage";
241		if (cpuvcp == NULL) {
242			DBG("wf: cpuvcp partition (%02x) not found\n",
243			    SMU_SDB_CPUVCP_ID);
244			goto fail;
245		}
246	} else if (!strcmp(c, "power-sensor") &&
247		   !strcmp(l, "Slots Power")) {
248		ads->sens.ops = &smu_slotspow_ops;
249		ads->sens.name = "slots-power";
250		if (slotspow == NULL) {
251			DBG("wf: slotspow partition (%02x) not found\n",
252			    SMU_SDB_SLOTSPOW_ID);
253			goto fail;
254		}
255	} else
256		goto fail;
257
258	v = of_get_property(node, "reg", NULL);
259	if (v == NULL)
260		goto fail;
261	ads->reg = *v;
262
263	if (wf_register_sensor(&ads->sens))
264		goto fail;
265	return ads;
266 fail:
267	kfree(ads);
268	return NULL;
269}
270
271/*
272 * SMU Power combo sensor object
273 */
274
275struct smu_cpu_power_sensor {
276	struct list_head	link;
277	struct wf_sensor	*volts;
278	struct wf_sensor	*amps;
279	int			fake_volts : 1;
280	int			quadratic : 1;
281	struct wf_sensor	sens;
282};
283#define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens)
284
285static struct smu_cpu_power_sensor *smu_cpu_power;
286
287static void smu_cpu_power_release(struct wf_sensor *sr)
288{
289	struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
290
291	if (pow->volts)
292		wf_put_sensor(pow->volts);
293	if (pow->amps)
294		wf_put_sensor(pow->amps);
295	kfree(pow);
296}
297
298static int smu_cpu_power_get(struct wf_sensor *sr, s32 *value)
299{
300	struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
301	s32 volts, amps, power;
302	u64 tmps, tmpa, tmpb;
303	int rc;
304
305	rc = pow->amps->ops->get_value(pow->amps, &amps);
306	if (rc)
307		return rc;
308
309	if (pow->fake_volts) {
310		*value = amps * 12 - 0x30000;
311		return 0;
312	}
313
314	rc = pow->volts->ops->get_value(pow->volts, &volts);
315	if (rc)
316		return rc;
317
318	power = (s32)((((u64)volts) * ((u64)amps)) >> 16);
319	if (!pow->quadratic) {
320		*value = power;
321		return 0;
322	}
323	tmps = (((u64)power) * ((u64)power)) >> 16;
324	tmpa = ((u64)cpuvcp->power_quads[0]) * tmps;
325	tmpb = ((u64)cpuvcp->power_quads[1]) * ((u64)power);
326	*value = (tmpa >> 28) + (tmpb >> 28) + (cpuvcp->power_quads[2] >> 12);
327
328	return 0;
329}
330
331static struct wf_sensor_ops smu_cpu_power_ops = {
332	.get_value	= smu_cpu_power_get,
333	.release	= smu_cpu_power_release,
334	.owner		= THIS_MODULE,
335};
336
337
338static struct smu_cpu_power_sensor *
339smu_cpu_power_create(struct wf_sensor *volts, struct wf_sensor *amps)
340{
341	struct smu_cpu_power_sensor *pow;
342
343	pow = kmalloc(sizeof(struct smu_cpu_power_sensor), GFP_KERNEL);
344	if (pow == NULL)
345		return NULL;
346	pow->sens.ops = &smu_cpu_power_ops;
347	pow->sens.name = "cpu-power";
348
349	wf_get_sensor(volts);
350	pow->volts = volts;
351	wf_get_sensor(amps);
352	pow->amps = amps;
353
354	/* Some early machines need a faked voltage */
355	if (debugswitches && ((*debugswitches) & 0x80)) {
356		printk(KERN_INFO "windfarm: CPU Power sensor using faked"
357		       " voltage !\n");
358		pow->fake_volts = 1;
359	} else
360		pow->fake_volts = 0;
361
362	/* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now,
363	 * I yet have to figure out what's up with 8,2 and will have to
364	 * adjust for later, unless we can 100% trust the SDB partition...
365	 */
366	if ((of_machine_is_compatible("PowerMac8,1") ||
367	     of_machine_is_compatible("PowerMac8,2") ||
368	     of_machine_is_compatible("PowerMac9,1")) &&
369	    cpuvcp_version >= 2) {
370		pow->quadratic = 1;
371		DBG("windfarm: CPU Power using quadratic transform\n");
372	} else
373		pow->quadratic = 0;
374
375	if (wf_register_sensor(&pow->sens))
376		goto fail;
377	return pow;
378 fail:
379	kfree(pow);
380	return NULL;
381}
382
383static void smu_fetch_param_partitions(void)
384{
385	const struct smu_sdbp_header *hdr;
386
387	/* Get CPU voltage/current/power calibration data */
388	hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL);
389	if (hdr != NULL) {
390		cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1];
391		/* Keep version around */
392		cpuvcp_version = hdr->version;
393	}
394
395	/* Get CPU diode calibration data */
396	hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL);
397	if (hdr != NULL)
398		cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1];
399
400	/* Get slots power calibration data if any */
401	hdr = smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID, NULL);
402	if (hdr != NULL)
403		slotspow = (struct smu_sdbp_slotspow *)&hdr[1];
404
405	/* Get debug switches if any */
406	hdr = smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID, NULL);
407	if (hdr != NULL)
408		debugswitches = (u8 *)&hdr[1];
409}
410
411static int __init smu_sensors_init(void)
412{
413	struct device_node *smu, *sensors, *s;
414	struct smu_ad_sensor *volt_sensor = NULL, *curr_sensor = NULL;
415
416	if (!smu_present())
417		return -ENODEV;
418
419	/* Get parameters partitions */
420	smu_fetch_param_partitions();
421
422	smu = of_find_node_by_type(NULL, "smu");
423	if (smu == NULL)
424		return -ENODEV;
425
426	/* Look for sensors subdir */
427	for (sensors = NULL;
428	     (sensors = of_get_next_child(smu, sensors)) != NULL;)
429		if (!strcmp(sensors->name, "sensors"))
430			break;
431
432	of_node_put(smu);
433
434	/* Create basic sensors */
435	for (s = NULL;
436	     sensors && (s = of_get_next_child(sensors, s)) != NULL;) {
437		struct smu_ad_sensor *ads;
438
439		ads = smu_ads_create(s);
440		if (ads == NULL)
441			continue;
442		list_add(&ads->link, &smu_ads);
443		/* keep track of cpu voltage & current */
444		if (!strcmp(ads->sens.name, "cpu-voltage"))
445			volt_sensor = ads;
446		else if (!strcmp(ads->sens.name, "cpu-current"))
447			curr_sensor = ads;
448	}
449
450	of_node_put(sensors);
451
452	/* Create CPU power sensor if possible */
453	if (volt_sensor && curr_sensor)
454		smu_cpu_power = smu_cpu_power_create(&volt_sensor->sens,
455						     &curr_sensor->sens);
456
457	return 0;
458}
459
460static void __exit smu_sensors_exit(void)
461{
462	struct smu_ad_sensor *ads;
463
464	/* dispose of power sensor */
465	if (smu_cpu_power)
466		wf_unregister_sensor(&smu_cpu_power->sens);
467
468	/* dispose of basic sensors */
469	while (!list_empty(&smu_ads)) {
470		ads = list_entry(smu_ads.next, struct smu_ad_sensor, link);
471		list_del(&ads->link);
472		wf_unregister_sensor(&ads->sens);
473	}
474}
475
476
477module_init(smu_sensors_init);
478module_exit(smu_sensors_exit);
479
480MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
481MODULE_DESCRIPTION("SMU sensor objects for PowerMacs thermal control");
482MODULE_LICENSE("GPL");
483
v3.15
  1/*
  2 * Windfarm PowerMac thermal control. SMU based sensors
  3 *
  4 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  5 *                    <benh@kernel.crashing.org>
  6 *
  7 * Released under the term of the GNU GPL v2.
  8 */
  9
 10#include <linux/types.h>
 11#include <linux/errno.h>
 12#include <linux/kernel.h>
 13#include <linux/delay.h>
 14#include <linux/slab.h>
 15#include <linux/init.h>
 16#include <linux/wait.h>
 17#include <linux/completion.h>
 18#include <asm/prom.h>
 19#include <asm/machdep.h>
 20#include <asm/io.h>
 
 21#include <asm/sections.h>
 22#include <asm/smu.h>
 23
 24#include "windfarm.h"
 25
 26#define VERSION "0.2"
 27
 28#undef DEBUG
 29
 30#ifdef DEBUG
 31#define DBG(args...)	printk(args)
 32#else
 33#define DBG(args...)	do { } while(0)
 34#endif
 35
 36/*
 37 * Various SMU "partitions" calibration objects for which we
 38 * keep pointers here for use by bits & pieces of the driver
 39 */
 40static struct smu_sdbp_cpuvcp *cpuvcp;
 41static int  cpuvcp_version;
 42static struct smu_sdbp_cpudiode *cpudiode;
 43static struct smu_sdbp_slotspow *slotspow;
 44static u8 *debugswitches;
 45
 46/*
 47 * SMU basic sensors objects
 48 */
 49
 50static LIST_HEAD(smu_ads);
 51
 52struct smu_ad_sensor {
 53	struct list_head	link;
 54	u32			reg;		/* index in SMU */
 55	struct wf_sensor	sens;
 56};
 57#define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
 58
 59static void smu_ads_release(struct wf_sensor *sr)
 60{
 61	struct smu_ad_sensor *ads = to_smu_ads(sr);
 62
 63	kfree(ads);
 64}
 65
 66static int smu_read_adc(u8 id, s32 *value)
 67{
 68	struct smu_simple_cmd	cmd;
 69	DECLARE_COMPLETION_ONSTACK(comp);
 70	int rc;
 71
 72	rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1,
 73			      smu_done_complete, &comp, id);
 74	if (rc)
 75		return rc;
 76	wait_for_completion(&comp);
 77	if (cmd.cmd.status != 0)
 78		return cmd.cmd.status;
 79	if (cmd.cmd.reply_len != 2) {
 80		printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n",
 81		       id, cmd.cmd.reply_len);
 82		return -EIO;
 83	}
 84	*value = *((u16 *)cmd.buffer);
 85	return 0;
 86}
 87
 88static int smu_cputemp_get(struct wf_sensor *sr, s32 *value)
 89{
 90	struct smu_ad_sensor *ads = to_smu_ads(sr);
 91	int rc;
 92	s32 val;
 93	s64 scaled;
 94
 95	rc = smu_read_adc(ads->reg, &val);
 96	if (rc) {
 97		printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n",
 98		       rc);
 99		return rc;
100	}
101
102	/* Ok, we have to scale & adjust, taking units into account */
103	scaled = (s64)(((u64)val) * (u64)cpudiode->m_value);
104	scaled >>= 3;
105	scaled += ((s64)cpudiode->b_value) << 9;
106	*value = (s32)(scaled << 1);
107
108	return 0;
109}
110
111static int smu_cpuamp_get(struct wf_sensor *sr, s32 *value)
112{
113	struct smu_ad_sensor *ads = to_smu_ads(sr);
114	s32 val, scaled;
115	int rc;
116
117	rc = smu_read_adc(ads->reg, &val);
118	if (rc) {
119		printk(KERN_ERR "windfarm: read CPU current failed, err %d\n",
120		       rc);
121		return rc;
122	}
123
124	/* Ok, we have to scale & adjust, taking units into account */
125	scaled = (s32)(val * (u32)cpuvcp->curr_scale);
126	scaled += (s32)cpuvcp->curr_offset;
127	*value = scaled << 4;
128
129	return 0;
130}
131
132static int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value)
133{
134	struct smu_ad_sensor *ads = to_smu_ads(sr);
135	s32 val, scaled;
136	int rc;
137
138	rc = smu_read_adc(ads->reg, &val);
139	if (rc) {
140		printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n",
141		       rc);
142		return rc;
143	}
144
145	/* Ok, we have to scale & adjust, taking units into account */
146	scaled = (s32)(val * (u32)cpuvcp->volt_scale);
147	scaled += (s32)cpuvcp->volt_offset;
148	*value = scaled << 4;
149
150	return 0;
151}
152
153static int smu_slotspow_get(struct wf_sensor *sr, s32 *value)
154{
155	struct smu_ad_sensor *ads = to_smu_ads(sr);
156	s32 val, scaled;
157	int rc;
158
159	rc = smu_read_adc(ads->reg, &val);
160	if (rc) {
161		printk(KERN_ERR "windfarm: read slots power failed, err %d\n",
162		       rc);
163		return rc;
164	}
165
166	/* Ok, we have to scale & adjust, taking units into account */
167	scaled = (s32)(val * (u32)slotspow->pow_scale);
168	scaled += (s32)slotspow->pow_offset;
169	*value = scaled << 4;
170
171	return 0;
172}
173
174
175static struct wf_sensor_ops smu_cputemp_ops = {
176	.get_value	= smu_cputemp_get,
177	.release	= smu_ads_release,
178	.owner		= THIS_MODULE,
179};
180static struct wf_sensor_ops smu_cpuamp_ops = {
181	.get_value	= smu_cpuamp_get,
182	.release	= smu_ads_release,
183	.owner		= THIS_MODULE,
184};
185static struct wf_sensor_ops smu_cpuvolt_ops = {
186	.get_value	= smu_cpuvolt_get,
187	.release	= smu_ads_release,
188	.owner		= THIS_MODULE,
189};
190static struct wf_sensor_ops smu_slotspow_ops = {
191	.get_value	= smu_slotspow_get,
192	.release	= smu_ads_release,
193	.owner		= THIS_MODULE,
194};
195
196
197static struct smu_ad_sensor *smu_ads_create(struct device_node *node)
198{
199	struct smu_ad_sensor *ads;
200	const char *c, *l;
201	const u32 *v;
202
203	ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL);
204	if (ads == NULL)
205		return NULL;
206	c = of_get_property(node, "device_type", NULL);
207	l = of_get_property(node, "location", NULL);
208	if (c == NULL || l == NULL)
209		goto fail;
210
211	/* We currently pick the sensors based on the OF name and location
212	 * properties, while Darwin uses the sensor-id's.
213	 * The problem with the IDs is that they are model specific while it
214	 * looks like apple has been doing a reasonably good job at keeping
215	 * the names and locations consistents so I'll stick with the names
216	 * and locations for now.
217	 */
218	if (!strcmp(c, "temp-sensor") &&
219	    !strcmp(l, "CPU T-Diode")) {
220		ads->sens.ops = &smu_cputemp_ops;
221		ads->sens.name = "cpu-temp";
222		if (cpudiode == NULL) {
223			DBG("wf: cpudiode partition (%02x) not found\n",
224			    SMU_SDB_CPUDIODE_ID);
225			goto fail;
226		}
227	} else if (!strcmp(c, "current-sensor") &&
228		   !strcmp(l, "CPU Current")) {
229		ads->sens.ops = &smu_cpuamp_ops;
230		ads->sens.name = "cpu-current";
231		if (cpuvcp == NULL) {
232			DBG("wf: cpuvcp partition (%02x) not found\n",
233			    SMU_SDB_CPUVCP_ID);
234			goto fail;
235		}
236	} else if (!strcmp(c, "voltage-sensor") &&
237		   !strcmp(l, "CPU Voltage")) {
238		ads->sens.ops = &smu_cpuvolt_ops;
239		ads->sens.name = "cpu-voltage";
240		if (cpuvcp == NULL) {
241			DBG("wf: cpuvcp partition (%02x) not found\n",
242			    SMU_SDB_CPUVCP_ID);
243			goto fail;
244		}
245	} else if (!strcmp(c, "power-sensor") &&
246		   !strcmp(l, "Slots Power")) {
247		ads->sens.ops = &smu_slotspow_ops;
248		ads->sens.name = "slots-power";
249		if (slotspow == NULL) {
250			DBG("wf: slotspow partition (%02x) not found\n",
251			    SMU_SDB_SLOTSPOW_ID);
252			goto fail;
253		}
254	} else
255		goto fail;
256
257	v = of_get_property(node, "reg", NULL);
258	if (v == NULL)
259		goto fail;
260	ads->reg = *v;
261
262	if (wf_register_sensor(&ads->sens))
263		goto fail;
264	return ads;
265 fail:
266	kfree(ads);
267	return NULL;
268}
269
270/*
271 * SMU Power combo sensor object
272 */
273
274struct smu_cpu_power_sensor {
275	struct list_head	link;
276	struct wf_sensor	*volts;
277	struct wf_sensor	*amps;
278	int			fake_volts : 1;
279	int			quadratic : 1;
280	struct wf_sensor	sens;
281};
282#define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens)
283
284static struct smu_cpu_power_sensor *smu_cpu_power;
285
286static void smu_cpu_power_release(struct wf_sensor *sr)
287{
288	struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
289
290	if (pow->volts)
291		wf_put_sensor(pow->volts);
292	if (pow->amps)
293		wf_put_sensor(pow->amps);
294	kfree(pow);
295}
296
297static int smu_cpu_power_get(struct wf_sensor *sr, s32 *value)
298{
299	struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
300	s32 volts, amps, power;
301	u64 tmps, tmpa, tmpb;
302	int rc;
303
304	rc = pow->amps->ops->get_value(pow->amps, &amps);
305	if (rc)
306		return rc;
307
308	if (pow->fake_volts) {
309		*value = amps * 12 - 0x30000;
310		return 0;
311	}
312
313	rc = pow->volts->ops->get_value(pow->volts, &volts);
314	if (rc)
315		return rc;
316
317	power = (s32)((((u64)volts) * ((u64)amps)) >> 16);
318	if (!pow->quadratic) {
319		*value = power;
320		return 0;
321	}
322	tmps = (((u64)power) * ((u64)power)) >> 16;
323	tmpa = ((u64)cpuvcp->power_quads[0]) * tmps;
324	tmpb = ((u64)cpuvcp->power_quads[1]) * ((u64)power);
325	*value = (tmpa >> 28) + (tmpb >> 28) + (cpuvcp->power_quads[2] >> 12);
326
327	return 0;
328}
329
330static struct wf_sensor_ops smu_cpu_power_ops = {
331	.get_value	= smu_cpu_power_get,
332	.release	= smu_cpu_power_release,
333	.owner		= THIS_MODULE,
334};
335
336
337static struct smu_cpu_power_sensor *
338smu_cpu_power_create(struct wf_sensor *volts, struct wf_sensor *amps)
339{
340	struct smu_cpu_power_sensor *pow;
341
342	pow = kmalloc(sizeof(struct smu_cpu_power_sensor), GFP_KERNEL);
343	if (pow == NULL)
344		return NULL;
345	pow->sens.ops = &smu_cpu_power_ops;
346	pow->sens.name = "cpu-power";
347
348	wf_get_sensor(volts);
349	pow->volts = volts;
350	wf_get_sensor(amps);
351	pow->amps = amps;
352
353	/* Some early machines need a faked voltage */
354	if (debugswitches && ((*debugswitches) & 0x80)) {
355		printk(KERN_INFO "windfarm: CPU Power sensor using faked"
356		       " voltage !\n");
357		pow->fake_volts = 1;
358	} else
359		pow->fake_volts = 0;
360
361	/* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now,
362	 * I yet have to figure out what's up with 8,2 and will have to
363	 * adjust for later, unless we can 100% trust the SDB partition...
364	 */
365	if ((of_machine_is_compatible("PowerMac8,1") ||
366	     of_machine_is_compatible("PowerMac8,2") ||
367	     of_machine_is_compatible("PowerMac9,1")) &&
368	    cpuvcp_version >= 2) {
369		pow->quadratic = 1;
370		DBG("windfarm: CPU Power using quadratic transform\n");
371	} else
372		pow->quadratic = 0;
373
374	if (wf_register_sensor(&pow->sens))
375		goto fail;
376	return pow;
377 fail:
378	kfree(pow);
379	return NULL;
380}
381
382static void smu_fetch_param_partitions(void)
383{
384	const struct smu_sdbp_header *hdr;
385
386	/* Get CPU voltage/current/power calibration data */
387	hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL);
388	if (hdr != NULL) {
389		cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1];
390		/* Keep version around */
391		cpuvcp_version = hdr->version;
392	}
393
394	/* Get CPU diode calibration data */
395	hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL);
396	if (hdr != NULL)
397		cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1];
398
399	/* Get slots power calibration data if any */
400	hdr = smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID, NULL);
401	if (hdr != NULL)
402		slotspow = (struct smu_sdbp_slotspow *)&hdr[1];
403
404	/* Get debug switches if any */
405	hdr = smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID, NULL);
406	if (hdr != NULL)
407		debugswitches = (u8 *)&hdr[1];
408}
409
410static int __init smu_sensors_init(void)
411{
412	struct device_node *smu, *sensors, *s;
413	struct smu_ad_sensor *volt_sensor = NULL, *curr_sensor = NULL;
414
415	if (!smu_present())
416		return -ENODEV;
417
418	/* Get parameters partitions */
419	smu_fetch_param_partitions();
420
421	smu = of_find_node_by_type(NULL, "smu");
422	if (smu == NULL)
423		return -ENODEV;
424
425	/* Look for sensors subdir */
426	for (sensors = NULL;
427	     (sensors = of_get_next_child(smu, sensors)) != NULL;)
428		if (!strcmp(sensors->name, "sensors"))
429			break;
430
431	of_node_put(smu);
432
433	/* Create basic sensors */
434	for (s = NULL;
435	     sensors && (s = of_get_next_child(sensors, s)) != NULL;) {
436		struct smu_ad_sensor *ads;
437
438		ads = smu_ads_create(s);
439		if (ads == NULL)
440			continue;
441		list_add(&ads->link, &smu_ads);
442		/* keep track of cpu voltage & current */
443		if (!strcmp(ads->sens.name, "cpu-voltage"))
444			volt_sensor = ads;
445		else if (!strcmp(ads->sens.name, "cpu-current"))
446			curr_sensor = ads;
447	}
448
449	of_node_put(sensors);
450
451	/* Create CPU power sensor if possible */
452	if (volt_sensor && curr_sensor)
453		smu_cpu_power = smu_cpu_power_create(&volt_sensor->sens,
454						     &curr_sensor->sens);
455
456	return 0;
457}
458
459static void __exit smu_sensors_exit(void)
460{
461	struct smu_ad_sensor *ads;
462
463	/* dispose of power sensor */
464	if (smu_cpu_power)
465		wf_unregister_sensor(&smu_cpu_power->sens);
466
467	/* dispose of basic sensors */
468	while (!list_empty(&smu_ads)) {
469		ads = list_entry(smu_ads.next, struct smu_ad_sensor, link);
470		list_del(&ads->link);
471		wf_unregister_sensor(&ads->sens);
472	}
473}
474
475
476module_init(smu_sensors_init);
477module_exit(smu_sensors_exit);
478
479MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
480MODULE_DESCRIPTION("SMU sensor objects for PowerMacs thermal control");
481MODULE_LICENSE("GPL");
482