summaryrefslogtreecommitdiff
path: root/drivers/hwmon/asus_rog_ryujin.c
blob: f8b20346a9956fabcfbd531c6a616a6d53906e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// SPDX-License-Identifier: GPL-2.0+
/*
 * hwmon driver for Asus ROG Ryujin II 360 AIO cooler.
 *
 * Copyright 2024 Aleksa Savic <savicaleksa83@gmail.com>
 */

#include <linux/debugfs.h>
#include <linux/hid.h>
#include <linux/hwmon.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <asm/unaligned.h>

#define DRIVER_NAME	"asus_rog_ryujin"

#define USB_VENDOR_ID_ASUS_ROG		0x0b05
#define USB_PRODUCT_ID_RYUJIN_AIO	0x1988	/* ASUS ROG RYUJIN II 360 */

#define STATUS_VALIDITY		1500	/* ms */
#define MAX_REPORT_LENGTH	65

/* Cooler status report offsets */
#define RYUJIN_TEMP_SENSOR_1		3
#define RYUJIN_TEMP_SENSOR_2		4
#define RYUJIN_PUMP_SPEED		5
#define RYUJIN_INTERNAL_FAN_SPEED	7

/* Cooler duty report offsets */
#define RYUJIN_PUMP_DUTY		4
#define RYUJIN_INTERNAL_FAN_DUTY	5

/* Controller status (speeds) report offsets */
#define RYUJIN_CONTROLLER_SPEED_1	5
#define RYUJIN_CONTROLLER_SPEED_2	7
#define RYUJIN_CONTROLLER_SPEED_3	9
#define RYUJIN_CONTROLLER_SPEED_4	3

/* Controller duty report offsets */
#define RYUJIN_CONTROLLER_DUTY		4

/* Control commands and their inner offsets */
#define RYUJIN_CMD_PREFIX	0xEC

static const u8 get_cooler_status_cmd[] = { RYUJIN_CMD_PREFIX, 0x99 };
static const u8 get_cooler_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x9A };
static const u8 get_controller_speed_cmd[] = { RYUJIN_CMD_PREFIX, 0xA0 };
static const u8 get_controller_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0xA1 };

#define RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET	3
#define RYUJIN_SET_COOLER_FAN_DUTY_OFFSET	4
static const u8 set_cooler_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x1A, 0x00, 0x00, 0x00 };

#define RYUJIN_SET_CONTROLLER_FAN_DUTY_OFFSET	4
static const u8 set_controller_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x21, 0x00, 0x00, 0x00 };

/* Command lengths */
#define GET_CMD_LENGTH	2	/* Same length for all get commands */
#define SET_CMD_LENGTH	5	/* Same length for all set commands */

/* Command response headers */
#define RYUJIN_GET_COOLER_STATUS_CMD_RESPONSE		0x19
#define RYUJIN_GET_COOLER_DUTY_CMD_RESPONSE		0x1A
#define RYUJIN_GET_CONTROLLER_SPEED_CMD_RESPONSE	0x20
#define RYUJIN_GET_CONTROLLER_DUTY_CMD_RESPONSE		0x21

static const char *const rog_ryujin_temp_label[] = {
	"Coolant temp"
};

static const char *const rog_ryujin_speed_label[] = {
	"Pump speed",
	"Internal fan speed",
	"Controller fan 1 speed",
	"Controller fan 2 speed",
	"Controller fan 3 speed",
	"Controller fan 4 speed",
};

struct rog_ryujin_data {
	struct hid_device *hdev;
	struct device *hwmon_dev;
	/* For locking access to buffer */
	struct mutex buffer_lock;
	/* For queueing multiple readers */
	struct mutex status_report_request_mutex;
	/* For reinitializing the completions below */
	spinlock_t status_report_request_lock;
	struct completion cooler_status_received;
	struct completion controller_status_received;
	struct completion cooler_duty_received;
	struct completion controller_duty_received;
	struct completion cooler_duty_set;
	struct completion controller_duty_set;

	/* Sensor data */
	s32 temp_input[1];
	u16 speed_input[6];	/* Pump, internal fan and four controller fan speeds in RPM */
	u8 duty_input[3];	/* Pump, internal fan and controller fan duty in PWM */

