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