blob: 00ab59d45da1bf6708b3d77e3cfcfe31ac4e2304 [file] [log] [blame]
Li Jun57677be2014-04-23 15:56:44 +08001/*
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 Jun4dcf7202014-04-23 15:56:50 +080016 *
17 * TODO List
18 * - ADP
19 * - OTG test device
Li Jun57677be2014-04-23 15:56:44 +080020 */
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 Jun826cfe72014-04-23 15:56:48 +080026#include <linux/regulator/consumer.h>
Li Jun57677be2014-04-23 15:56:44 +080027
28#include "ci.h"
29#include "bits.h"
30#include "otg.h"
31#include "otg_fsm.h"
32
Li Jun15f75de2014-04-23 15:56:51 +080033/* 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 }
70
Peter Chenbe6b0c12014-05-23 08:12:49 +080071 ci_otg_queue_work(ci);
Li Jun15f75de2014-04-23 15:56:51 +080072 mutex_unlock(&ci->fsm.lock);
73
74 return count;
75}
76static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req, set_a_bus_req);
77
78static ssize_t
79get_a_bus_drop(struct device *dev, struct device_attribute *attr, char *buf)
80{
81 char *next;
82 unsigned size, t;
83 struct ci_hdrc *ci = dev_get_drvdata(dev);
84
85 next = buf;
86 size = PAGE_SIZE;
87 t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_drop);
88 size -= t;
89 next += t;
90
91 return PAGE_SIZE - size;
92}
93
94static ssize_t
95set_a_bus_drop(struct device *dev, struct device_attribute *attr,
96 const char *buf, size_t count)
97{
98 struct ci_hdrc *ci = dev_get_drvdata(dev);
99
100 if (count > 2)
101 return -1;
102
103 mutex_lock(&ci->fsm.lock);
104 if (buf[0] == '0') {
105 ci->fsm.a_bus_drop = 0;
106 } else if (buf[0] == '1') {
107 ci->fsm.a_bus_drop = 1;
108 ci->fsm.a_bus_req = 0;
109 }
110
Peter Chenbe6b0c12014-05-23 08:12:49 +0800111 ci_otg_queue_work(ci);
Li Jun15f75de2014-04-23 15:56:51 +0800112 mutex_unlock(&ci->fsm.lock);
113
114 return count;
115}
116static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUSR, get_a_bus_drop,
117 set_a_bus_drop);
118
119static ssize_t
120get_b_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
121{
122 char *next;
123 unsigned size, t;
124 struct ci_hdrc *ci = dev_get_drvdata(dev);
125
126 next = buf;
127 size = PAGE_SIZE;
128 t = scnprintf(next, size, "%d\n", ci->fsm.b_bus_req);
129 size -= t;
130 next += t;
131
132 return PAGE_SIZE - size;
133}
134
135static ssize_t
136set_b_bus_req(struct device *dev, struct device_attribute *attr,
137 const char *buf, size_t count)
138{
139 struct ci_hdrc *ci = dev_get_drvdata(dev);
140
141 if (count > 2)
142 return -1;
143
144 mutex_lock(&ci->fsm.lock);
145 if (buf[0] == '0')
146 ci->fsm.b_bus_req = 0;
147 else if (buf[0] == '1')
148 ci->fsm.b_bus_req = 1;
149
Peter Chenbe6b0c12014-05-23 08:12:49 +0800150 ci_otg_queue_work(ci);
Li Jun15f75de2014-04-23 15:56:51 +0800151 mutex_unlock(&ci->fsm.lock);
152
153 return count;
154}
155static DEVICE_ATTR(b_bus_req, S_IRUGO | S_IWUSR, get_b_bus_req, set_b_bus_req);
156
157static ssize_t
158set_a_clr_err(struct device *dev, struct device_attribute *attr,
159 const char *buf, size_t count)
160{
161 struct ci_hdrc *ci = dev_get_drvdata(dev);
162
163 if (count > 2)
164 return -1;
165
166 mutex_lock(&ci->fsm.lock);
167 if (buf[0] == '1')
168 ci->fsm.a_clr_err = 1;
169
Peter Chenbe6b0c12014-05-23 08:12:49 +0800170 ci_otg_queue_work(ci);
Li Jun15f75de2014-04-23 15:56:51 +0800171 mutex_unlock(&ci->fsm.lock);
172
173 return count;
174}
175static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err);
176
177static struct attribute *inputs_attrs[] = {
178 &dev_attr_a_bus_req.attr,
179 &dev_attr_a_bus_drop.attr,
180 &dev_attr_b_bus_req.attr,
181 &dev_attr_a_clr_err.attr,
182 NULL,
183};
184
185static struct attribute_group inputs_attr_group = {
186 .name = "inputs",
187 .attrs = inputs_attrs,
188};
189
Li Jun826cfe72014-04-23 15:56:48 +0800190/*
Li Jun3a316ec2015-03-20 16:28:06 +0800191 * Keep this list in the same order as timers indexed
192 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
193 */
194static unsigned otg_timer_ms[] = {
195 TA_WAIT_VRISE,
196 TA_WAIT_VFALL,
197 TA_WAIT_BCON,
198 TA_AIDL_BDIS,
199 TB_ASE0_BRST,
200 TA_BIDL_ADIS,
201 TB_SE0_SRP,
202 TB_SRP_FAIL,
203 0,
204 TB_DATA_PLS,
205 TB_SSEND_SRP,
206};
207
208/*
Li Jun826cfe72014-04-23 15:56:48 +0800209 * Add timer to active timer list
210 */
Li Jun2f8a4672015-03-20 16:28:05 +0800211static void ci_otg_add_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
Li Jun826cfe72014-04-23 15:56:48 +0800212{
Li Jun3a316ec2015-03-20 16:28:06 +0800213 unsigned long flags, timer_sec, timer_nsec;
Li Jun826cfe72014-04-23 15:56:48 +0800214
Li Jun2f8a4672015-03-20 16:28:05 +0800215 if (t >= NUM_OTG_FSM_TIMERS)
Li Jun826cfe72014-04-23 15:56:48 +0800216 return;
217
Li Jun3a316ec2015-03-20 16:28:06 +0800218 spin_lock_irqsave(&ci->lock, flags);
219 timer_sec = otg_timer_ms[t] / MSEC_PER_SEC;
220 timer_nsec = (otg_timer_ms[t] % MSEC_PER_SEC) * NSEC_PER_MSEC;
221 ci->hr_timeouts[t] = ktime_add(ktime_get(),
222 ktime_set(timer_sec, timer_nsec));
223 ci->enabled_otg_timer_bits |= (1 << t);
224 if ((ci->next_otg_timer == NUM_OTG_FSM_TIMERS) ||
225 (ci->hr_timeouts[ci->next_otg_timer].tv64 >
226 ci->hr_timeouts[t].tv64)) {
227 ci->next_otg_timer = t;
228 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
229 ci->hr_timeouts[t], NSEC_PER_MSEC,
230 HRTIMER_MODE_ABS);
231 }
232 spin_unlock_irqrestore(&ci->lock, flags);
Li Jun826cfe72014-04-23 15:56:48 +0800233}
234
235/*
236 * Remove timer from active timer list
237 */
Li Jun2f8a4672015-03-20 16:28:05 +0800238static void ci_otg_del_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
Li Jun826cfe72014-04-23 15:56:48 +0800239{
Li Jun3a316ec2015-03-20 16:28:06 +0800240 unsigned long flags, enabled_timer_bits;
241 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
Li Jun826cfe72014-04-23 15:56:48 +0800242
Li Jun3a316ec2015-03-20 16:28:06 +0800243 if ((t >= NUM_OTG_FSM_TIMERS) ||
244 !(ci->enabled_otg_timer_bits & (1 << t)))
Li Jun826cfe72014-04-23 15:56:48 +0800245 return;
246
Li Jun3a316ec2015-03-20 16:28:06 +0800247 spin_lock_irqsave(&ci->lock, flags);
248 ci->enabled_otg_timer_bits &= ~(1 << t);
249 if (ci->next_otg_timer == t) {
250 if (ci->enabled_otg_timer_bits == 0) {
251 /* No enabled timers after delete it */
252 hrtimer_cancel(&ci->otg_fsm_hrtimer);
253 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
254 } else {
255 /* Find the next timer */
256 enabled_timer_bits = ci->enabled_otg_timer_bits;
257 for_each_set_bit(cur_timer, &enabled_timer_bits,
258 NUM_OTG_FSM_TIMERS) {
259 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
260 (ci->hr_timeouts[next_timer].tv64 <
261 ci->hr_timeouts[cur_timer].tv64))
262 next_timer = cur_timer;
263 }
Li Jun4dcf7202014-04-23 15:56:50 +0800264 }
265 }
Li Jun3a316ec2015-03-20 16:28:06 +0800266 if (next_timer != NUM_OTG_FSM_TIMERS) {
267 ci->next_otg_timer = next_timer;
268 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
269 ci->hr_timeouts[next_timer], NSEC_PER_MSEC,
270 HRTIMER_MODE_ABS);
Li Jun961ea492015-02-11 12:45:03 +0800271 }
Li Jun3a316ec2015-03-20 16:28:06 +0800272 spin_unlock_irqrestore(&ci->lock, flags);
Li Jun4dcf7202014-04-23 15:56:50 +0800273}
274
Li Jun3a316ec2015-03-20 16:28:06 +0800275/* OTG FSM timer handlers */
276static int a_wait_vrise_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800277{
Li Jun3a316ec2015-03-20 16:28:06 +0800278 ci->fsm.a_wait_vrise_tmout = 1;
279 return 0;
Li June287b672014-04-23 15:56:49 +0800280}
281
Li Jun3a316ec2015-03-20 16:28:06 +0800282static int a_wait_vfall_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800283{
Li Jun3a316ec2015-03-20 16:28:06 +0800284 ci->fsm.a_wait_vfall_tmout = 1;
285 return 0;
Li June287b672014-04-23 15:56:49 +0800286}
287
Li Jun3a316ec2015-03-20 16:28:06 +0800288static int a_wait_bcon_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800289{
Li Jun3a316ec2015-03-20 16:28:06 +0800290 ci->fsm.a_wait_bcon_tmout = 1;
291 return 0;
Li June287b672014-04-23 15:56:49 +0800292}
293
Li Jun3a316ec2015-03-20 16:28:06 +0800294static int a_aidl_bdis_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800295{
Li Jun3a316ec2015-03-20 16:28:06 +0800296 ci->fsm.a_aidl_bdis_tmout = 1;
297 return 0;
Li June287b672014-04-23 15:56:49 +0800298}
299
Li Jun3a316ec2015-03-20 16:28:06 +0800300static int b_ase0_brst_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800301{
Li Jun3a316ec2015-03-20 16:28:06 +0800302 ci->fsm.b_ase0_brst_tmout = 1;
303 return 0;
304}
Li June287b672014-04-23 15:56:49 +0800305
Li Jun3a316ec2015-03-20 16:28:06 +0800306static int a_bidl_adis_tmout(struct ci_hdrc *ci)
307{
308 ci->fsm.a_bidl_adis_tmout = 1;
309 return 0;
310}
311
312static int b_se0_srp_tmout(struct ci_hdrc *ci)
313{
314 ci->fsm.b_se0_srp = 1;
315 return 0;
316}
317
318static int b_srp_fail_tmout(struct ci_hdrc *ci)
319{
320 ci->fsm.b_srp_done = 1;
321 return 1;
322}
323
324static int b_data_pls_tmout(struct ci_hdrc *ci)
325{
Li June287b672014-04-23 15:56:49 +0800326 ci->fsm.b_srp_done = 1;
327 ci->fsm.b_bus_req = 0;
328 if (ci->fsm.power_up)
329 ci->fsm.power_up = 0;
Li June287b672014-04-23 15:56:49 +0800330 hw_write_otgsc(ci, OTGSC_HABA, 0);
Li Jun3a316ec2015-03-20 16:28:06 +0800331 pm_runtime_put(ci->dev);
332 return 0;
333}
Li June287b672014-04-23 15:56:49 +0800334
Li Jun3a316ec2015-03-20 16:28:06 +0800335static int b_ssend_srp_tmout(struct ci_hdrc *ci)
336{
337 ci->fsm.b_ssend_srp = 1;
338 /* only vbus fall below B_sess_vld in b_idle state */
339 if (ci->fsm.otg->state == OTG_STATE_B_IDLE)
340 return 0;
341 else
342 return 1;
343}
344
345/*
346 * Keep this list in the same order as timers indexed
347 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
348 */
349static int (*otg_timer_handlers[])(struct ci_hdrc *) = {
350 a_wait_vrise_tmout, /* A_WAIT_VRISE */
351 a_wait_vfall_tmout, /* A_WAIT_VFALL */
352 a_wait_bcon_tmout, /* A_WAIT_BCON */
353 a_aidl_bdis_tmout, /* A_AIDL_BDIS */
354 b_ase0_brst_tmout, /* B_ASE0_BRST */
355 a_bidl_adis_tmout, /* A_BIDL_ADIS */
356 b_se0_srp_tmout, /* B_SE0_SRP */
357 b_srp_fail_tmout, /* B_SRP_FAIL */
358 NULL, /* A_WAIT_ENUM */
359 b_data_pls_tmout, /* B_DATA_PLS */
360 b_ssend_srp_tmout, /* B_SSEND_SRP */
361};
362
363/*
364 * Enable the next nearest enabled timer if have
365 */
366static enum hrtimer_restart ci_otg_hrtimer_func(struct hrtimer *t)
367{
368 struct ci_hdrc *ci = container_of(t, struct ci_hdrc, otg_fsm_hrtimer);
369 ktime_t now, *timeout;
370 unsigned long enabled_timer_bits;
371 unsigned long flags;
372 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
373 int ret = -EINVAL;
374
375 spin_lock_irqsave(&ci->lock, flags);
376 enabled_timer_bits = ci->enabled_otg_timer_bits;
377 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
378
379 now = ktime_get();
380 for_each_set_bit(cur_timer, &enabled_timer_bits, NUM_OTG_FSM_TIMERS) {
381 if (now.tv64 >= ci->hr_timeouts[cur_timer].tv64) {
382 ci->enabled_otg_timer_bits &= ~(1 << cur_timer);
383 if (otg_timer_handlers[cur_timer])
384 ret = otg_timer_handlers[cur_timer](ci);
385 } else {
386 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
387 (ci->hr_timeouts[cur_timer].tv64 <
388 ci->hr_timeouts[next_timer].tv64))
389 next_timer = cur_timer;
390 }
391 }
392 /* Enable the next nearest timer */
393 if (next_timer < NUM_OTG_FSM_TIMERS) {
394 timeout = &ci->hr_timeouts[next_timer];
395 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer, *timeout,
396 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
397 ci->next_otg_timer = next_timer;
398 }
399 spin_unlock_irqrestore(&ci->lock, flags);
400
401 if (!ret)
402 ci_otg_queue_work(ci);
403
404 return HRTIMER_NORESTART;
Li June287b672014-04-23 15:56:49 +0800405}
406
407/* Initialize timers */
408static int ci_otg_init_timers(struct ci_hdrc *ci)
409{
Li Jun3a316ec2015-03-20 16:28:06 +0800410 hrtimer_init(&ci->otg_fsm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
411 ci->otg_fsm_hrtimer.function = ci_otg_hrtimer_func;
Li June287b672014-04-23 15:56:49 +0800412
Li June287b672014-04-23 15:56:49 +0800413 return 0;
414}
415
Li Jun826cfe72014-04-23 15:56:48 +0800416/* -------------------------------------------------------------*/
417/* Operations that will be called from OTG Finite State Machine */
418/* -------------------------------------------------------------*/
419static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
420{
421 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
422
423 if (t < NUM_OTG_FSM_TIMERS)
424 ci_otg_add_timer(ci, t);
425 return;
426}
427
428static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
429{
430 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
431
432 if (t < NUM_OTG_FSM_TIMERS)
433 ci_otg_del_timer(ci, t);
434 return;
435}
436
437/*
438 * A-device drive vbus: turn on vbus regulator and enable port power
439 * Data pulse irq should be disabled while vbus is on.
440 */
441static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
442{
443 int ret;
444 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
445
446 if (on) {
447 /* Enable power power */
448 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
449 PORTSC_PP);
450 if (ci->platdata->reg_vbus) {
451 ret = regulator_enable(ci->platdata->reg_vbus);
452 if (ret) {
453 dev_err(ci->dev,
454 "Failed to enable vbus regulator, ret=%d\n",
455 ret);
456 return;
457 }
458 }
459 /* Disable data pulse irq */
460 hw_write_otgsc(ci, OTGSC_DPIE, 0);
461
462 fsm->a_srp_det = 0;
463 fsm->power_up = 0;
464 } else {
465 if (ci->platdata->reg_vbus)
466 regulator_disable(ci->platdata->reg_vbus);
467
468 fsm->a_bus_drop = 1;
469 fsm->a_bus_req = 0;
470 }
471}
472
473/*
474 * Control data line by Run Stop bit.
475 */
476static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
477{
478 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
479
480 if (on)
481 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
482 else
483 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
484}
485
486/*
487 * Generate SOF by host.
488 * This is controlled through suspend/resume the port.
489 * In host mode, controller will automatically send SOF.
490 * Suspend will block the data on the port.
491 */
492static void ci_otg_loc_sof(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_PORTSC, PORTSC_W1C_BITS | PORTSC_FPR,
498 PORTSC_FPR);
499 else
500 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_SUSP,
501 PORTSC_SUSP);
502}
503
504/*
505 * Start SRP pulsing by data-line pulsing,
506 * no v-bus pulsing followed
507 */
508static void ci_otg_start_pulse(struct otg_fsm *fsm)
509{
510 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
511
512 /* Hardware Assistant Data pulse */
513 hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
514
Li Jun3a316ec2015-03-20 16:28:06 +0800515 pm_runtime_get(ci->dev);
Li Jun826cfe72014-04-23 15:56:48 +0800516 ci_otg_add_timer(ci, B_DATA_PLS);
517}
518
519static int ci_otg_start_host(struct otg_fsm *fsm, int on)
520{
521 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
522
Li Jun826cfe72014-04-23 15:56:48 +0800523 if (on) {
524 ci_role_stop(ci);
525 ci_role_start(ci, CI_ROLE_HOST);
526 } else {
527 ci_role_stop(ci);
Li Jun826cfe72014-04-23 15:56:48 +0800528 ci_role_start(ci, CI_ROLE_GADGET);
529 }
Li Jun826cfe72014-04-23 15:56:48 +0800530 return 0;
531}
532
533static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
534{
535 struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
536
Li Jun826cfe72014-04-23 15:56:48 +0800537 if (on)
538 usb_gadget_vbus_connect(&ci->gadget);
539 else
540 usb_gadget_vbus_disconnect(&ci->gadget);
Li Jun826cfe72014-04-23 15:56:48 +0800541
542 return 0;
543}
544
545static struct otg_fsm_ops ci_otg_ops = {
546 .drv_vbus = ci_otg_drv_vbus,
547 .loc_conn = ci_otg_loc_conn,
548 .loc_sof = ci_otg_loc_sof,
549 .start_pulse = ci_otg_start_pulse,
550 .add_timer = ci_otg_fsm_add_timer,
551 .del_timer = ci_otg_fsm_del_timer,
552 .start_host = ci_otg_start_host,
553 .start_gadget = ci_otg_start_gadget,
554};
555
Li Jun4dcf7202014-04-23 15:56:50 +0800556int ci_otg_fsm_work(struct ci_hdrc *ci)
557{
558 /*
559 * Don't do fsm transition for B device
560 * when there is no gadget class driver
561 */
562 if (ci->fsm.id && !(ci->driver) &&
Antoine Tenarte47d9252014-10-30 18:41:13 +0100563 ci->fsm.otg->state < OTG_STATE_A_IDLE)
Li Jun4dcf7202014-04-23 15:56:50 +0800564 return 0;
565
Li Jun961ea492015-02-11 12:45:03 +0800566 pm_runtime_get_sync(ci->dev);
Li Jun4dcf7202014-04-23 15:56:50 +0800567 if (otg_statemachine(&ci->fsm)) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100568 if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
Li Jun4dcf7202014-04-23 15:56:50 +0800569 /*
570 * Further state change for cases:
571 * a_idle to b_idle; or
572 * a_idle to a_wait_vrise due to ID change(1->0), so
573 * B-dev becomes A-dev can try to start new session
574 * consequently; or
575 * a_idle to a_wait_vrise when power up
576 */
577 if ((ci->fsm.id) || (ci->id_event) ||
Li Jun3a316ec2015-03-20 16:28:06 +0800578 (ci->fsm.power_up)) {
Peter Chenbe6b0c12014-05-23 08:12:49 +0800579 ci_otg_queue_work(ci);
Li Jun3a316ec2015-03-20 16:28:06 +0800580 } else {
581 /* Enable data pulse irq */
582 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS |
583 PORTSC_PP, 0);
584 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
585 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
586 }
Li Jun4dcf7202014-04-23 15:56:50 +0800587 if (ci->id_event)
588 ci->id_event = false;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100589 } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) {
Li Jun4dcf7202014-04-23 15:56:50 +0800590 if (ci->fsm.b_sess_vld) {
591 ci->fsm.power_up = 0;
592 /*
593 * Further transite to b_periphearl state
594 * when register gadget driver with vbus on
595 */
Peter Chenbe6b0c12014-05-23 08:12:49 +0800596 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800597 }
Li Jun961ea492015-02-11 12:45:03 +0800598 } else if (ci->fsm.otg->state == OTG_STATE_A_HOST) {
599 pm_runtime_mark_last_busy(ci->dev);
600 pm_runtime_put_autosuspend(ci->dev);
601 return 0;
Li Jun4dcf7202014-04-23 15:56:50 +0800602 }
603 }
Li Jun961ea492015-02-11 12:45:03 +0800604 pm_runtime_put_sync(ci->dev);
Li Jun4dcf7202014-04-23 15:56:50 +0800605 return 0;
606}
607
608/*
609 * Update fsm variables in each state if catching expected interrupts,
610 * called by otg fsm isr.
611 */
612static void ci_otg_fsm_event(struct ci_hdrc *ci)
613{
614 u32 intr_sts, otg_bsess_vld, port_conn;
615 struct otg_fsm *fsm = &ci->fsm;
616
617 intr_sts = hw_read_intr_status(ci);
618 otg_bsess_vld = hw_read_otgsc(ci, OTGSC_BSV);
619 port_conn = hw_read(ci, OP_PORTSC, PORTSC_CCS);
620
Antoine Tenarte47d9252014-10-30 18:41:13 +0100621 switch (ci->fsm.otg->state) {
Li Jun4dcf7202014-04-23 15:56:50 +0800622 case OTG_STATE_A_WAIT_BCON:
623 if (port_conn) {
624 fsm->b_conn = 1;
625 fsm->a_bus_req = 1;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800626 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800627 }
628 break;
629 case OTG_STATE_B_IDLE:
630 if (otg_bsess_vld && (intr_sts & USBi_PCI) && port_conn) {
631 fsm->b_sess_vld = 1;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800632 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800633 }
634 break;
635 case OTG_STATE_B_PERIPHERAL:
636 if ((intr_sts & USBi_SLI) && port_conn && otg_bsess_vld) {
637 fsm->a_bus_suspend = 1;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800638 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800639 } else if (intr_sts & USBi_PCI) {
640 if (fsm->a_bus_suspend == 1)
641 fsm->a_bus_suspend = 0;
642 }
643 break;
644 case OTG_STATE_B_HOST:
645 if ((intr_sts & USBi_PCI) && !port_conn) {
646 fsm->a_conn = 0;
647 fsm->b_bus_req = 0;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800648 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800649 }
650 break;
651 case OTG_STATE_A_PERIPHERAL:
652 if (intr_sts & USBi_SLI) {
653 fsm->b_bus_suspend = 1;
654 /*
655 * Init a timer to know how long this suspend
Mickael Maison66294672014-11-26 13:44:38 +0800656 * will continue, if time out, indicates B no longer
Li Jun4dcf7202014-04-23 15:56:50 +0800657 * wants to be host role
658 */
659 ci_otg_add_timer(ci, A_BIDL_ADIS);
660 }
661
662 if (intr_sts & USBi_URI)
663 ci_otg_del_timer(ci, A_BIDL_ADIS);
664
665 if (intr_sts & USBi_PCI) {
666 if (fsm->b_bus_suspend == 1) {
667 ci_otg_del_timer(ci, A_BIDL_ADIS);
668 fsm->b_bus_suspend = 0;
669 }
670 }
671 break;
672 case OTG_STATE_A_SUSPEND:
673 if ((intr_sts & USBi_PCI) && !port_conn) {
674 fsm->b_conn = 0;
675
676 /* if gadget driver is binded */
677 if (ci->driver) {
678 /* A device to be peripheral mode */
679 ci->gadget.is_a_peripheral = 1;
680 }
Peter Chenbe6b0c12014-05-23 08:12:49 +0800681 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800682 }
683 break;
684 case OTG_STATE_A_HOST:
685 if ((intr_sts & USBi_PCI) && !port_conn) {
686 fsm->b_conn = 0;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800687 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800688 }
689 break;
690 case OTG_STATE_B_WAIT_ACON:
691 if ((intr_sts & USBi_PCI) && port_conn) {
692 fsm->a_conn = 1;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800693 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800694 }
695 break;
696 default:
697 break;
698 }
699}
700
701/*
702 * ci_otg_irq - otg fsm related irq handling
703 * and also update otg fsm variable by monitoring usb host and udc
704 * state change interrupts.
705 * @ci: ci_hdrc
706 */
707irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
708{
709 irqreturn_t retval = IRQ_NONE;
710 u32 otgsc, otg_int_src = 0;
711 struct otg_fsm *fsm = &ci->fsm;
712
713 otgsc = hw_read_otgsc(ci, ~0);
714 otg_int_src = otgsc & OTGSC_INT_STATUS_BITS & (otgsc >> 8);
715 fsm->id = (otgsc & OTGSC_ID) ? 1 : 0;
716
717 if (otg_int_src) {
Li Jun3a316ec2015-03-20 16:28:06 +0800718 if (otg_int_src & OTGSC_DPIS) {
Li Jun4dcf7202014-04-23 15:56:50 +0800719 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
720 fsm->a_srp_det = 1;
721 fsm->a_bus_drop = 0;
722 } else if (otg_int_src & OTGSC_IDIS) {
723 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
724 if (fsm->id == 0) {
725 fsm->a_bus_drop = 0;
726 fsm->a_bus_req = 1;
727 ci->id_event = true;
728 }
729 } else if (otg_int_src & OTGSC_BSVIS) {
730 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
731 if (otgsc & OTGSC_BSV) {
732 fsm->b_sess_vld = 1;
733 ci_otg_del_timer(ci, B_SSEND_SRP);
734 ci_otg_del_timer(ci, B_SRP_FAIL);
735 fsm->b_ssend_srp = 0;
736 } else {
737 fsm->b_sess_vld = 0;
738 if (fsm->id)
739 ci_otg_add_timer(ci, B_SSEND_SRP);
740 }
741 } else if (otg_int_src & OTGSC_AVVIS) {
742 hw_write_otgsc(ci, OTGSC_AVVIS, OTGSC_AVVIS);
743 if (otgsc & OTGSC_AVV) {
744 fsm->a_vbus_vld = 1;
745 } else {
746 fsm->a_vbus_vld = 0;
747 fsm->b_conn = 0;
748 }
749 }
Peter Chenbe6b0c12014-05-23 08:12:49 +0800750 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800751 return IRQ_HANDLED;
752 }
753
754 ci_otg_fsm_event(ci);
755
756 return retval;
757}
758
759void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
760{
Peter Chenbe6b0c12014-05-23 08:12:49 +0800761 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800762}
763
Li Jun57677be2014-04-23 15:56:44 +0800764int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
765{
Li June287b672014-04-23 15:56:49 +0800766 int retval = 0;
Li Jun57677be2014-04-23 15:56:44 +0800767
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100768 if (ci->phy)
769 ci->otg.phy = ci->phy;
770 else
771 ci->otg.usb_phy = ci->usb_phy;
Li Jun57677be2014-04-23 15:56:44 +0800772
Antoine Tenartef44cb42014-10-30 18:41:16 +0100773 ci->otg.gadget = &ci->gadget;
774 ci->fsm.otg = &ci->otg;
Li Jun57677be2014-04-23 15:56:44 +0800775 ci->fsm.power_up = 1;
776 ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100777 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
Li Jun826cfe72014-04-23 15:56:48 +0800778 ci->fsm.ops = &ci_otg_ops;
Li Jun57677be2014-04-23 15:56:44 +0800779
780 mutex_init(&ci->fsm.lock);
781
Li June287b672014-04-23 15:56:49 +0800782 retval = ci_otg_init_timers(ci);
783 if (retval) {
784 dev_err(ci->dev, "Couldn't init OTG timers\n");
785 return retval;
786 }
Li Jun3a316ec2015-03-20 16:28:06 +0800787 ci->enabled_otg_timer_bits = 0;
788 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
Li June287b672014-04-23 15:56:49 +0800789
Li Jun15f75de2014-04-23 15:56:51 +0800790 retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
791 if (retval < 0) {
792 dev_dbg(ci->dev,
793 "Can't register sysfs attr group: %d\n", retval);
794 return retval;
795 }
796
Li Jun57677be2014-04-23 15:56:44 +0800797 /* Enable A vbus valid irq */
798 hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
799
800 if (ci->fsm.id) {
801 ci->fsm.b_ssend_srp =
802 hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
803 ci->fsm.b_sess_vld =
804 hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
805 /* Enable BSV irq */
806 hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
807 }
808
809 return 0;
810}
Li Jun15f75de2014-04-23 15:56:51 +0800811
812void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
813{
814 sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);
815}