hwmon: (k8temp) Fix wrong sensor selection for AMD K8 RevF/RevG CPUs
[pandora-kernel.git] / drivers / hwmon / k8temp.c
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
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>
35
36 #define TEMP_FROM_REG(val)      (((((val) >> 16) & 0xff) - 49) * 1000)
37 #define REG_TEMP        0xe4
38 #define SEL_PLACE       0x40
39 #define SEL_CORE        0x04
40
41 struct k8temp_data {
42         struct device *hwmon_dev;
43         struct mutex update_lock;
44         const char *name;
45         char valid;             /* zero until following fields are valid */
46         unsigned long last_updated;     /* in jiffies */
47
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 */
52 };
53
54 static struct k8temp_data *k8temp_update_device(struct device *dev)
55 {
56         struct k8temp_data *data = dev_get_drvdata(dev);
57         struct pci_dev *pdev = to_pci_dev(dev);
58         u8 tmp;
59
60         mutex_lock(&data->update_lock);
61
62         if (!data->valid
63             || time_after(jiffies, data->last_updated + HZ)) {
64                 pci_read_config_byte(pdev, REG_TEMP, &tmp);
65                 tmp &= ~(SEL_PLACE | SEL_CORE);         /* Select sensor 0, core0 */
66                 pci_write_config_byte(pdev, REG_TEMP, tmp);
67                 pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
68
69                 if (data->sensorsp & SEL_PLACE) {
70                         tmp |= SEL_PLACE;       /* Select sensor 1, core0 */
71                         pci_write_config_byte(pdev, REG_TEMP, tmp);
72                         pci_read_config_dword(pdev, REG_TEMP,
73                                               &data->temp[0][1]);
74                 }
75
76                 if (data->sensorsp & SEL_CORE) {
77                         tmp &= ~SEL_PLACE;      /* Select sensor 0, core1 */
78                         tmp |= SEL_CORE;
79                         pci_write_config_byte(pdev, REG_TEMP, tmp);
80                         pci_read_config_dword(pdev, REG_TEMP,
81                                               &data->temp[1][0]);
82
83                         if (data->sensorsp & SEL_PLACE) {
84                                 tmp |= SEL_PLACE;       /* Select sensor 1, core1 */
85                                 pci_write_config_byte(pdev, REG_TEMP, tmp);
86                                 pci_read_config_dword(pdev, REG_TEMP,
87                                                       &data->temp[1][1]);
88                         }
89                 }
90
91                 data->last_updated = jiffies;
92                 data->valid = 1;
93         }
94
95         mutex_unlock(&data->update_lock);
96         return data;
97 }
98
99 /*
100  * Sysfs stuff
101  */
102
103 static ssize_t show_name(struct device *dev, struct device_attribute
104                          *devattr, char *buf)
105 {
106         struct k8temp_data *data = dev_get_drvdata(dev);
107
108         return sprintf(buf, "%s\n", data->name);
109 }
110
111
112 static ssize_t show_temp(struct device *dev,
113                          struct device_attribute *devattr, char *buf)
114 {
115         struct sensor_device_attribute_2 *attr =
116             to_sensor_dev_attr_2(devattr);
117         int core = attr->nr;
118         int place = attr->index;
119         struct k8temp_data *data = k8temp_update_device(dev);
120
121         if (data->swap_core_select)
122                 core = core ? 0 : 1;
123
124         return sprintf(buf, "%d\n",
125                        TEMP_FROM_REG(data->temp[core][place]));
126 }
127
128 /* core, place */
129
130 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
131 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
132 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0);
133 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1);
134 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
135
136 static struct pci_device_id k8temp_ids[] = {
137         { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
138         { 0 },
139 };
140
141 MODULE_DEVICE_TABLE(pci, k8temp_ids);
142
143 static int __devinit k8temp_probe(struct pci_dev *pdev,
144                                   const struct pci_device_id *id)
145 {
146         int err;
147         u8 scfg;
148         u32 temp;
149         u8 model, stepping;
150         struct k8temp_data *data;
151
152         if (!(data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL))) {
153                 err = -ENOMEM;
154                 goto exit;
155         }
156
157         model = boot_cpu_data.x86_model;
158         stepping = boot_cpu_data.x86_mask;
159
160         switch (boot_cpu_data.x86) {
161         case 0xf:
162                 /* feature available since SH-C0, exclude older revisions */
163                 if (((model == 4) && (stepping == 0)) ||
164                     ((model == 5) && (stepping <= 1))) {
165                         err = -ENODEV;
166                         goto exit_free;
167                 }
168
169                 /*
170                  * AMD NPT family 0fh, i.e. RevF and RevG:
171                  * meaning of SEL_CORE bit is inverted
172                  */
173                 if (model >= 0x40) {
174                         data->swap_core_select = 1;
175                         dev_warn(&pdev->dev, "Temperature readouts might be "
176                                  "wrong - check erratum #141\n");
177                 }
178
179                 break;
180         }
181
182         pci_read_config_byte(pdev, REG_TEMP, &scfg);
183         scfg &= ~(SEL_PLACE | SEL_CORE);                /* Select sensor 0, core0 */
184         pci_write_config_byte(pdev, REG_TEMP, scfg);
185         pci_read_config_byte(pdev, REG_TEMP, &scfg);
186
187         if (scfg & (SEL_PLACE | SEL_CORE)) {
188                 dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
189                 err = -ENODEV;
190                 goto exit_free;
191         }
192
193         scfg |= (SEL_PLACE | SEL_CORE);
194         pci_write_config_byte(pdev, REG_TEMP, scfg);
195
196         /* now we know if we can change core and/or sensor */
197         pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
198
199         if (data->sensorsp & SEL_PLACE) {
200                 scfg &= ~SEL_CORE;      /* Select sensor 1, core0 */
201                 pci_write_config_byte(pdev, REG_TEMP, scfg);
202                 pci_read_config_dword(pdev, REG_TEMP, &temp);
203                 scfg |= SEL_CORE;       /* prepare for next selection */
204                 if (!((temp >> 16) & 0xff))     /* if temp is 0 -49C is not likely */
205                         data->sensorsp &= ~SEL_PLACE;
206         }
207
208         if (data->sensorsp & SEL_CORE) {
209                 scfg &= ~SEL_PLACE;     /* Select sensor 0, core1 */
210                 pci_write_config_byte(pdev, REG_TEMP, scfg);
211                 pci_read_config_dword(pdev, REG_TEMP, &temp);
212                 if (!((temp >> 16) & 0xff))     /* if temp is 0 -49C is not likely */
213                         data->sensorsp &= ~SEL_CORE;
214         }
215
216         data->name = "k8temp";
217         mutex_init(&data->update_lock);
218         dev_set_drvdata(&pdev->dev, data);
219
220         /* Register sysfs hooks */
221         err = device_create_file(&pdev->dev,
222                            &sensor_dev_attr_temp1_input.dev_attr);
223         if (err)
224                 goto exit_remove;
225
226         /* sensor can be changed and reports something */
227         if (data->sensorsp & SEL_PLACE) {
228                 err = device_create_file(&pdev->dev,
229                                    &sensor_dev_attr_temp2_input.dev_attr);
230                 if (err)
231                         goto exit_remove;
232         }
233
234         /* core can be changed and reports something */
235         if (data->sensorsp & SEL_CORE) {
236                 err = device_create_file(&pdev->dev,
237                                    &sensor_dev_attr_temp3_input.dev_attr);
238                 if (err)
239                         goto exit_remove;
240                 if (data->sensorsp & SEL_PLACE)
241                         err = device_create_file(&pdev->dev,
242                                            &sensor_dev_attr_temp4_input.
243                                            dev_attr);
244                         if (err)
245                                 goto exit_remove;
246         }
247
248         err = device_create_file(&pdev->dev, &dev_attr_name);
249         if (err)
250                 goto exit_remove;
251
252         data->hwmon_dev = hwmon_device_register(&pdev->dev);
253
254         if (IS_ERR(data->hwmon_dev)) {
255                 err = PTR_ERR(data->hwmon_dev);
256                 goto exit_remove;
257         }
258
259         return 0;
260
261 exit_remove:
262         device_remove_file(&pdev->dev,
263                            &sensor_dev_attr_temp1_input.dev_attr);
264         device_remove_file(&pdev->dev,
265                            &sensor_dev_attr_temp2_input.dev_attr);
266         device_remove_file(&pdev->dev,
267                            &sensor_dev_attr_temp3_input.dev_attr);
268         device_remove_file(&pdev->dev,
269                            &sensor_dev_attr_temp4_input.dev_attr);
270         device_remove_file(&pdev->dev, &dev_attr_name);
271 exit_free:
272         dev_set_drvdata(&pdev->dev, NULL);
273         kfree(data);
274 exit:
275         return err;
276 }
277
278 static void __devexit k8temp_remove(struct pci_dev *pdev)
279 {
280         struct k8temp_data *data = dev_get_drvdata(&pdev->dev);
281
282         hwmon_device_unregister(data->hwmon_dev);
283         device_remove_file(&pdev->dev,
284                            &sensor_dev_attr_temp1_input.dev_attr);
285         device_remove_file(&pdev->dev,
286                            &sensor_dev_attr_temp2_input.dev_attr);
287         device_remove_file(&pdev->dev,
288                            &sensor_dev_attr_temp3_input.dev_attr);
289         device_remove_file(&pdev->dev,
290                            &sensor_dev_attr_temp4_input.dev_attr);
291         device_remove_file(&pdev->dev, &dev_attr_name);
292         dev_set_drvdata(&pdev->dev, NULL);
293         kfree(data);
294 }
295
296 static struct pci_driver k8temp_driver = {
297         .name = "k8temp",
298         .id_table = k8temp_ids,
299         .probe = k8temp_probe,
300         .remove = __devexit_p(k8temp_remove),
301 };
302
303 static int __init k8temp_init(void)
304 {
305         return pci_register_driver(&k8temp_driver);
306 }
307
308 static void __exit k8temp_exit(void)
309 {
310         pci_unregister_driver(&k8temp_driver);
311 }
312
313 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
314 MODULE_DESCRIPTION("AMD K8 core temperature monitor");
315 MODULE_LICENSE("GPL");
316
317 module_init(k8temp_init)
318 module_exit(k8temp_exit)