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