blob: 76272deda4e6973328752c2a66cb4d90e770d340 [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2011-2012, 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 Thakurffd21b02013-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 Thakurffd21b02013-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 Thakurffd21b02013-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 Thakurffd21b02013-01-30 11:33:22 -080057 mdm_drv->peripheral_status = 1;
Joel Kingd8052c02012-02-03 12:33:31 -080058out:
Ameya Thakurffd21b02013-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 Thakurffd21b02013-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 Thakurffd21b02013-01-30 11:33:22 -080071 mdm_drv->peripheral_status = 0;
Joel Kingd8052c02012-02-03 12:33:31 -080072out:
Ameya Thakurffd21b02013-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
Devin Kim803bb632012-08-30 02:54: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--) {
agathon.jungb0c0c6d2012-09-26 11:36:51 -0700112 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) {
113 if (mdm_debug_mask & MDM_DEBUG_MASK_SHDN_LOG)
Ameya Thakurffd21b02013-01-30 11:33:22 -0800114 pr_info("%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;
agathon.jungb0c0c6d2012-09-26 11:36:51 -0700117 }
Joel King733377c2012-06-20 13:07:38 -0700118 msleep(100);
119 }
Taniya Das70a64b42012-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,
Joel King733377c2012-06-20 13:07:38 -0700123 soft_reset_direction);
Taniya Das70a64b42012-12-11 10:23:33 +0530124 if (i == 0) {
125 pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n",
126 __func__);
Joel King733377c2012-06-20 13:07:38 -0700127 /*
128 * Currently, there is a debounce timer on the charm PMIC. It is
129 * necessary to hold the PMIC RESET low for ~3.5 seconds
130 * for the reset to fully take place. Sleep here to ensure the
131 * reset has occured before the function exits.
132 */
133 msleep(4000);
134 }
Joel Kinge92eb872012-05-06 09:30:24 -0700135}
136
137static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
138{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700139 int i;
140 int pblrdy;
Joel King68cb5462013-03-15 12:54:33 -0700141 int kpd_direction_assert = 1,
142 kpd_direction_de_assert = 0;
143
144 if (mdm_drv->pdata->kpd_not_inverted) {
145 kpd_direction_assert = 0;
146 kpd_direction_de_assert = 1;
147 }
148
Ameya Thakurffd21b02013-01-30 11:33:22 -0800149 if (mdm_drv->power_on_count != 1) {
150 pr_err("%s:id %d: Calling fn when power_on_count != 1\n",
151 __func__, mdm_drv->device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700152 return;
153 }
154
Ameya Thakurffd21b02013-01-30 11:33:22 -0800155 pr_err("%s:id %d: Powering on modem for the first time\n",
156 __func__, mdm_drv->device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700157 mdm_peripheral_disconnect(mdm_drv);
158
Joel King733377c2012-06-20 13:07:38 -0700159 /* If this is the first power-up after a panic, the modem may still
160 * be in a power-on state, in which case we need to toggle the gpio
161 * instead of just de-asserting it. No harm done if the modem was
162 * powered down.
163 */
Joel King68cb5462013-03-15 12:54:33 -0700164 if (!mdm_drv->pdata->no_reset_on_first_powerup)
165 mdm_toggle_soft_reset(mdm_drv);
166
Joel Kinge92eb872012-05-06 09:30:24 -0700167 /* If the device has a kpd pwr gpio then toggle it. */
Ameya Thakure155ece2012-07-09 12:08:37 -0700168 if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio)) {
Joel Kinge92eb872012-05-06 09:30:24 -0700169 /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
170 * then pull it back low.
171 */
Taniya Das51d80662013-01-21 15:14:15 +0530172 pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__);
173 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
174 gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1);
Joel Kinge92eb872012-05-06 09:30:24 -0700175 msleep(1000);
Taniya Das51d80662013-01-21 15:14:15 +0530176 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
177 } else
178 gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1);
Joel Kinge92eb872012-05-06 09:30:24 -0700179
Ameya Thakure155ece2012-07-09 12:08:37 -0700180 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700181 goto start_mdm_peripheral;
Joel Kinge92eb872012-05-06 09:30:24 -0700182
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700183 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
184 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
185 if (pblrdy)
186 break;
187 usleep_range(5000, 5000);
188 }
Ameya Thakurffd21b02013-01-30 11:33:22 -0800189 pr_debug("%s: id %d: pblrdy i:%d\n", __func__,
190 mdm_drv->device_id, i);
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700191
192start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700193 mdm_peripheral_connect(mdm_drv);
194 msleep(200);
195}
196
197static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
198{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700199 int i;
200 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700201
Ameya Thakurffd21b02013-01-30 11:33:22 -0800202 pr_err("%s: id %d: soft resetting mdm modem\n",
203 __func__, mdm_drv->device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700204 mdm_peripheral_disconnect(mdm_drv);
Joel King733377c2012-06-20 13:07:38 -0700205 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700206
Ameya Thakure155ece2012-07-09 12:08:37 -0700207 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700208 goto start_mdm_peripheral;
209
210 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
211 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
212 if (pblrdy)
213 break;
214 usleep_range(5000, 5000);
215 }
216
Ameya Thakurffd21b02013-01-30 11:33:22 -0800217 pr_debug("%s: id %d: pblrdy i:%d\n", __func__,
218 mdm_drv->device_id, i);
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700219
220start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700221 mdm_peripheral_connect(mdm_drv);
222 msleep(200);
223}
224
225static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700226{
Ameya Thakurffd21b02013-01-30 11:33:22 -0800227 mdm_drv->power_on_count++;
Joel King3da02a92012-03-20 10:55:52 -0700228
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700229 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700230 * de-assert it now so that it can be asserted later.
231 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700232 */
Ameya Thakure155ece2012-07-09 12:08:37 -0700233 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700234 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700235
Joel Kinge92eb872012-05-06 09:30:24 -0700236 /*
237 * If we did an "early power on" then ignore the very next
238 * power-on request because it would the be first request from
239 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700240 */
Joel Kinge92eb872012-05-06 09:30:24 -0700241 if (mdm_drv->pdata->early_power_on &&
Ameya Thakurffd21b02013-01-30 11:33:22 -0800242 (mdm_drv->power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700243 return;
244
Ameya Thakurffd21b02013-01-30 11:33:22 -0800245 if (mdm_drv->power_on_count == 1)
Joel Kinge92eb872012-05-06 09:30:24 -0700246 mdm_do_first_power_on(mdm_drv);
247 else
248 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700249}
250
Joel Kingb6f0f612011-11-01 16:59:14 -0700251static void debug_state_changed(int value)
252{
Joel King96c96dc2012-07-30 09:06:15 -0700253 mdm_debug_mask = value;
Joel Kingb6f0f612011-11-01 16:59:14 -0700254}
255
Joel Kingd8052c02012-02-03 12:33:31 -0800256static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800257{
Ameya Thakurffd21b02013-01-30 11:33:22 -0800258 pr_debug("%s: id %d: value:%d\n", __func__,
259 value, mdm_drv->device_id);
Vamsi Krishna33925632011-12-13 15:43:09 -0800260
261 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800262 mdm_peripheral_disconnect(mdm_drv);
263 mdm_peripheral_connect(mdm_drv);
Ameya Thakure155ece2012-07-09 12:08:37 -0700264 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700265 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800266 }
267}
268
Ameya Thakur43248fd2012-07-10 18:50:52 -0700269static void mdm_image_upgrade(struct mdm_modem_drv *mdm_drv, int type)
270{
271 switch (type) {
272 case APQ_CONTROLLED_UPGRADE:
Ameya Thakurffd21b02013-01-30 11:33:22 -0800273 pr_debug("%s: id %d: APQ 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 mdm_toggle_soft_reset(mdm_drv);
277 break;
278 case MDM_CONTROLLED_UPGRADE:
Ameya Thakurffd21b02013-01-30 11:33:22 -0800279 pr_debug("%s: id %d: MDM controlled modem image upgrade\n",
280 __func__, mdm_drv->device_id);
281 atomic_set(&mdm_drv->mdm_ready, 0);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700282 /*
283 * If we have no image currently present on the modem, then we
284 * would be in PBL, in which case the status gpio would not go
285 * high.
286 */
287 mdm_drv->disable_status_check = 1;
Ameya Thakure155ece2012-07-09 12:08:37 -0700288 if (GPIO_IS_VALID(mdm_drv->usb_switch_gpio)) {
Ameya Thakurffd21b02013-01-30 11:33:22 -0800289 pr_info("%s: id %d: Switching usb control to MDM\n",
290 __func__, mdm_drv->device_id);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700291 gpio_direction_output(mdm_drv->usb_switch_gpio, 1);
292 } else
Ameya Thakurffd21b02013-01-30 11:33:22 -0800293 pr_err("%s: id %d: usb switch gpio unavailable\n",
294 __func__, mdm_drv->device_id);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700295 break;
296 default:
Ameya Thakurffd21b02013-01-30 11:33:22 -0800297 pr_err("%s: id %d: invalid upgrade type\n",
298 __func__, mdm_drv->device_id);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700299 }
300}
Ameya Thakurffd21b02013-01-30 11:33:22 -0800301
Joel Kinge9cd5272012-01-28 12:48:59 -0800302static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700303 .power_on_mdm_cb = mdm_power_on_common,
304 .reset_mdm_cb = mdm_power_on_common,
Joel Kinga7a7b9a2012-06-28 13:35:28 -0700305 .atomic_reset_mdm_cb = mdm_atomic_soft_reset,
Joel Kinge92eb872012-05-06 09:30:24 -0700306 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800307 .debug_state_changed_cb = debug_state_changed,
308 .status_cb = mdm_status_changed,
Ameya Thakur43248fd2012-07-10 18:50:52 -0700309 .image_upgrade_cb = mdm_image_upgrade,
Joel Kinge9cd5272012-01-28 12:48:59 -0800310};
311
Ameya Thakurffd21b02013-01-30 11:33:22 -0800312int mdm_get_ops(struct mdm_ops **mdm_ops)
Joel Kingb6f0f612011-11-01 16:59:14 -0700313{
Ameya Thakurffd21b02013-01-30 11:33:22 -0800314 *mdm_ops = &mdm_cb;
315 return 0;
Joel Kingb6f0f612011-11-01 16:59:14 -0700316}
317
Joel Kingb6f0f612011-11-01 16:59:14 -0700318