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 | |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 8 | #include <linux/bitops.h> |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/jiffies.h> |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 13 | #include <linux/device.h> |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 14 | #include <linux/pci.h> |
| 15 | #include <linux/hwmon.h> |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 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 | |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 32 | static umode_t i5500_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr, |
| 33 | int channel) |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 34 | { |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 35 | return 0444; |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 38 | static int i5500_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, |
| 39 | long *val) |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 40 | { |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 41 | struct pci_dev *pdev = to_pci_dev(dev->parent); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 42 | u16 tsthr; |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 43 | s8 tsfsc; |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 44 | u8 ctsts; |
| 45 | |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 46 | switch (type) { |
| 47 | case hwmon_temp: |
| 48 | switch (attr) { |
| 49 | /* Sensor resolution : 0.5 degree C */ |
| 50 | case hwmon_temp_input: |
| 51 | pci_read_config_word(pdev, REG_TSTHRHI, &tsthr); |
| 52 | pci_read_config_byte(pdev, REG_TSFSC, &tsfsc); |
| 53 | *val = (tsthr - tsfsc) * 500; |
| 54 | return 0; |
| 55 | case hwmon_temp_max: |
| 56 | pci_read_config_word(pdev, REG_TSTHRHI, &tsthr); |
| 57 | *val = tsthr * 500; |
| 58 | return 0; |
| 59 | case hwmon_temp_max_hyst: |
| 60 | pci_read_config_word(pdev, REG_TSTHRLO, &tsthr); |
| 61 | *val = tsthr * 500; |
| 62 | return 0; |
| 63 | case hwmon_temp_crit: |
| 64 | pci_read_config_word(pdev, REG_TSTHRCATA, &tsthr); |
| 65 | *val = tsthr * 500; |
| 66 | return 0; |
| 67 | case hwmon_temp_max_alarm: |
| 68 | pci_read_config_byte(pdev, REG_CTSTS, &ctsts); |
| 69 | *val = !!(ctsts & BIT(1)); |
| 70 | return 0; |
| 71 | case hwmon_temp_crit_alarm: |
| 72 | pci_read_config_byte(pdev, REG_CTSTS, &ctsts); |
| 73 | *val = !!(ctsts & BIT(0)); |
| 74 | return 0; |
| 75 | default: |
| 76 | break; |
| 77 | } |
| 78 | break; |
| 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | return -EOPNOTSUPP; |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 86 | static const struct hwmon_ops i5500_ops = { |
| 87 | .is_visible = i5500_is_visible, |
| 88 | .read = i5500_read, |
| 89 | }; |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 90 | |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 91 | static const struct hwmon_channel_info *i5500_info[] = { |
| 92 | HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), |
| 93 | HWMON_CHANNEL_INFO(temp, |
| 94 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | HWMON_T_CRIT | |
| 95 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM |
| 96 | ), |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 97 | NULL |
| 98 | }; |
| 99 | |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 100 | static const struct hwmon_chip_info i5500_chip_info = { |
| 101 | .ops = &i5500_ops, |
| 102 | .info = i5500_info, |
| 103 | }; |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 104 | |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 105 | static const struct pci_device_id i5500_temp_ids[] = { |
| 106 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) }, |
| 107 | { 0 }, |
| 108 | }; |
| 109 | |
| 110 | MODULE_DEVICE_TABLE(pci, i5500_temp_ids); |
| 111 | |
| 112 | static int i5500_temp_probe(struct pci_dev *pdev, |
| 113 | const struct pci_device_id *id) |
| 114 | { |
| 115 | int err; |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 116 | struct device *hwmon_dev; |
Jean Delvare | e3d9820 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 117 | u32 tstimer; |
| 118 | s8 tsfsc; |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 119 | |
| 120 | err = pci_enable_device(pdev); |
| 121 | if (err) { |
| 122 | dev_err(&pdev->dev, "Failed to enable device\n"); |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 123 | return err; |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Jean Delvare | e3d9820 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 126 | pci_read_config_byte(pdev, REG_TSFSC, &tsfsc); |
| 127 | pci_read_config_dword(pdev, REG_TSTIMER, &tstimer); |
| 128 | if (tsfsc == 0x7F && tstimer == 0x07D30D40) { |
Axel Lin | 86c725e | 2015-01-24 14:16:22 +0100 | [diff] [blame] | 129 | dev_notice(&pdev->dev, "Sensor seems to be disabled\n"); |
Jean Delvare | e3d9820 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 130 | return -ENODEV; |
| 131 | } |
| 132 | |
Armin Wolf | 6665e10 | 2021-08-23 19:07:24 +0200 | [diff] [blame] | 133 | hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, "intel5500", NULL, |
| 134 | &i5500_chip_info, NULL); |
Jean Delvare | b8d48ce | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 135 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static struct pci_driver i5500_temp_driver = { |
| 139 | .name = "i5500_temp", |
| 140 | .id_table = i5500_temp_ids, |
| 141 | .probe = i5500_temp_probe, |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
Axel Lin | aef64d0 | 2015-01-24 14:16:22 +0100 | [diff] [blame] | 144 | module_pci_driver(i5500_temp_driver); |
Jean Delvare | ada0728 | 2015-01-24 14:16:21 +0100 | [diff] [blame] | 145 | |
| 146 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
| 147 | MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver"); |
| 148 | MODULE_LICENSE("GPL"); |