via-cputemp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * via-cputemp.c - Driver for VIA CPU core temperature monitoring
  3. * Copyright (C) 2009 VIA Technologies, Inc.
  4. *
  5. * based on existing coretemp.c, which is
  6. *
  7. * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. #include <linux/list.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/cpu.h>
  35. #include <asm/msr.h>
  36. #include <asm/processor.h>
  37. #define DRVNAME "via_cputemp"
  38. enum { SHOW_TEMP, SHOW_LABEL, SHOW_NAME };
  39. /*
  40. * Functions declaration
  41. */
  42. struct via_cputemp_data {
  43. struct device *hwmon_dev;
  44. const char *name;
  45. u32 id;
  46. u32 msr;
  47. };
  48. /*
  49. * Sysfs stuff
  50. */
  51. static ssize_t show_name(struct device *dev, struct device_attribute
  52. *devattr, char *buf)
  53. {
  54. int ret;
  55. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  56. struct via_cputemp_data *data = dev_get_drvdata(dev);
  57. if (attr->index == SHOW_NAME)
  58. ret = sprintf(buf, "%s\n", data->name);
  59. else /* show label */
  60. ret = sprintf(buf, "Core %d\n", data->id);
  61. return ret;
  62. }
  63. static ssize_t show_temp(struct device *dev,
  64. struct device_attribute *devattr, char *buf)
  65. {
  66. struct via_cputemp_data *data = dev_get_drvdata(dev);
  67. u32 eax, edx;
  68. int err;
  69. err = rdmsr_safe_on_cpu(data->id, data->msr, &eax, &edx);
  70. if (err)
  71. return -EAGAIN;
  72. return sprintf(buf, "%lu\n", ((unsigned long)eax & 0xffffff) * 1000);
  73. }
  74. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
  75. SHOW_TEMP);
  76. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
  77. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
  78. static struct attribute *via_cputemp_attributes[] = {
  79. &sensor_dev_attr_name.dev_attr.attr,
  80. &sensor_dev_attr_temp1_label.dev_attr.attr,
  81. &sensor_dev_attr_temp1_input.dev_attr.attr,
  82. NULL
  83. };
  84. static const struct attribute_group via_cputemp_group = {
  85. .attrs = via_cputemp_attributes,
  86. };
  87. static int __devinit via_cputemp_probe(struct platform_device *pdev)
  88. {
  89. struct via_cputemp_data *data;
  90. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  91. int err;
  92. u32 eax, edx;
  93. data = kzalloc(sizeof(struct via_cputemp_data), GFP_KERNEL);
  94. if (!data) {
  95. err = -ENOMEM;
  96. dev_err(&pdev->dev, "Out of memory\n");
  97. goto exit;
  98. }
  99. data->id = pdev->id;
  100. data->name = "via_cputemp";
  101. switch (c->x86_model) {
  102. case 0xA:
  103. /* C7 A */
  104. case 0xD:
  105. /* C7 D */
  106. data->msr = 0x1169;
  107. break;
  108. case 0xF:
  109. /* Nano */
  110. data->msr = 0x1423;
  111. break;
  112. default:
  113. err = -ENODEV;
  114. goto exit_free;
  115. }
  116. /* test if we can access the TEMPERATURE MSR */
  117. err = rdmsr_safe_on_cpu(data->id, data->msr, &eax, &edx);
  118. if (err) {
  119. dev_err(&pdev->dev,
  120. "Unable to access TEMPERATURE MSR, giving up\n");
  121. goto exit_free;
  122. }
  123. platform_set_drvdata(pdev, data);
  124. err = sysfs_create_group(&pdev->dev.kobj, &via_cputemp_group);
  125. if (err)
  126. goto exit_free;
  127. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  128. if (IS_ERR(data->hwmon_dev)) {
  129. err = PTR_ERR(data->hwmon_dev);
  130. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  131. err);
  132. goto exit_remove;
  133. }
  134. return 0;
  135. exit_remove:
  136. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  137. exit_free:
  138. platform_set_drvdata(pdev, NULL);
  139. kfree(data);
  140. exit:
  141. return err;
  142. }
  143. static int __devexit via_cputemp_remove(struct platform_device *pdev)
  144. {
  145. struct via_cputemp_data *data = platform_get_drvdata(pdev);
  146. hwmon_device_unregister(data->hwmon_dev);
  147. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  148. platform_set_drvdata(pdev, NULL);
  149. kfree(data);
  150. return 0;
  151. }
  152. static struct platform_driver via_cputemp_driver = {
  153. .driver = {
  154. .owner = THIS_MODULE,
  155. .name = DRVNAME,
  156. },
  157. .probe = via_cputemp_probe,
  158. .remove = __devexit_p(via_cputemp_remove),
  159. };
  160. struct pdev_entry {
  161. struct list_head list;
  162. struct platform_device *pdev;
  163. unsigned int cpu;
  164. };
  165. static LIST_HEAD(pdev_list);
  166. static DEFINE_MUTEX(pdev_list_mutex);
  167. static int __cpuinit via_cputemp_device_add(unsigned int cpu)
  168. {
  169. int err;
  170. struct platform_device *pdev;
  171. struct pdev_entry *pdev_entry;
  172. pdev = platform_device_alloc(DRVNAME, cpu);
  173. if (!pdev) {
  174. err = -ENOMEM;
  175. pr_err("Device allocation failed\n");
  176. goto exit;
  177. }
  178. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  179. if (!pdev_entry) {
  180. err = -ENOMEM;
  181. goto exit_device_put;
  182. }
  183. err = platform_device_add(pdev);
  184. if (err) {
  185. pr_err("Device addition failed (%d)\n", err);
  186. goto exit_device_free;
  187. }
  188. pdev_entry->pdev = pdev;
  189. pdev_entry->cpu = cpu;
  190. mutex_lock(&pdev_list_mutex);
  191. list_add_tail(&pdev_entry->list, &pdev_list);
  192. mutex_unlock(&pdev_list_mutex);
  193. return 0;
  194. exit_device_free:
  195. kfree(pdev_entry);
  196. exit_device_put:
  197. platform_device_put(pdev);
  198. exit:
  199. return err;
  200. }
  201. static void __cpuinit via_cputemp_device_remove(unsigned int cpu)
  202. {
  203. struct pdev_entry *p;
  204. mutex_lock(&pdev_list_mutex);
  205. list_for_each_entry(p, &pdev_list, list) {
  206. if (p->cpu == cpu) {
  207. platform_device_unregister(p->pdev);
  208. list_del(&p->list);
  209. mutex_unlock(&pdev_list_mutex);
  210. kfree(p);
  211. return;
  212. }
  213. }
  214. mutex_unlock(&pdev_list_mutex);
  215. }
  216. static int __cpuinit via_cputemp_cpu_callback(struct notifier_block *nfb,
  217. unsigned long action, void *hcpu)
  218. {
  219. unsigned int cpu = (unsigned long) hcpu;
  220. switch (action) {
  221. case CPU_ONLINE:
  222. case CPU_DOWN_FAILED:
  223. via_cputemp_device_add(cpu);
  224. break;
  225. case CPU_DOWN_PREPARE:
  226. via_cputemp_device_remove(cpu);
  227. break;
  228. }
  229. return NOTIFY_OK;
  230. }
  231. static struct notifier_block via_cputemp_cpu_notifier __refdata = {
  232. .notifier_call = via_cputemp_cpu_callback,
  233. };
  234. static int __init via_cputemp_init(void)
  235. {
  236. int i, err;
  237. if (cpu_data(0).x86_vendor != X86_VENDOR_CENTAUR) {
  238. printk(KERN_DEBUG DRVNAME ": Not a VIA CPU\n");
  239. err = -ENODEV;
  240. goto exit;
  241. }
  242. err = platform_driver_register(&via_cputemp_driver);
  243. if (err)
  244. goto exit;
  245. for_each_online_cpu(i) {
  246. struct cpuinfo_x86 *c = &cpu_data(i);
  247. if (c->x86 != 6)
  248. continue;
  249. if (c->x86_model < 0x0a)
  250. continue;
  251. if (c->x86_model > 0x0f) {
  252. pr_warn("Unknown CPU model 0x%x\n", c->x86_model);
  253. continue;
  254. }
  255. via_cputemp_device_add(i);
  256. }
  257. #ifndef CONFIG_HOTPLUG_CPU
  258. if (list_empty(&pdev_list)) {
  259. err = -ENODEV;
  260. goto exit_driver_unreg;
  261. }
  262. #endif
  263. register_hotcpu_notifier(&via_cputemp_cpu_notifier);
  264. return 0;
  265. #ifndef CONFIG_HOTPLUG_CPU
  266. exit_driver_unreg:
  267. platform_driver_unregister(&via_cputemp_driver);
  268. #endif
  269. exit:
  270. return err;
  271. }
  272. static void __exit via_cputemp_exit(void)
  273. {
  274. struct pdev_entry *p, *n;
  275. unregister_hotcpu_notifier(&via_cputemp_cpu_notifier);
  276. mutex_lock(&pdev_list_mutex);
  277. list_for_each_entry_safe(p, n, &pdev_list, list) {
  278. platform_device_unregister(p->pdev);
  279. list_del(&p->list);
  280. kfree(p);
  281. }
  282. mutex_unlock(&pdev_list_mutex);
  283. platform_driver_unregister(&via_cputemp_driver);
  284. }
  285. MODULE_AUTHOR("Harald Welte <HaraldWelte@viatech.com>");
  286. MODULE_DESCRIPTION("VIA CPU temperature monitor");
  287. MODULE_LICENSE("GPL");
  288. module_init(via_cputemp_init)
  289. module_exit(via_cputemp_exit)