windfarm_smu_sensors.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Windfarm PowerMac thermal control. SMU based sensors
  3. *
  4. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  5. * <benh@kernel.crashing.org>
  6. *
  7. * Released under the term of the GNU GPL v2.
  8. */
  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>
  17. #include <asm/prom.h>
  18. #include <asm/machdep.h>
  19. #include <asm/io.h>
  20. #include <asm/system.h>
  21. #include <asm/sections.h>
  22. #include <asm/smu.h>
  23. #include "windfarm.h"
  24. #define VERSION "0.2"
  25. #undef DEBUG
  26. #ifdef DEBUG
  27. #define DBG(args...) printk(args)
  28. #else
  29. #define DBG(args...) do { } while(0)
  30. #endif
  31. /*
  32. * Various SMU "partitions" calibration objects for which we
  33. * keep pointers here for use by bits & pieces of the driver
  34. */
  35. static struct smu_sdbp_cpuvcp *cpuvcp;
  36. static int cpuvcp_version;
  37. static struct smu_sdbp_cpudiode *cpudiode;
  38. static struct smu_sdbp_slotspow *slotspow;
  39. static u8 *debugswitches;
  40. /*
  41. * SMU basic sensors objects
  42. */
  43. static LIST_HEAD(smu_ads);
  44. struct smu_ad_sensor {
  45. struct list_head link;
  46. u32 reg; /* index in SMU */
  47. struct wf_sensor sens;
  48. };
  49. #define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
  50. static void smu_ads_release(struct wf_sensor *sr)
  51. {
  52. struct smu_ad_sensor *ads = to_smu_ads(sr);
  53. kfree(ads);
  54. }
  55. static int smu_read_adc(u8 id, s32 *value)
  56. {
  57. struct smu_simple_cmd cmd;
  58. DECLARE_COMPLETION(comp);
  59. int rc;
  60. rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1,
  61. smu_done_complete, &comp, id);
  62. if (rc)
  63. return rc;
  64. wait_for_completion(&comp);
  65. if (cmd.cmd.status != 0)
  66. return cmd.cmd.status;
  67. if (cmd.cmd.reply_len != 2) {
  68. printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n",
  69. id, cmd.cmd.reply_len);
  70. return -EIO;
  71. }
  72. *value = *((u16 *)cmd.buffer);
  73. return 0;
  74. }
  75. static int smu_cputemp_get(struct wf_sensor *sr, s32 *value)
  76. {
  77. struct smu_ad_sensor *ads = to_smu_ads(sr);
  78. int rc;
  79. s32 val;
  80. s64 scaled;
  81. rc = smu_read_adc(ads->reg, &val);
  82. if (rc) {
  83. printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n",
  84. rc);
  85. return rc;
  86. }
  87. /* Ok, we have to scale & adjust, taking units into account */
  88. scaled = (s64)(((u64)val) * (u64)cpudiode->m_value);
  89. scaled >>= 3;
  90. scaled += ((s64)cpudiode->b_value) << 9;
  91. *value = (s32)(scaled << 1);
  92. return 0;
  93. }
  94. static int smu_cpuamp_get(struct wf_sensor *sr, s32 *value)
  95. {
  96. struct smu_ad_sensor *ads = to_smu_ads(sr);
  97. s32 val, scaled;
  98. int rc;
  99. rc = smu_read_adc(ads->reg, &val);
  100. if (rc) {
  101. printk(KERN_ERR "windfarm: read CPU current failed, err %d\n",
  102. rc);
  103. return rc;
  104. }
  105. /* Ok, we have to scale & adjust, taking units into account */
  106. scaled = (s32)(val * (u32)cpuvcp->curr_scale);
  107. scaled += (s32)cpuvcp->curr_offset;
  108. *value = scaled << 4;
  109. return 0;
  110. }
  111. static int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value)
  112. {
  113. struct smu_ad_sensor *ads = to_smu_ads(sr);
  114. s32 val, scaled;
  115. int rc;
  116. rc = smu_read_adc(ads->reg, &val);
  117. if (rc) {
  118. printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n",
  119. rc);
  120. return rc;
  121. }
  122. /* Ok, we have to scale & adjust, taking units into account */
  123. scaled = (s32)(val * (u32)cpuvcp->volt_scale);
  124. scaled += (s32)cpuvcp->volt_offset;
  125. *value = scaled << 4;
  126. return 0;
  127. }
  128. static int smu_slotspow_get(struct wf_sensor *sr, s32 *value)
  129. {
  130. struct smu_ad_sensor *ads = to_smu_ads(sr);
  131. s32 val, scaled;
  132. int rc;
  133. rc = smu_read_adc(ads->reg, &val);
  134. if (rc) {
  135. printk(KERN_ERR "windfarm: read slots power failed, err %d\n",
  136. rc);
  137. return rc;
  138. }
  139. /* Ok, we have to scale & adjust, taking units into account */
  140. scaled = (s32)(val * (u32)slotspow->pow_scale);
  141. scaled += (s32)slotspow->pow_offset;
  142. *value = scaled << 4;
  143. return 0;
  144. }
  145. static struct wf_sensor_ops smu_cputemp_ops = {
  146. .get_value = smu_cputemp_get,
  147. .release = smu_ads_release,
  148. .owner = THIS_MODULE,
  149. };
  150. static struct wf_sensor_ops smu_cpuamp_ops = {
  151. .get_value = smu_cpuamp_get,
  152. .release = smu_ads_release,
  153. .owner = THIS_MODULE,
  154. };
  155. static struct wf_sensor_ops smu_cpuvolt_ops = {
  156. .get_value = smu_cpuvolt_get,
  157. .release = smu_ads_release,
  158. .owner = THIS_MODULE,
  159. };
  160. static struct wf_sensor_ops smu_slotspow_ops = {
  161. .get_value = smu_slotspow_get,
  162. .release = smu_ads_release,
  163. .owner = THIS_MODULE,
  164. };
  165. static struct smu_ad_sensor *smu_ads_create(struct device_node *node)
  166. {
  167. struct smu_ad_sensor *ads;
  168. char *c, *l;
  169. u32 *v;
  170. ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL);
  171. if (ads == NULL)
  172. return NULL;
  173. c = (char *)get_property(node, "device_type", NULL);
  174. l = (char *)get_property(node, "location", NULL);
  175. if (c == NULL || l == NULL)
  176. goto fail;
  177. /* We currently pick the sensors based on the OF name and location
  178. * properties, while Darwin uses the sensor-id's.
  179. * The problem with the IDs is that they are model specific while it
  180. * looks like apple has been doing a reasonably good job at keeping
  181. * the names and locations consistents so I'll stick with the names
  182. * and locations for now.
  183. */
  184. if (!strcmp(c, "temp-sensor") &&
  185. !strcmp(l, "CPU T-Diode")) {
  186. ads->sens.ops = &smu_cputemp_ops;
  187. ads->sens.name = "cpu-temp";
  188. } else if (!strcmp(c, "current-sensor") &&
  189. !strcmp(l, "CPU Current")) {
  190. ads->sens.ops = &smu_cpuamp_ops;
  191. ads->sens.name = "cpu-current";
  192. } else if (!strcmp(c, "voltage-sensor") &&
  193. !strcmp(l, "CPU Voltage")) {
  194. ads->sens.ops = &smu_cpuvolt_ops;
  195. ads->sens.name = "cpu-voltage";
  196. } else if (!strcmp(c, "power-sensor") &&
  197. !strcmp(l, "Slots Power")) {
  198. ads->sens.ops = &smu_slotspow_ops;
  199. ads->sens.name = "slots-power";
  200. if (slotspow == NULL) {
  201. DBG("wf: slotspow partition (%02x) not found\n",
  202. SMU_SDB_SLOTSPOW_ID);
  203. goto fail;
  204. }
  205. } else
  206. goto fail;
  207. v = (u32 *)get_property(node, "reg", NULL);
  208. if (v == NULL)
  209. goto fail;
  210. ads->reg = *v;
  211. if (wf_register_sensor(&ads->sens))
  212. goto fail;
  213. return ads;
  214. fail:
  215. kfree(ads);
  216. return NULL;
  217. }
  218. /*
  219. * SMU Power combo sensor object
  220. */
  221. struct smu_cpu_power_sensor {
  222. struct list_head link;
  223. struct wf_sensor *volts;
  224. struct wf_sensor *amps;
  225. int fake_volts : 1;
  226. int quadratic : 1;
  227. struct wf_sensor sens;
  228. };
  229. #define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens)
  230. static struct smu_cpu_power_sensor *smu_cpu_power;
  231. static void smu_cpu_power_release(struct wf_sensor *sr)
  232. {
  233. struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
  234. if (pow->volts)
  235. wf_put_sensor(pow->volts);
  236. if (pow->amps)
  237. wf_put_sensor(pow->amps);
  238. kfree(pow);
  239. }
  240. static int smu_cpu_power_get(struct wf_sensor *sr, s32 *value)
  241. {
  242. struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
  243. s32 volts, amps, power;
  244. u64 tmps, tmpa, tmpb;
  245. int rc;
  246. rc = pow->amps->ops->get_value(pow->amps, &amps);
  247. if (rc)
  248. return rc;
  249. if (pow->fake_volts) {
  250. *value = amps * 12 - 0x30000;
  251. return 0;
  252. }
  253. rc = pow->volts->ops->get_value(pow->volts, &volts);
  254. if (rc)
  255. return rc;
  256. power = (s32)((((u64)volts) * ((u64)amps)) >> 16);
  257. if (!pow->quadratic) {
  258. *value = power;
  259. return 0;
  260. }
  261. tmps = (((u64)power) * ((u64)power)) >> 16;
  262. tmpa = ((u64)cpuvcp->power_quads[0]) * tmps;
  263. tmpb = ((u64)cpuvcp->power_quads[1]) * ((u64)power);
  264. *value = (tmpa >> 28) + (tmpb >> 28) + (cpuvcp->power_quads[2] >> 12);
  265. return 0;
  266. }
  267. static struct wf_sensor_ops smu_cpu_power_ops = {
  268. .get_value = smu_cpu_power_get,
  269. .release = smu_cpu_power_release,
  270. .owner = THIS_MODULE,
  271. };
  272. static struct smu_cpu_power_sensor *
  273. smu_cpu_power_create(struct wf_sensor *volts, struct wf_sensor *amps)
  274. {
  275. struct smu_cpu_power_sensor *pow;
  276. pow = kmalloc(sizeof(struct smu_cpu_power_sensor), GFP_KERNEL);
  277. if (pow == NULL)
  278. return NULL;
  279. pow->sens.ops = &smu_cpu_power_ops;
  280. pow->sens.name = "cpu-power";
  281. wf_get_sensor(volts);
  282. pow->volts = volts;
  283. wf_get_sensor(amps);
  284. pow->amps = amps;
  285. /* Some early machines need a faked voltage */
  286. if (debugswitches && ((*debugswitches) & 0x80)) {
  287. printk(KERN_INFO "windfarm: CPU Power sensor using faked"
  288. " voltage !\n");
  289. pow->fake_volts = 1;
  290. } else
  291. pow->fake_volts = 0;
  292. /* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now,
  293. * I yet have to figure out what's up with 8,2 and will have to
  294. * adjust for later, unless we can 100% trust the SDB partition...
  295. */
  296. if ((machine_is_compatible("PowerMac8,1") ||
  297. machine_is_compatible("PowerMac8,2") ||
  298. machine_is_compatible("PowerMac9,1")) &&
  299. cpuvcp_version >= 2) {
  300. pow->quadratic = 1;
  301. DBG("windfarm: CPU Power using quadratic transform\n");
  302. } else
  303. pow->quadratic = 0;
  304. if (wf_register_sensor(&pow->sens))
  305. goto fail;
  306. return pow;
  307. fail:
  308. kfree(pow);
  309. return NULL;
  310. }
  311. static int smu_fetch_param_partitions(void)
  312. {
  313. struct smu_sdbp_header *hdr;
  314. /* Get CPU voltage/current/power calibration data */
  315. hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL);
  316. if (hdr == NULL) {
  317. DBG("wf: cpuvcp partition (%02x) not found\n",
  318. SMU_SDB_CPUVCP_ID);
  319. return -ENODEV;
  320. }
  321. cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1];
  322. /* Keep version around */
  323. cpuvcp_version = hdr->version;
  324. /* Get CPU diode calibration data */
  325. hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL);
  326. if (hdr == NULL) {
  327. DBG("wf: cpudiode partition (%02x) not found\n",
  328. SMU_SDB_CPUDIODE_ID);
  329. return -ENODEV;
  330. }
  331. cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1];
  332. /* Get slots power calibration data if any */
  333. hdr = smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID, NULL);
  334. if (hdr != NULL)
  335. slotspow = (struct smu_sdbp_slotspow *)&hdr[1];
  336. /* Get debug switches if any */
  337. hdr = smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID, NULL);
  338. if (hdr != NULL)
  339. debugswitches = (u8 *)&hdr[1];
  340. return 0;
  341. }
  342. static int __init smu_sensors_init(void)
  343. {
  344. struct device_node *smu, *sensors, *s;
  345. struct smu_ad_sensor *volt_sensor = NULL, *curr_sensor = NULL;
  346. int rc;
  347. if (!smu_present())
  348. return -ENODEV;
  349. /* Get parameters partitions */
  350. rc = smu_fetch_param_partitions();
  351. if (rc)
  352. return rc;
  353. smu = of_find_node_by_type(NULL, "smu");
  354. if (smu == NULL)
  355. return -ENODEV;
  356. /* Look for sensors subdir */
  357. for (sensors = NULL;
  358. (sensors = of_get_next_child(smu, sensors)) != NULL;)
  359. if (!strcmp(sensors->name, "sensors"))
  360. break;
  361. of_node_put(smu);
  362. /* Create basic sensors */
  363. for (s = NULL;
  364. sensors && (s = of_get_next_child(sensors, s)) != NULL;) {
  365. struct smu_ad_sensor *ads;
  366. ads = smu_ads_create(s);
  367. if (ads == NULL)
  368. continue;
  369. list_add(&ads->link, &smu_ads);
  370. /* keep track of cpu voltage & current */
  371. if (!strcmp(ads->sens.name, "cpu-voltage"))
  372. volt_sensor = ads;
  373. else if (!strcmp(ads->sens.name, "cpu-current"))
  374. curr_sensor = ads;
  375. }
  376. of_node_put(sensors);
  377. /* Create CPU power sensor if possible */
  378. if (volt_sensor && curr_sensor)
  379. smu_cpu_power = smu_cpu_power_create(&volt_sensor->sens,
  380. &curr_sensor->sens);
  381. return 0;
  382. }
  383. static void __exit smu_sensors_exit(void)
  384. {
  385. struct smu_ad_sensor *ads;
  386. /* dispose of power sensor */
  387. if (smu_cpu_power)
  388. wf_unregister_sensor(&smu_cpu_power->sens);
  389. /* dispose of basic sensors */
  390. while (!list_empty(&smu_ads)) {
  391. ads = list_entry(smu_ads.next, struct smu_ad_sensor, link);
  392. list_del(&ads->link);
  393. wf_unregister_sensor(&ads->sens);
  394. }
  395. }
  396. module_init(smu_sensors_init);
  397. module_exit(smu_sensors_exit);
  398. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  399. MODULE_DESCRIPTION("SMU sensor objects for PowerMacs thermal control");
  400. MODULE_LICENSE("GPL");