Debian Codegraph @

drivers/usb/chipidea/core.c

raw symbols SCIP
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 struct usb_role_switch_desc ci_role_switch = {
674 .set = ci_usb_role_switch_set,
675 .get = ci_usb_role_switch_get,
676 .allow_userspace_control = true,
677 };
678
679 static int ci_get_platdata(struct device *dev,
680 struct ci_hdrc_platform_data *platdata)
681 {
682 struct extcon_dev *ext_vbus, *ext_id;
683 struct ci_hdrc_cable *cable;
684 int ret;
685
686 if (!platdata->phy_mode)
687 platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
688
689 if (!platdata->dr_mode)
690 platdata->dr_mode = usb_get_dr_mode(dev);
691
692 if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
693 platdata->dr_mode = USB_DR_MODE_OTG;
694
695 if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
696 /* Get the vbus regulator */
697 platdata->reg_vbus = devm_regulator_get_optional(dev, "vbus");
698 if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
699 return -EPROBE_DEFER;
700 } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
701 /* no vbus regulator is needed */
702 platdata->reg_vbus = NULL;
703 } else if (IS_ERR(platdata->reg_vbus)) {
704 dev_err(dev, "Getting regulator error: %ld\n",
705 PTR_ERR(platdata->reg_vbus));
706 return PTR_ERR(platdata->reg_vbus);
707 }
708 /* Get TPL support */
709 if (!platdata->tpl_support)
710 platdata->tpl_support =
711 of_usb_host_tpl_support(dev->of_node);
712 }
713
714 if (platdata->dr_mode == USB_DR_MODE_OTG) {
715 /* We can support HNP and SRP of OTG 2.0 */
716 platdata->ci_otg_caps.otg_rev = 0x0200;
717 platdata->ci_otg_caps.hnp_support = true;
718 platdata->ci_otg_caps.srp_support = true;
719
720 /* Update otg capabilities by DT properties */
721 ret = of_usb_update_otg_caps(dev->of_node,
722 &platdata->ci_otg_caps);
723 if (ret)
724 return ret;
725 }
726
727 if (usb_get_maximum_speed(dev) == USB_SPEED_FULL)
728 platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
729
730 of_property_read_u32(dev->of_node, "phy-clkgate-delay-us",
731 &platdata->phy_clkgate_delay_us);
732
733 platdata->itc_setting = 1;
734
735 of_property_read_u32(dev->of_node, "itc-setting",
736 &platdata->itc_setting);
737
738 ret = of_property_read_u32(dev->of_node, "ahb-burst-config",
739 &platdata->ahb_burst_config);
740 if (!ret) {
741 platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST;
742 } else if (ret != -EINVAL) {
743 dev_err(dev, "failed to get ahb-burst-config\n");
744 return ret;
745 }
746
747 ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword",
748 &platdata->tx_burst_size);
749 if (!ret) {
750 platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST;
751 } else if (ret != -EINVAL) {
752 dev_err(dev, "failed to get tx-burst-size-dword\n");
753 return ret;
754 }
755
756 ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword",
757 &platdata->rx_burst_size);
758 if (!ret) {
759 platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST;
760 } else if (ret != -EINVAL) {
761 dev_err(dev, "failed to get rx-burst-size-dword\n");
762 return ret;
763 }
764
765 if (of_property_read_bool(dev->of_node, "non-zero-ttctrl-ttha"))
766 platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
767
768 ext_id = ERR_PTR(-ENODEV);
769 ext_vbus = ERR_PTR(-ENODEV);
770 if (of_property_present(dev->of_node, "extcon")) {
771 /* Each one of them is not mandatory */
772 ext_vbus = extcon_get_edev_by_phandle(dev, 0);
773 if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
774 return PTR_ERR(ext_vbus);
775
776 ext_id = extcon_get_edev_by_phandle(dev, 1);
777 if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
778 return PTR_ERR(ext_id);
779 }
780
781 cable = &platdata->vbus_extcon;
782 cable->nb.notifier_call = ci_cable_notifier;
783 cable->edev = ext_vbus;
784
785 if (!IS_ERR(ext_vbus)) {
786 ret = extcon_get_state(cable->edev, EXTCON_USB);
787 if (ret)
788 cable->connected = true;
789 else
790 cable->connected = false;
791 }
792
793 cable = &platdata->id_extcon;
794 cable->nb.notifier_call = ci_cable_notifier;
795 cable->edev = ext_id;
796
797 if (!IS_ERR(ext_id)) {
798 ret = extcon_get_state(cable->edev, EXTCON_USB_HOST);
799 if (ret)
800 cable->connected = true;
801 else
802 cable->connected = false;
803 }
804
805 if (device_property_read_bool(dev, "usb-role-switch"))
806 ci_role_switch.fwnode = dev->fwnode;
807
808 platdata->pctl = devm_pinctrl_get(dev);
809 if (!IS_ERR(platdata->pctl)) {
810 struct pinctrl_state *p;
811
812 p = pinctrl_lookup_state(platdata->pctl, "default");
813 if (!IS_ERR(p))
814 platdata->pins_default = p;
815
816 p = pinctrl_lookup_state(platdata->pctl, "host");
817 if (!IS_ERR(p))
818 platdata->pins_host = p;
819
820 p = pinctrl_lookup_state(platdata->pctl, "device");
821 if (!IS_ERR(p))
822 platdata->pins_device = p;
823 }
824
825 if (!platdata->enter_lpm)
826 platdata->enter_lpm = ci_hdrc_enter_lpm_common;
827
828 return 0;
829 }
830
831 static int ci_extcon_register(struct ci_hdrc *ci)
832 {
833 struct ci_hdrc_cable *id, *vbus;
834 int ret;
835
836 id = &ci->platdata->id_extcon;
837 id->ci = ci;
838 if (!IS_ERR_OR_NULL(id->edev)) {
839 ret = devm_extcon_register_notifier(ci->dev, id->edev,
840 EXTCON_USB_HOST, &id->nb);
841 if (ret < 0) {
842 dev_err(ci->dev, "register ID failed\n");
843 return ret;
844 }
845 }
846
847 vbus = &ci->platdata->vbus_extcon;
848 vbus->ci = ci;
849 if (!IS_ERR_OR_NULL(vbus->edev)) {
850 ret = devm_extcon_register_notifier(ci->dev, vbus->edev,
851 EXTCON_USB, &vbus->nb);
852 if (ret < 0) {
853 dev_err(ci->dev, "register VBUS failed\n");
854 return ret;
855 }
856 }
857
858 return 0;
859 }
860
861 static void ci_power_lost_work(struct work_struct *work)
862 {
863 struct ci_hdrc *ci = container_of(work, struct ci_hdrc, power_lost_work);
864 enum ci_role role;
865
866 disable_irq_nosync(ci->irq);
867 pm_runtime_get_sync(ci->dev);
868 if (!ci_otg_is_fsm_mode(ci)) {
869 role = ci_get_role(ci);
870
871 if (ci->role != role) {
872 ci_handle_id_switch(ci);
873 } else if (role == CI_ROLE_GADGET) {
874 if (ci->is_otg && hw_read_otgsc(ci, OTGSC_BSV))
875 usb_gadget_vbus_connect(&ci->gadget);
876 }
877 }
878 pm_runtime_put_sync(ci->dev);
879 enable_irq(ci->irq);
880 }
881
882 static DEFINE_IDA(ci_ida);
883
884 struct platform_device *ci_hdrc_add_device(struct device *dev,
885 struct resource *res, int nres,
886 struct ci_hdrc_platform_data *platdata)
887 {
888 struct platform_device *pdev;
889 int id, ret;
890
891 ret = ci_get_platdata(dev, platdata);
892 if (ret)
893 return ERR_PTR(ret);
894
895 id = ida_alloc(&ci_ida, GFP_KERNEL);
896 if (id < 0)
897 return ERR_PTR(id);
898
899 pdev = platform_device_alloc("ci_hdrc", id);
900 if (!pdev) {
901 ret = -ENOMEM;
902 goto put_id;
903 }
904
905 pdev->dev.parent = dev;
906 device_set_of_node_from_dev(&pdev->dev, dev);
907
908 ret = platform_device_add_resources(pdev, res, nres);
909 if (ret)
910 goto err;
911
912 ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
913 if (ret)
914 goto err;
915
916 ret = platform_device_add(pdev);
917 if (ret)
918 goto err;
919
920 dev_pm_domain_detach(&pdev->dev, false);
921
922 return pdev;
923
924 err:
925 platform_device_put(pdev);
926 put_id:
927 ida_free(&ci_ida, id);
928 return ERR_PTR(ret);
929 }
930 EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
931
932 void ci_hdrc_remove_device(struct platform_device *pdev)
933 {
934 int id = pdev->id;
935 platform_device_unregister(pdev);
936 ida_free(&ci_ida, id);
937 }
938 EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
939
940 /**
941 * ci_hdrc_query_available_role: get runtime available operation mode
942 *
943 * The glue layer can get current operation mode (host/peripheral/otg)
944 * This function should be called after ci core device has created.
945 *
946 * @pdev: the platform device of ci core.
947 *
948 * Return runtime usb_dr_mode.
949 */
950 enum usb_dr_mode ci_hdrc_query_available_role(struct platform_device *pdev)
951 {
952 struct ci_hdrc *ci = platform_get_drvdata(pdev);
953
954 if (!ci)
955 return USB_DR_MODE_UNKNOWN;
956 if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])
957 return USB_DR_MODE_OTG;
958 else if (ci->roles[CI_ROLE_HOST])
959 return USB_DR_MODE_HOST;
960 else if (ci->roles[CI_ROLE_GADGET])
961 return USB_DR_MODE_PERIPHERAL;
962 else
963 return USB_DR_MODE_UNKNOWN;
964 }
965 EXPORT_SYMBOL_GPL(ci_hdrc_query_available_role);
966
967 static inline void ci_role_destroy(struct ci_hdrc *ci)
968 {
969 ci_hdrc_gadget_destroy(ci);
970 ci_hdrc_host_destroy(ci);
971 if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
972 ci_hdrc_otg_destroy(ci);
973 }
974
975 static void ci_get_otg_capable(struct ci_hdrc *ci)
976 {
977 if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
978 ci->is_otg = false;
979 else
980 ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
981 DCCPARAMS_DC | DCCPARAMS_HC)
982 == (DCCPARAMS_DC | DCCPARAMS_HC));
983 if (ci->is_otg) {
984 dev_dbg(ci->dev, "It is OTG capable controller\n");
985 /* Disable and clear all OTG irq */
986 hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
987 OTGSC_INT_STATUS_BITS);
988 }
989 }
990
991 static ssize_t role_show(struct device *dev, struct device_attribute *attr,
992 char *buf)
993 {
994 struct ci_hdrc *ci = dev_get_drvdata(dev);
995
996 if (ci->role != CI_ROLE_END)
997 return sprintf(buf, "%s\n", ci_role(ci)->name);
998
999 return 0;
1000 }
1001
1002 static ssize_t role_store(struct device *dev,
1003 struct device_attribute *attr, const char *buf, size_t n)
1004 {
1005 struct ci_hdrc *ci = dev_get_drvdata(dev);
1006 enum ci_role role;
1007 int ret;
1008
1009 if (!(ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])) {
1010 dev_warn(dev, "Current configuration is not dual-role, quit\n");
1011 return -EPERM;
1012 }
1013
1014 for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
1015 if (!strncmp(buf, ci->roles[role]->name,
1016 strlen(ci->roles[role]->name)))
1017 break;
1018
1019 if (role == CI_ROLE_END)
1020 return -EINVAL;
1021
1022 mutex_lock(&ci->mutex);
1023
1024 if (role == ci->role) {
1025 mutex_unlock(&ci->mutex);
1026 return n;
1027 }
1028
1029 pm_runtime_get_sync(dev);
1030 disable_irq(ci->irq);
1031 ci_role_stop(ci);
1032 ret = ci_role_start(ci, role);
1033 if (!ret && ci->role == CI_ROLE_GADGET)
1034 ci_handle_vbus_change(ci);
1035 enable_irq(ci->irq);
1036 pm_runtime_put_sync(dev);
1037 mutex_unlock(&ci->mutex);
1038
1039 return (ret == 0) ? n : ret;
1040 }
1041 static DEVICE_ATTR_RW(role);
1042
1043 static struct attribute *ci_attrs[] = {
1044 &dev_attr_role.attr,
1045 NULL,
1046 };
1047 ATTRIBUTE_GROUPS(ci);
1048
1049 static int ci_hdrc_probe(struct platform_device *pdev)
1050 {
1051 struct device *dev = &pdev->dev;
1052 struct ci_hdrc *ci;
1053 struct resource *res;
1054 void __iomem *base;
1055 int ret;
1056 enum usb_dr_mode dr_mode;
1057
1058 if (!dev_get_platdata(dev)) {
1059 dev_err(dev, "platform data missing\n");
1060 return -ENODEV;
1061 }
1062
1063 base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1064 if (IS_ERR(base))
1065 return PTR_ERR(base);
1066
1067 ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
1068 if (!ci)
1069 return -ENOMEM;
1070
1071 spin_lock_init(&ci->lock);
1072 mutex_init(&ci->mutex);
1073 INIT_WORK(&ci->power_lost_work, ci_power_lost_work);
1074
1075 ci->dev = dev;
1076 ci->platdata = dev_get_platdata(dev);
1077 ci->imx28_write_fix = !!(ci->platdata->flags &
1078 CI_HDRC_IMX28_WRITE_FIX);
1079 ci->supports_runtime_pm = !!(ci->platdata->flags &
1080 CI_HDRC_SUPPORTS_RUNTIME_PM);
1081 ci->has_portsc_pec_bug = !!(ci->platdata->flags &
1082 CI_HDRC_HAS_PORTSC_PEC_MISSED);
1083 ci->has_short_pkt_limit = !!(ci->platdata->flags &
1084 CI_HDRC_HAS_SHORT_PKT_LIMIT);
1085 platform_set_drvdata(pdev, ci);
1086
1087 ret = hw_device_init(ci, base);
1088 if (ret < 0) {
1089 dev_err(dev, "can't initialize hardware\n");
1090 return -ENODEV;
1091 }
1092
1093 ret = ci_ulpi_init(ci);
1094 if (ret)
1095 return ret;
1096
1097 if (ci->platdata->phy) {
1098 ci->phy = ci->platdata->phy;
1099 } else if (ci->platdata->usb_phy) {
1100 ci->usb_phy = ci->platdata->usb_phy;
1101 } else {
1102 /* Look for a generic PHY first */
1103 ci->phy = devm_phy_get(dev->parent, "usb-phy");
1104
1105 if (PTR_ERR(ci->phy) == -EPROBE_DEFER) {
1106 ret = -EPROBE_DEFER;
1107 goto ulpi_exit;
1108 } else if (IS_ERR(ci->phy)) {
1109 ci->phy = NULL;
1110 }
1111
1112 /* Look for a legacy USB PHY from device-tree next */
1113 if (!ci->phy) {
1114 ci->usb_phy = devm_usb_get_phy_by_phandle(dev->parent,
1115 "phys", 0);
1116
1117 if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
1118 ret = -EPROBE_DEFER;
1119 goto ulpi_exit;
1120 } else if (IS_ERR(ci->usb_phy)) {
1121 ci->usb_phy = NULL;
1122 }
1123 }
1124
1125 /* Look for any registered legacy USB PHY as last resort */
1126 if (!ci->phy && !ci->usb_phy) {
1127 ci->usb_phy = devm_usb_get_phy(dev->parent,
1128 USB_PHY_TYPE_USB2);
1129
1130 if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
1131 ret = -EPROBE_DEFER;
1132 goto ulpi_exit;
1133 } else if (IS_ERR(ci->usb_phy)) {
1134 ci->usb_phy = NULL;
1135 }
1136 }
1137
1138 /* No USB PHY was found in the end */
1139 if (!ci->phy && !ci->usb_phy) {
1140 ret = -ENXIO;
1141 goto ulpi_exit;
1142 }
1143 }
1144
1145 ret = ci_usb_phy_init(ci);
1146 if (ret) {
1147 dev_err(dev, "unable to init phy: %d\n", ret);
1148 goto ulpi_exit;
1149 }
1150
1151 ci->hw_bank.phys = res->start;
1152
1153 ci->irq = platform_get_irq(pdev, 0);
1154 if (ci->irq < 0) {
1155 ret = ci->irq;
1156 goto deinit_phy;
1157 }
1158
1159 ci_get_otg_capable(ci);
1160
1161 dr_mode = ci->platdata->dr_mode;
1162 /* initialize role(s) before the interrupt is requested */
1163 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
1164 ret = ci_hdrc_host_init(ci);
1165 if (ret) {
1166 if (ret == -ENXIO)
1167 dev_info(dev, "doesn't support host\n");
1168 else
1169 goto deinit_phy;
1170 }
1171 }
1172
1173 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
1174 ret = ci_hdrc_gadget_init(ci);
1175 if (ret) {
1176 if (ret == -ENXIO)
1177 dev_info(dev, "doesn't support gadget\n");
1178 else
1179 goto deinit_host;
1180 }
1181 }
1182
1183 if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
1184 dev_err(dev, "no supported roles\n");
1185 ret = -ENODEV;
1186 goto deinit_gadget;
1187 }
1188
1189 if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
1190 ret = ci_hdrc_otg_init(ci);
1191 if (ret) {
1192 dev_err(dev, "init otg fails, ret = %d\n", ret);
1193 goto deinit_gadget;
1194 }
1195 }
1196
1197 if (ci_role_switch.fwnode) {
1198 ci_role_switch.driver_data = ci;
1199 ci->role_switch = usb_role_switch_register(dev,
1200 &ci_role_switch);
1201 if (IS_ERR(ci->role_switch)) {
1202 ret = PTR_ERR(ci->role_switch);
1203 goto deinit_otg;
1204 }
1205 }
1206
1207 ci->role = ci_get_role(ci);
1208 if (!ci_otg_is_fsm_mode(ci)) {
1209 /* only update vbus status for peripheral */
1210 if (ci->role == CI_ROLE_GADGET) {
1211 /* Pull down DP for possible charger detection */
1212 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1213 ci_handle_vbus_change(ci);
1214 }
1215
1216 ret = ci_role_start(ci, ci->role);
1217 if (ret) {
1218 dev_err(dev, "can't start %s role\n",
1219 ci_role(ci)->name);
1220 goto stop;
1221 }
1222 }
1223
1224 ret = devm_request_irq(dev, ci->irq, ci_irq_handler, IRQF_SHARED,
1225 ci->platdata->name, ci);
1226 if (ret)
1227 goto stop;
1228
1229 ret = ci_extcon_register(ci);
1230 if (ret)
1231 goto stop;
1232
1233 if (ci->supports_runtime_pm) {
1234 pm_runtime_set_active(&pdev->dev);
1235 pm_runtime_enable(&pdev->dev);
1236 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
1237 pm_runtime_mark_last_busy(ci->dev);
1238 pm_runtime_use_autosuspend(&pdev->dev);
1239 }
1240
1241 if (ci_otg_is_fsm_mode(ci))
1242 ci_hdrc_otg_fsm_start(ci);
1243
1244 device_set_wakeup_capable(&pdev->dev, true);
1245 dbg_create_files(ci);
1246
1247 return 0;
1248
1249 stop:
1250 if (ci->role_switch)
1251 usb_role_switch_unregister(ci->role_switch);
1252 deinit_otg:
1253 if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
1254 ci_hdrc_otg_destroy(ci);
1255 deinit_gadget:
1256 ci_hdrc_gadget_destroy(ci);
1257 deinit_host:
1258 ci_hdrc_host_destroy(ci);
1259 deinit_phy:
1260 ci_usb_phy_exit(ci);
1261 ulpi_exit:
1262 ci_ulpi_exit(ci);
1263
1264 return ret;
1265 }
1266
1267 static void ci_hdrc_remove(struct platform_device *pdev)
1268 {
1269 struct ci_hdrc *ci = platform_get_drvdata(pdev);
1270
1271 if (ci->role_switch)
1272 usb_role_switch_unregister(ci->role_switch);
1273
1274 if (ci->supports_runtime_pm) {
1275 pm_runtime_get_sync(&pdev->dev);
1276 pm_runtime_disable(&pdev->dev);
1277 pm_runtime_put_noidle(&pdev->dev);
1278 }
1279
1280 dbg_remove_files(ci);
1281 ci_role_destroy(ci);
1282 ci_hdrc_enter_lpm(ci, true);
1283 ci_usb_phy_exit(ci);
1284 ci_ulpi_exit(ci);
1285 }
1286
1287 #ifdef CONFIG_PM
1288 /* Prepare wakeup by SRP before suspend */
1289 static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci)
1290 {
1291 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1292 !hw_read_otgsc(ci, OTGSC_ID)) {
1293 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
1294 PORTSC_PP);
1295 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN,
1296 PORTSC_WKCN);
1297 }
1298 }
1299
1300 /* Handle SRP when wakeup by data pulse */
1301 static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci)
1302 {
1303 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1304 (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) {
1305 if (!hw_read_otgsc(ci, OTGSC_ID)) {
1306 ci->fsm.a_srp_det = 1;
1307 ci->fsm.a_bus_drop = 0;
1308 } else {
1309 ci->fsm.id = 1;
1310 }
1311 ci_otg_queue_work(ci);
1312 }
1313 }
1314
1315 static void ci_controller_suspend(struct ci_hdrc *ci)
1316 {
1317 disable_irq(ci->irq);
1318 ci_hdrc_enter_lpm(ci, true);
1319 if (ci->platdata->phy_clkgate_delay_us)
1320 usleep_range(ci->platdata->phy_clkgate_delay_us,
1321 ci->platdata->phy_clkgate_delay_us + 50);
1322 usb_phy_set_suspend(ci->usb_phy, 1);
1323 ci->in_lpm = true;
1324 enable_irq(ci->irq);
1325 }
1326
1327 /*
1328 * Handle the wakeup interrupt triggered by extcon connector
1329 * We need to call ci_irq again for extcon since the first
1330 * interrupt (wakeup int) only let the controller be out of
1331 * low power mode, but not handle any interrupts.
1332 */
1333 static void ci_extcon_wakeup_int(struct ci_hdrc *ci)
1334 {
1335 struct ci_hdrc_cable *cable_id, *cable_vbus;
1336 u32 otgsc = hw_read_otgsc(ci, ~0);
1337
1338 cable_id = &ci->platdata->id_extcon;
1339 cable_vbus = &ci->platdata->vbus_extcon;
1340
1341 if ((!IS_ERR(cable_id->edev) || ci->role_switch)
1342 && ci->is_otg &&
1343 (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS))
1344 ci_irq(ci);
1345
1346 if ((!IS_ERR(cable_vbus->edev) || ci->role_switch)
1347 && ci->is_otg &&
1348 (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS))
1349 ci_irq(ci);
1350 }
1351
1352 static int ci_controller_resume(struct device *dev)
1353 {
1354 struct ci_hdrc *ci = dev_get_drvdata(dev);
1355 int ret;
1356
1357 dev_dbg(dev, "at %s\n", __func__);
1358
1359 if (!ci->in_lpm) {
1360 WARN_ON(1);
1361 return 0;
1362 }
1363
1364 ci_hdrc_enter_lpm(ci, false);
1365
1366 ret = ci_ulpi_resume(ci);
1367 if (ret)
1368 return ret;
1369
1370 if (ci->usb_phy) {
1371 usb_phy_set_suspend(ci->usb_phy, 0);
1372 usb_phy_set_wakeup(ci->usb_phy, false);
1373 hw_wait_phy_stable();
1374 }
1375
1376 ci->in_lpm = false;
1377 if (ci->wakeup_int) {
1378 ci->wakeup_int = false;
1379 pm_runtime_put_autosuspend(ci->dev);
1380 enable_irq(ci->irq);
1381 if (ci_otg_is_fsm_mode(ci))
1382 ci_otg_fsm_wakeup_by_srp(ci);
1383 ci_extcon_wakeup_int(ci);
1384 }
1385
1386 return 0;
1387 }
1388
1389 #ifdef CONFIG_PM_SLEEP
1390 static int ci_suspend(struct device *dev)
1391 {
1392 struct ci_hdrc *ci = dev_get_drvdata(dev);
1393
1394 if (ci->wq)
1395 flush_workqueue(ci->wq);
1396 /*
1397 * Controller needs to be active during suspend, otherwise the core
1398 * may run resume when the parent is at suspend if other driver's
1399 * suspend fails, it occurs before parent's suspend has not started,
1400 * but the core suspend has finished.
1401 */
1402 if (ci->in_lpm)
1403 pm_runtime_resume(dev);
1404
1405 if (ci->in_lpm) {
1406 WARN_ON(1);
1407 return 0;
1408 }
1409
1410 /* Extra routine per role before system suspend */
1411 if (ci->role != CI_ROLE_END && ci_role(ci)->suspend)
1412 ci_role(ci)->suspend(ci);
1413
1414 if (device_may_wakeup(dev)) {
1415 if (ci_otg_is_fsm_mode(ci))
1416 ci_otg_fsm_suspend_for_srp(ci);
1417
1418 usb_phy_set_wakeup(ci->usb_phy, true);
1419 enable_irq_wake(ci->irq);
1420 }
1421
1422 ci_controller_suspend(ci);
1423
1424 return 0;
1425 }
1426
1427 static int ci_resume(struct device *dev)
1428 {
1429 struct ci_hdrc *ci = dev_get_drvdata(dev);
1430 bool power_lost;
1431 int ret;
1432
1433 /* Since ASYNCLISTADDR (host mode) and ENDPTLISTADDR (device
1434 * mode) share the same register address. We can check if
1435 * controller resume from power lost based on this address
1436 * due to this register will be reset after power lost.
1437 */
1438 power_lost = !hw_read(ci, OP_ENDPTLISTADDR, ~0);
1439
1440 if (device_may_wakeup(dev))
1441 disable_irq_wake(ci->irq);
1442
1443 ret = ci_controller_resume(dev);
1444 if (ret)
1445 return ret;
1446
1447 if (power_lost) {
1448 /* shutdown and re-init for phy */
1449 ci_usb_phy_exit(ci);
1450 ci_usb_phy_init(ci);
1451 }
1452
1453 /* Extra routine per role after system resume */
1454 if (ci->role != CI_ROLE_END && ci_role(ci)->resume)
1455 ci_role(ci)->resume(ci, power_lost);
1456
1457 if (power_lost)
1458 queue_work(system_freezable_wq, &ci->power_lost_work);
1459
1460 if (ci->supports_runtime_pm) {
1461 pm_runtime_disable(dev);
1462 pm_runtime_set_active(dev);
1463 pm_runtime_enable(dev);
1464 }
1465
1466 return ret;
1467 }
1468 #endif /* CONFIG_PM_SLEEP */
1469
1470 static int ci_runtime_suspend(struct device *dev)
1471 {
1472 struct ci_hdrc *ci = dev_get_drvdata(dev);
1473
1474 dev_dbg(dev, "at %s\n", __func__);
1475
1476 if (ci->in_lpm) {
1477 WARN_ON(1);
1478 return 0;
1479 }
1480
1481 if (ci_otg_is_fsm_mode(ci))
1482 ci_otg_fsm_suspend_for_srp(ci);
1483
1484 usb_phy_set_wakeup(ci->usb_phy, true);
1485 ci_controller_suspend(ci);
1486
1487 return 0;
1488 }
1489
1490 static int ci_runtime_resume(struct device *dev)
1491 {
1492 return ci_controller_resume(dev);
1493 }
1494
1495 #endif /* CONFIG_PM */
1496 static const struct dev_pm_ops ci_pm_ops = {
1497 SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
1498 SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL)
1499 };
1500
1501 static struct platform_driver ci_hdrc_driver = {
1502 .probe = ci_hdrc_probe,
1503 .remove = ci_hdrc_remove,
1504 .driver = {
1505 .name = "ci_hdrc",
1506 .pm = &ci_pm_ops,
1507 .dev_groups = ci_groups,
1508 },
1509 };
1510
1511 static int __init ci_hdrc_platform_register(void)
1512 {
1513 ci_hdrc_host_driver_init();
1514 return platform_driver_register(&ci_hdrc_driver);
1515 }
1516 module_init(ci_hdrc_platform_register);
1517
1518 static void __exit ci_hdrc_platform_unregister(void)
1519 {
1520 platform_driver_unregister(&ci_hdrc_driver);
1521 }
1522 module_exit(ci_hdrc_platform_unregister);
1523
1524 MODULE_ALIAS("platform:ci_hdrc");
1525 MODULE_LICENSE("GPL v2");
1526 MODULE_AUTHOR("David Lopo <[email protected]>");
1527 MODULE_DESCRIPTION("ChipIdea HDRC Driver");