xref: /openbmc/linux/drivers/hwmon/occ/sysfs.c (revision 6109c3e1)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright IBM Corp 2019
3 
4 #include <linux/bitops.h>
5 #include <linux/device.h>
6 #include <linux/export.h>
7 #include <linux/hwmon-sysfs.h>
8 #include <linux/kernel.h>
9 #include <linux/sysfs.h>
10 
11 #include "common.h"
12 
13 /* OCC status register */
14 #define OCC_STAT_MASTER			BIT(7)
15 #define OCC_STAT_ACTIVE			BIT(0)
16 
17 /* OCC extended status register */
18 #define OCC_EXT_STAT_DVFS_OT		BIT(7)
19 #define OCC_EXT_STAT_DVFS_POWER		BIT(6)
20 #define OCC_EXT_STAT_MEM_THROTTLE	BIT(5)
21 #define OCC_EXT_STAT_QUICK_DROP		BIT(4)
22 
23 static ssize_t occ_sysfs_show(struct device *dev,
24 			      struct device_attribute *attr, char *buf)
25 {
26 	int rc;
27 	int val = 0;
28 	struct occ *occ = dev_get_drvdata(dev);
29 	struct occ_poll_response_header *header;
30 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
31 
32 	rc = occ_update_response(occ);
33 	if (rc)
34 		return rc;
35 
36 	header = (struct occ_poll_response_header *)occ->resp.data;
37 
38 	switch (sattr->index) {
39 	case 0:
40 		val = !!(header->status & OCC_STAT_MASTER);
41 		break;
42 	case 1:
43 		val = !!(header->status & OCC_STAT_ACTIVE);
44 		break;
45 	case 2:
46 		val = !!(header->ext_status & OCC_EXT_STAT_DVFS_OT);
47 		break;
48 	case 3:
49 		val = !!(header->ext_status & OCC_EXT_STAT_DVFS_POWER);
50 		break;
51 	case 4:
52 		val = !!(header->ext_status & OCC_EXT_STAT_MEM_THROTTLE);
53 		break;
54 	case 5:
55 		val = !!(header->ext_status & OCC_EXT_STAT_QUICK_DROP);
56 		break;
57 	case 6:
58 		val = header->occ_state;
59 		break;
60 	case 7:
61 		if (header->status & OCC_STAT_MASTER)
62 			val = hweight8(header->occs_present);
63 		else
64 			val = 1;
65 		break;
66 	case 8:
67 		val = header->ips_status;
68 		break;
69 	default:
70 		return -EINVAL;
71 	}
72 
73 	return sysfs_emit(buf, "%d\n", val);
74 }
75 
76 static ssize_t occ_error_show(struct device *dev,
77 			      struct device_attribute *attr, char *buf)
78 {
79 	struct occ *occ = dev_get_drvdata(dev);
80 
81 	occ_update_response(occ);
82 
83 	return sysfs_emit(buf, "%d\n", occ->error);
84 }
85 
86 static SENSOR_DEVICE_ATTR(occ_master, 0444, occ_sysfs_show, NULL, 0);
87 static SENSOR_DEVICE_ATTR(occ_active, 0444, occ_sysfs_show, NULL, 1);
88 static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp, 0444, occ_sysfs_show, NULL, 2);
89 static SENSOR_DEVICE_ATTR(occ_dvfs_power, 0444, occ_sysfs_show, NULL, 3);
90 static SENSOR_DEVICE_ATTR(occ_mem_throttle, 0444, occ_sysfs_show, NULL, 4);
91 static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop, 0444, occ_sysfs_show, NULL, 5);
92 static SENSOR_DEVICE_ATTR(occ_state, 0444, occ_sysfs_show, NULL, 6);
93 static SENSOR_DEVICE_ATTR(occs_present, 0444, occ_sysfs_show, NULL, 7);
94 static SENSOR_DEVICE_ATTR(occ_ips_status, 0444, occ_sysfs_show, NULL, 8);
95 static DEVICE_ATTR_RO(occ_error);
96 
97 static struct attribute *occ_attributes[] = {
98 	&sensor_dev_attr_occ_master.dev_attr.attr,
99 	&sensor_dev_attr_occ_active.dev_attr.attr,
100 	&sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr,
101 	&sensor_dev_attr_occ_dvfs_power.dev_attr.attr,
102 	&sensor_dev_attr_occ_mem_throttle.dev_attr.attr,
103 	&sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr,
104 	&sensor_dev_attr_occ_state.dev_attr.attr,
105 	&sensor_dev_attr_occs_present.dev_attr.attr,
106 	&sensor_dev_attr_occ_ips_status.dev_attr.attr,
107 	&dev_attr_occ_error.attr,
108 	NULL
109 };
110 
111 static const struct attribute_group occ_sysfs = {
112 	.attrs = occ_attributes,
113 };
114 
115 void occ_sysfs_poll_done(struct occ *occ)
116 {
117 	const char *name;
118 	struct occ_poll_response_header *header =
119 		(struct occ_poll_response_header *)occ->resp.data;
120 
121 	/*
122 	 * On the first poll response, we haven't yet created the sysfs
123 	 * attributes, so don't make any notify calls.
124 	 */
125 	if (!occ->hwmon)
126 		goto done;
127 
128 	if ((header->status & OCC_STAT_MASTER) !=
129 	    (occ->prev_stat & OCC_STAT_MASTER)) {
130 		name = sensor_dev_attr_occ_master.dev_attr.attr.name;
131 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
132 	}
133 
134 	if ((header->status & OCC_STAT_ACTIVE) !=
135 	    (occ->prev_stat & OCC_STAT_ACTIVE)) {
136 		name = sensor_dev_attr_occ_active.dev_attr.attr.name;
137 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
138 	}
139 
140 	if ((header->ext_status & OCC_EXT_STAT_DVFS_OT) !=
141 	    (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_OT)) {
142 		name = sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr.name;
143 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
144 	}
145 
146 	if ((header->ext_status & OCC_EXT_STAT_DVFS_POWER) !=
147 	    (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_POWER)) {
148 		name = sensor_dev_attr_occ_dvfs_power.dev_attr.attr.name;
149 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
150 	}
151 
152 	if ((header->ext_status & OCC_EXT_STAT_MEM_THROTTLE) !=
153 	    (occ->prev_ext_stat & OCC_EXT_STAT_MEM_THROTTLE)) {
154 		name = sensor_dev_attr_occ_mem_throttle.dev_attr.attr.name;
155 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
156 	}
157 
158 	if ((header->ext_status & OCC_EXT_STAT_QUICK_DROP) !=
159 	    (occ->prev_ext_stat & OCC_EXT_STAT_QUICK_DROP)) {
160 		name = sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr.name;
161 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
162 	}
163 
164 	if ((header->status & OCC_STAT_MASTER) &&
165 	    header->occs_present != occ->prev_occs_present) {
166 		name = sensor_dev_attr_occs_present.dev_attr.attr.name;
167 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
168 	}
169 
170 	if (header->ips_status != occ->prev_ips_status) {
171 		name = sensor_dev_attr_occ_ips_status.dev_attr.attr.name;
172 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
173 	}
174 
175 	if (occ->error && occ->error != occ->prev_error) {
176 		name = dev_attr_occ_error.attr.name;
177 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
178 	}
179 
180 	/* no notifications for OCC state; doesn't indicate error condition */
181 
182 done:
183 	occ->prev_error = occ->error;
184 	occ->prev_stat = header->status;
185 	occ->prev_ext_stat = header->ext_status;
186 	occ->prev_occs_present = header->occs_present;
187 	occ->prev_ips_status = header->ips_status;
188 }
189 
190 int occ_setup_sysfs(struct occ *occ)
191 {
192 	return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs);
193 }
194 
195 void occ_shutdown(struct occ *occ)
196 {
197 	sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs);
198 }
199 EXPORT_SYMBOL_GPL(occ_shutdown);
200