blob: 0c0866203907c6669481e46b17a40037706dcb01 [file] [log] [blame]
Pavankumar Kondeti39504502013-01-04 12:28:10 +05301/* Copyright (c) 2009-2013, Linux Foundation. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020042#include <linux/power_supply.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
jh.koo78df86f2012-09-04 20:13:19 +090044#include <linux/slimport.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045
Devin Kimd10f03c2012-06-25 17:05:13 -070046#include <asm/mach-types.h>
47
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053048#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070049#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080050#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053051#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053052#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053053
54#define MSM_USB_BASE (motg->regs)
55#define DRIVER_NAME "msm_otg"
56
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053057#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053058#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053059#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
60#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
61#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
62#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
63
64#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
65#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
66#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
67#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
68
Mayank Rana248698c2012-04-19 00:03:16 +053069#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080070#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053071#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
72
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053073static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053075static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053076static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053077static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078
Anji jonnala11aa5c42011-05-04 10:19:48 +053079static struct regulator *hsusb_3p3;
80static struct regulator *hsusb_1p8;
81static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053082static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053083static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053084static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053085
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053086static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053087static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053088{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053089#ifdef CONFIG_USB_MSM_ACA
90 return true;
91#else
92 return debug_aca_enabled;
93#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053094}
95
Mayank Rana248698c2012-04-19 00:03:16 +053096static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
97 { /* VDD_CX CORNER Voting */
98 [VDD_NONE] = RPM_VREG_CORNER_NONE,
99 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
100 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
101 },
102 { /* VDD_CX Voltage Voting */
103 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
104 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
105 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
106 },
107};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530108
109static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
110{
111 int rc = 0;
112
113 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700114 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530115 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200116 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530117 return PTR_ERR(hsusb_3p3);
118 }
119
120 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
121 USB_PHY_3P3_VOL_MAX);
122 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700123 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530125 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700127 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200129 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530130 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 }
133 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
134 USB_PHY_1P8_VOL_MAX);
135 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700136 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138 goto put_1p8;
139 }
140
141 return 0;
142 }
143
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146put_3p3_lpm:
147 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 return rc;
149}
150
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530151static int msm_hsusb_config_vddcx(int high)
152{
Mayank Rana248698c2012-04-19 00:03:16 +0530153 struct msm_otg *motg = the_msm_otg;
154 enum usb_vdd_type vdd_type = motg->vdd_type;
155 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530156 int min_vol;
157 int ret;
158
Mayank Rana248698c2012-04-19 00:03:16 +0530159 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530160 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
161 if (ret) {
162 pr_err("%s: unable to set the voltage for regulator "
163 "HSUSB_VDDCX\n", __func__);
164 return ret;
165 }
166
167 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
168
169 return ret;
170}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530173{
174 int ret = 0;
175
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530176 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530177 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
178 return -ENODEV;
179 }
180
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530181 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530182 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
183 return -ENODEV;
184 }
185
186 if (on) {
187 ret = regulator_set_optimum_mode(hsusb_1p8,
188 USB_PHY_1P8_HPM_LOAD);
189 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530191 "HSUSB_1p8\n", __func__);
192 return ret;
193 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195 ret = regulator_enable(hsusb_1p8);
196 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700197 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 __func__);
199 regulator_set_optimum_mode(hsusb_1p8, 0);
200 return ret;
201 }
202
Anji jonnala11aa5c42011-05-04 10:19:48 +0530203 ret = regulator_set_optimum_mode(hsusb_3p3,
204 USB_PHY_3P3_HPM_LOAD);
205 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 regulator_set_optimum_mode(hsusb_1p8, 0);
209 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530210 return ret;
211 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
213 ret = regulator_enable(hsusb_3p3);
214 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700215 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 __func__);
217 regulator_set_optimum_mode(hsusb_3p3, 0);
218 regulator_set_optimum_mode(hsusb_1p8, 0);
219 regulator_disable(hsusb_1p8);
220 return ret;
221 }
222
Anji jonnala11aa5c42011-05-04 10:19:48 +0530223 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 ret = regulator_disable(hsusb_1p8);
225 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700226 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 __func__);
228 return ret;
229 }
230
231 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530234 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235
236 ret = regulator_disable(hsusb_3p3);
237 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700238 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 __func__);
240 return ret;
241 }
242 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530243 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530245 "HSUSB_3p3\n", __func__);
246 }
247
248 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
249 return ret < 0 ? ret : 0;
250}
251
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530252static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
253{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530254 struct msm_otg_platform_data *pdata = motg->pdata;
255
256 if (!pdata->mhl_enable)
257 return;
258
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530259 if (!mhl_usb_hs_switch) {
260 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530261 return;
262 }
263
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530264 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530265 if (regulator_enable(mhl_usb_hs_switch))
266 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530267 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530268 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530269 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530270}
271
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200272static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530273{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200274 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530275 int cnt = 0;
276
277 /* initiate read operation */
278 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
279 USB_ULPI_VIEWPORT);
280
281 /* wait for completion */
282 while (cnt < ULPI_IO_TIMEOUT_USEC) {
283 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
284 break;
285 udelay(1);
286 cnt++;
287 }
288
289 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200290 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530291 readl(USB_ULPI_VIEWPORT));
292 return -ETIMEDOUT;
293 }
294 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
295}
296
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200297static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530298{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200299 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530300 int cnt = 0;
301
302 /* initiate write operation */
303 writel(ULPI_RUN | ULPI_WRITE |
304 ULPI_ADDR(reg) | ULPI_DATA(val),
305 USB_ULPI_VIEWPORT);
306
307 /* wait for completion */
308 while (cnt < ULPI_IO_TIMEOUT_USEC) {
309 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
310 break;
311 udelay(1);
312 cnt++;
313 }
314
315 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200316 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530317 return -ETIMEDOUT;
318 }
319 return 0;
320}
321
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200322static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530323 .read = ulpi_read,
324 .write = ulpi_write,
325};
326
327static void ulpi_init(struct msm_otg *motg)
328{
329 struct msm_otg_platform_data *pdata = motg->pdata;
330 int *seq = pdata->phy_init_seq;
331
332 if (!seq)
333 return;
334
335 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200336 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530337 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200338 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530339 seq += 2;
340 }
341}
342
343static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
344{
345 int ret;
346
347 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530348 if (!IS_ERR(motg->clk)) {
349 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
350 } else {
351 /* Using asynchronous block reset to the hardware */
352 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
353 clk_disable_unprepare(motg->pclk);
354 clk_disable_unprepare(motg->core_clk);
355 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
356 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530357 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200358 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530359 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530360 if (!IS_ERR(motg->clk)) {
361 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
362 } else {
363 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
364 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
365 ndelay(200);
366 clk_prepare_enable(motg->core_clk);
367 clk_prepare_enable(motg->pclk);
368 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530369 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200370 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530371 }
372 return ret;
373}
374
375static int msm_otg_phy_clk_reset(struct msm_otg *motg)
376{
377 int ret;
378
Amit Blay02eff132011-09-21 16:46:24 +0300379 if (IS_ERR(motg->phy_reset_clk))
380 return 0;
381
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530382 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
383 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200384 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530385 return ret;
386 }
387 usleep_range(10000, 12000);
388 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
389 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200390 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530391 return ret;
392}
393
394static int msm_otg_phy_reset(struct msm_otg *motg)
395{
396 u32 val;
397 int ret;
398 int retries;
399
400 ret = msm_otg_link_clk_reset(motg, 1);
401 if (ret)
402 return ret;
403 ret = msm_otg_phy_clk_reset(motg);
404 if (ret)
405 return ret;
406 ret = msm_otg_link_clk_reset(motg, 0);
407 if (ret)
408 return ret;
409
410 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
411 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
412
413 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200414 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530415 ULPI_CLR(ULPI_FUNC_CTRL));
416 if (!ret)
417 break;
418 ret = msm_otg_phy_clk_reset(motg);
419 if (ret)
420 return ret;
421 }
422 if (!retries)
423 return -ETIMEDOUT;
424
425 /* This reset calibrates the phy, if the above write succeeded */
426 ret = msm_otg_phy_clk_reset(motg);
427 if (ret)
428 return ret;
429
430 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200431 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 if (ret != -ETIMEDOUT)
433 break;
434 ret = msm_otg_phy_clk_reset(motg);
435 if (ret)
436 return ret;
437 }
438 if (!retries)
439 return -ETIMEDOUT;
440
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200441 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530442 return 0;
443}
444
445#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530446static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530447{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530448 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530449
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530450 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530451 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530452 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 break;
454 udelay(1);
455 cnt++;
456 }
457 if (cnt >= LINK_RESET_TIMEOUT_USEC)
458 return -ETIMEDOUT;
459
460 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530461 writel_relaxed(0x80000000, USB_PORTSC);
462 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530463 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530464
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530465 return 0;
466}
467
Steve Mucklef132c6c2012-06-06 18:30:57 -0700468static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530469{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700470 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530471 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530472 int ret;
473 u32 val = 0;
474 u32 ulpi_val = 0;
475
Ofir Cohen4da266f2012-01-03 10:19:29 +0200476 /*
477 * USB PHY and Link reset also reset the USB BAM.
478 * Thus perform reset operation only once to avoid
479 * USB BAM reset on other cases e.g. USB cable disconnections.
480 */
481 if (pdata->disable_reset_on_disconnect) {
482 if (motg->reset_counter)
483 return 0;
484 else
485 motg->reset_counter++;
486 }
487
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530488 if (!IS_ERR(motg->clk))
489 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530490 ret = msm_otg_phy_reset(motg);
491 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700492 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530493 return ret;
494 }
495
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530496 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530497 ret = msm_otg_link_reset(motg);
498 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700499 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530500 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530501 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530502 msleep(100);
503
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530504 ulpi_init(motg);
505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 /* Ensure that RESET operation is completed before turning off clock */
507 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530508
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530509 if (!IS_ERR(motg->clk))
510 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530511
512 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530513 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530514 if (pdata->mode == USB_OTG) {
515 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
516 val |= OTGSC_IDIE | OTGSC_BSVIE;
517 } else if (pdata->mode == USB_PERIPHERAL) {
518 ulpi_val = ULPI_INT_SESS_VALID;
519 val |= OTGSC_BSVIE;
520 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530521 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200522 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
523 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530524 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700525 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530526 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530527 /* Enable PMIC pull-up */
528 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530529 }
530
531 return 0;
532}
533
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530534static const char *timer_string(int bit)
535{
536 switch (bit) {
537 case A_WAIT_VRISE: return "a_wait_vrise";
538 case A_WAIT_VFALL: return "a_wait_vfall";
539 case B_SRP_FAIL: return "b_srp_fail";
540 case A_WAIT_BCON: return "a_wait_bcon";
541 case A_AIDL_BDIS: return "a_aidl_bdis";
542 case A_BIDL_ADIS: return "a_bidl_adis";
543 case B_ASE0_BRST: return "b_ase0_brst";
544 case A_TST_MAINT: return "a_tst_maint";
545 case B_TST_SRP: return "b_tst_srp";
546 case B_TST_CONFIG: return "b_tst_config";
547 default: return "UNDEFINED";
548 }
549}
550
551static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
552{
553 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
554
555 switch (motg->active_tmout) {
556 case A_WAIT_VRISE:
557 /* TODO: use vbus_vld interrupt */
558 set_bit(A_VBUS_VLD, &motg->inputs);
559 break;
560 case A_TST_MAINT:
561 /* OTG PET: End session after TA_TST_MAINT */
562 set_bit(A_BUS_DROP, &motg->inputs);
563 break;
564 case B_TST_SRP:
565 /*
566 * OTG PET: Initiate SRP after TB_TST_SRP of
567 * previous session end.
568 */
569 set_bit(B_BUS_REQ, &motg->inputs);
570 break;
571 case B_TST_CONFIG:
572 clear_bit(A_CONN, &motg->inputs);
573 break;
574 default:
575 set_bit(motg->active_tmout, &motg->tmouts);
576 }
577
578 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
579 queue_work(system_nrt_wq, &motg->sm_work);
580 return HRTIMER_NORESTART;
581}
582
583static void msm_otg_del_timer(struct msm_otg *motg)
584{
585 int bit = motg->active_tmout;
586
587 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
588 div_s64(ktime_to_us(hrtimer_get_remaining(
589 &motg->timer)), 1000));
590 hrtimer_cancel(&motg->timer);
591 clear_bit(bit, &motg->tmouts);
592}
593
594static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
595{
596 clear_bit(bit, &motg->tmouts);
597 motg->active_tmout = bit;
598 pr_debug("starting %s timer\n", timer_string(bit));
599 hrtimer_start(&motg->timer,
600 ktime_set(time / 1000, (time % 1000) * 1000000),
601 HRTIMER_MODE_REL);
602}
603
604static void msm_otg_init_timer(struct msm_otg *motg)
605{
606 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
607 motg->timer.function = msm_otg_timer_func;
608}
609
Steve Mucklef132c6c2012-06-06 18:30:57 -0700610static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530611{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700612 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530613
Steve Mucklef132c6c2012-06-06 18:30:57 -0700614 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530615 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700616 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530617 return -EINVAL;
618 }
619
620 pr_debug("A-Host: HNP initiated\n");
621 clear_bit(A_BUS_REQ, &motg->inputs);
622 queue_work(system_nrt_wq, &motg->sm_work);
623 return 0;
624}
625
Steve Mucklef132c6c2012-06-06 18:30:57 -0700626static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530627{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700628 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530629 u32 val;
630 int ret = 0;
631
Steve Mucklef132c6c2012-06-06 18:30:57 -0700632 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530633 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700634 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530635 ret = -EINVAL;
636 goto out;
637 }
638
639 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
640 pr_debug("initial conditions of SRP are not met. Try again"
641 "after some time\n");
642 ret = -EAGAIN;
643 goto out;
644 }
645
646 pr_debug("B-Device SRP started\n");
647
648 /*
649 * PHY won't pull D+ high unless it detects Vbus valid.
650 * Since by definition, SRP is only done when Vbus is not valid,
651 * software work-around needs to be used to spoof the PHY into
652 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
653 * VBUSVLDEXT register bits.
654 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700655 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530656 /*
657 * Harware auto assist data pulsing: Data pulse is given
658 * for 7msec; wait for vbus
659 */
660 val = readl_relaxed(USB_OTGSC);
661 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
662
663 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
664out:
665 return ret;
666}
667
Steve Mucklef132c6c2012-06-06 18:30:57 -0700668static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530669{
670 struct usb_hcd *hcd = bus_to_hcd(otg->host);
671 struct usb_device *rhub = otg->host->root_hub;
672
673 if (enable) {
674 pm_runtime_disable(&rhub->dev);
675 rhub->state = USB_STATE_NOTATTACHED;
676 hcd->driver->bus_suspend(hcd);
677 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
678 } else {
679 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700680 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530681 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
682 }
683}
684
Steve Mucklef132c6c2012-06-06 18:30:57 -0700685static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530686{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700687 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530688
Amit Blay6fa647a2012-05-24 14:12:08 +0300689 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530690 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530691
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530692 /*
693 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
694 * during bus suspend/resume. Update the relevant state
695 * machine inputs and trigger LPM entry/exit. Checking
696 * in_lpm flag would avoid unnecessary work scheduling.
697 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530698 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700699 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530700 case OTG_STATE_A_WAIT_BCON:
701 if (TA_WAIT_BCON > 0)
702 break;
703 /* fall through */
704 case OTG_STATE_A_HOST:
705 pr_debug("host bus suspend\n");
706 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530707 if (!atomic_read(&motg->in_lpm))
708 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530709 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300710 case OTG_STATE_B_PERIPHERAL:
711 pr_debug("peripheral bus suspend\n");
712 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
713 break;
714 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530715 if (!atomic_read(&motg->in_lpm))
716 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300717 break;
718
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530719 default:
720 break;
721 }
722 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700723 switch (phy->state) {
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530724 case OTG_STATE_A_WAIT_BCON:
725 /* Remote wakeup or resume */
726 set_bit(A_BUS_REQ, &motg->inputs);
727 /* ensure hardware is not in low power mode */
728 if (atomic_read(&motg->in_lpm))
729 pm_runtime_resume(phy->dev);
730 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530731 case OTG_STATE_A_SUSPEND:
732 /* Remote wakeup or resume */
733 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700734 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700735
736 /* ensure hardware is not in low power mode */
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530737 if (atomic_read(&motg->in_lpm))
738 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530739 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300740 case OTG_STATE_B_PERIPHERAL:
741 pr_debug("peripheral bus resume\n");
742 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
743 break;
744 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530745 if (atomic_read(&motg->in_lpm))
746 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300747 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530748 default:
749 break;
750 }
751 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530752 return 0;
753}
754
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530755#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530756#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
757
758#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530759static int msm_otg_suspend(struct msm_otg *motg)
760{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200761 struct usb_phy *phy = &motg->phy;
762 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763 struct msm_otg_platform_data *pdata = motg->pdata;
764 int cnt = 0;
Pavankumar Kondeti853d1982013-05-16 13:37:24 +0530765 bool host_bus_suspend, device_bus_suspend, dcp, prop_charger;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530766 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800767 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530768 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530769
770 if (atomic_read(&motg->in_lpm))
771 return 0;
772
773 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530774 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
775 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700776 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300777 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
778 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530779 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti853d1982013-05-16 13:37:24 +0530780 prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
Jack Pham6ff217b2012-08-13 15:34:20 -0700781
Pavankumar Kondetib91ab712012-10-22 13:21:19 +0530782 /*
783 * Abort suspend when,
784 * 1. charging detection in progress due to cable plug-in
785 * 2. host mode activation in progress due to Micro-A cable insertion
786 */
787
788 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
Pavankumar Kondeti853d1982013-05-16 13:37:24 +0530789 !dcp && !prop_charger) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham6ff217b2012-08-13 15:34:20 -0700790 enable_irq(motg->irq);
791 return -EBUSY;
792 }
793
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530794 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530795 * Chipidea 45-nm PHY suspend sequence:
796 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530797 * Interrupt Latch Register auto-clear feature is not present
798 * in all PHY versions. Latch register is clear on read type.
799 * Clear latch register to avoid spurious wakeup from
800 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530801 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530802 * PHY comparators are disabled when PHY enters into low power
803 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
804 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
805 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530806 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530807 * PLL is not turned off when PHY enters into low power mode (LPM).
808 * Disable PLL for maximum power savings.
809 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530810
811 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200812 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530813 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200814 ulpi_write(phy, 0x01, 0x30);
815 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530816 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530817
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530819 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530820 * PHY may take some time or even fail to enter into low power
821 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
822 * in failure case.
823 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530824 portsc = readl_relaxed(USB_PORTSC);
825 if (!(portsc & PORTSC_PHCD)) {
826 writel_relaxed(portsc | PORTSC_PHCD,
827 USB_PORTSC);
828 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
829 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
830 break;
831 udelay(1);
832 cnt++;
833 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530834 }
835
836 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200837 dev_err(phy->dev, "Unable to suspend PHY\n");
838 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530839 enable_irq(motg->irq);
840 return -ETIMEDOUT;
841 }
842
843 /*
844 * PHY has capability to generate interrupt asynchronously in low
845 * power mode (LPM). This interrupt is level triggered. So USB IRQ
846 * line must be disabled till async interrupt enable bit is cleared
847 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
848 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530849 *
850 * PHY retention mode is disallowed while entering to LPM with wall
851 * charger connected. But PHY is put into suspend mode. Hence
852 * enable asynchronous interrupt to detect charger disconnection when
853 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530854 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530855 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300856 if (host_bus_suspend || device_bus_suspend ||
Pavankumar Kondeti853d1982013-05-16 13:37:24 +0530857 (motg->pdata->otg_control == OTG_PHY_CONTROL))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530858 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
859 else
860 cmd_val |= ULPI_STP_CTRL;
861 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530862
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530863 /*
864 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
865 * PHY retention and collapse can not happen with VDP_SRC enabled.
866 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300867 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
868 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200869 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
870 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
871 /* Enable PHY HV interrupts to wake MPM/Link */
872 phy_ctrl_val |=
873 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530874
Amit Blay58b31472011-11-18 09:39:39 +0200875 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530877 }
878
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 /* Ensure that above operation is completed before turning off clocks */
880 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300881 /* Consider clocks on workaround flag only in case of bus suspend */
882 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
883 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
884 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300885 clk_disable_unprepare(motg->pclk);
886 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300887 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300888 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530889
Anji jonnala7da3f262011-12-02 17:22:14 -0800890 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530891 if (!host_bus_suspend) {
892 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
893 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700894 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530895 "TCXO D0 buffer%d\n", __func__, ret);
896 else
897 motg->lpm_flags |= XO_SHUTDOWN;
898 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800899
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530900 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
901 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 msm_hsusb_ldo_enable(motg, 0);
903 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530904 }
905
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530906 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530908 msm_hsusb_mhl_switch_enable(motg, 0);
909 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910
Steve Mucklef132c6c2012-06-06 18:30:57 -0700911 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530912 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700913 if (motg->async_irq)
914 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 if (motg->pdata->pmic_id_irq)
916 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700917 if (pdata->otg_control == OTG_PHY_CONTROL &&
918 pdata->mpm_otgsessvld_int)
919 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530921 if (bus)
922 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
923
924 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700925 /* Enable ASYNC IRQ (if present) during LPM */
926 if (motg->async_irq)
927 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530928 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530930
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200931 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530932
933 return 0;
934}
935
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530936static int msm_otg_resume(struct msm_otg *motg)
937{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200938 struct usb_phy *phy = &motg->phy;
939 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700940 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530941 int cnt = 0;
942 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200943 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800944 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530945
946 if (!atomic_read(&motg->in_lpm))
947 return 0;
948
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530950
Anji jonnala7da3f262011-12-02 17:22:14 -0800951 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530952 if (motg->lpm_flags & XO_SHUTDOWN) {
953 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
954 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700955 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530956 "TCXO D0 buffer%d\n", __func__, ret);
957 motg->lpm_flags &= ~XO_SHUTDOWN;
958 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530959
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300960 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300961 clk_prepare_enable(motg->core_clk);
962 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300963 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300964 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530965
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
967 msm_hsusb_ldo_enable(motg, 1);
968 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
969 }
970
971 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530972 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530973 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200974 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
975 phy_ctrl_val |= PHY_RETEN;
976 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
977 /* Disable PHY HV interrupts */
978 phy_ctrl_val &=
979 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
980 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530982 }
983
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530984 temp = readl(USB_USBCMD);
985 temp &= ~ASYNC_INTR_CTRL;
986 temp &= ~ULPI_STP_CTRL;
987 writel(temp, USB_USBCMD);
988
989 /*
990 * PHY comes out of low power mode (LPM) in case of wakeup
991 * from asynchronous interrupt.
992 */
993 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
994 goto skip_phy_resume;
995
996 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
997 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
998 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
999 break;
1000 udelay(1);
1001 cnt++;
1002 }
1003
1004 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1005 /*
1006 * This is a fatal error. Reset the link and
1007 * PHY. USB state can not be restored. Re-insertion
1008 * of USB cable is the only way to get USB working.
1009 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001010 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301011 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001012 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301013 }
1014
1015skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001016 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301017 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001018 if (motg->async_irq)
1019 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 if (motg->pdata->pmic_id_irq)
1021 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001022 if (pdata->otg_control == OTG_PHY_CONTROL &&
1023 pdata->mpm_otgsessvld_int)
1024 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301026 if (bus)
1027 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1028
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301029 atomic_set(&motg->in_lpm, 0);
1030
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301031 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001032 /* Match the disable_irq call from ISR */
1033 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301034 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301035 }
1036
Manu Gautamf8c45642012-08-10 10:20:56 -07001037 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1038 if (motg->async_irq)
1039 disable_irq(motg->async_irq);
1040
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001041 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301042
1043 return 0;
1044}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301045#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301046
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001047static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1048{
Pavankumar Kondeti84fec152012-11-09 20:51:36 +05301049 int ret;
1050
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001051 if (!psy)
1052 goto psy_not_supported;
1053
Pavankumar Kondeti84fec152012-11-09 20:51:36 +05301054 if (host_mode) {
1055 ret = power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1056 } else {
1057 ret = power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1058 /*
1059 * VBUS comparator is disabled by PMIC charging driver
1060 * when SYSTEM scope is selected. For ID_GND->ID_A
1061 * transition, give 50 msec delay so that PMIC charger
1062 * driver detect the VBUS and ready for accepting
1063 * charging current value from USB.
1064 */
1065 if (test_bit(ID_A, &motg->inputs))
1066 msleep(50);
1067 }
1068 return ret;
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001069
1070psy_not_supported:
1071 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1072 return -ENXIO;
1073}
1074
David Keitel081a3e22012-04-18 12:37:07 -07001075static int msm_otg_notify_chg_type(struct msm_otg *motg)
1076{
roy.parkd063ef32012-08-30 09:37:38 -07001077 int charger_type;
David Keitel081a3e22012-04-18 12:37:07 -07001078 /*
1079 * TODO
1080 * Unify OTG driver charger types and power supply charger types
1081 */
David Keitel081a3e22012-04-18 12:37:07 -07001082
1083 if (motg->chg_type == USB_SDP_CHARGER)
1084 charger_type = POWER_SUPPLY_TYPE_USB;
1085 else if (motg->chg_type == USB_CDP_CHARGER)
1086 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301087 else if (motg->chg_type == USB_DCP_CHARGER ||
1088 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001089 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1090 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1091 motg->chg_type == USB_ACA_A_CHARGER ||
1092 motg->chg_type == USB_ACA_B_CHARGER ||
1093 motg->chg_type == USB_ACA_C_CHARGER))
1094 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1095 else
Abhijeet Dharmapurikar999ee572012-08-28 19:33:49 -07001096 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001097
1098 return pm8921_set_usb_power_supply_type(charger_type);
1099}
1100
Amit Blay0f7edf72012-01-15 10:11:27 +02001101static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1102{
Amit Blay0f7edf72012-01-15 10:11:27 +02001103
Amit Blay0f7edf72012-01-15 10:11:27 +02001104 if (!psy)
1105 goto psy_not_supported;
1106
1107 if (motg->cur_power == 0 && mA > 0) {
1108 /* Enable charging */
1109 if (power_supply_set_online(psy, true))
1110 goto psy_not_supported;
1111 } else if (motg->cur_power > 0 && mA == 0) {
1112 /* Disable charging */
1113 if (power_supply_set_online(psy, false))
1114 goto psy_not_supported;
1115 return 0;
1116 }
1117 /* Set max current limit */
1118 if (power_supply_set_current_limit(psy, 1000*mA))
1119 goto psy_not_supported;
1120
1121 return 0;
1122
1123psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001124 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001125 return -ENXIO;
1126}
1127
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301128static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1129{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001130 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301131
1132 if (g && g->is_a_peripheral)
1133 return;
1134
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301135 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1136 motg->chg_type == USB_ACA_A_CHARGER ||
1137 motg->chg_type == USB_ACA_B_CHARGER ||
1138 motg->chg_type == USB_ACA_C_CHARGER) &&
1139 mA > IDEV_ACA_CHG_LIMIT)
1140 mA = IDEV_ACA_CHG_LIMIT;
1141
David Keitel081a3e22012-04-18 12:37:07 -07001142 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001143 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001144 "Failed notifying %d charger type to PMIC\n",
1145 motg->chg_type);
1146
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301147 if (motg->cur_power == mA)
1148 return;
1149
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001150 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001151
Xiaozhe Shi7c229e52013-01-22 09:46:45 -08001152 /*
1153 * Use Power Supply API if supported, otherwise fallback
1154 * to legacy pm8921 API.
1155 */
1156 if (msm_otg_notify_power_supply(motg, mA))
1157 pm8921_charger_vbus_draw(mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001158
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301159 motg->cur_power = mA;
1160}
1161
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001162static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301163{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001164 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301165
1166 /*
1167 * Gadget driver uses set_power method to notify about the
1168 * available current based on suspend/configured states.
1169 *
1170 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1171 * states when CDP/ACA is connected.
1172 */
1173 if (motg->chg_type == USB_SDP_CHARGER)
1174 msm_otg_notify_charger(motg, mA);
1175
1176 return 0;
1177}
1178
Steve Mucklef132c6c2012-06-06 18:30:57 -07001179static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301180{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001181 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301182 struct msm_otg_platform_data *pdata = motg->pdata;
1183 struct usb_hcd *hcd;
ChandanaKishori Chiluveru810bd8c2013-11-06 15:33:51 +05301184 int rc;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301185
1186 if (!otg->host)
1187 return;
1188
1189 hcd = bus_to_hcd(otg->host);
1190
1191 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001192 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301193
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301194 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001195 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301196 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1197
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301198 /*
1199 * Some boards have a switch cotrolled by gpio
1200 * to enable/disable internal HUB. Enable internal
1201 * HUB before kicking the host.
1202 */
1203 if (pdata->setup_gpio)
1204 pdata->setup_gpio(OTG_STATE_A_HOST);
ChandanaKishori Chiluveru810bd8c2013-11-06 15:33:51 +05301205
1206 /*
1207 * Increase 3.3V rail voltage to increase cross over voltage.
1208 * This is required to get some full speed audio headsets
1209 * working.
1210 */
1211 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MAX,
1212 USB_PHY_3P3_VOL_MAX);
1213 if (rc)
1214 dev_dbg(otg->phy->dev, "unable to increase 3.3V rail\n");
1215
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301216 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301217 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001218 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301219
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301220 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301221 /* HCD core reset all bits of PORTSC. select ULPI phy */
1222 writel_relaxed(0x80000000, USB_PORTSC);
1223
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301224 if (pdata->setup_gpio)
1225 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301226
1227 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001228 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301229 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
ChandanaKishori Chiluveru810bd8c2013-11-06 15:33:51 +05301230
1231 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
1232 USB_PHY_3P3_VOL_MAX);
1233 if (rc)
1234 dev_dbg(otg->phy->dev, "unable to restore 3.075V rail\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301235 }
1236}
1237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238static int msm_otg_usbdev_notify(struct notifier_block *self,
1239 unsigned long action, void *priv)
1240{
1241 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001242 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301243 struct usb_device *udev = priv;
1244
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301245 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1246 goto out;
1247
Steve Mucklef132c6c2012-06-06 18:30:57 -07001248 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301249 goto out;
1250 /*
1251 * Interested in devices connected directly to the root hub.
1252 * ACA dock can supply IDEV_CHG irrespective devices connected
1253 * on the accessory port.
1254 */
1255 if (!udev->parent || udev->parent->parent ||
1256 motg->chg_type == USB_ACA_DOCK_CHARGER)
1257 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258
1259 switch (action) {
1260 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301261 if (aca_enabled())
1262 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001263 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301264 pr_debug("B_CONN set\n");
1265 set_bit(B_CONN, &motg->inputs);
1266 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001267 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301268 /*
1269 * OTG PET: A-device must end session within
1270 * 10 sec after PET enumeration.
1271 */
1272 if (udev->quirks & USB_QUIRK_OTG_PET)
1273 msm_otg_start_timer(motg, TA_TST_MAINT,
1274 A_TST_MAINT);
1275 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301276 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278 if (udev->actconfig)
1279 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1280 else
1281 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001282 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301283 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301284 break;
1285 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001286 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1287 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301288 pr_debug("B_CONN clear\n");
1289 clear_bit(B_CONN, &motg->inputs);
1290 /*
1291 * OTG PET: A-device must end session after
1292 * PET disconnection if it is enumerated
1293 * with bcdDevice[0] = 1. USB core sets
1294 * bus->otg_vbus_off for us. clear it here.
1295 */
1296 if (udev->bus->otg_vbus_off) {
1297 udev->bus->otg_vbus_off = 0;
1298 set_bit(A_BUS_DROP, &motg->inputs);
1299 }
1300 queue_work(system_nrt_wq, &motg->sm_work);
1301 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 default:
1303 break;
1304 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301305 if (test_bit(ID_A, &motg->inputs))
1306 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1307 motg->mA_port);
1308out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 return NOTIFY_OK;
1310}
1311
Mayank Ranae3926882011-12-26 09:47:54 +05301312static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1313{
1314 int ret;
1315 static bool vbus_is_on;
1316
1317 if (vbus_is_on == on)
1318 return;
1319
1320 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301321 ret = motg->pdata->vbus_power(on);
1322 if (!ret)
1323 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301324 return;
1325 }
1326
1327 if (!vbus_otg) {
1328 pr_err("vbus_otg is NULL.");
1329 return;
1330 }
1331
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001332 /*
1333 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001334 * from usb before turning on the boost.
1335 * if exiting host mode disable the boost before enabling to draw
1336 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001337 */
Mayank Ranae3926882011-12-26 09:47:54 +05301338 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001339 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301340 ret = regulator_enable(vbus_otg);
1341 if (ret) {
1342 pr_err("unable to enable vbus_otg\n");
1343 return;
1344 }
1345 vbus_is_on = true;
1346 } else {
1347 ret = regulator_disable(vbus_otg);
1348 if (ret) {
1349 pr_err("unable to disable vbus_otg\n");
1350 return;
1351 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001352 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301353 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301354 }
1355}
1356
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001357static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301358{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001359 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301360 struct usb_hcd *hcd;
1361
1362 /*
1363 * Fail host registration if this board can support
1364 * only peripheral configuration.
1365 */
1366 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001367 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301368 return -ENODEV;
1369 }
1370
Devin Kimd10f03c2012-06-25 17:05:13 -07001371 if (!machine_is_apq8064_mako()) {
1372 if (!motg->pdata->vbus_power && host) {
1373 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1374 if (IS_ERR(vbus_otg)) {
1375 pr_err("Unable to get vbus_otg\n");
1376 return -ENODEV;
1377 }
Mayank Ranae3926882011-12-26 09:47:54 +05301378 }
1379 }
1380
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301381 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001382 if (otg->phy->state == OTG_STATE_A_HOST) {
1383 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001384 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301385 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301386 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301387 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001388 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301389 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301390 } else {
1391 otg->host = NULL;
1392 }
1393
1394 return 0;
1395 }
1396
1397 hcd = bus_to_hcd(host);
1398 hcd->power_budget = motg->pdata->power_budget;
1399
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301400#ifdef CONFIG_USB_OTG
1401 host->otg_port = 1;
1402#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1404 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301405 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001406 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301407
1408 /*
1409 * Kick the state machine work, if peripheral is not supported
1410 * or peripheral is already registered with us.
1411 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301412 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001413 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301414 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301415 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301416
1417 return 0;
1418}
1419
Steve Mucklef132c6c2012-06-06 18:30:57 -07001420static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301421{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301422 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001423 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301424 struct msm_otg_platform_data *pdata = motg->pdata;
1425
1426 if (!otg->gadget)
1427 return;
1428
1429 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001430 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301431 /*
1432 * Some boards have a switch cotrolled by gpio
1433 * to enable/disable internal HUB. Disable internal
1434 * HUB before kicking the gadget.
1435 */
1436 if (pdata->setup_gpio)
1437 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001438
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301439 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301440 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301441 ret = msm_bus_scale_client_update_request(
1442 motg->bus_perf_client, 1);
1443 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001444 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301445 "bus bandwidth %d\n", __func__, ret);
1446 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301447 usb_gadget_vbus_connect(otg->gadget);
1448 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001449 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301450 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301451 /* Configure BUS performance parameters to default */
1452 if (motg->bus_perf_client) {
1453 ret = msm_bus_scale_client_update_request(
1454 motg->bus_perf_client, 0);
1455 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001456 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301457 "for bus bw %d\n", __func__, ret);
1458 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301459 if (pdata->setup_gpio)
1460 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1461 }
1462
1463}
1464
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001465static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301466 struct usb_gadget *gadget)
1467{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001468 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301469
1470 /*
1471 * Fail peripheral registration if this board can support
1472 * only host configuration.
1473 */
1474 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001475 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301476 return -ENODEV;
1477 }
1478
1479 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001480 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1481 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301482 msm_otg_start_peripheral(otg, 0);
1483 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001484 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301485 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301486 } else {
1487 otg->gadget = NULL;
1488 }
1489
1490 return 0;
1491 }
1492 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001493 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301494
1495 /*
1496 * Kick the state machine work, if host is not supported
1497 * or host is already registered with us.
1498 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301499 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001500 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301501 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301502 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301503
1504 return 0;
1505}
1506
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301507static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1508{
1509 unsigned long flags;
1510 int id;
1511
1512 if (!motg->pdata->pmic_id_irq)
1513 return -ENODEV;
1514
1515 local_irq_save(flags);
1516 id = irq_read_line(motg->pdata->pmic_id_irq);
1517 local_irq_restore(flags);
1518
1519 /*
1520 * If we can not read ID line state for some reason, treat
1521 * it as float. This would prevent MHL discovery and kicking
1522 * host mode unnecessarily.
1523 */
1524 return !!id;
1525}
1526
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301527static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1528 void (*callback)(int on))
1529{
1530 struct usb_phy *phy = &motg->phy;
1531 int ret;
1532
1533 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1534 !motg->pdata->pmic_id_irq) {
1535 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1536 return -ENODEV;
1537 }
1538
1539 if (!motg->pdata->mhl_dev_name) {
1540 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1541 return -ENODEV;
1542 }
1543
1544 if (callback)
1545 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1546 callback);
1547 else
1548 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1549
1550 if (ret)
1551 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1552 motg->pdata->mhl_dev_name, ret);
1553 else
1554 motg->mhl_enabled = true;
1555
1556 return ret;
1557}
1558
1559static void msm_otg_mhl_notify_online(int on)
1560{
1561 struct msm_otg *motg = the_msm_otg;
1562 struct usb_phy *phy = &motg->phy;
1563 bool queue = false;
1564
1565 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1566
1567 if (on) {
1568 set_bit(MHL, &motg->inputs);
1569 } else {
1570 clear_bit(MHL, &motg->inputs);
1571 queue = true;
1572 }
1573
1574 if (queue && phy->state != OTG_STATE_UNDEFINED)
1575 schedule_work(&motg->sm_work);
1576}
1577
1578static bool msm_otg_is_mhl(struct msm_otg *motg)
1579{
1580 struct usb_phy *phy = &motg->phy;
1581 int is_mhl, ret;
1582
1583 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1584 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1585 /*
1586 * MHL driver calls our callback saying that MHL connected
1587 * if RID_GND is detected. But at later part of discovery
1588 * it may figure out MHL is not connected and returns
1589 * false. Hence clear MHL input here.
1590 */
1591 clear_bit(MHL, &motg->inputs);
1592 dev_dbg(phy->dev, "MHL device not found\n");
1593 return false;
1594 }
1595
1596 set_bit(MHL, &motg->inputs);
1597 dev_dbg(phy->dev, "MHL device found\n");
1598 return true;
1599}
1600
1601static bool msm_chg_mhl_detect(struct msm_otg *motg)
1602{
1603 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301604
1605 if (!motg->mhl_enabled)
1606 return false;
1607
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301608 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301609
1610 if (id)
1611 return false;
1612
1613 mhl_det_in_progress = true;
1614 ret = msm_otg_is_mhl(motg);
1615 mhl_det_in_progress = false;
1616
1617 return ret;
1618}
1619
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620static bool msm_chg_aca_detect(struct msm_otg *motg)
1621{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001622 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 u32 int_sts;
1624 bool ret = false;
1625
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301626 if (!aca_enabled())
1627 goto out;
1628
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1630 goto out;
1631
Steve Mucklef132c6c2012-06-06 18:30:57 -07001632 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001633 switch (int_sts & 0x1C) {
1634 case 0x08:
1635 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001636 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 motg->chg_type = USB_ACA_A_CHARGER;
1638 motg->chg_state = USB_CHG_STATE_DETECTED;
1639 clear_bit(ID_B, &motg->inputs);
1640 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301641 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001642 ret = true;
1643 }
1644 break;
1645 case 0x0C:
1646 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001647 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001648 motg->chg_type = USB_ACA_B_CHARGER;
1649 motg->chg_state = USB_CHG_STATE_DETECTED;
1650 clear_bit(ID_A, &motg->inputs);
1651 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301652 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653 ret = true;
1654 }
1655 break;
1656 case 0x10:
1657 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001658 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001659 motg->chg_type = USB_ACA_C_CHARGER;
1660 motg->chg_state = USB_CHG_STATE_DETECTED;
1661 clear_bit(ID_A, &motg->inputs);
1662 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301663 set_bit(ID, &motg->inputs);
1664 ret = true;
1665 }
1666 break;
1667 case 0x04:
1668 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001669 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301670 motg->chg_type = USB_INVALID_CHARGER;
1671 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1672 clear_bit(ID_A, &motg->inputs);
1673 clear_bit(ID_B, &motg->inputs);
1674 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 ret = true;
1676 }
1677 break;
1678 default:
1679 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1680 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301681 test_and_clear_bit(ID_C, &motg->inputs) |
1682 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001683 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001684 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001685 motg->chg_type = USB_INVALID_CHARGER;
1686 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1687 }
1688 }
1689out:
1690 return ret;
1691}
1692
1693static void msm_chg_enable_aca_det(struct msm_otg *motg)
1694{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001695 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301697 if (!aca_enabled())
1698 return;
1699
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001700 switch (motg->pdata->phy_type) {
1701 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301702 /* Disable ID_GND in link and PHY */
1703 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1704 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001705 ulpi_write(phy, 0x01, 0x0C);
1706 ulpi_write(phy, 0x10, 0x0F);
1707 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301708 /* Disable PMIC ID pull-up */
1709 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301710 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001711 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301712 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 break;
1714 default:
1715 break;
1716 }
1717}
1718
1719static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1720{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001721 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301723 if (!aca_enabled())
1724 return;
1725
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001726 switch (motg->pdata->phy_type) {
1727 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301728 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001729 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301730 break;
1731 default:
1732 break;
1733 }
1734}
1735
1736static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1737{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001738 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301739
1740 if (!aca_enabled())
1741 return;
1742
1743 switch (motg->pdata->phy_type) {
1744 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001745 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 break;
1747 default:
1748 break;
1749 }
1750}
1751
1752static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1753{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001754 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755 bool ret = false;
1756
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301757 if (!aca_enabled())
1758 return ret;
1759
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001760 switch (motg->pdata->phy_type) {
1761 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001762 if (ulpi_read(phy, 0x91) & 1) {
1763 dev_dbg(phy->dev, "RID change\n");
1764 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001765 ret = msm_chg_aca_detect(motg);
1766 }
1767 default:
1768 break;
1769 }
1770 return ret;
1771}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301772
1773static void msm_otg_id_timer_func(unsigned long data)
1774{
1775 struct msm_otg *motg = (struct msm_otg *) data;
1776
1777 if (!aca_enabled())
1778 return;
1779
1780 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001781 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301782 return;
1783 }
1784
Steve Mucklef132c6c2012-06-06 18:30:57 -07001785 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301786 goto out;
1787
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301788 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001789 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301790 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301791 }
1792
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301793out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301794 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1795 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1796}
1797
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301798static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1799{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001800 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301801 u32 chg_det;
1802 bool ret = false;
1803
1804 switch (motg->pdata->phy_type) {
1805 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001806 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301807 ret = chg_det & (1 << 4);
1808 break;
1809 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001810 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301811 ret = chg_det & 1;
1812 break;
1813 default:
1814 break;
1815 }
1816 return ret;
1817}
1818
1819static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1820{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001821 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301822 u32 chg_det;
1823
1824 switch (motg->pdata->phy_type) {
1825 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001826 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301827 /* Turn off charger block */
1828 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001829 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301830 udelay(20);
1831 /* control chg block via ULPI */
1832 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001833 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301834 /* put it in host mode for enabling D- source */
1835 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001836 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301837 /* Turn on chg detect block */
1838 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001839 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301840 udelay(20);
1841 /* enable chg detection */
1842 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001843 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 break;
1845 case SNPS_28NM_INTEGRATED_PHY:
1846 /*
1847 * Configure DM as current source, DP as current sink
1848 * and enable battery charging comparators.
1849 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001850 ulpi_write(phy, 0x8, 0x85);
1851 ulpi_write(phy, 0x2, 0x85);
1852 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301853 break;
1854 default:
1855 break;
1856 }
1857}
1858
1859static bool msm_chg_check_primary_det(struct msm_otg *motg)
1860{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001861 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301862 u32 chg_det;
1863 bool ret = false;
1864
1865 switch (motg->pdata->phy_type) {
1866 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001867 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301868 ret = chg_det & (1 << 4);
1869 break;
1870 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001871 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301872 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301873 /* Turn off VDP_SRC */
1874 ulpi_write(phy, 0x3, 0x86);
1875 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301876 break;
1877 default:
1878 break;
1879 }
1880 return ret;
1881}
1882
1883static void msm_chg_enable_primary_det(struct msm_otg *motg)
1884{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001885 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301886 u32 chg_det;
1887
1888 switch (motg->pdata->phy_type) {
1889 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001890 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301891 /* enable chg detection */
1892 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001893 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301894 break;
1895 case SNPS_28NM_INTEGRATED_PHY:
1896 /*
1897 * Configure DP as current source, DM as current sink
1898 * and enable battery charging comparators.
1899 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001900 ulpi_write(phy, 0x2, 0x85);
1901 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301902 break;
1903 default:
1904 break;
1905 }
1906}
1907
1908static bool msm_chg_check_dcd(struct msm_otg *motg)
1909{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001910 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301911 u32 line_state;
1912 bool ret = false;
1913
1914 switch (motg->pdata->phy_type) {
1915 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001916 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917 ret = !(line_state & 1);
1918 break;
1919 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001920 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301921 ret = line_state & 2;
1922 break;
1923 default:
1924 break;
1925 }
1926 return ret;
1927}
1928
1929static void msm_chg_disable_dcd(struct msm_otg *motg)
1930{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001931 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301932 u32 chg_det;
1933
1934 switch (motg->pdata->phy_type) {
1935 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001936 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301937 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001938 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301939 break;
1940 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001941 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301942 break;
1943 default:
1944 break;
1945 }
1946}
1947
1948static void msm_chg_enable_dcd(struct msm_otg *motg)
1949{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001950 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301951 u32 chg_det;
1952
1953 switch (motg->pdata->phy_type) {
1954 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001955 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 /* Turn on D+ current source */
1957 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001958 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301959 break;
1960 case SNPS_28NM_INTEGRATED_PHY:
1961 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001962 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301963 break;
1964 default:
1965 break;
1966 }
1967}
1968
1969static void msm_chg_block_on(struct msm_otg *motg)
1970{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001971 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301972 u32 func_ctrl, chg_det;
1973
1974 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001975 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301976 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1977 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001978 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301979
1980 switch (motg->pdata->phy_type) {
1981 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001982 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301983 /* control chg block via ULPI */
1984 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001985 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301986 /* Turn on chg detect block */
1987 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001988 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301989 udelay(20);
1990 break;
1991 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301992 /* disable DP and DM pull down resistors */
1993 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301994 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001995 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301996 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001997 ulpi_write(phy, 0x1F, 0x92);
1998 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301999 udelay(100);
2000 break;
2001 default:
2002 break;
2003 }
2004}
2005
2006static void msm_chg_block_off(struct msm_otg *motg)
2007{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002008 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302009 u32 func_ctrl, chg_det;
2010
2011 switch (motg->pdata->phy_type) {
2012 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002013 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302014 /* Turn off charger block */
2015 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002016 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302017 break;
2018 case SNPS_28NM_INTEGRATED_PHY:
2019 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002020 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302021 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002022 ulpi_write(phy, 0x1F, 0x92);
2023 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302024 break;
2025 default:
2026 break;
2027 }
2028
2029 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002030 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302031 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2032 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002033 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302034}
2035
Anji jonnalad270e2d2011-08-09 11:28:32 +05302036static const char *chg_to_string(enum usb_chg_type chg_type)
2037{
2038 switch (chg_type) {
2039 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2040 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2041 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2042 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2043 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2044 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2045 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302046 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302047 default: return "INVALID_CHARGER";
2048 }
2049}
kibum.leeec040442012-08-22 22:29:46 +09002050
Devin Kimd93ff032012-09-14 15:01:31 -07002051#define MSM_CHECK_TA_DELAY (5 * HZ)
2052#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09002053static void msm_ta_detect_work(struct work_struct *w)
2054{
Devin Kimd93ff032012-09-14 15:01:31 -07002055 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09002056 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09002057
Devin Kim6b8a5d12012-10-16 23:12:18 -07002058 pr_debug("msm_ta_detect_work: ta detection work\n");
Devin Kimd93ff032012-09-14 15:01:31 -07002059
2060 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
2061 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
2062 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
2063 return;
2064 }
2065
2066 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
2067 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09002068 /* inform to user space that SDP is no longer detected */
2069 msm_otg_notify_charger(motg, 0);
2070 motg->chg_state = USB_CHG_STATE_DETECTED;
2071 motg->chg_type = USB_DCP_CHARGER;
2072 motg->cur_power = 0;
2073 msm_otg_start_peripheral(otg, 0);
2074 otg->phy->state = OTG_STATE_B_IDLE;
2075 schedule_work(&motg->sm_work);
2076 return;
kibum.leeec040442012-08-22 22:29:46 +09002077 }
Devin Kimd93ff032012-09-14 15:01:31 -07002078 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002079}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302080
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302081#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2082#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302083#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2084#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302085static void msm_chg_detect_work(struct work_struct *w)
2086{
2087 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002088 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302089 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302090 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302091 unsigned long delay;
2092
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002093 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302094
2095 if (test_bit(MHL, &motg->inputs)) {
2096 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2097 return;
2098 }
2099
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302100 switch (motg->chg_state) {
2101 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302103 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302105 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302106 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302107 delay = MSM_CHG_DCD_POLL_TIME;
2108 break;
2109 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002110 if (slimport_is_connected()) {
2111 msm_chg_block_off(motg);
2112 motg->chg_state = USB_CHG_STATE_DETECTED;
2113 motg->chg_type = USB_SDP_CHARGER;
2114 queue_work(system_nrt_wq, &motg->sm_work);
2115 return;
2116 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302117 if (msm_chg_mhl_detect(motg)) {
2118 msm_chg_block_off(motg);
2119 motg->chg_state = USB_CHG_STATE_DETECTED;
2120 motg->chg_type = USB_INVALID_CHARGER;
2121 queue_work(system_nrt_wq, &motg->sm_work);
2122 return;
2123 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002124 is_aca = msm_chg_aca_detect(motg);
2125 if (is_aca) {
2126 /*
2127 * ID_A can be ACA dock too. continue
2128 * primary detection after DCD.
2129 */
2130 if (test_bit(ID_A, &motg->inputs)) {
2131 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2132 } else {
2133 delay = 0;
2134 break;
2135 }
2136 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302137 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302138 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2139 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302140 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302141 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302142 msm_chg_enable_primary_det(motg);
2143 delay = MSM_CHG_PRIMARY_DET_TIME;
2144 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2145 } else {
2146 delay = MSM_CHG_DCD_POLL_TIME;
2147 }
2148 break;
2149 case USB_CHG_STATE_DCD_DONE:
2150 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302151 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2152 dm_vlgc = line_state & PORTSC_LS_DM;
2153 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302154 if (test_bit(ID_A, &motg->inputs)) {
2155 motg->chg_type = USB_ACA_DOCK_CHARGER;
2156 motg->chg_state = USB_CHG_STATE_DETECTED;
2157 delay = 0;
2158 break;
2159 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302160 if (line_state) { /* DP > VLGC */
2161 motg->chg_type = USB_PROPRIETARY_CHARGER;
2162 motg->chg_state = USB_CHG_STATE_DETECTED;
2163 delay = 0;
2164 } else {
2165 msm_chg_enable_secondary_det(motg);
2166 delay = MSM_CHG_SECONDARY_DET_TIME;
2167 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2168 }
2169 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302170 if (test_bit(ID_A, &motg->inputs)) {
2171 motg->chg_type = USB_ACA_A_CHARGER;
2172 motg->chg_state = USB_CHG_STATE_DETECTED;
2173 delay = 0;
2174 break;
2175 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302176
2177 if (line_state) /* DP > VLGC or/and DM > VLGC */
2178 motg->chg_type = USB_PROPRIETARY_CHARGER;
2179 else
2180 motg->chg_type = USB_SDP_CHARGER;
2181
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302182 motg->chg_state = USB_CHG_STATE_DETECTED;
2183 delay = 0;
2184 }
2185 break;
2186 case USB_CHG_STATE_PRIMARY_DONE:
2187 vout = msm_chg_check_secondary_det(motg);
2188 if (vout)
2189 motg->chg_type = USB_DCP_CHARGER;
2190 else
2191 motg->chg_type = USB_CDP_CHARGER;
2192 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2193 /* fall through */
2194 case USB_CHG_STATE_SECONDARY_DONE:
2195 motg->chg_state = USB_CHG_STATE_DETECTED;
2196 case USB_CHG_STATE_DETECTED:
2197 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002198 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302199 /*
2200 * Spurious interrupt is seen after enabling ACA detection
2201 * due to which charger detection fails in case of PET.
2202 * Add delay of 100 microsec to avoid that.
2203 */
2204 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002206 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302207 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302208 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302209 return;
2210 default:
2211 return;
2212 }
2213
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302214 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302215}
2216
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302217/*
2218 * We support OTG, Peripheral only and Host only configurations. In case
2219 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2220 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2221 * enabled when switch is controlled by user and default mode is supplied
2222 * by board file, which can be changed by userspace later.
2223 */
2224static void msm_otg_init_sm(struct msm_otg *motg)
2225{
2226 struct msm_otg_platform_data *pdata = motg->pdata;
2227 u32 otgsc = readl(USB_OTGSC);
2228
2229 switch (pdata->mode) {
2230 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002231 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302232 if (pdata->default_mode == USB_HOST) {
2233 clear_bit(ID, &motg->inputs);
2234 } else if (pdata->default_mode == USB_PERIPHERAL) {
2235 set_bit(ID, &motg->inputs);
2236 set_bit(B_SESS_VLD, &motg->inputs);
2237 } else {
2238 set_bit(ID, &motg->inputs);
2239 clear_bit(B_SESS_VLD, &motg->inputs);
2240 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302241 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302242 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302243 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302244 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302245 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302246 set_bit(A_BUS_REQ, &motg->inputs);
2247 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002248 if (otgsc & OTGSC_BSV)
2249 set_bit(B_SESS_VLD, &motg->inputs);
2250 else
2251 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302252 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302253 if (pdata->pmic_id_irq) {
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302254 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302255 set_bit(ID, &motg->inputs);
2256 else
2257 clear_bit(ID, &motg->inputs);
2258 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302259 /*
2260 * VBUS initial state is reported after PMIC
2261 * driver initialization. Wait for it.
2262 */
2263 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302264 }
2265 break;
2266 case USB_HOST:
2267 clear_bit(ID, &motg->inputs);
2268 break;
2269 case USB_PERIPHERAL:
2270 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302271 if (pdata->otg_control == OTG_PHY_CONTROL) {
2272 if (otgsc & OTGSC_BSV)
2273 set_bit(B_SESS_VLD, &motg->inputs);
2274 else
2275 clear_bit(B_SESS_VLD, &motg->inputs);
2276 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2277 /*
2278 * VBUS initial state is reported after PMIC
2279 * driver initialization. Wait for it.
2280 */
2281 wait_for_completion(&pmic_vbus_init);
2282 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302283 break;
2284 default:
2285 break;
2286 }
2287}
2288
2289static void msm_otg_sm_work(struct work_struct *w)
2290{
2291 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002292 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302293 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302294
Steve Mucklef132c6c2012-06-06 18:30:57 -07002295 pm_runtime_resume(otg->phy->dev);
2296 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002297 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302298 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002299 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302300 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302301 psy = power_supply_get_by_name("usb");
2302 if (!psy)
2303 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002304 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302305 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2306 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002307 pm_runtime_put_noidle(otg->phy->dev);
2308 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302309 break;
2310 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302311 /* FALL THROUGH */
2312 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302313 if (test_bit(MHL, &motg->inputs)) {
2314 /* allow LPM */
2315 pm_runtime_put_noidle(otg->phy->dev);
2316 pm_runtime_suspend(otg->phy->dev);
2317 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002318 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302319 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002320 if (slimport_is_connected()) {
2321 work = 1;
2322 break;
2323 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302324 if (msm_chg_mhl_detect(motg)) {
2325 work = 1;
2326 break;
2327 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302328 clear_bit(B_BUS_REQ, &motg->inputs);
2329 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002330 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302331 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302332 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302333 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302334 switch (motg->chg_state) {
2335 case USB_CHG_STATE_UNDEFINED:
2336 msm_chg_detect_work(&motg->chg_work.work);
2337 break;
2338 case USB_CHG_STATE_DETECTED:
2339 switch (motg->chg_type) {
2340 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302341 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002342 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302343 /* fall through */
2344 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302345 msm_otg_notify_charger(motg,
2346 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002347 pm_runtime_put_noidle(otg->phy->dev);
2348 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302349 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302350 case USB_ACA_B_CHARGER:
2351 msm_otg_notify_charger(motg,
2352 IDEV_ACA_CHG_MAX);
2353 /*
2354 * (ID_B --> ID_C) PHY_ALT interrupt can
2355 * not be detected in LPM.
2356 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302357 break;
2358 case USB_CDP_CHARGER:
2359 msm_otg_notify_charger(motg,
2360 IDEV_CHG_MAX);
2361 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002362 otg->phy->state =
2363 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302364 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302365 case USB_ACA_C_CHARGER:
2366 msm_otg_notify_charger(motg,
2367 IDEV_ACA_CHG_MAX);
2368 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002369 otg->phy->state =
2370 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302371 break;
2372 case USB_SDP_CHARGER:
jh.koo78df86f2012-09-04 20:13:19 +09002373 if(!slimport_is_connected()) {
2374 msm_otg_start_peripheral(otg, 1);
2375 otg->phy->state =
2376 OTG_STATE_B_PERIPHERAL;
2377 }
kibum.leeec040442012-08-22 22:29:46 +09002378 schedule_delayed_work(&motg->check_ta_work,
2379 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302380 break;
2381 default:
2382 break;
2383 }
2384 break;
2385 default:
2386 break;
2387 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302388 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2389 pr_debug("b_sess_end && b_bus_req\n");
2390 if (msm_otg_start_srp(otg) < 0) {
2391 clear_bit(B_BUS_REQ, &motg->inputs);
2392 work = 1;
2393 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302394 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002395 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302396 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2397 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302398 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302399 pr_debug("chg_work cancel");
Mayank Rana9062a672013-01-23 19:26:21 +05302400 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302401 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002402 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302403 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2404 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302405 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002406 msm_otg_reset(otg->phy);
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302407 /*
2408 * There is a small window where ID interrupt
2409 * is not monitored during ID detection circuit
2410 * switch from ACA to PMIC. Check ID state
2411 * before entering into low power mode.
2412 */
2413 if (!msm_otg_read_pmic_id_state(motg)) {
2414 pr_debug("process missed ID intr\n");
2415 clear_bit(ID, &motg->inputs);
2416 work = 1;
2417 break;
2418 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002419 pm_runtime_put_noidle(otg->phy->dev);
2420 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302421 }
2422 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302423 case OTG_STATE_B_SRP_INIT:
2424 if (!test_bit(ID, &motg->inputs) ||
2425 test_bit(ID_A, &motg->inputs) ||
2426 test_bit(ID_C, &motg->inputs) ||
2427 (test_bit(B_SESS_VLD, &motg->inputs) &&
2428 !test_bit(ID_B, &motg->inputs))) {
2429 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2430 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002431 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302432 /*
2433 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2434 * bits after SRP initiation.
2435 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002436 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302437 work = 1;
2438 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2439 pr_debug("b_srp_fail\n");
2440 pr_info("A-device did not respond to SRP\n");
2441 clear_bit(B_BUS_REQ, &motg->inputs);
2442 clear_bit(B_SRP_FAIL, &motg->tmouts);
2443 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002444 ulpi_write(otg->phy, 0x0, 0x98);
2445 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302446 motg->b_last_se0_sess = jiffies;
2447 work = 1;
2448 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302449 break;
2450 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302451 if (!test_bit(ID, &motg->inputs) ||
2452 test_bit(ID_A, &motg->inputs) ||
2453 test_bit(ID_B, &motg->inputs) ||
2454 !test_bit(B_SESS_VLD, &motg->inputs)) {
2455 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302456 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2457 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302458 msm_otg_notify_charger(motg, 0);
2459 srp_reqd = otg->gadget->otg_srp_reqd;
2460 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302461 if (test_bit(ID_B, &motg->inputs))
2462 clear_bit(ID_B, &motg->inputs);
2463 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002464 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302465 motg->b_last_se0_sess = jiffies;
2466 if (srp_reqd)
2467 msm_otg_start_timer(motg,
2468 TB_TST_SRP, B_TST_SRP);
2469 else
2470 work = 1;
2471 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2472 otg->gadget->b_hnp_enable &&
2473 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2474 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2475 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2476 /* D+ pullup should not be disconnected within 4msec
2477 * after A device suspends the bus. Otherwise PET will
2478 * fail the compliance test.
2479 */
2480 udelay(1000);
2481 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002482 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302483 /*
2484 * start HCD even before A-device enable
2485 * pull-up to meet HNP timings.
2486 */
2487 otg->host->is_b_host = 1;
2488 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002489 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2490 test_bit(B_SESS_VLD, &motg->inputs)) {
2491 pr_debug("a_bus_suspend && b_sess_vld\n");
2492 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002493 pm_runtime_put_noidle(otg->phy->dev);
2494 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002495 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002496 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302497 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498 }
2499 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302500 case OTG_STATE_B_WAIT_ACON:
2501 if (!test_bit(ID, &motg->inputs) ||
2502 test_bit(ID_A, &motg->inputs) ||
2503 test_bit(ID_B, &motg->inputs) ||
2504 !test_bit(B_SESS_VLD, &motg->inputs)) {
2505 pr_debug("!id || id_a/b || !b_sess_vld\n");
2506 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302507 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302508 * A-device is physically disconnected during
2509 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302510 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302511 msm_otg_start_host(otg, 0);
2512 otg->host->is_b_host = 0;
2513
2514 clear_bit(B_BUS_REQ, &motg->inputs);
2515 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2516 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002517 otg->phy->state = OTG_STATE_B_IDLE;
2518 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302519 work = 1;
2520 } else if (test_bit(A_CONN, &motg->inputs)) {
2521 pr_debug("a_conn\n");
2522 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002523 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302524 /*
2525 * PET disconnects D+ pullup after reset is generated
2526 * by B device in B_HOST role which is not detected by
2527 * B device. As workaorund , start timer of 300msec
2528 * and stop timer if A device is enumerated else clear
2529 * A_CONN.
2530 */
2531 msm_otg_start_timer(motg, TB_TST_CONFIG,
2532 B_TST_CONFIG);
2533 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2534 pr_debug("b_ase0_brst_tmout\n");
2535 pr_info("B HNP fail:No response from A device\n");
2536 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002537 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302538 otg->host->is_b_host = 0;
2539 clear_bit(B_ASE0_BRST, &motg->tmouts);
2540 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2541 clear_bit(B_BUS_REQ, &motg->inputs);
2542 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002543 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302544 work = 1;
2545 } else if (test_bit(ID_C, &motg->inputs)) {
2546 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2547 }
2548 break;
2549 case OTG_STATE_B_HOST:
2550 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2551 !test_bit(A_CONN, &motg->inputs) ||
2552 !test_bit(B_SESS_VLD, &motg->inputs)) {
2553 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2554 clear_bit(A_CONN, &motg->inputs);
2555 clear_bit(B_BUS_REQ, &motg->inputs);
2556 msm_otg_start_host(otg, 0);
2557 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002558 otg->phy->state = OTG_STATE_B_IDLE;
2559 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302560 work = 1;
2561 } else if (test_bit(ID_C, &motg->inputs)) {
2562 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2563 }
2564 break;
2565 case OTG_STATE_A_IDLE:
2566 otg->default_a = 1;
2567 if (test_bit(ID, &motg->inputs) &&
2568 !test_bit(ID_A, &motg->inputs)) {
2569 pr_debug("id && !id_a\n");
2570 otg->default_a = 0;
2571 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002572 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302573 del_timer_sync(&motg->id_timer);
2574 msm_otg_link_reset(motg);
2575 msm_chg_enable_aca_intr(motg);
2576 msm_otg_notify_charger(motg, 0);
2577 work = 1;
2578 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2579 (test_bit(A_SRP_DET, &motg->inputs) ||
2580 test_bit(A_BUS_REQ, &motg->inputs))) {
2581 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2582
2583 clear_bit(A_SRP_DET, &motg->inputs);
2584 /* Disable SRP detection */
2585 writel_relaxed((readl_relaxed(USB_OTGSC) &
2586 ~OTGSC_INTSTS_MASK) &
2587 ~OTGSC_DPIE, USB_OTGSC);
2588
Steve Mucklef132c6c2012-06-06 18:30:57 -07002589 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302590 /* VBUS should not be supplied before end of SRP pulse
2591 * generated by PET, if not complaince test fail.
2592 */
2593 usleep_range(10000, 12000);
2594 /* ACA: ID_A: Stop charging untill enumeration */
2595 if (test_bit(ID_A, &motg->inputs))
2596 msm_otg_notify_charger(motg, 0);
2597 else
2598 msm_hsusb_vbus_power(motg, 1);
2599 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2600 } else {
2601 pr_debug("No session requested\n");
2602 clear_bit(A_BUS_DROP, &motg->inputs);
2603 if (test_bit(ID_A, &motg->inputs)) {
2604 msm_otg_notify_charger(motg,
2605 IDEV_ACA_CHG_MAX);
2606 } else if (!test_bit(ID, &motg->inputs)) {
2607 msm_otg_notify_charger(motg, 0);
2608 /*
2609 * A-device is not providing power on VBUS.
2610 * Enable SRP detection.
2611 */
2612 writel_relaxed(0x13, USB_USBMODE);
2613 writel_relaxed((readl_relaxed(USB_OTGSC) &
2614 ~OTGSC_INTSTS_MASK) |
2615 OTGSC_DPIE, USB_OTGSC);
2616 mb();
2617 }
2618 }
2619 break;
2620 case OTG_STATE_A_WAIT_VRISE:
2621 if ((test_bit(ID, &motg->inputs) &&
2622 !test_bit(ID_A, &motg->inputs)) ||
2623 test_bit(A_BUS_DROP, &motg->inputs) ||
2624 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2625 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2626 clear_bit(A_BUS_REQ, &motg->inputs);
2627 msm_otg_del_timer(motg);
2628 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002629 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302630 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2631 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2632 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002633 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302634 if (TA_WAIT_BCON > 0)
2635 msm_otg_start_timer(motg, TA_WAIT_BCON,
2636 A_WAIT_BCON);
2637 msm_otg_start_host(otg, 1);
2638 msm_chg_enable_aca_det(motg);
2639 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302640 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302641 if (msm_chg_check_aca_intr(motg))
2642 work = 1;
2643 }
2644 break;
2645 case OTG_STATE_A_WAIT_BCON:
2646 if ((test_bit(ID, &motg->inputs) &&
2647 !test_bit(ID_A, &motg->inputs)) ||
2648 test_bit(A_BUS_DROP, &motg->inputs) ||
2649 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2650 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2651 "a_wait_bcon_tmout\n");
2652 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2653 pr_info("Device No Response\n");
2654 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2655 }
2656 msm_otg_del_timer(motg);
2657 clear_bit(A_BUS_REQ, &motg->inputs);
2658 clear_bit(B_CONN, &motg->inputs);
2659 msm_otg_start_host(otg, 0);
2660 /*
2661 * ACA: ID_A with NO accessory, just the A plug is
2662 * attached to ACA: Use IDCHG_MAX for charging
2663 */
2664 if (test_bit(ID_A, &motg->inputs))
2665 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2666 else
2667 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002668 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302669 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2670 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2671 pr_debug("!a_vbus_vld\n");
2672 clear_bit(B_CONN, &motg->inputs);
2673 msm_otg_del_timer(motg);
2674 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002675 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2676 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302677 } else if (test_bit(ID_A, &motg->inputs)) {
2678 msm_hsusb_vbus_power(motg, 0);
2679 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2680 /*
2681 * If TA_WAIT_BCON is infinite, we don;t
2682 * turn off VBUS. Enter low power mode.
2683 */
2684 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002685 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302686 } else if (!test_bit(ID, &motg->inputs)) {
2687 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302688 }
2689 break;
2690 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302691 if ((test_bit(ID, &motg->inputs) &&
2692 !test_bit(ID_A, &motg->inputs)) ||
2693 test_bit(A_BUS_DROP, &motg->inputs)) {
2694 pr_debug("id_a/b/c || a_bus_drop\n");
2695 clear_bit(B_CONN, &motg->inputs);
2696 clear_bit(A_BUS_REQ, &motg->inputs);
2697 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002698 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302699 msm_otg_start_host(otg, 0);
2700 if (!test_bit(ID_A, &motg->inputs))
2701 msm_hsusb_vbus_power(motg, 0);
2702 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2703 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2704 pr_debug("!a_vbus_vld\n");
2705 clear_bit(B_CONN, &motg->inputs);
2706 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002707 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302708 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002709 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302710 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2711 /*
2712 * a_bus_req is de-asserted when root hub is
2713 * suspended or HNP is in progress.
2714 */
2715 pr_debug("!a_bus_req\n");
2716 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002717 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302718 if (otg->host->b_hnp_enable)
2719 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2720 A_AIDL_BDIS);
2721 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002722 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302723 } else if (!test_bit(B_CONN, &motg->inputs)) {
2724 pr_debug("!b_conn\n");
2725 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002726 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302727 if (TA_WAIT_BCON > 0)
2728 msm_otg_start_timer(motg, TA_WAIT_BCON,
2729 A_WAIT_BCON);
2730 if (msm_chg_check_aca_intr(motg))
2731 work = 1;
2732 } else if (test_bit(ID_A, &motg->inputs)) {
2733 msm_otg_del_timer(motg);
2734 msm_hsusb_vbus_power(motg, 0);
2735 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2736 msm_otg_notify_charger(motg,
2737 IDEV_ACA_CHG_MAX);
2738 else
2739 msm_otg_notify_charger(motg,
2740 IDEV_CHG_MIN - motg->mA_port);
2741 } else if (!test_bit(ID, &motg->inputs)) {
2742 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2743 motg->chg_type = USB_INVALID_CHARGER;
2744 msm_otg_notify_charger(motg, 0);
2745 msm_hsusb_vbus_power(motg, 1);
2746 }
2747 break;
2748 case OTG_STATE_A_SUSPEND:
2749 if ((test_bit(ID, &motg->inputs) &&
2750 !test_bit(ID_A, &motg->inputs)) ||
2751 test_bit(A_BUS_DROP, &motg->inputs) ||
2752 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2753 pr_debug("id_a/b/c || a_bus_drop ||"
2754 "a_aidl_bdis_tmout\n");
2755 msm_otg_del_timer(motg);
2756 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002757 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302758 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002759 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302760 if (!test_bit(ID_A, &motg->inputs))
2761 msm_hsusb_vbus_power(motg, 0);
2762 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2763 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2764 pr_debug("!a_vbus_vld\n");
2765 msm_otg_del_timer(motg);
2766 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002767 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302768 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002769 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302770 } else if (!test_bit(B_CONN, &motg->inputs) &&
2771 otg->host->b_hnp_enable) {
2772 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002773 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302774 msm_otg_host_hnp_enable(otg, 1);
2775 otg->gadget->is_a_peripheral = 1;
2776 msm_otg_start_peripheral(otg, 1);
2777 } else if (!test_bit(B_CONN, &motg->inputs) &&
2778 !otg->host->b_hnp_enable) {
2779 pr_debug("!b_conn && !b_hnp_enable");
2780 /*
2781 * bus request is dropped during suspend.
2782 * acquire again for next device.
2783 */
2784 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002785 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302786 if (TA_WAIT_BCON > 0)
2787 msm_otg_start_timer(motg, TA_WAIT_BCON,
2788 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002789 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302790 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002791 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302792 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002793 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002794 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302795 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302796 }
2797 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302798 case OTG_STATE_A_PERIPHERAL:
2799 if ((test_bit(ID, &motg->inputs) &&
2800 !test_bit(ID_A, &motg->inputs)) ||
2801 test_bit(A_BUS_DROP, &motg->inputs)) {
2802 pr_debug("id _f/b/c || a_bus_drop\n");
2803 /* Clear BIDL_ADIS timer */
2804 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002805 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302806 msm_otg_start_peripheral(otg, 0);
2807 otg->gadget->is_a_peripheral = 0;
2808 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002809 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302810 if (!test_bit(ID_A, &motg->inputs))
2811 msm_hsusb_vbus_power(motg, 0);
2812 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2813 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2814 pr_debug("!a_vbus_vld\n");
2815 /* Clear BIDL_ADIS timer */
2816 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002817 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302818 msm_otg_start_peripheral(otg, 0);
2819 otg->gadget->is_a_peripheral = 0;
2820 msm_otg_start_host(otg, 0);
2821 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2822 pr_debug("a_bidl_adis_tmout\n");
2823 msm_otg_start_peripheral(otg, 0);
2824 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002825 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302826 set_bit(A_BUS_REQ, &motg->inputs);
2827 msm_otg_host_hnp_enable(otg, 0);
2828 if (TA_WAIT_BCON > 0)
2829 msm_otg_start_timer(motg, TA_WAIT_BCON,
2830 A_WAIT_BCON);
2831 } else if (test_bit(ID_A, &motg->inputs)) {
2832 msm_hsusb_vbus_power(motg, 0);
2833 msm_otg_notify_charger(motg,
2834 IDEV_CHG_MIN - motg->mA_port);
2835 } else if (!test_bit(ID, &motg->inputs)) {
2836 msm_otg_notify_charger(motg, 0);
2837 msm_hsusb_vbus_power(motg, 1);
2838 }
2839 break;
2840 case OTG_STATE_A_WAIT_VFALL:
2841 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2842 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002843 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302844 work = 1;
2845 }
2846 break;
2847 case OTG_STATE_A_VBUS_ERR:
2848 if ((test_bit(ID, &motg->inputs) &&
2849 !test_bit(ID_A, &motg->inputs)) ||
2850 test_bit(A_BUS_DROP, &motg->inputs) ||
2851 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002852 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302853 if (!test_bit(ID_A, &motg->inputs))
2854 msm_hsusb_vbus_power(motg, 0);
2855 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2856 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2857 motg->chg_type = USB_INVALID_CHARGER;
2858 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302859 }
2860 break;
2861 default:
2862 break;
2863 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302864 if (work)
2865 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302866}
2867
2868static irqreturn_t msm_otg_irq(int irq, void *data)
2869{
2870 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002871 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302872 u32 otgsc = 0, usbsts, pc;
2873 bool work = 0;
2874 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302875
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302876 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002877 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302878 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002879 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002880 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002881 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302882 return IRQ_HANDLED;
2883 }
2884
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002885 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302886 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302887
2888 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302889 return IRQ_NONE;
2890
2891 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302892 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302893 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302894 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302895 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302896 pr_debug("Id clear\n");
2897 /*
2898 * Assert a_bus_req to supply power on
2899 * VBUS when Micro/Mini-A cable is connected
2900 * with out user intervention.
2901 */
2902 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302903 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302904 msm_chg_enable_aca_det(motg);
2905 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302906 writel_relaxed(otgsc, USB_OTGSC);
2907 work = 1;
2908 } else if (otgsc & OTGSC_DPIS) {
2909 pr_debug("DPIS detected\n");
2910 writel_relaxed(otgsc, USB_OTGSC);
2911 set_bit(A_SRP_DET, &motg->inputs);
2912 set_bit(A_BUS_REQ, &motg->inputs);
2913 work = 1;
2914 } else if (otgsc & OTGSC_BSVIS) {
2915 writel_relaxed(otgsc, USB_OTGSC);
2916 /*
2917 * BSV interrupt comes when operating as an A-device
2918 * (VBUS on/off).
2919 * But, handle BSV when charger is removed from ACA in ID_A
2920 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002921 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302922 !test_bit(ID_A, &motg->inputs))
2923 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302924 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302925 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302926 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302927 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302928 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302929 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002930 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2931
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302932 msm_chg_check_aca_intr(motg);
2933 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302934 work = 1;
2935 } else if (usbsts & STS_PCI) {
2936 pc = readl_relaxed(USB_PORTSC);
2937 pr_debug("portsc = %x\n", pc);
2938 ret = IRQ_NONE;
2939 /*
2940 * HCD Acks PCI interrupt. We use this to switch
2941 * between different OTG states.
2942 */
2943 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002944 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302945 case OTG_STATE_A_SUSPEND:
2946 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2947 !(pc & PORTSC_CCS)) {
2948 pr_debug("B_CONN clear\n");
2949 clear_bit(B_CONN, &motg->inputs);
2950 msm_otg_del_timer(motg);
2951 }
2952 break;
2953 case OTG_STATE_A_PERIPHERAL:
2954 /*
2955 * A-peripheral observed activity on bus.
2956 * clear A_BIDL_ADIS timer.
2957 */
2958 msm_otg_del_timer(motg);
2959 work = 0;
2960 break;
2961 case OTG_STATE_B_WAIT_ACON:
2962 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2963 pr_debug("A_CONN set\n");
2964 set_bit(A_CONN, &motg->inputs);
2965 /* Clear ASE0_BRST timer */
2966 msm_otg_del_timer(motg);
2967 }
2968 break;
2969 case OTG_STATE_B_HOST:
2970 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2971 pr_debug("A_CONN clear\n");
2972 clear_bit(A_CONN, &motg->inputs);
2973 msm_otg_del_timer(motg);
2974 }
2975 break;
2976 case OTG_STATE_A_WAIT_BCON:
2977 if (TA_WAIT_BCON < 0)
2978 set_bit(A_BUS_REQ, &motg->inputs);
2979 default:
2980 work = 0;
2981 break;
2982 }
2983 } else if (usbsts & STS_URI) {
2984 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002985 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302986 case OTG_STATE_A_PERIPHERAL:
2987 /*
2988 * A-peripheral observed activity on bus.
2989 * clear A_BIDL_ADIS timer.
2990 */
2991 msm_otg_del_timer(motg);
2992 work = 0;
2993 break;
2994 default:
2995 work = 0;
2996 break;
2997 }
2998 } else if (usbsts & STS_SLI) {
2999 ret = IRQ_NONE;
3000 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003001 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303002 case OTG_STATE_B_PERIPHERAL:
3003 if (otg->gadget->b_hnp_enable) {
3004 set_bit(A_BUS_SUSPEND, &motg->inputs);
3005 set_bit(B_BUS_REQ, &motg->inputs);
3006 work = 1;
3007 }
3008 break;
3009 case OTG_STATE_A_PERIPHERAL:
3010 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3011 A_BIDL_ADIS);
3012 break;
3013 default:
3014 break;
3015 }
3016 } else if ((usbsts & PHY_ALT_INT)) {
3017 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3018 if (msm_chg_check_aca_intr(motg))
3019 work = 1;
3020 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303021 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303022 if (work)
3023 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303024
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303025 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003026}
3027
3028static void msm_otg_set_vbus_state(int online)
3029{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303030 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003031 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303032 struct usb_otg *otg = motg->phy.otg;
3033
3034 /* In A Host Mode, ignore received BSV interrupts */
3035 if (otg->phy->state >= OTG_STATE_A_IDLE)
3036 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003037
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303038 if (online) {
3039 pr_debug("PMIC: BSV set\n");
3040 set_bit(B_SESS_VLD, &motg->inputs);
3041 } else {
3042 pr_debug("PMIC: BSV clear\n");
3043 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303044 }
3045
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303046 if (!init) {
3047 init = true;
3048 complete(&pmic_vbus_init);
3049 pr_debug("PMIC: BSV init complete\n");
3050 return;
3051 }
3052
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303053 if (test_bit(MHL, &motg->inputs) ||
3054 mhl_det_in_progress) {
3055 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3056 return;
3057 }
3058
Jack Pham5ca279b2012-05-14 18:42:54 -07003059 if (atomic_read(&motg->pm_suspended))
3060 motg->sm_work_pending = true;
3061 else
3062 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003063}
3064
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303065static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003066{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303067 struct msm_otg *motg = container_of(w, struct msm_otg,
3068 pmic_id_status_work.work);
3069 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003070
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05303071 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303072 if (!test_and_set_bit(ID, &motg->inputs)) {
3073 pr_debug("PMIC: ID set\n");
3074 work = 1;
3075 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303076 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303077 if (test_and_clear_bit(ID, &motg->inputs)) {
3078 pr_debug("PMIC: ID clear\n");
3079 set_bit(A_BUS_REQ, &motg->inputs);
3080 work = 1;
3081 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303082 }
3083
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303084 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003085 if (atomic_read(&motg->pm_suspended))
3086 motg->sm_work_pending = true;
3087 else
3088 queue_work(system_nrt_wq, &motg->sm_work);
3089 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303090
3091}
3092
3093#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3094static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3095{
3096 struct msm_otg *motg = data;
3097
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303098 if (test_bit(MHL, &motg->inputs) ||
3099 mhl_det_in_progress) {
3100 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3101 return IRQ_HANDLED;
3102 }
3103
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303104 if (!aca_id_turned_on)
3105 /*schedule delayed work for 5msec for ID line state to settle*/
3106 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3107 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003108
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303109 return IRQ_HANDLED;
3110}
3111
3112static int msm_otg_mode_show(struct seq_file *s, void *unused)
3113{
3114 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003115 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303116
Steve Mucklef132c6c2012-06-06 18:30:57 -07003117 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303118 case OTG_STATE_A_HOST:
3119 seq_printf(s, "host\n");
3120 break;
3121 case OTG_STATE_B_PERIPHERAL:
3122 seq_printf(s, "peripheral\n");
3123 break;
3124 default:
3125 seq_printf(s, "none\n");
3126 break;
3127 }
3128
3129 return 0;
3130}
3131
3132static int msm_otg_mode_open(struct inode *inode, struct file *file)
3133{
3134 return single_open(file, msm_otg_mode_show, inode->i_private);
3135}
3136
3137static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3138 size_t count, loff_t *ppos)
3139{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303140 struct seq_file *s = file->private_data;
3141 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303142 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003143 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303144 int status = count;
3145 enum usb_mode_type req_mode;
3146
3147 memset(buf, 0x00, sizeof(buf));
3148
3149 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3150 status = -EFAULT;
3151 goto out;
3152 }
3153
3154 if (!strncmp(buf, "host", 4)) {
3155 req_mode = USB_HOST;
3156 } else if (!strncmp(buf, "peripheral", 10)) {
3157 req_mode = USB_PERIPHERAL;
3158 } else if (!strncmp(buf, "none", 4)) {
3159 req_mode = USB_NONE;
3160 } else {
3161 status = -EINVAL;
3162 goto out;
3163 }
3164
3165 switch (req_mode) {
3166 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003167 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303168 case OTG_STATE_A_HOST:
3169 case OTG_STATE_B_PERIPHERAL:
3170 set_bit(ID, &motg->inputs);
3171 clear_bit(B_SESS_VLD, &motg->inputs);
3172 break;
3173 default:
3174 goto out;
3175 }
3176 break;
3177 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003178 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303179 case OTG_STATE_B_IDLE:
3180 case OTG_STATE_A_HOST:
3181 set_bit(ID, &motg->inputs);
3182 set_bit(B_SESS_VLD, &motg->inputs);
3183 break;
3184 default:
3185 goto out;
3186 }
3187 break;
3188 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003189 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303190 case OTG_STATE_B_IDLE:
3191 case OTG_STATE_B_PERIPHERAL:
3192 clear_bit(ID, &motg->inputs);
3193 break;
3194 default:
3195 goto out;
3196 }
3197 break;
3198 default:
3199 goto out;
3200 }
3201
Steve Mucklef132c6c2012-06-06 18:30:57 -07003202 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303203 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303204out:
3205 return status;
3206}
3207
3208const struct file_operations msm_otg_mode_fops = {
3209 .open = msm_otg_mode_open,
3210 .read = seq_read,
3211 .write = msm_otg_mode_write,
3212 .llseek = seq_lseek,
3213 .release = single_release,
3214};
3215
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303216static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3217{
3218 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003219 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303220
Steve Mucklef132c6c2012-06-06 18:30:57 -07003221 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303222 return 0;
3223}
3224
3225static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3226{
3227 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3228}
3229
3230const struct file_operations msm_otg_state_fops = {
3231 .open = msm_otg_otg_state_open,
3232 .read = seq_read,
3233 .llseek = seq_lseek,
3234 .release = single_release,
3235};
3236
Anji jonnalad270e2d2011-08-09 11:28:32 +05303237static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3238{
3239 struct msm_otg *motg = s->private;
3240
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303241 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303242 return 0;
3243}
3244
3245static int msm_otg_chg_open(struct inode *inode, struct file *file)
3246{
3247 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3248}
3249
3250const struct file_operations msm_otg_chg_fops = {
3251 .open = msm_otg_chg_open,
3252 .read = seq_read,
3253 .llseek = seq_lseek,
3254 .release = single_release,
3255};
3256
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303257static int msm_otg_aca_show(struct seq_file *s, void *unused)
3258{
3259 if (debug_aca_enabled)
3260 seq_printf(s, "enabled\n");
3261 else
3262 seq_printf(s, "disabled\n");
3263
3264 return 0;
3265}
3266
3267static int msm_otg_aca_open(struct inode *inode, struct file *file)
3268{
3269 return single_open(file, msm_otg_aca_show, inode->i_private);
3270}
3271
3272static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3273 size_t count, loff_t *ppos)
3274{
3275 char buf[8];
3276
3277 memset(buf, 0x00, sizeof(buf));
3278
3279 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3280 return -EFAULT;
3281
3282 if (!strncmp(buf, "enable", 6))
3283 debug_aca_enabled = true;
3284 else
3285 debug_aca_enabled = false;
3286
3287 return count;
3288}
3289
3290const struct file_operations msm_otg_aca_fops = {
3291 .open = msm_otg_aca_open,
3292 .read = seq_read,
3293 .write = msm_otg_aca_write,
3294 .llseek = seq_lseek,
3295 .release = single_release,
3296};
3297
Manu Gautam8bdcc592012-03-06 11:26:06 +05303298static int msm_otg_bus_show(struct seq_file *s, void *unused)
3299{
3300 if (debug_bus_voting_enabled)
3301 seq_printf(s, "enabled\n");
3302 else
3303 seq_printf(s, "disabled\n");
3304
3305 return 0;
3306}
3307
3308static int msm_otg_bus_open(struct inode *inode, struct file *file)
3309{
3310 return single_open(file, msm_otg_bus_show, inode->i_private);
3311}
3312
3313static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3314 size_t count, loff_t *ppos)
3315{
3316 char buf[8];
3317 int ret;
3318 struct seq_file *s = file->private_data;
3319 struct msm_otg *motg = s->private;
3320
3321 memset(buf, 0x00, sizeof(buf));
3322
3323 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3324 return -EFAULT;
3325
3326 if (!strncmp(buf, "enable", 6)) {
3327 /* Do not vote here. Let OTG statemachine decide when to vote */
3328 debug_bus_voting_enabled = true;
3329 } else {
3330 debug_bus_voting_enabled = false;
3331 if (motg->bus_perf_client) {
3332 ret = msm_bus_scale_client_update_request(
3333 motg->bus_perf_client, 0);
3334 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003335 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303336 "for bus bw %d\n", __func__, ret);
3337 }
3338 }
3339
3340 return count;
3341}
3342
3343const struct file_operations msm_otg_bus_fops = {
3344 .open = msm_otg_bus_open,
3345 .read = seq_read,
3346 .write = msm_otg_bus_write,
3347 .llseek = seq_lseek,
3348 .release = single_release,
3349};
3350
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303351static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303352
3353static int msm_otg_debugfs_init(struct msm_otg *motg)
3354{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303355 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303356
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303357 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3358
3359 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3360 return -ENODEV;
3361
Anji jonnalad270e2d2011-08-09 11:28:32 +05303362 if (motg->pdata->mode == USB_OTG &&
3363 motg->pdata->otg_control == OTG_USER_CONTROL) {
3364
Manu Gautam8bdcc592012-03-06 11:26:06 +05303365 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303366 S_IWUSR, msm_otg_dbg_root, motg,
3367 &msm_otg_mode_fops);
3368
Manu Gautam8bdcc592012-03-06 11:26:06 +05303369 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303370 debugfs_remove(msm_otg_dbg_root);
3371 msm_otg_dbg_root = NULL;
3372 return -ENODEV;
3373 }
3374 }
3375
Manu Gautam8bdcc592012-03-06 11:26:06 +05303376 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303377 msm_otg_dbg_root, motg,
3378 &msm_otg_chg_fops);
3379
Manu Gautam8bdcc592012-03-06 11:26:06 +05303380 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303381 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303382 return -ENODEV;
3383 }
3384
Manu Gautam8bdcc592012-03-06 11:26:06 +05303385 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303386 msm_otg_dbg_root, motg,
3387 &msm_otg_aca_fops);
3388
Manu Gautam8bdcc592012-03-06 11:26:06 +05303389 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303390 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303391 return -ENODEV;
3392 }
3393
Manu Gautam8bdcc592012-03-06 11:26:06 +05303394 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3395 msm_otg_dbg_root, motg,
3396 &msm_otg_bus_fops);
3397
3398 if (!msm_otg_dentry) {
3399 debugfs_remove_recursive(msm_otg_dbg_root);
3400 return -ENODEV;
3401 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303402
3403 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3404 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3405
3406 if (!msm_otg_dentry) {
3407 debugfs_remove_recursive(msm_otg_dbg_root);
3408 return -ENODEV;
3409 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303410 return 0;
3411}
3412
3413static void msm_otg_debugfs_cleanup(void)
3414{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303415 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303416}
3417
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303418static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3419static struct platform_device *msm_otg_add_pdev(
3420 struct platform_device *ofdev, const char *name)
3421{
3422 struct platform_device *pdev;
3423 const struct resource *res = ofdev->resource;
3424 unsigned int num = ofdev->num_resources;
3425 int retval;
3426
3427 pdev = platform_device_alloc(name, -1);
3428 if (!pdev) {
3429 retval = -ENOMEM;
3430 goto error;
3431 }
3432
3433 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3434 pdev->dev.dma_mask = &msm_otg_dma_mask;
3435
3436 if (num) {
3437 retval = platform_device_add_resources(pdev, res, num);
3438 if (retval)
3439 goto error;
3440 }
3441
3442 retval = platform_device_add(pdev);
3443 if (retval)
3444 goto error;
3445
3446 return pdev;
3447
3448error:
3449 platform_device_put(pdev);
3450 return ERR_PTR(retval);
3451}
3452
3453static int msm_otg_setup_devices(struct platform_device *ofdev,
3454 enum usb_mode_type mode, bool init)
3455{
3456 const char *gadget_name = "msm_hsusb";
3457 const char *host_name = "msm_hsusb_host";
3458 static struct platform_device *gadget_pdev;
3459 static struct platform_device *host_pdev;
3460 int retval = 0;
3461
3462 if (!init) {
3463 if (gadget_pdev)
3464 platform_device_unregister(gadget_pdev);
3465 if (host_pdev)
3466 platform_device_unregister(host_pdev);
3467 return 0;
3468 }
3469
3470 switch (mode) {
3471 case USB_OTG:
3472 /* fall through */
3473 case USB_PERIPHERAL:
3474 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3475 if (IS_ERR(gadget_pdev)) {
3476 retval = PTR_ERR(gadget_pdev);
3477 break;
3478 }
3479 if (mode == USB_PERIPHERAL)
3480 break;
3481 /* fall through */
3482 case USB_HOST:
3483 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3484 if (IS_ERR(host_pdev)) {
3485 retval = PTR_ERR(host_pdev);
3486 if (mode == USB_OTG)
3487 platform_device_unregister(gadget_pdev);
3488 }
3489 break;
3490 default:
3491 break;
3492 }
3493
3494 return retval;
3495}
3496
3497struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3498{
3499 struct device_node *node = pdev->dev.of_node;
3500 struct msm_otg_platform_data *pdata;
3501 int len = 0;
3502
3503 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3504 if (!pdata) {
3505 pr_err("unable to allocate platform data\n");
3506 return NULL;
3507 }
3508 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3509 if (len) {
3510 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3511 if (!pdata->phy_init_seq)
3512 return NULL;
3513 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3514 pdata->phy_init_seq,
3515 len/sizeof(*pdata->phy_init_seq));
3516 }
3517 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3518 &pdata->power_budget);
3519 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3520 &pdata->mode);
3521 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3522 &pdata->otg_control);
3523 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3524 &pdata->default_mode);
3525 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3526 &pdata->phy_type);
3527 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3528 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303529 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3530 "qcom,hsusb-otg-disable-reset");
3531
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303532 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303533}
3534
3535static int __init msm_otg_probe(struct platform_device *pdev)
3536{
3537 int ret = 0;
3538 struct resource *res;
3539 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003540 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303541 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303542
3543 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303544
3545 if (pdev->dev.of_node) {
3546 dev_dbg(&pdev->dev, "device tree enabled\n");
3547 pdata = msm_otg_dt_to_pdata(pdev);
3548 if (!pdata)
3549 return -ENOMEM;
3550 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3551 if (ret) {
3552 dev_err(&pdev->dev, "devices setup failed\n");
3553 return ret;
3554 }
3555 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303556 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3557 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303558 } else {
3559 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303560 }
3561
3562 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3563 if (!motg) {
3564 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3565 return -ENOMEM;
3566 }
3567
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003568 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3569 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003570 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3571 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303572 goto free_motg;
3573 }
3574
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003575 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303576 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003577 phy = &motg->phy;
3578 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303579
3580 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303581 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3582 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3583 * PMIC can detect ACA ID_GND and generate an interrupt.
3584 */
3585 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3586 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3587 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003588 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303589 }
3590
Ofir Cohen4da266f2012-01-03 10:19:29 +02003591 /* initialize reset counter */
3592 motg->reset_counter = 0;
3593
Amit Blay02eff132011-09-21 16:46:24 +03003594 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003595 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003596 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003597 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303598
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303599 /*
3600 * Targets on which link uses asynchronous reset methodology,
3601 * free running clock is not required during the reset.
3602 */
Manu Gautam5143b252012-01-05 19:25:23 -08003603 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303604 if (IS_ERR(motg->clk))
3605 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3606 else
3607 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303608
3609 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003610 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303611 * CORE CLK must be running at >55Mhz for correct HSUSB
3612 * operation and USB core cannot tolerate frequency changes on
3613 * CORE CLK. For such USB cores, vote for maximum clk frequency
3614 * on pclk source
3615 */
Manu Gautam5143b252012-01-05 19:25:23 -08003616 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3617 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303618 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003619 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303620 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003621 goto put_clk;
3622 }
3623 clk_set_rate(motg->core_clk, INT_MAX);
3624
3625 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3626 if (IS_ERR(motg->pclk)) {
3627 dev_err(&pdev->dev, "failed to get iface_clk\n");
3628 ret = PTR_ERR(motg->pclk);
3629 goto put_core_clk;
3630 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303631
3632 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3633 if (!res) {
3634 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3635 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003636 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303637 }
3638
3639 motg->regs = ioremap(res->start, resource_size(res));
3640 if (!motg->regs) {
3641 dev_err(&pdev->dev, "ioremap failed\n");
3642 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003643 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303644 }
3645 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3646
3647 motg->irq = platform_get_irq(pdev, 0);
3648 if (!motg->irq) {
3649 dev_err(&pdev->dev, "platform_get_irq failed\n");
3650 ret = -ENODEV;
3651 goto free_regs;
3652 }
3653
Manu Gautamf8c45642012-08-10 10:20:56 -07003654 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3655 if (motg->async_irq < 0) {
3656 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3657 motg->async_irq = 0;
3658 }
3659
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003660 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003661 if (IS_ERR(motg->xo_handle)) {
3662 dev_err(&pdev->dev, "%s not able to get the handle "
3663 "to vote for TCXO D0 buffer\n", __func__);
3664 ret = PTR_ERR(motg->xo_handle);
3665 goto free_regs;
3666 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303667
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003668 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003669 if (ret) {
3670 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3671 "D0 buffer%d\n", __func__, ret);
3672 goto free_xo_handle;
3673 }
3674
Manu Gautam28b1bac2012-01-30 16:43:06 +05303675 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303676
Mayank Rana248698c2012-04-19 00:03:16 +05303677 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003678 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303679 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003680 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303681 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003682 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003683 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303684 goto devote_xo_handle;
3685 }
3686 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303687 }
3688
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003689 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303690 if (ret) {
3691 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303692 goto devote_xo_handle;
3693 }
3694
3695 ret = regulator_enable(hsusb_vddcx);
3696 if (ret) {
3697 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3698 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303699 }
3700
3701 ret = msm_hsusb_ldo_init(motg, 1);
3702 if (ret) {
3703 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303704 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303705 }
3706
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303707 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303708 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3709 "mhl_usb_hs_switch");
3710 if (IS_ERR(mhl_usb_hs_switch)) {
3711 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003712 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303713 goto free_ldo_init;
3714 }
3715 }
3716
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003717 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303718 if (ret) {
3719 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003720 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303721 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303722 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303723
3724 writel(0, USB_USBINTR);
3725 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003726 /* Ensure that above STOREs are completed before enabling interrupts */
3727 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303728
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303729 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3730 if (ret)
3731 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003732 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303733 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303734 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303735 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303736 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003737 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303738 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3739 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303740 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3741 "msm_otg", motg);
3742 if (ret) {
3743 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003744 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303745 }
3746
Manu Gautamf8c45642012-08-10 10:20:56 -07003747 if (motg->async_irq) {
3748 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3749 "msm_otg", motg);
3750 if (ret) {
3751 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3752 goto free_irq;
3753 }
3754 disable_irq(motg->async_irq);
3755 }
3756
Jack Pham87f202f2012-08-06 00:24:22 -07003757 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3758 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3759
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003760 phy->init = msm_otg_reset;
3761 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003762 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303763
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003764 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303765
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003766 phy->otg->phy = &motg->phy;
3767 phy->otg->set_host = msm_otg_set_host;
3768 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003769 phy->otg->start_hnp = msm_otg_start_hnp;
3770 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003771
3772 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303773 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003774 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003775 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303776 }
3777
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303778 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303779 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003780 if (motg->pdata->pmic_id_irq) {
3781 ret = request_irq(motg->pdata->pmic_id_irq,
3782 msm_pmic_id_irq,
3783 IRQF_TRIGGER_RISING |
3784 IRQF_TRIGGER_FALLING,
3785 "msm_otg", motg);
3786 if (ret) {
3787 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003788 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003789 }
3790 } else {
3791 ret = -ENODEV;
3792 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003793 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003794 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303795 }
3796
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303797 msm_hsusb_mhl_switch_enable(motg, 1);
3798
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303799 platform_set_drvdata(pdev, motg);
3800 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003801 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303802
Anji jonnalad270e2d2011-08-09 11:28:32 +05303803 ret = msm_otg_debugfs_init(motg);
3804 if (ret)
3805 dev_dbg(&pdev->dev, "mode debugfs file is"
3806 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003808 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3809 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3810
Amit Blay58b31472011-11-18 09:39:39 +02003811 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3812 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303813 (!(motg->pdata->mode == USB_OTG) ||
3814 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003815 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303816 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003817
Amit Blay58b31472011-11-18 09:39:39 +02003818 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3819 motg->caps = ALLOW_PHY_RETENTION;
3820 }
3821
Amit Blay6fa647a2012-05-24 14:12:08 +03003822 if (motg->pdata->enable_lpm_on_dev_suspend)
3823 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3824
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003825 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303826 pm_runtime_set_active(&pdev->dev);
3827 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303828
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303829 if (motg->pdata->bus_scale_table) {
3830 motg->bus_perf_client =
3831 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3832 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003833 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303834 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303835 else
3836 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303837 }
3838
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303839 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003840
Steve Mucklef132c6c2012-06-06 18:30:57 -07003841remove_phy:
3842 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003843free_async_irq:
3844 if (motg->async_irq)
3845 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303846free_irq:
3847 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003848destroy_wlock:
3849 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303850 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003851 msm_hsusb_ldo_enable(motg, 0);
3852free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303853 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303854free_hsusb_vddcx:
3855 regulator_disable(hsusb_vddcx);
3856free_config_vddcx:
3857 regulator_set_voltage(hsusb_vddcx,
3858 vdd_val[motg->vdd_type][VDD_NONE],
3859 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003860devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303861 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003862 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003863free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003864 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303865free_regs:
3866 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003867put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303868 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303869put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003870 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303871put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303872 if (!IS_ERR(motg->clk))
3873 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003874 if (!IS_ERR(motg->phy_reset_clk))
3875 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003876free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003877 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303878free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303879 kfree(motg);
3880 return ret;
3881}
3882
3883static int __devexit msm_otg_remove(struct platform_device *pdev)
3884{
3885 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003886 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303887 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303888
3889 if (otg->host || otg->gadget)
3890 return -EBUSY;
3891
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303892 if (pdev->dev.of_node)
3893 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003894 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3895 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303896 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303897 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303898 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303899 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003900 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303901 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303902
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303903 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303904
3905 device_init_wakeup(&pdev->dev, 0);
3906 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003907 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303908
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303909 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003910 if (motg->pdata->pmic_id_irq)
3911 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003912 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303913 free_irq(motg->irq, motg);
3914
Jack Pham87f202f2012-08-06 00:24:22 -07003915 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3916 motg->pdata->mpm_otgsessvld_int)
3917 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3918
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303919 /*
3920 * Put PHY in low power mode.
3921 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003922 ulpi_read(otg->phy, 0x14);
3923 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303924
3925 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3926 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3927 if (readl(USB_PORTSC) & PORTSC_PHCD)
3928 break;
3929 udelay(1);
3930 cnt++;
3931 }
3932 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003933 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303934
Manu Gautam28b1bac2012-01-30 16:43:06 +05303935 clk_disable_unprepare(motg->pclk);
3936 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003937 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003938 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303939 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303940 regulator_disable(hsusb_vddcx);
3941 regulator_set_voltage(hsusb_vddcx,
3942 vdd_val[motg->vdd_type][VDD_NONE],
3943 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303944
3945 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303946 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303947
Amit Blay02eff132011-09-21 16:46:24 +03003948 if (!IS_ERR(motg->phy_reset_clk))
3949 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303950 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303951 if (!IS_ERR(motg->clk))
3952 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003953 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303954
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303955 if (motg->bus_perf_client)
3956 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303957
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003958 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303959 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303960 return 0;
3961}
3962
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303963#ifdef CONFIG_PM_RUNTIME
3964static int msm_otg_runtime_idle(struct device *dev)
3965{
3966 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003967 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303968
3969 dev_dbg(dev, "OTG runtime idle\n");
3970
Steve Mucklef132c6c2012-06-06 18:30:57 -07003971 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303972 return -EAGAIN;
3973 else
3974 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303975}
3976
3977static int msm_otg_runtime_suspend(struct device *dev)
3978{
3979 struct msm_otg *motg = dev_get_drvdata(dev);
3980
3981 dev_dbg(dev, "OTG runtime suspend\n");
3982 return msm_otg_suspend(motg);
3983}
3984
3985static int msm_otg_runtime_resume(struct device *dev)
3986{
3987 struct msm_otg *motg = dev_get_drvdata(dev);
3988
3989 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303990 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303991 return msm_otg_resume(motg);
3992}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303993#endif
3994
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303995#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303996static int msm_otg_pm_suspend(struct device *dev)
3997{
Jack Pham5ca279b2012-05-14 18:42:54 -07003998 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303999 struct msm_otg *motg = dev_get_drvdata(dev);
4000
4001 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004002
4003 atomic_set(&motg->pm_suspended, 1);
4004 ret = msm_otg_suspend(motg);
4005 if (ret)
4006 atomic_set(&motg->pm_suspended, 0);
4007
4008 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304009}
4010
4011static int msm_otg_pm_resume(struct device *dev)
4012{
Jack Pham5ca279b2012-05-14 18:42:54 -07004013 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304014 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304015
4016 dev_dbg(dev, "OTG PM resume\n");
4017
Jack Pham5ca279b2012-05-14 18:42:54 -07004018 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07004019 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004020 pm_runtime_get_noresume(dev);
4021 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304022
Jack Pham5ca279b2012-05-14 18:42:54 -07004023 /* Update runtime PM status */
4024 pm_runtime_disable(dev);
4025 pm_runtime_set_active(dev);
4026 pm_runtime_enable(dev);
4027
Jack Phamb1aa4d82012-08-13 15:32:39 -07004028 if (motg->sm_work_pending) {
4029 motg->sm_work_pending = false;
4030 queue_work(system_nrt_wq, &motg->sm_work);
4031 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004032 }
4033
4034 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304035}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304036#endif
4037
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304038#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304039static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304040 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4041 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4042 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304043};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304044#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304045
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304046static struct of_device_id msm_otg_dt_match[] = {
4047 { .compatible = "qcom,hsusb-otg",
4048 },
4049 {}
4050};
4051
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304052static struct platform_driver msm_otg_driver = {
4053 .remove = __devexit_p(msm_otg_remove),
4054 .driver = {
4055 .name = DRIVER_NAME,
4056 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304057#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304058 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304059#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304060 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304061 },
4062};
4063
4064static int __init msm_otg_init(void)
4065{
4066 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4067}
4068
4069static void __exit msm_otg_exit(void)
4070{
4071 platform_driver_unregister(&msm_otg_driver);
4072}
4073
4074module_init(msm_otg_init);
4075module_exit(msm_otg_exit);
4076
4077MODULE_LICENSE("GPL v2");
4078MODULE_DESCRIPTION("MSM USB transceiver driver");