Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/miscdevice.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/irq.h> |
| 25 | #include <linux/ioctl.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/reboot.h> |
| 28 | #include <linux/debugfs.h> |
| 29 | #include <linux/completion.h> |
| 30 | #include <linux/workqueue.h> |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 31 | #include <linux/mfd/pmic8058.h> |
| 32 | #include <asm/mach-types.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <mach/mdm2.h> |
| 35 | #include <mach/restart.h> |
| 36 | #include <mach/subsystem_notif.h> |
| 37 | #include <mach/subsystem_restart.h> |
| 38 | #include <linux/msm_charm.h> |
| 39 | #include "msm_watchdog.h" |
| 40 | #include "devices.h" |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 41 | #include "mdm_private.h" |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 42 | #define MDM_PBLRDY_CNT 20 |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 43 | |
Joel King | 96c96dc | 2012-07-30 09:06:15 -0700 | [diff] [blame] | 44 | static int mdm_debug_mask; |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 45 | static int power_on_count; |
Hemant Kumar | f1ca919 | 2012-02-07 18:59:33 -0800 | [diff] [blame] | 46 | static int hsic_peripheral_status; |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 47 | static DEFINE_MUTEX(hsic_status_lock); |
| 48 | |
| 49 | static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv) |
| 50 | { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 51 | if (!mdm_drv->pdata->peripheral_platform_device) |
| 52 | return; |
| 53 | |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 54 | mutex_lock(&hsic_status_lock); |
| 55 | if (hsic_peripheral_status) |
| 56 | goto out; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 57 | platform_device_add(mdm_drv->pdata->peripheral_platform_device); |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 58 | hsic_peripheral_status = 1; |
| 59 | out: |
| 60 | mutex_unlock(&hsic_status_lock); |
| 61 | } |
| 62 | |
| 63 | static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv) |
| 64 | { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 65 | if (!mdm_drv->pdata->peripheral_platform_device) |
| 66 | return; |
| 67 | |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 68 | mutex_lock(&hsic_status_lock); |
| 69 | if (!hsic_peripheral_status) |
| 70 | goto out; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 71 | platform_device_del(mdm_drv->pdata->peripheral_platform_device); |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 72 | hsic_peripheral_status = 0; |
| 73 | out: |
| 74 | mutex_unlock(&hsic_status_lock); |
| 75 | } |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 76 | |
Joel King | a7a7b9a | 2012-06-28 13:35:28 -0700 | [diff] [blame] | 77 | /* This function can be called from atomic context. */ |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame] | 78 | static void mdm_toggle_soft_reset(struct mdm_modem_drv *mdm_drv) |
| 79 | { |
| 80 | int soft_reset_direction_assert = 0, |
| 81 | soft_reset_direction_de_assert = 1; |
| 82 | |
| 83 | if (mdm_drv->pdata->soft_reset_inverted) { |
| 84 | soft_reset_direction_assert = 1; |
| 85 | soft_reset_direction_de_assert = 0; |
| 86 | } |
| 87 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 88 | soft_reset_direction_assert); |
Joel King | a7a7b9a | 2012-06-28 13:35:28 -0700 | [diff] [blame] | 89 | /* Use mdelay because this function can be called from atomic |
| 90 | * context. |
| 91 | */ |
| 92 | mdelay(10); |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame] | 93 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 94 | soft_reset_direction_de_assert); |
| 95 | } |
| 96 | |
Joel King | a7a7b9a | 2012-06-28 13:35:28 -0700 | [diff] [blame] | 97 | /* This function can be called from atomic context. */ |
| 98 | static void mdm_atomic_soft_reset(struct mdm_modem_drv *mdm_drv) |
| 99 | { |
| 100 | mdm_toggle_soft_reset(mdm_drv); |
| 101 | } |
| 102 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 103 | static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv) |
| 104 | { |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame] | 105 | int i; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 106 | int soft_reset_direction = |
| 107 | mdm_drv->pdata->soft_reset_inverted ? 1 : 0; |
| 108 | |
Hemant Kumar | 069f643 | 2012-08-22 18:30:19 -0700 | [diff] [blame] | 109 | mdm_peripheral_disconnect(mdm_drv); |
| 110 | |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame] | 111 | /* Wait for the modem to complete its power down actions. */ |
| 112 | for (i = 20; i > 0; i--) { |
Joel King | bf3e4b5 | 2012-09-26 09:10:34 -0700 | [diff] [blame] | 113 | if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) { |
| 114 | if (mdm_debug_mask & MDM_DEBUG_MASK_SHDN_LOG) |
| 115 | pr_info("%s: mdm2ap_status went low, i = %d\n", |
| 116 | __func__, i); |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame] | 117 | break; |
Joel King | bf3e4b5 | 2012-09-26 09:10:34 -0700 | [diff] [blame] | 118 | } |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame] | 119 | msleep(100); |
| 120 | } |
| 121 | if (i == 0) { |
| 122 | pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n", |
| 123 | __func__); |
| 124 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 125 | soft_reset_direction); |
| 126 | /* |
| 127 | * Currently, there is a debounce timer on the charm PMIC. It is |
| 128 | * necessary to hold the PMIC RESET low for ~3.5 seconds |
| 129 | * for the reset to fully take place. Sleep here to ensure the |
| 130 | * reset has occured before the function exits. |
| 131 | */ |
| 132 | msleep(4000); |
| 133 | } |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv) |
| 137 | { |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 138 | int i; |
| 139 | int pblrdy; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 140 | if (power_on_count != 1) { |
| 141 | pr_err("%s: Calling fn when power_on_count != 1\n", |
| 142 | __func__); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | pr_err("%s: Powering on modem for the first time\n", __func__); |
| 147 | mdm_peripheral_disconnect(mdm_drv); |
| 148 | |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame] | 149 | /* If this is the first power-up after a panic, the modem may still |
| 150 | * be in a power-on state, in which case we need to toggle the gpio |
| 151 | * instead of just de-asserting it. No harm done if the modem was |
| 152 | * powered down. |
| 153 | */ |
| 154 | mdm_toggle_soft_reset(mdm_drv); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 155 | /* If the device has a kpd pwr gpio then toggle it. */ |
Ameya Thakur | e155ece | 2012-07-09 12:08:37 -0700 | [diff] [blame] | 156 | if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio)) { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 157 | /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle, |
| 158 | * then pull it back low. |
| 159 | */ |
| 160 | pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__); |
| 161 | gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1); |
| 162 | msleep(1000); |
| 163 | gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0); |
| 164 | } |
| 165 | |
Ameya Thakur | e155ece | 2012-07-09 12:08:37 -0700 | [diff] [blame] | 166 | if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy)) |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 167 | goto start_mdm_peripheral; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 168 | |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 169 | for (i = 0; i < MDM_PBLRDY_CNT; i++) { |
| 170 | pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy); |
| 171 | if (pblrdy) |
| 172 | break; |
| 173 | usleep_range(5000, 5000); |
| 174 | } |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 175 | pr_debug("%s: i:%d\n", __func__, i); |
| 176 | |
| 177 | start_mdm_peripheral: |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 178 | mdm_peripheral_connect(mdm_drv); |
| 179 | msleep(200); |
| 180 | } |
| 181 | |
| 182 | static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv) |
| 183 | { |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 184 | int i; |
| 185 | int pblrdy; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 186 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 187 | pr_err("%s: soft resetting mdm modem\n", __func__); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 188 | mdm_peripheral_disconnect(mdm_drv); |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame] | 189 | mdm_toggle_soft_reset(mdm_drv); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 190 | |
Ameya Thakur | e155ece | 2012-07-09 12:08:37 -0700 | [diff] [blame] | 191 | if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy)) |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 192 | goto start_mdm_peripheral; |
| 193 | |
| 194 | for (i = 0; i < MDM_PBLRDY_CNT; i++) { |
| 195 | pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy); |
| 196 | if (pblrdy) |
| 197 | break; |
| 198 | usleep_range(5000, 5000); |
| 199 | } |
| 200 | |
| 201 | pr_debug("%s: i:%d\n", __func__, i); |
| 202 | |
| 203 | start_mdm_peripheral: |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 204 | mdm_peripheral_connect(mdm_drv); |
| 205 | msleep(200); |
| 206 | } |
| 207 | |
| 208 | static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv) |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 209 | { |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 210 | power_on_count++; |
| 211 | |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 212 | /* this gpio will be used to indicate apq readiness, |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 213 | * de-assert it now so that it can be asserted later. |
| 214 | * May not be used. |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 215 | */ |
Ameya Thakur | e155ece | 2012-07-09 12:08:37 -0700 | [diff] [blame] | 216 | if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio)) |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 217 | gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0); |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 218 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 219 | /* |
| 220 | * If we did an "early power on" then ignore the very next |
| 221 | * power-on request because it would the be first request from |
| 222 | * user space but we're already powered on. Ignore it. |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 223 | */ |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 224 | if (mdm_drv->pdata->early_power_on && |
| 225 | (power_on_count == 2)) |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 226 | return; |
| 227 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 228 | if (power_on_count == 1) |
| 229 | mdm_do_first_power_on(mdm_drv); |
| 230 | else |
| 231 | mdm_do_soft_power_on(mdm_drv); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 234 | static void debug_state_changed(int value) |
| 235 | { |
Joel King | 96c96dc | 2012-07-30 09:06:15 -0700 | [diff] [blame] | 236 | mdm_debug_mask = value; |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 239 | static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value) |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 240 | { |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 241 | pr_debug("%s: value:%d\n", __func__, value); |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 242 | |
| 243 | if (value) { |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 244 | mdm_peripheral_disconnect(mdm_drv); |
| 245 | mdm_peripheral_connect(mdm_drv); |
Ameya Thakur | e155ece | 2012-07-09 12:08:37 -0700 | [diff] [blame] | 246 | if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio)) |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 247 | gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1); |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Ameya Thakur | 43248fd | 2012-07-10 18:50:52 -0700 | [diff] [blame] | 251 | static void mdm_image_upgrade(struct mdm_modem_drv *mdm_drv, int type) |
| 252 | { |
| 253 | switch (type) { |
| 254 | case APQ_CONTROLLED_UPGRADE: |
| 255 | pr_debug("%s APQ controlled modem image upgrade\n", __func__); |
| 256 | mdm_drv->mdm_ready = 0; |
| 257 | mdm_toggle_soft_reset(mdm_drv); |
| 258 | break; |
| 259 | case MDM_CONTROLLED_UPGRADE: |
| 260 | pr_debug("%s MDM controlled modem image upgrade\n", __func__); |
| 261 | mdm_drv->mdm_ready = 0; |
| 262 | /* |
| 263 | * If we have no image currently present on the modem, then we |
| 264 | * would be in PBL, in which case the status gpio would not go |
| 265 | * high. |
| 266 | */ |
| 267 | mdm_drv->disable_status_check = 1; |
Ameya Thakur | e155ece | 2012-07-09 12:08:37 -0700 | [diff] [blame] | 268 | if (GPIO_IS_VALID(mdm_drv->usb_switch_gpio)) { |
Ameya Thakur | 43248fd | 2012-07-10 18:50:52 -0700 | [diff] [blame] | 269 | pr_info("%s Switching usb control to MDM\n", __func__); |
| 270 | gpio_direction_output(mdm_drv->usb_switch_gpio, 1); |
| 271 | } else |
| 272 | pr_err("%s usb switch gpio unavailable\n", __func__); |
| 273 | break; |
| 274 | default: |
| 275 | pr_err("%s invalid upgrade type\n", __func__); |
| 276 | } |
| 277 | } |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 278 | static struct mdm_ops mdm_cb = { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 279 | .power_on_mdm_cb = mdm_power_on_common, |
| 280 | .reset_mdm_cb = mdm_power_on_common, |
Joel King | a7a7b9a | 2012-06-28 13:35:28 -0700 | [diff] [blame] | 281 | .atomic_reset_mdm_cb = mdm_atomic_soft_reset, |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 282 | .power_down_mdm_cb = mdm_power_down_common, |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 283 | .debug_state_changed_cb = debug_state_changed, |
| 284 | .status_cb = mdm_status_changed, |
Ameya Thakur | 43248fd | 2012-07-10 18:50:52 -0700 | [diff] [blame] | 285 | .image_upgrade_cb = mdm_image_upgrade, |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 286 | }; |
| 287 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 288 | static int __init mdm_modem_probe(struct platform_device *pdev) |
| 289 | { |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 290 | return mdm_common_create(pdev, &mdm_cb); |
| 291 | } |
| 292 | |
| 293 | static int __devexit mdm_modem_remove(struct platform_device *pdev) |
| 294 | { |
| 295 | return mdm_common_modem_remove(pdev); |
| 296 | } |
| 297 | |
| 298 | static void mdm_modem_shutdown(struct platform_device *pdev) |
| 299 | { |
| 300 | mdm_common_modem_shutdown(pdev); |
| 301 | } |
| 302 | |
| 303 | static struct platform_driver mdm_modem_driver = { |
| 304 | .remove = mdm_modem_remove, |
| 305 | .shutdown = mdm_modem_shutdown, |
| 306 | .driver = { |
| 307 | .name = "mdm2_modem", |
| 308 | .owner = THIS_MODULE |
| 309 | }, |
| 310 | }; |
| 311 | |
| 312 | static int __init mdm_modem_init(void) |
| 313 | { |
| 314 | return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe); |
| 315 | } |
| 316 | |
| 317 | static void __exit mdm_modem_exit(void) |
| 318 | { |
| 319 | platform_driver_unregister(&mdm_modem_driver); |
| 320 | } |
| 321 | |
| 322 | module_init(mdm_modem_init); |
| 323 | module_exit(mdm_modem_exit); |
| 324 | |
| 325 | MODULE_LICENSE("GPL v2"); |
| 326 | MODULE_DESCRIPTION("mdm modem driver"); |
| 327 | MODULE_VERSION("2.0"); |
| 328 | MODULE_ALIAS("mdm_modem"); |