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