blob: 874c51a856835490825d16b13c23ecb8ca8ae8e7 [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);
351 if (pdata->mode == USB_OTG) {
352 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
353 val |= OTGSC_IDIE | OTGSC_BSVIE;
354 } else if (pdata->mode == USB_PERIPHERAL) {
355 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 jonnala0f73cac82011-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 jonnala0f73cac82011-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 jonnala0f73cac82011-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 jonnala0f73cac82011-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 */
640 if (motg->pdata->mode == USB_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 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530669 if (motg->pdata->mode == USB_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 */
713 if (motg->pdata->mode == USB_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 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530738 if (motg->pdata->mode == USB_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) {
1059 case USB_OTG:
1060 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) {
1071 if (pdata->default_mode == USB_HOST) {
1072 clear_bit(ID, &motg->inputs);
1073 } else if (pdata->default_mode == USB_PERIPHERAL) {
1074 set_bit(ID, &motg->inputs);
1075 set_bit(B_SESS_VLD, &motg->inputs);
1076 } else {
1077 set_bit(ID, &motg->inputs);
1078 clear_bit(B_SESS_VLD, &motg->inputs);
1079 }
1080 }
1081 break;
1082 case USB_HOST:
1083 clear_bit(ID, &motg->inputs);
1084 break;
1085 case USB_PERIPHERAL:
1086 set_bit(ID, &motg->inputs);
1087 if (otgsc & OTGSC_BSV)
1088 set_bit(B_SESS_VLD, &motg->inputs);
1089 else
1090 clear_bit(B_SESS_VLD, &motg->inputs);
1091 break;
1092 default:
1093 break;
1094 }
1095}
1096
1097static void msm_otg_sm_work(struct work_struct *w)
1098{
1099 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001100 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301101
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001102 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301103 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001104 dev_dbg(otg->phy->dev, "OTG_STATE_UNDEFINED state\n");
1105 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301106 msm_otg_init_sm(motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001107 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301108 /* FALL THROUGH */
1109 case OTG_STATE_B_IDLE:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001110 dev_dbg(otg->phy->dev, "OTG_STATE_B_IDLE state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301111 if (!test_bit(ID, &motg->inputs) && otg->host) {
1112 /* disable BSV bit */
1113 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001114 msm_otg_start_host(otg->phy, 1);
1115 otg->phy->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301116 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1117 switch (motg->chg_state) {
1118 case USB_CHG_STATE_UNDEFINED:
1119 msm_chg_detect_work(&motg->chg_work.work);
1120 break;
1121 case USB_CHG_STATE_DETECTED:
1122 switch (motg->chg_type) {
1123 case USB_DCP_CHARGER:
1124 msm_otg_notify_charger(motg,
1125 IDEV_CHG_MAX);
1126 break;
1127 case USB_CDP_CHARGER:
1128 msm_otg_notify_charger(motg,
1129 IDEV_CHG_MAX);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001130 msm_otg_start_peripheral(otg->phy, 1);
1131 otg->phy->state
1132 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301133 break;
1134 case USB_SDP_CHARGER:
1135 msm_otg_notify_charger(motg, IUNIT);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001136 msm_otg_start_peripheral(otg->phy, 1);
1137 otg->phy->state
1138 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301139 break;
1140 default:
1141 break;
1142 }
1143 break;
1144 default:
1145 break;
1146 }
1147 } else {
1148 /*
1149 * If charger detection work is pending, decrement
1150 * the pm usage counter to balance with the one that
1151 * is incremented in charger detection work.
1152 */
1153 if (cancel_delayed_work_sync(&motg->chg_work)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001154 pm_runtime_put_sync(otg->phy->dev);
1155 msm_otg_reset(otg->phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301156 }
1157 msm_otg_notify_charger(motg, 0);
1158 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1159 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301160 }
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001161 pm_runtime_put_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301162 break;
1163 case OTG_STATE_B_PERIPHERAL:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001164 dev_dbg(otg->phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301165 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1166 !test_bit(ID, &motg->inputs)) {
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301167 msm_otg_notify_charger(motg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001168 msm_otg_start_peripheral(otg->phy, 0);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301169 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1170 motg->chg_type = USB_INVALID_CHARGER;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001171 otg->phy->state = OTG_STATE_B_IDLE;
1172 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301173 schedule_work(w);
1174 }
1175 break;
1176 case OTG_STATE_A_HOST:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001177 dev_dbg(otg->phy->dev, "OTG_STATE_A_HOST state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301178 if (test_bit(ID, &motg->inputs)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001179 msm_otg_start_host(otg->phy, 0);
1180 otg->phy->state = OTG_STATE_B_IDLE;
1181 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301182 schedule_work(w);
1183 }
1184 break;
1185 default:
1186 break;
1187 }
1188}
1189
1190static irqreturn_t msm_otg_irq(int irq, void *data)
1191{
1192 struct msm_otg *motg = data;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001193 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301194 u32 otgsc = 0;
1195
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301196 if (atomic_read(&motg->in_lpm)) {
1197 disable_irq_nosync(irq);
1198 motg->async_int = 1;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001199 pm_runtime_get(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301200 return IRQ_HANDLED;
1201 }
1202
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301203 otgsc = readl(USB_OTGSC);
1204 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1205 return IRQ_NONE;
1206
1207 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1208 if (otgsc & OTGSC_ID)
1209 set_bit(ID, &motg->inputs);
1210 else
1211 clear_bit(ID, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001212 dev_dbg(phy->dev, "ID set/clear\n");
1213 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301214 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1215 if (otgsc & OTGSC_BSV)
1216 set_bit(B_SESS_VLD, &motg->inputs);
1217 else
1218 clear_bit(B_SESS_VLD, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001219 dev_dbg(phy->dev, "BSV set/clear\n");
1220 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301221 }
1222
1223 writel(otgsc, USB_OTGSC);
1224 schedule_work(&motg->sm_work);
1225 return IRQ_HANDLED;
1226}
1227
1228static int msm_otg_mode_show(struct seq_file *s, void *unused)
1229{
1230 struct msm_otg *motg = s->private;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001231 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301232
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001233 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301234 case OTG_STATE_A_HOST:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001235 seq_puts(s, "host\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301236 break;
1237 case OTG_STATE_B_PERIPHERAL:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001238 seq_puts(s, "peripheral\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301239 break;
1240 default:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001241 seq_puts(s, "none\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301242 break;
1243 }
1244
1245 return 0;
1246}
1247
1248static int msm_otg_mode_open(struct inode *inode, struct file *file)
1249{
1250 return single_open(file, msm_otg_mode_show, inode->i_private);
1251}
1252
1253static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1254 size_t count, loff_t *ppos)
1255{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301256 struct seq_file *s = file->private_data;
1257 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301258 char buf[16];
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001259 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301260 int status = count;
1261 enum usb_mode_type req_mode;
1262
1263 memset(buf, 0x00, sizeof(buf));
1264
1265 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1266 status = -EFAULT;
1267 goto out;
1268 }
1269
1270 if (!strncmp(buf, "host", 4)) {
1271 req_mode = USB_HOST;
1272 } else if (!strncmp(buf, "peripheral", 10)) {
1273 req_mode = USB_PERIPHERAL;
1274 } else if (!strncmp(buf, "none", 4)) {
1275 req_mode = USB_NONE;
1276 } else {
1277 status = -EINVAL;
1278 goto out;
1279 }
1280
1281 switch (req_mode) {
1282 case USB_NONE:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001283 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301284 case OTG_STATE_A_HOST:
1285 case OTG_STATE_B_PERIPHERAL:
1286 set_bit(ID, &motg->inputs);
1287 clear_bit(B_SESS_VLD, &motg->inputs);
1288 break;
1289 default:
1290 goto out;
1291 }
1292 break;
1293 case USB_PERIPHERAL:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001294 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301295 case OTG_STATE_B_IDLE:
1296 case OTG_STATE_A_HOST:
1297 set_bit(ID, &motg->inputs);
1298 set_bit(B_SESS_VLD, &motg->inputs);
1299 break;
1300 default:
1301 goto out;
1302 }
1303 break;
1304 case USB_HOST:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001305 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301306 case OTG_STATE_B_IDLE:
1307 case OTG_STATE_B_PERIPHERAL:
1308 clear_bit(ID, &motg->inputs);
1309 break;
1310 default:
1311 goto out;
1312 }
1313 break;
1314 default:
1315 goto out;
1316 }
1317
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001318 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301319 schedule_work(&motg->sm_work);
1320out:
1321 return status;
1322}
1323
1324const struct file_operations msm_otg_mode_fops = {
1325 .open = msm_otg_mode_open,
1326 .read = seq_read,
1327 .write = msm_otg_mode_write,
1328 .llseek = seq_lseek,
1329 .release = single_release,
1330};
1331
1332static struct dentry *msm_otg_dbg_root;
1333static struct dentry *msm_otg_dbg_mode;
1334
1335static int msm_otg_debugfs_init(struct msm_otg *motg)
1336{
1337 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1338
1339 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1340 return -ENODEV;
1341
1342 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1343 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1344 if (!msm_otg_dbg_mode) {
1345 debugfs_remove(msm_otg_dbg_root);
1346 msm_otg_dbg_root = NULL;
1347 return -ENODEV;
1348 }
1349
1350 return 0;
1351}
1352
1353static void msm_otg_debugfs_cleanup(void)
1354{
1355 debugfs_remove(msm_otg_dbg_mode);
1356 debugfs_remove(msm_otg_dbg_root);
1357}
1358
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001359static int msm_otg_probe(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301360{
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001361 struct regulator_bulk_data regs[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301362 int ret = 0;
1363 struct resource *res;
1364 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001365 struct usb_phy *phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301366
1367 dev_info(&pdev->dev, "msm_otg probe\n");
Jingoo Han19f9e182013-07-30 17:02:13 +09001368 if (!dev_get_platdata(&pdev->dev)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301369 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
1370 return -ENODEV;
1371 }
1372
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001373 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374 if (!motg) {
1375 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
1376 return -ENOMEM;
1377 }
1378
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001379 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1380 GFP_KERNEL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001381 if (!motg->phy.otg) {
1382 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001383 return -ENOMEM;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001384 }
1385
Jingoo Han19f9e182013-07-30 17:02:13 +09001386 motg->pdata = dev_get_platdata(&pdev->dev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001387 phy = &motg->phy;
1388 phy->dev = &pdev->dev;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301389
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001390 motg->phy_reset_clk = devm_clk_get(&pdev->dev, "usb_phy_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301391 if (IS_ERR(motg->phy_reset_clk)) {
1392 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001393 return PTR_ERR(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301394 }
1395
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001396 motg->clk = devm_clk_get(&pdev->dev, "usb_hs_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301397 if (IS_ERR(motg->clk)) {
1398 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001399 return PTR_ERR(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301400 }
Anji jonnala0f73cac82011-05-04 10:19:46 +05301401
1402 /*
1403 * If USB Core is running its protocol engine based on CORE CLK,
1404 * CORE CLK must be running at >55Mhz for correct HSUSB
1405 * operation and USB core cannot tolerate frequency changes on
1406 * CORE CLK. For such USB cores, vote for maximum clk frequency
1407 * on pclk source
1408 */
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001409 motg->pclk_src = ERR_PTR(-ENOENT);
Anji jonnala0f73cac82011-05-04 10:19:46 +05301410 if (motg->pdata->pclk_src_name) {
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001411 motg->pclk_src = devm_clk_get(&pdev->dev,
1412 motg->pdata->pclk_src_name);
Anji jonnala0f73cac82011-05-04 10:19:46 +05301413 if (IS_ERR(motg->pclk_src))
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001414 return PTR_ERR(motg->pclk_src);
1415 }
Anji jonnala0f73cac82011-05-04 10:19:46 +05301416
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001417 motg->pclk = devm_clk_get(&pdev->dev, "usb_hs_pclk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301418 if (IS_ERR(motg->pclk)) {
1419 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001420 return PTR_ERR(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301421 }
1422
1423 /*
1424 * USB core clock is not present on all MSM chips. This
1425 * clock is introduced to remove the dependency on AXI
1426 * bus frequency.
1427 */
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001428 motg->core_clk = devm_clk_get(&pdev->dev, "usb_hs_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301429
1430 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001431 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1432 if (IS_ERR(motg->regs))
1433 return PTR_ERR(motg->regs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301434
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301435 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1436
1437 motg->irq = platform_get_irq(pdev, 0);
1438 if (!motg->irq) {
1439 dev_err(&pdev->dev, "platform_get_irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001440 return motg->irq;
1441 }
1442
1443 regs[0].supply = "HSUSB_VDDCX";
1444 regs[1].supply = "HSUSB_3p3";
1445 regs[2].supply = "HSUSB_1p8";
1446
1447 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1448 if (ret)
1449 return ret;
1450
1451 motg->vddcx = regs[0].consumer;
1452 motg->v3p3 = regs[1].consumer;
1453 motg->v1p8 = regs[2].consumer;
1454
1455 clk_set_rate(motg->clk, 60000000);
1456 if (!IS_ERR(motg->pclk_src)) {
1457 clk_set_rate(motg->pclk_src, INT_MAX);
1458 clk_prepare_enable(motg->pclk_src);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301459 }
1460
Stephen Boydb99a8f62013-06-17 10:43:10 -07001461 clk_prepare_enable(motg->clk);
1462 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301463
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001464 if (!IS_ERR(motg->core_clk))
1465 clk_prepare_enable(motg->core_clk);
1466
Anji jonnala11aa5c42011-05-04 10:19:48 +05301467 ret = msm_hsusb_init_vddcx(motg, 1);
1468 if (ret) {
1469 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001470 goto disable_clks;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301471 }
1472
1473 ret = msm_hsusb_ldo_init(motg, 1);
1474 if (ret) {
1475 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001476 goto disable_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301477 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +03001478 ret = msm_hsusb_ldo_set_mode(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301479 if (ret) {
1480 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001481 goto disable_ldo;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301482 }
1483
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301484 writel(0, USB_USBINTR);
1485 writel(0, USB_OTGSC);
1486
1487 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301488 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001489 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301490 "msm_otg", motg);
1491 if (ret) {
1492 dev_err(&pdev->dev, "request irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001493 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301494 }
1495
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001496 phy->init = msm_otg_reset;
1497 phy->set_power = msm_otg_set_power;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301498
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001499 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301500
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001501 phy->otg->phy = &motg->phy;
1502 phy->otg->set_host = msm_otg_set_host;
1503 phy->otg->set_peripheral = msm_otg_set_peripheral;
1504
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301505 ret = usb_add_phy(&motg->phy, USB_PHY_TYPE_USB2);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301506 if (ret) {
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301507 dev_err(&pdev->dev, "usb_add_phy failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001508 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301509 }
1510
1511 platform_set_drvdata(pdev, motg);
1512 device_init_wakeup(&pdev->dev, 1);
1513
1514 if (motg->pdata->mode == USB_OTG &&
1515 motg->pdata->otg_control == OTG_USER_CONTROL) {
1516 ret = msm_otg_debugfs_init(motg);
1517 if (ret)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001518 dev_dbg(&pdev->dev, "Can not create mode change file\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301519 }
1520
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301521 pm_runtime_set_active(&pdev->dev);
1522 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301523
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301524 return 0;
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001525
1526disable_ldo:
1527 msm_hsusb_ldo_init(motg, 0);
1528disable_vddcx:
1529 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301530disable_clks:
Stephen Boydb99a8f62013-06-17 10:43:10 -07001531 clk_disable_unprepare(motg->pclk);
1532 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001533 if (!IS_ERR(motg->core_clk))
1534 clk_disable_unprepare(motg->core_clk);
1535 if (!IS_ERR(motg->pclk_src))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001536 clk_disable_unprepare(motg->pclk_src);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301537 return ret;
1538}
1539
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001540static int msm_otg_remove(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301541{
1542 struct msm_otg *motg = platform_get_drvdata(pdev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001543 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301544 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301545
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001546 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301547 return -EBUSY;
1548
1549 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301550 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301551 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301552
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301553 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301554
1555 device_init_wakeup(&pdev->dev, 0);
1556 pm_runtime_disable(&pdev->dev);
1557
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301558 usb_remove_phy(phy);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001559 disable_irq(motg->irq);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301560
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301561 /*
1562 * Put PHY in low power mode.
1563 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001564 ulpi_read(phy, 0x14);
1565 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301566
1567 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1568 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1569 if (readl(USB_PORTSC) & PORTSC_PHCD)
1570 break;
1571 udelay(1);
1572 cnt++;
1573 }
1574 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001575 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301576
Stephen Boydb99a8f62013-06-17 10:43:10 -07001577 clk_disable_unprepare(motg->pclk);
1578 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001579 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001580 clk_disable_unprepare(motg->core_clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001581 if (!IS_ERR(motg->pclk_src))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001582 clk_disable_unprepare(motg->pclk_src);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001583
Anji jonnala11aa5c42011-05-04 10:19:48 +05301584 msm_hsusb_ldo_init(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301585
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301586 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301587
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301588 return 0;
1589}
1590
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301591#ifdef CONFIG_PM_RUNTIME
1592static int msm_otg_runtime_idle(struct device *dev)
1593{
1594 struct msm_otg *motg = dev_get_drvdata(dev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001595 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301596
1597 dev_dbg(dev, "OTG runtime idle\n");
1598
1599 /*
1600 * It is observed some times that a spurious interrupt
1601 * comes when PHY is put into LPM immediately after PHY reset.
1602 * This 1 sec delay also prevents entering into LPM immediately
1603 * after asynchronous interrupt.
1604 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001605 if (otg->phy->state != OTG_STATE_UNDEFINED)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301606 pm_schedule_suspend(dev, 1000);
1607
1608 return -EAGAIN;
1609}
1610
1611static int msm_otg_runtime_suspend(struct device *dev)
1612{
1613 struct msm_otg *motg = dev_get_drvdata(dev);
1614
1615 dev_dbg(dev, "OTG runtime suspend\n");
1616 return msm_otg_suspend(motg);
1617}
1618
1619static int msm_otg_runtime_resume(struct device *dev)
1620{
1621 struct msm_otg *motg = dev_get_drvdata(dev);
1622
1623 dev_dbg(dev, "OTG runtime resume\n");
1624 return msm_otg_resume(motg);
1625}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301626#endif
1627
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301628#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301629static int msm_otg_pm_suspend(struct device *dev)
1630{
1631 struct msm_otg *motg = dev_get_drvdata(dev);
1632
1633 dev_dbg(dev, "OTG PM suspend\n");
1634 return msm_otg_suspend(motg);
1635}
1636
1637static int msm_otg_pm_resume(struct device *dev)
1638{
1639 struct msm_otg *motg = dev_get_drvdata(dev);
1640 int ret;
1641
1642 dev_dbg(dev, "OTG PM resume\n");
1643
1644 ret = msm_otg_resume(motg);
1645 if (ret)
1646 return ret;
1647
1648 /*
1649 * Runtime PM Documentation recommends bringing the
1650 * device to full powered state upon resume.
1651 */
1652 pm_runtime_disable(dev);
1653 pm_runtime_set_active(dev);
1654 pm_runtime_enable(dev);
1655
1656 return 0;
1657}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301658#endif
1659
1660static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301661 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
1662 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
1663 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301664};
1665
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301666static struct platform_driver msm_otg_driver = {
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001667 .probe = msm_otg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001668 .remove = msm_otg_remove,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301669 .driver = {
1670 .name = DRIVER_NAME,
1671 .owner = THIS_MODULE,
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301672 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301673 },
1674};
1675
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001676module_platform_driver(msm_otg_driver);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301677
1678MODULE_LICENSE("GPL v2");
1679MODULE_DESCRIPTION("MSM USB transceiver driver");