blob: 77eeb530ff37a460b9cf7d244e0bef466ecccdd5 [file] [log] [blame]
Joel Kinge9cd5272012-01-28 12:48:59 -08001/* Copyright (c) 2011-2012, Code Aurora Forum. 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 }
121 if (i == 0) {
122 pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n",
123 __func__);
124 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
125 soft_reset_direction);
126 /*
127 * Currently, there is a debounce timer on the charm PMIC. It is
128 * necessary to hold the PMIC RESET low for ~3.5 seconds
129 * for the reset to fully take place. Sleep here to ensure the
130 * reset has occured before the function exits.
131 */
132 msleep(4000);
133 }
Joel Kinge92eb872012-05-06 09:30:24 -0700134}
135
136static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
137{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700138 int i;
139 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700140 if (power_on_count != 1) {
141 pr_err("%s: Calling fn when power_on_count != 1\n",
142 __func__);
143 return;
144 }
145
146 pr_err("%s: Powering on modem for the first time\n", __func__);
147 mdm_peripheral_disconnect(mdm_drv);
148
Joel King733377c2012-06-20 13:07:38 -0700149 /* If this is the first power-up after a panic, the modem may still
150 * be in a power-on state, in which case we need to toggle the gpio
151 * instead of just de-asserting it. No harm done if the modem was
152 * powered down.
153 */
154 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700155 /* If the device has a kpd pwr gpio then toggle it. */
Ameya Thakure155ece2012-07-09 12:08:37 -0700156 if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio)) {
Joel Kinge92eb872012-05-06 09:30:24 -0700157 /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
158 * then pull it back low.
159 */
160 pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__);
161 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
162 msleep(1000);
163 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
164 }
165
Ameya Thakure155ece2012-07-09 12:08:37 -0700166 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700167 goto start_mdm_peripheral;
Joel Kinge92eb872012-05-06 09:30:24 -0700168
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700169 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
170 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
171 if (pblrdy)
172 break;
173 usleep_range(5000, 5000);
174 }
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700175 pr_debug("%s: i:%d\n", __func__, i);
176
177start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700178 mdm_peripheral_connect(mdm_drv);
179 msleep(200);
180}
181
182static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
183{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700184 int i;
185 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700186
Joel Kinge92eb872012-05-06 09:30:24 -0700187 pr_err("%s: soft resetting mdm modem\n", __func__);
Joel Kinge92eb872012-05-06 09:30:24 -0700188 mdm_peripheral_disconnect(mdm_drv);
Joel King733377c2012-06-20 13:07:38 -0700189 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700190
Ameya Thakure155ece2012-07-09 12:08:37 -0700191 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700192 goto start_mdm_peripheral;
193
194 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
195 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
196 if (pblrdy)
197 break;
198 usleep_range(5000, 5000);
199 }
200
201 pr_debug("%s: i:%d\n", __func__, i);
202
203start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700204 mdm_peripheral_connect(mdm_drv);
205 msleep(200);
206}
207
208static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700209{
Joel King3da02a92012-03-20 10:55:52 -0700210 power_on_count++;
211
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700212 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700213 * de-assert it now so that it can be asserted later.
214 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700215 */
Ameya Thakure155ece2012-07-09 12:08:37 -0700216 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700217 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700218
Joel Kinge92eb872012-05-06 09:30:24 -0700219 /*
220 * If we did an "early power on" then ignore the very next
221 * power-on request because it would the be first request from
222 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700223 */
Joel Kinge92eb872012-05-06 09:30:24 -0700224 if (mdm_drv->pdata->early_power_on &&
225 (power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700226 return;
227
Joel Kinge92eb872012-05-06 09:30:24 -0700228 if (power_on_count == 1)
229 mdm_do_first_power_on(mdm_drv);
230 else
231 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700232}
233
Joel Kingb6f0f612011-11-01 16:59:14 -0700234static void debug_state_changed(int value)
235{
Joel King96c96dc2012-07-30 09:06:15 -0700236 mdm_debug_mask = value;
Joel Kingb6f0f612011-11-01 16:59:14 -0700237}
238
Joel Kingd8052c02012-02-03 12:33:31 -0800239static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800240{
Joel King2a42f502012-02-03 11:36:25 -0800241 pr_debug("%s: value:%d\n", __func__, value);
Vamsi Krishna33925632011-12-13 15:43:09 -0800242
243 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800244 mdm_peripheral_disconnect(mdm_drv);
245 mdm_peripheral_connect(mdm_drv);
Ameya Thakure155ece2012-07-09 12:08:37 -0700246 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700247 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800248 }
249}
250
Ameya Thakur43248fd2012-07-10 18:50:52 -0700251static void mdm_image_upgrade(struct mdm_modem_drv *mdm_drv, int type)
252{
253 switch (type) {
254 case APQ_CONTROLLED_UPGRADE:
255 pr_debug("%s APQ controlled modem image upgrade\n", __func__);
256 mdm_drv->mdm_ready = 0;
257 mdm_toggle_soft_reset(mdm_drv);
258 break;
259 case MDM_CONTROLLED_UPGRADE:
260 pr_debug("%s MDM controlled modem image upgrade\n", __func__);
261 mdm_drv->mdm_ready = 0;
262 /*
263 * If we have no image currently present on the modem, then we
264 * would be in PBL, in which case the status gpio would not go
265 * high.
266 */
267 mdm_drv->disable_status_check = 1;
Ameya Thakure155ece2012-07-09 12:08:37 -0700268 if (GPIO_IS_VALID(mdm_drv->usb_switch_gpio)) {
Ameya Thakur43248fd2012-07-10 18:50:52 -0700269 pr_info("%s Switching usb control to MDM\n", __func__);
270 gpio_direction_output(mdm_drv->usb_switch_gpio, 1);
271 } else
272 pr_err("%s usb switch gpio unavailable\n", __func__);
273 break;
274 default:
275 pr_err("%s invalid upgrade type\n", __func__);
276 }
277}
Joel Kinge9cd5272012-01-28 12:48:59 -0800278static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700279 .power_on_mdm_cb = mdm_power_on_common,
280 .reset_mdm_cb = mdm_power_on_common,
Joel Kinga7a7b9a2012-06-28 13:35:28 -0700281 .atomic_reset_mdm_cb = mdm_atomic_soft_reset,
Joel Kinge92eb872012-05-06 09:30:24 -0700282 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800283 .debug_state_changed_cb = debug_state_changed,
284 .status_cb = mdm_status_changed,
Ameya Thakur43248fd2012-07-10 18:50:52 -0700285 .image_upgrade_cb = mdm_image_upgrade,
Joel Kinge9cd5272012-01-28 12:48:59 -0800286};
287
Joel Kingb6f0f612011-11-01 16:59:14 -0700288static int __init mdm_modem_probe(struct platform_device *pdev)
289{
Joel Kingb6f0f612011-11-01 16:59:14 -0700290 return mdm_common_create(pdev, &mdm_cb);
291}
292
293static int __devexit mdm_modem_remove(struct platform_device *pdev)
294{
295 return mdm_common_modem_remove(pdev);
296}
297
298static void mdm_modem_shutdown(struct platform_device *pdev)
299{
300 mdm_common_modem_shutdown(pdev);
301}
302
303static struct platform_driver mdm_modem_driver = {
304 .remove = mdm_modem_remove,
305 .shutdown = mdm_modem_shutdown,
306 .driver = {
307 .name = "mdm2_modem",
308 .owner = THIS_MODULE
309 },
310};
311
312static int __init mdm_modem_init(void)
313{
314 return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe);
315}
316
317static void __exit mdm_modem_exit(void)
318{
319 platform_driver_unregister(&mdm_modem_driver);
320}
321
322module_init(mdm_modem_init);
323module_exit(mdm_modem_exit);
324
325MODULE_LICENSE("GPL v2");
326MODULE_DESCRIPTION("mdm modem driver");
327MODULE_VERSION("2.0");
328MODULE_ALIAS("mdm_modem");