blob: e43a0e27870757e00aa17175df0ad08f5daa6cd6 [file] [log] [blame]
Ameya Thakur552e58a2013-01-30 11:33:22 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Joel Kingb6f0f612011-11-01 16:59:14 -07002 *
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>
Ameya Thakur552e58a2013-01-30 11:33:22 -080031#include <linux/clk.h>
Joel Kingb6f0f612011-11-01 16:59:14 -070032#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"
Ameya Thakur552e58a2013-01-30 11:33:22 -080042#include "clock.h"
Joel Kingb6f0f612011-11-01 16:59:14 -070043#include "mdm_private.h"
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -070044#define MDM_PBLRDY_CNT 20
Joel Kingb6f0f612011-11-01 16:59:14 -070045
Joel King96c96dc2012-07-30 09:06:15 -070046static int mdm_debug_mask;
Joel Kingd8052c02012-02-03 12:33:31 -080047
48static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv)
49{
Joel Kinge92eb872012-05-06 09:30:24 -070050 if (!mdm_drv->pdata->peripheral_platform_device)
51 return;
52
Ameya Thakur552e58a2013-01-30 11:33:22 -080053 mutex_lock(&mdm_drv->peripheral_status_lock);
54 if (mdm_drv->peripheral_status)
Joel Kingd8052c02012-02-03 12:33:31 -080055 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070056 platform_device_add(mdm_drv->pdata->peripheral_platform_device);
Ameya Thakur552e58a2013-01-30 11:33:22 -080057 mdm_drv->peripheral_status = 1;
Joel Kingd8052c02012-02-03 12:33:31 -080058out:
Ameya Thakur552e58a2013-01-30 11:33:22 -080059 mutex_unlock(&mdm_drv->peripheral_status_lock);
Joel Kingd8052c02012-02-03 12:33:31 -080060}
61
62static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv)
63{
Joel Kinge92eb872012-05-06 09:30:24 -070064 if (!mdm_drv->pdata->peripheral_platform_device)
65 return;
66
Ameya Thakur552e58a2013-01-30 11:33:22 -080067 mutex_lock(&mdm_drv->peripheral_status_lock);
68 if (!mdm_drv->peripheral_status)
Joel Kingd8052c02012-02-03 12:33:31 -080069 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070070 platform_device_del(mdm_drv->pdata->peripheral_platform_device);
Ameya Thakur552e58a2013-01-30 11:33:22 -080071 mdm_drv->peripheral_status = 0;
Joel Kingd8052c02012-02-03 12:33:31 -080072out:
Ameya Thakur552e58a2013-01-30 11:33:22 -080073 mutex_unlock(&mdm_drv->peripheral_status_lock);
Joel Kingd8052c02012-02-03 12:33:31 -080074}
Joel Kingb6f0f612011-11-01 16:59:14 -070075
Joel Kinga7a7b9a2012-06-28 13:35:28 -070076/* This function can be called from atomic context. */
Joel King733377c2012-06-20 13:07:38 -070077static void mdm_toggle_soft_reset(struct mdm_modem_drv *mdm_drv)
78{
79 int soft_reset_direction_assert = 0,
80 soft_reset_direction_de_assert = 1;
81
82 if (mdm_drv->pdata->soft_reset_inverted) {
83 soft_reset_direction_assert = 1;
84 soft_reset_direction_de_assert = 0;
85 }
86 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
87 soft_reset_direction_assert);
Joel Kinga7a7b9a2012-06-28 13:35:28 -070088 /* Use mdelay because this function can be called from atomic
89 * context.
90 */
91 mdelay(10);
Joel King733377c2012-06-20 13:07:38 -070092 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
93 soft_reset_direction_de_assert);
94}
95
Joel Kinga7a7b9a2012-06-28 13:35:28 -070096/* This function can be called from atomic context. */
97static void mdm_atomic_soft_reset(struct mdm_modem_drv *mdm_drv)
98{
99 mdm_toggle_soft_reset(mdm_drv);
100}
101
Joel Kinge92eb872012-05-06 09:30:24 -0700102static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv)
103{
Joel King733377c2012-06-20 13:07:38 -0700104 int i;
Joel Kinge92eb872012-05-06 09:30:24 -0700105 int soft_reset_direction =
106 mdm_drv->pdata->soft_reset_inverted ? 1 : 0;
107
Hemant Kumar069f6432012-08-22 18:30:19 -0700108 mdm_peripheral_disconnect(mdm_drv);
109
Joel King733377c2012-06-20 13:07:38 -0700110 /* Wait for the modem to complete its power down actions. */
111 for (i = 20; i > 0; i--) {
Joel Kingbf3e4b52012-09-26 09:10:34 -0700112 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) {
113 if (mdm_debug_mask & MDM_DEBUG_MASK_SHDN_LOG)
Ameya Thakur552e58a2013-01-30 11:33:22 -0800114 pr_debug("%s:id %d: mdm2ap_statuswent low, i=%d\n",
115 __func__, mdm_drv->device_id, i);
Joel King733377c2012-06-20 13:07:38 -0700116 break;
Joel Kingbf3e4b52012-09-26 09:10:34 -0700117 }
Joel King733377c2012-06-20 13:07:38 -0700118 msleep(100);
119 }
Taniya Das835e28f2012-12-11 10:23:33 +0530120
121 /* Assert the soft reset line whether mdm2ap_status went low or not */
122 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
123 soft_reset_direction);
Joel King733377c2012-06-20 13:07:38 -0700124 if (i == 0) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800125 pr_debug("%s:id %d: MDM2AP_STATUS never went low. Doing a hard reset\n",
126 __func__, mdm_drv->device_id);
127 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
128 soft_reset_direction);
Joel King733377c2012-06-20 13:07:38 -0700129 /*
130 * Currently, there is a debounce timer on the charm PMIC. It is
131 * necessary to hold the PMIC RESET low for ~3.5 seconds
132 * for the reset to fully take place. Sleep here to ensure the
133 * reset has occured before the function exits.
134 */
135 msleep(4000);
136 }
Joel Kinge92eb872012-05-06 09:30:24 -0700137}
138
139static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
140{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700141 int i;
142 int pblrdy;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800143 if (mdm_drv->power_on_count != 1) {
144 pr_debug("%s:id %d: Calling fn when power_on_count != 1\n",
145 __func__, mdm_drv->device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700146 return;
147 }
148
Ameya Thakur552e58a2013-01-30 11:33:22 -0800149 pr_debug("%s:id %d: Powering on modem for the first time\n",
150 __func__, mdm_drv->device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700151 mdm_peripheral_disconnect(mdm_drv);
152
Joel King733377c2012-06-20 13:07:38 -0700153 /* If this is the first power-up after a panic, the modem may still
154 * be in a power-on state, in which case we need to toggle the gpio
155 * instead of just de-asserting it. No harm done if the modem was
156 * powered down.
157 */
158 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700159 /* If the device has a kpd pwr gpio then toggle it. */
Ameya Thakure155ece2012-07-09 12:08:37 -0700160 if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio)) {
Joel Kinge92eb872012-05-06 09:30:24 -0700161 /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
162 * then pull it back low.
163 */
Ameya Thakur552e58a2013-01-30 11:33:22 -0800164 pr_debug("%s:id %d: Pulling AP2MDM_KPDPWR gpio high\n",
165 __func__, mdm_drv->device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700166 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
Taniya Dasa1a14a92013-01-21 15:14:15 +0530167 gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1);
Joel Kinge92eb872012-05-06 09:30:24 -0700168 msleep(1000);
169 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800170 } else {
Taniya Dasa1a14a92013-01-21 15:14:15 +0530171 gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800172 }
Joel Kinge92eb872012-05-06 09:30:24 -0700173
Ameya Thakure155ece2012-07-09 12:08:37 -0700174 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700175 goto start_mdm_peripheral;
Joel Kinge92eb872012-05-06 09:30:24 -0700176
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700177 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
178 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
179 if (pblrdy)
180 break;
181 usleep_range(5000, 5000);
182 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800183 pr_debug("%s: id %d: pblrdy i:%d\n", __func__,
184 mdm_drv->device_id, i);
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700185
186start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700187 mdm_peripheral_connect(mdm_drv);
188 msleep(200);
189}
190
191static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
192{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700193 int i;
194 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700195
Ameya Thakur552e58a2013-01-30 11:33:22 -0800196 pr_debug("%s: id %d: soft resetting mdm modem\n",
197 __func__, mdm_drv->device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700198 mdm_peripheral_disconnect(mdm_drv);
Joel King733377c2012-06-20 13:07:38 -0700199 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700200
Ameya Thakure155ece2012-07-09 12:08:37 -0700201 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700202 goto start_mdm_peripheral;
203
204 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
205 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
206 if (pblrdy)
207 break;
208 usleep_range(5000, 5000);
209 }
210
Ameya Thakur552e58a2013-01-30 11:33:22 -0800211 pr_debug("%s: id %d: pblrdy i:%d\n", __func__,
212 mdm_drv->device_id, i);
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700213
214start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700215 mdm_peripheral_connect(mdm_drv);
216 msleep(200);
217}
218
219static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700220{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800221 mdm_drv->power_on_count++;
Joel King3da02a92012-03-20 10:55:52 -0700222
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700223 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700224 * de-assert it now so that it can be asserted later.
225 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700226 */
Ameya Thakure155ece2012-07-09 12:08:37 -0700227 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700228 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700229
Joel Kinge92eb872012-05-06 09:30:24 -0700230 /*
231 * If we did an "early power on" then ignore the very next
232 * power-on request because it would the be first request from
233 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700234 */
Joel Kinge92eb872012-05-06 09:30:24 -0700235 if (mdm_drv->pdata->early_power_on &&
Ameya Thakur552e58a2013-01-30 11:33:22 -0800236 (mdm_drv->power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700237 return;
238
Ameya Thakur552e58a2013-01-30 11:33:22 -0800239 if (mdm_drv->power_on_count == 1)
Joel Kinge92eb872012-05-06 09:30:24 -0700240 mdm_do_first_power_on(mdm_drv);
241 else
242 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700243}
244
Joel Kingb6f0f612011-11-01 16:59:14 -0700245static void debug_state_changed(int value)
246{
Joel King96c96dc2012-07-30 09:06:15 -0700247 mdm_debug_mask = value;
Joel Kingb6f0f612011-11-01 16:59:14 -0700248}
249
Joel Kingd8052c02012-02-03 12:33:31 -0800250static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800251{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800252 pr_debug("%s: id %d: value:%d\n", __func__,
253 value, mdm_drv->device_id);
Vamsi Krishna33925632011-12-13 15:43:09 -0800254
255 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800256 mdm_peripheral_disconnect(mdm_drv);
257 mdm_peripheral_connect(mdm_drv);
Ameya Thakure155ece2012-07-09 12:08:37 -0700258 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700259 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800260 }
261}
262
Ameya Thakur43248fd2012-07-10 18:50:52 -0700263static void mdm_image_upgrade(struct mdm_modem_drv *mdm_drv, int type)
264{
265 switch (type) {
266 case APQ_CONTROLLED_UPGRADE:
Ameya Thakur552e58a2013-01-30 11:33:22 -0800267 pr_debug("%s: id %d: APQ controlled modem image upgrade\n",
268 __func__, mdm_drv->device_id);
269 atomic_set(&mdm_drv->mdm_ready, 0);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700270 mdm_toggle_soft_reset(mdm_drv);
271 break;
272 case MDM_CONTROLLED_UPGRADE:
Ameya Thakur552e58a2013-01-30 11:33:22 -0800273 pr_debug("%s: id %d: MDM controlled modem image upgrade\n",
274 __func__, mdm_drv->device_id);
275 atomic_set(&mdm_drv->mdm_ready, 0);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700276 /*
277 * If we have no image currently present on the modem, then we
278 * would be in PBL, in which case the status gpio would not go
279 * high.
280 */
281 mdm_drv->disable_status_check = 1;
Ameya Thakure155ece2012-07-09 12:08:37 -0700282 if (GPIO_IS_VALID(mdm_drv->usb_switch_gpio)) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800283 pr_debug("%s: id %d: Switching usb control to MDM\n",
284 __func__, mdm_drv->device_id);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700285 gpio_direction_output(mdm_drv->usb_switch_gpio, 1);
286 } else
Ameya Thakur552e58a2013-01-30 11:33:22 -0800287 pr_err("%s: id %d: usb switch gpio unavailable\n",
288 __func__, mdm_drv->device_id);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700289 break;
290 default:
Ameya Thakur552e58a2013-01-30 11:33:22 -0800291 pr_err("%s: id %d: invalid upgrade type\n",
292 __func__, mdm_drv->device_id);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700293 }
294}
Ameya Thakur552e58a2013-01-30 11:33:22 -0800295
Joel Kinge9cd5272012-01-28 12:48:59 -0800296static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700297 .power_on_mdm_cb = mdm_power_on_common,
298 .reset_mdm_cb = mdm_power_on_common,
Joel Kinga7a7b9a2012-06-28 13:35:28 -0700299 .atomic_reset_mdm_cb = mdm_atomic_soft_reset,
Joel Kinge92eb872012-05-06 09:30:24 -0700300 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800301 .debug_state_changed_cb = debug_state_changed,
302 .status_cb = mdm_status_changed,
Ameya Thakur43248fd2012-07-10 18:50:52 -0700303 .image_upgrade_cb = mdm_image_upgrade,
Joel Kinge9cd5272012-01-28 12:48:59 -0800304};
305
Ameya Thakur552e58a2013-01-30 11:33:22 -0800306int mdm_get_ops(struct mdm_ops **mdm_ops)
Joel Kingb6f0f612011-11-01 16:59:14 -0700307{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800308 *mdm_ops = &mdm_cb;
309 return 0;
Joel Kingb6f0f612011-11-01 16:59:14 -0700310}
311
Joel Kingb6f0f612011-11-01 16:59:14 -0700312