	u8 *buffer;
	unsigned long updated;	/* jiffies */
};

static int rog_ryujin_percent_to_pwm(u16 val)
{
	return DIV_ROUND_CLOSEST(val * 255, 100);
}

static int rog_ryujin_pwm_to_percent(long val)
{
	return DIV_ROUND_CLOSEST(val * 100, 255);
}

static umode_t rog_ryujin_is_visible(const void *data,
				     enum hwmon_sensor_types type, u32 attr, int channel)
{
	switch (type) {
	case hwmon_temp:
		switch (attr) {
		case hwmon_temp_label:
		case hwmon_temp_input:
			return 0444;
		default:
			break;
		}
		break;
	case hwmon_fan:
		switch (attr) {
		case hwmon_fan_label:
		case hwmon_fan_input:
			return 0444;
		default:
			break;
		}
		break;
	case hwmon_pwm:
		switch (attr) {
		case hwmon_pwm_input:
			return 0644;
		default:
			break;
		}
		break;
	default:
		break;
	}

	return 0;
}

/* Writes the command to the device with the rest of the report filled with zeroes */
static int rog_ryujin_write_expanded(struct rog_ryujin_data *priv, const u8 *cmd, int cmd_length)
{
	int ret;

	mutex_lock(&priv->buffer_lock);

	memcpy_and_pad(priv->buffer, MAX_REPORT_LENGTH, cmd, cmd_length, 0x00);
	ret = hid_hw_output_report(priv->hdev, priv->buffer, MAX_REPORT_LENGTH);

	mutex_unlock(&priv->buffer_lock);
	return ret;
}

/* Assumes priv->status_report_request_mutex is locked */
static int rog_ryujin_execute_cmd(struct rog_ryujin_data *priv, const u8 *cmd, int cmd_length,
				  struct completion *status_completion)
{
	int ret;

	/*
	 * Disable raw event parsing for a moment to safely reinitialize the
	 * completion. Reinit is done because hidraw could have triggered
	 * the raw event parsing and marked the passed in completion as done.
	 */
	spin_lock_bh(&priv->status_report_request_lock);
	reinit_completion(status_completion);
	spin_unlock_bh(&priv->status_report_request_lock);

	/* Send command for getting data */
	ret = rog_ryujin_write_expanded(priv, cmd, cmd_length);
	if (ret < 0)
		return ret;

	ret = wait_for_completion_interruptible_timeout(status_completion,
							msecs_to_jiffies(STATUS_VALIDITY));
	if (ret == 0)
		return -ETIMEDOUT;
	else if (ret < 0)
		return ret;

	return 0;
}

static int rog_ryujin_get_status(struct rog_ryujin_data *priv)
{
	int ret = mutex_lock_interruptible(&priv->status_report_request_mutex);

	if (ret < 0)
		return ret;

	if (!time_after(jiffies, priv->updated + msecs_to_jiffies(STATUS_VALIDITY))) {
		/* Data is up to date */
		goto unlock_and_return;
	}

	/* Retrieve cooler status */
	ret =
	    rog_ryujin_execute_cmd(priv, get_cooler_status_cmd, GET_CMD_LENGTH,
				   &priv->cooler_status_received);
	if (ret < 0)
		goto unlock_and_return;

	/* Retrieve controller status (speeds) */
	ret =
	    rog_ryujin_execute_cmd(priv, get_controller_speed_cmd, GET_CMD_LENGTH,
				   &priv->controller_status_received);
	if (ret < 0)
		goto unlock_and_return;

	/* Retrieve cooler duty */
	ret =
	    rog_ryujin_execute_cmd(priv, get_cooler_duty_cmd, GET_CMD_LENGTH,
				   &priv->cooler_duty_received);
	if (ret < 0)
		goto unlock_and_return;

	/* Retrieve controller duty */
	ret =
	    rog_ryujin_execute_cmd(priv, get_controller_duty_cmd, GET_CMD_LENGTH,
				   &priv->controller_duty_received);
	if (ret < 0)
		goto unlock_and_return;

	priv->updated = jiffies;

unlock_and_return:
	mutex_unlock(&priv->status_report_request_mutex);
	if (ret < 0)
		return ret;

	return 0;
}

