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> |
Joel King | ef39084 | 2012-05-23 16:42:48 -0700 | [diff] [blame^] | 39 | #include <mach/rpm.h> |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 40 | #include <linux/msm_charm.h> |
| 41 | #include "msm_watchdog.h" |
| 42 | #include "mdm_private.h" |
Joel King | 30fdd66 | 2012-05-07 19:39:29 -0700 | [diff] [blame] | 43 | #include "sysmon.h" |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 44 | |
| 45 | #define MDM_MODEM_TIMEOUT 6000 |
| 46 | #define MDM_MODEM_DELTA 100 |
Joel King | 52d7fa6 | 2012-03-21 08:40:52 -0700 | [diff] [blame] | 47 | #define MDM_BOOT_TIMEOUT 60000L |
| 48 | #define MDM_RDUMP_TIMEOUT 60000L |
Joel King | 493b3ac | 2012-05-14 11:39:45 -0700 | [diff] [blame] | 49 | #define MDM2AP_STATUS_TIMEOUT_MS 60000L |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 50 | |
| 51 | static int mdm_debug_on; |
| 52 | static struct workqueue_struct *mdm_queue; |
Joel King | 30fdd66 | 2012-05-07 19:39:29 -0700 | [diff] [blame] | 53 | static struct workqueue_struct *mdm_sfr_queue; |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 54 | |
| 55 | #define EXTERNAL_MODEM "external_modem" |
| 56 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 57 | static struct mdm_modem_drv *mdm_drv; |
| 58 | |
| 59 | DECLARE_COMPLETION(mdm_needs_reload); |
| 60 | DECLARE_COMPLETION(mdm_boot); |
| 61 | DECLARE_COMPLETION(mdm_ram_dumps); |
| 62 | |
| 63 | static int first_boot = 1; |
| 64 | |
Joel King | 30fdd66 | 2012-05-07 19:39:29 -0700 | [diff] [blame] | 65 | #define RD_BUF_SIZE 100 |
| 66 | #define SFR_MAX_RETRIES 10 |
| 67 | #define SFR_RETRY_INTERVAL 1000 |
| 68 | |
Joel King | ef39084 | 2012-05-23 16:42:48 -0700 | [diff] [blame^] | 69 | static irqreturn_t mdm_vddmin_change(int irq, void *dev_id) |
| 70 | { |
| 71 | int value = gpio_get_value( |
| 72 | mdm_drv->pdata->vddmin_resource->mdm2ap_vddmin_gpio); |
| 73 | |
| 74 | if (value == 0) |
| 75 | pr_info("External Modem entered Vddmin\n"); |
| 76 | else |
| 77 | pr_info("External Modem exited Vddmin\n"); |
| 78 | |
| 79 | return IRQ_HANDLED; |
| 80 | } |
| 81 | |
| 82 | static void mdm_setup_vddmin_gpios(void) |
| 83 | { |
| 84 | struct msm_rpm_iv_pair req; |
| 85 | struct mdm_vddmin_resource *vddmin_res; |
| 86 | int irq, ret; |
| 87 | |
| 88 | /* This resource may not be supported by some platforms. */ |
| 89 | vddmin_res = mdm_drv->pdata->vddmin_resource; |
| 90 | if (!vddmin_res) |
| 91 | return; |
| 92 | |
| 93 | req.id = vddmin_res->rpm_id; |
| 94 | req.value = ((uint32_t)vddmin_res->ap2mdm_vddmin_gpio & 0x0000FFFF) |
| 95 | << 16; |
| 96 | req.value |= ((uint32_t)vddmin_res->modes & 0x000000FF) << 8; |
| 97 | req.value |= (uint32_t)vddmin_res->drive_strength & 0x000000FF; |
| 98 | |
| 99 | msm_rpm_set(MSM_RPM_CTX_SET_0, &req, 1); |
| 100 | |
| 101 | /* Monitor low power gpio from mdm */ |
| 102 | irq = MSM_GPIO_TO_INT(vddmin_res->mdm2ap_vddmin_gpio); |
| 103 | if (irq < 0) { |
| 104 | pr_err("%s: could not get LPM POWER IRQ resource.\n", |
| 105 | __func__); |
| 106 | goto error_end; |
| 107 | } |
| 108 | |
| 109 | ret = request_threaded_irq(irq, NULL, mdm_vddmin_change, |
| 110 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 111 | "mdm lpm", NULL); |
| 112 | |
| 113 | if (ret < 0) |
| 114 | pr_err("%s: MDM LPM IRQ#%d request failed with error=%d", |
| 115 | __func__, irq, ret); |
| 116 | error_end: |
| 117 | return; |
| 118 | } |
| 119 | |
Joel King | 30fdd66 | 2012-05-07 19:39:29 -0700 | [diff] [blame] | 120 | static void mdm_restart_reason_fn(struct work_struct *work) |
| 121 | { |
| 122 | int ret, ntries = 0; |
| 123 | char sfr_buf[RD_BUF_SIZE]; |
| 124 | |
| 125 | do { |
| 126 | msleep(SFR_RETRY_INTERVAL); |
| 127 | ret = sysmon_get_reason(SYSMON_SS_EXT_MODEM, |
| 128 | sfr_buf, sizeof(sfr_buf)); |
| 129 | if (ret) { |
| 130 | /* |
| 131 | * The sysmon device may not have been probed as yet |
| 132 | * after the restart. |
| 133 | */ |
| 134 | pr_err("%s: Error retrieving mdm restart reason, ret = %d, " |
| 135 | "%d/%d tries\n", __func__, ret, |
| 136 | ntries + 1, SFR_MAX_RETRIES); |
| 137 | } else { |
| 138 | pr_err("mdm restart reason: %s\n", sfr_buf); |
| 139 | break; |
| 140 | } |
| 141 | } while (++ntries < SFR_MAX_RETRIES); |
| 142 | } |
| 143 | |
| 144 | static DECLARE_WORK(sfr_reason_work, mdm_restart_reason_fn); |
| 145 | |
Joel King | 493b3ac | 2012-05-14 11:39:45 -0700 | [diff] [blame] | 146 | static void mdm2ap_status_check(struct work_struct *work) |
| 147 | { |
| 148 | /* |
| 149 | * If the mdm modem did not pull the MDM2AP_STATUS gpio |
| 150 | * high then call subsystem_restart. |
| 151 | */ |
| 152 | if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) { |
| 153 | pr_err("%s: MDM2AP_STATUS gpio did not go high\n", |
| 154 | __func__); |
| 155 | mdm_drv->mdm_ready = 0; |
| 156 | subsystem_restart(EXTERNAL_MODEM); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static DECLARE_DELAYED_WORK(mdm2ap_status_check_work, mdm2ap_status_check); |
| 161 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 162 | long mdm_modem_ioctl(struct file *filp, unsigned int cmd, |
| 163 | unsigned long arg) |
| 164 | { |
| 165 | int status, ret = 0; |
| 166 | |
| 167 | if (_IOC_TYPE(cmd) != CHARM_CODE) { |
| 168 | pr_err("%s: invalid ioctl code\n", __func__); |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 172 | pr_debug("%s: Entering ioctl cmd = %d\n", __func__, _IOC_NR(cmd)); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 173 | switch (cmd) { |
| 174 | case WAKE_CHARM: |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 175 | pr_info("%s: Powering on mdm\n", __func__); |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 176 | mdm_drv->ops->power_on_mdm_cb(mdm_drv); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 177 | break; |
| 178 | case CHECK_FOR_BOOT: |
| 179 | if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) |
| 180 | put_user(1, (unsigned long __user *) arg); |
| 181 | else |
| 182 | put_user(0, (unsigned long __user *) arg); |
| 183 | break; |
| 184 | case NORMAL_BOOT_DONE: |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 185 | pr_debug("%s: check if mdm is booted up\n", __func__); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 186 | get_user(status, (unsigned long __user *) arg); |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 187 | if (status) { |
| 188 | pr_debug("%s: normal boot failed\n", __func__); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 189 | mdm_drv->mdm_boot_status = -EIO; |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 190 | } else { |
| 191 | pr_info("%s: normal boot done\n", __func__); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 192 | mdm_drv->mdm_boot_status = 0; |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 193 | } |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 194 | mdm_drv->mdm_ready = 1; |
| 195 | |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 196 | if (mdm_drv->ops->normal_boot_done_cb != NULL) |
| 197 | mdm_drv->ops->normal_boot_done_cb(mdm_drv); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 198 | |
| 199 | if (!first_boot) |
| 200 | complete(&mdm_boot); |
| 201 | else |
| 202 | first_boot = 0; |
Joel King | 493b3ac | 2012-05-14 11:39:45 -0700 | [diff] [blame] | 203 | |
| 204 | /* Start a timer to check that the mdm2ap_status gpio |
| 205 | * goes high. |
| 206 | */ |
| 207 | |
| 208 | if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) |
| 209 | schedule_delayed_work(&mdm2ap_status_check_work, |
| 210 | msecs_to_jiffies(MDM2AP_STATUS_TIMEOUT_MS)); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 211 | break; |
| 212 | case RAM_DUMP_DONE: |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 213 | pr_debug("%s: mdm done collecting RAM dumps\n", __func__); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 214 | get_user(status, (unsigned long __user *) arg); |
| 215 | if (status) |
| 216 | mdm_drv->mdm_ram_dump_status = -EIO; |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 217 | else { |
| 218 | pr_info("%s: ramdump collection completed\n", __func__); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 219 | mdm_drv->mdm_ram_dump_status = 0; |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 220 | } |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 221 | complete(&mdm_ram_dumps); |
| 222 | break; |
| 223 | case WAIT_FOR_RESTART: |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 224 | pr_debug("%s: wait for mdm to need images reloaded\n", |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 225 | __func__); |
| 226 | ret = wait_for_completion_interruptible(&mdm_needs_reload); |
| 227 | if (!ret) |
| 228 | put_user(mdm_drv->boot_type, |
| 229 | (unsigned long __user *) arg); |
| 230 | INIT_COMPLETION(mdm_needs_reload); |
| 231 | break; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 232 | case GET_DLOAD_STATUS: |
| 233 | pr_debug("getting status of mdm2ap_errfatal_gpio\n"); |
| 234 | if (gpio_get_value(mdm_drv->mdm2ap_errfatal_gpio) == 1 && |
| 235 | !mdm_drv->mdm_ready) |
| 236 | put_user(1, (unsigned long __user *) arg); |
| 237 | else |
| 238 | put_user(0, (unsigned long __user *) arg); |
| 239 | break; |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 240 | default: |
| 241 | pr_err("%s: invalid ioctl cmd = %d\n", __func__, _IOC_NR(cmd)); |
| 242 | ret = -EINVAL; |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 249 | static void mdm_status_fn(struct work_struct *work) |
| 250 | { |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 251 | int value = gpio_get_value(mdm_drv->mdm2ap_status_gpio); |
| 252 | |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 253 | pr_debug("%s: status:%d\n", __func__, value); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 254 | if (mdm_drv->mdm_ready && mdm_drv->ops->status_cb) |
| 255 | mdm_drv->ops->status_cb(mdm_drv, value); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static DECLARE_WORK(mdm_status_work, mdm_status_fn); |
| 259 | |
| 260 | static void mdm_disable_irqs(void) |
| 261 | { |
| 262 | disable_irq_nosync(mdm_drv->mdm_errfatal_irq); |
| 263 | disable_irq_nosync(mdm_drv->mdm_status_irq); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static irqreturn_t mdm_errfatal(int irq, void *dev_id) |
| 267 | { |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 268 | pr_debug("%s: mdm got errfatal interrupt\n", __func__); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 269 | if (mdm_drv->mdm_ready && |
| 270 | (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 1)) { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 271 | pr_info("%s: Reseting the mdm due to an errfatal\n", __func__); |
| 272 | mdm_drv->mdm_ready = 0; |
| 273 | subsystem_restart(EXTERNAL_MODEM); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 274 | } |
| 275 | return IRQ_HANDLED; |
| 276 | } |
| 277 | |
| 278 | static int mdm_modem_open(struct inode *inode, struct file *file) |
| 279 | { |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static const struct file_operations mdm_modem_fops = { |
| 284 | .owner = THIS_MODULE, |
| 285 | .open = mdm_modem_open, |
| 286 | .unlocked_ioctl = mdm_modem_ioctl, |
| 287 | }; |
| 288 | |
| 289 | |
| 290 | static struct miscdevice mdm_modem_misc = { |
| 291 | .minor = MISC_DYNAMIC_MINOR, |
| 292 | .name = "mdm", |
| 293 | .fops = &mdm_modem_fops |
| 294 | }; |
| 295 | |
| 296 | static int mdm_panic_prep(struct notifier_block *this, |
| 297 | unsigned long event, void *ptr) |
| 298 | { |
| 299 | int i; |
| 300 | |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 301 | pr_debug("%s: setting AP2MDM_ERRFATAL high for a non graceful reset\n", |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 302 | __func__); |
| 303 | mdm_disable_irqs(); |
| 304 | gpio_set_value(mdm_drv->ap2mdm_errfatal_gpio, 1); |
| 305 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 306 | for (i = MDM_MODEM_TIMEOUT; i > 0; i -= MDM_MODEM_DELTA) { |
| 307 | pet_watchdog(); |
| 308 | mdelay(MDM_MODEM_DELTA); |
| 309 | if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) |
| 310 | break; |
| 311 | } |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 312 | if (i <= 0) { |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 313 | pr_err("%s: MDM2AP_STATUS never went low\n", __func__); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 314 | /* Reset the modem so that it will go into download mode. */ |
| 315 | if (mdm_drv && mdm_drv->ops->reset_mdm_cb) |
| 316 | mdm_drv->ops->reset_mdm_cb(mdm_drv); |
| 317 | } |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 318 | return NOTIFY_DONE; |
| 319 | } |
| 320 | |
| 321 | static struct notifier_block mdm_panic_blk = { |
| 322 | .notifier_call = mdm_panic_prep, |
| 323 | }; |
| 324 | |
| 325 | static irqreturn_t mdm_status_change(int irq, void *dev_id) |
| 326 | { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 327 | int value = gpio_get_value(mdm_drv->mdm2ap_status_gpio); |
| 328 | |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 329 | pr_debug("%s: mdm sent status change interrupt\n", __func__); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 330 | if (value == 0 && mdm_drv->mdm_ready == 1) { |
| 331 | pr_info("%s: unexpected reset external modem\n", __func__); |
| 332 | mdm_drv->mdm_unexpected_reset_occurred = 1; |
| 333 | mdm_drv->mdm_ready = 0; |
| 334 | subsystem_restart(EXTERNAL_MODEM); |
| 335 | } else if (value == 1) { |
Joel King | 493b3ac | 2012-05-14 11:39:45 -0700 | [diff] [blame] | 336 | cancel_delayed_work(&mdm2ap_status_check_work); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 337 | pr_info("%s: status = 1: mdm is now ready\n", __func__); |
| 338 | queue_work(mdm_queue, &mdm_status_work); |
| 339 | } |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 340 | return IRQ_HANDLED; |
| 341 | } |
| 342 | |
| 343 | static int mdm_subsys_shutdown(const struct subsys_data *crashed_subsys) |
| 344 | { |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 345 | gpio_direction_output(mdm_drv->ap2mdm_errfatal_gpio, 1); |
Joel King | 415af51 | 2012-02-03 10:22:43 -0800 | [diff] [blame] | 346 | if (mdm_drv->pdata->ramdump_delay_ms > 0) { |
| 347 | /* Wait for the external modem to complete |
| 348 | * its preparation for ramdumps. |
| 349 | */ |
Joel King | 6e7a3e8 | 2012-04-13 17:37:33 -0700 | [diff] [blame] | 350 | msleep(mdm_drv->pdata->ramdump_delay_ms); |
Joel King | 415af51 | 2012-02-03 10:22:43 -0800 | [diff] [blame] | 351 | } |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 352 | if (!mdm_drv->mdm_unexpected_reset_occurred) |
| 353 | mdm_drv->ops->reset_mdm_cb(mdm_drv); |
| 354 | else |
| 355 | mdm_drv->mdm_unexpected_reset_occurred = 0; |
| 356 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int mdm_subsys_powerup(const struct subsys_data *crashed_subsys) |
| 361 | { |
Joel King | bc48e4c | 2012-02-27 13:18:52 -0800 | [diff] [blame] | 362 | gpio_direction_output(mdm_drv->ap2mdm_errfatal_gpio, 0); |
| 363 | gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1); |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 364 | mdm_drv->ops->power_on_mdm_cb(mdm_drv); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 365 | mdm_drv->boot_type = CHARM_NORMAL_BOOT; |
| 366 | complete(&mdm_needs_reload); |
Joel King | 52d7fa6 | 2012-03-21 08:40:52 -0700 | [diff] [blame] | 367 | if (!wait_for_completion_timeout(&mdm_boot, |
| 368 | msecs_to_jiffies(MDM_BOOT_TIMEOUT))) { |
| 369 | mdm_drv->mdm_boot_status = -ETIMEDOUT; |
| 370 | pr_info("%s: mdm modem restart timed out.\n", __func__); |
Joel King | 30fdd66 | 2012-05-07 19:39:29 -0700 | [diff] [blame] | 371 | } else { |
Joel King | 52d7fa6 | 2012-03-21 08:40:52 -0700 | [diff] [blame] | 372 | pr_info("%s: mdm modem has been restarted\n", __func__); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 373 | |
Joel King | 30fdd66 | 2012-05-07 19:39:29 -0700 | [diff] [blame] | 374 | /* Log the reason for the restart */ |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 375 | if (mdm_drv->pdata->sfr_query) |
| 376 | queue_work(mdm_sfr_queue, &sfr_reason_work); |
Joel King | 30fdd66 | 2012-05-07 19:39:29 -0700 | [diff] [blame] | 377 | } |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 378 | INIT_COMPLETION(mdm_boot); |
| 379 | return mdm_drv->mdm_boot_status; |
| 380 | } |
| 381 | |
| 382 | static int mdm_subsys_ramdumps(int want_dumps, |
| 383 | const struct subsys_data *crashed_subsys) |
| 384 | { |
| 385 | mdm_drv->mdm_ram_dump_status = 0; |
| 386 | if (want_dumps) { |
| 387 | mdm_drv->boot_type = CHARM_RAM_DUMPS; |
| 388 | complete(&mdm_needs_reload); |
Joel King | 52d7fa6 | 2012-03-21 08:40:52 -0700 | [diff] [blame] | 389 | if (!wait_for_completion_timeout(&mdm_ram_dumps, |
| 390 | msecs_to_jiffies(MDM_RDUMP_TIMEOUT))) { |
| 391 | mdm_drv->mdm_ram_dump_status = -ETIMEDOUT; |
| 392 | pr_info("%s: mdm modem ramdumps timed out.\n", |
| 393 | __func__); |
| 394 | } else |
| 395 | pr_info("%s: mdm modem ramdumps completed.\n", |
| 396 | __func__); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 397 | INIT_COMPLETION(mdm_ram_dumps); |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 398 | mdm_drv->ops->power_down_mdm_cb(mdm_drv); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 399 | } |
| 400 | return mdm_drv->mdm_ram_dump_status; |
| 401 | } |
| 402 | |
| 403 | static struct subsys_data mdm_subsystem = { |
| 404 | .shutdown = mdm_subsys_shutdown, |
| 405 | .ramdump = mdm_subsys_ramdumps, |
| 406 | .powerup = mdm_subsys_powerup, |
| 407 | .name = EXTERNAL_MODEM, |
| 408 | }; |
| 409 | |
| 410 | static int mdm_debug_on_set(void *data, u64 val) |
| 411 | { |
| 412 | mdm_debug_on = val; |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 413 | if (mdm_drv->ops->debug_state_changed_cb) |
| 414 | mdm_drv->ops->debug_state_changed_cb(mdm_debug_on); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int mdm_debug_on_get(void *data, u64 *val) |
| 419 | { |
| 420 | *val = mdm_debug_on; |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | DEFINE_SIMPLE_ATTRIBUTE(mdm_debug_on_fops, |
| 425 | mdm_debug_on_get, |
| 426 | mdm_debug_on_set, "%llu\n"); |
| 427 | |
| 428 | static int mdm_debugfs_init(void) |
| 429 | { |
| 430 | struct dentry *dent; |
| 431 | |
| 432 | dent = debugfs_create_dir("mdm_dbg", 0); |
| 433 | if (IS_ERR(dent)) |
| 434 | return PTR_ERR(dent); |
| 435 | |
| 436 | debugfs_create_file("debug_on", 0644, dent, NULL, |
| 437 | &mdm_debug_on_fops); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static void mdm_modem_initialize_data(struct platform_device *pdev, |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 442 | struct mdm_ops *mdm_ops) |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 443 | { |
| 444 | struct resource *pres; |
| 445 | |
| 446 | /* MDM2AP_ERRFATAL */ |
| 447 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 448 | "MDM2AP_ERRFATAL"); |
| 449 | if (pres) |
| 450 | mdm_drv->mdm2ap_errfatal_gpio = pres->start; |
| 451 | |
| 452 | /* AP2MDM_ERRFATAL */ |
| 453 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 454 | "AP2MDM_ERRFATAL"); |
| 455 | if (pres) |
| 456 | mdm_drv->ap2mdm_errfatal_gpio = pres->start; |
| 457 | |
| 458 | /* MDM2AP_STATUS */ |
| 459 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 460 | "MDM2AP_STATUS"); |
| 461 | if (pres) |
| 462 | mdm_drv->mdm2ap_status_gpio = pres->start; |
| 463 | |
| 464 | /* AP2MDM_STATUS */ |
| 465 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 466 | "AP2MDM_STATUS"); |
| 467 | if (pres) |
| 468 | mdm_drv->ap2mdm_status_gpio = pres->start; |
| 469 | |
| 470 | /* MDM2AP_WAKEUP */ |
| 471 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 472 | "MDM2AP_WAKEUP"); |
| 473 | if (pres) |
| 474 | mdm_drv->mdm2ap_wakeup_gpio = pres->start; |
| 475 | |
| 476 | /* AP2MDM_WAKEUP */ |
| 477 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 478 | "AP2MDM_WAKEUP"); |
| 479 | if (pres) |
| 480 | mdm_drv->ap2mdm_wakeup_gpio = pres->start; |
| 481 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 482 | /* AP2MDM_SOFT_RESET */ |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 483 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 484 | "AP2MDM_SOFT_RESET"); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 485 | if (pres) |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 486 | mdm_drv->ap2mdm_soft_reset_gpio = pres->start; |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 487 | |
| 488 | /* AP2MDM_KPDPWR_N */ |
| 489 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 490 | "AP2MDM_KPDPWR_N"); |
| 491 | if (pres) |
| 492 | mdm_drv->ap2mdm_kpdpwr_n_gpio = pres->start; |
| 493 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 494 | /* AP2MDM_PMIC_PWR_EN */ |
| 495 | pres = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 496 | "AP2MDM_PMIC_PWR_EN"); |
| 497 | if (pres) |
| 498 | mdm_drv->ap2mdm_pmic_pwr_en_gpio = pres->start; |
| 499 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 500 | mdm_drv->boot_type = CHARM_NORMAL_BOOT; |
| 501 | |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 502 | mdm_drv->ops = mdm_ops; |
Joel King | 415af51 | 2012-02-03 10:22:43 -0800 | [diff] [blame] | 503 | mdm_drv->pdata = pdev->dev.platform_data; |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | int mdm_common_create(struct platform_device *pdev, |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 507 | struct mdm_ops *p_mdm_cb) |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 508 | { |
| 509 | int ret = -1, irq; |
| 510 | |
| 511 | mdm_drv = kzalloc(sizeof(struct mdm_modem_drv), GFP_KERNEL); |
| 512 | if (mdm_drv == NULL) { |
| 513 | pr_err("%s: kzalloc fail.\n", __func__); |
| 514 | goto alloc_err; |
| 515 | } |
| 516 | |
| 517 | mdm_modem_initialize_data(pdev, p_mdm_cb); |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 518 | if (mdm_drv->ops->debug_state_changed_cb) |
| 519 | mdm_drv->ops->debug_state_changed_cb(mdm_debug_on); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 520 | |
| 521 | gpio_request(mdm_drv->ap2mdm_status_gpio, "AP2MDM_STATUS"); |
| 522 | gpio_request(mdm_drv->ap2mdm_errfatal_gpio, "AP2MDM_ERRFATAL"); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 523 | if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) |
| 524 | gpio_request(mdm_drv->ap2mdm_kpdpwr_n_gpio, "AP2MDM_KPDPWR_N"); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 525 | gpio_request(mdm_drv->mdm2ap_status_gpio, "MDM2AP_STATUS"); |
| 526 | gpio_request(mdm_drv->mdm2ap_errfatal_gpio, "MDM2AP_ERRFATAL"); |
| 527 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 528 | if (mdm_drv->ap2mdm_pmic_pwr_en_gpio > 0) |
| 529 | gpio_request(mdm_drv->ap2mdm_pmic_pwr_en_gpio, |
| 530 | "AP2MDM_PMIC_PWR_EN"); |
| 531 | if (mdm_drv->ap2mdm_soft_reset_gpio > 0) |
| 532 | gpio_request(mdm_drv->ap2mdm_soft_reset_gpio, |
| 533 | "AP2MDM_SOFT_RESET"); |
| 534 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 535 | if (mdm_drv->ap2mdm_wakeup_gpio > 0) |
| 536 | gpio_request(mdm_drv->ap2mdm_wakeup_gpio, "AP2MDM_WAKEUP"); |
| 537 | |
| 538 | gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1); |
| 539 | gpio_direction_output(mdm_drv->ap2mdm_errfatal_gpio, 0); |
| 540 | |
| 541 | if (mdm_drv->ap2mdm_wakeup_gpio > 0) |
| 542 | gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0); |
| 543 | |
| 544 | gpio_direction_input(mdm_drv->mdm2ap_status_gpio); |
| 545 | gpio_direction_input(mdm_drv->mdm2ap_errfatal_gpio); |
| 546 | |
| 547 | mdm_queue = create_singlethread_workqueue("mdm_queue"); |
| 548 | if (!mdm_queue) { |
| 549 | pr_err("%s: could not create workqueue. All mdm " |
| 550 | "functionality will be disabled\n", |
| 551 | __func__); |
| 552 | ret = -ENOMEM; |
| 553 | goto fatal_err; |
| 554 | } |
| 555 | |
Joel King | 30fdd66 | 2012-05-07 19:39:29 -0700 | [diff] [blame] | 556 | mdm_sfr_queue = alloc_workqueue("mdm_sfr_queue", 0, 0); |
| 557 | if (!mdm_sfr_queue) { |
| 558 | pr_err("%s: could not create workqueue mdm_sfr_queue." |
| 559 | " All mdm functionality will be disabled\n", |
| 560 | __func__); |
| 561 | ret = -ENOMEM; |
| 562 | destroy_workqueue(mdm_queue); |
| 563 | goto fatal_err; |
| 564 | } |
| 565 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 566 | atomic_notifier_chain_register(&panic_notifier_list, &mdm_panic_blk); |
| 567 | mdm_debugfs_init(); |
| 568 | |
| 569 | /* Register subsystem handlers */ |
| 570 | ssr_register_subsystem(&mdm_subsystem); |
| 571 | |
| 572 | /* ERR_FATAL irq. */ |
| 573 | irq = MSM_GPIO_TO_INT(mdm_drv->mdm2ap_errfatal_gpio); |
| 574 | if (irq < 0) { |
| 575 | pr_err("%s: could not get MDM2AP_ERRFATAL IRQ resource. " |
| 576 | "error=%d No IRQ will be generated on errfatal.", |
| 577 | __func__, irq); |
| 578 | goto errfatal_err; |
| 579 | } |
| 580 | ret = request_irq(irq, mdm_errfatal, |
| 581 | IRQF_TRIGGER_RISING , "mdm errfatal", NULL); |
| 582 | |
| 583 | if (ret < 0) { |
| 584 | pr_err("%s: MDM2AP_ERRFATAL IRQ#%d request failed with error=%d" |
| 585 | ". No IRQ will be generated on errfatal.", |
| 586 | __func__, irq, ret); |
| 587 | goto errfatal_err; |
| 588 | } |
| 589 | mdm_drv->mdm_errfatal_irq = irq; |
| 590 | |
| 591 | errfatal_err: |
| 592 | |
| 593 | /* status irq */ |
| 594 | irq = MSM_GPIO_TO_INT(mdm_drv->mdm2ap_status_gpio); |
| 595 | if (irq < 0) { |
| 596 | pr_err("%s: could not get MDM2AP_STATUS IRQ resource. " |
| 597 | "error=%d No IRQ will be generated on status change.", |
| 598 | __func__, irq); |
| 599 | goto status_err; |
| 600 | } |
| 601 | |
| 602 | ret = request_threaded_irq(irq, NULL, mdm_status_change, |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 603 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_SHARED, |
| 604 | "mdm status", mdm_drv); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 605 | |
| 606 | if (ret < 0) { |
| 607 | pr_err("%s: MDM2AP_STATUS IRQ#%d request failed with error=%d" |
| 608 | ". No IRQ will be generated on status change.", |
| 609 | __func__, irq, ret); |
| 610 | goto status_err; |
| 611 | } |
| 612 | mdm_drv->mdm_status_irq = irq; |
| 613 | |
| 614 | status_err: |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 615 | /* |
| 616 | * If AP2MDM_PMIC_PWR_EN gpio is used, pull it high. It remains |
| 617 | * high until the whole phone is shut down. |
| 618 | */ |
| 619 | if (mdm_drv->ap2mdm_pmic_pwr_en_gpio > 0) |
| 620 | gpio_direction_output(mdm_drv->ap2mdm_pmic_pwr_en_gpio, 1); |
Joel King | ef39084 | 2012-05-23 16:42:48 -0700 | [diff] [blame^] | 621 | /* Register VDDmin gpios with RPM */ |
| 622 | mdm_setup_vddmin_gpios(); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 623 | |
Joel King | 35f819e | 2012-02-05 12:05:57 -0800 | [diff] [blame] | 624 | /* Perform early powerup of the external modem in order to |
| 625 | * allow tabla devices to be found. |
| 626 | */ |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 627 | if (mdm_drv->pdata->early_power_on) |
| 628 | mdm_drv->ops->power_on_mdm_cb(mdm_drv); |
Joel King | 35f819e | 2012-02-05 12:05:57 -0800 | [diff] [blame] | 629 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 630 | pr_info("%s: Registering mdm modem\n", __func__); |
| 631 | return misc_register(&mdm_modem_misc); |
| 632 | |
| 633 | fatal_err: |
| 634 | gpio_free(mdm_drv->ap2mdm_status_gpio); |
| 635 | gpio_free(mdm_drv->ap2mdm_errfatal_gpio); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 636 | if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) |
| 637 | gpio_free(mdm_drv->ap2mdm_kpdpwr_n_gpio); |
| 638 | if (mdm_drv->ap2mdm_pmic_pwr_en_gpio > 0) |
| 639 | gpio_free(mdm_drv->ap2mdm_pmic_pwr_en_gpio); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 640 | gpio_free(mdm_drv->mdm2ap_status_gpio); |
| 641 | gpio_free(mdm_drv->mdm2ap_errfatal_gpio); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 642 | if (mdm_drv->ap2mdm_soft_reset_gpio > 0) |
| 643 | gpio_free(mdm_drv->ap2mdm_soft_reset_gpio); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 644 | |
| 645 | if (mdm_drv->ap2mdm_wakeup_gpio > 0) |
| 646 | gpio_free(mdm_drv->ap2mdm_wakeup_gpio); |
| 647 | |
| 648 | kfree(mdm_drv); |
| 649 | ret = -ENODEV; |
| 650 | |
| 651 | alloc_err: |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | int mdm_common_modem_remove(struct platform_device *pdev) |
| 656 | { |
| 657 | int ret; |
| 658 | |
| 659 | gpio_free(mdm_drv->ap2mdm_status_gpio); |
| 660 | gpio_free(mdm_drv->ap2mdm_errfatal_gpio); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 661 | if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) |
| 662 | gpio_free(mdm_drv->ap2mdm_kpdpwr_n_gpio); |
| 663 | if (mdm_drv->ap2mdm_pmic_pwr_en_gpio > 0) |
| 664 | gpio_free(mdm_drv->ap2mdm_pmic_pwr_en_gpio); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 665 | gpio_free(mdm_drv->mdm2ap_status_gpio); |
| 666 | gpio_free(mdm_drv->mdm2ap_errfatal_gpio); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 667 | if (mdm_drv->ap2mdm_soft_reset_gpio > 0) |
| 668 | gpio_free(mdm_drv->ap2mdm_soft_reset_gpio); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 669 | |
| 670 | if (mdm_drv->ap2mdm_wakeup_gpio > 0) |
| 671 | gpio_free(mdm_drv->ap2mdm_wakeup_gpio); |
| 672 | |
| 673 | kfree(mdm_drv); |
| 674 | |
| 675 | ret = misc_deregister(&mdm_modem_misc); |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | void mdm_common_modem_shutdown(struct platform_device *pdev) |
| 680 | { |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 681 | mdm_disable_irqs(); |
| 682 | |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 683 | mdm_drv->ops->power_down_mdm_cb(mdm_drv); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 684 | if (mdm_drv->ap2mdm_pmic_pwr_en_gpio > 0) |
| 685 | gpio_direction_output(mdm_drv->ap2mdm_pmic_pwr_en_gpio, 0); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 686 | } |
| 687 | |