blob: 60d7119360b2bc04ed2cbae21b305eb976a2f02c [file] [log] [blame]
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002 *
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 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
Manu Gautamb5067272012-07-02 09:53:41 +053019#include <linux/pm_runtime.h>
Manu Gautam377821c2012-09-28 16:53:24 +053020#include <linux/ratelimit.h>
Manu Gautamb5067272012-07-02 09:53:41 +053021#include <linux/interrupt.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020022#include <linux/ioport.h>
Manu Gautam1742db22012-06-19 13:33:24 +053023#include <linux/clk.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020024#include <linux/io.h>
25#include <linux/module.h>
26#include <linux/types.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020027#include <linux/delay.h>
28#include <linux/of.h>
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +053029#include <linux/of_platform.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030030#include <linux/list.h>
Manu Gautamb5067272012-07-02 09:53:41 +053031#include <linux/debugfs.h>
32#include <linux/uaccess.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030033#include <linux/usb/ch9.h>
34#include <linux/usb/gadget.h>
David Keitelad4a0282013-03-19 18:04:27 -070035#include <linux/qpnp-misc.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030036#include <linux/usb/msm_hsusb.h>
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +053037#include <linux/usb/msm_ext_chg.h>
Manu Gautam60e01352012-05-29 09:00:34 +053038#include <linux/regulator/consumer.h>
Jack Pham924cbe872013-07-10 16:40:55 -070039#include <linux/pm_wakeup.h>
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +053040#include <linux/power_supply.h>
Jack Pham0fc12332012-11-19 13:14:22 -080041#include <linux/qpnp/qpnp-adc.h>
Pavankumar Kondeti08693e72013-05-03 11:55:48 +053042#include <linux/cdev.h>
43#include <linux/completion.h>
Manu Gautam60e01352012-05-29 09:00:34 +053044
45#include <mach/rpm-regulator.h>
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +053046#include <mach/rpm-regulator-smd.h>
Manu Gautam2617deb2012-08-31 17:50:06 -070047#include <mach/msm_bus.h>
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053048#include <mach/clk.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030049
Manu Gautam8c642812012-06-07 10:35:10 +053050#include "dwc3_otg.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030051#include "core.h"
52#include "gadget.h"
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +053053#include "debug.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030054
Jack Pham0fc12332012-11-19 13:14:22 -080055/* ADC threshold values */
56static int adc_low_threshold = 700;
57module_param(adc_low_threshold, int, S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(adc_low_threshold, "ADC ID Low voltage threshold");
59
60static int adc_high_threshold = 950;
61module_param(adc_high_threshold, int, S_IRUGO | S_IWUSR);
62MODULE_PARM_DESC(adc_high_threshold, "ADC ID High voltage threshold");
63
64static int adc_meas_interval = ADC_MEAS1_INTERVAL_1S;
65module_param(adc_meas_interval, int, S_IRUGO | S_IWUSR);
66MODULE_PARM_DESC(adc_meas_interval, "ADC ID polling period");
67
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +053068static int override_phy_init;
69module_param(override_phy_init, int, S_IRUGO|S_IWUSR);
70MODULE_PARM_DESC(override_phy_init, "Override HSPHY Init Seq");
71
Jack Pham9b4606b2013-04-02 17:32:25 -070072/* Enable Proprietary charger detection */
73static bool prop_chg_detect;
74module_param(prop_chg_detect, bool, S_IRUGO | S_IWUSR);
75MODULE_PARM_DESC(prop_chg_detect, "Enable Proprietary charger detection");
76
Ido Shayevitz9fb83452012-04-01 17:45:58 +030077/**
78 * USB DBM Hardware registers.
79 *
80 */
Shimrit Malichia00d7322012-08-05 13:56:28 +030081#define DBM_BASE 0x000F8000
82#define DBM_EP_CFG(n) (DBM_BASE + (0x00 + 4 * (n)))
83#define DBM_DATA_FIFO(n) (DBM_BASE + (0x10 + 4 * (n)))
84#define DBM_DATA_FIFO_SIZE(n) (DBM_BASE + (0x20 + 4 * (n)))
85#define DBM_DATA_FIFO_EN (DBM_BASE + (0x30))
86#define DBM_GEVNTADR (DBM_BASE + (0x34))
87#define DBM_GEVNTSIZ (DBM_BASE + (0x38))
88#define DBM_DBG_CNFG (DBM_BASE + (0x3C))
89#define DBM_HW_TRB0_EP(n) (DBM_BASE + (0x40 + 4 * (n)))
90#define DBM_HW_TRB1_EP(n) (DBM_BASE + (0x50 + 4 * (n)))
91#define DBM_HW_TRB2_EP(n) (DBM_BASE + (0x60 + 4 * (n)))
92#define DBM_HW_TRB3_EP(n) (DBM_BASE + (0x70 + 4 * (n)))
93#define DBM_PIPE_CFG (DBM_BASE + (0x80))
94#define DBM_SOFT_RESET (DBM_BASE + (0x84))
95#define DBM_GEN_CFG (DBM_BASE + (0x88))
Ido Shayevitz9fb83452012-04-01 17:45:58 +030096
97/**
98 * USB DBM Hardware registers bitmask.
99 *
100 */
101/* DBM_EP_CFG */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300102#define DBM_EN_EP 0x00000001
103#define USB3_EPNUM 0x0000003E
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300104#define DBM_BAM_PIPE_NUM 0x000000C0
105#define DBM_PRODUCER 0x00000100
106#define DBM_DISABLE_WB 0x00000200
107#define DBM_INT_RAM_ACC 0x00000400
108
109/* DBM_DATA_FIFO_SIZE */
110#define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff
111
112/* DBM_GEVNTSIZ */
113#define DBM_GEVNTSIZ_MASK 0x0000ffff
114
115/* DBM_DBG_CNFG */
116#define DBM_ENABLE_IOC_MASK 0x0000000f
117
118/* DBM_SOFT_RESET */
119#define DBM_SFT_RST_EP0 0x00000001
120#define DBM_SFT_RST_EP1 0x00000002
121#define DBM_SFT_RST_EP2 0x00000004
122#define DBM_SFT_RST_EP3 0x00000008
Shimrit Malichia00d7322012-08-05 13:56:28 +0300123#define DBM_SFT_RST_EPS_MASK 0x0000000F
124#define DBM_SFT_RST_MASK 0x80000000
125#define DBM_EN_MASK 0x00000002
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200126
127#define DBM_MAX_EPS 4
128
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300129/* DBM TRB configurations */
130#define DBM_TRB_BIT 0x80000000
131#define DBM_TRB_DATA_SRC 0x40000000
132#define DBM_TRB_DMA 0x20000000
133#define DBM_TRB_EP_NUM(ep) (ep<<24)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300134
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +0530135#define USB3_PORTSC (0x430)
136#define PORT_PE (0x1 << 1)
Manu Gautam8c642812012-06-07 10:35:10 +0530137/**
138 * USB QSCRATCH Hardware registers
139 *
140 */
141#define QSCRATCH_REG_OFFSET (0x000F8800)
Pavankumar Kondeti08693e72013-05-03 11:55:48 +0530142#define QSCRATCH_CTRL_REG (QSCRATCH_REG_OFFSET + 0x04)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300143#define QSCRATCH_GENERAL_CFG (QSCRATCH_REG_OFFSET + 0x08)
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +0530144#define QSCRATCH_RAM1_REG (QSCRATCH_REG_OFFSET + 0x0C)
Manu Gautambd0e5782012-08-30 10:39:01 -0700145#define HS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x10)
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530146#define PARAMETER_OVERRIDE_X_REG (QSCRATCH_REG_OFFSET + 0x14)
Manu Gautam8c642812012-06-07 10:35:10 +0530147#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
148#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
149#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
150#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
Manu Gautamd4108b72012-12-14 17:35:18 +0530151#define CGCTL_REG (QSCRATCH_REG_OFFSET + 0x28)
Manu Gautambd0e5782012-08-30 10:39:01 -0700152#define SS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x30)
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +0530153#define SS_PHY_PARAM_CTRL_1 (QSCRATCH_REG_OFFSET + 0x34)
154#define SS_PHY_PARAM_CTRL_2 (QSCRATCH_REG_OFFSET + 0x38)
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530155#define SS_CR_PROTOCOL_DATA_IN_REG (QSCRATCH_REG_OFFSET + 0x3C)
156#define SS_CR_PROTOCOL_DATA_OUT_REG (QSCRATCH_REG_OFFSET + 0x40)
157#define SS_CR_PROTOCOL_CAP_ADDR_REG (QSCRATCH_REG_OFFSET + 0x44)
158#define SS_CR_PROTOCOL_CAP_DATA_REG (QSCRATCH_REG_OFFSET + 0x48)
159#define SS_CR_PROTOCOL_READ_REG (QSCRATCH_REG_OFFSET + 0x4C)
160#define SS_CR_PROTOCOL_WRITE_REG (QSCRATCH_REG_OFFSET + 0x50)
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +0530161#define PWR_EVNT_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x58)
162#define PWR_EVNT_IRQ_MASK_REG (QSCRATCH_REG_OFFSET + 0x5C)
Manu Gautam8c642812012-06-07 10:35:10 +0530163
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300164struct dwc3_msm_req_complete {
165 struct list_head list_item;
166 struct usb_request *req;
167 void (*orig_complete)(struct usb_ep *ep,
168 struct usb_request *req);
169};
170
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200171struct dwc3_msm {
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200172 struct device *dev;
173 void __iomem *base;
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +0530174 struct resource *io_res;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200175 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300176 u8 ep_num_mapping[DBM_MAX_EPS];
177 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
178 struct list_head req_complete_list;
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +0530179 struct clk *xo_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700180 struct clk *ref_clk;
Manu Gautam1742db22012-06-19 13:33:24 +0530181 struct clk *core_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700182 struct clk *iface_clk;
183 struct clk *sleep_clk;
184 struct clk *hsphy_sleep_clk;
Jack Pham22698b82013-02-13 17:45:06 -0800185 struct clk *utmi_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530186 struct regulator *hsusb_3p3;
187 struct regulator *hsusb_1p8;
188 struct regulator *hsusb_vddcx;
189 struct regulator *ssusb_1p8;
190 struct regulator *ssusb_vddcx;
Hemant Kumar086bf6b2013-06-10 19:29:27 -0700191 struct regulator *dwc3_gdsc;
Manu Gautambb825d72013-03-12 16:25:42 +0530192
193 /* VBUS regulator if no OTG and running in host only mode */
194 struct regulator *vbus_otg;
Manu Gautamb5067272012-07-02 09:53:41 +0530195 struct dwc3_ext_xceiv ext_xceiv;
196 bool resume_pending;
197 atomic_t pm_suspended;
198 atomic_t in_lpm;
Manu Gautam377821c2012-09-28 16:53:24 +0530199 int hs_phy_irq;
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530200 int hsphy_init_seq;
Manu Gautam377821c2012-09-28 16:53:24 +0530201 bool lpm_irq_seen;
Manu Gautamb5067272012-07-02 09:53:41 +0530202 struct delayed_work resume_work;
Manu Gautam6eb13e32013-02-01 15:19:15 +0530203 struct work_struct restart_usb_work;
Vijayavardhan Vennapusaddd04742013-09-26 19:47:18 +0530204 struct work_struct usb_block_reset_work;
Manu Gautam8c642812012-06-07 10:35:10 +0530205 struct dwc3_charger charger;
206 struct usb_phy *otg_xceiv;
207 struct delayed_work chg_work;
208 enum usb_chg_state chg_state;
Jack Pham0cca9412013-03-08 13:22:42 -0800209 int pmic_id_irq;
210 struct work_struct id_work;
Siddartha Mohanadossa3e35512013-02-22 17:06:07 -0800211 struct qpnp_adc_tm_btm_param adc_param;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700212 struct qpnp_adc_tm_chip *adc_tm_dev;
Jack Pham0fc12332012-11-19 13:14:22 -0800213 struct delayed_work init_adc_work;
214 bool id_adc_detect;
Manu Gautam8c642812012-06-07 10:35:10 +0530215 u8 dcd_retries;
Manu Gautam2617deb2012-08-31 17:50:06 -0700216 u32 bus_perf_client;
217 struct msm_bus_scale_pdata *bus_scale_table;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530218 struct power_supply usb_psy;
Jack Pham9354c6a2012-12-20 19:19:32 -0800219 struct power_supply *ext_vbus_psy;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530220 unsigned int online;
221 unsigned int host_mode;
Pavankumar Kondetifbd4b142013-07-16 11:13:05 +0530222 unsigned int voltage_max;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530223 unsigned int current_max;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +0530224 unsigned int vdd_no_vol_level;
225 unsigned int vdd_low_vol_level;
226 unsigned int vdd_high_vol_level;
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +0530227 unsigned int tx_fifo_size;
228 unsigned int qdss_tx_fifo_size;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530229 bool vbus_active;
Jack Phamfadd6432012-12-07 19:03:41 -0800230 bool ext_inuse;
Jack Phamf12b7e12012-12-28 14:27:26 -0800231 enum dwc3_id_state id_state;
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +0530232 unsigned long lpm_flags;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530233#define MDWC3_PHY_REF_AND_CORECLK_OFF BIT(0)
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +0530234#define MDWC3_TCXO_SHUTDOWN BIT(1)
Pavankumar Kondeti08693e72013-05-03 11:55:48 +0530235
236 u32 qscratch_ctl_val;
237 dev_t ext_chg_dev;
238 struct cdev ext_chg_cdev;
239 struct class *ext_chg_class;
240 struct device *ext_chg_device;
241 bool ext_chg_opened;
242 bool ext_chg_active;
243 struct completion ext_chg_wait;
Manu Gautam60e01352012-05-29 09:00:34 +0530244};
245
246#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
247#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
248#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
249
250#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
251#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
252#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
253
254#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
255#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
256#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
257
Jack Phamfadd6432012-12-07 19:03:41 -0800258static struct usb_ext_notification *usb_ext;
259
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300260/**
261 *
262 * Read register with debug info.
263 *
264 * @base - DWC3 base virtual address.
265 * @offset - register offset.
266 *
267 * @return u32
268 */
269static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
270{
271 u32 val = ioread32(base + offset);
272 return val;
273}
274
275/**
276 * Read register masked field with debug info.
277 *
278 * @base - DWC3 base virtual address.
279 * @offset - register offset.
280 * @mask - register bitmask.
281 *
282 * @return u32
283 */
284static inline u32 dwc3_msm_read_reg_field(void *base,
285 u32 offset,
286 const u32 mask)
287{
288 u32 shift = find_first_bit((void *)&mask, 32);
289 u32 val = ioread32(base + offset);
290 val &= mask; /* clear other bits */
291 val >>= shift;
292 return val;
293}
294
295/**
296 *
297 * Write register with debug info.
298 *
299 * @base - DWC3 base virtual address.
300 * @offset - register offset.
301 * @val - value to write.
302 *
303 */
304static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
305{
306 iowrite32(val, base + offset);
307}
308
309/**
310 * Write register masked field with debug info.
311 *
312 * @base - DWC3 base virtual address.
313 * @offset - register offset.
314 * @mask - register bitmask.
315 * @val - value to write.
316 *
317 */
318static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
319 const u32 mask, u32 val)
320{
321 u32 shift = find_first_bit((void *)&mask, 32);
322 u32 tmp = ioread32(base + offset);
323
324 tmp &= ~mask; /* clear written bits */
325 val = tmp | (val << shift);
326 iowrite32(val, base + offset);
327}
328
329/**
Manu Gautam8c642812012-06-07 10:35:10 +0530330 * Write register and read back masked value to confirm it is written
331 *
332 * @base - DWC3 base virtual address.
333 * @offset - register offset.
334 * @mask - register bitmask specifying what should be updated
335 * @val - value to write.
336 *
337 */
338static inline void dwc3_msm_write_readback(void *base, u32 offset,
339 const u32 mask, u32 val)
340{
341 u32 write_val, tmp = ioread32(base + offset);
342
343 tmp &= ~mask; /* retain other bits */
344 write_val = tmp | val;
345
346 iowrite32(write_val, base + offset);
347
348 /* Read back to see if val was written */
349 tmp = ioread32(base + offset);
350 tmp &= mask; /* clear other bits */
351
352 if (tmp != val)
Jack Pham4b00e702013-07-03 17:10:36 -0700353 pr_err("%s: write: %x to QSCRATCH: %x FAILED\n",
354 __func__, val, offset);
Manu Gautam8c642812012-06-07 10:35:10 +0530355}
356
357/**
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530358 *
359 * Write SSPHY register with debug info.
360 *
361 * @base - DWC3 base virtual address.
362 * @addr - SSPHY address to write.
363 * @val - value to write.
364 *
365 */
366static void dwc3_msm_ssusb_write_phycreg(void *base, u32 addr, u32 val)
367{
368 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
369 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
370 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
371 cpu_relax();
372
373 iowrite32(val, base + SS_CR_PROTOCOL_DATA_IN_REG);
374 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_DATA_REG);
375 while (ioread32(base + SS_CR_PROTOCOL_CAP_DATA_REG))
376 cpu_relax();
377
378 iowrite32(0x1, base + SS_CR_PROTOCOL_WRITE_REG);
379 while (ioread32(base + SS_CR_PROTOCOL_WRITE_REG))
380 cpu_relax();
381}
382
383/**
384 *
385 * Read SSPHY register with debug info.
386 *
387 * @base - DWC3 base virtual address.
388 * @addr - SSPHY address to read.
389 *
390 */
391static u32 dwc3_msm_ssusb_read_phycreg(void *base, u32 addr)
392{
Vijayavardhan Vennapusa96201212013-06-12 19:59:27 +0530393 bool first_read = true;
394
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530395 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
396 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
397 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
398 cpu_relax();
399
Vijayavardhan Vennapusa96201212013-06-12 19:59:27 +0530400 /*
401 * Due to hardware bug, first read of SSPHY register might be
402 * incorrect. Hence as workaround, SW should perform SSPHY register
403 * read twice, but use only second read and ignore first read.
404 */
405retry:
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530406 iowrite32(0x1, base + SS_CR_PROTOCOL_READ_REG);
407 while (ioread32(base + SS_CR_PROTOCOL_READ_REG))
408 cpu_relax();
409
Vijayavardhan Vennapusa96201212013-06-12 19:59:27 +0530410 if (first_read) {
411 ioread32(base + SS_CR_PROTOCOL_DATA_OUT_REG);
412 first_read = false;
413 goto retry;
414 }
415
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530416 return ioread32(base + SS_CR_PROTOCOL_DATA_OUT_REG);
417}
418
419/**
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +0530420 * Dump all QSCRATCH registers.
421 *
422 */
423static void dwc3_msm_dump_phy_info(struct dwc3_msm *mdwc)
424{
425
426 dbg_print_reg("SSPHY_CTRL_REG", dwc3_msm_read_reg(mdwc->base,
427 SS_PHY_CTRL_REG));
428 dbg_print_reg("HSPHY_CTRL_REG", dwc3_msm_read_reg(mdwc->base,
429 HS_PHY_CTRL_REG));
430 dbg_print_reg("QSCRATCH_CTRL_REG", dwc3_msm_read_reg(mdwc->base,
431 QSCRATCH_CTRL_REG));
432 dbg_print_reg("QSCRATCH_GENERAL_CFG", dwc3_msm_read_reg(mdwc->base,
433 QSCRATCH_GENERAL_CFG));
434 dbg_print_reg("PARAMETER_OVERRIDE_X_REG", dwc3_msm_read_reg(mdwc->base,
435 PARAMETER_OVERRIDE_X_REG));
436 dbg_print_reg("HS_PHY_IRQ_STAT_REG", dwc3_msm_read_reg(mdwc->base,
437 HS_PHY_IRQ_STAT_REG));
438 dbg_print_reg("SS_PHY_PARAM_CTRL_1", dwc3_msm_read_reg(mdwc->base,
439 SS_PHY_PARAM_CTRL_1));
440 dbg_print_reg("SS_PHY_PARAM_CTRL_2", dwc3_msm_read_reg(mdwc->base,
441 SS_PHY_PARAM_CTRL_2));
442 dbg_print_reg("QSCRATCH_RAM1_REG", dwc3_msm_read_reg(mdwc->base,
443 QSCRATCH_RAM1_REG));
444 dbg_print_reg("PWR_EVNT_IRQ_STAT_REG", dwc3_msm_read_reg(mdwc->base,
445 PWR_EVNT_IRQ_STAT_REG));
446 dbg_print_reg("PWR_EVNT_IRQ_MASK_REG", dwc3_msm_read_reg(mdwc->base,
447 PWR_EVNT_IRQ_MASK_REG));
448}
449
450/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300451 * Return DBM EP number according to usb endpoint number.
452 *
453 */
Jack Pham62c19a42013-07-09 17:55:09 -0700454static int dwc3_msm_find_matching_dbm_ep(struct dwc3_msm *mdwc, u8 usb_ep)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300455{
456 int i;
457
Jack Pham62c19a42013-07-09 17:55:09 -0700458 for (i = 0; i < mdwc->dbm_num_eps; i++)
459 if (mdwc->ep_num_mapping[i] == usb_ep)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300460 return i;
461
462 return -ENODEV; /* Not found */
463}
464
465/**
466 * Return number of configured DBM endpoints.
467 *
468 */
Jack Pham62c19a42013-07-09 17:55:09 -0700469static int dwc3_msm_configured_dbm_ep_num(struct dwc3_msm *mdwc)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300470{
471 int i;
472 int count = 0;
473
Jack Pham62c19a42013-07-09 17:55:09 -0700474 for (i = 0; i < mdwc->dbm_num_eps; i++)
475 if (mdwc->ep_num_mapping[i])
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300476 count++;
477
478 return count;
479}
480
481/**
482 * Configure the DBM with the USB3 core event buffer.
483 * This function is called by the SNPS UDC upon initialization.
484 *
485 * @addr - address of the event buffer.
486 * @size - size of the event buffer.
487 *
488 */
Jack Pham62c19a42013-07-09 17:55:09 -0700489static int dwc3_msm_event_buffer_config(struct dwc3_msm *mdwc,
490 u32 addr, u16 size)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300491{
Jack Pham62c19a42013-07-09 17:55:09 -0700492 dev_dbg(mdwc->dev, "%s\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300493
Jack Pham62c19a42013-07-09 17:55:09 -0700494 dwc3_msm_write_reg(mdwc->base, DBM_GEVNTADR, addr);
495 dwc3_msm_write_reg_field(mdwc->base, DBM_GEVNTSIZ,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300496 DBM_GEVNTSIZ_MASK, size);
497
498 return 0;
499}
500
501/**
502 * Reset the DBM registers upon initialization.
503 *
504 */
Jack Pham62c19a42013-07-09 17:55:09 -0700505static int dwc3_msm_dbm_soft_reset(struct dwc3_msm *mdwc, int enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300506{
Jack Pham62c19a42013-07-09 17:55:09 -0700507 dev_dbg(mdwc->dev, "%s\n", __func__);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300508 if (enter_reset) {
Jack Pham62c19a42013-07-09 17:55:09 -0700509 dev_dbg(mdwc->dev, "enter DBM reset\n");
510 dwc3_msm_write_reg_field(mdwc->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300511 DBM_SFT_RST_MASK, 1);
512 } else {
Jack Pham62c19a42013-07-09 17:55:09 -0700513 dev_dbg(mdwc->dev, "exit DBM reset\n");
514 dwc3_msm_write_reg_field(mdwc->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300515 DBM_SFT_RST_MASK, 0);
516 /*enable DBM*/
Jack Pham62c19a42013-07-09 17:55:09 -0700517 dwc3_msm_write_reg_field(mdwc->base, QSCRATCH_GENERAL_CFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300518 DBM_EN_MASK, 0x1);
519 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300520
521 return 0;
522}
523
524/**
525 * Soft reset specific DBM ep.
526 * This function is called by the function driver upon events
527 * such as transfer aborting, USB re-enumeration and USB
528 * disconnection.
529 *
530 * @dbm_ep - DBM ep number.
531 * @enter_reset - should we enter a reset state or get out of it.
532 *
533 */
Jack Pham62c19a42013-07-09 17:55:09 -0700534static int dwc3_msm_dbm_ep_soft_reset(struct dwc3_msm *mdwc,
535 u8 dbm_ep, bool enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300536{
Jack Pham62c19a42013-07-09 17:55:09 -0700537 dev_dbg(mdwc->dev, "%s\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300538
Jack Pham62c19a42013-07-09 17:55:09 -0700539 if (dbm_ep >= mdwc->dbm_num_eps) {
540 dev_err(mdwc->dev, "%s: Invalid DBM ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300541 return -ENODEV;
542 }
543
544 if (enter_reset) {
Jack Pham62c19a42013-07-09 17:55:09 -0700545 dwc3_msm_write_reg_field(mdwc->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300546 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300547 } else {
Jack Pham62c19a42013-07-09 17:55:09 -0700548 dwc3_msm_write_reg_field(mdwc->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300549 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300550 }
551
552 return 0;
553}
554
555/**
556 * Configure a USB DBM ep to work in BAM mode.
557 *
558 *
559 * @usb_ep - USB physical EP number.
560 * @producer - producer/consumer.
561 * @disable_wb - disable write back to system memory.
562 * @internal_mem - use internal USB memory for data fifo.
563 * @ioc - enable interrupt on completion.
564 *
565 * @return int - DBM ep number.
566 */
Jack Pham62c19a42013-07-09 17:55:09 -0700567static int dwc3_msm_dbm_ep_config(struct dwc3_msm *mdwc, u8 usb_ep, u8 bam_pipe,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300568 bool producer, bool disable_wb,
569 bool internal_mem, bool ioc)
570{
571 u8 dbm_ep;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300572 u32 ep_cfg;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300573
Jack Pham62c19a42013-07-09 17:55:09 -0700574 dev_dbg(mdwc->dev, "%s\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300575
Jack Pham62c19a42013-07-09 17:55:09 -0700576 dbm_ep = dwc3_msm_find_matching_dbm_ep(mdwc, usb_ep);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300577
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300578 if (dbm_ep < 0) {
Jack Pham62c19a42013-07-09 17:55:09 -0700579 dev_err(mdwc->dev,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300580 "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300581 return -ENODEV;
582 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300583 /* First, reset the dbm endpoint */
Jack Pham62c19a42013-07-09 17:55:09 -0700584 dwc3_msm_dbm_ep_soft_reset(mdwc, dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300585
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300586 /* Set ioc bit for dbm_ep if needed */
Jack Pham62c19a42013-07-09 17:55:09 -0700587 dwc3_msm_write_reg_field(mdwc->base, DBM_DBG_CNFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300588 DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300589
Shimrit Malichia00d7322012-08-05 13:56:28 +0300590 ep_cfg = (producer ? DBM_PRODUCER : 0) |
591 (disable_wb ? DBM_DISABLE_WB : 0) |
592 (internal_mem ? DBM_INT_RAM_ACC : 0);
593
Jack Pham62c19a42013-07-09 17:55:09 -0700594 dwc3_msm_write_reg_field(mdwc->base, DBM_EP_CFG(dbm_ep),
Shimrit Malichia00d7322012-08-05 13:56:28 +0300595 DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8);
596
Jack Pham62c19a42013-07-09 17:55:09 -0700597 dwc3_msm_write_reg_field(mdwc->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300598 usb_ep);
Jack Pham62c19a42013-07-09 17:55:09 -0700599 dwc3_msm_write_reg_field(mdwc->base, DBM_EP_CFG(dbm_ep),
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300600 DBM_BAM_PIPE_NUM, bam_pipe);
Jack Pham62c19a42013-07-09 17:55:09 -0700601 dwc3_msm_write_reg_field(mdwc->base, DBM_PIPE_CFG, 0x000000ff,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300602 0xe4);
Jack Pham62c19a42013-07-09 17:55:09 -0700603 dwc3_msm_write_reg_field(mdwc->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300604 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300605
606 return dbm_ep;
607}
608
609/**
610 * Configure a USB DBM ep to work in normal mode.
611 *
612 * @usb_ep - USB ep number.
613 *
614 */
Jack Pham62c19a42013-07-09 17:55:09 -0700615static int dwc3_msm_dbm_ep_unconfig(struct dwc3_msm *mdwc, u8 usb_ep)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300616{
617 u8 dbm_ep;
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530618 u32 data;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300619
Jack Pham62c19a42013-07-09 17:55:09 -0700620 dev_dbg(mdwc->dev, "%s\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300621
Jack Pham62c19a42013-07-09 17:55:09 -0700622 dbm_ep = dwc3_msm_find_matching_dbm_ep(mdwc, usb_ep);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300623
624 if (dbm_ep < 0) {
Jack Pham62c19a42013-07-09 17:55:09 -0700625 dev_err(mdwc->dev, "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300626 return -ENODEV;
627 }
628
Jack Pham62c19a42013-07-09 17:55:09 -0700629 mdwc->ep_num_mapping[dbm_ep] = 0;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300630
Jack Pham62c19a42013-07-09 17:55:09 -0700631 data = dwc3_msm_read_reg(mdwc->base, DBM_EP_CFG(dbm_ep));
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530632 data &= (~0x1);
Jack Pham62c19a42013-07-09 17:55:09 -0700633 dwc3_msm_write_reg(mdwc->base, DBM_EP_CFG(dbm_ep), data);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300634
635 /* Reset the dbm endpoint */
Jack Pham62c19a42013-07-09 17:55:09 -0700636 dwc3_msm_dbm_ep_soft_reset(mdwc, dbm_ep, true);
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530637 /*
638 * 10 usec delay is required before deasserting DBM endpoint reset
639 * according to hardware programming guide.
640 */
641 udelay(10);
Jack Pham62c19a42013-07-09 17:55:09 -0700642 dwc3_msm_dbm_ep_soft_reset(mdwc, dbm_ep, false);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300643
644 return 0;
645}
646
647/**
648 * Configure the DBM with the BAM's data fifo.
649 * This function is called by the USB BAM Driver
650 * upon initialization.
651 *
652 * @ep - pointer to usb endpoint.
653 * @addr - address of data fifo.
654 * @size - size of data fifo.
655 *
656 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300657int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300658{
659 u8 dbm_ep;
660 struct dwc3_ep *dep = to_dwc3_ep(ep);
Jack Pham62c19a42013-07-09 17:55:09 -0700661 struct dwc3 *dwc = dep->dwc;
662 struct dwc3_msm *mdwc = dev_get_drvdata(dwc->dev->parent);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300663 u8 bam_pipe = dst_pipe_idx;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300664
Jack Pham62c19a42013-07-09 17:55:09 -0700665 dev_dbg(mdwc->dev, "%s\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300666
Shimrit Malichia00d7322012-08-05 13:56:28 +0300667 dbm_ep = bam_pipe;
Jack Pham62c19a42013-07-09 17:55:09 -0700668 mdwc->ep_num_mapping[dbm_ep] = dep->number;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300669
Jack Pham62c19a42013-07-09 17:55:09 -0700670 dwc3_msm_write_reg(mdwc->base, DBM_DATA_FIFO(dbm_ep), addr);
671 dwc3_msm_write_reg_field(mdwc->base, DBM_DATA_FIFO_SIZE(dbm_ep),
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300672 DBM_DATA_FIFO_SIZE_MASK, size);
673
674 return 0;
675}
676
677/**
678* Cleanups for msm endpoint on request complete.
679*
680* Also call original request complete.
681*
682* @usb_ep - pointer to usb_ep instance.
683* @request - pointer to usb_request instance.
684*
685* @return int - 0 on success, negetive on error.
686*/
687static void dwc3_msm_req_complete_func(struct usb_ep *ep,
688 struct usb_request *request)
689{
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300690 struct dwc3_ep *dep = to_dwc3_ep(ep);
Jack Pham62c19a42013-07-09 17:55:09 -0700691 struct dwc3 *dwc = dep->dwc;
692 struct dwc3_msm *mdwc = dev_get_drvdata(dwc->dev->parent);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300693 struct dwc3_msm_req_complete *req_complete = NULL;
694
695 /* Find original request complete function and remove it from list */
Jack Pham62c19a42013-07-09 17:55:09 -0700696 list_for_each_entry(req_complete, &mdwc->req_complete_list, list_item) {
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300697 if (req_complete->req == request)
698 break;
699 }
700 if (!req_complete || req_complete->req != request) {
701 dev_err(dep->dwc->dev, "%s: could not find the request\n",
702 __func__);
703 return;
704 }
705 list_del(&req_complete->list_item);
706
707 /*
708 * Release another one TRB to the pool since DBM queue took 2 TRBs
709 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
710 * released only one.
711 */
Manu Gautam55d34222012-12-19 16:49:47 +0530712 dep->busy_slot++;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300713
714 /* Unconfigure dbm ep */
Jack Pham62c19a42013-07-09 17:55:09 -0700715 dwc3_msm_dbm_ep_unconfig(mdwc, dep->number);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300716
717 /*
718 * If this is the last endpoint we unconfigured, than reset also
719 * the event buffers.
720 */
Jack Pham62c19a42013-07-09 17:55:09 -0700721 if (0 == dwc3_msm_configured_dbm_ep_num(mdwc))
722 dwc3_msm_event_buffer_config(mdwc, 0, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300723
724 /*
725 * Call original complete function, notice that dwc->lock is already
726 * taken by the caller of this function (dwc3_gadget_giveback()).
727 */
728 request->complete = req_complete->orig_complete;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300729 if (request->complete)
730 request->complete(ep, request);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300731
732 kfree(req_complete);
733}
734
735/**
736* Helper function.
737* See the header of the dwc3_msm_ep_queue function.
738*
739* @dwc3_ep - pointer to dwc3_ep instance.
740* @req - pointer to dwc3_request instance.
741*
742* @return int - 0 on success, negetive on error.
743*/
744static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
745{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300746 struct dwc3_trb *trb;
747 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300748 struct dwc3_gadget_ep_cmd_params params;
749 u32 cmd;
750 int ret = 0;
751
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300752 /* We push the request to the dep->req_queued list to indicate that
753 * this request is issued with start transfer. The request will be out
754 * from this list in 2 cases. The first is that the transfer will be
755 * completed (not if the transfer is endless using a circular TRBs with
756 * with link TRB). The second case is an option to do stop stransfer,
757 * this can be initiated by the function driver when calling dequeue.
758 */
759 req->queued = true;
760 list_add_tail(&req->list, &dep->req_queued);
761
762 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300763 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300764 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300765 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300766
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300767 req->trb = trb;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300768 trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300769 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
770 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300771 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300772
773 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300774 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300775 dep->free_slot++;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300776 memset(trb_link, 0, sizeof *trb_link);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300777
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300778 trb_link->bpl = lower_32_bits(req->trb_dma);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300779 trb_link->bph = DBM_TRB_BIT |
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300780 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
781 trb_link->size = 0;
782 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300783
784 /*
785 * Now start the transfer
786 */
787 memset(&params, 0, sizeof(params));
Shimrit Malichia00d7322012-08-05 13:56:28 +0300788 params.param0 = 0; /* TDAddr High */
789 params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */
790
Manu Gautam5b2bf9a2012-10-18 10:52:50 +0530791 /* DBM requires IOC to be set */
792 cmd = DWC3_DEPCMD_STARTTRANSFER | DWC3_DEPCMD_CMDIOC;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300793 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
794 if (ret < 0) {
795 dev_dbg(dep->dwc->dev,
796 "%s: failed to send STARTTRANSFER command\n",
797 __func__);
798
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300799 list_del(&req->list);
800 return ret;
801 }
Manu Gautam4a51a062012-12-07 11:24:39 +0530802 dep->flags |= DWC3_EP_BUSY;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300803
804 return ret;
805}
806
807/**
808* Queue a usb request to the DBM endpoint.
809* This function should be called after the endpoint
810* was enabled by the ep_enable.
811*
812* This function prepares special structure of TRBs which
813* is familier with the DBM HW, so it will possible to use
814* this endpoint in DBM mode.
815*
816* The TRBs prepared by this function, is one normal TRB
817* which point to a fake buffer, followed by a link TRB
818* that points to the first TRB.
819*
820* The API of this function follow the regular API of
821* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
822*
823* @usb_ep - pointer to usb_ep instance.
824* @request - pointer to usb_request instance.
825* @gfp_flags - possible flags.
826*
827* @return int - 0 on success, negetive on error.
828*/
829static int dwc3_msm_ep_queue(struct usb_ep *ep,
830 struct usb_request *request, gfp_t gfp_flags)
831{
832 struct dwc3_request *req = to_dwc3_request(request);
833 struct dwc3_ep *dep = to_dwc3_ep(ep);
834 struct dwc3 *dwc = dep->dwc;
Jack Pham62c19a42013-07-09 17:55:09 -0700835 struct dwc3_msm *mdwc = dev_get_drvdata(dwc->dev->parent);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300836 struct dwc3_msm_req_complete *req_complete;
837 unsigned long flags;
838 int ret = 0;
839 u8 bam_pipe;
840 bool producer;
841 bool disable_wb;
842 bool internal_mem;
843 bool ioc;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300844 u8 speed;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300845
846 if (!(request->udc_priv & MSM_SPS_MODE)) {
847 /* Not SPS mode, call original queue */
Jack Pham62c19a42013-07-09 17:55:09 -0700848 dev_vdbg(mdwc->dev, "%s: not sps mode, use regular queue\n",
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300849 __func__);
850
Jack Pham62c19a42013-07-09 17:55:09 -0700851 return (mdwc->original_ep_ops[dep->number])->queue(ep,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300852 request,
853 gfp_flags);
854 }
855
856 if (!dep->endpoint.desc) {
Jack Pham62c19a42013-07-09 17:55:09 -0700857 dev_err(mdwc->dev,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300858 "%s: trying to queue request %p to disabled ep %s\n",
859 __func__, request, ep->name);
860 return -EPERM;
861 }
862
863 if (dep->number == 0 || dep->number == 1) {
Jack Pham62c19a42013-07-09 17:55:09 -0700864 dev_err(mdwc->dev,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300865 "%s: trying to queue dbm request %p to control ep %s\n",
866 __func__, request, ep->name);
867 return -EPERM;
868 }
869
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300870
Manu Gautam4a51a062012-12-07 11:24:39 +0530871 if (dep->busy_slot != dep->free_slot || !list_empty(&dep->request_list)
872 || !list_empty(&dep->req_queued)) {
Jack Pham62c19a42013-07-09 17:55:09 -0700873 dev_err(mdwc->dev,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300874 "%s: trying to queue dbm request %p tp ep %s\n",
875 __func__, request, ep->name);
876 return -EPERM;
Manu Gautam4a51a062012-12-07 11:24:39 +0530877 } else {
878 dep->busy_slot = 0;
879 dep->free_slot = 0;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300880 }
881
882 /*
883 * Override req->complete function, but before doing that,
884 * store it's original pointer in the req_complete_list.
885 */
886 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
887 if (!req_complete) {
Jack Pham62c19a42013-07-09 17:55:09 -0700888 dev_err(mdwc->dev, "%s: not enough memory\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300889 return -ENOMEM;
890 }
891 req_complete->req = request;
892 req_complete->orig_complete = request->complete;
Jack Pham62c19a42013-07-09 17:55:09 -0700893 list_add_tail(&req_complete->list_item, &mdwc->req_complete_list);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300894 request->complete = dwc3_msm_req_complete_func;
895
896 /*
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300897 * Configure the DBM endpoint
898 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300899 bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300900 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
901 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
902 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
903 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
904
Jack Pham62c19a42013-07-09 17:55:09 -0700905 ret = dwc3_msm_dbm_ep_config(mdwc, dep->number,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300906 bam_pipe, producer,
907 disable_wb, internal_mem, ioc);
908 if (ret < 0) {
Jack Pham62c19a42013-07-09 17:55:09 -0700909 dev_err(mdwc->dev,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300910 "error %d after calling dwc3_msm_dbm_ep_config\n",
911 ret);
912 return ret;
913 }
914
915 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
916 __func__, request, ep->name, request->length);
917
918 /*
919 * We must obtain the lock of the dwc3 core driver,
920 * including disabling interrupts, so we will be sure
921 * that we are the only ones that configure the HW device
922 * core and ensure that we queuing the request will finish
923 * as soon as possible so we will release back the lock.
924 */
925 spin_lock_irqsave(&dwc->lock, flags);
926 ret = __dwc3_msm_ep_queue(dep, req);
927 spin_unlock_irqrestore(&dwc->lock, flags);
928 if (ret < 0) {
Jack Pham62c19a42013-07-09 17:55:09 -0700929 dev_err(mdwc->dev,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300930 "error %d after calling __dwc3_msm_ep_queue\n", ret);
931 return ret;
932 }
933
Shimrit Malichia00d7322012-08-05 13:56:28 +0300934 speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD;
Jack Pham62c19a42013-07-09 17:55:09 -0700935 dwc3_msm_write_reg(mdwc->base, DBM_GEN_CFG, speed >> 2);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300936
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300937 return 0;
938}
939
940/**
941 * Configure MSM endpoint.
942 * This function do specific configurations
943 * to an endpoint which need specific implementaion
944 * in the MSM architecture.
945 *
946 * This function should be called by usb function/class
947 * layer which need a support from the specific MSM HW
948 * which wrap the USB3 core. (like DBM specific endpoints)
949 *
950 * @ep - a pointer to some usb_ep instance
951 *
952 * @return int - 0 on success, negetive on error.
953 */
954int msm_ep_config(struct usb_ep *ep)
955{
956 struct dwc3_ep *dep = to_dwc3_ep(ep);
Jack Pham62c19a42013-07-09 17:55:09 -0700957 struct dwc3 *dwc = dep->dwc;
958 struct dwc3_msm *mdwc = dev_get_drvdata(dwc->dev->parent);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300959 struct usb_ep_ops *new_ep_ops;
960
Jack Pham62c19a42013-07-09 17:55:09 -0700961 dwc3_msm_event_buffer_config(mdwc,
962 dwc3_msm_read_reg(mdwc->base, DWC3_GEVNTADRLO(0)),
963 dwc3_msm_read_reg(mdwc->base, DWC3_GEVNTSIZ(0)));
Manu Gautama302f612012-12-18 17:33:06 +0530964
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300965 /* Save original ep ops for future restore*/
Jack Pham62c19a42013-07-09 17:55:09 -0700966 if (mdwc->original_ep_ops[dep->number]) {
967 dev_err(mdwc->dev,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300968 "ep [%s,%d] already configured as msm endpoint\n",
969 ep->name, dep->number);
970 return -EPERM;
971 }
Jack Pham62c19a42013-07-09 17:55:09 -0700972 mdwc->original_ep_ops[dep->number] = ep->ops;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300973
974 /* Set new usb ops as we like */
975 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
976 if (!new_ep_ops) {
Jack Pham62c19a42013-07-09 17:55:09 -0700977 dev_err(mdwc->dev,
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300978 "%s: unable to allocate mem for new usb ep ops\n",
979 __func__);
980 return -ENOMEM;
981 }
982 (*new_ep_ops) = (*ep->ops);
983 new_ep_ops->queue = dwc3_msm_ep_queue;
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530984 new_ep_ops->disable = ep->ops->disable;
985
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300986 ep->ops = new_ep_ops;
987
988 /*
989 * Do HERE more usb endpoint configurations
990 * which are specific to MSM.
991 */
992
993 return 0;
994}
995EXPORT_SYMBOL(msm_ep_config);
996
997/**
998 * Un-configure MSM endpoint.
999 * Tear down configurations done in the
1000 * dwc3_msm_ep_config function.
1001 *
1002 * @ep - a pointer to some usb_ep instance
1003 *
1004 * @return int - 0 on success, negetive on error.
1005 */
1006int msm_ep_unconfig(struct usb_ep *ep)
1007{
1008 struct dwc3_ep *dep = to_dwc3_ep(ep);
Jack Pham62c19a42013-07-09 17:55:09 -07001009 struct dwc3 *dwc = dep->dwc;
1010 struct dwc3_msm *mdwc = dev_get_drvdata(dwc->dev->parent);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001011 struct usb_ep_ops *old_ep_ops;
1012
1013 /* Restore original ep ops */
Jack Pham62c19a42013-07-09 17:55:09 -07001014 if (!mdwc->original_ep_ops[dep->number]) {
1015 dev_err(mdwc->dev,
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001016 "ep [%s,%d] was not configured as msm endpoint\n",
1017 ep->name, dep->number);
1018 return -EINVAL;
1019 }
1020 old_ep_ops = (struct usb_ep_ops *)ep->ops;
Jack Pham62c19a42013-07-09 17:55:09 -07001021 ep->ops = mdwc->original_ep_ops[dep->number];
1022 mdwc->original_ep_ops[dep->number] = NULL;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001023 kfree(old_ep_ops);
1024
1025 /*
1026 * Do HERE more usb endpoint un-configurations
1027 * which are specific to MSM.
1028 */
1029
1030 return 0;
1031}
1032EXPORT_SYMBOL(msm_ep_unconfig);
1033
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +05301034void dwc3_tx_fifo_resize_request(struct usb_ep *ep, bool qdss_enabled)
1035{
1036 struct dwc3_ep *dep = to_dwc3_ep(ep);
1037 struct dwc3 *dwc = dep->dwc;
1038 struct dwc3_msm *mdwc = dev_get_drvdata(dwc->dev->parent);
1039
1040 if (qdss_enabled)
1041 dwc->tx_fifo_size = mdwc->qdss_tx_fifo_size;
1042 else
1043 dwc->tx_fifo_size = mdwc->tx_fifo_size;
1044}
1045EXPORT_SYMBOL(dwc3_tx_fifo_resize_request);
1046
Manu Gautam6eb13e32013-02-01 15:19:15 +05301047static void dwc3_restart_usb_work(struct work_struct *w)
1048{
1049 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1050 restart_usb_work);
1051
1052 dev_dbg(mdwc->dev, "%s\n", __func__);
1053
1054 if (atomic_read(&mdwc->in_lpm) || !mdwc->otg_xceiv) {
1055 dev_err(mdwc->dev, "%s failed!!!\n", __func__);
1056 return;
1057 }
1058
1059 if (!mdwc->ext_xceiv.bsv) {
1060 dev_dbg(mdwc->dev, "%s bailing out in disconnect\n", __func__);
1061 return;
1062 }
1063
1064 /* Reset active USB connection */
1065 mdwc->ext_xceiv.bsv = false;
1066 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
1067 /* Make sure disconnect is processed before sending connect */
1068 flush_delayed_work(&mdwc->resume_work);
1069
1070 mdwc->ext_xceiv.bsv = true;
1071 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
1072}
1073
1074/**
1075 * Reset USB peripheral connection
1076 * Inform OTG for Vbus LOW followed by Vbus HIGH notification.
1077 * This performs full hardware reset and re-initialization which
1078 * might be required by some DBM client driver during uninit/cleanup.
1079 */
Jack Pham62c19a42013-07-09 17:55:09 -07001080void msm_dwc3_restart_usb_session(struct usb_gadget *gadget)
Manu Gautam6eb13e32013-02-01 15:19:15 +05301081{
Jack Pham62c19a42013-07-09 17:55:09 -07001082 struct dwc3 *dwc = container_of(gadget, struct dwc3, gadget);
1083 struct dwc3_msm *mdwc = dev_get_drvdata(dwc->dev->parent);
1084
1085 if (mdwc)
1086 return;
Manu Gautam6eb13e32013-02-01 15:19:15 +05301087
1088 dev_dbg(mdwc->dev, "%s\n", __func__);
1089 queue_work(system_nrt_wq, &mdwc->restart_usb_work);
Manu Gautam6eb13e32013-02-01 15:19:15 +05301090}
1091EXPORT_SYMBOL(msm_dwc3_restart_usb_session);
1092
Jack Phamfadd6432012-12-07 19:03:41 -08001093/**
1094 * msm_register_usb_ext_notification: register for event notification
1095 * @info: pointer to client usb_ext_notification structure. May be NULL.
1096 *
1097 * @return int - 0 on success, negative on error
1098 */
1099int msm_register_usb_ext_notification(struct usb_ext_notification *info)
1100{
1101 pr_debug("%s usb_ext: %p\n", __func__, info);
1102
1103 if (info) {
1104 if (usb_ext) {
1105 pr_err("%s: already registered\n", __func__);
1106 return -EEXIST;
1107 }
1108
1109 if (!info->notify) {
1110 pr_err("%s: notify is NULL\n", __func__);
1111 return -EINVAL;
1112 }
1113 }
1114
1115 usb_ext = info;
1116 return 0;
1117}
1118EXPORT_SYMBOL(msm_register_usb_ext_notification);
1119
Manu Gautam60e01352012-05-29 09:00:34 +05301120/* HSPHY */
Jack Pham4b00e702013-07-03 17:10:36 -07001121static int dwc3_hsusb_config_vddcx(struct dwc3_msm *dwc, int high)
Manu Gautam60e01352012-05-29 09:00:34 +05301122{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301123 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301124
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301125 max_vol = dwc->vdd_high_vol_level;
1126 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301127 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
1128 if (ret) {
1129 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
1130 return ret;
1131 }
1132
1133 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1134 min_vol, max_vol);
1135
1136 return ret;
1137}
1138
Jack Pham4b00e702013-07-03 17:10:36 -07001139static int dwc3_hsusb_ldo_init(struct dwc3_msm *dwc, int init)
Manu Gautam60e01352012-05-29 09:00:34 +05301140{
1141 int rc = 0;
Manu Gautam60e01352012-05-29 09:00:34 +05301142
1143 if (!init) {
1144 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
1145 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1146 return 0;
1147 }
1148
1149 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
1150 if (IS_ERR(dwc->hsusb_3p3)) {
1151 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
1152 return PTR_ERR(dwc->hsusb_3p3);
1153 }
1154
1155 rc = regulator_set_voltage(dwc->hsusb_3p3,
1156 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
1157 if (rc) {
1158 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
1159 return rc;
1160 }
1161 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
1162 if (IS_ERR(dwc->hsusb_1p8)) {
1163 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
1164 rc = PTR_ERR(dwc->hsusb_1p8);
1165 goto devote_3p3;
1166 }
1167 rc = regulator_set_voltage(dwc->hsusb_1p8,
1168 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
1169 if (rc) {
1170 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
1171 goto devote_3p3;
1172 }
1173
1174 return 0;
1175
1176devote_3p3:
1177 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1178
1179 return rc;
1180}
1181
Jack Pham4b00e702013-07-03 17:10:36 -07001182static int dwc3_hsusb_ldo_enable(struct dwc3_msm *dwc, int on)
Manu Gautam60e01352012-05-29 09:00:34 +05301183{
1184 int rc = 0;
Manu Gautam60e01352012-05-29 09:00:34 +05301185
1186 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1187
1188 if (!on)
1189 goto disable_regulators;
1190
1191
1192 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
1193 if (rc < 0) {
1194 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
1195 return rc;
1196 }
1197
1198 rc = regulator_enable(dwc->hsusb_1p8);
1199 if (rc) {
1200 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
1201 goto put_1p8_lpm;
1202 }
1203
1204 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
1205 if (rc < 0) {
1206 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
1207 goto disable_1p8;
1208 }
1209
1210 rc = regulator_enable(dwc->hsusb_3p3);
1211 if (rc) {
1212 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
1213 goto put_3p3_lpm;
1214 }
1215
1216 return 0;
1217
1218disable_regulators:
1219 rc = regulator_disable(dwc->hsusb_3p3);
1220 if (rc)
1221 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
1222
1223put_3p3_lpm:
1224 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
1225 if (rc < 0)
1226 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
1227
1228disable_1p8:
1229 rc = regulator_disable(dwc->hsusb_1p8);
1230 if (rc)
1231 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
1232
1233put_1p8_lpm:
1234 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
1235 if (rc < 0)
1236 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
1237
1238 return rc < 0 ? rc : 0;
1239}
1240
1241/* SSPHY */
Jack Pham4b00e702013-07-03 17:10:36 -07001242static int dwc3_ssusb_config_vddcx(struct dwc3_msm *dwc, int high)
Manu Gautam60e01352012-05-29 09:00:34 +05301243{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301244 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301245
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301246 max_vol = dwc->vdd_high_vol_level;
1247 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301248 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
1249 if (ret) {
1250 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
1251 return ret;
1252 }
1253
1254 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1255 min_vol, max_vol);
1256 return ret;
1257}
1258
1259/* 3.3v supply not needed for SS PHY */
Jack Pham4b00e702013-07-03 17:10:36 -07001260static int dwc3_ssusb_ldo_init(struct dwc3_msm *dwc, int init)
Manu Gautam60e01352012-05-29 09:00:34 +05301261{
1262 int rc = 0;
Manu Gautam60e01352012-05-29 09:00:34 +05301263
1264 if (!init) {
1265 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
1266 return 0;
1267 }
1268
1269 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
1270 if (IS_ERR(dwc->ssusb_1p8)) {
1271 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
1272 return PTR_ERR(dwc->ssusb_1p8);
1273 }
1274 rc = regulator_set_voltage(dwc->ssusb_1p8,
1275 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
1276 if (rc)
1277 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
1278
1279 return rc;
1280}
1281
Jack Pham4b00e702013-07-03 17:10:36 -07001282static int dwc3_ssusb_ldo_enable(struct dwc3_msm *dwc, int on)
Manu Gautam60e01352012-05-29 09:00:34 +05301283{
1284 int rc = 0;
Manu Gautam60e01352012-05-29 09:00:34 +05301285
Jack Pham4b00e702013-07-03 17:10:36 -07001286 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
Manu Gautam60e01352012-05-29 09:00:34 +05301287
1288 if (!on)
1289 goto disable_regulators;
1290
1291
1292 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1293 if (rc < 0) {
1294 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1295 return rc;
1296 }
1297
1298 rc = regulator_enable(dwc->ssusb_1p8);
1299 if (rc) {
1300 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1301 goto put_1p8_lpm;
1302 }
1303
1304 return 0;
1305
1306disable_regulators:
1307 rc = regulator_disable(dwc->ssusb_1p8);
1308 if (rc)
1309 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1310
1311put_1p8_lpm:
1312 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1313 if (rc < 0)
1314 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1315
1316 return rc < 0 ? rc : 0;
1317}
1318
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001319/*
1320 * Config Global Distributed Switch Controller (GDSC)
1321 * to support controller power collapse
1322 */
Jack Pham80162462013-07-10 11:59:01 -07001323static int dwc3_msm_config_gdsc(struct dwc3_msm *mdwc, int on)
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001324{
1325 int ret = 0;
1326
Jack Pham80162462013-07-10 11:59:01 -07001327 if (IS_ERR(mdwc->dwc3_gdsc))
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001328 return 0;
1329
Jack Pham80162462013-07-10 11:59:01 -07001330 if (!mdwc->dwc3_gdsc) {
1331 mdwc->dwc3_gdsc = devm_regulator_get(mdwc->dev,
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001332 "USB3_GDSC");
Jack Pham80162462013-07-10 11:59:01 -07001333 if (IS_ERR(mdwc->dwc3_gdsc))
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001334 return 0;
1335 }
1336
1337 if (on) {
Jack Pham80162462013-07-10 11:59:01 -07001338 ret = regulator_enable(mdwc->dwc3_gdsc);
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001339 if (ret) {
Jack Pham80162462013-07-10 11:59:01 -07001340 dev_err(mdwc->dev, "unable to enable usb3 gdsc\n");
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001341 return ret;
1342 }
1343 } else {
Jack Pham80162462013-07-10 11:59:01 -07001344 regulator_disable(mdwc->dwc3_gdsc);
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001345 }
1346
1347 return 0;
1348}
1349
Jack Pham4b00e702013-07-03 17:10:36 -07001350static int dwc3_msm_link_clk_reset(struct dwc3_msm *mdwc, bool assert)
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301351{
1352 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301353
1354 if (assert) {
1355 /* Using asynchronous block reset to the hardware */
1356 dev_dbg(mdwc->dev, "block_reset ASSERT\n");
1357 clk_disable_unprepare(mdwc->ref_clk);
1358 clk_disable_unprepare(mdwc->iface_clk);
1359 clk_disable_unprepare(mdwc->core_clk);
1360 ret = clk_reset(mdwc->core_clk, CLK_RESET_ASSERT);
1361 if (ret)
1362 dev_err(mdwc->dev, "dwc3 core_clk assert failed\n");
1363 } else {
1364 dev_dbg(mdwc->dev, "block_reset DEASSERT\n");
1365 ret = clk_reset(mdwc->core_clk, CLK_RESET_DEASSERT);
1366 ndelay(200);
1367 clk_prepare_enable(mdwc->core_clk);
1368 clk_prepare_enable(mdwc->ref_clk);
1369 clk_prepare_enable(mdwc->iface_clk);
1370 if (ret)
1371 dev_err(mdwc->dev, "dwc3 core_clk deassert failed\n");
1372 }
1373
1374 return ret;
1375}
1376
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301377/* Reinitialize SSPHY parameters by overriding using QSCRATCH CR interface */
Jack Pham80162462013-07-10 11:59:01 -07001378static void dwc3_msm_ss_phy_reg_init(struct dwc3_msm *mdwc)
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301379{
1380 u32 data = 0;
1381
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301382 /*
1383 * WORKAROUND: There is SSPHY suspend bug due to which USB enumerates
1384 * in HS mode instead of SS mode. Workaround it by asserting
1385 * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus mode
1386 */
Jack Pham80162462013-07-10 11:59:01 -07001387 data = dwc3_msm_ssusb_read_phycreg(mdwc->base, 0x102D);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301388 data |= (1 << 7);
Jack Pham80162462013-07-10 11:59:01 -07001389 dwc3_msm_ssusb_write_phycreg(mdwc->base, 0x102D, data);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301390
Jack Pham80162462013-07-10 11:59:01 -07001391 data = dwc3_msm_ssusb_read_phycreg(mdwc->base, 0x1010);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301392 data &= ~0xFF0;
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301393 data |= 0x20;
Jack Pham80162462013-07-10 11:59:01 -07001394 dwc3_msm_ssusb_write_phycreg(mdwc->base, 0x1010, data);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301395
1396 /*
1397 * Fix RX Equalization setting as follows
1398 * LANE0.RX_OVRD_IN_HI. RX_EQ_EN set to 0
1399 * LANE0.RX_OVRD_IN_HI.RX_EQ_EN_OVRD set to 1
1400 * LANE0.RX_OVRD_IN_HI.RX_EQ set to 3
1401 * LANE0.RX_OVRD_IN_HI.RX_EQ_OVRD set to 1
1402 */
Jack Pham80162462013-07-10 11:59:01 -07001403 data = dwc3_msm_ssusb_read_phycreg(mdwc->base, 0x1006);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301404 data &= ~(1 << 6);
1405 data |= (1 << 7);
1406 data &= ~(0x7 << 8);
1407 data |= (0x3 << 8);
1408 data |= (0x1 << 11);
Jack Pham80162462013-07-10 11:59:01 -07001409 dwc3_msm_ssusb_write_phycreg(mdwc->base, 0x1006, data);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301410
1411 /*
1412 * Set EQ and TX launch amplitudes as follows
1413 * LANE0.TX_OVRD_DRV_LO.PREEMPH set to 22
1414 * LANE0.TX_OVRD_DRV_LO.AMPLITUDE set to 127
1415 * LANE0.TX_OVRD_DRV_LO.EN set to 1.
1416 */
Jack Pham80162462013-07-10 11:59:01 -07001417 data = dwc3_msm_ssusb_read_phycreg(mdwc->base, 0x1002);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301418 data &= ~0x3F80;
1419 data |= (0x16 << 7);
1420 data &= ~0x7F;
1421 data |= (0x7F | (1 << 14));
Jack Pham80162462013-07-10 11:59:01 -07001422 dwc3_msm_ssusb_write_phycreg(mdwc->base, 0x1002, data);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301423
Jack Pham63c8c702013-04-24 19:21:33 -07001424 /*
1425 * Set the QSCRATCH SS_PHY_PARAM_CTRL1 parameters as follows
1426 * TX_FULL_SWING [26:20] amplitude to 127
1427 * TX_DEEMPH_3_5DB [13:8] to 22
1428 * LOS_BIAS [2:0] to 0x5
1429 */
Jack Pham80162462013-07-10 11:59:01 -07001430 dwc3_msm_write_readback(mdwc->base, SS_PHY_PARAM_CTRL_1,
Jack Pham63c8c702013-04-24 19:21:33 -07001431 0x07f03f07, 0x07f01605);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301432}
1433
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301434/* Initialize QSCRATCH registers for HSPHY and SSPHY operation */
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301435static void dwc3_msm_qscratch_reg_init(struct dwc3_msm *mdwc,
1436 unsigned event_status)
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301437{
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301438 if (event_status == DWC3_CONTROLLER_POST_RESET_EVENT) {
1439 dwc3_msm_ss_phy_reg_init(mdwc);
1440 return;
1441 }
1442
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301443 /* SSPHY Initialization: Use ref_clk from pads and set its parameters */
Jack Pham80162462013-07-10 11:59:01 -07001444 dwc3_msm_write_reg(mdwc->base, SS_PHY_CTRL_REG, 0x10210002);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301445 msleep(30);
1446 /* Assert SSPHY reset */
Jack Pham80162462013-07-10 11:59:01 -07001447 dwc3_msm_write_reg(mdwc->base, SS_PHY_CTRL_REG, 0x10210082);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301448 usleep_range(2000, 2200);
1449 /* De-assert SSPHY reset - power and ref_clock must be ON */
Jack Pham80162462013-07-10 11:59:01 -07001450 dwc3_msm_write_reg(mdwc->base, SS_PHY_CTRL_REG, 0x10210002);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301451 usleep_range(2000, 2200);
1452 /* Ref clock must be stable now, enable ref clock for HS mode */
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301453 dwc3_msm_write_reg(mdwc->base, SS_PHY_CTRL_REG, 0x11210102);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301454 usleep_range(2000, 2200);
1455 /*
1456 * HSPHY Initialization: Enable UTMI clock and clamp enable HVINTs,
1457 * and disable RETENTION (power-on default is ENABLED)
1458 */
Jack Pham80162462013-07-10 11:59:01 -07001459 dwc3_msm_write_reg(mdwc->base, HS_PHY_CTRL_REG, 0x5220bb2);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301460 usleep_range(2000, 2200);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301461 /* Set XHCI_REV bit (2) to 1 - XHCI version 1.0 */
1462 dwc3_msm_write_reg(mdwc->base, QSCRATCH_GENERAL_CFG, 0x4);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301463 /*
1464 * write HSPHY init value to QSCRATCH reg to set HSPHY parameters like
1465 * VBUS valid threshold, disconnect valid threshold, DC voltage level,
1466 * preempasis and rise/fall time.
1467 */
1468 if (override_phy_init)
Jack Pham80162462013-07-10 11:59:01 -07001469 mdwc->hsphy_init_seq = override_phy_init;
1470 if (mdwc->hsphy_init_seq)
1471 dwc3_msm_write_readback(mdwc->base,
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301472 PARAMETER_OVERRIDE_X_REG, 0x03FFFFFF,
Jack Pham80162462013-07-10 11:59:01 -07001473 mdwc->hsphy_init_seq & 0x03FFFFFF);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301474
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301475 /*
1476 * Enable master clock for RAMs to allow BAM to access RAMs when
1477 * RAM clock gating is enabled via DWC3's GCTL. Otherwise issues
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301478 * are seen where RAM clocks get turned OFF in SS mode
1479 */
Jack Pham80162462013-07-10 11:59:01 -07001480 dwc3_msm_write_reg(mdwc->base, CGCTL_REG,
1481 dwc3_msm_read_reg(mdwc->base, CGCTL_REG) | 0x18);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301482
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301483 /*
1484 * This is required to restore the POR value after userspace
1485 * is done with charger detection.
1486 */
Jack Pham80162462013-07-10 11:59:01 -07001487 mdwc->qscratch_ctl_val =
1488 dwc3_msm_read_reg(mdwc->base, QSCRATCH_CTRL_REG);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301489}
1490
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05301491static void dwc3_msm_notify_event(struct dwc3 *dwc, unsigned event)
1492{
1493 struct dwc3_msm *mdwc = dev_get_drvdata(dwc->dev->parent);
1494
1495 switch (event) {
1496 case DWC3_CONTROLLER_ERROR_EVENT:
1497 dev_info(mdwc->dev, "DWC3_CONTROLLER_ERROR_EVENT received\n");
1498 dwc3_msm_dump_phy_info(mdwc);
Vijayavardhan Vennapusaddd04742013-09-26 19:47:18 +05301499 /*
1500 * schedule work for doing block reset for recovery from erratic
1501 * error event.
1502 */
1503 queue_work(system_nrt_wq, &mdwc->usb_block_reset_work);
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05301504 break;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301505 case DWC3_CONTROLLER_RESET_EVENT:
1506 dev_dbg(mdwc->dev, "DWC3_CONTROLLER_RESET_EVENT received\n");
1507 dwc3_msm_qscratch_reg_init(mdwc, DWC3_CONTROLLER_RESET_EVENT);
1508 break;
1509 case DWC3_CONTROLLER_POST_RESET_EVENT:
1510 dev_dbg(mdwc->dev,
1511 "DWC3_CONTROLLER_POST_RESET_EVENT received\n");
1512 dwc3_msm_qscratch_reg_init(mdwc,
1513 DWC3_CONTROLLER_POST_RESET_EVENT);
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +05301514 dwc->tx_fifo_size = mdwc->tx_fifo_size;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301515 break;
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05301516 default:
1517 dev_dbg(mdwc->dev, "unknown dwc3 event\n");
1518 break;
1519 }
1520}
1521
Jack Pham4b00e702013-07-03 17:10:36 -07001522static void dwc3_msm_block_reset(struct dwc3_ext_xceiv *xceiv, bool core_reset)
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301523{
Jack Pham4b00e702013-07-03 17:10:36 -07001524 struct dwc3_msm *mdwc = container_of(xceiv, struct dwc3_msm, ext_xceiv);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301525 int ret = 0;
1526
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301527 if (core_reset) {
Jack Pham4b00e702013-07-03 17:10:36 -07001528 ret = dwc3_msm_link_clk_reset(mdwc, 1);
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301529 if (ret)
1530 return;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301531
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301532 usleep_range(1000, 1200);
Jack Pham4b00e702013-07-03 17:10:36 -07001533 ret = dwc3_msm_link_clk_reset(mdwc, 0);
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301534 if (ret)
1535 return;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301536
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301537 usleep_range(10000, 12000);
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301538 }
Manu Gautama302f612012-12-18 17:33:06 +05301539
1540 /* Reset the DBM */
Jack Pham62c19a42013-07-09 17:55:09 -07001541 dwc3_msm_dbm_soft_reset(mdwc, 1);
Manu Gautama302f612012-12-18 17:33:06 +05301542 usleep_range(1000, 1200);
Jack Pham62c19a42013-07-09 17:55:09 -07001543 dwc3_msm_dbm_soft_reset(mdwc, 0);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301544}
1545
Vijayavardhan Vennapusaddd04742013-09-26 19:47:18 +05301546static void dwc3_block_reset_usb_work(struct work_struct *w)
1547{
1548 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1549 usb_block_reset_work);
1550
1551 dev_dbg(mdwc->dev, "%s\n", __func__);
1552
1553 dwc3_msm_block_reset(&mdwc->ext_xceiv, true);
1554}
1555
Manu Gautam8c642812012-06-07 10:35:10 +05301556static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1557{
1558 u32 chg_ctrl;
1559
1560 /* Turn off VDP_SRC */
1561 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1562 msleep(20);
1563
1564 /* Before proceeding make sure VDP_SRC is OFF */
1565 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1566 if (chg_ctrl & 0x3F)
1567 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1568 __func__, chg_ctrl);
1569 /*
1570 * Configure DM as current source, DP as current sink
1571 * and enable battery charging comparators.
1572 */
1573 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1574}
1575
Manu Gautama1e331d2013-02-07 14:55:05 +05301576static bool dwc3_chg_det_check_linestate(struct dwc3_msm *mdwc)
1577{
1578 u32 chg_det;
Jack Pham9b4606b2013-04-02 17:32:25 -07001579
1580 if (!prop_chg_detect)
1581 return false;
Manu Gautama1e331d2013-02-07 14:55:05 +05301582
1583 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
Jack Pham9b4606b2013-04-02 17:32:25 -07001584 return chg_det & (3 << 8);
Manu Gautama1e331d2013-02-07 14:55:05 +05301585}
1586
Manu Gautam8c642812012-06-07 10:35:10 +05301587static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1588{
1589 u32 chg_det;
1590 bool ret = false;
1591
1592 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1593 ret = chg_det & 1;
1594
1595 return ret;
1596}
1597
1598static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1599{
1600 /*
1601 * Configure DP as current source, DM as current sink
1602 * and enable battery charging comparators.
1603 */
1604 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1605}
1606
1607static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1608{
1609 u32 chg_state;
1610 bool ret = false;
1611
1612 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1613 ret = chg_state & 2;
1614
1615 return ret;
1616}
1617
1618static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1619{
1620 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1621}
1622
1623static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1624{
1625 /* Data contact detection enable, DCDENB */
1626 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1627}
1628
1629static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1630{
1631 u32 chg_ctrl;
1632
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301633 dwc3_msm_write_reg(mdwc->base, QSCRATCH_CTRL_REG,
1634 mdwc->qscratch_ctl_val);
Manu Gautam8c642812012-06-07 10:35:10 +05301635 /* Clear charger detecting control bits */
1636 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1637
1638 /* Clear alt interrupt latch and enable bits */
1639 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1640 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1641
1642 udelay(100);
1643
1644 /* Before proceeding make sure charger block is RESET */
1645 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1646 if (chg_ctrl & 0x3F)
1647 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1648 __func__, chg_ctrl);
1649}
1650
1651static const char *chg_to_string(enum dwc3_chg_type chg_type)
1652{
1653 switch (chg_type) {
Manu Gautama1e331d2013-02-07 14:55:05 +05301654 case DWC3_SDP_CHARGER: return "USB_SDP_CHARGER";
1655 case DWC3_DCP_CHARGER: return "USB_DCP_CHARGER";
1656 case DWC3_CDP_CHARGER: return "USB_CDP_CHARGER";
1657 case DWC3_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Vijayavardhan Vennapusab11d7fd2013-07-01 16:40:57 +05301658 case DWC3_FLOATED_CHARGER: return "USB_FLOATED_CHARGER";
Vijayavardhan Vennapusaa04e0c92013-06-04 12:37:10 +05301659 default: return "UNKNOWN_CHARGER";
Manu Gautam8c642812012-06-07 10:35:10 +05301660 }
1661}
1662
1663#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1664#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1665#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1666#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1667
1668static void dwc3_chg_detect_work(struct work_struct *w)
1669{
1670 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1671 bool is_dcd = false, tmout, vout;
Vijayavardhan Vennapusab11d7fd2013-07-01 16:40:57 +05301672 static bool dcd;
Manu Gautam8c642812012-06-07 10:35:10 +05301673 unsigned long delay;
1674
1675 dev_dbg(mdwc->dev, "chg detection work\n");
1676 switch (mdwc->chg_state) {
1677 case USB_CHG_STATE_UNDEFINED:
1678 dwc3_chg_block_reset(mdwc);
1679 dwc3_chg_enable_dcd(mdwc);
1680 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1681 mdwc->dcd_retries = 0;
1682 delay = DWC3_CHG_DCD_POLL_TIME;
1683 break;
1684 case USB_CHG_STATE_WAIT_FOR_DCD:
1685 is_dcd = dwc3_chg_check_dcd(mdwc);
1686 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1687 if (is_dcd || tmout) {
Vijayavardhan Vennapusab11d7fd2013-07-01 16:40:57 +05301688 if (is_dcd)
1689 dcd = true;
1690 else
1691 dcd = false;
Manu Gautam8c642812012-06-07 10:35:10 +05301692 dwc3_chg_disable_dcd(mdwc);
Vijayavardhan Vennapusab11d7fd2013-07-01 16:40:57 +05301693 usleep_range(1000, 1200);
Manu Gautama1e331d2013-02-07 14:55:05 +05301694 if (dwc3_chg_det_check_linestate(mdwc)) {
Vijayavardhan Vennapusab11d7fd2013-07-01 16:40:57 +05301695 mdwc->charger.chg_type =
Manu Gautama1e331d2013-02-07 14:55:05 +05301696 DWC3_PROPRIETARY_CHARGER;
1697 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1698 delay = 0;
1699 break;
1700 }
Manu Gautam8c642812012-06-07 10:35:10 +05301701 dwc3_chg_enable_primary_det(mdwc);
1702 delay = DWC3_CHG_PRIMARY_DET_TIME;
1703 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1704 } else {
1705 delay = DWC3_CHG_DCD_POLL_TIME;
1706 }
1707 break;
1708 case USB_CHG_STATE_DCD_DONE:
1709 vout = dwc3_chg_det_check_output(mdwc);
1710 if (vout) {
1711 dwc3_chg_enable_secondary_det(mdwc);
1712 delay = DWC3_CHG_SECONDARY_DET_TIME;
1713 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1714 } else {
Vijayavardhan Vennapusab11d7fd2013-07-01 16:40:57 +05301715 /*
1716 * Detect floating charger only if propreitary
1717 * charger detection is enabled.
1718 */
1719 if (!dcd && prop_chg_detect)
1720 mdwc->charger.chg_type =
1721 DWC3_FLOATED_CHARGER;
1722 else
1723 mdwc->charger.chg_type = DWC3_SDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301724 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1725 delay = 0;
1726 }
1727 break;
1728 case USB_CHG_STATE_PRIMARY_DONE:
1729 vout = dwc3_chg_det_check_output(mdwc);
1730 if (vout)
Manu Gautama1e331d2013-02-07 14:55:05 +05301731 mdwc->charger.chg_type = DWC3_DCP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301732 else
Manu Gautama1e331d2013-02-07 14:55:05 +05301733 mdwc->charger.chg_type = DWC3_CDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301734 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1735 /* fall through */
1736 case USB_CHG_STATE_SECONDARY_DONE:
1737 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1738 /* fall through */
1739 case USB_CHG_STATE_DETECTED:
1740 dwc3_chg_block_reset(mdwc);
Manu Gautama48296e2012-12-05 17:37:56 +05301741 /* Enable VDP_SRC */
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301742 if (mdwc->charger.chg_type == DWC3_DCP_CHARGER) {
Manu Gautama48296e2012-12-05 17:37:56 +05301743 dwc3_msm_write_readback(mdwc->base,
1744 CHARGING_DET_CTRL_REG, 0x1F, 0x10);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301745 if (mdwc->ext_chg_opened) {
1746 init_completion(&mdwc->ext_chg_wait);
1747 mdwc->ext_chg_active = true;
1748 }
1749 }
Manu Gautam8c642812012-06-07 10:35:10 +05301750 dev_dbg(mdwc->dev, "chg_type = %s\n",
1751 chg_to_string(mdwc->charger.chg_type));
1752 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1753 &mdwc->charger);
1754 return;
1755 default:
1756 return;
1757 }
1758
1759 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1760}
1761
1762static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1763{
Jack Phamea382b72013-07-09 17:50:20 -07001764 struct dwc3_msm *mdwc = container_of(charger, struct dwc3_msm, charger);
Manu Gautam8c642812012-06-07 10:35:10 +05301765
1766 if (start == false) {
Jack Pham9354c6a2012-12-20 19:19:32 -08001767 dev_dbg(mdwc->dev, "canceling charging detection work\n");
Manu Gautam8c642812012-06-07 10:35:10 +05301768 cancel_delayed_work_sync(&mdwc->chg_work);
1769 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1770 charger->chg_type = DWC3_INVALID_CHARGER;
1771 return;
1772 }
1773
1774 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1775 charger->chg_type = DWC3_INVALID_CHARGER;
1776 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1777}
1778
Manu Gautamb5067272012-07-02 09:53:41 +05301779static int dwc3_msm_suspend(struct dwc3_msm *mdwc)
1780{
Manu Gautam2617deb2012-08-31 17:50:06 -07001781 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301782 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301783 bool host_bus_suspend;
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301784 bool host_ss_active;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301785 bool host_ss_suspend;
Manu Gautam2617deb2012-08-31 17:50:06 -07001786
Manu Gautamb5067272012-07-02 09:53:41 +05301787 dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__);
1788
1789 if (atomic_read(&mdwc->in_lpm)) {
1790 dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__);
1791 return 0;
1792 }
1793
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301794 host_ss_active = dwc3_msm_read_reg(mdwc->base, USB3_PORTSC) & PORT_PE;
Manu Gautama48296e2012-12-05 17:37:56 +05301795 if (mdwc->hs_phy_irq)
1796 disable_irq(mdwc->hs_phy_irq);
1797
Manu Gautam98013c22012-11-20 17:42:42 +05301798 if (cancel_delayed_work_sync(&mdwc->chg_work))
1799 dev_dbg(mdwc->dev, "%s: chg_work was pending\n", __func__);
1800 if (mdwc->chg_state != USB_CHG_STATE_DETECTED) {
1801 /* charger detection wasn't complete; re-init flags */
1802 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1803 mdwc->charger.chg_type = DWC3_INVALID_CHARGER;
Manu Gautama48296e2012-12-05 17:37:56 +05301804 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG,
1805 0x37, 0x0);
Manu Gautam98013c22012-11-20 17:42:42 +05301806 }
1807
Manu Gautam840f4fe2013-04-16 16:50:30 +05301808 dcp = ((mdwc->charger.chg_type == DWC3_DCP_CHARGER) ||
Vijayavardhan Vennapusac4974862013-07-23 17:36:37 +05301809 (mdwc->charger.chg_type == DWC3_PROPRIETARY_CHARGER) ||
1810 (mdwc->charger.chg_type == DWC3_FLOATED_CHARGER));
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301811 host_bus_suspend = mdwc->host_mode == 1;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301812 host_ss_suspend = host_bus_suspend && host_ss_active;
Manu Gautam377821c2012-09-28 16:53:24 +05301813
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301814 if (!dcp && !host_bus_suspend)
1815 dwc3_msm_write_reg(mdwc->base, QSCRATCH_CTRL_REG,
1816 mdwc->qscratch_ctl_val);
1817
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301818 /* Sequence to put SSPHY in low power state:
1819 * 1. Clear REF_SS_PHY_EN in SS_PHY_CTRL_REG
1820 * 2. Clear REF_USE_PAD in SS_PHY_CTRL_REG
1821 * 3. Set TEST_POWERED_DOWN in SS_PHY_CTRL_REG to enable PHY retention
1822 * 4. Disable SSPHY ref clk
1823 */
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301824 if (!host_ss_suspend) {
1825 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8),
1826 0x0);
1827 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28),
1828 0x0);
1829 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26),
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301830 (1 << 26));
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301831 }
Manu Gautam377821c2012-09-28 16:53:24 +05301832 usleep_range(1000, 1200);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301833 if (!host_ss_suspend)
1834 clk_disable_unprepare(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301835
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301836 if (host_bus_suspend) {
1837 /* Sequence for host bus suspend case:
1838 * 1. Set suspend and sleep bits in GUSB2PHYCONFIG reg
1839 * 2. Clear interrupt latch register and enable BSV, ID HV intr
1840 * 3. Enable DP and DM HV interrupts in ALT_INTERRUPT_EN_REG
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301841 */
1842 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1843 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1844 0x00000140);
1845 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1846 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1847 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1848 0x18000, 0x18000);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301849 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0xFC0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301850 udelay(5);
1851 } else {
1852 /* Sequence to put hardware in low power state:
1853 * 1. Set OTGDISABLE to disable OTG block in HSPHY (saves power)
1854 * 2. Clear charger detection control fields (performed above)
1855 * 3. SUSPEND PHY and turn OFF core clock after some delay
1856 * 4. Clear interrupt latch register and enable BSV, ID HV intr
1857 * 5. Enable PHY retention
1858 */
1859 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x1000,
1860 0x1000);
1861 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1862 0xC00000, 0x800000);
1863 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1864 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1865 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1866 0x18000, 0x18000);
1867 if (!dcp)
1868 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1869 0x2, 0x0);
1870 }
Manu Gautam377821c2012-09-28 16:53:24 +05301871
1872 /* make sure above writes are completed before turning off clocks */
1873 wmb();
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001874
1875 /* remove vote for controller power collapse */
1876 if (!host_bus_suspend)
1877 dwc3_msm_config_gdsc(mdwc, 0);
1878
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301879 if (!host_ss_suspend) {
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301880 clk_disable_unprepare(mdwc->core_clk);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301881 mdwc->lpm_flags |= MDWC3_PHY_REF_AND_CORECLK_OFF;
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301882 }
Manu Gautam377821c2012-09-28 16:53:24 +05301883 clk_disable_unprepare(mdwc->iface_clk);
1884
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301885 if (!host_bus_suspend)
Jack Pham22698b82013-02-13 17:45:06 -08001886 clk_disable_unprepare(mdwc->utmi_clk);
1887
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301888 if (!host_bus_suspend) {
Jack Pham22698b82013-02-13 17:45:06 -08001889 /* USB PHY no more requires TCXO */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301890 clk_disable_unprepare(mdwc->xo_clk);
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301891 mdwc->lpm_flags |= MDWC3_TCXO_SHUTDOWN;
Jack Pham22698b82013-02-13 17:45:06 -08001892 }
Manu Gautamb5067272012-07-02 09:53:41 +05301893
Manu Gautam2617deb2012-08-31 17:50:06 -07001894 if (mdwc->bus_perf_client) {
1895 ret = msm_bus_scale_client_update_request(
1896 mdwc->bus_perf_client, 0);
1897 if (ret)
1898 dev_err(mdwc->dev, "Failed to reset bus bw vote\n");
1899 }
1900
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301901 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1902 !host_bus_suspend)
Jack Pham4b00e702013-07-03 17:10:36 -07001903 dwc3_hsusb_ldo_enable(mdwc, 0);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301904
Jack Pham4b00e702013-07-03 17:10:36 -07001905 dwc3_ssusb_ldo_enable(mdwc, 0);
1906 dwc3_ssusb_config_vddcx(mdwc, 0);
Manu Gautam840f4fe2013-04-16 16:50:30 +05301907 if (!host_bus_suspend && !dcp)
Jack Pham4b00e702013-07-03 17:10:36 -07001908 dwc3_hsusb_config_vddcx(mdwc, 0);
Jack Pham924cbe872013-07-10 16:40:55 -07001909 pm_relax(mdwc->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05301910 atomic_set(&mdwc->in_lpm, 1);
Manu Gautam377821c2012-09-28 16:53:24 +05301911
Manu Gautamb5067272012-07-02 09:53:41 +05301912 dev_info(mdwc->dev, "DWC3 in low power mode\n");
1913
Manu Gautam840f4fe2013-04-16 16:50:30 +05301914 if (mdwc->hs_phy_irq) {
Manu Gautama48296e2012-12-05 17:37:56 +05301915 enable_irq(mdwc->hs_phy_irq);
Manu Gautam840f4fe2013-04-16 16:50:30 +05301916 /* with DCP we dont require wakeup using HS_PHY_IRQ */
1917 if (dcp)
1918 disable_irq_wake(mdwc->hs_phy_irq);
1919 }
Manu Gautama48296e2012-12-05 17:37:56 +05301920
Manu Gautamb5067272012-07-02 09:53:41 +05301921 return 0;
1922}
1923
1924static int dwc3_msm_resume(struct dwc3_msm *mdwc)
1925{
Manu Gautam2617deb2012-08-31 17:50:06 -07001926 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301927 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301928 bool host_bus_suspend;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301929 bool resume_from_core_clk_off = false;
Manu Gautam2617deb2012-08-31 17:50:06 -07001930
Manu Gautamb5067272012-07-02 09:53:41 +05301931 dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__);
1932
1933 if (!atomic_read(&mdwc->in_lpm)) {
1934 dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__);
1935 return 0;
1936 }
1937
Jack Pham924cbe872013-07-10 16:40:55 -07001938 pm_stay_awake(mdwc->dev);
Manu Gautam377821c2012-09-28 16:53:24 +05301939
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301940 if (mdwc->lpm_flags & MDWC3_PHY_REF_AND_CORECLK_OFF)
1941 resume_from_core_clk_off = true;
1942
Manu Gautam2617deb2012-08-31 17:50:06 -07001943 if (mdwc->bus_perf_client) {
1944 ret = msm_bus_scale_client_update_request(
1945 mdwc->bus_perf_client, 1);
1946 if (ret)
1947 dev_err(mdwc->dev, "Failed to vote for bus scaling\n");
1948 }
1949
Manu Gautam840f4fe2013-04-16 16:50:30 +05301950 dcp = ((mdwc->charger.chg_type == DWC3_DCP_CHARGER) ||
Vijayavardhan Vennapusac4974862013-07-23 17:36:37 +05301951 (mdwc->charger.chg_type == DWC3_PROPRIETARY_CHARGER) ||
1952 (mdwc->charger.chg_type == DWC3_FLOATED_CHARGER));
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301953 host_bus_suspend = mdwc->host_mode == 1;
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301954
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301955 if (mdwc->lpm_flags & MDWC3_TCXO_SHUTDOWN) {
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301956 /* Vote for TCXO while waking up USB HSPHY */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301957 ret = clk_prepare_enable(mdwc->xo_clk);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301958 if (ret)
1959 dev_err(mdwc->dev, "%s failed to vote TCXO buffer%d\n",
1960 __func__, ret);
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301961 mdwc->lpm_flags &= ~MDWC3_TCXO_SHUTDOWN;
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301962 }
1963
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001964 /* add vote for controller power collapse */
1965 if (!host_bus_suspend)
1966 dwc3_msm_config_gdsc(mdwc, 1);
1967
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301968 if (!host_bus_suspend)
1969 clk_prepare_enable(mdwc->utmi_clk);
1970
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301971 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1972 !host_bus_suspend)
Jack Pham4b00e702013-07-03 17:10:36 -07001973 dwc3_hsusb_ldo_enable(mdwc, 1);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301974
Jack Pham4b00e702013-07-03 17:10:36 -07001975 dwc3_ssusb_ldo_enable(mdwc, 1);
1976 dwc3_ssusb_config_vddcx(mdwc, 1);
Jack Pham22698b82013-02-13 17:45:06 -08001977
Manu Gautam840f4fe2013-04-16 16:50:30 +05301978 if (!host_bus_suspend && !dcp)
Jack Pham4b00e702013-07-03 17:10:36 -07001979 dwc3_hsusb_config_vddcx(mdwc, 1);
Jack Pham22698b82013-02-13 17:45:06 -08001980
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301981 if (mdwc->lpm_flags & MDWC3_PHY_REF_AND_CORECLK_OFF)
1982 clk_prepare_enable(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301983 usleep_range(1000, 1200);
1984
Manu Gautam3e9ad352012-08-16 14:44:47 -07001985 clk_prepare_enable(mdwc->iface_clk);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301986 if (mdwc->lpm_flags & MDWC3_PHY_REF_AND_CORECLK_OFF) {
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301987 clk_prepare_enable(mdwc->core_clk);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301988 mdwc->lpm_flags &= ~MDWC3_PHY_REF_AND_CORECLK_OFF;
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301989 }
Manu Gautam377821c2012-09-28 16:53:24 +05301990
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301991 if (host_bus_suspend) {
1992 /* Disable HV interrupt */
1993 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1994 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1995 0x18000, 0x0);
1996 /* Clear interrupt latch register */
1997 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301998
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301999 /* Disable DP and DM HV interrupt */
2000 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x000);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302001 } else {
2002 /* Disable HV interrupt */
2003 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
2004 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
2005 0x18000, 0x0);
2006 /* Disable Retention */
2007 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x2, 0x2);
2008
2009 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
2010 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
2011 0xF0000000);
2012 /* 10usec delay required before de-asserting PHY RESET */
2013 udelay(10);
2014 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
2015 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) &
2016 0x7FFFFFFF);
2017
2018 /* Bring PHY out of suspend */
2019 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0xC00000,
2020 0x0);
2021
2022 }
Manu Gautamb5067272012-07-02 09:53:41 +05302023
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05302024 if (resume_from_core_clk_off) {
2025 /* Assert SS PHY RESET */
2026 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7),
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05302027 (1 << 7));
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05302028 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28),
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05302029 (1 << 28));
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05302030 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8),
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05302031 (1 << 8));
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05302032 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26),
2033 0x0);
2034 /* 10usec delay required before de-asserting SS PHY RESET */
2035 udelay(10);
2036 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7),
2037 0x0);
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05302038
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05302039 /*
2040 * Reinitilize SSPHY parameters as SS_PHY RESET will reset
2041 * the internal registers to default values.
2042 */
2043 dwc3_msm_ss_phy_reg_init(mdwc);
2044 }
Manu Gautamb5067272012-07-02 09:53:41 +05302045 atomic_set(&mdwc->in_lpm, 0);
Manu Gautam377821c2012-09-28 16:53:24 +05302046
2047 /* match disable_irq call from isr */
2048 if (mdwc->lpm_irq_seen && mdwc->hs_phy_irq) {
2049 enable_irq(mdwc->hs_phy_irq);
2050 mdwc->lpm_irq_seen = false;
2051 }
Manu Gautam840f4fe2013-04-16 16:50:30 +05302052 /* it must DCP disconnect, re-enable HS_PHY wakeup IRQ */
2053 if (mdwc->hs_phy_irq && dcp)
2054 enable_irq_wake(mdwc->hs_phy_irq);
Manu Gautam377821c2012-09-28 16:53:24 +05302055
Manu Gautamb5067272012-07-02 09:53:41 +05302056 dev_info(mdwc->dev, "DWC3 exited from low power mode\n");
2057
2058 return 0;
2059}
2060
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302061static void dwc3_wait_for_ext_chg_done(struct dwc3_msm *mdwc)
2062{
2063 unsigned long t;
2064
2065 /*
2066 * Defer next cable connect event till external charger
2067 * detection is completed.
2068 */
2069
2070 if (mdwc->ext_chg_active && (mdwc->ext_xceiv.bsv ||
2071 !mdwc->ext_xceiv.id)) {
2072
2073 dev_dbg(mdwc->dev, "before ext chg wait\n");
2074
2075 t = wait_for_completion_timeout(&mdwc->ext_chg_wait,
2076 msecs_to_jiffies(3000));
2077 if (!t)
2078 dev_err(mdwc->dev, "ext chg wait timeout\n");
2079 else
2080 dev_dbg(mdwc->dev, "ext chg wait done\n");
2081 }
2082
2083}
2084
Manu Gautamb5067272012-07-02 09:53:41 +05302085static void dwc3_resume_work(struct work_struct *w)
2086{
2087 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
2088 resume_work.work);
2089
2090 dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__);
2091 /* handle any event that was queued while work was already running */
2092 if (!atomic_read(&mdwc->in_lpm)) {
2093 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302094 if (mdwc->otg_xceiv) {
2095 dwc3_wait_for_ext_chg_done(mdwc);
Manu Gautamb5067272012-07-02 09:53:41 +05302096 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
2097 DWC3_EVENT_XCEIV_STATE);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302098 }
Manu Gautamb5067272012-07-02 09:53:41 +05302099 return;
2100 }
2101
2102 /* bail out if system resume in process, else initiate RESUME */
2103 if (atomic_read(&mdwc->pm_suspended)) {
2104 mdwc->resume_pending = true;
2105 } else {
2106 pm_runtime_get_sync(mdwc->dev);
2107 if (mdwc->otg_xceiv)
2108 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
2109 DWC3_EVENT_PHY_RESUME);
Manu Gautambb825d72013-03-12 16:25:42 +05302110 pm_runtime_put_noidle(mdwc->dev);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302111 if (mdwc->otg_xceiv && (mdwc->ext_xceiv.otg_capability)) {
2112 dwc3_wait_for_ext_chg_done(mdwc);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302113 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
2114 DWC3_EVENT_XCEIV_STATE);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302115 }
Manu Gautamb5067272012-07-02 09:53:41 +05302116 }
2117}
2118
Jack Pham0fc12332012-11-19 13:14:22 -08002119static u32 debug_id = true, debug_bsv, debug_connect;
Manu Gautamb5067272012-07-02 09:53:41 +05302120
2121static int dwc3_connect_show(struct seq_file *s, void *unused)
2122{
2123 if (debug_connect)
2124 seq_printf(s, "true\n");
2125 else
2126 seq_printf(s, "false\n");
2127
2128 return 0;
2129}
2130
2131static int dwc3_connect_open(struct inode *inode, struct file *file)
2132{
2133 return single_open(file, dwc3_connect_show, inode->i_private);
2134}
2135
2136static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf,
2137 size_t count, loff_t *ppos)
2138{
2139 struct seq_file *s = file->private_data;
2140 struct dwc3_msm *mdwc = s->private;
2141 char buf[8];
2142
2143 memset(buf, 0x00, sizeof(buf));
2144
2145 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2146 return -EFAULT;
2147
2148 if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) {
2149 debug_connect = true;
2150 } else {
2151 debug_connect = debug_bsv = false;
2152 debug_id = true;
2153 }
2154
2155 mdwc->ext_xceiv.bsv = debug_bsv;
2156 mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND;
2157
2158 if (atomic_read(&mdwc->in_lpm)) {
2159 dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__);
2160 dwc3_resume_work(&mdwc->resume_work.work);
2161 } else {
2162 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
2163 if (mdwc->otg_xceiv)
2164 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
2165 DWC3_EVENT_XCEIV_STATE);
2166 }
2167
2168 return count;
2169}
2170
2171const struct file_operations dwc3_connect_fops = {
2172 .open = dwc3_connect_open,
2173 .read = seq_read,
2174 .write = dwc3_connect_write,
2175 .llseek = seq_lseek,
2176 .release = single_release,
2177};
2178
2179static struct dentry *dwc3_debugfs_root;
2180
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302181static void dwc3_msm_debugfs_init(struct dwc3_msm *mdwc)
Manu Gautamb5067272012-07-02 09:53:41 +05302182{
2183 dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL);
2184
2185 if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root))
2186 return;
2187
2188 if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302189 &debug_id))
Manu Gautamb5067272012-07-02 09:53:41 +05302190 goto error;
2191
2192 if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302193 &debug_bsv))
Manu Gautamb5067272012-07-02 09:53:41 +05302194 goto error;
2195
2196 if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR,
2197 dwc3_debugfs_root, mdwc, &dwc3_connect_fops))
2198 goto error;
2199
2200 return;
2201
2202error:
2203 debugfs_remove_recursive(dwc3_debugfs_root);
2204}
Manu Gautam8c642812012-06-07 10:35:10 +05302205
Manu Gautam377821c2012-09-28 16:53:24 +05302206static irqreturn_t msm_dwc3_irq(int irq, void *data)
2207{
2208 struct dwc3_msm *mdwc = data;
2209
2210 if (atomic_read(&mdwc->in_lpm)) {
2211 dev_dbg(mdwc->dev, "%s received in LPM\n", __func__);
2212 mdwc->lpm_irq_seen = true;
2213 disable_irq_nosync(irq);
2214 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
2215 } else {
2216 pr_info_ratelimited("%s: IRQ outside LPM\n", __func__);
2217 }
2218
2219 return IRQ_HANDLED;
2220}
2221
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302222static int dwc3_msm_power_get_property_usb(struct power_supply *psy,
2223 enum power_supply_property psp,
2224 union power_supply_propval *val)
2225{
2226 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
2227 usb_psy);
2228 switch (psp) {
2229 case POWER_SUPPLY_PROP_SCOPE:
2230 val->intval = mdwc->host_mode;
2231 break;
Pavankumar Kondetifbd4b142013-07-16 11:13:05 +05302232 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
2233 val->intval = mdwc->voltage_max;
2234 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302235 case POWER_SUPPLY_PROP_CURRENT_MAX:
2236 val->intval = mdwc->current_max;
2237 break;
2238 case POWER_SUPPLY_PROP_PRESENT:
2239 val->intval = mdwc->vbus_active;
2240 break;
2241 case POWER_SUPPLY_PROP_ONLINE:
2242 val->intval = mdwc->online;
2243 break;
Manu Gautamfa40cae2013-03-01 16:37:12 +05302244 case POWER_SUPPLY_PROP_TYPE:
2245 val->intval = psy->type;
2246 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302247 default:
2248 return -EINVAL;
2249 }
2250 return 0;
2251}
2252
2253static int dwc3_msm_power_set_property_usb(struct power_supply *psy,
2254 enum power_supply_property psp,
2255 const union power_supply_propval *val)
2256{
2257 static bool init;
2258 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
2259 usb_psy);
2260
2261 switch (psp) {
2262 case POWER_SUPPLY_PROP_SCOPE:
2263 mdwc->host_mode = val->intval;
2264 break;
2265 /* Process PMIC notification in PRESENT prop */
2266 case POWER_SUPPLY_PROP_PRESENT:
2267 dev_dbg(mdwc->dev, "%s: notify xceiv event\n", __func__);
Jack Pham9354c6a2012-12-20 19:19:32 -08002268 if (mdwc->otg_xceiv && !mdwc->ext_inuse &&
2269 (mdwc->ext_xceiv.otg_capability || !init)) {
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302270 mdwc->ext_xceiv.bsv = val->intval;
Manu Gautamf71d9cb2013-02-07 13:52:12 +05302271 queue_delayed_work(system_nrt_wq,
Jack Pham4d91aab2013-03-08 10:02:16 -08002272 &mdwc->resume_work, 20);
Jack Pham9354c6a2012-12-20 19:19:32 -08002273
2274 if (!init)
2275 init = true;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302276 }
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302277 mdwc->vbus_active = val->intval;
2278 break;
2279 case POWER_SUPPLY_PROP_ONLINE:
2280 mdwc->online = val->intval;
2281 break;
Pavankumar Kondetifbd4b142013-07-16 11:13:05 +05302282 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
2283 mdwc->voltage_max = val->intval;
2284 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302285 case POWER_SUPPLY_PROP_CURRENT_MAX:
2286 mdwc->current_max = val->intval;
2287 break;
Manu Gautamfa40cae2013-03-01 16:37:12 +05302288 case POWER_SUPPLY_PROP_TYPE:
2289 psy->type = val->intval;
2290 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302291 default:
2292 return -EINVAL;
2293 }
2294
2295 power_supply_changed(&mdwc->usb_psy);
2296 return 0;
2297}
2298
Jack Pham9354c6a2012-12-20 19:19:32 -08002299static void dwc3_msm_external_power_changed(struct power_supply *psy)
2300{
2301 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm, usb_psy);
2302 union power_supply_propval ret = {0,};
2303
2304 if (!mdwc->ext_vbus_psy)
2305 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2306
2307 if (!mdwc->ext_vbus_psy) {
2308 pr_err("%s: Unable to get ext_vbus power_supply\n", __func__);
2309 return;
2310 }
2311
2312 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2313 POWER_SUPPLY_PROP_ONLINE, &ret);
2314 if (ret.intval) {
2315 dwc3_start_chg_det(&mdwc->charger, false);
2316 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2317 POWER_SUPPLY_PROP_CURRENT_MAX, &ret);
2318 power_supply_set_current_limit(&mdwc->usb_psy, ret.intval);
2319 }
2320
2321 power_supply_set_online(&mdwc->usb_psy, ret.intval);
2322 power_supply_changed(&mdwc->usb_psy);
2323}
2324
Pavankumar Kondetifbd4b142013-07-16 11:13:05 +05302325static int
2326dwc3_msm_property_is_writeable(struct power_supply *psy,
2327 enum power_supply_property psp)
2328{
2329 switch (psp) {
2330 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
2331 return 1;
2332 default:
2333 break;
2334 }
2335
2336 return 0;
2337}
2338
Jack Pham9354c6a2012-12-20 19:19:32 -08002339
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302340static char *dwc3_msm_pm_power_supplied_to[] = {
2341 "battery",
2342};
2343
2344static enum power_supply_property dwc3_msm_pm_power_props_usb[] = {
2345 POWER_SUPPLY_PROP_PRESENT,
2346 POWER_SUPPLY_PROP_ONLINE,
Pavankumar Kondetifbd4b142013-07-16 11:13:05 +05302347 POWER_SUPPLY_PROP_VOLTAGE_MAX,
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302348 POWER_SUPPLY_PROP_CURRENT_MAX,
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302349 POWER_SUPPLY_PROP_TYPE,
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302350 POWER_SUPPLY_PROP_SCOPE,
2351};
2352
Jack Phamfadd6432012-12-07 19:03:41 -08002353static void dwc3_init_adc_work(struct work_struct *w);
2354
Jack Phamb7209152013-07-03 17:04:53 -07002355static void dwc3_ext_notify_online(void *ctx, int on)
Jack Phamfadd6432012-12-07 19:03:41 -08002356{
Jack Phamb7209152013-07-03 17:04:53 -07002357 struct dwc3_msm *mdwc = ctx;
Jack Phamf12b7e12012-12-28 14:27:26 -08002358 bool notify_otg = false;
Jack Phamfadd6432012-12-07 19:03:41 -08002359
2360 if (!mdwc) {
2361 pr_err("%s: DWC3 driver already removed\n", __func__);
2362 return;
2363 }
2364
2365 dev_dbg(mdwc->dev, "notify %s%s\n", on ? "" : "dis", "connected");
2366
Jack Pham9354c6a2012-12-20 19:19:32 -08002367 if (!mdwc->ext_vbus_psy)
2368 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2369
2370 mdwc->ext_inuse = on;
Jack Phamf12b7e12012-12-28 14:27:26 -08002371 if (on) {
2372 /* force OTG to exit B-peripheral state */
2373 mdwc->ext_xceiv.bsv = false;
2374 notify_otg = true;
Jack Pham9354c6a2012-12-20 19:19:32 -08002375 dwc3_start_chg_det(&mdwc->charger, false);
Jack Phamf12b7e12012-12-28 14:27:26 -08002376 } else {
2377 /* external client offline; tell OTG about cached ID/BSV */
2378 if (mdwc->ext_xceiv.id != mdwc->id_state) {
2379 mdwc->ext_xceiv.id = mdwc->id_state;
2380 notify_otg = true;
2381 }
2382
2383 mdwc->ext_xceiv.bsv = mdwc->vbus_active;
2384 notify_otg |= mdwc->vbus_active;
2385 }
Jack Pham9354c6a2012-12-20 19:19:32 -08002386
2387 if (mdwc->ext_vbus_psy)
2388 power_supply_set_present(mdwc->ext_vbus_psy, on);
Jack Phamf12b7e12012-12-28 14:27:26 -08002389
2390 if (notify_otg)
2391 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
Jack Phamfadd6432012-12-07 19:03:41 -08002392}
2393
Jack Pham0cca9412013-03-08 13:22:42 -08002394static void dwc3_id_work(struct work_struct *w)
Jack Phamfadd6432012-12-07 19:03:41 -08002395{
Jack Pham0cca9412013-03-08 13:22:42 -08002396 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, id_work);
Jack Pham5c585062013-03-25 18:39:12 -07002397 int ret;
Jack Phamfadd6432012-12-07 19:03:41 -08002398
Jack Pham0cca9412013-03-08 13:22:42 -08002399 /* Give external client a chance to handle */
Jack Pham5c585062013-03-25 18:39:12 -07002400 if (!mdwc->ext_inuse && usb_ext) {
2401 if (mdwc->pmic_id_irq)
2402 disable_irq(mdwc->pmic_id_irq);
2403
2404 ret = usb_ext->notify(usb_ext->ctxt, mdwc->id_state,
Jack Phamb7209152013-07-03 17:04:53 -07002405 dwc3_ext_notify_online, mdwc);
Jack Pham5c585062013-03-25 18:39:12 -07002406 dev_dbg(mdwc->dev, "%s: external handler returned %d\n",
2407 __func__, ret);
2408
2409 if (mdwc->pmic_id_irq) {
Vijayavardhan Vennapusa242eaf02013-07-01 12:39:31 +05302410 unsigned long flags;
2411 local_irq_save(flags);
Jack Pham5c585062013-03-25 18:39:12 -07002412 /* ID may have changed while IRQ disabled; update it */
2413 mdwc->id_state = !!irq_read_line(mdwc->pmic_id_irq);
Vijayavardhan Vennapusa242eaf02013-07-01 12:39:31 +05302414 local_irq_restore(flags);
Jack Pham5c585062013-03-25 18:39:12 -07002415 enable_irq(mdwc->pmic_id_irq);
Jack Pham0cca9412013-03-08 13:22:42 -08002416 }
Jack Pham5c585062013-03-25 18:39:12 -07002417
2418 mdwc->ext_inuse = (ret == 0);
Jack Pham0cca9412013-03-08 13:22:42 -08002419 }
Jack Phamfadd6432012-12-07 19:03:41 -08002420
Jack Pham0cca9412013-03-08 13:22:42 -08002421 if (!mdwc->ext_inuse) { /* notify OTG */
2422 mdwc->ext_xceiv.id = mdwc->id_state;
2423 dwc3_resume_work(&mdwc->resume_work.work);
2424 }
2425}
2426
2427static irqreturn_t dwc3_pmic_id_irq(int irq, void *data)
2428{
2429 struct dwc3_msm *mdwc = data;
Jack Pham5c585062013-03-25 18:39:12 -07002430 enum dwc3_id_state id;
Jack Pham0cca9412013-03-08 13:22:42 -08002431
2432 /* If we can't read ID line state for some reason, treat it as float */
Jack Pham5c585062013-03-25 18:39:12 -07002433 id = !!irq_read_line(irq);
2434 if (mdwc->id_state != id) {
2435 mdwc->id_state = id;
2436 queue_work(system_nrt_wq, &mdwc->id_work);
2437 }
Jack Pham0cca9412013-03-08 13:22:42 -08002438
2439 return IRQ_HANDLED;
Jack Phamfadd6432012-12-07 19:03:41 -08002440}
2441
Jack Pham0fc12332012-11-19 13:14:22 -08002442static void dwc3_adc_notification(enum qpnp_tm_state state, void *ctx)
2443{
2444 struct dwc3_msm *mdwc = ctx;
2445
2446 if (state >= ADC_TM_STATE_NUM) {
2447 pr_err("%s: invalid notification %d\n", __func__, state);
2448 return;
2449 }
2450
2451 dev_dbg(mdwc->dev, "%s: state = %s\n", __func__,
2452 state == ADC_TM_HIGH_STATE ? "high" : "low");
2453
Jack Phamf12b7e12012-12-28 14:27:26 -08002454 /* save ID state, but don't necessarily notify OTG */
Jack Pham0fc12332012-11-19 13:14:22 -08002455 if (state == ADC_TM_HIGH_STATE) {
Jack Phamf12b7e12012-12-28 14:27:26 -08002456 mdwc->id_state = DWC3_ID_FLOAT;
Jack Pham0fc12332012-11-19 13:14:22 -08002457 mdwc->adc_param.state_request = ADC_TM_LOW_THR_ENABLE;
2458 } else {
Jack Phamf12b7e12012-12-28 14:27:26 -08002459 mdwc->id_state = DWC3_ID_GROUND;
Jack Pham0fc12332012-11-19 13:14:22 -08002460 mdwc->adc_param.state_request = ADC_TM_HIGH_THR_ENABLE;
2461 }
2462
Jack Pham0cca9412013-03-08 13:22:42 -08002463 dwc3_id_work(&mdwc->id_work);
2464
Jack Phamfadd6432012-12-07 19:03:41 -08002465 /* re-arm ADC interrupt */
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002466 qpnp_adc_tm_usbid_configure(mdwc->adc_tm_dev, &mdwc->adc_param);
Jack Pham0fc12332012-11-19 13:14:22 -08002467}
2468
2469static void dwc3_init_adc_work(struct work_struct *w)
2470{
2471 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
2472 init_adc_work.work);
2473 int ret;
2474
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002475 mdwc->adc_tm_dev = qpnp_get_adc_tm(mdwc->dev, "dwc_usb3-adc_tm");
2476 if (IS_ERR(mdwc->adc_tm_dev)) {
2477 if (PTR_ERR(mdwc->adc_tm_dev) == -EPROBE_DEFER)
2478 queue_delayed_work(system_nrt_wq, to_delayed_work(w),
Jack Pham90b4d122012-12-13 11:46:22 -08002479 msecs_to_jiffies(100));
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002480 else
2481 mdwc->adc_tm_dev = NULL;
2482
Jack Pham0fc12332012-11-19 13:14:22 -08002483 return;
2484 }
2485
2486 mdwc->adc_param.low_thr = adc_low_threshold;
2487 mdwc->adc_param.high_thr = adc_high_threshold;
2488 mdwc->adc_param.timer_interval = adc_meas_interval;
2489 mdwc->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadossa3e35512013-02-22 17:06:07 -08002490 mdwc->adc_param.btm_ctx = mdwc;
Jack Pham0fc12332012-11-19 13:14:22 -08002491 mdwc->adc_param.threshold_notification = dwc3_adc_notification;
2492
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002493 ret = qpnp_adc_tm_usbid_configure(mdwc->adc_tm_dev, &mdwc->adc_param);
Jack Pham0fc12332012-11-19 13:14:22 -08002494 if (ret) {
2495 dev_err(mdwc->dev, "%s: request ADC error %d\n", __func__, ret);
2496 return;
2497 }
2498
2499 mdwc->id_adc_detect = true;
2500}
2501
2502static ssize_t adc_enable_show(struct device *dev,
2503 struct device_attribute *attr, char *buf)
2504{
Jack Pham84fc1ac2013-07-09 17:51:41 -07002505 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2506
2507 if (!mdwc)
2508 return -EINVAL;
2509
2510 return snprintf(buf, PAGE_SIZE, "%s\n", mdwc->id_adc_detect ?
Jack Pham0fc12332012-11-19 13:14:22 -08002511 "enabled" : "disabled");
2512}
2513
2514static ssize_t adc_enable_store(struct device *dev,
2515 struct device_attribute *attr, const char
2516 *buf, size_t size)
2517{
Jack Pham84fc1ac2013-07-09 17:51:41 -07002518 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2519
2520 if (!mdwc)
2521 return -EINVAL;
2522
Jack Pham0fc12332012-11-19 13:14:22 -08002523 if (!strnicmp(buf, "enable", 6)) {
Jack Pham84fc1ac2013-07-09 17:51:41 -07002524 if (!mdwc->id_adc_detect)
2525 dwc3_init_adc_work(&mdwc->init_adc_work.work);
Jack Pham0fc12332012-11-19 13:14:22 -08002526 return size;
2527 } else if (!strnicmp(buf, "disable", 7)) {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002528 qpnp_adc_tm_usbid_end(mdwc->adc_tm_dev);
Jack Pham84fc1ac2013-07-09 17:51:41 -07002529 mdwc->id_adc_detect = false;
Jack Pham0fc12332012-11-19 13:14:22 -08002530 return size;
2531 }
2532
2533 return -EINVAL;
2534}
2535
2536static DEVICE_ATTR(adc_enable, S_IRUGO | S_IWUSR, adc_enable_show,
2537 adc_enable_store);
2538
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302539static int dwc3_msm_ext_chg_open(struct inode *inode, struct file *file)
2540{
Jack Phamea382b72013-07-09 17:50:20 -07002541 struct dwc3_msm *mdwc =
2542 container_of(inode->i_cdev, struct dwc3_msm, ext_chg_cdev);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302543
2544 pr_debug("dwc3-msm ext chg open\n");
Jack Phamea382b72013-07-09 17:50:20 -07002545 file->private_data = mdwc;
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302546 mdwc->ext_chg_opened = true;
Jack Phamea382b72013-07-09 17:50:20 -07002547
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302548 return 0;
2549}
2550
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302551static long
2552dwc3_msm_ext_chg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302553{
Jack Phamea382b72013-07-09 17:50:20 -07002554 struct dwc3_msm *mdwc = file->private_data;
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302555 struct msm_usb_chg_info info = {0};
2556 int ret = 0, val;
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302557
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302558 switch (cmd) {
2559 case MSM_USB_EXT_CHG_INFO:
2560 info.chg_block_type = USB_CHG_BLOCK_QSCRATCH;
Jack Phamea382b72013-07-09 17:50:20 -07002561 info.page_offset = (mdwc->io_res->start +
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302562 QSCRATCH_REG_OFFSET) & ~PAGE_MASK;
2563 /*
2564 * The charger block register address space is only
2565 * 512 bytes. But mmap() works on PAGE granularity.
2566 */
2567 info.length = PAGE_SIZE;
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302568
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302569 if (copy_to_user((void __user *)arg, &info, sizeof(info))) {
2570 pr_err("%s: copy to user failed\n\n", __func__);
2571 ret = -EFAULT;
2572 }
2573 break;
2574 case MSM_USB_EXT_CHG_BLOCK_LPM:
2575 if (get_user(val, (int __user *)arg)) {
2576 pr_err("%s: get_user failed\n\n", __func__);
2577 ret = -EFAULT;
2578 break;
2579 }
2580 pr_debug("%s: LPM block request %d\n", __func__, val);
2581 if (val) { /* block LPM */
2582 if (mdwc->charger.chg_type == DWC3_DCP_CHARGER) {
2583 pm_runtime_get_sync(mdwc->dev);
2584 } else {
2585 mdwc->ext_chg_active = false;
2586 complete(&mdwc->ext_chg_wait);
2587 ret = -ENODEV;
2588 }
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302589 } else {
2590 mdwc->ext_chg_active = false;
2591 complete(&mdwc->ext_chg_wait);
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302592 pm_runtime_put(mdwc->dev);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302593 }
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302594 break;
2595 default:
2596 ret = -EINVAL;
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302597 }
2598
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302599 return ret;
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302600}
2601
2602static int dwc3_msm_ext_chg_mmap(struct file *file, struct vm_area_struct *vma)
2603{
Jack Phamea382b72013-07-09 17:50:20 -07002604 struct dwc3_msm *mdwc = file->private_data;
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302605 unsigned long vsize = vma->vm_end - vma->vm_start;
2606 int ret;
2607
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302608 if (vma->vm_pgoff != 0 || vsize > PAGE_SIZE)
2609 return -EINVAL;
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302610
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302611 vma->vm_pgoff = __phys_to_pfn(mdwc->io_res->start +
2612 QSCRATCH_REG_OFFSET);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302613 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2614
2615 ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
2616 vsize, vma->vm_page_prot);
2617 if (ret < 0)
2618 pr_err("%s: failed with return val %d\n", __func__, ret);
2619
2620 return ret;
2621}
2622
2623static int dwc3_msm_ext_chg_release(struct inode *inode, struct file *file)
2624{
Jack Phamea382b72013-07-09 17:50:20 -07002625 struct dwc3_msm *mdwc = file->private_data;
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302626
2627 pr_debug("dwc3-msm ext chg release\n");
2628
2629 mdwc->ext_chg_opened = false;
2630
2631 return 0;
2632}
2633
2634static const struct file_operations dwc3_msm_ext_chg_fops = {
2635 .owner = THIS_MODULE,
2636 .open = dwc3_msm_ext_chg_open,
Pavankumar Kondeti17b52e72013-06-28 10:54:18 +05302637 .unlocked_ioctl = dwc3_msm_ext_chg_ioctl,
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302638 .mmap = dwc3_msm_ext_chg_mmap,
2639 .release = dwc3_msm_ext_chg_release,
2640};
2641
2642static int dwc3_msm_setup_cdev(struct dwc3_msm *mdwc)
2643{
2644 int ret;
2645
2646 ret = alloc_chrdev_region(&mdwc->ext_chg_dev, 0, 1, "usb_ext_chg");
2647 if (ret < 0) {
2648 pr_err("Fail to allocate usb ext char dev region\n");
2649 return ret;
2650 }
2651 mdwc->ext_chg_class = class_create(THIS_MODULE, "dwc_ext_chg");
2652 if (ret < 0) {
2653 pr_err("Fail to create usb ext chg class\n");
2654 goto unreg_chrdev;
2655 }
2656 cdev_init(&mdwc->ext_chg_cdev, &dwc3_msm_ext_chg_fops);
2657 mdwc->ext_chg_cdev.owner = THIS_MODULE;
2658
2659 ret = cdev_add(&mdwc->ext_chg_cdev, mdwc->ext_chg_dev, 1);
2660 if (ret < 0) {
2661 pr_err("Fail to add usb ext chg cdev\n");
2662 goto destroy_class;
2663 }
2664 mdwc->ext_chg_device = device_create(mdwc->ext_chg_class,
2665 NULL, mdwc->ext_chg_dev, NULL,
2666 "usb_ext_chg");
2667 if (IS_ERR(mdwc->ext_chg_device)) {
2668 pr_err("Fail to create usb ext chg device\n");
2669 ret = PTR_ERR(mdwc->ext_chg_device);
2670 mdwc->ext_chg_device = NULL;
2671 goto del_cdev;
2672 }
2673
2674 pr_debug("dwc3 msm ext chg cdev setup success\n");
2675 return 0;
2676
2677del_cdev:
2678 cdev_del(&mdwc->ext_chg_cdev);
2679destroy_class:
2680 class_destroy(mdwc->ext_chg_class);
2681unreg_chrdev:
2682 unregister_chrdev_region(mdwc->ext_chg_dev, 1);
2683
2684 return ret;
2685}
2686
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002687static int __devinit dwc3_msm_probe(struct platform_device *pdev)
2688{
2689 struct device_node *node = pdev->dev.of_node;
Jack Pham80162462013-07-10 11:59:01 -07002690 struct dwc3_msm *mdwc;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002691 struct resource *res;
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002692 void __iomem *tcsr;
Manu Gautamf08f7b62013-04-02 16:09:42 +05302693 unsigned long flags;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002694 int ret = 0;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302695 int len = 0;
2696 u32 tmp[3];
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002697
Jack Pham80162462013-07-10 11:59:01 -07002698 mdwc = devm_kzalloc(&pdev->dev, sizeof(*mdwc), GFP_KERNEL);
2699 if (!mdwc) {
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002700 dev_err(&pdev->dev, "not enough memory\n");
2701 return -ENOMEM;
2702 }
2703
Jack Pham80162462013-07-10 11:59:01 -07002704 platform_set_drvdata(pdev, mdwc);
2705 mdwc->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002706
Jack Pham80162462013-07-10 11:59:01 -07002707 INIT_LIST_HEAD(&mdwc->req_complete_list);
2708 INIT_DELAYED_WORK(&mdwc->chg_work, dwc3_chg_detect_work);
2709 INIT_DELAYED_WORK(&mdwc->resume_work, dwc3_resume_work);
2710 INIT_WORK(&mdwc->restart_usb_work, dwc3_restart_usb_work);
Vijayavardhan Vennapusaddd04742013-09-26 19:47:18 +05302711 INIT_WORK(&mdwc->usb_block_reset_work, dwc3_block_reset_usb_work);
Jack Pham80162462013-07-10 11:59:01 -07002712 INIT_WORK(&mdwc->id_work, dwc3_id_work);
2713 INIT_DELAYED_WORK(&mdwc->init_adc_work, dwc3_init_adc_work);
2714 init_completion(&mdwc->ext_chg_wait);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002715
Jack Pham80162462013-07-10 11:59:01 -07002716 ret = dwc3_msm_config_gdsc(mdwc, 1);
Hemant Kumar086bf6b2013-06-10 19:29:27 -07002717 if (ret) {
2718 dev_err(&pdev->dev, "unable to configure usb3 gdsc\n");
2719 return ret;
2720 }
2721
Jack Pham80162462013-07-10 11:59:01 -07002722 mdwc->xo_clk = clk_get(&pdev->dev, "xo");
2723 if (IS_ERR(mdwc->xo_clk)) {
Manu Gautam377821c2012-09-28 16:53:24 +05302724 dev_err(&pdev->dev, "%s unable to get TCXO buffer handle\n",
2725 __func__);
Jack Pham80162462013-07-10 11:59:01 -07002726 ret = PTR_ERR(mdwc->xo_clk);
Hemant Kumar086bf6b2013-06-10 19:29:27 -07002727 goto disable_dwc3_gdsc;
Manu Gautam377821c2012-09-28 16:53:24 +05302728 }
2729
Jack Pham80162462013-07-10 11:59:01 -07002730 ret = clk_prepare_enable(mdwc->xo_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05302731 if (ret) {
2732 dev_err(&pdev->dev, "%s failed to vote for TCXO buffer%d\n",
2733 __func__, ret);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302734 goto put_xo;
Manu Gautam377821c2012-09-28 16:53:24 +05302735 }
2736
Manu Gautam1742db22012-06-19 13:33:24 +05302737 /*
2738 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
2739 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
2740 */
Jack Pham80162462013-07-10 11:59:01 -07002741 mdwc->core_clk = devm_clk_get(&pdev->dev, "core_clk");
2742 if (IS_ERR(mdwc->core_clk)) {
Manu Gautam1742db22012-06-19 13:33:24 +05302743 dev_err(&pdev->dev, "failed to get core_clk\n");
Jack Pham80162462013-07-10 11:59:01 -07002744 ret = PTR_ERR(mdwc->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302745 goto disable_xo;
Manu Gautam1742db22012-06-19 13:33:24 +05302746 }
Jack Pham80162462013-07-10 11:59:01 -07002747 clk_set_rate(mdwc->core_clk, 125000000);
2748 clk_prepare_enable(mdwc->core_clk);
Manu Gautam1742db22012-06-19 13:33:24 +05302749
Jack Pham80162462013-07-10 11:59:01 -07002750 mdwc->iface_clk = devm_clk_get(&pdev->dev, "iface_clk");
2751 if (IS_ERR(mdwc->iface_clk)) {
Manu Gautam3e9ad352012-08-16 14:44:47 -07002752 dev_err(&pdev->dev, "failed to get iface_clk\n");
Jack Pham80162462013-07-10 11:59:01 -07002753 ret = PTR_ERR(mdwc->iface_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002754 goto disable_core_clk;
2755 }
Jack Pham80162462013-07-10 11:59:01 -07002756 clk_prepare_enable(mdwc->iface_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002757
Jack Pham80162462013-07-10 11:59:01 -07002758 mdwc->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2759 if (IS_ERR(mdwc->sleep_clk)) {
Manu Gautam3e9ad352012-08-16 14:44:47 -07002760 dev_err(&pdev->dev, "failed to get sleep_clk\n");
Jack Pham80162462013-07-10 11:59:01 -07002761 ret = PTR_ERR(mdwc->sleep_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002762 goto disable_iface_clk;
2763 }
Jack Pham80162462013-07-10 11:59:01 -07002764 clk_prepare_enable(mdwc->sleep_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002765
Jack Pham80162462013-07-10 11:59:01 -07002766 mdwc->hsphy_sleep_clk = devm_clk_get(&pdev->dev, "sleep_a_clk");
2767 if (IS_ERR(mdwc->hsphy_sleep_clk)) {
Manu Gautam3e9ad352012-08-16 14:44:47 -07002768 dev_err(&pdev->dev, "failed to get sleep_a_clk\n");
Jack Pham80162462013-07-10 11:59:01 -07002769 ret = PTR_ERR(mdwc->hsphy_sleep_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002770 goto disable_sleep_clk;
2771 }
Jack Pham80162462013-07-10 11:59:01 -07002772 clk_prepare_enable(mdwc->hsphy_sleep_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002773
Jack Pham80162462013-07-10 11:59:01 -07002774 mdwc->utmi_clk = devm_clk_get(&pdev->dev, "utmi_clk");
2775 if (IS_ERR(mdwc->utmi_clk)) {
Jack Pham22698b82013-02-13 17:45:06 -08002776 dev_err(&pdev->dev, "failed to get utmi_clk\n");
Jack Pham80162462013-07-10 11:59:01 -07002777 ret = PTR_ERR(mdwc->utmi_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002778 goto disable_sleep_a_clk;
2779 }
Jack Pham80162462013-07-10 11:59:01 -07002780 clk_prepare_enable(mdwc->utmi_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002781
Jack Pham80162462013-07-10 11:59:01 -07002782 mdwc->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
2783 if (IS_ERR(mdwc->ref_clk)) {
Manu Gautam3e9ad352012-08-16 14:44:47 -07002784 dev_err(&pdev->dev, "failed to get ref_clk\n");
Jack Pham80162462013-07-10 11:59:01 -07002785 ret = PTR_ERR(mdwc->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002786 goto disable_utmi_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -07002787 }
Jack Pham80162462013-07-10 11:59:01 -07002788 clk_prepare_enable(mdwc->ref_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002789
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302790 of_get_property(node, "qcom,vdd-voltage-level", &len);
2791 if (len == sizeof(tmp)) {
2792 of_property_read_u32_array(node, "qcom,vdd-voltage-level",
2793 tmp, len/sizeof(*tmp));
Jack Pham80162462013-07-10 11:59:01 -07002794 mdwc->vdd_no_vol_level = tmp[0];
2795 mdwc->vdd_low_vol_level = tmp[1];
2796 mdwc->vdd_high_vol_level = tmp[2];
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302797 } else {
2798 dev_err(&pdev->dev, "no qcom,vdd-voltage-level property\n");
2799 ret = -EINVAL;
2800 goto disable_ref_clk;
2801 }
2802
Manu Gautam60e01352012-05-29 09:00:34 +05302803 /* SS PHY */
Jack Pham80162462013-07-10 11:59:01 -07002804 mdwc->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
2805 if (IS_ERR(mdwc->ssusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302806 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
Jack Pham80162462013-07-10 11:59:01 -07002807 ret = PTR_ERR(mdwc->ssusb_vddcx);
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302808 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302809 }
2810
Jack Pham80162462013-07-10 11:59:01 -07002811 ret = dwc3_ssusb_config_vddcx(mdwc, 1);
Manu Gautam60e01352012-05-29 09:00:34 +05302812 if (ret) {
2813 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam3e9ad352012-08-16 14:44:47 -07002814 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302815 }
2816
Jack Pham80162462013-07-10 11:59:01 -07002817 ret = regulator_enable(mdwc->ssusb_vddcx);
Manu Gautam60e01352012-05-29 09:00:34 +05302818 if (ret) {
2819 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
2820 goto unconfig_ss_vddcx;
2821 }
2822
Jack Pham80162462013-07-10 11:59:01 -07002823 ret = dwc3_ssusb_ldo_init(mdwc, 1);
Manu Gautam60e01352012-05-29 09:00:34 +05302824 if (ret) {
2825 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
2826 goto disable_ss_vddcx;
2827 }
2828
Jack Pham80162462013-07-10 11:59:01 -07002829 ret = dwc3_ssusb_ldo_enable(mdwc, 1);
Manu Gautam60e01352012-05-29 09:00:34 +05302830 if (ret) {
2831 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
2832 goto free_ss_ldo_init;
2833 }
2834
2835 /* HS PHY */
Jack Pham80162462013-07-10 11:59:01 -07002836 mdwc->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
2837 if (IS_ERR(mdwc->hsusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302838 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
Jack Pham80162462013-07-10 11:59:01 -07002839 ret = PTR_ERR(mdwc->hsusb_vddcx);
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302840 goto disable_ss_ldo;
Manu Gautam60e01352012-05-29 09:00:34 +05302841 }
2842
Jack Pham80162462013-07-10 11:59:01 -07002843 ret = dwc3_hsusb_config_vddcx(mdwc, 1);
Manu Gautam60e01352012-05-29 09:00:34 +05302844 if (ret) {
2845 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2846 goto disable_ss_ldo;
2847 }
2848
Jack Pham80162462013-07-10 11:59:01 -07002849 ret = regulator_enable(mdwc->hsusb_vddcx);
Manu Gautam60e01352012-05-29 09:00:34 +05302850 if (ret) {
2851 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
2852 goto unconfig_hs_vddcx;
2853 }
2854
Jack Pham80162462013-07-10 11:59:01 -07002855 ret = dwc3_hsusb_ldo_init(mdwc, 1);
Manu Gautam60e01352012-05-29 09:00:34 +05302856 if (ret) {
2857 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
2858 goto disable_hs_vddcx;
2859 }
2860
Jack Pham80162462013-07-10 11:59:01 -07002861 ret = dwc3_hsusb_ldo_enable(mdwc, 1);
Manu Gautam60e01352012-05-29 09:00:34 +05302862 if (ret) {
2863 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
2864 goto free_hs_ldo_init;
2865 }
2866
Jack Pham80162462013-07-10 11:59:01 -07002867 mdwc->id_state = mdwc->ext_xceiv.id = DWC3_ID_FLOAT;
2868 mdwc->ext_xceiv.otg_capability = of_property_read_bool(node,
Manu Gautam6c0ff032012-11-02 14:55:35 +05302869 "qcom,otg-capability");
Jack Pham80162462013-07-10 11:59:01 -07002870 mdwc->charger.charging_disabled = of_property_read_bool(node,
Manu Gautam6c0ff032012-11-02 14:55:35 +05302871 "qcom,charging-disabled");
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302872
Jack Pham80162462013-07-10 11:59:01 -07002873 mdwc->charger.skip_chg_detect = of_property_read_bool(node,
Hemant Kumar6d7b7242013-04-18 16:44:38 -07002874 "qcom,skip-charger-detection");
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302875 /*
2876 * DWC3 has separate IRQ line for OTG events (ID/BSV) and for
2877 * DP and DM linestate transitions during low power mode.
2878 */
Jack Pham80162462013-07-10 11:59:01 -07002879 mdwc->hs_phy_irq = platform_get_irq_byname(pdev, "hs_phy_irq");
2880 if (mdwc->hs_phy_irq < 0) {
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302881 dev_dbg(&pdev->dev, "pget_irq for hs_phy_irq failed\n");
Jack Pham80162462013-07-10 11:59:01 -07002882 mdwc->hs_phy_irq = 0;
Jack Pham0fc12332012-11-19 13:14:22 -08002883 } else {
Jack Pham80162462013-07-10 11:59:01 -07002884 ret = devm_request_irq(&pdev->dev, mdwc->hs_phy_irq,
Jack Pham56a0a632013-03-08 13:18:42 -08002885 msm_dwc3_irq, IRQF_TRIGGER_RISING,
Jack Pham80162462013-07-10 11:59:01 -07002886 "msm_dwc3", mdwc);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302887 if (ret) {
2888 dev_err(&pdev->dev, "irqreq HSPHYINT failed\n");
2889 goto disable_hs_ldo;
2890 }
Jack Pham80162462013-07-10 11:59:01 -07002891 enable_irq_wake(mdwc->hs_phy_irq);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302892 }
Jack Pham0cca9412013-03-08 13:22:42 -08002893
Jack Pham80162462013-07-10 11:59:01 -07002894 if (mdwc->ext_xceiv.otg_capability) {
2895 mdwc->pmic_id_irq =
2896 platform_get_irq_byname(pdev, "pmic_id_irq");
2897 if (mdwc->pmic_id_irq > 0) {
David Keitelad4a0282013-03-19 18:04:27 -07002898 /* check if PMIC ID IRQ is supported */
2899 ret = qpnp_misc_irqs_available(&pdev->dev);
2900
2901 if (ret == -EPROBE_DEFER) {
2902 /* qpnp hasn't probed yet; defer dwc probe */
Jack Pham0cca9412013-03-08 13:22:42 -08002903 goto disable_hs_ldo;
David Keitelad4a0282013-03-19 18:04:27 -07002904 } else if (ret == 0) {
Jack Pham80162462013-07-10 11:59:01 -07002905 mdwc->pmic_id_irq = 0;
David Keitelad4a0282013-03-19 18:04:27 -07002906 } else {
2907 ret = devm_request_irq(&pdev->dev,
Jack Pham80162462013-07-10 11:59:01 -07002908 mdwc->pmic_id_irq,
David Keitelad4a0282013-03-19 18:04:27 -07002909 dwc3_pmic_id_irq,
2910 IRQF_TRIGGER_RISING |
2911 IRQF_TRIGGER_FALLING,
Jack Pham80162462013-07-10 11:59:01 -07002912 "dwc3_msm_pmic_id",
2913 mdwc);
David Keitelad4a0282013-03-19 18:04:27 -07002914 if (ret) {
2915 dev_err(&pdev->dev, "irqreq IDINT failed\n");
2916 goto disable_hs_ldo;
2917 }
Jack Pham9198d9f2013-04-09 17:54:54 -07002918
Manu Gautamf08f7b62013-04-02 16:09:42 +05302919 local_irq_save(flags);
2920 /* Update initial ID state */
Jack Pham80162462013-07-10 11:59:01 -07002921 mdwc->id_state =
2922 !!irq_read_line(mdwc->pmic_id_irq);
2923 if (mdwc->id_state == DWC3_ID_GROUND)
Jack Pham9198d9f2013-04-09 17:54:54 -07002924 queue_work(system_nrt_wq,
Jack Pham80162462013-07-10 11:59:01 -07002925 &mdwc->id_work);
Manu Gautamf08f7b62013-04-02 16:09:42 +05302926 local_irq_restore(flags);
Jack Pham80162462013-07-10 11:59:01 -07002927 enable_irq_wake(mdwc->pmic_id_irq);
Jack Pham0cca9412013-03-08 13:22:42 -08002928 }
David Keitelad4a0282013-03-19 18:04:27 -07002929 }
2930
Jack Pham80162462013-07-10 11:59:01 -07002931 if (mdwc->pmic_id_irq <= 0) {
Jack Pham0cca9412013-03-08 13:22:42 -08002932 /* If no PMIC ID IRQ, use ADC for ID pin detection */
Jack Pham80162462013-07-10 11:59:01 -07002933 queue_work(system_nrt_wq, &mdwc->init_adc_work.work);
Jack Pham0cca9412013-03-08 13:22:42 -08002934 device_create_file(&pdev->dev, &dev_attr_adc_enable);
Jack Pham80162462013-07-10 11:59:01 -07002935 mdwc->pmic_id_irq = 0;
Jack Pham0cca9412013-03-08 13:22:42 -08002936 }
Manu Gautam377821c2012-09-28 16:53:24 +05302937 }
2938
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002939 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2940 if (!res) {
2941 dev_dbg(&pdev->dev, "missing TCSR memory resource\n");
2942 } else {
2943 tcsr = devm_ioremap_nocache(&pdev->dev, res->start,
2944 resource_size(res));
2945 if (!tcsr) {
2946 dev_dbg(&pdev->dev, "tcsr ioremap failed\n");
2947 } else {
2948 /* Enable USB3 on the primary USB port. */
2949 writel_relaxed(0x1, tcsr);
2950 /*
2951 * Ensure that TCSR write is completed before
2952 * USB registers initialization.
2953 */
2954 mb();
2955 }
2956 }
2957
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002958 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2959 if (!res) {
2960 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302961 ret = -ENODEV;
Jack Pham56a0a632013-03-08 13:18:42 -08002962 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002963 }
2964
Jack Pham80162462013-07-10 11:59:01 -07002965 mdwc->base = devm_ioremap_nocache(&pdev->dev, res->start,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002966 resource_size(res));
Jack Pham80162462013-07-10 11:59:01 -07002967 if (!mdwc->base) {
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002968 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302969 ret = -ENODEV;
Jack Pham56a0a632013-03-08 13:18:42 -08002970 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002971 }
2972
Jack Pham80162462013-07-10 11:59:01 -07002973 mdwc->io_res = res; /* used to calculate chg block offset */
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002974
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05302975 if (of_property_read_u32(node, "qcom,dwc-hsphy-init",
Jack Pham80162462013-07-10 11:59:01 -07002976 &mdwc->hsphy_init_seq))
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05302977 dev_dbg(&pdev->dev, "unable to read hsphy init seq\n");
Jack Pham80162462013-07-10 11:59:01 -07002978 else if (!mdwc->hsphy_init_seq)
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05302979 dev_warn(&pdev->dev, "incorrect hsphyinitseq.Using PORvalue\n");
2980
Jack Pham80162462013-07-10 11:59:01 -07002981 pm_runtime_set_active(mdwc->dev);
2982 pm_runtime_enable(mdwc->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05302983
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002984 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
Jack Pham80162462013-07-10 11:59:01 -07002985 &mdwc->dbm_num_eps)) {
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002986 dev_err(&pdev->dev,
2987 "unable to read platform data num of dbm eps\n");
Jack Pham80162462013-07-10 11:59:01 -07002988 mdwc->dbm_num_eps = DBM_MAX_EPS;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002989 }
2990
Jack Pham80162462013-07-10 11:59:01 -07002991 if (mdwc->dbm_num_eps > DBM_MAX_EPS) {
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002992 dev_err(&pdev->dev,
2993 "Driver doesn't support number of DBM EPs. "
2994 "max: %d, dbm_num_eps: %d\n",
Jack Pham80162462013-07-10 11:59:01 -07002995 DBM_MAX_EPS, mdwc->dbm_num_eps);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002996 ret = -ENODEV;
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +05302997 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002998 }
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +05302999
3000 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-tx-fifo-size",
3001 &mdwc->tx_fifo_size))
3002 dev_err(&pdev->dev,
3003 "unable to read platform data tx fifo size\n");
3004
3005 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-qdss-tx-fifo-size",
3006 &mdwc->qdss_tx_fifo_size))
3007 dev_err(&pdev->dev,
3008 "unable to read platform data qdss tx fifo size\n");
3009
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05303010 dwc3_set_notifier(&dwc3_msm_notify_event);
Manu Gautambb825d72013-03-12 16:25:42 +05303011 /* usb_psy required only for vbus_notifications or charging support */
Jack Pham80162462013-07-10 11:59:01 -07003012 if (mdwc->ext_xceiv.otg_capability ||
3013 !mdwc->charger.charging_disabled) {
3014 mdwc->usb_psy.name = "usb";
3015 mdwc->usb_psy.type = POWER_SUPPLY_TYPE_USB;
3016 mdwc->usb_psy.supplied_to = dwc3_msm_pm_power_supplied_to;
3017 mdwc->usb_psy.num_supplicants = ARRAY_SIZE(
Manu Gautambb825d72013-03-12 16:25:42 +05303018 dwc3_msm_pm_power_supplied_to);
Jack Pham80162462013-07-10 11:59:01 -07003019 mdwc->usb_psy.properties = dwc3_msm_pm_power_props_usb;
3020 mdwc->usb_psy.num_properties =
Manu Gautambb825d72013-03-12 16:25:42 +05303021 ARRAY_SIZE(dwc3_msm_pm_power_props_usb);
Jack Pham80162462013-07-10 11:59:01 -07003022 mdwc->usb_psy.get_property = dwc3_msm_power_get_property_usb;
3023 mdwc->usb_psy.set_property = dwc3_msm_power_set_property_usb;
3024 mdwc->usb_psy.external_power_changed =
Manu Gautambb825d72013-03-12 16:25:42 +05303025 dwc3_msm_external_power_changed;
Pavankumar Kondetifbd4b142013-07-16 11:13:05 +05303026 mdwc->usb_psy.property_is_writeable =
3027 dwc3_msm_property_is_writeable;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05303028
Jack Pham80162462013-07-10 11:59:01 -07003029 ret = power_supply_register(&pdev->dev, &mdwc->usb_psy);
Manu Gautambb825d72013-03-12 16:25:42 +05303030 if (ret < 0) {
3031 dev_err(&pdev->dev,
3032 "%s:power_supply_register usb failed\n",
3033 __func__);
3034 goto disable_hs_ldo;
3035 }
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05303036 }
3037
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +05303038 if (node) {
3039 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
3040 if (ret) {
3041 dev_err(&pdev->dev,
3042 "failed to add create dwc3 core\n");
3043 goto put_psupply;
3044 }
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003045 }
3046
Jack Pham80162462013-07-10 11:59:01 -07003047 mdwc->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3048 if (!mdwc->bus_scale_table) {
Manu Gautam2617deb2012-08-31 17:50:06 -07003049 dev_err(&pdev->dev, "bus scaling is disabled\n");
3050 } else {
Jack Pham80162462013-07-10 11:59:01 -07003051 mdwc->bus_perf_client =
3052 msm_bus_scale_register_client(mdwc->bus_scale_table);
Manu Gautam2617deb2012-08-31 17:50:06 -07003053 ret = msm_bus_scale_client_update_request(
Jack Pham80162462013-07-10 11:59:01 -07003054 mdwc->bus_perf_client, 1);
Manu Gautam2617deb2012-08-31 17:50:06 -07003055 if (ret)
3056 dev_err(&pdev->dev, "Failed to vote for bus scaling\n");
3057 }
3058
Jack Pham80162462013-07-10 11:59:01 -07003059 mdwc->otg_xceiv = usb_get_transceiver();
Manu Gautambb825d72013-03-12 16:25:42 +05303060 /* Register with OTG if present, ignore USB2 OTG using other PHY */
Jack Pham80162462013-07-10 11:59:01 -07003061 if (mdwc->otg_xceiv &&
3062 !(mdwc->otg_xceiv->flags & ENABLE_SECONDARY_PHY)) {
Hemant Kumar6d7b7242013-04-18 16:44:38 -07003063 /* Skip charger detection for simulator targets */
Jack Pham80162462013-07-10 11:59:01 -07003064 if (!mdwc->charger.skip_chg_detect) {
3065 mdwc->charger.start_detection = dwc3_start_chg_det;
3066 ret = dwc3_set_charger(mdwc->otg_xceiv->otg,
3067 &mdwc->charger);
3068 if (ret || !mdwc->charger.notify_detection_complete) {
Hemant Kumar6d7b7242013-04-18 16:44:38 -07003069 dev_err(&pdev->dev,
3070 "failed to register charger: %d\n",
3071 ret);
3072 goto put_xcvr;
3073 }
Manu Gautam8c642812012-06-07 10:35:10 +05303074 }
Manu Gautamb5067272012-07-02 09:53:41 +05303075
Jack Pham80162462013-07-10 11:59:01 -07003076 if (mdwc->ext_xceiv.otg_capability)
3077 mdwc->ext_xceiv.ext_block_reset = dwc3_msm_block_reset;
3078 ret = dwc3_set_ext_xceiv(mdwc->otg_xceiv->otg,
3079 &mdwc->ext_xceiv);
3080 if (ret || !mdwc->ext_xceiv.notify_ext_events) {
Manu Gautamb5067272012-07-02 09:53:41 +05303081 dev_err(&pdev->dev, "failed to register xceiver: %d\n",
3082 ret);
3083 goto put_xcvr;
3084 }
Manu Gautam8c642812012-06-07 10:35:10 +05303085 } else {
Manu Gautambb825d72013-03-12 16:25:42 +05303086 dev_dbg(&pdev->dev, "No OTG, DWC3 running in host only mode\n");
Jack Pham80162462013-07-10 11:59:01 -07003087 mdwc->host_mode = 1;
3088 mdwc->vbus_otg = devm_regulator_get(&pdev->dev, "vbus_dwc3");
3089 if (IS_ERR(mdwc->vbus_otg)) {
Manu Gautambb825d72013-03-12 16:25:42 +05303090 dev_dbg(&pdev->dev, "Failed to get vbus regulator\n");
Jack Pham80162462013-07-10 11:59:01 -07003091 mdwc->vbus_otg = 0;
Manu Gautambb825d72013-03-12 16:25:42 +05303092 } else {
Jack Pham80162462013-07-10 11:59:01 -07003093 ret = regulator_enable(mdwc->vbus_otg);
Manu Gautambb825d72013-03-12 16:25:42 +05303094 if (ret) {
Jack Pham80162462013-07-10 11:59:01 -07003095 mdwc->vbus_otg = 0;
Manu Gautambb825d72013-03-12 16:25:42 +05303096 dev_err(&pdev->dev, "Failed to enable vbus_otg\n");
3097 }
3098 }
Jack Pham80162462013-07-10 11:59:01 -07003099 mdwc->otg_xceiv = NULL;
Manu Gautam8c642812012-06-07 10:35:10 +05303100 }
Jack Pham80162462013-07-10 11:59:01 -07003101 if (mdwc->ext_xceiv.otg_capability && mdwc->charger.start_detection) {
3102 ret = dwc3_msm_setup_cdev(mdwc);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05303103 if (ret)
3104 dev_err(&pdev->dev, "Fail to setup dwc3 setup cdev\n");
3105 }
Manu Gautam8c642812012-06-07 10:35:10 +05303106
Jack Pham80162462013-07-10 11:59:01 -07003107 device_init_wakeup(mdwc->dev, 1);
3108 pm_stay_awake(mdwc->dev);
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05303109 dwc3_msm_debugfs_init(mdwc);
Manu Gautamb5067272012-07-02 09:53:41 +05303110
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003111 return 0;
3112
Manu Gautam8c642812012-06-07 10:35:10 +05303113put_xcvr:
Jack Pham80162462013-07-10 11:59:01 -07003114 usb_put_transceiver(mdwc->otg_xceiv);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05303115put_psupply:
Jack Pham80162462013-07-10 11:59:01 -07003116 if (mdwc->usb_psy.dev)
3117 power_supply_unregister(&mdwc->usb_psy);
Manu Gautam60e01352012-05-29 09:00:34 +05303118disable_hs_ldo:
Jack Pham80162462013-07-10 11:59:01 -07003119 dwc3_hsusb_ldo_enable(mdwc, 0);
Manu Gautam60e01352012-05-29 09:00:34 +05303120free_hs_ldo_init:
Jack Pham80162462013-07-10 11:59:01 -07003121 dwc3_hsusb_ldo_init(mdwc, 0);
Manu Gautam60e01352012-05-29 09:00:34 +05303122disable_hs_vddcx:
Jack Pham80162462013-07-10 11:59:01 -07003123 regulator_disable(mdwc->hsusb_vddcx);
Manu Gautam60e01352012-05-29 09:00:34 +05303124unconfig_hs_vddcx:
Jack Pham80162462013-07-10 11:59:01 -07003125 dwc3_hsusb_config_vddcx(mdwc, 0);
Manu Gautam60e01352012-05-29 09:00:34 +05303126disable_ss_ldo:
Jack Pham80162462013-07-10 11:59:01 -07003127 dwc3_ssusb_ldo_enable(mdwc, 0);
Manu Gautam60e01352012-05-29 09:00:34 +05303128free_ss_ldo_init:
Jack Pham80162462013-07-10 11:59:01 -07003129 dwc3_ssusb_ldo_init(mdwc, 0);
Manu Gautam60e01352012-05-29 09:00:34 +05303130disable_ss_vddcx:
Jack Pham80162462013-07-10 11:59:01 -07003131 regulator_disable(mdwc->ssusb_vddcx);
Manu Gautam60e01352012-05-29 09:00:34 +05303132unconfig_ss_vddcx:
Jack Pham80162462013-07-10 11:59:01 -07003133 dwc3_ssusb_config_vddcx(mdwc, 0);
Manu Gautam3e9ad352012-08-16 14:44:47 -07003134disable_ref_clk:
Jack Pham80162462013-07-10 11:59:01 -07003135 clk_disable_unprepare(mdwc->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08003136disable_utmi_clk:
Jack Pham80162462013-07-10 11:59:01 -07003137 clk_disable_unprepare(mdwc->utmi_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07003138disable_sleep_a_clk:
Jack Pham80162462013-07-10 11:59:01 -07003139 clk_disable_unprepare(mdwc->hsphy_sleep_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07003140disable_sleep_clk:
Jack Pham80162462013-07-10 11:59:01 -07003141 clk_disable_unprepare(mdwc->sleep_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07003142disable_iface_clk:
Jack Pham80162462013-07-10 11:59:01 -07003143 clk_disable_unprepare(mdwc->iface_clk);
Manu Gautam1742db22012-06-19 13:33:24 +05303144disable_core_clk:
Jack Pham80162462013-07-10 11:59:01 -07003145 clk_disable_unprepare(mdwc->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05303146disable_xo:
Jack Pham80162462013-07-10 11:59:01 -07003147 clk_disable_unprepare(mdwc->xo_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05303148put_xo:
Jack Pham80162462013-07-10 11:59:01 -07003149 clk_put(mdwc->xo_clk);
Hemant Kumar086bf6b2013-06-10 19:29:27 -07003150disable_dwc3_gdsc:
Jack Pham80162462013-07-10 11:59:01 -07003151 dwc3_msm_config_gdsc(mdwc, 0);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003152
3153 return ret;
3154}
3155
3156static int __devexit dwc3_msm_remove(struct platform_device *pdev)
3157{
Jack Pham80162462013-07-10 11:59:01 -07003158 struct dwc3_msm *mdwc = platform_get_drvdata(pdev);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003159
Jack Pham80162462013-07-10 11:59:01 -07003160 if (!mdwc->ext_chg_device) {
3161 device_destroy(mdwc->ext_chg_class, mdwc->ext_chg_dev);
3162 cdev_del(&mdwc->ext_chg_cdev);
3163 class_destroy(mdwc->ext_chg_class);
3164 unregister_chrdev_region(mdwc->ext_chg_dev, 1);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05303165 }
3166
Jack Pham80162462013-07-10 11:59:01 -07003167 if (mdwc->id_adc_detect)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003168 qpnp_adc_tm_usbid_end(mdwc->adc_tm_dev);
Manu Gautamb5067272012-07-02 09:53:41 +05303169 if (dwc3_debugfs_root)
3170 debugfs_remove_recursive(dwc3_debugfs_root);
Jack Pham80162462013-07-10 11:59:01 -07003171 if (mdwc->otg_xceiv) {
3172 dwc3_start_chg_det(&mdwc->charger, false);
3173 usb_put_transceiver(mdwc->otg_xceiv);
Manu Gautam8c642812012-06-07 10:35:10 +05303174 }
Jack Pham80162462013-07-10 11:59:01 -07003175 if (mdwc->usb_psy.dev)
3176 power_supply_unregister(&mdwc->usb_psy);
3177 if (mdwc->vbus_otg)
3178 regulator_disable(mdwc->vbus_otg);
Jack Pham0fc12332012-11-19 13:14:22 -08003179
Jack Pham80162462013-07-10 11:59:01 -07003180 pm_runtime_disable(mdwc->dev);
3181 device_init_wakeup(mdwc->dev, 0);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003182
Jack Pham80162462013-07-10 11:59:01 -07003183 dwc3_hsusb_ldo_enable(mdwc, 0);
3184 dwc3_hsusb_ldo_init(mdwc, 0);
3185 regulator_disable(mdwc->hsusb_vddcx);
3186 dwc3_hsusb_config_vddcx(mdwc, 0);
3187 dwc3_ssusb_ldo_enable(mdwc, 0);
3188 dwc3_ssusb_ldo_init(mdwc, 0);
3189 regulator_disable(mdwc->ssusb_vddcx);
3190 dwc3_ssusb_config_vddcx(mdwc, 0);
3191 clk_disable_unprepare(mdwc->core_clk);
3192 clk_disable_unprepare(mdwc->iface_clk);
3193 clk_disable_unprepare(mdwc->sleep_clk);
3194 clk_disable_unprepare(mdwc->hsphy_sleep_clk);
3195 clk_disable_unprepare(mdwc->ref_clk);
3196 clk_disable_unprepare(mdwc->xo_clk);
3197 clk_put(mdwc->xo_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05303198
Jack Pham80162462013-07-10 11:59:01 -07003199 dwc3_msm_config_gdsc(mdwc, 0);
Hemant Kumar086bf6b2013-06-10 19:29:27 -07003200
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003201 return 0;
3202}
3203
Manu Gautamb5067272012-07-02 09:53:41 +05303204static int dwc3_msm_pm_suspend(struct device *dev)
3205{
3206 int ret = 0;
3207 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3208
3209 dev_dbg(dev, "dwc3-msm PM suspend\n");
3210
Manu Gautam8d98a572013-01-21 16:34:50 +05303211 flush_delayed_work_sync(&mdwc->resume_work);
3212 if (!atomic_read(&mdwc->in_lpm)) {
3213 dev_err(mdwc->dev, "Abort PM suspend!! (USB is outside LPM)\n");
3214 return -EBUSY;
3215 }
3216
Manu Gautamb5067272012-07-02 09:53:41 +05303217 ret = dwc3_msm_suspend(mdwc);
3218 if (!ret)
3219 atomic_set(&mdwc->pm_suspended, 1);
3220
3221 return ret;
3222}
3223
3224static int dwc3_msm_pm_resume(struct device *dev)
3225{
3226 int ret = 0;
3227 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3228
3229 dev_dbg(dev, "dwc3-msm PM resume\n");
3230
3231 atomic_set(&mdwc->pm_suspended, 0);
3232 if (mdwc->resume_pending) {
3233 mdwc->resume_pending = false;
3234
3235 ret = dwc3_msm_resume(mdwc);
3236 /* Update runtime PM status */
3237 pm_runtime_disable(dev);
3238 pm_runtime_set_active(dev);
3239 pm_runtime_enable(dev);
3240
3241 /* Let OTG know about resume event and update pm_count */
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05303242 if (mdwc->otg_xceiv) {
Manu Gautamb5067272012-07-02 09:53:41 +05303243 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
3244 DWC3_EVENT_PHY_RESUME);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05303245 if (mdwc->ext_xceiv.otg_capability)
3246 mdwc->ext_xceiv.notify_ext_events(
3247 mdwc->otg_xceiv->otg,
3248 DWC3_EVENT_XCEIV_STATE);
3249 }
Manu Gautamb5067272012-07-02 09:53:41 +05303250 }
3251
3252 return ret;
3253}
3254
3255static int dwc3_msm_runtime_idle(struct device *dev)
3256{
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05303257 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3258
Manu Gautamb5067272012-07-02 09:53:41 +05303259 dev_dbg(dev, "DWC3-msm runtime idle\n");
3260
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05303261 if (mdwc->ext_chg_active) {
3262 dev_dbg(dev, "Deferring LPM\n");
3263 /*
3264 * Charger detection may happen in user space.
3265 * Delay entering LPM by 3 sec. Otherwise we
3266 * have to exit LPM when user space begins
3267 * charger detection.
3268 *
3269 * This timer will be canceled when user space
3270 * votes against LPM by incrementing PM usage
3271 * counter. We enter low power mode when
3272 * PM usage counter is decremented.
3273 */
3274 pm_schedule_suspend(dev, 3000);
3275 return -EAGAIN;
3276 }
3277
Manu Gautamb5067272012-07-02 09:53:41 +05303278 return 0;
3279}
3280
3281static int dwc3_msm_runtime_suspend(struct device *dev)
3282{
3283 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3284
3285 dev_dbg(dev, "DWC3-msm runtime suspend\n");
3286
3287 return dwc3_msm_suspend(mdwc);
3288}
3289
3290static int dwc3_msm_runtime_resume(struct device *dev)
3291{
3292 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3293
3294 dev_dbg(dev, "DWC3-msm runtime resume\n");
3295
3296 return dwc3_msm_resume(mdwc);
3297}
3298
3299static const struct dev_pm_ops dwc3_msm_dev_pm_ops = {
3300 SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume)
3301 SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume,
3302 dwc3_msm_runtime_idle)
3303};
3304
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003305static const struct of_device_id of_dwc3_matach[] = {
3306 {
3307 .compatible = "qcom,dwc-usb3-msm",
3308 },
3309 { },
3310};
3311MODULE_DEVICE_TABLE(of, of_dwc3_matach);
3312
3313static struct platform_driver dwc3_msm_driver = {
3314 .probe = dwc3_msm_probe,
3315 .remove = __devexit_p(dwc3_msm_remove),
3316 .driver = {
3317 .name = "msm-dwc3",
Manu Gautamb5067272012-07-02 09:53:41 +05303318 .pm = &dwc3_msm_dev_pm_ops,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003319 .of_match_table = of_dwc3_matach,
3320 },
3321};
3322
Manu Gautam377821c2012-09-28 16:53:24 +05303323MODULE_LICENSE("GPL v2");
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003324MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
3325
3326static int __devinit dwc3_msm_init(void)
3327{
3328 return platform_driver_register(&dwc3_msm_driver);
3329}
3330module_init(dwc3_msm_init);
3331
3332static void __exit dwc3_msm_exit(void)
3333{
3334 platform_driver_unregister(&dwc3_msm_driver);
3335}
3336module_exit(dwc3_msm_exit);