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