GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / usb / chipidea / otg_fsm.c
1 /*
2  * otg_fsm.c - ChipIdea USB IP core OTG FSM driver
3  *
4  * Copyright (C) 2014 Freescale Semiconductor, Inc.
5  *
6  * Author: Jun Li
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*
14  * This file mainly handles OTG fsm, it includes OTG fsm operations
15  * for HNP and SRP.
16  *
17  * TODO List
18  * - ADP
19  * - OTG test device
20  */
21
22 #include <linux/usb/otg.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/usb/chipidea.h>
26 #include <linux/regulator/consumer.h>
27
28 #include "ci.h"
29 #include "bits.h"
30 #include "otg.h"
31 #include "otg_fsm.h"
32
33 /* Add for otg: interact with user space app */
34 static ssize_t
35 get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
36 {
37         char            *next;
38         unsigned        size, t;
39         struct ci_hdrc  *ci = dev_get_drvdata(dev);
40
41         next = buf;
42         size = PAGE_SIZE;
43         t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_req);
44         size -= t;
45         next += t;
46
47         return PAGE_SIZE - size;
48 }
49
50 static ssize_t
51 set_a_bus_req(struct device *dev, struct device_attribute *attr,
52                                         const char *buf, size_t count)
53 {
54         struct ci_hdrc *ci = dev_get_drvdata(dev);
55
56         if (count > 2)
57                 return -1;
58
59         mutex_lock(&ci->fsm.lock);
60         if (buf[0] == '0') {
61                 ci->fsm.a_bus_req = 0;
62         } else if (buf[0] == '1') {
63                 /* If a_bus_drop is TRUE, a_bus_req can't be set */
64                 if (ci->fsm.a_bus_drop) {
65                         mutex_unlock(&ci->fsm.lock);
66                         return count;
67                 }
68                 ci->fsm.a_bus_req = 1;
69                 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
70                         ci->gadget.host_request_flag = 1;
71                         mutex_unlock(&ci->fsm.lock);
72                         return count;
73                 }
74         }
75
76         ci_otg_queue_work(ci);
77         mutex_unlock(&ci->fsm.lock);
78
79         return count;
80 }
81 static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req, set_a_bus_req);
82
83 static ssize_t
84 get_a_bus_drop(struct device *dev, struct device_attribute *attr, char *buf)
85 {
86         char            *next;
87         unsigned        size, t;
88         struct ci_hdrc  *ci = dev_get_drvdata(dev);
89
90         next = buf;
91         size = PAGE_SIZE;
92         t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_drop);
93         size -= t;
94         next += t;
95
96         return PAGE_SIZE - size;
97 }
98
99 static ssize_t
100 set_a_bus_drop(struct device *dev, struct device_attribute *attr,
101                                         const char *buf, size_t count)
102 {
103         struct ci_hdrc  *ci = dev_get_drvdata(dev);
104
105         if (count > 2)
106                 return -1;
107
108         mutex_lock(&ci->fsm.lock);
109         if (buf[0] == '0') {
110                 ci->fsm.a_bus_drop = 0;
111         } else if (buf[0] == '1') {
112                 ci->fsm.a_bus_drop = 1;
113                 ci->fsm.a_bus_req = 0;
114         }
115
116         ci_otg_queue_work(ci);
117         mutex_unlock(&ci->fsm.lock);
118
119         return count;
120 }
121 static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUSR, get_a_bus_drop,
122                                                 set_a_bus_drop);
123
124 static ssize_t
125 get_b_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
126 {
127         char            *next;
128         unsigned        size, t;
129         struct ci_hdrc  *ci = dev_get_drvdata(dev);
130
131         next = buf;
132         size = PAGE_SIZE;
133         t = scnprintf(next, size, "%d\n", ci->fsm.b_bus_req);
134         size -= t;
135         next += t;
136
137         return PAGE_SIZE - size;
138 }
139
140 static ssize_t
141 set_b_bus_req(struct device *dev, struct device_attribute *attr,
142                                         const char *buf, size_t count)
143 {
144         struct ci_hdrc  *ci = dev_get_drvdata(dev);
145
146         if (count > 2)
147                 return -1;
148
149         mutex_lock(&ci->fsm.lock);
150         if (buf[0] == '0')
151                 ci->fsm.b_bus_req = 0;
152         else if (buf[0] == '1') {
153                 ci->fsm.b_bus_req = 1;
154                 if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
155                         ci->gadget.host_request_flag = 1;
156                         mutex_unlock(&ci->fsm.lock);
157                         return count;
158                 }
159         }
160
161         ci_otg_queue_work(ci);
162         mutex_unlock(&ci->fsm.lock);
163
164         return count;
165 }
166 static DEVICE_ATTR(b_bus_req, S_IRUGO | S_IWUSR, get_b_bus_req, set_b_bus_req);
167
168 static ssize_t
169 set_a_clr_err(struct device *dev, struct device_attribute *attr,
170                                         const char *buf, size_t count)
171 {
172         struct ci_hdrc  *ci = dev_get_drvdata(dev);
173
174         if (count > 2)
175                 return -1;
176
177         mutex_lock(&ci->fsm.lock);
178         if (buf[0] == '1')
179                 ci->fsm.a_clr_err = 1;
180
181         ci_otg_queue_work(ci);
182         mutex_unlock(&ci->fsm.lock);
183
184         return count;
185 }
186 static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err);
187
188 static struct attribute *inputs_attrs[] = {
189         &dev_attr_a_bus_req.attr,
190         &dev_attr_a_bus_drop.attr,
191         &dev_attr_b_bus_req.attr,
192         &dev_attr_a_clr_err.attr,
193         NULL,
194 };
195
196 static struct attribute_group inputs_attr_group = {
197         .name = "inputs",
198         .attrs = inputs_attrs,
199 };
200
201 /*
202  * Keep this list in the same order as timers indexed
203  * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
204  */
205 static unsigned otg_timer_ms[] = {
206         TA_WAIT_VRISE,
207         TA_WAIT_VFALL,
208         TA_WAIT_BCON,
209         TA_AIDL_BDIS,
210         TB_ASE0_BRST,
211         TA_BIDL_ADIS,
212         TB_AIDL_BDIS,
213         TB_SE0_SRP,
214         TB_SRP_FAIL,
215         0,
216         TB_DATA_PLS,
217         TB_SSEND_SRP,
218 };
219
220 /*
221  * Add timer to active timer list
222  */
223 static void ci_otg_add_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
224 {
225         unsigned long flags, timer_sec, timer_nsec;
226
227         if (t >= NUM_OTG_FSM_TIMERS)
228                 return;
229
230         spin_lock_irqsave(&ci->lock, flags);
231         timer_sec = otg_timer_ms[t] / MSEC_PER_SEC;
232         timer_nsec = (otg_timer_ms[t] % MSEC_PER_SEC) * NSEC_PER_MSEC;
233         ci->hr_timeouts[t] = ktime_add(ktime_get(),
234                                 ktime_set(timer_sec, timer_nsec));
235         ci->enabled_otg_timer_bits |= (1 << t);
236         if ((ci->next_otg_timer == NUM_OTG_FSM_TIMERS) ||
237                         (ci->hr_timeouts[ci->next_otg_timer].tv64 >
238                                                 ci->hr_timeouts[t].tv64)) {
239                         ci->next_otg_timer = t;
240                         hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
241                                         ci->hr_timeouts[t], NSEC_PER_MSEC,
242                                                         HRTIMER_MODE_ABS);
243         }
244         spin_unlock_irqrestore(&ci->lock, flags);
245 }
246
247 /*
248  * Remove timer from active timer list
249  */
250 static void ci_otg_del_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
251 {
252         unsigned long flags, enabled_timer_bits;
253         enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
254
255         if ((t >= NUM_OTG_FSM_TIMERS) ||
256                         !(ci->enabled_otg_timer_bits & (1 << t)))
257                 return;
258
259         spin_lock_irqsave(&ci->lock, flags);
260         ci->enabled_otg_timer_bits &= ~(1 << t);
261         if (ci->next_otg_timer == t) {
262                 if (ci->enabled_otg_timer_bits == 0) {
263                         spin_unlock_irqrestore(&ci->lock, flags);
264                         /* No enabled timers after delete it */
265                         hrtimer_cancel(&ci->otg_fsm_hrtimer);
266                         spin_lock_irqsave(&ci->lock, flags);
267                         ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
268                 } else {
269                         /* Find the next timer */
270                         enabled_timer_bits = ci->enabled_otg_timer_bits;
271                         for_each_set_bit(cur_timer, &enabled_timer_bits,
272                                                         NUM_OTG_FSM_TIMERS) {
273                                 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
274                                         (ci->hr_timeouts[next_timer].tv64 <
275                                         ci->hr_timeouts[cur_timer].tv64))
276                                         next_timer = cur_timer;
277                         }
278                 }
279         }
280         if (next_timer != NUM_OTG_FSM_TIMERS) {
281                 ci->next_otg_timer = next_timer;
282                 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
283                         ci->hr_timeouts[next_timer], NSEC_PER_MSEC,
284                                                         HRTIMER_MODE_ABS);
285         }
286         spin_unlock_irqrestore(&ci->lock, flags);
287 }
288
289 /* OTG FSM timer handlers */
290 static int a_wait_vrise_tmout(struct ci_hdrc *ci)
291 {
292         ci->fsm.a_wait_vrise_tmout = 1;
293         return 0;
294 }
295
296 static int a_wait_vfall_tmout(struct ci_hdrc *ci)
297 {
298         ci->fsm.a_wait_vfall_tmout = 1;
299         return 0;
300 }
301
302 static int a_wait_bcon_tmout(struct ci_hdrc *ci)
303 {
304         ci->fsm.a_wait_bcon_tmout = 1;
305         return 0;
306 }
307
308 static int a_aidl_bdis_tmout(struct ci_hdrc *ci)
309 {
310         ci->fsm.a_aidl_bdis_tmout = 1;
311         return 0;
312 }
313
314 static int b_ase0_brst_tmout(struct ci_hdrc *ci)
315 {
316         ci->fsm.b_ase0_brst_tmout = 1;
317         return 0;
318 }
319
320 static int a_bidl_adis_tmout(struct ci_hdrc *ci)
321 {
322         ci->fsm.a_bidl_adis_tmout = 1;
323         return 0;
324 }
325
326 static int b_aidl_bdis_tmout(struct ci_hdrc *ci)
327 {
328         ci->fsm.a_bus_suspend = 1;
329         return 0;
330 }
331
332 static int b_se0_srp_tmout(struct ci_hdrc *ci)
333 {
334         ci->fsm.b_se0_srp = 1;
335         return 0;
336 }
337
338 static int b_srp_fail_tmout(struct ci_hdrc *ci)
339 {
340         ci->fsm.b_srp_done = 1;
341         return 1;
342 }
343
344 static int b_data_pls_tmout(struct ci_hdrc *ci)
345 {
346         ci->fsm.b_srp_done = 1;
347         ci->fsm.b_bus_req = 0;
348         if (ci->fsm.power_up)
349                 ci->fsm.power_up = 0;
350         hw_write_otgsc(ci, OTGSC_HABA, 0);
351         pm_runtime_put(ci->dev);
352         return 0;
353 }
354
355 static int b_ssend_srp_tmout(struct ci_hdrc *ci)
356 {
357         ci->fsm.b_ssend_srp = 1;
358         /* only vbus fall below B_sess_vld in b_idle state */
359         if (ci->fsm.otg->state == OTG_STATE_B_IDLE)
360                 return 0;
361         else
362                 return 1;
363 }
364
365 /*
366  * Keep this list in the same order as timers indexed
367  * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
368  */
369 static int (*otg_timer_handlers[])(struct ci_hdrc *) = {
370         a_wait_vrise_tmout,     /* A_WAIT_VRISE */
371         a_wait_vfall_tmout,     /* A_WAIT_VFALL */
372         a_wait_bcon_tmout,      /* A_WAIT_BCON */
373         a_aidl_bdis_tmout,      /* A_AIDL_BDIS */
374         b_ase0_brst_tmout,      /* B_ASE0_BRST */
375         a_bidl_adis_tmout,      /* A_BIDL_ADIS */
376         b_aidl_bdis_tmout,      /* B_AIDL_BDIS */
377         b_se0_srp_tmout,        /* B_SE0_SRP */
378         b_srp_fail_tmout,       /* B_SRP_FAIL */
379         NULL,                   /* A_WAIT_ENUM */
380         b_data_pls_tmout,       /* B_DATA_PLS */
381         b_ssend_srp_tmout,      /* B_SSEND_SRP */
382 };
383
384 /*
385  * Enable the next nearest enabled timer if have
386  */
387 static enum hrtimer_restart ci_otg_hrtimer_func(struct hrtimer *t)
388 {
389         struct ci_hdrc *ci = container_of(t, struct ci_hdrc, otg_fsm_hrtimer);
390         ktime_t now, *timeout;
391         unsigned long   enabled_timer_bits;
392         unsigned long   flags;
393         enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
394         int ret = -EINVAL;
395
396         spin_lock_irqsave(&ci->lock, flags);
397         enabled_timer_bits = ci->enabled_otg_timer_bits;
398         ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
399
400         now = ktime_get();
401         for_each_set_bit(cur_timer, &enabled_timer_bits, NUM_OTG_FSM_TIMERS) {
402                 if (now.tv64 >= ci->hr_timeouts[cur_timer].tv64) {
403                         ci->enabled_otg_timer_bits &= ~(1 << cur_timer);
404                         if (otg_timer_handlers[cur_timer])
405                                 ret = otg_timer_handlers[cur_timer](ci);
406                 } else {
407                         if ((next_timer == NUM_OTG_FSM_TIMERS) ||
408                                 (ci->hr_timeouts[cur_timer].tv64 <
409                                         ci->hr_timeouts[next_timer].tv64))
410                                 next_timer = cur_timer;
411                 }
412         }
413         /* Enable the next nearest timer */
414         if (next_timer < NUM_OTG_FSM_TIMERS) {
415                 timeout = &ci->hr_timeouts[next_timer];
416                 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer, *timeout,
417                                         NSEC_PER_MSEC, HRTIMER_MODE_ABS);
418                 ci->next_otg_timer = next_timer;
419         }
420         spin_unlock_irqrestore(&ci->lock, flags);
421
422         if (!ret)
423                 ci_otg_queue_work(ci);
424
425         return HRTIMER_NORESTART;
426 }
427
428 /* Initialize timers */
429 static int ci_otg_init_timers(struct ci_hdrc *ci)
430 {
431         hrtimer_init(&ci->otg_fsm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
432         ci->otg_fsm_hrtimer.function = ci_otg_hrtimer_func;
433
434         return 0;
435 }
436
437 /* -------------------------------------------------------------*/
438 /* Operations that will be called from OTG Finite State Machine */
439 /* -------------------------------------------------------------*/
440 static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
441 {
442         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
443
444         if (t < NUM_OTG_FSM_TIMERS)
445                 ci_otg_add_timer(ci, t);
446         return;
447 }
448
449 static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
450 {
451         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
452
453         if (t < NUM_OTG_FSM_TIMERS)
454                 ci_otg_del_timer(ci, t);
455         return;
456 }
457
458 /*
459  * A-device drive vbus: turn on vbus regulator and enable port power
460  * Data pulse irq should be disabled while vbus is on.
461  */
462 static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
463 {
464         int ret;
465         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
466
467         if (on) {
468                 /* Enable power power */
469                 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
470                                                         PORTSC_PP);
471                 if (ci->platdata->reg_vbus) {
472                         ret = regulator_enable(ci->platdata->reg_vbus);
473                         if (ret) {
474                                 dev_err(ci->dev,
475                                 "Failed to enable vbus regulator, ret=%d\n",
476                                 ret);
477                                 return;
478                         }
479                 }
480                 /* Disable data pulse irq */
481                 hw_write_otgsc(ci, OTGSC_DPIE, 0);
482
483                 fsm->a_srp_det = 0;
484                 fsm->power_up = 0;
485         } else {
486                 if (ci->platdata->reg_vbus)
487                         regulator_disable(ci->platdata->reg_vbus);
488
489                 fsm->a_bus_drop = 1;
490                 fsm->a_bus_req = 0;
491         }
492 }
493
494 /*
495  * Control data line by Run Stop bit.
496  */
497 static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
498 {
499         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
500
501         if (on)
502                 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
503         else
504                 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
505 }
506
507 /*
508  * Generate SOF by host.
509  * In host mode, controller will automatically send SOF.
510  * Suspend will block the data on the port.
511  *
512  * This is controlled through usbcore by usb autosuspend,
513  * so the usb device class driver need support autosuspend,
514  * otherwise the bus suspend will not happen.
515  */
516 static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
517 {
518         struct usb_device *udev;
519
520         if (!fsm->otg->host)
521                 return;
522
523         udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
524         if (!udev)
525                 return;
526
527         if (on) {
528                 usb_disable_autosuspend(udev);
529         } else {
530                 pm_runtime_set_autosuspend_delay(&udev->dev, 0);
531                 usb_enable_autosuspend(udev);
532         }
533 }
534
535 /*
536  * Start SRP pulsing by data-line pulsing,
537  * no v-bus pulsing followed
538  */
539 static void ci_otg_start_pulse(struct otg_fsm *fsm)
540 {
541         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
542
543         /* Hardware Assistant Data pulse */
544         hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
545
546         pm_runtime_get(ci->dev);
547         ci_otg_add_timer(ci, B_DATA_PLS);
548 }
549
550 static int ci_otg_start_host(struct otg_fsm *fsm, int on)
551 {
552         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
553
554         if (on) {
555                 ci_role_stop(ci);
556                 ci_role_start(ci, CI_ROLE_HOST);
557         } else {
558                 ci_role_stop(ci);
559                 ci_role_start(ci, CI_ROLE_GADGET);
560         }
561         return 0;
562 }
563
564 static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
565 {
566         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
567
568         if (on)
569                 usb_gadget_vbus_connect(&ci->gadget);
570         else
571                 usb_gadget_vbus_disconnect(&ci->gadget);
572
573         return 0;
574 }
575
576 static struct otg_fsm_ops ci_otg_ops = {
577         .drv_vbus = ci_otg_drv_vbus,
578         .loc_conn = ci_otg_loc_conn,
579         .loc_sof = ci_otg_loc_sof,
580         .start_pulse = ci_otg_start_pulse,
581         .add_timer = ci_otg_fsm_add_timer,
582         .del_timer = ci_otg_fsm_del_timer,
583         .start_host = ci_otg_start_host,
584         .start_gadget = ci_otg_start_gadget,
585 };
586
587 int ci_otg_fsm_work(struct ci_hdrc *ci)
588 {
589         /*
590          * Don't do fsm transition for B device
591          * when there is no gadget class driver
592          */
593         if (ci->fsm.id && !(ci->driver) &&
594                 ci->fsm.otg->state < OTG_STATE_A_IDLE)
595                 return 0;
596
597         pm_runtime_get_sync(ci->dev);
598         if (otg_statemachine(&ci->fsm)) {
599                 if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
600                         /*
601                          * Further state change for cases:
602                          * a_idle to b_idle; or
603                          * a_idle to a_wait_vrise due to ID change(1->0), so
604                          * B-dev becomes A-dev can try to start new session
605                          * consequently; or
606                          * a_idle to a_wait_vrise when power up
607                          */
608                         if ((ci->fsm.id) || (ci->id_event) ||
609                                                 (ci->fsm.power_up)) {
610                                 ci_otg_queue_work(ci);
611                         } else {
612                                 /* Enable data pulse irq */
613                                 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS |
614                                                                 PORTSC_PP, 0);
615                                 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
616                                 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
617                         }
618                         if (ci->id_event)
619                                 ci->id_event = false;
620                 } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) {
621                         if (ci->fsm.b_sess_vld) {
622                                 ci->fsm.power_up = 0;
623                                 /*
624                                  * Further transite to b_periphearl state
625                                  * when register gadget driver with vbus on
626                                  */
627                                 ci_otg_queue_work(ci);
628                         }
629                 } else if (ci->fsm.otg->state == OTG_STATE_A_HOST) {
630                         pm_runtime_mark_last_busy(ci->dev);
631                         pm_runtime_put_autosuspend(ci->dev);
632                         return 0;
633                 }
634         }
635         pm_runtime_put_sync(ci->dev);
636         return 0;
637 }
638
639 /*
640  * Update fsm variables in each state if catching expected interrupts,
641  * called by otg fsm isr.
642  */
643 static void ci_otg_fsm_event(struct ci_hdrc *ci)
644 {
645         u32 intr_sts, otg_bsess_vld, port_conn;
646         struct otg_fsm *fsm = &ci->fsm;
647
648         intr_sts = hw_read_intr_status(ci);
649         otg_bsess_vld = hw_read_otgsc(ci, OTGSC_BSV);
650         port_conn = hw_read(ci, OP_PORTSC, PORTSC_CCS);
651
652         switch (ci->fsm.otg->state) {
653         case OTG_STATE_A_WAIT_BCON:
654                 if (port_conn) {
655                         fsm->b_conn = 1;
656                         fsm->a_bus_req = 1;
657                         ci_otg_queue_work(ci);
658                 }
659                 break;
660         case OTG_STATE_B_IDLE:
661                 if (otg_bsess_vld && (intr_sts & USBi_PCI) && port_conn) {
662                         fsm->b_sess_vld = 1;
663                         ci_otg_queue_work(ci);
664                 }
665                 break;
666         case OTG_STATE_B_PERIPHERAL:
667                 if ((intr_sts & USBi_SLI) && port_conn && otg_bsess_vld) {
668                         ci_otg_add_timer(ci, B_AIDL_BDIS);
669                 } else if (intr_sts & USBi_PCI) {
670                         ci_otg_del_timer(ci, B_AIDL_BDIS);
671                         if (fsm->a_bus_suspend == 1)
672                                 fsm->a_bus_suspend = 0;
673                 }
674                 break;
675         case OTG_STATE_B_HOST:
676                 if ((intr_sts & USBi_PCI) && !port_conn) {
677                         fsm->a_conn = 0;
678                         fsm->b_bus_req = 0;
679                         ci_otg_queue_work(ci);
680                 }
681                 break;
682         case OTG_STATE_A_PERIPHERAL:
683                 if (intr_sts & USBi_SLI) {
684                          fsm->b_bus_suspend = 1;
685                         /*
686                          * Init a timer to know how long this suspend
687                          * will continue, if time out, indicates B no longer
688                          * wants to be host role
689                          */
690                          ci_otg_add_timer(ci, A_BIDL_ADIS);
691                 }
692
693                 if (intr_sts & USBi_URI)
694                         ci_otg_del_timer(ci, A_BIDL_ADIS);
695
696                 if (intr_sts & USBi_PCI) {
697                         if (fsm->b_bus_suspend == 1) {
698                                 ci_otg_del_timer(ci, A_BIDL_ADIS);
699                                 fsm->b_bus_suspend = 0;
700                         }
701                 }
702                 break;
703         case OTG_STATE_A_SUSPEND:
704                 if ((intr_sts & USBi_PCI) && !port_conn) {
705                         fsm->b_conn = 0;
706
707                         /* if gadget driver is binded */
708                         if (ci->driver) {
709                                 /* A device to be peripheral mode */
710                                 ci->gadget.is_a_peripheral = 1;
711                         }
712                         ci_otg_queue_work(ci);
713                 }
714                 break;
715         case OTG_STATE_A_HOST:
716                 if ((intr_sts & USBi_PCI) && !port_conn) {
717                         fsm->b_conn = 0;
718                         ci_otg_queue_work(ci);
719                 }
720                 break;
721         case OTG_STATE_B_WAIT_ACON:
722                 if ((intr_sts & USBi_PCI) && port_conn) {
723                         fsm->a_conn = 1;
724                         ci_otg_queue_work(ci);
725                 }
726                 break;
727         default:
728                 break;
729         }
730 }
731
732 /*
733  * ci_otg_irq - otg fsm related irq handling
734  * and also update otg fsm variable by monitoring usb host and udc
735  * state change interrupts.
736  * @ci: ci_hdrc
737  */
738 irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
739 {
740         irqreturn_t retval =  IRQ_NONE;
741         u32 otgsc, otg_int_src = 0;
742         struct otg_fsm *fsm = &ci->fsm;
743
744         otgsc = hw_read_otgsc(ci, ~0);
745         otg_int_src = otgsc & OTGSC_INT_STATUS_BITS & (otgsc >> 8);
746         fsm->id = (otgsc & OTGSC_ID) ? 1 : 0;
747
748         if (otg_int_src) {
749                 if (otg_int_src & OTGSC_DPIS) {
750                         hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
751                         fsm->a_srp_det = 1;
752                         fsm->a_bus_drop = 0;
753                 } else if (otg_int_src & OTGSC_IDIS) {
754                         hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
755                         if (fsm->id == 0) {
756                                 fsm->a_bus_drop = 0;
757                                 fsm->a_bus_req = 1;
758                                 ci->id_event = true;
759                         }
760                 } else if (otg_int_src & OTGSC_BSVIS) {
761                         hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
762                         if (otgsc & OTGSC_BSV) {
763                                 fsm->b_sess_vld = 1;
764                                 ci_otg_del_timer(ci, B_SSEND_SRP);
765                                 ci_otg_del_timer(ci, B_SRP_FAIL);
766                                 fsm->b_ssend_srp = 0;
767                         } else {
768                                 fsm->b_sess_vld = 0;
769                                 if (fsm->id)
770                                         ci_otg_add_timer(ci, B_SSEND_SRP);
771                         }
772                 } else if (otg_int_src & OTGSC_AVVIS) {
773                         hw_write_otgsc(ci, OTGSC_AVVIS, OTGSC_AVVIS);
774                         if (otgsc & OTGSC_AVV) {
775                                 fsm->a_vbus_vld = 1;
776                         } else {
777                                 fsm->a_vbus_vld = 0;
778                                 fsm->b_conn = 0;
779                         }
780                 }
781                 ci_otg_queue_work(ci);
782                 return IRQ_HANDLED;
783         }
784
785         ci_otg_fsm_event(ci);
786
787         return retval;
788 }
789
790 void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
791 {
792         ci_otg_queue_work(ci);
793 }
794
795 int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
796 {
797         int retval = 0;
798
799         if (ci->phy)
800                 ci->otg.phy = ci->phy;
801         else
802                 ci->otg.usb_phy = ci->usb_phy;
803
804         ci->otg.gadget = &ci->gadget;
805         ci->fsm.otg = &ci->otg;
806         ci->fsm.power_up = 1;
807         ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
808         ci->fsm.otg->state = OTG_STATE_UNDEFINED;
809         ci->fsm.ops = &ci_otg_ops;
810         ci->gadget.hnp_polling_support = 1;
811         ci->fsm.host_req_flag = devm_kzalloc(ci->dev, 1, GFP_KERNEL);
812         if (!ci->fsm.host_req_flag)
813                 return -ENOMEM;
814
815         mutex_init(&ci->fsm.lock);
816
817         retval = ci_otg_init_timers(ci);
818         if (retval) {
819                 dev_err(ci->dev, "Couldn't init OTG timers\n");
820                 return retval;
821         }
822         ci->enabled_otg_timer_bits = 0;
823         ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
824
825         retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
826         if (retval < 0) {
827                 dev_dbg(ci->dev,
828                         "Can't register sysfs attr group: %d\n", retval);
829                 return retval;
830         }
831
832         /* Enable A vbus valid irq */
833         hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
834
835         if (ci->fsm.id) {
836                 ci->fsm.b_ssend_srp =
837                         hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
838                 ci->fsm.b_sess_vld =
839                         hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
840                 /* Enable BSV irq */
841                 hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
842         }
843
844         return 0;
845 }
846
847 void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
848 {
849         sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);
850 }