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