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