1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2023 Thomas Weißschuh <linux@weissschuh.net>
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/completion.h>
8 #include <linux/device.h>
9 #include <linux/hwmon.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/types.h>
13 #include <linux/usb.h>
15 #define DRIVER_NAME "powerz"
16 #define POWERZ_EP_CMD_OUT 0x01
17 #define POWERZ_EP_DATA_IN 0x81
19 struct powerz_sensor_data
{
36 char transfer_buffer
[64]; /* first member to satisfy DMA alignment */
38 struct completion completion
;
43 static const struct hwmon_channel_info
*const powerz_info
[] = {
44 HWMON_CHANNEL_INFO(in
,
45 HWMON_I_INPUT
| HWMON_I_LABEL
| HWMON_I_AVERAGE
,
46 HWMON_I_INPUT
| HWMON_I_LABEL
,
47 HWMON_I_INPUT
| HWMON_I_LABEL
,
48 HWMON_I_INPUT
| HWMON_I_LABEL
,
49 HWMON_I_INPUT
| HWMON_I_LABEL
,
50 HWMON_I_INPUT
| HWMON_I_LABEL
),
51 HWMON_CHANNEL_INFO(curr
,
52 HWMON_C_INPUT
| HWMON_C_LABEL
| HWMON_C_AVERAGE
),
53 HWMON_CHANNEL_INFO(temp
, HWMON_T_INPUT
| HWMON_T_LABEL
),
57 static int powerz_read_string(struct device
*dev
, enum hwmon_sensor_types type
,
58 u32 attr
, int channel
, const char **str
)
60 if (type
== hwmon_curr
&& attr
== hwmon_curr_label
) {
62 } else if (type
== hwmon_in
&& attr
== hwmon_in_label
) {
65 else if (channel
== 1)
67 else if (channel
== 2)
69 else if (channel
== 3)
71 else if (channel
== 4)
73 else if (channel
== 5)
77 } else if (type
== hwmon_temp
&& attr
== hwmon_temp_label
) {
86 static void powerz_usb_data_complete(struct urb
*urb
)
88 struct powerz_priv
*priv
= urb
->context
;
90 complete(&priv
->completion
);
93 static void powerz_usb_cmd_complete(struct urb
*urb
)
95 struct powerz_priv
*priv
= urb
->context
;
97 usb_fill_bulk_urb(urb
, urb
->dev
,
98 usb_rcvbulkpipe(urb
->dev
, POWERZ_EP_DATA_IN
),
99 priv
->transfer_buffer
, sizeof(priv
->transfer_buffer
),
100 powerz_usb_data_complete
, priv
);
102 priv
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
104 complete(&priv
->completion
);
107 static int powerz_read_data(struct usb_device
*udev
, struct powerz_priv
*priv
)
111 priv
->status
= -ETIMEDOUT
;
112 reinit_completion(&priv
->completion
);
114 priv
->transfer_buffer
[0] = 0x0c;
115 priv
->transfer_buffer
[1] = 0x00;
116 priv
->transfer_buffer
[2] = 0x02;
117 priv
->transfer_buffer
[3] = 0x00;
119 usb_fill_bulk_urb(priv
->urb
, udev
,
120 usb_sndbulkpipe(udev
, POWERZ_EP_CMD_OUT
),
121 priv
->transfer_buffer
, 4, powerz_usb_cmd_complete
,
123 ret
= usb_submit_urb(priv
->urb
, GFP_KERNEL
);
127 if (!wait_for_completion_interruptible_timeout
128 (&priv
->completion
, msecs_to_jiffies(5))) {
129 usb_kill_urb(priv
->urb
);
133 if (priv
->urb
->actual_length
< sizeof(struct powerz_sensor_data
))
139 static int powerz_read(struct device
*dev
, enum hwmon_sensor_types type
,
140 u32 attr
, int channel
, long *val
)
142 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
143 struct usb_device
*udev
= interface_to_usbdev(intf
);
144 struct powerz_priv
*priv
= usb_get_intfdata(intf
);
145 struct powerz_sensor_data
*data
;
149 return -EIO
; /* disconnected */
151 mutex_lock(&priv
->mutex
);
152 ret
= powerz_read_data(udev
, priv
);
156 data
= (struct powerz_sensor_data
*)priv
->transfer_buffer
;
158 if (type
== hwmon_curr
) {
159 if (attr
== hwmon_curr_input
)
160 *val
= ((s32
)le32_to_cpu(data
->I_bus
)) / 1000;
161 else if (attr
== hwmon_curr_average
)
162 *val
= ((s32
)le32_to_cpu(data
->I_bus_avg
)) / 1000;
165 } else if (type
== hwmon_in
) {
166 if (attr
== hwmon_in_input
) {
168 *val
= le32_to_cpu(data
->V_bus
) / 1000;
169 else if (channel
== 1)
170 *val
= le16_to_cpu(data
->V_cc1
) / 10;
171 else if (channel
== 2)
172 *val
= le16_to_cpu(data
->V_cc2
) / 10;
173 else if (channel
== 3)
174 *val
= le16_to_cpu(data
->V_dp
) / 10;
175 else if (channel
== 4)
176 *val
= le16_to_cpu(data
->V_dm
) / 10;
177 else if (channel
== 5)
178 *val
= le16_to_cpu(data
->V_dd
) / 10;
181 } else if (attr
== hwmon_in_average
&& channel
== 0) {
182 *val
= le32_to_cpu(data
->V_bus_avg
) / 1000;
186 } else if (type
== hwmon_temp
&& attr
== hwmon_temp_input
) {
187 *val
= data
->temp
[1] * 2000 + data
->temp
[0] * 1000 / 128;
193 mutex_unlock(&priv
->mutex
);
197 static const struct hwmon_ops powerz_hwmon_ops
= {
200 .read_string
= powerz_read_string
,
203 static const struct hwmon_chip_info powerz_chip_info
= {
204 .ops
= &powerz_hwmon_ops
,
208 static int powerz_probe(struct usb_interface
*intf
,
209 const struct usb_device_id
*id
)
211 struct powerz_priv
*priv
;
212 struct device
*hwmon_dev
;
213 struct device
*parent
;
217 priv
= devm_kzalloc(parent
, sizeof(*priv
), GFP_KERNEL
);
221 priv
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
224 mutex_init(&priv
->mutex
);
225 init_completion(&priv
->completion
);
228 devm_hwmon_device_register_with_info(parent
, DRIVER_NAME
, priv
,
229 &powerz_chip_info
, NULL
);
230 if (IS_ERR(hwmon_dev
)) {
231 usb_free_urb(priv
->urb
);
232 return PTR_ERR(hwmon_dev
);
235 usb_set_intfdata(intf
, priv
);
240 static void powerz_disconnect(struct usb_interface
*intf
)
242 struct powerz_priv
*priv
= usb_get_intfdata(intf
);
244 mutex_lock(&priv
->mutex
);
245 usb_kill_urb(priv
->urb
);
246 usb_free_urb(priv
->urb
);
247 mutex_unlock(&priv
->mutex
);
250 static const struct usb_device_id powerz_id_table
[] = {
251 { USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0061, 0x00) }, /* ChargerLAB POWER-Z KM002C */
252 { USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0063, 0x00) }, /* ChargerLAB POWER-Z KM003C */
256 MODULE_DEVICE_TABLE(usb
, powerz_id_table
);
258 static struct usb_driver powerz_driver
= {
260 .id_table
= powerz_id_table
,
261 .probe
= powerz_probe
,
262 .disconnect
= powerz_disconnect
,
265 module_usb_driver(powerz_driver
);
267 MODULE_LICENSE("GPL");
268 MODULE_AUTHOR("Thomas Weißschuh <linux@weissschuh.net>");
269 MODULE_DESCRIPTION("ChargerLAB POWER-Z USB-C tester");