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>
18 #include <asm/machdep.h>
20 #include <asm/sections.h>
30 #define DBG(args...) printk(args)
32 #define DBG(args...) do { } while(0)
36 * Various SMU "partitions" calibration objects for which we
37 * keep pointers here for use by bits & pieces of the driver
39 static struct smu_sdbp_cpuvcp
*cpuvcp
;
40 static int cpuvcp_version
;
41 static struct smu_sdbp_cpudiode
*cpudiode
;
42 static struct smu_sdbp_slotspow
*slotspow
;
43 static u8
*debugswitches
;
46 * SMU basic sensors objects
49 static LIST_HEAD(smu_ads
);
51 struct smu_ad_sensor
{
52 struct list_head link
;
53 u32 reg
; /* index in SMU */
54 struct wf_sensor sens
;
56 #define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
58 static void smu_ads_release(struct wf_sensor
*sr
)
60 struct smu_ad_sensor
*ads
= to_smu_ads(sr
);
65 static int smu_read_adc(u8 id
, s32
*value
)
67 struct smu_simple_cmd cmd
;
68 DECLARE_COMPLETION_ONSTACK(comp
);
71 rc
= smu_queue_simple(&cmd
, SMU_CMD_READ_ADC
, 1,
72 smu_done_complete
, &comp
, id
);
75 wait_for_completion(&comp
);
76 if (cmd
.cmd
.status
!= 0)
77 return cmd
.cmd
.status
;
78 if (cmd
.cmd
.reply_len
!= 2) {
79 printk(KERN_ERR
"winfarm: read ADC 0x%x returned %d bytes !\n",
80 id
, cmd
.cmd
.reply_len
);
83 *value
= *((u16
*)cmd
.buffer
);
87 static int smu_cputemp_get(struct wf_sensor
*sr
, s32
*value
)
89 struct smu_ad_sensor
*ads
= to_smu_ads(sr
);
94 rc
= smu_read_adc(ads
->reg
, &val
);
96 printk(KERN_ERR
"windfarm: read CPU temp failed, err %d\n",
101 /* Ok, we have to scale & adjust, taking units into account */
102 scaled
= (s64
)(((u64
)val
) * (u64
)cpudiode
->m_value
);
104 scaled
+= ((s64
)cpudiode
->b_value
) << 9;
105 *value
= (s32
)(scaled
<< 1);
110 static int smu_cpuamp_get(struct wf_sensor
*sr
, s32
*value
)
112 struct smu_ad_sensor
*ads
= to_smu_ads(sr
);
116 rc
= smu_read_adc(ads
->reg
, &val
);
118 printk(KERN_ERR
"windfarm: read CPU current failed, err %d\n",
123 /* Ok, we have to scale & adjust, taking units into account */
124 scaled
= (s32
)(val
* (u32
)cpuvcp
->curr_scale
);
125 scaled
+= (s32
)cpuvcp
->curr_offset
;
126 *value
= scaled
<< 4;
131 static int smu_cpuvolt_get(struct wf_sensor
*sr
, s32
*value
)
133 struct smu_ad_sensor
*ads
= to_smu_ads(sr
);
137 rc
= smu_read_adc(ads
->reg
, &val
);
139 printk(KERN_ERR
"windfarm: read CPU voltage failed, err %d\n",
144 /* Ok, we have to scale & adjust, taking units into account */
145 scaled
= (s32
)(val
* (u32
)cpuvcp
->volt_scale
);
146 scaled
+= (s32
)cpuvcp
->volt_offset
;
147 *value
= scaled
<< 4;
152 static int smu_slotspow_get(struct wf_sensor
*sr
, s32
*value
)
154 struct smu_ad_sensor
*ads
= to_smu_ads(sr
);
158 rc
= smu_read_adc(ads
->reg
, &val
);
160 printk(KERN_ERR
"windfarm: read slots power failed, err %d\n",
165 /* Ok, we have to scale & adjust, taking units into account */
166 scaled
= (s32
)(val
* (u32
)slotspow
->pow_scale
);
167 scaled
+= (s32
)slotspow
->pow_offset
;
168 *value
= scaled
<< 4;
174 static const struct wf_sensor_ops smu_cputemp_ops
= {
175 .get_value
= smu_cputemp_get
,
176 .release
= smu_ads_release
,
177 .owner
= THIS_MODULE
,
179 static const struct wf_sensor_ops smu_cpuamp_ops
= {
180 .get_value
= smu_cpuamp_get
,
181 .release
= smu_ads_release
,
182 .owner
= THIS_MODULE
,
184 static const struct wf_sensor_ops smu_cpuvolt_ops
= {
185 .get_value
= smu_cpuvolt_get
,
186 .release
= smu_ads_release
,
187 .owner
= THIS_MODULE
,
189 static const struct wf_sensor_ops smu_slotspow_ops
= {
190 .get_value
= smu_slotspow_get
,
191 .release
= smu_ads_release
,
192 .owner
= THIS_MODULE
,
196 static struct smu_ad_sensor
*smu_ads_create(struct device_node
*node
)
198 struct smu_ad_sensor
*ads
;
202 ads
= kmalloc(sizeof(struct smu_ad_sensor
), GFP_KERNEL
);
205 l
= of_get_property(node
, "location", NULL
);
209 /* We currently pick the sensors based on the OF name and location
210 * properties, while Darwin uses the sensor-id's.
211 * The problem with the IDs is that they are model specific while it
212 * looks like apple has been doing a reasonably good job at keeping
213 * the names and locations consistents so I'll stick with the names
214 * and locations for now.
216 if (of_node_is_type(node
, "temp-sensor") &&
217 !strcmp(l
, "CPU T-Diode")) {
218 ads
->sens
.ops
= &smu_cputemp_ops
;
219 ads
->sens
.name
= "cpu-temp";
220 if (cpudiode
== NULL
) {
221 DBG("wf: cpudiode partition (%02x) not found\n",
222 SMU_SDB_CPUDIODE_ID
);
225 } else if (of_node_is_type(node
, "current-sensor") &&
226 !strcmp(l
, "CPU Current")) {
227 ads
->sens
.ops
= &smu_cpuamp_ops
;
228 ads
->sens
.name
= "cpu-current";
229 if (cpuvcp
== NULL
) {
230 DBG("wf: cpuvcp partition (%02x) not found\n",
234 } else if (of_node_is_type(node
, "voltage-sensor") &&
235 !strcmp(l
, "CPU Voltage")) {
236 ads
->sens
.ops
= &smu_cpuvolt_ops
;
237 ads
->sens
.name
= "cpu-voltage";
238 if (cpuvcp
== NULL
) {
239 DBG("wf: cpuvcp partition (%02x) not found\n",
243 } else if (of_node_is_type(node
, "power-sensor") &&
244 !strcmp(l
, "Slots Power")) {
245 ads
->sens
.ops
= &smu_slotspow_ops
;
246 ads
->sens
.name
= "slots-power";
247 if (slotspow
== NULL
) {
248 DBG("wf: slotspow partition (%02x) not found\n",
249 SMU_SDB_SLOTSPOW_ID
);
255 v
= of_get_property(node
, "reg", NULL
);
260 if (wf_register_sensor(&ads
->sens
))
269 * SMU Power combo sensor object
272 struct smu_cpu_power_sensor
{
273 struct list_head link
;
274 struct wf_sensor
*volts
;
275 struct wf_sensor
*amps
;
278 struct wf_sensor sens
;
280 #define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens)
282 static struct smu_cpu_power_sensor
*smu_cpu_power
;
284 static void smu_cpu_power_release(struct wf_sensor
*sr
)
286 struct smu_cpu_power_sensor
*pow
= to_smu_cpu_power(sr
);
289 wf_put_sensor(pow
->volts
);
291 wf_put_sensor(pow
->amps
);
295 static int smu_cpu_power_get(struct wf_sensor
*sr
, s32
*value
)
297 struct smu_cpu_power_sensor
*pow
= to_smu_cpu_power(sr
);
298 s32 volts
, amps
, power
;
299 u64 tmps
, tmpa
, tmpb
;
302 rc
= pow
->amps
->ops
->get_value(pow
->amps
, &s
);
306 if (pow
->fake_volts
) {
307 *value
= amps
* 12 - 0x30000;
311 rc
= pow
->volts
->ops
->get_value(pow
->volts
, &volts
);
315 power
= (s32
)((((u64
)volts
) * ((u64
)amps
)) >> 16);
316 if (!pow
->quadratic
) {
320 tmps
= (((u64
)power
) * ((u64
)power
)) >> 16;
321 tmpa
= ((u64
)cpuvcp
->power_quads
[0]) * tmps
;
322 tmpb
= ((u64
)cpuvcp
->power_quads
[1]) * ((u64
)power
);
323 *value
= (tmpa
>> 28) + (tmpb
>> 28) + (cpuvcp
->power_quads
[2] >> 12);
328 static const struct wf_sensor_ops smu_cpu_power_ops
= {
329 .get_value
= smu_cpu_power_get
,
330 .release
= smu_cpu_power_release
,
331 .owner
= THIS_MODULE
,
335 static struct smu_cpu_power_sensor
*
336 smu_cpu_power_create(struct wf_sensor
*volts
, struct wf_sensor
*amps
)
338 struct smu_cpu_power_sensor
*pow
;
340 pow
= kmalloc(sizeof(struct smu_cpu_power_sensor
), GFP_KERNEL
);
343 pow
->sens
.ops
= &smu_cpu_power_ops
;
344 pow
->sens
.name
= "cpu-power";
346 wf_get_sensor(volts
);
351 /* Some early machines need a faked voltage */
352 if (debugswitches
&& ((*debugswitches
) & 0x80)) {
353 printk(KERN_INFO
"windfarm: CPU Power sensor using faked"
359 /* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now,
360 * I yet have to figure out what's up with 8,2 and will have to
361 * adjust for later, unless we can 100% trust the SDB partition...
363 if ((of_machine_is_compatible("PowerMac8,1") ||
364 of_machine_is_compatible("PowerMac8,2") ||
365 of_machine_is_compatible("PowerMac9,1")) &&
366 cpuvcp_version
>= 2) {
368 DBG("windfarm: CPU Power using quadratic transform\n");
372 if (wf_register_sensor(&pow
->sens
))
380 static void smu_fetch_param_partitions(void)
382 const struct smu_sdbp_header
*hdr
;
384 /* Get CPU voltage/current/power calibration data */
385 hdr
= smu_get_sdb_partition(SMU_SDB_CPUVCP_ID
, NULL
);
387 cpuvcp
= (struct smu_sdbp_cpuvcp
*)&hdr
[1];
388 /* Keep version around */
389 cpuvcp_version
= hdr
->version
;
392 /* Get CPU diode calibration data */
393 hdr
= smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID
, NULL
);
395 cpudiode
= (struct smu_sdbp_cpudiode
*)&hdr
[1];
397 /* Get slots power calibration data if any */
398 hdr
= smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID
, NULL
);
400 slotspow
= (struct smu_sdbp_slotspow
*)&hdr
[1];
402 /* Get debug switches if any */
403 hdr
= smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID
, NULL
);
405 debugswitches
= (u8
*)&hdr
[1];
408 static int __init
smu_sensors_init(void)
410 struct device_node
*smu
, *sensors
, *s
;
411 struct smu_ad_sensor
*volt_sensor
= NULL
, *curr_sensor
= NULL
;
416 /* Get parameters partitions */
417 smu_fetch_param_partitions();
419 smu
= of_find_node_by_type(NULL
, "smu");
423 /* Look for sensors subdir */
425 (sensors
= of_get_next_child(smu
, sensors
)) != NULL
;)
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");