Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, The Linux Foundation. All rights reserved. |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/dma-mapping.h> |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 19 | #include <linux/pm_runtime.h> |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 20 | #include <linux/ratelimit.h> |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 22 | #include <linux/ioport.h> |
Manu Gautam | 1742db2 | 2012-06-19 13:33:24 +0530 | [diff] [blame] | 23 | #include <linux/clk.h> |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 24 | #include <linux/io.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/types.h> |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 27 | #include <linux/delay.h> |
| 28 | #include <linux/of.h> |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 29 | #include <linux/list.h> |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/uaccess.h> |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 32 | #include <linux/usb/ch9.h> |
| 33 | #include <linux/usb/gadget.h> |
| 34 | #include <linux/usb/msm_hsusb.h> |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 35 | #include <linux/regulator/consumer.h> |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 36 | #include <linux/power_supply.h> |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 37 | #include <linux/qpnp/qpnp-adc.h> |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 38 | |
| 39 | #include <mach/rpm-regulator.h> |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 40 | #include <mach/rpm-regulator-smd.h> |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 41 | #include <mach/msm_xo.h> |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 42 | #include <mach/msm_bus.h> |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 43 | #include <mach/clk.h> |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 44 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 45 | #include "dwc3_otg.h" |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 46 | #include "core.h" |
| 47 | #include "gadget.h" |
| 48 | |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 49 | /* ADC threshold values */ |
| 50 | static int adc_low_threshold = 700; |
| 51 | module_param(adc_low_threshold, int, S_IRUGO | S_IWUSR); |
| 52 | MODULE_PARM_DESC(adc_low_threshold, "ADC ID Low voltage threshold"); |
| 53 | |
| 54 | static int adc_high_threshold = 950; |
| 55 | module_param(adc_high_threshold, int, S_IRUGO | S_IWUSR); |
| 56 | MODULE_PARM_DESC(adc_high_threshold, "ADC ID High voltage threshold"); |
| 57 | |
| 58 | static int adc_meas_interval = ADC_MEAS1_INTERVAL_1S; |
| 59 | module_param(adc_meas_interval, int, S_IRUGO | S_IWUSR); |
| 60 | MODULE_PARM_DESC(adc_meas_interval, "ADC ID polling period"); |
| 61 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 62 | /** |
| 63 | * USB DBM Hardware registers. |
| 64 | * |
| 65 | */ |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 66 | #define DBM_BASE 0x000F8000 |
| 67 | #define DBM_EP_CFG(n) (DBM_BASE + (0x00 + 4 * (n))) |
| 68 | #define DBM_DATA_FIFO(n) (DBM_BASE + (0x10 + 4 * (n))) |
| 69 | #define DBM_DATA_FIFO_SIZE(n) (DBM_BASE + (0x20 + 4 * (n))) |
| 70 | #define DBM_DATA_FIFO_EN (DBM_BASE + (0x30)) |
| 71 | #define DBM_GEVNTADR (DBM_BASE + (0x34)) |
| 72 | #define DBM_GEVNTSIZ (DBM_BASE + (0x38)) |
| 73 | #define DBM_DBG_CNFG (DBM_BASE + (0x3C)) |
| 74 | #define DBM_HW_TRB0_EP(n) (DBM_BASE + (0x40 + 4 * (n))) |
| 75 | #define DBM_HW_TRB1_EP(n) (DBM_BASE + (0x50 + 4 * (n))) |
| 76 | #define DBM_HW_TRB2_EP(n) (DBM_BASE + (0x60 + 4 * (n))) |
| 77 | #define DBM_HW_TRB3_EP(n) (DBM_BASE + (0x70 + 4 * (n))) |
| 78 | #define DBM_PIPE_CFG (DBM_BASE + (0x80)) |
| 79 | #define DBM_SOFT_RESET (DBM_BASE + (0x84)) |
| 80 | #define DBM_GEN_CFG (DBM_BASE + (0x88)) |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * USB DBM Hardware registers bitmask. |
| 84 | * |
| 85 | */ |
| 86 | /* DBM_EP_CFG */ |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 87 | #define DBM_EN_EP 0x00000001 |
| 88 | #define USB3_EPNUM 0x0000003E |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 89 | #define DBM_BAM_PIPE_NUM 0x000000C0 |
| 90 | #define DBM_PRODUCER 0x00000100 |
| 91 | #define DBM_DISABLE_WB 0x00000200 |
| 92 | #define DBM_INT_RAM_ACC 0x00000400 |
| 93 | |
| 94 | /* DBM_DATA_FIFO_SIZE */ |
| 95 | #define DBM_DATA_FIFO_SIZE_MASK 0x0000ffff |
| 96 | |
| 97 | /* DBM_GEVNTSIZ */ |
| 98 | #define DBM_GEVNTSIZ_MASK 0x0000ffff |
| 99 | |
| 100 | /* DBM_DBG_CNFG */ |
| 101 | #define DBM_ENABLE_IOC_MASK 0x0000000f |
| 102 | |
| 103 | /* DBM_SOFT_RESET */ |
| 104 | #define DBM_SFT_RST_EP0 0x00000001 |
| 105 | #define DBM_SFT_RST_EP1 0x00000002 |
| 106 | #define DBM_SFT_RST_EP2 0x00000004 |
| 107 | #define DBM_SFT_RST_EP3 0x00000008 |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 108 | #define DBM_SFT_RST_EPS_MASK 0x0000000F |
| 109 | #define DBM_SFT_RST_MASK 0x80000000 |
| 110 | #define DBM_EN_MASK 0x00000002 |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 111 | |
| 112 | #define DBM_MAX_EPS 4 |
| 113 | |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 114 | /* DBM TRB configurations */ |
| 115 | #define DBM_TRB_BIT 0x80000000 |
| 116 | #define DBM_TRB_DATA_SRC 0x40000000 |
| 117 | #define DBM_TRB_DMA 0x20000000 |
| 118 | #define DBM_TRB_EP_NUM(ep) (ep<<24) |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 119 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 120 | /** |
| 121 | * USB QSCRATCH Hardware registers |
| 122 | * |
| 123 | */ |
| 124 | #define QSCRATCH_REG_OFFSET (0x000F8800) |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 125 | #define QSCRATCH_GENERAL_CFG (QSCRATCH_REG_OFFSET + 0x08) |
Manu Gautam | bd0e578 | 2012-08-30 10:39:01 -0700 | [diff] [blame] | 126 | #define HS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x10) |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 127 | #define CHARGING_DET_CTRL_REG (QSCRATCH_REG_OFFSET + 0x18) |
| 128 | #define CHARGING_DET_OUTPUT_REG (QSCRATCH_REG_OFFSET + 0x1C) |
| 129 | #define ALT_INTERRUPT_EN_REG (QSCRATCH_REG_OFFSET + 0x20) |
| 130 | #define HS_PHY_IRQ_STAT_REG (QSCRATCH_REG_OFFSET + 0x24) |
Manu Gautam | bd0e578 | 2012-08-30 10:39:01 -0700 | [diff] [blame] | 131 | #define SS_PHY_CTRL_REG (QSCRATCH_REG_OFFSET + 0x30) |
Vijayavardhan Vennapusa | d81aed3 | 2012-12-05 17:30:40 +0530 | [diff] [blame] | 132 | #define SS_CR_PROTOCOL_DATA_IN_REG (QSCRATCH_REG_OFFSET + 0x3C) |
| 133 | #define SS_CR_PROTOCOL_DATA_OUT_REG (QSCRATCH_REG_OFFSET + 0x40) |
| 134 | #define SS_CR_PROTOCOL_CAP_ADDR_REG (QSCRATCH_REG_OFFSET + 0x44) |
| 135 | #define SS_CR_PROTOCOL_CAP_DATA_REG (QSCRATCH_REG_OFFSET + 0x48) |
| 136 | #define SS_CR_PROTOCOL_READ_REG (QSCRATCH_REG_OFFSET + 0x4C) |
| 137 | #define SS_CR_PROTOCOL_WRITE_REG (QSCRATCH_REG_OFFSET + 0x50) |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 138 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 139 | struct dwc3_msm_req_complete { |
| 140 | struct list_head list_item; |
| 141 | struct usb_request *req; |
| 142 | void (*orig_complete)(struct usb_ep *ep, |
| 143 | struct usb_request *req); |
| 144 | }; |
| 145 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 146 | struct dwc3_msm { |
| 147 | struct platform_device *dwc3; |
| 148 | struct device *dev; |
| 149 | void __iomem *base; |
| 150 | u32 resource_size; |
| 151 | int dbm_num_eps; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 152 | u8 ep_num_mapping[DBM_MAX_EPS]; |
| 153 | const struct usb_ep_ops *original_ep_ops[DWC3_ENDPOINTS_NUM]; |
| 154 | struct list_head req_complete_list; |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 155 | struct msm_xo_voter *xo_handle; |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 156 | struct clk *ref_clk; |
Manu Gautam | 1742db2 | 2012-06-19 13:33:24 +0530 | [diff] [blame] | 157 | struct clk *core_clk; |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 158 | struct clk *iface_clk; |
| 159 | struct clk *sleep_clk; |
| 160 | struct clk *hsphy_sleep_clk; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 161 | struct regulator *hsusb_3p3; |
| 162 | struct regulator *hsusb_1p8; |
| 163 | struct regulator *hsusb_vddcx; |
| 164 | struct regulator *ssusb_1p8; |
| 165 | struct regulator *ssusb_vddcx; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 166 | struct dwc3_ext_xceiv ext_xceiv; |
| 167 | bool resume_pending; |
| 168 | atomic_t pm_suspended; |
| 169 | atomic_t in_lpm; |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 170 | int hs_phy_irq; |
| 171 | bool lpm_irq_seen; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 172 | struct delayed_work resume_work; |
| 173 | struct wake_lock wlock; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 174 | struct dwc3_charger charger; |
| 175 | struct usb_phy *otg_xceiv; |
| 176 | struct delayed_work chg_work; |
| 177 | enum usb_chg_state chg_state; |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 178 | struct qpnp_adc_tm_usbid_param adc_param; |
| 179 | struct delayed_work init_adc_work; |
| 180 | bool id_adc_detect; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 181 | u8 dcd_retries; |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 182 | u32 bus_perf_client; |
| 183 | struct msm_bus_scale_pdata *bus_scale_table; |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 184 | struct power_supply usb_psy; |
| 185 | unsigned int online; |
| 186 | unsigned int host_mode; |
| 187 | unsigned int current_max; |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 188 | unsigned int vdd_no_vol_level; |
| 189 | unsigned int vdd_low_vol_level; |
| 190 | unsigned int vdd_high_vol_level; |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 191 | bool vbus_active; |
Jack Pham | fadd643 | 2012-12-07 19:03:41 -0800 | [diff] [blame] | 192 | bool ext_inuse; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | #define USB_HSPHY_3P3_VOL_MIN 3050000 /* uV */ |
| 196 | #define USB_HSPHY_3P3_VOL_MAX 3300000 /* uV */ |
| 197 | #define USB_HSPHY_3P3_HPM_LOAD 16000 /* uA */ |
| 198 | |
| 199 | #define USB_HSPHY_1P8_VOL_MIN 1800000 /* uV */ |
| 200 | #define USB_HSPHY_1P8_VOL_MAX 1800000 /* uV */ |
| 201 | #define USB_HSPHY_1P8_HPM_LOAD 19000 /* uA */ |
| 202 | |
| 203 | #define USB_SSPHY_1P8_VOL_MIN 1800000 /* uV */ |
| 204 | #define USB_SSPHY_1P8_VOL_MAX 1800000 /* uV */ |
| 205 | #define USB_SSPHY_1P8_HPM_LOAD 23000 /* uA */ |
| 206 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 207 | static struct dwc3_msm *context; |
Ido Shayevitz | c9e92e9 | 2012-05-30 14:36:35 +0300 | [diff] [blame] | 208 | static u64 dwc3_msm_dma_mask = DMA_BIT_MASK(64); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 209 | |
Jack Pham | fadd643 | 2012-12-07 19:03:41 -0800 | [diff] [blame] | 210 | static struct usb_ext_notification *usb_ext; |
| 211 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 212 | /** |
| 213 | * |
| 214 | * Read register with debug info. |
| 215 | * |
| 216 | * @base - DWC3 base virtual address. |
| 217 | * @offset - register offset. |
| 218 | * |
| 219 | * @return u32 |
| 220 | */ |
| 221 | static inline u32 dwc3_msm_read_reg(void *base, u32 offset) |
| 222 | { |
| 223 | u32 val = ioread32(base + offset); |
| 224 | return val; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Read register masked field with debug info. |
| 229 | * |
| 230 | * @base - DWC3 base virtual address. |
| 231 | * @offset - register offset. |
| 232 | * @mask - register bitmask. |
| 233 | * |
| 234 | * @return u32 |
| 235 | */ |
| 236 | static inline u32 dwc3_msm_read_reg_field(void *base, |
| 237 | u32 offset, |
| 238 | const u32 mask) |
| 239 | { |
| 240 | u32 shift = find_first_bit((void *)&mask, 32); |
| 241 | u32 val = ioread32(base + offset); |
| 242 | val &= mask; /* clear other bits */ |
| 243 | val >>= shift; |
| 244 | return val; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * |
| 249 | * Write register with debug info. |
| 250 | * |
| 251 | * @base - DWC3 base virtual address. |
| 252 | * @offset - register offset. |
| 253 | * @val - value to write. |
| 254 | * |
| 255 | */ |
| 256 | static inline void dwc3_msm_write_reg(void *base, u32 offset, u32 val) |
| 257 | { |
| 258 | iowrite32(val, base + offset); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Write register masked field with debug info. |
| 263 | * |
| 264 | * @base - DWC3 base virtual address. |
| 265 | * @offset - register offset. |
| 266 | * @mask - register bitmask. |
| 267 | * @val - value to write. |
| 268 | * |
| 269 | */ |
| 270 | static inline void dwc3_msm_write_reg_field(void *base, u32 offset, |
| 271 | const u32 mask, u32 val) |
| 272 | { |
| 273 | u32 shift = find_first_bit((void *)&mask, 32); |
| 274 | u32 tmp = ioread32(base + offset); |
| 275 | |
| 276 | tmp &= ~mask; /* clear written bits */ |
| 277 | val = tmp | (val << shift); |
| 278 | iowrite32(val, base + offset); |
| 279 | } |
| 280 | |
| 281 | /** |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 282 | * Write register and read back masked value to confirm it is written |
| 283 | * |
| 284 | * @base - DWC3 base virtual address. |
| 285 | * @offset - register offset. |
| 286 | * @mask - register bitmask specifying what should be updated |
| 287 | * @val - value to write. |
| 288 | * |
| 289 | */ |
| 290 | static inline void dwc3_msm_write_readback(void *base, u32 offset, |
| 291 | const u32 mask, u32 val) |
| 292 | { |
| 293 | u32 write_val, tmp = ioread32(base + offset); |
| 294 | |
| 295 | tmp &= ~mask; /* retain other bits */ |
| 296 | write_val = tmp | val; |
| 297 | |
| 298 | iowrite32(write_val, base + offset); |
| 299 | |
| 300 | /* Read back to see if val was written */ |
| 301 | tmp = ioread32(base + offset); |
| 302 | tmp &= mask; /* clear other bits */ |
| 303 | |
| 304 | if (tmp != val) |
| 305 | dev_err(context->dev, "%s: write: %x to QSCRATCH: %x FAILED\n", |
| 306 | __func__, val, offset); |
| 307 | } |
| 308 | |
| 309 | /** |
Vijayavardhan Vennapusa | d81aed3 | 2012-12-05 17:30:40 +0530 | [diff] [blame] | 310 | * |
| 311 | * Write SSPHY register with debug info. |
| 312 | * |
| 313 | * @base - DWC3 base virtual address. |
| 314 | * @addr - SSPHY address to write. |
| 315 | * @val - value to write. |
| 316 | * |
| 317 | */ |
| 318 | static void dwc3_msm_ssusb_write_phycreg(void *base, u32 addr, u32 val) |
| 319 | { |
| 320 | iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG); |
| 321 | iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG); |
| 322 | while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG)) |
| 323 | cpu_relax(); |
| 324 | |
| 325 | iowrite32(val, base + SS_CR_PROTOCOL_DATA_IN_REG); |
| 326 | iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_DATA_REG); |
| 327 | while (ioread32(base + SS_CR_PROTOCOL_CAP_DATA_REG)) |
| 328 | cpu_relax(); |
| 329 | |
| 330 | iowrite32(0x1, base + SS_CR_PROTOCOL_WRITE_REG); |
| 331 | while (ioread32(base + SS_CR_PROTOCOL_WRITE_REG)) |
| 332 | cpu_relax(); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * |
| 337 | * Read SSPHY register with debug info. |
| 338 | * |
| 339 | * @base - DWC3 base virtual address. |
| 340 | * @addr - SSPHY address to read. |
| 341 | * |
| 342 | */ |
| 343 | static u32 dwc3_msm_ssusb_read_phycreg(void *base, u32 addr) |
| 344 | { |
| 345 | iowrite32(addr, base + SS_CR_PROTOCOL_DATA_IN_REG); |
| 346 | iowrite32(0x1, base + SS_CR_PROTOCOL_CAP_ADDR_REG); |
| 347 | while (ioread32(base + SS_CR_PROTOCOL_CAP_ADDR_REG)) |
| 348 | cpu_relax(); |
| 349 | |
| 350 | iowrite32(0x1, base + SS_CR_PROTOCOL_READ_REG); |
| 351 | while (ioread32(base + SS_CR_PROTOCOL_READ_REG)) |
| 352 | cpu_relax(); |
| 353 | |
| 354 | return ioread32(base + SS_CR_PROTOCOL_DATA_OUT_REG); |
| 355 | } |
| 356 | |
| 357 | /** |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 358 | * Return DBM EP number according to usb endpoint number. |
| 359 | * |
| 360 | */ |
| 361 | static int dwc3_msm_find_matching_dbm_ep(u8 usb_ep) |
| 362 | { |
| 363 | int i; |
| 364 | |
| 365 | for (i = 0; i < context->dbm_num_eps; i++) |
| 366 | if (context->ep_num_mapping[i] == usb_ep) |
| 367 | return i; |
| 368 | |
| 369 | return -ENODEV; /* Not found */ |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Return number of configured DBM endpoints. |
| 374 | * |
| 375 | */ |
| 376 | static int dwc3_msm_configured_dbm_ep_num(void) |
| 377 | { |
| 378 | int i; |
| 379 | int count = 0; |
| 380 | |
| 381 | for (i = 0; i < context->dbm_num_eps; i++) |
| 382 | if (context->ep_num_mapping[i]) |
| 383 | count++; |
| 384 | |
| 385 | return count; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Configure the DBM with the USB3 core event buffer. |
| 390 | * This function is called by the SNPS UDC upon initialization. |
| 391 | * |
| 392 | * @addr - address of the event buffer. |
| 393 | * @size - size of the event buffer. |
| 394 | * |
| 395 | */ |
| 396 | static int dwc3_msm_event_buffer_config(u32 addr, u16 size) |
| 397 | { |
| 398 | dev_dbg(context->dev, "%s\n", __func__); |
| 399 | |
| 400 | dwc3_msm_write_reg(context->base, DBM_GEVNTADR, addr); |
| 401 | dwc3_msm_write_reg_field(context->base, DBM_GEVNTSIZ, |
| 402 | DBM_GEVNTSIZ_MASK, size); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Reset the DBM registers upon initialization. |
| 409 | * |
| 410 | */ |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 411 | static int dwc3_msm_dbm_soft_reset(int enter_reset) |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 412 | { |
| 413 | dev_dbg(context->dev, "%s\n", __func__); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 414 | if (enter_reset) { |
| 415 | dev_dbg(context->dev, "enter DBM reset\n"); |
| 416 | dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET, |
| 417 | DBM_SFT_RST_MASK, 1); |
| 418 | } else { |
| 419 | dev_dbg(context->dev, "exit DBM reset\n"); |
| 420 | dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET, |
| 421 | DBM_SFT_RST_MASK, 0); |
| 422 | /*enable DBM*/ |
| 423 | dwc3_msm_write_reg_field(context->base, QSCRATCH_GENERAL_CFG, |
| 424 | DBM_EN_MASK, 0x1); |
| 425 | } |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Soft reset specific DBM ep. |
| 432 | * This function is called by the function driver upon events |
| 433 | * such as transfer aborting, USB re-enumeration and USB |
| 434 | * disconnection. |
| 435 | * |
| 436 | * @dbm_ep - DBM ep number. |
| 437 | * @enter_reset - should we enter a reset state or get out of it. |
| 438 | * |
| 439 | */ |
| 440 | static int dwc3_msm_dbm_ep_soft_reset(u8 dbm_ep, bool enter_reset) |
| 441 | { |
| 442 | dev_dbg(context->dev, "%s\n", __func__); |
| 443 | |
| 444 | if (dbm_ep >= context->dbm_num_eps) { |
| 445 | dev_err(context->dev, |
| 446 | "%s: Invalid DBM ep index\n", __func__); |
| 447 | return -ENODEV; |
| 448 | } |
| 449 | |
| 450 | if (enter_reset) { |
| 451 | dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET, |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 452 | DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 1); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 453 | } else { |
| 454 | dwc3_msm_write_reg_field(context->base, DBM_SOFT_RESET, |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 455 | DBM_SFT_RST_EPS_MASK & 1 << dbm_ep, 0); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Configure a USB DBM ep to work in BAM mode. |
| 463 | * |
| 464 | * |
| 465 | * @usb_ep - USB physical EP number. |
| 466 | * @producer - producer/consumer. |
| 467 | * @disable_wb - disable write back to system memory. |
| 468 | * @internal_mem - use internal USB memory for data fifo. |
| 469 | * @ioc - enable interrupt on completion. |
| 470 | * |
| 471 | * @return int - DBM ep number. |
| 472 | */ |
| 473 | static int dwc3_msm_dbm_ep_config(u8 usb_ep, u8 bam_pipe, |
| 474 | bool producer, bool disable_wb, |
| 475 | bool internal_mem, bool ioc) |
| 476 | { |
| 477 | u8 dbm_ep; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 478 | u32 ep_cfg; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 479 | |
| 480 | dev_dbg(context->dev, "%s\n", __func__); |
| 481 | |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 482 | dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep); |
| 483 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 484 | if (dbm_ep < 0) { |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 485 | dev_err(context->dev, |
| 486 | "%s: Invalid usb ep index\n", __func__); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 487 | return -ENODEV; |
| 488 | } |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 489 | /* First, reset the dbm endpoint */ |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 490 | dwc3_msm_dbm_ep_soft_reset(dbm_ep, 0); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 491 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 492 | /* Set ioc bit for dbm_ep if needed */ |
| 493 | dwc3_msm_write_reg_field(context->base, DBM_DBG_CNFG, |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 494 | DBM_ENABLE_IOC_MASK & 1 << dbm_ep, ioc ? 1 : 0); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 495 | |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 496 | ep_cfg = (producer ? DBM_PRODUCER : 0) | |
| 497 | (disable_wb ? DBM_DISABLE_WB : 0) | |
| 498 | (internal_mem ? DBM_INT_RAM_ACC : 0); |
| 499 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 500 | dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 501 | DBM_PRODUCER | DBM_DISABLE_WB | DBM_INT_RAM_ACC, ep_cfg >> 8); |
| 502 | |
| 503 | dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), USB3_EPNUM, |
| 504 | usb_ep); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 505 | dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), |
| 506 | DBM_BAM_PIPE_NUM, bam_pipe); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 507 | dwc3_msm_write_reg_field(context->base, DBM_PIPE_CFG, 0x000000ff, |
| 508 | 0xe4); |
| 509 | dwc3_msm_write_reg_field(context->base, DBM_EP_CFG(dbm_ep), DBM_EN_EP, |
| 510 | 1); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 511 | |
| 512 | return dbm_ep; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Configure a USB DBM ep to work in normal mode. |
| 517 | * |
| 518 | * @usb_ep - USB ep number. |
| 519 | * |
| 520 | */ |
| 521 | static int dwc3_msm_dbm_ep_unconfig(u8 usb_ep) |
| 522 | { |
| 523 | u8 dbm_ep; |
| 524 | |
| 525 | dev_dbg(context->dev, "%s\n", __func__); |
| 526 | |
| 527 | dbm_ep = dwc3_msm_find_matching_dbm_ep(usb_ep); |
| 528 | |
| 529 | if (dbm_ep < 0) { |
| 530 | dev_err(context->dev, |
| 531 | "%s: Invalid usb ep index\n", __func__); |
| 532 | return -ENODEV; |
| 533 | } |
| 534 | |
| 535 | context->ep_num_mapping[dbm_ep] = 0; |
| 536 | |
| 537 | dwc3_msm_write_reg(context->base, DBM_EP_CFG(dbm_ep), 0); |
| 538 | |
| 539 | /* Reset the dbm endpoint */ |
| 540 | dwc3_msm_dbm_ep_soft_reset(dbm_ep, true); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Configure the DBM with the BAM's data fifo. |
| 547 | * This function is called by the USB BAM Driver |
| 548 | * upon initialization. |
| 549 | * |
| 550 | * @ep - pointer to usb endpoint. |
| 551 | * @addr - address of data fifo. |
| 552 | * @size - size of data fifo. |
| 553 | * |
| 554 | */ |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 555 | int msm_data_fifo_config(struct usb_ep *ep, u32 addr, u32 size, u8 dst_pipe_idx) |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 556 | { |
| 557 | u8 dbm_ep; |
| 558 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 559 | u8 bam_pipe = dst_pipe_idx; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 560 | |
| 561 | dev_dbg(context->dev, "%s\n", __func__); |
| 562 | |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 563 | dbm_ep = bam_pipe; |
| 564 | context->ep_num_mapping[dbm_ep] = dep->number; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 565 | |
| 566 | dwc3_msm_write_reg(context->base, DBM_DATA_FIFO(dbm_ep), addr); |
| 567 | dwc3_msm_write_reg_field(context->base, DBM_DATA_FIFO_SIZE(dbm_ep), |
| 568 | DBM_DATA_FIFO_SIZE_MASK, size); |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Cleanups for msm endpoint on request complete. |
| 575 | * |
| 576 | * Also call original request complete. |
| 577 | * |
| 578 | * @usb_ep - pointer to usb_ep instance. |
| 579 | * @request - pointer to usb_request instance. |
| 580 | * |
| 581 | * @return int - 0 on success, negetive on error. |
| 582 | */ |
| 583 | static void dwc3_msm_req_complete_func(struct usb_ep *ep, |
| 584 | struct usb_request *request) |
| 585 | { |
| 586 | struct dwc3_request *req = to_dwc3_request(request); |
| 587 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 588 | struct dwc3_msm_req_complete *req_complete = NULL; |
| 589 | |
| 590 | /* Find original request complete function and remove it from list */ |
| 591 | list_for_each_entry(req_complete, |
| 592 | &context->req_complete_list, |
| 593 | list_item) { |
| 594 | if (req_complete->req == request) |
| 595 | break; |
| 596 | } |
| 597 | if (!req_complete || req_complete->req != request) { |
| 598 | dev_err(dep->dwc->dev, "%s: could not find the request\n", |
| 599 | __func__); |
| 600 | return; |
| 601 | } |
| 602 | list_del(&req_complete->list_item); |
| 603 | |
| 604 | /* |
| 605 | * Release another one TRB to the pool since DBM queue took 2 TRBs |
| 606 | * (normal and link), and the dwc3/gadget.c :: dwc3_gadget_giveback |
| 607 | * released only one. |
| 608 | */ |
| 609 | if (req->queued) |
| 610 | dep->busy_slot++; |
| 611 | |
| 612 | /* Unconfigure dbm ep */ |
| 613 | dwc3_msm_dbm_ep_unconfig(dep->number); |
| 614 | |
| 615 | /* |
| 616 | * If this is the last endpoint we unconfigured, than reset also |
| 617 | * the event buffers. |
| 618 | */ |
| 619 | if (0 == dwc3_msm_configured_dbm_ep_num()) |
| 620 | dwc3_msm_event_buffer_config(0, 0); |
| 621 | |
| 622 | /* |
| 623 | * Call original complete function, notice that dwc->lock is already |
| 624 | * taken by the caller of this function (dwc3_gadget_giveback()). |
| 625 | */ |
| 626 | request->complete = req_complete->orig_complete; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 627 | if (request->complete) |
| 628 | request->complete(ep, request); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 629 | |
| 630 | kfree(req_complete); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Helper function. |
| 635 | * See the header of the dwc3_msm_ep_queue function. |
| 636 | * |
| 637 | * @dwc3_ep - pointer to dwc3_ep instance. |
| 638 | * @req - pointer to dwc3_request instance. |
| 639 | * |
| 640 | * @return int - 0 on success, negetive on error. |
| 641 | */ |
| 642 | static int __dwc3_msm_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
| 643 | { |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 644 | struct dwc3_trb *trb; |
| 645 | struct dwc3_trb *trb_link; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 646 | struct dwc3_gadget_ep_cmd_params params; |
| 647 | u32 cmd; |
| 648 | int ret = 0; |
| 649 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 650 | /* We push the request to the dep->req_queued list to indicate that |
| 651 | * this request is issued with start transfer. The request will be out |
| 652 | * from this list in 2 cases. The first is that the transfer will be |
| 653 | * completed (not if the transfer is endless using a circular TRBs with |
| 654 | * with link TRB). The second case is an option to do stop stransfer, |
| 655 | * this can be initiated by the function driver when calling dequeue. |
| 656 | */ |
| 657 | req->queued = true; |
| 658 | list_add_tail(&req->list, &dep->req_queued); |
| 659 | |
| 660 | /* First, prepare a normal TRB, point to the fake buffer */ |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 661 | trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK]; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 662 | dep->free_slot++; |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 663 | memset(trb, 0, sizeof(*trb)); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 664 | |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 665 | req->trb = trb; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 666 | trb->bph = DBM_TRB_BIT | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number); |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 667 | trb->size = DWC3_TRB_SIZE_LENGTH(req->request.length); |
| 668 | trb->ctrl = DWC3_TRBCTL_NORMAL | DWC3_TRB_CTRL_HWO | DWC3_TRB_CTRL_CHN; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 669 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 670 | |
| 671 | /* Second, prepare a Link TRB that points to the first TRB*/ |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 672 | trb_link = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK]; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 673 | dep->free_slot++; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 674 | memset(trb_link, 0, sizeof *trb_link); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 675 | |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 676 | trb_link->bpl = lower_32_bits(req->trb_dma); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 677 | trb_link->bph = DBM_TRB_BIT | |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 678 | DBM_TRB_DMA | DBM_TRB_EP_NUM(dep->number); |
| 679 | trb_link->size = 0; |
| 680 | trb_link->ctrl = DWC3_TRBCTL_LINK_TRB | DWC3_TRB_CTRL_HWO; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 681 | |
| 682 | /* |
| 683 | * Now start the transfer |
| 684 | */ |
| 685 | memset(¶ms, 0, sizeof(params)); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 686 | params.param0 = 0; /* TDAddr High */ |
| 687 | params.param1 = lower_32_bits(req->trb_dma); /* DAddr Low */ |
| 688 | |
Manu Gautam | 5b2bf9a | 2012-10-18 10:52:50 +0530 | [diff] [blame] | 689 | /* DBM requires IOC to be set */ |
| 690 | cmd = DWC3_DEPCMD_STARTTRANSFER | DWC3_DEPCMD_CMDIOC; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 691 | ret = dwc3_send_gadget_ep_cmd(dep->dwc, dep->number, cmd, ¶ms); |
| 692 | if (ret < 0) { |
| 693 | dev_dbg(dep->dwc->dev, |
| 694 | "%s: failed to send STARTTRANSFER command\n", |
| 695 | __func__); |
| 696 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 697 | list_del(&req->list); |
| 698 | return ret; |
| 699 | } |
Manu Gautam | 4a51a06 | 2012-12-07 11:24:39 +0530 | [diff] [blame] | 700 | dep->flags |= DWC3_EP_BUSY; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 701 | |
| 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Queue a usb request to the DBM endpoint. |
| 707 | * This function should be called after the endpoint |
| 708 | * was enabled by the ep_enable. |
| 709 | * |
| 710 | * This function prepares special structure of TRBs which |
| 711 | * is familier with the DBM HW, so it will possible to use |
| 712 | * this endpoint in DBM mode. |
| 713 | * |
| 714 | * The TRBs prepared by this function, is one normal TRB |
| 715 | * which point to a fake buffer, followed by a link TRB |
| 716 | * that points to the first TRB. |
| 717 | * |
| 718 | * The API of this function follow the regular API of |
| 719 | * usb_ep_queue (see usb_ep_ops in include/linuk/usb/gadget.h). |
| 720 | * |
| 721 | * @usb_ep - pointer to usb_ep instance. |
| 722 | * @request - pointer to usb_request instance. |
| 723 | * @gfp_flags - possible flags. |
| 724 | * |
| 725 | * @return int - 0 on success, negetive on error. |
| 726 | */ |
| 727 | static int dwc3_msm_ep_queue(struct usb_ep *ep, |
| 728 | struct usb_request *request, gfp_t gfp_flags) |
| 729 | { |
| 730 | struct dwc3_request *req = to_dwc3_request(request); |
| 731 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 732 | struct dwc3 *dwc = dep->dwc; |
| 733 | struct dwc3_msm_req_complete *req_complete; |
| 734 | unsigned long flags; |
| 735 | int ret = 0; |
| 736 | u8 bam_pipe; |
| 737 | bool producer; |
| 738 | bool disable_wb; |
| 739 | bool internal_mem; |
| 740 | bool ioc; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 741 | u8 speed; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 742 | |
| 743 | if (!(request->udc_priv & MSM_SPS_MODE)) { |
| 744 | /* Not SPS mode, call original queue */ |
| 745 | dev_vdbg(dwc->dev, "%s: not sps mode, use regular queue\n", |
| 746 | __func__); |
| 747 | |
| 748 | return (context->original_ep_ops[dep->number])->queue(ep, |
| 749 | request, |
| 750 | gfp_flags); |
| 751 | } |
| 752 | |
| 753 | if (!dep->endpoint.desc) { |
| 754 | dev_err(dwc->dev, |
| 755 | "%s: trying to queue request %p to disabled ep %s\n", |
| 756 | __func__, request, ep->name); |
| 757 | return -EPERM; |
| 758 | } |
| 759 | |
| 760 | if (dep->number == 0 || dep->number == 1) { |
| 761 | dev_err(dwc->dev, |
| 762 | "%s: trying to queue dbm request %p to control ep %s\n", |
| 763 | __func__, request, ep->name); |
| 764 | return -EPERM; |
| 765 | } |
| 766 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 767 | |
Manu Gautam | 4a51a06 | 2012-12-07 11:24:39 +0530 | [diff] [blame] | 768 | if (dep->busy_slot != dep->free_slot || !list_empty(&dep->request_list) |
| 769 | || !list_empty(&dep->req_queued)) { |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 770 | dev_err(dwc->dev, |
| 771 | "%s: trying to queue dbm request %p tp ep %s\n", |
| 772 | __func__, request, ep->name); |
| 773 | return -EPERM; |
Manu Gautam | 4a51a06 | 2012-12-07 11:24:39 +0530 | [diff] [blame] | 774 | } else { |
| 775 | dep->busy_slot = 0; |
| 776 | dep->free_slot = 0; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Override req->complete function, but before doing that, |
| 781 | * store it's original pointer in the req_complete_list. |
| 782 | */ |
| 783 | req_complete = kzalloc(sizeof(*req_complete), GFP_KERNEL); |
| 784 | if (!req_complete) { |
| 785 | dev_err(dep->dwc->dev, "%s: not enough memory\n", __func__); |
| 786 | return -ENOMEM; |
| 787 | } |
| 788 | req_complete->req = request; |
| 789 | req_complete->orig_complete = request->complete; |
| 790 | list_add_tail(&req_complete->list_item, &context->req_complete_list); |
| 791 | request->complete = dwc3_msm_req_complete_func; |
| 792 | |
| 793 | /* |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 794 | * Configure the DBM endpoint |
| 795 | */ |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 796 | bam_pipe = request->udc_priv & MSM_PIPE_ID_MASK; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 797 | producer = ((request->udc_priv & MSM_PRODUCER) ? true : false); |
| 798 | disable_wb = ((request->udc_priv & MSM_DISABLE_WB) ? true : false); |
| 799 | internal_mem = ((request->udc_priv & MSM_INTERNAL_MEM) ? true : false); |
| 800 | ioc = ((request->udc_priv & MSM_ETD_IOC) ? true : false); |
| 801 | |
| 802 | ret = dwc3_msm_dbm_ep_config(dep->number, |
| 803 | bam_pipe, producer, |
| 804 | disable_wb, internal_mem, ioc); |
| 805 | if (ret < 0) { |
| 806 | dev_err(context->dev, |
| 807 | "error %d after calling dwc3_msm_dbm_ep_config\n", |
| 808 | ret); |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | dev_vdbg(dwc->dev, "%s: queing request %p to ep %s length %d\n", |
| 813 | __func__, request, ep->name, request->length); |
| 814 | |
| 815 | /* |
| 816 | * We must obtain the lock of the dwc3 core driver, |
| 817 | * including disabling interrupts, so we will be sure |
| 818 | * that we are the only ones that configure the HW device |
| 819 | * core and ensure that we queuing the request will finish |
| 820 | * as soon as possible so we will release back the lock. |
| 821 | */ |
| 822 | spin_lock_irqsave(&dwc->lock, flags); |
| 823 | ret = __dwc3_msm_ep_queue(dep, req); |
| 824 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 825 | if (ret < 0) { |
| 826 | dev_err(context->dev, |
| 827 | "error %d after calling __dwc3_msm_ep_queue\n", ret); |
| 828 | return ret; |
| 829 | } |
| 830 | |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 831 | speed = dwc3_readl(dwc->regs, DWC3_DSTS) & DWC3_DSTS_CONNECTSPD; |
| 832 | dwc3_msm_write_reg(context->base, DBM_GEN_CFG, speed >> 2); |
| 833 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Configure MSM endpoint. |
| 839 | * This function do specific configurations |
| 840 | * to an endpoint which need specific implementaion |
| 841 | * in the MSM architecture. |
| 842 | * |
| 843 | * This function should be called by usb function/class |
| 844 | * layer which need a support from the specific MSM HW |
| 845 | * which wrap the USB3 core. (like DBM specific endpoints) |
| 846 | * |
| 847 | * @ep - a pointer to some usb_ep instance |
| 848 | * |
| 849 | * @return int - 0 on success, negetive on error. |
| 850 | */ |
| 851 | int msm_ep_config(struct usb_ep *ep) |
| 852 | { |
| 853 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 854 | struct usb_ep_ops *new_ep_ops; |
| 855 | |
| 856 | /* Save original ep ops for future restore*/ |
| 857 | if (context->original_ep_ops[dep->number]) { |
| 858 | dev_err(context->dev, |
| 859 | "ep [%s,%d] already configured as msm endpoint\n", |
| 860 | ep->name, dep->number); |
| 861 | return -EPERM; |
| 862 | } |
| 863 | context->original_ep_ops[dep->number] = ep->ops; |
| 864 | |
| 865 | /* Set new usb ops as we like */ |
| 866 | new_ep_ops = kzalloc(sizeof(struct usb_ep_ops), GFP_KERNEL); |
| 867 | if (!new_ep_ops) { |
| 868 | dev_err(context->dev, |
| 869 | "%s: unable to allocate mem for new usb ep ops\n", |
| 870 | __func__); |
| 871 | return -ENOMEM; |
| 872 | } |
| 873 | (*new_ep_ops) = (*ep->ops); |
| 874 | new_ep_ops->queue = dwc3_msm_ep_queue; |
| 875 | ep->ops = new_ep_ops; |
| 876 | |
| 877 | /* |
| 878 | * Do HERE more usb endpoint configurations |
| 879 | * which are specific to MSM. |
| 880 | */ |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | EXPORT_SYMBOL(msm_ep_config); |
| 885 | |
| 886 | /** |
| 887 | * Un-configure MSM endpoint. |
| 888 | * Tear down configurations done in the |
| 889 | * dwc3_msm_ep_config function. |
| 890 | * |
| 891 | * @ep - a pointer to some usb_ep instance |
| 892 | * |
| 893 | * @return int - 0 on success, negetive on error. |
| 894 | */ |
| 895 | int msm_ep_unconfig(struct usb_ep *ep) |
| 896 | { |
| 897 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 898 | struct usb_ep_ops *old_ep_ops; |
| 899 | |
| 900 | /* Restore original ep ops */ |
| 901 | if (!context->original_ep_ops[dep->number]) { |
| 902 | dev_err(context->dev, |
| 903 | "ep [%s,%d] was not configured as msm endpoint\n", |
| 904 | ep->name, dep->number); |
| 905 | return -EINVAL; |
| 906 | } |
| 907 | old_ep_ops = (struct usb_ep_ops *)ep->ops; |
| 908 | ep->ops = context->original_ep_ops[dep->number]; |
| 909 | context->original_ep_ops[dep->number] = NULL; |
| 910 | kfree(old_ep_ops); |
| 911 | |
| 912 | /* |
| 913 | * Do HERE more usb endpoint un-configurations |
| 914 | * which are specific to MSM. |
| 915 | */ |
| 916 | |
| 917 | return 0; |
| 918 | } |
| 919 | EXPORT_SYMBOL(msm_ep_unconfig); |
| 920 | |
Jack Pham | fadd643 | 2012-12-07 19:03:41 -0800 | [diff] [blame] | 921 | /** |
| 922 | * msm_register_usb_ext_notification: register for event notification |
| 923 | * @info: pointer to client usb_ext_notification structure. May be NULL. |
| 924 | * |
| 925 | * @return int - 0 on success, negative on error |
| 926 | */ |
| 927 | int msm_register_usb_ext_notification(struct usb_ext_notification *info) |
| 928 | { |
| 929 | pr_debug("%s usb_ext: %p\n", __func__, info); |
| 930 | |
| 931 | if (info) { |
| 932 | if (usb_ext) { |
| 933 | pr_err("%s: already registered\n", __func__); |
| 934 | return -EEXIST; |
| 935 | } |
| 936 | |
| 937 | if (!info->notify) { |
| 938 | pr_err("%s: notify is NULL\n", __func__); |
| 939 | return -EINVAL; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | usb_ext = info; |
| 944 | return 0; |
| 945 | } |
| 946 | EXPORT_SYMBOL(msm_register_usb_ext_notification); |
| 947 | |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 948 | /* HSPHY */ |
| 949 | static int dwc3_hsusb_config_vddcx(int high) |
| 950 | { |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 951 | int min_vol, max_vol, ret; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 952 | struct dwc3_msm *dwc = context; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 953 | |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 954 | max_vol = dwc->vdd_high_vol_level; |
| 955 | min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 956 | ret = regulator_set_voltage(dwc->hsusb_vddcx, min_vol, max_vol); |
| 957 | if (ret) { |
| 958 | dev_err(dwc->dev, "unable to set voltage for HSUSB_VDDCX\n"); |
| 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__, |
| 963 | min_vol, max_vol); |
| 964 | |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | static int dwc3_hsusb_ldo_init(int init) |
| 969 | { |
| 970 | int rc = 0; |
| 971 | struct dwc3_msm *dwc = context; |
| 972 | |
| 973 | if (!init) { |
| 974 | regulator_set_voltage(dwc->hsusb_1p8, 0, USB_HSPHY_1P8_VOL_MAX); |
| 975 | regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX); |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | dwc->hsusb_3p3 = devm_regulator_get(dwc->dev, "HSUSB_3p3"); |
| 980 | if (IS_ERR(dwc->hsusb_3p3)) { |
| 981 | dev_err(dwc->dev, "unable to get hsusb 3p3\n"); |
| 982 | return PTR_ERR(dwc->hsusb_3p3); |
| 983 | } |
| 984 | |
| 985 | rc = regulator_set_voltage(dwc->hsusb_3p3, |
| 986 | USB_HSPHY_3P3_VOL_MIN, USB_HSPHY_3P3_VOL_MAX); |
| 987 | if (rc) { |
| 988 | dev_err(dwc->dev, "unable to set voltage for hsusb 3p3\n"); |
| 989 | return rc; |
| 990 | } |
| 991 | dwc->hsusb_1p8 = devm_regulator_get(dwc->dev, "HSUSB_1p8"); |
| 992 | if (IS_ERR(dwc->hsusb_1p8)) { |
| 993 | dev_err(dwc->dev, "unable to get hsusb 1p8\n"); |
| 994 | rc = PTR_ERR(dwc->hsusb_1p8); |
| 995 | goto devote_3p3; |
| 996 | } |
| 997 | rc = regulator_set_voltage(dwc->hsusb_1p8, |
| 998 | USB_HSPHY_1P8_VOL_MIN, USB_HSPHY_1P8_VOL_MAX); |
| 999 | if (rc) { |
| 1000 | dev_err(dwc->dev, "unable to set voltage for hsusb 1p8\n"); |
| 1001 | goto devote_3p3; |
| 1002 | } |
| 1003 | |
| 1004 | return 0; |
| 1005 | |
| 1006 | devote_3p3: |
| 1007 | regulator_set_voltage(dwc->hsusb_3p3, 0, USB_HSPHY_3P3_VOL_MAX); |
| 1008 | |
| 1009 | return rc; |
| 1010 | } |
| 1011 | |
| 1012 | static int dwc3_hsusb_ldo_enable(int on) |
| 1013 | { |
| 1014 | int rc = 0; |
| 1015 | struct dwc3_msm *dwc = context; |
| 1016 | |
| 1017 | dev_dbg(dwc->dev, "reg (%s)\n", on ? "HPM" : "LPM"); |
| 1018 | |
| 1019 | if (!on) |
| 1020 | goto disable_regulators; |
| 1021 | |
| 1022 | |
| 1023 | rc = regulator_set_optimum_mode(dwc->hsusb_1p8, USB_HSPHY_1P8_HPM_LOAD); |
| 1024 | if (rc < 0) { |
| 1025 | dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_1p8\n"); |
| 1026 | return rc; |
| 1027 | } |
| 1028 | |
| 1029 | rc = regulator_enable(dwc->hsusb_1p8); |
| 1030 | if (rc) { |
| 1031 | dev_err(dwc->dev, "Unable to enable HSUSB_1p8\n"); |
| 1032 | goto put_1p8_lpm; |
| 1033 | } |
| 1034 | |
| 1035 | rc = regulator_set_optimum_mode(dwc->hsusb_3p3, USB_HSPHY_3P3_HPM_LOAD); |
| 1036 | if (rc < 0) { |
| 1037 | dev_err(dwc->dev, "Unable to set HPM of regulator HSUSB_3p3\n"); |
| 1038 | goto disable_1p8; |
| 1039 | } |
| 1040 | |
| 1041 | rc = regulator_enable(dwc->hsusb_3p3); |
| 1042 | if (rc) { |
| 1043 | dev_err(dwc->dev, "Unable to enable HSUSB_3p3\n"); |
| 1044 | goto put_3p3_lpm; |
| 1045 | } |
| 1046 | |
| 1047 | return 0; |
| 1048 | |
| 1049 | disable_regulators: |
| 1050 | rc = regulator_disable(dwc->hsusb_3p3); |
| 1051 | if (rc) |
| 1052 | dev_err(dwc->dev, "Unable to disable HSUSB_3p3\n"); |
| 1053 | |
| 1054 | put_3p3_lpm: |
| 1055 | rc = regulator_set_optimum_mode(dwc->hsusb_3p3, 0); |
| 1056 | if (rc < 0) |
| 1057 | dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_3p3\n"); |
| 1058 | |
| 1059 | disable_1p8: |
| 1060 | rc = regulator_disable(dwc->hsusb_1p8); |
| 1061 | if (rc) |
| 1062 | dev_err(dwc->dev, "Unable to disable HSUSB_1p8\n"); |
| 1063 | |
| 1064 | put_1p8_lpm: |
| 1065 | rc = regulator_set_optimum_mode(dwc->hsusb_1p8, 0); |
| 1066 | if (rc < 0) |
| 1067 | dev_err(dwc->dev, "Unable to set LPM of regulator HSUSB_1p8\n"); |
| 1068 | |
| 1069 | return rc < 0 ? rc : 0; |
| 1070 | } |
| 1071 | |
| 1072 | /* SSPHY */ |
| 1073 | static int dwc3_ssusb_config_vddcx(int high) |
| 1074 | { |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 1075 | int min_vol, max_vol, ret; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 1076 | struct dwc3_msm *dwc = context; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 1077 | |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 1078 | max_vol = dwc->vdd_high_vol_level; |
| 1079 | min_vol = high ? dwc->vdd_low_vol_level : dwc->vdd_no_vol_level; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 1080 | ret = regulator_set_voltage(dwc->ssusb_vddcx, min_vol, max_vol); |
| 1081 | if (ret) { |
| 1082 | dev_err(dwc->dev, "unable to set voltage for SSUSB_VDDCX\n"); |
| 1083 | return ret; |
| 1084 | } |
| 1085 | |
| 1086 | dev_dbg(dwc->dev, "%s: min_vol:%d max_vol:%d\n", __func__, |
| 1087 | min_vol, max_vol); |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 1091 | /* 3.3v supply not needed for SS PHY */ |
| 1092 | static int dwc3_ssusb_ldo_init(int init) |
| 1093 | { |
| 1094 | int rc = 0; |
| 1095 | struct dwc3_msm *dwc = context; |
| 1096 | |
| 1097 | if (!init) { |
| 1098 | regulator_set_voltage(dwc->ssusb_1p8, 0, USB_SSPHY_1P8_VOL_MAX); |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | dwc->ssusb_1p8 = devm_regulator_get(dwc->dev, "SSUSB_1p8"); |
| 1103 | if (IS_ERR(dwc->ssusb_1p8)) { |
| 1104 | dev_err(dwc->dev, "unable to get ssusb 1p8\n"); |
| 1105 | return PTR_ERR(dwc->ssusb_1p8); |
| 1106 | } |
| 1107 | rc = regulator_set_voltage(dwc->ssusb_1p8, |
| 1108 | USB_SSPHY_1P8_VOL_MIN, USB_SSPHY_1P8_VOL_MAX); |
| 1109 | if (rc) |
| 1110 | dev_err(dwc->dev, "unable to set voltage for ssusb 1p8\n"); |
| 1111 | |
| 1112 | return rc; |
| 1113 | } |
| 1114 | |
| 1115 | static int dwc3_ssusb_ldo_enable(int on) |
| 1116 | { |
| 1117 | int rc = 0; |
| 1118 | struct dwc3_msm *dwc = context; |
| 1119 | |
| 1120 | dev_dbg(context->dev, "reg (%s)\n", on ? "HPM" : "LPM"); |
| 1121 | |
| 1122 | if (!on) |
| 1123 | goto disable_regulators; |
| 1124 | |
| 1125 | |
| 1126 | rc = regulator_set_optimum_mode(dwc->ssusb_1p8, USB_SSPHY_1P8_HPM_LOAD); |
| 1127 | if (rc < 0) { |
| 1128 | dev_err(dwc->dev, "Unable to set HPM of SSUSB_1p8\n"); |
| 1129 | return rc; |
| 1130 | } |
| 1131 | |
| 1132 | rc = regulator_enable(dwc->ssusb_1p8); |
| 1133 | if (rc) { |
| 1134 | dev_err(dwc->dev, "Unable to enable SSUSB_1p8\n"); |
| 1135 | goto put_1p8_lpm; |
| 1136 | } |
| 1137 | |
| 1138 | return 0; |
| 1139 | |
| 1140 | disable_regulators: |
| 1141 | rc = regulator_disable(dwc->ssusb_1p8); |
| 1142 | if (rc) |
| 1143 | dev_err(dwc->dev, "Unable to disable SSUSB_1p8\n"); |
| 1144 | |
| 1145 | put_1p8_lpm: |
| 1146 | rc = regulator_set_optimum_mode(dwc->ssusb_1p8, 0); |
| 1147 | if (rc < 0) |
| 1148 | dev_err(dwc->dev, "Unable to set LPM of SSUSB_1p8\n"); |
| 1149 | |
| 1150 | return rc < 0 ? rc : 0; |
| 1151 | } |
| 1152 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 1153 | static int dwc3_msm_link_clk_reset(bool assert) |
| 1154 | { |
| 1155 | int ret = 0; |
| 1156 | struct dwc3_msm *mdwc = context; |
| 1157 | |
| 1158 | if (assert) { |
| 1159 | /* Using asynchronous block reset to the hardware */ |
| 1160 | dev_dbg(mdwc->dev, "block_reset ASSERT\n"); |
| 1161 | clk_disable_unprepare(mdwc->ref_clk); |
| 1162 | clk_disable_unprepare(mdwc->iface_clk); |
| 1163 | clk_disable_unprepare(mdwc->core_clk); |
| 1164 | ret = clk_reset(mdwc->core_clk, CLK_RESET_ASSERT); |
| 1165 | if (ret) |
| 1166 | dev_err(mdwc->dev, "dwc3 core_clk assert failed\n"); |
| 1167 | } else { |
| 1168 | dev_dbg(mdwc->dev, "block_reset DEASSERT\n"); |
| 1169 | ret = clk_reset(mdwc->core_clk, CLK_RESET_DEASSERT); |
| 1170 | ndelay(200); |
| 1171 | clk_prepare_enable(mdwc->core_clk); |
| 1172 | clk_prepare_enable(mdwc->ref_clk); |
| 1173 | clk_prepare_enable(mdwc->iface_clk); |
| 1174 | if (ret) |
| 1175 | dev_err(mdwc->dev, "dwc3 core_clk deassert failed\n"); |
| 1176 | } |
| 1177 | |
| 1178 | return ret; |
| 1179 | } |
| 1180 | |
| 1181 | /* Initialize QSCRATCH registers for HSPHY and SSPHY operation */ |
| 1182 | static void dwc3_msm_qscratch_reg_init(struct dwc3_msm *msm) |
| 1183 | { |
| 1184 | u32 data = 0; |
| 1185 | |
| 1186 | /* SSPHY Initialization: Use ref_clk from pads and set its parameters */ |
| 1187 | dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002); |
| 1188 | msleep(30); |
| 1189 | /* Assert SSPHY reset */ |
| 1190 | dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210082); |
| 1191 | usleep_range(2000, 2200); |
| 1192 | /* De-assert SSPHY reset - power and ref_clock must be ON */ |
| 1193 | dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210002); |
| 1194 | usleep_range(2000, 2200); |
| 1195 | /* Ref clock must be stable now, enable ref clock for HS mode */ |
| 1196 | dwc3_msm_write_reg(msm->base, SS_PHY_CTRL_REG, 0x10210102); |
| 1197 | usleep_range(2000, 2200); |
| 1198 | /* |
| 1199 | * HSPHY Initialization: Enable UTMI clock and clamp enable HVINTs, |
| 1200 | * and disable RETENTION (power-on default is ENABLED) |
| 1201 | */ |
| 1202 | dwc3_msm_write_reg(msm->base, HS_PHY_CTRL_REG, 0x5220bb2); |
| 1203 | usleep_range(2000, 2200); |
| 1204 | /* Disable (bypass) VBUS and ID filters */ |
| 1205 | dwc3_msm_write_reg(msm->base, QSCRATCH_GENERAL_CFG, 0x78); |
| 1206 | |
| 1207 | /* |
| 1208 | * WORKAROUND: There is SSPHY suspend bug due to which USB enumerates |
| 1209 | * in HS mode instead of SS mode. Workaround it by asserting |
| 1210 | * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus mode |
| 1211 | */ |
| 1212 | data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x102D); |
| 1213 | data |= (1 << 7); |
| 1214 | dwc3_msm_ssusb_write_phycreg(msm->base, 0x102D, data); |
| 1215 | |
| 1216 | data = dwc3_msm_ssusb_read_phycreg(msm->base, 0x1010); |
| 1217 | data &= ~0xFF0; |
| 1218 | data |= 0x40; |
| 1219 | dwc3_msm_ssusb_write_phycreg(msm->base, 0x1010, data); |
| 1220 | } |
| 1221 | |
| 1222 | static void dwc3_msm_block_reset(void) |
| 1223 | { |
| 1224 | struct dwc3_msm *mdwc = context; |
| 1225 | int ret = 0; |
| 1226 | |
| 1227 | ret = dwc3_msm_link_clk_reset(1); |
| 1228 | if (ret) |
| 1229 | return; |
| 1230 | |
| 1231 | usleep_range(1000, 1200); |
| 1232 | ret = dwc3_msm_link_clk_reset(0); |
| 1233 | if (ret) |
| 1234 | return; |
| 1235 | |
| 1236 | usleep_range(10000, 12000); |
| 1237 | |
| 1238 | /* Reinitialize QSCRATCH registers after block reset */ |
| 1239 | dwc3_msm_qscratch_reg_init(mdwc); |
| 1240 | } |
| 1241 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 1242 | static void dwc3_chg_enable_secondary_det(struct dwc3_msm *mdwc) |
| 1243 | { |
| 1244 | u32 chg_ctrl; |
| 1245 | |
| 1246 | /* Turn off VDP_SRC */ |
| 1247 | dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0); |
| 1248 | msleep(20); |
| 1249 | |
| 1250 | /* Before proceeding make sure VDP_SRC is OFF */ |
| 1251 | chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG); |
| 1252 | if (chg_ctrl & 0x3F) |
| 1253 | dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n", |
| 1254 | __func__, chg_ctrl); |
| 1255 | /* |
| 1256 | * Configure DM as current source, DP as current sink |
| 1257 | * and enable battery charging comparators. |
| 1258 | */ |
| 1259 | dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x34); |
| 1260 | } |
| 1261 | |
| 1262 | static bool dwc3_chg_det_check_output(struct dwc3_msm *mdwc) |
| 1263 | { |
| 1264 | u32 chg_det; |
| 1265 | bool ret = false; |
| 1266 | |
| 1267 | chg_det = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG); |
| 1268 | ret = chg_det & 1; |
| 1269 | |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
| 1273 | static void dwc3_chg_enable_primary_det(struct dwc3_msm *mdwc) |
| 1274 | { |
| 1275 | /* |
| 1276 | * Configure DP as current source, DM as current sink |
| 1277 | * and enable battery charging comparators. |
| 1278 | */ |
| 1279 | dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x30); |
| 1280 | } |
| 1281 | |
| 1282 | static inline bool dwc3_chg_check_dcd(struct dwc3_msm *mdwc) |
| 1283 | { |
| 1284 | u32 chg_state; |
| 1285 | bool ret = false; |
| 1286 | |
| 1287 | chg_state = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_OUTPUT_REG); |
| 1288 | ret = chg_state & 2; |
| 1289 | |
| 1290 | return ret; |
| 1291 | } |
| 1292 | |
| 1293 | static inline void dwc3_chg_disable_dcd(struct dwc3_msm *mdwc) |
| 1294 | { |
| 1295 | dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x0); |
| 1296 | } |
| 1297 | |
| 1298 | static inline void dwc3_chg_enable_dcd(struct dwc3_msm *mdwc) |
| 1299 | { |
| 1300 | /* Data contact detection enable, DCDENB */ |
| 1301 | dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, 0x3F, 0x2); |
| 1302 | } |
| 1303 | |
| 1304 | static void dwc3_chg_block_reset(struct dwc3_msm *mdwc) |
| 1305 | { |
| 1306 | u32 chg_ctrl; |
| 1307 | |
| 1308 | /* Clear charger detecting control bits */ |
| 1309 | dwc3_msm_write_reg(mdwc->base, CHARGING_DET_CTRL_REG, 0x0); |
| 1310 | |
| 1311 | /* Clear alt interrupt latch and enable bits */ |
| 1312 | dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF); |
| 1313 | dwc3_msm_write_reg(mdwc->base, ALT_INTERRUPT_EN_REG, 0x0); |
| 1314 | |
| 1315 | udelay(100); |
| 1316 | |
| 1317 | /* Before proceeding make sure charger block is RESET */ |
| 1318 | chg_ctrl = dwc3_msm_read_reg(mdwc->base, CHARGING_DET_CTRL_REG); |
| 1319 | if (chg_ctrl & 0x3F) |
| 1320 | dev_err(mdwc->dev, "%s Unable to reset chg_det block: %x\n", |
| 1321 | __func__, chg_ctrl); |
| 1322 | } |
| 1323 | |
| 1324 | static const char *chg_to_string(enum dwc3_chg_type chg_type) |
| 1325 | { |
| 1326 | switch (chg_type) { |
| 1327 | case USB_SDP_CHARGER: return "USB_SDP_CHARGER"; |
| 1328 | case USB_DCP_CHARGER: return "USB_DCP_CHARGER"; |
| 1329 | case USB_CDP_CHARGER: return "USB_CDP_CHARGER"; |
| 1330 | default: return "INVALID_CHARGER"; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | #define DWC3_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */ |
| 1335 | #define DWC3_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */ |
| 1336 | #define DWC3_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */ |
| 1337 | #define DWC3_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */ |
| 1338 | |
| 1339 | static void dwc3_chg_detect_work(struct work_struct *w) |
| 1340 | { |
| 1341 | struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, chg_work.work); |
| 1342 | bool is_dcd = false, tmout, vout; |
| 1343 | unsigned long delay; |
| 1344 | |
| 1345 | dev_dbg(mdwc->dev, "chg detection work\n"); |
| 1346 | switch (mdwc->chg_state) { |
| 1347 | case USB_CHG_STATE_UNDEFINED: |
| 1348 | dwc3_chg_block_reset(mdwc); |
| 1349 | dwc3_chg_enable_dcd(mdwc); |
| 1350 | mdwc->chg_state = USB_CHG_STATE_WAIT_FOR_DCD; |
| 1351 | mdwc->dcd_retries = 0; |
| 1352 | delay = DWC3_CHG_DCD_POLL_TIME; |
| 1353 | break; |
| 1354 | case USB_CHG_STATE_WAIT_FOR_DCD: |
| 1355 | is_dcd = dwc3_chg_check_dcd(mdwc); |
| 1356 | tmout = ++mdwc->dcd_retries == DWC3_CHG_DCD_MAX_RETRIES; |
| 1357 | if (is_dcd || tmout) { |
| 1358 | dwc3_chg_disable_dcd(mdwc); |
| 1359 | dwc3_chg_enable_primary_det(mdwc); |
| 1360 | delay = DWC3_CHG_PRIMARY_DET_TIME; |
| 1361 | mdwc->chg_state = USB_CHG_STATE_DCD_DONE; |
| 1362 | } else { |
| 1363 | delay = DWC3_CHG_DCD_POLL_TIME; |
| 1364 | } |
| 1365 | break; |
| 1366 | case USB_CHG_STATE_DCD_DONE: |
| 1367 | vout = dwc3_chg_det_check_output(mdwc); |
| 1368 | if (vout) { |
| 1369 | dwc3_chg_enable_secondary_det(mdwc); |
| 1370 | delay = DWC3_CHG_SECONDARY_DET_TIME; |
| 1371 | mdwc->chg_state = USB_CHG_STATE_PRIMARY_DONE; |
| 1372 | } else { |
| 1373 | mdwc->charger.chg_type = USB_SDP_CHARGER; |
| 1374 | mdwc->chg_state = USB_CHG_STATE_DETECTED; |
| 1375 | delay = 0; |
| 1376 | } |
| 1377 | break; |
| 1378 | case USB_CHG_STATE_PRIMARY_DONE: |
| 1379 | vout = dwc3_chg_det_check_output(mdwc); |
| 1380 | if (vout) |
| 1381 | mdwc->charger.chg_type = USB_DCP_CHARGER; |
| 1382 | else |
| 1383 | mdwc->charger.chg_type = USB_CDP_CHARGER; |
| 1384 | mdwc->chg_state = USB_CHG_STATE_SECONDARY_DONE; |
| 1385 | /* fall through */ |
| 1386 | case USB_CHG_STATE_SECONDARY_DONE: |
| 1387 | mdwc->chg_state = USB_CHG_STATE_DETECTED; |
| 1388 | /* fall through */ |
| 1389 | case USB_CHG_STATE_DETECTED: |
| 1390 | dwc3_chg_block_reset(mdwc); |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1391 | /* Enable VDP_SRC */ |
| 1392 | if (mdwc->charger.chg_type == DWC3_DCP_CHARGER) |
| 1393 | dwc3_msm_write_readback(mdwc->base, |
| 1394 | CHARGING_DET_CTRL_REG, 0x1F, 0x10); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 1395 | dev_dbg(mdwc->dev, "chg_type = %s\n", |
| 1396 | chg_to_string(mdwc->charger.chg_type)); |
| 1397 | mdwc->charger.notify_detection_complete(mdwc->otg_xceiv->otg, |
| 1398 | &mdwc->charger); |
| 1399 | return; |
| 1400 | default: |
| 1401 | return; |
| 1402 | } |
| 1403 | |
| 1404 | queue_delayed_work(system_nrt_wq, &mdwc->chg_work, delay); |
| 1405 | } |
| 1406 | |
| 1407 | static void dwc3_start_chg_det(struct dwc3_charger *charger, bool start) |
| 1408 | { |
| 1409 | struct dwc3_msm *mdwc = context; |
| 1410 | |
| 1411 | if (start == false) { |
| 1412 | cancel_delayed_work_sync(&mdwc->chg_work); |
| 1413 | mdwc->chg_state = USB_CHG_STATE_UNDEFINED; |
| 1414 | charger->chg_type = DWC3_INVALID_CHARGER; |
| 1415 | return; |
| 1416 | } |
| 1417 | |
| 1418 | mdwc->chg_state = USB_CHG_STATE_UNDEFINED; |
| 1419 | charger->chg_type = DWC3_INVALID_CHARGER; |
| 1420 | queue_delayed_work(system_nrt_wq, &mdwc->chg_work, 0); |
| 1421 | } |
| 1422 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1423 | static int dwc3_msm_suspend(struct dwc3_msm *mdwc) |
| 1424 | { |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 1425 | int ret; |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1426 | bool dcp; |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 1427 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1428 | dev_dbg(mdwc->dev, "%s: entering lpm\n", __func__); |
| 1429 | |
| 1430 | if (atomic_read(&mdwc->in_lpm)) { |
| 1431 | dev_dbg(mdwc->dev, "%s: Already suspended\n", __func__); |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1435 | if (mdwc->hs_phy_irq) |
| 1436 | disable_irq(mdwc->hs_phy_irq); |
| 1437 | |
Manu Gautam | 98013c2 | 2012-11-20 17:42:42 +0530 | [diff] [blame] | 1438 | if (cancel_delayed_work_sync(&mdwc->chg_work)) |
| 1439 | dev_dbg(mdwc->dev, "%s: chg_work was pending\n", __func__); |
| 1440 | if (mdwc->chg_state != USB_CHG_STATE_DETECTED) { |
| 1441 | /* charger detection wasn't complete; re-init flags */ |
| 1442 | mdwc->chg_state = USB_CHG_STATE_UNDEFINED; |
| 1443 | mdwc->charger.chg_type = DWC3_INVALID_CHARGER; |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1444 | dwc3_msm_write_readback(mdwc->base, CHARGING_DET_CTRL_REG, |
| 1445 | 0x37, 0x0); |
Manu Gautam | 98013c2 | 2012-11-20 17:42:42 +0530 | [diff] [blame] | 1446 | } |
| 1447 | |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1448 | dcp = mdwc->charger.chg_type == DWC3_DCP_CHARGER; |
| 1449 | |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1450 | /* Sequence to put hardware in low power state: |
| 1451 | * 1. Set OTGDISABLE to disable OTG block in HSPHY (saves power) |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1452 | * 2. Clear charger detection control fields (performed above) |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1453 | * 3. SUSPEND PHY and turn OFF core clock after some delay |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 1454 | * 4. Clear interrupt latch register and enable BSV, ID HV interrupts |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1455 | * 5. Enable PHY retention |
| 1456 | */ |
| 1457 | dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x1000, 0x1000); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1458 | dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, |
| 1459 | 0xC00000, 0x800000); |
| 1460 | |
Vijayavardhan Vennapusa | 4188de2 | 2012-11-06 15:20:18 +0530 | [diff] [blame] | 1461 | /* Sequence to put SSPHY in low power state: |
| 1462 | * 1. Clear REF_SS_PHY_EN in SS_PHY_CTRL_REG |
| 1463 | * 2. Clear REF_USE_PAD in SS_PHY_CTRL_REG |
| 1464 | * 3. Set TEST_POWERED_DOWN in SS_PHY_CTRL_REG to enable PHY retention |
| 1465 | * 4. Disable SSPHY ref clk |
| 1466 | */ |
| 1467 | dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8), 0x0); |
| 1468 | dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28), 0x0); |
| 1469 | dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26), |
| 1470 | (1 << 26)); |
| 1471 | |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1472 | usleep_range(1000, 1200); |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 1473 | clk_disable_unprepare(mdwc->ref_clk); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1474 | |
| 1475 | dwc3_msm_write_reg(mdwc->base, HS_PHY_IRQ_STAT_REG, 0xFFF); |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 1476 | dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x18000, 0x18000); |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1477 | if (!dcp) |
| 1478 | dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x2, 0x0); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1479 | |
| 1480 | /* make sure above writes are completed before turning off clocks */ |
| 1481 | wmb(); |
| 1482 | clk_disable_unprepare(mdwc->core_clk); |
| 1483 | clk_disable_unprepare(mdwc->iface_clk); |
| 1484 | |
| 1485 | /* USB PHY no more requires TCXO */ |
| 1486 | ret = msm_xo_mode_vote(mdwc->xo_handle, MSM_XO_MODE_OFF); |
| 1487 | if (ret) |
| 1488 | dev_err(mdwc->dev, "%s failed to devote for TCXO buffer%d\n", |
| 1489 | __func__, ret); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1490 | |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 1491 | if (mdwc->bus_perf_client) { |
| 1492 | ret = msm_bus_scale_client_update_request( |
| 1493 | mdwc->bus_perf_client, 0); |
| 1494 | if (ret) |
| 1495 | dev_err(mdwc->dev, "Failed to reset bus bw vote\n"); |
| 1496 | } |
| 1497 | |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1498 | if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp) |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 1499 | dwc3_hsusb_ldo_enable(0); |
| 1500 | |
Vijayavardhan Vennapusa | 6bc0696 | 2012-10-31 13:23:38 +0530 | [diff] [blame] | 1501 | dwc3_ssusb_ldo_enable(0); |
| 1502 | dwc3_ssusb_config_vddcx(0); |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 1503 | dwc3_hsusb_config_vddcx(0); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1504 | wake_unlock(&mdwc->wlock); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1505 | atomic_set(&mdwc->in_lpm, 1); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1506 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1507 | dev_info(mdwc->dev, "DWC3 in low power mode\n"); |
| 1508 | |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1509 | if (mdwc->hs_phy_irq) |
| 1510 | enable_irq(mdwc->hs_phy_irq); |
| 1511 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | static int dwc3_msm_resume(struct dwc3_msm *mdwc) |
| 1516 | { |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 1517 | int ret; |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1518 | bool dcp; |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 1519 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1520 | dev_dbg(mdwc->dev, "%s: exiting lpm\n", __func__); |
| 1521 | |
| 1522 | if (!atomic_read(&mdwc->in_lpm)) { |
| 1523 | dev_dbg(mdwc->dev, "%s: Already resumed\n", __func__); |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1527 | wake_lock(&mdwc->wlock); |
| 1528 | |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 1529 | if (mdwc->bus_perf_client) { |
| 1530 | ret = msm_bus_scale_client_update_request( |
| 1531 | mdwc->bus_perf_client, 1); |
| 1532 | if (ret) |
| 1533 | dev_err(mdwc->dev, "Failed to vote for bus scaling\n"); |
| 1534 | } |
| 1535 | |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1536 | /* Vote for TCXO while waking up USB HSPHY */ |
| 1537 | ret = msm_xo_mode_vote(mdwc->xo_handle, MSM_XO_MODE_ON); |
| 1538 | if (ret) |
| 1539 | dev_err(mdwc->dev, "%s failed to vote for TCXO buffer%d\n", |
| 1540 | __func__, ret); |
| 1541 | |
Manu Gautam | a48296e | 2012-12-05 17:37:56 +0530 | [diff] [blame] | 1542 | dcp = mdwc->charger.chg_type == DWC3_DCP_CHARGER; |
| 1543 | if (mdwc->otg_xceiv && mdwc->ext_xceiv.otg_capability && !dcp) |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 1544 | dwc3_hsusb_ldo_enable(1); |
| 1545 | |
Vijayavardhan Vennapusa | 6bc0696 | 2012-10-31 13:23:38 +0530 | [diff] [blame] | 1546 | dwc3_ssusb_ldo_enable(1); |
| 1547 | dwc3_ssusb_config_vddcx(1); |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 1548 | dwc3_hsusb_config_vddcx(1); |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 1549 | clk_prepare_enable(mdwc->ref_clk); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1550 | usleep_range(1000, 1200); |
| 1551 | |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 1552 | clk_prepare_enable(mdwc->iface_clk); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1553 | clk_prepare_enable(mdwc->core_clk); |
| 1554 | |
| 1555 | /* Disable HV interrupt */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 1556 | dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x18000, 0x0); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1557 | /* Disable Retention */ |
| 1558 | dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0x2, 0x2); |
| 1559 | |
| 1560 | dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0), |
| 1561 | dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) | 0xF0000000); |
Vijayavardhan Vennapusa | 4188de2 | 2012-11-06 15:20:18 +0530 | [diff] [blame] | 1562 | /* 10usec delay required before de-asserting PHY RESET */ |
| 1563 | udelay(10); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1564 | dwc3_msm_write_reg(mdwc->base, DWC3_GUSB2PHYCFG(0), |
| 1565 | dwc3_msm_read_reg(mdwc->base, DWC3_GUSB2PHYCFG(0)) & 0x7FFFFFFF); |
| 1566 | |
| 1567 | /* Bring PHY out of suspend */ |
| 1568 | dwc3_msm_write_readback(mdwc->base, HS_PHY_CTRL_REG, 0xC00000, 0x0); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1569 | |
Vijayavardhan Vennapusa | 4188de2 | 2012-11-06 15:20:18 +0530 | [diff] [blame] | 1570 | /* Assert SS PHY RESET */ |
| 1571 | dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7), |
| 1572 | (1 << 7)); |
| 1573 | dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 28), |
| 1574 | (1 << 28)); |
| 1575 | dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 8), |
| 1576 | (1 << 8)); |
| 1577 | dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 26), 0x0); |
| 1578 | /* 10usec delay required before de-asserting SS PHY RESET */ |
| 1579 | udelay(10); |
| 1580 | dwc3_msm_write_readback(mdwc->base, SS_PHY_CTRL_REG, (1 << 7), 0x0); |
| 1581 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1582 | atomic_set(&mdwc->in_lpm, 0); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1583 | |
| 1584 | /* match disable_irq call from isr */ |
| 1585 | if (mdwc->lpm_irq_seen && mdwc->hs_phy_irq) { |
| 1586 | enable_irq(mdwc->hs_phy_irq); |
| 1587 | mdwc->lpm_irq_seen = false; |
| 1588 | } |
| 1589 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1590 | dev_info(mdwc->dev, "DWC3 exited from low power mode\n"); |
| 1591 | |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
| 1595 | static void dwc3_resume_work(struct work_struct *w) |
| 1596 | { |
| 1597 | struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, |
| 1598 | resume_work.work); |
| 1599 | |
| 1600 | dev_dbg(mdwc->dev, "%s: dwc3 resume work\n", __func__); |
| 1601 | /* handle any event that was queued while work was already running */ |
| 1602 | if (!atomic_read(&mdwc->in_lpm)) { |
| 1603 | dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__); |
| 1604 | if (mdwc->otg_xceiv) |
| 1605 | mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg, |
| 1606 | DWC3_EVENT_XCEIV_STATE); |
| 1607 | return; |
| 1608 | } |
| 1609 | |
| 1610 | /* bail out if system resume in process, else initiate RESUME */ |
| 1611 | if (atomic_read(&mdwc->pm_suspended)) { |
| 1612 | mdwc->resume_pending = true; |
| 1613 | } else { |
| 1614 | pm_runtime_get_sync(mdwc->dev); |
| 1615 | if (mdwc->otg_xceiv) |
| 1616 | mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg, |
| 1617 | DWC3_EVENT_PHY_RESUME); |
| 1618 | pm_runtime_put_sync(mdwc->dev); |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 1619 | if (mdwc->otg_xceiv && (mdwc->ext_xceiv.otg_capability)) |
| 1620 | mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg, |
| 1621 | DWC3_EVENT_XCEIV_STATE); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 1625 | static u32 debug_id = true, debug_bsv, debug_connect; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1626 | |
| 1627 | static int dwc3_connect_show(struct seq_file *s, void *unused) |
| 1628 | { |
| 1629 | if (debug_connect) |
| 1630 | seq_printf(s, "true\n"); |
| 1631 | else |
| 1632 | seq_printf(s, "false\n"); |
| 1633 | |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | static int dwc3_connect_open(struct inode *inode, struct file *file) |
| 1638 | { |
| 1639 | return single_open(file, dwc3_connect_show, inode->i_private); |
| 1640 | } |
| 1641 | |
| 1642 | static ssize_t dwc3_connect_write(struct file *file, const char __user *ubuf, |
| 1643 | size_t count, loff_t *ppos) |
| 1644 | { |
| 1645 | struct seq_file *s = file->private_data; |
| 1646 | struct dwc3_msm *mdwc = s->private; |
| 1647 | char buf[8]; |
| 1648 | |
| 1649 | memset(buf, 0x00, sizeof(buf)); |
| 1650 | |
| 1651 | if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) |
| 1652 | return -EFAULT; |
| 1653 | |
| 1654 | if (!strncmp(buf, "enable", 6) || !strncmp(buf, "true", 4)) { |
| 1655 | debug_connect = true; |
| 1656 | } else { |
| 1657 | debug_connect = debug_bsv = false; |
| 1658 | debug_id = true; |
| 1659 | } |
| 1660 | |
| 1661 | mdwc->ext_xceiv.bsv = debug_bsv; |
| 1662 | mdwc->ext_xceiv.id = debug_id ? DWC3_ID_FLOAT : DWC3_ID_GROUND; |
| 1663 | |
| 1664 | if (atomic_read(&mdwc->in_lpm)) { |
| 1665 | dev_dbg(mdwc->dev, "%s: calling resume_work\n", __func__); |
| 1666 | dwc3_resume_work(&mdwc->resume_work.work); |
| 1667 | } else { |
| 1668 | dev_dbg(mdwc->dev, "%s: notifying xceiv event\n", __func__); |
| 1669 | if (mdwc->otg_xceiv) |
| 1670 | mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg, |
| 1671 | DWC3_EVENT_XCEIV_STATE); |
| 1672 | } |
| 1673 | |
| 1674 | return count; |
| 1675 | } |
| 1676 | |
| 1677 | const struct file_operations dwc3_connect_fops = { |
| 1678 | .open = dwc3_connect_open, |
| 1679 | .read = seq_read, |
| 1680 | .write = dwc3_connect_write, |
| 1681 | .llseek = seq_lseek, |
| 1682 | .release = single_release, |
| 1683 | }; |
| 1684 | |
| 1685 | static struct dentry *dwc3_debugfs_root; |
| 1686 | |
| 1687 | static void dwc3_debugfs_init(struct dwc3_msm *mdwc) |
| 1688 | { |
| 1689 | dwc3_debugfs_root = debugfs_create_dir("msm_dwc3", NULL); |
| 1690 | |
| 1691 | if (!dwc3_debugfs_root || IS_ERR(dwc3_debugfs_root)) |
| 1692 | return; |
| 1693 | |
| 1694 | if (!debugfs_create_bool("id", S_IRUGO | S_IWUSR, dwc3_debugfs_root, |
Vijayavardhan Vennapusa | 54be1d6 | 2012-10-06 18:32:06 +0530 | [diff] [blame] | 1695 | &debug_id)) |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1696 | goto error; |
| 1697 | |
| 1698 | if (!debugfs_create_bool("bsv", S_IRUGO | S_IWUSR, dwc3_debugfs_root, |
Vijayavardhan Vennapusa | 54be1d6 | 2012-10-06 18:32:06 +0530 | [diff] [blame] | 1699 | &debug_bsv)) |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1700 | goto error; |
| 1701 | |
| 1702 | if (!debugfs_create_file("connect", S_IRUGO | S_IWUSR, |
| 1703 | dwc3_debugfs_root, mdwc, &dwc3_connect_fops)) |
| 1704 | goto error; |
| 1705 | |
| 1706 | return; |
| 1707 | |
| 1708 | error: |
| 1709 | debugfs_remove_recursive(dwc3_debugfs_root); |
| 1710 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 1711 | |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1712 | static irqreturn_t msm_dwc3_irq(int irq, void *data) |
| 1713 | { |
| 1714 | struct dwc3_msm *mdwc = data; |
| 1715 | |
| 1716 | if (atomic_read(&mdwc->in_lpm)) { |
| 1717 | dev_dbg(mdwc->dev, "%s received in LPM\n", __func__); |
| 1718 | mdwc->lpm_irq_seen = true; |
| 1719 | disable_irq_nosync(irq); |
| 1720 | queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0); |
| 1721 | } else { |
| 1722 | pr_info_ratelimited("%s: IRQ outside LPM\n", __func__); |
| 1723 | } |
| 1724 | |
| 1725 | return IRQ_HANDLED; |
| 1726 | } |
| 1727 | |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 1728 | static int dwc3_msm_power_get_property_usb(struct power_supply *psy, |
| 1729 | enum power_supply_property psp, |
| 1730 | union power_supply_propval *val) |
| 1731 | { |
| 1732 | struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm, |
| 1733 | usb_psy); |
| 1734 | switch (psp) { |
| 1735 | case POWER_SUPPLY_PROP_SCOPE: |
| 1736 | val->intval = mdwc->host_mode; |
| 1737 | break; |
| 1738 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 1739 | val->intval = mdwc->current_max; |
| 1740 | break; |
| 1741 | case POWER_SUPPLY_PROP_PRESENT: |
| 1742 | val->intval = mdwc->vbus_active; |
| 1743 | break; |
| 1744 | case POWER_SUPPLY_PROP_ONLINE: |
| 1745 | val->intval = mdwc->online; |
| 1746 | break; |
| 1747 | default: |
| 1748 | return -EINVAL; |
| 1749 | } |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
| 1753 | static int dwc3_msm_power_set_property_usb(struct power_supply *psy, |
| 1754 | enum power_supply_property psp, |
| 1755 | const union power_supply_propval *val) |
| 1756 | { |
| 1757 | static bool init; |
| 1758 | struct dwc3_msm *mdwc = container_of(psy, struct dwc3_msm, |
| 1759 | usb_psy); |
| 1760 | |
| 1761 | switch (psp) { |
| 1762 | case POWER_SUPPLY_PROP_SCOPE: |
| 1763 | mdwc->host_mode = val->intval; |
| 1764 | break; |
| 1765 | /* Process PMIC notification in PRESENT prop */ |
| 1766 | case POWER_SUPPLY_PROP_PRESENT: |
| 1767 | dev_dbg(mdwc->dev, "%s: notify xceiv event\n", __func__); |
| 1768 | if (mdwc->otg_xceiv && (mdwc->ext_xceiv.otg_capability || |
| 1769 | !init)) { |
| 1770 | mdwc->ext_xceiv.bsv = val->intval; |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 1771 | if (atomic_read(&mdwc->in_lpm)) { |
| 1772 | dev_dbg(mdwc->dev, |
| 1773 | "%s received in LPM\n", __func__); |
| 1774 | queue_delayed_work(system_nrt_wq, |
| 1775 | &mdwc->resume_work, 0); |
| 1776 | } else { |
| 1777 | mdwc->ext_xceiv.notify_ext_events( |
| 1778 | mdwc->otg_xceiv->otg, |
| 1779 | DWC3_EVENT_XCEIV_STATE); |
| 1780 | } |
| 1781 | } |
| 1782 | if (!init) |
| 1783 | init = true; |
| 1784 | mdwc->vbus_active = val->intval; |
| 1785 | break; |
| 1786 | case POWER_SUPPLY_PROP_ONLINE: |
| 1787 | mdwc->online = val->intval; |
| 1788 | break; |
| 1789 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 1790 | mdwc->current_max = val->intval; |
| 1791 | break; |
| 1792 | default: |
| 1793 | return -EINVAL; |
| 1794 | } |
| 1795 | |
| 1796 | power_supply_changed(&mdwc->usb_psy); |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | static char *dwc3_msm_pm_power_supplied_to[] = { |
| 1801 | "battery", |
| 1802 | }; |
| 1803 | |
| 1804 | static enum power_supply_property dwc3_msm_pm_power_props_usb[] = { |
| 1805 | POWER_SUPPLY_PROP_PRESENT, |
| 1806 | POWER_SUPPLY_PROP_ONLINE, |
| 1807 | POWER_SUPPLY_PROP_CURRENT_MAX, |
| 1808 | POWER_SUPPLY_PROP_SCOPE, |
| 1809 | }; |
| 1810 | |
Jack Pham | fadd643 | 2012-12-07 19:03:41 -0800 | [diff] [blame] | 1811 | static void dwc3_init_adc_work(struct work_struct *w); |
| 1812 | |
| 1813 | static void dwc3_ext_notify_online(int on) |
| 1814 | { |
| 1815 | struct dwc3_msm *mdwc = context; |
| 1816 | |
| 1817 | if (!mdwc) { |
| 1818 | pr_err("%s: DWC3 driver already removed\n", __func__); |
| 1819 | return; |
| 1820 | } |
| 1821 | |
| 1822 | dev_dbg(mdwc->dev, "notify %s%s\n", on ? "" : "dis", "connected"); |
| 1823 | |
| 1824 | if (!on) { |
| 1825 | /* external client offline; revert back to USB */ |
| 1826 | mdwc->ext_inuse = false; |
| 1827 | queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0); |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | static bool dwc3_ext_trigger_handled(struct dwc3_msm *mdwc, |
| 1832 | enum dwc3_id_state id) |
| 1833 | { |
| 1834 | int ret; |
| 1835 | |
| 1836 | if (!usb_ext) |
| 1837 | return false; |
| 1838 | |
| 1839 | ret = usb_ext->notify(usb_ext->ctxt, id, dwc3_ext_notify_online); |
| 1840 | dev_dbg(mdwc->dev, "%s: external event handler returned %d\n", __func__, |
| 1841 | ret); |
| 1842 | mdwc->ext_inuse = ret == 0; |
| 1843 | return mdwc->ext_inuse; |
| 1844 | } |
| 1845 | |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 1846 | static void dwc3_adc_notification(enum qpnp_tm_state state, void *ctx) |
| 1847 | { |
| 1848 | struct dwc3_msm *mdwc = ctx; |
| 1849 | |
| 1850 | if (state >= ADC_TM_STATE_NUM) { |
| 1851 | pr_err("%s: invalid notification %d\n", __func__, state); |
| 1852 | return; |
| 1853 | } |
| 1854 | |
| 1855 | dev_dbg(mdwc->dev, "%s: state = %s\n", __func__, |
| 1856 | state == ADC_TM_HIGH_STATE ? "high" : "low"); |
| 1857 | |
| 1858 | if (state == ADC_TM_HIGH_STATE) { |
| 1859 | mdwc->ext_xceiv.id = DWC3_ID_FLOAT; |
| 1860 | mdwc->adc_param.state_request = ADC_TM_LOW_THR_ENABLE; |
| 1861 | } else { |
| 1862 | mdwc->ext_xceiv.id = DWC3_ID_GROUND; |
| 1863 | mdwc->adc_param.state_request = ADC_TM_HIGH_THR_ENABLE; |
| 1864 | } |
| 1865 | |
Jack Pham | fadd643 | 2012-12-07 19:03:41 -0800 | [diff] [blame] | 1866 | /* Give external client a chance to handle, otherwise notify OTG */ |
| 1867 | if (!mdwc->ext_inuse && |
| 1868 | !dwc3_ext_trigger_handled(mdwc, mdwc->ext_xceiv.id)) |
| 1869 | queue_delayed_work(system_nrt_wq, &mdwc->resume_work, 0); |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 1870 | |
Jack Pham | fadd643 | 2012-12-07 19:03:41 -0800 | [diff] [blame] | 1871 | /* re-arm ADC interrupt */ |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 1872 | qpnp_adc_tm_usbid_configure(&mdwc->adc_param); |
| 1873 | } |
| 1874 | |
| 1875 | static void dwc3_init_adc_work(struct work_struct *w) |
| 1876 | { |
| 1877 | struct dwc3_msm *mdwc = container_of(w, struct dwc3_msm, |
| 1878 | init_adc_work.work); |
| 1879 | int ret; |
| 1880 | |
| 1881 | ret = qpnp_adc_tm_is_ready(); |
| 1882 | if (ret == -EPROBE_DEFER) { |
Jack Pham | 90b4d12 | 2012-12-13 11:46:22 -0800 | [diff] [blame] | 1883 | queue_delayed_work(system_nrt_wq, to_delayed_work(w), |
| 1884 | msecs_to_jiffies(100)); |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 1885 | return; |
| 1886 | } |
| 1887 | |
| 1888 | mdwc->adc_param.low_thr = adc_low_threshold; |
| 1889 | mdwc->adc_param.high_thr = adc_high_threshold; |
| 1890 | mdwc->adc_param.timer_interval = adc_meas_interval; |
| 1891 | mdwc->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE; |
| 1892 | mdwc->adc_param.usbid_ctx = mdwc; |
| 1893 | mdwc->adc_param.threshold_notification = dwc3_adc_notification; |
| 1894 | |
| 1895 | ret = qpnp_adc_tm_usbid_configure(&mdwc->adc_param); |
| 1896 | if (ret) { |
| 1897 | dev_err(mdwc->dev, "%s: request ADC error %d\n", __func__, ret); |
| 1898 | return; |
| 1899 | } |
| 1900 | |
| 1901 | mdwc->id_adc_detect = true; |
| 1902 | } |
| 1903 | |
| 1904 | static ssize_t adc_enable_show(struct device *dev, |
| 1905 | struct device_attribute *attr, char *buf) |
| 1906 | { |
| 1907 | return snprintf(buf, PAGE_SIZE, "%s\n", context->id_adc_detect ? |
| 1908 | "enabled" : "disabled"); |
| 1909 | } |
| 1910 | |
| 1911 | static ssize_t adc_enable_store(struct device *dev, |
| 1912 | struct device_attribute *attr, const char |
| 1913 | *buf, size_t size) |
| 1914 | { |
| 1915 | if (!strnicmp(buf, "enable", 6)) { |
| 1916 | if (!context->id_adc_detect) |
| 1917 | dwc3_init_adc_work(&context->init_adc_work.work); |
| 1918 | return size; |
| 1919 | } else if (!strnicmp(buf, "disable", 7)) { |
| 1920 | qpnp_adc_tm_usbid_end(); |
| 1921 | context->id_adc_detect = false; |
| 1922 | return size; |
| 1923 | } |
| 1924 | |
| 1925 | return -EINVAL; |
| 1926 | } |
| 1927 | |
| 1928 | static DEVICE_ATTR(adc_enable, S_IRUGO | S_IWUSR, adc_enable_show, |
| 1929 | adc_enable_store); |
| 1930 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 1931 | static int __devinit dwc3_msm_probe(struct platform_device *pdev) |
| 1932 | { |
| 1933 | struct device_node *node = pdev->dev.of_node; |
| 1934 | struct platform_device *dwc3; |
| 1935 | struct dwc3_msm *msm; |
| 1936 | struct resource *res; |
Ido Shayevitz | 7ad8ded | 2012-08-28 04:30:58 +0300 | [diff] [blame] | 1937 | void __iomem *tcsr; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 1938 | int ret = 0; |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 1939 | int len = 0; |
| 1940 | u32 tmp[3]; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 1941 | |
| 1942 | msm = devm_kzalloc(&pdev->dev, sizeof(*msm), GFP_KERNEL); |
| 1943 | if (!msm) { |
| 1944 | dev_err(&pdev->dev, "not enough memory\n"); |
| 1945 | return -ENOMEM; |
| 1946 | } |
| 1947 | |
| 1948 | platform_set_drvdata(pdev, msm); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 1949 | context = msm; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 1950 | msm->dev = &pdev->dev; |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 1951 | |
| 1952 | INIT_LIST_HEAD(&msm->req_complete_list); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 1953 | INIT_DELAYED_WORK(&msm->chg_work, dwc3_chg_detect_work); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 1954 | INIT_DELAYED_WORK(&msm->resume_work, dwc3_resume_work); |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 1955 | INIT_DELAYED_WORK(&msm->init_adc_work, dwc3_init_adc_work); |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 1956 | |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1957 | msm->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb"); |
| 1958 | if (IS_ERR(msm->xo_handle)) { |
| 1959 | dev_err(&pdev->dev, "%s unable to get TCXO buffer handle\n", |
| 1960 | __func__); |
| 1961 | return PTR_ERR(msm->xo_handle); |
| 1962 | } |
| 1963 | |
| 1964 | ret = msm_xo_mode_vote(msm->xo_handle, MSM_XO_MODE_ON); |
| 1965 | if (ret) { |
| 1966 | dev_err(&pdev->dev, "%s failed to vote for TCXO buffer%d\n", |
| 1967 | __func__, ret); |
| 1968 | goto free_xo_handle; |
| 1969 | } |
| 1970 | |
Manu Gautam | 1742db2 | 2012-06-19 13:33:24 +0530 | [diff] [blame] | 1971 | /* |
| 1972 | * DWC3 Core requires its CORE CLK (aka master / bus clk) to |
| 1973 | * run at 125Mhz in SSUSB mode and >60MHZ for HSUSB mode. |
| 1974 | */ |
| 1975 | msm->core_clk = devm_clk_get(&pdev->dev, "core_clk"); |
| 1976 | if (IS_ERR(msm->core_clk)) { |
| 1977 | dev_err(&pdev->dev, "failed to get core_clk\n"); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 1978 | ret = PTR_ERR(msm->core_clk); |
| 1979 | goto free_xo_handle; |
Manu Gautam | 1742db2 | 2012-06-19 13:33:24 +0530 | [diff] [blame] | 1980 | } |
| 1981 | clk_set_rate(msm->core_clk, 125000000); |
| 1982 | clk_prepare_enable(msm->core_clk); |
| 1983 | |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 1984 | msm->iface_clk = devm_clk_get(&pdev->dev, "iface_clk"); |
| 1985 | if (IS_ERR(msm->iface_clk)) { |
| 1986 | dev_err(&pdev->dev, "failed to get iface_clk\n"); |
| 1987 | ret = PTR_ERR(msm->iface_clk); |
| 1988 | goto disable_core_clk; |
| 1989 | } |
| 1990 | clk_prepare_enable(msm->iface_clk); |
| 1991 | |
| 1992 | msm->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk"); |
| 1993 | if (IS_ERR(msm->sleep_clk)) { |
| 1994 | dev_err(&pdev->dev, "failed to get sleep_clk\n"); |
| 1995 | ret = PTR_ERR(msm->sleep_clk); |
| 1996 | goto disable_iface_clk; |
| 1997 | } |
| 1998 | clk_prepare_enable(msm->sleep_clk); |
| 1999 | |
| 2000 | msm->hsphy_sleep_clk = devm_clk_get(&pdev->dev, "sleep_a_clk"); |
| 2001 | if (IS_ERR(msm->hsphy_sleep_clk)) { |
| 2002 | dev_err(&pdev->dev, "failed to get sleep_a_clk\n"); |
| 2003 | ret = PTR_ERR(msm->hsphy_sleep_clk); |
| 2004 | goto disable_sleep_clk; |
| 2005 | } |
| 2006 | clk_prepare_enable(msm->hsphy_sleep_clk); |
| 2007 | |
| 2008 | msm->ref_clk = devm_clk_get(&pdev->dev, "ref_clk"); |
| 2009 | if (IS_ERR(msm->ref_clk)) { |
| 2010 | dev_err(&pdev->dev, "failed to get ref_clk\n"); |
| 2011 | ret = PTR_ERR(msm->ref_clk); |
| 2012 | goto disable_sleep_a_clk; |
| 2013 | } |
| 2014 | clk_prepare_enable(msm->ref_clk); |
| 2015 | |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 2016 | |
| 2017 | of_get_property(node, "qcom,vdd-voltage-level", &len); |
| 2018 | if (len == sizeof(tmp)) { |
| 2019 | of_property_read_u32_array(node, "qcom,vdd-voltage-level", |
| 2020 | tmp, len/sizeof(*tmp)); |
| 2021 | msm->vdd_no_vol_level = tmp[0]; |
| 2022 | msm->vdd_low_vol_level = tmp[1]; |
| 2023 | msm->vdd_high_vol_level = tmp[2]; |
| 2024 | } else { |
| 2025 | dev_err(&pdev->dev, "no qcom,vdd-voltage-level property\n"); |
| 2026 | ret = -EINVAL; |
| 2027 | goto disable_ref_clk; |
| 2028 | } |
| 2029 | |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2030 | /* SS PHY */ |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2031 | msm->ssusb_vddcx = devm_regulator_get(&pdev->dev, "ssusb_vdd_dig"); |
| 2032 | if (IS_ERR(msm->ssusb_vddcx)) { |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 2033 | dev_err(&pdev->dev, "unable to get ssusb vddcx\n"); |
| 2034 | ret = PTR_ERR(msm->ssusb_vddcx); |
| 2035 | goto disable_ref_clk; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | ret = dwc3_ssusb_config_vddcx(1); |
| 2039 | if (ret) { |
| 2040 | dev_err(&pdev->dev, "ssusb vddcx configuration failed\n"); |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 2041 | goto disable_ref_clk; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | ret = regulator_enable(context->ssusb_vddcx); |
| 2045 | if (ret) { |
| 2046 | dev_err(&pdev->dev, "unable to enable the ssusb vddcx\n"); |
| 2047 | goto unconfig_ss_vddcx; |
| 2048 | } |
| 2049 | |
| 2050 | ret = dwc3_ssusb_ldo_init(1); |
| 2051 | if (ret) { |
| 2052 | dev_err(&pdev->dev, "ssusb vreg configuration failed\n"); |
| 2053 | goto disable_ss_vddcx; |
| 2054 | } |
| 2055 | |
| 2056 | ret = dwc3_ssusb_ldo_enable(1); |
| 2057 | if (ret) { |
| 2058 | dev_err(&pdev->dev, "ssusb vreg enable failed\n"); |
| 2059 | goto free_ss_ldo_init; |
| 2060 | } |
| 2061 | |
| 2062 | /* HS PHY */ |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2063 | msm->hsusb_vddcx = devm_regulator_get(&pdev->dev, "hsusb_vdd_dig"); |
| 2064 | if (IS_ERR(msm->hsusb_vddcx)) { |
Vijayavardhan Vennapusa | 993798a | 2012-11-09 15:11:21 +0530 | [diff] [blame] | 2065 | dev_err(&pdev->dev, "unable to get hsusb vddcx\n"); |
| 2066 | ret = PTR_ERR(msm->hsusb_vddcx); |
| 2067 | goto disable_ss_ldo; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | ret = dwc3_hsusb_config_vddcx(1); |
| 2071 | if (ret) { |
| 2072 | dev_err(&pdev->dev, "hsusb vddcx configuration failed\n"); |
| 2073 | goto disable_ss_ldo; |
| 2074 | } |
| 2075 | |
| 2076 | ret = regulator_enable(context->hsusb_vddcx); |
| 2077 | if (ret) { |
| 2078 | dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n"); |
| 2079 | goto unconfig_hs_vddcx; |
| 2080 | } |
| 2081 | |
| 2082 | ret = dwc3_hsusb_ldo_init(1); |
| 2083 | if (ret) { |
| 2084 | dev_err(&pdev->dev, "hsusb vreg configuration failed\n"); |
| 2085 | goto disable_hs_vddcx; |
| 2086 | } |
| 2087 | |
| 2088 | ret = dwc3_hsusb_ldo_enable(1); |
| 2089 | if (ret) { |
| 2090 | dev_err(&pdev->dev, "hsusb vreg enable failed\n"); |
| 2091 | goto free_hs_ldo_init; |
| 2092 | } |
| 2093 | |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 2094 | msm->ext_xceiv.id = DWC3_ID_FLOAT; |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 2095 | msm->ext_xceiv.otg_capability = of_property_read_bool(node, |
Manu Gautam | 6c0ff03 | 2012-11-02 14:55:35 +0530 | [diff] [blame] | 2096 | "qcom,otg-capability"); |
| 2097 | msm->charger.charging_disabled = of_property_read_bool(node, |
| 2098 | "qcom,charging-disabled"); |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 2099 | |
| 2100 | if (!msm->ext_xceiv.otg_capability) { |
| 2101 | /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */ |
| 2102 | msm->hs_phy_irq = platform_get_irq_byname(pdev, "hs_phy_irq"); |
| 2103 | if (msm->hs_phy_irq < 0) { |
| 2104 | dev_dbg(&pdev->dev, "pget_irq for hs_phy_irq failed\n"); |
| 2105 | msm->hs_phy_irq = 0; |
| 2106 | } else { |
| 2107 | ret = request_irq(msm->hs_phy_irq, msm_dwc3_irq, |
| 2108 | IRQF_TRIGGER_RISING, "msm_dwc3", msm); |
| 2109 | if (ret) { |
| 2110 | dev_err(&pdev->dev, "irqreq HSPHYINT failed\n"); |
| 2111 | goto disable_hs_ldo; |
| 2112 | } |
| 2113 | enable_irq_wake(msm->hs_phy_irq); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2114 | } |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 2115 | } else { |
| 2116 | /* Use ADC for ID pin detection */ |
| 2117 | queue_delayed_work(system_nrt_wq, &msm->init_adc_work, 0); |
| 2118 | device_create_file(&pdev->dev, &dev_attr_adc_enable); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2119 | } |
| 2120 | |
Ido Shayevitz | 7ad8ded | 2012-08-28 04:30:58 +0300 | [diff] [blame] | 2121 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 2122 | if (!res) { |
| 2123 | dev_dbg(&pdev->dev, "missing TCSR memory resource\n"); |
| 2124 | } else { |
| 2125 | tcsr = devm_ioremap_nocache(&pdev->dev, res->start, |
| 2126 | resource_size(res)); |
| 2127 | if (!tcsr) { |
| 2128 | dev_dbg(&pdev->dev, "tcsr ioremap failed\n"); |
| 2129 | } else { |
| 2130 | /* Enable USB3 on the primary USB port. */ |
| 2131 | writel_relaxed(0x1, tcsr); |
| 2132 | /* |
| 2133 | * Ensure that TCSR write is completed before |
| 2134 | * USB registers initialization. |
| 2135 | */ |
| 2136 | mb(); |
| 2137 | } |
| 2138 | } |
| 2139 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2140 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2141 | if (!res) { |
| 2142 | dev_err(&pdev->dev, "missing memory base resource\n"); |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2143 | ret = -ENODEV; |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2144 | goto free_hsphy_irq; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | msm->base = devm_ioremap_nocache(&pdev->dev, res->start, |
| 2148 | resource_size(res)); |
| 2149 | if (!msm->base) { |
| 2150 | dev_err(&pdev->dev, "ioremap failed\n"); |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2151 | ret = -ENODEV; |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2152 | goto free_hsphy_irq; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2153 | } |
| 2154 | |
Ido Shayevitz | ca2691e | 2012-04-17 15:54:53 +0300 | [diff] [blame] | 2155 | dwc3 = platform_device_alloc("dwc3", -1); |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2156 | if (!dwc3) { |
| 2157 | dev_err(&pdev->dev, "couldn't allocate dwc3 device\n"); |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2158 | ret = -ENODEV; |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2159 | goto free_hsphy_irq; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2160 | } |
| 2161 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2162 | dwc3->dev.parent = &pdev->dev; |
Ido Shayevitz | c9e92e9 | 2012-05-30 14:36:35 +0300 | [diff] [blame] | 2163 | dwc3->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
| 2164 | dwc3->dev.dma_mask = &dwc3_msm_dma_mask; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2165 | dwc3->dev.dma_parms = pdev->dev.dma_parms; |
| 2166 | msm->resource_size = resource_size(res); |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2167 | msm->dwc3 = dwc3; |
| 2168 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 2169 | dwc3_msm_qscratch_reg_init(msm); |
Vijayavardhan Vennapusa | d81aed3 | 2012-12-05 17:30:40 +0530 | [diff] [blame] | 2170 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2171 | pm_runtime_set_active(msm->dev); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2172 | pm_runtime_enable(msm->dev); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2173 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2174 | if (of_property_read_u32(node, "qcom,dwc-usb3-msm-dbm-eps", |
| 2175 | &msm->dbm_num_eps)) { |
| 2176 | dev_err(&pdev->dev, |
| 2177 | "unable to read platform data num of dbm eps\n"); |
| 2178 | msm->dbm_num_eps = DBM_MAX_EPS; |
| 2179 | } |
| 2180 | |
| 2181 | if (msm->dbm_num_eps > DBM_MAX_EPS) { |
| 2182 | dev_err(&pdev->dev, |
| 2183 | "Driver doesn't support number of DBM EPs. " |
| 2184 | "max: %d, dbm_num_eps: %d\n", |
| 2185 | DBM_MAX_EPS, msm->dbm_num_eps); |
| 2186 | ret = -ENODEV; |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2187 | goto put_pdev; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2188 | } |
| 2189 | |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 2190 | msm->usb_psy.name = "usb"; |
| 2191 | msm->usb_psy.type = POWER_SUPPLY_TYPE_USB; |
| 2192 | msm->usb_psy.supplied_to = dwc3_msm_pm_power_supplied_to; |
| 2193 | msm->usb_psy.num_supplicants = ARRAY_SIZE( |
| 2194 | dwc3_msm_pm_power_supplied_to); |
| 2195 | msm->usb_psy.properties = dwc3_msm_pm_power_props_usb; |
| 2196 | msm->usb_psy.num_properties = ARRAY_SIZE(dwc3_msm_pm_power_props_usb); |
| 2197 | msm->usb_psy.get_property = dwc3_msm_power_get_property_usb; |
| 2198 | msm->usb_psy.set_property = dwc3_msm_power_set_property_usb; |
| 2199 | |
| 2200 | ret = power_supply_register(&pdev->dev, &msm->usb_psy); |
| 2201 | if (ret < 0) { |
| 2202 | dev_err(&pdev->dev, |
| 2203 | "%s:power_supply_register usb failed\n", |
| 2204 | __func__); |
| 2205 | goto put_pdev; |
| 2206 | } |
| 2207 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2208 | ret = platform_device_add_resources(dwc3, pdev->resource, |
| 2209 | pdev->num_resources); |
| 2210 | if (ret) { |
| 2211 | dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n"); |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 2212 | goto put_psupply; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | ret = platform_device_add(dwc3); |
| 2216 | if (ret) { |
| 2217 | dev_err(&pdev->dev, "failed to register dwc3 device\n"); |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 2218 | goto put_psupply; |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2219 | } |
| 2220 | |
Manu Gautam | 2617deb | 2012-08-31 17:50:06 -0700 | [diff] [blame] | 2221 | msm->bus_scale_table = msm_bus_cl_get_pdata(pdev); |
| 2222 | if (!msm->bus_scale_table) { |
| 2223 | dev_err(&pdev->dev, "bus scaling is disabled\n"); |
| 2224 | } else { |
| 2225 | msm->bus_perf_client = |
| 2226 | msm_bus_scale_register_client(msm->bus_scale_table); |
| 2227 | ret = msm_bus_scale_client_update_request( |
| 2228 | msm->bus_perf_client, 1); |
| 2229 | if (ret) |
| 2230 | dev_err(&pdev->dev, "Failed to vote for bus scaling\n"); |
| 2231 | } |
| 2232 | |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 2233 | /* Reset the DBM */ |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 2234 | dwc3_msm_dbm_soft_reset(1); |
| 2235 | usleep_range(1000, 1200); |
| 2236 | dwc3_msm_dbm_soft_reset(0); |
| 2237 | |
Manu Gautam | 5b2bf9a | 2012-10-18 10:52:50 +0530 | [diff] [blame] | 2238 | dwc3_msm_event_buffer_config(dwc3_msm_read_reg(msm->base, |
| 2239 | DWC3_GEVNTADRLO(0)), |
| 2240 | dwc3_msm_read_reg(msm->base, DWC3_GEVNTSIZ(0))); |
Ido Shayevitz | 9fb8345 | 2012-04-01 17:45:58 +0300 | [diff] [blame] | 2241 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 2242 | msm->otg_xceiv = usb_get_transceiver(); |
| 2243 | if (msm->otg_xceiv) { |
| 2244 | msm->charger.start_detection = dwc3_start_chg_det; |
| 2245 | ret = dwc3_set_charger(msm->otg_xceiv->otg, &msm->charger); |
| 2246 | if (ret || !msm->charger.notify_detection_complete) { |
| 2247 | dev_err(&pdev->dev, "failed to register charger: %d\n", |
| 2248 | ret); |
| 2249 | goto put_xcvr; |
| 2250 | } |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2251 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 2252 | if (msm->ext_xceiv.otg_capability) |
| 2253 | msm->ext_xceiv.ext_block_reset = dwc3_msm_block_reset; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2254 | ret = dwc3_set_ext_xceiv(msm->otg_xceiv->otg, &msm->ext_xceiv); |
| 2255 | if (ret || !msm->ext_xceiv.notify_ext_events) { |
| 2256 | dev_err(&pdev->dev, "failed to register xceiver: %d\n", |
| 2257 | ret); |
| 2258 | goto put_xcvr; |
| 2259 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 2260 | } else { |
| 2261 | dev_err(&pdev->dev, "%s: No OTG transceiver found\n", __func__); |
| 2262 | } |
| 2263 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2264 | wake_lock_init(&msm->wlock, WAKE_LOCK_SUSPEND, "msm_dwc3"); |
| 2265 | wake_lock(&msm->wlock); |
| 2266 | dwc3_debugfs_init(msm); |
| 2267 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2268 | return 0; |
| 2269 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 2270 | put_xcvr: |
| 2271 | usb_put_transceiver(msm->otg_xceiv); |
| 2272 | platform_device_del(dwc3); |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 2273 | put_psupply: |
| 2274 | power_supply_unregister(&msm->usb_psy); |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2275 | put_pdev: |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2276 | platform_device_put(dwc3); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2277 | free_hsphy_irq: |
| 2278 | if (msm->hs_phy_irq) |
| 2279 | free_irq(msm->hs_phy_irq, msm); |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2280 | disable_hs_ldo: |
| 2281 | dwc3_hsusb_ldo_enable(0); |
| 2282 | free_hs_ldo_init: |
| 2283 | dwc3_hsusb_ldo_init(0); |
| 2284 | disable_hs_vddcx: |
| 2285 | regulator_disable(context->hsusb_vddcx); |
| 2286 | unconfig_hs_vddcx: |
| 2287 | dwc3_hsusb_config_vddcx(0); |
| 2288 | disable_ss_ldo: |
| 2289 | dwc3_ssusb_ldo_enable(0); |
| 2290 | free_ss_ldo_init: |
| 2291 | dwc3_ssusb_ldo_init(0); |
| 2292 | disable_ss_vddcx: |
| 2293 | regulator_disable(context->ssusb_vddcx); |
| 2294 | unconfig_ss_vddcx: |
| 2295 | dwc3_ssusb_config_vddcx(0); |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 2296 | disable_ref_clk: |
| 2297 | clk_disable_unprepare(msm->ref_clk); |
| 2298 | disable_sleep_a_clk: |
| 2299 | clk_disable_unprepare(msm->hsphy_sleep_clk); |
| 2300 | disable_sleep_clk: |
| 2301 | clk_disable_unprepare(msm->sleep_clk); |
| 2302 | disable_iface_clk: |
| 2303 | clk_disable_unprepare(msm->iface_clk); |
Manu Gautam | 1742db2 | 2012-06-19 13:33:24 +0530 | [diff] [blame] | 2304 | disable_core_clk: |
| 2305 | clk_disable_unprepare(msm->core_clk); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2306 | free_xo_handle: |
| 2307 | msm_xo_put(msm->xo_handle); |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2308 | |
| 2309 | return ret; |
| 2310 | } |
| 2311 | |
| 2312 | static int __devexit dwc3_msm_remove(struct platform_device *pdev) |
| 2313 | { |
| 2314 | struct dwc3_msm *msm = platform_get_drvdata(pdev); |
| 2315 | |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 2316 | if (msm->id_adc_detect) |
| 2317 | qpnp_adc_tm_usbid_end(); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2318 | if (dwc3_debugfs_root) |
| 2319 | debugfs_remove_recursive(dwc3_debugfs_root); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 2320 | if (msm->otg_xceiv) { |
| 2321 | dwc3_start_chg_det(&msm->charger, false); |
| 2322 | usb_put_transceiver(msm->otg_xceiv); |
| 2323 | } |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 2324 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2325 | pm_runtime_disable(msm->dev); |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2326 | platform_device_unregister(msm->dwc3); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2327 | wake_lock_destroy(&msm->wlock); |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2328 | |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2329 | dwc3_hsusb_ldo_enable(0); |
| 2330 | dwc3_hsusb_ldo_init(0); |
| 2331 | regulator_disable(msm->hsusb_vddcx); |
| 2332 | dwc3_hsusb_config_vddcx(0); |
| 2333 | dwc3_ssusb_ldo_enable(0); |
| 2334 | dwc3_ssusb_ldo_init(0); |
| 2335 | regulator_disable(msm->ssusb_vddcx); |
| 2336 | dwc3_ssusb_config_vddcx(0); |
Manu Gautam | 1742db2 | 2012-06-19 13:33:24 +0530 | [diff] [blame] | 2337 | clk_disable_unprepare(msm->core_clk); |
Manu Gautam | 3e9ad35 | 2012-08-16 14:44:47 -0700 | [diff] [blame] | 2338 | clk_disable_unprepare(msm->iface_clk); |
| 2339 | clk_disable_unprepare(msm->sleep_clk); |
| 2340 | clk_disable_unprepare(msm->hsphy_sleep_clk); |
| 2341 | clk_disable_unprepare(msm->ref_clk); |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2342 | msm_xo_put(msm->xo_handle); |
Manu Gautam | 60e0135 | 2012-05-29 09:00:34 +0530 | [diff] [blame] | 2343 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2344 | return 0; |
| 2345 | } |
| 2346 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2347 | static int dwc3_msm_pm_suspend(struct device *dev) |
| 2348 | { |
| 2349 | int ret = 0; |
| 2350 | struct dwc3_msm *mdwc = dev_get_drvdata(dev); |
| 2351 | |
| 2352 | dev_dbg(dev, "dwc3-msm PM suspend\n"); |
| 2353 | |
| 2354 | ret = dwc3_msm_suspend(mdwc); |
| 2355 | if (!ret) |
| 2356 | atomic_set(&mdwc->pm_suspended, 1); |
| 2357 | |
| 2358 | return ret; |
| 2359 | } |
| 2360 | |
| 2361 | static int dwc3_msm_pm_resume(struct device *dev) |
| 2362 | { |
| 2363 | int ret = 0; |
| 2364 | struct dwc3_msm *mdwc = dev_get_drvdata(dev); |
| 2365 | |
| 2366 | dev_dbg(dev, "dwc3-msm PM resume\n"); |
| 2367 | |
| 2368 | atomic_set(&mdwc->pm_suspended, 0); |
| 2369 | if (mdwc->resume_pending) { |
| 2370 | mdwc->resume_pending = false; |
| 2371 | |
| 2372 | ret = dwc3_msm_resume(mdwc); |
| 2373 | /* Update runtime PM status */ |
| 2374 | pm_runtime_disable(dev); |
| 2375 | pm_runtime_set_active(dev); |
| 2376 | pm_runtime_enable(dev); |
| 2377 | |
| 2378 | /* Let OTG know about resume event and update pm_count */ |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 2379 | if (mdwc->otg_xceiv) { |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2380 | mdwc->ext_xceiv.notify_ext_events(mdwc->otg_xceiv->otg, |
| 2381 | DWC3_EVENT_PHY_RESUME); |
Vijayavardhan Vennapusa | d2993b8 | 2012-10-22 13:08:21 +0530 | [diff] [blame] | 2382 | if (mdwc->ext_xceiv.otg_capability) |
| 2383 | mdwc->ext_xceiv.notify_ext_events( |
| 2384 | mdwc->otg_xceiv->otg, |
| 2385 | DWC3_EVENT_XCEIV_STATE); |
| 2386 | } |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | return ret; |
| 2390 | } |
| 2391 | |
| 2392 | static int dwc3_msm_runtime_idle(struct device *dev) |
| 2393 | { |
| 2394 | dev_dbg(dev, "DWC3-msm runtime idle\n"); |
| 2395 | |
| 2396 | return 0; |
| 2397 | } |
| 2398 | |
| 2399 | static int dwc3_msm_runtime_suspend(struct device *dev) |
| 2400 | { |
| 2401 | struct dwc3_msm *mdwc = dev_get_drvdata(dev); |
| 2402 | |
| 2403 | dev_dbg(dev, "DWC3-msm runtime suspend\n"); |
| 2404 | |
| 2405 | return dwc3_msm_suspend(mdwc); |
| 2406 | } |
| 2407 | |
| 2408 | static int dwc3_msm_runtime_resume(struct device *dev) |
| 2409 | { |
| 2410 | struct dwc3_msm *mdwc = dev_get_drvdata(dev); |
| 2411 | |
| 2412 | dev_dbg(dev, "DWC3-msm runtime resume\n"); |
| 2413 | |
| 2414 | return dwc3_msm_resume(mdwc); |
| 2415 | } |
| 2416 | |
| 2417 | static const struct dev_pm_ops dwc3_msm_dev_pm_ops = { |
| 2418 | SET_SYSTEM_SLEEP_PM_OPS(dwc3_msm_pm_suspend, dwc3_msm_pm_resume) |
| 2419 | SET_RUNTIME_PM_OPS(dwc3_msm_runtime_suspend, dwc3_msm_runtime_resume, |
| 2420 | dwc3_msm_runtime_idle) |
| 2421 | }; |
| 2422 | |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2423 | static const struct of_device_id of_dwc3_matach[] = { |
| 2424 | { |
| 2425 | .compatible = "qcom,dwc-usb3-msm", |
| 2426 | }, |
| 2427 | { }, |
| 2428 | }; |
| 2429 | MODULE_DEVICE_TABLE(of, of_dwc3_matach); |
| 2430 | |
| 2431 | static struct platform_driver dwc3_msm_driver = { |
| 2432 | .probe = dwc3_msm_probe, |
| 2433 | .remove = __devexit_p(dwc3_msm_remove), |
| 2434 | .driver = { |
| 2435 | .name = "msm-dwc3", |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2436 | .pm = &dwc3_msm_dev_pm_ops, |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2437 | .of_match_table = of_dwc3_matach, |
| 2438 | }, |
| 2439 | }; |
| 2440 | |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 2441 | MODULE_LICENSE("GPL v2"); |
Ido Shayevitz | ef72ddd | 2012-03-28 18:55:55 +0200 | [diff] [blame] | 2442 | MODULE_DESCRIPTION("DesignWare USB3 MSM Glue Layer"); |
| 2443 | |
| 2444 | static int __devinit dwc3_msm_init(void) |
| 2445 | { |
| 2446 | return platform_driver_register(&dwc3_msm_driver); |
| 2447 | } |
| 2448 | module_init(dwc3_msm_init); |
| 2449 | |
| 2450 | static void __exit dwc3_msm_exit(void) |
| 2451 | { |
| 2452 | platform_driver_unregister(&dwc3_msm_driver); |
| 2453 | } |
| 2454 | module_exit(dwc3_msm_exit); |