Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * i5500_temp - Driver for Intel 5500/5520/X58 chipset thermal sensor |
| 4 | * |
| 5 | * Copyright (C) 2012, 2014 Jean Delvare <jdelvare@suse.de> |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/jiffies.h> |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 12 | #include <linux/device.h> |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 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 | |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 32 | /* |
| 33 | * Sysfs stuff |
| 34 | */ |
| 35 | |
| 36 | /* Sensor resolution : 0.5 degree C */ |
Julia Lawall | 3eb52cf | 2016-12-22 13:04:46 +0100 | [diff] [blame] | 37 | static ssize_t temp1_input_show(struct device *dev, |
| 38 | struct device_attribute *devattr, char *buf) |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 39 | { |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 40 | struct pci_dev *pdev = to_pci_dev(dev->parent); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 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 | |
Guenter Roeck | 1221130 | 2018-12-10 14:02:08 -0800 | [diff] [blame] | 52 | static ssize_t thresh_show(struct device *dev, |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 53 | struct device_attribute *devattr, char *buf) |
| 54 | { |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 55 | struct pci_dev *pdev = to_pci_dev(dev->parent); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 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 | |
Guenter Roeck | 1221130 | 2018-12-10 14:02:08 -0800 | [diff] [blame] | 66 | static ssize_t alarm_show(struct device *dev, |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 67 | struct device_attribute *devattr, char *buf) |
| 68 | { |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 69 | struct pci_dev *pdev = to_pci_dev(dev->parent); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 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 | |
Julia Lawall | 3eb52cf | 2016-12-22 13:04:46 +0100 | [diff] [blame] | 77 | static DEVICE_ATTR_RO(temp1_input); |
Guenter Roeck | 1221130 | 2018-12-10 14:02:08 -0800 | [diff] [blame] | 78 | static SENSOR_DEVICE_ATTR_RO(temp1_crit, thresh, 0xE2); |
| 79 | static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, thresh, 0xEC); |
| 80 | static SENSOR_DEVICE_ATTR_RO(temp1_max, thresh, 0xEE); |
| 81 | static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 0); |
| 82 | static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 83 | |
Axel Lin | 86c725e | 2015-01-24 14:16:22 +0100 | [diff] [blame] | 84 | static struct attribute *i5500_temp_attrs[] = { |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 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, |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 91 | NULL |
| 92 | }; |
| 93 | |
Axel Lin | 86c725e | 2015-01-24 14:16:22 +0100 | [diff] [blame] | 94 | ATTRIBUTE_GROUPS(i5500_temp); |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 95 | |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 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; |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 107 | struct device *hwmon_dev; |
Jean Delvare | e3d9820 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 108 | u32 tstimer; |
| 109 | s8 tsfsc; |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 110 | |
| 111 | err = pci_enable_device(pdev); |
| 112 | if (err) { |
| 113 | dev_err(&pdev->dev, "Failed to enable device\n"); |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 114 | return err; |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Jean Delvare | e3d9820 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 117 | pci_read_config_byte(pdev, REG_TSFSC, &tsfsc); |
| 118 | pci_read_config_dword(pdev, REG_TSTIMER, &tstimer); |
| 119 | if (tsfsc == 0x7F && tstimer == 0x07D30D40) { |
Axel Lin | 86c725e | 2015-01-24 14:16:22 +0100 | [diff] [blame] | 120 | dev_notice(&pdev->dev, "Sensor seems to be disabled\n"); |
Jean Delvare | e3d9820 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 121 | return -ENODEV; |
| 122 | } |
| 123 | |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 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); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static struct pci_driver i5500_temp_driver = { |
| 131 | .name = "i5500_temp", |
| 132 | .id_table = i5500_temp_ids, |
| 133 | .probe = i5500_temp_probe, |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
Axel Lin | aef64d0 | 2015-01-24 14:16:22 +0100 | [diff] [blame] | 136 | module_pci_driver(i5500_temp_driver); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 137 | |
| 138 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
| 139 | MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver"); |
| 140 | MODULE_LICENSE("GPL"); |