static int rog_ryujin_read(struct device *dev, enum hwmon_sensor_types type,
			   u32 attr, int channel, long *val)
{
	struct rog_ryujin_data *priv = dev_get_drvdata(dev);
	int ret = rog_ryujin_get_status(priv);

	if (ret < 0)
		return ret;

	switch (type) {
	case hwmon_temp:
		*val = priv->temp_input[channel];
		break;
	case hwmon_fan:
		*val = priv->speed_input[channel];
		break;
	case hwmon_pwm:
		switch (attr) {
		case hwmon_pwm_input:
			*val = priv->duty_input[channel];
			break;
		default:
			return -EOPNOTSUPP;
		}
		break;
	default:
		return -EOPNOTSUPP;	/* unreachable */
	}

	return 0;
}

static int rog_ryujin_read_string(struct device *dev, enum hwmon_sensor_types type,
				  u32 attr, int channel, const char **str)
{
	switch (type) {
	case hwmon_temp:
		*str = rog_ryujin_temp_label[channel];
		break;
	case hwmon_fan:
		*str = rog_ryujin_speed_label[channel];
		break;
	default:
		return -EOPNOTSUPP;	/* unreachable */
	}

	return 0;
}

static int rog_ryujin_write_fixed_duty(struct rog_ryujin_data *priv, int channel, int val)
{
	u8 set_cmd[SET_CMD_LENGTH];
	int ret;

	if (channel < 2) {
		/*
		 * Retrieve cooler duty since both pump and internal fan are set
		 * together, then write back with one of them modified.
		 */
		ret = mutex_lock_interruptible(&priv->status_report_request_mutex);
		if (ret < 0)
			return ret;
		ret =
		    rog_ryujin_execute_cmd(priv, get_cooler_duty_cmd, GET_CMD_LENGTH,
					   &priv->cooler_duty_received);
		if (ret < 0)
			goto unlock_and_return;

		memcpy(set_cmd, set_cooler_duty_cmd, SET_CMD_LENGTH);

		/* Cooler duties are set as 0-100% */
		val = rog_ryujin_pwm_to_percent(val);

		if (channel == 0) {
			/* Cooler pump duty */
			set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] = val;
			set_cmd[RYUJIN_SET_COOLER_FAN_DUTY_OFFSET] =
			    rog_ryujin_pwm_to_percent(priv->duty_input[1]);
		} else if (channel == 1) {
			/* Cooler internal fan duty */
			set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] =
			    rog_ryujin_pwm_to_percent(priv->duty_input[0]);
			set_cmd[RYUJIN_SET_COOLER_FAN_DUTY_OFFSET] = val;
		}

		ret = rog_ryujin_execute_cmd(priv, set_cmd, SET_CMD_LENGTH, &priv->cooler_duty_set);
unlock_and_return:
		mutex_unlock(&priv->status_report_request_mutex);
		if (ret < 0)
			return ret;
	} else {
		/*
		 * Controller fan duty (channel == 2). No need to retrieve current
		 * duty, so just send the command.
		 */
		memcpy(set_cmd, set_controller_duty_cmd, SET_CMD_LENGTH);
		set_cmd[RYUJIN_SET_CONTROLLER_FAN_DUTY_OFFSET] = val;

		ret =
		    rog_ryujin_execute_cmd(priv, set_cmd, SET_CMD_LENGTH,
					   &priv->controller_duty_set);
		if (ret < 0)
			return ret;
	}

	/* Lock onto this value until next refresh cycle */
	priv->duty_input[channel] = val;

	return 0;
}

static int rog_ryujin_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
			    long val)
{
	struct rog_ryujin_data *priv = dev_get_drvdata(dev);
	int ret;

	switch (type) {
	case hwmon_pwm:
		switch (attr) {
		case hwmon_pwm_input:
			if (val < 0 || val > 255)
				return -EINVAL;

			ret = rog_ryujin_write_fixed_duty(priv, channel, val);
			if (ret < 0)
				return ret;
			break;
		default:
			return -EOPNOTSUPP;
		}
		break;
	default:
		return -EOPNOTSUPP;
	}

	return 0;
}

