Merge tag '6.10-rc6-smb3-server-fixes' of git://git.samba.org/ksmbd
[linux.git] / drivers / hid / hid-mcp2200.c
blobbf57f7f6caa084ef2004ce66753fa192b661d9c3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * MCP2200 - Microchip USB to GPIO bridge
5 * Copyright (c) 2023, Johannes Roith <johannes@gnu-linux.rocks>
7 * Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/22228A.pdf
8 * App Note for HID: https://ww1.microchip.com/downloads/en/DeviceDoc/93066A.pdf
9 */
10 #include <linux/completion.h>
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/hid.h>
15 #include <linux/hidraw.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include "hid-ids.h"
20 /* Commands codes in a raw output report */
21 #define SET_CLEAR_OUTPUTS 0x08
22 #define CONFIGURE 0x10
23 #define READ_EE 0x20
24 #define WRITE_EE 0x40
25 #define READ_ALL 0x80
27 /* MCP GPIO direction encoding */
28 enum MCP_IO_DIR {
29 MCP2200_DIR_OUT = 0x00,
30 MCP2200_DIR_IN = 0x01,
33 /* Altternative pin assignments */
34 #define TXLED 2
35 #define RXLED 3
36 #define USBCFG 6
37 #define SSPND 7
38 #define MCP_NGPIO 8
40 /* CMD to set or clear a GPIO output */
41 struct mcp_set_clear_outputs {
42 u8 cmd;
43 u8 dummys1[10];
44 u8 set_bmap;
45 u8 clear_bmap;
46 u8 dummys2[3];
47 } __packed;
49 /* CMD to configure the IOs */
50 struct mcp_configure {
51 u8 cmd;
52 u8 dummys1[3];
53 u8 io_bmap;
54 u8 config_alt_pins;
55 u8 io_default_val_bmap;
56 u8 config_alt_options;
57 u8 baud_h;
58 u8 baud_l;
59 u8 dummys2[6];
60 } __packed;
62 /* CMD to read all parameters */
63 struct mcp_read_all {
64 u8 cmd;
65 u8 dummys[15];
66 } __packed;
68 /* Response to the read all cmd */
69 struct mcp_read_all_resp {
70 u8 cmd;
71 u8 eep_addr;
72 u8 dummy;
73 u8 eep_val;
74 u8 io_bmap;
75 u8 config_alt_pins;
76 u8 io_default_val_bmap;
77 u8 config_alt_options;
78 u8 baud_h;
79 u8 baud_l;
80 u8 io_port_val_bmap;
81 u8 dummys[5];
82 } __packed;
84 struct mcp2200 {
85 struct hid_device *hdev;
86 struct mutex lock;
87 struct completion wait_in_report;
88 u8 gpio_dir;
89 u8 gpio_val;
90 u8 gpio_inval;
91 u8 baud_h;
92 u8 baud_l;
93 u8 config_alt_pins;
94 u8 gpio_reset_val;
95 u8 config_alt_options;
96 int status;
97 struct gpio_chip gc;
98 u8 hid_report[16];
101 /* this executes the READ_ALL cmd */
102 static int mcp_cmd_read_all(struct mcp2200 *mcp)
104 struct mcp_read_all *read_all;
105 int len, t;
107 reinit_completion(&mcp->wait_in_report);
109 mutex_lock(&mcp->lock);
111 read_all = (struct mcp_read_all *) mcp->hid_report;
112 read_all->cmd = READ_ALL;
113 len = hid_hw_output_report(mcp->hdev, (u8 *) read_all,
114 sizeof(struct mcp_read_all));
116 mutex_unlock(&mcp->lock);
118 if (len != sizeof(struct mcp_read_all))
119 return -EINVAL;
121 t = wait_for_completion_timeout(&mcp->wait_in_report,
122 msecs_to_jiffies(4000));
123 if (!t)
124 return -ETIMEDOUT;
126 /* return status, negative value if wrong response was received */
127 return mcp->status;
130 static void mcp_set_multiple(struct gpio_chip *gc, unsigned long *mask,
131 unsigned long *bits)
133 struct mcp2200 *mcp = gpiochip_get_data(gc);
134 u8 value;
135 int status;
136 struct mcp_set_clear_outputs *cmd;
138 mutex_lock(&mcp->lock);
139 cmd = (struct mcp_set_clear_outputs *) mcp->hid_report;
141 value = mcp->gpio_val & ~*mask;
142 value |= (*mask & *bits);
144 cmd->cmd = SET_CLEAR_OUTPUTS;
145 cmd->set_bmap = value;
146 cmd->clear_bmap = ~(value);
148 status = hid_hw_output_report(mcp->hdev, (u8 *) cmd,
149 sizeof(struct mcp_set_clear_outputs));
151 if (status == sizeof(struct mcp_set_clear_outputs))
152 mcp->gpio_val = value;
154 mutex_unlock(&mcp->lock);
157 static void mcp_set(struct gpio_chip *gc, unsigned int gpio_nr, int value)
159 unsigned long mask = 1 << gpio_nr;
160 unsigned long bmap_value = value << gpio_nr;
162 mcp_set_multiple(gc, &mask, &bmap_value);
165 static int mcp_get_multiple(struct gpio_chip *gc, unsigned long *mask,
166 unsigned long *bits)
168 u32 val;
169 struct mcp2200 *mcp = gpiochip_get_data(gc);
170 int status;
172 status = mcp_cmd_read_all(mcp);
173 if (status)
174 return status;
176 val = mcp->gpio_inval;
177 *bits = (val & *mask);
178 return 0;
181 static int mcp_get(struct gpio_chip *gc, unsigned int gpio_nr)
183 unsigned long mask = 0, bits = 0;
185 mask = (1 << gpio_nr);
186 mcp_get_multiple(gc, &mask, &bits);
187 return bits > 0;
190 static int mcp_get_direction(struct gpio_chip *gc, unsigned int gpio_nr)
192 struct mcp2200 *mcp = gpiochip_get_data(gc);
194 return (mcp->gpio_dir & (MCP2200_DIR_IN << gpio_nr))
195 ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
198 static int mcp_set_direction(struct gpio_chip *gc, unsigned int gpio_nr,
199 enum MCP_IO_DIR io_direction)
201 struct mcp2200 *mcp = gpiochip_get_data(gc);
202 struct mcp_configure *conf;
203 int status;
204 /* after the configure cmd we will need to set the outputs again */
205 unsigned long mask = ~(mcp->gpio_dir); /* only set outputs */
206 unsigned long bits = mcp->gpio_val;
207 /* Offsets of alternative pins in config_alt_pins, 0 is not used */
208 u8 alt_pin_conf[8] = {SSPND, USBCFG, 0, 0, 0, 0, RXLED, TXLED};
209 u8 config_alt_pins = mcp->config_alt_pins;
211 /* Read in the reset baudrate first, we need it later */
212 status = mcp_cmd_read_all(mcp);
213 if (status != 0)
214 return status;
216 mutex_lock(&mcp->lock);
217 conf = (struct mcp_configure *) mcp->hid_report;
219 /* configure will reset the chip! */
220 conf->cmd = CONFIGURE;
221 conf->io_bmap = (mcp->gpio_dir & ~(1 << gpio_nr))
222 | (io_direction << gpio_nr);
223 /* Don't overwrite the reset parameters */
224 conf->baud_h = mcp->baud_h;
225 conf->baud_l = mcp->baud_l;
226 conf->config_alt_options = mcp->config_alt_options;
227 conf->io_default_val_bmap = mcp->gpio_reset_val;
228 /* Adjust alt. func if necessary */
229 if (alt_pin_conf[gpio_nr])
230 config_alt_pins &= ~(1 << alt_pin_conf[gpio_nr]);
231 conf->config_alt_pins = config_alt_pins;
233 status = hid_hw_output_report(mcp->hdev, (u8 *) conf,
234 sizeof(struct mcp_set_clear_outputs));
236 if (status == sizeof(struct mcp_set_clear_outputs)) {
237 mcp->gpio_dir = conf->io_bmap;
238 mcp->config_alt_pins = config_alt_pins;
239 } else {
240 mutex_unlock(&mcp->lock);
241 return -EIO;
244 mutex_unlock(&mcp->lock);
246 /* Configure CMD will clear all IOs -> rewrite them */
247 mcp_set_multiple(gc, &mask, &bits);
248 return 0;
251 static int mcp_direction_input(struct gpio_chip *gc, unsigned int gpio_nr)
253 return mcp_set_direction(gc, gpio_nr, MCP2200_DIR_IN);
256 static int mcp_direction_output(struct gpio_chip *gc, unsigned int gpio_nr,
257 int value)
259 int ret;
260 unsigned long mask, bmap_value;
262 mask = 1 << gpio_nr;
263 bmap_value = value << gpio_nr;
265 ret = mcp_set_direction(gc, gpio_nr, MCP2200_DIR_OUT);
266 if (!ret)
267 mcp_set_multiple(gc, &mask, &bmap_value);
268 return ret;
271 static const struct gpio_chip template_chip = {
272 .label = "mcp2200",
273 .owner = THIS_MODULE,
274 .get_direction = mcp_get_direction,
275 .direction_input = mcp_direction_input,
276 .direction_output = mcp_direction_output,
277 .set = mcp_set,
278 .set_multiple = mcp_set_multiple,
279 .get = mcp_get,
280 .get_multiple = mcp_get_multiple,
281 .base = -1,
282 .ngpio = MCP_NGPIO,
283 .can_sleep = true,
287 * MCP2200 uses interrupt endpoint for input reports. This function
288 * is called by HID layer when it receives i/p report from mcp2200,
289 * which is actually a response to the previously sent command.
291 static int mcp2200_raw_event(struct hid_device *hdev, struct hid_report *report,
292 u8 *data, int size)
294 struct mcp2200 *mcp = hid_get_drvdata(hdev);
295 struct mcp_read_all_resp *all_resp;
297 switch (data[0]) {
298 case READ_ALL:
299 all_resp = (struct mcp_read_all_resp *) data;
300 mcp->status = 0;
301 mcp->gpio_inval = all_resp->io_port_val_bmap;
302 mcp->baud_h = all_resp->baud_h;
303 mcp->baud_l = all_resp->baud_l;
304 mcp->gpio_reset_val = all_resp->io_default_val_bmap;
305 mcp->config_alt_pins = all_resp->config_alt_pins;
306 mcp->config_alt_options = all_resp->config_alt_options;
307 break;
308 default:
309 mcp->status = -EIO;
310 break;
313 complete(&mcp->wait_in_report);
314 return 0;
317 static int mcp2200_probe(struct hid_device *hdev, const struct hid_device_id *id)
319 int ret;
320 struct mcp2200 *mcp;
322 mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
323 if (!mcp)
324 return -ENOMEM;
326 ret = hid_parse(hdev);
327 if (ret) {
328 hid_err(hdev, "can't parse reports\n");
329 return ret;
332 ret = hid_hw_start(hdev, 0);
333 if (ret) {
334 hid_err(hdev, "can't start hardware\n");
335 return ret;
338 hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8,
339 hdev->version & 0xff, hdev->name, hdev->phys);
341 ret = hid_hw_open(hdev);
342 if (ret) {
343 hid_err(hdev, "can't open device\n");
344 hid_hw_stop(hdev);
345 return ret;
348 mutex_init(&mcp->lock);
349 init_completion(&mcp->wait_in_report);
350 hid_set_drvdata(hdev, mcp);
351 mcp->hdev = hdev;
353 mcp->gc = template_chip;
354 mcp->gc.parent = &hdev->dev;
356 ret = devm_gpiochip_add_data(&hdev->dev, &mcp->gc, mcp);
357 if (ret < 0) {
358 hid_err(hdev, "Unable to register gpiochip\n");
359 hid_hw_close(hdev);
360 hid_hw_stop(hdev);
361 return ret;
364 return 0;
367 static void mcp2200_remove(struct hid_device *hdev)
369 hid_hw_close(hdev);
370 hid_hw_stop(hdev);
373 static const struct hid_device_id mcp2200_devices[] = {
374 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2200) },
377 MODULE_DEVICE_TABLE(hid, mcp2200_devices);
379 static struct hid_driver mcp2200_driver = {
380 .name = "mcp2200",
381 .id_table = mcp2200_devices,
382 .probe = mcp2200_probe,
383 .remove = mcp2200_remove,
384 .raw_event = mcp2200_raw_event,
387 /* Register with HID core */
388 module_hid_driver(mcp2200_driver);
390 MODULE_AUTHOR("Johannes Roith <johannes@gnu-linux.rocks>");
391 MODULE_DESCRIPTION("MCP2200 Microchip HID USB to GPIO bridge");
392 MODULE_LICENSE("GPL");