blob: e74af2ebfef9086d424ed68c9ef696d3daaa50d4 [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>
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
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -070045#define MDM_PBLRDY_CNT 20
Joel Kingb6f0f612011-11-01 16:59:14 -070046
47static int mdm_debug_on;
Joel King3da02a92012-03-20 10:55:52 -070048static int power_on_count;
Hemant Kumarf1ca9192012-02-07 18:59:33 -080049static int hsic_peripheral_status;
Joel Kingd8052c02012-02-03 12:33:31 -080050static DEFINE_MUTEX(hsic_status_lock);
51
52static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv)
53{
Joel Kinge92eb872012-05-06 09:30:24 -070054 if (!mdm_drv->pdata->peripheral_platform_device)
55 return;
56
Joel Kingd8052c02012-02-03 12:33:31 -080057 mutex_lock(&hsic_status_lock);
58 if (hsic_peripheral_status)
59 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070060 platform_device_add(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080061 hsic_peripheral_status = 1;
62out:
63 mutex_unlock(&hsic_status_lock);
64}
65
66static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv)
67{
Joel Kinge92eb872012-05-06 09:30:24 -070068 if (!mdm_drv->pdata->peripheral_platform_device)
69 return;
70
Joel Kingd8052c02012-02-03 12:33:31 -080071 mutex_lock(&hsic_status_lock);
72 if (!hsic_peripheral_status)
73 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070074 platform_device_del(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080075 hsic_peripheral_status = 0;
76out:
77 mutex_unlock(&hsic_status_lock);
78}
Joel Kingb6f0f612011-11-01 16:59:14 -070079
Joel Kinga7a7b9a2012-06-28 13:35:28 -070080/* This function can be called from atomic context. */
Joel King733377c2012-06-20 13:07:38 -070081static void mdm_toggle_soft_reset(struct mdm_modem_drv *mdm_drv)
82{
83 int soft_reset_direction_assert = 0,
84 soft_reset_direction_de_assert = 1;
85
86 if (mdm_drv->pdata->soft_reset_inverted) {
87 soft_reset_direction_assert = 1;
88 soft_reset_direction_de_assert = 0;
89 }
90 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
91 soft_reset_direction_assert);
Joel Kinga7a7b9a2012-06-28 13:35:28 -070092 /* Use mdelay because this function can be called from atomic
93 * context.
94 */
95 mdelay(10);
Joel King733377c2012-06-20 13:07:38 -070096 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
97 soft_reset_direction_de_assert);
98}
99
Joel Kinga7a7b9a2012-06-28 13:35:28 -0700100/* This function can be called from atomic context. */
101static void mdm_atomic_soft_reset(struct mdm_modem_drv *mdm_drv)
102{
103 mdm_toggle_soft_reset(mdm_drv);
104}
105
Joel Kinge92eb872012-05-06 09:30:24 -0700106static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv)
107{
Joel King733377c2012-06-20 13:07:38 -0700108 int i;
Joel Kinge92eb872012-05-06 09:30:24 -0700109 int soft_reset_direction =
110 mdm_drv->pdata->soft_reset_inverted ? 1 : 0;
111
Joel King733377c2012-06-20 13:07:38 -0700112 /* Wait for the modem to complete its power down actions. */
113 for (i = 20; i > 0; i--) {
114 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0)
115 break;
116 msleep(100);
117 }
118 if (i == 0) {
119 pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n",
120 __func__);
121 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
122 soft_reset_direction);
123 /*
124 * Currently, there is a debounce timer on the charm PMIC. It is
125 * necessary to hold the PMIC RESET low for ~3.5 seconds
126 * for the reset to fully take place. Sleep here to ensure the
127 * reset has occured before the function exits.
128 */
129 msleep(4000);
130 }
Joel Kinge92eb872012-05-06 09:30:24 -0700131 mdm_peripheral_disconnect(mdm_drv);
132}
133
134static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
135{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700136 int i;
137 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700138
139 if (power_on_count != 1) {
140 pr_err("%s: Calling fn when power_on_count != 1\n",
141 __func__);
142 return;
143 }
144
145 pr_err("%s: Powering on modem for the first time\n", __func__);
146 mdm_peripheral_disconnect(mdm_drv);
147
Joel King733377c2012-06-20 13:07:38 -0700148 /* If this is the first power-up after a panic, the modem may still
149 * be in a power-on state, in which case we need to toggle the gpio
150 * instead of just de-asserting it. No harm done if the modem was
151 * powered down.
152 */
153 mdm_toggle_soft_reset(mdm_drv);
154
Joel Kinge92eb872012-05-06 09:30:24 -0700155 /* If the device has a kpd pwr gpio then toggle it. */
156 if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) {
157 /* 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
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700166 if (!mdm_drv->mdm2ap_pblrdy)
167 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 }
175
176 pr_debug("%s: i:%d\n", __func__, i);
177
178start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700179 mdm_peripheral_connect(mdm_drv);
180 msleep(200);
181}
182
183static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
184{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700185 int i;
186 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700187
Joel Kinge92eb872012-05-06 09:30:24 -0700188 pr_err("%s: soft resetting mdm modem\n", __func__);
Joel Kinge92eb872012-05-06 09:30:24 -0700189 mdm_peripheral_disconnect(mdm_drv);
Joel King733377c2012-06-20 13:07:38 -0700190 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700191
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700192 if (!mdm_drv->mdm2ap_pblrdy)
193 goto start_mdm_peripheral;
194
195 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
196 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
197 if (pblrdy)
198 break;
199 usleep_range(5000, 5000);
200 }
201
202 pr_debug("%s: i:%d\n", __func__, i);
203
204start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700205 mdm_peripheral_connect(mdm_drv);
206 msleep(200);
207}
208
209static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700210{
Joel King3da02a92012-03-20 10:55:52 -0700211 power_on_count++;
212
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700213 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700214 * de-assert it now so that it can be asserted later.
215 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700216 */
Joel Kinge92eb872012-05-06 09:30:24 -0700217 if (mdm_drv->ap2mdm_wakeup_gpio > 0)
218 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700219
Joel Kinge92eb872012-05-06 09:30:24 -0700220 /*
221 * If we did an "early power on" then ignore the very next
222 * power-on request because it would the be first request from
223 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700224 */
Joel Kinge92eb872012-05-06 09:30:24 -0700225 if (mdm_drv->pdata->early_power_on &&
226 (power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700227 return;
228
Joel Kinge92eb872012-05-06 09:30:24 -0700229 if (power_on_count == 1)
230 mdm_do_first_power_on(mdm_drv);
231 else
232 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700233}
234
Joel Kingb6f0f612011-11-01 16:59:14 -0700235static void debug_state_changed(int value)
236{
237 mdm_debug_on = value;
238}
239
Joel Kingd8052c02012-02-03 12:33:31 -0800240static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800241{
Joel King2a42f502012-02-03 11:36:25 -0800242 pr_debug("%s: value:%d\n", __func__, value);
Vamsi Krishna33925632011-12-13 15:43:09 -0800243
244 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800245 mdm_peripheral_disconnect(mdm_drv);
246 mdm_peripheral_connect(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700247 if (mdm_drv->ap2mdm_wakeup_gpio > 0)
248 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800249 }
250}
251
Ameya Thakur43248fd2012-07-10 18:50:52 -0700252static void mdm_image_upgrade(struct mdm_modem_drv *mdm_drv, int type)
253{
254 switch (type) {
255 case APQ_CONTROLLED_UPGRADE:
256 pr_debug("%s APQ controlled modem image upgrade\n", __func__);
257 mdm_drv->mdm_ready = 0;
258 mdm_toggle_soft_reset(mdm_drv);
259 break;
260 case MDM_CONTROLLED_UPGRADE:
261 pr_debug("%s MDM controlled modem image upgrade\n", __func__);
262 mdm_drv->mdm_ready = 0;
263 /*
264 * If we have no image currently present on the modem, then we
265 * would be in PBL, in which case the status gpio would not go
266 * high.
267 */
268 mdm_drv->disable_status_check = 1;
269 if (mdm_drv->usb_switch_gpio > 0) {
270 pr_info("%s Switching usb control to MDM\n", __func__);
271 gpio_direction_output(mdm_drv->usb_switch_gpio, 1);
272 } else
273 pr_err("%s usb switch gpio unavailable\n", __func__);
274 break;
275 default:
276 pr_err("%s invalid upgrade type\n", __func__);
277 }
278}
Joel Kinge9cd5272012-01-28 12:48:59 -0800279static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700280 .power_on_mdm_cb = mdm_power_on_common,
281 .reset_mdm_cb = mdm_power_on_common,
Joel Kinga7a7b9a2012-06-28 13:35:28 -0700282 .atomic_reset_mdm_cb = mdm_atomic_soft_reset,
Joel Kinge92eb872012-05-06 09:30:24 -0700283 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800284 .debug_state_changed_cb = debug_state_changed,
285 .status_cb = mdm_status_changed,
Ameya Thakur43248fd2012-07-10 18:50:52 -0700286 .image_upgrade_cb = mdm_image_upgrade,
Joel Kinge9cd5272012-01-28 12:48:59 -0800287};
288
Joel Kingb6f0f612011-11-01 16:59:14 -0700289static int __init mdm_modem_probe(struct platform_device *pdev)
290{
Joel Kingb6f0f612011-11-01 16:59:14 -0700291 return mdm_common_create(pdev, &mdm_cb);
292}
293
294static int __devexit mdm_modem_remove(struct platform_device *pdev)
295{
296 return mdm_common_modem_remove(pdev);
297}
298
299static void mdm_modem_shutdown(struct platform_device *pdev)
300{
301 mdm_common_modem_shutdown(pdev);
302}
303
304static struct platform_driver mdm_modem_driver = {
305 .remove = mdm_modem_remove,
306 .shutdown = mdm_modem_shutdown,
307 .driver = {
308 .name = "mdm2_modem",
309 .owner = THIS_MODULE
310 },
311};
312
313static int __init mdm_modem_init(void)
314{
315 return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe);
316}
317
318static void __exit mdm_modem_exit(void)
319{
320 platform_driver_unregister(&mdm_modem_driver);
321}
322
323module_init(mdm_modem_init);
324module_exit(mdm_modem_exit);
325
326MODULE_LICENSE("GPL v2");
327MODULE_DESCRIPTION("mdm modem driver");
328MODULE_VERSION("2.0");
329MODULE_ALIAS("mdm_modem");