1 // SPDX-License-Identifier: GPL-2.0
3 * OCC hwmon driver sysfs interface
5 * Copyright (C) IBM Corporation 2018
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/bitops.h>
14 #include <linux/device.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/kernel.h>
17 #include <linux/sysfs.h>
21 /* OCC status register */
22 #define OCC_STAT_MASTER BIT(7)
23 #define OCC_STAT_ACTIVE BIT(0)
25 /* OCC extended status register */
26 #define OCC_EXT_STAT_DVFS_OT BIT(7)
27 #define OCC_EXT_STAT_DVFS_POWER BIT(6)
28 #define OCC_EXT_STAT_MEM_THROTTLE BIT(5)
29 #define OCC_EXT_STAT_QUICK_DROP BIT(4)
31 static ssize_t occ_sysfs_show(struct device *dev,
32 struct device_attribute *attr, char *buf)
36 struct occ *occ = dev_get_drvdata(dev);
37 struct occ_poll_response_header *header;
38 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
40 rc = occ_update_response(occ);
44 header = (struct occ_poll_response_header *)occ->resp.data;
46 switch (sattr->index) {
48 val = !!(header->status & OCC_STAT_MASTER);
51 val = !!(header->status & OCC_STAT_ACTIVE);
54 val = !!(header->status & OCC_EXT_STAT_DVFS_OT);
57 val = !!(header->status & OCC_EXT_STAT_DVFS_POWER);
60 val = !!(header->status & OCC_EXT_STAT_MEM_THROTTLE);
63 val = !!(header->status & OCC_EXT_STAT_QUICK_DROP);
66 val = header->occ_state;
69 if (header->status & OCC_STAT_MASTER)
70 val = hweight8(header->occs_present);
81 return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
84 static SENSOR_DEVICE_ATTR(occ_master, 0444, occ_sysfs_show, NULL, 0);
85 static SENSOR_DEVICE_ATTR(occ_active, 0444, occ_sysfs_show, NULL, 1);
86 static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp, 0444, occ_sysfs_show, NULL, 2);
87 static SENSOR_DEVICE_ATTR(occ_dvfs_power, 0444, occ_sysfs_show, NULL, 3);
88 static SENSOR_DEVICE_ATTR(occ_mem_throttle, 0444, occ_sysfs_show, NULL, 4);
89 static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop, 0444, occ_sysfs_show, NULL, 5);
90 static SENSOR_DEVICE_ATTR(occ_state, 0444, occ_sysfs_show, NULL, 6);
91 static SENSOR_DEVICE_ATTR(occs_present, 0444, occ_sysfs_show, NULL, 7);
92 static SENSOR_DEVICE_ATTR(occ_error, 0444, occ_sysfs_show, NULL, 8);
94 static struct attribute *occ_attributes[] = {
95 &sensor_dev_attr_occ_master.dev_attr.attr,
96 &sensor_dev_attr_occ_active.dev_attr.attr,
97 &sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr,
98 &sensor_dev_attr_occ_dvfs_power.dev_attr.attr,
99 &sensor_dev_attr_occ_mem_throttle.dev_attr.attr,
100 &sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr,
101 &sensor_dev_attr_occ_state.dev_attr.attr,
102 &sensor_dev_attr_occs_present.dev_attr.attr,
103 &sensor_dev_attr_occ_error.dev_attr.attr,
107 static const struct attribute_group occ_sysfs = {
108 .attrs = occ_attributes,
111 void occ_sysfs_poll_done(struct occ *occ)
114 struct occ_poll_response_header *header =
115 (struct occ_poll_response_header *)occ->resp.data;
118 * On the first poll response, we haven't yet created the sysfs
119 * attributes, so don't make any notify calls.
124 if ((header->status & OCC_STAT_MASTER) !=
125 (occ->prev_stat & OCC_STAT_MASTER)) {
126 name = sensor_dev_attr_occ_master.dev_attr.attr.name;
127 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
130 if ((header->status & OCC_STAT_ACTIVE) !=
131 (occ->prev_stat & OCC_STAT_ACTIVE)) {
132 name = sensor_dev_attr_occ_active.dev_attr.attr.name;
133 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
136 if ((header->ext_status & OCC_EXT_STAT_DVFS_OT) !=
137 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_OT)) {
138 name = sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr.name;
139 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
142 if ((header->ext_status & OCC_EXT_STAT_DVFS_POWER) !=
143 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_POWER)) {
144 name = sensor_dev_attr_occ_dvfs_power.dev_attr.attr.name;
145 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
148 if ((header->ext_status & OCC_EXT_STAT_MEM_THROTTLE) !=
149 (occ->prev_ext_stat & OCC_EXT_STAT_MEM_THROTTLE)) {
150 name = sensor_dev_attr_occ_mem_throttle.dev_attr.attr.name;
151 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
154 if ((header->ext_status & OCC_EXT_STAT_QUICK_DROP) !=
155 (occ->prev_ext_stat & OCC_EXT_STAT_QUICK_DROP)) {
156 name = sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr.name;
157 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
160 if ((header->status & OCC_STAT_MASTER) &&
161 header->occs_present != occ->prev_occs_present) {
162 name = sensor_dev_attr_occs_present.dev_attr.attr.name;
163 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
166 if (occ->error && occ->error != occ->prev_error) {
167 name = sensor_dev_attr_occ_error.dev_attr.attr.name;
168 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
171 /* no notifications for OCC state; doesn't indicate error condition */
174 occ->prev_error = occ->error;
175 occ->prev_stat = header->status;
176 occ->prev_ext_stat = header->ext_status;
177 occ->prev_occs_present = header->occs_present;
180 int occ_setup_sysfs(struct occ *occ)
182 return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs);
185 void occ_shutdown(struct occ *occ)
187 sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs);