forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/hwmon/k8temp.c
....@@ -1,33 +1,17 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * k8temp.c - Linux kernel module for hardware monitoring
34 *
45 * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
56 *
67 * Inspired from the w83785 and amd756 drivers.
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version.
12
- *
13
- * This program is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- * GNU General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU General Public License
19
- * along with this program; if not, write to the Free Software
20
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
- * 02110-1301 USA.
228 */
239
2410 #include <linux/module.h>
2511 #include <linux/init.h>
2612 #include <linux/slab.h>
27
-#include <linux/jiffies.h>
2813 #include <linux/pci.h>
2914 #include <linux/hwmon.h>
30
-#include <linux/hwmon-sysfs.h>
3115 #include <linux/err.h>
3216 #include <linux/mutex.h>
3317 #include <asm/processor.h>
....@@ -38,108 +22,18 @@
3822 #define SEL_CORE 0x04
3923
4024 struct k8temp_data {
41
- struct device *hwmon_dev;
4225 struct mutex update_lock;
43
- const char *name;
44
- char valid; /* zero until following fields are valid */
45
- unsigned long last_updated; /* in jiffies */
4626
4727 /* registers values */
4828 u8 sensorsp; /* sensor presence bits - SEL_CORE, SEL_PLACE */
49
- u32 temp[2][2]; /* core, place */
5029 u8 swap_core_select; /* meaning of SEL_CORE is inverted */
5130 u32 temp_offset;
5231 };
53
-
54
-static struct k8temp_data *k8temp_update_device(struct device *dev)
55
-{
56
- struct k8temp_data *data = dev_get_drvdata(dev);
57
- struct pci_dev *pdev = to_pci_dev(dev);
58
- u8 tmp;
59
-
60
- mutex_lock(&data->update_lock);
61
-
62
- if (!data->valid
63
- || time_after(jiffies, data->last_updated + HZ)) {
64
- pci_read_config_byte(pdev, REG_TEMP, &tmp);
65
- tmp &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
66
- pci_write_config_byte(pdev, REG_TEMP, tmp);
67
- pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
68
-
69
- if (data->sensorsp & SEL_PLACE) {
70
- tmp |= SEL_PLACE; /* Select sensor 1, core0 */
71
- pci_write_config_byte(pdev, REG_TEMP, tmp);
72
- pci_read_config_dword(pdev, REG_TEMP,
73
- &data->temp[0][1]);
74
- }
75
-
76
- if (data->sensorsp & SEL_CORE) {
77
- tmp &= ~SEL_PLACE; /* Select sensor 0, core1 */
78
- tmp |= SEL_CORE;
79
- pci_write_config_byte(pdev, REG_TEMP, tmp);
80
- pci_read_config_dword(pdev, REG_TEMP,
81
- &data->temp[1][0]);
82
-
83
- if (data->sensorsp & SEL_PLACE) {
84
- tmp |= SEL_PLACE; /* Select sensor 1, core1 */
85
- pci_write_config_byte(pdev, REG_TEMP, tmp);
86
- pci_read_config_dword(pdev, REG_TEMP,
87
- &data->temp[1][1]);
88
- }
89
- }
90
-
91
- data->last_updated = jiffies;
92
- data->valid = 1;
93
- }
94
-
95
- mutex_unlock(&data->update_lock);
96
- return data;
97
-}
98
-
99
-/*
100
- * Sysfs stuff
101
- */
102
-
103
-static ssize_t name_show(struct device *dev, struct device_attribute
104
- *devattr, char *buf)
105
-{
106
- struct k8temp_data *data = dev_get_drvdata(dev);
107
-
108
- return sprintf(buf, "%s\n", data->name);
109
-}
110
-
111
-
112
-static ssize_t show_temp(struct device *dev,
113
- struct device_attribute *devattr, char *buf)
114
-{
115
- struct sensor_device_attribute_2 *attr =
116
- to_sensor_dev_attr_2(devattr);
117
- int core = attr->nr;
118
- int place = attr->index;
119
- int temp;
120
- struct k8temp_data *data = k8temp_update_device(dev);
121
-
122
- if (data->swap_core_select && (data->sensorsp & SEL_CORE))
123
- core = core ? 0 : 1;
124
-
125
- temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
126
-
127
- return sprintf(buf, "%d\n", temp);
128
-}
129
-
130
-/* core, place */
131
-
132
-static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
133
-static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
134
-static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0);
135
-static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1);
136
-static DEVICE_ATTR_RO(name);
13732
13833 static const struct pci_device_id k8temp_ids[] = {
13934 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
14035 { 0 },
14136 };
142
-
14337 MODULE_DEVICE_TABLE(pci, k8temp_ids);
14438
14539 static int is_rev_g_desktop(u8 model)
....@@ -173,14 +67,76 @@
17367 return 1;
17468 }
17569
70
+static umode_t
71
+k8temp_is_visible(const void *drvdata, enum hwmon_sensor_types type,
72
+ u32 attr, int channel)
73
+{
74
+ const struct k8temp_data *data = drvdata;
75
+
76
+ if ((channel & 1) && !(data->sensorsp & SEL_PLACE))
77
+ return 0;
78
+
79
+ if ((channel & 2) && !(data->sensorsp & SEL_CORE))
80
+ return 0;
81
+
82
+ return 0444;
83
+}
84
+
85
+static int
86
+k8temp_read(struct device *dev, enum hwmon_sensor_types type,
87
+ u32 attr, int channel, long *val)
88
+{
89
+ struct k8temp_data *data = dev_get_drvdata(dev);
90
+ struct pci_dev *pdev = to_pci_dev(dev->parent);
91
+ int core, place;
92
+ u32 temp;
93
+ u8 tmp;
94
+
95
+ core = (channel >> 1) & 1;
96
+ place = channel & 1;
97
+
98
+ core ^= data->swap_core_select;
99
+
100
+ mutex_lock(&data->update_lock);
101
+ pci_read_config_byte(pdev, REG_TEMP, &tmp);
102
+ tmp &= ~(SEL_PLACE | SEL_CORE);
103
+ if (core)
104
+ tmp |= SEL_CORE;
105
+ if (place)
106
+ tmp |= SEL_PLACE;
107
+ pci_write_config_byte(pdev, REG_TEMP, tmp);
108
+ pci_read_config_dword(pdev, REG_TEMP, &temp);
109
+ mutex_unlock(&data->update_lock);
110
+
111
+ *val = TEMP_FROM_REG(temp) + data->temp_offset;
112
+
113
+ return 0;
114
+}
115
+
116
+static const struct hwmon_ops k8temp_ops = {
117
+ .is_visible = k8temp_is_visible,
118
+ .read = k8temp_read,
119
+};
120
+
121
+static const struct hwmon_channel_info *k8temp_info[] = {
122
+ HWMON_CHANNEL_INFO(temp,
123
+ HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT),
124
+ NULL
125
+};
126
+
127
+static const struct hwmon_chip_info k8temp_chip_info = {
128
+ .ops = &k8temp_ops,
129
+ .info = k8temp_info,
130
+};
131
+
176132 static int k8temp_probe(struct pci_dev *pdev,
177133 const struct pci_device_id *id)
178134 {
179
- int err;
180135 u8 scfg;
181136 u32 temp;
182137 u8 model, stepping;
183138 struct k8temp_data *data;
139
+ struct device *hwmon_dev;
184140
185141 data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
186142 if (!data)
....@@ -245,86 +201,21 @@
245201 data->sensorsp &= ~SEL_CORE;
246202 }
247203
248
- data->name = "k8temp";
249204 mutex_init(&data->update_lock);
250
- pci_set_drvdata(pdev, data);
251205
252
- /* Register sysfs hooks */
253
- err = device_create_file(&pdev->dev,
254
- &sensor_dev_attr_temp1_input.dev_attr);
255
- if (err)
256
- goto exit_remove;
206
+ hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
207
+ "k8temp",
208
+ data,
209
+ &k8temp_chip_info,
210
+ NULL);
257211
258
- /* sensor can be changed and reports something */
259
- if (data->sensorsp & SEL_PLACE) {
260
- err = device_create_file(&pdev->dev,
261
- &sensor_dev_attr_temp2_input.dev_attr);
262
- if (err)
263
- goto exit_remove;
264
- }
265
-
266
- /* core can be changed and reports something */
267
- if (data->sensorsp & SEL_CORE) {
268
- err = device_create_file(&pdev->dev,
269
- &sensor_dev_attr_temp3_input.dev_attr);
270
- if (err)
271
- goto exit_remove;
272
- if (data->sensorsp & SEL_PLACE) {
273
- err = device_create_file(&pdev->dev,
274
- &sensor_dev_attr_temp4_input.
275
- dev_attr);
276
- if (err)
277
- goto exit_remove;
278
- }
279
- }
280
-
281
- err = device_create_file(&pdev->dev, &dev_attr_name);
282
- if (err)
283
- goto exit_remove;
284
-
285
- data->hwmon_dev = hwmon_device_register(&pdev->dev);
286
-
287
- if (IS_ERR(data->hwmon_dev)) {
288
- err = PTR_ERR(data->hwmon_dev);
289
- goto exit_remove;
290
- }
291
-
292
- return 0;
293
-
294
-exit_remove:
295
- device_remove_file(&pdev->dev,
296
- &sensor_dev_attr_temp1_input.dev_attr);
297
- device_remove_file(&pdev->dev,
298
- &sensor_dev_attr_temp2_input.dev_attr);
299
- device_remove_file(&pdev->dev,
300
- &sensor_dev_attr_temp3_input.dev_attr);
301
- device_remove_file(&pdev->dev,
302
- &sensor_dev_attr_temp4_input.dev_attr);
303
- device_remove_file(&pdev->dev, &dev_attr_name);
304
- return err;
305
-}
306
-
307
-static void k8temp_remove(struct pci_dev *pdev)
308
-{
309
- struct k8temp_data *data = pci_get_drvdata(pdev);
310
-
311
- hwmon_device_unregister(data->hwmon_dev);
312
- device_remove_file(&pdev->dev,
313
- &sensor_dev_attr_temp1_input.dev_attr);
314
- device_remove_file(&pdev->dev,
315
- &sensor_dev_attr_temp2_input.dev_attr);
316
- device_remove_file(&pdev->dev,
317
- &sensor_dev_attr_temp3_input.dev_attr);
318
- device_remove_file(&pdev->dev,
319
- &sensor_dev_attr_temp4_input.dev_attr);
320
- device_remove_file(&pdev->dev, &dev_attr_name);
212
+ return PTR_ERR_OR_ZERO(hwmon_dev);
321213 }
322214
323215 static struct pci_driver k8temp_driver = {
324216 .name = "k8temp",
325217 .id_table = k8temp_ids,
326218 .probe = k8temp_probe,
327
- .remove = k8temp_remove,
328219 };
329220
330221 module_pci_driver(k8temp_driver);