via-cputemp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/hwmon-sysfs.h>
  29. #include <linux/err.h>
  30. #include <linux/mutex.h>
  31. #include <linux/list.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/cpu.h>
  34. #include <asm/msr.h>
  35. #include <asm/processor.h>
  36. #define DRVNAME "via_cputemp"
  37. enum { SHOW_TEMP, SHOW_LABEL, SHOW_NAME };
  38. /*
  39. * Functions declaration
  40. */
  41. struct via_cputemp_data {
  42. struct device *hwmon_dev;
  43. const char *name;
  44. u32 id;
  45. u32 msr;
  46. };
  47. /*
  48. * Sysfs stuff
  49. */
  50. static ssize_t show_name(struct device *dev, struct device_attribute
  51. *devattr, char *buf)
  52. {
  53. int ret;
  54. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  55. struct via_cputemp_data *data = dev_get_drvdata(dev);
  56. if (attr->index == SHOW_NAME)
  57. ret = sprintf(buf, "%s\n", data->name);
  58. else /* show label */
  59. ret = sprintf(buf, "Core %d\n", data->id);
  60. return ret;
  61. }
  62. static ssize_t show_temp(struct device *dev,
  63. struct device_attribute *devattr, char *buf)
  64. {
  65. struct via_cputemp_data *data = dev_get_drvdata(dev);
  66. u32 eax, edx;
  67. int err;
  68. err = rdmsr_safe_on_cpu(data->id, data->msr, &eax, &edx);
  69. if (err)
  70. return -EAGAIN;
  71. return sprintf(buf, "%lu\n", ((unsigned long)eax & 0xffffff) * 1000);
  72. }
  73. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
  74. SHOW_TEMP);
  75. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
  76. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
  77. static struct attribute *via_cputemp_attributes[] = {
  78. &sensor_dev_attr_name.dev_attr.attr,
  79. &sensor_dev_attr_temp1_label.dev_attr.attr,
  80. &sensor_dev_attr_temp1_input.dev_attr.attr,
  81. NULL
  82. };
  83. static const struct attribute_group via_cputemp_group = {
  84. .attrs = via_cputemp_attributes,
  85. };
  86. static int __devinit via_cputemp_probe(struct platform_device *pdev)
  87. {
  88. struct via_cputemp_data *data;
  89. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  90. int err;
  91. u32 eax, edx;
  92. data = kzalloc(sizeof(struct via_cputemp_data), GFP_KERNEL);
  93. if (!data) {
  94. err = -ENOMEM;
  95. dev_err(&pdev->dev, "Out of memory\n");
  96. goto exit;
  97. }
  98. data->id = pdev->id;
  99. data->name = "via_cputemp";
  100. switch (c->x86_model) {
  101. case 0xA:
  102. /* C7 A */
  103. case 0xD:
  104. /* C7 D */
  105. data->msr = 0x1169;
  106. break;
  107. case 0xF:
  108. /* Nano */
  109. data->msr = 0x1423;
  110. break;
  111. default:
  112. err = -ENODEV;
  113. goto exit_free;
  114. }
  115. /* test if we can access the TEMPERATURE MSR */
  116. err = rdmsr_safe_on_cpu(data->id, data->msr, &eax, &edx);
  117. if (err) {
  118. dev_err(&pdev->dev,
  119. "Unable to access TEMPERATURE MSR, giving up\n");
  120. goto exit_free;
  121. }
  122. platform_set_drvdata(pdev, data);
  123. err = sysfs_create_group(&pdev->dev.kobj, &via_cputemp_group);
  124. if (err)
  125. goto exit_free;
  126. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  127. if (IS_ERR(data->hwmon_dev)) {
  128. err = PTR_ERR(data->hwmon_dev);
  129. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  130. err);
  131. goto exit_remove;
  132. }
  133. return 0;
  134. exit_remove:
  135. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  136. exit_free:
  137. platform_set_drvdata(pdev, NULL);
  138. kfree(data);
  139. exit:
  140. return err;
  141. }
  142. static int __devexit via_cputemp_remove(struct platform_device *pdev)
  143. {
  144. struct via_cputemp_data *data = platform_get_drvdata(pdev);
  145. hwmon_device_unregister(data->hwmon_dev);
  146. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  147. platform_set_drvdata(pdev, NULL);
  148. kfree(data);
  149. return 0;
  150. }
  151. static struct platform_driver via_cputemp_driver = {
  152. .driver = {
  153. .owner = THIS_MODULE,
  154. .name = DRVNAME,
  155. },
  156. .probe = via_cputemp_probe,
  157. .remove = __devexit_p(via_cputemp_remove),
  158. };
  159. struct pdev_entry {
  160. struct list_head list;
  161. struct platform_device *pdev;
  162. unsigned int cpu;
  163. };
  164. static LIST_HEAD(pdev_list);
  165. static DEFINE_MUTEX(pdev_list_mutex);
  166. static int __cpuinit via_cputemp_device_add(unsigned int cpu)
  167. {
  168. int err;
  169. struct platform_device *pdev;
  170. struct pdev_entry *pdev_entry;
  171. pdev = platform_device_alloc(DRVNAME, cpu);
  172. if (!pdev) {
  173. err = -ENOMEM;
  174. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  175. goto exit;
  176. }
  177. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  178. if (!pdev_entry) {
  179. err = -ENOMEM;
  180. goto exit_device_put;
  181. }
  182. err = platform_device_add(pdev);
  183. if (err) {
  184. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  185. 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, *n;
  204. mutex_lock(&pdev_list_mutex);
  205. list_for_each_entry_safe(p, n, &pdev_list, list) {
  206. if (p->cpu == cpu) {
  207. platform_device_unregister(p->pdev);
  208. list_del(&p->list);
  209. kfree(p);
  210. }
  211. }
  212. mutex_unlock(&pdev_list_mutex);
  213. }
  214. static int __cpuinit via_cputemp_cpu_callback(struct notifier_block *nfb,
  215. unsigned long action, void *hcpu)
  216. {
  217. unsigned int cpu = (unsigned long) hcpu;
  218. switch (action) {
  219. case CPU_ONLINE:
  220. case CPU_DOWN_FAILED:
  221. via_cputemp_device_add(cpu);
  222. break;
  223. case CPU_DOWN_PREPARE:
  224. via_cputemp_device_remove(cpu);
  225. break;
  226. }
  227. return NOTIFY_OK;
  228. }
  229. static struct notifier_block via_cputemp_cpu_notifier __refdata = {
  230. .notifier_call = via_cputemp_cpu_callback,
  231. };
  232. static int __init via_cputemp_init(void)
  233. {
  234. int i, err;
  235. struct pdev_entry *p, *n;
  236. if (cpu_data(0).x86_vendor != X86_VENDOR_CENTAUR) {
  237. printk(KERN_DEBUG DRVNAME ": Not a VIA CPU\n");
  238. err = -ENODEV;
  239. goto exit;
  240. }
  241. err = platform_driver_register(&via_cputemp_driver);
  242. if (err)
  243. goto exit;
  244. for_each_online_cpu(i) {
  245. struct cpuinfo_x86 *c = &cpu_data(i);
  246. if (c->x86 != 6)
  247. continue;
  248. if (c->x86_model < 0x0a)
  249. continue;
  250. if (c->x86_model > 0x0f) {
  251. printk(KERN_WARNING DRVNAME ": Unknown CPU "
  252. "model 0x%x\n", c->x86_model);
  253. continue;
  254. }
  255. err = via_cputemp_device_add(i);
  256. if (err)
  257. goto exit_devices_unreg;
  258. }
  259. if (list_empty(&pdev_list)) {
  260. err = -ENODEV;
  261. goto exit_driver_unreg;
  262. }
  263. register_hotcpu_notifier(&via_cputemp_cpu_notifier);
  264. return 0;
  265. exit_devices_unreg:
  266. mutex_lock(&pdev_list_mutex);
  267. list_for_each_entry_safe(p, n, &pdev_list, list) {
  268. platform_device_unregister(p->pdev);
  269. list_del(&p->list);
  270. kfree(p);
  271. }
  272. mutex_unlock(&pdev_list_mutex);
  273. exit_driver_unreg:
  274. platform_driver_unregister(&via_cputemp_driver);
  275. exit:
  276. return err;
  277. }
  278. static void __exit via_cputemp_exit(void)
  279. {
  280. struct pdev_entry *p, *n;
  281. unregister_hotcpu_notifier(&via_cputemp_cpu_notifier);
  282. mutex_lock(&pdev_list_mutex);
  283. list_for_each_entry_safe(p, n, &pdev_list, list) {
  284. platform_device_unregister(p->pdev);
  285. list_del(&p->list);
  286. kfree(p);
  287. }
  288. mutex_unlock(&pdev_list_mutex);
  289. platform_driver_unregister(&via_cputemp_driver);
  290. }
  291. MODULE_AUTHOR("Harald Welte <HaraldWelte@viatech.com>");
  292. MODULE_DESCRIPTION("VIA CPU temperature monitor");
  293. MODULE_LICENSE("GPL");
  294. module_init(via_cputemp_init)
  295. module_exit(via_cputemp_exit)