1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/mutex.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/kernel.h>
#include <linux/irq.h>
#include <linux/ioctl.h>
#include <linux/delay.h>
#include <linux/reboot.h>
#include <linux/debugfs.h>
#include <linux/completion.h>
#include <linux/workqueue.h>
#include <linux/clk.h>
#include <linux/mfd/pmic8058.h>
#include <asm/mach-types.h>
#include <asm/uaccess.h>
#include <mach/mdm2.h>
#include <mach/restart.h>
#include <mach/subsystem_notif.h>
#include <mach/subsystem_restart.h>
#include <linux/msm_charm.h>
#include "msm_watchdog.h"
#include "devices.h"
#include "clock.h"
#include "mdm_private.h"
#define MDM_PBLRDY_CNT 20
static int mdm_debug_mask;
static void mdm_peripheral_connect(struct mdm_modem_drv *mdm_drv)
{
if (!mdm_drv->pdata->peripheral_platform_device)
return;
mutex_lock(&mdm_drv->peripheral_status_lock);
if (mdm_drv->peripheral_status)
goto out;
platform_device_add(mdm_drv->pdata->peripheral_platform_device);
mdm_drv->peripheral_status = 1;
out:
mutex_unlock(&mdm_drv->peripheral_status_lock);
}
static void mdm_peripheral_disconnect(struct mdm_modem_drv *mdm_drv)
{
if (!mdm_drv->pdata->peripheral_platform_device)
return;
mutex_lock(&mdm_drv->peripheral_status_lock);
if (!mdm_drv->peripheral_status)
goto out;
platform_device_del(mdm_drv->pdata->peripheral_platform_device);
mdm_drv->peripheral_status = 0;
out:
mutex_unlock(&mdm_drv->peripheral_status_lock);
}
/* This function can be called from atomic context. */
static void mdm_toggle_soft_reset(struct mdm_modem_drv *mdm_drv)
{
int soft_reset_direction_assert = 0,
soft_reset_direction_de_assert = 1;
if (mdm_drv->pdata->soft_reset_inverted) {
soft_reset_direction_assert = 1;
soft_reset_direction_de_assert = 0;
}
gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
soft_reset_direction_assert);
/* Use mdelay because this function can be called from atomic
* context.
*/
mdelay(10);
gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
soft_reset_direction_de_assert);
}
/* This function can be called from atomic context. */
static void mdm_atomic_soft_reset(struct mdm_modem_drv *mdm_drv)
{
mdm_toggle_soft_reset(mdm_drv);
}
static void mdm_power_down_common(struct mdm_modem_drv *mdm_drv)
{
int i;
int soft_reset_direction =
mdm_drv->pdata->soft_reset_inverted ? 1 : 0;
mdm_peripheral_disconnect(mdm_drv);
/* Wait for the modem to complete its power down actions. */
for (i = 20; i > 0; i--) {
if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) {
if (mdm_debug_mask & MDM_DEBUG_MASK_SHDN_LOG)
pr_info("%s:id %d: mdm2ap_statuswent low, i=%d\n",
__func__, mdm_drv->device_id, i);
break;
}
msleep(100);
}
/* Assert the soft reset line whether mdm2ap_status went low or not */
gpio_direction_output(mdm_drv->ap2mdm_soft_reset_gpio,
soft_reset_direction);
if (i == 0) {
pr_err("%s: MDM2AP_STATUS never went low. Doing a hard reset\n",
__func__);
/*
* Currently, there is a debounce timer on the charm PMIC. It is
* necessary to hold the PMIC RESET low for ~3.5 seconds
* for the reset to fully take place. Sleep here to ensure the
* reset has occured before the function exits.
*/
msleep(4000);
}
}
static void mdm_do_first_power_on(struct mdm_modem_drv *mdm_drv)
{
int i;
int pblrdy;
if (mdm_drv->power_on_count != 1) {
pr_err("%s:id %d: Calling fn when power_on_count != 1\n",
__func__, mdm_drv->device_id);
return;
}
pr_err("%s:id %d: Powering on modem for the first time\n",
__func__, mdm_drv->device_id);
mdm_peripheral_disconnect(mdm_drv);
/* If this is the first power-up after a panic, the modem may still
* be in a power-on state, in which case we need to toggle the gpio
* instead of just de-asserting it. No harm done if the modem was
* powered down.
*/
mdm_toggle_soft_reset(mdm_drv);
/* If the device has a kpd pwr gpio then toggle it. */
if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio)) {
/* Pull AP2MDM_KPDPWR gpio high and wait for PS_HOLD to settle,
* then pull it back low.
*/
pr_debug("%s:id %d: Pulling AP2MDM_KPDPWR gpio high\n",
__func__, mdm_drv->device_id);
gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 1);
msleep(1000);
gpio_direction_output(mdm_drv->ap2mdm_kpdpwr_n_gpio, 0);
}
msleep(50);
gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1);
if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
goto start_mdm_peripheral;
for (i = 0; i < MDM_PBLRDY_CNT; i++) {
pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
if (pblrdy)
break;
usleep_range(5000, 5000);
}
pr_debug("%s: id %d: pblrdy i:%d\n", __func__,
mdm_drv->device_id, i);
start_mdm_peripheral:
mdm_peripheral_connect(mdm_drv);
msleep(200);
}
static void mdm_do_soft_power_on(struct mdm_modem_drv *mdm_drv)
{
int i;
int pblrdy;
pr_err("%s: id %d: soft resetting mdm modem\n",
__func__, mdm_drv->device_id);
mdm_peripheral_disconnect(mdm_drv);
mdm_toggle_soft_reset(mdm_drv);
if (!GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
goto start_mdm_peripheral;
for (i = 0; i < MDM_PBLRDY_CNT; i++) {
pblrdy = gpio_get_value(mdm_drv->mdm2ap_pblrdy);
if (pblrdy)
break;
usleep_range(5000, 5000);
}
pr_debug("%s: id %d: pblrdy i:%d\n", __func__,
mdm_drv->device_id, i);
start_mdm_peripheral:
mdm_peripheral_connect(mdm_drv);
msleep(200);
}
static void mdm_power_on_common(struct mdm_modem_drv *mdm_drv)
{
mdm_drv->power_on_count++;
/* this gpio will be used to indicate apq readiness,
* de-assert it now so that it can be asserted later.
* May not be used.
*/
if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
/*
* If we did an "early power on" then ignore the very next
* power-on request because it would the be first request from
* user space but we're already powered on. Ignore it.
*/
if (mdm_drv->pdata->early_power_on &&
(mdm_drv->power_on_count == 2))
return;
if (mdm_drv->power_on_count == 1)
mdm_do_first_power_on(mdm_drv);
else
mdm_do_soft_power_on(mdm_drv);
}
static void debug_state_changed(int value)
{
mdm_debug_mask = value;
}
static void mdm_status_changed(struct mdm_modem_drv *mdm_drv, int value)
{
pr_debug("%s: id %d: value:%d\n", __func__,
value, mdm_drv->device_id);
if (value) {
mdm_peripheral_disconnect(mdm_drv);
mdm_peripheral_connect(mdm_drv);
if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 1);
}
}
static void mdm_image_upgrade(struct mdm_modem_drv *mdm_drv, int type)
{
switch (type) {
case APQ_CONTROLLED_UPGRADE:
pr_debug("%s: id %d: APQ controlled modem image upgrade\n",
__func__, mdm_drv->device_id);
atomic_set(&mdm_drv->mdm_ready, 0);
mdm_toggle_soft_reset(mdm_drv);
break;
case MDM_CONTROLLED_UPGRADE:
pr_debug("%s: id %d: MDM controlled modem image upgrade\n",
__func__, mdm_drv->device_id);
atomic_set(&mdm_drv->mdm_ready, 0);
/*
* If we have no image currently present on the modem, then we
* would be in PBL, in which case the status gpio would not go
* high.
*/
mdm_drv->disable_status_check = 1;
if (GPIO_IS_VALID(mdm_drv->usb_switch_gpio)) {
pr_info("%s: id %d: Switching usb control to MDM\n",
__func__, mdm_drv->device_id);
gpio_direction_output(mdm_drv->usb_switch_gpio, 1);
} else
pr_err("%s: id %d: usb switch gpio unavailable\n",
__func__, mdm_drv->device_id);
break;
default:
pr_err("%s: id %d: invalid upgrade type\n",
__func__, mdm_drv->device_id);
}
}
static struct mdm_ops mdm_cb = {
.power_on_mdm_cb = mdm_power_on_common,
.reset_mdm_cb = mdm_power_on_common,
.atomic_reset_mdm_cb = mdm_atomic_soft_reset,
.power_down_mdm_cb = mdm_power_down_common,
.debug_state_changed_cb = debug_state_changed,
.status_cb = mdm_status_changed,
.image_upgrade_cb = mdm_image_upgrade,
};
int mdm_get_ops(struct mdm_ops **mdm_ops)
{
*mdm_ops = &mdm_cb;
return 0;
}
|