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