blob: 882aa7fc701cdb05eefbbbdaebb0335f46cb3c04 [file] [log] [blame]
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
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>
20#include <linux/interrupt.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020021#include <linux/ioport.h>
Manu Gautam1742db22012-06-19 13:33:24 +053022#include <linux/clk.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020023#include <linux/io.h>
24#include <linux/module.h>
25#include <linux/types.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020026#include <linux/delay.h>
27#include <linux/of.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030028#include <linux/list.h>
Manu Gautamb5067272012-07-02 09:53:41 +053029#include <linux/debugfs.h>
30#include <linux/uaccess.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030031#include <linux/usb/ch9.h>
32#include <linux/usb/gadget.h>
33#include <linux/usb/msm_hsusb.h>
Manu Gautam60e01352012-05-29 09:00:34 +053034#include <linux/regulator/consumer.h>
35
36#include <mach/rpm-regulator.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030037
Manu Gautam8c642812012-06-07 10:35:10 +053038#include "dwc3_otg.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030039#include "core.h"
40#include "gadget.h"
41
42/**
43 * USB DBM Hardware registers.
44 *
45 */
Shimrit Malichia00d7322012-08-05 13:56:28 +030046#define DBM_BASE 0x000F8000
47#define DBM_EP_CFG(n) (DBM_BASE + (0x00 + 4 * (n)))
48#define DBM_DATA_FIFO(n) (DBM_BASE + (0x10 + 4 * (n)))
49#define DBM_DATA_FIFO_SIZE(n) (DBM_BASE + (0x20 + 4 * (n)))
50#define DBM_DATA_FIFO_EN (DBM_BASE + (0x30))
51#define DBM_GEVNTADR (DBM_BASE + (0x34))
52#define DBM_GEVNTSIZ (DBM_BASE + (0x38))
53#define DBM_DBG_CNFG (DBM_BASE + (0x3C))
54#define DBM_HW_TRB0_EP(n) (DBM_BASE + (0x40 + 4 * (n)))
55#define DBM_HW_TRB1_EP(n) (DBM_BASE + (0x50 + 4 * (n)))
56#define DBM_HW_TRB2_EP(n) (DBM_BASE + (0x60 + 4 * (n)))
57#define DBM_HW_TRB3_EP(n) (DBM_BASE + (0x70 + 4 * (n)))
58#define DBM_PIPE_CFG (DBM_BASE + (0x80))
59#define DBM_SOFT_RESET (DBM_BASE + (0x84))
60#define DBM_GEN_CFG (DBM_BASE + (0x88))
Ido Shayevitz9fb83452012-04-01 17:45:58 +030061
62/**
63 * USB DBM Hardware registers bitmask.
64 *
65 */
66/* DBM_EP_CFG */
Shimrit Malichia00d7322012-08-05 13:56:28 +030067#define DBM_EN_EP 0x00000001
68#define USB3_EPNUM 0x0000003E
Ido Shayevitz9fb83452012-04-01 17:45:58 +030069#define DBM_BAM_PIPE_NUM 0x000000C0
70#define DBM_PRODUCER 0x00000100
71#define DBM_DISABLE_WB 0x00000200
72#define DBM_INT_RAM_ACC 0x00000400
73
74/* DBM_DATA_FIFO_SIZE */
75#define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff
76
77/* DBM_GEVNTSIZ */
78#define DBM_GEVNTSIZ_MASK 0x0000ffff
79
80/* DBM_DBG_CNFG */
81#define DBM_ENABLE_IOC_MASK 0x0000000f
82
83/* DBM_SOFT_RESET */
84#define DBM_SFT_RST_EP0 0x00000001
85#define DBM_SFT_RST_EP1 0x00000002
86#define DBM_SFT_RST_EP2 0x00000004
87#define DBM_SFT_RST_EP3 0x00000008
Shimrit Malichia00d7322012-08-05 13:56:28 +030088#define DBM_SFT_RST_EPS_MASK 0x0000000F
89#define DBM_SFT_RST_MASK 0x80000000
90#define DBM_EN_MASK 0x00000002
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020091
92#define DBM_MAX_EPS 4
93
Ido Shayevitzfa65a582012-06-06 14:39:54 +030094/* DBM TRB configurations */
95#define DBM_TRB_BIT 0x80000000
96#define DBM_TRB_DATA_SRC 0x40000000
97#define DBM_TRB_DMA 0x20000000
98#define DBM_TRB_EP_NUM(ep) (ep<<24)
Shimrit Malichia00d7322012-08-05 13:56:28 +030099
Manu Gautam8c642812012-06-07 10:35:10 +0530100/**
101 * USB QSCRATCH Hardware registers
102 *
103 */
104#define QSCRATCH_REG_OFFSET (0x000F8800)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300105#define QSCRATCH_GENERAL_CFG (QSCRATCH_REG_OFFSET + 0x08)
Manu Gautam8c642812012-06-07 10:35:10 +0530106#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
107#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
108#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
109#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
110
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300111struct dwc3_msm_req_complete {
112 struct list_head list_item;
113 struct usb_request *req;
114 void (*orig_complete)(struct usb_ep *ep,
115 struct usb_request *req);
116};
117
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200118struct dwc3_msm {
119 struct platform_device *dwc3;
120 struct device *dev;
121 void __iomem *base;
122 u32 resource_size;
123 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300124 u8 ep_num_mapping[DBM_MAX_EPS];
125 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
126 struct list_head req_complete_list;
Manu Gautam1742db22012-06-19 13:33:24 +0530127 struct clk *core_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530128 struct regulator *hsusb_3p3;
129 struct regulator *hsusb_1p8;
130 struct regulator *hsusb_vddcx;
131 struct regulator *ssusb_1p8;
132 struct regulator *ssusb_vddcx;
133 enum usb_vdd_type ss_vdd_type;
134 enum usb_vdd_type hs_vdd_type;
Manu Gautamb5067272012-07-02 09:53:41 +0530135 struct dwc3_ext_xceiv ext_xceiv;
136 bool resume_pending;
137 atomic_t pm_suspended;
138 atomic_t in_lpm;
139 struct delayed_work resume_work;
140 struct wake_lock wlock;
Manu Gautam8c642812012-06-07 10:35:10 +0530141 struct dwc3_charger charger;
142 struct usb_phy *otg_xceiv;
143 struct delayed_work chg_work;
144 enum usb_chg_state chg_state;
145 u8 dcd_retries;
Manu Gautam60e01352012-05-29 09:00:34 +0530146};
147
148#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
149#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
150#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
151
152#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
153#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
154#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
155
156#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
157#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
158#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
159
160#define USB_PHY_VDD_DIG_VOL_NONE 0 /* uV */
161#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
162#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
163
Manu Gautam60e01352012-05-29 09:00:34 +0530164static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
165 { /* VDD_CX CORNER Voting */
166 [VDD_NONE] = RPM_VREG_CORNER_NONE,
167 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
168 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
169 },
170 { /* VDD_CX Voltage Voting */
171 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
172 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
173 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
174 },
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200175};
176
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300177static struct dwc3_msm *context;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +0300178static u64 dwc3_msm_dma_mask = DMA_BIT_MASK(64);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300179
180/**
181 *
182 * Read register with debug info.
183 *
184 * @base - DWC3 base virtual address.
185 * @offset - register offset.
186 *
187 * @return u32
188 */
189static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
190{
191 u32 val = ioread32(base + offset);
192 return val;
193}
194
195/**
196 * Read register masked field with debug info.
197 *
198 * @base - DWC3 base virtual address.
199 * @offset - register offset.
200 * @mask - register bitmask.
201 *
202 * @return u32
203 */
204static inline u32 dwc3_msm_read_reg_field(void *base,
205 u32 offset,
206 const u32 mask)
207{
208 u32 shift = find_first_bit((void *)&mask, 32);
209 u32 val = ioread32(base + offset);
210 val &= mask; /* clear other bits */
211 val >>= shift;
212 return val;
213}
214
215/**
216 *
217 * Write register with debug info.
218 *
219 * @base - DWC3 base virtual address.
220 * @offset - register offset.
221 * @val - value to write.
222 *
223 */
224static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
225{
226 iowrite32(val, base + offset);
227}
228
229/**
230 * Write register masked field with debug info.
231 *
232 * @base - DWC3 base virtual address.
233 * @offset - register offset.
234 * @mask - register bitmask.
235 * @val - value to write.
236 *
237 */
238static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
239 const u32 mask, u32 val)
240{
241 u32 shift = find_first_bit((void *)&mask, 32);
242 u32 tmp = ioread32(base + offset);
243
244 tmp &= ~mask; /* clear written bits */
245 val = tmp | (val << shift);
246 iowrite32(val, base + offset);
247}
248
249/**
Manu Gautam8c642812012-06-07 10:35:10 +0530250 * Write register and read back masked value to confirm it is written
251 *
252 * @base - DWC3 base virtual address.
253 * @offset - register offset.
254 * @mask - register bitmask specifying what should be updated
255 * @val - value to write.
256 *
257 */
258static inline void dwc3_msm_write_readback(void *base, u32 offset,
259 const u32 mask, u32 val)
260{
261 u32 write_val, tmp = ioread32(base + offset);
262
263 tmp &= ~mask; /* retain other bits */
264 write_val = tmp | val;
265
266 iowrite32(write_val, base + offset);
267
268 /* Read back to see if val was written */
269 tmp = ioread32(base + offset);
270 tmp &= mask; /* clear other bits */
271
272 if (tmp != val)
273 dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n",
274 __func__, val, offset);
275}
276
277/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300278 * Return DBM EP number according to usb endpoint number.
279 *
280 */
281static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep)
282{
283 int i;
284
285 for (i = 0; i < context->dbm_num_eps; i++)
286 if (context->ep_num_mapping[i] == usb_ep)
287 return i;
288
289 return -ENODEV; /* Not found */
290}
291
292/**
293 * Return number of configured DBM endpoints.
294 *
295 */
296static int dwc3_msm_configured_dbm_ep_num(void)
297{
298 int i;
299 int count = 0;
300
301 for (i = 0; i < context->dbm_num_eps; i++)
302 if (context->ep_num_mapping[i])
303 count++;
304
305 return count;
306}
307
308/**
309 * Configure the DBM with the USB3 core event buffer.
310 * This function is called by the SNPS UDC upon initialization.
311 *
312 * @addr - address of the event buffer.
313 * @size - size of the event buffer.
314 *
315 */
316static int dwc3_msm_event_buffer_config(u32 addr, u16 size)
317{
318 dev_dbg(context->dev, "%s\n", __func__);
319
320 dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr);
321 dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ,
322 DBM_GEVNTSIZ_MASK, size);
323
324 return 0;
325}
326
327/**
328 * Reset the DBM registers upon initialization.
329 *
330 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300331static int dwc3_msm_dbm_soft_reset(int enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300332{
333 dev_dbg(context->dev, "%s\n", __func__);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300334 if (enter_reset) {
335 dev_dbg(context->dev, "enter DBM reset\n");
336 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
337 DBM_SFT_RST_MASK, 1);
338 } else {
339 dev_dbg(context->dev, "exit DBM reset\n");
340 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
341 DBM_SFT_RST_MASK, 0);
342 /*enable DBM*/
343 dwc3_msm_write_reg_field(context->base, QSCRATCH_GENERAL_CFG,
344 DBM_EN_MASK, 0x1);
345 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300346
347 return 0;
348}
349
350/**
351 * Soft reset specific DBM ep.
352 * This function is called by the function driver upon events
353 * such as transfer aborting, USB re-enumeration and USB
354 * disconnection.
355 *
356 * @dbm_ep - DBM ep number.
357 * @enter_reset - should we enter a reset state or get out of it.
358 *
359 */
360static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset)
361{
362 dev_dbg(context->dev, "%s\n", __func__);
363
364 if (dbm_ep >= context->dbm_num_eps) {
365 dev_err(context->dev,
366 "%s: Invalid DBM ep index\n", __func__);
367 return -ENODEV;
368 }
369
370 if (enter_reset) {
371 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300372 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300373 } else {
374 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300375 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300376 }
377
378 return 0;
379}
380
381/**
382 * Configure a USB DBM ep to work in BAM mode.
383 *
384 *
385 * @usb_ep - USB physical EP number.
386 * @producer - producer/consumer.
387 * @disable_wb - disable write back to system memory.
388 * @internal_mem - use internal USB memory for data fifo.
389 * @ioc - enable interrupt on completion.
390 *
391 * @return int - DBM ep number.
392 */
393static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe,
394 bool producer, bool disable_wb,
395 bool internal_mem, bool ioc)
396{
397 u8 dbm_ep;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300398 u32 ep_cfg;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300399
400 dev_dbg(context->dev, "%s\n", __func__);
401
Shimrit Malichia00d7322012-08-05 13:56:28 +0300402 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
403
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300404 if (dbm_ep < 0) {
Shimrit Malichia00d7322012-08-05 13:56:28 +0300405 dev_err(context->dev,
406 "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300407 return -ENODEV;
408 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300409 /* First, reset the dbm endpoint */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300410 dwc3_msm_dbm_ep_soft_reset(dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300411
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300412 /* Set ioc bit for dbm_ep if needed */
413 dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300414 DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300415
Shimrit Malichia00d7322012-08-05 13:56:28 +0300416 ep_cfg = (producer ? DBM_PRODUCER : 0) |
417 (disable_wb ? DBM_DISABLE_WB : 0) |
418 (internal_mem ? DBM_INT_RAM_ACC : 0);
419
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300420 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
Shimrit Malichia00d7322012-08-05 13:56:28 +0300421 DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8);
422
423 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM,
424 usb_ep);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300425 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
426 DBM_BAM_PIPE_NUM, bam_pipe);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300427 dwc3_msm_write_reg_field(context->base, DBM_PIPE_CFG, 0x000000ff,
428 0xe4);
429 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP,
430 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300431
432 return dbm_ep;
433}
434
435/**
436 * Configure a USB DBM ep to work in normal mode.
437 *
438 * @usb_ep - USB ep number.
439 *
440 */
441static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep)
442{
443 u8 dbm_ep;
444
445 dev_dbg(context->dev, "%s\n", __func__);
446
447 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
448
449 if (dbm_ep < 0) {
450 dev_err(context->dev,
451 "%s: Invalid usb ep index\n", __func__);
452 return -ENODEV;
453 }
454
455 context->ep_num_mapping[dbm_ep] = 0;
456
457 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), 0);
458
459 /* Reset the dbm endpoint */
460 dwc3_msm_dbm_ep_soft_reset(dbm_ep, true);
461
462 return 0;
463}
464
465/**
466 * Configure the DBM with the BAM's data fifo.
467 * This function is called by the USB BAM Driver
468 * upon initialization.
469 *
470 * @ep - pointer to usb endpoint.
471 * @addr - address of data fifo.
472 * @size - size of data fifo.
473 *
474 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300475int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300476{
477 u8 dbm_ep;
478 struct dwc3_ep *dep = to_dwc3_ep(ep);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300479 u8 bam_pipe = dst_pipe_idx;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300480
481 dev_dbg(context->dev, "%s\n", __func__);
482
Shimrit Malichia00d7322012-08-05 13:56:28 +0300483 dbm_ep = bam_pipe;
484 context->ep_num_mapping[dbm_ep] = dep->number;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300485
486 dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr);
487 dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep),
488 DBM_DATA_FIFO_SIZE_MASK, size);
489
490 return 0;
491}
492
493/**
494* Cleanups for msm endpoint on request complete.
495*
496* Also call original request complete.
497*
498* @usb_ep - pointer to usb_ep instance.
499* @request - pointer to usb_request instance.
500*
501* @return int - 0 on success, negetive on error.
502*/
503static void dwc3_msm_req_complete_func(struct usb_ep *ep,
504 struct usb_request *request)
505{
506 struct dwc3_request *req = to_dwc3_request(request);
507 struct dwc3_ep *dep = to_dwc3_ep(ep);
508 struct dwc3_msm_req_complete *req_complete = NULL;
509
510 /* Find original request complete function and remove it from list */
511 list_for_each_entry(req_complete,
512 &context->req_complete_list,
513 list_item) {
514 if (req_complete->req == request)
515 break;
516 }
517 if (!req_complete || req_complete->req != request) {
518 dev_err(dep->dwc->dev, "%s: could not find the request\n",
519 __func__);
520 return;
521 }
522 list_del(&req_complete->list_item);
523
524 /*
525 * Release another one TRB to the pool since DBM queue took 2 TRBs
526 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
527 * released only one.
528 */
529 if (req->queued)
530 dep->busy_slot++;
531
532 /* Unconfigure dbm ep */
533 dwc3_msm_dbm_ep_unconfig(dep->number);
534
535 /*
536 * If this is the last endpoint we unconfigured, than reset also
537 * the event buffers.
538 */
539 if (0 == dwc3_msm_configured_dbm_ep_num())
540 dwc3_msm_event_buffer_config(0, 0);
541
542 /*
543 * Call original complete function, notice that dwc->lock is already
544 * taken by the caller of this function (dwc3_gadget_giveback()).
545 */
546 request->complete = req_complete->orig_complete;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300547 if (request->complete)
548 request->complete(ep, request);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300549
550 kfree(req_complete);
551}
552
553/**
554* Helper function.
555* See the header of the dwc3_msm_ep_queue function.
556*
557* @dwc3_ep - pointer to dwc3_ep instance.
558* @req - pointer to dwc3_request instance.
559*
560* @return int - 0 on success, negetive on error.
561*/
562static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
563{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300564 struct dwc3_trb *trb;
565 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300566 struct dwc3_gadget_ep_cmd_params params;
567 u32 cmd;
568 int ret = 0;
569
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300570 /* We push the request to the dep->req_queued list to indicate that
571 * this request is issued with start transfer. The request will be out
572 * from this list in 2 cases. The first is that the transfer will be
573 * completed (not if the transfer is endless using a circular TRBs with
574 * with link TRB). The second case is an option to do stop stransfer,
575 * this can be initiated by the function driver when calling dequeue.
576 */
577 req->queued = true;
578 list_add_tail(&req->list, &dep->req_queued);
579
580 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300581 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300582 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300583 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300584
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300585 req->trb = trb;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300586 trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300587 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
588 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300589 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300590
591 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300592 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300593 dep->free_slot++;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300594 memset(trb_link, 0, sizeof *trb_link);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300595
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300596 trb_link->bpl = lower_32_bits(req->trb_dma);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300597 trb_link->bph = DBM_TRB_BIT |
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300598 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
599 trb_link->size = 0;
600 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300601
602 /*
603 * Now start the transfer
604 */
605 memset(&params, 0, sizeof(params));
Shimrit Malichia00d7322012-08-05 13:56:28 +0300606 params.param0 = 0; /* TDAddr High */
607 params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */
608
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300609 cmd = DWC3_DEPCMD_STARTTRANSFER;
610 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
611 if (ret < 0) {
612 dev_dbg(dep->dwc->dev,
613 "%s: failed to send STARTTRANSFER command\n",
614 __func__);
615
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300616 list_del(&req->list);
617 return ret;
618 }
619
620 return ret;
621}
622
623/**
624* Queue a usb request to the DBM endpoint.
625* This function should be called after the endpoint
626* was enabled by the ep_enable.
627*
628* This function prepares special structure of TRBs which
629* is familier with the DBM HW, so it will possible to use
630* this endpoint in DBM mode.
631*
632* The TRBs prepared by this function, is one normal TRB
633* which point to a fake buffer, followed by a link TRB
634* that points to the first TRB.
635*
636* The API of this function follow the regular API of
637* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
638*
639* @usb_ep - pointer to usb_ep instance.
640* @request - pointer to usb_request instance.
641* @gfp_flags - possible flags.
642*
643* @return int - 0 on success, negetive on error.
644*/
645static int dwc3_msm_ep_queue(struct usb_ep *ep,
646 struct usb_request *request, gfp_t gfp_flags)
647{
648 struct dwc3_request *req = to_dwc3_request(request);
649 struct dwc3_ep *dep = to_dwc3_ep(ep);
650 struct dwc3 *dwc = dep->dwc;
651 struct dwc3_msm_req_complete *req_complete;
652 unsigned long flags;
653 int ret = 0;
654 u8 bam_pipe;
655 bool producer;
656 bool disable_wb;
657 bool internal_mem;
658 bool ioc;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300659 u8 speed;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300660
661 if (!(request->udc_priv & MSM_SPS_MODE)) {
662 /* Not SPS mode, call original queue */
663 dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n",
664 __func__);
665
666 return (context->original_ep_ops[dep->number])->queue(ep,
667 request,
668 gfp_flags);
669 }
670
671 if (!dep->endpoint.desc) {
672 dev_err(dwc->dev,
673 "%s: trying to queue request %p to disabled ep %s\n",
674 __func__, request, ep->name);
675 return -EPERM;
676 }
677
678 if (dep->number == 0 || dep->number == 1) {
679 dev_err(dwc->dev,
680 "%s: trying to queue dbm request %p to control ep %s\n",
681 __func__, request, ep->name);
682 return -EPERM;
683 }
684
685 if (dep->free_slot > 0 || dep->busy_slot > 0 ||
686 !list_empty(&dep->request_list) ||
687 !list_empty(&dep->req_queued)) {
688
689 dev_err(dwc->dev,
690 "%s: trying to queue dbm request %p tp ep %s\n",
691 __func__, request, ep->name);
692 return -EPERM;
693 }
694
695 /*
696 * Override req->complete function, but before doing that,
697 * store it's original pointer in the req_complete_list.
698 */
699 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
700 if (!req_complete) {
701 dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__);
702 return -ENOMEM;
703 }
704 req_complete->req = request;
705 req_complete->orig_complete = request->complete;
706 list_add_tail(&req_complete->list_item, &context->req_complete_list);
707 request->complete = dwc3_msm_req_complete_func;
708
709 /*
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300710 * Configure the DBM endpoint
711 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300712 bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300713 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
714 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
715 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
716 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
717
718 ret = dwc3_msm_dbm_ep_config(dep->number,
719 bam_pipe, producer,
720 disable_wb, internal_mem, ioc);
721 if (ret < 0) {
722 dev_err(context->dev,
723 "error %d after calling dwc3_msm_dbm_ep_config\n",
724 ret);
725 return ret;
726 }
727
728 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
729 __func__, request, ep->name, request->length);
730
731 /*
732 * We must obtain the lock of the dwc3 core driver,
733 * including disabling interrupts, so we will be sure
734 * that we are the only ones that configure the HW device
735 * core and ensure that we queuing the request will finish
736 * as soon as possible so we will release back the lock.
737 */
738 spin_lock_irqsave(&dwc->lock, flags);
739 ret = __dwc3_msm_ep_queue(dep, req);
740 spin_unlock_irqrestore(&dwc->lock, flags);
741 if (ret < 0) {
742 dev_err(context->dev,
743 "error %d after calling __dwc3_msm_ep_queue\n", ret);
744 return ret;
745 }
746
Shimrit Malichia00d7322012-08-05 13:56:28 +0300747 speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD;
748 dwc3_msm_write_reg(context->base, DBM_GEN_CFG, speed >> 2);
749
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300750 return 0;
751}
752
753/**
754 * Configure MSM endpoint.
755 * This function do specific configurations
756 * to an endpoint which need specific implementaion
757 * in the MSM architecture.
758 *
759 * This function should be called by usb function/class
760 * layer which need a support from the specific MSM HW
761 * which wrap the USB3 core. (like DBM specific endpoints)
762 *
763 * @ep - a pointer to some usb_ep instance
764 *
765 * @return int - 0 on success, negetive on error.
766 */
767int msm_ep_config(struct usb_ep *ep)
768{
769 struct dwc3_ep *dep = to_dwc3_ep(ep);
770 struct usb_ep_ops *new_ep_ops;
771
772 /* Save original ep ops for future restore*/
773 if (context->original_ep_ops[dep->number]) {
774 dev_err(context->dev,
775 "ep [%s,%d] already configured as msm endpoint\n",
776 ep->name, dep->number);
777 return -EPERM;
778 }
779 context->original_ep_ops[dep->number] = ep->ops;
780
781 /* Set new usb ops as we like */
782 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
783 if (!new_ep_ops) {
784 dev_err(context->dev,
785 "%s: unable to allocate mem for new usb ep ops\n",
786 __func__);
787 return -ENOMEM;
788 }
789 (*new_ep_ops) = (*ep->ops);
790 new_ep_ops->queue = dwc3_msm_ep_queue;
791 ep->ops = new_ep_ops;
792
793 /*
794 * Do HERE more usb endpoint configurations
795 * which are specific to MSM.
796 */
797
798 return 0;
799}
800EXPORT_SYMBOL(msm_ep_config);
801
802/**
803 * Un-configure MSM endpoint.
804 * Tear down configurations done in the
805 * dwc3_msm_ep_config function.
806 *
807 * @ep - a pointer to some usb_ep instance
808 *
809 * @return int - 0 on success, negetive on error.
810 */
811int msm_ep_unconfig(struct usb_ep *ep)
812{
813 struct dwc3_ep *dep = to_dwc3_ep(ep);
814 struct usb_ep_ops *old_ep_ops;
815
816 /* Restore original ep ops */
817 if (!context->original_ep_ops[dep->number]) {
818 dev_err(context->dev,
819 "ep [%s,%d] was not configured as msm endpoint\n",
820 ep->name, dep->number);
821 return -EINVAL;
822 }
823 old_ep_ops = (struct usb_ep_ops *)ep->ops;
824 ep->ops = context->original_ep_ops[dep->number];
825 context->original_ep_ops[dep->number] = NULL;
826 kfree(old_ep_ops);
827
828 /*
829 * Do HERE more usb endpoint un-configurations
830 * which are specific to MSM.
831 */
832
833 return 0;
834}
835EXPORT_SYMBOL(msm_ep_unconfig);
836
Manu Gautam60e01352012-05-29 09:00:34 +0530837/* HSPHY */
838static int dwc3_hsusb_config_vddcx(int high)
839{
840 int min_vol, ret;
841 struct dwc3_msm *dwc = context;
842 enum usb_vdd_type vdd_type = context->hs_vdd_type;
843 int max_vol = vdd_val[vdd_type][VDD_MAX];
844
845 min_vol = vdd_val[vdd_type][high ? VDD_MIN : VDD_NONE];
846 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
847 if (ret) {
848 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
849 return ret;
850 }
851
852 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
853 min_vol, max_vol);
854
855 return ret;
856}
857
858static int dwc3_hsusb_ldo_init(int init)
859{
860 int rc = 0;
861 struct dwc3_msm *dwc = context;
862
863 if (!init) {
864 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
865 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
866 return 0;
867 }
868
869 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
870 if (IS_ERR(dwc->hsusb_3p3)) {
871 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
872 return PTR_ERR(dwc->hsusb_3p3);
873 }
874
875 rc = regulator_set_voltage(dwc->hsusb_3p3,
876 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
877 if (rc) {
878 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
879 return rc;
880 }
881 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
882 if (IS_ERR(dwc->hsusb_1p8)) {
883 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
884 rc = PTR_ERR(dwc->hsusb_1p8);
885 goto devote_3p3;
886 }
887 rc = regulator_set_voltage(dwc->hsusb_1p8,
888 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
889 if (rc) {
890 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
891 goto devote_3p3;
892 }
893
894 return 0;
895
896devote_3p3:
897 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
898
899 return rc;
900}
901
902static int dwc3_hsusb_ldo_enable(int on)
903{
904 int rc = 0;
905 struct dwc3_msm *dwc = context;
906
907 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
908
909 if (!on)
910 goto disable_regulators;
911
912
913 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
914 if (rc < 0) {
915 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
916 return rc;
917 }
918
919 rc = regulator_enable(dwc->hsusb_1p8);
920 if (rc) {
921 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
922 goto put_1p8_lpm;
923 }
924
925 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
926 if (rc < 0) {
927 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
928 goto disable_1p8;
929 }
930
931 rc = regulator_enable(dwc->hsusb_3p3);
932 if (rc) {
933 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
934 goto put_3p3_lpm;
935 }
936
937 return 0;
938
939disable_regulators:
940 rc = regulator_disable(dwc->hsusb_3p3);
941 if (rc)
942 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
943
944put_3p3_lpm:
945 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
946 if (rc < 0)
947 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
948
949disable_1p8:
950 rc = regulator_disable(dwc->hsusb_1p8);
951 if (rc)
952 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
953
954put_1p8_lpm:
955 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
956 if (rc < 0)
957 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
958
959 return rc < 0 ? rc : 0;
960}
961
962/* SSPHY */
963static int dwc3_ssusb_config_vddcx(int high)
964{
965 int min_vol, ret;
966 struct dwc3_msm *dwc = context;
967 enum usb_vdd_type vdd_type = context->ss_vdd_type;
968 int max_vol = vdd_val[vdd_type][VDD_MAX];
969
970 min_vol = vdd_val[vdd_type][high ? VDD_MIN : VDD_NONE];
971 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
972 if (ret) {
973 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
974 return ret;
975 }
976
977 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
978 min_vol, max_vol);
979 return ret;
980}
981
982/* 3.3v supply not needed for SS PHY */
983static int dwc3_ssusb_ldo_init(int init)
984{
985 int rc = 0;
986 struct dwc3_msm *dwc = context;
987
988 if (!init) {
989 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
990 return 0;
991 }
992
993 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
994 if (IS_ERR(dwc->ssusb_1p8)) {
995 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
996 return PTR_ERR(dwc->ssusb_1p8);
997 }
998 rc = regulator_set_voltage(dwc->ssusb_1p8,
999 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
1000 if (rc)
1001 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
1002
1003 return rc;
1004}
1005
1006static int dwc3_ssusb_ldo_enable(int on)
1007{
1008 int rc = 0;
1009 struct dwc3_msm *dwc = context;
1010
1011 dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1012
1013 if (!on)
1014 goto disable_regulators;
1015
1016
1017 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1018 if (rc < 0) {
1019 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1020 return rc;
1021 }
1022
1023 rc = regulator_enable(dwc->ssusb_1p8);
1024 if (rc) {
1025 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1026 goto put_1p8_lpm;
1027 }
1028
1029 return 0;
1030
1031disable_regulators:
1032 rc = regulator_disable(dwc->ssusb_1p8);
1033 if (rc)
1034 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1035
1036put_1p8_lpm:
1037 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1038 if (rc < 0)
1039 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1040
1041 return rc < 0 ? rc : 0;
1042}
1043
Manu Gautam8c642812012-06-07 10:35:10 +05301044static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1045{
1046 u32 chg_ctrl;
1047
1048 /* Turn off VDP_SRC */
1049 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1050 msleep(20);
1051
1052 /* Before proceeding make sure VDP_SRC is OFF */
1053 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1054 if (chg_ctrl & 0x3F)
1055 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1056 __func__, chg_ctrl);
1057 /*
1058 * Configure DM as current source, DP as current sink
1059 * and enable battery charging comparators.
1060 */
1061 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1062}
1063
1064static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1065{
1066 u32 chg_det;
1067 bool ret = false;
1068
1069 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1070 ret = chg_det & 1;
1071
1072 return ret;
1073}
1074
1075static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1076{
1077 /*
1078 * Configure DP as current source, DM as current sink
1079 * and enable battery charging comparators.
1080 */
1081 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1082}
1083
1084static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1085{
1086 u32 chg_state;
1087 bool ret = false;
1088
1089 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1090 ret = chg_state & 2;
1091
1092 return ret;
1093}
1094
1095static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1096{
1097 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1098}
1099
1100static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1101{
1102 /* Data contact detection enable, DCDENB */
1103 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1104}
1105
1106static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1107{
1108 u32 chg_ctrl;
1109
1110 /* Clear charger detecting control bits */
1111 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1112
1113 /* Clear alt interrupt latch and enable bits */
1114 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1115 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1116
1117 udelay(100);
1118
1119 /* Before proceeding make sure charger block is RESET */
1120 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1121 if (chg_ctrl & 0x3F)
1122 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1123 __func__, chg_ctrl);
1124}
1125
1126static const char *chg_to_string(enum dwc3_chg_type chg_type)
1127{
1128 switch (chg_type) {
1129 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1130 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1131 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1132 default: return "INVALID_CHARGER";
1133 }
1134}
1135
1136#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1137#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1138#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1139#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1140
1141static void dwc3_chg_detect_work(struct work_struct *w)
1142{
1143 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1144 bool is_dcd = false, tmout, vout;
1145 unsigned long delay;
1146
1147 dev_dbg(mdwc->dev, "chg detection work\n");
1148 switch (mdwc->chg_state) {
1149 case USB_CHG_STATE_UNDEFINED:
1150 dwc3_chg_block_reset(mdwc);
1151 dwc3_chg_enable_dcd(mdwc);
1152 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1153 mdwc->dcd_retries = 0;
1154 delay = DWC3_CHG_DCD_POLL_TIME;
1155 break;
1156 case USB_CHG_STATE_WAIT_FOR_DCD:
1157 is_dcd = dwc3_chg_check_dcd(mdwc);
1158 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1159 if (is_dcd || tmout) {
1160 dwc3_chg_disable_dcd(mdwc);
1161 dwc3_chg_enable_primary_det(mdwc);
1162 delay = DWC3_CHG_PRIMARY_DET_TIME;
1163 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1164 } else {
1165 delay = DWC3_CHG_DCD_POLL_TIME;
1166 }
1167 break;
1168 case USB_CHG_STATE_DCD_DONE:
1169 vout = dwc3_chg_det_check_output(mdwc);
1170 if (vout) {
1171 dwc3_chg_enable_secondary_det(mdwc);
1172 delay = DWC3_CHG_SECONDARY_DET_TIME;
1173 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1174 } else {
1175 mdwc->charger.chg_type = USB_SDP_CHARGER;
1176 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1177 delay = 0;
1178 }
1179 break;
1180 case USB_CHG_STATE_PRIMARY_DONE:
1181 vout = dwc3_chg_det_check_output(mdwc);
1182 if (vout)
1183 mdwc->charger.chg_type = USB_DCP_CHARGER;
1184 else
1185 mdwc->charger.chg_type = USB_CDP_CHARGER;
1186 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1187 /* fall through */
1188 case USB_CHG_STATE_SECONDARY_DONE:
1189 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1190 /* fall through */
1191 case USB_CHG_STATE_DETECTED:
1192 dwc3_chg_block_reset(mdwc);
1193 dev_dbg(mdwc->dev, "chg_type = %s\n",
1194 chg_to_string(mdwc->charger.chg_type));
1195 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1196 &mdwc->charger);
1197 return;
1198 default:
1199 return;
1200 }
1201
1202 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1203}
1204
1205static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1206{
1207 struct dwc3_msm *mdwc = context;
1208
1209 if (start == false) {
1210 cancel_delayed_work_sync(&mdwc->chg_work);
1211 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1212 charger->chg_type = DWC3_INVALID_CHARGER;
1213 return;
1214 }
1215
1216 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1217 charger->chg_type = DWC3_INVALID_CHARGER;
1218 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1219}
1220
Manu Gautamb5067272012-07-02 09:53:41 +05301221static int dwc3_msm_suspend(struct dwc3_msm *mdwc)
1222{
1223 dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__);
1224
1225 if (atomic_read(&mdwc->in_lpm)) {
1226 dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__);
1227 return 0;
1228 }
1229
1230 clk_disable_unprepare(mdwc->core_clk);
1231 dwc3_hsusb_ldo_enable(0);
1232 dwc3_ssusb_ldo_enable(0);
1233 wake_unlock(&mdwc->wlock);
1234
1235 atomic_set(&mdwc->in_lpm, 1);
1236 dev_info(mdwc->dev, "DWC3 in low power mode\n");
1237
1238 return 0;
1239}
1240
1241static int dwc3_msm_resume(struct dwc3_msm *mdwc)
1242{
1243 dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__);
1244
1245 if (!atomic_read(&mdwc->in_lpm)) {
1246 dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__);
1247 return 0;
1248 }
1249
1250 wake_lock(&mdwc->wlock);
1251 clk_prepare_enable(mdwc->core_clk);
1252 dwc3_hsusb_ldo_enable(1);
1253 dwc3_ssusb_ldo_enable(1);
1254
1255 atomic_set(&mdwc->in_lpm, 0);
1256 dev_info(mdwc->dev, "DWC3 exited from low power mode\n");
1257
1258 return 0;
1259}
1260
1261static void dwc3_resume_work(struct work_struct *w)
1262{
1263 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1264 resume_work.work);
1265
1266 dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__);
1267 /* handle any event that was queued while work was already running */
1268 if (!atomic_read(&mdwc->in_lpm)) {
1269 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1270 if (mdwc->otg_xceiv)
1271 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1272 DWC3_EVENT_XCEIV_STATE);
1273 return;
1274 }
1275
1276 /* bail out if system resume in process, else initiate RESUME */
1277 if (atomic_read(&mdwc->pm_suspended)) {
1278 mdwc->resume_pending = true;
1279 } else {
1280 pm_runtime_get_sync(mdwc->dev);
1281 if (mdwc->otg_xceiv)
1282 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1283 DWC3_EVENT_PHY_RESUME);
1284 pm_runtime_put_sync(mdwc->dev);
1285 }
1286}
1287
1288static bool debug_id, debug_bsv, debug_connect;
1289
1290static int dwc3_connect_show(struct seq_file *s, void *unused)
1291{
1292 if (debug_connect)
1293 seq_printf(s, "true\n");
1294 else
1295 seq_printf(s, "false\n");
1296
1297 return 0;
1298}
1299
1300static int dwc3_connect_open(struct inode *inode, struct file *file)
1301{
1302 return single_open(file, dwc3_connect_show, inode->i_private);
1303}
1304
1305static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf,
1306 size_t count, loff_t *ppos)
1307{
1308 struct seq_file *s = file->private_data;
1309 struct dwc3_msm *mdwc = s->private;
1310 char buf[8];
1311
1312 memset(buf, 0x00, sizeof(buf));
1313
1314 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1315 return -EFAULT;
1316
1317 if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) {
1318 debug_connect = true;
1319 } else {
1320 debug_connect = debug_bsv = false;
1321 debug_id = true;
1322 }
1323
1324 mdwc->ext_xceiv.bsv = debug_bsv;
1325 mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND;
1326
1327 if (atomic_read(&mdwc->in_lpm)) {
1328 dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__);
1329 dwc3_resume_work(&mdwc->resume_work.work);
1330 } else {
1331 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1332 if (mdwc->otg_xceiv)
1333 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1334 DWC3_EVENT_XCEIV_STATE);
1335 }
1336
1337 return count;
1338}
1339
1340const struct file_operations dwc3_connect_fops = {
1341 .open = dwc3_connect_open,
1342 .read = seq_read,
1343 .write = dwc3_connect_write,
1344 .llseek = seq_lseek,
1345 .release = single_release,
1346};
1347
1348static struct dentry *dwc3_debugfs_root;
1349
1350static void dwc3_debugfs_init(struct dwc3_msm *mdwc)
1351{
1352 dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL);
1353
1354 if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root))
1355 return;
1356
1357 if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
1358 (u32 *)&debug_id))
1359 goto error;
1360
1361 if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
1362 (u32 *)&debug_bsv))
1363 goto error;
1364
1365 if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR,
1366 dwc3_debugfs_root, mdwc, &dwc3_connect_fops))
1367 goto error;
1368
1369 return;
1370
1371error:
1372 debugfs_remove_recursive(dwc3_debugfs_root);
1373}
Manu Gautam8c642812012-06-07 10:35:10 +05301374
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001375static int __devinit dwc3_msm_probe(struct platform_device *pdev)
1376{
1377 struct device_node *node = pdev->dev.of_node;
1378 struct platform_device *dwc3;
1379 struct dwc3_msm *msm;
1380 struct resource *res;
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03001381 void __iomem *tcsr;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001382 int ret = 0;
1383
1384 msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL);
1385 if (!msm) {
1386 dev_err(&pdev->dev, "not enough memory\n");
1387 return -ENOMEM;
1388 }
1389
1390 platform_set_drvdata(pdev, msm);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001391 context = msm;
Manu Gautam60e01352012-05-29 09:00:34 +05301392 msm->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001393
1394 INIT_LIST_HEAD(&msm->req_complete_list);
Manu Gautam8c642812012-06-07 10:35:10 +05301395 INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work);
Manu Gautamb5067272012-07-02 09:53:41 +05301396 INIT_DELAYED_WORK(&msm->resume_work, dwc3_resume_work);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001397
Manu Gautam1742db22012-06-19 13:33:24 +05301398 /*
1399 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
1400 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
1401 */
1402 msm->core_clk = devm_clk_get(&pdev->dev, "core_clk");
1403 if (IS_ERR(msm->core_clk)) {
1404 dev_err(&pdev->dev, "failed to get core_clk\n");
1405 return PTR_ERR(msm->core_clk);
1406 }
1407 clk_set_rate(msm->core_clk, 125000000);
1408 clk_prepare_enable(msm->core_clk);
1409
Manu Gautam60e01352012-05-29 09:00:34 +05301410 /* SS PHY */
1411 msm->ss_vdd_type = VDDCX_CORNER;
1412 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
1413 if (IS_ERR(msm->ssusb_vddcx)) {
1414 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev,
1415 "SSUSB_VDDCX");
1416 if (IS_ERR(msm->ssusb_vddcx)) {
1417 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
Manu Gautam1742db22012-06-19 13:33:24 +05301418 ret = PTR_ERR(msm->ssusb_vddcx);
1419 goto disable_core_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05301420 }
1421 msm->ss_vdd_type = VDDCX;
1422 dev_dbg(&pdev->dev, "ss_vdd_type: VDDCX\n");
1423 }
1424
1425 ret = dwc3_ssusb_config_vddcx(1);
1426 if (ret) {
1427 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam1742db22012-06-19 13:33:24 +05301428 goto disable_core_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05301429 }
1430
1431 ret = regulator_enable(context->ssusb_vddcx);
1432 if (ret) {
1433 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
1434 goto unconfig_ss_vddcx;
1435 }
1436
1437 ret = dwc3_ssusb_ldo_init(1);
1438 if (ret) {
1439 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
1440 goto disable_ss_vddcx;
1441 }
1442
1443 ret = dwc3_ssusb_ldo_enable(1);
1444 if (ret) {
1445 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
1446 goto free_ss_ldo_init;
1447 }
1448
1449 /* HS PHY */
1450 msm->hs_vdd_type = VDDCX_CORNER;
1451 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
1452 if (IS_ERR(msm->hsusb_vddcx)) {
1453 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev,
1454 "HSUSB_VDDCX");
1455 if (IS_ERR(msm->hsusb_vddcx)) {
1456 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
1457 ret = PTR_ERR(msm->ssusb_vddcx);
1458 goto disable_ss_ldo;
1459 }
1460 msm->hs_vdd_type = VDDCX;
1461 dev_dbg(&pdev->dev, "hs_vdd_type: VDDCX\n");
1462 }
1463
1464 ret = dwc3_hsusb_config_vddcx(1);
1465 if (ret) {
1466 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1467 goto disable_ss_ldo;
1468 }
1469
1470 ret = regulator_enable(context->hsusb_vddcx);
1471 if (ret) {
1472 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
1473 goto unconfig_hs_vddcx;
1474 }
1475
1476 ret = dwc3_hsusb_ldo_init(1);
1477 if (ret) {
1478 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1479 goto disable_hs_vddcx;
1480 }
1481
1482 ret = dwc3_hsusb_ldo_enable(1);
1483 if (ret) {
1484 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1485 goto free_hs_ldo_init;
1486 }
1487
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03001488 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1489 if (!res) {
1490 dev_dbg(&pdev->dev, "missing TCSR memory resource\n");
1491 } else {
1492 tcsr = devm_ioremap_nocache(&pdev->dev, res->start,
1493 resource_size(res));
1494 if (!tcsr) {
1495 dev_dbg(&pdev->dev, "tcsr ioremap failed\n");
1496 } else {
1497 /* Enable USB3 on the primary USB port. */
1498 writel_relaxed(0x1, tcsr);
1499 /*
1500 * Ensure that TCSR write is completed before
1501 * USB registers initialization.
1502 */
1503 mb();
1504 }
1505 }
1506
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001507 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1508 if (!res) {
1509 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301510 ret = -ENODEV;
1511 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001512 }
1513
1514 msm->base = devm_ioremap_nocache(&pdev->dev, res->start,
1515 resource_size(res));
1516 if (!msm->base) {
1517 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301518 ret = -ENODEV;
1519 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001520 }
1521
Ido Shayevitzca2691e2012-04-17 15:54:53 +03001522 dwc3 = platform_device_alloc("dwc3", -1);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001523 if (!dwc3) {
1524 dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301525 ret = -ENODEV;
1526 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001527 }
1528
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001529 dwc3->dev.parent = &pdev->dev;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +03001530 dwc3->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1531 dwc3->dev.dma_mask = &dwc3_msm_dma_mask;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001532 dwc3->dev.dma_parms = pdev->dev.dma_parms;
1533 msm->resource_size = resource_size(res);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001534 msm->dwc3 = dwc3;
1535
Manu Gautamb5067272012-07-02 09:53:41 +05301536 pm_runtime_set_active(msm->dev);
1537 pm_runtime_enable(msm->dev);
1538
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001539 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
1540 &msm->dbm_num_eps)) {
1541 dev_err(&pdev->dev,
1542 "unable to read platform data num of dbm eps\n");
1543 msm->dbm_num_eps = DBM_MAX_EPS;
1544 }
1545
1546 if (msm->dbm_num_eps > DBM_MAX_EPS) {
1547 dev_err(&pdev->dev,
1548 "Driver doesn't support number of DBM EPs. "
1549 "max: %d, dbm_num_eps: %d\n",
1550 DBM_MAX_EPS, msm->dbm_num_eps);
1551 ret = -ENODEV;
Manu Gautam60e01352012-05-29 09:00:34 +05301552 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001553 }
1554
1555 ret = platform_device_add_resources(dwc3, pdev->resource,
1556 pdev->num_resources);
1557 if (ret) {
1558 dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301559 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001560 }
1561
1562 ret = platform_device_add(dwc3);
1563 if (ret) {
1564 dev_err(&pdev->dev, "failed to register dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301565 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001566 }
1567
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001568 /* Reset the DBM */
Shimrit Malichia00d7322012-08-05 13:56:28 +03001569 dwc3_msm_dbm_soft_reset(1);
1570 usleep_range(1000, 1200);
1571 dwc3_msm_dbm_soft_reset(0);
1572
1573 dwc3_msm_event_buffer_config(dwc3_readl(msm->base, DWC3_GEVNTADRLO(0)),
1574 dwc3_readl(msm->base, DWC3_GEVNTSIZ(0)));
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001575
Manu Gautam8c642812012-06-07 10:35:10 +05301576 msm->otg_xceiv = usb_get_transceiver();
1577 if (msm->otg_xceiv) {
1578 msm->charger.start_detection = dwc3_start_chg_det;
1579 ret = dwc3_set_charger(msm->otg_xceiv->otg, &msm->charger);
1580 if (ret || !msm->charger.notify_detection_complete) {
1581 dev_err(&pdev->dev, "failed to register charger: %d\n",
1582 ret);
1583 goto put_xcvr;
1584 }
Manu Gautamb5067272012-07-02 09:53:41 +05301585
1586 ret = dwc3_set_ext_xceiv(msm->otg_xceiv->otg, &msm->ext_xceiv);
1587 if (ret || !msm->ext_xceiv.notify_ext_events) {
1588 dev_err(&pdev->dev, "failed to register xceiver: %d\n",
1589 ret);
1590 goto put_xcvr;
1591 }
Manu Gautam8c642812012-06-07 10:35:10 +05301592 } else {
1593 dev_err(&pdev->dev, "%s: No OTG transceiver found\n", __func__);
1594 }
1595
Manu Gautamb5067272012-07-02 09:53:41 +05301596 wake_lock_init(&msm->wlock, WAKE_LOCK_SUSPEND, "msm_dwc3");
1597 wake_lock(&msm->wlock);
1598 dwc3_debugfs_init(msm);
1599
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001600 return 0;
1601
Manu Gautam8c642812012-06-07 10:35:10 +05301602put_xcvr:
1603 usb_put_transceiver(msm->otg_xceiv);
1604 platform_device_del(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05301605put_pdev:
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001606 platform_device_put(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05301607disable_hs_ldo:
1608 dwc3_hsusb_ldo_enable(0);
1609free_hs_ldo_init:
1610 dwc3_hsusb_ldo_init(0);
1611disable_hs_vddcx:
1612 regulator_disable(context->hsusb_vddcx);
1613unconfig_hs_vddcx:
1614 dwc3_hsusb_config_vddcx(0);
1615disable_ss_ldo:
1616 dwc3_ssusb_ldo_enable(0);
1617free_ss_ldo_init:
1618 dwc3_ssusb_ldo_init(0);
1619disable_ss_vddcx:
1620 regulator_disable(context->ssusb_vddcx);
1621unconfig_ss_vddcx:
1622 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05301623disable_core_clk:
1624 clk_disable_unprepare(msm->core_clk);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001625
1626 return ret;
1627}
1628
1629static int __devexit dwc3_msm_remove(struct platform_device *pdev)
1630{
1631 struct dwc3_msm *msm = platform_get_drvdata(pdev);
1632
Manu Gautamb5067272012-07-02 09:53:41 +05301633 if (dwc3_debugfs_root)
1634 debugfs_remove_recursive(dwc3_debugfs_root);
Manu Gautam8c642812012-06-07 10:35:10 +05301635 if (msm->otg_xceiv) {
1636 dwc3_start_chg_det(&msm->charger, false);
1637 usb_put_transceiver(msm->otg_xceiv);
1638 }
Manu Gautamb5067272012-07-02 09:53:41 +05301639 pm_runtime_disable(msm->dev);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001640 platform_device_unregister(msm->dwc3);
Manu Gautamb5067272012-07-02 09:53:41 +05301641 wake_lock_destroy(&msm->wlock);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001642
Manu Gautam60e01352012-05-29 09:00:34 +05301643 dwc3_hsusb_ldo_enable(0);
1644 dwc3_hsusb_ldo_init(0);
1645 regulator_disable(msm->hsusb_vddcx);
1646 dwc3_hsusb_config_vddcx(0);
1647 dwc3_ssusb_ldo_enable(0);
1648 dwc3_ssusb_ldo_init(0);
1649 regulator_disable(msm->ssusb_vddcx);
1650 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05301651 clk_disable_unprepare(msm->core_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05301652
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001653 return 0;
1654}
1655
Manu Gautamb5067272012-07-02 09:53:41 +05301656static int dwc3_msm_pm_suspend(struct device *dev)
1657{
1658 int ret = 0;
1659 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1660
1661 dev_dbg(dev, "dwc3-msm PM suspend\n");
1662
1663 ret = dwc3_msm_suspend(mdwc);
1664 if (!ret)
1665 atomic_set(&mdwc->pm_suspended, 1);
1666
1667 return ret;
1668}
1669
1670static int dwc3_msm_pm_resume(struct device *dev)
1671{
1672 int ret = 0;
1673 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1674
1675 dev_dbg(dev, "dwc3-msm PM resume\n");
1676
1677 atomic_set(&mdwc->pm_suspended, 0);
1678 if (mdwc->resume_pending) {
1679 mdwc->resume_pending = false;
1680
1681 ret = dwc3_msm_resume(mdwc);
1682 /* Update runtime PM status */
1683 pm_runtime_disable(dev);
1684 pm_runtime_set_active(dev);
1685 pm_runtime_enable(dev);
1686
1687 /* Let OTG know about resume event and update pm_count */
1688 if (mdwc->otg_xceiv)
1689 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1690 DWC3_EVENT_PHY_RESUME);
1691 }
1692
1693 return ret;
1694}
1695
1696static int dwc3_msm_runtime_idle(struct device *dev)
1697{
1698 dev_dbg(dev, "DWC3-msm runtime idle\n");
1699
1700 return 0;
1701}
1702
1703static int dwc3_msm_runtime_suspend(struct device *dev)
1704{
1705 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1706
1707 dev_dbg(dev, "DWC3-msm runtime suspend\n");
1708
1709 return dwc3_msm_suspend(mdwc);
1710}
1711
1712static int dwc3_msm_runtime_resume(struct device *dev)
1713{
1714 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1715
1716 dev_dbg(dev, "DWC3-msm runtime resume\n");
1717
1718 return dwc3_msm_resume(mdwc);
1719}
1720
1721static const struct dev_pm_ops dwc3_msm_dev_pm_ops = {
1722 SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume)
1723 SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume,
1724 dwc3_msm_runtime_idle)
1725};
1726
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001727static const struct of_device_id of_dwc3_matach[] = {
1728 {
1729 .compatible = "qcom,dwc-usb3-msm",
1730 },
1731 { },
1732};
1733MODULE_DEVICE_TABLE(of, of_dwc3_matach);
1734
1735static struct platform_driver dwc3_msm_driver = {
1736 .probe = dwc3_msm_probe,
1737 .remove = __devexit_p(dwc3_msm_remove),
1738 .driver = {
1739 .name = "msm-dwc3",
Manu Gautamb5067272012-07-02 09:53:41 +05301740 .pm = &dwc3_msm_dev_pm_ops,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001741 .of_match_table = of_dwc3_matach,
1742 },
1743};
1744
1745MODULE_LICENSE("GPLV2");
1746MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
1747
1748static int __devinit dwc3_msm_init(void)
1749{
1750 return platform_driver_register(&dwc3_msm_driver);
1751}
1752module_init(dwc3_msm_init);
1753
1754static void __exit dwc3_msm_exit(void)
1755{
1756 platform_driver_unregister(&dwc3_msm_driver);
1757}
1758module_exit(dwc3_msm_exit);