k8temp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * k8temp.c - Linux kernel module for hardware monitoring
  3. *
  4. * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
  5. *
  6. * Inspired from the w83785 and amd756 drivers.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  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/pci.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/err.h>
  32. #include <linux/mutex.h>
  33. #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
  34. #define REG_TEMP 0xe4
  35. #define SEL_PLACE 0x40
  36. #define SEL_CORE 0x04
  37. struct k8temp_data {
  38. struct device *hwmon_dev;
  39. struct mutex update_lock;
  40. const char *name;
  41. char valid; /* zero until following fields are valid */
  42. unsigned long last_updated; /* in jiffies */
  43. /* registers values */
  44. u8 sensorsp; /* sensor presence bits - SEL_CORE & SEL_PLACE */
  45. u32 temp[2][2]; /* core, place */
  46. };
  47. static struct k8temp_data *k8temp_update_device(struct device *dev)
  48. {
  49. struct k8temp_data *data = dev_get_drvdata(dev);
  50. struct pci_dev *pdev = to_pci_dev(dev);
  51. u8 tmp;
  52. mutex_lock(&data->update_lock);
  53. if (!data->valid
  54. || time_after(jiffies, data->last_updated + HZ)) {
  55. pci_read_config_byte(pdev, REG_TEMP, &tmp);
  56. tmp &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
  57. pci_write_config_byte(pdev, REG_TEMP, tmp);
  58. pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
  59. if (data->sensorsp & SEL_PLACE) {
  60. tmp |= SEL_PLACE; /* Select sensor 1, core0 */
  61. pci_write_config_byte(pdev, REG_TEMP, tmp);
  62. pci_read_config_dword(pdev, REG_TEMP,
  63. &data->temp[0][1]);
  64. }
  65. if (data->sensorsp & SEL_CORE) {
  66. tmp &= ~SEL_PLACE; /* Select sensor 0, core1 */
  67. tmp |= SEL_CORE;
  68. pci_write_config_byte(pdev, REG_TEMP, tmp);
  69. pci_read_config_dword(pdev, REG_TEMP,
  70. &data->temp[1][0]);
  71. if (data->sensorsp & SEL_PLACE) {
  72. tmp |= SEL_PLACE; /* Select sensor 1, core1 */
  73. pci_write_config_byte(pdev, REG_TEMP, tmp);
  74. pci_read_config_dword(pdev, REG_TEMP,
  75. &data->temp[1][1]);
  76. }
  77. }
  78. data->last_updated = jiffies;
  79. data->valid = 1;
  80. }
  81. mutex_unlock(&data->update_lock);
  82. return data;
  83. }
  84. /*
  85. * Sysfs stuff
  86. */
  87. static ssize_t show_name(struct device *dev, struct device_attribute
  88. *devattr, char *buf)
  89. {
  90. struct k8temp_data *data = dev_get_drvdata(dev);
  91. return sprintf(buf, "%s\n", data->name);
  92. }
  93. static ssize_t show_temp(struct device *dev,
  94. struct device_attribute *devattr, char *buf)
  95. {
  96. struct sensor_device_attribute_2 *attr =
  97. to_sensor_dev_attr_2(devattr);
  98. int core = attr->nr;
  99. int place = attr->index;
  100. struct k8temp_data *data = k8temp_update_device(dev);
  101. return sprintf(buf, "%d\n",
  102. TEMP_FROM_REG(data->temp[core][place]));
  103. }
  104. /* core, place */
  105. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
  106. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
  107. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0);
  108. static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1);
  109. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  110. static struct pci_device_id k8temp_ids[] = {
  111. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
  112. { 0 },
  113. };
  114. MODULE_DEVICE_TABLE(pci, k8temp_ids);
  115. static int __devinit k8temp_probe(struct pci_dev *pdev,
  116. const struct pci_device_id *id)
  117. {
  118. int err;
  119. u8 scfg;
  120. u32 temp;
  121. struct k8temp_data *data;
  122. u32 cpuid = cpuid_eax(1);
  123. /* this feature should be available since SH-C0 core */
  124. if ((cpuid == 0xf40) || (cpuid == 0xf50) || (cpuid == 0xf51)) {
  125. err = -ENODEV;
  126. goto exit;
  127. }
  128. if (!(data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL))) {
  129. err = -ENOMEM;
  130. goto exit;
  131. }
  132. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  133. scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
  134. pci_write_config_byte(pdev, REG_TEMP, scfg);
  135. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  136. if (scfg & (SEL_PLACE | SEL_CORE)) {
  137. dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
  138. err = -ENODEV;
  139. goto exit_free;
  140. }
  141. scfg |= (SEL_PLACE | SEL_CORE);
  142. pci_write_config_byte(pdev, REG_TEMP, scfg);
  143. /* now we know if we can change core and/or sensor */
  144. pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
  145. if (data->sensorsp & SEL_PLACE) {
  146. scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
  147. pci_write_config_byte(pdev, REG_TEMP, scfg);
  148. pci_read_config_dword(pdev, REG_TEMP, &temp);
  149. scfg |= SEL_CORE; /* prepare for next selection */
  150. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */
  151. data->sensorsp &= ~SEL_PLACE;
  152. }
  153. if (data->sensorsp & SEL_CORE) {
  154. scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
  155. pci_write_config_byte(pdev, REG_TEMP, scfg);
  156. pci_read_config_dword(pdev, REG_TEMP, &temp);
  157. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */
  158. data->sensorsp &= ~SEL_CORE;
  159. }
  160. data->name = "k8temp";
  161. mutex_init(&data->update_lock);
  162. dev_set_drvdata(&pdev->dev, data);
  163. /* Register sysfs hooks */
  164. err = device_create_file(&pdev->dev,
  165. &sensor_dev_attr_temp1_input.dev_attr);
  166. if (err)
  167. goto exit_remove;
  168. /* sensor can be changed and reports something */
  169. if (data->sensorsp & SEL_PLACE) {
  170. err = device_create_file(&pdev->dev,
  171. &sensor_dev_attr_temp2_input.dev_attr);
  172. if (err)
  173. goto exit_remove;
  174. }
  175. /* core can be changed and reports something */
  176. if (data->sensorsp & SEL_CORE) {
  177. err = device_create_file(&pdev->dev,
  178. &sensor_dev_attr_temp3_input.dev_attr);
  179. if (err)
  180. goto exit_remove;
  181. if (data->sensorsp & SEL_PLACE)
  182. err = device_create_file(&pdev->dev,
  183. &sensor_dev_attr_temp4_input.
  184. dev_attr);
  185. if (err)
  186. goto exit_remove;
  187. }
  188. err = device_create_file(&pdev->dev, &dev_attr_name);
  189. if (err)
  190. goto exit_remove;
  191. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  192. if (IS_ERR(data->hwmon_dev)) {
  193. err = PTR_ERR(data->hwmon_dev);
  194. goto exit_remove;
  195. }
  196. return 0;
  197. exit_remove:
  198. device_remove_file(&pdev->dev,
  199. &sensor_dev_attr_temp1_input.dev_attr);
  200. device_remove_file(&pdev->dev,
  201. &sensor_dev_attr_temp2_input.dev_attr);
  202. device_remove_file(&pdev->dev,
  203. &sensor_dev_attr_temp3_input.dev_attr);
  204. device_remove_file(&pdev->dev,
  205. &sensor_dev_attr_temp4_input.dev_attr);
  206. device_remove_file(&pdev->dev, &dev_attr_name);
  207. exit_free:
  208. dev_set_drvdata(&pdev->dev, NULL);
  209. kfree(data);
  210. exit:
  211. return err;
  212. }
  213. static void __devexit k8temp_remove(struct pci_dev *pdev)
  214. {
  215. struct k8temp_data *data = dev_get_drvdata(&pdev->dev);
  216. hwmon_device_unregister(data->hwmon_dev);
  217. device_remove_file(&pdev->dev,
  218. &sensor_dev_attr_temp1_input.dev_attr);
  219. device_remove_file(&pdev->dev,
  220. &sensor_dev_attr_temp2_input.dev_attr);
  221. device_remove_file(&pdev->dev,
  222. &sensor_dev_attr_temp3_input.dev_attr);
  223. device_remove_file(&pdev->dev,
  224. &sensor_dev_attr_temp4_input.dev_attr);
  225. device_remove_file(&pdev->dev, &dev_attr_name);
  226. dev_set_drvdata(&pdev->dev, NULL);
  227. kfree(data);
  228. }
  229. static struct pci_driver k8temp_driver = {
  230. .name = "k8temp",
  231. .id_table = k8temp_ids,
  232. .probe = k8temp_probe,
  233. .remove = __devexit_p(k8temp_remove),
  234. };
  235. static int __init k8temp_init(void)
  236. {
  237. return pci_register_driver(&k8temp_driver);
  238. }
  239. static void __exit k8temp_exit(void)
  240. {
  241. pci_unregister_driver(&k8temp_driver);
  242. }
  243. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  244. MODULE_DESCRIPTION("AMD K8 core temperature monitor");
  245. MODULE_LICENSE("GPL");
  246. module_init(k8temp_init)
  247. module_exit(k8temp_exit)