blob: b71bd3e050708b957bec8091dca86e0294f97146 [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>
Manu Gautam2617deb2012-08-31 17:50:06 -070037#include <mach/msm_bus.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030038
Manu Gautam8c642812012-06-07 10:35:10 +053039#include "dwc3_otg.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030040#include "core.h"
41#include "gadget.h"
42
43/**
44 * USB DBM Hardware registers.
45 *
46 */
Shimrit Malichia00d7322012-08-05 13:56:28 +030047#define DBM_BASE 0x000F8000
48#define DBM_EP_CFG(n) (DBM_BASE + (0x00 + 4 * (n)))
49#define DBM_DATA_FIFO(n) (DBM_BASE + (0x10 + 4 * (n)))
50#define DBM_DATA_FIFO_SIZE(n) (DBM_BASE + (0x20 + 4 * (n)))
51#define DBM_DATA_FIFO_EN (DBM_BASE + (0x30))
52#define DBM_GEVNTADR (DBM_BASE + (0x34))
53#define DBM_GEVNTSIZ (DBM_BASE + (0x38))
54#define DBM_DBG_CNFG (DBM_BASE + (0x3C))
55#define DBM_HW_TRB0_EP(n) (DBM_BASE + (0x40 + 4 * (n)))
56#define DBM_HW_TRB1_EP(n) (DBM_BASE + (0x50 + 4 * (n)))
57#define DBM_HW_TRB2_EP(n) (DBM_BASE + (0x60 + 4 * (n)))
58#define DBM_HW_TRB3_EP(n) (DBM_BASE + (0x70 + 4 * (n)))
59#define DBM_PIPE_CFG (DBM_BASE + (0x80))
60#define DBM_SOFT_RESET (DBM_BASE + (0x84))
61#define DBM_GEN_CFG (DBM_BASE + (0x88))
Ido Shayevitz9fb83452012-04-01 17:45:58 +030062
63/**
64 * USB DBM Hardware registers bitmask.
65 *
66 */
67/* DBM_EP_CFG */
Shimrit Malichia00d7322012-08-05 13:56:28 +030068#define DBM_EN_EP 0x00000001
69#define USB3_EPNUM 0x0000003E
Ido Shayevitz9fb83452012-04-01 17:45:58 +030070#define DBM_BAM_PIPE_NUM 0x000000C0
71#define DBM_PRODUCER 0x00000100
72#define DBM_DISABLE_WB 0x00000200
73#define DBM_INT_RAM_ACC 0x00000400
74
75/* DBM_DATA_FIFO_SIZE */
76#define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff
77
78/* DBM_GEVNTSIZ */
79#define DBM_GEVNTSIZ_MASK 0x0000ffff
80
81/* DBM_DBG_CNFG */
82#define DBM_ENABLE_IOC_MASK 0x0000000f
83
84/* DBM_SOFT_RESET */
85#define DBM_SFT_RST_EP0 0x00000001
86#define DBM_SFT_RST_EP1 0x00000002
87#define DBM_SFT_RST_EP2 0x00000004
88#define DBM_SFT_RST_EP3 0x00000008
Shimrit Malichia00d7322012-08-05 13:56:28 +030089#define DBM_SFT_RST_EPS_MASK 0x0000000F
90#define DBM_SFT_RST_MASK 0x80000000
91#define DBM_EN_MASK 0x00000002
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020092
93#define DBM_MAX_EPS 4
94
Ido Shayevitzfa65a582012-06-06 14:39:54 +030095/* DBM TRB configurations */
96#define DBM_TRB_BIT 0x80000000
97#define DBM_TRB_DATA_SRC 0x40000000
98#define DBM_TRB_DMA 0x20000000
99#define DBM_TRB_EP_NUM(ep) (ep<<24)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300100
Manu Gautam8c642812012-06-07 10:35:10 +0530101/**
102 * USB QSCRATCH Hardware registers
103 *
104 */
105#define QSCRATCH_REG_OFFSET (0x000F8800)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300106#define QSCRATCH_GENERAL_CFG (QSCRATCH_REG_OFFSET + 0x08)
Manu Gautambd0e5782012-08-30 10:39:01 -0700107#define HS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x10)
Manu Gautam8c642812012-06-07 10:35:10 +0530108#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
109#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
110#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
111#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
Manu Gautambd0e5782012-08-30 10:39:01 -0700112#define SS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x30)
Manu Gautam8c642812012-06-07 10:35:10 +0530113
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300114struct dwc3_msm_req_complete {
115 struct list_head list_item;
116 struct usb_request *req;
117 void (*orig_complete)(struct usb_ep *ep,
118 struct usb_request *req);
119};
120
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200121struct dwc3_msm {
122 struct platform_device *dwc3;
123 struct device *dev;
124 void __iomem *base;
125 u32 resource_size;
126 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300127 u8 ep_num_mapping[DBM_MAX_EPS];
128 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
129 struct list_head req_complete_list;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700130 struct clk *ref_clk;
Manu Gautam1742db22012-06-19 13:33:24 +0530131 struct clk *core_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700132 struct clk *iface_clk;
133 struct clk *sleep_clk;
134 struct clk *hsphy_sleep_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530135 struct regulator *hsusb_3p3;
136 struct regulator *hsusb_1p8;
137 struct regulator *hsusb_vddcx;
138 struct regulator *ssusb_1p8;
139 struct regulator *ssusb_vddcx;
140 enum usb_vdd_type ss_vdd_type;
141 enum usb_vdd_type hs_vdd_type;
Manu Gautamb5067272012-07-02 09:53:41 +0530142 struct dwc3_ext_xceiv ext_xceiv;
143 bool resume_pending;
144 atomic_t pm_suspended;
145 atomic_t in_lpm;
146 struct delayed_work resume_work;
147 struct wake_lock wlock;
Manu Gautam8c642812012-06-07 10:35:10 +0530148 struct dwc3_charger charger;
149 struct usb_phy *otg_xceiv;
150 struct delayed_work chg_work;
151 enum usb_chg_state chg_state;
152 u8 dcd_retries;
Manu Gautam2617deb2012-08-31 17:50:06 -0700153 u32 bus_perf_client;
154 struct msm_bus_scale_pdata *bus_scale_table;
Manu Gautam60e01352012-05-29 09:00:34 +0530155};
156
157#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
158#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
159#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
160
161#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
162#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
163#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
164
165#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
166#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
167#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
168
169#define USB_PHY_VDD_DIG_VOL_NONE 0 /* uV */
170#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
171#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
172
Manu Gautam60e01352012-05-29 09:00:34 +0530173static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
174 { /* VDD_CX CORNER Voting */
175 [VDD_NONE] = RPM_VREG_CORNER_NONE,
176 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
177 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
178 },
179 { /* VDD_CX Voltage Voting */
180 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
181 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
182 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
183 },
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200184};
185
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300186static struct dwc3_msm *context;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +0300187static u64 dwc3_msm_dma_mask = DMA_BIT_MASK(64);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300188
189/**
190 *
191 * Read register with debug info.
192 *
193 * @base - DWC3 base virtual address.
194 * @offset - register offset.
195 *
196 * @return u32
197 */
198static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
199{
200 u32 val = ioread32(base + offset);
201 return val;
202}
203
204/**
205 * Read register masked field with debug info.
206 *
207 * @base - DWC3 base virtual address.
208 * @offset - register offset.
209 * @mask - register bitmask.
210 *
211 * @return u32
212 */
213static inline u32 dwc3_msm_read_reg_field(void *base,
214 u32 offset,
215 const u32 mask)
216{
217 u32 shift = find_first_bit((void *)&mask, 32);
218 u32 val = ioread32(base + offset);
219 val &= mask; /* clear other bits */
220 val >>= shift;
221 return val;
222}
223
224/**
225 *
226 * Write register with debug info.
227 *
228 * @base - DWC3 base virtual address.
229 * @offset - register offset.
230 * @val - value to write.
231 *
232 */
233static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
234{
235 iowrite32(val, base + offset);
236}
237
238/**
239 * Write register masked field with debug info.
240 *
241 * @base - DWC3 base virtual address.
242 * @offset - register offset.
243 * @mask - register bitmask.
244 * @val - value to write.
245 *
246 */
247static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
248 const u32 mask, u32 val)
249{
250 u32 shift = find_first_bit((void *)&mask, 32);
251 u32 tmp = ioread32(base + offset);
252
253 tmp &= ~mask; /* clear written bits */
254 val = tmp | (val << shift);
255 iowrite32(val, base + offset);
256}
257
258/**
Manu Gautam8c642812012-06-07 10:35:10 +0530259 * Write register and read back masked value to confirm it is written
260 *
261 * @base - DWC3 base virtual address.
262 * @offset - register offset.
263 * @mask - register bitmask specifying what should be updated
264 * @val - value to write.
265 *
266 */
267static inline void dwc3_msm_write_readback(void *base, u32 offset,
268 const u32 mask, u32 val)
269{
270 u32 write_val, tmp = ioread32(base + offset);
271
272 tmp &= ~mask; /* retain other bits */
273 write_val = tmp | val;
274
275 iowrite32(write_val, base + offset);
276
277 /* Read back to see if val was written */
278 tmp = ioread32(base + offset);
279 tmp &= mask; /* clear other bits */
280
281 if (tmp != val)
282 dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n",
283 __func__, val, offset);
284}
285
286/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300287 * Return DBM EP number according to usb endpoint number.
288 *
289 */
290static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep)
291{
292 int i;
293
294 for (i = 0; i < context->dbm_num_eps; i++)
295 if (context->ep_num_mapping[i] == usb_ep)
296 return i;
297
298 return -ENODEV; /* Not found */
299}
300
301/**
302 * Return number of configured DBM endpoints.
303 *
304 */
305static int dwc3_msm_configured_dbm_ep_num(void)
306{
307 int i;
308 int count = 0;
309
310 for (i = 0; i < context->dbm_num_eps; i++)
311 if (context->ep_num_mapping[i])
312 count++;
313
314 return count;
315}
316
317/**
318 * Configure the DBM with the USB3 core event buffer.
319 * This function is called by the SNPS UDC upon initialization.
320 *
321 * @addr - address of the event buffer.
322 * @size - size of the event buffer.
323 *
324 */
325static int dwc3_msm_event_buffer_config(u32 addr, u16 size)
326{
327 dev_dbg(context->dev, "%s\n", __func__);
328
329 dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr);
330 dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ,
331 DBM_GEVNTSIZ_MASK, size);
332
333 return 0;
334}
335
336/**
337 * Reset the DBM registers upon initialization.
338 *
339 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300340static int dwc3_msm_dbm_soft_reset(int enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300341{
342 dev_dbg(context->dev, "%s\n", __func__);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300343 if (enter_reset) {
344 dev_dbg(context->dev, "enter DBM reset\n");
345 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
346 DBM_SFT_RST_MASK, 1);
347 } else {
348 dev_dbg(context->dev, "exit DBM reset\n");
349 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
350 DBM_SFT_RST_MASK, 0);
351 /*enable DBM*/
352 dwc3_msm_write_reg_field(context->base, QSCRATCH_GENERAL_CFG,
353 DBM_EN_MASK, 0x1);
354 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300355
356 return 0;
357}
358
359/**
360 * Soft reset specific DBM ep.
361 * This function is called by the function driver upon events
362 * such as transfer aborting, USB re-enumeration and USB
363 * disconnection.
364 *
365 * @dbm_ep - DBM ep number.
366 * @enter_reset - should we enter a reset state or get out of it.
367 *
368 */
369static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset)
370{
371 dev_dbg(context->dev, "%s\n", __func__);
372
373 if (dbm_ep >= context->dbm_num_eps) {
374 dev_err(context->dev,
375 "%s: Invalid DBM ep index\n", __func__);
376 return -ENODEV;
377 }
378
379 if (enter_reset) {
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, 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300382 } else {
383 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300384 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300385 }
386
387 return 0;
388}
389
390/**
391 * Configure a USB DBM ep to work in BAM mode.
392 *
393 *
394 * @usb_ep - USB physical EP number.
395 * @producer - producer/consumer.
396 * @disable_wb - disable write back to system memory.
397 * @internal_mem - use internal USB memory for data fifo.
398 * @ioc - enable interrupt on completion.
399 *
400 * @return int - DBM ep number.
401 */
402static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe,
403 bool producer, bool disable_wb,
404 bool internal_mem, bool ioc)
405{
406 u8 dbm_ep;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300407 u32 ep_cfg;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300408
409 dev_dbg(context->dev, "%s\n", __func__);
410
Shimrit Malichia00d7322012-08-05 13:56:28 +0300411 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
412
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300413 if (dbm_ep < 0) {
Shimrit Malichia00d7322012-08-05 13:56:28 +0300414 dev_err(context->dev,
415 "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300416 return -ENODEV;
417 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300418 /* First, reset the dbm endpoint */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300419 dwc3_msm_dbm_ep_soft_reset(dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300420
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300421 /* Set ioc bit for dbm_ep if needed */
422 dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300423 DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300424
Shimrit Malichia00d7322012-08-05 13:56:28 +0300425 ep_cfg = (producer ? DBM_PRODUCER : 0) |
426 (disable_wb ? DBM_DISABLE_WB : 0) |
427 (internal_mem ? DBM_INT_RAM_ACC : 0);
428
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300429 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
Shimrit Malichia00d7322012-08-05 13:56:28 +0300430 DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8);
431
432 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM,
433 usb_ep);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300434 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
435 DBM_BAM_PIPE_NUM, bam_pipe);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300436 dwc3_msm_write_reg_field(context->base, DBM_PIPE_CFG, 0x000000ff,
437 0xe4);
438 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP,
439 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300440
441 return dbm_ep;
442}
443
444/**
445 * Configure a USB DBM ep to work in normal mode.
446 *
447 * @usb_ep - USB ep number.
448 *
449 */
450static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep)
451{
452 u8 dbm_ep;
453
454 dev_dbg(context->dev, "%s\n", __func__);
455
456 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
457
458 if (dbm_ep < 0) {
459 dev_err(context->dev,
460 "%s: Invalid usb ep index\n", __func__);
461 return -ENODEV;
462 }
463
464 context->ep_num_mapping[dbm_ep] = 0;
465
466 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), 0);
467
468 /* Reset the dbm endpoint */
469 dwc3_msm_dbm_ep_soft_reset(dbm_ep, true);
470
471 return 0;
472}
473
474/**
475 * Configure the DBM with the BAM's data fifo.
476 * This function is called by the USB BAM Driver
477 * upon initialization.
478 *
479 * @ep - pointer to usb endpoint.
480 * @addr - address of data fifo.
481 * @size - size of data fifo.
482 *
483 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300484int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300485{
486 u8 dbm_ep;
487 struct dwc3_ep *dep = to_dwc3_ep(ep);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300488 u8 bam_pipe = dst_pipe_idx;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300489
490 dev_dbg(context->dev, "%s\n", __func__);
491
Shimrit Malichia00d7322012-08-05 13:56:28 +0300492 dbm_ep = bam_pipe;
493 context->ep_num_mapping[dbm_ep] = dep->number;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300494
495 dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr);
496 dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep),
497 DBM_DATA_FIFO_SIZE_MASK, size);
498
499 return 0;
500}
501
502/**
503* Cleanups for msm endpoint on request complete.
504*
505* Also call original request complete.
506*
507* @usb_ep - pointer to usb_ep instance.
508* @request - pointer to usb_request instance.
509*
510* @return int - 0 on success, negetive on error.
511*/
512static void dwc3_msm_req_complete_func(struct usb_ep *ep,
513 struct usb_request *request)
514{
515 struct dwc3_request *req = to_dwc3_request(request);
516 struct dwc3_ep *dep = to_dwc3_ep(ep);
517 struct dwc3_msm_req_complete *req_complete = NULL;
518
519 /* Find original request complete function and remove it from list */
520 list_for_each_entry(req_complete,
521 &context->req_complete_list,
522 list_item) {
523 if (req_complete->req == request)
524 break;
525 }
526 if (!req_complete || req_complete->req != request) {
527 dev_err(dep->dwc->dev, "%s: could not find the request\n",
528 __func__);
529 return;
530 }
531 list_del(&req_complete->list_item);
532
533 /*
534 * Release another one TRB to the pool since DBM queue took 2 TRBs
535 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
536 * released only one.
537 */
538 if (req->queued)
539 dep->busy_slot++;
540
541 /* Unconfigure dbm ep */
542 dwc3_msm_dbm_ep_unconfig(dep->number);
543
544 /*
545 * If this is the last endpoint we unconfigured, than reset also
546 * the event buffers.
547 */
548 if (0 == dwc3_msm_configured_dbm_ep_num())
549 dwc3_msm_event_buffer_config(0, 0);
550
551 /*
552 * Call original complete function, notice that dwc->lock is already
553 * taken by the caller of this function (dwc3_gadget_giveback()).
554 */
555 request->complete = req_complete->orig_complete;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300556 if (request->complete)
557 request->complete(ep, request);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300558
559 kfree(req_complete);
560}
561
562/**
563* Helper function.
564* See the header of the dwc3_msm_ep_queue function.
565*
566* @dwc3_ep - pointer to dwc3_ep instance.
567* @req - pointer to dwc3_request instance.
568*
569* @return int - 0 on success, negetive on error.
570*/
571static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
572{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300573 struct dwc3_trb *trb;
574 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300575 struct dwc3_gadget_ep_cmd_params params;
576 u32 cmd;
577 int ret = 0;
578
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300579 /* We push the request to the dep->req_queued list to indicate that
580 * this request is issued with start transfer. The request will be out
581 * from this list in 2 cases. The first is that the transfer will be
582 * completed (not if the transfer is endless using a circular TRBs with
583 * with link TRB). The second case is an option to do stop stransfer,
584 * this can be initiated by the function driver when calling dequeue.
585 */
586 req->queued = true;
587 list_add_tail(&req->list, &dep->req_queued);
588
589 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300590 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300591 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300592 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300593
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300594 req->trb = trb;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300595 trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300596 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
597 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300598 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300599
600 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300601 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300602 dep->free_slot++;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300603 memset(trb_link, 0, sizeof *trb_link);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300604
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300605 trb_link->bpl = lower_32_bits(req->trb_dma);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300606 trb_link->bph = DBM_TRB_BIT |
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300607 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
608 trb_link->size = 0;
609 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300610
611 /*
612 * Now start the transfer
613 */
614 memset(&params, 0, sizeof(params));
Shimrit Malichia00d7322012-08-05 13:56:28 +0300615 params.param0 = 0; /* TDAddr High */
616 params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */
617
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300618 cmd = DWC3_DEPCMD_STARTTRANSFER;
619 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
620 if (ret < 0) {
621 dev_dbg(dep->dwc->dev,
622 "%s: failed to send STARTTRANSFER command\n",
623 __func__);
624
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300625 list_del(&req->list);
626 return ret;
627 }
628
629 return ret;
630}
631
632/**
633* Queue a usb request to the DBM endpoint.
634* This function should be called after the endpoint
635* was enabled by the ep_enable.
636*
637* This function prepares special structure of TRBs which
638* is familier with the DBM HW, so it will possible to use
639* this endpoint in DBM mode.
640*
641* The TRBs prepared by this function, is one normal TRB
642* which point to a fake buffer, followed by a link TRB
643* that points to the first TRB.
644*
645* The API of this function follow the regular API of
646* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
647*
648* @usb_ep - pointer to usb_ep instance.
649* @request - pointer to usb_request instance.
650* @gfp_flags - possible flags.
651*
652* @return int - 0 on success, negetive on error.
653*/
654static int dwc3_msm_ep_queue(struct usb_ep *ep,
655 struct usb_request *request, gfp_t gfp_flags)
656{
657 struct dwc3_request *req = to_dwc3_request(request);
658 struct dwc3_ep *dep = to_dwc3_ep(ep);
659 struct dwc3 *dwc = dep->dwc;
660 struct dwc3_msm_req_complete *req_complete;
661 unsigned long flags;
662 int ret = 0;
663 u8 bam_pipe;
664 bool producer;
665 bool disable_wb;
666 bool internal_mem;
667 bool ioc;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300668 u8 speed;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300669
670 if (!(request->udc_priv & MSM_SPS_MODE)) {
671 /* Not SPS mode, call original queue */
672 dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n",
673 __func__);
674
675 return (context->original_ep_ops[dep->number])->queue(ep,
676 request,
677 gfp_flags);
678 }
679
680 if (!dep->endpoint.desc) {
681 dev_err(dwc->dev,
682 "%s: trying to queue request %p to disabled ep %s\n",
683 __func__, request, ep->name);
684 return -EPERM;
685 }
686
687 if (dep->number == 0 || dep->number == 1) {
688 dev_err(dwc->dev,
689 "%s: trying to queue dbm request %p to control ep %s\n",
690 __func__, request, ep->name);
691 return -EPERM;
692 }
693
694 if (dep->free_slot > 0 || dep->busy_slot > 0 ||
695 !list_empty(&dep->request_list) ||
696 !list_empty(&dep->req_queued)) {
697
698 dev_err(dwc->dev,
699 "%s: trying to queue dbm request %p tp ep %s\n",
700 __func__, request, ep->name);
701 return -EPERM;
702 }
703
704 /*
705 * Override req->complete function, but before doing that,
706 * store it's original pointer in the req_complete_list.
707 */
708 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
709 if (!req_complete) {
710 dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__);
711 return -ENOMEM;
712 }
713 req_complete->req = request;
714 req_complete->orig_complete = request->complete;
715 list_add_tail(&req_complete->list_item, &context->req_complete_list);
716 request->complete = dwc3_msm_req_complete_func;
717
718 /*
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300719 * Configure the DBM endpoint
720 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300721 bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300722 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
723 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
724 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
725 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
726
727 ret = dwc3_msm_dbm_ep_config(dep->number,
728 bam_pipe, producer,
729 disable_wb, internal_mem, ioc);
730 if (ret < 0) {
731 dev_err(context->dev,
732 "error %d after calling dwc3_msm_dbm_ep_config\n",
733 ret);
734 return ret;
735 }
736
737 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
738 __func__, request, ep->name, request->length);
739
740 /*
741 * We must obtain the lock of the dwc3 core driver,
742 * including disabling interrupts, so we will be sure
743 * that we are the only ones that configure the HW device
744 * core and ensure that we queuing the request will finish
745 * as soon as possible so we will release back the lock.
746 */
747 spin_lock_irqsave(&dwc->lock, flags);
748 ret = __dwc3_msm_ep_queue(dep, req);
749 spin_unlock_irqrestore(&dwc->lock, flags);
750 if (ret < 0) {
751 dev_err(context->dev,
752 "error %d after calling __dwc3_msm_ep_queue\n", ret);
753 return ret;
754 }
755
Shimrit Malichia00d7322012-08-05 13:56:28 +0300756 speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD;
757 dwc3_msm_write_reg(context->base, DBM_GEN_CFG, speed >> 2);
758
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300759 return 0;
760}
761
762/**
763 * Configure MSM endpoint.
764 * This function do specific configurations
765 * to an endpoint which need specific implementaion
766 * in the MSM architecture.
767 *
768 * This function should be called by usb function/class
769 * layer which need a support from the specific MSM HW
770 * which wrap the USB3 core. (like DBM specific endpoints)
771 *
772 * @ep - a pointer to some usb_ep instance
773 *
774 * @return int - 0 on success, negetive on error.
775 */
776int msm_ep_config(struct usb_ep *ep)
777{
778 struct dwc3_ep *dep = to_dwc3_ep(ep);
779 struct usb_ep_ops *new_ep_ops;
780
781 /* Save original ep ops for future restore*/
782 if (context->original_ep_ops[dep->number]) {
783 dev_err(context->dev,
784 "ep [%s,%d] already configured as msm endpoint\n",
785 ep->name, dep->number);
786 return -EPERM;
787 }
788 context->original_ep_ops[dep->number] = ep->ops;
789
790 /* Set new usb ops as we like */
791 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
792 if (!new_ep_ops) {
793 dev_err(context->dev,
794 "%s: unable to allocate mem for new usb ep ops\n",
795 __func__);
796 return -ENOMEM;
797 }
798 (*new_ep_ops) = (*ep->ops);
799 new_ep_ops->queue = dwc3_msm_ep_queue;
800 ep->ops = new_ep_ops;
801
802 /*
803 * Do HERE more usb endpoint configurations
804 * which are specific to MSM.
805 */
806
807 return 0;
808}
809EXPORT_SYMBOL(msm_ep_config);
810
811/**
812 * Un-configure MSM endpoint.
813 * Tear down configurations done in the
814 * dwc3_msm_ep_config function.
815 *
816 * @ep - a pointer to some usb_ep instance
817 *
818 * @return int - 0 on success, negetive on error.
819 */
820int msm_ep_unconfig(struct usb_ep *ep)
821{
822 struct dwc3_ep *dep = to_dwc3_ep(ep);
823 struct usb_ep_ops *old_ep_ops;
824
825 /* Restore original ep ops */
826 if (!context->original_ep_ops[dep->number]) {
827 dev_err(context->dev,
828 "ep [%s,%d] was not configured as msm endpoint\n",
829 ep->name, dep->number);
830 return -EINVAL;
831 }
832 old_ep_ops = (struct usb_ep_ops *)ep->ops;
833 ep->ops = context->original_ep_ops[dep->number];
834 context->original_ep_ops[dep->number] = NULL;
835 kfree(old_ep_ops);
836
837 /*
838 * Do HERE more usb endpoint un-configurations
839 * which are specific to MSM.
840 */
841
842 return 0;
843}
844EXPORT_SYMBOL(msm_ep_unconfig);
845
Manu Gautam60e01352012-05-29 09:00:34 +0530846/* HSPHY */
847static int dwc3_hsusb_config_vddcx(int high)
848{
849 int min_vol, ret;
850 struct dwc3_msm *dwc = context;
851 enum usb_vdd_type vdd_type = context->hs_vdd_type;
852 int max_vol = vdd_val[vdd_type][VDD_MAX];
853
854 min_vol = vdd_val[vdd_type][high ? VDD_MIN : VDD_NONE];
855 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
856 if (ret) {
857 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
858 return ret;
859 }
860
861 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
862 min_vol, max_vol);
863
864 return ret;
865}
866
867static int dwc3_hsusb_ldo_init(int init)
868{
869 int rc = 0;
870 struct dwc3_msm *dwc = context;
871
872 if (!init) {
873 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
874 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
875 return 0;
876 }
877
878 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
879 if (IS_ERR(dwc->hsusb_3p3)) {
880 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
881 return PTR_ERR(dwc->hsusb_3p3);
882 }
883
884 rc = regulator_set_voltage(dwc->hsusb_3p3,
885 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
886 if (rc) {
887 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
888 return rc;
889 }
890 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
891 if (IS_ERR(dwc->hsusb_1p8)) {
892 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
893 rc = PTR_ERR(dwc->hsusb_1p8);
894 goto devote_3p3;
895 }
896 rc = regulator_set_voltage(dwc->hsusb_1p8,
897 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
898 if (rc) {
899 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
900 goto devote_3p3;
901 }
902
903 return 0;
904
905devote_3p3:
906 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
907
908 return rc;
909}
910
911static int dwc3_hsusb_ldo_enable(int on)
912{
913 int rc = 0;
914 struct dwc3_msm *dwc = context;
915
916 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
917
918 if (!on)
919 goto disable_regulators;
920
921
922 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
923 if (rc < 0) {
924 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
925 return rc;
926 }
927
928 rc = regulator_enable(dwc->hsusb_1p8);
929 if (rc) {
930 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
931 goto put_1p8_lpm;
932 }
933
934 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
935 if (rc < 0) {
936 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
937 goto disable_1p8;
938 }
939
940 rc = regulator_enable(dwc->hsusb_3p3);
941 if (rc) {
942 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
943 goto put_3p3_lpm;
944 }
945
946 return 0;
947
948disable_regulators:
949 rc = regulator_disable(dwc->hsusb_3p3);
950 if (rc)
951 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
952
953put_3p3_lpm:
954 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
955 if (rc < 0)
956 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
957
958disable_1p8:
959 rc = regulator_disable(dwc->hsusb_1p8);
960 if (rc)
961 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
962
963put_1p8_lpm:
964 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
965 if (rc < 0)
966 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
967
968 return rc < 0 ? rc : 0;
969}
970
971/* SSPHY */
972static int dwc3_ssusb_config_vddcx(int high)
973{
974 int min_vol, ret;
975 struct dwc3_msm *dwc = context;
976 enum usb_vdd_type vdd_type = context->ss_vdd_type;
977 int max_vol = vdd_val[vdd_type][VDD_MAX];
978
979 min_vol = vdd_val[vdd_type][high ? VDD_MIN : VDD_NONE];
980 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
981 if (ret) {
982 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
983 return ret;
984 }
985
986 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
987 min_vol, max_vol);
988 return ret;
989}
990
991/* 3.3v supply not needed for SS PHY */
992static int dwc3_ssusb_ldo_init(int init)
993{
994 int rc = 0;
995 struct dwc3_msm *dwc = context;
996
997 if (!init) {
998 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
999 return 0;
1000 }
1001
1002 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
1003 if (IS_ERR(dwc->ssusb_1p8)) {
1004 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
1005 return PTR_ERR(dwc->ssusb_1p8);
1006 }
1007 rc = regulator_set_voltage(dwc->ssusb_1p8,
1008 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
1009 if (rc)
1010 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
1011
1012 return rc;
1013}
1014
1015static int dwc3_ssusb_ldo_enable(int on)
1016{
1017 int rc = 0;
1018 struct dwc3_msm *dwc = context;
1019
1020 dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1021
1022 if (!on)
1023 goto disable_regulators;
1024
1025
1026 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1027 if (rc < 0) {
1028 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1029 return rc;
1030 }
1031
1032 rc = regulator_enable(dwc->ssusb_1p8);
1033 if (rc) {
1034 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1035 goto put_1p8_lpm;
1036 }
1037
1038 return 0;
1039
1040disable_regulators:
1041 rc = regulator_disable(dwc->ssusb_1p8);
1042 if (rc)
1043 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1044
1045put_1p8_lpm:
1046 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1047 if (rc < 0)
1048 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1049
1050 return rc < 0 ? rc : 0;
1051}
1052
Manu Gautam8c642812012-06-07 10:35:10 +05301053static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1054{
1055 u32 chg_ctrl;
1056
1057 /* Turn off VDP_SRC */
1058 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1059 msleep(20);
1060
1061 /* Before proceeding make sure VDP_SRC is OFF */
1062 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1063 if (chg_ctrl & 0x3F)
1064 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1065 __func__, chg_ctrl);
1066 /*
1067 * Configure DM as current source, DP as current sink
1068 * and enable battery charging comparators.
1069 */
1070 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1071}
1072
1073static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1074{
1075 u32 chg_det;
1076 bool ret = false;
1077
1078 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1079 ret = chg_det & 1;
1080
1081 return ret;
1082}
1083
1084static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1085{
1086 /*
1087 * Configure DP as current source, DM as current sink
1088 * and enable battery charging comparators.
1089 */
1090 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1091}
1092
1093static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1094{
1095 u32 chg_state;
1096 bool ret = false;
1097
1098 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1099 ret = chg_state & 2;
1100
1101 return ret;
1102}
1103
1104static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1105{
1106 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1107}
1108
1109static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1110{
1111 /* Data contact detection enable, DCDENB */
1112 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1113}
1114
1115static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1116{
1117 u32 chg_ctrl;
1118
1119 /* Clear charger detecting control bits */
1120 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1121
1122 /* Clear alt interrupt latch and enable bits */
1123 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1124 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1125
1126 udelay(100);
1127
1128 /* Before proceeding make sure charger block is RESET */
1129 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1130 if (chg_ctrl & 0x3F)
1131 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1132 __func__, chg_ctrl);
1133}
1134
1135static const char *chg_to_string(enum dwc3_chg_type chg_type)
1136{
1137 switch (chg_type) {
1138 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1139 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1140 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1141 default: return "INVALID_CHARGER";
1142 }
1143}
1144
1145#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1146#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1147#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1148#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1149
1150static void dwc3_chg_detect_work(struct work_struct *w)
1151{
1152 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1153 bool is_dcd = false, tmout, vout;
1154 unsigned long delay;
1155
1156 dev_dbg(mdwc->dev, "chg detection work\n");
1157 switch (mdwc->chg_state) {
1158 case USB_CHG_STATE_UNDEFINED:
1159 dwc3_chg_block_reset(mdwc);
1160 dwc3_chg_enable_dcd(mdwc);
1161 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1162 mdwc->dcd_retries = 0;
1163 delay = DWC3_CHG_DCD_POLL_TIME;
1164 break;
1165 case USB_CHG_STATE_WAIT_FOR_DCD:
1166 is_dcd = dwc3_chg_check_dcd(mdwc);
1167 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1168 if (is_dcd || tmout) {
1169 dwc3_chg_disable_dcd(mdwc);
1170 dwc3_chg_enable_primary_det(mdwc);
1171 delay = DWC3_CHG_PRIMARY_DET_TIME;
1172 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1173 } else {
1174 delay = DWC3_CHG_DCD_POLL_TIME;
1175 }
1176 break;
1177 case USB_CHG_STATE_DCD_DONE:
1178 vout = dwc3_chg_det_check_output(mdwc);
1179 if (vout) {
1180 dwc3_chg_enable_secondary_det(mdwc);
1181 delay = DWC3_CHG_SECONDARY_DET_TIME;
1182 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1183 } else {
1184 mdwc->charger.chg_type = USB_SDP_CHARGER;
1185 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1186 delay = 0;
1187 }
1188 break;
1189 case USB_CHG_STATE_PRIMARY_DONE:
1190 vout = dwc3_chg_det_check_output(mdwc);
1191 if (vout)
1192 mdwc->charger.chg_type = USB_DCP_CHARGER;
1193 else
1194 mdwc->charger.chg_type = USB_CDP_CHARGER;
1195 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1196 /* fall through */
1197 case USB_CHG_STATE_SECONDARY_DONE:
1198 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1199 /* fall through */
1200 case USB_CHG_STATE_DETECTED:
1201 dwc3_chg_block_reset(mdwc);
1202 dev_dbg(mdwc->dev, "chg_type = %s\n",
1203 chg_to_string(mdwc->charger.chg_type));
1204 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1205 &mdwc->charger);
1206 return;
1207 default:
1208 return;
1209 }
1210
1211 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1212}
1213
1214static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1215{
1216 struct dwc3_msm *mdwc = context;
1217
1218 if (start == false) {
1219 cancel_delayed_work_sync(&mdwc->chg_work);
1220 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1221 charger->chg_type = DWC3_INVALID_CHARGER;
1222 return;
1223 }
1224
1225 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1226 charger->chg_type = DWC3_INVALID_CHARGER;
1227 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1228}
1229
Manu Gautamb5067272012-07-02 09:53:41 +05301230static int dwc3_msm_suspend(struct dwc3_msm *mdwc)
1231{
Manu Gautam2617deb2012-08-31 17:50:06 -07001232 int ret;
1233
Manu Gautamb5067272012-07-02 09:53:41 +05301234 dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__);
1235
1236 if (atomic_read(&mdwc->in_lpm)) {
1237 dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__);
1238 return 0;
1239 }
1240
Manu Gautam3e9ad352012-08-16 14:44:47 -07001241 clk_disable_unprepare(mdwc->iface_clk);
Manu Gautamb5067272012-07-02 09:53:41 +05301242 clk_disable_unprepare(mdwc->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001243 clk_disable_unprepare(mdwc->ref_clk);
Manu Gautamb5067272012-07-02 09:53:41 +05301244 dwc3_hsusb_ldo_enable(0);
1245 dwc3_ssusb_ldo_enable(0);
1246 wake_unlock(&mdwc->wlock);
1247
Manu Gautam2617deb2012-08-31 17:50:06 -07001248 if (mdwc->bus_perf_client) {
1249 ret = msm_bus_scale_client_update_request(
1250 mdwc->bus_perf_client, 0);
1251 if (ret)
1252 dev_err(mdwc->dev, "Failed to reset bus bw vote\n");
1253 }
1254
Manu Gautamb5067272012-07-02 09:53:41 +05301255 atomic_set(&mdwc->in_lpm, 1);
1256 dev_info(mdwc->dev, "DWC3 in low power mode\n");
1257
1258 return 0;
1259}
1260
1261static int dwc3_msm_resume(struct dwc3_msm *mdwc)
1262{
Manu Gautam2617deb2012-08-31 17:50:06 -07001263 int ret;
1264
Manu Gautamb5067272012-07-02 09:53:41 +05301265 dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__);
1266
1267 if (!atomic_read(&mdwc->in_lpm)) {
1268 dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__);
1269 return 0;
1270 }
1271
Manu Gautam2617deb2012-08-31 17:50:06 -07001272 if (mdwc->bus_perf_client) {
1273 ret = msm_bus_scale_client_update_request(
1274 mdwc->bus_perf_client, 1);
1275 if (ret)
1276 dev_err(mdwc->dev, "Failed to vote for bus scaling\n");
1277 }
1278
Manu Gautamb5067272012-07-02 09:53:41 +05301279 wake_lock(&mdwc->wlock);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001280 clk_prepare_enable(mdwc->ref_clk);
Manu Gautamb5067272012-07-02 09:53:41 +05301281 clk_prepare_enable(mdwc->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001282 clk_prepare_enable(mdwc->iface_clk);
Manu Gautamb5067272012-07-02 09:53:41 +05301283 dwc3_hsusb_ldo_enable(1);
1284 dwc3_ssusb_ldo_enable(1);
1285
1286 atomic_set(&mdwc->in_lpm, 0);
1287 dev_info(mdwc->dev, "DWC3 exited from low power mode\n");
1288
1289 return 0;
1290}
1291
1292static void dwc3_resume_work(struct work_struct *w)
1293{
1294 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1295 resume_work.work);
1296
1297 dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__);
1298 /* handle any event that was queued while work was already running */
1299 if (!atomic_read(&mdwc->in_lpm)) {
1300 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1301 if (mdwc->otg_xceiv)
1302 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1303 DWC3_EVENT_XCEIV_STATE);
1304 return;
1305 }
1306
1307 /* bail out if system resume in process, else initiate RESUME */
1308 if (atomic_read(&mdwc->pm_suspended)) {
1309 mdwc->resume_pending = true;
1310 } else {
1311 pm_runtime_get_sync(mdwc->dev);
1312 if (mdwc->otg_xceiv)
1313 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1314 DWC3_EVENT_PHY_RESUME);
1315 pm_runtime_put_sync(mdwc->dev);
1316 }
1317}
1318
1319static bool debug_id, debug_bsv, debug_connect;
1320
1321static int dwc3_connect_show(struct seq_file *s, void *unused)
1322{
1323 if (debug_connect)
1324 seq_printf(s, "true\n");
1325 else
1326 seq_printf(s, "false\n");
1327
1328 return 0;
1329}
1330
1331static int dwc3_connect_open(struct inode *inode, struct file *file)
1332{
1333 return single_open(file, dwc3_connect_show, inode->i_private);
1334}
1335
1336static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf,
1337 size_t count, loff_t *ppos)
1338{
1339 struct seq_file *s = file->private_data;
1340 struct dwc3_msm *mdwc = s->private;
1341 char buf[8];
1342
1343 memset(buf, 0x00, sizeof(buf));
1344
1345 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1346 return -EFAULT;
1347
1348 if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) {
1349 debug_connect = true;
1350 } else {
1351 debug_connect = debug_bsv = false;
1352 debug_id = true;
1353 }
1354
1355 mdwc->ext_xceiv.bsv = debug_bsv;
1356 mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND;
1357
1358 if (atomic_read(&mdwc->in_lpm)) {
1359 dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__);
1360 dwc3_resume_work(&mdwc->resume_work.work);
1361 } else {
1362 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1363 if (mdwc->otg_xceiv)
1364 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1365 DWC3_EVENT_XCEIV_STATE);
1366 }
1367
1368 return count;
1369}
1370
1371const struct file_operations dwc3_connect_fops = {
1372 .open = dwc3_connect_open,
1373 .read = seq_read,
1374 .write = dwc3_connect_write,
1375 .llseek = seq_lseek,
1376 .release = single_release,
1377};
1378
1379static struct dentry *dwc3_debugfs_root;
1380
1381static void dwc3_debugfs_init(struct dwc3_msm *mdwc)
1382{
1383 dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL);
1384
1385 if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root))
1386 return;
1387
1388 if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
1389 (u32 *)&debug_id))
1390 goto error;
1391
1392 if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
1393 (u32 *)&debug_bsv))
1394 goto error;
1395
1396 if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR,
1397 dwc3_debugfs_root, mdwc, &dwc3_connect_fops))
1398 goto error;
1399
1400 return;
1401
1402error:
1403 debugfs_remove_recursive(dwc3_debugfs_root);
1404}
Manu Gautam8c642812012-06-07 10:35:10 +05301405
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001406static int __devinit dwc3_msm_probe(struct platform_device *pdev)
1407{
1408 struct device_node *node = pdev->dev.of_node;
1409 struct platform_device *dwc3;
1410 struct dwc3_msm *msm;
1411 struct resource *res;
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03001412 void __iomem *tcsr;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001413 int ret = 0;
1414
1415 msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL);
1416 if (!msm) {
1417 dev_err(&pdev->dev, "not enough memory\n");
1418 return -ENOMEM;
1419 }
1420
1421 platform_set_drvdata(pdev, msm);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001422 context = msm;
Manu Gautam60e01352012-05-29 09:00:34 +05301423 msm->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001424
1425 INIT_LIST_HEAD(&msm->req_complete_list);
Manu Gautam8c642812012-06-07 10:35:10 +05301426 INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work);
Manu Gautamb5067272012-07-02 09:53:41 +05301427 INIT_DELAYED_WORK(&msm->resume_work, dwc3_resume_work);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001428
Manu Gautam1742db22012-06-19 13:33:24 +05301429 /*
1430 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
1431 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
1432 */
1433 msm->core_clk = devm_clk_get(&pdev->dev, "core_clk");
1434 if (IS_ERR(msm->core_clk)) {
1435 dev_err(&pdev->dev, "failed to get core_clk\n");
1436 return PTR_ERR(msm->core_clk);
1437 }
1438 clk_set_rate(msm->core_clk, 125000000);
1439 clk_prepare_enable(msm->core_clk);
1440
Manu Gautam3e9ad352012-08-16 14:44:47 -07001441 msm->iface_clk = devm_clk_get(&pdev->dev, "iface_clk");
1442 if (IS_ERR(msm->iface_clk)) {
1443 dev_err(&pdev->dev, "failed to get iface_clk\n");
1444 ret = PTR_ERR(msm->iface_clk);
1445 goto disable_core_clk;
1446 }
1447 clk_prepare_enable(msm->iface_clk);
1448
1449 msm->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
1450 if (IS_ERR(msm->sleep_clk)) {
1451 dev_err(&pdev->dev, "failed to get sleep_clk\n");
1452 ret = PTR_ERR(msm->sleep_clk);
1453 goto disable_iface_clk;
1454 }
1455 clk_prepare_enable(msm->sleep_clk);
1456
1457 msm->hsphy_sleep_clk = devm_clk_get(&pdev->dev, "sleep_a_clk");
1458 if (IS_ERR(msm->hsphy_sleep_clk)) {
1459 dev_err(&pdev->dev, "failed to get sleep_a_clk\n");
1460 ret = PTR_ERR(msm->hsphy_sleep_clk);
1461 goto disable_sleep_clk;
1462 }
1463 clk_prepare_enable(msm->hsphy_sleep_clk);
1464
1465 msm->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
1466 if (IS_ERR(msm->ref_clk)) {
1467 dev_err(&pdev->dev, "failed to get ref_clk\n");
1468 ret = PTR_ERR(msm->ref_clk);
1469 goto disable_sleep_a_clk;
1470 }
1471 clk_prepare_enable(msm->ref_clk);
1472
Manu Gautam60e01352012-05-29 09:00:34 +05301473 /* SS PHY */
1474 msm->ss_vdd_type = VDDCX_CORNER;
1475 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
1476 if (IS_ERR(msm->ssusb_vddcx)) {
1477 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev,
1478 "SSUSB_VDDCX");
1479 if (IS_ERR(msm->ssusb_vddcx)) {
1480 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
Manu Gautam1742db22012-06-19 13:33:24 +05301481 ret = PTR_ERR(msm->ssusb_vddcx);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001482 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05301483 }
1484 msm->ss_vdd_type = VDDCX;
1485 dev_dbg(&pdev->dev, "ss_vdd_type: VDDCX\n");
1486 }
1487
1488 ret = dwc3_ssusb_config_vddcx(1);
1489 if (ret) {
1490 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam3e9ad352012-08-16 14:44:47 -07001491 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05301492 }
1493
1494 ret = regulator_enable(context->ssusb_vddcx);
1495 if (ret) {
1496 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
1497 goto unconfig_ss_vddcx;
1498 }
1499
1500 ret = dwc3_ssusb_ldo_init(1);
1501 if (ret) {
1502 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
1503 goto disable_ss_vddcx;
1504 }
1505
1506 ret = dwc3_ssusb_ldo_enable(1);
1507 if (ret) {
1508 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
1509 goto free_ss_ldo_init;
1510 }
1511
1512 /* HS PHY */
1513 msm->hs_vdd_type = VDDCX_CORNER;
1514 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
1515 if (IS_ERR(msm->hsusb_vddcx)) {
1516 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev,
1517 "HSUSB_VDDCX");
1518 if (IS_ERR(msm->hsusb_vddcx)) {
1519 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
1520 ret = PTR_ERR(msm->ssusb_vddcx);
1521 goto disable_ss_ldo;
1522 }
1523 msm->hs_vdd_type = VDDCX;
1524 dev_dbg(&pdev->dev, "hs_vdd_type: VDDCX\n");
1525 }
1526
1527 ret = dwc3_hsusb_config_vddcx(1);
1528 if (ret) {
1529 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1530 goto disable_ss_ldo;
1531 }
1532
1533 ret = regulator_enable(context->hsusb_vddcx);
1534 if (ret) {
1535 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
1536 goto unconfig_hs_vddcx;
1537 }
1538
1539 ret = dwc3_hsusb_ldo_init(1);
1540 if (ret) {
1541 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1542 goto disable_hs_vddcx;
1543 }
1544
1545 ret = dwc3_hsusb_ldo_enable(1);
1546 if (ret) {
1547 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1548 goto free_hs_ldo_init;
1549 }
1550
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03001551 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1552 if (!res) {
1553 dev_dbg(&pdev->dev, "missing TCSR memory resource\n");
1554 } else {
1555 tcsr = devm_ioremap_nocache(&pdev->dev, res->start,
1556 resource_size(res));
1557 if (!tcsr) {
1558 dev_dbg(&pdev->dev, "tcsr ioremap failed\n");
1559 } else {
1560 /* Enable USB3 on the primary USB port. */
1561 writel_relaxed(0x1, tcsr);
1562 /*
1563 * Ensure that TCSR write is completed before
1564 * USB registers initialization.
1565 */
1566 mb();
1567 }
1568 }
1569
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001570 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1571 if (!res) {
1572 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301573 ret = -ENODEV;
1574 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001575 }
1576
1577 msm->base = devm_ioremap_nocache(&pdev->dev, res->start,
1578 resource_size(res));
1579 if (!msm->base) {
1580 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301581 ret = -ENODEV;
1582 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001583 }
1584
Ido Shayevitzca2691e2012-04-17 15:54:53 +03001585 dwc3 = platform_device_alloc("dwc3", -1);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001586 if (!dwc3) {
1587 dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301588 ret = -ENODEV;
1589 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001590 }
1591
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001592 dwc3->dev.parent = &pdev->dev;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +03001593 dwc3->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1594 dwc3->dev.dma_mask = &dwc3_msm_dma_mask;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001595 dwc3->dev.dma_parms = pdev->dev.dma_parms;
1596 msm->resource_size = resource_size(res);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001597 msm->dwc3 = dwc3;
1598
Manu Gautambd0e5782012-08-30 10:39:01 -07001599 /* SSPHY Initialization: Use ref_clk from pads and set its parameters */
1600 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1601 msleep(30);
1602 /* Assert SSPHY reset */
1603 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210082);
1604 usleep_range(2000, 2200);
1605 /* De-assert SSPHY reset - power and ref_clock must be ON */
1606 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1607 usleep_range(2000, 2200);
1608 /* Ref clock must be stable now, enable ref clock for HS mode */
1609 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210102);
1610 usleep_range(2000, 2200);
1611 /*
1612 * HSPHY Initialization: Enable UTMI clock and clamp enable HVINTs,
1613 * and disable RETENTION (power-on default is ENABLED)
1614 */
1615 dwc3_msm_write_reg(msm->base, HS_PHY_CTRL_REG, 0x5220bb2);
1616 usleep_range(2000, 2200);
1617 /* Disable (bypass) VBUS filter */
1618 dwc3_msm_write_reg(msm->base, QSCRATCH_GENERAL_CFG, 0x38);
1619
Manu Gautamb5067272012-07-02 09:53:41 +05301620 pm_runtime_set_active(msm->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05301621
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001622 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
1623 &msm->dbm_num_eps)) {
1624 dev_err(&pdev->dev,
1625 "unable to read platform data num of dbm eps\n");
1626 msm->dbm_num_eps = DBM_MAX_EPS;
1627 }
1628
1629 if (msm->dbm_num_eps > DBM_MAX_EPS) {
1630 dev_err(&pdev->dev,
1631 "Driver doesn't support number of DBM EPs. "
1632 "max: %d, dbm_num_eps: %d\n",
1633 DBM_MAX_EPS, msm->dbm_num_eps);
1634 ret = -ENODEV;
Manu Gautam60e01352012-05-29 09:00:34 +05301635 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001636 }
1637
1638 ret = platform_device_add_resources(dwc3, pdev->resource,
1639 pdev->num_resources);
1640 if (ret) {
1641 dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301642 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001643 }
1644
1645 ret = platform_device_add(dwc3);
1646 if (ret) {
1647 dev_err(&pdev->dev, "failed to register dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301648 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001649 }
1650
Manu Gautam2617deb2012-08-31 17:50:06 -07001651 msm->bus_scale_table = msm_bus_cl_get_pdata(pdev);
1652 if (!msm->bus_scale_table) {
1653 dev_err(&pdev->dev, "bus scaling is disabled\n");
1654 } else {
1655 msm->bus_perf_client =
1656 msm_bus_scale_register_client(msm->bus_scale_table);
1657 ret = msm_bus_scale_client_update_request(
1658 msm->bus_perf_client, 1);
1659 if (ret)
1660 dev_err(&pdev->dev, "Failed to vote for bus scaling\n");
1661 }
1662
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001663 /* Reset the DBM */
Shimrit Malichia00d7322012-08-05 13:56:28 +03001664 dwc3_msm_dbm_soft_reset(1);
1665 usleep_range(1000, 1200);
1666 dwc3_msm_dbm_soft_reset(0);
1667
1668 dwc3_msm_event_buffer_config(dwc3_readl(msm->base, DWC3_GEVNTADRLO(0)),
1669 dwc3_readl(msm->base, DWC3_GEVNTSIZ(0)));
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001670
Manu Gautam8c642812012-06-07 10:35:10 +05301671 msm->otg_xceiv = usb_get_transceiver();
1672 if (msm->otg_xceiv) {
1673 msm->charger.start_detection = dwc3_start_chg_det;
1674 ret = dwc3_set_charger(msm->otg_xceiv->otg, &msm->charger);
1675 if (ret || !msm->charger.notify_detection_complete) {
1676 dev_err(&pdev->dev, "failed to register charger: %d\n",
1677 ret);
1678 goto put_xcvr;
1679 }
Manu Gautamb5067272012-07-02 09:53:41 +05301680
1681 ret = dwc3_set_ext_xceiv(msm->otg_xceiv->otg, &msm->ext_xceiv);
1682 if (ret || !msm->ext_xceiv.notify_ext_events) {
1683 dev_err(&pdev->dev, "failed to register xceiver: %d\n",
1684 ret);
1685 goto put_xcvr;
1686 }
Manu Gautam8c642812012-06-07 10:35:10 +05301687 } else {
1688 dev_err(&pdev->dev, "%s: No OTG transceiver found\n", __func__);
1689 }
1690
Manu Gautamb5067272012-07-02 09:53:41 +05301691 wake_lock_init(&msm->wlock, WAKE_LOCK_SUSPEND, "msm_dwc3");
1692 wake_lock(&msm->wlock);
1693 dwc3_debugfs_init(msm);
1694
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001695 return 0;
1696
Manu Gautam8c642812012-06-07 10:35:10 +05301697put_xcvr:
1698 usb_put_transceiver(msm->otg_xceiv);
1699 platform_device_del(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05301700put_pdev:
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001701 platform_device_put(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05301702disable_hs_ldo:
1703 dwc3_hsusb_ldo_enable(0);
1704free_hs_ldo_init:
1705 dwc3_hsusb_ldo_init(0);
1706disable_hs_vddcx:
1707 regulator_disable(context->hsusb_vddcx);
1708unconfig_hs_vddcx:
1709 dwc3_hsusb_config_vddcx(0);
1710disable_ss_ldo:
1711 dwc3_ssusb_ldo_enable(0);
1712free_ss_ldo_init:
1713 dwc3_ssusb_ldo_init(0);
1714disable_ss_vddcx:
1715 regulator_disable(context->ssusb_vddcx);
1716unconfig_ss_vddcx:
1717 dwc3_ssusb_config_vddcx(0);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001718disable_ref_clk:
1719 clk_disable_unprepare(msm->ref_clk);
1720disable_sleep_a_clk:
1721 clk_disable_unprepare(msm->hsphy_sleep_clk);
1722disable_sleep_clk:
1723 clk_disable_unprepare(msm->sleep_clk);
1724disable_iface_clk:
1725 clk_disable_unprepare(msm->iface_clk);
Manu Gautam1742db22012-06-19 13:33:24 +05301726disable_core_clk:
1727 clk_disable_unprepare(msm->core_clk);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001728
1729 return ret;
1730}
1731
1732static int __devexit dwc3_msm_remove(struct platform_device *pdev)
1733{
1734 struct dwc3_msm *msm = platform_get_drvdata(pdev);
1735
Manu Gautamb5067272012-07-02 09:53:41 +05301736 if (dwc3_debugfs_root)
1737 debugfs_remove_recursive(dwc3_debugfs_root);
Manu Gautam8c642812012-06-07 10:35:10 +05301738 if (msm->otg_xceiv) {
1739 dwc3_start_chg_det(&msm->charger, false);
1740 usb_put_transceiver(msm->otg_xceiv);
1741 }
Manu Gautamb5067272012-07-02 09:53:41 +05301742 pm_runtime_disable(msm->dev);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001743 platform_device_unregister(msm->dwc3);
Manu Gautamb5067272012-07-02 09:53:41 +05301744 wake_lock_destroy(&msm->wlock);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001745
Manu Gautam60e01352012-05-29 09:00:34 +05301746 dwc3_hsusb_ldo_enable(0);
1747 dwc3_hsusb_ldo_init(0);
1748 regulator_disable(msm->hsusb_vddcx);
1749 dwc3_hsusb_config_vddcx(0);
1750 dwc3_ssusb_ldo_enable(0);
1751 dwc3_ssusb_ldo_init(0);
1752 regulator_disable(msm->ssusb_vddcx);
1753 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05301754 clk_disable_unprepare(msm->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001755 clk_disable_unprepare(msm->iface_clk);
1756 clk_disable_unprepare(msm->sleep_clk);
1757 clk_disable_unprepare(msm->hsphy_sleep_clk);
1758 clk_disable_unprepare(msm->ref_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05301759
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001760 return 0;
1761}
1762
Manu Gautamb5067272012-07-02 09:53:41 +05301763static int dwc3_msm_pm_suspend(struct device *dev)
1764{
1765 int ret = 0;
1766 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1767
1768 dev_dbg(dev, "dwc3-msm PM suspend\n");
1769
1770 ret = dwc3_msm_suspend(mdwc);
1771 if (!ret)
1772 atomic_set(&mdwc->pm_suspended, 1);
1773
1774 return ret;
1775}
1776
1777static int dwc3_msm_pm_resume(struct device *dev)
1778{
1779 int ret = 0;
1780 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1781
1782 dev_dbg(dev, "dwc3-msm PM resume\n");
1783
1784 atomic_set(&mdwc->pm_suspended, 0);
1785 if (mdwc->resume_pending) {
1786 mdwc->resume_pending = false;
1787
1788 ret = dwc3_msm_resume(mdwc);
1789 /* Update runtime PM status */
1790 pm_runtime_disable(dev);
1791 pm_runtime_set_active(dev);
1792 pm_runtime_enable(dev);
1793
1794 /* Let OTG know about resume event and update pm_count */
1795 if (mdwc->otg_xceiv)
1796 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1797 DWC3_EVENT_PHY_RESUME);
1798 }
1799
1800 return ret;
1801}
1802
1803static int dwc3_msm_runtime_idle(struct device *dev)
1804{
1805 dev_dbg(dev, "DWC3-msm runtime idle\n");
1806
1807 return 0;
1808}
1809
1810static int dwc3_msm_runtime_suspend(struct device *dev)
1811{
1812 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1813
1814 dev_dbg(dev, "DWC3-msm runtime suspend\n");
1815
1816 return dwc3_msm_suspend(mdwc);
1817}
1818
1819static int dwc3_msm_runtime_resume(struct device *dev)
1820{
1821 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
1822
1823 dev_dbg(dev, "DWC3-msm runtime resume\n");
1824
1825 return dwc3_msm_resume(mdwc);
1826}
1827
1828static const struct dev_pm_ops dwc3_msm_dev_pm_ops = {
1829 SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume)
1830 SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume,
1831 dwc3_msm_runtime_idle)
1832};
1833
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001834static const struct of_device_id of_dwc3_matach[] = {
1835 {
1836 .compatible = "qcom,dwc-usb3-msm",
1837 },
1838 { },
1839};
1840MODULE_DEVICE_TABLE(of, of_dwc3_matach);
1841
1842static struct platform_driver dwc3_msm_driver = {
1843 .probe = dwc3_msm_probe,
1844 .remove = __devexit_p(dwc3_msm_remove),
1845 .driver = {
1846 .name = "msm-dwc3",
Manu Gautamb5067272012-07-02 09:53:41 +05301847 .pm = &dwc3_msm_dev_pm_ops,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001848 .of_match_table = of_dwc3_matach,
1849 },
1850};
1851
1852MODULE_LICENSE("GPLV2");
1853MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
1854
1855static int __devinit dwc3_msm_init(void)
1856{
1857 return platform_driver_register(&dwc3_msm_driver);
1858}
1859module_init(dwc3_msm_init);
1860
1861static void __exit dwc3_msm_exit(void)
1862{
1863 platform_driver_unregister(&dwc3_msm_driver);
1864}
1865module_exit(dwc3_msm_exit);