1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright IBM Corp 2019
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>
13 /* OCC status register */
14 #define OCC_STAT_MASTER BIT(7)
15 #define OCC_STAT_ACTIVE BIT(0)
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 #define OCC_EXT_STAT_DVFS_VDD BIT(3)
23 #define OCC_EXT_STAT_GPU_THROTTLE GENMASK(2, 0)
25 static ssize_t occ_sysfs_show(struct device *dev,
26 struct device_attribute *attr, char *buf)
30 struct occ *occ = dev_get_drvdata(dev);
31 struct occ_poll_response_header *header;
32 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
34 rc = occ_update_response(occ);
38 header = (struct occ_poll_response_header *)occ->resp.data;
40 switch (sattr->index) {
42 val = !!(header->status & OCC_STAT_MASTER);
45 val = !!(header->status & OCC_STAT_ACTIVE);
48 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_OT);
51 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_POWER);
54 val = !!(header->ext_status & OCC_EXT_STAT_MEM_THROTTLE);
57 val = !!(header->ext_status & OCC_EXT_STAT_QUICK_DROP);
60 val = header->occ_state;
63 if (header->status & OCC_STAT_MASTER)
64 val = hweight8(header->occs_present);
69 val = header->ips_status;
75 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_VDD);
78 val = header->ext_status & OCC_EXT_STAT_GPU_THROTTLE;
84 return sysfs_emit(buf, "%d\n", val);
87 static ssize_t occ_error_show(struct device *dev,
88 struct device_attribute *attr, char *buf)
90 struct occ *occ = dev_get_drvdata(dev);
92 occ_update_response(occ);
94 return sysfs_emit(buf, "%d\n", occ->error);
97 static SENSOR_DEVICE_ATTR(occ_master, 0444, occ_sysfs_show, NULL, 0);
98 static SENSOR_DEVICE_ATTR(occ_active, 0444, occ_sysfs_show, NULL, 1);
99 static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp, 0444, occ_sysfs_show, NULL, 2);
100 static SENSOR_DEVICE_ATTR(occ_dvfs_power, 0444, occ_sysfs_show, NULL, 3);
101 static SENSOR_DEVICE_ATTR(occ_mem_throttle, 0444, occ_sysfs_show, NULL, 4);
102 static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop, 0444, occ_sysfs_show, NULL, 5);
103 static SENSOR_DEVICE_ATTR(occ_state, 0444, occ_sysfs_show, NULL, 6);
104 static SENSOR_DEVICE_ATTR(occs_present, 0444, occ_sysfs_show, NULL, 7);
105 static SENSOR_DEVICE_ATTR(occ_ips_status, 0444, occ_sysfs_show, NULL, 8);
106 static SENSOR_DEVICE_ATTR(occ_mode, 0444, occ_sysfs_show, NULL, 9);
107 static SENSOR_DEVICE_ATTR(occ_dvfs_vdd, 0444, occ_sysfs_show, NULL, 10);
108 static SENSOR_DEVICE_ATTR(occ_gpu_throttle, 0444, occ_sysfs_show, NULL, 11);
109 static DEVICE_ATTR_RO(occ_error);
111 static struct attribute *occ_attributes[] = {
112 &sensor_dev_attr_occ_master.dev_attr.attr,
113 &sensor_dev_attr_occ_active.dev_attr.attr,
114 &sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr,
115 &sensor_dev_attr_occ_dvfs_power.dev_attr.attr,
116 &sensor_dev_attr_occ_mem_throttle.dev_attr.attr,
117 &sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr,
118 &sensor_dev_attr_occ_state.dev_attr.attr,
119 &sensor_dev_attr_occs_present.dev_attr.attr,
120 &sensor_dev_attr_occ_ips_status.dev_attr.attr,
121 &sensor_dev_attr_occ_mode.dev_attr.attr,
122 &sensor_dev_attr_occ_dvfs_vdd.dev_attr.attr,
123 &sensor_dev_attr_occ_gpu_throttle.dev_attr.attr,
124 &dev_attr_occ_error.attr,
128 static const struct attribute_group occ_sysfs = {
129 .attrs = occ_attributes,
132 void occ_sysfs_poll_done(struct occ *occ)
135 struct occ_poll_response_header *header =
136 (struct occ_poll_response_header *)occ->resp.data;
139 * On the first poll response, we haven't yet created the sysfs
140 * attributes, so don't make any notify calls.
145 if ((header->status & OCC_STAT_MASTER) !=
146 (occ->prev_stat & OCC_STAT_MASTER)) {
147 name = sensor_dev_attr_occ_master.dev_attr.attr.name;
148 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
151 if ((header->status & OCC_STAT_ACTIVE) !=
152 (occ->prev_stat & OCC_STAT_ACTIVE)) {
153 name = sensor_dev_attr_occ_active.dev_attr.attr.name;
154 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
157 if ((header->ext_status & OCC_EXT_STAT_DVFS_OT) !=
158 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_OT)) {
159 name = sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr.name;
160 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
163 if ((header->ext_status & OCC_EXT_STAT_DVFS_POWER) !=
164 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_POWER)) {
165 name = sensor_dev_attr_occ_dvfs_power.dev_attr.attr.name;
166 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
169 if ((header->ext_status & OCC_EXT_STAT_MEM_THROTTLE) !=
170 (occ->prev_ext_stat & OCC_EXT_STAT_MEM_THROTTLE)) {
171 name = sensor_dev_attr_occ_mem_throttle.dev_attr.attr.name;
172 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
175 if ((header->ext_status & OCC_EXT_STAT_QUICK_DROP) !=
176 (occ->prev_ext_stat & OCC_EXT_STAT_QUICK_DROP)) {
177 name = sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr.name;
178 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
181 if ((header->ext_status & OCC_EXT_STAT_DVFS_VDD) !=
182 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_VDD)) {
183 name = sensor_dev_attr_occ_dvfs_vdd.dev_attr.attr.name;
184 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
187 if ((header->ext_status & OCC_EXT_STAT_GPU_THROTTLE) !=
188 (occ->prev_ext_stat & OCC_EXT_STAT_GPU_THROTTLE)) {
189 name = sensor_dev_attr_occ_gpu_throttle.dev_attr.attr.name;
190 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
193 if ((header->status & OCC_STAT_MASTER) &&
194 header->occs_present != occ->prev_occs_present) {
195 name = sensor_dev_attr_occs_present.dev_attr.attr.name;
196 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
199 if (header->ips_status != occ->prev_ips_status) {
200 name = sensor_dev_attr_occ_ips_status.dev_attr.attr.name;
201 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
204 if (header->mode != occ->prev_mode) {
205 name = sensor_dev_attr_occ_mode.dev_attr.attr.name;
206 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
209 if (occ->error && occ->error != occ->prev_error) {
210 name = dev_attr_occ_error.attr.name;
211 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
214 /* no notifications for OCC state; doesn't indicate error condition */
217 occ->prev_error = occ->error;
218 occ->prev_stat = header->status;
219 occ->prev_ext_stat = header->ext_status;
220 occ->prev_occs_present = header->occs_present;
221 occ->prev_ips_status = header->ips_status;
222 occ->prev_mode = header->mode;
225 int occ_setup_sysfs(struct occ *occ)
227 return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs);
230 void occ_shutdown(struct occ *occ)
232 sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs);
234 EXPORT_SYMBOL_GPL(occ_shutdown);