xref: /openbmc/linux/drivers/hwmon/occ/sysfs.c (revision 6a0be74b)
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 	case 9:
70 		val = header->mode;
71 		break;
72 	default:
73 		return -EINVAL;
74 	}
75 
76 	return sysfs_emit(buf, "%d\n", val);
77 }
78 
79 static ssize_t occ_error_show(struct device *dev,
80 			      struct device_attribute *attr, char *buf)
81 {
82 	struct occ *occ = dev_get_drvdata(dev);
83 
84 	occ_update_response(occ);
85 
86 	return sysfs_emit(buf, "%d\n", occ->error);
87 }
88 
89 static SENSOR_DEVICE_ATTR(occ_master, 0444, occ_sysfs_show, NULL, 0);
90 static SENSOR_DEVICE_ATTR(occ_active, 0444, occ_sysfs_show, NULL, 1);
91 static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp, 0444, occ_sysfs_show, NULL, 2);
92 static SENSOR_DEVICE_ATTR(occ_dvfs_power, 0444, occ_sysfs_show, NULL, 3);
93 static SENSOR_DEVICE_ATTR(occ_mem_throttle, 0444, occ_sysfs_show, NULL, 4);
94 static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop, 0444, occ_sysfs_show, NULL, 5);
95 static SENSOR_DEVICE_ATTR(occ_state, 0444, occ_sysfs_show, NULL, 6);
96 static SENSOR_DEVICE_ATTR(occs_present, 0444, occ_sysfs_show, NULL, 7);
97 static SENSOR_DEVICE_ATTR(occ_ips_status, 0444, occ_sysfs_show, NULL, 8);
98 static SENSOR_DEVICE_ATTR(occ_mode, 0444, occ_sysfs_show, NULL, 9);
99 static DEVICE_ATTR_RO(occ_error);
100 
101 static struct attribute *occ_attributes[] = {
102 	&sensor_dev_attr_occ_master.dev_attr.attr,
103 	&sensor_dev_attr_occ_active.dev_attr.attr,
104 	&sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr,
105 	&sensor_dev_attr_occ_dvfs_power.dev_attr.attr,
106 	&sensor_dev_attr_occ_mem_throttle.dev_attr.attr,
107 	&sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr,
108 	&sensor_dev_attr_occ_state.dev_attr.attr,
109 	&sensor_dev_attr_occs_present.dev_attr.attr,
110 	&sensor_dev_attr_occ_ips_status.dev_attr.attr,
111 	&sensor_dev_attr_occ_mode.dev_attr.attr,
112 	&dev_attr_occ_error.attr,
113 	NULL
114 };
115 
116 static const struct attribute_group occ_sysfs = {
117 	.attrs = occ_attributes,
118 };
119 
120 void occ_sysfs_poll_done(struct occ *occ)
121 {
122 	const char *name;
123 	struct occ_poll_response_header *header =
124 		(struct occ_poll_response_header *)occ->resp.data;
125 
126 	/*
127 	 * On the first poll response, we haven't yet created the sysfs
128 	 * attributes, so don't make any notify calls.
129 	 */
130 	if (!occ->hwmon)
131 		goto done;
132 
133 	if ((header->status & OCC_STAT_MASTER) !=
134 	    (occ->prev_stat & OCC_STAT_MASTER)) {
135 		name = sensor_dev_attr_occ_master.dev_attr.attr.name;
136 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
137 	}
138 
139 	if ((header->status & OCC_STAT_ACTIVE) !=
140 	    (occ->prev_stat & OCC_STAT_ACTIVE)) {
141 		name = sensor_dev_attr_occ_active.dev_attr.attr.name;
142 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
143 	}
144 
145 	if ((header->ext_status & OCC_EXT_STAT_DVFS_OT) !=
146 	    (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_OT)) {
147 		name = sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr.name;
148 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
149 	}
150 
151 	if ((header->ext_status & OCC_EXT_STAT_DVFS_POWER) !=
152 	    (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_POWER)) {
153 		name = sensor_dev_attr_occ_dvfs_power.dev_attr.attr.name;
154 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
155 	}
156 
157 	if ((header->ext_status & OCC_EXT_STAT_MEM_THROTTLE) !=
158 	    (occ->prev_ext_stat & OCC_EXT_STAT_MEM_THROTTLE)) {
159 		name = sensor_dev_attr_occ_mem_throttle.dev_attr.attr.name;
160 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
161 	}
162 
163 	if ((header->ext_status & OCC_EXT_STAT_QUICK_DROP) !=
164 	    (occ->prev_ext_stat & OCC_EXT_STAT_QUICK_DROP)) {
165 		name = sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr.name;
166 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
167 	}
168 
169 	if ((header->status & OCC_STAT_MASTER) &&
170 	    header->occs_present != occ->prev_occs_present) {
171 		name = sensor_dev_attr_occs_present.dev_attr.attr.name;
172 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
173 	}
174 
175 	if (header->ips_status != occ->prev_ips_status) {
176 		name = sensor_dev_attr_occ_ips_status.dev_attr.attr.name;
177 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
178 	}
179 
180 	if (header->mode != occ->prev_mode) {
181 		name = sensor_dev_attr_occ_mode.dev_attr.attr.name;
182 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
183 	}
184 
185 	if (occ->error && occ->error != occ->prev_error) {
186 		name = dev_attr_occ_error.attr.name;
187 		sysfs_notify(&occ->bus_dev->kobj, NULL, name);
188 	}
189 
190 	/* no notifications for OCC state; doesn't indicate error condition */
191 
192 done:
193 	occ->prev_error = occ->error;
194 	occ->prev_stat = header->status;
195 	occ->prev_ext_stat = header->ext_status;
196 	occ->prev_occs_present = header->occs_present;
197 	occ->prev_ips_status = header->ips_status;
198 	occ->prev_mode = header->mode;
199 }
200 
201 int occ_setup_sysfs(struct occ *occ)
202 {
203 	return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs);
204 }
205 
206 void occ_shutdown(struct occ *occ)
207 {
208 	sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs);
209 }
210 EXPORT_SYMBOL_GPL(occ_shutdown);
211