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)
23 static ssize_t occ_sysfs_show(struct device *dev,
24 struct device_attribute *attr, char *buf)
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);
32 rc = occ_update_response(occ);
36 header = (struct occ_poll_response_header *)occ->resp.data;
38 switch (sattr->index) {
40 val = !!(header->status & OCC_STAT_MASTER);
43 val = !!(header->status & OCC_STAT_ACTIVE);
46 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_OT);
49 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_POWER);
52 val = !!(header->ext_status & OCC_EXT_STAT_MEM_THROTTLE);
55 val = !!(header->ext_status & OCC_EXT_STAT_QUICK_DROP);
58 val = header->occ_state;
61 if (header->status & OCC_STAT_MASTER)
62 val = hweight8(header->occs_present);
67 val = header->ips_status;
73 return sysfs_emit(buf, "%d\n", val);
76 static ssize_t occ_error_show(struct device *dev,
77 struct device_attribute *attr, char *buf)
79 struct occ *occ = dev_get_drvdata(dev);
81 occ_update_response(occ);
83 return sysfs_emit(buf, "%d\n", occ->error);
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);
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,
111 static const struct attribute_group occ_sysfs = {
112 .attrs = occ_attributes,
115 void occ_sysfs_poll_done(struct occ *occ)
118 struct occ_poll_response_header *header =
119 (struct occ_poll_response_header *)occ->resp.data;
122 * On the first poll response, we haven't yet created the sysfs
123 * attributes, so don't make any notify calls.
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);
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);
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);
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);
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);
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);
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);
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);
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);
180 /* no notifications for OCC state; doesn't indicate error condition */
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;
190 int occ_setup_sysfs(struct occ *occ)
192 return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs);
195 void occ_shutdown(struct occ *occ)
197 sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs);
199 EXPORT_SYMBOL_GPL(occ_shutdown);