1d115b51eSWilken Gottwalt // SPDX-License-Identifier: GPL-2.0-or-later
2d115b51eSWilken Gottwalt /*
3d115b51eSWilken Gottwalt * corsair-psu.c - Linux driver for Corsair power supplies with HID sensors interface
4d115b51eSWilken Gottwalt * Copyright (C) 2020 Wilken Gottwalt <wilken.gottwalt@posteo.net>
5d115b51eSWilken Gottwalt */
6d115b51eSWilken Gottwalt
7d115b51eSWilken Gottwalt #include <linux/completion.h>
8d115b51eSWilken Gottwalt #include <linux/debugfs.h>
9d115b51eSWilken Gottwalt #include <linux/errno.h>
10d115b51eSWilken Gottwalt #include <linux/hid.h>
11d115b51eSWilken Gottwalt #include <linux/hwmon.h>
12d115b51eSWilken Gottwalt #include <linux/hwmon-sysfs.h>
13d115b51eSWilken Gottwalt #include <linux/jiffies.h>
14d115b51eSWilken Gottwalt #include <linux/kernel.h>
15d115b51eSWilken Gottwalt #include <linux/module.h>
16d115b51eSWilken Gottwalt #include <linux/mutex.h>
17d115b51eSWilken Gottwalt #include <linux/slab.h>
18d115b51eSWilken Gottwalt #include <linux/types.h>
19d115b51eSWilken Gottwalt
20d115b51eSWilken Gottwalt /*
21d115b51eSWilken Gottwalt * Corsair protocol for PSUs
22d115b51eSWilken Gottwalt *
23d115b51eSWilken Gottwalt * message size = 64 bytes (request and response, little endian)
24d115b51eSWilken Gottwalt * request:
25d115b51eSWilken Gottwalt * [length][command][param0][param1][paramX]...
26d115b51eSWilken Gottwalt * reply:
27d115b51eSWilken Gottwalt * [echo of length][echo of command][data0][data1][dataX]...
28d115b51eSWilken Gottwalt *
29d115b51eSWilken Gottwalt * - commands are byte sized opcodes
30d115b51eSWilken Gottwalt * - length is the sum of all bytes of the commands/params
31d115b51eSWilken Gottwalt * - the micro-controller of most of these PSUs support concatenation in the request and reply,
32d115b51eSWilken Gottwalt * but it is better to not rely on this (it is also hard to parse)
33d115b51eSWilken Gottwalt * - the driver uses raw events to be accessible from userspace (though this is not really
34d115b51eSWilken Gottwalt * supported, it is just there for convenience, may be removed in the future)
35b54c4b02SWilken Gottwalt * - a reply always starts with the length and command in the same order the request used it
36d115b51eSWilken Gottwalt * - length of the reply data is specific to the command used
37d115b51eSWilken Gottwalt * - some of the commands work on a rail and can be switched to a specific rail (0 = 12v,
38d115b51eSWilken Gottwalt * 1 = 5v, 2 = 3.3v)
39d115b51eSWilken Gottwalt * - the format of the init command 0xFE is swapped length/command bytes
40d115b51eSWilken Gottwalt * - parameter bytes amount and values are specific to the command (rail setting is the only
41b54c4b02SWilken Gottwalt * one for now that uses non-zero values)
42d115b51eSWilken Gottwalt * - the driver supports debugfs for values not fitting into the hwmon class
43b54c4b02SWilken Gottwalt * - not every device class (HXi or RMi) supports all commands
44b54c4b02SWilken Gottwalt * - if configured wrong the PSU resets or shuts down, often before actually hitting the
45741ed085SWilken Gottwalt * reported critical temperature
46741ed085SWilken Gottwalt * - new models like HX1500i Series 2023 have changes in the reported vendor and product
47741ed085SWilken Gottwalt * strings, both are slightly longer now, report vendor and product in one string and are
48741ed085SWilken Gottwalt * the same now
49d115b51eSWilken Gottwalt */
50d115b51eSWilken Gottwalt
51d115b51eSWilken Gottwalt #define DRIVER_NAME "corsair-psu"
52d115b51eSWilken Gottwalt
53741ed085SWilken Gottwalt #define REPLY_SIZE 24 /* max length of a reply to a single command */
54d115b51eSWilken Gottwalt #define CMD_BUFFER_SIZE 64
55d115b51eSWilken Gottwalt #define CMD_TIMEOUT_MS 250
56d115b51eSWilken Gottwalt #define SECONDS_PER_HOUR (60 * 60)
57d115b51eSWilken Gottwalt #define SECONDS_PER_DAY (SECONDS_PER_HOUR * 24)
58c2d5f273SWilken Gottwalt #define RAIL_COUNT 3 /* 3v3 + 5v + 12v */
59c2d5f273SWilken Gottwalt #define TEMP_COUNT 2
60eb12f548SWilken Gottwalt #define OCP_MULTI_RAIL 0x02
61d115b51eSWilken Gottwalt
62d115b51eSWilken Gottwalt #define PSU_CMD_SELECT_RAIL 0x00 /* expects length 2 */
630d67bbc4SWilken Gottwalt #define PSU_CMD_FAN_PWM 0x3B /* the rest of the commands expect length 3 */
640d67bbc4SWilken Gottwalt #define PSU_CMD_RAIL_VOLTS_HCRIT 0x40
65c2d5f273SWilken Gottwalt #define PSU_CMD_RAIL_VOLTS_LCRIT 0x44
66c2d5f273SWilken Gottwalt #define PSU_CMD_RAIL_AMPS_HCRIT 0x46
67c2d5f273SWilken Gottwalt #define PSU_CMD_TEMP_HCRIT 0x4F
68c2d5f273SWilken Gottwalt #define PSU_CMD_IN_VOLTS 0x88
69d115b51eSWilken Gottwalt #define PSU_CMD_IN_AMPS 0x89
70c2d5f273SWilken Gottwalt #define PSU_CMD_RAIL_VOLTS 0x8B
71d115b51eSWilken Gottwalt #define PSU_CMD_RAIL_AMPS 0x8C
72d115b51eSWilken Gottwalt #define PSU_CMD_TEMP0 0x8D
73d115b51eSWilken Gottwalt #define PSU_CMD_TEMP1 0x8E
74d115b51eSWilken Gottwalt #define PSU_CMD_FAN 0x90
75d115b51eSWilken Gottwalt #define PSU_CMD_RAIL_WATTS 0x96
76d115b51eSWilken Gottwalt #define PSU_CMD_VEND_STR 0x99
77d115b51eSWilken Gottwalt #define PSU_CMD_PROD_STR 0x9A
78d115b51eSWilken Gottwalt #define PSU_CMD_TOTAL_UPTIME 0xD1
79d115b51eSWilken Gottwalt #define PSU_CMD_UPTIME 0xD2
80eb12f548SWilken Gottwalt #define PSU_CMD_OCPMODE 0xD8
81eb12f548SWilken Gottwalt #define PSU_CMD_TOTAL_WATTS 0xEE
820d67bbc4SWilken Gottwalt #define PSU_CMD_FAN_PWM_ENABLE 0xF0
83d115b51eSWilken Gottwalt #define PSU_CMD_INIT 0xFE
84d115b51eSWilken Gottwalt
85d115b51eSWilken Gottwalt #define L_IN_VOLTS "v_in"
86d115b51eSWilken Gottwalt #define L_OUT_VOLTS_12V "v_out +12v"
87d115b51eSWilken Gottwalt #define L_OUT_VOLTS_5V "v_out +5v"
88d115b51eSWilken Gottwalt #define L_OUT_VOLTS_3_3V "v_out +3.3v"
89d115b51eSWilken Gottwalt #define L_IN_AMPS "curr in"
90d115b51eSWilken Gottwalt #define L_AMPS_12V "curr +12v"
91d115b51eSWilken Gottwalt #define L_AMPS_5V "curr +5v"
92d115b51eSWilken Gottwalt #define L_AMPS_3_3V "curr +3.3v"
93d115b51eSWilken Gottwalt #define L_FAN "psu fan"
94d115b51eSWilken Gottwalt #define L_TEMP0 "vrm temp"
95d115b51eSWilken Gottwalt #define L_TEMP1 "case temp"
96d115b51eSWilken Gottwalt #define L_WATTS "power total"
97d115b51eSWilken Gottwalt #define L_WATTS_12V "power +12v"
98d115b51eSWilken Gottwalt #define L_WATTS_5V "power +5v"
99d115b51eSWilken Gottwalt #define L_WATTS_3_3V "power +3.3v"
100d115b51eSWilken Gottwalt
101d115b51eSWilken Gottwalt static const char *const label_watts[] = {
102d115b51eSWilken Gottwalt L_WATTS,
103d115b51eSWilken Gottwalt L_WATTS_12V,
104d115b51eSWilken Gottwalt L_WATTS_5V,
105d115b51eSWilken Gottwalt L_WATTS_3_3V
106d115b51eSWilken Gottwalt };
107d115b51eSWilken Gottwalt
108d115b51eSWilken Gottwalt static const char *const label_volts[] = {
109d115b51eSWilken Gottwalt L_IN_VOLTS,
110d115b51eSWilken Gottwalt L_OUT_VOLTS_12V,
111d115b51eSWilken Gottwalt L_OUT_VOLTS_5V,
112d115b51eSWilken Gottwalt L_OUT_VOLTS_3_3V
113d115b51eSWilken Gottwalt };
114d115b51eSWilken Gottwalt
115d115b51eSWilken Gottwalt static const char *const label_amps[] = {
116d115b51eSWilken Gottwalt L_IN_AMPS,
117d115b51eSWilken Gottwalt L_AMPS_12V,
118d115b51eSWilken Gottwalt L_AMPS_5V,
119d115b51eSWilken Gottwalt L_AMPS_3_3V
120d115b51eSWilken Gottwalt };
121d115b51eSWilken Gottwalt
122d115b51eSWilken Gottwalt struct corsairpsu_data {
123d115b51eSWilken Gottwalt struct hid_device *hdev;
124d115b51eSWilken Gottwalt struct device *hwmon_dev;
125d115b51eSWilken Gottwalt struct dentry *debugfs;
126d115b51eSWilken Gottwalt struct completion wait_completion;
127d115b51eSWilken Gottwalt struct mutex lock; /* for locking access to cmd_buffer */
128d115b51eSWilken Gottwalt u8 *cmd_buffer;
129d115b51eSWilken Gottwalt char vendor[REPLY_SIZE];
130d115b51eSWilken Gottwalt char product[REPLY_SIZE];
131c2d5f273SWilken Gottwalt long temp_crit[TEMP_COUNT];
132c2d5f273SWilken Gottwalt long in_crit[RAIL_COUNT];
133c2d5f273SWilken Gottwalt long in_lcrit[RAIL_COUNT];
134c2d5f273SWilken Gottwalt long curr_crit[RAIL_COUNT];
135c2d5f273SWilken Gottwalt u8 temp_crit_support;
136c2d5f273SWilken Gottwalt u8 in_crit_support;
137c2d5f273SWilken Gottwalt u8 in_lcrit_support;
138c2d5f273SWilken Gottwalt u8 curr_crit_support;
139c2d5f273SWilken Gottwalt bool in_curr_cmd_support; /* not all commands are supported on every PSU */
140d115b51eSWilken Gottwalt };
141d115b51eSWilken Gottwalt
142d115b51eSWilken Gottwalt /* some values are SMBus LINEAR11 data which need a conversion */
corsairpsu_linear11_to_int(const u16 val,const int scale)143918f2210SWilken Gottwalt static int corsairpsu_linear11_to_int(const u16 val, const int scale)
144d115b51eSWilken Gottwalt {
145918f2210SWilken Gottwalt const int exp = ((s16)val) >> 11;
146918f2210SWilken Gottwalt const int mant = (((s16)(val & 0x7ff)) << 5) >> 5;
147918f2210SWilken Gottwalt const int result = mant * scale;
148d115b51eSWilken Gottwalt
149918f2210SWilken Gottwalt return (exp >= 0) ? (result << exp) : (result >> -exp);
150d115b51eSWilken Gottwalt }
151d115b51eSWilken Gottwalt
1520d67bbc4SWilken Gottwalt /* the micro-controller uses percentage values to control pwm */
corsairpsu_dutycycle_to_pwm(const long dutycycle)1530d67bbc4SWilken Gottwalt static int corsairpsu_dutycycle_to_pwm(const long dutycycle)
1540d67bbc4SWilken Gottwalt {
1550d67bbc4SWilken Gottwalt const int result = (256 << 16) / 100;
1560d67bbc4SWilken Gottwalt
1570d67bbc4SWilken Gottwalt return (result * dutycycle) >> 16;
1580d67bbc4SWilken Gottwalt }
1590d67bbc4SWilken Gottwalt
corsairpsu_usb_cmd(struct corsairpsu_data * priv,u8 p0,u8 p1,u8 p2,void * data)160d115b51eSWilken Gottwalt static int corsairpsu_usb_cmd(struct corsairpsu_data *priv, u8 p0, u8 p1, u8 p2, void *data)
161d115b51eSWilken Gottwalt {
162d115b51eSWilken Gottwalt unsigned long time;
163d115b51eSWilken Gottwalt int ret;
164d115b51eSWilken Gottwalt
165d115b51eSWilken Gottwalt memset(priv->cmd_buffer, 0, CMD_BUFFER_SIZE);
166d115b51eSWilken Gottwalt priv->cmd_buffer[0] = p0;
167d115b51eSWilken Gottwalt priv->cmd_buffer[1] = p1;
168d115b51eSWilken Gottwalt priv->cmd_buffer[2] = p2;
169d115b51eSWilken Gottwalt
170d115b51eSWilken Gottwalt reinit_completion(&priv->wait_completion);
171d115b51eSWilken Gottwalt
172d115b51eSWilken Gottwalt ret = hid_hw_output_report(priv->hdev, priv->cmd_buffer, CMD_BUFFER_SIZE);
173d115b51eSWilken Gottwalt if (ret < 0)
174d115b51eSWilken Gottwalt return ret;
175d115b51eSWilken Gottwalt
176d115b51eSWilken Gottwalt time = wait_for_completion_timeout(&priv->wait_completion,
177d115b51eSWilken Gottwalt msecs_to_jiffies(CMD_TIMEOUT_MS));
178d115b51eSWilken Gottwalt if (!time)
179d115b51eSWilken Gottwalt return -ETIMEDOUT;
180d115b51eSWilken Gottwalt
181d115b51eSWilken Gottwalt /*
182d115b51eSWilken Gottwalt * at the start of the reply is an echo of the send command/length in the same order it
183d115b51eSWilken Gottwalt * was send, not every command is supported on every device class, if a command is not
184d115b51eSWilken Gottwalt * supported, the length value in the reply is okay, but the command value is set to 0
185d115b51eSWilken Gottwalt */
186d115b51eSWilken Gottwalt if (p0 != priv->cmd_buffer[0] || p1 != priv->cmd_buffer[1])
187d115b51eSWilken Gottwalt return -EOPNOTSUPP;
188d115b51eSWilken Gottwalt
189d115b51eSWilken Gottwalt if (data)
190d115b51eSWilken Gottwalt memcpy(data, priv->cmd_buffer + 2, REPLY_SIZE);
191d115b51eSWilken Gottwalt
192d115b51eSWilken Gottwalt return 0;
193d115b51eSWilken Gottwalt }
194d115b51eSWilken Gottwalt
corsairpsu_init(struct corsairpsu_data * priv)195d115b51eSWilken Gottwalt static int corsairpsu_init(struct corsairpsu_data *priv)
196d115b51eSWilken Gottwalt {
197d115b51eSWilken Gottwalt /*
198d115b51eSWilken Gottwalt * PSU_CMD_INIT uses swapped length/command and expects 2 parameter bytes, this command
199d115b51eSWilken Gottwalt * actually generates a reply, but we don't need it
200d115b51eSWilken Gottwalt */
201d115b51eSWilken Gottwalt return corsairpsu_usb_cmd(priv, PSU_CMD_INIT, 3, 0, NULL);
202d115b51eSWilken Gottwalt }
203d115b51eSWilken Gottwalt
corsairpsu_fwinfo(struct corsairpsu_data * priv)204d115b51eSWilken Gottwalt static int corsairpsu_fwinfo(struct corsairpsu_data *priv)
205d115b51eSWilken Gottwalt {
206d115b51eSWilken Gottwalt int ret;
207d115b51eSWilken Gottwalt
208d115b51eSWilken Gottwalt ret = corsairpsu_usb_cmd(priv, 3, PSU_CMD_VEND_STR, 0, priv->vendor);
209d115b51eSWilken Gottwalt if (ret < 0)
210d115b51eSWilken Gottwalt return ret;
211d115b51eSWilken Gottwalt
212d115b51eSWilken Gottwalt ret = corsairpsu_usb_cmd(priv, 3, PSU_CMD_PROD_STR, 0, priv->product);
213d115b51eSWilken Gottwalt if (ret < 0)
214d115b51eSWilken Gottwalt return ret;
215d115b51eSWilken Gottwalt
216d115b51eSWilken Gottwalt return 0;
217d115b51eSWilken Gottwalt }
218d115b51eSWilken Gottwalt
corsairpsu_request(struct corsairpsu_data * priv,u8 cmd,u8 rail,void * data)219d115b51eSWilken Gottwalt static int corsairpsu_request(struct corsairpsu_data *priv, u8 cmd, u8 rail, void *data)
220d115b51eSWilken Gottwalt {
221d115b51eSWilken Gottwalt int ret;
222d115b51eSWilken Gottwalt
223d115b51eSWilken Gottwalt mutex_lock(&priv->lock);
224d115b51eSWilken Gottwalt switch (cmd) {
225c2d5f273SWilken Gottwalt case PSU_CMD_RAIL_VOLTS_HCRIT:
226c2d5f273SWilken Gottwalt case PSU_CMD_RAIL_VOLTS_LCRIT:
227c2d5f273SWilken Gottwalt case PSU_CMD_RAIL_AMPS_HCRIT:
228c2d5f273SWilken Gottwalt case PSU_CMD_RAIL_VOLTS:
229d115b51eSWilken Gottwalt case PSU_CMD_RAIL_AMPS:
230d115b51eSWilken Gottwalt case PSU_CMD_RAIL_WATTS:
231d115b51eSWilken Gottwalt ret = corsairpsu_usb_cmd(priv, 2, PSU_CMD_SELECT_RAIL, rail, NULL);
232d115b51eSWilken Gottwalt if (ret < 0)
233d115b51eSWilken Gottwalt goto cmd_fail;
234d115b51eSWilken Gottwalt break;
235d115b51eSWilken Gottwalt default:
236d115b51eSWilken Gottwalt break;
237d115b51eSWilken Gottwalt }
238d115b51eSWilken Gottwalt
239d115b51eSWilken Gottwalt ret = corsairpsu_usb_cmd(priv, 3, cmd, 0, data);
240d115b51eSWilken Gottwalt
241d115b51eSWilken Gottwalt cmd_fail:
242d115b51eSWilken Gottwalt mutex_unlock(&priv->lock);
243d115b51eSWilken Gottwalt return ret;
244d115b51eSWilken Gottwalt }
245d115b51eSWilken Gottwalt
corsairpsu_get_value(struct corsairpsu_data * priv,u8 cmd,u8 rail,long * val)246d115b51eSWilken Gottwalt static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8 rail, long *val)
247d115b51eSWilken Gottwalt {
248d115b51eSWilken Gottwalt u8 data[REPLY_SIZE];
249d115b51eSWilken Gottwalt long tmp;
250d115b51eSWilken Gottwalt int ret;
251d115b51eSWilken Gottwalt
252d115b51eSWilken Gottwalt ret = corsairpsu_request(priv, cmd, rail, data);
253d115b51eSWilken Gottwalt if (ret < 0)
254d115b51eSWilken Gottwalt return ret;
255d115b51eSWilken Gottwalt
256d115b51eSWilken Gottwalt /*
257d115b51eSWilken Gottwalt * the biggest value here comes from the uptime command and to exceed MAXINT total uptime
258d115b51eSWilken Gottwalt * needs to be about 68 years, the rest are u16 values and the biggest value coming out of
259b54c4b02SWilken Gottwalt * the LINEAR11 conversion are the watts values which are about 1500 for the strongest psu
260b54c4b02SWilken Gottwalt * supported (HX1500i)
261d115b51eSWilken Gottwalt */
26290673f71SColin Ian King tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
263d115b51eSWilken Gottwalt switch (cmd) {
264c2d5f273SWilken Gottwalt case PSU_CMD_RAIL_VOLTS_HCRIT:
265c2d5f273SWilken Gottwalt case PSU_CMD_RAIL_VOLTS_LCRIT:
266c2d5f273SWilken Gottwalt case PSU_CMD_RAIL_AMPS_HCRIT:
267c2d5f273SWilken Gottwalt case PSU_CMD_TEMP_HCRIT:
268d115b51eSWilken Gottwalt case PSU_CMD_IN_VOLTS:
269d115b51eSWilken Gottwalt case PSU_CMD_IN_AMPS:
270c2d5f273SWilken Gottwalt case PSU_CMD_RAIL_VOLTS:
271d115b51eSWilken Gottwalt case PSU_CMD_RAIL_AMPS:
272d115b51eSWilken Gottwalt case PSU_CMD_TEMP0:
273d115b51eSWilken Gottwalt case PSU_CMD_TEMP1:
274918f2210SWilken Gottwalt *val = corsairpsu_linear11_to_int(tmp & 0xFFFF, 1000);
275d115b51eSWilken Gottwalt break;
276d115b51eSWilken Gottwalt case PSU_CMD_FAN:
277918f2210SWilken Gottwalt *val = corsairpsu_linear11_to_int(tmp & 0xFFFF, 1);
278d115b51eSWilken Gottwalt break;
2790d67bbc4SWilken Gottwalt case PSU_CMD_FAN_PWM_ENABLE:
2800d67bbc4SWilken Gottwalt *val = corsairpsu_linear11_to_int(tmp & 0xFFFF, 1);
2810d67bbc4SWilken Gottwalt /*
2820d67bbc4SWilken Gottwalt * 0 = automatic mode, means the micro-controller controls the fan using a plan
2830d67bbc4SWilken Gottwalt * which can be modified, but changing this plan is not supported by this
2840d67bbc4SWilken Gottwalt * driver, the matching PWM mode is automatic fan speed control = PWM 2
2850d67bbc4SWilken Gottwalt * 1 = fixed mode, fan runs at a fixed speed represented by a percentage
2860d67bbc4SWilken Gottwalt * value 0-100, this matches the PWM manual fan speed control = PWM 1
2870d67bbc4SWilken Gottwalt * technically there is no PWM no fan speed control mode, it would be a combination
2880d67bbc4SWilken Gottwalt * of 1 at 100%
2890d67bbc4SWilken Gottwalt */
2900d67bbc4SWilken Gottwalt if (*val == 0)
2910d67bbc4SWilken Gottwalt *val = 2;
2920d67bbc4SWilken Gottwalt break;
2930d67bbc4SWilken Gottwalt case PSU_CMD_FAN_PWM:
2940d67bbc4SWilken Gottwalt *val = corsairpsu_linear11_to_int(tmp & 0xFFFF, 1);
2950d67bbc4SWilken Gottwalt *val = corsairpsu_dutycycle_to_pwm(*val);
2960d67bbc4SWilken Gottwalt break;
297d115b51eSWilken Gottwalt case PSU_CMD_RAIL_WATTS:
298d115b51eSWilken Gottwalt case PSU_CMD_TOTAL_WATTS:
299918f2210SWilken Gottwalt *val = corsairpsu_linear11_to_int(tmp & 0xFFFF, 1000000);
300d115b51eSWilken Gottwalt break;
301d115b51eSWilken Gottwalt case PSU_CMD_TOTAL_UPTIME:
302d115b51eSWilken Gottwalt case PSU_CMD_UPTIME:
303eb12f548SWilken Gottwalt case PSU_CMD_OCPMODE:
304d115b51eSWilken Gottwalt *val = tmp;
305d115b51eSWilken Gottwalt break;
306d115b51eSWilken Gottwalt default:
307d115b51eSWilken Gottwalt ret = -EOPNOTSUPP;
308d115b51eSWilken Gottwalt break;
309d115b51eSWilken Gottwalt }
310d115b51eSWilken Gottwalt
311d115b51eSWilken Gottwalt return ret;
312d115b51eSWilken Gottwalt }
313d115b51eSWilken Gottwalt
corsairpsu_get_criticals(struct corsairpsu_data * priv)314c2d5f273SWilken Gottwalt static void corsairpsu_get_criticals(struct corsairpsu_data *priv)
315c2d5f273SWilken Gottwalt {
316c2d5f273SWilken Gottwalt long tmp;
317c2d5f273SWilken Gottwalt int rail;
318c2d5f273SWilken Gottwalt
319c2d5f273SWilken Gottwalt for (rail = 0; rail < TEMP_COUNT; ++rail) {
320c2d5f273SWilken Gottwalt if (!corsairpsu_get_value(priv, PSU_CMD_TEMP_HCRIT, rail, &tmp)) {
321c2d5f273SWilken Gottwalt priv->temp_crit_support |= BIT(rail);
322c2d5f273SWilken Gottwalt priv->temp_crit[rail] = tmp;
323c2d5f273SWilken Gottwalt }
324c2d5f273SWilken Gottwalt }
325c2d5f273SWilken Gottwalt
326c2d5f273SWilken Gottwalt for (rail = 0; rail < RAIL_COUNT; ++rail) {
327c2d5f273SWilken Gottwalt if (!corsairpsu_get_value(priv, PSU_CMD_RAIL_VOLTS_HCRIT, rail, &tmp)) {
328c2d5f273SWilken Gottwalt priv->in_crit_support |= BIT(rail);
329c2d5f273SWilken Gottwalt priv->in_crit[rail] = tmp;
330c2d5f273SWilken Gottwalt }
331c2d5f273SWilken Gottwalt
332c2d5f273SWilken Gottwalt if (!corsairpsu_get_value(priv, PSU_CMD_RAIL_VOLTS_LCRIT, rail, &tmp)) {
333c2d5f273SWilken Gottwalt priv->in_lcrit_support |= BIT(rail);
334c2d5f273SWilken Gottwalt priv->in_lcrit[rail] = tmp;
335c2d5f273SWilken Gottwalt }
336c2d5f273SWilken Gottwalt
337c2d5f273SWilken Gottwalt if (!corsairpsu_get_value(priv, PSU_CMD_RAIL_AMPS_HCRIT, rail, &tmp)) {
338c2d5f273SWilken Gottwalt priv->curr_crit_support |= BIT(rail);
339c2d5f273SWilken Gottwalt priv->curr_crit[rail] = tmp;
340c2d5f273SWilken Gottwalt }
341c2d5f273SWilken Gottwalt }
342c2d5f273SWilken Gottwalt }
343c2d5f273SWilken Gottwalt
corsairpsu_check_cmd_support(struct corsairpsu_data * priv)344c2d5f273SWilken Gottwalt static void corsairpsu_check_cmd_support(struct corsairpsu_data *priv)
345c2d5f273SWilken Gottwalt {
346c2d5f273SWilken Gottwalt long tmp;
347c2d5f273SWilken Gottwalt
348c2d5f273SWilken Gottwalt priv->in_curr_cmd_support = !corsairpsu_get_value(priv, PSU_CMD_IN_AMPS, 0, &tmp);
349c2d5f273SWilken Gottwalt }
350c2d5f273SWilken Gottwalt
corsairpsu_hwmon_temp_is_visible(const struct corsairpsu_data * priv,u32 attr,int channel)351c2d5f273SWilken Gottwalt static umode_t corsairpsu_hwmon_temp_is_visible(const struct corsairpsu_data *priv, u32 attr,
352c2d5f273SWilken Gottwalt int channel)
353c2d5f273SWilken Gottwalt {
354c2d5f273SWilken Gottwalt umode_t res = 0444;
355c2d5f273SWilken Gottwalt
356c2d5f273SWilken Gottwalt switch (attr) {
357c2d5f273SWilken Gottwalt case hwmon_temp_input:
358c2d5f273SWilken Gottwalt case hwmon_temp_label:
359c2d5f273SWilken Gottwalt case hwmon_temp_crit:
360c2d5f273SWilken Gottwalt if (channel > 0 && !(priv->temp_crit_support & BIT(channel - 1)))
361c2d5f273SWilken Gottwalt res = 0;
362c2d5f273SWilken Gottwalt break;
363c2d5f273SWilken Gottwalt default:
364c2d5f273SWilken Gottwalt break;
365c2d5f273SWilken Gottwalt }
366c2d5f273SWilken Gottwalt
367c2d5f273SWilken Gottwalt return res;
368c2d5f273SWilken Gottwalt }
369c2d5f273SWilken Gottwalt
corsairpsu_hwmon_fan_is_visible(const struct corsairpsu_data * priv,u32 attr,int channel)370c2d5f273SWilken Gottwalt static umode_t corsairpsu_hwmon_fan_is_visible(const struct corsairpsu_data *priv, u32 attr,
371c2d5f273SWilken Gottwalt int channel)
372c2d5f273SWilken Gottwalt {
373c2d5f273SWilken Gottwalt switch (attr) {
374c2d5f273SWilken Gottwalt case hwmon_fan_input:
375c2d5f273SWilken Gottwalt case hwmon_fan_label:
376c2d5f273SWilken Gottwalt return 0444;
377c2d5f273SWilken Gottwalt default:
378c2d5f273SWilken Gottwalt return 0;
379c2d5f273SWilken Gottwalt }
380c2d5f273SWilken Gottwalt }
381c2d5f273SWilken Gottwalt
corsairpsu_hwmon_pwm_is_visible(const struct corsairpsu_data * priv,u32 attr,int channel)3820d67bbc4SWilken Gottwalt static umode_t corsairpsu_hwmon_pwm_is_visible(const struct corsairpsu_data *priv, u32 attr,
3830d67bbc4SWilken Gottwalt int channel)
3840d67bbc4SWilken Gottwalt {
3850d67bbc4SWilken Gottwalt switch (attr) {
3860d67bbc4SWilken Gottwalt case hwmon_pwm_input:
3870d67bbc4SWilken Gottwalt case hwmon_pwm_enable:
3880d67bbc4SWilken Gottwalt return 0444;
3890d67bbc4SWilken Gottwalt default:
3900d67bbc4SWilken Gottwalt return 0;
3910d67bbc4SWilken Gottwalt }
3920d67bbc4SWilken Gottwalt }
3930d67bbc4SWilken Gottwalt
corsairpsu_hwmon_power_is_visible(const struct corsairpsu_data * priv,u32 attr,int channel)394c2d5f273SWilken Gottwalt static umode_t corsairpsu_hwmon_power_is_visible(const struct corsairpsu_data *priv, u32 attr,
395c2d5f273SWilken Gottwalt int channel)
396c2d5f273SWilken Gottwalt {
397c2d5f273SWilken Gottwalt switch (attr) {
398c2d5f273SWilken Gottwalt case hwmon_power_input:
399c2d5f273SWilken Gottwalt case hwmon_power_label:
400c2d5f273SWilken Gottwalt return 0444;
401c2d5f273SWilken Gottwalt default:
402c2d5f273SWilken Gottwalt return 0;
403726c945aSWan Jiabing }
404c2d5f273SWilken Gottwalt }
405c2d5f273SWilken Gottwalt
corsairpsu_hwmon_in_is_visible(const struct corsairpsu_data * priv,u32 attr,int channel)406c2d5f273SWilken Gottwalt static umode_t corsairpsu_hwmon_in_is_visible(const struct corsairpsu_data *priv, u32 attr,
407c2d5f273SWilken Gottwalt int channel)
408c2d5f273SWilken Gottwalt {
409c2d5f273SWilken Gottwalt umode_t res = 0444;
410c2d5f273SWilken Gottwalt
411c2d5f273SWilken Gottwalt switch (attr) {
412c2d5f273SWilken Gottwalt case hwmon_in_input:
413c2d5f273SWilken Gottwalt case hwmon_in_label:
414c2d5f273SWilken Gottwalt case hwmon_in_crit:
415c2d5f273SWilken Gottwalt if (channel > 0 && !(priv->in_crit_support & BIT(channel - 1)))
416c2d5f273SWilken Gottwalt res = 0;
417c2d5f273SWilken Gottwalt break;
418c2d5f273SWilken Gottwalt case hwmon_in_lcrit:
419c2d5f273SWilken Gottwalt if (channel > 0 && !(priv->in_lcrit_support & BIT(channel - 1)))
420c2d5f273SWilken Gottwalt res = 0;
421c2d5f273SWilken Gottwalt break;
422c2d5f273SWilken Gottwalt default:
423c2d5f273SWilken Gottwalt break;
424726c945aSWan Jiabing }
425c2d5f273SWilken Gottwalt
426c2d5f273SWilken Gottwalt return res;
427c2d5f273SWilken Gottwalt }
428c2d5f273SWilken Gottwalt
corsairpsu_hwmon_curr_is_visible(const struct corsairpsu_data * priv,u32 attr,int channel)429c2d5f273SWilken Gottwalt static umode_t corsairpsu_hwmon_curr_is_visible(const struct corsairpsu_data *priv, u32 attr,
430c2d5f273SWilken Gottwalt int channel)
431c2d5f273SWilken Gottwalt {
432c2d5f273SWilken Gottwalt umode_t res = 0444;
433c2d5f273SWilken Gottwalt
434c2d5f273SWilken Gottwalt switch (attr) {
435c2d5f273SWilken Gottwalt case hwmon_curr_input:
436c2d5f273SWilken Gottwalt if (channel == 0 && !priv->in_curr_cmd_support)
437c2d5f273SWilken Gottwalt res = 0;
438c2d5f273SWilken Gottwalt break;
439c2d5f273SWilken Gottwalt case hwmon_curr_label:
440c2d5f273SWilken Gottwalt case hwmon_curr_crit:
441c2d5f273SWilken Gottwalt if (channel > 0 && !(priv->curr_crit_support & BIT(channel - 1)))
442c2d5f273SWilken Gottwalt res = 0;
443c2d5f273SWilken Gottwalt break;
444c2d5f273SWilken Gottwalt default:
445c2d5f273SWilken Gottwalt break;
446c2d5f273SWilken Gottwalt }
447c2d5f273SWilken Gottwalt
448c2d5f273SWilken Gottwalt return res;
449c2d5f273SWilken Gottwalt }
450c2d5f273SWilken Gottwalt
corsairpsu_hwmon_ops_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)451d115b51eSWilken Gottwalt static umode_t corsairpsu_hwmon_ops_is_visible(const void *data, enum hwmon_sensor_types type,
452d115b51eSWilken Gottwalt u32 attr, int channel)
453d115b51eSWilken Gottwalt {
454c2d5f273SWilken Gottwalt const struct corsairpsu_data *priv = data;
455d115b51eSWilken Gottwalt
456c2d5f273SWilken Gottwalt switch (type) {
457c2d5f273SWilken Gottwalt case hwmon_temp:
458c2d5f273SWilken Gottwalt return corsairpsu_hwmon_temp_is_visible(priv, attr, channel);
459c2d5f273SWilken Gottwalt case hwmon_fan:
460c2d5f273SWilken Gottwalt return corsairpsu_hwmon_fan_is_visible(priv, attr, channel);
4610d67bbc4SWilken Gottwalt case hwmon_pwm:
4620d67bbc4SWilken Gottwalt return corsairpsu_hwmon_pwm_is_visible(priv, attr, channel);
463c2d5f273SWilken Gottwalt case hwmon_power:
464c2d5f273SWilken Gottwalt return corsairpsu_hwmon_power_is_visible(priv, attr, channel);
465c2d5f273SWilken Gottwalt case hwmon_in:
466c2d5f273SWilken Gottwalt return corsairpsu_hwmon_in_is_visible(priv, attr, channel);
467c2d5f273SWilken Gottwalt case hwmon_curr:
468c2d5f273SWilken Gottwalt return corsairpsu_hwmon_curr_is_visible(priv, attr, channel);
469c2d5f273SWilken Gottwalt default:
470d115b51eSWilken Gottwalt return 0;
471d115b51eSWilken Gottwalt }
472c2d5f273SWilken Gottwalt }
473c2d5f273SWilken Gottwalt
corsairpsu_hwmon_temp_read(struct corsairpsu_data * priv,u32 attr,int channel,long * val)474c2d5f273SWilken Gottwalt static int corsairpsu_hwmon_temp_read(struct corsairpsu_data *priv, u32 attr, int channel,
475c2d5f273SWilken Gottwalt long *val)
476c2d5f273SWilken Gottwalt {
477c2d5f273SWilken Gottwalt int err = -EOPNOTSUPP;
478c2d5f273SWilken Gottwalt
479c2d5f273SWilken Gottwalt switch (attr) {
480c2d5f273SWilken Gottwalt case hwmon_temp_input:
481c2d5f273SWilken Gottwalt return corsairpsu_get_value(priv, channel ? PSU_CMD_TEMP1 : PSU_CMD_TEMP0,
482c2d5f273SWilken Gottwalt channel, val);
483c2d5f273SWilken Gottwalt case hwmon_temp_crit:
484c2d5f273SWilken Gottwalt *val = priv->temp_crit[channel];
485c2d5f273SWilken Gottwalt err = 0;
486c2d5f273SWilken Gottwalt break;
487c2d5f273SWilken Gottwalt default:
488c2d5f273SWilken Gottwalt break;
489c2d5f273SWilken Gottwalt }
490c2d5f273SWilken Gottwalt
491c2d5f273SWilken Gottwalt return err;
492c2d5f273SWilken Gottwalt }
493c2d5f273SWilken Gottwalt
corsairpsu_hwmon_pwm_read(struct corsairpsu_data * priv,u32 attr,int channel,long * val)4940d67bbc4SWilken Gottwalt static int corsairpsu_hwmon_pwm_read(struct corsairpsu_data *priv, u32 attr, int channel, long *val)
4950d67bbc4SWilken Gottwalt {
4960d67bbc4SWilken Gottwalt switch (attr) {
4970d67bbc4SWilken Gottwalt case hwmon_pwm_input:
4980d67bbc4SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_FAN_PWM, 0, val);
4990d67bbc4SWilken Gottwalt case hwmon_pwm_enable:
5000d67bbc4SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_FAN_PWM_ENABLE, 0, val);
5010d67bbc4SWilken Gottwalt default:
5020d67bbc4SWilken Gottwalt break;
5030d67bbc4SWilken Gottwalt }
5040d67bbc4SWilken Gottwalt
5050d67bbc4SWilken Gottwalt return -EOPNOTSUPP;
5060d67bbc4SWilken Gottwalt }
5070d67bbc4SWilken Gottwalt
corsairpsu_hwmon_power_read(struct corsairpsu_data * priv,u32 attr,int channel,long * val)508c2d5f273SWilken Gottwalt static int corsairpsu_hwmon_power_read(struct corsairpsu_data *priv, u32 attr, int channel,
509c2d5f273SWilken Gottwalt long *val)
510c2d5f273SWilken Gottwalt {
511c2d5f273SWilken Gottwalt if (attr == hwmon_power_input) {
512c2d5f273SWilken Gottwalt switch (channel) {
513c2d5f273SWilken Gottwalt case 0:
514c2d5f273SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_TOTAL_WATTS, 0, val);
515c2d5f273SWilken Gottwalt case 1 ... 3:
516c2d5f273SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_RAIL_WATTS, channel - 1, val);
517c2d5f273SWilken Gottwalt default:
518c2d5f273SWilken Gottwalt break;
519c2d5f273SWilken Gottwalt }
520c2d5f273SWilken Gottwalt }
521c2d5f273SWilken Gottwalt
522c2d5f273SWilken Gottwalt return -EOPNOTSUPP;
523c2d5f273SWilken Gottwalt }
524c2d5f273SWilken Gottwalt
corsairpsu_hwmon_in_read(struct corsairpsu_data * priv,u32 attr,int channel,long * val)525c2d5f273SWilken Gottwalt static int corsairpsu_hwmon_in_read(struct corsairpsu_data *priv, u32 attr, int channel, long *val)
526c2d5f273SWilken Gottwalt {
527c2d5f273SWilken Gottwalt int err = -EOPNOTSUPP;
528c2d5f273SWilken Gottwalt
529c2d5f273SWilken Gottwalt switch (attr) {
530c2d5f273SWilken Gottwalt case hwmon_in_input:
531c2d5f273SWilken Gottwalt switch (channel) {
532c2d5f273SWilken Gottwalt case 0:
533c2d5f273SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_IN_VOLTS, 0, val);
534c2d5f273SWilken Gottwalt case 1 ... 3:
535c2d5f273SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_RAIL_VOLTS, channel - 1, val);
536c2d5f273SWilken Gottwalt default:
537c2d5f273SWilken Gottwalt break;
538c2d5f273SWilken Gottwalt }
539c2d5f273SWilken Gottwalt break;
540c2d5f273SWilken Gottwalt case hwmon_in_crit:
541c2d5f273SWilken Gottwalt *val = priv->in_crit[channel - 1];
542c2d5f273SWilken Gottwalt err = 0;
543c2d5f273SWilken Gottwalt break;
544c2d5f273SWilken Gottwalt case hwmon_in_lcrit:
545c2d5f273SWilken Gottwalt *val = priv->in_lcrit[channel - 1];
546c2d5f273SWilken Gottwalt err = 0;
547c2d5f273SWilken Gottwalt break;
548c2d5f273SWilken Gottwalt }
549c2d5f273SWilken Gottwalt
550c2d5f273SWilken Gottwalt return err;
551c2d5f273SWilken Gottwalt }
552c2d5f273SWilken Gottwalt
corsairpsu_hwmon_curr_read(struct corsairpsu_data * priv,u32 attr,int channel,long * val)553c2d5f273SWilken Gottwalt static int corsairpsu_hwmon_curr_read(struct corsairpsu_data *priv, u32 attr, int channel,
554c2d5f273SWilken Gottwalt long *val)
555c2d5f273SWilken Gottwalt {
556c2d5f273SWilken Gottwalt int err = -EOPNOTSUPP;
557c2d5f273SWilken Gottwalt
558c2d5f273SWilken Gottwalt switch (attr) {
559c2d5f273SWilken Gottwalt case hwmon_curr_input:
560c2d5f273SWilken Gottwalt switch (channel) {
561c2d5f273SWilken Gottwalt case 0:
562c2d5f273SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_IN_AMPS, 0, val);
563c2d5f273SWilken Gottwalt case 1 ... 3:
564c2d5f273SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_RAIL_AMPS, channel - 1, val);
565c2d5f273SWilken Gottwalt default:
566c2d5f273SWilken Gottwalt break;
567c2d5f273SWilken Gottwalt }
568c2d5f273SWilken Gottwalt break;
569c2d5f273SWilken Gottwalt case hwmon_curr_crit:
570c2d5f273SWilken Gottwalt *val = priv->curr_crit[channel - 1];
571c2d5f273SWilken Gottwalt err = 0;
572c2d5f273SWilken Gottwalt break;
573c2d5f273SWilken Gottwalt default:
574c2d5f273SWilken Gottwalt break;
575c2d5f273SWilken Gottwalt }
576c2d5f273SWilken Gottwalt
577c2d5f273SWilken Gottwalt return err;
578c2d5f273SWilken Gottwalt }
579d115b51eSWilken Gottwalt
corsairpsu_hwmon_ops_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)580d115b51eSWilken Gottwalt static int corsairpsu_hwmon_ops_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
581d115b51eSWilken Gottwalt int channel, long *val)
582d115b51eSWilken Gottwalt {
583d115b51eSWilken Gottwalt struct corsairpsu_data *priv = dev_get_drvdata(dev);
584d115b51eSWilken Gottwalt
585c2d5f273SWilken Gottwalt switch (type) {
586c2d5f273SWilken Gottwalt case hwmon_temp:
587c2d5f273SWilken Gottwalt return corsairpsu_hwmon_temp_read(priv, attr, channel, val);
588c2d5f273SWilken Gottwalt case hwmon_fan:
589c2d5f273SWilken Gottwalt if (attr == hwmon_fan_input)
590c2d5f273SWilken Gottwalt return corsairpsu_get_value(priv, PSU_CMD_FAN, 0, val);
591c2d5f273SWilken Gottwalt return -EOPNOTSUPP;
5920d67bbc4SWilken Gottwalt case hwmon_pwm:
5930d67bbc4SWilken Gottwalt return corsairpsu_hwmon_pwm_read(priv, attr, channel, val);
594c2d5f273SWilken Gottwalt case hwmon_power:
595c2d5f273SWilken Gottwalt return corsairpsu_hwmon_power_read(priv, attr, channel, val);
596c2d5f273SWilken Gottwalt case hwmon_in:
597c2d5f273SWilken Gottwalt return corsairpsu_hwmon_in_read(priv, attr, channel, val);
598c2d5f273SWilken Gottwalt case hwmon_curr:
599c2d5f273SWilken Gottwalt return corsairpsu_hwmon_curr_read(priv, attr, channel, val);
600d115b51eSWilken Gottwalt default:
601d115b51eSWilken Gottwalt return -EOPNOTSUPP;
602d115b51eSWilken Gottwalt }
603d115b51eSWilken Gottwalt }
604d115b51eSWilken Gottwalt
corsairpsu_hwmon_ops_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)605d115b51eSWilken Gottwalt static int corsairpsu_hwmon_ops_read_string(struct device *dev, enum hwmon_sensor_types type,
606d115b51eSWilken Gottwalt u32 attr, int channel, const char **str)
607d115b51eSWilken Gottwalt {
608d115b51eSWilken Gottwalt if (type == hwmon_temp && attr == hwmon_temp_label) {
609d115b51eSWilken Gottwalt *str = channel ? L_TEMP1 : L_TEMP0;
610d115b51eSWilken Gottwalt return 0;
611d115b51eSWilken Gottwalt } else if (type == hwmon_fan && attr == hwmon_fan_label) {
612d115b51eSWilken Gottwalt *str = L_FAN;
613d115b51eSWilken Gottwalt return 0;
614d115b51eSWilken Gottwalt } else if (type == hwmon_power && attr == hwmon_power_label && channel < 4) {
615d115b51eSWilken Gottwalt *str = label_watts[channel];
616d115b51eSWilken Gottwalt return 0;
617d115b51eSWilken Gottwalt } else if (type == hwmon_in && attr == hwmon_in_label && channel < 4) {
618d115b51eSWilken Gottwalt *str = label_volts[channel];
619d115b51eSWilken Gottwalt return 0;
620d115b51eSWilken Gottwalt } else if (type == hwmon_curr && attr == hwmon_curr_label && channel < 4) {
621d115b51eSWilken Gottwalt *str = label_amps[channel];
622d115b51eSWilken Gottwalt return 0;
623d115b51eSWilken Gottwalt }
624d115b51eSWilken Gottwalt
625d115b51eSWilken Gottwalt return -EOPNOTSUPP;
626d115b51eSWilken Gottwalt }
627d115b51eSWilken Gottwalt
628d115b51eSWilken Gottwalt static const struct hwmon_ops corsairpsu_hwmon_ops = {
629d115b51eSWilken Gottwalt .is_visible = corsairpsu_hwmon_ops_is_visible,
630d115b51eSWilken Gottwalt .read = corsairpsu_hwmon_ops_read,
631d115b51eSWilken Gottwalt .read_string = corsairpsu_hwmon_ops_read_string,
632d115b51eSWilken Gottwalt };
633d115b51eSWilken Gottwalt
63442bd7a59SKrzysztof Kozlowski static const struct hwmon_channel_info *const corsairpsu_info[] = {
635d115b51eSWilken Gottwalt HWMON_CHANNEL_INFO(chip,
636d115b51eSWilken Gottwalt HWMON_C_REGISTER_TZ),
637d115b51eSWilken Gottwalt HWMON_CHANNEL_INFO(temp,
638c2d5f273SWilken Gottwalt HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_CRIT,
639c2d5f273SWilken Gottwalt HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_CRIT),
640d115b51eSWilken Gottwalt HWMON_CHANNEL_INFO(fan,
641d115b51eSWilken Gottwalt HWMON_F_INPUT | HWMON_F_LABEL),
6420d67bbc4SWilken Gottwalt HWMON_CHANNEL_INFO(pwm,
6430d67bbc4SWilken Gottwalt HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
644d115b51eSWilken Gottwalt HWMON_CHANNEL_INFO(power,
645d115b51eSWilken Gottwalt HWMON_P_INPUT | HWMON_P_LABEL,
646d115b51eSWilken Gottwalt HWMON_P_INPUT | HWMON_P_LABEL,
647d115b51eSWilken Gottwalt HWMON_P_INPUT | HWMON_P_LABEL,
648d115b51eSWilken Gottwalt HWMON_P_INPUT | HWMON_P_LABEL),
649d115b51eSWilken Gottwalt HWMON_CHANNEL_INFO(in,
650d115b51eSWilken Gottwalt HWMON_I_INPUT | HWMON_I_LABEL,
651c2d5f273SWilken Gottwalt HWMON_I_INPUT | HWMON_I_LABEL | HWMON_I_LCRIT | HWMON_I_CRIT,
652c2d5f273SWilken Gottwalt HWMON_I_INPUT | HWMON_I_LABEL | HWMON_I_LCRIT | HWMON_I_CRIT,
653c2d5f273SWilken Gottwalt HWMON_I_INPUT | HWMON_I_LABEL | HWMON_I_LCRIT | HWMON_I_CRIT),
654d115b51eSWilken Gottwalt HWMON_CHANNEL_INFO(curr,
655d115b51eSWilken Gottwalt HWMON_C_INPUT | HWMON_C_LABEL,
656c2d5f273SWilken Gottwalt HWMON_C_INPUT | HWMON_C_LABEL | HWMON_C_CRIT,
657c2d5f273SWilken Gottwalt HWMON_C_INPUT | HWMON_C_LABEL | HWMON_C_CRIT,
658c2d5f273SWilken Gottwalt HWMON_C_INPUT | HWMON_C_LABEL | HWMON_C_CRIT),
659d115b51eSWilken Gottwalt NULL
660d115b51eSWilken Gottwalt };
661d115b51eSWilken Gottwalt
662d115b51eSWilken Gottwalt static const struct hwmon_chip_info corsairpsu_chip_info = {
663d115b51eSWilken Gottwalt .ops = &corsairpsu_hwmon_ops,
664d115b51eSWilken Gottwalt .info = corsairpsu_info,
665d115b51eSWilken Gottwalt };
666d115b51eSWilken Gottwalt
667d115b51eSWilken Gottwalt #ifdef CONFIG_DEBUG_FS
668d115b51eSWilken Gottwalt
print_uptime(struct seq_file * seqf,u8 cmd)669d115b51eSWilken Gottwalt static void print_uptime(struct seq_file *seqf, u8 cmd)
670d115b51eSWilken Gottwalt {
671d115b51eSWilken Gottwalt struct corsairpsu_data *priv = seqf->private;
672d115b51eSWilken Gottwalt long val;
673d115b51eSWilken Gottwalt int ret;
674d115b51eSWilken Gottwalt
675d115b51eSWilken Gottwalt ret = corsairpsu_get_value(priv, cmd, 0, &val);
676d115b51eSWilken Gottwalt if (ret < 0) {
677d115b51eSWilken Gottwalt seq_puts(seqf, "N/A\n");
678d115b51eSWilken Gottwalt return;
679d115b51eSWilken Gottwalt }
680d115b51eSWilken Gottwalt
681d115b51eSWilken Gottwalt if (val > SECONDS_PER_DAY) {
682d115b51eSWilken Gottwalt seq_printf(seqf, "%ld day(s), %02ld:%02ld:%02ld\n", val / SECONDS_PER_DAY,
683d115b51eSWilken Gottwalt val % SECONDS_PER_DAY / SECONDS_PER_HOUR, val % SECONDS_PER_HOUR / 60,
684d115b51eSWilken Gottwalt val % 60);
685d115b51eSWilken Gottwalt return;
686d115b51eSWilken Gottwalt }
687d115b51eSWilken Gottwalt
688d115b51eSWilken Gottwalt seq_printf(seqf, "%02ld:%02ld:%02ld\n", val % SECONDS_PER_DAY / SECONDS_PER_HOUR,
689d115b51eSWilken Gottwalt val % SECONDS_PER_HOUR / 60, val % 60);
690d115b51eSWilken Gottwalt }
691d115b51eSWilken Gottwalt
uptime_show(struct seq_file * seqf,void * unused)692d115b51eSWilken Gottwalt static int uptime_show(struct seq_file *seqf, void *unused)
693d115b51eSWilken Gottwalt {
694d115b51eSWilken Gottwalt print_uptime(seqf, PSU_CMD_UPTIME);
695d115b51eSWilken Gottwalt
696d115b51eSWilken Gottwalt return 0;
697d115b51eSWilken Gottwalt }
698d115b51eSWilken Gottwalt DEFINE_SHOW_ATTRIBUTE(uptime);
699d115b51eSWilken Gottwalt
uptime_total_show(struct seq_file * seqf,void * unused)700d115b51eSWilken Gottwalt static int uptime_total_show(struct seq_file *seqf, void *unused)
701d115b51eSWilken Gottwalt {
702d115b51eSWilken Gottwalt print_uptime(seqf, PSU_CMD_TOTAL_UPTIME);
703d115b51eSWilken Gottwalt
704d115b51eSWilken Gottwalt return 0;
705d115b51eSWilken Gottwalt }
706d115b51eSWilken Gottwalt DEFINE_SHOW_ATTRIBUTE(uptime_total);
707d115b51eSWilken Gottwalt
vendor_show(struct seq_file * seqf,void * unused)708d115b51eSWilken Gottwalt static int vendor_show(struct seq_file *seqf, void *unused)
709d115b51eSWilken Gottwalt {
710d115b51eSWilken Gottwalt struct corsairpsu_data *priv = seqf->private;
711d115b51eSWilken Gottwalt
712d115b51eSWilken Gottwalt seq_printf(seqf, "%s\n", priv->vendor);
713d115b51eSWilken Gottwalt
714d115b51eSWilken Gottwalt return 0;
715d115b51eSWilken Gottwalt }
716d115b51eSWilken Gottwalt DEFINE_SHOW_ATTRIBUTE(vendor);
717d115b51eSWilken Gottwalt
product_show(struct seq_file * seqf,void * unused)718d115b51eSWilken Gottwalt static int product_show(struct seq_file *seqf, void *unused)
719d115b51eSWilken Gottwalt {
720d115b51eSWilken Gottwalt struct corsairpsu_data *priv = seqf->private;
721d115b51eSWilken Gottwalt
722d115b51eSWilken Gottwalt seq_printf(seqf, "%s\n", priv->product);
723d115b51eSWilken Gottwalt
724d115b51eSWilken Gottwalt return 0;
725d115b51eSWilken Gottwalt }
726d115b51eSWilken Gottwalt DEFINE_SHOW_ATTRIBUTE(product);
727d115b51eSWilken Gottwalt
ocpmode_show(struct seq_file * seqf,void * unused)728eb12f548SWilken Gottwalt static int ocpmode_show(struct seq_file *seqf, void *unused)
729eb12f548SWilken Gottwalt {
730eb12f548SWilken Gottwalt struct corsairpsu_data *priv = seqf->private;
731eb12f548SWilken Gottwalt long val;
732eb12f548SWilken Gottwalt int ret;
733eb12f548SWilken Gottwalt
734eb12f548SWilken Gottwalt /*
735eb12f548SWilken Gottwalt * The rail mode is switchable on the fly. The RAW interface can be used for this. But it
736eb12f548SWilken Gottwalt * will not be included here, because I consider it somewhat dangerous for the health of the
737eb12f548SWilken Gottwalt * PSU. The returned value can be a bogus one, if the PSU is in the process of switching and
738eb12f548SWilken Gottwalt * getting of the value itself can also fail during this. Because of this every other value
739eb12f548SWilken Gottwalt * than OCP_MULTI_RAIL can be considered as "single rail".
740eb12f548SWilken Gottwalt */
741eb12f548SWilken Gottwalt ret = corsairpsu_get_value(priv, PSU_CMD_OCPMODE, 0, &val);
742eb12f548SWilken Gottwalt if (ret < 0)
743eb12f548SWilken Gottwalt seq_puts(seqf, "N/A\n");
744eb12f548SWilken Gottwalt else
745eb12f548SWilken Gottwalt seq_printf(seqf, "%s\n", (val == OCP_MULTI_RAIL) ? "multi rail" : "single rail");
746eb12f548SWilken Gottwalt
747eb12f548SWilken Gottwalt return 0;
748eb12f548SWilken Gottwalt }
749eb12f548SWilken Gottwalt DEFINE_SHOW_ATTRIBUTE(ocpmode);
750eb12f548SWilken Gottwalt
corsairpsu_debugfs_init(struct corsairpsu_data * priv)751d115b51eSWilken Gottwalt static void corsairpsu_debugfs_init(struct corsairpsu_data *priv)
752d115b51eSWilken Gottwalt {
753d115b51eSWilken Gottwalt char name[32];
754d115b51eSWilken Gottwalt
755d115b51eSWilken Gottwalt scnprintf(name, sizeof(name), "%s-%s", DRIVER_NAME, dev_name(&priv->hdev->dev));
756d115b51eSWilken Gottwalt
757d115b51eSWilken Gottwalt priv->debugfs = debugfs_create_dir(name, NULL);
758d115b51eSWilken Gottwalt debugfs_create_file("uptime", 0444, priv->debugfs, priv, &uptime_fops);
759d115b51eSWilken Gottwalt debugfs_create_file("uptime_total", 0444, priv->debugfs, priv, &uptime_total_fops);
760d115b51eSWilken Gottwalt debugfs_create_file("vendor", 0444, priv->debugfs, priv, &vendor_fops);
761d115b51eSWilken Gottwalt debugfs_create_file("product", 0444, priv->debugfs, priv, &product_fops);
762eb12f548SWilken Gottwalt debugfs_create_file("ocpmode", 0444, priv->debugfs, priv, &ocpmode_fops);
763d115b51eSWilken Gottwalt }
764d115b51eSWilken Gottwalt
765d115b51eSWilken Gottwalt #else
766d115b51eSWilken Gottwalt
corsairpsu_debugfs_init(struct corsairpsu_data * priv)767d115b51eSWilken Gottwalt static void corsairpsu_debugfs_init(struct corsairpsu_data *priv)
768d115b51eSWilken Gottwalt {
769d115b51eSWilken Gottwalt }
770d115b51eSWilken Gottwalt
771d115b51eSWilken Gottwalt #endif
772d115b51eSWilken Gottwalt
corsairpsu_probe(struct hid_device * hdev,const struct hid_device_id * id)773d115b51eSWilken Gottwalt static int corsairpsu_probe(struct hid_device *hdev, const struct hid_device_id *id)
774d115b51eSWilken Gottwalt {
775d115b51eSWilken Gottwalt struct corsairpsu_data *priv;
776d115b51eSWilken Gottwalt int ret;
777d115b51eSWilken Gottwalt
778d115b51eSWilken Gottwalt priv = devm_kzalloc(&hdev->dev, sizeof(struct corsairpsu_data), GFP_KERNEL);
779d115b51eSWilken Gottwalt if (!priv)
780d115b51eSWilken Gottwalt return -ENOMEM;
781d115b51eSWilken Gottwalt
782d115b51eSWilken Gottwalt priv->cmd_buffer = devm_kmalloc(&hdev->dev, CMD_BUFFER_SIZE, GFP_KERNEL);
783d115b51eSWilken Gottwalt if (!priv->cmd_buffer)
784d115b51eSWilken Gottwalt return -ENOMEM;
785d115b51eSWilken Gottwalt
786d115b51eSWilken Gottwalt ret = hid_parse(hdev);
787d115b51eSWilken Gottwalt if (ret)
788d115b51eSWilken Gottwalt return ret;
789d115b51eSWilken Gottwalt
790d115b51eSWilken Gottwalt ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
791d115b51eSWilken Gottwalt if (ret)
792d115b51eSWilken Gottwalt return ret;
793d115b51eSWilken Gottwalt
794d115b51eSWilken Gottwalt ret = hid_hw_open(hdev);
795d115b51eSWilken Gottwalt if (ret)
796d115b51eSWilken Gottwalt goto fail_and_stop;
797d115b51eSWilken Gottwalt
798d115b51eSWilken Gottwalt priv->hdev = hdev;
799d115b51eSWilken Gottwalt hid_set_drvdata(hdev, priv);
800d115b51eSWilken Gottwalt mutex_init(&priv->lock);
801d115b51eSWilken Gottwalt init_completion(&priv->wait_completion);
802d115b51eSWilken Gottwalt
803d115b51eSWilken Gottwalt hid_device_io_start(hdev);
804d115b51eSWilken Gottwalt
805d115b51eSWilken Gottwalt ret = corsairpsu_init(priv);
806d115b51eSWilken Gottwalt if (ret < 0) {
807d115b51eSWilken Gottwalt dev_err(&hdev->dev, "unable to initialize device (%d)\n", ret);
808d115b51eSWilken Gottwalt goto fail_and_stop;
809d115b51eSWilken Gottwalt }
810d115b51eSWilken Gottwalt
811d115b51eSWilken Gottwalt ret = corsairpsu_fwinfo(priv);
812d115b51eSWilken Gottwalt if (ret < 0) {
813d115b51eSWilken Gottwalt dev_err(&hdev->dev, "unable to query firmware (%d)\n", ret);
814d115b51eSWilken Gottwalt goto fail_and_stop;
815d115b51eSWilken Gottwalt }
816d115b51eSWilken Gottwalt
817c2d5f273SWilken Gottwalt corsairpsu_get_criticals(priv);
818c2d5f273SWilken Gottwalt corsairpsu_check_cmd_support(priv);
819c2d5f273SWilken Gottwalt
820d115b51eSWilken Gottwalt priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsairpsu", priv,
82183832265SWilken Gottwalt &corsairpsu_chip_info, NULL);
822d115b51eSWilken Gottwalt
823d115b51eSWilken Gottwalt if (IS_ERR(priv->hwmon_dev)) {
824d115b51eSWilken Gottwalt ret = PTR_ERR(priv->hwmon_dev);
825d115b51eSWilken Gottwalt goto fail_and_close;
826d115b51eSWilken Gottwalt }
827d115b51eSWilken Gottwalt
828d115b51eSWilken Gottwalt corsairpsu_debugfs_init(priv);
829d115b51eSWilken Gottwalt
830d115b51eSWilken Gottwalt return 0;
831d115b51eSWilken Gottwalt
832d115b51eSWilken Gottwalt fail_and_close:
833d115b51eSWilken Gottwalt hid_hw_close(hdev);
834d115b51eSWilken Gottwalt fail_and_stop:
835d115b51eSWilken Gottwalt hid_hw_stop(hdev);
836d115b51eSWilken Gottwalt return ret;
837d115b51eSWilken Gottwalt }
838d115b51eSWilken Gottwalt
corsairpsu_remove(struct hid_device * hdev)839d115b51eSWilken Gottwalt static void corsairpsu_remove(struct hid_device *hdev)
840d115b51eSWilken Gottwalt {
841d115b51eSWilken Gottwalt struct corsairpsu_data *priv = hid_get_drvdata(hdev);
842d115b51eSWilken Gottwalt
843d115b51eSWilken Gottwalt debugfs_remove_recursive(priv->debugfs);
844d115b51eSWilken Gottwalt hwmon_device_unregister(priv->hwmon_dev);
845d115b51eSWilken Gottwalt hid_hw_close(hdev);
846d115b51eSWilken Gottwalt hid_hw_stop(hdev);
847d115b51eSWilken Gottwalt }
848d115b51eSWilken Gottwalt
corsairpsu_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)849d115b51eSWilken Gottwalt static int corsairpsu_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data,
850d115b51eSWilken Gottwalt int size)
851d115b51eSWilken Gottwalt {
852d115b51eSWilken Gottwalt struct corsairpsu_data *priv = hid_get_drvdata(hdev);
853d115b51eSWilken Gottwalt
854d115b51eSWilken Gottwalt if (completion_done(&priv->wait_completion))
855d115b51eSWilken Gottwalt return 0;
856d115b51eSWilken Gottwalt
857d115b51eSWilken Gottwalt memcpy(priv->cmd_buffer, data, min(CMD_BUFFER_SIZE, size));
858d115b51eSWilken Gottwalt complete(&priv->wait_completion);
859d115b51eSWilken Gottwalt
860d115b51eSWilken Gottwalt return 0;
861d115b51eSWilken Gottwalt }
862d115b51eSWilken Gottwalt
8637656cd21SWilken Gottwalt #ifdef CONFIG_PM
corsairpsu_resume(struct hid_device * hdev)8647656cd21SWilken Gottwalt static int corsairpsu_resume(struct hid_device *hdev)
8657656cd21SWilken Gottwalt {
8667656cd21SWilken Gottwalt struct corsairpsu_data *priv = hid_get_drvdata(hdev);
8677656cd21SWilken Gottwalt
8687656cd21SWilken Gottwalt /* some PSUs turn off the microcontroller during standby, so a reinit is required */
8697656cd21SWilken Gottwalt return corsairpsu_init(priv);
8707656cd21SWilken Gottwalt }
8717656cd21SWilken Gottwalt #endif
8727656cd21SWilken Gottwalt
873d115b51eSWilken Gottwalt static const struct hid_device_id corsairpsu_idtable[] = {
874d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c03) }, /* Corsair HX550i */
875d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c04) }, /* Corsair HX650i */
876d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c05) }, /* Corsair HX750i */
877d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c06) }, /* Corsair HX850i */
878*95cf80c9SWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c07) }, /* Corsair HX1000i Legacy */
879*95cf80c9SWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c08) }, /* Corsair HX1200i Legacy */
880d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c09) }, /* Corsair RM550i */
881d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c0a) }, /* Corsair RM650i */
882d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c0b) }, /* Corsair RM750i */
883d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c0c) }, /* Corsair RM850i */
884d115b51eSWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c0d) }, /* Corsair RM1000i */
885b54c4b02SWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c1e) }, /* Corsair HX1000i Series 2023 */
886*95cf80c9SWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c1f) }, /* Corsair HX1500i Legacy and Series 2023 */
887*95cf80c9SWilken Gottwalt { HID_USB_DEVICE(0x1b1c, 0x1c23) }, /* Corsair HX1200i Series 2023 */
888d115b51eSWilken Gottwalt { },
889d115b51eSWilken Gottwalt };
890d115b51eSWilken Gottwalt MODULE_DEVICE_TABLE(hid, corsairpsu_idtable);
891d115b51eSWilken Gottwalt
892d115b51eSWilken Gottwalt static struct hid_driver corsairpsu_driver = {
893d115b51eSWilken Gottwalt .name = DRIVER_NAME,
894d115b51eSWilken Gottwalt .id_table = corsairpsu_idtable,
895d115b51eSWilken Gottwalt .probe = corsairpsu_probe,
896d115b51eSWilken Gottwalt .remove = corsairpsu_remove,
897d115b51eSWilken Gottwalt .raw_event = corsairpsu_raw_event,
8987656cd21SWilken Gottwalt #ifdef CONFIG_PM
8997656cd21SWilken Gottwalt .resume = corsairpsu_resume,
9007656cd21SWilken Gottwalt .reset_resume = corsairpsu_resume,
9017656cd21SWilken Gottwalt #endif
902d115b51eSWilken Gottwalt };
9033584858bSArmin Wolf
corsair_init(void)9043584858bSArmin Wolf static int __init corsair_init(void)
9053584858bSArmin Wolf {
9063584858bSArmin Wolf return hid_register_driver(&corsairpsu_driver);
9073584858bSArmin Wolf }
9083584858bSArmin Wolf
corsair_exit(void)9093584858bSArmin Wolf static void __exit corsair_exit(void)
9103584858bSArmin Wolf {
9113584858bSArmin Wolf hid_unregister_driver(&corsairpsu_driver);
9123584858bSArmin Wolf }
9133584858bSArmin Wolf
9143584858bSArmin Wolf /*
9153584858bSArmin Wolf * With module_init() the driver would load before the HID bus when
9163584858bSArmin Wolf * built-in, so use late_initcall() instead.
9173584858bSArmin Wolf */
9183584858bSArmin Wolf late_initcall(corsair_init);
9193584858bSArmin Wolf module_exit(corsair_exit);
920d115b51eSWilken Gottwalt
921d115b51eSWilken Gottwalt MODULE_LICENSE("GPL");
922d115b51eSWilken Gottwalt MODULE_AUTHOR("Wilken Gottwalt <wilken.gottwalt@posteo.net>");
923d115b51eSWilken Gottwalt MODULE_DESCRIPTION("Linux driver for Corsair power supplies with HID sensors interface");
924