1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright IBM Corp 2019
4 #include <linux/bitops.h>
5 #include <linux/device.h>
6 #include <linux/export.h>
7 #include <linux/hwmon-sysfs.h>
8 #include <linux/kernel.h>
9 #include <linux/sysfs.h>
13 /* OCC status register */
14 #define OCC_STAT_MASTER BIT(7)
15 #define OCC_STAT_ACTIVE BIT(0)
17 /* OCC extended status register */
18 #define OCC_EXT_STAT_DVFS_OT BIT(7)
19 #define OCC_EXT_STAT_DVFS_POWER BIT(6)
20 #define OCC_EXT_STAT_MEM_THROTTLE BIT(5)
21 #define OCC_EXT_STAT_QUICK_DROP BIT(4)
23 static ssize_t
occ_sysfs_show(struct device
*dev
,
24 struct device_attribute
*attr
, char *buf
)
28 struct occ
*occ
= dev_get_drvdata(dev
);
29 struct occ_poll_response_header
*header
;
30 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
32 rc
= occ_update_response(occ
);
36 header
= (struct occ_poll_response_header
*)occ
->resp
.data
;
38 switch (sattr
->index
) {
40 val
= !!(header
->status
& OCC_STAT_MASTER
);
43 val
= !!(header
->status
& OCC_STAT_ACTIVE
);
46 val
= !!(header
->ext_status
& OCC_EXT_STAT_DVFS_OT
);
49 val
= !!(header
->ext_status
& OCC_EXT_STAT_DVFS_POWER
);
52 val
= !!(header
->ext_status
& OCC_EXT_STAT_MEM_THROTTLE
);
55 val
= !!(header
->ext_status
& OCC_EXT_STAT_QUICK_DROP
);
58 val
= header
->occ_state
;
61 if (header
->status
& OCC_STAT_MASTER
)
62 val
= hweight8(header
->occs_present
);
70 return snprintf(buf
, PAGE_SIZE
- 1, "%d\n", val
);
73 static ssize_t
occ_error_show(struct device
*dev
,
74 struct device_attribute
*attr
, char *buf
)
76 struct occ
*occ
= dev_get_drvdata(dev
);
78 occ_update_response(occ
);
80 return snprintf(buf
, PAGE_SIZE
- 1, "%d\n", occ
->error
);
83 static SENSOR_DEVICE_ATTR(occ_master
, 0444, occ_sysfs_show
, NULL
, 0);
84 static SENSOR_DEVICE_ATTR(occ_active
, 0444, occ_sysfs_show
, NULL
, 1);
85 static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp
, 0444, occ_sysfs_show
, NULL
, 2);
86 static SENSOR_DEVICE_ATTR(occ_dvfs_power
, 0444, occ_sysfs_show
, NULL
, 3);
87 static SENSOR_DEVICE_ATTR(occ_mem_throttle
, 0444, occ_sysfs_show
, NULL
, 4);
88 static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop
, 0444, occ_sysfs_show
, NULL
, 5);
89 static SENSOR_DEVICE_ATTR(occ_state
, 0444, occ_sysfs_show
, NULL
, 6);
90 static SENSOR_DEVICE_ATTR(occs_present
, 0444, occ_sysfs_show
, NULL
, 7);
91 static DEVICE_ATTR_RO(occ_error
);
93 static struct attribute
*occ_attributes
[] = {
94 &sensor_dev_attr_occ_master
.dev_attr
.attr
,
95 &sensor_dev_attr_occ_active
.dev_attr
.attr
,
96 &sensor_dev_attr_occ_dvfs_overtemp
.dev_attr
.attr
,
97 &sensor_dev_attr_occ_dvfs_power
.dev_attr
.attr
,
98 &sensor_dev_attr_occ_mem_throttle
.dev_attr
.attr
,
99 &sensor_dev_attr_occ_quick_pwr_drop
.dev_attr
.attr
,
100 &sensor_dev_attr_occ_state
.dev_attr
.attr
,
101 &sensor_dev_attr_occs_present
.dev_attr
.attr
,
102 &dev_attr_occ_error
.attr
,
106 static const struct attribute_group occ_sysfs
= {
107 .attrs
= occ_attributes
,
110 void occ_sysfs_poll_done(struct occ
*occ
)
113 struct occ_poll_response_header
*header
=
114 (struct occ_poll_response_header
*)occ
->resp
.data
;
117 * On the first poll response, we haven't yet created the sysfs
118 * attributes, so don't make any notify calls.
123 if ((header
->status
& OCC_STAT_MASTER
) !=
124 (occ
->prev_stat
& OCC_STAT_MASTER
)) {
125 name
= sensor_dev_attr_occ_master
.dev_attr
.attr
.name
;
126 sysfs_notify(&occ
->bus_dev
->kobj
, NULL
, name
);
129 if ((header
->status
& OCC_STAT_ACTIVE
) !=
130 (occ
->prev_stat
& OCC_STAT_ACTIVE
)) {
131 name
= sensor_dev_attr_occ_active
.dev_attr
.attr
.name
;
132 sysfs_notify(&occ
->bus_dev
->kobj
, NULL
, name
);
135 if ((header
->ext_status
& OCC_EXT_STAT_DVFS_OT
) !=
136 (occ
->prev_ext_stat
& OCC_EXT_STAT_DVFS_OT
)) {
137 name
= sensor_dev_attr_occ_dvfs_overtemp
.dev_attr
.attr
.name
;
138 sysfs_notify(&occ
->bus_dev
->kobj
, NULL
, name
);
141 if ((header
->ext_status
& OCC_EXT_STAT_DVFS_POWER
) !=
142 (occ
->prev_ext_stat
& OCC_EXT_STAT_DVFS_POWER
)) {
143 name
= sensor_dev_attr_occ_dvfs_power
.dev_attr
.attr
.name
;
144 sysfs_notify(&occ
->bus_dev
->kobj
, NULL
, name
);
147 if ((header
->ext_status
& OCC_EXT_STAT_MEM_THROTTLE
) !=
148 (occ
->prev_ext_stat
& OCC_EXT_STAT_MEM_THROTTLE
)) {
149 name
= sensor_dev_attr_occ_mem_throttle
.dev_attr
.attr
.name
;
150 sysfs_notify(&occ
->bus_dev
->kobj
, NULL
, name
);
153 if ((header
->ext_status
& OCC_EXT_STAT_QUICK_DROP
) !=
154 (occ
->prev_ext_stat
& OCC_EXT_STAT_QUICK_DROP
)) {
155 name
= sensor_dev_attr_occ_quick_pwr_drop
.dev_attr
.attr
.name
;
156 sysfs_notify(&occ
->bus_dev
->kobj
, NULL
, name
);
159 if ((header
->status
& OCC_STAT_MASTER
) &&
160 header
->occs_present
!= occ
->prev_occs_present
) {
161 name
= sensor_dev_attr_occs_present
.dev_attr
.attr
.name
;
162 sysfs_notify(&occ
->bus_dev
->kobj
, NULL
, name
);
165 if (occ
->error
&& occ
->error
!= occ
->prev_error
) {
166 name
= dev_attr_occ_error
.attr
.name
;
167 sysfs_notify(&occ
->bus_dev
->kobj
, NULL
, name
);
170 /* no notifications for OCC state; doesn't indicate error condition */
173 occ
->prev_error
= occ
->error
;
174 occ
->prev_stat
= header
->status
;
175 occ
->prev_ext_stat
= header
->ext_status
;
176 occ
->prev_occs_present
= header
->occs_present
;
179 int occ_setup_sysfs(struct occ
*occ
)
181 return sysfs_create_group(&occ
->bus_dev
->kobj
, &occ_sysfs
);
184 void occ_shutdown(struct occ
*occ
)
186 sysfs_remove_group(&occ
->bus_dev
->kobj
, &occ_sysfs
);
188 EXPORT_SYMBOL_GPL(occ_shutdown
);