blob: 47101106173f10a8b73082d0c122ed0c3ef2afdb [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>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030029#include <linux/list.h>
Manu Gautamb5067272012-07-02 09:53:41 +053030#include <linux/debugfs.h>
31#include <linux/uaccess.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030032#include <linux/usb/ch9.h>
33#include <linux/usb/gadget.h>
David Keitelad4a0282013-03-19 18:04:27 -070034#include <linux/qpnp-misc.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030035#include <linux/usb/msm_hsusb.h>
Manu Gautam60e01352012-05-29 09:00:34 +053036#include <linux/regulator/consumer.h>
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +053037#include <linux/power_supply.h>
Jack Pham0fc12332012-11-19 13:14:22 -080038#include <linux/qpnp/qpnp-adc.h>
Manu Gautam60e01352012-05-29 09:00:34 +053039
40#include <mach/rpm-regulator.h>
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +053041#include <mach/rpm-regulator-smd.h>
Manu Gautam2617deb2012-08-31 17:50:06 -070042#include <mach/msm_bus.h>
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053043#include <mach/clk.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030044
Manu Gautam8c642812012-06-07 10:35:10 +053045#include "dwc3_otg.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030046#include "core.h"
47#include "gadget.h"
48
Jack Pham0fc12332012-11-19 13:14:22 -080049/* ADC threshold values */
50static int adc_low_threshold = 700;
51module_param(adc_low_threshold, int, S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(adc_low_threshold, "ADC ID Low voltage threshold");
53
54static int adc_high_threshold = 950;
55module_param(adc_high_threshold, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(adc_high_threshold, "ADC ID High voltage threshold");
57
58static int adc_meas_interval = ADC_MEAS1_INTERVAL_1S;
59module_param(adc_meas_interval, int, S_IRUGO | S_IWUSR);
60MODULE_PARM_DESC(adc_meas_interval, "ADC ID polling period");
61
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +053062static int override_phy_init;
63module_param(override_phy_init, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(override_phy_init, "Override HSPHY Init Seq");
65
Ido Shayevitz9fb83452012-04-01 17:45:58 +030066/**
67 * USB DBM Hardware registers.
68 *
69 */
Shimrit Malichia00d7322012-08-05 13:56:28 +030070#define DBM_BASE 0x000F8000
71#define DBM_EP_CFG(n) (DBM_BASE + (0x00 + 4 * (n)))
72#define DBM_DATA_FIFO(n) (DBM_BASE + (0x10 + 4 * (n)))
73#define DBM_DATA_FIFO_SIZE(n) (DBM_BASE + (0x20 + 4 * (n)))
74#define DBM_DATA_FIFO_EN (DBM_BASE + (0x30))
75#define DBM_GEVNTADR (DBM_BASE + (0x34))
76#define DBM_GEVNTSIZ (DBM_BASE + (0x38))
77#define DBM_DBG_CNFG (DBM_BASE + (0x3C))
78#define DBM_HW_TRB0_EP(n) (DBM_BASE + (0x40 + 4 * (n)))
79#define DBM_HW_TRB1_EP(n) (DBM_BASE + (0x50 + 4 * (n)))
80#define DBM_HW_TRB2_EP(n) (DBM_BASE + (0x60 + 4 * (n)))
81#define DBM_HW_TRB3_EP(n) (DBM_BASE + (0x70 + 4 * (n)))
82#define DBM_PIPE_CFG (DBM_BASE + (0x80))
83#define DBM_SOFT_RESET (DBM_BASE + (0x84))
84#define DBM_GEN_CFG (DBM_BASE + (0x88))
Ido Shayevitz9fb83452012-04-01 17:45:58 +030085
86/**
87 * USB DBM Hardware registers bitmask.
88 *
89 */
90/* DBM_EP_CFG */
Shimrit Malichia00d7322012-08-05 13:56:28 +030091#define DBM_EN_EP 0x00000001
92#define USB3_EPNUM 0x0000003E
Ido Shayevitz9fb83452012-04-01 17:45:58 +030093#define DBM_BAM_PIPE_NUM 0x000000C0
94#define DBM_PRODUCER 0x00000100
95#define DBM_DISABLE_WB 0x00000200
96#define DBM_INT_RAM_ACC 0x00000400
97
98/* DBM_DATA_FIFO_SIZE */
99#define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff
100
101/* DBM_GEVNTSIZ */
102#define DBM_GEVNTSIZ_MASK 0x0000ffff
103
104/* DBM_DBG_CNFG */
105#define DBM_ENABLE_IOC_MASK 0x0000000f
106
107/* DBM_SOFT_RESET */
108#define DBM_SFT_RST_EP0 0x00000001
109#define DBM_SFT_RST_EP1 0x00000002
110#define DBM_SFT_RST_EP2 0x00000004
111#define DBM_SFT_RST_EP3 0x00000008
Shimrit Malichia00d7322012-08-05 13:56:28 +0300112#define DBM_SFT_RST_EPS_MASK 0x0000000F
113#define DBM_SFT_RST_MASK 0x80000000
114#define DBM_EN_MASK 0x00000002
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200115
116#define DBM_MAX_EPS 4
117
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300118/* DBM TRB configurations */
119#define DBM_TRB_BIT 0x80000000
120#define DBM_TRB_DATA_SRC 0x40000000
121#define DBM_TRB_DMA 0x20000000
122#define DBM_TRB_EP_NUM(ep) (ep<<24)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300123
Manu Gautam8c642812012-06-07 10:35:10 +0530124/**
125 * USB QSCRATCH Hardware registers
126 *
127 */
128#define QSCRATCH_REG_OFFSET (0x000F8800)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300129#define QSCRATCH_GENERAL_CFG (QSCRATCH_REG_OFFSET + 0x08)
Manu Gautambd0e5782012-08-30 10:39:01 -0700130#define HS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x10)
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530131#define PARAMETER_OVERRIDE_X_REG (QSCRATCH_REG_OFFSET + 0x14)
Manu Gautam8c642812012-06-07 10:35:10 +0530132#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
133#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
134#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
135#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
Manu Gautamd4108b72012-12-14 17:35:18 +0530136#define CGCTL_REG (QSCRATCH_REG_OFFSET + 0x28)
Manu Gautambd0e5782012-08-30 10:39:01 -0700137#define SS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x30)
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +0530138#define SS_PHY_PARAM_CTRL_1 (QSCRATCH_REG_OFFSET + 0x34)
139#define SS_PHY_PARAM_CTRL_2 (QSCRATCH_REG_OFFSET + 0x38)
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530140#define SS_CR_PROTOCOL_DATA_IN_REG (QSCRATCH_REG_OFFSET + 0x3C)
141#define SS_CR_PROTOCOL_DATA_OUT_REG (QSCRATCH_REG_OFFSET + 0x40)
142#define SS_CR_PROTOCOL_CAP_ADDR_REG (QSCRATCH_REG_OFFSET + 0x44)
143#define SS_CR_PROTOCOL_CAP_DATA_REG (QSCRATCH_REG_OFFSET + 0x48)
144#define SS_CR_PROTOCOL_READ_REG (QSCRATCH_REG_OFFSET + 0x4C)
145#define SS_CR_PROTOCOL_WRITE_REG (QSCRATCH_REG_OFFSET + 0x50)
Manu Gautam8c642812012-06-07 10:35:10 +0530146
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300147struct dwc3_msm_req_complete {
148 struct list_head list_item;
149 struct usb_request *req;
150 void (*orig_complete)(struct usb_ep *ep,
151 struct usb_request *req);
152};
153
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200154struct dwc3_msm {
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +0530155 struct platform_device *dwc3;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200156 struct device *dev;
157 void __iomem *base;
158 u32 resource_size;
159 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300160 u8 ep_num_mapping[DBM_MAX_EPS];
161 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
162 struct list_head req_complete_list;
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +0530163 struct clk *xo_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700164 struct clk *ref_clk;
Manu Gautam1742db22012-06-19 13:33:24 +0530165 struct clk *core_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700166 struct clk *iface_clk;
167 struct clk *sleep_clk;
168 struct clk *hsphy_sleep_clk;
Jack Pham22698b82013-02-13 17:45:06 -0800169 struct clk *utmi_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530170 struct regulator *hsusb_3p3;
171 struct regulator *hsusb_1p8;
172 struct regulator *hsusb_vddcx;
173 struct regulator *ssusb_1p8;
174 struct regulator *ssusb_vddcx;
Manu Gautamb5067272012-07-02 09:53:41 +0530175 struct dwc3_ext_xceiv ext_xceiv;
176 bool resume_pending;
177 atomic_t pm_suspended;
178 atomic_t in_lpm;
Manu Gautam377821c2012-09-28 16:53:24 +0530179 int hs_phy_irq;
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530180 int hsphy_init_seq;
Manu Gautam377821c2012-09-28 16:53:24 +0530181 bool lpm_irq_seen;
Manu Gautamb5067272012-07-02 09:53:41 +0530182 struct delayed_work resume_work;
Manu Gautam6eb13e32013-02-01 15:19:15 +0530183 struct work_struct restart_usb_work;
Manu Gautamb5067272012-07-02 09:53:41 +0530184 struct wake_lock wlock;
Manu Gautam8c642812012-06-07 10:35:10 +0530185 struct dwc3_charger charger;
186 struct usb_phy *otg_xceiv;
187 struct delayed_work chg_work;
188 enum usb_chg_state chg_state;
Jack Pham0cca9412013-03-08 13:22:42 -0800189 int pmic_id_irq;
190 struct work_struct id_work;
Jack Pham0fc12332012-11-19 13:14:22 -0800191 struct qpnp_adc_tm_usbid_param adc_param;
192 struct delayed_work init_adc_work;
193 bool id_adc_detect;
Manu Gautam8c642812012-06-07 10:35:10 +0530194 u8 dcd_retries;
Manu Gautam2617deb2012-08-31 17:50:06 -0700195 u32 bus_perf_client;
196 struct msm_bus_scale_pdata *bus_scale_table;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530197 struct power_supply usb_psy;
Jack Pham9354c6a2012-12-20 19:19:32 -0800198 struct power_supply *ext_vbus_psy;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530199 unsigned int online;
200 unsigned int host_mode;
201 unsigned int current_max;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +0530202 unsigned int vdd_no_vol_level;
203 unsigned int vdd_low_vol_level;
204 unsigned int vdd_high_vol_level;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530205 bool vbus_active;
Jack Phamfadd6432012-12-07 19:03:41 -0800206 bool ext_inuse;
Jack Phamf12b7e12012-12-28 14:27:26 -0800207 enum dwc3_id_state id_state;
Manu Gautam60e01352012-05-29 09:00:34 +0530208};
209
210#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
211#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
212#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
213
214#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
215#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
216#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
217
218#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
219#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
220#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
221
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300222static struct dwc3_msm *context;
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +0530223static u64 dwc3_msm_dma_mask = DMA_BIT_MASK(64);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300224
Jack Phamfadd6432012-12-07 19:03:41 -0800225static struct usb_ext_notification *usb_ext;
226
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300227/**
228 *
229 * Read register with debug info.
230 *
231 * @base - DWC3 base virtual address.
232 * @offset - register offset.
233 *
234 * @return u32
235 */
236static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
237{
238 u32 val = ioread32(base + offset);
239 return val;
240}
241
242/**
243 * Read register masked field with debug info.
244 *
245 * @base - DWC3 base virtual address.
246 * @offset - register offset.
247 * @mask - register bitmask.
248 *
249 * @return u32
250 */
251static inline u32 dwc3_msm_read_reg_field(void *base,
252 u32 offset,
253 const u32 mask)
254{
255 u32 shift = find_first_bit((void *)&mask, 32);
256 u32 val = ioread32(base + offset);
257 val &= mask; /* clear other bits */
258 val >>= shift;
259 return val;
260}
261
262/**
263 *
264 * Write register with debug info.
265 *
266 * @base - DWC3 base virtual address.
267 * @offset - register offset.
268 * @val - value to write.
269 *
270 */
271static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
272{
273 iowrite32(val, base + offset);
274}
275
276/**
277 * Write register masked field with debug info.
278 *
279 * @base - DWC3 base virtual address.
280 * @offset - register offset.
281 * @mask - register bitmask.
282 * @val - value to write.
283 *
284 */
285static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
286 const u32 mask, u32 val)
287{
288 u32 shift = find_first_bit((void *)&mask, 32);
289 u32 tmp = ioread32(base + offset);
290
291 tmp &= ~mask; /* clear written bits */
292 val = tmp | (val << shift);
293 iowrite32(val, base + offset);
294}
295
296/**
Manu Gautam8c642812012-06-07 10:35:10 +0530297 * Write register and read back masked value to confirm it is written
298 *
299 * @base - DWC3 base virtual address.
300 * @offset - register offset.
301 * @mask - register bitmask specifying what should be updated
302 * @val - value to write.
303 *
304 */
305static inline void dwc3_msm_write_readback(void *base, u32 offset,
306 const u32 mask, u32 val)
307{
308 u32 write_val, tmp = ioread32(base + offset);
309
310 tmp &= ~mask; /* retain other bits */
311 write_val = tmp | val;
312
313 iowrite32(write_val, base + offset);
314
315 /* Read back to see if val was written */
316 tmp = ioread32(base + offset);
317 tmp &= mask; /* clear other bits */
318
319 if (tmp != val)
320 dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n",
321 __func__, val, offset);
322}
323
324/**
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530325 *
326 * Write SSPHY register with debug info.
327 *
328 * @base - DWC3 base virtual address.
329 * @addr - SSPHY address to write.
330 * @val - value to write.
331 *
332 */
333static void dwc3_msm_ssusb_write_phycreg(void *base, u32 addr, u32 val)
334{
335 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
336 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
337 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
338 cpu_relax();
339
340 iowrite32(val, base + SS_CR_PROTOCOL_DATA_IN_REG);
341 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_DATA_REG);
342 while (ioread32(base + SS_CR_PROTOCOL_CAP_DATA_REG))
343 cpu_relax();
344
345 iowrite32(0x1, base + SS_CR_PROTOCOL_WRITE_REG);
346 while (ioread32(base + SS_CR_PROTOCOL_WRITE_REG))
347 cpu_relax();
348}
349
350/**
351 *
352 * Read SSPHY register with debug info.
353 *
354 * @base - DWC3 base virtual address.
355 * @addr - SSPHY address to read.
356 *
357 */
358static u32 dwc3_msm_ssusb_read_phycreg(void *base, u32 addr)
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(0x1, base + SS_CR_PROTOCOL_READ_REG);
366 while (ioread32(base + SS_CR_PROTOCOL_READ_REG))
367 cpu_relax();
368
369 return ioread32(base + SS_CR_PROTOCOL_DATA_OUT_REG);
370}
371
372/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300373 * Return DBM EP number according to usb endpoint number.
374 *
375 */
376static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep)
377{
378 int i;
379
380 for (i = 0; i < context->dbm_num_eps; i++)
381 if (context->ep_num_mapping[i] == usb_ep)
382 return i;
383
384 return -ENODEV; /* Not found */
385}
386
387/**
388 * Return number of configured DBM endpoints.
389 *
390 */
391static int dwc3_msm_configured_dbm_ep_num(void)
392{
393 int i;
394 int count = 0;
395
396 for (i = 0; i < context->dbm_num_eps; i++)
397 if (context->ep_num_mapping[i])
398 count++;
399
400 return count;
401}
402
403/**
404 * Configure the DBM with the USB3 core event buffer.
405 * This function is called by the SNPS UDC upon initialization.
406 *
407 * @addr - address of the event buffer.
408 * @size - size of the event buffer.
409 *
410 */
411static int dwc3_msm_event_buffer_config(u32 addr, u16 size)
412{
413 dev_dbg(context->dev, "%s\n", __func__);
414
415 dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr);
416 dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ,
417 DBM_GEVNTSIZ_MASK, size);
418
419 return 0;
420}
421
422/**
423 * Reset the DBM registers upon initialization.
424 *
425 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300426static int dwc3_msm_dbm_soft_reset(int enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300427{
428 dev_dbg(context->dev, "%s\n", __func__);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300429 if (enter_reset) {
430 dev_dbg(context->dev, "enter DBM reset\n");
431 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
432 DBM_SFT_RST_MASK, 1);
433 } else {
434 dev_dbg(context->dev, "exit DBM reset\n");
435 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
436 DBM_SFT_RST_MASK, 0);
437 /*enable DBM*/
438 dwc3_msm_write_reg_field(context->base, QSCRATCH_GENERAL_CFG,
439 DBM_EN_MASK, 0x1);
440 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300441
442 return 0;
443}
444
445/**
446 * Soft reset specific DBM ep.
447 * This function is called by the function driver upon events
448 * such as transfer aborting, USB re-enumeration and USB
449 * disconnection.
450 *
451 * @dbm_ep - DBM ep number.
452 * @enter_reset - should we enter a reset state or get out of it.
453 *
454 */
455static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset)
456{
457 dev_dbg(context->dev, "%s\n", __func__);
458
459 if (dbm_ep >= context->dbm_num_eps) {
460 dev_err(context->dev,
461 "%s: Invalid DBM ep index\n", __func__);
462 return -ENODEV;
463 }
464
465 if (enter_reset) {
466 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300467 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300468 } else {
469 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300470 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300471 }
472
473 return 0;
474}
475
476/**
477 * Configure a USB DBM ep to work in BAM mode.
478 *
479 *
480 * @usb_ep - USB physical EP number.
481 * @producer - producer/consumer.
482 * @disable_wb - disable write back to system memory.
483 * @internal_mem - use internal USB memory for data fifo.
484 * @ioc - enable interrupt on completion.
485 *
486 * @return int - DBM ep number.
487 */
488static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe,
489 bool producer, bool disable_wb,
490 bool internal_mem, bool ioc)
491{
492 u8 dbm_ep;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300493 u32 ep_cfg;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300494
495 dev_dbg(context->dev, "%s\n", __func__);
496
Shimrit Malichia00d7322012-08-05 13:56:28 +0300497 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
498
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300499 if (dbm_ep < 0) {
Shimrit Malichia00d7322012-08-05 13:56:28 +0300500 dev_err(context->dev,
501 "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300502 return -ENODEV;
503 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300504 /* First, reset the dbm endpoint */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300505 dwc3_msm_dbm_ep_soft_reset(dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300506
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300507 /* Set ioc bit for dbm_ep if needed */
508 dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300509 DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300510
Shimrit Malichia00d7322012-08-05 13:56:28 +0300511 ep_cfg = (producer ? DBM_PRODUCER : 0) |
512 (disable_wb ? DBM_DISABLE_WB : 0) |
513 (internal_mem ? DBM_INT_RAM_ACC : 0);
514
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300515 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
Shimrit Malichia00d7322012-08-05 13:56:28 +0300516 DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8);
517
518 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM,
519 usb_ep);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300520 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
521 DBM_BAM_PIPE_NUM, bam_pipe);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300522 dwc3_msm_write_reg_field(context->base, DBM_PIPE_CFG, 0x000000ff,
523 0xe4);
524 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP,
525 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300526
527 return dbm_ep;
528}
529
530/**
531 * Configure a USB DBM ep to work in normal mode.
532 *
533 * @usb_ep - USB ep number.
534 *
535 */
536static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep)
537{
538 u8 dbm_ep;
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530539 u32 data;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300540
541 dev_dbg(context->dev, "%s\n", __func__);
542
543 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
544
545 if (dbm_ep < 0) {
546 dev_err(context->dev,
547 "%s: Invalid usb ep index\n", __func__);
548 return -ENODEV;
549 }
550
551 context->ep_num_mapping[dbm_ep] = 0;
552
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530553 data = dwc3_msm_read_reg(context->base, DBM_EP_CFG(dbm_ep));
554 data &= (~0x1);
555 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), data);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300556
557 /* Reset the dbm endpoint */
558 dwc3_msm_dbm_ep_soft_reset(dbm_ep, true);
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530559 /*
560 * 10 usec delay is required before deasserting DBM endpoint reset
561 * according to hardware programming guide.
562 */
563 udelay(10);
564 dwc3_msm_dbm_ep_soft_reset(dbm_ep, false);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300565
566 return 0;
567}
568
569/**
570 * Configure the DBM with the BAM's data fifo.
571 * This function is called by the USB BAM Driver
572 * upon initialization.
573 *
574 * @ep - pointer to usb endpoint.
575 * @addr - address of data fifo.
576 * @size - size of data fifo.
577 *
578 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300579int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300580{
581 u8 dbm_ep;
582 struct dwc3_ep *dep = to_dwc3_ep(ep);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300583 u8 bam_pipe = dst_pipe_idx;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300584
585 dev_dbg(context->dev, "%s\n", __func__);
586
Shimrit Malichia00d7322012-08-05 13:56:28 +0300587 dbm_ep = bam_pipe;
588 context->ep_num_mapping[dbm_ep] = dep->number;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300589
590 dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr);
591 dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep),
592 DBM_DATA_FIFO_SIZE_MASK, size);
593
594 return 0;
595}
596
597/**
598* Cleanups for msm endpoint on request complete.
599*
600* Also call original request complete.
601*
602* @usb_ep - pointer to usb_ep instance.
603* @request - pointer to usb_request instance.
604*
605* @return int - 0 on success, negetive on error.
606*/
607static void dwc3_msm_req_complete_func(struct usb_ep *ep,
608 struct usb_request *request)
609{
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300610 struct dwc3_ep *dep = to_dwc3_ep(ep);
611 struct dwc3_msm_req_complete *req_complete = NULL;
612
613 /* Find original request complete function and remove it from list */
614 list_for_each_entry(req_complete,
615 &context->req_complete_list,
616 list_item) {
617 if (req_complete->req == request)
618 break;
619 }
620 if (!req_complete || req_complete->req != request) {
621 dev_err(dep->dwc->dev, "%s: could not find the request\n",
622 __func__);
623 return;
624 }
625 list_del(&req_complete->list_item);
626
627 /*
628 * Release another one TRB to the pool since DBM queue took 2 TRBs
629 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
630 * released only one.
631 */
Manu Gautam55d34222012-12-19 16:49:47 +0530632 dep->busy_slot++;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300633
634 /* Unconfigure dbm ep */
635 dwc3_msm_dbm_ep_unconfig(dep->number);
636
637 /*
638 * If this is the last endpoint we unconfigured, than reset also
639 * the event buffers.
640 */
641 if (0 == dwc3_msm_configured_dbm_ep_num())
642 dwc3_msm_event_buffer_config(0, 0);
643
644 /*
645 * Call original complete function, notice that dwc->lock is already
646 * taken by the caller of this function (dwc3_gadget_giveback()).
647 */
648 request->complete = req_complete->orig_complete;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300649 if (request->complete)
650 request->complete(ep, request);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300651
652 kfree(req_complete);
653}
654
655/**
656* Helper function.
657* See the header of the dwc3_msm_ep_queue function.
658*
659* @dwc3_ep - pointer to dwc3_ep instance.
660* @req - pointer to dwc3_request instance.
661*
662* @return int - 0 on success, negetive on error.
663*/
664static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
665{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300666 struct dwc3_trb *trb;
667 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300668 struct dwc3_gadget_ep_cmd_params params;
669 u32 cmd;
670 int ret = 0;
671
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300672 /* We push the request to the dep->req_queued list to indicate that
673 * this request is issued with start transfer. The request will be out
674 * from this list in 2 cases. The first is that the transfer will be
675 * completed (not if the transfer is endless using a circular TRBs with
676 * with link TRB). The second case is an option to do stop stransfer,
677 * this can be initiated by the function driver when calling dequeue.
678 */
679 req->queued = true;
680 list_add_tail(&req->list, &dep->req_queued);
681
682 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300683 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300684 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300685 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300686
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300687 req->trb = trb;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300688 trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300689 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
690 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300691 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300692
693 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300694 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300695 dep->free_slot++;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300696 memset(trb_link, 0, sizeof *trb_link);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300697
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300698 trb_link->bpl = lower_32_bits(req->trb_dma);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300699 trb_link->bph = DBM_TRB_BIT |
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300700 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
701 trb_link->size = 0;
702 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300703
704 /*
705 * Now start the transfer
706 */
707 memset(&params, 0, sizeof(params));
Shimrit Malichia00d7322012-08-05 13:56:28 +0300708 params.param0 = 0; /* TDAddr High */
709 params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */
710
Manu Gautam5b2bf9a2012-10-18 10:52:50 +0530711 /* DBM requires IOC to be set */
712 cmd = DWC3_DEPCMD_STARTTRANSFER | DWC3_DEPCMD_CMDIOC;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300713 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
714 if (ret < 0) {
715 dev_dbg(dep->dwc->dev,
716 "%s: failed to send STARTTRANSFER command\n",
717 __func__);
718
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300719 list_del(&req->list);
720 return ret;
721 }
Manu Gautam4a51a062012-12-07 11:24:39 +0530722 dep->flags |= DWC3_EP_BUSY;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300723
724 return ret;
725}
726
727/**
728* Queue a usb request to the DBM endpoint.
729* This function should be called after the endpoint
730* was enabled by the ep_enable.
731*
732* This function prepares special structure of TRBs which
733* is familier with the DBM HW, so it will possible to use
734* this endpoint in DBM mode.
735*
736* The TRBs prepared by this function, is one normal TRB
737* which point to a fake buffer, followed by a link TRB
738* that points to the first TRB.
739*
740* The API of this function follow the regular API of
741* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
742*
743* @usb_ep - pointer to usb_ep instance.
744* @request - pointer to usb_request instance.
745* @gfp_flags - possible flags.
746*
747* @return int - 0 on success, negetive on error.
748*/
749static int dwc3_msm_ep_queue(struct usb_ep *ep,
750 struct usb_request *request, gfp_t gfp_flags)
751{
752 struct dwc3_request *req = to_dwc3_request(request);
753 struct dwc3_ep *dep = to_dwc3_ep(ep);
754 struct dwc3 *dwc = dep->dwc;
755 struct dwc3_msm_req_complete *req_complete;
756 unsigned long flags;
757 int ret = 0;
758 u8 bam_pipe;
759 bool producer;
760 bool disable_wb;
761 bool internal_mem;
762 bool ioc;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300763 u8 speed;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300764
765 if (!(request->udc_priv & MSM_SPS_MODE)) {
766 /* Not SPS mode, call original queue */
767 dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n",
768 __func__);
769
770 return (context->original_ep_ops[dep->number])->queue(ep,
771 request,
772 gfp_flags);
773 }
774
775 if (!dep->endpoint.desc) {
776 dev_err(dwc->dev,
777 "%s: trying to queue request %p to disabled ep %s\n",
778 __func__, request, ep->name);
779 return -EPERM;
780 }
781
782 if (dep->number == 0 || dep->number == 1) {
783 dev_err(dwc->dev,
784 "%s: trying to queue dbm request %p to control ep %s\n",
785 __func__, request, ep->name);
786 return -EPERM;
787 }
788
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300789
Manu Gautam4a51a062012-12-07 11:24:39 +0530790 if (dep->busy_slot != dep->free_slot || !list_empty(&dep->request_list)
791 || !list_empty(&dep->req_queued)) {
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300792 dev_err(dwc->dev,
793 "%s: trying to queue dbm request %p tp ep %s\n",
794 __func__, request, ep->name);
795 return -EPERM;
Manu Gautam4a51a062012-12-07 11:24:39 +0530796 } else {
797 dep->busy_slot = 0;
798 dep->free_slot = 0;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300799 }
800
801 /*
802 * Override req->complete function, but before doing that,
803 * store it's original pointer in the req_complete_list.
804 */
805 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
806 if (!req_complete) {
807 dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__);
808 return -ENOMEM;
809 }
810 req_complete->req = request;
811 req_complete->orig_complete = request->complete;
812 list_add_tail(&req_complete->list_item, &context->req_complete_list);
813 request->complete = dwc3_msm_req_complete_func;
814
815 /*
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300816 * Configure the DBM endpoint
817 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300818 bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300819 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
820 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
821 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
822 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
823
824 ret = dwc3_msm_dbm_ep_config(dep->number,
825 bam_pipe, producer,
826 disable_wb, internal_mem, ioc);
827 if (ret < 0) {
828 dev_err(context->dev,
829 "error %d after calling dwc3_msm_dbm_ep_config\n",
830 ret);
831 return ret;
832 }
833
834 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
835 __func__, request, ep->name, request->length);
836
837 /*
838 * We must obtain the lock of the dwc3 core driver,
839 * including disabling interrupts, so we will be sure
840 * that we are the only ones that configure the HW device
841 * core and ensure that we queuing the request will finish
842 * as soon as possible so we will release back the lock.
843 */
844 spin_lock_irqsave(&dwc->lock, flags);
845 ret = __dwc3_msm_ep_queue(dep, req);
846 spin_unlock_irqrestore(&dwc->lock, flags);
847 if (ret < 0) {
848 dev_err(context->dev,
849 "error %d after calling __dwc3_msm_ep_queue\n", ret);
850 return ret;
851 }
852
Shimrit Malichia00d7322012-08-05 13:56:28 +0300853 speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD;
854 dwc3_msm_write_reg(context->base, DBM_GEN_CFG, speed >> 2);
855
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300856 return 0;
857}
858
859/**
860 * Configure MSM endpoint.
861 * This function do specific configurations
862 * to an endpoint which need specific implementaion
863 * in the MSM architecture.
864 *
865 * This function should be called by usb function/class
866 * layer which need a support from the specific MSM HW
867 * which wrap the USB3 core. (like DBM specific endpoints)
868 *
869 * @ep - a pointer to some usb_ep instance
870 *
871 * @return int - 0 on success, negetive on error.
872 */
873int msm_ep_config(struct usb_ep *ep)
874{
875 struct dwc3_ep *dep = to_dwc3_ep(ep);
876 struct usb_ep_ops *new_ep_ops;
877
Manu Gautama302f612012-12-18 17:33:06 +0530878 dwc3_msm_event_buffer_config(dwc3_msm_read_reg(context->base,
879 DWC3_GEVNTADRLO(0)),
880 dwc3_msm_read_reg(context->base, DWC3_GEVNTSIZ(0)));
881
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300882 /* Save original ep ops for future restore*/
883 if (context->original_ep_ops[dep->number]) {
884 dev_err(context->dev,
885 "ep [%s,%d] already configured as msm endpoint\n",
886 ep->name, dep->number);
887 return -EPERM;
888 }
889 context->original_ep_ops[dep->number] = ep->ops;
890
891 /* Set new usb ops as we like */
892 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
893 if (!new_ep_ops) {
894 dev_err(context->dev,
895 "%s: unable to allocate mem for new usb ep ops\n",
896 __func__);
897 return -ENOMEM;
898 }
899 (*new_ep_ops) = (*ep->ops);
900 new_ep_ops->queue = dwc3_msm_ep_queue;
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530901 new_ep_ops->disable = ep->ops->disable;
902
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300903 ep->ops = new_ep_ops;
904
905 /*
906 * Do HERE more usb endpoint configurations
907 * which are specific to MSM.
908 */
909
910 return 0;
911}
912EXPORT_SYMBOL(msm_ep_config);
913
914/**
915 * Un-configure MSM endpoint.
916 * Tear down configurations done in the
917 * dwc3_msm_ep_config function.
918 *
919 * @ep - a pointer to some usb_ep instance
920 *
921 * @return int - 0 on success, negetive on error.
922 */
923int msm_ep_unconfig(struct usb_ep *ep)
924{
925 struct dwc3_ep *dep = to_dwc3_ep(ep);
926 struct usb_ep_ops *old_ep_ops;
927
928 /* Restore original ep ops */
929 if (!context->original_ep_ops[dep->number]) {
930 dev_err(context->dev,
931 "ep [%s,%d] was not configured as msm endpoint\n",
932 ep->name, dep->number);
933 return -EINVAL;
934 }
935 old_ep_ops = (struct usb_ep_ops *)ep->ops;
936 ep->ops = context->original_ep_ops[dep->number];
937 context->original_ep_ops[dep->number] = NULL;
938 kfree(old_ep_ops);
939
940 /*
941 * Do HERE more usb endpoint un-configurations
942 * which are specific to MSM.
943 */
944
945 return 0;
946}
947EXPORT_SYMBOL(msm_ep_unconfig);
948
Manu Gautam6eb13e32013-02-01 15:19:15 +0530949static void dwc3_restart_usb_work(struct work_struct *w)
950{
951 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
952 restart_usb_work);
953
954 dev_dbg(mdwc->dev, "%s\n", __func__);
955
956 if (atomic_read(&mdwc->in_lpm) || !mdwc->otg_xceiv) {
957 dev_err(mdwc->dev, "%s failed!!!\n", __func__);
958 return;
959 }
960
961 if (!mdwc->ext_xceiv.bsv) {
962 dev_dbg(mdwc->dev, "%s bailing out in disconnect\n", __func__);
963 return;
964 }
965
966 /* Reset active USB connection */
967 mdwc->ext_xceiv.bsv = false;
968 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
969 /* Make sure disconnect is processed before sending connect */
970 flush_delayed_work(&mdwc->resume_work);
971
972 mdwc->ext_xceiv.bsv = true;
973 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
974}
975
976/**
977 * Reset USB peripheral connection
978 * Inform OTG for Vbus LOW followed by Vbus HIGH notification.
979 * This performs full hardware reset and re-initialization which
980 * might be required by some DBM client driver during uninit/cleanup.
981 */
982void msm_dwc3_restart_usb_session(void)
983{
984 struct dwc3_msm *mdwc = context;
985
986 dev_dbg(mdwc->dev, "%s\n", __func__);
987 queue_work(system_nrt_wq, &mdwc->restart_usb_work);
988
989 return;
990}
991EXPORT_SYMBOL(msm_dwc3_restart_usb_session);
992
Jack Phamfadd6432012-12-07 19:03:41 -0800993/**
994 * msm_register_usb_ext_notification: register for event notification
995 * @info: pointer to client usb_ext_notification structure. May be NULL.
996 *
997 * @return int - 0 on success, negative on error
998 */
999int msm_register_usb_ext_notification(struct usb_ext_notification *info)
1000{
1001 pr_debug("%s usb_ext: %p\n", __func__, info);
1002
1003 if (info) {
1004 if (usb_ext) {
1005 pr_err("%s: already registered\n", __func__);
1006 return -EEXIST;
1007 }
1008
1009 if (!info->notify) {
1010 pr_err("%s: notify is NULL\n", __func__);
1011 return -EINVAL;
1012 }
1013 }
1014
1015 usb_ext = info;
1016 return 0;
1017}
1018EXPORT_SYMBOL(msm_register_usb_ext_notification);
1019
Manu Gautam60e01352012-05-29 09:00:34 +05301020/* HSPHY */
1021static int dwc3_hsusb_config_vddcx(int high)
1022{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301023 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301024 struct dwc3_msm *dwc = context;
Manu Gautam60e01352012-05-29 09:00:34 +05301025
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301026 max_vol = dwc->vdd_high_vol_level;
1027 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301028 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
1029 if (ret) {
1030 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
1031 return ret;
1032 }
1033
1034 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1035 min_vol, max_vol);
1036
1037 return ret;
1038}
1039
1040static int dwc3_hsusb_ldo_init(int init)
1041{
1042 int rc = 0;
1043 struct dwc3_msm *dwc = context;
1044
1045 if (!init) {
1046 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
1047 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1048 return 0;
1049 }
1050
1051 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
1052 if (IS_ERR(dwc->hsusb_3p3)) {
1053 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
1054 return PTR_ERR(dwc->hsusb_3p3);
1055 }
1056
1057 rc = regulator_set_voltage(dwc->hsusb_3p3,
1058 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
1059 if (rc) {
1060 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
1061 return rc;
1062 }
1063 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
1064 if (IS_ERR(dwc->hsusb_1p8)) {
1065 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
1066 rc = PTR_ERR(dwc->hsusb_1p8);
1067 goto devote_3p3;
1068 }
1069 rc = regulator_set_voltage(dwc->hsusb_1p8,
1070 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
1071 if (rc) {
1072 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
1073 goto devote_3p3;
1074 }
1075
1076 return 0;
1077
1078devote_3p3:
1079 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1080
1081 return rc;
1082}
1083
1084static int dwc3_hsusb_ldo_enable(int on)
1085{
1086 int rc = 0;
1087 struct dwc3_msm *dwc = context;
1088
1089 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1090
1091 if (!on)
1092 goto disable_regulators;
1093
1094
1095 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
1096 if (rc < 0) {
1097 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
1098 return rc;
1099 }
1100
1101 rc = regulator_enable(dwc->hsusb_1p8);
1102 if (rc) {
1103 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
1104 goto put_1p8_lpm;
1105 }
1106
1107 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
1108 if (rc < 0) {
1109 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
1110 goto disable_1p8;
1111 }
1112
1113 rc = regulator_enable(dwc->hsusb_3p3);
1114 if (rc) {
1115 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
1116 goto put_3p3_lpm;
1117 }
1118
1119 return 0;
1120
1121disable_regulators:
1122 rc = regulator_disable(dwc->hsusb_3p3);
1123 if (rc)
1124 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
1125
1126put_3p3_lpm:
1127 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
1128 if (rc < 0)
1129 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
1130
1131disable_1p8:
1132 rc = regulator_disable(dwc->hsusb_1p8);
1133 if (rc)
1134 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
1135
1136put_1p8_lpm:
1137 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
1138 if (rc < 0)
1139 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
1140
1141 return rc < 0 ? rc : 0;
1142}
1143
1144/* SSPHY */
1145static int dwc3_ssusb_config_vddcx(int high)
1146{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301147 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301148 struct dwc3_msm *dwc = context;
Manu Gautam60e01352012-05-29 09:00:34 +05301149
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301150 max_vol = dwc->vdd_high_vol_level;
1151 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301152 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
1153 if (ret) {
1154 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
1155 return ret;
1156 }
1157
1158 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1159 min_vol, max_vol);
1160 return ret;
1161}
1162
1163/* 3.3v supply not needed for SS PHY */
1164static int dwc3_ssusb_ldo_init(int init)
1165{
1166 int rc = 0;
1167 struct dwc3_msm *dwc = context;
1168
1169 if (!init) {
1170 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
1171 return 0;
1172 }
1173
1174 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
1175 if (IS_ERR(dwc->ssusb_1p8)) {
1176 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
1177 return PTR_ERR(dwc->ssusb_1p8);
1178 }
1179 rc = regulator_set_voltage(dwc->ssusb_1p8,
1180 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
1181 if (rc)
1182 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
1183
1184 return rc;
1185}
1186
1187static int dwc3_ssusb_ldo_enable(int on)
1188{
1189 int rc = 0;
1190 struct dwc3_msm *dwc = context;
1191
1192 dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1193
1194 if (!on)
1195 goto disable_regulators;
1196
1197
1198 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1199 if (rc < 0) {
1200 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1201 return rc;
1202 }
1203
1204 rc = regulator_enable(dwc->ssusb_1p8);
1205 if (rc) {
1206 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1207 goto put_1p8_lpm;
1208 }
1209
1210 return 0;
1211
1212disable_regulators:
1213 rc = regulator_disable(dwc->ssusb_1p8);
1214 if (rc)
1215 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1216
1217put_1p8_lpm:
1218 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1219 if (rc < 0)
1220 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1221
1222 return rc < 0 ? rc : 0;
1223}
1224
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301225static int dwc3_msm_link_clk_reset(bool assert)
1226{
1227 int ret = 0;
1228 struct dwc3_msm *mdwc = context;
1229
1230 if (assert) {
1231 /* Using asynchronous block reset to the hardware */
1232 dev_dbg(mdwc->dev, "block_reset ASSERT\n");
1233 clk_disable_unprepare(mdwc->ref_clk);
1234 clk_disable_unprepare(mdwc->iface_clk);
1235 clk_disable_unprepare(mdwc->core_clk);
1236 ret = clk_reset(mdwc->core_clk, CLK_RESET_ASSERT);
1237 if (ret)
1238 dev_err(mdwc->dev, "dwc3 core_clk assert failed\n");
1239 } else {
1240 dev_dbg(mdwc->dev, "block_reset DEASSERT\n");
1241 ret = clk_reset(mdwc->core_clk, CLK_RESET_DEASSERT);
1242 ndelay(200);
1243 clk_prepare_enable(mdwc->core_clk);
1244 clk_prepare_enable(mdwc->ref_clk);
1245 clk_prepare_enable(mdwc->iface_clk);
1246 if (ret)
1247 dev_err(mdwc->dev, "dwc3 core_clk deassert failed\n");
1248 }
1249
1250 return ret;
1251}
1252
1253/* Initialize QSCRATCH registers for HSPHY and SSPHY operation */
1254static void dwc3_msm_qscratch_reg_init(struct dwc3_msm *msm)
1255{
1256 u32 data = 0;
1257
1258 /* SSPHY Initialization: Use ref_clk from pads and set its parameters */
1259 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1260 msleep(30);
1261 /* Assert SSPHY reset */
1262 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210082);
1263 usleep_range(2000, 2200);
1264 /* De-assert SSPHY reset - power and ref_clock must be ON */
1265 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1266 usleep_range(2000, 2200);
1267 /* Ref clock must be stable now, enable ref clock for HS mode */
1268 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210102);
1269 usleep_range(2000, 2200);
1270 /*
1271 * HSPHY Initialization: Enable UTMI clock and clamp enable HVINTs,
1272 * and disable RETENTION (power-on default is ENABLED)
1273 */
1274 dwc3_msm_write_reg(msm->base, HS_PHY_CTRL_REG, 0x5220bb2);
1275 usleep_range(2000, 2200);
1276 /* Disable (bypass) VBUS and ID filters */
1277 dwc3_msm_write_reg(msm->base, QSCRATCH_GENERAL_CFG, 0x78);
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05301278 /*
1279 * write HSPHY init value to QSCRATCH reg to set HSPHY parameters like
1280 * VBUS valid threshold, disconnect valid threshold, DC voltage level,
1281 * preempasis and rise/fall time.
1282 */
1283 if (override_phy_init)
1284 msm->hsphy_init_seq = override_phy_init;
1285 if (msm->hsphy_init_seq)
1286 dwc3_msm_write_readback(msm->base,
1287 PARAMETER_OVERRIDE_X_REG, 0x03FFFFFF,
1288 msm->hsphy_init_seq & 0x03FFFFFF);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301289
Manu Gautamd4108b72012-12-14 17:35:18 +05301290 /* Enable master clock for RAMs to allow BAM to access RAMs when
1291 * RAM clock gating is enabled via DWC3's GCTL. Otherwise, issues
1292 * are seen where RAM clocks get turned OFF in SS mode
1293 */
1294 dwc3_msm_write_reg(msm->base, CGCTL_REG,
1295 dwc3_msm_read_reg(msm->base, CGCTL_REG) | 0x18);
1296
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301297 /*
1298 * WORKAROUND: There is SSPHY suspend bug due to which USB enumerates
1299 * in HS mode instead of SS mode. Workaround it by asserting
1300 * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus mode
1301 */
1302 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x102D);
1303 data |= (1 << 7);
1304 dwc3_msm_ssusb_write_phycreg(msm->base, 0x102D, data);
1305
1306 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1010);
1307 data &= ~0xFF0;
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301308 data |= 0x20;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301309 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1010, data);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301310
1311 /*
1312 * Fix RX Equalization setting as follows
1313 * LANE0.RX_OVRD_IN_HI. RX_EQ_EN set to 0
1314 * LANE0.RX_OVRD_IN_HI.RX_EQ_EN_OVRD set to 1
1315 * LANE0.RX_OVRD_IN_HI.RX_EQ set to 3
1316 * LANE0.RX_OVRD_IN_HI.RX_EQ_OVRD set to 1
1317 */
1318 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1006);
1319 data &= ~(1 << 6);
1320 data |= (1 << 7);
1321 data &= ~(0x7 << 8);
1322 data |= (0x3 << 8);
1323 data |= (0x1 << 11);
1324 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1006, data);
1325
1326 /*
1327 * Set EQ and TX launch amplitudes as follows
1328 * LANE0.TX_OVRD_DRV_LO.PREEMPH set to 22
1329 * LANE0.TX_OVRD_DRV_LO.AMPLITUDE set to 127
1330 * LANE0.TX_OVRD_DRV_LO.EN set to 1.
1331 */
1332 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1002);
1333 data &= ~0x3F80;
1334 data |= (0x16 << 7);
1335 data &= ~0x7F;
1336 data |= (0x7F | (1 << 14));
1337 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1002, data);
1338
1339 /* Set LOS_BIAS to 0x5 */
1340 dwc3_msm_write_readback(msm->base, SS_PHY_PARAM_CTRL_1, 0x07, 0x5);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301341}
1342
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301343static void dwc3_msm_block_reset(bool core_reset)
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301344{
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301345
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301346 struct dwc3_msm *mdwc = context;
1347 int ret = 0;
1348
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301349 if (core_reset) {
1350 ret = dwc3_msm_link_clk_reset(1);
1351 if (ret)
1352 return;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301353
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301354 usleep_range(1000, 1200);
1355 ret = dwc3_msm_link_clk_reset(0);
1356 if (ret)
1357 return;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301358
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301359 usleep_range(10000, 12000);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301360
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301361 /* Reinitialize QSCRATCH registers after block reset */
1362 dwc3_msm_qscratch_reg_init(mdwc);
1363 }
Manu Gautama302f612012-12-18 17:33:06 +05301364
1365 /* Reset the DBM */
1366 dwc3_msm_dbm_soft_reset(1);
1367 usleep_range(1000, 1200);
1368 dwc3_msm_dbm_soft_reset(0);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301369}
1370
Manu Gautam8c642812012-06-07 10:35:10 +05301371static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1372{
1373 u32 chg_ctrl;
1374
1375 /* Turn off VDP_SRC */
1376 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1377 msleep(20);
1378
1379 /* Before proceeding make sure VDP_SRC is OFF */
1380 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1381 if (chg_ctrl & 0x3F)
1382 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1383 __func__, chg_ctrl);
1384 /*
1385 * Configure DM as current source, DP as current sink
1386 * and enable battery charging comparators.
1387 */
1388 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1389}
1390
Manu Gautama1e331d2013-02-07 14:55:05 +05301391static bool dwc3_chg_det_check_linestate(struct dwc3_msm *mdwc)
1392{
1393 u32 chg_det;
1394 bool ret = false;
1395
1396 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1397 ret = chg_det & (3 << 8);
1398
1399 return ret;
1400}
1401
Manu Gautam8c642812012-06-07 10:35:10 +05301402static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1403{
1404 u32 chg_det;
1405 bool ret = false;
1406
1407 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1408 ret = chg_det & 1;
1409
1410 return ret;
1411}
1412
1413static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1414{
1415 /*
1416 * Configure DP as current source, DM as current sink
1417 * and enable battery charging comparators.
1418 */
1419 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1420}
1421
1422static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1423{
1424 u32 chg_state;
1425 bool ret = false;
1426
1427 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1428 ret = chg_state & 2;
1429
1430 return ret;
1431}
1432
1433static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1434{
1435 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1436}
1437
1438static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1439{
1440 /* Data contact detection enable, DCDENB */
1441 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1442}
1443
1444static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1445{
1446 u32 chg_ctrl;
1447
1448 /* Clear charger detecting control bits */
1449 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1450
1451 /* Clear alt interrupt latch and enable bits */
1452 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1453 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1454
1455 udelay(100);
1456
1457 /* Before proceeding make sure charger block is RESET */
1458 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1459 if (chg_ctrl & 0x3F)
1460 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1461 __func__, chg_ctrl);
1462}
1463
1464static const char *chg_to_string(enum dwc3_chg_type chg_type)
1465{
1466 switch (chg_type) {
Manu Gautama1e331d2013-02-07 14:55:05 +05301467 case DWC3_SDP_CHARGER: return "USB_SDP_CHARGER";
1468 case DWC3_DCP_CHARGER: return "USB_DCP_CHARGER";
1469 case DWC3_CDP_CHARGER: return "USB_CDP_CHARGER";
1470 case DWC3_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Manu Gautam8c642812012-06-07 10:35:10 +05301471 default: return "INVALID_CHARGER";
1472 }
1473}
1474
1475#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1476#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1477#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1478#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1479
1480static void dwc3_chg_detect_work(struct work_struct *w)
1481{
1482 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1483 bool is_dcd = false, tmout, vout;
1484 unsigned long delay;
1485
1486 dev_dbg(mdwc->dev, "chg detection work\n");
1487 switch (mdwc->chg_state) {
1488 case USB_CHG_STATE_UNDEFINED:
1489 dwc3_chg_block_reset(mdwc);
1490 dwc3_chg_enable_dcd(mdwc);
1491 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1492 mdwc->dcd_retries = 0;
1493 delay = DWC3_CHG_DCD_POLL_TIME;
1494 break;
1495 case USB_CHG_STATE_WAIT_FOR_DCD:
1496 is_dcd = dwc3_chg_check_dcd(mdwc);
1497 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1498 if (is_dcd || tmout) {
1499 dwc3_chg_disable_dcd(mdwc);
Manu Gautama1e331d2013-02-07 14:55:05 +05301500 if (dwc3_chg_det_check_linestate(mdwc)) {
1501 dev_dbg(mdwc->dev, "proprietary charger\n");
1502 mdwc->charger.chg_type =
1503 DWC3_PROPRIETARY_CHARGER;
1504 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1505 delay = 0;
1506 break;
1507 }
Manu Gautam8c642812012-06-07 10:35:10 +05301508 dwc3_chg_enable_primary_det(mdwc);
1509 delay = DWC3_CHG_PRIMARY_DET_TIME;
1510 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1511 } else {
1512 delay = DWC3_CHG_DCD_POLL_TIME;
1513 }
1514 break;
1515 case USB_CHG_STATE_DCD_DONE:
1516 vout = dwc3_chg_det_check_output(mdwc);
1517 if (vout) {
1518 dwc3_chg_enable_secondary_det(mdwc);
1519 delay = DWC3_CHG_SECONDARY_DET_TIME;
1520 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1521 } else {
Manu Gautama1e331d2013-02-07 14:55:05 +05301522 mdwc->charger.chg_type = DWC3_SDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301523 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1524 delay = 0;
1525 }
1526 break;
1527 case USB_CHG_STATE_PRIMARY_DONE:
1528 vout = dwc3_chg_det_check_output(mdwc);
1529 if (vout)
Manu Gautama1e331d2013-02-07 14:55:05 +05301530 mdwc->charger.chg_type = DWC3_DCP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301531 else
Manu Gautama1e331d2013-02-07 14:55:05 +05301532 mdwc->charger.chg_type = DWC3_CDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301533 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1534 /* fall through */
1535 case USB_CHG_STATE_SECONDARY_DONE:
1536 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1537 /* fall through */
1538 case USB_CHG_STATE_DETECTED:
1539 dwc3_chg_block_reset(mdwc);
Manu Gautama48296e2012-12-05 17:37:56 +05301540 /* Enable VDP_SRC */
1541 if (mdwc->charger.chg_type == DWC3_DCP_CHARGER)
1542 dwc3_msm_write_readback(mdwc->base,
1543 CHARGING_DET_CTRL_REG, 0x1F, 0x10);
Manu Gautam8c642812012-06-07 10:35:10 +05301544 dev_dbg(mdwc->dev, "chg_type = %s\n",
1545 chg_to_string(mdwc->charger.chg_type));
1546 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1547 &mdwc->charger);
1548 return;
1549 default:
1550 return;
1551 }
1552
1553 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1554}
1555
1556static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1557{
1558 struct dwc3_msm *mdwc = context;
1559
1560 if (start == false) {
Jack Pham9354c6a2012-12-20 19:19:32 -08001561 dev_dbg(mdwc->dev, "canceling charging detection work\n");
Manu Gautam8c642812012-06-07 10:35:10 +05301562 cancel_delayed_work_sync(&mdwc->chg_work);
1563 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1564 charger->chg_type = DWC3_INVALID_CHARGER;
1565 return;
1566 }
1567
1568 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1569 charger->chg_type = DWC3_INVALID_CHARGER;
1570 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1571}
1572
Manu Gautamb5067272012-07-02 09:53:41 +05301573static int dwc3_msm_suspend(struct dwc3_msm *mdwc)
1574{
Manu Gautam2617deb2012-08-31 17:50:06 -07001575 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301576 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301577 bool host_bus_suspend;
Manu Gautam2617deb2012-08-31 17:50:06 -07001578
Manu Gautamb5067272012-07-02 09:53:41 +05301579 dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__);
1580
1581 if (atomic_read(&mdwc->in_lpm)) {
1582 dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__);
1583 return 0;
1584 }
1585
Manu Gautama48296e2012-12-05 17:37:56 +05301586 if (mdwc->hs_phy_irq)
1587 disable_irq(mdwc->hs_phy_irq);
1588
Manu Gautam98013c22012-11-20 17:42:42 +05301589 if (cancel_delayed_work_sync(&mdwc->chg_work))
1590 dev_dbg(mdwc->dev, "%s: chg_work was pending\n", __func__);
1591 if (mdwc->chg_state != USB_CHG_STATE_DETECTED) {
1592 /* charger detection wasn't complete; re-init flags */
1593 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1594 mdwc->charger.chg_type = DWC3_INVALID_CHARGER;
Manu Gautama48296e2012-12-05 17:37:56 +05301595 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG,
1596 0x37, 0x0);
Manu Gautam98013c22012-11-20 17:42:42 +05301597 }
1598
Manu Gautama48296e2012-12-05 17:37:56 +05301599 dcp = mdwc->charger.chg_type == DWC3_DCP_CHARGER;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301600 host_bus_suspend = mdwc->host_mode == 1;
Manu Gautam377821c2012-09-28 16:53:24 +05301601
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301602 /* Sequence to put SSPHY in low power state:
1603 * 1. Clear REF_SS_PHY_EN in SS_PHY_CTRL_REG
1604 * 2. Clear REF_USE_PAD in SS_PHY_CTRL_REG
1605 * 3. Set TEST_POWERED_DOWN in SS_PHY_CTRL_REG to enable PHY retention
1606 * 4. Disable SSPHY ref clk
1607 */
1608 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8), 0x0);
1609 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28), 0x0);
1610 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26),
1611 (1 << 26));
1612
Manu Gautam377821c2012-09-28 16:53:24 +05301613 usleep_range(1000, 1200);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001614 clk_disable_unprepare(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301615
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301616 if (host_bus_suspend) {
1617 /* Sequence for host bus suspend case:
1618 * 1. Set suspend and sleep bits in GUSB2PHYCONFIG reg
1619 * 2. Clear interrupt latch register and enable BSV, ID HV intr
1620 * 3. Enable DP and DM HV interrupts in ALT_INTERRUPT_EN_REG
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301621 */
1622 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1623 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1624 0x00000140);
1625 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1626 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1627 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1628 0x18000, 0x18000);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301629 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0xFC0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301630 udelay(5);
1631 } else {
1632 /* Sequence to put hardware in low power state:
1633 * 1. Set OTGDISABLE to disable OTG block in HSPHY (saves power)
1634 * 2. Clear charger detection control fields (performed above)
1635 * 3. SUSPEND PHY and turn OFF core clock after some delay
1636 * 4. Clear interrupt latch register and enable BSV, ID HV intr
1637 * 5. Enable PHY retention
1638 */
1639 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x1000,
1640 0x1000);
1641 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1642 0xC00000, 0x800000);
1643 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1644 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1645 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1646 0x18000, 0x18000);
1647 if (!dcp)
1648 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1649 0x2, 0x0);
1650 }
Manu Gautam377821c2012-09-28 16:53:24 +05301651
1652 /* make sure above writes are completed before turning off clocks */
1653 wmb();
1654 clk_disable_unprepare(mdwc->core_clk);
1655 clk_disable_unprepare(mdwc->iface_clk);
1656
Jack Pham22698b82013-02-13 17:45:06 -08001657 if (!host_bus_suspend) {
1658 clk_disable_unprepare(mdwc->utmi_clk);
1659
1660 /* USB PHY no more requires TCXO */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301661 clk_disable_unprepare(mdwc->xo_clk);
Jack Pham22698b82013-02-13 17:45:06 -08001662 }
Manu Gautamb5067272012-07-02 09:53:41 +05301663
Manu Gautam2617deb2012-08-31 17:50:06 -07001664 if (mdwc->bus_perf_client) {
1665 ret = msm_bus_scale_client_update_request(
1666 mdwc->bus_perf_client, 0);
1667 if (ret)
1668 dev_err(mdwc->dev, "Failed to reset bus bw vote\n");
1669 }
1670
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301671 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1672 !host_bus_suspend)
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301673 dwc3_hsusb_ldo_enable(0);
1674
Vijayavardhan Vennapusa6bc06962012-10-31 13:23:38 +05301675 dwc3_ssusb_ldo_enable(0);
1676 dwc3_ssusb_config_vddcx(0);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301677 if (!host_bus_suspend)
1678 dwc3_hsusb_config_vddcx(0);
Manu Gautam377821c2012-09-28 16:53:24 +05301679 wake_unlock(&mdwc->wlock);
Manu Gautamb5067272012-07-02 09:53:41 +05301680 atomic_set(&mdwc->in_lpm, 1);
Manu Gautam377821c2012-09-28 16:53:24 +05301681
Manu Gautamb5067272012-07-02 09:53:41 +05301682 dev_info(mdwc->dev, "DWC3 in low power mode\n");
1683
Manu Gautama48296e2012-12-05 17:37:56 +05301684 if (mdwc->hs_phy_irq)
1685 enable_irq(mdwc->hs_phy_irq);
1686
Manu Gautamb5067272012-07-02 09:53:41 +05301687 return 0;
1688}
1689
1690static int dwc3_msm_resume(struct dwc3_msm *mdwc)
1691{
Manu Gautam2617deb2012-08-31 17:50:06 -07001692 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301693 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301694 bool host_bus_suspend;
Manu Gautam2617deb2012-08-31 17:50:06 -07001695
Manu Gautamb5067272012-07-02 09:53:41 +05301696 dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__);
1697
1698 if (!atomic_read(&mdwc->in_lpm)) {
1699 dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__);
1700 return 0;
1701 }
1702
Manu Gautam377821c2012-09-28 16:53:24 +05301703 wake_lock(&mdwc->wlock);
1704
Manu Gautam2617deb2012-08-31 17:50:06 -07001705 if (mdwc->bus_perf_client) {
1706 ret = msm_bus_scale_client_update_request(
1707 mdwc->bus_perf_client, 1);
1708 if (ret)
1709 dev_err(mdwc->dev, "Failed to vote for bus scaling\n");
1710 }
1711
Manu Gautama48296e2012-12-05 17:37:56 +05301712 dcp = mdwc->charger.chg_type == DWC3_DCP_CHARGER;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301713 host_bus_suspend = mdwc->host_mode == 1;
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301714
1715 if (!host_bus_suspend) {
1716 /* Vote for TCXO while waking up USB HSPHY */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301717 ret = clk_prepare_enable(mdwc->xo_clk);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301718 if (ret)
1719 dev_err(mdwc->dev, "%s failed to vote TCXO buffer%d\n",
1720 __func__, ret);
1721 }
1722
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301723 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1724 !host_bus_suspend)
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301725 dwc3_hsusb_ldo_enable(1);
1726
Vijayavardhan Vennapusa6bc06962012-10-31 13:23:38 +05301727 dwc3_ssusb_ldo_enable(1);
1728 dwc3_ssusb_config_vddcx(1);
Jack Pham22698b82013-02-13 17:45:06 -08001729
1730 if (!host_bus_suspend) {
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301731 dwc3_hsusb_config_vddcx(1);
Jack Pham22698b82013-02-13 17:45:06 -08001732 clk_prepare_enable(mdwc->utmi_clk);
1733 }
1734
Manu Gautam3e9ad352012-08-16 14:44:47 -07001735 clk_prepare_enable(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301736 usleep_range(1000, 1200);
1737
Manu Gautam3e9ad352012-08-16 14:44:47 -07001738 clk_prepare_enable(mdwc->iface_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301739 clk_prepare_enable(mdwc->core_clk);
1740
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301741 if (host_bus_suspend) {
1742 /* Disable HV interrupt */
1743 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1744 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1745 0x18000, 0x0);
1746 /* Clear interrupt latch register */
1747 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301748
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301749 /* Disable DP and DM HV interrupt */
1750 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301751
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301752 /* Clear suspend bit in GUSB2PHYCONFIG register */
1753 dwc3_msm_write_readback(mdwc->base, DWC3_GUSB2PHYCFG(0),
1754 0x40, 0x0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301755 } else {
1756 /* Disable HV interrupt */
1757 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1758 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1759 0x18000, 0x0);
1760 /* Disable Retention */
1761 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x2, 0x2);
1762
1763 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1764 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1765 0xF0000000);
1766 /* 10usec delay required before de-asserting PHY RESET */
1767 udelay(10);
1768 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1769 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) &
1770 0x7FFFFFFF);
1771
1772 /* Bring PHY out of suspend */
1773 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0xC00000,
1774 0x0);
1775
1776 }
Manu Gautamb5067272012-07-02 09:53:41 +05301777
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301778 /* Assert SS PHY RESET */
1779 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7),
1780 (1 << 7));
1781 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28),
1782 (1 << 28));
1783 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8),
1784 (1 << 8));
1785 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26), 0x0);
1786 /* 10usec delay required before de-asserting SS PHY RESET */
1787 udelay(10);
1788 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7), 0x0);
1789
Manu Gautamb5067272012-07-02 09:53:41 +05301790 atomic_set(&mdwc->in_lpm, 0);
Manu Gautam377821c2012-09-28 16:53:24 +05301791
1792 /* match disable_irq call from isr */
1793 if (mdwc->lpm_irq_seen && mdwc->hs_phy_irq) {
1794 enable_irq(mdwc->hs_phy_irq);
1795 mdwc->lpm_irq_seen = false;
1796 }
1797
Manu Gautamb5067272012-07-02 09:53:41 +05301798 dev_info(mdwc->dev, "DWC3 exited from low power mode\n");
1799
1800 return 0;
1801}
1802
1803static void dwc3_resume_work(struct work_struct *w)
1804{
1805 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1806 resume_work.work);
1807
1808 dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__);
1809 /* handle any event that was queued while work was already running */
1810 if (!atomic_read(&mdwc->in_lpm)) {
1811 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1812 if (mdwc->otg_xceiv)
1813 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1814 DWC3_EVENT_XCEIV_STATE);
1815 return;
1816 }
1817
1818 /* bail out if system resume in process, else initiate RESUME */
1819 if (atomic_read(&mdwc->pm_suspended)) {
1820 mdwc->resume_pending = true;
1821 } else {
1822 pm_runtime_get_sync(mdwc->dev);
1823 if (mdwc->otg_xceiv)
1824 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1825 DWC3_EVENT_PHY_RESUME);
1826 pm_runtime_put_sync(mdwc->dev);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301827 if (mdwc->otg_xceiv && (mdwc->ext_xceiv.otg_capability))
1828 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1829 DWC3_EVENT_XCEIV_STATE);
Manu Gautamb5067272012-07-02 09:53:41 +05301830 }
1831}
1832
Jack Pham0fc12332012-11-19 13:14:22 -08001833static u32 debug_id = true, debug_bsv, debug_connect;
Manu Gautamb5067272012-07-02 09:53:41 +05301834
1835static int dwc3_connect_show(struct seq_file *s, void *unused)
1836{
1837 if (debug_connect)
1838 seq_printf(s, "true\n");
1839 else
1840 seq_printf(s, "false\n");
1841
1842 return 0;
1843}
1844
1845static int dwc3_connect_open(struct inode *inode, struct file *file)
1846{
1847 return single_open(file, dwc3_connect_show, inode->i_private);
1848}
1849
1850static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf,
1851 size_t count, loff_t *ppos)
1852{
1853 struct seq_file *s = file->private_data;
1854 struct dwc3_msm *mdwc = s->private;
1855 char buf[8];
1856
1857 memset(buf, 0x00, sizeof(buf));
1858
1859 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1860 return -EFAULT;
1861
1862 if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) {
1863 debug_connect = true;
1864 } else {
1865 debug_connect = debug_bsv = false;
1866 debug_id = true;
1867 }
1868
1869 mdwc->ext_xceiv.bsv = debug_bsv;
1870 mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND;
1871
1872 if (atomic_read(&mdwc->in_lpm)) {
1873 dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__);
1874 dwc3_resume_work(&mdwc->resume_work.work);
1875 } else {
1876 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1877 if (mdwc->otg_xceiv)
1878 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1879 DWC3_EVENT_XCEIV_STATE);
1880 }
1881
1882 return count;
1883}
1884
1885const struct file_operations dwc3_connect_fops = {
1886 .open = dwc3_connect_open,
1887 .read = seq_read,
1888 .write = dwc3_connect_write,
1889 .llseek = seq_lseek,
1890 .release = single_release,
1891};
1892
1893static struct dentry *dwc3_debugfs_root;
1894
1895static void dwc3_debugfs_init(struct dwc3_msm *mdwc)
1896{
1897 dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL);
1898
1899 if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root))
1900 return;
1901
1902 if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05301903 &debug_id))
Manu Gautamb5067272012-07-02 09:53:41 +05301904 goto error;
1905
1906 if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05301907 &debug_bsv))
Manu Gautamb5067272012-07-02 09:53:41 +05301908 goto error;
1909
1910 if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR,
1911 dwc3_debugfs_root, mdwc, &dwc3_connect_fops))
1912 goto error;
1913
1914 return;
1915
1916error:
1917 debugfs_remove_recursive(dwc3_debugfs_root);
1918}
Manu Gautam8c642812012-06-07 10:35:10 +05301919
Manu Gautam377821c2012-09-28 16:53:24 +05301920static irqreturn_t msm_dwc3_irq(int irq, void *data)
1921{
1922 struct dwc3_msm *mdwc = data;
1923
1924 if (atomic_read(&mdwc->in_lpm)) {
1925 dev_dbg(mdwc->dev, "%s received in LPM\n", __func__);
1926 mdwc->lpm_irq_seen = true;
1927 disable_irq_nosync(irq);
1928 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
1929 } else {
1930 pr_info_ratelimited("%s: IRQ outside LPM\n", __func__);
1931 }
1932
1933 return IRQ_HANDLED;
1934}
1935
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301936static int dwc3_msm_power_get_property_usb(struct power_supply *psy,
1937 enum power_supply_property psp,
1938 union power_supply_propval *val)
1939{
1940 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
1941 usb_psy);
1942 switch (psp) {
1943 case POWER_SUPPLY_PROP_SCOPE:
1944 val->intval = mdwc->host_mode;
1945 break;
1946 case POWER_SUPPLY_PROP_CURRENT_MAX:
1947 val->intval = mdwc->current_max;
1948 break;
1949 case POWER_SUPPLY_PROP_PRESENT:
1950 val->intval = mdwc->vbus_active;
1951 break;
1952 case POWER_SUPPLY_PROP_ONLINE:
1953 val->intval = mdwc->online;
1954 break;
Manu Gautamfa40cae2013-03-01 16:37:12 +05301955 case POWER_SUPPLY_PROP_TYPE:
1956 val->intval = psy->type;
1957 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301958 default:
1959 return -EINVAL;
1960 }
1961 return 0;
1962}
1963
1964static int dwc3_msm_power_set_property_usb(struct power_supply *psy,
1965 enum power_supply_property psp,
1966 const union power_supply_propval *val)
1967{
1968 static bool init;
1969 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
1970 usb_psy);
1971
1972 switch (psp) {
1973 case POWER_SUPPLY_PROP_SCOPE:
1974 mdwc->host_mode = val->intval;
1975 break;
1976 /* Process PMIC notification in PRESENT prop */
1977 case POWER_SUPPLY_PROP_PRESENT:
1978 dev_dbg(mdwc->dev, "%s: notify xceiv event\n", __func__);
Jack Pham9354c6a2012-12-20 19:19:32 -08001979 if (mdwc->otg_xceiv && !mdwc->ext_inuse &&
1980 (mdwc->ext_xceiv.otg_capability || !init)) {
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301981 mdwc->ext_xceiv.bsv = val->intval;
Manu Gautamf71d9cb2013-02-07 13:52:12 +05301982 queue_delayed_work(system_nrt_wq,
Jack Pham4d91aab2013-03-08 10:02:16 -08001983 &mdwc->resume_work, 20);
Jack Pham9354c6a2012-12-20 19:19:32 -08001984
1985 if (!init)
1986 init = true;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301987 }
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301988 mdwc->vbus_active = val->intval;
1989 break;
1990 case POWER_SUPPLY_PROP_ONLINE:
1991 mdwc->online = val->intval;
1992 break;
1993 case POWER_SUPPLY_PROP_CURRENT_MAX:
1994 mdwc->current_max = val->intval;
1995 break;
Manu Gautamfa40cae2013-03-01 16:37:12 +05301996 case POWER_SUPPLY_PROP_TYPE:
1997 psy->type = val->intval;
1998 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301999 default:
2000 return -EINVAL;
2001 }
2002
2003 power_supply_changed(&mdwc->usb_psy);
2004 return 0;
2005}
2006
Jack Pham9354c6a2012-12-20 19:19:32 -08002007static void dwc3_msm_external_power_changed(struct power_supply *psy)
2008{
2009 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm, usb_psy);
2010 union power_supply_propval ret = {0,};
2011
2012 if (!mdwc->ext_vbus_psy)
2013 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2014
2015 if (!mdwc->ext_vbus_psy) {
2016 pr_err("%s: Unable to get ext_vbus power_supply\n", __func__);
2017 return;
2018 }
2019
2020 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2021 POWER_SUPPLY_PROP_ONLINE, &ret);
2022 if (ret.intval) {
2023 dwc3_start_chg_det(&mdwc->charger, false);
2024 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2025 POWER_SUPPLY_PROP_CURRENT_MAX, &ret);
2026 power_supply_set_current_limit(&mdwc->usb_psy, ret.intval);
2027 }
2028
2029 power_supply_set_online(&mdwc->usb_psy, ret.intval);
2030 power_supply_changed(&mdwc->usb_psy);
2031}
2032
2033
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302034static char *dwc3_msm_pm_power_supplied_to[] = {
2035 "battery",
2036};
2037
2038static enum power_supply_property dwc3_msm_pm_power_props_usb[] = {
2039 POWER_SUPPLY_PROP_PRESENT,
2040 POWER_SUPPLY_PROP_ONLINE,
2041 POWER_SUPPLY_PROP_CURRENT_MAX,
2042 POWER_SUPPLY_PROP_SCOPE,
2043};
2044
Jack Phamfadd6432012-12-07 19:03:41 -08002045static void dwc3_init_adc_work(struct work_struct *w);
2046
2047static void dwc3_ext_notify_online(int on)
2048{
2049 struct dwc3_msm *mdwc = context;
Jack Phamf12b7e12012-12-28 14:27:26 -08002050 bool notify_otg = false;
Jack Phamfadd6432012-12-07 19:03:41 -08002051
2052 if (!mdwc) {
2053 pr_err("%s: DWC3 driver already removed\n", __func__);
2054 return;
2055 }
2056
2057 dev_dbg(mdwc->dev, "notify %s%s\n", on ? "" : "dis", "connected");
2058
Jack Pham9354c6a2012-12-20 19:19:32 -08002059 if (!mdwc->ext_vbus_psy)
2060 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2061
2062 mdwc->ext_inuse = on;
Jack Phamf12b7e12012-12-28 14:27:26 -08002063 if (on) {
2064 /* force OTG to exit B-peripheral state */
2065 mdwc->ext_xceiv.bsv = false;
2066 notify_otg = true;
Jack Pham9354c6a2012-12-20 19:19:32 -08002067 dwc3_start_chg_det(&mdwc->charger, false);
Jack Phamf12b7e12012-12-28 14:27:26 -08002068 } else {
2069 /* external client offline; tell OTG about cached ID/BSV */
2070 if (mdwc->ext_xceiv.id != mdwc->id_state) {
2071 mdwc->ext_xceiv.id = mdwc->id_state;
2072 notify_otg = true;
2073 }
2074
2075 mdwc->ext_xceiv.bsv = mdwc->vbus_active;
2076 notify_otg |= mdwc->vbus_active;
2077 }
Jack Pham9354c6a2012-12-20 19:19:32 -08002078
2079 if (mdwc->ext_vbus_psy)
2080 power_supply_set_present(mdwc->ext_vbus_psy, on);
Jack Phamf12b7e12012-12-28 14:27:26 -08002081
2082 if (notify_otg)
2083 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
Jack Phamfadd6432012-12-07 19:03:41 -08002084}
2085
Jack Pham0cca9412013-03-08 13:22:42 -08002086static void dwc3_id_work(struct work_struct *w)
Jack Phamfadd6432012-12-07 19:03:41 -08002087{
Jack Pham0cca9412013-03-08 13:22:42 -08002088 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, id_work);
Jack Phamfadd6432012-12-07 19:03:41 -08002089
Jack Pham0cca9412013-03-08 13:22:42 -08002090 /* Give external client a chance to handle */
2091 if (!mdwc->ext_inuse) {
2092 if (usb_ext) {
2093 int ret = usb_ext->notify(usb_ext->ctxt, mdwc->id_state,
2094 dwc3_ext_notify_online);
2095 dev_dbg(mdwc->dev, "%s: external handler returned %d\n",
2096 __func__, ret);
2097 mdwc->ext_inuse = (ret == 0);
2098 }
2099 }
Jack Phamfadd6432012-12-07 19:03:41 -08002100
Jack Pham0cca9412013-03-08 13:22:42 -08002101 if (!mdwc->ext_inuse) { /* notify OTG */
2102 mdwc->ext_xceiv.id = mdwc->id_state;
2103 dwc3_resume_work(&mdwc->resume_work.work);
2104 }
2105}
2106
2107static irqreturn_t dwc3_pmic_id_irq(int irq, void *data)
2108{
2109 struct dwc3_msm *mdwc = data;
2110
2111 /* If we can't read ID line state for some reason, treat it as float */
2112 mdwc->id_state = !!irq_read_line(irq);
2113 queue_work(system_nrt_wq, &mdwc->id_work);
2114
2115 return IRQ_HANDLED;
Jack Phamfadd6432012-12-07 19:03:41 -08002116}
2117
Jack Pham0fc12332012-11-19 13:14:22 -08002118static void dwc3_adc_notification(enum qpnp_tm_state state, void *ctx)
2119{
2120 struct dwc3_msm *mdwc = ctx;
2121
2122 if (state >= ADC_TM_STATE_NUM) {
2123 pr_err("%s: invalid notification %d\n", __func__, state);
2124 return;
2125 }
2126
2127 dev_dbg(mdwc->dev, "%s: state = %s\n", __func__,
2128 state == ADC_TM_HIGH_STATE ? "high" : "low");
2129
Jack Phamf12b7e12012-12-28 14:27:26 -08002130 /* save ID state, but don't necessarily notify OTG */
Jack Pham0fc12332012-11-19 13:14:22 -08002131 if (state == ADC_TM_HIGH_STATE) {
Jack Phamf12b7e12012-12-28 14:27:26 -08002132 mdwc->id_state = DWC3_ID_FLOAT;
Jack Pham0fc12332012-11-19 13:14:22 -08002133 mdwc->adc_param.state_request = ADC_TM_LOW_THR_ENABLE;
2134 } else {
Jack Phamf12b7e12012-12-28 14:27:26 -08002135 mdwc->id_state = DWC3_ID_GROUND;
Jack Pham0fc12332012-11-19 13:14:22 -08002136 mdwc->adc_param.state_request = ADC_TM_HIGH_THR_ENABLE;
2137 }
2138
Jack Pham0cca9412013-03-08 13:22:42 -08002139 dwc3_id_work(&mdwc->id_work);
2140
Jack Phamfadd6432012-12-07 19:03:41 -08002141 /* re-arm ADC interrupt */
Jack Pham0fc12332012-11-19 13:14:22 -08002142 qpnp_adc_tm_usbid_configure(&mdwc->adc_param);
2143}
2144
2145static void dwc3_init_adc_work(struct work_struct *w)
2146{
2147 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
2148 init_adc_work.work);
2149 int ret;
2150
2151 ret = qpnp_adc_tm_is_ready();
2152 if (ret == -EPROBE_DEFER) {
Jack Pham90b4d122012-12-13 11:46:22 -08002153 queue_delayed_work(system_nrt_wq, to_delayed_work(w),
2154 msecs_to_jiffies(100));
Jack Pham0fc12332012-11-19 13:14:22 -08002155 return;
2156 }
2157
2158 mdwc->adc_param.low_thr = adc_low_threshold;
2159 mdwc->adc_param.high_thr = adc_high_threshold;
2160 mdwc->adc_param.timer_interval = adc_meas_interval;
2161 mdwc->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2162 mdwc->adc_param.usbid_ctx = mdwc;
2163 mdwc->adc_param.threshold_notification = dwc3_adc_notification;
2164
2165 ret = qpnp_adc_tm_usbid_configure(&mdwc->adc_param);
2166 if (ret) {
2167 dev_err(mdwc->dev, "%s: request ADC error %d\n", __func__, ret);
2168 return;
2169 }
2170
2171 mdwc->id_adc_detect = true;
2172}
2173
2174static ssize_t adc_enable_show(struct device *dev,
2175 struct device_attribute *attr, char *buf)
2176{
2177 return snprintf(buf, PAGE_SIZE, "%s\n", context->id_adc_detect ?
2178 "enabled" : "disabled");
2179}
2180
2181static ssize_t adc_enable_store(struct device *dev,
2182 struct device_attribute *attr, const char
2183 *buf, size_t size)
2184{
2185 if (!strnicmp(buf, "enable", 6)) {
2186 if (!context->id_adc_detect)
2187 dwc3_init_adc_work(&context->init_adc_work.work);
2188 return size;
2189 } else if (!strnicmp(buf, "disable", 7)) {
2190 qpnp_adc_tm_usbid_end();
2191 context->id_adc_detect = false;
2192 return size;
2193 }
2194
2195 return -EINVAL;
2196}
2197
2198static DEVICE_ATTR(adc_enable, S_IRUGO | S_IWUSR, adc_enable_show,
2199 adc_enable_store);
2200
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002201static int __devinit dwc3_msm_probe(struct platform_device *pdev)
2202{
2203 struct device_node *node = pdev->dev.of_node;
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302204 struct platform_device *dwc3;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002205 struct dwc3_msm *msm;
2206 struct resource *res;
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002207 void __iomem *tcsr;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002208 int ret = 0;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302209 int len = 0;
2210 u32 tmp[3];
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002211
2212 msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL);
2213 if (!msm) {
2214 dev_err(&pdev->dev, "not enough memory\n");
2215 return -ENOMEM;
2216 }
2217
2218 platform_set_drvdata(pdev, msm);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002219 context = msm;
Manu Gautam60e01352012-05-29 09:00:34 +05302220 msm->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002221
2222 INIT_LIST_HEAD(&msm->req_complete_list);
Manu Gautam8c642812012-06-07 10:35:10 +05302223 INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work);
Manu Gautamb5067272012-07-02 09:53:41 +05302224 INIT_DELAYED_WORK(&msm->resume_work, dwc3_resume_work);
Manu Gautam6eb13e32013-02-01 15:19:15 +05302225 INIT_WORK(&msm->restart_usb_work, dwc3_restart_usb_work);
Jack Pham0cca9412013-03-08 13:22:42 -08002226 INIT_WORK(&msm->id_work, dwc3_id_work);
Jack Pham0fc12332012-11-19 13:14:22 -08002227 INIT_DELAYED_WORK(&msm->init_adc_work, dwc3_init_adc_work);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002228
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302229 msm->xo_clk = clk_get(&pdev->dev, "xo");
2230 if (IS_ERR(msm->xo_clk)) {
Manu Gautam377821c2012-09-28 16:53:24 +05302231 dev_err(&pdev->dev, "%s unable to get TCXO buffer handle\n",
2232 __func__);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302233 return PTR_ERR(msm->xo_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05302234 }
2235
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302236 ret = clk_prepare_enable(msm->xo_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05302237 if (ret) {
2238 dev_err(&pdev->dev, "%s failed to vote for TCXO buffer%d\n",
2239 __func__, ret);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302240 goto put_xo;
Manu Gautam377821c2012-09-28 16:53:24 +05302241 }
2242
Manu Gautam1742db22012-06-19 13:33:24 +05302243 /*
2244 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
2245 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
2246 */
2247 msm->core_clk = devm_clk_get(&pdev->dev, "core_clk");
2248 if (IS_ERR(msm->core_clk)) {
2249 dev_err(&pdev->dev, "failed to get core_clk\n");
Manu Gautam377821c2012-09-28 16:53:24 +05302250 ret = PTR_ERR(msm->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302251 goto disable_xo;
Manu Gautam1742db22012-06-19 13:33:24 +05302252 }
2253 clk_set_rate(msm->core_clk, 125000000);
2254 clk_prepare_enable(msm->core_clk);
2255
Manu Gautam3e9ad352012-08-16 14:44:47 -07002256 msm->iface_clk = devm_clk_get(&pdev->dev, "iface_clk");
2257 if (IS_ERR(msm->iface_clk)) {
2258 dev_err(&pdev->dev, "failed to get iface_clk\n");
2259 ret = PTR_ERR(msm->iface_clk);
2260 goto disable_core_clk;
2261 }
2262 clk_prepare_enable(msm->iface_clk);
2263
2264 msm->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2265 if (IS_ERR(msm->sleep_clk)) {
2266 dev_err(&pdev->dev, "failed to get sleep_clk\n");
2267 ret = PTR_ERR(msm->sleep_clk);
2268 goto disable_iface_clk;
2269 }
2270 clk_prepare_enable(msm->sleep_clk);
2271
2272 msm->hsphy_sleep_clk = devm_clk_get(&pdev->dev, "sleep_a_clk");
2273 if (IS_ERR(msm->hsphy_sleep_clk)) {
2274 dev_err(&pdev->dev, "failed to get sleep_a_clk\n");
2275 ret = PTR_ERR(msm->hsphy_sleep_clk);
2276 goto disable_sleep_clk;
2277 }
2278 clk_prepare_enable(msm->hsphy_sleep_clk);
2279
Jack Pham22698b82013-02-13 17:45:06 -08002280 msm->utmi_clk = devm_clk_get(&pdev->dev, "utmi_clk");
2281 if (IS_ERR(msm->utmi_clk)) {
2282 dev_err(&pdev->dev, "failed to get utmi_clk\n");
2283 ret = PTR_ERR(msm->utmi_clk);
2284 goto disable_sleep_a_clk;
2285 }
2286 clk_prepare_enable(msm->utmi_clk);
2287
Manu Gautam3e9ad352012-08-16 14:44:47 -07002288 msm->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
2289 if (IS_ERR(msm->ref_clk)) {
2290 dev_err(&pdev->dev, "failed to get ref_clk\n");
2291 ret = PTR_ERR(msm->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002292 goto disable_utmi_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -07002293 }
2294 clk_prepare_enable(msm->ref_clk);
2295
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302296 of_get_property(node, "qcom,vdd-voltage-level", &len);
2297 if (len == sizeof(tmp)) {
2298 of_property_read_u32_array(node, "qcom,vdd-voltage-level",
2299 tmp, len/sizeof(*tmp));
2300 msm->vdd_no_vol_level = tmp[0];
2301 msm->vdd_low_vol_level = tmp[1];
2302 msm->vdd_high_vol_level = tmp[2];
2303 } else {
2304 dev_err(&pdev->dev, "no qcom,vdd-voltage-level property\n");
2305 ret = -EINVAL;
2306 goto disable_ref_clk;
2307 }
2308
Manu Gautam60e01352012-05-29 09:00:34 +05302309 /* SS PHY */
Manu Gautam60e01352012-05-29 09:00:34 +05302310 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
2311 if (IS_ERR(msm->ssusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302312 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
2313 ret = PTR_ERR(msm->ssusb_vddcx);
2314 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302315 }
2316
2317 ret = dwc3_ssusb_config_vddcx(1);
2318 if (ret) {
2319 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam3e9ad352012-08-16 14:44:47 -07002320 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302321 }
2322
2323 ret = regulator_enable(context->ssusb_vddcx);
2324 if (ret) {
2325 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
2326 goto unconfig_ss_vddcx;
2327 }
2328
2329 ret = dwc3_ssusb_ldo_init(1);
2330 if (ret) {
2331 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
2332 goto disable_ss_vddcx;
2333 }
2334
2335 ret = dwc3_ssusb_ldo_enable(1);
2336 if (ret) {
2337 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
2338 goto free_ss_ldo_init;
2339 }
2340
2341 /* HS PHY */
Manu Gautam60e01352012-05-29 09:00:34 +05302342 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
2343 if (IS_ERR(msm->hsusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302344 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
2345 ret = PTR_ERR(msm->hsusb_vddcx);
2346 goto disable_ss_ldo;
Manu Gautam60e01352012-05-29 09:00:34 +05302347 }
2348
2349 ret = dwc3_hsusb_config_vddcx(1);
2350 if (ret) {
2351 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2352 goto disable_ss_ldo;
2353 }
2354
2355 ret = regulator_enable(context->hsusb_vddcx);
2356 if (ret) {
2357 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
2358 goto unconfig_hs_vddcx;
2359 }
2360
2361 ret = dwc3_hsusb_ldo_init(1);
2362 if (ret) {
2363 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
2364 goto disable_hs_vddcx;
2365 }
2366
2367 ret = dwc3_hsusb_ldo_enable(1);
2368 if (ret) {
2369 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
2370 goto free_hs_ldo_init;
2371 }
2372
Jack Pham0fc12332012-11-19 13:14:22 -08002373 msm->ext_xceiv.id = DWC3_ID_FLOAT;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302374 msm->ext_xceiv.otg_capability = of_property_read_bool(node,
Manu Gautam6c0ff032012-11-02 14:55:35 +05302375 "qcom,otg-capability");
2376 msm->charger.charging_disabled = of_property_read_bool(node,
2377 "qcom,charging-disabled");
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302378
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302379 /*
2380 * DWC3 has separate IRQ line for OTG events (ID/BSV) and for
2381 * DP and DM linestate transitions during low power mode.
2382 */
2383 msm->hs_phy_irq = platform_get_irq_byname(pdev, "hs_phy_irq");
2384 if (msm->hs_phy_irq < 0) {
2385 dev_dbg(&pdev->dev, "pget_irq for hs_phy_irq failed\n");
2386 msm->hs_phy_irq = 0;
Jack Pham0fc12332012-11-19 13:14:22 -08002387 } else {
Jack Pham56a0a632013-03-08 13:18:42 -08002388 ret = devm_request_irq(&pdev->dev, msm->hs_phy_irq,
2389 msm_dwc3_irq, IRQF_TRIGGER_RISING,
2390 "msm_dwc3", msm);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302391 if (ret) {
2392 dev_err(&pdev->dev, "irqreq HSPHYINT failed\n");
2393 goto disable_hs_ldo;
2394 }
2395 enable_irq_wake(msm->hs_phy_irq);
2396 }
Jack Pham0cca9412013-03-08 13:22:42 -08002397
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302398 if (msm->ext_xceiv.otg_capability) {
Jack Pham0cca9412013-03-08 13:22:42 -08002399 msm->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
2400 if (msm->pmic_id_irq > 0) {
David Keitelad4a0282013-03-19 18:04:27 -07002401 /* check if PMIC ID IRQ is supported */
2402 ret = qpnp_misc_irqs_available(&pdev->dev);
2403
2404 if (ret == -EPROBE_DEFER) {
2405 /* qpnp hasn't probed yet; defer dwc probe */
Jack Pham0cca9412013-03-08 13:22:42 -08002406 goto disable_hs_ldo;
David Keitelad4a0282013-03-19 18:04:27 -07002407 } else if (ret == 0) {
2408 msm->pmic_id_irq = 0;
2409 } else {
2410 ret = devm_request_irq(&pdev->dev,
2411 msm->pmic_id_irq,
2412 dwc3_pmic_id_irq,
2413 IRQF_TRIGGER_RISING |
2414 IRQF_TRIGGER_FALLING,
2415 "dwc3_msm_pmic_id", msm);
2416 if (ret) {
2417 dev_err(&pdev->dev, "irqreq IDINT failed\n");
2418 goto disable_hs_ldo;
2419 }
2420 enable_irq_wake(msm->pmic_id_irq);
Jack Pham0cca9412013-03-08 13:22:42 -08002421 }
David Keitelad4a0282013-03-19 18:04:27 -07002422 }
2423
2424 if (msm->pmic_id_irq <= 0) {
Jack Pham0cca9412013-03-08 13:22:42 -08002425 /* If no PMIC ID IRQ, use ADC for ID pin detection */
2426 queue_work(system_nrt_wq, &msm->init_adc_work.work);
2427 device_create_file(&pdev->dev, &dev_attr_adc_enable);
2428 msm->pmic_id_irq = 0;
2429 }
Manu Gautam377821c2012-09-28 16:53:24 +05302430 }
2431
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002432 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2433 if (!res) {
2434 dev_dbg(&pdev->dev, "missing TCSR memory resource\n");
2435 } else {
2436 tcsr = devm_ioremap_nocache(&pdev->dev, res->start,
2437 resource_size(res));
2438 if (!tcsr) {
2439 dev_dbg(&pdev->dev, "tcsr ioremap failed\n");
2440 } else {
2441 /* Enable USB3 on the primary USB port. */
2442 writel_relaxed(0x1, tcsr);
2443 /*
2444 * Ensure that TCSR write is completed before
2445 * USB registers initialization.
2446 */
2447 mb();
2448 }
2449 }
2450
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002451 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2452 if (!res) {
2453 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302454 ret = -ENODEV;
Jack Pham56a0a632013-03-08 13:18:42 -08002455 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002456 }
2457
2458 msm->base = devm_ioremap_nocache(&pdev->dev, res->start,
2459 resource_size(res));
2460 if (!msm->base) {
2461 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302462 ret = -ENODEV;
Jack Pham56a0a632013-03-08 13:18:42 -08002463 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002464 }
2465
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302466 dwc3 = platform_device_alloc("dwc3", -1);
2467 if (!dwc3) {
2468 dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
2469 ret = -ENODEV;
2470 goto disable_hs_ldo;
2471 }
2472
2473 dwc3->dev.parent = &pdev->dev;
2474 dwc3->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2475 dwc3->dev.dma_mask = &dwc3_msm_dma_mask;
2476 dwc3->dev.dma_parms = pdev->dev.dma_parms;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002477 msm->resource_size = resource_size(res);
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302478 msm->dwc3 = dwc3;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002479
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05302480 if (of_property_read_u32(node, "qcom,dwc-hsphy-init",
2481 &msm->hsphy_init_seq))
2482 dev_dbg(&pdev->dev, "unable to read hsphy init seq\n");
2483 else if (!msm->hsphy_init_seq)
2484 dev_warn(&pdev->dev, "incorrect hsphyinitseq.Using PORvalue\n");
2485
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05302486 dwc3_msm_qscratch_reg_init(msm);
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +05302487
Manu Gautamb5067272012-07-02 09:53:41 +05302488 pm_runtime_set_active(msm->dev);
Manu Gautam377821c2012-09-28 16:53:24 +05302489 pm_runtime_enable(msm->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05302490
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002491 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
2492 &msm->dbm_num_eps)) {
2493 dev_err(&pdev->dev,
2494 "unable to read platform data num of dbm eps\n");
2495 msm->dbm_num_eps = DBM_MAX_EPS;
2496 }
2497
2498 if (msm->dbm_num_eps > DBM_MAX_EPS) {
2499 dev_err(&pdev->dev,
2500 "Driver doesn't support number of DBM EPs. "
2501 "max: %d, dbm_num_eps: %d\n",
2502 DBM_MAX_EPS, msm->dbm_num_eps);
2503 ret = -ENODEV;
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302504 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002505 }
2506
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302507 msm->usb_psy.name = "usb";
2508 msm->usb_psy.type = POWER_SUPPLY_TYPE_USB;
2509 msm->usb_psy.supplied_to = dwc3_msm_pm_power_supplied_to;
2510 msm->usb_psy.num_supplicants = ARRAY_SIZE(
2511 dwc3_msm_pm_power_supplied_to);
2512 msm->usb_psy.properties = dwc3_msm_pm_power_props_usb;
2513 msm->usb_psy.num_properties = ARRAY_SIZE(dwc3_msm_pm_power_props_usb);
2514 msm->usb_psy.get_property = dwc3_msm_power_get_property_usb;
2515 msm->usb_psy.set_property = dwc3_msm_power_set_property_usb;
Jack Pham9354c6a2012-12-20 19:19:32 -08002516 msm->usb_psy.external_power_changed =
2517 dwc3_msm_external_power_changed;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302518
2519 ret = power_supply_register(&pdev->dev, &msm->usb_psy);
2520 if (ret < 0) {
2521 dev_err(&pdev->dev,
2522 "%s:power_supply_register usb failed\n",
2523 __func__);
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302524 goto put_pdev;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302525 }
2526
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302527 ret = platform_device_add_resources(dwc3, pdev->resource,
2528 pdev->num_resources);
2529 if (ret) {
2530 dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
2531 goto put_psupply;
2532 }
2533
2534 ret = platform_device_add(dwc3);
2535 if (ret) {
2536 dev_err(&pdev->dev, "failed to register dwc3 device\n");
2537 goto put_psupply;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002538 }
2539
Manu Gautam2617deb2012-08-31 17:50:06 -07002540 msm->bus_scale_table = msm_bus_cl_get_pdata(pdev);
2541 if (!msm->bus_scale_table) {
2542 dev_err(&pdev->dev, "bus scaling is disabled\n");
2543 } else {
2544 msm->bus_perf_client =
2545 msm_bus_scale_register_client(msm->bus_scale_table);
2546 ret = msm_bus_scale_client_update_request(
2547 msm->bus_perf_client, 1);
2548 if (ret)
2549 dev_err(&pdev->dev, "Failed to vote for bus scaling\n");
2550 }
2551
Manu Gautam8c642812012-06-07 10:35:10 +05302552 msm->otg_xceiv = usb_get_transceiver();
2553 if (msm->otg_xceiv) {
2554 msm->charger.start_detection = dwc3_start_chg_det;
2555 ret = dwc3_set_charger(msm->otg_xceiv->otg, &msm->charger);
2556 if (ret || !msm->charger.notify_detection_complete) {
2557 dev_err(&pdev->dev, "failed to register charger: %d\n",
2558 ret);
2559 goto put_xcvr;
2560 }
Manu Gautamb5067272012-07-02 09:53:41 +05302561
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05302562 if (msm->ext_xceiv.otg_capability)
2563 msm->ext_xceiv.ext_block_reset = dwc3_msm_block_reset;
Manu Gautamb5067272012-07-02 09:53:41 +05302564 ret = dwc3_set_ext_xceiv(msm->otg_xceiv->otg, &msm->ext_xceiv);
2565 if (ret || !msm->ext_xceiv.notify_ext_events) {
2566 dev_err(&pdev->dev, "failed to register xceiver: %d\n",
2567 ret);
2568 goto put_xcvr;
2569 }
Manu Gautam8c642812012-06-07 10:35:10 +05302570 } else {
2571 dev_err(&pdev->dev, "%s: No OTG transceiver found\n", __func__);
2572 }
2573
Manu Gautamb5067272012-07-02 09:53:41 +05302574 wake_lock_init(&msm->wlock, WAKE_LOCK_SUSPEND, "msm_dwc3");
2575 wake_lock(&msm->wlock);
2576 dwc3_debugfs_init(msm);
2577
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002578 return 0;
2579
Manu Gautam8c642812012-06-07 10:35:10 +05302580put_xcvr:
2581 usb_put_transceiver(msm->otg_xceiv);
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302582 platform_device_del(dwc3);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302583put_psupply:
2584 power_supply_unregister(&msm->usb_psy);
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302585put_pdev:
2586 platform_device_put(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05302587disable_hs_ldo:
2588 dwc3_hsusb_ldo_enable(0);
2589free_hs_ldo_init:
2590 dwc3_hsusb_ldo_init(0);
2591disable_hs_vddcx:
2592 regulator_disable(context->hsusb_vddcx);
2593unconfig_hs_vddcx:
2594 dwc3_hsusb_config_vddcx(0);
2595disable_ss_ldo:
2596 dwc3_ssusb_ldo_enable(0);
2597free_ss_ldo_init:
2598 dwc3_ssusb_ldo_init(0);
2599disable_ss_vddcx:
2600 regulator_disable(context->ssusb_vddcx);
2601unconfig_ss_vddcx:
2602 dwc3_ssusb_config_vddcx(0);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002603disable_ref_clk:
2604 clk_disable_unprepare(msm->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002605disable_utmi_clk:
2606 clk_disable_unprepare(msm->utmi_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002607disable_sleep_a_clk:
2608 clk_disable_unprepare(msm->hsphy_sleep_clk);
2609disable_sleep_clk:
2610 clk_disable_unprepare(msm->sleep_clk);
2611disable_iface_clk:
2612 clk_disable_unprepare(msm->iface_clk);
Manu Gautam1742db22012-06-19 13:33:24 +05302613disable_core_clk:
2614 clk_disable_unprepare(msm->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302615disable_xo:
2616 clk_disable_unprepare(msm->xo_clk);
2617put_xo:
2618 clk_put(msm->xo_clk);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002619
2620 return ret;
2621}
2622
2623static int __devexit dwc3_msm_remove(struct platform_device *pdev)
2624{
2625 struct dwc3_msm *msm = platform_get_drvdata(pdev);
2626
Jack Pham0fc12332012-11-19 13:14:22 -08002627 if (msm->id_adc_detect)
2628 qpnp_adc_tm_usbid_end();
Manu Gautamb5067272012-07-02 09:53:41 +05302629 if (dwc3_debugfs_root)
2630 debugfs_remove_recursive(dwc3_debugfs_root);
Manu Gautam8c642812012-06-07 10:35:10 +05302631 if (msm->otg_xceiv) {
2632 dwc3_start_chg_det(&msm->charger, false);
2633 usb_put_transceiver(msm->otg_xceiv);
2634 }
Jack Pham0fc12332012-11-19 13:14:22 -08002635
Manu Gautamb5067272012-07-02 09:53:41 +05302636 pm_runtime_disable(msm->dev);
Vijayavardhan Vennapusab48a4e82013-03-25 09:00:21 +05302637 platform_device_unregister(msm->dwc3);
Manu Gautamb5067272012-07-02 09:53:41 +05302638 wake_lock_destroy(&msm->wlock);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002639
Manu Gautam60e01352012-05-29 09:00:34 +05302640 dwc3_hsusb_ldo_enable(0);
2641 dwc3_hsusb_ldo_init(0);
2642 regulator_disable(msm->hsusb_vddcx);
2643 dwc3_hsusb_config_vddcx(0);
2644 dwc3_ssusb_ldo_enable(0);
2645 dwc3_ssusb_ldo_init(0);
2646 regulator_disable(msm->ssusb_vddcx);
2647 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05302648 clk_disable_unprepare(msm->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002649 clk_disable_unprepare(msm->iface_clk);
2650 clk_disable_unprepare(msm->sleep_clk);
2651 clk_disable_unprepare(msm->hsphy_sleep_clk);
2652 clk_disable_unprepare(msm->ref_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302653 clk_disable_unprepare(msm->xo_clk);
2654 clk_put(msm->xo_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05302655
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002656 return 0;
2657}
2658
Manu Gautamb5067272012-07-02 09:53:41 +05302659static int dwc3_msm_pm_suspend(struct device *dev)
2660{
2661 int ret = 0;
2662 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2663
2664 dev_dbg(dev, "dwc3-msm PM suspend\n");
2665
Manu Gautam8d98a572013-01-21 16:34:50 +05302666 flush_delayed_work_sync(&mdwc->resume_work);
2667 if (!atomic_read(&mdwc->in_lpm)) {
2668 dev_err(mdwc->dev, "Abort PM suspend!! (USB is outside LPM)\n");
2669 return -EBUSY;
2670 }
2671
Manu Gautamb5067272012-07-02 09:53:41 +05302672 ret = dwc3_msm_suspend(mdwc);
2673 if (!ret)
2674 atomic_set(&mdwc->pm_suspended, 1);
2675
2676 return ret;
2677}
2678
2679static int dwc3_msm_pm_resume(struct device *dev)
2680{
2681 int ret = 0;
2682 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2683
2684 dev_dbg(dev, "dwc3-msm PM resume\n");
2685
2686 atomic_set(&mdwc->pm_suspended, 0);
2687 if (mdwc->resume_pending) {
2688 mdwc->resume_pending = false;
2689
2690 ret = dwc3_msm_resume(mdwc);
2691 /* Update runtime PM status */
2692 pm_runtime_disable(dev);
2693 pm_runtime_set_active(dev);
2694 pm_runtime_enable(dev);
2695
2696 /* Let OTG know about resume event and update pm_count */
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302697 if (mdwc->otg_xceiv) {
Manu Gautamb5067272012-07-02 09:53:41 +05302698 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
2699 DWC3_EVENT_PHY_RESUME);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302700 if (mdwc->ext_xceiv.otg_capability)
2701 mdwc->ext_xceiv.notify_ext_events(
2702 mdwc->otg_xceiv->otg,
2703 DWC3_EVENT_XCEIV_STATE);
2704 }
Manu Gautamb5067272012-07-02 09:53:41 +05302705 }
2706
2707 return ret;
2708}
2709
2710static int dwc3_msm_runtime_idle(struct device *dev)
2711{
2712 dev_dbg(dev, "DWC3-msm runtime idle\n");
2713
2714 return 0;
2715}
2716
2717static int dwc3_msm_runtime_suspend(struct device *dev)
2718{
2719 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2720
2721 dev_dbg(dev, "DWC3-msm runtime suspend\n");
2722
2723 return dwc3_msm_suspend(mdwc);
2724}
2725
2726static int dwc3_msm_runtime_resume(struct device *dev)
2727{
2728 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2729
2730 dev_dbg(dev, "DWC3-msm runtime resume\n");
2731
2732 return dwc3_msm_resume(mdwc);
2733}
2734
2735static const struct dev_pm_ops dwc3_msm_dev_pm_ops = {
2736 SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume)
2737 SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume,
2738 dwc3_msm_runtime_idle)
2739};
2740
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002741static const struct of_device_id of_dwc3_matach[] = {
2742 {
2743 .compatible = "qcom,dwc-usb3-msm",
2744 },
2745 { },
2746};
2747MODULE_DEVICE_TABLE(of, of_dwc3_matach);
2748
2749static struct platform_driver dwc3_msm_driver = {
2750 .probe = dwc3_msm_probe,
2751 .remove = __devexit_p(dwc3_msm_remove),
2752 .driver = {
2753 .name = "msm-dwc3",
Manu Gautamb5067272012-07-02 09:53:41 +05302754 .pm = &dwc3_msm_dev_pm_ops,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002755 .of_match_table = of_dwc3_matach,
2756 },
2757};
2758
Manu Gautam377821c2012-09-28 16:53:24 +05302759MODULE_LICENSE("GPL v2");
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002760MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
2761
2762static int __devinit dwc3_msm_init(void)
2763{
2764 return platform_driver_register(&dwc3_msm_driver);
2765}
2766module_init(dwc3_msm_init);
2767
2768static void __exit dwc3_msm_exit(void)
2769{
2770 platform_driver_unregister(&dwc3_msm_driver);
2771}
2772module_exit(dwc3_msm_exit);