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