blob: 2600001cde2232c91709ae3020edead649c2701a [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>
Manu Gautam60e01352012-05-29 09:00:34 +053037#include <linux/regulator/consumer.h>
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +053038#include <linux/power_supply.h>
Jack Pham0fc12332012-11-19 13:14:22 -080039#include <linux/qpnp/qpnp-adc.h>
Pavankumar Kondeti08693e72013-05-03 11:55:48 +053040#include <linux/cdev.h>
41#include <linux/completion.h>
Manu Gautam60e01352012-05-29 09:00:34 +053042
43#include <mach/rpm-regulator.h>
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +053044#include <mach/rpm-regulator-smd.h>
Manu Gautam2617deb2012-08-31 17:50:06 -070045#include <mach/msm_bus.h>
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053046#include <mach/clk.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030047
Manu Gautam8c642812012-06-07 10:35:10 +053048#include "dwc3_otg.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030049#include "core.h"
50#include "gadget.h"
51
Jack Pham0fc12332012-11-19 13:14:22 -080052/* ADC threshold values */
53static int adc_low_threshold = 700;
54module_param(adc_low_threshold, int, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(adc_low_threshold, "ADC ID Low voltage threshold");
56
57static int adc_high_threshold = 950;
58module_param(adc_high_threshold, int, S_IRUGO | S_IWUSR);
59MODULE_PARM_DESC(adc_high_threshold, "ADC ID High voltage threshold");
60
61static int adc_meas_interval = ADC_MEAS1_INTERVAL_1S;
62module_param(adc_meas_interval, int, S_IRUGO | S_IWUSR);
63MODULE_PARM_DESC(adc_meas_interval, "ADC ID polling period");
64
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +053065static int override_phy_init;
66module_param(override_phy_init, int, S_IRUGO|S_IWUSR);
67MODULE_PARM_DESC(override_phy_init, "Override HSPHY Init Seq");
68
Jack Pham9b4606b2013-04-02 17:32:25 -070069/* Enable Proprietary charger detection */
70static bool prop_chg_detect;
71module_param(prop_chg_detect, bool, S_IRUGO | S_IWUSR);
72MODULE_PARM_DESC(prop_chg_detect, "Enable Proprietary charger detection");
73
Ido Shayevitz9fb83452012-04-01 17:45:58 +030074/**
75 * USB DBM Hardware registers.
76 *
77 */
Shimrit Malichia00d7322012-08-05 13:56:28 +030078#define DBM_BASE 0x000F8000
79#define DBM_EP_CFG(n) (DBM_BASE + (0x00 + 4 * (n)))
80#define DBM_DATA_FIFO(n) (DBM_BASE + (0x10 + 4 * (n)))
81#define DBM_DATA_FIFO_SIZE(n) (DBM_BASE + (0x20 + 4 * (n)))
82#define DBM_DATA_FIFO_EN (DBM_BASE + (0x30))
83#define DBM_GEVNTADR (DBM_BASE + (0x34))
84#define DBM_GEVNTSIZ (DBM_BASE + (0x38))
85#define DBM_DBG_CNFG (DBM_BASE + (0x3C))
86#define DBM_HW_TRB0_EP(n) (DBM_BASE + (0x40 + 4 * (n)))
87#define DBM_HW_TRB1_EP(n) (DBM_BASE + (0x50 + 4 * (n)))
88#define DBM_HW_TRB2_EP(n) (DBM_BASE + (0x60 + 4 * (n)))
89#define DBM_HW_TRB3_EP(n) (DBM_BASE + (0x70 + 4 * (n)))
90#define DBM_PIPE_CFG (DBM_BASE + (0x80))
91#define DBM_SOFT_RESET (DBM_BASE + (0x84))
92#define DBM_GEN_CFG (DBM_BASE + (0x88))
Ido Shayevitz9fb83452012-04-01 17:45:58 +030093
94/**
95 * USB DBM Hardware registers bitmask.
96 *
97 */
98/* DBM_EP_CFG */
Shimrit Malichia00d7322012-08-05 13:56:28 +030099#define DBM_EN_EP 0x00000001
100#define USB3_EPNUM 0x0000003E
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300101#define DBM_BAM_PIPE_NUM 0x000000C0
102#define DBM_PRODUCER 0x00000100
103#define DBM_DISABLE_WB 0x00000200
104#define DBM_INT_RAM_ACC 0x00000400
105
106/* DBM_DATA_FIFO_SIZE */
107#define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff
108
109/* DBM_GEVNTSIZ */
110#define DBM_GEVNTSIZ_MASK 0x0000ffff
111
112/* DBM_DBG_CNFG */
113#define DBM_ENABLE_IOC_MASK 0x0000000f
114
115/* DBM_SOFT_RESET */
116#define DBM_SFT_RST_EP0 0x00000001
117#define DBM_SFT_RST_EP1 0x00000002
118#define DBM_SFT_RST_EP2 0x00000004
119#define DBM_SFT_RST_EP3 0x00000008
Shimrit Malichia00d7322012-08-05 13:56:28 +0300120#define DBM_SFT_RST_EPS_MASK 0x0000000F
121#define DBM_SFT_RST_MASK 0x80000000
122#define DBM_EN_MASK 0x00000002
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200123
124#define DBM_MAX_EPS 4
125
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300126/* DBM TRB configurations */
127#define DBM_TRB_BIT 0x80000000
128#define DBM_TRB_DATA_SRC 0x40000000
129#define DBM_TRB_DMA 0x20000000
130#define DBM_TRB_EP_NUM(ep) (ep<<24)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300131
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +0530132#define USB3_PORTSC (0x430)
133#define PORT_PE (0x1 << 1)
Manu Gautam8c642812012-06-07 10:35:10 +0530134/**
135 * USB QSCRATCH Hardware registers
136 *
137 */
138#define QSCRATCH_REG_OFFSET (0x000F8800)
Pavankumar Kondeti08693e72013-05-03 11:55:48 +0530139#define QSCRATCH_CTRL_REG (QSCRATCH_REG_OFFSET + 0x04)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300140#define QSCRATCH_GENERAL_CFG (QSCRATCH_REG_OFFSET + 0x08)
Manu Gautambd0e5782012-08-30 10:39:01 -0700141#define HS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x10)
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530142#define PARAMETER_OVERRIDE_X_REG (QSCRATCH_REG_OFFSET + 0x14)
Manu Gautam8c642812012-06-07 10:35:10 +0530143#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
144#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
145#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
146#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
Manu Gautamd4108b72012-12-14 17:35:18 +0530147#define CGCTL_REG (QSCRATCH_REG_OFFSET + 0x28)
Manu Gautambd0e5782012-08-30 10:39:01 -0700148#define SS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x30)
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +0530149#define SS_PHY_PARAM_CTRL_1 (QSCRATCH_REG_OFFSET + 0x34)
150#define SS_PHY_PARAM_CTRL_2 (QSCRATCH_REG_OFFSET + 0x38)
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530151#define SS_CR_PROTOCOL_DATA_IN_REG (QSCRATCH_REG_OFFSET + 0x3C)
152#define SS_CR_PROTOCOL_DATA_OUT_REG (QSCRATCH_REG_OFFSET + 0x40)
153#define SS_CR_PROTOCOL_CAP_ADDR_REG (QSCRATCH_REG_OFFSET + 0x44)
154#define SS_CR_PROTOCOL_CAP_DATA_REG (QSCRATCH_REG_OFFSET + 0x48)
155#define SS_CR_PROTOCOL_READ_REG (QSCRATCH_REG_OFFSET + 0x4C)
156#define SS_CR_PROTOCOL_WRITE_REG (QSCRATCH_REG_OFFSET + 0x50)
Manu Gautam8c642812012-06-07 10:35:10 +0530157
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300158struct dwc3_msm_req_complete {
159 struct list_head list_item;
160 struct usb_request *req;
161 void (*orig_complete)(struct usb_ep *ep,
162 struct usb_request *req);
163};
164
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200165struct dwc3_msm {
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200166 struct device *dev;
167 void __iomem *base;
168 u32 resource_size;
169 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300170 u8 ep_num_mapping[DBM_MAX_EPS];
171 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
172 struct list_head req_complete_list;
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +0530173 struct clk *xo_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700174 struct clk *ref_clk;
Manu Gautam1742db22012-06-19 13:33:24 +0530175 struct clk *core_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700176 struct clk *iface_clk;
177 struct clk *sleep_clk;
178 struct clk *hsphy_sleep_clk;
Jack Pham22698b82013-02-13 17:45:06 -0800179 struct clk *utmi_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530180 struct regulator *hsusb_3p3;
181 struct regulator *hsusb_1p8;
182 struct regulator *hsusb_vddcx;
183 struct regulator *ssusb_1p8;
184 struct regulator *ssusb_vddcx;
Hemant Kumar086bf6b2013-06-10 19:29:27 -0700185 struct regulator *dwc3_gdsc;
Manu Gautambb825d72013-03-12 16:25:42 +0530186
187 /* VBUS regulator if no OTG and running in host only mode */
188 struct regulator *vbus_otg;
Manu Gautamb5067272012-07-02 09:53:41 +0530189 struct dwc3_ext_xceiv ext_xceiv;
190 bool resume_pending;
191 atomic_t pm_suspended;
192 atomic_t in_lpm;
Manu Gautam377821c2012-09-28 16:53:24 +0530193 int hs_phy_irq;
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530194 int hsphy_init_seq;
Manu Gautam377821c2012-09-28 16:53:24 +0530195 bool lpm_irq_seen;
Manu Gautamb5067272012-07-02 09:53:41 +0530196 struct delayed_work resume_work;
Manu Gautam6eb13e32013-02-01 15:19:15 +0530197 struct work_struct restart_usb_work;
Manu Gautamb5067272012-07-02 09:53:41 +0530198 struct wake_lock wlock;
Manu Gautam8c642812012-06-07 10:35:10 +0530199 struct dwc3_charger charger;
200 struct usb_phy *otg_xceiv;
201 struct delayed_work chg_work;
202 enum usb_chg_state chg_state;
Jack Pham0cca9412013-03-08 13:22:42 -0800203 int pmic_id_irq;
204 struct work_struct id_work;
Siddartha Mohanadossa3e35512013-02-22 17:06:07 -0800205 struct qpnp_adc_tm_btm_param adc_param;
Jack Pham0fc12332012-11-19 13:14:22 -0800206 struct delayed_work init_adc_work;
207 bool id_adc_detect;
Manu Gautam8c642812012-06-07 10:35:10 +0530208 u8 dcd_retries;
Manu Gautam2617deb2012-08-31 17:50:06 -0700209 u32 bus_perf_client;
210 struct msm_bus_scale_pdata *bus_scale_table;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530211 struct power_supply usb_psy;
Jack Pham9354c6a2012-12-20 19:19:32 -0800212 struct power_supply *ext_vbus_psy;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530213 unsigned int online;
214 unsigned int host_mode;
215 unsigned int current_max;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +0530216 unsigned int vdd_no_vol_level;
217 unsigned int vdd_low_vol_level;
218 unsigned int vdd_high_vol_level;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530219 bool vbus_active;
Jack Phamfadd6432012-12-07 19:03:41 -0800220 bool ext_inuse;
Jack Phamf12b7e12012-12-28 14:27:26 -0800221 enum dwc3_id_state id_state;
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +0530222 unsigned long lpm_flags;
223#define MDWC3_CORECLK_OFF BIT(0)
224#define MDWC3_TCXO_SHUTDOWN BIT(1)
Pavankumar Kondeti08693e72013-05-03 11:55:48 +0530225
226 u32 qscratch_ctl_val;
227 dev_t ext_chg_dev;
228 struct cdev ext_chg_cdev;
229 struct class *ext_chg_class;
230 struct device *ext_chg_device;
231 bool ext_chg_opened;
232 bool ext_chg_active;
233 struct completion ext_chg_wait;
Manu Gautam60e01352012-05-29 09:00:34 +0530234};
235
236#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
237#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
238#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
239
240#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
241#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
242#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
243
244#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
245#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
246#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
247
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300248static struct dwc3_msm *context;
249
Jack Phamfadd6432012-12-07 19:03:41 -0800250static struct usb_ext_notification *usb_ext;
251
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300252/**
253 *
254 * Read register with debug info.
255 *
256 * @base - DWC3 base virtual address.
257 * @offset - register offset.
258 *
259 * @return u32
260 */
261static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
262{
263 u32 val = ioread32(base + offset);
264 return val;
265}
266
267/**
268 * Read register masked field with debug info.
269 *
270 * @base - DWC3 base virtual address.
271 * @offset - register offset.
272 * @mask - register bitmask.
273 *
274 * @return u32
275 */
276static inline u32 dwc3_msm_read_reg_field(void *base,
277 u32 offset,
278 const u32 mask)
279{
280 u32 shift = find_first_bit((void *)&mask, 32);
281 u32 val = ioread32(base + offset);
282 val &= mask; /* clear other bits */
283 val >>= shift;
284 return val;
285}
286
287/**
288 *
289 * Write register with debug info.
290 *
291 * @base - DWC3 base virtual address.
292 * @offset - register offset.
293 * @val - value to write.
294 *
295 */
296static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
297{
298 iowrite32(val, base + offset);
299}
300
301/**
302 * Write register masked field with debug info.
303 *
304 * @base - DWC3 base virtual address.
305 * @offset - register offset.
306 * @mask - register bitmask.
307 * @val - value to write.
308 *
309 */
310static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
311 const u32 mask, u32 val)
312{
313 u32 shift = find_first_bit((void *)&mask, 32);
314 u32 tmp = ioread32(base + offset);
315
316 tmp &= ~mask; /* clear written bits */
317 val = tmp | (val << shift);
318 iowrite32(val, base + offset);
319}
320
321/**
Manu Gautam8c642812012-06-07 10:35:10 +0530322 * Write register and read back masked value to confirm it is written
323 *
324 * @base - DWC3 base virtual address.
325 * @offset - register offset.
326 * @mask - register bitmask specifying what should be updated
327 * @val - value to write.
328 *
329 */
330static inline void dwc3_msm_write_readback(void *base, u32 offset,
331 const u32 mask, u32 val)
332{
333 u32 write_val, tmp = ioread32(base + offset);
334
335 tmp &= ~mask; /* retain other bits */
336 write_val = tmp | val;
337
338 iowrite32(write_val, base + offset);
339
340 /* Read back to see if val was written */
341 tmp = ioread32(base + offset);
342 tmp &= mask; /* clear other bits */
343
344 if (tmp != val)
345 dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n",
346 __func__, val, offset);
347}
348
349/**
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530350 *
351 * Write SSPHY register with debug info.
352 *
353 * @base - DWC3 base virtual address.
354 * @addr - SSPHY address to write.
355 * @val - value to write.
356 *
357 */
358static void dwc3_msm_ssusb_write_phycreg(void *base, u32 addr, u32 val)
359{
360 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
361 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
362 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
363 cpu_relax();
364
365 iowrite32(val, base + SS_CR_PROTOCOL_DATA_IN_REG);
366 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_DATA_REG);
367 while (ioread32(base + SS_CR_PROTOCOL_CAP_DATA_REG))
368 cpu_relax();
369
370 iowrite32(0x1, base + SS_CR_PROTOCOL_WRITE_REG);
371 while (ioread32(base + SS_CR_PROTOCOL_WRITE_REG))
372 cpu_relax();
373}
374
375/**
376 *
377 * Read SSPHY register with debug info.
378 *
379 * @base - DWC3 base virtual address.
380 * @addr - SSPHY address to read.
381 *
382 */
383static u32 dwc3_msm_ssusb_read_phycreg(void *base, u32 addr)
384{
Vijayavardhan Vennapusa96201212013-06-12 19:59:27 +0530385 bool first_read = true;
386
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530387 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
388 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
389 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
390 cpu_relax();
391
Vijayavardhan Vennapusa96201212013-06-12 19:59:27 +0530392 /*
393 * Due to hardware bug, first read of SSPHY register might be
394 * incorrect. Hence as workaround, SW should perform SSPHY register
395 * read twice, but use only second read and ignore first read.
396 */
397retry:
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530398 iowrite32(0x1, base + SS_CR_PROTOCOL_READ_REG);
399 while (ioread32(base + SS_CR_PROTOCOL_READ_REG))
400 cpu_relax();
401
Vijayavardhan Vennapusa96201212013-06-12 19:59:27 +0530402 if (first_read) {
403 ioread32(base + SS_CR_PROTOCOL_DATA_OUT_REG);
404 first_read = false;
405 goto retry;
406 }
407
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530408 return ioread32(base + SS_CR_PROTOCOL_DATA_OUT_REG);
409}
410
411/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300412 * Return DBM EP number according to usb endpoint number.
413 *
414 */
415static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep)
416{
417 int i;
418
419 for (i = 0; i < context->dbm_num_eps; i++)
420 if (context->ep_num_mapping[i] == usb_ep)
421 return i;
422
423 return -ENODEV; /* Not found */
424}
425
426/**
427 * Return number of configured DBM endpoints.
428 *
429 */
430static int dwc3_msm_configured_dbm_ep_num(void)
431{
432 int i;
433 int count = 0;
434
435 for (i = 0; i < context->dbm_num_eps; i++)
436 if (context->ep_num_mapping[i])
437 count++;
438
439 return count;
440}
441
442/**
443 * Configure the DBM with the USB3 core event buffer.
444 * This function is called by the SNPS UDC upon initialization.
445 *
446 * @addr - address of the event buffer.
447 * @size - size of the event buffer.
448 *
449 */
450static int dwc3_msm_event_buffer_config(u32 addr, u16 size)
451{
452 dev_dbg(context->dev, "%s\n", __func__);
453
454 dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr);
455 dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ,
456 DBM_GEVNTSIZ_MASK, size);
457
458 return 0;
459}
460
461/**
462 * Reset the DBM registers upon initialization.
463 *
464 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300465static int dwc3_msm_dbm_soft_reset(int enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300466{
467 dev_dbg(context->dev, "%s\n", __func__);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300468 if (enter_reset) {
469 dev_dbg(context->dev, "enter DBM reset\n");
470 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
471 DBM_SFT_RST_MASK, 1);
472 } else {
473 dev_dbg(context->dev, "exit DBM reset\n");
474 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
475 DBM_SFT_RST_MASK, 0);
476 /*enable DBM*/
477 dwc3_msm_write_reg_field(context->base, QSCRATCH_GENERAL_CFG,
478 DBM_EN_MASK, 0x1);
479 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300480
481 return 0;
482}
483
484/**
485 * Soft reset specific DBM ep.
486 * This function is called by the function driver upon events
487 * such as transfer aborting, USB re-enumeration and USB
488 * disconnection.
489 *
490 * @dbm_ep - DBM ep number.
491 * @enter_reset - should we enter a reset state or get out of it.
492 *
493 */
494static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset)
495{
496 dev_dbg(context->dev, "%s\n", __func__);
497
498 if (dbm_ep >= context->dbm_num_eps) {
499 dev_err(context->dev,
500 "%s: Invalid DBM ep index\n", __func__);
501 return -ENODEV;
502 }
503
504 if (enter_reset) {
505 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300506 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300507 } else {
508 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300509 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300510 }
511
512 return 0;
513}
514
515/**
516 * Configure a USB DBM ep to work in BAM mode.
517 *
518 *
519 * @usb_ep - USB physical EP number.
520 * @producer - producer/consumer.
521 * @disable_wb - disable write back to system memory.
522 * @internal_mem - use internal USB memory for data fifo.
523 * @ioc - enable interrupt on completion.
524 *
525 * @return int - DBM ep number.
526 */
527static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe,
528 bool producer, bool disable_wb,
529 bool internal_mem, bool ioc)
530{
531 u8 dbm_ep;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300532 u32 ep_cfg;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300533
534 dev_dbg(context->dev, "%s\n", __func__);
535
Shimrit Malichia00d7322012-08-05 13:56:28 +0300536 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
537
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300538 if (dbm_ep < 0) {
Shimrit Malichia00d7322012-08-05 13:56:28 +0300539 dev_err(context->dev,
540 "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300541 return -ENODEV;
542 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300543 /* First, reset the dbm endpoint */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300544 dwc3_msm_dbm_ep_soft_reset(dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300545
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300546 /* Set ioc bit for dbm_ep if needed */
547 dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300548 DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300549
Shimrit Malichia00d7322012-08-05 13:56:28 +0300550 ep_cfg = (producer ? DBM_PRODUCER : 0) |
551 (disable_wb ? DBM_DISABLE_WB : 0) |
552 (internal_mem ? DBM_INT_RAM_ACC : 0);
553
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300554 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
Shimrit Malichia00d7322012-08-05 13:56:28 +0300555 DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8);
556
557 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM,
558 usb_ep);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300559 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
560 DBM_BAM_PIPE_NUM, bam_pipe);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300561 dwc3_msm_write_reg_field(context->base, DBM_PIPE_CFG, 0x000000ff,
562 0xe4);
563 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP,
564 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300565
566 return dbm_ep;
567}
568
569/**
570 * Configure a USB DBM ep to work in normal mode.
571 *
572 * @usb_ep - USB ep number.
573 *
574 */
575static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep)
576{
577 u8 dbm_ep;
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530578 u32 data;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300579
580 dev_dbg(context->dev, "%s\n", __func__);
581
582 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
583
584 if (dbm_ep < 0) {
585 dev_err(context->dev,
586 "%s: Invalid usb ep index\n", __func__);
587 return -ENODEV;
588 }
589
590 context->ep_num_mapping[dbm_ep] = 0;
591
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530592 data = dwc3_msm_read_reg(context->base, DBM_EP_CFG(dbm_ep));
593 data &= (~0x1);
594 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), data);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300595
596 /* Reset the dbm endpoint */
597 dwc3_msm_dbm_ep_soft_reset(dbm_ep, true);
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530598 /*
599 * 10 usec delay is required before deasserting DBM endpoint reset
600 * according to hardware programming guide.
601 */
602 udelay(10);
603 dwc3_msm_dbm_ep_soft_reset(dbm_ep, false);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300604
605 return 0;
606}
607
608/**
609 * Configure the DBM with the BAM's data fifo.
610 * This function is called by the USB BAM Driver
611 * upon initialization.
612 *
613 * @ep - pointer to usb endpoint.
614 * @addr - address of data fifo.
615 * @size - size of data fifo.
616 *
617 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300618int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300619{
620 u8 dbm_ep;
621 struct dwc3_ep *dep = to_dwc3_ep(ep);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300622 u8 bam_pipe = dst_pipe_idx;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300623
624 dev_dbg(context->dev, "%s\n", __func__);
625
Shimrit Malichia00d7322012-08-05 13:56:28 +0300626 dbm_ep = bam_pipe;
627 context->ep_num_mapping[dbm_ep] = dep->number;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300628
629 dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr);
630 dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep),
631 DBM_DATA_FIFO_SIZE_MASK, size);
632
633 return 0;
634}
635
636/**
637* Cleanups for msm endpoint on request complete.
638*
639* Also call original request complete.
640*
641* @usb_ep - pointer to usb_ep instance.
642* @request - pointer to usb_request instance.
643*
644* @return int - 0 on success, negetive on error.
645*/
646static void dwc3_msm_req_complete_func(struct usb_ep *ep,
647 struct usb_request *request)
648{
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300649 struct dwc3_ep *dep = to_dwc3_ep(ep);
650 struct dwc3_msm_req_complete *req_complete = NULL;
651
652 /* Find original request complete function and remove it from list */
653 list_for_each_entry(req_complete,
654 &context->req_complete_list,
655 list_item) {
656 if (req_complete->req == request)
657 break;
658 }
659 if (!req_complete || req_complete->req != request) {
660 dev_err(dep->dwc->dev, "%s: could not find the request\n",
661 __func__);
662 return;
663 }
664 list_del(&req_complete->list_item);
665
666 /*
667 * Release another one TRB to the pool since DBM queue took 2 TRBs
668 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
669 * released only one.
670 */
Manu Gautam55d34222012-12-19 16:49:47 +0530671 dep->busy_slot++;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300672
673 /* Unconfigure dbm ep */
674 dwc3_msm_dbm_ep_unconfig(dep->number);
675
676 /*
677 * If this is the last endpoint we unconfigured, than reset also
678 * the event buffers.
679 */
680 if (0 == dwc3_msm_configured_dbm_ep_num())
681 dwc3_msm_event_buffer_config(0, 0);
682
683 /*
684 * Call original complete function, notice that dwc->lock is already
685 * taken by the caller of this function (dwc3_gadget_giveback()).
686 */
687 request->complete = req_complete->orig_complete;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300688 if (request->complete)
689 request->complete(ep, request);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300690
691 kfree(req_complete);
692}
693
694/**
695* Helper function.
696* See the header of the dwc3_msm_ep_queue function.
697*
698* @dwc3_ep - pointer to dwc3_ep instance.
699* @req - pointer to dwc3_request instance.
700*
701* @return int - 0 on success, negetive on error.
702*/
703static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
704{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300705 struct dwc3_trb *trb;
706 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300707 struct dwc3_gadget_ep_cmd_params params;
708 u32 cmd;
709 int ret = 0;
710
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300711 /* We push the request to the dep->req_queued list to indicate that
712 * this request is issued with start transfer. The request will be out
713 * from this list in 2 cases. The first is that the transfer will be
714 * completed (not if the transfer is endless using a circular TRBs with
715 * with link TRB). The second case is an option to do stop stransfer,
716 * this can be initiated by the function driver when calling dequeue.
717 */
718 req->queued = true;
719 list_add_tail(&req->list, &dep->req_queued);
720
721 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300722 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300723 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300724 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300725
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300726 req->trb = trb;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300727 trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300728 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
729 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300730 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300731
732 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300733 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300734 dep->free_slot++;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300735 memset(trb_link, 0, sizeof *trb_link);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300736
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300737 trb_link->bpl = lower_32_bits(req->trb_dma);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300738 trb_link->bph = DBM_TRB_BIT |
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300739 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
740 trb_link->size = 0;
741 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300742
743 /*
744 * Now start the transfer
745 */
746 memset(&params, 0, sizeof(params));
Shimrit Malichia00d7322012-08-05 13:56:28 +0300747 params.param0 = 0; /* TDAddr High */
748 params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */
749
Manu Gautam5b2bf9a2012-10-18 10:52:50 +0530750 /* DBM requires IOC to be set */
751 cmd = DWC3_DEPCMD_STARTTRANSFER | DWC3_DEPCMD_CMDIOC;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300752 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
753 if (ret < 0) {
754 dev_dbg(dep->dwc->dev,
755 "%s: failed to send STARTTRANSFER command\n",
756 __func__);
757
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300758 list_del(&req->list);
759 return ret;
760 }
Manu Gautam4a51a062012-12-07 11:24:39 +0530761 dep->flags |= DWC3_EP_BUSY;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300762
763 return ret;
764}
765
766/**
767* Queue a usb request to the DBM endpoint.
768* This function should be called after the endpoint
769* was enabled by the ep_enable.
770*
771* This function prepares special structure of TRBs which
772* is familier with the DBM HW, so it will possible to use
773* this endpoint in DBM mode.
774*
775* The TRBs prepared by this function, is one normal TRB
776* which point to a fake buffer, followed by a link TRB
777* that points to the first TRB.
778*
779* The API of this function follow the regular API of
780* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
781*
782* @usb_ep - pointer to usb_ep instance.
783* @request - pointer to usb_request instance.
784* @gfp_flags - possible flags.
785*
786* @return int - 0 on success, negetive on error.
787*/
788static int dwc3_msm_ep_queue(struct usb_ep *ep,
789 struct usb_request *request, gfp_t gfp_flags)
790{
791 struct dwc3_request *req = to_dwc3_request(request);
792 struct dwc3_ep *dep = to_dwc3_ep(ep);
793 struct dwc3 *dwc = dep->dwc;
794 struct dwc3_msm_req_complete *req_complete;
795 unsigned long flags;
796 int ret = 0;
797 u8 bam_pipe;
798 bool producer;
799 bool disable_wb;
800 bool internal_mem;
801 bool ioc;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300802 u8 speed;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300803
804 if (!(request->udc_priv & MSM_SPS_MODE)) {
805 /* Not SPS mode, call original queue */
806 dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n",
807 __func__);
808
809 return (context->original_ep_ops[dep->number])->queue(ep,
810 request,
811 gfp_flags);
812 }
813
814 if (!dep->endpoint.desc) {
815 dev_err(dwc->dev,
816 "%s: trying to queue request %p to disabled ep %s\n",
817 __func__, request, ep->name);
818 return -EPERM;
819 }
820
821 if (dep->number == 0 || dep->number == 1) {
822 dev_err(dwc->dev,
823 "%s: trying to queue dbm request %p to control ep %s\n",
824 __func__, request, ep->name);
825 return -EPERM;
826 }
827
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300828
Manu Gautam4a51a062012-12-07 11:24:39 +0530829 if (dep->busy_slot != dep->free_slot || !list_empty(&dep->request_list)
830 || !list_empty(&dep->req_queued)) {
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300831 dev_err(dwc->dev,
832 "%s: trying to queue dbm request %p tp ep %s\n",
833 __func__, request, ep->name);
834 return -EPERM;
Manu Gautam4a51a062012-12-07 11:24:39 +0530835 } else {
836 dep->busy_slot = 0;
837 dep->free_slot = 0;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300838 }
839
840 /*
841 * Override req->complete function, but before doing that,
842 * store it's original pointer in the req_complete_list.
843 */
844 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
845 if (!req_complete) {
846 dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__);
847 return -ENOMEM;
848 }
849 req_complete->req = request;
850 req_complete->orig_complete = request->complete;
851 list_add_tail(&req_complete->list_item, &context->req_complete_list);
852 request->complete = dwc3_msm_req_complete_func;
853
854 /*
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300855 * Configure the DBM endpoint
856 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300857 bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300858 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
859 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
860 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
861 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
862
863 ret = dwc3_msm_dbm_ep_config(dep->number,
864 bam_pipe, producer,
865 disable_wb, internal_mem, ioc);
866 if (ret < 0) {
867 dev_err(context->dev,
868 "error %d after calling dwc3_msm_dbm_ep_config\n",
869 ret);
870 return ret;
871 }
872
873 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
874 __func__, request, ep->name, request->length);
875
876 /*
877 * We must obtain the lock of the dwc3 core driver,
878 * including disabling interrupts, so we will be sure
879 * that we are the only ones that configure the HW device
880 * core and ensure that we queuing the request will finish
881 * as soon as possible so we will release back the lock.
882 */
883 spin_lock_irqsave(&dwc->lock, flags);
884 ret = __dwc3_msm_ep_queue(dep, req);
885 spin_unlock_irqrestore(&dwc->lock, flags);
886 if (ret < 0) {
887 dev_err(context->dev,
888 "error %d after calling __dwc3_msm_ep_queue\n", ret);
889 return ret;
890 }
891
Shimrit Malichia00d7322012-08-05 13:56:28 +0300892 speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD;
893 dwc3_msm_write_reg(context->base, DBM_GEN_CFG, speed >> 2);
894
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300895 return 0;
896}
897
898/**
899 * Configure MSM endpoint.
900 * This function do specific configurations
901 * to an endpoint which need specific implementaion
902 * in the MSM architecture.
903 *
904 * This function should be called by usb function/class
905 * layer which need a support from the specific MSM HW
906 * which wrap the USB3 core. (like DBM specific endpoints)
907 *
908 * @ep - a pointer to some usb_ep instance
909 *
910 * @return int - 0 on success, negetive on error.
911 */
912int msm_ep_config(struct usb_ep *ep)
913{
914 struct dwc3_ep *dep = to_dwc3_ep(ep);
915 struct usb_ep_ops *new_ep_ops;
916
Manu Gautama302f612012-12-18 17:33:06 +0530917 dwc3_msm_event_buffer_config(dwc3_msm_read_reg(context->base,
918 DWC3_GEVNTADRLO(0)),
919 dwc3_msm_read_reg(context->base, DWC3_GEVNTSIZ(0)));
920
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300921 /* Save original ep ops for future restore*/
922 if (context->original_ep_ops[dep->number]) {
923 dev_err(context->dev,
924 "ep [%s,%d] already configured as msm endpoint\n",
925 ep->name, dep->number);
926 return -EPERM;
927 }
928 context->original_ep_ops[dep->number] = ep->ops;
929
930 /* Set new usb ops as we like */
931 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
932 if (!new_ep_ops) {
933 dev_err(context->dev,
934 "%s: unable to allocate mem for new usb ep ops\n",
935 __func__);
936 return -ENOMEM;
937 }
938 (*new_ep_ops) = (*ep->ops);
939 new_ep_ops->queue = dwc3_msm_ep_queue;
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530940 new_ep_ops->disable = ep->ops->disable;
941
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300942 ep->ops = new_ep_ops;
943
944 /*
945 * Do HERE more usb endpoint configurations
946 * which are specific to MSM.
947 */
948
949 return 0;
950}
951EXPORT_SYMBOL(msm_ep_config);
952
953/**
954 * Un-configure MSM endpoint.
955 * Tear down configurations done in the
956 * dwc3_msm_ep_config function.
957 *
958 * @ep - a pointer to some usb_ep instance
959 *
960 * @return int - 0 on success, negetive on error.
961 */
962int msm_ep_unconfig(struct usb_ep *ep)
963{
964 struct dwc3_ep *dep = to_dwc3_ep(ep);
965 struct usb_ep_ops *old_ep_ops;
966
967 /* Restore original ep ops */
968 if (!context->original_ep_ops[dep->number]) {
969 dev_err(context->dev,
970 "ep [%s,%d] was not configured as msm endpoint\n",
971 ep->name, dep->number);
972 return -EINVAL;
973 }
974 old_ep_ops = (struct usb_ep_ops *)ep->ops;
975 ep->ops = context->original_ep_ops[dep->number];
976 context->original_ep_ops[dep->number] = NULL;
977 kfree(old_ep_ops);
978
979 /*
980 * Do HERE more usb endpoint un-configurations
981 * which are specific to MSM.
982 */
983
984 return 0;
985}
986EXPORT_SYMBOL(msm_ep_unconfig);
987
Manu Gautam6eb13e32013-02-01 15:19:15 +0530988static void dwc3_restart_usb_work(struct work_struct *w)
989{
990 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
991 restart_usb_work);
992
993 dev_dbg(mdwc->dev, "%s\n", __func__);
994
995 if (atomic_read(&mdwc->in_lpm) || !mdwc->otg_xceiv) {
996 dev_err(mdwc->dev, "%s failed!!!\n", __func__);
997 return;
998 }
999
1000 if (!mdwc->ext_xceiv.bsv) {
1001 dev_dbg(mdwc->dev, "%s bailing out in disconnect\n", __func__);
1002 return;
1003 }
1004
1005 /* Reset active USB connection */
1006 mdwc->ext_xceiv.bsv = false;
1007 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
1008 /* Make sure disconnect is processed before sending connect */
1009 flush_delayed_work(&mdwc->resume_work);
1010
1011 mdwc->ext_xceiv.bsv = true;
1012 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
1013}
1014
1015/**
1016 * Reset USB peripheral connection
1017 * Inform OTG for Vbus LOW followed by Vbus HIGH notification.
1018 * This performs full hardware reset and re-initialization which
1019 * might be required by some DBM client driver during uninit/cleanup.
1020 */
1021void msm_dwc3_restart_usb_session(void)
1022{
1023 struct dwc3_msm *mdwc = context;
1024
1025 dev_dbg(mdwc->dev, "%s\n", __func__);
1026 queue_work(system_nrt_wq, &mdwc->restart_usb_work);
1027
1028 return;
1029}
1030EXPORT_SYMBOL(msm_dwc3_restart_usb_session);
1031
Jack Phamfadd6432012-12-07 19:03:41 -08001032/**
1033 * msm_register_usb_ext_notification: register for event notification
1034 * @info: pointer to client usb_ext_notification structure. May be NULL.
1035 *
1036 * @return int - 0 on success, negative on error
1037 */
1038int msm_register_usb_ext_notification(struct usb_ext_notification *info)
1039{
1040 pr_debug("%s usb_ext: %p\n", __func__, info);
1041
1042 if (info) {
1043 if (usb_ext) {
1044 pr_err("%s: already registered\n", __func__);
1045 return -EEXIST;
1046 }
1047
1048 if (!info->notify) {
1049 pr_err("%s: notify is NULL\n", __func__);
1050 return -EINVAL;
1051 }
1052 }
1053
1054 usb_ext = info;
1055 return 0;
1056}
1057EXPORT_SYMBOL(msm_register_usb_ext_notification);
1058
Manu Gautam60e01352012-05-29 09:00:34 +05301059/* HSPHY */
1060static int dwc3_hsusb_config_vddcx(int high)
1061{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301062 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301063 struct dwc3_msm *dwc = context;
Manu Gautam60e01352012-05-29 09:00:34 +05301064
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301065 max_vol = dwc->vdd_high_vol_level;
1066 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301067 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
1068 if (ret) {
1069 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
1070 return ret;
1071 }
1072
1073 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1074 min_vol, max_vol);
1075
1076 return ret;
1077}
1078
1079static int dwc3_hsusb_ldo_init(int init)
1080{
1081 int rc = 0;
1082 struct dwc3_msm *dwc = context;
1083
1084 if (!init) {
1085 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
1086 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1087 return 0;
1088 }
1089
1090 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
1091 if (IS_ERR(dwc->hsusb_3p3)) {
1092 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
1093 return PTR_ERR(dwc->hsusb_3p3);
1094 }
1095
1096 rc = regulator_set_voltage(dwc->hsusb_3p3,
1097 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
1098 if (rc) {
1099 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
1100 return rc;
1101 }
1102 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
1103 if (IS_ERR(dwc->hsusb_1p8)) {
1104 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
1105 rc = PTR_ERR(dwc->hsusb_1p8);
1106 goto devote_3p3;
1107 }
1108 rc = regulator_set_voltage(dwc->hsusb_1p8,
1109 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
1110 if (rc) {
1111 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
1112 goto devote_3p3;
1113 }
1114
1115 return 0;
1116
1117devote_3p3:
1118 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1119
1120 return rc;
1121}
1122
1123static int dwc3_hsusb_ldo_enable(int on)
1124{
1125 int rc = 0;
1126 struct dwc3_msm *dwc = context;
1127
1128 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1129
1130 if (!on)
1131 goto disable_regulators;
1132
1133
1134 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
1135 if (rc < 0) {
1136 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
1137 return rc;
1138 }
1139
1140 rc = regulator_enable(dwc->hsusb_1p8);
1141 if (rc) {
1142 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
1143 goto put_1p8_lpm;
1144 }
1145
1146 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
1147 if (rc < 0) {
1148 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
1149 goto disable_1p8;
1150 }
1151
1152 rc = regulator_enable(dwc->hsusb_3p3);
1153 if (rc) {
1154 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
1155 goto put_3p3_lpm;
1156 }
1157
1158 return 0;
1159
1160disable_regulators:
1161 rc = regulator_disable(dwc->hsusb_3p3);
1162 if (rc)
1163 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
1164
1165put_3p3_lpm:
1166 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
1167 if (rc < 0)
1168 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
1169
1170disable_1p8:
1171 rc = regulator_disable(dwc->hsusb_1p8);
1172 if (rc)
1173 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
1174
1175put_1p8_lpm:
1176 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
1177 if (rc < 0)
1178 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
1179
1180 return rc < 0 ? rc : 0;
1181}
1182
1183/* SSPHY */
1184static int dwc3_ssusb_config_vddcx(int high)
1185{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301186 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301187 struct dwc3_msm *dwc = context;
Manu Gautam60e01352012-05-29 09:00:34 +05301188
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301189 max_vol = dwc->vdd_high_vol_level;
1190 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301191 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
1192 if (ret) {
1193 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
1194 return ret;
1195 }
1196
1197 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1198 min_vol, max_vol);
1199 return ret;
1200}
1201
1202/* 3.3v supply not needed for SS PHY */
1203static int dwc3_ssusb_ldo_init(int init)
1204{
1205 int rc = 0;
1206 struct dwc3_msm *dwc = context;
1207
1208 if (!init) {
1209 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
1210 return 0;
1211 }
1212
1213 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
1214 if (IS_ERR(dwc->ssusb_1p8)) {
1215 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
1216 return PTR_ERR(dwc->ssusb_1p8);
1217 }
1218 rc = regulator_set_voltage(dwc->ssusb_1p8,
1219 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
1220 if (rc)
1221 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
1222
1223 return rc;
1224}
1225
1226static int dwc3_ssusb_ldo_enable(int on)
1227{
1228 int rc = 0;
1229 struct dwc3_msm *dwc = context;
1230
1231 dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1232
1233 if (!on)
1234 goto disable_regulators;
1235
1236
1237 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1238 if (rc < 0) {
1239 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1240 return rc;
1241 }
1242
1243 rc = regulator_enable(dwc->ssusb_1p8);
1244 if (rc) {
1245 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1246 goto put_1p8_lpm;
1247 }
1248
1249 return 0;
1250
1251disable_regulators:
1252 rc = regulator_disable(dwc->ssusb_1p8);
1253 if (rc)
1254 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1255
1256put_1p8_lpm:
1257 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1258 if (rc < 0)
1259 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1260
1261 return rc < 0 ? rc : 0;
1262}
1263
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001264/*
1265 * Config Global Distributed Switch Controller (GDSC)
1266 * to support controller power collapse
1267 */
1268static int dwc3_msm_config_gdsc(struct dwc3_msm *msm, int on)
1269{
1270 int ret = 0;
1271
1272 if (IS_ERR(msm->dwc3_gdsc))
1273 return 0;
1274
1275 if (!msm->dwc3_gdsc) {
1276 msm->dwc3_gdsc = devm_regulator_get(msm->dev,
1277 "USB3_GDSC");
1278 if (IS_ERR(msm->dwc3_gdsc))
1279 return 0;
1280 }
1281
1282 if (on) {
1283 ret = regulator_enable(msm->dwc3_gdsc);
1284 if (ret) {
1285 dev_err(msm->dev, "unable to enable usb3 gdsc\n");
1286 return ret;
1287 }
1288 } else {
1289 regulator_disable(msm->dwc3_gdsc);
1290 }
1291
1292 return 0;
1293}
1294
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301295static int dwc3_msm_link_clk_reset(bool assert)
1296{
1297 int ret = 0;
1298 struct dwc3_msm *mdwc = context;
1299
1300 if (assert) {
1301 /* Using asynchronous block reset to the hardware */
1302 dev_dbg(mdwc->dev, "block_reset ASSERT\n");
1303 clk_disable_unprepare(mdwc->ref_clk);
1304 clk_disable_unprepare(mdwc->iface_clk);
1305 clk_disable_unprepare(mdwc->core_clk);
1306 ret = clk_reset(mdwc->core_clk, CLK_RESET_ASSERT);
1307 if (ret)
1308 dev_err(mdwc->dev, "dwc3 core_clk assert failed\n");
1309 } else {
1310 dev_dbg(mdwc->dev, "block_reset DEASSERT\n");
1311 ret = clk_reset(mdwc->core_clk, CLK_RESET_DEASSERT);
1312 ndelay(200);
1313 clk_prepare_enable(mdwc->core_clk);
1314 clk_prepare_enable(mdwc->ref_clk);
1315 clk_prepare_enable(mdwc->iface_clk);
1316 if (ret)
1317 dev_err(mdwc->dev, "dwc3 core_clk deassert failed\n");
1318 }
1319
1320 return ret;
1321}
1322
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301323/* Reinitialize SSPHY parameters by overriding using QSCRATCH CR interface */
1324static void dwc3_msm_ss_phy_reg_init(struct dwc3_msm *msm)
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301325{
1326 u32 data = 0;
1327
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301328 /*
1329 * WORKAROUND: There is SSPHY suspend bug due to which USB enumerates
1330 * in HS mode instead of SS mode. Workaround it by asserting
1331 * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus mode
1332 */
1333 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x102D);
1334 data |= (1 << 7);
1335 dwc3_msm_ssusb_write_phycreg(msm->base, 0x102D, data);
1336
1337 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1010);
1338 data &= ~0xFF0;
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301339 data |= 0x20;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301340 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1010, data);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301341
1342 /*
1343 * Fix RX Equalization setting as follows
1344 * LANE0.RX_OVRD_IN_HI. RX_EQ_EN set to 0
1345 * LANE0.RX_OVRD_IN_HI.RX_EQ_EN_OVRD set to 1
1346 * LANE0.RX_OVRD_IN_HI.RX_EQ set to 3
1347 * LANE0.RX_OVRD_IN_HI.RX_EQ_OVRD set to 1
1348 */
1349 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1006);
1350 data &= ~(1 << 6);
1351 data |= (1 << 7);
1352 data &= ~(0x7 << 8);
1353 data |= (0x3 << 8);
1354 data |= (0x1 << 11);
1355 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1006, data);
1356
1357 /*
1358 * Set EQ and TX launch amplitudes as follows
1359 * LANE0.TX_OVRD_DRV_LO.PREEMPH set to 22
1360 * LANE0.TX_OVRD_DRV_LO.AMPLITUDE set to 127
1361 * LANE0.TX_OVRD_DRV_LO.EN set to 1.
1362 */
1363 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1002);
1364 data &= ~0x3F80;
1365 data |= (0x16 << 7);
1366 data &= ~0x7F;
1367 data |= (0x7F | (1 << 14));
1368 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1002, data);
1369
Jack Pham63c8c702013-04-24 19:21:33 -07001370 /*
1371 * Set the QSCRATCH SS_PHY_PARAM_CTRL1 parameters as follows
1372 * TX_FULL_SWING [26:20] amplitude to 127
1373 * TX_DEEMPH_3_5DB [13:8] to 22
1374 * LOS_BIAS [2:0] to 0x5
1375 */
1376 dwc3_msm_write_readback(msm->base, SS_PHY_PARAM_CTRL_1,
1377 0x07f03f07, 0x07f01605);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301378}
1379
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301380/* Initialize QSCRATCH registers for HSPHY and SSPHY operation */
1381static void dwc3_msm_qscratch_reg_init(struct dwc3_msm *msm)
1382{
1383 /* SSPHY Initialization: Use ref_clk from pads and set its parameters */
1384 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1385 msleep(30);
1386 /* Assert SSPHY reset */
1387 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210082);
1388 usleep_range(2000, 2200);
1389 /* De-assert SSPHY reset - power and ref_clock must be ON */
1390 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1391 usleep_range(2000, 2200);
1392 /* Ref clock must be stable now, enable ref clock for HS mode */
1393 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210102);
1394 usleep_range(2000, 2200);
1395 /*
1396 * HSPHY Initialization: Enable UTMI clock and clamp enable HVINTs,
1397 * and disable RETENTION (power-on default is ENABLED)
1398 */
1399 dwc3_msm_write_reg(msm->base, HS_PHY_CTRL_REG, 0x5220bb2);
1400 usleep_range(2000, 2200);
1401 /* Disable (bypass) VBUS and ID filters */
1402 dwc3_msm_write_reg(msm->base, QSCRATCH_GENERAL_CFG, 0x78);
1403 /*
1404 * write HSPHY init value to QSCRATCH reg to set HSPHY parameters like
1405 * VBUS valid threshold, disconnect valid threshold, DC voltage level,
1406 * preempasis and rise/fall time.
1407 */
1408 if (override_phy_init)
1409 msm->hsphy_init_seq = override_phy_init;
1410 if (msm->hsphy_init_seq)
1411 dwc3_msm_write_readback(msm->base,
1412 PARAMETER_OVERRIDE_X_REG, 0x03FFFFFF,
1413 msm->hsphy_init_seq & 0x03FFFFFF);
1414
1415 /* Enable master clock for RAMs to allow BAM to access RAMs when
1416 * RAM clock gating is enabled via DWC3's GCTL. Otherwise, issues
1417 * are seen where RAM clocks get turned OFF in SS mode
1418 */
1419 dwc3_msm_write_reg(msm->base, CGCTL_REG,
1420 dwc3_msm_read_reg(msm->base, CGCTL_REG) | 0x18);
1421
1422 dwc3_msm_ss_phy_reg_init(msm);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301423 /*
1424 * This is required to restore the POR value after userspace
1425 * is done with charger detection.
1426 */
1427 msm->qscratch_ctl_val = dwc3_msm_read_reg(msm->base, QSCRATCH_CTRL_REG);
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301428}
1429
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301430static void dwc3_msm_block_reset(bool core_reset)
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301431{
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301432
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301433 struct dwc3_msm *mdwc = context;
1434 int ret = 0;
1435
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301436 if (core_reset) {
1437 ret = dwc3_msm_link_clk_reset(1);
1438 if (ret)
1439 return;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301440
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301441 usleep_range(1000, 1200);
1442 ret = dwc3_msm_link_clk_reset(0);
1443 if (ret)
1444 return;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301445
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301446 usleep_range(10000, 12000);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301447
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301448 /* Reinitialize QSCRATCH registers after block reset */
1449 dwc3_msm_qscratch_reg_init(mdwc);
1450 }
Manu Gautama302f612012-12-18 17:33:06 +05301451
1452 /* Reset the DBM */
1453 dwc3_msm_dbm_soft_reset(1);
1454 usleep_range(1000, 1200);
1455 dwc3_msm_dbm_soft_reset(0);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301456}
1457
Manu Gautam8c642812012-06-07 10:35:10 +05301458static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1459{
1460 u32 chg_ctrl;
1461
1462 /* Turn off VDP_SRC */
1463 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1464 msleep(20);
1465
1466 /* Before proceeding make sure VDP_SRC is OFF */
1467 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1468 if (chg_ctrl & 0x3F)
1469 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1470 __func__, chg_ctrl);
1471 /*
1472 * Configure DM as current source, DP as current sink
1473 * and enable battery charging comparators.
1474 */
1475 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1476}
1477
Manu Gautama1e331d2013-02-07 14:55:05 +05301478static bool dwc3_chg_det_check_linestate(struct dwc3_msm *mdwc)
1479{
1480 u32 chg_det;
Jack Pham9b4606b2013-04-02 17:32:25 -07001481
1482 if (!prop_chg_detect)
1483 return false;
Manu Gautama1e331d2013-02-07 14:55:05 +05301484
1485 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
Jack Pham9b4606b2013-04-02 17:32:25 -07001486 return chg_det & (3 << 8);
Manu Gautama1e331d2013-02-07 14:55:05 +05301487}
1488
Manu Gautam8c642812012-06-07 10:35:10 +05301489static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1490{
1491 u32 chg_det;
1492 bool ret = false;
1493
1494 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1495 ret = chg_det & 1;
1496
1497 return ret;
1498}
1499
1500static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1501{
1502 /*
1503 * Configure DP as current source, DM as current sink
1504 * and enable battery charging comparators.
1505 */
1506 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1507}
1508
1509static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1510{
1511 u32 chg_state;
1512 bool ret = false;
1513
1514 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1515 ret = chg_state & 2;
1516
1517 return ret;
1518}
1519
1520static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1521{
1522 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1523}
1524
1525static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1526{
1527 /* Data contact detection enable, DCDENB */
1528 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1529}
1530
1531static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1532{
1533 u32 chg_ctrl;
1534
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301535 dwc3_msm_write_reg(mdwc->base, QSCRATCH_CTRL_REG,
1536 mdwc->qscratch_ctl_val);
Manu Gautam8c642812012-06-07 10:35:10 +05301537 /* Clear charger detecting control bits */
1538 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1539
1540 /* Clear alt interrupt latch and enable bits */
1541 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1542 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1543
1544 udelay(100);
1545
1546 /* Before proceeding make sure charger block is RESET */
1547 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1548 if (chg_ctrl & 0x3F)
1549 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1550 __func__, chg_ctrl);
1551}
1552
1553static const char *chg_to_string(enum dwc3_chg_type chg_type)
1554{
1555 switch (chg_type) {
Manu Gautama1e331d2013-02-07 14:55:05 +05301556 case DWC3_SDP_CHARGER: return "USB_SDP_CHARGER";
1557 case DWC3_DCP_CHARGER: return "USB_DCP_CHARGER";
1558 case DWC3_CDP_CHARGER: return "USB_CDP_CHARGER";
1559 case DWC3_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Vijayavardhan Vennapusaa04e0c92013-06-04 12:37:10 +05301560 case DWC3_UNSUPPORTED_CHARGER: return "INVALID_CHARGER";
1561 default: return "UNKNOWN_CHARGER";
Manu Gautam8c642812012-06-07 10:35:10 +05301562 }
1563}
1564
1565#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1566#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1567#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1568#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1569
1570static void dwc3_chg_detect_work(struct work_struct *w)
1571{
1572 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1573 bool is_dcd = false, tmout, vout;
1574 unsigned long delay;
1575
1576 dev_dbg(mdwc->dev, "chg detection work\n");
1577 switch (mdwc->chg_state) {
1578 case USB_CHG_STATE_UNDEFINED:
1579 dwc3_chg_block_reset(mdwc);
1580 dwc3_chg_enable_dcd(mdwc);
1581 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1582 mdwc->dcd_retries = 0;
1583 delay = DWC3_CHG_DCD_POLL_TIME;
1584 break;
1585 case USB_CHG_STATE_WAIT_FOR_DCD:
1586 is_dcd = dwc3_chg_check_dcd(mdwc);
1587 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1588 if (is_dcd || tmout) {
1589 dwc3_chg_disable_dcd(mdwc);
Manu Gautama1e331d2013-02-07 14:55:05 +05301590 if (dwc3_chg_det_check_linestate(mdwc)) {
Vijayavardhan Vennapusaa04e0c92013-06-04 12:37:10 +05301591 dwc3_chg_enable_primary_det(mdwc);
1592 usleep_range(1000, 1200);
1593 vout = dwc3_chg_det_check_output(mdwc);
1594 if (!vout)
1595 mdwc->charger.chg_type =
1596 DWC3_UNSUPPORTED_CHARGER;
1597 else
1598 mdwc->charger.chg_type =
Manu Gautama1e331d2013-02-07 14:55:05 +05301599 DWC3_PROPRIETARY_CHARGER;
Vijayavardhan Vennapusaa04e0c92013-06-04 12:37:10 +05301600 dwc3_msm_write_reg(mdwc->base,
1601 CHARGING_DET_CTRL_REG, 0x0);
Manu Gautama1e331d2013-02-07 14:55:05 +05301602 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1603 delay = 0;
1604 break;
1605 }
Manu Gautam8c642812012-06-07 10:35:10 +05301606 dwc3_chg_enable_primary_det(mdwc);
1607 delay = DWC3_CHG_PRIMARY_DET_TIME;
1608 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1609 } else {
1610 delay = DWC3_CHG_DCD_POLL_TIME;
1611 }
1612 break;
1613 case USB_CHG_STATE_DCD_DONE:
1614 vout = dwc3_chg_det_check_output(mdwc);
1615 if (vout) {
1616 dwc3_chg_enable_secondary_det(mdwc);
1617 delay = DWC3_CHG_SECONDARY_DET_TIME;
1618 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1619 } else {
Manu Gautama1e331d2013-02-07 14:55:05 +05301620 mdwc->charger.chg_type = DWC3_SDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301621 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1622 delay = 0;
1623 }
1624 break;
1625 case USB_CHG_STATE_PRIMARY_DONE:
1626 vout = dwc3_chg_det_check_output(mdwc);
1627 if (vout)
Manu Gautama1e331d2013-02-07 14:55:05 +05301628 mdwc->charger.chg_type = DWC3_DCP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301629 else
Manu Gautama1e331d2013-02-07 14:55:05 +05301630 mdwc->charger.chg_type = DWC3_CDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301631 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1632 /* fall through */
1633 case USB_CHG_STATE_SECONDARY_DONE:
1634 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1635 /* fall through */
1636 case USB_CHG_STATE_DETECTED:
1637 dwc3_chg_block_reset(mdwc);
Manu Gautama48296e2012-12-05 17:37:56 +05301638 /* Enable VDP_SRC */
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301639 if (mdwc->charger.chg_type == DWC3_DCP_CHARGER) {
Manu Gautama48296e2012-12-05 17:37:56 +05301640 dwc3_msm_write_readback(mdwc->base,
1641 CHARGING_DET_CTRL_REG, 0x1F, 0x10);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301642 if (mdwc->ext_chg_opened) {
1643 init_completion(&mdwc->ext_chg_wait);
1644 mdwc->ext_chg_active = true;
1645 }
1646 }
Manu Gautam8c642812012-06-07 10:35:10 +05301647 dev_dbg(mdwc->dev, "chg_type = %s\n",
1648 chg_to_string(mdwc->charger.chg_type));
1649 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1650 &mdwc->charger);
1651 return;
1652 default:
1653 return;
1654 }
1655
1656 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1657}
1658
1659static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1660{
1661 struct dwc3_msm *mdwc = context;
1662
1663 if (start == false) {
Jack Pham9354c6a2012-12-20 19:19:32 -08001664 dev_dbg(mdwc->dev, "canceling charging detection work\n");
Manu Gautam8c642812012-06-07 10:35:10 +05301665 cancel_delayed_work_sync(&mdwc->chg_work);
1666 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1667 charger->chg_type = DWC3_INVALID_CHARGER;
1668 return;
1669 }
1670
1671 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1672 charger->chg_type = DWC3_INVALID_CHARGER;
1673 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1674}
1675
Manu Gautamb5067272012-07-02 09:53:41 +05301676static int dwc3_msm_suspend(struct dwc3_msm *mdwc)
1677{
Manu Gautam2617deb2012-08-31 17:50:06 -07001678 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301679 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301680 bool host_bus_suspend;
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301681 bool host_ss_active;
Manu Gautam2617deb2012-08-31 17:50:06 -07001682
Manu Gautamb5067272012-07-02 09:53:41 +05301683 dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__);
1684
1685 if (atomic_read(&mdwc->in_lpm)) {
1686 dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__);
1687 return 0;
1688 }
1689
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301690 host_ss_active = dwc3_msm_read_reg(mdwc->base, USB3_PORTSC) & PORT_PE;
Manu Gautama48296e2012-12-05 17:37:56 +05301691 if (mdwc->hs_phy_irq)
1692 disable_irq(mdwc->hs_phy_irq);
1693
Manu Gautam98013c22012-11-20 17:42:42 +05301694 if (cancel_delayed_work_sync(&mdwc->chg_work))
1695 dev_dbg(mdwc->dev, "%s: chg_work was pending\n", __func__);
1696 if (mdwc->chg_state != USB_CHG_STATE_DETECTED) {
1697 /* charger detection wasn't complete; re-init flags */
1698 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1699 mdwc->charger.chg_type = DWC3_INVALID_CHARGER;
Manu Gautama48296e2012-12-05 17:37:56 +05301700 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG,
1701 0x37, 0x0);
Manu Gautam98013c22012-11-20 17:42:42 +05301702 }
1703
Manu Gautam840f4fe2013-04-16 16:50:30 +05301704 dcp = ((mdwc->charger.chg_type == DWC3_DCP_CHARGER) ||
1705 (mdwc->charger.chg_type == DWC3_PROPRIETARY_CHARGER));
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301706 host_bus_suspend = mdwc->host_mode == 1;
Manu Gautam377821c2012-09-28 16:53:24 +05301707
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301708 if (!dcp && !host_bus_suspend)
1709 dwc3_msm_write_reg(mdwc->base, QSCRATCH_CTRL_REG,
1710 mdwc->qscratch_ctl_val);
1711
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301712 /* Sequence to put SSPHY in low power state:
1713 * 1. Clear REF_SS_PHY_EN in SS_PHY_CTRL_REG
1714 * 2. Clear REF_USE_PAD in SS_PHY_CTRL_REG
1715 * 3. Set TEST_POWERED_DOWN in SS_PHY_CTRL_REG to enable PHY retention
1716 * 4. Disable SSPHY ref clk
1717 */
1718 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8), 0x0);
1719 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28), 0x0);
1720 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26),
1721 (1 << 26));
1722
Manu Gautam377821c2012-09-28 16:53:24 +05301723 usleep_range(1000, 1200);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001724 clk_disable_unprepare(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301725
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301726 if (host_bus_suspend) {
1727 /* Sequence for host bus suspend case:
1728 * 1. Set suspend and sleep bits in GUSB2PHYCONFIG reg
1729 * 2. Clear interrupt latch register and enable BSV, ID HV intr
1730 * 3. Enable DP and DM HV interrupts in ALT_INTERRUPT_EN_REG
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301731 */
1732 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1733 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1734 0x00000140);
1735 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1736 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1737 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1738 0x18000, 0x18000);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301739 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0xFC0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301740 udelay(5);
1741 } else {
1742 /* Sequence to put hardware in low power state:
1743 * 1. Set OTGDISABLE to disable OTG block in HSPHY (saves power)
1744 * 2. Clear charger detection control fields (performed above)
1745 * 3. SUSPEND PHY and turn OFF core clock after some delay
1746 * 4. Clear interrupt latch register and enable BSV, ID HV intr
1747 * 5. Enable PHY retention
1748 */
1749 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x1000,
1750 0x1000);
1751 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1752 0xC00000, 0x800000);
1753 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1754 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1755 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1756 0x18000, 0x18000);
1757 if (!dcp)
1758 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1759 0x2, 0x0);
1760 }
Manu Gautam377821c2012-09-28 16:53:24 +05301761
1762 /* make sure above writes are completed before turning off clocks */
1763 wmb();
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001764
1765 /* remove vote for controller power collapse */
1766 if (!host_bus_suspend)
1767 dwc3_msm_config_gdsc(mdwc, 0);
1768
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301769 if (!host_bus_suspend || !host_ss_active) {
1770 clk_disable_unprepare(mdwc->core_clk);
1771 mdwc->lpm_flags |= MDWC3_CORECLK_OFF;
1772 }
Manu Gautam377821c2012-09-28 16:53:24 +05301773 clk_disable_unprepare(mdwc->iface_clk);
1774
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301775 if (!host_bus_suspend)
Jack Pham22698b82013-02-13 17:45:06 -08001776 clk_disable_unprepare(mdwc->utmi_clk);
1777
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301778 if (!host_bus_suspend) {
Jack Pham22698b82013-02-13 17:45:06 -08001779 /* USB PHY no more requires TCXO */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301780 clk_disable_unprepare(mdwc->xo_clk);
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301781 mdwc->lpm_flags |= MDWC3_TCXO_SHUTDOWN;
Jack Pham22698b82013-02-13 17:45:06 -08001782 }
Manu Gautamb5067272012-07-02 09:53:41 +05301783
Manu Gautam2617deb2012-08-31 17:50:06 -07001784 if (mdwc->bus_perf_client) {
1785 ret = msm_bus_scale_client_update_request(
1786 mdwc->bus_perf_client, 0);
1787 if (ret)
1788 dev_err(mdwc->dev, "Failed to reset bus bw vote\n");
1789 }
1790
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301791 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1792 !host_bus_suspend)
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301793 dwc3_hsusb_ldo_enable(0);
1794
Vijayavardhan Vennapusa6bc06962012-10-31 13:23:38 +05301795 dwc3_ssusb_ldo_enable(0);
1796 dwc3_ssusb_config_vddcx(0);
Manu Gautam840f4fe2013-04-16 16:50:30 +05301797 if (!host_bus_suspend && !dcp)
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301798 dwc3_hsusb_config_vddcx(0);
Manu Gautam377821c2012-09-28 16:53:24 +05301799 wake_unlock(&mdwc->wlock);
Manu Gautamb5067272012-07-02 09:53:41 +05301800 atomic_set(&mdwc->in_lpm, 1);
Manu Gautam377821c2012-09-28 16:53:24 +05301801
Manu Gautamb5067272012-07-02 09:53:41 +05301802 dev_info(mdwc->dev, "DWC3 in low power mode\n");
1803
Manu Gautam840f4fe2013-04-16 16:50:30 +05301804 if (mdwc->hs_phy_irq) {
Manu Gautama48296e2012-12-05 17:37:56 +05301805 enable_irq(mdwc->hs_phy_irq);
Manu Gautam840f4fe2013-04-16 16:50:30 +05301806 /* with DCP we dont require wakeup using HS_PHY_IRQ */
1807 if (dcp)
1808 disable_irq_wake(mdwc->hs_phy_irq);
1809 }
Manu Gautama48296e2012-12-05 17:37:56 +05301810
Manu Gautamb5067272012-07-02 09:53:41 +05301811 return 0;
1812}
1813
1814static int dwc3_msm_resume(struct dwc3_msm *mdwc)
1815{
Manu Gautam2617deb2012-08-31 17:50:06 -07001816 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301817 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301818 bool host_bus_suspend;
Manu Gautam2617deb2012-08-31 17:50:06 -07001819
Manu Gautamb5067272012-07-02 09:53:41 +05301820 dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__);
1821
1822 if (!atomic_read(&mdwc->in_lpm)) {
1823 dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__);
1824 return 0;
1825 }
1826
Manu Gautam377821c2012-09-28 16:53:24 +05301827 wake_lock(&mdwc->wlock);
1828
Manu Gautam2617deb2012-08-31 17:50:06 -07001829 if (mdwc->bus_perf_client) {
1830 ret = msm_bus_scale_client_update_request(
1831 mdwc->bus_perf_client, 1);
1832 if (ret)
1833 dev_err(mdwc->dev, "Failed to vote for bus scaling\n");
1834 }
1835
Manu Gautam840f4fe2013-04-16 16:50:30 +05301836 dcp = ((mdwc->charger.chg_type == DWC3_DCP_CHARGER) ||
1837 (mdwc->charger.chg_type == DWC3_PROPRIETARY_CHARGER));
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301838 host_bus_suspend = mdwc->host_mode == 1;
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301839
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301840 if (mdwc->lpm_flags & MDWC3_TCXO_SHUTDOWN) {
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301841 /* Vote for TCXO while waking up USB HSPHY */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301842 ret = clk_prepare_enable(mdwc->xo_clk);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301843 if (ret)
1844 dev_err(mdwc->dev, "%s failed to vote TCXO buffer%d\n",
1845 __func__, ret);
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301846 mdwc->lpm_flags &= ~MDWC3_TCXO_SHUTDOWN;
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301847 }
1848
Hemant Kumar086bf6b2013-06-10 19:29:27 -07001849 /* add vote for controller power collapse */
1850 if (!host_bus_suspend)
1851 dwc3_msm_config_gdsc(mdwc, 1);
1852
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301853 if (!host_bus_suspend)
1854 clk_prepare_enable(mdwc->utmi_clk);
1855
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301856 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1857 !host_bus_suspend)
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301858 dwc3_hsusb_ldo_enable(1);
1859
Vijayavardhan Vennapusa6bc06962012-10-31 13:23:38 +05301860 dwc3_ssusb_ldo_enable(1);
1861 dwc3_ssusb_config_vddcx(1);
Jack Pham22698b82013-02-13 17:45:06 -08001862
Manu Gautam840f4fe2013-04-16 16:50:30 +05301863 if (!host_bus_suspend && !dcp)
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301864 dwc3_hsusb_config_vddcx(1);
Jack Pham22698b82013-02-13 17:45:06 -08001865
Manu Gautam3e9ad352012-08-16 14:44:47 -07001866 clk_prepare_enable(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301867 usleep_range(1000, 1200);
1868
Manu Gautam3e9ad352012-08-16 14:44:47 -07001869 clk_prepare_enable(mdwc->iface_clk);
Vijayavardhan Vennapusaa00a5062013-04-19 12:31:07 +05301870 if (mdwc->lpm_flags & MDWC3_CORECLK_OFF) {
1871 clk_prepare_enable(mdwc->core_clk);
1872 mdwc->lpm_flags &= ~MDWC3_CORECLK_OFF;
1873 }
Manu Gautam377821c2012-09-28 16:53:24 +05301874
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301875 if (host_bus_suspend) {
1876 /* Disable HV interrupt */
1877 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1878 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1879 0x18000, 0x0);
1880 /* Clear interrupt latch register */
1881 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301882
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301883 /* Disable DP and DM HV interrupt */
1884 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301885
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301886 /* Clear suspend bit in GUSB2PHYCONFIG register */
1887 dwc3_msm_write_readback(mdwc->base, DWC3_GUSB2PHYCFG(0),
1888 0x40, 0x0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301889 } else {
1890 /* Disable HV interrupt */
1891 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1892 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1893 0x18000, 0x0);
1894 /* Disable Retention */
1895 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x2, 0x2);
1896
1897 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1898 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1899 0xF0000000);
1900 /* 10usec delay required before de-asserting PHY RESET */
1901 udelay(10);
1902 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1903 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) &
1904 0x7FFFFFFF);
1905
1906 /* Bring PHY out of suspend */
1907 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0xC00000,
1908 0x0);
1909
1910 }
Manu Gautamb5067272012-07-02 09:53:41 +05301911
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301912 /* Assert SS PHY RESET */
1913 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7),
1914 (1 << 7));
1915 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28),
1916 (1 << 28));
1917 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8),
1918 (1 << 8));
1919 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26), 0x0);
1920 /* 10usec delay required before de-asserting SS PHY RESET */
1921 udelay(10);
1922 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7), 0x0);
1923
Vijayavardhan Vennapusa5b286322013-04-12 12:15:00 +05301924 /*
1925 * Reinitilize SSPHY parameters as SS_PHY RESET will reset
1926 * the internal registers to default values.
1927 */
1928 dwc3_msm_ss_phy_reg_init(mdwc);
Manu Gautamb5067272012-07-02 09:53:41 +05301929 atomic_set(&mdwc->in_lpm, 0);
Manu Gautam377821c2012-09-28 16:53:24 +05301930
1931 /* match disable_irq call from isr */
1932 if (mdwc->lpm_irq_seen && mdwc->hs_phy_irq) {
1933 enable_irq(mdwc->hs_phy_irq);
1934 mdwc->lpm_irq_seen = false;
1935 }
Manu Gautam840f4fe2013-04-16 16:50:30 +05301936 /* it must DCP disconnect, re-enable HS_PHY wakeup IRQ */
1937 if (mdwc->hs_phy_irq && dcp)
1938 enable_irq_wake(mdwc->hs_phy_irq);
Manu Gautam377821c2012-09-28 16:53:24 +05301939
Manu Gautamb5067272012-07-02 09:53:41 +05301940 dev_info(mdwc->dev, "DWC3 exited from low power mode\n");
1941
1942 return 0;
1943}
1944
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301945static void dwc3_wait_for_ext_chg_done(struct dwc3_msm *mdwc)
1946{
1947 unsigned long t;
1948
1949 /*
1950 * Defer next cable connect event till external charger
1951 * detection is completed.
1952 */
1953
1954 if (mdwc->ext_chg_active && (mdwc->ext_xceiv.bsv ||
1955 !mdwc->ext_xceiv.id)) {
1956
1957 dev_dbg(mdwc->dev, "before ext chg wait\n");
1958
1959 t = wait_for_completion_timeout(&mdwc->ext_chg_wait,
1960 msecs_to_jiffies(3000));
1961 if (!t)
1962 dev_err(mdwc->dev, "ext chg wait timeout\n");
1963 else
1964 dev_dbg(mdwc->dev, "ext chg wait done\n");
1965 }
1966
1967}
1968
Manu Gautamb5067272012-07-02 09:53:41 +05301969static void dwc3_resume_work(struct work_struct *w)
1970{
1971 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1972 resume_work.work);
1973
1974 dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__);
1975 /* handle any event that was queued while work was already running */
1976 if (!atomic_read(&mdwc->in_lpm)) {
1977 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301978 if (mdwc->otg_xceiv) {
1979 dwc3_wait_for_ext_chg_done(mdwc);
Manu Gautamb5067272012-07-02 09:53:41 +05301980 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1981 DWC3_EVENT_XCEIV_STATE);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301982 }
Manu Gautamb5067272012-07-02 09:53:41 +05301983 return;
1984 }
1985
1986 /* bail out if system resume in process, else initiate RESUME */
1987 if (atomic_read(&mdwc->pm_suspended)) {
1988 mdwc->resume_pending = true;
1989 } else {
1990 pm_runtime_get_sync(mdwc->dev);
1991 if (mdwc->otg_xceiv)
1992 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1993 DWC3_EVENT_PHY_RESUME);
Manu Gautambb825d72013-03-12 16:25:42 +05301994 pm_runtime_put_noidle(mdwc->dev);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301995 if (mdwc->otg_xceiv && (mdwc->ext_xceiv.otg_capability)) {
1996 dwc3_wait_for_ext_chg_done(mdwc);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301997 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1998 DWC3_EVENT_XCEIV_STATE);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05301999 }
Manu Gautamb5067272012-07-02 09:53:41 +05302000 }
2001}
2002
Jack Pham0fc12332012-11-19 13:14:22 -08002003static u32 debug_id = true, debug_bsv, debug_connect;
Manu Gautamb5067272012-07-02 09:53:41 +05302004
2005static int dwc3_connect_show(struct seq_file *s, void *unused)
2006{
2007 if (debug_connect)
2008 seq_printf(s, "true\n");
2009 else
2010 seq_printf(s, "false\n");
2011
2012 return 0;
2013}
2014
2015static int dwc3_connect_open(struct inode *inode, struct file *file)
2016{
2017 return single_open(file, dwc3_connect_show, inode->i_private);
2018}
2019
2020static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf,
2021 size_t count, loff_t *ppos)
2022{
2023 struct seq_file *s = file->private_data;
2024 struct dwc3_msm *mdwc = s->private;
2025 char buf[8];
2026
2027 memset(buf, 0x00, sizeof(buf));
2028
2029 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2030 return -EFAULT;
2031
2032 if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) {
2033 debug_connect = true;
2034 } else {
2035 debug_connect = debug_bsv = false;
2036 debug_id = true;
2037 }
2038
2039 mdwc->ext_xceiv.bsv = debug_bsv;
2040 mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND;
2041
2042 if (atomic_read(&mdwc->in_lpm)) {
2043 dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__);
2044 dwc3_resume_work(&mdwc->resume_work.work);
2045 } else {
2046 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
2047 if (mdwc->otg_xceiv)
2048 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
2049 DWC3_EVENT_XCEIV_STATE);
2050 }
2051
2052 return count;
2053}
2054
2055const struct file_operations dwc3_connect_fops = {
2056 .open = dwc3_connect_open,
2057 .read = seq_read,
2058 .write = dwc3_connect_write,
2059 .llseek = seq_lseek,
2060 .release = single_release,
2061};
2062
2063static struct dentry *dwc3_debugfs_root;
2064
2065static void dwc3_debugfs_init(struct dwc3_msm *mdwc)
2066{
2067 dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL);
2068
2069 if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root))
2070 return;
2071
2072 if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302073 &debug_id))
Manu Gautamb5067272012-07-02 09:53:41 +05302074 goto error;
2075
2076 if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302077 &debug_bsv))
Manu Gautamb5067272012-07-02 09:53:41 +05302078 goto error;
2079
2080 if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR,
2081 dwc3_debugfs_root, mdwc, &dwc3_connect_fops))
2082 goto error;
2083
2084 return;
2085
2086error:
2087 debugfs_remove_recursive(dwc3_debugfs_root);
2088}
Manu Gautam8c642812012-06-07 10:35:10 +05302089
Manu Gautam377821c2012-09-28 16:53:24 +05302090static irqreturn_t msm_dwc3_irq(int irq, void *data)
2091{
2092 struct dwc3_msm *mdwc = data;
2093
2094 if (atomic_read(&mdwc->in_lpm)) {
2095 dev_dbg(mdwc->dev, "%s received in LPM\n", __func__);
2096 mdwc->lpm_irq_seen = true;
2097 disable_irq_nosync(irq);
2098 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
2099 } else {
2100 pr_info_ratelimited("%s: IRQ outside LPM\n", __func__);
2101 }
2102
2103 return IRQ_HANDLED;
2104}
2105
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302106static int dwc3_msm_power_get_property_usb(struct power_supply *psy,
2107 enum power_supply_property psp,
2108 union power_supply_propval *val)
2109{
2110 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
2111 usb_psy);
2112 switch (psp) {
2113 case POWER_SUPPLY_PROP_SCOPE:
2114 val->intval = mdwc->host_mode;
2115 break;
2116 case POWER_SUPPLY_PROP_CURRENT_MAX:
2117 val->intval = mdwc->current_max;
2118 break;
2119 case POWER_SUPPLY_PROP_PRESENT:
2120 val->intval = mdwc->vbus_active;
2121 break;
2122 case POWER_SUPPLY_PROP_ONLINE:
2123 val->intval = mdwc->online;
2124 break;
Manu Gautamfa40cae2013-03-01 16:37:12 +05302125 case POWER_SUPPLY_PROP_TYPE:
2126 val->intval = psy->type;
2127 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302128 default:
2129 return -EINVAL;
2130 }
2131 return 0;
2132}
2133
2134static int dwc3_msm_power_set_property_usb(struct power_supply *psy,
2135 enum power_supply_property psp,
2136 const union power_supply_propval *val)
2137{
2138 static bool init;
2139 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
2140 usb_psy);
2141
2142 switch (psp) {
2143 case POWER_SUPPLY_PROP_SCOPE:
2144 mdwc->host_mode = val->intval;
2145 break;
2146 /* Process PMIC notification in PRESENT prop */
2147 case POWER_SUPPLY_PROP_PRESENT:
2148 dev_dbg(mdwc->dev, "%s: notify xceiv event\n", __func__);
Jack Pham9354c6a2012-12-20 19:19:32 -08002149 if (mdwc->otg_xceiv && !mdwc->ext_inuse &&
2150 (mdwc->ext_xceiv.otg_capability || !init)) {
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302151 mdwc->ext_xceiv.bsv = val->intval;
Manu Gautamf71d9cb2013-02-07 13:52:12 +05302152 queue_delayed_work(system_nrt_wq,
Jack Pham4d91aab2013-03-08 10:02:16 -08002153 &mdwc->resume_work, 20);
Jack Pham9354c6a2012-12-20 19:19:32 -08002154
2155 if (!init)
2156 init = true;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302157 }
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302158 mdwc->vbus_active = val->intval;
2159 break;
2160 case POWER_SUPPLY_PROP_ONLINE:
2161 mdwc->online = val->intval;
2162 break;
2163 case POWER_SUPPLY_PROP_CURRENT_MAX:
2164 mdwc->current_max = val->intval;
2165 break;
Manu Gautamfa40cae2013-03-01 16:37:12 +05302166 case POWER_SUPPLY_PROP_TYPE:
2167 psy->type = val->intval;
2168 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302169 default:
2170 return -EINVAL;
2171 }
2172
2173 power_supply_changed(&mdwc->usb_psy);
2174 return 0;
2175}
2176
Jack Pham9354c6a2012-12-20 19:19:32 -08002177static void dwc3_msm_external_power_changed(struct power_supply *psy)
2178{
2179 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm, usb_psy);
2180 union power_supply_propval ret = {0,};
2181
2182 if (!mdwc->ext_vbus_psy)
2183 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2184
2185 if (!mdwc->ext_vbus_psy) {
2186 pr_err("%s: Unable to get ext_vbus power_supply\n", __func__);
2187 return;
2188 }
2189
2190 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2191 POWER_SUPPLY_PROP_ONLINE, &ret);
2192 if (ret.intval) {
2193 dwc3_start_chg_det(&mdwc->charger, false);
2194 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2195 POWER_SUPPLY_PROP_CURRENT_MAX, &ret);
2196 power_supply_set_current_limit(&mdwc->usb_psy, ret.intval);
2197 }
2198
2199 power_supply_set_online(&mdwc->usb_psy, ret.intval);
2200 power_supply_changed(&mdwc->usb_psy);
2201}
2202
2203
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302204static char *dwc3_msm_pm_power_supplied_to[] = {
2205 "battery",
2206};
2207
2208static enum power_supply_property dwc3_msm_pm_power_props_usb[] = {
2209 POWER_SUPPLY_PROP_PRESENT,
2210 POWER_SUPPLY_PROP_ONLINE,
2211 POWER_SUPPLY_PROP_CURRENT_MAX,
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302212 POWER_SUPPLY_PROP_TYPE,
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302213 POWER_SUPPLY_PROP_SCOPE,
2214};
2215
Jack Phamfadd6432012-12-07 19:03:41 -08002216static void dwc3_init_adc_work(struct work_struct *w);
2217
2218static void dwc3_ext_notify_online(int on)
2219{
2220 struct dwc3_msm *mdwc = context;
Jack Phamf12b7e12012-12-28 14:27:26 -08002221 bool notify_otg = false;
Jack Phamfadd6432012-12-07 19:03:41 -08002222
2223 if (!mdwc) {
2224 pr_err("%s: DWC3 driver already removed\n", __func__);
2225 return;
2226 }
2227
2228 dev_dbg(mdwc->dev, "notify %s%s\n", on ? "" : "dis", "connected");
2229
Jack Pham9354c6a2012-12-20 19:19:32 -08002230 if (!mdwc->ext_vbus_psy)
2231 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2232
2233 mdwc->ext_inuse = on;
Jack Phamf12b7e12012-12-28 14:27:26 -08002234 if (on) {
2235 /* force OTG to exit B-peripheral state */
2236 mdwc->ext_xceiv.bsv = false;
2237 notify_otg = true;
Jack Pham9354c6a2012-12-20 19:19:32 -08002238 dwc3_start_chg_det(&mdwc->charger, false);
Jack Phamf12b7e12012-12-28 14:27:26 -08002239 } else {
2240 /* external client offline; tell OTG about cached ID/BSV */
2241 if (mdwc->ext_xceiv.id != mdwc->id_state) {
2242 mdwc->ext_xceiv.id = mdwc->id_state;
2243 notify_otg = true;
2244 }
2245
2246 mdwc->ext_xceiv.bsv = mdwc->vbus_active;
2247 notify_otg |= mdwc->vbus_active;
2248 }
Jack Pham9354c6a2012-12-20 19:19:32 -08002249
2250 if (mdwc->ext_vbus_psy)
2251 power_supply_set_present(mdwc->ext_vbus_psy, on);
Jack Phamf12b7e12012-12-28 14:27:26 -08002252
2253 if (notify_otg)
2254 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
Jack Phamfadd6432012-12-07 19:03:41 -08002255}
2256
Jack Pham0cca9412013-03-08 13:22:42 -08002257static void dwc3_id_work(struct work_struct *w)
Jack Phamfadd6432012-12-07 19:03:41 -08002258{
Jack Pham0cca9412013-03-08 13:22:42 -08002259 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, id_work);
Jack Pham5c585062013-03-25 18:39:12 -07002260 int ret;
Jack Phamfadd6432012-12-07 19:03:41 -08002261
Jack Pham0cca9412013-03-08 13:22:42 -08002262 /* Give external client a chance to handle */
Jack Pham5c585062013-03-25 18:39:12 -07002263 if (!mdwc->ext_inuse && usb_ext) {
2264 if (mdwc->pmic_id_irq)
2265 disable_irq(mdwc->pmic_id_irq);
2266
2267 ret = usb_ext->notify(usb_ext->ctxt, mdwc->id_state,
2268 dwc3_ext_notify_online);
2269 dev_dbg(mdwc->dev, "%s: external handler returned %d\n",
2270 __func__, ret);
2271
2272 if (mdwc->pmic_id_irq) {
2273 /* ID may have changed while IRQ disabled; update it */
2274 mdwc->id_state = !!irq_read_line(mdwc->pmic_id_irq);
2275 enable_irq(mdwc->pmic_id_irq);
Jack Pham0cca9412013-03-08 13:22:42 -08002276 }
Jack Pham5c585062013-03-25 18:39:12 -07002277
2278 mdwc->ext_inuse = (ret == 0);
Jack Pham0cca9412013-03-08 13:22:42 -08002279 }
Jack Phamfadd6432012-12-07 19:03:41 -08002280
Jack Pham0cca9412013-03-08 13:22:42 -08002281 if (!mdwc->ext_inuse) { /* notify OTG */
2282 mdwc->ext_xceiv.id = mdwc->id_state;
2283 dwc3_resume_work(&mdwc->resume_work.work);
2284 }
2285}
2286
2287static irqreturn_t dwc3_pmic_id_irq(int irq, void *data)
2288{
2289 struct dwc3_msm *mdwc = data;
Jack Pham5c585062013-03-25 18:39:12 -07002290 enum dwc3_id_state id;
Jack Pham0cca9412013-03-08 13:22:42 -08002291
2292 /* If we can't read ID line state for some reason, treat it as float */
Jack Pham5c585062013-03-25 18:39:12 -07002293 id = !!irq_read_line(irq);
2294 if (mdwc->id_state != id) {
2295 mdwc->id_state = id;
2296 queue_work(system_nrt_wq, &mdwc->id_work);
2297 }
Jack Pham0cca9412013-03-08 13:22:42 -08002298
2299 return IRQ_HANDLED;
Jack Phamfadd6432012-12-07 19:03:41 -08002300}
2301
Jack Pham0fc12332012-11-19 13:14:22 -08002302static void dwc3_adc_notification(enum qpnp_tm_state state, void *ctx)
2303{
2304 struct dwc3_msm *mdwc = ctx;
2305
2306 if (state >= ADC_TM_STATE_NUM) {
2307 pr_err("%s: invalid notification %d\n", __func__, state);
2308 return;
2309 }
2310
2311 dev_dbg(mdwc->dev, "%s: state = %s\n", __func__,
2312 state == ADC_TM_HIGH_STATE ? "high" : "low");
2313
Jack Phamf12b7e12012-12-28 14:27:26 -08002314 /* save ID state, but don't necessarily notify OTG */
Jack Pham0fc12332012-11-19 13:14:22 -08002315 if (state == ADC_TM_HIGH_STATE) {
Jack Phamf12b7e12012-12-28 14:27:26 -08002316 mdwc->id_state = DWC3_ID_FLOAT;
Jack Pham0fc12332012-11-19 13:14:22 -08002317 mdwc->adc_param.state_request = ADC_TM_LOW_THR_ENABLE;
2318 } else {
Jack Phamf12b7e12012-12-28 14:27:26 -08002319 mdwc->id_state = DWC3_ID_GROUND;
Jack Pham0fc12332012-11-19 13:14:22 -08002320 mdwc->adc_param.state_request = ADC_TM_HIGH_THR_ENABLE;
2321 }
2322
Jack Pham0cca9412013-03-08 13:22:42 -08002323 dwc3_id_work(&mdwc->id_work);
2324
Jack Phamfadd6432012-12-07 19:03:41 -08002325 /* re-arm ADC interrupt */
Jack Pham0fc12332012-11-19 13:14:22 -08002326 qpnp_adc_tm_usbid_configure(&mdwc->adc_param);
2327}
2328
2329static void dwc3_init_adc_work(struct work_struct *w)
2330{
2331 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
2332 init_adc_work.work);
2333 int ret;
2334
2335 ret = qpnp_adc_tm_is_ready();
2336 if (ret == -EPROBE_DEFER) {
Jack Pham90b4d122012-12-13 11:46:22 -08002337 queue_delayed_work(system_nrt_wq, to_delayed_work(w),
2338 msecs_to_jiffies(100));
Jack Pham0fc12332012-11-19 13:14:22 -08002339 return;
2340 }
2341
2342 mdwc->adc_param.low_thr = adc_low_threshold;
2343 mdwc->adc_param.high_thr = adc_high_threshold;
2344 mdwc->adc_param.timer_interval = adc_meas_interval;
2345 mdwc->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadossa3e35512013-02-22 17:06:07 -08002346 mdwc->adc_param.btm_ctx = mdwc;
Jack Pham0fc12332012-11-19 13:14:22 -08002347 mdwc->adc_param.threshold_notification = dwc3_adc_notification;
2348
2349 ret = qpnp_adc_tm_usbid_configure(&mdwc->adc_param);
2350 if (ret) {
2351 dev_err(mdwc->dev, "%s: request ADC error %d\n", __func__, ret);
2352 return;
2353 }
2354
2355 mdwc->id_adc_detect = true;
2356}
2357
2358static ssize_t adc_enable_show(struct device *dev,
2359 struct device_attribute *attr, char *buf)
2360{
2361 return snprintf(buf, PAGE_SIZE, "%s\n", context->id_adc_detect ?
2362 "enabled" : "disabled");
2363}
2364
2365static ssize_t adc_enable_store(struct device *dev,
2366 struct device_attribute *attr, const char
2367 *buf, size_t size)
2368{
2369 if (!strnicmp(buf, "enable", 6)) {
2370 if (!context->id_adc_detect)
2371 dwc3_init_adc_work(&context->init_adc_work.work);
2372 return size;
2373 } else if (!strnicmp(buf, "disable", 7)) {
2374 qpnp_adc_tm_usbid_end();
2375 context->id_adc_detect = false;
2376 return size;
2377 }
2378
2379 return -EINVAL;
2380}
2381
2382static DEVICE_ATTR(adc_enable, S_IRUGO | S_IWUSR, adc_enable_show,
2383 adc_enable_store);
2384
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302385static int dwc3_msm_ext_chg_open(struct inode *inode, struct file *file)
2386{
2387 struct dwc3_msm *mdwc = context;
2388
2389 pr_debug("dwc3-msm ext chg open\n");
2390
2391 mdwc->ext_chg_opened = true;
2392 return 0;
2393}
2394
2395static ssize_t
2396dwc3_msm_ext_chg_write(struct file *file, const char __user *ubuf,
2397 size_t size, loff_t *pos)
2398{
2399 struct dwc3_msm *mdwc = context;
2400 char kbuf[16];
2401
2402 memset(kbuf, 0x00, sizeof(kbuf));
2403 if (copy_from_user(&kbuf, ubuf, min_t(size_t, sizeof(kbuf) - 1, size)))
2404 return -EFAULT;
2405
2406 pr_debug("%s: buf = %s\n", __func__, kbuf);
2407
2408 if (!strncmp(kbuf, "enable", 6)) {
2409 pr_info("%s: on\n", __func__);
2410 if (mdwc->charger.chg_type == DWC3_DCP_CHARGER) {
2411 pm_runtime_get_sync(mdwc->dev);
2412 } else {
2413 mdwc->ext_chg_active = false;
2414 complete(&mdwc->ext_chg_wait);
2415 return -ENODEV;
2416 }
2417 } else if (!strncmp(kbuf, "disable", 7)) {
2418 pr_info("%s: off\n", __func__);
2419 mdwc->ext_chg_active = false;
2420 complete(&mdwc->ext_chg_wait);
2421 pm_runtime_put(mdwc->dev);
2422 } else {
2423 return -EINVAL;
2424 }
2425
2426 return size;
2427}
2428
2429static int dwc3_msm_ext_chg_mmap(struct file *file, struct vm_area_struct *vma)
2430{
2431 unsigned long vsize = vma->vm_end - vma->vm_start;
2432 int ret;
2433
2434 pr_debug("%s: size = %lu %x\n", __func__, vsize, (int) vma->vm_pgoff);
2435
2436 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2437
2438 ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
2439 vsize, vma->vm_page_prot);
2440 if (ret < 0)
2441 pr_err("%s: failed with return val %d\n", __func__, ret);
2442
2443 return ret;
2444}
2445
2446static int dwc3_msm_ext_chg_release(struct inode *inode, struct file *file)
2447{
2448 struct dwc3_msm *mdwc = context;
2449
2450 pr_debug("dwc3-msm ext chg release\n");
2451
2452 mdwc->ext_chg_opened = false;
2453
2454 return 0;
2455}
2456
2457static const struct file_operations dwc3_msm_ext_chg_fops = {
2458 .owner = THIS_MODULE,
2459 .open = dwc3_msm_ext_chg_open,
2460 .write = dwc3_msm_ext_chg_write,
2461 .mmap = dwc3_msm_ext_chg_mmap,
2462 .release = dwc3_msm_ext_chg_release,
2463};
2464
2465static int dwc3_msm_setup_cdev(struct dwc3_msm *mdwc)
2466{
2467 int ret;
2468
2469 ret = alloc_chrdev_region(&mdwc->ext_chg_dev, 0, 1, "usb_ext_chg");
2470 if (ret < 0) {
2471 pr_err("Fail to allocate usb ext char dev region\n");
2472 return ret;
2473 }
2474 mdwc->ext_chg_class = class_create(THIS_MODULE, "dwc_ext_chg");
2475 if (ret < 0) {
2476 pr_err("Fail to create usb ext chg class\n");
2477 goto unreg_chrdev;
2478 }
2479 cdev_init(&mdwc->ext_chg_cdev, &dwc3_msm_ext_chg_fops);
2480 mdwc->ext_chg_cdev.owner = THIS_MODULE;
2481
2482 ret = cdev_add(&mdwc->ext_chg_cdev, mdwc->ext_chg_dev, 1);
2483 if (ret < 0) {
2484 pr_err("Fail to add usb ext chg cdev\n");
2485 goto destroy_class;
2486 }
2487 mdwc->ext_chg_device = device_create(mdwc->ext_chg_class,
2488 NULL, mdwc->ext_chg_dev, NULL,
2489 "usb_ext_chg");
2490 if (IS_ERR(mdwc->ext_chg_device)) {
2491 pr_err("Fail to create usb ext chg device\n");
2492 ret = PTR_ERR(mdwc->ext_chg_device);
2493 mdwc->ext_chg_device = NULL;
2494 goto del_cdev;
2495 }
2496
2497 pr_debug("dwc3 msm ext chg cdev setup success\n");
2498 return 0;
2499
2500del_cdev:
2501 cdev_del(&mdwc->ext_chg_cdev);
2502destroy_class:
2503 class_destroy(mdwc->ext_chg_class);
2504unreg_chrdev:
2505 unregister_chrdev_region(mdwc->ext_chg_dev, 1);
2506
2507 return ret;
2508}
2509
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002510static int __devinit dwc3_msm_probe(struct platform_device *pdev)
2511{
2512 struct device_node *node = pdev->dev.of_node;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002513 struct dwc3_msm *msm;
2514 struct resource *res;
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002515 void __iomem *tcsr;
Manu Gautamf08f7b62013-04-02 16:09:42 +05302516 unsigned long flags;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002517 int ret = 0;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302518 int len = 0;
2519 u32 tmp[3];
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002520
2521 msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL);
2522 if (!msm) {
2523 dev_err(&pdev->dev, "not enough memory\n");
2524 return -ENOMEM;
2525 }
2526
2527 platform_set_drvdata(pdev, msm);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002528 context = msm;
Manu Gautam60e01352012-05-29 09:00:34 +05302529 msm->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002530
2531 INIT_LIST_HEAD(&msm->req_complete_list);
Manu Gautam8c642812012-06-07 10:35:10 +05302532 INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work);
Manu Gautamb5067272012-07-02 09:53:41 +05302533 INIT_DELAYED_WORK(&msm->resume_work, dwc3_resume_work);
Manu Gautam6eb13e32013-02-01 15:19:15 +05302534 INIT_WORK(&msm->restart_usb_work, dwc3_restart_usb_work);
Jack Pham0cca9412013-03-08 13:22:42 -08002535 INIT_WORK(&msm->id_work, dwc3_id_work);
Jack Pham0fc12332012-11-19 13:14:22 -08002536 INIT_DELAYED_WORK(&msm->init_adc_work, dwc3_init_adc_work);
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302537 init_completion(&msm->ext_chg_wait);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002538
Hemant Kumar086bf6b2013-06-10 19:29:27 -07002539 ret = dwc3_msm_config_gdsc(msm, 1);
2540 if (ret) {
2541 dev_err(&pdev->dev, "unable to configure usb3 gdsc\n");
2542 return ret;
2543 }
2544
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302545 msm->xo_clk = clk_get(&pdev->dev, "xo");
2546 if (IS_ERR(msm->xo_clk)) {
Manu Gautam377821c2012-09-28 16:53:24 +05302547 dev_err(&pdev->dev, "%s unable to get TCXO buffer handle\n",
2548 __func__);
Hemant Kumar086bf6b2013-06-10 19:29:27 -07002549 ret = PTR_ERR(msm->xo_clk);
2550 goto disable_dwc3_gdsc;
Manu Gautam377821c2012-09-28 16:53:24 +05302551 }
2552
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302553 ret = clk_prepare_enable(msm->xo_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05302554 if (ret) {
2555 dev_err(&pdev->dev, "%s failed to vote for TCXO buffer%d\n",
2556 __func__, ret);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302557 goto put_xo;
Manu Gautam377821c2012-09-28 16:53:24 +05302558 }
2559
Manu Gautam1742db22012-06-19 13:33:24 +05302560 /*
2561 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
2562 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
2563 */
2564 msm->core_clk = devm_clk_get(&pdev->dev, "core_clk");
2565 if (IS_ERR(msm->core_clk)) {
2566 dev_err(&pdev->dev, "failed to get core_clk\n");
Manu Gautam377821c2012-09-28 16:53:24 +05302567 ret = PTR_ERR(msm->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302568 goto disable_xo;
Manu Gautam1742db22012-06-19 13:33:24 +05302569 }
2570 clk_set_rate(msm->core_clk, 125000000);
2571 clk_prepare_enable(msm->core_clk);
2572
Manu Gautam3e9ad352012-08-16 14:44:47 -07002573 msm->iface_clk = devm_clk_get(&pdev->dev, "iface_clk");
2574 if (IS_ERR(msm->iface_clk)) {
2575 dev_err(&pdev->dev, "failed to get iface_clk\n");
2576 ret = PTR_ERR(msm->iface_clk);
2577 goto disable_core_clk;
2578 }
2579 clk_prepare_enable(msm->iface_clk);
2580
2581 msm->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2582 if (IS_ERR(msm->sleep_clk)) {
2583 dev_err(&pdev->dev, "failed to get sleep_clk\n");
2584 ret = PTR_ERR(msm->sleep_clk);
2585 goto disable_iface_clk;
2586 }
2587 clk_prepare_enable(msm->sleep_clk);
2588
2589 msm->hsphy_sleep_clk = devm_clk_get(&pdev->dev, "sleep_a_clk");
2590 if (IS_ERR(msm->hsphy_sleep_clk)) {
2591 dev_err(&pdev->dev, "failed to get sleep_a_clk\n");
2592 ret = PTR_ERR(msm->hsphy_sleep_clk);
2593 goto disable_sleep_clk;
2594 }
2595 clk_prepare_enable(msm->hsphy_sleep_clk);
2596
Jack Pham22698b82013-02-13 17:45:06 -08002597 msm->utmi_clk = devm_clk_get(&pdev->dev, "utmi_clk");
2598 if (IS_ERR(msm->utmi_clk)) {
2599 dev_err(&pdev->dev, "failed to get utmi_clk\n");
2600 ret = PTR_ERR(msm->utmi_clk);
2601 goto disable_sleep_a_clk;
2602 }
2603 clk_prepare_enable(msm->utmi_clk);
2604
Manu Gautam3e9ad352012-08-16 14:44:47 -07002605 msm->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
2606 if (IS_ERR(msm->ref_clk)) {
2607 dev_err(&pdev->dev, "failed to get ref_clk\n");
2608 ret = PTR_ERR(msm->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002609 goto disable_utmi_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -07002610 }
2611 clk_prepare_enable(msm->ref_clk);
2612
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302613 of_get_property(node, "qcom,vdd-voltage-level", &len);
2614 if (len == sizeof(tmp)) {
2615 of_property_read_u32_array(node, "qcom,vdd-voltage-level",
2616 tmp, len/sizeof(*tmp));
2617 msm->vdd_no_vol_level = tmp[0];
2618 msm->vdd_low_vol_level = tmp[1];
2619 msm->vdd_high_vol_level = tmp[2];
2620 } else {
2621 dev_err(&pdev->dev, "no qcom,vdd-voltage-level property\n");
2622 ret = -EINVAL;
2623 goto disable_ref_clk;
2624 }
2625
Manu Gautam60e01352012-05-29 09:00:34 +05302626 /* SS PHY */
Manu Gautam60e01352012-05-29 09:00:34 +05302627 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
2628 if (IS_ERR(msm->ssusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302629 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
2630 ret = PTR_ERR(msm->ssusb_vddcx);
2631 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302632 }
2633
2634 ret = dwc3_ssusb_config_vddcx(1);
2635 if (ret) {
2636 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam3e9ad352012-08-16 14:44:47 -07002637 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302638 }
2639
2640 ret = regulator_enable(context->ssusb_vddcx);
2641 if (ret) {
2642 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
2643 goto unconfig_ss_vddcx;
2644 }
2645
2646 ret = dwc3_ssusb_ldo_init(1);
2647 if (ret) {
2648 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
2649 goto disable_ss_vddcx;
2650 }
2651
2652 ret = dwc3_ssusb_ldo_enable(1);
2653 if (ret) {
2654 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
2655 goto free_ss_ldo_init;
2656 }
2657
2658 /* HS PHY */
Manu Gautam60e01352012-05-29 09:00:34 +05302659 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
2660 if (IS_ERR(msm->hsusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302661 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
2662 ret = PTR_ERR(msm->hsusb_vddcx);
2663 goto disable_ss_ldo;
Manu Gautam60e01352012-05-29 09:00:34 +05302664 }
2665
2666 ret = dwc3_hsusb_config_vddcx(1);
2667 if (ret) {
2668 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2669 goto disable_ss_ldo;
2670 }
2671
2672 ret = regulator_enable(context->hsusb_vddcx);
2673 if (ret) {
2674 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
2675 goto unconfig_hs_vddcx;
2676 }
2677
2678 ret = dwc3_hsusb_ldo_init(1);
2679 if (ret) {
2680 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
2681 goto disable_hs_vddcx;
2682 }
2683
2684 ret = dwc3_hsusb_ldo_enable(1);
2685 if (ret) {
2686 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
2687 goto free_hs_ldo_init;
2688 }
2689
Jack Pham5c585062013-03-25 18:39:12 -07002690 msm->id_state = msm->ext_xceiv.id = DWC3_ID_FLOAT;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302691 msm->ext_xceiv.otg_capability = of_property_read_bool(node,
Manu Gautam6c0ff032012-11-02 14:55:35 +05302692 "qcom,otg-capability");
2693 msm->charger.charging_disabled = of_property_read_bool(node,
2694 "qcom,charging-disabled");
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302695
Hemant Kumar6d7b7242013-04-18 16:44:38 -07002696 msm->charger.skip_chg_detect = of_property_read_bool(node,
2697 "qcom,skip-charger-detection");
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302698 /*
2699 * DWC3 has separate IRQ line for OTG events (ID/BSV) and for
2700 * DP and DM linestate transitions during low power mode.
2701 */
2702 msm->hs_phy_irq = platform_get_irq_byname(pdev, "hs_phy_irq");
2703 if (msm->hs_phy_irq < 0) {
2704 dev_dbg(&pdev->dev, "pget_irq for hs_phy_irq failed\n");
2705 msm->hs_phy_irq = 0;
Jack Pham0fc12332012-11-19 13:14:22 -08002706 } else {
Jack Pham56a0a632013-03-08 13:18:42 -08002707 ret = devm_request_irq(&pdev->dev, msm->hs_phy_irq,
2708 msm_dwc3_irq, IRQF_TRIGGER_RISING,
2709 "msm_dwc3", msm);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302710 if (ret) {
2711 dev_err(&pdev->dev, "irqreq HSPHYINT failed\n");
2712 goto disable_hs_ldo;
2713 }
2714 enable_irq_wake(msm->hs_phy_irq);
2715 }
Jack Pham0cca9412013-03-08 13:22:42 -08002716
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302717 if (msm->ext_xceiv.otg_capability) {
Jack Pham0cca9412013-03-08 13:22:42 -08002718 msm->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
2719 if (msm->pmic_id_irq > 0) {
David Keitelad4a0282013-03-19 18:04:27 -07002720 /* check if PMIC ID IRQ is supported */
2721 ret = qpnp_misc_irqs_available(&pdev->dev);
2722
2723 if (ret == -EPROBE_DEFER) {
2724 /* qpnp hasn't probed yet; defer dwc probe */
Jack Pham0cca9412013-03-08 13:22:42 -08002725 goto disable_hs_ldo;
David Keitelad4a0282013-03-19 18:04:27 -07002726 } else if (ret == 0) {
2727 msm->pmic_id_irq = 0;
2728 } else {
2729 ret = devm_request_irq(&pdev->dev,
2730 msm->pmic_id_irq,
2731 dwc3_pmic_id_irq,
2732 IRQF_TRIGGER_RISING |
2733 IRQF_TRIGGER_FALLING,
2734 "dwc3_msm_pmic_id", msm);
2735 if (ret) {
2736 dev_err(&pdev->dev, "irqreq IDINT failed\n");
2737 goto disable_hs_ldo;
2738 }
Jack Pham9198d9f2013-04-09 17:54:54 -07002739
Manu Gautamf08f7b62013-04-02 16:09:42 +05302740 local_irq_save(flags);
2741 /* Update initial ID state */
Jack Pham9198d9f2013-04-09 17:54:54 -07002742 msm->id_state =
Manu Gautamf08f7b62013-04-02 16:09:42 +05302743 !!irq_read_line(msm->pmic_id_irq);
Jack Pham9198d9f2013-04-09 17:54:54 -07002744 if (msm->id_state == DWC3_ID_GROUND)
2745 queue_work(system_nrt_wq,
2746 &msm->id_work);
Manu Gautamf08f7b62013-04-02 16:09:42 +05302747 local_irq_restore(flags);
David Keitelad4a0282013-03-19 18:04:27 -07002748 enable_irq_wake(msm->pmic_id_irq);
Jack Pham0cca9412013-03-08 13:22:42 -08002749 }
David Keitelad4a0282013-03-19 18:04:27 -07002750 }
2751
2752 if (msm->pmic_id_irq <= 0) {
Jack Pham0cca9412013-03-08 13:22:42 -08002753 /* If no PMIC ID IRQ, use ADC for ID pin detection */
2754 queue_work(system_nrt_wq, &msm->init_adc_work.work);
2755 device_create_file(&pdev->dev, &dev_attr_adc_enable);
2756 msm->pmic_id_irq = 0;
2757 }
Manu Gautam377821c2012-09-28 16:53:24 +05302758 }
2759
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002760 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2761 if (!res) {
2762 dev_dbg(&pdev->dev, "missing TCSR memory resource\n");
2763 } else {
2764 tcsr = devm_ioremap_nocache(&pdev->dev, res->start,
2765 resource_size(res));
2766 if (!tcsr) {
2767 dev_dbg(&pdev->dev, "tcsr ioremap failed\n");
2768 } else {
2769 /* Enable USB3 on the primary USB port. */
2770 writel_relaxed(0x1, tcsr);
2771 /*
2772 * Ensure that TCSR write is completed before
2773 * USB registers initialization.
2774 */
2775 mb();
2776 }
2777 }
2778
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002779 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2780 if (!res) {
2781 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302782 ret = -ENODEV;
Jack Pham56a0a632013-03-08 13:18:42 -08002783 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002784 }
2785
2786 msm->base = devm_ioremap_nocache(&pdev->dev, res->start,
2787 resource_size(res));
2788 if (!msm->base) {
2789 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302790 ret = -ENODEV;
Jack Pham56a0a632013-03-08 13:18:42 -08002791 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002792 }
2793
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002794 msm->resource_size = resource_size(res);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002795
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05302796 if (of_property_read_u32(node, "qcom,dwc-hsphy-init",
2797 &msm->hsphy_init_seq))
2798 dev_dbg(&pdev->dev, "unable to read hsphy init seq\n");
2799 else if (!msm->hsphy_init_seq)
2800 dev_warn(&pdev->dev, "incorrect hsphyinitseq.Using PORvalue\n");
2801
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05302802 dwc3_msm_qscratch_reg_init(msm);
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +05302803
Manu Gautamb5067272012-07-02 09:53:41 +05302804 pm_runtime_set_active(msm->dev);
Manu Gautam377821c2012-09-28 16:53:24 +05302805 pm_runtime_enable(msm->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05302806
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002807 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
2808 &msm->dbm_num_eps)) {
2809 dev_err(&pdev->dev,
2810 "unable to read platform data num of dbm eps\n");
2811 msm->dbm_num_eps = DBM_MAX_EPS;
2812 }
2813
2814 if (msm->dbm_num_eps > DBM_MAX_EPS) {
2815 dev_err(&pdev->dev,
2816 "Driver doesn't support number of DBM EPs. "
2817 "max: %d, dbm_num_eps: %d\n",
2818 DBM_MAX_EPS, msm->dbm_num_eps);
2819 ret = -ENODEV;
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +05302820 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002821 }
2822
Manu Gautambb825d72013-03-12 16:25:42 +05302823 /* usb_psy required only for vbus_notifications or charging support */
2824 if (msm->ext_xceiv.otg_capability || !msm->charger.charging_disabled) {
2825 msm->usb_psy.name = "usb";
2826 msm->usb_psy.type = POWER_SUPPLY_TYPE_USB;
2827 msm->usb_psy.supplied_to = dwc3_msm_pm_power_supplied_to;
2828 msm->usb_psy.num_supplicants = ARRAY_SIZE(
2829 dwc3_msm_pm_power_supplied_to);
2830 msm->usb_psy.properties = dwc3_msm_pm_power_props_usb;
2831 msm->usb_psy.num_properties =
2832 ARRAY_SIZE(dwc3_msm_pm_power_props_usb);
2833 msm->usb_psy.get_property = dwc3_msm_power_get_property_usb;
2834 msm->usb_psy.set_property = dwc3_msm_power_set_property_usb;
2835 msm->usb_psy.external_power_changed =
2836 dwc3_msm_external_power_changed;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302837
Manu Gautambb825d72013-03-12 16:25:42 +05302838 ret = power_supply_register(&pdev->dev, &msm->usb_psy);
2839 if (ret < 0) {
2840 dev_err(&pdev->dev,
2841 "%s:power_supply_register usb failed\n",
2842 __func__);
2843 goto disable_hs_ldo;
2844 }
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302845 }
2846
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +05302847 if (node) {
2848 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2849 if (ret) {
2850 dev_err(&pdev->dev,
2851 "failed to add create dwc3 core\n");
2852 goto put_psupply;
2853 }
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002854 }
2855
Manu Gautam2617deb2012-08-31 17:50:06 -07002856 msm->bus_scale_table = msm_bus_cl_get_pdata(pdev);
2857 if (!msm->bus_scale_table) {
2858 dev_err(&pdev->dev, "bus scaling is disabled\n");
2859 } else {
2860 msm->bus_perf_client =
2861 msm_bus_scale_register_client(msm->bus_scale_table);
2862 ret = msm_bus_scale_client_update_request(
2863 msm->bus_perf_client, 1);
2864 if (ret)
2865 dev_err(&pdev->dev, "Failed to vote for bus scaling\n");
2866 }
2867
Manu Gautam8c642812012-06-07 10:35:10 +05302868 msm->otg_xceiv = usb_get_transceiver();
Manu Gautambb825d72013-03-12 16:25:42 +05302869 /* Register with OTG if present, ignore USB2 OTG using other PHY */
2870 if (msm->otg_xceiv && !(msm->otg_xceiv->flags & ENABLE_SECONDARY_PHY)) {
Hemant Kumar6d7b7242013-04-18 16:44:38 -07002871 /* Skip charger detection for simulator targets */
2872 if (!msm->charger.skip_chg_detect) {
2873 msm->charger.start_detection = dwc3_start_chg_det;
2874 ret = dwc3_set_charger(msm->otg_xceiv->otg,
2875 &msm->charger);
2876 if (ret || !msm->charger.notify_detection_complete) {
2877 dev_err(&pdev->dev,
2878 "failed to register charger: %d\n",
2879 ret);
2880 goto put_xcvr;
2881 }
Manu Gautam8c642812012-06-07 10:35:10 +05302882 }
Manu Gautamb5067272012-07-02 09:53:41 +05302883
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05302884 if (msm->ext_xceiv.otg_capability)
2885 msm->ext_xceiv.ext_block_reset = dwc3_msm_block_reset;
Manu Gautamb5067272012-07-02 09:53:41 +05302886 ret = dwc3_set_ext_xceiv(msm->otg_xceiv->otg, &msm->ext_xceiv);
2887 if (ret || !msm->ext_xceiv.notify_ext_events) {
2888 dev_err(&pdev->dev, "failed to register xceiver: %d\n",
2889 ret);
2890 goto put_xcvr;
2891 }
Manu Gautam8c642812012-06-07 10:35:10 +05302892 } else {
Manu Gautambb825d72013-03-12 16:25:42 +05302893 dev_dbg(&pdev->dev, "No OTG, DWC3 running in host only mode\n");
2894 msm->host_mode = 1;
2895 msm->vbus_otg = devm_regulator_get(&pdev->dev, "vbus_dwc3");
2896 if (IS_ERR(msm->vbus_otg)) {
2897 dev_dbg(&pdev->dev, "Failed to get vbus regulator\n");
2898 msm->vbus_otg = 0;
2899 } else {
2900 ret = regulator_enable(msm->vbus_otg);
2901 if (ret) {
2902 msm->vbus_otg = 0;
2903 dev_err(&pdev->dev, "Failed to enable vbus_otg\n");
2904 }
2905 }
2906 msm->otg_xceiv = NULL;
Manu Gautam8c642812012-06-07 10:35:10 +05302907 }
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302908 if (msm->ext_xceiv.otg_capability && msm->charger.start_detection) {
2909 ret = dwc3_msm_setup_cdev(msm);
2910 if (ret)
2911 dev_err(&pdev->dev, "Fail to setup dwc3 setup cdev\n");
2912 }
Manu Gautam8c642812012-06-07 10:35:10 +05302913
Manu Gautamb5067272012-07-02 09:53:41 +05302914 wake_lock_init(&msm->wlock, WAKE_LOCK_SUSPEND, "msm_dwc3");
2915 wake_lock(&msm->wlock);
2916 dwc3_debugfs_init(msm);
2917
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002918 return 0;
2919
Manu Gautam8c642812012-06-07 10:35:10 +05302920put_xcvr:
2921 usb_put_transceiver(msm->otg_xceiv);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302922put_psupply:
Manu Gautambb825d72013-03-12 16:25:42 +05302923 if (msm->usb_psy.dev)
2924 power_supply_unregister(&msm->usb_psy);
Manu Gautam60e01352012-05-29 09:00:34 +05302925disable_hs_ldo:
2926 dwc3_hsusb_ldo_enable(0);
2927free_hs_ldo_init:
2928 dwc3_hsusb_ldo_init(0);
2929disable_hs_vddcx:
2930 regulator_disable(context->hsusb_vddcx);
2931unconfig_hs_vddcx:
2932 dwc3_hsusb_config_vddcx(0);
2933disable_ss_ldo:
2934 dwc3_ssusb_ldo_enable(0);
2935free_ss_ldo_init:
2936 dwc3_ssusb_ldo_init(0);
2937disable_ss_vddcx:
2938 regulator_disable(context->ssusb_vddcx);
2939unconfig_ss_vddcx:
2940 dwc3_ssusb_config_vddcx(0);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002941disable_ref_clk:
2942 clk_disable_unprepare(msm->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002943disable_utmi_clk:
2944 clk_disable_unprepare(msm->utmi_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002945disable_sleep_a_clk:
2946 clk_disable_unprepare(msm->hsphy_sleep_clk);
2947disable_sleep_clk:
2948 clk_disable_unprepare(msm->sleep_clk);
2949disable_iface_clk:
2950 clk_disable_unprepare(msm->iface_clk);
Manu Gautam1742db22012-06-19 13:33:24 +05302951disable_core_clk:
2952 clk_disable_unprepare(msm->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302953disable_xo:
2954 clk_disable_unprepare(msm->xo_clk);
2955put_xo:
2956 clk_put(msm->xo_clk);
Hemant Kumar086bf6b2013-06-10 19:29:27 -07002957disable_dwc3_gdsc:
2958 dwc3_msm_config_gdsc(msm, 0);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002959
2960 return ret;
2961}
2962
2963static int __devexit dwc3_msm_remove(struct platform_device *pdev)
2964{
2965 struct dwc3_msm *msm = platform_get_drvdata(pdev);
2966
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05302967 if (!msm->ext_chg_device) {
2968 device_destroy(msm->ext_chg_class, msm->ext_chg_dev);
2969 cdev_del(&msm->ext_chg_cdev);
2970 class_destroy(msm->ext_chg_class);
2971 unregister_chrdev_region(msm->ext_chg_dev, 1);
2972 }
2973
Jack Pham0fc12332012-11-19 13:14:22 -08002974 if (msm->id_adc_detect)
2975 qpnp_adc_tm_usbid_end();
Manu Gautamb5067272012-07-02 09:53:41 +05302976 if (dwc3_debugfs_root)
2977 debugfs_remove_recursive(dwc3_debugfs_root);
Manu Gautam8c642812012-06-07 10:35:10 +05302978 if (msm->otg_xceiv) {
2979 dwc3_start_chg_det(&msm->charger, false);
2980 usb_put_transceiver(msm->otg_xceiv);
2981 }
Manu Gautambb825d72013-03-12 16:25:42 +05302982 if (msm->usb_psy.dev)
2983 power_supply_unregister(&msm->usb_psy);
2984 if (msm->vbus_otg)
2985 regulator_disable(msm->vbus_otg);
Jack Pham0fc12332012-11-19 13:14:22 -08002986
Manu Gautamb5067272012-07-02 09:53:41 +05302987 pm_runtime_disable(msm->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05302988 wake_lock_destroy(&msm->wlock);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002989
Manu Gautam60e01352012-05-29 09:00:34 +05302990 dwc3_hsusb_ldo_enable(0);
2991 dwc3_hsusb_ldo_init(0);
2992 regulator_disable(msm->hsusb_vddcx);
2993 dwc3_hsusb_config_vddcx(0);
2994 dwc3_ssusb_ldo_enable(0);
2995 dwc3_ssusb_ldo_init(0);
2996 regulator_disable(msm->ssusb_vddcx);
2997 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05302998 clk_disable_unprepare(msm->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002999 clk_disable_unprepare(msm->iface_clk);
3000 clk_disable_unprepare(msm->sleep_clk);
3001 clk_disable_unprepare(msm->hsphy_sleep_clk);
3002 clk_disable_unprepare(msm->ref_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05303003 clk_disable_unprepare(msm->xo_clk);
3004 clk_put(msm->xo_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05303005
Hemant Kumar086bf6b2013-06-10 19:29:27 -07003006 dwc3_msm_config_gdsc(msm, 0);
3007
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003008 return 0;
3009}
3010
Manu Gautamb5067272012-07-02 09:53:41 +05303011static int dwc3_msm_pm_suspend(struct device *dev)
3012{
3013 int ret = 0;
3014 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3015
3016 dev_dbg(dev, "dwc3-msm PM suspend\n");
3017
Manu Gautam8d98a572013-01-21 16:34:50 +05303018 flush_delayed_work_sync(&mdwc->resume_work);
3019 if (!atomic_read(&mdwc->in_lpm)) {
3020 dev_err(mdwc->dev, "Abort PM suspend!! (USB is outside LPM)\n");
3021 return -EBUSY;
3022 }
3023
Manu Gautamb5067272012-07-02 09:53:41 +05303024 ret = dwc3_msm_suspend(mdwc);
3025 if (!ret)
3026 atomic_set(&mdwc->pm_suspended, 1);
3027
3028 return ret;
3029}
3030
3031static int dwc3_msm_pm_resume(struct device *dev)
3032{
3033 int ret = 0;
3034 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3035
3036 dev_dbg(dev, "dwc3-msm PM resume\n");
3037
3038 atomic_set(&mdwc->pm_suspended, 0);
3039 if (mdwc->resume_pending) {
3040 mdwc->resume_pending = false;
3041
3042 ret = dwc3_msm_resume(mdwc);
3043 /* Update runtime PM status */
3044 pm_runtime_disable(dev);
3045 pm_runtime_set_active(dev);
3046 pm_runtime_enable(dev);
3047
3048 /* Let OTG know about resume event and update pm_count */
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05303049 if (mdwc->otg_xceiv) {
Manu Gautamb5067272012-07-02 09:53:41 +05303050 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
3051 DWC3_EVENT_PHY_RESUME);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05303052 if (mdwc->ext_xceiv.otg_capability)
3053 mdwc->ext_xceiv.notify_ext_events(
3054 mdwc->otg_xceiv->otg,
3055 DWC3_EVENT_XCEIV_STATE);
3056 }
Manu Gautamb5067272012-07-02 09:53:41 +05303057 }
3058
3059 return ret;
3060}
3061
3062static int dwc3_msm_runtime_idle(struct device *dev)
3063{
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05303064 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3065
Manu Gautamb5067272012-07-02 09:53:41 +05303066 dev_dbg(dev, "DWC3-msm runtime idle\n");
3067
Pavankumar Kondeti08693e72013-05-03 11:55:48 +05303068 if (mdwc->ext_chg_active) {
3069 dev_dbg(dev, "Deferring LPM\n");
3070 /*
3071 * Charger detection may happen in user space.
3072 * Delay entering LPM by 3 sec. Otherwise we
3073 * have to exit LPM when user space begins
3074 * charger detection.
3075 *
3076 * This timer will be canceled when user space
3077 * votes against LPM by incrementing PM usage
3078 * counter. We enter low power mode when
3079 * PM usage counter is decremented.
3080 */
3081 pm_schedule_suspend(dev, 3000);
3082 return -EAGAIN;
3083 }
3084
Manu Gautamb5067272012-07-02 09:53:41 +05303085 return 0;
3086}
3087
3088static int dwc3_msm_runtime_suspend(struct device *dev)
3089{
3090 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3091
3092 dev_dbg(dev, "DWC3-msm runtime suspend\n");
3093
3094 return dwc3_msm_suspend(mdwc);
3095}
3096
3097static int dwc3_msm_runtime_resume(struct device *dev)
3098{
3099 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
3100
3101 dev_dbg(dev, "DWC3-msm runtime resume\n");
3102
3103 return dwc3_msm_resume(mdwc);
3104}
3105
3106static const struct dev_pm_ops dwc3_msm_dev_pm_ops = {
3107 SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume)
3108 SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume,
3109 dwc3_msm_runtime_idle)
3110};
3111
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003112static const struct of_device_id of_dwc3_matach[] = {
3113 {
3114 .compatible = "qcom,dwc-usb3-msm",
3115 },
3116 { },
3117};
3118MODULE_DEVICE_TABLE(of, of_dwc3_matach);
3119
3120static struct platform_driver dwc3_msm_driver = {
3121 .probe = dwc3_msm_probe,
3122 .remove = __devexit_p(dwc3_msm_remove),
3123 .driver = {
3124 .name = "msm-dwc3",
Manu Gautamb5067272012-07-02 09:53:41 +05303125 .pm = &dwc3_msm_dev_pm_ops,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003126 .of_match_table = of_dwc3_matach,
3127 },
3128};
3129
Manu Gautam377821c2012-09-28 16:53:24 +05303130MODULE_LICENSE("GPL v2");
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02003131MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
3132
3133static int __devinit dwc3_msm_init(void)
3134{
3135 return platform_driver_register(&dwc3_msm_driver);
3136}
3137module_init(dwc3_msm_init);
3138
3139static void __exit dwc3_msm_exit(void)
3140{
3141 platform_driver_unregister(&dwc3_msm_driver);
3142}
3143module_exit(dwc3_msm_exit);