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 | |
| 45 | #define MDM_MODEM_TIMEOUT 6000 |
| 46 | #define MDM_HOLD_TIME 4000 |
| 47 | #define MDM_MODEM_DELTA 100 |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame^] | 48 | #define MDM_PBLRDY_CNT 20 |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 49 | |
| 50 | static int mdm_debug_on; |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 51 | static int power_on_count; |
Hemant Kumar | f1ca919 | 2012-02-07 18:59:33 -0800 | [diff] [blame] | 52 | static int hsic_peripheral_status; |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 53 | static DEFINE_MUTEX(hsic_status_lock); |
| 54 | |
| 55 | static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv) |
| 56 | { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 57 | if (!mdm_drv->pdata->peripheral_platform_device) |
| 58 | return; |
| 59 | |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 60 | mutex_lock(&hsic_status_lock); |
| 61 | if (hsic_peripheral_status) |
| 62 | goto out; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 63 | platform_device_add(mdm_drv->pdata->peripheral_platform_device); |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 64 | hsic_peripheral_status = 1; |
| 65 | out: |
| 66 | mutex_unlock(&hsic_status_lock); |
| 67 | } |
| 68 | |
| 69 | static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv) |
| 70 | { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 71 | if (!mdm_drv->pdata->peripheral_platform_device) |
| 72 | return; |
| 73 | |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 74 | mutex_lock(&hsic_status_lock); |
| 75 | if (!hsic_peripheral_status) |
| 76 | goto out; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 77 | platform_device_del(mdm_drv->pdata->peripheral_platform_device); |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 78 | hsic_peripheral_status = 0; |
| 79 | out: |
| 80 | mutex_unlock(&hsic_status_lock); |
| 81 | } |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 82 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 83 | static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv) |
| 84 | { |
| 85 | int soft_reset_direction = |
| 86 | mdm_drv->pdata->soft_reset_inverted ? 1 : 0; |
| 87 | |
| 88 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 89 | soft_reset_direction); |
| 90 | mdm_peripheral_disconnect(mdm_drv); |
| 91 | } |
| 92 | |
| 93 | static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv) |
| 94 | { |
| 95 | int soft_reset_direction = |
| 96 | mdm_drv->pdata->soft_reset_inverted ? 0 : 1; |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame^] | 97 | int i; |
| 98 | int pblrdy; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 99 | |
| 100 | if (power_on_count != 1) { |
| 101 | pr_err("%s: Calling fn when power_on_count != 1\n", |
| 102 | __func__); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | pr_err("%s: Powering on modem for the first time\n", __func__); |
| 107 | mdm_peripheral_disconnect(mdm_drv); |
| 108 | |
| 109 | /* If the device has a kpd pwr gpio then toggle it. */ |
| 110 | if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) { |
| 111 | /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle, |
| 112 | * then pull it back low. |
| 113 | */ |
| 114 | pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__); |
| 115 | gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1); |
| 116 | msleep(1000); |
| 117 | gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0); |
| 118 | } |
| 119 | |
| 120 | /* De-assert the soft reset line. */ |
| 121 | pr_debug("%s: De-asserting soft reset gpio\n", __func__); |
| 122 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 123 | soft_reset_direction); |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame^] | 124 | if (!mdm_drv->mdm2ap_pblrdy) |
| 125 | goto start_mdm_peripheral; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 126 | |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame^] | 127 | for (i = 0; i < MDM_PBLRDY_CNT; i++) { |
| 128 | pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy); |
| 129 | if (pblrdy) |
| 130 | break; |
| 131 | usleep_range(5000, 5000); |
| 132 | } |
| 133 | |
| 134 | pr_debug("%s: i:%d\n", __func__, i); |
| 135 | |
| 136 | start_mdm_peripheral: |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 137 | mdm_peripheral_connect(mdm_drv); |
| 138 | msleep(200); |
| 139 | } |
| 140 | |
| 141 | static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv) |
| 142 | { |
| 143 | int soft_reset_direction = |
| 144 | mdm_drv->pdata->soft_reset_inverted ? 0 : 1; |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame^] | 145 | int i; |
| 146 | int pblrdy; |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 147 | |
| 148 | /* De-assert the soft reset line. */ |
| 149 | pr_err("%s: soft resetting mdm modem\n", __func__); |
| 150 | |
| 151 | mdm_peripheral_disconnect(mdm_drv); |
| 152 | |
| 153 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 154 | soft_reset_direction == 1 ? 0 : 1); |
| 155 | usleep_range(5000, 10000); |
| 156 | gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio, |
| 157 | soft_reset_direction == 1 ? 1 : 0); |
| 158 | |
Vamsi Krishna | c6dcd5e | 2012-05-09 15:38:01 -0700 | [diff] [blame^] | 159 | if (!mdm_drv->mdm2ap_pblrdy) |
| 160 | goto start_mdm_peripheral; |
| 161 | |
| 162 | for (i = 0; i < MDM_PBLRDY_CNT; i++) { |
| 163 | pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy); |
| 164 | if (pblrdy) |
| 165 | break; |
| 166 | usleep_range(5000, 5000); |
| 167 | } |
| 168 | |
| 169 | pr_debug("%s: i:%d\n", __func__, i); |
| 170 | |
| 171 | start_mdm_peripheral: |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 172 | mdm_peripheral_connect(mdm_drv); |
| 173 | msleep(200); |
| 174 | } |
| 175 | |
| 176 | static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv) |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 177 | { |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 178 | power_on_count++; |
| 179 | |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 180 | /* this gpio will be used to indicate apq readiness, |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 181 | * de-assert it now so that it can be asserted later. |
| 182 | * May not be used. |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 183 | */ |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 184 | if (mdm_drv->ap2mdm_wakeup_gpio > 0) |
| 185 | gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0); |
Vamsi Krishna | 9e307cd | 2012-04-11 13:15:36 -0700 | [diff] [blame] | 186 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 187 | /* |
| 188 | * If we did an "early power on" then ignore the very next |
| 189 | * power-on request because it would the be first request from |
| 190 | * user space but we're already powered on. Ignore it. |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 191 | */ |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 192 | if (mdm_drv->pdata->early_power_on && |
| 193 | (power_on_count == 2)) |
Joel King | 3da02a9 | 2012-03-20 10:55:52 -0700 | [diff] [blame] | 194 | return; |
| 195 | |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 196 | if (power_on_count == 1) |
| 197 | mdm_do_first_power_on(mdm_drv); |
| 198 | else |
| 199 | mdm_do_soft_power_on(mdm_drv); |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 202 | static void debug_state_changed(int value) |
| 203 | { |
| 204 | mdm_debug_on = value; |
| 205 | } |
| 206 | |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 207 | 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] | 208 | { |
Joel King | 2a42f50 | 2012-02-03 11:36:25 -0800 | [diff] [blame] | 209 | pr_debug("%s: value:%d\n", __func__, value); |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 210 | |
| 211 | if (value) { |
Joel King | d8052c0 | 2012-02-03 12:33:31 -0800 | [diff] [blame] | 212 | mdm_peripheral_disconnect(mdm_drv); |
| 213 | mdm_peripheral_connect(mdm_drv); |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 214 | if (mdm_drv->ap2mdm_wakeup_gpio > 0) |
| 215 | gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1); |
Vamsi Krishna | 3392563 | 2011-12-13 15:43:09 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 219 | static struct mdm_ops mdm_cb = { |
Joel King | e92eb87 | 2012-05-06 09:30:24 -0700 | [diff] [blame] | 220 | .power_on_mdm_cb = mdm_power_on_common, |
| 221 | .reset_mdm_cb = mdm_power_on_common, |
| 222 | .power_down_mdm_cb = mdm_power_down_common, |
Joel King | e9cd527 | 2012-01-28 12:48:59 -0800 | [diff] [blame] | 223 | .debug_state_changed_cb = debug_state_changed, |
| 224 | .status_cb = mdm_status_changed, |
| 225 | }; |
| 226 | |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 227 | static int __init mdm_modem_probe(struct platform_device *pdev) |
| 228 | { |
Joel King | b6f0f61 | 2011-11-01 16:59:14 -0700 | [diff] [blame] | 229 | return mdm_common_create(pdev, &mdm_cb); |
| 230 | } |
| 231 | |
| 232 | static int __devexit mdm_modem_remove(struct platform_device *pdev) |
| 233 | { |
| 234 | return mdm_common_modem_remove(pdev); |
| 235 | } |
| 236 | |
| 237 | static void mdm_modem_shutdown(struct platform_device *pdev) |
| 238 | { |
| 239 | mdm_common_modem_shutdown(pdev); |
| 240 | } |
| 241 | |
| 242 | static struct platform_driver mdm_modem_driver = { |
| 243 | .remove = mdm_modem_remove, |
| 244 | .shutdown = mdm_modem_shutdown, |
| 245 | .driver = { |
| 246 | .name = "mdm2_modem", |
| 247 | .owner = THIS_MODULE |
| 248 | }, |
| 249 | }; |
| 250 | |
| 251 | static int __init mdm_modem_init(void) |
| 252 | { |
| 253 | return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe); |
| 254 | } |
| 255 | |
| 256 | static void __exit mdm_modem_exit(void) |
| 257 | { |
| 258 | platform_driver_unregister(&mdm_modem_driver); |
| 259 | } |
| 260 | |
| 261 | module_init(mdm_modem_init); |
| 262 | module_exit(mdm_modem_exit); |
| 263 | |
| 264 | MODULE_LICENSE("GPL v2"); |
| 265 | MODULE_DESCRIPTION("mdm modem driver"); |
| 266 | MODULE_VERSION("2.0"); |
| 267 | MODULE_ALIAS("mdm_modem"); |