Skip to content

Commit bc3e452

Browse files
t-8chTzung-Bi Shih
authored andcommitted
hwmon: add ChromeOS EC driver
The ChromeOS Embedded Controller exposes fan speed and temperature readings. Expose this data through the hwmon subsystem. The driver is designed to be probed via the cros_ec mfd device. Signed-off-by: Thomas Weißschuh <[email protected]> Acked-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/[email protected] [tzungbi: Fixed typo in MAINTAINERS: "chros_ec_hwmon" -> "cros_ec_hwmon"] Signed-off-by: Tzung-Bi Shih <[email protected]>
1 parent a14a569 commit bc3e452

File tree

6 files changed

+329
-0
lines changed

6 files changed

+329
-0
lines changed

Documentation/hwmon/cros_ec_hwmon.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.. SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
Kernel driver cros_ec_hwmon
4+
===========================
5+
6+
Supported chips:
7+
8+
* ChromeOS embedded controllers.
9+
10+
Prefix: 'cros_ec'
11+
12+
Addresses scanned: -
13+
14+
Author:
15+
16+
- Thomas Weißschuh <[email protected]>
17+
18+
Description
19+
-----------
20+
21+
This driver implements support for hardware monitoring commands exposed by the
22+
ChromeOS embedded controller used in Chromebooks and other devices.
23+
24+
The channel labels exposed via hwmon are retrieved from the EC itself.
25+
26+
Fan and temperature readings are supported.

Documentation/hwmon/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Hardware Monitoring Kernel Drivers
5858
coretemp
5959
corsair-cpro
6060
corsair-psu
61+
cros_ec_hwmon
6162
da9052
6263
da9055
6364
dell-smm-hwmon

MAINTAINERS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5135,6 +5135,14 @@ S: Maintained
51355135
F: Documentation/devicetree/bindings/sound/google,cros-ec-codec.yaml
51365136
F: sound/soc/codecs/cros_ec_codec.*
51375137

5138+
CHROMEOS EC HARDWARE MONITORING
5139+
M: Thomas Weißschuh <[email protected]>
5140+
5141+
5142+
S: Maintained
5143+
F: Documentation/hwmon/cros_ec_hwmon.rst
5144+
F: drivers/hwmon/cros_ec_hwmon.c
5145+
51385146
CHROMEOS EC SUBDRIVERS
51395147
M: Benson Leung <[email protected]>
51405148
R: Guenter Roeck <[email protected]>

drivers/hwmon/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,17 @@ config SENSORS_CORSAIR_PSU
506506
This driver can also be built as a module. If so, the module
507507
will be called corsair-psu.
508508

509+
config SENSORS_CROS_EC
510+
tristate "ChromeOS Embedded Controller sensors"
511+
depends on MFD_CROS_EC_DEV
512+
default MFD_CROS_EC_DEV
513+
help
514+
If you say yes here you get support for ChromeOS Embedded Controller
515+
sensors.
516+
517+
This driver can also be built as a module. If so, the module
518+
will be called cros_ec_hwmon.
519+
509520
config SENSORS_DRIVETEMP
510521
tristate "Hard disk drives with temperature sensors"
511522
depends on SCSI && ATA

drivers/hwmon/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ obj-$(CONFIG_SENSORS_CHIPCAP2) += chipcap2.o
6464
obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o
6565
obj-$(CONFIG_SENSORS_CORSAIR_CPRO) += corsair-cpro.o
6666
obj-$(CONFIG_SENSORS_CORSAIR_PSU) += corsair-psu.o
67+
obj-$(CONFIG_SENSORS_CROS_EC) += cros_ec_hwmon.o
6768
obj-$(CONFIG_SENSORS_DA9052_ADC)+= da9052-hwmon.o
6869
obj-$(CONFIG_SENSORS_DA9055)+= da9055-hwmon.o
6970
obj-$(CONFIG_SENSORS_DELL_SMM) += dell-smm-hwmon.o

