linux/drivers/usb/chipidea/otg_fsm.c
<<
>>
Prefs
   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 */
  34static ssize_t
  35get_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
  50static ssize_t
  51set_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}
  81static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req, set_a_bus_req);
  82
  83static ssize_t
  84get_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
  99static ssize_t
 100set_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}
 121static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUSR, get_a_bus_drop,
 122                                                set_a_bus_drop);
 123
 124static ssize_t
 125get_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
 140static ssize_t
 141set_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}
 166static DEVICE_ATTR(b_bus_req, S_IRUGO | S_IWUSR, get_b_bus_req, set_b_bus_req);
 167
 168static ssize_t
 169set_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}
 186static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err);
 187
 188static 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
 196static 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 */
 205static 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 */
 223static 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 */
 250static 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                        /* No enabled timers after delete it */
 264                        hrtimer_cancel(&ci->otg_fsm_hrtimer);
 265                        ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
 266                } else {
 267                        /* Find the next timer */
 268                        enabled_timer_bits = ci->enabled_otg_timer_bits;
 269                        for_each_set_bit(cur_timer, &enabled_timer_bits,
 270                                                        NUM_OTG_FSM_TIMERS) {
 271                                if ((next_timer == NUM_OTG_FSM_TIMERS) ||
 272                                        (ci->hr_timeouts[next_timer].tv64 <
 273                                        ci->hr_timeouts[cur_timer].tv64))
 274                                        next_timer = cur_timer;
 275                        }
 276                }
 277        }
 278        if (next_timer != NUM_OTG_FSM_TIMERS) {
 279                ci->next_otg_timer = next_timer;
 280                hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
 281                        ci->hr_timeouts[next_timer], NSEC_PER_MSEC,
 282                                                        HRTIMER_MODE_ABS);
 283        }
 284        spin_unlock_irqrestore(&ci->lock, flags);
 285}
 286
 287/* OTG FSM timer handlers */
 288static int a_wait_vrise_tmout(struct ci_hdrc *ci)
 289{
 290        ci->fsm.a_wait_vrise_tmout = 1;
 291        return 0;
 292}
 293
 294static int a_wait_vfall_tmout(struct ci_hdrc *ci)
 295{
 296        ci->fsm.a_wait_vfall_tmout = 1;
 297        return 0;
 298}
 299
 300static int a_wait_bcon_tmout(struct ci_hdrc *ci)
 301{
 302        ci->fsm.a_wait_bcon_tmout = 1;
 303        return 0;
 304}
 305
 306static int a_aidl_bdis_tmout(struct ci_hdrc *ci)
 307{
 308        ci->fsm.a_aidl_bdis_tmout = 1;
 309        return 0;
 310}
 311
 312static int b_ase0_brst_tmout(struct ci_hdrc *ci)
 313{
 314        ci->fsm.b_ase0_brst_tmout = 1;
 315        return 0;
 316}
 317
 318static int a_bidl_adis_tmout(struct ci_hdrc *ci)
 319{
 320        ci->fsm.a_bidl_adis_tmout = 1;
 321        return 0;
 322}
 323
 324static int b_aidl_bdis_tmout(struct ci_hdrc *ci)
 325{
 326        ci->fsm.a_bus_suspend = 1;
 327        return 0;
 328}
 329
 330static int b_se0_srp_tmout(struct ci_hdrc *ci)
 331{
 332        ci->fsm.b_se0_srp = 1;
 333        return 0;
 334}
 335
 336static int b_srp_fail_tmout(struct ci_hdrc *ci)
 337{
 338        ci->fsm.b_srp_done = 1;
 339        return 1;
 340}
 341
 342static int b_data_pls_tmout(struct ci_hdrc *ci)
 343{
 344        ci->fsm.b_srp_done = 1;
 345        ci->fsm.b_bus_req = 0;
 346        if (ci->fsm.power_up)
 347                ci->fsm.power_up = 0;
 348        hw_write_otgsc(ci, OTGSC_HABA, 0);
 349        pm_runtime_put(ci->dev);
 350        return 0;
 351}
 352
 353static int b_ssend_srp_tmout(struct ci_hdrc *ci)
 354{
 355        ci->fsm.b_ssend_srp = 1;
 356        /* only vbus fall below B_sess_vld in b_idle state */
 357        if (ci->fsm.otg->state == OTG_STATE_B_IDLE)
 358                return 0;
 359        else
 360                return 1;
 361}
 362
 363/*
 364 * Keep this list in the same order as timers indexed
 365 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
 366 */
 367static int (*otg_timer_handlers[])(struct ci_hdrc *) = {
 368        a_wait_vrise_tmout,     /* A_WAIT_VRISE */
 369        a_wait_vfall_tmout,     /* A_WAIT_VFALL */
 370        a_wait_bcon_tmout,      /* A_WAIT_BCON */
 371        a_aidl_bdis_tmout,      /* A_AIDL_BDIS */
 372        b_ase0_brst_tmout,      /* B_ASE0_BRST */
 373        a_bidl_adis_tmout,      /* A_BIDL_ADIS */
 374        b_aidl_bdis_tmout,      /* B_AIDL_BDIS */
 375        b_se0_srp_tmout,        /* B_SE0_SRP */
 376        b_srp_fail_tmout,       /* B_SRP_FAIL */
 377        NULL,                   /* A_WAIT_ENUM */
 378        b_data_pls_tmout,       /* B_DATA_PLS */
 379        b_ssend_srp_tmout,      /* B_SSEND_SRP */
 380};
 381
 382/*
 383 * Enable the next nearest enabled timer if have
 384 */
 385static enum hrtimer_restart ci_otg_hrtimer_func(struct hrtimer *t)
 386{
 387        struct ci_hdrc *ci = container_of(t, struct ci_hdrc, otg_fsm_hrtimer);
 388        ktime_t now, *timeout;
 389        unsigned long   enabled_timer_bits;
 390        unsigned long   flags;
 391        enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
 392        int ret = -EINVAL;
 393
 394        spin_lock_irqsave(&ci->lock, flags);
 395        enabled_timer_bits = ci->enabled_otg_timer_bits;
 396        ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
 397
 398        now = ktime_get();
 399        for_each_set_bit(cur_timer, &enabled_timer_bits, NUM_OTG_FSM_TIMERS) {
 400                if (now.tv64 >= ci->hr_timeouts[cur_timer].tv64) {
 401                        ci->enabled_otg_timer_bits &= ~(1 << cur_timer);
 402                        if (otg_timer_handlers[cur_timer])
 403                                ret = otg_timer_handlers[cur_timer](ci);
 404                } else {
 405                        if ((next_timer == NUM_OTG_FSM_TIMERS) ||
 406                                (ci->hr_timeouts[cur_timer].tv64 <
 407                                        ci->hr_timeouts[next_timer].tv64))
 408                                next_timer = cur_timer;
 409                }
 410        }
 411        /* Enable the next nearest timer */
 412        if (next_timer < NUM_OTG_FSM_TIMERS) {
 413                timeout = &ci->hr_timeouts[next_timer];
 414                hrtimer_start_range_ns(&ci->otg_fsm_hrtimer, *timeout,
 415                                        NSEC_PER_MSEC, HRTIMER_MODE_ABS);
 416                ci->next_otg_timer = next_timer;
 417        }
 418        spin_unlock_irqrestore(&ci->lock, flags);
 419
 420        if (!ret)
 421                ci_otg_queue_work(ci);
 422
 423        return HRTIMER_NORESTART;
 424}
 425
 426/* Initialize timers */
 427static int ci_otg_init_timers(struct ci_hdrc *ci)
 428{
 429        hrtimer_init(&ci->otg_fsm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
 430        ci->otg_fsm_hrtimer.function = ci_otg_hrtimer_func;
 431
 432        return 0;
 433}
 434
 435/* -------------------------------------------------------------*/
 436/* Operations that will be called from OTG Finite State Machine */
 437/* -------------------------------------------------------------*/
 438static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
 439{
 440        struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
 441
 442        if (t < NUM_OTG_FSM_TIMERS)
 443                ci_otg_add_timer(ci, t);
 444        return;
 445}
 446
 447static void ci_otg_fsm_del_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_del_timer(ci, t);
 453        return;
 454}
 455
 456/*
 457 * A-device drive vbus: turn on vbus regulator and enable port power
 458 * Data pulse irq should be disabled while vbus is on.
 459 */
 460static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
 461{
 462        int ret;
 463        struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
 464
 465        if (on) {
 466                /* Enable power power */
 467                hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
 468                                                        PORTSC_PP);
 469                if (ci->platdata->reg_vbus) {
 470                        ret = regulator_enable(ci->platdata->reg_vbus);
 471                        if (ret) {
 472                                dev_err(ci->dev,
 473                                "Failed to enable vbus regulator, ret=%d\n",
 474                                ret);
 475                                return;
 476                        }
 477                }
 478
 479                if (ci->platdata->flags & CI_HDRC_PHY_VBUS_CONTROL &&
 480                                ci->usb_phy && ci->usb_phy->set_vbus)
 481                        ci->usb_phy->set_vbus(ci->usb_phy, 1);
 482
 483                /* Disable data pulse irq */
 484                hw_write_otgsc(ci, OTGSC_DPIE, 0);
 485
 486                fsm->a_srp_det = 0;
 487                fsm->power_up = 0;
 488        } else {
 489                if (ci->platdata->reg_vbus)
 490                        regulator_disable(ci->platdata->reg_vbus);
 491
 492                if (ci->platdata->flags & CI_HDRC_PHY_VBUS_CONTROL &&
 493                                ci->usb_phy && ci->usb_phy->set_vbus)
 494                        ci->usb_phy->set_vbus(ci->usb_phy, 0);
 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 */
 504static 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 * In host mode, controller will automatically send SOF.
 517 * Suspend will block the data on the port.
 518 *
 519 * This is controlled through usbcore by usb autosuspend,
 520 * so the usb device class driver need support autosuspend,
 521 * otherwise the bus suspend will not happen.
 522 */
 523static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
 524{
 525        struct usb_device *udev;
 526
 527        if (!fsm->otg->host)
 528                return;
 529
 530        udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
 531        if (!udev)
 532                return;
 533
 534        if (on) {
 535                usb_disable_autosuspend(udev);
 536        } else {
 537                pm_runtime_set_autosuspend_delay(&udev->dev, 0);
 538                usb_enable_autosuspend(udev);
 539        }
 540}
 541
 542/*
 543 * Start SRP pulsing by data-line pulsing,
 544 * no v-bus pulsing followed
 545 */
 546static void ci_otg_start_pulse(struct otg_fsm *fsm)
 547{
 548        struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
 549
 550        /* Hardware Assistant Data pulse */
 551        hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
 552
 553        pm_runtime_get(ci->dev);
 554        ci_otg_add_timer(ci, B_DATA_PLS);
 555}
 556
 557static int ci_otg_start_host(struct otg_fsm *fsm, int on)
 558{
 559        struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
 560
 561        if (on) {
 562                ci_role_stop(ci);
 563                ci_role_start(ci, CI_ROLE_HOST);
 564        } else {
 565                ci_role_stop(ci);
 566                ci_role_start(ci, CI_ROLE_GADGET);
 567        }
 568        return 0;
 569}
 570
 571static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
 572{
 573        struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
 574
 575        if (on)
 576                usb_gadget_vbus_connect(&ci->gadget);
 577        else
 578                usb_gadget_vbus_disconnect(&ci->gadget);
 579
 580        return 0;
 581}
 582
 583static struct otg_fsm_ops ci_otg_ops = {
 584        .drv_vbus = ci_otg_drv_vbus,
 585        .loc_conn = ci_otg_loc_conn,
 586        .loc_sof = ci_otg_loc_sof,
 587        .start_pulse = ci_otg_start_pulse,
 588        .add_timer = ci_otg_fsm_add_timer,
 589        .del_timer = ci_otg_fsm_del_timer,
 590        .start_host = ci_otg_start_host,
 591        .start_gadget = ci_otg_start_gadget,
 592};
 593
 594int ci_otg_fsm_work(struct ci_hdrc *ci)
 595{
 596        /*
 597         * Don't do fsm transition for B device
 598         * when there is no gadget class driver
 599         */
 600        if (ci->fsm.id && !(ci->driver) &&
 601                ci->fsm.otg->state < OTG_STATE_A_IDLE)
 602                return 0;
 603
 604        pm_runtime_get_sync(ci->dev);
 605        if (otg_statemachine(&ci->fsm)) {
 606                if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
 607                        /*
 608                         * Further state change for cases:
 609                         * a_idle to b_idle; or
 610                         * a_idle to a_wait_vrise due to ID change(1->0), so
 611                         * B-dev becomes A-dev can try to start new session
 612                         * consequently; or
 613                         * a_idle to a_wait_vrise when power up
 614                         */
 615                        if ((ci->fsm.id) || (ci->id_event) ||
 616                                                (ci->fsm.power_up)) {
 617                                ci_otg_queue_work(ci);
 618                        } else {
 619                                /* Enable data pulse irq */
 620                                hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS |
 621                                                                PORTSC_PP, 0);
 622                                hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
 623                                hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
 624                        }
 625                        if (ci->id_event)
 626                                ci->id_event = false;
 627                } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) {
 628                        if (ci->fsm.b_sess_vld) {
 629                                ci->fsm.power_up = 0;
 630                                /*
 631                                 * Further transite to b_periphearl state
 632                                 * when register gadget driver with vbus on
 633                                 */
 634                                ci_otg_queue_work(ci);
 635                        }
 636                } else if (ci->fsm.otg->state == OTG_STATE_A_HOST) {
 637                        pm_runtime_mark_last_busy(ci->dev);
 638                        pm_runtime_put_autosuspend(ci->dev);
 639                        return 0;
 640                }
 641        }
 642        pm_runtime_put_sync(ci->dev);
 643        return 0;
 644}
 645
 646/*
 647 * Update fsm variables in each state if catching expected interrupts,
 648 * called by otg fsm isr.
 649 */
 650static void ci_otg_fsm_event(struct ci_hdrc *ci)
 651{
 652        u32 intr_sts, otg_bsess_vld, port_conn;
 653        struct otg_fsm *fsm = &ci->fsm;
 654
 655        intr_sts = hw_read_intr_status(ci);
 656        otg_bsess_vld = hw_read_otgsc(ci, OTGSC_BSV);
 657        port_conn = hw_read(ci, OP_PORTSC, PORTSC_CCS);
 658
 659        switch (ci->fsm.otg->state) {
 660        case OTG_STATE_A_WAIT_BCON:
 661                if (port_conn) {
 662                        fsm->b_conn = 1;
 663                        fsm->a_bus_req = 1;
 664                        ci_otg_queue_work(ci);
 665                }
 666                break;
 667        case OTG_STATE_B_IDLE:
 668                if (otg_bsess_vld && (intr_sts & USBi_PCI) && port_conn) {
 669                        fsm->b_sess_vld = 1;
 670                        ci_otg_queue_work(ci);
 671                }
 672                break;
 673        case OTG_STATE_B_PERIPHERAL:
 674                if ((intr_sts & USBi_SLI) && port_conn && otg_bsess_vld) {
 675                        ci_otg_add_timer(ci, B_AIDL_BDIS);
 676                } else if (intr_sts & USBi_PCI) {
 677                        ci_otg_del_timer(ci, B_AIDL_BDIS);
 678                        if (fsm->a_bus_suspend == 1)
 679                                fsm->a_bus_suspend = 0;
 680                }
 681                break;
 682        case OTG_STATE_B_HOST:
 683                if ((intr_sts & USBi_PCI) && !port_conn) {
 684                        fsm->a_conn = 0;
 685                        fsm->b_bus_req = 0;
 686                        ci_otg_queue_work(ci);
 687                }
 688                break;
 689        case OTG_STATE_A_PERIPHERAL:
 690                if (intr_sts & USBi_SLI) {
 691                         fsm->b_bus_suspend = 1;
 692                        /*
 693                         * Init a timer to know how long this suspend
 694                         * will continue, if time out, indicates B no longer
 695                         * wants to be host role
 696                         */
 697                         ci_otg_add_timer(ci, A_BIDL_ADIS);
 698                }
 699
 700                if (intr_sts & USBi_URI)
 701                        ci_otg_del_timer(ci, A_BIDL_ADIS);
 702
 703                if (intr_sts & USBi_PCI) {
 704                        if (fsm->b_bus_suspend == 1) {
 705                                ci_otg_del_timer(ci, A_BIDL_ADIS);
 706                                fsm->b_bus_suspend = 0;
 707                        }
 708                }
 709                break;
 710        case OTG_STATE_A_SUSPEND:
 711                if ((intr_sts & USBi_PCI) && !port_conn) {
 712                        fsm->b_conn = 0;
 713
 714                        /* if gadget driver is binded */
 715                        if (ci->driver) {
 716                                /* A device to be peripheral mode */
 717                                ci->gadget.is_a_peripheral = 1;
 718                        }
 719                        ci_otg_queue_work(ci);
 720                }
 721                break;
 722        case OTG_STATE_A_HOST:
 723                if ((intr_sts & USBi_PCI) && !port_conn) {
 724                        fsm->b_conn = 0;
 725                        ci_otg_queue_work(ci);
 726                }
 727                break;
 728        case OTG_STATE_B_WAIT_ACON:
 729                if ((intr_sts & USBi_PCI) && port_conn) {
 730                        fsm->a_conn = 1;
 731                        ci_otg_queue_work(ci);
 732                }
 733                break;
 734        default:
 735                break;
 736        }
 737}
 738
 739/*
 740 * ci_otg_irq - otg fsm related irq handling
 741 * and also update otg fsm variable by monitoring usb host and udc
 742 * state change interrupts.
 743 * @ci: ci_hdrc
 744 */
 745irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
 746{
 747        irqreturn_t retval =  IRQ_NONE;
 748        u32 otgsc, otg_int_src = 0;
 749        struct otg_fsm *fsm = &ci->fsm;
 750
 751        otgsc = hw_read_otgsc(ci, ~0);
 752        otg_int_src = otgsc & OTGSC_INT_STATUS_BITS & (otgsc >> 8);
 753        fsm->id = (otgsc & OTGSC_ID) ? 1 : 0;
 754
 755        if (otg_int_src) {
 756                if (otg_int_src & OTGSC_DPIS) {
 757                        hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
 758                        fsm->a_srp_det = 1;
 759                        fsm->a_bus_drop = 0;
 760                } else if (otg_int_src & OTGSC_IDIS) {
 761                        hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
 762                        if (fsm->id == 0) {
 763                                fsm->a_bus_drop = 0;
 764                                fsm->a_bus_req = 1;
 765                                ci->id_event = true;
 766                        }
 767                } else if (otg_int_src & OTGSC_BSVIS) {
 768                        hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
 769                        if (otgsc & OTGSC_BSV) {
 770                                fsm->b_sess_vld = 1;
 771                                ci_otg_del_timer(ci, B_SSEND_SRP);
 772                                ci_otg_del_timer(ci, B_SRP_FAIL);
 773                                fsm->b_ssend_srp = 0;
 774                        } else {
 775                                fsm->b_sess_vld = 0;
 776                                if (fsm->id)
 777                                        ci_otg_add_timer(ci, B_SSEND_SRP);
 778                        }
 779                } else if (otg_int_src & OTGSC_AVVIS) {
 780                        hw_write_otgsc(ci, OTGSC_AVVIS, OTGSC_AVVIS);
 781                        if (otgsc & OTGSC_AVV) {
 782                                fsm->a_vbus_vld = 1;
 783                        } else {
 784                                fsm->a_vbus_vld = 0;
 785                                fsm->b_conn = 0;
 786                        }
 787                }
 788                ci_otg_queue_work(ci);
 789                return IRQ_HANDLED;
 790        }
 791
 792        ci_otg_fsm_event(ci);
 793
 794        return retval;
 795}
 796
 797void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
 798{
 799        ci_otg_queue_work(ci);
 800}
 801
 802int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
 803{
 804        int retval = 0;
 805
 806        if (ci->phy)
 807                ci->otg.phy = ci->phy;
 808        else
 809                ci->otg.usb_phy = ci->usb_phy;
 810
 811        ci->otg.gadget = &ci->gadget;
 812        ci->fsm.otg = &ci->otg;
 813        ci->fsm.power_up = 1;
 814        ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
 815        ci->fsm.otg->state = OTG_STATE_UNDEFINED;
 816        ci->fsm.ops = &ci_otg_ops;
 817        ci->gadget.hnp_polling_support = 1;
 818        ci->fsm.host_req_flag = devm_kzalloc(ci->dev, 1, GFP_KERNEL);
 819        if (!ci->fsm.host_req_flag)
 820                return -ENOMEM;
 821
 822        mutex_init(&ci->fsm.lock);
 823
 824        retval = ci_otg_init_timers(ci);
 825        if (retval) {
 826                dev_err(ci->dev, "Couldn't init OTG timers\n");
 827                return retval;
 828        }
 829        ci->enabled_otg_timer_bits = 0;
 830        ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
 831
 832        retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
 833        if (retval < 0) {
 834                dev_dbg(ci->dev,
 835                        "Can't register sysfs attr group: %d\n", retval);
 836                return retval;
 837        }
 838
 839        /* Enable A vbus valid irq */
 840        hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
 841
 842        if (ci->fsm.id) {
 843                ci->fsm.b_ssend_srp =
 844                        hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
 845                ci->fsm.b_sess_vld =
 846                        hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
 847                /* Enable BSV irq */
 848                hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
 849        }
 850
 851        return 0;
 852}
 853
 854void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
 855{
 856        sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);
 857}
 858