static const struct hwmon_ops rog_ryujin_hwmon_ops = {
	.is_visible = rog_ryujin_is_visible,
	.read = rog_ryujin_read,
	.read_string = rog_ryujin_read_string,
	.write = rog_ryujin_write
};

static const struct hwmon_channel_info *rog_ryujin_info[] = {
	HWMON_CHANNEL_INFO(temp,
			   HWMON_T_INPUT | HWMON_T_LABEL),
	HWMON_CHANNEL_INFO(fan,
			   HWMON_F_INPUT | HWMON_F_LABEL,
			   HWMON_F_INPUT | HWMON_F_LABEL,
			   HWMON_F_INPUT | HWMON_F_LABEL,
			   HWMON_F_INPUT | HWMON_F_LABEL,
			   HWMON_F_INPUT | HWMON_F_LABEL,
			   HWMON_F_INPUT | HWMON_F_LABEL),
	HWMON_CHANNEL_INFO(pwm,
			   HWMON_PWM_INPUT,
			   HWMON_PWM_INPUT,
			   HWMON_PWM_INPUT),
	NULL
};

static const struct hwmon_chip_info rog_ryujin_chip_info = {
	.ops = &rog_ryujin_hwmon_ops,
	.info = rog_ryujin_info,
};

static int rog_ryujin_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data,
				int size)
{
	struct rog_ryujin_data *priv = hid_get_drvdata(hdev);

	if (data[0] != RYUJIN_CMD_PREFIX)
		return 0;

	if (data[1] == RYUJIN_GET_COOLER_STATUS_CMD_RESPONSE) {
		/* Received coolant temp and speeds of pump and internal fan */
		priv->temp_input[0] =
		    data[RYUJIN_TEMP_SENSOR_1] * 1000 + data[RYUJIN_TEMP_SENSOR_2] * 100;
		priv->speed_input[0] = get_unaligned_le16(data + RYUJIN_PUMP_SPEED);
		priv->speed_input[1] = get_unaligned_le16(data + RYUJIN_INTERNAL_FAN_SPEED);

		if (!completion_done(&priv->cooler_status_received))
			complete_all(&priv->cooler_status_received);
	} else if (data[1] == RYUJIN_GET_CONTROLLER_SPEED_CMD_RESPONSE) {
		/* Received speeds of four fans attached to the controller */
		priv->speed_input[2] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_1);
		priv->speed_input[3] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_2);
		priv->speed_input[4] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_3);
		priv->speed_input[5] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_4);

		if (!completion_done(&priv->controller_status_received))
			complete_all(&priv->controller_status_received);
	} else if (data[1] == RYUJIN_GET_COOLER_DUTY_CMD_RESPONSE) {
		/* Received report for pump and internal fan duties (in %) */
		if (data[RYUJIN_PUMP_DUTY] == 0 && data[RYUJIN_INTERNAL_FAN_DUTY] == 0) {
			/*
			 * We received a report with zeroes for duty in both places.
			 * The device returns this as a confirmation that setting values
			 * is successful. If we initiated a write, mark it as complete.
			 */
			if (!completion_done(&priv->cooler_duty_set))
				complete_all(&priv->cooler_duty_set);
			else if (!completion_done(&priv->cooler_duty_received))
				/*
				 * We didn't initiate a write, but received both zeroes.
				 * This means that either both duties are actually zero,
				 * or that we received a success report caused by userspace.
				 * We're expecting a report, so parse it.
				 */
				goto read_cooler_duty;
			return 0;
		}
read_cooler_duty:
		priv->duty_input[0] = rog_ryujin_percent_to_pwm(data[RYUJIN_PUMP_DUTY]);
		priv->duty_input[1] = rog_ryujin_percent_to_pwm(data[RYUJIN_INTERNAL_FAN_DUTY]);

		if (!completion_done(&priv->cooler_duty_received))
			complete_all(&priv->cooler_duty_received);
	} else if (data[1] == RYUJIN_GET_CONTROLLER_DUTY_CMD_RESPONSE) {
		/* Received report for controller duty for fans (in PWM) */
		if (data[RYUJIN_CONTROLLER_DUTY] == 0) {
			/*
			 * We received a report with a zero for duty. The device returns this as
			 * a confirmation that setting the controller duty value was successful.
			 * If we initiated a write, mark it as complete.
			 */
			if (!completion_done(&priv->controller_duty_set))
				complete_all(&priv->controller_duty_set);
			else if (!completion_done(&priv->controller_duty_received))
				/*
				 * We didn't initiate a write, but received a zero for duty.
				 * This means that either the duty is actually zero, or that
				 * we received a success report caused by userspace.
				 * We're expecting a report, so parse it.
				 */
				goto read_controller_duty;
			return 0;
		}
read_controller_duty:
		priv->duty_input[2] = data[RYUJIN_CONTROLLER_DUTY];

		if (!completion_done(&priv->controller_duty_received))
			complete_all(&priv->controller_duty_received);
	}

	return 0;
}

