blob: bd7bd9e9fc19814eca2a2906376f4b05bd4f6b89 [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
45#define MDM_MODEM_TIMEOUT 6000
46#define MDM_HOLD_TIME 4000
47#define MDM_MODEM_DELTA 100
Joel Kingb6f0f612011-11-01 16:59:14 -070048
49static int mdm_debug_on;
Joel King3da02a92012-03-20 10:55:52 -070050static int power_on_count;
Hemant Kumarf1ca9192012-02-07 18:59:33 -080051static int hsic_peripheral_status;
Joel Kingd8052c02012-02-03 12:33:31 -080052static DEFINE_MUTEX(hsic_status_lock);
53
54static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv)
55{
Joel Kinge92eb872012-05-06 09:30:24 -070056 if (!mdm_drv->pdata->peripheral_platform_device)
57 return;
58
Joel Kingd8052c02012-02-03 12:33:31 -080059 mutex_lock(&hsic_status_lock);
60 if (hsic_peripheral_status)
61 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070062 platform_device_add(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080063 hsic_peripheral_status = 1;
64out:
65 mutex_unlock(&hsic_status_lock);
66}
67
68static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv)
69{
Joel Kinge92eb872012-05-06 09:30:24 -070070 if (!mdm_drv->pdata->peripheral_platform_device)
71 return;
72
Joel Kingd8052c02012-02-03 12:33:31 -080073 mutex_lock(&hsic_status_lock);
74 if (!hsic_peripheral_status)
75 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070076 platform_device_del(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080077 hsic_peripheral_status = 0;
78out:
79 mutex_unlock(&hsic_status_lock);
80}
Joel Kingb6f0f612011-11-01 16:59:14 -070081
Joel Kinge92eb872012-05-06 09:30:24 -070082static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv)
83{
84 int soft_reset_direction =
85 mdm_drv->pdata->soft_reset_inverted ? 1 : 0;
86
87 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
88 soft_reset_direction);
89 mdm_peripheral_disconnect(mdm_drv);
90}
91
92static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
93{
94 int soft_reset_direction =
95 mdm_drv->pdata->soft_reset_inverted ? 0 : 1;
96
97 if (power_on_count != 1) {
98 pr_err("%s: Calling fn when power_on_count != 1\n",
99 __func__);
100 return;
101 }
102
103 pr_err("%s: Powering on modem for the first time\n", __func__);
104 mdm_peripheral_disconnect(mdm_drv);
105
106 /* If the device has a kpd pwr gpio then toggle it. */
107 if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) {
108 /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
109 * then pull it back low.
110 */
111 pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__);
112 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
113 msleep(1000);
114 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
115 }
116
117 /* De-assert the soft reset line. */
118 pr_debug("%s: De-asserting soft reset gpio\n", __func__);
119 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
120 soft_reset_direction);
121
122 mdm_peripheral_connect(mdm_drv);
123 msleep(200);
124}
125
126static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
127{
128 int soft_reset_direction =
129 mdm_drv->pdata->soft_reset_inverted ? 0 : 1;
130
131 /* De-assert the soft reset line. */
132 pr_err("%s: soft resetting mdm modem\n", __func__);
133
134 mdm_peripheral_disconnect(mdm_drv);
135
136 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
137 soft_reset_direction == 1 ? 0 : 1);
138 usleep_range(5000, 10000);
139 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
140 soft_reset_direction == 1 ? 1 : 0);
141
142 mdm_peripheral_connect(mdm_drv);
143 msleep(200);
144}
145
146static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700147{
Joel King3da02a92012-03-20 10:55:52 -0700148 power_on_count++;
149
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700150 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700151 * de-assert it now so that it can be asserted later.
152 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700153 */
Joel Kinge92eb872012-05-06 09:30:24 -0700154 if (mdm_drv->ap2mdm_wakeup_gpio > 0)
155 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700156
Joel Kinge92eb872012-05-06 09:30:24 -0700157 /*
158 * If we did an "early power on" then ignore the very next
159 * power-on request because it would the be first request from
160 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700161 */
Joel Kinge92eb872012-05-06 09:30:24 -0700162 if (mdm_drv->pdata->early_power_on &&
163 (power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700164 return;
165
Joel Kinge92eb872012-05-06 09:30:24 -0700166 if (power_on_count == 1)
167 mdm_do_first_power_on(mdm_drv);
168 else
169 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700170}
171
Joel Kingb6f0f612011-11-01 16:59:14 -0700172static void debug_state_changed(int value)
173{
174 mdm_debug_on = value;
175}
176
Joel Kingd8052c02012-02-03 12:33:31 -0800177static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800178{
Joel King2a42f502012-02-03 11:36:25 -0800179 pr_debug("%s: value:%d\n", __func__, value);
Vamsi Krishna33925632011-12-13 15:43:09 -0800180
181 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800182 mdm_peripheral_disconnect(mdm_drv);
183 mdm_peripheral_connect(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700184 if (mdm_drv->ap2mdm_wakeup_gpio > 0)
185 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800186 }
187}
188
Joel Kinge9cd5272012-01-28 12:48:59 -0800189static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700190 .power_on_mdm_cb = mdm_power_on_common,
191 .reset_mdm_cb = mdm_power_on_common,
192 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800193 .debug_state_changed_cb = debug_state_changed,
194 .status_cb = mdm_status_changed,
195};
196
Joel Kingb6f0f612011-11-01 16:59:14 -0700197static int __init mdm_modem_probe(struct platform_device *pdev)
198{
Joel Kingb6f0f612011-11-01 16:59:14 -0700199 return mdm_common_create(pdev, &mdm_cb);
200}
201
202static int __devexit mdm_modem_remove(struct platform_device *pdev)
203{
204 return mdm_common_modem_remove(pdev);
205}
206
207static void mdm_modem_shutdown(struct platform_device *pdev)
208{
209 mdm_common_modem_shutdown(pdev);
210}
211
212static struct platform_driver mdm_modem_driver = {
213 .remove = mdm_modem_remove,
214 .shutdown = mdm_modem_shutdown,
215 .driver = {
216 .name = "mdm2_modem",
217 .owner = THIS_MODULE
218 },
219};
220
221static int __init mdm_modem_init(void)
222{
223 return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe);
224}
225
226static void __exit mdm_modem_exit(void)
227{
228 platform_driver_unregister(&mdm_modem_driver);
229}
230
231module_init(mdm_modem_init);
232module_exit(mdm_modem_exit);
233
234MODULE_LICENSE("GPL v2");
235MODULE_DESCRIPTION("mdm modem driver");
236MODULE_VERSION("2.0");
237MODULE_ALIAS("mdm_modem");