2 * otg_fsm.c - ChipIdea USB IP core OTG FSM driver
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
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.
14 * This file mainly handles OTG fsm, it includes OTG fsm operations
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>
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)
37 struct ci_otg_fsm_timer *timer;
39 timer = devm_kzalloc(ci->dev, sizeof(struct ci_otg_fsm_timer),
43 timer->function = function;
44 timer->expires = expires;
49 /* Add for otg: interact with user space app */
51 get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
55 struct ci_hdrc *ci = dev_get_drvdata(dev);
59 t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_req);
63 return PAGE_SIZE - size;
67 set_a_bus_req(struct device *dev, struct device_attribute *attr,
68 const char *buf, size_t count)
70 struct ci_hdrc *ci = dev_get_drvdata(dev);
75 mutex_lock(&ci->fsm.lock);
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);
84 ci->fsm.a_bus_req = 1;
87 disable_irq_nosync(ci->irq);
88 queue_work(ci->wq, &ci->work);
89 mutex_unlock(&ci->fsm.lock);
93 static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req, set_a_bus_req);
96 get_a_bus_drop(struct device *dev, struct device_attribute *attr, char *buf)
100 struct ci_hdrc *ci = dev_get_drvdata(dev);
104 t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_drop);
108 return PAGE_SIZE - size;
112 set_a_bus_drop(struct device *dev, struct device_attribute *attr,
113 const char *buf, size_t count)
115 struct ci_hdrc *ci = dev_get_drvdata(dev);
120 mutex_lock(&ci->fsm.lock);
122 ci->fsm.a_bus_drop = 0;
123 } else if (buf[0] == '1') {
124 ci->fsm.a_bus_drop = 1;
125 ci->fsm.a_bus_req = 0;
128 disable_irq_nosync(ci->irq);
129 queue_work(ci->wq, &ci->work);
130 mutex_unlock(&ci->fsm.lock);
134 static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUSR, get_a_bus_drop,
138 get_b_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
142 struct ci_hdrc *ci = dev_get_drvdata(dev);
146 t = scnprintf(next, size, "%d\n", ci->fsm.b_bus_req);
150 return PAGE_SIZE - size;
154 set_b_bus_req(struct device *dev, struct device_attribute *attr,
155 const char *buf, size_t count)
157 struct ci_hdrc *ci = dev_get_drvdata(dev);
162 mutex_lock(&ci->fsm.lock);
164 ci->fsm.b_bus_req = 0;
165 else if (buf[0] == '1')
166 ci->fsm.b_bus_req = 1;
168 disable_irq_nosync(ci->irq);
169 queue_work(ci->wq, &ci->work);
170 mutex_unlock(&ci->fsm.lock);
174 static DEVICE_ATTR(b_bus_req, S_IRUGO | S_IWUSR, get_b_bus_req, set_b_bus_req);
177 set_a_clr_err(struct device *dev, struct device_attribute *attr,
178 const char *buf, size_t count)
180 struct ci_hdrc *ci = dev_get_drvdata(dev);
185 mutex_lock(&ci->fsm.lock);
187 ci->fsm.a_clr_err = 1;
189 disable_irq_nosync(ci->irq);
190 queue_work(ci->wq, &ci->work);
191 mutex_unlock(&ci->fsm.lock);
195 static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err);
197 static struct attribute *inputs_attrs[] = {
198 &dev_attr_a_bus_req.attr,
199 &dev_attr_a_bus_drop.attr,
200 &dev_attr_b_bus_req.attr,
201 &dev_attr_a_clr_err.attr,
205 static struct attribute_group inputs_attr_group = {
207 .attrs = inputs_attrs,
211 * Add timer to active timer list
213 static void ci_otg_add_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t)
215 struct ci_otg_fsm_timer *tmp_timer;
216 struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
217 struct list_head *active_timers = &ci->fsm_timer->active_timers;
219 if (t >= NUM_CI_OTG_FSM_TIMERS)
223 * Check if the timer is already in the active list,
224 * if so update timer count
226 list_for_each_entry(tmp_timer, active_timers, list)
227 if (tmp_timer == timer) {
228 timer->count = timer->expires;
232 timer->count = timer->expires;
233 list_add_tail(&timer->list, active_timers);
236 if (!(hw_read_otgsc(ci, OTGSC_1MSIE)))
237 hw_write_otgsc(ci, OTGSC_1MSIE, OTGSC_1MSIE);
241 * Remove timer from active timer list
243 static void ci_otg_del_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t)
245 struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
246 struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
247 struct list_head *active_timers = &ci->fsm_timer->active_timers;
249 if (t >= NUM_CI_OTG_FSM_TIMERS)
252 list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list)
253 if (tmp_timer == timer)
254 list_del(&timer->list);
256 /* Disable 1ms irq if there is no any active timer */
257 if (list_empty(active_timers))
258 hw_write_otgsc(ci, OTGSC_1MSIE, 0);
262 * Reduce timer count by 1, and find timeout conditions.
263 * Called by otg 1ms timer interrupt
265 static inline int ci_otg_tick_timer(struct ci_hdrc *ci)
267 struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
268 struct list_head *active_timers = &ci->fsm_timer->active_timers;
271 list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list) {
273 /* check if timer expires */
274 if (!tmp_timer->count) {
275 list_del(&tmp_timer->list);
276 tmp_timer->function(ci, tmp_timer->data);
281 /* disable 1ms irq if there is no any timer active */
282 if ((expired == 1) && list_empty(active_timers))
283 hw_write_otgsc(ci, OTGSC_1MSIE, 0);
288 /* The timeout callback function to set time out bit */
289 static void set_tmout(void *ptr, unsigned long indicator)
291 *(int *)indicator = 1;
294 static void set_tmout_and_fsm(void *ptr, unsigned long indicator)
296 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
298 set_tmout(ci, indicator);
300 disable_irq_nosync(ci->irq);
301 queue_work(ci->wq, &ci->work);
304 static void a_wait_vfall_tmout_func(void *ptr, unsigned long indicator)
306 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
308 set_tmout(ci, indicator);
309 /* Disable port power */
310 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP, 0);
311 /* Clear exsiting DP irq */
312 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
313 /* Enable data pulse irq */
314 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
315 disable_irq_nosync(ci->irq);
316 queue_work(ci->wq, &ci->work);
319 static void b_ase0_brst_tmout_func(void *ptr, unsigned long indicator)
321 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
323 set_tmout(ci, indicator);
324 if (!hw_read_otgsc(ci, OTGSC_BSV))
325 ci->fsm.b_sess_vld = 0;
327 disable_irq_nosync(ci->irq);
328 queue_work(ci->wq, &ci->work);
331 static void b_ssend_srp_tmout_func(void *ptr, unsigned long indicator)
333 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
335 set_tmout(ci, indicator);
337 /* only vbus fall below B_sess_vld in b_idle state */
338 if (ci->transceiver->state == OTG_STATE_B_IDLE) {
339 disable_irq_nosync(ci->irq);
340 queue_work(ci->wq, &ci->work);
344 static void b_sess_vld_tmout_func(void *ptr, unsigned long indicator)
346 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
348 /* Check if A detached */
349 if (!(hw_read_otgsc(ci, OTGSC_BSV))) {
350 ci->fsm.b_sess_vld = 0;
351 ci_otg_add_timer(ci, B_SSEND_SRP);
352 disable_irq_nosync(ci->irq);
353 queue_work(ci->wq, &ci->work);
357 static void b_data_pulse_end(void *ptr, unsigned long indicator)
359 struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
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;
366 hw_write_otgsc(ci, OTGSC_HABA, 0);
368 disable_irq_nosync(ci->irq);
369 queue_work(ci->wq, &ci->work);
372 /* Initialize timers */
373 static int ci_otg_init_timers(struct ci_hdrc *ci)
375 struct otg_fsm *fsm = &ci->fsm;
377 /* FSM used timers */
378 ci->fsm_timer->timer_list[A_WAIT_VRISE] =
379 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_VRISE,
380 (unsigned long)&fsm->a_wait_vrise_tmout);
381 if (ci->fsm_timer->timer_list[A_WAIT_VRISE] == NULL)
384 ci->fsm_timer->timer_list[A_WAIT_VFALL] =
385 otg_timer_initializer(ci, &a_wait_vfall_tmout_func,
386 TA_WAIT_VFALL, (unsigned long)&fsm->a_wait_vfall_tmout);
387 if (ci->fsm_timer->timer_list[A_WAIT_VFALL] == NULL)
390 ci->fsm_timer->timer_list[A_WAIT_BCON] =
391 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_BCON,
392 (unsigned long)&fsm->a_wait_bcon_tmout);
393 if (ci->fsm_timer->timer_list[A_WAIT_BCON] == NULL)
396 ci->fsm_timer->timer_list[A_AIDL_BDIS] =
397 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_AIDL_BDIS,
398 (unsigned long)&fsm->a_aidl_bdis_tmout);
399 if (ci->fsm_timer->timer_list[A_AIDL_BDIS] == NULL)
402 ci->fsm_timer->timer_list[A_BIDL_ADIS] =
403 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_BIDL_ADIS,
404 (unsigned long)&fsm->a_bidl_adis_tmout);
405 if (ci->fsm_timer->timer_list[A_BIDL_ADIS] == NULL)
408 ci->fsm_timer->timer_list[B_ASE0_BRST] =
409 otg_timer_initializer(ci, &b_ase0_brst_tmout_func, TB_ASE0_BRST,
410 (unsigned long)&fsm->b_ase0_brst_tmout);
411 if (ci->fsm_timer->timer_list[B_ASE0_BRST] == NULL)
414 ci->fsm_timer->timer_list[B_SE0_SRP] =
415 otg_timer_initializer(ci, &set_tmout_and_fsm, TB_SE0_SRP,
416 (unsigned long)&fsm->b_se0_srp);
417 if (ci->fsm_timer->timer_list[B_SE0_SRP] == NULL)
420 ci->fsm_timer->timer_list[B_SSEND_SRP] =
421 otg_timer_initializer(ci, &b_ssend_srp_tmout_func, TB_SSEND_SRP,
422 (unsigned long)&fsm->b_ssend_srp);
423 if (ci->fsm_timer->timer_list[B_SSEND_SRP] == NULL)
426 ci->fsm_timer->timer_list[B_SRP_FAIL] =
427 otg_timer_initializer(ci, &set_tmout, TB_SRP_FAIL,
428 (unsigned long)&fsm->b_srp_done);
429 if (ci->fsm_timer->timer_list[B_SRP_FAIL] == NULL)
432 ci->fsm_timer->timer_list[B_DATA_PLS] =
433 otg_timer_initializer(ci, &b_data_pulse_end, TB_DATA_PLS, 0);
434 if (ci->fsm_timer->timer_list[B_DATA_PLS] == NULL)
437 ci->fsm_timer->timer_list[B_SESS_VLD] = otg_timer_initializer(ci,
438 &b_sess_vld_tmout_func, TB_SESS_VLD, 0);
439 if (ci->fsm_timer->timer_list[B_SESS_VLD] == NULL)
445 /* -------------------------------------------------------------*/
446 /* Operations that will be called from OTG Finite State Machine */
447 /* -------------------------------------------------------------*/
448 static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
450 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
452 if (t < NUM_OTG_FSM_TIMERS)
453 ci_otg_add_timer(ci, t);
457 static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
459 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
461 if (t < NUM_OTG_FSM_TIMERS)
462 ci_otg_del_timer(ci, t);
467 * A-device drive vbus: turn on vbus regulator and enable port power
468 * Data pulse irq should be disabled while vbus is on.
470 static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
473 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
476 /* Enable power power */
477 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
479 if (ci->platdata->reg_vbus) {
480 ret = regulator_enable(ci->platdata->reg_vbus);
483 "Failed to enable vbus regulator, ret=%d\n",
488 /* Disable data pulse irq */
489 hw_write_otgsc(ci, OTGSC_DPIE, 0);
494 if (ci->platdata->reg_vbus)
495 regulator_disable(ci->platdata->reg_vbus);
503 * Control data line by Run Stop bit.
505 static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
507 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
510 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
512 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
516 * Generate SOF by host.
517 * This is controlled through suspend/resume the port.
518 * In host mode, controller will automatically send SOF.
519 * Suspend will block the data on the port.
521 static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
523 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
526 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_FPR,
529 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_SUSP,
534 * Start SRP pulsing by data-line pulsing,
535 * no v-bus pulsing followed
537 static void ci_otg_start_pulse(struct otg_fsm *fsm)
539 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
541 /* Hardware Assistant Data pulse */
542 hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
544 ci_otg_add_timer(ci, B_DATA_PLS);
547 static int ci_otg_start_host(struct otg_fsm *fsm, int on)
549 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
551 mutex_unlock(&fsm->lock);
554 ci_role_start(ci, CI_ROLE_HOST);
557 hw_device_reset(ci, USBMODE_CM_DC);
558 ci_role_start(ci, CI_ROLE_GADGET);
560 mutex_lock(&fsm->lock);
564 static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
566 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
568 mutex_unlock(&fsm->lock);
570 usb_gadget_vbus_connect(&ci->gadget);
572 usb_gadget_vbus_disconnect(&ci->gadget);
573 mutex_lock(&fsm->lock);
578 static struct otg_fsm_ops ci_otg_ops = {
579 .drv_vbus = ci_otg_drv_vbus,
580 .loc_conn = ci_otg_loc_conn,
581 .loc_sof = ci_otg_loc_sof,
582 .start_pulse = ci_otg_start_pulse,
583 .add_timer = ci_otg_fsm_add_timer,
584 .del_timer = ci_otg_fsm_del_timer,
585 .start_host = ci_otg_start_host,
586 .start_gadget = ci_otg_start_gadget,
589 int ci_otg_fsm_work(struct ci_hdrc *ci)
592 * Don't do fsm transition for B device
593 * when there is no gadget class driver
595 if (ci->fsm.id && !(ci->driver) &&
596 ci->transceiver->state < OTG_STATE_A_IDLE)
599 if (otg_statemachine(&ci->fsm)) {
600 if (ci->transceiver->state == OTG_STATE_A_IDLE) {
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
607 * a_idle to a_wait_vrise when power up
609 if ((ci->fsm.id) || (ci->id_event) ||
610 (ci->fsm.power_up)) {
611 disable_irq_nosync(ci->irq);
612 queue_work(ci->wq, &ci->work);
615 ci->id_event = false;
616 } else if (ci->transceiver->state == OTG_STATE_B_IDLE) {
617 if (ci->fsm.b_sess_vld) {
618 ci->fsm.power_up = 0;
620 * Further transite to b_periphearl state
621 * when register gadget driver with vbus on
623 disable_irq_nosync(ci->irq);
624 queue_work(ci->wq, &ci->work);
632 * Update fsm variables in each state if catching expected interrupts,
633 * called by otg fsm isr.
635 static void ci_otg_fsm_event(struct ci_hdrc *ci)
637 u32 intr_sts, otg_bsess_vld, port_conn;
638 struct otg_fsm *fsm = &ci->fsm;
640 intr_sts = hw_read_intr_status(ci);
641 otg_bsess_vld = hw_read_otgsc(ci, OTGSC_BSV);
642 port_conn = hw_read(ci, OP_PORTSC, PORTSC_CCS);
644 switch (ci->transceiver->state) {
645 case OTG_STATE_A_WAIT_BCON:
649 disable_irq_nosync(ci->irq);
650 queue_work(ci->wq, &ci->work);
653 case OTG_STATE_B_IDLE:
654 if (otg_bsess_vld && (intr_sts & USBi_PCI) && port_conn) {
656 disable_irq_nosync(ci->irq);
657 queue_work(ci->wq, &ci->work);
660 case OTG_STATE_B_PERIPHERAL:
661 if ((intr_sts & USBi_SLI) && port_conn && otg_bsess_vld) {
662 fsm->a_bus_suspend = 1;
663 disable_irq_nosync(ci->irq);
664 queue_work(ci->wq, &ci->work);
665 } else if (intr_sts & USBi_PCI) {
666 if (fsm->a_bus_suspend == 1)
667 fsm->a_bus_suspend = 0;
670 case OTG_STATE_B_HOST:
671 if ((intr_sts & USBi_PCI) && !port_conn) {
674 disable_irq_nosync(ci->irq);
675 queue_work(ci->wq, &ci->work);
676 ci_otg_add_timer(ci, B_SESS_VLD);
679 case OTG_STATE_A_PERIPHERAL:
680 if (intr_sts & USBi_SLI) {
681 fsm->b_bus_suspend = 1;
683 * Init a timer to know how long this suspend
684 * will contine, if time out, indicates B no longer
685 * wants to be host role
687 ci_otg_add_timer(ci, A_BIDL_ADIS);
690 if (intr_sts & USBi_URI)
691 ci_otg_del_timer(ci, A_BIDL_ADIS);
693 if (intr_sts & USBi_PCI) {
694 if (fsm->b_bus_suspend == 1) {
695 ci_otg_del_timer(ci, A_BIDL_ADIS);
696 fsm->b_bus_suspend = 0;
700 case OTG_STATE_A_SUSPEND:
701 if ((intr_sts & USBi_PCI) && !port_conn) {
704 /* if gadget driver is binded */
706 /* A device to be peripheral mode */
707 ci->gadget.is_a_peripheral = 1;
709 disable_irq_nosync(ci->irq);
710 queue_work(ci->wq, &ci->work);
713 case OTG_STATE_A_HOST:
714 if ((intr_sts & USBi_PCI) && !port_conn) {
716 disable_irq_nosync(ci->irq);
717 queue_work(ci->wq, &ci->work);
720 case OTG_STATE_B_WAIT_ACON:
721 if ((intr_sts & USBi_PCI) && port_conn) {
723 disable_irq_nosync(ci->irq);
724 queue_work(ci->wq, &ci->work);
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.
738 irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
740 irqreturn_t retval = IRQ_NONE;
741 u32 otgsc, otg_int_src = 0;
742 struct otg_fsm *fsm = &ci->fsm;
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;
749 if (otg_int_src & OTGSC_1MSIS) {
750 hw_write_otgsc(ci, OTGSC_1MSIS, OTGSC_1MSIS);
751 retval = ci_otg_tick_timer(ci);
753 } else if (otg_int_src & OTGSC_DPIS) {
754 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
757 } else if (otg_int_src & OTGSC_IDIS) {
758 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
764 } else if (otg_int_src & OTGSC_BSVIS) {
765 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
766 if (otgsc & OTGSC_BSV) {
768 ci_otg_del_timer(ci, B_SSEND_SRP);
769 ci_otg_del_timer(ci, B_SRP_FAIL);
770 fsm->b_ssend_srp = 0;
774 ci_otg_add_timer(ci, B_SSEND_SRP);
776 } else if (otg_int_src & OTGSC_AVVIS) {
777 hw_write_otgsc(ci, OTGSC_AVVIS, OTGSC_AVVIS);
778 if (otgsc & OTGSC_AVV) {
785 disable_irq_nosync(ci->irq);
786 queue_work(ci->wq, &ci->work);
790 ci_otg_fsm_event(ci);
795 void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
797 disable_irq_nosync(ci->irq);
798 queue_work(ci->wq, &ci->work);
801 int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
806 otg = devm_kzalloc(ci->dev,
807 sizeof(struct usb_otg), GFP_KERNEL);
810 "Failed to allocate usb_otg structure for ci hdrc otg!\n");
814 otg->phy = ci->transceiver;
815 otg->gadget = &ci->gadget;
817 ci->transceiver->otg = ci->fsm.otg;
818 ci->fsm.power_up = 1;
819 ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
820 ci->transceiver->state = OTG_STATE_UNDEFINED;
821 ci->fsm.ops = &ci_otg_ops;
823 mutex_init(&ci->fsm.lock);
825 ci->fsm_timer = devm_kzalloc(ci->dev,
826 sizeof(struct ci_otg_fsm_timer_list), GFP_KERNEL);
827 if (!ci->fsm_timer) {
829 "Failed to allocate timer structure for ci hdrc otg!\n");
833 INIT_LIST_HEAD(&ci->fsm_timer->active_timers);
834 retval = ci_otg_init_timers(ci);
836 dev_err(ci->dev, "Couldn't init OTG timers\n");
840 retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
843 "Can't register sysfs attr group: %d\n", retval);
847 /* Enable A vbus valid irq */
848 hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
851 ci->fsm.b_ssend_srp =
852 hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
854 hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
856 hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
862 void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
864 sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);