static int rog_ryujin_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
	struct rog_ryujin_data *priv;
	int ret;

	priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->hdev = hdev;
	hid_set_drvdata(hdev, priv);

	/*
	 * Initialize priv->updated to STATUS_VALIDITY seconds in the past, making
	 * the initial empty data invalid for rog_ryujin_read() without the need for
	 * a special case there.
	 */
	priv->updated = jiffies - msecs_to_jiffies(STATUS_VALIDITY);

	ret = hid_parse(hdev);
	if (ret) {
		hid_err(hdev, "hid parse failed with %d\n", ret);
		return ret;
	}

	/* Enable hidraw so existing user-space tools can continue to work */
	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
	if (ret) {
		hid_err(hdev, "hid hw start failed with %d\n", ret);
		return ret;
	}

	ret = hid_hw_open(hdev);
	if (ret) {
		hid_err(hdev, "hid hw open failed with %d\n", ret);
		goto fail_and_stop;
	}

	priv->buffer = devm_kzalloc(&hdev->dev, MAX_REPORT_LENGTH, GFP_KERNEL);
	if (!priv->buffer) {
		ret = -ENOMEM;
		goto fail_and_close;
	}

	mutex_init(&priv->status_report_request_mutex);
	mutex_init(&priv->buffer_lock);
	spin_lock_init(&priv->status_report_request_lock);
	init_completion(&priv->cooler_status_received);
	init_completion(&priv->controller_status_received);
	init_completion(&priv->cooler_duty_received);
	init_completion(&priv->controller_duty_received);
	init_completion(&priv->cooler_duty_set);
	init_completion(&priv->controller_duty_set);

	priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "rog_ryujin",
							  priv, &rog_ryujin_chip_info, NULL);
	if (IS_ERR(priv->hwmon_dev)) {
		ret = PTR_ERR(priv->hwmon_dev);
		hid_err(hdev, "hwmon registration failed with %d\n", ret);
		goto fail_and_close;
	}

	return 0;

fail_and_close:
	hid_hw_close(hdev);
fail_and_stop:
	hid_hw_stop(hdev);
	return ret;
}

static void rog_ryujin_remove(struct hid_device *hdev)
{
	struct rog_ryujin_data *priv = hid_get_drvdata(hdev);

	hwmon_device_unregister(priv->hwmon_dev);

	hid_hw_close(hdev);
	hid_hw_stop(hdev);
}

static const struct hid_device_id rog_ryujin_table[] = {
	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_AIO) },
	{ }
};

MODULE_DEVICE_TABLE(hid, rog_ryujin_table);

static struct hid_driver rog_ryujin_driver = {
	.name = "rog_ryujin",
	.id_table = rog_ryujin_table,
	.probe = rog_ryujin_probe,
	.remove = rog_ryujin_remove,
	.raw_event = rog_ryujin_raw_event,
};

static int __init rog_ryujin_init(void)
{
	return hid_register_driver(&rog_ryujin_driver);
}

static void __exit rog_ryujin_exit(void)
{
	hid_unregister_driver(&rog_ryujin_driver);
}

/* When compiled into the kernel, initialize after the HID bus */
late_initcall(rog_ryujin_init);
module_exit(rog_ryujin_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Aleksa Savic <savicaleksa83@gmail.com>");
MODULE_DESCRIPTION("Hwmon driver for Asus ROG Ryujin II 360 AIO cooler");