blob: 43c85bb5295a0965d7f6a16c3678db9584dee677 [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"
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 King3da02a92012-03-20 10:55:52 -070047static int power_on_count;
Hemant Kumarf1ca9192012-02-07 18:59:33 -080048static int hsic_peripheral_status;
Joel Kingd8052c02012-02-03 12:33:31 -080049static DEFINE_MUTEX(hsic_status_lock);
50
51static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv)
52{
Joel Kinge92eb872012-05-06 09:30:24 -070053 if (!mdm_drv->pdata->peripheral_platform_device)
54 return;
55
Joel Kingd8052c02012-02-03 12:33:31 -080056 mutex_lock(&hsic_status_lock);
57 if (hsic_peripheral_status)
58 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070059 platform_device_add(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080060 hsic_peripheral_status = 1;
61out:
62 mutex_unlock(&hsic_status_lock);
63}
64
65static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv)
66{
Joel Kinge92eb872012-05-06 09:30:24 -070067 if (!mdm_drv->pdata->peripheral_platform_device)
68 return;
69
Joel Kingd8052c02012-02-03 12:33:31 -080070 mutex_lock(&hsic_status_lock);
71 if (!hsic_peripheral_status)
72 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070073 platform_device_del(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080074 hsic_peripheral_status = 0;
75out:
76 mutex_unlock(&hsic_status_lock);
77}
Joel Kingb6f0f612011-11-01 16:59:14 -070078
Joel Kinga7a7b9a2012-06-28 13:35:28 -070079/* This function can be called from atomic context. */
Joel King733377c2012-06-20 13:07:38 -070080static void mdm_toggle_soft_reset(struct mdm_modem_drv *mdm_drv)
81{
82 int soft_reset_direction_assert = 0,
83 soft_reset_direction_de_assert = 1;
84
85 if (mdm_drv->pdata->soft_reset_inverted) {
86 soft_reset_direction_assert = 1;
87 soft_reset_direction_de_assert = 0;
88 }
89 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
90 soft_reset_direction_assert);
Joel Kinga7a7b9a2012-06-28 13:35:28 -070091 /* Use mdelay because this function can be called from atomic
92 * context.
93 */
94 mdelay(10);
Joel King733377c2012-06-20 13:07:38 -070095 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
96 soft_reset_direction_de_assert);
97}
98
Joel Kinga7a7b9a2012-06-28 13:35:28 -070099/* This function can be called from atomic context. */
100static void mdm_atomic_soft_reset(struct mdm_modem_drv *mdm_drv)
101{
102 mdm_toggle_soft_reset(mdm_drv);
103}
104
Joel Kinge92eb872012-05-06 09:30:24 -0700105static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv)
106{
Joel King733377c2012-06-20 13:07:38 -0700107 int i;
Joel Kinge92eb872012-05-06 09:30:24 -0700108 int soft_reset_direction =
109 mdm_drv->pdata->soft_reset_inverted ? 1 : 0;
110
Hemant Kumar069f6432012-08-22 18:30:19 -0700111 mdm_peripheral_disconnect(mdm_drv);
112
Joel King733377c2012-06-20 13:07:38 -0700113 /* Wait for the modem to complete its power down actions. */
114 for (i = 20; i > 0; i--) {
115 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0)
116 break;
117 msleep(100);
118 }
119 if (i == 0) {
120 pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n",
121 __func__);
122 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
123 soft_reset_direction);
124 /*
125 * Currently, there is a debounce timer on the charm PMIC. It is
126 * necessary to hold the PMIC RESET low for ~3.5 seconds
127 * for the reset to fully take place. Sleep here to ensure the
128 * reset has occured before the function exits.
129 */
130 msleep(4000);
131 }
Joel Kinge92eb872012-05-06 09:30:24 -0700132}
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 if (power_on_count != 1) {
139 pr_err("%s: Calling fn when power_on_count != 1\n",
140 __func__);
141 return;
142 }
143
144 pr_err("%s: Powering on modem for the first time\n", __func__);
145 mdm_peripheral_disconnect(mdm_drv);
146
Joel King733377c2012-06-20 13:07:38 -0700147 /* If this is the first power-up after a panic, the modem may still
148 * be in a power-on state, in which case we need to toggle the gpio
149 * instead of just de-asserting it. No harm done if the modem was
150 * powered down.
151 */
152 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700153 /* If the device has a kpd pwr gpio then toggle it. */
Ameya Thakure155ece2012-07-09 12:08:37 -0700154 if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio)) {
Joel Kinge92eb872012-05-06 09:30:24 -0700155 /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
156 * then pull it back low.
157 */
158 pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__);
159 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
160 msleep(1000);
161 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
162 }
163
Ameya Thakure155ece2012-07-09 12:08:37 -0700164 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700165 goto start_mdm_peripheral;
Joel Kinge92eb872012-05-06 09:30:24 -0700166
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700167 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
168 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
169 if (pblrdy)
170 break;
171 usleep_range(5000, 5000);
172 }
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700173 pr_debug("%s: i:%d\n", __func__, i);
174
175start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700176 mdm_peripheral_connect(mdm_drv);
177 msleep(200);
178}
179
180static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
181{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700182 int i;
183 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700184
Joel Kinge92eb872012-05-06 09:30:24 -0700185 pr_err("%s: soft resetting mdm modem\n", __func__);
Joel Kinge92eb872012-05-06 09:30:24 -0700186 mdm_peripheral_disconnect(mdm_drv);
Joel King733377c2012-06-20 13:07:38 -0700187 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700188
Ameya Thakure155ece2012-07-09 12:08:37 -0700189 if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700190 goto start_mdm_peripheral;
191
192 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
193 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
194 if (pblrdy)
195 break;
196 usleep_range(5000, 5000);
197 }
198
199 pr_debug("%s: i:%d\n", __func__, i);
200
201start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700202 mdm_peripheral_connect(mdm_drv);
203 msleep(200);
204}
205
206static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700207{
Joel King3da02a92012-03-20 10:55:52 -0700208 power_on_count++;
209
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700210 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700211 * de-assert it now so that it can be asserted later.
212 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700213 */
Ameya Thakure155ece2012-07-09 12:08:37 -0700214 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700215 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700216
Joel Kinge92eb872012-05-06 09:30:24 -0700217 /*
218 * If we did an "early power on" then ignore the very next
219 * power-on request because it would the be first request from
220 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700221 */
Joel Kinge92eb872012-05-06 09:30:24 -0700222 if (mdm_drv->pdata->early_power_on &&
223 (power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700224 return;
225
Joel Kinge92eb872012-05-06 09:30:24 -0700226 if (power_on_count == 1)
227 mdm_do_first_power_on(mdm_drv);
228 else
229 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700230}
231
Joel Kingb6f0f612011-11-01 16:59:14 -0700232static void debug_state_changed(int value)
233{
Joel King96c96dc2012-07-30 09:06:15 -0700234 mdm_debug_mask = value;
Joel Kingb6f0f612011-11-01 16:59:14 -0700235}
236
Joel Kingd8052c02012-02-03 12:33:31 -0800237static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800238{
Joel King2a42f502012-02-03 11:36:25 -0800239 pr_debug("%s: value:%d\n", __func__, value);
Vamsi Krishna33925632011-12-13 15:43:09 -0800240
241 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800242 mdm_peripheral_disconnect(mdm_drv);
243 mdm_peripheral_connect(mdm_drv);
Ameya Thakure155ece2012-07-09 12:08:37 -0700244 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700245 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800246 }
247}
248
Ameya Thakur43248fd2012-07-10 18:50:52 -0700249static void mdm_image_upgrade(struct mdm_modem_drv *mdm_drv, int type)
250{
251 switch (type) {
252 case APQ_CONTROLLED_UPGRADE:
253 pr_debug("%s APQ controlled modem image upgrade\n", __func__);
254 mdm_drv->mdm_ready = 0;
255 mdm_toggle_soft_reset(mdm_drv);
256 break;
257 case MDM_CONTROLLED_UPGRADE:
258 pr_debug("%s MDM controlled modem image upgrade\n", __func__);
259 mdm_drv->mdm_ready = 0;
260 /*
261 * If we have no image currently present on the modem, then we
262 * would be in PBL, in which case the status gpio would not go
263 * high.
264 */
265 mdm_drv->disable_status_check = 1;
Ameya Thakure155ece2012-07-09 12:08:37 -0700266 if (GPIO_IS_VALID(mdm_drv->usb_switch_gpio)) {
Ameya Thakur43248fd2012-07-10 18:50:52 -0700267 pr_info("%s Switching usb control to MDM\n", __func__);
268 gpio_direction_output(mdm_drv->usb_switch_gpio, 1);
269 } else
270 pr_err("%s usb switch gpio unavailable\n", __func__);
271 break;
272 default:
273 pr_err("%s invalid upgrade type\n", __func__);
274 }
275}
Joel Kinge9cd5272012-01-28 12:48:59 -0800276static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700277 .power_on_mdm_cb = mdm_power_on_common,
278 .reset_mdm_cb = mdm_power_on_common,
Joel Kinga7a7b9a2012-06-28 13:35:28 -0700279 .atomic_reset_mdm_cb = mdm_atomic_soft_reset,
Joel Kinge92eb872012-05-06 09:30:24 -0700280 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800281 .debug_state_changed_cb = debug_state_changed,
282 .status_cb = mdm_status_changed,
Ameya Thakur43248fd2012-07-10 18:50:52 -0700283 .image_upgrade_cb = mdm_image_upgrade,
Joel Kinge9cd5272012-01-28 12:48:59 -0800284};
285
Joel Kingb6f0f612011-11-01 16:59:14 -0700286static int __init mdm_modem_probe(struct platform_device *pdev)
287{
Joel Kingb6f0f612011-11-01 16:59:14 -0700288 return mdm_common_create(pdev, &mdm_cb);
289}
290
291static int __devexit mdm_modem_remove(struct platform_device *pdev)
292{
293 return mdm_common_modem_remove(pdev);
294}
295
296static void mdm_modem_shutdown(struct platform_device *pdev)
297{
298 mdm_common_modem_shutdown(pdev);
299}
300
301static struct platform_driver mdm_modem_driver = {
302 .remove = mdm_modem_remove,
303 .shutdown = mdm_modem_shutdown,
304 .driver = {
305 .name = "mdm2_modem",
306 .owner = THIS_MODULE
307 },
308};
309
310static int __init mdm_modem_init(void)
311{
312 return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe);
313}
314
315static void __exit mdm_modem_exit(void)
316{
317 platform_driver_unregister(&mdm_modem_driver);
318}
319
320module_init(mdm_modem_init);
321module_exit(mdm_modem_exit);
322
323MODULE_LICENSE("GPL v2");
324MODULE_DESCRIPTION("mdm modem driver");
325MODULE_VERSION("2.0");
326MODULE_ALIAS("mdm_modem");