2 * i5500_temp - Driver for Intel 5500/5520/X58 chipset thermal sensor
4 * Copyright (C) 2012, 2014 Jean Delvare <jdelvare@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <linux/pci.h>
22 #include <linux/hwmon.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/err.h>
25 #include <linux/mutex.h>
27 /* Register definitions from datasheet */
28 #define REG_TSTHRCATA 0xE2
29 #define REG_TSCTRL 0xE8
30 #define REG_TSTHRRPEX 0xEB
31 #define REG_TSTHRLO 0xEC
32 #define REG_TSTHRHI 0xEE
33 #define REG_CTHINT 0xF0
34 #define REG_TSFSC 0xF3
35 #define REG_CTSTS 0xF4
36 #define REG_TSTHRRQPI 0xF5
37 #define REG_CTCTRL 0xF7
38 #define REG_TSTIMER 0xF8
40 struct i5500_temp_data {
41 struct device *hwmon_dev;
49 /* Sensor resolution : 0.5 degree C */
50 static ssize_t show_temp(struct device *dev,
51 struct device_attribute *devattr, char *buf)
53 struct pci_dev *pdev = to_pci_dev(dev);
58 pci_read_config_word(pdev, REG_TSTHRHI, &tsthrhi);
59 pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
60 temp = ((long)tsthrhi - tsfsc) * 500;
62 return sprintf(buf, "%ld\n", temp);
65 static ssize_t show_thresh(struct device *dev,
66 struct device_attribute *devattr, char *buf)
68 struct pci_dev *pdev = to_pci_dev(dev);
69 int reg = to_sensor_dev_attr(devattr)->index;
73 pci_read_config_word(pdev, reg, &tsthr);
76 return sprintf(buf, "%ld\n", temp);
79 static ssize_t show_alarm(struct device *dev,
80 struct device_attribute *devattr, char *buf)
82 struct pci_dev *pdev = to_pci_dev(dev);
83 int nr = to_sensor_dev_attr(devattr)->index;
86 pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
87 return sprintf(buf, "%u\n", (unsigned int)ctsts & (1 << nr));
90 static ssize_t show_name(struct device *dev, struct device_attribute
93 struct i5500_temp_data *data = dev_get_drvdata(dev);
95 return sprintf(buf, "%s\n", data->name);
98 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
99 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_thresh, NULL, 0xE2);
100 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_thresh, NULL, 0xEC);
101 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_thresh, NULL, 0xEE);
102 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
103 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
104 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
106 static struct attribute *i5500_temp_attributes[] = {
107 &dev_attr_temp1_input.attr,
108 &sensor_dev_attr_temp1_crit.dev_attr.attr,
109 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
110 &sensor_dev_attr_temp1_max.dev_attr.attr,
111 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
112 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
117 static const struct attribute_group i5500_temp_group = {
118 .attrs = i5500_temp_attributes,
121 static const struct pci_device_id i5500_temp_ids[] = {
122 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) },
126 MODULE_DEVICE_TABLE(pci, i5500_temp_ids);
128 static int i5500_temp_probe(struct pci_dev *pdev,
129 const struct pci_device_id *id)
132 struct i5500_temp_data *data;
134 data = kzalloc(sizeof(struct i5500_temp_data), GFP_KERNEL);
140 data->name = "intel5500";
141 dev_set_drvdata(&pdev->dev, data);
143 err = pci_enable_device(pdev);
145 dev_err(&pdev->dev, "Failed to enable device\n");
149 /* Register sysfs hooks */
150 err = sysfs_create_group(&pdev->dev.kobj, &i5500_temp_group);
154 data->hwmon_dev = hwmon_device_register(&pdev->dev);
155 if (IS_ERR(data->hwmon_dev)) {
156 err = PTR_ERR(data->hwmon_dev);
163 sysfs_remove_group(&pdev->dev.kobj, &i5500_temp_group);
170 static void i5500_temp_remove(struct pci_dev *pdev)
172 struct i5500_temp_data *data = dev_get_drvdata(&pdev->dev);
174 hwmon_device_unregister(data->hwmon_dev);
175 sysfs_remove_group(&pdev->dev.kobj, &i5500_temp_group);
179 static struct pci_driver i5500_temp_driver = {
180 .name = "i5500_temp",
181 .id_table = i5500_temp_ids,
182 .probe = i5500_temp_probe,
183 .remove = i5500_temp_remove,
186 static int __init i5500_temp_init(void)
188 return pci_register_driver(&i5500_temp_driver);
191 static void __exit i5500_temp_exit(void)
193 pci_unregister_driver(&i5500_temp_driver);
196 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
197 MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver");
198 MODULE_LICENSE("GPL");
200 module_init(i5500_temp_init)
201 module_exit(i5500_temp_exit)