via-cputemp.c 7.9 KB

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