drivers/hwmon/cros_ec_hwmon.c

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* ChromeOS EC driver for hwmon
4+
*
5+
* Copyright (C) 2024 Thomas Weißschuh <[email protected]>
6+
*/
7+
8+
#include <linux/device.h>
9+
#include <linux/hwmon.h>
10+
#include <linux/mod_devicetable.h>
11+
#include <linux/module.h>
12+
#include <linux/platform_device.h>
13+
#include <linux/platform_data/cros_ec_commands.h>
14+
#include <linux/platform_data/cros_ec_proto.h>
15+
#include <linux/types.h>
16+
#include <linux/units.h>
17+
18+
#define DRV_NAME "cros-ec-hwmon"
19+
20+
struct cros_ec_hwmon_priv {
21+
struct cros_ec_device *cros_ec;
22+
const char *temp_sensor_names[EC_TEMP_SENSOR_ENTRIES + EC_TEMP_SENSOR_B_ENTRIES];
23+
u8 usable_fans;
24+
};
25+
26+
static int cros_ec_hwmon_read_fan_speed(struct cros_ec_device *cros_ec, u8 index, u16 *speed)
27+
{
28+
int ret;
29+
30+
ret = cros_ec_cmd_readmem(cros_ec, EC_MEMMAP_FAN + index * 2, 2, speed);
31+
if (ret < 0)
32+
return ret;
33+
34+
*speed = le16_to_cpu(*speed);
35+
return 0;
36+
}
37+
38+
static int cros_ec_hwmon_read_temp(struct cros_ec_device *cros_ec, u8 index, u8 *temp)
39+
{
40+
unsigned int offset;
41+
int ret;
42+
43+
if (index < EC_TEMP_SENSOR_ENTRIES)
44+
offset = EC_MEMMAP_TEMP_SENSOR + index;
45+
else
46+
offset = EC_MEMMAP_TEMP_SENSOR_B + index - EC_TEMP_SENSOR_ENTRIES;
47+
48+
ret = cros_ec_cmd_readmem(cros_ec, offset, 1, temp);
49+
if (ret < 0)
50+
return ret;
51+
return 0;
52+
}
53+
54+
static bool cros_ec_hwmon_is_error_fan(u16 speed)
55+
{
56+
return speed == EC_FAN_SPEED_NOT_PRESENT || speed == EC_FAN_SPEED_STALLED;
57+
}
58+
59+
static bool cros_ec_hwmon_is_error_temp(u8 temp)
60+
{
61+
return temp == EC_TEMP_SENSOR_NOT_PRESENT ||
62+
temp == EC_TEMP_SENSOR_ERROR ||
63+
temp == EC_TEMP_SENSOR_NOT_POWERED ||
64+
temp == EC_TEMP_SENSOR_NOT_CALIBRATED;
65+
}
66+
67+
static long cros_ec_hwmon_temp_to_millicelsius(u8 temp)
68+
{
69+
return kelvin_to_millicelsius((((long)temp) + EC_TEMP_SENSOR_OFFSET));
70+
}
71+
72+
static int cros_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
73+
u32 attr, int channel, long *val)
74+
{
75+
struct cros_ec_hwmon_priv *priv = dev_get_drvdata(dev);
76+
int ret = -EOPNOTSUPP;
77+
u16 speed;
78+
u8 temp;
79+
80+
if (type == hwmon_fan) {
81+
if (attr == hwmon_fan_input) {
82+
ret = cros_ec_hwmon_read_fan_speed(priv->cros_ec, channel, &speed);
83+
if (ret == 0) {
84+
if (cros_ec_hwmon_is_error_fan(speed))
85+
ret = -ENODATA;
86+
else
87+
*val = speed;
88+
}
89+
} else if (attr == hwmon_fan_fault) {
90+
ret = cros_ec_hwmon_read_fan_speed(priv->cros_ec, channel, &speed);
91+
if (ret == 0)
92+
*val = cros_ec_hwmon_is_error_fan(speed);
93+
}
94+
} else if (type == hwmon_temp) {
95+
if (attr == hwmon_temp_input) {
96+
ret = cros_ec_hwmon_read_temp(priv->cros_ec, channel, &temp);
97+
if (ret == 0) {
98+
if (cros_ec_hwmon_is_error_temp(temp))
99+
ret = -ENODATA;
100+
else
101+
*val = cros_ec_hwmon_temp_to_millicelsius(temp);
102+
}
103+
} else if (attr == hwmon_temp_fault) {
104+
ret = cros_ec_hwmon_read_temp(priv->cros_ec, channel, &temp);
105+
if (ret == 0)
106+
*val = cros_ec_hwmon_is_error_temp(temp);
107+
}
108+
}
109+
110+
return ret;
111+
}
112+
113+
static int cros_ec_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
114+
u32 attr, int channel, const char **str)
115+
{
116+
struct cros_ec_hwmon_priv *priv = dev_get_drvdata(dev);
117+
118+
if (type == hwmon_temp && attr == hwmon_temp_label) {
119+
*str = priv->temp_sensor_names[channel];
120+
return 0;
121+
}
122+
123+
return -EOPNOTSUPP;
124+
}
125+
126+
static umode_t cros_ec_hwmon_is_visible(const void *data, enum hwmon_sensor_types type,
127+
u32 attr, int channel)
128+
{
129+
const struct cros_ec_hwmon_priv *priv = data;
130+
131+
if (type == hwmon_fan) {
132+
if (priv->usable_fans & BIT(channel))
133+
return 0444;
134+
} else if (type == hwmon_temp) {
135+
if (priv->temp_sensor_names[channel])
136+
return 0444;
137+
}
138+
139+
return 0;
140+
}
141+
142+
static const struct hwmon_channel_info * const cros_ec_hwmon_info[] = {
143+
HWMON_CHANNEL_INFO(fan,
144+
HWMON_F_INPUT | HWMON_F_FAULT,
145+
HWMON_F_INPUT | HWMON_F_FAULT,
146+
HWMON_F_INPUT | HWMON_F_FAULT,
147+
HWMON_F_INPUT | HWMON_F_FAULT),
148+
HWMON_CHANNEL_INFO(temp,
149+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
150+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
151+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
152+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
153+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
154+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
155+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
156+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
157+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
158+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
159+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
160+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
161+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
162+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
163+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
164+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
165+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
166+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
167+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
168+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
169+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
170+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
171+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL,
172+
HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL),
173+
NULL
174+
};
175+
176+
static const struct hwmon_ops cros_ec_hwmon_ops = {
177+
.read = cros_ec_hwmon_read,
178+
.read_string = cros_ec_hwmon_read_string,
179+
.is_visible = cros_ec_hwmon_is_visible,
180+
};
181+
182+
static const struct hwmon_chip_info cros_ec_hwmon_chip_info = {
183+
.ops = &cros_ec_hwmon_ops,
184+
.info = cros_ec_hwmon_info,
185+
};
186+
187+
static void cros_ec_hwmon_probe_temp_sensors(struct device *dev, struct cros_ec_hwmon_priv *priv,
188+
u8 thermal_version)
189+
{
190+
struct ec_params_temp_sensor_get_info req = {};
191+
struct ec_response_temp_sensor_get_info resp;
192+
size_t candidates, i, sensor_name_size;
193+
int ret;
194+
u8 temp;
195+
196+
if (thermal_version < 2)
197+
candidates = EC_TEMP_SENSOR_ENTRIES;
198+
else
199+
candidates = ARRAY_SIZE(priv->temp_sensor_names);
200+
201+
for (i = 0; i < candidates; i++) {
202+
if (cros_ec_hwmon_read_temp(priv->cros_ec, i, &temp) < 0)
203+
continue;
204+
205+
if (temp == EC_TEMP_SENSOR_NOT_PRESENT)
206+
continue;
207+
208+
req.id = i;
209+
ret = cros_ec_cmd(priv->cros_ec, 0, EC_CMD_TEMP_SENSOR_GET_INFO,
210+
&req, sizeof(req), &resp, sizeof(resp));
211+
if (ret < 0)
212+
continue;
213+
214+
sensor_name_size = strnlen(resp.sensor_name, sizeof(resp.sensor_name));
215+
priv->temp_sensor_names[i] = devm_kasprintf(dev, GFP_KERNEL, "%*s",
216+
(int)sensor_name_size,
217+
resp.sensor_name);
218+
}
219+
}
220+
221+
static void cros_ec_hwmon_probe_fans(struct cros_ec_hwmon_priv *priv)
222+
{
223+
u16 speed;
224+
size_t i;
225+
int ret;
226+
227+
for (i = 0; i < EC_FAN_SPEED_ENTRIES; i++) {
228+
ret = cros_ec_hwmon_read_fan_speed(priv->cros_ec, i, &speed);
229+
if (ret == 0 && speed != EC_FAN_SPEED_NOT_PRESENT)
230+
priv->usable_fans |= BIT(i);
231+
}
232+
}
233+
234+
static int cros_ec_hwmon_probe(struct platform_device *pdev)
235+
{
236+
struct device *dev = &pdev->dev;
237+
struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
238+
struct cros_ec_device *cros_ec = ec_dev->ec_dev;
239+
struct cros_ec_hwmon_priv *priv;
240+
struct device *hwmon_dev;
241+
u8 thermal_version;
242+
int ret;
243+
244+
ret = cros_ec_cmd_readmem(cros_ec, EC_MEMMAP_THERMAL_VERSION, 1, &thermal_version);
245+
if (ret < 0)
246+
return ret;
247+
248+
/* Covers both fan and temp sensors */
249+
if (thermal_version == 0)
250+
return -ENODEV;
251+
252+
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
253+
if (!priv)
254+
return -ENOMEM;
255+
256+
priv->cros_ec = cros_ec;
257+
258+
cros_ec_hwmon_probe_temp_sensors(dev, priv, thermal_version);
259+
cros_ec_hwmon_probe_fans(priv);
260+
261+
hwmon_dev = devm_hwmon_device_register_with_info(dev, "cros_ec", priv,
262+
&cros_ec_hwmon_chip_info, NULL);
263+
264+
return PTR_ERR_OR_ZERO(hwmon_dev);
265+
}
266+
267+
static const struct platform_device_id cros_ec_hwmon_id[] = {
268+
{ DRV_NAME, 0 },
269+
{}
270+
};
271+
272+
static struct platform_driver cros_ec_hwmon_driver = {
273+
.driver.name = DRV_NAME,
274+
.probe = cros_ec_hwmon_probe,
275+
.id_table = cros_ec_hwmon_id,
276+
};
277+
module_platform_driver(cros_ec_hwmon_driver);
278+
279+
MODULE_DEVICE_TABLE(platform, cros_ec_hwmon_id);
280+
MODULE_DESCRIPTION("ChromeOS EC Hardware Monitoring Driver");
281+
MODULE_AUTHOR("Thomas Weißschuh <[email protected]");
282+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)