| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2023 Thomas Weißschuh <[email protected]> |
| 4 |
*/ |
| 5 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 6 |
|
| 7 |
#include <linux/completion.h> |
| 8 |
#include <linux/device.h> |
| 9 |
#include <linux/dma-mapping.h> |
| 10 |
#include <linux/hwmon.h> |
| 11 |
#include <linux/module.h> |
| 12 |
#include <linux/mutex.h> |
| 13 |
#include <linux/types.h> |
| 14 |
#include <linux/usb.h> |
| 15 |
|
| 16 |
#define DRIVER_NAME "powerz" |
| 17 |
#define POWERZ_EP_CMD_OUT 0x01 |
| 18 |
#define POWERZ_EP_DATA_IN 0x81 |
| 19 |
|
| 20 |
struct powerz_sensor_data { |
| 21 |
u8 _unknown_1[8]; |
| 22 |
__le32 V_bus; |
| 23 |
__le32 I_bus; |
| 24 |
__le32 V_bus_avg; |
| 25 |
__le32 I_bus_avg; |
| 26 |
u8 _unknown_2[8]; |
| 27 |
u8 temp[2]; |
| 28 |
__le16 V_cc1; |
| 29 |
__le16 V_cc2; |
| 30 |
__le16 V_dp; |
| 31 |
__le16 V_dm; |
| 32 |
__le16 V_dd; |
| 33 |
u8 _unknown_3[4]; |
| 34 |
} __packed; |
| 35 |
|
| 36 |
struct powerz_priv { |
| 37 |
__dma_from_device_group_begin(); |
| 38 |
char transfer_buffer[64]; |
| 39 |
__dma_from_device_group_end(); |
| 40 |
struct mutex mutex; |
| 41 |
struct completion completion; |
| 42 |
struct urb *urb; |
| 43 |
int status; |
| 44 |
}; |
| 45 |
|
| 46 |
static const struct hwmon_channel_info *const powerz_info[] = { |
| 47 |
HWMON_CHANNEL_INFO(in, |
| 48 |
HWMON_I_INPUT | HWMON_I_LABEL | HWMON_I_AVERAGE, |
| 49 |
HWMON_I_INPUT | HWMON_I_LABEL, |
| 50 |
HWMON_I_INPUT | HWMON_I_LABEL, |
| 51 |
HWMON_I_INPUT | HWMON_I_LABEL, |
| 52 |
HWMON_I_INPUT | HWMON_I_LABEL, |
| 53 |
HWMON_I_INPUT | HWMON_I_LABEL), |
| 54 |
HWMON_CHANNEL_INFO(curr, |
| 55 |
HWMON_C_INPUT | HWMON_C_LABEL | HWMON_C_AVERAGE), |
| 56 |
HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL), |
| 57 |
NULL |
| 58 |
}; |
| 59 |
|
| 60 |
static int powerz_read_string(struct device *dev, enum hwmon_sensor_types type, |
| 61 |
u32 attr, int channel, const char **str) |
| 62 |
{ |
| 63 |
if (type == hwmon_curr && attr == hwmon_curr_label) { |
| 64 |
*str = "IBUS"; |
| 65 |
} else if (type == hwmon_in && attr == hwmon_in_label) { |
| 66 |
if (channel == 0) |
| 67 |
*str = "VBUS"; |
| 68 |
else if (channel == 1) |
| 69 |
*str = "VCC1"; |
| 70 |
else if (channel == 2) |
| 71 |
*str = "VCC2"; |
| 72 |
else if (channel == 3) |
| 73 |
*str = "VDP"; |
| 74 |
else if (channel == 4) |
| 75 |
*str = "VDM"; |
| 76 |
else if (channel == 5) |
| 77 |
*str = "VDD"; |
| 78 |
else |
| 79 |
return -EOPNOTSUPP; |
| 80 |
} else if (type == hwmon_temp && attr == hwmon_temp_label) { |
| 81 |
*str = "TEMP"; |
| 82 |
} else { |
| 83 |
return -EOPNOTSUPP; |
| 84 |
} |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static void powerz_usb_data_complete(struct urb *urb) |
| 90 |
{ |
| 91 |
struct powerz_priv *priv = urb->context; |
| 92 |
|
| 93 |
complete(&priv->completion); |
| 94 |
} |
| 95 |
|
| 96 |
static void powerz_usb_cmd_complete(struct urb *urb) |
| 97 |
{ |
| 98 |
struct powerz_priv *priv = urb->context; |
| 99 |
|
| 100 |
usb_fill_bulk_urb(urb, urb->dev, |
| 101 |
usb_rcvbulkpipe(urb->dev, POWERZ_EP_DATA_IN), |
| 102 |
priv->transfer_buffer, sizeof(priv->transfer_buffer), |
| 103 |
powerz_usb_data_complete, priv); |
| 104 |
|
| 105 |
priv->status = usb_submit_urb(urb, GFP_ATOMIC); |
| 106 |
if (priv->status) |
| 107 |
complete(&priv->completion); |
| 108 |
} |
| 109 |
|
| 110 |
static int powerz_read_data(struct usb_device *udev, struct powerz_priv *priv) |
| 111 |
{ |
| 112 |
long rc; |
| 113 |
int ret; |
| 114 |
|
| 115 |
if (!priv->urb) |
| 116 |
return -ENODEV; |
| 117 |
|
| 118 |
priv->status = -ETIMEDOUT; |
| 119 |
reinit_completion(&priv->completion); |
| 120 |
|
| 121 |
priv->transfer_buffer[0] = 0x0c; |
| 122 |
priv->transfer_buffer[1] = 0x00; |
| 123 |
priv->transfer_buffer[2] = 0x02; |
| 124 |
priv->transfer_buffer[3] = 0x00; |
| 125 |
|
| 126 |
usb_fill_bulk_urb(priv->urb, udev, |
| 127 |
usb_sndbulkpipe(udev, POWERZ_EP_CMD_OUT), |
| 128 |
priv->transfer_buffer, 4, powerz_usb_cmd_complete, |
| 129 |
priv); |
| 130 |
ret = usb_submit_urb(priv->urb, GFP_KERNEL); |
| 131 |
if (ret) |
| 132 |
return ret; |
| 133 |
|
| 134 |
rc = wait_for_completion_interruptible_timeout(&priv->completion, |
| 135 |
msecs_to_jiffies(5)); |
| 136 |
if (rc < 0) { |
| 137 |
usb_kill_urb(priv->urb); |
| 138 |
return rc; |
| 139 |
} |
| 140 |
|
| 141 |
if (rc == 0) { |
| 142 |
usb_kill_urb(priv->urb); |
| 143 |
return -EIO; |
| 144 |
} |
| 145 |
|
| 146 |
if (priv->urb->actual_length < sizeof(struct powerz_sensor_data)) |
| 147 |
return -EIO; |
| 148 |
|
| 149 |
return priv->status; |
| 150 |
} |
| 151 |
|
| 152 |
static int powerz_read(struct device *dev, enum hwmon_sensor_types type, |
| 153 |
u32 attr, int channel, long *val) |
| 154 |
{ |
| 155 |
struct usb_interface *intf = to_usb_interface(dev->parent); |
| 156 |
struct usb_device *udev = interface_to_usbdev(intf); |
| 157 |
struct powerz_priv *priv = usb_get_intfdata(intf); |
| 158 |
struct powerz_sensor_data *data; |
| 159 |
int ret; |
| 160 |
|
| 161 |
if (!priv) |
| 162 |
return -EIO; /* disconnected */ |
| 163 |
|
| 164 |
mutex_lock(&priv->mutex); |
| 165 |
ret = powerz_read_data(udev, priv); |
| 166 |
if (ret) |
| 167 |
goto out; |
| 168 |
|
| 169 |
data = (struct powerz_sensor_data *)priv->transfer_buffer; |
| 170 |
|
| 171 |
if (type == hwmon_curr) { |
| 172 |
if (attr == hwmon_curr_input) |
| 173 |
*val = ((s32)le32_to_cpu(data->I_bus)) / 1000; |
| 174 |
else if (attr == hwmon_curr_average) |
| 175 |
*val = ((s32)le32_to_cpu(data->I_bus_avg)) / 1000; |
| 176 |
else |
| 177 |
ret = -EOPNOTSUPP; |
| 178 |
} else if (type == hwmon_in) { |
| 179 |
if (attr == hwmon_in_input) { |
| 180 |
if (channel == 0) |
| 181 |
*val = le32_to_cpu(data->V_bus) / 1000; |
| 182 |
else if (channel == 1) |
| 183 |
*val = le16_to_cpu(data->V_cc1) / 10; |
| 184 |
else if (channel == 2) |
| 185 |
*val = le16_to_cpu(data->V_cc2) / 10; |
| 186 |
else if (channel == 3) |
| 187 |
*val = le16_to_cpu(data->V_dp) / 10; |
| 188 |
else if (channel == 4) |
| 189 |
*val = le16_to_cpu(data->V_dm) / 10; |
| 190 |
else if (channel == 5) |
| 191 |
*val = le16_to_cpu(data->V_dd) / 10; |
| 192 |
else |
| 193 |
ret = -EOPNOTSUPP; |
| 194 |
} else if (attr == hwmon_in_average && channel == 0) { |
| 195 |
*val = le32_to_cpu(data->V_bus_avg) / 1000; |
| 196 |
} else { |
| 197 |
ret = -EOPNOTSUPP; |
| 198 |
} |
| 199 |
} else if (type == hwmon_temp && attr == hwmon_temp_input) { |
| 200 |
*val = data->temp[1] * 2000 + data->temp[0] * 1000 / 128; |
| 201 |
} else { |
| 202 |
ret = -EOPNOTSUPP; |
| 203 |
} |
| 204 |
|
| 205 |
out: |
| 206 |
mutex_unlock(&priv->mutex); |
| 207 |
return ret; |
| 208 |
} |
| 209 |
|
| 210 |
static const struct hwmon_ops powerz_hwmon_ops = { |
| 211 |
.visible = 0444, |
| 212 |
.read = powerz_read, |
| 213 |
.read_string = powerz_read_string, |
| 214 |
}; |
| 215 |
|
| 216 |
static const struct hwmon_chip_info powerz_chip_info = { |
| 217 |
.ops = &powerz_hwmon_ops, |
| 218 |
.info = powerz_info, |
| 219 |
}; |
| 220 |
|
| 221 |
static int powerz_probe(struct usb_interface *intf, |
| 222 |
const struct usb_device_id *id) |
| 223 |
{ |
| 224 |
struct powerz_priv *priv; |
| 225 |
struct device *hwmon_dev; |
| 226 |
struct device *parent; |
| 227 |
|
| 228 |
parent = &intf->dev; |
| 229 |
|
| 230 |
priv = devm_kzalloc(parent, sizeof(*priv), GFP_KERNEL); |
| 231 |
if (!priv) |
| 232 |
return -ENOMEM; |
| 233 |
|
| 234 |
priv->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 235 |
if (!priv->urb) |
| 236 |
return -ENOMEM; |
| 237 |
mutex_init(&priv->mutex); |
| 238 |
init_completion(&priv->completion); |
| 239 |
|
| 240 |
usb_set_intfdata(intf, priv); |
| 241 |
|
| 242 |
hwmon_dev = |
| 243 |
devm_hwmon_device_register_with_info(parent, DRIVER_NAME, priv, |
| 244 |
&powerz_chip_info, NULL); |
| 245 |
if (IS_ERR(hwmon_dev)) { |
| 246 |
usb_free_urb(priv->urb); |
| 247 |
return PTR_ERR(hwmon_dev); |
| 248 |
} |
| 249 |
|
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
static void powerz_disconnect(struct usb_interface *intf) |
| 254 |
{ |
| 255 |
struct powerz_priv *priv = usb_get_intfdata(intf); |
| 256 |
|
| 257 |
mutex_lock(&priv->mutex); |
| 258 |
usb_kill_urb(priv->urb); |
| 259 |
usb_free_urb(priv->urb); |
| 260 |
priv->urb = NULL; |
| 261 |
mutex_unlock(&priv->mutex); |
| 262 |
} |
| 263 |
|
| 264 |
static const struct usb_device_id powerz_id_table[] = { |
| 265 |
{ USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0061, 0x00) }, /* ChargerLAB POWER-Z KM002C */ |
| 266 |
{ USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0063, 0x00) }, /* ChargerLAB POWER-Z KM003C */ |
| 267 |
{ } |
| 268 |
}; |
| 269 |
|
| 270 |
MODULE_DEVICE_TABLE(usb, powerz_id_table); |
| 271 |
|
| 272 |
static struct usb_driver powerz_driver = { |
| 273 |
.name = DRIVER_NAME, |
| 274 |
.id_table = powerz_id_table, |
| 275 |
.probe = powerz_probe, |
| 276 |
.disconnect = powerz_disconnect, |
| 277 |
}; |
| 278 |
|
| 279 |
module_usb_driver(powerz_driver); |
| 280 |
|
| 281 |
MODULE_LICENSE("GPL"); |
| 282 |
MODULE_AUTHOR("Thomas Weißschuh <[email protected]>"); |
| 283 |
MODULE_DESCRIPTION("ChargerLAB POWER-Z USB-C tester"); |