Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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