blob: 9a963a7494675f932f7ec69145e99dfbbc744084 [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;
Li Jun2dfb46b2016-02-19 10:04:45 +080069 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 Jun15f75de2014-04-23 15:56:51 +080074 }
75
Peter Chenbe6b0c12014-05-23 08:12:49 +080076 ci_otg_queue_work(ci);
Li Jun15f75de2014-04-23 15:56:51 +080077 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
Peter Chenbe6b0c12014-05-23 08:12:49 +0800116 ci_otg_queue_work(ci);
Li Jun15f75de2014-04-23 15:56:51 +0800117 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;
Li Jun2dfb46b2016-02-19 10:04:45 +0800152 else if (buf[0] == '1') {
Li Jun15f75de2014-04-23 15:56:51 +0800153 ci->fsm.b_bus_req = 1;
Li Jun2dfb46b2016-02-19 10:04:45 +0800154 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 Jun15f75de2014-04-23 15:56:51 +0800160
Peter Chenbe6b0c12014-05-23 08:12:49 +0800161 ci_otg_queue_work(ci);
Li Jun15f75de2014-04-23 15:56:51 +0800162 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
Peter Chenbe6b0c12014-05-23 08:12:49 +0800181 ci_otg_queue_work(ci);
Li Jun15f75de2014-04-23 15:56:51 +0800182 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
Li Jun826cfe72014-04-23 15:56:48 +0800201/*
Li Jun3a316ec2015-03-20 16:28:06 +0800202 * 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_SE0_SRP,
213 TB_SRP_FAIL,
214 0,
215 TB_DATA_PLS,
216 TB_SSEND_SRP,
217};
218
219/*
Li Jun826cfe72014-04-23 15:56:48 +0800220 * Add timer to active timer list
221 */
Li Jun2f8a4672015-03-20 16:28:05 +0800222static void ci_otg_add_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
Li Jun826cfe72014-04-23 15:56:48 +0800223{
Li Jun3a316ec2015-03-20 16:28:06 +0800224 unsigned long flags, timer_sec, timer_nsec;
Li Jun826cfe72014-04-23 15:56:48 +0800225
Li Jun2f8a4672015-03-20 16:28:05 +0800226 if (t >= NUM_OTG_FSM_TIMERS)
Li Jun826cfe72014-04-23 15:56:48 +0800227 return;
228
Li Jun3a316ec2015-03-20 16:28:06 +0800229 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 Jun826cfe72014-04-23 15:56:48 +0800244}
245
246/*
247 * Remove timer from active timer list
248 */
Li Jun2f8a4672015-03-20 16:28:05 +0800249static void ci_otg_del_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
Li Jun826cfe72014-04-23 15:56:48 +0800250{
Li Jun3a316ec2015-03-20 16:28:06 +0800251 unsigned long flags, enabled_timer_bits;
252 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
Li Jun826cfe72014-04-23 15:56:48 +0800253
Li Jun3a316ec2015-03-20 16:28:06 +0800254 if ((t >= NUM_OTG_FSM_TIMERS) ||
255 !(ci->enabled_otg_timer_bits & (1 << t)))
Li Jun826cfe72014-04-23 15:56:48 +0800256 return;
257
Li Jun3a316ec2015-03-20 16:28:06 +0800258 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 Jun4dcf7202014-04-23 15:56:50 +0800275 }
276 }
Li Jun3a316ec2015-03-20 16:28:06 +0800277 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 Jun961ea492015-02-11 12:45:03 +0800282 }
Li Jun3a316ec2015-03-20 16:28:06 +0800283 spin_unlock_irqrestore(&ci->lock, flags);
Li Jun4dcf7202014-04-23 15:56:50 +0800284}
285
Li Jun3a316ec2015-03-20 16:28:06 +0800286/* OTG FSM timer handlers */
287static int a_wait_vrise_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800288{
Li Jun3a316ec2015-03-20 16:28:06 +0800289 ci->fsm.a_wait_vrise_tmout = 1;
290 return 0;
Li June287b672014-04-23 15:56:49 +0800291}
292
Li Jun3a316ec2015-03-20 16:28:06 +0800293static int a_wait_vfall_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800294{
Li Jun3a316ec2015-03-20 16:28:06 +0800295 ci->fsm.a_wait_vfall_tmout = 1;
296 return 0;
Li June287b672014-04-23 15:56:49 +0800297}
298
Li Jun3a316ec2015-03-20 16:28:06 +0800299static int a_wait_bcon_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800300{
Li Jun3a316ec2015-03-20 16:28:06 +0800301 ci->fsm.a_wait_bcon_tmout = 1;
302 return 0;
Li June287b672014-04-23 15:56:49 +0800303}
304
Li Jun3a316ec2015-03-20 16:28:06 +0800305static int a_aidl_bdis_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800306{
Li Jun3a316ec2015-03-20 16:28:06 +0800307 ci->fsm.a_aidl_bdis_tmout = 1;
308 return 0;
Li June287b672014-04-23 15:56:49 +0800309}
310
Li Jun3a316ec2015-03-20 16:28:06 +0800311static int b_ase0_brst_tmout(struct ci_hdrc *ci)
Li June287b672014-04-23 15:56:49 +0800312{
Li Jun3a316ec2015-03-20 16:28:06 +0800313 ci->fsm.b_ase0_brst_tmout = 1;
314 return 0;
315}
Li June287b672014-04-23 15:56:49 +0800316
Li Jun3a316ec2015-03-20 16:28:06 +0800317static int a_bidl_adis_tmout(struct ci_hdrc *ci)
318{
319 ci->fsm.a_bidl_adis_tmout = 1;
320 return 0;
321}
322
323static int b_se0_srp_tmout(struct ci_hdrc *ci)
324{
325 ci->fsm.b_se0_srp = 1;
326 return 0;
327}
328
329static int b_srp_fail_tmout(struct ci_hdrc *ci)
330{
331 ci->fsm.b_srp_done = 1;
332 return 1;
333}
334
335static int b_data_pls_tmout(struct ci_hdrc *ci)
336{
Li June287b672014-04-23 15:56:49 +0800337 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 June287b672014-04-23 15:56:49 +0800341 hw_write_otgsc(ci, OTGSC_HABA, 0);
Li Jun3a316ec2015-03-20 16:28:06 +0800342 pm_runtime_put(ci->dev);
343 return 0;
344}
Li June287b672014-04-23 15:56:49 +0800345
Li Jun3a316ec2015-03-20 16:28:06 +0800346static 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 */
360static 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 */
377static 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 June287b672014-04-23 15:56:49 +0800416}
417
418/* Initialize timers */
419static int ci_otg_init_timers(struct ci_hdrc *ci)
420{
Li Jun3a316ec2015-03-20 16:28:06 +0800421 hrtimer_init(&ci->otg_fsm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
422 ci->otg_fsm_hrtimer.function = ci_otg_hrtimer_func;
Li June287b672014-04-23 15:56:49 +0800423
Li June287b672014-04-23 15:56:49 +0800424 return 0;
425}
426
Li Jun826cfe72014-04-23 15:56:48 +0800427/* -------------------------------------------------------------*/
428/* Operations that will be called from OTG Finite State Machine */
429/* -------------------------------------------------------------*/
430static 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
439static 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 */
452static 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 */
487static 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 Jun826cfe72014-04-23 15:56:48 +0800499 * In host mode, controller will automatically send SOF.
500 * Suspend will block the data on the port.
Li Jun8c100e72015-12-15 17:51:29 +0800501 *
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 Jun826cfe72014-04-23 15:56:48 +0800505 */
506static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
507{
Li Jun8c100e72015-12-15 17:51:29 +0800508 struct usb_device *udev;
Li Jun826cfe72014-04-23 15:56:48 +0800509
Li Jun8c100e72015-12-15 17:51:29 +0800510 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 Jun826cfe72014-04-23 15:56:48 +0800523}
524
525/*
526 * Start SRP pulsing by data-line pulsing,
527 * no v-bus pulsing followed
528 */
529static 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 Jun3a316ec2015-03-20 16:28:06 +0800536 pm_runtime_get(ci->dev);
Li Jun826cfe72014-04-23 15:56:48 +0800537 ci_otg_add_timer(ci, B_DATA_PLS);
538}
539
540static 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 Jun826cfe72014-04-23 15:56:48 +0800544 if (on) {
545 ci_role_stop(ci);
546 ci_role_start(ci, CI_ROLE_HOST);
547 } else {
548 ci_role_stop(ci);
Li Jun826cfe72014-04-23 15:56:48 +0800549 ci_role_start(ci, CI_ROLE_GADGET);
550 }
Li Jun826cfe72014-04-23 15:56:48 +0800551 return 0;
552}
553
554static 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 Jun826cfe72014-04-23 15:56:48 +0800558 if (on)
559 usb_gadget_vbus_connect(&ci->gadget);
560 else
561 usb_gadget_vbus_disconnect(&ci->gadget);
Li Jun826cfe72014-04-23 15:56:48 +0800562
563 return 0;
564}
565
566static 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 Jun4dcf7202014-04-23 15:56:50 +0800577int 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 Tenarte47d9252014-10-30 18:41:13 +0100584 ci->fsm.otg->state < OTG_STATE_A_IDLE)
Li Jun4dcf7202014-04-23 15:56:50 +0800585 return 0;
586
Li Jun961ea492015-02-11 12:45:03 +0800587 pm_runtime_get_sync(ci->dev);
Li Jun4dcf7202014-04-23 15:56:50 +0800588 if (otg_statemachine(&ci->fsm)) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100589 if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
Li Jun4dcf7202014-04-23 15:56:50 +0800590 /*
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 Jun3a316ec2015-03-20 16:28:06 +0800599 (ci->fsm.power_up)) {
Peter Chenbe6b0c12014-05-23 08:12:49 +0800600 ci_otg_queue_work(ci);
Li Jun3a316ec2015-03-20 16:28:06 +0800601 } 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 Jun4dcf7202014-04-23 15:56:50 +0800608 if (ci->id_event)
609 ci->id_event = false;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100610 } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) {
Li Jun4dcf7202014-04-23 15:56:50 +0800611 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 Chenbe6b0c12014-05-23 08:12:49 +0800617 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800618 }
Li Jun961ea492015-02-11 12:45:03 +0800619 } 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 Jun4dcf7202014-04-23 15:56:50 +0800623 }
624 }
Li Jun961ea492015-02-11 12:45:03 +0800625 pm_runtime_put_sync(ci->dev);
Li Jun4dcf7202014-04-23 15:56:50 +0800626 return 0;
627}
628
629/*
630 * Update fsm variables in each state if catching expected interrupts,
631 * called by otg fsm isr.
632 */
633static 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 Tenarte47d9252014-10-30 18:41:13 +0100642 switch (ci->fsm.otg->state) {
Li Jun4dcf7202014-04-23 15:56:50 +0800643 case OTG_STATE_A_WAIT_BCON:
644 if (port_conn) {
645 fsm->b_conn = 1;
646 fsm->a_bus_req = 1;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800647 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800648 }
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 Chenbe6b0c12014-05-23 08:12:49 +0800653 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800654 }
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 Chenbe6b0c12014-05-23 08:12:49 +0800659 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800660 } 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 Chenbe6b0c12014-05-23 08:12:49 +0800669 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800670 }
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 Maison66294672014-11-26 13:44:38 +0800677 * will continue, if time out, indicates B no longer
Li Jun4dcf7202014-04-23 15:56:50 +0800678 * 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 Chenbe6b0c12014-05-23 08:12:49 +0800702 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800703 }
704 break;
705 case OTG_STATE_A_HOST:
706 if ((intr_sts & USBi_PCI) && !port_conn) {
707 fsm->b_conn = 0;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800708 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800709 }
710 break;
711 case OTG_STATE_B_WAIT_ACON:
712 if ((intr_sts & USBi_PCI) && port_conn) {
713 fsm->a_conn = 1;
Peter Chenbe6b0c12014-05-23 08:12:49 +0800714 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800715 }
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 */
728irqreturn_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 Jun3a316ec2015-03-20 16:28:06 +0800739 if (otg_int_src & OTGSC_DPIS) {
Li Jun4dcf7202014-04-23 15:56:50 +0800740 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 Chenbe6b0c12014-05-23 08:12:49 +0800771 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800772 return IRQ_HANDLED;
773 }
774
775 ci_otg_fsm_event(ci);
776
777 return retval;
778}
779
780void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
781{
Peter Chenbe6b0c12014-05-23 08:12:49 +0800782 ci_otg_queue_work(ci);
Li Jun4dcf7202014-04-23 15:56:50 +0800783}
784
Li Jun57677be2014-04-23 15:56:44 +0800785int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
786{
Li June287b672014-04-23 15:56:49 +0800787 int retval = 0;
Li Jun57677be2014-04-23 15:56:44 +0800788
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100789 if (ci->phy)
790 ci->otg.phy = ci->phy;
791 else
792 ci->otg.usb_phy = ci->usb_phy;
Li Jun57677be2014-04-23 15:56:44 +0800793
Antoine Tenartef44cb42014-10-30 18:41:16 +0100794 ci->otg.gadget = &ci->gadget;
795 ci->fsm.otg = &ci->otg;
Li Jun57677be2014-04-23 15:56:44 +0800796 ci->fsm.power_up = 1;
797 ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100798 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
Li Jun826cfe72014-04-23 15:56:48 +0800799 ci->fsm.ops = &ci_otg_ops;
Li Jun75d2f752016-02-19 10:04:46 +0800800 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 Jun57677be2014-04-23 15:56:44 +0800804
805 mutex_init(&ci->fsm.lock);
806
Li June287b672014-04-23 15:56:49 +0800807 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 Jun3a316ec2015-03-20 16:28:06 +0800812 ci->enabled_otg_timer_bits = 0;
813 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
Li June287b672014-04-23 15:56:49 +0800814
Li Jun15f75de2014-04-23 15:56:51 +0800815 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 Jun57677be2014-04-23 15:56:44 +0800822 /* 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 Jun15f75de2014-04-23 15:56:51 +0800836
837void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
838{
839 sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);
840}