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 | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame^] | 80 | static void mdm_toggle_soft_reset(struct mdm_modem_drv *mdm_drv) |
| 81 | { |
| 82 | int soft_reset_direction_assert = 0, |
| 83 | soft_reset_direction_de_assert = 1; |
| 84 | |
| 85 | if (mdm_drv->pdata->soft_reset_inverted) { |
| 86 | soft_reset_direction_assert = 1; |
| 87 | soft_reset_direction_de_assert = 0; |
| 88 | } |
| 89 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 90 | soft_reset_direction_assert); |
| 91 | usleep_range(5000, 10000); |
| 92 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 93 | soft_reset_direction_de_assert); |
| 94 | } |
| 95 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 96 | static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv) |
| 97 | { |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame^] | 98 | int i; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 99 | int soft_reset_direction = |
| 100 | mdm_drv->pdata->soft_reset_inverted ? 1 : 0; |
| 101 | |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame^] | 102 | /* Wait for the modem to complete its power down actions. */ |
| 103 | for (i = 20; i > 0; i--) { |
| 104 | if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) |
| 105 | break; |
| 106 | msleep(100); |
| 107 | } |
| 108 | if (i == 0) { |
| 109 | pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n", |
| 110 | __func__); |
| 111 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 112 | soft_reset_direction); |
| 113 | /* |
| 114 | * Currently, there is a debounce timer on the charm PMIC. It is |
| 115 | * necessary to hold the PMIC RESET low for ~3.5 seconds |
| 116 | * for the reset to fully take place. Sleep here to ensure the |
| 117 | * reset has occured before the function exits. |
| 118 | */ |
| 119 | msleep(4000); |
| 120 | } |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 121 | mdm_peripheral_disconnect(mdm_drv); |
| 122 | } |
| 123 | |
| 124 | static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv) |
| 125 | { |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 126 | int i; |
| 127 | int pblrdy; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 128 | |
| 129 | if (power_on_count != 1) { |
| 130 | pr_err("%s: Calling fn when power_on_count != 1\n", |
| 131 | __func__); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | pr_err("%s: Powering on modem for the first time\n", __func__); |
| 136 | mdm_peripheral_disconnect(mdm_drv); |
| 137 | |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame^] | 138 | /* If this is the first power-up after a panic, the modem may still |
| 139 | * be in a power-on state, in which case we need to toggle the gpio |
| 140 | * instead of just de-asserting it. No harm done if the modem was |
| 141 | * powered down. |
| 142 | */ |
| 143 | mdm_toggle_soft_reset(mdm_drv); |
| 144 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 145 | /* If the device has a kpd pwr gpio then toggle it. */ |
| 146 | if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) { |
| 147 | /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle, |
| 148 | * then pull it back low. |
| 149 | */ |
| 150 | pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__); |
| 151 | gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1); |
| 152 | msleep(1000); |
| 153 | gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0); |
| 154 | } |
| 155 | |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 156 | if (!mdm_drv->mdm2ap_pblrdy) |
| 157 | goto start_mdm_peripheral; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 158 | |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 159 | for (i = 0; i < MDM_PBLRDY_CNT; i++) { |
| 160 | pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy); |
| 161 | if (pblrdy) |
| 162 | break; |
| 163 | usleep_range(5000, 5000); |
| 164 | } |
| 165 | |
| 166 | pr_debug("%s: i:%d\n", __func__, i); |
| 167 | |
| 168 | start_mdm_peripheral: |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 169 | mdm_peripheral_connect(mdm_drv); |
| 170 | msleep(200); |
| 171 | } |
| 172 | |
| 173 | static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv) |
| 174 | { |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 175 | int i; |
| 176 | int pblrdy; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 177 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 178 | pr_err("%s: soft resetting mdm modem\n", __func__); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 179 | mdm_peripheral_disconnect(mdm_drv); |
Joel King | 733377c | 2012-06-20 13:07:38 -0700 | [diff] [blame^] | 180 | mdm_toggle_soft_reset(mdm_drv); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 181 | |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame] | 182 | if (!mdm_drv->mdm2ap_pblrdy) |
| 183 | goto start_mdm_peripheral; |
| 184 | |
| 185 | for (i = 0; i < MDM_PBLRDY_CNT; i++) { |
| 186 | pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy); |
| 187 | if (pblrdy) |
| 188 | break; |
| 189 | usleep_range(5000, 5000); |
| 190 | } |
| 191 | |
| 192 | pr_debug("%s: i:%d\n", __func__, i); |
| 193 | |
| 194 | start_mdm_peripheral: |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 195 | mdm_peripheral_connect(mdm_drv); |
| 196 | msleep(200); |
| 197 | } |
| 198 | |
| 199 | static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv) |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 200 | { |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 201 | power_on_count++; |
| 202 | |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 203 | /* this gpio will be used to indicate apq readiness, |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 204 | * de-assert it now so that it can be asserted later. |
| 205 | * May not be used. |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 206 | */ |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 207 | if (mdm_drv->ap2mdm_wakeup_gpio > 0) |
| 208 | gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0); |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 209 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 210 | /* |
| 211 | * If we did an "early power on" then ignore the very next |
| 212 | * power-on request because it would the be first request from |
| 213 | * user space but we're already powered on. Ignore it. |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 214 | */ |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 215 | if (mdm_drv->pdata->early_power_on && |
| 216 | (power_on_count == 2)) |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 217 | return; |
| 218 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 219 | if (power_on_count == 1) |
| 220 | mdm_do_first_power_on(mdm_drv); |
| 221 | else |
| 222 | mdm_do_soft_power_on(mdm_drv); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 225 | static void debug_state_changed(int value) |
| 226 | { |
| 227 | mdm_debug_on = value; |
| 228 | } |
| 229 | |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 230 | 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] | 231 | { |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 232 | pr_debug("%s: value:%d\n", __func__, value); |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 233 | |
| 234 | if (value) { |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 235 | mdm_peripheral_disconnect(mdm_drv); |
| 236 | mdm_peripheral_connect(mdm_drv); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 237 | if (mdm_drv->ap2mdm_wakeup_gpio > 0) |
| 238 | gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1); |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 242 | static struct mdm_ops mdm_cb = { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 243 | .power_on_mdm_cb = mdm_power_on_common, |
| 244 | .reset_mdm_cb = mdm_power_on_common, |
| 245 | .power_down_mdm_cb = mdm_power_down_common, |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 246 | .debug_state_changed_cb = debug_state_changed, |
| 247 | .status_cb = mdm_status_changed, |
| 248 | }; |
| 249 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 250 | static int __init mdm_modem_probe(struct platform_device *pdev) |
| 251 | { |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 252 | return mdm_common_create(pdev, &mdm_cb); |
| 253 | } |
| 254 | |
| 255 | static int __devexit mdm_modem_remove(struct platform_device *pdev) |
| 256 | { |
| 257 | return mdm_common_modem_remove(pdev); |
| 258 | } |
| 259 | |
| 260 | static void mdm_modem_shutdown(struct platform_device *pdev) |
| 261 | { |
| 262 | mdm_common_modem_shutdown(pdev); |
| 263 | } |
| 264 | |
| 265 | static struct platform_driver mdm_modem_driver = { |
| 266 | .remove = mdm_modem_remove, |
| 267 | .shutdown = mdm_modem_shutdown, |
| 268 | .driver = { |
| 269 | .name = "mdm2_modem", |
| 270 | .owner = THIS_MODULE |
| 271 | }, |
| 272 | }; |
| 273 | |
| 274 | static int __init mdm_modem_init(void) |
| 275 | { |
| 276 | return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe); |
| 277 | } |
| 278 | |
| 279 | static void __exit mdm_modem_exit(void) |
| 280 | { |
| 281 | platform_driver_unregister(&mdm_modem_driver); |
| 282 | } |
| 283 | |
| 284 | module_init(mdm_modem_init); |
| 285 | module_exit(mdm_modem_exit); |
| 286 | |
| 287 | MODULE_LICENSE("GPL v2"); |
| 288 | MODULE_DESCRIPTION("mdm modem driver"); |
| 289 | MODULE_VERSION("2.0"); |
| 290 | MODULE_ALIAS("mdm_modem"); |