2 * k8temp.c - Linux kernel module for hardware monitoring
4 * Copyright (C) 2006 Rudolf Marek <r.marek@sh.cvut.cz>
6 * Inspired from the w83785 and amd756 drivers.
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.
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.
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
24 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/pci.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
35 #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
37 #define SEL_PLACE 0x40
41 struct class_device *class_dev;
42 struct mutex update_lock;
44 char valid; /* zero until following fields are valid */
45 unsigned long last_updated; /* in jiffies */
47 /* registers values */
48 u8 sensorsp; /* sensor presence bits - SEL_CORE & SEL_PLACE */
49 u32 temp[2][2]; /* core, place */
52 static struct k8temp_data *k8temp_update_device(struct device *dev)
54 struct k8temp_data *data = dev_get_drvdata(dev);
55 struct pci_dev *pdev = to_pci_dev(dev);
58 mutex_lock(&data->update_lock);
61 || time_after(jiffies, data->last_updated + HZ)) {
62 pci_read_config_byte(pdev, REG_TEMP, &tmp);
63 tmp &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
64 pci_write_config_byte(pdev, REG_TEMP, tmp);
65 pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
67 if (data->sensorsp & SEL_PLACE) {
68 tmp |= SEL_PLACE; /* Select sensor 1, core0 */
69 pci_write_config_byte(pdev, REG_TEMP, tmp);
70 pci_read_config_dword(pdev, REG_TEMP,
74 if (data->sensorsp & SEL_CORE) {
75 tmp &= ~SEL_PLACE; /* Select sensor 0, core1 */
77 pci_write_config_byte(pdev, REG_TEMP, tmp);
78 pci_read_config_dword(pdev, REG_TEMP,
81 if (data->sensorsp & SEL_PLACE) {
82 tmp |= SEL_PLACE; /* Select sensor 1, core1 */
83 pci_write_config_byte(pdev, REG_TEMP, tmp);
84 pci_read_config_dword(pdev, REG_TEMP,
89 data->last_updated = jiffies;
93 mutex_unlock(&data->update_lock);
101 static ssize_t show_name(struct device *dev, struct device_attribute
104 struct k8temp_data *data = dev_get_drvdata(dev);
106 return sprintf(buf, "%s\n", data->name);
110 static ssize_t show_temp(struct device *dev,
111 struct device_attribute *devattr, char *buf)
113 struct sensor_device_attribute_2 *attr =
114 to_sensor_dev_attr_2(devattr);
116 int place = attr->index;
117 struct k8temp_data *data = k8temp_update_device(dev);
119 return sprintf(buf, "%d\n",
120 TEMP_FROM_REG(data->temp[core][place]));
125 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
126 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
127 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0);
128 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1);
129 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
131 static struct pci_device_id k8temp_ids[] = {
132 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
136 static int __devinit k8temp_probe(struct pci_dev *pdev,
137 const struct pci_device_id *id)
142 struct k8temp_data *data;
143 u32 cpuid = cpuid_eax(1);
145 /* this feature should be available since SH-C0 core */
146 if ((cpuid == 0xf40) || (cpuid == 0xf50) || (cpuid == 0xf51)) {
151 if (!(data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL))) {
156 pci_read_config_byte(pdev, REG_TEMP, &scfg);
157 scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
158 pci_write_config_byte(pdev, REG_TEMP, scfg);
159 pci_read_config_byte(pdev, REG_TEMP, &scfg);
161 if (scfg & (SEL_PLACE | SEL_CORE)) {
162 dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
167 scfg |= (SEL_PLACE | SEL_CORE);
168 pci_write_config_byte(pdev, REG_TEMP, scfg);
170 /* now we know if we can change core and/or sensor */
171 pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
173 if (data->sensorsp & SEL_PLACE) {
174 scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
175 pci_write_config_byte(pdev, REG_TEMP, scfg);
176 pci_read_config_dword(pdev, REG_TEMP, &temp);
177 scfg |= SEL_CORE; /* prepare for next selection */
178 if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */
179 data->sensorsp &= ~SEL_PLACE;
182 if (data->sensorsp & SEL_CORE) {
183 scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
184 pci_write_config_byte(pdev, REG_TEMP, scfg);
185 pci_read_config_dword(pdev, REG_TEMP, &temp);
186 if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */
187 data->sensorsp &= ~SEL_CORE;
190 data->name = "k8temp";
191 mutex_init(&data->update_lock);
192 dev_set_drvdata(&pdev->dev, data);
194 /* Register sysfs hooks */
195 err = device_create_file(&pdev->dev,
196 &sensor_dev_attr_temp1_input.dev_attr);
200 /* sensor can be changed and reports something */
201 if (data->sensorsp & SEL_PLACE) {
202 err = device_create_file(&pdev->dev,
203 &sensor_dev_attr_temp2_input.dev_attr);
208 /* core can be changed and reports something */
209 if (data->sensorsp & SEL_CORE) {
210 err = device_create_file(&pdev->dev,
211 &sensor_dev_attr_temp3_input.dev_attr);
214 if (data->sensorsp & SEL_PLACE)
215 err = device_create_file(&pdev->dev,
216 &sensor_dev_attr_temp4_input.
222 err = device_create_file(&pdev->dev, &dev_attr_name);
226 data->class_dev = hwmon_device_register(&pdev->dev);
228 if (IS_ERR(data->class_dev)) {
229 err = PTR_ERR(data->class_dev);
236 device_remove_file(&pdev->dev,
237 &sensor_dev_attr_temp1_input.dev_attr);
238 device_remove_file(&pdev->dev,
239 &sensor_dev_attr_temp2_input.dev_attr);
240 device_remove_file(&pdev->dev,
241 &sensor_dev_attr_temp3_input.dev_attr);
242 device_remove_file(&pdev->dev,
243 &sensor_dev_attr_temp4_input.dev_attr);
244 device_remove_file(&pdev->dev, &dev_attr_name);
246 dev_set_drvdata(&pdev->dev, NULL);
252 static void __devexit k8temp_remove(struct pci_dev *pdev)
254 struct k8temp_data *data = dev_get_drvdata(&pdev->dev);
256 hwmon_device_unregister(data->class_dev);
257 device_remove_file(&pdev->dev,
258 &sensor_dev_attr_temp1_input.dev_attr);
259 device_remove_file(&pdev->dev,
260 &sensor_dev_attr_temp2_input.dev_attr);
261 device_remove_file(&pdev->dev,
262 &sensor_dev_attr_temp3_input.dev_attr);
263 device_remove_file(&pdev->dev,
264 &sensor_dev_attr_temp4_input.dev_attr);
265 device_remove_file(&pdev->dev, &dev_attr_name);
266 dev_set_drvdata(&pdev->dev, NULL);
270 static struct pci_driver k8temp_driver = {
272 .id_table = k8temp_ids,
273 .probe = k8temp_probe,
274 .remove = __devexit_p(k8temp_remove),
277 static int __init k8temp_init(void)
279 return pci_register_driver(&k8temp_driver);
282 static void __exit k8temp_exit(void)
284 pci_unregister_driver(&k8temp_driver);
287 MODULE_AUTHOR("Rudolf Marek <r.marek@sh.cvut.cz>");
288 MODULE_DESCRIPTION("AMD K8 core temperature monitor");
289 MODULE_LICENSE("GPL");
291 module_init(k8temp_init)
292 module_exit(k8temp_exit)