Commit | Line | Data |
---|---|---|
c10b4f03 PC |
1 | /* |
2 | * otg.c - ChipIdea USB IP core OTG driver | |
3 | * | |
4 | * Copyright (C) 2013 Freescale Semiconductor, Inc. | |
5 | * | |
6 | * Author: Peter Chen | |
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 otgsc register, it may include OTG operation | |
15 | * in the future. | |
16 | */ | |
17 | ||
18 | #include <linux/usb/otg.h> | |
19 | #include <linux/usb/gadget.h> | |
20 | #include <linux/usb/chipidea.h> | |
21 | ||
22 | #include "ci.h" | |
23 | #include "bits.h" | |
24 | #include "otg.h" | |
25 | ||
26 | /** | |
cbec6bd5 PC |
27 | * ci_otg_role - pick role based on ID pin state |
28 | * @ci: the controller | |
29 | */ | |
30 | enum ci_role ci_otg_role(struct ci_hdrc *ci) | |
31 | { | |
32 | u32 sts = hw_read(ci, OP_OTGSC, ~0); | |
33 | enum ci_role role = sts & OTGSC_ID | |
34 | ? CI_ROLE_GADGET | |
35 | : CI_ROLE_HOST; | |
36 | ||
37 | return role; | |
38 | } | |
39 | ||
40 | /** | |
41 | * ci_role_work - perform role changing based on ID pin | |
42 | * @work: work struct | |
43 | */ | |
44 | static void ci_role_work(struct work_struct *work) | |
45 | { | |
46 | struct ci_hdrc *ci = container_of(work, struct ci_hdrc, work); | |
47 | enum ci_role role = ci_otg_role(ci); | |
48 | ||
49 | if (role != ci->role) { | |
50 | dev_dbg(ci->dev, "switching from %s to %s\n", | |
51 | ci_role(ci)->name, ci->roles[role]->name); | |
52 | ||
53 | ci_role_stop(ci); | |
54 | ci_role_start(ci, role); | |
55 | } | |
56 | ||
57 | enable_irq(ci->irq); | |
58 | } | |
59 | ||
60 | /** | |
61 | * ci_hdrc_otg_init - initialize otg struct | |
c10b4f03 PC |
62 | * ci: the controller |
63 | */ | |
64 | int ci_hdrc_otg_init(struct ci_hdrc *ci) | |
65 | { | |
cbec6bd5 PC |
66 | INIT_WORK(&ci->work, ci_role_work); |
67 | ci->wq = create_singlethread_workqueue("ci_otg"); | |
68 | if (!ci->wq) { | |
69 | dev_err(ci->dev, "can't create workqueue\n"); | |
70 | return -ENODEV; | |
71 | } | |
c10b4f03 PC |
72 | |
73 | return 0; | |
74 | } | |
cbec6bd5 PC |
75 | |
76 | /** | |
77 | * ci_hdrc_otg_destroy - destroy otg struct | |
78 | * ci: the controller | |
79 | */ | |
80 | void ci_hdrc_otg_destroy(struct ci_hdrc *ci) | |
81 | { | |
82 | if (ci->wq) { | |
83 | flush_workqueue(ci->wq); | |
84 | destroy_workqueue(ci->wq); | |
85 | } | |
86 | ci_disable_otg_interrupt(ci, OTGSC_INT_EN_BITS); | |
87 | ci_clear_otg_interrupt(ci, OTGSC_INT_STATUS_BITS); | |
88 | } |