Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved. |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [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 | * |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/debugfs.h> |
| 26 | #include <linux/seq_file.h> |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 27 | #include <linux/pm_runtime.h> |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 28 | #include <linux/of.h> |
| 29 | #include <linux/dma-mapping.h> |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 30 | |
| 31 | #include <linux/usb.h> |
| 32 | #include <linux/usb/otg.h> |
| 33 | #include <linux/usb/ulpi.h> |
| 34 | #include <linux/usb/gadget.h> |
| 35 | #include <linux/usb/hcd.h> |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 36 | #include <linux/usb/quirks.h> |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 37 | #include <linux/usb/msm_hsusb.h> |
| 38 | #include <linux/usb/msm_hsusb_hw.h> |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 39 | #include <linux/regulator/consumer.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 40 | #include <linux/mfd/pm8xxx/pm8921-charger.h> |
Pavankumar Kondeti | 446f454 | 2012-02-01 13:57:13 +0530 | [diff] [blame] | 41 | #include <linux/mfd/pm8xxx/misc.h> |
Anji jonnala | a7c1c5c | 2011-12-12 12:20:36 +0530 | [diff] [blame] | 42 | #include <linux/pm_qos_params.h> |
Amit Blay | 0f7edf7 | 2012-01-15 10:11:27 +0200 | [diff] [blame] | 43 | #include <linux/power_supply.h> |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 44 | |
| 45 | #include <mach/clk.h> |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 46 | #include <mach/msm_xo.h> |
Manu Gautam | cd82e9d | 2011-12-20 14:17:28 +0530 | [diff] [blame] | 47 | #include <mach/msm_bus.h> |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 48 | |
| 49 | #define MSM_USB_BASE (motg->regs) |
| 50 | #define DRIVER_NAME "msm_otg" |
| 51 | |
Chiranjeevi Velempati | 489a27c | 2012-03-29 09:47:17 +0530 | [diff] [blame] | 52 | #define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500)) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 53 | #define ULPI_IO_TIMEOUT_USEC (10 * 1000) |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 54 | #define USB_PHY_3P3_VOL_MIN 3050000 /* uV */ |
| 55 | #define USB_PHY_3P3_VOL_MAX 3300000 /* uV */ |
| 56 | #define USB_PHY_3P3_HPM_LOAD 50000 /* uA */ |
| 57 | #define USB_PHY_3P3_LPM_LOAD 4000 /* uA */ |
| 58 | |
| 59 | #define USB_PHY_1P8_VOL_MIN 1800000 /* uV */ |
| 60 | #define USB_PHY_1P8_VOL_MAX 1800000 /* uV */ |
| 61 | #define USB_PHY_1P8_HPM_LOAD 50000 /* uA */ |
| 62 | #define USB_PHY_1P8_LPM_LOAD 4000 /* uA */ |
| 63 | |
Vamsi Krishna | 132b276 | 2011-11-11 16:09:20 -0800 | [diff] [blame] | 64 | #define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */ |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 65 | #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */ |
| 66 | |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 67 | static DECLARE_COMPLETION(pmic_vbus_init); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 68 | static struct msm_otg *the_msm_otg; |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 69 | static bool debug_aca_enabled; |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 70 | static bool debug_bus_voting_enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 71 | |
Anji jonnala | a7c1c5c | 2011-12-12 12:20:36 +0530 | [diff] [blame] | 72 | /* Prevent idle power collapse(pc) while operating in peripheral mode */ |
| 73 | static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote) |
| 74 | { |
| 75 | struct msm_otg_platform_data *pdata = dev->pdata; |
| 76 | u32 swfi_latency = 0; |
| 77 | |
| 78 | if (!pdata || !pdata->swfi_latency) |
| 79 | return; |
| 80 | |
| 81 | swfi_latency = pdata->swfi_latency + 1; |
| 82 | |
| 83 | if (vote) |
| 84 | pm_qos_update_request(&dev->pm_qos_req_dma, |
| 85 | swfi_latency); |
| 86 | else |
| 87 | pm_qos_update_request(&dev->pm_qos_req_dma, |
| 88 | PM_QOS_DEFAULT_VALUE); |
| 89 | } |
| 90 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 91 | static struct regulator *hsusb_3p3; |
| 92 | static struct regulator *hsusb_1p8; |
| 93 | static struct regulator *hsusb_vddcx; |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 94 | static struct regulator *vbus_otg; |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 95 | static struct regulator *mhl_analog_switch; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 96 | |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 97 | static bool aca_id_turned_on; |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 98 | static inline bool aca_enabled(void) |
| 99 | { |
| 100 | #ifdef CONFIG_USB_MSM_ACA |
| 101 | return true; |
| 102 | #else |
| 103 | return debug_aca_enabled; |
| 104 | #endif |
| 105 | } |
| 106 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 107 | static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init) |
| 108 | { |
| 109 | int ret = 0; |
| 110 | |
| 111 | if (init) { |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 112 | hsusb_vddcx = devm_regulator_get(motg->otg.dev, "HSUSB_VDDCX"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 113 | if (IS_ERR(hsusb_vddcx)) { |
| 114 | dev_err(motg->otg.dev, "unable to get hsusb vddcx\n"); |
| 115 | return PTR_ERR(hsusb_vddcx); |
| 116 | } |
| 117 | |
| 118 | ret = regulator_set_voltage(hsusb_vddcx, |
| 119 | USB_PHY_VDD_DIG_VOL_MIN, |
| 120 | USB_PHY_VDD_DIG_VOL_MAX); |
| 121 | if (ret) { |
| 122 | dev_err(motg->otg.dev, "unable to set the voltage " |
| 123 | "for hsusb vddcx\n"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | ret = regulator_enable(hsusb_vddcx); |
| 128 | if (ret) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 129 | regulator_set_voltage(hsusb_vddcx, 0, |
| 130 | USB_PHY_VDD_DIG_VOL_MIN); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 131 | dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n"); |
| 132 | return ret; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 133 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 134 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 135 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 136 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 137 | ret = regulator_disable(hsusb_vddcx); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 138 | if (ret) { |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 139 | dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | ret = regulator_set_voltage(hsusb_vddcx, 0, |
| 144 | USB_PHY_VDD_DIG_VOL_MIN); |
| 145 | if (ret) { |
| 146 | dev_err(motg->otg.dev, "unable to set the voltage" |
| 147 | "for hsusb vddcx\n"); |
| 148 | return ret; |
| 149 | } |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | static int msm_hsusb_ldo_init(struct msm_otg *motg, int init) |
| 156 | { |
| 157 | int rc = 0; |
| 158 | |
| 159 | if (init) { |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 160 | hsusb_3p3 = devm_regulator_get(motg->otg.dev, "HSUSB_3p3"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 161 | if (IS_ERR(hsusb_3p3)) { |
| 162 | dev_err(motg->otg.dev, "unable to get hsusb 3p3\n"); |
| 163 | return PTR_ERR(hsusb_3p3); |
| 164 | } |
| 165 | |
| 166 | rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN, |
| 167 | USB_PHY_3P3_VOL_MAX); |
| 168 | if (rc) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 169 | dev_err(motg->otg.dev, "unable to set voltage level for" |
| 170 | "hsusb 3p3\n"); |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 171 | return rc; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 172 | } |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 173 | hsusb_1p8 = devm_regulator_get(motg->otg.dev, "HSUSB_1p8"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 174 | if (IS_ERR(hsusb_1p8)) { |
| 175 | dev_err(motg->otg.dev, "unable to get hsusb 1p8\n"); |
| 176 | rc = PTR_ERR(hsusb_1p8); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 177 | goto put_3p3_lpm; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 178 | } |
| 179 | rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN, |
| 180 | USB_PHY_1P8_VOL_MAX); |
| 181 | if (rc) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | dev_err(motg->otg.dev, "unable to set voltage level for" |
| 183 | "hsusb 1p8\n"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 184 | goto put_1p8; |
| 185 | } |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 190 | put_1p8: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 192 | put_3p3_lpm: |
| 193 | regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 194 | return rc; |
| 195 | } |
| 196 | |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 197 | #ifdef CONFIG_PM_SLEEP |
| 198 | #define USB_PHY_SUSP_DIG_VOL 500000 |
| 199 | static int msm_hsusb_config_vddcx(int high) |
| 200 | { |
| 201 | int max_vol = USB_PHY_VDD_DIG_VOL_MAX; |
| 202 | int min_vol; |
| 203 | int ret; |
| 204 | |
| 205 | if (high) |
| 206 | min_vol = USB_PHY_VDD_DIG_VOL_MIN; |
| 207 | else |
| 208 | min_vol = USB_PHY_SUSP_DIG_VOL; |
| 209 | |
| 210 | ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol); |
| 211 | if (ret) { |
| 212 | pr_err("%s: unable to set the voltage for regulator " |
| 213 | "HSUSB_VDDCX\n", __func__); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol); |
| 218 | |
| 219 | return ret; |
| 220 | } |
Hemant Kumar | 8e7bd07 | 2011-08-01 14:14:24 -0700 | [diff] [blame] | 221 | #else |
| 222 | static int msm_hsusb_config_vddcx(int high) |
| 223 | { |
| 224 | return 0; |
| 225 | } |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 226 | #endif |
| 227 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 228 | static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on) |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 229 | { |
| 230 | int ret = 0; |
| 231 | |
Pavankumar Kondeti | 68964c9 | 2011-10-27 14:58:56 +0530 | [diff] [blame] | 232 | if (IS_ERR(hsusb_1p8)) { |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 233 | pr_err("%s: HSUSB_1p8 is not initialized\n", __func__); |
| 234 | return -ENODEV; |
| 235 | } |
| 236 | |
Pavankumar Kondeti | 68964c9 | 2011-10-27 14:58:56 +0530 | [diff] [blame] | 237 | if (IS_ERR(hsusb_3p3)) { |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 238 | pr_err("%s: HSUSB_3p3 is not initialized\n", __func__); |
| 239 | return -ENODEV; |
| 240 | } |
| 241 | |
| 242 | if (on) { |
| 243 | ret = regulator_set_optimum_mode(hsusb_1p8, |
| 244 | USB_PHY_1P8_HPM_LOAD); |
| 245 | if (ret < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 246 | pr_err("%s: Unable to set HPM of the regulator:" |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 247 | "HSUSB_1p8\n", __func__); |
| 248 | return ret; |
| 249 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 250 | |
| 251 | ret = regulator_enable(hsusb_1p8); |
| 252 | if (ret) { |
| 253 | dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n", |
| 254 | __func__); |
| 255 | regulator_set_optimum_mode(hsusb_1p8, 0); |
| 256 | return ret; |
| 257 | } |
| 258 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 259 | ret = regulator_set_optimum_mode(hsusb_3p3, |
| 260 | USB_PHY_3P3_HPM_LOAD); |
| 261 | if (ret < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 262 | pr_err("%s: Unable to set HPM of the regulator:" |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 263 | "HSUSB_3p3\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 264 | regulator_set_optimum_mode(hsusb_1p8, 0); |
| 265 | regulator_disable(hsusb_1p8); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 266 | return ret; |
| 267 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 268 | |
| 269 | ret = regulator_enable(hsusb_3p3); |
| 270 | if (ret) { |
| 271 | dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n", |
| 272 | __func__); |
| 273 | regulator_set_optimum_mode(hsusb_3p3, 0); |
| 274 | regulator_set_optimum_mode(hsusb_1p8, 0); |
| 275 | regulator_disable(hsusb_1p8); |
| 276 | return ret; |
| 277 | } |
| 278 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 279 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 280 | ret = regulator_disable(hsusb_1p8); |
| 281 | if (ret) { |
| 282 | dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n", |
| 283 | __func__); |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | ret = regulator_set_optimum_mode(hsusb_1p8, 0); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 288 | if (ret < 0) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 289 | pr_err("%s: Unable to set LPM of the regulator:" |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 290 | "HSUSB_1p8\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 291 | |
| 292 | ret = regulator_disable(hsusb_3p3); |
| 293 | if (ret) { |
| 294 | dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n", |
| 295 | __func__); |
| 296 | return ret; |
| 297 | } |
| 298 | ret = regulator_set_optimum_mode(hsusb_3p3, 0); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 299 | if (ret < 0) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 300 | pr_err("%s: Unable to set LPM of the regulator:" |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 301 | "HSUSB_3p3\n", __func__); |
| 302 | } |
| 303 | |
| 304 | pr_debug("reg (%s)\n", on ? "HPM" : "LPM"); |
| 305 | return ret < 0 ? ret : 0; |
| 306 | } |
| 307 | |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 308 | static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on) |
| 309 | { |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 310 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 311 | |
| 312 | if (!pdata->mhl_enable) |
| 313 | return; |
| 314 | |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 315 | if (!mhl_analog_switch) { |
| 316 | pr_err("%s: mhl_analog_switch is NULL.\n", __func__); |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 320 | if (on) { |
| 321 | if (regulator_enable(mhl_analog_switch)) |
| 322 | pr_err("unable to enable mhl_analog_switch\n"); |
| 323 | } else { |
| 324 | regulator_disable(mhl_analog_switch); |
| 325 | } |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 326 | } |
| 327 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 328 | static int ulpi_read(struct otg_transceiver *otg, u32 reg) |
| 329 | { |
| 330 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 331 | int cnt = 0; |
| 332 | |
| 333 | /* initiate read operation */ |
| 334 | writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg), |
| 335 | USB_ULPI_VIEWPORT); |
| 336 | |
| 337 | /* wait for completion */ |
| 338 | while (cnt < ULPI_IO_TIMEOUT_USEC) { |
| 339 | if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN)) |
| 340 | break; |
| 341 | udelay(1); |
| 342 | cnt++; |
| 343 | } |
| 344 | |
| 345 | if (cnt >= ULPI_IO_TIMEOUT_USEC) { |
| 346 | dev_err(otg->dev, "ulpi_read: timeout %08x\n", |
| 347 | readl(USB_ULPI_VIEWPORT)); |
| 348 | return -ETIMEDOUT; |
| 349 | } |
| 350 | return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT)); |
| 351 | } |
| 352 | |
| 353 | static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg) |
| 354 | { |
| 355 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 356 | int cnt = 0; |
| 357 | |
| 358 | /* initiate write operation */ |
| 359 | writel(ULPI_RUN | ULPI_WRITE | |
| 360 | ULPI_ADDR(reg) | ULPI_DATA(val), |
| 361 | USB_ULPI_VIEWPORT); |
| 362 | |
| 363 | /* wait for completion */ |
| 364 | while (cnt < ULPI_IO_TIMEOUT_USEC) { |
| 365 | if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN)) |
| 366 | break; |
| 367 | udelay(1); |
| 368 | cnt++; |
| 369 | } |
| 370 | |
| 371 | if (cnt >= ULPI_IO_TIMEOUT_USEC) { |
| 372 | dev_err(otg->dev, "ulpi_write: timeout\n"); |
| 373 | return -ETIMEDOUT; |
| 374 | } |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static struct otg_io_access_ops msm_otg_io_ops = { |
| 379 | .read = ulpi_read, |
| 380 | .write = ulpi_write, |
| 381 | }; |
| 382 | |
| 383 | static void ulpi_init(struct msm_otg *motg) |
| 384 | { |
| 385 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 386 | int *seq = pdata->phy_init_seq; |
| 387 | |
| 388 | if (!seq) |
| 389 | return; |
| 390 | |
| 391 | while (seq[0] >= 0) { |
| 392 | dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n", |
| 393 | seq[0], seq[1]); |
| 394 | ulpi_write(&motg->otg, seq[0], seq[1]); |
| 395 | seq += 2; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert) |
| 400 | { |
| 401 | int ret; |
| 402 | |
Pavankumar Kondeti | 923262e | 2012-04-20 15:34:24 +0530 | [diff] [blame^] | 403 | if (IS_ERR(motg->clk)) |
| 404 | return 0; |
| 405 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 406 | if (assert) { |
| 407 | ret = clk_reset(motg->clk, CLK_RESET_ASSERT); |
| 408 | if (ret) |
| 409 | dev_err(motg->otg.dev, "usb hs_clk assert failed\n"); |
| 410 | } else { |
| 411 | ret = clk_reset(motg->clk, CLK_RESET_DEASSERT); |
| 412 | if (ret) |
| 413 | dev_err(motg->otg.dev, "usb hs_clk deassert failed\n"); |
| 414 | } |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static int msm_otg_phy_clk_reset(struct msm_otg *motg) |
| 419 | { |
| 420 | int ret; |
| 421 | |
Amit Blay | 02eff13 | 2011-09-21 16:46:24 +0300 | [diff] [blame] | 422 | if (IS_ERR(motg->phy_reset_clk)) |
| 423 | return 0; |
| 424 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 425 | ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT); |
| 426 | if (ret) { |
| 427 | dev_err(motg->otg.dev, "usb phy clk assert failed\n"); |
| 428 | return ret; |
| 429 | } |
| 430 | usleep_range(10000, 12000); |
| 431 | ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT); |
| 432 | if (ret) |
| 433 | dev_err(motg->otg.dev, "usb phy clk deassert failed\n"); |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static int msm_otg_phy_reset(struct msm_otg *motg) |
| 438 | { |
| 439 | u32 val; |
| 440 | int ret; |
| 441 | int retries; |
| 442 | |
| 443 | ret = msm_otg_link_clk_reset(motg, 1); |
| 444 | if (ret) |
| 445 | return ret; |
| 446 | ret = msm_otg_phy_clk_reset(motg); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | ret = msm_otg_link_clk_reset(motg, 0); |
| 450 | if (ret) |
| 451 | return ret; |
| 452 | |
| 453 | val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK; |
| 454 | writel(val | PORTSC_PTS_ULPI, USB_PORTSC); |
| 455 | |
| 456 | for (retries = 3; retries > 0; retries--) { |
| 457 | ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM, |
| 458 | ULPI_CLR(ULPI_FUNC_CTRL)); |
| 459 | if (!ret) |
| 460 | break; |
| 461 | ret = msm_otg_phy_clk_reset(motg); |
| 462 | if (ret) |
| 463 | return ret; |
| 464 | } |
| 465 | if (!retries) |
| 466 | return -ETIMEDOUT; |
| 467 | |
| 468 | /* This reset calibrates the phy, if the above write succeeded */ |
| 469 | ret = msm_otg_phy_clk_reset(motg); |
| 470 | if (ret) |
| 471 | return ret; |
| 472 | |
| 473 | for (retries = 3; retries > 0; retries--) { |
| 474 | ret = ulpi_read(&motg->otg, ULPI_DEBUG); |
| 475 | if (ret != -ETIMEDOUT) |
| 476 | break; |
| 477 | ret = msm_otg_phy_clk_reset(motg); |
| 478 | if (ret) |
| 479 | return ret; |
| 480 | } |
| 481 | if (!retries) |
| 482 | return -ETIMEDOUT; |
| 483 | |
| 484 | dev_info(motg->otg.dev, "phy_reset: success\n"); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | #define LINK_RESET_TIMEOUT_USEC (250 * 1000) |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 489 | static int msm_otg_link_reset(struct msm_otg *motg) |
| 490 | { |
| 491 | int cnt = 0; |
| 492 | |
| 493 | writel_relaxed(USBCMD_RESET, USB_USBCMD); |
| 494 | while (cnt < LINK_RESET_TIMEOUT_USEC) { |
| 495 | if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET)) |
| 496 | break; |
| 497 | udelay(1); |
| 498 | cnt++; |
| 499 | } |
| 500 | if (cnt >= LINK_RESET_TIMEOUT_USEC) |
| 501 | return -ETIMEDOUT; |
| 502 | |
| 503 | /* select ULPI phy */ |
| 504 | writel_relaxed(0x80000000, USB_PORTSC); |
| 505 | writel_relaxed(0x0, USB_AHBBURST); |
Vijayavardhan Vennapusa | 5f32d7a | 2012-03-14 16:30:26 +0530 | [diff] [blame] | 506 | writel_relaxed(0x08, USB_AHBMODE); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 511 | static int msm_otg_reset(struct otg_transceiver *otg) |
| 512 | { |
| 513 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 514 | struct msm_otg_platform_data *pdata = motg->pdata; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 515 | int ret; |
| 516 | u32 val = 0; |
| 517 | u32 ulpi_val = 0; |
| 518 | |
Ofir Cohen | 4da266f | 2012-01-03 10:19:29 +0200 | [diff] [blame] | 519 | /* |
| 520 | * USB PHY and Link reset also reset the USB BAM. |
| 521 | * Thus perform reset operation only once to avoid |
| 522 | * USB BAM reset on other cases e.g. USB cable disconnections. |
| 523 | */ |
| 524 | if (pdata->disable_reset_on_disconnect) { |
| 525 | if (motg->reset_counter) |
| 526 | return 0; |
| 527 | else |
| 528 | motg->reset_counter++; |
| 529 | } |
| 530 | |
Pavankumar Kondeti | 923262e | 2012-04-20 15:34:24 +0530 | [diff] [blame^] | 531 | if (!IS_ERR(motg->clk)) |
| 532 | clk_prepare_enable(motg->clk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 533 | ret = msm_otg_phy_reset(motg); |
| 534 | if (ret) { |
| 535 | dev_err(otg->dev, "phy_reset failed\n"); |
| 536 | return ret; |
| 537 | } |
| 538 | |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 539 | aca_id_turned_on = false; |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 540 | ret = msm_otg_link_reset(motg); |
| 541 | if (ret) { |
| 542 | dev_err(otg->dev, "link reset failed\n"); |
| 543 | return ret; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 544 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 545 | msleep(100); |
Anji jonnala | a8b8d73 | 2011-12-06 10:03:24 +0530 | [diff] [blame] | 546 | |
| 547 | ulpi_init(motg); |
| 548 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 549 | /* Ensure that RESET operation is completed before turning off clock */ |
| 550 | mb(); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 551 | |
Pavankumar Kondeti | 923262e | 2012-04-20 15:34:24 +0530 | [diff] [blame^] | 552 | if (!IS_ERR(motg->clk)) |
| 553 | clk_disable_unprepare(motg->clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 554 | |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 555 | if (pdata->otg_control == OTG_PHY_CONTROL) { |
| 556 | val = readl_relaxed(USB_OTGSC); |
| 557 | if (pdata->mode == USB_OTG) { |
| 558 | ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID; |
| 559 | val |= OTGSC_IDIE | OTGSC_BSVIE; |
| 560 | } else if (pdata->mode == USB_PERIPHERAL) { |
| 561 | ulpi_val = ULPI_INT_SESS_VALID; |
| 562 | val |= OTGSC_BSVIE; |
| 563 | } |
| 564 | writel_relaxed(val, USB_OTGSC); |
| 565 | ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE); |
| 566 | ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL); |
Pavankumar Kondeti | 446f454 | 2012-02-01 13:57:13 +0530 | [diff] [blame] | 567 | } else if (pdata->otg_control == OTG_PMIC_CONTROL) { |
| 568 | /* Enable PMIC pull-up */ |
| 569 | pm8xxx_usb_id_pullup(1); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 570 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 571 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 575 | static const char *timer_string(int bit) |
| 576 | { |
| 577 | switch (bit) { |
| 578 | case A_WAIT_VRISE: return "a_wait_vrise"; |
| 579 | case A_WAIT_VFALL: return "a_wait_vfall"; |
| 580 | case B_SRP_FAIL: return "b_srp_fail"; |
| 581 | case A_WAIT_BCON: return "a_wait_bcon"; |
| 582 | case A_AIDL_BDIS: return "a_aidl_bdis"; |
| 583 | case A_BIDL_ADIS: return "a_bidl_adis"; |
| 584 | case B_ASE0_BRST: return "b_ase0_brst"; |
| 585 | case A_TST_MAINT: return "a_tst_maint"; |
| 586 | case B_TST_SRP: return "b_tst_srp"; |
| 587 | case B_TST_CONFIG: return "b_tst_config"; |
| 588 | default: return "UNDEFINED"; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer) |
| 593 | { |
| 594 | struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer); |
| 595 | |
| 596 | switch (motg->active_tmout) { |
| 597 | case A_WAIT_VRISE: |
| 598 | /* TODO: use vbus_vld interrupt */ |
| 599 | set_bit(A_VBUS_VLD, &motg->inputs); |
| 600 | break; |
| 601 | case A_TST_MAINT: |
| 602 | /* OTG PET: End session after TA_TST_MAINT */ |
| 603 | set_bit(A_BUS_DROP, &motg->inputs); |
| 604 | break; |
| 605 | case B_TST_SRP: |
| 606 | /* |
| 607 | * OTG PET: Initiate SRP after TB_TST_SRP of |
| 608 | * previous session end. |
| 609 | */ |
| 610 | set_bit(B_BUS_REQ, &motg->inputs); |
| 611 | break; |
| 612 | case B_TST_CONFIG: |
| 613 | clear_bit(A_CONN, &motg->inputs); |
| 614 | break; |
| 615 | default: |
| 616 | set_bit(motg->active_tmout, &motg->tmouts); |
| 617 | } |
| 618 | |
| 619 | pr_debug("expired %s timer\n", timer_string(motg->active_tmout)); |
| 620 | queue_work(system_nrt_wq, &motg->sm_work); |
| 621 | return HRTIMER_NORESTART; |
| 622 | } |
| 623 | |
| 624 | static void msm_otg_del_timer(struct msm_otg *motg) |
| 625 | { |
| 626 | int bit = motg->active_tmout; |
| 627 | |
| 628 | pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit), |
| 629 | div_s64(ktime_to_us(hrtimer_get_remaining( |
| 630 | &motg->timer)), 1000)); |
| 631 | hrtimer_cancel(&motg->timer); |
| 632 | clear_bit(bit, &motg->tmouts); |
| 633 | } |
| 634 | |
| 635 | static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit) |
| 636 | { |
| 637 | clear_bit(bit, &motg->tmouts); |
| 638 | motg->active_tmout = bit; |
| 639 | pr_debug("starting %s timer\n", timer_string(bit)); |
| 640 | hrtimer_start(&motg->timer, |
| 641 | ktime_set(time / 1000, (time % 1000) * 1000000), |
| 642 | HRTIMER_MODE_REL); |
| 643 | } |
| 644 | |
| 645 | static void msm_otg_init_timer(struct msm_otg *motg) |
| 646 | { |
| 647 | hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 648 | motg->timer.function = msm_otg_timer_func; |
| 649 | } |
| 650 | |
| 651 | static int msm_otg_start_hnp(struct otg_transceiver *otg) |
| 652 | { |
| 653 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 654 | |
| 655 | if (otg->state != OTG_STATE_A_HOST) { |
| 656 | pr_err("HNP can not be initiated in %s state\n", |
| 657 | otg_state_string(otg->state)); |
| 658 | return -EINVAL; |
| 659 | } |
| 660 | |
| 661 | pr_debug("A-Host: HNP initiated\n"); |
| 662 | clear_bit(A_BUS_REQ, &motg->inputs); |
| 663 | queue_work(system_nrt_wq, &motg->sm_work); |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static int msm_otg_start_srp(struct otg_transceiver *otg) |
| 668 | { |
| 669 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 670 | u32 val; |
| 671 | int ret = 0; |
| 672 | |
| 673 | if (otg->state != OTG_STATE_B_IDLE) { |
| 674 | pr_err("SRP can not be initiated in %s state\n", |
| 675 | otg_state_string(otg->state)); |
| 676 | ret = -EINVAL; |
| 677 | goto out; |
| 678 | } |
| 679 | |
| 680 | if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) { |
| 681 | pr_debug("initial conditions of SRP are not met. Try again" |
| 682 | "after some time\n"); |
| 683 | ret = -EAGAIN; |
| 684 | goto out; |
| 685 | } |
| 686 | |
| 687 | pr_debug("B-Device SRP started\n"); |
| 688 | |
| 689 | /* |
| 690 | * PHY won't pull D+ high unless it detects Vbus valid. |
| 691 | * Since by definition, SRP is only done when Vbus is not valid, |
| 692 | * software work-around needs to be used to spoof the PHY into |
| 693 | * thinking it is valid. This can be done using the VBUSVLDEXTSEL and |
| 694 | * VBUSVLDEXT register bits. |
| 695 | */ |
| 696 | ulpi_write(otg, 0x03, 0x97); |
| 697 | /* |
| 698 | * Harware auto assist data pulsing: Data pulse is given |
| 699 | * for 7msec; wait for vbus |
| 700 | */ |
| 701 | val = readl_relaxed(USB_OTGSC); |
| 702 | writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC); |
| 703 | |
| 704 | /* VBUS plusing is obsoleted in OTG 2.0 supplement */ |
| 705 | out: |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | static void msm_otg_host_hnp_enable(struct otg_transceiver *otg, bool enable) |
| 710 | { |
| 711 | struct usb_hcd *hcd = bus_to_hcd(otg->host); |
| 712 | struct usb_device *rhub = otg->host->root_hub; |
| 713 | |
| 714 | if (enable) { |
| 715 | pm_runtime_disable(&rhub->dev); |
| 716 | rhub->state = USB_STATE_NOTATTACHED; |
| 717 | hcd->driver->bus_suspend(hcd); |
| 718 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
| 719 | } else { |
| 720 | usb_remove_hcd(hcd); |
| 721 | msm_otg_reset(otg); |
| 722 | usb_add_hcd(hcd, hcd->irq, IRQF_SHARED); |
| 723 | } |
| 724 | } |
| 725 | |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 726 | static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend) |
| 727 | { |
| 728 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 729 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 730 | if (aca_enabled() || (test_bit(ID, &motg->inputs) && |
| 731 | !test_bit(ID_A, &motg->inputs))) |
| 732 | return 0; |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 733 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 734 | if (suspend) { |
| 735 | switch (otg->state) { |
| 736 | case OTG_STATE_A_WAIT_BCON: |
| 737 | if (TA_WAIT_BCON > 0) |
| 738 | break; |
| 739 | /* fall through */ |
| 740 | case OTG_STATE_A_HOST: |
| 741 | pr_debug("host bus suspend\n"); |
| 742 | clear_bit(A_BUS_REQ, &motg->inputs); |
| 743 | queue_work(system_nrt_wq, &motg->sm_work); |
| 744 | break; |
| 745 | default: |
| 746 | break; |
| 747 | } |
| 748 | } else { |
| 749 | switch (otg->state) { |
| 750 | case OTG_STATE_A_SUSPEND: |
| 751 | /* Remote wakeup or resume */ |
| 752 | set_bit(A_BUS_REQ, &motg->inputs); |
| 753 | otg->state = OTG_STATE_A_HOST; |
| 754 | break; |
| 755 | default: |
| 756 | break; |
| 757 | } |
| 758 | } |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 762 | #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000) |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 763 | #define PHY_RESUME_TIMEOUT_USEC (100 * 1000) |
| 764 | |
| 765 | #ifdef CONFIG_PM_SLEEP |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 766 | static int msm_otg_suspend(struct msm_otg *motg) |
| 767 | { |
| 768 | struct otg_transceiver *otg = &motg->otg; |
| 769 | struct usb_bus *bus = otg->host; |
| 770 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 771 | int cnt = 0; |
Pavankumar Kondeti | 283146f | 2012-01-12 12:51:19 +0530 | [diff] [blame] | 772 | bool host_bus_suspend, dcp; |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 773 | u32 phy_ctrl_val = 0, cmd_val; |
Stephen Boyd | 30ad10b | 2012-03-01 14:51:04 -0800 | [diff] [blame] | 774 | unsigned ret; |
Rajkumar Raghupathy | 242565d | 2011-12-13 12:10:59 +0530 | [diff] [blame] | 775 | u32 portsc; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 776 | |
| 777 | if (atomic_read(&motg->in_lpm)) |
| 778 | return 0; |
| 779 | |
| 780 | disable_irq(motg->irq); |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 781 | host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs); |
Pavankumar Kondeti | 283146f | 2012-01-12 12:51:19 +0530 | [diff] [blame] | 782 | dcp = motg->chg_type == USB_DCP_CHARGER; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 783 | /* |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 784 | * Chipidea 45-nm PHY suspend sequence: |
| 785 | * |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 786 | * Interrupt Latch Register auto-clear feature is not present |
| 787 | * in all PHY versions. Latch register is clear on read type. |
| 788 | * Clear latch register to avoid spurious wakeup from |
| 789 | * low power mode (LPM). |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 790 | * |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 791 | * PHY comparators are disabled when PHY enters into low power |
| 792 | * mode (LPM). Keep PHY comparators ON in LPM only when we expect |
| 793 | * VBUS/Id notifications from USB PHY. Otherwise turn off USB |
| 794 | * PHY comparators. This save significant amount of power. |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 795 | * |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 796 | * PLL is not turned off when PHY enters into low power mode (LPM). |
| 797 | * Disable PLL for maximum power savings. |
| 798 | */ |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 799 | |
| 800 | if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) { |
| 801 | ulpi_read(otg, 0x14); |
| 802 | if (pdata->otg_control == OTG_PHY_CONTROL) |
| 803 | ulpi_write(otg, 0x01, 0x30); |
| 804 | ulpi_write(otg, 0x08, 0x09); |
| 805 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 806 | |
| 807 | /* |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 808 | * Turn off the OTG comparators, if depends on PMIC for |
| 809 | * VBUS and ID notifications. |
| 810 | */ |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 811 | if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 812 | ulpi_write(otg, OTG_COMP_DISABLE, |
| 813 | ULPI_SET(ULPI_PWR_CLK_MNG_REG)); |
| 814 | motg->lpm_flags |= PHY_OTG_COMP_DISABLED; |
| 815 | } |
| 816 | |
Rajkumar Raghupathy | 242565d | 2011-12-13 12:10:59 +0530 | [diff] [blame] | 817 | /* Set the PHCD bit, only if it is not set by the controller. |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 818 | * PHY may take some time or even fail to enter into low power |
| 819 | * mode (LPM). Hence poll for 500 msec and reset the PHY and link |
| 820 | * in failure case. |
| 821 | */ |
Rajkumar Raghupathy | 242565d | 2011-12-13 12:10:59 +0530 | [diff] [blame] | 822 | portsc = readl_relaxed(USB_PORTSC); |
| 823 | if (!(portsc & PORTSC_PHCD)) { |
| 824 | writel_relaxed(portsc | PORTSC_PHCD, |
| 825 | USB_PORTSC); |
| 826 | while (cnt < PHY_SUSPEND_TIMEOUT_USEC) { |
| 827 | if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD) |
| 828 | break; |
| 829 | udelay(1); |
| 830 | cnt++; |
| 831 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) { |
| 835 | dev_err(otg->dev, "Unable to suspend PHY\n"); |
| 836 | msm_otg_reset(otg); |
| 837 | enable_irq(motg->irq); |
| 838 | return -ETIMEDOUT; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * PHY has capability to generate interrupt asynchronously in low |
| 843 | * power mode (LPM). This interrupt is level triggered. So USB IRQ |
| 844 | * line must be disabled till async interrupt enable bit is cleared |
| 845 | * in USBCMD register. Assert STP (ULPI interface STOP signal) to |
| 846 | * block data communication from PHY. |
Pavankumar Kondeti | 6be675f | 2012-04-16 13:29:24 +0530 | [diff] [blame] | 847 | * |
| 848 | * PHY retention mode is disallowed while entering to LPM with wall |
| 849 | * charger connected. But PHY is put into suspend mode. Hence |
| 850 | * enable asynchronous interrupt to detect charger disconnection when |
| 851 | * PMIC notifications are unavailable. |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 852 | */ |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 853 | cmd_val = readl_relaxed(USB_USBCMD); |
Pavankumar Kondeti | 6be675f | 2012-04-16 13:29:24 +0530 | [diff] [blame] | 854 | if (host_bus_suspend || (motg->pdata->otg_control == OTG_PHY_CONTROL && |
| 855 | dcp)) |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 856 | cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL; |
| 857 | else |
| 858 | cmd_val |= ULPI_STP_CTRL; |
| 859 | writel_relaxed(cmd_val, USB_USBCMD); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 860 | |
Pavankumar Kondeti | 283146f | 2012-01-12 12:51:19 +0530 | [diff] [blame] | 861 | /* |
| 862 | * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP. |
| 863 | * PHY retention and collapse can not happen with VDP_SRC enabled. |
| 864 | */ |
| 865 | if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend && !dcp) { |
Amit Blay | 58b3147 | 2011-11-18 09:39:39 +0200 | [diff] [blame] | 866 | phy_ctrl_val = readl_relaxed(USB_PHY_CTRL); |
| 867 | if (motg->pdata->otg_control == OTG_PHY_CONTROL) |
| 868 | /* Enable PHY HV interrupts to wake MPM/Link */ |
| 869 | phy_ctrl_val |= |
| 870 | (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN); |
| 871 | |
| 872 | writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 873 | motg->lpm_flags |= PHY_RETENTIONED; |
| 874 | } |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 875 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 876 | /* Ensure that above operation is completed before turning off clocks */ |
| 877 | mb(); |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 878 | clk_disable_unprepare(motg->pclk); |
| 879 | clk_disable_unprepare(motg->core_clk); |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 880 | |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 881 | /* usb phy no more require TCXO clock, hence vote for TCXO disable */ |
Stephen Boyd | 30ad10b | 2012-03-01 14:51:04 -0800 | [diff] [blame] | 882 | ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF); |
| 883 | if (ret) |
| 884 | dev_err(otg->dev, "%s failed to devote for " |
| 885 | "TCXO D0 buffer%d\n", __func__, ret); |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 886 | |
Pavankumar Kondeti | 283146f | 2012-01-12 12:51:19 +0530 | [diff] [blame] | 887 | if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && |
| 888 | !host_bus_suspend && !dcp) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 889 | msm_hsusb_ldo_enable(motg, 0); |
| 890 | motg->lpm_flags |= PHY_PWR_COLLAPSED; |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 891 | } |
| 892 | |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 893 | if (motg->lpm_flags & PHY_RETENTIONED) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 894 | msm_hsusb_config_vddcx(0); |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 895 | msm_hsusb_mhl_switch_enable(motg, 0); |
| 896 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 897 | |
| 898 | if (device_may_wakeup(otg->dev)) { |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 899 | enable_irq_wake(motg->irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 900 | if (motg->pdata->pmic_id_irq) |
| 901 | enable_irq_wake(motg->pdata->pmic_id_irq); |
| 902 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 903 | if (bus) |
| 904 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags); |
| 905 | |
| 906 | atomic_set(&motg->in_lpm, 1); |
| 907 | enable_irq(motg->irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 908 | wake_unlock(&motg->wlock); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 909 | |
| 910 | dev_info(otg->dev, "USB in low power mode\n"); |
| 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 915 | static int msm_otg_resume(struct msm_otg *motg) |
| 916 | { |
| 917 | struct otg_transceiver *otg = &motg->otg; |
| 918 | struct usb_bus *bus = otg->host; |
| 919 | int cnt = 0; |
| 920 | unsigned temp; |
Amit Blay | 58b3147 | 2011-11-18 09:39:39 +0200 | [diff] [blame] | 921 | u32 phy_ctrl_val = 0; |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 922 | unsigned ret; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 923 | |
| 924 | if (!atomic_read(&motg->in_lpm)) |
| 925 | return 0; |
| 926 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 927 | wake_lock(&motg->wlock); |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 928 | |
| 929 | /* Vote for TCXO when waking up the phy */ |
Stephen Boyd | 30ad10b | 2012-03-01 14:51:04 -0800 | [diff] [blame] | 930 | ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON); |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 931 | if (ret) |
| 932 | dev_err(otg->dev, "%s failed to vote for " |
| 933 | "TCXO D0 buffer%d\n", __func__, ret); |
| 934 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 935 | clk_prepare_enable(motg->core_clk); |
Amit Blay | 137575f | 2011-11-06 15:20:54 +0200 | [diff] [blame] | 936 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 937 | clk_prepare_enable(motg->pclk); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 938 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 939 | if (motg->lpm_flags & PHY_PWR_COLLAPSED) { |
| 940 | msm_hsusb_ldo_enable(motg, 1); |
| 941 | motg->lpm_flags &= ~PHY_PWR_COLLAPSED; |
| 942 | } |
| 943 | |
| 944 | if (motg->lpm_flags & PHY_RETENTIONED) { |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 945 | msm_hsusb_mhl_switch_enable(motg, 1); |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 946 | msm_hsusb_config_vddcx(1); |
Amit Blay | 58b3147 | 2011-11-18 09:39:39 +0200 | [diff] [blame] | 947 | phy_ctrl_val = readl_relaxed(USB_PHY_CTRL); |
| 948 | phy_ctrl_val |= PHY_RETEN; |
| 949 | if (motg->pdata->otg_control == OTG_PHY_CONTROL) |
| 950 | /* Disable PHY HV interrupts */ |
| 951 | phy_ctrl_val &= |
| 952 | ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN); |
| 953 | writel_relaxed(phy_ctrl_val, USB_PHY_CTRL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 954 | motg->lpm_flags &= ~PHY_RETENTIONED; |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 955 | } |
| 956 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 957 | temp = readl(USB_USBCMD); |
| 958 | temp &= ~ASYNC_INTR_CTRL; |
| 959 | temp &= ~ULPI_STP_CTRL; |
| 960 | writel(temp, USB_USBCMD); |
| 961 | |
| 962 | /* |
| 963 | * PHY comes out of low power mode (LPM) in case of wakeup |
| 964 | * from asynchronous interrupt. |
| 965 | */ |
| 966 | if (!(readl(USB_PORTSC) & PORTSC_PHCD)) |
| 967 | goto skip_phy_resume; |
| 968 | |
| 969 | writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC); |
| 970 | while (cnt < PHY_RESUME_TIMEOUT_USEC) { |
| 971 | if (!(readl(USB_PORTSC) & PORTSC_PHCD)) |
| 972 | break; |
| 973 | udelay(1); |
| 974 | cnt++; |
| 975 | } |
| 976 | |
| 977 | if (cnt >= PHY_RESUME_TIMEOUT_USEC) { |
| 978 | /* |
| 979 | * This is a fatal error. Reset the link and |
| 980 | * PHY. USB state can not be restored. Re-insertion |
| 981 | * of USB cable is the only way to get USB working. |
| 982 | */ |
| 983 | dev_err(otg->dev, "Unable to resume USB." |
| 984 | "Re-plugin the cable\n"); |
| 985 | msm_otg_reset(otg); |
| 986 | } |
| 987 | |
| 988 | skip_phy_resume: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 989 | /* Turn on the OTG comparators on resume */ |
| 990 | if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) { |
| 991 | ulpi_write(otg, OTG_COMP_DISABLE, |
| 992 | ULPI_CLR(ULPI_PWR_CLK_MNG_REG)); |
| 993 | motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED; |
| 994 | } |
| 995 | if (device_may_wakeup(otg->dev)) { |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 996 | disable_irq_wake(motg->irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 997 | if (motg->pdata->pmic_id_irq) |
| 998 | disable_irq_wake(motg->pdata->pmic_id_irq); |
| 999 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1000 | if (bus) |
| 1001 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags); |
| 1002 | |
Pavankumar Kondeti | 2ce2c3a | 2011-05-02 11:56:33 +0530 | [diff] [blame] | 1003 | atomic_set(&motg->in_lpm, 0); |
| 1004 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1005 | if (motg->async_int) { |
| 1006 | motg->async_int = 0; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1007 | enable_irq(motg->irq); |
| 1008 | } |
| 1009 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1010 | dev_info(otg->dev, "USB exited from low power mode\n"); |
| 1011 | |
| 1012 | return 0; |
| 1013 | } |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 1014 | #endif |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1015 | |
David Keitel | 081a3e2 | 2012-04-18 12:37:07 -0700 | [diff] [blame] | 1016 | static int msm_otg_notify_chg_type(struct msm_otg *motg) |
| 1017 | { |
| 1018 | static int charger_type; |
| 1019 | /* |
| 1020 | * TODO |
| 1021 | * Unify OTG driver charger types and power supply charger types |
| 1022 | */ |
| 1023 | if (charger_type == motg->chg_type) |
| 1024 | return 0; |
| 1025 | |
| 1026 | if (motg->chg_type == USB_SDP_CHARGER) |
| 1027 | charger_type = POWER_SUPPLY_TYPE_USB; |
| 1028 | else if (motg->chg_type == USB_CDP_CHARGER) |
| 1029 | charger_type = POWER_SUPPLY_TYPE_USB_CDP; |
| 1030 | else if (motg->chg_type == USB_DCP_CHARGER) |
| 1031 | charger_type = POWER_SUPPLY_TYPE_USB_DCP; |
| 1032 | else if ((motg->chg_type == USB_ACA_DOCK_CHARGER || |
| 1033 | motg->chg_type == USB_ACA_A_CHARGER || |
| 1034 | motg->chg_type == USB_ACA_B_CHARGER || |
| 1035 | motg->chg_type == USB_ACA_C_CHARGER)) |
| 1036 | charger_type = POWER_SUPPLY_TYPE_USB_ACA; |
| 1037 | else |
| 1038 | charger_type = POWER_SUPPLY_TYPE_BATTERY; |
| 1039 | |
| 1040 | return pm8921_set_usb_power_supply_type(charger_type); |
| 1041 | } |
| 1042 | |
Amit Blay | 0f7edf7 | 2012-01-15 10:11:27 +0200 | [diff] [blame] | 1043 | static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA) |
| 1044 | { |
| 1045 | struct power_supply *psy; |
| 1046 | |
| 1047 | psy = power_supply_get_by_name("usb"); |
| 1048 | if (!psy) |
| 1049 | goto psy_not_supported; |
| 1050 | |
| 1051 | if (motg->cur_power == 0 && mA > 0) { |
| 1052 | /* Enable charging */ |
| 1053 | if (power_supply_set_online(psy, true)) |
| 1054 | goto psy_not_supported; |
| 1055 | } else if (motg->cur_power > 0 && mA == 0) { |
| 1056 | /* Disable charging */ |
| 1057 | if (power_supply_set_online(psy, false)) |
| 1058 | goto psy_not_supported; |
| 1059 | return 0; |
| 1060 | } |
| 1061 | /* Set max current limit */ |
| 1062 | if (power_supply_set_current_limit(psy, 1000*mA)) |
| 1063 | goto psy_not_supported; |
| 1064 | |
| 1065 | return 0; |
| 1066 | |
| 1067 | psy_not_supported: |
| 1068 | dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n"); |
| 1069 | return -ENXIO; |
| 1070 | } |
| 1071 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1072 | static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA) |
| 1073 | { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1074 | struct usb_gadget *g = motg->otg.gadget; |
| 1075 | |
| 1076 | if (g && g->is_a_peripheral) |
| 1077 | return; |
| 1078 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1079 | if ((motg->chg_type == USB_ACA_DOCK_CHARGER || |
| 1080 | motg->chg_type == USB_ACA_A_CHARGER || |
| 1081 | motg->chg_type == USB_ACA_B_CHARGER || |
| 1082 | motg->chg_type == USB_ACA_C_CHARGER) && |
| 1083 | mA > IDEV_ACA_CHG_LIMIT) |
| 1084 | mA = IDEV_ACA_CHG_LIMIT; |
| 1085 | |
David Keitel | 081a3e2 | 2012-04-18 12:37:07 -0700 | [diff] [blame] | 1086 | if (msm_otg_notify_chg_type(motg)) |
| 1087 | dev_err(motg->otg.dev, |
| 1088 | "Failed notifying %d charger type to PMIC\n", |
| 1089 | motg->chg_type); |
| 1090 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1091 | if (motg->cur_power == mA) |
| 1092 | return; |
| 1093 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1094 | dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA); |
Amit Blay | 0f7edf7 | 2012-01-15 10:11:27 +0200 | [diff] [blame] | 1095 | |
| 1096 | /* |
| 1097 | * Use Power Supply API if supported, otherwise fallback |
| 1098 | * to legacy pm8921 API. |
| 1099 | */ |
| 1100 | if (msm_otg_notify_power_supply(motg, mA)) |
| 1101 | pm8921_charger_vbus_draw(mA); |
| 1102 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1103 | motg->cur_power = mA; |
| 1104 | } |
| 1105 | |
| 1106 | static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA) |
| 1107 | { |
| 1108 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 1109 | |
| 1110 | /* |
| 1111 | * Gadget driver uses set_power method to notify about the |
| 1112 | * available current based on suspend/configured states. |
| 1113 | * |
| 1114 | * IDEV_CHG can be drawn irrespective of suspend/un-configured |
| 1115 | * states when CDP/ACA is connected. |
| 1116 | */ |
| 1117 | if (motg->chg_type == USB_SDP_CHARGER) |
| 1118 | msm_otg_notify_charger(motg, mA); |
| 1119 | |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1123 | static void msm_otg_start_host(struct otg_transceiver *otg, int on) |
| 1124 | { |
| 1125 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 1126 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 1127 | struct usb_hcd *hcd; |
| 1128 | |
| 1129 | if (!otg->host) |
| 1130 | return; |
| 1131 | |
| 1132 | hcd = bus_to_hcd(otg->host); |
| 1133 | |
| 1134 | if (on) { |
| 1135 | dev_dbg(otg->dev, "host on\n"); |
| 1136 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1137 | /* |
| 1138 | * Some boards have a switch cotrolled by gpio |
| 1139 | * to enable/disable internal HUB. Enable internal |
| 1140 | * HUB before kicking the host. |
| 1141 | */ |
| 1142 | if (pdata->setup_gpio) |
| 1143 | pdata->setup_gpio(OTG_STATE_A_HOST); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1144 | usb_add_hcd(hcd, hcd->irq, IRQF_SHARED); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1145 | } else { |
| 1146 | dev_dbg(otg->dev, "host off\n"); |
| 1147 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1148 | usb_remove_hcd(hcd); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1149 | /* HCD core reset all bits of PORTSC. select ULPI phy */ |
| 1150 | writel_relaxed(0x80000000, USB_PORTSC); |
| 1151 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1152 | if (pdata->setup_gpio) |
| 1153 | pdata->setup_gpio(OTG_STATE_UNDEFINED); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1157 | static int msm_otg_usbdev_notify(struct notifier_block *self, |
| 1158 | unsigned long action, void *priv) |
| 1159 | { |
| 1160 | struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1161 | struct otg_transceiver *otg = &motg->otg; |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1162 | struct usb_device *udev = priv; |
| 1163 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1164 | if (action == USB_BUS_ADD || action == USB_BUS_REMOVE) |
| 1165 | goto out; |
| 1166 | |
| 1167 | if (udev->bus != motg->otg.host) |
| 1168 | goto out; |
| 1169 | /* |
| 1170 | * Interested in devices connected directly to the root hub. |
| 1171 | * ACA dock can supply IDEV_CHG irrespective devices connected |
| 1172 | * on the accessory port. |
| 1173 | */ |
| 1174 | if (!udev->parent || udev->parent->parent || |
| 1175 | motg->chg_type == USB_ACA_DOCK_CHARGER) |
| 1176 | goto out; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1177 | |
| 1178 | switch (action) { |
| 1179 | case USB_DEVICE_ADD: |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1180 | if (aca_enabled()) |
| 1181 | usb_disable_autosuspend(udev); |
| 1182 | if (otg->state == OTG_STATE_A_WAIT_BCON) { |
| 1183 | pr_debug("B_CONN set\n"); |
| 1184 | set_bit(B_CONN, &motg->inputs); |
| 1185 | msm_otg_del_timer(motg); |
| 1186 | otg->state = OTG_STATE_A_HOST; |
| 1187 | /* |
| 1188 | * OTG PET: A-device must end session within |
| 1189 | * 10 sec after PET enumeration. |
| 1190 | */ |
| 1191 | if (udev->quirks & USB_QUIRK_OTG_PET) |
| 1192 | msm_otg_start_timer(motg, TA_TST_MAINT, |
| 1193 | A_TST_MAINT); |
| 1194 | } |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1195 | /* fall through */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1196 | case USB_DEVICE_CONFIG: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1197 | if (udev->actconfig) |
| 1198 | motg->mA_port = udev->actconfig->desc.bMaxPower * 2; |
| 1199 | else |
| 1200 | motg->mA_port = IUNIT; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1201 | if (otg->state == OTG_STATE_B_HOST) |
| 1202 | msm_otg_del_timer(motg); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1203 | break; |
| 1204 | case USB_DEVICE_REMOVE: |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1205 | if ((otg->state == OTG_STATE_A_HOST) || |
| 1206 | (otg->state == OTG_STATE_A_SUSPEND)) { |
| 1207 | pr_debug("B_CONN clear\n"); |
| 1208 | clear_bit(B_CONN, &motg->inputs); |
| 1209 | /* |
| 1210 | * OTG PET: A-device must end session after |
| 1211 | * PET disconnection if it is enumerated |
| 1212 | * with bcdDevice[0] = 1. USB core sets |
| 1213 | * bus->otg_vbus_off for us. clear it here. |
| 1214 | */ |
| 1215 | if (udev->bus->otg_vbus_off) { |
| 1216 | udev->bus->otg_vbus_off = 0; |
| 1217 | set_bit(A_BUS_DROP, &motg->inputs); |
| 1218 | } |
| 1219 | queue_work(system_nrt_wq, &motg->sm_work); |
| 1220 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1221 | default: |
| 1222 | break; |
| 1223 | } |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1224 | if (test_bit(ID_A, &motg->inputs)) |
| 1225 | msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX - |
| 1226 | motg->mA_port); |
| 1227 | out: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1228 | return NOTIFY_OK; |
| 1229 | } |
| 1230 | |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 1231 | static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on) |
| 1232 | { |
| 1233 | int ret; |
| 1234 | static bool vbus_is_on; |
| 1235 | |
| 1236 | if (vbus_is_on == on) |
| 1237 | return; |
| 1238 | |
| 1239 | if (motg->pdata->vbus_power) { |
Mayank Rana | 91f597e | 2012-01-20 10:12:06 +0530 | [diff] [blame] | 1240 | ret = motg->pdata->vbus_power(on); |
| 1241 | if (!ret) |
| 1242 | vbus_is_on = on; |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 1243 | return; |
| 1244 | } |
| 1245 | |
| 1246 | if (!vbus_otg) { |
| 1247 | pr_err("vbus_otg is NULL."); |
| 1248 | return; |
| 1249 | } |
| 1250 | |
Abhijeet Dharmapurikar | be05488 | 2012-01-03 20:27:07 -0800 | [diff] [blame] | 1251 | /* |
| 1252 | * if entering host mode tell the charger to not draw any current |
| 1253 | * from usb - if exiting host mode let the charger draw current |
| 1254 | */ |
| 1255 | pm8921_disable_source_current(on); |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 1256 | if (on) { |
| 1257 | ret = regulator_enable(vbus_otg); |
| 1258 | if (ret) { |
| 1259 | pr_err("unable to enable vbus_otg\n"); |
| 1260 | return; |
| 1261 | } |
| 1262 | vbus_is_on = true; |
| 1263 | } else { |
| 1264 | ret = regulator_disable(vbus_otg); |
| 1265 | if (ret) { |
| 1266 | pr_err("unable to disable vbus_otg\n"); |
| 1267 | return; |
| 1268 | } |
| 1269 | vbus_is_on = false; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1273 | static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host) |
| 1274 | { |
| 1275 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 1276 | struct usb_hcd *hcd; |
| 1277 | |
| 1278 | /* |
| 1279 | * Fail host registration if this board can support |
| 1280 | * only peripheral configuration. |
| 1281 | */ |
| 1282 | if (motg->pdata->mode == USB_PERIPHERAL) { |
| 1283 | dev_info(otg->dev, "Host mode is not supported\n"); |
| 1284 | return -ENODEV; |
| 1285 | } |
| 1286 | |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 1287 | if (!motg->pdata->vbus_power && host) { |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 1288 | vbus_otg = devm_regulator_get(motg->otg.dev, "vbus_otg"); |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 1289 | if (IS_ERR(vbus_otg)) { |
| 1290 | pr_err("Unable to get vbus_otg\n"); |
| 1291 | return -ENODEV; |
| 1292 | } |
| 1293 | } |
| 1294 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1295 | if (!host) { |
| 1296 | if (otg->state == OTG_STATE_A_HOST) { |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1297 | pm_runtime_get_sync(otg->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1298 | usb_unregister_notify(&motg->usbdev_nb); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1299 | msm_otg_start_host(otg, 0); |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 1300 | msm_hsusb_vbus_power(motg, 0); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1301 | otg->host = NULL; |
| 1302 | otg->state = OTG_STATE_UNDEFINED; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1303 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1304 | } else { |
| 1305 | otg->host = NULL; |
| 1306 | } |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | hcd = bus_to_hcd(host); |
| 1312 | hcd->power_budget = motg->pdata->power_budget; |
| 1313 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1314 | #ifdef CONFIG_USB_OTG |
| 1315 | host->otg_port = 1; |
| 1316 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1317 | motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify; |
| 1318 | usb_register_notify(&motg->usbdev_nb); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1319 | otg->host = host; |
| 1320 | dev_dbg(otg->dev, "host driver registered w/ tranceiver\n"); |
| 1321 | |
| 1322 | /* |
| 1323 | * Kick the state machine work, if peripheral is not supported |
| 1324 | * or peripheral is already registered with us. |
| 1325 | */ |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1326 | if (motg->pdata->mode == USB_HOST || otg->gadget) { |
| 1327 | pm_runtime_get_sync(otg->dev); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1328 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1329 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on) |
| 1335 | { |
Manu Gautam | cd82e9d | 2011-12-20 14:17:28 +0530 | [diff] [blame] | 1336 | int ret; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1337 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 1338 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 1339 | |
| 1340 | if (!otg->gadget) |
| 1341 | return; |
| 1342 | |
| 1343 | if (on) { |
| 1344 | dev_dbg(otg->dev, "gadget on\n"); |
| 1345 | /* |
| 1346 | * Some boards have a switch cotrolled by gpio |
| 1347 | * to enable/disable internal HUB. Disable internal |
| 1348 | * HUB before kicking the gadget. |
| 1349 | */ |
| 1350 | if (pdata->setup_gpio) |
| 1351 | pdata->setup_gpio(OTG_STATE_B_PERIPHERAL); |
Anji jonnala | a7c1c5c | 2011-12-12 12:20:36 +0530 | [diff] [blame] | 1352 | /* |
| 1353 | * vote for minimum dma_latency to prevent idle |
| 1354 | * power collapse(pc) while running in peripheral mode. |
| 1355 | */ |
| 1356 | otg_pm_qos_update_latency(motg, 1); |
Manu Gautam | cd82e9d | 2011-12-20 14:17:28 +0530 | [diff] [blame] | 1357 | /* Configure BUS performance parameters for MAX bandwidth */ |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 1358 | if (motg->bus_perf_client && debug_bus_voting_enabled) { |
Manu Gautam | cd82e9d | 2011-12-20 14:17:28 +0530 | [diff] [blame] | 1359 | ret = msm_bus_scale_client_update_request( |
| 1360 | motg->bus_perf_client, 1); |
| 1361 | if (ret) |
| 1362 | dev_err(motg->otg.dev, "%s: Failed to vote for " |
| 1363 | "bus bandwidth %d\n", __func__, ret); |
| 1364 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1365 | usb_gadget_vbus_connect(otg->gadget); |
| 1366 | } else { |
| 1367 | dev_dbg(otg->dev, "gadget off\n"); |
| 1368 | usb_gadget_vbus_disconnect(otg->gadget); |
Anji jonnala | a7c1c5c | 2011-12-12 12:20:36 +0530 | [diff] [blame] | 1369 | otg_pm_qos_update_latency(motg, 0); |
Manu Gautam | cd82e9d | 2011-12-20 14:17:28 +0530 | [diff] [blame] | 1370 | /* Configure BUS performance parameters to default */ |
| 1371 | if (motg->bus_perf_client) { |
| 1372 | ret = msm_bus_scale_client_update_request( |
| 1373 | motg->bus_perf_client, 0); |
| 1374 | if (ret) |
| 1375 | dev_err(motg->otg.dev, "%s: Failed to devote " |
| 1376 | "for bus bw %d\n", __func__, ret); |
| 1377 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1378 | if (pdata->setup_gpio) |
| 1379 | pdata->setup_gpio(OTG_STATE_UNDEFINED); |
| 1380 | } |
| 1381 | |
| 1382 | } |
| 1383 | |
| 1384 | static int msm_otg_set_peripheral(struct otg_transceiver *otg, |
| 1385 | struct usb_gadget *gadget) |
| 1386 | { |
| 1387 | struct msm_otg *motg = container_of(otg, struct msm_otg, otg); |
| 1388 | |
| 1389 | /* |
| 1390 | * Fail peripheral registration if this board can support |
| 1391 | * only host configuration. |
| 1392 | */ |
| 1393 | if (motg->pdata->mode == USB_HOST) { |
| 1394 | dev_info(otg->dev, "Peripheral mode is not supported\n"); |
| 1395 | return -ENODEV; |
| 1396 | } |
| 1397 | |
| 1398 | if (!gadget) { |
| 1399 | if (otg->state == OTG_STATE_B_PERIPHERAL) { |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1400 | pm_runtime_get_sync(otg->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1401 | msm_otg_start_peripheral(otg, 0); |
| 1402 | otg->gadget = NULL; |
| 1403 | otg->state = OTG_STATE_UNDEFINED; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1404 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1405 | } else { |
| 1406 | otg->gadget = NULL; |
| 1407 | } |
| 1408 | |
| 1409 | return 0; |
| 1410 | } |
| 1411 | otg->gadget = gadget; |
| 1412 | dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n"); |
| 1413 | |
| 1414 | /* |
| 1415 | * Kick the state machine work, if host is not supported |
| 1416 | * or host is already registered with us. |
| 1417 | */ |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1418 | if (motg->pdata->mode == USB_PERIPHERAL || otg->host) { |
| 1419 | pm_runtime_get_sync(otg->dev); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1420 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1421 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1422 | |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1426 | static bool msm_chg_aca_detect(struct msm_otg *motg) |
| 1427 | { |
| 1428 | struct otg_transceiver *otg = &motg->otg; |
| 1429 | u32 int_sts; |
| 1430 | bool ret = false; |
| 1431 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1432 | if (!aca_enabled()) |
| 1433 | goto out; |
| 1434 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1435 | if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) |
| 1436 | goto out; |
| 1437 | |
| 1438 | int_sts = ulpi_read(otg, 0x87); |
| 1439 | switch (int_sts & 0x1C) { |
| 1440 | case 0x08: |
| 1441 | if (!test_and_set_bit(ID_A, &motg->inputs)) { |
| 1442 | dev_dbg(otg->dev, "ID_A\n"); |
| 1443 | motg->chg_type = USB_ACA_A_CHARGER; |
| 1444 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1445 | clear_bit(ID_B, &motg->inputs); |
| 1446 | clear_bit(ID_C, &motg->inputs); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1447 | set_bit(ID, &motg->inputs); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1448 | ret = true; |
| 1449 | } |
| 1450 | break; |
| 1451 | case 0x0C: |
| 1452 | if (!test_and_set_bit(ID_B, &motg->inputs)) { |
| 1453 | dev_dbg(otg->dev, "ID_B\n"); |
| 1454 | motg->chg_type = USB_ACA_B_CHARGER; |
| 1455 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1456 | clear_bit(ID_A, &motg->inputs); |
| 1457 | clear_bit(ID_C, &motg->inputs); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1458 | set_bit(ID, &motg->inputs); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1459 | ret = true; |
| 1460 | } |
| 1461 | break; |
| 1462 | case 0x10: |
| 1463 | if (!test_and_set_bit(ID_C, &motg->inputs)) { |
| 1464 | dev_dbg(otg->dev, "ID_C\n"); |
| 1465 | motg->chg_type = USB_ACA_C_CHARGER; |
| 1466 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1467 | clear_bit(ID_A, &motg->inputs); |
| 1468 | clear_bit(ID_B, &motg->inputs); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1469 | set_bit(ID, &motg->inputs); |
| 1470 | ret = true; |
| 1471 | } |
| 1472 | break; |
| 1473 | case 0x04: |
| 1474 | if (test_and_clear_bit(ID, &motg->inputs)) { |
| 1475 | dev_dbg(otg->dev, "ID_GND\n"); |
| 1476 | motg->chg_type = USB_INVALID_CHARGER; |
| 1477 | motg->chg_state = USB_CHG_STATE_UNDEFINED; |
| 1478 | clear_bit(ID_A, &motg->inputs); |
| 1479 | clear_bit(ID_B, &motg->inputs); |
| 1480 | clear_bit(ID_C, &motg->inputs); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1481 | ret = true; |
| 1482 | } |
| 1483 | break; |
| 1484 | default: |
| 1485 | ret = test_and_clear_bit(ID_A, &motg->inputs) | |
| 1486 | test_and_clear_bit(ID_B, &motg->inputs) | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1487 | test_and_clear_bit(ID_C, &motg->inputs) | |
| 1488 | !test_and_set_bit(ID, &motg->inputs); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1489 | if (ret) { |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1490 | dev_dbg(otg->dev, "ID A/B/C/GND is no more\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1491 | motg->chg_type = USB_INVALID_CHARGER; |
| 1492 | motg->chg_state = USB_CHG_STATE_UNDEFINED; |
| 1493 | } |
| 1494 | } |
| 1495 | out: |
| 1496 | return ret; |
| 1497 | } |
| 1498 | |
| 1499 | static void msm_chg_enable_aca_det(struct msm_otg *motg) |
| 1500 | { |
| 1501 | struct otg_transceiver *otg = &motg->otg; |
| 1502 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1503 | if (!aca_enabled()) |
| 1504 | return; |
| 1505 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1506 | switch (motg->pdata->phy_type) { |
| 1507 | case SNPS_28NM_INTEGRATED_PHY: |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1508 | /* Disable ID_GND in link and PHY */ |
| 1509 | writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU | |
| 1510 | OTGSC_IDIE), USB_OTGSC); |
| 1511 | ulpi_write(otg, 0x01, 0x0C); |
| 1512 | ulpi_write(otg, 0x10, 0x0F); |
| 1513 | ulpi_write(otg, 0x10, 0x12); |
Pavankumar Kondeti | 446f454 | 2012-02-01 13:57:13 +0530 | [diff] [blame] | 1514 | /* Disable PMIC ID pull-up */ |
| 1515 | pm8xxx_usb_id_pullup(0); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1516 | /* Enable ACA ID detection */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1517 | ulpi_write(otg, 0x20, 0x85); |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 1518 | aca_id_turned_on = true; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1519 | break; |
| 1520 | default: |
| 1521 | break; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | static void msm_chg_enable_aca_intr(struct msm_otg *motg) |
| 1526 | { |
| 1527 | struct otg_transceiver *otg = &motg->otg; |
| 1528 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1529 | if (!aca_enabled()) |
| 1530 | return; |
| 1531 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1532 | switch (motg->pdata->phy_type) { |
| 1533 | case SNPS_28NM_INTEGRATED_PHY: |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1534 | /* Enable ACA Detection interrupt (on any RID change) */ |
| 1535 | ulpi_write(otg, 0x01, 0x94); |
| 1536 | break; |
| 1537 | default: |
| 1538 | break; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | static void msm_chg_disable_aca_intr(struct msm_otg *motg) |
| 1543 | { |
| 1544 | struct otg_transceiver *otg = &motg->otg; |
| 1545 | |
| 1546 | if (!aca_enabled()) |
| 1547 | return; |
| 1548 | |
| 1549 | switch (motg->pdata->phy_type) { |
| 1550 | case SNPS_28NM_INTEGRATED_PHY: |
| 1551 | ulpi_write(otg, 0x01, 0x95); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1552 | break; |
| 1553 | default: |
| 1554 | break; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | static bool msm_chg_check_aca_intr(struct msm_otg *motg) |
| 1559 | { |
| 1560 | struct otg_transceiver *otg = &motg->otg; |
| 1561 | bool ret = false; |
| 1562 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1563 | if (!aca_enabled()) |
| 1564 | return ret; |
| 1565 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1566 | switch (motg->pdata->phy_type) { |
| 1567 | case SNPS_28NM_INTEGRATED_PHY: |
| 1568 | if (ulpi_read(otg, 0x91) & 1) { |
| 1569 | dev_dbg(otg->dev, "RID change\n"); |
| 1570 | ulpi_write(otg, 0x01, 0x92); |
| 1571 | ret = msm_chg_aca_detect(motg); |
| 1572 | } |
| 1573 | default: |
| 1574 | break; |
| 1575 | } |
| 1576 | return ret; |
| 1577 | } |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1578 | |
| 1579 | static void msm_otg_id_timer_func(unsigned long data) |
| 1580 | { |
| 1581 | struct msm_otg *motg = (struct msm_otg *) data; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1582 | struct otg_transceiver *otg = &motg->otg; |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1583 | |
| 1584 | if (!aca_enabled()) |
| 1585 | return; |
| 1586 | |
| 1587 | if (atomic_read(&motg->in_lpm)) { |
| 1588 | dev_dbg(motg->otg.dev, "timer: in lpm\n"); |
| 1589 | return; |
| 1590 | } |
| 1591 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1592 | if (otg->state == OTG_STATE_A_SUSPEND) |
| 1593 | goto out; |
| 1594 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1595 | if (msm_chg_check_aca_intr(motg)) { |
| 1596 | dev_dbg(motg->otg.dev, "timer: aca work\n"); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1597 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1598 | } |
| 1599 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1600 | out: |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1601 | if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) |
| 1602 | mod_timer(&motg->id_timer, ID_TIMER_FREQ); |
| 1603 | } |
| 1604 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1605 | static bool msm_chg_check_secondary_det(struct msm_otg *motg) |
| 1606 | { |
| 1607 | struct otg_transceiver *otg = &motg->otg; |
| 1608 | u32 chg_det; |
| 1609 | bool ret = false; |
| 1610 | |
| 1611 | switch (motg->pdata->phy_type) { |
| 1612 | case CI_45NM_INTEGRATED_PHY: |
| 1613 | chg_det = ulpi_read(otg, 0x34); |
| 1614 | ret = chg_det & (1 << 4); |
| 1615 | break; |
| 1616 | case SNPS_28NM_INTEGRATED_PHY: |
| 1617 | chg_det = ulpi_read(otg, 0x87); |
| 1618 | ret = chg_det & 1; |
| 1619 | break; |
| 1620 | default: |
| 1621 | break; |
| 1622 | } |
| 1623 | return ret; |
| 1624 | } |
| 1625 | |
| 1626 | static void msm_chg_enable_secondary_det(struct msm_otg *motg) |
| 1627 | { |
| 1628 | struct otg_transceiver *otg = &motg->otg; |
| 1629 | u32 chg_det; |
| 1630 | |
| 1631 | switch (motg->pdata->phy_type) { |
| 1632 | case CI_45NM_INTEGRATED_PHY: |
| 1633 | chg_det = ulpi_read(otg, 0x34); |
| 1634 | /* Turn off charger block */ |
| 1635 | chg_det |= ~(1 << 1); |
| 1636 | ulpi_write(otg, chg_det, 0x34); |
| 1637 | udelay(20); |
| 1638 | /* control chg block via ULPI */ |
| 1639 | chg_det &= ~(1 << 3); |
| 1640 | ulpi_write(otg, chg_det, 0x34); |
| 1641 | /* put it in host mode for enabling D- source */ |
| 1642 | chg_det &= ~(1 << 2); |
| 1643 | ulpi_write(otg, chg_det, 0x34); |
| 1644 | /* Turn on chg detect block */ |
| 1645 | chg_det &= ~(1 << 1); |
| 1646 | ulpi_write(otg, chg_det, 0x34); |
| 1647 | udelay(20); |
| 1648 | /* enable chg detection */ |
| 1649 | chg_det &= ~(1 << 0); |
| 1650 | ulpi_write(otg, chg_det, 0x34); |
| 1651 | break; |
| 1652 | case SNPS_28NM_INTEGRATED_PHY: |
Pavankumar Kondeti | 283146f | 2012-01-12 12:51:19 +0530 | [diff] [blame] | 1653 | /* Turn off VDP_SRC */ |
| 1654 | ulpi_write(otg, 0x3, 0x86); |
| 1655 | msleep(20); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1656 | /* |
| 1657 | * Configure DM as current source, DP as current sink |
| 1658 | * and enable battery charging comparators. |
| 1659 | */ |
| 1660 | ulpi_write(otg, 0x8, 0x85); |
| 1661 | ulpi_write(otg, 0x2, 0x85); |
| 1662 | ulpi_write(otg, 0x1, 0x85); |
| 1663 | break; |
| 1664 | default: |
| 1665 | break; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | static bool msm_chg_check_primary_det(struct msm_otg *motg) |
| 1670 | { |
| 1671 | struct otg_transceiver *otg = &motg->otg; |
| 1672 | u32 chg_det; |
| 1673 | bool ret = false; |
| 1674 | |
| 1675 | switch (motg->pdata->phy_type) { |
| 1676 | case CI_45NM_INTEGRATED_PHY: |
| 1677 | chg_det = ulpi_read(otg, 0x34); |
| 1678 | ret = chg_det & (1 << 4); |
| 1679 | break; |
| 1680 | case SNPS_28NM_INTEGRATED_PHY: |
| 1681 | chg_det = ulpi_read(otg, 0x87); |
| 1682 | ret = chg_det & 1; |
| 1683 | break; |
| 1684 | default: |
| 1685 | break; |
| 1686 | } |
| 1687 | return ret; |
| 1688 | } |
| 1689 | |
| 1690 | static void msm_chg_enable_primary_det(struct msm_otg *motg) |
| 1691 | { |
| 1692 | struct otg_transceiver *otg = &motg->otg; |
| 1693 | u32 chg_det; |
| 1694 | |
| 1695 | switch (motg->pdata->phy_type) { |
| 1696 | case CI_45NM_INTEGRATED_PHY: |
| 1697 | chg_det = ulpi_read(otg, 0x34); |
| 1698 | /* enable chg detection */ |
| 1699 | chg_det &= ~(1 << 0); |
| 1700 | ulpi_write(otg, chg_det, 0x34); |
| 1701 | break; |
| 1702 | case SNPS_28NM_INTEGRATED_PHY: |
| 1703 | /* |
| 1704 | * Configure DP as current source, DM as current sink |
| 1705 | * and enable battery charging comparators. |
| 1706 | */ |
| 1707 | ulpi_write(otg, 0x2, 0x85); |
| 1708 | ulpi_write(otg, 0x1, 0x85); |
| 1709 | break; |
| 1710 | default: |
| 1711 | break; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | static bool msm_chg_check_dcd(struct msm_otg *motg) |
| 1716 | { |
| 1717 | struct otg_transceiver *otg = &motg->otg; |
| 1718 | u32 line_state; |
| 1719 | bool ret = false; |
| 1720 | |
| 1721 | switch (motg->pdata->phy_type) { |
| 1722 | case CI_45NM_INTEGRATED_PHY: |
| 1723 | line_state = ulpi_read(otg, 0x15); |
| 1724 | ret = !(line_state & 1); |
| 1725 | break; |
| 1726 | case SNPS_28NM_INTEGRATED_PHY: |
| 1727 | line_state = ulpi_read(otg, 0x87); |
| 1728 | ret = line_state & 2; |
| 1729 | break; |
| 1730 | default: |
| 1731 | break; |
| 1732 | } |
| 1733 | return ret; |
| 1734 | } |
| 1735 | |
| 1736 | static void msm_chg_disable_dcd(struct msm_otg *motg) |
| 1737 | { |
| 1738 | struct otg_transceiver *otg = &motg->otg; |
| 1739 | u32 chg_det; |
| 1740 | |
| 1741 | switch (motg->pdata->phy_type) { |
| 1742 | case CI_45NM_INTEGRATED_PHY: |
| 1743 | chg_det = ulpi_read(otg, 0x34); |
| 1744 | chg_det &= ~(1 << 5); |
| 1745 | ulpi_write(otg, chg_det, 0x34); |
| 1746 | break; |
| 1747 | case SNPS_28NM_INTEGRATED_PHY: |
| 1748 | ulpi_write(otg, 0x10, 0x86); |
| 1749 | break; |
| 1750 | default: |
| 1751 | break; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | static void msm_chg_enable_dcd(struct msm_otg *motg) |
| 1756 | { |
| 1757 | struct otg_transceiver *otg = &motg->otg; |
| 1758 | u32 chg_det; |
| 1759 | |
| 1760 | switch (motg->pdata->phy_type) { |
| 1761 | case CI_45NM_INTEGRATED_PHY: |
| 1762 | chg_det = ulpi_read(otg, 0x34); |
| 1763 | /* Turn on D+ current source */ |
| 1764 | chg_det |= (1 << 5); |
| 1765 | ulpi_write(otg, chg_det, 0x34); |
| 1766 | break; |
| 1767 | case SNPS_28NM_INTEGRATED_PHY: |
| 1768 | /* Data contact detection enable */ |
| 1769 | ulpi_write(otg, 0x10, 0x85); |
| 1770 | break; |
| 1771 | default: |
| 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | static void msm_chg_block_on(struct msm_otg *motg) |
| 1777 | { |
| 1778 | struct otg_transceiver *otg = &motg->otg; |
| 1779 | u32 func_ctrl, chg_det; |
| 1780 | |
| 1781 | /* put the controller in non-driving mode */ |
| 1782 | func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL); |
| 1783 | func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK; |
| 1784 | func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING; |
| 1785 | ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL); |
| 1786 | |
| 1787 | switch (motg->pdata->phy_type) { |
| 1788 | case CI_45NM_INTEGRATED_PHY: |
| 1789 | chg_det = ulpi_read(otg, 0x34); |
| 1790 | /* control chg block via ULPI */ |
| 1791 | chg_det &= ~(1 << 3); |
| 1792 | ulpi_write(otg, chg_det, 0x34); |
| 1793 | /* Turn on chg detect block */ |
| 1794 | chg_det &= ~(1 << 1); |
| 1795 | ulpi_write(otg, chg_det, 0x34); |
| 1796 | udelay(20); |
| 1797 | break; |
| 1798 | case SNPS_28NM_INTEGRATED_PHY: |
| 1799 | /* Clear charger detecting control bits */ |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1800 | ulpi_write(otg, 0x1F, 0x86); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1801 | /* Clear alt interrupt latch and enable bits */ |
| 1802 | ulpi_write(otg, 0x1F, 0x92); |
| 1803 | ulpi_write(otg, 0x1F, 0x95); |
| 1804 | udelay(100); |
| 1805 | break; |
| 1806 | default: |
| 1807 | break; |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | static void msm_chg_block_off(struct msm_otg *motg) |
| 1812 | { |
| 1813 | struct otg_transceiver *otg = &motg->otg; |
| 1814 | u32 func_ctrl, chg_det; |
| 1815 | |
| 1816 | switch (motg->pdata->phy_type) { |
| 1817 | case CI_45NM_INTEGRATED_PHY: |
| 1818 | chg_det = ulpi_read(otg, 0x34); |
| 1819 | /* Turn off charger block */ |
| 1820 | chg_det |= ~(1 << 1); |
| 1821 | ulpi_write(otg, chg_det, 0x34); |
| 1822 | break; |
| 1823 | case SNPS_28NM_INTEGRATED_PHY: |
| 1824 | /* Clear charger detecting control bits */ |
| 1825 | ulpi_write(otg, 0x3F, 0x86); |
| 1826 | /* Clear alt interrupt latch and enable bits */ |
| 1827 | ulpi_write(otg, 0x1F, 0x92); |
| 1828 | ulpi_write(otg, 0x1F, 0x95); |
| 1829 | break; |
| 1830 | default: |
| 1831 | break; |
| 1832 | } |
| 1833 | |
| 1834 | /* put the controller in normal mode */ |
| 1835 | func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL); |
| 1836 | func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK; |
| 1837 | func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL; |
| 1838 | ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL); |
| 1839 | } |
| 1840 | |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 1841 | static const char *chg_to_string(enum usb_chg_type chg_type) |
| 1842 | { |
| 1843 | switch (chg_type) { |
| 1844 | case USB_SDP_CHARGER: return "USB_SDP_CHARGER"; |
| 1845 | case USB_DCP_CHARGER: return "USB_DCP_CHARGER"; |
| 1846 | case USB_CDP_CHARGER: return "USB_CDP_CHARGER"; |
| 1847 | case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER"; |
| 1848 | case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER"; |
| 1849 | case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER"; |
| 1850 | case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER"; |
| 1851 | default: return "INVALID_CHARGER"; |
| 1852 | } |
| 1853 | } |
| 1854 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1855 | #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */ |
| 1856 | #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */ |
Pavankumar Kondeti | 283146f | 2012-01-12 12:51:19 +0530 | [diff] [blame] | 1857 | #define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */ |
| 1858 | #define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */ |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1859 | static void msm_chg_detect_work(struct work_struct *w) |
| 1860 | { |
| 1861 | struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work); |
| 1862 | struct otg_transceiver *otg = &motg->otg; |
Pavankumar Kondeti | 2d09e5f | 2012-01-16 08:56:57 +0530 | [diff] [blame] | 1863 | bool is_dcd = false, tmout, vout, is_aca; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1864 | unsigned long delay; |
| 1865 | |
| 1866 | dev_dbg(otg->dev, "chg detection work\n"); |
| 1867 | switch (motg->chg_state) { |
| 1868 | case USB_CHG_STATE_UNDEFINED: |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1869 | msm_chg_block_on(motg); |
Pavankumar Kondeti | 2d09e5f | 2012-01-16 08:56:57 +0530 | [diff] [blame] | 1870 | if (motg->pdata->enable_dcd) |
| 1871 | msm_chg_enable_dcd(motg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1872 | msm_chg_enable_aca_det(motg); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1873 | motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD; |
| 1874 | motg->dcd_retries = 0; |
| 1875 | delay = MSM_CHG_DCD_POLL_TIME; |
| 1876 | break; |
| 1877 | case USB_CHG_STATE_WAIT_FOR_DCD: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1878 | is_aca = msm_chg_aca_detect(motg); |
| 1879 | if (is_aca) { |
| 1880 | /* |
| 1881 | * ID_A can be ACA dock too. continue |
| 1882 | * primary detection after DCD. |
| 1883 | */ |
| 1884 | if (test_bit(ID_A, &motg->inputs)) { |
| 1885 | motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD; |
| 1886 | } else { |
| 1887 | delay = 0; |
| 1888 | break; |
| 1889 | } |
| 1890 | } |
Pavankumar Kondeti | 2d09e5f | 2012-01-16 08:56:57 +0530 | [diff] [blame] | 1891 | if (motg->pdata->enable_dcd) |
| 1892 | is_dcd = msm_chg_check_dcd(motg); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1893 | tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES; |
| 1894 | if (is_dcd || tmout) { |
Pavankumar Kondeti | 2d09e5f | 2012-01-16 08:56:57 +0530 | [diff] [blame] | 1895 | if (motg->pdata->enable_dcd) |
| 1896 | msm_chg_disable_dcd(motg); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1897 | msm_chg_enable_primary_det(motg); |
| 1898 | delay = MSM_CHG_PRIMARY_DET_TIME; |
| 1899 | motg->chg_state = USB_CHG_STATE_DCD_DONE; |
| 1900 | } else { |
| 1901 | delay = MSM_CHG_DCD_POLL_TIME; |
| 1902 | } |
| 1903 | break; |
| 1904 | case USB_CHG_STATE_DCD_DONE: |
| 1905 | vout = msm_chg_check_primary_det(motg); |
| 1906 | if (vout) { |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1907 | if (test_bit(ID_A, &motg->inputs)) { |
| 1908 | motg->chg_type = USB_ACA_DOCK_CHARGER; |
| 1909 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1910 | delay = 0; |
| 1911 | break; |
| 1912 | } |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1913 | msm_chg_enable_secondary_det(motg); |
| 1914 | delay = MSM_CHG_SECONDARY_DET_TIME; |
| 1915 | motg->chg_state = USB_CHG_STATE_PRIMARY_DONE; |
| 1916 | } else { |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 1917 | if (test_bit(ID_A, &motg->inputs)) { |
| 1918 | motg->chg_type = USB_ACA_A_CHARGER; |
| 1919 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1920 | delay = 0; |
| 1921 | break; |
| 1922 | } |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1923 | motg->chg_type = USB_SDP_CHARGER; |
| 1924 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1925 | delay = 0; |
| 1926 | } |
| 1927 | break; |
| 1928 | case USB_CHG_STATE_PRIMARY_DONE: |
| 1929 | vout = msm_chg_check_secondary_det(motg); |
| 1930 | if (vout) |
| 1931 | motg->chg_type = USB_DCP_CHARGER; |
| 1932 | else |
| 1933 | motg->chg_type = USB_CDP_CHARGER; |
| 1934 | motg->chg_state = USB_CHG_STATE_SECONDARY_DONE; |
| 1935 | /* fall through */ |
| 1936 | case USB_CHG_STATE_SECONDARY_DONE: |
| 1937 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1938 | case USB_CHG_STATE_DETECTED: |
| 1939 | msm_chg_block_off(motg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1940 | msm_chg_enable_aca_det(motg); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1941 | /* |
| 1942 | * Spurious interrupt is seen after enabling ACA detection |
| 1943 | * due to which charger detection fails in case of PET. |
| 1944 | * Add delay of 100 microsec to avoid that. |
| 1945 | */ |
| 1946 | udelay(100); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1947 | msm_chg_enable_aca_intr(motg); |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 1948 | dev_dbg(otg->dev, "chg_type = %s\n", |
| 1949 | chg_to_string(motg->chg_type)); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1950 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1951 | return; |
| 1952 | default: |
| 1953 | return; |
| 1954 | } |
| 1955 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1956 | queue_delayed_work(system_nrt_wq, &motg->chg_work, delay); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1957 | } |
| 1958 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1959 | /* |
| 1960 | * We support OTG, Peripheral only and Host only configurations. In case |
| 1961 | * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen |
| 1962 | * via Id pin status or user request (debugfs). Id/BSV interrupts are not |
| 1963 | * enabled when switch is controlled by user and default mode is supplied |
| 1964 | * by board file, which can be changed by userspace later. |
| 1965 | */ |
| 1966 | static void msm_otg_init_sm(struct msm_otg *motg) |
| 1967 | { |
| 1968 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 1969 | u32 otgsc = readl(USB_OTGSC); |
| 1970 | |
| 1971 | switch (pdata->mode) { |
| 1972 | case USB_OTG: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1973 | if (pdata->otg_control == OTG_USER_CONTROL) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1974 | if (pdata->default_mode == USB_HOST) { |
| 1975 | clear_bit(ID, &motg->inputs); |
| 1976 | } else if (pdata->default_mode == USB_PERIPHERAL) { |
| 1977 | set_bit(ID, &motg->inputs); |
| 1978 | set_bit(B_SESS_VLD, &motg->inputs); |
| 1979 | } else { |
| 1980 | set_bit(ID, &motg->inputs); |
| 1981 | clear_bit(B_SESS_VLD, &motg->inputs); |
| 1982 | } |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 1983 | } else if (pdata->otg_control == OTG_PHY_CONTROL) { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1984 | if (otgsc & OTGSC_ID) { |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 1985 | set_bit(ID, &motg->inputs); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1986 | } else { |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 1987 | clear_bit(ID, &motg->inputs); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 1988 | set_bit(A_BUS_REQ, &motg->inputs); |
| 1989 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1990 | if (otgsc & OTGSC_BSV) |
| 1991 | set_bit(B_SESS_VLD, &motg->inputs); |
| 1992 | else |
| 1993 | clear_bit(B_SESS_VLD, &motg->inputs); |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 1994 | } else if (pdata->otg_control == OTG_PMIC_CONTROL) { |
Pavankumar Kondeti | 0d81f31 | 2012-01-13 11:34:10 +0530 | [diff] [blame] | 1995 | if (pdata->pmic_id_irq) { |
| 1996 | if (irq_read_line(pdata->pmic_id_irq)) |
| 1997 | set_bit(ID, &motg->inputs); |
| 1998 | else |
| 1999 | clear_bit(ID, &motg->inputs); |
| 2000 | } |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 2001 | /* |
| 2002 | * VBUS initial state is reported after PMIC |
| 2003 | * driver initialization. Wait for it. |
| 2004 | */ |
| 2005 | wait_for_completion(&pmic_vbus_init); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2006 | } |
| 2007 | break; |
| 2008 | case USB_HOST: |
| 2009 | clear_bit(ID, &motg->inputs); |
| 2010 | break; |
| 2011 | case USB_PERIPHERAL: |
| 2012 | set_bit(ID, &motg->inputs); |
Pavankumar Kondeti | 0d81f31 | 2012-01-13 11:34:10 +0530 | [diff] [blame] | 2013 | if (pdata->otg_control == OTG_PHY_CONTROL) { |
| 2014 | if (otgsc & OTGSC_BSV) |
| 2015 | set_bit(B_SESS_VLD, &motg->inputs); |
| 2016 | else |
| 2017 | clear_bit(B_SESS_VLD, &motg->inputs); |
| 2018 | } else if (pdata->otg_control == OTG_PMIC_CONTROL) { |
| 2019 | /* |
| 2020 | * VBUS initial state is reported after PMIC |
| 2021 | * driver initialization. Wait for it. |
| 2022 | */ |
| 2023 | wait_for_completion(&pmic_vbus_init); |
| 2024 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2025 | break; |
| 2026 | default: |
| 2027 | break; |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | static void msm_otg_sm_work(struct work_struct *w) |
| 2032 | { |
| 2033 | struct msm_otg *motg = container_of(w, struct msm_otg, sm_work); |
| 2034 | struct otg_transceiver *otg = &motg->otg; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2035 | bool work = 0, srp_reqd; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2036 | |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 2037 | pm_runtime_resume(otg->dev); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2038 | pr_debug("%s work\n", otg_state_string(otg->state)); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2039 | switch (otg->state) { |
| 2040 | case OTG_STATE_UNDEFINED: |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2041 | msm_otg_reset(otg); |
| 2042 | msm_otg_init_sm(motg); |
| 2043 | otg->state = OTG_STATE_B_IDLE; |
Pavankumar Kondeti | 8a379b4 | 2011-12-12 13:07:23 +0530 | [diff] [blame] | 2044 | if (!test_bit(B_SESS_VLD, &motg->inputs) && |
| 2045 | test_bit(ID, &motg->inputs)) { |
| 2046 | pm_runtime_put_noidle(otg->dev); |
| 2047 | pm_runtime_suspend(otg->dev); |
| 2048 | break; |
| 2049 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2050 | /* FALL THROUGH */ |
| 2051 | case OTG_STATE_B_IDLE: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2052 | if ((!test_bit(ID, &motg->inputs) || |
| 2053 | test_bit(ID_A, &motg->inputs)) && otg->host) { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2054 | pr_debug("!id || id_A\n"); |
| 2055 | clear_bit(B_BUS_REQ, &motg->inputs); |
| 2056 | set_bit(A_BUS_REQ, &motg->inputs); |
| 2057 | otg->state = OTG_STATE_A_IDLE; |
| 2058 | work = 1; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2059 | } else if (test_bit(B_SESS_VLD, &motg->inputs)) { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2060 | pr_debug("b_sess_vld\n"); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2061 | switch (motg->chg_state) { |
| 2062 | case USB_CHG_STATE_UNDEFINED: |
| 2063 | msm_chg_detect_work(&motg->chg_work.work); |
| 2064 | break; |
| 2065 | case USB_CHG_STATE_DETECTED: |
| 2066 | switch (motg->chg_type) { |
| 2067 | case USB_DCP_CHARGER: |
Pavankumar Kondeti | 283146f | 2012-01-12 12:51:19 +0530 | [diff] [blame] | 2068 | /* Enable VDP_SRC */ |
| 2069 | ulpi_write(otg, 0x2, 0x85); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2070 | msm_otg_notify_charger(motg, |
| 2071 | IDEV_CHG_MAX); |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 2072 | pm_runtime_put_noidle(otg->dev); |
| 2073 | pm_runtime_suspend(otg->dev); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2074 | break; |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2075 | case USB_ACA_B_CHARGER: |
| 2076 | msm_otg_notify_charger(motg, |
| 2077 | IDEV_ACA_CHG_MAX); |
| 2078 | /* |
| 2079 | * (ID_B --> ID_C) PHY_ALT interrupt can |
| 2080 | * not be detected in LPM. |
| 2081 | */ |
| 2082 | break; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2083 | case USB_CDP_CHARGER: |
| 2084 | msm_otg_notify_charger(motg, |
| 2085 | IDEV_CHG_MAX); |
| 2086 | msm_otg_start_peripheral(otg, 1); |
| 2087 | otg->state = OTG_STATE_B_PERIPHERAL; |
| 2088 | break; |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2089 | case USB_ACA_C_CHARGER: |
| 2090 | msm_otg_notify_charger(motg, |
| 2091 | IDEV_ACA_CHG_MAX); |
| 2092 | msm_otg_start_peripheral(otg, 1); |
| 2093 | otg->state = OTG_STATE_B_PERIPHERAL; |
| 2094 | break; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2095 | case USB_SDP_CHARGER: |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2096 | msm_otg_start_peripheral(otg, 1); |
| 2097 | otg->state = OTG_STATE_B_PERIPHERAL; |
| 2098 | break; |
| 2099 | default: |
| 2100 | break; |
| 2101 | } |
| 2102 | break; |
| 2103 | default: |
| 2104 | break; |
| 2105 | } |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2106 | } else if (test_bit(B_BUS_REQ, &motg->inputs)) { |
| 2107 | pr_debug("b_sess_end && b_bus_req\n"); |
| 2108 | if (msm_otg_start_srp(otg) < 0) { |
| 2109 | clear_bit(B_BUS_REQ, &motg->inputs); |
| 2110 | work = 1; |
| 2111 | break; |
| 2112 | } |
| 2113 | otg->state = OTG_STATE_B_SRP_INIT; |
| 2114 | msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL); |
| 2115 | break; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2116 | } else { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2117 | pr_debug("chg_work cancel"); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2118 | cancel_delayed_work_sync(&motg->chg_work); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2119 | motg->chg_state = USB_CHG_STATE_UNDEFINED; |
| 2120 | motg->chg_type = USB_INVALID_CHARGER; |
Rajkumar Raghupathy | 18fd713 | 2012-04-20 11:28:13 +0530 | [diff] [blame] | 2121 | msm_otg_notify_charger(motg, 0); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2122 | msm_otg_reset(otg); |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 2123 | pm_runtime_put_noidle(otg->dev); |
| 2124 | pm_runtime_suspend(otg->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2125 | } |
| 2126 | break; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2127 | case OTG_STATE_B_SRP_INIT: |
| 2128 | if (!test_bit(ID, &motg->inputs) || |
| 2129 | test_bit(ID_A, &motg->inputs) || |
| 2130 | test_bit(ID_C, &motg->inputs) || |
| 2131 | (test_bit(B_SESS_VLD, &motg->inputs) && |
| 2132 | !test_bit(ID_B, &motg->inputs))) { |
| 2133 | pr_debug("!id || id_a/c || b_sess_vld+!id_b\n"); |
| 2134 | msm_otg_del_timer(motg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2135 | otg->state = OTG_STATE_B_IDLE; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2136 | /* |
| 2137 | * clear VBUSVLDEXTSEL and VBUSVLDEXT register |
| 2138 | * bits after SRP initiation. |
| 2139 | */ |
| 2140 | ulpi_write(otg, 0x0, 0x98); |
| 2141 | work = 1; |
| 2142 | } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) { |
| 2143 | pr_debug("b_srp_fail\n"); |
| 2144 | pr_info("A-device did not respond to SRP\n"); |
| 2145 | clear_bit(B_BUS_REQ, &motg->inputs); |
| 2146 | clear_bit(B_SRP_FAIL, &motg->tmouts); |
| 2147 | otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP); |
| 2148 | ulpi_write(otg, 0x0, 0x98); |
| 2149 | otg->state = OTG_STATE_B_IDLE; |
| 2150 | motg->b_last_se0_sess = jiffies; |
| 2151 | work = 1; |
| 2152 | } |
| 2153 | break; |
| 2154 | case OTG_STATE_B_PERIPHERAL: |
| 2155 | if (!test_bit(ID, &motg->inputs) || |
| 2156 | test_bit(ID_A, &motg->inputs) || |
| 2157 | test_bit(ID_B, &motg->inputs) || |
| 2158 | !test_bit(B_SESS_VLD, &motg->inputs)) { |
| 2159 | pr_debug("!id || id_a/b || !b_sess_vld\n"); |
Rajkumar Raghupathy | 18fd713 | 2012-04-20 11:28:13 +0530 | [diff] [blame] | 2160 | motg->chg_state = USB_CHG_STATE_UNDEFINED; |
| 2161 | motg->chg_type = USB_INVALID_CHARGER; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2162 | msm_otg_notify_charger(motg, 0); |
| 2163 | srp_reqd = otg->gadget->otg_srp_reqd; |
| 2164 | msm_otg_start_peripheral(otg, 0); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2165 | if (test_bit(ID_B, &motg->inputs)) |
| 2166 | clear_bit(ID_B, &motg->inputs); |
| 2167 | clear_bit(B_BUS_REQ, &motg->inputs); |
| 2168 | otg->state = OTG_STATE_B_IDLE; |
| 2169 | motg->b_last_se0_sess = jiffies; |
| 2170 | if (srp_reqd) |
| 2171 | msm_otg_start_timer(motg, |
| 2172 | TB_TST_SRP, B_TST_SRP); |
| 2173 | else |
| 2174 | work = 1; |
| 2175 | } else if (test_bit(B_BUS_REQ, &motg->inputs) && |
| 2176 | otg->gadget->b_hnp_enable && |
| 2177 | test_bit(A_BUS_SUSPEND, &motg->inputs)) { |
| 2178 | pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n"); |
| 2179 | msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST); |
| 2180 | /* D+ pullup should not be disconnected within 4msec |
| 2181 | * after A device suspends the bus. Otherwise PET will |
| 2182 | * fail the compliance test. |
| 2183 | */ |
| 2184 | udelay(1000); |
| 2185 | msm_otg_start_peripheral(otg, 0); |
| 2186 | otg->state = OTG_STATE_B_WAIT_ACON; |
| 2187 | /* |
| 2188 | * start HCD even before A-device enable |
| 2189 | * pull-up to meet HNP timings. |
| 2190 | */ |
| 2191 | otg->host->is_b_host = 1; |
| 2192 | msm_otg_start_host(otg, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2193 | } else if (test_bit(ID_C, &motg->inputs)) { |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2194 | msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2195 | } |
| 2196 | break; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2197 | case OTG_STATE_B_WAIT_ACON: |
| 2198 | if (!test_bit(ID, &motg->inputs) || |
| 2199 | test_bit(ID_A, &motg->inputs) || |
| 2200 | test_bit(ID_B, &motg->inputs) || |
| 2201 | !test_bit(B_SESS_VLD, &motg->inputs)) { |
| 2202 | pr_debug("!id || id_a/b || !b_sess_vld\n"); |
| 2203 | msm_otg_del_timer(motg); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2204 | /* |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2205 | * A-device is physically disconnected during |
| 2206 | * HNP. Remove HCD. |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2207 | */ |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2208 | msm_otg_start_host(otg, 0); |
| 2209 | otg->host->is_b_host = 0; |
| 2210 | |
| 2211 | clear_bit(B_BUS_REQ, &motg->inputs); |
| 2212 | clear_bit(A_BUS_SUSPEND, &motg->inputs); |
| 2213 | motg->b_last_se0_sess = jiffies; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2214 | otg->state = OTG_STATE_B_IDLE; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2215 | msm_otg_reset(otg); |
| 2216 | work = 1; |
| 2217 | } else if (test_bit(A_CONN, &motg->inputs)) { |
| 2218 | pr_debug("a_conn\n"); |
| 2219 | clear_bit(A_BUS_SUSPEND, &motg->inputs); |
| 2220 | otg->state = OTG_STATE_B_HOST; |
| 2221 | /* |
| 2222 | * PET disconnects D+ pullup after reset is generated |
| 2223 | * by B device in B_HOST role which is not detected by |
| 2224 | * B device. As workaorund , start timer of 300msec |
| 2225 | * and stop timer if A device is enumerated else clear |
| 2226 | * A_CONN. |
| 2227 | */ |
| 2228 | msm_otg_start_timer(motg, TB_TST_CONFIG, |
| 2229 | B_TST_CONFIG); |
| 2230 | } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) { |
| 2231 | pr_debug("b_ase0_brst_tmout\n"); |
| 2232 | pr_info("B HNP fail:No response from A device\n"); |
| 2233 | msm_otg_start_host(otg, 0); |
| 2234 | msm_otg_reset(otg); |
| 2235 | otg->host->is_b_host = 0; |
| 2236 | clear_bit(B_ASE0_BRST, &motg->tmouts); |
| 2237 | clear_bit(A_BUS_SUSPEND, &motg->inputs); |
| 2238 | clear_bit(B_BUS_REQ, &motg->inputs); |
| 2239 | otg_send_event(OTG_EVENT_HNP_FAILED); |
| 2240 | otg->state = OTG_STATE_B_IDLE; |
| 2241 | work = 1; |
| 2242 | } else if (test_bit(ID_C, &motg->inputs)) { |
| 2243 | msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX); |
| 2244 | } |
| 2245 | break; |
| 2246 | case OTG_STATE_B_HOST: |
| 2247 | if (!test_bit(B_BUS_REQ, &motg->inputs) || |
| 2248 | !test_bit(A_CONN, &motg->inputs) || |
| 2249 | !test_bit(B_SESS_VLD, &motg->inputs)) { |
| 2250 | pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n"); |
| 2251 | clear_bit(A_CONN, &motg->inputs); |
| 2252 | clear_bit(B_BUS_REQ, &motg->inputs); |
| 2253 | msm_otg_start_host(otg, 0); |
| 2254 | otg->host->is_b_host = 0; |
| 2255 | otg->state = OTG_STATE_B_IDLE; |
| 2256 | msm_otg_reset(otg); |
| 2257 | work = 1; |
| 2258 | } else if (test_bit(ID_C, &motg->inputs)) { |
| 2259 | msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX); |
| 2260 | } |
| 2261 | break; |
| 2262 | case OTG_STATE_A_IDLE: |
| 2263 | otg->default_a = 1; |
| 2264 | if (test_bit(ID, &motg->inputs) && |
| 2265 | !test_bit(ID_A, &motg->inputs)) { |
| 2266 | pr_debug("id && !id_a\n"); |
| 2267 | otg->default_a = 0; |
| 2268 | clear_bit(A_BUS_DROP, &motg->inputs); |
| 2269 | otg->state = OTG_STATE_B_IDLE; |
| 2270 | del_timer_sync(&motg->id_timer); |
| 2271 | msm_otg_link_reset(motg); |
| 2272 | msm_chg_enable_aca_intr(motg); |
| 2273 | msm_otg_notify_charger(motg, 0); |
| 2274 | work = 1; |
| 2275 | } else if (!test_bit(A_BUS_DROP, &motg->inputs) && |
| 2276 | (test_bit(A_SRP_DET, &motg->inputs) || |
| 2277 | test_bit(A_BUS_REQ, &motg->inputs))) { |
| 2278 | pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n"); |
| 2279 | |
| 2280 | clear_bit(A_SRP_DET, &motg->inputs); |
| 2281 | /* Disable SRP detection */ |
| 2282 | writel_relaxed((readl_relaxed(USB_OTGSC) & |
| 2283 | ~OTGSC_INTSTS_MASK) & |
| 2284 | ~OTGSC_DPIE, USB_OTGSC); |
| 2285 | |
| 2286 | otg->state = OTG_STATE_A_WAIT_VRISE; |
| 2287 | /* VBUS should not be supplied before end of SRP pulse |
| 2288 | * generated by PET, if not complaince test fail. |
| 2289 | */ |
| 2290 | usleep_range(10000, 12000); |
| 2291 | /* ACA: ID_A: Stop charging untill enumeration */ |
| 2292 | if (test_bit(ID_A, &motg->inputs)) |
| 2293 | msm_otg_notify_charger(motg, 0); |
| 2294 | else |
| 2295 | msm_hsusb_vbus_power(motg, 1); |
| 2296 | msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE); |
| 2297 | } else { |
| 2298 | pr_debug("No session requested\n"); |
| 2299 | clear_bit(A_BUS_DROP, &motg->inputs); |
| 2300 | if (test_bit(ID_A, &motg->inputs)) { |
| 2301 | msm_otg_notify_charger(motg, |
| 2302 | IDEV_ACA_CHG_MAX); |
| 2303 | } else if (!test_bit(ID, &motg->inputs)) { |
| 2304 | msm_otg_notify_charger(motg, 0); |
| 2305 | /* |
| 2306 | * A-device is not providing power on VBUS. |
| 2307 | * Enable SRP detection. |
| 2308 | */ |
| 2309 | writel_relaxed(0x13, USB_USBMODE); |
| 2310 | writel_relaxed((readl_relaxed(USB_OTGSC) & |
| 2311 | ~OTGSC_INTSTS_MASK) | |
| 2312 | OTGSC_DPIE, USB_OTGSC); |
| 2313 | mb(); |
| 2314 | } |
| 2315 | } |
| 2316 | break; |
| 2317 | case OTG_STATE_A_WAIT_VRISE: |
| 2318 | if ((test_bit(ID, &motg->inputs) && |
| 2319 | !test_bit(ID_A, &motg->inputs)) || |
| 2320 | test_bit(A_BUS_DROP, &motg->inputs) || |
| 2321 | test_bit(A_WAIT_VRISE, &motg->tmouts)) { |
| 2322 | pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n"); |
| 2323 | clear_bit(A_BUS_REQ, &motg->inputs); |
| 2324 | msm_otg_del_timer(motg); |
| 2325 | msm_hsusb_vbus_power(motg, 0); |
| 2326 | otg->state = OTG_STATE_A_WAIT_VFALL; |
| 2327 | msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL); |
| 2328 | } else if (test_bit(A_VBUS_VLD, &motg->inputs)) { |
| 2329 | pr_debug("a_vbus_vld\n"); |
| 2330 | otg->state = OTG_STATE_A_WAIT_BCON; |
| 2331 | if (TA_WAIT_BCON > 0) |
| 2332 | msm_otg_start_timer(motg, TA_WAIT_BCON, |
| 2333 | A_WAIT_BCON); |
| 2334 | msm_otg_start_host(otg, 1); |
| 2335 | msm_chg_enable_aca_det(motg); |
| 2336 | msm_chg_disable_aca_intr(motg); |
Chiranjeevi Velempati | 489a27c | 2012-03-29 09:47:17 +0530 | [diff] [blame] | 2337 | mod_timer(&motg->id_timer, ID_TIMER_FREQ); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2338 | if (msm_chg_check_aca_intr(motg)) |
| 2339 | work = 1; |
| 2340 | } |
| 2341 | break; |
| 2342 | case OTG_STATE_A_WAIT_BCON: |
| 2343 | if ((test_bit(ID, &motg->inputs) && |
| 2344 | !test_bit(ID_A, &motg->inputs)) || |
| 2345 | test_bit(A_BUS_DROP, &motg->inputs) || |
| 2346 | test_bit(A_WAIT_BCON, &motg->tmouts)) { |
| 2347 | pr_debug("(id && id_a/b/c) || a_bus_drop ||" |
| 2348 | "a_wait_bcon_tmout\n"); |
| 2349 | if (test_bit(A_WAIT_BCON, &motg->tmouts)) { |
| 2350 | pr_info("Device No Response\n"); |
| 2351 | otg_send_event(OTG_EVENT_DEV_CONN_TMOUT); |
| 2352 | } |
| 2353 | msm_otg_del_timer(motg); |
| 2354 | clear_bit(A_BUS_REQ, &motg->inputs); |
| 2355 | clear_bit(B_CONN, &motg->inputs); |
| 2356 | msm_otg_start_host(otg, 0); |
| 2357 | /* |
| 2358 | * ACA: ID_A with NO accessory, just the A plug is |
| 2359 | * attached to ACA: Use IDCHG_MAX for charging |
| 2360 | */ |
| 2361 | if (test_bit(ID_A, &motg->inputs)) |
| 2362 | msm_otg_notify_charger(motg, IDEV_CHG_MIN); |
| 2363 | else |
| 2364 | msm_hsusb_vbus_power(motg, 0); |
| 2365 | otg->state = OTG_STATE_A_WAIT_VFALL; |
| 2366 | msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL); |
| 2367 | } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) { |
| 2368 | pr_debug("!a_vbus_vld\n"); |
| 2369 | clear_bit(B_CONN, &motg->inputs); |
| 2370 | msm_otg_del_timer(motg); |
| 2371 | msm_otg_start_host(otg, 0); |
| 2372 | otg->state = OTG_STATE_A_VBUS_ERR; |
| 2373 | msm_otg_reset(otg); |
| 2374 | } else if (test_bit(ID_A, &motg->inputs)) { |
| 2375 | msm_hsusb_vbus_power(motg, 0); |
| 2376 | } else if (!test_bit(A_BUS_REQ, &motg->inputs)) { |
| 2377 | /* |
| 2378 | * If TA_WAIT_BCON is infinite, we don;t |
| 2379 | * turn off VBUS. Enter low power mode. |
| 2380 | */ |
| 2381 | if (TA_WAIT_BCON < 0) |
| 2382 | pm_runtime_put_sync(otg->dev); |
| 2383 | } else if (!test_bit(ID, &motg->inputs)) { |
| 2384 | msm_hsusb_vbus_power(motg, 1); |
| 2385 | } |
| 2386 | break; |
| 2387 | case OTG_STATE_A_HOST: |
| 2388 | if ((test_bit(ID, &motg->inputs) && |
| 2389 | !test_bit(ID_A, &motg->inputs)) || |
| 2390 | test_bit(A_BUS_DROP, &motg->inputs)) { |
| 2391 | pr_debug("id_a/b/c || a_bus_drop\n"); |
| 2392 | clear_bit(B_CONN, &motg->inputs); |
| 2393 | clear_bit(A_BUS_REQ, &motg->inputs); |
| 2394 | msm_otg_del_timer(motg); |
| 2395 | otg->state = OTG_STATE_A_WAIT_VFALL; |
| 2396 | msm_otg_start_host(otg, 0); |
| 2397 | if (!test_bit(ID_A, &motg->inputs)) |
| 2398 | msm_hsusb_vbus_power(motg, 0); |
| 2399 | msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL); |
| 2400 | } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) { |
| 2401 | pr_debug("!a_vbus_vld\n"); |
| 2402 | clear_bit(B_CONN, &motg->inputs); |
| 2403 | msm_otg_del_timer(motg); |
| 2404 | otg->state = OTG_STATE_A_VBUS_ERR; |
| 2405 | msm_otg_start_host(otg, 0); |
| 2406 | msm_otg_reset(otg); |
| 2407 | } else if (!test_bit(A_BUS_REQ, &motg->inputs)) { |
| 2408 | /* |
| 2409 | * a_bus_req is de-asserted when root hub is |
| 2410 | * suspended or HNP is in progress. |
| 2411 | */ |
| 2412 | pr_debug("!a_bus_req\n"); |
| 2413 | msm_otg_del_timer(motg); |
| 2414 | otg->state = OTG_STATE_A_SUSPEND; |
| 2415 | if (otg->host->b_hnp_enable) |
| 2416 | msm_otg_start_timer(motg, TA_AIDL_BDIS, |
| 2417 | A_AIDL_BDIS); |
| 2418 | else |
| 2419 | pm_runtime_put_sync(otg->dev); |
| 2420 | } else if (!test_bit(B_CONN, &motg->inputs)) { |
| 2421 | pr_debug("!b_conn\n"); |
| 2422 | msm_otg_del_timer(motg); |
| 2423 | otg->state = OTG_STATE_A_WAIT_BCON; |
| 2424 | if (TA_WAIT_BCON > 0) |
| 2425 | msm_otg_start_timer(motg, TA_WAIT_BCON, |
| 2426 | A_WAIT_BCON); |
| 2427 | if (msm_chg_check_aca_intr(motg)) |
| 2428 | work = 1; |
| 2429 | } else if (test_bit(ID_A, &motg->inputs)) { |
| 2430 | msm_otg_del_timer(motg); |
| 2431 | msm_hsusb_vbus_power(motg, 0); |
| 2432 | if (motg->chg_type == USB_ACA_DOCK_CHARGER) |
| 2433 | msm_otg_notify_charger(motg, |
| 2434 | IDEV_ACA_CHG_MAX); |
| 2435 | else |
| 2436 | msm_otg_notify_charger(motg, |
| 2437 | IDEV_CHG_MIN - motg->mA_port); |
| 2438 | } else if (!test_bit(ID, &motg->inputs)) { |
| 2439 | motg->chg_state = USB_CHG_STATE_UNDEFINED; |
| 2440 | motg->chg_type = USB_INVALID_CHARGER; |
| 2441 | msm_otg_notify_charger(motg, 0); |
| 2442 | msm_hsusb_vbus_power(motg, 1); |
| 2443 | } |
| 2444 | break; |
| 2445 | case OTG_STATE_A_SUSPEND: |
| 2446 | if ((test_bit(ID, &motg->inputs) && |
| 2447 | !test_bit(ID_A, &motg->inputs)) || |
| 2448 | test_bit(A_BUS_DROP, &motg->inputs) || |
| 2449 | test_bit(A_AIDL_BDIS, &motg->tmouts)) { |
| 2450 | pr_debug("id_a/b/c || a_bus_drop ||" |
| 2451 | "a_aidl_bdis_tmout\n"); |
| 2452 | msm_otg_del_timer(motg); |
| 2453 | clear_bit(B_CONN, &motg->inputs); |
| 2454 | otg->state = OTG_STATE_A_WAIT_VFALL; |
| 2455 | msm_otg_start_host(otg, 0); |
| 2456 | msm_otg_reset(otg); |
| 2457 | if (!test_bit(ID_A, &motg->inputs)) |
| 2458 | msm_hsusb_vbus_power(motg, 0); |
| 2459 | msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL); |
| 2460 | } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) { |
| 2461 | pr_debug("!a_vbus_vld\n"); |
| 2462 | msm_otg_del_timer(motg); |
| 2463 | clear_bit(B_CONN, &motg->inputs); |
| 2464 | otg->state = OTG_STATE_A_VBUS_ERR; |
| 2465 | msm_otg_start_host(otg, 0); |
| 2466 | msm_otg_reset(otg); |
| 2467 | } else if (!test_bit(B_CONN, &motg->inputs) && |
| 2468 | otg->host->b_hnp_enable) { |
| 2469 | pr_debug("!b_conn && b_hnp_enable"); |
| 2470 | otg->state = OTG_STATE_A_PERIPHERAL; |
| 2471 | msm_otg_host_hnp_enable(otg, 1); |
| 2472 | otg->gadget->is_a_peripheral = 1; |
| 2473 | msm_otg_start_peripheral(otg, 1); |
| 2474 | } else if (!test_bit(B_CONN, &motg->inputs) && |
| 2475 | !otg->host->b_hnp_enable) { |
| 2476 | pr_debug("!b_conn && !b_hnp_enable"); |
| 2477 | /* |
| 2478 | * bus request is dropped during suspend. |
| 2479 | * acquire again for next device. |
| 2480 | */ |
| 2481 | set_bit(A_BUS_REQ, &motg->inputs); |
| 2482 | otg->state = OTG_STATE_A_WAIT_BCON; |
| 2483 | if (TA_WAIT_BCON > 0) |
| 2484 | msm_otg_start_timer(motg, TA_WAIT_BCON, |
| 2485 | A_WAIT_BCON); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2486 | } else if (test_bit(ID_A, &motg->inputs)) { |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 2487 | msm_hsusb_vbus_power(motg, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2488 | msm_otg_notify_charger(motg, |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2489 | IDEV_CHG_MIN - motg->mA_port); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2490 | } else if (!test_bit(ID, &motg->inputs)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2491 | msm_otg_notify_charger(motg, 0); |
Mayank Rana | e392688 | 2011-12-26 09:47:54 +0530 | [diff] [blame] | 2492 | msm_hsusb_vbus_power(motg, 1); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2493 | } |
| 2494 | break; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2495 | case OTG_STATE_A_PERIPHERAL: |
| 2496 | if ((test_bit(ID, &motg->inputs) && |
| 2497 | !test_bit(ID_A, &motg->inputs)) || |
| 2498 | test_bit(A_BUS_DROP, &motg->inputs)) { |
| 2499 | pr_debug("id _f/b/c || a_bus_drop\n"); |
| 2500 | /* Clear BIDL_ADIS timer */ |
| 2501 | msm_otg_del_timer(motg); |
| 2502 | otg->state = OTG_STATE_A_WAIT_VFALL; |
| 2503 | msm_otg_start_peripheral(otg, 0); |
| 2504 | otg->gadget->is_a_peripheral = 0; |
| 2505 | msm_otg_start_host(otg, 0); |
| 2506 | msm_otg_reset(otg); |
| 2507 | if (!test_bit(ID_A, &motg->inputs)) |
| 2508 | msm_hsusb_vbus_power(motg, 0); |
| 2509 | msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL); |
| 2510 | } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) { |
| 2511 | pr_debug("!a_vbus_vld\n"); |
| 2512 | /* Clear BIDL_ADIS timer */ |
| 2513 | msm_otg_del_timer(motg); |
| 2514 | otg->state = OTG_STATE_A_VBUS_ERR; |
| 2515 | msm_otg_start_peripheral(otg, 0); |
| 2516 | otg->gadget->is_a_peripheral = 0; |
| 2517 | msm_otg_start_host(otg, 0); |
| 2518 | } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) { |
| 2519 | pr_debug("a_bidl_adis_tmout\n"); |
| 2520 | msm_otg_start_peripheral(otg, 0); |
| 2521 | otg->gadget->is_a_peripheral = 0; |
| 2522 | otg->state = OTG_STATE_A_WAIT_BCON; |
| 2523 | set_bit(A_BUS_REQ, &motg->inputs); |
| 2524 | msm_otg_host_hnp_enable(otg, 0); |
| 2525 | if (TA_WAIT_BCON > 0) |
| 2526 | msm_otg_start_timer(motg, TA_WAIT_BCON, |
| 2527 | A_WAIT_BCON); |
| 2528 | } else if (test_bit(ID_A, &motg->inputs)) { |
| 2529 | msm_hsusb_vbus_power(motg, 0); |
| 2530 | msm_otg_notify_charger(motg, |
| 2531 | IDEV_CHG_MIN - motg->mA_port); |
| 2532 | } else if (!test_bit(ID, &motg->inputs)) { |
| 2533 | msm_otg_notify_charger(motg, 0); |
| 2534 | msm_hsusb_vbus_power(motg, 1); |
| 2535 | } |
| 2536 | break; |
| 2537 | case OTG_STATE_A_WAIT_VFALL: |
| 2538 | if (test_bit(A_WAIT_VFALL, &motg->tmouts)) { |
| 2539 | clear_bit(A_VBUS_VLD, &motg->inputs); |
| 2540 | otg->state = OTG_STATE_A_IDLE; |
| 2541 | work = 1; |
| 2542 | } |
| 2543 | break; |
| 2544 | case OTG_STATE_A_VBUS_ERR: |
| 2545 | if ((test_bit(ID, &motg->inputs) && |
| 2546 | !test_bit(ID_A, &motg->inputs)) || |
| 2547 | test_bit(A_BUS_DROP, &motg->inputs) || |
| 2548 | test_bit(A_CLR_ERR, &motg->inputs)) { |
| 2549 | otg->state = OTG_STATE_A_WAIT_VFALL; |
| 2550 | if (!test_bit(ID_A, &motg->inputs)) |
| 2551 | msm_hsusb_vbus_power(motg, 0); |
| 2552 | msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL); |
| 2553 | motg->chg_state = USB_CHG_STATE_UNDEFINED; |
| 2554 | motg->chg_type = USB_INVALID_CHARGER; |
| 2555 | msm_otg_notify_charger(motg, 0); |
| 2556 | } |
| 2557 | break; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2558 | default: |
| 2559 | break; |
| 2560 | } |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2561 | if (work) |
| 2562 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | static irqreturn_t msm_otg_irq(int irq, void *data) |
| 2566 | { |
| 2567 | struct msm_otg *motg = data; |
| 2568 | struct otg_transceiver *otg = &motg->otg; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2569 | u32 otgsc = 0, usbsts, pc; |
| 2570 | bool work = 0; |
| 2571 | irqreturn_t ret = IRQ_HANDLED; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2572 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 2573 | if (atomic_read(&motg->in_lpm)) { |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 2574 | pr_debug("OTG IRQ: in LPM\n"); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 2575 | disable_irq_nosync(irq); |
| 2576 | motg->async_int = 1; |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 2577 | pm_request_resume(otg->dev); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 2578 | return IRQ_HANDLED; |
| 2579 | } |
| 2580 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2581 | usbsts = readl(USB_USBSTS); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2582 | otgsc = readl(USB_OTGSC); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2583 | |
| 2584 | if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK)) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2585 | return IRQ_NONE; |
| 2586 | |
| 2587 | if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) { |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2588 | if (otgsc & OTGSC_ID) { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2589 | pr_debug("Id set\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2590 | set_bit(ID, &motg->inputs); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2591 | } else { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2592 | pr_debug("Id clear\n"); |
| 2593 | /* |
| 2594 | * Assert a_bus_req to supply power on |
| 2595 | * VBUS when Micro/Mini-A cable is connected |
| 2596 | * with out user intervention. |
| 2597 | */ |
| 2598 | set_bit(A_BUS_REQ, &motg->inputs); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2599 | clear_bit(ID, &motg->inputs); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2600 | msm_chg_enable_aca_det(motg); |
| 2601 | } |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2602 | writel_relaxed(otgsc, USB_OTGSC); |
| 2603 | work = 1; |
| 2604 | } else if (otgsc & OTGSC_DPIS) { |
| 2605 | pr_debug("DPIS detected\n"); |
| 2606 | writel_relaxed(otgsc, USB_OTGSC); |
| 2607 | set_bit(A_SRP_DET, &motg->inputs); |
| 2608 | set_bit(A_BUS_REQ, &motg->inputs); |
| 2609 | work = 1; |
| 2610 | } else if (otgsc & OTGSC_BSVIS) { |
| 2611 | writel_relaxed(otgsc, USB_OTGSC); |
| 2612 | /* |
| 2613 | * BSV interrupt comes when operating as an A-device |
| 2614 | * (VBUS on/off). |
| 2615 | * But, handle BSV when charger is removed from ACA in ID_A |
| 2616 | */ |
| 2617 | if ((otg->state >= OTG_STATE_A_IDLE) && |
| 2618 | !test_bit(ID_A, &motg->inputs)) |
| 2619 | return IRQ_HANDLED; |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2620 | if (otgsc & OTGSC_BSV) { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2621 | pr_debug("BSV set\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2622 | set_bit(B_SESS_VLD, &motg->inputs); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2623 | } else { |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2624 | pr_debug("BSV clear\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2625 | clear_bit(B_SESS_VLD, &motg->inputs); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2626 | msm_chg_check_aca_intr(motg); |
| 2627 | } |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2628 | work = 1; |
| 2629 | } else if (usbsts & STS_PCI) { |
| 2630 | pc = readl_relaxed(USB_PORTSC); |
| 2631 | pr_debug("portsc = %x\n", pc); |
| 2632 | ret = IRQ_NONE; |
| 2633 | /* |
| 2634 | * HCD Acks PCI interrupt. We use this to switch |
| 2635 | * between different OTG states. |
| 2636 | */ |
| 2637 | work = 1; |
| 2638 | switch (otg->state) { |
| 2639 | case OTG_STATE_A_SUSPEND: |
| 2640 | if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) && |
| 2641 | !(pc & PORTSC_CCS)) { |
| 2642 | pr_debug("B_CONN clear\n"); |
| 2643 | clear_bit(B_CONN, &motg->inputs); |
| 2644 | msm_otg_del_timer(motg); |
| 2645 | } |
| 2646 | break; |
| 2647 | case OTG_STATE_A_PERIPHERAL: |
| 2648 | /* |
| 2649 | * A-peripheral observed activity on bus. |
| 2650 | * clear A_BIDL_ADIS timer. |
| 2651 | */ |
| 2652 | msm_otg_del_timer(motg); |
| 2653 | work = 0; |
| 2654 | break; |
| 2655 | case OTG_STATE_B_WAIT_ACON: |
| 2656 | if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) { |
| 2657 | pr_debug("A_CONN set\n"); |
| 2658 | set_bit(A_CONN, &motg->inputs); |
| 2659 | /* Clear ASE0_BRST timer */ |
| 2660 | msm_otg_del_timer(motg); |
| 2661 | } |
| 2662 | break; |
| 2663 | case OTG_STATE_B_HOST: |
| 2664 | if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) { |
| 2665 | pr_debug("A_CONN clear\n"); |
| 2666 | clear_bit(A_CONN, &motg->inputs); |
| 2667 | msm_otg_del_timer(motg); |
| 2668 | } |
| 2669 | break; |
| 2670 | case OTG_STATE_A_WAIT_BCON: |
| 2671 | if (TA_WAIT_BCON < 0) |
| 2672 | set_bit(A_BUS_REQ, &motg->inputs); |
| 2673 | default: |
| 2674 | work = 0; |
| 2675 | break; |
| 2676 | } |
| 2677 | } else if (usbsts & STS_URI) { |
| 2678 | ret = IRQ_NONE; |
| 2679 | switch (otg->state) { |
| 2680 | case OTG_STATE_A_PERIPHERAL: |
| 2681 | /* |
| 2682 | * A-peripheral observed activity on bus. |
| 2683 | * clear A_BIDL_ADIS timer. |
| 2684 | */ |
| 2685 | msm_otg_del_timer(motg); |
| 2686 | work = 0; |
| 2687 | break; |
| 2688 | default: |
| 2689 | work = 0; |
| 2690 | break; |
| 2691 | } |
| 2692 | } else if (usbsts & STS_SLI) { |
| 2693 | ret = IRQ_NONE; |
| 2694 | work = 0; |
| 2695 | switch (otg->state) { |
| 2696 | case OTG_STATE_B_PERIPHERAL: |
| 2697 | if (otg->gadget->b_hnp_enable) { |
| 2698 | set_bit(A_BUS_SUSPEND, &motg->inputs); |
| 2699 | set_bit(B_BUS_REQ, &motg->inputs); |
| 2700 | work = 1; |
| 2701 | } |
| 2702 | break; |
| 2703 | case OTG_STATE_A_PERIPHERAL: |
| 2704 | msm_otg_start_timer(motg, TA_BIDL_ADIS, |
| 2705 | A_BIDL_ADIS); |
| 2706 | break; |
| 2707 | default: |
| 2708 | break; |
| 2709 | } |
| 2710 | } else if ((usbsts & PHY_ALT_INT)) { |
| 2711 | writel_relaxed(PHY_ALT_INT, USB_USBSTS); |
| 2712 | if (msm_chg_check_aca_intr(motg)) |
| 2713 | work = 1; |
| 2714 | ret = IRQ_HANDLED; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2715 | } |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2716 | if (work) |
| 2717 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2718 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2719 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | static void msm_otg_set_vbus_state(int online) |
| 2723 | { |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 2724 | static bool init; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2725 | struct msm_otg *motg = the_msm_otg; |
| 2726 | |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 2727 | if (online) { |
| 2728 | pr_debug("PMIC: BSV set\n"); |
| 2729 | set_bit(B_SESS_VLD, &motg->inputs); |
| 2730 | } else { |
| 2731 | pr_debug("PMIC: BSV clear\n"); |
| 2732 | clear_bit(B_SESS_VLD, &motg->inputs); |
| 2733 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2734 | |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 2735 | if (!init) { |
| 2736 | init = true; |
| 2737 | complete(&pmic_vbus_init); |
| 2738 | pr_debug("PMIC: BSV init complete\n"); |
| 2739 | return; |
| 2740 | } |
| 2741 | |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2742 | queue_work(system_nrt_wq, &motg->sm_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | static irqreturn_t msm_pmic_id_irq(int irq, void *data) |
| 2746 | { |
| 2747 | struct msm_otg *motg = data; |
| 2748 | |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 2749 | if (aca_id_turned_on) |
| 2750 | return IRQ_HANDLED; |
| 2751 | |
| 2752 | if (irq_read_line(motg->pdata->pmic_id_irq)) { |
| 2753 | pr_debug("PMIC: ID set\n"); |
| 2754 | set_bit(ID, &motg->inputs); |
| 2755 | } else { |
| 2756 | pr_debug("PMIC: ID clear\n"); |
| 2757 | clear_bit(ID, &motg->inputs); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2758 | set_bit(A_BUS_REQ, &motg->inputs); |
Pavankumar Kondeti | 4960f31 | 2011-12-06 15:46:14 +0530 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | if (motg->otg.state != OTG_STATE_UNDEFINED) |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2762 | queue_work(system_nrt_wq, &motg->sm_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2763 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2764 | return IRQ_HANDLED; |
| 2765 | } |
| 2766 | |
| 2767 | static int msm_otg_mode_show(struct seq_file *s, void *unused) |
| 2768 | { |
| 2769 | struct msm_otg *motg = s->private; |
| 2770 | struct otg_transceiver *otg = &motg->otg; |
| 2771 | |
| 2772 | switch (otg->state) { |
| 2773 | case OTG_STATE_A_HOST: |
| 2774 | seq_printf(s, "host\n"); |
| 2775 | break; |
| 2776 | case OTG_STATE_B_PERIPHERAL: |
| 2777 | seq_printf(s, "peripheral\n"); |
| 2778 | break; |
| 2779 | default: |
| 2780 | seq_printf(s, "none\n"); |
| 2781 | break; |
| 2782 | } |
| 2783 | |
| 2784 | return 0; |
| 2785 | } |
| 2786 | |
| 2787 | static int msm_otg_mode_open(struct inode *inode, struct file *file) |
| 2788 | { |
| 2789 | return single_open(file, msm_otg_mode_show, inode->i_private); |
| 2790 | } |
| 2791 | |
| 2792 | static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf, |
| 2793 | size_t count, loff_t *ppos) |
| 2794 | { |
Pavankumar Kondeti | e2904ee | 2011-02-15 09:42:35 +0530 | [diff] [blame] | 2795 | struct seq_file *s = file->private_data; |
| 2796 | struct msm_otg *motg = s->private; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2797 | char buf[16]; |
| 2798 | struct otg_transceiver *otg = &motg->otg; |
| 2799 | int status = count; |
| 2800 | enum usb_mode_type req_mode; |
| 2801 | |
| 2802 | memset(buf, 0x00, sizeof(buf)); |
| 2803 | |
| 2804 | if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) { |
| 2805 | status = -EFAULT; |
| 2806 | goto out; |
| 2807 | } |
| 2808 | |
| 2809 | if (!strncmp(buf, "host", 4)) { |
| 2810 | req_mode = USB_HOST; |
| 2811 | } else if (!strncmp(buf, "peripheral", 10)) { |
| 2812 | req_mode = USB_PERIPHERAL; |
| 2813 | } else if (!strncmp(buf, "none", 4)) { |
| 2814 | req_mode = USB_NONE; |
| 2815 | } else { |
| 2816 | status = -EINVAL; |
| 2817 | goto out; |
| 2818 | } |
| 2819 | |
| 2820 | switch (req_mode) { |
| 2821 | case USB_NONE: |
| 2822 | switch (otg->state) { |
| 2823 | case OTG_STATE_A_HOST: |
| 2824 | case OTG_STATE_B_PERIPHERAL: |
| 2825 | set_bit(ID, &motg->inputs); |
| 2826 | clear_bit(B_SESS_VLD, &motg->inputs); |
| 2827 | break; |
| 2828 | default: |
| 2829 | goto out; |
| 2830 | } |
| 2831 | break; |
| 2832 | case USB_PERIPHERAL: |
| 2833 | switch (otg->state) { |
| 2834 | case OTG_STATE_B_IDLE: |
| 2835 | case OTG_STATE_A_HOST: |
| 2836 | set_bit(ID, &motg->inputs); |
| 2837 | set_bit(B_SESS_VLD, &motg->inputs); |
| 2838 | break; |
| 2839 | default: |
| 2840 | goto out; |
| 2841 | } |
| 2842 | break; |
| 2843 | case USB_HOST: |
| 2844 | switch (otg->state) { |
| 2845 | case OTG_STATE_B_IDLE: |
| 2846 | case OTG_STATE_B_PERIPHERAL: |
| 2847 | clear_bit(ID, &motg->inputs); |
| 2848 | break; |
| 2849 | default: |
| 2850 | goto out; |
| 2851 | } |
| 2852 | break; |
| 2853 | default: |
| 2854 | goto out; |
| 2855 | } |
| 2856 | |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 2857 | pm_runtime_resume(otg->dev); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 2858 | queue_work(system_nrt_wq, &motg->sm_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2859 | out: |
| 2860 | return status; |
| 2861 | } |
| 2862 | |
| 2863 | const struct file_operations msm_otg_mode_fops = { |
| 2864 | .open = msm_otg_mode_open, |
| 2865 | .read = seq_read, |
| 2866 | .write = msm_otg_mode_write, |
| 2867 | .llseek = seq_lseek, |
| 2868 | .release = single_release, |
| 2869 | }; |
| 2870 | |
Chiranjeevi Velempati | f9a1154 | 2012-03-28 18:18:34 +0530 | [diff] [blame] | 2871 | static int msm_otg_show_otg_state(struct seq_file *s, void *unused) |
| 2872 | { |
| 2873 | struct msm_otg *motg = s->private; |
| 2874 | struct otg_transceiver *otg = &motg->otg; |
| 2875 | |
| 2876 | seq_printf(s, "%s\n", otg_state_string(otg->state)); |
| 2877 | return 0; |
| 2878 | } |
| 2879 | |
| 2880 | static int msm_otg_otg_state_open(struct inode *inode, struct file *file) |
| 2881 | { |
| 2882 | return single_open(file, msm_otg_show_otg_state, inode->i_private); |
| 2883 | } |
| 2884 | |
| 2885 | const struct file_operations msm_otg_state_fops = { |
| 2886 | .open = msm_otg_otg_state_open, |
| 2887 | .read = seq_read, |
| 2888 | .llseek = seq_lseek, |
| 2889 | .release = single_release, |
| 2890 | }; |
| 2891 | |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 2892 | static int msm_otg_show_chg_type(struct seq_file *s, void *unused) |
| 2893 | { |
| 2894 | struct msm_otg *motg = s->private; |
| 2895 | |
Pavankumar Kondeti | 9ef69cb | 2011-12-12 14:18:22 +0530 | [diff] [blame] | 2896 | seq_printf(s, "%s\n", chg_to_string(motg->chg_type)); |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 2897 | return 0; |
| 2898 | } |
| 2899 | |
| 2900 | static int msm_otg_chg_open(struct inode *inode, struct file *file) |
| 2901 | { |
| 2902 | return single_open(file, msm_otg_show_chg_type, inode->i_private); |
| 2903 | } |
| 2904 | |
| 2905 | const struct file_operations msm_otg_chg_fops = { |
| 2906 | .open = msm_otg_chg_open, |
| 2907 | .read = seq_read, |
| 2908 | .llseek = seq_lseek, |
| 2909 | .release = single_release, |
| 2910 | }; |
| 2911 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 2912 | static int msm_otg_aca_show(struct seq_file *s, void *unused) |
| 2913 | { |
| 2914 | if (debug_aca_enabled) |
| 2915 | seq_printf(s, "enabled\n"); |
| 2916 | else |
| 2917 | seq_printf(s, "disabled\n"); |
| 2918 | |
| 2919 | return 0; |
| 2920 | } |
| 2921 | |
| 2922 | static int msm_otg_aca_open(struct inode *inode, struct file *file) |
| 2923 | { |
| 2924 | return single_open(file, msm_otg_aca_show, inode->i_private); |
| 2925 | } |
| 2926 | |
| 2927 | static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf, |
| 2928 | size_t count, loff_t *ppos) |
| 2929 | { |
| 2930 | char buf[8]; |
| 2931 | |
| 2932 | memset(buf, 0x00, sizeof(buf)); |
| 2933 | |
| 2934 | if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) |
| 2935 | return -EFAULT; |
| 2936 | |
| 2937 | if (!strncmp(buf, "enable", 6)) |
| 2938 | debug_aca_enabled = true; |
| 2939 | else |
| 2940 | debug_aca_enabled = false; |
| 2941 | |
| 2942 | return count; |
| 2943 | } |
| 2944 | |
| 2945 | const struct file_operations msm_otg_aca_fops = { |
| 2946 | .open = msm_otg_aca_open, |
| 2947 | .read = seq_read, |
| 2948 | .write = msm_otg_aca_write, |
| 2949 | .llseek = seq_lseek, |
| 2950 | .release = single_release, |
| 2951 | }; |
| 2952 | |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 2953 | static int msm_otg_bus_show(struct seq_file *s, void *unused) |
| 2954 | { |
| 2955 | if (debug_bus_voting_enabled) |
| 2956 | seq_printf(s, "enabled\n"); |
| 2957 | else |
| 2958 | seq_printf(s, "disabled\n"); |
| 2959 | |
| 2960 | return 0; |
| 2961 | } |
| 2962 | |
| 2963 | static int msm_otg_bus_open(struct inode *inode, struct file *file) |
| 2964 | { |
| 2965 | return single_open(file, msm_otg_bus_show, inode->i_private); |
| 2966 | } |
| 2967 | |
| 2968 | static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf, |
| 2969 | size_t count, loff_t *ppos) |
| 2970 | { |
| 2971 | char buf[8]; |
| 2972 | int ret; |
| 2973 | struct seq_file *s = file->private_data; |
| 2974 | struct msm_otg *motg = s->private; |
| 2975 | |
| 2976 | memset(buf, 0x00, sizeof(buf)); |
| 2977 | |
| 2978 | if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) |
| 2979 | return -EFAULT; |
| 2980 | |
| 2981 | if (!strncmp(buf, "enable", 6)) { |
| 2982 | /* Do not vote here. Let OTG statemachine decide when to vote */ |
| 2983 | debug_bus_voting_enabled = true; |
| 2984 | } else { |
| 2985 | debug_bus_voting_enabled = false; |
| 2986 | if (motg->bus_perf_client) { |
| 2987 | ret = msm_bus_scale_client_update_request( |
| 2988 | motg->bus_perf_client, 0); |
| 2989 | if (ret) |
| 2990 | dev_err(motg->otg.dev, "%s: Failed to devote " |
| 2991 | "for bus bw %d\n", __func__, ret); |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | return count; |
| 2996 | } |
| 2997 | |
| 2998 | const struct file_operations msm_otg_bus_fops = { |
| 2999 | .open = msm_otg_bus_open, |
| 3000 | .read = seq_read, |
| 3001 | .write = msm_otg_bus_write, |
| 3002 | .llseek = seq_lseek, |
| 3003 | .release = single_release, |
| 3004 | }; |
| 3005 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3006 | static struct dentry *msm_otg_dbg_root; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3007 | |
| 3008 | static int msm_otg_debugfs_init(struct msm_otg *motg) |
| 3009 | { |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3010 | struct dentry *msm_otg_dentry; |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 3011 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3012 | msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL); |
| 3013 | |
| 3014 | if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root)) |
| 3015 | return -ENODEV; |
| 3016 | |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 3017 | if (motg->pdata->mode == USB_OTG && |
| 3018 | motg->pdata->otg_control == OTG_USER_CONTROL) { |
| 3019 | |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3020 | msm_otg_dentry = debugfs_create_file("mode", S_IRUGO | |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 3021 | S_IWUSR, msm_otg_dbg_root, motg, |
| 3022 | &msm_otg_mode_fops); |
| 3023 | |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3024 | if (!msm_otg_dentry) { |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 3025 | debugfs_remove(msm_otg_dbg_root); |
| 3026 | msm_otg_dbg_root = NULL; |
| 3027 | return -ENODEV; |
| 3028 | } |
| 3029 | } |
| 3030 | |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3031 | msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO, |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 3032 | msm_otg_dbg_root, motg, |
| 3033 | &msm_otg_chg_fops); |
| 3034 | |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3035 | if (!msm_otg_dentry) { |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 3036 | debugfs_remove_recursive(msm_otg_dbg_root); |
| 3037 | return -ENODEV; |
| 3038 | } |
| 3039 | |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3040 | msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR, |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 3041 | msm_otg_dbg_root, motg, |
| 3042 | &msm_otg_aca_fops); |
| 3043 | |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3044 | if (!msm_otg_dentry) { |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 3045 | debugfs_remove_recursive(msm_otg_dbg_root); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3046 | return -ENODEV; |
| 3047 | } |
| 3048 | |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3049 | msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR, |
| 3050 | msm_otg_dbg_root, motg, |
| 3051 | &msm_otg_bus_fops); |
| 3052 | |
| 3053 | if (!msm_otg_dentry) { |
| 3054 | debugfs_remove_recursive(msm_otg_dbg_root); |
| 3055 | return -ENODEV; |
| 3056 | } |
Chiranjeevi Velempati | f9a1154 | 2012-03-28 18:18:34 +0530 | [diff] [blame] | 3057 | |
| 3058 | msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO, |
| 3059 | msm_otg_dbg_root, motg, &msm_otg_state_fops); |
| 3060 | |
| 3061 | if (!msm_otg_dentry) { |
| 3062 | debugfs_remove_recursive(msm_otg_dbg_root); |
| 3063 | return -ENODEV; |
| 3064 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3065 | return 0; |
| 3066 | } |
| 3067 | |
| 3068 | static void msm_otg_debugfs_cleanup(void) |
| 3069 | { |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 3070 | debugfs_remove_recursive(msm_otg_dbg_root); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3071 | } |
| 3072 | |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3073 | static u64 msm_otg_dma_mask = DMA_BIT_MASK(64); |
| 3074 | static struct platform_device *msm_otg_add_pdev( |
| 3075 | struct platform_device *ofdev, const char *name) |
| 3076 | { |
| 3077 | struct platform_device *pdev; |
| 3078 | const struct resource *res = ofdev->resource; |
| 3079 | unsigned int num = ofdev->num_resources; |
| 3080 | int retval; |
| 3081 | |
| 3082 | pdev = platform_device_alloc(name, -1); |
| 3083 | if (!pdev) { |
| 3084 | retval = -ENOMEM; |
| 3085 | goto error; |
| 3086 | } |
| 3087 | |
| 3088 | pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
| 3089 | pdev->dev.dma_mask = &msm_otg_dma_mask; |
| 3090 | |
| 3091 | if (num) { |
| 3092 | retval = platform_device_add_resources(pdev, res, num); |
| 3093 | if (retval) |
| 3094 | goto error; |
| 3095 | } |
| 3096 | |
| 3097 | retval = platform_device_add(pdev); |
| 3098 | if (retval) |
| 3099 | goto error; |
| 3100 | |
| 3101 | return pdev; |
| 3102 | |
| 3103 | error: |
| 3104 | platform_device_put(pdev); |
| 3105 | return ERR_PTR(retval); |
| 3106 | } |
| 3107 | |
| 3108 | static int msm_otg_setup_devices(struct platform_device *ofdev, |
| 3109 | enum usb_mode_type mode, bool init) |
| 3110 | { |
| 3111 | const char *gadget_name = "msm_hsusb"; |
| 3112 | const char *host_name = "msm_hsusb_host"; |
| 3113 | static struct platform_device *gadget_pdev; |
| 3114 | static struct platform_device *host_pdev; |
| 3115 | int retval = 0; |
| 3116 | |
| 3117 | if (!init) { |
| 3118 | if (gadget_pdev) |
| 3119 | platform_device_unregister(gadget_pdev); |
| 3120 | if (host_pdev) |
| 3121 | platform_device_unregister(host_pdev); |
| 3122 | return 0; |
| 3123 | } |
| 3124 | |
| 3125 | switch (mode) { |
| 3126 | case USB_OTG: |
| 3127 | /* fall through */ |
| 3128 | case USB_PERIPHERAL: |
| 3129 | gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name); |
| 3130 | if (IS_ERR(gadget_pdev)) { |
| 3131 | retval = PTR_ERR(gadget_pdev); |
| 3132 | break; |
| 3133 | } |
| 3134 | if (mode == USB_PERIPHERAL) |
| 3135 | break; |
| 3136 | /* fall through */ |
| 3137 | case USB_HOST: |
| 3138 | host_pdev = msm_otg_add_pdev(ofdev, host_name); |
| 3139 | if (IS_ERR(host_pdev)) { |
| 3140 | retval = PTR_ERR(host_pdev); |
| 3141 | if (mode == USB_OTG) |
| 3142 | platform_device_unregister(gadget_pdev); |
| 3143 | } |
| 3144 | break; |
| 3145 | default: |
| 3146 | break; |
| 3147 | } |
| 3148 | |
| 3149 | return retval; |
| 3150 | } |
| 3151 | |
| 3152 | struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev) |
| 3153 | { |
| 3154 | struct device_node *node = pdev->dev.of_node; |
| 3155 | struct msm_otg_platform_data *pdata; |
| 3156 | int len = 0; |
| 3157 | |
| 3158 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 3159 | if (!pdata) { |
| 3160 | pr_err("unable to allocate platform data\n"); |
| 3161 | return NULL; |
| 3162 | } |
| 3163 | of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len); |
| 3164 | if (len) { |
| 3165 | pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); |
| 3166 | if (!pdata->phy_init_seq) |
| 3167 | return NULL; |
| 3168 | of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq", |
| 3169 | pdata->phy_init_seq, |
| 3170 | len/sizeof(*pdata->phy_init_seq)); |
| 3171 | } |
| 3172 | of_property_read_u32(node, "qcom,hsusb-otg-power-budget", |
| 3173 | &pdata->power_budget); |
| 3174 | of_property_read_u32(node, "qcom,hsusb-otg-mode", |
| 3175 | &pdata->mode); |
| 3176 | of_property_read_u32(node, "qcom,hsusb-otg-otg-control", |
| 3177 | &pdata->otg_control); |
| 3178 | of_property_read_u32(node, "qcom,hsusb-otg-default-mode", |
| 3179 | &pdata->default_mode); |
| 3180 | of_property_read_u32(node, "qcom,hsusb-otg-phy-type", |
| 3181 | &pdata->phy_type); |
| 3182 | of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq", |
| 3183 | &pdata->pmic_id_irq); |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3184 | return pdata; |
| 3185 | } |
| 3186 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3187 | static int __init msm_otg_probe(struct platform_device *pdev) |
| 3188 | { |
| 3189 | int ret = 0; |
| 3190 | struct resource *res; |
| 3191 | struct msm_otg *motg; |
| 3192 | struct otg_transceiver *otg; |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3193 | struct msm_otg_platform_data *pdata; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3194 | |
| 3195 | dev_info(&pdev->dev, "msm_otg probe\n"); |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3196 | |
| 3197 | if (pdev->dev.of_node) { |
| 3198 | dev_dbg(&pdev->dev, "device tree enabled\n"); |
| 3199 | pdata = msm_otg_dt_to_pdata(pdev); |
| 3200 | if (!pdata) |
| 3201 | return -ENOMEM; |
| 3202 | ret = msm_otg_setup_devices(pdev, pdata->mode, true); |
| 3203 | if (ret) { |
| 3204 | dev_err(&pdev->dev, "devices setup failed\n"); |
| 3205 | return ret; |
| 3206 | } |
| 3207 | } else if (!pdev->dev.platform_data) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3208 | dev_err(&pdev->dev, "No platform data given. Bailing out\n"); |
| 3209 | return -ENODEV; |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3210 | } else { |
| 3211 | pdata = pdev->dev.platform_data; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL); |
| 3215 | if (!motg) { |
| 3216 | dev_err(&pdev->dev, "unable to allocate msm_otg\n"); |
| 3217 | return -ENOMEM; |
| 3218 | } |
| 3219 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3220 | the_msm_otg = motg; |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3221 | motg->pdata = pdata; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3222 | otg = &motg->otg; |
| 3223 | otg->dev = &pdev->dev; |
| 3224 | |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 3225 | /* |
| 3226 | * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence |
| 3227 | * PHY treat ACA ID_GND as float and no interrupt is generated. But |
| 3228 | * PMIC can detect ACA ID_GND and generate an interrupt. |
| 3229 | */ |
| 3230 | if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) { |
| 3231 | dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n"); |
| 3232 | ret = -EINVAL; |
| 3233 | goto free_motg; |
| 3234 | } |
| 3235 | |
Ofir Cohen | 4da266f | 2012-01-03 10:19:29 +0200 | [diff] [blame] | 3236 | /* initialize reset counter */ |
| 3237 | motg->reset_counter = 0; |
| 3238 | |
Amit Blay | 02eff13 | 2011-09-21 16:46:24 +0300 | [diff] [blame] | 3239 | /* Some targets don't support PHY clock. */ |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3240 | motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk"); |
Amit Blay | 02eff13 | 2011-09-21 16:46:24 +0300 | [diff] [blame] | 3241 | if (IS_ERR(motg->phy_reset_clk)) |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3242 | dev_err(&pdev->dev, "failed to get phy_clk\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3243 | |
Pavankumar Kondeti | 923262e | 2012-04-20 15:34:24 +0530 | [diff] [blame^] | 3244 | /* |
| 3245 | * Targets on which link uses asynchronous reset methodology, |
| 3246 | * free running clock is not required during the reset. |
| 3247 | */ |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3248 | motg->clk = clk_get(&pdev->dev, "alt_core_clk"); |
Pavankumar Kondeti | 923262e | 2012-04-20 15:34:24 +0530 | [diff] [blame^] | 3249 | if (IS_ERR(motg->clk)) |
| 3250 | dev_dbg(&pdev->dev, "alt_core_clk is not present\n"); |
| 3251 | else |
| 3252 | clk_set_rate(motg->clk, 60000000); |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 3253 | |
Anji jonnala | a7c1c5c | 2011-12-12 12:20:36 +0530 | [diff] [blame] | 3254 | /* pm qos request to prevent apps idle power collapse */ |
| 3255 | if (motg->pdata->swfi_latency) |
| 3256 | pm_qos_add_request(&motg->pm_qos_req_dma, |
| 3257 | PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3258 | |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 3259 | /* |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3260 | * USB Core is running its protocol engine based on CORE CLK, |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 3261 | * CORE CLK must be running at >55Mhz for correct HSUSB |
| 3262 | * operation and USB core cannot tolerate frequency changes on |
| 3263 | * CORE CLK. For such USB cores, vote for maximum clk frequency |
| 3264 | * on pclk source |
| 3265 | */ |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3266 | motg->core_clk = clk_get(&pdev->dev, "core_clk"); |
| 3267 | if (IS_ERR(motg->core_clk)) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3268 | motg->core_clk = NULL; |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3269 | dev_err(&pdev->dev, "failed to get core_clk\n"); |
Pavankumar Kondeti | bc54133 | 2012-04-20 15:32:04 +0530 | [diff] [blame] | 3270 | ret = PTR_ERR(motg->core_clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3271 | goto put_clk; |
| 3272 | } |
| 3273 | clk_set_rate(motg->core_clk, INT_MAX); |
| 3274 | |
| 3275 | motg->pclk = clk_get(&pdev->dev, "iface_clk"); |
| 3276 | if (IS_ERR(motg->pclk)) { |
| 3277 | dev_err(&pdev->dev, "failed to get iface_clk\n"); |
| 3278 | ret = PTR_ERR(motg->pclk); |
| 3279 | goto put_core_clk; |
| 3280 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3281 | |
| 3282 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 3283 | if (!res) { |
| 3284 | dev_err(&pdev->dev, "failed to get platform resource mem\n"); |
| 3285 | ret = -ENODEV; |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3286 | goto put_pclk; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | motg->regs = ioremap(res->start, resource_size(res)); |
| 3290 | if (!motg->regs) { |
| 3291 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 3292 | ret = -ENOMEM; |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3293 | goto put_pclk; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3294 | } |
| 3295 | dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs); |
| 3296 | |
| 3297 | motg->irq = platform_get_irq(pdev, 0); |
| 3298 | if (!motg->irq) { |
| 3299 | dev_err(&pdev->dev, "platform_get_irq failed\n"); |
| 3300 | ret = -ENODEV; |
| 3301 | goto free_regs; |
| 3302 | } |
| 3303 | |
Stephen Boyd | 30ad10b | 2012-03-01 14:51:04 -0800 | [diff] [blame] | 3304 | motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb"); |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 3305 | if (IS_ERR(motg->xo_handle)) { |
| 3306 | dev_err(&pdev->dev, "%s not able to get the handle " |
| 3307 | "to vote for TCXO D0 buffer\n", __func__); |
| 3308 | ret = PTR_ERR(motg->xo_handle); |
| 3309 | goto free_regs; |
| 3310 | } |
| 3311 | |
Stephen Boyd | 30ad10b | 2012-03-01 14:51:04 -0800 | [diff] [blame] | 3312 | ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON); |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 3313 | if (ret) { |
| 3314 | dev_err(&pdev->dev, "%s failed to vote for TCXO " |
| 3315 | "D0 buffer%d\n", __func__, ret); |
| 3316 | goto free_xo_handle; |
| 3317 | } |
| 3318 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 3319 | clk_prepare_enable(motg->pclk); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3320 | |
| 3321 | ret = msm_hsusb_init_vddcx(motg, 1); |
| 3322 | if (ret) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3323 | dev_err(&pdev->dev, "hsusb vddcx init failed\n"); |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 3324 | goto devote_xo_handle; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3325 | } |
| 3326 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3327 | ret = msm_hsusb_config_vddcx(1); |
| 3328 | if (ret) { |
| 3329 | dev_err(&pdev->dev, "hsusb vddcx configuration failed\n"); |
| 3330 | goto free_init_vddcx; |
| 3331 | } |
| 3332 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3333 | ret = msm_hsusb_ldo_init(motg, 1); |
| 3334 | if (ret) { |
| 3335 | dev_err(&pdev->dev, "hsusb vreg configuration failed\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3336 | goto free_init_vddcx; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3337 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3338 | |
Mayank Rana | 9e9a2ac | 2012-03-24 04:05:28 +0530 | [diff] [blame] | 3339 | if (pdata->mhl_enable) { |
| 3340 | mhl_analog_switch = devm_regulator_get(motg->otg.dev, |
| 3341 | "mhl_ext_3p3v"); |
| 3342 | if (IS_ERR(mhl_analog_switch)) { |
| 3343 | dev_err(&pdev->dev, "Unable to get mhl_analog_switch\n"); |
| 3344 | goto free_ldo_init; |
| 3345 | } |
| 3346 | } |
| 3347 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3348 | ret = msm_hsusb_ldo_enable(motg, 1); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3349 | if (ret) { |
| 3350 | dev_err(&pdev->dev, "hsusb vreg enable failed\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3351 | goto free_ldo_init; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3352 | } |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 3353 | clk_prepare_enable(motg->core_clk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3354 | |
| 3355 | writel(0, USB_USBINTR); |
| 3356 | writel(0, USB_OTGSC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3357 | /* Ensure that above STOREs are completed before enabling interrupts */ |
| 3358 | mb(); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3359 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3360 | wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg"); |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 3361 | msm_otg_init_timer(motg); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3362 | INIT_WORK(&motg->sm_work, msm_otg_sm_work); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 3363 | INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 3364 | setup_timer(&motg->id_timer, msm_otg_id_timer_func, |
| 3365 | (unsigned long) motg); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3366 | ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED, |
| 3367 | "msm_otg", motg); |
| 3368 | if (ret) { |
| 3369 | dev_err(&pdev->dev, "request irq failed\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3370 | goto destroy_wlock; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | otg->init = msm_otg_reset; |
| 3374 | otg->set_host = msm_otg_set_host; |
| 3375 | otg->set_peripheral = msm_otg_set_peripheral; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 3376 | otg->set_power = msm_otg_set_power; |
Vijayavardhan Vennapusa | a315203 | 2012-03-05 16:29:30 +0530 | [diff] [blame] | 3377 | otg->start_hnp = msm_otg_start_hnp; |
| 3378 | otg->start_srp = msm_otg_start_srp; |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 3379 | otg->set_suspend = msm_otg_set_suspend; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3380 | |
| 3381 | otg->io_ops = &msm_otg_io_ops; |
| 3382 | |
| 3383 | ret = otg_set_transceiver(&motg->otg); |
| 3384 | if (ret) { |
| 3385 | dev_err(&pdev->dev, "otg_set_transceiver failed\n"); |
| 3386 | goto free_irq; |
| 3387 | } |
| 3388 | |
Pavankumar Kondeti | 0d81f31 | 2012-01-13 11:34:10 +0530 | [diff] [blame] | 3389 | if (motg->pdata->mode == USB_OTG && |
| 3390 | motg->pdata->otg_control == OTG_PMIC_CONTROL) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3391 | if (motg->pdata->pmic_id_irq) { |
| 3392 | ret = request_irq(motg->pdata->pmic_id_irq, |
| 3393 | msm_pmic_id_irq, |
| 3394 | IRQF_TRIGGER_RISING | |
| 3395 | IRQF_TRIGGER_FALLING, |
| 3396 | "msm_otg", motg); |
| 3397 | if (ret) { |
| 3398 | dev_err(&pdev->dev, "request irq failed for PMIC ID\n"); |
| 3399 | goto remove_otg; |
| 3400 | } |
| 3401 | } else { |
| 3402 | ret = -ENODEV; |
| 3403 | dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n"); |
| 3404 | goto remove_otg; |
| 3405 | } |
| 3406 | } |
| 3407 | |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 3408 | msm_hsusb_mhl_switch_enable(motg, 1); |
| 3409 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3410 | platform_set_drvdata(pdev, motg); |
| 3411 | device_init_wakeup(&pdev->dev, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3412 | motg->mA_port = IUNIT; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3413 | |
Anji jonnala | d270e2d | 2011-08-09 11:28:32 +0530 | [diff] [blame] | 3414 | ret = msm_otg_debugfs_init(motg); |
| 3415 | if (ret) |
| 3416 | dev_dbg(&pdev->dev, "mode debugfs file is" |
| 3417 | "not available\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3418 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3419 | if (motg->pdata->otg_control == OTG_PMIC_CONTROL) |
| 3420 | pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state); |
| 3421 | |
Amit Blay | 58b3147 | 2011-11-18 09:39:39 +0200 | [diff] [blame] | 3422 | if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) { |
| 3423 | if (motg->pdata->otg_control == OTG_PMIC_CONTROL && |
Pavankumar Kondeti | 0d81f31 | 2012-01-13 11:34:10 +0530 | [diff] [blame] | 3424 | (!(motg->pdata->mode == USB_OTG) || |
| 3425 | motg->pdata->pmic_id_irq)) |
Amit Blay | 58b3147 | 2011-11-18 09:39:39 +0200 | [diff] [blame] | 3426 | motg->caps = ALLOW_PHY_POWER_COLLAPSE | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3427 | ALLOW_PHY_RETENTION | |
| 3428 | ALLOW_PHY_COMP_DISABLE; |
| 3429 | |
Amit Blay | 58b3147 | 2011-11-18 09:39:39 +0200 | [diff] [blame] | 3430 | if (motg->pdata->otg_control == OTG_PHY_CONTROL) |
| 3431 | motg->caps = ALLOW_PHY_RETENTION; |
| 3432 | } |
| 3433 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3434 | wake_lock(&motg->wlock); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3435 | pm_runtime_set_active(&pdev->dev); |
| 3436 | pm_runtime_enable(&pdev->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3437 | |
Manu Gautam | cd82e9d | 2011-12-20 14:17:28 +0530 | [diff] [blame] | 3438 | if (motg->pdata->bus_scale_table) { |
| 3439 | motg->bus_perf_client = |
| 3440 | msm_bus_scale_register_client(motg->pdata->bus_scale_table); |
| 3441 | if (!motg->bus_perf_client) |
| 3442 | dev_err(motg->otg.dev, "%s: Failed to register BUS " |
| 3443 | "scaling client!!\n", __func__); |
Manu Gautam | 8bdcc59 | 2012-03-06 11:26:06 +0530 | [diff] [blame] | 3444 | else |
| 3445 | debug_bus_voting_enabled = true; |
Manu Gautam | cd82e9d | 2011-12-20 14:17:28 +0530 | [diff] [blame] | 3446 | } |
| 3447 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3448 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3449 | |
| 3450 | remove_otg: |
| 3451 | otg_set_transceiver(NULL); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3452 | free_irq: |
| 3453 | free_irq(motg->irq, motg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3454 | destroy_wlock: |
| 3455 | wake_lock_destroy(&motg->wlock); |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 3456 | clk_disable_unprepare(motg->core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3457 | msm_hsusb_ldo_enable(motg, 0); |
| 3458 | free_ldo_init: |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3459 | msm_hsusb_ldo_init(motg, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3460 | free_init_vddcx: |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3461 | msm_hsusb_init_vddcx(motg, 0); |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 3462 | devote_xo_handle: |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 3463 | clk_disable_unprepare(motg->pclk); |
Stephen Boyd | 30ad10b | 2012-03-01 14:51:04 -0800 | [diff] [blame] | 3464 | msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF); |
Anji jonnala | 7da3f26 | 2011-12-02 17:22:14 -0800 | [diff] [blame] | 3465 | free_xo_handle: |
Stephen Boyd | 30ad10b | 2012-03-01 14:51:04 -0800 | [diff] [blame] | 3466 | msm_xo_put(motg->xo_handle); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3467 | free_regs: |
| 3468 | iounmap(motg->regs); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3469 | put_pclk: |
| 3470 | clk_put(motg->pclk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3471 | put_core_clk: |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3472 | clk_put(motg->core_clk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3473 | put_clk: |
Pavankumar Kondeti | 923262e | 2012-04-20 15:34:24 +0530 | [diff] [blame^] | 3474 | if (!IS_ERR(motg->clk)) |
| 3475 | clk_put(motg->clk); |
Amit Blay | 02eff13 | 2011-09-21 16:46:24 +0300 | [diff] [blame] | 3476 | if (!IS_ERR(motg->phy_reset_clk)) |
| 3477 | clk_put(motg->phy_reset_clk); |
Pavankumar Kondeti | aa449e1 | 2011-11-04 11:09:26 +0530 | [diff] [blame] | 3478 | free_motg: |
Anji jonnala | a7c1c5c | 2011-12-12 12:20:36 +0530 | [diff] [blame] | 3479 | if (motg->pdata->swfi_latency) |
| 3480 | pm_qos_remove_request(&motg->pm_qos_req_dma); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3481 | kfree(motg); |
| 3482 | return ret; |
| 3483 | } |
| 3484 | |
| 3485 | static int __devexit msm_otg_remove(struct platform_device *pdev) |
| 3486 | { |
| 3487 | struct msm_otg *motg = platform_get_drvdata(pdev); |
| 3488 | struct otg_transceiver *otg = &motg->otg; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3489 | int cnt = 0; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3490 | |
| 3491 | if (otg->host || otg->gadget) |
| 3492 | return -EBUSY; |
| 3493 | |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3494 | if (pdev->dev.of_node) |
| 3495 | msm_otg_setup_devices(pdev, motg->pdata->mode, false); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3496 | if (motg->pdata->otg_control == OTG_PMIC_CONTROL) |
| 3497 | pm8921_charger_unregister_vbus_sn(0); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3498 | msm_otg_debugfs_cleanup(); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 3499 | cancel_delayed_work_sync(&motg->chg_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3500 | cancel_work_sync(&motg->sm_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3501 | |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 3502 | pm_runtime_resume(&pdev->dev); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3503 | |
| 3504 | device_init_wakeup(&pdev->dev, 0); |
| 3505 | pm_runtime_disable(&pdev->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3506 | wake_lock_destroy(&motg->wlock); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3507 | |
Vijayavardhan Vennapusa | fc464f0 | 2011-11-04 21:54:00 +0530 | [diff] [blame] | 3508 | msm_hsusb_mhl_switch_enable(motg, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3509 | if (motg->pdata->pmic_id_irq) |
| 3510 | free_irq(motg->pdata->pmic_id_irq, motg); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3511 | otg_set_transceiver(NULL); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3512 | free_irq(motg->irq, motg); |
| 3513 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3514 | /* |
| 3515 | * Put PHY in low power mode. |
| 3516 | */ |
| 3517 | ulpi_read(otg, 0x14); |
| 3518 | ulpi_write(otg, 0x08, 0x09); |
| 3519 | |
| 3520 | writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC); |
| 3521 | while (cnt < PHY_SUSPEND_TIMEOUT_USEC) { |
| 3522 | if (readl(USB_PORTSC) & PORTSC_PHCD) |
| 3523 | break; |
| 3524 | udelay(1); |
| 3525 | cnt++; |
| 3526 | } |
| 3527 | if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) |
| 3528 | dev_err(otg->dev, "Unable to suspend PHY\n"); |
| 3529 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 3530 | clk_disable_unprepare(motg->pclk); |
| 3531 | clk_disable_unprepare(motg->core_clk); |
Stephen Boyd | 30ad10b | 2012-03-01 14:51:04 -0800 | [diff] [blame] | 3532 | msm_xo_put(motg->xo_handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3533 | msm_hsusb_ldo_enable(motg, 0); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 3534 | msm_hsusb_ldo_init(motg, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3535 | msm_hsusb_init_vddcx(motg, 0); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3536 | |
| 3537 | iounmap(motg->regs); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3538 | pm_runtime_set_suspended(&pdev->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3539 | |
Amit Blay | 02eff13 | 2011-09-21 16:46:24 +0300 | [diff] [blame] | 3540 | if (!IS_ERR(motg->phy_reset_clk)) |
| 3541 | clk_put(motg->phy_reset_clk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3542 | clk_put(motg->pclk); |
Pavankumar Kondeti | 923262e | 2012-04-20 15:34:24 +0530 | [diff] [blame^] | 3543 | if (!IS_ERR(motg->clk)) |
| 3544 | clk_put(motg->clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 3545 | clk_put(motg->core_clk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3546 | |
Anji jonnala | a7c1c5c | 2011-12-12 12:20:36 +0530 | [diff] [blame] | 3547 | if (motg->pdata->swfi_latency) |
| 3548 | pm_qos_remove_request(&motg->pm_qos_req_dma); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3549 | |
Manu Gautam | cd82e9d | 2011-12-20 14:17:28 +0530 | [diff] [blame] | 3550 | if (motg->bus_perf_client) |
| 3551 | msm_bus_scale_unregister_client(motg->bus_perf_client); |
| 3552 | |
Anji jonnala | a7c1c5c | 2011-12-12 12:20:36 +0530 | [diff] [blame] | 3553 | kfree(motg); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3554 | return 0; |
| 3555 | } |
| 3556 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3557 | #ifdef CONFIG_PM_RUNTIME |
| 3558 | static int msm_otg_runtime_idle(struct device *dev) |
| 3559 | { |
| 3560 | struct msm_otg *motg = dev_get_drvdata(dev); |
| 3561 | struct otg_transceiver *otg = &motg->otg; |
| 3562 | |
| 3563 | dev_dbg(dev, "OTG runtime idle\n"); |
| 3564 | |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 3565 | if (otg->state == OTG_STATE_UNDEFINED) |
| 3566 | return -EAGAIN; |
| 3567 | else |
| 3568 | return 0; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | static int msm_otg_runtime_suspend(struct device *dev) |
| 3572 | { |
| 3573 | struct msm_otg *motg = dev_get_drvdata(dev); |
| 3574 | |
| 3575 | dev_dbg(dev, "OTG runtime suspend\n"); |
| 3576 | return msm_otg_suspend(motg); |
| 3577 | } |
| 3578 | |
| 3579 | static int msm_otg_runtime_resume(struct device *dev) |
| 3580 | { |
| 3581 | struct msm_otg *motg = dev_get_drvdata(dev); |
| 3582 | |
| 3583 | dev_dbg(dev, "OTG runtime resume\n"); |
Pavankumar Kondeti | 8be99cf | 2011-08-04 10:48:08 +0530 | [diff] [blame] | 3584 | pm_runtime_get_noresume(dev); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3585 | return msm_otg_resume(motg); |
| 3586 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3587 | #endif |
| 3588 | |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 3589 | #ifdef CONFIG_PM_SLEEP |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3590 | static int msm_otg_pm_suspend(struct device *dev) |
| 3591 | { |
Jack Pham | d44455a | 2012-04-10 16:23:49 -0700 | [diff] [blame] | 3592 | struct msm_otg *motg = dev_get_drvdata(dev); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3593 | |
| 3594 | dev_dbg(dev, "OTG PM suspend\n"); |
Jack Pham | d44455a | 2012-04-10 16:23:49 -0700 | [diff] [blame] | 3595 | return msm_otg_suspend(motg); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | static int msm_otg_pm_resume(struct device *dev) |
| 3599 | { |
| 3600 | struct msm_otg *motg = dev_get_drvdata(dev); |
Jack Pham | d44455a | 2012-04-10 16:23:49 -0700 | [diff] [blame] | 3601 | int ret; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3602 | |
| 3603 | dev_dbg(dev, "OTG PM resume\n"); |
Jack Pham | d44455a | 2012-04-10 16:23:49 -0700 | [diff] [blame] | 3604 | ret = msm_otg_resume(motg); |
| 3605 | if (ret) |
| 3606 | return ret; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3607 | |
Jack Pham | d44455a | 2012-04-10 16:23:49 -0700 | [diff] [blame] | 3608 | /* Update runtime PM status */ |
| 3609 | pm_runtime_disable(dev); |
| 3610 | pm_runtime_set_active(dev); |
| 3611 | pm_runtime_enable(dev); |
| 3612 | |
Gregory Bean | ebd8ca2 | 2011-10-11 12:02:35 -0700 | [diff] [blame] | 3613 | return 0; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3614 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3615 | #endif |
| 3616 | |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 3617 | #ifdef CONFIG_PM |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3618 | static const struct dev_pm_ops msm_otg_dev_pm_ops = { |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 3619 | SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume) |
| 3620 | SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume, |
| 3621 | msm_otg_runtime_idle) |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3622 | }; |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 3623 | #endif |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3624 | |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3625 | static struct of_device_id msm_otg_dt_match[] = { |
| 3626 | { .compatible = "qcom,hsusb-otg", |
| 3627 | }, |
| 3628 | {} |
| 3629 | }; |
| 3630 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3631 | static struct platform_driver msm_otg_driver = { |
| 3632 | .remove = __devexit_p(msm_otg_remove), |
| 3633 | .driver = { |
| 3634 | .name = DRIVER_NAME, |
| 3635 | .owner = THIS_MODULE, |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 3636 | #ifdef CONFIG_PM |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 3637 | .pm = &msm_otg_dev_pm_ops, |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 3638 | #endif |
Pavankumar Kondeti | eaea7fe | 2011-10-27 14:46:45 +0530 | [diff] [blame] | 3639 | .of_match_table = msm_otg_dt_match, |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 3640 | }, |
| 3641 | }; |
| 3642 | |
| 3643 | static int __init msm_otg_init(void) |
| 3644 | { |
| 3645 | return platform_driver_probe(&msm_otg_driver, msm_otg_probe); |
| 3646 | } |
| 3647 | |
| 3648 | static void __exit msm_otg_exit(void) |
| 3649 | { |
| 3650 | platform_driver_unregister(&msm_otg_driver); |
| 3651 | } |
| 3652 | |
| 3653 | module_init(msm_otg_init); |
| 3654 | module_exit(msm_otg_exit); |
| 3655 | |
| 3656 | MODULE_LICENSE("GPL v2"); |
| 3657 | MODULE_DESCRIPTION("MSM USB transceiver driver"); |