[PATCH] Make sure to always check upper bits of tv_nsec in timespec_valid.
[wandboard.git] / drivers / macintosh / windfarm_smu_sensors.c
blob1a00d9c75a233cb267c55863cf0c90b4207bf35b
1 /*
2 * Windfarm PowerMac thermal control. SMU based sensors
4 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
7 * Released under the term of the GNU GPL v2.
8 */
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/wait.h>
17 #include <linux/completion.h>
18 #include <asm/prom.h>
19 #include <asm/machdep.h>
20 #include <asm/io.h>
21 #include <asm/system.h>
22 #include <asm/sections.h>
23 #include <asm/smu.h>
25 #include "windfarm.h"
27 #define VERSION "0.2"
29 #undef DEBUG
31 #ifdef DEBUG
32 #define DBG(args...) printk(args)
33 #else
34 #define DBG(args...) do { } while(0)
35 #endif
38 * Various SMU "partitions" calibration objects for which we
39 * keep pointers here for use by bits & pieces of the driver
41 static struct smu_sdbp_cpuvcp *cpuvcp;
42 static int cpuvcp_version;
43 static struct smu_sdbp_cpudiode *cpudiode;
44 static struct smu_sdbp_slotspow *slotspow;
45 static u8 *debugswitches;
48 * SMU basic sensors objects
51 static LIST_HEAD(smu_ads);
53 struct smu_ad_sensor {
54 struct list_head link;
55 u32 reg; /* index in SMU */
56 struct wf_sensor sens;
58 #define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
60 static void smu_ads_release(struct wf_sensor *sr)
62 struct smu_ad_sensor *ads = to_smu_ads(sr);
64 kfree(ads);
67 static int smu_read_adc(u8 id, s32 *value)
69 struct smu_simple_cmd cmd;
70 DECLARE_COMPLETION(comp);
71 int rc;
73 rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1,
74 smu_done_complete, &comp, id);
75 if (rc)
76 return rc;
77 wait_for_completion(&comp);
78 if (cmd.cmd.status != 0)
79 return cmd.cmd.status;
80 if (cmd.cmd.reply_len != 2) {
81 printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n",
82 id, cmd.cmd.reply_len);
83 return -EIO;
85 *value = *((u16 *)cmd.buffer);
86 return 0;
89 static int smu_cputemp_get(struct wf_sensor *sr, s32 *value)
91 struct smu_ad_sensor *ads = to_smu_ads(sr);
92 int rc;
93 s32 val;
94 s64 scaled;
96 rc = smu_read_adc(ads->reg, &val);
97 if (rc) {
98 printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n",
99 rc);
100 return rc;
103 /* Ok, we have to scale & adjust, taking units into account */
104 scaled = (s64)(((u64)val) * (u64)cpudiode->m_value);
105 scaled >>= 3;
106 scaled += ((s64)cpudiode->b_value) << 9;
107 *value = (s32)(scaled << 1);
109 return 0;
112 static int smu_cpuamp_get(struct wf_sensor *sr, s32 *value)
114 struct smu_ad_sensor *ads = to_smu_ads(sr);
115 s32 val, scaled;
116 int rc;
118 rc = smu_read_adc(ads->reg, &val);
119 if (rc) {
120 printk(KERN_ERR "windfarm: read CPU current failed, err %d\n",
121 rc);
122 return rc;
125 /* Ok, we have to scale & adjust, taking units into account */
126 scaled = (s32)(val * (u32)cpuvcp->curr_scale);
127 scaled += (s32)cpuvcp->curr_offset;
128 *value = scaled << 4;
130 return 0;
133 static int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value)
135 struct smu_ad_sensor *ads = to_smu_ads(sr);
136 s32 val, scaled;
137 int rc;
139 rc = smu_read_adc(ads->reg, &val);
140 if (rc) {
141 printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n",
142 rc);
143 return rc;
146 /* Ok, we have to scale & adjust, taking units into account */
147 scaled = (s32)(val * (u32)cpuvcp->volt_scale);
148 scaled += (s32)cpuvcp->volt_offset;
149 *value = scaled << 4;
151 return 0;
154 static int smu_slotspow_get(struct wf_sensor *sr, s32 *value)
156 struct smu_ad_sensor *ads = to_smu_ads(sr);
157 s32 val, scaled;
158 int rc;
160 rc = smu_read_adc(ads->reg, &val);
161 if (rc) {
162 printk(KERN_ERR "windfarm: read slots power failed, err %d\n",
163 rc);
164 return rc;
167 /* Ok, we have to scale & adjust, taking units into account */
168 scaled = (s32)(val * (u32)slotspow->pow_scale);
169 scaled += (s32)slotspow->pow_offset;
170 *value = scaled << 4;
172 return 0;
176 static struct wf_sensor_ops smu_cputemp_ops = {
177 .get_value = smu_cputemp_get,
178 .release = smu_ads_release,
179 .owner = THIS_MODULE,
181 static struct wf_sensor_ops smu_cpuamp_ops = {
182 .get_value = smu_cpuamp_get,
183 .release = smu_ads_release,
184 .owner = THIS_MODULE,
186 static struct wf_sensor_ops smu_cpuvolt_ops = {
187 .get_value = smu_cpuvolt_get,
188 .release = smu_ads_release,
189 .owner = THIS_MODULE,
191 static struct wf_sensor_ops smu_slotspow_ops = {
192 .get_value = smu_slotspow_get,
193 .release = smu_ads_release,
194 .owner = THIS_MODULE,
198 static struct smu_ad_sensor *smu_ads_create(struct device_node *node)
200 struct smu_ad_sensor *ads;
201 char *c, *l;
202 u32 *v;
204 ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL);
205 if (ads == NULL)
206 return NULL;
207 c = (char *)get_property(node, "device_type", NULL);
208 l = (char *)get_property(node, "location", NULL);
209 if (c == NULL || l == NULL)
210 goto fail;
212 /* We currently pick the sensors based on the OF name and location
213 * properties, while Darwin uses the sensor-id's.
214 * The problem with the IDs is that they are model specific while it
215 * looks like apple has been doing a reasonably good job at keeping
216 * the names and locations consistents so I'll stick with the names
217 * and locations for now.
219 if (!strcmp(c, "temp-sensor") &&
220 !strcmp(l, "CPU T-Diode")) {
221 ads->sens.ops = &smu_cputemp_ops;
222 ads->sens.name = "cpu-temp";
223 } else if (!strcmp(c, "current-sensor") &&
224 !strcmp(l, "CPU Current")) {
225 ads->sens.ops = &smu_cpuamp_ops;
226 ads->sens.name = "cpu-current";
227 } else if (!strcmp(c, "voltage-sensor") &&
228 !strcmp(l, "CPU Voltage")) {
229 ads->sens.ops = &smu_cpuvolt_ops;
230 ads->sens.name = "cpu-voltage";
231 } else if (!strcmp(c, "power-sensor") &&
232 !strcmp(l, "Slots Power")) {
233 ads->sens.ops = &smu_slotspow_ops;
234 ads->sens.name = "slots-power";
235 if (slotspow == NULL) {
236 DBG("wf: slotspow partition (%02x) not found\n",
237 SMU_SDB_SLOTSPOW_ID);
238 goto fail;
240 } else
241 goto fail;
243 v = (u32 *)get_property(node, "reg", NULL);
244 if (v == NULL)
245 goto fail;
246 ads->reg = *v;
248 if (wf_register_sensor(&ads->sens))
249 goto fail;
250 return ads;
251 fail:
252 kfree(ads);
253 return NULL;
257 * SMU Power combo sensor object
260 struct smu_cpu_power_sensor {
261 struct list_head link;
262 struct wf_sensor *volts;
263 struct wf_sensor *amps;
264 int fake_volts : 1;
265 int quadratic : 1;
266 struct wf_sensor sens;
268 #define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens)
270 static struct smu_cpu_power_sensor *smu_cpu_power;
272 static void smu_cpu_power_release(struct wf_sensor *sr)
274 struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
276 if (pow->volts)
277 wf_put_sensor(pow->volts);
278 if (pow->amps)
279 wf_put_sensor(pow->amps);
280 kfree(pow);
283 static int smu_cpu_power_get(struct wf_sensor *sr, s32 *value)
285 struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
286 s32 volts, amps, power;
287 u64 tmps, tmpa, tmpb;
288 int rc;
290 rc = pow->amps->ops->get_value(pow->amps, &amps);
291 if (rc)
292 return rc;
294 if (pow->fake_volts) {
295 *value = amps * 12 - 0x30000;
296 return 0;
299 rc = pow->volts->ops->get_value(pow->volts, &volts);
300 if (rc)
301 return rc;
303 power = (s32)((((u64)volts) * ((u64)amps)) >> 16);
304 if (!pow->quadratic) {
305 *value = power;
306 return 0;
308 tmps = (((u64)power) * ((u64)power)) >> 16;
309 tmpa = ((u64)cpuvcp->power_quads[0]) * tmps;
310 tmpb = ((u64)cpuvcp->power_quads[1]) * ((u64)power);
311 *value = (tmpa >> 28) + (tmpb >> 28) + (cpuvcp->power_quads[2] >> 12);
313 return 0;
316 static struct wf_sensor_ops smu_cpu_power_ops = {
317 .get_value = smu_cpu_power_get,
318 .release = smu_cpu_power_release,
319 .owner = THIS_MODULE,
323 static struct smu_cpu_power_sensor *
324 smu_cpu_power_create(struct wf_sensor *volts, struct wf_sensor *amps)
326 struct smu_cpu_power_sensor *pow;
328 pow = kmalloc(sizeof(struct smu_cpu_power_sensor), GFP_KERNEL);
329 if (pow == NULL)
330 return NULL;
331 pow->sens.ops = &smu_cpu_power_ops;
332 pow->sens.name = "cpu-power";
334 wf_get_sensor(volts);
335 pow->volts = volts;
336 wf_get_sensor(amps);
337 pow->amps = amps;
339 /* Some early machines need a faked voltage */
340 if (debugswitches && ((*debugswitches) & 0x80)) {
341 printk(KERN_INFO "windfarm: CPU Power sensor using faked"
342 " voltage !\n");
343 pow->fake_volts = 1;
344 } else
345 pow->fake_volts = 0;
347 /* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now,
348 * I yet have to figure out what's up with 8,2 and will have to
349 * adjust for later, unless we can 100% trust the SDB partition...
351 if ((machine_is_compatible("PowerMac8,1") ||
352 machine_is_compatible("PowerMac8,2") ||
353 machine_is_compatible("PowerMac9,1")) &&
354 cpuvcp_version >= 2) {
355 pow->quadratic = 1;
356 DBG("windfarm: CPU Power using quadratic transform\n");
357 } else
358 pow->quadratic = 0;
360 if (wf_register_sensor(&pow->sens))
361 goto fail;
362 return pow;
363 fail:
364 kfree(pow);
365 return NULL;
368 static int smu_fetch_param_partitions(void)
370 struct smu_sdbp_header *hdr;
372 /* Get CPU voltage/current/power calibration data */
373 hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL);
374 if (hdr == NULL) {
375 DBG("wf: cpuvcp partition (%02x) not found\n",
376 SMU_SDB_CPUVCP_ID);
377 return -ENODEV;
379 cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1];
380 /* Keep version around */
381 cpuvcp_version = hdr->version;
383 /* Get CPU diode calibration data */
384 hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL);
385 if (hdr == NULL) {
386 DBG("wf: cpudiode partition (%02x) not found\n",
387 SMU_SDB_CPUDIODE_ID);
388 return -ENODEV;
390 cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1];
392 /* Get slots power calibration data if any */
393 hdr = smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID, NULL);
394 if (hdr != NULL)
395 slotspow = (struct smu_sdbp_slotspow *)&hdr[1];
397 /* Get debug switches if any */
398 hdr = smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID, NULL);
399 if (hdr != NULL)
400 debugswitches = (u8 *)&hdr[1];
402 return 0;
405 static int __init smu_sensors_init(void)
407 struct device_node *smu, *sensors, *s;
408 struct smu_ad_sensor *volt_sensor = NULL, *curr_sensor = NULL;
409 int rc;
411 if (!smu_present())
412 return -ENODEV;
414 /* Get parameters partitions */
415 rc = smu_fetch_param_partitions();
416 if (rc)
417 return rc;
419 smu = of_find_node_by_type(NULL, "smu");
420 if (smu == NULL)
421 return -ENODEV;
423 /* Look for sensors subdir */
424 for (sensors = NULL;
425 (sensors = of_get_next_child(smu, sensors)) != NULL;)
426 if (!strcmp(sensors->name, "sensors"))
427 break;
429 of_node_put(smu);
431 /* Create basic sensors */
432 for (s = NULL;
433 sensors && (s = of_get_next_child(sensors, s)) != NULL;) {
434 struct smu_ad_sensor *ads;
436 ads = smu_ads_create(s);
437 if (ads == NULL)
438 continue;
439 list_add(&ads->link, &smu_ads);
440 /* keep track of cpu voltage & current */
441 if (!strcmp(ads->sens.name, "cpu-voltage"))
442 volt_sensor = ads;
443 else if (!strcmp(ads->sens.name, "cpu-current"))
444 curr_sensor = ads;
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,
452 &curr_sensor->sens);
454 return 0;
457 static void __exit smu_sensors_exit(void)
459 struct smu_ad_sensor *ads;
461 /* dispose of power sensor */
462 if (smu_cpu_power)
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");