blob: 45b9ad3790b8d7da9e4ff07ccb54b3cda21d1a25 [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 Gautambd0e5782012-08-30 10:39:01 -0700106#define HS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x10)
Manu Gautam8c642812012-06-07 10:35:10 +0530107#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
108#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
109#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
110#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
Manu Gautambd0e5782012-08-30 10:39:01 -0700111#define SS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x30)
Manu Gautam8c642812012-06-07 10:35:10 +0530112
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300113struct dwc3_msm_req_complete {
114 struct list_head list_item;
115 struct usb_request *req;
116 void (*orig_complete)(struct usb_ep *ep,
117 struct usb_request *req);
118};
119
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200120struct dwc3_msm {
121 struct platform_device *dwc3;
122 struct device *dev;
123 void __iomem *base;
124 u32 resource_size;
125 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300126 u8 ep_num_mapping[DBM_MAX_EPS];
127 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
128 struct list_head req_complete_list;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700129 struct clk *ref_clk;
Manu Gautam1742db22012-06-19 13:33:24 +0530130 struct clk *core_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700131 struct clk *iface_clk;
132 struct clk *sleep_clk;
133 struct clk *hsphy_sleep_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530134 struct regulator *hsusb_3p3;
135 struct regulator *hsusb_1p8;
136 struct regulator *hsusb_vddcx;
137 struct regulator *ssusb_1p8;
138 struct regulator *ssusb_vddcx;
139 enum usb_vdd_type ss_vdd_type;
140 enum usb_vdd_type hs_vdd_type;
Manu Gautamb5067272012-07-02 09:53:41 +0530141 struct dwc3_ext_xceiv ext_xceiv;
142 bool resume_pending;
143 atomic_t pm_suspended;
144 atomic_t in_lpm;
145 struct delayed_work resume_work;
146 struct wake_lock wlock;
Manu Gautam8c642812012-06-07 10:35:10 +0530147 struct dwc3_charger charger;
148 struct usb_phy *otg_xceiv;
149 struct delayed_work chg_work;
150 enum usb_chg_state chg_state;
151 u8 dcd_retries;
Manu Gautam60e01352012-05-29 09:00:34 +0530152};
153
154#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
155#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
156#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
157
158#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
159#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
160#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
161
162#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
163#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
164#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
165
166#define USB_PHY_VDD_DIG_VOL_NONE 0 /* uV */
167#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
168#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
169
Manu Gautam60e01352012-05-29 09:00:34 +0530170static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
171 { /* VDD_CX CORNER Voting */
172 [VDD_NONE] = RPM_VREG_CORNER_NONE,
173 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
174 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
175 },
176 { /* VDD_CX Voltage Voting */
177 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
178 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
179 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
180 },
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200181};
182
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300183static struct dwc3_msm *context;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +0300184static u64 dwc3_msm_dma_mask = DMA_BIT_MASK(64);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300185
186/**
187 *
188 * Read register with debug info.
189 *
190 * @base - DWC3 base virtual address.
191 * @offset - register offset.
192 *
193 * @return u32
194 */
195static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
196{
197 u32 val = ioread32(base + offset);
198 return val;
199}
200
201/**
202 * Read register masked field with debug info.
203 *
204 * @base - DWC3 base virtual address.
205 * @offset - register offset.
206 * @mask - register bitmask.
207 *
208 * @return u32
209 */
210static inline u32 dwc3_msm_read_reg_field(void *base,
211 u32 offset,
212 const u32 mask)
213{
214 u32 shift = find_first_bit((void *)&mask, 32);
215 u32 val = ioread32(base + offset);
216 val &= mask; /* clear other bits */
217 val >>= shift;
218 return val;
219}
220
221/**
222 *
223 * Write register with debug info.
224 *
225 * @base - DWC3 base virtual address.
226 * @offset - register offset.
227 * @val - value to write.
228 *
229 */
230static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
231{
232 iowrite32(val, base + offset);
233}
234
235/**
236 * Write register masked field with debug info.
237 *
238 * @base - DWC3 base virtual address.
239 * @offset - register offset.
240 * @mask - register bitmask.
241 * @val - value to write.
242 *
243 */
244static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
245 const u32 mask, u32 val)
246{
247 u32 shift = find_first_bit((void *)&mask, 32);
248 u32 tmp = ioread32(base + offset);
249
250 tmp &= ~mask; /* clear written bits */
251 val = tmp | (val << shift);
252 iowrite32(val, base + offset);
253}
254
255/**
Manu Gautam8c642812012-06-07 10:35:10 +0530256 * Write register and read back masked value to confirm it is written
257 *
258 * @base - DWC3 base virtual address.
259 * @offset - register offset.
260 * @mask - register bitmask specifying what should be updated
261 * @val - value to write.
262 *
263 */
264static inline void dwc3_msm_write_readback(void *base, u32 offset,
265 const u32 mask, u32 val)
266{
267 u32 write_val, tmp = ioread32(base + offset);
268
269 tmp &= ~mask; /* retain other bits */
270 write_val = tmp | val;
271
272 iowrite32(write_val, base + offset);
273
274 /* Read back to see if val was written */
275 tmp = ioread32(base + offset);
276 tmp &= mask; /* clear other bits */
277
278 if (tmp != val)
279 dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n",
280 __func__, val, offset);
281}
282
283/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300284 * Return DBM EP number according to usb endpoint number.
285 *
286 */
287static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep)
288{
289 int i;
290
291 for (i = 0; i < context->dbm_num_eps; i++)
292 if (context->ep_num_mapping[i] == usb_ep)
293 return i;
294
295 return -ENODEV; /* Not found */
296}
297
298/**
299 * Return number of configured DBM endpoints.
300 *
301 */
302static int dwc3_msm_configured_dbm_ep_num(void)
303{
304 int i;
305 int count = 0;
306
307 for (i = 0; i < context->dbm_num_eps; i++)
308 if (context->ep_num_mapping[i])
309 count++;
310
311 return count;
312}
313
314/**
315 * Configure the DBM with the USB3 core event buffer.
316 * This function is called by the SNPS UDC upon initialization.
317 *
318 * @addr - address of the event buffer.
319 * @size - size of the event buffer.
320 *
321 */
322static int dwc3_msm_event_buffer_config(u32 addr, u16 size)
323{
324 dev_dbg(context->dev, "%s\n", __func__);
325
326 dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr);
327 dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ,
328 DBM_GEVNTSIZ_MASK, size);
329
330 return 0;
331}
332
333/**
334 * Reset the DBM registers upon initialization.
335 *
336 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300337static int dwc3_msm_dbm_soft_reset(int enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300338{
339 dev_dbg(context->dev, "%s\n", __func__);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300340 if (enter_reset) {
341 dev_dbg(context->dev, "enter DBM reset\n");
342 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
343 DBM_SFT_RST_MASK, 1);
344 } else {
345 dev_dbg(context->dev, "exit DBM reset\n");
346 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
347 DBM_SFT_RST_MASK, 0);
348 /*enable DBM*/
349 dwc3_msm_write_reg_field(context->base, QSCRATCH_GENERAL_CFG,
350 DBM_EN_MASK, 0x1);
351 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300352
353 return 0;
354}
355
356/**
357 * Soft reset specific DBM ep.
358 * This function is called by the function driver upon events
359 * such as transfer aborting, USB re-enumeration and USB
360 * disconnection.
361 *
362 * @dbm_ep - DBM ep number.
363 * @enter_reset - should we enter a reset state or get out of it.
364 *
365 */
366static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset)
367{
368 dev_dbg(context->dev, "%s\n", __func__);
369
370 if (dbm_ep >= context->dbm_num_eps) {
371 dev_err(context->dev,
372 "%s: Invalid DBM ep index\n", __func__);
373 return -ENODEV;
374 }
375
376 if (enter_reset) {
377 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300378 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300379 } else {
380 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300381 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300382 }
383
384 return 0;
385}
386
387/**
388 * Configure a USB DBM ep to work in BAM mode.
389 *
390 *
391 * @usb_ep - USB physical EP number.
392 * @producer - producer/consumer.
393 * @disable_wb - disable write back to system memory.
394 * @internal_mem - use internal USB memory for data fifo.
395 * @ioc - enable interrupt on completion.
396 *
397 * @return int - DBM ep number.
398 */
399static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe,
400 bool producer, bool disable_wb,
401 bool internal_mem, bool ioc)
402{
403 u8 dbm_ep;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300404 u32 ep_cfg;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300405
406 dev_dbg(context->dev, "%s\n", __func__);
407
Shimrit Malichia00d7322012-08-05 13:56:28 +0300408 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
409
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300410 if (dbm_ep < 0) {
Shimrit Malichia00d7322012-08-05 13:56:28 +0300411 dev_err(context->dev,
412 "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300413 return -ENODEV;
414 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300415 /* First, reset the dbm endpoint */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300416 dwc3_msm_dbm_ep_soft_reset(dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300417
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300418 /* Set ioc bit for dbm_ep if needed */
419 dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300420 DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300421
Shimrit Malichia00d7322012-08-05 13:56:28 +0300422 ep_cfg = (producer ? DBM_PRODUCER : 0) |
423 (disable_wb ? DBM_DISABLE_WB : 0) |
424 (internal_mem ? DBM_INT_RAM_ACC : 0);
425
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300426 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
Shimrit Malichia00d7322012-08-05 13:56:28 +0300427 DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8);
428
429 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM,
430 usb_ep);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300431 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
432 DBM_BAM_PIPE_NUM, bam_pipe);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300433 dwc3_msm_write_reg_field(context->base, DBM_PIPE_CFG, 0x000000ff,
434 0xe4);
435 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP,
436 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300437
438 return dbm_ep;
439}
440
441/**
442 * Configure a USB DBM ep to work in normal mode.
443 *
444 * @usb_ep - USB ep number.
445 *
446 */
447static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep)
448{
449 u8 dbm_ep;
450
451 dev_dbg(context->dev, "%s\n", __func__);
452
453 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
454
455 if (dbm_ep < 0) {
456 dev_err(context->dev,
457 "%s: Invalid usb ep index\n", __func__);
458 return -ENODEV;
459 }
460
461 context->ep_num_mapping[dbm_ep] = 0;
462
463 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), 0);
464
465 /* Reset the dbm endpoint */
466 dwc3_msm_dbm_ep_soft_reset(dbm_ep, true);
467
468 return 0;
469}
470
471/**
472 * Configure the DBM with the BAM's data fifo.
473 * This function is called by the USB BAM Driver
474 * upon initialization.
475 *
476 * @ep - pointer to usb endpoint.
477 * @addr - address of data fifo.
478 * @size - size of data fifo.
479 *
480 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300481int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300482{
483 u8 dbm_ep;
484 struct dwc3_ep *dep = to_dwc3_ep(ep);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300485 u8 bam_pipe = dst_pipe_idx;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300486
487 dev_dbg(context->dev, "%s\n", __func__);
488
Shimrit Malichia00d7322012-08-05 13:56:28 +0300489 dbm_ep = bam_pipe;
490 context->ep_num_mapping[dbm_ep] = dep->number;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300491
492 dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr);
493 dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep),
494 DBM_DATA_FIFO_SIZE_MASK, size);
495
496 return 0;
497}
498
499/**
500* Cleanups for msm endpoint on request complete.
501*
502* Also call original request complete.
503*
504* @usb_ep - pointer to usb_ep instance.
505* @request - pointer to usb_request instance.
506*
507* @return int - 0 on success, negetive on error.
508*/
509static void dwc3_msm_req_complete_func(struct usb_ep *ep,
510 struct usb_request *request)
511{
512 struct dwc3_request *req = to_dwc3_request(request);
513 struct dwc3_ep *dep = to_dwc3_ep(ep);
514 struct dwc3_msm_req_complete *req_complete = NULL;
515
516 /* Find original request complete function and remove it from list */
517 list_for_each_entry(req_complete,
518 &context->req_complete_list,
519 list_item) {
520 if (req_complete->req == request)
521 break;
522 }
523 if (!req_complete || req_complete->req != request) {
524 dev_err(dep->dwc->dev, "%s: could not find the request\n",
525 __func__);
526 return;
527 }
528 list_del(&req_complete->list_item);
529
530 /*
531 * Release another one TRB to the pool since DBM queue took 2 TRBs
532 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
533 * released only one.
534 */
535 if (req->queued)
536 dep->busy_slot++;
537
538 /* Unconfigure dbm ep */
539 dwc3_msm_dbm_ep_unconfig(dep->number);
540
541 /*
542 * If this is the last endpoint we unconfigured, than reset also
543 * the event buffers.
544 */
545 if (0 == dwc3_msm_configured_dbm_ep_num())
546 dwc3_msm_event_buffer_config(0, 0);
547
548 /*
549 * Call original complete function, notice that dwc->lock is already
550 * taken by the caller of this function (dwc3_gadget_giveback()).
551 */
552 request->complete = req_complete->orig_complete;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300553 if (request->complete)
554 request->complete(ep, request);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300555
556 kfree(req_complete);
557}
558
559/**
560* Helper function.
561* See the header of the dwc3_msm_ep_queue function.
562*
563* @dwc3_ep - pointer to dwc3_ep instance.
564* @req - pointer to dwc3_request instance.
565*
566* @return int - 0 on success, negetive on error.
567*/
568static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
569{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300570 struct dwc3_trb *trb;
571 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300572 struct dwc3_gadget_ep_cmd_params params;
573 u32 cmd;
574 int ret = 0;
575
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300576 /* We push the request to the dep->req_queued list to indicate that
577 * this request is issued with start transfer. The request will be out
578 * from this list in 2 cases. The first is that the transfer will be
579 * completed (not if the transfer is endless using a circular TRBs with
580 * with link TRB). The second case is an option to do stop stransfer,
581 * this can be initiated by the function driver when calling dequeue.
582 */
583 req->queued = true;
584 list_add_tail(&req->list, &dep->req_queued);
585
586 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300587 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300588 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300589 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300590
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300591 req->trb = trb;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300592 trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300593 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
594 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300595 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300596
597 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300598 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300599 dep->free_slot++;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300600 memset(trb_link, 0, sizeof *trb_link);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300601
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300602 trb_link->bpl = lower_32_bits(req->trb_dma);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300603 trb_link->bph = DBM_TRB_BIT |
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300604 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
605 trb_link->size = 0;
606 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300607
608 /*
609 * Now start the transfer
610 */
611 memset(&params, 0, sizeof(params));
Shimrit Malichia00d7322012-08-05 13:56:28 +0300612 params.param0 = 0; /* TDAddr High */
613 params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */
614
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300615 cmd = DWC3_DEPCMD_STARTTRANSFER;
616 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
617 if (ret < 0) {
618 dev_dbg(dep->dwc->dev,
619 "%s: failed to send STARTTRANSFER command\n",
620 __func__);
621
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300622 list_del(&req->list);
623 return ret;
624 }
625
626 return ret;
627}
628
629/**
630* Queue a usb request to the DBM endpoint.
631* This function should be called after the endpoint
632* was enabled by the ep_enable.
633*
634* This function prepares special structure of TRBs which
635* is familier with the DBM HW, so it will possible to use
636* this endpoint in DBM mode.
637*
638* The TRBs prepared by this function, is one normal TRB
639* which point to a fake buffer, followed by a link TRB
640* that points to the first TRB.
641*
642* The API of this function follow the regular API of
643* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
644*
645* @usb_ep - pointer to usb_ep instance.
646* @request - pointer to usb_request instance.
647* @gfp_flags - possible flags.
648*
649* @return int - 0 on success, negetive on error.
650*/
651static int dwc3_msm_ep_queue(struct usb_ep *ep,
652 struct usb_request *request, gfp_t gfp_flags)
653{
654 struct dwc3_request *req = to_dwc3_request(request);
655 struct dwc3_ep *dep = to_dwc3_ep(ep);
656 struct dwc3 *dwc = dep->dwc;
657 struct dwc3_msm_req_complete *req_complete;
658 unsigned long flags;
659 int ret = 0;
660 u8 bam_pipe;
661 bool producer;
662 bool disable_wb;
663 bool internal_mem;
664 bool ioc;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300665 u8 speed;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300666
667 if (!(request->udc_priv & MSM_SPS_MODE)) {
668 /* Not SPS mode, call original queue */
669 dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n",
670 __func__);
671
672 return (context->original_ep_ops[dep->number])->queue(ep,
673 request,
674 gfp_flags);
675 }
676
677 if (!dep->endpoint.desc) {
678 dev_err(dwc->dev,
679 "%s: trying to queue request %p to disabled ep %s\n",
680 __func__, request, ep->name);
681 return -EPERM;
682 }
683
684 if (dep->number == 0 || dep->number == 1) {
685 dev_err(dwc->dev,
686 "%s: trying to queue dbm request %p to control ep %s\n",
687 __func__, request, ep->name);
688 return -EPERM;
689 }
690
691 if (dep->free_slot > 0 || dep->busy_slot > 0 ||
692 !list_empty(&dep->request_list) ||
693 !list_empty(&dep->req_queued)) {
694
695 dev_err(dwc->dev,
696 "%s: trying to queue dbm request %p tp ep %s\n",
697 __func__, request, ep->name);
698 return -EPERM;
699 }
700
701 /*
702 * Override req->complete function, but before doing that,
703 * store it's original pointer in the req_complete_list.
704 */
705 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
706 if (!req_complete) {
707 dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__);
708 return -ENOMEM;
709 }
710 req_complete->req = request;
711 req_complete->orig_complete = request->complete;
712 list_add_tail(&req_complete->list_item, &context->req_complete_list);
713 request->complete = dwc3_msm_req_complete_func;
714
715 /*
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300716 * Configure the DBM endpoint
717 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300718 bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300719 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
720 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
721 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
722 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
723
724 ret = dwc3_msm_dbm_ep_config(dep->number,
725 bam_pipe, producer,
726 disable_wb, internal_mem, ioc);
727 if (ret < 0) {
728 dev_err(context->dev,
729 "error %d after calling dwc3_msm_dbm_ep_config\n",
730 ret);
731 return ret;
732 }
733
734 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
735 __func__, request, ep->name, request->length);
736
737 /*
738 * We must obtain the lock of the dwc3 core driver,
739 * including disabling interrupts, so we will be sure
740 * that we are the only ones that configure the HW device
741 * core and ensure that we queuing the request will finish
742 * as soon as possible so we will release back the lock.
743 */
744 spin_lock_irqsave(&dwc->lock, flags);
745 ret = __dwc3_msm_ep_queue(dep, req);
746 spin_unlock_irqrestore(&dwc->lock, flags);
747 if (ret < 0) {
748 dev_err(context->dev,
749 "error %d after calling __dwc3_msm_ep_queue\n", ret);
750 return ret;
751 }
752
Shimrit Malichia00d7322012-08-05 13:56:28 +0300753 speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD;
754 dwc3_msm_write_reg(context->base, DBM_GEN_CFG, speed >> 2);
755
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300756 return 0;
757}
758
759/**
760 * Configure MSM endpoint.
761 * This function do specific configurations
762 * to an endpoint which need specific implementaion
763 * in the MSM architecture.
764 *
765 * This function should be called by usb function/class
766 * layer which need a support from the specific MSM HW
767 * which wrap the USB3 core. (like DBM specific endpoints)
768 *
769 * @ep - a pointer to some usb_ep instance
770 *
771 * @return int - 0 on success, negetive on error.
772 */
773int msm_ep_config(struct usb_ep *ep)
774{
775 struct dwc3_ep *dep = to_dwc3_ep(ep);
776 struct usb_ep_ops *new_ep_ops;
777
778 /* Save original ep ops for future restore*/
779 if (context->original_ep_ops[dep->number]) {
780 dev_err(context->dev,
781 "ep [%s,%d] already configured as msm endpoint\n",
782 ep->name, dep->number);
783 return -EPERM;
784 }
785 context->original_ep_ops[dep->number] = ep->ops;
786
787 /* Set new usb ops as we like */
788 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
789 if (!new_ep_ops) {
790 dev_err(context->dev,
791 "%s: unable to allocate mem for new usb ep ops\n",
792 __func__);
793 return -ENOMEM;
794 }
795 (*new_ep_ops) = (*ep->ops);
796 new_ep_ops->queue = dwc3_msm_ep_queue;
797 ep->ops = new_ep_ops;
798
799 /*
800 * Do HERE more usb endpoint configurations
801 * which are specific to MSM.
802 */
803
804 return 0;
805}
806EXPORT_SYMBOL(msm_ep_config);
807
808/**
809 * Un-configure MSM endpoint.
810 * Tear down configurations done in the
811 * dwc3_msm_ep_config function.
812 *
813 * @ep - a pointer to some usb_ep instance
814 *
815 * @return int - 0 on success, negetive on error.
816 */
817int msm_ep_unconfig(struct usb_ep *ep)
818{
819 struct dwc3_ep *dep = to_dwc3_ep(ep);
820 struct usb_ep_ops *old_ep_ops;
821
822 /* Restore original ep ops */
823 if (!context->original_ep_ops[dep->number]) {
824 dev_err(context->dev,
825 "ep [%s,%d] was not configured as msm endpoint\n",
826 ep->name, dep->number);
827 return -EINVAL;
828 }
829 old_ep_ops = (struct usb_ep_ops *)ep->ops;
830 ep->ops = context->original_ep_ops[dep->number];
831 context->original_ep_ops[dep->number] = NULL;
832 kfree(old_ep_ops);
833
834 /*
835 * Do HERE more usb endpoint un-configurations
836 * which are specific to MSM.
837 */
838
839 return 0;
840}
841EXPORT_SYMBOL(msm_ep_unconfig);
842
Manu Gautam60e01352012-05-29 09:00:34 +0530843/* HSPHY */
844static int dwc3_hsusb_config_vddcx(int high)
845{
846 int min_vol, ret;
847 struct dwc3_msm *dwc = context;
848 enum usb_vdd_type vdd_type = context->hs_vdd_type;
849 int max_vol = vdd_val[vdd_type][VDD_MAX];
850
851 min_vol = vdd_val[vdd_type][high ? VDD_MIN : VDD_NONE];
852 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
853 if (ret) {
854 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
855 return ret;
856 }
857
858 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
859 min_vol, max_vol);
860
861 return ret;
862}
863
864static int dwc3_hsusb_ldo_init(int init)
865{
866 int rc = 0;
867 struct dwc3_msm *dwc = context;
868
869 if (!init) {
870 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
871 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
872 return 0;
873 }
874
875 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
876 if (IS_ERR(dwc->hsusb_3p3)) {
877 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
878 return PTR_ERR(dwc->hsusb_3p3);
879 }
880
881 rc = regulator_set_voltage(dwc->hsusb_3p3,
882 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
883 if (rc) {
884 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
885 return rc;
886 }
887 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
888 if (IS_ERR(dwc->hsusb_1p8)) {
889 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
890 rc = PTR_ERR(dwc->hsusb_1p8);
891 goto devote_3p3;
892 }
893 rc = regulator_set_voltage(dwc->hsusb_1p8,
894 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
895 if (rc) {
896 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
897 goto devote_3p3;
898 }
899
900 return 0;
901
902devote_3p3:
903 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
904
905 return rc;
906}
907
908static int dwc3_hsusb_ldo_enable(int on)
909{
910 int rc = 0;
911 struct dwc3_msm *dwc = context;
912
913 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
914
915 if (!on)
916 goto disable_regulators;
917
918
919 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
920 if (rc < 0) {
921 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
922 return rc;
923 }
924
925 rc = regulator_enable(dwc->hsusb_1p8);
926 if (rc) {
927 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
928 goto put_1p8_lpm;
929 }
930
931 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
932 if (rc < 0) {
933 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
934 goto disable_1p8;
935 }
936
937 rc = regulator_enable(dwc->hsusb_3p3);
938 if (rc) {
939 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
940 goto put_3p3_lpm;
941 }
942
943 return 0;
944
945disable_regulators:
946 rc = regulator_disable(dwc->hsusb_3p3);
947 if (rc)
948 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
949
950put_3p3_lpm:
951 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
952 if (rc < 0)
953 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
954
955disable_1p8:
956 rc = regulator_disable(dwc->hsusb_1p8);
957 if (rc)
958 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
959
960put_1p8_lpm:
961 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
962 if (rc < 0)
963 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
964
965 return rc < 0 ? rc : 0;
966}
967
968/* SSPHY */
969static int dwc3_ssusb_config_vddcx(int high)
970{
971 int min_vol, ret;
972 struct dwc3_msm *dwc = context;
973 enum usb_vdd_type vdd_type = context->ss_vdd_type;
974 int max_vol = vdd_val[vdd_type][VDD_MAX];
975
976 min_vol = vdd_val[vdd_type][high ? VDD_MIN : VDD_NONE];
977 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
978 if (ret) {
979 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
980 return ret;
981 }
982
983 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
984 min_vol, max_vol);
985 return ret;
986}
987
988/* 3.3v supply not needed for SS PHY */
989static int dwc3_ssusb_ldo_init(int init)
990{
991 int rc = 0;
992 struct dwc3_msm *dwc = context;
993
994 if (!init) {
995 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
996 return 0;
997 }
998
999 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
1000 if (IS_ERR(dwc->ssusb_1p8)) {
1001 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
1002 return PTR_ERR(dwc->ssusb_1p8);
1003 }
1004 rc = regulator_set_voltage(dwc->ssusb_1p8,
1005 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
1006 if (rc)
1007 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
1008
1009 return rc;
1010}
1011
1012static int dwc3_ssusb_ldo_enable(int on)
1013{
1014 int rc = 0;
1015 struct dwc3_msm *dwc = context;
1016
1017 dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1018
1019 if (!on)
1020 goto disable_regulators;
1021
1022
1023 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1024 if (rc < 0) {
1025 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1026 return rc;
1027 }
1028
1029 rc = regulator_enable(dwc->ssusb_1p8);
1030 if (rc) {
1031 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1032 goto put_1p8_lpm;
1033 }
1034
1035 return 0;
1036
1037disable_regulators:
1038 rc = regulator_disable(dwc->ssusb_1p8);
1039 if (rc)
1040 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1041
1042put_1p8_lpm:
1043 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1044 if (rc < 0)
1045 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1046
1047 return rc < 0 ? rc : 0;
1048}
1049
Manu Gautam8c642812012-06-07 10:35:10 +05301050static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1051{
1052 u32 chg_ctrl;
1053
1054 /* Turn off VDP_SRC */
1055 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1056 msleep(20);
1057
1058 /* Before proceeding make sure VDP_SRC is OFF */
1059 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1060 if (chg_ctrl & 0x3F)
1061 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1062 __func__, chg_ctrl);
1063 /*
1064 * Configure DM as current source, DP as current sink
1065 * and enable battery charging comparators.
1066 */
1067 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1068}
1069
1070static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1071{
1072 u32 chg_det;
1073 bool ret = false;
1074
1075 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1076 ret = chg_det & 1;
1077
1078 return ret;
1079}
1080
1081static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1082{
1083 /*
1084 * Configure DP as current source, DM as current sink
1085 * and enable battery charging comparators.
1086 */
1087 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1088}
1089
1090static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1091{
1092 u32 chg_state;
1093 bool ret = false;
1094
1095 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1096 ret = chg_state & 2;
1097
1098 return ret;
1099}
1100
1101static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1102{
1103 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1104}
1105
1106static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1107{
1108 /* Data contact detection enable, DCDENB */
1109 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1110}
1111
1112static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1113{
1114 u32 chg_ctrl;
1115
1116 /* Clear charger detecting control bits */
1117 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1118
1119 /* Clear alt interrupt latch and enable bits */
1120 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1121 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1122
1123 udelay(100);
1124
1125 /* Before proceeding make sure charger block is RESET */
1126 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1127 if (chg_ctrl & 0x3F)
1128 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1129 __func__, chg_ctrl);
1130}
1131
1132static const char *chg_to_string(enum dwc3_chg_type chg_type)
1133{
1134 switch (chg_type) {
1135 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1136 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1137 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1138 default: return "INVALID_CHARGER";
1139 }
1140}
1141
1142#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1143#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1144#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1145#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1146
1147static void dwc3_chg_detect_work(struct work_struct *w)
1148{
1149 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1150 bool is_dcd = false, tmout, vout;
1151 unsigned long delay;
1152
1153 dev_dbg(mdwc->dev, "chg detection work\n");
1154 switch (mdwc->chg_state) {
1155 case USB_CHG_STATE_UNDEFINED:
1156 dwc3_chg_block_reset(mdwc);
1157 dwc3_chg_enable_dcd(mdwc);
1158 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1159 mdwc->dcd_retries = 0;
1160 delay = DWC3_CHG_DCD_POLL_TIME;
1161 break;
1162 case USB_CHG_STATE_WAIT_FOR_DCD:
1163 is_dcd = dwc3_chg_check_dcd(mdwc);
1164 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1165 if (is_dcd || tmout) {
1166 dwc3_chg_disable_dcd(mdwc);
1167 dwc3_chg_enable_primary_det(mdwc);
1168 delay = DWC3_CHG_PRIMARY_DET_TIME;
1169 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1170 } else {
1171 delay = DWC3_CHG_DCD_POLL_TIME;
1172 }
1173 break;
1174 case USB_CHG_STATE_DCD_DONE:
1175 vout = dwc3_chg_det_check_output(mdwc);
1176 if (vout) {
1177 dwc3_chg_enable_secondary_det(mdwc);
1178 delay = DWC3_CHG_SECONDARY_DET_TIME;
1179 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1180 } else {
1181 mdwc->charger.chg_type = USB_SDP_CHARGER;
1182 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1183 delay = 0;
1184 }
1185 break;
1186 case USB_CHG_STATE_PRIMARY_DONE:
1187 vout = dwc3_chg_det_check_output(mdwc);
1188 if (vout)
1189 mdwc->charger.chg_type = USB_DCP_CHARGER;
1190 else
1191 mdwc->charger.chg_type = USB_CDP_CHARGER;
1192 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1193 /* fall through */
1194 case USB_CHG_STATE_SECONDARY_DONE:
1195 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1196 /* fall through */
1197 case USB_CHG_STATE_DETECTED:
1198 dwc3_chg_block_reset(mdwc);
1199 dev_dbg(mdwc->dev, "chg_type = %s\n",
1200 chg_to_string(mdwc->charger.chg_type));
1201 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1202 &mdwc->charger);
1203 return;
1204 default:
1205 return;
1206 }
1207
1208 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1209}
1210
1211static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1212{
1213 struct dwc3_msm *mdwc = context;
1214
1215 if (start == false) {
1216 cancel_delayed_work_sync(&mdwc->chg_work);
1217 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1218 charger->chg_type = DWC3_INVALID_CHARGER;
1219 return;
1220 }
1221
1222 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1223 charger->chg_type = DWC3_INVALID_CHARGER;
1224 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1225}
1226
Manu Gautamb5067272012-07-02 09:53:41 +05301227static int dwc3_msm_suspend(struct dwc3_msm *mdwc)
1228{
1229 dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__);
1230
1231 if (atomic_read(&mdwc->in_lpm)) {
1232 dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__);
1233 return 0;
1234 }
1235
Manu Gautam3e9ad352012-08-16 14:44:47 -07001236 clk_disable_unprepare(mdwc->iface_clk);
Manu Gautamb5067272012-07-02 09:53:41 +05301237 clk_disable_unprepare(mdwc->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001238 clk_disable_unprepare(mdwc->ref_clk);
Manu Gautamb5067272012-07-02 09:53:41 +05301239 dwc3_hsusb_ldo_enable(0);
1240 dwc3_ssusb_ldo_enable(0);
1241 wake_unlock(&mdwc->wlock);
1242
1243 atomic_set(&mdwc->in_lpm, 1);
1244 dev_info(mdwc->dev, "DWC3 in low power mode\n");
1245
1246 return 0;
1247}
1248
1249static int dwc3_msm_resume(struct dwc3_msm *mdwc)
1250{
1251 dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__);
1252
1253 if (!atomic_read(&mdwc->in_lpm)) {
1254 dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__);
1255 return 0;
1256 }
1257
1258 wake_lock(&mdwc->wlock);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001259 clk_prepare_enable(mdwc->ref_clk);
Manu Gautamb5067272012-07-02 09:53:41 +05301260 clk_prepare_enable(mdwc->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001261 clk_prepare_enable(mdwc->iface_clk);
Manu Gautamb5067272012-07-02 09:53:41 +05301262 dwc3_hsusb_ldo_enable(1);
1263 dwc3_ssusb_ldo_enable(1);
1264
1265 atomic_set(&mdwc->in_lpm, 0);
1266 dev_info(mdwc->dev, "DWC3 exited from low power mode\n");
1267
1268 return 0;
1269}
1270
1271static void dwc3_resume_work(struct work_struct *w)
1272{
1273 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1274 resume_work.work);
1275
1276 dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__);
1277 /* handle any event that was queued while work was already running */
1278 if (!atomic_read(&mdwc->in_lpm)) {
1279 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1280 if (mdwc->otg_xceiv)
1281 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1282 DWC3_EVENT_XCEIV_STATE);
1283 return;
1284 }
1285
1286 /* bail out if system resume in process, else initiate RESUME */
1287 if (atomic_read(&mdwc->pm_suspended)) {
1288 mdwc->resume_pending = true;
1289 } else {
1290 pm_runtime_get_sync(mdwc->dev);
1291 if (mdwc->otg_xceiv)
1292 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1293 DWC3_EVENT_PHY_RESUME);
1294 pm_runtime_put_sync(mdwc->dev);
1295 }
1296}
1297
1298static bool debug_id, debug_bsv, debug_connect;
1299
1300static int dwc3_connect_show(struct seq_file *s, void *unused)
1301{
1302 if (debug_connect)
1303 seq_printf(s, "true\n");
1304 else
1305 seq_printf(s, "false\n");
1306
1307 return 0;
1308}
1309
1310static int dwc3_connect_open(struct inode *inode, struct file *file)
1311{
1312 return single_open(file, dwc3_connect_show, inode->i_private);
1313}
1314
1315static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf,
1316 size_t count, loff_t *ppos)
1317{
1318 struct seq_file *s = file->private_data;
1319 struct dwc3_msm *mdwc = s->private;
1320 char buf[8];
1321
1322 memset(buf, 0x00, sizeof(buf));
1323
1324 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1325 return -EFAULT;
1326
1327 if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) {
1328 debug_connect = true;
1329 } else {
1330 debug_connect = debug_bsv = false;
1331 debug_id = true;
1332 }
1333
1334 mdwc->ext_xceiv.bsv = debug_bsv;
1335 mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND;
1336
1337 if (atomic_read(&mdwc->in_lpm)) {
1338 dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__);
1339 dwc3_resume_work(&mdwc->resume_work.work);
1340 } else {
1341 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1342 if (mdwc->otg_xceiv)
1343 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1344 DWC3_EVENT_XCEIV_STATE);
1345 }
1346
1347 return count;
1348}
1349
1350const struct file_operations dwc3_connect_fops = {
1351 .open = dwc3_connect_open,
1352 .read = seq_read,
1353 .write = dwc3_connect_write,
1354 .llseek = seq_lseek,
1355 .release = single_release,
1356};
1357
1358static struct dentry *dwc3_debugfs_root;
1359
1360static void dwc3_debugfs_init(struct dwc3_msm *mdwc)
1361{
1362 dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL);
1363
1364 if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root))
1365 return;
1366
1367 if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
1368 (u32 *)&debug_id))
1369 goto error;
1370
1371 if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
1372 (u32 *)&debug_bsv))
1373 goto error;
1374
1375 if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR,
1376 dwc3_debugfs_root, mdwc, &dwc3_connect_fops))
1377 goto error;
1378
1379 return;
1380
1381error:
1382 debugfs_remove_recursive(dwc3_debugfs_root);
1383}
Manu Gautam8c642812012-06-07 10:35:10 +05301384
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001385static int __devinit dwc3_msm_probe(struct platform_device *pdev)
1386{
1387 struct device_node *node = pdev->dev.of_node;
1388 struct platform_device *dwc3;
1389 struct dwc3_msm *msm;
1390 struct resource *res;
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03001391 void __iomem *tcsr;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001392 int ret = 0;
1393
1394 msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL);
1395 if (!msm) {
1396 dev_err(&pdev->dev, "not enough memory\n");
1397 return -ENOMEM;
1398 }
1399
1400 platform_set_drvdata(pdev, msm);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001401 context = msm;
Manu Gautam60e01352012-05-29 09:00:34 +05301402 msm->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001403
1404 INIT_LIST_HEAD(&msm->req_complete_list);
Manu Gautam8c642812012-06-07 10:35:10 +05301405 INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work);
Manu Gautamb5067272012-07-02 09:53:41 +05301406 INIT_DELAYED_WORK(&msm->resume_work, dwc3_resume_work);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001407
Manu Gautam1742db22012-06-19 13:33:24 +05301408 /*
1409 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
1410 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
1411 */
1412 msm->core_clk = devm_clk_get(&pdev->dev, "core_clk");
1413 if (IS_ERR(msm->core_clk)) {
1414 dev_err(&pdev->dev, "failed to get core_clk\n");
1415 return PTR_ERR(msm->core_clk);
1416 }
1417 clk_set_rate(msm->core_clk, 125000000);
1418 clk_prepare_enable(msm->core_clk);
1419
Manu Gautam3e9ad352012-08-16 14:44:47 -07001420 msm->iface_clk = devm_clk_get(&pdev->dev, "iface_clk");
1421 if (IS_ERR(msm->iface_clk)) {
1422 dev_err(&pdev->dev, "failed to get iface_clk\n");
1423 ret = PTR_ERR(msm->iface_clk);
1424 goto disable_core_clk;
1425 }
1426 clk_prepare_enable(msm->iface_clk);
1427
1428 msm->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
1429 if (IS_ERR(msm->sleep_clk)) {
1430 dev_err(&pdev->dev, "failed to get sleep_clk\n");
1431 ret = PTR_ERR(msm->sleep_clk);
1432 goto disable_iface_clk;
1433 }
1434 clk_prepare_enable(msm->sleep_clk);
1435
1436 msm->hsphy_sleep_clk = devm_clk_get(&pdev->dev, "sleep_a_clk");
1437 if (IS_ERR(msm->hsphy_sleep_clk)) {
1438 dev_err(&pdev->dev, "failed to get sleep_a_clk\n");
1439 ret = PTR_ERR(msm->hsphy_sleep_clk);
1440 goto disable_sleep_clk;
1441 }
1442 clk_prepare_enable(msm->hsphy_sleep_clk);
1443
1444 msm->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
1445 if (IS_ERR(msm->ref_clk)) {
1446 dev_err(&pdev->dev, "failed to get ref_clk\n");
1447 ret = PTR_ERR(msm->ref_clk);
1448 goto disable_sleep_a_clk;
1449 }
1450 clk_prepare_enable(msm->ref_clk);
1451
Manu Gautam60e01352012-05-29 09:00:34 +05301452 /* SS PHY */
1453 msm->ss_vdd_type = VDDCX_CORNER;
1454 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
1455 if (IS_ERR(msm->ssusb_vddcx)) {
1456 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev,
1457 "SSUSB_VDDCX");
1458 if (IS_ERR(msm->ssusb_vddcx)) {
1459 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
Manu Gautam1742db22012-06-19 13:33:24 +05301460 ret = PTR_ERR(msm->ssusb_vddcx);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001461 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05301462 }
1463 msm->ss_vdd_type = VDDCX;
1464 dev_dbg(&pdev->dev, "ss_vdd_type: VDDCX\n");
1465 }
1466
1467 ret = dwc3_ssusb_config_vddcx(1);
1468 if (ret) {
1469 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam3e9ad352012-08-16 14:44:47 -07001470 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05301471 }
1472
1473 ret = regulator_enable(context->ssusb_vddcx);
1474 if (ret) {
1475 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
1476 goto unconfig_ss_vddcx;
1477 }
1478
1479 ret = dwc3_ssusb_ldo_init(1);
1480 if (ret) {
1481 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
1482 goto disable_ss_vddcx;
1483 }
1484
1485 ret = dwc3_ssusb_ldo_enable(1);
1486 if (ret) {
1487 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
1488 goto free_ss_ldo_init;
1489 }
1490
1491 /* HS PHY */
1492 msm->hs_vdd_type = VDDCX_CORNER;
1493 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
1494 if (IS_ERR(msm->hsusb_vddcx)) {
1495 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev,
1496 "HSUSB_VDDCX");
1497 if (IS_ERR(msm->hsusb_vddcx)) {
1498 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
1499 ret = PTR_ERR(msm->ssusb_vddcx);
1500 goto disable_ss_ldo;
1501 }
1502 msm->hs_vdd_type = VDDCX;
1503 dev_dbg(&pdev->dev, "hs_vdd_type: VDDCX\n");
1504 }
1505
1506 ret = dwc3_hsusb_config_vddcx(1);
1507 if (ret) {
1508 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1509 goto disable_ss_ldo;
1510 }
1511
1512 ret = regulator_enable(context->hsusb_vddcx);
1513 if (ret) {
1514 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
1515 goto unconfig_hs_vddcx;
1516 }
1517
1518 ret = dwc3_hsusb_ldo_init(1);
1519 if (ret) {
1520 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1521 goto disable_hs_vddcx;
1522 }
1523
1524 ret = dwc3_hsusb_ldo_enable(1);
1525 if (ret) {
1526 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1527 goto free_hs_ldo_init;
1528 }
1529
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03001530 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1531 if (!res) {
1532 dev_dbg(&pdev->dev, "missing TCSR memory resource\n");
1533 } else {
1534 tcsr = devm_ioremap_nocache(&pdev->dev, res->start,
1535 resource_size(res));
1536 if (!tcsr) {
1537 dev_dbg(&pdev->dev, "tcsr ioremap failed\n");
1538 } else {
1539 /* Enable USB3 on the primary USB port. */
1540 writel_relaxed(0x1, tcsr);
1541 /*
1542 * Ensure that TCSR write is completed before
1543 * USB registers initialization.
1544 */
1545 mb();
1546 }
1547 }
1548
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001549 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1550 if (!res) {
1551 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301552 ret = -ENODEV;
1553 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001554 }
1555
1556 msm->base = devm_ioremap_nocache(&pdev->dev, res->start,
1557 resource_size(res));
1558 if (!msm->base) {
1559 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301560 ret = -ENODEV;
1561 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001562 }
1563
Ido Shayevitzca2691e2012-04-17 15:54:53 +03001564 dwc3 = platform_device_alloc("dwc3", -1);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001565 if (!dwc3) {
1566 dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301567 ret = -ENODEV;
1568 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001569 }
1570
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001571 dwc3->dev.parent = &pdev->dev;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +03001572 dwc3->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1573 dwc3->dev.dma_mask = &dwc3_msm_dma_mask;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001574 dwc3->dev.dma_parms = pdev->dev.dma_parms;
1575 msm->resource_size = resource_size(res);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001576 msm->dwc3 = dwc3;
1577
Manu Gautambd0e5782012-08-30 10:39:01 -07001578 /* SSPHY Initialization: Use ref_clk from pads and set its parameters */
1579 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1580 msleep(30);
1581 /* Assert SSPHY reset */
1582 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210082);
1583 usleep_range(2000, 2200);
1584 /* De-assert SSPHY reset - power and ref_clock must be ON */
1585 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1586 usleep_range(2000, 2200);
1587 /* Ref clock must be stable now, enable ref clock for HS mode */
1588 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210102);
1589 usleep_range(2000, 2200);
1590 /*
1591 * HSPHY Initialization: Enable UTMI clock and clamp enable HVINTs,
1592 * and disable RETENTION (power-on default is ENABLED)
1593 */
1594 dwc3_msm_write_reg(msm->base, HS_PHY_CTRL_REG, 0x5220bb2);
1595 usleep_range(2000, 2200);
1596 /* Disable (bypass) VBUS filter */
1597 dwc3_msm_write_reg(msm->base, QSCRATCH_GENERAL_CFG, 0x38);
1598
Manu Gautamb5067272012-07-02 09:53:41 +05301599 pm_runtime_set_active(msm->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05301600
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001601 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
1602 &msm->dbm_num_eps)) {
1603 dev_err(&pdev->dev,
1604 "unable to read platform data num of dbm eps\n");
1605 msm->dbm_num_eps = DBM_MAX_EPS;
1606 }
1607
1608 if (msm->dbm_num_eps > DBM_MAX_EPS) {
1609 dev_err(&pdev->dev,
1610 "Driver doesn't support number of DBM EPs. "
1611 "max: %d, dbm_num_eps: %d\n",
1612 DBM_MAX_EPS, msm->dbm_num_eps);
1613 ret = -ENODEV;
Manu Gautam60e01352012-05-29 09:00:34 +05301614 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001615 }
1616
1617 ret = platform_device_add_resources(dwc3, pdev->resource,
1618 pdev->num_resources);
1619 if (ret) {
1620 dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301621 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001622 }
1623
1624 ret = platform_device_add(dwc3);
1625 if (ret) {
1626 dev_err(&pdev->dev, "failed to register dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301627 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001628 }
1629
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001630 /* Reset the DBM */
Shimrit Malichia00d7322012-08-05 13:56:28 +03001631 dwc3_msm_dbm_soft_reset(1);
1632 usleep_range(1000, 1200);
1633 dwc3_msm_dbm_soft_reset(0);
1634
1635 dwc3_msm_event_buffer_config(dwc3_readl(msm->base, DWC3_GEVNTADRLO(0)),
1636 dwc3_readl(msm->base, DWC3_GEVNTSIZ(0)));
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001637
Manu Gautam8c642812012-06-07 10:35:10 +05301638 msm->otg_xceiv = usb_get_transceiver();
1639 if (msm->otg_xceiv) {
1640 msm->charger.start_detection = dwc3_start_chg_det;
1641 ret = dwc3_set_charger(msm->otg_xceiv->otg, &msm->charger);
1642 if (ret || !msm->charger.notify_detection_complete) {
1643 dev_err(&pdev->dev, "failed to register charger: %d\n",
1644 ret);
1645 goto put_xcvr;
1646 }
Manu Gautamb5067272012-07-02 09:53:41 +05301647
1648 ret = dwc3_set_ext_xceiv(msm->otg_xceiv->otg, &msm->ext_xceiv);
1649 if (ret || !msm->ext_xceiv.notify_ext_events) {
1650 dev_err(&pdev->dev, "failed to register xceiver: %d\n",
1651 ret);
1652 goto put_xcvr;
1653 }
Manu Gautam8c642812012-06-07 10:35:10 +05301654 } else {
1655 dev_err(&pdev->dev, "%s: No OTG transceiver found\n", __func__);
1656 }
1657
Manu Gautamb5067272012-07-02 09:53:41 +05301658 wake_lock_init(&msm->wlock, WAKE_LOCK_SUSPEND, "msm_dwc3");
1659 wake_lock(&msm->wlock);
1660 dwc3_debugfs_init(msm);
1661
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001662 return 0;
1663
Manu Gautam8c642812012-06-07 10:35:10 +05301664put_xcvr:
1665 usb_put_transceiver(msm->otg_xceiv);
1666 platform_device_del(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05301667put_pdev:
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001668 platform_device_put(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05301669disable_hs_ldo:
1670 dwc3_hsusb_ldo_enable(0);
1671free_hs_ldo_init:
1672 dwc3_hsusb_ldo_init(0);
1673disable_hs_vddcx:
1674 regulator_disable(context->hsusb_vddcx);
1675unconfig_hs_vddcx:
1676 dwc3_hsusb_config_vddcx(0);
1677disable_ss_ldo:
1678 dwc3_ssusb_ldo_enable(0);
1679free_ss_ldo_init:
1680 dwc3_ssusb_ldo_init(0);
1681disable_ss_vddcx:
1682 regulator_disable(context->ssusb_vddcx);
1683unconfig_ss_vddcx:
1684 dwc3_ssusb_config_vddcx(0);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001685disable_ref_clk:
1686 clk_disable_unprepare(msm->ref_clk);
1687disable_sleep_a_clk:
1688 clk_disable_unprepare(msm->hsphy_sleep_clk);
1689disable_sleep_clk:
1690 clk_disable_unprepare(msm->sleep_clk);
1691disable_iface_clk:
1692 clk_disable_unprepare(msm->iface_clk);
Manu Gautam1742db22012-06-19 13:33:24 +05301693disable_core_clk:
1694 clk_disable_unprepare(msm->core_clk);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001695
1696 return ret;
1697}
1698
1699static int __devexit dwc3_msm_remove(struct platform_device *pdev)
1700{
1701 struct dwc3_msm *msm = platform_get_drvdata(pdev);
1702
Manu Gautamb5067272012-07-02 09:53:41 +05301703 if (dwc3_debugfs_root)
1704 debugfs_remove_recursive(dwc3_debugfs_root);
Manu Gautam8c642812012-06-07 10:35:10 +05301705 if (msm->otg_xceiv) {
1706 dwc3_start_chg_det(&msm->charger, false);
1707 usb_put_transceiver(msm->otg_xceiv);
1708 }
Manu Gautamb5067272012-07-02 09:53:41 +05301709 pm_runtime_disable(msm->dev);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001710 platform_device_unregister(msm->dwc3);
Manu Gautamb5067272012-07-02 09:53:41 +05301711 wake_lock_destroy(&msm->wlock);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001712
Manu Gautam60e01352012-05-29 09:00:34 +05301713 dwc3_hsusb_ldo_enable(0);
1714 dwc3_hsusb_ldo_init(0);
1715 regulator_disable(msm->hsusb_vddcx);
1716 dwc3_hsusb_config_vddcx(0);
1717 dwc3_ssusb_ldo_enable(0);
1718 dwc3_ssusb_ldo_init(0);
1719 regulator_disable(msm->ssusb_vddcx);
1720 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05301721 clk_disable_unprepare(msm->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001722 clk_disable_unprepare(msm->iface_clk);
1723 clk_disable_unprepare(msm->sleep_clk);
1724 clk_disable_unprepare(msm->hsphy_sleep_clk);
1725 clk_disable_unprepare(msm->ref_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05301726
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001727 return 0;
1728}
1729
Manu Gautamb5067272012-07-02 09:53:41 +05301730static int dwc3_msm_pm_suspend(struct device *dev)
1731{
1732 int ret = 0;
1733 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1734
1735 dev_dbg(dev, "dwc3-msm PM suspend\n");
1736
1737 ret = dwc3_msm_suspend(mdwc);
1738 if (!ret)
1739 atomic_set(&mdwc->pm_suspended, 1);
1740
1741 return ret;
1742}
1743
1744static int dwc3_msm_pm_resume(struct device *dev)
1745{
1746 int ret = 0;
1747 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1748
1749 dev_dbg(dev, "dwc3-msm PM resume\n");
1750
1751 atomic_set(&mdwc->pm_suspended, 0);
1752 if (mdwc->resume_pending) {
1753 mdwc->resume_pending = false;
1754
1755 ret = dwc3_msm_resume(mdwc);
1756 /* Update runtime PM status */
1757 pm_runtime_disable(dev);
1758 pm_runtime_set_active(dev);
1759 pm_runtime_enable(dev);
1760
1761 /* Let OTG know about resume event and update pm_count */
1762 if (mdwc->otg_xceiv)
1763 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1764 DWC3_EVENT_PHY_RESUME);
1765 }
1766
1767 return ret;
1768}
1769
1770static int dwc3_msm_runtime_idle(struct device *dev)
1771{
1772 dev_dbg(dev, "DWC3-msm runtime idle\n");
1773
1774 return 0;
1775}
1776
1777static int dwc3_msm_runtime_suspend(struct device *dev)
1778{
1779 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1780
1781 dev_dbg(dev, "DWC3-msm runtime suspend\n");
1782
1783 return dwc3_msm_suspend(mdwc);
1784}
1785
1786static int dwc3_msm_runtime_resume(struct device *dev)
1787{
1788 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1789
1790 dev_dbg(dev, "DWC3-msm runtime resume\n");
1791
1792 return dwc3_msm_resume(mdwc);
1793}
1794
1795static const struct dev_pm_ops dwc3_msm_dev_pm_ops = {
1796 SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume)
1797 SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume,
1798 dwc3_msm_runtime_idle)
1799};
1800
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001801static const struct of_device_id of_dwc3_matach[] = {
1802 {
1803 .compatible = "qcom,dwc-usb3-msm",
1804 },
1805 { },
1806};
1807MODULE_DEVICE_TABLE(of, of_dwc3_matach);
1808
1809static struct platform_driver dwc3_msm_driver = {
1810 .probe = dwc3_msm_probe,
1811 .remove = __devexit_p(dwc3_msm_remove),
1812 .driver = {
1813 .name = "msm-dwc3",
Manu Gautamb5067272012-07-02 09:53:41 +05301814 .pm = &dwc3_msm_dev_pm_ops,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001815 .of_match_table = of_dwc3_matach,
1816 },
1817};
1818
1819MODULE_LICENSE("GPLV2");
1820MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
1821
1822static int __devinit dwc3_msm_init(void)
1823{
1824 return platform_driver_register(&dwc3_msm_driver);
1825}
1826module_init(dwc3_msm_init);
1827
1828static void __exit dwc3_msm_exit(void)
1829{
1830 platform_driver_unregister(&dwc3_msm_driver);
1831}
1832module_exit(dwc3_msm_exit);