blob: 47919559a75296153989f453a5295c33e3c6e6bf [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
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -070048#define MDM_PBLRDY_CNT 20
Joel Kingb6f0f612011-11-01 16:59:14 -070049
50static int mdm_debug_on;
Joel King3da02a92012-03-20 10:55:52 -070051static int power_on_count;
Hemant Kumarf1ca9192012-02-07 18:59:33 -080052static int hsic_peripheral_status;
Joel Kingd8052c02012-02-03 12:33:31 -080053static DEFINE_MUTEX(hsic_status_lock);
54
55static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv)
56{
Joel Kinge92eb872012-05-06 09:30:24 -070057 if (!mdm_drv->pdata->peripheral_platform_device)
58 return;
59
Joel Kingd8052c02012-02-03 12:33:31 -080060 mutex_lock(&hsic_status_lock);
61 if (hsic_peripheral_status)
62 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070063 platform_device_add(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080064 hsic_peripheral_status = 1;
65out:
66 mutex_unlock(&hsic_status_lock);
67}
68
69static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv)
70{
Joel Kinge92eb872012-05-06 09:30:24 -070071 if (!mdm_drv->pdata->peripheral_platform_device)
72 return;
73
Joel Kingd8052c02012-02-03 12:33:31 -080074 mutex_lock(&hsic_status_lock);
75 if (!hsic_peripheral_status)
76 goto out;
Joel Kinge92eb872012-05-06 09:30:24 -070077 platform_device_del(mdm_drv->pdata->peripheral_platform_device);
Joel Kingd8052c02012-02-03 12:33:31 -080078 hsic_peripheral_status = 0;
79out:
80 mutex_unlock(&hsic_status_lock);
81}
Joel Kingb6f0f612011-11-01 16:59:14 -070082
Joel Kinge92eb872012-05-06 09:30:24 -070083static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv)
84{
85 int soft_reset_direction =
86 mdm_drv->pdata->soft_reset_inverted ? 1 : 0;
87
88 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
89 soft_reset_direction);
90 mdm_peripheral_disconnect(mdm_drv);
91}
92
93static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
94{
95 int soft_reset_direction =
96 mdm_drv->pdata->soft_reset_inverted ? 0 : 1;
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -070097 int i;
98 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -070099
100 if (power_on_count != 1) {
101 pr_err("%s: Calling fn when power_on_count != 1\n",
102 __func__);
103 return;
104 }
105
106 pr_err("%s: Powering on modem for the first time\n", __func__);
107 mdm_peripheral_disconnect(mdm_drv);
108
109 /* If the device has a kpd pwr gpio then toggle it. */
110 if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) {
111 /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
112 * then pull it back low.
113 */
114 pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__);
115 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
116 msleep(1000);
117 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
118 }
119
120 /* De-assert the soft reset line. */
121 pr_debug("%s: De-asserting soft reset gpio\n", __func__);
122 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
123 soft_reset_direction);
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700124 if (!mdm_drv->mdm2ap_pblrdy)
125 goto start_mdm_peripheral;
Joel Kinge92eb872012-05-06 09:30:24 -0700126
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700127 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
128 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
129 if (pblrdy)
130 break;
131 usleep_range(5000, 5000);
132 }
133
134 pr_debug("%s: i:%d\n", __func__, i);
135
136start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700137 mdm_peripheral_connect(mdm_drv);
138 msleep(200);
139}
140
141static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
142{
143 int soft_reset_direction =
144 mdm_drv->pdata->soft_reset_inverted ? 0 : 1;
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700145 int i;
146 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700147
148 /* De-assert the soft reset line. */
149 pr_err("%s: soft resetting mdm modem\n", __func__);
150
151 mdm_peripheral_disconnect(mdm_drv);
152
153 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
154 soft_reset_direction == 1 ? 0 : 1);
155 usleep_range(5000, 10000);
156 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
157 soft_reset_direction == 1 ? 1 : 0);
158
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700159 if (!mdm_drv->mdm2ap_pblrdy)
160 goto start_mdm_peripheral;
161
162 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
163 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
164 if (pblrdy)
165 break;
166 usleep_range(5000, 5000);
167 }
168
169 pr_debug("%s: i:%d\n", __func__, i);
170
171start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700172 mdm_peripheral_connect(mdm_drv);
173 msleep(200);
174}
175
176static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700177{
Joel King3da02a92012-03-20 10:55:52 -0700178 power_on_count++;
179
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700180 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700181 * de-assert it now so that it can be asserted later.
182 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700183 */
Joel Kinge92eb872012-05-06 09:30:24 -0700184 if (mdm_drv->ap2mdm_wakeup_gpio > 0)
185 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700186
Joel Kinge92eb872012-05-06 09:30:24 -0700187 /*
188 * If we did an "early power on" then ignore the very next
189 * power-on request because it would the be first request from
190 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700191 */
Joel Kinge92eb872012-05-06 09:30:24 -0700192 if (mdm_drv->pdata->early_power_on &&
193 (power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700194 return;
195
Joel Kinge92eb872012-05-06 09:30:24 -0700196 if (power_on_count == 1)
197 mdm_do_first_power_on(mdm_drv);
198 else
199 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700200}
201
Joel Kingb6f0f612011-11-01 16:59:14 -0700202static void debug_state_changed(int value)
203{
204 mdm_debug_on = value;
205}
206
Joel Kingd8052c02012-02-03 12:33:31 -0800207static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800208{
Joel King2a42f502012-02-03 11:36:25 -0800209 pr_debug("%s: value:%d\n", __func__, value);
Vamsi Krishna33925632011-12-13 15:43:09 -0800210
211 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800212 mdm_peripheral_disconnect(mdm_drv);
213 mdm_peripheral_connect(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700214 if (mdm_drv->ap2mdm_wakeup_gpio > 0)
215 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800216 }
217}
218
Joel Kinge9cd5272012-01-28 12:48:59 -0800219static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700220 .power_on_mdm_cb = mdm_power_on_common,
221 .reset_mdm_cb = mdm_power_on_common,
222 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800223 .debug_state_changed_cb = debug_state_changed,
224 .status_cb = mdm_status_changed,
225};
226
Joel Kingb6f0f612011-11-01 16:59:14 -0700227static int __init mdm_modem_probe(struct platform_device *pdev)
228{
Joel Kingb6f0f612011-11-01 16:59:14 -0700229 return mdm_common_create(pdev, &mdm_cb);
230}
231
232static int __devexit mdm_modem_remove(struct platform_device *pdev)
233{
234 return mdm_common_modem_remove(pdev);
235}
236
237static void mdm_modem_shutdown(struct platform_device *pdev)
238{
239 mdm_common_modem_shutdown(pdev);
240}
241
242static struct platform_driver mdm_modem_driver = {
243 .remove = mdm_modem_remove,
244 .shutdown = mdm_modem_shutdown,
245 .driver = {
246 .name = "mdm2_modem",
247 .owner = THIS_MODULE
248 },
249};
250
251static int __init mdm_modem_init(void)
252{
253 return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe);
254}
255
256static void __exit mdm_modem_exit(void)
257{
258 platform_driver_unregister(&mdm_modem_driver);
259}
260
261module_init(mdm_modem_init);
262module_exit(mdm_modem_exit);
263
264MODULE_LICENSE("GPL v2");
265MODULE_DESCRIPTION("mdm modem driver");
266MODULE_VERSION("2.0");
267MODULE_ALIAS("mdm_modem");