Linux Audio

Check our new training course

Linux kernel drivers training

Mar 31-Apr 9, 2025, special US time zones
Register
Loading...
v6.13.7
  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/slab.h>
 12#include <linux/jiffies.h>
 13#include <linux/device.h>
 14#include <linux/pci.h>
 15#include <linux/hwmon.h>
 
 16#include <linux/err.h>
 17#include <linux/mutex.h>
 18
 19/* Register definitions from datasheet */
 20#define REG_TSTHRCATA	0xE2
 21#define REG_TSCTRL	0xE8
 22#define REG_TSTHRRPEX	0xEB
 23#define REG_TSTHRLO	0xEC
 24#define REG_TSTHRHI	0xEE
 25#define REG_CTHINT	0xF0
 26#define REG_TSFSC	0xF3
 27#define REG_CTSTS	0xF4
 28#define REG_TSTHRRQPI	0xF5
 29#define REG_CTCTRL	0xF7
 30#define REG_TSTIMER	0xF8
 31
 32static int i5500_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
 33		      long *val)
 
 
 
 
 
 34{
 35	struct pci_dev *pdev = to_pci_dev(dev->parent);
 36	u16 tsthr;
 
 37	s8 tsfsc;
 38	u8 ctsts;
 39
 40	switch (type) {
 41	case hwmon_temp:
 42		switch (attr) {
 43		/* Sensor resolution : 0.5 degree C */
 44		case hwmon_temp_input:
 45			pci_read_config_word(pdev, REG_TSTHRHI, &tsthr);
 46			pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
 47			*val = (tsthr - tsfsc) * 500;
 48			return 0;
 49		case hwmon_temp_max:
 50			pci_read_config_word(pdev, REG_TSTHRHI, &tsthr);
 51			*val = tsthr * 500;
 52			return 0;
 53		case hwmon_temp_max_hyst:
 54			pci_read_config_word(pdev, REG_TSTHRLO, &tsthr);
 55			*val = tsthr * 500;
 56			return 0;
 57		case hwmon_temp_crit:
 58			pci_read_config_word(pdev, REG_TSTHRCATA, &tsthr);
 59			*val = tsthr * 500;
 60			return 0;
 61		case hwmon_temp_max_alarm:
 62			pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
 63			*val = !!(ctsts & BIT(1));
 64			return 0;
 65		case hwmon_temp_crit_alarm:
 66			pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
 67			*val = !!(ctsts & BIT(0));
 68			return 0;
 69		default:
 70			break;
 71		}
 72		break;
 73	default:
 74		break;
 75	}
 76
 77	return -EOPNOTSUPP;
 78}
 79
 80static const struct hwmon_ops i5500_ops = {
 81	.visible = 0444,
 82	.read = i5500_read,
 83};
 
 
 
 
 
 
 84
 85static const struct hwmon_channel_info * const i5500_info[] = {
 86	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
 87	HWMON_CHANNEL_INFO(temp,
 88			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | HWMON_T_CRIT |
 89			   HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM
 90			   ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 91	NULL
 92};
 93
 94static const struct hwmon_chip_info i5500_chip_info = {
 95	.ops = &i5500_ops,
 96	.info = i5500_info,
 97};
 98
 99static const struct pci_device_id i5500_temp_ids[] = {
100	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) },
101	{ 0 },
102};
103
104MODULE_DEVICE_TABLE(pci, i5500_temp_ids);
105
106static int i5500_temp_probe(struct pci_dev *pdev,
107			    const struct pci_device_id *id)
108{
109	int err;
110	struct device *hwmon_dev;
111	u32 tstimer;
112	s8 tsfsc;
113
114	err = pcim_enable_device(pdev);
115	if (err) {
116		dev_err(&pdev->dev, "Failed to enable device\n");
117		return err;
118	}
119
120	pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
121	pci_read_config_dword(pdev, REG_TSTIMER, &tstimer);
122	if (tsfsc == 0x7F && tstimer == 0x07D30D40) {
123		dev_notice(&pdev->dev, "Sensor seems to be disabled\n");
124		return -ENODEV;
125	}
126
127	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, "intel5500", NULL,
128							 &i5500_chip_info, NULL);
 
