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