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