blob: 8d57045ac93840b1a1a9f871d13cdd4c174e676b [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>
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +030033#include <linux/of.h>
34#include <linux/of_device.h>
Ivan T. Ivanova2734542014-04-28 16:34:16 +030035#include <linux/reset.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053036
37#include <linux/usb.h>
38#include <linux/usb/otg.h>
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +030039#include <linux/usb/of.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053040#include <linux/usb/ulpi.h>
41#include <linux/usb/gadget.h>
42#include <linux/usb/hcd.h>
43#include <linux/usb/msm_hsusb.h>
44#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053045#include <linux/regulator/consumer.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053046
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053047#define MSM_USB_BASE (motg->regs)
48#define DRIVER_NAME "msm_otg"
49
50#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053051
52#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
53#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
54#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
55#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
56
57#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
58#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
59#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
60#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
61
62#define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
63#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
64
Anji jonnala11aa5c42011-05-04 10:19:48 +053065static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
66{
67 int ret = 0;
68
69 if (init) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030070 ret = regulator_set_voltage(motg->vddcx,
Anji jonnala11aa5c42011-05-04 10:19:48 +053071 USB_PHY_VDD_DIG_VOL_MIN,
72 USB_PHY_VDD_DIG_VOL_MAX);
73 if (ret) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +030074 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053075 return ret;
76 }
77
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030078 ret = regulator_enable(motg->vddcx);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +030079 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +020080 dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053081 } else {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030082 ret = regulator_set_voltage(motg->vddcx, 0,
Mark Brown7b521fc2011-05-15 09:55:57 -070083 USB_PHY_VDD_DIG_VOL_MAX);
Mark Browne99c4302011-05-15 09:55:58 -070084 if (ret)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +030085 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030086 ret = regulator_disable(motg->vddcx);
Anji jonnala11aa5c42011-05-04 10:19:48 +053087 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +020088 dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053089 }
90
91 return ret;
92}
93
94static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
95{
96 int rc = 0;
97
98 if (init) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030099 rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530100 USB_PHY_3P3_VOL_MAX);
101 if (rc) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300102 dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300103 goto exit;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530104 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300105 rc = regulator_enable(motg->v3p3);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530106 if (rc) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200107 dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300108 goto exit;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530109 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300110 rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530111 USB_PHY_1P8_VOL_MAX);
112 if (rc) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300113 dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300114 goto disable_3p3;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530115 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300116 rc = regulator_enable(motg->v1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530117 if (rc) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200118 dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300119 goto disable_3p3;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530120 }
121
122 return 0;
123 }
124
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300125 regulator_disable(motg->v1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126disable_3p3:
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300127 regulator_disable(motg->v3p3);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300128exit:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530129 return rc;
130}
131
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300132static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133{
134 int ret = 0;
135
Anji jonnala11aa5c42011-05-04 10:19:48 +0530136 if (on) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300137 ret = regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138 USB_PHY_1P8_HPM_LOAD);
139 if (ret < 0) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300140 pr_err("Could not set HPM for v1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530141 return ret;
142 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300143 ret = regulator_set_optimum_mode(motg->v3p3,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144 USB_PHY_3P3_HPM_LOAD);
145 if (ret < 0) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300146 pr_err("Could not set HPM for v3p3\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300147 regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 USB_PHY_1P8_LPM_LOAD);
149 return ret;
150 }
151 } else {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300152 ret = regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530153 USB_PHY_1P8_LPM_LOAD);
154 if (ret < 0)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300155 pr_err("Could not set LPM for v1p8\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300156 ret = regulator_set_optimum_mode(motg->v3p3,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530157 USB_PHY_3P3_LPM_LOAD);
158 if (ret < 0)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300159 pr_err("Could not set LPM for v3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530160 }
161
162 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
163 return ret < 0 ? ret : 0;
164}
165
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200166static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530167{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200168 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530169 int cnt = 0;
170
171 /* initiate read operation */
172 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
173 USB_ULPI_VIEWPORT);
174
175 /* wait for completion */
176 while (cnt < ULPI_IO_TIMEOUT_USEC) {
177 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
178 break;
179 udelay(1);
180 cnt++;
181 }
182
183 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200184 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530185 readl(USB_ULPI_VIEWPORT));
186 return -ETIMEDOUT;
187 }
188 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
189}
190
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200191static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530192{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200193 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530194 int cnt = 0;
195
196 /* initiate write operation */
197 writel(ULPI_RUN | ULPI_WRITE |
198 ULPI_ADDR(reg) | ULPI_DATA(val),
199 USB_ULPI_VIEWPORT);
200
201 /* wait for completion */
202 while (cnt < ULPI_IO_TIMEOUT_USEC) {
203 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
204 break;
205 udelay(1);
206 cnt++;
207 }
208
209 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200210 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530211 return -ETIMEDOUT;
212 }
213 return 0;
214}
215
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200216static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530217 .read = ulpi_read,
218 .write = ulpi_write,
219};
220
221static void ulpi_init(struct msm_otg *motg)
222{
223 struct msm_otg_platform_data *pdata = motg->pdata;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300224 int *seq = pdata->phy_init_seq, idx;
225 u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530226
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300227 for (idx = 0; idx < pdata->phy_init_sz; idx++) {
228 if (seq[idx] == -1)
229 continue;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530230
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200231 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300232 seq[idx], addr + idx);
233 ulpi_write(&motg->phy, seq[idx], addr + idx);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530234 }
235}
236
237static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
238{
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300239 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530240
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300241 if (motg->pdata->link_clk_reset)
242 ret = motg->pdata->link_clk_reset(motg->clk, assert);
243 else if (assert)
244 ret = reset_control_assert(motg->link_rst);
245 else
246 ret = reset_control_deassert(motg->link_rst);
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800247
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800248 if (ret)
249 dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
250 assert ? "assert" : "deassert");
251
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530252 return ret;
253}
254
255static int msm_otg_phy_clk_reset(struct msm_otg *motg)
256{
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300257 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530258
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300259 if (motg->pdata->phy_clk_reset)
260 ret = motg->pdata->phy_clk_reset(motg->phy_reset_clk);
261 else
262 ret = reset_control_reset(motg->phy_rst);
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800263
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530264 if (ret)
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800265 dev_err(motg->phy.dev, "usb phy clk reset failed\n");
266
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530267 return ret;
268}
269
270static int msm_otg_phy_reset(struct msm_otg *motg)
271{
272 u32 val;
273 int ret;
274 int retries;
275
276 ret = msm_otg_link_clk_reset(motg, 1);
277 if (ret)
278 return ret;
279 ret = msm_otg_phy_clk_reset(motg);
280 if (ret)
281 return ret;
282 ret = msm_otg_link_clk_reset(motg, 0);
283 if (ret)
284 return ret;
285
286 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
287 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
288
289 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200290 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530291 ULPI_CLR(ULPI_FUNC_CTRL));
292 if (!ret)
293 break;
294 ret = msm_otg_phy_clk_reset(motg);
295 if (ret)
296 return ret;
297 }
298 if (!retries)
299 return -ETIMEDOUT;
300
301 /* This reset calibrates the phy, if the above write succeeded */
302 ret = msm_otg_phy_clk_reset(motg);
303 if (ret)
304 return ret;
305
306 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200307 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530308 if (ret != -ETIMEDOUT)
309 break;
310 ret = msm_otg_phy_clk_reset(motg);
311 if (ret)
312 return ret;
313 }
314 if (!retries)
315 return -ETIMEDOUT;
316
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300317 if (motg->phy_number)
318 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
319
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200320 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530321 return 0;
322}
323
324#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200325static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530326{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200327 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530328 struct msm_otg_platform_data *pdata = motg->pdata;
329 int cnt = 0;
330 int ret;
331 u32 val = 0;
332 u32 ulpi_val = 0;
333
334 ret = msm_otg_phy_reset(motg);
335 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200336 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530337 return ret;
338 }
339
340 ulpi_init(motg);
341
342 writel(USBCMD_RESET, USB_USBCMD);
343 while (cnt < LINK_RESET_TIMEOUT_USEC) {
344 if (!(readl(USB_USBCMD) & USBCMD_RESET))
345 break;
346 udelay(1);
347 cnt++;
348 }
349 if (cnt >= LINK_RESET_TIMEOUT_USEC)
350 return -ETIMEDOUT;
351
352 /* select ULPI phy */
353 writel(0x80000000, USB_PORTSC);
354
355 msleep(100);
356
357 writel(0x0, USB_AHBBURST);
358 writel(0x00, USB_AHBMODE);
359
360 if (pdata->otg_control == OTG_PHY_CONTROL) {
361 val = readl(USB_OTGSC);
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300362 if (pdata->mode == USB_DR_MODE_OTG) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530363 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
364 val |= OTGSC_IDIE | OTGSC_BSVIE;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300365 } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530366 ulpi_val = ULPI_INT_SESS_VALID;
367 val |= OTGSC_BSVIE;
368 }
369 writel(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200370 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
371 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530372 }
373
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300374 if (motg->phy_number)
375 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
376
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530377 return 0;
378}
379
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530380#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530381#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
382
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600383#ifdef CONFIG_PM
384
385#define USB_PHY_SUSP_DIG_VOL 500000
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300386static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600387{
388 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
389 int min_vol;
390 int ret;
391
392 if (high)
393 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
394 else
395 min_vol = USB_PHY_SUSP_DIG_VOL;
396
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300397 ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600398 if (ret) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300399 pr_err("Cannot set vddcx voltage\n");
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600400 return ret;
401 }
402
403 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
404
405 return ret;
406}
407
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530408static int msm_otg_suspend(struct msm_otg *motg)
409{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200410 struct usb_phy *phy = &motg->phy;
411 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530412 struct msm_otg_platform_data *pdata = motg->pdata;
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300413 void __iomem *addr;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530414 int cnt = 0;
415
416 if (atomic_read(&motg->in_lpm))
417 return 0;
418
419 disable_irq(motg->irq);
420 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530421 * Chipidea 45-nm PHY suspend sequence:
422 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530423 * Interrupt Latch Register auto-clear feature is not present
424 * in all PHY versions. Latch register is clear on read type.
425 * Clear latch register to avoid spurious wakeup from
426 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530427 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530428 * PHY comparators are disabled when PHY enters into low power
429 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
430 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
431 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530432 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530433 * PLL is not turned off when PHY enters into low power mode (LPM).
434 * Disable PLL for maximum power savings.
435 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530436
437 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200438 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530439 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200440 ulpi_write(phy, 0x01, 0x30);
441 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530442 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530443
444 /*
445 * PHY may take some time or even fail to enter into low power
446 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
447 * in failure case.
448 */
449 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
450 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
451 if (readl(USB_PORTSC) & PORTSC_PHCD)
452 break;
453 udelay(1);
454 cnt++;
455 }
456
457 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200458 dev_err(phy->dev, "Unable to suspend PHY\n");
459 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530460 enable_irq(motg->irq);
461 return -ETIMEDOUT;
462 }
463
464 /*
465 * PHY has capability to generate interrupt asynchronously in low
466 * power mode (LPM). This interrupt is level triggered. So USB IRQ
467 * line must be disabled till async interrupt enable bit is cleared
468 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
469 * block data communication from PHY.
470 */
471 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
472
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300473 addr = USB_PHY_CTRL;
474 if (motg->phy_number)
475 addr = USB_PHY_CTRL2;
476
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530477 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
478 motg->pdata->otg_control == OTG_PMIC_CONTROL)
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300479 writel(readl(addr) | PHY_RETEN, addr);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530480
Stephen Boydb99a8f62013-06-17 10:43:10 -0700481 clk_disable_unprepare(motg->pclk);
482 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300483 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700484 clk_disable_unprepare(motg->core_clk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530485
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530486 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
487 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300488 msm_hsusb_ldo_set_mode(motg, 0);
489 msm_hsusb_config_vddcx(motg, 0);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530490 }
491
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200492 if (device_may_wakeup(phy->dev))
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530493 enable_irq_wake(motg->irq);
494 if (bus)
495 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
496
497 atomic_set(&motg->in_lpm, 1);
498 enable_irq(motg->irq);
499
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200500 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530501
502 return 0;
503}
504
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530505static int msm_otg_resume(struct msm_otg *motg)
506{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200507 struct usb_phy *phy = &motg->phy;
508 struct usb_bus *bus = phy->otg->host;
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300509 void __iomem *addr;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530510 int cnt = 0;
511 unsigned temp;
512
513 if (!atomic_read(&motg->in_lpm))
514 return 0;
515
Stephen Boydb99a8f62013-06-17 10:43:10 -0700516 clk_prepare_enable(motg->pclk);
517 clk_prepare_enable(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300518 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700519 clk_prepare_enable(motg->core_clk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530520
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530521 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
522 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300523
524 addr = USB_PHY_CTRL;
525 if (motg->phy_number)
526 addr = USB_PHY_CTRL2;
527
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300528 msm_hsusb_ldo_set_mode(motg, 1);
529 msm_hsusb_config_vddcx(motg, 1);
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300530 writel(readl(addr) & ~PHY_RETEN, addr);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530531 }
532
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530533 temp = readl(USB_USBCMD);
534 temp &= ~ASYNC_INTR_CTRL;
535 temp &= ~ULPI_STP_CTRL;
536 writel(temp, USB_USBCMD);
537
538 /*
539 * PHY comes out of low power mode (LPM) in case of wakeup
540 * from asynchronous interrupt.
541 */
542 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
543 goto skip_phy_resume;
544
545 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
546 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
547 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
548 break;
549 udelay(1);
550 cnt++;
551 }
552
553 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
554 /*
555 * This is a fatal error. Reset the link and
556 * PHY. USB state can not be restored. Re-insertion
557 * of USB cable is the only way to get USB working.
558 */
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300559 dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200560 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530561 }
562
563skip_phy_resume:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200564 if (device_may_wakeup(phy->dev))
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530565 disable_irq_wake(motg->irq);
566 if (bus)
567 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
568
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530569 atomic_set(&motg->in_lpm, 0);
570
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530571 if (motg->async_int) {
572 motg->async_int = 0;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200573 pm_runtime_put(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530574 enable_irq(motg->irq);
575 }
576
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200577 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530578
579 return 0;
580}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530581#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530582
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530583static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
584{
585 if (motg->cur_power == mA)
586 return;
587
588 /* TODO: Notify PMIC about available current */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200589 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530590 motg->cur_power = mA;
591}
592
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200593static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530594{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200595 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530596
597 /*
598 * Gadget driver uses set_power method to notify about the
599 * available current based on suspend/configured states.
600 *
601 * IDEV_CHG can be drawn irrespective of suspend/un-configured
602 * states when CDP/ACA is connected.
603 */
604 if (motg->chg_type == USB_SDP_CHARGER)
605 msm_otg_notify_charger(motg, mA);
606
607 return 0;
608}
609
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200610static void msm_otg_start_host(struct usb_phy *phy, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530611{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200612 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530613 struct msm_otg_platform_data *pdata = motg->pdata;
614 struct usb_hcd *hcd;
615
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200616 if (!phy->otg->host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530617 return;
618
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200619 hcd = bus_to_hcd(phy->otg->host);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530620
621 if (on) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200622 dev_dbg(phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530623
624 if (pdata->vbus_power)
625 pdata->vbus_power(1);
626 /*
627 * Some boards have a switch cotrolled by gpio
628 * to enable/disable internal HUB. Enable internal
629 * HUB before kicking the host.
630 */
631 if (pdata->setup_gpio)
632 pdata->setup_gpio(OTG_STATE_A_HOST);
633#ifdef CONFIG_USB
634 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Peter Chen3c9740a2013-11-05 10:46:02 +0800635 device_wakeup_enable(hcd->self.controller);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530636#endif
637 } else {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200638 dev_dbg(phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530639
640#ifdef CONFIG_USB
641 usb_remove_hcd(hcd);
642#endif
643 if (pdata->setup_gpio)
644 pdata->setup_gpio(OTG_STATE_UNDEFINED);
645 if (pdata->vbus_power)
646 pdata->vbus_power(0);
647 }
648}
649
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200650static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530651{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200652 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530653 struct usb_hcd *hcd;
654
655 /*
656 * Fail host registration if this board can support
657 * only peripheral configuration.
658 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300659 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200660 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530661 return -ENODEV;
662 }
663
664 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200665 if (otg->phy->state == OTG_STATE_A_HOST) {
666 pm_runtime_get_sync(otg->phy->dev);
667 msm_otg_start_host(otg->phy, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530668 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200669 otg->phy->state = OTG_STATE_UNDEFINED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530670 schedule_work(&motg->sm_work);
671 } else {
672 otg->host = NULL;
673 }
674
675 return 0;
676 }
677
678 hcd = bus_to_hcd(host);
679 hcd->power_budget = motg->pdata->power_budget;
680
681 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200682 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530683
684 /*
685 * Kick the state machine work, if peripheral is not supported
686 * or peripheral is already registered with us.
687 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300688 if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200689 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530690 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530691 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530692
693 return 0;
694}
695
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200696static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530697{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200698 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530699 struct msm_otg_platform_data *pdata = motg->pdata;
700
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200701 if (!phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530702 return;
703
704 if (on) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200705 dev_dbg(phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530706 /*
707 * Some boards have a switch cotrolled by gpio
708 * to enable/disable internal HUB. Disable internal
709 * HUB before kicking the gadget.
710 */
711 if (pdata->setup_gpio)
712 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200713 usb_gadget_vbus_connect(phy->otg->gadget);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530714 } else {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200715 dev_dbg(phy->dev, "gadget off\n");
716 usb_gadget_vbus_disconnect(phy->otg->gadget);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530717 if (pdata->setup_gpio)
718 pdata->setup_gpio(OTG_STATE_UNDEFINED);
719 }
720
721}
722
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200723static int msm_otg_set_peripheral(struct usb_otg *otg,
724 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530725{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200726 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530727
728 /*
729 * Fail peripheral registration if this board can support
730 * only host configuration.
731 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300732 if (motg->pdata->mode == USB_DR_MODE_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200733 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530734 return -ENODEV;
735 }
736
737 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200738 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
739 pm_runtime_get_sync(otg->phy->dev);
740 msm_otg_start_peripheral(otg->phy, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530741 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200742 otg->phy->state = OTG_STATE_UNDEFINED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530743 schedule_work(&motg->sm_work);
744 } else {
745 otg->gadget = NULL;
746 }
747
748 return 0;
749 }
750 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200751 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530752
753 /*
754 * Kick the state machine work, if host is not supported
755 * or host is already registered with us.
756 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300757 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200758 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530759 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530760 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530761
762 return 0;
763}
764
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530765static bool msm_chg_check_secondary_det(struct msm_otg *motg)
766{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200767 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530768 u32 chg_det;
769 bool ret = false;
770
771 switch (motg->pdata->phy_type) {
772 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200773 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530774 ret = chg_det & (1 << 4);
775 break;
776 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200777 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530778 ret = chg_det & 1;
779 break;
780 default:
781 break;
782 }
783 return ret;
784}
785
786static void msm_chg_enable_secondary_det(struct msm_otg *motg)
787{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200788 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530789 u32 chg_det;
790
791 switch (motg->pdata->phy_type) {
792 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200793 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530794 /* Turn off charger block */
795 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200796 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530797 udelay(20);
798 /* control chg block via ULPI */
799 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200800 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530801 /* put it in host mode for enabling D- source */
802 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200803 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530804 /* Turn on chg detect block */
805 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200806 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530807 udelay(20);
808 /* enable chg detection */
809 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200810 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530811 break;
812 case SNPS_28NM_INTEGRATED_PHY:
813 /*
814 * Configure DM as current source, DP as current sink
815 * and enable battery charging comparators.
816 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200817 ulpi_write(phy, 0x8, 0x85);
818 ulpi_write(phy, 0x2, 0x85);
819 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530820 break;
821 default:
822 break;
823 }
824}
825
826static bool msm_chg_check_primary_det(struct msm_otg *motg)
827{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200828 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530829 u32 chg_det;
830 bool ret = false;
831
832 switch (motg->pdata->phy_type) {
833 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200834 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530835 ret = chg_det & (1 << 4);
836 break;
837 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200838 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530839 ret = chg_det & 1;
840 break;
841 default:
842 break;
843 }
844 return ret;
845}
846
847static void msm_chg_enable_primary_det(struct msm_otg *motg)
848{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200849 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530850 u32 chg_det;
851
852 switch (motg->pdata->phy_type) {
853 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200854 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530855 /* enable chg detection */
856 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200857 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530858 break;
859 case SNPS_28NM_INTEGRATED_PHY:
860 /*
861 * Configure DP as current source, DM as current sink
862 * and enable battery charging comparators.
863 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200864 ulpi_write(phy, 0x2, 0x85);
865 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530866 break;
867 default:
868 break;
869 }
870}
871
872static bool msm_chg_check_dcd(struct msm_otg *motg)
873{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200874 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530875 u32 line_state;
876 bool ret = false;
877
878 switch (motg->pdata->phy_type) {
879 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200880 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530881 ret = !(line_state & 1);
882 break;
883 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200884 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530885 ret = line_state & 2;
886 break;
887 default:
888 break;
889 }
890 return ret;
891}
892
893static void msm_chg_disable_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 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200902 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530903 break;
904 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200905 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530906 break;
907 default:
908 break;
909 }
910}
911
912static void msm_chg_enable_dcd(struct msm_otg *motg)
913{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200914 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530915 u32 chg_det;
916
917 switch (motg->pdata->phy_type) {
918 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200919 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530920 /* Turn on D+ current source */
921 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200922 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530923 break;
924 case SNPS_28NM_INTEGRATED_PHY:
925 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200926 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530927 break;
928 default:
929 break;
930 }
931}
932
933static void msm_chg_block_on(struct msm_otg *motg)
934{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200935 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530936 u32 func_ctrl, chg_det;
937
938 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200939 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530940 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
941 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200942 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530943
944 switch (motg->pdata->phy_type) {
945 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200946 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530947 /* control chg block via ULPI */
948 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200949 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530950 /* Turn on chg detect block */
951 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200952 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530953 udelay(20);
954 break;
955 case SNPS_28NM_INTEGRATED_PHY:
956 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200957 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530958 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200959 ulpi_write(phy, 0x1F, 0x92);
960 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530961 udelay(100);
962 break;
963 default:
964 break;
965 }
966}
967
968static void msm_chg_block_off(struct msm_otg *motg)
969{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200970 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530971 u32 func_ctrl, chg_det;
972
973 switch (motg->pdata->phy_type) {
974 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200975 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530976 /* Turn off charger block */
977 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200978 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530979 break;
980 case SNPS_28NM_INTEGRATED_PHY:
981 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200982 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530983 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200984 ulpi_write(phy, 0x1F, 0x92);
985 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530986 break;
987 default:
988 break;
989 }
990
991 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200992 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530993 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
994 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200995 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530996}
997
998#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
999#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1000#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1001#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1002static void msm_chg_detect_work(struct work_struct *w)
1003{
1004 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001005 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301006 bool is_dcd, tmout, vout;
1007 unsigned long delay;
1008
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001009 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301010 switch (motg->chg_state) {
1011 case USB_CHG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001012 pm_runtime_get_sync(phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301013 msm_chg_block_on(motg);
1014 msm_chg_enable_dcd(motg);
1015 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1016 motg->dcd_retries = 0;
1017 delay = MSM_CHG_DCD_POLL_TIME;
1018 break;
1019 case USB_CHG_STATE_WAIT_FOR_DCD:
1020 is_dcd = msm_chg_check_dcd(motg);
1021 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1022 if (is_dcd || tmout) {
1023 msm_chg_disable_dcd(motg);
1024 msm_chg_enable_primary_det(motg);
1025 delay = MSM_CHG_PRIMARY_DET_TIME;
1026 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1027 } else {
1028 delay = MSM_CHG_DCD_POLL_TIME;
1029 }
1030 break;
1031 case USB_CHG_STATE_DCD_DONE:
1032 vout = msm_chg_check_primary_det(motg);
1033 if (vout) {
1034 msm_chg_enable_secondary_det(motg);
1035 delay = MSM_CHG_SECONDARY_DET_TIME;
1036 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1037 } else {
1038 motg->chg_type = USB_SDP_CHARGER;
1039 motg->chg_state = USB_CHG_STATE_DETECTED;
1040 delay = 0;
1041 }
1042 break;
1043 case USB_CHG_STATE_PRIMARY_DONE:
1044 vout = msm_chg_check_secondary_det(motg);
1045 if (vout)
1046 motg->chg_type = USB_DCP_CHARGER;
1047 else
1048 motg->chg_type = USB_CDP_CHARGER;
1049 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1050 /* fall through */
1051 case USB_CHG_STATE_SECONDARY_DONE:
1052 motg->chg_state = USB_CHG_STATE_DETECTED;
1053 case USB_CHG_STATE_DETECTED:
1054 msm_chg_block_off(motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001055 dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301056 schedule_work(&motg->sm_work);
1057 return;
1058 default:
1059 return;
1060 }
1061
1062 schedule_delayed_work(&motg->chg_work, delay);
1063}
1064
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301065/*
1066 * We support OTG, Peripheral only and Host only configurations. In case
1067 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1068 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1069 * enabled when switch is controlled by user and default mode is supplied
1070 * by board file, which can be changed by userspace later.
1071 */
1072static void msm_otg_init_sm(struct msm_otg *motg)
1073{
1074 struct msm_otg_platform_data *pdata = motg->pdata;
1075 u32 otgsc = readl(USB_OTGSC);
1076
1077 switch (pdata->mode) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001078 case USB_DR_MODE_OTG:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301079 if (pdata->otg_control == OTG_PHY_CONTROL) {
1080 if (otgsc & OTGSC_ID)
1081 set_bit(ID, &motg->inputs);
1082 else
1083 clear_bit(ID, &motg->inputs);
1084
1085 if (otgsc & OTGSC_BSV)
1086 set_bit(B_SESS_VLD, &motg->inputs);
1087 else
1088 clear_bit(B_SESS_VLD, &motg->inputs);
1089 } else if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301090 set_bit(ID, &motg->inputs);
1091 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301092 }
1093 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001094 case USB_DR_MODE_HOST:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301095 clear_bit(ID, &motg->inputs);
1096 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001097 case USB_DR_MODE_PERIPHERAL:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301098 set_bit(ID, &motg->inputs);
1099 if (otgsc & OTGSC_BSV)
1100 set_bit(B_SESS_VLD, &motg->inputs);
1101 else
1102 clear_bit(B_SESS_VLD, &motg->inputs);
1103 break;
1104 default:
1105 break;
1106 }
1107}
1108
1109static void msm_otg_sm_work(struct work_struct *w)
1110{
1111 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001112 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301113
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001114 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301115 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001116 dev_dbg(otg->phy->dev, "OTG_STATE_UNDEFINED state\n");
1117 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301118 msm_otg_init_sm(motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001119 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301120 /* FALL THROUGH */
1121 case OTG_STATE_B_IDLE:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001122 dev_dbg(otg->phy->dev, "OTG_STATE_B_IDLE state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301123 if (!test_bit(ID, &motg->inputs) && otg->host) {
1124 /* disable BSV bit */
1125 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001126 msm_otg_start_host(otg->phy, 1);
1127 otg->phy->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301128 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1129 switch (motg->chg_state) {
1130 case USB_CHG_STATE_UNDEFINED:
1131 msm_chg_detect_work(&motg->chg_work.work);
1132 break;
1133 case USB_CHG_STATE_DETECTED:
1134 switch (motg->chg_type) {
1135 case USB_DCP_CHARGER:
1136 msm_otg_notify_charger(motg,
1137 IDEV_CHG_MAX);
1138 break;
1139 case USB_CDP_CHARGER:
1140 msm_otg_notify_charger(motg,
1141 IDEV_CHG_MAX);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001142 msm_otg_start_peripheral(otg->phy, 1);
1143 otg->phy->state
1144 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301145 break;
1146 case USB_SDP_CHARGER:
1147 msm_otg_notify_charger(motg, IUNIT);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001148 msm_otg_start_peripheral(otg->phy, 1);
1149 otg->phy->state
1150 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301151 break;
1152 default:
1153 break;
1154 }
1155 break;
1156 default:
1157 break;
1158 }
1159 } else {
1160 /*
1161 * If charger detection work is pending, decrement
1162 * the pm usage counter to balance with the one that
1163 * is incremented in charger detection work.
1164 */
1165 if (cancel_delayed_work_sync(&motg->chg_work)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001166 pm_runtime_put_sync(otg->phy->dev);
1167 msm_otg_reset(otg->phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301168 }
1169 msm_otg_notify_charger(motg, 0);
1170 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1171 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301172 }
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001173 pm_runtime_put_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301174 break;
1175 case OTG_STATE_B_PERIPHERAL:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001176 dev_dbg(otg->phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301177 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1178 !test_bit(ID, &motg->inputs)) {
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301179 msm_otg_notify_charger(motg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001180 msm_otg_start_peripheral(otg->phy, 0);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301181 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1182 motg->chg_type = USB_INVALID_CHARGER;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001183 otg->phy->state = OTG_STATE_B_IDLE;
1184 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301185 schedule_work(w);
1186 }
1187 break;
1188 case OTG_STATE_A_HOST:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001189 dev_dbg(otg->phy->dev, "OTG_STATE_A_HOST state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301190 if (test_bit(ID, &motg->inputs)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001191 msm_otg_start_host(otg->phy, 0);
1192 otg->phy->state = OTG_STATE_B_IDLE;
1193 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301194 schedule_work(w);
1195 }
1196 break;
1197 default:
1198 break;
1199 }
1200}
1201
1202static irqreturn_t msm_otg_irq(int irq, void *data)
1203{
1204 struct msm_otg *motg = data;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001205 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301206 u32 otgsc = 0;
1207
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301208 if (atomic_read(&motg->in_lpm)) {
1209 disable_irq_nosync(irq);
1210 motg->async_int = 1;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001211 pm_runtime_get(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301212 return IRQ_HANDLED;
1213 }
1214
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301215 otgsc = readl(USB_OTGSC);
1216 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1217 return IRQ_NONE;
1218
1219 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1220 if (otgsc & OTGSC_ID)
1221 set_bit(ID, &motg->inputs);
1222 else
1223 clear_bit(ID, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001224 dev_dbg(phy->dev, "ID set/clear\n");
1225 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301226 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1227 if (otgsc & OTGSC_BSV)
1228 set_bit(B_SESS_VLD, &motg->inputs);
1229 else
1230 clear_bit(B_SESS_VLD, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001231 dev_dbg(phy->dev, "BSV set/clear\n");
1232 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301233 }
1234
1235 writel(otgsc, USB_OTGSC);
1236 schedule_work(&motg->sm_work);
1237 return IRQ_HANDLED;
1238}
1239
1240static int msm_otg_mode_show(struct seq_file *s, void *unused)
1241{
1242 struct msm_otg *motg = s->private;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001243 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301244
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001245 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301246 case OTG_STATE_A_HOST:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001247 seq_puts(s, "host\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301248 break;
1249 case OTG_STATE_B_PERIPHERAL:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001250 seq_puts(s, "peripheral\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301251 break;
1252 default:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001253 seq_puts(s, "none\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301254 break;
1255 }
1256
1257 return 0;
1258}
1259
1260static int msm_otg_mode_open(struct inode *inode, struct file *file)
1261{
1262 return single_open(file, msm_otg_mode_show, inode->i_private);
1263}
1264
1265static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1266 size_t count, loff_t *ppos)
1267{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301268 struct seq_file *s = file->private_data;
1269 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301270 char buf[16];
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001271 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301272 int status = count;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001273 enum usb_dr_mode req_mode;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301274
1275 memset(buf, 0x00, sizeof(buf));
1276
1277 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1278 status = -EFAULT;
1279 goto out;
1280 }
1281
1282 if (!strncmp(buf, "host", 4)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001283 req_mode = USB_DR_MODE_HOST;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301284 } else if (!strncmp(buf, "peripheral", 10)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001285 req_mode = USB_DR_MODE_PERIPHERAL;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301286 } else if (!strncmp(buf, "none", 4)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001287 req_mode = USB_DR_MODE_UNKNOWN;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301288 } else {
1289 status = -EINVAL;
1290 goto out;
1291 }
1292
1293 switch (req_mode) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001294 case USB_DR_MODE_UNKNOWN:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001295 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301296 case OTG_STATE_A_HOST:
1297 case OTG_STATE_B_PERIPHERAL:
1298 set_bit(ID, &motg->inputs);
1299 clear_bit(B_SESS_VLD, &motg->inputs);
1300 break;
1301 default:
1302 goto out;
1303 }
1304 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001305 case USB_DR_MODE_PERIPHERAL:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001306 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301307 case OTG_STATE_B_IDLE:
1308 case OTG_STATE_A_HOST:
1309 set_bit(ID, &motg->inputs);
1310 set_bit(B_SESS_VLD, &motg->inputs);
1311 break;
1312 default:
1313 goto out;
1314 }
1315 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001316 case USB_DR_MODE_HOST:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001317 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301318 case OTG_STATE_B_IDLE:
1319 case OTG_STATE_B_PERIPHERAL:
1320 clear_bit(ID, &motg->inputs);
1321 break;
1322 default:
1323 goto out;
1324 }
1325 break;
1326 default:
1327 goto out;
1328 }
1329
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001330 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301331 schedule_work(&motg->sm_work);
1332out:
1333 return status;
1334}
1335
1336const struct file_operations msm_otg_mode_fops = {
1337 .open = msm_otg_mode_open,
1338 .read = seq_read,
1339 .write = msm_otg_mode_write,
1340 .llseek = seq_lseek,
1341 .release = single_release,
1342};
1343
1344static struct dentry *msm_otg_dbg_root;
1345static struct dentry *msm_otg_dbg_mode;
1346
1347static int msm_otg_debugfs_init(struct msm_otg *motg)
1348{
1349 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1350
1351 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1352 return -ENODEV;
1353
1354 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1355 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1356 if (!msm_otg_dbg_mode) {
1357 debugfs_remove(msm_otg_dbg_root);
1358 msm_otg_dbg_root = NULL;
1359 return -ENODEV;
1360 }
1361
1362 return 0;
1363}
1364
1365static void msm_otg_debugfs_cleanup(void)
1366{
1367 debugfs_remove(msm_otg_dbg_mode);
1368 debugfs_remove(msm_otg_dbg_root);
1369}
1370
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001371static struct of_device_id msm_otg_dt_match[] = {
1372 {
1373 .compatible = "qcom,usb-otg-ci",
1374 .data = (void *) CI_45NM_INTEGRATED_PHY
1375 },
1376 {
1377 .compatible = "qcom,usb-otg-snps",
1378 .data = (void *) SNPS_28NM_INTEGRATED_PHY
1379 },
1380 { }
1381};
1382MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
1383
1384static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
1385{
1386 struct msm_otg_platform_data *pdata;
1387 const struct of_device_id *id;
1388 struct device_node *node = pdev->dev.of_node;
1389 struct property *prop;
1390 int len, ret, words;
1391 u32 val;
1392
1393 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1394 if (!pdata)
1395 return -ENOMEM;
1396
1397 motg->pdata = pdata;
1398
1399 id = of_match_device(msm_otg_dt_match, &pdev->dev);
1400 pdata->phy_type = (int) id->data;
1401
Ivan T. Ivanova2734542014-04-28 16:34:16 +03001402 motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
1403 if (IS_ERR(motg->link_rst))
1404 return PTR_ERR(motg->link_rst);
1405
1406 motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
1407 if (IS_ERR(motg->phy_rst))
1408 return PTR_ERR(motg->phy_rst);
1409
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001410 pdata->mode = of_usb_get_dr_mode(node);
1411 if (pdata->mode == USB_DR_MODE_UNKNOWN)
1412 pdata->mode = USB_DR_MODE_OTG;
1413
1414 pdata->otg_control = OTG_PHY_CONTROL;
1415 if (!of_property_read_u32(node, "qcom,otg-control", &val))
1416 if (val == OTG_PMIC_CONTROL)
1417 pdata->otg_control = val;
1418
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +03001419 if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
1420 motg->phy_number = val;
1421
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001422 prop = of_find_property(node, "qcom,phy-init-sequence", &len);
1423 if (!prop || !len)
1424 return 0;
1425
1426 words = len / sizeof(u32);
1427
1428 if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
1429 dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
1430 return 0;
1431 }
1432
1433 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
1434 if (!pdata->phy_init_seq) {
1435 dev_warn(&pdev->dev, "No space for PHY init sequence\n");
1436 return 0;
1437 }
1438
1439 ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
1440 pdata->phy_init_seq, words);
1441 if (!ret)
1442 pdata->phy_init_sz = words;
1443
1444 return 0;
1445}
1446
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001447static int msm_otg_probe(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301448{
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001449 struct regulator_bulk_data regs[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301450 int ret = 0;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001451 struct device_node *np = pdev->dev.of_node;
1452 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301453 struct resource *res;
1454 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001455 struct usb_phy *phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301456
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001457 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301458 if (!motg) {
1459 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
1460 return -ENOMEM;
1461 }
1462
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001463 pdata = dev_get_platdata(&pdev->dev);
1464 if (!pdata) {
1465 if (!np)
1466 return -ENXIO;
1467 ret = msm_otg_read_dt(pdev, motg);
1468 if (ret)
1469 return ret;
1470 }
1471
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001472 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1473 GFP_KERNEL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001474 if (!motg->phy.otg) {
1475 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001476 return -ENOMEM;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001477 }
1478
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001479 phy = &motg->phy;
1480 phy->dev = &pdev->dev;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301481
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001482 motg->phy_reset_clk = devm_clk_get(&pdev->dev,
1483 np ? "phy" : "usb_phy_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301484 if (IS_ERR(motg->phy_reset_clk)) {
1485 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001486 return PTR_ERR(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301487 }
1488
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001489 motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301490 if (IS_ERR(motg->clk)) {
1491 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001492 return PTR_ERR(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301493 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05301494
1495 /*
1496 * If USB Core is running its protocol engine based on CORE CLK,
1497 * CORE CLK must be running at >55Mhz for correct HSUSB
1498 * operation and USB core cannot tolerate frequency changes on
Ivan T. Ivanovff0e4a62014-04-28 16:34:12 +03001499 * CORE CLK.
Anji jonnala0f73cac2011-05-04 10:19:46 +05301500 */
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001501 motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301502 if (IS_ERR(motg->pclk)) {
1503 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001504 return PTR_ERR(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301505 }
1506
1507 /*
1508 * USB core clock is not present on all MSM chips. This
1509 * clock is introduced to remove the dependency on AXI
1510 * bus frequency.
1511 */
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001512 motg->core_clk = devm_clk_get(&pdev->dev,
1513 np ? "alt_core" : "usb_hs_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301514
1515 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001516 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1517 if (IS_ERR(motg->regs))
1518 return PTR_ERR(motg->regs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301519
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301520 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1521
1522 motg->irq = platform_get_irq(pdev, 0);
Ivan T. Ivanovf60c1142014-04-28 16:34:14 +03001523 if (motg->irq < 0) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301524 dev_err(&pdev->dev, "platform_get_irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001525 return motg->irq;
1526 }
1527
Ivan T. Ivanovf5ef2372014-04-28 16:34:13 +03001528 regs[0].supply = "vddcx";
1529 regs[1].supply = "v3p3";
1530 regs[2].supply = "v1p8";
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001531
1532 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1533 if (ret)
1534 return ret;
1535
1536 motg->vddcx = regs[0].consumer;
1537 motg->v3p3 = regs[1].consumer;
1538 motg->v1p8 = regs[2].consumer;
1539
1540 clk_set_rate(motg->clk, 60000000);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301541
Stephen Boydb99a8f62013-06-17 10:43:10 -07001542 clk_prepare_enable(motg->clk);
1543 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301544
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001545 if (!IS_ERR(motg->core_clk))
1546 clk_prepare_enable(motg->core_clk);
1547
Anji jonnala11aa5c42011-05-04 10:19:48 +05301548 ret = msm_hsusb_init_vddcx(motg, 1);
1549 if (ret) {
1550 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001551 goto disable_clks;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301552 }
1553
1554 ret = msm_hsusb_ldo_init(motg, 1);
1555 if (ret) {
1556 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001557 goto disable_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301558 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +03001559 ret = msm_hsusb_ldo_set_mode(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301560 if (ret) {
1561 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001562 goto disable_ldo;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301563 }
1564
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301565 writel(0, USB_USBINTR);
1566 writel(0, USB_OTGSC);
1567
1568 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301569 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001570 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301571 "msm_otg", motg);
1572 if (ret) {
1573 dev_err(&pdev->dev, "request irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001574 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301575 }
1576
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001577 phy->init = msm_otg_reset;
1578 phy->set_power = msm_otg_set_power;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301579
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001580 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301581
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001582 phy->otg->phy = &motg->phy;
1583 phy->otg->set_host = msm_otg_set_host;
1584 phy->otg->set_peripheral = msm_otg_set_peripheral;
1585
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301586 ret = usb_add_phy(&motg->phy, USB_PHY_TYPE_USB2);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301587 if (ret) {
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301588 dev_err(&pdev->dev, "usb_add_phy failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001589 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301590 }
1591
1592 platform_set_drvdata(pdev, motg);
1593 device_init_wakeup(&pdev->dev, 1);
1594
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001595 if (motg->pdata->mode == USB_DR_MODE_OTG &&
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001596 motg->pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301597 ret = msm_otg_debugfs_init(motg);
1598 if (ret)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001599 dev_dbg(&pdev->dev, "Can not create mode change file\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301600 }
1601
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301602 pm_runtime_set_active(&pdev->dev);
1603 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301604
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301605 return 0;
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001606
1607disable_ldo:
1608 msm_hsusb_ldo_init(motg, 0);
1609disable_vddcx:
1610 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301611disable_clks:
Stephen Boydb99a8f62013-06-17 10:43:10 -07001612 clk_disable_unprepare(motg->pclk);
1613 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001614 if (!IS_ERR(motg->core_clk))
1615 clk_disable_unprepare(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301616 return ret;
1617}
1618
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001619static int msm_otg_remove(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301620{
1621 struct msm_otg *motg = platform_get_drvdata(pdev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001622 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301623 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301624
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001625 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301626 return -EBUSY;
1627
1628 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301629 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301630 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301631
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301632 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301633
1634 device_init_wakeup(&pdev->dev, 0);
1635 pm_runtime_disable(&pdev->dev);
1636
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301637 usb_remove_phy(phy);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001638 disable_irq(motg->irq);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301639
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301640 /*
1641 * Put PHY in low power mode.
1642 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001643 ulpi_read(phy, 0x14);
1644 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301645
1646 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1647 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1648 if (readl(USB_PORTSC) & PORTSC_PHCD)
1649 break;
1650 udelay(1);
1651 cnt++;
1652 }
1653 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001654 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301655
Stephen Boydb99a8f62013-06-17 10:43:10 -07001656 clk_disable_unprepare(motg->pclk);
1657 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001658 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001659 clk_disable_unprepare(motg->core_clk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301660 msm_hsusb_ldo_init(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301661
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301662 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301663
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301664 return 0;
1665}
1666
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301667#ifdef CONFIG_PM_RUNTIME
1668static int msm_otg_runtime_idle(struct device *dev)
1669{
1670 struct msm_otg *motg = dev_get_drvdata(dev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001671 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301672
1673 dev_dbg(dev, "OTG runtime idle\n");
1674
1675 /*
1676 * It is observed some times that a spurious interrupt
1677 * comes when PHY is put into LPM immediately after PHY reset.
1678 * This 1 sec delay also prevents entering into LPM immediately
1679 * after asynchronous interrupt.
1680 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001681 if (otg->phy->state != OTG_STATE_UNDEFINED)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301682 pm_schedule_suspend(dev, 1000);
1683
1684 return -EAGAIN;
1685}
1686
1687static int msm_otg_runtime_suspend(struct device *dev)
1688{
1689 struct msm_otg *motg = dev_get_drvdata(dev);
1690
1691 dev_dbg(dev, "OTG runtime suspend\n");
1692 return msm_otg_suspend(motg);
1693}
1694
1695static int msm_otg_runtime_resume(struct device *dev)
1696{
1697 struct msm_otg *motg = dev_get_drvdata(dev);
1698
1699 dev_dbg(dev, "OTG runtime resume\n");
1700 return msm_otg_resume(motg);
1701}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301702#endif
1703
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301704#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301705static int msm_otg_pm_suspend(struct device *dev)
1706{
1707 struct msm_otg *motg = dev_get_drvdata(dev);
1708
1709 dev_dbg(dev, "OTG PM suspend\n");
1710 return msm_otg_suspend(motg);
1711}
1712
1713static int msm_otg_pm_resume(struct device *dev)
1714{
1715 struct msm_otg *motg = dev_get_drvdata(dev);
1716 int ret;
1717
1718 dev_dbg(dev, "OTG PM resume\n");
1719
1720 ret = msm_otg_resume(motg);
1721 if (ret)
1722 return ret;
1723
1724 /*
1725 * Runtime PM Documentation recommends bringing the
1726 * device to full powered state upon resume.
1727 */
1728 pm_runtime_disable(dev);
1729 pm_runtime_set_active(dev);
1730 pm_runtime_enable(dev);
1731
1732 return 0;
1733}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301734#endif
1735
1736static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301737 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
1738 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
1739 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301740};
1741
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301742static struct platform_driver msm_otg_driver = {
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001743 .probe = msm_otg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001744 .remove = msm_otg_remove,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301745 .driver = {
1746 .name = DRIVER_NAME,
1747 .owner = THIS_MODULE,
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301748 .pm = &msm_otg_dev_pm_ops,
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001749 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301750 },
1751};
1752
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001753module_platform_driver(msm_otg_driver);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301754
1755MODULE_LICENSE("GPL v2");
1756MODULE_DESCRIPTION("MSM USB transceiver driver");