0001
0002
0003
0004 #include <linux/bitops.h>
0005 #include <linux/device.h>
0006 #include <linux/export.h>
0007 #include <linux/hwmon-sysfs.h>
0008 #include <linux/kernel.h>
0009 #include <linux/kstrtox.h>
0010 #include <linux/sysfs.h>
0011
0012 #include "common.h"
0013
0014
0015 #define OCC_STAT_MASTER BIT(7)
0016
0017
0018 #define OCC_EXT_STAT_DVFS_OT BIT(7)
0019 #define OCC_EXT_STAT_DVFS_POWER BIT(6)
0020 #define OCC_EXT_STAT_MEM_THROTTLE BIT(5)
0021 #define OCC_EXT_STAT_QUICK_DROP BIT(4)
0022 #define OCC_EXT_STAT_DVFS_VDD BIT(3)
0023 #define OCC_EXT_STAT_GPU_THROTTLE GENMASK(2, 0)
0024
0025 static ssize_t occ_active_store(struct device *dev,
0026 struct device_attribute *attr,
0027 const char *buf, size_t count)
0028 {
0029 int rc;
0030 bool active;
0031 struct occ *occ = dev_get_drvdata(dev);
0032
0033 rc = kstrtobool(buf, &active);
0034 if (rc)
0035 return rc;
0036
0037 rc = occ_active(occ, active);
0038 if (rc)
0039 return rc;
0040
0041 return count;
0042 }
0043
0044 static ssize_t occ_sysfs_show(struct device *dev,
0045 struct device_attribute *attr, char *buf)
0046 {
0047 int rc;
0048 int val = 0;
0049 struct occ *occ = dev_get_drvdata(dev);
0050 struct occ_poll_response_header *header;
0051 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
0052
0053 if (occ->active) {
0054 rc = occ_update_response(occ);
0055 if (rc)
0056 return rc;
0057
0058 header = (struct occ_poll_response_header *)occ->resp.data;
0059
0060 switch (sattr->index) {
0061 case 0:
0062 val = !!(header->status & OCC_STAT_MASTER);
0063 break;
0064 case 1:
0065 val = 1;
0066 break;
0067 case 2:
0068 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_OT);
0069 break;
0070 case 3:
0071 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_POWER);
0072 break;
0073 case 4:
0074 val = !!(header->ext_status &
0075 OCC_EXT_STAT_MEM_THROTTLE);
0076 break;
0077 case 5:
0078 val = !!(header->ext_status & OCC_EXT_STAT_QUICK_DROP);
0079 break;
0080 case 6:
0081 val = header->occ_state;
0082 break;
0083 case 7:
0084 if (header->status & OCC_STAT_MASTER)
0085 val = hweight8(header->occs_present);
0086 else
0087 val = 1;
0088 break;
0089 case 8:
0090 val = header->ips_status;
0091 break;
0092 case 9:
0093 val = header->mode;
0094 break;
0095 case 10:
0096 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_VDD);
0097 break;
0098 case 11:
0099 val = header->ext_status & OCC_EXT_STAT_GPU_THROTTLE;
0100 break;
0101 default:
0102 return -EINVAL;
0103 }
0104 } else {
0105 if (sattr->index == 1)
0106 val = 0;
0107 else if (sattr->index <= 11)
0108 val = -ENODATA;
0109 else
0110 return -EINVAL;
0111 }
0112
0113 return sysfs_emit(buf, "%d\n", val);
0114 }
0115
0116 static ssize_t occ_error_show(struct device *dev,
0117 struct device_attribute *attr, char *buf)
0118 {
0119 struct occ *occ = dev_get_drvdata(dev);
0120
0121 occ_update_response(occ);
0122
0123 return sysfs_emit(buf, "%d\n", occ->error);
0124 }
0125
0126 static SENSOR_DEVICE_ATTR(occ_master, 0444, occ_sysfs_show, NULL, 0);
0127 static SENSOR_DEVICE_ATTR(occ_active, 0644, occ_sysfs_show, occ_active_store,
0128 1);
0129 static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp, 0444, occ_sysfs_show, NULL, 2);
0130 static SENSOR_DEVICE_ATTR(occ_dvfs_power, 0444, occ_sysfs_show, NULL, 3);
0131 static SENSOR_DEVICE_ATTR(occ_mem_throttle, 0444, occ_sysfs_show, NULL, 4);
0132 static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop, 0444, occ_sysfs_show, NULL, 5);
0133 static SENSOR_DEVICE_ATTR(occ_state, 0444, occ_sysfs_show, NULL, 6);
0134 static SENSOR_DEVICE_ATTR(occs_present, 0444, occ_sysfs_show, NULL, 7);
0135 static SENSOR_DEVICE_ATTR(occ_ips_status, 0444, occ_sysfs_show, NULL, 8);
0136 static SENSOR_DEVICE_ATTR(occ_mode, 0444, occ_sysfs_show, NULL, 9);
0137 static SENSOR_DEVICE_ATTR(occ_dvfs_vdd, 0444, occ_sysfs_show, NULL, 10);
0138 static SENSOR_DEVICE_ATTR(occ_gpu_throttle, 0444, occ_sysfs_show, NULL, 11);
0139 static DEVICE_ATTR_RO(occ_error);
0140
0141 static struct attribute *occ_attributes[] = {
0142 &sensor_dev_attr_occ_master.dev_attr.attr,
0143 &sensor_dev_attr_occ_active.dev_attr.attr,
0144 &sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr,
0145 &sensor_dev_attr_occ_dvfs_power.dev_attr.attr,
0146 &sensor_dev_attr_occ_mem_throttle.dev_attr.attr,
0147 &sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr,
0148 &sensor_dev_attr_occ_state.dev_attr.attr,
0149 &sensor_dev_attr_occs_present.dev_attr.attr,
0150 &sensor_dev_attr_occ_ips_status.dev_attr.attr,
0151 &sensor_dev_attr_occ_mode.dev_attr.attr,
0152 &sensor_dev_attr_occ_dvfs_vdd.dev_attr.attr,
0153 &sensor_dev_attr_occ_gpu_throttle.dev_attr.attr,
0154 &dev_attr_occ_error.attr,
0155 NULL
0156 };
0157
0158 static const struct attribute_group occ_sysfs = {
0159 .attrs = occ_attributes,
0160 };
0161
0162 void occ_sysfs_poll_done(struct occ *occ)
0163 {
0164 const char *name;
0165 struct occ_poll_response_header *header =
0166 (struct occ_poll_response_header *)occ->resp.data;
0167
0168
0169
0170
0171
0172 if (!occ->active)
0173 goto done;
0174
0175 if ((header->status & OCC_STAT_MASTER) !=
0176 (occ->prev_stat & OCC_STAT_MASTER)) {
0177 name = sensor_dev_attr_occ_master.dev_attr.attr.name;
0178 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0179 }
0180
0181 if ((header->ext_status & OCC_EXT_STAT_DVFS_OT) !=
0182 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_OT)) {
0183 name = sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr.name;
0184 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0185 }
0186
0187 if ((header->ext_status & OCC_EXT_STAT_DVFS_POWER) !=
0188 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_POWER)) {
0189 name = sensor_dev_attr_occ_dvfs_power.dev_attr.attr.name;
0190 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0191 }
0192
0193 if ((header->ext_status & OCC_EXT_STAT_MEM_THROTTLE) !=
0194 (occ->prev_ext_stat & OCC_EXT_STAT_MEM_THROTTLE)) {
0195 name = sensor_dev_attr_occ_mem_throttle.dev_attr.attr.name;
0196 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0197 }
0198
0199 if ((header->ext_status & OCC_EXT_STAT_QUICK_DROP) !=
0200 (occ->prev_ext_stat & OCC_EXT_STAT_QUICK_DROP)) {
0201 name = sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr.name;
0202 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0203 }
0204
0205 if ((header->ext_status & OCC_EXT_STAT_DVFS_VDD) !=
0206 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_VDD)) {
0207 name = sensor_dev_attr_occ_dvfs_vdd.dev_attr.attr.name;
0208 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0209 }
0210
0211 if ((header->ext_status & OCC_EXT_STAT_GPU_THROTTLE) !=
0212 (occ->prev_ext_stat & OCC_EXT_STAT_GPU_THROTTLE)) {
0213 name = sensor_dev_attr_occ_gpu_throttle.dev_attr.attr.name;
0214 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0215 }
0216
0217 if ((header->status & OCC_STAT_MASTER) &&
0218 header->occs_present != occ->prev_occs_present) {
0219 name = sensor_dev_attr_occs_present.dev_attr.attr.name;
0220 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0221 }
0222
0223 if (header->ips_status != occ->prev_ips_status) {
0224 name = sensor_dev_attr_occ_ips_status.dev_attr.attr.name;
0225 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0226 }
0227
0228 if (header->mode != occ->prev_mode) {
0229 name = sensor_dev_attr_occ_mode.dev_attr.attr.name;
0230 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0231 }
0232
0233 if (occ->error && occ->error != occ->prev_error) {
0234 name = dev_attr_occ_error.attr.name;
0235 sysfs_notify(&occ->bus_dev->kobj, NULL, name);
0236 }
0237
0238
0239
0240 done:
0241 occ->prev_error = occ->error;
0242 occ->prev_stat = header->status;
0243 occ->prev_ext_stat = header->ext_status;
0244 occ->prev_occs_present = header->occs_present;
0245 occ->prev_ips_status = header->ips_status;
0246 occ->prev_mode = header->mode;
0247 }
0248
0249 int occ_setup_sysfs(struct occ *occ)
0250 {
0251 return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs);
0252 }
0253
0254 void occ_shutdown_sysfs(struct occ *occ)
0255 {
0256 sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs);
0257 }