1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * OCC hwmon driver sysfs interface 4 * 5 * Copyright (C) IBM Corporation 2018 6 * 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. 11 */ 12 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> 18 19 #include "common.h" 20 21 /* OCC status register */ 22 #define OCC_STAT_MASTER BIT(7) 23 #define OCC_STAT_ACTIVE BIT(0) 24 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) 30 31 static ssize_t occ_sysfs_show(struct device *dev, 32 struct device_attribute *attr, char *buf) 33 { 34 int rc; 35 int val = 0; 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); 39 40 rc = occ_update_response(occ); 41 if (rc) 42 return rc; 43 44 header = (struct occ_poll_response_header *)occ->resp.data; 45 46 switch (sattr->index) { 47 case 0: 48 val = !!(header->status & OCC_STAT_MASTER); 49 break; 50 case 1: 51 val = !!(header->status & OCC_STAT_ACTIVE); 52 break; 53 case 2: 54 val = !!(header->status & OCC_EXT_STAT_DVFS_OT); 55 break; 56 case 3: 57 val = !!(header->status & OCC_EXT_STAT_DVFS_POWER); 58 break; 59 case 4: 60 val = !!(header->status & OCC_EXT_STAT_MEM_THROTTLE); 61 break; 62 case 5: 63 val = !!(header->status & OCC_EXT_STAT_QUICK_DROP); 64 break; 65 case 6: 66 val = header->occ_state; 67 break; 68 case 7: 69 if (header->status & OCC_STAT_MASTER) 70 val = hweight8(header->occs_present); 71 else 72 val = 1; 73 break; 74 case 8: 75 val = occ->error; 76 break; 77 default: 78 return -EINVAL; 79 } 80 81 return snprintf(buf, PAGE_SIZE - 1, "%d\n", val); 82 } 83 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); 93 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, 104 NULL 105 }; 106 107 static const struct attribute_group occ_sysfs = { 108 .attrs = occ_attributes, 109 }; 110 111 void occ_sysfs_poll_done(struct occ *occ) 112 { 113 const char *name; 114 struct occ_poll_response_header *header = 115 (struct occ_poll_response_header *)occ->resp.data; 116 117 /* 118 * On the first poll response, we haven't yet created the sysfs 119 * attributes, so don't make any notify calls. 120 */ 121 if (!occ->hwmon) 122 goto done; 123 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); 128 } 129 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); 134 } 135 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); 140 } 141 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); 146 } 147 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); 152 } 153 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); 158 } 159 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); 164 } 165 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); 169 } 170 171 /* no notifications for OCC state; doesn't indicate error condition */ 172 173 done: 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; 178 } 179 180 int occ_setup_sysfs(struct occ *occ) 181 { 182 return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs); 183 } 184 185 void occ_shutdown(struct occ *occ) 186 { 187 sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs); 188 } 189