1 // SPDX-License-Identifier: GPL-2.0-only
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 umode_t powerz_is_visible(const void *data, enum hwmon_sensor_types type,
58 u32 attr, int channel)
63 static int powerz_read_string(struct device *dev, enum hwmon_sensor_types type,
64 u32 attr, int channel, const char **str)
66 if (type == hwmon_curr && attr == hwmon_curr_label) {
68 } else if (type == hwmon_in && attr == hwmon_in_label) {
71 else if (channel == 1)
73 else if (channel == 2)
75 else if (channel == 3)
77 else if (channel == 4)
79 else if (channel == 5)
83 } else if (type == hwmon_temp && attr == hwmon_temp_label) {
92 static void powerz_usb_data_complete(struct urb *urb)
94 struct powerz_priv *priv = urb->context;
96 complete(&priv->completion);
99 static void powerz_usb_cmd_complete(struct urb *urb)
101 struct powerz_priv *priv = urb->context;
103 usb_fill_bulk_urb(urb, urb->dev,
104 usb_rcvbulkpipe(urb->dev, POWERZ_EP_DATA_IN),
105 priv->transfer_buffer, sizeof(priv->transfer_buffer),
106 powerz_usb_data_complete, priv);
108 priv->status = usb_submit_urb(urb, GFP_ATOMIC);
110 complete(&priv->completion);
113 static int powerz_read_data(struct usb_device *udev, struct powerz_priv *priv)
117 priv->status = -ETIMEDOUT;
118 reinit_completion(&priv->completion);
120 priv->transfer_buffer[0] = 0x0c;
121 priv->transfer_buffer[1] = 0x00;
122 priv->transfer_buffer[2] = 0x02;
123 priv->transfer_buffer[3] = 0x00;
125 usb_fill_bulk_urb(priv->urb, udev,
126 usb_sndbulkpipe(udev, POWERZ_EP_CMD_OUT),
127 priv->transfer_buffer, 4, powerz_usb_cmd_complete,
129 ret = usb_submit_urb(priv->urb, GFP_KERNEL);
133 if (!wait_for_completion_interruptible_timeout
134 (&priv->completion, msecs_to_jiffies(5))) {
135 usb_kill_urb(priv->urb);
139 if (priv->urb->actual_length < sizeof(struct powerz_sensor_data))
145 static int powerz_read(struct device *dev, enum hwmon_sensor_types type,
146 u32 attr, int channel, long *val)
148 struct usb_interface *intf = to_usb_interface(dev->parent);
149 struct usb_device *udev = interface_to_usbdev(intf);
150 struct powerz_priv *priv = usb_get_intfdata(intf);
151 struct powerz_sensor_data *data;
155 return -EIO; /* disconnected */
157 mutex_lock(&priv->mutex);
158 ret = powerz_read_data(udev, priv);
162 data = (struct powerz_sensor_data *)priv->transfer_buffer;
164 if (type == hwmon_curr) {
165 if (attr == hwmon_curr_input)
166 *val = ((s32)le32_to_cpu(data->I_bus)) / 1000;
167 else if (attr == hwmon_curr_average)
168 *val = ((s32)le32_to_cpu(data->I_bus_avg)) / 1000;
171 } else if (type == hwmon_in) {
172 if (attr == hwmon_in_input) {
174 *val = le32_to_cpu(data->V_bus) / 1000;
175 else if (channel == 1)
176 *val = le16_to_cpu(data->V_cc1) / 10;
177 else if (channel == 2)
178 *val = le16_to_cpu(data->V_cc2) / 10;
179 else if (channel == 3)
180 *val = le16_to_cpu(data->V_dp) / 10;
181 else if (channel == 4)
182 *val = le16_to_cpu(data->V_dm) / 10;
183 else if (channel == 5)
184 *val = le16_to_cpu(data->V_dd) / 10;
187 } else if (attr == hwmon_in_average && channel == 0) {
188 *val = le32_to_cpu(data->V_bus_avg) / 1000;
192 } else if (type == hwmon_temp && attr == hwmon_temp_input) {
193 *val = data->temp[1] * 2000 + data->temp[0] * 1000 / 128;
199 mutex_unlock(&priv->mutex);
203 static const struct hwmon_ops powerz_hwmon_ops = {
204 .is_visible = powerz_is_visible,
206 .read_string = powerz_read_string,
209 static const struct hwmon_chip_info powerz_chip_info = {
210 .ops = &powerz_hwmon_ops,
214 static int powerz_probe(struct usb_interface *intf,
215 const struct usb_device_id *id)
217 struct powerz_priv *priv;
218 struct device *hwmon_dev;
219 struct device *parent;
223 priv = devm_kzalloc(parent, sizeof(*priv), GFP_KERNEL);
227 priv->urb = usb_alloc_urb(0, GFP_KERNEL);
230 mutex_init(&priv->mutex);
231 init_completion(&priv->completion);
234 devm_hwmon_device_register_with_info(parent, DRIVER_NAME, priv,
235 &powerz_chip_info, NULL);
236 if (IS_ERR(hwmon_dev)) {
237 usb_free_urb(priv->urb);
238 return PTR_ERR(hwmon_dev);
241 usb_set_intfdata(intf, priv);
246 static void powerz_disconnect(struct usb_interface *intf)
248 struct powerz_priv *priv = usb_get_intfdata(intf);
250 mutex_lock(&priv->mutex);
251 usb_kill_urb(priv->urb);
252 usb_free_urb(priv->urb);
253 mutex_unlock(&priv->mutex);
256 static const struct usb_device_id powerz_id_table[] = {
257 { USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0061, 0x00) }, /* ChargerLAB POWER-Z KM002C */
258 { USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0063, 0x00) }, /* ChargerLAB POWER-Z KM003C */
262 MODULE_DEVICE_TABLE(usb, powerz_id_table);
264 static struct usb_driver powerz_driver = {
266 .id_table = powerz_id_table,
267 .probe = powerz_probe,
268 .disconnect = powerz_disconnect,
271 module_usb_driver(powerz_driver);
273 MODULE_LICENSE("GPL");
275 MODULE_DESCRIPTION("ChargerLAB POWER-Z USB-C tester");