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