xref: /linux/drivers/hwmon/i5500_temp.c (revision c31f4aa8fed048fa70e742c4bb49bb48dc489ab3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * i5500_temp - Driver for Intel 5500/5520/X58 chipset thermal sensor
4  *
5  * Copyright (C) 2012, 2014 Jean Delvare <jdelvare@suse.de>
6  */
7 
8 #include <linux/bitops.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/device.h>
12 #include <linux/pci.h>
13 #include <linux/hwmon.h>
14 #include <linux/err.h>
15 
16 /* Register definitions from datasheet */
17 #define REG_TSTHRCATA	0xE2
18 #define REG_TSCTRL	0xE8
19 #define REG_TSTHRRPEX	0xEB
20 #define REG_TSTHRLO	0xEC
21 #define REG_TSTHRHI	0xEE
22 #define REG_CTHINT	0xF0
23 #define REG_TSFSC	0xF3
24 #define REG_CTSTS	0xF4
25 #define REG_TSTHRRQPI	0xF5
26 #define REG_CTCTRL	0xF7
27 #define REG_TSTIMER	0xF8
28 
29 static int i5500_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
30 		      long *val)
31 {
32 	struct pci_dev *pdev = to_pci_dev(dev->parent);
33 	u16 tsthr;
34 	s8 tsfsc;
35 	u8 ctsts;
36 
37 	switch (type) {
38 	case hwmon_temp:
39 		switch (attr) {
40 		/* Sensor resolution : 0.5 degree C */
41 		case hwmon_temp_input:
42 			pci_read_config_word(pdev, REG_TSTHRHI, &tsthr);
43 			pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
44 			*val = (tsthr - tsfsc) * 500;
45 			return 0;
46 		case hwmon_temp_max:
47 			pci_read_config_word(pdev, REG_TSTHRHI, &tsthr);
48 			*val = tsthr * 500;
49 			return 0;
50 		case hwmon_temp_max_hyst:
51 			pci_read_config_word(pdev, REG_TSTHRLO, &tsthr);
52 			*val = tsthr * 500;
53 			return 0;
54 		case hwmon_temp_crit:
55 			pci_read_config_word(pdev, REG_TSTHRCATA, &tsthr);
56 			*val = tsthr * 500;
57 			return 0;
58 		case hwmon_temp_max_alarm:
59 			pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
60 			*val = !!(ctsts & BIT(1));
61 			return 0;
62 		case hwmon_temp_crit_alarm:
63 			pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
64 			*val = !!(ctsts & BIT(0));
65 			return 0;
66 		default:
67 			break;
68 		}
69 		break;
70 	default:
71 		break;
72 	}
73 
74 	return -EOPNOTSUPP;
75 }
76 
77 static const struct hwmon_ops i5500_ops = {
78 	.visible = 0444,
79 	.read = i5500_read,
80 };
81 
82 static const struct hwmon_channel_info * const i5500_info[] = {
83 	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
84 	HWMON_CHANNEL_INFO(temp,
85 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | HWMON_T_CRIT |
86 			   HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM
87 			   ),
88 	NULL
89 };
90 
91 static const struct hwmon_chip_info i5500_chip_info = {
92 	.ops = &i5500_ops,
93 	.info = i5500_info,
94 };
95 
96 static const struct pci_device_id i5500_temp_ids[] = {
97 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) },
98 	{ 0 },
99 };
100 
101 MODULE_DEVICE_TABLE(pci, i5500_temp_ids);
102 
103 static int i5500_temp_probe(struct pci_dev *pdev,
104 			    const struct pci_device_id *id)
105 {
106 	int err;
107 	struct device *hwmon_dev;
108 	u32 tstimer;
109 	s8 tsfsc;
110 
111 	err = pcim_enable_device(pdev);
112 	if (err) {
113 		dev_err(&pdev->dev, "Failed to enable device\n");
114 		return err;
115 	}
116 
117 	pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
118 	pci_read_config_dword(pdev, REG_TSTIMER, &tstimer);
119 	if (tsfsc == 0x7F && tstimer == 0x07D30D40) {
120 		dev_notice(&pdev->dev, "Sensor seems to be disabled\n");
121 		return -ENODEV;
122 	}
123 
124 	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, "intel5500", NULL,
125 							 &i5500_chip_info, NULL);
126 	return PTR_ERR_OR_ZERO(hwmon_dev);
127 }
128 
129 static struct pci_driver i5500_temp_driver = {
130 	.name = "i5500_temp",
131 	.id_table = i5500_temp_ids,
132 	.probe = i5500_temp_probe,
133 };
134 
135 module_pci_driver(i5500_temp_driver);
136 
137 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
138 MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver");
139 MODULE_LICENSE("GPL");
140