xref: /linux/drivers/macintosh/windfarm_smu_sensors.c (revision 6e9a4738c9fadb7cbdcabc1e3b415159f3741ed9)
175722d39SBenjamin Herrenschmidt /*
275722d39SBenjamin Herrenschmidt  * Windfarm PowerMac thermal control. SMU based sensors
375722d39SBenjamin Herrenschmidt  *
475722d39SBenjamin Herrenschmidt  * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
575722d39SBenjamin Herrenschmidt  *                    <benh@kernel.crashing.org>
675722d39SBenjamin Herrenschmidt  *
775722d39SBenjamin Herrenschmidt  * Released under the term of the GNU GPL v2.
875722d39SBenjamin Herrenschmidt  */
975722d39SBenjamin Herrenschmidt 
1075722d39SBenjamin Herrenschmidt #include <linux/types.h>
1175722d39SBenjamin Herrenschmidt #include <linux/errno.h>
1275722d39SBenjamin Herrenschmidt #include <linux/kernel.h>
1375722d39SBenjamin Herrenschmidt #include <linux/delay.h>
1475722d39SBenjamin Herrenschmidt #include <linux/slab.h>
1575722d39SBenjamin Herrenschmidt #include <linux/init.h>
1675722d39SBenjamin Herrenschmidt #include <linux/wait.h>
17de25968cSTim Schmielau #include <linux/completion.h>
1875722d39SBenjamin Herrenschmidt #include <asm/prom.h>
1975722d39SBenjamin Herrenschmidt #include <asm/machdep.h>
2075722d39SBenjamin Herrenschmidt #include <asm/io.h>
2175722d39SBenjamin Herrenschmidt #include <asm/system.h>
2275722d39SBenjamin Herrenschmidt #include <asm/sections.h>
2375722d39SBenjamin Herrenschmidt #include <asm/smu.h>
2475722d39SBenjamin Herrenschmidt 
2575722d39SBenjamin Herrenschmidt #include "windfarm.h"
2675722d39SBenjamin Herrenschmidt 
2775722d39SBenjamin Herrenschmidt #define VERSION "0.2"
2875722d39SBenjamin Herrenschmidt 
2975722d39SBenjamin Herrenschmidt #undef DEBUG
3075722d39SBenjamin Herrenschmidt 
3175722d39SBenjamin Herrenschmidt #ifdef DEBUG
3275722d39SBenjamin Herrenschmidt #define DBG(args...)	printk(args)
3375722d39SBenjamin Herrenschmidt #else
3475722d39SBenjamin Herrenschmidt #define DBG(args...)	do { } while(0)
3575722d39SBenjamin Herrenschmidt #endif
3675722d39SBenjamin Herrenschmidt 
3775722d39SBenjamin Herrenschmidt /*
3875722d39SBenjamin Herrenschmidt  * Various SMU "partitions" calibration objects for which we
3975722d39SBenjamin Herrenschmidt  * keep pointers here for use by bits & pieces of the driver
4075722d39SBenjamin Herrenschmidt  */
4175722d39SBenjamin Herrenschmidt static struct smu_sdbp_cpuvcp *cpuvcp;
4275722d39SBenjamin Herrenschmidt static int  cpuvcp_version;
4375722d39SBenjamin Herrenschmidt static struct smu_sdbp_cpudiode *cpudiode;
4475722d39SBenjamin Herrenschmidt static struct smu_sdbp_slotspow *slotspow;
4575722d39SBenjamin Herrenschmidt static u8 *debugswitches;
4675722d39SBenjamin Herrenschmidt 
4775722d39SBenjamin Herrenschmidt /*
4875722d39SBenjamin Herrenschmidt  * SMU basic sensors objects
4975722d39SBenjamin Herrenschmidt  */
5075722d39SBenjamin Herrenschmidt 
5175722d39SBenjamin Herrenschmidt static LIST_HEAD(smu_ads);
5275722d39SBenjamin Herrenschmidt 
5375722d39SBenjamin Herrenschmidt struct smu_ad_sensor {
5475722d39SBenjamin Herrenschmidt 	struct list_head	link;
5575722d39SBenjamin Herrenschmidt 	u32			reg;		/* index in SMU */
5675722d39SBenjamin Herrenschmidt 	struct wf_sensor	sens;
5775722d39SBenjamin Herrenschmidt };
5875722d39SBenjamin Herrenschmidt #define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
5975722d39SBenjamin Herrenschmidt 
6075722d39SBenjamin Herrenschmidt static void smu_ads_release(struct wf_sensor *sr)
6175722d39SBenjamin Herrenschmidt {
6275722d39SBenjamin Herrenschmidt 	struct smu_ad_sensor *ads = to_smu_ads(sr);
6375722d39SBenjamin Herrenschmidt 
6475722d39SBenjamin Herrenschmidt 	kfree(ads);
6575722d39SBenjamin Herrenschmidt }
6675722d39SBenjamin Herrenschmidt 
6775722d39SBenjamin Herrenschmidt static int smu_read_adc(u8 id, s32 *value)
6875722d39SBenjamin Herrenschmidt {
6975722d39SBenjamin Herrenschmidt 	struct smu_simple_cmd	cmd;
70*6e9a4738SPeter Zijlstra 	DECLARE_COMPLETION_ONSTACK(comp);
7175722d39SBenjamin Herrenschmidt 	int rc;
7275722d39SBenjamin Herrenschmidt 
7375722d39SBenjamin Herrenschmidt 	rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1,
7475722d39SBenjamin Herrenschmidt 			      smu_done_complete, &comp, id);
7575722d39SBenjamin Herrenschmidt 	if (rc)
7675722d39SBenjamin Herrenschmidt 		return rc;
7775722d39SBenjamin Herrenschmidt 	wait_for_completion(&comp);
7875722d39SBenjamin Herrenschmidt 	if (cmd.cmd.status != 0)
7975722d39SBenjamin Herrenschmidt 		return cmd.cmd.status;
8075722d39SBenjamin Herrenschmidt 	if (cmd.cmd.reply_len != 2) {
8175722d39SBenjamin Herrenschmidt 		printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n",
8275722d39SBenjamin Herrenschmidt 		       id, cmd.cmd.reply_len);
8375722d39SBenjamin Herrenschmidt 		return -EIO;
8475722d39SBenjamin Herrenschmidt 	}
8575722d39SBenjamin Herrenschmidt 	*value = *((u16 *)cmd.buffer);
8675722d39SBenjamin Herrenschmidt 	return 0;
8775722d39SBenjamin Herrenschmidt }
8875722d39SBenjamin Herrenschmidt 
8975722d39SBenjamin Herrenschmidt static int smu_cputemp_get(struct wf_sensor *sr, s32 *value)
9075722d39SBenjamin Herrenschmidt {
9175722d39SBenjamin Herrenschmidt 	struct smu_ad_sensor *ads = to_smu_ads(sr);
9275722d39SBenjamin Herrenschmidt 	int rc;
9375722d39SBenjamin Herrenschmidt 	s32 val;
9475722d39SBenjamin Herrenschmidt 	s64 scaled;
9575722d39SBenjamin Herrenschmidt 
9675722d39SBenjamin Herrenschmidt 	rc = smu_read_adc(ads->reg, &val);
9775722d39SBenjamin Herrenschmidt 	if (rc) {
9875722d39SBenjamin Herrenschmidt 		printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n",
9975722d39SBenjamin Herrenschmidt 		       rc);
10075722d39SBenjamin Herrenschmidt 		return rc;
10175722d39SBenjamin Herrenschmidt 	}
10275722d39SBenjamin Herrenschmidt 
10375722d39SBenjamin Herrenschmidt 	/* Ok, we have to scale & adjust, taking units into account */
10475722d39SBenjamin Herrenschmidt 	scaled = (s64)(((u64)val) * (u64)cpudiode->m_value);
10575722d39SBenjamin Herrenschmidt 	scaled >>= 3;
10675722d39SBenjamin Herrenschmidt 	scaled += ((s64)cpudiode->b_value) << 9;
10775722d39SBenjamin Herrenschmidt 	*value = (s32)(scaled << 1);
10875722d39SBenjamin Herrenschmidt 
10975722d39SBenjamin Herrenschmidt 	return 0;
11075722d39SBenjamin Herrenschmidt }
11175722d39SBenjamin Herrenschmidt 
11275722d39SBenjamin Herrenschmidt static int smu_cpuamp_get(struct wf_sensor *sr, s32 *value)
11375722d39SBenjamin Herrenschmidt {
11475722d39SBenjamin Herrenschmidt 	struct smu_ad_sensor *ads = to_smu_ads(sr);
11575722d39SBenjamin Herrenschmidt 	s32 val, scaled;
11675722d39SBenjamin Herrenschmidt 	int rc;
11775722d39SBenjamin Herrenschmidt 
11875722d39SBenjamin Herrenschmidt 	rc = smu_read_adc(ads->reg, &val);
11975722d39SBenjamin Herrenschmidt 	if (rc) {
12075722d39SBenjamin Herrenschmidt 		printk(KERN_ERR "windfarm: read CPU current failed, err %d\n",
12175722d39SBenjamin Herrenschmidt 		       rc);
12275722d39SBenjamin Herrenschmidt 		return rc;
12375722d39SBenjamin Herrenschmidt 	}
12475722d39SBenjamin Herrenschmidt 
12575722d39SBenjamin Herrenschmidt 	/* Ok, we have to scale & adjust, taking units into account */
12675722d39SBenjamin Herrenschmidt 	scaled = (s32)(val * (u32)cpuvcp->curr_scale);
12775722d39SBenjamin Herrenschmidt 	scaled += (s32)cpuvcp->curr_offset;
12875722d39SBenjamin Herrenschmidt 	*value = scaled << 4;
12975722d39SBenjamin Herrenschmidt 
13075722d39SBenjamin Herrenschmidt 	return 0;
13175722d39SBenjamin Herrenschmidt }
13275722d39SBenjamin Herrenschmidt 
13375722d39SBenjamin Herrenschmidt static int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value)
13475722d39SBenjamin Herrenschmidt {
13575722d39SBenjamin Herrenschmidt 	struct smu_ad_sensor *ads = to_smu_ads(sr);
13675722d39SBenjamin Herrenschmidt 	s32 val, scaled;
13775722d39SBenjamin Herrenschmidt 	int rc;
13875722d39SBenjamin Herrenschmidt 
13975722d39SBenjamin Herrenschmidt 	rc = smu_read_adc(ads->reg, &val);
14075722d39SBenjamin Herrenschmidt 	if (rc) {
14175722d39SBenjamin Herrenschmidt 		printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n",
14275722d39SBenjamin Herrenschmidt 		       rc);
14375722d39SBenjamin Herrenschmidt 		return rc;
14475722d39SBenjamin Herrenschmidt 	}
14575722d39SBenjamin Herrenschmidt 
14675722d39SBenjamin Herrenschmidt 	/* Ok, we have to scale & adjust, taking units into account */
14775722d39SBenjamin Herrenschmidt 	scaled = (s32)(val * (u32)cpuvcp->volt_scale);
14875722d39SBenjamin Herrenschmidt 	scaled += (s32)cpuvcp->volt_offset;
14975722d39SBenjamin Herrenschmidt 	*value = scaled << 4;
15075722d39SBenjamin Herrenschmidt 
15175722d39SBenjamin Herrenschmidt 	return 0;
15275722d39SBenjamin Herrenschmidt }
15375722d39SBenjamin Herrenschmidt 
15475722d39SBenjamin Herrenschmidt static int smu_slotspow_get(struct wf_sensor *sr, s32 *value)
15575722d39SBenjamin Herrenschmidt {
15675722d39SBenjamin Herrenschmidt 	struct smu_ad_sensor *ads = to_smu_ads(sr);
15775722d39SBenjamin Herrenschmidt 	s32 val, scaled;
15875722d39SBenjamin Herrenschmidt 	int rc;
15975722d39SBenjamin Herrenschmidt 
16075722d39SBenjamin Herrenschmidt 	rc = smu_read_adc(ads->reg, &val);
16175722d39SBenjamin Herrenschmidt 	if (rc) {
16275722d39SBenjamin Herrenschmidt 		printk(KERN_ERR "windfarm: read slots power failed, err %d\n",
16375722d39SBenjamin Herrenschmidt 		       rc);
16475722d39SBenjamin Herrenschmidt 		return rc;
16575722d39SBenjamin Herrenschmidt 	}
16675722d39SBenjamin Herrenschmidt 
16775722d39SBenjamin Herrenschmidt 	/* Ok, we have to scale & adjust, taking units into account */
16875722d39SBenjamin Herrenschmidt 	scaled = (s32)(val * (u32)slotspow->pow_scale);
16975722d39SBenjamin Herrenschmidt 	scaled += (s32)slotspow->pow_offset;
17075722d39SBenjamin Herrenschmidt 	*value = scaled << 4;
17175722d39SBenjamin Herrenschmidt 
17275722d39SBenjamin Herrenschmidt 	return 0;
17375722d39SBenjamin Herrenschmidt }
17475722d39SBenjamin Herrenschmidt 
17575722d39SBenjamin Herrenschmidt 
17675722d39SBenjamin Herrenschmidt static struct wf_sensor_ops smu_cputemp_ops = {
17775722d39SBenjamin Herrenschmidt 	.get_value	= smu_cputemp_get,
17875722d39SBenjamin Herrenschmidt 	.release	= smu_ads_release,
17975722d39SBenjamin Herrenschmidt 	.owner		= THIS_MODULE,
18075722d39SBenjamin Herrenschmidt };
18175722d39SBenjamin Herrenschmidt static struct wf_sensor_ops smu_cpuamp_ops = {
18275722d39SBenjamin Herrenschmidt 	.get_value	= smu_cpuamp_get,
18375722d39SBenjamin Herrenschmidt 	.release	= smu_ads_release,
18475722d39SBenjamin Herrenschmidt 	.owner		= THIS_MODULE,
18575722d39SBenjamin Herrenschmidt };
18675722d39SBenjamin Herrenschmidt static struct wf_sensor_ops smu_cpuvolt_ops = {
18775722d39SBenjamin Herrenschmidt 	.get_value	= smu_cpuvolt_get,
18875722d39SBenjamin Herrenschmidt 	.release	= smu_ads_release,
18975722d39SBenjamin Herrenschmidt 	.owner		= THIS_MODULE,
19075722d39SBenjamin Herrenschmidt };
19175722d39SBenjamin Herrenschmidt static struct wf_sensor_ops smu_slotspow_ops = {
19275722d39SBenjamin Herrenschmidt 	.get_value	= smu_slotspow_get,
19375722d39SBenjamin Herrenschmidt 	.release	= smu_ads_release,
19475722d39SBenjamin Herrenschmidt 	.owner		= THIS_MODULE,
19575722d39SBenjamin Herrenschmidt };
19675722d39SBenjamin Herrenschmidt 
19775722d39SBenjamin Herrenschmidt 
19875722d39SBenjamin Herrenschmidt static struct smu_ad_sensor *smu_ads_create(struct device_node *node)
19975722d39SBenjamin Herrenschmidt {
20075722d39SBenjamin Herrenschmidt 	struct smu_ad_sensor *ads;
201018a3d1dSJeremy Kerr 	const char *c, *l;
202018a3d1dSJeremy Kerr 	const u32 *v;
20375722d39SBenjamin Herrenschmidt 
20475722d39SBenjamin Herrenschmidt 	ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL);
20575722d39SBenjamin Herrenschmidt 	if (ads == NULL)
20675722d39SBenjamin Herrenschmidt 		return NULL;
207018a3d1dSJeremy Kerr 	c = get_property(node, "device_type", NULL);
208018a3d1dSJeremy Kerr 	l = get_property(node, "location", NULL);
20975722d39SBenjamin Herrenschmidt 	if (c == NULL || l == NULL)
21075722d39SBenjamin Herrenschmidt 		goto fail;
21175722d39SBenjamin Herrenschmidt 
21275722d39SBenjamin Herrenschmidt 	/* We currently pick the sensors based on the OF name and location
21375722d39SBenjamin Herrenschmidt 	 * properties, while Darwin uses the sensor-id's.
21475722d39SBenjamin Herrenschmidt 	 * The problem with the IDs is that they are model specific while it
21575722d39SBenjamin Herrenschmidt 	 * looks like apple has been doing a reasonably good job at keeping
21675722d39SBenjamin Herrenschmidt 	 * the names and locations consistents so I'll stick with the names
21775722d39SBenjamin Herrenschmidt 	 * and locations for now.
21875722d39SBenjamin Herrenschmidt 	 */
21975722d39SBenjamin Herrenschmidt 	if (!strcmp(c, "temp-sensor") &&
22075722d39SBenjamin Herrenschmidt 	    !strcmp(l, "CPU T-Diode")) {
22175722d39SBenjamin Herrenschmidt 		ads->sens.ops = &smu_cputemp_ops;
22275722d39SBenjamin Herrenschmidt 		ads->sens.name = "cpu-temp";
223ac171c46SBenjamin Herrenschmidt 		if (cpudiode == NULL) {
224ac171c46SBenjamin Herrenschmidt 			DBG("wf: cpudiode partition (%02x) not found\n",
225ac171c46SBenjamin Herrenschmidt 			    SMU_SDB_CPUDIODE_ID);
226ac171c46SBenjamin Herrenschmidt 			goto fail;
227ac171c46SBenjamin Herrenschmidt 		}
22875722d39SBenjamin Herrenschmidt 	} else if (!strcmp(c, "current-sensor") &&
22975722d39SBenjamin Herrenschmidt 		   !strcmp(l, "CPU Current")) {
23075722d39SBenjamin Herrenschmidt 		ads->sens.ops = &smu_cpuamp_ops;
23175722d39SBenjamin Herrenschmidt 		ads->sens.name = "cpu-current";
232ac171c46SBenjamin Herrenschmidt 		if (cpuvcp == NULL) {
233ac171c46SBenjamin Herrenschmidt 			DBG("wf: cpuvcp partition (%02x) not found\n",
234ac171c46SBenjamin Herrenschmidt 			    SMU_SDB_CPUVCP_ID);
235ac171c46SBenjamin Herrenschmidt 			goto fail;
236ac171c46SBenjamin Herrenschmidt 		}
23775722d39SBenjamin Herrenschmidt 	} else if (!strcmp(c, "voltage-sensor") &&
23875722d39SBenjamin Herrenschmidt 		   !strcmp(l, "CPU Voltage")) {
23975722d39SBenjamin Herrenschmidt 		ads->sens.ops = &smu_cpuvolt_ops;
24075722d39SBenjamin Herrenschmidt 		ads->sens.name = "cpu-voltage";
241ac171c46SBenjamin Herrenschmidt 		if (cpuvcp == NULL) {
242ac171c46SBenjamin Herrenschmidt 			DBG("wf: cpuvcp partition (%02x) not found\n",
243ac171c46SBenjamin Herrenschmidt 			    SMU_SDB_CPUVCP_ID);
244ac171c46SBenjamin Herrenschmidt 			goto fail;
245ac171c46SBenjamin Herrenschmidt 		}
24675722d39SBenjamin Herrenschmidt 	} else if (!strcmp(c, "power-sensor") &&
24775722d39SBenjamin Herrenschmidt 		   !strcmp(l, "Slots Power")) {
24875722d39SBenjamin Herrenschmidt 		ads->sens.ops = &smu_slotspow_ops;
24975722d39SBenjamin Herrenschmidt 		ads->sens.name = "slots-power";
25075722d39SBenjamin Herrenschmidt 		if (slotspow == NULL) {
25175722d39SBenjamin Herrenschmidt 			DBG("wf: slotspow partition (%02x) not found\n",
25275722d39SBenjamin Herrenschmidt 			    SMU_SDB_SLOTSPOW_ID);
25375722d39SBenjamin Herrenschmidt 			goto fail;
25475722d39SBenjamin Herrenschmidt 		}
25575722d39SBenjamin Herrenschmidt 	} else
25675722d39SBenjamin Herrenschmidt 		goto fail;
25775722d39SBenjamin Herrenschmidt 
258018a3d1dSJeremy Kerr 	v = get_property(node, "reg", NULL);
25975722d39SBenjamin Herrenschmidt 	if (v == NULL)
26075722d39SBenjamin Herrenschmidt 		goto fail;
26175722d39SBenjamin Herrenschmidt 	ads->reg = *v;
26275722d39SBenjamin Herrenschmidt 
26375722d39SBenjamin Herrenschmidt 	if (wf_register_sensor(&ads->sens))
26475722d39SBenjamin Herrenschmidt 		goto fail;
26575722d39SBenjamin Herrenschmidt 	return ads;
26675722d39SBenjamin Herrenschmidt  fail:
26775722d39SBenjamin Herrenschmidt 	kfree(ads);
26875722d39SBenjamin Herrenschmidt 	return NULL;
26975722d39SBenjamin Herrenschmidt }
27075722d39SBenjamin Herrenschmidt 
27175722d39SBenjamin Herrenschmidt /*
27275722d39SBenjamin Herrenschmidt  * SMU Power combo sensor object
27375722d39SBenjamin Herrenschmidt  */
27475722d39SBenjamin Herrenschmidt 
27575722d39SBenjamin Herrenschmidt struct smu_cpu_power_sensor {
27675722d39SBenjamin Herrenschmidt 	struct list_head	link;
27775722d39SBenjamin Herrenschmidt 	struct wf_sensor	*volts;
27875722d39SBenjamin Herrenschmidt 	struct wf_sensor	*amps;
27975722d39SBenjamin Herrenschmidt 	int			fake_volts : 1;
28075722d39SBenjamin Herrenschmidt 	int			quadratic : 1;
28175722d39SBenjamin Herrenschmidt 	struct wf_sensor	sens;
28275722d39SBenjamin Herrenschmidt };
28375722d39SBenjamin Herrenschmidt #define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens)
28475722d39SBenjamin Herrenschmidt 
28575722d39SBenjamin Herrenschmidt static struct smu_cpu_power_sensor *smu_cpu_power;
28675722d39SBenjamin Herrenschmidt 
28775722d39SBenjamin Herrenschmidt static void smu_cpu_power_release(struct wf_sensor *sr)
28875722d39SBenjamin Herrenschmidt {
28975722d39SBenjamin Herrenschmidt 	struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
29075722d39SBenjamin Herrenschmidt 
29175722d39SBenjamin Herrenschmidt 	if (pow->volts)
29275722d39SBenjamin Herrenschmidt 		wf_put_sensor(pow->volts);
29375722d39SBenjamin Herrenschmidt 	if (pow->amps)
29475722d39SBenjamin Herrenschmidt 		wf_put_sensor(pow->amps);
29575722d39SBenjamin Herrenschmidt 	kfree(pow);
29675722d39SBenjamin Herrenschmidt }
29775722d39SBenjamin Herrenschmidt 
29875722d39SBenjamin Herrenschmidt static int smu_cpu_power_get(struct wf_sensor *sr, s32 *value)
29975722d39SBenjamin Herrenschmidt {
30075722d39SBenjamin Herrenschmidt 	struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
30175722d39SBenjamin Herrenschmidt 	s32 volts, amps, power;
30275722d39SBenjamin Herrenschmidt 	u64 tmps, tmpa, tmpb;
30375722d39SBenjamin Herrenschmidt 	int rc;
30475722d39SBenjamin Herrenschmidt 
30575722d39SBenjamin Herrenschmidt 	rc = pow->amps->ops->get_value(pow->amps, &amps);
30675722d39SBenjamin Herrenschmidt 	if (rc)
30775722d39SBenjamin Herrenschmidt 		return rc;
30875722d39SBenjamin Herrenschmidt 
30975722d39SBenjamin Herrenschmidt 	if (pow->fake_volts) {
31075722d39SBenjamin Herrenschmidt 		*value = amps * 12 - 0x30000;
31175722d39SBenjamin Herrenschmidt 		return 0;
31275722d39SBenjamin Herrenschmidt 	}
31375722d39SBenjamin Herrenschmidt 
31475722d39SBenjamin Herrenschmidt 	rc = pow->volts->ops->get_value(pow->volts, &volts);
31575722d39SBenjamin Herrenschmidt 	if (rc)
31675722d39SBenjamin Herrenschmidt 		return rc;
31775722d39SBenjamin Herrenschmidt 
31875722d39SBenjamin Herrenschmidt 	power = (s32)((((u64)volts) * ((u64)amps)) >> 16);
31975722d39SBenjamin Herrenschmidt 	if (!pow->quadratic) {
32075722d39SBenjamin Herrenschmidt 		*value = power;
32175722d39SBenjamin Herrenschmidt 		return 0;
32275722d39SBenjamin Herrenschmidt 	}
32375722d39SBenjamin Herrenschmidt 	tmps = (((u64)power) * ((u64)power)) >> 16;
32475722d39SBenjamin Herrenschmidt 	tmpa = ((u64)cpuvcp->power_quads[0]) * tmps;
32575722d39SBenjamin Herrenschmidt 	tmpb = ((u64)cpuvcp->power_quads[1]) * ((u64)power);
32675722d39SBenjamin Herrenschmidt 	*value = (tmpa >> 28) + (tmpb >> 28) + (cpuvcp->power_quads[2] >> 12);
32775722d39SBenjamin Herrenschmidt 
32875722d39SBenjamin Herrenschmidt 	return 0;
32975722d39SBenjamin Herrenschmidt }
33075722d39SBenjamin Herrenschmidt 
33175722d39SBenjamin Herrenschmidt static struct wf_sensor_ops smu_cpu_power_ops = {
33275722d39SBenjamin Herrenschmidt 	.get_value	= smu_cpu_power_get,
33375722d39SBenjamin Herrenschmidt 	.release	= smu_cpu_power_release,
33475722d39SBenjamin Herrenschmidt 	.owner		= THIS_MODULE,
33575722d39SBenjamin Herrenschmidt };
33675722d39SBenjamin Herrenschmidt 
33775722d39SBenjamin Herrenschmidt 
33875722d39SBenjamin Herrenschmidt static struct smu_cpu_power_sensor *
33975722d39SBenjamin Herrenschmidt smu_cpu_power_create(struct wf_sensor *volts, struct wf_sensor *amps)
34075722d39SBenjamin Herrenschmidt {
34175722d39SBenjamin Herrenschmidt 	struct smu_cpu_power_sensor *pow;
34275722d39SBenjamin Herrenschmidt 
34375722d39SBenjamin Herrenschmidt 	pow = kmalloc(sizeof(struct smu_cpu_power_sensor), GFP_KERNEL);
34475722d39SBenjamin Herrenschmidt 	if (pow == NULL)
34575722d39SBenjamin Herrenschmidt 		return NULL;
34675722d39SBenjamin Herrenschmidt 	pow->sens.ops = &smu_cpu_power_ops;
34775722d39SBenjamin Herrenschmidt 	pow->sens.name = "cpu-power";
34875722d39SBenjamin Herrenschmidt 
34975722d39SBenjamin Herrenschmidt 	wf_get_sensor(volts);
35075722d39SBenjamin Herrenschmidt 	pow->volts = volts;
35175722d39SBenjamin Herrenschmidt 	wf_get_sensor(amps);
35275722d39SBenjamin Herrenschmidt 	pow->amps = amps;
35375722d39SBenjamin Herrenschmidt 
35475722d39SBenjamin Herrenschmidt 	/* Some early machines need a faked voltage */
35575722d39SBenjamin Herrenschmidt 	if (debugswitches && ((*debugswitches) & 0x80)) {
35675722d39SBenjamin Herrenschmidt 		printk(KERN_INFO "windfarm: CPU Power sensor using faked"
35775722d39SBenjamin Herrenschmidt 		       " voltage !\n");
35875722d39SBenjamin Herrenschmidt 		pow->fake_volts = 1;
35975722d39SBenjamin Herrenschmidt 	} else
36075722d39SBenjamin Herrenschmidt 		pow->fake_volts = 0;
36175722d39SBenjamin Herrenschmidt 
36275722d39SBenjamin Herrenschmidt 	/* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now,
36375722d39SBenjamin Herrenschmidt 	 * I yet have to figure out what's up with 8,2 and will have to
36475722d39SBenjamin Herrenschmidt 	 * adjust for later, unless we can 100% trust the SDB partition...
36575722d39SBenjamin Herrenschmidt 	 */
36675722d39SBenjamin Herrenschmidt 	if ((machine_is_compatible("PowerMac8,1") ||
36775722d39SBenjamin Herrenschmidt 	     machine_is_compatible("PowerMac8,2") ||
36875722d39SBenjamin Herrenschmidt 	     machine_is_compatible("PowerMac9,1")) &&
36975722d39SBenjamin Herrenschmidt 	    cpuvcp_version >= 2) {
37075722d39SBenjamin Herrenschmidt 		pow->quadratic = 1;
37175722d39SBenjamin Herrenschmidt 		DBG("windfarm: CPU Power using quadratic transform\n");
37275722d39SBenjamin Herrenschmidt 	} else
37375722d39SBenjamin Herrenschmidt 		pow->quadratic = 0;
37475722d39SBenjamin Herrenschmidt 
37575722d39SBenjamin Herrenschmidt 	if (wf_register_sensor(&pow->sens))
37675722d39SBenjamin Herrenschmidt 		goto fail;
37775722d39SBenjamin Herrenschmidt 	return pow;
37875722d39SBenjamin Herrenschmidt  fail:
37975722d39SBenjamin Herrenschmidt 	kfree(pow);
38075722d39SBenjamin Herrenschmidt 	return NULL;
38175722d39SBenjamin Herrenschmidt }
38275722d39SBenjamin Herrenschmidt 
383ac171c46SBenjamin Herrenschmidt static void smu_fetch_param_partitions(void)
38475722d39SBenjamin Herrenschmidt {
385018a3d1dSJeremy Kerr 	const struct smu_sdbp_header *hdr;
38675722d39SBenjamin Herrenschmidt 
38775722d39SBenjamin Herrenschmidt 	/* Get CPU voltage/current/power calibration data */
38875722d39SBenjamin Herrenschmidt 	hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL);
389ac171c46SBenjamin Herrenschmidt 	if (hdr != NULL) {
39075722d39SBenjamin Herrenschmidt 		cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1];
39175722d39SBenjamin Herrenschmidt 		/* Keep version around */
39275722d39SBenjamin Herrenschmidt 		cpuvcp_version = hdr->version;
393ac171c46SBenjamin Herrenschmidt 	}
39475722d39SBenjamin Herrenschmidt 
39575722d39SBenjamin Herrenschmidt 	/* Get CPU diode calibration data */
39675722d39SBenjamin Herrenschmidt 	hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL);
397ac171c46SBenjamin Herrenschmidt 	if (hdr != NULL)
39875722d39SBenjamin Herrenschmidt 		cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1];
39975722d39SBenjamin Herrenschmidt 
40075722d39SBenjamin Herrenschmidt 	/* Get slots power calibration data if any */
40175722d39SBenjamin Herrenschmidt 	hdr = smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID, NULL);
40275722d39SBenjamin Herrenschmidt 	if (hdr != NULL)
40375722d39SBenjamin Herrenschmidt 		slotspow = (struct smu_sdbp_slotspow *)&hdr[1];
40475722d39SBenjamin Herrenschmidt 
40575722d39SBenjamin Herrenschmidt 	/* Get debug switches if any */
40675722d39SBenjamin Herrenschmidt 	hdr = smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID, NULL);
40775722d39SBenjamin Herrenschmidt 	if (hdr != NULL)
40875722d39SBenjamin Herrenschmidt 		debugswitches = (u8 *)&hdr[1];
40975722d39SBenjamin Herrenschmidt }
41075722d39SBenjamin Herrenschmidt 
41175722d39SBenjamin Herrenschmidt static int __init smu_sensors_init(void)
41275722d39SBenjamin Herrenschmidt {
41375722d39SBenjamin Herrenschmidt 	struct device_node *smu, *sensors, *s;
41475722d39SBenjamin Herrenschmidt 	struct smu_ad_sensor *volt_sensor = NULL, *curr_sensor = NULL;
41575722d39SBenjamin Herrenschmidt 
41675722d39SBenjamin Herrenschmidt 	if (!smu_present())
41775722d39SBenjamin Herrenschmidt 		return -ENODEV;
41875722d39SBenjamin Herrenschmidt 
41975722d39SBenjamin Herrenschmidt 	/* Get parameters partitions */
420ac171c46SBenjamin Herrenschmidt 	smu_fetch_param_partitions();
42175722d39SBenjamin Herrenschmidt 
42275722d39SBenjamin Herrenschmidt 	smu = of_find_node_by_type(NULL, "smu");
42375722d39SBenjamin Herrenschmidt 	if (smu == NULL)
42475722d39SBenjamin Herrenschmidt 		return -ENODEV;
42575722d39SBenjamin Herrenschmidt 
42675722d39SBenjamin Herrenschmidt 	/* Look for sensors subdir */
42775722d39SBenjamin Herrenschmidt 	for (sensors = NULL;
42875722d39SBenjamin Herrenschmidt 	     (sensors = of_get_next_child(smu, sensors)) != NULL;)
42975722d39SBenjamin Herrenschmidt 		if (!strcmp(sensors->name, "sensors"))
43075722d39SBenjamin Herrenschmidt 			break;
43175722d39SBenjamin Herrenschmidt 
43275722d39SBenjamin Herrenschmidt 	of_node_put(smu);
43375722d39SBenjamin Herrenschmidt 
43475722d39SBenjamin Herrenschmidt 	/* Create basic sensors */
43575722d39SBenjamin Herrenschmidt 	for (s = NULL;
43675722d39SBenjamin Herrenschmidt 	     sensors && (s = of_get_next_child(sensors, s)) != NULL;) {
43775722d39SBenjamin Herrenschmidt 		struct smu_ad_sensor *ads;
43875722d39SBenjamin Herrenschmidt 
43975722d39SBenjamin Herrenschmidt 		ads = smu_ads_create(s);
44075722d39SBenjamin Herrenschmidt 		if (ads == NULL)
44175722d39SBenjamin Herrenschmidt 			continue;
44275722d39SBenjamin Herrenschmidt 		list_add(&ads->link, &smu_ads);
44375722d39SBenjamin Herrenschmidt 		/* keep track of cpu voltage & current */
44475722d39SBenjamin Herrenschmidt 		if (!strcmp(ads->sens.name, "cpu-voltage"))
44575722d39SBenjamin Herrenschmidt 			volt_sensor = ads;
44675722d39SBenjamin Herrenschmidt 		else if (!strcmp(ads->sens.name, "cpu-current"))
44775722d39SBenjamin Herrenschmidt 			curr_sensor = ads;
44875722d39SBenjamin Herrenschmidt 	}
44975722d39SBenjamin Herrenschmidt 
45075722d39SBenjamin Herrenschmidt 	of_node_put(sensors);
45175722d39SBenjamin Herrenschmidt 
45275722d39SBenjamin Herrenschmidt 	/* Create CPU power sensor if possible */
45375722d39SBenjamin Herrenschmidt 	if (volt_sensor && curr_sensor)
45475722d39SBenjamin Herrenschmidt 		smu_cpu_power = smu_cpu_power_create(&volt_sensor->sens,
45575722d39SBenjamin Herrenschmidt 						     &curr_sensor->sens);
45675722d39SBenjamin Herrenschmidt 
45775722d39SBenjamin Herrenschmidt 	return 0;
45875722d39SBenjamin Herrenschmidt }
45975722d39SBenjamin Herrenschmidt 
46075722d39SBenjamin Herrenschmidt static void __exit smu_sensors_exit(void)
46175722d39SBenjamin Herrenschmidt {
46275722d39SBenjamin Herrenschmidt 	struct smu_ad_sensor *ads;
46375722d39SBenjamin Herrenschmidt 
46475722d39SBenjamin Herrenschmidt 	/* dispose of power sensor */
46575722d39SBenjamin Herrenschmidt 	if (smu_cpu_power)
46675722d39SBenjamin Herrenschmidt 		wf_unregister_sensor(&smu_cpu_power->sens);
46775722d39SBenjamin Herrenschmidt 
46875722d39SBenjamin Herrenschmidt 	/* dispose of basic sensors */
46975722d39SBenjamin Herrenschmidt 	while (!list_empty(&smu_ads)) {
47075722d39SBenjamin Herrenschmidt 		ads = list_entry(smu_ads.next, struct smu_ad_sensor, link);
47175722d39SBenjamin Herrenschmidt 		list_del(&ads->link);
47275722d39SBenjamin Herrenschmidt 		wf_unregister_sensor(&ads->sens);
47375722d39SBenjamin Herrenschmidt 	}
47475722d39SBenjamin Herrenschmidt }
47575722d39SBenjamin Herrenschmidt 
47675722d39SBenjamin Herrenschmidt 
47775722d39SBenjamin Herrenschmidt module_init(smu_sensors_init);
47875722d39SBenjamin Herrenschmidt module_exit(smu_sensors_exit);
47975722d39SBenjamin Herrenschmidt 
48075722d39SBenjamin Herrenschmidt MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
48175722d39SBenjamin Herrenschmidt MODULE_DESCRIPTION("SMU sensor objects for PowerMacs thermal control");
48275722d39SBenjamin Herrenschmidt MODULE_LICENSE("GPL");
48375722d39SBenjamin Herrenschmidt 
484