summaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/powerz.c
blob: cfb635f94d6654da0052e235df0a2a49473f1982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// SPDX-License-Identifier: GPL-2.0-only
/*
 *  Copyright (C) 2023 Thomas Weißschuh <linux@weissschuh.net>
 */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/completion.h>
#include <linux/device.h>
#include <linux/hwmon.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/types.h>
#include <linux/usb.h>

#define DRIVER_NAME	"powerz"
#define POWERZ_EP_CMD_OUT	0x01
#define POWERZ_EP_DATA_IN	0x81

struct powerz_sensor_data {
	u8 _unknown_1[8];
	__le32 V_bus;
	__le32 I_bus;
	__le32 V_bus_avg;
	__le32 I_bus_avg;
	u8 _unknown_2[8];
	u8 temp[2];
	__le16 V_cc1;
	__le16 V_cc2;
	__le16 V_dp;
	__le16 V_dm;
	__le16 V_dd;
	u8 _unknown_3[4];
} __packed;

struct powerz_priv {
	char transfer_buffer[64];	/* first member to satisfy DMA alignment */
	struct mutex mutex;
	struct completion completion;
	struct urb *urb;
	int status;
};

static const struct hwmon_channel_info *const powerz_info[] = {
	HWMON_CHANNEL_INFO(in,
			   HWMON_I_INPUT | HWMON_I_LABEL | HWMON_I_AVERAGE,
			   HWMON_I_INPUT | HWMON_I_LABEL,
			   HWMON_I_INPUT | HWMON_I_LABEL,
			   HWMON_I_INPUT | HWMON_I_LABEL,
			   HWMON_I_INPUT | HWMON_I_LABEL,
			   HWMON_I_INPUT | HWMON_I_LABEL),
	HWMON_CHANNEL_INFO(curr,
			   HWMON_C_INPUT | HWMON_C_LABEL | HWMON_C_AVERAGE),
	    HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
	NULL
};

static umode_t powerz_is_visible(const void *data, enum hwmon_sensor_types type,
				 u32 attr, int channel)
{
	return 0444;
}

static int powerz_read_string(struct device *dev, enum hwmon_sensor_types type,
			      u32 attr, int channel, const char **str)
{
	if (type == hwmon_curr && attr == hwmon_curr_label) {
		*str = "IBUS";
	} else if (type == hwmon_in && attr == hwmon_in_label) {
		if (channel == 0)
			*str = "VBUS";
		else if (channel == 1)
			*str = "VCC1";
		else if (channel == 2)
			*str = "VCC2";
		else if (channel == 3)
			*str = "VDP";
		else if (channel == 4)
			*str = "VDM";
		else if (channel == 5)
			*str = "VDD";
		else
			return -EOPNOTSUPP;
	} else if (type == hwmon_temp && attr == hwmon_temp_label) {
		*str = "TEMP";
	} else {
		return -EOPNOTSUPP;
	}

	return 0;
}

static void powerz_usb_data_complete(struct urb *urb)
{
	struct powerz_priv *priv = urb->context;

	complete(&priv->completion);
}

static void powerz_usb_cmd_complete(struct urb *urb)
{
	struct powerz_priv *priv = urb->context;

	usb_fill_bulk_urb(urb, urb->dev,
			  usb_rcvbulkpipe(urb->dev, POWERZ_EP_DATA_IN),
			  priv->transfer_buffer, sizeof(priv->transfer_buffer),
			  powerz_usb_data_complete, priv);

	priv->status = usb_submit_urb(urb, GFP_ATOMIC);
	if (priv->status)
		complete(&priv->completion);
}

static int powerz_read_data(struct usb_device *udev, struct powerz_priv *priv)
{
	int ret;

	priv->status = -ETIMEDOUT;
	reinit_completion(&priv->completion);

	priv->transfer_buffer[0] = 0x0c;
	priv->transfer_buffer[1] = 0x00;
	priv->transfer_buffer[2] = 0x02;
	priv->transfer_buffer[3] = 0x00;

	usb_fill_bulk_urb(priv->urb, udev,
			  usb_sndbulkpipe(udev, POWERZ_EP_CMD_OUT),
			  priv->transfer_buffer, 4, powerz_usb_cmd_complete,
			  priv);
	ret = usb_submit_urb(priv->urb, GFP_KERNEL);
	if (ret)
		return ret;

	if (!wait_for_completion_interruptible_timeout
	    (&priv->completion, msecs_to_jiffies(5))) {
		usb_kill_urb(priv->urb);
		return -EIO;
	}

	if (priv->urb->actual_length < sizeof(struct powerz_sensor_data))
		return -EIO;

	return priv->status;
}

