File Index
Files and directories
Sign in
FILE

realtek_hwmon.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/phy/realtek/realtek_hwmon.c C
 1// SPDX-License-Identifier: GPL-2.0+
 2/*
 3 * HWMON support for Realtek PHY's
 4 *
 5 * Author: Heiner Kallweit <hkallweit1@gmail.com>
 6 */
 7
 8#include <linux/hwmon.h>
 9#include <linux/phy.h>
10
11#include "realtek.h"
12
13#define RTL822X_VND2_TSALRM				0xa662
14#define RTL822X_VND2_TSRR				0xbd84
15#define RTL822X_VND2_TSSR				0xb54c
16
17static int rtl822x_hwmon_get_temp(int raw)
18{
19	if (raw >= 512)
20		raw -= 1024;
21
22	return 1000 * raw / 2;
23}
24
25static int rtl822x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
26			      u32 attr, int channel, long *val)
27{
28	struct phy_device *phydev = dev_get_drvdata(dev);
29	int raw;
30
31	switch (attr) {
32	case hwmon_temp_input:
33		raw = phy_read_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSRR) & 0x3ff;
34		*val = rtl822x_hwmon_get_temp(raw);
35		break;
36	case hwmon_temp_max:
37		/* Chip reduces speed to 1G if threshold is exceeded */
38		raw = phy_read_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSSR) >> 6;
39		*val = rtl822x_hwmon_get_temp(raw);
40		break;
41	default:
42		return -EINVAL;
43	}
44
45	return 0;
46}
47
48static const struct hwmon_ops rtl822x_hwmon_ops = {
49	.visible = 0444,
50	.read = rtl822x_hwmon_read,
51};
52
53static const struct hwmon_channel_info * const rtl822x_hwmon_info[] = {
54	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MAX),
55	NULL
56};
57
58static const struct hwmon_chip_info rtl822x_hwmon_chip_info = {
59	.ops = &rtl822x_hwmon_ops,
60	.info = rtl822x_hwmon_info,
61};
62
63int rtl822x_hwmon_init(struct phy_device *phydev)
64{
65	struct device *hwdev, *dev = &phydev->mdio.dev;
66
67	/* Ensure over-temp alarm is reset. */
68	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSALRM, 3);
69
70	hwdev = devm_hwmon_device_register_with_info(dev, NULL, phydev,
71						     &rtl822x_hwmon_chip_info,
72						     NULL);
73	return PTR_ERR_OR_ZERO(hwdev);
74}
~/ Read-only file preview.