129	return PTR_ERR_OR_ZERO(hwmon_dev);
130}
131
132static struct pci_driver i5500_temp_driver = {
133	.name = "i5500_temp",
134	.id_table = i5500_temp_ids,
135	.probe = i5500_temp_probe,
136};
137
138module_pci_driver(i5500_temp_driver);
139
140MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
141MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver");
142MODULE_LICENSE("GPL");
v5.9
  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/module.h>
  9#include <linux/init.h>
 10#include <linux/slab.h>
 11#include <linux/jiffies.h>
 12#include <linux/device.h>
 13#include <linux/pci.h>
 14#include <linux/hwmon.h>
 15#include <linux/hwmon-sysfs.h>
 16#include <linux/err.h>
 17#include <linux/mutex.h>
 18
 19/* Register definitions from datasheet */
 20#define REG_TSTHRCATA	0xE2
 21#define REG_TSCTRL	0xE8
 22#define REG_TSTHRRPEX	0xEB
 23#define REG_TSTHRLO	0xEC
 24#define REG_TSTHRHI	0xEE
 25#define REG_CTHINT	0xF0
 26#define REG_TSFSC	0xF3
 27#define REG_CTSTS	0xF4
 28#define REG_TSTHRRQPI	0xF5
 29#define REG_CTCTRL	0xF7
 30#define REG_TSTIMER	0xF8
 31
 32/*
 33 * Sysfs stuff
 34 */
 35
 36/* Sensor resolution : 0.5 degree C */
 37static ssize_t temp1_input_show(struct device *dev,
 38				struct device_attribute *devattr, char *buf)
 39{
 40	struct pci_dev *pdev = to_pci_dev(dev->parent);
 41	long temp;
 42	u16 tsthrhi;
 43	s8 tsfsc;
 
 44
 45	pci_read_config_word(pdev, REG_TSTHRHI, &tsthrhi);
 46	pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
 47	temp = ((long)tsthrhi - tsfsc) * 500;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 48
 49	return sprintf(buf, "%ld\n", temp);
 50}
 51
 52static ssize_t thresh_show(struct device *dev,
 53			   struct device_attribute *devattr, char *buf)
 54{
 55	struct pci_dev *pdev = to_pci_dev(dev->parent);
 56	int reg = to_sensor_dev_attr(devattr)->index;
 57	long temp;
 58	u16 tsthr;
 59
 60	pci_read_config_word(pdev, reg, &tsthr);
 61	temp = tsthr * 500;
 62
 63	return sprintf(buf, "%ld\n", temp);
 64}
 65
 66static ssize_t alarm_show(struct device *dev,
 67			  struct device_attribute *devattr, char *buf)
 68{
 69	struct pci_dev *pdev = to_pci_dev(dev->parent);
 70	int nr = to_sensor_dev_attr(devattr)->index;
 71	u8 ctsts;
 72
 73	pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
 74	return sprintf(buf, "%u\n", (unsigned int)ctsts & (1 << nr));
 75}
 76
 77static DEVICE_ATTR_RO(temp1_input);
 78static SENSOR_DEVICE_ATTR_RO(temp1_crit, thresh, 0xE2);
 79static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, thresh, 0xEC);
 80static SENSOR_DEVICE_ATTR_RO(temp1_max, thresh, 0xEE);
 81static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 0);
 82static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
 83
 84static struct attribute *i5500_temp_attrs[] = {
 85	&dev_attr_temp1_input.attr,
 86	&sensor_dev_attr_temp1_crit.dev_attr.attr,
 87	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
 88	&sensor_dev_attr_temp1_max.dev_attr.attr,
 89	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
 90	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
 91	NULL
 92};
 93
 94ATTRIBUTE_GROUPS(i5500_temp);
 
 
 
 95
 96static const struct pci_device_id i5500_temp_ids[] = {
 97	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) },
 98	{ 0 },
 99};
100
101MODULE_DEVICE_TABLE(pci, i5500_temp_ids);
102
103static 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 = pci_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_groups(&pdev->dev,
125							   "intel5500", NULL,
126							   i5500_temp_groups);
127	return PTR_ERR_OR_ZERO(hwmon_dev);
128}
129
130static struct pci_driver i5500_temp_driver = {
131	.name = "i5500_temp",
132	.id_table = i5500_temp_ids,
133	.probe = i5500_temp_probe,
134};
135
136module_pci_driver(i5500_temp_driver);
137
138MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
139MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver");
140MODULE_LICENSE("GPL");