File Index
Files and directories
Sign in
FILE

powerz.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hwmon/powerz.c C
  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
 19struct 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
 35struct 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
 43static 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
 57static 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
 86static void powerz_usb_data_complete(struct urb *urb)
 87{
 88	struct powerz_priv *priv = urb->context;
 89
 90	complete(&priv->completion);
 91}
 92
 93static void powerz_usb_cmd_complete(struct urb *urb)
 94{
 95	struct powerz_priv *priv = urb->context;
 96
 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);
101
102	priv->status = usb_submit_urb(urb, GFP_ATOMIC);
103	if (priv->status)
104		complete(&priv->completion);
105}
106
107static int powerz_read_data(struct usb_device *udev, struct powerz_priv *priv)
108{
109	int ret;
110
111	if (!priv->urb)
112		return -ENODEV;
113
114	priv->status = -ETIMEDOUT;
115	reinit_completion(&priv->completion);
116
117	priv->transfer_buffer[0] = 0x0c;
118	priv->transfer_buffer[1] = 0x00;
119	priv->transfer_buffer[2] = 0x02;
120	priv->transfer_buffer[3] = 0x00;
121
122	usb_fill_bulk_urb(priv->urb, udev,
123			  usb_sndbulkpipe(udev, POWERZ_EP_CMD_OUT),
124			  priv->transfer_buffer, 4, powerz_usb_cmd_complete,
125			  priv);
126	ret = usb_submit_urb(priv->urb, GFP_KERNEL);
127	if (ret)
128		return ret;
129
130	if (!wait_for_completion_interruptible_timeout
131	    (&priv->completion, msecs_to_jiffies(5))) {
132		usb_kill_urb(priv->urb);
133		return -EIO;
134	}
135
136	if (priv->urb->actual_length < sizeof(struct powerz_sensor_data))
137		return -EIO;
138
139	return priv->status;
140}
141
142static int powerz_read(struct device *dev, enum hwmon_sensor_types type,
143		       u32 attr, int channel, long *val)
144{
145	struct usb_interface *intf = to_usb_interface(dev->parent);
146	struct usb_device *udev = interface_to_usbdev(intf);
147	struct powerz_priv *priv = usb_get_intfdata(intf);
148	struct powerz_sensor_data *data;
149	int ret;
150
151	if (!priv)
152		return -EIO;	/* disconnected */
153
154	mutex_lock(&priv->mutex);
155	ret = powerz_read_data(udev, priv);
156	if (ret)
157		goto out;
158
159	data = (struct powerz_sensor_data *)priv->transfer_buffer;
160
161	if (type == hwmon_curr) {
162		if (attr == hwmon_curr_input)
163			*val = ((s32)le32_to_cpu(data->I_bus)) / 1000;
164		else if (attr == hwmon_curr_average)
165			*val = ((s32)le32_to_cpu(data->I_bus_avg)) / 1000;
166		else
167			ret = -EOPNOTSUPP;
168	} else if (type == hwmon_in) {
169		if (attr == hwmon_in_input) {
170			if (channel == 0)
171				*val = le32_to_cpu(data->V_bus) / 1000;
172			else if (channel == 1)
173				*val = le16_to_cpu(data->V_cc1) / 10;
174			else if (channel == 2)
175				*val = le16_to_cpu(data->V_cc2) / 10;
176			else if (channel == 3)
177				*val = le16_to_cpu(data->V_dp) / 10;
178			else if (channel == 4)
179				*val = le16_to_cpu(data->V_dm) / 10;
180			else if (channel == 5)
181				*val = le16_to_cpu(data->V_dd) / 10;
182			else
183				ret = -EOPNOTSUPP;
184		} else if (attr == hwmon_in_average && channel == 0) {
185			*val = le32_to_cpu(data->V_bus_avg) / 1000;
186		} else {
187			ret = -EOPNOTSUPP;
188		}
189	} else if (type == hwmon_temp && attr == hwmon_temp_input) {
190		*val = data->temp[1] * 2000 + data->temp[0] * 1000 / 128;
191	} else {
192		ret = -EOPNOTSUPP;
193	}
194
195out:
196	mutex_unlock(&priv->mutex);
197	return ret;
198}
199
200static const struct hwmon_ops powerz_hwmon_ops = {
201	.visible = 0444,
202	.read = powerz_read,
203	.read_string = powerz_read_string,
204};
205
206static const struct hwmon_chip_info powerz_chip_info = {
207	.ops = &powerz_hwmon_ops,
208	.info = powerz_info,
209};
210
211static int powerz_probe(struct usb_interface *intf,
212			const struct usb_device_id *id)
213{
214	struct powerz_priv *priv;
215	struct device *hwmon_dev;
216	struct device *parent;
217
218	parent = &intf->dev;
219
220	priv = devm_kzalloc(parent, sizeof(*priv), GFP_KERNEL);
221	if (!priv)
222		return -ENOMEM;
223
224	priv->urb = usb_alloc_urb(0, GFP_KERNEL);
225	if (!priv->urb)
226		return -ENOMEM;
227	mutex_init(&priv->mutex);
228	init_completion(&priv->completion);
229
230	usb_set_intfdata(intf, priv);
231
232	hwmon_dev =
233	    devm_hwmon_device_register_with_info(parent, DRIVER_NAME, priv,
234						 &powerz_chip_info, NULL);
235	if (IS_ERR(hwmon_dev)) {
236		usb_free_urb(priv->urb);
237		return PTR_ERR(hwmon_dev);
238	}
239
240	return 0;
241}
242
243static void powerz_disconnect(struct usb_interface *intf)
244{
245	struct powerz_priv *priv = usb_get_intfdata(intf);
246
247	mutex_lock(&priv->mutex);
248	usb_kill_urb(priv->urb);
249	usb_free_urb(priv->urb);
250	priv->urb = NULL;
251	mutex_unlock(&priv->mutex);
252}
253
254static const struct usb_device_id powerz_id_table[] = {
255	{ USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0061, 0x00) },	/* ChargerLAB POWER-Z KM002C */
256	{ USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0063, 0x00) },	/* ChargerLAB POWER-Z KM003C */
257	{ }
258};
259
260MODULE_DEVICE_TABLE(usb, powerz_id_table);
261
262static struct usb_driver powerz_driver = {
263	.name = DRIVER_NAME,
264	.id_table = powerz_id_table,
265	.probe = powerz_probe,
266	.disconnect = powerz_disconnect,
267};
268
269module_usb_driver(powerz_driver);
270
271MODULE_LICENSE("GPL");
272MODULE_AUTHOR("Thomas Weißschuh <linux@weissschuh.net>");
273MODULE_DESCRIPTION("ChargerLAB POWER-Z USB-C tester");
~/ Read-only file preview.