blob: f851545cc931bc0b7979b75acf46487fc0a60850 [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 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);
91 usleep_range(5000, 10000);
92 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
93 soft_reset_direction_de_assert);
94}
95
Joel Kinge92eb872012-05-06 09:30:24 -070096static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv)
97{
Joel King733377c2012-06-20 13:07:38 -070098 int i;
Joel Kinge92eb872012-05-06 09:30:24 -070099 int soft_reset_direction =
100 mdm_drv->pdata->soft_reset_inverted ? 1 : 0;
101
Joel King733377c2012-06-20 13:07:38 -0700102 /* Wait for the modem to complete its power down actions. */
103 for (i = 20; i > 0; i--) {
104 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0)
105 break;
106 msleep(100);
107 }
108 if (i == 0) {
109 pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n",
110 __func__);
111 gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
112 soft_reset_direction);
113 /*
114 * Currently, there is a debounce timer on the charm PMIC. It is
115 * necessary to hold the PMIC RESET low for ~3.5 seconds
116 * for the reset to fully take place. Sleep here to ensure the
117 * reset has occured before the function exits.
118 */
119 msleep(4000);
120 }
Joel Kinge92eb872012-05-06 09:30:24 -0700121 mdm_peripheral_disconnect(mdm_drv);
122}
123
124static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
125{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700126 int i;
127 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700128
129 if (power_on_count != 1) {
130 pr_err("%s: Calling fn when power_on_count != 1\n",
131 __func__);
132 return;
133 }
134
135 pr_err("%s: Powering on modem for the first time\n", __func__);
136 mdm_peripheral_disconnect(mdm_drv);
137
Joel King733377c2012-06-20 13:07:38 -0700138 /* If this is the first power-up after a panic, the modem may still
139 * be in a power-on state, in which case we need to toggle the gpio
140 * instead of just de-asserting it. No harm done if the modem was
141 * powered down.
142 */
143 mdm_toggle_soft_reset(mdm_drv);
144
Joel Kinge92eb872012-05-06 09:30:24 -0700145 /* If the device has a kpd pwr gpio then toggle it. */
146 if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) {
147 /* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
148 * then pull it back low.
149 */
150 pr_debug("%s: Pulling AP2MDM_KPDPWR gpio high\n", __func__);
151 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
152 msleep(1000);
153 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
154 }
155
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700156 if (!mdm_drv->mdm2ap_pblrdy)
157 goto start_mdm_peripheral;
Joel Kinge92eb872012-05-06 09:30:24 -0700158
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700159 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
160 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
161 if (pblrdy)
162 break;
163 usleep_range(5000, 5000);
164 }
165
166 pr_debug("%s: i:%d\n", __func__, i);
167
168start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700169 mdm_peripheral_connect(mdm_drv);
170 msleep(200);
171}
172
173static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
174{
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700175 int i;
176 int pblrdy;
Joel Kinge92eb872012-05-06 09:30:24 -0700177
Joel Kinge92eb872012-05-06 09:30:24 -0700178 pr_err("%s: soft resetting mdm modem\n", __func__);
Joel Kinge92eb872012-05-06 09:30:24 -0700179 mdm_peripheral_disconnect(mdm_drv);
Joel King733377c2012-06-20 13:07:38 -0700180 mdm_toggle_soft_reset(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700181
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700182 if (!mdm_drv->mdm2ap_pblrdy)
183 goto start_mdm_peripheral;
184
185 for (i = 0; i < MDM_PBLRDY_CNT; i++) {
186 pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
187 if (pblrdy)
188 break;
189 usleep_range(5000, 5000);
190 }
191
192 pr_debug("%s: i:%d\n", __func__, i);
193
194start_mdm_peripheral:
Joel Kinge92eb872012-05-06 09:30:24 -0700195 mdm_peripheral_connect(mdm_drv);
196 msleep(200);
197}
198
199static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
Joel Kingb6f0f612011-11-01 16:59:14 -0700200{
Joel King3da02a92012-03-20 10:55:52 -0700201 power_on_count++;
202
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700203 /* this gpio will be used to indicate apq readiness,
Joel Kinge92eb872012-05-06 09:30:24 -0700204 * de-assert it now so that it can be asserted later.
205 * May not be used.
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700206 */
Joel Kinge92eb872012-05-06 09:30:24 -0700207 if (mdm_drv->ap2mdm_wakeup_gpio > 0)
208 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
Vamsi Krishna9e307cd2012-04-11 13:15:36 -0700209
Joel Kinge92eb872012-05-06 09:30:24 -0700210 /*
211 * If we did an "early power on" then ignore the very next
212 * power-on request because it would the be first request from
213 * user space but we're already powered on. Ignore it.
Joel King3da02a92012-03-20 10:55:52 -0700214 */
Joel Kinge92eb872012-05-06 09:30:24 -0700215 if (mdm_drv->pdata->early_power_on &&
216 (power_on_count == 2))
Joel King3da02a92012-03-20 10:55:52 -0700217 return;
218
Joel Kinge92eb872012-05-06 09:30:24 -0700219 if (power_on_count == 1)
220 mdm_do_first_power_on(mdm_drv);
221 else
222 mdm_do_soft_power_on(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700223}
224
Joel Kingb6f0f612011-11-01 16:59:14 -0700225static void debug_state_changed(int value)
226{
227 mdm_debug_on = value;
228}
229
Joel Kingd8052c02012-02-03 12:33:31 -0800230static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800231{
Joel King2a42f502012-02-03 11:36:25 -0800232 pr_debug("%s: value:%d\n", __func__, value);
Vamsi Krishna33925632011-12-13 15:43:09 -0800233
234 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800235 mdm_peripheral_disconnect(mdm_drv);
236 mdm_peripheral_connect(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700237 if (mdm_drv->ap2mdm_wakeup_gpio > 0)
238 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
Vamsi Krishna33925632011-12-13 15:43:09 -0800239 }
240}
241
Joel Kinge9cd5272012-01-28 12:48:59 -0800242static struct mdm_ops mdm_cb = {
Joel Kinge92eb872012-05-06 09:30:24 -0700243 .power_on_mdm_cb = mdm_power_on_common,
244 .reset_mdm_cb = mdm_power_on_common,
245 .power_down_mdm_cb = mdm_power_down_common,
Joel Kinge9cd5272012-01-28 12:48:59 -0800246 .debug_state_changed_cb = debug_state_changed,
247 .status_cb = mdm_status_changed,
248};
249
Joel Kingb6f0f612011-11-01 16:59:14 -0700250static int __init mdm_modem_probe(struct platform_device *pdev)
251{
Joel Kingb6f0f612011-11-01 16:59:14 -0700252 return mdm_common_create(pdev, &mdm_cb);
253}
254
255static int __devexit mdm_modem_remove(struct platform_device *pdev)
256{
257 return mdm_common_modem_remove(pdev);
258}
259
260static void mdm_modem_shutdown(struct platform_device *pdev)
261{
262 mdm_common_modem_shutdown(pdev);
263}
264
265static struct platform_driver mdm_modem_driver = {
266 .remove = mdm_modem_remove,
267 .shutdown = mdm_modem_shutdown,
268 .driver = {
269 .name = "mdm2_modem",
270 .owner = THIS_MODULE
271 },
272};
273
274static int __init mdm_modem_init(void)
275{
276 return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe);
277}
278
279static void __exit mdm_modem_exit(void)
280{
281 platform_driver_unregister(&mdm_modem_driver);
282}
283
284module_init(mdm_modem_init);
285module_exit(mdm_modem_exit);
286
287MODULE_LICENSE("GPL v2");
288MODULE_DESCRIPTION("mdm modem driver");
289MODULE_VERSION("2.0");
290MODULE_ALIAS("mdm_modem");