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/hwmon-sysfs.h> 7 #include <linux/kernel.h> 8 #include <linux/sysfs.h> 9 10 #include "common.h" 11 12 /* OCC status register */ 13 #define OCC_STAT_MASTER BIT(7) 14 #define OCC_STAT_ACTIVE BIT(0) 15 16 /* OCC extended status register */ 17 #define OCC_EXT_STAT_DVFS_OT BIT(7) 18 #define OCC_EXT_STAT_DVFS_POWER BIT(6) 19 #define OCC_EXT_STAT_MEM_THROTTLE BIT(5) 20 #define OCC_EXT_STAT_QUICK_DROP BIT(4) 21 22 static ssize_t occ_sysfs_show(struct device *dev, 23 struct device_attribute *attr, char *buf) 24 { 25 int rc; 26 int val = 0; 27 struct occ *occ = dev_get_drvdata(dev); 28 struct occ_poll_response_header *header; 29 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 30 31 rc = occ_update_response(occ); 32 if (rc) 33 return rc; 34 35 header = (struct occ_poll_response_header *)occ->resp.data; 36 37 switch (sattr->index) { 38 case 0: 39 val = !!(header->status & OCC_STAT_MASTER); 40 break; 41 case 1: 42 val = !!(header->status & OCC_STAT_ACTIVE); 43 break; 44 case 2: 45 val = !!(header->status & OCC_EXT_STAT_DVFS_OT); 46 break; 47 case 3: 48 val = !!(header->status & OCC_EXT_STAT_DVFS_POWER); 49 break; 50 case 4: 51 val = !!(header->status & OCC_EXT_STAT_MEM_THROTTLE); 52 break; 53 case 5: 54 val = !!(header->status & OCC_EXT_STAT_QUICK_DROP); 55 break; 56 case 6: 57 val = header->occ_state; 58 break; 59 case 7: 60 if (header->status & OCC_STAT_MASTER) 61 val = hweight8(header->occs_present); 62 else 63 val = 1; 64 break; 65 case 8: 66 val = occ->error; 67 break; 68 default: 69 return -EINVAL; 70 } 71 72 return snprintf(buf, PAGE_SIZE - 1, "%d\n", val); 73 } 74 75 static SENSOR_DEVICE_ATTR(occ_master, 0444, occ_sysfs_show, NULL, 0); 76 static SENSOR_DEVICE_ATTR(occ_active, 0444, occ_sysfs_show, NULL, 1); 77 static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp, 0444, occ_sysfs_show, NULL, 2); 78 static SENSOR_DEVICE_ATTR(occ_dvfs_power, 0444, occ_sysfs_show, NULL, 3); 79 static SENSOR_DEVICE_ATTR(occ_mem_throttle, 0444, occ_sysfs_show, NULL, 4); 80 static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop, 0444, occ_sysfs_show, NULL, 5); 81 static SENSOR_DEVICE_ATTR(occ_state, 0444, occ_sysfs_show, NULL, 6); 82 static SENSOR_DEVICE_ATTR(occs_present, 0444, occ_sysfs_show, NULL, 7); 83 static SENSOR_DEVICE_ATTR(occ_error, 0444, occ_sysfs_show, NULL, 8); 84 85 static struct attribute *occ_attributes[] = { 86 &sensor_dev_attr_occ_master.dev_attr.attr, 87 &sensor_dev_attr_occ_active.dev_attr.attr, 88 &sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr, 89 &sensor_dev_attr_occ_dvfs_power.dev_attr.attr, 90 &sensor_dev_attr_occ_mem_throttle.dev_attr.attr, 91 &sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr, 92 &sensor_dev_attr_occ_state.dev_attr.attr, 93 &sensor_dev_attr_occs_present.dev_attr.attr, 94 &sensor_dev_attr_occ_error.dev_attr.attr, 95 NULL 96 }; 97 98 static const struct attribute_group occ_sysfs = { 99 .attrs = occ_attributes, 100 }; 101 102 void occ_sysfs_poll_done(struct occ *occ) 103 { 104 const char *name; 105 struct occ_poll_response_header *header = 106 (struct occ_poll_response_header *)occ->resp.data; 107 108 /* 109 * On the first poll response, we haven't yet created the sysfs 110 * attributes, so don't make any notify calls. 111 */ 112 if (!occ->hwmon) 113 goto done; 114 115 if ((header->status & OCC_STAT_MASTER) != 116 (occ->prev_stat & OCC_STAT_MASTER)) { 117 name = sensor_dev_attr_occ_master.dev_attr.attr.name; 118 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 119 } 120 121 if ((header->status & OCC_STAT_ACTIVE) != 122 (occ->prev_stat & OCC_STAT_ACTIVE)) { 123 name = sensor_dev_attr_occ_active.dev_attr.attr.name; 124 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 125 } 126 127 if ((header->ext_status & OCC_EXT_STAT_DVFS_OT) != 128 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_OT)) { 129 name = sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr.name; 130 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 131 } 132 133 if ((header->ext_status & OCC_EXT_STAT_DVFS_POWER) != 134 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_POWER)) { 135 name = sensor_dev_attr_occ_dvfs_power.dev_attr.attr.name; 136 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 137 } 138 139 if ((header->ext_status & OCC_EXT_STAT_MEM_THROTTLE) != 140 (occ->prev_ext_stat & OCC_EXT_STAT_MEM_THROTTLE)) { 141 name = sensor_dev_attr_occ_mem_throttle.dev_attr.attr.name; 142 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 143 } 144 145 if ((header->ext_status & OCC_EXT_STAT_QUICK_DROP) != 146 (occ->prev_ext_stat & OCC_EXT_STAT_QUICK_DROP)) { 147 name = sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr.name; 148 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 149 } 150 151 if ((header->status & OCC_STAT_MASTER) && 152 header->occs_present != occ->prev_occs_present) { 153 name = sensor_dev_attr_occs_present.dev_attr.attr.name; 154 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 155 } 156 157 if (occ->error && occ->error != occ->prev_error) { 158 name = sensor_dev_attr_occ_error.dev_attr.attr.name; 159 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 160 } 161 162 /* no notifications for OCC state; doesn't indicate error condition */ 163 164 done: 165 occ->prev_error = occ->error; 166 occ->prev_stat = header->status; 167 occ->prev_ext_stat = header->ext_status; 168 occ->prev_occs_present = header->occs_present; 169 } 170 171 int occ_setup_sysfs(struct occ *occ) 172 { 173 return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs); 174 } 175 176 void occ_shutdown(struct occ *occ) 177 { 178 sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs); 179 } 180