blob: 05f1a60c24b29ffd67de4fb01eaaee5004d4b198 [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>
19#include <linux/ioport.h>
Manu Gautam1742db22012-06-19 13:33:24 +053020#include <linux/clk.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020021#include <linux/io.h>
22#include <linux/module.h>
23#include <linux/types.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020024#include <linux/delay.h>
25#include <linux/of.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030026#include <linux/list.h>
27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
29#include <linux/usb/msm_hsusb.h>
Manu Gautam60e01352012-05-29 09:00:34 +053030#include <linux/regulator/consumer.h>
31
32#include <mach/rpm-regulator.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030033
Manu Gautam8c642812012-06-07 10:35:10 +053034#include "dwc3_otg.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030035#include "core.h"
36#include "gadget.h"
37
38/**
39 * USB DBM Hardware registers.
40 *
41 */
42#define DBM_EP_CFG(n) (0x00 + 4 * (n))
43#define DBM_DATA_FIFO(n) (0x10 + 4 * (n))
44#define DBM_DATA_FIFO_SIZE(n) (0x20 + 4 * (n))
45#define DBM_DATA_FIFO_EN (0x30)
46#define DBM_GEVNTADR (0x34)
47#define DBM_GEVNTSIZ (0x38)
48#define DBM_DBG_CNFG (0x3C)
49#define DBM_HW_TRB0_EP(n) (0x40 + 4 * (n))
50#define DBM_HW_TRB1_EP(n) (0x50 + 4 * (n))
51#define DBM_HW_TRB2_EP(n) (0x60 + 4 * (n))
52#define DBM_HW_TRB3_EP(n) (0x70 + 4 * (n))
53#define DBM_PIPE_CFG (0x80)
54#define DBM_SOFT_RESET (0x84)
55
56/**
57 * USB DBM Hardware registers bitmask.
58 *
59 */
60/* DBM_EP_CFG */
61#define DBM_EN_EP 0x00000000
62#define DBM_USB3_EP_NUM 0x0000003E
63#define DBM_BAM_PIPE_NUM 0x000000C0
64#define DBM_PRODUCER 0x00000100
65#define DBM_DISABLE_WB 0x00000200
66#define DBM_INT_RAM_ACC 0x00000400
67
68/* DBM_DATA_FIFO_SIZE */
69#define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff
70
71/* DBM_GEVNTSIZ */
72#define DBM_GEVNTSIZ_MASK 0x0000ffff
73
74/* DBM_DBG_CNFG */
75#define DBM_ENABLE_IOC_MASK 0x0000000f
76
77/* DBM_SOFT_RESET */
78#define DBM_SFT_RST_EP0 0x00000001
79#define DBM_SFT_RST_EP1 0x00000002
80#define DBM_SFT_RST_EP2 0x00000004
81#define DBM_SFT_RST_EP3 0x00000008
82#define DBM_SFT_RST_EPS 0x0000000F
83#define DBM_SFT_RST 0x80000000
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020084
85#define DBM_MAX_EPS 4
86
Ido Shayevitzfa65a582012-06-06 14:39:54 +030087/* DBM TRB configurations */
88#define DBM_TRB_BIT 0x80000000
89#define DBM_TRB_DATA_SRC 0x40000000
90#define DBM_TRB_DMA 0x20000000
91#define DBM_TRB_EP_NUM(ep) (ep<<24)
Manu Gautam8c642812012-06-07 10:35:10 +053092/**
93 * USB QSCRATCH Hardware registers
94 *
95 */
96#define QSCRATCH_REG_OFFSET (0x000F8800)
97#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
98#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
99#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
100#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
101
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300102
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300103struct dwc3_msm_req_complete {
104 struct list_head list_item;
105 struct usb_request *req;
106 void (*orig_complete)(struct usb_ep *ep,
107 struct usb_request *req);
108};
109
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200110struct dwc3_msm {
111 struct platform_device *dwc3;
112 struct device *dev;
113 void __iomem *base;
114 u32 resource_size;
115 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300116 u8 ep_num_mapping[DBM_MAX_EPS];
117 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
118 struct list_head req_complete_list;
Manu Gautam1742db22012-06-19 13:33:24 +0530119 struct clk *core_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530120 struct regulator *hsusb_3p3;
121 struct regulator *hsusb_1p8;
122 struct regulator *hsusb_vddcx;
123 struct regulator *ssusb_1p8;
124 struct regulator *ssusb_vddcx;
125 enum usb_vdd_type ss_vdd_type;
126 enum usb_vdd_type hs_vdd_type;
Manu Gautam8c642812012-06-07 10:35:10 +0530127 struct dwc3_charger charger;
128 struct usb_phy *otg_xceiv;
129 struct delayed_work chg_work;
130 enum usb_chg_state chg_state;
131 u8 dcd_retries;
Manu Gautam60e01352012-05-29 09:00:34 +0530132};
133
134#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
135#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
136#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
137
138#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
139#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
140#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
141
142#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
143#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
144#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
145
146#define USB_PHY_VDD_DIG_VOL_NONE 0 /* uV */
147#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
148#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
149
Manu Gautam60e01352012-05-29 09:00:34 +0530150static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
151 { /* VDD_CX CORNER Voting */
152 [VDD_NONE] = RPM_VREG_CORNER_NONE,
153 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
154 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
155 },
156 { /* VDD_CX Voltage Voting */
157 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
158 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
159 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
160 },
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200161};
162
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300163static struct dwc3_msm *context;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +0300164static u64 dwc3_msm_dma_mask = DMA_BIT_MASK(64);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300165
166/**
167 *
168 * Read register with debug info.
169 *
170 * @base - DWC3 base virtual address.
171 * @offset - register offset.
172 *
173 * @return u32
174 */
175static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
176{
177 u32 val = ioread32(base + offset);
178 return val;
179}
180
181/**
182 * Read register masked field with debug info.
183 *
184 * @base - DWC3 base virtual address.
185 * @offset - register offset.
186 * @mask - register bitmask.
187 *
188 * @return u32
189 */
190static inline u32 dwc3_msm_read_reg_field(void *base,
191 u32 offset,
192 const u32 mask)
193{
194 u32 shift = find_first_bit((void *)&mask, 32);
195 u32 val = ioread32(base + offset);
196 val &= mask; /* clear other bits */
197 val >>= shift;
198 return val;
199}
200
201/**
202 *
203 * Write register with debug info.
204 *
205 * @base - DWC3 base virtual address.
206 * @offset - register offset.
207 * @val - value to write.
208 *
209 */
210static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
211{
212 iowrite32(val, base + offset);
213}
214
215/**
216 * Write register masked field with debug info.
217 *
218 * @base - DWC3 base virtual address.
219 * @offset - register offset.
220 * @mask - register bitmask.
221 * @val - value to write.
222 *
223 */
224static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
225 const u32 mask, u32 val)
226{
227 u32 shift = find_first_bit((void *)&mask, 32);
228 u32 tmp = ioread32(base + offset);
229
230 tmp &= ~mask; /* clear written bits */
231 val = tmp | (val << shift);
232 iowrite32(val, base + offset);
233}
234
235/**
Manu Gautam8c642812012-06-07 10:35:10 +0530236 * Write register and read back masked value to confirm it is written
237 *
238 * @base - DWC3 base virtual address.
239 * @offset - register offset.
240 * @mask - register bitmask specifying what should be updated
241 * @val - value to write.
242 *
243 */
244static inline void dwc3_msm_write_readback(void *base, u32 offset,
245 const u32 mask, u32 val)
246{
247 u32 write_val, tmp = ioread32(base + offset);
248
249 tmp &= ~mask; /* retain other bits */
250 write_val = tmp | val;
251
252 iowrite32(write_val, base + offset);
253
254 /* Read back to see if val was written */
255 tmp = ioread32(base + offset);
256 tmp &= mask; /* clear other bits */
257
258 if (tmp != val)
259 dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n",
260 __func__, val, offset);
261}
262
263/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300264 * Return DBM EP number which is not already configured.
265 *
266 */
267static int dwc3_msm_find_avail_dbm_ep(void)
268{
269 int i;
270
271 for (i = 0; i < context->dbm_num_eps; i++)
272 if (!context->ep_num_mapping[i])
273 return i;
274
275 return -ENODEV; /* Not found */
276}
277
278/**
279 * Return DBM EP number according to usb endpoint number.
280 *
281 */
282static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep)
283{
284 int i;
285
286 for (i = 0; i < context->dbm_num_eps; i++)
287 if (context->ep_num_mapping[i] == usb_ep)
288 return i;
289
290 return -ENODEV; /* Not found */
291}
292
293/**
294 * Return number of configured DBM endpoints.
295 *
296 */
297static int dwc3_msm_configured_dbm_ep_num(void)
298{
299 int i;
300 int count = 0;
301
302 for (i = 0; i < context->dbm_num_eps; i++)
303 if (context->ep_num_mapping[i])
304 count++;
305
306 return count;
307}
308
309/**
310 * Configure the DBM with the USB3 core event buffer.
311 * This function is called by the SNPS UDC upon initialization.
312 *
313 * @addr - address of the event buffer.
314 * @size - size of the event buffer.
315 *
316 */
317static int dwc3_msm_event_buffer_config(u32 addr, u16 size)
318{
319 dev_dbg(context->dev, "%s\n", __func__);
320
321 dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr);
322 dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ,
323 DBM_GEVNTSIZ_MASK, size);
324
325 return 0;
326}
327
328/**
329 * Reset the DBM registers upon initialization.
330 *
331 */
332static int dwc3_msm_dbm_soft_reset(void)
333{
334 dev_dbg(context->dev, "%s\n", __func__);
335
336 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
337 DBM_SFT_RST, 1);
338
339 return 0;
340}
341
342/**
343 * Soft reset specific DBM ep.
344 * This function is called by the function driver upon events
345 * such as transfer aborting, USB re-enumeration and USB
346 * disconnection.
347 *
348 * @dbm_ep - DBM ep number.
349 * @enter_reset - should we enter a reset state or get out of it.
350 *
351 */
352static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset)
353{
354 dev_dbg(context->dev, "%s\n", __func__);
355
356 if (dbm_ep >= context->dbm_num_eps) {
357 dev_err(context->dev,
358 "%s: Invalid DBM ep index\n", __func__);
359 return -ENODEV;
360 }
361
362 if (enter_reset) {
363 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
364 DBM_SFT_RST_EPS, 1 << dbm_ep);
365 } else {
366 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
367 DBM_SFT_RST_EPS, 0);
368 }
369
370 return 0;
371}
372
373/**
374 * Configure a USB DBM ep to work in BAM mode.
375 *
376 *
377 * @usb_ep - USB physical EP number.
378 * @producer - producer/consumer.
379 * @disable_wb - disable write back to system memory.
380 * @internal_mem - use internal USB memory for data fifo.
381 * @ioc - enable interrupt on completion.
382 *
383 * @return int - DBM ep number.
384 */
385static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe,
386 bool producer, bool disable_wb,
387 bool internal_mem, bool ioc)
388{
389 u8 dbm_ep;
390 u8 ioc_mask;
391
392 dev_dbg(context->dev, "%s\n", __func__);
393
394 dbm_ep = dwc3_msm_find_avail_dbm_ep();
395 if (dbm_ep < 0) {
396 dev_err(context->dev, "%s: No more DBM eps\n", __func__);
397 return -ENODEV;
398 }
399
400 context->ep_num_mapping[dbm_ep] = usb_ep;
401
402 /* First, reset the dbm endpoint */
403 dwc3_msm_dbm_ep_soft_reset(dbm_ep, false);
404
405 ioc_mask = dwc3_msm_read_reg_field(context->base, DBM_DBG_CNFG,
406 DBM_ENABLE_IOC_MASK);
407 ioc_mask &= ~(ioc << dbm_ep); /* Clear ioc bit for dbm_ep */
408 /* Set ioc bit for dbm_ep if needed */
409 dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG,
410 DBM_ENABLE_IOC_MASK, ioc_mask | (ioc << dbm_ep));
411
412 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep),
413 producer | disable_wb | internal_mem);
414 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
415 DBM_USB3_EP_NUM, usb_ep);
416 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
417 DBM_BAM_PIPE_NUM, bam_pipe);
418 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
419 DBM_EN_EP, 1);
420
421 return dbm_ep;
422}
423
424/**
425 * Configure a USB DBM ep to work in normal mode.
426 *
427 * @usb_ep - USB ep number.
428 *
429 */
430static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep)
431{
432 u8 dbm_ep;
433
434 dev_dbg(context->dev, "%s\n", __func__);
435
436 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
437
438 if (dbm_ep < 0) {
439 dev_err(context->dev,
440 "%s: Invalid usb ep index\n", __func__);
441 return -ENODEV;
442 }
443
444 context->ep_num_mapping[dbm_ep] = 0;
445
446 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), 0);
447
448 /* Reset the dbm endpoint */
449 dwc3_msm_dbm_ep_soft_reset(dbm_ep, true);
450
451 return 0;
452}
453
454/**
455 * Configure the DBM with the BAM's data fifo.
456 * This function is called by the USB BAM Driver
457 * upon initialization.
458 *
459 * @ep - pointer to usb endpoint.
460 * @addr - address of data fifo.
461 * @size - size of data fifo.
462 *
463 */
464int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size)
465{
466 u8 dbm_ep;
467 struct dwc3_ep *dep = to_dwc3_ep(ep);
468
469 dev_dbg(context->dev, "%s\n", __func__);
470
471 dbm_ep = dwc3_msm_find_matching_dbm_ep(dep->number);
472
473 if (dbm_ep >= context->dbm_num_eps) {
474 dev_err(context->dev,
475 "%s: Invalid DBM ep index\n", __func__);
476 return -ENODEV;
477 }
478
479 dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr);
480 dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep),
481 DBM_DATA_FIFO_SIZE_MASK, size);
482
483 return 0;
484}
485
486/**
487* Cleanups for msm endpoint on request complete.
488*
489* Also call original request complete.
490*
491* @usb_ep - pointer to usb_ep instance.
492* @request - pointer to usb_request instance.
493*
494* @return int - 0 on success, negetive on error.
495*/
496static void dwc3_msm_req_complete_func(struct usb_ep *ep,
497 struct usb_request *request)
498{
499 struct dwc3_request *req = to_dwc3_request(request);
500 struct dwc3_ep *dep = to_dwc3_ep(ep);
501 struct dwc3_msm_req_complete *req_complete = NULL;
502
503 /* Find original request complete function and remove it from list */
504 list_for_each_entry(req_complete,
505 &context->req_complete_list,
506 list_item) {
507 if (req_complete->req == request)
508 break;
509 }
510 if (!req_complete || req_complete->req != request) {
511 dev_err(dep->dwc->dev, "%s: could not find the request\n",
512 __func__);
513 return;
514 }
515 list_del(&req_complete->list_item);
516
517 /*
518 * Release another one TRB to the pool since DBM queue took 2 TRBs
519 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
520 * released only one.
521 */
522 if (req->queued)
523 dep->busy_slot++;
524
525 /* Unconfigure dbm ep */
526 dwc3_msm_dbm_ep_unconfig(dep->number);
527
528 /*
529 * If this is the last endpoint we unconfigured, than reset also
530 * the event buffers.
531 */
532 if (0 == dwc3_msm_configured_dbm_ep_num())
533 dwc3_msm_event_buffer_config(0, 0);
534
535 /*
536 * Call original complete function, notice that dwc->lock is already
537 * taken by the caller of this function (dwc3_gadget_giveback()).
538 */
539 request->complete = req_complete->orig_complete;
540 request->complete(ep, request);
541
542 kfree(req_complete);
543}
544
545/**
546* Helper function.
547* See the header of the dwc3_msm_ep_queue function.
548*
549* @dwc3_ep - pointer to dwc3_ep instance.
550* @req - pointer to dwc3_request instance.
551*
552* @return int - 0 on success, negetive on error.
553*/
554static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
555{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300556 struct dwc3_trb *trb;
557 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300558 struct dwc3_gadget_ep_cmd_params params;
559 u32 cmd;
560 int ret = 0;
561
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300562 /* We push the request to the dep->req_queued list to indicate that
563 * this request is issued with start transfer. The request will be out
564 * from this list in 2 cases. The first is that the transfer will be
565 * completed (not if the transfer is endless using a circular TRBs with
566 * with link TRB). The second case is an option to do stop stransfer,
567 * this can be initiated by the function driver when calling dequeue.
568 */
569 req->queued = true;
570 list_add_tail(&req->list, &dep->req_queued);
571
572 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300573 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300574 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300575 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300576
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300577 req->trb = trb;
578 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
579 trb->bph = DBM_TRB_BIT | DBM_TRB_DATA_SRC |
580 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
581 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
582 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300583
584 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300585 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300586 dep->free_slot++;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300587
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300588 trb_link->bpl = lower_32_bits(req->trb_dma);
589 trb_link->bph = DBM_TRB_BIT | DBM_TRB_DATA_SRC |
590 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
591 trb_link->size = 0;
592 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300593
594 /*
595 * Now start the transfer
596 */
597 memset(&params, 0, sizeof(params));
598 params.param0 = upper_32_bits(req->trb_dma);
599 params.param1 = lower_32_bits(req->trb_dma);
600 cmd = DWC3_DEPCMD_STARTTRANSFER;
601 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
602 if (ret < 0) {
603 dev_dbg(dep->dwc->dev,
604 "%s: failed to send STARTTRANSFER command\n",
605 __func__);
606
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300607 list_del(&req->list);
608 return ret;
609 }
610
611 return ret;
612}
613
614/**
615* Queue a usb request to the DBM endpoint.
616* This function should be called after the endpoint
617* was enabled by the ep_enable.
618*
619* This function prepares special structure of TRBs which
620* is familier with the DBM HW, so it will possible to use
621* this endpoint in DBM mode.
622*
623* The TRBs prepared by this function, is one normal TRB
624* which point to a fake buffer, followed by a link TRB
625* that points to the first TRB.
626*
627* The API of this function follow the regular API of
628* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
629*
630* @usb_ep - pointer to usb_ep instance.
631* @request - pointer to usb_request instance.
632* @gfp_flags - possible flags.
633*
634* @return int - 0 on success, negetive on error.
635*/
636static int dwc3_msm_ep_queue(struct usb_ep *ep,
637 struct usb_request *request, gfp_t gfp_flags)
638{
639 struct dwc3_request *req = to_dwc3_request(request);
640 struct dwc3_ep *dep = to_dwc3_ep(ep);
641 struct dwc3 *dwc = dep->dwc;
642 struct dwc3_msm_req_complete *req_complete;
643 unsigned long flags;
644 int ret = 0;
645 u8 bam_pipe;
646 bool producer;
647 bool disable_wb;
648 bool internal_mem;
649 bool ioc;
650
651 if (!(request->udc_priv & MSM_SPS_MODE)) {
652 /* Not SPS mode, call original queue */
653 dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n",
654 __func__);
655
656 return (context->original_ep_ops[dep->number])->queue(ep,
657 request,
658 gfp_flags);
659 }
660
661 if (!dep->endpoint.desc) {
662 dev_err(dwc->dev,
663 "%s: trying to queue request %p to disabled ep %s\n",
664 __func__, request, ep->name);
665 return -EPERM;
666 }
667
668 if (dep->number == 0 || dep->number == 1) {
669 dev_err(dwc->dev,
670 "%s: trying to queue dbm request %p to control ep %s\n",
671 __func__, request, ep->name);
672 return -EPERM;
673 }
674
675 if (dep->free_slot > 0 || dep->busy_slot > 0 ||
676 !list_empty(&dep->request_list) ||
677 !list_empty(&dep->req_queued)) {
678
679 dev_err(dwc->dev,
680 "%s: trying to queue dbm request %p tp ep %s\n",
681 __func__, request, ep->name);
682 return -EPERM;
683 }
684
685 /*
686 * Override req->complete function, but before doing that,
687 * store it's original pointer in the req_complete_list.
688 */
689 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
690 if (!req_complete) {
691 dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__);
692 return -ENOMEM;
693 }
694 req_complete->req = request;
695 req_complete->orig_complete = request->complete;
696 list_add_tail(&req_complete->list_item, &context->req_complete_list);
697 request->complete = dwc3_msm_req_complete_func;
698
699 /*
700 * Configure dbm event buffers if this is the first
701 * dbm endpoint we about to configure.
702 */
703 if (0 == dwc3_msm_configured_dbm_ep_num())
704 dwc3_msm_event_buffer_config(dwc->ev_buffs[0]->dma,
705 dwc->ev_buffs[0]->length);
706
707 /*
708 * Configure the DBM endpoint
709 */
710 bam_pipe = (request->udc_priv & MSM_PIPE_ID_MASK);
711 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
712 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
713 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
714 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
715
716 ret = dwc3_msm_dbm_ep_config(dep->number,
717 bam_pipe, producer,
718 disable_wb, internal_mem, ioc);
719 if (ret < 0) {
720 dev_err(context->dev,
721 "error %d after calling dwc3_msm_dbm_ep_config\n",
722 ret);
723 return ret;
724 }
725
726 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
727 __func__, request, ep->name, request->length);
728
729 /*
730 * We must obtain the lock of the dwc3 core driver,
731 * including disabling interrupts, so we will be sure
732 * that we are the only ones that configure the HW device
733 * core and ensure that we queuing the request will finish
734 * as soon as possible so we will release back the lock.
735 */
736 spin_lock_irqsave(&dwc->lock, flags);
737 ret = __dwc3_msm_ep_queue(dep, req);
738 spin_unlock_irqrestore(&dwc->lock, flags);
739 if (ret < 0) {
740 dev_err(context->dev,
741 "error %d after calling __dwc3_msm_ep_queue\n", ret);
742 return ret;
743 }
744
745 return 0;
746}
747
748/**
749 * Configure MSM endpoint.
750 * This function do specific configurations
751 * to an endpoint which need specific implementaion
752 * in the MSM architecture.
753 *
754 * This function should be called by usb function/class
755 * layer which need a support from the specific MSM HW
756 * which wrap the USB3 core. (like DBM specific endpoints)
757 *
758 * @ep - a pointer to some usb_ep instance
759 *
760 * @return int - 0 on success, negetive on error.
761 */
762int msm_ep_config(struct usb_ep *ep)
763{
764 struct dwc3_ep *dep = to_dwc3_ep(ep);
765 struct usb_ep_ops *new_ep_ops;
766
767 /* Save original ep ops for future restore*/
768 if (context->original_ep_ops[dep->number]) {
769 dev_err(context->dev,
770 "ep [%s,%d] already configured as msm endpoint\n",
771 ep->name, dep->number);
772 return -EPERM;
773 }
774 context->original_ep_ops[dep->number] = ep->ops;
775
776 /* Set new usb ops as we like */
777 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
778 if (!new_ep_ops) {
779 dev_err(context->dev,
780 "%s: unable to allocate mem for new usb ep ops\n",
781 __func__);
782 return -ENOMEM;
783 }
784 (*new_ep_ops) = (*ep->ops);
785 new_ep_ops->queue = dwc3_msm_ep_queue;
786 ep->ops = new_ep_ops;
787
788 /*
789 * Do HERE more usb endpoint configurations
790 * which are specific to MSM.
791 */
792
793 return 0;
794}
795EXPORT_SYMBOL(msm_ep_config);
796
797/**
798 * Un-configure MSM endpoint.
799 * Tear down configurations done in the
800 * dwc3_msm_ep_config function.
801 *
802 * @ep - a pointer to some usb_ep instance
803 *
804 * @return int - 0 on success, negetive on error.
805 */
806int msm_ep_unconfig(struct usb_ep *ep)
807{
808 struct dwc3_ep *dep = to_dwc3_ep(ep);
809 struct usb_ep_ops *old_ep_ops;
810
811 /* Restore original ep ops */
812 if (!context->original_ep_ops[dep->number]) {
813 dev_err(context->dev,
814 "ep [%s,%d] was not configured as msm endpoint\n",
815 ep->name, dep->number);
816 return -EINVAL;
817 }
818 old_ep_ops = (struct usb_ep_ops *)ep->ops;
819 ep->ops = context->original_ep_ops[dep->number];
820 context->original_ep_ops[dep->number] = NULL;
821 kfree(old_ep_ops);
822
823 /*
824 * Do HERE more usb endpoint un-configurations
825 * which are specific to MSM.
826 */
827
828 return 0;
829}
830EXPORT_SYMBOL(msm_ep_unconfig);
831
Manu Gautam60e01352012-05-29 09:00:34 +0530832/* HSPHY */
833static int dwc3_hsusb_config_vddcx(int high)
834{
835 int min_vol, ret;
836 struct dwc3_msm *dwc = context;
837 enum usb_vdd_type vdd_type = context->hs_vdd_type;
838 int max_vol = vdd_val[vdd_type][VDD_MAX];
839
840 min_vol = vdd_val[vdd_type][high ? VDD_MIN : VDD_NONE];
841 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
842 if (ret) {
843 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
844 return ret;
845 }
846
847 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
848 min_vol, max_vol);
849
850 return ret;
851}
852
853static int dwc3_hsusb_ldo_init(int init)
854{
855 int rc = 0;
856 struct dwc3_msm *dwc = context;
857
858 if (!init) {
859 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
860 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
861 return 0;
862 }
863
864 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
865 if (IS_ERR(dwc->hsusb_3p3)) {
866 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
867 return PTR_ERR(dwc->hsusb_3p3);
868 }
869
870 rc = regulator_set_voltage(dwc->hsusb_3p3,
871 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
872 if (rc) {
873 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
874 return rc;
875 }
876 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
877 if (IS_ERR(dwc->hsusb_1p8)) {
878 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
879 rc = PTR_ERR(dwc->hsusb_1p8);
880 goto devote_3p3;
881 }
882 rc = regulator_set_voltage(dwc->hsusb_1p8,
883 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
884 if (rc) {
885 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
886 goto devote_3p3;
887 }
888
889 return 0;
890
891devote_3p3:
892 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
893
894 return rc;
895}
896
897static int dwc3_hsusb_ldo_enable(int on)
898{
899 int rc = 0;
900 struct dwc3_msm *dwc = context;
901
902 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
903
904 if (!on)
905 goto disable_regulators;
906
907
908 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
909 if (rc < 0) {
910 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
911 return rc;
912 }
913
914 rc = regulator_enable(dwc->hsusb_1p8);
915 if (rc) {
916 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
917 goto put_1p8_lpm;
918 }
919
920 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
921 if (rc < 0) {
922 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
923 goto disable_1p8;
924 }
925
926 rc = regulator_enable(dwc->hsusb_3p3);
927 if (rc) {
928 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
929 goto put_3p3_lpm;
930 }
931
932 return 0;
933
934disable_regulators:
935 rc = regulator_disable(dwc->hsusb_3p3);
936 if (rc)
937 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
938
939put_3p3_lpm:
940 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
941 if (rc < 0)
942 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
943
944disable_1p8:
945 rc = regulator_disable(dwc->hsusb_1p8);
946 if (rc)
947 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
948
949put_1p8_lpm:
950 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
951 if (rc < 0)
952 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
953
954 return rc < 0 ? rc : 0;
955}
956
957/* SSPHY */
958static int dwc3_ssusb_config_vddcx(int high)
959{
960 int min_vol, ret;
961 struct dwc3_msm *dwc = context;
962 enum usb_vdd_type vdd_type = context->ss_vdd_type;
963 int max_vol = vdd_val[vdd_type][VDD_MAX];
964
965 min_vol = vdd_val[vdd_type][high ? VDD_MIN : VDD_NONE];
966 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
967 if (ret) {
968 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
969 return ret;
970 }
971
972 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
973 min_vol, max_vol);
974 return ret;
975}
976
977/* 3.3v supply not needed for SS PHY */
978static int dwc3_ssusb_ldo_init(int init)
979{
980 int rc = 0;
981 struct dwc3_msm *dwc = context;
982
983 if (!init) {
984 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
985 return 0;
986 }
987
988 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
989 if (IS_ERR(dwc->ssusb_1p8)) {
990 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
991 return PTR_ERR(dwc->ssusb_1p8);
992 }
993 rc = regulator_set_voltage(dwc->ssusb_1p8,
994 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
995 if (rc)
996 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
997
998 return rc;
999}
1000
1001static int dwc3_ssusb_ldo_enable(int on)
1002{
1003 int rc = 0;
1004 struct dwc3_msm *dwc = context;
1005
1006 dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1007
1008 if (!on)
1009 goto disable_regulators;
1010
1011
1012 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1013 if (rc < 0) {
1014 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1015 return rc;
1016 }
1017
1018 rc = regulator_enable(dwc->ssusb_1p8);
1019 if (rc) {
1020 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1021 goto put_1p8_lpm;
1022 }
1023
1024 return 0;
1025
1026disable_regulators:
1027 rc = regulator_disable(dwc->ssusb_1p8);
1028 if (rc)
1029 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1030
1031put_1p8_lpm:
1032 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1033 if (rc < 0)
1034 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1035
1036 return rc < 0 ? rc : 0;
1037}
1038
Manu Gautam8c642812012-06-07 10:35:10 +05301039static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1040{
1041 u32 chg_ctrl;
1042
1043 /* Turn off VDP_SRC */
1044 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1045 msleep(20);
1046
1047 /* Before proceeding make sure VDP_SRC is OFF */
1048 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1049 if (chg_ctrl & 0x3F)
1050 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1051 __func__, chg_ctrl);
1052 /*
1053 * Configure DM as current source, DP as current sink
1054 * and enable battery charging comparators.
1055 */
1056 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1057}
1058
1059static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1060{
1061 u32 chg_det;
1062 bool ret = false;
1063
1064 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1065 ret = chg_det & 1;
1066
1067 return ret;
1068}
1069
1070static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1071{
1072 /*
1073 * Configure DP as current source, DM as current sink
1074 * and enable battery charging comparators.
1075 */
1076 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1077}
1078
1079static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1080{
1081 u32 chg_state;
1082 bool ret = false;
1083
1084 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1085 ret = chg_state & 2;
1086
1087 return ret;
1088}
1089
1090static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1091{
1092 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1093}
1094
1095static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1096{
1097 /* Data contact detection enable, DCDENB */
1098 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1099}
1100
1101static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1102{
1103 u32 chg_ctrl;
1104
1105 /* Clear charger detecting control bits */
1106 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1107
1108 /* Clear alt interrupt latch and enable bits */
1109 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1110 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1111
1112 udelay(100);
1113
1114 /* Before proceeding make sure charger block is RESET */
1115 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1116 if (chg_ctrl & 0x3F)
1117 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1118 __func__, chg_ctrl);
1119}
1120
1121static const char *chg_to_string(enum dwc3_chg_type chg_type)
1122{
1123 switch (chg_type) {
1124 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1125 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1126 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1127 default: return "INVALID_CHARGER";
1128 }
1129}
1130
1131#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1132#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1133#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1134#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1135
1136static void dwc3_chg_detect_work(struct work_struct *w)
1137{
1138 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1139 bool is_dcd = false, tmout, vout;
1140 unsigned long delay;
1141
1142 dev_dbg(mdwc->dev, "chg detection work\n");
1143 switch (mdwc->chg_state) {
1144 case USB_CHG_STATE_UNDEFINED:
1145 dwc3_chg_block_reset(mdwc);
1146 dwc3_chg_enable_dcd(mdwc);
1147 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1148 mdwc->dcd_retries = 0;
1149 delay = DWC3_CHG_DCD_POLL_TIME;
1150 break;
1151 case USB_CHG_STATE_WAIT_FOR_DCD:
1152 is_dcd = dwc3_chg_check_dcd(mdwc);
1153 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1154 if (is_dcd || tmout) {
1155 dwc3_chg_disable_dcd(mdwc);
1156 dwc3_chg_enable_primary_det(mdwc);
1157 delay = DWC3_CHG_PRIMARY_DET_TIME;
1158 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1159 } else {
1160 delay = DWC3_CHG_DCD_POLL_TIME;
1161 }
1162 break;
1163 case USB_CHG_STATE_DCD_DONE:
1164 vout = dwc3_chg_det_check_output(mdwc);
1165 if (vout) {
1166 dwc3_chg_enable_secondary_det(mdwc);
1167 delay = DWC3_CHG_SECONDARY_DET_TIME;
1168 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1169 } else {
1170 mdwc->charger.chg_type = USB_SDP_CHARGER;
1171 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1172 delay = 0;
1173 }
1174 break;
1175 case USB_CHG_STATE_PRIMARY_DONE:
1176 vout = dwc3_chg_det_check_output(mdwc);
1177 if (vout)
1178 mdwc->charger.chg_type = USB_DCP_CHARGER;
1179 else
1180 mdwc->charger.chg_type = USB_CDP_CHARGER;
1181 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1182 /* fall through */
1183 case USB_CHG_STATE_SECONDARY_DONE:
1184 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1185 /* fall through */
1186 case USB_CHG_STATE_DETECTED:
1187 dwc3_chg_block_reset(mdwc);
1188 dev_dbg(mdwc->dev, "chg_type = %s\n",
1189 chg_to_string(mdwc->charger.chg_type));
1190 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1191 &mdwc->charger);
1192 return;
1193 default:
1194 return;
1195 }
1196
1197 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1198}
1199
1200static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1201{
1202 struct dwc3_msm *mdwc = context;
1203
1204 if (start == false) {
1205 cancel_delayed_work_sync(&mdwc->chg_work);
1206 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1207 charger->chg_type = DWC3_INVALID_CHARGER;
1208 return;
1209 }
1210
1211 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1212 charger->chg_type = DWC3_INVALID_CHARGER;
1213 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1214}
1215
1216
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001217static int __devinit dwc3_msm_probe(struct platform_device *pdev)
1218{
1219 struct device_node *node = pdev->dev.of_node;
1220 struct platform_device *dwc3;
1221 struct dwc3_msm *msm;
1222 struct resource *res;
1223 int ret = 0;
1224
1225 msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL);
1226 if (!msm) {
1227 dev_err(&pdev->dev, "not enough memory\n");
1228 return -ENOMEM;
1229 }
1230
1231 platform_set_drvdata(pdev, msm);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001232 context = msm;
Manu Gautam60e01352012-05-29 09:00:34 +05301233 msm->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001234
1235 INIT_LIST_HEAD(&msm->req_complete_list);
Manu Gautam8c642812012-06-07 10:35:10 +05301236 INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001237
Manu Gautam1742db22012-06-19 13:33:24 +05301238 /*
1239 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
1240 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
1241 */
1242 msm->core_clk = devm_clk_get(&pdev->dev, "core_clk");
1243 if (IS_ERR(msm->core_clk)) {
1244 dev_err(&pdev->dev, "failed to get core_clk\n");
1245 return PTR_ERR(msm->core_clk);
1246 }
1247 clk_set_rate(msm->core_clk, 125000000);
1248 clk_prepare_enable(msm->core_clk);
1249
Manu Gautam60e01352012-05-29 09:00:34 +05301250 /* SS PHY */
1251 msm->ss_vdd_type = VDDCX_CORNER;
1252 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
1253 if (IS_ERR(msm->ssusb_vddcx)) {
1254 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev,
1255 "SSUSB_VDDCX");
1256 if (IS_ERR(msm->ssusb_vddcx)) {
1257 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
Manu Gautam1742db22012-06-19 13:33:24 +05301258 ret = PTR_ERR(msm->ssusb_vddcx);
1259 goto disable_core_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05301260 }
1261 msm->ss_vdd_type = VDDCX;
1262 dev_dbg(&pdev->dev, "ss_vdd_type: VDDCX\n");
1263 }
1264
1265 ret = dwc3_ssusb_config_vddcx(1);
1266 if (ret) {
1267 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam1742db22012-06-19 13:33:24 +05301268 goto disable_core_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05301269 }
1270
1271 ret = regulator_enable(context->ssusb_vddcx);
1272 if (ret) {
1273 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
1274 goto unconfig_ss_vddcx;
1275 }
1276
1277 ret = dwc3_ssusb_ldo_init(1);
1278 if (ret) {
1279 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
1280 goto disable_ss_vddcx;
1281 }
1282
1283 ret = dwc3_ssusb_ldo_enable(1);
1284 if (ret) {
1285 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
1286 goto free_ss_ldo_init;
1287 }
1288
1289 /* HS PHY */
1290 msm->hs_vdd_type = VDDCX_CORNER;
1291 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
1292 if (IS_ERR(msm->hsusb_vddcx)) {
1293 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev,
1294 "HSUSB_VDDCX");
1295 if (IS_ERR(msm->hsusb_vddcx)) {
1296 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
1297 ret = PTR_ERR(msm->ssusb_vddcx);
1298 goto disable_ss_ldo;
1299 }
1300 msm->hs_vdd_type = VDDCX;
1301 dev_dbg(&pdev->dev, "hs_vdd_type: VDDCX\n");
1302 }
1303
1304 ret = dwc3_hsusb_config_vddcx(1);
1305 if (ret) {
1306 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1307 goto disable_ss_ldo;
1308 }
1309
1310 ret = regulator_enable(context->hsusb_vddcx);
1311 if (ret) {
1312 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
1313 goto unconfig_hs_vddcx;
1314 }
1315
1316 ret = dwc3_hsusb_ldo_init(1);
1317 if (ret) {
1318 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1319 goto disable_hs_vddcx;
1320 }
1321
1322 ret = dwc3_hsusb_ldo_enable(1);
1323 if (ret) {
1324 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1325 goto free_hs_ldo_init;
1326 }
1327
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001328 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1329 if (!res) {
1330 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301331 ret = -ENODEV;
1332 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001333 }
1334
1335 msm->base = devm_ioremap_nocache(&pdev->dev, res->start,
1336 resource_size(res));
1337 if (!msm->base) {
1338 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301339 ret = -ENODEV;
1340 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001341 }
1342
Ido Shayevitzca2691e2012-04-17 15:54:53 +03001343 dwc3 = platform_device_alloc("dwc3", -1);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001344 if (!dwc3) {
1345 dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301346 ret = -ENODEV;
1347 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001348 }
1349
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001350 dwc3->dev.parent = &pdev->dev;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +03001351 dwc3->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1352 dwc3->dev.dma_mask = &dwc3_msm_dma_mask;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001353 dwc3->dev.dma_parms = pdev->dev.dma_parms;
1354 msm->resource_size = resource_size(res);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001355 msm->dwc3 = dwc3;
1356
1357 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
1358 &msm->dbm_num_eps)) {
1359 dev_err(&pdev->dev,
1360 "unable to read platform data num of dbm eps\n");
1361 msm->dbm_num_eps = DBM_MAX_EPS;
1362 }
1363
1364 if (msm->dbm_num_eps > DBM_MAX_EPS) {
1365 dev_err(&pdev->dev,
1366 "Driver doesn't support number of DBM EPs. "
1367 "max: %d, dbm_num_eps: %d\n",
1368 DBM_MAX_EPS, msm->dbm_num_eps);
1369 ret = -ENODEV;
Manu Gautam60e01352012-05-29 09:00:34 +05301370 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001371 }
1372
1373 ret = platform_device_add_resources(dwc3, pdev->resource,
1374 pdev->num_resources);
1375 if (ret) {
1376 dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301377 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001378 }
1379
1380 ret = platform_device_add(dwc3);
1381 if (ret) {
1382 dev_err(&pdev->dev, "failed to register dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05301383 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001384 }
1385
Ido Shayevitz9fb83452012-04-01 17:45:58 +03001386 /* Reset the DBM */
1387 dwc3_msm_dbm_soft_reset();
1388
Manu Gautam8c642812012-06-07 10:35:10 +05301389 msm->otg_xceiv = usb_get_transceiver();
1390 if (msm->otg_xceiv) {
1391 msm->charger.start_detection = dwc3_start_chg_det;
1392 ret = dwc3_set_charger(msm->otg_xceiv->otg, &msm->charger);
1393 if (ret || !msm->charger.notify_detection_complete) {
1394 dev_err(&pdev->dev, "failed to register charger: %d\n",
1395 ret);
1396 goto put_xcvr;
1397 }
1398 } else {
1399 dev_err(&pdev->dev, "%s: No OTG transceiver found\n", __func__);
1400 }
1401
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001402 return 0;
1403
Manu Gautam8c642812012-06-07 10:35:10 +05301404put_xcvr:
1405 usb_put_transceiver(msm->otg_xceiv);
1406 platform_device_del(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05301407put_pdev:
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001408 platform_device_put(dwc3);
Manu Gautam60e01352012-05-29 09:00:34 +05301409disable_hs_ldo:
1410 dwc3_hsusb_ldo_enable(0);
1411free_hs_ldo_init:
1412 dwc3_hsusb_ldo_init(0);
1413disable_hs_vddcx:
1414 regulator_disable(context->hsusb_vddcx);
1415unconfig_hs_vddcx:
1416 dwc3_hsusb_config_vddcx(0);
1417disable_ss_ldo:
1418 dwc3_ssusb_ldo_enable(0);
1419free_ss_ldo_init:
1420 dwc3_ssusb_ldo_init(0);
1421disable_ss_vddcx:
1422 regulator_disable(context->ssusb_vddcx);
1423unconfig_ss_vddcx:
1424 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05301425disable_core_clk:
1426 clk_disable_unprepare(msm->core_clk);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001427
1428 return ret;
1429}
1430
1431static int __devexit dwc3_msm_remove(struct platform_device *pdev)
1432{
1433 struct dwc3_msm *msm = platform_get_drvdata(pdev);
1434
Manu Gautam8c642812012-06-07 10:35:10 +05301435 if (msm->otg_xceiv) {
1436 dwc3_start_chg_det(&msm->charger, false);
1437 usb_put_transceiver(msm->otg_xceiv);
1438 }
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001439 platform_device_unregister(msm->dwc3);
1440
Manu Gautam60e01352012-05-29 09:00:34 +05301441 dwc3_hsusb_ldo_enable(0);
1442 dwc3_hsusb_ldo_init(0);
1443 regulator_disable(msm->hsusb_vddcx);
1444 dwc3_hsusb_config_vddcx(0);
1445 dwc3_ssusb_ldo_enable(0);
1446 dwc3_ssusb_ldo_init(0);
1447 regulator_disable(msm->ssusb_vddcx);
1448 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05301449 clk_disable_unprepare(msm->core_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05301450
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02001451 return 0;
1452}
1453
1454static const struct of_device_id of_dwc3_matach[] = {
1455 {
1456 .compatible = "qcom,dwc-usb3-msm",
1457 },
1458 { },
1459};
1460MODULE_DEVICE_TABLE(of, of_dwc3_matach);
1461
1462static struct platform_driver dwc3_msm_driver = {
1463 .probe = dwc3_msm_probe,
1464 .remove = __devexit_p(dwc3_msm_remove),
1465 .driver = {
1466 .name = "msm-dwc3",
1467 .of_match_table = of_dwc3_matach,
1468 },
1469};
1470
1471MODULE_LICENSE("GPLV2");
1472MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
1473
1474static int __devinit dwc3_msm_init(void)
1475{
1476 return platform_driver_register(&dwc3_msm_driver);
1477}
1478module_init(dwc3_msm_init);
1479
1480static void __exit dwc3_msm_exit(void)
1481{
1482 platform_driver_unregister(&dwc3_msm_driver);
1483}
1484module_exit(dwc3_msm_exit);