2 * k8temp.c - Linux kernel module for hardware monitoring
4 * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.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>
34 #include <asm/processor.h>
36 #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
38 #define SEL_PLACE 0x40
42 struct device
*hwmon_dev
;
43 struct mutex update_lock
;
45 char valid
; /* zero until following fields are valid */
46 unsigned long last_updated
; /* in jiffies */
48 /* registers values */
49 u8 sensorsp
; /* sensor presence bits - SEL_CORE & SEL_PLACE */
50 u32 temp
[2][2]; /* core, place */
51 u8 swap_core_select
; /* meaning of SEL_CORE is inverted */
55 static struct k8temp_data
*k8temp_update_device(struct device
*dev
)
57 struct k8temp_data
*data
= dev_get_drvdata(dev
);
58 struct pci_dev
*pdev
= to_pci_dev(dev
);
61 mutex_lock(&data
->update_lock
);
64 || time_after(jiffies
, data
->last_updated
+ HZ
)) {
65 pci_read_config_byte(pdev
, REG_TEMP
, &tmp
);
66 tmp
&= ~(SEL_PLACE
| SEL_CORE
); /* Select sensor 0, core0 */
67 pci_write_config_byte(pdev
, REG_TEMP
, tmp
);
68 pci_read_config_dword(pdev
, REG_TEMP
, &data
->temp
[0][0]);
70 if (data
->sensorsp
& SEL_PLACE
) {
71 tmp
|= SEL_PLACE
; /* Select sensor 1, core0 */
72 pci_write_config_byte(pdev
, REG_TEMP
, tmp
);
73 pci_read_config_dword(pdev
, REG_TEMP
,
77 if (data
->sensorsp
& SEL_CORE
) {
78 tmp
&= ~SEL_PLACE
; /* Select sensor 0, core1 */
80 pci_write_config_byte(pdev
, REG_TEMP
, tmp
);
81 pci_read_config_dword(pdev
, REG_TEMP
,
84 if (data
->sensorsp
& SEL_PLACE
) {
85 tmp
|= SEL_PLACE
; /* Select sensor 1, core1 */
86 pci_write_config_byte(pdev
, REG_TEMP
, tmp
);
87 pci_read_config_dword(pdev
, REG_TEMP
,
92 data
->last_updated
= jiffies
;
96 mutex_unlock(&data
->update_lock
);
104 static ssize_t
show_name(struct device
*dev
, struct device_attribute
107 struct k8temp_data
*data
= dev_get_drvdata(dev
);
109 return sprintf(buf
, "%s\n", data
->name
);
113 static ssize_t
show_temp(struct device
*dev
,
114 struct device_attribute
*devattr
, char *buf
)
116 struct sensor_device_attribute_2
*attr
=
117 to_sensor_dev_attr_2(devattr
);
119 int place
= attr
->index
;
121 struct k8temp_data
*data
= k8temp_update_device(dev
);
123 if (data
->swap_core_select
&& (data
->sensorsp
& SEL_CORE
))
126 temp
= TEMP_FROM_REG(data
->temp
[core
][place
]) + data
->temp_offset
;
128 return sprintf(buf
, "%d\n", temp
);
133 static SENSOR_DEVICE_ATTR_2(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0, 0);
134 static SENSOR_DEVICE_ATTR_2(temp2_input
, S_IRUGO
, show_temp
, NULL
, 0, 1);
135 static SENSOR_DEVICE_ATTR_2(temp3_input
, S_IRUGO
, show_temp
, NULL
, 1, 0);
136 static SENSOR_DEVICE_ATTR_2(temp4_input
, S_IRUGO
, show_temp
, NULL
, 1, 1);
137 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
139 static struct pci_device_id k8temp_ids
[] = {
140 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_K8_NB_MISC
) },
144 MODULE_DEVICE_TABLE(pci
, k8temp_ids
);
146 static int __devinit
is_rev_g_desktop(u8 model
)
153 if (model
== 0xc1 || model
== 0x6c || model
== 0x7c)
157 * Differentiate between AM2 and ASB1.
158 * See "Constructing the processor Name String" in "Revision
159 * Guide for AMD NPT Family 0Fh Processors" (33610).
161 brandidx
= cpuid_ebx(0x80000001);
162 brandidx
= (brandidx
>> 9) & 0x1f;
165 if ((model
== 0x6f || model
== 0x7f) &&
166 (brandidx
== 0x7 || brandidx
== 0x9 || brandidx
== 0xc))
171 (brandidx
== 0xb || brandidx
== 0xc))
177 static int __devinit
k8temp_probe(struct pci_dev
*pdev
,
178 const struct pci_device_id
*id
)
184 struct k8temp_data
*data
;
186 if (!(data
= kzalloc(sizeof(struct k8temp_data
), GFP_KERNEL
))) {
191 model
= boot_cpu_data
.x86_model
;
192 stepping
= boot_cpu_data
.x86_mask
;
194 switch (boot_cpu_data
.x86
) {
196 /* feature available since SH-C0, exclude older revisions */
197 if (((model
== 4) && (stepping
== 0)) ||
198 ((model
== 5) && (stepping
<= 1))) {
204 * AMD NPT family 0fh, i.e. RevF and RevG:
205 * meaning of SEL_CORE bit is inverted
208 data
->swap_core_select
= 1;
209 dev_warn(&pdev
->dev
, "Temperature readouts might be "
210 "wrong - check erratum #141\n");
213 if (is_rev_g_desktop(model
)) {
215 * RevG desktop CPUs (i.e. no socket S1G1 or
216 * ASB1 parts) need additional offset,
217 * otherwise reported temperature is below
218 * ambient temperature
220 data
->temp_offset
= 21000;
226 pci_read_config_byte(pdev
, REG_TEMP
, &scfg
);
227 scfg
&= ~(SEL_PLACE
| SEL_CORE
); /* Select sensor 0, core0 */
228 pci_write_config_byte(pdev
, REG_TEMP
, scfg
);
229 pci_read_config_byte(pdev
, REG_TEMP
, &scfg
);
231 if (scfg
& (SEL_PLACE
| SEL_CORE
)) {
232 dev_err(&pdev
->dev
, "Configuration bit(s) stuck at 1!\n");
237 scfg
|= (SEL_PLACE
| SEL_CORE
);
238 pci_write_config_byte(pdev
, REG_TEMP
, scfg
);
240 /* now we know if we can change core and/or sensor */
241 pci_read_config_byte(pdev
, REG_TEMP
, &data
->sensorsp
);
243 if (data
->sensorsp
& SEL_PLACE
) {
244 scfg
&= ~SEL_CORE
; /* Select sensor 1, core0 */
245 pci_write_config_byte(pdev
, REG_TEMP
, scfg
);
246 pci_read_config_dword(pdev
, REG_TEMP
, &temp
);
247 scfg
|= SEL_CORE
; /* prepare for next selection */
248 if (!((temp
>> 16) & 0xff)) /* if temp is 0 -49C is not likely */
249 data
->sensorsp
&= ~SEL_PLACE
;
252 if (data
->sensorsp
& SEL_CORE
) {
253 scfg
&= ~SEL_PLACE
; /* Select sensor 0, core1 */
254 pci_write_config_byte(pdev
, REG_TEMP
, scfg
);
255 pci_read_config_dword(pdev
, REG_TEMP
, &temp
);
256 if (!((temp
>> 16) & 0xff)) /* if temp is 0 -49C is not likely */
257 data
->sensorsp
&= ~SEL_CORE
;
260 data
->name
= "k8temp";
261 mutex_init(&data
->update_lock
);
262 dev_set_drvdata(&pdev
->dev
, data
);
264 /* Register sysfs hooks */
265 err
= device_create_file(&pdev
->dev
,
266 &sensor_dev_attr_temp1_input
.dev_attr
);
270 /* sensor can be changed and reports something */
271 if (data
->sensorsp
& SEL_PLACE
) {
272 err
= device_create_file(&pdev
->dev
,
273 &sensor_dev_attr_temp2_input
.dev_attr
);
278 /* core can be changed and reports something */
279 if (data
->sensorsp
& SEL_CORE
) {
280 err
= device_create_file(&pdev
->dev
,
281 &sensor_dev_attr_temp3_input
.dev_attr
);
284 if (data
->sensorsp
& SEL_PLACE
)
285 err
= device_create_file(&pdev
->dev
,
286 &sensor_dev_attr_temp4_input
.
292 err
= device_create_file(&pdev
->dev
, &dev_attr_name
);
296 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
298 if (IS_ERR(data
->hwmon_dev
)) {
299 err
= PTR_ERR(data
->hwmon_dev
);
306 device_remove_file(&pdev
->dev
,
307 &sensor_dev_attr_temp1_input
.dev_attr
);
308 device_remove_file(&pdev
->dev
,
309 &sensor_dev_attr_temp2_input
.dev_attr
);
310 device_remove_file(&pdev
->dev
,
311 &sensor_dev_attr_temp3_input
.dev_attr
);
312 device_remove_file(&pdev
->dev
,
313 &sensor_dev_attr_temp4_input
.dev_attr
);
314 device_remove_file(&pdev
->dev
, &dev_attr_name
);
316 dev_set_drvdata(&pdev
->dev
, NULL
);
322 static void __devexit
k8temp_remove(struct pci_dev
*pdev
)
324 struct k8temp_data
*data
= dev_get_drvdata(&pdev
->dev
);
326 hwmon_device_unregister(data
->hwmon_dev
);
327 device_remove_file(&pdev
->dev
,
328 &sensor_dev_attr_temp1_input
.dev_attr
);
329 device_remove_file(&pdev
->dev
,
330 &sensor_dev_attr_temp2_input
.dev_attr
);
331 device_remove_file(&pdev
->dev
,
332 &sensor_dev_attr_temp3_input
.dev_attr
);
333 device_remove_file(&pdev
->dev
,
334 &sensor_dev_attr_temp4_input
.dev_attr
);
335 device_remove_file(&pdev
->dev
, &dev_attr_name
);
336 dev_set_drvdata(&pdev
->dev
, NULL
);
340 static struct pci_driver k8temp_driver
= {
342 .id_table
= k8temp_ids
,
343 .probe
= k8temp_probe
,
344 .remove
= __devexit_p(k8temp_remove
),
347 static int __init
k8temp_init(void)
349 return pci_register_driver(&k8temp_driver
);
352 static void __exit
k8temp_exit(void)
354 pci_unregister_driver(&k8temp_driver
);
357 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
358 MODULE_DESCRIPTION("AMD K8 core temperature monitor");
359 MODULE_LICENSE("GPL");
361 module_init(k8temp_init
)
362 module_exit(k8temp_exit
)