static int powerz_read(struct device *dev, enum hwmon_sensor_types type,
		       u32 attr, int channel, long *val)
{
	struct usb_interface *intf = to_usb_interface(dev->parent);
	struct usb_device *udev = interface_to_usbdev(intf);
	struct powerz_priv *priv = usb_get_intfdata(intf);
	struct powerz_sensor_data *data;
	int ret;

	if (!priv)
		return -EIO;	/* disconnected */

	mutex_lock(&priv->mutex);
	ret = powerz_read_data(udev, priv);
	if (ret)
		goto out;

	data = (struct powerz_sensor_data *)priv->transfer_buffer;

	if (type == hwmon_curr) {
		if (attr == hwmon_curr_input)
			*val = ((s32)le32_to_cpu(data->I_bus)) / 1000;
		else if (attr == hwmon_curr_average)
			*val = ((s32)le32_to_cpu(data->I_bus_avg)) / 1000;
		else
			ret = -EOPNOTSUPP;
	} else if (type == hwmon_in) {
		if (attr == hwmon_in_input) {
			if (channel == 0)
				*val = le32_to_cpu(data->V_bus) / 1000;
			else if (channel == 1)
				*val = le16_to_cpu(data->V_cc1) / 10;
			else if (channel == 2)
				*val = le16_to_cpu(data->V_cc2) / 10;
			else if (channel == 3)
				*val = le16_to_cpu(data->V_dp) / 10;
			else if (channel == 4)
				*val = le16_to_cpu(data->V_dm) / 10;
			else if (channel == 5)
				*val = le16_to_cpu(data->V_dd) / 10;
			else
				ret = -EOPNOTSUPP;
		} else if (attr == hwmon_in_average && channel == 0) {
			*val = le32_to_cpu(data->V_bus_avg) / 1000;
		} else {
			ret = -EOPNOTSUPP;
		}
	} else if (type == hwmon_temp && attr == hwmon_temp_input) {
		*val = data->temp[1] * 2000 + data->temp[0] * 1000 / 128;
	} else {
		ret = -EOPNOTSUPP;
	}

out:
	mutex_unlock(&priv->mutex);
	return ret;
}

static const struct hwmon_ops powerz_hwmon_ops = {
	.is_visible = powerz_is_visible,
	.read = powerz_read,
	.read_string = powerz_read_string,
};

static const struct hwmon_chip_info powerz_chip_info = {
	.ops = &powerz_hwmon_ops,
	.info = powerz_info,
};

static int powerz_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
	struct powerz_priv *priv;
	struct device *hwmon_dev;
	struct device *parent;

	parent = &intf->dev;

	priv = devm_kzalloc(parent, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!priv->urb)
		return -ENOMEM;
	mutex_init(&priv->mutex);
	init_completion(&priv->completion);

	hwmon_dev =
	    devm_hwmon_device_register_with_info(parent, DRIVER_NAME, priv,
						 &powerz_chip_info, NULL);
	if (IS_ERR(hwmon_dev)) {
		usb_free_urb(priv->urb);
		return PTR_ERR(hwmon_dev);
	}

	usb_set_intfdata(intf, priv);

	return 0;
}

static void powerz_disconnect(struct usb_interface *intf)
{
	struct powerz_priv *priv = usb_get_intfdata(intf);

	mutex_lock(&priv->mutex);
	usb_kill_urb(priv->urb);
	usb_free_urb(priv->urb);
	mutex_unlock(&priv->mutex);
}

static const struct usb_device_id powerz_id_table[] = {
	{ USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0061, 0x00) },	/* ChargerLAB POWER-Z KM002C */
	{ USB_DEVICE_INTERFACE_NUMBER(0x5FC9, 0x0063, 0x00) },	/* ChargerLAB POWER-Z KM003C */
	{ }
};

MODULE_DEVICE_TABLE(usb, powerz_id_table);

static struct usb_driver powerz_driver = {
	.name = DRIVER_NAME,
	.id_table = powerz_id_table,
	.probe = powerz_probe,
	.disconnect = powerz_disconnect,
};

module_usb_driver(powerz_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Thomas Weißschuh <linux@weissschuh.net>");
MODULE_DESCRIPTION("ChargerLAB POWER-Z USB-C tester");