drm/nouveau: fix kernel-doc comments
[drm/drm-misc.git] / drivers / hwmon / nzxt-kraken2.c
blob034698232758615447c5ed5e469bb448b6597fb0
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * nzxt-kraken2.c - hwmon driver for NZXT Kraken X42/X52/X62/X72 coolers
5 * The device asynchronously sends HID reports (with id 0x04) twice a second to
6 * communicate current fan speed, pump speed and coolant temperature. The
7 * device does not respond to Get_Report requests for this status report.
9 * Copyright 2019-2021 Jonas Malaco <jonas@protocubo.io>
12 #include <linux/unaligned.h>
13 #include <linux/hid.h>
14 #include <linux/hwmon.h>
15 #include <linux/jiffies.h>
16 #include <linux/module.h>
18 #define STATUS_REPORT_ID 0x04
19 #define STATUS_VALIDITY 2 /* seconds; equivalent to 4 missed updates */
21 static const char *const kraken2_temp_label[] = {
22 "Coolant",
25 static const char *const kraken2_fan_label[] = {
26 "Fan",
27 "Pump",
30 struct kraken2_priv_data {
31 struct hid_device *hid_dev;
32 struct device *hwmon_dev;
33 s32 temp_input[1];
34 u16 fan_input[2];
35 unsigned long updated; /* jiffies */
38 static int kraken2_read(struct device *dev, enum hwmon_sensor_types type,
39 u32 attr, int channel, long *val)
41 struct kraken2_priv_data *priv = dev_get_drvdata(dev);
43 if (time_after(jiffies, priv->updated + STATUS_VALIDITY * HZ))
44 return -ENODATA;
46 switch (type) {
47 case hwmon_temp:
48 *val = priv->temp_input[channel];
49 break;
50 case hwmon_fan:
51 *val = priv->fan_input[channel];
52 break;
53 default:
54 return -EOPNOTSUPP; /* unreachable */
57 return 0;
60 static int kraken2_read_string(struct device *dev, enum hwmon_sensor_types type,
61 u32 attr, int channel, const char **str)
63 switch (type) {
64 case hwmon_temp:
65 *str = kraken2_temp_label[channel];
66 break;
67 case hwmon_fan:
68 *str = kraken2_fan_label[channel];
69 break;
70 default:
71 return -EOPNOTSUPP; /* unreachable */
73 return 0;
76 static const struct hwmon_ops kraken2_hwmon_ops = {
77 .visible = 0444,
78 .read = kraken2_read,
79 .read_string = kraken2_read_string,
82 static const struct hwmon_channel_info * const kraken2_info[] = {
83 HWMON_CHANNEL_INFO(temp,
84 HWMON_T_INPUT | HWMON_T_LABEL),
85 HWMON_CHANNEL_INFO(fan,
86 HWMON_F_INPUT | HWMON_F_LABEL,
87 HWMON_F_INPUT | HWMON_F_LABEL),
88 NULL
91 static const struct hwmon_chip_info kraken2_chip_info = {
92 .ops = &kraken2_hwmon_ops,
93 .info = kraken2_info,
96 static int kraken2_raw_event(struct hid_device *hdev,
97 struct hid_report *report, u8 *data, int size)
99 struct kraken2_priv_data *priv;
101 if (size < 7 || report->id != STATUS_REPORT_ID)
102 return 0;
104 priv = hid_get_drvdata(hdev);
107 * The fractional byte of the coolant temperature has been observed to
108 * be in the interval [1,9], but some of these steps are also
109 * consistently skipped for certain integer parts.
111 * For the lack of a better idea, assume that the resolution is 0.1°C,
112 * and that the missing steps are artifacts of how the firmware
113 * processes the raw sensor data.
115 priv->temp_input[0] = data[1] * 1000 + data[2] * 100;
117 priv->fan_input[0] = get_unaligned_be16(data + 3);
118 priv->fan_input[1] = get_unaligned_be16(data + 5);
120 priv->updated = jiffies;
122 return 0;
125 static int kraken2_probe(struct hid_device *hdev,
126 const struct hid_device_id *id)
128 struct kraken2_priv_data *priv;
129 int ret;
131 priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
132 if (!priv)
133 return -ENOMEM;
135 priv->hid_dev = hdev;
136 hid_set_drvdata(hdev, priv);
139 * Initialize ->updated to STATUS_VALIDITY seconds in the past, making
140 * the initial empty data invalid for kraken2_read without the need for
141 * a special case there.
143 priv->updated = jiffies - STATUS_VALIDITY * HZ;
145 ret = hid_parse(hdev);
146 if (ret) {
147 hid_err(hdev, "hid parse failed with %d\n", ret);
148 return ret;
152 * Enable hidraw so existing user-space tools can continue to work.
154 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
155 if (ret) {
156 hid_err(hdev, "hid hw start failed with %d\n", ret);
157 return ret;
160 ret = hid_hw_open(hdev);
161 if (ret) {
162 hid_err(hdev, "hid hw open failed with %d\n", ret);
163 goto fail_and_stop;
166 priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "kraken2",
167 priv, &kraken2_chip_info,
168 NULL);
169 if (IS_ERR(priv->hwmon_dev)) {
170 ret = PTR_ERR(priv->hwmon_dev);
171 hid_err(hdev, "hwmon registration failed with %d\n", ret);
172 goto fail_and_close;
175 return 0;
177 fail_and_close:
178 hid_hw_close(hdev);
179 fail_and_stop:
180 hid_hw_stop(hdev);
181 return ret;
184 static void kraken2_remove(struct hid_device *hdev)
186 struct kraken2_priv_data *priv = hid_get_drvdata(hdev);
188 hwmon_device_unregister(priv->hwmon_dev);
190 hid_hw_close(hdev);
191 hid_hw_stop(hdev);
194 static const struct hid_device_id kraken2_table[] = {
195 { HID_USB_DEVICE(0x1e71, 0x170e) }, /* NZXT Kraken X42/X52/X62/X72 */
199 MODULE_DEVICE_TABLE(hid, kraken2_table);
201 static struct hid_driver kraken2_driver = {
202 .name = "nzxt-kraken2",
203 .id_table = kraken2_table,
204 .probe = kraken2_probe,
205 .remove = kraken2_remove,
206 .raw_event = kraken2_raw_event,
209 static int __init kraken2_init(void)
211 return hid_register_driver(&kraken2_driver);
214 static void __exit kraken2_exit(void)
216 hid_unregister_driver(&kraken2_driver);
220 * When compiled into the kernel, initialize after the hid bus.
222 late_initcall(kraken2_init);
223 module_exit(kraken2_exit);
225 MODULE_LICENSE("GPL");
226 MODULE_AUTHOR("Jonas Malaco <jonas@protocubo.io>");
227 MODULE_DESCRIPTION("Hwmon driver for NZXT Kraken X42/X52/X62/X72 coolers");