Skip to content

Commit abae018

Browse files
groecktsbogend
authored andcommitted
MIPS: Loongson: Use hwmon_device_register_with_groups() to register hwmon
Calling hwmon_device_register_with_info() with NULL dev and/or chip information parameters is an ABI abuse and not a real conversion to the new API. Also, the code creates sysfs attributes _after_ creating the hwmon device, which is racy and unsupported to start with. On top of that, the removal code tries to remove the name attribute which is owned by the hwmon core. Use hwmon_device_register_with_groups() to register the hwmon device instead. In the future, the hwmon subsystem will reject calls to hwmon_device_register_with_info with NULL dev or chip/info parameters. Without this patch, the hwmon device will fail to register. Fixes: f59dc51 ("MIPS: Loongson: Fix boot warning about hwmon_device_register()") Cc: Zhi Li <lizhi01@loongson.cn> Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
1 parent 85663a8 commit abae018

1 file changed

Lines changed: 41 additions & 86 deletions

File tree

drivers/platform/mips/cpu_hwmon.c

Lines changed: 41 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -55,55 +55,6 @@ int loongson3_cpu_temp(int cpu)
5555
static int nr_packages;
5656
static struct device *cpu_hwmon_dev;
5757

58-
static SENSOR_DEVICE_ATTR(name, 0444, NULL, NULL, 0);
59-
60-
static struct attribute *cpu_hwmon_attributes[] = {
61-
&sensor_dev_attr_name.dev_attr.attr,
62-
NULL
63-
};
64-
65-
/* Hwmon device attribute group */
66-
static struct attribute_group cpu_hwmon_attribute_group = {
67-
.attrs = cpu_hwmon_attributes,
68-
};
69-
70-
static ssize_t get_cpu_temp(struct device *dev,
71-
struct device_attribute *attr, char *buf);
72-
static ssize_t cpu_temp_label(struct device *dev,
73-
struct device_attribute *attr, char *buf);
74-
75-
static SENSOR_DEVICE_ATTR(temp1_input, 0444, get_cpu_temp, NULL, 1);
76-
static SENSOR_DEVICE_ATTR(temp1_label, 0444, cpu_temp_label, NULL, 1);
77-
static SENSOR_DEVICE_ATTR(temp2_input, 0444, get_cpu_temp, NULL, 2);
78-
static SENSOR_DEVICE_ATTR(temp2_label, 0444, cpu_temp_label, NULL, 2);
79-
static SENSOR_DEVICE_ATTR(temp3_input, 0444, get_cpu_temp, NULL, 3);
80-
static SENSOR_DEVICE_ATTR(temp3_label, 0444, cpu_temp_label, NULL, 3);
81-
static SENSOR_DEVICE_ATTR(temp4_input, 0444, get_cpu_temp, NULL, 4);
82-
static SENSOR_DEVICE_ATTR(temp4_label, 0444, cpu_temp_label, NULL, 4);
83-
84-
static const struct attribute *hwmon_cputemp[4][3] = {
85-
{
86-
&sensor_dev_attr_temp1_input.dev_attr.attr,
87-
&sensor_dev_attr_temp1_label.dev_attr.attr,
88-
NULL
89-
},
90-
{
91-
&sensor_dev_attr_temp2_input.dev_attr.attr,
92-
&sensor_dev_attr_temp2_label.dev_attr.attr,
93-
NULL
94-
},
95-
{
96-
&sensor_dev_attr_temp3_input.dev_attr.attr,
97-
&sensor_dev_attr_temp3_label.dev_attr.attr,
98-
NULL
99-
},
100-
{
101-
&sensor_dev_attr_temp4_input.dev_attr.attr,
102-
&sensor_dev_attr_temp4_label.dev_attr.attr,
103-
NULL
104-
}
105-
};
106-
10758
static ssize_t cpu_temp_label(struct device *dev,
10859
struct device_attribute *attr, char *buf)
10960
{
@@ -121,24 +72,47 @@ static ssize_t get_cpu_temp(struct device *dev,
12172
return sprintf(buf, "%d\n", value);
12273
}
12374

124-
static int create_sysfs_cputemp_files(struct kobject *kobj)
125-
{
126-
int i, ret = 0;
127-
128-
for (i = 0; i < nr_packages; i++)
129-
ret = sysfs_create_files(kobj, hwmon_cputemp[i]);
75+
static SENSOR_DEVICE_ATTR(temp1_input, 0444, get_cpu_temp, NULL, 1);
76+
static SENSOR_DEVICE_ATTR(temp1_label, 0444, cpu_temp_label, NULL, 1);
77+
static SENSOR_DEVICE_ATTR(temp2_input, 0444, get_cpu_temp, NULL, 2);
78+
static SENSOR_DEVICE_ATTR(temp2_label, 0444, cpu_temp_label, NULL, 2);
79+
static SENSOR_DEVICE_ATTR(temp3_input, 0444, get_cpu_temp, NULL, 3);
80+
static SENSOR_DEVICE_ATTR(temp3_label, 0444, cpu_temp_label, NULL, 3);
81+
static SENSOR_DEVICE_ATTR(temp4_input, 0444, get_cpu_temp, NULL, 4);
82+
static SENSOR_DEVICE_ATTR(temp4_label, 0444, cpu_temp_label, NULL, 4);
13083

131-
return ret;
132-
}
84+
static struct attribute *cpu_hwmon_attributes[] = {
85+
&sensor_dev_attr_temp1_input.dev_attr.attr,
86+
&sensor_dev_attr_temp1_label.dev_attr.attr,
87+
&sensor_dev_attr_temp2_input.dev_attr.attr,
88+
&sensor_dev_attr_temp2_label.dev_attr.attr,
89+
&sensor_dev_attr_temp3_input.dev_attr.attr,
90+
&sensor_dev_attr_temp3_label.dev_attr.attr,
91+
&sensor_dev_attr_temp4_input.dev_attr.attr,
92+
&sensor_dev_attr_temp4_label.dev_attr.attr,
93+
NULL
94+
};
13395

134-
static void remove_sysfs_cputemp_files(struct kobject *kobj)
96+
static umode_t cpu_hwmon_is_visible(struct kobject *kobj,
97+
struct attribute *attr, int i)
13598
{
136-
int i;
99+
int id = i / 2;
137100

138-
for (i = 0; i < nr_packages; i++)
139-
sysfs_remove_files(kobj, hwmon_cputemp[i]);
101+
if (id < nr_packages)
102+
return attr->mode;
103+
return 0;
140104
}
141105

106+
static struct attribute_group cpu_hwmon_group = {
107+
.attrs = cpu_hwmon_attributes,
108+
.is_visible = cpu_hwmon_is_visible,
109+
};
110+
111+
static const struct attribute_group *cpu_hwmon_groups[] = {
112+
&cpu_hwmon_group,
113+
NULL
114+
};
115+
142116
#define CPU_THERMAL_THRESHOLD 90000
143117
static struct delayed_work thermal_work;
144118

@@ -159,50 +133,31 @@ static void do_thermal_timer(struct work_struct *work)
159133

160134
static int __init loongson_hwmon_init(void)
161135
{
162-
int ret;
163-
164136
pr_info("Loongson Hwmon Enter...\n");
165137

166138
if (cpu_has_csr())
167139
csr_temp_enable = csr_readl(LOONGSON_CSR_FEATURES) &
168140
LOONGSON_CSRF_TEMP;
169141

170-
cpu_hwmon_dev = hwmon_device_register_with_info(NULL, "cpu_hwmon", NULL, NULL, NULL);
171-
if (IS_ERR(cpu_hwmon_dev)) {
172-
ret = PTR_ERR(cpu_hwmon_dev);
173-
pr_err("hwmon_device_register fail!\n");
174-
goto fail_hwmon_device_register;
175-
}
176-
177142
nr_packages = loongson_sysconf.nr_cpus /
178143
loongson_sysconf.cores_per_package;
179144

180-
ret = create_sysfs_cputemp_files(&cpu_hwmon_dev->kobj);
181-
if (ret) {
182-
pr_err("fail to create cpu temperature interface!\n");
183-
goto fail_create_sysfs_cputemp_files;
145+
cpu_hwmon_dev = hwmon_device_register_with_groups(NULL, "cpu_hwmon",
146+
NULL, cpu_hwmon_groups);
147+
if (IS_ERR(cpu_hwmon_dev)) {
148+
pr_err("hwmon_device_register fail!\n");
149+
return PTR_ERR(cpu_hwmon_dev);
184150
}
185151

186152
INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer);
187153
schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000));
188154

189-
return ret;
190-
191-
fail_create_sysfs_cputemp_files:
192-
sysfs_remove_group(&cpu_hwmon_dev->kobj,
193-
&cpu_hwmon_attribute_group);
194-
hwmon_device_unregister(cpu_hwmon_dev);
195-
196-
fail_hwmon_device_register:
197-
return ret;
155+
return 0;
198156
}
199157

200158
static void __exit loongson_hwmon_exit(void)
201159
{
202160
cancel_delayed_work_sync(&thermal_work);
203-
remove_sysfs_cputemp_files(&cpu_hwmon_dev->kobj);
204-
sysfs_remove_group(&cpu_hwmon_dev->kobj,
205-
&cpu_hwmon_attribute_group);
206161
hwmon_device_unregister(cpu_hwmon_dev);
207162
}
208163

0 commit comments

Comments
 (0)