windfarm_smu_sensors.c 11 KB

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