1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
4 #include <linux/hwmon.h>
9 static int fbnic_hwmon_sensor_id(enum hwmon_sensor_types type
)
11 if (type
== hwmon_temp
)
12 return FBNIC_SENSOR_TEMP
;
14 return FBNIC_SENSOR_VOLTAGE
;
19 static umode_t
fbnic_hwmon_is_visible(const void *drvdata
,
20 enum hwmon_sensor_types type
,
21 u32 attr
, int channel
)
23 if (type
== hwmon_temp
&& attr
== hwmon_temp_input
)
25 if (type
== hwmon_in
&& attr
== hwmon_in_input
)
31 static int fbnic_hwmon_read(struct device
*dev
, enum hwmon_sensor_types type
,
32 u32 attr
, int channel
, long *val
)
34 struct fbnic_dev
*fbd
= dev_get_drvdata(dev
);
35 const struct fbnic_mac
*mac
= fbd
->mac
;
38 id
= fbnic_hwmon_sensor_id(type
);
39 return id
< 0 ? id
: mac
->get_sensor(fbd
, id
, val
);
42 static const struct hwmon_ops fbnic_hwmon_ops
= {
43 .is_visible
= fbnic_hwmon_is_visible
,
44 .read
= fbnic_hwmon_read
,
47 static const struct hwmon_channel_info
*fbnic_hwmon_info
[] = {
48 HWMON_CHANNEL_INFO(temp
, HWMON_T_INPUT
),
49 HWMON_CHANNEL_INFO(in
, HWMON_I_INPUT
),
53 static const struct hwmon_chip_info fbnic_chip_info
= {
54 .ops
= &fbnic_hwmon_ops
,
55 .info
= fbnic_hwmon_info
,
58 void fbnic_hwmon_register(struct fbnic_dev
*fbd
)
60 if (!IS_REACHABLE(CONFIG_HWMON
))
63 fbd
->hwmon
= hwmon_device_register_with_info(fbd
->dev
, "fbnic",
64 fbd
, &fbnic_chip_info
,
66 if (IS_ERR(fbd
->hwmon
)) {
68 "Failed to register hwmon device %pe\n",
74 void fbnic_hwmon_unregister(struct fbnic_dev
*fbd
)
76 if (!IS_REACHABLE(CONFIG_HWMON
) || !fbd
->hwmon
)
79 hwmon_device_unregister(fbd
->hwmon
);