blob: 7eb2abf3f8741ee98a49f47f51afa82fffa986fc [file] [log] [blame]
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301/* Copyright (c) 2009-2011, Code Aurora Forum. 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 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/device.h>
21#include <linux/platform_device.h>
22#include <linux/clk.h>
23#include <linux/slab.h>
24#include <linux/interrupt.h>
25#include <linux/err.h>
26#include <linux/delay.h>
27#include <linux/io.h>
28#include <linux/ioport.h>
29#include <linux/uaccess.h>
30#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053032#include <linux/pm_runtime.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053033
34#include <linux/usb.h>
35#include <linux/usb/otg.h>
36#include <linux/usb/ulpi.h>
37#include <linux/usb/gadget.h>
38#include <linux/usb/hcd.h>
39#include <linux/usb/msm_hsusb.h>
40#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053041#include <linux/regulator/consumer.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053042
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053043#define MSM_USB_BASE (motg->regs)
44#define DRIVER_NAME "msm_otg"
45
46#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053047
48#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
49#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
50#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
51#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
52
53#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
54#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
55#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
56#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
57
58#define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
59#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
60
Anji jonnala11aa5c42011-05-04 10:19:48 +053061static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
62{
63 int ret = 0;
64
65 if (init) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030066 ret = regulator_set_voltage(motg->vddcx,
Anji jonnala11aa5c42011-05-04 10:19:48 +053067 USB_PHY_VDD_DIG_VOL_MIN,
68 USB_PHY_VDD_DIG_VOL_MAX);
69 if (ret) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +030070 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053071 return ret;
72 }
73
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030074 ret = regulator_enable(motg->vddcx);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +030075 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +020076 dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053077 } else {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030078 ret = regulator_set_voltage(motg->vddcx, 0,
Mark Brown7b521fc2011-05-15 09:55:57 -070079 USB_PHY_VDD_DIG_VOL_MAX);
Mark Browne99c4302011-05-15 09:55:58 -070080 if (ret)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +030081 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030082 ret = regulator_disable(motg->vddcx);
Anji jonnala11aa5c42011-05-04 10:19:48 +053083 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +020084 dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053085 }
86
87 return ret;
88}
89
90static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
91{
92 int rc = 0;
93
94 if (init) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030095 rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
Anji jonnala11aa5c42011-05-04 10:19:48 +053096 USB_PHY_3P3_VOL_MAX);
97 if (rc) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +030098 dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +030099 goto exit;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530100 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300101 rc = regulator_enable(motg->v3p3);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102 if (rc) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200103 dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300104 goto exit;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530105 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300106 rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530107 USB_PHY_1P8_VOL_MAX);
108 if (rc) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300109 dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300110 goto disable_3p3;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530111 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300112 rc = regulator_enable(motg->v1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530113 if (rc) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200114 dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300115 goto disable_3p3;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530116 }
117
118 return 0;
119 }
120
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300121 regulator_disable(motg->v1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530122disable_3p3:
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300123 regulator_disable(motg->v3p3);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300124exit:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530125 return rc;
126}
127
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300128static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530129{
130 int ret = 0;
131
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 if (on) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300133 ret = regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530134 USB_PHY_1P8_HPM_LOAD);
135 if (ret < 0) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300136 pr_err("Could not set HPM for v1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530137 return ret;
138 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300139 ret = regulator_set_optimum_mode(motg->v3p3,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530140 USB_PHY_3P3_HPM_LOAD);
141 if (ret < 0) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300142 pr_err("Could not set HPM for v3p3\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300143 regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144 USB_PHY_1P8_LPM_LOAD);
145 return ret;
146 }
147 } else {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300148 ret = regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530149 USB_PHY_1P8_LPM_LOAD);
150 if (ret < 0)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300151 pr_err("Could not set LPM for v1p8\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300152 ret = regulator_set_optimum_mode(motg->v3p3,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530153 USB_PHY_3P3_LPM_LOAD);
154 if (ret < 0)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300155 pr_err("Could not set LPM for v3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530156 }
157
158 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
159 return ret < 0 ? ret : 0;
160}
161
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200162static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530163{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200164 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530165 int cnt = 0;
166
167 /* initiate read operation */
168 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
169 USB_ULPI_VIEWPORT);
170
171 /* wait for completion */
172 while (cnt < ULPI_IO_TIMEOUT_USEC) {
173 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
174 break;
175 udelay(1);
176 cnt++;
177 }
178
179 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200180 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530181 readl(USB_ULPI_VIEWPORT));
182 return -ETIMEDOUT;
183 }
184 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
185}
186
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200187static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530188{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200189 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530190 int cnt = 0;
191
192 /* initiate write operation */
193 writel(ULPI_RUN | ULPI_WRITE |
194 ULPI_ADDR(reg) | ULPI_DATA(val),
195 USB_ULPI_VIEWPORT);
196
197 /* wait for completion */
198 while (cnt < ULPI_IO_TIMEOUT_USEC) {
199 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
200 break;
201 udelay(1);
202 cnt++;
203 }
204
205 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200206 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530207 return -ETIMEDOUT;
208 }
209 return 0;
210}
211
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200212static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530213 .read = ulpi_read,
214 .write = ulpi_write,
215};
216
217static void ulpi_init(struct msm_otg *motg)
218{
219 struct msm_otg_platform_data *pdata = motg->pdata;
220 int *seq = pdata->phy_init_seq;
221
222 if (!seq)
223 return;
224
225 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200226 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530227 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200228 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530229 seq += 2;
230 }
231}
232
233static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
234{
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800235 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530236
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800237 if (!motg->pdata->link_clk_reset)
238 return ret;
239
240 ret = motg->pdata->link_clk_reset(motg->clk, assert);
241 if (ret)
242 dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
243 assert ? "assert" : "deassert");
244
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530245 return ret;
246}
247
248static int msm_otg_phy_clk_reset(struct msm_otg *motg)
249{
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800250 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530251
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800252 if (!motg->pdata->phy_clk_reset)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530253 return ret;
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800254
255 ret = motg->pdata->phy_clk_reset(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530256 if (ret)
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800257 dev_err(motg->phy.dev, "usb phy clk reset failed\n");
258
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530259 return ret;
260}
261
262static int msm_otg_phy_reset(struct msm_otg *motg)
263{
264 u32 val;
265 int ret;
266 int retries;
267
268 ret = msm_otg_link_clk_reset(motg, 1);
269 if (ret)
270 return ret;
271 ret = msm_otg_phy_clk_reset(motg);
272 if (ret)
273 return ret;
274 ret = msm_otg_link_clk_reset(motg, 0);
275 if (ret)
276 return ret;
277
278 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
279 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
280
281 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200282 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530283 ULPI_CLR(ULPI_FUNC_CTRL));
284 if (!ret)
285 break;
286 ret = msm_otg_phy_clk_reset(motg);
287 if (ret)
288 return ret;
289 }
290 if (!retries)
291 return -ETIMEDOUT;
292
293 /* This reset calibrates the phy, if the above write succeeded */
294 ret = msm_otg_phy_clk_reset(motg);
295 if (ret)
296 return ret;
297
298 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200299 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530300 if (ret != -ETIMEDOUT)
301 break;
302 ret = msm_otg_phy_clk_reset(motg);
303 if (ret)
304 return ret;
305 }
306 if (!retries)
307 return -ETIMEDOUT;
308
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200309 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530310 return 0;
311}
312
313#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200314static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530315{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200316 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530317 struct msm_otg_platform_data *pdata = motg->pdata;
318 int cnt = 0;
319 int ret;
320 u32 val = 0;
321 u32 ulpi_val = 0;
322
323 ret = msm_otg_phy_reset(motg);
324 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200325 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530326 return ret;
327 }
328
329 ulpi_init(motg);
330
331 writel(USBCMD_RESET, USB_USBCMD);
332 while (cnt < LINK_RESET_TIMEOUT_USEC) {
333 if (!(readl(USB_USBCMD) & USBCMD_RESET))
334 break;
335 udelay(1);
336 cnt++;
337 }
338 if (cnt >= LINK_RESET_TIMEOUT_USEC)
339 return -ETIMEDOUT;
340
341 /* select ULPI phy */
342 writel(0x80000000, USB_PORTSC);
343
344 msleep(100);
345
346 writel(0x0, USB_AHBBURST);
347 writel(0x00, USB_AHBMODE);
348
349 if (pdata->otg_control == OTG_PHY_CONTROL) {
350 val = readl(USB_OTGSC);
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300351 if (pdata->mode == USB_DR_MODE_OTG) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530352 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
353 val |= OTGSC_IDIE | OTGSC_BSVIE;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300354 } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530355 ulpi_val = ULPI_INT_SESS_VALID;
356 val |= OTGSC_BSVIE;
357 }
358 writel(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200359 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
360 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530361 }
362
363 return 0;
364}
365
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530366#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530367#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
368
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600369#ifdef CONFIG_PM
370
371#define USB_PHY_SUSP_DIG_VOL 500000
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300372static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600373{
374 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
375 int min_vol;
376 int ret;
377
378 if (high)
379 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
380 else
381 min_vol = USB_PHY_SUSP_DIG_VOL;
382
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300383 ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600384 if (ret) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300385 pr_err("Cannot set vddcx voltage\n");
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600386 return ret;
387 }
388
389 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
390
391 return ret;
392}
393
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530394static int msm_otg_suspend(struct msm_otg *motg)
395{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200396 struct usb_phy *phy = &motg->phy;
397 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530398 struct msm_otg_platform_data *pdata = motg->pdata;
399 int cnt = 0;
400
401 if (atomic_read(&motg->in_lpm))
402 return 0;
403
404 disable_irq(motg->irq);
405 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530406 * Chipidea 45-nm PHY suspend sequence:
407 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530408 * Interrupt Latch Register auto-clear feature is not present
409 * in all PHY versions. Latch register is clear on read type.
410 * Clear latch register to avoid spurious wakeup from
411 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530412 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530413 * PHY comparators are disabled when PHY enters into low power
414 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
415 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
416 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530417 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530418 * PLL is not turned off when PHY enters into low power mode (LPM).
419 * Disable PLL for maximum power savings.
420 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530421
422 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200423 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530424 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200425 ulpi_write(phy, 0x01, 0x30);
426 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530427 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530428
429 /*
430 * PHY may take some time or even fail to enter into low power
431 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
432 * in failure case.
433 */
434 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
435 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
436 if (readl(USB_PORTSC) & PORTSC_PHCD)
437 break;
438 udelay(1);
439 cnt++;
440 }
441
442 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200443 dev_err(phy->dev, "Unable to suspend PHY\n");
444 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530445 enable_irq(motg->irq);
446 return -ETIMEDOUT;
447 }
448
449 /*
450 * PHY has capability to generate interrupt asynchronously in low
451 * power mode (LPM). This interrupt is level triggered. So USB IRQ
452 * line must be disabled till async interrupt enable bit is cleared
453 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
454 * block data communication from PHY.
455 */
456 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
457
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530458 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
459 motg->pdata->otg_control == OTG_PMIC_CONTROL)
460 writel(readl(USB_PHY_CTRL) | PHY_RETEN, USB_PHY_CTRL);
461
Stephen Boydb99a8f62013-06-17 10:43:10 -0700462 clk_disable_unprepare(motg->pclk);
463 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300464 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700465 clk_disable_unprepare(motg->core_clk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530466
Anji jonnala0f73cac2011-05-04 10:19:46 +0530467 if (!IS_ERR(motg->pclk_src))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700468 clk_disable_unprepare(motg->pclk_src);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530469
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530470 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
471 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300472 msm_hsusb_ldo_set_mode(motg, 0);
473 msm_hsusb_config_vddcx(motg, 0);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530474 }
475
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200476 if (device_may_wakeup(phy->dev))
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530477 enable_irq_wake(motg->irq);
478 if (bus)
479 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
480
481 atomic_set(&motg->in_lpm, 1);
482 enable_irq(motg->irq);
483
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200484 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530485
486 return 0;
487}
488
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530489static int msm_otg_resume(struct msm_otg *motg)
490{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200491 struct usb_phy *phy = &motg->phy;
492 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530493 int cnt = 0;
494 unsigned temp;
495
496 if (!atomic_read(&motg->in_lpm))
497 return 0;
498
Anji jonnala0f73cac2011-05-04 10:19:46 +0530499 if (!IS_ERR(motg->pclk_src))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700500 clk_prepare_enable(motg->pclk_src);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530501
Stephen Boydb99a8f62013-06-17 10:43:10 -0700502 clk_prepare_enable(motg->pclk);
503 clk_prepare_enable(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300504 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700505 clk_prepare_enable(motg->core_clk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530506
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530507 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
508 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300509 msm_hsusb_ldo_set_mode(motg, 1);
510 msm_hsusb_config_vddcx(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530511 writel(readl(USB_PHY_CTRL) & ~PHY_RETEN, USB_PHY_CTRL);
512 }
513
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530514 temp = readl(USB_USBCMD);
515 temp &= ~ASYNC_INTR_CTRL;
516 temp &= ~ULPI_STP_CTRL;
517 writel(temp, USB_USBCMD);
518
519 /*
520 * PHY comes out of low power mode (LPM) in case of wakeup
521 * from asynchronous interrupt.
522 */
523 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
524 goto skip_phy_resume;
525
526 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
527 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
528 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
529 break;
530 udelay(1);
531 cnt++;
532 }
533
534 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
535 /*
536 * This is a fatal error. Reset the link and
537 * PHY. USB state can not be restored. Re-insertion
538 * of USB cable is the only way to get USB working.
539 */
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300540 dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200541 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530542 }
543
544skip_phy_resume:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200545 if (device_may_wakeup(phy->dev))
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530546 disable_irq_wake(motg->irq);
547 if (bus)
548 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
549
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530550 atomic_set(&motg->in_lpm, 0);
551
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530552 if (motg->async_int) {
553 motg->async_int = 0;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200554 pm_runtime_put(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530555 enable_irq(motg->irq);
556 }
557
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200558 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530559
560 return 0;
561}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530562#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530563
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530564static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
565{
566 if (motg->cur_power == mA)
567 return;
568
569 /* TODO: Notify PMIC about available current */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200570 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530571 motg->cur_power = mA;
572}
573
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200574static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530575{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200576 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530577
578 /*
579 * Gadget driver uses set_power method to notify about the
580 * available current based on suspend/configured states.
581 *
582 * IDEV_CHG can be drawn irrespective of suspend/un-configured
583 * states when CDP/ACA is connected.
584 */
585 if (motg->chg_type == USB_SDP_CHARGER)
586 msm_otg_notify_charger(motg, mA);
587
588 return 0;
589}
590
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200591static void msm_otg_start_host(struct usb_phy *phy, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530592{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200593 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530594 struct msm_otg_platform_data *pdata = motg->pdata;
595 struct usb_hcd *hcd;
596
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200597 if (!phy->otg->host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530598 return;
599
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200600 hcd = bus_to_hcd(phy->otg->host);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530601
602 if (on) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200603 dev_dbg(phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530604
605 if (pdata->vbus_power)
606 pdata->vbus_power(1);
607 /*
608 * Some boards have a switch cotrolled by gpio
609 * to enable/disable internal HUB. Enable internal
610 * HUB before kicking the host.
611 */
612 if (pdata->setup_gpio)
613 pdata->setup_gpio(OTG_STATE_A_HOST);
614#ifdef CONFIG_USB
615 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Peter Chen3c9740a2013-11-05 10:46:02 +0800616 device_wakeup_enable(hcd->self.controller);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530617#endif
618 } else {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200619 dev_dbg(phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530620
621#ifdef CONFIG_USB
622 usb_remove_hcd(hcd);
623#endif
624 if (pdata->setup_gpio)
625 pdata->setup_gpio(OTG_STATE_UNDEFINED);
626 if (pdata->vbus_power)
627 pdata->vbus_power(0);
628 }
629}
630
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200631static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530632{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200633 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530634 struct usb_hcd *hcd;
635
636 /*
637 * Fail host registration if this board can support
638 * only peripheral configuration.
639 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300640 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200641 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530642 return -ENODEV;
643 }
644
645 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200646 if (otg->phy->state == OTG_STATE_A_HOST) {
647 pm_runtime_get_sync(otg->phy->dev);
648 msm_otg_start_host(otg->phy, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530649 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200650 otg->phy->state = OTG_STATE_UNDEFINED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530651 schedule_work(&motg->sm_work);
652 } else {
653 otg->host = NULL;
654 }
655
656 return 0;
657 }
658
659 hcd = bus_to_hcd(host);
660 hcd->power_budget = motg->pdata->power_budget;
661
662 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200663 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530664
665 /*
666 * Kick the state machine work, if peripheral is not supported
667 * or peripheral is already registered with us.
668 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300669 if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200670 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530671 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530672 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530673
674 return 0;
675}
676
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200677static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530678{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200679 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530680 struct msm_otg_platform_data *pdata = motg->pdata;
681
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200682 if (!phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530683 return;
684
685 if (on) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200686 dev_dbg(phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530687 /*
688 * Some boards have a switch cotrolled by gpio
689 * to enable/disable internal HUB. Disable internal
690 * HUB before kicking the gadget.
691 */
692 if (pdata->setup_gpio)
693 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200694 usb_gadget_vbus_connect(phy->otg->gadget);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530695 } else {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200696 dev_dbg(phy->dev, "gadget off\n");
697 usb_gadget_vbus_disconnect(phy->otg->gadget);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530698 if (pdata->setup_gpio)
699 pdata->setup_gpio(OTG_STATE_UNDEFINED);
700 }
701
702}
703
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200704static int msm_otg_set_peripheral(struct usb_otg *otg,
705 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530706{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200707 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530708
709 /*
710 * Fail peripheral registration if this board can support
711 * only host configuration.
712 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300713 if (motg->pdata->mode == USB_DR_MODE_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200714 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530715 return -ENODEV;
716 }
717
718 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200719 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
720 pm_runtime_get_sync(otg->phy->dev);
721 msm_otg_start_peripheral(otg->phy, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530722 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200723 otg->phy->state = OTG_STATE_UNDEFINED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530724 schedule_work(&motg->sm_work);
725 } else {
726 otg->gadget = NULL;
727 }
728
729 return 0;
730 }
731 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200732 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530733
734 /*
735 * Kick the state machine work, if host is not supported
736 * or host is already registered with us.
737 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300738 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200739 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530740 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530741 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530742
743 return 0;
744}
745
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530746static bool msm_chg_check_secondary_det(struct msm_otg *motg)
747{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200748 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530749 u32 chg_det;
750 bool ret = false;
751
752 switch (motg->pdata->phy_type) {
753 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200754 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530755 ret = chg_det & (1 << 4);
756 break;
757 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200758 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530759 ret = chg_det & 1;
760 break;
761 default:
762 break;
763 }
764 return ret;
765}
766
767static void msm_chg_enable_secondary_det(struct msm_otg *motg)
768{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200769 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530770 u32 chg_det;
771
772 switch (motg->pdata->phy_type) {
773 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200774 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530775 /* Turn off charger block */
776 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200777 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530778 udelay(20);
779 /* control chg block via ULPI */
780 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200781 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530782 /* put it in host mode for enabling D- source */
783 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200784 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530785 /* Turn on chg detect block */
786 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200787 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530788 udelay(20);
789 /* enable chg detection */
790 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200791 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530792 break;
793 case SNPS_28NM_INTEGRATED_PHY:
794 /*
795 * Configure DM as current source, DP as current sink
796 * and enable battery charging comparators.
797 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200798 ulpi_write(phy, 0x8, 0x85);
799 ulpi_write(phy, 0x2, 0x85);
800 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530801 break;
802 default:
803 break;
804 }
805}
806
807static bool msm_chg_check_primary_det(struct msm_otg *motg)
808{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200809 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530810 u32 chg_det;
811 bool ret = false;
812
813 switch (motg->pdata->phy_type) {
814 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200815 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530816 ret = chg_det & (1 << 4);
817 break;
818 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200819 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530820 ret = chg_det & 1;
821 break;
822 default:
823 break;
824 }
825 return ret;
826}
827
828static void msm_chg_enable_primary_det(struct msm_otg *motg)
829{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200830 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530831 u32 chg_det;
832
833 switch (motg->pdata->phy_type) {
834 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200835 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530836 /* enable chg detection */
837 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200838 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530839 break;
840 case SNPS_28NM_INTEGRATED_PHY:
841 /*
842 * Configure DP as current source, DM as current sink
843 * and enable battery charging comparators.
844 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200845 ulpi_write(phy, 0x2, 0x85);
846 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530847 break;
848 default:
849 break;
850 }
851}
852
853static bool msm_chg_check_dcd(struct msm_otg *motg)
854{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200855 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530856 u32 line_state;
857 bool ret = false;
858
859 switch (motg->pdata->phy_type) {
860 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200861 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530862 ret = !(line_state & 1);
863 break;
864 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200865 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530866 ret = line_state & 2;
867 break;
868 default:
869 break;
870 }
871 return ret;
872}
873
874static void msm_chg_disable_dcd(struct msm_otg *motg)
875{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200876 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530877 u32 chg_det;
878
879 switch (motg->pdata->phy_type) {
880 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200881 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530882 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200883 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530884 break;
885 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200886 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530887 break;
888 default:
889 break;
890 }
891}
892
893static void msm_chg_enable_dcd(struct msm_otg *motg)
894{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200895 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530896 u32 chg_det;
897
898 switch (motg->pdata->phy_type) {
899 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200900 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530901 /* Turn on D+ current source */
902 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200903 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530904 break;
905 case SNPS_28NM_INTEGRATED_PHY:
906 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200907 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530908 break;
909 default:
910 break;
911 }
912}
913
914static void msm_chg_block_on(struct msm_otg *motg)
915{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200916 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530917 u32 func_ctrl, chg_det;
918
919 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200920 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530921 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
922 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200923 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530924
925 switch (motg->pdata->phy_type) {
926 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200927 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530928 /* control chg block via ULPI */
929 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200930 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530931 /* Turn on chg detect block */
932 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200933 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530934 udelay(20);
935 break;
936 case SNPS_28NM_INTEGRATED_PHY:
937 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200938 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530939 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200940 ulpi_write(phy, 0x1F, 0x92);
941 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530942 udelay(100);
943 break;
944 default:
945 break;
946 }
947}
948
949static void msm_chg_block_off(struct msm_otg *motg)
950{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200951 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530952 u32 func_ctrl, chg_det;
953
954 switch (motg->pdata->phy_type) {
955 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200956 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530957 /* Turn off charger block */
958 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200959 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530960 break;
961 case SNPS_28NM_INTEGRATED_PHY:
962 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200963 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530964 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200965 ulpi_write(phy, 0x1F, 0x92);
966 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530967 break;
968 default:
969 break;
970 }
971
972 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200973 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530974 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
975 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200976 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530977}
978
979#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
980#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
981#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
982#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
983static void msm_chg_detect_work(struct work_struct *w)
984{
985 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200986 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530987 bool is_dcd, tmout, vout;
988 unsigned long delay;
989
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200990 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530991 switch (motg->chg_state) {
992 case USB_CHG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200993 pm_runtime_get_sync(phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530994 msm_chg_block_on(motg);
995 msm_chg_enable_dcd(motg);
996 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
997 motg->dcd_retries = 0;
998 delay = MSM_CHG_DCD_POLL_TIME;
999 break;
1000 case USB_CHG_STATE_WAIT_FOR_DCD:
1001 is_dcd = msm_chg_check_dcd(motg);
1002 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1003 if (is_dcd || tmout) {
1004 msm_chg_disable_dcd(motg);
1005 msm_chg_enable_primary_det(motg);
1006 delay = MSM_CHG_PRIMARY_DET_TIME;
1007 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1008 } else {
1009 delay = MSM_CHG_DCD_POLL_TIME;
1010 }
1011 break;
1012 case USB_CHG_STATE_DCD_DONE:
1013 vout = msm_chg_check_primary_det(motg);
1014 if (vout) {
1015 msm_chg_enable_secondary_det(motg);
1016 delay = MSM_CHG_SECONDARY_DET_TIME;
1017 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1018 } else {
1019 motg->chg_type = USB_SDP_CHARGER;
1020 motg->chg_state = USB_CHG_STATE_DETECTED;
1021 delay = 0;
1022 }
1023 break;
1024 case USB_CHG_STATE_PRIMARY_DONE:
1025 vout = msm_chg_check_secondary_det(motg);
1026 if (vout)
1027 motg->chg_type = USB_DCP_CHARGER;
1028 else
1029 motg->chg_type = USB_CDP_CHARGER;
1030 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1031 /* fall through */
1032 case USB_CHG_STATE_SECONDARY_DONE:
1033 motg->chg_state = USB_CHG_STATE_DETECTED;
1034 case USB_CHG_STATE_DETECTED:
1035 msm_chg_block_off(motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001036 dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301037 schedule_work(&motg->sm_work);
1038 return;
1039 default:
1040 return;
1041 }
1042
1043 schedule_delayed_work(&motg->chg_work, delay);
1044}
1045
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301046/*
1047 * We support OTG, Peripheral only and Host only configurations. In case
1048 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1049 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1050 * enabled when switch is controlled by user and default mode is supplied
1051 * by board file, which can be changed by userspace later.
1052 */
1053static void msm_otg_init_sm(struct msm_otg *motg)
1054{
1055 struct msm_otg_platform_data *pdata = motg->pdata;
1056 u32 otgsc = readl(USB_OTGSC);
1057
1058 switch (pdata->mode) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001059 case USB_DR_MODE_OTG:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301060 if (pdata->otg_control == OTG_PHY_CONTROL) {
1061 if (otgsc & OTGSC_ID)
1062 set_bit(ID, &motg->inputs);
1063 else
1064 clear_bit(ID, &motg->inputs);
1065
1066 if (otgsc & OTGSC_BSV)
1067 set_bit(B_SESS_VLD, &motg->inputs);
1068 else
1069 clear_bit(B_SESS_VLD, &motg->inputs);
1070 } else if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301071 set_bit(ID, &motg->inputs);
1072 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301073 }
1074 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001075 case USB_DR_MODE_HOST:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301076 clear_bit(ID, &motg->inputs);
1077 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001078 case USB_DR_MODE_PERIPHERAL:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301079 set_bit(ID, &motg->inputs);
1080 if (otgsc & OTGSC_BSV)
1081 set_bit(B_SESS_VLD, &motg->inputs);
1082 else
1083 clear_bit(B_SESS_VLD, &motg->inputs);
1084 break;
1085 default:
1086 break;
1087 }
1088}
1089
1090static void msm_otg_sm_work(struct work_struct *w)
1091{
1092 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001093 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301094
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001095 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301096 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001097 dev_dbg(otg->phy->dev, "OTG_STATE_UNDEFINED state\n");
1098 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301099 msm_otg_init_sm(motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001100 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301101 /* FALL THROUGH */
1102 case OTG_STATE_B_IDLE:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001103 dev_dbg(otg->phy->dev, "OTG_STATE_B_IDLE state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301104 if (!test_bit(ID, &motg->inputs) && otg->host) {
1105 /* disable BSV bit */
1106 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001107 msm_otg_start_host(otg->phy, 1);
1108 otg->phy->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301109 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1110 switch (motg->chg_state) {
1111 case USB_CHG_STATE_UNDEFINED:
1112 msm_chg_detect_work(&motg->chg_work.work);
1113 break;
1114 case USB_CHG_STATE_DETECTED:
1115 switch (motg->chg_type) {
1116 case USB_DCP_CHARGER:
1117 msm_otg_notify_charger(motg,
1118 IDEV_CHG_MAX);
1119 break;
1120 case USB_CDP_CHARGER:
1121 msm_otg_notify_charger(motg,
1122 IDEV_CHG_MAX);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001123 msm_otg_start_peripheral(otg->phy, 1);
1124 otg->phy->state
1125 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301126 break;
1127 case USB_SDP_CHARGER:
1128 msm_otg_notify_charger(motg, IUNIT);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001129 msm_otg_start_peripheral(otg->phy, 1);
1130 otg->phy->state
1131 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301132 break;
1133 default:
1134 break;
1135 }
1136 break;
1137 default:
1138 break;
1139 }
1140 } else {
1141 /*
1142 * If charger detection work is pending, decrement
1143 * the pm usage counter to balance with the one that
1144 * is incremented in charger detection work.
1145 */
1146 if (cancel_delayed_work_sync(&motg->chg_work)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001147 pm_runtime_put_sync(otg->phy->dev);
1148 msm_otg_reset(otg->phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301149 }
1150 msm_otg_notify_charger(motg, 0);
1151 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1152 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301153 }
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001154 pm_runtime_put_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301155 break;
1156 case OTG_STATE_B_PERIPHERAL:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001157 dev_dbg(otg->phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301158 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1159 !test_bit(ID, &motg->inputs)) {
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301160 msm_otg_notify_charger(motg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001161 msm_otg_start_peripheral(otg->phy, 0);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301162 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1163 motg->chg_type = USB_INVALID_CHARGER;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001164 otg->phy->state = OTG_STATE_B_IDLE;
1165 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301166 schedule_work(w);
1167 }
1168 break;
1169 case OTG_STATE_A_HOST:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001170 dev_dbg(otg->phy->dev, "OTG_STATE_A_HOST state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301171 if (test_bit(ID, &motg->inputs)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001172 msm_otg_start_host(otg->phy, 0);
1173 otg->phy->state = OTG_STATE_B_IDLE;
1174 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301175 schedule_work(w);
1176 }
1177 break;
1178 default:
1179 break;
1180 }
1181}
1182
1183static irqreturn_t msm_otg_irq(int irq, void *data)
1184{
1185 struct msm_otg *motg = data;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001186 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301187 u32 otgsc = 0;
1188
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301189 if (atomic_read(&motg->in_lpm)) {
1190 disable_irq_nosync(irq);
1191 motg->async_int = 1;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001192 pm_runtime_get(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301193 return IRQ_HANDLED;
1194 }
1195
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301196 otgsc = readl(USB_OTGSC);
1197 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1198 return IRQ_NONE;
1199
1200 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1201 if (otgsc & OTGSC_ID)
1202 set_bit(ID, &motg->inputs);
1203 else
1204 clear_bit(ID, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001205 dev_dbg(phy->dev, "ID set/clear\n");
1206 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301207 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1208 if (otgsc & OTGSC_BSV)
1209 set_bit(B_SESS_VLD, &motg->inputs);
1210 else
1211 clear_bit(B_SESS_VLD, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001212 dev_dbg(phy->dev, "BSV set/clear\n");
1213 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301214 }
1215
1216 writel(otgsc, USB_OTGSC);
1217 schedule_work(&motg->sm_work);
1218 return IRQ_HANDLED;
1219}
1220
1221static int msm_otg_mode_show(struct seq_file *s, void *unused)
1222{
1223 struct msm_otg *motg = s->private;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001224 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301225
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001226 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301227 case OTG_STATE_A_HOST:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001228 seq_puts(s, "host\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301229 break;
1230 case OTG_STATE_B_PERIPHERAL:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001231 seq_puts(s, "peripheral\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301232 break;
1233 default:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001234 seq_puts(s, "none\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301235 break;
1236 }
1237
1238 return 0;
1239}
1240
1241static int msm_otg_mode_open(struct inode *inode, struct file *file)
1242{
1243 return single_open(file, msm_otg_mode_show, inode->i_private);
1244}
1245
1246static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1247 size_t count, loff_t *ppos)
1248{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301249 struct seq_file *s = file->private_data;
1250 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301251 char buf[16];
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001252 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301253 int status = count;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001254 enum usb_dr_mode req_mode;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301255
1256 memset(buf, 0x00, sizeof(buf));
1257
1258 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1259 status = -EFAULT;
1260 goto out;
1261 }
1262
1263 if (!strncmp(buf, "host", 4)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001264 req_mode = USB_DR_MODE_HOST;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301265 } else if (!strncmp(buf, "peripheral", 10)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001266 req_mode = USB_DR_MODE_PERIPHERAL;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301267 } else if (!strncmp(buf, "none", 4)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001268 req_mode = USB_DR_MODE_UNKNOWN;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301269 } else {
1270 status = -EINVAL;
1271 goto out;
1272 }
1273
1274 switch (req_mode) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001275 case USB_DR_MODE_UNKNOWN:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001276 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301277 case OTG_STATE_A_HOST:
1278 case OTG_STATE_B_PERIPHERAL:
1279 set_bit(ID, &motg->inputs);
1280 clear_bit(B_SESS_VLD, &motg->inputs);
1281 break;
1282 default:
1283 goto out;
1284 }
1285 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001286 case USB_DR_MODE_PERIPHERAL:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001287 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301288 case OTG_STATE_B_IDLE:
1289 case OTG_STATE_A_HOST:
1290 set_bit(ID, &motg->inputs);
1291 set_bit(B_SESS_VLD, &motg->inputs);
1292 break;
1293 default:
1294 goto out;
1295 }
1296 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001297 case USB_DR_MODE_HOST:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001298 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301299 case OTG_STATE_B_IDLE:
1300 case OTG_STATE_B_PERIPHERAL:
1301 clear_bit(ID, &motg->inputs);
1302 break;
1303 default:
1304 goto out;
1305 }
1306 break;
1307 default:
1308 goto out;
1309 }
1310
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001311 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301312 schedule_work(&motg->sm_work);
1313out:
1314 return status;
1315}
1316
1317const struct file_operations msm_otg_mode_fops = {
1318 .open = msm_otg_mode_open,
1319 .read = seq_read,
1320 .write = msm_otg_mode_write,
1321 .llseek = seq_lseek,
1322 .release = single_release,
1323};
1324
1325static struct dentry *msm_otg_dbg_root;
1326static struct dentry *msm_otg_dbg_mode;
1327
1328static int msm_otg_debugfs_init(struct msm_otg *motg)
1329{
1330 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1331
1332 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1333 return -ENODEV;
1334
1335 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1336 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1337 if (!msm_otg_dbg_mode) {
1338 debugfs_remove(msm_otg_dbg_root);
1339 msm_otg_dbg_root = NULL;
1340 return -ENODEV;
1341 }
1342
1343 return 0;
1344}
1345
1346static void msm_otg_debugfs_cleanup(void)
1347{
1348 debugfs_remove(msm_otg_dbg_mode);
1349 debugfs_remove(msm_otg_dbg_root);
1350}
1351
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001352static int msm_otg_probe(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301353{
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001354 struct regulator_bulk_data regs[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301355 int ret = 0;
1356 struct resource *res;
1357 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001358 struct usb_phy *phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301359
1360 dev_info(&pdev->dev, "msm_otg probe\n");
Jingoo Han19f9e182013-07-30 17:02:13 +09001361 if (!dev_get_platdata(&pdev->dev)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301362 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
1363 return -ENODEV;
1364 }
1365
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001366 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301367 if (!motg) {
1368 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
1369 return -ENOMEM;
1370 }
1371
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001372 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1373 GFP_KERNEL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001374 if (!motg->phy.otg) {
1375 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001376 return -ENOMEM;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001377 }
1378
Jingoo Han19f9e182013-07-30 17:02:13 +09001379 motg->pdata = dev_get_platdata(&pdev->dev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001380 phy = &motg->phy;
1381 phy->dev = &pdev->dev;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301382
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001383 motg->phy_reset_clk = devm_clk_get(&pdev->dev, "usb_phy_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301384 if (IS_ERR(motg->phy_reset_clk)) {
1385 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001386 return PTR_ERR(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301387 }
1388
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001389 motg->clk = devm_clk_get(&pdev->dev, "usb_hs_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301390 if (IS_ERR(motg->clk)) {
1391 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001392 return PTR_ERR(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301393 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05301394
1395 /*
1396 * If USB Core is running its protocol engine based on CORE CLK,
1397 * CORE CLK must be running at >55Mhz for correct HSUSB
1398 * operation and USB core cannot tolerate frequency changes on
1399 * CORE CLK. For such USB cores, vote for maximum clk frequency
1400 * on pclk source
1401 */
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001402 motg->pclk_src = ERR_PTR(-ENOENT);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301403 if (motg->pdata->pclk_src_name) {
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001404 motg->pclk_src = devm_clk_get(&pdev->dev,
1405 motg->pdata->pclk_src_name);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301406 if (IS_ERR(motg->pclk_src))
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001407 return PTR_ERR(motg->pclk_src);
1408 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05301409
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001410 motg->pclk = devm_clk_get(&pdev->dev, "usb_hs_pclk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301411 if (IS_ERR(motg->pclk)) {
1412 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001413 return PTR_ERR(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301414 }
1415
1416 /*
1417 * USB core clock is not present on all MSM chips. This
1418 * clock is introduced to remove the dependency on AXI
1419 * bus frequency.
1420 */
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001421 motg->core_clk = devm_clk_get(&pdev->dev, "usb_hs_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301422
1423 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001424 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1425 if (IS_ERR(motg->regs))
1426 return PTR_ERR(motg->regs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301427
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301428 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1429
1430 motg->irq = platform_get_irq(pdev, 0);
1431 if (!motg->irq) {
1432 dev_err(&pdev->dev, "platform_get_irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001433 return motg->irq;
1434 }
1435
1436 regs[0].supply = "HSUSB_VDDCX";
1437 regs[1].supply = "HSUSB_3p3";
1438 regs[2].supply = "HSUSB_1p8";
1439
1440 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1441 if (ret)
1442 return ret;
1443
1444 motg->vddcx = regs[0].consumer;
1445 motg->v3p3 = regs[1].consumer;
1446 motg->v1p8 = regs[2].consumer;
1447
1448 clk_set_rate(motg->clk, 60000000);
1449 if (!IS_ERR(motg->pclk_src)) {
1450 clk_set_rate(motg->pclk_src, INT_MAX);
1451 clk_prepare_enable(motg->pclk_src);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301452 }
1453
Stephen Boydb99a8f62013-06-17 10:43:10 -07001454 clk_prepare_enable(motg->clk);
1455 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301456
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001457 if (!IS_ERR(motg->core_clk))
1458 clk_prepare_enable(motg->core_clk);
1459
Anji jonnala11aa5c42011-05-04 10:19:48 +05301460 ret = msm_hsusb_init_vddcx(motg, 1);
1461 if (ret) {
1462 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001463 goto disable_clks;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301464 }
1465
1466 ret = msm_hsusb_ldo_init(motg, 1);
1467 if (ret) {
1468 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001469 goto disable_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301470 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +03001471 ret = msm_hsusb_ldo_set_mode(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301472 if (ret) {
1473 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001474 goto disable_ldo;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301475 }
1476
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301477 writel(0, USB_USBINTR);
1478 writel(0, USB_OTGSC);
1479
1480 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301481 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001482 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301483 "msm_otg", motg);
1484 if (ret) {
1485 dev_err(&pdev->dev, "request irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001486 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301487 }
1488
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001489 phy->init = msm_otg_reset;
1490 phy->set_power = msm_otg_set_power;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301491
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001492 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301493
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001494 phy->otg->phy = &motg->phy;
1495 phy->otg->set_host = msm_otg_set_host;
1496 phy->otg->set_peripheral = msm_otg_set_peripheral;
1497
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301498 ret = usb_add_phy(&motg->phy, USB_PHY_TYPE_USB2);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301499 if (ret) {
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301500 dev_err(&pdev->dev, "usb_add_phy failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001501 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301502 }
1503
1504 platform_set_drvdata(pdev, motg);
1505 device_init_wakeup(&pdev->dev, 1);
1506
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001507 if (motg->pdata->mode == USB_DR_MODE_OTG &&
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301508 motg->pdata->otg_control == OTG_USER_CONTROL) {
1509 ret = msm_otg_debugfs_init(motg);
1510 if (ret)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001511 dev_dbg(&pdev->dev, "Can not create mode change file\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301512 }
1513
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301514 pm_runtime_set_active(&pdev->dev);
1515 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301516
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301517 return 0;
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001518
1519disable_ldo:
1520 msm_hsusb_ldo_init(motg, 0);
1521disable_vddcx:
1522 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301523disable_clks:
Stephen Boydb99a8f62013-06-17 10:43:10 -07001524 clk_disable_unprepare(motg->pclk);
1525 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001526 if (!IS_ERR(motg->core_clk))
1527 clk_disable_unprepare(motg->core_clk);
1528 if (!IS_ERR(motg->pclk_src))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001529 clk_disable_unprepare(motg->pclk_src);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301530 return ret;
1531}
1532
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001533static int msm_otg_remove(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301534{
1535 struct msm_otg *motg = platform_get_drvdata(pdev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001536 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301537 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301538
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001539 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301540 return -EBUSY;
1541
1542 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301543 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301544 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301545
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301546 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301547
1548 device_init_wakeup(&pdev->dev, 0);
1549 pm_runtime_disable(&pdev->dev);
1550
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301551 usb_remove_phy(phy);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001552 disable_irq(motg->irq);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301553
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301554 /*
1555 * Put PHY in low power mode.
1556 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001557 ulpi_read(phy, 0x14);
1558 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301559
1560 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1561 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1562 if (readl(USB_PORTSC) & PORTSC_PHCD)
1563 break;
1564 udelay(1);
1565 cnt++;
1566 }
1567 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001568 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301569
Stephen Boydb99a8f62013-06-17 10:43:10 -07001570 clk_disable_unprepare(motg->pclk);
1571 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001572 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001573 clk_disable_unprepare(motg->core_clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001574 if (!IS_ERR(motg->pclk_src))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001575 clk_disable_unprepare(motg->pclk_src);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001576
Anji jonnala11aa5c42011-05-04 10:19:48 +05301577 msm_hsusb_ldo_init(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301578
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301579 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301580
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301581 return 0;
1582}
1583
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301584#ifdef CONFIG_PM_RUNTIME
1585static int msm_otg_runtime_idle(struct device *dev)
1586{
1587 struct msm_otg *motg = dev_get_drvdata(dev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001588 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301589
1590 dev_dbg(dev, "OTG runtime idle\n");
1591
1592 /*
1593 * It is observed some times that a spurious interrupt
1594 * comes when PHY is put into LPM immediately after PHY reset.
1595 * This 1 sec delay also prevents entering into LPM immediately
1596 * after asynchronous interrupt.
1597 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001598 if (otg->phy->state != OTG_STATE_UNDEFINED)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301599 pm_schedule_suspend(dev, 1000);
1600
1601 return -EAGAIN;
1602}
1603
1604static int msm_otg_runtime_suspend(struct device *dev)
1605{
1606 struct msm_otg *motg = dev_get_drvdata(dev);
1607
1608 dev_dbg(dev, "OTG runtime suspend\n");
1609 return msm_otg_suspend(motg);
1610}
1611
1612static int msm_otg_runtime_resume(struct device *dev)
1613{
1614 struct msm_otg *motg = dev_get_drvdata(dev);
1615
1616 dev_dbg(dev, "OTG runtime resume\n");
1617 return msm_otg_resume(motg);
1618}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301619#endif
1620
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301621#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301622static int msm_otg_pm_suspend(struct device *dev)
1623{
1624 struct msm_otg *motg = dev_get_drvdata(dev);
1625
1626 dev_dbg(dev, "OTG PM suspend\n");
1627 return msm_otg_suspend(motg);
1628}
1629
1630static int msm_otg_pm_resume(struct device *dev)
1631{
1632 struct msm_otg *motg = dev_get_drvdata(dev);
1633 int ret;
1634
1635 dev_dbg(dev, "OTG PM resume\n");
1636
1637 ret = msm_otg_resume(motg);
1638 if (ret)
1639 return ret;
1640
1641 /*
1642 * Runtime PM Documentation recommends bringing the
1643 * device to full powered state upon resume.
1644 */
1645 pm_runtime_disable(dev);
1646 pm_runtime_set_active(dev);
1647 pm_runtime_enable(dev);
1648
1649 return 0;
1650}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301651#endif
1652
1653static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301654 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
1655 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
1656 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301657};
1658
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301659static struct platform_driver msm_otg_driver = {
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001660 .probe = msm_otg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001661 .remove = msm_otg_remove,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301662 .driver = {
1663 .name = DRIVER_NAME,
1664 .owner = THIS_MODULE,
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301665 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301666 },
1667};
1668
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001669module_platform_driver(msm_otg_driver);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301670
1671MODULE_LICENSE("GPL v2");
1672MODULE_DESCRIPTION("MSM USB transceiver driver");