1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
4 *
5 * This file defines the sysfs class "hwmon", for use by sensors drivers.
6 *
7 * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
8 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gfp.h>
16 #include <linux/hwmon.h>
17 #include <linux/i2c.h>
18 #include <linux/idr.h>
19 #include <linux/kstrtox.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/property.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/thermal.h>
27
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/hwmon.h>
30
31 #define HWMON_ID_PREFIX "hwmon"
32 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
33
34 struct hwmon_device {
35 const char *name;
36 const char *label;
37 struct device dev;
38 const struct hwmon_chip_info *chip;
39 struct list_head tzdata;
40 struct attribute_group group;
41 const struct attribute_group **groups;
42 };
43
44 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
45
46 #define MAX_SYSFS_ATTR_NAME_LENGTH 32
47
48 struct hwmon_device_attribute {
49 struct device_attribute dev_attr;
50 const struct hwmon_ops *ops;
51 enum hwmon_sensor_types type;
52 u32 attr;
53 int index;
54 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
55 };
56
57 #define to_hwmon_attr(d) \
58 container_of(d, struct hwmon_device_attribute, dev_attr)
59 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
60
61 /*
62 * Thermal zone information
63 */
64 struct hwmon_thermal_data {
65 struct list_head node; /* hwmon tzdata list entry */
66 struct device *dev; /* Reference to hwmon device */
67 int index; /* sensor index */
68 struct thermal_zone_device *tzd;/* thermal zone device */
69 };
70
71 static ssize_t
name_show(struct device * dev,struct device_attribute * attr,char * buf)72 name_show(struct device *dev, struct device_attribute *attr, char *buf)
73 {
74 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
75 }
76 static DEVICE_ATTR_RO(name);
77
78 static ssize_t
label_show(struct device * dev,struct device_attribute * attr,char * buf)79 label_show(struct device *dev, struct device_attribute *attr, char *buf)
80 {
81 return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
82 }
83 static DEVICE_ATTR_RO(label);
84
85 static struct attribute *hwmon_dev_attrs[] = {
86 &dev_attr_name.attr,
87 &dev_attr_label.attr,
88 NULL
89 };
90
hwmon_dev_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)91 static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
92 struct attribute *attr, int n)
93 {
94 struct device *dev = kobj_to_dev(kobj);
95 struct hwmon_device *hdev = to_hwmon_device(dev);
96
97 if (attr == &dev_attr_name.attr && hdev->name == NULL)
98 return 0;
99
100 if (attr == &dev_attr_label.attr && hdev->label == NULL)
101 return 0;
102
103 return attr->mode;
104 }
105
106 static const struct attribute_group hwmon_dev_attr_group = {
107 .attrs = hwmon_dev_attrs,
108 .is_visible = hwmon_dev_attr_is_visible,
109 };
110
111 static const struct attribute_group *hwmon_dev_attr_groups[] = {
112 &hwmon_dev_attr_group,
113 NULL
114 };
115
hwmon_free_attrs(struct attribute ** attrs)116 static void hwmon_free_attrs(struct attribute **attrs)
117 {
118 int i;
119
120 for (i = 0; attrs[i]; i++) {
121 struct device_attribute *dattr = to_dev_attr(attrs[i]);
122 struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
123
124 kfree(hattr);
125 }
126 kfree(attrs);
127 }
128
hwmon_dev_release(struct device * dev)129 static void hwmon_dev_release(struct device *dev)
130 {
131 struct hwmon_device *hwdev = to_hwmon_device(dev);
132
133 if (hwdev->group.attrs)
134 hwmon_free_attrs(hwdev->group.attrs);
135 kfree(hwdev->groups);
136 kfree(hwdev->label);
137 kfree(hwdev);
138 }
139
140 static const struct class hwmon_class = {
141 .name = "hwmon",
142 .dev_groups = hwmon_dev_attr_groups,
143 .dev_release = hwmon_dev_release,
144 };
145
146 static DEFINE_IDA(hwmon_ida);
147
hwmon_is_visible(const struct hwmon_ops * ops,const void * drvdata,enum hwmon_sensor_types type,u32 attr,int channel)148 static umode_t hwmon_is_visible(const struct hwmon_ops *ops,
149 const void *drvdata,
150 enum hwmon_sensor_types type,
151 u32 attr, int channel)
152 {
153 if (ops->visible)
154 return ops->visible;
155
156 return ops->is_visible(drvdata, type, attr, channel);
157 }
158
159 /* Thermal zone handling */
160
hwmon_thermal_get_temp(struct thermal_zone_device * tz,int * temp)161 static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
162 {
163 struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
164 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
165 int ret;
166 long t;
167
168 ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
169 tdata->index, &t);
170 if (ret < 0)
171 return ret;
172
173 *temp = t;
174
175 return 0;
176 }
177
hwmon_thermal_set_trips(struct thermal_zone_device * tz,int low,int high)178 static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
179 {
180 struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
181 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
182 const struct hwmon_chip_info *chip = hwdev->chip;
183 const struct hwmon_channel_info * const *info = chip->info;
184 unsigned int i;
185 int err;
186
187 if (!chip->ops->write)
188 return 0;
189
190 for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
191 continue;
192
193 if (!info[i])
194 return 0;
195
196 if (info[i]->config[tdata->index] & HWMON_T_MIN) {
197 err = chip->ops->write(tdata->dev, hwmon_temp,
198 hwmon_temp_min, tdata->index, low);
199 if (err && err != -EOPNOTSUPP)
200 return err;
201 }
202
203 if (info[i]->config[tdata->index] & HWMON_T_MAX) {
204 err = chip->ops->write(tdata->dev, hwmon_temp,
205 hwmon_temp_max, tdata->index, high);
206 if (err && err != -EOPNOTSUPP)
207 return err;
208 }
209
210 return 0;
211 }
212
213 static const struct thermal_zone_device_ops hwmon_thermal_ops = {
214 .get_temp = hwmon_thermal_get_temp,
215 .set_trips = hwmon_thermal_set_trips,
216 };
217
hwmon_thermal_remove_sensor(void * data)218 static void hwmon_thermal_remove_sensor(void *data)
219 {
220 list_del(data);
221 }
222
hwmon_thermal_add_sensor(struct device * dev,int index)223 static int hwmon_thermal_add_sensor(struct device *dev, int index)
224 {
225 struct hwmon_device *hwdev = to_hwmon_device(dev);
226 struct hwmon_thermal_data *tdata;
227 struct thermal_zone_device *tzd;
228 int err;
229
230 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
231 if (!tdata)
232 return -ENOMEM;
233
234 tdata->dev = dev;
235 tdata->index = index;
236
237 tzd = devm_thermal_of_zone_register(dev, index, tdata,
238 &hwmon_thermal_ops);
239 if (IS_ERR(tzd)) {
240 if (PTR_ERR(tzd) != -ENODEV)
241 return PTR_ERR(tzd);
242 dev_info(dev, "temp%d_input not attached to any thermal zone\n",
243 index + 1);
244 devm_kfree(dev, tdata);
245 return 0;
246 }
247
248 err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
249 if (err)
250 return err;
251
252 tdata->tzd = tzd;
253 list_add(&tdata->node, &hwdev->tzdata);
254
255 return 0;
256 }
257
hwmon_thermal_register_sensors(struct device * dev)258 static int hwmon_thermal_register_sensors(struct device *dev)
259 {
260 struct hwmon_device *hwdev = to_hwmon_device(dev);
261 const struct hwmon_chip_info *chip = hwdev->chip;
262 const struct hwmon_channel_info * const *info = chip->info;
263 void *drvdata = dev_get_drvdata(dev);
264 int i;
265
266 if (!IS_ENABLED(CONFIG_THERMAL_OF))
267 return 0;
268
269 for (i = 1; info[i]; i++) {
270 int j;
271
272 if (info[i]->type != hwmon_temp)
273 continue;
274
275 for (j = 0; info[i]->config[j]; j++) {
276 int err;
277
278 if (!(info[i]->config[j] & HWMON_T_INPUT) ||
279 !hwmon_is_visible(chip->ops, drvdata, hwmon_temp,
280 hwmon_temp_input, j))
281 continue;
282
283 err = hwmon_thermal_add_sensor(dev, j);
284 if (err)
285 return err;
286 }
287 }
288
289 return 0;
290 }
291
hwmon_thermal_notify(struct device * dev,int index)292 static void hwmon_thermal_notify(struct device *dev, int index)
293 {
294 struct hwmon_device *hwdev = to_hwmon_device(dev);
295 struct hwmon_thermal_data *tzdata;
296
297 if (!IS_ENABLED(CONFIG_THERMAL_OF))
298 return;
299
300 list_for_each_entry(tzdata, &hwdev->tzdata, node) {
301 if (tzdata->index == index) {
302 thermal_zone_device_update(tzdata->tzd,
303 THERMAL_EVENT_UNSPECIFIED);
304 }
305 }
306 }
307
hwmon_attr_base(enum hwmon_sensor_types type)308 static int hwmon_attr_base(enum hwmon_sensor_types type)
309 {
310 if (type == hwmon_in || type == hwmon_intrusion)
311 return 0;
312 return 1;
313 }
314
315 #if IS_REACHABLE(CONFIG_I2C)
316
317 /*
318 * PEC support
319 *
320 * The 'pec' attribute is attached to I2C client devices. It is only provided
321 * if the i2c controller supports PEC.
322 *
323 * The mutex ensures that PEC configuration between i2c device and the hardware
324 * is consistent. Use a single mutex because attribute writes are supposed to be
325 * rare, and maintaining a separate mutex for each hardware monitoring device
326 * would add substantial complexity to the driver for little if any gain.
327 *
328 * The hardware monitoring device is identified as child of the i2c client
329 * device. This assumes that only a single hardware monitoring device is
330 * attached to an i2c client device.
331 */
332
333 static DEFINE_MUTEX(hwmon_pec_mutex);
334
hwmon_match_device(struct device * dev,const void * data)335 static int hwmon_match_device(struct device *dev, const void *data)
336 {
337 return dev->class == &hwmon_class;
338 }
339
pec_show(struct device * dev,struct device_attribute * dummy,char * buf)340 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
341 char *buf)
342 {
343 struct i2c_client *client = to_i2c_client(dev);
344
345 return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
346 }
347
pec_store(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)348 static ssize_t pec_store(struct device *dev, struct device_attribute *devattr,
349 const char *buf, size_t count)
350 {
351 struct i2c_client *client = to_i2c_client(dev);
352 struct hwmon_device *hwdev;
353 struct device *hdev;
354 bool val;
355 int err;
356
357 err = kstrtobool(buf, &val);
358 if (err < 0)
359 return err;
360
361 hdev = device_find_child(dev, NULL, hwmon_match_device);
362 if (!hdev)
363 return -ENODEV;
364
365 mutex_lock(&hwmon_pec_mutex);
366
367 /*
368 * If there is no write function, we assume that chip specific
369 * handling is not required.
370 */
371 hwdev = to_hwmon_device(hdev);
372 if (hwdev->chip->ops->write) {
373 err = hwdev->chip->ops->write(hdev, hwmon_chip, hwmon_chip_pec, 0, val);
374 if (err && err != -EOPNOTSUPP)
375 goto unlock;
376 }
377
378 if (!val)
379 client->flags &= ~I2C_CLIENT_PEC;
380 else
381 client->flags |= I2C_CLIENT_PEC;
382
383 err = count;
384 unlock:
385 mutex_unlock(&hwmon_pec_mutex);
386 put_device(hdev);
387
388 return err;
389 }
390
391 static DEVICE_ATTR_RW(pec);
392
hwmon_remove_pec(void * dev)393 static void hwmon_remove_pec(void *dev)
394 {
395 device_remove_file(dev, &dev_attr_pec);
396 }
397
hwmon_pec_register(struct device * hdev)398 static int hwmon_pec_register(struct device *hdev)
399 {
400 struct i2c_client *client = i2c_verify_client(hdev->parent);
401 int err;
402
403 if (!client)
404 return -EINVAL;
405
406 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC))
407 return 0;
408
409 err = device_create_file(&client->dev, &dev_attr_pec);
410 if (err)
411 return err;
412
413 return devm_add_action_or_reset(hdev, hwmon_remove_pec, &client->dev);
414 }
415
416 #else /* CONFIG_I2C */
hwmon_pec_register(struct device * hdev)417 static int hwmon_pec_register(struct device *hdev)
418 {
419 return -EINVAL;
420 }
421 #endif /* CONFIG_I2C */
422
423 /* sysfs attribute management */
424
hwmon_attr_show(struct device * dev,struct device_attribute * devattr,char * buf)425 static ssize_t hwmon_attr_show(struct device *dev,
426 struct device_attribute *devattr, char *buf)
427 {
428 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
429 long val;
430 int ret;
431
432 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
433 &val);
434 if (ret < 0)
435 return ret;
436
437 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
438 hattr->name, val);
439
440 return sprintf(buf, "%ld\n", val);
441 }
442
hwmon_attr_show_string(struct device * dev,struct device_attribute * devattr,char * buf)443 static ssize_t hwmon_attr_show_string(struct device *dev,
444 struct device_attribute *devattr,
445 char *buf)
446 {
447 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
448 enum hwmon_sensor_types type = hattr->type;
449 const char *s;
450 int ret;
451
452 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
453 hattr->index, &s);
454 if (ret < 0)
455 return ret;
456
457 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
458 hattr->name, s);
459
460 return sprintf(buf, "%s\n", s);
461 }
462
hwmon_attr_store(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)463 static ssize_t hwmon_attr_store(struct device *dev,
464 struct device_attribute *devattr,
465 const char *buf, size_t count)
466 {
467 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
468 long val;
469 int ret;
470
471 ret = kstrtol(buf, 10, &val);
472 if (ret < 0)
473 return ret;
474
475 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
476 val);
477 if (ret < 0)
478 return ret;
479
480 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
481 hattr->name, val);
482
483 return count;
484 }
485
is_string_attr(enum hwmon_sensor_types type,u32 attr)486 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
487 {
488 return (type == hwmon_temp && attr == hwmon_temp_label) ||
489 (type == hwmon_in && attr == hwmon_in_label) ||
490 (type == hwmon_curr && attr == hwmon_curr_label) ||
491 (type == hwmon_power && attr == hwmon_power_label) ||
492 (type == hwmon_energy && attr == hwmon_energy_label) ||
493 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
494 (type == hwmon_fan && attr == hwmon_fan_label);
495 }
496
hwmon_genattr(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int index,const char * template,const struct hwmon_ops * ops)497 static struct attribute *hwmon_genattr(const void *drvdata,
498 enum hwmon_sensor_types type,
499 u32 attr,
500 int index,
501 const char *template,
502 const struct hwmon_ops *ops)
503 {
504 struct hwmon_device_attribute *hattr;
505 struct device_attribute *dattr;
506 struct attribute *a;
507 umode_t mode;
508 const char *name;
509 bool is_string = is_string_attr(type, attr);
510
511 mode = hwmon_is_visible(ops, drvdata, type, attr, index);
512 if (!mode)
513 return ERR_PTR(-ENOENT);
514
515 if ((mode & 0444) && ((is_string && !ops->read_string) ||
516 (!is_string && !ops->read)))
517 return ERR_PTR(-EINVAL);
518 if ((mode & 0222) && !ops->write)
519 return ERR_PTR(-EINVAL);
520
521 hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
522 if (!hattr)
523 return ERR_PTR(-ENOMEM);
524
525 if (type == hwmon_chip) {
526 name = template;
527 } else {
528 scnprintf(hattr->name, sizeof(hattr->name), template,
529 index + hwmon_attr_base(type));
530 name = hattr->name;
531 }
532
533 hattr->type = type;
534 hattr->attr = attr;
535 hattr->index = index;
536 hattr->ops = ops;
537
538 dattr = &hattr->dev_attr;
539 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
540 dattr->store = hwmon_attr_store;
541
542 a = &dattr->attr;
543 sysfs_attr_init(a);
544 a->name = name;
545 a->mode = mode;
546
547 return a;
548 }
549
550 /*
551 * Chip attributes are not attribute templates but actual sysfs attributes.
552 * See hwmon_genattr() for special handling.
553 */
554 static const char * const hwmon_chip_attrs[] = {
555 [hwmon_chip_temp_reset_history] = "temp_reset_history",
556 [hwmon_chip_in_reset_history] = "in_reset_history",
557 [hwmon_chip_curr_reset_history] = "curr_reset_history",
558 [hwmon_chip_power_reset_history] = "power_reset_history",
559 [hwmon_chip_update_interval] = "update_interval",
560 [hwmon_chip_alarms] = "alarms",
561 [hwmon_chip_samples] = "samples",
562 [hwmon_chip_curr_samples] = "curr_samples",
563 [hwmon_chip_in_samples] = "in_samples",
564 [hwmon_chip_power_samples] = "power_samples",
565 [hwmon_chip_temp_samples] = "temp_samples",
566 [hwmon_chip_beep_enable] = "beep_enable",
567 };
568
569 static const char * const hwmon_temp_attr_templates[] = {
570 [hwmon_temp_enable] = "temp%d_enable",
571 [hwmon_temp_input] = "temp%d_input",
572 [hwmon_temp_type] = "temp%d_type",
573 [hwmon_temp_lcrit] = "temp%d_lcrit",
574 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
575 [hwmon_temp_min] = "temp%d_min",
576 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
577 [hwmon_temp_max] = "temp%d_max",
578 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
579 [hwmon_temp_crit] = "temp%d_crit",
580 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
581 [hwmon_temp_emergency] = "temp%d_emergency",
582 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
583 [hwmon_temp_alarm] = "temp%d_alarm",
584 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
585 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
586 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
587 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
588 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
589 [hwmon_temp_fault] = "temp%d_fault",
590 [hwmon_temp_offset] = "temp%d_offset",
591 [hwmon_temp_label] = "temp%d_label",
592 [hwmon_temp_lowest] = "temp%d_lowest",
593 [hwmon_temp_highest] = "temp%d_highest",
594 [hwmon_temp_reset_history] = "temp%d_reset_history",
595 [hwmon_temp_rated_min] = "temp%d_rated_min",
596 [hwmon_temp_rated_max] = "temp%d_rated_max",
597 [hwmon_temp_beep] = "temp%d_beep",
598 };
599
600 static const char * const hwmon_in_attr_templates[] = {
601 [hwmon_in_enable] = "in%d_enable",
602 [hwmon_in_input] = "in%d_input",
603 [hwmon_in_min] = "in%d_min",
604 [hwmon_in_max] = "in%d_max",
605 [hwmon_in_lcrit] = "in%d_lcrit",
606 [hwmon_in_crit] = "in%d_crit",
607 [hwmon_in_average] = "in%d_average",
608 [hwmon_in_lowest] = "in%d_lowest",
609 [hwmon_in_highest] = "in%d_highest",
610 [hwmon_in_reset_history] = "in%d_reset_history",
611 [hwmon_in_label] = "in%d_label",
612 [hwmon_in_alarm] = "in%d_alarm",
613 [hwmon_in_min_alarm] = "in%d_min_alarm",
614 [hwmon_in_max_alarm] = "in%d_max_alarm",
615 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
616 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
617 [hwmon_in_rated_min] = "in%d_rated_min",
618 [hwmon_in_rated_max] = "in%d_rated_max",
619 [hwmon_in_beep] = "in%d_beep",
620 [hwmon_in_fault] = "in%d_fault",
621 };
622
623 static const char * const hwmon_curr_attr_templates[] = {
624 [hwmon_curr_enable] = "curr%d_enable",
625 [hwmon_curr_input] = "curr%d_input",
626 [hwmon_curr_min] = "curr%d_min",
627 [hwmon_curr_max] = "curr%d_max",
628 [hwmon_curr_lcrit] = "curr%d_lcrit",
629 [hwmon_curr_crit] = "curr%d_crit",
630 [hwmon_curr_average] = "curr%d_average",
631 [hwmon_curr_lowest] = "curr%d_lowest",
632 [hwmon_curr_highest] = "curr%d_highest",
633 [hwmon_curr_reset_history] = "curr%d_reset_history",
634 [hwmon_curr_label] = "curr%d_label",
635 [hwmon_curr_alarm] = "curr%d_alarm",
636 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
637 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
638 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
639 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
640 [hwmon_curr_rated_min] = "curr%d_rated_min",
641 [hwmon_curr_rated_max] = "curr%d_rated_max",
642 [hwmon_curr_beep] = "curr%d_beep",
643 };
644
645 static const char * const hwmon_power_attr_templates[] = {
646 [hwmon_power_enable] = "power%d_enable",
647 [hwmon_power_average] = "power%d_average",
648 [hwmon_power_average_interval] = "power%d_average_interval",
649 [hwmon_power_average_interval_max] = "power%d_interval_max",
650 [hwmon_power_average_interval_min] = "power%d_interval_min",
651 [hwmon_power_average_highest] = "power%d_average_highest",
652 [hwmon_power_average_lowest] = "power%d_average_lowest",
653 [hwmon_power_average_max] = "power%d_average_max",
654 [hwmon_power_average_min] = "power%d_average_min",
655 [hwmon_power_input] = "power%d_input",
656 [hwmon_power_input_highest] = "power%d_input_highest",
657 [hwmon_power_input_lowest] = "power%d_input_lowest",
658 [hwmon_power_reset_history] = "power%d_reset_history",
659 [hwmon_power_accuracy] = "power%d_accuracy",
660 [hwmon_power_cap] = "power%d_cap",
661 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
662 [hwmon_power_cap_max] = "power%d_cap_max",
663 [hwmon_power_cap_min] = "power%d_cap_min",
664 [hwmon_power_min] = "power%d_min",
665 [hwmon_power_max] = "power%d_max",
666 [hwmon_power_lcrit] = "power%d_lcrit",
667 [hwmon_power_crit] = "power%d_crit",
668 [hwmon_power_label] = "power%d_label",
669 [hwmon_power_alarm] = "power%d_alarm",
670 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
671 [hwmon_power_min_alarm] = "power%d_min_alarm",
672 [hwmon_power_max_alarm] = "power%d_max_alarm",
673 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
674 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
675 [hwmon_power_rated_min] = "power%d_rated_min",
676 [hwmon_power_rated_max] = "power%d_rated_max",
677 };
678
679 static const char * const hwmon_energy_attr_templates[] = {
680 [hwmon_energy_enable] = "energy%d_enable",
681 [hwmon_energy_input] = "energy%d_input",
682 [hwmon_energy_label] = "energy%d_label",
683 };
684
685 static const char * const hwmon_humidity_attr_templates[] = {
686 [hwmon_humidity_enable] = "humidity%d_enable",
687 [hwmon_humidity_input] = "humidity%d_input",
688 [hwmon_humidity_label] = "humidity%d_label",
689 [hwmon_humidity_min] = "humidity%d_min",
690 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
691 [hwmon_humidity_max] = "humidity%d_max",
692 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
693 [hwmon_humidity_alarm] = "humidity%d_alarm",
694 [hwmon_humidity_fault] = "humidity%d_fault",
695 [hwmon_humidity_rated_min] = "humidity%d_rated_min",
696 [hwmon_humidity_rated_max] = "humidity%d_rated_max",
697 [hwmon_humidity_min_alarm] = "humidity%d_min_alarm",
698 [hwmon_humidity_max_alarm] = "humidity%d_max_alarm",
699 };
700
701 static const char * const hwmon_fan_attr_templates[] = {
702 [hwmon_fan_enable] = "fan%d_enable",
703 [hwmon_fan_input] = "fan%d_input",
704 [hwmon_fan_label] = "fan%d_label",
705 [hwmon_fan_min] = "fan%d_min",
706 [hwmon_fan_max] = "fan%d_max",
707 [hwmon_fan_div] = "fan%d_div",
708 [hwmon_fan_pulses] = "fan%d_pulses",
709 [hwmon_fan_target] = "fan%d_target",
710 [hwmon_fan_alarm] = "fan%d_alarm",
711 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
712 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
713 [hwmon_fan_fault] = "fan%d_fault",
714 [hwmon_fan_beep] = "fan%d_beep",
715 };
716
717 static const char * const hwmon_pwm_attr_templates[] = {
718 [hwmon_pwm_input] = "pwm%d",
719 [hwmon_pwm_enable] = "pwm%d_enable",
720 [hwmon_pwm_mode] = "pwm%d_mode",
721 [hwmon_pwm_freq] = "pwm%d_freq",
722 [hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
723 };
724
725 static const char * const hwmon_intrusion_attr_templates[] = {
726 [hwmon_intrusion_alarm] = "intrusion%d_alarm",
727 [hwmon_intrusion_beep] = "intrusion%d_beep",
728 };
729
730 static const char * const *__templates[] = {
731 [hwmon_chip] = hwmon_chip_attrs,
732 [hwmon_temp] = hwmon_temp_attr_templates,
733 [hwmon_in] = hwmon_in_attr_templates,
734 [hwmon_curr] = hwmon_curr_attr_templates,
735 [hwmon_power] = hwmon_power_attr_templates,
736 [hwmon_energy] = hwmon_energy_attr_templates,
737 [hwmon_humidity] = hwmon_humidity_attr_templates,
738 [hwmon_fan] = hwmon_fan_attr_templates,
739 [hwmon_pwm] = hwmon_pwm_attr_templates,
740 [hwmon_intrusion] = hwmon_intrusion_attr_templates,
741 };
742
743 static const int __templates_size[] = {
744 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
745 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
746 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
747 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
748 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
749 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
750 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
751 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
752 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
753 [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
754 };
755
hwmon_notify_event(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel)756 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
757 u32 attr, int channel)
758 {
759 char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
760 char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
761 char *envp[] = { event, NULL };
762 const char * const *templates;
763 const char *template;
764 int base;
765
766 if (type >= ARRAY_SIZE(__templates))
767 return -EINVAL;
768 if (attr >= __templates_size[type])
769 return -EINVAL;
770
771 templates = __templates[type];
772 template = templates[attr];
773
774 base = hwmon_attr_base(type);
775
776 scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
777 scnprintf(event, sizeof(event), "NAME=%s", sattr);
778 sysfs_notify(&dev->kobj, NULL, sattr);
779 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
780
781 if (type == hwmon_temp)
782 hwmon_thermal_notify(dev, channel);
783
784 return 0;
785 }
786 EXPORT_SYMBOL_GPL(hwmon_notify_event);
787
hwmon_num_channel_attrs(const struct hwmon_channel_info * info)788 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
789 {
790 int i, n;
791
792 for (i = n = 0; info->config[i]; i++)
793 n += hweight32(info->config[i]);
794
795 return n;
796 }
797
hwmon_genattrs(const void * drvdata,struct attribute ** attrs,const struct hwmon_ops * ops,const struct hwmon_channel_info * info)798 static int hwmon_genattrs(const void *drvdata,
799 struct attribute **attrs,
800 const struct hwmon_ops *ops,
801 const struct hwmon_channel_info *info)
802 {
803 const char * const *templates;
804 int template_size;
805 int i, aindex = 0;
806
807 if (info->type >= ARRAY_SIZE(__templates))
808 return -EINVAL;
809
810 templates = __templates[info->type];
811 template_size = __templates_size[info->type];
812
813 for (i = 0; info->config[i]; i++) {
814 u32 attr_mask = info->config[i];
815 u32 attr;
816
817 while (attr_mask) {
818 struct attribute *a;
819
820 attr = __ffs(attr_mask);
821 attr_mask &= ~BIT(attr);
822 if (attr >= template_size || !templates[attr])
823 continue; /* attribute is invisible */
824 a = hwmon_genattr(drvdata, info->type, attr, i,
825 templates[attr], ops);
826 if (IS_ERR(a)) {
827 if (PTR_ERR(a) != -ENOENT)
828 return PTR_ERR(a);
829 continue;
830 }
831 attrs[aindex++] = a;
832 }
833 }
834 return aindex;
835 }
836
837 static struct attribute **
__hwmon_create_attrs(const void * drvdata,const struct hwmon_chip_info * chip)838 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
839 {
840 int ret, i, aindex = 0, nattrs = 0;
841 struct attribute **attrs;
842
843 for (i = 0; chip->info[i]; i++)
844 nattrs += hwmon_num_channel_attrs(chip->info[i]);
845
846 if (nattrs == 0)
847 return ERR_PTR(-EINVAL);
848
849 attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
850 if (!attrs)
851 return ERR_PTR(-ENOMEM);
852
853 for (i = 0; chip->info[i]; i++) {
854 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
855 chip->info[i]);
856 if (ret < 0) {
857 hwmon_free_attrs(attrs);
858 return ERR_PTR(ret);
859 }
860 aindex += ret;
861 }
862
863 return attrs;
864 }
865
866 static struct device *
__hwmon_device_register(struct device * dev,const char * name,void * drvdata,const struct hwmon_chip_info * chip,const struct attribute_group ** groups)867 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
868 const struct hwmon_chip_info *chip,
869 const struct attribute_group **groups)
870 {
871 struct hwmon_device *hwdev;
872 const char *label;
873 struct device *hdev;
874 struct device *tdev = dev;
875 int i, err, id;
876
877 /* Complain about invalid characters in hwmon name attribute */
878 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
879 dev_warn(dev,
880 "hwmon: '%s' is not a valid name attribute, please fix\n",
881 name);
882
883 id = ida_alloc(&hwmon_ida, GFP_KERNEL);
884 if (id < 0)
885 return ERR_PTR(id);
886
887 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
888 if (hwdev == NULL) {
889 err = -ENOMEM;
890 goto ida_remove;
891 }
892
893 hdev = &hwdev->dev;
894
895 if (chip) {
896 struct attribute **attrs;
897 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
898
899 if (groups)
900 for (i = 0; groups[i]; i++)
901 ngroups++;
902
903 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
904 if (!hwdev->groups) {
905 err = -ENOMEM;
906 goto free_hwmon;
907 }
908
909 attrs = __hwmon_create_attrs(drvdata, chip);
910 if (IS_ERR(attrs)) {
911 err = PTR_ERR(attrs);
912 goto free_hwmon;
913 }
914
915 hwdev->group.attrs = attrs;
916 ngroups = 0;
917 hwdev->groups[ngroups++] = &hwdev->group;
918
919 if (groups) {
920 for (i = 0; groups[i]; i++)
921 hwdev->groups[ngroups++] = groups[i];
922 }
923
924 hdev->groups = hwdev->groups;
925 } else {
926 hdev->groups = groups;
927 }
928
929 if (dev && device_property_present(dev, "label")) {
930 err = device_property_read_string(dev, "label", &label);
931 if (err < 0)
932 goto free_hwmon;
933
934 hwdev->label = kstrdup(label, GFP_KERNEL);
935 if (hwdev->label == NULL) {
936 err = -ENOMEM;
937 goto free_hwmon;
938 }
939 }
940
941 hwdev->name = name;
942 hdev->class = &hwmon_class;
943 hdev->parent = dev;
944 while (tdev && !tdev->of_node)
945 tdev = tdev->parent;
946 hdev->of_node = tdev ? tdev->of_node : NULL;
947 hwdev->chip = chip;
948 dev_set_drvdata(hdev, drvdata);
949 dev_set_name(hdev, HWMON_ID_FORMAT, id);
950 err = device_register(hdev);
951 if (err) {
952 put_device(hdev);
953 goto ida_remove;
954 }
955
956 INIT_LIST_HEAD(&hwdev->tzdata);
957
958 if (hdev->of_node && chip && chip->ops->read &&
959 chip->info[0]->type == hwmon_chip) {
960 u32 config = chip->info[0]->config[0];
961
962 if (config & HWMON_C_REGISTER_TZ) {
963 err = hwmon_thermal_register_sensors(hdev);
964 if (err) {
965 device_unregister(hdev);
966 /*
967 * Don't worry about hwdev; hwmon_dev_release(),
968 * called from device_unregister(), will free it.
969 */
970 goto ida_remove;
971 }
972 }
973 if (config & HWMON_C_PEC) {
974 err = hwmon_pec_register(hdev);
975 if (err) {
976 device_unregister(hdev);
977 goto ida_remove;
978 }
979 }
980 }
981
982 return hdev;
983
984 free_hwmon:
985 hwmon_dev_release(hdev);
986 ida_remove:
987 ida_free(&hwmon_ida, id);
988 return ERR_PTR(err);
989 }
990
991 /**
992 * hwmon_device_register_with_groups - register w/ hwmon
993 * @dev: the parent device
994 * @name: hwmon name attribute
995 * @drvdata: driver data to attach to created device
996 * @groups: List of attribute groups to create
997 *
998 * hwmon_device_unregister() must be called when the device is no
999 * longer needed.
1000 *
1001 * Returns the pointer to the new device.
1002 */
1003 struct device *
hwmon_device_register_with_groups(struct device * dev,const char * name,void * drvdata,const struct attribute_group ** groups)1004 hwmon_device_register_with_groups(struct device *dev, const char *name,
1005 void *drvdata,
1006 const struct attribute_group **groups)
1007 {
1008 if (!name)
1009 return ERR_PTR(-EINVAL);
1010
1011 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
1012 }
1013 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
1014
1015 /**
1016 * hwmon_device_register_with_info - register w/ hwmon
1017 * @dev: the parent device (mandatory)
1018 * @name: hwmon name attribute (mandatory)
1019 * @drvdata: driver data to attach to created device (optional)
1020 * @chip: pointer to hwmon chip information (mandatory)
1021 * @extra_groups: pointer to list of additional non-standard attribute groups
1022 * (optional)
1023 *
1024 * hwmon_device_unregister() must be called when the device is no
1025 * longer needed.
1026 *
1027 * Returns the pointer to the new device.
1028 */
1029 struct device *
hwmon_device_register_with_info(struct device * dev,const char * name,void * drvdata,const struct hwmon_chip_info * chip,const struct attribute_group ** extra_groups)1030 hwmon_device_register_with_info(struct device *dev, const char *name,
1031 void *drvdata,
1032 const struct hwmon_chip_info *chip,
1033 const struct attribute_group **extra_groups)
1034 {
1035 if (!dev || !name || !chip)
1036 return ERR_PTR(-EINVAL);
1037
1038 if (!chip->ops || !(chip->ops->visible || chip->ops->is_visible) || !chip->info)
1039 return ERR_PTR(-EINVAL);
1040
1041 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
1042 }
1043 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
1044
1045 /**
1046 * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
1047 * @dev: the parent device
1048 * @name: hwmon name attribute
1049 * @drvdata: driver data to attach to created device
1050 *
1051 * The use of this function is restricted. It is provided for legacy reasons
1052 * and must only be called from the thermal subsystem.
1053 *
1054 * hwmon_device_unregister() must be called when the device is no
1055 * longer needed.
1056 *
1057 * Returns the pointer to the new device.
1058 */
1059 struct device *
hwmon_device_register_for_thermal(struct device * dev,const char * name,void * drvdata)1060 hwmon_device_register_for_thermal(struct device *dev, const char *name,
1061 void *drvdata)
1062 {
1063 if (!name || !dev)
1064 return ERR_PTR(-EINVAL);
1065
1066 return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
1067 }
1068 EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, "HWMON_THERMAL");
1069
1070 /**
1071 * hwmon_device_register - register w/ hwmon
1072 * @dev: the device to register
1073 *
1074 * hwmon_device_unregister() must be called when the device is no
1075 * longer needed.
1076 *
1077 * Returns the pointer to the new device.
1078 */
hwmon_device_register(struct device * dev)1079 struct device *hwmon_device_register(struct device *dev)
1080 {
1081 dev_warn(dev,
1082 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
1083
1084 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
1085 }
1086 EXPORT_SYMBOL_GPL(hwmon_device_register);
1087
1088 /**
1089 * hwmon_device_unregister - removes the previously registered class device
1090 *
1091 * @dev: the class device to destroy
1092 */
hwmon_device_unregister(struct device * dev)1093 void hwmon_device_unregister(struct device *dev)
1094 {
1095 int id;
1096
1097 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
1098 device_unregister(dev);
1099 ida_free(&hwmon_ida, id);
1100 } else
1101 dev_dbg(dev->parent,
1102 "hwmon_device_unregister() failed: bad class ID!\n");
1103 }
1104 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
1105
devm_hwmon_release(struct device * dev,void * res)1106 static void devm_hwmon_release(struct device *dev, void *res)
1107 {
1108 struct device *hwdev = *(struct device **)res;
1109
1110 hwmon_device_unregister(hwdev);
1111 }
1112
1113 /**
1114 * devm_hwmon_device_register_with_groups - register w/ hwmon
1115 * @dev: the parent device
1116 * @name: hwmon name attribute
1117 * @drvdata: driver data to attach to created device
1118 * @groups: List of attribute groups to create
1119 *
1120 * Returns the pointer to the new device. The new device is automatically
1121 * unregistered with the parent device.
1122 */
1123 struct device *
devm_hwmon_device_register_with_groups(struct device * dev,const char * name,void * drvdata,const struct attribute_group ** groups)1124 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
1125 void *drvdata,
1126 const struct attribute_group **groups)
1127 {
1128 struct device **ptr, *hwdev;
1129
1130 if (!dev)
1131 return ERR_PTR(-EINVAL);
1132
1133 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1134 if (!ptr)
1135 return ERR_PTR(-ENOMEM);
1136
1137 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
1138 if (IS_ERR(hwdev))
1139 goto error;
1140
1141 *ptr = hwdev;
1142 devres_add(dev, ptr);
1143 return hwdev;
1144
1145 error:
1146 devres_free(ptr);
1147 return hwdev;
1148 }
1149 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
1150
1151 /**
1152 * devm_hwmon_device_register_with_info - register w/ hwmon
1153 * @dev: the parent device
1154 * @name: hwmon name attribute
1155 * @drvdata: driver data to attach to created device
1156 * @chip: pointer to hwmon chip information
1157 * @extra_groups: pointer to list of driver specific attribute groups
1158 *
1159 * Returns the pointer to the new device. The new device is automatically
1160 * unregistered with the parent device.
1161 */
1162 struct device *
devm_hwmon_device_register_with_info(struct device * dev,const char * name,void * drvdata,const struct hwmon_chip_info * chip,const struct attribute_group ** extra_groups)1163 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1164 void *drvdata,
1165 const struct hwmon_chip_info *chip,
1166 const struct attribute_group **extra_groups)
1167 {
1168 struct device **ptr, *hwdev;
1169
1170 if (!dev)
1171 return ERR_PTR(-EINVAL);
1172
1173 if (!name) {
1174 name = devm_hwmon_sanitize_name(dev, dev_name(dev));
1175 if (IS_ERR(name))
1176 return ERR_CAST(name);
1177 }
1178
1179 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1180 if (!ptr)
1181 return ERR_PTR(-ENOMEM);
1182
1183 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1184 extra_groups);
1185 if (IS_ERR(hwdev))
1186 goto error;
1187
1188 *ptr = hwdev;
1189 devres_add(dev, ptr);
1190
1191 return hwdev;
1192
1193 error:
1194 devres_free(ptr);
1195 return hwdev;
1196 }
1197 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1198
__hwmon_sanitize_name(struct device * dev,const char * old_name)1199 static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
1200 {
1201 char *name, *p;
1202
1203 if (dev)
1204 name = devm_kstrdup(dev, old_name, GFP_KERNEL);
1205 else
1206 name = kstrdup(old_name, GFP_KERNEL);
1207 if (!name)
1208 return ERR_PTR(-ENOMEM);
1209
1210 for (p = name; *p; p++)
1211 if (hwmon_is_bad_char(*p))
1212 *p = '_';
1213
1214 return name;
1215 }
1216
1217 /**
1218 * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
1219 * @name: NUL-terminated name
1220 *
1221 * Allocates a new string where any invalid characters will be replaced
1222 * by an underscore. It is the responsibility of the caller to release
1223 * the memory.
1224 *
1225 * Returns newly allocated name, or ERR_PTR on error.
1226 */
hwmon_sanitize_name(const char * name)1227 char *hwmon_sanitize_name(const char *name)
1228 {
1229 return __hwmon_sanitize_name(NULL, name);
1230 }
1231 EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
1232
1233 /**
1234 * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
1235 * @dev: device to allocate memory for
1236 * @name: NUL-terminated name
1237 *
1238 * Allocates a new string where any invalid characters will be replaced
1239 * by an underscore.
1240 *
1241 * Returns newly allocated name, or ERR_PTR on error.
1242 */
devm_hwmon_sanitize_name(struct device * dev,const char * name)1243 char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
1244 {
1245 if (!dev)
1246 return ERR_PTR(-EINVAL);
1247
1248 return __hwmon_sanitize_name(dev, name);
1249 }
1250 EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
1251
hwmon_pci_quirks(void)1252 static void __init hwmon_pci_quirks(void)
1253 {
1254 #if defined CONFIG_X86 && defined CONFIG_PCI
1255 struct pci_dev *sb;
1256 u16 base;
1257 u8 enable;
1258
1259 /* Open access to 0x295-0x296 on MSI MS-7031 */
1260 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1261 if (sb) {
1262 if (sb->subsystem_vendor == 0x1462 && /* MSI */
1263 sb->subsystem_device == 0x0031) { /* MS-7031 */
1264 pci_read_config_byte(sb, 0x48, &enable);
1265 pci_read_config_word(sb, 0x64, &base);
1266
1267 if (base == 0 && !(enable & BIT(2))) {
1268 dev_info(&sb->dev,
1269 "Opening wide generic port at 0x295\n");
1270 pci_write_config_word(sb, 0x64, 0x295);
1271 pci_write_config_byte(sb, 0x48,
1272 enable | BIT(2));
1273 }
1274 }
1275 pci_dev_put(sb);
1276 }
1277 #endif
1278 }
1279
hwmon_init(void)1280 static int __init hwmon_init(void)
1281 {
1282 int err;
1283
1284 hwmon_pci_quirks();
1285
1286 err = class_register(&hwmon_class);
1287 if (err) {
1288 pr_err("couldn't register hwmon sysfs class\n");
1289 return err;
1290 }
1291 return 0;
1292 }
1293
hwmon_exit(void)1294 static void __exit hwmon_exit(void)
1295 {
1296 class_unregister(&hwmon_class);
1297 }
1298
1299 subsys_initcall(hwmon_init);
1300 module_exit(hwmon_exit);
1301
1302 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1303 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1304 MODULE_LICENSE("GPL");
1305
1306