blob: beba33f495be50c425759fcdfffc5a8fac102f31 [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>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030029#include <linux/list.h>
Manu Gautamb5067272012-07-02 09:53:41 +053030#include <linux/debugfs.h>
31#include <linux/uaccess.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030032#include <linux/usb/ch9.h>
33#include <linux/usb/gadget.h>
34#include <linux/usb/msm_hsusb.h>
Manu Gautam60e01352012-05-29 09:00:34 +053035#include <linux/regulator/consumer.h>
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +053036#include <linux/power_supply.h>
Jack Pham0fc12332012-11-19 13:14:22 -080037#include <linux/qpnp/qpnp-adc.h>
Manu Gautam60e01352012-05-29 09:00:34 +053038
39#include <mach/rpm-regulator.h>
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +053040#include <mach/rpm-regulator-smd.h>
Manu Gautam2617deb2012-08-31 17:50:06 -070041#include <mach/msm_bus.h>
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053042#include <mach/clk.h>
Ido Shayevitz9fb83452012-04-01 17:45:58 +030043
Manu Gautam8c642812012-06-07 10:35:10 +053044#include "dwc3_otg.h"
Ido Shayevitz9fb83452012-04-01 17:45:58 +030045#include "core.h"
46#include "gadget.h"
47
Jack Pham0fc12332012-11-19 13:14:22 -080048/* ADC threshold values */
49static int adc_low_threshold = 700;
50module_param(adc_low_threshold, int, S_IRUGO | S_IWUSR);
51MODULE_PARM_DESC(adc_low_threshold, "ADC ID Low voltage threshold");
52
53static int adc_high_threshold = 950;
54module_param(adc_high_threshold, int, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(adc_high_threshold, "ADC ID High voltage threshold");
56
57static int adc_meas_interval = ADC_MEAS1_INTERVAL_1S;
58module_param(adc_meas_interval, int, S_IRUGO | S_IWUSR);
59MODULE_PARM_DESC(adc_meas_interval, "ADC ID polling period");
60
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +053061static int override_phy_init;
62module_param(override_phy_init, int, S_IRUGO|S_IWUSR);
63MODULE_PARM_DESC(override_phy_init, "Override HSPHY Init Seq");
64
Ido Shayevitz9fb83452012-04-01 17:45:58 +030065/**
66 * USB DBM Hardware registers.
67 *
68 */
Shimrit Malichia00d7322012-08-05 13:56:28 +030069#define DBM_BASE 0x000F8000
70#define DBM_EP_CFG(n) (DBM_BASE + (0x00 + 4 * (n)))
71#define DBM_DATA_FIFO(n) (DBM_BASE + (0x10 + 4 * (n)))
72#define DBM_DATA_FIFO_SIZE(n) (DBM_BASE + (0x20 + 4 * (n)))
73#define DBM_DATA_FIFO_EN (DBM_BASE + (0x30))
74#define DBM_GEVNTADR (DBM_BASE + (0x34))
75#define DBM_GEVNTSIZ (DBM_BASE + (0x38))
76#define DBM_DBG_CNFG (DBM_BASE + (0x3C))
77#define DBM_HW_TRB0_EP(n) (DBM_BASE + (0x40 + 4 * (n)))
78#define DBM_HW_TRB1_EP(n) (DBM_BASE + (0x50 + 4 * (n)))
79#define DBM_HW_TRB2_EP(n) (DBM_BASE + (0x60 + 4 * (n)))
80#define DBM_HW_TRB3_EP(n) (DBM_BASE + (0x70 + 4 * (n)))
81#define DBM_PIPE_CFG (DBM_BASE + (0x80))
82#define DBM_SOFT_RESET (DBM_BASE + (0x84))
83#define DBM_GEN_CFG (DBM_BASE + (0x88))
Ido Shayevitz9fb83452012-04-01 17:45:58 +030084
85/**
86 * USB DBM Hardware registers bitmask.
87 *
88 */
89/* DBM_EP_CFG */
Shimrit Malichia00d7322012-08-05 13:56:28 +030090#define DBM_EN_EP 0x00000001
91#define USB3_EPNUM 0x0000003E
Ido Shayevitz9fb83452012-04-01 17:45:58 +030092#define DBM_BAM_PIPE_NUM 0x000000C0
93#define DBM_PRODUCER 0x00000100
94#define DBM_DISABLE_WB 0x00000200
95#define DBM_INT_RAM_ACC 0x00000400
96
97/* DBM_DATA_FIFO_SIZE */
98#define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff
99
100/* DBM_GEVNTSIZ */
101#define DBM_GEVNTSIZ_MASK 0x0000ffff
102
103/* DBM_DBG_CNFG */
104#define DBM_ENABLE_IOC_MASK 0x0000000f
105
106/* DBM_SOFT_RESET */
107#define DBM_SFT_RST_EP0 0x00000001
108#define DBM_SFT_RST_EP1 0x00000002
109#define DBM_SFT_RST_EP2 0x00000004
110#define DBM_SFT_RST_EP3 0x00000008
Shimrit Malichia00d7322012-08-05 13:56:28 +0300111#define DBM_SFT_RST_EPS_MASK 0x0000000F
112#define DBM_SFT_RST_MASK 0x80000000
113#define DBM_EN_MASK 0x00000002
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200114
115#define DBM_MAX_EPS 4
116
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300117/* DBM TRB configurations */
118#define DBM_TRB_BIT 0x80000000
119#define DBM_TRB_DATA_SRC 0x40000000
120#define DBM_TRB_DMA 0x20000000
121#define DBM_TRB_EP_NUM(ep) (ep<<24)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300122
Manu Gautam8c642812012-06-07 10:35:10 +0530123/**
124 * USB QSCRATCH Hardware registers
125 *
126 */
127#define QSCRATCH_REG_OFFSET (0x000F8800)
Shimrit Malichia00d7322012-08-05 13:56:28 +0300128#define QSCRATCH_GENERAL_CFG (QSCRATCH_REG_OFFSET + 0x08)
Manu Gautambd0e5782012-08-30 10:39:01 -0700129#define HS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x10)
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530130#define PARAMETER_OVERRIDE_X_REG (QSCRATCH_REG_OFFSET + 0x14)
Manu Gautam8c642812012-06-07 10:35:10 +0530131#define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18)
132#define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C)
133#define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20)
134#define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24)
Manu Gautamd4108b72012-12-14 17:35:18 +0530135#define CGCTL_REG (QSCRATCH_REG_OFFSET + 0x28)
Manu Gautambd0e5782012-08-30 10:39:01 -0700136#define SS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x30)
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +0530137#define SS_PHY_PARAM_CTRL_1 (QSCRATCH_REG_OFFSET + 0x34)
138#define SS_PHY_PARAM_CTRL_2 (QSCRATCH_REG_OFFSET + 0x38)
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530139#define SS_CR_PROTOCOL_DATA_IN_REG (QSCRATCH_REG_OFFSET + 0x3C)
140#define SS_CR_PROTOCOL_DATA_OUT_REG (QSCRATCH_REG_OFFSET + 0x40)
141#define SS_CR_PROTOCOL_CAP_ADDR_REG (QSCRATCH_REG_OFFSET + 0x44)
142#define SS_CR_PROTOCOL_CAP_DATA_REG (QSCRATCH_REG_OFFSET + 0x48)
143#define SS_CR_PROTOCOL_READ_REG (QSCRATCH_REG_OFFSET + 0x4C)
144#define SS_CR_PROTOCOL_WRITE_REG (QSCRATCH_REG_OFFSET + 0x50)
Manu Gautam8c642812012-06-07 10:35:10 +0530145
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300146struct dwc3_msm_req_complete {
147 struct list_head list_item;
148 struct usb_request *req;
149 void (*orig_complete)(struct usb_ep *ep,
150 struct usb_request *req);
151};
152
Ido Shayevitzef72ddd2012-03-28 18:55:55 +0200153struct dwc3_msm {
154 struct platform_device *dwc3;
155 struct device *dev;
156 void __iomem *base;
157 u32 resource_size;
158 int dbm_num_eps;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300159 u8 ep_num_mapping[DBM_MAX_EPS];
160 const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM];
161 struct list_head req_complete_list;
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +0530162 struct clk *xo_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700163 struct clk *ref_clk;
Manu Gautam1742db22012-06-19 13:33:24 +0530164 struct clk *core_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -0700165 struct clk *iface_clk;
166 struct clk *sleep_clk;
167 struct clk *hsphy_sleep_clk;
Jack Pham22698b82013-02-13 17:45:06 -0800168 struct clk *utmi_clk;
Manu Gautam60e01352012-05-29 09:00:34 +0530169 struct regulator *hsusb_3p3;
170 struct regulator *hsusb_1p8;
171 struct regulator *hsusb_vddcx;
172 struct regulator *ssusb_1p8;
173 struct regulator *ssusb_vddcx;
Manu Gautamb5067272012-07-02 09:53:41 +0530174 struct dwc3_ext_xceiv ext_xceiv;
175 bool resume_pending;
176 atomic_t pm_suspended;
177 atomic_t in_lpm;
Manu Gautam377821c2012-09-28 16:53:24 +0530178 int hs_phy_irq;
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +0530179 int hsphy_init_seq;
Manu Gautam377821c2012-09-28 16:53:24 +0530180 bool lpm_irq_seen;
Manu Gautamb5067272012-07-02 09:53:41 +0530181 struct delayed_work resume_work;
Manu Gautam6eb13e32013-02-01 15:19:15 +0530182 struct work_struct restart_usb_work;
Manu Gautamb5067272012-07-02 09:53:41 +0530183 struct wake_lock wlock;
Manu Gautam8c642812012-06-07 10:35:10 +0530184 struct dwc3_charger charger;
185 struct usb_phy *otg_xceiv;
186 struct delayed_work chg_work;
187 enum usb_chg_state chg_state;
Jack Pham0fc12332012-11-19 13:14:22 -0800188 struct qpnp_adc_tm_usbid_param adc_param;
189 struct delayed_work init_adc_work;
190 bool id_adc_detect;
Manu Gautam8c642812012-06-07 10:35:10 +0530191 u8 dcd_retries;
Manu Gautam2617deb2012-08-31 17:50:06 -0700192 u32 bus_perf_client;
193 struct msm_bus_scale_pdata *bus_scale_table;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530194 struct power_supply usb_psy;
Jack Pham9354c6a2012-12-20 19:19:32 -0800195 struct power_supply *ext_vbus_psy;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530196 unsigned int online;
197 unsigned int host_mode;
198 unsigned int current_max;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +0530199 unsigned int vdd_no_vol_level;
200 unsigned int vdd_low_vol_level;
201 unsigned int vdd_high_vol_level;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +0530202 bool vbus_active;
Jack Phamfadd6432012-12-07 19:03:41 -0800203 bool ext_inuse;
Jack Phamf12b7e12012-12-28 14:27:26 -0800204 enum dwc3_id_state id_state;
Manu Gautam60e01352012-05-29 09:00:34 +0530205};
206
207#define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */
208#define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */
209#define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */
210
211#define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */
212#define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */
213#define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */
214
215#define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */
216#define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */
217#define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */
218
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300219static struct dwc3_msm *context;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +0300220static u64 dwc3_msm_dma_mask = DMA_BIT_MASK(64);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300221
Jack Phamfadd6432012-12-07 19:03:41 -0800222static struct usb_ext_notification *usb_ext;
223
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300224/**
225 *
226 * Read register with debug info.
227 *
228 * @base - DWC3 base virtual address.
229 * @offset - register offset.
230 *
231 * @return u32
232 */
233static inline u32 dwc3_msm_read_reg(void *base, u32 offset)
234{
235 u32 val = ioread32(base + offset);
236 return val;
237}
238
239/**
240 * Read register masked field with debug info.
241 *
242 * @base - DWC3 base virtual address.
243 * @offset - register offset.
244 * @mask - register bitmask.
245 *
246 * @return u32
247 */
248static inline u32 dwc3_msm_read_reg_field(void *base,
249 u32 offset,
250 const u32 mask)
251{
252 u32 shift = find_first_bit((void *)&mask, 32);
253 u32 val = ioread32(base + offset);
254 val &= mask; /* clear other bits */
255 val >>= shift;
256 return val;
257}
258
259/**
260 *
261 * Write register with debug info.
262 *
263 * @base - DWC3 base virtual address.
264 * @offset - register offset.
265 * @val - value to write.
266 *
267 */
268static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val)
269{
270 iowrite32(val, base + offset);
271}
272
273/**
274 * Write register masked field with debug info.
275 *
276 * @base - DWC3 base virtual address.
277 * @offset - register offset.
278 * @mask - register bitmask.
279 * @val - value to write.
280 *
281 */
282static inline void dwc3_msm_write_reg_field(void *base, u32 offset,
283 const u32 mask, u32 val)
284{
285 u32 shift = find_first_bit((void *)&mask, 32);
286 u32 tmp = ioread32(base + offset);
287
288 tmp &= ~mask; /* clear written bits */
289 val = tmp | (val << shift);
290 iowrite32(val, base + offset);
291}
292
293/**
Manu Gautam8c642812012-06-07 10:35:10 +0530294 * Write register and read back masked value to confirm it is written
295 *
296 * @base - DWC3 base virtual address.
297 * @offset - register offset.
298 * @mask - register bitmask specifying what should be updated
299 * @val - value to write.
300 *
301 */
302static inline void dwc3_msm_write_readback(void *base, u32 offset,
303 const u32 mask, u32 val)
304{
305 u32 write_val, tmp = ioread32(base + offset);
306
307 tmp &= ~mask; /* retain other bits */
308 write_val = tmp | val;
309
310 iowrite32(write_val, base + offset);
311
312 /* Read back to see if val was written */
313 tmp = ioread32(base + offset);
314 tmp &= mask; /* clear other bits */
315
316 if (tmp != val)
317 dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n",
318 __func__, val, offset);
319}
320
321/**
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +0530322 *
323 * Write SSPHY register with debug info.
324 *
325 * @base - DWC3 base virtual address.
326 * @addr - SSPHY address to write.
327 * @val - value to write.
328 *
329 */
330static void dwc3_msm_ssusb_write_phycreg(void *base, u32 addr, u32 val)
331{
332 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
333 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
334 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
335 cpu_relax();
336
337 iowrite32(val, base + SS_CR_PROTOCOL_DATA_IN_REG);
338 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_DATA_REG);
339 while (ioread32(base + SS_CR_PROTOCOL_CAP_DATA_REG))
340 cpu_relax();
341
342 iowrite32(0x1, base + SS_CR_PROTOCOL_WRITE_REG);
343 while (ioread32(base + SS_CR_PROTOCOL_WRITE_REG))
344 cpu_relax();
345}
346
347/**
348 *
349 * Read SSPHY register with debug info.
350 *
351 * @base - DWC3 base virtual address.
352 * @addr - SSPHY address to read.
353 *
354 */
355static u32 dwc3_msm_ssusb_read_phycreg(void *base, u32 addr)
356{
357 iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG);
358 iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG);
359 while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG))
360 cpu_relax();
361
362 iowrite32(0x1, base + SS_CR_PROTOCOL_READ_REG);
363 while (ioread32(base + SS_CR_PROTOCOL_READ_REG))
364 cpu_relax();
365
366 return ioread32(base + SS_CR_PROTOCOL_DATA_OUT_REG);
367}
368
369/**
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300370 * Return DBM EP number according to usb endpoint number.
371 *
372 */
373static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep)
374{
375 int i;
376
377 for (i = 0; i < context->dbm_num_eps; i++)
378 if (context->ep_num_mapping[i] == usb_ep)
379 return i;
380
381 return -ENODEV; /* Not found */
382}
383
384/**
385 * Return number of configured DBM endpoints.
386 *
387 */
388static int dwc3_msm_configured_dbm_ep_num(void)
389{
390 int i;
391 int count = 0;
392
393 for (i = 0; i < context->dbm_num_eps; i++)
394 if (context->ep_num_mapping[i])
395 count++;
396
397 return count;
398}
399
400/**
401 * Configure the DBM with the USB3 core event buffer.
402 * This function is called by the SNPS UDC upon initialization.
403 *
404 * @addr - address of the event buffer.
405 * @size - size of the event buffer.
406 *
407 */
408static int dwc3_msm_event_buffer_config(u32 addr, u16 size)
409{
410 dev_dbg(context->dev, "%s\n", __func__);
411
412 dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr);
413 dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ,
414 DBM_GEVNTSIZ_MASK, size);
415
416 return 0;
417}
418
419/**
420 * Reset the DBM registers upon initialization.
421 *
422 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300423static int dwc3_msm_dbm_soft_reset(int enter_reset)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300424{
425 dev_dbg(context->dev, "%s\n", __func__);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300426 if (enter_reset) {
427 dev_dbg(context->dev, "enter DBM reset\n");
428 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
429 DBM_SFT_RST_MASK, 1);
430 } else {
431 dev_dbg(context->dev, "exit DBM reset\n");
432 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
433 DBM_SFT_RST_MASK, 0);
434 /*enable DBM*/
435 dwc3_msm_write_reg_field(context->base, QSCRATCH_GENERAL_CFG,
436 DBM_EN_MASK, 0x1);
437 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300438
439 return 0;
440}
441
442/**
443 * Soft reset specific DBM ep.
444 * This function is called by the function driver upon events
445 * such as transfer aborting, USB re-enumeration and USB
446 * disconnection.
447 *
448 * @dbm_ep - DBM ep number.
449 * @enter_reset - should we enter a reset state or get out of it.
450 *
451 */
452static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset)
453{
454 dev_dbg(context->dev, "%s\n", __func__);
455
456 if (dbm_ep >= context->dbm_num_eps) {
457 dev_err(context->dev,
458 "%s: Invalid DBM ep index\n", __func__);
459 return -ENODEV;
460 }
461
462 if (enter_reset) {
463 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300464 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300465 } else {
466 dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300467 DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300468 }
469
470 return 0;
471}
472
473/**
474 * Configure a USB DBM ep to work in BAM mode.
475 *
476 *
477 * @usb_ep - USB physical EP number.
478 * @producer - producer/consumer.
479 * @disable_wb - disable write back to system memory.
480 * @internal_mem - use internal USB memory for data fifo.
481 * @ioc - enable interrupt on completion.
482 *
483 * @return int - DBM ep number.
484 */
485static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe,
486 bool producer, bool disable_wb,
487 bool internal_mem, bool ioc)
488{
489 u8 dbm_ep;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300490 u32 ep_cfg;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300491
492 dev_dbg(context->dev, "%s\n", __func__);
493
Shimrit Malichia00d7322012-08-05 13:56:28 +0300494 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
495
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300496 if (dbm_ep < 0) {
Shimrit Malichia00d7322012-08-05 13:56:28 +0300497 dev_err(context->dev,
498 "%s: Invalid usb ep index\n", __func__);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300499 return -ENODEV;
500 }
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300501 /* First, reset the dbm endpoint */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300502 dwc3_msm_dbm_ep_soft_reset(dbm_ep, 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300503
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300504 /* Set ioc bit for dbm_ep if needed */
505 dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG,
Shimrit Malichia00d7322012-08-05 13:56:28 +0300506 DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300507
Shimrit Malichia00d7322012-08-05 13:56:28 +0300508 ep_cfg = (producer ? DBM_PRODUCER : 0) |
509 (disable_wb ? DBM_DISABLE_WB : 0) |
510 (internal_mem ? DBM_INT_RAM_ACC : 0);
511
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300512 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
Shimrit Malichia00d7322012-08-05 13:56:28 +0300513 DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8);
514
515 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM,
516 usb_ep);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300517 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep),
518 DBM_BAM_PIPE_NUM, bam_pipe);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300519 dwc3_msm_write_reg_field(context->base, DBM_PIPE_CFG, 0x000000ff,
520 0xe4);
521 dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP,
522 1);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300523
524 return dbm_ep;
525}
526
527/**
528 * Configure a USB DBM ep to work in normal mode.
529 *
530 * @usb_ep - USB ep number.
531 *
532 */
533static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep)
534{
535 u8 dbm_ep;
536
537 dev_dbg(context->dev, "%s\n", __func__);
538
539 dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep);
540
541 if (dbm_ep < 0) {
542 dev_err(context->dev,
543 "%s: Invalid usb ep index\n", __func__);
544 return -ENODEV;
545 }
546
547 context->ep_num_mapping[dbm_ep] = 0;
548
549 dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), 0);
550
551 /* Reset the dbm endpoint */
552 dwc3_msm_dbm_ep_soft_reset(dbm_ep, true);
553
554 return 0;
555}
556
557/**
558 * Configure the DBM with the BAM's data fifo.
559 * This function is called by the USB BAM Driver
560 * upon initialization.
561 *
562 * @ep - pointer to usb endpoint.
563 * @addr - address of data fifo.
564 * @size - size of data fifo.
565 *
566 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300567int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx)
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300568{
569 u8 dbm_ep;
570 struct dwc3_ep *dep = to_dwc3_ep(ep);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300571 u8 bam_pipe = dst_pipe_idx;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300572
573 dev_dbg(context->dev, "%s\n", __func__);
574
Shimrit Malichia00d7322012-08-05 13:56:28 +0300575 dbm_ep = bam_pipe;
576 context->ep_num_mapping[dbm_ep] = dep->number;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300577
578 dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr);
579 dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep),
580 DBM_DATA_FIFO_SIZE_MASK, size);
581
582 return 0;
583}
584
585/**
586* Cleanups for msm endpoint on request complete.
587*
588* Also call original request complete.
589*
590* @usb_ep - pointer to usb_ep instance.
591* @request - pointer to usb_request instance.
592*
593* @return int - 0 on success, negetive on error.
594*/
595static void dwc3_msm_req_complete_func(struct usb_ep *ep,
596 struct usb_request *request)
597{
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300598 struct dwc3_ep *dep = to_dwc3_ep(ep);
599 struct dwc3_msm_req_complete *req_complete = NULL;
600
601 /* Find original request complete function and remove it from list */
602 list_for_each_entry(req_complete,
603 &context->req_complete_list,
604 list_item) {
605 if (req_complete->req == request)
606 break;
607 }
608 if (!req_complete || req_complete->req != request) {
609 dev_err(dep->dwc->dev, "%s: could not find the request\n",
610 __func__);
611 return;
612 }
613 list_del(&req_complete->list_item);
614
615 /*
616 * Release another one TRB to the pool since DBM queue took 2 TRBs
617 * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback
618 * released only one.
619 */
Manu Gautam55d34222012-12-19 16:49:47 +0530620 dep->busy_slot++;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300621
622 /* Unconfigure dbm ep */
623 dwc3_msm_dbm_ep_unconfig(dep->number);
624
625 /*
626 * If this is the last endpoint we unconfigured, than reset also
627 * the event buffers.
628 */
629 if (0 == dwc3_msm_configured_dbm_ep_num())
630 dwc3_msm_event_buffer_config(0, 0);
631
632 /*
633 * Call original complete function, notice that dwc->lock is already
634 * taken by the caller of this function (dwc3_gadget_giveback()).
635 */
636 request->complete = req_complete->orig_complete;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300637 if (request->complete)
638 request->complete(ep, request);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300639
640 kfree(req_complete);
641}
642
643/**
644* Helper function.
645* See the header of the dwc3_msm_ep_queue function.
646*
647* @dwc3_ep - pointer to dwc3_ep instance.
648* @req - pointer to dwc3_request instance.
649*
650* @return int - 0 on success, negetive on error.
651*/
652static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
653{
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300654 struct dwc3_trb *trb;
655 struct dwc3_trb *trb_link;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300656 struct dwc3_gadget_ep_cmd_params params;
657 u32 cmd;
658 int ret = 0;
659
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300660 /* We push the request to the dep->req_queued list to indicate that
661 * this request is issued with start transfer. The request will be out
662 * from this list in 2 cases. The first is that the transfer will be
663 * completed (not if the transfer is endless using a circular TRBs with
664 * with link TRB). The second case is an option to do stop stransfer,
665 * this can be initiated by the function driver when calling dequeue.
666 */
667 req->queued = true;
668 list_add_tail(&req->list, &dep->req_queued);
669
670 /* First, prepare a normal TRB, point to the fake buffer */
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300671 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300672 dep->free_slot++;
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300673 memset(trb, 0, sizeof(*trb));
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300674
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300675 req->trb = trb;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300676 trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300677 trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length);
678 trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300679 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300680
681 /* Second, prepare a Link TRB that points to the first TRB*/
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300682 trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300683 dep->free_slot++;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300684 memset(trb_link, 0, sizeof *trb_link);
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300685
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300686 trb_link->bpl = lower_32_bits(req->trb_dma);
Shimrit Malichia00d7322012-08-05 13:56:28 +0300687 trb_link->bph = DBM_TRB_BIT |
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300688 DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number);
689 trb_link->size = 0;
690 trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300691
692 /*
693 * Now start the transfer
694 */
695 memset(&params, 0, sizeof(params));
Shimrit Malichia00d7322012-08-05 13:56:28 +0300696 params.param0 = 0; /* TDAddr High */
697 params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */
698
Manu Gautam5b2bf9a2012-10-18 10:52:50 +0530699 /* DBM requires IOC to be set */
700 cmd = DWC3_DEPCMD_STARTTRANSFER | DWC3_DEPCMD_CMDIOC;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300701 ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, &params);
702 if (ret < 0) {
703 dev_dbg(dep->dwc->dev,
704 "%s: failed to send STARTTRANSFER command\n",
705 __func__);
706
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300707 list_del(&req->list);
708 return ret;
709 }
Manu Gautam4a51a062012-12-07 11:24:39 +0530710 dep->flags |= DWC3_EP_BUSY;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300711
712 return ret;
713}
714
715/**
716* Queue a usb request to the DBM endpoint.
717* This function should be called after the endpoint
718* was enabled by the ep_enable.
719*
720* This function prepares special structure of TRBs which
721* is familier with the DBM HW, so it will possible to use
722* this endpoint in DBM mode.
723*
724* The TRBs prepared by this function, is one normal TRB
725* which point to a fake buffer, followed by a link TRB
726* that points to the first TRB.
727*
728* The API of this function follow the regular API of
729* usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h).
730*
731* @usb_ep - pointer to usb_ep instance.
732* @request - pointer to usb_request instance.
733* @gfp_flags - possible flags.
734*
735* @return int - 0 on success, negetive on error.
736*/
737static int dwc3_msm_ep_queue(struct usb_ep *ep,
738 struct usb_request *request, gfp_t gfp_flags)
739{
740 struct dwc3_request *req = to_dwc3_request(request);
741 struct dwc3_ep *dep = to_dwc3_ep(ep);
742 struct dwc3 *dwc = dep->dwc;
743 struct dwc3_msm_req_complete *req_complete;
744 unsigned long flags;
745 int ret = 0;
746 u8 bam_pipe;
747 bool producer;
748 bool disable_wb;
749 bool internal_mem;
750 bool ioc;
Shimrit Malichia00d7322012-08-05 13:56:28 +0300751 u8 speed;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300752
753 if (!(request->udc_priv & MSM_SPS_MODE)) {
754 /* Not SPS mode, call original queue */
755 dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n",
756 __func__);
757
758 return (context->original_ep_ops[dep->number])->queue(ep,
759 request,
760 gfp_flags);
761 }
762
763 if (!dep->endpoint.desc) {
764 dev_err(dwc->dev,
765 "%s: trying to queue request %p to disabled ep %s\n",
766 __func__, request, ep->name);
767 return -EPERM;
768 }
769
770 if (dep->number == 0 || dep->number == 1) {
771 dev_err(dwc->dev,
772 "%s: trying to queue dbm request %p to control ep %s\n",
773 __func__, request, ep->name);
774 return -EPERM;
775 }
776
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300777
Manu Gautam4a51a062012-12-07 11:24:39 +0530778 if (dep->busy_slot != dep->free_slot || !list_empty(&dep->request_list)
779 || !list_empty(&dep->req_queued)) {
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300780 dev_err(dwc->dev,
781 "%s: trying to queue dbm request %p tp ep %s\n",
782 __func__, request, ep->name);
783 return -EPERM;
Manu Gautam4a51a062012-12-07 11:24:39 +0530784 } else {
785 dep->busy_slot = 0;
786 dep->free_slot = 0;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300787 }
788
789 /*
790 * Override req->complete function, but before doing that,
791 * store it's original pointer in the req_complete_list.
792 */
793 req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL);
794 if (!req_complete) {
795 dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__);
796 return -ENOMEM;
797 }
798 req_complete->req = request;
799 req_complete->orig_complete = request->complete;
800 list_add_tail(&req_complete->list_item, &context->req_complete_list);
801 request->complete = dwc3_msm_req_complete_func;
802
803 /*
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300804 * Configure the DBM endpoint
805 */
Shimrit Malichia00d7322012-08-05 13:56:28 +0300806 bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK;
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300807 producer = ((request->udc_priv & MSM_PRODUCER) ? true : false);
808 disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false);
809 internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false);
810 ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false);
811
812 ret = dwc3_msm_dbm_ep_config(dep->number,
813 bam_pipe, producer,
814 disable_wb, internal_mem, ioc);
815 if (ret < 0) {
816 dev_err(context->dev,
817 "error %d after calling dwc3_msm_dbm_ep_config\n",
818 ret);
819 return ret;
820 }
821
822 dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n",
823 __func__, request, ep->name, request->length);
824
825 /*
826 * We must obtain the lock of the dwc3 core driver,
827 * including disabling interrupts, so we will be sure
828 * that we are the only ones that configure the HW device
829 * core and ensure that we queuing the request will finish
830 * as soon as possible so we will release back the lock.
831 */
832 spin_lock_irqsave(&dwc->lock, flags);
833 ret = __dwc3_msm_ep_queue(dep, req);
834 spin_unlock_irqrestore(&dwc->lock, flags);
835 if (ret < 0) {
836 dev_err(context->dev,
837 "error %d after calling __dwc3_msm_ep_queue\n", ret);
838 return ret;
839 }
840
Shimrit Malichia00d7322012-08-05 13:56:28 +0300841 speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD;
842 dwc3_msm_write_reg(context->base, DBM_GEN_CFG, speed >> 2);
843
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300844 return 0;
845}
846
847/**
848 * Configure MSM endpoint.
849 * This function do specific configurations
850 * to an endpoint which need specific implementaion
851 * in the MSM architecture.
852 *
853 * This function should be called by usb function/class
854 * layer which need a support from the specific MSM HW
855 * which wrap the USB3 core. (like DBM specific endpoints)
856 *
857 * @ep - a pointer to some usb_ep instance
858 *
859 * @return int - 0 on success, negetive on error.
860 */
861int msm_ep_config(struct usb_ep *ep)
862{
863 struct dwc3_ep *dep = to_dwc3_ep(ep);
864 struct usb_ep_ops *new_ep_ops;
865
Manu Gautama302f612012-12-18 17:33:06 +0530866 dwc3_msm_event_buffer_config(dwc3_msm_read_reg(context->base,
867 DWC3_GEVNTADRLO(0)),
868 dwc3_msm_read_reg(context->base, DWC3_GEVNTSIZ(0)));
869
Ido Shayevitz9fb83452012-04-01 17:45:58 +0300870 /* Save original ep ops for future restore*/
871 if (context->original_ep_ops[dep->number]) {
872 dev_err(context->dev,
873 "ep [%s,%d] already configured as msm endpoint\n",
874 ep->name, dep->number);
875 return -EPERM;
876 }
877 context->original_ep_ops[dep->number] = ep->ops;
878
879 /* Set new usb ops as we like */
880 new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL);
881 if (!new_ep_ops) {
882 dev_err(context->dev,
883 "%s: unable to allocate mem for new usb ep ops\n",
884 __func__);
885 return -ENOMEM;
886 }
887 (*new_ep_ops) = (*ep->ops);
888 new_ep_ops->queue = dwc3_msm_ep_queue;
889 ep->ops = new_ep_ops;
890
891 /*
892 * Do HERE more usb endpoint configurations
893 * which are specific to MSM.
894 */
895
896 return 0;
897}
898EXPORT_SYMBOL(msm_ep_config);
899
900/**
901 * Un-configure MSM endpoint.
902 * Tear down configurations done in the
903 * dwc3_msm_ep_config function.
904 *
905 * @ep - a pointer to some usb_ep instance
906 *
907 * @return int - 0 on success, negetive on error.
908 */
909int msm_ep_unconfig(struct usb_ep *ep)
910{
911 struct dwc3_ep *dep = to_dwc3_ep(ep);
912 struct usb_ep_ops *old_ep_ops;
913
914 /* Restore original ep ops */
915 if (!context->original_ep_ops[dep->number]) {
916 dev_err(context->dev,
917 "ep [%s,%d] was not configured as msm endpoint\n",
918 ep->name, dep->number);
919 return -EINVAL;
920 }
921 old_ep_ops = (struct usb_ep_ops *)ep->ops;
922 ep->ops = context->original_ep_ops[dep->number];
923 context->original_ep_ops[dep->number] = NULL;
924 kfree(old_ep_ops);
925
926 /*
927 * Do HERE more usb endpoint un-configurations
928 * which are specific to MSM.
929 */
930
931 return 0;
932}
933EXPORT_SYMBOL(msm_ep_unconfig);
934
Manu Gautam6eb13e32013-02-01 15:19:15 +0530935static void dwc3_restart_usb_work(struct work_struct *w)
936{
937 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
938 restart_usb_work);
939
940 dev_dbg(mdwc->dev, "%s\n", __func__);
941
942 if (atomic_read(&mdwc->in_lpm) || !mdwc->otg_xceiv) {
943 dev_err(mdwc->dev, "%s failed!!!\n", __func__);
944 return;
945 }
946
947 if (!mdwc->ext_xceiv.bsv) {
948 dev_dbg(mdwc->dev, "%s bailing out in disconnect\n", __func__);
949 return;
950 }
951
952 /* Reset active USB connection */
953 mdwc->ext_xceiv.bsv = false;
954 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
955 /* Make sure disconnect is processed before sending connect */
956 flush_delayed_work(&mdwc->resume_work);
957
958 mdwc->ext_xceiv.bsv = true;
959 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
960}
961
962/**
963 * Reset USB peripheral connection
964 * Inform OTG for Vbus LOW followed by Vbus HIGH notification.
965 * This performs full hardware reset and re-initialization which
966 * might be required by some DBM client driver during uninit/cleanup.
967 */
968void msm_dwc3_restart_usb_session(void)
969{
970 struct dwc3_msm *mdwc = context;
971
972 dev_dbg(mdwc->dev, "%s\n", __func__);
973 queue_work(system_nrt_wq, &mdwc->restart_usb_work);
974
975 return;
976}
977EXPORT_SYMBOL(msm_dwc3_restart_usb_session);
978
Jack Phamfadd6432012-12-07 19:03:41 -0800979/**
980 * msm_register_usb_ext_notification: register for event notification
981 * @info: pointer to client usb_ext_notification structure. May be NULL.
982 *
983 * @return int - 0 on success, negative on error
984 */
985int msm_register_usb_ext_notification(struct usb_ext_notification *info)
986{
987 pr_debug("%s usb_ext: %p\n", __func__, info);
988
989 if (info) {
990 if (usb_ext) {
991 pr_err("%s: already registered\n", __func__);
992 return -EEXIST;
993 }
994
995 if (!info->notify) {
996 pr_err("%s: notify is NULL\n", __func__);
997 return -EINVAL;
998 }
999 }
1000
1001 usb_ext = info;
1002 return 0;
1003}
1004EXPORT_SYMBOL(msm_register_usb_ext_notification);
1005
Manu Gautam60e01352012-05-29 09:00:34 +05301006/* HSPHY */
1007static int dwc3_hsusb_config_vddcx(int high)
1008{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301009 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301010 struct dwc3_msm *dwc = context;
Manu Gautam60e01352012-05-29 09:00:34 +05301011
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301012 max_vol = dwc->vdd_high_vol_level;
1013 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301014 ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol);
1015 if (ret) {
1016 dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n");
1017 return ret;
1018 }
1019
1020 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1021 min_vol, max_vol);
1022
1023 return ret;
1024}
1025
1026static int dwc3_hsusb_ldo_init(int init)
1027{
1028 int rc = 0;
1029 struct dwc3_msm *dwc = context;
1030
1031 if (!init) {
1032 regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX);
1033 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1034 return 0;
1035 }
1036
1037 dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3");
1038 if (IS_ERR(dwc->hsusb_3p3)) {
1039 dev_err(dwc->dev, "unable to get hsusb 3p3\n");
1040 return PTR_ERR(dwc->hsusb_3p3);
1041 }
1042
1043 rc = regulator_set_voltage(dwc->hsusb_3p3,
1044 USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX);
1045 if (rc) {
1046 dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n");
1047 return rc;
1048 }
1049 dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8");
1050 if (IS_ERR(dwc->hsusb_1p8)) {
1051 dev_err(dwc->dev, "unable to get hsusb 1p8\n");
1052 rc = PTR_ERR(dwc->hsusb_1p8);
1053 goto devote_3p3;
1054 }
1055 rc = regulator_set_voltage(dwc->hsusb_1p8,
1056 USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX);
1057 if (rc) {
1058 dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n");
1059 goto devote_3p3;
1060 }
1061
1062 return 0;
1063
1064devote_3p3:
1065 regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX);
1066
1067 return rc;
1068}
1069
1070static int dwc3_hsusb_ldo_enable(int on)
1071{
1072 int rc = 0;
1073 struct dwc3_msm *dwc = context;
1074
1075 dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1076
1077 if (!on)
1078 goto disable_regulators;
1079
1080
1081 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD);
1082 if (rc < 0) {
1083 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n");
1084 return rc;
1085 }
1086
1087 rc = regulator_enable(dwc->hsusb_1p8);
1088 if (rc) {
1089 dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n");
1090 goto put_1p8_lpm;
1091 }
1092
1093 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD);
1094 if (rc < 0) {
1095 dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n");
1096 goto disable_1p8;
1097 }
1098
1099 rc = regulator_enable(dwc->hsusb_3p3);
1100 if (rc) {
1101 dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n");
1102 goto put_3p3_lpm;
1103 }
1104
1105 return 0;
1106
1107disable_regulators:
1108 rc = regulator_disable(dwc->hsusb_3p3);
1109 if (rc)
1110 dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n");
1111
1112put_3p3_lpm:
1113 rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0);
1114 if (rc < 0)
1115 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n");
1116
1117disable_1p8:
1118 rc = regulator_disable(dwc->hsusb_1p8);
1119 if (rc)
1120 dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n");
1121
1122put_1p8_lpm:
1123 rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0);
1124 if (rc < 0)
1125 dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n");
1126
1127 return rc < 0 ? rc : 0;
1128}
1129
1130/* SSPHY */
1131static int dwc3_ssusb_config_vddcx(int high)
1132{
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301133 int min_vol, max_vol, ret;
Manu Gautam60e01352012-05-29 09:00:34 +05301134 struct dwc3_msm *dwc = context;
Manu Gautam60e01352012-05-29 09:00:34 +05301135
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05301136 max_vol = dwc->vdd_high_vol_level;
1137 min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level;
Manu Gautam60e01352012-05-29 09:00:34 +05301138 ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol);
1139 if (ret) {
1140 dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n");
1141 return ret;
1142 }
1143
1144 dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__,
1145 min_vol, max_vol);
1146 return ret;
1147}
1148
1149/* 3.3v supply not needed for SS PHY */
1150static int dwc3_ssusb_ldo_init(int init)
1151{
1152 int rc = 0;
1153 struct dwc3_msm *dwc = context;
1154
1155 if (!init) {
1156 regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX);
1157 return 0;
1158 }
1159
1160 dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8");
1161 if (IS_ERR(dwc->ssusb_1p8)) {
1162 dev_err(dwc->dev, "unable to get ssusb 1p8\n");
1163 return PTR_ERR(dwc->ssusb_1p8);
1164 }
1165 rc = regulator_set_voltage(dwc->ssusb_1p8,
1166 USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX);
1167 if (rc)
1168 dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n");
1169
1170 return rc;
1171}
1172
1173static int dwc3_ssusb_ldo_enable(int on)
1174{
1175 int rc = 0;
1176 struct dwc3_msm *dwc = context;
1177
1178 dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM");
1179
1180 if (!on)
1181 goto disable_regulators;
1182
1183
1184 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD);
1185 if (rc < 0) {
1186 dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n");
1187 return rc;
1188 }
1189
1190 rc = regulator_enable(dwc->ssusb_1p8);
1191 if (rc) {
1192 dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n");
1193 goto put_1p8_lpm;
1194 }
1195
1196 return 0;
1197
1198disable_regulators:
1199 rc = regulator_disable(dwc->ssusb_1p8);
1200 if (rc)
1201 dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n");
1202
1203put_1p8_lpm:
1204 rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0);
1205 if (rc < 0)
1206 dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n");
1207
1208 return rc < 0 ? rc : 0;
1209}
1210
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301211static int dwc3_msm_link_clk_reset(bool assert)
1212{
1213 int ret = 0;
1214 struct dwc3_msm *mdwc = context;
1215
1216 if (assert) {
1217 /* Using asynchronous block reset to the hardware */
1218 dev_dbg(mdwc->dev, "block_reset ASSERT\n");
1219 clk_disable_unprepare(mdwc->ref_clk);
1220 clk_disable_unprepare(mdwc->iface_clk);
1221 clk_disable_unprepare(mdwc->core_clk);
1222 ret = clk_reset(mdwc->core_clk, CLK_RESET_ASSERT);
1223 if (ret)
1224 dev_err(mdwc->dev, "dwc3 core_clk assert failed\n");
1225 } else {
1226 dev_dbg(mdwc->dev, "block_reset DEASSERT\n");
1227 ret = clk_reset(mdwc->core_clk, CLK_RESET_DEASSERT);
1228 ndelay(200);
1229 clk_prepare_enable(mdwc->core_clk);
1230 clk_prepare_enable(mdwc->ref_clk);
1231 clk_prepare_enable(mdwc->iface_clk);
1232 if (ret)
1233 dev_err(mdwc->dev, "dwc3 core_clk deassert failed\n");
1234 }
1235
1236 return ret;
1237}
1238
1239/* Initialize QSCRATCH registers for HSPHY and SSPHY operation */
1240static void dwc3_msm_qscratch_reg_init(struct dwc3_msm *msm)
1241{
1242 u32 data = 0;
1243
1244 /* SSPHY Initialization: Use ref_clk from pads and set its parameters */
1245 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1246 msleep(30);
1247 /* Assert SSPHY reset */
1248 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210082);
1249 usleep_range(2000, 2200);
1250 /* De-assert SSPHY reset - power and ref_clock must be ON */
1251 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002);
1252 usleep_range(2000, 2200);
1253 /* Ref clock must be stable now, enable ref clock for HS mode */
1254 dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210102);
1255 usleep_range(2000, 2200);
1256 /*
1257 * HSPHY Initialization: Enable UTMI clock and clamp enable HVINTs,
1258 * and disable RETENTION (power-on default is ENABLED)
1259 */
1260 dwc3_msm_write_reg(msm->base, HS_PHY_CTRL_REG, 0x5220bb2);
1261 usleep_range(2000, 2200);
1262 /* Disable (bypass) VBUS and ID filters */
1263 dwc3_msm_write_reg(msm->base, QSCRATCH_GENERAL_CFG, 0x78);
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05301264 /*
1265 * write HSPHY init value to QSCRATCH reg to set HSPHY parameters like
1266 * VBUS valid threshold, disconnect valid threshold, DC voltage level,
1267 * preempasis and rise/fall time.
1268 */
1269 if (override_phy_init)
1270 msm->hsphy_init_seq = override_phy_init;
1271 if (msm->hsphy_init_seq)
1272 dwc3_msm_write_readback(msm->base,
1273 PARAMETER_OVERRIDE_X_REG, 0x03FFFFFF,
1274 msm->hsphy_init_seq & 0x03FFFFFF);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301275
Manu Gautamd4108b72012-12-14 17:35:18 +05301276 /* Enable master clock for RAMs to allow BAM to access RAMs when
1277 * RAM clock gating is enabled via DWC3's GCTL. Otherwise, issues
1278 * are seen where RAM clocks get turned OFF in SS mode
1279 */
1280 dwc3_msm_write_reg(msm->base, CGCTL_REG,
1281 dwc3_msm_read_reg(msm->base, CGCTL_REG) | 0x18);
1282
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301283 /*
1284 * WORKAROUND: There is SSPHY suspend bug due to which USB enumerates
1285 * in HS mode instead of SS mode. Workaround it by asserting
1286 * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus mode
1287 */
1288 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x102D);
1289 data |= (1 << 7);
1290 dwc3_msm_ssusb_write_phycreg(msm->base, 0x102D, data);
1291
1292 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1010);
1293 data &= ~0xFF0;
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301294 data |= 0x20;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301295 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1010, data);
Vijayavardhan Vennapusa164b0f42013-01-17 19:33:53 +05301296
1297 /*
1298 * Fix RX Equalization setting as follows
1299 * LANE0.RX_OVRD_IN_HI. RX_EQ_EN set to 0
1300 * LANE0.RX_OVRD_IN_HI.RX_EQ_EN_OVRD set to 1
1301 * LANE0.RX_OVRD_IN_HI.RX_EQ set to 3
1302 * LANE0.RX_OVRD_IN_HI.RX_EQ_OVRD set to 1
1303 */
1304 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1006);
1305 data &= ~(1 << 6);
1306 data |= (1 << 7);
1307 data &= ~(0x7 << 8);
1308 data |= (0x3 << 8);
1309 data |= (0x1 << 11);
1310 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1006, data);
1311
1312 /*
1313 * Set EQ and TX launch amplitudes as follows
1314 * LANE0.TX_OVRD_DRV_LO.PREEMPH set to 22
1315 * LANE0.TX_OVRD_DRV_LO.AMPLITUDE set to 127
1316 * LANE0.TX_OVRD_DRV_LO.EN set to 1.
1317 */
1318 data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1002);
1319 data &= ~0x3F80;
1320 data |= (0x16 << 7);
1321 data &= ~0x7F;
1322 data |= (0x7F | (1 << 14));
1323 dwc3_msm_ssusb_write_phycreg(msm->base, 0x1002, data);
1324
1325 /* Set LOS_BIAS to 0x5 */
1326 dwc3_msm_write_readback(msm->base, SS_PHY_PARAM_CTRL_1, 0x07, 0x5);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301327}
1328
1329static void dwc3_msm_block_reset(void)
1330{
1331 struct dwc3_msm *mdwc = context;
1332 int ret = 0;
1333
1334 ret = dwc3_msm_link_clk_reset(1);
1335 if (ret)
1336 return;
1337
1338 usleep_range(1000, 1200);
1339 ret = dwc3_msm_link_clk_reset(0);
1340 if (ret)
1341 return;
1342
1343 usleep_range(10000, 12000);
1344
1345 /* Reinitialize QSCRATCH registers after block reset */
1346 dwc3_msm_qscratch_reg_init(mdwc);
Manu Gautama302f612012-12-18 17:33:06 +05301347
1348 /* Reset the DBM */
1349 dwc3_msm_dbm_soft_reset(1);
1350 usleep_range(1000, 1200);
1351 dwc3_msm_dbm_soft_reset(0);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301352}
1353
Manu Gautam8c642812012-06-07 10:35:10 +05301354static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc)
1355{
1356 u32 chg_ctrl;
1357
1358 /* Turn off VDP_SRC */
1359 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1360 msleep(20);
1361
1362 /* Before proceeding make sure VDP_SRC is OFF */
1363 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1364 if (chg_ctrl & 0x3F)
1365 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1366 __func__, chg_ctrl);
1367 /*
1368 * Configure DM as current source, DP as current sink
1369 * and enable battery charging comparators.
1370 */
1371 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34);
1372}
1373
Manu Gautama1e331d2013-02-07 14:55:05 +05301374static bool dwc3_chg_det_check_linestate(struct dwc3_msm *mdwc)
1375{
1376 u32 chg_det;
1377 bool ret = false;
1378
1379 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1380 ret = chg_det & (3 << 8);
1381
1382 return ret;
1383}
1384
Manu Gautam8c642812012-06-07 10:35:10 +05301385static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc)
1386{
1387 u32 chg_det;
1388 bool ret = false;
1389
1390 chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1391 ret = chg_det & 1;
1392
1393 return ret;
1394}
1395
1396static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc)
1397{
1398 /*
1399 * Configure DP as current source, DM as current sink
1400 * and enable battery charging comparators.
1401 */
1402 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30);
1403}
1404
1405static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc)
1406{
1407 u32 chg_state;
1408 bool ret = false;
1409
1410 chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG);
1411 ret = chg_state & 2;
1412
1413 return ret;
1414}
1415
1416static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc)
1417{
1418 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0);
1419}
1420
1421static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc)
1422{
1423 /* Data contact detection enable, DCDENB */
1424 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2);
1425}
1426
1427static void dwc3_chg_block_reset(struct dwc3_msm *mdwc)
1428{
1429 u32 chg_ctrl;
1430
1431 /* Clear charger detecting control bits */
1432 dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0);
1433
1434 /* Clear alt interrupt latch and enable bits */
1435 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1436 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0);
1437
1438 udelay(100);
1439
1440 /* Before proceeding make sure charger block is RESET */
1441 chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG);
1442 if (chg_ctrl & 0x3F)
1443 dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n",
1444 __func__, chg_ctrl);
1445}
1446
1447static const char *chg_to_string(enum dwc3_chg_type chg_type)
1448{
1449 switch (chg_type) {
Manu Gautama1e331d2013-02-07 14:55:05 +05301450 case DWC3_SDP_CHARGER: return "USB_SDP_CHARGER";
1451 case DWC3_DCP_CHARGER: return "USB_DCP_CHARGER";
1452 case DWC3_CDP_CHARGER: return "USB_CDP_CHARGER";
1453 case DWC3_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Manu Gautam8c642812012-06-07 10:35:10 +05301454 default: return "INVALID_CHARGER";
1455 }
1456}
1457
1458#define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1459#define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1460#define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1461#define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
1462
1463static void dwc3_chg_detect_work(struct work_struct *w)
1464{
1465 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work);
1466 bool is_dcd = false, tmout, vout;
1467 unsigned long delay;
1468
1469 dev_dbg(mdwc->dev, "chg detection work\n");
1470 switch (mdwc->chg_state) {
1471 case USB_CHG_STATE_UNDEFINED:
1472 dwc3_chg_block_reset(mdwc);
1473 dwc3_chg_enable_dcd(mdwc);
1474 mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1475 mdwc->dcd_retries = 0;
1476 delay = DWC3_CHG_DCD_POLL_TIME;
1477 break;
1478 case USB_CHG_STATE_WAIT_FOR_DCD:
1479 is_dcd = dwc3_chg_check_dcd(mdwc);
1480 tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES;
1481 if (is_dcd || tmout) {
1482 dwc3_chg_disable_dcd(mdwc);
Manu Gautama1e331d2013-02-07 14:55:05 +05301483 if (dwc3_chg_det_check_linestate(mdwc)) {
1484 dev_dbg(mdwc->dev, "proprietary charger\n");
1485 mdwc->charger.chg_type =
1486 DWC3_PROPRIETARY_CHARGER;
1487 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1488 delay = 0;
1489 break;
1490 }
Manu Gautam8c642812012-06-07 10:35:10 +05301491 dwc3_chg_enable_primary_det(mdwc);
1492 delay = DWC3_CHG_PRIMARY_DET_TIME;
1493 mdwc->chg_state = USB_CHG_STATE_DCD_DONE;
1494 } else {
1495 delay = DWC3_CHG_DCD_POLL_TIME;
1496 }
1497 break;
1498 case USB_CHG_STATE_DCD_DONE:
1499 vout = dwc3_chg_det_check_output(mdwc);
1500 if (vout) {
1501 dwc3_chg_enable_secondary_det(mdwc);
1502 delay = DWC3_CHG_SECONDARY_DET_TIME;
1503 mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1504 } else {
Manu Gautama1e331d2013-02-07 14:55:05 +05301505 mdwc->charger.chg_type = DWC3_SDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301506 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1507 delay = 0;
1508 }
1509 break;
1510 case USB_CHG_STATE_PRIMARY_DONE:
1511 vout = dwc3_chg_det_check_output(mdwc);
1512 if (vout)
Manu Gautama1e331d2013-02-07 14:55:05 +05301513 mdwc->charger.chg_type = DWC3_DCP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301514 else
Manu Gautama1e331d2013-02-07 14:55:05 +05301515 mdwc->charger.chg_type = DWC3_CDP_CHARGER;
Manu Gautam8c642812012-06-07 10:35:10 +05301516 mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1517 /* fall through */
1518 case USB_CHG_STATE_SECONDARY_DONE:
1519 mdwc->chg_state = USB_CHG_STATE_DETECTED;
1520 /* fall through */
1521 case USB_CHG_STATE_DETECTED:
1522 dwc3_chg_block_reset(mdwc);
Manu Gautama48296e2012-12-05 17:37:56 +05301523 /* Enable VDP_SRC */
1524 if (mdwc->charger.chg_type == DWC3_DCP_CHARGER)
1525 dwc3_msm_write_readback(mdwc->base,
1526 CHARGING_DET_CTRL_REG, 0x1F, 0x10);
Manu Gautam8c642812012-06-07 10:35:10 +05301527 dev_dbg(mdwc->dev, "chg_type = %s\n",
1528 chg_to_string(mdwc->charger.chg_type));
1529 mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg,
1530 &mdwc->charger);
1531 return;
1532 default:
1533 return;
1534 }
1535
1536 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay);
1537}
1538
1539static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start)
1540{
1541 struct dwc3_msm *mdwc = context;
1542
1543 if (start == false) {
Jack Pham9354c6a2012-12-20 19:19:32 -08001544 dev_dbg(mdwc->dev, "canceling charging detection work\n");
Manu Gautam8c642812012-06-07 10:35:10 +05301545 cancel_delayed_work_sync(&mdwc->chg_work);
1546 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1547 charger->chg_type = DWC3_INVALID_CHARGER;
1548 return;
1549 }
1550
1551 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1552 charger->chg_type = DWC3_INVALID_CHARGER;
1553 queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0);
1554}
1555
Manu Gautamb5067272012-07-02 09:53:41 +05301556static int dwc3_msm_suspend(struct dwc3_msm *mdwc)
1557{
Manu Gautam2617deb2012-08-31 17:50:06 -07001558 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301559 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301560 bool host_bus_suspend;
Manu Gautam2617deb2012-08-31 17:50:06 -07001561
Manu Gautamb5067272012-07-02 09:53:41 +05301562 dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__);
1563
1564 if (atomic_read(&mdwc->in_lpm)) {
1565 dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__);
1566 return 0;
1567 }
1568
Manu Gautama48296e2012-12-05 17:37:56 +05301569 if (mdwc->hs_phy_irq)
1570 disable_irq(mdwc->hs_phy_irq);
1571
Manu Gautam98013c22012-11-20 17:42:42 +05301572 if (cancel_delayed_work_sync(&mdwc->chg_work))
1573 dev_dbg(mdwc->dev, "%s: chg_work was pending\n", __func__);
1574 if (mdwc->chg_state != USB_CHG_STATE_DETECTED) {
1575 /* charger detection wasn't complete; re-init flags */
1576 mdwc->chg_state = USB_CHG_STATE_UNDEFINED;
1577 mdwc->charger.chg_type = DWC3_INVALID_CHARGER;
Manu Gautama48296e2012-12-05 17:37:56 +05301578 dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG,
1579 0x37, 0x0);
Manu Gautam98013c22012-11-20 17:42:42 +05301580 }
1581
Manu Gautama48296e2012-12-05 17:37:56 +05301582 dcp = mdwc->charger.chg_type == DWC3_DCP_CHARGER;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301583 host_bus_suspend = mdwc->host_mode == 1;
Manu Gautam377821c2012-09-28 16:53:24 +05301584
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301585 /* Sequence to put SSPHY in low power state:
1586 * 1. Clear REF_SS_PHY_EN in SS_PHY_CTRL_REG
1587 * 2. Clear REF_USE_PAD in SS_PHY_CTRL_REG
1588 * 3. Set TEST_POWERED_DOWN in SS_PHY_CTRL_REG to enable PHY retention
1589 * 4. Disable SSPHY ref clk
1590 */
1591 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8), 0x0);
1592 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28), 0x0);
1593 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26),
1594 (1 << 26));
1595
Manu Gautam377821c2012-09-28 16:53:24 +05301596 usleep_range(1000, 1200);
Manu Gautam3e9ad352012-08-16 14:44:47 -07001597 clk_disable_unprepare(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301598
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301599 if (host_bus_suspend) {
1600 /* Sequence for host bus suspend case:
1601 * 1. Set suspend and sleep bits in GUSB2PHYCONFIG reg
1602 * 2. Clear interrupt latch register and enable BSV, ID HV intr
1603 * 3. Enable DP and DM HV interrupts in ALT_INTERRUPT_EN_REG
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301604 */
1605 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1606 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1607 0x00000140);
1608 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1609 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1610 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1611 0x18000, 0x18000);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301612 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0xFC0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301613 udelay(5);
1614 } else {
1615 /* Sequence to put hardware in low power state:
1616 * 1. Set OTGDISABLE to disable OTG block in HSPHY (saves power)
1617 * 2. Clear charger detection control fields (performed above)
1618 * 3. SUSPEND PHY and turn OFF core clock after some delay
1619 * 4. Clear interrupt latch register and enable BSV, ID HV intr
1620 * 5. Enable PHY retention
1621 */
1622 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x1000,
1623 0x1000);
1624 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1625 0xC00000, 0x800000);
1626 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF);
1627 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1628 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1629 0x18000, 0x18000);
1630 if (!dcp)
1631 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1632 0x2, 0x0);
1633 }
Manu Gautam377821c2012-09-28 16:53:24 +05301634
1635 /* make sure above writes are completed before turning off clocks */
1636 wmb();
1637 clk_disable_unprepare(mdwc->core_clk);
1638 clk_disable_unprepare(mdwc->iface_clk);
1639
Jack Pham22698b82013-02-13 17:45:06 -08001640 if (!host_bus_suspend) {
1641 clk_disable_unprepare(mdwc->utmi_clk);
1642
1643 /* USB PHY no more requires TCXO */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301644 clk_disable_unprepare(mdwc->xo_clk);
Jack Pham22698b82013-02-13 17:45:06 -08001645 }
Manu Gautamb5067272012-07-02 09:53:41 +05301646
Manu Gautam2617deb2012-08-31 17:50:06 -07001647 if (mdwc->bus_perf_client) {
1648 ret = msm_bus_scale_client_update_request(
1649 mdwc->bus_perf_client, 0);
1650 if (ret)
1651 dev_err(mdwc->dev, "Failed to reset bus bw vote\n");
1652 }
1653
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301654 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1655 !host_bus_suspend)
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301656 dwc3_hsusb_ldo_enable(0);
1657
Vijayavardhan Vennapusa6bc06962012-10-31 13:23:38 +05301658 dwc3_ssusb_ldo_enable(0);
1659 dwc3_ssusb_config_vddcx(0);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301660 if (!host_bus_suspend)
1661 dwc3_hsusb_config_vddcx(0);
Manu Gautam377821c2012-09-28 16:53:24 +05301662 wake_unlock(&mdwc->wlock);
Manu Gautamb5067272012-07-02 09:53:41 +05301663 atomic_set(&mdwc->in_lpm, 1);
Manu Gautam377821c2012-09-28 16:53:24 +05301664
Manu Gautamb5067272012-07-02 09:53:41 +05301665 dev_info(mdwc->dev, "DWC3 in low power mode\n");
1666
Manu Gautama48296e2012-12-05 17:37:56 +05301667 if (mdwc->hs_phy_irq)
1668 enable_irq(mdwc->hs_phy_irq);
1669
Manu Gautamb5067272012-07-02 09:53:41 +05301670 return 0;
1671}
1672
1673static int dwc3_msm_resume(struct dwc3_msm *mdwc)
1674{
Manu Gautam2617deb2012-08-31 17:50:06 -07001675 int ret;
Manu Gautama48296e2012-12-05 17:37:56 +05301676 bool dcp;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301677 bool host_bus_suspend;
Manu Gautam2617deb2012-08-31 17:50:06 -07001678
Manu Gautamb5067272012-07-02 09:53:41 +05301679 dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__);
1680
1681 if (!atomic_read(&mdwc->in_lpm)) {
1682 dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__);
1683 return 0;
1684 }
1685
Manu Gautam377821c2012-09-28 16:53:24 +05301686 wake_lock(&mdwc->wlock);
1687
Manu Gautam2617deb2012-08-31 17:50:06 -07001688 if (mdwc->bus_perf_client) {
1689 ret = msm_bus_scale_client_update_request(
1690 mdwc->bus_perf_client, 1);
1691 if (ret)
1692 dev_err(mdwc->dev, "Failed to vote for bus scaling\n");
1693 }
1694
Manu Gautama48296e2012-12-05 17:37:56 +05301695 dcp = mdwc->charger.chg_type == DWC3_DCP_CHARGER;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301696 host_bus_suspend = mdwc->host_mode == 1;
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301697
1698 if (!host_bus_suspend) {
1699 /* Vote for TCXO while waking up USB HSPHY */
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05301700 ret = clk_prepare_enable(mdwc->xo_clk);
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301701 if (ret)
1702 dev_err(mdwc->dev, "%s failed to vote TCXO buffer%d\n",
1703 __func__, ret);
1704 }
1705
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301706 if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp &&
1707 !host_bus_suspend)
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301708 dwc3_hsusb_ldo_enable(1);
1709
Vijayavardhan Vennapusa6bc06962012-10-31 13:23:38 +05301710 dwc3_ssusb_ldo_enable(1);
1711 dwc3_ssusb_config_vddcx(1);
Jack Pham22698b82013-02-13 17:45:06 -08001712
1713 if (!host_bus_suspend) {
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301714 dwc3_hsusb_config_vddcx(1);
Jack Pham22698b82013-02-13 17:45:06 -08001715 clk_prepare_enable(mdwc->utmi_clk);
1716 }
1717
Manu Gautam3e9ad352012-08-16 14:44:47 -07001718 clk_prepare_enable(mdwc->ref_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301719 usleep_range(1000, 1200);
1720
Manu Gautam3e9ad352012-08-16 14:44:47 -07001721 clk_prepare_enable(mdwc->iface_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05301722 clk_prepare_enable(mdwc->core_clk);
1723
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301724 if (host_bus_suspend) {
1725 /* Disable HV interrupt */
1726 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1727 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1728 0x18000, 0x0);
1729 /* Clear interrupt latch register */
1730 dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301731
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301732 /* Disable DP and DM HV interrupt */
1733 dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x000);
Manu Gautam377821c2012-09-28 16:53:24 +05301734
Vijayavardhan Vennapusa98bccc52013-01-24 13:07:34 +05301735 /* Clear suspend bit in GUSB2PHYCONFIG register */
1736 dwc3_msm_write_readback(mdwc->base, DWC3_GUSB2PHYCFG(0),
1737 0x40, 0x0);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05301738 } else {
1739 /* Disable HV interrupt */
1740 if (mdwc->otg_xceiv && (!mdwc->ext_xceiv.otg_capability))
1741 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG,
1742 0x18000, 0x0);
1743 /* Disable Retention */
1744 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x2, 0x2);
1745
1746 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1747 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) |
1748 0xF0000000);
1749 /* 10usec delay required before de-asserting PHY RESET */
1750 udelay(10);
1751 dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0),
1752 dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) &
1753 0x7FFFFFFF);
1754
1755 /* Bring PHY out of suspend */
1756 dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0xC00000,
1757 0x0);
1758
1759 }
Manu Gautamb5067272012-07-02 09:53:41 +05301760
Vijayavardhan Vennapusa4188de22012-11-06 15:20:18 +05301761 /* Assert SS PHY RESET */
1762 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7),
1763 (1 << 7));
1764 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28),
1765 (1 << 28));
1766 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8),
1767 (1 << 8));
1768 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26), 0x0);
1769 /* 10usec delay required before de-asserting SS PHY RESET */
1770 udelay(10);
1771 dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7), 0x0);
1772
Manu Gautamb5067272012-07-02 09:53:41 +05301773 atomic_set(&mdwc->in_lpm, 0);
Manu Gautam377821c2012-09-28 16:53:24 +05301774
1775 /* match disable_irq call from isr */
1776 if (mdwc->lpm_irq_seen && mdwc->hs_phy_irq) {
1777 enable_irq(mdwc->hs_phy_irq);
1778 mdwc->lpm_irq_seen = false;
1779 }
1780
Manu Gautamb5067272012-07-02 09:53:41 +05301781 dev_info(mdwc->dev, "DWC3 exited from low power mode\n");
1782
1783 return 0;
1784}
1785
1786static void dwc3_resume_work(struct work_struct *w)
1787{
1788 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
1789 resume_work.work);
1790
1791 dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__);
1792 /* handle any event that was queued while work was already running */
1793 if (!atomic_read(&mdwc->in_lpm)) {
1794 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1795 if (mdwc->otg_xceiv)
1796 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1797 DWC3_EVENT_XCEIV_STATE);
1798 return;
1799 }
1800
1801 /* bail out if system resume in process, else initiate RESUME */
1802 if (atomic_read(&mdwc->pm_suspended)) {
1803 mdwc->resume_pending = true;
1804 } else {
1805 pm_runtime_get_sync(mdwc->dev);
1806 if (mdwc->otg_xceiv)
1807 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1808 DWC3_EVENT_PHY_RESUME);
1809 pm_runtime_put_sync(mdwc->dev);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301810 if (mdwc->otg_xceiv && (mdwc->ext_xceiv.otg_capability))
1811 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1812 DWC3_EVENT_XCEIV_STATE);
Manu Gautamb5067272012-07-02 09:53:41 +05301813 }
1814}
1815
Jack Pham0fc12332012-11-19 13:14:22 -08001816static u32 debug_id = true, debug_bsv, debug_connect;
Manu Gautamb5067272012-07-02 09:53:41 +05301817
1818static int dwc3_connect_show(struct seq_file *s, void *unused)
1819{
1820 if (debug_connect)
1821 seq_printf(s, "true\n");
1822 else
1823 seq_printf(s, "false\n");
1824
1825 return 0;
1826}
1827
1828static int dwc3_connect_open(struct inode *inode, struct file *file)
1829{
1830 return single_open(file, dwc3_connect_show, inode->i_private);
1831}
1832
1833static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf,
1834 size_t count, loff_t *ppos)
1835{
1836 struct seq_file *s = file->private_data;
1837 struct dwc3_msm *mdwc = s->private;
1838 char buf[8];
1839
1840 memset(buf, 0x00, sizeof(buf));
1841
1842 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1843 return -EFAULT;
1844
1845 if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) {
1846 debug_connect = true;
1847 } else {
1848 debug_connect = debug_bsv = false;
1849 debug_id = true;
1850 }
1851
1852 mdwc->ext_xceiv.bsv = debug_bsv;
1853 mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND;
1854
1855 if (atomic_read(&mdwc->in_lpm)) {
1856 dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__);
1857 dwc3_resume_work(&mdwc->resume_work.work);
1858 } else {
1859 dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__);
1860 if (mdwc->otg_xceiv)
1861 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
1862 DWC3_EVENT_XCEIV_STATE);
1863 }
1864
1865 return count;
1866}
1867
1868const struct file_operations dwc3_connect_fops = {
1869 .open = dwc3_connect_open,
1870 .read = seq_read,
1871 .write = dwc3_connect_write,
1872 .llseek = seq_lseek,
1873 .release = single_release,
1874};
1875
1876static struct dentry *dwc3_debugfs_root;
1877
1878static void dwc3_debugfs_init(struct dwc3_msm *mdwc)
1879{
1880 dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL);
1881
1882 if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root))
1883 return;
1884
1885 if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05301886 &debug_id))
Manu Gautamb5067272012-07-02 09:53:41 +05301887 goto error;
1888
1889 if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root,
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05301890 &debug_bsv))
Manu Gautamb5067272012-07-02 09:53:41 +05301891 goto error;
1892
1893 if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR,
1894 dwc3_debugfs_root, mdwc, &dwc3_connect_fops))
1895 goto error;
1896
1897 return;
1898
1899error:
1900 debugfs_remove_recursive(dwc3_debugfs_root);
1901}
Manu Gautam8c642812012-06-07 10:35:10 +05301902
Manu Gautam377821c2012-09-28 16:53:24 +05301903static irqreturn_t msm_dwc3_irq(int irq, void *data)
1904{
1905 struct dwc3_msm *mdwc = data;
1906
1907 if (atomic_read(&mdwc->in_lpm)) {
1908 dev_dbg(mdwc->dev, "%s received in LPM\n", __func__);
1909 mdwc->lpm_irq_seen = true;
1910 disable_irq_nosync(irq);
1911 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
1912 } else {
1913 pr_info_ratelimited("%s: IRQ outside LPM\n", __func__);
1914 }
1915
1916 return IRQ_HANDLED;
1917}
1918
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301919static int dwc3_msm_power_get_property_usb(struct power_supply *psy,
1920 enum power_supply_property psp,
1921 union power_supply_propval *val)
1922{
1923 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
1924 usb_psy);
1925 switch (psp) {
1926 case POWER_SUPPLY_PROP_SCOPE:
1927 val->intval = mdwc->host_mode;
1928 break;
1929 case POWER_SUPPLY_PROP_CURRENT_MAX:
1930 val->intval = mdwc->current_max;
1931 break;
1932 case POWER_SUPPLY_PROP_PRESENT:
1933 val->intval = mdwc->vbus_active;
1934 break;
1935 case POWER_SUPPLY_PROP_ONLINE:
1936 val->intval = mdwc->online;
1937 break;
1938 default:
1939 return -EINVAL;
1940 }
1941 return 0;
1942}
1943
1944static int dwc3_msm_power_set_property_usb(struct power_supply *psy,
1945 enum power_supply_property psp,
1946 const union power_supply_propval *val)
1947{
1948 static bool init;
1949 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm,
1950 usb_psy);
1951
1952 switch (psp) {
1953 case POWER_SUPPLY_PROP_SCOPE:
1954 mdwc->host_mode = val->intval;
1955 break;
1956 /* Process PMIC notification in PRESENT prop */
1957 case POWER_SUPPLY_PROP_PRESENT:
1958 dev_dbg(mdwc->dev, "%s: notify xceiv event\n", __func__);
Jack Pham9354c6a2012-12-20 19:19:32 -08001959 if (mdwc->otg_xceiv && !mdwc->ext_inuse &&
1960 (mdwc->ext_xceiv.otg_capability || !init)) {
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301961 mdwc->ext_xceiv.bsv = val->intval;
Manu Gautamf71d9cb2013-02-07 13:52:12 +05301962 queue_delayed_work(system_nrt_wq,
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301963 &mdwc->resume_work, 0);
Jack Pham9354c6a2012-12-20 19:19:32 -08001964
1965 if (!init)
1966 init = true;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301967 }
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05301968 mdwc->vbus_active = val->intval;
1969 break;
1970 case POWER_SUPPLY_PROP_ONLINE:
1971 mdwc->online = val->intval;
1972 break;
1973 case POWER_SUPPLY_PROP_CURRENT_MAX:
1974 mdwc->current_max = val->intval;
1975 break;
1976 default:
1977 return -EINVAL;
1978 }
1979
1980 power_supply_changed(&mdwc->usb_psy);
1981 return 0;
1982}
1983
Jack Pham9354c6a2012-12-20 19:19:32 -08001984static void dwc3_msm_external_power_changed(struct power_supply *psy)
1985{
1986 struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm, usb_psy);
1987 union power_supply_propval ret = {0,};
1988
1989 if (!mdwc->ext_vbus_psy)
1990 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
1991
1992 if (!mdwc->ext_vbus_psy) {
1993 pr_err("%s: Unable to get ext_vbus power_supply\n", __func__);
1994 return;
1995 }
1996
1997 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
1998 POWER_SUPPLY_PROP_ONLINE, &ret);
1999 if (ret.intval) {
2000 dwc3_start_chg_det(&mdwc->charger, false);
2001 mdwc->ext_vbus_psy->get_property(mdwc->ext_vbus_psy,
2002 POWER_SUPPLY_PROP_CURRENT_MAX, &ret);
2003 power_supply_set_current_limit(&mdwc->usb_psy, ret.intval);
2004 }
2005
2006 power_supply_set_online(&mdwc->usb_psy, ret.intval);
2007 power_supply_changed(&mdwc->usb_psy);
2008}
2009
2010
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302011static char *dwc3_msm_pm_power_supplied_to[] = {
2012 "battery",
2013};
2014
2015static enum power_supply_property dwc3_msm_pm_power_props_usb[] = {
2016 POWER_SUPPLY_PROP_PRESENT,
2017 POWER_SUPPLY_PROP_ONLINE,
2018 POWER_SUPPLY_PROP_CURRENT_MAX,
2019 POWER_SUPPLY_PROP_SCOPE,
2020};
2021
Jack Phamfadd6432012-12-07 19:03:41 -08002022static void dwc3_init_adc_work(struct work_struct *w);
2023
2024static void dwc3_ext_notify_online(int on)
2025{
2026 struct dwc3_msm *mdwc = context;
Jack Phamf12b7e12012-12-28 14:27:26 -08002027 bool notify_otg = false;
Jack Phamfadd6432012-12-07 19:03:41 -08002028
2029 if (!mdwc) {
2030 pr_err("%s: DWC3 driver already removed\n", __func__);
2031 return;
2032 }
2033
2034 dev_dbg(mdwc->dev, "notify %s%s\n", on ? "" : "dis", "connected");
2035
Jack Pham9354c6a2012-12-20 19:19:32 -08002036 if (!mdwc->ext_vbus_psy)
2037 mdwc->ext_vbus_psy = power_supply_get_by_name("ext-vbus");
2038
2039 mdwc->ext_inuse = on;
Jack Phamf12b7e12012-12-28 14:27:26 -08002040 if (on) {
2041 /* force OTG to exit B-peripheral state */
2042 mdwc->ext_xceiv.bsv = false;
2043 notify_otg = true;
Jack Pham9354c6a2012-12-20 19:19:32 -08002044 dwc3_start_chg_det(&mdwc->charger, false);
Jack Phamf12b7e12012-12-28 14:27:26 -08002045 } else {
2046 /* external client offline; tell OTG about cached ID/BSV */
2047 if (mdwc->ext_xceiv.id != mdwc->id_state) {
2048 mdwc->ext_xceiv.id = mdwc->id_state;
2049 notify_otg = true;
2050 }
2051
2052 mdwc->ext_xceiv.bsv = mdwc->vbus_active;
2053 notify_otg |= mdwc->vbus_active;
2054 }
Jack Pham9354c6a2012-12-20 19:19:32 -08002055
2056 if (mdwc->ext_vbus_psy)
2057 power_supply_set_present(mdwc->ext_vbus_psy, on);
Jack Phamf12b7e12012-12-28 14:27:26 -08002058
2059 if (notify_otg)
2060 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
Jack Phamfadd6432012-12-07 19:03:41 -08002061}
2062
2063static bool dwc3_ext_trigger_handled(struct dwc3_msm *mdwc,
2064 enum dwc3_id_state id)
2065{
2066 int ret;
2067
2068 if (!usb_ext)
2069 return false;
2070
2071 ret = usb_ext->notify(usb_ext->ctxt, id, dwc3_ext_notify_online);
2072 dev_dbg(mdwc->dev, "%s: external event handler returned %d\n", __func__,
2073 ret);
2074 mdwc->ext_inuse = ret == 0;
2075 return mdwc->ext_inuse;
2076}
2077
Jack Pham0fc12332012-11-19 13:14:22 -08002078static void dwc3_adc_notification(enum qpnp_tm_state state, void *ctx)
2079{
2080 struct dwc3_msm *mdwc = ctx;
2081
2082 if (state >= ADC_TM_STATE_NUM) {
2083 pr_err("%s: invalid notification %d\n", __func__, state);
2084 return;
2085 }
2086
2087 dev_dbg(mdwc->dev, "%s: state = %s\n", __func__,
2088 state == ADC_TM_HIGH_STATE ? "high" : "low");
2089
Jack Pham9354c6a2012-12-20 19:19:32 -08002090 /* Give external client a chance to handle */
2091 if (!mdwc->ext_inuse)
2092 dwc3_ext_trigger_handled(mdwc, (state == ADC_TM_HIGH_STATE));
2093
Jack Phamf12b7e12012-12-28 14:27:26 -08002094 /* save ID state, but don't necessarily notify OTG */
Jack Pham0fc12332012-11-19 13:14:22 -08002095 if (state == ADC_TM_HIGH_STATE) {
Jack Phamf12b7e12012-12-28 14:27:26 -08002096 mdwc->id_state = DWC3_ID_FLOAT;
Jack Pham0fc12332012-11-19 13:14:22 -08002097 mdwc->adc_param.state_request = ADC_TM_LOW_THR_ENABLE;
2098 } else {
Jack Phamf12b7e12012-12-28 14:27:26 -08002099 mdwc->id_state = DWC3_ID_GROUND;
Jack Pham0fc12332012-11-19 13:14:22 -08002100 mdwc->adc_param.state_request = ADC_TM_HIGH_THR_ENABLE;
2101 }
2102
Jack Phamfadd6432012-12-07 19:03:41 -08002103 /* re-arm ADC interrupt */
Jack Pham0fc12332012-11-19 13:14:22 -08002104 qpnp_adc_tm_usbid_configure(&mdwc->adc_param);
Jack Pham9354c6a2012-12-20 19:19:32 -08002105
Jack Phamf12b7e12012-12-28 14:27:26 -08002106 if (!mdwc->ext_inuse) { /* notify OTG */
2107 mdwc->ext_xceiv.id = mdwc->id_state;
Jack Pham9354c6a2012-12-20 19:19:32 -08002108 queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0);
Jack Phamf12b7e12012-12-28 14:27:26 -08002109 }
Jack Pham0fc12332012-11-19 13:14:22 -08002110}
2111
2112static void dwc3_init_adc_work(struct work_struct *w)
2113{
2114 struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm,
2115 init_adc_work.work);
2116 int ret;
2117
2118 ret = qpnp_adc_tm_is_ready();
2119 if (ret == -EPROBE_DEFER) {
Jack Pham90b4d122012-12-13 11:46:22 -08002120 queue_delayed_work(system_nrt_wq, to_delayed_work(w),
2121 msecs_to_jiffies(100));
Jack Pham0fc12332012-11-19 13:14:22 -08002122 return;
2123 }
2124
2125 mdwc->adc_param.low_thr = adc_low_threshold;
2126 mdwc->adc_param.high_thr = adc_high_threshold;
2127 mdwc->adc_param.timer_interval = adc_meas_interval;
2128 mdwc->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2129 mdwc->adc_param.usbid_ctx = mdwc;
2130 mdwc->adc_param.threshold_notification = dwc3_adc_notification;
2131
2132 ret = qpnp_adc_tm_usbid_configure(&mdwc->adc_param);
2133 if (ret) {
2134 dev_err(mdwc->dev, "%s: request ADC error %d\n", __func__, ret);
2135 return;
2136 }
2137
2138 mdwc->id_adc_detect = true;
2139}
2140
2141static ssize_t adc_enable_show(struct device *dev,
2142 struct device_attribute *attr, char *buf)
2143{
2144 return snprintf(buf, PAGE_SIZE, "%s\n", context->id_adc_detect ?
2145 "enabled" : "disabled");
2146}
2147
2148static ssize_t adc_enable_store(struct device *dev,
2149 struct device_attribute *attr, const char
2150 *buf, size_t size)
2151{
2152 if (!strnicmp(buf, "enable", 6)) {
2153 if (!context->id_adc_detect)
2154 dwc3_init_adc_work(&context->init_adc_work.work);
2155 return size;
2156 } else if (!strnicmp(buf, "disable", 7)) {
2157 qpnp_adc_tm_usbid_end();
2158 context->id_adc_detect = false;
2159 return size;
2160 }
2161
2162 return -EINVAL;
2163}
2164
2165static DEVICE_ATTR(adc_enable, S_IRUGO | S_IWUSR, adc_enable_show,
2166 adc_enable_store);
2167
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002168static int __devinit dwc3_msm_probe(struct platform_device *pdev)
2169{
2170 struct device_node *node = pdev->dev.of_node;
2171 struct platform_device *dwc3;
2172 struct dwc3_msm *msm;
2173 struct resource *res;
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002174 void __iomem *tcsr;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002175 int ret = 0;
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302176 int len = 0;
2177 u32 tmp[3];
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002178
2179 msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL);
2180 if (!msm) {
2181 dev_err(&pdev->dev, "not enough memory\n");
2182 return -ENOMEM;
2183 }
2184
2185 platform_set_drvdata(pdev, msm);
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002186 context = msm;
Manu Gautam60e01352012-05-29 09:00:34 +05302187 msm->dev = &pdev->dev;
Ido Shayevitz9fb83452012-04-01 17:45:58 +03002188
2189 INIT_LIST_HEAD(&msm->req_complete_list);
Manu Gautam8c642812012-06-07 10:35:10 +05302190 INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work);
Manu Gautamb5067272012-07-02 09:53:41 +05302191 INIT_DELAYED_WORK(&msm->resume_work, dwc3_resume_work);
Manu Gautam6eb13e32013-02-01 15:19:15 +05302192 INIT_WORK(&msm->restart_usb_work, dwc3_restart_usb_work);
Jack Pham0fc12332012-11-19 13:14:22 -08002193 INIT_DELAYED_WORK(&msm->init_adc_work, dwc3_init_adc_work);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002194
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302195 msm->xo_clk = clk_get(&pdev->dev, "xo");
2196 if (IS_ERR(msm->xo_clk)) {
Manu Gautam377821c2012-09-28 16:53:24 +05302197 dev_err(&pdev->dev, "%s unable to get TCXO buffer handle\n",
2198 __func__);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302199 return PTR_ERR(msm->xo_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05302200 }
2201
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302202 ret = clk_prepare_enable(msm->xo_clk);
Manu Gautam377821c2012-09-28 16:53:24 +05302203 if (ret) {
2204 dev_err(&pdev->dev, "%s failed to vote for TCXO buffer%d\n",
2205 __func__, ret);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302206 goto put_xo;
Manu Gautam377821c2012-09-28 16:53:24 +05302207 }
2208
Manu Gautam1742db22012-06-19 13:33:24 +05302209 /*
2210 * DWC3 Core requires its CORE CLK (aka master / bus clk) to
2211 * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode.
2212 */
2213 msm->core_clk = devm_clk_get(&pdev->dev, "core_clk");
2214 if (IS_ERR(msm->core_clk)) {
2215 dev_err(&pdev->dev, "failed to get core_clk\n");
Manu Gautam377821c2012-09-28 16:53:24 +05302216 ret = PTR_ERR(msm->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302217 goto disable_xo;
Manu Gautam1742db22012-06-19 13:33:24 +05302218 }
2219 clk_set_rate(msm->core_clk, 125000000);
2220 clk_prepare_enable(msm->core_clk);
2221
Manu Gautam3e9ad352012-08-16 14:44:47 -07002222 msm->iface_clk = devm_clk_get(&pdev->dev, "iface_clk");
2223 if (IS_ERR(msm->iface_clk)) {
2224 dev_err(&pdev->dev, "failed to get iface_clk\n");
2225 ret = PTR_ERR(msm->iface_clk);
2226 goto disable_core_clk;
2227 }
2228 clk_prepare_enable(msm->iface_clk);
2229
2230 msm->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2231 if (IS_ERR(msm->sleep_clk)) {
2232 dev_err(&pdev->dev, "failed to get sleep_clk\n");
2233 ret = PTR_ERR(msm->sleep_clk);
2234 goto disable_iface_clk;
2235 }
2236 clk_prepare_enable(msm->sleep_clk);
2237
2238 msm->hsphy_sleep_clk = devm_clk_get(&pdev->dev, "sleep_a_clk");
2239 if (IS_ERR(msm->hsphy_sleep_clk)) {
2240 dev_err(&pdev->dev, "failed to get sleep_a_clk\n");
2241 ret = PTR_ERR(msm->hsphy_sleep_clk);
2242 goto disable_sleep_clk;
2243 }
2244 clk_prepare_enable(msm->hsphy_sleep_clk);
2245
Jack Pham22698b82013-02-13 17:45:06 -08002246 msm->utmi_clk = devm_clk_get(&pdev->dev, "utmi_clk");
2247 if (IS_ERR(msm->utmi_clk)) {
2248 dev_err(&pdev->dev, "failed to get utmi_clk\n");
2249 ret = PTR_ERR(msm->utmi_clk);
2250 goto disable_sleep_a_clk;
2251 }
2252 clk_prepare_enable(msm->utmi_clk);
2253
Manu Gautam3e9ad352012-08-16 14:44:47 -07002254 msm->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
2255 if (IS_ERR(msm->ref_clk)) {
2256 dev_err(&pdev->dev, "failed to get ref_clk\n");
2257 ret = PTR_ERR(msm->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002258 goto disable_utmi_clk;
Manu Gautam3e9ad352012-08-16 14:44:47 -07002259 }
2260 clk_prepare_enable(msm->ref_clk);
2261
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302262 of_get_property(node, "qcom,vdd-voltage-level", &len);
2263 if (len == sizeof(tmp)) {
2264 of_property_read_u32_array(node, "qcom,vdd-voltage-level",
2265 tmp, len/sizeof(*tmp));
2266 msm->vdd_no_vol_level = tmp[0];
2267 msm->vdd_low_vol_level = tmp[1];
2268 msm->vdd_high_vol_level = tmp[2];
2269 } else {
2270 dev_err(&pdev->dev, "no qcom,vdd-voltage-level property\n");
2271 ret = -EINVAL;
2272 goto disable_ref_clk;
2273 }
2274
Manu Gautam60e01352012-05-29 09:00:34 +05302275 /* SS PHY */
Manu Gautam60e01352012-05-29 09:00:34 +05302276 msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig");
2277 if (IS_ERR(msm->ssusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302278 dev_err(&pdev->dev, "unable to get ssusb vddcx\n");
2279 ret = PTR_ERR(msm->ssusb_vddcx);
2280 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302281 }
2282
2283 ret = dwc3_ssusb_config_vddcx(1);
2284 if (ret) {
2285 dev_err(&pdev->dev, "ssusb vddcx configuration failed\n");
Manu Gautam3e9ad352012-08-16 14:44:47 -07002286 goto disable_ref_clk;
Manu Gautam60e01352012-05-29 09:00:34 +05302287 }
2288
2289 ret = regulator_enable(context->ssusb_vddcx);
2290 if (ret) {
2291 dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n");
2292 goto unconfig_ss_vddcx;
2293 }
2294
2295 ret = dwc3_ssusb_ldo_init(1);
2296 if (ret) {
2297 dev_err(&pdev->dev, "ssusb vreg configuration failed\n");
2298 goto disable_ss_vddcx;
2299 }
2300
2301 ret = dwc3_ssusb_ldo_enable(1);
2302 if (ret) {
2303 dev_err(&pdev->dev, "ssusb vreg enable failed\n");
2304 goto free_ss_ldo_init;
2305 }
2306
2307 /* HS PHY */
Manu Gautam60e01352012-05-29 09:00:34 +05302308 msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig");
2309 if (IS_ERR(msm->hsusb_vddcx)) {
Vijayavardhan Vennapusa993798a2012-11-09 15:11:21 +05302310 dev_err(&pdev->dev, "unable to get hsusb vddcx\n");
2311 ret = PTR_ERR(msm->hsusb_vddcx);
2312 goto disable_ss_ldo;
Manu Gautam60e01352012-05-29 09:00:34 +05302313 }
2314
2315 ret = dwc3_hsusb_config_vddcx(1);
2316 if (ret) {
2317 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2318 goto disable_ss_ldo;
2319 }
2320
2321 ret = regulator_enable(context->hsusb_vddcx);
2322 if (ret) {
2323 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
2324 goto unconfig_hs_vddcx;
2325 }
2326
2327 ret = dwc3_hsusb_ldo_init(1);
2328 if (ret) {
2329 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
2330 goto disable_hs_vddcx;
2331 }
2332
2333 ret = dwc3_hsusb_ldo_enable(1);
2334 if (ret) {
2335 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
2336 goto free_hs_ldo_init;
2337 }
2338
Jack Pham0fc12332012-11-19 13:14:22 -08002339 msm->ext_xceiv.id = DWC3_ID_FLOAT;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302340 msm->ext_xceiv.otg_capability = of_property_read_bool(node,
Manu Gautam6c0ff032012-11-02 14:55:35 +05302341 "qcom,otg-capability");
2342 msm->charger.charging_disabled = of_property_read_bool(node,
2343 "qcom,charging-disabled");
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302344
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302345 /*
2346 * DWC3 has separate IRQ line for OTG events (ID/BSV) and for
2347 * DP and DM linestate transitions during low power mode.
2348 */
2349 msm->hs_phy_irq = platform_get_irq_byname(pdev, "hs_phy_irq");
2350 if (msm->hs_phy_irq < 0) {
2351 dev_dbg(&pdev->dev, "pget_irq for hs_phy_irq failed\n");
2352 msm->hs_phy_irq = 0;
Jack Pham0fc12332012-11-19 13:14:22 -08002353 } else {
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05302354 ret = request_irq(msm->hs_phy_irq, msm_dwc3_irq,
2355 IRQF_TRIGGER_RISING, "msm_dwc3", msm);
2356 if (ret) {
2357 dev_err(&pdev->dev, "irqreq HSPHYINT failed\n");
2358 goto disable_hs_ldo;
2359 }
2360 enable_irq_wake(msm->hs_phy_irq);
2361 }
2362 if (msm->ext_xceiv.otg_capability) {
Jack Pham0fc12332012-11-19 13:14:22 -08002363 /* Use ADC for ID pin detection */
2364 queue_delayed_work(system_nrt_wq, &msm->init_adc_work, 0);
2365 device_create_file(&pdev->dev, &dev_attr_adc_enable);
Manu Gautam377821c2012-09-28 16:53:24 +05302366 }
2367
Ido Shayevitz7ad8ded2012-08-28 04:30:58 +03002368 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2369 if (!res) {
2370 dev_dbg(&pdev->dev, "missing TCSR memory resource\n");
2371 } else {
2372 tcsr = devm_ioremap_nocache(&pdev->dev, res->start,
2373 resource_size(res));
2374 if (!tcsr) {
2375 dev_dbg(&pdev->dev, "tcsr ioremap failed\n");
2376 } else {
2377 /* Enable USB3 on the primary USB port. */
2378 writel_relaxed(0x1, tcsr);
2379 /*
2380 * Ensure that TCSR write is completed before
2381 * USB registers initialization.
2382 */
2383 mb();
2384 }
2385 }
2386
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002387 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2388 if (!res) {
2389 dev_err(&pdev->dev, "missing memory base resource\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302390 ret = -ENODEV;
Manu Gautam377821c2012-09-28 16:53:24 +05302391 goto free_hsphy_irq;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002392 }
2393
2394 msm->base = devm_ioremap_nocache(&pdev->dev, res->start,
2395 resource_size(res));
2396 if (!msm->base) {
2397 dev_err(&pdev->dev, "ioremap failed\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302398 ret = -ENODEV;
Manu Gautam377821c2012-09-28 16:53:24 +05302399 goto free_hsphy_irq;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002400 }
2401
Ido Shayevitzca2691e2012-04-17 15:54:53 +03002402 dwc3 = platform_device_alloc("dwc3", -1);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002403 if (!dwc3) {
2404 dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
Manu Gautam60e01352012-05-29 09:00:34 +05302405 ret = -ENODEV;
Manu Gautam377821c2012-09-28 16:53:24 +05302406 goto free_hsphy_irq;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002407 }
2408
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002409 dwc3->dev.parent = &pdev->dev;
Ido Shayevitzc9e92e92012-05-30 14:36:35 +03002410 dwc3->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2411 dwc3->dev.dma_mask = &dwc3_msm_dma_mask;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002412 dwc3->dev.dma_parms = pdev->dev.dma_parms;
2413 msm->resource_size = resource_size(res);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002414 msm->dwc3 = dwc3;
2415
Vijayavardhan Vennapusa26a49602012-12-18 13:51:45 +05302416 if (of_property_read_u32(node, "qcom,dwc-hsphy-init",
2417 &msm->hsphy_init_seq))
2418 dev_dbg(&pdev->dev, "unable to read hsphy init seq\n");
2419 else if (!msm->hsphy_init_seq)
2420 dev_warn(&pdev->dev, "incorrect hsphyinitseq.Using PORvalue\n");
2421
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05302422 dwc3_msm_qscratch_reg_init(msm);
Vijayavardhan Vennapusad81aed32012-12-05 17:30:40 +05302423
Manu Gautamb5067272012-07-02 09:53:41 +05302424 pm_runtime_set_active(msm->dev);
Manu Gautam377821c2012-09-28 16:53:24 +05302425 pm_runtime_enable(msm->dev);
Manu Gautamb5067272012-07-02 09:53:41 +05302426
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002427 if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps",
2428 &msm->dbm_num_eps)) {
2429 dev_err(&pdev->dev,
2430 "unable to read platform data num of dbm eps\n");
2431 msm->dbm_num_eps = DBM_MAX_EPS;
2432 }
2433
2434 if (msm->dbm_num_eps > DBM_MAX_EPS) {
2435 dev_err(&pdev->dev,
2436 "Driver doesn't support number of DBM EPs. "
2437 "max: %d, dbm_num_eps: %d\n",
2438 DBM_MAX_EPS, msm->dbm_num_eps);
2439 ret = -ENODEV;
Manu Gautam60e01352012-05-29 09:00:34 +05302440 goto put_pdev;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002441 }
2442
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302443 msm->usb_psy.name = "usb";
2444 msm->usb_psy.type = POWER_SUPPLY_TYPE_USB;
2445 msm->usb_psy.supplied_to = dwc3_msm_pm_power_supplied_to;
2446 msm->usb_psy.num_supplicants = ARRAY_SIZE(
2447 dwc3_msm_pm_power_supplied_to);
2448 msm->usb_psy.properties = dwc3_msm_pm_power_props_usb;
2449 msm->usb_psy.num_properties = ARRAY_SIZE(dwc3_msm_pm_power_props_usb);
2450 msm->usb_psy.get_property = dwc3_msm_power_get_property_usb;
2451 msm->usb_psy.set_property = dwc3_msm_power_set_property_usb;
Jack Pham9354c6a2012-12-20 19:19:32 -08002452 msm->usb_psy.external_power_changed =
2453 dwc3_msm_external_power_changed;
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302454
2455 ret = power_supply_register(&pdev->dev, &msm->usb_psy);
2456 if (ret < 0) {
2457 dev_err(&pdev->dev,
2458 "%s:power_supply_register usb failed\n",
2459 __func__);
2460 goto put_pdev;
2461 }
2462
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002463 ret = platform_device_add_resources(dwc3, pdev->resource,
2464 pdev->num_resources);
2465 if (ret) {
2466 dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302467 goto put_psupply;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002468 }
2469
2470 ret = platform_device_add(dwc3);
2471 if (ret) {
2472 dev_err(&pdev->dev, "failed to register dwc3 device\n");
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302473 goto put_psupply;
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002474 }
2475
Manu Gautam2617deb2012-08-31 17:50:06 -07002476 msm->bus_scale_table = msm_bus_cl_get_pdata(pdev);
2477 if (!msm->bus_scale_table) {
2478 dev_err(&pdev->dev, "bus scaling is disabled\n");
2479 } else {
2480 msm->bus_perf_client =
2481 msm_bus_scale_register_client(msm->bus_scale_table);
2482 ret = msm_bus_scale_client_update_request(
2483 msm->bus_perf_client, 1);
2484 if (ret)
2485 dev_err(&pdev->dev, "Failed to vote for bus scaling\n");
2486 }
2487
Manu Gautam8c642812012-06-07 10:35:10 +05302488 msm->otg_xceiv = usb_get_transceiver();
2489 if (msm->otg_xceiv) {
2490 msm->charger.start_detection = dwc3_start_chg_det;
2491 ret = dwc3_set_charger(msm->otg_xceiv->otg, &msm->charger);
2492 if (ret || !msm->charger.notify_detection_complete) {
2493 dev_err(&pdev->dev, "failed to register charger: %d\n",
2494 ret);
2495 goto put_xcvr;
2496 }
Manu Gautamb5067272012-07-02 09:53:41 +05302497
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05302498 if (msm->ext_xceiv.otg_capability)
2499 msm->ext_xceiv.ext_block_reset = dwc3_msm_block_reset;
Manu Gautamb5067272012-07-02 09:53:41 +05302500 ret = dwc3_set_ext_xceiv(msm->otg_xceiv->otg, &msm->ext_xceiv);
2501 if (ret || !msm->ext_xceiv.notify_ext_events) {
2502 dev_err(&pdev->dev, "failed to register xceiver: %d\n",
2503 ret);
2504 goto put_xcvr;
2505 }
Manu Gautam8c642812012-06-07 10:35:10 +05302506 } else {
2507 dev_err(&pdev->dev, "%s: No OTG transceiver found\n", __func__);
2508 }
2509
Manu Gautamb5067272012-07-02 09:53:41 +05302510 wake_lock_init(&msm->wlock, WAKE_LOCK_SUSPEND, "msm_dwc3");
2511 wake_lock(&msm->wlock);
2512 dwc3_debugfs_init(msm);
2513
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002514 return 0;
2515
Manu Gautam8c642812012-06-07 10:35:10 +05302516put_xcvr:
2517 usb_put_transceiver(msm->otg_xceiv);
2518 platform_device_del(dwc3);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302519put_psupply:
2520 power_supply_unregister(&msm->usb_psy);
Manu Gautam60e01352012-05-29 09:00:34 +05302521put_pdev:
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002522 platform_device_put(dwc3);
Manu Gautam377821c2012-09-28 16:53:24 +05302523free_hsphy_irq:
2524 if (msm->hs_phy_irq)
2525 free_irq(msm->hs_phy_irq, msm);
Manu Gautam60e01352012-05-29 09:00:34 +05302526disable_hs_ldo:
2527 dwc3_hsusb_ldo_enable(0);
2528free_hs_ldo_init:
2529 dwc3_hsusb_ldo_init(0);
2530disable_hs_vddcx:
2531 regulator_disable(context->hsusb_vddcx);
2532unconfig_hs_vddcx:
2533 dwc3_hsusb_config_vddcx(0);
2534disable_ss_ldo:
2535 dwc3_ssusb_ldo_enable(0);
2536free_ss_ldo_init:
2537 dwc3_ssusb_ldo_init(0);
2538disable_ss_vddcx:
2539 regulator_disable(context->ssusb_vddcx);
2540unconfig_ss_vddcx:
2541 dwc3_ssusb_config_vddcx(0);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002542disable_ref_clk:
2543 clk_disable_unprepare(msm->ref_clk);
Jack Pham22698b82013-02-13 17:45:06 -08002544disable_utmi_clk:
2545 clk_disable_unprepare(msm->utmi_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002546disable_sleep_a_clk:
2547 clk_disable_unprepare(msm->hsphy_sleep_clk);
2548disable_sleep_clk:
2549 clk_disable_unprepare(msm->sleep_clk);
2550disable_iface_clk:
2551 clk_disable_unprepare(msm->iface_clk);
Manu Gautam1742db22012-06-19 13:33:24 +05302552disable_core_clk:
2553 clk_disable_unprepare(msm->core_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302554disable_xo:
2555 clk_disable_unprepare(msm->xo_clk);
2556put_xo:
2557 clk_put(msm->xo_clk);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002558
2559 return ret;
2560}
2561
2562static int __devexit dwc3_msm_remove(struct platform_device *pdev)
2563{
2564 struct dwc3_msm *msm = platform_get_drvdata(pdev);
2565
Jack Pham0fc12332012-11-19 13:14:22 -08002566 if (msm->id_adc_detect)
2567 qpnp_adc_tm_usbid_end();
Manu Gautamb5067272012-07-02 09:53:41 +05302568 if (dwc3_debugfs_root)
2569 debugfs_remove_recursive(dwc3_debugfs_root);
Manu Gautam8c642812012-06-07 10:35:10 +05302570 if (msm->otg_xceiv) {
2571 dwc3_start_chg_det(&msm->charger, false);
2572 usb_put_transceiver(msm->otg_xceiv);
2573 }
Jack Pham0fc12332012-11-19 13:14:22 -08002574
Manu Gautamb5067272012-07-02 09:53:41 +05302575 pm_runtime_disable(msm->dev);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002576 platform_device_unregister(msm->dwc3);
Manu Gautamb5067272012-07-02 09:53:41 +05302577 wake_lock_destroy(&msm->wlock);
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002578
Manu Gautam60e01352012-05-29 09:00:34 +05302579 dwc3_hsusb_ldo_enable(0);
2580 dwc3_hsusb_ldo_init(0);
2581 regulator_disable(msm->hsusb_vddcx);
2582 dwc3_hsusb_config_vddcx(0);
2583 dwc3_ssusb_ldo_enable(0);
2584 dwc3_ssusb_ldo_init(0);
2585 regulator_disable(msm->ssusb_vddcx);
2586 dwc3_ssusb_config_vddcx(0);
Manu Gautam1742db22012-06-19 13:33:24 +05302587 clk_disable_unprepare(msm->core_clk);
Manu Gautam3e9ad352012-08-16 14:44:47 -07002588 clk_disable_unprepare(msm->iface_clk);
2589 clk_disable_unprepare(msm->sleep_clk);
2590 clk_disable_unprepare(msm->hsphy_sleep_clk);
2591 clk_disable_unprepare(msm->ref_clk);
Vijayavardhan Vennapusadec1fe62013-02-12 16:05:14 +05302592 clk_disable_unprepare(msm->xo_clk);
2593 clk_put(msm->xo_clk);
Manu Gautam60e01352012-05-29 09:00:34 +05302594
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002595 return 0;
2596}
2597
Manu Gautamb5067272012-07-02 09:53:41 +05302598static int dwc3_msm_pm_suspend(struct device *dev)
2599{
2600 int ret = 0;
2601 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2602
2603 dev_dbg(dev, "dwc3-msm PM suspend\n");
2604
Manu Gautam8d98a572013-01-21 16:34:50 +05302605 flush_delayed_work_sync(&mdwc->resume_work);
2606 if (!atomic_read(&mdwc->in_lpm)) {
2607 dev_err(mdwc->dev, "Abort PM suspend!! (USB is outside LPM)\n");
2608 return -EBUSY;
2609 }
2610
Manu Gautamb5067272012-07-02 09:53:41 +05302611 ret = dwc3_msm_suspend(mdwc);
2612 if (!ret)
2613 atomic_set(&mdwc->pm_suspended, 1);
2614
2615 return ret;
2616}
2617
2618static int dwc3_msm_pm_resume(struct device *dev)
2619{
2620 int ret = 0;
2621 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2622
2623 dev_dbg(dev, "dwc3-msm PM resume\n");
2624
2625 atomic_set(&mdwc->pm_suspended, 0);
2626 if (mdwc->resume_pending) {
2627 mdwc->resume_pending = false;
2628
2629 ret = dwc3_msm_resume(mdwc);
2630 /* Update runtime PM status */
2631 pm_runtime_disable(dev);
2632 pm_runtime_set_active(dev);
2633 pm_runtime_enable(dev);
2634
2635 /* Let OTG know about resume event and update pm_count */
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302636 if (mdwc->otg_xceiv) {
Manu Gautamb5067272012-07-02 09:53:41 +05302637 mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg,
2638 DWC3_EVENT_PHY_RESUME);
Vijayavardhan Vennapusad2993b82012-10-22 13:08:21 +05302639 if (mdwc->ext_xceiv.otg_capability)
2640 mdwc->ext_xceiv.notify_ext_events(
2641 mdwc->otg_xceiv->otg,
2642 DWC3_EVENT_XCEIV_STATE);
2643 }
Manu Gautamb5067272012-07-02 09:53:41 +05302644 }
2645
2646 return ret;
2647}
2648
2649static int dwc3_msm_runtime_idle(struct device *dev)
2650{
2651 dev_dbg(dev, "DWC3-msm runtime idle\n");
2652
2653 return 0;
2654}
2655
2656static int dwc3_msm_runtime_suspend(struct device *dev)
2657{
2658 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2659
2660 dev_dbg(dev, "DWC3-msm runtime suspend\n");
2661
2662 return dwc3_msm_suspend(mdwc);
2663}
2664
2665static int dwc3_msm_runtime_resume(struct device *dev)
2666{
2667 struct dwc3_msm *mdwc = dev_get_drvdata(dev);
2668
2669 dev_dbg(dev, "DWC3-msm runtime resume\n");
2670
2671 return dwc3_msm_resume(mdwc);
2672}
2673
2674static const struct dev_pm_ops dwc3_msm_dev_pm_ops = {
2675 SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume)
2676 SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume,
2677 dwc3_msm_runtime_idle)
2678};
2679
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002680static const struct of_device_id of_dwc3_matach[] = {
2681 {
2682 .compatible = "qcom,dwc-usb3-msm",
2683 },
2684 { },
2685};
2686MODULE_DEVICE_TABLE(of, of_dwc3_matach);
2687
2688static struct platform_driver dwc3_msm_driver = {
2689 .probe = dwc3_msm_probe,
2690 .remove = __devexit_p(dwc3_msm_remove),
2691 .driver = {
2692 .name = "msm-dwc3",
Manu Gautamb5067272012-07-02 09:53:41 +05302693 .pm = &dwc3_msm_dev_pm_ops,
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002694 .of_match_table = of_dwc3_matach,
2695 },
2696};
2697
Manu Gautam377821c2012-09-28 16:53:24 +05302698MODULE_LICENSE("GPL v2");
Ido Shayevitzef72ddd2012-03-28 18:55:55 +02002699MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer");
2700
2701static int __devinit dwc3_msm_init(void)
2702{
2703 return platform_driver_register(&dwc3_msm_driver);
2704}
2705module_init(dwc3_msm_init);
2706
2707static void __exit dwc3_msm_exit(void)
2708{
2709 platform_driver_unregister(&dwc3_msm_driver);
2710}
2711module_exit(dwc3_msm_exit);