blob: 3e3ccbf18b4efb0dfa482140cd77d72ddbff5898 [file] [log] [blame]
Jean Delvareada07282015-01-24 14:16:21 +01001/*
2 * i5500_temp - Driver for Intel 5500/5520/X58 chipset thermal sensor
3 *
4 * Copyright (C) 2012, 2014 Jean Delvare <jdelvare@suse.de>
5 *
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.
10 *
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.
15 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/jiffies.h>
Jean Delvareb8d48ce2015-01-24 14:16:21 +010021#include <linux/device.h>
Jean Delvareada07282015-01-24 14:16:21 +010022#include <linux/pci.h>
23#include <linux/hwmon.h>
24#include <linux/hwmon-sysfs.h>
25#include <linux/err.h>
26#include <linux/mutex.h>
27
28/* Register definitions from datasheet */
29#define REG_TSTHRCATA 0xE2
30#define REG_TSCTRL 0xE8
31#define REG_TSTHRRPEX 0xEB
32#define REG_TSTHRLO 0xEC
33#define REG_TSTHRHI 0xEE
34#define REG_CTHINT 0xF0
35#define REG_TSFSC 0xF3
36#define REG_CTSTS 0xF4
37#define REG_TSTHRRQPI 0xF5
38#define REG_CTCTRL 0xF7
39#define REG_TSTIMER 0xF8
40
Jean Delvareada07282015-01-24 14:16:21 +010041/*
42 * Sysfs stuff
43 */
44
45/* Sensor resolution : 0.5 degree C */
46static ssize_t show_temp(struct device *dev,
47 struct device_attribute *devattr, char *buf)
48{
Jean Delvareb8d48ce2015-01-24 14:16:21 +010049 struct pci_dev *pdev = to_pci_dev(dev->parent);
Jean Delvareada07282015-01-24 14:16:21 +010050 long temp;
51 u16 tsthrhi;
52 s8 tsfsc;
53
54 pci_read_config_word(pdev, REG_TSTHRHI, &tsthrhi);
55 pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
56 temp = ((long)tsthrhi - tsfsc) * 500;
57
58 return sprintf(buf, "%ld\n", temp);
59}
60
61static ssize_t show_thresh(struct device *dev,
62 struct device_attribute *devattr, char *buf)
63{
Jean Delvareb8d48ce2015-01-24 14:16:21 +010064 struct pci_dev *pdev = to_pci_dev(dev->parent);
Jean Delvareada07282015-01-24 14:16:21 +010065 int reg = to_sensor_dev_attr(devattr)->index;
66 long temp;
67 u16 tsthr;
68
69 pci_read_config_word(pdev, reg, &tsthr);
70 temp = tsthr * 500;
71
72 return sprintf(buf, "%ld\n", temp);
73}
74
75static ssize_t show_alarm(struct device *dev,
76 struct device_attribute *devattr, char *buf)
77{
Jean Delvareb8d48ce2015-01-24 14:16:21 +010078 struct pci_dev *pdev = to_pci_dev(dev->parent);
Jean Delvareada07282015-01-24 14:16:21 +010079 int nr = to_sensor_dev_attr(devattr)->index;
80 u8 ctsts;
81
82 pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
83 return sprintf(buf, "%u\n", (unsigned int)ctsts & (1 << nr));
84}
85
Jean Delvareada07282015-01-24 14:16:21 +010086static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
87static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_thresh, NULL, 0xE2);
88static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_thresh, NULL, 0xEC);
89static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_thresh, NULL, 0xEE);
90static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
91static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvareada07282015-01-24 14:16:21 +010092
Axel Lin86c725e2015-01-24 14:16:22 +010093static struct attribute *i5500_temp_attrs[] = {
Jean Delvareada07282015-01-24 14:16:21 +010094 &dev_attr_temp1_input.attr,
95 &sensor_dev_attr_temp1_crit.dev_attr.attr,
96 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
97 &sensor_dev_attr_temp1_max.dev_attr.attr,
98 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
99 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
Jean Delvareada07282015-01-24 14:16:21 +0100100 NULL
101};
102
Axel Lin86c725e2015-01-24 14:16:22 +0100103ATTRIBUTE_GROUPS(i5500_temp);
Jean Delvareb8d48ce2015-01-24 14:16:21 +0100104
Jean Delvareada07282015-01-24 14:16:21 +0100105static const struct pci_device_id i5500_temp_ids[] = {
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) },
107 { 0 },
108};
109
110MODULE_DEVICE_TABLE(pci, i5500_temp_ids);
111
112static int i5500_temp_probe(struct pci_dev *pdev,
113 const struct pci_device_id *id)
114{
115 int err;
Jean Delvareb8d48ce2015-01-24 14:16:21 +0100116 struct device *hwmon_dev;
Jean Delvaree3d98202015-01-24 14:16:21 +0100117 u32 tstimer;
118 s8 tsfsc;
Jean Delvareada07282015-01-24 14:16:21 +0100119
120 err = pci_enable_device(pdev);
121 if (err) {
122 dev_err(&pdev->dev, "Failed to enable device\n");
Jean Delvareb8d48ce2015-01-24 14:16:21 +0100123 return err;
Jean Delvareada07282015-01-24 14:16:21 +0100124 }
125
Jean Delvaree3d98202015-01-24 14:16:21 +0100126 pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
127 pci_read_config_dword(pdev, REG_TSTIMER, &tstimer);
128 if (tsfsc == 0x7F && tstimer == 0x07D30D40) {
Axel Lin86c725e2015-01-24 14:16:22 +0100129 dev_notice(&pdev->dev, "Sensor seems to be disabled\n");
Jean Delvaree3d98202015-01-24 14:16:21 +0100130 return -ENODEV;
131 }
132
Jean Delvareb8d48ce2015-01-24 14:16:21 +0100133 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
134 "intel5500", NULL,
135 i5500_temp_groups);
136 return PTR_ERR_OR_ZERO(hwmon_dev);
Jean Delvareada07282015-01-24 14:16:21 +0100137}
138
139static struct pci_driver i5500_temp_driver = {
140 .name = "i5500_temp",
141 .id_table = i5500_temp_ids,
142 .probe = i5500_temp_probe,
Jean Delvareada07282015-01-24 14:16:21 +0100143};
144
Axel Linaef64d02015-01-24 14:16:22 +0100145module_pci_driver(i5500_temp_driver);
Jean Delvareada07282015-01-24 14:16:21 +0100146
147MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
148MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver");
149MODULE_LICENSE("GPL");