| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (C) 2023 Thomas Weißschuh <linux@weissschuh.net> |
| 4 | */ |
| 5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 6 | |
| 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> |
| 14 | |
| 15 | #define DRIVER_NAME "powerz" |
| 16 | #define POWERZ_EP_CMD_OUT 0x01 |
| 17 | #define POWERZ_EP_DATA_IN 0x81 |
| 18 | |
| 19 | struct powerz_sensor_data { |
| 20 | u8 _unknown_1[8]; |
| 21 | __le32 V_bus; |
| 22 | __le32 I_bus; |
| 23 | __le32 V_bus_avg; |
| 24 | __le32 I_bus_avg; |
| 25 | u8 _unknown_2[8]; |
| 26 | u8 temp[2]; |
| 27 | __le16 V_cc1; |
| 28 | __le16 V_cc2; |
| 29 | __le16 V_dp; |
| 30 | __le16 V_dm; |
| 31 | __le16 V_dd; |
| 32 | u8 _unknown_3[4]; |
| 33 | } __packed; |
| 34 | |
| 35 | struct powerz_priv { |
| 36 | char transfer_buffer[64]; /* first member to satisfy DMA alignment */ |
| 37 | struct mutex mutex; |
| 38 | struct completion completion; |
| 39 | struct urb *urb; |
| 40 | int status; |
| 41 | }; |
| 42 | |
| 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), |
| 54 | NULL |
| 55 | }; |
| 56 | |
| 57 | static int powerz_read_string(struct device *dev, enum hwmon_sensor_types type, |
| 58 | u32 attr, int channel, const char **str) |
| 59 | { |
| 60 | if (type == hwmon_curr && attr == hwmon_curr_label) { |
| 61 | *str = "IBUS" ; |
| 62 | } else if (type == hwmon_in && attr == hwmon_in_label) { |
| 63 | if (channel == 0) |
| 64 | *str = "VBUS" ; |
| 65 | else if (channel == 1) |
| 66 | *str = "VCC1" ; |
| 67 | else if (channel == 2) |
| 68 | *str = "VCC2" ; |
| 69 | else if (channel == 3) |
| 70 | *str = "VDP" ; |
| 71 | else if (channel == 4) |
| 72 | *str = "VDM" ; |
| 73 | else if (channel == 5) |
| 74 | *str = "VDD" ; |
| 75 | else |
| 76 | return -EOPNOTSUPP; |
| 77 | } else if (type == hwmon_temp && attr == hwmon_temp_label) { |
| 78 | *str = "TEMP" ; |
| 79 | } else { |
| 80 | return -EOPNOTSUPP; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static void powerz_usb_data_complete(struct urb *urb) |
| 87 | { |
| 88 | struct powerz_priv *priv = urb->context; |
| 89 | |
| 90 | complete(&priv->completion); |
| 91 | } |
| 92 | |
| 93 | static void powerz_usb_cmd_complete(struct urb *urb) |
| 94 | { |
| 95 | struct powerz_priv *priv = urb->context; |
| 96 | |
| 97 | usb_fill_bulk_urb(urb, dev: urb->dev, |
| 98 | usb_rcvbulkpipe(urb->dev, POWERZ_EP_DATA_IN), |
| 99 | transfer_buffer: priv->transfer_buffer, buffer_length: sizeof(priv->transfer_buffer), |
| 100 | complete_fn: powerz_usb_data_complete, context: priv); |
| 101 | |
| 102 | priv->status = usb_submit_urb(urb, GFP_ATOMIC); |
| 103 | if (priv->status) |
| 104 | complete(&priv->completion); |
| 105 | } |
| 106 | |
| 107 | static int powerz_read_data(struct usb_device *udev, struct powerz_priv *priv) |
| 108 | { |
| 109 | int ret; |
| 110 | |
| 111 | priv->status = -ETIMEDOUT; |
| 112 | reinit_completion(x: &priv->completion); |
| 113 | |
| 114 | priv->transfer_buffer[0] = 0x0c; |
| 115 | priv->transfer_buffer[1] = 0x00; |
| 116 | priv->transfer_buffer[2] = 0x02; |
| 117 | priv->transfer_buffer[3] = 0x00; |
| 118 | |
| 119 | usb_fill_bulk_urb(urb: priv->urb, dev: udev, |
| 120 | usb_sndbulkpipe(udev, POWERZ_EP_CMD_OUT), |
| 121 | transfer_buffer: priv->transfer_buffer, buffer_length: 4, complete_fn: powerz_usb_cmd_complete, |
| 122 | context: priv); |
| 123 | ret = usb_submit_urb(urb: priv->urb, GFP_KERNEL); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | |
| 127 | if (!wait_for_completion_interruptible_timeout |
| 128 | (x: &priv->completion, timeout: msecs_to_jiffies(m: 5))) { |
| 129 | usb_kill_urb(urb: priv->urb); |
| 130 | return -EIO; |
| 131 | } |
| 132 | |
| 133 | if (priv->urb->actual_length < sizeof(struct powerz_sensor_data)) |
| 134 | return -EIO; |
| 135 | |
| 136 | return priv->status; |
| 137 | } |
| 138 | |
| 139 | static int powerz_read(struct device *dev, enum hwmon_sensor_types type, |
| 140 | u32 attr, int channel, long *val) |
| 141 | { |
| 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; |
| 146 | int ret; |
| 147 | |
| 148 | if (!priv) |
| 149 | return -EIO; /* disconnected */ |
| 150 | |
| 151 | mutex_lock(&priv->mutex); |
| 152 | ret = powerz_read_data(udev, priv); |
| 153 | if (ret) |
| 154 | goto out; |
| 155 | |
| 156 | data = (struct powerz_sensor_data *)priv->transfer_buffer; |
| 157 | |
| 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; |
| 163 | else |
| 164 | ret = -EOPNOTSUPP; |
| 165 | } else if (type == hwmon_in) { |
| 166 | if (attr == hwmon_in_input) { |
| 167 | if (channel == 0) |
| 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; |
| 179 | else |
| 180 | ret = -EOPNOTSUPP; |
| 181 | } else if (attr == hwmon_in_average && channel == 0) { |
| 182 | *val = le32_to_cpu(data->V_bus_avg) / 1000; |
| 183 | } else { |
| 184 | ret = -EOPNOTSUPP; |
| 185 | } |
| 186 | } else if (type == hwmon_temp && attr == hwmon_temp_input) { |
| 187 | *val = data->temp[1] * 2000 + data->temp[0] * 1000 / 128; |
| 188 | } else { |
| 189 | ret = -EOPNOTSUPP; |
| 190 | } |
| 191 | |
| 192 | out: |
| 193 | mutex_unlock(lock: &priv->mutex); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static const struct hwmon_ops powerz_hwmon_ops = { |
| 198 | .visible = 0444, |
| 199 | .read = powerz_read, |
| 200 | .read_string = powerz_read_string, |
| 201 | }; |
| 202 | |
| 203 | static const struct hwmon_chip_info powerz_chip_info = { |
| 204 | .ops = &powerz_hwmon_ops, |
| 205 | .info = powerz_info, |
| 206 | }; |
| 207 | |
| 208 | static int powerz_probe(struct usb_interface *intf, |
| 209 | const struct usb_device_id *id) |
| 210 | { |
| 211 | struct powerz_priv *priv; |
| 212 | struct device *hwmon_dev; |
| 213 | struct device *parent; |
| 214 | |
| 215 | parent = &intf->dev; |
| 216 | |
| 217 | priv = devm_kzalloc(dev: parent, size: sizeof(*priv), GFP_KERNEL); |
| 218 | if (!priv) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | priv->urb = usb_alloc_urb(iso_packets: 0, GFP_KERNEL); |
| 222 | if (!priv->urb) |
| 223 | return -ENOMEM; |
| 224 | mutex_init(&priv->mutex); |
| 225 | init_completion(x: &priv->completion); |
| 226 | |
| 227 | hwmon_dev = |
| 228 | devm_hwmon_device_register_with_info(dev: parent, DRIVER_NAME, drvdata: priv, |
| 229 | info: &powerz_chip_info, NULL); |
| 230 | if (IS_ERR(ptr: hwmon_dev)) { |
| 231 | usb_free_urb(urb: priv->urb); |
| 232 | return PTR_ERR(ptr: hwmon_dev); |
| 233 | } |
| 234 | |
| 235 | usb_set_intfdata(intf, data: priv); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static void powerz_disconnect(struct usb_interface *intf) |
| 241 | { |
| 242 | struct powerz_priv *priv = usb_get_intfdata(intf); |
| 243 | |
| 244 | mutex_lock(&priv->mutex); |
| 245 | usb_kill_urb(urb: priv->urb); |
| 246 | usb_free_urb(urb: priv->urb); |
| 247 | mutex_unlock(lock: &priv->mutex); |
| 248 | } |
| 249 | |
| 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 */ |
| 253 | { } |
| 254 | }; |
| 255 | |
| 256 | MODULE_DEVICE_TABLE(usb, powerz_id_table); |
| 257 | |
| 258 | static struct usb_driver powerz_driver = { |
| 259 | .name = DRIVER_NAME, |
| 260 | .id_table = powerz_id_table, |
| 261 | .probe = powerz_probe, |
| 262 | .disconnect = powerz_disconnect, |
| 263 | }; |
| 264 | |
| 265 | module_usb_driver(powerz_driver); |
| 266 | |
| 267 | MODULE_LICENSE("GPL" ); |
| 268 | MODULE_AUTHOR("Thomas Weißschuh <linux@weissschuh.net>" ); |
| 269 | MODULE_DESCRIPTION("ChargerLAB POWER-Z USB-C tester" ); |
| 270 | |