Debian Codegraph @

drivers/usb/chipidea/core.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * core.c - ChipIdea USB IP core family device controller
4 *
5 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
6 * Copyright (C) 2020 NXP
7 *
8 * Author: David Lopo
9 * Peter Chen <[email protected]>
10 *
11 * Main Features:
12 * - Four transfers are supported, usbtest is passed
13 * - USB Certification for gadget: CH9 and Mass Storage are passed
14 * - Low power mode
15 * - USB wakeup
16 */
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/extcon.h>
21 #include <linux/phy/phy.h>
22 #include <linux/platform_device.h>
23 #include <linux/module.h>
24 #include <linux/idr.h>
25 #include <linux/interrupt.h>
26 #include <linux/io.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/pm_domain.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/otg.h>
35 #include <linux/usb/chipidea.h>
36 #include <linux/usb/of.h>
37 #include <linux/of.h>
38 #include <linux/regulator/consumer.h>
39 #include <linux/usb/ehci_def.h>
40
41 #include "ci.h"
42 #include "udc.h"
43 #include "bits.h"
44 #include "host.h"
45 #include "otg.h"
46 #include "otg_fsm.h"
47
48 /* Controller register map */
49 static const u8 ci_regs_nolpm[] = {
50 [CAP_CAPLENGTH] = 0x00U,
51 [CAP_HCCPARAMS] = 0x08U,
52 [CAP_DCCPARAMS] = 0x24U,
53 [CAP_TESTMODE] = 0x38U,
54 [OP_USBCMD] = 0x00U,
55 [OP_USBSTS] = 0x04U,
56 [OP_USBINTR] = 0x08U,
57 [OP_FRINDEX] = 0x0CU,
58 [OP_DEVICEADDR] = 0x14U,
59 [OP_ENDPTLISTADDR] = 0x18U,
60 [OP_TTCTRL] = 0x1CU,
61 [OP_BURSTSIZE] = 0x20U,
62 [OP_ULPI_VIEWPORT] = 0x30U,
63 [OP_PORTSC] = 0x44U,
64 [OP_DEVLC] = 0x84U,
65 [OP_OTGSC] = 0x64U,
66 [OP_USBMODE] = 0x68U,
67 [OP_ENDPTSETUPSTAT] = 0x6CU,
68 [OP_ENDPTPRIME] = 0x70U,
69 [OP_ENDPTFLUSH] = 0x74U,
70 [OP_ENDPTSTAT] = 0x78U,
71 [OP_ENDPTCOMPLETE] = 0x7CU,
72 [OP_ENDPTCTRL] = 0x80U,
73 };
74
75 static const u8 ci_regs_lpm[] = {
76 [CAP_CAPLENGTH] = 0x00U,
77 [CAP_HCCPARAMS] = 0x08U,
78 [CAP_DCCPARAMS] = 0x24U,
79 [CAP_TESTMODE] = 0xFCU,
80 [OP_USBCMD] = 0x00U,
81 [OP_USBSTS] = 0x04U,
82 [OP_USBINTR] = 0x08U,
83 [OP_FRINDEX] = 0x0CU,
84 [OP_DEVICEADDR] = 0x14U,
85 [OP_ENDPTLISTADDR] = 0x18U,
86 [OP_TTCTRL] = 0x1CU,
87 [OP_BURSTSIZE] = 0x20U,
88 [OP_ULPI_VIEWPORT] = 0x30U,
89 [OP_PORTSC] = 0x44U,
90 [OP_DEVLC] = 0x84U,
91 [OP_OTGSC] = 0xC4U,
92 [OP_USBMODE] = 0xC8U,
93 [OP_ENDPTSETUPSTAT] = 0xD8U,
94 [OP_ENDPTPRIME] = 0xDCU,
95 [OP_ENDPTFLUSH] = 0xE0U,
96 [OP_ENDPTSTAT] = 0xE4U,
97 [OP_ENDPTCOMPLETE] = 0xE8U,
98 [OP_ENDPTCTRL] = 0xECU,
99 };
100
101 static void hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
102 {
103 int i;
104
105 for (i = 0; i < OP_ENDPTCTRL; i++)
106 ci->hw_bank.regmap[i] =
107 (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
108 (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
109
110 for (; i <= OP_LAST; i++)
111 ci->hw_bank.regmap[i] = ci->hw_bank.op +
112 4 * (i - OP_ENDPTCTRL) +
113 (is_lpm
114 ? ci_regs_lpm[OP_ENDPTCTRL]
115 : ci_regs_nolpm[OP_ENDPTCTRL]);
116
117 }
118
119 static enum ci_revision ci_get_revision(struct ci_hdrc *ci)
120 {
121 int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION);
122 enum ci_revision rev = CI_REVISION_UNKNOWN;
123
124 if (ver == 0x2) {
125 rev = hw_read_id_reg(ci, ID_ID, REVISION)
126 >> __ffs(REVISION);
127 rev += CI_REVISION_20;
128 } else if (ver == 0x0) {
129 rev = CI_REVISION_1X;
130 }
131
132 return rev;
133 }
134
135 /**
136 * hw_read_intr_enable: returns interrupt enable register
137 *
138 * @ci: the controller
139 *
140 * This function returns register data
141 */
142 u32 hw_read_intr_enable(struct ci_hdrc *ci)
143 {
144 return hw_read(ci, OP_USBINTR, ~0);
145 }
146
147 /**
148 * hw_read_intr_status: returns interrupt status register
149 *
150 * @ci: the controller
151 *
152 * This function returns register data
153 */
154 u32 hw_read_intr_status(struct ci_hdrc *ci)
155 {
156 return hw_read(ci, OP_USBSTS, ~0);
157 }
158
159 /**
160 * hw_port_test_set: writes port test mode (execute without interruption)
161 * @ci: the controller
162 * @mode: new value
163 *
164 * This function returns an error code
165 */
166 int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
167 {
168 const u8 TEST_MODE_MAX = 7;
169
170 if (mode > TEST_MODE_MAX)
171 return -EINVAL;
172
173 hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
174 return 0;
175 }
176
177 /**
178 * hw_port_test_get: reads port test mode value
179 *
180 * @ci: the controller
181 *
182 * This function returns port test mode value
183 */
184 u8 hw_port_test_get(struct ci_hdrc *ci)
185 {
186 return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
187 }
188
189 static void hw_wait_phy_stable(void)
190 {
191 /*
192 * The phy needs some delay to output the stable status from low
193 * power mode. And for OTGSC, the status inputs are debounced
194 * using a 1 ms time constant, so, delay 2ms for controller to get
195 * the stable status, like vbus and id when the phy leaves low power.
196 */
197 usleep_range(2000, 2500);
198 }
199
200 /* The PHY enters/leaves low power mode */
201 static void ci_hdrc_enter_lpm_common(struct ci_hdrc *ci, bool enable)
202 {
203 enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
204 bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
205
206 if (enable && !lpm)
207 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
208 PORTSC_PHCD(ci->hw_bank.lpm));
209 else if (!enable && lpm)
210 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
211 0);
212 }
213
214 static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
215 {
216 return ci->platdata->enter_lpm(ci, enable);
217 }
218
219 static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
220 {
221 u32 reg;
222
223 /* bank is a module variable */
224 ci->hw_bank.abs = base;
225
226 ci->hw_bank.cap = ci->hw_bank.abs;
227 ci->hw_bank.cap += ci->platdata->capoffset;
228 ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
229
230 hw_alloc_regmap(ci, false);
231 reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
232 __ffs(HCCPARAMS_LEN);
233 ci->hw_bank.lpm = reg;
234 if (reg)
235 hw_alloc_regmap(ci, !!reg);
236 ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
237 ci->hw_bank.size += OP_LAST;
238 ci->hw_bank.size /= sizeof(u32);
239
240 reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
241 __ffs(DCCPARAMS_DEN);
242 ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
243
244 if (ci->hw_ep_max > ENDPT_MAX)
245 return -ENODEV;
246
247 ci_hdrc_enter_lpm(ci, false);
248
249 /* Disable all interrupts bits */
250 hw_write(ci, OP_USBINTR, 0xffffffff, 0);
251
252 /* Clear all interrupts status bits*/
253 hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
254
255 ci->rev = ci_get_revision(ci);
256
257 dev_dbg(ci->dev,
258 "revision: %d, lpm: %d; cap: %px op: %px\n",
259 ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
260
261 /* setup lock mode ? */
262
263 /* ENDPTSETUPSTAT is '0' by default */
264
265 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
266
267 return 0;
268 }
269
270 void hw_phymode_configure(struct ci_hdrc *ci)
271 {
272 u32 portsc, lpm, sts = 0;
273
274 switch (ci->platdata->phy_mode) {
275 case USBPHY_INTERFACE_MODE_UTMI:
276 portsc = PORTSC_PTS(PTS_UTMI);
277 lpm = DEVLC_PTS(PTS_UTMI);
278 break;
279 case USBPHY_INTERFACE_MODE_UTMIW:
280 portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
281 lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
282 break;
283 case USBPHY_INTERFACE_MODE_ULPI:
284 portsc = PORTSC_PTS(PTS_ULPI);
285 lpm = DEVLC_PTS(PTS_ULPI);
286 break;
287 case USBPHY_INTERFACE_MODE_SERIAL:
288 portsc = PORTSC_PTS(PTS_SERIAL);
289 lpm = DEVLC_PTS(PTS_SERIAL);
290 sts = 1;
291 break;
292 case USBPHY_INTERFACE_MODE_HSIC:
293 portsc = PORTSC_PTS(PTS_HSIC);
294 lpm = DEVLC_PTS(PTS_HSIC);
295 break;
296 default:
297 return;
298 }
299
300 if (ci->hw_bank.lpm) {
301 hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
302 if (sts)
303 hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS);
304 } else {
305 hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
306 if (sts)
307 hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS);
308 }
309 }
310 EXPORT_SYMBOL_GPL(hw_phymode_configure);
311
312 /**
313 * _ci_usb_phy_init: initialize phy taking in account both phy and usb_phy
314 * interfaces
315 * @ci: the controller
316 *
317 * This function returns an error code if the phy failed to init
318 */
319 static int _ci_usb_phy_init(struct ci_hdrc *ci)
320 {
321 int ret;
322
323 if (ci->phy) {
324 ret = phy_init(ci->phy);
325 if (ret)
326 return ret;
327
328 ret = phy_power_on(ci->phy);
329 if (ret) {
330 phy_exit(ci->phy);
331 return ret;
332 }
333 } else {
334 ret = usb_phy_init(ci->usb_phy);
335 }
336
337 return ret;
338 }
339
340 /**
341 * ci_usb_phy_exit: deinitialize phy taking in account both phy and usb_phy
342 * interfaces
343 * @ci: the controller
344 */
345 static void ci_usb_phy_exit(struct ci_hdrc *ci)
346 {
347 if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL)
348 return;
349
350 if (ci->phy) {
351 phy_power_off(ci->phy);
352 phy_exit(ci->phy);
353 } else {
354 usb_phy_shutdown(ci->usb_phy);
355 }
356 }
357
358 /**
359 * ci_usb_phy_init: initialize phy according to different phy type
360 * @ci: the controller
361 *
362 * This function returns an error code if usb_phy_init has failed
363 */
364 static int ci_usb_phy_init(struct ci_hdrc *ci)
365 {
366 int ret;
367
368 if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL)
369 return 0;
370
371 switch (ci->platdata->phy_mode) {
372 case USBPHY_INTERFACE_MODE_UTMI:
373 case USBPHY_INTERFACE_MODE_UTMIW:
374 case USBPHY_INTERFACE_MODE_HSIC:
375 ret = _ci_usb_phy_init(ci);
376 if (!ret)
377 hw_wait_phy_stable();
378 else
379 return ret;
380 hw_phymode_configure(ci);
381 break;
382 case USBPHY_INTERFACE_MODE_ULPI:
383 case USBPHY_INTERFACE_MODE_SERIAL:
384 hw_phymode_configure(ci);
385 ret = _ci_usb_phy_init(ci);
386 if (ret)
387 return ret;
388 break;
389 default:
390 ret = _ci_usb_phy_init(ci);
391 if (!ret)
392 hw_wait_phy_stable();
393 }
394
395 return ret;
396 }
397
398
399 /**
400 * ci_platform_configure: do controller configure
401 * @ci: the controller
402 *
403 */
404 void ci_platform_configure(struct ci_hdrc *ci)
405 {
406 bool is_device_mode, is_host_mode;
407
408 is_device_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_DC;
409 is_host_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_HC;
410
411 if (is_device_mode) {
412 phy_set_mode(ci->phy, PHY_MODE_USB_DEVICE);
413
414 if (ci->platdata->flags & CI_HDRC_DISABLE_DEVICE_STREAMING)
415 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS,
416 USBMODE_CI_SDIS);
417 }
418
419 if (is_host_mode) {
420 phy_set_mode(ci->phy, PHY_MODE_USB_HOST);
421
422 if (ci->platdata->flags & CI_HDRC_DISABLE_HOST_STREAMING)
423 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS,
424 USBMODE_CI_SDIS);
425 }
426
427 if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) {
428 if (ci->hw_bank.lpm)
429 hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC);
430 else
431 hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
432 }
433
434 if (ci->platdata->flags & CI_HDRC_SET_NON_ZERO_TTHA)
435 hw_write(ci, OP_TTCTRL, TTCTRL_TTHA_MASK, TTCTRL_TTHA);
436
437 hw_write(ci, OP_USBCMD, 0xff0000, ci->platdata->itc_setting << 16);
438
439 if (ci->platdata->flags & CI_HDRC_OVERRIDE_AHB_BURST)
440 hw_write_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK,
441 ci->platdata->ahb_burst_config);
442
443 /* override burst size, take effect only when ahb_burst_config is 0 */
444 if (!hw_read_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK)) {
445 if (ci->platdata->flags & CI_HDRC_OVERRIDE_TX_BURST)
446 hw_write(ci, OP_BURSTSIZE, TX_BURST_MASK,
447 ci->platdata->tx_burst_size << __ffs(TX_BURST_MASK));
448
449 if (ci->platdata->flags & CI_HDRC_OVERRIDE_RX_BURST)
450 hw_write(ci, OP_BURSTSIZE, RX_BURST_MASK,
451 ci->platdata->rx_burst_size);
452 }
453 }
454
455 /**
456 * hw_controller_reset: do controller reset
457 * @ci: the controller
458 *
459 * This function returns an error code
460 */
461 static int hw_controller_reset(struct ci_hdrc *ci)
462 {
463 int count = 0;
464
465 hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
466 while (hw_read(ci, OP_USBCMD, USBCMD_RST)) {
467 udelay(10);
468 if (count++ > 1000)
469 return -ETIMEDOUT;
470 }
471
472 return 0;
473 }
474
475 /**
476 * hw_device_reset: resets chip (execute without interruption)
477 * @ci: the controller
478 *
479 * This function returns an error code
480 */
481 int hw_device_reset(struct ci_hdrc *ci)
482 {
483 int ret;
484
485 /* should flush & stop before reset */
486 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
487 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
488
489 ret = hw_controller_reset(ci);
490 if (ret) {
491 dev_err(ci->dev, "error resetting controller, ret=%d\n", ret);
492 return ret;
493 }
494
495 if (ci->platdata->notify_event) {
496 ret = ci->platdata->notify_event(ci,
497 CI_HDRC_CONTROLLER_RESET_EVENT);
498 if (ret)
499 return ret;
500 }
501
502 /* USBMODE should be configured step by step */
503 hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
504 hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC);
505 /* HW >= 2.3 */
506 hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
507
508 if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
509 dev_err(ci->dev, "cannot enter in %s device mode\n",
510 ci_role(ci)->name);
511 dev_err(ci->dev, "lpm = %i\n", ci->hw_bank.lpm);
512 return -ENODEV;
513 }
514
515 ci_platform_configure(ci);
516
517 return 0;
518 }
519
520 static irqreturn_t ci_irq_handler(int irq, void *data)
521 {
522 struct ci_hdrc *ci = data;
523 irqreturn_t ret = IRQ_NONE;
524 u32 otgsc = 0;
525
526 if (ci->in_lpm) {
527 /*
528 * If we already have a wakeup irq pending there,
529 * let's just return to wait resume finished firstly.
530 */
531 if (ci->wakeup_int)
532 return IRQ_HANDLED;
533
534 disable_irq_nosync(irq);
535 ci->wakeup_int = true;
536 pm_runtime_get(ci->dev);
537 return IRQ_HANDLED;
538 }
539
540 if (ci->is_otg) {
541 otgsc = hw_read_otgsc(ci, ~0);
542 if (ci_otg_is_fsm_mode(ci)) {
543 ret = ci_otg_fsm_irq(ci);
544 if (ret == IRQ_HANDLED)
545 return ret;
546 }
547
548 /*
549 * Handle id change interrupt, it indicates device/host function
550 * switch.
551 */
552 if ((otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
553 ci->id_event = true;
554 /* Clear ID change irq status */
555 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
556 }
557
558 /*
559 * Handle vbus change interrupt, it indicates device connection
560 * and disconnection events.
561 */
562 if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
563 ci->b_sess_valid_event = true;
564 /* Clear BSV irq */
565 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
566 }
567
568 if (ci->id_event || ci->b_sess_valid_event) {
569 ci_otg_queue_work(ci);
570 return IRQ_HANDLED;
571 }
572 }
573
574 /* Handle device/host interrupt */
575 if (ci->role != CI_ROLE_END)
576 ret = ci_role(ci)->irq(ci);
577
578 return ret;
579 }
580
581 static void ci_irq(struct ci_hdrc *ci)
582 {
583 unsigned long flags;
584
585 local_irq_save(flags);
586 ci_irq_handler(ci->irq, ci);
587 local_irq_restore(flags);
588 }
589
590 static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
591 void *ptr)
592 {
593 struct ci_hdrc_cable *cbl = container_of(nb, struct ci_hdrc_cable, nb);
594 struct ci_hdrc *ci = cbl->ci;
595
596 cbl->connected = event;
597 cbl->changed = true;
598
599 ci_irq(ci);
600 return NOTIFY_DONE;
601 }
602
603 static enum usb_role ci_usb_role_switch_get(struct usb_role_switch *sw)
604 {
605 struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
606 enum usb_role role;
607 unsigned long flags;
608
609 spin_lock_irqsave(&ci->lock, flags);
610 role = ci_role_to_usb_role(ci);
611 spin_unlock_irqrestore(&ci->lock, flags);
612
613 return role;
614 }
615
616 static int ci_usb_role_switch_set(struct usb_role_switch *sw,
617 enum usb_role role)
618 {
619 struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
620 struct ci_hdrc_cable *cable;
621
622 if (role == USB_ROLE_HOST) {
623 cable = &ci->platdata->id_extcon;
624 cable->changed = true;
625 cable->connected = true;
626 cable = &ci->platdata->vbus_extcon;
627 cable->changed = true;
628 cable->connected = false;
629 } else if (role == USB_ROLE_DEVICE) {
630 cable = &ci->platdata->id_extcon;
631 cable->changed = true;
632 cable->connected = false;
633 cable = &ci->platdata->vbus_extcon;
634 cable->changed = true;
635 cable->connected = true;
636 } else {
637 cable = &ci->platdata->id_extcon;
638 cable->changed = true;
639 cable->connected = false;
640 cable = &ci->platdata->vbus_extcon;
641 cable->changed = true;
642 cable->connected = false;
643 }
644
645 ci_irq(ci);
646 return 0;
647 }
648
649 static enum ci_role ci_get_role(struct ci_hdrc *ci)
650 {
651 enum ci_role role;
652
653 if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
654 if (ci->is_otg) {
655 role = ci_otg_role(ci);
656 hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
657 } else {
658 /*
659 * If the controller is not OTG capable, but support
660 * role switch, the defalt role is gadget, and the
661 * user can switch it through debugfs.
662 */
663 role = CI_ROLE_GADGET;
664 }
665 } else {
666 role = ci->roles[CI_ROLE_HOST] ? CI_ROLE_HOST
667 : CI_ROLE_GADGET;
668 }
669
670 return role;
671 }
672
673 static int ci_get_platdata(struct device *dev,
674 struct ci_hdrc_platform_data *platdata)
675 {
676 struct extcon_dev *ext_vbus, *ext_id;
677 struct ci_hdrc_cable *cable;
678 int ret;
679
680 if (!platdata->phy_mode)
681 platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
682
683 if (!platdata->dr_mode)
684 platdata->dr_mode = usb_get_dr_mode(dev);
685
686 if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
687 platdata->dr_mode = USB_DR_MODE_OTG;
688
689 if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
690 /* Get the vbus regulator */
691 platdata->reg_vbus = devm_regulator_get_optional(dev, "vbus");
692 if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
693 return -EPROBE_DEFER;
694 } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
695 /* no vbus regulator is needed */
696 platdata->reg_vbus = NULL;
697 } else if (IS_ERR(platdata->reg_vbus)) {
698 dev_err(dev, "Getting regulator error: %ld\n",
699 PTR_ERR(platdata->reg_vbus));
700 return PTR_ERR(platdata->reg_vbus);
701 }
702 /* Get TPL support */
703 if (!platdata->tpl_support)
704 platdata->tpl_support =
705 of_usb_host_tpl_support(dev->of_node);
706 }
707
708 if (platdata->dr_mode == USB_DR_MODE_OTG) {
709 /* We can support HNP and SRP of OTG 2.0 */
710 platdata->ci_otg_caps.otg_rev = 0x0200;
711 platdata->ci_otg_caps.hnp_support = true;
712 platdata->ci_otg_caps.srp_support = true;
713
714 /* Update otg capabilities by DT properties */
715 ret = of_usb_update_otg_caps(dev->of_node,
716 &platdata->ci_otg_caps);
717 if (ret)
718 return ret;
719 }
720
721 if (usb_get_maximum_speed(dev) == USB_SPEED_FULL)
722 platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
723
724 of_property_read_u32(dev->of_node, "phy-clkgate-delay-us",
725 &platdata->phy_clkgate_delay_us);
726
727 platdata->itc_setting = 1;
728
729 of_property_read_u32(dev->of_node, "itc-setting",
730 &platdata->itc_setting);
731
732 ret = of_property_read_u32(dev->of_node, "ahb-burst-config",
733 &platdata->ahb_burst_config);
734 if (!ret) {
735 platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST;
736 } else if (ret != -EINVAL) {
737 dev_err(dev, "failed to get ahb-burst-config\n");
738 return ret;
739 }
740
741 ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword",
742 &platdata->tx_burst_size);
743 if (!ret) {
744 platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST;
745 } else if (ret != -EINVAL) {
746 dev_err(dev, "failed to get tx-burst-size-dword\n");
747 return ret;
748 }
749
750 ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword",
751 &platdata->rx_burst_size);
752 if (!ret) {
753 platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST;
754 } else if (ret != -EINVAL) {
755 dev_err(dev, "failed to get rx-burst-size-dword\n");
756 return ret;
757 }
758
759 if (of_property_read_bool(dev->of_node, "non-zero-ttctrl-ttha"))
760 platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
761
762 ext_id = ERR_PTR(-ENODEV);
763 ext_vbus = ERR_PTR(-ENODEV);
764 if (of_property_present(dev->of_node, "extcon")) {
765 /* Each one of them is not mandatory */
766 ext_vbus = extcon_get_edev_by_phandle(dev, 0);
767 if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
768 return PTR_ERR(ext_vbus);
769
770 ext_id = extcon_get_edev_by_phandle(dev, 1);
771 if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
772 return PTR_ERR(ext_id);
773 }
774
775 cable = &platdata->vbus_extcon;
776 cable->nb.notifier_call = ci_cable_notifier;
777 cable->edev = ext_vbus;
778
779 if (!IS_ERR(ext_vbus)) {
780 ret = extcon_get_state(cable->edev, EXTCON_USB);
781 if (ret)
782 cable->connected = true;
783 else
784 cable->connected = false;
785 }
786
787 cable = &platdata->id_extcon;
788 cable->nb.notifier_call = ci_cable_notifier;
789 cable->edev = ext_id;
790
791 if (!IS_ERR(ext_id)) {
792 ret = extcon_get_state(cable->edev, EXTCON_USB_HOST);
793 if (ret)
794 cable->connected = true;
795 else
796 cable->connected = false;
797 }
798
799 platdata->pctl = devm_pinctrl_get(dev);
800 if (!IS_ERR(platdata->pctl)) {
801 struct pinctrl_state *p;
802
803 p = pinctrl_lookup_state(platdata->pctl, "default");
804 if (!IS_ERR(p))
805 platdata->pins_default = p;
806
807 p = pinctrl_lookup_state(platdata->pctl, "host");
808 if (!IS_ERR(p))
809 platdata->pins_host = p;
810
811 p = pinctrl_lookup_state(platdata->pctl, "device");
812 if (!IS_ERR(p))
813 platdata->pins_device = p;
814 }
815
816 if (!platdata->enter_lpm)
817 platdata->enter_lpm = ci_hdrc_enter_lpm_common;
818
819 return 0;
820 }
821
822 static int ci_extcon_register(struct ci_hdrc *ci)
823 {
824 struct ci_hdrc_cable *id, *vbus;
825 int ret;
826
827 id = &ci->platdata->id_extcon;
828 id->ci = ci;
829 if (!IS_ERR_OR_NULL(id->edev)) {
830 ret = devm_extcon_register_notifier(ci->dev, id->edev,
831 EXTCON_USB_HOST, &id->nb);
832 if (ret < 0) {
833 dev_err(ci->dev, "register ID failed\n");
834 return ret;
835 }
836 }
837
838 vbus = &ci->platdata->vbus_extcon;
839 vbus->ci = ci;
840 if (!IS_ERR_OR_NULL(vbus->edev)) {
841 ret = devm_extcon_register_notifier(ci->dev, vbus->edev,
842 EXTCON_USB, &vbus->nb);
843 if (ret < 0) {
844 dev_err(ci->dev, "register VBUS failed\n");
845 return ret;
846 }
847 }
848
849 return 0;
850 }
851
852 static void ci_power_lost_work(struct work_struct *work)
853 {
854 struct ci_hdrc *ci = container_of(work, struct ci_hdrc, power_lost_work);
855 enum ci_role role;
856
857 disable_irq_nosync(ci->irq);
858 pm_runtime_get_sync(ci->dev);
859 if (!ci_otg_is_fsm_mode(ci)) {
860 role = ci_get_role(ci);
861
862 if (ci->role != role) {
863 ci_handle_id_switch(ci);
864 } else if (role == CI_ROLE_GADGET) {
865 if (ci->is_otg && hw_read_otgsc(ci, OTGSC_BSV))
866 usb_gadget_vbus_connect(&ci->gadget);
867 }
868 }
869 pm_runtime_put_sync(ci->dev);
870 enable_irq(ci->irq);
871 }
872
873 static DEFINE_IDA(ci_ida);
874
875 struct platform_device *ci_hdrc_add_device(struct device *dev,
876 struct resource *res, int nres,
877 struct ci_hdrc_platform_data *platdata)
878 {
879 struct platform_device *pdev;
880 int id, ret;
881
882 ret = ci_get_platdata(dev, platdata);
883 if (ret)
884 return ERR_PTR(ret);
885
886 id = ida_alloc(&ci_ida, GFP_KERNEL);
887 if (id < 0)
888 return ERR_PTR(id);
889
890 pdev = platform_device_alloc("ci_hdrc", id);
891 if (!pdev) {
892 ret = -ENOMEM;
893 goto put_id;
894 }
895
896 pdev->dev.parent = dev;
897 device_set_of_node_from_dev(&pdev->dev, dev);
898
899 ret = platform_device_add_resources(pdev, res, nres);
900 if (ret)
901 goto err;
902
903 ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
904 if (ret)
905 goto err;
906
907 ret = platform_device_add(pdev);
908 if (ret)
909 goto err;
910
911 dev_pm_domain_detach(&pdev->dev, false);
912
913 return pdev;
914
915 err:
916 platform_device_put(pdev);
917 put_id:
918 ida_free(&ci_ida, id);
919 return ERR_PTR(ret);
920 }
921 EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
922
923 void ci_hdrc_remove_device(struct platform_device *pdev)
924 {
925 int id = pdev->id;
926 platform_device_unregister(pdev);
927 ida_free(&ci_ida, id);
928 }
929 EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
930
931 /**
932 * ci_hdrc_query_available_role: get runtime available operation mode
933 *
934 * The glue layer can get current operation mode (host/peripheral/otg)
935 * This function should be called after ci core device has created.
936 *
937 * @pdev: the platform device of ci core.
938 *
939 * Return runtime usb_dr_mode.
940 */
941 enum usb_dr_mode ci_hdrc_query_available_role(struct platform_device *pdev)
942 {
943 struct ci_hdrc *ci = platform_get_drvdata(pdev);
944
945 if (!ci)
946 return USB_DR_MODE_UNKNOWN;
947 if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])
948 return USB_DR_MODE_OTG;
949 else if (ci->roles[CI_ROLE_HOST])
950 return USB_DR_MODE_HOST;
951 else if (ci->roles[CI_ROLE_GADGET])
952 return USB_DR_MODE_PERIPHERAL;
953 else
954 return USB_DR_MODE_UNKNOWN;
955 }
956 EXPORT_SYMBOL_GPL(ci_hdrc_query_available_role);
957
958 static inline void ci_role_destroy(struct ci_hdrc *ci)
959 {
960 ci_hdrc_gadget_destroy(ci);
961 ci_hdrc_host_destroy(ci);
962 if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
963 ci_hdrc_otg_destroy(ci);
964 }
965
966 static void ci_get_otg_capable(struct ci_hdrc *ci)
967 {
968 if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
969 ci->is_otg = false;
970 else
971 ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
972 DCCPARAMS_DC | DCCPARAMS_HC)
973 == (DCCPARAMS_DC | DCCPARAMS_HC));
974 if (ci->is_otg) {
975 dev_dbg(ci->dev, "It is OTG capable controller\n");
976 /* Disable and clear all OTG irq */
977 hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
978 OTGSC_INT_STATUS_BITS);
979 }
980 }
981
982 static ssize_t role_show(struct device *dev, struct device_attribute *attr,
983 char *buf)
984 {
985 struct ci_hdrc *ci = dev_get_drvdata(dev);
986
987 if (ci->role != CI_ROLE_END)
988 return sprintf(buf, "%s\n", ci_role(ci)->name);
989
990 return 0;
991 }
992
993 static ssize_t role_store(struct device *dev,
994 struct device_attribute *attr, const char *buf, size_t n)
995 {
996 struct ci_hdrc *ci = dev_get_drvdata(dev);
997 enum ci_role role;
998 int ret;
999
1000 if (!(ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])) {
1001 dev_warn(dev, "Current configuration is not dual-role, quit\n");
1002 return -EPERM;
1003 }
1004
1005 for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
1006 if (!strncmp(buf, ci->roles[role]->name,
1007 strlen(ci->roles[role]->name)))
1008 break;
1009
1010 if (role == CI_ROLE_END)
1011 return -EINVAL;
1012
1013 mutex_lock(&ci->mutex);
1014
1015 if (role == ci->role) {
1016 mutex_unlock(&ci->mutex);
1017 return n;
1018 }
1019
1020 pm_runtime_get_sync(dev);
1021 disable_irq(ci->irq);
1022 ci_role_stop(ci);
1023 ret = ci_role_start(ci, role);
1024 if (!ret && ci->role == CI_ROLE_GADGET)
1025 ci_handle_vbus_change(ci);
1026 enable_irq(ci->irq);
1027 pm_runtime_put_sync(dev);
1028 mutex_unlock(&ci->mutex);
1029
1030 return (ret == 0) ? n : ret;
1031 }
1032 static DEVICE_ATTR_RW(role);
1033
1034 static struct attribute *ci_attrs[] = {
1035 &dev_attr_role.attr,
1036 NULL,
1037 };
1038 ATTRIBUTE_GROUPS(ci);
1039
1040 static int ci_hdrc_probe(struct platform_device *pdev)
1041 {
1042 struct usb_role_switch_desc ci_role_switch = {};
1043 struct device *dev = &pdev->dev;
1044 struct ci_hdrc *ci;
1045 struct resource *res;
1046 void __iomem *base;
1047 int ret;
1048 enum usb_dr_mode dr_mode;
1049
1050 if (!dev_get_platdata(dev)) {
1051 dev_err(dev, "platform data missing\n");
1052 return -ENODEV;
1053 }
1054
1055 base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1056 if (IS_ERR(base))
1057 return PTR_ERR(base);
1058
1059 ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
1060 if (!ci)
1061 return -ENOMEM;
1062
1063 spin_lock_init(&ci->lock);
1064 mutex_init(&ci->mutex);
1065 INIT_WORK(&ci->power_lost_work, ci_power_lost_work);
1066
1067 ci->dev = dev;
1068 ci->platdata = dev_get_platdata(dev);
1069 ci->imx28_write_fix = !!(ci->platdata->flags &
1070 CI_HDRC_IMX28_WRITE_FIX);
1071 ci->supports_runtime_pm = !!(ci->platdata->flags &
1072 CI_HDRC_SUPPORTS_RUNTIME_PM);
1073 ci->has_portsc_pec_bug = !!(ci->platdata->flags &
1074 CI_HDRC_HAS_PORTSC_PEC_MISSED);
1075 ci->has_short_pkt_limit = !!(ci->platdata->flags &
1076 CI_HDRC_HAS_SHORT_PKT_LIMIT);
1077 platform_set_drvdata(pdev, ci);
1078
1079 ret = hw_device_init(ci, base);
1080 if (ret < 0) {
1081 dev_err(dev, "can't initialize hardware\n");
1082 return -ENODEV;
1083 }
1084
1085 ret = ci_ulpi_init(ci);
1086 if (ret)
1087 return ret;
1088
1089 if (ci->platdata->phy) {
1090 ci->phy = ci->platdata->phy;
1091 } else if (ci->platdata->usb_phy) {
1092 ci->usb_phy = ci->platdata->usb_phy;
1093 } else {
1094 /* Look for a generic PHY first */
1095 ci->phy = devm_phy_get(dev->parent, "usb-phy");
1096
1097 if (PTR_ERR(ci->phy) == -EPROBE_DEFER) {
1098 ret = -EPROBE_DEFER;
1099 goto ulpi_exit;
1100 } else if (IS_ERR(ci->phy)) {
1101 ci->phy = NULL;
1102 }
1103
1104 /* Look for a legacy USB PHY from device-tree next */
1105 if (!ci->phy) {
1106 ci->usb_phy = devm_usb_get_phy_by_phandle(dev->parent,
1107 "phys", 0);
1108
1109 if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
1110 ret = -EPROBE_DEFER;
1111 goto ulpi_exit;
1112 } else if (IS_ERR(ci->usb_phy)) {
1113 ci->usb_phy = NULL;
1114 }
1115 }
1116
1117 /* Look for any registered legacy USB PHY as last resort */
1118 if (!ci->phy && !ci->usb_phy) {
1119 ci->usb_phy = devm_usb_get_phy(dev->parent,
1120 USB_PHY_TYPE_USB2);
1121
1122 if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
1123 ret = -EPROBE_DEFER;
1124 goto ulpi_exit;
1125 } else if (IS_ERR(ci->usb_phy)) {
1126 ci->usb_phy = NULL;
1127 }
1128 }
1129
1130 /* No USB PHY was found in the end */
1131 if (!ci->phy && !ci->usb_phy) {
1132 ret = -ENXIO;
1133 goto ulpi_exit;
1134 }
1135 }
1136
1137 ret = ci_usb_phy_init(ci);
1138 if (ret) {
1139 dev_err(dev, "unable to init phy: %d\n", ret);
1140 goto ulpi_exit;
1141 }
1142
1143 ci->hw_bank.phys = res->start;
1144
1145 ci->irq = platform_get_irq(pdev, 0);
1146 if (ci->irq < 0) {
1147 ret = ci->irq;
1148 goto deinit_phy;
1149 }
1150
1151 ci_get_otg_capable(ci);
1152
1153 dr_mode = ci->platdata->dr_mode;
1154 /* initialize role(s) before the interrupt is requested */
1155 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
1156 ret = ci_hdrc_host_init(ci);
1157 if (ret) {
1158 if (ret == -ENXIO)
1159 dev_info(dev, "doesn't support host\n");
1160 else
1161 goto deinit_phy;
1162 }
1163 }
1164
1165 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
1166 ret = ci_hdrc_gadget_init(ci);
1167 if (ret) {
1168 if (ret == -ENXIO)
1169 dev_info(dev, "doesn't support gadget\n");
1170 else
1171 goto deinit_host;
1172 }
1173 }
1174
1175 if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
1176 dev_err(dev, "no supported roles\n");
1177 ret = -ENODEV;
1178 goto deinit_gadget;
1179 }
1180
1181 if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
1182 ret = ci_hdrc_otg_init(ci);
1183 if (ret) {
1184 dev_err(dev, "init otg fails, ret = %d\n", ret);
1185 goto deinit_gadget;
1186 }
1187 }
1188
1189 if (device_property_read_bool(dev, "usb-role-switch")) {
1190 ci_role_switch.set = ci_usb_role_switch_set;
1191 ci_role_switch.get = ci_usb_role_switch_get;
1192 ci_role_switch.allow_userspace_control = true;
1193 ci_role_switch.fwnode = dev_fwnode(dev);
1194 ci_role_switch.driver_data = ci;
1195 ci->role_switch = usb_role_switch_register(dev,
1196 &ci_role_switch);
1197 if (IS_ERR(ci->role_switch)) {
1198 ret = PTR_ERR(ci->role_switch);
1199 goto deinit_otg;
1200 }
1201 }
1202
1203 ci->role = ci_get_role(ci);
1204 if (!ci_otg_is_fsm_mode(ci)) {
1205 /* only update vbus status for peripheral */
1206 if (ci->role == CI_ROLE_GADGET) {
1207 /* Pull down DP for possible charger detection */
1208 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1209 ci_handle_vbus_change(ci);
1210 }
1211
1212 ret = ci_role_start(ci, ci->role);
1213 if (ret) {
1214 dev_err(dev, "can't start %s role\n",
1215 ci_role(ci)->name);
1216 goto stop;
1217 }
1218 }
1219
1220 ret = devm_request_irq(dev, ci->irq, ci_irq_handler, IRQF_SHARED,
1221 ci->platdata->name, ci);
1222 if (ret)
1223 goto stop;
1224
1225 ret = ci_extcon_register(ci);
1226 if (ret)
1227 goto stop;
1228
1229 if (ci->supports_runtime_pm) {
1230 pm_runtime_set_active(&pdev->dev);
1231 pm_runtime_enable(&pdev->dev);
1232 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
1233 pm_runtime_mark_last_busy(ci->dev);
1234 pm_runtime_use_autosuspend(&pdev->dev);
1235 }
1236
1237 if (ci_otg_is_fsm_mode(ci))
1238 ci_hdrc_otg_fsm_start(ci);
1239
1240 device_set_wakeup_capable(&pdev->dev, true);
1241 dbg_create_files(ci);
1242
1243 return 0;
1244
1245 stop:
1246 if (ci->role_switch)
1247 usb_role_switch_unregister(ci->role_switch);
1248 deinit_otg:
1249 if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
1250 ci_hdrc_otg_destroy(ci);
1251 deinit_gadget:
1252 ci_hdrc_gadget_destroy(ci);
1253 deinit_host:
1254 ci_hdrc_host_destroy(ci);
1255 deinit_phy:
1256 ci_usb_phy_exit(ci);
1257 ulpi_exit:
1258 ci_ulpi_exit(ci);
1259
1260 return ret;
1261 }
1262
1263 static void ci_hdrc_remove(struct platform_device *pdev)
1264 {
1265 struct ci_hdrc *ci = platform_get_drvdata(pdev);
1266
1267 if (ci->role_switch)
1268 usb_role_switch_unregister(ci->role_switch);
1269
1270 if (ci->supports_runtime_pm) {
1271 pm_runtime_get_sync(&pdev->dev);
1272 pm_runtime_disable(&pdev->dev);
1273 pm_runtime_put_noidle(&pdev->dev);
1274 }
1275
1276 dbg_remove_files(ci);
1277 ci_role_destroy(ci);
1278 ci_hdrc_enter_lpm(ci, true);
1279 ci_usb_phy_exit(ci);
1280 ci_ulpi_exit(ci);
1281 }
1282
1283 #ifdef CONFIG_PM
1284 /* Prepare wakeup by SRP before suspend */
1285 static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci)
1286 {
1287 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1288 !hw_read_otgsc(ci, OTGSC_ID)) {
1289 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
1290 PORTSC_PP);
1291 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN,
1292 PORTSC_WKCN);
1293 }
1294 }
1295
1296 /* Handle SRP when wakeup by data pulse */
1297 static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci)
1298 {
1299 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1300 (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) {
1301 if (!hw_read_otgsc(ci, OTGSC_ID)) {
1302 ci->fsm.a_srp_det = 1;
1303 ci->fsm.a_bus_drop = 0;
1304 } else {
1305 ci->fsm.id = 1;
1306 }
1307 ci_otg_queue_work(ci);
1308 }
1309 }
1310
1311 static void ci_controller_suspend(struct ci_hdrc *ci)
1312 {
1313 disable_irq(ci->irq);
1314 ci_hdrc_enter_lpm(ci, true);
1315 if (ci->platdata->phy_clkgate_delay_us)
1316 usleep_range(ci->platdata->phy_clkgate_delay_us,
1317 ci->platdata->phy_clkgate_delay_us + 50);
1318 usb_phy_set_suspend(ci->usb_phy, 1);
1319 ci->in_lpm = true;
1320 enable_irq(ci->irq);
1321 }
1322
1323 /*
1324 * Handle the wakeup interrupt triggered by extcon connector
1325 * We need to call ci_irq again for extcon since the first
1326 * interrupt (wakeup int) only let the controller be out of
1327 * low power mode, but not handle any interrupts.
1328 */
1329 static void ci_extcon_wakeup_int(struct ci_hdrc *ci)
1330 {
1331 struct ci_hdrc_cable *cable_id, *cable_vbus;
1332 u32 otgsc = hw_read_otgsc(ci, ~0);
1333
1334 cable_id = &ci->platdata->id_extcon;
1335 cable_vbus = &ci->platdata->vbus_extcon;
1336
1337 if ((!IS_ERR(cable_id->edev) || ci->role_switch)
1338 && ci->is_otg &&
1339 (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS))
1340 ci_irq(ci);
1341
1342 if ((!IS_ERR(cable_vbus->edev) || ci->role_switch)
1343 && ci->is_otg &&
1344 (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS))
1345 ci_irq(ci);
1346 }
1347
1348 static int ci_controller_resume(struct device *dev)
1349 {
1350 struct ci_hdrc *ci = dev_get_drvdata(dev);
1351 int ret;
1352
1353 dev_dbg(dev, "at %s\n", __func__);
1354
1355 if (!ci->in_lpm) {
1356 WARN_ON(1);
1357 return 0;
1358 }
1359
1360 ci_hdrc_enter_lpm(ci, false);
1361
1362 ret = ci_ulpi_resume(ci);
1363 if (ret)
1364 return ret;
1365
1366 if (ci->usb_phy) {
1367 usb_phy_set_suspend(ci->usb_phy, 0);
1368 usb_phy_set_wakeup(ci->usb_phy, false);
1369 hw_wait_phy_stable();
1370 }
1371
1372 ci->in_lpm = false;
1373 if (ci->wakeup_int) {
1374 ci->wakeup_int = false;
1375 pm_runtime_put_autosuspend(ci->dev);
1376 enable_irq(ci->irq);
1377 if (ci_otg_is_fsm_mode(ci))
1378 ci_otg_fsm_wakeup_by_srp(ci);
1379 ci_extcon_wakeup_int(ci);
1380 }
1381
1382 return 0;
1383 }
1384
1385 #ifdef CONFIG_PM_SLEEP
1386 static int ci_suspend(struct device *dev)
1387 {
1388 struct ci_hdrc *ci = dev_get_drvdata(dev);
1389
1390 if (ci->wq)
1391 flush_workqueue(ci->wq);
1392 /*
1393 * Controller needs to be active during suspend, otherwise the core
1394 * may run resume when the parent is at suspend if other driver's
1395 * suspend fails, it occurs before parent's suspend has not started,
1396 * but the core suspend has finished.
1397 */
1398 if (ci->in_lpm)
1399 pm_runtime_resume(dev);
1400
1401 if (ci->in_lpm) {
1402 WARN_ON(1);
1403 return 0;
1404 }
1405
1406 /* Extra routine per role before system suspend */
1407 if (ci->role != CI_ROLE_END && ci_role(ci)->suspend)
1408 ci_role(ci)->suspend(ci);
1409
1410 if (device_may_wakeup(dev)) {
1411 if (ci_otg_is_fsm_mode(ci))
1412 ci_otg_fsm_suspend_for_srp(ci);
1413
1414 usb_phy_set_wakeup(ci->usb_phy, true);
1415 enable_irq_wake(ci->irq);
1416 }
1417
1418 ci_controller_suspend(ci);
1419
1420 return 0;
1421 }
1422
1423 static int ci_resume(struct device *dev)
1424 {
1425 struct ci_hdrc *ci = dev_get_drvdata(dev);
1426 bool power_lost;
1427 int ret;
1428
1429 /* Since ASYNCLISTADDR (host mode) and ENDPTLISTADDR (device
1430 * mode) share the same register address. We can check if
1431 * controller resume from power lost based on this address
1432 * due to this register will be reset after power lost.
1433 */
1434 power_lost = !hw_read(ci, OP_ENDPTLISTADDR, ~0);
1435
1436 if (device_may_wakeup(dev))
1437 disable_irq_wake(ci->irq);
1438
1439 ret = ci_controller_resume(dev);
1440 if (ret)
1441 return ret;
1442
1443 if (power_lost) {
1444 /* shutdown and re-init for phy */
1445 ci_usb_phy_exit(ci);
1446 ci_usb_phy_init(ci);
1447 }
1448
1449 /* Extra routine per role after system resume */
1450 if (ci->role != CI_ROLE_END && ci_role(ci)->resume)
1451 ci_role(ci)->resume(ci, power_lost);
1452
1453 if (power_lost)
1454 queue_work(system_freezable_wq, &ci->power_lost_work);
1455
1456 if (ci->supports_runtime_pm) {
1457 pm_runtime_disable(dev);
1458 pm_runtime_set_active(dev);
1459 pm_runtime_enable(dev);
1460 }
1461
1462 return ret;
1463 }
1464 #endif /* CONFIG_PM_SLEEP */
1465
1466 static int ci_runtime_suspend(struct device *dev)
1467 {
1468 struct ci_hdrc *ci = dev_get_drvdata(dev);
1469
1470 dev_dbg(dev, "at %s\n", __func__);
1471
1472 if (ci->in_lpm) {
1473 WARN_ON(1);
1474 return 0;
1475 }
1476
1477 if (ci_otg_is_fsm_mode(ci))
1478 ci_otg_fsm_suspend_for_srp(ci);
1479
1480 usb_phy_set_wakeup(ci->usb_phy, true);
1481 ci_controller_suspend(ci);
1482
1483 return 0;
1484 }
1485
1486 static int ci_runtime_resume(struct device *dev)
1487 {
1488 return ci_controller_resume(dev);
1489 }
1490
1491 #endif /* CONFIG_PM */
1492 static const struct dev_pm_ops ci_pm_ops = {
1493 SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
1494 SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL)
1495 };
1496
1497 static struct platform_driver ci_hdrc_driver = {
1498 .probe = ci_hdrc_probe,
1499 .remove = ci_hdrc_remove,
1500 .driver = {
1501 .name = "ci_hdrc",
1502 .pm = &ci_pm_ops,
1503 .dev_groups = ci_groups,
1504 },
1505 };
1506
1507 static int __init ci_hdrc_platform_register(void)
1508 {
1509 ci_hdrc_host_driver_init();
1510 return platform_driver_register(&ci_hdrc_driver);
1511 }
1512 module_init(ci_hdrc_platform_register);
1513
1514 static void __exit ci_hdrc_platform_unregister(void)
1515 {
1516 platform_driver_unregister(&ci_hdrc_driver);
1517 }
1518 module_exit(ci_hdrc_platform_unregister);
1519
1520 MODULE_ALIAS("platform:ci_hdrc");
1521 MODULE_LICENSE("GPL v2");
1522 MODULE_AUTHOR("David Lopo <[email protected]>");
1523 MODULE_DESCRIPTION("ChipIdea HDRC Driver");