blob: e2ff0da0e7754865685443d5837f6cc08acd0819 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
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>
Willie Ruan20667832011-08-03 13:36:31 -070032#include <linux/mfd/pmic8058.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <asm/mach-types.h>
34#include <asm/uaccess.h>
35#include <mach/mdm.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
44#define CHARM_MODEM_TIMEOUT 6000
45#define CHARM_HOLD_TIME 4000
46#define CHARM_MODEM_DELTA 100
47
48static void (*power_on_charm)(void);
49static void (*power_down_charm)(void);
50
51static int charm_debug_on;
52static int charm_status_irq;
53static int charm_errfatal_irq;
54static int charm_ready;
55static enum charm_boot_type boot_type = CHARM_NORMAL_BOOT;
56static int charm_boot_status;
57static int charm_ram_dump_status;
58static struct workqueue_struct *charm_queue;
59
60#define CHARM_DBG(...) do { if (charm_debug_on) \
61 pr_info(__VA_ARGS__); \
62 } while (0);
63
64
65DECLARE_COMPLETION(charm_needs_reload);
66DECLARE_COMPLETION(charm_boot);
67DECLARE_COMPLETION(charm_ram_dumps);
68
69static void charm_disable_irqs(void)
70{
71 disable_irq_nosync(charm_errfatal_irq);
72 disable_irq_nosync(charm_status_irq);
73
74}
75
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076static int charm_subsys_shutdown(const struct subsys_data *crashed_subsys)
77{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078 charm_ready = 0;
Laura Abbottd161daf2011-08-10 17:24:48 -070079 power_down_charm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080 return 0;
81}
82
83static int charm_subsys_powerup(const struct subsys_data *crashed_subsys)
84{
85 power_on_charm();
86 boot_type = CHARM_NORMAL_BOOT;
87 complete(&charm_needs_reload);
88 wait_for_completion(&charm_boot);
89 pr_info("%s: charm modem has been restarted\n", __func__);
90 INIT_COMPLETION(charm_boot);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 return charm_boot_status;
92}
93
94static int charm_subsys_ramdumps(int want_dumps,
95 const struct subsys_data *crashed_subsys)
96{
97 charm_ram_dump_status = 0;
98 if (want_dumps) {
99 boot_type = CHARM_RAM_DUMPS;
100 complete(&charm_needs_reload);
101 wait_for_completion(&charm_ram_dumps);
102 INIT_COMPLETION(charm_ram_dumps);
103 power_down_charm();
104 }
105 return charm_ram_dump_status;
106}
107
108static struct subsys_data charm_subsystem = {
109 .shutdown = charm_subsys_shutdown,
110 .ramdump = charm_subsys_ramdumps,
111 .powerup = charm_subsys_powerup,
112 .name = "external_modem",
113};
114
115static int charm_panic_prep(struct notifier_block *this,
116 unsigned long event, void *ptr)
117{
118 CHARM_DBG("%s: setting AP2MDM_ERRFATAL high for a non graceful reset\n",
119 __func__);
120 charm_disable_irqs();
121 gpio_set_value(AP2MDM_ERRFATAL, 1);
122 return NOTIFY_DONE;
123}
124
125static struct notifier_block charm_panic_blk = {
126 .notifier_call = charm_panic_prep,
127};
128
Laura Abbottd42d1202011-07-20 14:49:57 -0700129static int first_boot = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
131static long charm_modem_ioctl(struct file *filp, unsigned int cmd,
132 unsigned long arg)
133{
134
135 int status, ret = 0;
136
137 if (_IOC_TYPE(cmd) != CHARM_CODE) {
138 pr_err("%s: invalid ioctl code\n", __func__);
139 return -EINVAL;
140 }
141
142 CHARM_DBG("%s: Entering ioctl cmd = %d\n", __func__, _IOC_NR(cmd));
143 switch (cmd) {
144 case WAKE_CHARM:
145 CHARM_DBG("%s: Powering on\n", __func__);
146 power_on_charm();
147 break;
148 case CHECK_FOR_BOOT:
149 if (gpio_get_value(MDM2AP_STATUS) == 0)
150 put_user(1, (unsigned long __user *) arg);
151 else
152 put_user(0, (unsigned long __user *) arg);
153 break;
154 case NORMAL_BOOT_DONE:
155 CHARM_DBG("%s: check if charm is booted up\n", __func__);
156 get_user(status, (unsigned long __user *) arg);
157 if (status)
158 charm_boot_status = -EIO;
159 else
160 charm_boot_status = 0;
Laura Abbotta267ea92011-07-15 19:53:05 -0700161 charm_ready = 1;
Laura Abbottd42d1202011-07-20 14:49:57 -0700162
163 if (!first_boot)
164 complete(&charm_boot);
165 else
166 first_boot = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 break;
168 case RAM_DUMP_DONE:
169 CHARM_DBG("%s: charm done collecting RAM dumps\n", __func__);
170 get_user(status, (unsigned long __user *) arg);
171 if (status)
172 charm_ram_dump_status = -EIO;
173 else
174 charm_ram_dump_status = 0;
175 complete(&charm_ram_dumps);
176 break;
177 case WAIT_FOR_RESTART:
178 CHARM_DBG("%s: wait for charm to need images reloaded\n",
179 __func__);
180 ret = wait_for_completion_interruptible(&charm_needs_reload);
181 if (!ret)
182 put_user(boot_type, (unsigned long __user *) arg);
183 INIT_COMPLETION(charm_needs_reload);
184 break;
185 default:
186 pr_err("%s: invalid ioctl cmd = %d\n", __func__, _IOC_NR(cmd));
187 ret = -EINVAL;
188 break;
189 }
190
191 return ret;
192}
193
194static int charm_modem_open(struct inode *inode, struct file *file)
195{
196 return 0;
197}
198
199static const struct file_operations charm_modem_fops = {
200 .owner = THIS_MODULE,
201 .open = charm_modem_open,
202 .unlocked_ioctl = charm_modem_ioctl,
203};
204
205
206struct miscdevice charm_modem_misc = {
207 .minor = MISC_DYNAMIC_MINOR,
208 .name = "mdm",
209 .fops = &charm_modem_fops
210};
211
212
213
214static void charm_status_fn(struct work_struct *work)
215{
216 pr_info("Reseting the charm because status changed\n");
217 subsystem_restart("external_modem");
218}
219
220static DECLARE_WORK(charm_status_work, charm_status_fn);
221
222static void charm_fatal_fn(struct work_struct *work)
223{
224 pr_info("Reseting the charm due to an errfatal\n");
Willie Ruan20667832011-08-03 13:36:31 -0700225 if (get_restart_level() == RESET_SOC)
226 pm8058_stay_on();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 subsystem_restart("external_modem");
228}
229
230static DECLARE_WORK(charm_fatal_work, charm_fatal_fn);
231
232static irqreturn_t charm_errfatal(int irq, void *dev_id)
233{
234 CHARM_DBG("%s: charm got errfatal interrupt\n", __func__);
Laura Abbottd161daf2011-08-10 17:24:48 -0700235 if (charm_ready && (gpio_get_value(MDM2AP_STATUS) == 1)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 CHARM_DBG("%s: scheduling work now\n", __func__);
237 queue_work(charm_queue, &charm_fatal_work);
238 }
239 return IRQ_HANDLED;
240}
241
242static irqreturn_t charm_status_change(int irq, void *dev_id)
243{
244 CHARM_DBG("%s: charm sent status change interrupt\n", __func__);
245 if ((gpio_get_value(MDM2AP_STATUS) == 0) && charm_ready) {
246 CHARM_DBG("%s: scheduling work now\n", __func__);
247 queue_work(charm_queue, &charm_status_work);
248 } else if (gpio_get_value(MDM2AP_STATUS) == 1) {
249 CHARM_DBG("%s: charm is now ready\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 }
251 return IRQ_HANDLED;
252}
253
254static int charm_debug_on_set(void *data, u64 val)
255{
256 charm_debug_on = val;
257 return 0;
258}
259
260static int charm_debug_on_get(void *data, u64 *val)
261{
262 *val = charm_debug_on;
263 return 0;
264}
265
266DEFINE_SIMPLE_ATTRIBUTE(charm_debug_on_fops,
267 charm_debug_on_get,
268 charm_debug_on_set, "%llu\n");
269
270static int charm_debugfs_init(void)
271{
272 struct dentry *dent;
273
274 dent = debugfs_create_dir("charm_dbg", 0);
275 if (IS_ERR(dent))
276 return PTR_ERR(dent);
277
278 debugfs_create_file("debug_on", 0644, dent, NULL,
279 &charm_debug_on_fops);
280 return 0;
281}
282
283static int gsbi9_uart_notifier_cb(struct notifier_block *this,
284 unsigned long code, void *_cmd)
285{
286 switch (code) {
287 case SUBSYS_AFTER_SHUTDOWN:
288 platform_device_unregister(msm_device_uart_gsbi9);
289 msm_device_uart_gsbi9 = msm_add_gsbi9_uart();
290 if (IS_ERR(msm_device_uart_gsbi9))
291 pr_err("%s(): Failed to create uart gsbi9 device\n",
292 __func__);
293 default:
294 break;
295 }
296 return NOTIFY_DONE;
297}
298
299static struct notifier_block gsbi9_nb = {
300 .notifier_call = gsbi9_uart_notifier_cb,
301};
302
303static int __init charm_modem_probe(struct platform_device *pdev)
304{
305 int ret, irq;
306 struct charm_platform_data *d = pdev->dev.platform_data;
307
308 gpio_request(AP2MDM_STATUS, "AP2MDM_STATUS");
309 gpio_request(AP2MDM_ERRFATAL, "AP2MDM_ERRFATAL");
310 gpio_request(AP2MDM_KPDPWR_N, "AP2MDM_KPDPWR_N");
311 gpio_request(AP2MDM_PMIC_RESET_N, "AP2MDM_PMIC_RESET_N");
312 gpio_request(MDM2AP_STATUS, "MDM2AP_STATUS");
313 gpio_request(MDM2AP_ERRFATAL, "MDM2AP_ERRFATAL");
Laura Abbottd2a3bb52011-07-21 12:39:22 -0700314 gpio_request(AP2MDM_WAKEUP, "AP2MDM_WAKEUP");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315
316 gpio_direction_output(AP2MDM_STATUS, 1);
317 gpio_direction_output(AP2MDM_ERRFATAL, 0);
Laura Abbottd2a3bb52011-07-21 12:39:22 -0700318 gpio_direction_output(AP2MDM_WAKEUP, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 gpio_direction_input(MDM2AP_STATUS);
320 gpio_direction_input(MDM2AP_ERRFATAL);
321
322 power_on_charm = d->charm_modem_on;
323 power_down_charm = d->charm_modem_off;
324
325 charm_queue = create_singlethread_workqueue("charm_queue");
326 if (!charm_queue) {
327 pr_err("%s: could not create workqueue. All charm \
328 functionality will be disabled\n",
329 __func__);
330 ret = -ENOMEM;
331 goto fatal_err;
332 }
333
334 atomic_notifier_chain_register(&panic_notifier_list, &charm_panic_blk);
335 charm_debugfs_init();
336
337 ssr_register_subsystem(&charm_subsystem);
338
339 irq = platform_get_irq(pdev, 0);
340 if (irq < 0) {
341 pr_err("%s: could not get MDM2AP_ERRFATAL IRQ resource. \
342 error=%d No IRQ will be generated on errfatal.",
343 __func__, irq);
344 goto errfatal_err;
345 }
346
347 ret = request_irq(irq, charm_errfatal,
348 IRQF_TRIGGER_RISING , "charm errfatal", NULL);
349
350 if (ret < 0) {
351 pr_err("%s: MDM2AP_ERRFATAL IRQ#%d request failed with error=%d\
352 . No IRQ will be generated on errfatal.",
353 __func__, irq, ret);
354 goto errfatal_err;
355 }
356 charm_errfatal_irq = irq;
357
358errfatal_err:
359
360 irq = platform_get_irq(pdev, 1);
361 if (irq < 0) {
362 pr_err("%s: could not get MDM2AP_STATUS IRQ resource. \
363 error=%d No IRQ will be generated on status change.",
364 __func__, irq);
365 goto status_err;
366 }
367
368 ret = request_threaded_irq(irq, NULL, charm_status_change,
369 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
370 "charm status", NULL);
371
372 if (ret < 0) {
373 pr_err("%s: MDM2AP_STATUS IRQ#%d request failed with error=%d\
374 . No IRQ will be generated on status change.",
375 __func__, irq, ret);
376 goto status_err;
377 }
378 charm_status_irq = irq;
379
380status_err:
381 subsys_notif_register_notifier("external_modem", &gsbi9_nb);
382
383 pr_info("%s: Registering charm modem\n", __func__);
384
385 return misc_register(&charm_modem_misc);
386
387fatal_err:
388 gpio_free(AP2MDM_STATUS);
389 gpio_free(AP2MDM_ERRFATAL);
390 gpio_free(AP2MDM_KPDPWR_N);
391 gpio_free(AP2MDM_PMIC_RESET_N);
392 gpio_free(MDM2AP_STATUS);
393 gpio_free(MDM2AP_ERRFATAL);
394 return ret;
395
396}
397
398
399static int __devexit charm_modem_remove(struct platform_device *pdev)
400{
401 gpio_free(AP2MDM_STATUS);
402 gpio_free(AP2MDM_ERRFATAL);
403 gpio_free(AP2MDM_KPDPWR_N);
404 gpio_free(AP2MDM_PMIC_RESET_N);
405 gpio_free(MDM2AP_STATUS);
406 gpio_free(MDM2AP_ERRFATAL);
407
408 return misc_deregister(&charm_modem_misc);
409}
410
411static void charm_modem_shutdown(struct platform_device *pdev)
412{
413 int i;
414
415 CHARM_DBG("%s: setting AP2MDM_STATUS low for a graceful restart\n",
416 __func__);
417
418 charm_disable_irqs();
419
420 gpio_set_value(AP2MDM_STATUS, 0);
Laura Abbottd2a3bb52011-07-21 12:39:22 -0700421 gpio_set_value(AP2MDM_WAKEUP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
423 for (i = CHARM_MODEM_TIMEOUT; i > 0; i -= CHARM_MODEM_DELTA) {
424 pet_watchdog();
425 msleep(CHARM_MODEM_DELTA);
426 if (gpio_get_value(MDM2AP_STATUS) == 0)
427 break;
428 }
429
430 if (i <= 0) {
431 pr_err("%s: MDM2AP_STATUS never went low.\n",
432 __func__);
433 gpio_direction_output(AP2MDM_PMIC_RESET_N, 1);
434 for (i = CHARM_HOLD_TIME; i > 0; i -= CHARM_MODEM_DELTA) {
435 pet_watchdog();
436 msleep(CHARM_MODEM_DELTA);
437 }
438 gpio_direction_output(AP2MDM_PMIC_RESET_N, 0);
439 }
Laura Abbottd2a3bb52011-07-21 12:39:22 -0700440 gpio_set_value(AP2MDM_WAKEUP, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441}
442
443static struct platform_driver charm_modem_driver = {
444 .remove = charm_modem_remove,
445 .shutdown = charm_modem_shutdown,
446 .driver = {
447 .name = "charm_modem",
448 .owner = THIS_MODULE
449 },
450};
451
452static int __init charm_modem_init(void)
453{
454 return platform_driver_probe(&charm_modem_driver, charm_modem_probe);
455}
456
457static void __exit charm_modem_exit(void)
458{
459 platform_driver_unregister(&charm_modem_driver);
460}
461
462module_init(charm_modem_init);
463module_exit(charm_modem_exit);
464
465MODULE_LICENSE("GPL v2");
466MODULE_DESCRIPTION("msm8660 charm modem driver");
467MODULE_VERSION("1.0");
468MODULE_ALIAS("charm_modem");