blob: c8d6d5a541d06d3a7133e3828f2098bea4ae376d [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -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>
Joel Kingb6f0f612011-11-01 16:59:14 -070031#include <linux/mfd/pmic8058.h>
32#include <asm/mach-types.h>
33#include <asm/uaccess.h>
34#include <mach/mdm2.h>
35#include <mach/restart.h>
36#include <mach/subsystem_notif.h>
37#include <mach/subsystem_restart.h>
38#include <linux/msm_charm.h>
39#include "msm_watchdog.h"
40#include "devices.h"
Joel Kingb6f0f612011-11-01 16:59:14 -070041#include "mdm_private.h"
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -070042#define MDM_PBLRDY_CNT 20
Joel Kingb6f0f612011-11-01 16:59:14 -070043
Joel King96c96dc2012-07-30 09:06:15 -070044static int mdm_debug_mask;
Joel King3da02a92012-03-20 10:55:52 -070045static int power_on_count;
Hemant Kumarf1ca9192012-02-07 18:59:33 -080046static int hsic_peripheral_status;
Joel Kingd8052c02012-02-03 12:33:31 -080047static DEFINE_MUTEX(hsic_status_lock);
48
49static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv)
50{
Joel Kinge92eb872012-05-06 09:30:24 -070051 if (!mdm_drv->pdata->peripheral_platform_device)
52 return;
53
Joel Kingd8052c02012-02-03 12:33:31 -080054 mutex_lock(&hsic_status_lock);
55 if (hsic_peripheral_status)
56 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070057 platform_device_add(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080058 hsic_peripheral_status = 1;
59out:
60 mutex_unlock(&hsic_status_lock);
61}
62
63static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv)
64{
Joel Kinge92eb872012-05-06 09:30:24 -070065 if (!mdm_drv->pdata->peripheral_platform_device)
66 return;
67
Joel Kingd8052c02012-02-03 12:33:31 -080068 mutex_lock(&hsic_status_lock);
69 if (!hsic_peripheral_status)
70 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070071 platform_device_del(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080072 hsic_peripheral_status = 0;
73out:
74 mutex_unlock(&hsic_status_lock);
75}
Joel Kingb6f0f612011-11-01 16:59:14 -070076
Joel Kinga7a7b9a2012-06-28 13:35:28 -070077/* This function can be called from atomic context. */
Joel King733377c2012-06-20 13:07:38 -070078static void mdm_toggle_soft_reset(struct mdm_modem_drv *mdm_drv)
79{
80 int soft_reset_direction_assert = 0,
81 soft_reset_direction_de_assert = 1;
82
83 if (mdm_drv->pdata->soft_reset_inverted) {
84 soft_reset_direction_assert = 1;
85 soft_reset_direction_de_assert = 0;
86 }
87 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
88 soft_reset_direction_assert);
Joel Kinga7a7b9a2012-06-28 13:35:28 -070089 /* Use mdelay because this function can be called from atomic
90 * context.
91 */
92 mdelay(10);
Joel King733377c2012-06-20 13:07:38 -070093 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
94 soft_reset_direction_de_assert);
95}
96
Joel Kinga7a7b9a2012-06-28 13:35:28 -070097/* This function can be called from atomic context. */
98static void mdm_atomic_soft_reset(struct mdm_modem_drv *mdm_drv)
99{
100 mdm_toggle_soft_reset(mdm_drv);
101}
102
Joel Kinge92eb872012-05-06 09:30:24 -0700103static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv)
104{
Joel King733377c2012-06-20 13:07:38 -0700105 int i;
Joel Kinge92eb872012-05-06 09:30:24 -0700106 int soft_reset_direction =
107 mdm_drv->pdata->soft_reset_inverted ? 1 : 0;
108
Hemant Kumar069f6432012-08-22 18:30:19 -0700109 mdm_peripheral_disconnect(mdm_drv);
110
Joel King733377c2012-06-20 13:07:38 -0700111 /* Wait for the modem to complete its power down actions. */
112 for (i = 20; i > 0; i--) {
Joel Kingbf3e4b52012-09-26 09:10:34 -0700113 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) {
114 if (mdm_debug_mask & MDM_DEBUG_MASK_SHDN_LOG)
115 pr_info("%s: mdm2ap_status went low, i = %d\n",
116 __func__, i);
Joel King733377c2012-06-20 13:07:38 -0700117 break;
Joel Kingbf3e4b52012-09-26 09:10:34 -0700118 }
Joel King733377c2012-06-20 13:07:38 -0700119 msleep(100);
120 }
Taniya Das835e28f2012-12-11 10:23:33 +0530121
122 /* Assert the soft reset line whether mdm2ap_status went low or not */
123 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
124 soft_reset_direction);
Joel King733377c2012-06-20 13:07:38 -0700125 if (i == 0) {
126 pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n",
127 __func__);
Joel King733377c2012-06-20 13:07:38 -0700128 /*
129 * Currently, there is a debounce timer on the charm PMIC. It is
130 * necessary to hold the PMIC RESET low for ~3.5 seconds
131 * for the reset to fully take place. Sleep here to ensure the
132 * reset has occured before the function exits.
133 */
134 msleep(4000);
135 }
Joel Kinge92eb872012-05-06 09:30:24 -0700136}
137
138static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
139{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700140 int i;
141 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700142 if (power_on_count != 1) {
143 pr_err("%s: Calling fn when power_on_count != 1\n",
144 __func__);
145 return;
146 }
147
148 pr_err("%s: Powering on modem for the first time\n", __func__);
149 mdm_peripheral_disconnect(mdm_drv);
150
Joel King733377c2012-06-20 13:07:38 -0700151 /* If this is the first power-up after a panic, the modem may still
152 * be in a power-on state, in which case we need to toggle the gpio
153 * instead of just de-asserting it. No harm done if the modem was
154 * powered down.
155 */
156 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700157 /* If the device has a kpd pwr gpio then toggle it. */
Ameya Thakure155ece2012-07-09 12:08:37 -0700158 if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio)) {
Joel Kinge92eb872012-05-06 09:30:24 -0700159 /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
160 * then pull it back low.
161 */
162 pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__);
163 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
Taniya Dasa1a14a92013-01-21 15:14:15 +0530164 gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1);
Joel Kinge92eb872012-05-06 09:30:24 -0700165 msleep(1000);
166 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
Taniya Dasa1a14a92013-01-21 15:14:15 +0530167 } else
168 gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1);
Joel Kinge92eb872012-05-06 09:30:24 -0700169
Ameya Thakure155ece2012-07-09 12:08:37 -0700170 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700171 goto start_mdm_peripheral;
Joel Kinge92eb872012-05-06 09:30:24 -0700172
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700173 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
174 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
175 if (pblrdy)
176 break;
177 usleep_range(5000, 5000);
178 }
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700179 pr_debug("%s: i:%d\n", __func__, i);
180
181start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700182 mdm_peripheral_connect(mdm_drv);
183 msleep(200);
184}
185
186static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
187{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700188 int i;
189 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700190
Joel Kinge92eb872012-05-06 09:30:24 -0700191 pr_err("%s: soft resetting mdm modem\n", __func__);
Joel Kinge92eb872012-05-06 09:30:24 -0700192 mdm_peripheral_disconnect(mdm_drv);
Joel King733377c2012-06-20 13:07:38 -0700193 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700194
Ameya Thakure155ece2012-07-09 12:08:37 -0700195 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700196 goto start_mdm_peripheral;
197
198 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
199 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
200 if (pblrdy)
201 break;
202 usleep_range(5000, 5000);
203 }
204
205 pr_debug("%s: i:%d\n", __func__, i);
206
207start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700208 mdm_peripheral_connect(mdm_drv);
209 msleep(200);
210}
211
212static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700213{
Joel King3da02a92012-03-20 10:55:52 -0700214 power_on_count++;
215
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700216 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700217 * de-assert it now so that it can be asserted later.
218 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700219 */
Ameya Thakure155ece2012-07-09 12:08:37 -0700220 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700221 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700222
Joel Kinge92eb872012-05-06 09:30:24 -0700223 /*
224 * If we did an "early power on" then ignore the very next
225 * power-on request because it would the be first request from
226 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700227 */
Joel Kinge92eb872012-05-06 09:30:24 -0700228 if (mdm_drv->pdata->early_power_on &&
229 (power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700230 return;
231
Joel Kinge92eb872012-05-06 09:30:24 -0700232 if (power_on_count == 1)
233 mdm_do_first_power_on(mdm_drv);
234 else
235 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700236}
237
Joel Kingb6f0f612011-11-01 16:59:14 -0700238static void debug_state_changed(int value)
239{
Joel King96c96dc2012-07-30 09:06:15 -0700240 mdm_debug_mask = value;
Joel Kingb6f0f612011-11-01 16:59:14 -0700241}
242
Joel Kingd8052c02012-02-03 12:33:31 -0800243static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800244{
Joel King2a42f502012-02-03 11:36:25 -0800245 pr_debug("%s: value:%d\n", __func__, value);
Vamsi Krishna33925632011-12-13 15:43:09 -0800246
247 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800248 mdm_peripheral_disconnect(mdm_drv);
249 mdm_peripheral_connect(mdm_drv);
Ameya Thakure155ece2012-07-09 12:08:37 -0700250 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700251 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800252 }
253}
254
Ameya Thakur43248fd2012-07-10 18:50:52 -0700255static void mdm_image_upgrade(struct mdm_modem_drv *mdm_drv, int type)
256{
257 switch (type) {
258 case APQ_CONTROLLED_UPGRADE:
259 pr_debug("%s APQ controlled modem image upgrade\n", __func__);
260 mdm_drv->mdm_ready = 0;
261 mdm_toggle_soft_reset(mdm_drv);
262 break;
263 case MDM_CONTROLLED_UPGRADE:
264 pr_debug("%s MDM controlled modem image upgrade\n", __func__);
265 mdm_drv->mdm_ready = 0;
266 /*
267 * If we have no image currently present on the modem, then we
268 * would be in PBL, in which case the status gpio would not go
269 * high.
270 */
271 mdm_drv->disable_status_check = 1;
Ameya Thakure155ece2012-07-09 12:08:37 -0700272 if (GPIO_IS_VALID(mdm_drv->usb_switch_gpio)) {
Ameya Thakur43248fd2012-07-10 18:50:52 -0700273 pr_info("%s Switching usb control to MDM\n", __func__);
274 gpio_direction_output(mdm_drv->usb_switch_gpio, 1);
275 } else
276 pr_err("%s usb switch gpio unavailable\n", __func__);
277 break;
278 default:
279 pr_err("%s invalid upgrade type\n", __func__);
280 }
281}
Joel Kinge9cd5272012-01-28 12:48:59 -0800282static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700283 .power_on_mdm_cb = mdm_power_on_common,
284 .reset_mdm_cb = mdm_power_on_common,
Joel Kinga7a7b9a2012-06-28 13:35:28 -0700285 .atomic_reset_mdm_cb = mdm_atomic_soft_reset,
Joel Kinge92eb872012-05-06 09:30:24 -0700286 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800287 .debug_state_changed_cb = debug_state_changed,
288 .status_cb = mdm_status_changed,
Ameya Thakur43248fd2012-07-10 18:50:52 -0700289 .image_upgrade_cb = mdm_image_upgrade,
Joel Kinge9cd5272012-01-28 12:48:59 -0800290};
291
Joel Kingb6f0f612011-11-01 16:59:14 -0700292static int __init mdm_modem_probe(struct platform_device *pdev)
293{
Joel Kingb6f0f612011-11-01 16:59:14 -0700294 return mdm_common_create(pdev, &mdm_cb);
295}
296
297static int __devexit mdm_modem_remove(struct platform_device *pdev)
298{
299 return mdm_common_modem_remove(pdev);
300}
301
302static void mdm_modem_shutdown(struct platform_device *pdev)
303{
304 mdm_common_modem_shutdown(pdev);
305}
306
307static struct platform_driver mdm_modem_driver = {
308 .remove = mdm_modem_remove,
309 .shutdown = mdm_modem_shutdown,
310 .driver = {
311 .name = "mdm2_modem",
312 .owner = THIS_MODULE
313 },
314};
315
316static int __init mdm_modem_init(void)
317{
318 return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe);
319}
320
321static void __exit mdm_modem_exit(void)
322{
323 platform_driver_unregister(&mdm_modem_driver);
324}
325
326module_init(mdm_modem_init);
327module_exit(mdm_modem_exit);
328
329MODULE_LICENSE("GPL v2");
330MODULE_DESCRIPTION("mdm modem driver");
331MODULE_VERSION("2.0");
332MODULE_ALIAS("mdm_modem");