2 * net/dsa/hwmon.c - HWMON subsystem support
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/ctype.h>
11 #include <linux/hwmon.h>
16 static ssize_t temp1_input_show(struct device *dev,
17 struct device_attribute *attr, char *buf)
19 struct dsa_switch *ds = dev_get_drvdata(dev);
22 ret = ds->ops->get_temp(ds, &temp);
26 return sprintf(buf, "%d\n", temp * 1000);
28 static DEVICE_ATTR_RO(temp1_input);
30 static ssize_t temp1_max_show(struct device *dev,
31 struct device_attribute *attr, char *buf)
33 struct dsa_switch *ds = dev_get_drvdata(dev);
36 ret = ds->ops->get_temp_limit(ds, &temp);
40 return sprintf(buf, "%d\n", temp * 1000);
43 static ssize_t temp1_max_store(struct device *dev,
44 struct device_attribute *attr, const char *buf,
47 struct dsa_switch *ds = dev_get_drvdata(dev);
50 ret = kstrtoint(buf, 0, &temp);
54 ret = ds->ops->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
60 static DEVICE_ATTR_RW(temp1_max);
62 static ssize_t temp1_max_alarm_show(struct device *dev,
63 struct device_attribute *attr, char *buf)
65 struct dsa_switch *ds = dev_get_drvdata(dev);
69 ret = ds->ops->get_temp_alarm(ds, &alarm);
73 return sprintf(buf, "%d\n", alarm);
75 static DEVICE_ATTR_RO(temp1_max_alarm);
77 static struct attribute *dsa_hwmon_attrs[] = {
78 &dev_attr_temp1_input.attr, /* 0 */
79 &dev_attr_temp1_max.attr, /* 1 */
80 &dev_attr_temp1_max_alarm.attr, /* 2 */
84 static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
85 struct attribute *attr, int index)
87 struct device *dev = container_of(kobj, struct device, kobj);
88 struct dsa_switch *ds = dev_get_drvdata(dev);
89 const struct dsa_switch_ops *ops = ds->ops;
90 umode_t mode = attr->mode;
93 if (!ops->get_temp_limit)
95 else if (!ops->set_temp_limit)
97 } else if (index == 2 && !ops->get_temp_alarm) {
103 static const struct attribute_group dsa_hwmon_group = {
104 .attrs = dsa_hwmon_attrs,
105 .is_visible = dsa_hwmon_attrs_visible,
107 __ATTRIBUTE_GROUPS(dsa_hwmon);
109 void dsa_hwmon_register(struct dsa_switch *ds)
111 const char *netname = netdev_name(ds->dst->master_netdev);
112 char hname[IFNAMSIZ + 1];
115 /* If the switch provides temperature accessors, register with hardware
116 * monitoring subsystem. Treat registration error as non-fatal.
118 if (!ds->ops->get_temp)
121 /* Create valid hwmon 'name' attribute */
122 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
123 if (isalnum(netname[i]))
124 hname[j++] = netname[i];
127 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d", hname,
129 ds->hwmon_dev = hwmon_device_register_with_groups(NULL, ds->hwmon_name,
130 ds, dsa_hwmon_groups);
131 if (IS_ERR(ds->hwmon_dev)) {
132 pr_warn("DSA: failed to register HWMON subsystem for switch %d\n",
134 ds->hwmon_dev = NULL;
136 pr_info("DSA: registered HWMON subsystem for switch %d\n",
141 void dsa_hwmon_unregister(struct dsa_switch *ds)
144 hwmon_device_unregister(ds->hwmon_dev);
145 ds->hwmon_dev = NULL;