1 // SPDX-License-Identifier: GPL-2.0-only
3 * Windfarm PowerMac thermal control. SMU based sensors
5 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
6 * <benh@kernel.crashing.org>
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/wait.h>
16 #include <linux/completion.h>
19 #include <asm/machdep.h>
21 #include <asm/sections.h>
31 #define DBG(args...) printk(args)
33 #define DBG(args...) do { } while(0)
37 * Various SMU "partitions" calibration objects for which we
38 * keep pointers here for use by bits & pieces of the driver
40 static struct smu_sdbp_cpuvcp *cpuvcp;
41 static int cpuvcp_version;
42 static struct smu_sdbp_cpudiode *cpudiode;
43 static struct smu_sdbp_slotspow *slotspow;
44 static u8 *debugswitches;
47 * SMU basic sensors objects
50 static LIST_HEAD(smu_ads);
52 struct smu_ad_sensor {
53 struct list_head link;
54 u32 reg; /* index in SMU */
55 struct wf_sensor sens;
57 #define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
59 static void smu_ads_release(struct wf_sensor *sr)
61 struct smu_ad_sensor *ads = to_smu_ads(sr);
66 static int smu_read_adc(u8 id, s32 *value)
68 struct smu_simple_cmd cmd;
69 DECLARE_COMPLETION_ONSTACK(comp);
72 rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1,
73 smu_done_complete, &comp, id);
76 wait_for_completion(&comp);
77 if (cmd.cmd.status != 0)
78 return cmd.cmd.status;
79 if (cmd.cmd.reply_len != 2) {
80 printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n",
81 id, cmd.cmd.reply_len);
84 *value = *((u16 *)cmd.buffer);
88 static int smu_cputemp_get(struct wf_sensor *sr, s32 *value)
90 struct smu_ad_sensor *ads = to_smu_ads(sr);
95 rc = smu_read_adc(ads->reg, &val);
97 printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n",
102 /* Ok, we have to scale & adjust, taking units into account */
103 scaled = (s64)(((u64)val) * (u64)cpudiode->m_value);
105 scaled += ((s64)cpudiode->b_value) << 9;
106 *value = (s32)(scaled << 1);
111 static int smu_cpuamp_get(struct wf_sensor *sr, s32 *value)
113 struct smu_ad_sensor *ads = to_smu_ads(sr);
117 rc = smu_read_adc(ads->reg, &val);
119 printk(KERN_ERR "windfarm: read CPU current failed, err %d\n",
124 /* Ok, we have to scale & adjust, taking units into account */
125 scaled = (s32)(val * (u32)cpuvcp->curr_scale);
126 scaled += (s32)cpuvcp->curr_offset;
127 *value = scaled << 4;
132 static int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value)
134 struct smu_ad_sensor *ads = to_smu_ads(sr);
138 rc = smu_read_adc(ads->reg, &val);
140 printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n",
145 /* Ok, we have to scale & adjust, taking units into account */
146 scaled = (s32)(val * (u32)cpuvcp->volt_scale);
147 scaled += (s32)cpuvcp->volt_offset;
148 *value = scaled << 4;
153 static int smu_slotspow_get(struct wf_sensor *sr, s32 *value)
155 struct smu_ad_sensor *ads = to_smu_ads(sr);
159 rc = smu_read_adc(ads->reg, &val);
161 printk(KERN_ERR "windfarm: read slots power failed, err %d\n",
166 /* Ok, we have to scale & adjust, taking units into account */
167 scaled = (s32)(val * (u32)slotspow->pow_scale);
168 scaled += (s32)slotspow->pow_offset;
169 *value = scaled << 4;
175 static const struct wf_sensor_ops smu_cputemp_ops = {
176 .get_value = smu_cputemp_get,
177 .release = smu_ads_release,
178 .owner = THIS_MODULE,
180 static const struct wf_sensor_ops smu_cpuamp_ops = {
181 .get_value = smu_cpuamp_get,
182 .release = smu_ads_release,
183 .owner = THIS_MODULE,
185 static const struct wf_sensor_ops smu_cpuvolt_ops = {
186 .get_value = smu_cpuvolt_get,
187 .release = smu_ads_release,
188 .owner = THIS_MODULE,
190 static const struct wf_sensor_ops smu_slotspow_ops = {
191 .get_value = smu_slotspow_get,
192 .release = smu_ads_release,
193 .owner = THIS_MODULE,
197 static struct smu_ad_sensor *smu_ads_create(struct device_node *node)
199 struct smu_ad_sensor *ads;
203 ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL);
206 l = of_get_property(node, "location", NULL);
210 /* We currently pick the sensors based on the OF name and location
211 * properties, while Darwin uses the sensor-id's.
212 * The problem with the IDs is that they are model specific while it
213 * looks like apple has been doing a reasonably good job at keeping
214 * the names and locations consistents so I'll stick with the names
215 * and locations for now.
217 if (of_node_is_type(node, "temp-sensor") &&
218 !strcmp(l, "CPU T-Diode")) {
219 ads->sens.ops = &smu_cputemp_ops;
220 ads->sens.name = "cpu-temp";
221 if (cpudiode == NULL) {
222 DBG("wf: cpudiode partition (%02x) not found\n",
223 SMU_SDB_CPUDIODE_ID);
226 } else if (of_node_is_type(node, "current-sensor") &&
227 !strcmp(l, "CPU Current")) {
228 ads->sens.ops = &smu_cpuamp_ops;
229 ads->sens.name = "cpu-current";
230 if (cpuvcp == NULL) {
231 DBG("wf: cpuvcp partition (%02x) not found\n",
235 } else if (of_node_is_type(node, "voltage-sensor") &&
236 !strcmp(l, "CPU Voltage")) {
237 ads->sens.ops = &smu_cpuvolt_ops;
238 ads->sens.name = "cpu-voltage";
239 if (cpuvcp == NULL) {
240 DBG("wf: cpuvcp partition (%02x) not found\n",
244 } else if (of_node_is_type(node, "power-sensor") &&
245 !strcmp(l, "Slots Power")) {
246 ads->sens.ops = &smu_slotspow_ops;
247 ads->sens.name = "slots-power";
248 if (slotspow == NULL) {
249 DBG("wf: slotspow partition (%02x) not found\n",
250 SMU_SDB_SLOTSPOW_ID);
256 v = of_get_property(node, "reg", NULL);
261 if (wf_register_sensor(&ads->sens))
270 * SMU Power combo sensor object
273 struct smu_cpu_power_sensor {
274 struct list_head link;
275 struct wf_sensor *volts;
276 struct wf_sensor *amps;
277 unsigned int fake_volts : 1;
278 unsigned int quadratic : 1;
279 struct wf_sensor sens;
281 #define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens)
283 static struct smu_cpu_power_sensor *smu_cpu_power;
285 static void smu_cpu_power_release(struct wf_sensor *sr)
287 struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
290 wf_put_sensor(pow->volts);
292 wf_put_sensor(pow->amps);
296 static int smu_cpu_power_get(struct wf_sensor *sr, s32 *value)
298 struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
299 s32 volts, amps, power;
300 u64 tmps, tmpa, tmpb;
303 rc = pow->amps->ops->get_value(pow->amps, &s);
307 if (pow->fake_volts) {
308 *value = amps * 12 - 0x30000;
312 rc = pow->volts->ops->get_value(pow->volts, &volts);
316 power = (s32)((((u64)volts) * ((u64)amps)) >> 16);
317 if (!pow->quadratic) {
321 tmps = (((u64)power) * ((u64)power)) >> 16;
322 tmpa = ((u64)cpuvcp->power_quads[0]) * tmps;
323 tmpb = ((u64)cpuvcp->power_quads[1]) * ((u64)power);
324 *value = (tmpa >> 28) + (tmpb >> 28) + (cpuvcp->power_quads[2] >> 12);
329 static const struct wf_sensor_ops smu_cpu_power_ops = {
330 .get_value = smu_cpu_power_get,
331 .release = smu_cpu_power_release,
332 .owner = THIS_MODULE,
336 static struct smu_cpu_power_sensor *
337 smu_cpu_power_create(struct wf_sensor *volts, struct wf_sensor *amps)
339 struct smu_cpu_power_sensor *pow;
341 pow = kmalloc(sizeof(struct smu_cpu_power_sensor), GFP_KERNEL);
344 pow->sens.ops = &smu_cpu_power_ops;
345 pow->sens.name = "cpu-power";
347 wf_get_sensor(volts);
352 /* Some early machines need a faked voltage */
353 if (debugswitches && ((*debugswitches) & 0x80)) {
354 printk(KERN_INFO "windfarm: CPU Power sensor using faked"
360 /* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now,
361 * I yet have to figure out what's up with 8,2 and will have to
362 * adjust for later, unless we can 100% trust the SDB partition...
364 if ((of_machine_is_compatible("PowerMac8,1") ||
365 of_machine_is_compatible("PowerMac8,2") ||
366 of_machine_is_compatible("PowerMac9,1")) &&
367 cpuvcp_version >= 2) {
369 DBG("windfarm: CPU Power using quadratic transform\n");
373 if (wf_register_sensor(&pow->sens))
381 static void smu_fetch_param_partitions(void)
383 const struct smu_sdbp_header *hdr;
385 /* Get CPU voltage/current/power calibration data */
386 hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL);
388 cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1];
389 /* Keep version around */
390 cpuvcp_version = hdr->version;
393 /* Get CPU diode calibration data */
394 hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL);
396 cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1];
398 /* Get slots power calibration data if any */
399 hdr = smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID, NULL);
401 slotspow = (struct smu_sdbp_slotspow *)&hdr[1];
403 /* Get debug switches if any */
404 hdr = smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID, NULL);
406 debugswitches = (u8 *)&hdr[1];
409 static int __init smu_sensors_init(void)
411 struct device_node *smu, *sensors, *s;
412 struct smu_ad_sensor *volt_sensor = NULL, *curr_sensor = NULL;
417 /* Get parameters partitions */
418 smu_fetch_param_partitions();
420 smu = of_find_node_by_type(NULL, "smu");
424 /* Look for sensors subdir */
425 for_each_child_of_node(smu, sensors)
426 if (of_node_name_eq(sensors, "sensors"))
431 /* Create basic sensors */
433 sensors && (s = of_get_next_child(sensors, s)) != NULL;) {
434 struct smu_ad_sensor *ads;
436 ads = smu_ads_create(s);
439 list_add(&ads->link, &smu_ads);
440 /* keep track of cpu voltage & current */
441 if (!strcmp(ads->sens.name, "cpu-voltage"))
443 else if (!strcmp(ads->sens.name, "cpu-current"))
447 of_node_put(sensors);
449 /* Create CPU power sensor if possible */
450 if (volt_sensor && curr_sensor)
451 smu_cpu_power = smu_cpu_power_create(&volt_sensor->sens,
457 static void __exit smu_sensors_exit(void)
459 struct smu_ad_sensor *ads;
461 /* dispose of power sensor */
463 wf_unregister_sensor(&smu_cpu_power->sens);
465 /* dispose of basic sensors */
466 while (!list_empty(&smu_ads)) {
467 ads = list_entry(smu_ads.next, struct smu_ad_sensor, link);
468 list_del(&ads->link);
469 wf_unregister_sensor(&ads->sens);
474 module_init(smu_sensors_init);
475 module_exit(smu_sensors_exit);
477 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
478 MODULE_DESCRIPTION("SMU sensor objects for PowerMacs thermal control");
479 MODULE_LICENSE("GPL");