blob: 5e12a0df649fbc1e767d65e3682a6ce3bbd06994 [file] [log] [blame]
Taniya Das2b7646b2013-01-15 11:28:14 +05301/* Copyright (c) 2011-2013, The Linux Foundation. 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>
Joel King269aa602012-07-23 08:07:35 -070033#include <linux/msm_charm.h>
Joel Kingb6f0f612011-11-01 16:59:14 -070034#include <asm/mach-types.h>
35#include <asm/uaccess.h>
36#include <mach/mdm2.h>
37#include <mach/restart.h>
38#include <mach/subsystem_notif.h>
39#include <mach/subsystem_restart.h>
Joel Kingef390842012-05-23 16:42:48 -070040#include <mach/rpm.h>
Joel King269aa602012-07-23 08:07:35 -070041#include <mach/gpiomux.h>
Joel Kingb6f0f612011-11-01 16:59:14 -070042#include "msm_watchdog.h"
43#include "mdm_private.h"
Joel King30fdd662012-05-07 19:39:29 -070044#include "sysmon.h"
Joel Kingb6f0f612011-11-01 16:59:14 -070045
46#define MDM_MODEM_TIMEOUT 6000
47#define MDM_MODEM_DELTA 100
Joel King52d7fa62012-03-21 08:40:52 -070048#define MDM_BOOT_TIMEOUT 60000L
Joel King733377c2012-06-20 13:07:38 -070049#define MDM_RDUMP_TIMEOUT 120000L
Joel King493b3ac2012-05-14 11:39:45 -070050#define MDM2AP_STATUS_TIMEOUT_MS 60000L
Joel Kingb6f0f612011-11-01 16:59:14 -070051
Ameya Thakur552e58a2013-01-30 11:33:22 -080052/* Allow a maximum device id of this many digits */
53#define MAX_DEVICE_DIGITS 10
Joel Kingb6f0f612011-11-01 16:59:14 -070054#define EXTERNAL_MODEM "external_modem"
Ameya Thakur552e58a2013-01-30 11:33:22 -080055#define SUBSYS_NAME_LENGTH \
56 (sizeof(EXTERNAL_MODEM) + MAX_DEVICE_DIGITS)
Joel Kingb6f0f612011-11-01 16:59:14 -070057
Ameya Thakur552e58a2013-01-30 11:33:22 -080058#define DEVICE_BASE_NAME "mdm"
59#define DEVICE_NAME_LENGTH \
60 (sizeof(DEVICE_BASE_NAME) + MAX_DEVICE_DIGITS)
Joel Kingb6f0f612011-11-01 16:59:14 -070061
Joel King30fdd662012-05-07 19:39:29 -070062#define RD_BUF_SIZE 100
63#define SFR_MAX_RETRIES 10
64#define SFR_RETRY_INTERVAL 1000
65
Joel King269aa602012-07-23 08:07:35 -070066enum gpio_update_config {
67 GPIO_UPDATE_BOOTING_CONFIG = 1,
68 GPIO_UPDATE_RUNNING_CONFIG,
69};
Ameya Thakur552e58a2013-01-30 11:33:22 -080070
71struct mdm_device {
72 struct list_head link;
73 struct mdm_modem_drv mdm_data;
74
75 int mdm2ap_status_valid_old_config;
76 struct gpiomux_setting mdm2ap_status_old_config;
77 int first_boot;
78 struct workqueue_struct *mdm_queue;
79 struct workqueue_struct *mdm_sfr_queue;
80 unsigned int dump_timeout_ms;
81
82 char subsys_name[SUBSYS_NAME_LENGTH];
83 struct subsys_desc mdm_subsys;
84 struct subsys_device *mdm_subsys_dev;
85
86 char device_name[DEVICE_NAME_LENGTH];
87 struct miscdevice misc_device;
88
89 struct completion mdm_needs_reload;
90 struct completion mdm_boot;
91 struct completion mdm_ram_dumps;
92 int mdm_errfatal_irq;
93 int mdm_status_irq;
94 int mdm_pblrdy_irq;
95
96 struct delayed_work mdm2ap_status_check_work;
97 struct work_struct mdm_status_work;
98 struct work_struct sfr_reason_work;
99
100 struct notifier_block mdm_panic_blk;
101
102 int ssr_started_internally;
103};
104
105static struct list_head mdm_devices;
106static DEFINE_SPINLOCK(mdm_devices_lock);
107
108static int ssr_count;
109static DEFINE_SPINLOCK(ssr_lock);
110
111static unsigned int mdm_debug_mask;
112int vddmin_gpios_sent;
113static struct mdm_ops *mdm_ops;
114
115static void mdm_device_list_add(struct mdm_device *mdev)
116{
117 unsigned long flags;
118
119 spin_lock_irqsave(&mdm_devices_lock, flags);
120 list_add_tail(&mdev->link, &mdm_devices);
121 spin_unlock_irqrestore(&mdm_devices_lock, flags);
122}
123
124static void mdm_device_list_remove(struct mdm_device *mdev)
125{
126 unsigned long flags;
127 struct mdm_device *lmdev, *tmp;
128
129 spin_lock_irqsave(&mdm_devices_lock, flags);
130 list_for_each_entry_safe(lmdev, tmp, &mdm_devices, link) {
131 if (mdev && mdev == lmdev) {
132 pr_debug("%s: removing device id %d\n",
133 __func__, mdev->mdm_data.device_id);
134 list_del(&mdev->link);
135 break;
136 }
137 }
138 spin_unlock_irqrestore(&mdm_devices_lock, flags);
139}
140
Ameya Thakur552e58a2013-01-30 11:33:22 -0800141/* If the platform's cascading_ssr flag is set, the subsystem
142 * restart module will restart the other modems so stop
143 * monitoring them as well.
144 * This function can be called from interrupt context.
145 */
146static void mdm_start_ssr(struct mdm_device *mdev)
147{
148 unsigned long flags;
149 int start_ssr = 1;
150
151 spin_lock_irqsave(&ssr_lock, flags);
152 if (mdev->mdm_data.pdata->cascading_ssr &&
153 ssr_count > 0) {
154 start_ssr = 0;
155 } else {
156 ssr_count++;
157 mdev->ssr_started_internally = 1;
158 }
159 spin_unlock_irqrestore(&ssr_lock, flags);
160
161 if (start_ssr) {
162 atomic_set(&mdev->mdm_data.mdm_ready, 0);
163 pr_debug("%s: Resetting mdm id %d due to mdm error\n",
164 __func__, mdev->mdm_data.device_id);
165 subsystem_restart_dev(mdev->mdm_subsys_dev);
166 } else {
167 pr_debug("%s: Another modem is already in SSR\n",
168 __func__);
169 }
170}
171
172/* Increment the reference count to handle the case where
173 * subsystem restart is initiated by the SSR service.
174 */
175static void mdm_ssr_started(struct mdm_device *mdev)
176{
177 unsigned long flags;
178
179 spin_lock_irqsave(&ssr_lock, flags);
180 ssr_count++;
181 atomic_set(&mdev->mdm_data.mdm_ready, 0);
182 spin_unlock_irqrestore(&ssr_lock, flags);
183}
184
185/* mdm_ssr_completed assumes that mdm_ssr_started has previously
186 * been called.
187 */
188static void mdm_ssr_completed(struct mdm_device *mdev)
189{
190 unsigned long flags;
191
192 spin_lock_irqsave(&ssr_lock, flags);
193 ssr_count--;
194 if (mdev->ssr_started_internally) {
195 mdev->ssr_started_internally = 0;
196 ssr_count--;
197 }
198
199 if (ssr_count < 0) {
200 pr_err("%s: ssr_count = %d\n",
201 __func__, ssr_count);
202 panic("%s: ssr_count = %d < 0\n",
203 __func__, ssr_count);
204 }
205 spin_unlock_irqrestore(&ssr_lock, flags);
206}
Joel King269aa602012-07-23 08:07:35 -0700207
Joel Kingef390842012-05-23 16:42:48 -0700208static irqreturn_t mdm_vddmin_change(int irq, void *dev_id)
209{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800210 struct mdm_device *mdev = (struct mdm_device *)dev_id;
211 struct mdm_vddmin_resource *vddmin_res;
212 int value;
Joel Kingef390842012-05-23 16:42:48 -0700213
Ameya Thakur552e58a2013-01-30 11:33:22 -0800214 if (!mdev)
215 goto handled;
216
217 vddmin_res = mdev->mdm_data.pdata->vddmin_resource;
218 if (!vddmin_res)
219 goto handled;
220
221 value = gpio_get_value(
222 vddmin_res->mdm2ap_vddmin_gpio);
Joel Kingef390842012-05-23 16:42:48 -0700223 if (value == 0)
Ameya Thakur552e58a2013-01-30 11:33:22 -0800224 pr_debug("External Modem id %d entered Vddmin\n",
225 mdev->mdm_data.device_id);
Joel Kingef390842012-05-23 16:42:48 -0700226 else
Ameya Thakur552e58a2013-01-30 11:33:22 -0800227 pr_debug("External Modem id %d exited Vddmin\n",
228 mdev->mdm_data.device_id);
229handled:
Joel Kingef390842012-05-23 16:42:48 -0700230 return IRQ_HANDLED;
231}
232
Ameya Thakur552e58a2013-01-30 11:33:22 -0800233/* The vddmin_res resource may not be supported by some platforms. */
Joel Kingef390842012-05-23 16:42:48 -0700234static void mdm_setup_vddmin_gpios(void)
235{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800236 unsigned long flags;
Joel Kingef390842012-05-23 16:42:48 -0700237 struct msm_rpm_iv_pair req;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800238 struct mdm_device *mdev;
Joel Kingef390842012-05-23 16:42:48 -0700239 struct mdm_vddmin_resource *vddmin_res;
240 int irq, ret;
241
Ameya Thakur552e58a2013-01-30 11:33:22 -0800242 spin_lock_irqsave(&mdm_devices_lock, flags);
243 list_for_each_entry(mdev, &mdm_devices, link) {
244 vddmin_res = mdev->mdm_data.pdata->vddmin_resource;
245 if (!vddmin_res)
246 continue;
Joel Kingef390842012-05-23 16:42:48 -0700247
Ameya Thakur552e58a2013-01-30 11:33:22 -0800248 pr_debug("Enabling vddmin logging on modem id %d\n",
249 mdev->mdm_data.device_id);
250 req.id = vddmin_res->rpm_id;
251 req.value =
252 ((uint32_t)vddmin_res->ap2mdm_vddmin_gpio & 0x0000FFFF)
253 << 16;
254 req.value |= ((uint32_t)vddmin_res->modes & 0x000000FF) << 8;
255 req.value |= (uint32_t)vddmin_res->drive_strength & 0x000000FF;
Joel Kingef390842012-05-23 16:42:48 -0700256
Ameya Thakur552e58a2013-01-30 11:33:22 -0800257 msm_rpm_set(MSM_RPM_CTX_SET_0, &req, 1);
Joel Kingef390842012-05-23 16:42:48 -0700258
Ameya Thakur552e58a2013-01-30 11:33:22 -0800259 /* Start monitoring low power gpio from mdm */
260 irq = gpio_to_irq(vddmin_res->mdm2ap_vddmin_gpio);
261 if (irq < 0)
262 pr_err("%s: could not get LPM POWER IRQ resource mdm id %d.\n",
263 __func__, mdev->mdm_data.device_id);
264 else {
265 ret = request_threaded_irq(irq, NULL, mdm_vddmin_change,
266 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
267 "mdm lpm", mdev);
268
269 if (ret < 0)
270 pr_err("%s: MDM LPM IRQ#%d request failed with error=%d",
271 __func__, irq, ret);
272 }
Joel Kingef390842012-05-23 16:42:48 -0700273 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800274 spin_unlock_irqrestore(&mdm_devices_lock, flags);
Joel Kingef390842012-05-23 16:42:48 -0700275 return;
276}
277
Joel King30fdd662012-05-07 19:39:29 -0700278static void mdm_restart_reason_fn(struct work_struct *work)
279{
280 int ret, ntries = 0;
281 char sfr_buf[RD_BUF_SIZE];
Ameya Thakur552e58a2013-01-30 11:33:22 -0800282 struct mdm_platform_data *pdata;
283 struct mdm_device *mdev = container_of(work,
284 struct mdm_device, sfr_reason_work);
Joel King30fdd662012-05-07 19:39:29 -0700285
Ameya Thakur552e58a2013-01-30 11:33:22 -0800286 pdata = mdev->mdm_data.pdata;
287 if (pdata->sysmon_subsys_id_valid) {
288 do {
289 ret = sysmon_get_reason(pdata->sysmon_subsys_id,
Joel King30fdd662012-05-07 19:39:29 -0700290 sfr_buf, sizeof(sfr_buf));
Ameya Thakur552e58a2013-01-30 11:33:22 -0800291 if (!ret) {
292 pr_err("mdm restart reason: %s\n", sfr_buf);
293 return;
294 }
295 /* Wait for the modem to be fully booted after a
296 * subsystem restart. This may take several seconds.
Joel King30fdd662012-05-07 19:39:29 -0700297 */
Ameya Thakur552e58a2013-01-30 11:33:22 -0800298 msleep(SFR_RETRY_INTERVAL);
299 } while (++ntries < SFR_MAX_RETRIES);
300 pr_debug("%s: Error retrieving restart reason: %d\n",
301 __func__, ret);
302 }
Joel King30fdd662012-05-07 19:39:29 -0700303}
304
Joel King493b3ac2012-05-14 11:39:45 -0700305static void mdm2ap_status_check(struct work_struct *work)
306{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800307 struct mdm_device *mdev =
308 container_of(work, struct mdm_device,
309 mdm2ap_status_check_work.work);
310 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
Joel King493b3ac2012-05-14 11:39:45 -0700311 /*
312 * If the mdm modem did not pull the MDM2AP_STATUS gpio
313 * high then call subsystem_restart.
314 */
Ameya Thakur43248fd2012-07-10 18:50:52 -0700315 if (!mdm_drv->disable_status_check) {
316 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800317 pr_debug("%s: MDM2AP_STATUS did not go high on mdm id %d\n",
318 __func__, mdev->mdm_data.device_id);
319 mdm_start_ssr(mdev);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700320 }
Joel King493b3ac2012-05-14 11:39:45 -0700321 }
322}
323
Ameya Thakur552e58a2013-01-30 11:33:22 -0800324static void mdm_update_gpio_configs(struct mdm_device *mdev,
325 enum gpio_update_config gpio_config)
Joel King269aa602012-07-23 08:07:35 -0700326{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800327 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
328
Joel King269aa602012-07-23 08:07:35 -0700329 /* Some gpio configuration may need updating after modem bootup.*/
330 switch (gpio_config) {
331 case GPIO_UPDATE_RUNNING_CONFIG:
332 if (mdm_drv->pdata->mdm2ap_status_gpio_run_cfg) {
333 if (msm_gpiomux_write(mdm_drv->mdm2ap_status_gpio,
334 GPIOMUX_ACTIVE,
335 mdm_drv->pdata->mdm2ap_status_gpio_run_cfg,
Ameya Thakur552e58a2013-01-30 11:33:22 -0800336 &mdev->mdm2ap_status_old_config))
337 pr_err("%s: failed updating running gpio config mdm id %d\n",
338 __func__, mdev->mdm_data.device_id);
Joel King269aa602012-07-23 08:07:35 -0700339 else
Ameya Thakur552e58a2013-01-30 11:33:22 -0800340 mdev->mdm2ap_status_valid_old_config = 1;
Joel King269aa602012-07-23 08:07:35 -0700341 }
342 break;
343 case GPIO_UPDATE_BOOTING_CONFIG:
Ameya Thakur552e58a2013-01-30 11:33:22 -0800344 if (mdev->mdm2ap_status_valid_old_config) {
Joel King269aa602012-07-23 08:07:35 -0700345 msm_gpiomux_write(mdm_drv->mdm2ap_status_gpio,
346 GPIOMUX_ACTIVE,
Ameya Thakur552e58a2013-01-30 11:33:22 -0800347 &mdev->mdm2ap_status_old_config,
Joel King269aa602012-07-23 08:07:35 -0700348 NULL);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800349 mdev->mdm2ap_status_valid_old_config = 0;
Joel King269aa602012-07-23 08:07:35 -0700350 }
351 break;
352 default:
353 pr_err("%s: called with no config\n", __func__);
354 break;
355 }
356}
357
Pavankumar Kondeti0161cf92013-02-12 18:10:21 +0530358static long mdm_modem_ioctl(struct file *filp, unsigned int cmd,
Joel Kingb6f0f612011-11-01 16:59:14 -0700359 unsigned long arg)
360{
361 int status, ret = 0;
Pavankumar Kondeti0161cf92013-02-12 18:10:21 +0530362 struct mdm_device *mdev = filp->private_data;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800363 struct mdm_modem_drv *mdm_drv;
364
Joel Kingb6f0f612011-11-01 16:59:14 -0700365 if (_IOC_TYPE(cmd) != CHARM_CODE) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800366 pr_err("%s: invalid ioctl code to mdm id %d\n",
367 __func__, mdev->mdm_data.device_id);
Joel Kingb6f0f612011-11-01 16:59:14 -0700368 return -EINVAL;
369 }
370
Ameya Thakur552e58a2013-01-30 11:33:22 -0800371 mdm_drv = &mdev->mdm_data;
372 pr_debug("%s: Entering ioctl cmd = %d, mdm id = %d\n",
373 __func__, _IOC_NR(cmd), mdev->mdm_data.device_id);
Joel Kingb6f0f612011-11-01 16:59:14 -0700374 switch (cmd) {
375 case WAKE_CHARM:
Ameya Thakur552e58a2013-01-30 11:33:22 -0800376 pr_debug("%s: Powering on mdm id %d\n",
377 __func__, mdev->mdm_data.device_id);
378 mdm_ops->power_on_mdm_cb(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700379 break;
380 case CHECK_FOR_BOOT:
381 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0)
382 put_user(1, (unsigned long __user *) arg);
383 else
384 put_user(0, (unsigned long __user *) arg);
385 break;
386 case NORMAL_BOOT_DONE:
Ameya Thakur552e58a2013-01-30 11:33:22 -0800387 pr_debug("%s: check if mdm id %d is booted up\n",
388 __func__, mdev->mdm_data.device_id);
Joel Kingb6f0f612011-11-01 16:59:14 -0700389 get_user(status, (unsigned long __user *) arg);
Joel King2a42f502012-02-03 11:36:25 -0800390 if (status) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800391 pr_debug("%s: normal boot of mdm id %d failed\n",
392 __func__, mdev->mdm_data.device_id);
Joel Kingb6f0f612011-11-01 16:59:14 -0700393 mdm_drv->mdm_boot_status = -EIO;
Joel King2a42f502012-02-03 11:36:25 -0800394 } else {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800395 pr_debug("%s: normal boot of mdm id %d done\n",
396 __func__, mdev->mdm_data.device_id);
Joel Kingb6f0f612011-11-01 16:59:14 -0700397 mdm_drv->mdm_boot_status = 0;
Joel King2a42f502012-02-03 11:36:25 -0800398 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800399 atomic_set(&mdm_drv->mdm_ready, 1);
Joel Kingb6f0f612011-11-01 16:59:14 -0700400
Ameya Thakur552e58a2013-01-30 11:33:22 -0800401 if (mdm_ops->normal_boot_done_cb != NULL)
402 mdm_ops->normal_boot_done_cb(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700403
Ameya Thakur552e58a2013-01-30 11:33:22 -0800404 if (!mdev->first_boot)
405 complete(&mdev->mdm_boot);
Joel Kingb6f0f612011-11-01 16:59:14 -0700406 else
Ameya Thakur552e58a2013-01-30 11:33:22 -0800407 mdev->first_boot = 0;
Joel King493b3ac2012-05-14 11:39:45 -0700408
Joel King269aa602012-07-23 08:07:35 -0700409 /* If successful, start a timer to check that the mdm2ap_status
410 * gpio goes high.
Joel King493b3ac2012-05-14 11:39:45 -0700411 */
Joel King269aa602012-07-23 08:07:35 -0700412 if (!status && gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0)
Ameya Thakur552e58a2013-01-30 11:33:22 -0800413 schedule_delayed_work(&mdev->mdm2ap_status_check_work,
Joel King493b3ac2012-05-14 11:39:45 -0700414 msecs_to_jiffies(MDM2AP_STATUS_TIMEOUT_MS));
Joel Kingb6f0f612011-11-01 16:59:14 -0700415 break;
416 case RAM_DUMP_DONE:
Joel King2a42f502012-02-03 11:36:25 -0800417 pr_debug("%s: mdm done collecting RAM dumps\n", __func__);
Joel Kingb6f0f612011-11-01 16:59:14 -0700418 get_user(status, (unsigned long __user *) arg);
419 if (status)
420 mdm_drv->mdm_ram_dump_status = -EIO;
Joel King2a42f502012-02-03 11:36:25 -0800421 else {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800422 pr_debug("%s: ramdump collection completed\n",
423 __func__);
Joel Kingb6f0f612011-11-01 16:59:14 -0700424 mdm_drv->mdm_ram_dump_status = 0;
Joel King2a42f502012-02-03 11:36:25 -0800425 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800426 complete(&mdev->mdm_ram_dumps);
Joel Kingb6f0f612011-11-01 16:59:14 -0700427 break;
428 case WAIT_FOR_RESTART:
Joel King2a42f502012-02-03 11:36:25 -0800429 pr_debug("%s: wait for mdm to need images reloaded\n",
Joel Kingb6f0f612011-11-01 16:59:14 -0700430 __func__);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800431 ret = wait_for_completion_interruptible(
432 &mdev->mdm_needs_reload);
Joel Kingb6f0f612011-11-01 16:59:14 -0700433 if (!ret)
434 put_user(mdm_drv->boot_type,
435 (unsigned long __user *) arg);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800436 init_completion(&mdev->mdm_needs_reload);
Joel Kingb6f0f612011-11-01 16:59:14 -0700437 break;
Joel Kinge92eb872012-05-06 09:30:24 -0700438 case GET_DLOAD_STATUS:
439 pr_debug("getting status of mdm2ap_errfatal_gpio\n");
440 if (gpio_get_value(mdm_drv->mdm2ap_errfatal_gpio) == 1 &&
Ameya Thakur552e58a2013-01-30 11:33:22 -0800441 !atomic_read(&mdm_drv->mdm_ready))
Joel Kinge92eb872012-05-06 09:30:24 -0700442 put_user(1, (unsigned long __user *) arg);
443 else
444 put_user(0, (unsigned long __user *) arg);
445 break;
Ameya Thakur43248fd2012-07-10 18:50:52 -0700446 case IMAGE_UPGRADE:
447 pr_debug("%s Image upgrade ioctl recieved\n", __func__);
448 if (mdm_drv->pdata->image_upgrade_supported &&
Ameya Thakur552e58a2013-01-30 11:33:22 -0800449 mdm_ops->image_upgrade_cb) {
Ameya Thakur43248fd2012-07-10 18:50:52 -0700450 get_user(status, (unsigned long __user *) arg);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800451 mdm_ops->image_upgrade_cb(mdm_drv, status);
Ameya Thakur43248fd2012-07-10 18:50:52 -0700452 } else
453 pr_debug("%s Image upgrade not supported\n", __func__);
454 break;
Joel Kingbf3e4b52012-09-26 09:10:34 -0700455 case SHUTDOWN_CHARM:
456 if (!mdm_drv->pdata->send_shdn)
457 break;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800458 atomic_set(&mdm_drv->mdm_ready, 0);
Joel Kingbf3e4b52012-09-26 09:10:34 -0700459 if (mdm_debug_mask & MDM_DEBUG_MASK_SHDN_LOG)
Ameya Thakur552e58a2013-01-30 11:33:22 -0800460 pr_debug("Sending shutdown request to mdm\n");
Joel Kingbf3e4b52012-09-26 09:10:34 -0700461 ret = sysmon_send_shutdown(SYSMON_SS_EXT_MODEM);
462 if (ret)
463 pr_err("%s: Graceful shutdown of the external modem failed, ret = %d\n",
464 __func__, ret);
Taniya Dasd1ed37b2012-11-29 12:56:38 +0530465 put_user(ret, (unsigned long __user *) arg);
Joel Kingbf3e4b52012-09-26 09:10:34 -0700466 break;
Joel Kingb6f0f612011-11-01 16:59:14 -0700467 default:
468 pr_err("%s: invalid ioctl cmd = %d\n", __func__, _IOC_NR(cmd));
469 ret = -EINVAL;
470 break;
471 }
Joel Kingb6f0f612011-11-01 16:59:14 -0700472 return ret;
473}
474
Joel Kingb6f0f612011-11-01 16:59:14 -0700475static void mdm_status_fn(struct work_struct *work)
476{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800477 struct mdm_device *mdev =
478 container_of(work, struct mdm_device, mdm_status_work);
479 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
Vamsi Krishna33925632011-12-13 15:43:09 -0800480 int value = gpio_get_value(mdm_drv->mdm2ap_status_gpio);
481
Joel King2a42f502012-02-03 11:36:25 -0800482 pr_debug("%s: status:%d\n", __func__, value);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800483 if (atomic_read(&mdm_drv->mdm_ready) && mdm_ops->status_cb)
484 mdm_ops->status_cb(mdm_drv, value);
Joel King269aa602012-07-23 08:07:35 -0700485
486 /* Update gpio configuration to "running" config. */
Ameya Thakur552e58a2013-01-30 11:33:22 -0800487 mdm_update_gpio_configs(mdev, GPIO_UPDATE_RUNNING_CONFIG);
Joel Kingb6f0f612011-11-01 16:59:14 -0700488}
489
Ameya Thakur552e58a2013-01-30 11:33:22 -0800490static void mdm_disable_irqs(struct mdm_device *mdev)
Joel Kingb6f0f612011-11-01 16:59:14 -0700491{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800492 if (!mdev)
493 return;
494 disable_irq_nosync(mdev->mdm_errfatal_irq);
495 disable_irq_nosync(mdev->mdm_status_irq);
496 disable_irq_nosync(mdev->mdm_pblrdy_irq);
Joel Kingb6f0f612011-11-01 16:59:14 -0700497}
498
499static irqreturn_t mdm_errfatal(int irq, void *dev_id)
500{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800501 struct mdm_modem_drv *mdm_drv;
502 struct mdm_device *mdev = (struct mdm_device *)dev_id;
503 if (!mdev)
504 return IRQ_HANDLED;
505
506 pr_debug("%s: mdm id %d sent errfatal interrupt\n",
507 __func__, mdev->mdm_data.device_id);
508 mdm_drv = &mdev->mdm_data;
509 if (atomic_read(&mdm_drv->mdm_ready) &&
Joel Kingb6f0f612011-11-01 16:59:14 -0700510 (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 1)) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800511 pr_debug("%s: Received err fatal from mdm id %d\n",
512 __func__, mdev->mdm_data.device_id);
513 mdm_start_ssr(mdev);
Joel Kingb6f0f612011-11-01 16:59:14 -0700514 }
515 return IRQ_HANDLED;
516}
517
Ameya Thakur552e58a2013-01-30 11:33:22 -0800518/* set the mdm_device as the file's private data */
Joel Kingb6f0f612011-11-01 16:59:14 -0700519static int mdm_modem_open(struct inode *inode, struct file *file)
520{
Pavankumar Kondeti0161cf92013-02-12 18:10:21 +0530521 struct miscdevice *misc = file->private_data;
522 struct mdm_device *mdev = container_of(misc,
523 struct mdm_device, misc_device);
524
525 file->private_data = mdev;
Joel Kingb6f0f612011-11-01 16:59:14 -0700526 return 0;
527}
528
Joel Kingb6f0f612011-11-01 16:59:14 -0700529static int mdm_panic_prep(struct notifier_block *this,
530 unsigned long event, void *ptr)
531{
532 int i;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800533 struct mdm_modem_drv *mdm_drv;
534 struct mdm_device *mdev =
535 container_of(this, struct mdm_device, mdm_panic_blk);
536
537 mdm_drv = &mdev->mdm_data;
Joel Kingb6f0f612011-11-01 16:59:14 -0700538
Joel King2a42f502012-02-03 11:36:25 -0800539 pr_debug("%s: setting AP2MDM_ERRFATAL high for a non graceful reset\n",
Joel Kingb6f0f612011-11-01 16:59:14 -0700540 __func__);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800541 mdm_disable_irqs(mdev);
Joel Kingb6f0f612011-11-01 16:59:14 -0700542 gpio_set_value(mdm_drv->ap2mdm_errfatal_gpio, 1);
543
Joel Kingb6f0f612011-11-01 16:59:14 -0700544 for (i = MDM_MODEM_TIMEOUT; i > 0; i -= MDM_MODEM_DELTA) {
545 pet_watchdog();
546 mdelay(MDM_MODEM_DELTA);
547 if (gpio_get_value(mdm_drv->mdm2ap_status_gpio) == 0)
548 break;
549 }
Joel Kinge92eb872012-05-06 09:30:24 -0700550 if (i <= 0) {
Joel Kingb6f0f612011-11-01 16:59:14 -0700551 pr_err("%s: MDM2AP_STATUS never went low\n", __func__);
Joel Kinge92eb872012-05-06 09:30:24 -0700552 /* Reset the modem so that it will go into download mode. */
Ameya Thakur552e58a2013-01-30 11:33:22 -0800553 if (mdm_drv && mdm_ops->atomic_reset_mdm_cb)
554 mdm_ops->atomic_reset_mdm_cb(mdm_drv);
Joel Kinge92eb872012-05-06 09:30:24 -0700555 }
Joel Kingb6f0f612011-11-01 16:59:14 -0700556 return NOTIFY_DONE;
557}
558
Joel Kingb6f0f612011-11-01 16:59:14 -0700559static irqreturn_t mdm_status_change(int irq, void *dev_id)
560{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800561 struct mdm_modem_drv *mdm_drv;
562 struct mdm_device *mdev = (struct mdm_device *)dev_id;
563 int value;
564 if (!mdev)
565 return IRQ_HANDLED;
566
567 mdm_drv = &mdev->mdm_data;
568 value = gpio_get_value(mdm_drv->mdm2ap_status_gpio);
Joel Kinge92eb872012-05-06 09:30:24 -0700569
Joel Kingbf3e4b52012-09-26 09:10:34 -0700570 if ((mdm_debug_mask & MDM_DEBUG_MASK_SHDN_LOG) && (value == 0))
Ameya Thakur552e58a2013-01-30 11:33:22 -0800571 pr_debug("%s: mdm2ap_status went low\n", __func__);
Joel Kingbf3e4b52012-09-26 09:10:34 -0700572
Ameya Thakur552e58a2013-01-30 11:33:22 -0800573 pr_debug("%s: mdm id %d sent status change interrupt\n",
574 __func__, mdev->mdm_data.device_id);
575 if (value == 0 && atomic_read(&mdm_drv->mdm_ready)) {
576 pr_debug("%s: unexpected reset external modem id %d\n",
577 __func__, mdev->mdm_data.device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700578 mdm_drv->mdm_unexpected_reset_occurred = 1;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800579 mdm_start_ssr(mdev);
Joel Kinge92eb872012-05-06 09:30:24 -0700580 } else if (value == 1) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800581 cancel_delayed_work(&mdev->mdm2ap_status_check_work);
582 pr_debug("%s: status = 1: mdm id %d is now ready\n",
583 __func__, mdev->mdm_data.device_id);
584 queue_work(mdev->mdm_queue, &mdev->mdm_status_work);
Joel Kinge92eb872012-05-06 09:30:24 -0700585 }
Joel Kingb6f0f612011-11-01 16:59:14 -0700586 return IRQ_HANDLED;
587}
588
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700589static irqreturn_t mdm_pblrdy_change(int irq, void *dev_id)
590{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800591 struct mdm_modem_drv *mdm_drv;
592 struct mdm_device *mdev = (struct mdm_device *)dev_id;
593 if (!mdev)
594 return IRQ_HANDLED;
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700595
Ameya Thakur552e58a2013-01-30 11:33:22 -0800596 mdm_drv = &mdev->mdm_data;
597 pr_debug("%s: mdm id %d: pbl ready:%d\n",
598 __func__, mdev->mdm_data.device_id,
599 gpio_get_value(mdm_drv->mdm2ap_pblrdy));
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700600 return IRQ_HANDLED;
601}
602
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700603static int mdm_subsys_shutdown(const struct subsys_desc *crashed_subsys)
Joel Kingb6f0f612011-11-01 16:59:14 -0700604{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800605 struct mdm_device *mdev =
606 container_of(crashed_subsys, struct mdm_device, mdm_subsys);
607 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
608
609 pr_debug("%s: ssr on modem id %d\n", __func__,
610 mdev->mdm_data.device_id);
611
612 mdm_ssr_started(mdev);
613 cancel_delayed_work(&mdev->mdm2ap_status_check_work);
Joel Kingb6f0f612011-11-01 16:59:14 -0700614 gpio_direction_output(mdm_drv->ap2mdm_errfatal_gpio, 1);
Joel King415af512012-02-03 10:22:43 -0800615 if (mdm_drv->pdata->ramdump_delay_ms > 0) {
616 /* Wait for the external modem to complete
617 * its preparation for ramdumps.
618 */
Joel King6e7a3e82012-04-13 17:37:33 -0700619 msleep(mdm_drv->pdata->ramdump_delay_ms);
Joel King415af512012-02-03 10:22:43 -0800620 }
Joel King269aa602012-07-23 08:07:35 -0700621 if (!mdm_drv->mdm_unexpected_reset_occurred) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800622 mdm_ops->reset_mdm_cb(mdm_drv);
Joel King269aa602012-07-23 08:07:35 -0700623 /* Update gpio configuration to "booting" config. */
Ameya Thakur552e58a2013-01-30 11:33:22 -0800624 mdm_update_gpio_configs(mdev, GPIO_UPDATE_BOOTING_CONFIG);
Joel King269aa602012-07-23 08:07:35 -0700625 } else {
Joel Kinge92eb872012-05-06 09:30:24 -0700626 mdm_drv->mdm_unexpected_reset_occurred = 0;
Joel King269aa602012-07-23 08:07:35 -0700627 }
Joel Kingb6f0f612011-11-01 16:59:14 -0700628 return 0;
629}
630
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700631static int mdm_subsys_powerup(const struct subsys_desc *crashed_subsys)
Joel Kingb6f0f612011-11-01 16:59:14 -0700632{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800633 struct mdm_device *mdev =
634 container_of(crashed_subsys, struct mdm_device,
635 mdm_subsys);
636 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
637
638 pr_debug("%s: ssr on modem id %d\n",
639 __func__, mdev->mdm_data.device_id);
640
Joel Kingbc48e4c2012-02-27 13:18:52 -0800641 gpio_direction_output(mdm_drv->ap2mdm_errfatal_gpio, 0);
642 gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 1);
Taniya Das2b7646b2013-01-15 11:28:14 +0530643
644 if (mdm_drv->pdata->ps_hold_delay_ms > 0)
645 msleep(mdm_drv->pdata->ps_hold_delay_ms);
646
Ameya Thakur552e58a2013-01-30 11:33:22 -0800647 mdm_ops->power_on_mdm_cb(mdm_drv);
Joel Kingb6f0f612011-11-01 16:59:14 -0700648 mdm_drv->boot_type = CHARM_NORMAL_BOOT;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800649 mdm_ssr_completed(mdev);
650 complete(&mdev->mdm_needs_reload);
651 if (!wait_for_completion_timeout(&mdev->mdm_boot,
Joel King52d7fa62012-03-21 08:40:52 -0700652 msecs_to_jiffies(MDM_BOOT_TIMEOUT))) {
653 mdm_drv->mdm_boot_status = -ETIMEDOUT;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800654 pr_debug("%s: mdm modem restart timed out.\n", __func__);
Joel King30fdd662012-05-07 19:39:29 -0700655 } else {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800656 pr_debug("%s: id %d: mdm modem has been restarted\n",
657 __func__, mdm_drv->device_id);
Joel Kinge92eb872012-05-06 09:30:24 -0700658
Joel King30fdd662012-05-07 19:39:29 -0700659 /* Log the reason for the restart */
Joel Kinge92eb872012-05-06 09:30:24 -0700660 if (mdm_drv->pdata->sfr_query)
Ameya Thakur552e58a2013-01-30 11:33:22 -0800661 queue_work(mdev->mdm_sfr_queue, &mdev->sfr_reason_work);
Joel King30fdd662012-05-07 19:39:29 -0700662 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800663 init_completion(&mdev->mdm_boot);
Joel Kingb6f0f612011-11-01 16:59:14 -0700664 return mdm_drv->mdm_boot_status;
665}
666
667static int mdm_subsys_ramdumps(int want_dumps,
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700668 const struct subsys_desc *crashed_subsys)
Joel Kingb6f0f612011-11-01 16:59:14 -0700669{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800670 struct mdm_device *mdev =
671 container_of(crashed_subsys, struct mdm_device,
672 mdm_subsys);
673 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
674
675 pr_debug("%s: ssr on modem id %d\n", __func__,
676 mdev->mdm_data.device_id);
677
Joel Kingb6f0f612011-11-01 16:59:14 -0700678 mdm_drv->mdm_ram_dump_status = 0;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800679 cancel_delayed_work(&mdev->mdm2ap_status_check_work);
Joel Kingb6f0f612011-11-01 16:59:14 -0700680 if (want_dumps) {
681 mdm_drv->boot_type = CHARM_RAM_DUMPS;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800682 complete(&mdev->mdm_needs_reload);
683 if (!wait_for_completion_timeout(&mdev->mdm_ram_dumps,
684 msecs_to_jiffies(mdev->dump_timeout_ms))) {
Joel King52d7fa62012-03-21 08:40:52 -0700685 mdm_drv->mdm_ram_dump_status = -ETIMEDOUT;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800686 mdm_ssr_completed(mdev);
687 pr_err("%s: mdm modem ramdumps timed out.\n",
Joel King52d7fa62012-03-21 08:40:52 -0700688 __func__);
689 } else
Ameya Thakur552e58a2013-01-30 11:33:22 -0800690 pr_debug("%s: mdm modem ramdumps completed.\n",
Joel King52d7fa62012-03-21 08:40:52 -0700691 __func__);
Ameya Thakur552e58a2013-01-30 11:33:22 -0800692 init_completion(&mdev->mdm_ram_dumps);
Joel King269aa602012-07-23 08:07:35 -0700693 if (!mdm_drv->pdata->no_powerdown_after_ramdumps) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800694 mdm_ops->power_down_mdm_cb(mdm_drv);
Joel King269aa602012-07-23 08:07:35 -0700695 /* Update gpio configuration to "booting" config. */
Ameya Thakur552e58a2013-01-30 11:33:22 -0800696 mdm_update_gpio_configs(mdev,
697 GPIO_UPDATE_BOOTING_CONFIG);
Joel King269aa602012-07-23 08:07:35 -0700698 }
Joel Kingb6f0f612011-11-01 16:59:14 -0700699 }
700 return mdm_drv->mdm_ram_dump_status;
701}
702
Joel King96c96dc2012-07-30 09:06:15 -0700703/* Once the gpios are sent to RPM and debugging
704 * starts, there is no way to stop it without
705 * rebooting the device.
706 */
707static int mdm_debug_mask_set(void *data, u64 val)
Joel Kingb6f0f612011-11-01 16:59:14 -0700708{
Joel King96c96dc2012-07-30 09:06:15 -0700709 if (!vddmin_gpios_sent &&
710 (val & MDM_DEBUG_MASK_VDDMIN_SETUP)) {
711 mdm_setup_vddmin_gpios();
712 vddmin_gpios_sent = 1;
713 }
714
715 mdm_debug_mask = val;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800716 if (mdm_ops->debug_state_changed_cb)
717 mdm_ops->debug_state_changed_cb(mdm_debug_mask);
Joel Kingb6f0f612011-11-01 16:59:14 -0700718 return 0;
719}
720
Joel King96c96dc2012-07-30 09:06:15 -0700721static int mdm_debug_mask_get(void *data, u64 *val)
Joel Kingb6f0f612011-11-01 16:59:14 -0700722{
Joel King96c96dc2012-07-30 09:06:15 -0700723 *val = mdm_debug_mask;
Joel Kingb6f0f612011-11-01 16:59:14 -0700724 return 0;
725}
726
Joel King96c96dc2012-07-30 09:06:15 -0700727DEFINE_SIMPLE_ATTRIBUTE(mdm_debug_mask_fops,
728 mdm_debug_mask_get,
729 mdm_debug_mask_set, "%llu\n");
Joel Kingb6f0f612011-11-01 16:59:14 -0700730
731static int mdm_debugfs_init(void)
732{
733 struct dentry *dent;
734
735 dent = debugfs_create_dir("mdm_dbg", 0);
736 if (IS_ERR(dent))
737 return PTR_ERR(dent);
738
Joel King96c96dc2012-07-30 09:06:15 -0700739 debugfs_create_file("debug_mask", 0644, dent, NULL,
740 &mdm_debug_mask_fops);
Joel Kingb6f0f612011-11-01 16:59:14 -0700741 return 0;
742}
743
Ameya Thakur552e58a2013-01-30 11:33:22 -0800744static const struct file_operations mdm_modem_fops = {
745 .owner = THIS_MODULE,
746 .open = mdm_modem_open,
747 .unlocked_ioctl = mdm_modem_ioctl,
748};
749
750static void mdm_modem_initialize_data(struct platform_device *pdev,
751 struct mdm_device *mdev)
Joel Kingb6f0f612011-11-01 16:59:14 -0700752{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800753 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
Joel Kingb6f0f612011-11-01 16:59:14 -0700754 struct resource *pres;
755
Ameya Thakur552e58a2013-01-30 11:33:22 -0800756 mdm_drv->pdata = pdev->dev.platform_data;
757 if (pdev->id < 0)
758 mdm_drv->device_id = 0;
759 else
760 mdm_drv->device_id = pdev->id;
761
762 memset((void *)&mdev->mdm_subsys, 0,
763 sizeof(struct subsys_desc));
764 if (mdev->mdm_data.device_id <= 0)
765 snprintf(mdev->subsys_name, sizeof(mdev->subsys_name),
766 "%s", EXTERNAL_MODEM);
767 else
768 snprintf(mdev->subsys_name, sizeof(mdev->subsys_name),
769 "%s.%d", EXTERNAL_MODEM, mdev->mdm_data.device_id);
770 mdev->mdm_subsys.shutdown = mdm_subsys_shutdown;
771 mdev->mdm_subsys.ramdump = mdm_subsys_ramdumps;
772 mdev->mdm_subsys.powerup = mdm_subsys_powerup;
773 mdev->mdm_subsys.name = mdev->subsys_name;
774
775 memset((void *)&mdev->misc_device, 0,
776 sizeof(struct miscdevice));
777 if (mdev->mdm_data.device_id <= 0)
778 snprintf(mdev->device_name, sizeof(mdev->device_name),
779 "%s", DEVICE_BASE_NAME);
780 else
781 snprintf(mdev->device_name, sizeof(mdev->device_name),
782 "%s%d", DEVICE_BASE_NAME, mdev->mdm_data.device_id);
783 mdev->misc_device.minor = MISC_DYNAMIC_MINOR;
784 mdev->misc_device.name = mdev->device_name;
785 mdev->misc_device.fops = &mdm_modem_fops;
786
787 memset((void *)&mdev->mdm_panic_blk, 0,
788 sizeof(struct notifier_block));
789 mdev->mdm_panic_blk.notifier_call = mdm_panic_prep;
790 atomic_notifier_chain_register(&panic_notifier_list,
791 &mdev->mdm_panic_blk);
792
Joel Kingb6f0f612011-11-01 16:59:14 -0700793 /* MDM2AP_ERRFATAL */
794 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
795 "MDM2AP_ERRFATAL");
Ameya Thakure155ece2012-07-09 12:08:37 -0700796 mdm_drv->mdm2ap_errfatal_gpio = pres ? pres->start : -1;
Joel Kingb6f0f612011-11-01 16:59:14 -0700797
798 /* AP2MDM_ERRFATAL */
799 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
800 "AP2MDM_ERRFATAL");
Ameya Thakure155ece2012-07-09 12:08:37 -0700801 mdm_drv->ap2mdm_errfatal_gpio = pres ? pres->start : -1;
Joel Kingb6f0f612011-11-01 16:59:14 -0700802
803 /* MDM2AP_STATUS */
804 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
805 "MDM2AP_STATUS");
Ameya Thakure155ece2012-07-09 12:08:37 -0700806 mdm_drv->mdm2ap_status_gpio = pres ? pres->start : -1;
Joel Kingb6f0f612011-11-01 16:59:14 -0700807
808 /* AP2MDM_STATUS */
809 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
810 "AP2MDM_STATUS");
Ameya Thakure155ece2012-07-09 12:08:37 -0700811 mdm_drv->ap2mdm_status_gpio = pres ? pres->start : -1;
Joel Kingb6f0f612011-11-01 16:59:14 -0700812
813 /* MDM2AP_WAKEUP */
814 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
815 "MDM2AP_WAKEUP");
Ameya Thakure155ece2012-07-09 12:08:37 -0700816 mdm_drv->mdm2ap_wakeup_gpio = pres ? pres->start : -1;
Joel Kingb6f0f612011-11-01 16:59:14 -0700817
818 /* AP2MDM_WAKEUP */
819 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
820 "AP2MDM_WAKEUP");
Ameya Thakure155ece2012-07-09 12:08:37 -0700821 mdm_drv->ap2mdm_wakeup_gpio = pres ? pres->start : -1;
Joel Kingb6f0f612011-11-01 16:59:14 -0700822
Joel Kinge92eb872012-05-06 09:30:24 -0700823 /* AP2MDM_SOFT_RESET */
Joel Kingb6f0f612011-11-01 16:59:14 -0700824 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
Joel Kinge92eb872012-05-06 09:30:24 -0700825 "AP2MDM_SOFT_RESET");
Ameya Thakure155ece2012-07-09 12:08:37 -0700826 mdm_drv->ap2mdm_soft_reset_gpio = pres ? pres->start : -1;
Joel Kingb6f0f612011-11-01 16:59:14 -0700827
828 /* AP2MDM_KPDPWR_N */
829 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
830 "AP2MDM_KPDPWR_N");
Ameya Thakure155ece2012-07-09 12:08:37 -0700831 mdm_drv->ap2mdm_kpdpwr_n_gpio = pres ? pres->start : -1;
Joel Kingb6f0f612011-11-01 16:59:14 -0700832
Joel Kinge92eb872012-05-06 09:30:24 -0700833 /* AP2MDM_PMIC_PWR_EN */
834 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
835 "AP2MDM_PMIC_PWR_EN");
Ameya Thakure155ece2012-07-09 12:08:37 -0700836 mdm_drv->ap2mdm_pmic_pwr_en_gpio = pres ? pres->start : -1;
Joel Kinge92eb872012-05-06 09:30:24 -0700837
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700838 /* MDM2AP_PBLRDY */
839 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
840 "MDM2AP_PBLRDY");
Ameya Thakure155ece2012-07-09 12:08:37 -0700841 mdm_drv->mdm2ap_pblrdy = pres ? pres->start : -1;
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700842
Ameya Thakur43248fd2012-07-10 18:50:52 -0700843 /*USB_SW*/
844 pres = platform_get_resource_byname(pdev, IORESOURCE_IO,
845 "USB_SW");
Ameya Thakure155ece2012-07-09 12:08:37 -0700846 mdm_drv->usb_switch_gpio = pres ? pres->start : -1;
Ameya Thakur43248fd2012-07-10 18:50:52 -0700847
Joel Kingb6f0f612011-11-01 16:59:14 -0700848 mdm_drv->boot_type = CHARM_NORMAL_BOOT;
849
Ameya Thakur552e58a2013-01-30 11:33:22 -0800850 mdm_drv->dump_timeout_ms = mdm_drv->pdata->ramdump_timeout_ms > 0 ?
Ameya Thakurc9a7a842012-06-24 22:47:52 -0700851 mdm_drv->pdata->ramdump_timeout_ms : MDM_RDUMP_TIMEOUT;
Ameya Thakur552e58a2013-01-30 11:33:22 -0800852
853 init_completion(&mdev->mdm_needs_reload);
854 init_completion(&mdev->mdm_boot);
855 init_completion(&mdev->mdm_ram_dumps);
856
857 mdev->first_boot = 1;
858 mutex_init(&mdm_drv->peripheral_status_lock);
Joel Kingb6f0f612011-11-01 16:59:14 -0700859}
860
Ameya Thakur552e58a2013-01-30 11:33:22 -0800861static void mdm_deconfigure_ipc(struct mdm_device *mdev)
Joel Kingb6f0f612011-11-01 16:59:14 -0700862{
Ameya Thakur552e58a2013-01-30 11:33:22 -0800863 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
Joel Kingb6f0f612011-11-01 16:59:14 -0700864
Ameya Thakur552e58a2013-01-30 11:33:22 -0800865 gpio_free(mdm_drv->ap2mdm_status_gpio);
866 gpio_free(mdm_drv->ap2mdm_errfatal_gpio);
867 if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio))
868 gpio_free(mdm_drv->ap2mdm_kpdpwr_n_gpio);
869 if (GPIO_IS_VALID(mdm_drv->ap2mdm_pmic_pwr_en_gpio))
870 gpio_free(mdm_drv->ap2mdm_pmic_pwr_en_gpio);
871 gpio_free(mdm_drv->mdm2ap_status_gpio);
872 gpio_free(mdm_drv->mdm2ap_errfatal_gpio);
873 if (GPIO_IS_VALID(mdm_drv->ap2mdm_soft_reset_gpio))
874 gpio_free(mdm_drv->ap2mdm_soft_reset_gpio);
875
876 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
877 gpio_free(mdm_drv->ap2mdm_wakeup_gpio);
878
879 if (mdev->mdm_queue) {
880 destroy_workqueue(mdev->mdm_queue);
881 mdev->mdm_queue = NULL;
Joel Kingb6f0f612011-11-01 16:59:14 -0700882 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800883 if (mdev->mdm_sfr_queue) {
884 destroy_workqueue(mdev->mdm_sfr_queue);
885 mdev->mdm_sfr_queue = NULL;
886 }
887}
Joel Kingb6f0f612011-11-01 16:59:14 -0700888
Ameya Thakur552e58a2013-01-30 11:33:22 -0800889static int mdm_configure_ipc(struct mdm_device *mdev)
890{
891 struct mdm_modem_drv *mdm_drv = &mdev->mdm_data;
892 int ret = -1, irq;
Joel Kingb6f0f612011-11-01 16:59:14 -0700893
894 gpio_request(mdm_drv->ap2mdm_status_gpio, "AP2MDM_STATUS");
895 gpio_request(mdm_drv->ap2mdm_errfatal_gpio, "AP2MDM_ERRFATAL");
Ameya Thakure155ece2012-07-09 12:08:37 -0700896 if (GPIO_IS_VALID(mdm_drv->ap2mdm_kpdpwr_n_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700897 gpio_request(mdm_drv->ap2mdm_kpdpwr_n_gpio, "AP2MDM_KPDPWR_N");
Joel Kingb6f0f612011-11-01 16:59:14 -0700898 gpio_request(mdm_drv->mdm2ap_status_gpio, "MDM2AP_STATUS");
899 gpio_request(mdm_drv->mdm2ap_errfatal_gpio, "MDM2AP_ERRFATAL");
Ameya Thakure155ece2012-07-09 12:08:37 -0700900 if (GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy))
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700901 gpio_request(mdm_drv->mdm2ap_pblrdy, "MDM2AP_PBLRDY");
Joel Kingb6f0f612011-11-01 16:59:14 -0700902
Ameya Thakure155ece2012-07-09 12:08:37 -0700903 if (GPIO_IS_VALID(mdm_drv->ap2mdm_pmic_pwr_en_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700904 gpio_request(mdm_drv->ap2mdm_pmic_pwr_en_gpio,
905 "AP2MDM_PMIC_PWR_EN");
Ameya Thakure155ece2012-07-09 12:08:37 -0700906 if (GPIO_IS_VALID(mdm_drv->ap2mdm_soft_reset_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -0700907 gpio_request(mdm_drv->ap2mdm_soft_reset_gpio,
908 "AP2MDM_SOFT_RESET");
909
Ameya Thakure155ece2012-07-09 12:08:37 -0700910 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kingb6f0f612011-11-01 16:59:14 -0700911 gpio_request(mdm_drv->ap2mdm_wakeup_gpio, "AP2MDM_WAKEUP");
912
Ameya Thakure155ece2012-07-09 12:08:37 -0700913 if (GPIO_IS_VALID(mdm_drv->usb_switch_gpio)) {
Ameya Thakur43248fd2012-07-10 18:50:52 -0700914 if (gpio_request(mdm_drv->usb_switch_gpio, "USB_SW")) {
915 pr_err("%s Failed to get usb switch gpio\n", __func__);
916 mdm_drv->usb_switch_gpio = -1;
917 }
918 }
Taniya Dasa1a14a92013-01-21 15:14:15 +0530919 gpio_direction_output(mdm_drv->ap2mdm_status_gpio, 0);
Joel Kingb6f0f612011-11-01 16:59:14 -0700920 gpio_direction_output(mdm_drv->ap2mdm_errfatal_gpio, 0);
921
Ameya Thakure155ece2012-07-09 12:08:37 -0700922 if (GPIO_IS_VALID(mdm_drv->ap2mdm_wakeup_gpio))
Joel Kingb6f0f612011-11-01 16:59:14 -0700923 gpio_direction_output(mdm_drv->ap2mdm_wakeup_gpio, 0);
924
925 gpio_direction_input(mdm_drv->mdm2ap_status_gpio);
926 gpio_direction_input(mdm_drv->mdm2ap_errfatal_gpio);
927
Ameya Thakur552e58a2013-01-30 11:33:22 -0800928 mdev->mdm_queue = alloc_workqueue("mdm_queue", 0, 0);
929 if (!mdev->mdm_queue) {
930 pr_err("%s: could not create mdm_queue for mdm id %d\n",
931 __func__, mdev->mdm_data.device_id);
Joel Kingb6f0f612011-11-01 16:59:14 -0700932 ret = -ENOMEM;
933 goto fatal_err;
934 }
935
Ameya Thakur552e58a2013-01-30 11:33:22 -0800936 mdev->mdm_sfr_queue = alloc_workqueue("mdm_sfr_queue", 0, 0);
937 if (!mdev->mdm_sfr_queue) {
938 pr_err("%s: could not create mdm_sfr_queue for mdm id %d\n",
939 __func__, mdev->mdm_data.device_id);
Joel King30fdd662012-05-07 19:39:29 -0700940 ret = -ENOMEM;
Joel King30fdd662012-05-07 19:39:29 -0700941 goto fatal_err;
942 }
943
Joel Kingb6f0f612011-11-01 16:59:14 -0700944 /* Register subsystem handlers */
Ameya Thakur552e58a2013-01-30 11:33:22 -0800945 mdev->mdm_subsys_dev = subsys_register(&mdev->mdm_subsys);
946 if (IS_ERR(mdev->mdm_subsys_dev)) {
947 ret = PTR_ERR(mdev->mdm_subsys_dev);
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700948 goto fatal_err;
949 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800950 subsys_default_online(mdev->mdm_subsys_dev);
Joel Kingb6f0f612011-11-01 16:59:14 -0700951
952 /* ERR_FATAL irq. */
Ameya Thakur552e58a2013-01-30 11:33:22 -0800953 irq = gpio_to_irq(mdm_drv->mdm2ap_errfatal_gpio);
Joel Kingb6f0f612011-11-01 16:59:14 -0700954 if (irq < 0) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800955 pr_err("%s: bad MDM2AP_ERRFATAL IRQ resource, err = %d\n",
956 __func__, irq);
Joel Kingb6f0f612011-11-01 16:59:14 -0700957 goto errfatal_err;
958 }
959 ret = request_irq(irq, mdm_errfatal,
Ameya Thakur552e58a2013-01-30 11:33:22 -0800960 IRQF_TRIGGER_RISING , "mdm errfatal", mdev);
Joel Kingb6f0f612011-11-01 16:59:14 -0700961
962 if (ret < 0) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800963 pr_err("%s: MDM2AP_ERRFATAL IRQ#%d request failed, err=%d\n",
964 __func__, irq, ret);
Joel Kingb6f0f612011-11-01 16:59:14 -0700965 goto errfatal_err;
966 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800967 mdev->mdm_errfatal_irq = irq;
Joel Kingb6f0f612011-11-01 16:59:14 -0700968
969errfatal_err:
970
Ameya Thakur552e58a2013-01-30 11:33:22 -0800971 /* status irq */
972 irq = gpio_to_irq(mdm_drv->mdm2ap_status_gpio);
Joel Kingb6f0f612011-11-01 16:59:14 -0700973 if (irq < 0) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800974 pr_err("%s: bad MDM2AP_STATUS IRQ resource, err = %d\n",
975 __func__, irq);
Joel Kingb6f0f612011-11-01 16:59:14 -0700976 goto status_err;
977 }
978
979 ret = request_threaded_irq(irq, NULL, mdm_status_change,
Vamsi Krishna33925632011-12-13 15:43:09 -0800980 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_SHARED,
Ameya Thakur552e58a2013-01-30 11:33:22 -0800981 "mdm status", mdev);
Joel Kingb6f0f612011-11-01 16:59:14 -0700982
983 if (ret < 0) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800984 pr_err("%s: MDM2AP_STATUS IRQ#%d request failed, err=%d",
985 __func__, irq, ret);
Joel Kingb6f0f612011-11-01 16:59:14 -0700986 goto status_err;
987 }
Ameya Thakur552e58a2013-01-30 11:33:22 -0800988 mdev->mdm_status_irq = irq;
Joel Kingb6f0f612011-11-01 16:59:14 -0700989
990status_err:
Ameya Thakure155ece2012-07-09 12:08:37 -0700991 if (GPIO_IS_VALID(mdm_drv->mdm2ap_pblrdy)) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800992 irq = gpio_to_irq(mdm_drv->mdm2ap_pblrdy);
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700993 if (irq < 0) {
Ameya Thakur552e58a2013-01-30 11:33:22 -0800994 pr_err("%s: could not get MDM2AP_PBLRDY IRQ resource\n",
995 __func__);
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -0700996 goto pblrdy_err;
997 }
998
999 ret = request_threaded_irq(irq, NULL, mdm_pblrdy_change,
Ameya Thakur552e58a2013-01-30 11:33:22 -08001000 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
1001 IRQF_SHARED,
1002 "mdm pbl ready", mdev);
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -07001003
1004 if (ret < 0) {
Ameya Thakur552e58a2013-01-30 11:33:22 -08001005 pr_err("%s: MDM2AP_PBL IRQ#%d request failed error=%d\n",
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -07001006 __func__, irq, ret);
1007 goto pblrdy_err;
1008 }
Ameya Thakur552e58a2013-01-30 11:33:22 -08001009 mdev->mdm_pblrdy_irq = irq;
Vamsi Krishnac6dcd5e2012-05-09 15:38:01 -07001010 }
1011
1012pblrdy_err:
Joel Kinge92eb872012-05-06 09:30:24 -07001013 /*
1014 * If AP2MDM_PMIC_PWR_EN gpio is used, pull it high. It remains
1015 * high until the whole phone is shut down.
1016 */
Ameya Thakure155ece2012-07-09 12:08:37 -07001017 if (GPIO_IS_VALID(mdm_drv->ap2mdm_pmic_pwr_en_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -07001018 gpio_direction_output(mdm_drv->ap2mdm_pmic_pwr_en_gpio, 1);
1019
Ameya Thakur552e58a2013-01-30 11:33:22 -08001020 return 0;
Joel Kingb6f0f612011-11-01 16:59:14 -07001021
1022fatal_err:
Ameya Thakur552e58a2013-01-30 11:33:22 -08001023 mdm_deconfigure_ipc(mdev);
Joel Kingb6f0f612011-11-01 16:59:14 -07001024 return ret;
1025}
1026
Ameya Thakur552e58a2013-01-30 11:33:22 -08001027static int __devinit mdm_modem_probe(struct platform_device *pdev)
1028{
1029 struct mdm_device *mdev = NULL;
1030 int ret = -1;
1031
1032 mdev = kzalloc(sizeof(struct mdm_device), GFP_KERNEL);
1033 if (!mdev) {
1034 pr_err("%s: kzalloc fail.\n", __func__);
1035 ret = -ENOMEM;
1036 goto init_err;
1037 }
1038
Pavankumar Kondeti0161cf92013-02-12 18:10:21 +05301039 platform_set_drvdata(pdev, mdev);
Ameya Thakur552e58a2013-01-30 11:33:22 -08001040 mdm_modem_initialize_data(pdev, mdev);
1041
1042 if (mdm_ops->debug_state_changed_cb)
1043 mdm_ops->debug_state_changed_cb(mdm_debug_mask);
1044
1045 if (mdm_configure_ipc(mdev)) {
1046 pr_err("%s: mdm_configure_ipc failed, id = %d\n",
1047 __func__, mdev->mdm_data.device_id);
1048 goto init_err;
1049 }
1050
1051 pr_debug("%s: Registering mdm id %d\n", __func__,
1052 mdev->mdm_data.device_id);
1053 ret = misc_register(&mdev->misc_device);
1054 if (ret) {
1055 pr_err("%s: failed registering mdm id %d, ret = %d\n",
1056 __func__, mdev->mdm_data.device_id, ret);
1057 mdm_deconfigure_ipc(mdev);
1058 goto init_err;
1059 } else {
1060 pr_err("%s: registered mdm id %d\n",
1061 __func__, mdev->mdm_data.device_id);
1062
1063 mdm_device_list_add(mdev);
1064 INIT_DELAYED_WORK(&mdev->mdm2ap_status_check_work,
1065 mdm2ap_status_check);
1066 INIT_WORK(&mdev->mdm_status_work, mdm_status_fn);
1067 INIT_WORK(&mdev->sfr_reason_work, mdm_restart_reason_fn);
1068
1069 /* Perform early powerup of the external modem in order to
1070 * allow tabla devices to be found.
1071 */
1072 if (mdev->mdm_data.pdata->early_power_on)
1073 mdm_ops->power_on_mdm_cb(&mdev->mdm_data);
1074 }
1075
1076 return ret;
1077
1078init_err:
1079 kfree(mdev);
1080 return ret;
1081}
1082
1083static int __devexit mdm_modem_remove(struct platform_device *pdev)
Joel Kingb6f0f612011-11-01 16:59:14 -07001084{
1085 int ret;
Pavankumar Kondeti0161cf92013-02-12 18:10:21 +05301086 struct mdm_device *mdev = platform_get_drvdata(pdev);
Joel Kingb6f0f612011-11-01 16:59:14 -07001087
Ameya Thakur552e58a2013-01-30 11:33:22 -08001088 pr_debug("%s: removing device id %d\n",
1089 __func__, mdev->mdm_data.device_id);
1090 mdm_deconfigure_ipc(mdev);
1091 ret = misc_deregister(&mdev->misc_device);
1092 mdm_device_list_remove(mdev);
1093 kfree(mdev);
Joel Kingb6f0f612011-11-01 16:59:14 -07001094 return ret;
1095}
1096
Ameya Thakur552e58a2013-01-30 11:33:22 -08001097static void mdm_modem_shutdown(struct platform_device *pdev)
Joel Kingb6f0f612011-11-01 16:59:14 -07001098{
Ameya Thakur552e58a2013-01-30 11:33:22 -08001099 struct mdm_modem_drv *mdm_drv;
Pavankumar Kondeti0161cf92013-02-12 18:10:21 +05301100 struct mdm_device *mdev = platform_get_drvdata(pdev);
Joel Kingb6f0f612011-11-01 16:59:14 -07001101
Ameya Thakur552e58a2013-01-30 11:33:22 -08001102 pr_debug("%s: shutting down device id %d\n",
1103 __func__, mdev->mdm_data.device_id);
1104
1105 mdm_disable_irqs(mdev);
1106 mdm_drv = &mdev->mdm_data;
1107 mdm_ops->power_down_mdm_cb(mdm_drv);
Ameya Thakure155ece2012-07-09 12:08:37 -07001108 if (GPIO_IS_VALID(mdm_drv->ap2mdm_pmic_pwr_en_gpio))
Joel Kinge92eb872012-05-06 09:30:24 -07001109 gpio_direction_output(mdm_drv->ap2mdm_pmic_pwr_en_gpio, 0);
Joel Kingb6f0f612011-11-01 16:59:14 -07001110}
1111
Ameya Thakur552e58a2013-01-30 11:33:22 -08001112static struct of_device_id mdm_match_table[] = {
1113 {.compatible = "qcom,mdm2_modem,mdm2_modem.1"},
1114 {},
1115};
1116
1117static struct platform_driver mdm_modem_driver = {
1118 .probe = mdm_modem_probe,
1119 .remove = __devexit_p(mdm_modem_remove),
1120 .shutdown = mdm_modem_shutdown,
1121 .driver = {
1122 .name = "mdm2_modem",
1123 .owner = THIS_MODULE,
1124 .of_match_table = mdm_match_table,
1125 },
1126};
1127
1128static int __init mdm_modem_init(void)
1129{
1130 int ret;
1131
1132 ret = mdm_get_ops(&mdm_ops);
1133 if (ret)
1134 return ret;
1135
1136 INIT_LIST_HEAD(&mdm_devices);
1137 mdm_debugfs_init();
1138 return platform_driver_register(&mdm_modem_driver);
1139}
1140
1141static void __exit mdm_modem_exit(void)
1142{
1143 platform_driver_unregister(&mdm_modem_driver);
1144}
1145
1146module_init(mdm_modem_init);
1147module_exit(mdm_modem_exit);
1148
1149MODULE_LICENSE("GPL v2");
1150MODULE_DESCRIPTION("mdm modem driver");
1151MODULE_VERSION("2.0");
1152MODULE_ALIAS("mdm_modem");