blob: 6ed67ea4ef7e594327e46c79d25451fe163713fa [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) {
Bjorn Anderssonfa53e352015-02-11 19:35:30 -0800145 ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_HPM_LOAD);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530146 if (ret < 0) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300147 pr_err("Could not set HPM for v1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 return ret;
149 }
Bjorn Anderssonfa53e352015-02-11 19:35:30 -0800150 ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_HPM_LOAD);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530151 if (ret < 0) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300152 pr_err("Could not set HPM for v3p3\n");
Bjorn Anderssonfa53e352015-02-11 19:35:30 -0800153 regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530154 return ret;
155 }
156 } else {
Bjorn Anderssonfa53e352015-02-11 19:35:30 -0800157 ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530158 if (ret < 0)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300159 pr_err("Could not set LPM for v1p8\n");
Bjorn Anderssonfa53e352015-02-11 19:35:30 -0800160 ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_LPM_LOAD);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530161 if (ret < 0)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300162 pr_err("Could not set LPM for v3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530163 }
164
165 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
166 return ret < 0 ? ret : 0;
167}
168
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200169static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530170{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200171 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530172 int cnt = 0;
173
174 /* initiate read operation */
175 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
176 USB_ULPI_VIEWPORT);
177
178 /* wait for completion */
179 while (cnt < ULPI_IO_TIMEOUT_USEC) {
180 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
181 break;
182 udelay(1);
183 cnt++;
184 }
185
186 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200187 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530188 readl(USB_ULPI_VIEWPORT));
189 return -ETIMEDOUT;
190 }
191 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
192}
193
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200194static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530195{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200196 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530197 int cnt = 0;
198
199 /* initiate write operation */
200 writel(ULPI_RUN | ULPI_WRITE |
201 ULPI_ADDR(reg) | ULPI_DATA(val),
202 USB_ULPI_VIEWPORT);
203
204 /* wait for completion */
205 while (cnt < ULPI_IO_TIMEOUT_USEC) {
206 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
207 break;
208 udelay(1);
209 cnt++;
210 }
211
212 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200213 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530214 return -ETIMEDOUT;
215 }
216 return 0;
217}
218
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200219static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530220 .read = ulpi_read,
221 .write = ulpi_write,
222};
223
224static void ulpi_init(struct msm_otg *motg)
225{
226 struct msm_otg_platform_data *pdata = motg->pdata;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300227 int *seq = pdata->phy_init_seq, idx;
228 u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530229
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300230 for (idx = 0; idx < pdata->phy_init_sz; idx++) {
231 if (seq[idx] == -1)
232 continue;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530233
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200234 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +0300235 seq[idx], addr + idx);
236 ulpi_write(&motg->phy, seq[idx], addr + idx);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530237 }
238}
239
Ivan T. Ivanov349907c2014-04-28 16:34:21 +0300240static int msm_phy_notify_disconnect(struct usb_phy *phy,
241 enum usb_device_speed speed)
242{
243 int val;
244
245 /*
246 * Put the transceiver in non-driving mode. Otherwise host
247 * may not detect soft-disconnection.
248 */
249 val = ulpi_read(phy, ULPI_FUNC_CTRL);
250 val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
251 val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
252 ulpi_write(phy, val, ULPI_FUNC_CTRL);
253
254 return 0;
255}
256
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530257static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
258{
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300259 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530260
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300261 if (motg->pdata->link_clk_reset)
262 ret = motg->pdata->link_clk_reset(motg->clk, assert);
263 else if (assert)
264 ret = reset_control_assert(motg->link_rst);
265 else
266 ret = reset_control_deassert(motg->link_rst);
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800267
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800268 if (ret)
269 dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
270 assert ? "assert" : "deassert");
271
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530272 return ret;
273}
274
275static int msm_otg_phy_clk_reset(struct msm_otg *motg)
276{
Srinivas Kandagatlae44f1f42014-06-30 18:29:49 +0100277 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530278
Srinivas Kandagatlaa1a4caf2014-08-21 07:45:10 +0100279 if (motg->pdata->phy_clk_reset)
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300280 ret = motg->pdata->phy_clk_reset(motg->phy_reset_clk);
Srinivas Kandagatlae44f1f42014-06-30 18:29:49 +0100281 else if (motg->phy_rst)
Ivan T. Ivanova2734542014-04-28 16:34:16 +0300282 ret = reset_control_reset(motg->phy_rst);
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800283
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530284 if (ret)
Ivan T. Ivanov5146d772013-12-30 13:15:27 -0800285 dev_err(motg->phy.dev, "usb phy clk reset failed\n");
286
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530287 return ret;
288}
289
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300290static int msm_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530291{
292 u32 val;
293 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530294
295 ret = msm_otg_link_clk_reset(motg, 1);
296 if (ret)
297 return ret;
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300298
299 /* wait for 1ms delay as suggested in HPG. */
300 usleep_range(1000, 1200);
301
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530302 ret = msm_otg_link_clk_reset(motg, 0);
303 if (ret)
304 return ret;
305
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300306 if (motg->phy_number)
307 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
308
Tim Bird9f27984b2014-04-28 16:34:19 +0300309 /* put transceiver in serial mode as part of reset */
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300310 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
Tim Bird9f27984b2014-04-28 16:34:19 +0300311 writel(val | PORTSC_PTS_SERIAL, USB_PORTSC);
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300312
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530313 return 0;
314}
315
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200316static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530317{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200318 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530319 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530320
321 writel(USBCMD_RESET, USB_USBCMD);
322 while (cnt < LINK_RESET_TIMEOUT_USEC) {
323 if (!(readl(USB_USBCMD) & USBCMD_RESET))
324 break;
325 udelay(1);
326 cnt++;
327 }
328 if (cnt >= LINK_RESET_TIMEOUT_USEC)
329 return -ETIMEDOUT;
330
Tim Bird9f27984b2014-04-28 16:34:19 +0300331 /* select ULPI phy and clear other status/control bits in PORTSC */
332 writel(PORTSC_PTS_ULPI, USB_PORTSC);
333
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300334 writel(0x0, USB_AHBBURST);
335 writel(0x08, USB_AHBMODE);
336
337 if (motg->phy_number)
338 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
339 return 0;
340}
341
342static void msm_phy_reset(struct msm_otg *motg)
343{
344 void __iomem *addr;
345
346 if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY) {
347 msm_otg_phy_clk_reset(motg);
348 return;
349 }
350
351 addr = USB_PHY_CTRL;
352 if (motg->phy_number)
353 addr = USB_PHY_CTRL2;
354
355 /* Assert USB PHY_POR */
356 writel(readl(addr) | PHY_POR_ASSERT, addr);
357
358 /*
359 * wait for minimum 10 microseconds as suggested in HPG.
360 * Use a slightly larger value since the exact value didn't
361 * work 100% of the time.
362 */
363 udelay(12);
364
365 /* Deassert USB PHY_POR */
366 writel(readl(addr) & ~PHY_POR_ASSERT, addr);
367}
368
369static int msm_usb_reset(struct usb_phy *phy)
370{
371 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
372 int ret;
373
374 if (!IS_ERR(motg->core_clk))
375 clk_prepare_enable(motg->core_clk);
376
377 ret = msm_link_reset(motg);
378 if (ret) {
379 dev_err(phy->dev, "phy_reset failed\n");
380 return ret;
381 }
382
383 ret = msm_otg_reset(&motg->phy);
384 if (ret) {
385 dev_err(phy->dev, "link reset failed\n");
386 return ret;
387 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530388
389 msleep(100);
390
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +0300391 /* Reset USB PHY after performing USB Link RESET */
392 msm_phy_reset(motg);
393
394 if (!IS_ERR(motg->core_clk))
395 clk_disable_unprepare(motg->core_clk);
396
397 return 0;
398}
399
400static int msm_phy_init(struct usb_phy *phy)
401{
402 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
403 struct msm_otg_platform_data *pdata = motg->pdata;
404 u32 val, ulpi_val = 0;
405
406 /* Program USB PHY Override registers. */
407 ulpi_init(motg);
408
409 /*
410 * It is recommended in HPG to reset USB PHY after programming
411 * USB PHY Override registers.
412 */
413 msm_phy_reset(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530414
415 if (pdata->otg_control == OTG_PHY_CONTROL) {
416 val = readl(USB_OTGSC);
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300417 if (pdata->mode == USB_DR_MODE_OTG) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530418 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
419 val |= OTGSC_IDIE | OTGSC_BSVIE;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300420 } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530421 ulpi_val = ULPI_INT_SESS_VALID;
422 val |= OTGSC_BSVIE;
423 }
424 writel(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200425 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
426 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530427 }
428
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300429 if (motg->phy_number)
430 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
431
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 return 0;
433}
434
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530435#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530436#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
437
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600438#ifdef CONFIG_PM
439
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300440static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600441{
Ivan T. Ivanov01799b62014-04-28 16:34:22 +0300442 int max_vol = motg->vdd_levels[VDD_LEVEL_MAX];
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600443 int min_vol;
444 int ret;
445
446 if (high)
Ivan T. Ivanov01799b62014-04-28 16:34:22 +0300447 min_vol = motg->vdd_levels[VDD_LEVEL_MIN];
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600448 else
Ivan T. Ivanov01799b62014-04-28 16:34:22 +0300449 min_vol = motg->vdd_levels[VDD_LEVEL_NONE];
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600450
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300451 ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600452 if (ret) {
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300453 pr_err("Cannot set vddcx voltage\n");
Josh Cartwrighte7d613d2014-02-18 10:36:29 -0600454 return ret;
455 }
456
457 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
458
459 return ret;
460}
461
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530462static int msm_otg_suspend(struct msm_otg *motg)
463{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200464 struct usb_phy *phy = &motg->phy;
465 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530466 struct msm_otg_platform_data *pdata = motg->pdata;
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300467 void __iomem *addr;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530468 int cnt = 0;
469
470 if (atomic_read(&motg->in_lpm))
471 return 0;
472
473 disable_irq(motg->irq);
474 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530475 * Chipidea 45-nm PHY suspend sequence:
476 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530477 * Interrupt Latch Register auto-clear feature is not present
478 * in all PHY versions. Latch register is clear on read type.
479 * Clear latch register to avoid spurious wakeup from
480 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530481 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530482 * PHY comparators are disabled when PHY enters into low power
483 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
484 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
485 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530486 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530487 * PLL is not turned off when PHY enters into low power mode (LPM).
488 * Disable PLL for maximum power savings.
489 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530490
491 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200492 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530493 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200494 ulpi_write(phy, 0x01, 0x30);
495 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530496 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530497
498 /*
499 * PHY may take some time or even fail to enter into low power
500 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
501 * in failure case.
502 */
503 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
504 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
505 if (readl(USB_PORTSC) & PORTSC_PHCD)
506 break;
507 udelay(1);
508 cnt++;
509 }
510
511 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200512 dev_err(phy->dev, "Unable to suspend PHY\n");
513 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530514 enable_irq(motg->irq);
515 return -ETIMEDOUT;
516 }
517
518 /*
519 * PHY has capability to generate interrupt asynchronously in low
520 * power mode (LPM). This interrupt is level triggered. So USB IRQ
521 * line must be disabled till async interrupt enable bit is cleared
522 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
523 * block data communication from PHY.
524 */
525 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
526
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300527 addr = USB_PHY_CTRL;
528 if (motg->phy_number)
529 addr = USB_PHY_CTRL2;
530
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530531 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
532 motg->pdata->otg_control == OTG_PMIC_CONTROL)
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300533 writel(readl(addr) | PHY_RETEN, addr);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530534
Stephen Boydb99a8f62013-06-17 10:43:10 -0700535 clk_disable_unprepare(motg->pclk);
536 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300537 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700538 clk_disable_unprepare(motg->core_clk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530539
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530540 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
541 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300542 msm_hsusb_ldo_set_mode(motg, 0);
543 msm_hsusb_config_vddcx(motg, 0);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530544 }
545
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200546 if (device_may_wakeup(phy->dev))
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530547 enable_irq_wake(motg->irq);
548 if (bus)
549 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
550
551 atomic_set(&motg->in_lpm, 1);
552 enable_irq(motg->irq);
553
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200554 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530555
556 return 0;
557}
558
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530559static int msm_otg_resume(struct msm_otg *motg)
560{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200561 struct usb_phy *phy = &motg->phy;
562 struct usb_bus *bus = phy->otg->host;
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300563 void __iomem *addr;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530564 int cnt = 0;
565 unsigned temp;
566
567 if (!atomic_read(&motg->in_lpm))
568 return 0;
569
Stephen Boydb99a8f62013-06-17 10:43:10 -0700570 clk_prepare_enable(motg->pclk);
571 clk_prepare_enable(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +0300572 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -0700573 clk_prepare_enable(motg->core_clk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530574
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530575 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
576 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300577
578 addr = USB_PHY_CTRL;
579 if (motg->phy_number)
580 addr = USB_PHY_CTRL2;
581
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +0300582 msm_hsusb_ldo_set_mode(motg, 1);
583 msm_hsusb_config_vddcx(motg, 1);
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +0300584 writel(readl(addr) & ~PHY_RETEN, addr);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530585 }
586
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530587 temp = readl(USB_USBCMD);
588 temp &= ~ASYNC_INTR_CTRL;
589 temp &= ~ULPI_STP_CTRL;
590 writel(temp, USB_USBCMD);
591
592 /*
593 * PHY comes out of low power mode (LPM) in case of wakeup
594 * from asynchronous interrupt.
595 */
596 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
597 goto skip_phy_resume;
598
599 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
600 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
601 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
602 break;
603 udelay(1);
604 cnt++;
605 }
606
607 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
608 /*
609 * This is a fatal error. Reset the link and
610 * PHY. USB state can not be restored. Re-insertion
611 * of USB cable is the only way to get USB working.
612 */
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +0300613 dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200614 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530615 }
616
617skip_phy_resume:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200618 if (device_may_wakeup(phy->dev))
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530619 disable_irq_wake(motg->irq);
620 if (bus)
621 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
622
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530623 atomic_set(&motg->in_lpm, 0);
624
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530625 if (motg->async_int) {
626 motg->async_int = 0;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200627 pm_runtime_put(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530628 enable_irq(motg->irq);
629 }
630
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200631 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530632
633 return 0;
634}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530635#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530636
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530637static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
638{
639 if (motg->cur_power == mA)
640 return;
641
642 /* TODO: Notify PMIC about available current */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200643 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530644 motg->cur_power = mA;
645}
646
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200647static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530648{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200649 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530650
651 /*
652 * Gadget driver uses set_power method to notify about the
653 * available current based on suspend/configured states.
654 *
655 * IDEV_CHG can be drawn irrespective of suspend/un-configured
656 * states when CDP/ACA is connected.
657 */
658 if (motg->chg_type == USB_SDP_CHARGER)
659 msm_otg_notify_charger(motg, mA);
660
661 return 0;
662}
663
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200664static void msm_otg_start_host(struct usb_phy *phy, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530665{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200666 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530667 struct msm_otg_platform_data *pdata = motg->pdata;
668 struct usb_hcd *hcd;
669
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200670 if (!phy->otg->host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530671 return;
672
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200673 hcd = bus_to_hcd(phy->otg->host);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530674
675 if (on) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200676 dev_dbg(phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530677
678 if (pdata->vbus_power)
679 pdata->vbus_power(1);
680 /*
681 * Some boards have a switch cotrolled by gpio
682 * to enable/disable internal HUB. Enable internal
683 * HUB before kicking the host.
684 */
685 if (pdata->setup_gpio)
686 pdata->setup_gpio(OTG_STATE_A_HOST);
687#ifdef CONFIG_USB
688 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Peter Chen3c9740a2013-11-05 10:46:02 +0800689 device_wakeup_enable(hcd->self.controller);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530690#endif
691 } else {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200692 dev_dbg(phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530693
694#ifdef CONFIG_USB
695 usb_remove_hcd(hcd);
696#endif
697 if (pdata->setup_gpio)
698 pdata->setup_gpio(OTG_STATE_UNDEFINED);
699 if (pdata->vbus_power)
700 pdata->vbus_power(0);
701 }
702}
703
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200704static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530705{
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100706 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530707 struct usb_hcd *hcd;
708
709 /*
710 * Fail host registration if this board can support
711 * only peripheral configuration.
712 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300713 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100714 dev_info(otg->usb_phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530715 return -ENODEV;
716 }
717
718 if (!host) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100719 if (otg->state == OTG_STATE_A_HOST) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100720 pm_runtime_get_sync(otg->usb_phy->dev);
721 msm_otg_start_host(otg->usb_phy, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530722 otg->host = NULL;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100723 otg->state = OTG_STATE_UNDEFINED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530724 schedule_work(&motg->sm_work);
725 } else {
726 otg->host = NULL;
727 }
728
729 return 0;
730 }
731
732 hcd = bus_to_hcd(host);
733 hcd->power_budget = motg->pdata->power_budget;
734
735 otg->host = host;
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100736 dev_dbg(otg->usb_phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530737
738 /*
739 * Kick the state machine work, if peripheral is not supported
740 * or peripheral is already registered with us.
741 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300742 if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100743 pm_runtime_get_sync(otg->usb_phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530744 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530745 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530746
747 return 0;
748}
749
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200750static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530751{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200752 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530753 struct msm_otg_platform_data *pdata = motg->pdata;
754
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200755 if (!phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530756 return;
757
758 if (on) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200759 dev_dbg(phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530760 /*
761 * Some boards have a switch cotrolled by gpio
762 * to enable/disable internal HUB. Disable internal
763 * HUB before kicking the gadget.
764 */
765 if (pdata->setup_gpio)
766 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200767 usb_gadget_vbus_connect(phy->otg->gadget);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530768 } else {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200769 dev_dbg(phy->dev, "gadget off\n");
770 usb_gadget_vbus_disconnect(phy->otg->gadget);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530771 if (pdata->setup_gpio)
772 pdata->setup_gpio(OTG_STATE_UNDEFINED);
773 }
774
775}
776
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200777static int msm_otg_set_peripheral(struct usb_otg *otg,
778 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530779{
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100780 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530781
782 /*
783 * Fail peripheral registration if this board can support
784 * only host configuration.
785 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300786 if (motg->pdata->mode == USB_DR_MODE_HOST) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100787 dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530788 return -ENODEV;
789 }
790
791 if (!gadget) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100792 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100793 pm_runtime_get_sync(otg->usb_phy->dev);
794 msm_otg_start_peripheral(otg->usb_phy, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530795 otg->gadget = NULL;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100796 otg->state = OTG_STATE_UNDEFINED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530797 schedule_work(&motg->sm_work);
798 } else {
799 otg->gadget = NULL;
800 }
801
802 return 0;
803 }
804 otg->gadget = gadget;
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100805 dev_dbg(otg->usb_phy->dev,
806 "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530807
808 /*
809 * Kick the state machine work, if host is not supported
810 * or host is already registered with us.
811 */
Ivan T. Ivanov971232c2014-04-28 16:34:11 +0300812 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100813 pm_runtime_get_sync(otg->usb_phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530814 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530815 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530816
817 return 0;
818}
819
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530820static bool msm_chg_check_secondary_det(struct msm_otg *motg)
821{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200822 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530823 u32 chg_det;
824 bool ret = false;
825
826 switch (motg->pdata->phy_type) {
827 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200828 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530829 ret = chg_det & (1 << 4);
830 break;
831 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200832 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530833 ret = chg_det & 1;
834 break;
835 default:
836 break;
837 }
838 return ret;
839}
840
841static void msm_chg_enable_secondary_det(struct msm_otg *motg)
842{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200843 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530844 u32 chg_det;
845
846 switch (motg->pdata->phy_type) {
847 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200848 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530849 /* Turn off charger block */
850 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200851 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530852 udelay(20);
853 /* control chg block via ULPI */
854 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200855 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530856 /* put it in host mode for enabling D- source */
857 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200858 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530859 /* Turn on chg detect block */
860 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200861 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530862 udelay(20);
863 /* enable chg detection */
864 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200865 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530866 break;
867 case SNPS_28NM_INTEGRATED_PHY:
868 /*
869 * Configure DM as current source, DP as current sink
870 * and enable battery charging comparators.
871 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200872 ulpi_write(phy, 0x8, 0x85);
873 ulpi_write(phy, 0x2, 0x85);
874 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530875 break;
876 default:
877 break;
878 }
879}
880
881static bool msm_chg_check_primary_det(struct msm_otg *motg)
882{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200883 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530884 u32 chg_det;
885 bool ret = false;
886
887 switch (motg->pdata->phy_type) {
888 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200889 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530890 ret = chg_det & (1 << 4);
891 break;
892 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200893 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530894 ret = chg_det & 1;
895 break;
896 default:
897 break;
898 }
899 return ret;
900}
901
902static void msm_chg_enable_primary_det(struct msm_otg *motg)
903{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200904 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530905 u32 chg_det;
906
907 switch (motg->pdata->phy_type) {
908 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200909 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530910 /* enable chg detection */
911 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200912 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530913 break;
914 case SNPS_28NM_INTEGRATED_PHY:
915 /*
916 * Configure DP as current source, DM as current sink
917 * and enable battery charging comparators.
918 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200919 ulpi_write(phy, 0x2, 0x85);
920 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530921 break;
922 default:
923 break;
924 }
925}
926
927static bool msm_chg_check_dcd(struct msm_otg *motg)
928{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200929 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530930 u32 line_state;
931 bool ret = false;
932
933 switch (motg->pdata->phy_type) {
934 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200935 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530936 ret = !(line_state & 1);
937 break;
938 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200939 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530940 ret = line_state & 2;
941 break;
942 default:
943 break;
944 }
945 return ret;
946}
947
948static void msm_chg_disable_dcd(struct msm_otg *motg)
949{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200950 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530951 u32 chg_det;
952
953 switch (motg->pdata->phy_type) {
954 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200955 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530956 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200957 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530958 break;
959 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200960 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530961 break;
962 default:
963 break;
964 }
965}
966
967static void msm_chg_enable_dcd(struct msm_otg *motg)
968{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200969 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530970 u32 chg_det;
971
972 switch (motg->pdata->phy_type) {
973 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200974 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530975 /* Turn on D+ current source */
976 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200977 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530978 break;
979 case SNPS_28NM_INTEGRATED_PHY:
980 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200981 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530982 break;
983 default:
984 break;
985 }
986}
987
988static void msm_chg_block_on(struct msm_otg *motg)
989{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200990 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530991 u32 func_ctrl, chg_det;
992
993 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200994 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530995 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
996 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200997 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530998
999 switch (motg->pdata->phy_type) {
1000 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001001 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301002 /* control chg block via ULPI */
1003 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001004 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301005 /* Turn on chg detect block */
1006 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001007 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301008 udelay(20);
1009 break;
1010 case SNPS_28NM_INTEGRATED_PHY:
1011 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001012 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301013 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001014 ulpi_write(phy, 0x1F, 0x92);
1015 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301016 udelay(100);
1017 break;
1018 default:
1019 break;
1020 }
1021}
1022
1023static void msm_chg_block_off(struct msm_otg *motg)
1024{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001025 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301026 u32 func_ctrl, chg_det;
1027
1028 switch (motg->pdata->phy_type) {
1029 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001030 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301031 /* Turn off charger block */
1032 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001033 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301034 break;
1035 case SNPS_28NM_INTEGRATED_PHY:
1036 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001037 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301038 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001039 ulpi_write(phy, 0x1F, 0x92);
1040 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301041 break;
1042 default:
1043 break;
1044 }
1045
1046 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001047 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301048 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1049 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001050 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301051}
1052
1053#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1054#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1055#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1056#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1057static void msm_chg_detect_work(struct work_struct *w)
1058{
1059 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001060 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301061 bool is_dcd, tmout, vout;
1062 unsigned long delay;
1063
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001064 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301065 switch (motg->chg_state) {
1066 case USB_CHG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001067 pm_runtime_get_sync(phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301068 msm_chg_block_on(motg);
1069 msm_chg_enable_dcd(motg);
1070 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1071 motg->dcd_retries = 0;
1072 delay = MSM_CHG_DCD_POLL_TIME;
1073 break;
1074 case USB_CHG_STATE_WAIT_FOR_DCD:
1075 is_dcd = msm_chg_check_dcd(motg);
1076 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1077 if (is_dcd || tmout) {
1078 msm_chg_disable_dcd(motg);
1079 msm_chg_enable_primary_det(motg);
1080 delay = MSM_CHG_PRIMARY_DET_TIME;
1081 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1082 } else {
1083 delay = MSM_CHG_DCD_POLL_TIME;
1084 }
1085 break;
1086 case USB_CHG_STATE_DCD_DONE:
1087 vout = msm_chg_check_primary_det(motg);
1088 if (vout) {
1089 msm_chg_enable_secondary_det(motg);
1090 delay = MSM_CHG_SECONDARY_DET_TIME;
1091 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1092 } else {
1093 motg->chg_type = USB_SDP_CHARGER;
1094 motg->chg_state = USB_CHG_STATE_DETECTED;
1095 delay = 0;
1096 }
1097 break;
1098 case USB_CHG_STATE_PRIMARY_DONE:
1099 vout = msm_chg_check_secondary_det(motg);
1100 if (vout)
1101 motg->chg_type = USB_DCP_CHARGER;
1102 else
1103 motg->chg_type = USB_CDP_CHARGER;
1104 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1105 /* fall through */
1106 case USB_CHG_STATE_SECONDARY_DONE:
1107 motg->chg_state = USB_CHG_STATE_DETECTED;
1108 case USB_CHG_STATE_DETECTED:
1109 msm_chg_block_off(motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001110 dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301111 schedule_work(&motg->sm_work);
1112 return;
1113 default:
1114 return;
1115 }
1116
1117 schedule_delayed_work(&motg->chg_work, delay);
1118}
1119
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301120/*
1121 * We support OTG, Peripheral only and Host only configurations. In case
1122 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1123 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1124 * enabled when switch is controlled by user and default mode is supplied
1125 * by board file, which can be changed by userspace later.
1126 */
1127static void msm_otg_init_sm(struct msm_otg *motg)
1128{
1129 struct msm_otg_platform_data *pdata = motg->pdata;
1130 u32 otgsc = readl(USB_OTGSC);
1131
1132 switch (pdata->mode) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001133 case USB_DR_MODE_OTG:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301134 if (pdata->otg_control == OTG_PHY_CONTROL) {
1135 if (otgsc & OTGSC_ID)
1136 set_bit(ID, &motg->inputs);
1137 else
1138 clear_bit(ID, &motg->inputs);
1139
1140 if (otgsc & OTGSC_BSV)
1141 set_bit(B_SESS_VLD, &motg->inputs);
1142 else
1143 clear_bit(B_SESS_VLD, &motg->inputs);
1144 } else if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301145 set_bit(ID, &motg->inputs);
1146 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301147 }
1148 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001149 case USB_DR_MODE_HOST:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301150 clear_bit(ID, &motg->inputs);
1151 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001152 case USB_DR_MODE_PERIPHERAL:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301153 set_bit(ID, &motg->inputs);
1154 if (otgsc & OTGSC_BSV)
1155 set_bit(B_SESS_VLD, &motg->inputs);
1156 else
1157 clear_bit(B_SESS_VLD, &motg->inputs);
1158 break;
1159 default:
1160 break;
1161 }
1162}
1163
1164static void msm_otg_sm_work(struct work_struct *w)
1165{
1166 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001167 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301168
Antoine Tenarte47d9252014-10-30 18:41:13 +01001169 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301170 case OTG_STATE_UNDEFINED:
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001171 dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
1172 msm_otg_reset(otg->usb_phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301173 msm_otg_init_sm(motg);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001174 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301175 /* FALL THROUGH */
1176 case OTG_STATE_B_IDLE:
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001177 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301178 if (!test_bit(ID, &motg->inputs) && otg->host) {
1179 /* disable BSV bit */
1180 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001181 msm_otg_start_host(otg->usb_phy, 1);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001182 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301183 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1184 switch (motg->chg_state) {
1185 case USB_CHG_STATE_UNDEFINED:
1186 msm_chg_detect_work(&motg->chg_work.work);
1187 break;
1188 case USB_CHG_STATE_DETECTED:
1189 switch (motg->chg_type) {
1190 case USB_DCP_CHARGER:
1191 msm_otg_notify_charger(motg,
1192 IDEV_CHG_MAX);
1193 break;
1194 case USB_CDP_CHARGER:
1195 msm_otg_notify_charger(motg,
1196 IDEV_CHG_MAX);
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001197 msm_otg_start_peripheral(otg->usb_phy,
1198 1);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001199 otg->state
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001200 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301201 break;
1202 case USB_SDP_CHARGER:
1203 msm_otg_notify_charger(motg, IUNIT);
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001204 msm_otg_start_peripheral(otg->usb_phy,
1205 1);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001206 otg->state
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001207 = OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301208 break;
1209 default:
1210 break;
1211 }
1212 break;
1213 default:
1214 break;
1215 }
1216 } else {
1217 /*
1218 * If charger detection work is pending, decrement
1219 * the pm usage counter to balance with the one that
1220 * is incremented in charger detection work.
1221 */
1222 if (cancel_delayed_work_sync(&motg->chg_work)) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001223 pm_runtime_put_sync(otg->usb_phy->dev);
1224 msm_otg_reset(otg->usb_phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301225 }
1226 msm_otg_notify_charger(motg, 0);
1227 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1228 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301229 }
Srinivas Kandagatla508ccea2014-06-30 18:29:57 +01001230
Antoine Tenarte47d9252014-10-30 18:41:13 +01001231 if (otg->state == OTG_STATE_B_IDLE)
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001232 pm_runtime_put_sync(otg->usb_phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301233 break;
1234 case OTG_STATE_B_PERIPHERAL:
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001235 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301236 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1237 !test_bit(ID, &motg->inputs)) {
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301238 msm_otg_notify_charger(motg, 0);
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001239 msm_otg_start_peripheral(otg->usb_phy, 0);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301240 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1241 motg->chg_type = USB_INVALID_CHARGER;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001242 otg->state = OTG_STATE_B_IDLE;
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001243 msm_otg_reset(otg->usb_phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301244 schedule_work(w);
1245 }
1246 break;
1247 case OTG_STATE_A_HOST:
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001248 dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301249 if (test_bit(ID, &motg->inputs)) {
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001250 msm_otg_start_host(otg->usb_phy, 0);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001251 otg->state = OTG_STATE_B_IDLE;
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001252 msm_otg_reset(otg->usb_phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301253 schedule_work(w);
1254 }
1255 break;
1256 default:
1257 break;
1258 }
1259}
1260
1261static irqreturn_t msm_otg_irq(int irq, void *data)
1262{
1263 struct msm_otg *motg = data;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001264 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301265 u32 otgsc = 0;
1266
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301267 if (atomic_read(&motg->in_lpm)) {
1268 disable_irq_nosync(irq);
1269 motg->async_int = 1;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001270 pm_runtime_get(phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301271 return IRQ_HANDLED;
1272 }
1273
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301274 otgsc = readl(USB_OTGSC);
1275 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1276 return IRQ_NONE;
1277
1278 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1279 if (otgsc & OTGSC_ID)
1280 set_bit(ID, &motg->inputs);
1281 else
1282 clear_bit(ID, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001283 dev_dbg(phy->dev, "ID set/clear\n");
1284 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301285 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1286 if (otgsc & OTGSC_BSV)
1287 set_bit(B_SESS_VLD, &motg->inputs);
1288 else
1289 clear_bit(B_SESS_VLD, &motg->inputs);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001290 dev_dbg(phy->dev, "BSV set/clear\n");
1291 pm_runtime_get_noresume(phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301292 }
1293
1294 writel(otgsc, USB_OTGSC);
1295 schedule_work(&motg->sm_work);
1296 return IRQ_HANDLED;
1297}
1298
1299static int msm_otg_mode_show(struct seq_file *s, void *unused)
1300{
1301 struct msm_otg *motg = s->private;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001302 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301303
Antoine Tenarte47d9252014-10-30 18:41:13 +01001304 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301305 case OTG_STATE_A_HOST:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001306 seq_puts(s, "host\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301307 break;
1308 case OTG_STATE_B_PERIPHERAL:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001309 seq_puts(s, "peripheral\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301310 break;
1311 default:
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001312 seq_puts(s, "none\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301313 break;
1314 }
1315
1316 return 0;
1317}
1318
1319static int msm_otg_mode_open(struct inode *inode, struct file *file)
1320{
1321 return single_open(file, msm_otg_mode_show, inode->i_private);
1322}
1323
1324static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1325 size_t count, loff_t *ppos)
1326{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301327 struct seq_file *s = file->private_data;
1328 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329 char buf[16];
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001330 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301331 int status = count;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001332 enum usb_dr_mode req_mode;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301333
1334 memset(buf, 0x00, sizeof(buf));
1335
1336 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1337 status = -EFAULT;
1338 goto out;
1339 }
1340
1341 if (!strncmp(buf, "host", 4)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001342 req_mode = USB_DR_MODE_HOST;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301343 } else if (!strncmp(buf, "peripheral", 10)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001344 req_mode = USB_DR_MODE_PERIPHERAL;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301345 } else if (!strncmp(buf, "none", 4)) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001346 req_mode = USB_DR_MODE_UNKNOWN;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301347 } else {
1348 status = -EINVAL;
1349 goto out;
1350 }
1351
1352 switch (req_mode) {
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001353 case USB_DR_MODE_UNKNOWN:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001354 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301355 case OTG_STATE_A_HOST:
1356 case OTG_STATE_B_PERIPHERAL:
1357 set_bit(ID, &motg->inputs);
1358 clear_bit(B_SESS_VLD, &motg->inputs);
1359 break;
1360 default:
1361 goto out;
1362 }
1363 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001364 case USB_DR_MODE_PERIPHERAL:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001365 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301366 case OTG_STATE_B_IDLE:
1367 case OTG_STATE_A_HOST:
1368 set_bit(ID, &motg->inputs);
1369 set_bit(B_SESS_VLD, &motg->inputs);
1370 break;
1371 default:
1372 goto out;
1373 }
1374 break;
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001375 case USB_DR_MODE_HOST:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001376 switch (otg->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301377 case OTG_STATE_B_IDLE:
1378 case OTG_STATE_B_PERIPHERAL:
1379 clear_bit(ID, &motg->inputs);
1380 break;
1381 default:
1382 goto out;
1383 }
1384 break;
1385 default:
1386 goto out;
1387 }
1388
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001389 pm_runtime_get_sync(otg->usb_phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301390 schedule_work(&motg->sm_work);
1391out:
1392 return status;
1393}
1394
Felipe Balbi8f90afd2014-08-20 13:38:18 -05001395static const struct file_operations msm_otg_mode_fops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301396 .open = msm_otg_mode_open,
1397 .read = seq_read,
1398 .write = msm_otg_mode_write,
1399 .llseek = seq_lseek,
1400 .release = single_release,
1401};
1402
1403static struct dentry *msm_otg_dbg_root;
1404static struct dentry *msm_otg_dbg_mode;
1405
1406static int msm_otg_debugfs_init(struct msm_otg *motg)
1407{
1408 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1409
1410 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1411 return -ENODEV;
1412
1413 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1414 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1415 if (!msm_otg_dbg_mode) {
1416 debugfs_remove(msm_otg_dbg_root);
1417 msm_otg_dbg_root = NULL;
1418 return -ENODEV;
1419 }
1420
1421 return 0;
1422}
1423
1424static void msm_otg_debugfs_cleanup(void)
1425{
1426 debugfs_remove(msm_otg_dbg_mode);
1427 debugfs_remove(msm_otg_dbg_root);
1428}
1429
Jingoo Han492240b2014-06-18 13:42:44 +09001430static const struct of_device_id msm_otg_dt_match[] = {
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001431 {
1432 .compatible = "qcom,usb-otg-ci",
1433 .data = (void *) CI_45NM_INTEGRATED_PHY
1434 },
1435 {
1436 .compatible = "qcom,usb-otg-snps",
1437 .data = (void *) SNPS_28NM_INTEGRATED_PHY
1438 },
1439 { }
1440};
1441MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
1442
1443static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
1444{
1445 struct msm_otg_platform_data *pdata;
1446 const struct of_device_id *id;
1447 struct device_node *node = pdev->dev.of_node;
1448 struct property *prop;
1449 int len, ret, words;
Ivan T. Ivanov01799b62014-04-28 16:34:22 +03001450 u32 val, tmp[3];
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001451
1452 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1453 if (!pdata)
1454 return -ENOMEM;
1455
1456 motg->pdata = pdata;
1457
1458 id = of_match_device(msm_otg_dt_match, &pdev->dev);
Felipe Balbib3025e62014-04-30 11:33:04 -05001459 pdata->phy_type = (enum msm_usb_phy_type) id->data;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001460
Ivan T. Ivanova2734542014-04-28 16:34:16 +03001461 motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
1462 if (IS_ERR(motg->link_rst))
1463 return PTR_ERR(motg->link_rst);
1464
1465 motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
1466 if (IS_ERR(motg->phy_rst))
Srinivas Kandagatlae44f1f42014-06-30 18:29:49 +01001467 motg->phy_rst = NULL;
Ivan T. Ivanova2734542014-04-28 16:34:16 +03001468
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001469 pdata->mode = of_usb_get_dr_mode(node);
1470 if (pdata->mode == USB_DR_MODE_UNKNOWN)
1471 pdata->mode = USB_DR_MODE_OTG;
1472
1473 pdata->otg_control = OTG_PHY_CONTROL;
1474 if (!of_property_read_u32(node, "qcom,otg-control", &val))
1475 if (val == OTG_PMIC_CONTROL)
1476 pdata->otg_control = val;
1477
Ivan T. Ivanovcfa3ff52014-04-28 16:34:17 +03001478 if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
1479 motg->phy_number = val;
1480
Ivan T. Ivanov01799b62014-04-28 16:34:22 +03001481 motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
1482 motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
1483 motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
1484
1485 if (of_get_property(node, "qcom,vdd-levels", &len) &&
1486 len == sizeof(tmp)) {
1487 of_property_read_u32_array(node, "qcom,vdd-levels",
1488 tmp, len / sizeof(*tmp));
1489 motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
1490 motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
1491 motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
1492 }
1493
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001494 prop = of_find_property(node, "qcom,phy-init-sequence", &len);
1495 if (!prop || !len)
1496 return 0;
1497
1498 words = len / sizeof(u32);
1499
1500 if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
1501 dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
1502 return 0;
1503 }
1504
1505 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
Peter Chen9da22202014-10-14 15:56:17 +08001506 if (!pdata->phy_init_seq)
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001507 return 0;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001508
1509 ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
1510 pdata->phy_init_seq, words);
1511 if (!ret)
1512 pdata->phy_init_sz = words;
1513
1514 return 0;
1515}
1516
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001517static int msm_otg_probe(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301518{
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001519 struct regulator_bulk_data regs[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301520 int ret = 0;
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001521 struct device_node *np = pdev->dev.of_node;
1522 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301523 struct resource *res;
1524 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001525 struct usb_phy *phy;
Tim Bird30bf8662014-04-28 16:34:20 +03001526 void __iomem *phy_select;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301527
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001528 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Peter Chen9da22202014-10-14 15:56:17 +08001529 if (!motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301530 return -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301531
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001532 pdata = dev_get_platdata(&pdev->dev);
1533 if (!pdata) {
1534 if (!np)
1535 return -ENXIO;
1536 ret = msm_otg_read_dt(pdev, motg);
1537 if (ret)
1538 return ret;
1539 }
1540
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001541 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1542 GFP_KERNEL);
Peter Chen9da22202014-10-14 15:56:17 +08001543 if (!motg->phy.otg)
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001544 return -ENOMEM;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001545
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001546 phy = &motg->phy;
1547 phy->dev = &pdev->dev;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301548
Srinivas Kandagatlaa1a4caf2014-08-21 07:45:10 +01001549 if (motg->pdata->phy_clk_reset) {
1550 motg->phy_reset_clk = devm_clk_get(&pdev->dev,
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001551 np ? "phy" : "usb_phy_clk");
Srinivas Kandagatlaa1a4caf2014-08-21 07:45:10 +01001552
1553 if (IS_ERR(motg->phy_reset_clk)) {
1554 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
1555 return PTR_ERR(motg->phy_reset_clk);
1556 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301557 }
1558
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001559 motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301560 if (IS_ERR(motg->clk)) {
1561 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001562 return PTR_ERR(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301563 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05301564
1565 /*
1566 * If USB Core is running its protocol engine based on CORE CLK,
1567 * CORE CLK must be running at >55Mhz for correct HSUSB
1568 * operation and USB core cannot tolerate frequency changes on
Ivan T. Ivanovff0e4a62014-04-28 16:34:12 +03001569 * CORE CLK.
Anji jonnala0f73cac2011-05-04 10:19:46 +05301570 */
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001571 motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301572 if (IS_ERR(motg->pclk)) {
1573 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001574 return PTR_ERR(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301575 }
1576
1577 /*
1578 * USB core clock is not present on all MSM chips. This
1579 * clock is introduced to remove the dependency on AXI
1580 * bus frequency.
1581 */
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001582 motg->core_clk = devm_clk_get(&pdev->dev,
1583 np ? "alt_core" : "usb_hs_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301584
1585 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter2ea7b142014-05-19 23:35:19 +03001586 if (!res)
1587 return -EINVAL;
1588 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1589 if (!motg->regs)
1590 return -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301591
Tim Bird30bf8662014-04-28 16:34:20 +03001592 /*
1593 * NOTE: The PHYs can be multiplexed between the chipidea controller
1594 * and the dwc3 controller, using a single bit. It is important that
1595 * the dwc3 driver does not set this bit in an incompatible way.
1596 */
1597 if (motg->phy_number) {
1598 phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
Wei Yongjun716d28e2014-07-20 11:40:37 +08001599 if (!phy_select)
1600 return -ENOMEM;
Tim Bird30bf8662014-04-28 16:34:20 +03001601 /* Enable second PHY with the OTG port */
Felipe Balbi24597492014-04-30 11:35:22 -05001602 writel(0x1, phy_select);
Tim Bird30bf8662014-04-28 16:34:20 +03001603 }
1604
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301605 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1606
1607 motg->irq = platform_get_irq(pdev, 0);
Ivan T. Ivanovf60c1142014-04-28 16:34:14 +03001608 if (motg->irq < 0) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301609 dev_err(&pdev->dev, "platform_get_irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001610 return motg->irq;
1611 }
1612
Ivan T. Ivanovf5ef2372014-04-28 16:34:13 +03001613 regs[0].supply = "vddcx";
1614 regs[1].supply = "v3p3";
1615 regs[2].supply = "v1p8";
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001616
1617 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1618 if (ret)
1619 return ret;
1620
1621 motg->vddcx = regs[0].consumer;
1622 motg->v3p3 = regs[1].consumer;
1623 motg->v1p8 = regs[2].consumer;
1624
1625 clk_set_rate(motg->clk, 60000000);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301626
Stephen Boydb99a8f62013-06-17 10:43:10 -07001627 clk_prepare_enable(motg->clk);
1628 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301629
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001630 if (!IS_ERR(motg->core_clk))
1631 clk_prepare_enable(motg->core_clk);
1632
Anji jonnala11aa5c42011-05-04 10:19:48 +05301633 ret = msm_hsusb_init_vddcx(motg, 1);
1634 if (ret) {
1635 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001636 goto disable_clks;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301637 }
1638
1639 ret = msm_hsusb_ldo_init(motg, 1);
1640 if (ret) {
1641 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001642 goto disable_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301643 }
Ivan T. Ivanov37cfdaf2014-04-28 16:34:06 +03001644 ret = msm_hsusb_ldo_set_mode(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301645 if (ret) {
1646 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001647 goto disable_ldo;
Anji jonnala11aa5c42011-05-04 10:19:48 +05301648 }
1649
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301650 writel(0, USB_USBINTR);
1651 writel(0, USB_OTGSC);
1652
1653 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301654 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001655 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301656 "msm_otg", motg);
1657 if (ret) {
1658 dev_err(&pdev->dev, "request irq failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001659 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301660 }
1661
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +03001662 phy->init = msm_phy_init;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001663 phy->set_power = msm_otg_set_power;
Ivan T. Ivanov349907c2014-04-28 16:34:21 +03001664 phy->notify_disconnect = msm_phy_notify_disconnect;
Ivan T. Ivanove695abb2014-04-28 16:34:23 +03001665 phy->type = USB_PHY_TYPE_USB2;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301666
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001667 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301668
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001669 phy->otg->usb_phy = &motg->phy;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001670 phy->otg->set_host = msm_otg_set_host;
1671 phy->otg->set_peripheral = msm_otg_set_peripheral;
1672
Ivan T. Ivanovd69c6f52014-04-28 16:34:18 +03001673 msm_usb_reset(phy);
1674
Ivan T. Ivanove695abb2014-04-28 16:34:23 +03001675 ret = usb_add_phy_dev(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301676 if (ret) {
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301677 dev_err(&pdev->dev, "usb_add_phy failed\n");
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001678 goto disable_ldo;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301679 }
1680
1681 platform_set_drvdata(pdev, motg);
1682 device_init_wakeup(&pdev->dev, 1);
1683
Ivan T. Ivanov971232c2014-04-28 16:34:11 +03001684 if (motg->pdata->mode == USB_DR_MODE_OTG &&
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001685 motg->pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301686 ret = msm_otg_debugfs_init(motg);
1687 if (ret)
Ivan T. Ivanov3aca0fa2014-04-28 16:34:10 +03001688 dev_dbg(&pdev->dev, "Can not create mode change file\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301689 }
1690
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301691 pm_runtime_set_active(&pdev->dev);
1692 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301693
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301694 return 0;
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001695
1696disable_ldo:
1697 msm_hsusb_ldo_init(motg, 0);
1698disable_vddcx:
1699 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301700disable_clks:
Stephen Boydb99a8f62013-06-17 10:43:10 -07001701 clk_disable_unprepare(motg->pclk);
1702 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001703 if (!IS_ERR(motg->core_clk))
1704 clk_disable_unprepare(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301705 return ret;
1706}
1707
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001708static int msm_otg_remove(struct platform_device *pdev)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301709{
1710 struct msm_otg *motg = platform_get_drvdata(pdev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001711 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301712 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301713
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001714 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301715 return -EBUSY;
1716
1717 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301718 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301719 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301720
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301721 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301722
1723 device_init_wakeup(&pdev->dev, 0);
1724 pm_runtime_disable(&pdev->dev);
1725
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301726 usb_remove_phy(phy);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001727 disable_irq(motg->irq);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301728
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301729 /*
1730 * Put PHY in low power mode.
1731 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001732 ulpi_read(phy, 0x14);
1733 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301734
1735 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1736 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1737 if (readl(USB_PORTSC) & PORTSC_PHCD)
1738 break;
1739 udelay(1);
1740 cnt++;
1741 }
1742 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001743 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301744
Stephen Boydb99a8f62013-06-17 10:43:10 -07001745 clk_disable_unprepare(motg->pclk);
1746 clk_disable_unprepare(motg->clk);
Ivan T. Ivanov6b99c68e2014-04-28 16:34:08 +03001747 if (!IS_ERR(motg->core_clk))
Stephen Boydb99a8f62013-06-17 10:43:10 -07001748 clk_disable_unprepare(motg->core_clk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301749 msm_hsusb_ldo_init(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301750
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301751 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301752
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301753 return 0;
1754}
1755
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001756#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301757static int msm_otg_runtime_idle(struct device *dev)
1758{
1759 struct msm_otg *motg = dev_get_drvdata(dev);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001760 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301761
1762 dev_dbg(dev, "OTG runtime idle\n");
1763
1764 /*
1765 * It is observed some times that a spurious interrupt
1766 * comes when PHY is put into LPM immediately after PHY reset.
1767 * This 1 sec delay also prevents entering into LPM immediately
1768 * after asynchronous interrupt.
1769 */
Antoine Tenarte47d9252014-10-30 18:41:13 +01001770 if (otg->state != OTG_STATE_UNDEFINED)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301771 pm_schedule_suspend(dev, 1000);
1772
1773 return -EAGAIN;
1774}
1775
1776static int msm_otg_runtime_suspend(struct device *dev)
1777{
1778 struct msm_otg *motg = dev_get_drvdata(dev);
1779
1780 dev_dbg(dev, "OTG runtime suspend\n");
1781 return msm_otg_suspend(motg);
1782}
1783
1784static int msm_otg_runtime_resume(struct device *dev)
1785{
1786 struct msm_otg *motg = dev_get_drvdata(dev);
1787
1788 dev_dbg(dev, "OTG runtime resume\n");
1789 return msm_otg_resume(motg);
1790}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301791#endif
1792
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301793#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301794static int msm_otg_pm_suspend(struct device *dev)
1795{
1796 struct msm_otg *motg = dev_get_drvdata(dev);
1797
1798 dev_dbg(dev, "OTG PM suspend\n");
1799 return msm_otg_suspend(motg);
1800}
1801
1802static int msm_otg_pm_resume(struct device *dev)
1803{
1804 struct msm_otg *motg = dev_get_drvdata(dev);
1805 int ret;
1806
1807 dev_dbg(dev, "OTG PM resume\n");
1808
1809 ret = msm_otg_resume(motg);
1810 if (ret)
1811 return ret;
1812
1813 /*
1814 * Runtime PM Documentation recommends bringing the
1815 * device to full powered state upon resume.
1816 */
1817 pm_runtime_disable(dev);
1818 pm_runtime_set_active(dev);
1819 pm_runtime_enable(dev);
1820
1821 return 0;
1822}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301823#endif
1824
1825static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301826 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
1827 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
1828 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301829};
1830
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301831static struct platform_driver msm_otg_driver = {
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001832 .probe = msm_otg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001833 .remove = msm_otg_remove,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301834 .driver = {
1835 .name = DRIVER_NAME,
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301836 .pm = &msm_otg_dev_pm_ops,
Ivan T. Ivanov8364f9a2014-04-28 16:34:15 +03001837 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301838 },
1839};
1840
Ivan T. Ivanov06a6ec42014-04-28 16:34:07 +03001841module_platform_driver(msm_otg_driver);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301842
1843MODULE_LICENSE("GPL v2");
1844MODULE_DESCRIPTION("MSM USB transceiver driver");