blob: e0d2696446d3db18b50bb68538e054b67b1414fe [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{
56 mutex_lock(&hsic_status_lock);
57 if (hsic_peripheral_status)
58 goto out;
59 if (mdm_drv->pdata->peripheral_platform_device)
60 platform_device_add(mdm_drv->pdata->peripheral_platform_device);
61 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{
68 mutex_lock(&hsic_status_lock);
69 if (!hsic_peripheral_status)
70 goto out;
71 if (mdm_drv->pdata->peripheral_platform_device)
72 platform_device_del(mdm_drv->pdata->peripheral_platform_device);
73 hsic_peripheral_status = 0;
74out:
75 mutex_unlock(&hsic_status_lock);
76}
Joel Kingb6f0f612011-11-01 16:59:14 -070077
78static void power_on_mdm(struct mdm_modem_drv *mdm_drv)
79{
Joel King3da02a92012-03-20 10:55:52 -070080 power_on_count++;
81
82 /* The second attempt to power-on the mdm is the first attempt
83 * from user space, but we're already powered on. Ignore this.
84 * Subsequent attempts are from SSR or if something failed, in
85 * which case we must always reset the modem.
86 */
87 if (power_on_count == 2)
88 return;
89
Joel Kingd8052c02012-02-03 12:33:31 -080090 mdm_peripheral_disconnect(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -070091
Joel Kingdacbc822012-01-25 13:30:57 -080092 /* Pull RESET gpio low and wait for it to settle. */
93 pr_debug("Pulling RESET gpio low\n");
Joel Kingb6f0f612011-11-01 16:59:14 -070094 gpio_direction_output(mdm_drv->ap2mdm_pmic_reset_n_gpio, 0);
Vamsi Krishnab8666692012-02-12 18:37:39 -080095 usleep_range(5000, 10000);
Joel Kingb6f0f612011-11-01 16:59:14 -070096
Joel King3da02a92012-03-20 10:55:52 -070097 /* Deassert RESET first and wait for it to settle. */
Joel King2a42f502012-02-03 11:36:25 -080098 pr_debug("%s: Pulling RESET gpio high\n", __func__);
Joel Kingb6f0f612011-11-01 16:59:14 -070099 gpio_direction_output(mdm_drv->ap2mdm_pmic_reset_n_gpio, 1);
100 usleep(1000);
101
Joel Kingdacbc822012-01-25 13:30:57 -0800102 /* Pull PWR gpio high and wait for it to settle, but only
103 * the first time the mdm is powered up.
104 * Some targets do not use ap2mdm_kpdpwr_n_gpio.
105 */
Joel King3da02a92012-03-20 10:55:52 -0700106 if (power_on_count == 1) {
Joel Kingdacbc822012-01-25 13:30:57 -0800107 if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0) {
108 pr_debug("%s: Powering on mdm modem\n", __func__);
109 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
110 usleep(1000);
111 }
Joel Kingdacbc822012-01-25 13:30:57 -0800112 }
Joel Kingd8052c02012-02-03 12:33:31 -0800113 mdm_peripheral_connect(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700114
115 msleep(200);
116}
117
118static void power_down_mdm(struct mdm_modem_drv *mdm_drv)
119{
120 int i;
121
122 for (i = MDM_MODEM_TIMEOUT; i > 0; i -= MDM_MODEM_DELTA) {
123 pet_watchdog();
124 msleep(MDM_MODEM_DELTA);
125 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0)
126 break;
127 }
Joel Kingb6f0f612011-11-01 16:59:14 -0700128 if (i <= 0) {
129 pr_err("%s: MDM2AP_STATUS never went low.\n",
130 __func__);
131 gpio_direction_output(mdm_drv->ap2mdm_pmic_reset_n_gpio, 0);
132
133 for (i = MDM_HOLD_TIME; i > 0; i -= MDM_MODEM_DELTA) {
134 pet_watchdog();
135 msleep(MDM_MODEM_DELTA);
136 }
137 }
Joel Kingdacbc822012-01-25 13:30:57 -0800138 if (mdm_drv->ap2mdm_kpdpwr_n_gpio > 0)
139 gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
Joel Kingd8052c02012-02-03 12:33:31 -0800140 mdm_peripheral_disconnect(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700141}
142
Joel Kingb6f0f612011-11-01 16:59:14 -0700143static void debug_state_changed(int value)
144{
145 mdm_debug_on = value;
146}
147
Joel Kingd8052c02012-02-03 12:33:31 -0800148static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
Vamsi Krishna33925632011-12-13 15:43:09 -0800149{
Joel King2a42f502012-02-03 11:36:25 -0800150 pr_debug("%s: value:%d\n", __func__, value);
Vamsi Krishna33925632011-12-13 15:43:09 -0800151
152 if (value) {
Joel Kingd8052c02012-02-03 12:33:31 -0800153 mdm_peripheral_disconnect(mdm_drv);
154 mdm_peripheral_connect(mdm_drv);
Vamsi Krishna33925632011-12-13 15:43:09 -0800155 }
156}
157
Joel Kinge9cd5272012-01-28 12:48:59 -0800158static struct mdm_ops mdm_cb = {
159 .power_on_mdm_cb = power_on_mdm,
160 .power_down_mdm_cb = power_down_mdm,
161 .debug_state_changed_cb = debug_state_changed,
162 .status_cb = mdm_status_changed,
163};
164
Joel Kingb6f0f612011-11-01 16:59:14 -0700165static int __init mdm_modem_probe(struct platform_device *pdev)
166{
Joel Kingb6f0f612011-11-01 16:59:14 -0700167 return mdm_common_create(pdev, &mdm_cb);
168}
169
170static int __devexit mdm_modem_remove(struct platform_device *pdev)
171{
172 return mdm_common_modem_remove(pdev);
173}
174
175static void mdm_modem_shutdown(struct platform_device *pdev)
176{
177 mdm_common_modem_shutdown(pdev);
178}
179
180static struct platform_driver mdm_modem_driver = {
181 .remove = mdm_modem_remove,
182 .shutdown = mdm_modem_shutdown,
183 .driver = {
184 .name = "mdm2_modem",
185 .owner = THIS_MODULE
186 },
187};
188
189static int __init mdm_modem_init(void)
190{
191 return platform_driver_probe(&mdm_modem_driver, mdm_modem_probe);
192}
193
194static void __exit mdm_modem_exit(void)
195{
196 platform_driver_unregister(&mdm_modem_driver);
197}
198
199module_init(mdm_modem_init);
200module_exit(mdm_modem_exit);
201
202MODULE_LICENSE("GPL v2");
203MODULE_DESCRIPTION("mdm modem driver");
204MODULE_VERSION("2.0");
205MODULE_ALIAS("mdm_modem");