spi: spi-fsl-qspi: Fix return value check of devm_ioremap() in probe
[linux/fpc-iii.git] / drivers / usb / gadget / udc / max3420_udc.c
blob8fbc083b6732900dd268762320e38f7ef143e235
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * MAX3420 Device Controller driver for USB.
5 * Author: Jaswinder Singh Brar <jaswinder.singh@linaro.org>
6 * (C) Copyright 2019-2020 Linaro Ltd
8 * Based on:
9 * o MAX3420E datasheet
10 * http://datasheets.maximintegrated.com/en/ds/MAX3420E.pdf
11 * o MAX342{0,1}E Programming Guides
12 * https://pdfserv.maximintegrated.com/en/an/AN3598.pdf
13 * https://pdfserv.maximintegrated.com/en/an/AN3785.pdf
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/module.h>
21 #include <linux/bitfield.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include <linux/of_platform.h>
25 #include <linux/of_irq.h>
26 #include <linux/prefetch.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
29 #include <linux/spi/spi.h>
30 #include <linux/gpio/consumer.h>
32 #define MAX3420_MAX_EPS 4
33 #define MAX3420_EP_MAX_PACKET 64 /* Same for all Endpoints */
34 #define MAX3420_EPNAME_SIZE 16 /* Buffer size for endpoint name */
36 #define MAX3420_ACKSTAT BIT(0)
38 #define MAX3420_SPI_DIR_RD 0 /* read register from MAX3420 */
39 #define MAX3420_SPI_DIR_WR 1 /* write register to MAX3420 */
41 /* SPI commands: */
42 #define MAX3420_SPI_DIR_SHIFT 1
43 #define MAX3420_SPI_REG_SHIFT 3
45 #define MAX3420_REG_EP0FIFO 0
46 #define MAX3420_REG_EP1FIFO 1
47 #define MAX3420_REG_EP2FIFO 2
48 #define MAX3420_REG_EP3FIFO 3
49 #define MAX3420_REG_SUDFIFO 4
50 #define MAX3420_REG_EP0BC 5
51 #define MAX3420_REG_EP1BC 6
52 #define MAX3420_REG_EP2BC 7
53 #define MAX3420_REG_EP3BC 8
55 #define MAX3420_REG_EPSTALLS 9
56 #define ACKSTAT BIT(6)
57 #define STLSTAT BIT(5)
58 #define STLEP3IN BIT(4)
59 #define STLEP2IN BIT(3)
60 #define STLEP1OUT BIT(2)
61 #define STLEP0OUT BIT(1)
62 #define STLEP0IN BIT(0)
64 #define MAX3420_REG_CLRTOGS 10
65 #define EP3DISAB BIT(7)
66 #define EP2DISAB BIT(6)
67 #define EP1DISAB BIT(5)
68 #define CTGEP3IN BIT(4)
69 #define CTGEP2IN BIT(3)
70 #define CTGEP1OUT BIT(2)
72 #define MAX3420_REG_EPIRQ 11
73 #define MAX3420_REG_EPIEN 12
74 #define SUDAVIRQ BIT(5)
75 #define IN3BAVIRQ BIT(4)
76 #define IN2BAVIRQ BIT(3)
77 #define OUT1DAVIRQ BIT(2)
78 #define OUT0DAVIRQ BIT(1)
79 #define IN0BAVIRQ BIT(0)
81 #define MAX3420_REG_USBIRQ 13
82 #define MAX3420_REG_USBIEN 14
83 #define OSCOKIRQ BIT(0)
84 #define RWUDNIRQ BIT(1)
85 #define BUSACTIRQ BIT(2)
86 #define URESIRQ BIT(3)
87 #define SUSPIRQ BIT(4)
88 #define NOVBUSIRQ BIT(5)
89 #define VBUSIRQ BIT(6)
90 #define URESDNIRQ BIT(7)
92 #define MAX3420_REG_USBCTL 15
93 #define HOSCSTEN BIT(7)
94 #define VBGATE BIT(6)
95 #define CHIPRES BIT(5)
96 #define PWRDOWN BIT(4)
97 #define CONNECT BIT(3)
98 #define SIGRWU BIT(2)
100 #define MAX3420_REG_CPUCTL 16
101 #define IE BIT(0)
103 #define MAX3420_REG_PINCTL 17
104 #define EP3INAK BIT(7)
105 #define EP2INAK BIT(6)
106 #define EP0INAK BIT(5)
107 #define FDUPSPI BIT(4)
108 #define INTLEVEL BIT(3)
109 #define POSINT BIT(2)
110 #define GPXB BIT(1)
111 #define GPXA BIT(0)
113 #define MAX3420_REG_REVISION 18
115 #define MAX3420_REG_FNADDR 19
116 #define FNADDR_MASK 0x7f
118 #define MAX3420_REG_IOPINS 20
119 #define MAX3420_REG_IOPINS2 21
120 #define MAX3420_REG_GPINIRQ 22
121 #define MAX3420_REG_GPINIEN 23
122 #define MAX3420_REG_GPINPOL 24
123 #define MAX3420_REG_HIRQ 25
124 #define MAX3420_REG_HIEN 26
125 #define MAX3420_REG_MODE 27
126 #define MAX3420_REG_PERADDR 28
127 #define MAX3420_REG_HCTL 29
128 #define MAX3420_REG_HXFR 30
129 #define MAX3420_REG_HRSL 31
131 #define ENABLE_IRQ BIT(0)
132 #define IOPIN_UPDATE BIT(1)
133 #define REMOTE_WAKEUP BIT(2)
134 #define CONNECT_HOST GENMASK(4, 3)
135 #define HCONNECT (1 << 3)
136 #define HDISCONNECT (3 << 3)
137 #define UDC_START GENMASK(6, 5)
138 #define START (1 << 5)
139 #define STOP (3 << 5)
140 #define ENABLE_EP GENMASK(8, 7)
141 #define ENABLE (1 << 7)
142 #define DISABLE (3 << 7)
143 #define STALL_EP GENMASK(10, 9)
144 #define STALL (1 << 9)
145 #define UNSTALL (3 << 9)
147 #define MAX3420_CMD(c) FIELD_PREP(GENMASK(7, 3), c)
148 #define MAX3420_SPI_CMD_RD(c) (MAX3420_CMD(c) | (0 << 1))
149 #define MAX3420_SPI_CMD_WR(c) (MAX3420_CMD(c) | (1 << 1))
151 struct max3420_req {
152 struct usb_request usb_req;
153 struct list_head queue;
154 struct max3420_ep *ep;
157 struct max3420_ep {
158 struct usb_ep ep_usb;
159 struct max3420_udc *udc;
160 struct list_head queue;
161 char name[MAX3420_EPNAME_SIZE];
162 unsigned int maxpacket;
163 spinlock_t lock;
164 int halted;
165 u32 todo;
166 int id;
169 struct max3420_udc {
170 struct usb_gadget gadget;
171 struct max3420_ep ep[MAX3420_MAX_EPS];
172 struct usb_gadget_driver *driver;
173 struct task_struct *thread_task;
174 int remote_wkp, is_selfpowered;
175 bool vbus_active, softconnect;
176 struct usb_ctrlrequest setup;
177 struct mutex spi_bus_mutex;
178 struct max3420_req ep0req;
179 struct spi_device *spi;
180 struct device *dev;
181 spinlock_t lock;
182 bool suspended;
183 u8 ep0buf[64];
184 u32 todo;
187 #define to_max3420_req(r) container_of((r), struct max3420_req, usb_req)
188 #define to_max3420_ep(e) container_of((e), struct max3420_ep, ep_usb)
189 #define to_udc(g) container_of((g), struct max3420_udc, gadget)
191 #define DRIVER_DESC "MAX3420 USB Device-Mode Driver"
192 static const char driver_name[] = "max3420-udc";
194 /* Control endpoint configuration.*/
195 static const struct usb_endpoint_descriptor ep0_desc = {
196 .bEndpointAddress = USB_DIR_OUT,
197 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
198 .wMaxPacketSize = cpu_to_le16(MAX3420_EP_MAX_PACKET),
201 static void spi_ack_ctrl(struct max3420_udc *udc)
203 struct spi_device *spi = udc->spi;
204 struct spi_transfer transfer;
205 struct spi_message msg;
206 u8 txdata[1];
208 memset(&transfer, 0, sizeof(transfer));
210 spi_message_init(&msg);
212 txdata[0] = MAX3420_ACKSTAT;
213 transfer.tx_buf = txdata;
214 transfer.len = 1;
216 spi_message_add_tail(&transfer, &msg);
217 spi_sync(spi, &msg);
220 static u8 spi_rd8_ack(struct max3420_udc *udc, u8 reg, int actstat)
222 struct spi_device *spi = udc->spi;
223 struct spi_transfer transfer;
224 struct spi_message msg;
225 u8 txdata[2], rxdata[2];
227 memset(&transfer, 0, sizeof(transfer));
229 spi_message_init(&msg);
231 txdata[0] = MAX3420_SPI_CMD_RD(reg) | (actstat ? MAX3420_ACKSTAT : 0);
232 transfer.tx_buf = txdata;
233 transfer.rx_buf = rxdata;
234 transfer.len = 2;
236 spi_message_add_tail(&transfer, &msg);
237 spi_sync(spi, &msg);
239 return rxdata[1];
242 static u8 spi_rd8(struct max3420_udc *udc, u8 reg)
244 return spi_rd8_ack(udc, reg, 0);
247 static void spi_wr8_ack(struct max3420_udc *udc, u8 reg, u8 val, int actstat)
249 struct spi_device *spi = udc->spi;
250 struct spi_transfer transfer;
251 struct spi_message msg;
252 u8 txdata[2];
254 memset(&transfer, 0, sizeof(transfer));
256 spi_message_init(&msg);
258 txdata[0] = MAX3420_SPI_CMD_WR(reg) | (actstat ? MAX3420_ACKSTAT : 0);
259 txdata[1] = val;
261 transfer.tx_buf = txdata;
262 transfer.len = 2;
264 spi_message_add_tail(&transfer, &msg);
265 spi_sync(spi, &msg);
268 static void spi_wr8(struct max3420_udc *udc, u8 reg, u8 val)
270 spi_wr8_ack(udc, reg, val, 0);
273 static void spi_rd_buf(struct max3420_udc *udc, u8 reg, void *buf, u8 len)
275 struct spi_device *spi = udc->spi;
276 struct spi_transfer transfer;
277 struct spi_message msg;
278 u8 local_buf[MAX3420_EP_MAX_PACKET + 1] = {};
280 memset(&transfer, 0, sizeof(transfer));
282 spi_message_init(&msg);
284 local_buf[0] = MAX3420_SPI_CMD_RD(reg);
285 transfer.tx_buf = &local_buf[0];
286 transfer.rx_buf = &local_buf[0];
287 transfer.len = len + 1;
289 spi_message_add_tail(&transfer, &msg);
290 spi_sync(spi, &msg);
292 memcpy(buf, &local_buf[1], len);
295 static void spi_wr_buf(struct max3420_udc *udc, u8 reg, void *buf, u8 len)
297 struct spi_device *spi = udc->spi;
298 struct spi_transfer transfer;
299 struct spi_message msg;
300 u8 local_buf[MAX3420_EP_MAX_PACKET + 1] = {};
302 memset(&transfer, 0, sizeof(transfer));
304 spi_message_init(&msg);
306 local_buf[0] = MAX3420_SPI_CMD_WR(reg);
307 memcpy(&local_buf[1], buf, len);
309 transfer.tx_buf = local_buf;
310 transfer.len = len + 1;
312 spi_message_add_tail(&transfer, &msg);
313 spi_sync(spi, &msg);
316 static int spi_max3420_enable(struct max3420_ep *ep)
318 struct max3420_udc *udc = ep->udc;
319 unsigned long flags;
320 u8 epdis, epien;
321 int todo;
323 spin_lock_irqsave(&ep->lock, flags);
324 todo = ep->todo & ENABLE_EP;
325 ep->todo &= ~ENABLE_EP;
326 spin_unlock_irqrestore(&ep->lock, flags);
328 if (!todo || ep->id == 0)
329 return false;
331 epien = spi_rd8(udc, MAX3420_REG_EPIEN);
332 epdis = spi_rd8(udc, MAX3420_REG_CLRTOGS);
334 if (todo == ENABLE) {
335 epdis &= ~BIT(ep->id + 4);
336 epien |= BIT(ep->id + 1);
337 } else {
338 epdis |= BIT(ep->id + 4);
339 epien &= ~BIT(ep->id + 1);
342 spi_wr8(udc, MAX3420_REG_CLRTOGS, epdis);
343 spi_wr8(udc, MAX3420_REG_EPIEN, epien);
345 return true;
348 static int spi_max3420_stall(struct max3420_ep *ep)
350 struct max3420_udc *udc = ep->udc;
351 unsigned long flags;
352 u8 epstalls;
353 int todo;
355 spin_lock_irqsave(&ep->lock, flags);
356 todo = ep->todo & STALL_EP;
357 ep->todo &= ~STALL_EP;
358 spin_unlock_irqrestore(&ep->lock, flags);
360 if (!todo || ep->id == 0)
361 return false;
363 epstalls = spi_rd8(udc, MAX3420_REG_EPSTALLS);
364 if (todo == STALL) {
365 ep->halted = 1;
366 epstalls |= BIT(ep->id + 1);
367 } else {
368 u8 clrtogs;
370 ep->halted = 0;
371 epstalls &= ~BIT(ep->id + 1);
372 clrtogs = spi_rd8(udc, MAX3420_REG_CLRTOGS);
373 clrtogs |= BIT(ep->id + 1);
374 spi_wr8(udc, MAX3420_REG_CLRTOGS, clrtogs);
376 spi_wr8(udc, MAX3420_REG_EPSTALLS, epstalls | ACKSTAT);
378 return true;
381 static int spi_max3420_rwkup(struct max3420_udc *udc)
383 unsigned long flags;
384 int wake_remote;
385 u8 usbctl;
387 spin_lock_irqsave(&udc->lock, flags);
388 wake_remote = udc->todo & REMOTE_WAKEUP;
389 udc->todo &= ~REMOTE_WAKEUP;
390 spin_unlock_irqrestore(&udc->lock, flags);
392 if (!wake_remote || !udc->suspended)
393 return false;
395 /* Set Remote-WkUp Signal*/
396 usbctl = spi_rd8(udc, MAX3420_REG_USBCTL);
397 usbctl |= SIGRWU;
398 spi_wr8(udc, MAX3420_REG_USBCTL, usbctl);
400 msleep_interruptible(5);
402 /* Clear Remote-WkUp Signal*/
403 usbctl = spi_rd8(udc, MAX3420_REG_USBCTL);
404 usbctl &= ~SIGRWU;
405 spi_wr8(udc, MAX3420_REG_USBCTL, usbctl);
407 udc->suspended = false;
409 return true;
412 static void max3420_nuke(struct max3420_ep *ep, int status);
413 static void __max3420_stop(struct max3420_udc *udc)
415 u8 val;
416 int i;
418 /* clear all pending requests */
419 for (i = 1; i < MAX3420_MAX_EPS; i++)
420 max3420_nuke(&udc->ep[i], -ECONNRESET);
422 /* Disable IRQ to CPU */
423 spi_wr8(udc, MAX3420_REG_CPUCTL, 0);
425 val = spi_rd8(udc, MAX3420_REG_USBCTL);
426 val |= PWRDOWN;
427 if (udc->is_selfpowered)
428 val &= ~HOSCSTEN;
429 else
430 val |= HOSCSTEN;
431 spi_wr8(udc, MAX3420_REG_USBCTL, val);
434 static void __max3420_start(struct max3420_udc *udc)
436 u8 val;
438 /* Need this delay if bus-powered,
439 * but even for self-powered it helps stability
441 msleep_interruptible(250);
443 /* configure SPI */
444 spi_wr8(udc, MAX3420_REG_PINCTL, FDUPSPI);
446 /* Chip Reset */
447 spi_wr8(udc, MAX3420_REG_USBCTL, CHIPRES);
448 msleep_interruptible(5);
449 spi_wr8(udc, MAX3420_REG_USBCTL, 0);
451 /* Poll for OSC to stabilize */
452 while (1) {
453 val = spi_rd8(udc, MAX3420_REG_USBIRQ);
454 if (val & OSCOKIRQ)
455 break;
456 cond_resched();
459 /* Enable PULL-UP only when Vbus detected */
460 val = spi_rd8(udc, MAX3420_REG_USBCTL);
461 val |= VBGATE | CONNECT;
462 spi_wr8(udc, MAX3420_REG_USBCTL, val);
464 val = URESDNIRQ | URESIRQ;
465 if (udc->is_selfpowered)
466 val |= NOVBUSIRQ;
467 spi_wr8(udc, MAX3420_REG_USBIEN, val);
469 /* Enable only EP0 interrupts */
470 val = IN0BAVIRQ | OUT0DAVIRQ | SUDAVIRQ;
471 spi_wr8(udc, MAX3420_REG_EPIEN, val);
473 /* Enable IRQ to CPU */
474 spi_wr8(udc, MAX3420_REG_CPUCTL, IE);
477 static int max3420_start(struct max3420_udc *udc)
479 unsigned long flags;
480 int todo;
482 spin_lock_irqsave(&udc->lock, flags);
483 todo = udc->todo & UDC_START;
484 udc->todo &= ~UDC_START;
485 spin_unlock_irqrestore(&udc->lock, flags);
487 if (!todo)
488 return false;
490 if (udc->vbus_active && udc->softconnect)
491 __max3420_start(udc);
492 else
493 __max3420_stop(udc);
495 return true;
498 static irqreturn_t max3420_vbus_handler(int irq, void *dev_id)
500 struct max3420_udc *udc = dev_id;
501 unsigned long flags;
503 spin_lock_irqsave(&udc->lock, flags);
504 /* its a vbus change interrupt */
505 udc->vbus_active = !udc->vbus_active;
506 udc->todo |= UDC_START;
507 usb_udc_vbus_handler(&udc->gadget, udc->vbus_active);
508 usb_gadget_set_state(&udc->gadget, udc->vbus_active
509 ? USB_STATE_POWERED : USB_STATE_NOTATTACHED);
510 spin_unlock_irqrestore(&udc->lock, flags);
512 if (udc->thread_task &&
513 udc->thread_task->state != TASK_RUNNING)
514 wake_up_process(udc->thread_task);
516 return IRQ_HANDLED;
519 static irqreturn_t max3420_irq_handler(int irq, void *dev_id)
521 struct max3420_udc *udc = dev_id;
522 struct spi_device *spi = udc->spi;
523 unsigned long flags;
525 spin_lock_irqsave(&udc->lock, flags);
526 if ((udc->todo & ENABLE_IRQ) == 0) {
527 disable_irq_nosync(spi->irq);
528 udc->todo |= ENABLE_IRQ;
530 spin_unlock_irqrestore(&udc->lock, flags);
532 if (udc->thread_task &&
533 udc->thread_task->state != TASK_RUNNING)
534 wake_up_process(udc->thread_task);
536 return IRQ_HANDLED;
539 static void max3420_getstatus(struct max3420_udc *udc)
541 struct max3420_ep *ep;
542 u16 status = 0;
544 switch (udc->setup.bRequestType & USB_RECIP_MASK) {
545 case USB_RECIP_DEVICE:
546 /* Get device status */
547 status = udc->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED;
548 status |= (udc->remote_wkp << USB_DEVICE_REMOTE_WAKEUP);
549 break;
550 case USB_RECIP_INTERFACE:
551 if (udc->driver->setup(&udc->gadget, &udc->setup) < 0)
552 goto stall;
553 break;
554 case USB_RECIP_ENDPOINT:
555 ep = &udc->ep[udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK];
556 if (udc->setup.wIndex & USB_DIR_IN) {
557 if (!ep->ep_usb.caps.dir_in)
558 goto stall;
559 } else {
560 if (!ep->ep_usb.caps.dir_out)
561 goto stall;
563 if (ep->halted)
564 status = 1 << USB_ENDPOINT_HALT;
565 break;
566 default:
567 goto stall;
570 status = cpu_to_le16(status);
571 spi_wr_buf(udc, MAX3420_REG_EP0FIFO, &status, 2);
572 spi_wr8_ack(udc, MAX3420_REG_EP0BC, 2, 1);
573 return;
574 stall:
575 dev_err(udc->dev, "Can't respond to getstatus request\n");
576 spi_wr8(udc, MAX3420_REG_EPSTALLS, STLEP0IN | STLEP0OUT | STLSTAT);
579 static void max3420_set_clear_feature(struct max3420_udc *udc)
581 struct max3420_ep *ep;
582 int set = udc->setup.bRequest == USB_REQ_SET_FEATURE;
583 unsigned long flags;
584 int id;
586 switch (udc->setup.bRequestType) {
587 case USB_RECIP_DEVICE:
588 if (udc->setup.wValue != USB_DEVICE_REMOTE_WAKEUP)
589 break;
591 if (udc->setup.bRequest == USB_REQ_SET_FEATURE)
592 udc->remote_wkp = 1;
593 else
594 udc->remote_wkp = 0;
596 return spi_ack_ctrl(udc);
598 case USB_RECIP_ENDPOINT:
599 if (udc->setup.wValue != USB_ENDPOINT_HALT)
600 break;
602 id = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
603 ep = &udc->ep[id];
605 spin_lock_irqsave(&ep->lock, flags);
606 ep->todo &= ~STALL_EP;
607 if (set)
608 ep->todo |= STALL;
609 else
610 ep->todo |= UNSTALL;
611 spin_unlock_irqrestore(&ep->lock, flags);
613 spi_max3420_stall(ep);
614 return;
615 default:
616 break;
619 dev_err(udc->dev, "Can't respond to SET/CLEAR FEATURE\n");
620 spi_wr8(udc, MAX3420_REG_EPSTALLS, STLEP0IN | STLEP0OUT | STLSTAT);
623 static void max3420_handle_setup(struct max3420_udc *udc)
625 struct usb_ctrlrequest setup;
626 u8 addr;
628 spi_rd_buf(udc, MAX3420_REG_SUDFIFO, (void *)&setup, 8);
630 udc->setup = setup;
631 udc->setup.wValue = cpu_to_le16(setup.wValue);
632 udc->setup.wIndex = cpu_to_le16(setup.wIndex);
633 udc->setup.wLength = cpu_to_le16(setup.wLength);
635 switch (udc->setup.bRequest) {
636 case USB_REQ_GET_STATUS:
637 /* Data+Status phase form udc */
638 if ((udc->setup.bRequestType &
639 (USB_DIR_IN | USB_TYPE_MASK)) !=
640 (USB_DIR_IN | USB_TYPE_STANDARD)) {
641 break;
643 return max3420_getstatus(udc);
644 case USB_REQ_SET_ADDRESS:
645 /* Status phase from udc */
646 if (udc->setup.bRequestType != (USB_DIR_OUT |
647 USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
648 break;
650 addr = spi_rd8_ack(udc, MAX3420_REG_FNADDR, 1);
651 dev_dbg(udc->dev, "Assigned Address=%d\n", udc->setup.wValue);
652 return;
653 case USB_REQ_CLEAR_FEATURE:
654 case USB_REQ_SET_FEATURE:
655 /* Requests with no data phase, status phase from udc */
656 if ((udc->setup.bRequestType & USB_TYPE_MASK)
657 != USB_TYPE_STANDARD)
658 break;
659 return max3420_set_clear_feature(udc);
660 default:
661 break;
664 if (udc->driver->setup(&udc->gadget, &setup) < 0) {
665 /* Stall EP0 */
666 spi_wr8(udc, MAX3420_REG_EPSTALLS,
667 STLEP0IN | STLEP0OUT | STLSTAT);
671 static void max3420_req_done(struct max3420_req *req, int status)
673 struct max3420_ep *ep = req->ep;
674 struct max3420_udc *udc = ep->udc;
676 if (req->usb_req.status == -EINPROGRESS)
677 req->usb_req.status = status;
678 else
679 status = req->usb_req.status;
681 if (status && status != -ESHUTDOWN)
682 dev_err(udc->dev, "%s done %p, status %d\n",
683 ep->ep_usb.name, req, status);
685 if (req->usb_req.complete)
686 req->usb_req.complete(&ep->ep_usb, &req->usb_req);
689 static int max3420_do_data(struct max3420_udc *udc, int ep_id, int in)
691 struct max3420_ep *ep = &udc->ep[ep_id];
692 struct max3420_req *req;
693 int done, length, psz;
694 void *buf;
696 if (list_empty(&ep->queue))
697 return false;
699 req = list_first_entry(&ep->queue, struct max3420_req, queue);
700 buf = req->usb_req.buf + req->usb_req.actual;
702 psz = ep->ep_usb.maxpacket;
703 length = req->usb_req.length - req->usb_req.actual;
704 length = min(length, psz);
706 if (length == 0) {
707 done = 1;
708 goto xfer_done;
711 done = 0;
712 if (in) {
713 prefetch(buf);
714 spi_wr_buf(udc, MAX3420_REG_EP0FIFO + ep_id, buf, length);
715 spi_wr8(udc, MAX3420_REG_EP0BC + ep_id, length);
716 if (length < psz)
717 done = 1;
718 } else {
719 psz = spi_rd8(udc, MAX3420_REG_EP0BC + ep_id);
720 length = min(length, psz);
721 prefetchw(buf);
722 spi_rd_buf(udc, MAX3420_REG_EP0FIFO + ep_id, buf, length);
723 if (length < ep->ep_usb.maxpacket)
724 done = 1;
727 req->usb_req.actual += length;
729 if (req->usb_req.actual == req->usb_req.length)
730 done = 1;
732 xfer_done:
733 if (done) {
734 unsigned long flags;
736 spin_lock_irqsave(&ep->lock, flags);
737 list_del_init(&req->queue);
738 spin_unlock_irqrestore(&ep->lock, flags);
740 if (ep_id == 0)
741 spi_ack_ctrl(udc);
743 max3420_req_done(req, 0);
746 return true;
749 static int max3420_handle_irqs(struct max3420_udc *udc)
751 u8 epien, epirq, usbirq, usbien, reg[4];
752 bool ret = false;
754 spi_rd_buf(udc, MAX3420_REG_EPIRQ, reg, 4);
755 epirq = reg[0];
756 epien = reg[1];
757 usbirq = reg[2];
758 usbien = reg[3];
760 usbirq &= usbien;
761 epirq &= epien;
763 if (epirq & SUDAVIRQ) {
764 spi_wr8(udc, MAX3420_REG_EPIRQ, SUDAVIRQ);
765 max3420_handle_setup(udc);
766 return true;
769 if (usbirq & VBUSIRQ) {
770 spi_wr8(udc, MAX3420_REG_USBIRQ, VBUSIRQ);
771 dev_dbg(udc->dev, "Cable plugged in\n");
772 return true;
775 if (usbirq & NOVBUSIRQ) {
776 spi_wr8(udc, MAX3420_REG_USBIRQ, NOVBUSIRQ);
777 dev_dbg(udc->dev, "Cable pulled out\n");
778 return true;
781 if (usbirq & URESIRQ) {
782 spi_wr8(udc, MAX3420_REG_USBIRQ, URESIRQ);
783 dev_dbg(udc->dev, "USB Reset - Start\n");
784 return true;
787 if (usbirq & URESDNIRQ) {
788 spi_wr8(udc, MAX3420_REG_USBIRQ, URESDNIRQ);
789 dev_dbg(udc->dev, "USB Reset - END\n");
790 spi_wr8(udc, MAX3420_REG_USBIEN, URESDNIRQ | URESIRQ);
791 spi_wr8(udc, MAX3420_REG_EPIEN, SUDAVIRQ | IN0BAVIRQ
792 | OUT0DAVIRQ);
793 return true;
796 if (usbirq & SUSPIRQ) {
797 spi_wr8(udc, MAX3420_REG_USBIRQ, SUSPIRQ);
798 dev_dbg(udc->dev, "USB Suspend - Enter\n");
799 udc->suspended = true;
800 return true;
803 if (usbirq & BUSACTIRQ) {
804 spi_wr8(udc, MAX3420_REG_USBIRQ, BUSACTIRQ);
805 dev_dbg(udc->dev, "USB Suspend - Exit\n");
806 udc->suspended = false;
807 return true;
810 if (usbirq & RWUDNIRQ) {
811 spi_wr8(udc, MAX3420_REG_USBIRQ, RWUDNIRQ);
812 dev_dbg(udc->dev, "Asked Host to wakeup\n");
813 return true;
816 if (usbirq & OSCOKIRQ) {
817 spi_wr8(udc, MAX3420_REG_USBIRQ, OSCOKIRQ);
818 dev_dbg(udc->dev, "Osc stabilized, start work\n");
819 return true;
822 if (epirq & OUT0DAVIRQ && max3420_do_data(udc, 0, 0)) {
823 spi_wr8_ack(udc, MAX3420_REG_EPIRQ, OUT0DAVIRQ, 1);
824 ret = true;
827 if (epirq & IN0BAVIRQ && max3420_do_data(udc, 0, 1))
828 ret = true;
830 if (epirq & OUT1DAVIRQ && max3420_do_data(udc, 1, 0)) {
831 spi_wr8_ack(udc, MAX3420_REG_EPIRQ, OUT1DAVIRQ, 1);
832 ret = true;
835 if (epirq & IN2BAVIRQ && max3420_do_data(udc, 2, 1))
836 ret = true;
838 if (epirq & IN3BAVIRQ && max3420_do_data(udc, 3, 1))
839 ret = true;
841 return ret;
844 static int max3420_thread(void *dev_id)
846 struct max3420_udc *udc = dev_id;
847 struct spi_device *spi = udc->spi;
848 int i, loop_again = 1;
849 unsigned long flags;
851 while (!kthread_should_stop()) {
852 if (!loop_again) {
853 ktime_t kt = ns_to_ktime(1000 * 1000 * 250); /* 250ms */
855 set_current_state(TASK_INTERRUPTIBLE);
857 spin_lock_irqsave(&udc->lock, flags);
858 if (udc->todo & ENABLE_IRQ) {
859 enable_irq(spi->irq);
860 udc->todo &= ~ENABLE_IRQ;
862 spin_unlock_irqrestore(&udc->lock, flags);
864 schedule_hrtimeout(&kt, HRTIMER_MODE_REL);
866 loop_again = 0;
868 mutex_lock(&udc->spi_bus_mutex);
870 /* If bus-vbus_active and disconnected */
871 if (!udc->vbus_active || !udc->softconnect)
872 goto loop;
874 if (max3420_start(udc)) {
875 loop_again = 1;
876 goto loop;
879 if (max3420_handle_irqs(udc)) {
880 loop_again = 1;
881 goto loop;
884 if (spi_max3420_rwkup(udc)) {
885 loop_again = 1;
886 goto loop;
889 max3420_do_data(udc, 0, 1); /* get done with the EP0 ZLP */
891 for (i = 1; i < MAX3420_MAX_EPS; i++) {
892 struct max3420_ep *ep = &udc->ep[i];
894 if (spi_max3420_enable(ep))
895 loop_again = 1;
896 if (spi_max3420_stall(ep))
897 loop_again = 1;
899 loop:
900 mutex_unlock(&udc->spi_bus_mutex);
903 set_current_state(TASK_RUNNING);
904 dev_info(udc->dev, "SPI thread exiting");
905 return 0;
908 static int max3420_ep_set_halt(struct usb_ep *_ep, int stall)
910 struct max3420_ep *ep = to_max3420_ep(_ep);
911 struct max3420_udc *udc = ep->udc;
912 unsigned long flags;
914 spin_lock_irqsave(&ep->lock, flags);
916 ep->todo &= ~STALL_EP;
917 if (stall)
918 ep->todo |= STALL;
919 else
920 ep->todo |= UNSTALL;
922 spin_unlock_irqrestore(&ep->lock, flags);
924 wake_up_process(udc->thread_task);
926 dev_dbg(udc->dev, "%sStall %s\n", stall ? "" : "Un", ep->name);
927 return 0;
930 static int __max3420_ep_enable(struct max3420_ep *ep,
931 const struct usb_endpoint_descriptor *desc)
933 unsigned int maxp = usb_endpoint_maxp(desc);
934 unsigned long flags;
936 spin_lock_irqsave(&ep->lock, flags);
937 ep->ep_usb.desc = desc;
938 ep->ep_usb.maxpacket = maxp;
940 ep->todo &= ~ENABLE_EP;
941 ep->todo |= ENABLE;
942 spin_unlock_irqrestore(&ep->lock, flags);
944 return 0;
947 static int max3420_ep_enable(struct usb_ep *_ep,
948 const struct usb_endpoint_descriptor *desc)
950 struct max3420_ep *ep = to_max3420_ep(_ep);
951 struct max3420_udc *udc = ep->udc;
953 __max3420_ep_enable(ep, desc);
955 wake_up_process(udc->thread_task);
957 return 0;
960 static void max3420_nuke(struct max3420_ep *ep, int status)
962 struct max3420_req *req, *r;
963 unsigned long flags;
965 spin_lock_irqsave(&ep->lock, flags);
967 list_for_each_entry_safe(req, r, &ep->queue, queue) {
968 list_del_init(&req->queue);
970 spin_unlock_irqrestore(&ep->lock, flags);
971 max3420_req_done(req, status);
972 spin_lock_irqsave(&ep->lock, flags);
975 spin_unlock_irqrestore(&ep->lock, flags);
978 static void __max3420_ep_disable(struct max3420_ep *ep)
980 struct max3420_udc *udc = ep->udc;
981 unsigned long flags;
983 spin_lock_irqsave(&ep->lock, flags);
985 ep->ep_usb.desc = NULL;
987 ep->todo &= ~ENABLE_EP;
988 ep->todo |= DISABLE;
990 spin_unlock_irqrestore(&ep->lock, flags);
992 dev_dbg(udc->dev, "Disabled %s\n", ep->name);
995 static int max3420_ep_disable(struct usb_ep *_ep)
997 struct max3420_ep *ep = to_max3420_ep(_ep);
998 struct max3420_udc *udc = ep->udc;
1000 max3420_nuke(ep, -ESHUTDOWN);
1002 __max3420_ep_disable(ep);
1004 wake_up_process(udc->thread_task);
1006 return 0;
1009 static struct usb_request *max3420_alloc_request(struct usb_ep *_ep,
1010 gfp_t gfp_flags)
1012 struct max3420_ep *ep = to_max3420_ep(_ep);
1013 struct max3420_req *req;
1015 req = kzalloc(sizeof(*req), gfp_flags);
1016 if (!req)
1017 return NULL;
1019 req->ep = ep;
1021 return &req->usb_req;
1024 static void max3420_free_request(struct usb_ep *_ep, struct usb_request *_req)
1026 kfree(to_max3420_req(_req));
1029 static int max3420_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1030 gfp_t ignored)
1032 struct max3420_req *req = to_max3420_req(_req);
1033 struct max3420_ep *ep = to_max3420_ep(_ep);
1034 struct max3420_udc *udc = ep->udc;
1035 unsigned long flags;
1037 _req->status = -EINPROGRESS;
1038 _req->actual = 0;
1040 spin_lock_irqsave(&ep->lock, flags);
1041 list_add_tail(&req->queue, &ep->queue);
1042 spin_unlock_irqrestore(&ep->lock, flags);
1044 wake_up_process(udc->thread_task);
1045 return 0;
1048 static int max3420_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1050 struct max3420_req *t, *req = to_max3420_req(_req);
1051 struct max3420_ep *ep = to_max3420_ep(_ep);
1052 unsigned long flags;
1054 spin_lock_irqsave(&ep->lock, flags);
1056 /* Pluck the descriptor from queue */
1057 list_for_each_entry(t, &ep->queue, queue)
1058 if (t == req) {
1059 list_del_init(&req->queue);
1060 break;
1063 spin_unlock_irqrestore(&ep->lock, flags);
1065 if (t == req)
1066 max3420_req_done(req, -ECONNRESET);
1068 return 0;
1071 static const struct usb_ep_ops max3420_ep_ops = {
1072 .enable = max3420_ep_enable,
1073 .disable = max3420_ep_disable,
1074 .alloc_request = max3420_alloc_request,
1075 .free_request = max3420_free_request,
1076 .queue = max3420_ep_queue,
1077 .dequeue = max3420_ep_dequeue,
1078 .set_halt = max3420_ep_set_halt,
1081 static int max3420_wakeup(struct usb_gadget *gadget)
1083 struct max3420_udc *udc = to_udc(gadget);
1084 unsigned long flags;
1085 int ret = -EINVAL;
1087 spin_lock_irqsave(&udc->lock, flags);
1089 /* Only if wakeup allowed by host */
1090 if (udc->remote_wkp) {
1091 udc->todo |= REMOTE_WAKEUP;
1092 ret = 0;
1095 spin_unlock_irqrestore(&udc->lock, flags);
1097 if (udc->thread_task &&
1098 udc->thread_task->state != TASK_RUNNING)
1099 wake_up_process(udc->thread_task);
1100 return ret;
1103 static int max3420_udc_start(struct usb_gadget *gadget,
1104 struct usb_gadget_driver *driver)
1106 struct max3420_udc *udc = to_udc(gadget);
1107 unsigned long flags;
1109 spin_lock_irqsave(&udc->lock, flags);
1110 /* hook up the driver */
1111 driver->driver.bus = NULL;
1112 udc->driver = driver;
1113 udc->gadget.speed = USB_SPEED_FULL;
1115 udc->gadget.is_selfpowered = udc->is_selfpowered;
1116 udc->remote_wkp = 0;
1117 udc->softconnect = true;
1118 udc->todo |= UDC_START;
1119 spin_unlock_irqrestore(&udc->lock, flags);
1121 if (udc->thread_task &&
1122 udc->thread_task->state != TASK_RUNNING)
1123 wake_up_process(udc->thread_task);
1125 return 0;
1128 static int max3420_udc_stop(struct usb_gadget *gadget)
1130 struct max3420_udc *udc = to_udc(gadget);
1131 unsigned long flags;
1133 spin_lock_irqsave(&udc->lock, flags);
1134 udc->is_selfpowered = udc->gadget.is_selfpowered;
1135 udc->gadget.speed = USB_SPEED_UNKNOWN;
1136 udc->driver = NULL;
1137 udc->softconnect = false;
1138 udc->todo |= UDC_START;
1139 spin_unlock_irqrestore(&udc->lock, flags);
1141 if (udc->thread_task &&
1142 udc->thread_task->state != TASK_RUNNING)
1143 wake_up_process(udc->thread_task);
1145 return 0;
1148 static const struct usb_gadget_ops max3420_udc_ops = {
1149 .udc_start = max3420_udc_start,
1150 .udc_stop = max3420_udc_stop,
1151 .wakeup = max3420_wakeup,
1154 static void max3420_eps_init(struct max3420_udc *udc)
1156 int idx;
1158 INIT_LIST_HEAD(&udc->gadget.ep_list);
1160 for (idx = 0; idx < MAX3420_MAX_EPS; idx++) {
1161 struct max3420_ep *ep = &udc->ep[idx];
1163 spin_lock_init(&ep->lock);
1164 INIT_LIST_HEAD(&ep->queue);
1166 ep->udc = udc;
1167 ep->id = idx;
1168 ep->halted = 0;
1169 ep->maxpacket = 0;
1170 ep->ep_usb.name = ep->name;
1171 ep->ep_usb.ops = &max3420_ep_ops;
1172 usb_ep_set_maxpacket_limit(&ep->ep_usb, MAX3420_EP_MAX_PACKET);
1174 if (idx == 0) { /* For EP0 */
1175 ep->ep_usb.desc = &ep0_desc;
1176 ep->ep_usb.maxpacket = usb_endpoint_maxp(&ep0_desc);
1177 ep->ep_usb.caps.type_control = true;
1178 ep->ep_usb.caps.dir_in = true;
1179 ep->ep_usb.caps.dir_out = true;
1180 snprintf(ep->name, MAX3420_EPNAME_SIZE, "ep0");
1181 continue;
1184 if (idx == 1) { /* EP1 is OUT */
1185 ep->ep_usb.caps.dir_in = false;
1186 ep->ep_usb.caps.dir_out = true;
1187 snprintf(ep->name, MAX3420_EPNAME_SIZE, "ep1-bulk-out");
1188 } else { /* EP2 & EP3 are IN */
1189 ep->ep_usb.caps.dir_in = true;
1190 ep->ep_usb.caps.dir_out = false;
1191 snprintf(ep->name, MAX3420_EPNAME_SIZE,
1192 "ep%d-bulk-in", idx);
1194 ep->ep_usb.caps.type_iso = false;
1195 ep->ep_usb.caps.type_int = false;
1196 ep->ep_usb.caps.type_bulk = true;
1198 list_add_tail(&ep->ep_usb.ep_list,
1199 &udc->gadget.ep_list);
1203 static int max3420_probe(struct spi_device *spi)
1205 struct max3420_udc *udc;
1206 int err, irq;
1207 u8 reg[8];
1209 if (spi->master->flags & SPI_MASTER_HALF_DUPLEX) {
1210 dev_err(&spi->dev, "UDC needs full duplex to work\n");
1211 return -EINVAL;
1214 spi->mode = SPI_MODE_3;
1215 spi->bits_per_word = 8;
1217 err = spi_setup(spi);
1218 if (err) {
1219 dev_err(&spi->dev, "Unable to setup SPI bus\n");
1220 return -EFAULT;
1223 udc = devm_kzalloc(&spi->dev, sizeof(*udc), GFP_KERNEL);
1224 if (!udc)
1225 return -ENOMEM;
1227 udc->spi = spi;
1229 udc->remote_wkp = 0;
1231 /* Setup gadget structure */
1232 udc->gadget.ops = &max3420_udc_ops;
1233 udc->gadget.max_speed = USB_SPEED_FULL;
1234 udc->gadget.speed = USB_SPEED_UNKNOWN;
1235 udc->gadget.ep0 = &udc->ep[0].ep_usb;
1236 udc->gadget.name = driver_name;
1238 spin_lock_init(&udc->lock);
1239 mutex_init(&udc->spi_bus_mutex);
1241 udc->ep0req.ep = &udc->ep[0];
1242 udc->ep0req.usb_req.buf = udc->ep0buf;
1243 INIT_LIST_HEAD(&udc->ep0req.queue);
1245 /* setup Endpoints */
1246 max3420_eps_init(udc);
1248 /* configure SPI */
1249 spi_rd_buf(udc, MAX3420_REG_EPIRQ, reg, 8);
1250 spi_wr8(udc, MAX3420_REG_PINCTL, FDUPSPI);
1252 err = usb_add_gadget_udc(&spi->dev, &udc->gadget);
1253 if (err)
1254 return err;
1256 udc->dev = &udc->gadget.dev;
1258 spi_set_drvdata(spi, udc);
1260 irq = of_irq_get_byname(spi->dev.of_node, "udc");
1261 err = devm_request_irq(&spi->dev, irq, max3420_irq_handler, 0,
1262 "max3420", udc);
1263 if (err < 0)
1264 return err;
1266 udc->thread_task = kthread_create(max3420_thread, udc,
1267 "max3420-thread");
1268 if (IS_ERR(udc->thread_task))
1269 return PTR_ERR(udc->thread_task);
1271 irq = of_irq_get_byname(spi->dev.of_node, "vbus");
1272 if (irq <= 0) { /* no vbus irq implies self-powered design */
1273 udc->is_selfpowered = 1;
1274 udc->vbus_active = true;
1275 udc->todo |= UDC_START;
1276 usb_udc_vbus_handler(&udc->gadget, udc->vbus_active);
1277 usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED);
1278 max3420_start(udc);
1279 } else {
1280 udc->is_selfpowered = 0;
1281 /* Detect current vbus status */
1282 spi_rd_buf(udc, MAX3420_REG_EPIRQ, reg, 8);
1283 if (reg[7] != 0xff)
1284 udc->vbus_active = true;
1286 err = devm_request_irq(&spi->dev, irq,
1287 max3420_vbus_handler, 0, "vbus", udc);
1288 if (err < 0)
1289 return err;
1292 return 0;
1295 static int max3420_remove(struct spi_device *spi)
1297 struct max3420_udc *udc = spi_get_drvdata(spi);
1298 unsigned long flags;
1300 usb_del_gadget_udc(&udc->gadget);
1302 spin_lock_irqsave(&udc->lock, flags);
1304 kthread_stop(udc->thread_task);
1306 spin_unlock_irqrestore(&udc->lock, flags);
1308 return 0;
1311 static const struct of_device_id max3420_udc_of_match[] = {
1312 { .compatible = "maxim,max3420-udc"},
1313 { .compatible = "maxim,max3421-udc"},
1316 MODULE_DEVICE_TABLE(of, max3420_udc_of_match);
1318 static struct spi_driver max3420_driver = {
1319 .driver = {
1320 .name = "max3420-udc",
1321 .of_match_table = of_match_ptr(max3420_udc_of_match),
1323 .probe = max3420_probe,
1324 .remove = max3420_remove,
1327 module_spi_driver(max3420_driver);
1329 MODULE_DESCRIPTION(DRIVER_DESC);
1330 MODULE_AUTHOR("Jassi Brar <jaswinder.singh@linaro.org>");
1331 MODULE_LICENSE("GPL");