blob: 84672d704817acf4307762f294bd248e8e7d0494 [file] [log] [blame]
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
Manu Gautamb5067272012-07-02 09:53:41 +053019#include <linux/pm_runtime.h>
Manu Gautam377821c2012-09-28 16:53:24 +053020#include <linux/ratelimit.h>
Manu Gautamb5067272012-07-02 09:53:41 +053021#include <linux/interrupt.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020022#include <linux/ioport.h>
Manu Gautam1742db22012-06-19 13:33:24 +053023#include <linux/clk.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020024#include <linux/io.h>
25#include <linux/module.h>
26#include <linux/types.h>
Ido Shayevitzef72ddd2012-03-28 18:55:55 +020027#include <linux/delay.h>
28#include <linux/of.h>
Vijayavardhan Vennapusad5cd3ee2013-03-19 11:52:59 +053029#include <linux/of_platform.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030030#include <linux/list.h>
Manu Gautamb5067272012-07-02 09:53:41 +053031#include <linux/debugfs.h>
32#include <linux/uaccess.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030033#include <linux/usb/ch9.h>
34#include <linux/usb/gadget.h>
David Keitelad4a0282013-03-19 18:04:27 -070035#include <linux/qpnp-misc.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030036#include <linux/usb/msm_hsusb.h>
Manu Gautam60e01352012-05-29 09:00:34 +053037#include <linux/regulator/consumer.h>
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +053038#include <linux/power_supply.h>
Jack Pham0fc12332012-11-19 13:14:22 -080039#include <linux/qpnp/qpnp-adc.h>
Manu Gautam60e01352012-05-29 09:00:34 +053040
41#include <mach/rpm-regulator.h>
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +053042#include <mach/rpm-regulator-smd.h>
Manu Gautam2617deb2012-08-31 17:50:06 -070043#include <mach/msm_bus.h>
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053044#include <mach/clk.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030045
Manu Gautam8c642812012-06-07 10:35:10 +053046#include "dwc3_otg.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030047#include "core.h"
48#include "gadget.h"
49
Jack Pham0fc12332012-11-19 13:14:22 -080050/* ADC threshold values */
51static int adc_low_threshold = 700;
52module_param(adc_low_threshold, int, S_IRUGO | S_IWUSR);
53MODULE_PARM_DESC(adc_low_threshold, "ADC ID Low voltage threshold");
54
55static int adc_high_threshold = 950;
56module_param(adc_high_threshold, int, S_IRUGO | S_IWUSR);
57MODULE_PARM_DESC(adc_high_threshold, "ADC ID High voltage threshold");
58
59static int adc_meas_interval = ADC_MEAS1_INTERVAL_1S;
60module_param(adc_meas_interval, int, S_IRUGO | S_IWUSR);
61MODULE_PARM_DESC(adc_meas_interval, "ADC ID polling period");
62
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +053063static int override_phy_init;
64module_param(override_phy_init, int, S_IRUGO|S_IWUSR);
65MODULE_PARM_DESC(override_phy_init, "Override HSPHY Init Seq");
66
Ido Shayevitz9fb83452012-04-01 17:45:58 +030067/**
68 * USB DBM Hardware registers.
69 *
70 */
Shimrit Malichia00d7322012-08-05 13:56:28 +030071#define DBM_BASE 0x000F8000
72#define DBM_EP_CFG(n) (DBM_BASE + (0x00 + 4 * (n)))
73#define DBM_DATA_FIFO(n) (DBM_BASE + (0x10 + 4 * (n)))
74#define DBM_DATA_FIFO_SIZE(n) (DBM_BASE + (0x20 + 4 * (n)))
75#define DBM_DATA_FIFO_EN (DBM_BASE + (0x30))
76#define DBM_GEVNTADR (DBM_BASE + (0x34))
77#define DBM_GEVNTSIZ (DBM_BASE + (0x38))
78#define DBM_DBG_CNFG (DBM_BASE + (0x3C))
79#define DBM_HW_TRB0_EP(n) (DBM_BASE + (0x40 + 4 * (n)))
80#define DBM_HW_TRB1_EP(n) (DBM_BASE + (0x50 + 4 * (n)))
81#define DBM_HW_TRB2_EP(n) (DBM_BASE + (0x60 + 4 * (n)))
82#define DBM_HW_TRB3_EP(n) (DBM_BASE + (0x70 + 4 * (n)))
83#define DBM_PIPE_CFG (DBM_BASE + (0x80))
84#define DBM_SOFT_RESET (DBM_BASE + (0x84))
85#define DBM_GEN_CFG (DBM_BASE + (0x88))
Ido Shayevitz9fb83452012-04-01 17:45:58 +030086
87/**
88 * USB DBM Hardware registers bitmask.
89 *
90 */
91/* DBM_EP_CFG */
Shimrit Malichia00d7322012-08-05 13:56:28 +030092#define DBM_EN_EP 0x00000001
93#define USB3_EPNUM 0x0000003E
Ido Shayevitz9fb83452012-04-01 17:45:58 +030094#define DBM_BAM_PIPE_NUM 0x000000C0
95#define DBM_PRODUCER 0x00000100
96#define DBM_DISABLE_WB 0x00000200
97#define DBM_INT_RAM_ACC 0x00000400
98
99/* DBM_DATA_FIFO_SIZE */
100#define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff
101
102/* DBM_GEVNTSIZ */
103#define DBM_GEVNTSIZ_MASK 0x0000ffff
104
105/* DBM_DBG_CNFG */
106#define DBM_ENABLE_IOC_MASK 0x0000000f
107
108/* DBM_SOFT_RESET */
109#define DBM_SFT_RST_EP0 0x00000001
110#define DBM_SFT_RST_EP1 0x00000002
111#define DBM_SFT_RST_EP2 0x00000004
112#define DBM_SFT_RST_EP3 0x00000008
Shimrit Malichia00d7322012-08-05 13:56:28 +0300113#define DBM_SFT_RST_EPS_MASK 0x0000000F
114#define DBM_SFT_RST_MASK 0x80000000
115#define DBM_EN_MASK 0x00000002
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200116
117#define DBM_MAX_EPS 4
118
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300119/* DBM TRB configurations */
120#define DBM_TRB_BIT 0x80000000
121#define DBM_TRB_DATA_SRC 0x40000000
122#define DBM_TRB_DMA 0x20000000
123#define DBM_TRB_EP_NUM(ep) (ep<<24)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300124
Manu Gautam8c642812012-06-07 10:35:10 +0530125/**
126 * USB QSCRATCH Hardware registers
127 *
128 */
129#define QSCRATCH_REG_OFFSET (0x000F8800)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300130#define QSCRATCH_GENERAL_CFG (QSCRATCH_REG_OFFSET + 0x08)
Manu Gautambd0e5782012-08-30 10:39:01 -0700131#define HS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x10)
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530132#define PARAMETER_OVERRIDE_X_REG (QSCRATCH_REG_OFFSET + 0x14)
Manu Gautam8c642812012-06-07 10:35:10 +0530133#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
134#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
135#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
136#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
Manu Gautamd4108b72012-12-14 17:35:18 +0530137#define CGCTL_REG (QSCRATCH_REG_OFFSET + 0x28)
Manu Gautambd0e5782012-08-30 10:39:01 -0700138#define SS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x30)
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +0530139#define SS_PHY_PARAM_CTRL_1 (QSCRATCH_REG_OFFSET + 0x34)
140#define SS_PHY_PARAM_CTRL_2 (QSCRATCH_REG_OFFSET + 0x38)
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530141#define SS_CR_PROTOCOL_DATA_IN_REG (QSCRATCH_REG_OFFSET + 0x3C)
142#define SS_CR_PROTOCOL_DATA_OUT_REG (QSCRATCH_REG_OFFSET + 0x40)
143#define SS_CR_PROTOCOL_CAP_ADDR_REG (QSCRATCH_REG_OFFSET + 0x44)
144#define SS_CR_PROTOCOL_CAP_DATA_REG (QSCRATCH_REG_OFFSET + 0x48)
145#define SS_CR_PROTOCOL_READ_REG (QSCRATCH_REG_OFFSET + 0x4C)
146#define SS_CR_PROTOCOL_WRITE_REG (QSCRATCH_REG_OFFSET + 0x50)
Manu Gautam8c642812012-06-07 10:35:10 +0530147
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300148struct dwc3_msm_req_complete {
149 struct list_head list_item;
150 struct usb_request *req;
151 void (*orig_complete)(struct usb_ep *ep,
152 struct usb_request *req);
153};
154
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200155struct dwc3_msm {
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200156 struct device *dev;
157 void __iomem *base;
158 u32 resource_size;
159 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300160 u8 ep_num_mapping[DBM_MAX_EPS];
161 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
162 struct list_head req_complete_list;
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +0530163 struct clk *xo_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700164 struct clk *ref_clk;
Manu Gautam1742db22012-06-19 13:33:24 +0530165 struct clk *core_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700166 struct clk *iface_clk;
167 struct clk *sleep_clk;
168 struct clk *hsphy_sleep_clk;
Jack Pham22698b82013-02-13 17:45:06 -0800169 struct clk *utmi_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530170 struct regulator *hsusb_3p3;
171 struct regulator *hsusb_1p8;
172 struct regulator *hsusb_vddcx;
173 struct regulator *ssusb_1p8;
174 struct regulator *ssusb_vddcx;
Manu Gautamb5067272012-07-02 09:53:41 +0530175 struct dwc3_ext_xceiv ext_xceiv;
176 bool resume_pending;
177 atomic_t pm_suspended;
178 atomic_t in_lpm;
Manu Gautam377821c2012-09-28 16:53:24 +0530179 int hs_phy_irq;
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530180 int hsphy_init_seq;
Manu Gautam377821c2012-09-28 16:53:24 +0530181 bool lpm_irq_seen;
Manu Gautamb5067272012-07-02 09:53:41 +0530182 struct delayed_work resume_work;
Manu Gautam6eb13e32013-02-01 15:19:15 +0530183 struct work_struct restart_usb_work;
Manu Gautamb5067272012-07-02 09:53:41 +0530184 struct wake_lock wlock;
Manu Gautam8c642812012-06-07 10:35:10 +0530185 struct dwc3_charger charger;
186 struct usb_phy *otg_xceiv;
187 struct delayed_work chg_work;
188 enum usb_chg_state chg_state;
Jack Pham0cca9412013-03-08 13:22:42 -0800189 int pmic_id_irq;
190 struct work_struct id_work;
Jack Pham0fc12332012-11-19 13:14:22 -0800191 struct qpnp_adc_tm_usbid_param adc_param;
192 struct delayed_work init_adc_work;
193 bool id_adc_detect;
Manu Gautam8c642812012-06-07 10:35:10 +0530194 u8 dcd_retries;
Manu Gautam2617deb2012-08-31 17:50:06 -0700195 u32 bus_perf_client;
196 struct msm_bus_scale_pdata *bus_scale_table;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530197 struct power_supply usb_psy;
Jack Pham9354c6a2012-12-20 19:19:32 -0800198 struct power_supply *ext_vbus_psy;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530199 unsigned int online;
200 unsigned int host_mode;
201 unsigned int current_max;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +0530202 unsigned int vdd_no_vol_level;
203 unsigned int vdd_low_vol_level;
204 unsigned int vdd_high_vol_level;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530205 bool vbus_active;
Jack Phamfadd6432012-12-07 19:03:41 -0800206 bool ext_inuse;
Jack Phamf12b7e12012-12-28 14:27:26 -0800207 enum dwc3_id_state id_state;
Manu Gautam60e01352012-05-29 09:00:34 +0530208};
209
210#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
211#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
212#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
213
214#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
215#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
216#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
217
218#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
219#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
220#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
221
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300222static struct dwc3_msm *context;
223
Jack Phamfadd6432012-12-07 19:03:41 -0800224static struct usb_ext_notification *usb_ext;
225
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300226/**
227 *
228 * Read register with debug info.
229 *
230 * @base - DWC3 base virtual address.
231 * @offset - register offset.
232 *
233 * @return u32
234 */
235static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
236{
237 u32 val = ioread32(base + offset);
238 return val;
239}
240
241/**
242 * Read register masked field with debug info.
243 *
244 * @base - DWC3 base virtual address.
245 * @offset - register offset.
246 * @mask - register bitmask.
247 *
248 * @return u32
249 */
250static inline u32 dwc3_msm_read_reg_field(void *base,
251 u32 offset,
252 const u32 mask)
253{
254 u32 shift = find_first_bit((void *)&mask, 32);
255 u32 val = ioread32(base + offset);
256 val &= mask; /* clear other bits */
257 val >>= shift;
258 return val;
259}
260
261/**
262 *
263 * Write register with debug info.
264 *
265 * @base - DWC3 base virtual address.
266 * @offset - register offset.
267 * @val - value to write.
268 *
269 */
270static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
271{
272 iowrite32(val, base + offset);
273}
274
275/**
276 * Write register masked field with debug info.
277 *
278 * @base - DWC3 base virtual address.
279 * @offset - register offset.
280 * @mask - register bitmask.
281 * @val - value to write.
282 *
283 */
284static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
285 const u32 mask, u32 val)
286{
287 u32 shift = find_first_bit((void *)&mask, 32);
288 u32 tmp = ioread32(base + offset);
289
290 tmp &= ~mask; /* clear written bits */
291 val = tmp | (val << shift);
292 iowrite32(val, base + offset);
293}
294
295/**
Manu Gautam8c642812012-06-07 10:35:10 +0530296 * Write register and read back masked value to confirm it is written
297 *
298 * @base - DWC3 base virtual address.
299 * @offset - register offset.
300 * @mask - register bitmask specifying what should be updated
301 * @val - value to write.
302 *
303 */
304static inline void dwc3_msm_write_readback(void *base, u32 offset,
305 const u32 mask, u32 val)
306{
307 u32 write_val, tmp = ioread32(base + offset);
308
309 tmp &= ~mask; /* retain other bits */
310 write_val = tmp | val;
311
312 iowrite32(write_val, base + offset);
313
314 /* Read back to see if val was written */
315 tmp = ioread32(base + offset);
316 tmp &= mask; /* clear other bits */
317
318 if (tmp != val)
319 dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n",
320 __func__, val, offset);
321}
322
323/**
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530324 *
325 * Write SSPHY register with debug info.
326 *
327 * @base - DWC3 base virtual address.
328 * @addr - SSPHY address to write.
329 * @val - value to write.
330 *
331 */
332static void dwc3_msm_ssusb_write_phycreg(void *base, u32 addr, u32 val)
333{
334 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
335 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
336 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
337 cpu_relax();
338
339 iowrite32(val, base + SS_CR_PROTOCOL_DATA_IN_REG);
340 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_DATA_REG);
341 while (ioread32(base + SS_CR_PROTOCOL_CAP_DATA_REG))
342 cpu_relax();
343
344 iowrite32(0x1, base + SS_CR_PROTOCOL_WRITE_REG);
345 while (ioread32(base + SS_CR_PROTOCOL_WRITE_REG))
346 cpu_relax();
347}
348
349/**
350 *
351 * Read SSPHY register with debug info.
352 *
353 * @base - DWC3 base virtual address.
354 * @addr - SSPHY address to read.
355 *
356 */
357static u32 dwc3_msm_ssusb_read_phycreg(void *base, u32 addr)
358{
359 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
360 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
361 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
362 cpu_relax();
363
364 iowrite32(0x1, base + SS_CR_PROTOCOL_READ_REG);
365 while (ioread32(base + SS_CR_PROTOCOL_READ_REG))
366 cpu_relax();
367
368 return ioread32(base + SS_CR_PROTOCOL_DATA_OUT_REG);
369}
370
371/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300372 * Return DBM EP number according to usb endpoint number.
373 *
374 */
375static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep)
376{
377 int i;
378
379 for (i = 0; i < context->dbm_num_eps; i++)
380 if (context->ep_num_mapping[i] == usb_ep)
381 return i;
382
383 return -ENODEV; /* Not found */
384}
385
386/**
387 * Return number of configured DBM endpoints.
388 *
389 */
390static int dwc3_msm_configured_dbm_ep_num(void)
391{
392 int i;
393 int count = 0;
394
395 for (i = 0; i < context->dbm_num_eps; i++)
396 if (context->ep_num_mapping[i])
397 count++;
398
399 return count;
400}
401
402/**
403 * Configure the DBM with the USB3 core event buffer.
404 * This function is called by the SNPS UDC upon initialization.
405 *
406 * @addr - address of the event buffer.
407 * @size - size of the event buffer.
408 *
409 */
410static int dwc3_msm_event_buffer_config(u32 addr, u16 size)
411{
412 dev_dbg(context->dev, "%s\n", __func__);
413
414 dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr);
415 dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ,
416 DBM_GEVNTSIZ_MASK, size);
417
418 return 0;
419}
420
421/**
422 * Reset the DBM registers upon initialization.
423 *
424 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300425static int dwc3_msm_dbm_soft_reset(int enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300426{
427 dev_dbg(context->dev, "%s\n", __func__);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300428 if (enter_reset) {
429 dev_dbg(context->dev, "enter DBM reset\n");
430 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
431 DBM_SFT_RST_MASK, 1);
432 } else {
433 dev_dbg(context->dev, "exit DBM reset\n");
434 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
435 DBM_SFT_RST_MASK, 0);
436 /*enable DBM*/
437 dwc3_msm_write_reg_field(context->base, QSCRATCH_GENERAL_CFG,
438 DBM_EN_MASK, 0x1);
439 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300440
441 return 0;
442}
443
444/**
445 * Soft reset specific DBM ep.
446 * This function is called by the function driver upon events
447 * such as transfer aborting, USB re-enumeration and USB
448 * disconnection.
449 *
450 * @dbm_ep - DBM ep number.
451 * @enter_reset - should we enter a reset state or get out of it.
452 *
453 */
454static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset)
455{
456 dev_dbg(context->dev, "%s\n", __func__);
457
458 if (dbm_ep >= context->dbm_num_eps) {
459 dev_err(context->dev,
460 "%s: Invalid DBM ep index\n", __func__);
461 return -ENODEV;
462 }
463
464 if (enter_reset) {
465 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300466 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300467 } else {
468 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300469 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300470 }
471
472 return 0;
473}
474
475/**
476 * Configure a USB DBM ep to work in BAM mode.
477 *
478 *
479 * @usb_ep - USB physical EP number.
480 * @producer - producer/consumer.
481 * @disable_wb - disable write back to system memory.
482 * @internal_mem - use internal USB memory for data fifo.
483 * @ioc - enable interrupt on completion.
484 *
485 * @return int - DBM ep number.
486 */
487static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe,
488 bool producer, bool disable_wb,
489 bool internal_mem, bool ioc)
490{
491 u8 dbm_ep;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300492 u32 ep_cfg;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300493
494 dev_dbg(context->dev, "%s\n", __func__);
495
Shimrit Malichia00d7322012-08-05 13:56:28 +0300496 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
497
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300498 if (dbm_ep < 0) {
Shimrit Malichia00d7322012-08-05 13:56:28 +0300499 dev_err(context->dev,
500 "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300501 return -ENODEV;
502 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300503 /* First, reset the dbm endpoint */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300504 dwc3_msm_dbm_ep_soft_reset(dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300505
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300506 /* Set ioc bit for dbm_ep if needed */
507 dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300508 DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300509
Shimrit Malichia00d7322012-08-05 13:56:28 +0300510 ep_cfg = (producer ? DBM_PRODUCER : 0) |
511 (disable_wb ? DBM_DISABLE_WB : 0) |
512 (internal_mem ? DBM_INT_RAM_ACC : 0);
513
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300514 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
Shimrit Malichia00d7322012-08-05 13:56:28 +0300515 DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8);
516
517 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM,
518 usb_ep);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300519 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
520 DBM_BAM_PIPE_NUM, bam_pipe);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300521 dwc3_msm_write_reg_field(context->base, DBM_PIPE_CFG, 0x000000ff,
522 0xe4);
523 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP,
524 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300525
526 return dbm_ep;
527}
528
529/**
530 * Configure a USB DBM ep to work in normal mode.
531 *
532 * @usb_ep - USB ep number.
533 *
534 */
535static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep)
536{
537 u8 dbm_ep;
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530538 u32 data;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300539
540 dev_dbg(context->dev, "%s\n", __func__);
541
542 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
543
544 if (dbm_ep < 0) {
545 dev_err(context->dev,
546 "%s: Invalid usb ep index\n", __func__);
547 return -ENODEV;
548 }
549
550 context->ep_num_mapping[dbm_ep] = 0;
551
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530552 data = dwc3_msm_read_reg(context->base, DBM_EP_CFG(dbm_ep));
553 data &= (~0x1);
554 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), data);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300555
556 /* Reset the dbm endpoint */
557 dwc3_msm_dbm_ep_soft_reset(dbm_ep, true);
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530558 /*
559 * 10 usec delay is required before deasserting DBM endpoint reset
560 * according to hardware programming guide.
561 */
562 udelay(10);
563 dwc3_msm_dbm_ep_soft_reset(dbm_ep, false);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300564
565 return 0;
566}
567
568/**
569 * Configure the DBM with the BAM's data fifo.
570 * This function is called by the USB BAM Driver
571 * upon initialization.
572 *
573 * @ep - pointer to usb endpoint.
574 * @addr - address of data fifo.
575 * @size - size of data fifo.
576 *
577 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300578int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300579{
580 u8 dbm_ep;
581 struct dwc3_ep *dep = to_dwc3_ep(ep);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300582 u8 bam_pipe = dst_pipe_idx;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300583
584 dev_dbg(context->dev, "%s\n", __func__);
585
Shimrit Malichia00d7322012-08-05 13:56:28 +0300586 dbm_ep = bam_pipe;
587 context->ep_num_mapping[dbm_ep] = dep->number;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300588
589 dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr);
590 dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep),
591 DBM_DATA_FIFO_SIZE_MASK, size);
592
593 return 0;
594}
595
596/**
597* Cleanups for msm endpoint on request complete.
598*
599* Also call original request complete.
600*
601* @usb_ep - pointer to usb_ep instance.
602* @request - pointer to usb_request instance.
603*
604* @return int - 0 on success, negetive on error.
605*/
606static void dwc3_msm_req_complete_func(struct usb_ep *ep,
607 struct usb_request *request)
608{
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300609 struct dwc3_ep *dep = to_dwc3_ep(ep);
610 struct dwc3_msm_req_complete *req_complete = NULL;
611
612 /* Find original request complete function and remove it from list */
613 list_for_each_entry(req_complete,
614 &context->req_complete_list,
615 list_item) {
616 if (req_complete->req == request)
617 break;
618 }
619 if (!req_complete || req_complete->req != request) {
620 dev_err(dep->dwc->dev, "%s: could not find the request\n",
621 __func__);
622 return;
623 }
624 list_del(&req_complete->list_item);
625
626 /*
627 * Release another one TRB to the pool since DBM queue took 2 TRBs
628 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
629 * released only one.
630 */
Manu Gautam55d34222012-12-19 16:49:47 +0530631 dep->busy_slot++;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300632
633 /* Unconfigure dbm ep */
634 dwc3_msm_dbm_ep_unconfig(dep->number);
635
636 /*
637 * If this is the last endpoint we unconfigured, than reset also
638 * the event buffers.
639 */
640 if (0 == dwc3_msm_configured_dbm_ep_num())
641 dwc3_msm_event_buffer_config(0, 0);
642
643 /*
644 * Call original complete function, notice that dwc->lock is already
645 * taken by the caller of this function (dwc3_gadget_giveback()).
646 */
647 request->complete = req_complete->orig_complete;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300648 if (request->complete)
649 request->complete(ep, request);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300650
651 kfree(req_complete);
652}
653
654/**
655* Helper function.
656* See the header of the dwc3_msm_ep_queue function.
657*
658* @dwc3_ep - pointer to dwc3_ep instance.
659* @req - pointer to dwc3_request instance.
660*
661* @return int - 0 on success, negetive on error.
662*/
663static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
664{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300665 struct dwc3_trb *trb;
666 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300667 struct dwc3_gadget_ep_cmd_params params;
668 u32 cmd;
669 int ret = 0;
670
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300671 /* We push the request to the dep->req_queued list to indicate that
672 * this request is issued with start transfer. The request will be out
673 * from this list in 2 cases. The first is that the transfer will be
674 * completed (not if the transfer is endless using a circular TRBs with
675 * with link TRB). The second case is an option to do stop stransfer,
676 * this can be initiated by the function driver when calling dequeue.
677 */
678 req->queued = true;
679 list_add_tail(&req->list, &dep->req_queued);
680
681 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300682 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300683 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300684 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300685
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300686 req->trb = trb;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300687 trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300688 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
689 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300690 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300691
692 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300693 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300694 dep->free_slot++;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300695 memset(trb_link, 0, sizeof *trb_link);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300696
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300697 trb_link->bpl = lower_32_bits(req->trb_dma);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300698 trb_link->bph = DBM_TRB_BIT |
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300699 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
700 trb_link->size = 0;
701 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300702
703 /*
704 * Now start the transfer
705 */
706 memset(&params, 0, sizeof(params));
Shimrit Malichia00d7322012-08-05 13:56:28 +0300707 params.param0 = 0; /* TDAddr High */
708 params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */
709
Manu Gautam5b2bf9a2012-10-18 10:52:50 +0530710 /* DBM requires IOC to be set */
711 cmd = DWC3_DEPCMD_STARTTRANSFER | DWC3_DEPCMD_CMDIOC;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300712 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
713 if (ret < 0) {
714 dev_dbg(dep->dwc->dev,
715 "%s: failed to send STARTTRANSFER command\n",
716 __func__);
717
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300718 list_del(&req->list);
719 return ret;
720 }
Manu Gautam4a51a062012-12-07 11:24:39 +0530721 dep->flags |= DWC3_EP_BUSY;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300722
723 return ret;
724}
725
726/**
727* Queue a usb request to the DBM endpoint.
728* This function should be called after the endpoint
729* was enabled by the ep_enable.
730*
731* This function prepares special structure of TRBs which
732* is familier with the DBM HW, so it will possible to use
733* this endpoint in DBM mode.
734*
735* The TRBs prepared by this function, is one normal TRB
736* which point to a fake buffer, followed by a link TRB
737* that points to the first TRB.
738*
739* The API of this function follow the regular API of
740* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
741*
742* @usb_ep - pointer to usb_ep instance.
743* @request - pointer to usb_request instance.
744* @gfp_flags - possible flags.
745*
746* @return int - 0 on success, negetive on error.
747*/
748static int dwc3_msm_ep_queue(struct usb_ep *ep,
749 struct usb_request *request, gfp_t gfp_flags)
750{
751 struct dwc3_request *req = to_dwc3_request(request);
752 struct dwc3_ep *dep = to_dwc3_ep(ep);
753 struct dwc3 *dwc = dep->dwc;
754 struct dwc3_msm_req_complete *req_complete;
755 unsigned long flags;
756 int ret = 0;
757 u8 bam_pipe;
758 bool producer;
759 bool disable_wb;
760 bool internal_mem;
761 bool ioc;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300762 u8 speed;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300763
764 if (!(request->udc_priv & MSM_SPS_MODE)) {
765 /* Not SPS mode, call original queue */
766 dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n",
767 __func__);
768
769 return (context->original_ep_ops[dep->number])->queue(ep,
770 request,
771 gfp_flags);
772 }
773
774 if (!dep->endpoint.desc) {
775 dev_err(dwc->dev,
776 "%s: trying to queue request %p to disabled ep %s\n",
777 __func__, request, ep->name);
778 return -EPERM;
779 }
780
781 if (dep->number == 0 || dep->number == 1) {
782 dev_err(dwc->dev,
783 "%s: trying to queue dbm request %p to control ep %s\n",
784 __func__, request, ep->name);
785 return -EPERM;
786 }
787
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300788
Manu Gautam4a51a062012-12-07 11:24:39 +0530789 if (dep->busy_slot != dep->free_slot || !list_empty(&dep->request_list)
790 || !list_empty(&dep->req_queued)) {
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300791 dev_err(dwc->dev,
792 "%s: trying to queue dbm request %p tp ep %s\n",
793 __func__, request, ep->name);
794 return -EPERM;
Manu Gautam4a51a062012-12-07 11:24:39 +0530795 } else {
796 dep->busy_slot = 0;
797 dep->free_slot = 0;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300798 }
799
800 /*
801 * Override req->complete function, but before doing that,
802 * store it's original pointer in the req_complete_list.
803 */
804 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
805 if (!req_complete) {
806 dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__);
807 return -ENOMEM;
808 }
809 req_complete->req = request;
810 req_complete->orig_complete = request->complete;
811 list_add_tail(&req_complete->list_item, &context->req_complete_list);
812 request->complete = dwc3_msm_req_complete_func;
813
814 /*
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300815 * Configure the DBM endpoint
816 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300817 bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300818 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
819 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
820 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
821 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
822
823 ret = dwc3_msm_dbm_ep_config(dep->number,
824 bam_pipe, producer,
825 disable_wb, internal_mem, ioc);
826 if (ret < 0) {
827 dev_err(context->dev,
828 "error %d after calling dwc3_msm_dbm_ep_config\n",
829 ret);
830 return ret;
831 }
832
833 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
834 __func__, request, ep->name, request->length);
835
836 /*
837 * We must obtain the lock of the dwc3 core driver,
838 * including disabling interrupts, so we will be sure
839 * that we are the only ones that configure the HW device
840 * core and ensure that we queuing the request will finish
841 * as soon as possible so we will release back the lock.
842 */
843 spin_lock_irqsave(&dwc->lock, flags);
844 ret = __dwc3_msm_ep_queue(dep, req);
845 spin_unlock_irqrestore(&dwc->lock, flags);
846 if (ret < 0) {
847 dev_err(context->dev,
848 "error %d after calling __dwc3_msm_ep_queue\n", ret);
849 return ret;
850 }
851
Shimrit Malichia00d7322012-08-05 13:56:28 +0300852 speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD;
853 dwc3_msm_write_reg(context->base, DBM_GEN_CFG, speed >> 2);
854
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300855 return 0;
856}
857
858/**
859 * Configure MSM endpoint.
860 * This function do specific configurations
861 * to an endpoint which need specific implementaion
862 * in the MSM architecture.
863 *
864 * This function should be called by usb function/class
865 * layer which need a support from the specific MSM HW
866 * which wrap the USB3 core. (like DBM specific endpoints)
867 *
868 * @ep - a pointer to some usb_ep instance
869 *
870 * @return int - 0 on success, negetive on error.
871 */
872int msm_ep_config(struct usb_ep *ep)
873{
874 struct dwc3_ep *dep = to_dwc3_ep(ep);
875 struct usb_ep_ops *new_ep_ops;
876
Manu Gautama302f612012-12-18 17:33:06 +0530877 dwc3_msm_event_buffer_config(dwc3_msm_read_reg(context->base,
878 DWC3_GEVNTADRLO(0)),
879 dwc3_msm_read_reg(context->base, DWC3_GEVNTSIZ(0)));
880
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300881 /* Save original ep ops for future restore*/
882 if (context->original_ep_ops[dep->number]) {
883 dev_err(context->dev,
884 "ep [%s,%d] already configured as msm endpoint\n",
885 ep->name, dep->number);
886 return -EPERM;
887 }
888 context->original_ep_ops[dep->number] = ep->ops;
889
890 /* Set new usb ops as we like */
891 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
892 if (!new_ep_ops) {
893 dev_err(context->dev,
894 "%s: unable to allocate mem for new usb ep ops\n",
895 __func__);
896 return -ENOMEM;
897 }
898 (*new_ep_ops) = (*ep->ops);
899 new_ep_ops->queue = dwc3_msm_ep_queue;
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530900 new_ep_ops->disable = ep->ops->disable;
901
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300902 ep->ops = new_ep_ops;
903
904 /*
905 * Do HERE more usb endpoint configurations
906 * which are specific to MSM.
907 */
908
909 return 0;
910}
911EXPORT_SYMBOL(msm_ep_config);
912
913/**
914 * Un-configure MSM endpoint.
915 * Tear down configurations done in the
916 * dwc3_msm_ep_config function.
917 *
918 * @ep - a pointer to some usb_ep instance
919 *
920 * @return int - 0 on success, negetive on error.
921 */
922int msm_ep_unconfig(struct usb_ep *ep)
923{
924 struct dwc3_ep *dep = to_dwc3_ep(ep);
925 struct usb_ep_ops *old_ep_ops;
926
927 /* Restore original ep ops */
928 if (!context->original_ep_ops[dep->number]) {
929 dev_err(context->dev,
930 "ep [%s,%d] was not configured as msm endpoint\n",
931 ep->name, dep->number);
932 return -EINVAL;
933 }
934 old_ep_ops = (struct usb_ep_ops *)ep->ops;
935 ep->ops = context->original_ep_ops[dep->number];
936 context->original_ep_ops[dep->number] = NULL;
937 kfree(old_ep_ops);
938
939 /*
940 * Do HERE more usb endpoint un-configurations
941 * which are specific to MSM.
942 */
943
944 return 0;
945}
946EXPORT_SYMBOL(msm_ep_unconfig);
947
Manu Gautam6eb13e32013-02-01 15:19:15 +0530948static void dwc3_restart_usb_work(struct work_struct *w)
949{
950 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
951 restart_usb_work);
952
953 dev_dbg(mdwc->dev, "%s\n", __func__);
954
955 if (atomic_read(&mdwc->in_lpm) || !mdwc->otg_xceiv) {
956 dev_err(mdwc->dev, "%s failed!!!\n", __func__);
957 return;
958 }
959
960 if (!mdwc->ext_xceiv.bsv) {
961 dev_dbg(mdwc->dev, "%s bailing out in disconnect\n", __func__);
962 return;
963 }
964
965 /* Reset active USB connection */
966 mdwc->ext_xceiv.bsv = false;
967 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
968 /* Make sure disconnect is processed before sending connect */
969 flush_delayed_work(&mdwc->resume_work);
970
971 mdwc->ext_xceiv.bsv = true;
972 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
973}
974
975/**
976 * Reset USB peripheral connection
977 * Inform OTG for Vbus LOW followed by Vbus HIGH notification.
978 * This performs full hardware reset and re-initialization which
979 * might be required by some DBM client driver during uninit/cleanup.
980 */
981void msm_dwc3_restart_usb_session(void)
982{
983 struct dwc3_msm *mdwc = context;
984
985 dev_dbg(mdwc->dev, "%s\n", __func__);
986 queue_work(system_nrt_wq, &mdwc->restart_usb_work);
987
988 return;
989}
990EXPORT_SYMBOL(msm_dwc3_restart_usb_session);
991
Jack Phamfadd6432012-12-07 19:03:41 -0800992/**
993 * msm_register_usb_ext_notification: register for event notification
994 * @info: pointer to client usb_ext_notification structure. May be NULL.
995 *
996 * @return int - 0 on success, negative on error
997 */
998int msm_register_usb_ext_notification(struct usb_ext_notification *info)
999{
1000 pr_debug("%s usb_ext: %p\n", __func__, info);
1001
1002 if (info) {
1003 if (usb_ext) {
1004 pr_err("%s: already registered\n", __func__);
1005 return -EEXIST;
1006 }
1007
1008 if (!info->notify) {
1009 pr_err("%s: notify is NULL\n", __func__);
1010 return -EINVAL;
1011 }
1012 }
1013
1014 usb_ext = info;
1015 return 0;
1016}
1017EXPORT_SYMBOL(msm_register_usb_ext_notification);
1018
Manu Gautam60e01352012-05-29 09:00:34 +05301019/* HSPHY */
1020static int dwc3_hsusb_config_vddcx(int high)
1021{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301022 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301023 struct dwc3_msm *dwc = context;
Manu Gautam60e01352012-05-29 09:00:34 +05301024
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301025 max_vol = dwc->vdd_high_vol_level;
1026 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301027 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
1028 if (ret) {
1029 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
1030 return ret;
1031 }
1032
1033 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1034 min_vol, max_vol);
1035
1036 return ret;
1037}
1038
1039static int dwc3_hsusb_ldo_init(int init)
1040{
1041 int rc = 0;
1042 struct dwc3_msm *dwc = context;
1043
1044 if (!init) {
1045 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
1046 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1047 return 0;
1048 }
1049
1050 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
1051 if (IS_ERR(dwc->hsusb_3p3)) {
1052 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
1053 return PTR_ERR(dwc->hsusb_3p3);
1054 }
1055
1056 rc = regulator_set_voltage(dwc->hsusb_3p3,
1057 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
1058 if (rc) {
1059 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
1060 return rc;
1061 }
1062 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
1063 if (IS_ERR(dwc->hsusb_1p8)) {
1064 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
1065 rc = PTR_ERR(dwc->hsusb_1p8);
1066 goto devote_3p3;
1067 }
1068 rc = regulator_set_voltage(dwc->hsusb_1p8,
1069 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
1070 if (rc) {
1071 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
1072 goto devote_3p3;
1073 }
1074
1075 return 0;
1076
1077devote_3p3:
1078 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1079
1080 return rc;
1081}
1082
1083static int dwc3_hsusb_ldo_enable(int on)
1084{
1085 int rc = 0;
1086 struct dwc3_msm *dwc = context;
1087
1088 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1089
1090 if (!on)
1091 goto disable_regulators;
1092
1093
1094 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
1095 if (rc < 0) {
1096 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
1097 return rc;
1098 }
1099
1100 rc = regulator_enable(dwc->hsusb_1p8);
1101 if (rc) {
1102 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
1103 goto put_1p8_lpm;
1104 }
1105
1106 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
1107 if (rc < 0) {
1108 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
1109 goto disable_1p8;
1110 }
1111
1112 rc = regulator_enable(dwc->hsusb_3p3);
1113 if (rc) {
1114 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
1115 goto put_3p3_lpm;
1116 }
1117
1118 return 0;
1119
1120disable_regulators:
1121 rc = regulator_disable(dwc->hsusb_3p3);
1122 if (rc)
1123 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
1124
1125put_3p3_lpm:
1126 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
1127 if (rc < 0)
1128 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
1129
1130disable_1p8:
1131 rc = regulator_disable(dwc->hsusb_1p8);
1132 if (rc)
1133 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
1134
1135put_1p8_lpm:
1136 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
1137 if (rc < 0)
1138 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
1139
1140 return rc < 0 ? rc : 0;
1141}
1142
1143/* SSPHY */
1144static int dwc3_ssusb_config_vddcx(int high)
1145{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301146 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301147 struct dwc3_msm *dwc = context;
Manu Gautam60e01352012-05-29 09:00:34 +05301148
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301149 max_vol = dwc->vdd_high_vol_level;
1150 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301151 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
1152 if (ret) {
1153 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
1154 return ret;
1155 }
1156
1157 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1158 min_vol, max_vol);
1159 return ret;
1160}
1161
1162/* 3.3v supply not needed for SS PHY */
1163static int dwc3_ssusb_ldo_init(int init)
1164{
1165 int rc = 0;
1166 struct dwc3_msm *dwc = context;
1167
1168 if (!init) {
1169 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
1170 return 0;
1171 }
1172
1173 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
1174 if (IS_ERR(dwc->ssusb_1p8)) {
1175 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
1176 return PTR_ERR(dwc->ssusb_1p8);
1177 }
1178 rc = regulator_set_voltage(dwc->ssusb_1p8,
1179 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
1180 if (rc)
1181 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
1182
1183 return rc;
1184}
1185
1186static int dwc3_ssusb_ldo_enable(int on)
1187{
1188 int rc = 0;
1189 struct dwc3_msm *dwc = context;
1190
1191 dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1192
1193 if (!on)
1194 goto disable_regulators;
1195
1196
1197 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1198 if (rc < 0) {
1199 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1200 return rc;
1201 }
1202
1203 rc = regulator_enable(dwc->ssusb_1p8);
1204 if (rc) {
1205 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1206 goto put_1p8_lpm;
1207 }
1208
1209 return 0;
1210
1211disable_regulators:
1212 rc = regulator_disable(dwc->ssusb_1p8);
1213 if (rc)
1214 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1215
1216put_1p8_lpm:
1217 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1218 if (rc < 0)
1219 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1220
1221 return rc < 0 ? rc : 0;
1222}
1223
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301224static int dwc3_msm_link_clk_reset(bool assert)
1225{
1226 int ret = 0;
1227 struct dwc3_msm *mdwc = context;
1228
1229 if (assert) {
1230 /* Using asynchronous block reset to the hardware */
1231 dev_dbg(mdwc->dev, "block_reset ASSERT\n");
1232 clk_disable_unprepare(mdwc->ref_clk);
1233 clk_disable_unprepare(mdwc->iface_clk);
1234 clk_disable_unprepare(mdwc->core_clk);
1235 ret = clk_reset(mdwc->core_clk, CLK_RESET_ASSERT);
1236 if (ret)
1237 dev_err(mdwc->dev, "dwc3 core_clk assert failed\n");
1238 } else {
1239 dev_dbg(mdwc->dev, "block_reset DEASSERT\n");
1240 ret = clk_reset(mdwc->core_clk, CLK_RESET_DEASSERT);
1241 ndelay(200);
1242 clk_prepare_enable(mdwc->core_clk);
1243 clk_prepare_enable(mdwc->ref_clk);
1244 clk_prepare_enable(mdwc->iface_clk);
1245 if (ret)
1246 dev_err(mdwc->dev, "dwc3 core_clk deassert failed\n");
1247 }
1248
1249 return ret;
1250}
1251
1252/* Initialize QSCRATCH registers for HSPHY and SSPHY operation */
1253static void dwc3_msm_qscratch_reg_init(struct dwc3_msm *msm)
1254{
1255 u32 data = 0;
1256
1257 /* SSPHY Initialization: Use ref_clk from pads and set its parameters */
1258 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1259 msleep(30);
1260 /* Assert SSPHY reset */
1261 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210082);
1262 usleep_range(2000, 2200);
1263 /* De-assert SSPHY reset - power and ref_clock must be ON */
1264 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1265 usleep_range(2000, 2200);
1266 /* Ref clock must be stable now, enable ref clock for HS mode */
1267 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210102);
1268 usleep_range(2000, 2200);
1269 /*
1270 * HSPHY Initialization: Enable UTMI clock and clamp enable HVINTs,
1271 * and disable RETENTION (power-on default is ENABLED)
1272 */
1273 dwc3_msm_write_reg(msm->base, HS_PHY_CTRL_REG, 0x5220bb2);
1274 usleep_range(2000, 2200);
1275 /* Disable (bypass) VBUS and ID filters */
1276 dwc3_msm_write_reg(msm->base, QSCRATCH_GENERAL_CFG, 0x78);
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05301277 /*
1278 * write HSPHY init value to QSCRATCH reg to set HSPHY parameters like
1279 * VBUS valid threshold, disconnect valid threshold, DC voltage level,
1280 * preempasis and rise/fall time.
1281 */
1282 if (override_phy_init)
1283 msm->hsphy_init_seq = override_phy_init;
1284 if (msm->hsphy_init_seq)
1285 dwc3_msm_write_readback(msm->base,
1286 PARAMETER_OVERRIDE_X_REG, 0x03FFFFFF,
1287 msm->hsphy_init_seq & 0x03FFFFFF);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301288
Manu Gautamd4108b72012-12-14 17:35:18 +05301289 /* Enable master clock for RAMs to allow BAM to access RAMs when
1290 * RAM clock gating is enabled via DWC3's GCTL. Otherwise, issues
1291 * are seen where RAM clocks get turned OFF in SS mode
1292 */
1293 dwc3_msm_write_reg(msm->base, CGCTL_REG,
1294 dwc3_msm_read_reg(msm->base, CGCTL_REG) | 0x18);
1295
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301296 /*
1297 * WORKAROUND: There is SSPHY suspend bug due to which USB enumerates
1298 * in HS mode instead of SS mode. Workaround it by asserting
1299 * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus mode
1300 */
1301 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x102D);
1302 data |= (1 << 7);
1303 dwc3_msm_ssusb_write_phycreg(msm->base, 0x102D, data);
1304
1305 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1010);
1306 data &= ~0xFF0;
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301307 data |= 0x20;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301308 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1010, data);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301309
1310 /*
1311 * Fix RX Equalization setting as follows
1312 * LANE0.RX_OVRD_IN_HI. RX_EQ_EN set to 0
1313 * LANE0.RX_OVRD_IN_HI.RX_EQ_EN_OVRD set to 1
1314 * LANE0.RX_OVRD_IN_HI.RX_EQ set to 3
1315 * LANE0.RX_OVRD_IN_HI.RX_EQ_OVRD set to 1
1316 */
1317 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1006);
1318 data &= ~(1 << 6);
1319 data |= (1 << 7);
1320 data &= ~(0x7 << 8);
1321 data |= (0x3 << 8);
1322 data |= (0x1 << 11);
1323 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1006, data);
1324
1325 /*
1326 * Set EQ and TX launch amplitudes as follows
1327 * LANE0.TX_OVRD_DRV_LO.PREEMPH set to 22
1328 * LANE0.TX_OVRD_DRV_LO.AMPLITUDE set to 127
1329 * LANE0.TX_OVRD_DRV_LO.EN set to 1.
1330 */
1331 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1002);
1332 data &= ~0x3F80;
1333 data |= (0x16 << 7);
1334 data &= ~0x7F;
1335 data |= (0x7F | (1 << 14));
1336 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1002, data);
1337
1338 /* Set LOS_BIAS to 0x5 */
1339 dwc3_msm_write_readback(msm->base, SS_PHY_PARAM_CTRL_1, 0x07, 0x5);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301340}
1341
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301342static void dwc3_msm_block_reset(bool core_reset)
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301343{
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301344
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301345 struct dwc3_msm *mdwc = context;
1346 int ret = 0;
1347
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301348 if (core_reset) {
1349 ret = dwc3_msm_link_clk_reset(1);
1350 if (ret)
1351 return;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301352
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301353 usleep_range(1000, 1200);
1354 ret = dwc3_msm_link_clk_reset(0);
1355 if (ret)
1356 return;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301357
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301358 usleep_range(10000, 12000);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301359
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +05301360 /* Reinitialize QSCRATCH registers after block reset */
1361 dwc3_msm_qscratch_reg_init(mdwc);
1362 }
Manu Gautama302f612012-12-18 17:33:06 +05301363
1364 /* Reset the DBM */
1365 dwc3_msm_dbm_soft_reset(1);
1366 usleep_range(1000, 1200);
1367 dwc3_msm_dbm_soft_reset(0);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301368}
1369
Manu Gautam8c642812012-06-07 10:35:10 +05301370static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1371{
1372 u32 chg_ctrl;
1373
1374 /* Turn off VDP_SRC */
1375 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1376 msleep(20);
1377
1378 /* Before proceeding make sure VDP_SRC is OFF */
1379 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1380 if (chg_ctrl & 0x3F)
1381 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1382 __func__, chg_ctrl);
1383 /*
1384 * Configure DM as current source, DP as current sink
1385 * and enable battery charging comparators.
1386 */
1387 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1388}
1389
Manu Gautama1e331d2013-02-07 14:55:05 +05301390static bool dwc3_chg_det_check_linestate(struct dwc3_msm *mdwc)
1391{
1392 u32 chg_det;
1393 bool ret = false;
1394
1395 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1396 ret = chg_det & (3 << 8);
1397
1398 return ret;
1399}
1400
Manu Gautam8c642812012-06-07 10:35:10 +05301401static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1402{
1403 u32 chg_det;
1404 bool ret = false;
1405
1406 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1407 ret = chg_det & 1;
1408
1409 return ret;
1410}
1411
1412static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1413{
1414 /*
1415 * Configure DP as current source, DM as current sink
1416 * and enable battery charging comparators.
1417 */
1418 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1419}
1420
1421static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1422{
1423 u32 chg_state;
1424 bool ret = false;
1425
1426 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1427 ret = chg_state & 2;
1428
1429 return ret;
1430}
1431
1432static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1433{
1434 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1435}
1436
1437static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1438{
1439 /* Data contact detection enable, DCDENB */
1440 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1441}
1442
1443static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1444{
1445 u32 chg_ctrl;
1446
1447 /* Clear charger detecting control bits */
1448 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1449
1450 /* Clear alt interrupt latch and enable bits */
1451 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1452 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1453
1454 udelay(100);
1455
1456 /* Before proceeding make sure charger block is RESET */
1457 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1458 if (chg_ctrl & 0x3F)
1459 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1460 __func__, chg_ctrl);
1461}
1462
1463static const char *chg_to_string(enum dwc3_chg_type chg_type)
1464{
1465 switch (chg_type) {
Manu Gautama1e331d2013-02-07 14:55:05 +05301466 case DWC3_SDP_CHARGER: return "USB_SDP_CHARGER";
1467 case DWC3_DCP_CHARGER: return "USB_DCP_CHARGER";
1468 case DWC3_CDP_CHARGER: return "USB_CDP_CHARGER";
1469 case DWC3_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Manu Gautam8c642812012-06-07 10:35:10 +05301470 default: return "INVALID_CHARGER";
1471 }
1472}
1473
1474#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1475#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1476#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1477#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1478
1479static void dwc3_chg_detect_work(struct work_struct *w)
1480{
1481 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1482 bool is_dcd = false, tmout, vout;
1483 unsigned long delay;
1484
1485 dev_dbg(mdwc->dev, "chg detection work\n");
1486 switch (mdwc->chg_state) {
1487 case USB_CHG_STATE_UNDEFINED:
1488 dwc3_chg_block_reset(mdwc);
1489 dwc3_chg_enable_dcd(mdwc);
1490 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1491 mdwc->dcd_retries = 0;
1492 delay = DWC3_CHG_DCD_POLL_TIME;
1493 break;
1494 case USB_CHG_STATE_WAIT_FOR_DCD:
1495 is_dcd = dwc3_chg_check_dcd(mdwc);
1496 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1497 if (is_dcd || tmout) {
1498 dwc3_chg_disable_dcd(mdwc);
Manu Gautama1e331d2013-02-07 14:55:05 +05301499 if (dwc3_chg_det_check_linestate(mdwc)) {
1500 dev_dbg(mdwc->dev, "proprietary charger\n");
1501 mdwc->charger.chg_type =
1502 DWC3_PROPRIETARY_CHARGER;
1503 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1504 delay = 0;
1505 break;
1506 }
Manu Gautam8c642812012-06-07 10:35:10 +05301507 dwc3_chg_enable_primary_det(mdwc);
1508 delay = DWC3_CHG_PRIMARY_DET_TIME;
1509 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1510 } else {
1511 delay = DWC3_CHG_DCD_POLL_TIME;
1512 }
1513 break;
1514 case USB_CHG_STATE_DCD_DONE:
1515 vout = dwc3_chg_det_check_output(mdwc);
1516 if (vout) {
1517 dwc3_chg_enable_secondary_det(mdwc);
1518 delay = DWC3_CHG_SECONDARY_DET_TIME;
1519 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1520 } else {
Manu Gautama1e331d2013-02-07 14:55:05 +05301521 mdwc->charger.chg_type = DWC3_SDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301522 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1523 delay = 0;
1524 }
1525 break;
1526 case USB_CHG_STATE_PRIMARY_DONE:
1527 vout = dwc3_chg_det_check_output(mdwc);
1528 if (vout)
Manu Gautama1e331d2013-02-07 14:55:05 +05301529 mdwc->charger.chg_type = DWC3_DCP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301530 else
Manu Gautama1e331d2013-02-07 14:55:05 +05301531 mdwc->charger.chg_type = DWC3_CDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301532 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1533 /* fall through */
1534 case USB_CHG_STATE_SECONDARY_DONE:
1535 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1536 /* fall through */
1537 case USB_CHG_STATE_DETECTED:
1538 dwc3_chg_block_reset(mdwc);
Manu Gautama48296e2012-12-05 17:37:56 +05301539 /* Enable VDP_SRC */
1540 if (mdwc->charger.chg_type == DWC3_DCP_CHARGER)
1541 dwc3_msm_write_readback(mdwc->base,
1542 CHARGING_DET_CTRL_REG, 0x1F, 0x10);
Manu Gautam8c642812012-06-07 10:35:10 +05301543 dev_dbg(mdwc->dev, "chg_type = %s\n",
1544 chg_to_string(mdwc->charger.chg_type));
1545 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1546 &mdwc->charger);
1547 return;
1548 default:
1549 return;
1550 }
1551
1552 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1553}
1554
1555static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1556{
1557 struct dwc3_msm *mdwc = context;
1558
1559 if (start == false) {
Jack Pham9354c6a2012-12-20 19:19:32 -08001560 dev_dbg(mdwc->dev, "canceling charging detection work\n");
Manu Gautam8c642812012-06-07 10:35:10 +05301561 cancel_delayed_work_sync(&mdwc->chg_work);
1562 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1563 charger->chg_type = DWC3_INVALID_CHARGER;
1564 return;
1565 }
1566
1567 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1568 charger->chg_type = DWC3_INVALID_CHARGER;
1569 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1570}
1571
Manu Gautamb5067272012-07-02 09:53:41 +05301572static int dwc3_msm_suspend(struct dwc3_msm *mdwc)
1573{
Manu Gautam2617deb2012-08-31 17:50:06 -07001574 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301575 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301576 bool host_bus_suspend;
Manu Gautam2617deb2012-08-31 17:50:06 -07001577
Manu Gautamb5067272012-07-02 09:53:41 +05301578 dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__);
1579
1580 if (atomic_read(&mdwc->in_lpm)) {
1581 dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__);
1582 return 0;
1583 }
1584
Manu Gautama48296e2012-12-05 17:37:56 +05301585 if (mdwc->hs_phy_irq)
1586 disable_irq(mdwc->hs_phy_irq);
1587
Manu Gautam98013c22012-11-20 17:42:42 +05301588 if (cancel_delayed_work_sync(&mdwc->chg_work))
1589 dev_dbg(mdwc->dev, "%s: chg_work was pending\n", __func__);
1590 if (mdwc->chg_state != USB_CHG_STATE_DETECTED) {
1591 /* charger detection wasn't complete; re-init flags */
1592 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1593 mdwc->charger.chg_type = DWC3_INVALID_CHARGER;
Manu Gautama48296e2012-12-05 17:37:56 +05301594 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG,
1595 0x37, 0x0);
Manu Gautam98013c22012-11-20 17:42:42 +05301596 }
1597
Manu Gautama48296e2012-12-05 17:37:56 +05301598 dcp = mdwc->charger.chg_type == DWC3_DCP_CHARGER;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301599 host_bus_suspend = mdwc->host_mode == 1;
Manu Gautam377821c2012-09-28 16:53:24 +05301600
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301601 /* Sequence to put SSPHY in low power state:
1602 * 1. Clear REF_SS_PHY_EN in SS_PHY_CTRL_REG
1603 * 2. Clear REF_USE_PAD in SS_PHY_CTRL_REG
1604 * 3. Set TEST_POWERED_DOWN in SS_PHY_CTRL_REG to enable PHY retention
1605 * 4. Disable SSPHY ref clk
1606 */
1607 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8), 0x0);
1608 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28), 0x0);
1609 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26),
1610 (1 << 26));
1611
Manu Gautam377821c2012-09-28 16:53:24 +05301612 usleep_range(1000, 1200);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001613 clk_disable_unprepare(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301614
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301615 if (host_bus_suspend) {
1616 /* Sequence for host bus suspend case:
1617 * 1. Set suspend and sleep bits in GUSB2PHYCONFIG reg
1618 * 2. Clear interrupt latch register and enable BSV, ID HV intr
1619 * 3. Enable DP and DM HV interrupts in ALT_INTERRUPT_EN_REG
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301620 */
1621 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1622 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1623 0x00000140);
1624 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1625 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1626 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1627 0x18000, 0x18000);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301628 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0xFC0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301629 udelay(5);
1630 } else {
1631 /* Sequence to put hardware in low power state:
1632 * 1. Set OTGDISABLE to disable OTG block in HSPHY (saves power)
1633 * 2. Clear charger detection control fields (performed above)
1634 * 3. SUSPEND PHY and turn OFF core clock after some delay
1635 * 4. Clear interrupt latch register and enable BSV, ID HV intr
1636 * 5. Enable PHY retention
1637 */
1638 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x1000,
1639 0x1000);
1640 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1641 0xC00000, 0x800000);
1642 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1643 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1644 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1645 0x18000, 0x18000);
1646 if (!dcp)
1647 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1648 0x2, 0x0);
1649 }
Manu Gautam377821c2012-09-28 16:53:24 +05301650
1651 /* make sure above writes are completed before turning off clocks */
1652 wmb();
1653 clk_disable_unprepare(mdwc->core_clk);
1654 clk_disable_unprepare(mdwc->iface_clk);
1655
Jack Pham22698b82013-02-13 17:45:06 -08001656 if (!host_bus_suspend) {
1657 clk_disable_unprepare(mdwc->utmi_clk);
1658
1659 /* USB PHY no more requires TCXO */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301660 clk_disable_unprepare(mdwc->xo_clk);
Jack Pham22698b82013-02-13 17:45:06 -08001661 }
Manu Gautamb5067272012-07-02 09:53:41 +05301662
Manu Gautam2617deb2012-08-31 17:50:06 -07001663 if (mdwc->bus_perf_client) {
1664 ret = msm_bus_scale_client_update_request(
1665 mdwc->bus_perf_client, 0);
1666 if (ret)
1667 dev_err(mdwc->dev, "Failed to reset bus bw vote\n");
1668 }
1669
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301670 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1671 !host_bus_suspend)
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301672 dwc3_hsusb_ldo_enable(0);
1673
Vijayavardhan Vennapusa6bc06962012-10-31 13:23:38 +05301674 dwc3_ssusb_ldo_enable(0);
1675 dwc3_ssusb_config_vddcx(0);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301676 if (!host_bus_suspend)
1677 dwc3_hsusb_config_vddcx(0);
Manu Gautam377821c2012-09-28 16:53:24 +05301678 wake_unlock(&mdwc->wlock);
Manu Gautamb5067272012-07-02 09:53:41 +05301679 atomic_set(&mdwc->in_lpm, 1);
Manu Gautam377821c2012-09-28 16:53:24 +05301680
Manu Gautamb5067272012-07-02 09:53:41 +05301681 dev_info(mdwc->dev, "DWC3 in low power mode\n");
1682
Manu Gautama48296e2012-12-05 17:37:56 +05301683 if (mdwc->hs_phy_irq)
1684 enable_irq(mdwc->hs_phy_irq);
1685
Manu Gautamb5067272012-07-02 09:53:41 +05301686 return 0;
1687}
1688
1689static int dwc3_msm_resume(struct dwc3_msm *mdwc)
1690{
Manu Gautam2617deb2012-08-31 17:50:06 -07001691 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301692 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301693 bool host_bus_suspend;
Manu Gautam2617deb2012-08-31 17:50:06 -07001694
Manu Gautamb5067272012-07-02 09:53:41 +05301695 dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__);
1696
1697 if (!atomic_read(&mdwc->in_lpm)) {
1698 dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__);
1699 return 0;
1700 }
1701
Manu Gautam377821c2012-09-28 16:53:24 +05301702 wake_lock(&mdwc->wlock);
1703
Manu Gautam2617deb2012-08-31 17:50:06 -07001704 if (mdwc->bus_perf_client) {
1705 ret = msm_bus_scale_client_update_request(
1706 mdwc->bus_perf_client, 1);
1707 if (ret)
1708 dev_err(mdwc->dev, "Failed to vote for bus scaling\n");
1709 }
1710
Manu Gautama48296e2012-12-05 17:37:56 +05301711 dcp = mdwc->charger.chg_type == DWC3_DCP_CHARGER;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301712 host_bus_suspend = mdwc->host_mode == 1;
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301713
1714 if (!host_bus_suspend) {
1715 /* Vote for TCXO while waking up USB HSPHY */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301716 ret = clk_prepare_enable(mdwc->xo_clk);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301717 if (ret)
1718 dev_err(mdwc->dev, "%s failed to vote TCXO buffer%d\n",
1719 __func__, ret);
1720 }
1721
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301722 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1723 !host_bus_suspend)
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301724 dwc3_hsusb_ldo_enable(1);
1725
Vijayavardhan Vennapusa6bc06962012-10-31 13:23:38 +05301726 dwc3_ssusb_ldo_enable(1);
1727 dwc3_ssusb_config_vddcx(1);
Jack Pham22698b82013-02-13 17:45:06 -08001728
1729 if (!host_bus_suspend) {
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301730 dwc3_hsusb_config_vddcx(1);
Jack Pham22698b82013-02-13 17:45:06 -08001731 clk_prepare_enable(mdwc->utmi_clk);
1732 }
1733
Manu Gautam3e9ad352012-08-16 14:44:47 -07001734 clk_prepare_enable(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301735 usleep_range(1000, 1200);
1736
Manu Gautam3e9ad352012-08-16 14:44:47 -07001737 clk_prepare_enable(mdwc->iface_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301738 clk_prepare_enable(mdwc->core_clk);
1739
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301740 if (host_bus_suspend) {
1741 /* Disable HV interrupt */
1742 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1743 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1744 0x18000, 0x0);
1745 /* Clear interrupt latch register */
1746 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301747
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301748 /* Disable DP and DM HV interrupt */
1749 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301750
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301751 /* Clear suspend bit in GUSB2PHYCONFIG register */
1752 dwc3_msm_write_readback(mdwc->base, DWC3_GUSB2PHYCFG(0),
1753 0x40, 0x0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301754 } else {
1755 /* Disable HV interrupt */
1756 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1757 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1758 0x18000, 0x0);
1759 /* Disable Retention */
1760 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x2, 0x2);
1761
1762 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1763 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1764 0xF0000000);
1765 /* 10usec delay required before de-asserting PHY RESET */
1766 udelay(10);
1767 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1768 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) &
1769 0x7FFFFFFF);
1770
1771 /* Bring PHY out of suspend */
1772 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0xC00000,
1773 0x0);
1774
1775 }
Manu Gautamb5067272012-07-02 09:53:41 +05301776
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301777 /* Assert SS PHY RESET */
1778 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7),
1779 (1 << 7));
1780 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28),
1781 (1 << 28));
1782 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8),
1783 (1 << 8));
1784 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26), 0x0);
1785 /* 10usec delay required before de-asserting SS PHY RESET */
1786 udelay(10);
1787 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7), 0x0);
1788
Manu Gautamb5067272012-07-02 09:53:41 +05301789 atomic_set(&mdwc->in_lpm, 0);
Manu Gautam377821c2012-09-28 16:53:24 +05301790
1791 /* match disable_irq call from isr */
1792 if (mdwc->lpm_irq_seen && mdwc->hs_phy_irq) {
1793 enable_irq(mdwc->hs_phy_irq);
1794 mdwc->lpm_irq_seen = false;
1795 }
1796
Manu Gautamb5067272012-07-02 09:53:41 +05301797 dev_info(mdwc->dev, "DWC3 exited from low power mode\n");
1798
1799 return 0;
1800}
1801
1802static void dwc3_resume_work(struct work_struct *w)
1803{
1804 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1805 resume_work.work);
1806
1807 dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__);
1808 /* handle any event that was queued while work was already running */
1809 if (!atomic_read(&mdwc->in_lpm)) {
1810 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1811 if (mdwc->otg_xceiv)
1812 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1813 DWC3_EVENT_XCEIV_STATE);
1814 return;
1815 }
1816
1817 /* bail out if system resume in process, else initiate RESUME */
1818 if (atomic_read(&mdwc->pm_suspended)) {
1819 mdwc->resume_pending = true;
1820 } else {
1821 pm_runtime_get_sync(mdwc->dev);
1822 if (mdwc->otg_xceiv)
1823 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1824 DWC3_EVENT_PHY_RESUME);
1825 pm_runtime_put_sync(mdwc->dev);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301826 if (mdwc->otg_xceiv && (mdwc->ext_xceiv.otg_capability))
1827 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1828 DWC3_EVENT_XCEIV_STATE);
Manu Gautamb5067272012-07-02 09:53:41 +05301829 }
1830}
1831
Jack Pham0fc12332012-11-19 13:14:22 -08001832static u32 debug_id = true, debug_bsv, debug_connect;
Manu Gautamb5067272012-07-02 09:53:41 +05301833
1834static int dwc3_connect_show(struct seq_file *s, void *unused)
1835{
1836 if (debug_connect)
1837 seq_printf(s, "true\n");
1838 else
1839 seq_printf(s, "false\n");
1840
1841 return 0;
1842}
1843
1844static int dwc3_connect_open(struct inode *inode, struct file *file)
1845{
1846 return single_open(file, dwc3_connect_show, inode->i_private);
1847}
1848
1849static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf,
1850 size_t count, loff_t *ppos)
1851{
1852 struct seq_file *s = file->private_data;
1853 struct dwc3_msm *mdwc = s->private;
1854 char buf[8];
1855
1856 memset(buf, 0x00, sizeof(buf));
1857
1858 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1859 return -EFAULT;
1860
1861 if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) {
1862 debug_connect = true;
1863 } else {
1864 debug_connect = debug_bsv = false;
1865 debug_id = true;
1866 }
1867
1868 mdwc->ext_xceiv.bsv = debug_bsv;
1869 mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND;
1870
1871 if (atomic_read(&mdwc->in_lpm)) {
1872 dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__);
1873 dwc3_resume_work(&mdwc->resume_work.work);
1874 } else {
1875 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1876 if (mdwc->otg_xceiv)
1877 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1878 DWC3_EVENT_XCEIV_STATE);
1879 }
1880
1881 return count;
1882}
1883
1884const struct file_operations dwc3_connect_fops = {
1885 .open = dwc3_connect_open,
1886 .read = seq_read,
1887 .write = dwc3_connect_write,
1888 .llseek = seq_lseek,
1889 .release = single_release,
1890};
1891
1892static struct dentry *dwc3_debugfs_root;
1893
1894static void dwc3_debugfs_init(struct dwc3_msm *mdwc)
1895{
1896 dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL);
1897
1898 if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root))
1899 return;
1900
1901 if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05301902 &debug_id))
Manu Gautamb5067272012-07-02 09:53:41 +05301903 goto error;
1904
1905 if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05301906 &debug_bsv))
Manu Gautamb5067272012-07-02 09:53:41 +05301907 goto error;
1908
1909 if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR,
1910 dwc3_debugfs_root, mdwc, &dwc3_connect_fops))
1911 goto error;
1912
1913 return;
1914
1915error:
1916 debugfs_remove_recursive(dwc3_debugfs_root);
1917}
Manu Gautam8c642812012-06-07 10:35:10 +05301918
Manu Gautam377821c2012-09-28 16:53:24 +05301919static irqreturn_t msm_dwc3_irq(int irq, void *data)
1920{
1921 struct dwc3_msm *mdwc = data;
1922
1923 if (atomic_read(&mdwc->in_lpm)) {
1924 dev_dbg(mdwc->dev, "%s received in LPM\n", __func__);
1925 mdwc->lpm_irq_seen = true;
1926 disable_irq_nosync(irq);
1927 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
1928 } else {
1929 pr_info_ratelimited("%s: IRQ outside LPM\n", __func__);
1930 }
1931
1932 return IRQ_HANDLED;
1933}
1934
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301935static int dwc3_msm_power_get_property_usb(struct power_supply *psy,
1936 enum power_supply_property psp,
1937 union power_supply_propval *val)
1938{
1939 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
1940 usb_psy);
1941 switch (psp) {
1942 case POWER_SUPPLY_PROP_SCOPE:
1943 val->intval = mdwc->host_mode;
1944 break;
1945 case POWER_SUPPLY_PROP_CURRENT_MAX:
1946 val->intval = mdwc->current_max;
1947 break;
1948 case POWER_SUPPLY_PROP_PRESENT:
1949 val->intval = mdwc->vbus_active;
1950 break;
1951 case POWER_SUPPLY_PROP_ONLINE:
1952 val->intval = mdwc->online;
1953 break;
Manu Gautamfa40cae2013-03-01 16:37:12 +05301954 case POWER_SUPPLY_PROP_TYPE:
1955 val->intval = psy->type;
1956 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301957 default:
1958 return -EINVAL;
1959 }
1960 return 0;
1961}
1962
1963static int dwc3_msm_power_set_property_usb(struct power_supply *psy,
1964 enum power_supply_property psp,
1965 const union power_supply_propval *val)
1966{
1967 static bool init;
1968 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
1969 usb_psy);
1970
1971 switch (psp) {
1972 case POWER_SUPPLY_PROP_SCOPE:
1973 mdwc->host_mode = val->intval;
1974 break;
1975 /* Process PMIC notification in PRESENT prop */
1976 case POWER_SUPPLY_PROP_PRESENT:
1977 dev_dbg(mdwc->dev, "%s: notify xceiv event\n", __func__);
Jack Pham9354c6a2012-12-20 19:19:32 -08001978 if (mdwc->otg_xceiv && !mdwc->ext_inuse &&
1979 (mdwc->ext_xceiv.otg_capability || !init)) {
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301980 mdwc->ext_xceiv.bsv = val->intval;
Manu Gautamf71d9cb2013-02-07 13:52:12 +05301981 queue_delayed_work(system_nrt_wq,
Jack Pham4d91aab2013-03-08 10:02:16 -08001982 &mdwc->resume_work, 20);
Jack Pham9354c6a2012-12-20 19:19:32 -08001983
1984 if (!init)
1985 init = true;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301986 }
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301987 mdwc->vbus_active = val->intval;
1988 break;
1989 case POWER_SUPPLY_PROP_ONLINE:
1990 mdwc->online = val->intval;
1991 break;
1992 case POWER_SUPPLY_PROP_CURRENT_MAX:
1993 mdwc->current_max = val->intval;
1994 break;
Manu Gautamfa40cae2013-03-01 16:37:12 +05301995 case POWER_SUPPLY_PROP_TYPE:
1996 psy->type = val->intval;
1997 break;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301998 default:
1999 return -EINVAL;
2000 }
2001
2002 power_supply_changed(&mdwc->usb_psy);
2003 return 0;
2004}
2005
Jack Pham9354c6a2012-12-20 19:19:32 -08002006static void dwc3_msm_external_power_changed(struct power_supply *psy)
2007{
2008 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm, usb_psy);
2009 union power_supply_propval ret = {0,};
2010
2011 if (!mdwc->ext_vbus_psy)
2012 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2013
2014 if (!mdwc->ext_vbus_psy) {
2015 pr_err("%s: Unable to get ext_vbus power_supply\n", __func__);
2016 return;
2017 }
2018
2019 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2020 POWER_SUPPLY_PROP_ONLINE, &ret);
2021 if (ret.intval) {
2022 dwc3_start_chg_det(&mdwc->charger, false);
2023 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2024 POWER_SUPPLY_PROP_CURRENT_MAX, &ret);
2025 power_supply_set_current_limit(&mdwc->usb_psy, ret.intval);
2026 }
2027
2028 power_supply_set_online(&mdwc->usb_psy, ret.intval);
2029 power_supply_changed(&mdwc->usb_psy);
2030}
2031
2032
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302033static char *dwc3_msm_pm_power_supplied_to[] = {
2034 "battery",
2035};
2036
2037static enum power_supply_property dwc3_msm_pm_power_props_usb[] = {
2038 POWER_SUPPLY_PROP_PRESENT,
2039 POWER_SUPPLY_PROP_ONLINE,
2040 POWER_SUPPLY_PROP_CURRENT_MAX,
2041 POWER_SUPPLY_PROP_SCOPE,
2042};
2043
Jack Phamfadd6432012-12-07 19:03:41 -08002044static void dwc3_init_adc_work(struct work_struct *w);
2045
2046static void dwc3_ext_notify_online(int on)
2047{
2048 struct dwc3_msm *mdwc = context;
Jack Phamf12b7e12012-12-28 14:27:26 -08002049 bool notify_otg = false;
Jack Phamfadd6432012-12-07 19:03:41 -08002050
2051 if (!mdwc) {
2052 pr_err("%s: DWC3 driver already removed\n", __func__);
2053 return;
2054 }
2055
2056 dev_dbg(mdwc->dev, "notify %s%s\n", on ? "" : "dis", "connected");
2057
Jack Pham9354c6a2012-12-20 19:19:32 -08002058 if (!mdwc->ext_vbus_psy)
2059 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2060
2061 mdwc->ext_inuse = on;
Jack Phamf12b7e12012-12-28 14:27:26 -08002062 if (on) {
2063 /* force OTG to exit B-peripheral state */
2064 mdwc->ext_xceiv.bsv = false;
2065 notify_otg = true;
Jack Pham9354c6a2012-12-20 19:19:32 -08002066 dwc3_start_chg_det(&mdwc->charger, false);
Jack Phamf12b7e12012-12-28 14:27:26 -08002067 } else {
2068 /* external client offline; tell OTG about cached ID/BSV */
2069 if (mdwc->ext_xceiv.id != mdwc->id_state) {
2070 mdwc->ext_xceiv.id = mdwc->id_state;
2071 notify_otg = true;
2072 }
2073
2074 mdwc->ext_xceiv.bsv = mdwc->vbus_active;
2075 notify_otg |= mdwc->vbus_active;
2076 }
Jack Pham9354c6a2012-12-20 19:19:32 -08002077
2078 if (mdwc->ext_vbus_psy)
2079 power_supply_set_present(mdwc->ext_vbus_psy, on);
Jack Phamf12b7e12012-12-28 14:27:26 -08002080
2081 if (notify_otg)
2082 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
Jack Phamfadd6432012-12-07 19:03:41 -08002083}
2084
Jack Pham0cca9412013-03-08 13:22:42 -08002085static void dwc3_id_work(struct work_struct *w)
Jack Phamfadd6432012-12-07 19:03:41 -08002086{
Jack Pham0cca9412013-03-08 13:22:42 -08002087 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, id_work);
Jack Phamfadd6432012-12-07 19:03:41 -08002088
Jack Pham0cca9412013-03-08 13:22:42 -08002089 /* Give external client a chance to handle */
2090 if (!mdwc->ext_inuse) {
2091 if (usb_ext) {
2092 int ret = usb_ext->notify(usb_ext->ctxt, mdwc->id_state,
2093 dwc3_ext_notify_online);
2094 dev_dbg(mdwc->dev, "%s: external handler returned %d\n",
2095 __func__, ret);
2096 mdwc->ext_inuse = (ret == 0);
2097 }
2098 }
Jack Phamfadd6432012-12-07 19:03:41 -08002099
Jack Pham0cca9412013-03-08 13:22:42 -08002100 if (!mdwc->ext_inuse) { /* notify OTG */
2101 mdwc->ext_xceiv.id = mdwc->id_state;
2102 dwc3_resume_work(&mdwc->resume_work.work);
2103 }
2104}
2105
2106static irqreturn_t dwc3_pmic_id_irq(int irq, void *data)
2107{
2108 struct dwc3_msm *mdwc = data;
2109
2110 /* If we can't read ID line state for some reason, treat it as float */
2111 mdwc->id_state = !!irq_read_line(irq);
2112 queue_work(system_nrt_wq, &mdwc->id_work);
2113
2114 return IRQ_HANDLED;
Jack Phamfadd6432012-12-07 19:03:41 -08002115}
2116
Jack Pham0fc12332012-11-19 13:14:22 -08002117static void dwc3_adc_notification(enum qpnp_tm_state state, void *ctx)
2118{
2119 struct dwc3_msm *mdwc = ctx;
2120
2121 if (state >= ADC_TM_STATE_NUM) {
2122 pr_err("%s: invalid notification %d\n", __func__, state);
2123 return;
2124 }
2125
2126 dev_dbg(mdwc->dev, "%s: state = %s\n", __func__,
2127 state == ADC_TM_HIGH_STATE ? "high" : "low");
2128
Jack Phamf12b7e12012-12-28 14:27:26 -08002129 /* save ID state, but don't necessarily notify OTG */
Jack Pham0fc12332012-11-19 13:14:22 -08002130 if (state == ADC_TM_HIGH_STATE) {
Jack Phamf12b7e12012-12-28 14:27:26 -08002131 mdwc->id_state = DWC3_ID_FLOAT;
Jack Pham0fc12332012-11-19 13:14:22 -08002132 mdwc->adc_param.state_request = ADC_TM_LOW_THR_ENABLE;
2133 } else {
Jack Phamf12b7e12012-12-28 14:27:26 -08002134 mdwc->id_state = DWC3_ID_GROUND;
Jack Pham0fc12332012-11-19 13:14:22 -08002135 mdwc->adc_param.state_request = ADC_TM_HIGH_THR_ENABLE;
2136 }
2137
Jack Pham0cca9412013-03-08 13:22:42 -08002138 dwc3_id_work(&mdwc->id_work);
2139
Jack Phamfadd6432012-12-07 19:03:41 -08002140 /* re-arm ADC interrupt */
Jack Pham0fc12332012-11-19 13:14:22 -08002141 qpnp_adc_tm_usbid_configure(&mdwc->adc_param);
2142}
2143
2144static void dwc3_init_adc_work(struct work_struct *w)
2145{
2146 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
2147 init_adc_work.work);
2148 int ret;
2149
2150 ret = qpnp_adc_tm_is_ready();
2151 if (ret == -EPROBE_DEFER) {
Jack Pham90b4d122012-12-13 11:46:22 -08002152 queue_delayed_work(system_nrt_wq, to_delayed_work(w),
2153 msecs_to_jiffies(100));
Jack Pham0fc12332012-11-19 13:14:22 -08002154 return;
2155 }
2156
2157 mdwc->adc_param.low_thr = adc_low_threshold;
2158 mdwc->adc_param.high_thr = adc_high_threshold;
2159 mdwc->adc_param.timer_interval = adc_meas_interval;
2160 mdwc->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2161 mdwc->adc_param.usbid_ctx = mdwc;
2162 mdwc->adc_param.threshold_notification = dwc3_adc_notification;
2163
2164 ret = qpnp_adc_tm_usbid_configure(&mdwc->adc_param);
2165 if (ret) {
2166 dev_err(mdwc->dev, "%s: request ADC error %d\n", __func__, ret);
2167 return;
2168 }
2169
2170 mdwc->id_adc_detect = true;
2171}
2172
2173static ssize_t adc_enable_show(struct device *dev,
2174 struct device_attribute *attr, char *buf)
2175{
2176 return snprintf(buf, PAGE_SIZE, "%s\n", context->id_adc_detect ?
2177 "enabled" : "disabled");
2178}
2179
2180static ssize_t adc_enable_store(struct device *dev,
2181 struct device_attribute *attr, const char
2182 *buf, size_t size)
2183{
2184 if (!strnicmp(buf, "enable", 6)) {
2185 if (!context->id_adc_detect)
2186 dwc3_init_adc_work(&context->init_adc_work.work);
2187 return size;
2188 } else if (!strnicmp(buf, "disable", 7)) {
2189 qpnp_adc_tm_usbid_end();
2190 context->id_adc_detect = false;
2191 return size;
2192 }
2193
2194 return -EINVAL;
2195}
2196
2197static DEVICE_ATTR(adc_enable, S_IRUGO | S_IWUSR, adc_enable_show,
2198 adc_enable_store);
2199
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002200static int __devinit dwc3_msm_probe(struct platform_device *pdev)
2201{
2202 struct device_node *node = pdev->dev.of_node;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002203 struct dwc3_msm *msm;
2204 struct resource *res;
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002205 void __iomem *tcsr;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002206 int ret = 0;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302207 int len = 0;
2208 u32 tmp[3];
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002209
2210 msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL);
2211 if (!msm) {
2212 dev_err(&pdev->dev, "not enough memory\n");
2213 return -ENOMEM;
2214 }
2215
2216 platform_set_drvdata(pdev, msm);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002217 context = msm;
Manu Gautam60e01352012-05-29 09:00:34 +05302218 msm->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002219
2220 INIT_LIST_HEAD(&msm->req_complete_list);
Manu Gautam8c642812012-06-07 10:35:10 +05302221 INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work);
Manu Gautamb5067272012-07-02 09:53:41 +05302222 INIT_DELAYED_WORK(&msm->resume_work, dwc3_resume_work);
Manu Gautam6eb13e32013-02-01 15:19:15 +05302223 INIT_WORK(&msm->restart_usb_work, dwc3_restart_usb_work);
Jack Pham0cca9412013-03-08 13:22:42 -08002224 INIT_WORK(&msm->id_work, dwc3_id_work);
Jack Pham0fc12332012-11-19 13:14:22 -08002225 INIT_DELAYED_WORK(&msm->init_adc_work, dwc3_init_adc_work);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002226
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302227 msm->xo_clk = clk_get(&pdev->dev, "xo");
2228 if (IS_ERR(msm->xo_clk)) {
Manu Gautam377821c2012-09-28 16:53:24 +05302229 dev_err(&pdev->dev, "%s unable to get TCXO buffer handle\n",
2230 __func__);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302231 return PTR_ERR(msm->xo_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05302232 }
2233
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302234 ret = clk_prepare_enable(msm->xo_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05302235 if (ret) {
2236 dev_err(&pdev->dev, "%s failed to vote for TCXO buffer%d\n",
2237 __func__, ret);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302238 goto put_xo;
Manu Gautam377821c2012-09-28 16:53:24 +05302239 }
2240
Manu Gautam1742db22012-06-19 13:33:24 +05302241 /*
2242 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
2243 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
2244 */
2245 msm->core_clk = devm_clk_get(&pdev->dev, "core_clk");
2246 if (IS_ERR(msm->core_clk)) {
2247 dev_err(&pdev->dev, "failed to get core_clk\n");
Manu Gautam377821c2012-09-28 16:53:24 +05302248 ret = PTR_ERR(msm->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302249 goto disable_xo;
Manu Gautam1742db22012-06-19 13:33:24 +05302250 }
2251 clk_set_rate(msm->core_clk, 125000000);
2252 clk_prepare_enable(msm->core_clk);
2253
Manu Gautam3e9ad352012-08-16 14:44:47 -07002254 msm->iface_clk = devm_clk_get(&pdev->dev, "iface_clk");
2255 if (IS_ERR(msm->iface_clk)) {
2256 dev_err(&pdev->dev, "failed to get iface_clk\n");
2257 ret = PTR_ERR(msm->iface_clk);
2258 goto disable_core_clk;
2259 }
2260 clk_prepare_enable(msm->iface_clk);
2261
2262 msm->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2263 if (IS_ERR(msm->sleep_clk)) {
2264 dev_err(&pdev->dev, "failed to get sleep_clk\n");
2265 ret = PTR_ERR(msm->sleep_clk);
2266 goto disable_iface_clk;
2267 }
2268 clk_prepare_enable(msm->sleep_clk);
2269
2270 msm->hsphy_sleep_clk = devm_clk_get(&pdev->dev, "sleep_a_clk");
2271 if (IS_ERR(msm->hsphy_sleep_clk)) {
2272 dev_err(&pdev->dev, "failed to get sleep_a_clk\n");
2273 ret = PTR_ERR(msm->hsphy_sleep_clk);
2274 goto disable_sleep_clk;
2275 }
2276 clk_prepare_enable(msm->hsphy_sleep_clk);
2277
Jack Pham22698b82013-02-13 17:45:06 -08002278 msm->utmi_clk = devm_clk_get(&pdev->dev, "utmi_clk");
2279 if (IS_ERR(msm->utmi_clk)) {
2280 dev_err(&pdev->dev, "failed to get utmi_clk\n");
2281 ret = PTR_ERR(msm->utmi_clk);
2282 goto disable_sleep_a_clk;
2283 }
2284 clk_prepare_enable(msm->utmi_clk);
2285
Manu Gautam3e9ad352012-08-16 14:44:47 -07002286 msm->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
2287 if (IS_ERR(msm->ref_clk)) {
2288 dev_err(&pdev->dev, "failed to get ref_clk\n");
2289 ret = PTR_ERR(msm->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002290 goto disable_utmi_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -07002291 }
2292 clk_prepare_enable(msm->ref_clk);
2293
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302294 of_get_property(node, "qcom,vdd-voltage-level", &len);
2295 if (len == sizeof(tmp)) {
2296 of_property_read_u32_array(node, "qcom,vdd-voltage-level",
2297 tmp, len/sizeof(*tmp));
2298 msm->vdd_no_vol_level = tmp[0];
2299 msm->vdd_low_vol_level = tmp[1];
2300 msm->vdd_high_vol_level = tmp[2];
2301 } else {
2302 dev_err(&pdev->dev, "no qcom,vdd-voltage-level property\n");
2303 ret = -EINVAL;
2304 goto disable_ref_clk;
2305 }
2306
Manu Gautam60e01352012-05-29 09:00:34 +05302307 /* SS PHY */
Manu Gautam60e01352012-05-29 09:00:34 +05302308 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
2309 if (IS_ERR(msm->ssusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302310 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
2311 ret = PTR_ERR(msm->ssusb_vddcx);
2312 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302313 }
2314
2315 ret = dwc3_ssusb_config_vddcx(1);
2316 if (ret) {
2317 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam3e9ad352012-08-16 14:44:47 -07002318 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302319 }
2320
2321 ret = regulator_enable(context->ssusb_vddcx);
2322 if (ret) {
2323 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
2324 goto unconfig_ss_vddcx;
2325 }
2326
2327 ret = dwc3_ssusb_ldo_init(1);
2328 if (ret) {
2329 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
2330 goto disable_ss_vddcx;
2331 }
2332
2333 ret = dwc3_ssusb_ldo_enable(1);
2334 if (ret) {
2335 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
2336 goto free_ss_ldo_init;
2337 }
2338
2339 /* HS PHY */
Manu Gautam60e01352012-05-29 09:00:34 +05302340 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
2341 if (IS_ERR(msm->hsusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302342 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
2343 ret = PTR_ERR(msm->hsusb_vddcx);
2344 goto disable_ss_ldo;
Manu Gautam60e01352012-05-29 09:00:34 +05302345 }
2346
2347 ret = dwc3_hsusb_config_vddcx(1);
2348 if (ret) {
2349 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2350 goto disable_ss_ldo;
2351 }
2352
2353 ret = regulator_enable(context->hsusb_vddcx);
2354 if (ret) {
2355 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
2356 goto unconfig_hs_vddcx;
2357 }
2358
2359 ret = dwc3_hsusb_ldo_init(1);
2360 if (ret) {
2361 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
2362 goto disable_hs_vddcx;
2363 }
2364
2365 ret = dwc3_hsusb_ldo_enable(1);
2366 if (ret) {
2367 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
2368 goto free_hs_ldo_init;
2369 }
2370
Jack Pham0fc12332012-11-19 13:14:22 -08002371 msm->ext_xceiv.id = DWC3_ID_FLOAT;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302372 msm->ext_xceiv.otg_capability = of_property_read_bool(node,
Manu Gautam6c0ff032012-11-02 14:55:35 +05302373 "qcom,otg-capability");
2374 msm->charger.charging_disabled = of_property_read_bool(node,
2375 "qcom,charging-disabled");
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302376
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302377 /*
2378 * DWC3 has separate IRQ line for OTG events (ID/BSV) and for
2379 * DP and DM linestate transitions during low power mode.
2380 */
2381 msm->hs_phy_irq = platform_get_irq_byname(pdev, "hs_phy_irq");
2382 if (msm->hs_phy_irq < 0) {
2383 dev_dbg(&pdev->dev, "pget_irq for hs_phy_irq failed\n");
2384 msm->hs_phy_irq = 0;
Jack Pham0fc12332012-11-19 13:14:22 -08002385 } else {
Jack Pham56a0a632013-03-08 13:18:42 -08002386 ret = devm_request_irq(&pdev->dev, msm->hs_phy_irq,
2387 msm_dwc3_irq, IRQF_TRIGGER_RISING,
2388 "msm_dwc3", msm);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302389 if (ret) {
2390 dev_err(&pdev->dev, "irqreq HSPHYINT failed\n");
2391 goto disable_hs_ldo;
2392 }
2393 enable_irq_wake(msm->hs_phy_irq);
2394 }
Jack Pham0cca9412013-03-08 13:22:42 -08002395
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302396 if (msm->ext_xceiv.otg_capability) {
Jack Pham0cca9412013-03-08 13:22:42 -08002397 msm->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
2398 if (msm->pmic_id_irq > 0) {
David Keitelad4a0282013-03-19 18:04:27 -07002399 /* check if PMIC ID IRQ is supported */
2400 ret = qpnp_misc_irqs_available(&pdev->dev);
2401
2402 if (ret == -EPROBE_DEFER) {
2403 /* qpnp hasn't probed yet; defer dwc probe */
Jack Pham0cca9412013-03-08 13:22:42 -08002404 goto disable_hs_ldo;
David Keitelad4a0282013-03-19 18:04:27 -07002405 } else if (ret == 0) {
2406 msm->pmic_id_irq = 0;
2407 } else {
2408 ret = devm_request_irq(&pdev->dev,
2409 msm->pmic_id_irq,
2410 dwc3_pmic_id_irq,
2411 IRQF_TRIGGER_RISING |
2412 IRQF_TRIGGER_FALLING,
2413 "dwc3_msm_pmic_id", msm);
2414 if (ret) {
2415 dev_err(&pdev->dev, "irqreq IDINT failed\n");
2416 goto disable_hs_ldo;
2417 }
2418 enable_irq_wake(msm->pmic_id_irq);
Jack Pham0cca9412013-03-08 13:22:42 -08002419 }
David Keitelad4a0282013-03-19 18:04:27 -07002420 }
2421
2422 if (msm->pmic_id_irq <= 0) {
Jack Pham0cca9412013-03-08 13:22:42 -08002423 /* If no PMIC ID IRQ, use ADC for ID pin detection */
2424 queue_work(system_nrt_wq, &msm->init_adc_work.work);
2425 device_create_file(&pdev->dev, &dev_attr_adc_enable);
2426 msm->pmic_id_irq = 0;
2427 }
Manu Gautam377821c2012-09-28 16:53:24 +05302428 }
2429
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002430 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2431 if (!res) {
2432 dev_dbg(&pdev->dev, "missing TCSR memory resource\n");
2433 } else {
2434 tcsr = devm_ioremap_nocache(&pdev->dev, res->start,
2435 resource_size(res));
2436 if (!tcsr) {
2437 dev_dbg(&pdev->dev, "tcsr ioremap failed\n");
2438 } else {
2439 /* Enable USB3 on the primary USB port. */
2440 writel_relaxed(0x1, tcsr);
2441 /*
2442 * Ensure that TCSR write is completed before
2443 * USB registers initialization.
2444 */
2445 mb();
2446 }
2447 }
2448
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002449 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2450 if (!res) {
2451 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302452 ret = -ENODEV;
Jack Pham56a0a632013-03-08 13:18:42 -08002453 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002454 }
2455
2456 msm->base = devm_ioremap_nocache(&pdev->dev, res->start,
2457 resource_size(res));
2458 if (!msm->base) {
2459 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302460 ret = -ENODEV;
Jack Pham56a0a632013-03-08 13:18:42 -08002461 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002462 }
2463
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002464 msm->resource_size = resource_size(res);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002465
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05302466 if (of_property_read_u32(node, "qcom,dwc-hsphy-init",
2467 &msm->hsphy_init_seq))
2468 dev_dbg(&pdev->dev, "unable to read hsphy init seq\n");
2469 else if (!msm->hsphy_init_seq)
2470 dev_warn(&pdev->dev, "incorrect hsphyinitseq.Using PORvalue\n");
2471
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05302472 dwc3_msm_qscratch_reg_init(msm);
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +05302473
Manu Gautamb5067272012-07-02 09:53:41 +05302474 pm_runtime_set_active(msm->dev);
Manu Gautam377821c2012-09-28 16:53:24 +05302475 pm_runtime_enable(msm->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05302476
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002477 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
2478 &msm->dbm_num_eps)) {
2479 dev_err(&pdev->dev,
2480 "unable to read platform data num of dbm eps\n");
2481 msm->dbm_num_eps = DBM_MAX_EPS;
2482 }
2483
2484 if (msm->dbm_num_eps > DBM_MAX_EPS) {
2485 dev_err(&pdev->dev,
2486 "Driver doesn't support number of DBM EPs. "
2487 "max: %d, dbm_num_eps: %d\n",
2488 DBM_MAX_EPS, msm->dbm_num_eps);
2489 ret = -ENODEV;
Vijayavardhan Vennapusad5cd3ee2013-03-19 11:52:59 +05302490 goto disable_hs_ldo;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002491 }
2492
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302493 msm->usb_psy.name = "usb";
2494 msm->usb_psy.type = POWER_SUPPLY_TYPE_USB;
2495 msm->usb_psy.supplied_to = dwc3_msm_pm_power_supplied_to;
2496 msm->usb_psy.num_supplicants = ARRAY_SIZE(
2497 dwc3_msm_pm_power_supplied_to);
2498 msm->usb_psy.properties = dwc3_msm_pm_power_props_usb;
2499 msm->usb_psy.num_properties = ARRAY_SIZE(dwc3_msm_pm_power_props_usb);
2500 msm->usb_psy.get_property = dwc3_msm_power_get_property_usb;
2501 msm->usb_psy.set_property = dwc3_msm_power_set_property_usb;
Jack Pham9354c6a2012-12-20 19:19:32 -08002502 msm->usb_psy.external_power_changed =
2503 dwc3_msm_external_power_changed;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302504
2505 ret = power_supply_register(&pdev->dev, &msm->usb_psy);
2506 if (ret < 0) {
2507 dev_err(&pdev->dev,
2508 "%s:power_supply_register usb failed\n",
2509 __func__);
Vijayavardhan Vennapusad5cd3ee2013-03-19 11:52:59 +05302510 goto disable_hs_ldo;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302511 }
2512
Vijayavardhan Vennapusad5cd3ee2013-03-19 11:52:59 +05302513 if (node) {
2514 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2515 if (ret) {
2516 dev_err(&pdev->dev,
2517 "failed to add create dwc3 core\n");
2518 goto put_psupply;
2519 }
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002520 }
2521
Manu Gautam2617deb2012-08-31 17:50:06 -07002522 msm->bus_scale_table = msm_bus_cl_get_pdata(pdev);
2523 if (!msm->bus_scale_table) {
2524 dev_err(&pdev->dev, "bus scaling is disabled\n");
2525 } else {
2526 msm->bus_perf_client =
2527 msm_bus_scale_register_client(msm->bus_scale_table);
2528 ret = msm_bus_scale_client_update_request(
2529 msm->bus_perf_client, 1);
2530 if (ret)
2531 dev_err(&pdev->dev, "Failed to vote for bus scaling\n");
2532 }
2533
Manu Gautam8c642812012-06-07 10:35:10 +05302534 msm->otg_xceiv = usb_get_transceiver();
2535 if (msm->otg_xceiv) {
2536 msm->charger.start_detection = dwc3_start_chg_det;
2537 ret = dwc3_set_charger(msm->otg_xceiv->otg, &msm->charger);
2538 if (ret || !msm->charger.notify_detection_complete) {
2539 dev_err(&pdev->dev, "failed to register charger: %d\n",
2540 ret);
2541 goto put_xcvr;
2542 }
Manu Gautamb5067272012-07-02 09:53:41 +05302543
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05302544 if (msm->ext_xceiv.otg_capability)
2545 msm->ext_xceiv.ext_block_reset = dwc3_msm_block_reset;
Manu Gautamb5067272012-07-02 09:53:41 +05302546 ret = dwc3_set_ext_xceiv(msm->otg_xceiv->otg, &msm->ext_xceiv);
2547 if (ret || !msm->ext_xceiv.notify_ext_events) {
2548 dev_err(&pdev->dev, "failed to register xceiver: %d\n",
2549 ret);
2550 goto put_xcvr;
2551 }
Manu Gautam8c642812012-06-07 10:35:10 +05302552 } else {
2553 dev_err(&pdev->dev, "%s: No OTG transceiver found\n", __func__);
2554 }
2555
Manu Gautamb5067272012-07-02 09:53:41 +05302556 wake_lock_init(&msm->wlock, WAKE_LOCK_SUSPEND, "msm_dwc3");
2557 wake_lock(&msm->wlock);
2558 dwc3_debugfs_init(msm);
2559
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002560 return 0;
2561
Manu Gautam8c642812012-06-07 10:35:10 +05302562put_xcvr:
2563 usb_put_transceiver(msm->otg_xceiv);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302564put_psupply:
2565 power_supply_unregister(&msm->usb_psy);
Manu Gautam60e01352012-05-29 09:00:34 +05302566disable_hs_ldo:
2567 dwc3_hsusb_ldo_enable(0);
2568free_hs_ldo_init:
2569 dwc3_hsusb_ldo_init(0);
2570disable_hs_vddcx:
2571 regulator_disable(context->hsusb_vddcx);
2572unconfig_hs_vddcx:
2573 dwc3_hsusb_config_vddcx(0);
2574disable_ss_ldo:
2575 dwc3_ssusb_ldo_enable(0);
2576free_ss_ldo_init:
2577 dwc3_ssusb_ldo_init(0);
2578disable_ss_vddcx:
2579 regulator_disable(context->ssusb_vddcx);
2580unconfig_ss_vddcx:
2581 dwc3_ssusb_config_vddcx(0);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002582disable_ref_clk:
2583 clk_disable_unprepare(msm->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002584disable_utmi_clk:
2585 clk_disable_unprepare(msm->utmi_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002586disable_sleep_a_clk:
2587 clk_disable_unprepare(msm->hsphy_sleep_clk);
2588disable_sleep_clk:
2589 clk_disable_unprepare(msm->sleep_clk);
2590disable_iface_clk:
2591 clk_disable_unprepare(msm->iface_clk);
Manu Gautam1742db22012-06-19 13:33:24 +05302592disable_core_clk:
2593 clk_disable_unprepare(msm->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302594disable_xo:
2595 clk_disable_unprepare(msm->xo_clk);
2596put_xo:
2597 clk_put(msm->xo_clk);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002598
2599 return ret;
2600}
2601
2602static int __devexit dwc3_msm_remove(struct platform_device *pdev)
2603{
2604 struct dwc3_msm *msm = platform_get_drvdata(pdev);
2605
Jack Pham0fc12332012-11-19 13:14:22 -08002606 if (msm->id_adc_detect)
2607 qpnp_adc_tm_usbid_end();
Manu Gautamb5067272012-07-02 09:53:41 +05302608 if (dwc3_debugfs_root)
2609 debugfs_remove_recursive(dwc3_debugfs_root);
Manu Gautam8c642812012-06-07 10:35:10 +05302610 if (msm->otg_xceiv) {
2611 dwc3_start_chg_det(&msm->charger, false);
2612 usb_put_transceiver(msm->otg_xceiv);
2613 }
Jack Pham0fc12332012-11-19 13:14:22 -08002614
Manu Gautamb5067272012-07-02 09:53:41 +05302615 pm_runtime_disable(msm->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05302616 wake_lock_destroy(&msm->wlock);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002617
Manu Gautam60e01352012-05-29 09:00:34 +05302618 dwc3_hsusb_ldo_enable(0);
2619 dwc3_hsusb_ldo_init(0);
2620 regulator_disable(msm->hsusb_vddcx);
2621 dwc3_hsusb_config_vddcx(0);
2622 dwc3_ssusb_ldo_enable(0);
2623 dwc3_ssusb_ldo_init(0);
2624 regulator_disable(msm->ssusb_vddcx);
2625 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05302626 clk_disable_unprepare(msm->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002627 clk_disable_unprepare(msm->iface_clk);
2628 clk_disable_unprepare(msm->sleep_clk);
2629 clk_disable_unprepare(msm->hsphy_sleep_clk);
2630 clk_disable_unprepare(msm->ref_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302631 clk_disable_unprepare(msm->xo_clk);
2632 clk_put(msm->xo_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05302633
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002634 return 0;
2635}
2636
Manu Gautamb5067272012-07-02 09:53:41 +05302637static int dwc3_msm_pm_suspend(struct device *dev)
2638{
2639 int ret = 0;
2640 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2641
2642 dev_dbg(dev, "dwc3-msm PM suspend\n");
2643
Manu Gautam8d98a572013-01-21 16:34:50 +05302644 flush_delayed_work_sync(&mdwc->resume_work);
2645 if (!atomic_read(&mdwc->in_lpm)) {
2646 dev_err(mdwc->dev, "Abort PM suspend!! (USB is outside LPM)\n");
2647 return -EBUSY;
2648 }
2649
Manu Gautamb5067272012-07-02 09:53:41 +05302650 ret = dwc3_msm_suspend(mdwc);
2651 if (!ret)
2652 atomic_set(&mdwc->pm_suspended, 1);
2653
2654 return ret;
2655}
2656
2657static int dwc3_msm_pm_resume(struct device *dev)
2658{
2659 int ret = 0;
2660 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2661
2662 dev_dbg(dev, "dwc3-msm PM resume\n");
2663
2664 atomic_set(&mdwc->pm_suspended, 0);
2665 if (mdwc->resume_pending) {
2666 mdwc->resume_pending = false;
2667
2668 ret = dwc3_msm_resume(mdwc);
2669 /* Update runtime PM status */
2670 pm_runtime_disable(dev);
2671 pm_runtime_set_active(dev);
2672 pm_runtime_enable(dev);
2673
2674 /* Let OTG know about resume event and update pm_count */
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302675 if (mdwc->otg_xceiv) {
Manu Gautamb5067272012-07-02 09:53:41 +05302676 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
2677 DWC3_EVENT_PHY_RESUME);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302678 if (mdwc->ext_xceiv.otg_capability)
2679 mdwc->ext_xceiv.notify_ext_events(
2680 mdwc->otg_xceiv->otg,
2681 DWC3_EVENT_XCEIV_STATE);
2682 }
Manu Gautamb5067272012-07-02 09:53:41 +05302683 }
2684
2685 return ret;
2686}
2687
2688static int dwc3_msm_runtime_idle(struct device *dev)
2689{
2690 dev_dbg(dev, "DWC3-msm runtime idle\n");
2691
2692 return 0;
2693}
2694
2695static int dwc3_msm_runtime_suspend(struct device *dev)
2696{
2697 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2698
2699 dev_dbg(dev, "DWC3-msm runtime suspend\n");
2700
2701 return dwc3_msm_suspend(mdwc);
2702}
2703
2704static int dwc3_msm_runtime_resume(struct device *dev)
2705{
2706 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2707
2708 dev_dbg(dev, "DWC3-msm runtime resume\n");
2709
2710 return dwc3_msm_resume(mdwc);
2711}
2712
2713static const struct dev_pm_ops dwc3_msm_dev_pm_ops = {
2714 SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume)
2715 SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume,
2716 dwc3_msm_runtime_idle)
2717};
2718
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002719static const struct of_device_id of_dwc3_matach[] = {
2720 {
2721 .compatible = "qcom,dwc-usb3-msm",
2722 },
2723 { },
2724};
2725MODULE_DEVICE_TABLE(of, of_dwc3_matach);
2726
2727static struct platform_driver dwc3_msm_driver = {
2728 .probe = dwc3_msm_probe,
2729 .remove = __devexit_p(dwc3_msm_remove),
2730 .driver = {
2731 .name = "msm-dwc3",
Manu Gautamb5067272012-07-02 09:53:41 +05302732 .pm = &dwc3_msm_dev_pm_ops,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002733 .of_match_table = of_dwc3_matach,
2734 },
2735};
2736
Manu Gautam377821c2012-09-28 16:53:24 +05302737MODULE_LICENSE("GPL v2");
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002738MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
2739
2740static int __devinit dwc3_msm_init(void)
2741{
2742 return platform_driver_register(&dwc3_msm_driver);
2743}
2744module_init(dwc3_msm_init);
2745
2746static void __exit dwc3_msm_exit(void)
2747{
2748 platform_driver_unregister(&dwc3_msm_driver);
2749}
2750module_exit(dwc3_msm_exit);