1 // SPDX-License-Identifier: GPL-2.0-only
3 * FTDI MPSSE GPIO support
5 * Based on code by Anatolij Gustschin
7 * Copyright (C) 2024 Mary Strodl <mstrodl@csh.rit.edu>
10 #include <linux/cleanup.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/mutex.h>
13 #include <linux/usb.h>
16 struct gpio_chip gpio
;
17 struct usb_device
*udev
; /* USB device encompassing all MPSSEs */
18 struct usb_interface
*intf
; /* USB interface for this MPSSE */
19 u8 intf_id
; /* USB interface number for this MPSSE */
20 struct work_struct irq_work
; /* polling work thread */
21 struct mutex irq_mutex
; /* lock over irq_data */
22 atomic_t irq_type
[16]; /* pin -> edge detection type */
26 u8 gpio_outputs
[2]; /* Output states for GPIOs [L, H] */
27 u8 gpio_dir
[2]; /* Directions for GPIOs [L, H] */
29 u8
*bulk_in_buf
; /* Extra recv buffer to grab status bytes */
31 struct usb_endpoint_descriptor
*bulk_in
;
32 struct usb_endpoint_descriptor
*bulk_out
;
34 struct mutex io_mutex
; /* sync I/O with disconnect */
38 bool tx
; /* direction of bulk transfer */
39 u8
*data
; /* input (tx) or output (rx) */
40 int len
; /* Length of `data` if tx, or length of */
41 /* Data to read if rx */
42 int len_actual
; /* Length successfully transferred */
46 static const struct usb_device_id gpio_mpsse_table
[] = {
47 { USB_DEVICE(0x0c52, 0xa064) }, /* SeaLevel Systems, Inc. */
48 { } /* Terminating entry */
51 MODULE_DEVICE_TABLE(usb
, gpio_mpsse_table
);
53 static DEFINE_IDA(gpio_mpsse_ida
);
56 #define SET_BITS_CMD 0x80
57 #define GET_BITS_CMD 0x81
59 #define SET_BITMODE_REQUEST 0x0B
60 #define MODE_MPSSE (2 << 8)
63 /* Arbitrarily decided. This could probably be much less */
64 #define MPSSE_WRITE_TIMEOUT 5000
65 #define MPSSE_READ_TIMEOUT 5000
67 /* 1 millisecond, also pretty arbitrary */
68 #define MPSSE_POLL_INTERVAL 1000
70 static int mpsse_bulk_xfer(struct usb_interface
*intf
, struct bulk_desc
*desc
)
72 struct mpsse_priv
*priv
= usb_get_intfdata(intf
);
73 struct usb_device
*udev
= priv
->udev
;
78 pipe
= usb_sndbulkpipe(udev
, priv
->bulk_out
->bEndpointAddress
);
80 pipe
= usb_rcvbulkpipe(udev
, priv
->bulk_in
->bEndpointAddress
);
82 ret
= usb_bulk_msg(udev
, pipe
, desc
->data
, desc
->len
,
83 &desc
->len_actual
, desc
->timeout
);
85 dev_dbg(&udev
->dev
, "mpsse: bulk transfer failed: %d\n", ret
);
90 static int mpsse_write(struct usb_interface
*intf
,
94 struct bulk_desc desc
;
100 desc
.timeout
= MPSSE_WRITE_TIMEOUT
;
102 ret
= mpsse_bulk_xfer(intf
, &desc
);
107 static int mpsse_read(struct usb_interface
*intf
, u8
*buf
, size_t len
)
110 struct bulk_desc desc
;
111 struct mpsse_priv
*priv
= usb_get_intfdata(intf
);
115 desc
.data
= priv
->bulk_in_buf
;
116 /* Device sends 2 additional status bytes, read len + 2 */
117 desc
.len
= min_t(size_t, len
+ 2, usb_endpoint_maxp(priv
->bulk_in
));
118 desc
.timeout
= MPSSE_READ_TIMEOUT
;
120 ret
= mpsse_bulk_xfer(intf
, &desc
);
124 /* Did we get enough data? */
125 if (desc
.len_actual
< desc
.len
)
128 memcpy(buf
, desc
.data
+ 2, desc
.len_actual
- 2);
133 static int gpio_mpsse_set_bank(struct mpsse_priv
*priv
, u8 bank
)
137 SET_BITS_CMD
| (bank
<< 1),
138 priv
->gpio_outputs
[bank
],
139 priv
->gpio_dir
[bank
],
142 ret
= mpsse_write(priv
->intf
, tx_buf
, 3);
147 static int gpio_mpsse_get_bank(struct mpsse_priv
*priv
, u8 bank
)
150 u8 buf
= GET_BITS_CMD
| (bank
<< 1);
152 ret
= mpsse_write(priv
->intf
, &buf
, 1);
156 ret
= mpsse_read(priv
->intf
, &buf
, 1);
163 static void gpio_mpsse_set_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
166 unsigned long i
, bank
, bank_mask
, bank_bits
;
168 struct mpsse_priv
*priv
= gpiochip_get_data(chip
);
170 guard(mutex
)(&priv
->io_mutex
);
171 for_each_set_clump8(i
, bank_mask
, mask
, chip
->ngpio
) {
175 bank_bits
= bitmap_get_value8(bits
, i
);
176 /* Zero out pins we want to change */
177 priv
->gpio_outputs
[bank
] &= ~bank_mask
;
178 /* Set pins we care about */
179 priv
->gpio_outputs
[bank
] |= bank_bits
& bank_mask
;
181 ret
= gpio_mpsse_set_bank(priv
, bank
);
183 dev_err(&priv
->intf
->dev
,
184 "Couldn't set values for bank %ld!",
190 static int gpio_mpsse_get_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
193 unsigned long i
, bank
, bank_mask
;
195 struct mpsse_priv
*priv
= gpiochip_get_data(chip
);
197 guard(mutex
)(&priv
->io_mutex
);
198 for_each_set_clump8(i
, bank_mask
, mask
, chip
->ngpio
) {
202 ret
= gpio_mpsse_get_bank(priv
, bank
);
206 bitmap_set_value8(bits
, ret
& bank_mask
, i
);
213 static int gpio_mpsse_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
216 unsigned long mask
= 0, bits
= 0;
218 __set_bit(offset
, &mask
);
219 err
= gpio_mpsse_get_multiple(chip
, &mask
, &bits
);
223 /* == is not guaranteed to give 1 if true */
230 static void gpio_mpsse_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
233 unsigned long mask
= 0, bits
= 0;
235 __set_bit(offset
, &mask
);
237 __set_bit(offset
, &bits
);
239 gpio_mpsse_set_multiple(chip
, &mask
, &bits
);
242 static int gpio_mpsse_direction_output(struct gpio_chip
*chip
,
243 unsigned int offset
, int value
)
245 struct mpsse_priv
*priv
= gpiochip_get_data(chip
);
246 int bank
= (offset
& 8) >> 3;
247 int bank_offset
= offset
& 7;
249 scoped_guard(mutex
, &priv
->io_mutex
)
250 priv
->gpio_dir
[bank
] |= BIT(bank_offset
);
252 gpio_mpsse_gpio_set(chip
, offset
, value
);
257 static int gpio_mpsse_direction_input(struct gpio_chip
*chip
,
260 struct mpsse_priv
*priv
= gpiochip_get_data(chip
);
261 int bank
= (offset
& 8) >> 3;
262 int bank_offset
= offset
& 7;
264 guard(mutex
)(&priv
->io_mutex
);
265 priv
->gpio_dir
[bank
] &= ~BIT(bank_offset
);
266 gpio_mpsse_set_bank(priv
, bank
);
271 static int gpio_mpsse_get_direction(struct gpio_chip
*chip
,
275 int bank
= (offset
& 8) >> 3;
276 int bank_offset
= offset
& 7;
277 struct mpsse_priv
*priv
= gpiochip_get_data(chip
);
279 guard(mutex
)(&priv
->io_mutex
);
280 /* MPSSE directions are inverted */
281 if (priv
->gpio_dir
[bank
] & BIT(bank_offset
))
282 ret
= GPIO_LINE_DIRECTION_OUT
;
284 ret
= GPIO_LINE_DIRECTION_IN
;
289 static void gpio_mpsse_poll(struct work_struct
*work
)
291 unsigned long pin_mask
, pin_states
, flags
;
292 int irq_enabled
, offset
, err
, value
, fire_irq
,
293 irq
, old_value
[16], irq_type
[16];
294 struct mpsse_priv
*priv
= container_of(work
, struct mpsse_priv
,
297 for (offset
= 0; offset
< priv
->gpio
.ngpio
; ++offset
)
298 old_value
[offset
] = -1;
300 while ((irq_enabled
= atomic_read(&priv
->irq_enabled
))) {
301 usleep_range(MPSSE_POLL_INTERVAL
, MPSSE_POLL_INTERVAL
+ 1000);
302 /* Cleanup will trigger at the end of the loop */
303 guard(mutex
)(&priv
->irq_mutex
);
307 for (offset
= 0; offset
< priv
->gpio
.ngpio
; ++offset
) {
308 irq_type
[offset
] = atomic_read(&priv
->irq_type
[offset
]);
309 if (irq_type
[offset
] != IRQ_TYPE_NONE
&&
310 irq_enabled
& BIT(offset
))
311 pin_mask
|= BIT(offset
);
313 old_value
[offset
] = -1;
316 err
= gpio_mpsse_get_multiple(&priv
->gpio
, &pin_mask
,
319 dev_err_ratelimited(&priv
->intf
->dev
,
324 /* Check each value */
325 for (offset
= 0; offset
< priv
->gpio
.ngpio
; ++offset
) {
326 if (old_value
[offset
] == -1)
330 value
= pin_states
& BIT(offset
);
332 switch (irq_type
[offset
]) {
333 case IRQ_TYPE_EDGE_RISING
:
334 fire_irq
= value
> old_value
[offset
];
336 case IRQ_TYPE_EDGE_FALLING
:
337 fire_irq
= value
< old_value
[offset
];
339 case IRQ_TYPE_EDGE_BOTH
:
340 fire_irq
= value
!= old_value
[offset
];
346 irq
= irq_find_mapping(priv
->gpio
.irq
.domain
,
348 local_irq_save(flags
);
349 generic_handle_irq(irq
);
351 local_irq_restore(flags
);
354 /* Sync back values so we can refer to them next tick */
355 for (offset
= 0; offset
< priv
->gpio
.ngpio
; ++offset
)
356 if (irq_type
[offset
] != IRQ_TYPE_NONE
&&
357 irq_enabled
& BIT(offset
))
358 old_value
[offset
] = pin_states
& BIT(offset
);
362 static int gpio_mpsse_set_irq_type(struct irq_data
*irqd
, unsigned int type
)
365 struct mpsse_priv
*priv
= irq_data_get_irq_chip_data(irqd
);
367 offset
= irqd
->hwirq
;
368 atomic_set(&priv
->irq_type
[offset
], type
& IRQ_TYPE_EDGE_BOTH
);
373 static void gpio_mpsse_irq_disable(struct irq_data
*irqd
)
375 struct mpsse_priv
*priv
= irq_data_get_irq_chip_data(irqd
);
377 atomic_and(~BIT(irqd
->hwirq
), &priv
->irq_enabled
);
378 gpiochip_disable_irq(&priv
->gpio
, irqd
->hwirq
);
381 static void gpio_mpsse_irq_enable(struct irq_data
*irqd
)
383 struct mpsse_priv
*priv
= irq_data_get_irq_chip_data(irqd
);
385 gpiochip_enable_irq(&priv
->gpio
, irqd
->hwirq
);
386 /* If no-one else was using the IRQ, enable it */
387 if (!atomic_fetch_or(BIT(irqd
->hwirq
), &priv
->irq_enabled
)) {
388 INIT_WORK(&priv
->irq_work
, gpio_mpsse_poll
);
389 schedule_work(&priv
->irq_work
);
393 static const struct irq_chip gpio_mpsse_irq_chip
= {
394 .name
= "gpio-mpsse-irq",
395 .irq_enable
= gpio_mpsse_irq_enable
,
396 .irq_disable
= gpio_mpsse_irq_disable
,
397 .irq_set_type
= gpio_mpsse_set_irq_type
,
398 .flags
= IRQCHIP_IMMUTABLE
,
399 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
402 static void gpio_mpsse_ida_remove(void *data
)
404 struct mpsse_priv
*priv
= data
;
406 ida_free(&gpio_mpsse_ida
, priv
->id
);
409 static int gpio_mpsse_probe(struct usb_interface
*interface
,
410 const struct usb_device_id
*id
)
412 struct mpsse_priv
*priv
;
416 dev
= &interface
->dev
;
417 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
421 priv
->udev
= usb_get_dev(interface_to_usbdev(interface
));
422 priv
->intf
= interface
;
423 priv
->intf_id
= interface
->cur_altsetting
->desc
.bInterfaceNumber
;
425 priv
->id
= ida_alloc(&gpio_mpsse_ida
, GFP_KERNEL
);
429 err
= devm_add_action_or_reset(dev
, gpio_mpsse_ida_remove
, priv
);
433 err
= devm_mutex_init(dev
, &priv
->io_mutex
);
437 err
= devm_mutex_init(dev
, &priv
->irq_mutex
);
441 priv
->gpio
.label
= devm_kasprintf(dev
, GFP_KERNEL
,
443 priv
->id
, priv
->intf_id
);
444 if (!priv
->gpio
.label
)
447 priv
->gpio
.owner
= THIS_MODULE
;
448 priv
->gpio
.parent
= interface
->usb_dev
;
449 priv
->gpio
.get_direction
= gpio_mpsse_get_direction
;
450 priv
->gpio
.direction_input
= gpio_mpsse_direction_input
;
451 priv
->gpio
.direction_output
= gpio_mpsse_direction_output
;
452 priv
->gpio
.get
= gpio_mpsse_gpio_get
;
453 priv
->gpio
.set
= gpio_mpsse_gpio_set
;
454 priv
->gpio
.get_multiple
= gpio_mpsse_get_multiple
;
455 priv
->gpio
.set_multiple
= gpio_mpsse_set_multiple
;
456 priv
->gpio
.base
= -1;
457 priv
->gpio
.ngpio
= 16;
458 priv
->gpio
.offset
= priv
->intf_id
* priv
->gpio
.ngpio
;
459 priv
->gpio
.can_sleep
= 1;
461 err
= usb_find_common_endpoints(interface
->cur_altsetting
,
462 &priv
->bulk_in
, &priv
->bulk_out
,
467 priv
->bulk_in_buf
= devm_kmalloc(dev
, usb_endpoint_maxp(priv
->bulk_in
),
469 if (!priv
->bulk_in_buf
)
472 usb_set_intfdata(interface
, priv
);
474 /* Reset mode, needed to correctly enter MPSSE mode */
475 err
= usb_control_msg(priv
->udev
, usb_sndctrlpipe(priv
->udev
, 0),
477 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
478 MODE_RESET
, priv
->intf_id
+ 1, NULL
, 0,
479 USB_CTRL_SET_TIMEOUT
);
483 /* Enter MPSSE mode */
484 err
= usb_control_msg(priv
->udev
, usb_sndctrlpipe(priv
->udev
, 0),
486 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
487 MODE_MPSSE
, priv
->intf_id
+ 1, NULL
, 0,
488 USB_CTRL_SET_TIMEOUT
);
492 gpio_irq_chip_set_chip(&priv
->gpio
.irq
, &gpio_mpsse_irq_chip
);
494 priv
->gpio
.irq
.parent_handler
= NULL
;
495 priv
->gpio
.irq
.num_parents
= 0;
496 priv
->gpio
.irq
.parents
= NULL
;
497 priv
->gpio
.irq
.default_type
= IRQ_TYPE_NONE
;
498 priv
->gpio
.irq
.handler
= handle_simple_irq
;
500 err
= devm_gpiochip_add_data(dev
, &priv
->gpio
, priv
);
507 static void gpio_mpsse_disconnect(struct usb_interface
*intf
)
509 struct mpsse_priv
*priv
= usb_get_intfdata(intf
);
512 usb_set_intfdata(intf
, NULL
);
513 usb_put_dev(priv
->udev
);
516 static struct usb_driver gpio_mpsse_driver
= {
517 .name
= "gpio-mpsse",
518 .probe
= gpio_mpsse_probe
,
519 .disconnect
= gpio_mpsse_disconnect
,
520 .id_table
= gpio_mpsse_table
,
523 module_usb_driver(gpio_mpsse_driver
);
525 MODULE_AUTHOR("Mary Strodl <mstrodl@csh.rit.edu>");
526 MODULE_DESCRIPTION("MPSSE GPIO driver");
527 MODULE_LICENSE("GPL");