blob: e120d87778b24c69cf42fd7d667eb3b5bc4a900d [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)
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +030051#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053052
53#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
54#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
55#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
56#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
57
58#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
59#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
60#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
61#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
62
63#define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
64#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
Ivan T. Ivanov01799b62014-04-28 16:34:22 +030065#define USB_PHY_SUSP_DIG_VOL 500000 /* uV */
66
67enum vdd_levels {
68 VDD_LEVEL_NONE = 0,
69 VDD_LEVEL_MIN,
70 VDD_LEVEL_MAX,
71};
Anji jonnala11aa5c42011-05-04 10:19:48 +053072
Anji jonnala11aa5c42011-05-04 10:19:48 +053073static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
74{
75 int ret = 0;
76
77 if (init) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030078 ret = regulator_set_voltage(motg->vddcx,
Ivan T. Ivanov01799b62014-04-28 16:34:22 +030079 motg->vdd_levels[VDD_LEVEL_MIN],
80 motg->vdd_levels[VDD_LEVEL_MAX]);
Anji jonnala11aa5c42011-05-04 10:19:48 +053081 if (ret) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +030082 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053083 return ret;
84 }
85
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030086 ret = regulator_enable(motg->vddcx);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +030087 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +020088 dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053089 } else {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030090 ret = regulator_set_voltage(motg->vddcx, 0,
Ivan T. Ivanov01799b62014-04-28 16:34:22 +030091 motg->vdd_levels[VDD_LEVEL_MAX]);
Mark Browne99c4302011-05-15 09:55:58 -070092 if (ret)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +030093 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +030094 ret = regulator_disable(motg->vddcx);
Anji jonnala11aa5c42011-05-04 10:19:48 +053095 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +020096 dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +053097 }
98
99 return ret;
100}
101
102static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
103{
104 int rc = 0;
105
106 if (init) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300107 rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530108 USB_PHY_3P3_VOL_MAX);
109 if (rc) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300110 dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300111 goto exit;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530112 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300113 rc = regulator_enable(motg->v3p3);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530114 if (rc) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200115 dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300116 goto exit;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530117 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300118 rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530119 USB_PHY_1P8_VOL_MAX);
120 if (rc) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300121 dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300122 goto disable_3p3;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530123 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300124 rc = regulator_enable(motg->v1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530125 if (rc) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200126 dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300127 goto disable_3p3;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 }
129
130 return 0;
131 }
132
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300133 regulator_disable(motg->v1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530134disable_3p3:
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300135 regulator_disable(motg->v3p3);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300136exit:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530137 return rc;
138}
139
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300140static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530141{
142 int ret = 0;
143
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144 if (on) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300145 ret = regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530146 USB_PHY_1P8_HPM_LOAD);
147 if (ret < 0) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300148 pr_err("Could not set HPM for v1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530149 return ret;
150 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300151 ret = regulator_set_optimum_mode(motg->v3p3,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530152 USB_PHY_3P3_HPM_LOAD);
153 if (ret < 0) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300154 pr_err("Could not set HPM for v3p3\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300155 regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530156 USB_PHY_1P8_LPM_LOAD);
157 return ret;
158 }
159 } else {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300160 ret = regulator_set_optimum_mode(motg->v1p8,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530161 USB_PHY_1P8_LPM_LOAD);
162 if (ret < 0)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300163 pr_err("Could not set LPM for v1p8\n");
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300164 ret = regulator_set_optimum_mode(motg->v3p3,
Anji jonnala11aa5c42011-05-04 10:19:48 +0530165 USB_PHY_3P3_LPM_LOAD);
166 if (ret < 0)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300167 pr_err("Could not set LPM for v3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530168 }
169
170 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
171 return ret < 0 ? ret : 0;
172}
173
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200174static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530175{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200176 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530177 int cnt = 0;
178
179 /* initiate read operation */
180 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
181 USB_ULPI_VIEWPORT);
182
183 /* wait for completion */
184 while (cnt < ULPI_IO_TIMEOUT_USEC) {
185 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
186 break;
187 udelay(1);
188 cnt++;
189 }
190
191 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200192 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530193 readl(USB_ULPI_VIEWPORT));
194 return -ETIMEDOUT;
195 }
196 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
197}
198
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200199static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530200{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200201 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530202 int cnt = 0;
203
204 /* initiate write operation */
205 writel(ULPI_RUN | ULPI_WRITE |
206 ULPI_ADDR(reg) | ULPI_DATA(val),
207 USB_ULPI_VIEWPORT);
208
209 /* wait for completion */
210 while (cnt < ULPI_IO_TIMEOUT_USEC) {
211 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
212 break;
213 udelay(1);
214 cnt++;
215 }
216
217 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200218 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530219 return -ETIMEDOUT;
220 }
221 return 0;
222}
223
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200224static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530225 .read = ulpi_read,
226 .write = ulpi_write,
227};
228
229static void ulpi_init(struct msm_otg *motg)
230{
231 struct msm_otg_platform_data *pdata = motg->pdata;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300232 int *seq = pdata->phy_init_seq, idx;
233 u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530234
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300235 for (idx = 0; idx < pdata->phy_init_sz; idx++) {
236 if (seq[idx] == -1)
237 continue;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530238
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200239 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300240 seq[idx], addr + idx);
241 ulpi_write(&motg->phy, seq[idx], addr + idx);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530242 }
243}
244
Ivan T. Ivanov349907c2014-04-28 16:34:21 +0300245static int msm_phy_notify_disconnect(struct usb_phy *phy,
246 enum usb_device_speed speed)
247{
248 int val;
249
250 /*
251 * Put the transceiver in non-driving mode. Otherwise host
252 * may not detect soft-disconnection.
253 */
254 val = ulpi_read(phy, ULPI_FUNC_CTRL);
255 val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
256 val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
257 ulpi_write(phy, val, ULPI_FUNC_CTRL);
258
259 return 0;
260}
261
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530262static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
263{
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300264 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530265
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300266 if (motg->pdata->link_clk_reset)
267 ret = motg->pdata->link_clk_reset(motg->clk, assert);
268 else if (assert)
269 ret = reset_control_assert(motg->link_rst);
270 else
271 ret = reset_control_deassert(motg->link_rst);
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800272
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800273 if (ret)
274 dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
275 assert ? "assert" : "deassert");
276
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530277 return ret;
278}
279
280static int msm_otg_phy_clk_reset(struct msm_otg *motg)
281{
Srinivas Kandagatlae44f1f42014-06-30 18:29:49 +0100282 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530283
Srinivas Kandagatlaa1a4caf2014-08-21 07:45:10 +0100284 if (motg->pdata->phy_clk_reset)
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300285 ret = motg->pdata->phy_clk_reset(motg->phy_reset_clk);
Srinivas Kandagatlae44f1f42014-06-30 18:29:49 +0100286 else if (motg->phy_rst)
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300287 ret = reset_control_reset(motg->phy_rst);
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800288
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530289 if (ret)
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800290 dev_err(motg->phy.dev, "usb phy clk reset failed\n");
291
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530292 return ret;
293}
294
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300295static int msm_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530296{
297 u32 val;
298 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530299
300 ret = msm_otg_link_clk_reset(motg, 1);
301 if (ret)
302 return ret;
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300303
304 /* wait for 1ms delay as suggested in HPG. */
305 usleep_range(1000, 1200);
306
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530307 ret = msm_otg_link_clk_reset(motg, 0);
308 if (ret)
309 return ret;
310
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300311 if (motg->phy_number)
312 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
313
Tim Bird9f27984b2014-04-28 16:34:19 +0300314 /* put transceiver in serial mode as part of reset */
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300315 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
Tim Bird9f27984b2014-04-28 16:34:19 +0300316 writel(val | PORTSC_PTS_SERIAL, USB_PORTSC);
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300317
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530318 return 0;
319}
320
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200321static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530322{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200323 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530324 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530325
326 writel(USBCMD_RESET, USB_USBCMD);
327 while (cnt < LINK_RESET_TIMEOUT_USEC) {
328 if (!(readl(USB_USBCMD) & USBCMD_RESET))
329 break;
330 udelay(1);
331 cnt++;
332 }
333 if (cnt >= LINK_RESET_TIMEOUT_USEC)
334 return -ETIMEDOUT;
335
Tim Bird9f27984b2014-04-28 16:34:19 +0300336 /* select ULPI phy and clear other status/control bits in PORTSC */
337 writel(PORTSC_PTS_ULPI, USB_PORTSC);
338
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300339 writel(0x0, USB_AHBBURST);
340 writel(0x08, USB_AHBMODE);
341
342 if (motg->phy_number)
343 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
344 return 0;
345}
346
347static void msm_phy_reset(struct msm_otg *motg)
348{
349 void __iomem *addr;
350
351 if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY) {
352 msm_otg_phy_clk_reset(motg);
353 return;
354 }
355
356 addr = USB_PHY_CTRL;
357 if (motg->phy_number)
358 addr = USB_PHY_CTRL2;
359
360 /* Assert USB PHY_POR */
361 writel(readl(addr) | PHY_POR_ASSERT, addr);
362
363 /*
364 * wait for minimum 10 microseconds as suggested in HPG.
365 * Use a slightly larger value since the exact value didn't
366 * work 100% of the time.
367 */
368 udelay(12);
369
370 /* Deassert USB PHY_POR */
371 writel(readl(addr) & ~PHY_POR_ASSERT, addr);
372}
373
374static int msm_usb_reset(struct usb_phy *phy)
375{
376 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
377 int ret;
378
379 if (!IS_ERR(motg->core_clk))
380 clk_prepare_enable(motg->core_clk);
381
382 ret = msm_link_reset(motg);
383 if (ret) {
384 dev_err(phy->dev, "phy_reset failed\n");
385 return ret;
386 }
387
388 ret = msm_otg_reset(&motg->phy);
389 if (ret) {
390 dev_err(phy->dev, "link reset failed\n");
391 return ret;
392 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530393
394 msleep(100);
395
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300396 /* Reset USB PHY after performing USB Link RESET */
397 msm_phy_reset(motg);
398
399 if (!IS_ERR(motg->core_clk))
400 clk_disable_unprepare(motg->core_clk);
401
402 return 0;
403}
404
405static int msm_phy_init(struct usb_phy *phy)
406{
407 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
408 struct msm_otg_platform_data *pdata = motg->pdata;
409 u32 val, ulpi_val = 0;
410
411 /* Program USB PHY Override registers. */
412 ulpi_init(motg);
413
414 /*
415 * It is recommended in HPG to reset USB PHY after programming
416 * USB PHY Override registers.
417 */
418 msm_phy_reset(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530419
420 if (pdata->otg_control == OTG_PHY_CONTROL) {
421 val = readl(USB_OTGSC);
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300422 if (pdata->mode == USB_DR_MODE_OTG) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530423 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
424 val |= OTGSC_IDIE | OTGSC_BSVIE;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300425 } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530426 ulpi_val = ULPI_INT_SESS_VALID;
427 val |= OTGSC_BSVIE;
428 }
429 writel(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200430 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
431 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 }
433
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300434 if (motg->phy_number)
435 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
436
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530437 return 0;
438}
439
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530440#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530441#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
442
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600443#ifdef CONFIG_PM
444
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300445static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600446{
Ivan T. Ivanov01799b62014-04-28 16:34:22 +0300447 int max_vol = motg->vdd_levels[VDD_LEVEL_MAX];
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600448 int min_vol;
449 int ret;
450
451 if (high)
Ivan T. Ivanov01799b62014-04-28 16:34:22 +0300452 min_vol = motg->vdd_levels[VDD_LEVEL_MIN];
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600453 else
Ivan T. Ivanov01799b62014-04-28 16:34:22 +0300454 min_vol = motg->vdd_levels[VDD_LEVEL_NONE];
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600455
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300456 ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600457 if (ret) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300458 pr_err("Cannot set vddcx voltage\n");
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600459 return ret;
460 }
461
462 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
463
464 return ret;
465}
466
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530467static int msm_otg_suspend(struct msm_otg *motg)
468{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200469 struct usb_phy *phy = &motg->phy;
470 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530471 struct msm_otg_platform_data *pdata = motg->pdata;
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300472 void __iomem *addr;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530473 int cnt = 0;
474
475 if (atomic_read(&motg->in_lpm))
476 return 0;
477
478 disable_irq(motg->irq);
479 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530480 * Chipidea 45-nm PHY suspend sequence:
481 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530482 * Interrupt Latch Register auto-clear feature is not present
483 * in all PHY versions. Latch register is clear on read type.
484 * Clear latch register to avoid spurious wakeup from
485 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530486 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530487 * PHY comparators are disabled when PHY enters into low power
488 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
489 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
490 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530491 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530492 * PLL is not turned off when PHY enters into low power mode (LPM).
493 * Disable PLL for maximum power savings.
494 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530495
496 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200497 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530498 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200499 ulpi_write(phy, 0x01, 0x30);
500 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530501 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530502
503 /*
504 * PHY may take some time or even fail to enter into low power
505 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
506 * in failure case.
507 */
508 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
509 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
510 if (readl(USB_PORTSC) & PORTSC_PHCD)
511 break;
512 udelay(1);
513 cnt++;
514 }
515
516 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200517 dev_err(phy->dev, "Unable to suspend PHY\n");
518 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530519 enable_irq(motg->irq);
520 return -ETIMEDOUT;
521 }
522
523 /*
524 * PHY has capability to generate interrupt asynchronously in low
525 * power mode (LPM). This interrupt is level triggered. So USB IRQ
526 * line must be disabled till async interrupt enable bit is cleared
527 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
528 * block data communication from PHY.
529 */
530 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
531
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300532 addr = USB_PHY_CTRL;
533 if (motg->phy_number)
534 addr = USB_PHY_CTRL2;
535
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530536 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
537 motg->pdata->otg_control == OTG_PMIC_CONTROL)
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300538 writel(readl(addr) | PHY_RETEN, addr);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530539
Stephen Boydb99a8f62013-06-17 10:43:10 -0700540 clk_disable_unprepare(motg->pclk);
541 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300542 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700543 clk_disable_unprepare(motg->core_clk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530544
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530545 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
546 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300547 msm_hsusb_ldo_set_mode(motg, 0);
548 msm_hsusb_config_vddcx(motg, 0);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530549 }
550
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200551 if (device_may_wakeup(phy->dev))
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530552 enable_irq_wake(motg->irq);
553 if (bus)
554 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
555
556 atomic_set(&motg->in_lpm, 1);
557 enable_irq(motg->irq);
558
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200559 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530560
561 return 0;
562}
563
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530564static int msm_otg_resume(struct msm_otg *motg)
565{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200566 struct usb_phy *phy = &motg->phy;
567 struct usb_bus *bus = phy->otg->host;
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300568 void __iomem *addr;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530569 int cnt = 0;
570 unsigned temp;
571
572 if (!atomic_read(&motg->in_lpm))
573 return 0;
574
Stephen Boydb99a8f62013-06-17 10:43:10 -0700575 clk_prepare_enable(motg->pclk);
576 clk_prepare_enable(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300577 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700578 clk_prepare_enable(motg->core_clk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530579
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530580 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
581 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300582
583 addr = USB_PHY_CTRL;
584 if (motg->phy_number)
585 addr = USB_PHY_CTRL2;
586
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300587 msm_hsusb_ldo_set_mode(motg, 1);
588 msm_hsusb_config_vddcx(motg, 1);
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300589 writel(readl(addr) & ~PHY_RETEN, addr);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530590 }
591
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530592 temp = readl(USB_USBCMD);
593 temp &= ~ASYNC_INTR_CTRL;
594 temp &= ~ULPI_STP_CTRL;
595 writel(temp, USB_USBCMD);
596
597 /*
598 * PHY comes out of low power mode (LPM) in case of wakeup
599 * from asynchronous interrupt.
600 */
601 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
602 goto skip_phy_resume;
603
604 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
605 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
606 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
607 break;
608 udelay(1);
609 cnt++;
610 }
611
612 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
613 /*
614 * This is a fatal error. Reset the link and
615 * PHY. USB state can not be restored. Re-insertion
616 * of USB cable is the only way to get USB working.
617 */
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300618 dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200619 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530620 }
621
622skip_phy_resume:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200623 if (device_may_wakeup(phy->dev))
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530624 disable_irq_wake(motg->irq);
625 if (bus)
626 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
627
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530628 atomic_set(&motg->in_lpm, 0);
629
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530630 if (motg->async_int) {
631 motg->async_int = 0;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200632 pm_runtime_put(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530633 enable_irq(motg->irq);
634 }
635
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200636 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530637
638 return 0;
639}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530640#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530641
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530642static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
643{
644 if (motg->cur_power == mA)
645 return;
646
647 /* TODO: Notify PMIC about available current */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200648 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530649 motg->cur_power = mA;
650}
651
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200652static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530653{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200654 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530655
656 /*
657 * Gadget driver uses set_power method to notify about the
658 * available current based on suspend/configured states.
659 *
660 * IDEV_CHG can be drawn irrespective of suspend/un-configured
661 * states when CDP/ACA is connected.
662 */
663 if (motg->chg_type == USB_SDP_CHARGER)
664 msm_otg_notify_charger(motg, mA);
665
666 return 0;
667}
668
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200669static void msm_otg_start_host(struct usb_phy *phy, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530670{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200671 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530672 struct msm_otg_platform_data *pdata = motg->pdata;
673 struct usb_hcd *hcd;
674
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200675 if (!phy->otg->host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530676 return;
677
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200678 hcd = bus_to_hcd(phy->otg->host);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530679
680 if (on) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200681 dev_dbg(phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530682
683 if (pdata->vbus_power)
684 pdata->vbus_power(1);
685 /*
686 * Some boards have a switch cotrolled by gpio
687 * to enable/disable internal HUB. Enable internal
688 * HUB before kicking the host.
689 */
690 if (pdata->setup_gpio)
691 pdata->setup_gpio(OTG_STATE_A_HOST);
692#ifdef CONFIG_USB
693 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Peter Chen3c9740a2013-11-05 10:46:02 +0800694 device_wakeup_enable(hcd->self.controller);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530695#endif
696 } else {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200697 dev_dbg(phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530698
699#ifdef CONFIG_USB
700 usb_remove_hcd(hcd);
701#endif
702 if (pdata->setup_gpio)
703 pdata->setup_gpio(OTG_STATE_UNDEFINED);
704 if (pdata->vbus_power)
705 pdata->vbus_power(0);
706 }
707}
708
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200709static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530710{
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100711 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530712 struct usb_hcd *hcd;
713
714 /*
715 * Fail host registration if this board can support
716 * only peripheral configuration.
717 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300718 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100719 dev_info(otg->usb_phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530720 return -ENODEV;
721 }
722
723 if (!host) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100724 if (otg->state == OTG_STATE_A_HOST) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100725 pm_runtime_get_sync(otg->usb_phy->dev);
726 msm_otg_start_host(otg->usb_phy, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530727 otg->host = NULL;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100728 otg->state = OTG_STATE_UNDEFINED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530729 schedule_work(&motg->sm_work);
730 } else {
731 otg->host = NULL;
732 }
733
734 return 0;
735 }
736
737 hcd = bus_to_hcd(host);
738 hcd->power_budget = motg->pdata->power_budget;
739
740 otg->host = host;
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100741 dev_dbg(otg->usb_phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530742
743 /*
744 * Kick the state machine work, if peripheral is not supported
745 * or peripheral is already registered with us.
746 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300747 if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100748 pm_runtime_get_sync(otg->usb_phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530749 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530750 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530751
752 return 0;
753}
754
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200755static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530756{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200757 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530758 struct msm_otg_platform_data *pdata = motg->pdata;
759
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200760 if (!phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530761 return;
762
763 if (on) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200764 dev_dbg(phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530765 /*
766 * Some boards have a switch cotrolled by gpio
767 * to enable/disable internal HUB. Disable internal
768 * HUB before kicking the gadget.
769 */
770 if (pdata->setup_gpio)
771 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200772 usb_gadget_vbus_connect(phy->otg->gadget);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530773 } else {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200774 dev_dbg(phy->dev, "gadget off\n");
775 usb_gadget_vbus_disconnect(phy->otg->gadget);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530776 if (pdata->setup_gpio)
777 pdata->setup_gpio(OTG_STATE_UNDEFINED);
778 }
779
780}
781
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200782static int msm_otg_set_peripheral(struct usb_otg *otg,
783 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530784{
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100785 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530786
787 /*
788 * Fail peripheral registration if this board can support
789 * only host configuration.
790 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300791 if (motg->pdata->mode == USB_DR_MODE_HOST) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100792 dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530793 return -ENODEV;
794 }
795
796 if (!gadget) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100797 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100798 pm_runtime_get_sync(otg->usb_phy->dev);
799 msm_otg_start_peripheral(otg->usb_phy, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530800 otg->gadget = NULL;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100801 otg->state = OTG_STATE_UNDEFINED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530802 schedule_work(&motg->sm_work);
803 } else {
804 otg->gadget = NULL;
805 }
806
807 return 0;
808 }
809 otg->gadget = gadget;
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100810 dev_dbg(otg->usb_phy->dev,
811 "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530812
813 /*
814 * Kick the state machine work, if host is not supported
815 * or host is already registered with us.
816 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300817 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100818 pm_runtime_get_sync(otg->usb_phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530819 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530820 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530821
822 return 0;
823}
824
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530825static bool msm_chg_check_secondary_det(struct msm_otg *motg)
826{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200827 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530828 u32 chg_det;
829 bool ret = false;
830
831 switch (motg->pdata->phy_type) {
832 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200833 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530834 ret = chg_det & (1 << 4);
835 break;
836 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200837 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530838 ret = chg_det & 1;
839 break;
840 default:
841 break;
842 }
843 return ret;
844}
845
846static void msm_chg_enable_secondary_det(struct msm_otg *motg)
847{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200848 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530849 u32 chg_det;
850
851 switch (motg->pdata->phy_type) {
852 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200853 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530854 /* Turn off charger block */
855 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200856 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530857 udelay(20);
858 /* control chg block via ULPI */
859 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200860 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530861 /* put it in host mode for enabling D- source */
862 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200863 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530864 /* Turn on chg detect block */
865 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200866 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530867 udelay(20);
868 /* enable chg detection */
869 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200870 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530871 break;
872 case SNPS_28NM_INTEGRATED_PHY:
873 /*
874 * Configure DM as current source, DP as current sink
875 * and enable battery charging comparators.
876 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200877 ulpi_write(phy, 0x8, 0x85);
878 ulpi_write(phy, 0x2, 0x85);
879 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530880 break;
881 default:
882 break;
883 }
884}
885
886static bool msm_chg_check_primary_det(struct msm_otg *motg)
887{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200888 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530889 u32 chg_det;
890 bool ret = false;
891
892 switch (motg->pdata->phy_type) {
893 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200894 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530895 ret = chg_det & (1 << 4);
896 break;
897 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200898 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530899 ret = chg_det & 1;
900 break;
901 default:
902 break;
903 }
904 return ret;
905}
906
907static void msm_chg_enable_primary_det(struct msm_otg *motg)
908{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200909 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530910 u32 chg_det;
911
912 switch (motg->pdata->phy_type) {
913 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200914 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530915 /* enable chg detection */
916 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200917 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530918 break;
919 case SNPS_28NM_INTEGRATED_PHY:
920 /*
921 * Configure DP as current source, DM as current sink
922 * and enable battery charging comparators.
923 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200924 ulpi_write(phy, 0x2, 0x85);
925 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530926 break;
927 default:
928 break;
929 }
930}
931
932static bool msm_chg_check_dcd(struct msm_otg *motg)
933{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200934 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530935 u32 line_state;
936 bool ret = false;
937
938 switch (motg->pdata->phy_type) {
939 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200940 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530941 ret = !(line_state & 1);
942 break;
943 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200944 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530945 ret = line_state & 2;
946 break;
947 default:
948 break;
949 }
950 return ret;
951}
952
953static void msm_chg_disable_dcd(struct msm_otg *motg)
954{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200955 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530956 u32 chg_det;
957
958 switch (motg->pdata->phy_type) {
959 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200960 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530961 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200962 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530963 break;
964 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200965 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530966 break;
967 default:
968 break;
969 }
970}
971
972static void msm_chg_enable_dcd(struct msm_otg *motg)
973{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200974 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530975 u32 chg_det;
976
977 switch (motg->pdata->phy_type) {
978 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200979 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530980 /* Turn on D+ current source */
981 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200982 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530983 break;
984 case SNPS_28NM_INTEGRATED_PHY:
985 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200986 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530987 break;
988 default:
989 break;
990 }
991}
992
993static void msm_chg_block_on(struct msm_otg *motg)
994{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200995 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530996 u32 func_ctrl, chg_det;
997
998 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200999 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301000 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1001 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001002 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301003
1004 switch (motg->pdata->phy_type) {
1005 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001006 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301007 /* control chg block via ULPI */
1008 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001009 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301010 /* Turn on chg detect block */
1011 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001012 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301013 udelay(20);
1014 break;
1015 case SNPS_28NM_INTEGRATED_PHY:
1016 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001017 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301018 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001019 ulpi_write(phy, 0x1F, 0x92);
1020 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301021 udelay(100);
1022 break;
1023 default:
1024 break;
1025 }
1026}
1027
1028static void msm_chg_block_off(struct msm_otg *motg)
1029{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001030 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301031 u32 func_ctrl, chg_det;
1032
1033 switch (motg->pdata->phy_type) {
1034 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001035 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301036 /* Turn off charger block */
1037 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001038 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301039 break;
1040 case SNPS_28NM_INTEGRATED_PHY:
1041 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001042 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301043 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001044 ulpi_write(phy, 0x1F, 0x92);
1045 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301046 break;
1047 default:
1048 break;
1049 }
1050
1051 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001052 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301053 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1054 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001055 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301056}
1057
1058#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1059#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1060#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1061#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1062static void msm_chg_detect_work(struct work_struct *w)
1063{
1064 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001065 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301066 bool is_dcd, tmout, vout;
1067 unsigned long delay;
1068
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001069 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301070 switch (motg->chg_state) {
1071 case USB_CHG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001072 pm_runtime_get_sync(phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301073 msm_chg_block_on(motg);
1074 msm_chg_enable_dcd(motg);
1075 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1076 motg->dcd_retries = 0;
1077 delay = MSM_CHG_DCD_POLL_TIME;
1078 break;
1079 case USB_CHG_STATE_WAIT_FOR_DCD:
1080 is_dcd = msm_chg_check_dcd(motg);
1081 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1082 if (is_dcd || tmout) {
1083 msm_chg_disable_dcd(motg);
1084 msm_chg_enable_primary_det(motg);
1085 delay = MSM_CHG_PRIMARY_DET_TIME;
1086 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1087 } else {
1088 delay = MSM_CHG_DCD_POLL_TIME;
1089 }
1090 break;
1091 case USB_CHG_STATE_DCD_DONE:
1092 vout = msm_chg_check_primary_det(motg);
1093 if (vout) {
1094 msm_chg_enable_secondary_det(motg);
1095 delay = MSM_CHG_SECONDARY_DET_TIME;
1096 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1097 } else {
1098 motg->chg_type = USB_SDP_CHARGER;
1099 motg->chg_state = USB_CHG_STATE_DETECTED;
1100 delay = 0;
1101 }
1102 break;
1103 case USB_CHG_STATE_PRIMARY_DONE:
1104 vout = msm_chg_check_secondary_det(motg);
1105 if (vout)
1106 motg->chg_type = USB_DCP_CHARGER;
1107 else
1108 motg->chg_type = USB_CDP_CHARGER;
1109 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1110 /* fall through */
1111 case USB_CHG_STATE_SECONDARY_DONE:
1112 motg->chg_state = USB_CHG_STATE_DETECTED;
1113 case USB_CHG_STATE_DETECTED:
1114 msm_chg_block_off(motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001115 dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301116 schedule_work(&motg->sm_work);
1117 return;
1118 default:
1119 return;
1120 }
1121
1122 schedule_delayed_work(&motg->chg_work, delay);
1123}
1124
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301125/*
1126 * We support OTG, Peripheral only and Host only configurations. In case
1127 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1128 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1129 * enabled when switch is controlled by user and default mode is supplied
1130 * by board file, which can be changed by userspace later.
1131 */
1132static void msm_otg_init_sm(struct msm_otg *motg)
1133{
1134 struct msm_otg_platform_data *pdata = motg->pdata;
1135 u32 otgsc = readl(USB_OTGSC);
1136
1137 switch (pdata->mode) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001138 case USB_DR_MODE_OTG:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301139 if (pdata->otg_control == OTG_PHY_CONTROL) {
1140 if (otgsc & OTGSC_ID)
1141 set_bit(ID, &motg->inputs);
1142 else
1143 clear_bit(ID, &motg->inputs);
1144
1145 if (otgsc & OTGSC_BSV)
1146 set_bit(B_SESS_VLD, &motg->inputs);
1147 else
1148 clear_bit(B_SESS_VLD, &motg->inputs);
1149 } else if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301150 set_bit(ID, &motg->inputs);
1151 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301152 }
1153 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001154 case USB_DR_MODE_HOST:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301155 clear_bit(ID, &motg->inputs);
1156 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001157 case USB_DR_MODE_PERIPHERAL:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301158 set_bit(ID, &motg->inputs);
1159 if (otgsc & OTGSC_BSV)
1160 set_bit(B_SESS_VLD, &motg->inputs);
1161 else
1162 clear_bit(B_SESS_VLD, &motg->inputs);
1163 break;
1164 default:
1165 break;
1166 }
1167}
1168
1169static void msm_otg_sm_work(struct work_struct *w)
1170{
1171 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001172 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301173
Antoine Tenarte47d9252014-10-30 18:41:13 +01001174 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301175 case OTG_STATE_UNDEFINED:
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001176 dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
1177 msm_otg_reset(otg->usb_phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301178 msm_otg_init_sm(motg);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001179 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301180 /* FALL THROUGH */
1181 case OTG_STATE_B_IDLE:
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001182 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301183 if (!test_bit(ID, &motg->inputs) && otg->host) {
1184 /* disable BSV bit */
1185 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001186 msm_otg_start_host(otg->usb_phy, 1);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001187 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301188 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1189 switch (motg->chg_state) {
1190 case USB_CHG_STATE_UNDEFINED:
1191 msm_chg_detect_work(&motg->chg_work.work);
1192 break;
1193 case USB_CHG_STATE_DETECTED:
1194 switch (motg->chg_type) {
1195 case USB_DCP_CHARGER:
1196 msm_otg_notify_charger(motg,
1197 IDEV_CHG_MAX);
1198 break;
1199 case USB_CDP_CHARGER:
1200 msm_otg_notify_charger(motg,
1201 IDEV_CHG_MAX);
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001202 msm_otg_start_peripheral(otg->usb_phy,
1203 1);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001204 otg->state
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001205 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301206 break;
1207 case USB_SDP_CHARGER:
1208 msm_otg_notify_charger(motg, IUNIT);
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001209 msm_otg_start_peripheral(otg->usb_phy,
1210 1);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001211 otg->state
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001212 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301213 break;
1214 default:
1215 break;
1216 }
1217 break;
1218 default:
1219 break;
1220 }
1221 } else {
1222 /*
1223 * If charger detection work is pending, decrement
1224 * the pm usage counter to balance with the one that
1225 * is incremented in charger detection work.
1226 */
1227 if (cancel_delayed_work_sync(&motg->chg_work)) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001228 pm_runtime_put_sync(otg->usb_phy->dev);
1229 msm_otg_reset(otg->usb_phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301230 }
1231 msm_otg_notify_charger(motg, 0);
1232 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1233 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301234 }
Srinivas Kandagatla508ccea2014-06-30 18:29:57 +01001235
Antoine Tenarte47d9252014-10-30 18:41:13 +01001236 if (otg->state == OTG_STATE_B_IDLE)
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001237 pm_runtime_put_sync(otg->usb_phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301238 break;
1239 case OTG_STATE_B_PERIPHERAL:
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001240 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301241 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1242 !test_bit(ID, &motg->inputs)) {
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301243 msm_otg_notify_charger(motg, 0);
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001244 msm_otg_start_peripheral(otg->usb_phy, 0);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301245 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1246 motg->chg_type = USB_INVALID_CHARGER;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001247 otg->state = OTG_STATE_B_IDLE;
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001248 msm_otg_reset(otg->usb_phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301249 schedule_work(w);
1250 }
1251 break;
1252 case OTG_STATE_A_HOST:
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001253 dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301254 if (test_bit(ID, &motg->inputs)) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001255 msm_otg_start_host(otg->usb_phy, 0);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001256 otg->state = OTG_STATE_B_IDLE;
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001257 msm_otg_reset(otg->usb_phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301258 schedule_work(w);
1259 }
1260 break;
1261 default:
1262 break;
1263 }
1264}
1265
1266static irqreturn_t msm_otg_irq(int irq, void *data)
1267{
1268 struct msm_otg *motg = data;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001269 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301270 u32 otgsc = 0;
1271
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301272 if (atomic_read(&motg->in_lpm)) {
1273 disable_irq_nosync(irq);
1274 motg->async_int = 1;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001275 pm_runtime_get(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301276 return IRQ_HANDLED;
1277 }
1278
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301279 otgsc = readl(USB_OTGSC);
1280 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1281 return IRQ_NONE;
1282
1283 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1284 if (otgsc & OTGSC_ID)
1285 set_bit(ID, &motg->inputs);
1286 else
1287 clear_bit(ID, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001288 dev_dbg(phy->dev, "ID set/clear\n");
1289 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301290 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1291 if (otgsc & OTGSC_BSV)
1292 set_bit(B_SESS_VLD, &motg->inputs);
1293 else
1294 clear_bit(B_SESS_VLD, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001295 dev_dbg(phy->dev, "BSV set/clear\n");
1296 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301297 }
1298
1299 writel(otgsc, USB_OTGSC);
1300 schedule_work(&motg->sm_work);
1301 return IRQ_HANDLED;
1302}
1303
1304static int msm_otg_mode_show(struct seq_file *s, void *unused)
1305{
1306 struct msm_otg *motg = s->private;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001307 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301308
Antoine Tenarte47d9252014-10-30 18:41:13 +01001309 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301310 case OTG_STATE_A_HOST:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001311 seq_puts(s, "host\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301312 break;
1313 case OTG_STATE_B_PERIPHERAL:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001314 seq_puts(s, "peripheral\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301315 break;
1316 default:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001317 seq_puts(s, "none\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301318 break;
1319 }
1320
1321 return 0;
1322}
1323
1324static int msm_otg_mode_open(struct inode *inode, struct file *file)
1325{
1326 return single_open(file, msm_otg_mode_show, inode->i_private);
1327}
1328
1329static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1330 size_t count, loff_t *ppos)
1331{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301332 struct seq_file *s = file->private_data;
1333 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301334 char buf[16];
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001335 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301336 int status = count;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001337 enum usb_dr_mode req_mode;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301338
1339 memset(buf, 0x00, sizeof(buf));
1340
1341 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1342 status = -EFAULT;
1343 goto out;
1344 }
1345
1346 if (!strncmp(buf, "host", 4)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001347 req_mode = USB_DR_MODE_HOST;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301348 } else if (!strncmp(buf, "peripheral", 10)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001349 req_mode = USB_DR_MODE_PERIPHERAL;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301350 } else if (!strncmp(buf, "none", 4)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001351 req_mode = USB_DR_MODE_UNKNOWN;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301352 } else {
1353 status = -EINVAL;
1354 goto out;
1355 }
1356
1357 switch (req_mode) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001358 case USB_DR_MODE_UNKNOWN:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001359 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301360 case OTG_STATE_A_HOST:
1361 case OTG_STATE_B_PERIPHERAL:
1362 set_bit(ID, &motg->inputs);
1363 clear_bit(B_SESS_VLD, &motg->inputs);
1364 break;
1365 default:
1366 goto out;
1367 }
1368 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001369 case USB_DR_MODE_PERIPHERAL:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001370 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301371 case OTG_STATE_B_IDLE:
1372 case OTG_STATE_A_HOST:
1373 set_bit(ID, &motg->inputs);
1374 set_bit(B_SESS_VLD, &motg->inputs);
1375 break;
1376 default:
1377 goto out;
1378 }
1379 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001380 case USB_DR_MODE_HOST:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001381 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301382 case OTG_STATE_B_IDLE:
1383 case OTG_STATE_B_PERIPHERAL:
1384 clear_bit(ID, &motg->inputs);
1385 break;
1386 default:
1387 goto out;
1388 }
1389 break;
1390 default:
1391 goto out;
1392 }
1393
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001394 pm_runtime_get_sync(otg->usb_phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301395 schedule_work(&motg->sm_work);
1396out:
1397 return status;
1398}
1399
Felipe Balbi8f90afd2014-08-20 13:38:18 -05001400static const struct file_operations msm_otg_mode_fops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301401 .open = msm_otg_mode_open,
1402 .read = seq_read,
1403 .write = msm_otg_mode_write,
1404 .llseek = seq_lseek,
1405 .release = single_release,
1406};
1407
1408static struct dentry *msm_otg_dbg_root;
1409static struct dentry *msm_otg_dbg_mode;
1410
1411static int msm_otg_debugfs_init(struct msm_otg *motg)
1412{
1413 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1414
1415 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1416 return -ENODEV;
1417
1418 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1419 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1420 if (!msm_otg_dbg_mode) {
1421 debugfs_remove(msm_otg_dbg_root);
1422 msm_otg_dbg_root = NULL;
1423 return -ENODEV;
1424 }
1425
1426 return 0;
1427}
1428
1429static void msm_otg_debugfs_cleanup(void)
1430{
1431 debugfs_remove(msm_otg_dbg_mode);
1432 debugfs_remove(msm_otg_dbg_root);
1433}
1434
Jingoo Han492240b2014-06-18 13:42:44 +09001435static const struct of_device_id msm_otg_dt_match[] = {
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001436 {
1437 .compatible = "qcom,usb-otg-ci",
1438 .data = (void *) CI_45NM_INTEGRATED_PHY
1439 },
1440 {
1441 .compatible = "qcom,usb-otg-snps",
1442 .data = (void *) SNPS_28NM_INTEGRATED_PHY
1443 },
1444 { }
1445};
1446MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
1447
1448static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
1449{
1450 struct msm_otg_platform_data *pdata;
1451 const struct of_device_id *id;
1452 struct device_node *node = pdev->dev.of_node;
1453 struct property *prop;
1454 int len, ret, words;
Ivan T. Ivanov01799b62014-04-28 16:34:22 +03001455 u32 val, tmp[3];
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001456
1457 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1458 if (!pdata)
1459 return -ENOMEM;
1460
1461 motg->pdata = pdata;
1462
1463 id = of_match_device(msm_otg_dt_match, &pdev->dev);
Felipe Balbib3025e62014-04-30 11:33:04 -05001464 pdata->phy_type = (enum msm_usb_phy_type) id->data;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001465
Ivan T. Ivanova2734542014-04-28 16:34:16 +03001466 motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
1467 if (IS_ERR(motg->link_rst))
1468 return PTR_ERR(motg->link_rst);
1469
1470 motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
1471 if (IS_ERR(motg->phy_rst))
Srinivas Kandagatlae44f1f42014-06-30 18:29:49 +01001472 motg->phy_rst = NULL;
Ivan T. Ivanova2734542014-04-28 16:34:16 +03001473
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001474 pdata->mode = of_usb_get_dr_mode(node);
1475 if (pdata->mode == USB_DR_MODE_UNKNOWN)
1476 pdata->mode = USB_DR_MODE_OTG;
1477
1478 pdata->otg_control = OTG_PHY_CONTROL;
1479 if (!of_property_read_u32(node, "qcom,otg-control", &val))
1480 if (val == OTG_PMIC_CONTROL)
1481 pdata->otg_control = val;
1482
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +03001483 if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
1484 motg->phy_number = val;
1485
Ivan T. Ivanov01799b62014-04-28 16:34:22 +03001486 motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
1487 motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
1488 motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
1489
1490 if (of_get_property(node, "qcom,vdd-levels", &len) &&
1491 len == sizeof(tmp)) {
1492 of_property_read_u32_array(node, "qcom,vdd-levels",
1493 tmp, len / sizeof(*tmp));
1494 motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
1495 motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
1496 motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
1497 }
1498
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001499 prop = of_find_property(node, "qcom,phy-init-sequence", &len);
1500 if (!prop || !len)
1501 return 0;
1502
1503 words = len / sizeof(u32);
1504
1505 if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
1506 dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
1507 return 0;
1508 }
1509
1510 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
Peter Chen9da22202014-10-14 15:56:17 +08001511 if (!pdata->phy_init_seq)
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001512 return 0;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001513
1514 ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
1515 pdata->phy_init_seq, words);
1516 if (!ret)
1517 pdata->phy_init_sz = words;
1518
1519 return 0;
1520}
1521
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001522static int msm_otg_probe(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301523{
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001524 struct regulator_bulk_data regs[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301525 int ret = 0;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001526 struct device_node *np = pdev->dev.of_node;
1527 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301528 struct resource *res;
1529 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001530 struct usb_phy *phy;
Tim Bird30bf8662014-04-28 16:34:20 +03001531 void __iomem *phy_select;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301532
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001533 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Peter Chen9da22202014-10-14 15:56:17 +08001534 if (!motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301535 return -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301536
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001537 pdata = dev_get_platdata(&pdev->dev);
1538 if (!pdata) {
1539 if (!np)
1540 return -ENXIO;
1541 ret = msm_otg_read_dt(pdev, motg);
1542 if (ret)
1543 return ret;
1544 }
1545
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001546 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1547 GFP_KERNEL);
Peter Chen9da22202014-10-14 15:56:17 +08001548 if (!motg->phy.otg)
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001549 return -ENOMEM;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001550
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001551 phy = &motg->phy;
1552 phy->dev = &pdev->dev;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301553
Srinivas Kandagatlaa1a4caf2014-08-21 07:45:10 +01001554 if (motg->pdata->phy_clk_reset) {
1555 motg->phy_reset_clk = devm_clk_get(&pdev->dev,
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001556 np ? "phy" : "usb_phy_clk");
Srinivas Kandagatlaa1a4caf2014-08-21 07:45:10 +01001557
1558 if (IS_ERR(motg->phy_reset_clk)) {
1559 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
1560 return PTR_ERR(motg->phy_reset_clk);
1561 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301562 }
1563
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001564 motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301565 if (IS_ERR(motg->clk)) {
1566 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001567 return PTR_ERR(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301568 }
Anji jonnala0f73cac82011-05-04 10:19:46 +05301569
1570 /*
1571 * If USB Core is running its protocol engine based on CORE CLK,
1572 * CORE CLK must be running at >55Mhz for correct HSUSB
1573 * operation and USB core cannot tolerate frequency changes on
Ivan T. Ivanovff0e4a62014-04-28 16:34:12 +03001574 * CORE CLK.
Anji jonnala0f73cac82011-05-04 10:19:46 +05301575 */
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001576 motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301577 if (IS_ERR(motg->pclk)) {
1578 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001579 return PTR_ERR(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301580 }
1581
1582 /*
1583 * USB core clock is not present on all MSM chips. This
1584 * clock is introduced to remove the dependency on AXI
1585 * bus frequency.
1586 */
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001587 motg->core_clk = devm_clk_get(&pdev->dev,
1588 np ? "alt_core" : "usb_hs_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301589
1590 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter2ea7b142014-05-19 23:35:19 +03001591 if (!res)
1592 return -EINVAL;
1593 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1594 if (!motg->regs)
1595 return -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301596
Tim Bird30bf8662014-04-28 16:34:20 +03001597 /*
1598 * NOTE: The PHYs can be multiplexed between the chipidea controller
1599 * and the dwc3 controller, using a single bit. It is important that
1600 * the dwc3 driver does not set this bit in an incompatible way.
1601 */
1602 if (motg->phy_number) {
1603 phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
Wei Yongjun716d28e2014-07-20 11:40:37 +08001604 if (!phy_select)
1605 return -ENOMEM;
Tim Bird30bf8662014-04-28 16:34:20 +03001606 /* Enable second PHY with the OTG port */
Felipe Balbi24597492014-04-30 11:35:22 -05001607 writel(0x1, phy_select);
Tim Bird30bf8662014-04-28 16:34:20 +03001608 }
1609
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301610 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1611
1612 motg->irq = platform_get_irq(pdev, 0);
Ivan T. Ivanovf60c1142014-04-28 16:34:14 +03001613 if (motg->irq < 0) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301614 dev_err(&pdev->dev, "platform_get_irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001615 return motg->irq;
1616 }
1617
Ivan T. Ivanovf5ef2372014-04-28 16:34:13 +03001618 regs[0].supply = "vddcx";
1619 regs[1].supply = "v3p3";
1620 regs[2].supply = "v1p8";
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001621
1622 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1623 if (ret)
1624 return ret;
1625
1626 motg->vddcx = regs[0].consumer;
1627 motg->v3p3 = regs[1].consumer;
1628 motg->v1p8 = regs[2].consumer;
1629
1630 clk_set_rate(motg->clk, 60000000);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301631
Stephen Boydb99a8f62013-06-17 10:43:10 -07001632 clk_prepare_enable(motg->clk);
1633 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301634
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001635 if (!IS_ERR(motg->core_clk))
1636 clk_prepare_enable(motg->core_clk);
1637
Anji jonnala11aa5c42011-05-04 10:19:48 +05301638 ret = msm_hsusb_init_vddcx(motg, 1);
1639 if (ret) {
1640 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001641 goto disable_clks;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301642 }
1643
1644 ret = msm_hsusb_ldo_init(motg, 1);
1645 if (ret) {
1646 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001647 goto disable_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301648 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +03001649 ret = msm_hsusb_ldo_set_mode(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301650 if (ret) {
1651 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001652 goto disable_ldo;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301653 }
1654
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301655 writel(0, USB_USBINTR);
1656 writel(0, USB_OTGSC);
1657
1658 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301659 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001660 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301661 "msm_otg", motg);
1662 if (ret) {
1663 dev_err(&pdev->dev, "request irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001664 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301665 }
1666
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +03001667 phy->init = msm_phy_init;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001668 phy->set_power = msm_otg_set_power;
Ivan T. Ivanov349907c2014-04-28 16:34:21 +03001669 phy->notify_disconnect = msm_phy_notify_disconnect;
Ivan T. Ivanove695abb2014-04-28 16:34:23 +03001670 phy->type = USB_PHY_TYPE_USB2;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301671
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001672 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301673
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001674 phy->otg->usb_phy = &motg->phy;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001675 phy->otg->set_host = msm_otg_set_host;
1676 phy->otg->set_peripheral = msm_otg_set_peripheral;
1677
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +03001678 msm_usb_reset(phy);
1679
Ivan T. Ivanove695abb2014-04-28 16:34:23 +03001680 ret = usb_add_phy_dev(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301681 if (ret) {
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301682 dev_err(&pdev->dev, "usb_add_phy failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001683 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301684 }
1685
1686 platform_set_drvdata(pdev, motg);
1687 device_init_wakeup(&pdev->dev, 1);
1688
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001689 if (motg->pdata->mode == USB_DR_MODE_OTG &&
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001690 motg->pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301691 ret = msm_otg_debugfs_init(motg);
1692 if (ret)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001693 dev_dbg(&pdev->dev, "Can not create mode change file\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301694 }
1695
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301696 pm_runtime_set_active(&pdev->dev);
1697 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301698
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301699 return 0;
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001700
1701disable_ldo:
1702 msm_hsusb_ldo_init(motg, 0);
1703disable_vddcx:
1704 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301705disable_clks:
Stephen Boydb99a8f62013-06-17 10:43:10 -07001706 clk_disable_unprepare(motg->pclk);
1707 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001708 if (!IS_ERR(motg->core_clk))
1709 clk_disable_unprepare(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301710 return ret;
1711}
1712
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001713static int msm_otg_remove(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301714{
1715 struct msm_otg *motg = platform_get_drvdata(pdev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001716 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301717 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301718
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001719 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301720 return -EBUSY;
1721
1722 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301723 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301724 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301725
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301726 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301727
1728 device_init_wakeup(&pdev->dev, 0);
1729 pm_runtime_disable(&pdev->dev);
1730
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301731 usb_remove_phy(phy);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001732 disable_irq(motg->irq);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301733
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301734 /*
1735 * Put PHY in low power mode.
1736 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001737 ulpi_read(phy, 0x14);
1738 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301739
1740 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1741 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1742 if (readl(USB_PORTSC) & PORTSC_PHCD)
1743 break;
1744 udelay(1);
1745 cnt++;
1746 }
1747 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001748 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301749
Stephen Boydb99a8f62013-06-17 10:43:10 -07001750 clk_disable_unprepare(motg->pclk);
1751 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001752 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001753 clk_disable_unprepare(motg->core_clk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301754 msm_hsusb_ldo_init(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301755
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301756 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301757
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301758 return 0;
1759}
1760
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301761#ifdef CONFIG_PM_RUNTIME
1762static int msm_otg_runtime_idle(struct device *dev)
1763{
1764 struct msm_otg *motg = dev_get_drvdata(dev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001765 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301766
1767 dev_dbg(dev, "OTG runtime idle\n");
1768
1769 /*
1770 * It is observed some times that a spurious interrupt
1771 * comes when PHY is put into LPM immediately after PHY reset.
1772 * This 1 sec delay also prevents entering into LPM immediately
1773 * after asynchronous interrupt.
1774 */
Antoine Tenarte47d9252014-10-30 18:41:13 +01001775 if (otg->state != OTG_STATE_UNDEFINED)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301776 pm_schedule_suspend(dev, 1000);
1777
1778 return -EAGAIN;
1779}
1780
1781static int msm_otg_runtime_suspend(struct device *dev)
1782{
1783 struct msm_otg *motg = dev_get_drvdata(dev);
1784
1785 dev_dbg(dev, "OTG runtime suspend\n");
1786 return msm_otg_suspend(motg);
1787}
1788
1789static int msm_otg_runtime_resume(struct device *dev)
1790{
1791 struct msm_otg *motg = dev_get_drvdata(dev);
1792
1793 dev_dbg(dev, "OTG runtime resume\n");
1794 return msm_otg_resume(motg);
1795}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301796#endif
1797
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301798#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301799static int msm_otg_pm_suspend(struct device *dev)
1800{
1801 struct msm_otg *motg = dev_get_drvdata(dev);
1802
1803 dev_dbg(dev, "OTG PM suspend\n");
1804 return msm_otg_suspend(motg);
1805}
1806
1807static int msm_otg_pm_resume(struct device *dev)
1808{
1809 struct msm_otg *motg = dev_get_drvdata(dev);
1810 int ret;
1811
1812 dev_dbg(dev, "OTG PM resume\n");
1813
1814 ret = msm_otg_resume(motg);
1815 if (ret)
1816 return ret;
1817
1818 /*
1819 * Runtime PM Documentation recommends bringing the
1820 * device to full powered state upon resume.
1821 */
1822 pm_runtime_disable(dev);
1823 pm_runtime_set_active(dev);
1824 pm_runtime_enable(dev);
1825
1826 return 0;
1827}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301828#endif
1829
1830static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301831 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
1832 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
1833 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301834};
1835
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301836static struct platform_driver msm_otg_driver = {
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001837 .probe = msm_otg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001838 .remove = msm_otg_remove,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301839 .driver = {
1840 .name = DRIVER_NAME,
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301841 .pm = &msm_otg_dev_pm_ops,
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001842 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301843 },
1844};
1845
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001846module_platform_driver(msm_otg_driver);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301847
1848MODULE_LICENSE("GPL v2");
1849MODULE_DESCRIPTION("MSM USB transceiver driver");